Skip to content

Commit

Permalink
Section styles: add slug to override non-kebab-cased variations.
Browse files Browse the repository at this point in the history
Props aaronrobertshaw, oandregal.
Fixes #61440.


git-svn-id: https://develop.svn.wordpress.org/trunk@58413 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
oandregal committed Jun 14, 2024
1 parent 6d2a39f commit 5983b3c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/wp-includes/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) {
Expand Down
3 changes: 2 additions & 1 deletion src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class WP_Theme_JSON {
'description',
'patterns',
'settings',
'slug',
'styles',
'templateParts',
'title',
Expand Down Expand Up @@ -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.
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"
}
}
}
27 changes: 26 additions & 1 deletion tests/phpunit/tests/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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(
Expand All @@ -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,

/*
Expand All @@ -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 );
}
}
13 changes: 13 additions & 0 deletions tests/phpunit/tests/theme/wpThemeJsonResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
),
),
),
),
);
Expand Down

0 comments on commit 5983b3c

Please sign in to comment.