2525 * },
2626 * ...
2727 * } $parsed_block
28+ * @phpstan-return array{
29+ * blockName: string|null,
30+ * attrs: array{
31+ * className?: string,
32+ * style?: array{
33+ * css?: string,
34+ * ...
35+ * },
36+ * ...
37+ * },
38+ * ...
39+ * }
2840 */
2941function wp_render_custom_css_support_styles ( $ parsed_block ) {
3042 $ custom_css = $ parsed_block ['attrs ' ]['style ' ]['css ' ] ?? null ;
@@ -49,20 +61,30 @@ function wp_render_custom_css_support_styles( $parsed_block ) {
4961 ? "$ existing_class_name $ class_name "
5062 : $ class_name ;
5163
52- _wp_array_set ( $ parsed_block, array ( 'attrs ' , 'className ' ), $ updated_class_name ) ;
64+ $ parsed_block[ 'attrs ' ][ 'className ' ] = $ updated_class_name ;
5365
5466 // Process the custom CSS using the same method as global styles.
5567 $ selector = '. ' . $ class_name ;
5668 $ processed_css = WP_Theme_JSON::process_blocks_custom_css ( $ custom_css , $ selector );
5769
5870 if ( ! empty ( $ processed_css ) ) {
59- /*
60- * Register and add inline style for block custom CSS.
61- * The style depends on global-styles to ensure custom CSS loads after
62- * and can override global styles.
71+ /**
72+ * Skip CSS that has already been added. Blocks with identical attributes
73+ * share the same class name and processed CSS via {@see wp_unique_id_from_values()},
74+ * so the same style would otherwise be enqueued more than once (e.g. inside
75+ * a Query Loop or when blocks share identical custom CSS).
6376 */
64- wp_register_style ( 'wp-block-custom-css ' , false , array ( 'global-styles ' ) );
65- wp_add_inline_style ( 'wp-block-custom-css ' , $ processed_css );
77+ $ handle = 'wp-block-custom-css ' ;
78+ if ( ! wp_style_is ( $ handle , 'registered ' ) ) {
79+ wp_register_style ( $ handle , false , array ( 'global-styles ' ) );
80+ }
81+ $ after_styles = wp_styles ()->get_data ( $ handle , 'after ' );
82+ if ( ! is_array ( $ after_styles ) ) {
83+ $ after_styles = array ();
84+ }
85+ if ( ! in_array ( $ processed_css , $ after_styles , true ) ) {
86+ wp_add_inline_style ( $ handle , $ processed_css );
87+ }
6688 }
6789
6890 return $ parsed_block ;
0 commit comments