Make WordPress Core

Changeset 54327


Ignore:
Timestamp:
09/27/2022 03:09:14 PM (2 months ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r54323 r54327  
    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'] );
     
    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() ) {
     
    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 {
Note: See TracChangeset for help on using the changeset viewer.