- md5
- 4f2f7babb8ee39a23f81781d1d1bb188
- sha1
- 3fafd2dd156735c043167411ac4a3a176d84e053
- sha256
- c98f9a80e0a8140894817c613ac20efd225c42e73ec58d3951b9c79c1fa68f7c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
<?php
/**
* Block state support for frontend CSS generation.
*
* Generates scoped CSS for per-instance state styles declared in block attributes,
* including pseudo-states (e.g., `style[':hover']`) and responsive states
* (e.g., `style['@mobile']` and `style['@mobile'][':hover']`).
*
* @package WordPress
* @since 7.1.0
*/
/**
* Converts internal preset references to CSS custom property references.
*
* State styles are emitted as CSS rules and cannot rely on preset classnames.
* Converting `var:preset|color|contrast` to
* `var(--wp--preset--color--contrast)` ensures preset values are emitted as
* declarations by the style engine.
*
* @since 7.1.0
*
* @param mixed $value Style value to normalize.
* @return mixed Normalized style value.
*/
function wp_normalize_state_preset_vars( $value ) {
if ( is_array( $value ) ) {
foreach ( $value as $key => $nested_value ) {
$value[ $key ] = wp_normalize_state_preset_vars( $nested_value );
}
return $value;
}
if ( ! is_string( $value ) || ! str_starts_with( $value, 'var:preset|' ) ) {
return $value;
}
$parts = explode( '|', substr( $value, strlen( 'var:' ) ) );
/*
* The slug is kebab-cased when the preset's custom property is generated,
* so a reference has to be converted the same way or it points at a
* property that does not exist (`--wp--preset--font-size--3xl` for a preset
* generated as `--wp--preset--font-size--3-xl`). Mirrors
* `WP_Theme_JSON::convert_custom_properties()` and the JavaScript style
* engine's `getCSSValueFromRawStyle()`.
*/
if ( 3 === count( $parts ) ) {
$parts[2] = _wp_to_kebab_case( $parts[2] );
}
return 'var(--wp--' . implode( '--', $parts ) . ')';
}
/**
* Normalizes a state style object before generating CSS declarations.
*
* @since 7.1.0
*
* @param array $style State style object.
* @return array Normalized state style object.
*/
function wp_normalize_state_style_for_css_output( $style ) {
// Layout is processed separately by wp_render_layout_support_flag(), so we remove it before declaration generation.
unset( $style['layout'] );
$style = wp_normalize_state_preset_vars( $style );
return $style;
}
/**
* Adds fallback border-style declarations for visible border declarations.
*
* CSS does not render border color or width unless a border style is also set.
* State styles are emitted as stylesheet rules rather than inline styles, so
* they cannot rely on the block-library inline-style attribute fallback rules.
*
* @since 7.1.0
*
* @param array $declarations CSS declarations generated by the style engine.
* @return array CSS declarations with fallback border styles applied where needed.
*/
function wp_get_state_declarations_with_fallback_border_styles( $declarations ) {
if ( ! is_array( $declarations ) ) {
return $declarations;
}
$has_border_style = isset( $declarations['border-style'] ) && '' !== $declarations['border-style'];
$has_border_color = isset( $declarations['border-color'] ) && '' !== $declarations['border-color'];
$has_border_width = isset( $declarations['border-width'] ) && '' !== $declarations['border-width'];
if ( ! $has_border_style && ( $has_border_color || $has_border_width ) ) {
$declarations['border-style'] = 'solid';
}
$sides = array( 'top', 'right', 'bottom', 'left' );
foreach ( $sides as $side ) {
$side_style_property = "border-$side-style";
$side_color_property = "border-$side-color";
$side_width_property = "border-$side-width";
$has_side_style = isset( $declarations[ $side_style_property ] ) && '' !== $declarations[ $side_style_property ];
$has_side_color = isset( $declarations[ $side_color_property ] ) && '' !== $declarations[ $side_color_property ];
$has_side_width = isset( $declarations[ $side_width_property ] ) && '' !== $declarations[ $side_width_property ];
if ( ! $has_border_style && ! $has_side_style && ( $has_side_color || $has_side_width ) ) {
$declarations[ $side_style_property ] = 'solid';
}
}
return $declarations;
}
/**
* Adds background reset declarations to prevent gradient/solid color conflicts.
*
* When a state sets a solid background-color, any gradient applied to the
* default state (via `background` shorthand or `background-image`) must be
* explicitly cleared. Without this, the gradient image layer remains visible
* on top of the solid hover color even when `!important` is used, because
* `background-color` and `background-image` are separate CSS properties.
*
* @since 7.1.0
*
* @param array $declarations CSS declarations generated by the style engine.
* @return array CSS declarations with background resets applied where needed.
*/
function wp_get_state_declarations_with_background_resets( $declarations ) {
if ( ! is_array( $declarations ) ) {
return $declarations;
}
$has_background_color = isset( $declarations['background-color'] ) && '' !== $declarations['background-color'];
$has_background = isset( $declarations['background'] ) && '' !== $declarations['background'];
$has_background_image = isset( $declarations['background-image'] ) && '' !== $declarations['background-image'];
/*
* When the state sets a solid background-color but no gradient of its own,
* emit `background-image: unset` to clear any gradient (whether stored as
* the `background` shorthand or as `background-image`) that was applied to
* the default / normal state via an inline style attribute. The declaration
* is marked important when the state rule is registered with the style engine.
*/
if ( $has_background_color && ! $has_background && ! $has_background_image ) {
$declarations['background-image'] = 'unset';
}
return $declarations;
}
/**
* Merges one state style object over another.
*
* Mirror of `mergeStyleObjects` in packages/block-editor/src/hooks/style.jsx.
*
* @since 7.2.0
*
* @param array<string, mixed> $target Styles underneath.
* @param array<string, mixed> $source Styles layered over them.
* @return array<string, mixed> Merged style object.
*/
function wp_merge_state_style_objects( $target, $source ) {
$target = is_array( $target ) ? $target : array();
$source = is_array( $source ) ? $source : array();
$merged = $target;
foreach ( $source as $key => $value ) {
$merged[ $key ] = is_array( $value ) && isset( $merged[ $key ] ) && is_array( $merged[ $key ] )
? wp_merge_state_style_objects( $merged[ $key ], $value )
: $value;
}
return $merged;
}
/**
* Checks whether a state paints its own background.
*
* @since 7.2.0
* @access private
*
* @param array<string, mixed> $state_style State style object.
* @return bool Whether the state sets a gradient or background image.
*/
function wp_state_paints_own_background( array $state_style ): bool {
return ! empty( $state_style['background']['gradient'] ) ||
! empty( $state_style['background']['backgroundImage'] ) ||
! empty( $state_style['color']['gradient'] );
}
/**
* Returns the declarations that unclip a text clip when a state sets a different clip.
*
* Mirror of `getStateTextClipEscapeCSS` in packages/block-editor/src/hooks/style.jsx.
*
* @since 7.2.0
*
* @param array<string, mixed> $state_style State style object.
* @param string $base_clip Background clip of the styles the state layers over.
* @return array<string, string> CSS declarations, empty when no escape is needed.
*
* @phpstan-return array<non-falsy-string, non-falsy-string>
*/
function wp_get_state_text_clip_escape( array $state_style, string $base_clip ): array {
$state_clip = $state_style['background']['backgroundClip'] ?? '';
if ( ! in_array( $state_clip, WP_Style_Engine::VALID_BACKGROUND_CLIP_VALUES, true ) ) {
$state_clip = '';
}
if ( empty( $state_clip ) || 'text' === $state_clip || 'text' !== $base_clip || wp_state_paints_own_background( $state_style ) ) {
return array();
}
return array( 'background-image' => 'unset' );
}
/**
* Returns the declarations that refill the letters when a state sets a text color but no clip.
*
* The fill is added even without a known text clip, since a block instance
* cannot see a clip set in global styles. It does nothing when no clip applies.
*
* Mirror of `getStateTextFillResetCSS` in packages/block-editor/src/hooks/style.jsx.
*
* @since 7.2.0
*
* @param array<string, mixed> $state_style State style object.
* @return array<string, string> CSS declarations, empty when no reset is needed.
*
* @phpstan-return array<non-falsy-string, non-falsy-string>
*/
function wp_get_state_text_fill_reset( array $state_style ): array {
$state_clip = $state_style['background']['backgroundClip'] ?? '';
if ( ! in_array( $state_clip, WP_Style_Engine::VALID_BACKGROUND_CLIP_VALUES, true ) ) {
$state_clip = '';
}
if ( ! empty( $state_clip ) || empty( $state_style['color']['text'] ) || wp_state_paints_own_background( $state_style ) ) {
return array();
}
return array( '-webkit-text-fill-color' => 'currentColor' );
}
/**
* Adds fallback dimension styles for aspectRatio and height block-support values.
*
* @since 7.1.0
*
* @param array $state_style State style object.
* @return array State style object with fallback dimension styles applied where needed.
*/
function wp_get_state_style_with_fallback_dimension_styles( $state_style ) {
if ( ! is_array( $state_style ) ) {
return $state_style;
}
$dimensions = isset( $state_style['dimensions'] ) && is_array( $state_style['dimensions'] )
? $state_style['dimensions']
: array();
if ( empty( $dimensions ) ) {
return $state_style;
}
if ( wp_is_explicit_aspect_ratio_value( $dimensions['aspectRatio'] ?? null ) ) {
return array_replace_recursive(
$state_style,
array(
'dimensions' => array(
'minHeight' => 'unset',
'height' => 'unset',
),
)
);
}
$has_min_height = isset( $dimensions['minHeight'] ) && ( is_string( $dimensions['minHeight'] ) || is_numeric( $dimensions['minHeight'] ) ) && '' !== trim( (string) $dimensions['minHeight'] );
$has_height = isset( $dimensions['height'] ) && ( is_string( $dimensions['height'] ) || is_numeric( $dimensions['height'] ) ) && '' !== trim( (string) $dimensions['height'] );
if ( $has_min_height || $has_height ) {
return array_replace_recursive(
$state_style,
array(
'dimensions' => array(
'aspectRatio' => 'unset',
),
)
);
}
return $state_style;
}
/**
* Adds a style fragment to a selector-keyed state style group.
*
* @since 7.1.0
*
* @param array $groups Selector-keyed style groups.
* @param string|null $selector Block or feature selector.
* @param array $style Style fragment.
*/
function wp_add_state_style_group( &$groups, $selector, $style ) {
$key = is_string( $selector ) ? $selector : '';
if ( ! isset( $groups[ $key ] ) ) {
$groups[ $key ] = array(
'selector' => $selector,
'style' => array(),
);
}
$groups[ $key ]['style'] = array_replace_recursive( $groups[ $key ]['style'], $style );
}
/**
* Splits a state style object into groups based on block feature selectors.
*
* @since 7.1.0
*
* @param array $state_style State style object.
* @param array $block_selectors Block selectors metadata.
* @return array[] Selector/style groups.
*/
function wp_get_state_style_groups( $state_style, $block_selectors ) {
$groups = array();
foreach ( $state_style as $feature => $feature_styles ) {
$feature_selectors = $block_selectors[ $feature ] ?? null;
if ( is_string( $feature_selectors ) ) {
wp_add_state_style_group(
$groups,
$feature_selectors,
array( $feature => $feature_styles )
);
continue;
}
if ( is_array( $feature_selectors ) && is_array( $feature_styles ) ) {
$remaining_styles = $feature_styles;
foreach ( $feature_selectors as $subfeature => $subfeature_selector ) {
if (
'root' === $subfeature ||
! is_string( $subfeature_selector ) ||
! array_key_exists( $subfeature, $feature_styles )
) {
continue;
}
wp_add_state_style_group(
$groups,
$subfeature_selector,
array(
$feature => array(
$subfeature => $feature_styles[ $subfeature ],
),
)
);
unset( $remaining_styles[ $subfeature ] );
}
if ( array() !== $remaining_styles ) {
wp_add_state_style_group(
$groups,
$feature_selectors['root'] ?? ( $block_selectors['root'] ?? null ),
array( $feature => $remaining_styles )
);
}
continue;
}
wp_add_state_style_group(
$groups,
$block_selectors['root'] ?? null,
array( $feature => $feature_styles )
);
}
return array_values( $groups );
}
/**
* Returns a style object with nested state keys removed.
*
* @since 7.1.0
*
* @param array $state_style State style object.
* @param array $nested_keys Keys to remove from the root style object.
* @return array Root-only style object.
*/
function wp_get_root_state_style( $state_style, $nested_keys ) {
if ( ! is_array( $state_style ) ) {
return $state_style;
}
$root_style = $state_style;
foreach ( $nested_keys as $key ) {
unset( $root_style[ $key ] );
}
return $root_style;
}
/**
* Generates all element selectors for a block root selector.
*
* @since 7.1.0
*
* @param string $root_selector The block root CSS selector.
* @return string[] Element selectors keyed by element name.
*/
function wp_get_block_state_element_selectors( $root_selector ) {
if ( ! is_string( $root_selector ) || '' === trim( $root_selector ) ) {
return array();
}
$block_selectors = wp_split_selector_list( $root_selector );
$element_selectors = array();
foreach ( WP_Theme_JSON::ELEMENTS as $element_name => $element_selector ) {
$selectors = array();
foreach ( $block_selectors as $block_selector ) {
$block_selector = trim( $block_selector );
if ( '' === $block_selector ) {
continue;
}
if ( $block_selector === $element_selector ) {
$selectors = array( $element_selector );
break;
}
$selector_prefix = "$block_selector ";
if ( ! str_contains( $element_selector, ',' ) ) {
$selectors[] = $selector_prefix . $element_selector;
continue;
}
$prepended_selectors = array();
foreach ( wp_split_selector_list( $element_selector ) as $selector ) {
$prepended_selectors[] = $selector_prefix . $selector;
}
$selectors[] = implode( ',', $prepended_selectors );
}
if ( ! empty( $selectors ) ) {
$element_selectors[ $element_name ] = implode( ',', $selectors );
}
}
return $element_selectors;
}
/**
* Adds a compiled state style rule to a rule list.
*
* @since 7.1.0
* @since 7.2.0 Added fallback dimension styles.
* Added the `$base_style` parameter.
*
* @param array[] $css_rules Style rules.
* @param string $state Pseudo-state selector.
* @param string|null $selector Block, feature, or element selector.
* @param array<string, mixed> $style Style object.
* @param string|null $rules_group Optional CSS grouping rule, e.g. a media query.
* @param array<string, mixed>|null $base_style Optional styles the state layers over.
*/
function wp_add_block_state_style_rule( &$css_rules, $state, $selector, $style, $rules_group = null, $base_style = null ) {
if ( empty( $style ) || ! is_array( $style ) ) {
return;
}
$style = wp_get_state_style_with_fallback_dimension_styles( $style );
$compiled = wp_style_engine_get_styles(
wp_normalize_state_style_for_css_output( $style )
);
$declarations = $compiled['declarations'] ?? array();
$text_align = $style['typography']['textAlign'] ?? null;
// Base text alignment is class-based, so state styles need a declaration.
if ( is_string( $text_align ) && '' !== trim( $text_align ) ) {
$declarations['text-align'] = $text_align;
}
// Only block states pass a base; element states match the editor and skip it.
if ( null !== $base_style ) {
$base_clip = $base_style['background']['backgroundClip'] ?? '';
$declarations = array_merge(
$declarations,
wp_get_state_text_clip_escape( $style, is_string( $base_clip ) ? $base_clip : '' ),
wp_get_state_text_fill_reset( $style )
);
}
if ( empty( $declarations ) ) {
return;
}
$css_rules[] = array(
'state' => $state,
'selector' => $selector,
'declarations' => $declarations,
);
if ( ! empty( $rules_group ) ) {
$css_rules[ count( $css_rules ) - 1 ]['rules_group'] = $rules_group;
}
}
/**
* Builds compiled state style rules, preserving the selector each rule targets.
*
* @since 7.1.0
* @since 7.2.0 Added the `$base_style` parameter.
*
* @param array<string, mixed> $state_styles Map of state to style array.
* @param WP_Block_Type $block_type Block type.
* @param string|null $rules_group Optional CSS grouping rule, e.g. a media query.
* @param array<string, mixed>|null $base_style Optional styles the state layers over.
* @return array[] State style rules.
*/
function wp_get_block_state_style_rules( $state_styles, $block_type, $rules_group = null, $base_style = null ) {
// Only a block that supports the clip can sit under a text clip to escape.
if ( ! block_has_support( $block_type, array( 'background', 'backgroundClip' ), false ) ) {
$base_style = null;
}
$css_rules = array();
$block_selectors = is_array( $block_type->selectors )
? $block_type->selectors
: array();
foreach ( $state_styles as $state => $state_style ) {
if ( empty( $state_style ) || ! is_array( $state_style ) ) {
continue;
}
foreach ( wp_get_state_style_groups( $state_style, $block_selectors ) as $group ) {
wp_add_block_state_style_rule(
$css_rules,
$state,
$group['selector'],
$group['style'],
$rules_group,
$base_style
);
}
}
return $css_rules;
}
/**
* Returns a unique class for a set of state style rules.
*
* @since 7.1.0
*
* @param string $block_name Block name.
* @param array $css_rules State style rules.
* @return string Unique class name.
*/
function wp_get_block_state_unique_class( $block_name, $css_rules ) {
return 'wp-states-' . substr(
md5(
wp_json_encode(
array(
'blockName' => $block_name,
'rules' => $css_rules,
)
)
),
0,
8
);
}
/**
* Splits a selector list by top-level commas.
*
* @since 7.1.0
*
* @param string $selector CSS selector list.
* @return string[] Selectors.
*/
function wp_split_selector_list( $selector ) {
if ( ! str_contains( $selector, ',' ) ) {
return array( $selector );
}
$selectors = array();
$current_selector = '';
$parentheses_depth = 0;
$selector_length = strlen( $selector );
for ( $i = 0; $i < $selector_length; $i++ ) {
$char = $selector[ $i ];
if ( '(' === $char ) {
++$parentheses_depth;
} elseif ( ')' === $char && $parentheses_depth > 0 ) {
--$parentheses_depth;
} elseif ( ',' === $char && 0 === $parentheses_depth ) {
$selectors[] = $current_selector;
$current_selector = '';
continue;
}
$current_selector .= $char;
}
$selectors[] = $current_selector;
return $selectors;
}
/**
* Returns the ancestor part of a block selector whose leading compound names
* a different block, e.g. `.wp-block-list > ` in List Item's `.wp-block-list > li`.
*
* The block-instance class is on the block's own wrapper, so a leading compound
* naming another block must be kept rather than swapped for that class. When the
* next compound carries a `wp-block-*` class (`.wp-block-button .wp-block-button__link`),
* the selector describes a nested element and keeps the leading swap.
*
* @since 7.2.0
*
* @param string $selector A single block or feature selector.
* @param string|null $block_name Name of the block being scoped.
* @return string The leading compound and its combinator, or an empty string.
*/
function wp_get_state_selector_ancestor( $selector, $block_name ) {
if (
! is_string( $block_name ) ||
'' === $block_name ||
! preg_match( '/^\.(wp-block-[-_a-zA-Z0-9]+)/', $selector, $matches ) ||
wp_get_block_default_classname( $block_name ) === $matches[1]
) {
return '';
}
$ancestor_length = 0;
$next_compound = '';
$depth = 0;
$selector_length = strlen( $selector );
for ( $i = 0; $i < $selector_length; $i++ ) {
$char = $selector[ $i ];
if ( '(' === $char || '[' === $char ) {
++$depth;
} elseif ( ( ')' === $char || ']' === $char ) && $depth > 0 ) {
--$depth;
} elseif ( 0 === $depth && str_contains( " \t\n\r\f>+~", $char ) ) {
if ( $ancestor_length ) {
break;
}
$ancestor_length = $i + strspn( $selector, " \t\n\r\f>+~", $i );
$i = $ancestor_length - 1;
} elseif ( 0 === $depth && $ancestor_length ) {
$next_compound .= $char;
}
}
if ( '' === $next_compound || str_contains( $next_compound, '.wp-block-' ) ) {
return '';
}
return substr( $selector, 0, $ancestor_length );
}
/**
* Builds a scoped selector from a block selector and optional pseudo-state.
*
* @since 7.1.0
* @since 7.2.0 Added the `$block_name` parameter.
*
* @param string $base_selector Block-instance scoping selector.
* @param string|null $block_selector Block or feature selector from metadata.
* @param string $state Pseudo-state selector.
* @param string|null $block_name Optional. Name of the block being scoped.
* @return string Scoped selector.
*/
function wp_build_state_selector( $base_selector, $block_selector, $state, $block_name = null ) {
if ( ! is_string( $block_selector ) || '' === trim( $block_selector ) ) {
return $base_selector . $state;
}
$selectors = wp_split_selector_list( $block_selector );
$scoped_selectors = array();
foreach ( $selectors as $selector ) {
$selector = trim( $selector );
if ( '' === $selector ) {
continue;
}
$ancestor = wp_get_state_selector_ancestor( $selector, $block_name );
$selector = substr( $selector, strlen( $ancestor ) );
/*
* Replace only the leading block selector part (e.g. class name,
* attribute selector, ID, or tag name) with the block instance selector.
* Preserve anything after that prefix, including modifier classes on the
* same element and combinators without spaces.
*/
if ( preg_match( '/^([.#]?[-_a-zA-Z0-9]+|\[[^\]]+\])/', $selector, $matches ) ) {
$scoped_selectors[] = $ancestor . $base_selector . substr( $selector, strlen( $matches[0] ) ) . $state;
continue;
}
$scoped_selectors[] = $ancestor . $base_selector . $state;
}
return empty( $scoped_selectors )
? $base_selector . $state
: implode( ', ', $scoped_selectors );
}
/**
* Renders per-instance state styles on the frontend.
*
* @since 7.1.0
*
* @param string $block_content The block's rendered HTML.
* @param array $block The block data including blockName and attrs.
* @return string Modified block content with injected state styles.
*/
function wp_render_block_states_support( $block_content, $block ) {
if ( empty( $block['blockName'] ) || empty( $block_content ) ) {
return $block_content;
}
/*
* Every CSS rule this function can produce is keyed off the block's `style`
* attribute. Without it there is nothing to generate, so bail before doing
* any of the lookups below — this runs for every block on every request.
*/
$style = $block['attrs']['style'] ?? array();
if ( empty( $style ) || ! is_array( $style ) ) {
return $block_content;
}
$block_name = $block['blockName'];
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
if ( ! $block_type ) {
return $block_content;
}
$supported_pseudo_states = WP_Theme_JSON::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] ?? array();
$css_rules = array();
$default_root_style = wp_get_root_state_style(
$style,
array_merge( array( 'elements' ), $supported_pseudo_states )
);
$viewport_settings = wp_get_global_settings( array( 'viewport' ) );
$responsive_media_queries = WP_Theme_JSON::get_viewport_media_queries( $viewport_settings );
foreach ( $supported_pseudo_states as $pseudo_state ) {
if ( empty( $style[ $pseudo_state ] ) || ! is_array( $style[ $pseudo_state ] ) ) {
continue;
}
$css_rules = array_merge(
$css_rules,
wp_get_block_state_style_rules(
array( $pseudo_state => $style[ $pseudo_state ] ),
$block_type,
null,
$default_root_style
)
);
}
foreach ( $responsive_media_queries as $breakpoint => $media_query ) {
if ( empty( $style[ $breakpoint ] ) || ! is_array( $style[ $breakpoint ] ) ) {
continue;
}
$root_state_style = wp_get_root_state_style(
$style[ $breakpoint ],
array_merge( array( 'elements' ), $supported_pseudo_states )
);
if ( ! empty( $root_state_style ) ) {
$css_rules = array_merge(
$css_rules,
wp_get_block_state_style_rules(
array( '' => $root_state_style ),
$block_type,
$media_query,
$default_root_style
)
);
}
if (
! empty( $style[ $breakpoint ]['elements'] ) &&
is_array( $style[ $breakpoint ]['elements'] )
) {
$element_selectors = wp_get_block_state_element_selectors(
wp_get_block_css_selector( $block_type )
);
foreach ( $style[ $breakpoint ]['elements'] as $element_name => $element_style ) {
if (
empty( $element_style ) ||
! is_array( $element_style ) ||
empty( $element_selectors[ $element_name ] )
) {
continue;
}
$element_pseudo_states = WP_Theme_JSON::VALID_ELEMENT_PSEUDO_SELECTORS[ $element_name ]
?? array();
$root_element_style = wp_get_root_state_style(
$element_style,
$element_pseudo_states
);
wp_add_block_state_style_rule(
$css_rules,
'',
$element_selectors[ $element_name ],
$root_element_style,
$media_query
);
foreach ( $element_pseudo_states as $pseudo_state ) {
if (
empty( $element_style[ $pseudo_state ] ) ||
! is_array( $element_style[ $pseudo_state ] )
) {
continue;
}
wp_add_block_state_style_rule(
$css_rules,
$pseudo_state,
$element_selectors[ $element_name ],
$element_style[ $pseudo_state ],
$media_query
);
}
}
}
foreach ( $supported_pseudo_states as $pseudo_state ) {
if ( empty( $style[ $breakpoint ][ $pseudo_state ] ) || ! is_array( $style[ $breakpoint ][ $pseudo_state ] ) ) {
continue;
}
$css_rules = array_merge(
$css_rules,
wp_get_block_state_style_rules(
array( $pseudo_state => $style[ $breakpoint ][ $pseudo_state ] ),
$block_type,
$media_query,
wp_merge_state_style_objects(
$default_root_style,
$root_state_style
)
)
);
}
}
if ( empty( $css_rules ) ) {
return $block_content;
}
$unique_class = wp_get_block_state_unique_class( $block_name, $css_rules );
/*
* Register each state's CSS rules with the block-supports style engine store.
* The store deduplicates rules by selector — two block instances with identical
* state styles share the same hash class and therefore the same selector,
* so only one CSS rule is emitted. The store is flushed to the page by
* wp_enqueue_stored_styles() rather than injected inline here.
*
* State declarations need !important to apply reliably over inline styles and
* preset utility classes such as .has-accent-3-background-color.
*
* Layout-driven state styles (responsive layout, blockGap, child layout) are
* handled by wp_render_layout_support_flag() so they share a selector with
* the base layout and target the correct (inner) wrapper element.
*/
$style_rules = array();
foreach ( $css_rules as $rule ) {
$declarations = $rule['declarations'];
$important_declaration_values = wp_get_state_declarations_with_background_resets( $declarations );
$important_declarations = new WP_Style_Engine_CSS_Declarations();
foreach ( $important_declaration_values as $property => $value ) {
$important_declarations->add_declaration(
$property,
$value,
array(
'important' => true,
)
);
}
$selector = wp_build_state_selector(
".$unique_class",
$rule['selector'],
$rule['state'],
$block_name
);
$important_style_rule = array(
'selector' => $selector,
'declarations' => $important_declarations,
);
if ( ! empty( $rule['rules_group'] ) ) {
$important_style_rule['rules_group'] = $rule['rules_group'];
}
$style_rules[] = $important_style_rule;
$fallback_declarations = wp_get_state_declarations_with_fallback_border_styles( $declarations );
foreach ( array_keys( $declarations ) as $property ) {
unset( $fallback_declarations[ $property ] );
}
if ( empty( $fallback_declarations ) ) {
continue;
}
$fallback_style_rule = array(
'selector' => $selector,
'declarations' => $fallback_declarations,
);
if ( ! empty( $rule['rules_group'] ) ) {
$fallback_style_rule['rules_group'] = $rule['rules_group'];
}
$style_rules[] = $fallback_style_rule;
}
wp_style_engine_get_stylesheet_from_css_rules(
$style_rules,
array(
'context' => 'block-supports',
'prettify' => false,
)
);
$processor = new WP_HTML_Tag_Processor( $block_content );
if ( $processor->next_tag() ) {
$processor->add_class( $unique_class );
}
return $processor->get_updated_html();
}
add_filter( 'render_block', 'wp_render_block_states_support', 10, 2 );
The same content is in 4 releases, WordPress 7.2-beta1 to 7.2-beta4. See them all, or every version of this file.