wppaste
WordPress

WP_Block_Metadata_Registry::register_collection( string $path, string $manifest ): bool

Since
6.7.0
Source
wp-includes/class-wp-block-metadata-registry.php:84
Registers a block metadata collection.

Description

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
);

Compatibility

WordPress
since 6.7.0
PHP
7.4–8.6-dev
  • 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), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$pathstring
The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ).
$manifeststring
The absolute path to the manifest file containing the metadata collection.

Return value

bool
True if the collection was registered successfully, false otherwise.

Performance profile

How much work a call to WP_Block_Metadata_Registry::register_collection() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9

Executed per call on PHP 8.5. The body compiles to 79.

Plugin surface
1 hook

Third-party callbacks on 'wp_allowed_block_metadata_collection_roots' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Block_Metadata_Registry::register_collection() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9wp_normalize_path(), rtrim()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7992
8.57992
8.479923 fewer instructions than PHP 8.3
8.38292
8.28292
8.182929 more instructions than PHP 7.4
7.47342–542

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_Block_Metadata_Registry::register_collection() runs, in this order:

  1. apply_filters( wp_allowed_block_metadata_collection_roots )filterline 110 (+26 into the body)

    Filters the root directory paths for block metadata collections.

Uses · 8

Used by · 1

Source code

	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 )`.		 *		 * The default list encompasses the `wp-includes` directory, as well as the root directories for plugins,		 * must-use plugins, and themes. This filter can be used to expand the list, e.g. to custom directories that		 * contain symlinked plugins, so that these root directories cannot be used themselves for a block metadata		 * collection either.		 *		 * @since 6.7.2		 *		 * @param string[] $collection_roots List of allowed metadata collection root paths.		 */		$collection_roots = apply_filters( 'wp_allowed_block_metadata_collection_roots', $collection_roots ); 		$collection_roots = array_unique(			array_map(				static function ( $allowed_root ) {					return rtrim( wp_normalize_path( $allowed_root ), '/' );				},				$collection_roots			)		); 		// Check if the path is valid:		if ( ! self::is_valid_collection_path( $path, $collection_roots ) ) {			_doing_it_wrong(				__METHOD__,				sprintf(					/* translators: %s: list of allowed collection roots */					__( 'Block metadata collections cannot be registered as one of the following directories or their parent directories: %s' ),					esc_html( implode( wp_get_list_item_separator(), $collection_roots ) )				),				'6.7.2'			);			return false;		} 		if ( ! file_exists( $manifest ) ) {			_doing_it_wrong(				__METHOD__,				__( 'The specified manifest file does not exist.' ),				'6.7.0'			);			return false;		} 		self::$collections[ $path ] = array(			'manifest' => $manifest,			'metadata' => null,		); 		return true;	}

Changelog

Introduced in 6.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

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.