/** * 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 all languages subdomains URLs * * @since 2.1 * * @return array $urls List of languages subdomains URLs */ function get_rocket_i18n_subdomains() { if (!rocket_has_i18n()) { return false; } $urls = array(); if (rocket_is_plugin_active('sitepress-multilingual-cms/sitepress.php')) { $option = get_option('icl_sitepress_settings'); if ((int) $option['language_negotiation_type'] == 2) { $urls = get_rocket_i18n_uri(); } } elseif (rocket_is_plugin_active('qtranslate/qtranslate.php')) { if ((int) $GLOBALS['q_config']['url_mode'] == 3) { $urls = get_rocket_i18n_uri(); } } elseif (rocket_is_plugin_active('polylang/polylang.php')) { if ((int) $GLOBALS['polylang']->options['force_lang'] == 2) { $urls = get_rocket_i18n_uri(); } } return $urls; }
/** * Get URI all of active languages * * @since 2.0 * @deprecated 2.2 * @deprecated Use get_rocket_i18n_uri() * */ function get_rocket_all_active_langs_uri() { _deprecated_function(__FUNCTION__, '2.2', 'get_rocket_i18n_uri()'); return get_rocket_i18n_uri(); }
/** * 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); } }
/** * Used to minify and concat JavaScript files * * @since 1.1.0 Fix Bug with externals URLs like //ajax.google.com * @since 1.0.2 Remove the filter, remove the array_chunk, add an automatic way to cut strings to 255c max * @since 1.0 */ function rocket_minify_js($buffer) { $home_host = parse_url(home_url(), PHP_URL_HOST); $internal_files = array(); $external_tags = array(); $excluded_tags = ''; $excluded_js = implode('|', get_rocket_exclude_js()); $excluded_js = str_replace('//' . $home_host, '', $excluded_js); $js_in_footer = get_rocket_minify_js_in_footer(); $wp_content_dirname = ltrim(str_replace(home_url(), '', WP_CONTENT_URL), '/') . '/'; $excluded_external_js = get_rocket_minify_excluded_external_js(); // Get all JS files with this regex preg_match_all(apply_filters('rocket_minify_js_regex_pattern', '#<script[^>]+?src=[\'|"]([^\'|"]+\\.js?)[\'|"]?.*>(?:<\\/script>)#i'), $buffer, $tags_match); $i = 0; foreach ($tags_match[0] as $tag) { // Chek if the file is already minify by get_rocket_minify_files // or the file is rejected to the process if (!strpos($tag, 'data-minify=') && !strpos($tag, 'data-no-minify=')) { // To check if a tag is to exclude of the minify process $excluded_tag = false; // Get JS URL with scheme $js_url_with_scheme = set_url_scheme($tags_match[1][$i]); // Get URL infos $js_url = parse_url($js_url_with_scheme); // Get host for all langs $langs_host = array(); if ($langs = get_rocket_i18n_uri()) { foreach ($langs as $lang) { $langs_host[] = parse_url($lang, PHP_URL_HOST); } } // Get host of CNAMES $cnames_host = get_rocket_cnames_host(array('all', 'css_and_js', 'js')); // Check if the link isn't external // Insert the relative path to the array without query string if (isset($js_url['host']) && ($js_url['host'] == $home_host || in_array($js_url['host'], $cnames_host) || in_array($js_url['host'], $langs_host)) || !isset($js_url['host']) && preg_match('#(' . $wp_content_dirname . '|wp-includes)#', $js_url['path'])) { // Check if it isn't a file to exclude if (!preg_match('#^(' . $excluded_js . ')$#', $js_url['path']) && pathinfo($js_url['path'], PATHINFO_EXTENSION) == 'js') { $internal_files[] = $js_url['path']; } else { $excluded_tag = true; } // If it's an excluded external file } else { if (isset($js_url['host']) && in_array($js_url['host'], $excluded_external_js)) { $excluded_tag = true; // If it's an external file } else { if (!in_array($tags_match[1][$i], $js_in_footer) && !in_array($js_url_with_scheme, $js_in_footer)) { $external_tags[] = $tag; } } } // Remove the tag if (!$excluded_tag) { $buffer = str_replace($tag, '', $buffer); } } $i++; } // Get external JS tags and remove duplicate scripts $external_tags = implode('', array_unique($external_tags)); // Exclude JS files to insert in footer foreach ($internal_files as $k => $url) { if (in_array(set_url_scheme('//' . $home_host . $url), $js_in_footer) || in_array($url, $js_in_footer)) { unset($internal_files[$k]); } } // Insert the minify JS file return array($buffer, $external_tags . get_rocket_minify_files($internal_files)); }