From 189c1ee49a64071f156918ec764c998cdc9badf8 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 19 Apr 2023 01:26:28 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@55658 git-svn-id: http://core.svn.wordpress.org/trunk@55170 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/script-loader.php | 59 +++++++++++++++-------------------- wp-includes/version.php | 2 +- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index ba94e3b3d2..9423145a77 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -2939,41 +2939,34 @@ function wp_maybe_inline_styles() { * @return string The CSS with URLs made relative to the WordPress installation. */ function _wp_normalize_relative_css_links( $css, $stylesheet_url ) { - $has_src_results = preg_match_all( '#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results ); - if ( $has_src_results ) { - // Loop through the URLs to find relative ones. - foreach ( $src_results[1] as $src_index => $src_result ) { - // Skip if this is an absolute URL. - if ( 0 === strpos( $src_result, 'http' ) || 0 === strpos( $src_result, '//' ) ) { - continue; + return preg_replace_callback( + '#(url\s*\(\s*[\'"]?\s*)([^\'"\)]+)#', + static function ( $matches ) use ( $stylesheet_url ) { + list( , $prefix, $url ) = $matches; + + if ( ! ( + str_starts_with( $url, 'http:' ) + || + str_starts_with( $url, 'https:' ) + || + str_starts_with( $url, '//' ) + || + str_starts_with( $url, '#' ) + || + str_starts_with( $url, 'data:' ) + ) ) { + // Build the absolute URL. + $absolute_url = dirname( $stylesheet_url ) . '/' . $url; + $absolute_url = str_replace( '/./', '/', $absolute_url ); + + // Convert to URL related to the site root. + $url = wp_make_link_relative( $absolute_url ); } - // Skip if the URL is an HTML ID. - if ( str_starts_with( $src_result, '#' ) ) { - continue; - } - - // Skip if the URL is a data URI. - if ( str_starts_with( $src_result, 'data:' ) ) { - continue; - } - - // Build the absolute URL. - $absolute_url = dirname( $stylesheet_url ) . '/' . $src_result; - $absolute_url = str_replace( '/./', '/', $absolute_url ); - // Convert to URL related to the site root. - $relative_url = wp_make_link_relative( $absolute_url ); - - // Replace the URL in the CSS. - $css = str_replace( - $src_results[0][ $src_index ], - str_replace( $src_result, $relative_url, $src_results[0][ $src_index ] ), - $css - ); - } - } - - return $css; + return $prefix . $url; + }, + $css + ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 1f40359f1c..e35a4f0ef6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-55657'; +$wp_version = '6.3-alpha-55658'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.