Skip to content

Commit 51c47cf

Browse files
committed
Mostly account for styles enqueued at enqueued_block_assets
1 parent 3993e53 commit 51c47cf

File tree

2 files changed

+73
-20
lines changed

2 files changed

+73
-20
lines changed

src/wp-includes/script-loader.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,23 @@ function wp_hoist_late_printed_styles() {
36783678
return;
36793679
}
36803680

3681+
// Capture the styles enqueued at the enqueue_block_assets action, so that non-core block styles and global styles can be inserted after at hoisting.
3682+
$style_handles_at_enqueued_block_assets = array();
3683+
add_action(
3684+
'enqueue_block_assets',
3685+
static function () use ( &$style_handles_at_enqueued_block_assets ) {
3686+
$style_handles_at_enqueued_block_assets = wp_styles()->queue;
3687+
},
3688+
PHP_INT_MIN
3689+
);
3690+
add_action(
3691+
'enqueue_block_assets',
3692+
static function () use ( &$style_handles_at_enqueued_block_assets ) {
3693+
$style_handles_at_enqueued_block_assets = array_values( array_diff( wp_styles()->queue, $style_handles_at_enqueued_block_assets ) );
3694+
},
3695+
PHP_INT_MAX
3696+
);
3697+
36813698
/*
36823699
* Add a placeholder comment into the inline styles for wp-block-library, after which where the late block styles
36833700
* can be hoisted from the footer to be printed in the header by means of a filter below on the template enhancement
@@ -3782,7 +3799,7 @@ static function () use ( $capture_late_styles ) {
37823799
// Replace placeholder with the captured late styles.
37833800
add_filter(
37843801
'wp_template_enhancement_output_buffer',
3785-
static function ( $buffer ) use ( $placeholder, &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) {
3802+
static function ( $buffer ) use ( $placeholder, &$style_handles_at_enqueued_block_assets, &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) {
37863803

37873804
// Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans.
37883805
$processor = new class( $buffer ) extends WP_HTML_Tag_Processor {
@@ -3834,20 +3851,23 @@ public function remove() {
38343851
'wp-block-library-inline-css' === $processor->get_attribute( 'id' )
38353852
) {
38363853
$processor->set_bookmark( 'wp_block_library' );
3837-
} elseif (
3838-
(
3839-
'STYLE' === $processor->get_tag() &&
3840-
'classic-theme-styles-inline-css' === $processor->get_attribute( 'id' )
3841-
) ||
3842-
(
3843-
'LINK' === $processor->get_tag() &&
3844-
'classic-theme-styles-css' === $processor->get_attribute( 'id' )
3845-
)
3846-
) {
3847-
$processor->set_bookmark( 'classic_theme_styles' );
38483854
} elseif ( 'HEAD' === $processor->get_tag() && $processor->is_tag_closer() ) {
38493855
$processor->set_bookmark( 'head_end' );
38503856
break;
3857+
} elseif ( ( 'STYLE' === $processor->get_tag() || 'LINK' === $processor->get_tag() ) && $processor->get_attribute( 'id' ) ) {
3858+
$id = $processor->get_attribute( 'id' );
3859+
$handle = null;
3860+
if ( 'STYLE' === $processor->get_tag() ) {
3861+
if ( preg_match( '/^(.+)-inline-css$/', $id, $matches ) ) {
3862+
$handle = $matches[1];
3863+
}
3864+
} elseif ( preg_match( '/^(.+)-css$/', $id, $matches ) ) {
3865+
$handle = $matches[1];
3866+
}
3867+
3868+
if ( $handle && in_array( $handle, $style_handles_at_enqueued_block_assets, true ) ) {
3869+
$processor->set_bookmark( 'last_style_at_enqueued_block_assets' );
3870+
}
38513871
}
38523872
}
38533873

@@ -3878,7 +3898,8 @@ public function remove() {
38783898
}
38793899

38803900
$inserted_after = $printed_core_block_styles;
3881-
if ( ! $processor->has_bookmark( 'classic_theme_styles' ) ) {
3901+
if ( ! $processor->has_bookmark( 'last_style_at_enqueued_block_assets' ) ) {
3902+
// TODO: There is no coverage for this.
38823903
$inserted_after .= $printed_other_block_styles;
38833904
$inserted_after .= $printed_global_styles;
38843905

@@ -3896,9 +3917,9 @@ public function remove() {
38963917
}
38973918

38983919
$inserted_after = $printed_other_block_styles . $printed_global_styles;
3899-
if ( '' !== $inserted_after && $processor->has_bookmark( 'classic_theme_styles' ) ) {
3900-
$processor->seek( 'classic_theme_styles' );
3901-
$processor->insert_after( $inserted_after );
3920+
if ( '' !== $inserted_after && $processor->has_bookmark( 'last_style_at_enqueued_block_assets' ) ) {
3921+
$processor->seek( 'last_style_at_enqueued_block_assets' );
3922+
$processor->insert_after( "\n" . $inserted_after );
39023923

39033924
// Prevent printing them again at </head>.
39043925
$printed_other_block_styles = '';

tests/phpunit/tests/template.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,14 +1485,31 @@ public function data_wp_hoist_late_printed_styles(): array {
14851485
'early-css',
14861486
'early-inline-css',
14871487
'wp-emoji-styles-inline-css',
1488-
'wp-block-library-css',
1489-
'wp-block-separator-css',
1490-
'classic-theme-styles-css',
1491-
'third-party-test-block-css',
1488+
1489+
// Core block styles enqueued by wp_common_block_scripts_and_styles() at which runs at wp_enqueue_scripts priority 10, added first.
1490+
'wp-block-library-css', // Inline printed.
1491+
'wp-block-separator-css', // Hoisted.
1492+
1493+
// The wp_common_block_scripts_and_styles() function also fires enqueue_block_assets, at which wp_enqueue_classic_theme_styles() runs.
1494+
'classic-theme-styles-css', // Printed at enqueue_block_assets.
1495+
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+
1499+
// Third-party block styles.
1500+
'third-party-test-block-css', // Hoisted.
1501+
1502+
// Hoisted. Enqueued by wp_enqueue_global_styles() which runs at wp_enqueue_scripts priority 10 and wp_footer priority 1.
14921503
'global-styles-inline-css',
1504+
1505+
// Styles enqueued at wp_enqueue_scripts (priority 10).
14931506
'normal-css',
14941507
'normal-inline-css',
1508+
1509+
// Styles printed at wp_head priority 10.
14951510
'wp-custom-css',
1511+
1512+
// Hoisted. Styles printed in the footer.
14961513
'late-css',
14971514
'late-inline-css',
14981515
'core-block-supports-inline-css',
@@ -1519,6 +1536,7 @@ public function data_wp_hoist_late_printed_styles(): array {
15191536
'wp-block-library-inline-css',
15201537
'wp-block-separator-inline-css',
15211538
'classic-theme-styles-inline-css',
1539+
'custom-block-styles-css',
15221540
'third-party-test-block-css',
15231541
'global-styles-inline-css',
15241542
'normal-css',
@@ -1565,6 +1583,7 @@ static function () {
15651583
'wp-emoji-styles-inline-css',
15661584
'wp-block-library-css',
15671585
'classic-theme-styles-css',
1586+
'custom-block-styles-css', // TODO: Test is failing here.
15681587
'third-party-test-block-css',
15691588
'global-styles-inline-css',
15701589
'normal-css',
@@ -1629,6 +1648,7 @@ function (): void {
16291648
'wp-emoji-styles-inline-css',
16301649
'classic-theme-styles-css',
16311650
'third-party-test-block-css',
1651+
'custom-block-styles-css',
16321652
'global-styles-inline-css',
16331653
'normal-css',
16341654
'normal-inline-css',
@@ -1662,6 +1682,7 @@ function (): void {
16621682
'wp-block-library-inline-css', // This contains the "OVERRIDDEN" text.
16631683
'wp-block-separator-css',
16641684
'classic-theme-styles-css',
1685+
'custom-block-styles-css',
16651686
'third-party-test-block-css',
16661687
'global-styles-inline-css',
16671688
'normal-css',
@@ -1714,6 +1735,17 @@ static function () {
17141735
)
17151736
);
17161737

1738+
/*
1739+
* This is very old guidance about how to add enqueue styles for blocks. Certain themes still enqueue block
1740+
* styles using this action.
1741+
*/
1742+
add_action(
1743+
'enqueue_block_assets',
1744+
static function () {
1745+
wp_enqueue_style( 'custom-block-styles', 'https://example.com/custom-block-styles.css', array(), null );
1746+
}
1747+
);
1748+
17171749
if ( $set_up ) {
17181750
$set_up();
17191751
}

0 commit comments

Comments
 (0)