Skip to content

Commit

Permalink
Reinstating render_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Nov 3, 2021
1 parent 5ac2a85 commit 9f98093
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@
*/

/**
* Adds a data-id attribute to the core Image block when nested in a Gallery block.
* Renders the `core/image` block on the server.
*
* @param WP_Post $post The WP post.
* @return string Returns the post content with the data-id attribute added to gallery images.
* @param array $attributes The block attributes.
* @param array $content The block content.
* @return string Returns the block content with the data-id attribute added.
*/
function get_block_core_image_post_content( $post ) {
if ( is_admin() ) {
return;
}
if ( has_blocks( $post->post_content ) ) {
$content = $post->post_content;
$blocks = parse_blocks( $content );
foreach ( $blocks as $block ) {
if ( 'core/gallery' === $block['blockName'] && ! empty( $block['innerBlocks'] ) ) {
foreach ( $block['innerBlocks'] as $inner_block ) {
if ( 'core/image' === $inner_block['blockName'] ) {
if ( isset( $inner_block['attrs']['id'] ) ) {
$image_id = esc_attr( $inner_block['attrs']['id'] );
$data_id_attribute = 'data-id="' . $image_id . '"';
$class_string = 'class="wp-image-' . $image_id . '"';
$content = str_replace( $class_string, $data_id_attribute . ' ' . $class_string, $content );
}
}
}
}
function render_block_core_image( $attributes, $content ) {
if ( isset( $attributes['id'] ) ) {
// Add the data-id="$id" attribute to the img element
// to provide backwards compatibility for the Gallery Block,
// which now wraps Image Blocks within innerBlocks.
$data_id_attribute = 'data-id="' . esc_attr( $attributes['id'] ) . '"';
if ( ! strpos( $content, $data_id_attribute ) ) {
$content = str_replace( '<img', '<img ' . $data_id_attribute . ' ', $content );
}
}
$post->post_content = $content;
return $content;
}

add_action( 'the_post', 'get_block_core_image_post_content', 10, 1 );

/**
* Register image block.
*/
function register_block_core_image() {
register_block_type_from_metadata(
__DIR__ . '/image',
array(
'render_callback' => 'render_block_core_image',
)
);
}
add_action( 'init', 'register_block_core_image' );

0 comments on commit 9f98093

Please sign in to comment.