absolutize_css_urls() public static method

Considerations: - Normal, relative URLs feh.png - Data URLs data:image/gif;base64,eh129ehiuehjdhsa== - Schema-agnostic URLs //domain.com/feh.png - Absolute URLs http://domain.com/feh.png - Domain root relative URLs /feh.png
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
 /**
  * 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;
 }
Example #2
0
    /**
     * @author georgestephanis
     * @covers Jetpack::absolutize_css_urls
     */
    public function test_absolutize_css_urls_properly_handles_use_cases()
    {
        $css = <<<CSS
.test-it {
\tbackground: url(same-dir.png);
\tbackground: url('same-dir.png');
\tbackground: url("same-dir.png");
\tbackground: url( same-dir.png );
\tbackground: url( 'same-dir.png' );
\tbackground: url( "same-dir.png" );
\tbackground: url(\t\tsame-dir.png\t\t);
\tbackground: url(\t\t'same-dir.png'\t);
\tbackground: url(\t\t"same-dir.png"\t);
\tbackground: url(./same-dir.png);
\tbackground: url(down/down-dir.png);
\tbackground: url(../up-dir.png);
\tbackground: url(../../up-2-dirs.png);
\tbackground: url(/at-root.png);
\tbackground: url(//other-domain.com/root.png);
\tbackground: url(https://other-domain.com/root.png);
\tbackground: url(data:image/gif;base64,eh129ehiuehjdhsa==);
}
CSS;
        $expected = <<<EXPECTED
.test-it {
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/./same-dir.png");
\tbackground: url("http://example.com/dir1/dir2/down/down-dir.png");
\tbackground: url("http://example.com/dir1/dir2/../up-dir.png");
\tbackground: url("http://example.com/dir1/dir2/../../up-2-dirs.png");
\tbackground: url("http://example.com/at-root.png");
\tbackground: url(//other-domain.com/root.png);
\tbackground: url(https://other-domain.com/root.png);
\tbackground: url(data:image/gif;base64,eh129ehiuehjdhsa==);
}
EXPECTED;
        $result = Jetpack::absolutize_css_urls($css, 'http://example.com/dir1/dir2/style.css');
        $this->assertEquals($expected, $result);
    }