@@ -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 );
0 commit comments