Blocks: Remove extra `get_theme_file_path()` calls in `register_block_style_handle()`.

The `register_block_style_handle()` function runs ~200 times on each page load. Each time it runs, we call `get_theme_file_path()` and then run it through `wp_normalize_path()`.

`get_theme_file_path()` calls a few other functions: `get_stylesheet_directory()`, `get_stylesheet()`, `get_option()`, and there's a bunch of filters that run on each iteration of that, without ever changing.

By caching the value in a static variable, we can avoid ~200 calls on many functions and filters, improving performance.

Follow-up to [53091], [54290], [54291], [54309].

Props aristath, mukesh27.
Fixes #56666.
Built from https://develop.svn.wordpress.org/trunk@54327


git-svn-id: http://core.svn.wordpress.org/trunk@53886 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-09-27 15:11:14 +00:00
parent b0ebbf6beb
commit 6dfd98cfe0
2 changed files with 19 additions and 7 deletions

View File

@ -127,12 +127,14 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
);
return false;
}
// Path needs to be normalized to work in Windows env.
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
$script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );
$script_uri = plugins_url( $script_path, $metadata['file'] );
if ( $is_core_block ) {
@ -181,8 +183,8 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
}
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
// Skip registering individual styles for each core block when a bundled version provided.
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
return false;
@ -212,15 +214,25 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
if ( $is_core_block ) {
$style_path = "style$suffix.css";
}
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
$has_style_file = '' !== $style_path_norm;
if ( $has_style_file ) {
$style_uri = plugins_url( $style_path, $metadata['file'] );
$style_uri = plugins_url( $style_path, $metadata['file'] );
// Cache $theme_path_norm to avoid calling get_theme_file_path() multiple times.
static $theme_path_norm = '';
if ( ! $theme_path_norm ) {
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
}
$is_theme_block = str_starts_with( $style_path_norm, $theme_path_norm );
if ( $is_theme_block ) {
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
} elseif ( $is_core_block ) {
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
}
} else {
$style_uri = false;

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-beta1-54326';
$wp_version = '6.1-beta1-54327';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.