Make WordPress Core


Ignore:
Timestamp:
04/19/2023 01:24:40 AM (15 months ago)
Author:
westonruter
Message:

Script Loader: Optimize performance of _wp_normalize_relative_css_links() by more than 2x.

  • Replace preg_match_all() and its secondary str_replace() call with preg_replace_callback().
  • Fix case where paths beginning with http and https (but not http: and https:) were erroneously not counted as relative.
  • Improve code style and readability by consolidating conditions and returning once.
  • Use str_starts_with() consistently instead of strpos().

Follow-up to [52036], [52695], and [52754].

Fixes #58069.
See #54243.

File:
1 edited

Legend:

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

    r55628 r55658  
    29402940 */
    29412941function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
    2942     $has_src_results = preg_match_all( '#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results );
    2943     if ( $has_src_results ) {
    2944         // Loop through the URLs to find relative ones.
    2945         foreach ( $src_results[1] as $src_index => $src_result ) {
    2946             // Skip if this is an absolute URL.
    2947             if ( 0 === strpos( $src_result, 'http' ) || 0 === strpos( $src_result, '//' ) ) {
    2948                 continue;
     2942    return preg_replace_callback(
     2943        '#(url\s*\(\s*[\'"]?\s*)([^\'"\)]+)#',
     2944        static function ( $matches ) use ( $stylesheet_url ) {
     2945            list( , $prefix, $url ) = $matches;
     2946
     2947            if ( ! (
     2948                str_starts_with( $url, 'http:' )
     2949                ||
     2950                str_starts_with( $url, 'https:' )
     2951                ||
     2952                str_starts_with( $url, '//' )
     2953                ||
     2954                str_starts_with( $url, '#' )
     2955                ||
     2956                str_starts_with( $url, 'data:' )
     2957            ) ) {
     2958                // Build the absolute URL.
     2959                $absolute_url = dirname( $stylesheet_url ) . '/' . $url;
     2960                $absolute_url = str_replace( '/./', '/', $absolute_url );
     2961
     2962                // Convert to URL related to the site root.
     2963                $url = wp_make_link_relative( $absolute_url );
    29492964            }
    29502965
    2951             // Skip if the URL is an HTML ID.
    2952             if ( str_starts_with( $src_result, '#' ) ) {
    2953                 continue;
    2954             }
    2955 
    2956             // Skip if the URL is a data URI.
    2957             if ( str_starts_with( $src_result, 'data:' ) ) {
    2958                 continue;
    2959             }
    2960 
    2961             // Build the absolute URL.
    2962             $absolute_url = dirname( $stylesheet_url ) . '/' . $src_result;
    2963             $absolute_url = str_replace( '/./', '/', $absolute_url );
    2964             // Convert to URL related to the site root.
    2965             $relative_url = wp_make_link_relative( $absolute_url );
    2966 
    2967             // Replace the URL in the CSS.
    2968             $css = str_replace(
    2969                 $src_results[0][ $src_index ],
    2970                 str_replace( $src_result, $relative_url, $src_results[0][ $src_index ] ),
    2971                 $css
    2972             );
    2973         }
    2974     }
    2975 
    2976     return $css;
     2966            return $prefix . $url;
     2967        },
     2968        $css
     2969    );
    29772970}
    29782971
Note: See TracChangeset for help on using the changeset viewer.