PluginProbe
Docket Cache – Object Cache Accelerator / 22.07.01
Docket Cache – Object Cache Accelerator v22.07.01
26.04.05 trunk 22.07.01 22.07.02 22.07.03 22.07.04 22.07.05 23.08.01 23.08.02 24.07.01 24.07.02 24.07.03 24.07.04 24.07.05 24.07.06 24.07.07 26.04.03 26.04.04
docket-cache / includes / src / Command.php

Command.php in Docket Cache – Object Cache Accelerator 22.07.01, at includes/src/Command.php

536 lines 15.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Docket Cache.
4 *
5 * @author Nawawi Jamili
6 * @license MIT
7 *
8 * @see https://github.com/nawawi/docket-cache
9 */
10
11 namespace Nawawi\DocketCache;
12
13 \defined('ABSPATH') || exit;
14
15 use WP_CLI;
16 use WP_CLI_Command;
17
18 /**
19 * Enables, disabled, updates, and checks the status of the Docket object cache.
20 */
21 class Command extends WP_CLI_Command
22 {
23 private $pt;
24
25 public function __construct(Plugin $pt)
26 {
27 $this->pt = $pt;
28 }
29
30 private function halt_error($error)
31 {
32 WP_CLI::error($error, false);
33 WP_CLI::halt(1);
34 }
35
36 private function halt_success($success)
37 {
38 WP_CLI::success($success, false);
39 WP_CLI::halt(0);
40 }
41
42 private function halt_status($text, $status = 0)
43 {
44 WP_CLI::line($text);
45 WP_CLI::halt($status);
46 }
47
48 private function title($text, $pad = 15)
49 {
50 return str_pad($text, $pad).': ';
51 }
52
53 private function status_color($status, $text)
54 {
55 switch ($status) {
56 case 1:
57 $text = WP_CLI::colorize("%g{$text}%n");
58 break;
59 default:
60 $text = WP_CLI::colorize("%r{$text}%n");
61 break;
62 }
63
64 return $text;
65 }
66
67 private function dropino_runtime_status()
68 {
69 $info = (object) $this->pt->get_info();
70 if (2 === $info->status_code) {
71 WP_CLI::line($this->title('Cache Status').$this->status_color($info->status_code, $info->status_text));
72 unset($info);
73 WP_CLI::halt(1);
74 }
75 unset($info);
76 }
77
78 /**
79 * Display the Docket Cache status.
80 *
81 * ## EXAMPLES
82 *
83 * wp cache status
84 */
85 public function status()
86 {
87 $info = (object) $this->pt->get_info();
88 $halt = $info->status_code ? 0 : 1;
89
90 WP_CLI::line($this->title('Cache Status').$this->status_color($info->status_code, $info->status_text));
91 WP_CLI::line($this->title('Cache Path').$info->cache_path);
92 if ($this->pt->cf()->is_dctrue('STATS')) {
93 WP_CLI::line($this->title('Cache Size').$info->cache_size);
94 }
95
96 unset($info);
97
98 WP_CLI::halt($halt);
99 }
100
101 /**
102 * Enables the Docket Cache Drop-In file.
103 *
104 * Default behavior is to create the object cache Drop-In,
105 * unless an unknown object cache Drop-In is present.
106 *
107 * ## EXAMPLES
108 *
109 * wp cache dropin:enable
110 */
111 public function dropino_enable()
112 {
113 $this->dropino_runtime_status();
114
115 if ($this->pt->cx()->exists()) {
116 if ($this->pt->cx()->validate()) {
117 $this->halt_success(__('Docket object cache already enabled.', 'docket-cache'));
118 }
119
120 $this->halt_error(__('An unknown object cache Drop-In was found. To use Docket object cache, run: wp cache dropin:update.', 'docket-cache'));
121 }
122
123 if ($this->pt->cx()->install()) {
124 $this->halt_success(__('Object cache enabled.', 'docket-cache'));
125 }
126
127 $this->halt_error(__('Object cache could not be enabled.', 'docket-cache'));
128 }
129
130 /**
131 * Disables the Docket Cache Drop-In file.
132 *
133 * Default behavior is to delete the object cache Drop-In,
134 * unless an unknown object cache Drop-In is present.
135 *
136 * ## EXAMPLES
137 *
138 * wp cache dropin:disable
139 */
140 public function dropino_disable()
141 {
142 $this->dropino_runtime_status();
143
144 if (!$this->pt->cx()->exists()) {
145 $this->halt_error(__('No object cache Drop-In found.', 'docket-cache'));
146 }
147
148 if (!$this->pt->cx()->validate()) {
149 $this->halt_error(__('An unknown object cache Drop-In was found. To use Docket run: wp cache dropin:update.', 'docket-cache'));
150 }
151
152 if ($this->pt->cx()->uninstall()) {
153 $this->halt_success(__('Object cache disabled.', 'docket-cache'));
154 }
155
156 $this->halt_error(__('Object cache could not be disabled.', 'docket-cache'));
157 }
158
159 /**
160 * Updates the Docket Cache Drop-In file.
161 *
162 * Default behavior is to overwrite any existing object cache Drop-In.
163 *
164 * ## EXAMPLES
165 *
166 * wp cache update
167 *
168 * @subcommand dropin:update
169 */
170 public function dropino_update()
171 {
172 $this->dropino_runtime_status();
173
174 if ($this->pt->cx()->install()) {
175 $this->halt_success(__('Updated object cache Drop-In and enabled Docket object cache.', 'docket-cache'));
176 }
177 $this->halt_error(__('Object cache Drop-In could not be updated.', 'docket-cache'));
178 }
179
180 /**
181 * Flushes the object cache.
182 *
183 * Remove the object cache files.
184 *
185 * ## EXAMPLES
186 *
187 * wp cache flush
188 *
189 * @subcommand flush
190 */
191 public function flush_cache()
192 {
193 WP_CLI::line(__('Flushing cache. Please wait..', 'docket-cache'));
194 sleep(1);
195
196 $is_timeout = false;
197
198 $total = $this->pt->flush_cache(true, $is_timeout);
199
200 $this->pt->cx()->undelay();
201
202 if ($is_timeout) {
203 /* translators: %d = seconds */
204 $this->halt_error(sprintf(__('Process aborted. The object cache is not fully flushed. The maximum execution time of %d seconds was exceeded.', 'docket-cache'), $result));
205 }
206
207 if (empty($total)) {
208 $this->halt_error(__('The cache is empty, no cache needs to be flushed.', 'docket-cache'));
209 }
210
211 /* translators: %d = count */
212 $this->halt_success(sprintf(__('The cache was flushed. Total cache flushed: %d', 'docket-cache'), $total));
213 }
214
215 /**
216 * Removes the Docket Cache lock files.
217 *
218 * Remove lock file.
219 *
220 * ## EXAMPLES
221 *
222 * wp cache reset:lock
223 *
224 * @subcommand reset:lock
225 */
226 public function reset_lock()
227 {
228 $this->pt->co()->clear_lock();
229 $this->halt_success(__('The lock has been removed.', 'docket-cache'));
230 }
231
232 /**
233 * Reset the Docket Cache cron event.
234 *
235 * Reset cron event.
236 *
237 * ## EXAMPLES
238 *
239 * wp cache reset:cron
240 *
241 * @subcommand reset:cron
242 */
243 public function reset_cron()
244 {
245 WP_CLI::line(__('Resetting cron event. Please wait..', 'docket-cache'));
246 ( new Event($this->pt) )->reset();
247 sleep(1);
248 WP_CLI::runcommand('cron event list');
249 $this->halt_success(__('Cron event has been reset.', 'docket-cache'));
250 }
251
252 /**
253 * Removes the Docket Cache runtime code.
254 *
255 * Remove runtime code.
256 *
257 * ## EXAMPLES
258 *
259 * wp cache runtime:remove
260 *
261 * @subcommand runtime:remove
262 */
263 public function runtime_remove()
264 {
265 if (WpConfig::is_bedrock()) {
266 WP_CLI::line(__('This command does not support Bedrock. Please manually remove the runtime code.', 'docket-cache'));
267 WP_CLI::halt(1);
268 }
269
270 if (WpConfig::runtime_remove()) {
271 $this->halt_success(__('The runtime code has been removed.', 'docket-cache'));
272 }
273 $this->halt_error(__('Failed to remove runtime code.', 'docket-cache'));
274 }
275
276 /**
277 * Install the Docket Cache runtime code.
278 *
279 * Install runtime code in wp-config file.
280 *
281 * ## EXAMPLES
282 *
283 * wp cache runtime:install
284 *
285 * @subcommand runtime:install
286 */
287 public function runtime_install()
288 {
289 if (WpConfig::is_bedrock()) {
290 WP_CLI::line(__('This command does not support Bedrock. Please manually install the runtime code.', 'docket-cache'));
291 WP_CLI::halt(1);
292 }
293
294 if (WpConfig::runtime_install()) {
295 $this->halt_success(__('Updating wp-config.php file successful', 'docket-cache'));
296 }
297 $this->halt_error(__('Failed to update wp-config.php file.', 'docket-cache'));
298 }
299
300 /**
301 * Flushes the precaching files.
302 *
303 * Remove the precaching files.
304 *
305 * ## EXAMPLES
306 *
307 * wp cache flush:precache
308 *
309 * @subcommand flush:precache
310 */
311 public function flush_precache()
312 {
313 if (!\function_exists('wp_cache_flush_group')) {
314 $this->halt_error(__('Precache could not be flushed.', 'docket-cache'));
315 }
316
317 WP_CLI::line(__('Flushing precache. Please wait..', 'docket-cache'));
318 sleep(1);
319 $total = wp_cache_flush_group('docketcache-precache');
320
321 /* translators: %d = count */
322 $this->halt_success(sprintf(__('The precache was flushed. Total cache flushed: %d', 'docket-cache'), $total));
323 }
324
325 /**
326 * Flushes the transient files.
327 *
328 * Remove the transient files.
329 *
330 * ## EXAMPLES
331 *
332 * wp cache flush:transient
333 *
334 * @subcommand flush:transient
335 */
336 public function flush_transient()
337 {
338 if (!\function_exists('wp_cache_flush_group')) {
339 $this->halt_error(__('Transient could not be flushed.', 'docket-cache'));
340 }
341
342 WP_CLI::line(__('Flushing transient. Please wait..', 'docket-cache'));
343 sleep(1);
344
345 $total = wp_cache_flush_group(['transient', 'site-transient']);
346
347 /* translators: %d = couint */
348 $this->halt_success(sprintf(__('The transient was flushed. Total cache flushed: %d', 'docket-cache'), $total));
349 }
350
351 /**
352 * Flushes the Advanced Post Cache files.
353 *
354 * Remove the Advanced Post Cache files.
355 *
356 * ## EXAMPLES
357 *
358 * wp cache flush:advcpost
359 *
360 * @subcommand flush:advcpost
361 */
362 public function flush_advcpost()
363 {
364 if (!\function_exists('wp_cache_flush_group_match')) {
365 $this->halt_error(__('Advanced Post Cache could not be flushed.', 'docket-cache'));
366 }
367
368 WP_CLI::line(__('Flushing Advanced Post Cache. Please wait..', 'docket-cache'));
369 sleep(1);
370 $total = wp_cache_flush_group_match('docketcache-post');
371
372 /* translators: %d = count */
373 $this->halt_success(sprintf(__('The Advanced Post Cache was flushed. Total cache flushed: %d', 'docket-cache'), $total));
374 }
375
376 /**
377 * Flushes the Menu Cache files.
378 *
379 * Remove the Menu Cache files.
380 *
381 * ## EXAMPLES
382 *
383 * wp cache flush:menucache
384 *
385 * @subcommand flush:menucache
386 */
387 public function flush_menucache()
388 {
389 if (!\function_exists('wp_cache_flush_group')) {
390 $this->halt_error(__('Menu Cache could not be flushed.', 'docket-cache'));
391 }
392
393 WP_CLI::line(__('Flushing Menu Cache. Please wait..', 'docket-cache'));
394 sleep(1);
395 $total = wp_cache_flush_group('docketcache-menu');
396
397 /* translators: %d = count */
398 $this->halt_success(sprintf(__('The Menu Cache was flushed. Total cache flushed: %d', 'docket-cache'), $total));
399 }
400
401 /**
402 * Flushes the Translation Cache files.
403 *
404 * Remove the Translation Cache files.
405 *
406 * ## EXAMPLES
407 *
408 * wp cache flush:mocache
409 *
410 * @subcommand flush:mocache
411 */
412 public function flush_mocache()
413 {
414 if (!\function_exists('wp_cache_flush_group')) {
415 $this->halt_error(__('Translation Cache could not be flushed.', 'docket-cache'));
416 }
417
418 WP_CLI::line(__('Flushing Translation Cache. Please wait..', 'docket-cache'));
419 sleep(1);
420 $total = wp_cache_flush_group('docketcache-mo');
421
422 /* translators: %d = count */
423 $this->halt_success(sprintf(__('The Translation Cache was flushed. Total cache flushed: %d', 'docket-cache'), $total));
424 }
425
426 /**
427 * Runs all cron event.
428 *
429 * Runs all cron event.
430 *
431 * ## EXAMPLES
432 *
433 * wp cache run:cron
434 *
435 * @subcommand run:cron
436 */
437 public function run_cron()
438 {
439 WP_CLI::line(__('Executing the cron event. Please wait..', 'docket-cache'));
440 sleep(1);
441 WP_CLI::runcommand('cron event run --all');
442 }
443
444 /**
445 * Runs the Docket Cache cache stats.
446 *
447 * Collect cache stats data.
448 *
449 * ## EXAMPLES
450 *
451 * wp cache run:stats
452 *
453 * @subcommand run:stats
454 */
455 public function run_stats()
456 {
457 WP_CLI::line(__('Executing the cache stats. Please wait..', 'docket-cache'));
458 sleep(1);
459 $pad = 15;
460 $stats = $this->pt->get_cache_stats(true);
461 WP_CLI::line($this->title(__('Object size', 'docket-cache'), $pad).$this->pt->normalize_size($stats->size));
462 WP_CLI::line($this->title(__('File size', 'docket-cache'), $pad).$this->pt->normalize_size($stats->filesize));
463 WP_CLI::line($this->title(__('Total file', 'docket-cache'), $pad).$stats->files);
464 $this->halt_success(__('Executing the cache stats completed.', 'docket-cache'));
465 }
466
467 /**
468 * Runs the Docket Cache garbage collector (GC).
469 *
470 * Remove empty and older files, and execute various actions.
471 *
472 * ## EXAMPLES
473 *
474 * wp cache run:gc
475 *
476 * @subcommand run:gc
477 */
478 public function run_gc()
479 {
480 if (!has_filter('docketcache/filter/garbagecollector')) {
481 $this->halt_error(__('Garbage collector not available.', 'docket-cache'));
482 }
483
484 WP_CLI::line(__('Executing the garbage collector. Please wait..', 'docket-cache'));
485 sleep(1);
486
487 $pad = 35;
488 $collect = apply_filters('docketcache/filter/garbagecollector', true);
489
490 WP_CLI::line(str_repeat('-', $pad).':'.str_repeat('-', 10));
491 WP_CLI::line($this->title(__('Cache MaxTTL', 'docket-cache'), $pad).$collect->cache_maxttl);
492 WP_CLI::line($this->title(__('Cache File Limit', 'docket-cache'), $pad).$collect->cache_maxfile);
493 WP_CLI::line($this->title(__('Cache Disk Limit', 'docket-cache'), $pad).$this->pt->normalize_size($collect->cache_maxdisk));
494 WP_CLI::line(str_repeat('-', $pad).':'.str_repeat('-', 10));
495 WP_CLI::line($this->title(__('Cleanup Cache MaxTTL', 'docket-cache'), $pad).$collect->cleanup_maxttl);
496 WP_CLI::line($this->title(__('Cleanup Cache File Limit', 'docket-cache'), $pad).$collect->cleanup_maxfile);
497 WP_CLI::line($this->title(__('Cleanup Cache Disk Limit', 'docket-cache'), $pad).$collect->cleanup_maxdisk);
498
499 if ($collect->cleanup_expire > 0) {
500 WP_CLI::line($this->title(__('Cleanup Cache Expire', 'docket-cache'), $pad).$collect->cleanup_expire);
501 }
502
503 if ($this->pt->get_precache_maxfile() > 0 && $collect->cleanup_precache_maxfile > 0) {
504 WP_CLI::line($this->title(__('Cleanup Precache Limit', 'docket-cache'), $pad).$collect->cleanup_precache_maxfile);
505 }
506
507 if ($this->pt->cf()->is_dctrue('FLUSH_STALECACHE') && $collect->cleanup_stalecache > 0) {
508 WP_CLI::line($this->title(__('Cleanup Stale Cache', 'docket-cache'), $pad).$collect->cleanup_stalecache);
509 }
510
511 WP_CLI::line(str_repeat('-', $pad).':'.str_repeat('-', 10));
512 WP_CLI::line($this->title(__('Total Cache Cleanup', 'docket-cache'), $pad).$collect->cache_cleanup);
513 WP_CLI::line($this->title(__('Total Cache Ignored', 'docket-cache'), $pad).$collect->cache_ignore);
514 WP_CLI::line($this->title(__('Total Cache File', 'docket-cache'), $pad).$collect->cache_file);
515 WP_CLI::line(str_repeat('-', $pad).':'.str_repeat('-', 10));
516 $this->halt_success(__('Executing the garbage collector completed.', 'docket-cache'));
517 }
518
519 /**
520 * Attempts to determine which object cache is being used.
521 *
522 * Note that the guesses made by this function are based on the
523 * WP_Object_Cache classes that define the 3rd party object cache extension.
524 * Changes to those classes could render problems with this function's
525 * ability to determine which object cache is being used.
526 *
527 * ## EXAMPLES
528 *
529 * wp cache type
530 */
531 public function type()
532 {
533 $this->halt_status($this->pt->slug.' (v'.$this->pt->version().')');
534 }
535 }
536