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

Section styles: consolidate variation name #62550

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions backport-changelog/6.6/6824.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/6824

* https://github.com/WordPress/gutenberg/pull/62550
4 changes: 2 additions & 2 deletions lib/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'] ) );
Copy link
Member Author

Choose a reason for hiding this comment

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

For most scenarios, just kebab-casing the title will be enough and that's a great devexp we may want to keep. The slug (could also use name) is a way to make it work for any scenario.

$variation_label = $variation['title'] ?? $variation_name;

foreach ( $supported_blocks as $block_type ) {
Expand Down
1 change: 1 addition & 0 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class WP_Theme_JSON_Gutenberg {
'styles',
'templateParts',
'title',
'slug',
'version',
);

Expand Down
23 changes: 23 additions & 0 deletions phpunit/block-supports/block-style-variations-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,

/*
Expand All @@ -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 );
}
Expand Down
12 changes: 12 additions & 0 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
),
),
),
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 3,
"blockTypes": [ "core/group", "core/columns" ],
"slug": "WithSlug",
"title": "With Slug",
"styles": {
"color": {
"background": "aliceblue",
"text": "midnightblue"
}
}
}
4 changes: 4 additions & 0 deletions schemas/json/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,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."
Expand Down
Loading