From 0b80501dcc3ec7fb571ec74bcdc95fee50fa5e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 20 Jun 2024 16:50:46 +0200 Subject: [PATCH 1/2] Unwrap shared block style variations --- src/wp-includes/class-wp-theme-json.php | 58 +++++++++++++++++++++ tests/phpunit/tests/theme/wpThemeJson.php | 62 +++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 311705b9adaa1..ec6a2695c9a36 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -756,6 +756,7 @@ public function __construct( $theme_json = array( 'version' => self::LATEST_SCHE } $this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin ); + $this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json ); $valid_block_names = array_keys( static::get_blocks_metadata() ); $valid_element_names = array_keys( static::ELEMENTS ); $valid_variations = static::get_valid_block_style_variations(); @@ -802,6 +803,63 @@ public function __construct( $theme_json = array( 'version' => self::LATEST_SCHE } } + /** + * Unwraps shared block style variations. + * + * Given the following input: + * + * { + * "styles": { + * "blocks": { + * "variations": { + * "blockTypes": [ "core/paragraph", "core/group" ], + * "color": { "background": "backgroundColor" } + * } + * } + * } + * } + * + * It returns the following output: + * + * { + * "styles": { + * "blocks": { + * "variations": { + * "core/paragraph": { "color": { "background": "backgroundColor" } }, + * "core/group": { "color": { "background": "backgroundColor" } } + * } + * } + * } + * } + */ + private static function unwrap_shared_block_style_variations( $theme_json ) { + $new_theme_json = $theme_json; + + if ( ! isset( $new_theme_json['styles']['blocks']['variations'] ) ) { + return $new_theme_json; + } + + $variations = $new_theme_json['styles']['blocks']['variations']; + foreach ( $variations as $variation_name => $data ) { + if ( ! isset( $data['blockTypes'] ) ) { + // Skip shared variations that do not declare blockTypes. + continue; + } + + $block_names = $data['blockTypes']; + unset( $data['blockTypes'] ); + unset( $data['title'] ); + foreach ( $block_names as $block_name ) { + // TODO: what if there was existing data for the block? + $new_theme_json['styles']['blocks'][ $block_name ]['variations'][ $variation_name ] = $data; + } + } + + unset( $new_theme_json['styles']['blocks']['variations'] ); + + return $new_theme_json; + } + /** * Enables some opt-in settings if theme declared support. * diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index 3c5431a32018f..33379fb10b60c 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -14,6 +14,7 @@ */ class Tests_Theme_wpThemeJson extends WP_UnitTestCase { + /** * Administrator ID. * @@ -3930,6 +3931,67 @@ public function test_sanitize_for_unregistered_style_variations() { $this->assertSameSetsWithIndex( $expected, $sanitized_theme_json, 'Sanitized theme.json styles does not match' ); } + /** + * @ticket 61451 + * @ticket 61312 + * + */ + public function test_unwraps_block_style_variations() { + register_block_style( + 'core/paragraph', + array( + 'name' => 'myVariation', + 'label' => 'My variation', + ) + ); + register_block_style( + 'core/group', + array( + 'name' => 'myVariation', + 'label' => 'My variation', + ) + ); + + $input = new WP_Theme_JSON( + array( + 'version' => 3, + 'styles' => array( + 'blocks' => array( + 'variations' => array( + 'myVariation' => array( + 'title' => 'My variation', + 'blockTypes' => array( 'core/paragraph', 'core/group' ), + 'color' => array( 'background' => 'backgroundColor' ), + ), + ), + ), + ), + ) + ); + $expected = array( + 'version' => WP_Theme_JSON::LATEST_SCHEMA, + 'styles' => array( + 'blocks' => array( + 'core/paragraph' => array( + 'variations' => array( + 'myVariation' => array( + 'color' => array( 'background' => 'backgroundColor' ), + ), + ), + ), + 'core/group' => array( + 'variations' => array( + 'myVariation' => array( + 'color' => array( 'background' => 'backgroundColor' ), + ), + ), + ), + ), + ), + ); + $this->assertSameSetsWithIndex( $expected, $input->get_raw_data(), 'Unwrapped block style variations do not match' ); + } + /** * @ticket 57583 * From 84bef95cdf68f8b020ec4fea395c0715d36b73ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 20 Jun 2024 16:55:38 +0200 Subject: [PATCH 2/2] Remove any post-processing for theme.json of the theme --- .../block-supports/block-style-variations.php | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/src/wp-includes/block-supports/block-style-variations.php b/src/wp-includes/block-supports/block-style-variations.php index bb663ee754fae..fb306b0fe98fb 100644 --- a/src/wp-includes/block-supports/block-style-variations.php +++ b/src/wp-includes/block-supports/block-style-variations.php @@ -333,25 +333,6 @@ function wp_resolve_block_style_variations_from_theme_json_partials( $theme_json return wp_merge_block_style_variations_data( $variations_data, $theme_json ); } -/** - * Merges shared block style variations registered within the - * `styles.blocks.variations` property of the primary theme.json file. - * - * @since 6.6.0 - * @access private - * - * @param WP_Theme_JSON_Data $theme_json Current theme.json data. - * - * @return WP_Theme_JSON_Data - */ -function wp_resolve_block_style_variations_from_primary_theme_json( $theme_json ) { - $theme_json_data = $theme_json->get_data(); - $block_style_variations = $theme_json_data['styles']['blocks']['variations'] ?? array(); - $variations_data = wp_resolve_block_style_variations( $block_style_variations ); - - return wp_merge_block_style_variations_data( $variations_data, $theme_json ); -} - /** * Merges block style variations registered via the block styles registry with a * style object, under their appropriate block types within theme.json styles. @@ -400,7 +381,6 @@ function wp_enqueue_block_style_variation_styles() { add_action( 'wp_enqueue_scripts', 'wp_enqueue_block_style_variation_styles', 1 ); // Resolve block style variations from all their potential sources. The order here is deliberate. -add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_primary_theme_json', 10, 1 ); add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_theme_json_partials', 10, 1 ); add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry', 10, 1 );