Skip to content

Commit 8b15438

Browse files
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
1 parent f53188f commit 8b15438

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/wp-includes/blocks.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,14 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
127127
);
128128
return false;
129129
}
130+
130131
// Path needs to be normalized to work in Windows env.
131132
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
132133
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
133134
$script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
134-
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
135-
$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );
135+
136+
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
137+
$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );
136138

137139
$script_uri = plugins_url( $script_path, $metadata['file'] );
138140
if ( $is_core_block ) {
@@ -181,8 +183,8 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
181183
}
182184

183185
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
184-
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
185-
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
186+
187+
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
186188
// Skip registering individual styles for each core block when a bundled version provided.
187189
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
188190
return false;
@@ -212,15 +214,25 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
212214
if ( $is_core_block ) {
213215
$style_path = "style$suffix.css";
214216
}
217+
215218
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
216219
$has_style_file = '' !== $style_path_norm;
220+
217221
if ( $has_style_file ) {
218-
$style_uri = plugins_url( $style_path, $metadata['file'] );
222+
$style_uri = plugins_url( $style_path, $metadata['file'] );
223+
224+
// Cache $theme_path_norm to avoid calling get_theme_file_path() multiple times.
225+
static $theme_path_norm = '';
226+
if ( ! $theme_path_norm ) {
227+
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
228+
}
229+
219230
$is_theme_block = str_starts_with( $style_path_norm, $theme_path_norm );
231+
220232
if ( $is_theme_block ) {
221233
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
222234
} elseif ( $is_core_block ) {
223-
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
235+
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
224236
}
225237
} else {
226238
$style_uri = false;

0 commit comments

Comments
 (0)