Make WordPress Core

Changeset 59823


Ignore:
Timestamp:
02/14/2025 06:36:48 PM (3 months ago)
Author:
flixos90
Message:

Editor: Introduce wp_should_load_block_assets_on_demand() with filter 'should_load_block_assets_on_demand'.

This function and filter complement the existing wp_should_load_separate_core_block_assets() with filter 'should_load_separate_core_block_assets', which until now was responsible for two different purposes:

  1. Loading separate stylesheets for Core blocks, instead of a combined wp-block-library stylesheet (as the name indicates).
  2. Loading block scripts and stylesheets on demand only if the blocks are included in the page (not indicated by the name).

The new function and filter handles exclusively the 2nd purpose, making it possible to individually adjust both behaviors. For backward compatibility, the return value of wp_should_load_separate_core_block_assets() is used as the filterable default for wp_should_load_block_assets_on_demand(). Yet, the two filters can now be individually be controlled: For example, a site owner that wants to keep loading the combined wp-block-library stylesheet can now do so without giving up on the ability to load block scripts and stylesheets on demand.

Block themes now opt in by default to both features, similar to how they were already doing before via just the one filter. This way, block themes that opt out of loading separate stylesheets for Core blocks will still benefit from loading block scripts and stylesheets on demand, which in the case of block themes is strongly recommended.

