-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathWP_Super_Cache_Command.php
More file actions
257 lines (220 loc) · 7.19 KB
/
WP_Super_Cache_Command.php
File metadata and controls
257 lines (220 loc) · 7.19 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
<?php
/**
* Manages the WP Super Cache plugin
*/
class WP_Super_Cache_Command extends WP_CLI_Command {
/**
* Version of WP Super Cache plugin.
*
* @var string Version.
*/
protected $wpsc_version;
/**
* Loads WP Super Cache config file and dependencies.
*
* @return void
*/
private function load() {
global $cache_enabled, $super_cache_enabled, $cache_path, $wp_cache_mod_rewrite, $wp_cache_debug_log;
$cli_loader = new WP_Super_Cache_CLI_Loader();
$cli_loader->load();
$this->wpsc_version = $cli_loader->get_wpsc_version();
// Check if basic global variables are populated.
if ( ! isset( $cache_enabled, $super_cache_enabled, $cache_path, $wp_cache_mod_rewrite, $wp_cache_debug_log ) ) {
WP_CLI::error( 'WP Super Cache plugin is not properly loaded' );
}
}
/**
* Clears the cache, or a specific post's cache.
*
* ## OPTIONS
*
* [--post_id=<post-id>]
* : Clear the cache for the post with this ID.
*
* [--permalink=<permalink>]
* : Clear the cache for the post with this permalink.
*
* ## EXAMPLES
*
* # Clear all cached pages.
* $ wp super-cache flush
* Success: Cache cleared.
*
* # Clear the cache for a specific post by ID.
* $ wp super-cache flush --post_id=42
* Success: Post cache cleared.
*
* # Clear the cache for a specific post by permalink.
* $ wp super-cache flush --permalink=https://example.com/my-post/
* Success: Post cache cleared.
*
* @when after_wp_load
*/
public function flush( $args = array(), $assoc_args = array() ) {
global $file_prefix;
$this->load();
if ( isset( $assoc_args['post_id'] ) ) {
$post_id = absint( $assoc_args['post_id'] );
if ( $post_id <= 0 ) {
WP_CLI::error( 'This is not a valid post id.' );
}
wp_cache_post_change( $post_id );
WP_CLI::success( 'Post cache cleared.' );
} elseif ( isset( $assoc_args['permalink'] ) ) {
$id = absint( url_to_postid( $assoc_args['permalink'] ) );
if ( $id <= 0 ) {
WP_CLI::error( 'There is no post with this permalink.' );
}
wp_cache_post_change( $id );
WP_CLI::success( 'Post cache cleared.' );
} else {
wp_cache_clean_cache( $file_prefix, true );
WP_CLI::success( 'Cache cleared.' );
}
}
/**
* Get the status of the cache.
*
* @when after_wp_load
*/
public function status( $args = array(), $assoc_args = array() ) {
global $cache_enabled, $super_cache_enabled, $wp_cache_mod_rewrite;
$this->load();
WP_CLI::line( 'Version of WP Super Cache: ' . $this->wpsc_version );
WP_CLI::line();
$cache_method = 'WP-Cache';
if ( $cache_enabled && $super_cache_enabled ) {
$cache_method = $wp_cache_mod_rewrite ? 'Expert' : 'Simple';
}
$cache_status = 'Cache status: ' . WP_CLI::colorize( $cache_enabled ? '%gOn%n' : '%rOff%n' ) . PHP_EOL;
$cache_status .= $cache_enabled
? 'Cache Delivery Method: ' . $cache_method . PHP_EOL
: '';
WP_CLI::line( $cache_status );
$cache_stats = get_option( 'supercache_stats' );
if ( ! empty( $cache_stats ) ) {
if ( $cache_stats['generated'] > time() - 3600 * 24 ) {
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
WP_CLI::line( 'Cache content on ' . date( 'r', $cache_stats['generated'] ) . ': ' );
WP_CLI::line();
WP_CLI::line( ' WordPress cache:' );
WP_CLI::line( ' Cached: ' . $cache_stats['wpcache']['cached'] );
WP_CLI::line( ' Expired: ' . $cache_stats['wpcache']['expired'] );
WP_CLI::line();
WP_CLI::line( ' WP Super Cache:' );
WP_CLI::line( ' Cached: ' . $cache_stats['supercache']['cached'] );
WP_CLI::line( ' Expired: ' . $cache_stats['supercache']['expired'] );
} else {
WP_CLI::error( 'The WP Super Cache stats are too old to work with (older than 24 hours).' );
}
} else {
WP_CLI::error( 'No WP Super Cache stats found.' );
}
}
/**
* Enable the WP Super Cache.
*
* @when after_wp_load
*/
public function enable( $args = array(), $assoc_args = array() ) {
global $cache_enabled, $wp_cache_mod_rewrite;
$this->load();
wp_cache_enable();
if ( ! defined( 'DISABLE_SUPERCACHE' ) ) {
wp_super_cache_enable();
}
if ( $wp_cache_mod_rewrite ) {
add_mod_rewrite_rules();
}
if ( $cache_enabled ) {
WP_CLI::success( 'The WP Super Cache is enabled.' );
} else {
WP_CLI::error( 'The WP Super Cache is not enabled, check its settings page for more info.' );
}
}
/**
* Disable the WP Super Cache.
*
* @when after_wp_load
*/
public function disable( $args = array(), $assoc_args = array() ) {
global $cache_enabled;
$this->load();
wp_cache_disable();
wp_super_cache_disable();
if ( ! $cache_enabled ) {
WP_CLI::success( 'The WP Super Cache is disabled.' );
} else {
WP_CLI::error( 'The WP Super Cache is still enabled, check its settings page for more info.' );
}
}
/**
* Primes the cache by creating static pages before users visit them
*
* @synopsis [--status] [--cancel]
*
* @when after_wp_load
*/
public function preload( $args = array(), $assoc_args = array() ) {
global $super_cache_enabled;
$this->load();
$preload_counter = get_option( 'preload_cache_counter' );
$preloading = is_array( $preload_counter ) && $preload_counter['c'] > 0;
$pending_cancel = get_option( 'preload_cache_stop' );
// Bail early if caching or preloading is disabled
if ( ! $super_cache_enabled ) {
WP_CLI::error( 'The WP Super Cache is not enabled.' );
}
if (
defined( 'DISABLESUPERCACHEPRELOADING' ) &&
( true === DISABLESUPERCACHEPRELOADING || 'true' === DISABLESUPERCACHEPRELOADING )
) {
WP_CLI::error( 'Cache preloading is not enabled.' );
}
// Display status
if ( isset( $assoc_args['status'] ) ) {
$this->preload_status( $preload_counter, $pending_cancel );
exit();
}
// Cancel preloading if in progress
if ( isset( $assoc_args['cancel'] ) ) {
if ( $preloading ) {
if ( $pending_cancel ) {
WP_CLI::error( 'There is already a pending preload cancel. It may take up to a minute for it to cancel completely.' );
} else {
update_option( 'preload_cache_stop', true );
WP_CLI::success( 'Scheduled preloading of cache almost cancelled. It may take up to a minute for it to cancel completely.' );
exit();
}
} else {
WP_CLI::error( 'Not currently preloading.' );
}
}
// Start preloading if not already in progress
if ( $preloading ) {
WP_CLI::warning( 'Cache preloading is already in progress.' );
$this->preload_status( $preload_counter, $pending_cancel );
exit();
} else {
wp_schedule_single_event( time(), 'wp_cache_full_preload_hook' );
WP_CLI::success( 'Scheduled preload for next cron run.' );
}
}
/**
* Outputs the status of preloading
*
* @param $preload_counter
* @param $pending_cancel
*/
protected function preload_status( $preload_counter, $pending_cancel ) {
if ( is_array( $preload_counter ) && $preload_counter['c'] > 0 ) {
WP_CLI::line( sprintf( 'Currently caching from post %d to %d.', $preload_counter['c'] - 100, $preload_counter['c'] ) );
if ( $pending_cancel ) {
WP_CLI::warning( 'Pending preload cancel. It may take up to a minute for it to cancel completely.' );
}
} else {
WP_CLI::line( 'Not currently preloading.' );
}
}
}