enable the cache. Based on WP-Cache by Ricardo Galli Granada.
Version: 0.1
Author: Donncha O Caoimh
Author URI: http://ocaoimh.ie/
*/
/* Copyright 2005-2006 Ricardo Galli Granada (email : gallir@uib.es)
Some code copyright 2007 Donncha O Caoimh (http://ocaoimh.ie/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
$wp_cache_config_file = ABSPATH . 'wp-content/wp-cache-config.php';
if( !@include($wp_cache_config_file) ) {
get_wpcachehome();
$wp_cache_config_file_sample = WPCACHEHOME . 'wp-cache-config-sample.php';
@include($wp_cache_config_file_sample);
} else {
get_wpcachehome();
}
$wp_cache_config_file_sample = WPCACHEHOME . 'wp-cache-config-sample.php';
$wp_cache_link = ABSPATH . 'wp-content/advanced-cache.php';
$wp_cache_file = WPCACHEHOME . 'wp-cache-phase1.php';
include(WPCACHEHOME . 'wp-cache-base.php');
function get_wpcachehome() {
if( defined( 'WPCACHEHOME' ) == false ) {
if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
define( 'WPCACHEHOME', trailingslashit( dirname(__FILE__) ) );
} elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
define( 'WPCACHEHOME', dirname(__FILE__) . '/wp-super-cache/' );
} else {
die( 'Please create wp-content/wp-cache-config.php from wp-super-cache/wp-cache-config-sample.php' );
}
}
}
function wp_cache_add_pages() {
if( function_exists( 'is_site_admin' ) )
if( !is_site_admin() )
return;
add_options_page('WP Super Cache Manager', 'WP Super Cache', 'administrator', __FILE__, 'wp_cache_manager');
}
function wp_cache_manager() {
global $wp_cache_config_file, $valid_nonce, $supercachedir, $cache_path, $cache_enabled, $cache_compression, $super_cache_enabled;
if( function_exists( 'is_site_admin' ) )
if( !is_site_admin() )
return;
$supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
if( get_option( 'gzipcompression' ) == 1 )
update_option( 'gzipcompression', 0 );
$valid_nonce = wp_verify_nonce($_REQUEST['_wpnonce'], 'wp-cache');
echo '
';
echo "
WP Super Cache Manager
\n";
if(isset($_REQUEST['wp_restore_config']) && $valid_nonce) {
unlink($wp_cache_config_file);
echo 'Configuration file changed, some values might be wrong. Load the page again from the "Options" menu to reset them.';
}
echo '
Add here strings (not a filename) that forces a page not to be cached. For example, if your URLs include year and you dont want to cache last year posts, it's enough to specify the year, i.e. '/2004/'. WP-Cache will search if that string is part of the URI and if so, it will no cache that page.
Add here those filenames that can be cached, even if they match one of the rejected substring specified above.
\n";
echo '\n";
}
function wp_cache_enable() {
global $wp_cache_config_file, $cache_enabled, $supercachedir;
if(get_settings('gzipcompression')) {
echo "Error: GZIP compression is enabled, disable it if you want to enable wp-cache.
";
return false;
}
if( wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = true;', $wp_cache_config_file) ) {
$cache_enabled = true;
}
wp_super_cache_enable();
}
function wp_cache_disable() {
global $wp_cache_config_file, $cache_enabled, $supercachedir, $cache_path;
if (wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = false;', $wp_cache_config_file)) {
$cache_enabled = false;
}
wp_super_cache_disable();
}
function wp_super_cache_enable() {
global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
if( is_dir( $supercachedir . ".disabled" ) )
rename( $supercachedir . ".disabled", $supercachedir );
wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = true;', $wp_cache_config_file);
$super_cache_enabled = true;
}
function wp_super_cache_disable() {
global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
if( is_dir( $supercachedir ) )
rename( $supercachedir, $supercachedir . ".disabled" );
wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = false;', $wp_cache_config_file);
$super_cache_enabled = false;
}
function wp_cache_is_enabled() {
global $wp_cache_config_file;
if(get_settings('gzipcompression')) {
echo "Warning: GZIP compression is enabled in Wordpress, wp-cache will be bypassed until you disable gzip compression. ";
return false;
}
$lines = file($wp_cache_config_file);
foreach($lines as $line) {
if (preg_match('/^ *\$cache_enabled *= *true *;/', $line))
return true;
}
return false;
}
function wp_cache_replace_line($old, $new, $my_file) {
if (!is_writable($my_file)) {
echo "Error: file $my_file is not writable. \n";
return false;
}
$found = false;
$lines = file($my_file);
foreach($lines as $line) {
if ( preg_match("/$old/", $line)) {
$found = true;
break;
}
}
if ($found) {
$fd = fopen($my_file, 'w');
foreach($lines as $line) {
if ( !preg_match("/$old/", $line))
fputs($fd, $line);
else {
fputs($fd, "$new //Added by WP-Cache Manager\n");
}
}
fclose($fd);
return true;
}
$fd = fopen($my_file, 'w');
$done = false;
foreach($lines as $line) {
if ( $done || !preg_match('/^define|\$|\?>/', $line))
fputs($fd, $line);
else {
fputs($fd, "$new //Added by WP-Cache Manager\n");
fputs($fd, $line);
$done = true;
}
}
fclose($fd);
return true;
/*
copy($my_file, $my_file . "-prev");
rename($my_file . '-new', $my_file);
*/
}
function wp_cache_verify_cache_dir() {
global $cache_path;
$dir = dirname($cache_path);
if ( !file_exists($cache_path) ) {
if ( !is_writable( $dir ) || !($dir = mkdir( $cache_path ) ) ) {
echo "Error: Your cache directory ($cache_path) did not exist and couldn't be created by the web server. Check $dir permissions.";
return false;
}
}
if ( !is_writable($cache_path)) {
echo "Error: Your cache directory ($cache_path) or $dir need to be writable for this plugin to work. Double-check it.";
return false;
}
if ( '/' != substr($cache_path, -1)) {
$cache_path .= '/';
}
@mkdir( $cache_path . 'meta/' );
return true;
}
function wp_cache_verify_config_file() {
global $wp_cache_config_file, $wp_cache_config_file_sample;
$new = false;
$dir = dirname($wp_cache_config_file);
if ( !is_writable($dir)) {
echo "Error: wp-content directory ($dir) is not writable by the Web server. Check its permissions.";
return false;
}
if ( !file_exists($wp_cache_config_file) ) {
if ( !file_exists($wp_cache_config_file_sample) ) {
echo "Error: Sample WP-Cache config file ($wp_cache_config_file_sample) does not exist. Verify you installation.";
return false;
}
copy($wp_cache_config_file_sample, $wp_cache_config_file);
if( is_file( dirname(__FILE__) . '/wp-cache-config-sample.php' ) ) {
wp_cache_replace_line('WPCACHEHOME', "define( 'WPCACHEHOME', " . str_replace( ABSPATH, 'ABSPATH . "', dirname(__FILE__) ) . "/\" );", $wp_cache_config_file);
} elseif( is_file( dirname(__FILE__) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
wp_cache_replace_line('WPCACHEHOME', "define( 'WPCACHEHOME', " . str_replace( ABSPATH, 'ABSPATH . "', dirname(__FILE__) ) . "/wp-super-cache/\" );", $wp_cache_config_file);
}
$new = true;
}
if ( !is_writable($wp_cache_config_file)) {
echo "Error: Your WP-Cache config file ($wp_cache_config_file) is not writable by the Web server. Check its permissions.";
return false;
}
require($wp_cache_config_file);
return true;
}
function wp_cache_check_link() {
global $wp_cache_link, $wp_cache_file;
if ( basename(@readlink($wp_cache_link)) != basename($wp_cache_file)) {
@unlink($wp_cache_link);
if (!@symlink ($wp_cache_file, $wp_cache_link)) {
echo "advanced-cache.php link does not exist ";
echo "Create it by executing: ln -s $wp_cache_file $wp_cache_link in your server ";
return false;
}
}
return true;
}
function wp_cache_check_global_config() {
$global = ABSPATH . 'wp-config.php';
$lines = file($global);
foreach($lines as $line) {
if (preg_match('/^ *define *\( *\'WP_CACHE\' *, *true *\) *;/', $line)) {
return true;
}
}
$line = 'define(\'WP_CACHE\', true);';
if (!is_writable($global) || !wp_cache_replace_line('define *\( *\'WP_CACHE\'', $line, $global) ) {
echo "Error: WP_CACHE is not enabled in your wp-config.php file and I couldn't modified it. ";
echo "Edit $global and add the following line: define('WP_CACHE', true); Otherwise, WP-Cache will not be executed by Wordpress core. ";
return false;
}
return true;
}
function wp_cache_files() {
global $cache_path, $file_prefix, $cache_max_time, $super_cache_max_time, $valid_nonce, $supercachedir;
if ( '/' != substr($cache_path, -1)) {
$cache_path .= '/';
}
if ( $valid_nonce ) {
if(isset($_REQUEST['wp_delete_cache'])) {
wp_cache_clean_cache($file_prefix);
}
if(isset($_REQUEST['wp_delete_cache_file'])) {
wp_cache_clean_cache($_REQUEST['wp_delete_cache_file']);
}
if(isset($_REQUEST['wp_delete_expired'])) {
wp_cache_clean_expired($file_prefix);
}
}
if(isset($_REQUEST['wp_list_cache'])) {
$list_files = true;
$list_mess = "Update list";
} else
$list_mess = "List files";
echo '';
}
function wpsc_dirsize($directory, $sizes) {
global $super_cache_max_time;
$now = time();
if (is_dir($directory)) {
$entries = glob($directory. '/*');
foreach ($entries as $entry) {
if ($entry != '.' && $entry != '..') {
$sizes = wpsc_dirsize($entry, $sizes);
}
}
} else {
if(is_file($directory) ) {
if( filemtime( $directory ) + $super_cache_max_time <= $now ) {
$sizes[ 'expired' ]+=1;
} else {
$sizes[ 'cached' ]+=1;
}
}
}
return $sizes;
}
function wp_cache_clean_cache($file_prefix) {
global $cache_path, $supercachedir;
// If phase2 was compiled, use its function to avoid race-conditions
if(function_exists('wp_cache_phase2_clean_cache')) {
if( is_dir( $supercachedir ) ) {
prune_super_cache( $supercachedir, true );
} elseif( is_dir( $supercachedir . '.disabled' ) ) {
prune_super_cache( $supercachedir . '.disabled', true );
}
prune_super_cache( $cache_path, true );
$_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
return wp_cache_phase2_clean_cache($file_prefix);
}
$expr = "/^$file_prefix/";
if ( ($handle = opendir( $cache_path )) ) {
while ( false !== ($file = readdir($handle))) {
if ( preg_match($expr, $file) ) {
unlink($cache_path . $file);
unlink($cache_path . 'meta/' . str_replace( '.html', '.term', $file ) );
}
}
closedir($handle);
}
}
function wp_cache_clean_expired($file_prefix) {
global $cache_path, $cache_max_time;
// If phase2 was compiled, use its function to avoid race-conditions
if(function_exists('wp_cache_phase2_clean_expired')) {
$dir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
if( is_dir( $dir ) ) {
prune_super_cache( $dir );
} elseif( is_dir( $dir . '.disabled' ) ) {
prune_super_cache( $dir . '.disabled' );
}
$_POST[ 'super_cache_stats' ] = 1; // regenerate super cache stats;
return wp_cache_phase2_clean_expired($file_prefix);
}
$expr = "/^$file_prefix/";
$now = time();
if ( ($handle = opendir( $cache_path )) ) {
while ( false !== ($file = readdir($handle))) {
if ( preg_match($expr, $file) &&
(filemtime($cache_path . $file) + $cache_max_time) <= $now) {
unlink($cache_path . $file);
unlink($cache_path . 'meta/' . str_replace( '.html', '.term', $file ) );
}
}
closedir($handle);
}
}
add_action('admin_menu', 'wp_cache_add_pages');
if( get_option( 'gzipcompression' ) )
update_option( 'gzipcompression', 0 );
?>