preload_user_agent = true; }else{ $this->preload_user_agent = false; } $this->options = $this->getOptions(); $this->set_cdn(); $this->set_cache_file_path(); $this->set_exclude_rules(); $this->set_content_url(); if(isset($this->options->wpFastestCacheDisableEmojis) && $this->options->wpFastestCacheDisableEmojis){ add_action('init', array($this, 'disable_emojis')); } } public function disable_emojis(){ remove_action('wp_head', 'print_emoji_detection_script', 7); remove_action('admin_print_scripts', 'print_emoji_detection_script'); remove_filter('the_content_feed', 'wp_staticize_emoji'); remove_filter('comment_text_rss', 'wp_staticize_emoji'); remove_action('wp_print_styles', 'print_emoji_styles'); remove_action('admin_print_styles', 'print_emoji_styles'); remove_filter('wp_mail', 'wp_staticize_emoji_for_email'); } public function detect_current_page_type(){ if(preg_match("/\?/", $_SERVER["REQUEST_URI"])){ return true; } if(preg_match("/^\/wp-json/", $_SERVER["REQUEST_URI"])){ return true; } if(is_front_page()){ echo ""; }else if(is_category()){ echo ""; }else if(is_tag()){ echo ""; }else if(is_singular('post')){ echo ""; }else if(is_page()){ echo ""; }else if(is_attachment()){ echo ""; } } public function set_exclude_rules(){ if($json_data = get_option("WpFastestCacheExclude")){ $this->exclude_rules = json_decode($json_data); } } public function set_cache_file_path(){ if($this->isMobile() && isset($this->options->wpFastestCacheMobile)){ if(class_exists("WpFcMobileCache") && isset($this->options->wpFastestCacheMobileTheme)){ $wpfc_mobile = new WpFcMobileCache(); $this->cacheFilePath = $this->getWpContentDir()."/cache/".$wpfc_mobile->get_folder_name()."".$_SERVER["REQUEST_URI"]; } }else{ if($this->isPluginActive('gtranslate/gtranslate.php')){ if(isset($_SERVER["HTTP_X_GT_LANG"])){ //$this->cacheFilePath = $this->getWpContentDir()."/cache/all/".$_SERVER["HTTP_X_GT_LANG"]; $this->cacheFilePath = $this->getWpContentDir()."/cache/all/".$_SERVER["HTTP_X_GT_LANG"].$_SERVER["REQUEST_URI"]; }else if(isset($_SERVER["REDIRECT_URL"]) && $_SERVER["REDIRECT_URL"] != "/index.php"){ $this->cacheFilePath = $this->getWpContentDir()."/cache/all/".$_SERVER["REDIRECT_URL"]; }else if(isset($_SERVER["REQUEST_URI"])){ $this->cacheFilePath = $this->getWpContentDir()."/cache/all/".$_SERVER["REQUEST_URI"]; } }else{ $this->cacheFilePath = $this->getWpContentDir()."/cache/all/".$_SERVER["REQUEST_URI"]; } } //WPML language switch //https://wpml.org/forums/topic/wpml-language-switch-wp-fastest-cache-issue/ if($this->isPluginActive('sitepress-multilingual-cms/sitepress.php')){ $current_language = apply_filters('wpml_current_language', false); $this->cacheFilePath = str_replace('/cache/all/', '/cache/all/'.$current_language.'/', $this->cacheFilePath); } $this->cacheFilePath = $this->cacheFilePath ? rtrim($this->cacheFilePath, "/")."/" : ""; $this->cacheFilePath = str_replace("/cache/all//", "/cache/all/", $this->cacheFilePath); if(strlen($_SERVER["REQUEST_URI"]) > 1){ // for the sub-pages if(!preg_match("/\.html/i", $_SERVER["REQUEST_URI"])){ if($this->is_trailing_slash()){ if(!preg_match("/\/$/", $_SERVER["REQUEST_URI"])){ if(defined('WPFC_CACHE_QUERYSTRING') && WPFC_CACHE_QUERYSTRING){ }else if(preg_match("/utm_(source|medium|campaign|content|term)/i", $this->cacheFilePath)){ }else{ $this->cacheFilePath = false; } } }else{ //toDo } } } $this->remove_google_analytics_paramters(); // to decode path if it is not utf-8 if($this->cacheFilePath){ $this->cacheFilePath = urldecode($this->cacheFilePath); } } public function remove_google_analytics_paramters(){ //to remove query strings for cache if google analytics parameters are set if(preg_match("/utm_(source|medium|campaign|content|term)/i", $this->cacheFilePath)){ if(strlen($_SERVER["REQUEST_URI"]) > 1){ // for the sub-pages $this->cacheFilePath = preg_replace("/\/*\?.+/", "", $this->cacheFilePath); $this->cacheFilePath = $this->cacheFilePath."/"; define('WPFC_CACHE_QUERYSTRING', true); } } } public function set_cdn(){ $cdn_values = get_option("WpFastestCacheCDN"); if($cdn_values){ $std_obj = json_decode($cdn_values); $arr = array(); if(is_array($std_obj)){ $arr = $std_obj; }else{ array_push($arr, $std_obj); } foreach ($arr as $key => &$std) { $std->originurl = trim($std->originurl); $std->originurl = trim($std->originurl, "/"); $std->originurl = preg_replace("/http(s?)\:\/\/(www\.)?/i", "", $std->originurl); $std->cdnurl = trim($std->cdnurl); $std->cdnurl = trim($std->cdnurl, "/"); if(!preg_match("/https\:\/\//", $std->cdnurl)){ $std->cdnurl = "//".preg_replace("/http(s?)\:\/\/(www\.)?/i", "", $std->cdnurl); } } $this->cdn = $arr; } } public function checkShortCode($content){ if(preg_match("/\[wpfcNOT\]/", $content)){ if(!is_home() || !is_archive()){ $this->blockCache = true; } $content = str_replace("[wpfcNOT]", "", $content); } return $content; } public function createCache(){ if(isset($this->options->wpFastestCacheStatus)){ // to check logged-in user if(isset($this->options->wpFastestCacheLoggedInUser) && $this->options->wpFastestCacheLoggedInUser == "on"){ foreach ((array)$_COOKIE as $cookie_key => $cookie_value){ if(preg_match("/wordpress_logged_in/i", $cookie_key)){ ob_start(array($this, "cdn_rewrite")); return 0; } } } // to exclude admin users $users_groups = get_users(array("role" => "administrator", "fields" => array("user_login"))); foreach ((array)$_COOKIE as $cookie_key => $cookie_value){ if(preg_match("/wordpress_logged_in/i", $cookie_key)){ foreach ($users_groups as $user_key => $user_value) { if(preg_match("/^".preg_quote($user_value->user_login, "/")."/", $cookie_value)){ ob_start(array($this, "cdn_rewrite")); return 0; } } } } // to check comment author foreach ((array)$_COOKIE as $cookie_key => $cookie_value){ if(preg_match("/comment_author_/i", $cookie_key)){ ob_start(array($this, "cdn_rewrite")); return 0; } } // to check wp_woocommerce_session cookie foreach ((array)$_COOKIE as $cookie_key => $cookie_value){ if(preg_match("/^wp\_woocommerce\_session/", $cookie_key)){ //""; ob_start(array($this, "cdn_rewrite")); return 0; } } if(isset($_COOKIE) && isset($_COOKIE['safirmobilswitcher'])){ ob_start(array($this, "cdn_rewrite")); return 0; } if(preg_match("/\?/", $_SERVER["REQUEST_URI"]) && !preg_match("/\/\?fdx\_switcher\=true/", $_SERVER["REQUEST_URI"])){ // for WP Mobile Edition if(preg_match("/\?amp(\=1)?/i", $_SERVER["REQUEST_URI"])){ // }else if(defined('WPFC_CACHE_QUERYSTRING') && WPFC_CACHE_QUERYSTRING){ // }else{ ob_start(array($this, "cdn_rewrite")); return 0; } } if(preg_match("/(".$this->get_excluded_useragent().")/", $_SERVER['HTTP_USER_AGENT'])){ return 0; } if(isset($_SERVER['REQUEST_URI']) && preg_match("/(\/){2}$/", $_SERVER['REQUEST_URI'])){ return 0; } if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "POST"){ return 0; } if(preg_match("/^https/i", get_option("home")) && !is_ssl()){ //Must be secure connection return 0; } if(!preg_match("/^https/i", get_option("home")) && is_ssl()){ //must be normal connection if(!$this->isPluginActive('really-simple-ssl/rlrsssl-really-simple-ssl.php')){ if(!$this->isPluginActive('really-simple-ssl-pro/really-simple-ssl-pro.php')){ if(!$this->isPluginActive('ssl-insecure-content-fixer/ssl-insecure-content-fixer.php')){ if(!$this->isPluginActive('https-redirection/https-redirection.php')){ return 0; } } } } } if(preg_match("/www\./", get_option("home")) && !preg_match("/www\./", $_SERVER['HTTP_HOST'])){ return 0; } if(!preg_match("/www\./", get_option("home")) && preg_match("/www\./", $_SERVER['HTTP_HOST'])){ return 0; } //different domain names may be used for different languages if($this->isPluginActive('polylang/polylang.php')){ if(!preg_match("/".preg_quote(str_replace("www.", "", $_SERVER["HTTP_HOST"]), "/")."/i", get_option("home"))){ return 0; } } if($this->exclude_page()){ //echo ""."\n"; return 0; } // http://mobiledetect.net/ does not contain the following user-agents if(preg_match("/Nokia309|Casper_VIA/i", $_SERVER['HTTP_USER_AGENT'])){ return 0; } if(preg_match("/Empty\sUser\sAgent/i", $_SERVER['HTTP_USER_AGENT'])){ // not to show the cache for command line return 0; } //to show cache version via php if htaccess rewrite rule does not work if(!$this->preload_user_agent && $this->cacheFilePath && @file_exists($this->cacheFilePath."index.html")){ if($content = @file_get_contents($this->cacheFilePath."index.html")){ if(defined('WPFC_REMOVE_VIA_FOOTER_COMMENT') && WPFC_REMOVE_VIA_FOOTER_COMMENT){ die($content); }else{ $content = $content.""; die($content); } } }else{ if($this->isMobile()){ if(class_exists("WpFcMobileCache") && isset($this->options->wpFastestCacheMobileTheme)){ if(isset($this->options->wpFastestCacheMobileTheme_themename) && $this->options->wpFastestCacheMobileTheme_themename){ $create_cache = true; }else if($this->isPluginActive('wptouch/wptouch.php') || $this->isPluginActive('wptouch-pro/wptouch-pro.php')){ //to check that user-agent exists in wp-touch's list or not if($this->is_wptouch_smartphone()){ $create_cache = true; }else{ $create_cache = false; } }else if($this->isPluginActive('any-mobile-theme-switcher/any-mobile-theme-switcher.php')){ if($this->is_anymobilethemeswitcher_mobile()){ $create_cache = true; }else{ $create_cache = false; } }else{ if((preg_match('/iPhone/', $_SERVER['HTTP_USER_AGENT']) && preg_match('/Mobile/', $_SERVER['HTTP_USER_AGENT'])) || (preg_match('/Android/', $_SERVER['HTTP_USER_AGENT']) && preg_match('/Mobile/', $_SERVER['HTTP_USER_AGENT']))){ $create_cache = true; }else{ $create_cache = false; } } }else{ $create_cache = false; } }else{ $create_cache = true; } if($create_cache){ $this->startTime = microtime(true); add_action('get_footer', array($this, "detect_current_page_type")); add_action('get_footer', array($this, "wp_print_scripts_action")); ob_start(array($this, "callback")); } } } } public function wp_print_scripts_action(){ echo ""; } public function ignored($buffer){ $list = array( "\/wp\-comments\-post\.php", "\/sitemap\.xml", "\/sitemap_index\.xml", "\/wp\-login\.php", "\/robots\.txt", "\/wp\-cron\.php", "\/wp\-content", "\/wp\-admin", "\/wp\-includes", "\/index\.php", "\/xmlrpc\.php", "\/wp\-api\/", "leaflet\-geojson\.php", "\/clientarea\.php" ); if($this->isPluginActive('woocommerce/woocommerce.php')){ if($this->current_page_type != "homepage"){ global $post; if(isset($post->ID) && $post->ID){ if(function_exists("wc_get_page_id")){ $woocommerce_ids = array(); //wc_get_page_id('product') //wc_get_page_id('product-category') array_push($woocommerce_ids, wc_get_page_id('cart'), wc_get_page_id('checkout'), wc_get_page_id('receipt'), wc_get_page_id('confirmation'), wc_get_page_id('myaccount')); if (in_array($post->ID, $woocommerce_ids)) { return true; } } } //"\/product" //"\/product-category" array_push($list, "\/cart", "\/checkout", "\/receipt", "\/confirmation", "\/wc-api\/"); } } if($this->isPluginActive('wp-easycart/wpeasycart.php')){ array_push($list, "\/cart"); } if($this->isPluginActive('easy-digital-downloads/easy-digital-downloads.php')){ array_push($list, "\/cart", "\/checkout"); } if(preg_match("/".implode("|", $list)."/i", $_SERVER["REQUEST_URI"])){ return true; } return false; } public function exclude_page($buffer = false){ $preg_match_rule = ""; $request_url = trim($_SERVER["REQUEST_URI"], "/"); if($this->exclude_rules){ foreach((array)$this->exclude_rules as $key => $value){ $value->type = isset($value->type) ? $value->type : "page"; if($buffer && isset($value->prefix) && $value->prefix && ($value->type == "page")){ $value->content = trim($value->content); $value->content = trim($value->content, "/"); if(preg_match("/^(homepage|category|tag|post|page|attachment)$/", $value->prefix)){ if(preg_match('/<\!--WPFC_PAGE_TYPE_'.$value->prefix.'-->/i', $buffer)){ return true; } }else if($value->prefix == "exact"){ if(strtolower($value->content) == strtolower($request_url)){ return true; } }else{ if($value->prefix == "startwith"){ $preg_match_rule = "^".preg_quote($value->content, "/"); }else if($value->prefix == "contain"){ $preg_match_rule = preg_quote($value->content, "/"); } if(preg_match("/".$preg_match_rule."/i", $request_url)){ return true; } } }else if($value->prefix == "googleanalytics"){ if(preg_match("/utm_(source|medium|campaign|content|term)/i", $request_url)){ return true; } }else if($value->type == "useragent"){ if(preg_match("/".preg_quote($value->content, "/")."/i", $_SERVER['HTTP_USER_AGENT'])){ return true; } }else if($value->type == "cookie"){ if(isset($_SERVER['HTTP_COOKIE'])){ if(preg_match("/".preg_quote($value->content, "/")."/i", $_SERVER['HTTP_COOKIE'])){ return true; } } } } } return false; } public function is_json($buffer){ if(isset($_SERVER["HTTP_ACCEPT"]) && preg_match("/json/i", $_SERVER["HTTP_ACCEPT"])){ return true; } if(preg_match("/^\/wp-json/", $_SERVER["REQUEST_URI"])){ return true; } if(preg_match("/^\s*\{\s*[\"\']/i", $buffer)){ return true; } if(preg_match("/^\s*\[\s*\{\s*[\"\']/i", $buffer)){ return true; } return false; } public function is_xml($buffer){ if(preg_match("/^\s*\<\?xml/i", $buffer)){ return true; } return false; } public function set_current_page_type($buffer){ preg_match('/<\!--WPFC_PAGE_TYPE_([a-z]+)-->/i', $buffer, $out); $this->current_page_type = isset($out[1]) ? $out[1] : false; } public function callback($buffer){ $this->set_current_page_type($buffer); $buffer = $this->checkShortCode($buffer); // for Wordfence: not to cache 503 pages if(defined('DONOTCACHEPAGE') && $this->isPluginActive('wordfence/wordfence.php')){ if(function_exists("http_response_code") && http_response_code() == 503){ return $buffer.""; } } if($this->exclude_page($buffer)){ $buffer = preg_replace('/<\!--WPFC_PAGE_TYPE_[a-z]+-->/i', '', $buffer); return $buffer; } $buffer = preg_replace('/<\!--WPFC_PAGE_TYPE_[a-z]+-->/i', '', $buffer); if(preg_match("/Mediapartners-Google|Google\sWireless\sTranscoder/i", $_SERVER['HTTP_USER_AGENT'])){ return $buffer; }else if($this->is_xml($buffer)){ return $buffer; }else if (is_user_logged_in() || $this->isCommenter()){ return $buffer; }else if($this->is_json($buffer)){ return $buffer; }else if(isset($_COOKIE["wptouch-pro-view"])){ return $buffer.""; }else if($this->isPasswordProtected($buffer)){ return $buffer.""; }else if($this->isWpLogin($buffer)){ return $buffer.""; }else if($this->hasContactForm7WithCaptcha($buffer)){ return $buffer.""; }else if(is_404() || preg_match("/
]*>/i", $buffer)){
preg_match_all("/]*>((?!<\/pre>).)+<\/pre>/is", $buffer, $pre_buffer);
preg_match_all("/]*>((?!<\/pre>).)+<\/pre>/is", $content, $pre_content);
if(isset($pre_content[0]) && isset($pre_content[0][0])){
foreach ($pre_content[0] as $key => $value){
$content = preg_replace("/".preg_quote($value, "/")."/", $pre_buffer[0][$key], $content);
}
}
}
return $content;
}
public function cdn_rewrite($content){
if($this->cdn){
$content = preg_replace_callback("/(srcset|src|href|data-bg-url|data-lazyload|data-source-url|data-srcsmall|data-srclarge|data-srcfull|data-slide-img|data-lazy-original)\s{0,2}\=[\'\"]([^\'\"]+)[\'\"]/i", array($this, 'cdn_replace_urls'), $content);
//url()
$content = preg_replace_callback("/(url)\(([^\)\>]+)\)/i", array($this, 'cdn_replace_urls'), $content);
//{"concatemoji":"http:\/\/your_url.com\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.7"}
$content = preg_replace_callback("/\{\"concatemoji\"\:\"[^\"]+\"\}/i", array($this, 'cdn_replace_urls'), $content);
//
$content = preg_replace_callback("/[\"\']([^\'\"]+)[\"\']\s*\:\s*[\"\']https?\:\\\\\/\\\\\/[^\"\']+[\"\']/i", array($this, 'cdn_replace_urls'), $content);
}
return $content;
}
public function get_header($content){
$head_first_index = strpos($content, "");
return substr($content, $head_first_index, ($head_last_index-$head_first_index + 1));
}
public function minify($content){
$content = preg_replace("/<\/html>\s+/", "", $content);
$content = str_replace("\r", "", $content);
return isset($this->options->wpFastestCacheMinifyHtml) ? preg_replace("/^\s+/m", "", ((string) $content)) : $content;
}
public function checkHtml($buffer){
if(preg_match('/]*>/si', $buffer) && preg_match('/]*>/si', $buffer)){
return false;
}
// if(strlen($buffer) > 10){
// return false;
// }
return true;
}
public function cacheDate($buffer){
if($this->isMobile() && class_exists("WpFcMobileCache")){
$comment = "";
}else{
$comment = "";
}
if(defined('WPFC_REMOVE_FOOTER_COMMENT') && WPFC_REMOVE_FOOTER_COMMENT){
return $buffer;
}else{
return $buffer.$comment;
}
}
public function creationTime(){
return microtime(true) - $this->startTime;
}
public function isCommenter(){
$commenter = wp_get_current_commenter();
return isset($commenter["comment_author_email"]) && $commenter["comment_author_email"] ? true : false;
}
public function isPasswordProtected($buffer){
if(preg_match("/action\=[\'\"].+postpass.*[\'\"]/", $buffer)){
return true;
}
foreach($_COOKIE as $key => $value){
if(preg_match("/wp\-postpass\_/", $key)){
return true;
}
}
return false;
}
public function createFolder($cachFilePath, $buffer, $extension = "html", $prefix = "", $gzip = false){
$create = false;
$update_db_statistic = true;
if($buffer && strlen($buffer) > 100 && $extension == "html"){
if(!preg_match("/^\<\!\-\-\sMobile\:\sWP\sFastest\sCache/i", $buffer)){
if(!preg_match("/^\<\!\-\-\sWP\sFastest\sCache/i", $buffer)){
$create = true;
}
}
if($this->preload_user_agent){
if(file_exists($cachFilePath."/".$prefix."index.".$extension)){
$update_db_statistic = false;
@unlink($cachFilePath."/".$prefix."index.".$extension);
}
}
}
if(($extension == "css" || $extension == "js") && $buffer && strlen($buffer) > 5){
$create = true;
$buffer = trim($buffer);
if($extension == "js"){
if(substr($buffer, -1) != ";"){
$buffer .= ";";
}
}
}
if($create){
if (!is_user_logged_in() && !$this->isCommenter()){
if(!is_dir($cachFilePath)){
if(is_writable($this->getWpContentDir()) || ((is_dir($this->getWpContentDir()."/cache")) && (is_writable($this->getWpContentDir()."/cache")))){
if (@mkdir($cachFilePath, 0755, true)){
file_put_contents($cachFilePath."/".$prefix."index.".$extension, $buffer);
if(class_exists("WpFastestCacheStatics")){
if($update_db_statistic && !preg_match("/After\sCache\sTimeout/i", $_SERVER['HTTP_USER_AGENT'])){
if(preg_match("/wpfc\-mobile\-cache/", $cachFilePath)){
$extension = "mobile";
}
$cache_statics = new WpFastestCacheStatics($extension, strlen($buffer));
$cache_statics->update_db();
}
}
if($extension == "html"){
if(!file_exists(WPFC_WP_CONTENT_DIR."/cache/index.html")){
@file_put_contents(WPFC_WP_CONTENT_DIR."/cache/index.html", "");
}
}else{
if(!file_exists(WPFC_WP_CONTENT_DIR."/cache/wpfc-minified/index.html")){
@file_put_contents(WPFC_WP_CONTENT_DIR."/cache/wpfc-minified/index.html", "");
}
}
}else{
}
}else{
}
}else{
if(file_exists($cachFilePath."/".$prefix."index.".$extension)){
}else{
file_put_contents($cachFilePath."/".$prefix."index.".$extension, $buffer);
if(class_exists("WpFastestCacheStatics")){
if($update_db_statistic && !preg_match("/After\sCache\sTimeout/i", $_SERVER['HTTP_USER_AGENT'])){
if(preg_match("/wpfc\-mobile\-cache/", $cachFilePath)){
$extension = "mobile";
}
$cache_statics = new WpFastestCacheStatics($extension, strlen($buffer));
$cache_statics->update_db();
}
}
}
}
}
}elseif($extension == "html"){
$this->err = "Buffer is empty so the cache cannot be created";
}
}
public function is_amp($content){
$request_uri = trim($_SERVER["REQUEST_URI"], "/");
if(preg_match("/^amp/", $request_uri) || preg_match("/amp$/", $request_uri)){
if(preg_match("/]+amp[^\>]*>/i", $content)){
return true;
}
}
return false;
}
public function isMobile(){
foreach ($this->get_mobile_browsers() as $value) {
if(preg_match("/".$value."/i", $_SERVER['HTTP_USER_AGENT'])){
return true;
}
}
foreach ($this->get_operating_systems() as $key => $value) {
if(preg_match("/".$value."/i", $_SERVER['HTTP_USER_AGENT'])){
return true;
}
}
}
public function isWpLogin($buffer){
// if(preg_match("/