Skip to content

Commit 357f746

Browse files
committed
Fix insertion of global styles and third-party block styles
1 parent 51c47cf commit 357f746

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/wp-includes/script-loader.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3865,7 +3865,14 @@ public function remove() {
38653865
$handle = $matches[1];
38663866
}
38673867

3868+
if ( 'classic-theme-styles' === $handle ) {
3869+
$processor->set_bookmark( 'classic_theme_styles' );
3870+
}
3871+
38683872
if ( $handle && in_array( $handle, $style_handles_at_enqueued_block_assets, true ) ) {
3873+
if ( ! $processor->has_bookmark( 'first_style_at_enqueued_block_assets' ) ) {
3874+
$processor->set_bookmark( 'first_style_at_enqueued_block_assets' );
3875+
}
38693876
$processor->set_bookmark( 'last_style_at_enqueued_block_assets' );
38703877
}
38713878
}
@@ -3897,40 +3904,40 @@ public function remove() {
38973904
$processor->set_modifiable_text( $css_text );
38983905
}
38993906

3900-
$inserted_after = $printed_core_block_styles;
3907+
$inserted_after = $printed_core_block_styles;
3908+
$printed_core_block_styles = '';
3909+
39013910
if ( ! $processor->has_bookmark( 'last_style_at_enqueued_block_assets' ) ) {
39023911
// TODO: There is no coverage for this.
39033912
$inserted_after .= $printed_other_block_styles;
39043913
$inserted_after .= $printed_global_styles;
39053914

3906-
// Prevent printing them again at </head>.
39073915
$printed_other_block_styles = '';
39083916
$printed_global_styles = '';
39093917
}
39103918

39113919
if ( '' !== $inserted_after ) {
3912-
$processor->insert_after( $inserted_after );
3913-
3914-
// Prevent printing them again at </head>.
3915-
$printed_core_block_styles = '';
3920+
$processor->insert_after( "\n" . $inserted_after );
39163921
}
39173922
}
39183923

3919-
$inserted_after = $printed_other_block_styles . $printed_global_styles;
3920-
if ( '' !== $inserted_after && $processor->has_bookmark( 'last_style_at_enqueued_block_assets' ) ) {
3924+
if ( '' !== $printed_global_styles && $processor->has_bookmark( 'last_style_at_enqueued_block_assets' ) ) {
39213925
$processor->seek( 'last_style_at_enqueued_block_assets' );
3922-
$processor->insert_after( "\n" . $inserted_after );
3926+
$processor->insert_after( "\n" . $printed_global_styles );
3927+
$printed_global_styles = '';
3928+
}
39233929

3924-
// Prevent printing them again at </head>.
3930+
if ( '' !== $printed_other_block_styles && $processor->has_bookmark( 'classic_theme_styles' ) ) {
3931+
$processor->seek( 'classic_theme_styles' );
3932+
$processor->insert_after( "\n" . $printed_other_block_styles );
39253933
$printed_other_block_styles = '';
3926-
$printed_global_styles = '';
39273934
}
39283935

39293936
// Print all remaining styles.
39303937
$remaining_styles = $printed_core_block_styles . $printed_other_block_styles . $printed_global_styles . $printed_late_styles;
39313938
if ( $remaining_styles && $processor->has_bookmark( 'head_end' ) ) {
39323939
$processor->seek( 'head_end' );
3933-
$processor->insert_before( $remaining_styles );
3940+
$processor->insert_before( $remaining_styles . "\n" );
39343941
}
39353942
return $processor->get_updated_html();
39363943
}

tests/phpunit/tests/template.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,12 +1493,12 @@ public function data_wp_hoist_late_printed_styles(): array {
14931493
// The wp_common_block_scripts_and_styles() function also fires enqueue_block_assets, at which wp_enqueue_classic_theme_styles() runs.
14941494
'classic-theme-styles-css', // Printed at enqueue_block_assets.
14951495

1496-
// Other styles enqueued at enqueue_block_assets, which is fired by wp_common_block_scripts_and_styles().
1497-
'custom-block-styles-css', // Printed at enqueue_block_assets.
1498-
14991496
// Third-party block styles.
15001497
'third-party-test-block-css', // Hoisted.
15011498

1499+
// Other styles enqueued at enqueue_block_assets, which is fired by wp_common_block_scripts_and_styles().
1500+
'custom-block-styles-css', // Printed at enqueue_block_assets.
1501+
15021502
// Hoisted. Enqueued by wp_enqueue_global_styles() which runs at wp_enqueue_scripts priority 10 and wp_footer priority 1.
15031503
'global-styles-inline-css',
15041504

@@ -1536,8 +1536,8 @@ public function data_wp_hoist_late_printed_styles(): array {
15361536
'wp-block-library-inline-css',
15371537
'wp-block-separator-inline-css',
15381538
'classic-theme-styles-inline-css',
1539-
'custom-block-styles-css',
15401539
'third-party-test-block-css',
1540+
'custom-block-styles-css',
15411541
'global-styles-inline-css',
15421542
'normal-css',
15431543
'normal-inline-css',
@@ -1583,8 +1583,8 @@ static function () {
15831583
'wp-emoji-styles-inline-css',
15841584
'wp-block-library-css',
15851585
'classic-theme-styles-css',
1586-
'custom-block-styles-css', // TODO: Test is failing here.
15871586
'third-party-test-block-css',
1587+
'custom-block-styles-css',
15881588
'global-styles-inline-css',
15891589
'normal-css',
15901590
'normal-inline-css',
@@ -1682,8 +1682,8 @@ function (): void {
16821682
'wp-block-library-inline-css', // This contains the "OVERRIDDEN" text.
16831683
'wp-block-separator-css',
16841684
'classic-theme-styles-css',
1685-
'custom-block-styles-css',
16861685
'third-party-test-block-css',
1686+
'custom-block-styles-css',
16871687
'global-styles-inline-css',
16881688
'normal-css',
16891689
'normal-inline-css',

0 commit comments

Comments
 (0)