Created
January 2, 2025 20:09
-
-
Save robertdevore/07a408f60a3598b3f738270cd3134290 to your computer and use it in GitHub Desktop.
Auto-clear the WP Rocket cache every 5 minutes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The plugin bootstrap file | |
* | |
* @link https://freshysites.com | |
* @since 1.0.0 | |
* @package Cache_Clearer_WP_Rocket | |
* | |
* @wordpress-plugin | |
* | |
* Plugin Name: Cache Clearer for WP Rocket | |
* Description: A plugin to automatically clear WP Rocket caches every 5 minutes using a scheduled cron job. | |
* Plugin URI: https://freshysites.com | |
* Version: 1.0.0 | |
* Author: Freshy Sites | |
* Author URI: https://freshysites.com/ | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: cache-clearer-for-wp-rocket | |
* Domain Path: /languages | |
*/ | |
// If this file is called directly, abort. | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
/** | |
* Schedule a custom cron interval of 5 minutes. | |
* | |
* @param array $schedules Existing schedules. | |
* | |
* @since 1.0.0 | |
* @return array Modified schedules. | |
*/ | |
function ccwpr_auto_cache_clear_schedule( $schedules ) { | |
$schedules['every_five_minutes'] = [ | |
'interval' => 300, | |
'display' => __( 'Every Five Minutes', 'cache-clearer-for-wp-rocket' ), | |
]; | |
return $schedules; | |
} | |
add_filter( 'cron_schedules', 'ccwpr_auto_cache_clear_schedule' ); | |
/** | |
* Schedule the WP Rocket cache clearing cron job on plugin activation. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function ccwpr_auto_cache_clear_activate() { | |
if ( ! wp_next_scheduled( 'ccwpr_auto_cache_clear_cron' ) ) { | |
wp_schedule_event( time(), 'every_five_minutes', 'ccwpr_auto_cache_clear_cron' ); | |
} | |
} | |
register_activation_hook( __FILE__, 'ccwpr_auto_cache_clear_activate' ); | |
/** | |
* Clear the WP Rocket cache. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function ccwpr_auto_cache_clear_handler() { | |
// Check if WP Rocket's clear cache function exists. | |
if ( function_exists( 'rocket_clean_domain' ) ) { | |
rocket_clean_domain(); | |
error_log( 'WP Rocket cache cleared via cron job.' ); // Optional logging for debugging. | |
} else { | |
error_log( 'WP Rocket is not active or rocket_clean_domain function is unavailable.' ); | |
} | |
} | |
add_action( 'ccwpr_auto_cache_clear_cron', 'ccwpr_auto_cache_clear_handler' ); | |
/** | |
* Remove the scheduled cron job on plugin deactivation. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function ccwpr_auto_cache_clear_deactivate() { | |
$timestamp = wp_next_scheduled( 'ccwpr_auto_cache_clear_cron' ); | |
if ( $timestamp ) { | |
wp_unschedule_event( $timestamp, 'ccwpr_auto_cache_clear_cron' ); | |
} | |
} | |
register_deactivation_hook( __FILE__, 'ccwpr_auto_cache_clear_deactivate' ); | |
/** | |
* Add settings page. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function ccwpr_add_settings_page() { | |
add_options_page( | |
__( 'Cache Clearer for WP Rocket', 'cache-clearer-for-wp-rocket' ), | |
__( 'Cache Clearer', 'cache-clearer-for-wp-rocket' ), | |
'manage_options', | |
'ccwpr-settings', | |
'ccwpr_render_settings_page' | |
); | |
} | |
add_action( 'admin_menu', 'ccwpr_add_settings_page' ); | |
/** | |
* Render the settings page. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function ccwpr_render_settings_page() { | |
?> | |
<div class="wrap"> | |
<h1><?php echo esc_html__( 'Cache Clearer for WP Rocket', 'cache-clearer-for-wp-rocket' ); ?></h1> | |
<form method="post"> | |
<?php wp_nonce_field( 'ccwpr_clear_cache_nonce_action', 'ccwpr_clear_cache_nonce' ); ?> | |
<p> | |
<button type="submit" name="ccwpr_clear_cache" class="button button-primary"> | |
<?php echo esc_html__( 'Clear Cache Now', 'cache-clearer-for-wp-rocket' ); ?> | |
</button> | |
</p> | |
</form> | |
</div> | |
<?php | |
} | |
/** | |
* Handle manual cache clearing. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function ccwpr_handle_manual_clear_cache() { | |
if ( isset( $_POST['ccwpr_clear_cache'] ) && check_admin_referer( 'ccwpr_clear_cache_nonce_action', 'ccwpr_clear_cache_nonce' ) ) { | |
if ( function_exists( 'rocket_clean_domain' ) ) { | |
rocket_clean_domain(); | |
add_action( 'admin_notices', function() { | |
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'WP Rocket cache cleared successfully!', 'cache-clearer-for-wp-rocket' ) . '</p></div>'; | |
} ); | |
} else { | |
add_action( 'admin_notices', function() { | |
echo '<div class="notice notice-error is-dismissible"><p>' . esc_html__( 'Failed to clear cache. WP Rocket may not be active.', 'cache-clearer-for-wp-rocket' ) . '</p></div>'; | |
} ); | |
} | |
} | |
} | |
add_action( 'admin_init', 'ccwpr_handle_manual_clear_cache' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment