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

Implement a mechanism to register all block types from a block metadata collection #8129

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

felixarntz
Copy link
Member

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:

  • Either to register all block types from a previously registered metadata collection, by referencing its path: wp_register_block_types_from_metadata_collection( WP_PLUGIN_DIR . '/my-plugin/blocks' )
    • In this scenario, the block metadata collection must have been registered prior, via wp_register_block_metadata_collection( WP_PLUGIN_DIR . '/my-plugin/blocks', /* manifest file path */ ). Otherwise this call would fail with a warning.
  • Or by registering a new block metadata collection and then registering all its block types in the same function call: 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.

Copy link

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props flixos90.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@felixarntz
Copy link
Member Author

@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 wp_register_block_metadata_collection() function seemed not ideal to me. I think a new function (like wp_register_block_types_from_metadata_collection()) is a more elegant solution as it separates concerns better. The two functions can be used together, but if someone wants to do it all in one function they can. And while in the latter case, the two functions still get intertwined, at least there is a clear difference in intent:

  • One function's purpose is to register a block metadata collection.
  • The other function's purpose is to register block types from a block metadata collection.

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 render_callback argument that is not possible to handle that way via block.json. Very few Core blocks also specify skip_inner_blocks, but I think those two arguments is all I could find.

A potential solution I thought about is that we could allow an optional callback function to dynamically compute additional $args based on the given block type name. For Core's usage, such a function implementation could look like this:

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 block.json for everything and encourage others to do so too?

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

* order to register the collection. If this parameter is not provided, the `$path` parameter
* must reference a previously registered block metadata collection.
*/
function wp_register_block_types_from_metadata_collection( $path, $manifest = '' ) {
Copy link
Member

Choose a reason for hiding this comment

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

Aside: I don't like that this function & wp_register_block_metadata_collection are prefixed with wp_, while register_block_type isn't. Not great for consistency 😞

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, but too late to change this now, the inconsistency is already there. The guidance that Core functions should be prefixed with wp_ has not always been around, so some inconsistencies like this are unavoidable I guess.

Copy link
Member

Choose a reason for hiding this comment

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

I know, just thinking out loud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants