From 5bd13e7cc1a2f7b649c1b97e3ada88b8950ed2b6 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 25 Jan 2024 10:41:38 +0000 Subject: [PATCH] Editor: Fix Theme.json application of custom root selector for styles. Theme.json stylesheets attempting to use a custom root selector are generated with in correct styles. Props aaronrobertshaw, get_dave, mukesh27. Fixes #60343. git-svn-id: https://develop.svn.wordpress.org/trunk@57352 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-theme-json.php | 4 +-- tests/phpunit/tests/theme/wpThemeJson.php | 30 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 4094e11524..f73781b2b3 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -935,7 +935,7 @@ class WP_Theme_JSON { if ( $duotone_support ) { $root_selector = wp_get_block_css_selector( $block_type ); - $duotone_selector = WP_Theme_JSON::scope_selector( $root_selector, $duotone_support ); + $duotone_selector = static::scope_selector( $root_selector, $duotone_support ); } } @@ -1078,7 +1078,7 @@ class WP_Theme_JSON { $setting_nodes[ $root_settings_key ]['selector'] = $options['root_selector']; } if ( false !== $root_style_key ) { - $setting_nodes[ $root_style_key ]['selector'] = $options['root_selector']; + $style_nodes[ $root_style_key ]['selector'] = $options['root_selector']; } } diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index 2a452ca2e1..1c99de56bd 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -3210,7 +3210,6 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase { * * @param array $styles An array with style definitions. * @param array $path Path to the desired properties. - * */ public function test_get_property_value_should_return_string_for_invalid_paths_or_null_values( $styles, $path ) { $reflection_class = new ReflectionClass( WP_Theme_JSON::class ); @@ -4939,4 +4938,33 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase { $this->assertEquals( $small_font, $styles['blocks']['core/quote']['variations']['plain']['typography']['fontSize'], 'Block variations: font-size' ); $this->assertEquals( $secondary_color, $styles['blocks']['core/quote']['variations']['plain']['color']['background'], 'Block variations: color' ); } + + /** + * Tests that a custom root selector is correctly applied when generating a stylesheet. + * + * @ticket 60343 + */ + public function test_get_stylesheet_custom_root_selector() { + $theme_json = new WP_Theme_JSON( + array( + 'version' => WP_Theme_JSON::LATEST_SCHEMA, + 'styles' => array( + 'color' => array( + 'text' => 'teal', + ), + ), + ), + 'default' + ); + + $options = array( 'root_selector' => '.custom' ); + $actual = $theme_json->get_stylesheet( array( 'styles' ), null, $options ); + + // Results also include root site blocks styles which hard code + // `body { margin: 0;}`. + $this->assertEquals( + 'body { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.custom{color: teal;}', + $actual + ); + } }