forked from wp-cli/wp-super-cache-cli
/
cli.php
169 lines (145 loc) · 5.04 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
<?php
/**
* Manages the WP Super Cache plugin
*/
class WPSuperCache_Command extends WP_CLI_Command {
/**
* Clear something from the cache.
*
* @synopsis [--post_id=<post-id>] [--permalink=<permalink>]
*/
function flush( $args = array(), $assoc_args = array() ) {
if ( isset($assoc_args['post_id']) ) {
if ( is_numeric( $assoc_args['post_id'] ) ) {
wp_cache_post_change( $assoc_args['post_id'] );
} else {
WP_CLI::error('This is not a valid post id.');
}
wp_cache_post_change( $assoc_args['post_id'] );
}
elseif ( isset( $assoc_args['permalink'] ) ) {
$id = url_to_postid( $assoc_args['permalink'] );
if ( is_numeric( $id ) ) {
wp_cache_post_change( $id );
} else {
WP_CLI::error('There is no post with this permalink.');
}
} else {
global $file_prefix;
wp_cache_clean_cache( $file_prefix, true );
WP_CLI::success( 'Cache cleared.' );
}
}
/**
* Get the status of the cache.
*/
function status( $args = array(), $assoc_args = array() ) {
$cache_stats = get_option( 'supercache_stats' );
if ( !empty( $cache_stats ) ) {
if ( $cache_stats['generated'] > time() - 3600 * 24 ) {
global $super_cache_enabled;
WP_CLI::line( 'Cache status: ' . ($super_cache_enabled ? '%gOn%n' : '%rOff%n') );
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.
*/
function enable( $args = array(), $assoc_args = array() ) {
global $super_cache_enabled;
wp_super_cache_enable();
if($super_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.
*/
function disable( $args = array(), $assoc_args = array() ) {
global $super_cache_enabled;
wp_super_cache_disable();
if(!$super_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]
*/
function preload( $args = array(), $assoc_args = array() ) {
global $super_cache_enabled;
$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 ) {
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.' );
}
}
}
WP_CLI::add_command( 'super-cache', 'WPSuperCache_Command' );