/** * Make API request * * @param string $url * @return string */ function _request($url) { $request_url = Util_Environment::url_format(W3TC_PAGESPEED_API_URL, array('url' => $url, 'key' => $this->key)); $response = Util_Http::get($request_url, array('timeout' => 120)); if (!is_wp_error($response) && $response['response']['code'] == 200) { return $response['body']; } return false; }
/** * Check * * @throws Util_WpFile_FilesystemOperationException * @throws FileOperationException */ public function get_ip_ranges() { $data = array(); $response = Util_Http::get('https://www.cloudflare.com/ips-v4'); if (!is_wp_error($response)) { $ip4_data = $response['body']; $ip4_data = explode("\n", $ip4_data); $data['ip4'] = $ip4_data; } $response = Util_Http::get('https://www.cloudflare.com/ips-v6'); if (!is_wp_error($response)) { $ip6_data = $response['body']; $ip6_data = explode("\n", $ip6_data); $data['ip6'] = $ip6_data; } return $data; }
/** * Makes get requests to url specific to a post, its permalink * * @param unknown $post_id * @return boolean returns true on success */ public function prime_post($post_id) { $post_urls = Util_PageUrls::get_post_urls($post_id); // Make HTTP requests and prime cache foreach ($post_urls as $url) { $result = Util_Http::get($url, array('user-agent' => '')); if (is_wp_error($result)) { return false; } } return true; }
/** * Perform rewrite test * * @param string $url * @return boolean */ private function test_rewrite($url) { $key = sprintf('w3tc_rewrite_test_%s', substr(md5($url), 0, 16)); $result = get_site_transient($key); if ($result != 'ok') { $response = Util_Http::get($url); $is_ok = !is_wp_error($response) && $response['response']['code'] == 200 && trim($response['body']) == 'OK'; if ($is_ok) { $result = 'ok'; } else { $result = is_wp_error($response) ? $response->get_error_message() : implode(' ', $response['response']); } set_site_transient($key, $result, 30); } return $result; }
/** * Returns theme recommendations * * @param string $theme_name * @return array */ function get_theme_recommendations($theme_name) { $urls = $this->get_theme_urls($theme_name); $js_groups = array(); $css_groups = array(); @set_time_limit($this->_config->get_integer('timelimit.minify_recommendations')); foreach ($urls as $template => $url) { /** * Append theme identifier */ $url .= (strstr($url, '?') !== false ? '&' : '?') . 'w3tc_theme=' . urlencode($theme_name); /** * If preview mode enabled append w3tc_preview */ if ($this->_config->is_preview()) { $url .= '&w3tc_preview=1'; } /** * Get page contents */ $response = Util_Http::get($url); if (!is_wp_error($response) && ($response['response']['code'] == 200 || $response['response']['code'] == 404 && $template == '404')) { $js_files = $this->get_recommendations_js($response['body']); $css_files = $this->get_recommendations_css($response['body']); $js_groups[$template] = $js_files; $css_groups[$template] = $css_files; } } $js_groups = $this->get_theme_recommendations_by_groups($js_groups); $css_groups = $this->get_theme_recommendations_by_groups($css_groups); $recommendations = array($js_groups, $css_groups); return $recommendations; }
/** * Show plugin changes * * @return void */ function in_plugin_update_message() { $response = Util_Http::get(W3TC_README_URL); if (is_wp_error($response) || $response['response']['code'] != 200) { return; } $matches = null; $regexp = '~==\\s*Changelog\\s*==\\s*=\\s*[0-9.]+\\s*=(.*)(=\\s*' . preg_quote(W3TC_VERSION) . '\\s*=|$)~Uis'; $body = $response['body']; if (!preg_match($regexp, $body, $matches)) { return; } $changelog = (array) preg_split('~[\\r\\n]+~', trim($matches[1])); echo '<div style="color: #f00;">' . __('Take a minute to update, here\'s why:', 'w3-total-cache') . '</div><div style="font-weight: normal;height:300px;overflow:auto">'; $ul = false; foreach ($changelog as $index => $line) { if (preg_match('~^\\s*\\*\\s*~', $line)) { if (!$ul) { echo '<ul style="list-style: disc; margin-left: 20px;margin-top:0;">'; $ul = true; } $line = preg_replace('~^\\s*\\*\\s*~', '', htmlspecialchars($line)); echo '<li style="width: 50%; margin: 0; float: left; ' . ($index % 2 == 0 ? 'clear: left;' : '') . '">' . $line . '</li>'; } else { if ($ul) { echo '</ul><div style="clear: left;"></div>'; $ul = false; } } } if ($ul) { echo '</ul><div style="clear: left;"></div>'; } echo '</div>'; }
/** * Exports min files to CDN * * @return array */ function get_files_minify() { $files = array(); if ($this->_config->get_boolean('minify.rewrite') && Util_Rule::can_check_rules() && (!$this->_config->get_boolean('minify.auto') || Cdn_Util::is_engine_mirror($this->_config->get_string('cdn.engine')))) { $minify = Dispatcher::component('Minify_Plugin'); $document_root = Util_Environment::document_root(); $minify_root = Util_Environment::cache_blog_dir('minify'); $minify_path = ltrim(str_replace($document_root, '', $minify_root), '/'); $urls = $minify->get_urls(); // in WPMU + network admin (this code used for minify manual only) // common minify files are stored under context of main blog (i.e. 1) // but have urls of 0 blog, so download has to be used if ($this->_config->get_string('minify.engine') == 'file' && !(Util_Environment::is_wpmu() && is_network_admin())) { foreach ($urls as $url) { Util_Http::get($url); } $files = Cdn_Util::search_files($minify_root, $minify_path, '*.css;*.js'); } else { foreach ($urls as $url) { $file = Util_Environment::normalize_file_minify($url); $file = Util_Environment::translate_file($file); if (!Util_Environment::is_url($file)) { $file = $document_root . '/' . $file; $file = ltrim(str_replace($minify_root, '', $file), '/'); $dir = dirname($file); if ($dir) { Util_File::mkdir($dir, 0777, $minify_root); } if (Util_Http::download($url, $minify_root . '/' . $file) !== false) { $files[] = $minify_path . '/' . $file; } } } } } return $files; }