[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-block-metadata-registry.php (summary)

Block Metadata Registry

File Size: 273 lines (8 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

WP_Block_Metadata_Registry:: (7 methods):
  register_collection()
  get_metadata()
  find_collection_path()
  has_metadata()
  default_identifier_callback()
  get_wpinc_dir()
  get_plugin_dir()


Class: WP_Block_Metadata_Registry  - X-Ref

Class used for managing block metadata collections.

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.

register_collection( $path, $manifest )   X-Ref
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
);
```

return: bool True if the collection was registered successfully, false otherwise.
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.

get_metadata( $file_or_folder )   X-Ref
Retrieves block metadata for a given block within a specific collection.

This method uses the registered collections to efficiently lookup
block metadata without reading individual `block.json` files.

return: array|null The block metadata for the block, or null if not found.
param: string $file_or_folder The path to the file or folder containing the block.

find_collection_path( $file_or_folder )   X-Ref
Finds the collection path for a given file or folder.

return: string|null The collection path if found, or null if not found.
param: string $file_or_folder The path to the file or folder.

has_metadata( $file_or_folder )   X-Ref
Checks if metadata exists for a given block name in a specific collection.

return: bool True if metadata exists for the block, false otherwise.
param: string $file_or_folder The path to the file or folder containing the block metadata.

default_identifier_callback( $path )   X-Ref
Default identifier function to determine the block identifier from a given path.

This function extracts the block identifier from the path:
- For 'block.json' files, it uses the parent directory name.
- For directories, it uses the directory name itself.
- For empty paths, it returns an empty string.

For example:
- Path: '/wp-content/plugins/my-plugin/blocks/example/block.json'
Identifier: 'example'
- Path: '/wp-content/plugins/my-plugin/blocks/another-block'
Identifier: 'another-block'

This default behavior matches the standard WordPress block structure.

return: string The block identifier, or an empty string if the path is empty.
param: string $path The file or folder path to determine the block identifier from.

get_wpinc_dir()   X-Ref
Gets the WordPress 'wp-includes' directory path.

return: string The WordPress 'wp-includes' directory path.

get_plugin_dir()   X-Ref
Gets the normalized WordPress plugin directory path.

return: string The normalized WordPress plugin directory path.



Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref