Make WordPress Core


Ignore:
Timestamp:
06/27/2023 11:26:12 AM (21 months ago)
Author:
spacedmonkey
Message:

Script Loader: Fix performance issues in wp_common_block_scripts_and_styles.

In [52069] the function wp_common_block_scripts_and_styles was changed load individual theme stylesheets, if the current theme supports block styles and loading separate core block assets. To do this, the function calls many expensive file operation functions, such as glob, file_exists and file_get_contents. This is wasteful, as these functions are loaded on every page request, even request that do not include blocks, like REST API calls. In [56044] all core block styles are registered in a single place. In register_core_block_style_handles calls glob to get all css styles in block directories. While registering style and editor styles, also register block theme styles, under a new style handle. Example wp-block-avatar-theme. If the current theme supports block styles, also request the block to enqueue the theme style on the front end. As these new stylesheets have a path attribute set, the function wp_maybe_inline_styles will automatically inline the styles for you.

Props spacedmonkey, flixos90, oandregal, costdev, audrasjb, mukesh27.
Fixes #58560.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php

    r56044 r56064  
    5656     *
    5757     * @dataProvider data_block_data
     58     *
     59     * @param string $name   The block name.
     60     * @param array  $schema The block's schema.
    5861     */
    5962    public function test_wp_should_load_separate_core_block_assets_false( $name, $schema ) {
     
    7578     *
    7679     * @dataProvider data_block_data
     80     *
     81     * @param string $name   The block name.
     82     * @param array  $schema The block's schema.
    7783     */
    7884    public function test_wp_should_load_separate_core_block_assets_true( $name, $schema ) {
     
    100106    }
    101107
     108    /**
     109     * @ticket 58560
     110     *
     111     * @dataProvider data_block_data
     112     *
     113     * @param string $name The block name.
     114     */
     115    public function test_wp_should_load_separate_core_block_assets_current_theme_supports( $name ) {
     116        add_filter( 'should_load_separate_core_block_assets', '__return_true' );
     117        add_theme_support( 'wp-block-styles' );
     118        register_core_block_style_handles();
     119
     120        $wp_styles = $GLOBALS['wp_styles'];
     121
     122        $style_handle = "wp-block-{$name}-theme";
     123
     124        $this->assertArrayHasKey( $style_handle, $wp_styles->registered, 'The key should exist, as this style should be registered' );
     125        if ( false === $wp_styles->registered[ $style_handle ]->src ) {
     126            $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, 'If source is false, not style path should be set' );
     127        } else {
     128            $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, 'Source of style should contain the includes url' );
     129            $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, 'The path of the style should exist' );
     130            $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, 'The path key of the style should exist in extra array' );
     131            $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], 'The path key of the style should not be empty' );
     132        }
     133    }
    102134
    103135    public function data_block_data() {
Note: See TracChangeset for help on using the changeset viewer.