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. Built from https://develop.svn.wordpress.org/trunk@56064 git-svn-id: http://core.svn.wordpress.org/trunk@55576 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
d7bda1a6be
commit
c009f9d719
|
@ -373,6 +373,10 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
|
|||
if ( ! isset( $metadata['style'] ) ) {
|
||||
$metadata['style'] = "wp-block-$block_name";
|
||||
}
|
||||
if ( current_theme_supports( 'wp-block-styles' ) && wp_should_load_separate_core_block_assets() ) {
|
||||
$metadata['style'] = (array) $metadata['style'];
|
||||
$metadata['style'][] = "wp-block-{$block_name}-theme";
|
||||
}
|
||||
if ( ! isset( $metadata['editorStyle'] ) ) {
|
||||
$metadata['editorStyle'] = "wp-block-{$block_name}-editor";
|
||||
}
|
||||
|
|
|
@ -55,6 +55,29 @@ function register_core_block_style_handles() {
|
|||
$files = glob( __DIR__ . '/**/**.css' );
|
||||
}
|
||||
|
||||
$register_style = static function( $name, $filename, $style_handle ) use ( $includes_path, $includes_url, $suffix, $wp_styles, $files ) {
|
||||
$style_path = "blocks/{$name}/{$filename}{$suffix}.css";
|
||||
$path = $includes_path . $style_path;
|
||||
|
||||
if ( ! in_array( $path, $files, true ) ) {
|
||||
$wp_styles->add(
|
||||
$style_handle,
|
||||
false
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$wp_styles->add( $style_handle, $includes_url . $style_path );
|
||||
$wp_styles->add_data( $style_handle, 'path', $path );
|
||||
|
||||
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path );
|
||||
if ( is_rtl() && in_array( $rtl_file, $files, true ) ) {
|
||||
$wp_styles->add_data( $style_handle, 'rtl', 'replace' );
|
||||
$wp_styles->add_data( $style_handle, 'suffix', $suffix );
|
||||
$wp_styles->add_data( $style_handle, 'path', $rtl_file );
|
||||
}
|
||||
};
|
||||
|
||||
foreach ( $core_blocks_meta as $name => $schema ) {
|
||||
/** This filter is documented in wp-includes/blocks.php */
|
||||
$schema = apply_filters( 'block_type_metadata', $schema );
|
||||
|
@ -67,32 +90,15 @@ function register_core_block_style_handles() {
|
|||
$schema['editorStyle'] = "wp-block-{$name}-editor";
|
||||
}
|
||||
|
||||
// Register block theme styles.
|
||||
$register_style( $name, 'theme', "wp-block-{$name}-theme" );
|
||||
|
||||
foreach ( $style_fields as $style_field => $filename ) {
|
||||
$style_handle = $schema[ $style_field ];
|
||||
if ( is_array( $style_handle ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$style_path = "blocks/{$name}/{$filename}{$suffix}.css";
|
||||
$path = $includes_path . $style_path;
|
||||
|
||||
if ( ! in_array( $path, $files, true ) ) {
|
||||
$wp_styles->add(
|
||||
$style_handle,
|
||||
false
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$wp_styles->add( $style_handle, $includes_url . $style_path );
|
||||
$wp_styles->add_data( $style_handle, 'path', $path );
|
||||
|
||||
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path );
|
||||
if ( is_rtl() && in_array( $rtl_file, $files, true ) ) {
|
||||
$wp_styles->add_data( $style_handle, 'rtl', 'replace' );
|
||||
$wp_styles->add_data( $style_handle, 'suffix', $suffix );
|
||||
$wp_styles->add_data( $style_handle, 'path', $rtl_file );
|
||||
}
|
||||
$register_style( $name, $filename, $style_handle );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2372,20 +2372,8 @@ function wp_common_block_scripts_and_styles() {
|
|||
|
||||
wp_enqueue_style( 'wp-block-library' );
|
||||
|
||||
if ( current_theme_supports( 'wp-block-styles' ) ) {
|
||||
if ( wp_should_load_separate_core_block_assets() ) {
|
||||
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'css' : 'min.css';
|
||||
$files = glob( __DIR__ . "/blocks/**/theme.$suffix" );
|
||||
foreach ( $files as $path ) {
|
||||
$block_name = basename( dirname( $path ) );
|
||||
if ( is_rtl() && file_exists( __DIR__ . "/blocks/$block_name/theme-rtl.$suffix" ) ) {
|
||||
$path = __DIR__ . "/blocks/$block_name/theme-rtl.$suffix";
|
||||
}
|
||||
wp_add_inline_style( "wp-block-{$block_name}", file_get_contents( $path ) );
|
||||
}
|
||||
} else {
|
||||
wp_enqueue_style( 'wp-block-library-theme' );
|
||||
}
|
||||
if ( current_theme_supports( 'wp-block-styles' ) && ! wp_should_load_separate_core_block_assets() ) {
|
||||
wp_enqueue_style( 'wp-block-library-theme' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.3-alpha-56063';
|
||||
$wp_version = '6.3-alpha-56064';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue