/** * Launch the Robot * * @since 2.6.4 Don't preload localhost & .dev domains * @since 1.0 * * @param string $spider (default: 'cache-preload') The spider name: cache-preload or cache-json * @param string $lang (default: '') The language code to preload * @return void */ function run_rocket_bot($spider = 'cache-preload', $lang = '') { $domain = parse_url(home_url(), PHP_URL_HOST); if ('localhost' == $domain || pathinfo($domain, PATHINFO_EXTENSION) == 'dev') { return false; } /** * Filter to manage the bot job * * @since 2.1 * * @param bool Do the job or not * @param string $spider The spider name * @param string $lang The language code to preload */ if (!apply_filters('do_run_rocket_bot', true, $spider, $lang)) { return false; } $urls = array(); switch ($spider) { case 'cache-preload': if (!$lang) { $urls = get_rocket_i18n_uri(); } else { $urls[] = get_rocket_i18n_home_url($lang); } break; case 'cache-json': $urls[] = WP_ROCKET_URL . 'cache.json'; break; default: return false; break; } foreach ($urls as $start_url) { /** * Fires before WP Rocket Bot is called * * @since 1.1.0 * * @param string $spider The spider name * @param string $start_url URL that crawl by the bot */ do_action('before_run_rocket_bot', $spider, $start_url); wp_remote_get(WP_ROCKET_BOT_URL . '?spider=' . $spider . '&start_url=' . $start_url, array('timeout' => 2, 'blocking' => false, 'sslverify' => false)); /** * Fires after WP Rocket Bot was called * * @since 1.1.0 * * @param string $spider The spider name * @param string $start_url URL that crawl by the bot */ do_action('after_run_rocket_bot', $spider, $start_url); } }
/** * 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; }
/** * 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)); }
/** * 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); } }