Blocks: Avoid extra calls to realpath() in block scripts and styles registration.

This affects:
* `register_block_script_handle()`
* `register_block_style_handle()`

Both functions set a variable with this code:
{{{
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
}}}

That value never changes during page load, so we can save it to a static variable. By doing so, we can avoid ~200 calls to `realpath()` and `wp_normalize_path()`, or even more if third-party plugins register scripts or styles.

Follow-up to [52291], [52939], [54290], [54291], [54309], [54327].

Props aristath, mukesh27, SergeyBiryukov.
Fixes #56758.

git-svn-id: https://develop.svn.wordpress.org/trunk@54415 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-10-07 15:44:31 +00:00
parent 494d20c469
commit 461f0b4887

View File

@ -129,7 +129,11 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
}
// Path needs to be normalized to work in Windows env.
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
static $wpinc_path_norm = '';
if ( ! $wpinc_path_norm ) {
$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 ) );
@ -182,7 +186,10 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
return false;
}
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
static $wpinc_path_norm = '';
if ( ! $wpinc_path_norm ) {
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
}
$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.