/** * Makes external event request * * @param string $type * @param string $value * @return array */ function external_event($type, $value) { $url = sprintf('%s?u=%s&tkn=%s&evnt_t=%s&evnt_v=%s', W3TC_CLOUDFLARE_EXTERNAL_EVENT_URL, urlencode($this->_config['email']), urlencode($this->_config['key']), urlencode($type), urlencode($value)); $response = w3_http_get($url, '', false); if ($response) { return json_decode($response); } return false; }
/** * Makes external event request * * @param string $type * @param string $value * @return array */ function external_event($type, $value) { require_once W3TC_INC_DIR . '/functions/http.php'; $url = sprintf('%s?u=%s&tkn=%s&evnt_t=%s&evnt_v=%s', W3TC_CLOUDFLARE_EXTERNAL_EVENT_URL, urlencode($this->_config['email']), urlencode($this->_config['key']), urlencode($type), urlencode($value)); $response = w3_http_get($url); if (!is_wp_error($response)) { return json_decode($response['body']); } return false; }
/** * Downloads URL into a file * * @param string $url * @param string $file * @return boolean */ function w3_download($url, $file) { if (strpos($url, '//') === 0) { $url = (w3_is_https() ? 'https:' : 'http:') . $url; } $response = w3_http_get($url); if (!is_wp_error($response) && $response['response']['code'] == 200) { return @file_put_contents($file, $response['body']); } return false; }
/** * Make API request * * @param string $url * @return string */ function _request($url) { require_once W3TC_INC_DIR . '/functions/http.php'; require_once W3TC_INC_DIR . '/functions/url.php'; $request_url = w3_url_format(W3TC_PAGESPEED_API_URL, array('url' => $url, 'key' => $this->key)); $response = w3_http_get($request_url); if (!is_wp_error($response) && $response['response']['code'] == 200) { return $response['body']; } return false; }
/** * 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_transient($key); if ($result === false) { w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/http.php'); $response = w3_http_get($url); $result = !is_wp_error($response) && $response['response']['code'] == 200 && trim($response['body']) == 'OK'; if ($result) { set_transient($key, $result, 30); } else { $key_result = sprintf('w3tc_rewrite_test_result_%s', substr(md5($url), 0, 16)); set_transient($key_result, is_wp_error($response) ? $response->get_error_message() : implode(' ', $response['response']), 30); } } return $result; }
/** * Parses sitemap * * @param string $url * @return array */ function parse_sitemap($url) { require_once W3TC_INC_DIR . '/functions/http.php'; $urls = array(); $response = w3_http_get($url); if (!is_wp_error($response) && $response['response']['code'] == 200) { $url_matches = null; $sitemap_matches = null; if (preg_match_all('~<sitemap>(.*?)</sitemap>~is', $response['body'], $sitemap_matches)) { $loc_matches = null; foreach ($sitemap_matches[1] as $sitemap_match) { if (preg_match('~<loc>(.*?)</loc>~is', $sitemap_match, $loc_matches)) { $loc = trim($loc_matches[1]); if ($loc) { $urls = array_merge($urls, $this->parse_sitemap($loc)); } } } } elseif (preg_match_all('~<url>(.*?)</url>~is', $response['body'], $url_matches)) { $locs = array(); $loc_matches = null; $priority_matches = null; foreach ($url_matches[1] as $url_match) { $loc = ''; $priority = 0; if (preg_match('~<loc>(.*?)</loc>~is', $url_match, $loc_matches)) { $loc = trim($loc_matches[1]); } if (preg_match('~<priority>(.*?)</priority>~is', $url_match, $priority_matches)) { $priority = (double) trim($priority_matches[1]); } if ($loc && $priority) { $locs[$loc] = $priority; } } arsort($locs); $urls = array_keys($locs); } } return $urls; }
/** * Downloads data to a file * * @param string $url * @param string $file * @return boolean */ function w3_download($url, $file) { if (strpos($url, '//') === 0) { $url = (w3_is_https() ? 'https:' : 'http:') . $url; } $data = w3_http_get($url); if ($data !== false) { return @file_put_contents($file, $data); } return false; }
/** * Make API request * * @param string $url * @return string */ function _request($url) { $request_url = w3_url_format(W3TC_PAGESPEED_API_URL, array('url' => $url, 'key' => $this->key)); $json = w3_http_get($request_url); return $json; }
/** * Check * @throws FilesystemOperationException * @throws FileOperationException */ public function update_ip_ranges() { w3_require_once(W3TC_INC_DIR . '/functions/http.php'); $ip4_diff = $ip6_diff = false; $response = w3_http_get(W3TC_CLOUDFLARE_IP4_URL); $extensions_settings = $this->_config->get_array('extensions.settings', array()); if (!is_wp_error($response)) { $ip4_data = $response['body']; $ip4_data = explode("\n", $ip4_data); $ip4_data_old = w3tc_get_extension_config('cloudflare', 'ips.ip4', $this->_config, array()); if ($ip4_diff = array_diff($ip4_data, $ip4_data_old)) { $extensions_settings['cloudflare']['ips.ip4'] = $ip4_data; $this->_config->set('extensions.settings', $extensions_settings); } } $response = w3_http_get(W3TC_CLOUDFLARE_IP6_URL); if (!is_wp_error($response)) { $ip6_data = $response['body']; $ip6_data = explode("\n", $ip6_data); $ip6_data_old = w3tc_get_extension_config('cloudflare', 'ips.ip6', $this->_config, array()); if ($ip6_diff = array_diff($ip6_data, $ip6_data_old)) { $extensions_settings['cloudflare']['ips.ip6'] = $ip6_data; $this->_config->set('extensions.settings', $extensions_settings); } } if ($ip4_diff || $ip6_diff) { try { $this->_config->save(); $this->_config->refresh_cache(); } catch (Exception $ex) { } } }
/** * Makes get requests to url specific to a post, its permalink * @param $post_id * @return boolean returns true on success */ public function prime_post($post_id) { /** @var $purges W3_PageUrls */ $purges = w3_instance('W3_PageUrls'); $post_urls = $purges->get_post_urls($post_id); foreach ($post_urls as $url) { $result = w3_http_get($url); if (is_wp_error($result)) { return false; } } return true; }
/** * Exports min files to CDN * * @return array */ function get_files_minify() { $files = array(); if (W3TC_PHP5 && $this->_config->get_boolean('minify.rewrite') && (!$this->_config->get_boolean('minify.auto') || w3_is_cdn_mirror($this->_config->get_string('cdn.engine')))) { require_once W3TC_INC_DIR . '/functions/http.php'; $minify =& w3_instance('W3_Plugin_Minify'); $document_root = w3_get_document_root(); $site_root = w3_get_site_root(); $minify_root = w3_path(W3TC_CACHE_FILE_MINIFY_DIR); $minify_path = ltrim(str_replace($site_root, rtrim(w3_get_site_path(), '/'), $minify_root), '/'); $urls = $minify->get_urls(); if ($this->_config->get_string('minify.engine') == 'file') { foreach ($urls as $url) { w3_http_get($url); } $files = $this->search_files($minify_root, $minify_path, '*.css;*.js'); } else { foreach ($urls as $url) { $file = w3_normalize_file_minify($url); $file = w3_translate_file($file); if (!w3_is_url($file)) { $file = $document_root . '/' . $file; $file = ltrim(str_replace($minify_root, '', $file), '/'); $dir = dirname($file); if ($dir) { w3_mkdir($dir, 0777, $minify_root); } if (w3_download($url, $minify_root . '/' . $file) !== false) { $files[] = $minify_path . '/' . $file; } } } } } return $files; }
/** * 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 (w3_is_preview_config()) { $url .= '&w3tc_preview=1'; } /** * Get page contents * Don't check response code for 404 template */ $content = w3_http_get($url, null, $template != '404'); if ($content) { $js_files = $this->get_recommendations_js($content); $css_files = $this->get_recommendations_css($content); $js_groups[$template] = $js_files; $css_groups[$template] = $css_files; } } $js_groups = $this->_get_theme_recommendations($js_groups); $css_groups = $this->_get_theme_recommendations($css_groups); $recommendations = array($js_groups, $css_groups); return $recommendations; }
/** * Makes get requests to url specific to a post, its permalink * @param $post_id * @return boolean returns true on success */ public function prime_post($post_id) { /** @var $purges W3_SharedPageUrls */ $purges = w3_instance('W3_SharedPageUrls'); $post_urls = $purges->get_post_urls($post_id); /** * Make HTTP requests and prime cache */ w3_require_once(W3TC_INC_DIR . '/functions/http.php'); w3_require_once(W3TC_INC_DIR . '/functions/url.php'); foreach ($post_urls as $url) { $result = w3_http_get($url, array('user-agent' => '')); if (is_wp_error($result)) { return false; } } return true; }
/** * Check * @throws FilesystemOperationException * @throws FileOperationException */ public function update_ip_ranges() { w3_require_once(W3TC_INC_DIR . '/functions/http.php'); $ip4_diff = $ip6_diff = false; $config_master = new W3_Config(true); $response = w3_http_get(W3TC_CLOUDFLARE_IP4_URL); if (!is_wp_error($response)) { $ip4_data = $response['body']; $ip4_data = explode("\n", $ip4_data); $ip4_data_old = $this->_config->get_array('cloudflare.ips.ip4'); if ($ip4_diff = array_diff($ip4_data, $ip4_data_old)) { $config_master->set('cloudflare.ips.ip4', $ip4_data); } } $response = w3_http_get(W3TC_CLOUDFLARE_IP6_URL); if (!is_wp_error($response)) { $ip6_data = $response['body']; $ip6_data = explode("\n", $ip6_data); $ip6_data_old = $this->_config->get_array('cloudflare.ips.ip6'); if ($ip6_diff = array_diff($ip6_data, $ip6_data_old)) { $config_master->set('cloudflare.ips.ip6', $ip6_data); } } if ($ip4_diff || $ip6_diff) { $config_master->save(); } }
/** * Perform rewrite test * * @param string $url * @return boolean */ function test_rewrite($url) { $key = sprintf('w3tc_rewrite_test_%s', substr(md5($url), 0, 16)); $result = get_transient($key); if ($result === false) { $response = w3_http_get($url); $result = !is_nxt_error($response) && $response['response']['code'] == 200 && trim($response['body']) == 'OK'; if ($result) { set_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 = w3_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() { w3_require_once(W3TC_INC_DIR . '/functions/http.php'); $response = w3_http_get(W3TC_README_URL); if (!is_wp_error($response) && $response['response']['code'] == 200) { w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/admin_ui.php'); w3_in_plugin_update_message($response['body']); } }
/** * Parses sitemap * * @param string $xml * @return array */ function parse_sitemap($url) { $urls = array(); $xml = w3_http_get($url); if ($xml) { $url_matches = null; $sitemap_matches = null; if (preg_match_all('~<sitemap>(.*?)</sitemap>~is', $xml, $sitemap_matches)) { $loc_matches = null; foreach ($sitemap_matches[1] as $sitemap_match) { if (preg_match('~<loc>(.*?)</loc>~is', $sitemap_match, $loc_matches)) { $loc = trim($loc_matches[1]); if ($loc) { $urls = array_merge($urls, $this->parse_sitemap($loc)); } } } } elseif (preg_match_all('~<url>(.*?)</url>~is', $xml, $url_matches)) { $locs = array(); $loc_matches = null; $priority_matches = null; foreach ($url_matches[1] as $url_match) { $loc = ''; $priority = 0; if (preg_match('~<loc>(.*?)</loc>~is', $url_match, $loc_matches)) { $loc = trim($loc_matches[1]); } if (preg_match('~<priority>(.*?)</priority>~is', $url_match, $priority_matches)) { $priority = (double) trim($priority_matches[1]); } if ($loc && $priority) { $locs[$loc] = $priority; } } arsort($locs); $urls = array_keys($locs); } } return $urls; }