Skip to content
Draft
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
91 changes: 91 additions & 0 deletions src/wp-includes/block-supports/states.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Block states support for frontend CSS generation.
*
* Generates scoped CSS for per-instance pseudo-state styles (e.g., :hover, :focus)
* declared in block attributes under `style[':hover']`, `style[':focus']`, etc.
*
* @package WordPress
*/

/**
* Renders per-instance state styles on the frontend for blocks that declare
* `__experimentalStates` support.
*
* @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;
}

$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
if ( ! $block_type ) {
return $block_content;
}

$supported_states = $block_type->supports['__experimentalStates'] ?? null;
if ( empty( $supported_states ) || ! is_array( $supported_states ) ) {
return $block_content;
}

$style = $block['attrs']['style'] ?? array();
$css_rules = array();

foreach ( $supported_states as $state ) {
if ( empty( $style[ $state ] ) || ! is_array( $style[ $state ] ) ) {
continue;
}

$compiled = wp_style_engine_get_styles( $style[ $state ] );
if ( ! empty( $compiled['css'] ) ) {
$css_rules[] = array(
'state' => $state,
'css' => $compiled['css'],
);
}
}

if ( empty( $css_rules ) ) {
return $block_content;
}

$unique_class = 'wp-states-' . substr( md5( wp_json_encode( $css_rules ) ), 0, 8 );
$css = '';

foreach ( $css_rules as $rule ) {
// Use !important to override utility classes like
// .has-accent-3-background-color which are generated with !important.
$declarations = str_replace( ';', ' !important;', $rule['css'] );
$css .= ".$unique_class$rule[state] { $declarations }\n";
}

// Add the unique class to the interactive element so that state selectors
// like `.$unique_class:hover` match directly without needing a descendant.
// If the block declares selectors.root with a descendant (e.g. the button
// block's ".wp-block-button .wp-block-button__link"), we extract the last
// class and walk to that element. Otherwise we fall back to the wrapper.
Comment on lines +65 to +69
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go for a /* */ comment, and iirc they are recommended in the coding standards, didn't find it right now though.

$root_selector = $block_type->selectors['root'] ?? null;
$target_class = null;
if ( $root_selector && preg_match( '/\.([a-zA-Z0-9_-]+)\s*$/', $root_selector, $matches ) ) {
$target_class = $matches[1];
}

$processor = new WP_HTML_Tag_Processor( $block_content );
if ( $target_class ) {
while ( $processor->next_tag() ) {
if ( $processor->has_class( $target_class ) ) {
$processor->add_class( $unique_class );
break;
}
}
} elseif ( $processor->next_tag() ) {
$processor->add_class( $unique_class );
}
$block_content = $processor->get_updated_html();

return '<style>' . $css . '</style>' . $block_content;
}
add_filter( 'render_block', 'wp_render_block_states_support', 10, 2 );
1 change: 1 addition & 0 deletions src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@
require ABSPATH . WPINC . '/block-supports/anchor.php';
require ABSPATH . WPINC . '/block-supports/block-visibility.php';
require ABSPATH . WPINC . '/block-supports/custom-css.php';
require ABSPATH . WPINC . '/block-supports/states.php';
require ABSPATH . WPINC . '/style-engine.php';
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine.php';
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine-css-declarations.php';
Expand Down
Loading
Loading