From 76e38bb30823eab557a1cb2d6f31c4927e6b0576 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 2 May 2024 13:12:04 +1000 Subject: [PATCH] Theme JSON: Extract util to get valid block style variations --- src/wp-includes/class-wp-theme-json.php | 39 +++++++++++++++---------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index d38fbb3110829..f45845c9b2c8f 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -731,15 +731,9 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { $this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json ); $valid_block_names = array_keys( static::get_blocks_metadata() ); $valid_element_names = array_keys( static::ELEMENTS ); - $valid_variations = array(); - foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) { - if ( ! isset( $block_meta['styleVariations'] ) ) { - continue; - } - $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] ); - } - $theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations ); - $this->theme_json = static::maybe_opt_in_into_settings( $theme_json ); + $valid_variations = static::get_valid_block_style_variations(); + $theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations ); + $this->theme_json = static::maybe_opt_in_into_settings( $theme_json ); // Internally, presets are keyed by origin. $nodes = static::get_setting_nodes( $this->theme_json ); @@ -3077,13 +3071,7 @@ public static function remove_insecure_properties( $theme_json ) { $valid_block_names = array_keys( static::get_blocks_metadata() ); $valid_element_names = array_keys( static::ELEMENTS ); - $valid_variations = array(); - foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) { - if ( ! isset( $block_meta['styleVariations'] ) ) { - continue; - } - $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] ); - } + $valid_variations = static::get_valid_block_style_variations(); $theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations ); @@ -4032,4 +4020,23 @@ function ( $matches ) use ( $variation_class ) { return implode( ',', $result ); } + + /** + * Collects valid block style variations keyed by block type. + * + * @since 6.6.0 + * + * @return array Valid block style variations by block type. + */ + protected static function get_valid_block_style_variations() { + $valid_variations = array(); + foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) { + if ( ! isset( $block_meta['styleVariations'] ) ) { + continue; + } + $valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] ); + } + + return $valid_variations; + } }