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]. Props westonruter, adamsilverstein, azaozz. Merges [55658] and [55669] to the 6.2 branch. Fixes #58069. See #54243. Built from https://develop.svn.wordpress.org/branches/6.2@55736 git-svn-id: http://core.svn.wordpress.org/branches/6.2@55248 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
5db2b65b67
commit
18bf5b98aa
|
@ -2941,41 +2941,33 @@ function wp_maybe_inline_styles() {
|
||||||
* @return string The CSS with URLs made relative to the WordPress installation.
|
* @return string The CSS with URLs made relative to the WordPress installation.
|
||||||
*/
|
*/
|
||||||
function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
|
function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
|
||||||
$has_src_results = preg_match_all( '#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results );
|
return preg_replace_callback(
|
||||||
if ( $has_src_results ) {
|
'#(url\s*\(\s*[\'"]?\s*)([^\'"\)]+)#',
|
||||||
// Loop through the URLs to find relative ones.
|
static function ( $matches ) use ( $stylesheet_url ) {
|
||||||
foreach ( $src_results[1] as $src_index => $src_result ) {
|
list( , $prefix, $url ) = $matches;
|
||||||
// Skip if this is an absolute URL.
|
|
||||||
if ( 0 === strpos( $src_result, 'http' ) || 0 === strpos( $src_result, '//' ) ) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip if the URL is an HTML ID.
|
// Short-circuit if the URL does not require normalization.
|
||||||
if ( str_starts_with( $src_result, '#' ) ) {
|
if (
|
||||||
continue;
|
str_starts_with( $url, 'http:' ) ||
|
||||||
}
|
str_starts_with( $url, 'https:' ) ||
|
||||||
|
str_starts_with( $url, '//' ) ||
|
||||||
// Skip if the URL is a data URI.
|
str_starts_with( $url, '#' ) ||
|
||||||
if ( str_starts_with( $src_result, 'data:' ) ) {
|
str_starts_with( $url, 'data:' )
|
||||||
continue;
|
) {
|
||||||
|
return $matches[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the absolute URL.
|
// Build the absolute URL.
|
||||||
$absolute_url = dirname( $stylesheet_url ) . '/' . $src_result;
|
$absolute_url = dirname( $stylesheet_url ) . '/' . $url;
|
||||||
$absolute_url = str_replace( '/./', '/', $absolute_url );
|
$absolute_url = str_replace( '/./', '/', $absolute_url );
|
||||||
|
|
||||||
// Convert to URL related to the site root.
|
// Convert to URL related to the site root.
|
||||||
$relative_url = wp_make_link_relative( $absolute_url );
|
$url = wp_make_link_relative( $absolute_url );
|
||||||
|
|
||||||
// Replace the URL in the CSS.
|
return $prefix . $url;
|
||||||
$css = str_replace(
|
},
|
||||||
$src_results[0][ $src_index ],
|
$css
|
||||||
str_replace( $src_result, $relative_url, $src_results[0][ $src_index ] ),
|
);
|
||||||
$css
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $css;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.2.1-alpha-55735';
|
$wp_version = '6.2.1-alpha-55736';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue