options['dns_prefetch']) && !empty($speedycache->options['dns_urls'])){ add_filter('wp_resource_hints', '\SpeedyCache\Cache::dns_prefetch_hint', 10, 2); } } // Filter for Gravatar cache. We are updating the URL of the gravatar here so the local hosted Gravatar URL will be cached. if(!empty($speedycache->options['gravatar_cache'])){ add_filter('get_avatar_data', '\SpeedyCache\Gravatar::get_avatar_data', 10, 2); } // Loads Instant Page to improve load page speed by 1% if(defined('SPEEDYCACHE_PRO') && !empty($speedycache->options['instant_page'])){ add_action('wp_enqueue_scripts', '\SpeedyCache\Cache::instant_page'); } if(!empty($speedycache->options['disable_emojis'])){ add_action('init', '\SpeedyCache\Cache::disable_emojis'); } // Optimizes images when a page gets loaded and it finds no image optimized if(class_exists('\SpeedyCache\Image') && !empty($speedycache->image['settings']['automatic_optm'])){ add_filter('the_content', '\SpeedyCache\Image::optimize_on_fly'); } // Adds preconnect if(class_exists('\SpeedyCache\Enhanced')){ if(!defined('SITEPAD')){ if(!empty($speedycache->options['pre_connect']) && !empty($speedycache->options['pre_connect_list'])){ add_filter('wp_resource_hints', '\SpeedyCache\Enhanced::pre_connect_hint', 10, 2); } } // Adds Preload link tag to the head if(!empty($speedycache->options['preload_resources'])){ add_action('wp_head', '\SpeedyCache\Enhanced::preload_resource', 0); } } // Image URL rewrite if(class_exists('\SpeedyCache\Image') && !empty($speedycache->image['settings']['url_rewrite'])){ add_filter('the_content', 'SpeedyCache\Image::rewrite_url_to_webp', 10); } ob_start('\SpeedyCache\Cache::optimize'); } static function create(){ global $speedycache; $cache_path = self::cache_path(); $cache_path = wp_normalize_path($cache_path); if(!file_exists($cache_path)){ mkdir($cache_path, 0755, true); } $cache_path .= '/' . self::cache_file_name(); $mobile = ''; if(strpos($cache_path, 'mobile-cache') !== FALSE){ $mobile = 'Mobile: '; } $cache_path = wp_normalize_path($cache_path); $comment = 'Cache by SpeedyCache https://speedycache.com at '.time().' -->'; file_put_contents($cache_path, self::$content . "\n'; self::$content .= ''; if(file_exists(self::$cache_file_path)){ header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime(self::$cache_file_path)) . ' GMT'); } return self::$content; } static function clean_html(){ self::$content = str_replace("\r\n", "\n", trim(self::$content)); } static function is_excluded(){ global $speedycache; $excludes = get_option('speedycache_exclude', []); if(empty($excludes)){ return false; } $is_excluded = false; foreach($excludes as $rule){ switch($rule['type']){ case 'page': $is_excluded = self::is_page_excluded($rule); break; case 'useragent': $is_excluded = self::is_useragent_excluded($rule); break; case 'cookie': $is_excluded = self::is_cookie_excluded($rule); break; } if(!empty($is_excluded)){ return true; } } return false; } static function can_handle_query(){ $uri = sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])); $uri = remove_query_arg(self::$ignored_parameters, $uri); $parsed_uri = wp_parse_url($uri); if(!empty($parsed_uri['query'])){ return false; } return true; } static function is_page_excluded($rule){ if(empty($rule['prefix'])){ return false; } if($rule['prefix'] === 'homepage'){ return is_front_page(); } if($rule['prefix'] === 'page'){ return is_page(); } if($rule['prefix'] === 'post_id' && !empty($rule['content'])){ $excluded_ids = is_array($rule['content']) ? $rule['content'] : explode(',', $rule['content']); return in_array(get_queried_object_id(), $excluded_ids); } // Excludes a page if it has the given shortcode. if($rule['prefix'] === 'shortcode' && !empty($rule['content'])){ if(self::has_shortcode($rule['content'])){ return true; } } if($rule['prefix'] === 'category'){ return is_category(); } if($rule['prefix'] === 'archive'){ return is_archive(); } if($rule['prefix'] === 'tag'){ return is_tag(); } if($rule['prefix'] === 'attachment'){ return is_attachment(); } if($rule['prefix'] === 'startwith' && !empty($rule['content'])){ return (bool) preg_match('/^'.preg_quote($rule['content'], '/').'/', trim($_SERVER['REQUEST_URI'], '/')); } if($rule['prefix'] === 'contain' && !empty($rule['content'])){ return (bool) preg_match('/'.preg_quote($rule['content'], '/').'/', trim($_SERVER['REQUEST_URI'], '/')); } if($rule['prefix'] === 'exact' && !empty($rule['content'])){ return trim($rule['content'], '/') === trim($_SERVER['REQUEST_URI'], '/'); } return false; } static function is_cookie_excluded($rule){ if(!isset($_SERVER['HTTP_COOKIE'])){ return false; } $cookie = sanitize_text_field(wp_unslash($_SERVER['HTTP_COOKIE'])); return preg_match('/'.preg_quote($rule['content'], '/').'/i', $cookie); } static function is_useragent_excluded($rule){ return preg_match('/'.preg_quote($rule['content'], '/').'/i', $_SERVER['HTTP_USER_AGENT']); } // Adds DNS prefetch static function dns_prefetch_hint($urls, $relation_type){ global $speedycache; if($relation_type !== 'dns-prefetch'){ return $urls; } foreach($speedycache->options['dns_urls'] as $url) { if(!empty($url)){ $urls[] = $url; } } return $urls; } // Depricated since 1.2.0 do not use it // Just to prevent site from breaking static function create_dir($path, $content, $type = ''){ } static function disable_emojis(){ add_filter('emoji_svg_url', '__return_false'); remove_action('wp_head', 'print_emoji_detection_script', 7); remove_action('admin_print_scripts', 'print_emoji_detection_script'); remove_action('wp_print_styles', 'print_emoji_styles'); remove_action('admin_print_styles', 'print_emoji_styles'); remove_filter('the_content_feed', 'wp_staticize_emoji'); remove_filter('comment_text_rss', 'wp_staticize_emoji'); remove_filter('wp_mail', 'wp_staticize_emoji_for_email'); } static function instant_page(){ wp_enqueue_script('speedycache_instant_page', SPEEDYCACHE_PRO_URL . '/assets/js/instantpage.js', array(), SPEEDYCACHE_PRO_VERSION, ['strategy' => 'defer', 'in_footer' => true]); } /* * @param string $shortcode shortcode tag name. * @return bool. */ static function has_shortcode($shortcode){ global $post; return \has_shortcode($post->post_content, $shortcode); } /* * Earlier we were using post_password_required which returns false if the user has placed correct password * making the password protected page, visible to all if once correct user opened the page. * * Since 1.3.8 * * @return bool */ static function is_password_protected(){ global $post; if(empty($post)){ return false; } if(!empty($post->post_password)){ return true; } return false; } }