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 '
' . __('Maintain period too short. I set minimum - 1 hour.') . '
'; $_POST['dbmaintain_period'] = 3600; } update_option('alpha_cache_settings', $_POST); $this->ac_set = $_POST; echo '

' . __("Setting are updated.") . '

'; break; case 'do_some_actions': //do some actions switch($_POST['commandToDo']) { case 'clear statistics': $this->ac_set['hits'] = 0; $this->ac_set['miss'] = 0; break; case 'clear cache data': $wpdb->query("TRUNCATE TABLE `{$wpdb->prefix}cache_alpha`"); break; case 'load defaults': $new_set = $this->default_settings(); $new_set['hits'] = $this->ac_set['hits']; $new_set['miss'] = $this->ac_set['miss']; $this->ac_set = $new_set; print_r($new_set); break; } update_option('alpha_cache_settings', $this->ac_set); } $acs = $this->ac_set; ?>


PCRE Patterns.')?>






/>

"; $rows = $wpdb->get_results(" SELECT ALPHA.uid, COUNT(*) as NN, US.user_login, US.user_email FROM {$wpdb->prefix}cache_alpha AS ALPHA LEFT JOIN {$wpdb->prefix}users US ON US.ID = ALPHA.uid WHERE expiretime > " . time() . " GROUP BY ALPHA.uid, US.user_login, US.user_email ORDER BY user_login "); echo ""; $total = 0; foreach($rows as $v) { echo ""; $total += $v->NN; } echo "
" . __('User name') . "" . __('Cached pages') . "
" . ($v->uid ? htmlspecialchars($v->user_login) : __('Anonymous')) . "" . $v->NN . "
" . __('Total') . ":$total
"; ?>
/>
/>
/>


query(" DROP TABLE IF EXISTS `cache_alpha`"); $wpdb->query(" DROP TABLE IF EXISTS `{$wpdb->prefix}cache_alpha`"); $wpdb->query(" CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}cache_alpha` ( `pagekey` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `themekey` VARCHAR(32) NOT NULL, `uid` int(11) NOT NULL, `pagedata` mediumtext CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `expiretime` int(11) NOT NULL, `debug` varchar(250) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, PRIMARY KEY (`pagekey`,`uid`,`themekey`), KEY `uid` (`uid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"); //set defaults add_option('alpha_cache_settings', AlphaCacheClass::default_settings() ); } static function default_settings() { return array( 'cache_lifetime' => 3600, 'dbmaintain_period' => 43200, //no cache on admin's pages 'avoid_urls' => '^/wp-admin/ ^/wp-login.php', 'users_nocache' => '', 'chPOST' => 1, 'doStat' => '', 'chTRACK' => 1, 'chAnon' => '', 'last-maintain' => time() ); } /* uninstall hook */ static function uninstall() { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS `{$wpdb->prefix}cache_alpha`"); delete_option('alpha_cache_settings'); } } register_uninstall_hook( __FILE__, array('AlphaCacheClass', 'uninstall')); register_activation_hook( __FILE__, array('AlphaCacheClass', 'install') ); if (class_exists("AlphaCacheClass")) { $alpha_cache_obj = new AlphaCacheClass(); } if (isset($alpha_cache_obj)) { //to do: ; } // if (isset($alpha_cache_obj))