Skip to content

Commit 7909495

Browse files
committed
shutdown autosync queue when disabled
1 parent 621ac33 commit 7909495

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

php/sync/class-sync-queue.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function is_enabled() {
175175
*/
176176
public function load_hooks() {
177177
add_action( 'cloudinary_resume_queue', array( $this, 'maybe_resume_queue' ) );
178-
add_action( 'settings_save_setting_auto_sync', array( $this, 'change_setting_state' ), 10, 3 );
178+
add_action( 'cloudinary_settings_save_setting_auto_sync', array( $this, 'change_setting_state' ), 10, 3 );
179179
}
180180

181181
/**
@@ -188,9 +188,14 @@ public function load_hooks() {
188188
* @return mixed
189189
*/
190190
public function change_setting_state( $new_value, $current_value, $setting ) {
191-
if ( $this->is_running() && 'on' === $current_value && 'off' === $new_value ) {
192-
$this->shutdown_queue();
193-
$setting->add_error_notice( 'disabled_sync', 'Bulk sync has been disabled.', 'warning' );
191+
// shutdown queues if needed.
192+
if ( 'on' === $current_value && 'off' === $new_value ) {
193+
if ( $this->is_running() ) {
194+
$this->shutdown_queue( 'queue' );
195+
$setting->add_error_notice( 'disabled_sync', 'Bulk sync has been disabled.', 'warning' );
196+
}
197+
// Shutdown autosync queue.
198+
$this->shutdown_queue( 'autosync' );
194199
}
195200

196201
return $new_value;
@@ -268,7 +273,7 @@ public function get_post( $thread ) {
268273
*/
269274
public function is_running( $type = 'queue' ) {
270275
if ( 'autosync' === $type ) {
271-
return $this->sync->is_auto_sync_enabled();
276+
return true; // Autosync always runs, however if off, auto sync queue building is off.
272277
}
273278
$queue = $this->get_queue();
274279

@@ -356,6 +361,9 @@ public function stop_maybe( $type = 'queue' ) {
356361
protected function shutdown_queue( $type = 'queue' ) {
357362
if ( 'queue' === $type ) {
358363
delete_option( self::$queue_enabled );
364+
} elseif ( 'autosync' === $type ) {
365+
// Remove pending flag.
366+
delete_post_meta_by_key( Sync::META_KEYS['pending'] );
359367
}
360368
$this->stop_queue( $type );
361369
}
@@ -535,7 +543,8 @@ protected function get_thread_queue_details( $thread ) {
535543
'posts_per_page' => 1,
536544
'fields' => 'ids',
537545
'cache_results' => false,
538-
'meta_query' => array( // phpcs:ignore
546+
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
547+
'meta_query' => array(
539548
array(
540549
'key' => $thread,
541550
'compare' => 'EXISTS',

0 commit comments

Comments
 (0)