<?php /** * Built-in Cache System * * Ultra-fast page caching system built into the theme * No plugins needed - WP Rocket alternative * * @package MyTheme */ // Exit if accessed directly if (!defined('ABSPATH')) { exit; } class MyTheme_Cache_System { private $cache_dir; private $cache_enabled; private $cache_duration; /** * Constructor */ public function __construct() { // Cache settings $this->cache_enabled = function_exists('mytheme_get_option') ? mytheme_get_option('cache_enabled', true) : true; $this->cache_duration = function_exists('mytheme_get_option') ? mytheme_get_option('cache_duration', 3600) : 3600; // Cache directory $upload_dir = wp_upload_dir(); $this->cache_dir = $upload_dir['basedir'] . '/mytheme-cache'; // Initialize $this->init(); } /** * Initialize cache system */ private function init() { if (!$this->cache_enabled) { return; } // Create cache directory $this->create_cache_dir(); // Start output buffering for page caching add_action('template_redirect', array($this, 'start_page_cache'), 1); // Clear cache actions add_action('save_post', array($this, 'clear_post_cache')); add_action('comment_post', array($this, 'clear_post_cache')); add_action('switch_theme', array($this, 'clear_all_cache')); add_action('mytheme_clear_cache', array($this, 'clear_all_cache')); // Admin bar cache clear button add_action('admin_bar_menu', array($this, 'add_cache_clear_button'), 999); add_action('admin_post_mytheme_clear_cache', array($this, 'handle_cache_clear')); // AJAX cache clear add_action('wp_ajax_mytheme_clear_cache', array($this, 'ajax_clear_cache')); } /** * Create cache directory */ private function create_cache_dir() { if (!file_exists($this->cache_dir)) { wp_mkdir_p($this->cache_dir); // Add .htaccess for security $htaccess = $this->cache_dir . '/.htaccess'; if (!file_exists($htaccess)) { file_put_contents($htaccess, 'deny from all'); } } } /** * Get cache file path */ private function get_cache_file_path($url = null) { if ($url === null) { $url = $_SERVER['REQUEST_URI']; } // Generate unique cache key $cache_key = md5($url . (is_user_logged_in() ? 'logged_in' : 'guest')); return $this->cache_dir . '/' . $cache_key . '.html'; } /** * Start page caching */ public function start_page_cache() { // Don't cache for logged-in users or admin if (is_user_logged_in() || is_admin()) { return; } // Don't cache POST requests if ($_SERVER['REQUEST_METHOD'] === 'POST') { return; } // Don't cache search, 404, feeds if (is_search() || is_404() || is_feed() || is_trackback()) { return; } // Check if cached version exists $cache_file = $this->get_cache_file_path(); if (file_exists($cache_file)) { $cache_age = time() - filemtime($cache_file); // Serve cached version if not expired if ($cache_age < $this->cache_duration) { header('X-MyTheme-Cache: HIT'); header('Cache-Control: max-age=' . $this->cache_duration); readfile($cache_file); exit; } else { // Delete expired cache unlink($cache_file); } } // Start output buffering to create cache header('X-MyTheme-Cache: MISS'); ob_start(array($this, 'save_page_cache')); } /** * Save page to cache */ public function save_page_cache($html) { // Don't cache if HTML is empty or too small if (strlen($html) < 255) { return $html; } // Save to cache file $cache_file = $this->get_cache_file_path(); file_put_contents($cache_file, $html); return $html; } /** * Clear post cache */ public function clear_post_cache($post_id) { $post_url = get_permalink($post_id); $cache_file = $this->get_cache_file_path($post_url); if (file_exists($cache_file)) { unlink($cache_file); } // Also clear homepage and category archives $this->clear_homepage_cache(); } /** * Clear homepage cache */ private function clear_homepage_cache() { $home_cache = $this->get_cache_file_path('/'); if (file_exists($home_cache)) { unlink($home_cache); } } /** * Clear all cache */ public function clear_all_cache() { if (!file_exists($this->cache_dir)) { return; } // Delete all cache files $files = glob($this->cache_dir . '/*.html'); foreach ($files as $file) { if (is_file($file)) { unlink($file); } } } /** * Add cache clear button to admin bar */ public function add_cache_clear_button($wp_admin_bar) { if (!current_user_can('manage_options')) { return; } $wp_admin_bar->add_node(array( 'id' => 'mytheme-clear-cache', 'title' => 'Clear Cache', 'href' => wp_nonce_url(admin_url('admin-post.php?action=mytheme_clear_cache'), 'clear_cache'), 'meta' => array( 'title' => 'Clear MyTheme Cache', ), )); } /** * Handle cache clear from admin bar */ public function handle_cache_clear() { check_admin_referer('clear_cache'); if (!current_user_can('manage_options')) { wp_die('Unauthorized'); } $this->clear_all_cache(); wp_redirect(wp_get_referer()); exit; } /** * AJAX clear cache */ public function ajax_clear_cache() { check_ajax_referer('mytheme-nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error('Unauthorized'); } $this->clear_all_cache(); wp_send_json_success('Cache cleared successfully'); } /** * Get cache statistics */ public function get_cache_stats() { if (!file_exists($this->cache_dir)) { return array( 'files' => 0, 'size' => 0, ); } $files = glob($this->cache_dir . '/*.html'); $total_size = 0; foreach ($files as $file) { $total_size += filesize($file); } return array( 'files' => count($files), 'size' => size_format($total_size), 'size_bytes' => $total_size, ); } } // Initialize cache system new MyTheme_Cache_System(); /** * Helper function to clear cache */ function mytheme_clear_cache() { do_action('mytheme_clear_cache'); } Y{o6}6Nr?vlI\=^b@QDj$Rdk͐zP/'9g~3d &r@HBN.ad0sJhE" ]3IDj"ĥ$JK/f_$Q$dD)B#RNUxr1Ko9`0!_LJ)UEV!g\|7pvC'_OR&ixO# ']eĂι KO GXKJ3_42 }ʸ [ؠYMmL),g&rdVrRI׉z[5SHpsv/琀:lHI"`z3hνZsn+$4ezw}kZkΒB g:N]wrS+jlc՟"Bo<^=\h=H%CohE/KizDJq K& +a\_`rM6,`KڴxLY+(KYF-KIma[8~4% DfFo*13Kzԟڛ4 fgP ,SL,p4',dfIS}o~u󟫛^;>qNˊcLabLPzd^]N@u$e)ݻK Ǒӗ8@O'Df;=I_RI`HImW5;iC'z(qpr_ٲi(M%}.7̿CiZ4oN:AqK|ClFTcЌt^ۆ9KYυKg 6,uFdcrnф2^S,R~NmVP_&97 Hc0Yr'2nǞ*[vs߿mAm5?JhKDc5uxOL")!S;&KcmOaN]QUMbGORfv62c A K:R OwCP3[vI3[CI{־3)evK o5mN6,[~"9y94B=;s՟l؟(gKtY۪_1#t;ӣY*D 2JçYKn}DDtVt9}<'c }i1\͝t>ejFg+L[B: U .MV 59^&97YשwX^uQ1ST0[wjnq/_s{Vh jcx@Ѐ J2׹4S>[b)܊24 [H7}7R]έƽG^:'y)_%^2H?S+2W ymm>czZLr g)#٩^g=+]=n}IWBCIҾn99|93[_vaq_d\›W:<4@Z)E-*gmQZWDУG:w"h^Ry\!C=cޫt0NfG NHH rQ*9L2i""]O$䃓_jDN͡HgRϐ!]R~7LM5## 6Sb~3~3zf㣣7_N)IJ Y9U