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;
}