Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse block metadata in WP_Theme_JSON::get_valid_block_style_variations() for better performance #7586

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,10 @@ public function __construct( $theme_json = array( 'version' => self::LATEST_SCHE
}

$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_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 );
Expand Down Expand Up @@ -3482,9 +3483,10 @@ public static function remove_insecure_properties( $theme_json, $origin = 'theme

$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_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 );

Expand Down Expand Up @@ -4531,12 +4533,15 @@ 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 ) {
Comment on lines +4541 to -4539
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should still call self::get_blocks_metadata() in case no value was provided, i.e. as default?

Since this method is protected (not private), in theory someone else could have overwritten this class. Highly unlikely but possible. Additionally though, not requiring the parameter makes using the method more ergonomic in situations where it hasn't been computed before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already added $blocks_metadata = empty( $blocks_metadata ) ? static::get_blocks_metadata() : $blocks_metadata; if the value is not passed of empty.

Let me know if i missed anything.

$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;
}
Expand Down
Loading