| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/image` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/image` block on the server, |
| 10 |
* adding a data-id attribute to the element if core/gallery has added on pre-render. |
| 11 |
* |
| 12 |
* @param array $attributes The block attributes. |
| 13 |
* @param array $content The block content. |
| 14 |
* @return string Returns the block content with the data-id attribute added. |
| 15 |
*/ |
| 16 |
function gutenberg_render_block_core_image( $attributes, $content ) { |
| 17 |
if ( isset( $attributes['data-id'] ) ) { |
| 18 |
// Add the data-id="$id" attribute to the img element |
| 19 |
// to provide backwards compatibility for the Gallery Block, |
| 20 |
// which now wraps Image Blocks within innerBlocks. |
| 21 |
// The data-id attribute is added in a core/gallery `render_block_data` hook. |
| 22 |
$data_id_attribute = 'data-id="' . esc_attr( $attributes['data-id'] ) . '"'; |
| 23 |
if ( ! strpos( $content, $data_id_attribute ) ) { |
| 24 |
$content = str_replace( '<img', '<img ' . $data_id_attribute . ' ', $content ); |
| 25 |
} |
| 26 |
} |
| 27 |
return $content; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Registers the `core/image` block on server. |
| 33 |
*/ |
| 34 |
function gutenberg_register_block_core_image() { |
| 35 |
register_block_type_from_metadata( |
| 36 |
__DIR__ . '/image', |
| 37 |
array( |
| 38 |
'render_callback' => 'gutenberg_render_block_core_image', |
| 39 |
) |
| 40 |
); |
| 41 |
} |
| 42 |
add_action( 'init', 'gutenberg_register_block_core_image', 20 ); |
| 43 |
|