| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | */ |
| 17 | 17 | public function __construct() { |
| 18 | 18 | $this->state_key = WPMCS_TOKEN . '_' . $this->action . '_state'; |
| 19 | 19 | $this->runner = BGRunner::instance(); |
| 20 | - $this->runner->set_callback($this->action, [$this, 'sync_to_cloud']); | |
| 20 | + $this->runner->set_callback($this->action, [$this, 'sync_to_cloud'], true); | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * Get Action |
| @@ -36,9 +36,10 @@ | ||
| 36 | 36 | $this->clear_state(); |
| 37 | 37 | } |
| 38 | 38 | $state = $this->get_state(); |
| 39 | 39 | $failed_count = isset($state['failed']) ? $state['failed'] : []; |
| 40 | - | |
| 40 | + $last_ids = isset($state['last_ids']) ? $state['last_ids'] : []; | |
| 41 | + | |
| 41 | 42 | $all_media_count = Counter::get_count( 'all', false ); |
| 42 | 43 | |
| 43 | 44 | $is_failed = false; |
| 44 | 45 | |
| @@ -51,10 +52,15 @@ | ||
| 51 | 52 | $pending = $total - ($uploaded + $failed); |
| 52 | 53 | if($pending > 0) { |
| 53 | 54 | $handler = Integration::instance()->get_handler_class($source_type); |
| 54 | 55 | if ($handler) { |
| 55 | - $result = $handler::instance()->upload_pending_media($source_type, 1, $failed); | |
| 56 | + $after_id = isset($last_ids[$source_type]) ? (int) $last_ids[$source_type] : 0; | |
| 57 | + $result = $handler::instance()->upload_pending_media($source_type, 1, $after_id); | |
| 56 | 58 | |
| 59 | + if (isset($result['last_id'])) { | |
| 60 | + $last_ids[$source_type] = (int) $result['last_id']; | |
| 61 | + } | |
| 62 | + | |
| 57 | 63 | if(isset($result['failed']) && !empty($result['failed']) && $result['failed'] > 0) { |
| 58 | 64 | $failed_count[$source_type] = $failed + $result['failed']; |
| 59 | 65 | $is_failed = true; |
| 60 | 66 | } |
| @@ -67,9 +73,10 @@ | ||
| 67 | 73 | if(($offset+1) == $total_iterations) { |
| 68 | 74 | $this->clear_state(); |
| 69 | 75 | } else { |
| 70 | 76 | $state = [ |
| 71 | - 'failed' => $failed_count | |
| 77 | + 'failed' => $failed_count, | |
| 78 | + 'last_ids' => $last_ids, | |
| 72 | 79 | ]; |
| 73 | 80 | $this->set_state($state); |
| 74 | 81 | } |
| 75 | 82 | |
| @@ -94,8 +101,17 @@ | ||
| 94 | 101 | $this->runner->stop($this->action); |
| 95 | 102 | } |
| 96 | 103 | |
| 97 | 104 | /** |
| 105 | + * Tick — process a small batch synchronously (ajax/mixed sync mode). | |
| 106 | + * @since 1.3.13 | |
| 107 | + */ | |
| 108 | + public function tick($batch = 5) { | |
| 109 | + $this->runner->run_all($this->action, $batch); | |
| 110 | + return $this->get_status(); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 98 | 114 | * Pause the action. |
| 99 | 115 | */ |
| 100 | 116 | public function pause() { |
| 101 | 117 | $this->runner->pause($this->action); |
| @@ -108,8 +124,15 @@ | ||
| 108 | 124 | $this->runner->resume($this->action); |
| 109 | 125 | } |
| 110 | 126 | |
| 111 | 127 | /** |
| 128 | + * Set the sync method preference for this type. | |
| 129 | + */ | |
| 130 | + public function set_sync_method($method) { | |
| 131 | + return $this->runner->set_sync_method($this->action, $method); | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 112 | 135 | * Get Status |
| 113 | 136 | */ |
| 114 | 137 | public function get_status() { |
| 115 | 138 | $status = $this->runner->status($this->action); |
| @@ -122,8 +145,13 @@ | ||
| 122 | 145 | |
| 123 | 146 | if($status['total'] <= 0) { |
| 124 | 147 | $status['total'] = $this->get_pending_media_count(); |
| 125 | 148 | } |
| 149 | + | |
| 150 | + // Signals to the frontend that this action has ajax/mixed sync mode wired up server-side | |
| 151 | + // (see BGRunner::set_callback()'s $supports_tick arg registered in the constructor above). | |
| 152 | + $status['tick_supported'] = true; | |
| 153 | + | |
| 126 | 154 | return $status; |
| 127 | 155 | } |
| 128 | 156 | |
| 129 | 157 | |