Author URI: http://shra.ru Version: 1.1.005 */ class AlphaCacheClass { var $active; var $ac_set; //settings var $timer; //store timer value here public function __construct() { $this->timer = microtime(true); $this->active = true; //Actions add_action('admin_menu', array($this, '_add_menu')); add_action('init', array($this, 'init_hook'), 0); $this->ac_set = get_option('alpha_cache_settings'); if (!is_array($this->ac_set)) $this->ac_set = AlphaCacheClass::default_settings(); //Activity hooks if (!empty($this->ac_set['chTRACK'])) { add_action('delete_post', array($this, 'post_hook')); add_action('post_updated', array($this, 'post_hook')); add_action('wp_set_comment_status', array($this, 'comment_status_hook')); add_action('wp_insert_comment', array($this, 'comment_status_hook')); add_action('trash_comment', array($this, 'comment_status_hook')); add_action('spam_comment', array($this, 'comment_status_hook')); add_action('edit_comment', array($this, 'comment_status_hook')); } //start maintain routine $this->maintain_db(); } /* comment status hook */ public function comment_status_hook($comment_id) { global $wpdb; $comment_id += 0; $post_id = $wpdb->get_var("SELECT comment_post_ID FROM {$wpdb->prefix}comments WHERE comment_ID = {$comment_id}"); $uri = $this->posturi($post_id); $this->delete_cache($uri); } /* site relative uri - get by post_id */ static function posturi($post_id) { $uri = get_permalink($post_id); $a = parse_url($uri); unset($a['scheme'], $a['host'], $a['fragment']); if (!empty($a['query'])) $a['query'] = '?' . $a['query']; return implode('', $a); } /* post hook */ public function post_hook($post_id) { $uri = $this->posturi($post_id); $this->delete_cache($uri); } /* admin_menu hook */ public function _add_menu() { add_options_page('Alpha Cache', 'Alpha Cache', 8, __FILE__, array($this, '_options_page')); } /* output buffer hook */ public function call_back_ob($data) { $this->set_cache($_SERVER['REQUEST_URI'], $data); return $data; } /* init hook */ public function init_hook() { global $user_ID, $user_login; //can do cache? $uri = $_SERVER['REQUEST_URI']; //check URL list $u = explode("\n", $this->ac_set['avoid_urls']); foreach($u as $v) { $v = trim($v); if ($v && preg_match("#{$v}#is", $uri, $m)) { $this->active = false; break; } } //cache for anonymous users only if ($this->active && !empty($this->ac_set['chAnon']) && $user_ID > 0) { $this->active = false; } if ($this->active && !empty($user_login)) { //check users list $u = split("[\s]*,[\s]*", $this->ac_set['users_nocache']); if (in_array($user_login, $u)) { $this->active = false; } } if ($this->active) { /* check post vars */ if ($this->active && !empty($_POST) && !empty($this->ac_set['chPOST'])) { $this->active = false; //allow any kind of form to do what they do } } if ($this->active) { //look to cache if (($data = $this->get_cache($uri)) !== false) { $this->stat_hit(); global $wpdb; echo $data . "\n'; die(); } $this->stat_miss(); //start buffering ob_start(array($this, 'call_back_ob')); } } /* successful hit to cache */ function stat_hit() { if (!empty($this->ac_set['doStat'])) { $this->ac_set['hits'] += 1; update_option('alpha_cache_settings', $this->ac_set); } } /* miss to cache */ function stat_miss() { if (!empty($this->ac_set['doStat'])) { $this->ac_set['miss'] += 1; update_option('alpha_cache_settings', $this->ac_set); } } static function getkey($uri) { return md5($uri); } private function delete_cache($uri) { global $wpdb; $wpdb->query("DELETE FROM {$wpdb->prefix}cache_alpha WHERE debug LIKE '" . mysql_escape_string($uri). "%%'"); } private function get_theme_key() { //do a theme-dependent key static $key = false; if ($key === false) { $obj = wp_get_theme(); $key = md5( $obj->__get('theme_root') . '/' . $obj->__get('stylesheet') ); } return $key; } private function get_cache($uri) { global $wpdb, $user_ID; $key = $this->getkey($uri); $user_ID += 0; //generate different cache for different themes $m = $this->get_theme_key(); $r = $wpdb->get_row("SELECT pagedata FROM {$wpdb->prefix}cache_alpha WHERE themekey = '{$m}' AND pagekey = '{$key}' AND uid = {$user_ID} AND expiretime > " . time()); if ($r === null) { return false; } else { return $r->pagedata; } } private function set_cache($uri, $data) { if (empty($data)) return false; global $wpdb, $user_ID; $key = $this->getkey($uri); $m = $this->get_theme_key(); $wpdb->replace($wpdb->prefix . 'cache_alpha', array('themekey' => $m, 'pagekey' => $key, 'pagedata' => $data, 'uid' => $user_ID + 0, 'debug' => $uri, 'expiretime' => time() + $this->ac_set['cache_lifetime'])); return true; } //clean up cache table and optimize it public function maintain_db() { global $wpdb; $t = time(); if ($this->ac_set['last-maintain'] + $this->ac_set['dbmaintain_period'] < $t) { $wpdb->query("DELETE FROM `{$wpdb->prefix}cache_alpha` WHERE expiretime < $t"); $wpdb->query("OPTIMIZE TABLE `{$wpdb->prefix}cache_alpha`"); $this->ac_set['last-maintain'] = $t; update_option('alpha_cache_settings', $this->ac_set); } } /* Options admin page */ public function _options_page() { global $wpdb; switch ($_POST['action']) { case 'save_cache_settings': //check & store new values unset($_POST['action'], $_POST['sbm']); if ($_POST['cache_lifetime'] < 15) { echo '
' . __("Setting are updated.") . '