From 461f0b48872b595fa11112e1e9950443c74739c3 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 7 Oct 2022 15:44:31 +0000 Subject: [PATCH] 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 --- src/wp-includes/blocks.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index d6db591bc6..30219f3ca0 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -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.