From 34f543f64694676c754863e55b3cd32f8c600a16 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 14 Aug 2026 15:06:40 +0530 Subject: [PATCH] Block Supports: Bail early in state styles when a block has no style attribute. `wp_render_block_states_support()` is registered on `render_block`, so it runs once per block on every request. Before determining that a block has no state styles, it performed a block type registry lookup, resolved global settings via `wp_get_global_settings()`, and computed responsive media queries through `WP_Theme_JSON::get_viewport_media_queries()`. Every CSS rule the function can produce is keyed off the block's `style` attribute, and the function already returns the content unchanged when no rules are generated. Bailing out as soon as `style` is absent is therefore behaviour-preserving, and skips that work for the overwhelming majority of blocks, which carry no `style` attribute at all. Measured on trunk with a `core/paragraph` block that has no `style` attribute: 1396.6 ns/block before, 100.4 ns/block after. Output verified byte-identical across styled and unstyled fixtures for `core/button`, `core/navigation-link`, and `core/paragraph`, plus empty-style, no-attrs, and unregistered-block cases. Co-Authored-By: Claude Opus 5 (1M context) --- src/wp-includes/block-supports/states.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-supports/states.php b/src/wp-includes/block-supports/states.php index 7b5f0af18e800..c13405d79d177 100644 --- a/src/wp-includes/block-supports/states.php +++ b/src/wp-includes/block-supports/states.php @@ -542,6 +542,16 @@ function wp_render_block_states_support( $block_content, $block ) { 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 ) { @@ -549,7 +559,6 @@ function wp_render_block_states_support( $block_content, $block ) { } $supported_pseudo_states = WP_Theme_JSON::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] ?? array(); - $style = $block['attrs']['style'] ?? array(); $css_rules = array(); $viewport_settings = wp_get_global_settings( array( 'viewport' ) ); $responsive_media_queries = WP_Theme_JSON::get_viewport_media_queries( $viewport_settings );