PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.11.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.11.0
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / classes / page-cache / cli.php

cli.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.11.0, at includes/classes/page-cache/cli.php

246 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Classes\PageCache;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * Page Cache — WP-CLI commands.
10 *
11 * Lives here rather than in includes/dev-cli/ because that directory is listed
12 * in .distignore and never ships; these commands are for site owners, not just
13 * for development.
14 *
15 * Registered even when the cache is switched off, so `purge` can still clear
16 * files left behind after someone disables the feature.
17 */
18 class Cli {
19
20 /**
21 * Register the command with WP-CLI.
22 */
23 public static function register() {
24 \WP_CLI::add_command( 'ablocks cache', __CLASS__ );
25 }
26
27 /**
28 * Show cache configuration and disk usage.
29 *
30 * ## OPTIONS
31 *
32 * [--format=<format>]
33 * : Output format.
34 * ---
35 * default: table
36 * options:
37 * - table
38 * - json
39 * - yaml
40 * ---
41 *
42 * ## EXAMPLES
43 *
44 * wp ablocks cache status
45 *
46 * @param array $args Positional arguments.
47 * @param array $assoc_args Named arguments.
48 */
49 public function status( $args, $assoc_args ) {
50 $stats = Store::stats();
51
52 $query_args = Rules::allowed_query_args();
53 $query_args = empty( $query_args ) ? '(none)' : implode( ', ', $query_args );
54
55 // size_format() returns false for a non-numeric input and '0 B' is a
56 // clearer empty-cache reading than a blank cell.
57 $size = size_format( $stats['bytes'], 2 );
58 $size = ( false === $size ) ? '0 B' : $size;
59
60 $next_prune = wp_next_scheduled( Scheduler::PRUNE_HOOK );
61
62 $rows = [
63 [
64 'key' => 'enabled',
65 'value' => Rules::is_enabled() ? 'yes' : 'no',
66 ],
67 [
68 'key' => 'scope',
69 'value' => (string) \ABlocks\Helper::get_settings( 'perf_page_cache_scope', 'all' ),
70 ],
71 [
72 'key' => 'ttl_hours',
73 'value' => (string) (int) \ABlocks\Helper::get_settings( 'perf_page_cache_ttl', 0 ),
74 ],
75 [
76 'key' => 'gzip',
77 'value' => \ABlocks\Helper::get_settings( 'perf_page_cache_gzip', true ) ? 'yes' : 'no',
78 ],
79 [
80 'key' => 'mobile_variant',
81 'value' => \ABlocks\Helper::get_settings( 'perf_page_cache_mobile_variant', false ) ? 'yes' : 'no',
82 ],
83 [
84 'key' => 'allowed_query_args',
85 'value' => $query_args,
86 ],
87 [
88 'key' => 'warm_backend',
89 'value' => Scheduler::warm_backend(),
90 ],
91 [
92 'key' => 'next_prune',
93 'value' => $next_prune
94 ? gmdate( 'Y-m-d H:i:s', $next_prune ) . ' UTC'
95 : 'not scheduled',
96 ],
97 [
98 'key' => 'directory',
99 'value' => Store::base_dir(),
100 ],
101 [
102 'key' => 'cached_pages',
103 'value' => (string) $stats['pages'],
104 ],
105 [
106 'key' => 'files_on_disk',
107 'value' => (string) $stats['files'],
108 ],
109 [
110 'key' => 'size',
111 'value' => $size,
112 ],
113 ];
114
115 $format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table';
116 \WP_CLI\Utils\format_items( $format, $rows, [ 'key', 'value' ] );
117 }
118
119 /**
120 * Delete cached pages.
121 *
122 * The URL is a positional argument rather than `--url=`, because WP-CLI
123 * reserves `--url` as a global parameter for selecting a site in a multisite
124 * network; it never reaches the command, so a `--url=` flag here would be
125 * silently ignored and quietly purge everything instead.
126 *
127 * ## OPTIONS
128 *
129 * [<url>]
130 * : Purge a single URL instead of the whole cache.
131 *
132 * ## EXAMPLES
133 *
134 * wp ablocks cache purge
135 * wp ablocks cache purge https://example.com/pricing/
136 *
137 * @param array $args Positional arguments.
138 * @param array $assoc_args Named arguments.
139 */
140 public function purge( $args, $assoc_args ) {
141 if ( ! empty( $args[0] ) ) {
142 $url = esc_url_raw( $args[0] );
143 if ( Store::delete_url( $url ) ) {
144 \WP_CLI::success( sprintf( 'Purged %s', $url ) );
145 } else {
146 \WP_CLI::warning( sprintf( 'Nothing cached for %s', $url ) );
147 }
148 return;
149 }
150
151 $removed = Store::flush();
152 \WP_CLI::success( sprintf( 'Purged the page cache (%d files removed).', $removed ) );
153 }
154
155 /**
156 * Queue public URLs to be rendered into the cache.
157 *
158 * Requests are spaced apart rather than fired at once — warming exists to
159 * spare visitors a render, and doing it in one burst would create the load
160 * spike the cache is meant to prevent.
161 *
162 * ## OPTIONS
163 *
164 * [--limit=<count>]
165 * : How many URLs to queue.
166 * ---
167 * default: 100
168 * ---
169 *
170 * [--now]
171 * : Fetch immediately in this process instead of queueing.
172 *
173 * ## EXAMPLES
174 *
175 * wp ablocks cache warm
176 * wp ablocks cache warm --limit=500
177 * wp ablocks cache warm --limit=20 --now
178 *
179 * @param array $args Positional arguments.
180 * @param array $assoc_args Named arguments.
181 */
182 public function warm( $args, $assoc_args ) {
183 if ( ! Rules::is_enabled() ) {
184 \WP_CLI::warning( 'The page cache is switched off, so warming would write nothing.' );
185 return;
186 }
187
188 $limit = isset( $assoc_args['limit'] ) ? (int) $assoc_args['limit'] : 100;
189 $urls = Scheduler::warmable_urls( $limit );
190
191 if ( empty( $urls ) ) {
192 \WP_CLI::warning( 'No public URLs found to warm.' );
193 return;
194 }
195
196 if ( isset( $assoc_args['now'] ) ) {
197 $progress = \WP_CLI\Utils\make_progress_bar( 'Warming', count( $urls ) );
198 foreach ( $urls as $url ) {
199 Scheduler::warm_url( $url );
200 $progress->tick();
201 }
202 $progress->finish();
203 \WP_CLI::success( sprintf( 'Requested %d URL(s).', count( $urls ) ) );
204 return;
205 }
206
207 $queued = Scheduler::queue_warm( $urls );
208
209 \WP_CLI::success(
210 sprintf(
211 'Queued %d URL(s) via %s. They warm gradually in the background.',
212 $queued,
213 Scheduler::warm_backend()
214 )
215 );
216 }
217
218 /**
219 * Delete cached pages older than the configured TTL.
220 *
221 * ## OPTIONS
222 *
223 * [--ttl=<hours>]
224 * : Override the configured TTL for this run.
225 *
226 * ## EXAMPLES
227 *
228 * wp ablocks cache prune
229 * wp ablocks cache prune --ttl=24
230 *
231 * @param array $args Positional arguments.
232 * @param array $assoc_args Named arguments.
233 */
234 public function prune( $args, $assoc_args ) {
235 $ttl = isset( $assoc_args['ttl'] ) ? (int) $assoc_args['ttl'] : null;
236
237 if ( ( null === $ttl && 0 >= (int) \ABlocks\Helper::get_settings( 'perf_page_cache_ttl', 0 ) ) || 0 === $ttl ) {
238 \WP_CLI::warning( 'No TTL configured — nothing expires. Set perf_page_cache_ttl or pass --ttl.' );
239 return;
240 }
241
242 $removed = Store::prune( $ttl );
243 \WP_CLI::success( sprintf( '%d expired file(s) removed.', $removed ) );
244 }
245 }
246