From 9356c01cbea6e4bebff79eff5790a19f1022842b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 3 Oct 2018 09:17:46 -0400 Subject: [PATCH] Add new method and transient to correctly cancel a process (#1) * add new method and transient to correctly cancel a process * remove copied comment --- classes/wp-background-process.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/classes/wp-background-process.php b/classes/wp-background-process.php index 49031a8..46be008 100644 --- a/classes/wp-background-process.php +++ b/classes/wp-background-process.php @@ -219,6 +219,20 @@ protected function is_process_running() { return false; } + /** + * Is process cancelled + * + * Check whether the current process is cancelled + * in a background process. + */ + protected function is_process_cancelled() { + if ( get_site_transient( $this->identifier . '_process_cancelled' ) ) { + return true; + } + + return false; + } + /** * Lock process * @@ -313,7 +327,7 @@ protected function handle() { } // Update or delete current batch. - if ( ! empty( $batch->data ) ) { + if ( ! empty( $batch->data ) && ! $this->is_process_cancelled() ) { $this->update( $batch->key, $batch->data ); } else { $this->delete( $batch->key ); @@ -401,6 +415,7 @@ protected function time_exceeded() { protected function complete() { // Unschedule the cron healthcheck. $this->clear_scheduled_event(); + delete_site_transient( $this->identifier . '_process_cancelled' ); } /** @@ -480,8 +495,10 @@ public function cancel_process() { $batch = $this->get_batch(); $this->delete( $batch->key ); - + $this->unlock_process(); wp_clear_scheduled_hook( $this->cron_hook_identifier ); + + set_site_transient( $this->identifier . '_process_cancelled', 1 ); } }