Editor: Register core block styles in one place.

Register all core blocks in a new function called `register_core_block_style_handles`. This mirrors the function `wp_default_styles` where all core styles are registered in one place. This improves block registration performance, as it avoids expensive file lookups, like realpath in `register_block_style_handle`. The new function `register_core_block_style_handles` uses `glob` to get all css files in the blocks directory. This glob is cached in a transient to save lookups on subsequent requests. The function `register_block_style_handle` now checks to see if the style handle is already registered before trying to register it again. 

Props mukesh27, westonruter, flixos90, joemcgill, spacedmonkey.
Fixes #58528.

git-svn-id: https://develop.svn.wordpress.org/trunk@56044 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-06-26 21:15:21 +00:00
parent c1a3975367
commit d8409a205a
5 changed files with 257 additions and 18 deletions
+23 -18
View File
@@ -186,6 +186,20 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
return false;
}
$style_handle = $metadata[ $field_name ];
if ( is_array( $style_handle ) ) {
if ( empty( $style_handle[ $index ] ) ) {
return false;
}
$style_handle = $style_handle[ $index ];
}
$style_handle_name = generate_block_asset_handle( $metadata['name'], $field_name, $index );
// If the style handle is already registered, skip re-registering.
if ( wp_style_is( $style_handle_name, 'registered' ) ) {
return $style_handle_name;
}
static $wpinc_path_norm = '';
if ( ! $wpinc_path_norm ) {
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
@@ -197,14 +211,6 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
return false;
}
$style_handle = $metadata[ $field_name ];
if ( is_array( $style_handle ) ) {
if ( empty( $style_handle[ $index ] ) ) {
return false;
}
$style_handle = $style_handle[ $index ];
}
$style_path = remove_block_asset_path_prefix( $style_handle );
$is_style_handle = $style_handle === $style_path;
// Allow only passing style handles for core blocks.
@@ -246,10 +252,9 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
$style_uri = false;
}
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$result = wp_register_style(
$style_handle,
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$result = wp_register_style(
$style_handle_name,
$style_uri,
array(),
$version
@@ -259,7 +264,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
}
if ( $has_style_file ) {
wp_style_add_data( $style_handle, 'path', $style_path_norm );
wp_style_add_data( $style_handle_name, 'path', $style_path_norm );
if ( $is_core_block ) {
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
@@ -268,13 +273,13 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
}
if ( is_rtl() && file_exists( $rtl_file ) ) {
wp_style_add_data( $style_handle, 'rtl', 'replace' );
wp_style_add_data( $style_handle, 'suffix', $suffix );
wp_style_add_data( $style_handle, 'path', $rtl_file );
wp_style_add_data( $style_handle_name, 'rtl', 'replace' );
wp_style_add_data( $style_handle_name, 'suffix', $suffix );
wp_style_add_data( $style_handle_name, 'path', $rtl_file );
}
}
return $style_handle;
return $style_handle_name;
}
/**
@@ -320,7 +325,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
*/
static $core_blocks_meta;
if ( ! $core_blocks_meta ) {
$core_blocks_meta = require_once ABSPATH . WPINC . '/blocks/blocks-json.php';
$core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
}
$metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?