diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 801da32c21..44e9263b78 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -2701,6 +2701,7 @@ function wp_maybe_inline_styles() { if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) { $styles[] = array( 'handle' => $handle, + 'src' => $wp_styles->registered[ $handle ]->src, 'path' => $wp_styles->registered[ $handle ]->extra['path'], 'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ), ); @@ -2735,6 +2736,10 @@ function wp_maybe_inline_styles() { // Get the styles if we don't already have them. $style['css'] = file_get_contents( $style['path'] ); + // Check if the style contains relative URLs that need to be modified. + // URLs relative to the stylesheet's path should be converted to relative to the site's root. + $style['css'] = _wp_normalize_relative_css_links( $style['css'], $style['src'] ); + // Set `src` to `false` and add styles inline. $wp_styles->registered[ $style['handle'] ]->src = false; if ( empty( $wp_styles->registered[ $style['handle'] ]->extra['after'] ) ) { @@ -2748,6 +2753,45 @@ function wp_maybe_inline_styles() { } } +/** + * Make URLs relative to the WordPress installation. + * + * @since 5.9.0 + * @access private + * + * @param string $css The CSS to make URLs relative to the WordPress installation. + * @param string $stylesheet_url The URL to the stylesheet. + * + * @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; + } + + // 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; +} + /** * Inject the block editor assets that need to be loaded into the editor's iframe as an inline script. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 56d16fdac5..8e95e2f2d3 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-52035'; +$wp_version = '5.9-alpha-52036'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.