Skip to content
Open
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
129 changes: 92 additions & 37 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Expand Down Expand Up @@ -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<string, mixed> $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<string, mixed> $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<string, mixed> $feature_selectors Optional. The block's feature selectors.
* @param array<string, 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<array{ name: non-falsy-string, value: scalar }> $declarations
* @phpstan-return list<array{ name: non-falsy-string, value: scalar }>
*/
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'] ) ) {
Expand All @@ -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;
Expand Down Expand Up @@ -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 );
Expand All @@ -4171,16 +4209,23 @@ 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 );

// 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'] ) ) {
Expand All @@ -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.
Expand Down Expand Up @@ -4241,26 +4286,30 @@ 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 );

$feature_ruleset = static::to_ruleset( ':root :where(' . $combined_selectors . ')', $feature_decl );
$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;
Expand Down Expand Up @@ -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 = '';
Expand Down
136 changes: 136 additions & 0 deletions tests/phpunit/tests/theme/wpThemeJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading