@@ -3698,25 +3698,17 @@ 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_late_styles = '' ;
3703- $ capture_late_styles = static function () use ( &$ printed_block_styles , &$ printed_late_styles ) {
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 ) {
37043705 // Gather the styles related to on-demand block enqueues.
37053706 $ all_block_style_handles = array ();
37063707 foreach ( WP_Block_Type_Registry::get_instance ()->get_all_registered () as $ block_type ) {
37073708 foreach ( $ block_type ->style_handles as $ style_handle ) {
37083709 $ all_block_style_handles [] = $ style_handle ;
37093710 }
37103711 }
3711- $ all_block_style_handles = array_merge (
3712- $ all_block_style_handles ,
3713- array (
3714- 'global-styles ' ,
3715- 'block-style-variation-styles ' ,
3716- 'core-block-supports ' ,
3717- 'core-block-supports-duotone ' ,
3718- )
3719- );
37203712
37213713 /*
37223714 * First print all styles related to blocks which should inserted right after the wp-block-library stylesheet
@@ -3729,6 +3721,13 @@ static function () use ( $placeholder ) {
37293721 $ printed_block_styles = ob_get_clean ();
37303722 }
37313723
3724+ // Capture the global-styles so that it can be printed separately after classic-theme-styles, to preserve the original order,
3725+ if ( wp_style_is ( 'global-styles ' ) ) {
3726+ ob_start ();
3727+ wp_styles ()->do_items ( array ( 'global-styles ' ) );
3728+ $ printed_global_styles = ob_get_clean ();
3729+ }
3730+
37323731 /*
37333732 * Print all remaining styles not related to blocks. This contains a subset of the logic from
37343733 * `print_late_styles()`, without admin-specific logic and the `print_late_styles` filter to control whether
@@ -3767,7 +3766,7 @@ static function () use ( $capture_late_styles ) {
37673766 // Replace placeholder with the captured late styles.
37683767 add_filter (
37693768 'wp_template_enhancement_output_buffer ' ,
3770- static function ( $ buffer ) use ( $ placeholder , &$ printed_block_styles , &$ printed_late_styles ) {
3769+ static function ( $ buffer ) use ( $ placeholder , &$ printed_block_styles , &$ printed_global_styles , & $ printed_late_styles ) {
37713770
37723771 // Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans.
37733772 $ processor = new class ( $ buffer ) extends WP_HTML_Tag_Processor {
@@ -3812,53 +3811,86 @@ public function remove() {
38123811 }
38133812 };
38143813
3815- /*
3816- * Insert block styles right after wp-block-library (if it is present), and then insert any remaining styles
3817- * at </head> (or else print everything there). The placeholder CSS comment will always be added to the
3818- * wp-block-library inline style since it gets printed at `wp_head` before the blocks are rendered.
3819- * This means that there may not actually be any block styles to hoist from the footer to insert after this
3820- * inline style. The placeholder CSS comment needs to be added so that the inline style gets printed, but
3821- * if the resulting inline style is empty after the placeholder is removed, then the inline style is
3822- * removed.
3823- */
3814+ // Locate the insertion points in the HEAD.
38243815 while ( $ processor ->next_tag ( array ( 'tag_closers ' => 'visit ' ) ) ) {
38253816 if (
38263817 'STYLE ' === $ processor ->get_tag () &&
38273818 'wp-block-library-inline-css ' === $ processor ->get_attribute ( 'id ' )
38283819 ) {
3829- $ css_text = $ processor ->get_modifiable_text ();
3830-
3831- /*
3832- * A placeholder CSS comment is added to the inline style in order to force an inline STYLE tag to
3833- * be printed. Now that we've located the inline style, the placeholder comment can be removed. If
3834- * there is no CSS left in the STYLE tag after removing the placeholder (aside from the sourceURL
3835- * comment, then remove the STYLE entirely.)
3836- */
3837- $ css_text = str_replace ( $ placeholder , '' , $ css_text );
3838- if ( preg_match ( ':^/\*# sourceURL=\S+? \*/$: ' , trim ( $ css_text ) ) ) {
3839- $ processor ->remove ();
3840- } else {
3841- $ processor ->set_modifiable_text ( $ css_text );
3842- }
3820+ $ processor ->set_bookmark ( 'wp_block_library ' );
3821+ } elseif (
3822+ (
3823+ 'STYLE ' === $ processor ->get_tag () &&
3824+ 'classic-theme-styles-inline-css ' === $ processor ->get_attribute ( 'id ' )
3825+ ) ||
3826+ (
3827+ 'LINK ' === $ processor ->get_tag () &&
3828+ 'classic-theme-styles-css ' === $ processor ->get_attribute ( 'id ' )
3829+ )
3830+ ) {
3831+ $ processor ->set_bookmark ( 'classic_theme_styles ' );
3832+ } elseif ( 'HEAD ' === $ processor ->get_tag () && $ processor ->is_tag_closer () ) {
3833+ $ processor ->set_bookmark ( 'head_end ' );
3834+ break ;
3835+ }
3836+ }
38433837
3844- // Insert the $printed_late_styles immediately after the closing inline STYLE tag. This preserves the CSS cascade.
3845- if ( '' !== $ printed_block_styles ) {
3846- $ processor ->insert_after ( $ printed_block_styles );
3838+ /*
3839+ * Insert block styles right after wp-block-library (if it is present). The placeholder CSS comment will
3840+ * always be added to the wp-block-library inline style since it gets printed at `wp_head` before the blocks
3841+ * are rendered. This means that there may not actually be any block styles to hoist from the footer to
3842+ * insert after this inline style. The placeholder CSS comment needs to be added so that the inline style
3843+ * gets printed, but if the resulting inline style is empty after the placeholder is removed, then the
3844+ * inline style is removed.
3845+ */
3846+ if ( $ processor ->has_bookmark ( 'wp_block_library ' ) ) {
3847+ $ processor ->seek ( 'wp_block_library ' );
38473848
3848- // Prevent printing them again at </head>.
3849- $ printed_block_styles = '' ;
3850- }
3849+ $ css_text = $ processor ->get_modifiable_text ();
38513850
3852- // If there aren't any late styles, there's no need to continue to finding </head>.
3853- if ( '' === $ printed_late_styles ) {
3854- break ;
3855- }
3856- } elseif ( 'HEAD ' === $ processor ->get_tag () && $ processor ->is_tag_closer () ) {
3857- $ processor ->insert_before ( $ printed_block_styles . $ printed_late_styles );
3858- break ;
3851+ /*
3852+ * A placeholder CSS comment is added to the inline style in order to force an inline STYLE tag to
3853+ * be printed. Now that we've located the inline style, the placeholder comment can be removed. If
3854+ * there is no CSS left in the STYLE tag after removing the placeholder (aside from the sourceURL
3855+ * comment, then remove the STYLE entirely.)
3856+ */
3857+ $ css_text = str_replace ( $ placeholder , '' , $ css_text );
3858+ if ( preg_match ( ':^/\*# sourceURL=\S+? \*/$: ' , trim ( $ css_text ) ) ) {
3859+ $ processor ->remove ();
3860+ } else {
3861+ $ processor ->set_modifiable_text ( $ css_text );
3862+ }
3863+
3864+ $ inserted_after = $ printed_block_styles ;
3865+ if ( ! $ processor ->has_bookmark ( 'classic_theme_styles ' ) ) {
3866+ $ inserted_after .= $ printed_global_styles ;
3867+
3868+ // Prevent printing them again at </head>.
3869+ $ printed_global_styles = '' ;
3870+ }
3871+
3872+ if ( '' !== $ inserted_after ) {
3873+ $ processor ->insert_after ( $ inserted_after );
3874+
3875+ // Prevent printing them again at </head>.
3876+ $ printed_block_styles = '' ;
38593877 }
38603878 }
38613879
3880+ if ( $ printed_global_styles && $ processor ->has_bookmark ( 'classic_theme_styles ' ) ) {
3881+ $ processor ->seek ( 'classic_theme_styles ' );
3882+ $ processor ->insert_after ( $ printed_global_styles );
3883+
3884+ // Prevent printing them again at </head>.
3885+ $ printed_global_styles = '' ;
3886+ }
3887+
3888+ // Print all remaining styles.
3889+ $ remaining_styles = $ printed_block_styles . $ printed_global_styles . $ printed_late_styles ;
3890+ if ( $ remaining_styles && $ processor ->has_bookmark ( 'head_end ' ) ) {
3891+ $ processor ->seek ( 'head_end ' );
3892+ $ processor ->insert_before ( $ remaining_styles );
3893+ }
38623894 return $ processor ->get_updated_html ();
38633895 }
38643896 );
0 commit comments