public static absolutize_css_urls ( $css, $css_file_url ) : mixed | string | ||
$css | string: The raw CSS -- should be read in directly from the file. | |
$css_file_url | : The URL that the file can be accessed at, for calculating paths from. | |
return | mixed | string |
public static function absolutize_css_urls($css, $css_file_url)
{
$pattern = '#url\\((?P<path>[^)]*)\\)#i';
$css_dir = dirname($css_file_url);
$p = parse_url($css_dir);
$domain = sprintf('%1$s//%2$s%3$s%4$s', isset($p['scheme']) ? "{$p['scheme']}:" : '', isset($p['user'], $p['pass']) ? "{$p['user']}:{$p['pass']}@" : '', $p['host'], isset($p['port']) ? ":{$p['port']}" : '');
if (preg_match_all($pattern, $css, $matches, PREG_SET_ORDER)) {
$find = $replace = array();
foreach ($matches as $match) {
$url = trim($match['path'], "'\" \t");
// If this is a data url, we don't want to mess with it.
if ('data:' === substr($url, 0, 5)) {
continue;
}
// If this is an absolute or protocol-agnostic url,
// we don't want to mess with it.
if (preg_match('#^(https?:)?//#i', $url)) {
continue;
}
switch (substr($url, 0, 1)) {
case '/':
$absolute = $domain . $url;
break;
default:
$absolute = $css_dir . '/' . $url;
}
$find[] = $match[0];
$replace[] = sprintf('url("%s")', $absolute);
}
$css = str_replace($find, $replace, $css);
}
return $css;
}
/** * Maybe inlines a stylesheet. * * If you'd like to inline a stylesheet instead of printing a link to it, * wp_style_add_data( 'handle', 'jetpack-inline', true ); * * Attached to `style_loader_tag` filter. * * @param string $tag The tag that would link to the external asset. * @param string $handle The registered handle of the script in question. * * @return string */ public static function maybe_inline_style($tag, $handle) { global $wp_styles; $item = $wp_styles->registered[$handle]; if (!isset($item->extra['jetpack-inline']) || !$item->extra['jetpack-inline']) { return $tag; } if (preg_match('# href=\'([^\']+)\' #i', $tag, $matches)) { $href = $matches[1]; // Strip off query string if ($pos = strpos($href, '?')) { $href = substr($href, 0, $pos); } // Strip off fragment if ($pos = strpos($href, '#')) { $href = substr($href, 0, $pos); } } else { return $tag; } $plugins_dir = plugin_dir_url(JETPACK__PLUGIN_FILE); if ($plugins_dir !== substr($href, 0, strlen($plugins_dir))) { return $tag; } // If this stylesheet has a RTL version, and the RTL version replaces normal... if (isset($item->extra['rtl']) && 'replace' === $item->extra['rtl'] && is_rtl()) { // And this isn't the pass that actually deals with the RTL version... if (false === strpos($tag, " id='{$handle}-rtl-css' ")) { // Short out, as the RTL version will deal with it in a moment. return $tag; } } $file = JETPACK__PLUGIN_DIR . substr($href, strlen($plugins_dir)); $css = Jetpack::absolutize_css_urls(file_get_contents($file), $href); if ($css) { $tag = "<!-- Inline {$item->handle} -->\r\n"; if (empty($item->extra['after'])) { wp_add_inline_style($handle, $css); } else { array_unshift($item->extra['after'], $css); wp_style_add_data($handle, 'after', $item->extra['after']); } } return $tag; }