@@ -3467,3 +3467,79 @@ function wp_remove_surrounding_empty_script_tags( $contents ) {
34673467 );
34683468 }
34693469}
3470+
3471+ /**
3472+ * Adds filters to ensure that block styles are loaded on demand in classic themes.
3473+ *
3474+ * @since 6.9.0
3475+ */
3476+ function wp_always_load_block_styles_on_demand_init () {
3477+ if ( wp_is_block_theme () ) {
3478+ return ;
3479+ }
3480+ /*
3481+ * Make sure that wp_should_output_buffer_template_for_enhancement() returns true even if there aren't any
3482+ * `wp_template_enhancement_output_buffer` filters added, but do so at priority zero so that applications which
3483+ * wish to stream responses can more easily turn this off.
3484+ */
3485+ add_filter ( 'wp_should_output_buffer_template_for_enhancement ' , '__return_true ' , 0 );
3486+
3487+ if ( ! wp_should_output_buffer_template_for_enhancement () ) {
3488+ return ;
3489+ }
3490+
3491+ // Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally,
3492+ // and so that block-specific styles will only be enqueued when they are used on the page.
3493+ add_filter ( 'should_load_separate_core_block_assets ' , '__return_true ' );
3494+
3495+ // Also ensure that block assets are loaded on demand (although the default value is should_load_separate_core_block_assets).
3496+ add_filter ( 'should_load_block_assets_on_demand ' , '__return_true ' );
3497+
3498+ // Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early.
3499+ add_action ( 'wp_template_enhancement_output_buffer_started ' , 'wp_use_placeholder_for_delayed_css ' );
3500+ }
3501+
3502+ /**
3503+ * Adds the hooks needed for CSS output to be delayed until after the content of the page has been established.
3504+ *
3505+ * @since 6.9.0
3506+ */
3507+ function wp_use_placeholder_for_delayed_css (): void {
3508+
3509+ // While normally late styles are printed, there is a filter to disable late styles, so this makes sure they are printed.
3510+ add_filter ( 'print_late_styles ' , '__return_true ' , 100 );
3511+
3512+ // Print a placeholder comment to inject late styles right after the head styles are printed.
3513+ $ placeholder = sprintf ( '<!--%s:%s--> ' , 'late_styles ' , wp_generate_uuid4 () );
3514+ remove_action ( 'wp_head ' , 'wp_print_styles ' , 8 );
3515+ add_action (
3516+ 'wp_head ' ,
3517+ static function () use ( $ placeholder ) {
3518+ wp_print_styles ();
3519+ echo $ placeholder ;
3520+ },
3521+ 8
3522+ );
3523+
3524+ // Replace logic that prints scripts and styles in the footer.
3525+ $ late_styles = '' ;
3526+ remove_action ( 'wp_print_footer_scripts ' , '_wp_footer_scripts ' );
3527+ add_action (
3528+ 'wp_print_footer_scripts ' ,
3529+ static function () use ( &$ late_styles ) {
3530+ ob_start ();
3531+ print_late_styles ();
3532+ $ late_styles = ob_get_clean ();
3533+
3534+ print_footer_scripts ();
3535+ }
3536+ );
3537+
3538+ // Replace placeholder with the captured late styles.
3539+ add_filter (
3540+ 'wp_template_enhancement_output_buffer ' ,
3541+ static function ( $ buffer ) use ( $ placeholder , &$ late_styles ) {
3542+ return str_replace ( $ placeholder , $ late_styles , $ buffer );
3543+ }
3544+ );
3545+ }
0 commit comments