@@ -172,8 +172,11 @@ function gutenberg_get_global_styles_custom_css() {
172172 * Adds global style rules to the inline style for each block.
173173 *
174174 * @return void
175+ *
176+ * @global WP_Styles $wp_styles
175177 */
176178function gutenberg_add_global_styles_for_blocks () {
179+ global $ wp_styles ;
177180 $ tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data ();
178181 $ block_nodes = $ tree ->get_styles_block_nodes ();
179182 foreach ( $ block_nodes as $ metadata ) {
@@ -185,38 +188,40 @@ function gutenberg_add_global_styles_for_blocks() {
185188 }
186189
187190 $ stylesheet_handle = 'global-styles ' ;
191+ /*
192+ * When `wp_should_load_separate_core_block_assets()` is true, block styles are
193+ * enqueued for each block on the page in class WP_Block's render function.
194+ * This means there will be a handle in the styles queue for each of those blocks.
195+ * Block-specific global styles should be attached to the global-styles handle, but
196+ * only for blocks on the page, thus we check if the block's handle is in the queue
197+ * before adding the inline style.
198+ * This conditional loading only applies to core blocks.
199+ */
188200 if ( isset ( $ metadata ['name ' ] ) ) {
189- /*
190- * These block styles are added on block_render.
191- * This hooks inline CSS to them so that they are loaded conditionally
192- * based on whether or not the block is used on the page.
193- */
194201 if ( str_starts_with ( $ metadata ['name ' ], 'core/ ' ) ) {
195- $ block_name = str_replace ( 'core/ ' , '' , $ metadata ['name ' ] );
196- $ stylesheet_handle = 'wp-block- ' . $ block_name ;
202+ $ block_name = str_replace ( 'core/ ' , '' , $ metadata ['name ' ] );
203+ $ block_handle = 'wp-block- ' . $ block_name ;
204+ if ( in_array ( $ block_handle , $ wp_styles ->queue , true ) ) {
205+ wp_add_inline_style ( $ stylesheet_handle , $ block_css );
206+ }
207+ } else {
208+ wp_add_inline_style ( $ stylesheet_handle , $ block_css );
197209 }
198- wp_add_inline_style ( $ stylesheet_handle , $ block_css );
199210 }
200211
201212 // The likes of block element styles from theme.json do not have $metadata['name'] set.
202213 if ( ! isset ( $ metadata ['name ' ] ) && ! empty ( $ metadata ['path ' ] ) ) {
203- $ result = array_values (
204- array_filter (
205- $ metadata ['path ' ],
206- static function ( $ item ) {
207- if ( strpos ( $ item , 'core/ ' ) !== false ) {
208- return true ;
209- }
210- return false ;
214+ $ block_name = wp_get_block_name_from_theme_json_path ( $ metadata ['path ' ] );
215+ if ( $ block_name ) {
216+ if ( str_starts_with ( $ block_name , 'core/ ' ) ) {
217+ $ block_name = str_replace ( 'core/ ' , '' , $ block_name );
218+ $ block_handle = 'wp-block- ' . $ block_name ;
219+ if ( in_array ( $ block_handle , $ wp_styles ->queue , true ) ) {
220+ wp_add_inline_style ( $ stylesheet_handle , $ block_css );
211221 }
212- )
213- );
214- if ( isset ( $ result [0 ] ) ) {
215- if ( str_starts_with ( $ result [0 ], 'core/ ' ) ) {
216- $ block_name = str_replace ( 'core/ ' , '' , $ result [0 ] );
217- $ stylesheet_handle = 'wp-block- ' . $ block_name ;
222+ } else {
223+ wp_add_inline_style ( $ stylesheet_handle , $ block_css );
218224 }
219- wp_add_inline_style ( $ stylesheet_handle , $ block_css );
220225 }
221226 }
222227 }
0 commit comments