PluginProbe
WP-Stateless – Google Cloud Storage / 3.1.1
WP-Stateless – Google Cloud Storage v3.1.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.1.1, at lib/cli/class-sm-cli-sync.php

271 lines 8.4 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 @error_reporting(0);
98 @set_time_limit(-1);
99
100 for ($this->start; $this->start < $this->end; $this->start += $this->limit) {
101
102 $limit = ($this->end - $this->start) < $this->limit ? ($this->end - $this->start) : $this->limit;
103
104 $this->output('Synced: ' . $synced_images . '. Extracting from ' . ($this->start + 1) . ' to ' . ($this->start + $limit));
105
106 /**
107 * Get Attachments data.
108 */
109 $attachments = $wpdb->get_results($wpdb->prepare("
110 SELECT ID
111 FROM {$wpdb->posts}
112 WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID {$this->order}
113 LIMIT %d, %d;
114 ", $this->start, $limit), ARRAY_A);
115
116 if (!empty($attachments)) {
117 foreach ($attachments as $a) {
118 try {
119 timer_start();
120 $response = Utility::process_image_by_id(intval($a['ID']));
121 if (!empty($response)) {
122 $synced_images++;
123 $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()));
124 }
125 } catch (FatalException $e) {
126 $this->output($e->getMessage());
127 break;
128 } catch (UnprocessableException $e) {
129 $this->output($e->getMessage());
130 } catch (\Throwable $e) {
131 $this->output($e->getMessage());
132 break;
133 }
134
135 /** Flush data */
136 $wpdb->flush();
137 @ob_flush();
138 @flush();
139 }
140
141 unset($attachments);
142 }
143 }
144 WP_CLI::success("Stateless Media is synced");
145 WP_CLI::line('Media which have been checked: ' . number_format_i18n($media_to_proceed));
146 WP_CLI::line('Synced stateless for ' . number_format_i18n($synced_images) . ' attachments');
147 WP_CLI::line('Spent Time: ' . (time() - $timer) . ' sec');
148 }
149
150
151 /**
152 * Sync files
153 */
154 public function files() {
155 global $wpdb;
156
157 /** Prepare arguments */
158 $this->_prepare();
159
160 $timer = time();
161
162 /** Get Total Amount of Attachments */
163 $this->total = $wpdb->get_var("
164 SELECT
165 COUNT(ID)
166 FROM {$wpdb->posts}
167 WHERE post_type = 'attachment' AND post_mime_type NOT LIKE 'image/%'
168 ");
169
170 if ($this->batch) {
171 $this->output("Running Batch {$this->batch} from {$this->batches}");
172 $range = round($this->total / $this->batches);
173 $this->start = ($this->batch * $range) - $range;
174 $this->end = $this->batches == $this->batch ? $this->total : $this->batch * $range;
175 $this->output("Starting from {$this->start} row. ");
176 $this->output("And proceeding up to {$this->end} row.");
177 } else {
178 $this->output("Running in default way. Starting from {$this->start} row and proceeding up to end.");
179 $this->end = $this->end ? $this->end : $this->total;
180 }
181 $media_to_proceed = $this->end - $this->start;
182
183 //** Counters */
184 $synced_files = 0;
185
186 WP_CLI::line('Starting extract attachments.');
187
188 @error_reporting(0);
189 @set_time_limit(-1);
190
191 for ($this->start; $this->start < $this->end; $this->start += $this->limit) {
192
193 $limit = ($this->end - $this->start) < $this->limit ? ($this->end - $this->start) : $this->limit;
194
195 $this->output('Synced: ' . $synced_files . '. Extracting from ' . ($this->start + 1) . ' to ' . ($this->start + $limit));
196
197 /**
198 * Get Attachments data.
199 */
200 $attachments = $wpdb->get_results($wpdb->prepare("
201 SELECT ID
202 FROM {$wpdb->posts}
203 WHERE post_type = 'attachment' AND post_mime_type NOT LIKE 'image/%' ORDER BY ID {$this->order}
204 LIMIT %d, %d;
205 ", $this->start, $limit), ARRAY_A);
206
207 if (!empty($attachments)) {
208 foreach ($attachments as $a) {
209 try {
210 timer_start();
211 $response = Utility::process_file_by_id(intval($a['ID']));
212 if (!empty($response)) {
213 $synced_files++;
214 $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()));
215 }
216 } catch (FatalException $e) {
217 $this->output($e->getMessage());
218 break;
219 } catch (UnprocessableException $e) {
220 $this->output($e->getMessage());
221 } catch (\Throwable $e) {
222 $this->output($e->getMessage());
223 break;
224 }
225
226 /** Flush data */
227 $wpdb->flush();
228 @ob_flush();
229 @flush();
230 }
231
232 unset($attachments);
233 }
234 }
235 WP_CLI::success("Stateless Media is synced");
236 WP_CLI::line('Media which have been checked: ' . number_format_i18n($media_to_proceed));
237 WP_CLI::line('Synced stateless for ' . number_format_i18n($synced_files) . ' attachments');
238 WP_CLI::line('Spent Time: ' . (time() - $timer) . ' sec');
239 }
240
241 /**
242 * Prepare
243 */
244 private function _prepare() {
245 $args = $this->assoc_args;
246 if (isset($args['b'])) {
247 WP_CLI::error('Invalid parameter --b. Command must not be run directly with --b parameter.');
248 }
249 $this->start = isset($args['start']) && is_numeric($args['start']) ? $args['start'] : 0;
250 $this->limit = isset($args['limit']) && is_numeric($args['limit']) ? $args['limit'] : 100;
251 $this->force = isset($args['force']) ? true : false;
252 $this->continue = isset($args['continue']) ? true : false;
253 $this->fix = isset($args['fix']) ? true : false;
254 $this->order = isset($args['order']) && strtoupper($args['order']) === 'ASC' ? 'ASC' : 'DESC';
255 if (isset($args['batch'])) {
256 if (!is_numeric($args['batch']) || $args['batch'] <= 0) {
257 WP_CLI::error('Invalid parameter --batch');
258 }
259 $this->batch = $args['batch'];
260 $this->batches = isset($args['batches']) ? $args['batches'] : 10;
261 if (!is_numeric($this->batches) || $this->batches <= 0) {
262 WP_CLI::error('Invalid parameter --batches');
263 } elseif ($this->batch > $this->batches) {
264 WP_CLI::error('--batch parameter must is invalid. It must not equal or less then --batches');
265 }
266 } else {
267 $this->end = isset($args['end']) && is_numeric($args['end']) ? $args['end'] : false;
268 }
269 }
270 }
271