PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.1
WP-Stateless – Google Cloud Storage v3.2.1
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / cli / class-sm-cli-sync.php

class-sm-cli-sync.php in WP-Stateless – Google Cloud Storage 3.2.1, at lib/cli/class-sm-cli-sync.php

269 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * CLI Sync Implementation
5 *
6 * @author palant@UDX
7 * @author korotkov@UDX
8 */
9
10 use wpCloud\StatelessMedia\FatalException;
11 use wpCloud\StatelessMedia\UnprocessableException;
12 use wpCloud\StatelessMedia\Utility;
13
14 if (!class_exists('SM_CLI_Scaffold')) {
15 require_once(dirname(__FILE__) . '/class-sm-cli-scaffold.php');
16 }
17
18 class SM_CLI_Sync extends SM_CLI_Scaffold {
19
20 /**
21 * Order
22 */
23 public $order = "";
24
25 /**
26 * Start
27 */
28 public $start = false;
29
30 /**
31 * End
32 */
33 public $end = false;
34
35 /**
36 * Limit
37 */
38 public $limit = false;
39
40 /**
41 * Batch
42 */
43 public $batch = false;
44
45 /**
46 * Batches
47 */
48 public $batches = false;
49
50 /**
51 * Total
52 */
53 public $total = 0;
54
55 /**
56 * Log
57 */
58 public $log;
59
60 /**
61 * Sync images
62 */
63 public function images() {
64 global $wpdb;
65
66 /** Prepare arguments */
67 $this->_prepare();
68
69 $timer = time();
70
71 /** Get Total Amount of Attachments */
72 $this->total = $wpdb->get_var("
73 SELECT
74 COUNT(ID)
75 FROM {$wpdb->posts}
76 WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%'
77 ");
78
79 if ($this->batch) {
80 $this->output("Running Batch {$this->batch} from {$this->batches}");
81 $range = round($this->total / $this->batches);
82 $this->start = ($this->batch * $range) - $range;
83 $this->end = $this->batches == $this->batch ? $this->total : $this->batch * $range;
84 $this->output("Starting from {$this->start} row. ");
85 $this->output("And proceeding up to {$this->end} row.");
86 } else {
87 $this->output("Running in default way. Starting from {$this->start} row and proceeding up to end.");
88 $this->end = $this->end ? $this->end : $this->total;
89 }
90 $media_to_proceed = $this->end - $this->start;
91
92 //** Counters */
93 $synced_images = 0;
94
95 WP_CLI::line('Starting extract attachments.');
96
97 @set_time_limit(-1);
98
99 for ($this->start; $this->start < $this->end; $this->start += $this->limit) {
100
101 $limit = ($this->end - $this->start) < $this->limit ? ($this->end - $this->start) : $this->limit;
102
103 $this->output('Synced: ' . $synced_images . '. Extracting from ' . ($this->start + 1) . ' to ' . ($this->start + $limit));
104
105 /**
106 * Get Attachments data.
107 */
108 $attachments = $wpdb->get_results($wpdb->prepare("
109 SELECT ID
110 FROM {$wpdb->posts}
111 WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID {$this->order}
112 LIMIT %d, %d;
113 ", $this->start, $limit), ARRAY_A);
114
115 if (!empty($attachments)) {
116 foreach ($attachments as $a) {
117 try {
118 timer_start();
119 $response = Utility::process_image_by_id(intval($a['ID']));
120 if (!empty($response)) {
121 $synced_images++;
122 $this->output(sprintf(__('%1$s (ID %2$s) was successfully synced in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($response->ID)), $response->ID, timer_stop()));
123 }
124 } catch (FatalException $e) {
125 $this->output($e->getMessage());
126 break;
127 } catch (UnprocessableException $e) {
128 $this->output($e->getMessage());
129 } catch (\Throwable $e) {
130 $this->output($e->getMessage());
131 break;
132 }
133
134 /** Flush data */
135 $wpdb->flush();
136 @ob_flush();
137 @flush();
138 }
139
140 unset($attachments);
141 }
142 }
143 WP_CLI::success("Stateless Media is synced");
144 WP_CLI::line('Media which have been checked: ' . number_format_i18n($media_to_proceed));
145 WP_CLI::line('Synced stateless for ' . number_format_i18n($synced_images) . ' attachments');
146 WP_CLI::line('Spent Time: ' . (time() - $timer) . ' sec');
147 }
148
149
150 /**
151 * Sync files
152 */
153 public function files() {
154 global $wpdb;
155
156 /** Prepare arguments */
157 $this->_prepare();
158
159 $timer = time();
160
161 /** Get Total Amount of Attachments */
162 $this->total = $wpdb->get_var("
163 SELECT
164 COUNT(ID)
165 FROM {$wpdb->posts}
166 WHERE post_type = 'attachment' AND post_mime_type NOT LIKE 'image/%'
167 ");
168
169 if ($this->batch) {
170 $this->output("Running Batch {$this->batch} from {$this->batches}");
171 $range = round($this->total / $this->batches);
172 $this->start = ($this->batch * $range) - $range;
173 $this->end = $this->batches == $this->batch ? $this->total : $this->batch * $range;
174 $this->output("Starting from {$this->start} row. ");
175 $this->output("And proceeding up to {$this->end} row.");
176 } else {
177 $this->output("Running in default way. Starting from {$this->start} row and proceeding up to end.");
178 $this->end = $this->end ? $this->end : $this->total;
179 }
180 $media_to_proceed = $this->end - $this->start;
181
182 //** Counters */
183 $synced_files = 0;
184
185 WP_CLI::line('Starting extract attachments.');
186
187 @set_time_limit(-1);
188
189 for ($this->start; $this->start < $this->end; $this->start += $this->limit) {
190
191 $limit = ($this->end - $this->start) < $this->limit ? ($this->end - $this->start) : $this->limit;
192
193 $this->output('Synced: ' . $synced_files . '. Extracting from ' . ($this->start + 1) . ' to ' . ($this->start + $limit));
194
195 /**
196 * Get Attachments data.
197 */
198 $attachments = $wpdb->get_results($wpdb->prepare("
199 SELECT ID
200 FROM {$wpdb->posts}
201 WHERE post_type = 'attachment' AND post_mime_type NOT LIKE 'image/%' ORDER BY ID {$this->order}
202 LIMIT %d, %d;
203 ", $this->start, $limit), ARRAY_A);
204
205 if (!empty($attachments)) {
206 foreach ($attachments as $a) {
207 try {
208 timer_start();
209 $response = Utility::process_file_by_id(intval($a['ID']));
210 if (!empty($response)) {
211 $synced_files++;
212 $this->output(sprintf(__('%1$s (ID %2$s) was successfully synchronised in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($response->ID)), $response->ID, timer_stop()));
213 }
214 } catch (FatalException $e) {
215 $this->output($e->getMessage());
216 break;
217 } catch (UnprocessableException $e) {
218 $this->output($e->getMessage());
219 } catch (\Throwable $e) {
220 $this->output($e->getMessage());
221 break;
222 }
223
224 /** Flush data */
225 $wpdb->flush();
226 @ob_flush();
227 @flush();
228 }
229
230 unset($attachments);
231 }
232 }
233 WP_CLI::success("Stateless Media is synced");
234 WP_CLI::line('Media which have been checked: ' . number_format_i18n($media_to_proceed));
235 WP_CLI::line('Synced stateless for ' . number_format_i18n($synced_files) . ' attachments');
236 WP_CLI::line('Spent Time: ' . (time() - $timer) . ' sec');
237 }
238
239 /**
240 * Prepare
241 */
242 private function _prepare() {
243 $args = $this->assoc_args;
244 if (isset($args['b'])) {
245 WP_CLI::error('Invalid parameter --b. Command must not be run directly with --b parameter.');
246 }
247 $this->start = isset($args['start']) && is_numeric($args['start']) ? $args['start'] : 0;
248 $this->limit = isset($args['limit']) && is_numeric($args['limit']) ? $args['limit'] : 100;
249 $this->force = isset($args['force']) ? true : false;
250 $this->continue = isset($args['continue']) ? true : false;
251 $this->fix = isset($args['fix']) ? true : false;
252 $this->order = isset($args['order']) && strtoupper($args['order']) === 'ASC' ? 'ASC' : 'DESC';
253 if (isset($args['batch'])) {
254 if (!is_numeric($args['batch']) || $args['batch'] <= 0) {
255 WP_CLI::error('Invalid parameter --batch');
256 }
257 $this->batch = $args['batch'];
258 $this->batches = isset($args['batches']) ? $args['batches'] : 10;
259 if (!is_numeric($this->batches) || $this->batches <= 0) {
260 WP_CLI::error('Invalid parameter --batches');
261 } elseif ($this->batch > $this->batches) {
262 WP_CLI::error('--batch parameter must is invalid. It must not equal or less then --batches');
263 }
264 } else {
265 $this->end = isset($args['end']) && is_numeric($args['end']) ? $args['end'] : false;
266 }
267 }
268 }
269