From 828868d680a4f052c35854d0ed0fb217179664b8 Mon Sep 17 00:00:00 2001 From: Ramon Date: Wed, 30 Oct 2024 07:24:47 +1100 Subject: [PATCH] Backport from Core: Reuse block metadata in WP_Theme_JSON::get_valid_block_style_variations() for better performance (#66539) * Backporting theme json performance optimizations from https://github.com/WordPress/wordpress-develop/pull/7586 * Tentatively backport using `get_blocks_metadata` instead of `WP_Block_Type_Registry` to get block names. Co-authored-by: ramonjd Co-authored-by: mukeshpanchal27 Co-authored-by: aaronrobertshaw --- lib/class-wp-theme-json-gutenberg.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 5975e4fe9852ff..09f388e7d3b93f 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -745,10 +745,10 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut } $this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin ); - $registry = WP_Block_Type_Registry::get_instance(); - $valid_block_names = array_keys( $registry->get_all_registered() ); + $blocks_metadata = static::get_blocks_metadata(); + $valid_block_names = array_keys( $blocks_metadata ); $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::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations ); $this->theme_json = static::maybe_opt_in_into_settings( $this->theme_json ); @@ -3506,9 +3506,10 @@ public static function remove_insecure_properties( $theme_json, $origin = 'theme $theme_json = WP_Theme_JSON_Schema_Gutenberg::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_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 ); @@ -4511,11 +4512,16 @@ function ( $matches ) use ( $variation_class ) { /** * Collects valid block style variations keyed by block type. * + * @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. */ - protected static function get_valid_block_style_variations() { + protected static function get_valid_block_style_variations( $blocks_metadata = 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'] ) ) { continue; }