Make WordPress Core

Changeset 57546


Ignore:
Timestamp:
02/07/2024 04:35:34 AM (3 months ago)
Author:
isabel_brison
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/global-styles-and-settings.php

    r57309 r57546  
    299299 *
    300300 * @since 6.1.0
     301 *
     302 * @global WP_Styles $wp_styles
    301303 */
    302304function 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();
     
    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
     
    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        }
Note: See TracChangeset for help on using the changeset viewer.