Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move image and gallery blocks to dynamic block registration #2009

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/wp-includes/blocks/gallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Server-side rendering of the `core/image` block.
*
* @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.
*
* @param array $parsed_block A single parsed block object.
*
* @return array The migrated block object.
*/
function render_block_core_gallery_data( $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', 'render_block_core_gallery_data' );

/**
* Registers the `core/gallery` block on server.
* This render callback needs to be here
* so that the gallery styles are loaded in block-based themes.
*/
function gutenberg_register_block_core_gallery() {
register_block_type_from_metadata(
__DIR__ . '/gallery',
array(
'render_callback' => function ( $attributes, $content ) {
return $content;
},
)
);
}

add_action( 'init', 'gutenberg_register_block_core_gallery', 20 );
42 changes: 42 additions & 0 deletions src/wp-includes/blocks/image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Server-side rendering of the `core/image` block.
*
* @package WordPress
*/

/**
* Renders the `core/image` block on the server,
* adding a data-id attribute to the element if core/gallery has added on pre-render.
*
* @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 render_block_core_image( $attributes, $content ) {
if ( isset( $attributes['data-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.
// The data-id attribute is added in a core/gallery `render_block_data` hook.
$data_id_attribute = 'data-id="' . esc_attr( $attributes['data-id'] ) . '"';
if ( ! strpos( $content, $data_id_attribute ) ) {
$content = str_replace( '<img', '<img ' . $data_id_attribute . ' ', $content );
}
}
return $content;
}


/**
* Registers the `core/image` block on server.
*/
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' );
4 changes: 2 additions & 2 deletions src/wp-includes/blocks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
require ABSPATH . WPINC . '/blocks/calendar.php';
require ABSPATH . WPINC . '/blocks/categories.php';
require ABSPATH . WPINC . '/blocks/file.php';
require ABSPATH . WPINC . '/blocks/gallery.php';
require ABSPATH . WPINC . '/blocks/image.php';
require ABSPATH . WPINC . '/blocks/latest-comments.php';
require ABSPATH . WPINC . '/blocks/latest-posts.php';
require ABSPATH . WPINC . '/blocks/legacy-widget.php';
Expand Down Expand Up @@ -64,11 +66,9 @@ function register_core_block_types_from_metadata() {
'cover',
'embed',
'freeform',
'gallery',
'group',
'heading',
'html',
'image',
'list',
'media-text',
'missing',
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ function _unhook_block_registration() {
remove_action( 'init', 'register_block_core_calendar' );
remove_action( 'init', 'register_block_core_categories' );
remove_action( 'init', 'register_block_core_file' );
remove_action( 'init', 'gutenberg_register_block_core_gallery', 20 );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo: Remove gutenberg_ prefix.

remove_action( 'init', 'register_block_core_image' );
remove_action( 'init', 'register_block_core_latest_comments' );
remove_action( 'init', 'register_block_core_latest_posts' );
remove_action( 'init', 'register_block_core_legacy_widget' );
Expand Down
4 changes: 2 additions & 2 deletions tools/webpack/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module.exports = function( env = { environment: 'production', watch: false, buil
'calendar',
'categories',
'file',
'gallery',
'image',
'latest-comments',
'latest-posts',
'loginout',
Expand Down Expand Up @@ -73,11 +75,9 @@ module.exports = function( env = { environment: 'production', watch: false, buil
'cover',
'embed',
'freeform',
'gallery',
'group',
'heading',
'html',
'image',
'list',
'media-text',
'missing',
Expand Down