212 lines
5.4 KiB
PHP
212 lines
5.4 KiB
PHP
<?php
|
|
/**
|
|
* Cache Manager Class
|
|
*
|
|
* Handles page caching and cache management
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class OptiCore_Cache_Manager {
|
|
|
|
private static $instance = null;
|
|
private $cache_dir;
|
|
private $cache_duration = 3600; // 1 hour
|
|
|
|
public static function get_instance() {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private function __construct() {
|
|
$this->cache_dir = WP_CONTENT_DIR . '/cache/opticore';
|
|
}
|
|
|
|
public function init() {
|
|
// Create cache directory if not exists
|
|
if (!file_exists($this->cache_dir)) {
|
|
wp_mkdir_p($this->cache_dir);
|
|
}
|
|
|
|
// Only cache for non-logged-in users
|
|
if (!is_user_logged_in() && !is_admin()) {
|
|
add_action('template_redirect', array($this, 'serve_cached_page'), 1);
|
|
add_action('shutdown', array($this, 'cache_page'), 999);
|
|
add_action('wp_footer', array($this, 'add_cache_comment'), 999);
|
|
}
|
|
|
|
// Clear cache on post update
|
|
add_action('save_post', array($this, 'clear_page_cache'));
|
|
add_action('comment_post', array($this, 'clear_page_cache'));
|
|
}
|
|
|
|
/**
|
|
* Add cache comment to footer
|
|
*/
|
|
public function add_cache_comment() {
|
|
if ($this->should_skip_cache()) {
|
|
return;
|
|
}
|
|
|
|
echo "\n" . '<!-- OptiCore Cache Active: Page will be cached on first visit -->' . "\n";
|
|
}
|
|
|
|
/**
|
|
* Get cache key for current request
|
|
*/
|
|
private function get_cache_key() {
|
|
$url = $_SERVER['REQUEST_URI'];
|
|
$device = wp_is_mobile() ? 'mobile' : 'desktop';
|
|
return md5($url . $device);
|
|
}
|
|
|
|
/**
|
|
* Get cache file path
|
|
*/
|
|
private function get_cache_file() {
|
|
$key = $this->get_cache_key();
|
|
return $this->cache_dir . '/' . $key . '.html';
|
|
}
|
|
|
|
/**
|
|
* Serve cached page if available
|
|
*/
|
|
public function serve_cached_page() {
|
|
// Skip cache for certain conditions
|
|
if ($this->should_skip_cache()) {
|
|
return;
|
|
}
|
|
|
|
$cache_file = $this->get_cache_file();
|
|
|
|
if (file_exists($cache_file)) {
|
|
$cache_time = filemtime($cache_file);
|
|
|
|
// Check if cache is still valid
|
|
if ((time() - $cache_time) < $this->cache_duration) {
|
|
$content = file_get_contents($cache_file);
|
|
echo $content;
|
|
echo '<!-- Cached by OptiCore at ' . date('Y-m-d H:i:s', $cache_time) . ' -->';
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cache the current page
|
|
*/
|
|
public function cache_page() {
|
|
if ($this->should_skip_cache()) {
|
|
return;
|
|
}
|
|
|
|
// Get buffer content
|
|
$content = '';
|
|
$levels = ob_get_level();
|
|
|
|
for ($i = 0; $i < $levels; $i++) {
|
|
$content = ob_get_contents();
|
|
if (!empty($content)) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (empty($content) || strlen($content) < 255) {
|
|
return;
|
|
}
|
|
|
|
$cache_file = $this->get_cache_file();
|
|
|
|
// Create directory if it doesn't exist
|
|
if (!file_exists($this->cache_dir)) {
|
|
wp_mkdir_p($this->cache_dir);
|
|
}
|
|
|
|
// Add cache comment
|
|
$content .= "\n" . '<!-- Cached by OptiCore at ' . date('Y-m-d H:i:s') . ' -->';
|
|
|
|
// Save cache file
|
|
file_put_contents($cache_file, $content);
|
|
|
|
// Also output the comment
|
|
echo "\n" . '<!-- Cached by OptiCore at ' . date('Y-m-d H:i:s') . ' -->';
|
|
}
|
|
|
|
/**
|
|
* Check if we should skip caching
|
|
*/
|
|
private function should_skip_cache() {
|
|
// Skip for logged-in users
|
|
if (is_user_logged_in()) {
|
|
return true;
|
|
}
|
|
|
|
// Skip for admin area
|
|
if (is_admin()) {
|
|
return true;
|
|
}
|
|
|
|
// Skip for POST requests
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
return true;
|
|
}
|
|
|
|
// Skip for AJAX requests
|
|
if (defined('DOING_AJAX') && DOING_AJAX) {
|
|
return true;
|
|
}
|
|
|
|
// Skip for certain query strings
|
|
if (!empty($_GET) && !isset($_GET['utm_source'])) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Clear page cache
|
|
*/
|
|
public function clear_page_cache($post_id = null) {
|
|
if (!file_exists($this->cache_dir)) {
|
|
return;
|
|
}
|
|
|
|
$files = glob($this->cache_dir . '/*.html');
|
|
foreach ($files as $file) {
|
|
if (is_file($file)) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get cache statistics
|
|
*/
|
|
public function get_cache_stats() {
|
|
$stats = array(
|
|
'total_files' => 0,
|
|
'total_size' => 0,
|
|
);
|
|
|
|
if (!file_exists($this->cache_dir)) {
|
|
return $stats;
|
|
}
|
|
|
|
$files = glob($this->cache_dir . '/*.html');
|
|
$stats['total_files'] = count($files);
|
|
|
|
foreach ($files as $file) {
|
|
if (is_file($file)) {
|
|
$stats['total_size'] += filesize($file);
|
|
}
|
|
}
|
|
|
|
return $stats;
|
|
}
|
|
}
|
|
|