From 4b813004f5b251fd3f5e1e1796153bb6673db814 Mon Sep 17 00:00:00 2001 From: Mustafa Bharmal Date: Tue, 19 May 2026 15:56:15 +0530 Subject: [PATCH 1/3] Block Supports: Prevent Additional CSS duplication inside Query Loop --- src/wp-includes/block-supports/custom-css.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/block-supports/custom-css.php b/src/wp-includes/block-supports/custom-css.php index d4331ae3706ae..7333e78e2ecb3 100644 --- a/src/wp-includes/block-supports/custom-css.php +++ b/src/wp-includes/block-supports/custom-css.php @@ -57,12 +57,22 @@ function wp_render_custom_css_support_styles( $parsed_block ) { if ( ! empty( $processed_css ) ) { /* - * Register and add inline style for block custom CSS. - * The style depends on global-styles to ensure custom CSS loads after - * and can override global styles. - */ - wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); - wp_add_inline_style( 'wp-block-custom-css', $processed_css ); + * Track which class names have already had their CSS enqueued to prevent + * duplicate styles when the same block is rendered multiple times inside + * a Query Loop (render_block_data fires once per loop iteration). + */ + static $enqueued_class_names = array(); + + if ( ! isset( $enqueued_class_names[ $class_name ] ) ) { + $enqueued_class_names[ $class_name ] = true; + /* + * Register and add inline style for block custom CSS. + * The style depends on global-styles to ensure custom CSS loads after + * and can override global styles. + */ + wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); + wp_add_inline_style( 'wp-block-custom-css', $processed_css ); + } } return $parsed_block; From e2b7da3ccae00bfd3b3738aece29812173d7d040 Mon Sep 17 00:00:00 2001 From: Mustafa Bharmal Date: Tue, 19 May 2026 17:18:37 +0530 Subject: [PATCH 2/3] Reformat block comment indentation in custom-css.php --- src/wp-includes/block-supports/custom-css.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/block-supports/custom-css.php b/src/wp-includes/block-supports/custom-css.php index 7333e78e2ecb3..69dc4aa256c85 100644 --- a/src/wp-includes/block-supports/custom-css.php +++ b/src/wp-includes/block-supports/custom-css.php @@ -57,19 +57,19 @@ function wp_render_custom_css_support_styles( $parsed_block ) { if ( ! empty( $processed_css ) ) { /* - * Track which class names have already had their CSS enqueued to prevent - * duplicate styles when the same block is rendered multiple times inside - * a Query Loop (render_block_data fires once per loop iteration). - */ + * Track which class names have already had their CSS enqueued to prevent + * duplicate styles when the same block is rendered multiple times inside + * a Query Loop (render_block_data fires once per loop iteration). + */ static $enqueued_class_names = array(); if ( ! isset( $enqueued_class_names[ $class_name ] ) ) { $enqueued_class_names[ $class_name ] = true; /* - * Register and add inline style for block custom CSS. - * The style depends on global-styles to ensure custom CSS loads after - * and can override global styles. - */ + * Register and add inline style for block custom CSS. + * The style depends on global-styles to ensure custom CSS loads after + * and can override global styles. + */ wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); wp_add_inline_style( 'wp-block-custom-css', $processed_css ); } From 7ac16588308d3c4ab12dd1fcad9b1601f66ebe28 Mon Sep 17 00:00:00 2001 From: Mustafa Bharmal Date: Wed, 3 Jun 2026 18:15:52 +0530 Subject: [PATCH 3/3] Refactor: custom CSS handling to prevent duplicate styles in Query Loop --- src/wp-includes/block-supports/custom-css.php | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/block-supports/custom-css.php b/src/wp-includes/block-supports/custom-css.php index 69dc4aa256c85..3df629afa72cf 100644 --- a/src/wp-includes/block-supports/custom-css.php +++ b/src/wp-includes/block-supports/custom-css.php @@ -61,17 +61,16 @@ function wp_render_custom_css_support_styles( $parsed_block ) { * duplicate styles when the same block is rendered multiple times inside * a Query Loop (render_block_data fires once per loop iteration). */ - static $enqueued_class_names = array(); - - if ( ! isset( $enqueued_class_names[ $class_name ] ) ) { - $enqueued_class_names[ $class_name ] = true; - /* - * Register and add inline style for block custom CSS. - * The style depends on global-styles to ensure custom CSS loads after - * and can override global styles. - */ - wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); - wp_add_inline_style( 'wp-block-custom-css', $processed_css ); + $handle = 'wp-block-custom-css'; + if ( ! wp_style_is( $handle, 'registered' ) ) { + wp_register_style( $handle, false, array( 'global-styles' ) ); + } + $after_styles = wp_styles()->get_data( $handle, 'after' ); + if ( ! is_array( $after_styles ) ) { + $after_styles = array(); + } + if ( ! in_array( $processed_css, $after_styles, true ) ) { + wp_add_inline_style( $handle, $processed_css ); } }