Skip to content

Commit 8b63fc7

Browse files
committed
fixes from review
1 parent 9d20cc6 commit 8b63fc7

File tree

4 files changed

+77
-82
lines changed

4 files changed

+77
-82
lines changed

src/wp-includes/block-styles-on-demand.php

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@
595595
add_action( 'enqueue_block_assets', 'wp_enqueue_classic_theme_styles' );
596596
add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
597597
add_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
598+
add_action( 'after_setup_theme', 'wp_always_load_block_styles_on_demand_init' );
598599
/*
599600
* `wp_enqueue_registered_block_scripts_and_styles` is bound to both
600601
* `enqueue_block_editor_assets` and `enqueue_block_assets` hooks

src/wp-includes/script-loader.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/wp-settings.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@
372372
require ABSPATH . WPINC . '/blocks/index.php';
373373
require ABSPATH . WPINC . '/block-editor.php';
374374
require ABSPATH . WPINC . '/block-patterns.php';
375-
require ABSPATH . WPINC . '/block-styles-on-demand.php';
376375
require ABSPATH . WPINC . '/class-wp-block-supports.php';
377376
require ABSPATH . WPINC . '/block-supports/utils.php';
378377
require ABSPATH . WPINC . '/block-supports/align.php';

0 commit comments

Comments
 (0)