Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,9 @@ static function ( $node ) {
* @since 5.8.0
*/
function wp_enqueue_global_styles() {
static $should_enqueue = true;
static $deferred_check_registered = false;

$assets_on_demand = wp_should_load_block_assets_on_demand();
$is_block_theme = wp_is_block_theme();
$is_classic_theme = ! $is_block_theme;
Expand Down Expand Up @@ -2576,8 +2579,51 @@ function wp_enqueue_global_styles() {
* HEAD, replacing the placeholder.
*
* @link https://core.trac.wordpress.org/ticket/64099
* @link https://core.trac.wordpress.org/ticket/65336
*/
if ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand ) {
$should_enqueue = true;

/*
* Queue the handle early so themes and plugins can dequeue it during wp_enqueue_scripts, even though the
* stylesheet itself is not generated until wp_footer.
*/
wp_enqueue_style( 'global-styles' );

if ( ! $deferred_check_registered ) {
$deferred_check_registered = true;
add_action(
'wp_enqueue_scripts',
static function () use ( &$should_enqueue ) {
if ( false === has_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ) ) {
$should_enqueue = false;
return;
}

if ( wp_style_is( 'global-styles', 'registered' ) ) {
$should_enqueue = wp_style_is( 'global-styles', 'enqueued' );
return;
}

/*
* The handle was queued before registration. Attempt registration so its enqueue state reflects
* whether it was dequeued during wp_enqueue_scripts.
*/
wp_register_style( 'global-styles', false );

if ( wp_style_is( 'global-styles', 'enqueued' ) ) {
wp_dequeue_style( 'global-styles' );
wp_deregister_style( 'global-styles' );
$should_enqueue = true;
} else {
wp_deregister_style( 'global-styles' );
$should_enqueue = false;
}
},
PHP_INT_MAX
);
}

if ( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) {
wp_register_style( 'wp-global-styles-placeholder', false );
wp_add_inline_style( 'wp-global-styles-placeholder', ':root { --wp-internal-comment: "Placeholder for wp_hoist_late_printed_styles() to replace with the global-styles printed at wp_footer." }' );
Expand All @@ -2586,6 +2632,16 @@ function wp_enqueue_global_styles() {
return;
}

/*
* When loading block assets on demand in classic themes, global styles are generated in the footer. Respect
* opt-outs that removed the wp_enqueue_scripts callback or dequeued the handle during wp_enqueue_scripts.
*/
if ( doing_action( 'wp_footer' ) && $is_classic_theme && $assets_on_demand ) {
if ( false === has_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ) || ! $should_enqueue ) {
return;
}
}

/*
* If loading the CSS for each block separately, then load the theme.json CSS conditionally.
* This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block.
Expand Down
32 changes: 31 additions & 1 deletion tests/phpunit/tests/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ static function () {
$dequeue = static function () {
wp_dequeue_style( 'global-styles' );
};
add_action( 'wp_enqueue_scripts', $dequeue, 1000 );
add_action( 'wp_enqueue_scripts', $dequeue, 100 );
add_action( 'wp_footer', $dequeue, 2 );
},
'content' => $blocks_content,
Expand All @@ -1660,6 +1660,30 @@ static function () {
),
),

'no_global_styles_via_remove_action' => array(
'set_up' => static function () {
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
},
'content' => $blocks_content,
'inline_size_limit' => PHP_INT_MAX,
'expected_styles' => array(
'HEAD' => array_merge(
$early_common_styles,
array(
'wp-block-library-inline-css',
'wp-block-separator-inline-css',
'classic-theme-styles-inline-css',
'third-party-test-block-css',
'custom-block-styles-css',
),
$common_at_wp_enqueue_scripts,
$common_late_in_head,
$common_late_in_body
),
'BODY' => array(),
),
),

'standard_classic_theme_config_extra_block_library_inline_style_none_inlined' => array(
'set_up' => static function () {
add_action(
Expand Down Expand Up @@ -1952,8 +1976,10 @@ static function ( $path, $file ) {
*
* @ticket 64099
* @ticket 64354
* @ticket 65336
* @covers ::wp_load_classic_theme_block_styles_on_demand
* @covers ::wp_hoist_late_printed_styles
* @covers ::wp_enqueue_global_styles
*
* @dataProvider data_wp_hoist_late_printed_styles
*
Expand Down Expand Up @@ -2122,6 +2148,10 @@ static function () {
if ( $assert ) {
$assert( $buffer, $filtered_buffer );
}

if ( false === has_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ) ) {
add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
}
}

/**
Expand Down
Loading