PluginProbe
Media Cloud Sync / 1.3.10
Media Cloud Sync v1.3.10
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 1.3.10, at includes/bulk/sync-to-cloud.php

193 lines 4.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']);
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
41 $all_media_count = Counter::get_count( 'all', false );
42
43 $is_failed = false;
44
45 if(!empty($all_media_count)) {
46 foreach($all_media_count as $source_type => $counts) {
47 $total = (int)$counts['total'];
48 $uploaded = (int)$counts['uploaded'];
49 $failed = isset($failed_count[$source_type]) ? $failed_count[$source_type] : 0;
50
51 $pending = $total - ($uploaded + $failed);
52 if($pending > 0) {
53 $handler = Integration::instance()->get_handler_class($source_type);
54 if ($handler) {
55 $result = $handler::instance()->upload_pending_media($source_type, 1, $failed);
56
57 if(isset($result['failed']) && !empty($result['failed']) && $result['failed'] > 0) {
58 $failed_count[$source_type] = $failed + $result['failed'];
59 $is_failed = true;
60 }
61 break;
62 }
63 }
64 }
65 }
66
67 if(($offset+1) == $total_iterations) {
68 $this->clear_state();
69 } else {
70 $state = [
71 'failed' => $failed_count
72 ];
73 $this->set_state($state);
74 }
75
76 return !$is_failed;
77 }
78
79 /**
80 * Start the action.
81 */
82 public function start() {
83 // Fetch and update media counts for make sure they are up to date
84 Counter::fetch_and_update();
85
86 // Start the action
87 $this->runner->start($this->action, $this->get_pending_media_count());
88 }
89
90 /**
91 * Stop the action.
92 */
93 public function stop() {
94 $this->runner->stop($this->action);
95 }
96
97 /**
98 * Pause the action.
99 */
100 public function pause() {
101 $this->runner->pause($this->action);
102 }
103
104 /**
105 * Resume the action.
106 */
107 public function resume() {
108 $this->runner->resume($this->action);
109 }
110
111 /**
112 * Get Status
113 */
114 public function get_status() {
115 $status = $this->runner->status($this->action);
116 // Clear state if stopped or completed
117 if(isset($status['status'])) {
118 if($status['status'] == 'stopped' || $status['status'] == 'completed') {
119 $this->clear_state();
120 }
121 }
122
123 if($status['total'] <= 0) {
124 $status['total'] = $this->get_pending_media_count();
125 }
126 return $status;
127 }
128
129
130 /**
131 * Retry Single Sync
132 * @since 1.3.3
133 */
134 public function retry_single($id, $source_type = 'media_library') {
135 $handler = Integration::instance()->get_handler_class($source_type);
136 if ($handler) {
137 $handler::instance()->upload_single_media($id, $source_type);
138 }
139 }
140
141
142 /**
143 * Set State
144 */
145 private function set_state($state) {
146 set_transient( $this->state_key, $state, WEEK_IN_SECONDS );
147 }
148
149 /**
150 * Get State
151 */
152 private function get_state() {
153 $state = get_transient( $this->state_key );
154 if ( ! is_array( $state ) ) {
155 $state = [];
156 }
157
158 return $state;
159 }
160
161
162 /**
163 * Get pending Media count
164 */
165 private function get_pending_media_count() {
166 $counts = Counter::get_count();
167
168 $total = isset($counts['total']) ? (int)$counts['total'] : 0;
169 $uploaded = isset($counts['uploaded']) ? (int)$counts['uploaded'] : 0;
170 $pending = $total - $uploaded;
171
172 return $pending;
173 }
174
175
176 /**
177 * Clear State
178 */
179 private function clear_state() {
180 delete_transient( $this->state_key );
181 }
182
183
184 /**
185 * Retrieves the singleton instance of the class.
186 *
187 * @return self
188 */
189 public static function instance() {
190 if (!self::$instance) self::$instance = new self();
191 return self::$instance;
192 }
193 }