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.

git-svn-id: https://develop.svn.wordpress.org/trunk@54327 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-09-27 15:09:14 +00:00
parent f53188ffff
commit 8b154385cb

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;