<?php
namespace SpeedyCache;
if(!defined('ABSPATH')){
die('HACKING ATTEMPT!');
}
use \SpeedyCache\Util;
class Cache {
static $cache_file_path = '';
static $ignored_parameters = ['fbclid', 'utm_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_source_platform', 'gclid', 'dclid', 'msclkid', 'ref', 'fbaction_ids', 'fbc', 'fbp', 'clid', 'mc_cid', 'mc_eid', 'hsCtaTracking', 'hsa_cam', 'hsa_grp', 'hsa_mt', 'hsa_src', 'hsa_ad', 'hsa_acc', 'hsa_net', 'hsa_kw'];
static function init(){
global $speedycache;
if(!defined('SPEEDYCACHE_SERVER_HOST')){
define('SPEEDYCACHE_SERVER_HOST', Util::sanitize_server('HTTP_HOST'));
}
// Remove all the WP Bloat
if(class_exists('\SpeedyCache\Bloat') && !empty($speedycache->bloat)){
\SpeedyCache\Bloat::actions();
}
if(!empty($speedycache->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(!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', 1);
}
}
// 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(&$content){
$cache_path = self::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);
file_put_contents($cache_path, $content . "\n<!-- ".esc_html($mobile)."Cache by SpeedyCache https://speedycache.com -->");
delete_option('speedycache_html_size');
delete_option('speedycache_assets_size');
}
static function cache_file_name(){
$file_name = 'index';
if(isset($_COOKIE['wcu_current_currency'])){
$file_name .= '-'. strtolower(sanitize_file_name($_COOKIE['wcu_current_currency']));
}
return $file_name . '.html';
}
static function cache_path(){
global $speedycache;
if(!file_exists(SPEEDYCACHE_CACHE_DIR)){
mkdir(SPEEDYCACHE_CACHE_DIR, 0755, true);
}
$host = $_SERVER['HTTP_HOST'];
$request_uri = urldecode(esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])));
$request_uri = preg_replace('/\.{2,}/', '', $request_uri); // Cleaning the path
$request_uri = remove_query_arg(self::$ignored_parameters, $request_uri); // Cleaning ignored query
$parsed_uri = wp_parse_url($request_uri);
$path = SPEEDYCACHE_CACHE_DIR;
$path .= '/' . $host;
if(wp_is_mobile() && !empty($speedycache->options['mobile_theme'])){
$path .= '/mobile-cache';
} else {
$path .= '/all';
}
// Handling WeGlot
if(function_exists('weglot_get_current_full_url')){
$weglot_url = weglot_get_current_full_url();
$weglot_path = parse_url($weglot_url, PHP_URL_PATH);
$path .= $weglot_path;
} else {
$path .= $parsed_uri['path'];
}
self::$cache_file_path = $path;
return $path;
}
static function can_cache(&$content){
global $speedycache;
if(empty($speedycache->options['status'])) return false;
if(empty($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] != 'GET') return false;
if(defined('WP_CLI') && !empty(WP_CLI)) return false;
if(defined('REST_REQUEST') && !empty(REST_REQUEST)) return false;
if(function_exists('http_response_code') && (http_response_code() > 309)) return false;
if(preg_match('/\./', $_SERVER['REQUEST_URI'])) return false;
if(preg_match('/(wp-(?:admin|login|register|comments-post|cron|json))/', $_SERVER['REQUEST_URI'])) return false;
if(preg_match('/html.*\s(amp|⚡)/', substr($content, 0, 300))) return false;
if(wp_is_mobile() && !empty($speedycache->options['mobile']) && empty($speedycache->options['mobile_theme'])) return false;
if(is_admin()) return false;
// Since: 1.2.8 we will only cache the page if user is not logged-in.
if(is_user_logged_in()) return false;
if(!preg_match( '/<\s*\/\s*html\s*>/i', $content)) return false;
if(is_singular() && post_password_required()) return false;
if(function_exists('is_404') && is_404()) return false;
if(self::is_excluded()) return false;
if(function_exists('is_cart') && is_cart()) return false;
if(function_exists('is_checkout') && is_checkout()) return false;
if(function_exists('is_account_page') && is_account_page()) return false;
if(!self::can_handle_query()) return false;
return true;
}
static function optimize($content){
global $speedycache;
$start_time = microtime(TRUE);
if(!self::can_cache($content)){
return $content;
}
self::clean_html($content);
// Minify HTML
if(class_exists('\SpeedyCache\Enhanced') && !empty($speedycache->options['minify_html']) && (defined('SPEEDYCACHE_PRO_VERSION') && version_compare(SPEEDYCACHE_PRO_VERSION, '1.2.0', '>='))){
\SpeedyCache\Enhanced::init();
\SpeedyCache\Enhanced::minify_html($content);
}
// ADD Font Rendering CSS
if(!empty($speedycache->options['font_rendering'])){
$content = str_replace('</head>', '<style>body{text-rendering: optimizeSpeed;}</style></head>', $content);
}
// Lazy Load HTML elements
if(class_exists('\SpeedyCache\Enhanced') && !empty($speedycache->options['lazy_load_html']) && !empty($speedycache->options['lazy_load_html_elements'])){
$content = \SpeedyCache\Enhanced::lazy_load_html($content);
}
if(!empty($speedycache->options['combine_css'])){
\SpeedyCache\CSS::combine($content);
}
if(!empty($speedycache->options['minify_css'])){
\SpeedyCache\CSS::minify($content);
}
if(!empty($speedycache->options['combine_js'])){
\SpeedyCache\JS::combine_head($content);
}
// if(class_exists('\SpeedyCache\Enhanced') && !empty($speedycache->options['combine_js'])){
// \SpeedyCache\JS::combine_body($content);
// }
if(!empty($speedycache->options['minify_js'])){
\SpeedyCache\JS::minify($content);
}
// Adds Image dimensions to the Image which does not have height or width
if(class_exists('\SpeedyCache\Enhanced') && !empty($speedycache->options['image_dimensions'])){
$content = \SpeedyCache\Enhanced::image_dimensions($content);
}
// Google Fonts
if(class_exists('\SpeedyCache\GoogleFonts') && !empty($speedycache->options['local_gfonts'])){
\SpeedyCache\GoogleFonts::get($content);
$content = \SpeedyCache\GoogleFonts::replace($content);
$content = \SpeedyCache\GoogleFonts::add_swap($content);
}
// Preload Critical Images
if(class_exists('\SpeedyCache\Enhanced') && !empty($speedycache->options['critical_images'])){
$content = \SpeedyCache\Enhanced::preload_critical_images($content);
}
// Delay JS
if(!empty($speedycache->options['delay_js']) && class_exists('\SpeedyCache\ProOptimizations')){
\SpeedyCache\ProOptimizations::delay_js($content);
}
// Defer JS
if(!empty($speedycache->options['render_blocking']) && class_exists('\SpeedyCache\ProOptimizations')){
\SpeedyCache\ProOptimizations::defer_js($content);
}
// IMG Lazy Load
if(class_exists('\SpeedyCache\ProOptimizations') && !empty($speedycache->options['lazy_load'])){
\SpeedyCache\ProOptimizations::img_lazy_load($content);
}
// For other plugins to hook into.
$content = (string) apply_filters('speedycache_content', $content);
// ----- DO NOT DO ANY OPTIMIZATION BELOW THIS ------
// Unused and Critical CSS
if(
!empty($_SERVER['HTTP_HOST']) &&
!empty($_SERVER['REQUEST_URI']) &&
!empty($_SERVER['HTTP_USER_AGENT']) &&
class_exists('\SpeedyCache\ProOptimizations') &&
speedycache_optserver('HTTP_USER_AGENT') !== 'SpeedyCacheCCSS'
){
$post_meta = get_post_meta(get_the_ID(), 'speedycache_post_meta', true);
if(!empty($speedycache->options['critical_css']) && empty($post_meta['disable_critical_css'])){
\SpeedyCache\ProOptimizations::critical_css();
}
if(empty($post_meta['disable_unused_css']) && !empty($speedycache->options['unused_css'])){
\SpeedyCache\ProOptimizations::unused_css();
}
}
// Rewriting to a CDN
if(
!empty($speedycache->cdn) &&
!empty($speedycache->cdn['enabled']) &&
!empty($speedycache->cdn['cdn_url']) &&
!empty($speedycache->cdn['cdn_type']) &&
$speedycache->cdn['cdn_type'] !== 'cloudflare'
){
\SpeedyCache\CDN::rewrite($content);
}
self::create($content);
$end_time = microtime(TRUE);
$content .= '<!-- Cached by SpeedyCache, it took '.($end_time - $start_time).'s-->';
$content .= '<!-- Refresh to see the cached version -->';
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 $content;
}
static function clean_html(&$content){
$content = str_replace("\r\n", "\n", trim($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);
case 'cookie':
$is_excluded = self::is_cookie_excluded($rule);
}
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'] === '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'], '/'));
}
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 (bool) 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;
}
// Deppricated since 1.2.0 do not use it
// Just to prevent site 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]);
}
}