-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathCli.php
623 lines (560 loc) · 16.7 KB
/
Cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
<?php
/**
* File: Cli.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class: W3TotalCache_Command
*
* W3 Total Cache plugin WP-CLI integration
*
* @package wp-cli
* @subpackage commands/third-party
*/
class W3TotalCache_Command extends \WP_CLI_Command {
/**
* Creates missing files, writes Apache/Nginx rules.
*
* ## OPTIONS
* [<server>]
* : Subcommand defines server type:
* apache Create rules for an Apache server
* nginx Create rules for an Nginx server
*
* @param array $args Arguments.
* @param array $vars Variables.
*/
public function fix_environment( array $args = array(), array $vars = array() ) {
$server_type = \array_shift( $args );
switch ( $server_type ) {
case 'apache':
$_SERVER['SERVER_SOFTWARE'] = 'Apache';
break;
case 'nginx':
$_SERVER['SERVER_SOFTWARE'] = 'nginx';
break;
default:
break;
}
try {
$config = Dispatcher::config();
$environment = Dispatcher::component( 'Root_Environment' );
$environment->fix_in_wpadmin( $config, true );
} catch ( Util_Environment_Exceptions $e ) {
\WP_CLI::error(
\sprintf(
// translators: 1: Error message.
\__( 'Environment adjustment failed with error: %1$s', 'w3-total-cache' ),
$e->getCombinedMessage()
)
);
}
\WP_CLI::success( \__( 'Environment adjusted.', 'w3-total-cache' ) );
}
/**
* Clear something from the cache.
*
* ## OPTIONS
* <cache>
* : Cache to flush
* all Flush all caches
* posts Flush posts (pagecache and further)
* post Flush the page cache
* database Flush the database cache
* db Flush the database cache
* object Flush the object cache
* minify Flush the minify cache
*
* [--post_id=<id>]
* : Flush a specific post ID
*
* [--permalink=<post-permalink>]
* : Flush a specific permalink
*
* ## EXAMPLES
* # Flush all
* $ wp w3-total-cache flush all
*
* # Flush pagecache and reverse proxies
* $ wp w3-total-cache flush posts
*
* @param array $args Arguments.
* @param array $vars Variables.
*/
public function flush( array $args = array(), array $vars = array() ) {
$args = \array_unique( $args );
do {
$cache_type = \array_shift( $args );
switch ( $cache_type ) {
case 'all':
try {
\w3tc_flush_all();
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Flushing all failed.', 'w3-total-cache' ) );
}
\WP_CLI::success( \__( 'Everything flushed successfully.', 'w3-total-cache' ) );
break;
case 'posts':
try {
\w3tc_flush_posts();
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Flushing posts/pages failed.', 'w3-total-cache' ) );
}
\WP_CLI::success( \__( 'Posts/pages flushed successfully.', 'w3-total-cache' ) );
break;
case 'db':
case 'database':
try {
$w3_db = Dispatcher::component( 'CacheFlush' );
$w3_db->dbcache_flush();
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Flushing the DB cache failed.', 'w3-total-cache' ) );
}
\WP_CLI::success( \__( 'The DB cache is flushed successfully.', 'w3-total-cache' ) );
break;
case 'minify':
try {
$w3_minify = Dispatcher::component( 'CacheFlush' );
$w3_minify->minifycache_flush();
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Flushing the minify cache failed.', 'w3-total-cache' ) );
}
\WP_CLI::success( \__( 'The minify cache is flushed successfully.', 'w3-total-cache' ) );
break;
case 'object':
try {
$w3_objectcache = Dispatcher::component( 'CacheFlush' );
$w3_objectcache->objectcache_flush();
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Flushing the object cache failed.', 'w3-total-cache' ) );
}
\WP_CLI::success( \__( 'The object cache is flushed successfully.', 'w3-total-cache' ) );
break;
case 'post':
if ( isset( $vars['post_id'] ) ) {
if ( \is_numeric( $vars['post_id'] ) ) {
try {
\w3tc_flush_post( $vars['post_id'], true );
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Flushing the page from cache failed.', 'w3-total-cache' ) );
}
\WP_CLI::success( \__( 'The page is flushed from cache successfully.', 'w3-total-cache' ) );
} else {
\WP_CLI::error( \__( 'This is not a valid post id.', 'w3-total-cache' ) );
}
} elseif ( isset( $vars['permalink'] ) ) {
try {
\w3tc_flush_url( $vars['permalink'] );
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Flushing the page from cache failed.', 'w3-total-cache' ) );
}
\WP_CLI::success( \__( 'The page is flushed from cache successfully.', 'w3-total-cache' ) );
} else {
if ( ! empty( $flushed_page_cache ) ) {
break;
}
try {
\w3tc_flush_posts();
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Flushing the page cache failed.', 'w3-total-cache' ) );
}
\WP_CLI::success( \__( 'The page cache is flushed successfully.', 'w3-total-cache' ) );
}
break;
default:
\WP_CLI::error( __( 'Not specified what to flush', 'w3-total-cache' ) );
}
} while ( ! empty( $args ) );
}
/**
* Get or set option.
*
* Options modifications do not update your .htaccess file automatically.
* Use the fix_environment command afterwards to do it.
*
* ## OPTIONS
* <operation>
* : Operation to do
* get Get option value
* set Set option value
* <name>
* : Option name
*
* [<value>]
* : (for set operation) Value to set
*
* [--state]
* : Use state, not config
* State is used for backend notifications
*
* [--master]
* : Use master config/state
*
* [--type=<type>]
* : Type of data used boolean/bool/string/integer/int/array/json. Default: string
*
* [--delimiter=<delimiter>]
* : Delimiter to use for array type values
*
* ## EXAMPLES
* # Get if pagecache enabled
* $ wp w3-total-cache option get pgcache.enabled --type=boolean
*
* # Rnable pagecache
* $ wp w3-total-cache option set pgcache.enabled true --type=boolean
*
* # Don't show wp-content permissions notification
* $ wp w3-total-cache option set common.hide_note_wp_content_permissions true --state --type=boolean
*
* @param array $args Arguments.
* @param array $vars Variables.
*/
public function option( array $args = array(), array $vars = array() ) {
$op = \array_shift( $args );
$name = \array_shift( $args );
$c = null;
if ( empty( $name ) ) {
\WP_CLI::error( \__( '<name> parameter is not specified', 'w3-total-cache' ) );
return;
}
if ( \strpos( $name, '::' ) !== false ) {
$name = \explode( '::', $name );
}
if ( isset( $vars['state'] ) ) {
$c = isset( $vars['master'] ) ? Dispatcher::config_state_master() : Dispatcher::config_state();
} else {
$c = isset( $vars['master'] ) ? Dispatcher::config_master() : Dispatcher::config();
}
if ( 'get' === $op ) {
$type = $vars['type'] ?? 'string';
switch ( $type ) {
case 'boolean':
case 'bool':
$v = $c->get_boolean( $name ) ? 'true' : 'false';
break;
case 'integer':
case 'int':
$v = $c->get_integer( $name );
break;
case 'string':
$v = $c->get_string( $name );
break;
case 'array':
\var_export( $c->get_array( $name ) );
echo "\n";
return;
case 'json':
echo \wp_json_encode( $c->get_array( $name ), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) . "\n";
return;
default:
\WP_CLI::error( \__( 'Unknown type ', 'w3-total-cache' ) . $type );
return;
}
echo \esc_html( $v ) . "\n";
} elseif ( 'set' === $op ) {
$type = $vars['type'] ?? 'string';
if ( count( $args ) <= 0 ) {
\WP_CLI::error( \__( '<value> parameter is not specified', 'w3-total-cache' ) );
return;
}
$value = \array_shift( $args );
switch ( $type ) {
case 'boolean':
case 'bool':
if ( 'true' === $value || '1' == $value || 'on' === $value ) {
$v = true;
} elseif ( 'false' === $value || '0' == $value || 'off' === $value ) {
$v = false;
} else {
\WP_CLI::error(
\sprintf(
// translators: 1: Value being set.
\__( '<value> parameter "%1$s" is not boolean', 'w3-total-cache' ),
$value
)
);
return;
}
break;
case 'integer':
case 'int':
$v = (int) $value;
break;
case 'string':
$v = $value;
break;
case 'array':
$delimiter = $vars['delimiter'] ?? ',';
$v = \explode( $delimiter, $value );
break;
case 'json':
$v = \json_decode( $value );
break;
default:
\WP_CLI::error( \__( 'Unknown type ', 'w3-total-cache' ) . $type );
return;
}
try {
$c->set( $name, $v );
$c->save();
\WP_CLI::success( \__( 'Option updated successfully.', 'w3-total-cache' ) );
} catch ( \Exception $e ) {
\WP_CLI::error( \__( 'Option value update failed.', 'w3-total-cache' ) );
}
} else {
\WP_CLI::error( \__( '<operation> parameter is not specified', 'w3-total-cache' ) );
}
}
/**
* Import a configuration file
*
* ## OPTIONS
* <filename>
* : Filename to import
*
* @global $wp_filesystem
* @see get_filesystem_method()
*
* @param array $args Arguments.
* @param array $vars Variables.
* @throws \Exception Exception.
*/
public function import( array $args = array(), array $vars = array() ) {
if ( 'direct' !== \get_filesystem_method() ) {
\WP_CLI::error( \__( 'The filesystem must be direct.', 'w3-total-cache' ) );
}
$filename = \array_shift( $args );
// Initialize WP_Filesystem.
global $wp_filesystem;
WP_Filesystem();
try {
$config = new Config();
if ( ! $wp_filesystem->exists( $filename ) || ! $wp_filesystem->is_readable( $filename ) ) {
throw new \Exception( \__( 'Cant read file: ', 'w3-total-cache' ) . $filename );
}
if ( ! $config->import( $filename ) ) {
throw new \Exception( \__( 'Import failed', 'w3-total-cache' ) );
}
$config->save();
} catch ( \Exception $e ) {
\WP_CLI::error(
sprintf(
// translators: 1: Error message.
\__( 'Config import failed: %1$s', 'w3-total-cache' ),
$e->getMessage()
)
);
}
\WP_CLI::success( \__( 'Configuration successfully imported.', 'w3-total-cache' ) );
}
/**
* Export configuration file
*
* ## OPTIONS
* <filename>
* : Filename to export -- Sanitized with sanitize_file_name()
*
* [--mode=<mode>]
* : Mode of the file. Default: 0600 (-rw-------)
*
* @global $wp_filesystem
* @see get_filesystem_method()
*
* @param array $args Arguments.
* @param array $vars Variables.
* @throws \Exception Exception.
*/
public function export( array $args = array(), array $vars = array() ) {
if ( 'direct' !== \get_filesystem_method() ) {
\WP_CLI::error( \__( 'The filesystem must be direct.', 'w3-total-cache' ) );
}
$filename = \sanitize_file_name( \array_shift( $args ) );
$mode = $vars['mode'] ?? '0600';
// Initialize WP_Filesystem.
global $wp_filesystem;
WP_Filesystem();
// Try to export the config and write to file.
try {
$config = new Config();
if ( ! $wp_filesystem->put_contents( $filename, $config->export( $filename ), octdec( $mode ) ) ) {
throw new \Exception( \__( 'Export failed', 'w3-total-cache' ) );
}
} catch ( \Exception $e ) {
\WP_CLI::error(
sprintf(
// translators: 1: Error message.
\__( 'Config export failed: %1$s', 'w3-total-cache' ),
$e->getMessage()
)
);
}
\WP_CLI::success(
\sprintf(
// translators: 1: Filename.
\__( 'Configuration successfully exported to "%1$s" with mode "%2$s".', 'w3-total-cache' ),
$filename,
$mode
)
);
}
/**
* Update query string for all static files.
*/
public function querystring() {
try {
$w3_querystring = Dispatcher::component( 'CacheFlush' );
$w3_querystring->browsercache_flush();
} catch ( \Exception $e ) {
\WP_CLI::error(
sprintf(
// translators: 1: Error message.
\__( 'updating the query string failed. with error %1$s', 'w3-total-cache' ),
$e->getMessage()
)
);
}
\WP_CLI::success( \__( 'The query string was updated successfully.', 'w3-total-cache' ) );
}
/**
* Purges URLs from CDN and varnish if enabled.
*
* @param array $args List of files to be purged, absolute path or relative to WordPress installation path.
*/
public function cdn_purge( array $args = array() ) {
$purgeitems = array();
foreach ( $args as $file ) {
$cdncommon = Dispatcher::component( 'Cdn_Core' );
if ( file_exists( $file ) ) {
$local_path = $file;
} else {
$local_path = ABSPATH . $file;
}
$remote_path = $file;
$purgeitems[] = $cdncommon->build_file_descriptor( $local_path, $remote_path );
}
try {
$w3_cdn_purge = Dispatcher::component( 'CacheFlush' );
$w3_cdn_purge->cdn_purge_files( $purgeitems );
} catch ( \Exception $e ) {
\WP_CLI::error(
\sprintf(
// translators: 1: Error message.
\__( 'Files did not successfully purge with error %1$s', 'w3-total-cache' ),
$e->getMessage()
)
);
}
\WP_CLI::success( \__( 'Files purged successfully.', 'w3-total-cache' ) );
}
/**
* Generally triggered from a cronjob, performs manual page cache Garbage collection.
*/
public function pgcache_cleanup() {
try {
$o = Dispatcher::component( 'PgCache_Plugin_Admin' );
$o->cleanup();
} catch ( \Exception $e ) {
\WP_CLI::error(
\sprintf(
// translators: 1: Error message.
\__( 'PageCache Garbage cleanup failed: %1$s', 'w3-total-cache' ),
$e->getMessage()
)
);
}
\WP_CLI::success( \__( 'PageCache Garbage cleanup triggered successfully.', 'w3-total-cache' ) );
}
/**
* Generally triggered from a cronjob, performs manual page cache priming
* ## OPTIONS
* [--start=<start>]
* : Start since <start> entry of sitemap
*
* [--limit=<limit>]
* : load no more than <limit> pages
*
* @param array $args Arguments.
* @param array $vars Variables.
*/
public function pgcache_prime( array $args = array(), array $vars = array() ) {
try {
$log_callback = function( $m ) {
\WP_CLI::log( $m );
};
$o = Dispatcher::component( 'PgCache_Plugin_Admin' );
$o->prime(
( isset( $vars['start'] ) ? $vars['start'] - 1 : null ),
( isset( $vars['limit'] ) ? $vars['limit'] : null ),
$log_callback
);
} catch ( \Exception $e ) {
\WP_CLI::error(
\sprintf(
// translators: 1: Error message.
\__( 'PageCache Priming did failed: %1$s', 'w3-total-cache' ),
$e->getMessage()
)
);
}
\WP_CLI::success( \__( 'PageCache Priming triggered successfully.', 'w3-total-cache' ) );
}
/**
* Generally triggered from a cronjob, processes always cached queue.
*/
public function alwayscached_process() {
if ( ! Extension_AlwaysCached_Plugin::is_enabled() ) {
\WP_CLI::error(
\__( 'Always Cached feature is not enabled.', 'w3-total-cache' )
);
return;
}
try {
Extension_AlwaysCached_Worker::run( false );
} catch ( \Exception $e ) {
\WP_CLI::error(
\sprintf(
// translators: 1: Error message.
\__( 'Always Cached queue processer failed: %1$s', 'w3-total-cache' ),
$e->getMessage()
)
);
}
\WP_CLI::success( \__( 'Always Cached queue processed successfully.', 'w3-total-cache' ) );
}
/**
* Generally triggered from a cronjob, processes AlwaysCached queue.
*/
public function alwayscached_clear() {
if ( ! Extension_AlwaysCached_Plugin::is_enabled() ) {
\WP_CLI::error(
\__( 'Always Cached feature is not enabled', 'w3-total-cache' )
);
return;
}
try {
Extension_AlwaysCached_Queue::empty();
} catch ( \Exception $e ) {
\WP_CLI::error(
\sprintf(
// translators: 1: Error message.
\__( 'Always Cached queue empty failed: %1$s', 'w3-total-cache' ),
$e->getMessage()
)
);
}
\WP_CLI::success( \__( 'Always Cached queue emptied successfully.', 'w3-total-cache' ) );
}
}
// Register WP-CLI commands.
if ( \method_exists( '\WP_CLI', 'add_command' ) ) {
\WP_CLI::add_command( 'w3-total-cache', '\W3TC\W3TotalCache_Command', array( 'shortdesc' => __( 'Manage W3TC settings, flush, and prime the cache.', 'w3-total-cache' ) ) );
\WP_CLI::add_command( 'total-cache', '\W3TC\W3TotalCache_Command', array( 'shortdesc' => __( 'Manage W3TC settings, flush, and prime the cache.', 'w3-total-cache' ) ) );
\WP_CLI::add_command( 'w3tc', '\W3TC\W3TotalCache_Command', array( 'shortdesc' => __( 'Manage W3TC settings, flush, and prime the cache.', 'w3-total-cache' ) ) );
} else {
// Backward compatibility.
\WP_CLI::addCommand( 'w3-total-cache', '\W3TC\W3TotalCache_Command' );
\WP_CLI::addCommand( 'total-cache', '\W3TC\W3TotalCache_Command' );
\WP_CLI::addCommand( 'w3tc', '\W3TC\W3TotalCache_Command' );
}