Make WordPress Core


Ignore:
Timestamp:
02/14/2025 06:36:48 PM (13 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.