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/script-loader.php

    r56047 r56064  
    23732373    wp_enqueue_style( 'wp-block-library' );
    23742374
    2375     if ( current_theme_supports( 'wp-block-styles' ) ) {
    2376         if ( wp_should_load_separate_core_block_assets() ) {
    2377             $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'css' : 'min.css';
    2378             $files  = glob( __DIR__ . "/blocks/**/theme.$suffix" );
    2379             foreach ( $files as $path ) {
    2380                 $block_name = basename( dirname( $path ) );
    2381                 if ( is_rtl() && file_exists( __DIR__ . "/blocks/$block_name/theme-rtl.$suffix" ) ) {
    2382                     $path = __DIR__ . "/blocks/$block_name/theme-rtl.$suffix";
    2383                 }
    2384                 wp_add_inline_style( "wp-block-{$block_name}", file_get_contents( $path ) );
    2385             }
    2386         } else {
    2387             wp_enqueue_style( 'wp-block-library-theme' );
    2388         }
     2375    if ( current_theme_supports( 'wp-block-styles' ) && ! wp_should_load_separate_core_block_assets() ) {
     2376        wp_enqueue_style( 'wp-block-library-theme' );
    23892377    }
    23902378
Note: See TracChangeset for help on using the changeset viewer.