-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Implement a mechanism to register all block types from a block metadata collection #8129
base: trunk
Are you sure you want to change the base?
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@gziolo @mreishus When looking at the code as I was starting to work on this, the idea of a new arguments array to also register the block types as part of the existing
Please let me know what you think about this approach. The other thing I'm wondering about is whether we can possibly use this for the Core blocks. The caveat is that many core blocks still provide a custom A potential solution I thought about is that we could allow an optional callback function to dynamically compute additional function get_args_for_block_type( string $block_name ) {
$args = array();
$function_name = 'render_block_core_' . str_replace( '-', '_', $block_name );
if ( function_exists( $function_name ) ) {
$args['render_callback'] = $function_name;
}
$blocks_to_skip_inner_blocks = array( /* Block name list. */ );
if ( in_array( $block_name, $blocks_to_skip_inner_blocks, true ) ) {
$args['skip_inner_blocks'] = true;
}
return $args;
} Do you think something like this would be worthwhile? Or is it overkill and we should instead update to use |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Dumb question, but why is block type registration made so complicated in the first place? Why didn't we support something like the following? foreach ( require 'path/to/blocks-json.php' as $block_type => $metadata ) {
register_block_type_from_metadata( $block_type, $metadata );
} This would help me better understand the context of this PR for doing a review. Right now my first impression is that this is overly complex and that |
$collection = &self::$collections[ $path ]; | ||
|
||
if ( null === $collection['metadata'] ) { | ||
// Load the manifest file if not already loaded |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Load the manifest file if not already loaded | |
// Load the manifest file if not already loaded. |
Why PHPCS didn't show issue 🤔
This PR introduces a new function
wp_register_block_types_from_metadata_collection()
which can be used to register all block types from a block metadata collection.It can be used in two different ways:
wp_register_block_types_from_metadata_collection( WP_PLUGIN_DIR . '/my-plugin/blocks' )
wp_register_block_metadata_collection( WP_PLUGIN_DIR . '/my-plugin/blocks', /* manifest file path */ )
. Otherwise this call would fail with a warning.wp_register_block_types_from_metadata_collection( WP_PLUGIN_DIR . '/my-plugin/blocks', WP_PLUGIN_DIR . '/my-plugin/block-manifest-json.php' )
For additional context on what block metadata collections are and how to use them, see the WordPress 6.7 dev note about block metadata collections.
Trac ticket: https://core.trac.wordpress.org/ticket/62267
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.