File Editor
Directories:
.. (Back)
ID3
IXR
PHPMailer
Requests
SimplePie
Text
assets
block-bindings
block-patterns
block-supports
blocks
certificates
css
customize
fonts
html-api
images
interactivity-api
js
l10n
php-compat
pomo
rest-api
sitemaps
sodium_compat
style-engine
theme-compat
widgets
Files:
admin-bar.php
atomlib.php
author-template.php
block-bindings.php
block-editor.php
block-patterns.php
block-template-utils.php
block-template.php
blocks.php
bookmark-template.php
bookmark.php
cache-compat.php
cache.php
canonical.php
capabilities.php
category-template.php
category.php
class-IXR.php
class-avif-info.php
class-feed.php
class-http.php
class-json.php
class-oembed.php
class-phpass.php
class-phpmailer.php
class-pop3.php
class-requests.php
class-simplepie.php
class-smtp.php
class-snoopy.php
class-walker-category-dropdown.php
class-walker-category.php
class-walker-comment.php
class-walker-nav-menu.php
class-walker-page-dropdown.php
class-walker-page.php
class-wp-admin-bar.php
class-wp-ajax-response.php
class-wp-application-passwords.php
class-wp-block-bindings-registry.php
class-wp-block-bindings-source.php
class-wp-block-editor-context.php
class-wp-block-list.php
class-wp-block-metadata-registry.php
class-wp-block-parser-block.php
class-wp-block-parser-frame.php
class-wp-block-parser.php
class-wp-block-pattern-categories-registry.php
class-wp-block-patterns-registry.php
class-wp-block-styles-registry.php
class-wp-block-supports.php
class-wp-block-template.php
class-wp-block-templates-registry.php
class-wp-block-type-registry.php
class-wp-block-type.php
class-wp-block.php
class-wp-classic-to-block-menu-converter.php
class-wp-comment-query.php
class-wp-comment.php
class-wp-customize-control.php
class-wp-customize-manager.php
class-wp-customize-nav-menus.php
class-wp-customize-panel.php
class-wp-customize-section.php
class-wp-customize-setting.php
class-wp-customize-widgets.php
class-wp-date-query.php
class-wp-dependencies.php
class-wp-dependency.php
class-wp-duotone.php
class-wp-editor.php
class-wp-embed.php
class-wp-error.php
class-wp-exception.php
class-wp-fatal-error-handler.php
class-wp-feed-cache-transient.php
class-wp-feed-cache.php
class-wp-hook.php
class-wp-http-cookie.php
class-wp-http-curl.php
class-wp-http-encoding.php
class-wp-http-ixr-client.php
class-wp-http-proxy.php
class-wp-http-requests-hooks.php
class-wp-http-requests-response.php
class-wp-http-response.php
class-wp-http-streams.php
class-wp-http.php
class-wp-image-editor-gd.php
class-wp-image-editor-imagick.php
class-wp-image-editor.php
class-wp-list-util.php
class-wp-locale-switcher.php
class-wp-locale.php
class-wp-matchesmapregex.php
class-wp-meta-query.php
class-wp-metadata-lazyloader.php
class-wp-navigation-fallback.php
class-wp-network-query.php
class-wp-network.php
class-wp-object-cache.php
class-wp-oembed-controller.php
class-wp-oembed.php
class-wp-paused-extensions-storage.php
class-wp-plugin-dependencies.php
class-wp-post-type.php
class-wp-post.php
class-wp-query.php
class-wp-recovery-mode-cookie-service.php
class-wp-recovery-mode-email-service.php
class-wp-recovery-mode-key-service.php
class-wp-recovery-mode-link-service.php
class-wp-recovery-mode.php
class-wp-rewrite.php
class-wp-role.php
class-wp-roles.php
class-wp-script-modules.php
class-wp-scripts.php
class-wp-session-tokens.php
class-wp-simplepie-file.php
class-wp-simplepie-sanitize-kses.php
class-wp-site-query.php
class-wp-site.php
class-wp-styles.php
class-wp-tax-query.php
class-wp-taxonomy.php
class-wp-term-query.php
class-wp-term.php
class-wp-text-diff-renderer-inline.php
class-wp-text-diff-renderer-table.php
class-wp-textdomain-registry.php
class-wp-theme-json-data.php
class-wp-theme-json-resolver.php
class-wp-theme-json-schema.php
class-wp-theme-json.php
class-wp-theme.php
class-wp-token-map.php
class-wp-user-meta-session-tokens.php
class-wp-user-query.php
class-wp-user-request.php
class-wp-user.php
class-wp-walker.php
class-wp-widget-factory.php
class-wp-widget.php
class-wp-xmlrpc-server.php
class-wp.php
class-wpdb.php
class.wp-dependencies.php
class.wp-scripts.php
class.wp-styles.php
comment-template.php
comment.php
compat.php
cron.php
date.php
default-constants.php
default-filters.php
default-widgets.php
deprecated.php
embed-template.php
embed.php
error-protection.php
feed-atom-comments.php
feed-atom.php
feed-rdf.php
feed-rss.php
feed-rss2-comments.php
feed-rss2.php
feed.php
fonts.php
formatting.php
functions.php
functions.wp-scripts.php
functions.wp-styles.php
general-template.php
global-styles-and-settings.php
http.php
https-detection.php
https-migration.php
kses.php
l10n.php
link-template.php
load.php
locale.php
media-template.php
media.php
meta.php
ms-blogs.php
ms-default-constants.php
ms-default-filters.php
ms-deprecated.php
ms-files.php
ms-functions.php
ms-load.php
ms-network.php
ms-settings.php
ms-site.php
nav-menu-template.php
nav-menu.php
option.php
pluggable-deprecated.php
pluggable.php
plugin.php
post-formats.php
post-template.php
post-thumbnail-template.php
post.php
query.php
registration-functions.php
registration.php
rest-api.php
revision.php
rewrite.php
robots-template.php
rss-functions.php
rss.php
script-loader.php
script-modules.php
session.php
shortcodes.php
sitemaps.php
spl-autoload-compat.php
style-engine.php
taxonomy.php
template-canvas.php
template-loader.php
template.php
theme-previews.php
theme-templates.php
theme.php
update.php
user.php
vars.php
version.php
widgets.php
wp-db.php
wp-diff.php
Create New File
Create
Edit File: class-wp-block-metadata-registry.php
<?php /** * Block Metadata Registry * * @package WordPress * @subpackage Blocks * @since 6.7.0 */ /** * 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. * * @since 6.7.0 */ 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 WordPress 'wp-includes' directory path. * * @since 6.7.0 * @var string|null */ private static $wpinc_dir = null; /** * Stores the normalized WordPress plugin directory path. * * @since 6.7.0 * @var string|null */ private static $plugin_dir = 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 = wp_normalize_path( rtrim( $path, '/' ) ); $wpinc_dir = self::get_wpinc_dir(); $plugin_dir = self::get_plugin_dir(); // Check if the path is valid: if ( str_starts_with( $path, $plugin_dir ) ) { // For plugins, ensure the path is within a specific plugin directory and not the base plugin directory. $relative_path = substr( $path, strlen( $plugin_dir ) + 1 ); $plugin_name = strtok( $relative_path, '/' ); if ( empty( $plugin_name ) || $plugin_name === $relative_path ) { _doing_it_wrong( __METHOD__, __( 'Block metadata collections can only be registered for a specific plugin. The provided path is neither a core path nor a valid plugin path.' ), '6.7.0' ); return false; } } elseif ( ! str_starts_with( $path, $wpinc_dir ) ) { // If it's neither a plugin directory path nor within 'wp-includes', the path is invalid. _doing_it_wrong( __METHOD__, __( 'Block metadata collections can only be registered for a specific plugin. The provided path is neither a core path nor a valid plugin path.' ), '6.7.0' ); 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; } /** * 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. * * @since 6.7.0 * * @param string $file_or_folder The path to the file or folder containing the block. * @return array|null The block metadata for the block, or null if not found. */ public static function get_metadata( $file_or_folder ) { $path = self::find_collection_path( $file_or_folder ); if ( ! $path ) { return null; } $collection = &self::$collections[ $path ]; if ( null === $collection['metadata'] ) { // Load the manifest file if not already loaded $collection['metadata'] = require $collection['manifest']; } // Get the block name from the path. $block_name = self::default_identifier_callback( $file_or_folder ); return isset( $collection['metadata'][ $block_name ] ) ? $collection['metadata'][ $block_name ] : null; } /** * Finds the collection path for a given file or folder. * * @since 6.7.0 * * @param string $file_or_folder The path to the file or folder. * @return string|null The collection path if found, or null if not found. */ private static function find_collection_path( $file_or_folder ) { if ( empty( $file_or_folder ) ) { return null; } // Check the last matched collection first, since block registration usually happens in batches per plugin or theme. $path = wp_normalize_path( rtrim( $file_or_folder, '/' ) ); if ( self::$last_matched_collection && str_starts_with( $path, self::$last_matched_collection ) ) { return self::$last_matched_collection; } $collection_paths = array_keys( self::$collections ); foreach ( $collection_paths as $collection_path ) { if ( str_starts_with( $path, $collection_path ) ) { self::$last_matched_collection = $collection_path; return $collection_path; } } return null; } /** * Checks if metadata exists for a given block name in a specific collection. * * @since 6.7.0 * * @param string $file_or_folder The path to the file or folder containing the block metadata. * @return bool True if metadata exists for the block, false otherwise. */ public static function has_metadata( $file_or_folder ) { return null !== self::get_metadata( $file_or_folder ); } /** * 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. * * @since 6.7.0 * * @param string $path The file or folder path to determine the block identifier from. * @return string The block identifier, or an empty string if the path is empty. */ private static function default_identifier_callback( $path ) { // Ensure $path is not empty to prevent unexpected behavior. if ( empty( $path ) ) { return ''; } if ( str_ends_with( $path, 'block.json' ) ) { // Return the parent directory name if it's a block.json file. return basename( dirname( $path ) ); } // Otherwise, assume it's a directory and return its name. return basename( $path ); } /** * Gets the WordPress 'wp-includes' directory path. * * @since 6.7.0 * * @return string The WordPress 'wp-includes' directory path. */ private static function get_wpinc_dir() { if ( ! isset( self::$wpinc_dir ) ) { self::$wpinc_dir = wp_normalize_path( ABSPATH . WPINC ); } return self::$wpinc_dir; } /** * Gets the normalized WordPress plugin directory path. * * @since 6.7.0 * * @return string The normalized WordPress plugin directory path. */ private static function get_plugin_dir() { if ( ! isset( self::$plugin_dir ) ) { self::$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); } return self::$plugin_dir; } }
Save Changes
Rename File
Rename