Make WordPress Core


Ignore:
Timestamp:
06/27/2023 11:26:12 AM (3 years 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.php

    r56044 r56064  
    373373        if ( ! isset( $metadata['style'] ) ) {
    374374            $metadata['style'] = "wp-block-$block_name";
     375        }
     376        if ( current_theme_supports( 'wp-block-styles' ) && wp_should_load_separate_core_block_assets() ) {
     377            $metadata['style']   = (array) $metadata['style'];
     378            $metadata['style'][] = "wp-block-{$block_name}-theme";
    375379        }
    376380        if ( ! isset( $metadata['editorStyle'] ) ) {
Note: See TracChangeset for help on using the changeset viewer.