diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 555898370f863..d59206f3fecf5 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1925,6 +1925,48 @@ public function get_styles_block_nodes() { return static::get_block_nodes( $this->theme_json ); } + /** + * Returns a filtered declarations array if there is a separator block with only a background + * style defined in theme.json by adding a color attribute to reflect the changes in the front. + * + * @since 6.1.1 + * + * @param array $declarations List of declarations. + * @return array $declarations List of declarations filtered. + */ + private static function update_separator_declarations( $declarations ) { + $background_matches = array_values( + array_filter( + $declarations, + static function( $declaration ) { + return 'background-color' === $declaration['name']; + } + ) + ); + + if ( isset( $background_matches[0]['value'] ) ) { + $border_color_matches = false; + $text_color_matches = false; + + foreach ( $declarations as $declaration ) { + if ( 'border-color' === $declaration['name'] ) { + $border_color_matches = true; + } elseif ( 'color' === $declaration['name'] ) { + $text_color_matches = true; + } + } + + if ( ! $border_color_matches && ! $text_color_matches ) { + $declarations[] = array( + 'name' => 'color', + 'value' => $background_matches[0]['value'], + ); + } + } + + return $declarations; + } + /** * An internal method to get the block nodes from a theme.json file. * @@ -2101,6 +2143,11 @@ function( $pseudo_selector ) use ( $selector ) { } } + // Update declarations if there are separators with only background color defined. + if ( '.wp-block-separator' === $selector ) { + $declarations = static::update_separator_declarations( $declarations ); + } + // 2. Generate and append the rules that use the general selector. $block_rules .= static::to_ruleset( $selector, $declarations ); diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index 56b1bec0e138c..5f373b52e2053 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -3997,4 +3997,96 @@ function data_set_spacing_sizes_when_invalid() { ), ); } + + /** + * Tests the core separator block outbut based on various provided settings. + * + * @ticket 56903 + * + * @dataProvider data_update_separator_declarations + * + * @param array $separator_block_settings Example separator block settings from the data provider. + * @param array $expected_output Expected output from data provider. + */ + public function test_update_separator_declarations( $separator_block_settings, $expected_output ) { + // If only background is defined, test that includes border-color to the style so it is applied on the front end. + $theme_json = new WP_Theme_JSON( + array( + 'version' => WP_Theme_JSON::LATEST_SCHEMA, + 'styles' => array( + 'blocks' => array( + 'core/separator' => $separator_block_settings, + ), + ), + ), + 'default' + ); + + $stylesheet = $theme_json->get_stylesheet( array( 'styles' ) ); + + $this->assertSame( $expected_output, $stylesheet ); + } + + /** + * Data provider for separator declaration tests. + * + * @return array + */ + function data_update_separator_declarations() { + return array( + // If only background is defined, test that includes border-color to the style so it is applied on the front end. + 'only background' => array( + array( + 'color' => array( + 'background' => 'blue', + ), + ), + 'expected_output' => '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; }.wp-block-separator{background-color: blue;color: blue;}', + ), + // If background and text are defined, do not include border-color, as text color is enough. + 'background and text, no border-color' => array( + array( + 'color' => array( + 'background' => 'blue', + 'text' => 'red', + ), + ), + 'expected_output' => '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; }.wp-block-separator{background-color: blue;color: red;}', + ), + // If only text is defined, do not include border-color, as by itself is enough. + 'only text' => array( + array( + 'color' => array( + 'text' => 'red', + ), + ), + 'expected_output' => '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; }.wp-block-separator{color: red;}', + ), + // If background, text, and border-color are defined, include everything, CSS specifity will decide which to apply. + 'background, text, and border-color' => array( + array( + 'color' => array( + 'background' => 'blue', + 'text' => 'red', + ), + 'border' => array( + 'color' => 'pink', + ), + ), + 'expected_output' => '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; }.wp-block-separator{background-color: blue;border-color: pink;color: red;}', + ), + // If background and border color are defined, include everything, CSS specifity will decide which to apply. + 'background, text, and border-color' => array( + array( + 'color' => array( + 'background' => 'blue', + ), + 'border' => array( + 'color' => 'pink', + ), + ), + 'expected_output' => '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; }.wp-block-separator{background-color: blue;border-color: pink;}', + ), + ); + } }