From 6f5a040eb01426bab38ea779ab0d508eba064b99 Mon Sep 17 00:00:00 2001 From: anupkankale Date: Mon, 8 Jun 2026 05:28:27 +0000 Subject: [PATCH 1/2] Block Supports: Prevent Additional CSS duplication inside Query Loop When a block inside a Query Loop has Additional CSS, wp_add_inline_style was called once per post in the loop, duplicating the same CSS N times. This was because wp_unique_id_from_values produces the same hash for the same block data on every loop iteration. This fix adds a static $enqueued_class_names array to track already-enqueued class names so subsequent loop iterations skip the redundant wp_add_inline_style call. Props mustafabharmal, westonruter. Fixes #65268. --- src/wp-includes/block-supports/custom-css.php | 9 ++++ .../wpRenderCustomCssSupportStyles.php | 47 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/wp-includes/block-supports/custom-css.php b/src/wp-includes/block-supports/custom-css.php index 9d5b13426f4ef..0721dd6b288ad 100644 --- a/src/wp-includes/block-supports/custom-css.php +++ b/src/wp-includes/block-supports/custom-css.php @@ -49,6 +49,15 @@ function wp_render_custom_css_support_styles( $parsed_block ) { * The style depends on global-styles to ensure custom CSS loads after * and can override global styles. */ + static $enqueued_class_names = array(); + + // Skip if already enqueued — prevents duplication inside Query Loop. + if ( isset( $enqueued_class_names[ $class_name ] ) ) { + return $parsed_block; + } + + $enqueued_class_names[ $class_name ] = true; + wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); wp_add_inline_style( 'wp-block-custom-css', $processed_css ); } diff --git a/tests/phpunit/tests/block-supports/wpRenderCustomCssSupportStyles.php b/tests/phpunit/tests/block-supports/wpRenderCustomCssSupportStyles.php index 0048b379f8f33..bdd47df0520a5 100644 --- a/tests/phpunit/tests/block-supports/wpRenderCustomCssSupportStyles.php +++ b/tests/phpunit/tests/block-supports/wpRenderCustomCssSupportStyles.php @@ -264,4 +264,51 @@ public function data_does_not_add_class_name() { ), ); } + + /** + * Tests that CSS is not duplicated when the same block renders multiple times (e.g. inside a Query Loop). + * + * @ticket 65268 + * + * @covers ::wp_render_custom_css_support_styles + */ + public function test_css_not_duplicated_inside_query_loop() { + $this->test_block_name = 'test/custom-css-query-loop'; + register_block_type( + $this->test_block_name, + array( + 'api_version' => 3, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( 'customCSS' => true ), + ) + ); + + $parsed_block = array( + 'blockName' => 'test/custom-css-query-loop', + 'attrs' => array( + 'style' => array( + 'css' => 'color: fuchsia;', + ), + ), + ); + + // Simulate the same block rendering 3 times inside a Query Loop. + wp_render_custom_css_support_styles( $parsed_block ); + wp_render_custom_css_support_styles( $parsed_block ); + wp_render_custom_css_support_styles( $parsed_block ); + + // Get the inline styles registered for wp-block-custom-css. + global $wp_styles; + $inline_css = $wp_styles->get_data( 'wp-block-custom-css', 'after' ); + + // CSS should appear only once, not 3 times. + $this->assertIsArray( $inline_css, 'Inline styles should be registered.' ); + $combined = implode( '', $inline_css ); + $count = substr_count( $combined, 'color: fuchsia' ); + $this->assertSame( 1, $count, 'CSS should only be enqueued once even when block renders multiple times.' ); + } } From 54dbace5638519ce9eca03094822ad59ff6bb03a Mon Sep 17 00:00:00 2001 From: anupkankale Date: Mon, 8 Jun 2026 05:56:29 +0000 Subject: [PATCH 2/2] Block Supports: Improve Additional CSS deduplication logic Replaced the static variable cache with a check against the global $wp_styles object. This ensures that the deduplication logic stays in sync with the styles system and correctly handles test environment resets where $wp_styles is re-initialized between tests. Props mustafabharmal, westonruter. See #65268. --- src/wp-includes/block-supports/custom-css.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/block-supports/custom-css.php b/src/wp-includes/block-supports/custom-css.php index 0721dd6b288ad..b825695c41de3 100644 --- a/src/wp-includes/block-supports/custom-css.php +++ b/src/wp-includes/block-supports/custom-css.php @@ -49,16 +49,19 @@ function wp_render_custom_css_support_styles( $parsed_block ) { * The style depends on global-styles to ensure custom CSS loads after * and can override global styles. */ - static $enqueued_class_names = array(); + wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); // Skip if already enqueued — prevents duplication inside Query Loop. - if ( isset( $enqueued_class_names[ $class_name ] ) ) { - return $parsed_block; + global $wp_styles; + $existing_inline_styles = $wp_styles->get_data( 'wp-block-custom-css', 'after' ); + if ( is_array( $existing_inline_styles ) ) { + foreach ( $existing_inline_styles as $style ) { + if ( str_contains( $style, $selector ) ) { + return $parsed_block; + } + } } - $enqueued_class_names[ $class_name ] = true; - - wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); wp_add_inline_style( 'wp-block-custom-css', $processed_css ); }