data 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; } }