Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ded6e2e
Add failing test case to capture the Elementor scenario
westonruter Feb 6, 2026
485549b
Add global-styles placeholder which is replaced during hoisting
westonruter Feb 6, 2026
ec819ad
Add test case for blockless theme without theme.json
westonruter Feb 6, 2026
2f8713c
Ensure global styles placeholder is removed even when there is no glo…
westonruter Feb 6, 2026
2282411
Use actual CSS instead of comment in case a minifier tries to strip o…
westonruter Feb 6, 2026
b7b1740
Remove obsolete conditions for inserting global-styles
westonruter Feb 6, 2026
4f99b7d
Refine comment copy
westonruter Feb 6, 2026
45ec7f4
Update return tag for data provider to reflect new array shape
westonruter Feb 6, 2026
ff8cbc1
Fix priority in comment regarding wp_custom_css_cb()
westonruter Feb 6, 2026
48eb4f6
Preserve CSS cascade so wp-block-library inline styles appear after i…
westonruter Feb 10, 2026
062fdad
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Feb 10, 2026
75eb70a
Merge branch 'trunk' into trac-64389
westonruter Mar 4, 2026
2cf0787
Leverage HTML Tag Processor in test
westonruter Mar 4, 2026
6b5633d
Use HTML Tag Processor to safely construct STYLE tag
westonruter Mar 4, 2026
80472f0
Add conditional return types and remove redundant param type for has_…
westonruter Mar 4, 2026
0c0f529
Fix PHPStan level 8 issues in wp_hoist_late_printed_styles()
westonruter Mar 4, 2026
c82418b
Address PHPStan issues with test_wp_hoist_late_printed_styles
westonruter Mar 4, 2026
e72fc45
Clarify comment
westonruter Mar 4, 2026
d54326f
Eliminate looking for classic-theme-styles in favor of adding placeho…
westonruter Mar 4, 2026
c09f349
Remove obsolete references to classic-theme-styles
westonruter Mar 4, 2026
59bdc8d
Fix grammatical typo in comment
westonruter Mar 4, 2026
ddcb748
Account for possibility of no sourceURL comment being present
westonruter Mar 4, 2026
87a5ccb
Improve grammar in comment
westonruter Mar 4, 2026
17443f4
Remove arg from being passed needlessly to assert closure
westonruter Mar 4, 2026
59ffd4f
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Mar 5, 2026
d1565c2
Revert "Add conditional return types and remove redundant param type …
westonruter Mar 5, 2026
70b2d17
Merge branch 'trunk' into trac-64389
westonruter Mar 10, 2026
9506133
Merge branch 'trunk' into trac-64389
westonruter Mar 10, 2026
dc609a3
Remove unused bookmarks and logic to set them
westonruter Mar 10, 2026
1884007
Fix docs for expected_styles param
westonruter Mar 10, 2026
674d062
Merge branch 'trunk' into trac-64389
westonruter Mar 11, 2026
7318ad9
Make it clear changes were introduced in 6.9
westonruter Mar 11, 2026
01c2393
Make it clear that style injection is replacing the placeholder
westonruter Mar 11, 2026
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
242 changes: 142 additions & 100 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2551,21 +2551,42 @@ function wp_enqueue_global_styles() {
$is_block_theme = wp_is_block_theme();
$is_classic_theme = ! $is_block_theme;

/*
* Global styles should be printed in the head for block themes, or for classic themes when loading assets on
* demand is disabled, which is the default.
* The footer should only be used for classic themes when loading assets on demand is enabled.
/**
* Global styles should be printed in the HEAD for block themes, or for classic themes when loading assets on
* demand is disabled (which is no longer the default since WordPress 6.9).
*
* See https://core.trac.wordpress.org/ticket/53494 and https://core.trac.wordpress.org/ticket/61965.
* @link https://core.trac.wordpress.org/ticket/53494
* @link https://core.trac.wordpress.org/ticket/61965
*/
if (
( $is_block_theme && doing_action( 'wp_footer' ) ) ||
( $is_classic_theme && doing_action( 'wp_footer' ) && ! $assets_on_demand ) ||
( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand )
doing_action( 'wp_footer' ) &&
(
$is_block_theme ||
( $is_classic_theme && ! $assets_on_demand )
)
) {
return;
}

/**
* The footer should only be used for classic themes when loading assets on demand is enabled. In WP 6.9 this is the
* default with the introduction of hoisting late-printed styles (via {@see wp_load_classic_theme_block_styles_on_demand()}).
Comment thread
westonruter marked this conversation as resolved.
* So even though the main global styles are not printed here in the HEAD for classic themes with on-demand asset
* loading, a placeholder for the global styles is still enqueued. Then when {@see wp_hoist_late_printed_styles()}
* processes the output buffer, it can locate the placeholder and inject the global styles from the footer into the
Comment thread
westonruter marked this conversation as resolved.
* HEAD, replacing the placeholder.
*
* @link https://core.trac.wordpress.org/ticket/64099
*/
if ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand ) {
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." }' );
Comment thread
westonruter marked this conversation as resolved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats cure, I've never seen style comments added like this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a CSS custom property (variable) leveraged as a comment 😄

wp_enqueue_style( 'wp-global-styles-placeholder' );
}
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 Expand Up @@ -2741,6 +2762,16 @@ function wp_should_load_block_assets_on_demand() {
*/
function wp_enqueue_registered_block_scripts_and_styles() {
if ( wp_should_load_block_assets_on_demand() ) {
/**
* Add placeholder for where block styles would historically get enqueued in a classic theme when block assets
* are not loaded on demand. This happens right after {@see wp_common_block_scripts_and_styles()} is called
* at which time wp-block-library is enqueued.
*/
if ( ! wp_is_block_theme() && has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) {
wp_register_style( 'wp-block-styles-placeholder', false );
wp_add_inline_style( 'wp-block-styles-placeholder', ':root { --wp-internal-comment: "Placeholder for wp_hoist_late_printed_styles() to replace with the block styles printed at wp_footer." }' );
Comment thread
westonruter marked this conversation as resolved.
wp_enqueue_style( 'wp-block-styles-placeholder' );
}
return;
}

Expand Down Expand Up @@ -3700,42 +3731,30 @@ function wp_load_classic_theme_block_styles_on_demand() {
* @see wp_load_classic_theme_block_styles_on_demand()
* @see _wp_footer_scripts()
*/
function wp_hoist_late_printed_styles() {
function wp_hoist_late_printed_styles(): void {
// Skip the embed template on-demand styles aren't relevant, and there is no wp_head action.
if ( is_embed() ) {
return;
}

// Capture the styles enqueued at the enqueue_block_assets action, so that non-core block styles and global styles can be inserted afterwards during hoisting.
$style_handles_at_enqueue_block_assets = array();
add_action(
'enqueue_block_assets',
static function () use ( &$style_handles_at_enqueue_block_assets ) {
$style_handles_at_enqueue_block_assets = wp_styles()->queue;
},
PHP_INT_MIN
);
add_action(
'enqueue_block_assets',
static function () use ( &$style_handles_at_enqueue_block_assets ) {
$style_handles_at_enqueue_block_assets = array_values( array_diff( wp_styles()->queue, $style_handles_at_enqueue_block_assets ) );
},
PHP_INT_MAX
);

Comment on lines -3709 to -3725
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is obsolete as it was just used to set the first_style_at_enqueue_block_assets and last_style_at_enqueue_block_assets bookmarks, which are also now no longer used.

/*
* Add a placeholder comment into the inline styles for wp-block-library, after which the late block styles
* can be hoisted from the footer to be printed in the header by means of a filter below on the template enhancement
* output buffer. The `wp_print_styles` action is used to ensure that if the inline style gets replaced at
* `enqueue_block_assets` or `wp_enqueue_scripts` that the placeholder will be sure to be present.
* output buffer.
*
* Note that wp_maybe_inline_styles() prepends the inlined style to the extra 'after' array, which happens after
* this code runs. This ensures that the placeholder appears right after any inlined wp-block-library styles,
* which would be common.css.
*/
$placeholder = sprintf( '/*%s*/', uniqid( 'wp_block_styles_on_demand_placeholder:' ) );
add_action(
'wp_print_styles',
static function () use ( $placeholder ) {
$dependency = wp_styles()->query( 'wp-block-library', 'registered' );
if ( $dependency ) {
if ( ! isset( $dependency->extra['after'] ) ) {
wp_add_inline_style( 'wp-block-library', $placeholder );
} else {
array_unshift( $dependency->extra['after'], $placeholder );
}
);
}

/*
* Create a substitute for `print_late_styles()` which is aware of block styles. This substitute does not print
Expand Down Expand Up @@ -3765,29 +3784,29 @@ static function () use ( $placeholder ) {
}

/*
* First print all styles related to blocks which should be inserted right after the wp-block-library stylesheet
* First print all styles related to core blocks which should be inserted right after the wp-block-library stylesheet
* to preserve the CSS cascade. The logic in this `if` statement is derived from `wp_print_styles()`.
*/
$enqueued_core_block_styles = array_values( array_intersect( $all_core_block_style_handles, wp_styles()->queue ) );
if ( count( $enqueued_core_block_styles ) > 0 ) {
ob_start();
wp_styles()->do_items( $enqueued_core_block_styles );
$printed_core_block_styles = ob_get_clean();
$printed_core_block_styles = (string) ob_get_clean();
}

// Non-core block styles get printed after the classic-theme-styles stylesheet.
// Capture non-core block styles so they can get printed at the point where wp_enqueue_registered_block_scripts_and_styles() runs.
$enqueued_other_block_styles = array_values( array_intersect( $all_other_block_style_handles, wp_styles()->queue ) );
if ( count( $enqueued_other_block_styles ) > 0 ) {
ob_start();
wp_styles()->do_items( $enqueued_other_block_styles );
$printed_other_block_styles = ob_get_clean();
$printed_other_block_styles = (string) ob_get_clean();
}

// Capture the global-styles so that it can be printed separately after classic-theme-styles and other styles enqueued at enqueue_block_assets.
// Capture the global-styles so that it can be printed at the point where wp_enqueue_global_styles() runs.
if ( wp_style_is( 'global-styles' ) ) {
ob_start();
wp_styles()->do_items( array( 'global-styles' ) );
$printed_global_styles = ob_get_clean();
$printed_global_styles = (string) ob_get_clean();
}

/*
Expand All @@ -3797,7 +3816,7 @@ static function () use ( $placeholder ) {
*/
ob_start();
wp_styles()->do_footer_items();
$printed_late_styles = ob_get_clean();
$printed_late_styles = (string) ob_get_clean();
};

/*
Expand Down Expand Up @@ -3828,7 +3847,7 @@ static function () use ( $capture_late_styles ) {
// Replace placeholder with the captured late styles.
add_filter(
'wp_template_enhancement_output_buffer',
static function ( $buffer ) use ( $placeholder, &$style_handles_at_enqueue_block_assets, &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) {
static function ( $buffer ) use ( $placeholder, &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) {

// Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans.
$processor = new class( $buffer ) extends WP_HTML_Tag_Processor {
Expand All @@ -3848,7 +3867,7 @@ private function get_span(): WP_HTML_Span {
*
* @param string $text Text to insert.
*/
public function insert_before( string $text ) {
public function insert_before( string $text ): void {
$this->lexical_updates[] = new WP_HTML_Text_Replacement( $this->get_span()->start, 0, $text );
}

Expand All @@ -3857,7 +3876,7 @@ public function insert_before( string $text ) {
*
* @param string $text Text to insert.
*/
public function insert_after( string $text ) {
public function insert_after( string $text ): void {
$span = $this->get_span();

$this->lexical_updates[] = new WP_HTML_Text_Replacement( $span->start + $span->length, 0, $text );
Expand All @@ -3866,47 +3885,60 @@ public function insert_after( string $text ) {
/**
* Removes the current token.
*/
public function remove() {
public function remove(): void {
$span = $this->get_span();

$this->lexical_updates[] = new WP_HTML_Text_Replacement( $span->start, $span->length, '' );
}

/**
* Replaces the current token.
*
* @param string $text Text to replace with.
*/
public function replace( string $text ): void {
$span = $this->get_span();

$this->lexical_updates[] = new WP_HTML_Text_Replacement( $span->start, $span->length, $text );
}
};

// Locate the insertion points in the HEAD.
while ( $processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
if (
'STYLE' === $processor->get_tag() &&
'wp-global-styles-placeholder-inline-css' === $processor->get_attribute( 'id' )
) {
/** This is added in {@see wp_enqueue_global_styles()} */
$processor->set_bookmark( 'wp_global_styles_placeholder' );
} elseif (
'STYLE' === $processor->get_tag() &&
'wp-block-styles-placeholder-inline-css' === $processor->get_attribute( 'id' )
) {
/** This is added in {@see wp_enqueue_registered_block_scripts_and_styles()} */
$processor->set_bookmark( 'wp_block_styles_placeholder' );
} elseif (
'STYLE' === $processor->get_tag() &&
'wp-block-library-inline-css' === $processor->get_attribute( 'id' )
) {
/** This is added here in {@see wp_hoist_late_printed_styles()} */
$processor->set_bookmark( 'wp_block_library' );
} elseif ( 'HEAD' === $processor->get_tag() && $processor->is_tag_closer() ) {
$processor->set_bookmark( 'head_end' );
break;
} elseif ( ( 'STYLE' === $processor->get_tag() || 'LINK' === $processor->get_tag() ) && $processor->get_attribute( 'id' ) ) {
$id = $processor->get_attribute( 'id' );
$handle = null;
if ( 'STYLE' === $processor->get_tag() ) {
if ( preg_match( '/^(.+)-inline-css$/', $id, $matches ) ) {
$handle = $matches[1];
}
} elseif ( preg_match( '/^(.+)-css$/', $id, $matches ) ) {
$handle = $matches[1];
}

if ( 'classic-theme-styles' === $handle ) {
$processor->set_bookmark( 'classic_theme_styles' );
}

if ( $handle && in_array( $handle, $style_handles_at_enqueue_block_assets, true ) ) {
if ( ! $processor->has_bookmark( 'first_style_at_enqueue_block_assets' ) ) {
$processor->set_bookmark( 'first_style_at_enqueue_block_assets' );
}
$processor->set_bookmark( 'last_style_at_enqueue_block_assets' );
}
Comment on lines -3886 to -3906
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is obsolete because the first_style_at_enqueue_block_assets and last_style_at_enqueue_block_assets bookmarks are no longer used.

}
}

/**
* Replace the placeholder for global styles enqueued during {@see wp_enqueue_global_styles()}. This is done
* even if $printed_global_styles is empty.
*/
if ( $processor->has_bookmark( 'wp_global_styles_placeholder' ) ) {
$processor->seek( 'wp_global_styles_placeholder' );
$processor->replace( $printed_global_styles );
$printed_global_styles = '';
}

/*
* Insert block styles right after wp-block-library (if it is present). The placeholder CSS comment will
* always be added to the wp-block-library inline style since it gets printed at `wp_head` before the blocks
Expand All @@ -3921,13 +3953,34 @@ public function remove() {
$css_text = $processor->get_modifiable_text();

/*
* A placeholder CSS comment is added to the inline style in order to force an inline STYLE tag to
* be printed. Now that we've located the inline style, the placeholder comment can be removed. If
* there is no CSS left in the STYLE tag after removing the placeholder (aside from the sourceURL
* comment), then remove the STYLE entirely.
* Split the block library inline style by the placeholder to identify the original inlined CSS, which
* likely would be common.css, followed by any inline styles which had been added by the theme or
* plugins via `wp_add_inline_style( 'wp-block-library', '...' )`. The separate block styles loaded on
* demand will get inserted after the inlined common.css and before the extra inline styles added by the
* user.
*/
$css_text = str_replace( $placeholder, '', $css_text );
if ( preg_match( ':^/\*# sourceURL=\S+? \*/$:', trim( $css_text ) ) ) {
$css_text_around_placeholder = explode( $placeholder, $css_text, 2 );
$extra_inline_styles = '';
if ( count( $css_text_around_placeholder ) === 2 ) {
$css_text = $css_text_around_placeholder[0];
if ( '' !== trim( $css_text ) ) {
$inlined_src = wp_styles()->get_data( 'wp-block-library', 'inlined_src' );
if ( $inlined_src ) {
$css_text .= sprintf(
"\n/*# sourceURL=%s */\n",
esc_url_raw( $inlined_src )
);
}
}
$extra_inline_styles = $css_text_around_placeholder[1];
}

/*
* The placeholder CSS comment was added to the inline style in order to force an inline STYLE tag to
* be printed. Now that the inline style has been located and the placeholder comment has been removed, if
* there is no CSS left in the STYLE tag after removal, then remove the STYLE tag entirely.
*/
if ( '' === trim( $css_text ) ) {
$processor->remove();
} else {
$processor->set_modifiable_text( $css_text );
Expand All @@ -3936,44 +3989,33 @@ public function remove() {
$inserted_after = $printed_core_block_styles;
$printed_core_block_styles = '';

// If the classic-theme-styles is absent, then the third-party block styles cannot be inserted after it, so they get inserted here.
if ( ! $processor->has_bookmark( 'classic_theme_styles' ) ) {
if ( '' !== $printed_other_block_styles ) {
$inserted_after .= $printed_other_block_styles;
}
$printed_other_block_styles = '';

// If there aren't any other styles printed at enqueue_block_assets either, then the global styles need to also be printed here.
if ( ! $processor->has_bookmark( 'last_style_at_enqueue_block_assets' ) ) {
if ( '' !== $printed_global_styles ) {
$inserted_after .= $printed_global_styles;
}
$printed_global_styles = '';
}
/*
* Add a new inline style for any user styles added via wp_add_inline_style( 'wp-block-library', '...' ).
* This must be added here after $printed_core_block_styles to preserve the original CSS cascade when
* the combined block library stylesheet was used. The pattern here is checking to see if it is not just
* a sourceURL comment after the placeholder above is removed.
*/
if ( ! preg_match( ':^\s*(/\*# sourceURL=\S+? \*/\s*)?$:s', $extra_inline_styles ) ) {
$style_processor = new WP_HTML_Tag_Processor( '<style></style>' );
$style_processor->next_tag();
$style_processor->set_attribute( 'id', 'wp-block-library-inline-css-extra' );
$style_processor->set_modifiable_text( $extra_inline_styles );
$inserted_after .= "{$style_processor->get_updated_html()}\n";
}

if ( '' !== $inserted_after ) {
$processor->insert_after( "\n" . $inserted_after );
}
}

// Insert global-styles after the styles enqueued at enqueue_block_assets.
if ( '' !== $printed_global_styles && $processor->has_bookmark( 'last_style_at_enqueue_block_assets' ) ) {
$processor->seek( 'last_style_at_enqueue_block_assets' );

$processor->insert_after( "\n" . $printed_global_styles );
$printed_global_styles = '';

if ( ! $processor->has_bookmark( 'classic_theme_styles' ) && '' !== $printed_other_block_styles ) {
$processor->insert_after( "\n" . $printed_other_block_styles );
$printed_other_block_styles = '';
// Insert block styles at the point where wp_enqueue_registered_block_scripts_and_styles() normally enqueues styles.
if ( $processor->has_bookmark( 'wp_block_styles_placeholder' ) ) {
$processor->seek( 'wp_block_styles_placeholder' );
if ( '' !== $printed_other_block_styles ) {
$processor->replace( "\n" . $printed_other_block_styles );
} else {
$processor->remove();
}
}

// Insert third-party block styles right after the classic-theme-styles.
if ( '' !== $printed_other_block_styles && $processor->has_bookmark( 'classic_theme_styles' ) ) {
$processor->seek( 'classic_theme_styles' );
$processor->insert_after( "\n" . $printed_other_block_styles );
$printed_other_block_styles = '';
}

Expand Down
Loading
Loading