Skip to content

Commit

Permalink
add $last_matched_collection
Browse files Browse the repository at this point in the history
  • Loading branch information
mreishus committed Sep 24, 2024
1 parent 2ff17a9 commit cfd9573
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/wp-includes/class-wp-block-metadata-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class WP_Block_Metadata_Registry {
*/
private static $collections = array();

/**
* Caches the last matched collection path for performance optimization.
*
* @since 6.X.0
* @var string|null
*/
private static $last_matched_collection = null;

/**
* Registers a block metadata collection.
*
Expand Down Expand Up @@ -108,9 +116,20 @@ public static function get_metadata( $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, '/' ) );
foreach ( self::$collections as $collection_path => $collection ) {
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;
}
}
Expand Down

0 comments on commit cfd9573

Please sign in to comment.