Skip to content

Commit 2fb1c42

Browse files
committed
Ensure styles for third-party blocks get printed after classic-theme-styles
1 parent 523be88 commit 2fb1c42

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

src/wp-includes/script-loader.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3698,27 +3698,43 @@ static function () use ( $placeholder ) {
36983698
* later hoisted to the HEAD in the template enhancement output buffer. This will run at `wp_print_footer_scripts`
36993699
* before `print_footer_scripts()` is called.
37003700
*/
3701-
$printed_block_styles = '';
3702-
$printed_global_styles = '';
3703-
$printed_late_styles = '';
3704-
$capture_late_styles = static function () use ( &$printed_block_styles, &$printed_global_styles, &$printed_late_styles ) {
3701+
$printed_core_block_styles = '';
3702+
$printed_other_block_styles = '';
3703+
$printed_global_styles = '';
3704+
$printed_late_styles = '';
3705+
3706+
$capture_late_styles = static function () use ( &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) {
37053707
// Gather the styles related to on-demand block enqueues.
3706-
$all_block_style_handles = array();
3708+
$all_core_block_style_handles = array();
3709+
$all_other_block_style_handles = array();
37073710
foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $block_type ) {
3708-
foreach ( $block_type->style_handles as $style_handle ) {
3709-
$all_block_style_handles[] = $style_handle;
3711+
if ( str_starts_with( $block_type->name, 'core/' ) ) {
3712+
foreach ( $block_type->style_handles as $style_handle ) {
3713+
$all_core_block_style_handles[] = $style_handle;
3714+
}
3715+
} else {
3716+
foreach ( $block_type->style_handles as $style_handle ) {
3717+
$all_other_block_style_handles[] = $style_handle;
3718+
}
37103719
}
37113720
}
37123721

37133722
/*
37143723
* First print all styles related to blocks which should inserted right after the wp-block-library stylesheet
37153724
* to preserve the CSS cascade. The logic in this `if` statement is derived from `wp_print_styles()`.
37163725
*/
3717-
$enqueued_block_styles = array_values( array_intersect( $all_block_style_handles, wp_styles()->queue ) );
3718-
if ( count( $enqueued_block_styles ) > 0 ) {
3726+
$enqueued_core_block_styles = array_values( array_intersect( $all_core_block_style_handles, wp_styles()->queue ) );
3727+
if ( count( $enqueued_core_block_styles ) > 0 ) {
3728+
ob_start();
3729+
wp_styles()->do_items( $enqueued_core_block_styles );
3730+
$printed_core_block_styles = ob_get_clean();
3731+
}
3732+
3733+
$enqueued_other_block_styles = array_values( array_intersect( $all_other_block_style_handles, wp_styles()->queue ) );
3734+
if ( count( $enqueued_other_block_styles ) > 0 ) {
37193735
ob_start();
3720-
wp_styles()->do_items( $enqueued_block_styles );
3721-
$printed_block_styles = ob_get_clean();
3736+
wp_styles()->do_items( $enqueued_other_block_styles );
3737+
$printed_other_block_styles = ob_get_clean();
37223738
}
37233739

37243740
// Capture the global-styles so that it can be printed separately after classic-theme-styles, to preserve the original order,
@@ -3766,7 +3782,7 @@ static function () use ( $capture_late_styles ) {
37663782
// Replace placeholder with the captured late styles.
37673783
add_filter(
37683784
'wp_template_enhancement_output_buffer',
3769-
static function ( $buffer ) use ( $placeholder, &$printed_block_styles, &$printed_global_styles, &$printed_late_styles ) {
3785+
static function ( $buffer ) use ( $placeholder, &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) {
37703786

37713787
// Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans.
37723788
$processor = new class( $buffer ) extends WP_HTML_Tag_Processor {
@@ -3861,32 +3877,36 @@ public function remove() {
38613877
$processor->set_modifiable_text( $css_text );
38623878
}
38633879

3864-
$inserted_after = $printed_block_styles;
3880+
$inserted_after = $printed_core_block_styles;
38653881
if ( ! $processor->has_bookmark( 'classic_theme_styles' ) ) {
3882+
$inserted_after .= $printed_other_block_styles;
38663883
$inserted_after .= $printed_global_styles;
38673884

38683885
// Prevent printing them again at </head>.
3869-
$printed_global_styles = '';
3886+
$printed_other_block_styles = '';
3887+
$printed_global_styles = '';
38703888
}
38713889

38723890
if ( '' !== $inserted_after ) {
38733891
$processor->insert_after( $inserted_after );
38743892

38753893
// Prevent printing them again at </head>.
3876-
$printed_block_styles = '';
3894+
$printed_core_block_styles = '';
38773895
}
38783896
}
38793897

3880-
if ( $printed_global_styles && $processor->has_bookmark( 'classic_theme_styles' ) ) {
3898+
$inserted_after = $printed_other_block_styles . $printed_global_styles;
3899+
if ( '' !== $inserted_after && $processor->has_bookmark( 'classic_theme_styles' ) ) {
38813900
$processor->seek( 'classic_theme_styles' );
3882-
$processor->insert_after( $printed_global_styles );
3901+
$processor->insert_after( $inserted_after );
38833902

38843903
// Prevent printing them again at </head>.
3885-
$printed_global_styles = '';
3904+
$printed_other_block_styles = '';
3905+
$printed_global_styles = '';
38863906
}
38873907

38883908
// Print all remaining styles.
3889-
$remaining_styles = $printed_block_styles . $printed_global_styles . $printed_late_styles;
3909+
$remaining_styles = $printed_core_block_styles . $printed_other_block_styles . $printed_global_styles . $printed_late_styles;
38903910
if ( $remaining_styles && $processor->has_bookmark( 'head_end' ) ) {
38913911
$processor->seek( 'head_end' );
38923912
$processor->insert_before( $remaining_styles );

tests/phpunit/tests/template.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ public function tear_down() {
146146
unregister_taxonomy( 'taxo' );
147147
$this->set_permalink_structure( '' );
148148

149+
$registry = WP_Block_Type_Registry::get_instance();
150+
if ( $registry->is_registered( 'third-party/test' ) ) {
151+
$registry->unregister( 'third-party/test' );
152+
}
153+
149154
parent::tear_down();
150155
}
151156

@@ -1483,6 +1488,7 @@ public function data_wp_hoist_late_printed_styles(): array {
14831488
'wp-block-library-css',
14841489
'wp-block-separator-css',
14851490
'classic-theme-styles-css',
1491+
'third-party-test-block-css',
14861492
'global-styles-inline-css',
14871493
'normal-css',
14881494
'normal-inline-css',
@@ -1513,6 +1519,7 @@ public function data_wp_hoist_late_printed_styles(): array {
15131519
'wp-block-library-inline-css',
15141520
'wp-block-separator-inline-css',
15151521
'classic-theme-styles-inline-css',
1522+
'third-party-test-block-css',
15161523
'global-styles-inline-css',
15171524
'normal-css',
15181525
'normal-inline-css',
@@ -1558,6 +1565,7 @@ static function () {
15581565
'wp-emoji-styles-inline-css',
15591566
'wp-block-library-css',
15601567
'classic-theme-styles-css',
1568+
'third-party-test-block-css',
15611569
'global-styles-inline-css',
15621570
'normal-css',
15631571
'normal-inline-css',
@@ -1620,6 +1628,7 @@ function (): void {
16201628
'early-inline-css',
16211629
'wp-emoji-styles-inline-css',
16221630
'classic-theme-styles-css',
1631+
'third-party-test-block-css',
16231632
'global-styles-inline-css',
16241633
'normal-css',
16251634
'normal-inline-css',
@@ -1653,6 +1662,7 @@ function (): void {
16531662
'wp-block-library-inline-css', // This contains the "OVERRIDDEN" text.
16541663
'wp-block-separator-css',
16551664
'classic-theme-styles-css',
1665+
'third-party-test-block-css',
16561666
'global-styles-inline-css',
16571667
'normal-css',
16581668
'normal-inline-css',
@@ -1696,6 +1706,14 @@ static function () {
16961706
}
16971707
);
16981708

1709+
wp_register_style( 'third-party-test-block', 'https://example.com/third-party-test-block.css', array(), null );
1710+
register_block_type(
1711+
'third-party/test',
1712+
array(
1713+
'style_handles' => array( 'third-party-test-block' ),
1714+
)
1715+
);
1716+
16991717
if ( $set_up ) {
17001718
$set_up();
17011719
}
@@ -1746,7 +1764,8 @@ static function () {
17461764
// Simulate the_content().
17471765
$content = apply_filters(
17481766
'the_content',
1749-
'<!-- wp:separator --><hr class="wp-block-separator has-alpha-channel-opacity"/><!-- /wp:separator -->'
1767+
'<!-- wp:separator --><hr class="wp-block-separator has-alpha-channel-opacity"/><!-- /wp:separator -->' .
1768+
'<!-- wp:third-party/test --><div>This is only a test!</div><!-- /wp:third-party/test -->'
17501769
);
17511770

17521771
// Simulate footer scripts.

0 commit comments

Comments
 (0)