Example #1
0
function get_rocket_cdn_url($url, $zone = array('all'))
{
    $cnames = get_rocket_cdn_cnames($zone);
    $wp_content_dirname = ltrim(str_replace(home_url(), '', WP_CONTENT_URL), '/') . '/';
    if (defined('DONOTCDN') && DONOTCDN || (int) get_rocket_option('cdn') == 0 || empty($cnames) || !is_rocket_cdn_on_ssl() || is_rocket_post_excluded_option('cdn')) {
        return $url;
    }
    list($host, $path, $scheme, $query) = get_rocket_parse_url($url);
    $query = !empty($query) ? '?' . $query : '';
    // Exclude rejected & external files from CDN
    $rejected_files = get_rocket_cdn_reject_files();
    if (!empty($rejected_files) && preg_match('#(' . $rejected_files . ')#', $path) || !empty($scheme) && $host != parse_url(home_url(), PHP_URL_HOST)) {
        return $url;
    }
    if (empty($scheme)) {
        // Check if the URL is external
        if (strpos($path, $home) === false && !preg_match('#(' . $wp_content_dirname . '|wp-includes)#', $path)) {
            return $url;
        } else {
            $path = str_replace($home, '', ltrim($path, '//'));
        }
    }
    $url = rtrim($cnames[abs(crc32($path)) % count($cnames)], '/') . '/' . ltrim($path, '/') . $query;
    $url = rocket_add_url_protocol($url);
    return $url;
}
Example #2
0
function __rocket_insert_minify_js_in_footer()
{
    global $pagenow;
    if (get_rocket_option('minify_js') && !in_array($pagenow, array('wp-login.php', 'wp-register.php')) && (!defined('DONOTMINIFYJS') || !DONOTMINIFYJS) && (!defined('DONOTCACHEPAGE') || !DONOTCACHEPAGE) && !is_rocket_post_excluded_option('minify_js') && !is_404()) {
        // Don't apply for logged users if the option is turned off.
        if (is_user_logged_in() && !get_rocket_option('cache_logged_user')) {
            return;
        }
        /** This filter is documented in inc/front/process.php */
        $rocket_cache_search = apply_filters('rocket_cache_search', false);
        // Don't apply on search page
        if (is_search() && !$rocket_cache_search) {
            return;
        }
        // Don't apply on excluded pages.
        if (in_array($_SERVER['REQUEST_URI'], get_rocket_option('cache_reject_uri', array()))) {
            return;
        }
        global $rocket_enqueue_js_in_footer;
        $home_host = parse_url(home_url(), PHP_URL_HOST);
        $files = get_rocket_minify_js_in_footer();
        $ordered_files = array();
        // Get host of CNAMES
        $cnames_host = get_rocket_cnames_host(array('all', 'css_and_js', 'js'));
        $i = 0;
        foreach ($files as $file) {
            list($file_host, $file_path) = get_rocket_parse_url($file);
            // Check if its an external file
            if ($home_host != $file_host && !in_array($file_host, $cnames_host) && !in_array($file_path, $rocket_enqueue_js_in_footer)) {
                if (isset($ordered_files[$i])) {
                    $i++;
                    $ordered_files[$i++] = $file;
                } else {
                    $ordered_files[$i++] = $file;
                }
            } else {
                $ordered_files[$i][] = $file;
            }
        }
        // Print tags
        foreach ($ordered_files as $files) {
            // Check if its an external file
            if (is_string($files)) {
                echo '<script src="' . $files . '" data-minify="1"></script>';
            } else {
                echo get_rocket_minify_files($files);
            }
        }
    }
}
Example #3
0
/**
 * Get directories paths to preserve languages ​​when purging a domain
 * This function is required when the domains of languages (​​other than the default) are managed by subdirectories
 * By default, when you clear the cache of the french website with the domain example.com, all subdirectory like /en/ and /de/ are deleted.
 * But, if you have a domain for your english and german websites with example.com/en/ and example.com/de/, you want to keep the /en/ and /de/ directory when the french domain is cleared.
 *
 * @since 2.0
 *
 * @param string $current_lang The current language code
 * @return array $langs_to_preserve List of directories path to preserve
 */
