From 6f0950bff0d5bf611dacdbb4901ea13bae054d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 14 Jun 2024 10:54:57 +0200 Subject: [PATCH] Section styles: consolidate variation name (#62550) Co-authored-by: oandregal Co-authored-by: aaronrobertshaw --- backport-changelog/6.6/6824.md | 3 +++ lib/block-supports/block-style-variations.php | 4 ++-- lib/class-wp-theme-json-gutenberg.php | 1 + .../block-style-variations-test.php | 23 +++++++++++++++++++ phpunit/class-wp-theme-json-resolver-test.php | 12 ++++++++++ .../block-style-variation-with-slug.json | 12 ++++++++++ schemas/json/theme.json | 4 ++++ 7 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 backport-changelog/6.6/6824.md create mode 100644 phpunit/data/themedir1/block-theme/styles/block-style-variation-with-slug.json diff --git a/backport-changelog/6.6/6824.md b/backport-changelog/6.6/6824.md new file mode 100644 index 0000000000000..7dc6e090f0149 --- /dev/null +++ b/backport-changelog/6.6/6824.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/6824 + +* https://github.com/WordPress/gutenberg/pull/62550 diff --git a/lib/block-supports/block-style-variations.php b/lib/block-supports/block-style-variations.php index f2bc6af92e9de..e078b50b19d5a 100644 --- a/lib/block-supports/block-style-variations.php +++ b/lib/block-supports/block-style-variations.php @@ -244,7 +244,7 @@ function gutenberg_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. @@ -455,7 +455,7 @@ function gutenberg_register_block_style_variations_from_theme_json_data( $variat * 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/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index db54757da68e7..deb4d850d271f 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -357,6 +357,7 @@ class WP_Theme_JSON_Gutenberg { 'styles', 'templateParts', 'title', + 'slug', 'version', ); diff --git a/phpunit/block-supports/block-style-variations-test.php b/phpunit/block-supports/block-style-variations-test.php index 870a76a4fb4a4..62fa923dbdd7e 100644 --- a/phpunit/block-supports/block-style-variations-test.php +++ b/phpunit/block-supports/block-style-variations-test.php @@ -94,6 +94,22 @@ public function test_add_registered_block_styles_to_theme_data() { ), ); + /* + * This style is to be deliberately overwritten by the theme.json partial + * See `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( @@ -106,6 +122,12 @@ public function test_add_registered_block_styles_to_theme_data() { $group_styles = $theme_json['styles']['blocks']['core/group'] ?? array(); $expected = array( 'variations' => array( + 'WithSlug' => array( + 'color' => array( + 'background' => 'aliceblue', + 'text' => 'midnightblue', + ), + ), 'my-variation' => $variation_styles_data, /* @@ -129,6 +151,7 @@ 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 ); } diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 50e1d9d846899..6333c3d1dd776 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -1118,6 +1118,18 @@ public function data_get_style_variations() { ), ), ), + array( + 'blockTypes' => array( 'core/group', 'core/columns' ), + 'version' => 3, + 'slug' => 'WithSlug', + 'title' => 'With Slug', + 'styles' => array( + 'color' => array( + 'background' => 'aliceblue', + 'text' => 'midnightblue', + ), + ), + ), ), ), ); diff --git a/phpunit/data/themedir1/block-theme/styles/block-style-variation-with-slug.json b/phpunit/data/themedir1/block-theme/styles/block-style-variation-with-slug.json new file mode 100644 index 0000000000000..d938a8a0db833 --- /dev/null +++ b/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/schemas/json/theme.json b/schemas/json/theme.json index aafafae360553..37bbd6f7f0893 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -2700,6 +2700,10 @@ "type": "string", "description": "Title of the global styles variation. If not defined, the file name will be used." }, + "slug": { + "type": "string", + "description": "Slug of the global styles variation. If not defined, the kebab-case title will be used." + }, "description": { "type": "string", "description": "Description of the global styles variation."