mirror of
https://github.com/WordPress/WordPress.git
synced 2025-03-13 07:10:00 +01:00
Editor: Allow registering block type collections with a single function call.
[59132] introduced the `wp_register_block_metadata_collection()` function and underlying `WP_Block_Metadata_Registry` class to allow central registration of a block metadata PHP manifest file in favor of parsing individual JSON files. While this improves performance, it only increases the amount of APIs and code that plugin developers need to use to register their block types properly. This changeset introduces a new function `wp_register_block_types_from_metadata_collection()` that improves the developer experience of registering block types from a single source, by handling it in only a single function call. Developers that already use a generated block metadata PHP manifest file (e.g. via the `wp-scripts build-blocks-manifest` tool) can now call `wp_register_block_types_from_metadata_collection()` with that file to automatically register all block types from that block metadata collection. Individual calls to `register_block_type()` or `register_block_type_from_metadata()` are no longer necessary when the new function is used. Props flixos90, gziolo, joemcgill, mreishus, mukesh27, swissspidy. Fixes #62267. See #62002. Built from https://develop.svn.wordpress.org/trunk@59874 git-svn-id: http://core.svn.wordpress.org/trunk@59216 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
10c16acbb2
commit
2649d14466
@ -375,6 +375,32 @@ function get_block_metadata_i18n_schema() {
|
||||
return $i18n_block_schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers all block types from a block metadata collection.
|
||||
*
|
||||
* This can either reference a previously registered metadata collection or, if the `$manifest` parameter is provided,
|
||||
* register the metadata collection directly within the same function call.
|
||||
*
|
||||
* @since 6.8.0
|
||||
* @see wp_register_block_metadata_collection()
|
||||
* @see register_block_type_from_metadata()
|
||||
*
|
||||
* @param string $path The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ).
|
||||
* @param string $manifest Optional. The absolute path to the manifest file containing the metadata collection, in
|
||||
* 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 = '' ) {
|
||||
if ( $manifest ) {
|
||||
wp_register_block_metadata_collection( $path, $manifest );
|
||||
}
|
||||
|
||||
$block_metadata_files = WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path );
|
||||
foreach ( $block_metadata_files as $block_metadata_file ) {
|
||||
register_block_type_from_metadata( $block_metadata_file );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a block metadata collection.
|
||||
*
|
||||
|
@ -179,6 +179,47 @@ class WP_Block_Metadata_Registry {
|
||||
return isset( $collection['metadata'][ $block_name ] ) ? $collection['metadata'][ $block_name ] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of absolute paths to all block metadata files that are part of the given collection.
|
||||
*
|
||||
* For instance, if a block metadata collection is registered with path `WP_PLUGIN_DIR . '/my-plugin/blocks/'`,
|
||||
* and the manifest file includes metadata for two blocks `'block-a'` and `'block-b'`, the result of this method
|
||||
* will be an array containing:
|
||||
* * `WP_PLUGIN_DIR . '/my-plugin/blocks/block-a/block.json'`
|
||||
* * `WP_PLUGIN_DIR . '/my-plugin/blocks/block-b/block.json'`
|
||||
*
|
||||
* @since 6.8.0
|
||||
*
|
||||
* @param string $path The absolute base path for a previously registered collection.
|
||||
* @return string[] List of block metadata file paths, or an empty array if the given `$path` is invalid.
|
||||
*/
|
||||
public static function get_collection_block_metadata_files( $path ) {
|
||||
$path = wp_normalize_path( rtrim( $path, '/' ) );
|
||||
|
||||
if ( ! isset( self::$collections[ $path ] ) ) {
|
||||
_doing_it_wrong(
|
||||
__METHOD__,
|
||||
__( 'No registered block metadata collection was found for the provided path.' ),
|
||||
'6.8.0'
|
||||
);
|
||||
return array();
|
||||
}
|
||||
|
||||
$collection = &self::$collections[ $path ];
|
||||
|
||||
if ( null === $collection['metadata'] ) {
|
||||
// Load the manifest file if not already loaded.
|
||||
$collection['metadata'] = require $collection['manifest'];
|
||||
}
|
||||
|
||||
return array_map(
|
||||
static function ( $block_name ) use ( $path ) {
|
||||
return "{$path}/{$block_name}/block.json";
|
||||
},
|
||||
array_keys( $collection['metadata'] )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the collection path for a given file or folder.
|
||||
*
|
||||
|
@ -16,7 +16,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.8-alpha-59873';
|
||||
$wp_version = '6.8-alpha-59874';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
Loading…
Reference in New Issue
Block a user