WP_Block_Metadata_Registry::find_collection_path
Finds the collection path for a given file or folder.
Method of the class: WP_Block_Metadata_Registry{}
No Hooks.
Returns
String|null. The normalized collection path if found, or null if not found.
Usage
$result = WP_Block_Metadata_Registry::find_collection_path( $file_or_folder );
- $file_or_folder(string) (required)
- The normalized path to the file or folder.
Changelog
| Since 6.7.0 | Introduced. |
WP_Block_Metadata_Registry::find_collection_path() WP Block Metadata Registry::find collection path code WP 7.0
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 = 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;
}