Cli.php
10 KB
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
<?php
namespace W3TC;
/**
* The W3 Total Cache plugin
*
* @package wp-cli
* @subpackage commands/third-party
*/
class W3TotalCache_Command extends \WP_CLI_Command {
/**
* Clear something from the cache
*
* @param array $args
* @param array $vars
*/
function flush( $args = array(), $vars = array() ) {
$args = array_unique( $args );
do {
$cache_type = array_shift( $args );
switch ( $cache_type ) {
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':
default:
if ( isset( $vars['post_id'] ) ) {
if ( is_numeric( $vars['post_id'] ) ) {
try {
$w3_cacheflush = Dispatcher::component( 'CacheFlush' );
$w3_cacheflush->flush_post( $vars['post_id'] );
}
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' ) );
}
w3tc_flush_post( $vars['post_id'] );
}
elseif ( isset( $vars['permalink'] ) ) {
$id = url_to_postid( $vars['permalink'] );
if ( is_numeric( $id ) ) {
try {
$w3_cacheflush = Dispatcher::component( 'CacheFlush' );
$w3_cacheflush->flush_post( $id );
}
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( __( 'There is no post with this permalink.', 'w3-total-cache' ) );
}
} else {
if ( isset( $flushed_page_cache ) && $flushed_page_cache )
break;
$flushed_page_cache = true;
try {
$w3_cacheflush = Dispatcher::component( 'CacheFlush' );
$w3_cacheflush->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' ) );
}
}
} while ( !empty( $args ) );
}
/**
* Update query string function
*/
function querystring() {
try {
$w3_querystring = Dispatcher::component( 'CacheFlush' );
$w3_querystring->browsercache_flush();
}
catch ( \Exception $e ) {
\WP_CLI::error( sprintf(
__( 'updating the query string failed. with error %s', 'w3-total-cache' ),
$e ) );
}
\WP_CLI::success( __( 'The query string was updated successfully.', 'w3-total-cache' ) );
}
/**
* Purge URL's from cdn and varnish if enabled
*
* @param array $args
*/
function cdn_purge( $args = array() ) {
$purgeitems = array();
foreach ( $args as $file ) {
$cdncommon = Dispatcher::component( 'Cdn_Core' );
$local_path = WP_ROOT . $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( __( 'Files did not successfully purge with error %s', 'w3-total-cache' ), $e );
}
\WP_CLI::success( __( 'Files purged successfully.', 'w3-total-cache' ) );
}
/**
* Tell opcache to reload PHP files
*
* @param array $args
*/
function opcache_flush_file( $args = array() ) {
try {
$method = array_shift( $args );
if ( !in_array( $method, array( 'SNS', 'local' ) ) )
\WP_CLI::error( $method . __( ' is not supported. Change to SNS or local to reload opcache files', 'w3-total-cache' ) );
if ( $method == 'SNS' ) {
$w3_cache = Dispatcher::component( 'CacheFlush' );
$w3_cache->opcache_flush_file( $args[0] );
} else {
$url = WP_PLUGIN_URL . '/' . dirname( W3TC_FILE ) . '/pub/opcache.php';
$path = parse_url( $url, PHP_URL_PATH );
$post = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'body' => array(
'nonce' => wp_hash( $path ),
'command' => 'flush_file',
'file' => $args[0]
),
);
$result = wp_remote_post( $url, $post );
if ( is_wp_error( $result ) ) {
\WP_CLI::error( __( 'Files did not successfully reload with error %s', 'w3-total-cache' ), $result );
} elseif ( $result['response']['code'] != '200' ) {
\WP_CLI::error( __( 'Files did not successfully reload with message: ', 'w3-total-cache' ) . $result['body'] );
}
}
}
catch ( \Exception $e ) {
\WP_CLI::error( __( 'Files did not successfully reload with error %s', 'w3-total-cache' ), $e );
}
\WP_CLI::success( __( 'Files reloaded successfully.', 'w3-total-cache' ) );
}
/**
* Tell opcache to reload PHP files
*
* @param array $args
*/
function opcache_flush( $args = array() ) {
try {
$method = array_shift( $args );
if ( !in_array( $method, array( 'SNS', 'local' ) ) )
\WP_CLI::error( $method . __( ' is not supported. Change to SNS or local to delete opcache files', 'w3-total-cache' ) );
if ( $method == 'SNS' ) {
$w3_cache = Dispatcher::component( 'CacheFlush' );
$w3_cache->opcache_flush();
} else {
$url = WP_PLUGIN_URL . '/' . dirname( W3TC_FILE ) . '/pub/opcache.php';
$path = parse_url( $url, PHP_URL_PATH );
$post = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'body' => array(
'nonce' => wp_hash( $path ),
'command' => 'flush'
),
);
$result = wp_remote_post( $url, $post );
if ( is_wp_error( $result ) ) {
\WP_CLI::error( __( 'Files did not successfully delete with error %s', 'w3-total-cache' ), $result );
} elseif ( $result['response']['code'] != '200' ) {
\WP_CLI::error( __( 'Files did not successfully delete with message: ', 'w3-total-cache' ). $result['body'] );
}
}
}
catch ( \Exception $e ) {
\WP_CLI::error( __( 'Files did not successfully delete with error %s', 'w3-total-cache' ), $e );
}
\WP_CLI::success( __( 'Files deleted successfully.', 'w3-total-cache' ) );
}
/**
* triggers PgCache Garbage Cleanup
*/
function pgcache_cleanup() {
try {
$pgcache_cleanup = Dispatcher::component( 'PgCache_Plugin_Admin' );
$pgcache_cleanup->cleanup();
} catch ( \Exception $e ) {
\WP_CLI::error( __( 'PageCache Garbage cleanup did not start with error %s',
'w3-total-cache' ), $e );
}
\WP_CLI::success( __( 'PageCache Garbage cleanup triggered successfully.',
'w3-total-cache' ) );
}
/**
* triggers PgCache Garbage Cleanup
*/
function fix_environment( $args = 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;
}
try {
$config = Dispatcher::config();
$environment = Dispatcher::component( 'Root_Environment' );
$environment->fix_in_wpadmin( $config, true );
} catch ( Util_Environment_Exceptions $e ) {
\WP_CLI::error( __( 'Environment adjustment failed with error', 'w3-total-cache' ),
$e->getCombinedMessage() );
}
\WP_CLI::success( __( 'Environment adjusted.', 'w3-total-cache' ) );
}
/**
* Help function for this command
*/
public static function help() {
\WP_CLI::line( <<<EOB
usage: wp w3-total-cache flush [post|database|minify|object] [--post_id=<post-id>] [--permalink=<post-permalink>]
or : wp w3-total-cache querystring
or : wp w3-total-cache cdn_purge <file> [<file2>]...
or : wp w3-total-cache pgcache_cleanup
flush flushes whole cache or specific items based on provided arguments
querystring update query string for all static files
cdn_purge Purges command line provided files from Varnish and the CDN
pgcache_cleanup Generally triggered from a cronjob, allows for manual Garbage collection of page cache to be triggered
opcache_flush_file SNS/local file.php Tells opcache to compile files
opcache_flush SNS/local expression Tells opcache to delete all files
fix_environment Creates missing files, writes apache/nginx rules. Subcommand defines server type:
apache create rules for apache server
nginx create rules for nginx server
Available flush sub-commands:
--post_id=<id> flush a specific post ID
--permalink=<post-permalink> flush a specific permalink
database flush the database cache
object flush the object cache
minify flush the minify cache
EOB
);
}
}
if ( method_exists( '\WP_CLI', 'add_command' ) ) {
\WP_CLI::add_command( 'w3-total-cache', '\W3TC\W3TotalCache_Command' );
\WP_CLI::add_command( 'total-cache', '\W3TC\W3TotalCache_Command' );
} else {
// backward compatibility
\WP_CLI::addCommand( 'w3-total-cache', '\W3TC\W3TotalCache_Command' );
\WP_CLI::addCommand( 'total-cache', '\W3TC\W3TotalCache_Command' );
}