options = get_option('aio_options', []); $upload_dir = wp_upload_dir(); $this->cache_dir = $upload_dir['basedir'] . '/aio-cache/'; // Check if WP Rocket or LiteSpeed is active $conflict = defined('WP_ROCKET_VERSION') || defined('LSCACHE_V') || class_exists('LiteSpeed_Cache'); $this->is_caching_active = !empty($this->options['optEnablePageCache']) && !$conflict; if ( $this->is_caching_active ) { // Serve cache extremely early add_action('plugins_loaded', [$this, 'serve_cache'], 1); // Generate cache add_action('template_redirect', [$this, 'start_buffer'], -9999); // Purge triggers add_action('save_post', [$this, 'clear_all_cache']); add_action('deleted_post', [$this, 'clear_all_cache']); add_action('switch_theme', [$this, 'clear_all_cache']); add_action('wp_update_nav_menu', [$this, 'clear_all_cache']); add_action('woocommerce_new_order', [$this, 'clear_all_cache']); } // Preload & Prefetch add_action('wp_head', [$this, 'output_preload_tags'], 1); // AJAX to clear cache add_action('wp_ajax_aio_clear_page_cache', [$this, 'ajax_clear_cache']); } private function is_logged_in() { foreach ( $_COOKIE as $key => $value ) { if ( strpos( $key, 'wordpress_logged_in_' ) === 0 ) { return true; } } return false; } private function has_woo_items_in_cart() { foreach ( $_COOKIE as $key => $value ) { if ( strpos( $key, 'woocommerce_items_in_cart' ) === 0 || strpos( $key, 'wp_woocommerce_session_' ) === 0 ) { return true; } } return false; } private function get_cache_filepath() { $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; // Remove query strings for file path $uri = strtok($uri, '?'); $path = $this->cache_dir . $host . $uri; if (substr($path, -1) !== '/') { $path .= '/'; } $path .= 'index.html'; return $path; } public function serve_cache() { if ( $_SERVER['REQUEST_METHOD'] !== 'GET' ) return; if ( !empty($_GET) ) return; if ( $this->is_logged_in() ) return; if ( $this->has_woo_items_in_cart() ) return; $cache_file = $this->get_cache_filepath(); if ( file_exists($cache_file) ) { // Optional: Check expiration (e.g., 24 hours) if ( time() - filemtime($cache_file) > 86400 ) { @unlink($cache_file); return; } // Serve the file header('Content-Type: text/html; charset=UTF-8'); header('X-AIO-Cache: HIT'); readfile($cache_file); exit; } } public function start_buffer() { if ( $_SERVER['REQUEST_METHOD'] !== 'GET' ) return; if ( !empty($_GET) ) return; if ( is_user_logged_in() ) return; if ( is_404() || is_search() || is_feed() || is_trackback() ) return; // WooCommerce specific checks if ( function_exists('is_cart') && (is_cart() || is_checkout() || is_account_page()) ) return; if ( $this->has_woo_items_in_cart() ) return; ob_start([$this, 'cache_callback']); } public function cache_callback($buffer) { if ( empty($buffer) || strlen($buffer) < 255 ) return $buffer; // Don't cache if it's an error page or doesn't end with html if ( stripos($buffer, '') === false ) return $buffer; $cache_file = $this->get_cache_filepath(); $dir = dirname($cache_file); if ( !file_exists($dir) ) { wp_mkdir_p($dir); } // Append signature $signature = "\n"; @file_put_contents($cache_file, $buffer . $signature); header('X-AIO-Cache: MISS'); return $buffer; } public function clear_all_cache() { if ( !file_exists($this->cache_dir) ) return; $this->delete_dir($this->cache_dir); } private function delete_dir($dirPath) { if (! is_dir($dirPath)) { return; } if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') { $dirPath .= '/'; } $files = glob($dirPath . '*', GLOB_MARK); foreach ($files as $file) { if (is_dir($file)) { $this->delete_dir($file); } else { @unlink($file); } } @rmdir($dirPath); } public function ajax_clear_cache() { check_ajax_referer('aio_settings_nonce', 'security'); if ( !current_user_can('manage_options') ) { wp_send_json_error('Unauthorized'); } $this->clear_all_cache(); wp_send_json_success(__('Page cache cleared successfully!', 'aio-tools')); } public function output_preload_tags() { // Prefetch Domains if ( !empty($this->options['optPrefetchDomains']) ) { $domains = explode("\n", str_replace("\r", "", $this->options['optPrefetchDomains'])); foreach ( $domains as $domain ) { $domain = trim($domain); if ( !empty($domain) ) { echo '' . "\n"; echo '' . "\n"; } } } // Preload Assets if ( !empty($this->options['optPreloadAssets']) ) { $assets = explode("\n", str_replace("\r", "", $this->options['optPreloadAssets'])); foreach ( $assets as $asset ) { $asset = trim($asset); if ( !empty($asset) ) { $as = 'fetch'; if ( strpos($asset, '.woff') !== false || strpos($asset, '.ttf') !== false ) { $as = 'font'; echo '' . "\n"; } elseif ( strpos($asset, '.css') !== false ) { $as = 'style'; echo '' . "\n"; } elseif ( strpos($asset, '.js') !== false ) { $as = 'script'; echo '' . "\n"; } elseif ( strpos($asset, '.jpg') !== false || strpos($asset, '.png') !== false || strpos($asset, '.webp') !== false ) { $as = 'image'; echo '' . "\n"; } else { echo '' . "\n"; } } } } } }