WP_Block_Metadata_Registry
- Since
- 6.7.0
- Source
wp-includes/class-wp-block-metadata-registry.php:20
Class used for managing block metadata collections.
Description
The WP_Block_Metadata_Registry allows plugins to register metadata for large collections of blocks (e.g., 50-100+) using a single PHP file. This approach reduces the need to read and decode multiple block.json files, enhancing performance through opcode caching.
Compatibility
- WordPress
- since 6.7.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Hooks and filters fired · 1
Every hook that fires from inside WP_Block_Metadata_Registry, in the order it appears in the class, grouped by the method that fires it.
Properties · 3
$collectionsarray<string,private static- Container for storing block metadata collections.
$last_matched_collectionstring|nullprivate static- Caches the last matched collection path for performance optimization.
$default_collection_rootsstring[]|nullprivate static- Stores the default allowed collection root paths.
Methods · 8
- register_collection()Registers a block metadata collection.
- get_metadata()Retrieves block metadata for a given block within a specific collection.
- get_collection_block_metadata_files()Gets the list of absolute paths to all block metadata files that are part of the given collection.
- find_collection_path()Finds the collection path for a given file or folder.
- has_metadata()Checks if metadata exists for a given block name in a specific collection.
- default_identifier_callback()Default identifier function to determine the block identifier from a given path.
- is_valid_collection_path()Checks whether the given block metadata collection path is valid against the list of collection roots.
- get_default_collection_roots()Gets the default collection root directory paths.
Source code
class WP_Block_Metadata_Registry { /** * Container for storing block metadata collections. * * Each entry maps a base path to its corresponding metadata and callback. * * @since 6.7.0 * @var array<string, array<string, mixed>> */ private static $collections = array(); /** * Caches the last matched collection path for performance optimization. * * @since 6.7.0 * @var string|null */ private static $last_matched_collection = null; /** * Stores the default allowed collection root paths. * * @since 6.7.2 * @var string[]|null */ private static $default_collection_roots = null; /** * Registers a block metadata collection. * * This method allows registering a collection of block metadata from a single * manifest file, improving performance for large sets of blocks. * * The manifest file should be a PHP file that returns an associative array, where * the keys are the block identifiers (without their namespace) and the values are * the corresponding block metadata arrays. The block identifiers must match the * parent directory name for the respective `block.json` file. * * Example manifest file structure: * ``` * return array( * 'example-block' => array( * 'title' => 'Example Block', * 'category' => 'widgets', * 'icon' => 'smiley', * // ... other block metadata * ), * 'another-block' => array( * 'title' => 'Another Block', * 'category' => 'formatting', * 'icon' => 'star-filled', * // ... other block metadata * ), * // ... more block metadata entries * ); * ``` * * @since 6.7.0 * * @param string $path The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ). * @param string $manifest The absolute path to the manifest file containing the metadata collection. * @return bool True if the collection was registered successfully, false otherwise. */ public static function register_collection( $path, $manifest ) { $path = rtrim( wp_normalize_path( $path ), '/' ); $collection_roots = self::get_default_collection_roots(); /** * Filters the root directory paths for block metadata collections. * * Any block metadata collection that is registered must not use any of these paths, or any parent directory * path of them. Most commonly, block metadata collections should reside within one of these paths, though in * some scenarios they may also reside in entirely different directories (e.g. in case of symlinked plugins). * * Example: * * It is allowed to register a collection with path `WP_PLUGIN_DIR . '/my-plugin'`. * * It is not allowed to register a collection with path `WP_PLUGIN_DIR`. * * It is not allowed to register a collection with path `dirname( WP_PLUGIN_DIR )`.Changelog
Introduced in 6.7.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
6.8.8
Method
get_collection_block_metadata_files() added.verified against source6.7.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-block-metadata-registry.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.