Make WordPress Core


Ignore:
Timestamp:
06/27/2023 11:26:12 AM (8 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/src/wp-includes/blocks/index.php

    r56044 r56064  
    5656    }
    5757
     58    $register_style = static function( $name, $filename, $style_handle ) use ( $includes_path, $includes_url, $suffix, $wp_styles, $files ) {
     59        $style_path = "blocks/{$name}/{$filename}{$suffix}.css";
     60        $path       = $includes_path . $style_path;
     61
     62        if ( ! in_array( $path, $files, true ) ) {
     63            $wp_styles->add(
     64                $style_handle,
     65                false
     66            );
     67            return;
     68        }
     69
     70        $wp_styles->add( $style_handle, $includes_url . $style_path );
     71        $wp_styles->add_data( $style_handle, 'path', $path );
     72
     73        $rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path );
     74        if ( is_rtl() && in_array( $rtl_file, $files, true ) ) {
     75            $wp_styles->add_data( $style_handle, 'rtl', 'replace' );
     76            $wp_styles->add_data( $style_handle, 'suffix', $suffix );
     77            $wp_styles->add_data( $style_handle, 'path', $rtl_file );
     78        }
     79    };
     80
    5881    foreach ( $core_blocks_meta as $name => $schema ) {
    5982        /** This filter is documented in wp-includes/blocks.php */
     
    6891        }
    6992
     93        // Register block theme styles.
     94        $register_style( $name, 'theme', "wp-block-{$name}-theme" );
     95
    7096        foreach ( $style_fields as $style_field => $filename ) {
    7197            $style_handle = $schema[ $style_field ];
     
    7399                continue;
    74100            }
    75 
    76             $style_path = "blocks/{$name}/{$filename}{$suffix}.css";
    77             $path       = $includes_path . $style_path;
    78 
    79             if ( ! in_array( $path, $files, true ) ) {
    80                 $wp_styles->add(
    81                     $style_handle,
    82                     false
    83                 );
    84                 continue;
    85             }
    86 
    87             $wp_styles->add( $style_handle, $includes_url . $style_path );
    88             $wp_styles->add_data( $style_handle, 'path', $path );
    89 
    90             $rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path );
    91             if ( is_rtl() && in_array( $rtl_file, $files, true ) ) {
    92                 $wp_styles->add_data( $style_handle, 'rtl', 'replace' );
    93                 $wp_styles->add_data( $style_handle, 'suffix', $suffix );
    94                 $wp_styles->add_data( $style_handle, 'path', $rtl_file );
    95             }
     101            $register_style( $name, $filename, $style_handle );
    96102        }
    97103    }
Note: See TracChangeset for help on using the changeset viewer.