PluginProbe
Media Cloud Sync / trunk
Media Cloud Sync vtrunk
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / bulk / sync-to-cloud.php

sync-to-cloud.php in Media Cloud Sync trunk, at includes/bulk/sync-to-cloud.php

221 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3 defined('ABSPATH') || exit;
4
5
6 class SyncToCloud {
7 private static $instance = null;
8 private $state_key;
9
10 private $action = 'sync_to_cloud';
11
12 private $runner;
13
14 /**
15 * Constructor method.
16 */
17 public function __construct() {
18 $this->state_key = WPMCS_TOKEN . '_' . $this->action . '_state';
19 $this->runner = BGRunner::instance();
20 $this->runner->set_callback($this->action, [$this, 'sync_to_cloud'], true);
21 }
22
23 /**
24 * Get Action
25 */
26 public function get_action() {
27 return $this->action;
28 }
29
30 /**
31 * Syncs to Cloud.
32 */
33 public function sync_to_cloud($offset = 0, $total_iterations = 0) {
34 if($offset == 0) {
35 // Clear previous state
36 $this->clear_state();
37 }
38 $state = $this->get_state();
39 $failed_count = isset($state['failed']) ? $state['failed'] : [];
40 $last_ids = isset($state['last_ids']) ? $state['last_ids'] : [];
41
42 $all_media_count = Counter::get_count( 'all', false );
43
44 $is_failed = false;
45
46 if(!empty($all_media_count)) {
47 foreach($all_media_count as $source_type => $counts) {
48 $total = (int)$counts['total'];
49 $uploaded = (int)$counts['uploaded'];
50 $failed = isset($failed_count[$source_type]) ? $failed_count[$source_type] : 0;
51
52 $pending = $total - ($uploaded + $failed);
53 if($pending > 0) {
54 $handler = Integration::instance()->get_handler_class($source_type);
55 if ($handler) {
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);
58
59 if (isset($result['last_id'])) {
60 $last_ids[$source_type] = (int) $result['last_id'];
61 }
62
63 if(isset($result['failed']) && !empty($result['failed']) && $result['failed'] > 0) {
64 $failed_count[$source_type] = $failed + $result['failed'];
65 $is_failed = true;
66 }
67 break;
68 }
69 }
70 }
71 }
72
73 if(($offset+1) == $total_iterations) {
74 $this->clear_state();
75 } else {
76 $state = [
77 'failed' => $failed_count,
78 'last_ids' => $last_ids,
79 ];
80 $this->set_state($state);
81 }
82
83 return !$is_failed;
84 }
85
86 /**
87 * Start the action.
88 */
89 public function start() {
90 // Fetch and update media counts for make sure they are up to date
91 Counter::fetch_and_update();
92
93 // Start the action
94 $this->runner->start($this->action, $this->get_pending_media_count());
95 }
96
97 /**
98 * Stop the action.
99 */
100 public function stop() {
101 $this->runner->stop($this->action);
102 }
103
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 /**
114 * Pause the action.
115 */
116 public function pause() {
117 $this->runner->pause($this->action);
118 }
119
120 /**
121 * Resume the action.
122 */
123 public function resume() {
124 $this->runner->resume($this->action);
125 }
126
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 /**
135 * Get Status
136 */
137 public function get_status() {
138 $status = $this->runner->status($this->action);
139 // Clear state if stopped or completed
140 if(isset($status['status'])) {
141 if($status['status'] == 'stopped' || $status['status'] == 'completed') {
142 $this->clear_state();
143 }
144 }
145
146 if($status['total'] <= 0) {
147 $status['total'] = $this->get_pending_media_count();
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
154 return $status;
155 }
156
157
158 /**
159 * Retry Single Sync
160 * @since 1.3.3
161 */
162 public function retry_single($id, $source_type = 'media_library') {
163 $handler = Integration::instance()->get_handler_class($source_type);
164 if ($handler) {
165 $handler::instance()->upload_single_media($id, $source_type);
166 }
167 }
168
169
170 /**
171 * Set State
172 */
173 private function set_state($state) {
174 set_transient( $this->state_key, $state, WEEK_IN_SECONDS );
175 }
176
177 /**
178 * Get State
179 */
180 private function get_state() {
181 $state = get_transient( $this->state_key );
182 if ( ! is_array( $state ) ) {
183 $state = [];
184 }
185
186 return $state;
187 }
188
189
190 /**
191 * Get pending Media count
192 */
193 private function get_pending_media_count() {
194 $counts = Counter::get_count();
195
196 $total = isset($counts['total']) ? (int)$counts['total'] : 0;
197 $uploaded = isset($counts['uploaded']) ? (int)$counts['uploaded'] : 0;
198 $pending = $total - $uploaded;
199
200 return $pending;
201 }
202
203
204 /**
205 * Clear State
206 */
207 private function clear_state() {
208 delete_transient( $this->state_key );
209 }
210
211
212 /**
213 * Retrieves the singleton instance of the class.
214 *
215 * @return self
216 */
217 public static function instance() {
218 if (!self::$instance) self::$instance = new self();
219 return self::$instance;
220 }
221 }