Skip to content

Instantly share code, notes, and snippets.

@roytanck
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roytanck/e28ec44b487d47940364 to your computer and use it in GitHub Desktop.
Save roytanck/e28ec44b487d47940364 to your computer and use it in GitHub Desktop.
WordPress plugin to force cleaning of expired WP Super Cache files
<?php
/**
* Plugin Name: RT Force Cache Refresh
* Plugin URI: http://www.this-play.nl
* Description: Periodically deletes expired WP Super Cache files using WP-Cron
* Version: 0.8
* Author: Roy Tanck
* Author URI: http://www.this-play.nl
* License: GPL2
*/
/**
* On activation, add scheduled event, but only on main site
*/
if( !function_exists('rt_force_cache_refresh_activation')){
function rt_force_cache_refresh_activation() {
if ( is_main_site() && !wp_next_scheduled( 'rt_cache_refresh_event' ) ) {
wp_schedule_event( time(), 'rt_cache_refresh', 'rt_cache_refresh_event');
}
}
register_activation_hook( __FILE__, 'rt_force_cache_refresh_activation' );
}
/**
* On deactivation, remove all functions from the scheduled action hook.
*/
if( !function_exists('rt_force_cache_refresh_deactivation')){
function rt_force_cache_refresh_deactivation() {
wp_clear_scheduled_hook( 'rt_cache_refresh_event' );
}
register_deactivation_hook( __FILE__, 'rt_force_cache_refresh_deactivation' );
}
/**
* Attempt to delete expired cache files by calling a WPSC function directly
*/
if( !function_exists('rt_cache_refresh_delete_expired')){
function rt_cache_refresh_delete_expired() {
global $file_prefix;
if( function_exists('wp_cache_clean_expired') && isset( $file_prefix ) ){
wp_cache_clean_expired($file_prefix);
error_log( 'RT Force Cache Refresh: Super Cache cleaned' );
} else {
error_log( 'RT Force Cache Refresh: Super Cache clean failed, prefix: ' . $file_prefix );
}
}
// if the event fires, call the delete function
add_action( 'rt_cache_refresh_event', 'rt_cache_refresh_delete_expired' );
}
/**
* Function to add a custom cron recurrence for the cleanup
*/
if( !function_exists('rt_add_cache_refresh_recurrence')){
function rt_add_cache_refresh_recurrence( $schedules ) {
// Add to the existing schedules
$schedules['rt_cache_refresh'] = array(
'interval' => 900,
'display' => __( 'RT Cache Refresh' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'rt_add_cache_refresh_recurrence' );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment