PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.4.2
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.4.2
1.4.2 1.4.1 1.4.0 1.3.7 1.3.6 1.3.5 1.3.4 1.3.3 1.3.2 1.3.1 1.3.0 1.2.4 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 36 releases
xspeed / includes / modules / Cache / CacheModule.php

CacheModule.php in xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN 1.4.2, at includes/modules/Cache/CacheModule.php

1,388 lines 56.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cache module.
4 *
5 * Owns the cache_expiry and excluded_urls settings. cache_enabled is
6 * deliberately NOT in this schema — flipping it triggers the
7 * advanced-cache.php drop-in install + WP_CACHE constant edit in
8 * wp-config.php, which is a sensitive single-purpose code path and lives
9 * in Cache::toggle() with its own dedicated /xspeed/v1/cache/toggle REST
10 * route. The dashboard's Cache page renders the special hero UI for it
11 * above this module's schema-driven settings panel.
12 *
13 * Tier: Free.
14 *
15 * @package XSpeed
16 */
17
18 declare(strict_types=1);
19
20 namespace XSpeed\Modules\Cache;
21
22 defined( 'ABSPATH' ) || exit;
23
24 use XSpeed\Module;
25 use XSpeed\Settings_Manager;
26
27 final class CacheModule extends Module {
28
29 /**
30 * Default Excluded Cookies.
31 *
32 * A constant for the same reason as DEFAULT_IGNORED_QUERY_PARAMS:
33 * Cache::rewrite_block_lines() needs the list at boot, where building
34 * the settings schema would translate its labels too early. The schema
35 * and that fallback both read THIS, so they cannot drift.
36 *
37 * @var string[]
38 */
39 public const DEFAULT_EXCLUDED_COOKIES = array(
40 'comment_author',
41 '~wordpress_[a-f0-9]+',
42 'wp-postpass',
43 'wordpress_no_cache',
44 'wordpress_logged_in',
45 'edd_items_in_cart',
46 'woocommerce_items_in_cart',
47 'fct_cart_hash',
48 'comment_',
49 'woocommerce_',
50 'wordpress',
51 'xf_',
52 'edd_',
53 'jetpack',
54 'yith_wcwl_session_',
55 'yith_wrvp_',
56 'wpsc_',
57 'ecwid',
58 'ec_',
59 'bookly',
60 );
61
62 /**
63 * Default Ignored Query Parameters.
64 *
65 * A constant because Cache::sync_query_allowlist() needs this list at
66 * boot, where building the settings schema would translate its labels
67 * before WordPress allows it. Both the schema below and that boot-time
68 * fallback read THIS, so the two cannot drift.
69 *
70 * @var string[]
71 */
72 public const DEFAULT_IGNORED_QUERY_PARAMS = array(
73 '__s',
74 '_ga',
75 '_ke',
76 '~[a-zA-Z0-9_-]+_sid',
77 'adgroupid',
78 'age-verified',
79 'ao_noptimize',
80 'campaignid',
81 'ck_subscriber_id',
82 'cn-reloaded',
83 'dclid',
84 'epik',
85 'fb_action_ids',
86 'fb_action_types',
87 'fb_source',
88 'fbclid',
89 'gclid',
90 'jobid',
91 'mc_cid',
92 'mc_eid',
93 'mkt_tok',
94 'msclkid',
95 'ref',
96 // Twitter/X (`ref_src`, `ref_url`) and Facebook (`refid`)
97 // decorations. Enumerated because param names match
98 // whole-name: the bare `ref` above no longer absorbs them,
99 // and a `ref*` glob would over-match `referrer` and
100 // `refund_id`, which are page-selecting.
101 'ref_src',
102 'ref_url',
103 'refid',
104 '~session_[a-zA-Z0-9_-]+_alive',
105 'sseid',
106 'sslid',
107 'usqp',
108 '~utm_[a-zA-Z0-9_-]+',
109 );
110
111 /**
112 * Click and campaign IDs added to the defaults in 1.3.6.
113 *
114 * Each is unique per click and never changes the page, yet each one
115 * bypassed the page cache and, with xSpeed Pro, was a new CSS entry to
116 * build: Google Merchant's `srsltid` and Ads' `gad_*`/`gbraid`/`wbraid`,
117 * GA4's cross-domain `_gl`, TikTok, X, Instagram, Yandex, HubSpot email
118 * and LinkedIn IDs. Kept as their own list so the upgrade can add exactly
119 * these to a saved list without re-adding anything a site removed.
120 *
121 * @var string[]
122 */
123 public const TRACKING_PARAMS_1_3_6 = array(
124 '_gl',
125 '_hsenc',
126 '_hsmi',
127 'gad_campaignid',
128 'gad_source',
129 'gbraid',
130 'igshid',
131 'li_fat_id',
132 'srsltid',
133 'ttclid',
134 'twclid',
135 'wbraid',
136 'yclid',
137 );
138
139
140 /**
141 * The shipped default list: the base list plus later additions, sorted.
142 *
143 * @return string[]
144 */
145 public static function default_ignored_query_params(): array {
146 $all = array_values( array_unique( array_merge( self::DEFAULT_IGNORED_QUERY_PARAMS, self::TRACKING_PARAMS_1_3_6 ) ) );
147 sort( $all );
148 return $all;
149 }
150
151 public const SLUG = 'cache';
152 public const TIER = self::TIER_FREE;
153 public const VERSION = '1.0.0';
154
155 /**
156 * Default cache lifetime in hours (7 days).
157 *
158 * Named so the readers that need a fallback share ONE value with the
159 * schema below. Three of them carried their own hardcoded `?? 24`, which
160 * silently became a stale copy the moment the default moved. They are
161 * unreachable today — Settings_Manager::get() always merges defaults —
162 * but an unreachable wrong number is still a trap for the next change.
163 * (#284 B5)
164 */
165 public const DEFAULT_EXPIRY_HOURS = 24 * 7;
166
167 public function ui_metadata(): array {
168 return array(
169 'label' => __( 'Page Cache', 'xspeed' ),
170 'icon' => 'Database',
171 'description' => __( 'Saves each page as a file and serves it to logged-out visitors.', 'xspeed' ),
172 'group' => 'cache',
173 );
174 }
175
176 /**
177 * @inheritDoc
178 *
179 * Nothing exempt. Purging only ever touches xSpeed's own cache, so on an
180 * occupied site the setting is inert either way — but a host installing xSpeed
181 * on a user's behalf should leave nothing switched on that the user did not
182 * ask for, and "inert today" is a weak reason to make an exception.
183 */
184 public function conflict_safe_exempt(): array {
185 return array();
186 }
187
188 public function settings_schema(): array {
189 $schema = array(
190 'cache_expiry' => array(
191 'type' => 'int',
192 // Matches the wizard's Balanced preset, which is what a fresh
193 // install starts on — a shorter module default meant the two
194 // disagreed about what "default" means. (#284)
195 'default' => self::DEFAULT_EXPIRY_HOURS,
196 'min' => 1,
197 'max' => 720,
198 'label' => __( 'Cache expiry (hours)', 'xspeed' ),
199 'unit' => 'hours',
200 'description' => __( 'How long a cached page is kept before xSpeed builds it again. 1 to 720 hours (30 days).', 'xspeed' ),
201 ),
202 'excluded_urls' => array(
203 'type' => 'list',
204 // Comprehensive LiteSpeed / WP Rocket-parity default URL
205 // exclusions (FBS-82181). Plain text = "contains", glob via
206 // * ? [ ], or a `~` prefix for raw regex (e.g. ~wp-.*\.php).
207 'default' => array(
208 '/wp-admin/',
209 '/wp-json/',
210 '/xmlrpc.php',
211 '~wp-.*\.php',
212 '/feed/',
213 'index.php',
214 // `sitemaps?` — SEOPress generates sitemaps.xml (plural).
215 '~sitemaps?(_index)?\.xml',
216 '/robots.txt',
217 // Bare (no trailing slash) so "contains" matches both
218 // /cart and /cart/items — WooCommerce serves both forms.
219 '/cart',
220 '/checkout',
221 '/my-account',
222 'ao_noptirocket',
223 'ao_speedup_cachebuster',
224 'removed_item',
225 '/wc-api',
226 '/edd-api',
227 '/wp-login',
228 ),
229 // `path`: kept as typed, percent-encoded slugs included.
230 'item_type' => 'path',
231 'label' => __( 'Excluded URLs', 'xspeed' ),
232 'description' => __( 'Pages whose URL matches a line here are never cached. Plain text matches anywhere (/cart). A pattern with * matches from the start of the path: /cart/* matches /cart/items but not /shop/cart/items. ~ starts a regex.', 'xspeed' ),
233 ),
234 'excluded_cookies' => array(
235 'type' => 'list',
236 // Cookies that signal a logged-in / transactional visitor
237 // whose response must not be served from a shared cache.
238 // `~` prefix = raw regex (e.g. ~wordpress_[a-f0-9]+). (FBS-82181)
239 'default' => self::DEFAULT_EXCLUDED_COOKIES,
240 'item_type' => 'string',
241 'label' => __( 'Excluded cookies', 'xspeed' ),
242 'description' => __( 'Visitors with a cookie whose name matches a line here always get a fresh page. One per line; * and ~regex work.', 'xspeed' ),
243 ),
244 'bypass_user_agents' => array(
245 'type' => 'list',
246 'default' => array(),
247 'item_type' => 'string',
248 'label' => __( 'Excluded browsers and bots', 'xspeed' ),
249 'description' => __( 'Visitors whose user agent contains a line here always get a fresh page. Useful for screenshot bots and uptime monitors.', 'xspeed' ),
250 ),
251 'ignored_query_params' => array(
252 'type' => 'list',
253 // Analytics / ad / session query keys stripped before the
254 // cache key is computed, so /post?utm_source=x and /post
255 // share one entry. `~` prefix = raw regex. (FBS-82181)
256 // Matched whole-name, so every entry here means the param
257 // it names and nothing that merely contains it.
258 'default' => self::default_ignored_query_params(),
259 'item_type' => 'string',
260 'label' => __( 'Ignored query parameters', 'xspeed' ),
261 'advanced' => true,
262 'description' => __( 'URL parameters to ignore, so /post?utm_source=x gets the same cached page as /post. Each line matches a whole parameter name.', 'xspeed' ),
263 ),
264 'purge_on_upgrade' => array(
265 'type' => 'bool',
266 'default' => true,
267 'label' => __( 'Clear cache after updates', 'xspeed' ),
268 'description' => __( 'Clear the page cache when a plugin, theme or WordPress is updated, so visitors never get old pages with broken asset links.', 'xspeed' ),
269 ),
270 'purge_affected_only' => array(
271 'type' => 'bool',
272 'default' => true,
273 'label' => __( 'Clear only the pages a change affects', 'xspeed' ),
274 'description' => __( 'When you publish, edit or delete a post, clear that post, the pages that list it and its feeds instead of the whole site. Pages with a post grid from a page builder, a block plugin or the theme are noted as they are built and cleared when the change can affect their list; turn this off if a grid still shows an old list.', 'xspeed' ),
275 'info_title' => __( 'Which pages are cleared', 'xspeed' ),
276 'info' => __( 'The post, its old address if it moved, the home page, the blog page, its categories, tags and author pages with every page of each, the date archives, feeds, the posts next to it, pages that show a Latest Posts or Query Loop block, directly or in a synced pattern, and pages noted running a post grid of their own (a page builder, block plugin, related-posts or theme list) that the change can affect. If your theme shows a list that can be on any page, and the change alters it, xSpeed clears the whole site instead: a Recent Posts, Archives or Calendar widget, a post list block, or a latest-posts grid from a page builder or plugin (usually a new post, or an edit to one of the newest few it shows), a page list or menu (a page published, withdrawn, renamed or moved), or a Categories or Tag Cloud list (a post moved to other categories or tags).', 'xspeed' ),
277 ),
278 'mobile_separate' => array(
279 'type' => 'bool',
280 'default' => false,
281 'label' => __( 'Separate mobile cache', 'xspeed' ),
282 'description' => __( 'Keep a separate cached copy for phones. Turn on only if your theme or plugins show different pages on mobile.', 'xspeed' ),
283 ),
284 'edge_provider' => array(
285 'type' => 'enum',
286 'default' => 'auto',
287 'options' => array( 'auto', 'off', 'cloudflare', 'fastly', 'varnish', 'nginx', 'akamai', 'cloudfront', 'google', 'keycdn', 'bunny', 'sucuri', 'incapsula', 'generic', 'custom' ),
288 'option_labels' => array(
289 'auto' => 'Detect automatically',
290 'off' => 'Off (no edge lifetime or cache tags)',
291 'cloudflare' => 'Cloudflare',
292 'varnish' => 'Varnish',
293 'nginx' => 'nginx proxy cache',
294 'cloudfront' => 'Amazon CloudFront',
295 'google' => 'Google Cloud CDN',
296 'keycdn' => 'KeyCDN',
297 'bunny' => 'Bunny',
298 // These four cannot be presented as supported on the same
299 // footing as the ones above. Vendor documentation either
300 // does not establish that they honour what we send, or
301 // establishes that they ignore origin cache headers until
302 // the property is configured to respect them — Akamai
303 // caches for a theoretically infinite time by default, and
304 // Sucuri's default caching level ignores the headers
305 // outright. Naming them without the caveat would promise a
306 // protection the CDN is not currently giving.
307 'fastly' => 'Fastly (needs CDN configuration)',
308 'akamai' => 'Akamai (needs CDN configuration)',
309 'sucuri' => 'Sucuri (needs CDN configuration)',
310 'incapsula' => 'Imperva / Incapsula (needs CDN configuration)',
311 'generic' => 'Something else',
312 'custom' => 'Custom headers',
313 ),
314 'label' => __( 'CDN or proxy in front', 'xspeed' ),
315 'description' => __( 'Tells your CDN or proxy not to store pages xSpeed does not cache. Leave on Detect automatically unless you know your provider.', 'xspeed' ),
316 'advanced' => true,
317 'info_title' => __( 'Cache in front of this site', 'xspeed' ),
318 'info' => __( 'Naming your provider narrows the headers to the one it reads. Detect automatically works it out per request and otherwise sends a set every cache ignores unless it understands it, so it is safe not to know. Off stops the edge lifetime and cache tags. When an add-on that manages the edge, such as a Cloudflare Enterprise add-on, is active, the "don\'t store" headers for logged-in, cart and search pages still go out. Run "wp xspeed cache edge" to see what was detected and what gets sent.', 'xspeed' )
319 ),
320 'edge_custom_headers' => array(
321 'type' => 'list',
322 'default' => array(),
323 'item_type' => 'string',
324 'label' => __( 'Custom CDN headers', 'xspeed' ),
325 'advanced' => true,
326 'dependsOn' => array( 'field' => 'edge_provider', 'value' => 'custom' ),
327 'description' => __( 'One header per line, as Name: value, for example "Surrogate-Control: no-store". Lines starting with # are ignored.', 'xspeed' ),
328 'info_title' => __( 'Custom edge headers', 'xspeed' ),
329 'info' => __( 'These replace the headers xSpeed would have picked for your CDN. Two baselines are still added underneath: a Cache-Control, and "X-Accel-Expires: 0" for a page cache running in nginx on your own server. Name either one yourself and yours is used instead. Values containing $, % or a backslash are dropped — the same pairs go into nginx and Apache directives, where those cannot be escaped safely. Content-Length, Content-Encoding, Content-Type, Transfer-Encoding, Set-Cookie and Location are refused.', 'xspeed' )
330 ),
331 );
332
333 // LiteSpeed-only opt-in (#509): meaningless on any other server, so
334 // the field only exists in the schema where it can act — elsewhere the
335 // stored value survives via preserved_keys(). Inserted right after
336 // mobile_separate, its sibling static-fast-path trade-off.
337 if ( \XSpeed\Server::LITESPEED === \XSpeed\Server::type() ) {
338 $litespeed = array(
339 'litespeed_static_rewrite' => array(
340 'type' => 'bool',
341 'default' => false,
342 'label' => __( 'LiteSpeed static fast path', 'xspeed' ),
343 'advanced' => true,
344 'description' => __( 'LiteSpeed serves cached pages without running PHP, which is faster on slow hosts. These visits are not counted in the dashboard hit ratio.', 'xspeed' ),
345 ),
346 );
347 $pos = (int) array_search( 'mobile_separate', array_keys( $schema ), true ) + 1;
348 $schema = array_slice( $schema, 0, $pos, true ) + $litespeed + array_slice( $schema, $pos, null, true );
349 }
350
351 return $schema;
352 }
353
354 /**
355 * `mobile_separate_review` lives outside the schema: migration sets it
356 * (bool) when a source plugin had "separate mobile cache" on, so the
357 * dashboard can prompt the user to re-enable it deliberately instead of
358 * silently importing it (which would kill the device-blind static fast
359 * path). Without preserving it here, the first schema-driven cache save
360 * would rebuild the option from the schema alone and drop the flag before
361 * the user ever saw the prompt. (FBS-83145)
362 *
363 * @return string[]
364 */
365 public function preserved_keys(): array {
366 $keys = array( 'mobile_separate_review' );
367 // On non-LiteSpeed servers the litespeed_static_rewrite field is not
368 // in the schema (see settings_schema()), so a schema-driven save
369 // would silently drop a value chosen while the site ran LiteSpeed.
370 // Preserve it so moving LiteSpeed → other → LiteSpeed keeps the
371 // user's choice. On LiteSpeed itself the schema owns the key.
372 if ( \XSpeed\Server::LITESPEED !== \XSpeed\Server::type() ) {
373 $keys[] = 'litespeed_static_rewrite';
374 }
375 return $keys;
376 }
377
378 /**
379 * Seed per-module option from the legacy xspeed_options blob if we
380 * haven't done so yet. Idempotent — once xspeed_module_cache exists
381 * or the legacy keys are gone, this is a no-op. Runs on both boot
382 * and activate so installs on every code path are covered.
383 */
384 public function boot(): void {
385 $this->seed_from_legacy_if_needed();
386
387 // Keep every mobile_separate-dependent artifact (the drop-in's
388 // `.mobile-separate` flag, the device-blind server rewrite, and the
389 // device-keyed caches) in lockstep with the setting — on boot, and
390 // whenever the cache settings are saved. The drop-in can't read WP
391 // options, so it reads the sidecar marker Cache maintains here.
392 \XSpeed\Cache::reconcile_mobile_separate();
393 // Both write paths matter. On a fresh install `xspeed_module_cache`
394 // does not exist yet, so core's update_option() delegates to
395 // add_option() and fires `add_option_…` INSTEAD of
396 // `update_option_…`. Hooking only the latter meant the very first
397 // save of Cache Expiry never re-baked the drop-in: the panel and the
398 // DB read the new value while the drop-in kept enforcing the old
399 // one, and re-saving the same value could not recover it because
400 // update_option() short-circuits on an unchanged value (#251).
401 $xspeed_resync_cache_artifacts = static function () {
402 \XSpeed\Cache::reconcile_mobile_separate();
403 // Re-bake the cookie / user-agent exclusion rules into the
404 // drop-in. It runs before WordPress loads and so carries a
405 // COPY of those rules, substituted at install time — and
406 // auto_heal() deliberately only reinstalls when the file is
407 // missing, foreign, or an older version, none of which a
408 // settings change makes true. Without this, adding an
409 // excluded cookie left the drop-in serving the shared
410 // anonymous page to exactly the visitors it excluded, until
411 // the next plugin upgrade happened to reinstall it.
412 //
413 // ONLY when page caching is actually on, for the same reason
414 // refresh_rewrite_if_installed() below refuses to write a
415 // block that isn't there: re-baking is maintenance of an
416 // artifact the user opted into, never a way to acquire one.
417 // Re-baking unconditionally reached past our own module — a
418 // site that had declined our page cache got the drop-in
419 // installed anyway on the next Cache Expiry save, and the
420 // following toggle(false) then removed it. auto_heal() has
421 // always gated on this flag; this path simply never did.
422 // (#251)
423 //
424 // Through toggle() rather than install_dropin() so the re-bake
425 // gets the same ownership check, lock and rollback as every
426 // other page-cache write. A drop-in that turned out not to be
427 // ours between the save and now is refused here too.
428 $xspeed_options = get_option( 'xspeed_options', array() );
429 if ( ! empty( $xspeed_options['cache_enabled'] ) ) {
430 \XSpeed\Cache::toggle( true );
431 }
432 // Same staleness applies to the .htaccess block, which is
433 // written to disk from the same generator. Refresh it only
434 // when a block is already installed — writing one here would
435 // enable the static path on a site that never opted in.
436 \XSpeed\Cache::refresh_rewrite_if_installed();
437 };
438 add_action( 'update_option_xspeed_module_cache', $xspeed_resync_cache_artifacts );
439 add_action( 'add_option_xspeed_module_cache', $xspeed_resync_cache_artifacts );
440
441 // Time-driven collection of expired entries and superseded minified
442 // assets. Scheduled here as well as in activate() because a site that
443 // upgrades into this version never runs the activation hook again.
444 add_action( \XSpeed\Cache_GC::CRON_HOOK, array( \XSpeed\Cache_GC::class, 'run' ) );
445 \XSpeed\Cache_GC::ensure_scheduled();
446 }
447
448 public function activate(): void {
449 $this->seed_from_legacy_if_needed();
450 \XSpeed\Cache_GC::ensure_scheduled();
451 }
452
453 public function deactivate(): void {
454 \XSpeed\Cache_GC::unschedule();
455 }
456
457 private function seed_from_legacy_if_needed(): void {
458 if ( null !== get_option( 'xspeed_module_cache', null ) ) {
459 return;
460 }
461 $legacy = get_option( 'xspeed_options', array() );
462 if ( ! is_array( $legacy ) ) {
463 return;
464 }
465 $seed = array( '_version' => self::VERSION );
466 $dirty = false;
467 if ( array_key_exists( 'cache_expiry', $legacy ) ) {
468 $seed['cache_expiry'] = max( 1, min( 720, (int) $legacy['cache_expiry'] ) );
469 unset( $legacy['cache_expiry'] );
470 $dirty = true;
471 }
472 if ( array_key_exists( 'excluded_urls', $legacy ) ) {
473 $seed['excluded_urls'] = is_array( $legacy['excluded_urls'] ) ? array_values( array_filter( $legacy['excluded_urls'], 'is_string' ) ) : array();
474 unset( $legacy['excluded_urls'] );
475 $dirty = true;
476 }
477 if ( $dirty ) {
478 update_option( 'xspeed_module_cache', $seed );
479 update_option( 'xspeed_options', $legacy );
480 }
481 }
482
483 public function cli_commands(): array {
484 return array(
485 array(
486 'name' => 'xspeed optimize',
487 'callback' => array( $this, 'cli_optimize' ),
488 'shortdesc' => 'Measure, apply the recommended settings one at a time, verify the page still works after each, and report what changed. Use --dry-run to see the plan without touching anything.',
489 'synopsis' => array(
490 array(
491 'type' => 'assoc',
492 'name' => 'aggressiveness',
493 'description' => 'safe (removals + server-side only), standard (default), or aggressive (includes settings known to break some themes).',
494 'optional' => true,
495 'options' => array( 'safe', 'standard', 'aggressive' ),
496 ),
497 array(
498 'type' => 'flag',
499 'name' => 'dry-run',
500 'description' => 'Show the plan and stop. Changes nothing.',
501 'optional' => true,
502 ),
503 array(
504 'type' => 'assoc',
505 'name' => 'budget',
506 'description' => 'Seconds to spend before stopping between steps. Default 120.',
507 'optional' => true,
508 ),
509 array(
510 'type' => 'assoc',
511 'name' => 'measure-score',
512 'description' => 'auto (default) measures when the stored score is stale and after changes land; never reuses the stored score; always measures even for a dry run.',
513 'optional' => true,
514 'options' => array( 'auto', 'never', 'always' ),
515 ),
516 ),
517 ),
518 array(
519 'name' => 'xspeed purge',
520 'callback' => array( $this, 'cli_purge' ),
521 'shortdesc' => 'Clear every cache xSpeed manages — page and static files, REST responses, minified assets, the object cache and the configured edge — and report per store what was cleared, what was skipped and why. Use --type to clear just one.',
522 'ai_hint' => 'Clear the cache after a change is live on the server but visitors still see the old version. Purges everything by default; --type=page for the local HTML only, --type=cloudflare for the edge only. Exits non-zero if a store that IS configured refused to purge, so its output can be trusted rather than assumed.',
523 'synopsis' => array(
524 array(
525 'type' => 'assoc',
526 'name' => 'type',
527 'description' => 'What to clear: all (default), page, object, cloudflare, xcloud (sites with xCloud\'s purge plugin), cdn — or a group name (edge). Comma-separate to clear several.',
528 'optional' => true,
529 ),
530 array(
531 'type' => 'assoc',
532 'name' => 'cause',
533 'description' => 'Label recorded in the purge log, so `wp xspeed cache purge-log` can tell this run apart from a click. Default "CLI".',
534 'optional' => true,
535 ),
536 array(
537 'type' => 'assoc',
538 'name' => 'format',
539 'description' => 'table (default, one line per store) or json (the full report, for scripts).',
540 'optional' => true,
541 'options' => array( 'table', 'json' ),
542 ),
543 ),
544 ),
545 array(
546 'name' => 'xspeed cache',
547 'callback' => array( $this, 'cli_handler' ),
548 'shortdesc' => 'Inspect the Cache module: `status` (settings), `inventory` (which pages are cached, and how old), `size` (where the disk usage goes), `purge-log` (what cleared the cache, when and why), `purge` to clear everything (same as `wp xspeed purge`), `purge <url>` or `purge-url <url>` to clear one page, `recheck-rewrite` to re-run the static-rewrite probe, or `nginx-config` to print the unified nginx server-block for pasting into a vhost, or `edge` to show which cache is in front of the site and what xSpeed tells it, or `listings` to show which pages were recorded running a post list of their own (cleared by a narrow purge when a save may change that list). To clear the whole site use `wp xspeed purge`.',
549 'synopsis' => array(
550 array(
551 'type' => 'positional',
552 'name' => 'action',
553 'options' => array( 'status', 'inventory', 'size', 'purge', 'purge-log', 'purge-url', 'recheck-rewrite', 'nginx-config', 'edge', 'listings' ),
554 'optional' => true,
555 ),
556 array(
557 'type' => 'positional',
558 'name' => 'url',
559 'description' => 'For purge and purge-url: the page to clear, as a URL on this site or a path such as /about/. Without it, purge clears everything.',
560 'optional' => true,
561 ),
562 array(
563 'type' => 'assoc',
564 'name' => 'limit',
565 'description' => 'Rows to print for inventory / purge-log (default 20), or sample pages for listings (default 5).',
566 'optional' => true,
567 ),
568 array(
569 'type' => 'assoc',
570 'name' => 'cause',
571 'description' => 'Label recorded in the purge log for purge and purge-url. Default "CLI".',
572 'optional' => true,
573 ),
574 array(
575 'type' => 'assoc',
576 'name' => 'type',
577 'description' => 'For purge without a URL: what to clear, as in `wp xspeed purge --type`. Default all.',
578 'optional' => true,
579 ),
580 array(
581 'type' => 'assoc',
582 'name' => 'format',
583 'description' => 'For purge without a URL: table (default) or json.',
584 'optional' => true,
585 'options' => array( 'table', 'json' ),
586 ),
587 array(
588 'type' => 'assoc',
589 'name' => 'server',
590 'description' => 'Server type to assume for nginx-config, skipping detection. Detection needs SERVER_SOFTWARE, which the command line does not have; an undetectable host is assumed to be nginx anyway, so this is for stating it outright — or for the case detection is positively wrong, such as nginx in front of Apache.',
591 'options' => array( 'nginx', 'apache', 'litespeed' ),
592 'optional' => true,
593 ),
594 ),
595 ),
596 );
597 }
598
599 /**
600 * `wp xspeed optimize` — run the autopilot.
601 *
602 * Prints what it DID, not what it hoped to do: applied steps, reverted
603 * steps with the reason they were undone, and the problems it could not
604 * touch. A run that changes nothing prints that plainly rather than a
605 * success banner.
606 *
607 * @param array<int,string> $args Positional args (unused).
608 * @param array<string,string> $assoc Flags.
609 */
610 public function cli_optimize( array $args, array $assoc ): void {
611 $result = \XSpeed\Optimize_Runner::run(
612 array(
613 'aggressiveness' => (string) ( $assoc['aggressiveness'] ?? 'standard' ),
614 'dry_run' => isset( $assoc['dry-run'] ),
615 'budget_seconds' => isset( $assoc['budget'] ) ? (int) $assoc['budget'] : 120,
616 'measure_score' => (string) ( $assoc['measure-score'] ?? 'auto' ),
617 )
618 );
619
620 if ( is_wp_error( $result ) ) {
621 \WP_CLI::error( $result->get_error_message() );
622 return;
623 }
624
625 if ( ! empty( $result['dry_run'] ) ) {
626 // The summary carries the score AND its age. Printing the plan
627 // without it left the one number a reader wants off the only
628 // command they run before deciding to apply anything.
629 if ( isset( $result['message'] ) ) {
630 \WP_CLI::log( (string) $result['message'] );
631 }
632 \WP_CLI::log( 'Plan (' . count( $result['plan'] ) . ' steps, nothing applied):' );
633 foreach ( $result['plan'] as $step ) {
634 \WP_CLI::log( ' - ' . $step['change'] . ' [' . $step['tier'] . ']' );
635 }
636 foreach ( $result['skipped'] as $row ) {
637 \WP_CLI::log( ' skipped: ' . $row['id'] . ' — ' . $row['why'] );
638 }
639 return;
640 }
641
642 if ( isset( $result['message'] ) ) {
643 \WP_CLI::success( (string) $result['message'] );
644 }
645
646 foreach ( $result['applied'] as $row ) {
647 \WP_CLI::log( ' ✓ ' . $row['change'] );
648 }
649 foreach ( $result['reverted'] as $row ) {
650 \WP_CLI::warning( 'Undone: ' . $row['id'] . ' — ' . $row['why'] );
651 }
652 foreach ( $result['unfixable'] as $row ) {
653 \WP_CLI::log( ' ! ' . $row['issue'] . ( '' !== $row['fix'] ? ' — ' . $row['fix'] : '' ) );
654 }
655
656 if ( ! empty( $result['applied'] ) ) {
657 // "applied and verified" was more than the checks earn. They read
658 // HTML in PHP and cannot run JavaScript, so this line was telling
659 // someone the site was fine when the only honest claim is that
660 // nothing in the markup looked broken.
661 \WP_CLI::success( count( $result['applied'] ) . ' change(s) applied; HTML checks passed.' );
662
663 if ( ! empty( $result['verify_urls'] ) ) {
664 \WP_CLI::log( '' );
665 \WP_CLI::log( 'Now open these and check they render, with no console errors:' );
666 foreach ( $result['verify_urls'] as $u ) {
667 \WP_CLI::log( ' ' . $u );
668 }
669 }
670 }
671 }
672
673 /**
674 * `wp xspeed purge` — clear every cache xSpeed owns, in one call.
675 *
676 * Reports per store rather than printing a success banner, because the
677 * banner was the bug: a site whose Cloudflare token had lost its purge
678 * permission saw "cache cleared" and kept serving stale HTML from the
679 * edge. What is skipped is as much of the answer as what is cleared, so
680 * every skip prints its reason.
681 *
682 * Exit code follows the same distinction. A store that is not configured
683 * has nothing to clear and does not fail the run — otherwise every CI
684 * pipeline on a site without Redis goes red for a purge that did exactly
685 * what it should. A store that IS configured and refused is a failure.
686 *
687 * There is deliberately no `--url`: WP-CLI reserves that flag for
688 * multisite site selection and consumes it before a handler ever sees it.
689 * Clearing one page is `wp xspeed cache purge-url <url>`.
690 *
691 * @param array<int,string> $args Positional args (unused).
692 * @param array<string,string> $assoc Flags.
693 */
694 public function cli_purge( array $args, array $assoc ): void {
695 unset( $args );
696
697 $requested = array_values(
698 array_filter(
699 array_map( 'trim', explode( ',', (string) ( $assoc['type'] ?? 'all' ) ) )
700 )
701 );
702 if ( ! $requested ) {
703 $requested = array( 'all' );
704 }
705
706 $accepted = \XSpeed\Purge_Runner::accepted_types();
707 $unknown = array_diff( $requested, $accepted );
708 if ( $unknown ) {
709 // Refuse before purging anything: a typo in --type must not
710 // quietly clear a DIFFERENT store than the one named.
711 \WP_CLI::error(
712 sprintf(
713 'Unknown purge type: %s. Expected one of: %s',
714 implode( ', ', $unknown ),
715 implode( ', ', $accepted )
716 )
717 );
718 return;
719 }
720
721 $cause = isset( $assoc['cause'] ) && '' !== trim( (string) $assoc['cause'] ) ? trim( (string) $assoc['cause'] ) : 'CLI';
722 $report = \XSpeed\Purge_Runner::run( $requested, $cause );
723
724 if ( 'json' === ( $assoc['format'] ?? 'table' ) ) {
725 // The report goes to STDOUT alone so `... --format=json | jq` works;
726 // the failure message goes to STDERR via ::error, which is also
727 // what produces the non-zero exit.
728 \WP_CLI::line( (string) wp_json_encode( $report ) );
729 if ( ! $report['ok'] ) {
730 \WP_CLI::error( 'One or more cache stores failed to purge; see the report above.' );
731 }
732 return;
733 }
734
735 $cleared = 0;
736 $skipped = 0;
737 $failed = 0;
738 foreach ( $report['types'] as $row ) {
739 switch ( $row['status'] ) {
740 case \XSpeed\Purge_Runner::CLEARED:
741 ++$cleared;
742 \WP_CLI::log(
743 sprintf(
744 ' cleared %s%s%s',
745 $row['label'],
746 self::purge_amount( $row ),
747 '' !== $row['reason'] ? '; ' . $row['reason'] : ''
748 )
749 );
750 break;
751 case \XSpeed\Purge_Runner::FAILED:
752 ++$failed;
753 \WP_CLI::log( sprintf( ' FAILED %s — %s', $row['label'], $row['reason'] ) );
754 break;
755 default:
756 ++$skipped;
757 \WP_CLI::log( sprintf( ' skipped %s — %s', $row['label'], $row['reason'] ) );
758 }
759 }
760
761 if ( $failed ) {
762 \WP_CLI::error(
763 sprintf(
764 '%d of %d cache store(s) failed to purge; %d cleared, %d skipped.',
765 $failed,
766 count( $report['types'] ),
767 $cleared,
768 $skipped
769 )
770 );
771 return;
772 }
773
774 if ( ! $cleared ) {
775 // Not a success banner: nothing was purged, and saying so is the
776 // honest answer for a --type nobody has configured.
777 \WP_CLI::log( sprintf( 'Nothing to purge — %d store(s) skipped.', $skipped ) );
778 return;
779 }
780
781 \WP_CLI::success( sprintf( 'Purged %d cache store(s); %d skipped.', $cleared, $skipped ) );
782 }
783
784 /**
785 * Clear one page: `wp xspeed cache purge-url <url>` and
786 * `wp xspeed cache purge <url>`.
787 *
788 * A string purge_url() cannot act on (another scheme, a scheme with no
789 * host, a bare word) is refused with an error. purge_url() returns 0 for
790 * it, which read as "already cold" and exited 0.
791 *
792 * @param string $url Absolute URL or site-relative path.
793 * @param array<string,string> $assoc Flags; only --cause is read.
794 */
795 private function cli_purge_url( string $url, array $assoc ): void {
796 if ( ! self::is_purgeable_url( $url ) ) {
797 \WP_CLI::error( sprintf( 'Not a page URL: %s. Give an http(s) URL on this site or a path starting with /, such as /about/.', $url ) );
798 return;
799 }
800 $cause = isset( $assoc['cause'] ) && '' !== trim( (string) $assoc['cause'] ) ? trim( (string) $assoc['cause'] ) : 'CLI';
801 $result = \XSpeed\Cache::purge_url_reported( $url, $cause );
802 $removed = $result['removed'];
803 $forwarded = implode( ', ', $result['forwarded'] );
804 if ( $removed > 0 ) {
805 \WP_CLI::success(
806 '' === $forwarded
807 ? sprintf( 'Purged %d cache file(s) for %s', $removed, $url )
808 : sprintf( 'Purged %d cache file(s) for %s, and sent the purge to %s', $removed, $url, $forwarded )
809 );
810 } elseif ( '' !== $forwarded ) {
811 // xSpeed's own cache held nothing, but a cache in front of
812 // PHP took the purge: that copy is the one visitors get.
813 \WP_CLI::success( sprintf( 'Sent the purge for %s to %s. xSpeed\'s own cache held no copy.', $url, $forwarded ) );
814 } else {
815 \WP_CLI::log( sprintf( 'No cache entries found for %s (already cold, or the URL never cached).', $url ) );
816 }
817 }
818
819 /**
820 * Whether purge_url() can act on this string: an http(s) URL with a
821 * host, a host without a scheme (`site.test/page/`), or a path starting
822 * with `/`. The same rules purge_url() applies before it returns 0.
823 *
824 * @param string $url What the operator typed.
825 */
826 private static function is_purgeable_url( string $url ): bool {
827 $parts = wp_parse_url( $url );
828 if ( ! is_array( $parts ) ) {
829 return false;
830 }
831 if ( isset( $parts['scheme'] ) ) {
832 return in_array( strtolower( (string) $parts['scheme'] ), array( 'http', 'https' ), true ) && ! empty( $parts['host'] );
833 }
834 if ( isset( $parts['host'] ) ) {
835 return true;
836 }
837 $path = isset( $parts['path'] ) ? (string) $parts['path'] : '';
838 return '' !== $path && '/' === $path[0];
839 }
840
841 /**
842 * The " — 42 entries (1.3 MB)" tail on a cleared line.
843 *
844 * Entries and bytes are both optional: a Redis FLUSHALL reports neither,
845 * and printing "0 entries" for it would read as an empty cache rather
846 * than an uncountable one.
847 *
848 * @param array{entries:int|null,bytes:int|null} $row Report row.
849 */
850 private static function purge_amount( array $row ): string {
851 $parts = array();
852 if ( null !== $row['entries'] ) {
853 $parts[] = sprintf( '%d entr%s', $row['entries'], 1 === (int) $row['entries'] ? 'y' : 'ies' );
854 }
855 if ( null !== $row['bytes'] && $row['bytes'] > 0 ) {
856 $parts[] = size_format( $row['bytes'], 1 );
857 }
858
859 return $parts ? ' — ' . implode( ', ', $parts ) : '';
860 }
861
862 public function cli_handler( array $args, array $assoc ): void {
863 $action = isset( $args[0] ) ? (string) $args[0] : 'status';
864 $limit = isset( $assoc['limit'] ) ? max( 1, (int) $assoc['limit'] ) : 20;
865
866 // `wp xspeed cache purge` is what people type. It runs the same
867 // purge as `wp xspeed purge`; before it was listed, WP-CLI rejected
868 // it with "Invalid value specified for positional arg". Given a URL
869 // it clears that one page, as purge-url does: it used to drop the
870 // URL and clear the whole site.
871 if ( 'purge' === $action ) {
872 $url = isset( $args[1] ) ? trim( (string) $args[1] ) : '';
873 if ( '' === $url ) {
874 $this->cli_purge( array(), $assoc );
875 return;
876 }
877 foreach ( array( 'type', 'format' ) as $flag ) {
878 if ( isset( $assoc[ $flag ] ) ) {
879 \WP_CLI::error( sprintf( '--%s applies to a full purge, not to one URL. Run `wp xspeed cache purge %s` without it, or drop the URL to purge everything.', $flag, $url ) );
880 return;
881 }
882 }
883 $this->cli_purge_url( $url, $assoc );
884 return;
885 }
886
887 /*
888 * Print the unified nginx server-block so an installer, provisioning
889 * script, or another plugin can fetch it non-interactively and write
890 * it into a vhost. Previously this was only reachable via
891 * `wp eval 'echo \XSpeed\Cache::full_nginx_server_block();'`, which
892 * is not a supported surface (and is unavailable over MCP, where
893 * run_command dispatches these same callbacks).
894 *
895 * Output discipline matters here: the config goes to STDOUT with
896 * nothing else, so `wp xspeed cache nginx-config > site.conf` yields a
897 * pasteable file. Every diagnostic goes to STDERR via WP_CLI::warning
898 * / ::error, and a non-nginx host or an empty block exits non-zero so
899 * a script can branch on it rather than writing an empty file.
900 *
901 * --server exists because detection cannot work here. WP-CLI runs
902 * without SERVER_SOFTWARE, so Server::type() falls back to the value
903 * a previous web request cached — and on a site provisioned entirely
904 * over WP-CLI there is no such value, leaving `unknown` on a genuine
905 * nginx host. Rather than guess (a loopback request is the one thing
906 * least likely to work mid-provisioning), let the caller state it:
907 * the script writing to /etc/nginx/ already knows the answer.
908 * Without the flag nothing changes, so a script sweeping a mixed
909 * fleet still gets its non-zero exit on Apache.
910 *
911 * It pins Server::type() rather than being passed down, because the
912 * decision is re-made at every level: full_nginx_server_block(),
913 * Cache::nginx_snippet(), and each module's own nginx_directives()
914 * all ask independently. Threading an argument through would leave
915 * the deeper gates still detecting, and the command would emit a
916 * config missing its cache rewrite — worse than refusing outright.
917 */
918 if ( 'nginx-config' === $action ) {
919 /*
920 * Scoped to this one generation pass, not the request. Under
921 * real WP-CLI the process ends here either way, but the same
922 * callback runs over MCP, where several commands share one PHP
923 * request — a pin left in place made the NEXT command report
924 * this host as nginx too.
925 */
926 $pin = null;
927 $assume = null;
928
929 if ( isset( $assoc['server'] ) ) {
930 $assume = strtolower( trim( (string) $assoc['server'] ) );
931 } elseif ( \XSpeed\Server::UNKNOWN === \XSpeed\Server::type() ) {
932 /*
933 * Nothing to detect from, and the action names the server:
934 * `nginx-config` is the request, so absence of evidence
935 * defers to it. Positive evidence to the contrary still
936 * wins — an Apache or LiteSpeed host is told it needs no
937 * nginx block at all, which is the answer that helps.
938 */
939 $assume = \XSpeed\Server::NGINX;
940
941 /*
942 * Only where warnings have somewhere else to go. Real WP-CLI
943 * sends them to STDERR, leaving the config clean on STDOUT.
944 * The MCP shim has ONE buffer for both, so warning there
945 * would prepend "Warning: …" to the config itself and hand
946 * the caller a file nginx refuses. The constant is the
947 * discriminator: real WP-CLI defines it, the shim defines
948 * only the class.
949 */
950 if ( defined( 'WP_CLI' ) && \WP_CLI ) {
951 \WP_CLI::warning(
952 'Could not detect the web server — no recognisable SERVER_SOFTWARE, and no web request has cached one yet. Assuming nginx, which is what this command generates. Pass --server= to state it explicitly, or load any page once to settle detection.'
953 );
954 }
955 }
956
957 if ( null !== $assume ) {
958 $pinned = $assume;
959 $pin = static function () use ( $pinned ) {
960 return $pinned;
961 };
962 add_filter( 'xspeed_server_type', $pin );
963 }
964
965 $block = \XSpeed\Cache::full_nginx_server_block();
966 $server = \XSpeed\Server::type();
967
968 if ( null !== $pin ) {
969 remove_filter( 'xspeed_server_type', $pin );
970 }
971
972 if ( ! is_string( $block ) || '' === trim( $block ) ) {
973 /*
974 * $server cannot be UNKNOWN here: an undetectable host was
975 * already assumed to be nginx above, so anything left is a
976 * server we positively identified — and telling an Apache or
977 * LiteSpeed operator that .htaccess already covers them is
978 * more useful than handing them a block to paste nowhere.
979 */
980 if ( \XSpeed\Server::NGINX !== $server ) {
981 \WP_CLI::error(
982 sprintf(
983 'No nginx server-block to print — this site is running on %s. On Apache and LiteSpeed xSpeed writes its rules to .htaccess automatically.',
984 $server
985 )
986 );
987 return;
988 }
989 \WP_CLI::error( 'No nginx directives to print — page caching and every module that contributes directives are currently disabled.' );
990 return;
991 }
992
993 // STDOUT only: no WP_CLI::log() prefixing, so redirection gives a
994 // clean file. WP_CLI::line() writes the raw string.
995 \WP_CLI::line( rtrim( $block, "\n" ) );
996 return;
997 }
998
999 /*
1000 * Force a fresh static-rewrite probe. The result is cached for five
1001 * minutes and nothing invalidated it, so after fixing an nginx config
1002 * there was no way to re-check — the "configure your server" banner
1003 * just stayed up. (FBS-84012)
1004 */
1005 if ( 'recheck-rewrite' === $action ) {
1006 // Qualify the raw probe against known config refusals before
1007 // reporting. The probe fetches its OWN file from the static tree,
1008 // which succeeds even when no real page is served that way — so
1009 // an unqualified `active` reported "the web server is serving
1010 // cache hits directly" on sites whose every page returned
1011 // HIT (php). See Cache::qualify_rewrite_probe().
1012 $probe = \XSpeed\Cache::qualify_rewrite_probe( \XSpeed\Cache::recheck_static_rewrite() );
1013 $blocked = '' !== (string) $probe['block_reason'];
1014
1015 // Whether the installed rules are the ones these settings
1016 // generate. Reported before the verdict below because it is the
1017 // question someone running this command has just acted on — they
1018 // pasted a block and want to know if it took — and because
1019 // "active" is true for a stale block too.
1020 $this->cli_report_rules_state( (array) ( $probe['rules'] ?? array() ) );
1021
1022 if ( $probe['active'] ) {
1023 \WP_CLI::success( 'Static rewrite is active — the web server is serving cache hits directly.' );
1024 return;
1025 }
1026 if ( $blocked ) {
1027 \WP_CLI::warning( sprintf( 'Static rewrite is not active: %s', (string) $probe['reason'] ) );
1028 return;
1029 }
1030 if ( $probe['inconclusive'] ) {
1031 \WP_CLI::warning( sprintf( 'Could not verify the static rewrite: %s', (string) $probe['reason'] ) );
1032 \WP_CLI::log( 'This is a probe failure, not proof that your server config is wrong.' );
1033 return;
1034 }
1035 \WP_CLI::warning( sprintf( 'Static rewrite is not active: %s', (string) ( $probe['reason'] ?: 'unknown' ) ) );
1036 return;
1037 }
1038
1039 if ( 'purge-url' === $action ) {
1040 $url = isset( $args[1] ) ? trim( (string) $args[1] ) : '';
1041 if ( '' === $url ) {
1042 \WP_CLI::error( 'Usage: wp xspeed cache purge-url <url-or-path>' );
1043 return;
1044 }
1045 $this->cli_purge_url( $url, $assoc );
1046 return;
1047 }
1048
1049 if ( 'inventory' === $action ) {
1050 $this->cli_inventory( $limit );
1051 return;
1052 }
1053
1054 if ( 'size' === $action ) {
1055 $this->cli_size();
1056 return;
1057 }
1058
1059 if ( 'purge-log' === $action ) {
1060 $this->cli_purge_log( $limit );
1061 return;
1062 }
1063
1064 if ( 'edge' === $action ) {
1065 $this->cli_edge();
1066 return;
1067 }
1068
1069 if ( 'listings' === $action ) {
1070 $this->cli_listings( isset( $assoc['limit'] ) ? $limit : 5 );
1071 return;
1072 }
1073
1074 $opts = Settings_Manager::get( self::SLUG );
1075 \WP_CLI::log( 'cache_expiry ' . $opts['cache_expiry'] . 'h' );
1076 \WP_CLI::log( 'excluded_urls ' . count( $opts['excluded_urls'] ) . ' entries' );
1077 foreach ( $opts['excluded_urls'] as $u ) {
1078 \WP_CLI::log( ' - ' . $u );
1079 }
1080 $edge = \XSpeed\Edge_Provider::detect();
1081 \WP_CLI::log( 'edge ' . ( '' !== $edge['provider'] ? $edge['provider'] : $edge['confidence'] ) . ' (' . $edge['source'] . ')' );
1082 }
1083
1084 /**
1085 * `wp xspeed cache edge` — what we think is in front, and what we say to it.
1086 *
1087 * Worth printing even when nothing is held back. "You are behind
1088 * Cloudflare, and a Cache Rule set to ignore origin headers overrides
1089 * anything xSpeed sends" is the answer to a support question that
1090 * otherwise costs someone a week, and it is true whether or not a hold
1091 * ever fires.
1092 */
1093 private function cli_edge(): void {
1094 $answer = \XSpeed\Edge_Provider::detect();
1095
1096 \WP_CLI::log( 'provider ' . ( '' !== $answer['provider'] ? $answer['provider'] : '(none named)' ) );
1097 \WP_CLI::log( 'confidence ' . $answer['confidence'] );
1098 \WP_CLI::log( 'source ' . $answer['source'] );
1099
1100 // A pin outranks detection by design, so nothing re-checks it on the
1101 // site's behalf. Saying the two disagree is the whole mechanism by
1102 // which a site that changed CDN ever finds out.
1103 $sniffed = \XSpeed\Edge_Provider::sniffed();
1104 if ( in_array( $answer['source'], array( 'setting', 'constant', 'filter' ), true )
1105 && '' !== $sniffed['provider']
1106 && $sniffed['provider'] !== $answer['provider'] ) {
1107 \WP_CLI::warning(
1108 sprintf(
1109 'This request looks like %s, but the provider is pinned to %s. If the site moved, change it — the pinned answer is also baked into the drop-in and the server rules.',
1110 $sniffed['provider'],
1111 '' !== $answer['provider'] ? $answer['provider'] : 'off'
1112 )
1113 );
1114 }
1115
1116 if ( \XSpeed\Edge_Provider::is_off( $answer ) ) {
1117 \WP_CLI::log( '' );
1118 \WP_CLI::log( 'Nothing is sent: this is switched off.' );
1119 return;
1120 }
1121
1122 // Resolved through edge_headers_for() rather than straight off the
1123 // provider, so this prints what the serve path would ACTUALLY send —
1124 // including `X-XSpeed-Edge-Hold`, and including the evidence gate.
1125 // Listing the provider's raw set ignored that gate and told operators
1126 // a first render would be held on a site where it would not be.
1127 //
1128 // `bake`, not `request`. Two reasons, and the second one matters:
1129 // this command answers for the site rather than for one response, and
1130 // `request` fires `xspeed_edge_optimization_pending`, whose Pro
1131 // listener resolves the CSS plan — which by its own description is
1132 // what queues a build. A read-only command must not burn a build
1133 // slot, quarantine an entry or purge a page just by being run, and
1134 // under WP-CLI it would do all three against the home page.
1135 $bypass = \XSpeed\Cache::edge_headers_for( 'BYPASS', 'bake', 'logged-in' );
1136 $miss = \XSpeed\Cache::edge_headers_for( 'MISS', 'bake' );
1137
1138 \WP_CLI::log( '' );
1139 \WP_CLI::log( 'On a page xSpeed refuses to cache (a cart, a logged-in view):' );
1140 foreach ( $bypass as $name => $value ) {
1141 \WP_CLI::log( sprintf( ' %s: %s', $name, $value ) );
1142 }
1143
1144 \WP_CLI::log( '' );
1145 if ( array() === $miss ) {
1146 \WP_CLI::log( 'On a first render: nothing. A MISS is a performance hedge, so it is held only where a cache in front was detected — and none was. Name the provider in Cache In Front Of This Site to cover first renders too.' );
1147 } else {
1148 \WP_CLI::log( 'On a first render:' );
1149 foreach ( $miss as $name => $value ) {
1150 \WP_CLI::log( sprintf( ' %s: %s', $name, $value ) );
1151 }
1152 }
1153
1154 // The same bake the drop-in gets, so it asks no per-page question.
1155 $variant = \XSpeed\Cache::query_variant_edge_headers();
1156
1157 \WP_CLI::log( '' );
1158 if ( array() === $variant ) {
1159 \WP_CLI::log( 'A cached page requested with an ignored parameter (?utm_source=x) gets the same headers as without one. It is held only where a cache in front was detected, and with Separate Mobile Cache on every page is held already.' );
1160 } else {
1161 \WP_CLI::log( 'On a cached page requested with an ignored parameter (?utm_source=x), a cached search or a query-form feed:' );
1162 foreach ( $variant as $name => $value ) {
1163 \WP_CLI::log( sprintf( ' %s: %s', $name, $value ) );
1164 }
1165 }
1166
1167 \WP_CLI::log( '' );
1168 \WP_CLI::log( 'X-XSpeed-Edge-Hold names why a response was held: bypass, bypass-shape, miss, mobile-split, pending or query-variant. No header means nothing was held.' );
1169
1170 if ( 'cloudflare' === $answer['provider'] ) {
1171 \WP_CLI::log( '' );
1172 \WP_CLI::log( 'A Cloudflare Cache Rule whose Edge TTL is "Ignore cache-control header and use this TTL" overrides all of the above. Use "Respect origin TTL" on that rule if pages are still being stored.' );
1173 }
1174 }
1175
1176 /**
1177 * `wp xspeed cache listings`: the pages recorded running a post list
1178 * outside the main loop, which a narrow purge adds when a save may
1179 * change that list.
1180 *
1181 * For measuring how wide that set gets on a real site: close to every
1182 * page means a narrow purge saves little there.
1183 *
1184 * @param int $samples Pages to print.
1185 */
1186 private function cli_listings( int $samples ): void {
1187 $narrow = ! empty( Settings_Manager::get( self::SLUG )['purge_affected_only'] );
1188 $stats = \XSpeed\Listing_Pages::stats( $samples );
1189 $limit = \XSpeed\Affected_Pages::LIMIT;
1190
1191 \WP_CLI::log( 'narrow purge ' . ( $narrow ? 'on' : 'off' ) );
1192 \WP_CLI::log( 'recording ' . ( \XSpeed\Listing_Pages::enabled() ? 'on' : 'off' ) );
1193 \WP_CLI::log( 'directory ' . $stats['dir'] );
1194
1195 \WP_CLI::log( '' );
1196 \WP_CLI::log( 'Newest-N lists (' . count( $stats['specs'] ) . ' of ' . \XSpeed\Listing_Pages::SPEC_CAP . '): a change to one of the newest N clears the whole site.' );
1197 foreach ( $stats['specs'] as $spec ) {
1198 \WP_CLI::log( sprintf( ' newest %d of %s%s', $spec['n'], implode( ', ', $spec['types'] ), $spec['sticky'] ? ', sticky posts first' : '' ) );
1199 }
1200
1201 \WP_CLI::log( '' );
1202 \WP_CLI::log( sprintf( 'Pages with other lists: %d recorded (%d files, cap %d); %d posts in the shown-on index.', $stats['pages'], $stats['files'], \XSpeed\Listing_Pages::CAP, $stats['posts'] ) );
1203 foreach ( $stats['types'] as $type => $counts ) {
1204 \WP_CLI::log(
1205 sprintf(
1206 ' %-14s %d pages, %d not precise%s',
1207 $type,
1208 $counts['all'],
1209 $counts['loose'],
1210 $counts['loose'] > $limit
1211 ? ' (over ' . $limit . ' not precise: any change to a post of this type clears the whole site)'
1212 : ( $counts['all'] > $limit ? ' (over ' . $limit . ': a change that is not a plain edit clears the whole site)' : '' )
1213 )
1214 );
1215 }
1216
1217 if ( null !== $stats['full'] ) {
1218 \WP_CLI::warning(
1219 sprintf(
1220 'The record is full, and pages listing %s went unrecorded: a change to a post of those types clears the whole site until a save finds the record under the cap.',
1221 implode( ', ', array_map( 'strval', (array) ( $stats['full']['types'] ?? array( 'any' ) ) ) )
1222 )
1223 );
1224 }
1225 if ( array() === $stats['samples'] ) {
1226 return;
1227 }
1228 \WP_CLI::log( '' );
1229 foreach ( $stats['samples'] as $record ) {
1230 \WP_CLI::log(
1231 sprintf(
1232 ' %s [%s] %s',
1233 $record['url'],
1234 implode( ', ', $record['types'] ),
1235 $record['precise'] ? 'precise, ' . count( $record['ids'] ) . ' post(s) shown' : 'not precise'
1236 )
1237 );
1238 }
1239 }
1240
1241 /**
1242 * Say which version of the cache rules the server is running.
1243 *
1244 * The generated block stamps every static hit with a hash of itself, and
1245 * the probe reads that back — the only way to tell what someone actually
1246 * pasted into a config WordPress cannot open. `stale` names the settings
1247 * that moved since, where we can tell, because "re-paste the block" with
1248 * no reason attached is what makes people ignore it.
1249 *
1250 * @param array $rules Cache::rules_state() output.
1251 */
1252 private function cli_report_rules_state( array $rules ): void {
1253 $state = (string) ( $rules['state'] ?? '' );
1254
1255 if ( 'current' === $state ) {
1256 \WP_CLI::log( 'Cache rules: current — the installed rules are the ones these settings generate.' );
1257 return;
1258 }
1259 if ( 'stale' === $state ) {
1260 $changed = array_filter( array_map( 'strval', (array) ( $rules['changed'] ?? array() ) ) );
1261 \WP_CLI::log(
1262 '' === implode( '', $changed )
1263 ? 'Cache rules: stale — a different version of the rules is installed. Re-paste the block.'
1264 : sprintf(
1265 'Cache rules: stale — a different version of the rules is installed (%s changed since). Re-paste the block.',
1266 implode( ', ', $changed )
1267 )
1268 );
1269 return;
1270 }
1271 if ( 'absent' === $state ) {
1272 \WP_CLI::log( 'Cache rules: absent — no xSpeed rules are installed, or they predate the version marker.' );
1273 return;
1274 }
1275
1276 \WP_CLI::log( 'Cache rules: unknown — the probe could not read a rules marker back from this server.' );
1277 }
1278
1279 /** `wp xspeed cache inventory [--limit=N]` — which pages are cached, and how old. */
1280 private function cli_inventory( int $limit ): void {
1281 $data = \XSpeed\Cache_Inventory::entries( $limit );
1282
1283 if ( empty( $data['entries'] ) ) {
1284 \WP_CLI::log( 'Cache is empty — no cached pages on disk.' );
1285 return;
1286 }
1287
1288 \WP_CLI::log( sprintf( '%d cached page(s); showing %d.', $data['total'], count( $data['entries'] ) ) );
1289 if ( ! empty( $data['capped'] ) ) {
1290 \WP_CLI::warning( sprintf( 'Scan stopped at %d files — the list is a recent sample, not the whole cache.', \XSpeed\Cache_Inventory::SCAN_CAP ) );
1291 }
1292 foreach ( $data['entries'] as $entry ) {
1293 \WP_CLI::log(
1294 sprintf(
1295 ' %-58s %8s %s [%s]',
1296 null === $entry['url'] ? '(url unknown: ' . $entry['key'] . ')' : $entry['url'],
1297 size_format( (int) $entry['bytes'] ),
1298 $this->relative_age( (int) $entry['age'] ),
1299 implode( '+', (array) $entry['stored_in'] )
1300 )
1301 );
1302 }
1303 }
1304
1305 /** `wp xspeed cache size` — where the cache's disk usage goes. */
1306 private function cli_size(): void {
1307 $data = \XSpeed\Cache_Inventory::size_breakdown();
1308
1309 \WP_CLI::log( sprintf( 'Total %s across %d file(s).', size_format( (int) $data['total_bytes'] ), (int) $data['total_files'] ) );
1310 foreach ( $data['buckets'] as $bucket ) {
1311 if ( 0 === (int) $bucket['files'] ) {
1312 continue;
1313 }
1314 \WP_CLI::log( sprintf( ' %-32s %10s %d file(s)', $bucket['label'], size_format( (int) $bucket['bytes'] ), (int) $bucket['files'] ) );
1315 }
1316 if ( (int) $data['compressed_bytes'] > 0 ) {
1317 \WP_CLI::log( sprintf( 'Precompressed on disk: %s (pages without a precompressed copy are compressed by the web server at request time).', size_format( (int) $data['compressed_bytes'] ) ) );
1318 }
1319 }
1320
1321 /** `wp xspeed cache purge-log [--limit=N]` — what cleared the cache, when, and why. */
1322 private function cli_purge_log( int $limit ): void {
1323 $data = \XSpeed\Cache_Inventory::purge_log( $limit );
1324
1325 if ( empty( $data['events'] ) ) {
1326 \WP_CLI::log( 'No purge events recorded yet.' );
1327 return;
1328 }
1329 foreach ( $data['events'] as $event ) {
1330 \WP_CLI::log( sprintf( ' %s %s', $this->relative_age( max( 0, time() - (int) $event['ts'] ) ), $event['message'] ) );
1331 }
1332 }
1333
1334 /** Compact "4h ago" for CLI columns. */
1335 private function relative_age( int $seconds ): string {
1336 if ( $seconds < 60 ) {
1337 return $seconds . 's ago';
1338 }
1339 if ( $seconds < 3600 ) {
1340 return (int) floor( $seconds / 60 ) . 'm ago';
1341 }
1342 if ( $seconds < 86400 ) {
1343 return (int) floor( $seconds / 3600 ) . 'h ago';
1344 }
1345 return (int) floor( $seconds / 86400 ) . 'd ago';
1346 }
1347
1348 /**
1349 * Static-rewrite directives for the unified nginx server-block
1350 * snippet. Returns null when cache is disabled — there's no rewrite
1351 * to install in that state. Delegates to \XSpeed\Cache::nginx_snippet()
1352 * which already produces nginx-detection-gated output.
1353 */
1354 public function nginx_directives(): ?string {
1355 $opts = get_option( 'xspeed_options', array() );
1356 if ( empty( $opts['cache_enabled'] ) ) {
1357 return null;
1358 }
1359 return \XSpeed\Cache::nginx_snippet();
1360 }
1361
1362 /**
1363 * Page caching's master switch is `cache_enabled` in the GLOBAL
1364 * `xspeed_options`, not a per-module `enabled` key -- Cache::toggle owns
1365 * it because flipping it rewrites .htaccess and wp-config.php. The base
1366 * implementation looks only at this module's own settings bag, so it
1367 * found nothing and reported null: the plugin's headline feature was
1368 * missing from its own "N on" count. (#363)
1369 */
1370 public function is_active(): ?bool {
1371 $opts = get_option( 'xspeed_options', array() );
1372 return ! empty( $opts['cache_enabled'] );
1373 }
1374
1375 /**
1376 * No reason shown: page caching has a single master switch, so the pill
1377 * already says everything an (i) would. The switch lives on the Overview
1378 * rather than on this page, but that is a "where is the control" question
1379 * the panel itself should answer, not a reason to explain the verdict.
1380 *
1381 * The (i) is reserved for modules whose on/off is genuinely non-obvious
1382 * -- counted from several flags, or from state outside the settings.
1383 */
1384 public function active_reason(): ?string {
1385 return null;
1386 }
1387 }
1388