PluginProbe
Media Cloud Sync / 1.3.0
Media Cloud Sync v1.3.0
1.4.1 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 All 35 releases
media-cloud-sync / includes / bulk / sync-to-cloud.php

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

171 lines 4.2 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 $token;
9
10 private $state_key = 'dw_sync_to_cloud_state';
11
12 private $action = 'sync_to_cloud';
13
14 private $runner;
15
16 /**
17 * Constructor method.
18 */
19 public function __construct() {
20 $this->token = WPMCS_TOKEN;
21
22 $this->runner = BGRunner::instance();
23 $this->runner->set_callback($this->action, [$this, 'sync_to_cloud']);
24 }
25
26 /**
27 * Get Action
28 */
29 public function get_action() {
30 return $this->action;
31 }
32
33 /**
34 * Syncs to Cloud.
35 */
36 public function sync_to_cloud($offset = 0, $failed_count = 0, $total_iterations = 0) {
37 if($offset == 0) {
38 // Clear previous state
39 $this->clear_state();
40 }
41 $state = $this->get_state();
42 $failed_count = isset($state['failed']) ? $state['failed'] : [];
43
44 $all_media_count = Counter::get_count( 'all', false );
45
46 $is_failed = false;
47
48 if(!empty($all_media_count)) {
49 foreach($all_media_count as $source_type => $counts) {
50 $total = (int)$counts['total'];
51 $uploaded = (int)$counts['uploaded'];
52 $failed = isset($failed_count[$source_type]) ? $failed_count[$source_type] : 0;
53
54 $pending = $total - ($uploaded + $failed);
55 if($pending > 0) {
56 $handler = Integration::instance()->get_handler_class($source_type);
57 if ($handler) {
58 $result = $handler::instance()->upload_pending_media($source_type, 1, $failed);
59
60 if(isset($result['failed']) && !empty($result['failed']) && $result['failed'] > 0) {
61 $failed_count[$source_type] = $failed + $result['failed'];
62 $is_failed = true;
63 }
64 break;
65 }
66 }
67 }
68 }
69
70 if(($offset+1) == $total_iterations) {
71 $this->clear_state();
72 } else {
73 $state = [
74 'failed' => $failed_count
75 ];
76 $this->set_state($state);
77 }
78
79 return !$is_failed;
80 }
81
82 /**
83 * Start the action.
84 */
85 public function start() {
86 // Fetch and update media counts for make sure they are up to date
87 Counter::fetch_and_update();
88
89 $counts = Counter::get_count();
90
91 $total = isset($counts['total']) ? (int)$counts['total'] : 0;
92 $uploaded = isset($counts['uploaded']) ? (int)$counts['uploaded'] : 0;
93 $pending = $total - $uploaded;
94
95 // Start the action
96 $this->runner->start($this->action, $pending);
97 }
98
99 /**
100 * Stop the action.
101 */
102 public function stop() {
103 $this->runner->stop($this->action);
104 }
105
106 /**
107 * Pause the action.
108 */
109 public function pause() {
110 $this->runner->pause($this->action);
111 }
112
113 /**
114 * Resume the action.
115 */
116 public function resume() {
117 $this->runner->resume($this->action);
118 }
119
120 /**
121 * Get Status
122 */
123 public function get_status() {
124 $status = $this->runner->status($this->action);
125 // Clear state if stopped or completed
126 if(isset($status['status'])) {
127 if($status['status'] == 'stopped' || $status['status'] == 'completed') {
128 $this->clear_state();
129 }
130 }
131 return $status;
132 }
133
134
135 /**
136 * Set State
137 */
138 private function set_state($state) {
139 set_transient( $this->state_key, $state, WEEK_IN_SECONDS );
140 }
141
142 /**
143 * Get State
144 */
145 private function get_state() {
146 $state = get_transient( $this->state_key );
147 if ( ! is_array( $state ) ) {
148 $state = [];
149 }
150
151 return $state;
152 }
153
154 /**
155 * Clear State
156 */
157 public function clear_state() {
158 delete_transient( $this->state_key );
159 }
160
161
162 /**
163 * Retrieves the singleton instance of the class.
164 *
165 * @return self
166 */
167 public static function instance() {
168 if (!self::$instance) self::$instance = new self();
169 return self::$instance;
170 }
171 }