From 5983b3c04bd7b182d2a89537d35c3928b6990589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=83=C2=A9?= Date: Fri, 14 Jun 2024 09:03:40 +0000 Subject: [PATCH] Section styles: add slug to override non-kebab-cased variations. Props aaronrobertshaw, oandregal. Fixes #61440. git-svn-id: https://develop.svn.wordpress.org/trunk@58413 602fd350-edb4-49c9-b593-d223f7449a82 --- .../block-supports/block-style-variations.php | 4 +-- src/wp-includes/class-wp-theme-json.php | 3 ++- .../block-style-variation-with-slug.json | 12 +++++++++ .../block-supports/block-style-variations.php | 27 ++++++++++++++++++- .../tests/theme/wpThemeJsonResolver.php | 13 +++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/data/themedir1/block-theme/styles/block-style-variation-with-slug.json diff --git a/src/wp-includes/block-supports/block-style-variations.php b/src/wp-includes/block-supports/block-style-variations.php index a44ecdd396e18..8e84292ac0eb4 100644 --- a/src/wp-includes/block-supports/block-style-variations.php +++ b/src/wp-includes/block-supports/block-style-variations.php @@ -249,7 +249,7 @@ function wp_resolve_block_style_variations( $variations ) { * Block style variations read in via standalone theme.json partials * need to have their name set to the kebab case version of their title. */ - $variation_name = $have_named_variations ? $key : _wp_to_kebab_case( $variation['title'] ); + $variation_name = $have_named_variations ? $key : ( $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ) ); foreach ( $supported_blocks as $block_type ) { // Add block style variation data under current block type. @@ -441,7 +441,7 @@ function wp_register_block_style_variations_from_theme_json_data( $variations ) * Block style variations read in via standalone theme.json partials * need to have their name set to the kebab case version of their title. */ - $variation_name = $have_named_variations ? $key : _wp_to_kebab_case( $variation['title'] ); + $variation_name = $have_named_variations ? $key : ( $variation['slug'] ?? _wp_to_kebab_case( $variation['title'] ) ); $variation_label = $variation['title'] ?? $variation_name; foreach ( $supported_blocks as $block_type ) { diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 957434de7fba6..05f5e7b9e7629 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -358,6 +358,7 @@ class WP_Theme_JSON { 'description', 'patterns', 'settings', + 'slug', 'styles', 'templateParts', 'title', @@ -3244,7 +3245,7 @@ protected static function filter_slugs( $node, $slugs ) { * @since 6.3.2 Preserves global styles block variations when securing styles. * @since 6.6.0 Updated to allow variation element styles and $origin parameter. * - * @param array $theme_json Structure to sanitize. + * @param array $theme_json Structure to sanitize. * @param string $origin Optional. What source of data this object represents. * One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'. * @return array Sanitized structure. diff --git a/tests/phpunit/data/themedir1/block-theme/styles/block-style-variation-with-slug.json b/tests/phpunit/data/themedir1/block-theme/styles/block-style-variation-with-slug.json new file mode 100644 index 0000000000000..d938a8a0db833 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/styles/block-style-variation-with-slug.json @@ -0,0 +1,12 @@ +{ + "version": 3, + "blockTypes": [ "core/group", "core/columns" ], + "slug": "WithSlug", + "title": "With Slug", + "styles": { + "color": { + "background": "aliceblue", + "text": "midnightblue" + } + } +} diff --git a/tests/phpunit/tests/block-supports/block-style-variations.php b/tests/phpunit/tests/block-supports/block-style-variations.php index 3adbaa407532e..6b50b87fa2abc 100644 --- a/tests/phpunit/tests/block-supports/block-style-variations.php +++ b/tests/phpunit/tests/block-supports/block-style-variations.php @@ -61,6 +61,7 @@ public function filter_set_theme_root() { * variation file within `/styles`, are added to the theme data. * * @ticket 61312 + * @ticket 61440 */ public function test_add_registered_block_styles_to_theme_data() { switch_theme( 'block-theme' ); @@ -98,6 +99,22 @@ public function test_add_registered_block_styles_to_theme_data() { ), ); + /* + * This style is to be deliberately overwritten by the theme.json partial + * See `tests/phpunit/data/themedir1/block-theme/styles/block-style-variation-with-slug.json`. + */ + register_block_style( + 'core/group', + array( + 'name' => 'WithSlug', + 'style_data' => array( + 'color' => array( + 'background' => 'whitesmoke', + 'text' => 'black', + ), + ), + ) + ); register_block_style( 'core/group', array( @@ -110,6 +127,13 @@ public function test_add_registered_block_styles_to_theme_data() { $group_styles = $theme_json['styles']['blocks']['core/group'] ?? array(); $expected = array( 'variations' => array( + // @ticket 61440 + 'WithSlug' => array( + 'color' => array( + 'background' => 'aliceblue', + 'text' => 'midnightblue', + ), + ), 'my-variation' => $variation_styles_data, /* @@ -133,7 +157,8 @@ public function test_add_registered_block_styles_to_theme_data() { ); unregister_block_style( 'core/group', 'my-variation' ); + unregister_block_style( 'core/group', 'WithSlug' ); - $this->assertSameSetsWithIndex( $group_styles, $expected ); + $this->assertSameSetsWithIndex( $expected, $group_styles ); } } diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 581828ba6f29c..0cb102b7accf4 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -1160,6 +1160,19 @@ public function data_get_style_variations() { ), ), ), + // @ticket 61440 + array( + 'blockTypes' => array( 'core/group', 'core/columns' ), + 'version' => 3, + 'slug' => 'WithSlug', + 'title' => 'With Slug', + 'styles' => array( + 'color' => array( + 'background' => 'aliceblue', + 'text' => 'midnightblue', + ), + ), + ), ), ), );