diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 4a9cf545e6..4d3ad3d4bd 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -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. * diff --git a/wp-includes/class-wp-block-metadata-registry.php b/wp-includes/class-wp-block-metadata-registry.php index 4b567d7d6f..e7e71afcd1 100644 --- a/wp-includes/class-wp-block-metadata-registry.php +++ b/wp-includes/class-wp-block-metadata-registry.php @@ -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. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 1cd0389c0b..16827cede7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.