diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 9027c779a7eb3..9fb0cef9ec614 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1025,26 +1025,35 @@ private function process_pseudo_selectors( $node, $base_selector, $settings, $bl foreach ( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] as $pseudo_selector ) { if ( isset( $node[ $pseudo_selector ] ) ) { - $pseudo_node = $node[ $pseudo_selector ]; + $pseudo_node = $node[ $pseudo_selector ]; + $feature_declarations = array(); if ( is_array( $block_metadata ) ) { $feature_declarations = $this->get_feature_declarations_for_node( $block_metadata, $pseudo_node ); $feature_declarations = self::update_paragraph_text_indent_selector( $feature_declarations, $settings, $block_name ); $feature_declarations = self::update_button_width_declarations( $feature_declarations, $settings ); + } - foreach ( $feature_declarations as $feature_selector => $declarations ) { - $target_selector = is_array( $style_variation ) - ? static::get_block_style_variation_feature_selector( $style_variation, $feature_selector ) - : $feature_selector; - $combined_selector = static::append_to_selector( $target_selector, $pseudo_selector ); + // `$pseudo_node` has lost its feature styles, so check the untouched state. + $declarations = static::compute_style_properties( $pseudo_node, $settings, null, null ); + $declarations = self::get_state_declarations_with_text_clip_escapes( + $declarations, + $node[ $pseudo_selector ], + $base_styles, + $block_metadata['selectors'] ?? array(), + $feature_declarations + ); - $add_declarations( $combined_selector, $declarations ); - } + foreach ( $feature_declarations as $feature_selector => $individual_feature_declarations ) { + $target_selector = is_array( $style_variation ) + ? static::get_block_style_variation_feature_selector( $style_variation, $feature_selector ) + : $feature_selector; + $combined_selector = static::append_to_selector( $target_selector, $pseudo_selector ); + + $add_declarations( $combined_selector, $individual_feature_declarations ); } $combined_selector = static::append_to_selector( $base_selector, $pseudo_selector ); - $declarations = static::compute_style_properties( $pseudo_node, $settings, null, null ); - $declarations = self::get_state_declarations_with_text_clip_escapes( $declarations, $pseudo_node, $base_styles ); $add_declarations( $combined_selector, $declarations ); } } @@ -4077,17 +4086,25 @@ private static function get_state_base_styles( array $path, array $tree, array $ /** * Adds the declarations a state needs to escape a text clip set below it. * - * @since 7.2.0 + * The text fill reset goes on the block's `color` feature selector and the + * clip escape on its `background` one, where those styles are output. + * Without those selectors, both stay on the state's own selector. * - * @param array[] $declarations Declarations generated for the state. - * @param array $state_style State style object. - * @param array[] $base_styles Style objects the state layers over, nearest first. + * @since 7.2.0 + * @since 7.3.0 Added the `$feature_selectors` and `$feature_declarations` parameters. + * + * @param array[] $declarations Declarations generated for the state. + * @param array $state_style State style object, as in the theme.json data. + * @param array[] $base_styles Style objects the state layers over, nearest first. + * @param array $feature_selectors Optional. The block's feature selectors. + * @param array $feature_declarations Optional. Feature declarations keyed by selector, + * which receive the escapes for feature selectors. * @return array[] Declarations with the escapes applied where needed. * * @phpstan-param list $declarations * @phpstan-return list */ - private static function get_state_declarations_with_text_clip_escapes( array $declarations, array $state_style, array $base_styles ): array { + private static function get_state_declarations_with_text_clip_escapes( array $declarations, array $state_style, array $base_styles, array $feature_selectors = array(), array &$feature_declarations = array() ): array { $base_clip = ''; foreach ( $base_styles as $base_style ) { if ( ! empty( $base_style['background']['backgroundClip'] ) ) { @@ -4100,16 +4117,29 @@ private static function get_state_declarations_with_text_clip_escapes( array $de return $declarations; } - $escapes = array_merge( - wp_get_state_text_clip_escape( $state_style, $base_clip ), - wp_get_state_text_fill_reset( $state_style ) + $escapes = array( + 'background' => wp_get_state_text_clip_escape( $state_style, $base_clip ), + 'color' => wp_get_state_text_fill_reset( $state_style ), ); - foreach ( $escapes as $name => $value ) { - $declarations[] = array( - 'name' => $name, - 'value' => $value, - ); + foreach ( $escapes as $feature => $properties ) { + $feature_selector = $feature_selectors[ $feature ] ?? null; + if ( is_array( $feature_selector ) ) { + $subfeature = 'color' === $feature ? 'text' : 'root'; + $feature_selector = $feature_selector[ $subfeature ] ?? $feature_selector['root'] ?? null; + } + + foreach ( $properties as $name => $value ) { + $declaration = array( + 'name' => $name, + 'value' => $value, + ); + if ( is_string( $feature_selector ) ) { + $feature_declarations[ $feature_selector ][] = $declaration; + } else { + $declarations[] = $declaration; + } + } } return $declarations; @@ -4159,8 +4189,16 @@ public function get_styles_for_block( $block_metadata ) { $style_variation_responsive_pseudo_css = array(); $style_variation_layout_metadata = array(); if ( ! $media_query && ! empty( $block_metadata['variations'] ) ) { + /* + * Feature selector extraction strips the nodes it reads, so the text clip + * checks read the theme.json data. A block pseudo node lists the variations + * too, so read the block's resting styles. + */ + $variation_block_node = _wp_array_get( $this->theme_json, array_slice( $block_metadata['path'], 0, 3 ), array() ); + foreach ( $block_metadata['variations'] as $style_variation ) { - $style_variation_node = _wp_array_get( $this->theme_json, $style_variation['path'], array() ); + $style_variation_node = _wp_array_get( $this->theme_json, $style_variation['path'], array() ); + $style_variation_source = $style_variation_node; // Generate any feature/subfeature style declarations for the current style variation. $variation_declarations = static::get_feature_declarations_for_node( $block_metadata, $style_variation_node ); @@ -4171,6 +4209,15 @@ public function get_styles_for_block( $block_metadata ) { // Update button width declarations for percentage values to use calc() with block gap. $variation_declarations = self::update_button_width_declarations( $variation_declarations, $settings ); + // Compute declarations for remaining styles not covered by feature level selectors. + $variation_base_declarations = self::get_state_declarations_with_text_clip_escapes( + static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json ), + $style_variation_source, + array( $variation_block_node ), + $block_metadata['selectors'] ?? array(), + $variation_declarations + ); + // Combine selectors with style variation's selector and add to overall style variation declarations. foreach ( $variation_declarations as $current_selector => $new_declarations ) { $combined_selectors = static::get_block_style_variation_feature_selector( $style_variation, $current_selector ); @@ -4178,9 +4225,7 @@ public function get_styles_for_block( $block_metadata ) { // Add the new declarations to the overall results under the modified selector. $style_variation_declarations[ $combined_selectors ] = $new_declarations; } - - // Compute declarations for remaining styles not covered by feature level selectors. - $style_variation_declarations[ $style_variation['selector'] ] = static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json ); + $style_variation_declarations[ $style_variation['selector'] ] = $variation_base_declarations; // Process pseudo-selectors for this variation (e.g., :hover, :focus) if ( isset( $block_metadata['name'] ) ) { @@ -4190,7 +4235,7 @@ public function get_styles_for_block( $block_metadata ) { } else { $block_name = null; } - $variation_pseudo_declarations = $this->process_pseudo_selectors( $style_variation_node, $style_variation['selector'], $settings, $block_name, $block_metadata, $style_variation ); + $variation_pseudo_declarations = $this->process_pseudo_selectors( $style_variation_source, $style_variation['selector'], $settings, $block_name, $block_metadata, $style_variation ); $style_variation_declarations = array_merge( $style_variation_declarations, $variation_pseudo_declarations ); // Custom CSS targets the block's `css` feature selector, if any. @@ -4241,6 +4286,17 @@ public function get_styles_for_block( $block_metadata ) { $breakpoint_feature_declarations = static::get_feature_declarations_for_node( $block_metadata, $breakpoint_node ); $breakpoint_feature_declarations = self::update_paragraph_text_indent_selector( $breakpoint_feature_declarations, $settings, $block_name ); $breakpoint_feature_declarations = self::update_button_width_declarations( $breakpoint_feature_declarations, $settings ); + + // Process base properties for this breakpoint. + $breakpoint_declarations = static::compute_style_properties( $breakpoint_node, $settings, null, $this->theme_json ); + $breakpoint_declarations = self::get_state_declarations_with_text_clip_escapes( + $breakpoint_declarations, + $style_variation_source[ $breakpoint ], + array( $style_variation_source, $variation_block_node ), + $block_metadata['selectors'] ?? array(), + $breakpoint_feature_declarations + ); + foreach ( $breakpoint_feature_declarations as $feature_selector => $feature_decl ) { $combined_selectors = static::get_block_style_variation_feature_selector( $style_variation, $feature_selector ); @@ -4248,19 +4304,12 @@ public function get_styles_for_block( $block_metadata ) { $variation_responsive_css .= $breakpoint_media . '{' . $feature_ruleset . '}'; } - // Process base properties for this breakpoint. - $breakpoint_declarations = static::compute_style_properties( $breakpoint_node, $settings, null, $this->theme_json ); - $breakpoint_declarations = self::get_state_declarations_with_text_clip_escapes( - $breakpoint_declarations, - $breakpoint_node, - array( $style_variation_node, $node ) - ); if ( ! empty( $breakpoint_declarations ) ) { $base_ruleset = static::to_ruleset( ':root :where(' . $style_variation['selector'] . ')', $breakpoint_declarations ); $variation_responsive_css .= $breakpoint_media . '{' . $base_ruleset . '}'; } - $breakpoint_pseudo_declarations = $this->process_pseudo_selectors( $breakpoint_node, $style_variation['selector'], $settings, $block_name, $block_metadata, $style_variation ); + $breakpoint_pseudo_declarations = $this->process_pseudo_selectors( $style_variation_source[ $breakpoint ], $style_variation['selector'], $settings, $block_name, $block_metadata, $style_variation ); foreach ( $breakpoint_pseudo_declarations as $pseudo_selector => $pseudo_declarations ) { if ( empty( $pseudo_declarations ) ) { continue; @@ -4398,7 +4447,13 @@ static function ( $pseudo_selector ) use ( $selector ) { * so those properties are not output twice. */ $declarations = static::compute_style_properties( $node, $settings, null, $this->theme_json, $selector, $use_root_padding ); - $declarations = self::get_state_declarations_with_text_clip_escapes( $declarations, $node, $base_styles ); + $declarations = self::get_state_declarations_with_text_clip_escapes( + $declarations, + _wp_array_get( $this->theme_json, $block_metadata['path'], array() ), + $base_styles, + $block_metadata['selectors'] ?? array(), + $feature_declarations + ); } $block_rules = ''; diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index 56c448ee1c43a..6568c0b1f44df 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -1814,6 +1814,142 @@ public function test_pseudo_state_inside_a_breakpoint_reads_the_breakpoint_clip( ); } + /** + * @ticket 66465 + */ + public function test_breakpoint_text_color_gets_its_fill_back_on_the_color_feature_selector() { + $theme_json = new WP_Theme_JSON( + array( + 'version' => WP_Theme_JSON::LATEST_SCHEMA, + 'settings' => array( 'viewport' => array( 'mobile' => '480px' ) ), + 'styles' => array( + 'blocks' => array( + 'core/search' => array( + 'background' => array( + 'gradient' => 'linear-gradient(135deg,#ff0000,#0000ff)', + 'backgroundClip' => 'text', + ), + '@mobile' => array( 'color' => array( 'text' => '#00ff00' ) ), + ), + ), + ), + ) + ); + + $stylesheet = $theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) ); + + $this->assertStringContainsString( + '@media (width <= 480px){:root :where(.wp-block-search .wp-block-search__button, .wp-block-search.wp-block-search__no-button .wp-block-search__input){color: #00ff00;-webkit-text-fill-color: currentColor;}}', + $stylesheet + ); + } + + /** + * @ticket 66465 + */ + public function test_variation_text_color_gets_its_fill_back_on_the_color_feature_selector() { + register_block_style( + 'core/search', + array( + 'name' => 'flashy', + 'label' => 'Flashy', + ) + ); + + $theme_json = new WP_Theme_JSON( + array( + 'version' => WP_Theme_JSON::LATEST_SCHEMA, + 'styles' => array( + 'blocks' => array( + 'core/search' => array( + 'background' => array( + 'gradient' => 'linear-gradient(135deg,#ff0000,#0000ff)', + 'backgroundClip' => 'text', + ), + 'variations' => array( + 'flashy' => array( 'color' => array( 'text' => '#00ff00' ) ), + ), + ), + ), + ), + ) + ); + + $stylesheet = $theme_json->get_stylesheet( + array( 'styles' ), + null, + array( + 'skip_root_layout_styles' => true, + 'include_block_style_variations' => true, + ) + ); + + unregister_block_style( 'core/search', 'flashy' ); + + $this->assertStringContainsString( + ':root :where(.wp-block-search.is-style-flashy .wp-block-search__button, .wp-block-search.wp-block-search__no-button.is-style-flashy .wp-block-search__input){color: #00ff00;-webkit-text-fill-color: currentColor;}', + $stylesheet + ); + } + + /** + * @ticket 66465 + */ + public function test_variation_text_clip_check_keeps_the_variation_gradient() { + register_block_style( + 'core/post-navigation-link', + array( + 'name' => 'flashy', + 'label' => 'Flashy', + ) + ); + + $theme_json = new WP_Theme_JSON( + array( + 'version' => WP_Theme_JSON::LATEST_SCHEMA, + 'settings' => array( 'viewport' => array( 'mobile' => '480px' ) ), + 'styles' => array( + 'blocks' => array( + 'core/post-navigation-link' => array( + 'background' => array( + 'gradient' => 'linear-gradient(135deg,#ff0000,#0000ff)', + 'backgroundClip' => 'text', + ), + 'variations' => array( + 'flashy' => array( + 'background' => array( 'gradient' => 'linear-gradient(135deg,#00ff00,#ffff00)' ), + 'color' => array( 'text' => '#00ff00' ), + '@mobile' => array( 'color' => array( 'text' => '#0000ff' ) ), + ), + ), + ), + ), + ), + ) + ); + + $stylesheet = $theme_json->get_stylesheet( + array( 'styles' ), + null, + array( + 'skip_root_layout_styles' => true, + 'include_block_style_variations' => true, + ) + ); + + unregister_block_style( 'core/post-navigation-link', 'flashy' ); + + // The variation paints its own gradient, so only its mobile text color needs the fill. + $this->assertStringContainsString( + ':root :where(.wp-block-post-navigation-link.is-style-flashy){color: #00ff00;}', + $stylesheet + ); + $this->assertStringContainsString( + '@media (width <= 480px){:root :where(.wp-block-post-navigation-link.is-style-flashy){color: #0000ff;-webkit-text-fill-color: currentColor;}}', + $stylesheet + ); + } + /** * @ticket 65596 */