forked from wp-media/wp-rocket-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.php
298 lines (227 loc) · 7.05 KB
/
command.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
<?php
/**
* Manage Revisions
*/
class WPRocket_CLI extends WP_CLI_Command {
/**
* Set WP_CACHE constant in wp-config.php to true
*
* ## EXAMPLES
*
* wp rocket activate
*
* @subcommand activate
*/
public function activate() {
if( defined( 'WP_CACHE' ) && ! WP_CACHE ) {
if( is_writable( rocket_find_wpconfig_path() ) ) {
set_rocket_wp_cache_define( true );
WP_CLI::success( 'WP Rocket is enable, WP_CACHE is set to true.' );
} else {
WP_CLI::error( 'It seems we don\'t have writing permissions on wp-config.php file.' );
}
} else {
WP_CLI::error( 'WP Rocket is already enable.' );
}
}
/**
* Set WP_CACHE constant in wp-config.php to false
*
* ## EXAMPLES
*
* wp rocket deactivate
*
* @subcommand deactivate
*/
public function deactivate() {
if( defined( 'WP_CACHE' ) && WP_CACHE ) {
if( is_writable( rocket_find_wpconfig_path() ) ) {
set_rocket_wp_cache_define( false );
WP_CLI::success( 'WP Rocket is disable, WP_CACHE is set to false.' );
} else {
WP_CLI::error( 'It seems we don\'t have writing permissions on wp-config.php file.' );
}
} else {
WP_CLI::error( 'WP Rocket is already disable.' );
}
}
/**
* Purge cache files
*
* ## OPTIONS
*
* [--post_id=<post_id>]
* : List posts to purge cache files.
*
* [--permalink=<permalink>]
* : List permalinks to purge cache files. Trumps --post_id.
*
* [--lang=<lang>]
* : List langs to purge cache files. Trumps --post_id & --permalink.
*
* [--blog_id=<blog_id>]
* : List blogs to purge cache files. Trumps --post_id & --permalink & lang.
*
* [--confirm]
* : Automatic 'yes' to any confirmation
*
* ## EXAMPLES
*
* wp rocket clean
* wp rocket clean --post_id=2
* wp rocket clean --post_id=2,4,6,8
* wp rocket clean --permalink=http://example.com
* wp rocket clean --permalink=http://example.com, http://example.com/category/(.*)
* wp rocket clean --lang=fr
* wp rocket clean --lang=fr,de,en,it
* wp rocket clean --blog_id=2
* wp rocket clean --blog_id=2,4,6,8
*
* @subcommand clean
*/
public function clean( $args = array(), $assoc_args = array() ) {
if( ! empty( $assoc_args['blog_id'] ) ) {
if ( ! defined( 'MULTISITE' ) || ! MULTISITE ) {
WP_CLI::error( 'This installation doesn\'t multisite support.' );
}
$blog_ids = explode( ',' , $assoc_args['blog_id'] );
$blog_ids = array_map( 'trim' , $blog_ids );
$total = 0;
$notify = \WP_CLI\Utils\make_progress_bar( 'Delete cache files', count( $blog_ids ) );
foreach ( $blog_ids as $blog_id ) {
if ( $bloginfo = get_blog_details( (int) $blog_id, false ) ) {
switch_to_blog( $blog_id );
rocket_clean_domain();
WP_CLI::line( 'Cache cleared for "' . esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ) . '".' );
restore_current_blog();
$total++;
} else {
WP_CLI::line( 'This blog ID "' . $blog_id . '" doesn\'t exist.' );
}
$notify->tick();
}
$notify->finish();
WP_CLI::success( 'Cache cleared for ' . $total . ' blog(s).' );
} else if( ! empty( $assoc_args['lang'] ) ) {
if( ! rocket_has_i18n() ) {
WP_CLI::error( 'No WPML or qTranslate in this website.' );
}
$langs = explode( ',' , $assoc_args['lang'] );
$langs = array_map( 'trim' , $langs );
$total = count( $langs );
$notify = \WP_CLI\Utils\make_progress_bar( 'Delete cache files', $total );
foreach ( $langs as $lang ) {
rocket_clean_domain_for_selected_lang( $lang );
$notify->tick();
}
$notify->finish();
WP_CLI::success( 'Cache files cleared for ' . $total . ' lang(s).' );
} else if( ! empty( $assoc_args['permalink'] ) ) {
$permalinks = explode( ',' , $assoc_args['permalink'] );
$permalinks = array_map( 'trim' , $permalinks );
$total = count( $permalinks );
$notify = \WP_CLI\Utils\make_progress_bar( 'Delete cache files', $total );
foreach ( $permalinks as $permalink ) {
rocket_clean_files( $permalink );
WP_CLI::line( 'Cache cleared for "' . $permalink . '".' );
$notify->tick();
}
$notify->finish();
WP_CLI::success( 'Cache files cleared for ' . $total . ' permalink(s).' );
} else if( ! empty( $assoc_args['post_id'] ) ) {
$total = 0;
$post_ids = explode( ',' , $assoc_args['post_id'] );
$post_ids = array_map( 'trim' , $post_ids );
$notify = \WP_CLI\Utils\make_progress_bar( 'Delete cache files', count( $post_ids ) );
foreach ( $post_ids as $post_id ) {
global $wpdb;
$post_exists = $wpdb->get_row( "SELECT ID FROM $wpdb->posts WHERE id = '" . (int) $post_id . "'");
if( $post_exists ) {
if( get_post_type( $post_id ) == 'attachment' ) {
WP_CLI::line( 'This post ID "' . $post_id . '" is an attachment.' );
} else {
rocket_clean_post( $post_id );
WP_CLI::line( 'Cache cleared for post ID "' . $post_id . '".' );
$total++;
}
} else {
WP_CLI::line( 'This post ID "' . $post_id . '" doesn\'t exist.' );
}
$notify->tick();
}
if( $total ) {
$notify->finish();
if( $total == 1 ) {
WP_CLI::success( '1 post is cleared.' );
} else {
WP_CLI::success( $total . ' posts are cleared.' );
}
} else {
WP_CLI::error( 'No cache files are cleared.' );
}
} else {
if ( ! empty( $assoc_args['confirm'] ) && $assoc_args['confirm'] ) {
WP_CLI::line( 'Deleting all cache files.' );
} else {
WP_CLI::confirm( 'Delete all cache files ?' );
}
rocket_clean_domain();
WP_CLI::success( 'All cache files cleared.' );
}
}
/**
* Run WP Rocket Bot for preload cache files
*
* ## EXAMPLES
*
* wp rocket preload
*
* @subcommand preload
*/
public function preload( $args = array(), $assoc_args = array() ) {
run_rocket_bot( 'cache-preload' );
WP_CLI::success( 'Finished WP Rocket preload cache files.' );
}
/**
* Regenerate file
*
* ## OPTIONS
*
* [--file=<file>]
* : The file to regenerate. It could be:
* - htaccess
* - advanced-cache
* - config (It's the config file stored in the wp-rocket-config folder)
*
* ## EXAMPLES
*
* wp rocket regenerate --file=htaccess
*
* @subcommand regenerate
*/
public function regenerate( $args = array(), $assoc_args = array() ) {
if( !empty( $assoc_args['file'] ) ) {
switch( $assoc_args['file'] ) {
case 'advanced-cache':
rocket_generate_advanced_cache_file();
WP_CLI::success( 'The advanced-cache.php file has just been regenerated.' );
break;
case 'config':
rocket_generate_config_file();
WP_CLI::success( 'The config file has just been regenerated.' );
break;
case 'htaccess':
$GLOBALS['is_apache'] = true;
flush_rocket_htaccess();
WP_CLI::success( 'The .htaccess file has just been regenerated.' );
break;
default:
WP_CLI::error( 'You don\'t specify a good value for the "file" argument. It should be: advanced-cache, config or htaccess.' );
break;
}
} else {
WP_CLI::error( 'You don\'t specify the "file" argument.' );
}
}
}
WP_CLI::add_command( 'rocket', 'WPRocket_CLI' );