enable the cache. Based on WP-Cache by Ricardo Galli Granada .
Version: 0.6.4
Author: Donncha O Caoimh
Author URI: http://ocaoimh.ie/
*/
/* Copyright 2005-2006 Ricardo Galli Granada (email : gallir@uib.es)
Some code copyright 2007-2008 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() ) {
add_submenu_page('wpmu-admin.php', __('WP Super Cache'), __('WP Super Cache'), 'manage_options', __FILE__, 'wp_cache_manager');
add_options_page('WP Super Cache', 'WP Super Cache', 'manage_options', __FILE__, 'wp_cache_manager');
}
} else {
add_options_page('WP Super Cache', 'WP Super Cache', 'manage_options', __FILE__, 'wp_cache_manager');
}
}
add_action('admin_menu', 'wp_cache_add_pages');
function wp_cache_manager() {
global $wp_cache_config_file, $valid_nonce, $supercachedir, $cache_path, $cache_enabled, $cache_compression, $super_cache_enabled, $wp_cache_hello_world;
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');
/* http://www.netlobo.com/div_hiding.html */
?>
';
echo "
WP Super Cache Manager \n";
if( ini_get( 'safe_mode' ) ) {
?>Warning! PHP safe mode enabled!
You may experience problems running this plugin because SAFE MODE is enabled. Your server is set up to check the owner of PHP scripts before allowing them to read and write files.
You or an administrator may be able to make it work by changing the group owner of the plugin scripts to match that of the web server user. The group owner of the wp-content/cache/ directory must also be changed. See the safe mode manual page for further details.
You or an administrator must disable this. See the safe mode manual page for further details. This cannot be disabled in a .htaccess file unfortunately. It must be done in the php.ini config file.Configuration file changed, some values might be wrong. Load the page again from the "Options" menu to reset them.';
}
if ( !wp_cache_check_link() ||
!wp_cache_verify_config_file() ||
!wp_cache_verify_cache_dir() ) {
echo " Cannot continue... fix previous problems and retry. ";
echo "\n";
return;
}
if (!wp_cache_check_global_config()) {
echo "\n";
return;
}
if( $cache_enabled == true && $super_cache_enabled == true && !got_mod_rewrite() ) {
?>Mod rewrite may not be installed!
It appears that mod_rewrite is not installed. Sometimes this check isn't 100% reliable, especially if you are not using Apache. Please verify that the mod_rewrite module is loaded. It is required for serving Super Cache static files. You will still be able to use WP-Cache.
Read Only Mode. Configuration cannot be changed. Why
The WP Super Cache configuration file is wp-content/wp-cache-config.php and cannot be modified. The wp-content directory and wp-cache-config.php file must be writeable by the webserver to make any changes.
A simple way of doing that is by changing the permissions temporarily using the CHMOD command or through your ftp client. Make sure it's globally writeable and it should be fine.
Writeable: chmod 777 wp-content; chmod 666 wp-content/wp-cache-config.php
Readonly: chmod 755 wp-content; chmod 644 wp-content/wp-cache-config.php
WP Super Cache Status ';
?>
> ON (WP Cache and Super Cache enabled)
> OFF (WP Cache and Super Cache disabled)
> HALF ON (Super Cache Disabled)
value='1'> Proudly tell the world your server is Digg proof! (places a message in your blog's footer)
";
wp_nonce_field('wp-cache');
?>
Super Cache Compression
\n";
?>
Mod Rewrite Rules Mod Rewrite rules cannot be updated!";
echo "You must have BEGIN and END markers in {$home_path}.htaccess for the auto update to work. They look like this and surround the main WordPress mod_rewrite rules:
# BEGIN WordPress RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress
Refresh this page when you have updated your .htaccess file.";
} elseif( strpos( $wprules, 'wordpressuser' ) ) { // Need to clear out old mod_rewrite rules
echo "Thank you for upgrading. The mod_rewrite rules changed since you last installed this plugin. Unfortunately you must remove the old supercache rules before the new ones are updated. Refresh this page when you have edited your .htaccess file. If you wish to manually upgrade, change the following line:
RewriteCond %{HTTP_COOKIE} !^.*wordpressuser.*\$ so it looks like this: RewriteCond %{HTTP_COOKIE} !^.*wordpress.*\$ The only change is 'wordpressuser' becomes 'wordpress'. This is a WordPress 2.5 change but it's backwards compatible with older versions if you're brave enough to use them.";
echo " ";
echo "\n";
return;
} elseif( strpos( $scrules, 'supercache' ) || strpos( $wprules, 'supercache' ) ) { // only write the rules once
$dohtaccess = false;
}
$rules = "\n";
$rules .= "RewriteEngine On\n";
$rules .= "RewriteBase $home_root\n"; // props Chris Messina
$charset = get_option('blog_charset') == '' ? 'UTF-8' : get_option('blog_charset');
$rules .= "AddDefaultCharset {$charset}\n";
$rules .= "RewriteCond %{REQUEST_METHOD} !=POST\n";
$rules .= "RewriteCond %{QUERY_STRING} !.*s=.*\n";
$rules .= "RewriteCond %{QUERY_STRING} !.*attachment_id=.*\n";
$rules .= "RewriteCond %{HTTP_COOKIE} !^.*(comment_author_|wordpress|wp-postpass_).*$\n";
$rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
$rules .= "RewriteCond %{DOCUMENT_ROOT}{$home_root}wp-content/cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html.gz -f\n";
$rules .= "RewriteRule ^(.*) {$home_root}wp-content/cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html.gz [L]\n\n";
$rules .= "RewriteCond %{REQUEST_METHOD} !=POST\n";
$rules .= "RewriteCond %{QUERY_STRING} !.*s=.*\n";
$rules .= "RewriteCond %{QUERY_STRING} !.*attachment_id=.*\n";
$rules .= "RewriteCond %{HTTP_COOKIE} !^.*(comment_author_|wordpress|wp-postpass_).*$\n";
$rules .= "RewriteCond %{DOCUMENT_ROOT}{$home_root}wp-content/cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html -f\n";
$rules .= "RewriteRule ^(.*) {$home_root}wp-content/cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html [L]\n";
$rules .= " \n";
if( $dohtaccess && !$_POST[ 'updatehtaccess' ] ) {
echo "In order to serve static html files your server must have the correct mod_rewrite rules added to a file called " . ABSPATH . ".htaccess This can be done automatically by clicking the 'Update mod_rewrite rules »' button or you can edit the file yourself and add the following rules. Make sure they appear before any existing WordPress rules.";
echo "
" . wp_specialchars( $rules ) . " ";
echo '\n";
} elseif( $dohtaccess && $valid_nonce && $_POST[ 'updatehtaccess' ] ) {
wpsc_remove_marker( $home_path.'.htaccess', 'WordPress' ); // remove original WP rules so SuperCache rules go on top
if( insert_with_markers( $home_path.'.htaccess', 'WPSuperCache', explode( "\n", $rules ) ) && insert_with_markers( $home_path.'.htaccess', 'WordPress', explode( "\n", $wprules ) ) ) {
echo "Mod Rewrite rules updated! ";
echo "" . ABSPATH . ".htaccess has been updated with the necessary mod_rewrite rules. Please verify they are correct. They should look like this:
\n";
} else {
echo "Mod Rewrite rules must be updated! ";
echo " Your " . ABSPATH . ".htaccess is not writable by the webserver and must be updated with the necessary mod_rewrite rules. The new rules go above the regular WordPress rules as shown in the code below:
\n";
}
echo "
" . wp_specialchars( $rules ) . " \n";
} else {
?>
WP Super Cache has modified your .htaccess file. Click the following link to see the lines added. If you have upgraded the plugin make sure these rules match. View mod_rewrite rules
" . wp_specialchars( $rules ) . " \n"; ?>
Gzip encoding rules in {$cache_path}.htaccess created.";
}
?>Accepted filenames, rejected URIs ';
wp_cache_edit_rejected();
echo " \n";
wp_cache_edit_accepted();
echo ' ';
wp_cache_edit_rejected_ua();
wp_cache_files();
wp_lock_down();
wp_cache_restore();
ob_start();
if( defined( 'WP_CACHE' ) ) {
if( function_exists( 'do_cacheaction' ) ) {
do_cacheaction( 'cache_admin_page' );
}
}
$out = ob_get_contents();
ob_end_clean();
if( SUBMITDISABLED == ' ' && $out != '' ) {
echo 'Cache Plugins ';
echo $out;
echo ' ';
}
echo "\n";
}
function wp_cache_restore() {
echo 'Configuration messed up? ';
echo '\n";
echo ' ';
}
function comment_form_lockdown_message() {
?>
Lock Down: (advanced use only)
Prepare your server for an expected spike in traffic by enabling the lock down. When this is enabled, new comments on a post will not refresh the cached static files.
Developers: Make your plugin lock down compatible by checking the 'WPLOCKDOWN' constant. The following code will make sure your plugin respects the WPLOCKDOWN setting.
if( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) ) {
echo "Sorry. My blog is locked down. Updates will appear shortly";
}
WordPress is locked down. Super Cache static files will not be deleted when new comments are made. WordPress is not locked down. New comments will refresh Super Cache static files as normal. ';
echo " ";
echo "
";
wp_nonce_field('wp-cache');
echo "\n";
?>
Directly Cached Files (advanced use only) escape( $page );
if( $page != '' ) {
$cached_direct_pages[] = $page;
$out .= "'$page', ";
}
}
if( $out == '' ) {
$out = "'', ";
}
}
if( $valid_nonce && $_POST[ 'new_direct_page' ] && '' != $_POST[ 'new_direct_page' ] ) {
$page = str_replace( get_option( 'siteurl' ), '', $_POST[ 'new_direct_page' ] );
if( substr( $page, 0, 1 ) != '/' )
$page = '/' . $page;
$page = $wpdb->escape( $page );
if( in_array( $page, $cached_direct_pages ) == false ) {
$cached_direct_pages[] = $page;
$out .= "'$page', ";
}
}
if( $out != '' ) {
$out = substr( $out, 0, -2 );
$out = '$cached_direct_pages = array( ' . $out . ' );';
wp_cache_replace_line('^ *\$cached_direct_pages', "$out", $wp_cache_config_file);
prune_super_cache( $cache_path, true );
}
if( !empty( $expiredfiles ) ) {
foreach( $expiredfiles as $file ) {
if( $file != '' ) {
$firstfolder = explode( '/', $file );
$firstfolder = ABSPATH . $firstfolder[1];
$file = ABSPATH . $file;
@unlink( trailingslashit( $file ) . 'index.html' );
@unlink( trailingslashit( $file ) . 'index.html.gz' );
RecursiveFolderDelete( trailingslashit( $firstfolder ) );
}
}
}
if( $valid_nonce && $_POST[ 'deletepage' ] ) {
$page = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '..', '', $_POST['deletepage']) );
$pagefile = ABSPATH . $page . 'index.html';
$firstfolder = explode( '/', $page );
$firstfolder = ABSPATH . $firstfolder[1];
$page = ABSPATH . $page;
if( is_file( $pagefile ) && is_writable( $pagefile ) && is_writable( $firstfolder ) ) {
@unlink( $pagefile );
@unlink( $pagefile . '.gz' );
RecursiveFolderDelete( $firstfolder );
echo "$pagefile removed! ";
prune_super_cache( $cache_path, true );
}
}
$readonly = '';
if( !is_writeable( ABSPATH ) ) {
$readonly = 'READONLY';
?>WARNING! You must make writable to enable this feature. As this is a security risk please make it readonly after your page is generated.
WARNING! is writable. Please make it readonly after your page is generated as this is a security risk.
';
if( is_array( $cached_direct_pages ) ) {
$out = '';
foreach( $cached_direct_pages as $page ) {
if( $page == '' )
continue;
$generated = '';
if( is_file( ABSPATH . $page . '/index.html' ) )
$generated = ' ';
$out .= "$generated ";
}
if( $out != '' ) {
?>Existing direct page Delete cached file ";
}
}
if( $readonly != 'READONLY' )
echo "Add direct page: ";
echo "Directly cached files are files created directly off " . ABSPATH . " where your blog lives. This feature is only useful if you are expecting a major Digg or Slashdot level of traffic to one post or page.
";
if( $readonly != 'READONLY' ) {
echo "For example: to cache '" . trailingslashit( get_option( 'siteurl' ) ) . "about/' , you would enter '" . trailingslashit( get_option( 'siteurl' ) ) . "about/' or '/about/'. The cached file will be generated the next time an anonymous user visits that page.
";
echo "Make the textbox blank to remove it from the list of direct pages and delete the cached file.
";
}
wp_nonce_field('wp-cache');
if( $readonly != 'READONLY' )
echo "
";
echo "\n";
?> 0) {
$cache_max_time = $max_time;
wp_cache_replace_line('^ *\$cache_max_time', "\$cache_max_time = $cache_max_time;", $wp_cache_config_file);
}
}
if(isset($_POST['super_cache_max_time']) && $valid_nonce) {
$max_time = (int)$_POST['super_cache_max_time'];
if ($max_time > 0) {
$super_cache_max_time = $max_time;
wp_cache_replace_line('^ *\$super_cache_max_time', "\$super_cache_max_time = $super_cache_max_time;", $wp_cache_config_file);
}
}
?>
Expiry Time ';
echo 'Expire time: ';
echo " seconds ";
if( $cache_enabled == true && $super_cache_enabled == true ) {
echo 'Super Cache Expire time: ';
echo " seconds";
}
echo '
';
wp_nonce_field('wp-cache');
echo "\n";
?> Rejected User Agents ';
echo "Strings in the HTTP 'User Agent' header that prevent WP-Cache from
caching bot, spiders, and crawlers' requests.
Note that cached files are still sent to these request if they already exists.
\n";
echo '';
echo " \n";
}
function wp_cache_edit_rejected() {
global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
if(isset($_REQUEST['wp_rejected_uri']) && $valid_nonce) {
$text = wp_cache_sanitize_value($_REQUEST['wp_rejected_uri'], $cache_rejected_uri);
wp_cache_replace_line('^ *\$cache_rejected_uri', "\$cache_rejected_uri = $text;", $wp_cache_config_file);
}
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 not cache that page.
\n";
echo '';
echo 'Rejected URIs ';
echo '';
foreach ($cache_rejected_uri as $file) {
echo wp_specialchars($file) . "\n";
}
echo ' ';
echo '
';
wp_nonce_field('wp-cache');
echo " \n";
}
function wp_cache_edit_accepted() {
global $cache_acceptable_files, $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;
if(isset($_REQUEST['wp_accepted_files']) && $valid_nonce) {
$text = wp_cache_sanitize_value($_REQUEST['wp_accepted_files'], $cache_acceptable_files);
wp_cache_replace_line('^ *\$cache_acceptable_files', "\$cache_acceptable_files = $text;", $wp_cache_config_file);
}
echo "Add here those filenames that can be cached, even if they match one of the rejected substring specified above.
\n";
echo '';
echo 'Accepted files ';
echo '';
foreach ($cache_acceptable_files as $file) {
echo wp_specialchars($file) . "\n";
}
echo ' ';
echo '
';
wp_nonce_field('wp-cache');
echo " \n";
}
function wp_cache_enable() {
global $wp_cache_config_file, $cache_enabled, $supercachedir;
if(get_option('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 $cache_path, $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();
sleep( 1 ); // allow existing processes to write to the supercachedir and then delete it
if (function_exists ('prune_super_cache') && is_dir( $supercachedir ) ) {
prune_super_cache( $cache_path, true );
}
}
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;
wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = false;', $wp_cache_config_file);
if( is_dir( $supercachedir ) )
rename( $supercachedir, $supercachedir . ".disabled" );
$super_cache_enabled = false;
}
function wp_cache_is_enabled() {
global $wp_cache_config_file;
if(get_option('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 ( file_exists($wp_cache_config_file) ) {
$lines = join( ' ', file( $wp_cache_config_file ) );
if( strpos( $lines, 'WPCACHEHOME' ) === false ) {
if( is_writable( $wp_cache_config_file ) ) {
@unlink( $wp_cache_config_file );
} else {
echo "Error: Your WP-Cache config file ($wp_cache_config_file ) is out of date and not writable by the Web server. Please delete it and refresh this page.";
return false;
}
}
} elseif( !is_writable($dir)) {
echo "Error: Configuration file missing and 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', ABSPATH . " . str_replace( '\\', '/', str_replace( 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', ABSPATH . " . str_replace( '\\', '/', str_replace( ABSPATH, ' "', dirname(__FILE__) ) ) . "/wp-super-cache/\" );", $wp_cache_config_file);
}
$new = true;
}
require($wp_cache_config_file);
return true;
}
function wp_cache_check_link() {
global $wp_cache_link, $wp_cache_file;
if( file_exists($wp_cache_link) ) {
if( strpos( join( "\n", file( $wp_cache_link ) ), 'WPCACHEHOME' ) ) {
// read the file and verify it's a super-cache file and not the wp-cache one.
return true;
} else {
// remove the old version
@unlink($wp_cache_link);
}
}
$ret = true;
if ( basename(@readlink($wp_cache_link)) != basename($wp_cache_file)) {
@unlink($wp_cache_link);
if( function_exists( 'symlink' ) ) {
if( !@symlink ($wp_cache_file, $wp_cache_link) ) {
$ret = false;
}
} elseif( !@copy( $wp_cache_file, $wp_cache_link ) ) {
$ret = false;
}
if( !$ret ) {
echo "advanced-cache.php does not exist ";
echo "Create it by executing: ln -s $wp_cache_file $wp_cache_link in your server ";
echo "Or by copying $wp_cache_file to $wp_cache_link. ";
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, $cache_enabled, $super_cache_enabled;
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 'Cache contents ';
/*
echo '';
echo ' ';
echo '
';
echo " \n";
*/
$list_files = false; // it doesn't list supercached files, and removing single pages is buggy
$count = 0;
$expired = 0;
$now = time();
if ( ($handle = @opendir( $cache_path . 'meta/' )) ) {
if ($list_files) echo "";
}
if( $cache_enabled == true && $super_cache_enabled == true ) {
$now = time();
$sizes = array( 'expired' => 0, 'cached' => 0, 'ts' => 0 );
if (is_dir($supercachedir)) {
$entries = glob($supercachedir. '/*');
foreach ($entries as $entry) {
if ($entry != '.' && $entry != '..') {
$sizes = wpsc_dirsize( $entry, $sizes );
}
}
} else {
if(is_file($supercachedir) && filemtime( $supercachedir ) + $super_cache_max_time <= $now )
$sizes[ 'expired' ] ++;
}
$sizes[ 'ts' ] = time();
}
echo "WP-Cache
";
echo "$count cached pages ";
echo "$expired expired pages ";
if( $cache_enabled == true && $super_cache_enabled == true ) {
echo "WP-Super-Cache
";
echo "" . intval($sizes['cached']/2) . " cached pages ";
$age = intval(($now - $sizes['ts'])/60);
echo "" . intval($sizes['expired']/2) . " expired pages. ";
}
echo '';
echo ' ';
echo '
';
wp_nonce_field('wp-cache');
echo " \n";
echo '';
echo ' ';
echo '
';
wp_nonce_field('wp-cache');
echo " \n";
echo ' ';
}
function wpsc_dirsize($directory, $sizes) {
global $super_cache_max_time;
$now = time();
if (is_dir($directory)) {
$entries = glob($directory. '/*');
if( is_array( $entries ) && !empty( $entries ) ) 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 (function_exists ('prune_super_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')) {
if (function_exists ('prune_super_cache')) {
$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);
}
}
function wpsc_remove_marker( $filename, $marker ) {
if (!file_exists( $filename ) || is_writeable( $filename ) ) {
if (!file_exists( $filename ) ) {
return '';
} else {
$markerdata = explode( "\n", implode( '', file( $filename ) ) );
}
$f = fopen( $filename, 'w' );
$foundit = false;
if ( $markerdata ) {
$state = true;
foreach ( $markerdata as $n => $markerline ) {
if (strpos($markerline, '# BEGIN ' . $marker) !== false)
$state = false;
if ( $state ) {
if ( $n + 1 < count( $markerdata ) )
fwrite( $f, "{$markerline}\n" );
else
fwrite( $f, "{$markerline}" );
}
if (strpos($markerline, '# END ' . $marker) !== false) {
$state = true;
}
}
}
return true;
} else {
return false;
}
}
function wp_super_cache_footer() {
?> is Digg proof thanks to caching by WP Super Cache !