-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[Button Block]: add support for pseudo elements for the block and its variations on theme.json #71418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Button Block]: add support for pseudo elements for the block and its variations on theme.json #71418
Changes from 10 commits
79c25b9
3ce94d4
3716d13
e279758
4a6e64e
82ed4a3
a863948
37cb2fc
a1cc83f
a76d7d8
f26e38b
a7d75af
6eb2dba
5c834d4
bd87a59
005f04e
4604ff9
bd14372
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/10523 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/71418 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -590,6 +590,16 @@ class WP_Theme_JSON_Gutenberg { | |
| 'button' => array( ':link', ':any-link', ':visited', ':hover', ':focus', ':focus-visible', ':active' ), | ||
| ); | ||
|
|
||
| /** | ||
| * The valid pseudo-selectors that can be used for blocks. | ||
| * | ||
| * @since 6.1.0 | ||
| * @var array | ||
| */ | ||
| const VALID_BLOCK_PSEUDO_SELECTORS = array( | ||
| 'core/button' => array( ':hover', ':focus', ':focus-visible', ':active' ), | ||
| ); | ||
|
|
||
| /** | ||
| * The valid elements that can be found under styles. | ||
| * | ||
|
|
@@ -680,6 +690,33 @@ protected static function schema_in_root_and_per_origin( $schema ) { | |
| return $schema_in_root_and_per_origin; | ||
| } | ||
|
|
||
| /** | ||
| * Processes pseudo-selectors for any node (block or variation). | ||
| * | ||
| * @param array $node The node data (block or variation). | ||
| * @param string $base_selector The base selector. | ||
| * @param array $settings The theme settings. | ||
| * @param string $block_name The block name. | ||
| * @return array Array of pseudo-selector declarations. | ||
| */ | ||
| private static function process_pseudo_selectors( $node, $base_selector, $settings, $block_name ) { | ||
| $pseudo_declarations = array(); | ||
|
|
||
| if ( ! isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] ) ) { | ||
| return $pseudo_declarations; | ||
| } | ||
|
|
||
| foreach ( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] as $pseudo_selector ) { | ||
| if ( isset( $node[ $pseudo_selector ] ) ) { | ||
| $combined_selector = static::append_to_selector( $base_selector, $pseudo_selector ); | ||
| $declarations = static::compute_style_properties( $node[ $pseudo_selector ], $settings, null, null ); | ||
| $pseudo_declarations[ $combined_selector ] = $declarations; | ||
| } | ||
| } | ||
|
|
||
| return $pseudo_declarations; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a class name by an element name. | ||
| * | ||
|
|
@@ -1001,6 +1038,13 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n | |
| $schema_settings_blocks[ $block ] = static::VALID_SETTINGS; | ||
| $schema_styles_blocks[ $block ] = $styles_non_top_level; | ||
| $schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements; | ||
|
|
||
| // Add pseudo-selectors for blocks that support them | ||
scruffian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ( isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block ] ) ) { | ||
| foreach ( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block ] as $pseudo_selector ) { | ||
| $schema_styles_blocks[ $block ][ $pseudo_selector ] = $styles_non_top_level; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $block_style_variation_styles = static::VALID_STYLES; | ||
|
|
@@ -1023,7 +1067,18 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n | |
|
|
||
| $schema_styles_variations = array(); | ||
| if ( ! empty( $style_variation_names ) ) { | ||
| $schema_styles_variations = array_fill_keys( $style_variation_names, $block_style_variation_styles ); | ||
| foreach ( $style_variation_names as $variation_name ) { | ||
| $variation_schema = $block_style_variation_styles; | ||
|
|
||
| // Add pseudo-selectors to variations for blocks that support them | ||
scruffian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ( isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block ] ) ) { | ||
| foreach ( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block ] as $pseudo_selector ) { | ||
| $variation_schema[ $pseudo_selector ] = $styles_non_top_level; | ||
| } | ||
| } | ||
|
|
||
| $schema_styles_variations[ $variation_name ] = $variation_schema; | ||
| } | ||
| } | ||
|
|
||
| $schema_styles_blocks[ $block ]['variations'] = $schema_styles_variations; | ||
|
|
@@ -2663,7 +2718,11 @@ protected static function get_style_nodes( $theme_json, $selectors = array(), $o | |
| return $nodes; | ||
| } | ||
|
|
||
| $block_nodes = static::get_block_nodes( $theme_json, $selectors, $options ); | ||
| $block_options = $options; | ||
| if ( ! isset( $block_options['include_block_style_variations'] ) ) { | ||
| $block_options['include_block_style_variations'] = true; | ||
| } | ||
| $block_nodes = static::get_block_nodes( $theme_json, $selectors, $block_options ); | ||
| foreach ( $block_nodes as $block_node ) { | ||
| $nodes[] = $block_node; | ||
| } | ||
|
|
@@ -2815,6 +2874,23 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt | |
| 'variations' => $variation_selectors, | ||
| 'css' => $selector, | ||
| ); | ||
|
|
||
| // Handle any pseudo selectors for the block. | ||
| if ( isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $name ] ) ) { | ||
| foreach ( static::VALID_BLOCK_PSEUDO_SELECTORS[ $name ] as $pseudo_selector ) { | ||
| if ( isset( $theme_json['styles']['blocks'][ $name ][ $pseudo_selector ] ) ) { | ||
| $nodes[] = array( | ||
| 'name' => $name, | ||
| 'path' => array( 'styles', 'blocks', $name, $pseudo_selector ), | ||
| 'selector' => static::append_to_selector( $selector, $pseudo_selector ), | ||
| 'selectors' => $feature_selectors, | ||
| 'duotone' => $duotone_selector, | ||
| 'variations' => $variation_selectors, | ||
| 'css' => static::append_to_selector( $selector, $pseudo_selector ), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) { | ||
| foreach ( $theme_json['styles']['blocks'][ $name ]['elements'] as $element => $node ) { | ||
|
|
@@ -2913,6 +2989,12 @@ static function ( $split_selector ) use ( $clean_style_variation_selector ) { | |
| } | ||
| // 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 ); | ||
|
|
||
| // Process pseudo-selectors for this variation (e.g., :hover, :focus) | ||
scruffian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $block_name = isset( $block_metadata['name'] ) ? $block_metadata['name'] : ( in_array( 'blocks', $block_metadata['path'], true ) && count( $block_metadata['path'] ) >= 3 ? $block_metadata['path'][2] : null ); | ||
|
||
| $variation_pseudo_declarations = static::process_pseudo_selectors( $style_variation_node, $style_variation['selector'], $settings, $block_name ); | ||
| $style_variation_declarations = array_merge( $style_variation_declarations, $variation_pseudo_declarations ); | ||
|
|
||
| // Store custom CSS for the style variation. | ||
| if ( isset( $style_variation_node['css'] ) ) { | ||
| $style_variation_custom_css[ $style_variation['selector'] ] = $this->process_blocks_custom_css( $style_variation_node['css'], $style_variation['selector'] ); | ||
|
|
@@ -2936,6 +3018,23 @@ static function ( $split_selector ) use ( $clean_style_variation_selector ) { | |
| $element_pseudo_allowed = static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ]; | ||
| } | ||
|
|
||
| /* | ||
| * Check if we're processing a block pseudo-selector. | ||
| * $block_metadata['path'] = array( 'styles', 'blocks', 'core/button', ':hover' ); | ||
| */ | ||
| $is_processing_block_pseudo = false; | ||
| $block_pseudo_selector = null; | ||
| if ( in_array( 'blocks', $block_metadata['path'], true ) && count( $block_metadata['path'] ) >= 4 ) { | ||
| $block_name = $block_metadata['path'][2]; // 'core/button' | ||
| $last_path_element = $block_metadata['path'][ count( $block_metadata['path'] ) - 1 ]; // ':hover' | ||
|
|
||
| if ( isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] ) && | ||
| in_array( $last_path_element, static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ], true ) ) { | ||
| $is_processing_block_pseudo = true; | ||
| $block_pseudo_selector = $last_path_element; | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Check for allowed pseudo classes (e.g. ":hover") from the $selector ("a:hover"). | ||
| * This also resets the array keys. | ||
|
|
@@ -2965,6 +3064,14 @@ static function ( $pseudo_selector ) use ( $selector ) { | |
| && in_array( $pseudo_selector, static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ], true ) | ||
| ) { | ||
| $declarations = static::compute_style_properties( $node[ $pseudo_selector ], $settings, null, $this->theme_json, $selector, $use_root_padding ); | ||
| } elseif ( $is_processing_block_pseudo ) { | ||
| // Process block pseudo-selector styles | ||
| // For block pseudo-selectors, we need to get the block data first, then access the pseudo-selector | ||
scruffian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $block_name = $block_metadata['path'][2]; // 'core/button' | ||
| $block_data = _wp_array_get( $this->theme_json, array( 'styles', 'blocks', $block_name ), array() ); | ||
| $pseudo_data = isset( $block_data[ $block_pseudo_selector ] ) ? $block_data[ $block_pseudo_selector ] : array(); | ||
|
|
||
| $declarations = static::compute_style_properties( $pseudo_data, $settings, null, $this->theme_json, $selector, $use_root_padding ); | ||
| } else { | ||
| $declarations = static::compute_style_properties( $node, $settings, null, $this->theme_json, $selector, $use_root_padding ); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.