function get_rocket_i18n_to_preserve($current_lang)
{
    $langs_to_preserve = array();
    if (!rocket_has_i18n()) {
        return $langs_to_preserve;
    }
    $langs = get_rocket_i18n_code();
    // Unset current lang to the preserve dirs
    $langs = array_flip($langs);
    if (isset($langs[$current_lang])) {
        unset($langs[$current_lang]);
    }
    $langs = array_flip($langs);
    // Stock all URLs of langs to preserve
    foreach ($langs as $lang) {
        list($host, $path) = get_rocket_parse_url(get_rocket_i18n_home_url($lang));
        $langs_to_preserve[] = WP_ROCKET_CACHE_PATH . $host . '(.*)/' . trim($path, '/');
    }
    /**
     * Filter directories path to preserve of cache purge
     *
     * @since 2.1
     *
     * @param array $langs_to_preserve List of directories path to preserve
     */
    $langs_to_preserve = apply_filters('rocket_langs_to_preserve', $langs_to_preserve);
    return $langs_to_preserve;
}
Example #4
0
 /**
  * Extract and return host, path and scheme for a specific lang
  *
  * @since 2.0
  * @deprecated 2.2
  * @deprecated Use get_rocket_parse_url()
  *
  */
 function get_rocket_parse_url_for_lang($lang)
 {
     _deprecated_function(__FUNCTION__, '2.2', 'get_rocket_parse_url()');
     return get_rocket_parse_url(get_rocket_i18n_home_url($lang));
 }
Example #5
0
/**
 * Remove all cache files of the domain
 *
 * @since 2.0 Delete domain cache files for all users
 * @since 1.0
 *
 * @param string $lang (default: '') The language code
 * @return void
 */
function rocket_clean_domain($lang = '')
{
    $urls = !$lang ? get_rocket_i18n_uri() : get_rocket_i18n_home_url($lang);
    $urls = (array) $urls;
    /**
     * Filter URLs to delete all caching files from a domain
     *
     * @since 2.6.4
     * @param array 	URLs that will be returned
     * @param string 	The language code
     */
    $urls = apply_filters('rocket_clean_domain_urls', $urls, $lang);
    $urls = array_filter($urls);
    foreach ($urls as $url) {
        list($host, $path) = get_rocket_parse_url($url);
        /** This filter is documented in inc/front/htaccess.php */
        if (apply_filters('rocket_url_no_dots', false)) {
            $host = str_replace('.', '_', $host);
        }
        $root = WP_ROCKET_CACHE_PATH . $host . '*' . $path;
        /**
         * Fires before all cache files are deleted
         *
         * @since 1.0
         *
         * @param string $root The path of home cache file
         * @param string $lang The current lang to purge
         */
        do_action('before_rocket_clean_domain', $root, $lang);
        // Delete cache domain files
        if ($dirs = glob($root . '*', GLOB_NOSORT)) {
            foreach ($dirs as $dir) {
                rocket_rrmdir($dir, get_rocket_i18n_to_preserve($lang));
            }
        }
        /**
         * Fires after all cache files was deleted
         *
         * @since 1.0
         *
         * @param string $root The path of home cache file
         * @param string $lang The current lang to purge
         */
        do_action('after_rocket_clean_domain', $root, $lang);
    }
}
Example #6
0
function rocket_cdn_enqueue($src)
{
    // Don't use CDN if in admin, in login page, in register page or in a post preview
    if (is_admin() || is_preview() || in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
        return $src;
    }
    $src = set_url_scheme($src);
    $zone = array('all', 'css_and_js');
    // Add only CSS zone
    if (current_filter() == 'style_loader_src') {
        $zone[] = 'css';
    }
    // Add only JS zone
    if (current_filter() == 'script_loader_src') {
        $zone[] = 'js';
    }
    if ($cnames = get_rocket_cdn_cnames($zone)) {
        list($src_host, $src_path) = get_rocket_parse_url($src);
        // Check if the link isn't external
        if ($src_host == parse_url(home_url(), PHP_URL_HOST) && trim($src_path, '/') != '') {
            $src = get_rocket_cdn_url($src, $zone);
        }
    }
    return $src;
}
Example #7
0
function rocket_clean_files_users($urls)
{
    $pattern_urls = array();
    foreach ($urls as $url) {
        list($host, $path, $scheme) = get_rocket_parse_url($url);
        $pattern_urls[] = $scheme . '://' . $host . '*' . $path;
    }
    return $pattern_urls;
}