function test_ensure_valid_url_with_null()
 {
     $this->assertEquals('http://', ensure_valid_url(NULL));
     // WTF
 }
Esempio n. 2
0
function imgix_replace_content_cdn($content)
{
    global $imgix_options;
    $slink = ensure_valid_url($imgix_options['cdn_link']);
    $auto_format = $imgix_options['auto_format'];
    $auto_enhance = $imgix_options['auto_enhance'];
    if (!empty($slink)) {
        // 1) Apply imgix host
        // img src tags
        $content = str_replace('src="' . home_url('/') . 'wp-content/', 'src="' . $slink . 'wp-content/', $content);
        $content = str_replace('src=\'' . home_url('/') . 'wp-content/', 'src=\'' . $slink . 'wp-content/', $content);
        // img href tags
        $content = str_replace('href="' . home_url('/') . 'wp-content/', 'href="' . $slink . 'wp-content/', $content);
        $content = str_replace('href=\'' . home_url('/') . 'wp-content/', 'href=\'' . $slink . 'wp-content/', $content);
        $data_w_h = imgix_extract_img_details($content);
        // 2) Handle Auto options
        $autos = array();
        $auto_params = '';
        if ($auto_format === "1") {
            array_push($autos, "format");
        }
        if ($auto_enhance === "1") {
            array_push($autos, "enhance");
        }
        if (sizeof($autos) > 0) {
            $auto_params = 'auto=' . implode(',', $autos);
        }
        // 3) Apply the h/w img params and any that already existed (text html edits)
        foreach ($data_w_h as $k => $v) {
            $extra = strlen($v['extra']) > 0 ? '&' . $v['extra'] : '';
            $to_replace = $v['raw'];
            $new_url = '.' . $v['type'] . '?h=' . $v['h'] . '&w=' . $v['w'] . $extra;
            $content = str_replace($to_replace, $new_url, $content);
        }
        // 4) Apply the auto_params
        $imgs = imgix_extract_imgs($content);
        foreach ($imgs as $k => $v) {
            if (strlen($v['params']) > 0) {
                $new_url = $v['url'] . '?' . $auto_params . '&' . $v['params'];
                $to_replace = $v['url'] . '?' . $v['params'];
            } else {
                $to_replace = $v['url'];
                $new_url = $v['url'] . '?' . $auto_params;
            }
            $to_replace .= '"';
            $new_url .= '"';
            if (strlen($to_replace) > 0) {
                $content = str_replace($to_replace, $new_url, $content);
            }
        }
        return $content;
    }
    return $content;
}
Esempio n. 3
0
/**
 * Finds references to the wordpress site URL in the given string,
 * (optionally prefixed by "src"), and changes them to the imgix URL.
 *
 * @return array An array countaining the final string, and a boolean value
 * indicating if it's different from the given input string.
 */
function replace_host($str, $require_prefix = false)
{
    global $imgix_options;
    $new_host = ensure_valid_url($imgix_options['cdn_link']);
    if (!$new_host) {
        return array($str, false);
    }
    // As soon as srcset is supported…
    //$prefix = $require_prefix? 'srcs?e?t?=[\'"]|,[\S+\n\r\s]*': '';
    $prefix = $require_prefix ? 'src=[\'"]' : '';
    $src = '(' . preg_quote(home_url('/'), '/') . '|\\/\\/)';
    $patt = '/(' . $prefix . ')' . $src . '/i';
    $str = preg_replace($patt, '$1' . $new_host, $str, -1, $count);
    return array($str, (bool) $count);
}