Skip to content

Commit 27087b7

Browse files
Script Loader: always output core block global styles after base global styles.
Changes the output of core block global styles when `should_load_separate_core_block_assets` is true so they are appended to base global styles instead of block-library styles. Props isabel_brison, oandregal, azaozz, ajlende. Fixes #60280. git-svn-id: https://develop.svn.wordpress.org/trunk@57546 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2b08a77 commit 27087b7

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

src/wp-includes/global-styles-and-settings.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,12 @@ function wp_get_global_styles_custom_css() {
298298
* Adds global style rules to the inline style for each block.
299299
*
300300
* @since 6.1.0
301+
*
302+
* @global WP_Styles $wp_styles
301303
*/
302304
function wp_add_global_styles_for_blocks() {
305+
global $wp_styles;
306+
303307
$tree = WP_Theme_JSON_Resolver::get_merged_data();
304308
$block_nodes = $tree->get_styles_block_nodes();
305309
foreach ( $block_nodes as $metadata ) {
@@ -311,28 +315,41 @@ function wp_add_global_styles_for_blocks() {
311315
}
312316

313317
$stylesheet_handle = 'global-styles';
318+
319+
/*
320+
* When `wp_should_load_separate_core_block_assets()` is true, block styles are
321+
* enqueued for each block on the page in class WP_Block's render function.
322+
* This means there will be a handle in the styles queue for each of those blocks.
323+
* Block-specific global styles should be attached to the global-styles handle, but
324+
* only for blocks on the page, thus we check if the block's handle is in the queue
325+
* before adding the inline style.
326+
* This conditional loading only applies to core blocks.
327+
*/
314328
if ( isset( $metadata['name'] ) ) {
315-
/*
316-
* These block styles are added on block_render.
317-
* This hooks inline CSS to them so that they are loaded conditionally
318-
* based on whether or not the block is used on the page.
319-
*/
320329
if ( str_starts_with( $metadata['name'], 'core/' ) ) {
321-
$block_name = str_replace( 'core/', '', $metadata['name'] );
322-
$stylesheet_handle = 'wp-block-' . $block_name;
330+
$block_name = str_replace( 'core/', '', $metadata['name'] );
331+
$block_handle = 'wp-block-' . $block_name;
332+
if ( in_array( $block_handle, $wp_styles->queue ) ) {
333+
wp_add_inline_style( $stylesheet_handle, $block_css );
334+
}
335+
} else {
336+
wp_add_inline_style( $stylesheet_handle, $block_css );
323337
}
324-
wp_add_inline_style( $stylesheet_handle, $block_css );
325338
}
326339

327340
// The likes of block element styles from theme.json do not have $metadata['name'] set.
328341
if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {
329342
$block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] );
330343
if ( $block_name ) {
331344
if ( str_starts_with( $block_name, 'core/' ) ) {
332-
$block_name = str_replace( 'core/', '', $block_name );
333-
$stylesheet_handle = 'wp-block-' . $block_name;
345+
$block_name = str_replace( 'core/', '', $block_name );
346+
$block_handle = 'wp-block-' . $block_name;
347+
if ( in_array( $block_handle, $wp_styles->queue ) ) {
348+
wp_add_inline_style( $stylesheet_handle, $block_css );
349+
}
350+
} else {
351+
wp_add_inline_style( $stylesheet_handle, $block_css );
334352
}
335-
wp_add_inline_style( $stylesheet_handle, $block_css );
336353
}
337354
}
338355
}

0 commit comments

Comments
 (0)