Skip to content

Commit 828868d

Browse files
ramonjdmukeshpanchal27aaronrobertshaw
authored
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 WordPress/wordpress-develop#7586 * Tentatively backport using `get_blocks_metadata` instead of `WP_Block_Type_Registry` to get block names. Co-authored-by: ramonjd <[email protected]> Co-authored-by: mukeshpanchal27 <[email protected]> Co-authored-by: aaronrobertshaw <[email protected]>
1 parent 6ffb672 commit 828868d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

lib/class-wp-theme-json-gutenberg.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -745,10 +745,10 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut
745745
}
746746

747747
$this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin );
748-
$registry = WP_Block_Type_Registry::get_instance();
749-
$valid_block_names = array_keys( $registry->get_all_registered() );
748+
$blocks_metadata = static::get_blocks_metadata();
749+
$valid_block_names = array_keys( $blocks_metadata );
750750
$valid_element_names = array_keys( static::ELEMENTS );
751-
$valid_variations = static::get_valid_block_style_variations();
751+
$valid_variations = static::get_valid_block_style_variations( $blocks_metadata );
752752
$this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations );
753753
$this->theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
754754
$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
35063506

35073507
$theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin );
35083508

3509-
$valid_block_names = array_keys( static::get_blocks_metadata() );
3509+
$blocks_metadata = static::get_blocks_metadata();
3510+
$valid_block_names = array_keys( $blocks_metadata );
35103511
$valid_element_names = array_keys( static::ELEMENTS );
3511-
$valid_variations = static::get_valid_block_style_variations();
3512+
$valid_variations = static::get_valid_block_style_variations( $blocks_metadata );
35123513

35133514
$theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );
35143515

@@ -4511,11 +4512,16 @@ function ( $matches ) use ( $variation_class ) {
45114512
/**
45124513
* Collects valid block style variations keyed by block type.
45134514
*
4515+
* @since 6.6.0
4516+
* @since 6.8.0 Added the `$blocks_metadata` parameter.
4517+
*
4518+
* @param array $blocks_metadata Optional. List of metadata per block. Default is the metadata for all blocks.
45144519
* @return array Valid block style variations by block type.
45154520
*/
4516-
protected static function get_valid_block_style_variations() {
4521+
protected static function get_valid_block_style_variations( $blocks_metadata = array() ) {
45174522
$valid_variations = array();
4518-
foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
4523+
$blocks_metadata = empty( $blocks_metadata ) ? static::get_blocks_metadata() : $blocks_metadata;
4524+
foreach ( $blocks_metadata as $block_name => $block_meta ) {
45194525
if ( ! isset( $block_meta['styleVariations'] ) ) {
45204526
continue;
45214527
}

0 commit comments

Comments
 (0)