diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php index dd31820057..a49217601f 100644 --- a/wp-admin/includes/upgrade.php +++ b/wp-admin/includes/upgrade.php @@ -654,6 +654,8 @@ if ( ! function_exists( 'wp_upgrade' ) ) : update_site_meta( get_current_blog_id(), 'db_last_updated', microtime() ); } + delete_transient( 'wp_core_block_css_files' ); + /** * Fires after a site is fully upgraded. * diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 3578b1d25e..5fa7f1d6d0 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -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' ) ) ? diff --git a/wp-includes/blocks/index.php b/wp-includes/blocks/index.php index 654e2fd02c..51cf0c91e8 100644 --- a/wp-includes/blocks/index.php +++ b/wp-includes/blocks/index.php @@ -12,6 +12,92 @@ require BLOCKS_PATH . 'legacy-widget.php'; require BLOCKS_PATH . 'widget-group.php'; require BLOCKS_PATH . 'require-dynamic-blocks.php'; +/** + * Registers core block style handles. + * + * While {@see register_block_style_handle()} is typically used for that, the way it is + * implemented is inefficient for core block styles. Registering those style handles here + * avoids unnecessary logic and filesystem lookups in the other function. + * + * @since 6.3.0 + */ +function register_core_block_style_handles() { + if ( ! wp_should_load_separate_core_block_assets() ) { + return; + } + + static $core_blocks_meta; + if ( ! $core_blocks_meta ) { + $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php'; + } + + $includes_url = includes_url(); + $includes_path = ABSPATH . WPINC . '/'; + $suffix = wp_scripts_get_suffix(); + $wp_styles = wp_styles(); + $style_fields = array( + 'style' => 'style', + 'editorStyle' => 'editor', + ); + + /* + * Ignore transient cache when the development mode is set to 'core'. Why? To avoid interfering with + * the core developer's workflow. + */ + if ( 'core' !== wp_get_development_mode() ) { + $transient_name = 'wp_core_block_css_files'; + $files = get_transient( $transient_name ); + if ( ! $files ) { + $files = glob( __DIR__ . '/**/**.css' ); + set_transient( $transient_name, $files ); + } + } else { + $files = glob( __DIR__ . '/**/**.css' ); + } + + foreach ( $core_blocks_meta as $name => $schema ) { + /** This filter is documented in wp-includes/blocks.php */ + $schema = apply_filters( 'block_type_metadata', $schema ); + + // Backfill these properties similar to `register_block_type_from_metadata()`. + if ( ! isset( $schema['style'] ) ) { + $schema['style'] = "wp-block-{$name}"; + } + if ( ! isset( $schema['editorStyle'] ) ) { + $schema['editorStyle'] = "wp-block-{$name}-editor"; + } + + 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 ); + } + } + } +} +add_action( 'init', 'register_core_block_style_handles', 9 ); + /** * Registers core block types using metadata files. * Dynamic core blocks are registered separately. diff --git a/wp-includes/version.php b/wp-includes/version.php index 8c1ae716a4..fdf52182a2 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-56043'; +$wp_version = '6.3-alpha-56044'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.