WP_Block_Metadata_Registry::get_collection_block_metadata_files( string $path ): string[]

Gets the list of absolute paths to all block metadata files that are part of the given collection.

Description

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'

Parameters

$pathstringrequired
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.

Source

public static function get_collection_block_metadata_files( $path ) {
	$path = rtrim( wp_normalize_path( $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(
		// No normalization necessary since `$path` is already normalized and `$block_name` is just a folder name.
		static function ( $block_name ) use ( $path ) {
			return "{$path}/{$block_name}/block.json";
		},
		array_keys( $collection['metadata'] )
	);
}

Changelog

VersionDescription
6.8.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.