Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP cron option to purge caches on schedule #971

Merged
merged 18 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 54 additions & 21 deletions DbCache_Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ class DbCache_Environment {
/**
* Fixes environment in each wp-admin request
*
* @param Config $config
* @param bool $force_all_checks
* @param Config $config Config.
* @param bool $force_all_checks Force checks flag.
*
* @throws Util_Environment_Exceptions
* @throws Util_Environment_Exceptions Exceptions.
*/
public function fix_on_wpadmin_request( $config, $force_all_checks ) {
$exs = new Util_Environment_Exceptions();
$exs = new Util_Environment_Exceptions();
$dbcache_enabled = $config->get_boolean( 'dbcache.enabled' );

try {
if ( $config->get_boolean( 'dbcache.enabled' ) ||
Util_Environment::is_dbcluster( $config ) ) {
if ( $dbcache_enabled || Util_Environment::is_dbcluster( $config ) ) {
$this->create_addin();
} else {
$this->delete_addin();
Expand All @@ -30,32 +31,47 @@ public function fix_on_wpadmin_request( $config, $force_all_checks ) {
$exs->push( $ex );
}

if ( count( $exs->exceptions() ) > 0 )
if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}

/**
* Fixes environment once event occurs
* Fixes environment once event occurs.
*
* @throws Util_Environment_Exceptions
* @param Config $config Config.
* @param string $event Event.
* @param null|Config $old_config Old Config.
*
* @throws Util_Environment_Exceptions Exception.
*/
public function fix_on_event( $config, $event, $old_config = null ) {
if ( $config->get_boolean( 'dbcache.enabled' ) &&
$config->get_string( 'dbcache.engine' ) == 'file' ) {
if ( !wp_next_scheduled( 'w3_dbcache_cleanup' ) ) {
wp_schedule_event( time(),
'w3_dbcache_cleanup', 'w3_dbcache_cleanup' );
$dbcache_enabled = $config->get_boolean( 'dbcache.enabled' );
$engine = $config->get_string( 'dbcache.engine' );

if ( $dbcache_enabled && ( 'file' === $engine || 'file_generic' === $engine ) ) {
$new_interval = $config->get_integer( 'dbcache.file.gc' );
$old_interval = $old_config ? $old_config->get_integer( 'dbcache.file.gc' ) : -1;

if ( null !== $old_config && $new_interval !== $old_interval ) {
$this->unschedule_gc();
}

if ( ! wp_next_scheduled( 'w3_dbcache_cleanup' ) ) {
wp_schedule_event( time(), 'w3_dbcache_cleanup', 'w3_dbcache_cleanup' );
}
} else {
$this->unschedule();
$this->unschedule_gc();
}
}

/**
* Fixes environment after plugin deactivation
*
* @throws Util_Environment_Exceptions
* @return array
* @throws Util_Environment_Exceptions Exception.
* @throws Util_WpFile_FilesystemOperationException Exception.
*
* @return void
*/
public function fix_after_deactivation() {
$exs = new Util_Environment_Exceptions();
Expand All @@ -66,10 +82,12 @@ public function fix_after_deactivation() {
$exs->push( $ex );
}

$this->unschedule();
$this->unschedule_gc();
$this->unschedule_purge_wpcron();

if ( count( $exs->exceptions() ) > 0 )
if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}

/**
Expand All @@ -83,14 +101,29 @@ function get_required_rules( $config ) {
}

/**
* scheduling stuff
* Remove Garbage collection cron job.
*
* @return void
*/
private function unschedule() {
private function unschedule_gc() {
if ( wp_next_scheduled( 'w3_dbcache_cleanup' ) ) {
wp_clear_scheduled_hook( 'w3_dbcache_cleanup' );
}
}

/**
* Remove cron job for pagecache purge.
*
* @since X.X.X
*
* @return void
*/
private function unschedule_purge_wpcron() {
if ( wp_next_scheduled( 'w3tc_dbcache_purge_wpcron' ) ) {
wp_clear_scheduled_hook( 'w3tc_dbcache_purge_wpcron' );
}
}

/**
* Creates add-in
*
Expand Down
43 changes: 29 additions & 14 deletions DbCache_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct() {
public function run() {
// phpcs:ignore WordPress.WP.CronInterval.ChangeDetected
add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) );
add_action( 'w3tc_dbcache_purge_wpcron', array( $this, 'w3tc_dbcache_purge_wpcron' ) );

if ( 'file' === $this->_config->get_string( 'dbcache.engine' ) ) {
add_action( 'w3_dbcache_cleanup', array( $this, 'cleanup' ) );
Expand Down Expand Up @@ -120,21 +121,35 @@ public function cleanup() {
* @return array
*/
public function cron_schedules( $schedules ) {
$gc = $this->_config->get_integer( 'dbcache.file.gc' );

return array_merge(
$schedules,
array(
'w3_dbcache_cleanup' => array(
'interval' => $gc,
'display' => sprintf(
// translators: 1 interval in seconds.
__( '[W3TC] Database Cache file GC (every %d seconds)', 'w3-total-cache' ),
$gc
),
$c = $this->_config;
$dbcache_enabled = $c->get_boolean( 'dbcache.enabled' );
$engine = $c->get_string( 'dbcache.engine' );

if ( $dbcache_enabled && ( 'file' === $engine || 'file_generic' === $engine ) ) {
$interval = $c->get_integer( 'dbcache.file.gc' );
$schedules['w3_dbcache_cleanup'] = array(
'interval' => $interval,
'display' => sprintf(
// translators: 1 interval in seconds.
__( '[W3TC] Database Cache file GC (every %d seconds)', 'w3-total-cache' ),
$interval
),
)
);
);
}

return $schedules;
}

/**
* Cron job for processing purging database cache.
*
* @since X.X.X
*
* @return void
*/
public function w3tc_dbcache_purge_wpcron() {
$flusher = Dispatcher::component( 'CacheFlush' );
$flusher->dbcache_flush();
}

/**
Expand Down
31 changes: 31 additions & 0 deletions DbCache_Plugin_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

class DbCache_Plugin_Admin {
function run() {
add_filter( 'w3tc_save_options', array( $this, 'w3tc_save_options' ) );

$config_labels = new DbCache_ConfigLabels();
add_filter( 'w3tc_config_labels', array( $config_labels, 'config_labels' ) );

Expand Down Expand Up @@ -68,4 +70,33 @@ public function w3tc_errors( $errors ) {

return $errors;
}

public function w3tc_save_options( $data ) {
$new_config = $data['new_config'];
$old_config = $data['old_config'];

// Schedule purge if enabled.
if ( $new_config->get_boolean( 'dbcache.enabled' ) && $new_config->get_boolean( 'dbcache.wp_cron' ) ) {
$new_wp_cron_time = $new_config->get_integer( 'dbcache.wp_cron_time' );
$old_wp_cron_time = $old_config ? $old_config->get_integer( 'dbcache.wp_cron_time' ) : -1;
$new_wp_cron_interval = $new_config->get_string( 'dbcache.wp_cron_interval' );
$old_wp_cron_interval = $old_config ? $old_config->get_string( 'dbcache.wp_cron_interval' ) : -1;
$schedule_needs_update = $new_wp_cron_time !== $old_wp_cron_time || $new_wp_cron_interval !== $old_wp_cron_interval;

// Clear the scheduled hook if a change in time or interval is detected.
if ( wp_next_scheduled( 'w3tc_dbcache_purge_wpcron' ) && $schedule_needs_update ) {
wp_clear_scheduled_hook( 'w3tc_dbcache_purge_wpcron' );
}

// Schedule if no existing cron event or settings have changed.
if ( ! wp_next_scheduled( 'w3tc_dbcache_purge_wpcron' ) || $schedule_needs_update ) {
$scheduled_timestamp_server = Util_Environment::get_cron_schedule_time( $new_wp_cron_time );
wp_schedule_event( $scheduled_timestamp_server, $new_wp_cron_interval, 'w3tc_dbcache_purge_wpcron' );
}
} elseif ( wp_next_scheduled( 'w3tc_dbcache_purge_wpcron' ) ) {
wp_clear_scheduled_hook( 'w3tc_dbcache_purge_wpcron' );
}

return $data;
}
}
45 changes: 34 additions & 11 deletions Generic_Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ class Generic_Environment {
/**
* Fixes environment
*
* @param Config $config
* @param bool $force_all_checks
* @throws Util_Environment_Exceptions
* @param Config $config Config.
* @param bool $force_all_checks Force checks flag.
*
* @throws Util_Environment_Exceptions Exceptions.
*/
function fix_on_wpadmin_request( $config, $force_all_checks ) {
public function fix_on_wpadmin_request( $config, $force_all_checks ) {
$exs = new Util_Environment_Exceptions();
// create add-ins

// create add-ins.
$this->create_required_files( $config, $exs );

// create folders
// create folders.
$this->create_required_folders( $exs );
$this->add_index_to_folders();

if ( count( $exs->exceptions() ) <= 0 ) {
// save actual version of config is it's built on legacy configs
$f = ConfigUtil::is_item_exists( 0, false );
// save actual version of config is it's built on legacy configs.
$f = ConfigUtil::is_item_exists( 0, false );
$f2 = file_exists( Config::util_config_filename_legacy_v2( 0, false ) );

$c = Dispatcher::config_master();
Expand All @@ -32,18 +34,24 @@ function fix_on_wpadmin_request( $config, $force_all_checks ) {
$f = ConfigUtil::is_item_exists( 0, false );
}

if ( $f && $f2 )
if ( $f && $f2 ) {
@unlink( Config::util_config_filename_legacy_v2( 0, false ) );
}
}

if ( count( $exs->exceptions() ) > 0 )
if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}

/**
* Fixes environment once event occurs
*
* @throws Util_Environment_Exceptions
* @param Config $config Config.
* @param string $event Event.
* @param null|Config $old_config Old Config.
*
* @throws Util_Environment_Exceptions Exceptions.
*/
public function fix_on_event( $config, $event, $old_config = null ) {
}
Expand All @@ -59,6 +67,8 @@ public function fix_after_deactivation() {

$this->delete_required_files( $exs );

$this->unschedule_purge_wpcron();

if ( count( $exs->exceptions() ) > 0 )
throw $exs;
}
Expand Down Expand Up @@ -207,4 +217,17 @@ public function is_advanced_cache_add_in() {
return ( ( $script_data = @file_get_contents( W3TC_ADDIN_FILE_ADVANCED_CACHE ) )
&& strstr( $script_data, 'PgCache_ContentGrabber' ) !== false );
}

/**
* Remove cron job for purge all caches.
*
* @since X.X.X
*
* @return void
*/
private function unschedule_purge_wpcron() {
if ( wp_next_scheduled( 'w3tc_purgeall_wpcron' ) ) {
wp_clear_scheduled_hook( 'w3tc_purgeall_wpcron' );
}
}
}
33 changes: 25 additions & 8 deletions Generic_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function __construct() {
*/
function run() {
add_filter( 'cron_schedules', array( $this, 'cron_schedules' ), 5 );
add_action( 'w3tc_purge_all_wpcron', array( $this, 'w3tc_purgeall_wpcron' ) );

/* need this to run before wp-cron to issue w3tc redirect */
add_action( 'init', array( $this, 'init' ), 1 );
Expand Down Expand Up @@ -65,16 +66,18 @@ public function wp_die_handler( $v ) {
/**
* Cron schedules filter
*
* @param array $schedules
* Sets default values which are overriden by apropriate plugins if they are enabled
*
* Absense of keys (if e.g. pgcaching became disabled, but there is cron event scheduled in db) causes PHP notices.
*
* @param array $schedules Schedules.
*
* @return array
*/
function cron_schedules( $schedules ) {
// Sets default values which are overriden by apropriate plugins
// if they are enabled
//
// absense of keys (if e.g. pgcaching became disabled, but there is
// cron event scheduled in db) causes PHP notices.
return array_merge(
public function cron_schedules( $schedules ) {
$c = $this->_config;

$schedules = array_merge(
$schedules,
array(
'w3_cdn_cron_queue_process' => array(
Expand Down Expand Up @@ -111,6 +114,20 @@ function cron_schedules( $schedules ) {
),
)
);

return $schedules;
}

/**
* Cron job for processing purging page cache.
*
* @since X.X.X
*
* @return void
*/
public function w3tc_purgeall_wpcron() {
$flusher = Dispatcher::component( 'CacheFlush' );
$flusher->flush_all();
}

/**
Expand Down
Loading