Props fabiankaegy, flixos90, gziolo.
Fixes #61965.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block.php

    r59662 r59823  
    609609        }
    610610
     611        /*
     612         * For Core blocks, these styles are only enqueued if `wp_should_load_separate_core_block_assets()` returns
     613         * true. Otherwise these `wp_enqueue_style()` calls will not have any effect, as the Core blocks are relying on
     614         * the combined 'wp-block-library' stylesheet instead, which is unconditionally enqueued.
     615         */
    611616        if ( ( ! empty( $this->block_type->style_handles ) ) ) {
    612617            foreach ( $this->block_type->style_handles as $style_handle ) {
  • trunk/src/wp-includes/global-styles-and-settings.php

    r59499 r59823  
    299299        }
    300300
    301         if ( ! wp_should_load_separate_core_block_assets() ) {
     301        if ( ! wp_should_load_block_assets_on_demand() ) {
    302302            wp_add_inline_style( 'global-styles', $block_css );
    303303            continue;
     
    307307
    308308        /*
    309          * When `wp_should_load_separate_core_block_assets()` is true, block styles are
     309         * When `wp_should_load_block_assets_on_demand()` is true, block styles are
    310310         * enqueued for each block on the page in class WP_Block's render function.
    311311         * This means there will be a handle in the styles queue for each of those blocks.
     
    314314         * before adding the inline style.
    315315         * This conditional loading only applies to core blocks.
     316         * TODO: Explore how this could be expanded to third-party blocks as well.
    316317         */
    317318        if ( isset( $metadata['name'] ) ) {
  • trunk/src/wp-includes/script-loader.php

    r59793 r59823  
    24882488 */
    24892489function wp_enqueue_global_styles() {
    2490     $separate_assets  = wp_should_load_separate_core_block_assets();
     2490    $assets_on_demand = wp_should_load_block_assets_on_demand();
    24912491    $is_block_theme   = wp_is_block_theme();
    24922492    $is_classic_theme = ! $is_block_theme;
    24932493
    24942494    /*
    2495      * Global styles should be printed in the head when loading all styles combined.
    2496      * The footer should only be used to print global styles for classic themes with separate core assets enabled.
    2497      *
    2498      * See https://core.trac.wordpress.org/ticket/53494.
     2495     * Global styles should be printed in the head for block themes, or for classic themes when loading assets on
     2496     * demand is disabled, which is the default.
     2497     * The footer should only be used for classic themes when loading assets on demand is enabled.
     2498     *
     2499     * See https://core.trac.wordpress.org/ticket/53494 and https://core.trac.wordpress.org/ticket/61965.
    24992500     */
    25002501    if (
    25012502        ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
    2502         ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $separate_assets ) ||
    2503         ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $separate_assets )
     2503        ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $assets_on_demand ) ||
     2504        ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand )
    25042505    ) {
    25052506        return;
     
    25682569
    25692570/**
    2570  * Checks whether separate styles should be loaded for core blocks on-render.
    2571  *
    2572  * When this function returns true, other functions ensure that core blocks
    2573  * only load their assets on-render, and each block loads its own, individual
    2574  * assets. Third-party blocks only load their assets when rendered.
    2575  *
    2576  * When this function returns false, all core block assets are loaded regardless
    2577  * of whether they are rendered in a page or not, because they are all part of
    2578  * the `block-library/style.css` file. Assets for third-party blocks are always
    2579  * enqueued regardless of whether they are rendered or not.
     2571 * Checks whether separate styles should be loaded for core blocks.
     2572 *
     2573 * When this function returns true, other functions ensure that core blocks use their own separate stylesheets.
     2574 * When this function returns false, all core blocks will use the single combined 'wp-block-library' stylesheet.
     2575 *
     2576 * As a side effect, the return value will by default result in block assets to be loaded on demand, via the
     2577 * {@see wp_should_load_block_assets_on_demand()} function. This behavior can be separately altered via that function.
    25802578 *
    25812579 * This only affects front end and not the block editor screens.
    25822580 *
     2581 * @since 5.8.0
     2582 * @see @see wp_should_load_block_assets_on_demand()
    25832583 * @see wp_enqueue_registered_block_scripts_and_styles()
    25842584 * @see register_block_style_handle()
    25852585 *
    2586  * @since 5.8.0
    2587  *
    2588  * @return bool Whether separate assets will be loaded.
     2586 * @return bool Whether separate core block assets will be loaded.
    25892587 */
    25902588function wp_should_load_separate_core_block_assets() {
     
    26082606
    26092607/**
     2608 * Checks whether block styles should be loaded only on-render.
     2609 *
     2610 * When this function returns true, other functions ensure that blocks only load their assets on-render.
     2611 * When this function returns false, all block assets are loaded regardless of whether they are rendered in a page.
     2612 *
     2613 * The default return value depends on the result of {@see wp_should_load_separate_core_block_assets()}, which controls
     2614 * whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library' stylesheet.
     2615 *
     2616 * This only affects front end and not the block editor screens.
     2617 *
     2618 * @since 6.8.0
     2619 * @see wp_should_load_separate_core_block_assets()
     2620 *
     2621 * @return bool Whether to load block assets only when they are rendered.
     2622 */
     2623function wp_should_load_block_assets_on_demand() {
     2624    if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
     2625        return false;
     2626    }
     2627
     2628    /*
     2629     * For backward compatibility, the default return value for this function is based on the return value of
     2630     * `wp_should_load_separate_core_block_assets()`. Initially, this function used to control both of these concerns.
     2631     */
     2632    $load_assets_on_demand = wp_should_load_separate_core_block_assets();
     2633
     2634    /**
     2635     * Filters whether block styles should be loaded on demand.
     2636     *
     2637     * Returning false loads all block assets, regardless of whether they are rendered in a page or not.
     2638     * Returning true loads block assets only when they are rendered.
     2639     *
     2640     * The default value of the filter depends on the result of {@see wp_should_load_separate_core_block_assets()},
     2641     * which controls whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library'
     2642     * stylesheet.
     2643     *
     2644     * @since 6.8.0
     2645     *
     2646     * @param bool $load_assets_on_demand Whether to load block assets only when they are rendered.
     2647     */
     2648    return apply_filters( 'should_load_block_assets_on_demand', $load_assets_on_demand );
     2649}
     2650
     2651/**
    26102652 * Enqueues registered block scripts and styles, depending on current rendered
    26112653 * context (only enqueuing editor scripts while in context of the editor).
     
    26182660    global $current_screen;
    26192661
    2620     if ( wp_should_load_separate_core_block_assets() ) {
     2662    if ( wp_should_load_block_assets_on_demand() ) {
    26212663        return;
    26222664    }
     
    26252667
    26262668    $block_registry = WP_Block_Type_Registry::get_instance();
     2669
     2670    /*
     2671     * Block styles are only enqueued if they're registered. For core blocks, this is only the case if
     2672     * `wp_should_load_separate_core_block_assets()` returns true. Otherwise they use the single combined
     2673     * 'wp-block-library` stylesheet. See also `register_core_block_style_handles()`.
     2674     * Since `wp_enqueue_style()` does not trigger warnings if the style is not registered, it is okay to not cater for
     2675     * this behavior here and simply call `wp_enqueue_style()` unconditionally.
     2676     */
    26272677    foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
    26282678        // Front-end and editor styles.
     
    26662716            if ( isset( $style_properties['style_handle'] ) ) {
    26672717
    2668                 // If the site loads separate styles per-block, enqueue the stylesheet on render.
    2669                 if ( wp_should_load_separate_core_block_assets() ) {
     2718                // If the site loads block styles on demand, enqueue the stylesheet on render.
     2719                if ( wp_should_load_block_assets_on_demand() ) {
    26702720                    add_filter(
    26712721                        'render_block',
     
    26882738                $handle = 'wp-block-library';
    26892739
    2690                 // If the site loads separate styles per-block, check if the block has a stylesheet registered.
    2691                 if ( wp_should_load_separate_core_block_assets() ) {
     2740                // If the site loads block styles on demand, check if the block has a stylesheet registered.
     2741                if ( wp_should_load_block_assets_on_demand() ) {
    26922742                    $block_stylesheet_handle = generate_block_asset_handle( $block_name, 'style' );
    26932743
     
    31883238 * Enqueues a stylesheet for a specific block.
    31893239 *
    3190  * If the theme has opted-in to separate-styles loading,
     3240 * If the theme has opted-in to load block styles on demand,
    31913241 * then the stylesheet will be enqueued on-render,
    31923242 * otherwise when the block inits.
     
    32563306
    32573307    $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
    3258     if ( wp_should_load_separate_core_block_assets() ) {
     3308    if ( wp_should_load_block_assets_on_demand() ) {
    32593309        /**
    32603310         * Callback function to register and enqueue styles.
  • trunk/src/wp-includes/theme.php

    r59813 r59823  
    43964396
    43974397    add_filter( 'should_load_separate_core_block_assets', '__return_true' );
     4398    add_filter( 'should_load_block_assets_on_demand', '__return_true' );
    43984399
    43994400    /*
  • trunk/tests/phpunit/tests/theme.php

    r59579 r59823  
    967967
    968968    /**
     969     * Tests that block themes load block assets on demand by default.
     970     *
     971     * @ticket 61965
     972     *
     973     * @covers ::_add_default_theme_supports
     974     * @covers ::wp_should_load_block_assets_on_demand
     975     */
     976    public function test_block_theme_should_load_block_assets_on_demand_by_default() {
     977        $this->helper_requires_block_theme();
     978
     979        add_filter( 'should_load_block_assets_on_demand', '__return_false' );
     980
     981        $this->assertFalse(
     982            wp_should_load_block_assets_on_demand(),
     983            'Could not disable loading block assets on demand.'
     984        );
     985
     986        do_action( 'after_setup_theme' );
     987        add_filter( 'should_load_separate_core_block_assets', '__return_false' );
     988
     989        $this->assertTrue(
     990            wp_should_load_block_assets_on_demand(),
     991            'Block themes do not load block assets on demand by default.'
     992        );
     993    }
     994
     995    /**
     996     * Tests that block themes load block assets on demand by default even when loading separate core block assets is disabled.
     997     *
     998     * @ticket 61965
     999     *
     1000     * @covers ::_add_default_theme_supports
     1001     * @covers ::wp_should_load_block_assets_on_demand
     1002     */
     1003    public function test_block_theme_should_load_block_assets_on_demand_by_default_even_with_separate_core_block_assets_disabled() {
     1004        $this->helper_requires_block_theme();
     1005
     1006        do_action( 'after_setup_theme' );
     1007        add_filter( 'should_load_separate_core_block_assets', '__return_false' );
     1008
     1009        $this->assertTrue( wp_should_load_block_assets_on_demand() );
     1010    }
     1011
     1012    /**
    9691013     * Tests that a theme in the custom test data theme directory is recognized.
    9701014     *
Note: See TracChangeset for help on using the changeset viewer.