Skip to content

Commit 5ff9717

Browse files
committed
Performance: reuse block metadata in WP_Theme_JSON::get_valid_block_style_variations()
In `WP_Theme_JSON::get_valid_block_style_variations()`, the method was calling `self::get_blocks_metadata()` even though the metadata was already retrieved in the parent function. This update reuses the existing block metadata instead of calling it again. A new optional parameter, `$blocks_metadata`, has been added to the function, allowing it to use pre-fetched metadata when available, improving efficiency. Fewer `self::get_blocks_metadata()` calls mean faster processing, especially in themes with many blocks. Props mukesh27, ramonopoly, aaronrobertshaw, flixos90. Fixes #62291. git-svn-id: https://develop.svn.wordpress.org/trunk@59359 602fd350-edb4-49c9-b593-d223f7449a82
1 parent eab73a7 commit 5ff9717

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/wp-includes/class-wp-theme-json.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,10 @@ public function __construct( $theme_json = array( 'version' => self::LATEST_SCHE
757757
}
758758

759759
$this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
760-
$valid_block_names = array_keys( static::get_blocks_metadata() );
760+
$blocks_metadata = static::get_blocks_metadata();
761+
$valid_block_names = array_keys( $blocks_metadata );
761762
$valid_element_names = array_keys( static::ELEMENTS );
762-
$valid_variations = static::get_valid_block_style_variations();
763+
$valid_variations = static::get_valid_block_style_variations( $blocks_metadata );
763764
$this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations );
764765
$this->theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
765766
$this->theme_json = static::maybe_opt_in_into_settings( $this->theme_json );
@@ -3482,9 +3483,10 @@ public static function remove_insecure_properties( $theme_json, $origin = 'theme
34823483

34833484
$theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
34843485

3485-
$valid_block_names = array_keys( static::get_blocks_metadata() );
3486+
$blocks_metadata = static::get_blocks_metadata();
3487+
$valid_block_names = array_keys( $blocks_metadata );
34863488
$valid_element_names = array_keys( static::ELEMENTS );
3487-
$valid_variations = static::get_valid_block_style_variations();
3489+
$valid_variations = static::get_valid_block_style_variations( $blocks_metadata );
34883490

34893491
$theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );
34903492

@@ -4531,12 +4533,15 @@ function ( $matches ) use ( $variation_class ) {
45314533
* Collects valid block style variations keyed by block type.
45324534
*
45334535
* @since 6.6.0
4536+
* @since 6.8.0 Added the `$blocks_metadata` parameter.
45344537
*
4538+
* @param array $blocks_metadata Optional. List of metadata per block. Default is the metadata for all blocks.
45354539
* @return array Valid block style variations by block type.
45364540
*/
4537-
protected static function get_valid_block_style_variations() {
4541+
protected static function get_valid_block_style_variations( $blocks_metadata = array() ) {
45384542
$valid_variations = array();
4539-
foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
4543+
$blocks_metadata = empty( $blocks_metadata ) ? static::get_blocks_metadata() : $blocks_metadata;
4544+
foreach ( $blocks_metadata as $block_name => $block_meta ) {
45404545
if ( ! isset( $block_meta['styleVariations'] ) ) {
45414546
continue;
45424547
}

0 commit comments

Comments
 (0)