Skip to content

Commit

Permalink
Try alternative approach
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jul 2, 2024
1 parent 1d23699 commit ad46c2d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
29 changes: 29 additions & 0 deletions packages/block-library/src/gallery/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@
* @package WordPress
*/

/**
* Handles backwards compatibility for Gallery Blocks,
* whose images feature a `data-id` attribute.
*
* Now that the Gallery Block contains inner Image Blocks,
* we add a custom `data-id` attribute before rendering the gallery
* so that the Image Block can pick it up in its render_callback.
*
* @since 5.9.0
*
* @param array $parsed_block The block being rendered.
* @return array The migrated block object.
*/
function block_core_gallery_data_id_backcompatibility( $parsed_block ) {
if ( 'core/gallery' === $parsed_block['blockName'] ) {
foreach ( $parsed_block['innerBlocks'] as $key => $inner_block ) {
if ( 'core/image' === $inner_block['blockName'] ) {
if ( ! isset( $parsed_block['innerBlocks'][ $key ]['attrs']['data-id'] ) && isset( $inner_block['attrs']['id'] ) ) {
$parsed_block['innerBlocks'][ $key ]['attrs']['data-id'] = esc_attr( $inner_block['attrs']['id'] );
}
}
}
}

return $parsed_block;
}

add_filter( 'render_block_data', 'block_core_gallery_data_id_backcompatibility' );

/**
* Renders the `core/gallery` block on the server.
*
Expand Down
52 changes: 27 additions & 25 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,34 @@ function render_block_core_image( $attributes, $content, $block ) {
return '';
}

if ( isset( $attributes['id'] ) ) {
$id = $attributes['id'];

// Use block context to detect whether the block is nested in a gallery.
// As the context names are very generic, check multiple array keys to
// reduce the chance a non-gallery parent is providing the same context.
$is_gallery_child = array_key_exists( 'allowResize', $block->context )
&& array_key_exists( 'imageCrop', $block->context )
&& array_key_exists( 'fixedHeight', $block->context );

// Adds the data-id="$id" attribute to the img element to provide backwards
// compatibility for the Gallery Block, which now wraps Image Blocks within
// innerBlocks. The data-id attribute is added in a core/gallery
// `render_block_data` hook.
if ( $is_gallery_child ) {
$p->set_attribute( 'data-id', $id );
}
/*
* The data-id attribute is added in a core/gallery
* `render_block_data` hook.
*/
if ( isset( $attributes['data-id'] ) ) {
// The initial id when the block was parsed.
$initial_id = $attributes['data-id'];
/*
* The id attribute could change during rendering while applying block bindings.
* If data-id attribute exists, it is safe to assume that
* id attribute is also present.
*/
$rendered_id = $attributes['id'];
/*
* Adds the data-id="$id" attribute to the img element to provide backwards
* compatibility for the Gallery Block, which now wraps Image Blocks within
* innerBlocks.
*/
$p->set_attribute( 'data-id', $rendered_id );

// If there's a mismatch with the 'wp-image-' class and the actual id, the id was
// probably overridden by block bindings. Update it to the correct value.
// See https://github.com/WordPress/gutenberg/issues/62886 for why this is needed.
$image_classnames = $p->get_attribute( 'class' );
$expected_id_class = "wp-image-$id";
if ( is_string( $image_classnames ) && ! str_contains( $image_classnames, $expected_id_class ) ) {
$image_classnames = preg_replace( '/wp-image-(\d+)/', "wp-image-$id", $image_classnames );
$p->set_attribute( 'class', $image_classnames );
/*
* If the id has changed, it was probably overridden by block bindings.
* Update it to the correct value.
* See https://github.com/WordPress/gutenberg/issues/62886 for why this is needed.
*/
if ( $initial_id !== $rendered_id ) {
$p->remove_class( 'wp-image-' . $attributes['data-id'] );
$p->add_class( 'wp-image-' . $rendered_id );
}
}

Expand Down

0 comments on commit ad46c2d

Please sign in to comment.