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.



Built from https://develop.svn.wordpress.org/trunk@59359


git-svn-id: http://core.svn.wordpress.org/trunk@58745 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ramonopoly 2024-11-06 00:44:15 +00:00
parent a739bf28a1
commit 16f4229d09
2 changed files with 12 additions and 7 deletions

View File

@ -757,9 +757,10 @@ class WP_Theme_JSON {
} }
$this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin ); $this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
$valid_block_names = array_keys( static::get_blocks_metadata() ); $blocks_metadata = static::get_blocks_metadata();
$valid_block_names = array_keys( $blocks_metadata );
$valid_element_names = array_keys( static::ELEMENTS ); $valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = static::get_valid_block_style_variations(); $valid_variations = static::get_valid_block_style_variations( $blocks_metadata );
$this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations ); $this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations );
$this->theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations ); $this->theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
$this->theme_json = static::maybe_opt_in_into_settings( $this->theme_json ); $this->theme_json = static::maybe_opt_in_into_settings( $this->theme_json );
@ -3482,9 +3483,10 @@ class WP_Theme_JSON {
$theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin ); $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
$valid_block_names = array_keys( static::get_blocks_metadata() ); $blocks_metadata = static::get_blocks_metadata();
$valid_block_names = array_keys( $blocks_metadata );
$valid_element_names = array_keys( static::ELEMENTS ); $valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = static::get_valid_block_style_variations(); $valid_variations = static::get_valid_block_style_variations( $blocks_metadata );
$theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations ); $theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );
@ -4531,12 +4533,15 @@ class WP_Theme_JSON {
* Collects valid block style variations keyed by block type. * Collects valid block style variations keyed by block type.
* *
* @since 6.6.0 * @since 6.6.0
* @since 6.8.0 Added the `$blocks_metadata` parameter.
* *
* @param array $blocks_metadata Optional. List of metadata per block. Default is the metadata for all blocks.
* @return array Valid block style variations by block type. * @return array Valid block style variations by block type.
*/ */
protected static function get_valid_block_style_variations() { protected static function get_valid_block_style_variations( $blocks_metadata = array() ) {
$valid_variations = array(); $valid_variations = array();
foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) { $blocks_metadata = empty( $blocks_metadata ) ? static::get_blocks_metadata() : $blocks_metadata;
foreach ( $blocks_metadata as $block_name => $block_meta ) {
if ( ! isset( $block_meta['styleVariations'] ) ) { if ( ! isset( $block_meta['styleVariations'] ) ) {
continue; continue;
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.8-alpha-59358'; $wp_version = '6.8-alpha-59359';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.