PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.0.7
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.0.7
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 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 All 29 releases
xspeed / includes / class-cache.php

class-cache.php in xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN 1.0.7, at includes/class-cache.php

2,229 lines 91.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Page cache engine.
4 *
5 * @package XSpeed
6 */
7
8 namespace XSpeed;
9
10 defined( 'ABSPATH' ) || exit;
11
12 class Cache {
13
14 /**
15 * Output-buffer nesting level at which we opened our cache buffer, so
16 * `close_buffer()` can flush ONLY our buffer and never disturb a buffer
17 * another plugin pushed on top of (or below) ours.
18 *
19 * @var int|null
20 */
21 private static $buffer_level = null;
22
23 public function __construct() {
24 add_action( 'template_redirect', array( $this, 'maybe_start_cache' ), 0 );
25
26 // Events that should invalidate cached output. Beyond posts/comments,
27 // this covers user and term changes — the REST cache can serve
28 // /wp/v2/users, /wp/v2/categories, /wp/v2/tags, and these also affect
29 // rendered author bylines / term-archive pages. Without them, an edit
30 // left the matching endpoint (and archives) stale for the full TTL.
31 // (FBS-82408)
32 $invalidate_hooks = array(
33 'save_post', 'deleted_post', 'trashed_post',
34 'comment_post', 'wp_set_comment_status',
35 'switch_theme', 'activated_plugin', 'deactivated_plugin',
36 // Users → /wp/v2/users + author archives.
37 'profile_update', 'user_register', 'deleted_user',
38 // Terms → /wp/v2/{taxonomy} + term archives.
39 'created_term', 'edited_term', 'delete_term',
40 );
41 foreach ( $invalidate_hooks as $hook ) {
42 add_action( $hook, array( __CLASS__, 'purge_all' ) );
43 add_action( $hook, array( 'XSpeed\\Minifier', 'purge_minified' ) );
44 }
45
46 add_action( 'update_option_xspeed_options', array( __CLASS__, 'on_settings_change' ), 10, 2 );
47
48 add_action( 'admin_bar_menu', array( $this, 'admin_bar_purge' ), 100 );
49 add_action( 'admin_post_xspeed_purge', array( $this, 'handle_admin_bar_purge' ) );
50 }
51
52 public static function on_settings_change( $old, $new ) {
53 // gzip_enabled moved to xspeed_module_gzip — GzipModule owns the
54 // .htaccess flip via its own update_option_xspeed_module_gzip hook.
55 // Same migration is planned for cache_expiry + excluded_urls
56 // (Cache module). Keep this handler around for whatever still
57 // lives in the legacy blob (cache_enabled is special and goes
58 // through Cache::toggle anyway).
59
60 // Any settings change — purge caches so changes take effect.
61 self::purge_all( 'settings change' );
62 Minifier::purge_minified();
63 }
64
65 public function maybe_start_cache() {
66 if ( ! self::should_cache() ) {
67 return;
68 }
69
70 $key = self::cache_key();
71 $file = self::cache_file_for( $key );
72
73 if ( file_exists( $file ) && ! self::is_expired( $file ) ) {
74 Hit_Counter::record_hit();
75 // Emit the HIT marker on THIS path too. The drop-in
76 // (advanced-cache.php) sends "HIT (php)" and the nginx static
77 // rewrite sends "HIT (nginx)", but this template_redirect
78 // serve path — the one that runs when the drop-in isn't loaded
79 // (e.g. WP_CACHE not true) — previously streamed the cached
80 // file with NO marker, so a genuine HIT looked like a MISS in
81 // the response headers. Same header + value as the drop-in.
82 if ( ! headers_sent() ) {
83 header( 'X-XSpeed-Cache: HIT (php)' );
84 }
85 // Replay stored response bits so the HIT matches the original:
86 // a non-HTML Content-Type (cached feeds, sitemaps) and a non-200
87 // status (a cached 404 must serve 404, not 200). No-op for
88 // ordinary pages, which write no .meta.
89 $meta = self::read_meta( $key );
90 if ( ! headers_sent() ) {
91 if ( ! empty( $meta['status'] ) && function_exists( 'http_response_code' ) ) {
92 http_response_code( (int) $meta['status'] );
93 }
94 if ( ! empty( $meta['content_type'] ) && is_string( $meta['content_type'] ) ) {
95 header( 'Content-Type: ' . $meta['content_type'] );
96 }
97 // Conditional GET: emit Last-Modified + ETag and answer a
98 // matching If-Modified-Since / If-None-Match with 304 so
99 // aggregators (and browsers) skip re-downloading an unchanged
100 // cached response — the bandwidth win feeds are about.
101 // (FBS-82407 #5)
102 if ( self::serve_not_modified( $file ) ) {
103 exit; // 304 sent, no body.
104 }
105 }
106 // Serve the precompressed Brotli sibling when the client accepts
107 // it (an add-on, the Pro Brotli module, wrote <file>.br). On this
108 // PHP serve path the web server never sees the .br, so without
109 // this a br-capable client got the plain .html — precompression
110 // did nothing here. Falls through to plain readfile otherwise.
111 $br = self::maybe_serve_brotli( $file );
112 if ( null !== $br ) {
113 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- streaming a static cache file directly; WP_Filesystem would buffer through PHP memory and is not appropriate for response streaming.
114 readfile( $br );
115 exit;
116 }
117 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- readfile is optimal for streaming a static cache file directly to the visitor; WP_Filesystem would buffer through PHP memory and is not appropriate for response streaming.
118 readfile( $file );
119 exit;
120 }
121
122 // Cache miss → render fresh + write cache. On LiteSpeed we send an
123 // explicit "stand down" header so the server's LSCache module does
124 // NOT cache + shadow our response — xSpeed's own .htaccess static
125 // rewrite owns hit serving (and hit accounting) here, exactly as on
126 // Apache. See maybe_emit_lscache_headers() for the full rationale.
127 self::maybe_emit_lscache_headers();
128
129 // We're about to render fresh + cache → miss for this request.
130 Hit_Counter::record_miss();
131
132
133 // WP < 6.9 fallback: ob_start() with a callback, paired with an
134 // explicit shutdown close so the buffer lifecycle is visible to
135 // reviewers and Plugin Check, instead of relying on PHP's implicit
136 // request-end flush. We record our nesting level so close_buffer()
137 // flushes ONLY the buffer we opened.
138 ob_start( array( __CLASS__, 'finalize_buffer' ) );
139 self::$buffer_level = ob_get_level();
140
141 add_action( 'shutdown', array( __CLASS__, 'close_buffer' ), 0 );
142 }
143
144 /**
145 * Close the cache buffer opened by maybe_start_cache().
146 *
147 * Guarded by the recorded buffer level so we never flush a buffer that
148 * another plugin pushed on top of (or under) ours. If something else is
149 * currently on top, we leave the stack alone — PHP's shutdown sequence
150 * will unwind buffers in order and our finalize_buffer() callback will
151 * still run when our level becomes the topmost one.
152 */
153 public static function close_buffer() {
154 if ( null === self::$buffer_level ) {
155 return;
156 }
157 if ( ob_get_level() === self::$buffer_level ) {
158 ob_end_flush();
159 }
160 self::$buffer_level = null;
161 }
162
163 public static function should_cache() {
164 $opts = Settings::get();
165 if ( empty( $opts['cache_enabled'] ) ) {
166 return false;
167 }
168
169 if ( is_user_logged_in() || is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
170 return false;
171 }
172
173 if ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) {
174 return false;
175 }
176
177 // All exclusion knobs now owned by CacheModule.
178 $cache_opts = Settings_Manager::get( 'cache' );
179
180 $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : '';
181 if ( 'GET' !== $method ) {
182 return false;
183 }
184
185 // Search-results requests carry a `s` query param, which the
186 // query-string gate below would normally reject as "dynamic". An
187 // add-on (xspeed-pro search cache) can opt them in: when this is a
188 // genuine is_search() and the filter returns true, the `s` param is
189 // treated as cacheable (the search term goes into the cache key so
190 // different searches stay distinct — see cache_key()).
191 $cache_search = self::should_cache_search();
192
193 // Feed opt-in is resolved BEFORE the query-string gate so query-form
194 // feeds (/?feed=rss2, used on plain-permalink sites) aren't rejected
195 // as "dynamic" by that gate — the `feed` param is then allowed through
196 // just like the search `s` param. Feeds are excluded by default (the
197 // `/feed/` pattern in excluded_urls); an add-on (xspeed-pro feed cache)
198 // opts them back in via the filter. (FBS-82407 #4)
199 $is_feed_request = function_exists( 'is_feed' ) && is_feed();
200 /**
201 * Whether to cache the current feed request.
202 *
203 * Default false → feeds fall through to the normal URL-exclusion
204 * rules (so `/feed/` keeps them out). A listener returning true
205 * opts this feed request into caching.
206 *
207 * @param bool $cache_feed Whether to cache this feed request.
208 */
209 $cache_feed = $is_feed_request && (bool) apply_filters( 'xspeed_should_cache_feed', false );
210
211 // Query string handling: anything OUTSIDE the ignored-params
212 // allow-list (utm_*, fbclid, gclid by default) means a unique
213 // request that we don't want to share with the canonical cache
214 // entry. Skip cache rather than poison the key.
215 $query_raw = isset( $_SERVER['QUERY_STRING'] ) ? sanitize_text_field( wp_unslash( $_SERVER['QUERY_STRING'] ) ) : '';
216 if ( '' !== $query_raw ) {
217 $ignored = is_array( $cache_opts['ignored_query_params'] ?? null ) ? $cache_opts['ignored_query_params'] : array();
218 parse_str( $query_raw, $params );
219 foreach ( $params as $key => $_ ) {
220 // Allow the search param through when search caching is on.
221 if ( $cache_search && 's' === $key ) {
222 continue;
223 }
224 // Allow query-form feed params through when feed caching opted
225 // this request in (?feed=rss2 / &withcomments=1 on feeds).
226 if ( $cache_feed && in_array( $key, array( 'feed', 'withcomments', 'withoutcomments' ), true ) ) {
227 continue;
228 }
229 if ( ! self::query_key_is_ignored( (string) $key, $ignored ) ) {
230 return false;
231 }
232 }
233 }
234
235 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
236 $path = (string) strtok( $request_uri, '?' );
237
238 $excluded_urls = is_array( $cache_opts['excluded_urls'] ?? null ) ? $cache_opts['excluded_urls'] : array();
239 if ( ! $cache_feed && Glob_Matcher::any_match( $excluded_urls, $path ) ) {
240 return false;
241 }
242
243 // Cookie-based exclusion. We only check cookie NAMES (matching
244 // values would leak content-sensitive logic into the cache key
245 // rules); presence of any matching cookie name skips cache.
246 $excluded_cookies = is_array( $cache_opts['excluded_cookies'] ?? null ) ? $cache_opts['excluded_cookies'] : array();
247 if ( ! empty( $excluded_cookies ) && ! empty( $_COOKIE ) ) {
248 foreach ( array_keys( $_COOKIE ) as $cookie_name ) {
249 if ( Glob_Matcher::any_match( $excluded_cookies, (string) $cookie_name ) ) {
250 return false;
251 }
252 }
253 }
254
255 // User-agent bypass list. Substring match (not glob) since UA
256 // strings have so much variation that glob anchoring rarely
257 // helps and confuses users.
258 $bypass_uas = is_array( $cache_opts['bypass_user_agents'] ?? null ) ? $cache_opts['bypass_user_agents'] : array();
259 if ( ! empty( $bypass_uas ) ) {
260 $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
261 foreach ( $bypass_uas as $needle ) {
262 if ( '' !== $needle && false !== stripos( $ua, (string) $needle ) ) {
263 return false;
264 }
265 }
266 }
267
268 // Per-post override (Phase 3.4). Honored only on singular
269 // post-context requests — archives / 404s / taxonomies use the
270 // global policy above.
271 if ( Cache_Rules::should_skip_for_post( Cache_Rules::current_post_id() ) ) {
272 return false;
273 }
274
275 /**
276 * Final say on whether the current request is cacheable.
277 *
278 * Runs at template_redirect (full WP context), so listeners may use
279 * conditional tags (is_search(), is_feed(), is_404(),
280 * wp_is_maintenance_mode(), …). The core engine has already applied
281 * its own exclusion rules and reached `true`; a listener returning
282 * false vetoes caching for this request. This is the documented
283 * extension point add-ons (xspeed-pro) hook to add their own
284 * request-level cache policy without forking the engine.
285 *
286 * Note: this gates the WRITE side. The pre-WP drop-in
287 * (advanced-cache.php) cannot run PHP filters, so request types that
288 * must never be *served* from a stale file are handled by not
289 * writing them here and/or by purging — see the conflict notes in
290 * advanced-cache.php.
291 *
292 * @param bool $should_cache Whether to cache the current request.
293 */
294 return (bool) apply_filters( 'xspeed_should_cache', true );
295 }
296
297 /**
298 * Whether the current request is a 404 we may cache.
299 *
300 * True only when: it's a genuine main-query is_404(), an add-on opted
301 * in via `xspeed_should_cache_404` (default false), and the request
302 * isn't a transient 404 we must never freeze — maintenance mode or a
303 * 404 emitted while the DB/site is in an error state. The xspeed-pro
304 * 404 cache flips the filter; Free never caches 404s on its own.
305 */
306 public static function should_cache_404(): bool {
307 if ( ! function_exists( 'is_404' ) || ! is_404() ) {
308 return false;
309 }
310 // Never cache a 404 served because the site is down for
311 // maintenance — that screen disappears the moment maintenance
312 // ends, and a cached copy would outlive it.
313 if ( function_exists( 'wp_is_maintenance_mode' ) && wp_is_maintenance_mode() ) {
314 return false;
315 }
316
317 /**
318 * Whether to cache the current 404 response.
319 *
320 * Default false. A listener returning true opts the (genuine)
321 * 404 into the page cache, served back for any unknown URL under
322 * one generic key. The 404 status is preserved on the HIT.
323 *
324 * @param bool $cache_404 Whether to cache this 404.
325 */
326 return (bool) apply_filters( 'xspeed_should_cache_404', false );
327 }
328
329 /**
330 * Whether the current request is an internal search-results page we
331 * may cache.
332 *
333 * True only when: it's a genuine main-query is_search() with a
334 * non-empty term, and an add-on opted in via `xspeed_should_cache_search`
335 * (default false). The search term is folded into the cache key (see
336 * search_term() / cache_key()) so different searches stay distinct.
337 * The xspeed-pro search cache flips the filter; Free never caches
338 * search results on its own.
339 */
340 public static function should_cache_search(): bool {
341 if ( ! function_exists( 'is_search' ) || ! is_search() ) {
342 return false;
343 }
344 // Empty search (`?s=`) renders the same as a normal archive and
345 // carries no term to key on — let it fall through to the usual
346 // rules rather than caching an ambiguous entry.
347 if ( '' === self::search_term() ) {
348 return false;
349 }
350
351 /**
352 * Whether to cache the current search-results request.
353 *
354 * Default false. A listener returning true opts the search page
355 * into the cache, keyed by the normalized search term.
356 *
357 * @param bool $cache_search Whether to cache this search request.
358 */
359 return (bool) apply_filters( 'xspeed_should_cache_search', false );
360 }
361
362 /**
363 * The current request's normalized search term, or '' if none. Reads
364 * the raw `s` query param (works on the pre-WP drop-in path too, where
365 * get_search_query() isn't available), trims + lowercases so
366 * "WordPress" and "wordpress" share one entry, and collapses internal
367 * whitespace.
368 */
369 public static function search_term(): string {
370 $raw = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only cache-key derivation from a public search param; no state change.
371 $raw = trim( $raw );
372 if ( '' === $raw ) {
373 return '';
374 }
375 $raw = preg_replace( '/\s+/', ' ', $raw );
376 return function_exists( 'mb_strtolower' ) ? mb_strtolower( $raw ) : strtolower( $raw );
377 }
378
379 /**
380 * Is this query-string key on the ignored-params allow-list? Supports
381 * trailing-star globs (`utm_*` matches `utm_source`, `utm_medium`,
382 * etc.) so users don't have to enumerate every UTM variant.
383 */
384 private static function query_key_is_ignored( string $key, array $ignored ): bool {
385 return Glob_Matcher::any_match( $ignored, $key );
386 }
387
388 public static function cache_key() {
389 $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : 'default';
390
391 // Cacheable 404s share ONE generic per-host entry — keying them by
392 // URL would let a scanner flood (millions of random paths) bloat
393 // the cache with identical 404 bodies. Both the write and the HIT
394 // lookup run through here, so they agree on the key automatically.
395 if ( self::should_cache_404() ) {
396 return md5( $host . '|404' );
397 }
398
399 $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/';
400 // Strip the query string from the key so /post and /post?utm_*=…
401 // share the same cache entry. should_cache() above already
402 // rejected requests with non-ignored params, so by the time we
403 // build the key the only params left are safe to drop.
404 $uri = (string) strtok( $uri, '?' );
405
406 // Optional device bucket: when mobile_separate is on, mobile and
407 // desktop responses live in different cache files so themes that
408 // serve different HTML by device (AMP, WPtouch, Jetpack mobile)
409 // can't poison each other.
410 $device = '';
411 $opts = Settings_Manager::get( 'cache' );
412 if ( ! empty( $opts['mobile_separate'] ) ) {
413 $device = self::is_mobile_request() ? '|m' : '|d';
414 }
415
416 // Search-results requests fold the normalized term into the key so
417 // /?s=foo and /?s=bar get distinct entries (the query string is
418 // otherwise stripped above). Only added when search caching opted
419 // in, so non-search URLs are unaffected.
420 $search = self::should_cache_search() ? '|s=' . self::search_term() : '';
421
422 // Query-form feeds (/?feed=rss2 vs /?feed=atom) share the same path
423 // once the query is stripped, so fold the feed type into the key to
424 // keep the flavors distinct. Pretty-permalink feeds (/feed/rss/) carry
425 // the type in $uri already and are unaffected. (FBS-82407 #4)
426 $feed = '';
427 if ( function_exists( 'is_feed' ) && is_feed() && function_exists( 'get_query_var' ) ) {
428 $feed_type = (string) get_query_var( 'feed' );
429 if ( '' !== $feed_type ) {
430 $feed = '|feed=' . preg_replace( '/[^a-z0-9]/i', '', $feed_type );
431 }
432 }
433
434 return md5( $host . $uri . $device . $search . $feed );
435 }
436
437 /**
438 * Server-side mobile detection. Prefers WordPress's `wp_is_mobile()`
439 * which uses the same UA tokens as core (so our bucket aligns with
440 * whatever theme-side branching uses). Falls back to a tiny inline
441 * detector if wp_is_mobile() isn't loaded (e.g. the drop-in path).
442 */
443 private static function is_mobile_request(): bool {
444 if ( function_exists( 'wp_is_mobile' ) ) {
445 return (bool) wp_is_mobile();
446 }
447 // Fallback for the rare context where wp_is_mobile() isn't loaded.
448 // Mirrors core's wp_is_mobile() EXACTLY — including the
449 // Sec-CH-UA-Mobile client hint it checks *before* UA tokens — so the
450 // bucket this picks matches whatever the engine's primary path (and
451 // the drop-in's own copy of this logic) would pick for the same
452 // request. Drift here re-introduces the cross-path key mismatch.
453 if ( isset( $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ) ) {
454 return '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE'];
455 }
456 $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
457 if ( '' === $ua ) {
458 return false;
459 }
460 return (bool) preg_match( '/(Mobile|Android|Silk\/|Kindle|BlackBerry|Opera Mini|Opera Mobi)/i', $ua );
461 }
462
463 public static function cache_file_for( $key ) {
464 return XSPEED_CACHE_DIR . '/' . $key . '.html';
465 }
466
467 /**
468 * If a precompressed Brotli sibling (`<file>.br`) exists and the client
469 * advertises `Accept-Encoding: br`, emit the Brotli response headers and
470 * return the `.br` path to stream. Returns null to fall through to the
471 * plain file. Keeps the PHP serve path in parity with the web server's
472 * static .br serving (mod_brotli / ngx_brotli rewrite).
473 *
474 * Free has no Brotli logic of its own — this only fires when an add-on
475 * (the Pro Brotli module) actually wrote the .br, so it's a safe no-op
476 * on Free-only installs.
477 *
478 * @param string $file Absolute path to the cached .html file.
479 * @return string|null The .br path to stream, or null to serve $file.
480 */
481 public static function maybe_serve_brotli( string $file ): ?string {
482 if ( headers_sent() ) {
483 return null;
484 }
485 $accept = isset( $_SERVER['HTTP_ACCEPT_ENCODING'] )
486 ? strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) )
487 : '';
488 // Match `br` as a token (comma/space delimited), not a substring, so
489 // a hypothetical "xbr" encoding can't false-positive.
490 if ( ! preg_match( '/(^|[\s,])br([\s,;]|$)/', $accept ) ) {
491 return null;
492 }
493 $br = $file . '.br';
494 if ( ! is_string( $br ) || ! file_exists( $br ) || ! is_readable( $br ) ) {
495 return null;
496 }
497 header( 'Content-Encoding: br' );
498 header( 'Vary: Accept-Encoding', false );
499 // The byte length changes for the compressed body — drop any
500 // Content-Length the caller may have set so the stream isn't
501 // truncated/padded. readfile() lets the SAPI set the right length.
502 header_remove( 'Content-Length' );
503 return $br;
504 }
505
506 /**
507 * Sidecar metadata file for a cache entry. Holds response bits the HIT
508 * path must replay — Content-Type (cached feeds → application/rss+xml,
509 * sitemaps → text/xml) and status (a cached 404 must serve 404, not
510 * 200). JSON, one tiny file per entry, written only when there's
511 * something non-default to replay.
512 */
513 public static function cache_meta_for( $key ) {
514 return XSPEED_CACHE_DIR . '/' . $key . '.meta';
515 }
516
517 /**
518 * Read the .meta sidecar for a cache entry as an array, or [] if none.
519 * Keys: 'content_type' (string), 'status' (int). Used on the HIT path
520 * to replay them before streaming the file.
521 */
522 private static function read_meta( $key ): array {
523 $meta_file = self::cache_meta_for( $key );
524 if ( ! file_exists( $meta_file ) ) {
525 return array();
526 }
527 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- our own cache dir; WP_Filesystem needs admin creds unavailable on a frontend HIT.
528 $raw = file_get_contents( $meta_file );
529 $data = json_decode( (string) $raw, true );
530 return is_array( $data ) ? $data : array();
531 }
532
533 /**
534 * Conditional-GET support for a cache HIT. Emits Last-Modified + ETag
535 * derived from the cache file's mtime, and — when the request's
536 * If-Modified-Since / If-None-Match still match — sends 304 Not Modified
537 * and returns true (caller should exit without a body). Returns false to
538 * proceed with a normal 200 body. Lets aggregators/browsers skip
539 * re-downloading an unchanged cached response. (FBS-82407 #5)
540 *
541 * @param string $file Absolute path to the cache .html file.
542 * @return bool True when a 304 was sent.
543 */
544 public static function serve_not_modified( string $file ): bool {
545 $mtime = (int) filemtime( $file );
546 if ( $mtime <= 0 ) {
547 return false;
548 }
549 $last_modified = gmdate( 'D, d M Y H:i:s', $mtime ) . ' GMT';
550 $etag = '"' . md5( $file . '|' . $mtime ) . '"';
551 header( 'Last-Modified: ' . $last_modified );
552 header( 'ETag: ' . $etag );
553
554 $ims = isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? trim( sanitize_text_field( wp_unslash( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) ) : '';
555 $inm = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? trim( sanitize_text_field( wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) ) : '';
556
557 $etag_match = '' !== $inm && false !== strpos( $inm, $etag );
558 $time_match = '' !== $ims && ( strtotime( $ims ) >= $mtime );
559
560 if ( $etag_match || $time_match ) {
561 if ( function_exists( 'http_response_code' ) ) {
562 http_response_code( 304 );
563 }
564 return true;
565 }
566 return false;
567 }
568
569 public static function is_expired( $file ) {
570 // cache_expiry now owned by CacheModule; per-post override
571 // (Phase 3.4) shrinks the TTL further when the editor set one.
572 $opts = Settings_Manager::get( 'cache' );
573 $max_age = (int) $opts['cache_expiry'] * HOUR_IN_SECONDS;
574 $post_override = Cache_Rules::expiry_override_seconds_for_post( Cache_Rules::current_post_id() );
575 if ( null !== $post_override ) {
576 $max_age = $post_override;
577 }
578
579 /**
580 * Filter the max-age (seconds) for the current cache entry.
581 *
582 * Lets an add-on apply a request-type-specific TTL — e.g. the
583 * xspeed-pro feed cache gives feeds a longer expiry than pages,
584 * since aggregators tolerate more staleness. Return seconds.
585 *
586 * @param int $max_age Computed max-age in seconds.
587 */
588 $max_age = (int) apply_filters( 'xspeed_cache_max_age', $max_age );
589
590 return ( time() - filemtime( $file ) ) > $max_age;
591 }
592
593 /**
594 * Accumulator for the full response body across all output-handler phases.
595 *
596 * PHP invokes an ob_start() callback once per flush, and each invocation
597 * only receives the chunk produced *since the previous flush*. If anything
598 * during the render calls `ob_flush()` or `flush()` (some themes, lazy-
599 * load plugins, AMP, etc. do), the final-phase call would otherwise only
600 * see the tail of the page — and we'd cache a truncated response that
601 * gets served repeatedly until purge. We accumulate every chunk here so
602 * the cache file always reflects the complete page.
603 *
604 * @var string
605 */
606 private static $accumulated = '';
607
608 public static function finalize_buffer( $buffer, $phase = PHP_OUTPUT_HANDLER_FINAL ) {
609 self::$accumulated .= $buffer;
610
611 // On non-final phases (mid-request flushes), pass the current chunk
612 // through to the client unmodified and keep collecting. The WP 6.9
613 // filter path always passes the full body in one shot with the
614 // default $phase, so it falls straight through to the final block.
615 $is_final = ( $phase & ( PHP_OUTPUT_HANDLER_FINAL | PHP_OUTPUT_HANDLER_END ) ) !== 0;
616 if ( ! $is_final ) {
617 return $buffer;
618 }
619
620 $full = self::$accumulated;
621 self::$accumulated = '';
622
623 if ( strlen( $full ) < 255 ) {
624 return $buffer;
625 }
626
627 // Status gate. We cache 200 by default. A 404 may be cached too,
628 // but only when an add-on (xspeed-pro 404 cache) opts in for a
629 // genuine is_404() — never a transient 404 (maintenance screen,
630 // DB error, or a 404 emitted outside the main query), which would
631 // otherwise be frozen until purge. Any other status is skipped.
632 $status = function_exists( 'http_response_code' ) ? (int) http_response_code() : 200;
633 if ( 200 !== $status ) {
634 if ( 404 !== $status || ! self::should_cache_404() ) {
635 return $buffer;
636 }
637 }
638
639 // If no mid-request flush happened, $buffer === $full and we can
640 // safely minify the on-wire bytes too. Otherwise earlier chunks have
641 // already been sent unminified, so we minify only what goes to disk —
642 // the first visitor sees unminified HTML, every cache hit after that
643 // is minified.
644 $single_chunk = ( $buffer === $full );
645
646 // minify_html now owned by the Minify module; read through the
647 // module's storage so this stays consistent with the engine that
648 // applies CSS/JS minification.
649 $minify_opts = Settings_Manager::get( 'minify' );
650 if ( ! empty( $minify_opts['minify_html'] ) ) {
651 $full = Minifier::minify_html( $full );
652 if ( $single_chunk ) {
653 $buffer = $full;
654 }
655 }
656
657 if ( ! file_exists( XSPEED_CACHE_DIR ) ) {
658 wp_mkdir_p( XSPEED_CACHE_DIR );
659 self::write_silence( XSPEED_CACHE_DIR );
660 }
661
662 // Path safety: cache_file_for() builds `XSPEED_CACHE_DIR . '/' . $key . '.html'`
663 // where $key comes from md5() — guaranteed to be exactly 32 lowercase
664 // hex chars, so no traversal sequence ('..', '/', null byte, etc.)
665 // can appear. The write is therefore always inside XSPEED_CACHE_DIR.
666 $key = self::cache_key();
667 $file = self::cache_file_for( $key );
668 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_put_contents_file_put_contents -- WP_Filesystem requires admin context for credentials; cache writes happen on frontend requests where it's unavailable.
669 file_put_contents( $file, $full, LOCK_EX );
670
671 // Persist a non-default Content-Type so the HIT path can replay it
672 // (cached feeds must serve application/rss+xml, not text/html).
673 // Only written when the response set a content-type other than
674 // the HTML default — pages don't pay for an extra file.
675 self::write_meta( $key );
676
677 // Static-cache tree (xspeed-static/{host}{path}/index.html). The
678 // .htaccess rewrite block serves this file directly via the web
679 // server, bypassing PHP for ~3-5× lower TTFB vs the drop-in path.
680 // store_static() returns silently on any path/permission issue —
681 // the drop-in remains the safety net.
682 //
683 // Skip it entirely when mobile_separate is on: the rewrite is
684 // disabled in that mode (static_rewrite_allowed()), so a static file
685 // would only be dead weight — and a device-blind one at that.
686 // Skip the static-tree write for responses the web server can't replay
687 // correctly: a non-200 status (a cached 404 would be served as a soft
688 // 200, FBS-82406) or a non-HTML content-type (a cached feed would go
689 // out as text/html, FBS-82407). The web server serves these .html files
690 // directly with no PHP, so there's no .meta replay — keep them on the
691 // drop-in / PHP path instead, which DOES replay status + content-type.
692 if ( self::static_rewrite_allowed() && self::response_is_plain_html() ) {
693 self::store_static( $full );
694 }
695
696 return $buffer;
697 }
698
699 /**
700 * Write the current response to the static-cache tree at
701 * `xspeed-static/{host}{request_uri}/index.html`. The web-server
702 * rewrite block points at this path so cache hits skip PHP
703 * entirely. Caller already minified/finalized $html.
704 *
705 * Path safety: $host is restricted to a `[a-zA-Z0-9.\-]` allowlist;
706 * $uri has its query string stripped, null bytes removed, '..'
707 * sequences collapsed, and after concatenation we verify the
708 * resolved real path stays inside XSPEED_CACHE_STATIC_DIR before
709 * any write. Anything off the happy path returns silently.
710 */
711 private static function store_static( string $html ): void {
712 $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
713 $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
714 $host = preg_replace( '/[^a-zA-Z0-9.\-]/', '', $host );
715 $uri = str_replace( "\0", '', $uri );
716 $uri = (string) strtok( $uri, '?' );
717 if ( '' === $host || '' === $uri ) {
718 return;
719 }
720 // Collapse any traversal sequences before path resolution.
721 $uri = preg_replace( '#/+#', '/', $uri );
722 if ( false !== strpos( $uri, '..' ) ) {
723 return;
724 }
725
726 $base = rtrim( XSPEED_CACHE_STATIC_DIR, '/' );
727 $dir = $base . '/' . $host . rtrim( $uri, '/' );
728 $file = $dir . '/index.html';
729
730 // Resolve the parent against the cache root to be sure the
731 // final path is inside our tree even if the OS does anything
732 // funny with multi-byte sequences.
733 $base_real = realpath( WP_CONTENT_DIR );
734 if ( false === $base_real || 0 !== strpos( $base, $base_real ) ) {
735 return;
736 }
737
738 if ( ! file_exists( $dir ) ) {
739 wp_mkdir_p( $dir );
740 }
741 if ( ! is_dir( $dir ) ) {
742 return;
743 }
744 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_put_contents_file_put_contents -- Same rationale as the flat-hash cache write above: WP_Filesystem isn't available on frontend requests, and the cache write must happen during shutdown.
745 $written = file_put_contents( $file, $html, LOCK_EX );
746
747 if ( false !== $written ) {
748 /**
749 * Fires after a static cache file (index.html) is written.
750 *
751 * The extension point for serving pre-compressed siblings:
752 * the xspeed-pro Brotli module writes `index.html.br` next to
753 * the file here so the web server's static rewrite can serve a
754 * Brotli copy to clients that advertise `Accept-Encoding: br`,
755 * falling back to GZIP / the plain file otherwise. No core
756 * behavior depends on a listener being present.
757 *
758 * @param string $file Absolute path to the static cache file just written.
759 * @param string $html The HTML written to it.
760 */
761 do_action( 'xspeed_static_file_written', $file, $html );
762 }
763 }
764
765 /**
766 * Write the .meta sidecar for a cache entry when the response carries
767 * anything the HIT path must replay beyond a plain 200 text/html:
768 * - a non-HTML Content-Type (cached feeds → application/rss+xml,
769 * sitemaps → text/xml, …), and/or
770 * - a non-200 status (a cached 404 must serve 404, not 200).
771 *
772 * Ordinary 200 text/html pages get NO .meta file, so the common path
773 * stays a single write.
774 *
775 * @param string $key Cache key for the current request.
776 */
777 /**
778 * True only for a plain 200 text/html response — the only kind the
779 * web-server static tree can serve correctly (it streams the .html with
780 * no PHP, so it can't replay a 404 status or a feed Content-Type). Used
781 * to gate store_static() so cached 404s / feeds stay on the replay-capable
782 * drop-in / PHP path. (FBS-82406, FBS-82407)
783 */
784 private static function response_is_plain_html(): bool {
785 $status = function_exists( 'http_response_code' ) ? (int) http_response_code() : 200;
786 if ( 200 !== $status && $status > 0 ) {
787 return false;
788 }
789 foreach ( headers_list() as $header ) {
790 if ( 0 === stripos( $header, 'content-type:' ) ) {
791 $ct = trim( substr( $header, strlen( 'content-type:' ) ) );
792 if ( '' !== $ct && false === stripos( $ct, 'text/html' ) ) {
793 return false;
794 }
795 }
796 }
797 return true;
798 }
799
800 private static function write_meta( string $key ): void {
801 $content_type = '';
802 foreach ( headers_list() as $header ) {
803 if ( 0 === stripos( $header, 'content-type:' ) ) {
804 $content_type = trim( substr( $header, strlen( 'content-type:' ) ) );
805 }
806 }
807 $status = function_exists( 'http_response_code' ) ? (int) http_response_code() : 200;
808
809 $meta = array();
810 $is_default_type = ( '' === $content_type || false !== stripos( $content_type, 'text/html' ) );
811 if ( ! $is_default_type ) {
812 $meta['content_type'] = $content_type;
813 }
814 if ( 200 !== $status && $status > 0 ) {
815 $meta['status'] = $status;
816 }
817
818 // Per-content TTL (seconds). The drop-in and static fast paths can't
819 // call is_expired() / the xspeed_cache_max_age filter (they run before
820 // WP), so persist the resolved max-age here whenever it differs from
821 // the plain page TTL — e.g. the Pro feed cache's 12h vs the 24h page
822 // default. The fast paths read this to expire correctly. (FBS-82407)
823 $opts = Settings_Manager::get( 'cache' );
824 $default_ttl = (int) $opts['cache_expiry'] * HOUR_IN_SECONDS;
825 $ttl = (int) apply_filters( 'xspeed_cache_max_age', $default_ttl );
826 if ( $ttl > 0 && $ttl !== $default_ttl ) {
827 $meta['ttl'] = $ttl;
828 }
829
830 // Nothing to replay → no sidecar.
831 if ( empty( $meta ) ) {
832 return;
833 }
834
835 $payload = wp_json_encode( $meta );
836 if ( false === $payload ) {
837 return;
838 }
839 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_put_contents_file_put_contents -- our own cache dir; WP_Filesystem needs admin creds unavailable on a frontend shutdown write.
840 file_put_contents( self::cache_meta_for( $key ), $payload, LOCK_EX );
841 }
842
843 /**
844 * @param string $cause Free-form human reason. Recorded in the
845 * Activity log to give users context (e.g.
846 * 'post saved', 'settings change', 'manual',
847 * 'theme switch').
848 */
849 public static function purge_all( string $cause = 'manual' ) {
850 $count = 0;
851 if ( is_dir( XSPEED_CACHE_DIR ) ) {
852 $files = glob( XSPEED_CACHE_DIR . '/*.html' );
853 if ( $files ) {
854 $count = count( $files );
855 foreach ( $files as $f ) {
856 wp_delete_file( $f );
857 }
858 }
859 // Remove the .meta sidecars (content-type for feeds/sitemaps)
860 // alongside their .html entries. Not counted — they're not
861 // cache "pages", just per-entry metadata.
862 $meta = glob( XSPEED_CACHE_DIR . '/*.meta' );
863 if ( $meta ) {
864 foreach ( $meta as $m ) {
865 wp_delete_file( $m );
866 }
867 }
868 // Remove precompressed siblings (e.g. <key>.html.br from the Pro
869 // Brotli module). Not counted — same as .meta. Without this a
870 // purge leaves stale .br bodies behind: disk bloat, and a
871 // staleness window if precompression is later disabled.
872 $br = glob( XSPEED_CACHE_DIR . '/*.br' );
873 if ( $br ) {
874 foreach ( $br as $b ) {
875 wp_delete_file( $b );
876 }
877 }
878 }
879 // Static-cache tree purge — recursive because the layout is
880 // xspeed-static/{host}/{path}/index.html, so a flat glob can't
881 // reach everything.
882 if ( is_dir( XSPEED_CACHE_STATIC_DIR ) ) {
883 $count += self::rmtree_html( XSPEED_CACHE_STATIC_DIR );
884 }
885 // REST response cache (cache/xspeed/rest/*.json) — same purge
886 // triggers (publish, settings change) invalidate it too.
887 $count += Rest_Cache::purge();
888
889 // Minified + combined CSS/JS (cache/xspeed/min/ and min/combined/).
890 // purge_all is a full filesystem sweep and must clear these too, even
891 // when the Minify module is currently disabled — orphaned min/ files
892 // from a feature the user later turned off must still be removed, and
893 // a stale combined-<hash>.css that the regenerated page no longer
894 // references otherwise 404s and breaks the frontend. (FBS-83114/83116)
895 if ( class_exists( '\\XSpeed\\Minifier' ) ) {
896 Minifier::purge_minified();
897 }
898
899 // Persistent object cache (Redis / Memcached). Flush regardless of
900 // whether the Object Cache module is currently enabled — a drop-in
901 // installed earlier keeps serving until flushed.
902 if ( function_exists( 'wp_cache_flush' ) ) {
903 wp_cache_flush();
904 }
905
906 self::update_stats( array( 'last_purge' => time() ) );
907
908 // Fire AFTER the local sweep so module listeners (Critical CSS,
909 // Unused CSS, Cloudflare edge purge) run — this action had three
910 // registered listeners but was never emitted. Treat it as additive
911 // (CDN / edge invalidation), not the mechanism for clearing local
912 // files. (FBS-83114)
913 do_action( 'xspeed_after_purge_all', $cause );
914
915 // Trigger of WP_CLI / hook / admin-bar purges all hit the same
916 // path. Record once with the supplied cause so the dashboard
917 // activity feed reads naturally.
918 Activity_Log::record(
919 'cache_purged',
920 sprintf( 'Cache purged (%s) — %d file%s removed', $cause, $count, 1 === $count ? '' : 's' ),
921 Activity_Log::INFO
922 );
923
924 return $count;
925 }
926
927 /**
928 * The per-type purge menu, LiteSpeed-style. Each entry is a cache type
929 * the user can purge individually from the admin-bar dropdown. `visible`
930 * controls whether the item shows (active + licensed module only) — it
931 * NEVER limits Purge All, which always sweeps everything on disk.
932 *
933 * Pro registers its own types (Critical CSS, Unused CSS, …) by filtering
934 * `xspeed_purge_types`, so Free degrades gracefully when Pro is absent.
935 *
936 * @return array<string,array{label:string,visible:bool}>
937 */
938 public static function purge_types(): array {
939 $minify_on = false;
940 if ( class_exists( '\\XSpeed\\Settings_Manager' ) ) {
941 $min = Settings_Manager::get( 'minify' );
942 $minify_on = ! empty( $min['minify_css'] ) || ! empty( $min['minify_js'] ) || ! empty( $min['combine_css'] ) || ! empty( $min['combine_js'] );
943 }
944 // Object cache is "active" when an external object-cache drop-in is in
945 // use — the canonical WP signal, independent of our settings option.
946 $oc_on = function_exists( 'wp_using_ext_object_cache' ) && wp_using_ext_object_cache();
947
948 $types = array(
949 'all' => array(
950 'label' => __( 'Purge All', 'xspeed' ),
951 'visible' => true,
952 ),
953 'page' => array(
954 'label' => __( 'Purge Page / Static Cache', 'xspeed' ),
955 'visible' => true,
956 ),
957 'assets' => array(
958 'label' => __( 'Purge CSS / JS Cache', 'xspeed' ),
959 'visible' => $minify_on,
960 ),
961 'object' => array(
962 'label' => __( 'Purge Object Cache', 'xspeed' ),
963 'visible' => $oc_on,
964 ),
965 'rest' => array(
966 'label' => __( 'Purge REST Cache', 'xspeed' ),
967 'visible' => true,
968 ),
969 );
970
971 /**
972 * Filter the admin-bar purge-type menu. Pro modules add their own
973 * (Critical CSS, Unused CSS, CDN). Adding a type here only adds a
974 * MENU item — purge_type() must know how to handle the same slug.
975 *
976 * @param array $types Map of slug => [label, visible].
977 */
978 return (array) apply_filters( 'xspeed_purge_types', $types );
979 }
980
981 /**
982 * Purge a single cache type by slug. 'all' delegates to purge_all();
983 * every other slug clears just its own artifacts. Unknown slugs (e.g. a
984 * Pro type) fan out via the `xspeed_purge_type_{slug}` action so the
985 * owning module can handle it. Returns the number of items removed where
986 * countable.
987 */
988 public static function purge_type( string $type ): int {
989 switch ( $type ) {
990 case 'all':
991 return self::purge_all( 'manual' );
992
993 case 'page':
994 $count = 0;
995 if ( is_dir( XSPEED_CACHE_DIR ) ) {
996 foreach ( (array) glob( XSPEED_CACHE_DIR . '/*.html' ) as $f ) {
997 wp_delete_file( $f );
998 ++$count;
999 }
1000 foreach ( (array) glob( XSPEED_CACHE_DIR . '/*.meta' ) as $m ) {
1001 wp_delete_file( $m );
1002 }
1003 foreach ( (array) glob( XSPEED_CACHE_DIR . '/*.br' ) as $b ) {
1004 wp_delete_file( $b );
1005 }
1006 }
1007 if ( is_dir( XSPEED_CACHE_STATIC_DIR ) ) {
1008 $count += self::rmtree_html( XSPEED_CACHE_STATIC_DIR );
1009 }
1010 self::update_stats( array( 'last_purge' => time() ) );
1011 return $count;
1012
1013 case 'assets':
1014 if ( class_exists( '\\XSpeed\\Minifier' ) ) {
1015 Minifier::purge_minified();
1016 }
1017 return 0;
1018
1019 case 'object':
1020 if ( function_exists( 'wp_cache_flush' ) ) {
1021 wp_cache_flush();
1022 }
1023 return 0;
1024
1025 case 'rest':
1026 return Rest_Cache::purge();
1027
1028 default:
1029 // Pro / third-party type — let the owning module handle it.
1030 do_action( 'xspeed_purge_type_' . $type );
1031 return 0;
1032 }
1033 }
1034
1035 /**
1036 * Recursively delete every `index.html` (and its precompressed
1037 * `index.html.br` sibling, if the Pro Brotli module wrote one) plus
1038 * empty directories inside the static-cache tree. Used by purge_all().
1039 * Returns the number of .html files removed so purge stats stay accurate
1040 * across the flat + static caches — .br siblings are not counted
1041 * (they're encodings of a page, not pages).
1042 */
1043 private static function rmtree_html( string $dir ): int {
1044 if ( ! is_dir( $dir ) ) {
1045 return 0;
1046 }
1047 $removed = 0;
1048 // SCANDIR_SORT_NONE skips alphabetic sort — we're going to walk
1049 // the whole tree regardless of order.
1050 $entries = @scandir( $dir, SCANDIR_SORT_NONE ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1051 if ( false === $entries ) {
1052 return 0;
1053 }
1054 foreach ( $entries as $entry ) {
1055 if ( '.' === $entry || '..' === $entry ) {
1056 continue;
1057 }
1058 $path = $dir . '/' . $entry;
1059 if ( is_dir( $path ) ) {
1060 $removed += self::rmtree_html( $path );
1061 // Best-effort empty-dir cleanup; ignore failures (a
1062 // foreign file inside would block rmdir, which is fine).
1063 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- Best-effort empty-dir cleanup; WP_Filesystem needs admin credentials we don't have during a normal purge.
1064 @rmdir( $path );
1065 continue;
1066 }
1067 if ( substr( $entry, -5 ) === '.html' ) {
1068 wp_delete_file( $path );
1069 ++$removed;
1070 } elseif ( substr( $entry, -3 ) === '.br' ) {
1071 // Precompressed sibling (index.html.br). Remove it too so a
1072 // purge doesn't orphan stale Brotli bodies. Not counted.
1073 wp_delete_file( $path );
1074 }
1075 }
1076 return $removed;
1077 }
1078
1079 /**
1080 * Drop a "silence is golden" index.php into a directory so apaches/nginx
1081 * with directory listing enabled don't expose cache contents.
1082 */
1083 public static function write_silence( $dir ) {
1084 $file = trailingslashit( $dir ) . 'index.php';
1085 if ( ! file_exists( $file ) ) {
1086 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_put_contents_file_put_contents -- WP_Filesystem requires admin context for credentials; cache dir setup may run during a frontend page render.
1087 file_put_contents( $file, "<?php\n// Silence is golden.\n" );
1088 }
1089 }
1090
1091 /**
1092 * Persist stats with autoload disabled — stats are only read in admin
1093 * contexts, so there is no reason to inflate every frontend request's
1094 * `wp_load_alloptions()` payload.
1095 */
1096 private static function update_stats( array $stats ) {
1097 if ( false === get_option( 'xspeed_stats' ) ) {
1098 add_option( 'xspeed_stats', $stats, '', 'no' );
1099 return;
1100 }
1101 update_option( 'xspeed_stats', $stats );
1102 }
1103
1104 public static function get_stats() {
1105 $count = 0;
1106 $size = 0;
1107 if ( is_dir( XSPEED_CACHE_DIR ) ) {
1108 $files = glob( XSPEED_CACHE_DIR . '/*.html' );
1109 if ( $files ) {
1110 $count = count( $files );
1111 foreach ( $files as $f ) {
1112 $size += filesize( $f );
1113 }
1114 }
1115 }
1116 // Drain the HIT-log file BEFORE reading totals. Two serve paths that
1117 // bypass the normal in-PHP record_hit() append one line per HIT here:
1118 // the nginx server-level rewrite (see nginx_snippet(), never reaches
1119 // PHP) and the advanced-cache.php drop-in (runs pre-WordPress, can't
1120 // reach Hit_Counter). Without this drain both look like a 0% hit-ratio
1121 // on a perfectly working cache.
1122 Hit_Counter::collect_nginx_log_hits();
1123
1124 // Apache/LiteSpeed static-rewrite HITs are served straight from disk
1125 // by .htaccess and never reach PHP either — but there's no .htaccess
1126 // equivalent of nginx's access_log directive, so we count them by
1127 // scanning the web server's own access log incrementally. No-op when
1128 // the log isn't readable (managed hosts) — see the method docblock.
1129 Hit_Counter::collect_server_log_hits();
1130
1131 $stats = get_option( 'xspeed_stats', array() );
1132 $totals = Hit_Counter::totals_24h();
1133 return array(
1134 'cached_pages' => $count,
1135 'cache_size' => $size,
1136 'last_purge' => isset( $stats['last_purge'] ) ? (int) $stats['last_purge'] : 0,
1137 // Rolling 24h cache performance — sourced from Hit_Counter's
1138 // hourly buckets. The frontend uses hit_ratio to drive the
1139 // CacheHero stat grid + the Health module's panel.
1140 'hits_24h' => $totals['hits'],
1141 'misses_24h' => $totals['misses'],
1142 'hit_ratio' => $totals['ratio'],
1143 );
1144 }
1145
1146 /**
1147 * Apply the user's enable/disable choice. Called only from the REST
1148 * toggle endpoint, which is gated by current_user_can( 'manage_options' )
1149 * and a verified REST nonce. This is the only place the drop-in and
1150 * the WP_CACHE constant are written — they MUST NOT happen on
1151 * register_activation_hook (WordPress.org review requirement).
1152 *
1153 * @param bool $enable User's choice.
1154 * @return array{
1155 * enabled: bool,
1156 * dropin_installed: bool,
1157 * wp_cache_constant: bool,
1158 * wp_config_writable: bool,
1159 * manual_snippet: ?string
1160 * }
1161 */
1162 public static function toggle( $enable ) {
1163 $enable = (bool) $enable;
1164
1165 if ( $enable ) {
1166 $dropin_ok = self::install_dropin();
1167 $wp_config_ok = self::set_wp_cache_constant( true );
1168 $rewrite_ok = self::install_rewrite();
1169 self::ensure_hits_log_file();
1170 self::sync_mobile_flag();
1171 $snippet = $wp_config_ok ? null : "define( 'WP_CACHE', true );";
1172
1173 Activity_Log::record(
1174 'cache_enabled_event',
1175 $wp_config_ok
1176 ? 'Cache enabled. Drop-in installed, WP_CACHE constant set.'
1177 : 'Cache enabled. Drop-in installed; wp-config.php not writable — add the WP_CACHE snippet manually.',
1178 $wp_config_ok ? Activity_Log::SUCCESS : Activity_Log::WARN
1179 );
1180
1181 return array(
1182 'enabled' => true,
1183 'dropin_installed' => (bool) $dropin_ok,
1184 'wp_cache_constant' => (bool) $wp_config_ok,
1185 'rewrite_installed' => (bool) $rewrite_ok,
1186 'wp_config_writable' => self::wp_config_writable(),
1187 'manual_snippet' => $snippet,
1188 'nginx_snippet' => self::nginx_snippet(),
1189 // Unified server-block snippet aggregating every enabled
1190 // module's directives — the same value the dashboard and
1191 // Health insight render. The wizard shows this so all three
1192 // surfaces stay in lockstep. Null on non-nginx hosts.
1193 'nginx_server_block' => self::full_nginx_server_block(),
1194 );
1195 }
1196
1197 self::remove_dropin();
1198 self::set_wp_cache_constant( false );
1199 self::remove_rewrite();
1200 // Drop the device-bucket marker too — with the drop-in gone there's
1201 // nothing left to read it, and leaving it behind would dirty a fresh
1202 // re-enable (and leaks across test runs).
1203 self::sync_mobile_flag( false );
1204
1205 Activity_Log::record(
1206 'cache_disabled_event',
1207 'Cache disabled. Drop-in removed.',
1208 Activity_Log::INFO
1209 );
1210
1211 return array(
1212 'enabled' => false,
1213 'dropin_installed' => false,
1214 'wp_cache_constant' => false,
1215 'rewrite_installed' => false,
1216 'wp_config_writable' => self::wp_config_writable(),
1217 'manual_snippet' => null,
1218 'nginx_snippet' => self::nginx_snippet(),
1219 'nginx_server_block' => self::full_nginx_server_block(),
1220 );
1221 }
1222
1223 /**
1224 * Check wp-config.php writability via WP_Filesystem. Plugin Check flags
1225 * direct is_writable() under WordPress.WP.AlternativeFunctions.
1226 */
1227 private static function wp_config_writable() {
1228 global $wp_filesystem;
1229 if ( ! function_exists( 'WP_Filesystem' ) ) {
1230 require_once ABSPATH . 'wp-admin/includes/file.php';
1231 }
1232 WP_Filesystem();
1233
1234 return $wp_filesystem ? (bool) $wp_filesystem->is_writable( ABSPATH . 'wp-config.php' ) : false;
1235 }
1236
1237 /**
1238 * Nginx server-block snippet mirroring the Apache rewrite block.
1239 * We never auto-write nginx config — it sits outside the WordPress
1240 * root and is owned by the server admin — but the dashboard
1241 * surfaces this snippet when nginx is detected so the admin can
1242 * paste it once and unlock the same PHP-bypass speedup we get on
1243 * Apache / LiteSpeed via .htaccess.
1244 *
1245 * Returns null when the server isn't nginx (no point showing it).
1246 */
1247 /**
1248 * Create wp-content/cache/xspeed/hits.log as an empty file so the
1249 * server-level rewrite's `access_log` directive has somewhere to
1250 * write on first request. Idempotent — touches an existing file
1251 * without disturbing accumulated lines. Called from Cache::toggle()
1252 * on enable and from auto_heal() when the file is missing.
1253 *
1254 * Permissions matter here. The file is created by PHP-FPM (often uid
1255 * www-data), but the nginx process that appends HIT lines may run as a
1256 * DIFFERENT uid — on multi-container hosts (e.g. xclude/Kinsta: nginx in
1257 * its own container as uid `nginx`, PHP-FPM in another as `www-data`)
1258 * they don't share a user at all. A default-umask 0644 file is then
1259 * unwritable by nginx, the access_log write silently fails, and the
1260 * dashboard shows a 0% hit ratio even though static HITs are serving.
1261 * So we widen the dir to 0777 and the file to 0666 — group/other write —
1262 * so whatever uid nginx runs as can append. (The file holds only HIT
1263 * request lines, no secrets.)
1264 */
1265 /**
1266 * Directory holding the nginx hit log. Lives under uploads/, NOT the
1267 * cache dir — uninstall.php and a cache purge both delete the cache
1268 * dir, which would orphan the pasted nginx `access_log` directive's
1269 * parent directory and make `nginx -t` fail [emerg], taking down every
1270 * vhost on the host (FBS-82478). uploads/ always exists, isn't a
1271 * plugin-managed cache dir, and is never deleted on uninstall — so the
1272 * directive's target dir survives both, and nginx (which creates a
1273 * missing log FILE but not a missing DIR) can always open it.
1274 *
1275 * Falls back to the cache dir only if uploads is somehow unavailable.
1276 */
1277 public static function hits_log_dir(): string {
1278 if ( function_exists( 'wp_upload_dir' ) ) {
1279 $uploads = wp_upload_dir( null, false );
1280 if ( is_array( $uploads ) && empty( $uploads['error'] ) && ! empty( $uploads['basedir'] ) ) {
1281 return rtrim( (string) $uploads['basedir'], '/' ) . '/xspeed';
1282 }
1283 }
1284 return XSPEED_CACHE_DIR;
1285 }
1286
1287 /** Absolute path to the nginx hit log file. */
1288 public static function hits_log_path(): string {
1289 return self::hits_log_dir() . '/hits.log';
1290 }
1291
1292 /**
1293 * Sync the drop-in's mobile-bucket flag file with the `mobile_separate`
1294 * setting. The drop-in (advanced-cache.php) runs before WordPress loads,
1295 * so it can't read the option — instead it checks for a zero-byte
1296 * `.mobile-separate` marker next to the cache files. When the setting is
1297 * on we touch the marker; when off we remove it. The drop-in's cache_key
1298 * computation keys off the marker's presence so its '|m'/'|d' device
1299 * bucket stays in lockstep with Cache::cache_key().
1300 *
1301 * Without this, turning on mobile_separate made Cache::store() write keys
1302 * with a '|d'/'|m' suffix the drop-in never reproduced — so the drop-in's
1303 * file_exists() always missed, every HIT fell through to a full WP boot,
1304 * and the fast pre-WP path was silently dead.
1305 *
1306 * @param bool|null $enabled Force a state; null reads the current setting.
1307 */
1308 public static function sync_mobile_flag( $enabled = null ): void {
1309 if ( null === $enabled ) {
1310 $opts = Settings_Manager::get( 'cache' );
1311 $enabled = ! empty( $opts['mobile_separate'] );
1312 }
1313 $dir = XSPEED_CACHE_DIR;
1314 $flag = $dir . '/.mobile-separate';
1315 if ( $enabled ) {
1316 if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) {
1317 return;
1318 }
1319 if ( ! file_exists( $flag ) ) {
1320 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch, WordPress.PHP.NoSilencedErrors.Discouraged -- read by the pre-WP drop-in via file_exists(); must be a plain marker, not WP_Filesystem.
1321 @touch( $flag );
1322 }
1323 return;
1324 }
1325 if ( file_exists( $flag ) ) {
1326 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_unlink, WordPress.PHP.NoSilencedErrors.Discouraged -- plain marker removal; non-fatal.
1327 @unlink( $flag );
1328 }
1329 }
1330
1331 /**
1332 * Write / remove the `.maintenance-active` sentinel next to the cache
1333 * files. The pre-WP drop-in checks for this marker and bails when present,
1334 * so a page cached while the site was live is NOT served during
1335 * maintenance / coming-soon mode — WordPress loads and renders the
1336 * maintenance screen instead. The Pro Maintenance-Cache module drives this
1337 * on the maintenance on/off transition. (FBS-82409 B1)
1338 *
1339 * @param bool $active True to arm the sentinel (entering maintenance),
1340 * false to clear it (site recovered).
1341 */
1342 public static function sync_maintenance_flag( bool $active ): void {
1343 $dir = XSPEED_CACHE_DIR;
1344 $flag = $dir . '/.maintenance-active';
1345 if ( $active ) {
1346 if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) {
1347 return;
1348 }
1349 if ( ! file_exists( $flag ) ) {
1350 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch, WordPress.PHP.NoSilencedErrors.Discouraged -- read by the pre-WP drop-in via file_exists(); must be a plain marker, not WP_Filesystem.
1351 @touch( $flag );
1352 }
1353 return;
1354 }
1355 if ( file_exists( $flag ) ) {
1356 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_unlink, WordPress.PHP.NoSilencedErrors.Discouraged -- plain marker removal; non-fatal.
1357 @unlink( $flag );
1358 }
1359 }
1360
1361 /**
1362 * Reconcile every mobile_separate-dependent artifact to the current
1363 * setting. Called on boot and whenever the cache settings are saved, so
1364 * flipping mobile_separate at runtime can't leave the install in a
1365 * half-converted state.
1366 *
1367 * Three things must agree with the setting:
1368 * 1. the drop-in's `.mobile-separate` flag (sync_mobile_flag()),
1369 * 2. the device-blind server rewrite — present only when OFF
1370 * (static_rewrite_allowed()),
1371 * 3. the now-stale static-cache tree + page cache, which were keyed
1372 * under the old scheme and would serve wrong-device HTML.
1373 *
1374 * No-ops when the cache is disabled — there's nothing installed to
1375 * reconcile, and toggle() handles install/teardown itself.
1376 */
1377 public static function reconcile_mobile_separate(): void {
1378 self::sync_mobile_flag();
1379
1380 // The rewrite/static reconciliation below needs the plugin's path
1381 // constants. They're absent in early-boot / unit-test contexts where
1382 // only the drop-in flag matters — bail to the flag-only behavior then.
1383 if ( ! defined( 'XSPEED_CACHE_STATIC_DIR' ) ) {
1384 return;
1385 }
1386
1387 // Only touch the rewrite + caches when caching is actually on.
1388 $opts = get_option( 'xspeed_options', array() );
1389 if ( empty( $opts['cache_enabled'] ) ) {
1390 return;
1391 }
1392
1393 $rewrite_present = self::rewrite_installed();
1394 $rewrite_wanted = self::static_rewrite_allowed();
1395
1396 if ( $rewrite_present === $rewrite_wanted ) {
1397 // Already consistent — nothing flipped, leave caches intact so a
1398 // plain settings save (e.g. expiry change) doesn't blow the cache.
1399 return;
1400 }
1401
1402 // The setting flipped. Bring the rewrite into line and purge the
1403 // now-misbucketed cache so the next request re-primes under the new
1404 // device scheme.
1405 if ( $rewrite_wanted ) {
1406 self::install_rewrite();
1407 } else {
1408 self::remove_rewrite();
1409 }
1410 self::purge_all( 'mobile_separate changed' );
1411 }
1412
1413 /**
1414 * Whether the server-level static-rewrite fast path may be used.
1415 *
1416 * The rewrite serves `{host}{path}/index.html` straight from the web
1417 * server, keyed only by host + path — it has no way to run our PHP
1418 * device detection, so it can't tell mobile from desktop. When
1419 * `mobile_separate` is on, a single static file would be shared across
1420 * devices and whoever primed it wins (mobile visitors could get desktop
1421 * HTML, or vice-versa). Rather than duplicate a wp_is_mobile()-equivalent
1422 * UA matcher into .htaccess AND the nginx snippet (three copies that
1423 * would inevitably drift), we simply DON'T engage the static rewrite when
1424 * mobile_separate is on. Requests then fall through to the PHP drop-in,
1425 * which buckets correctly — a small TTFB cost (~85ms vs ~30ms) paid only
1426 * on mobile-separate sites, in exchange for guaranteed correctness.
1427 *
1428 * LiteSpeed exclusion (2026-06-16): on LiteSpeed — OpenLiteSpeed in
1429 * particular — `.htaccess` CAN run our RewriteRule to serve the static
1430 * file, but its `.htaccess` engine ignores `mod_headers`, so we cannot
1431 * stamp the served response with `X-XSpeed-Cache: HIT`, AND there is no
1432 * `.htaccess` equivalent of nginx's per-location `access_log` to record
1433 * the hit. The result was a cache that worked but was invisible: no HIT
1434 * header and a hit-ratio frozen near 0%. Every OTHER server gives the
1435 * user a visible HIT header + a counted hit (nginx via add_header +
1436 * access_log in its snippet; Apache via .htaccess mod_headers, which it
1437 * honors). To keep LiteSpeed CONSISTENT with the rest, we route its hits
1438 * through the PHP drop-in instead — the drop-in emits
1439 * `X-XSpeed-Cache: HIT (php)` and calls Hit_Counter inline, exactly the
1440 * observable behavior the other servers get. The cost is the drop-in's
1441 * ~30ms TTFB vs the static path's ~10ms, paid only on LiteSpeed; in
1442 * exchange the dashboard hit-ratio and the response header finally tell
1443 * the truth there. (Apache keeps the static fast path — it honors the
1444 * header.) See maybe_emit_lscache_headers() for the paired LSCache
1445 * stand-down that stops LiteSpeed's own module from shadowing the
1446 * drop-in.
1447 */
1448 public static function static_rewrite_allowed(): bool {
1449 // LiteSpeed: drop-in serves hits (visible + counted) — see docblock.
1450 if ( Server::LITESPEED === Server::type() ) {
1451 return false;
1452 }
1453 $opts = Settings_Manager::get( 'cache' );
1454 return empty( $opts['mobile_separate'] );
1455 }
1456
1457 /**
1458 * Why the device-blind static rewrite is NOT installed, when it isn't.
1459 * Returns 'mobile_separate' when Separate Mobile Cache is the blocker
1460 * (the static file is one-per-URL, so it can't coexist with per-device
1461 * buckets), '' otherwise. Lets the dashboard explain the slow path
1462 * instead of silently falling back to PHP serving. (FBS-83145)
1463 */
1464 public static function static_rewrite_block_reason(): string {
1465 if ( Server::LITESPEED === Server::type() ) {
1466 return ''; // Intended on LiteSpeed — not a "block".
1467 }
1468 $opts = Settings_Manager::get( 'cache' );
1469 return ! empty( $opts['mobile_separate'] ) ? 'mobile_separate' : '';
1470 }
1471
1472 public static function ensure_hits_log_file(): bool {
1473 $dir = self::hits_log_dir();
1474 if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) {
1475 return false;
1476 }
1477 // Ensure the dir is traversable + writable by a different-uid nginx.
1478 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod -- nginx (a separate uid in multi-container setups) must be able to create/append the log; WP_Filesystem layers ownership overrides that defeat that intent.
1479 @chmod( $dir, 0777 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- best-effort; the access_log just stays empty if it fails.
1480 $path = self::hits_log_path();
1481 if ( ! file_exists( $path ) ) {
1482 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch -- See docblock: must be a plain touch, not WP_Filesystem.
1483 @touch( $path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- non-fatal helper; failures already covered by the dir check.
1484 }
1485 // World-writable so a different-uid nginx can append HIT lines.
1486 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod -- See docblock.
1487 @chmod( $path, 0666 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- best-effort.
1488 return file_exists( $path );
1489 }
1490
1491 public static function nginx_snippet(): ?string {
1492 if ( Server::NGINX !== Server::type() ) {
1493 return null;
1494 }
1495 $rel = '/' . ltrim( str_replace( ABSPATH, '/', XSPEED_CACHE_STATIC_DIR ), '/' );
1496 $rel = rtrim( $rel, '/' );
1497
1498 // WP-Rocket-canonical pattern: every condition lives at
1499 // SERVER level (outside any location block). Each one appends
1500 // a tag to $xspeed_no_cache; the final check is a single
1501 // string-equality against the unmodified default "no-cache".
1502 // Only when ALL conditions pass does the rewrite fire,
1503 // jumping the request to the static file's URL. nginx then
1504 // restarts location matching against the new path, where
1505 // regular static-file serving takes over.
1506 //
1507 // Why server-level + a single rewrite (instead of try_files
1508 // inside `location /`): nginx's well-documented "if is evil"
1509 // quirk silently disables `try_files`'s last fallback when
1510 // any `if` in the same location is true. Moving the `if`s
1511 // outside any location dodges the trap completely, because
1512 // server-level rewrite is the documented stable path.
1513 //
1514 // `last` (not `break`) restarts location matching — required
1515 // so the rewritten static-file URI gets served via the normal
1516 // static-file location, not re-matched against `location /`
1517 // where our own rewrite would loop.
1518 //
1519 // The cache existence check is the LAST condition in the
1520 // chain so when the file isn't cached, $xspeed_no_cache
1521 // gets a "-nofile" tag and the rewrite is skipped — the
1522 // request falls through to whatever `location /` the user
1523 // already had (typically `try_files $uri $uri/ /index.php?$args;`).
1524 // Absolute path to the hit-log file from the nginx process's
1525 // filesystem view. Nginx's `access_log buffer=N flush=Ns` form
1526 // requires a literal path — `$document_root` variables are
1527 // rejected — so PHP computes it. Lives under uploads/ (NOT the
1528 // cache dir): a cache purge or uninstall deletes the cache dir,
1529 // which would orphan this directive's parent directory and make
1530 // `nginx -t` fail [emerg] for EVERY vhost on the host
1531 // (FBS-82478). uploads/ survives both, so the directive can
1532 // never take nginx down. Works on every topology where the nginx
1533 // process shares a filesystem with PHP (container or host).
1534 $hits_abs = self::hits_log_path();
1535
1536 $lines = array();
1537 $lines[] = '# xSpeed static cache — paste at server level, above location / { }.';
1538 // Cache host must match the on-disk dir PHP writes: store_static() /
1539 // static_host() take HTTP_HOST and strip every char outside
1540 // [a-zA-Z0-9.\-] — i.e. it removes the colon but KEEPS the port digits
1541 // (localhost:8192 → localhost8192). nginx's own $host can't reproduce
1542 // that: $host has the port already stripped ENTIRELY (→ localhost), so
1543 // the -f check looks for localhost/... while PHP wrote localhost8192/...
1544 // and the rewrite never fires on a non-standard port. Derive
1545 // $xspeed_host from $http_host (which keeps the port) and drop just the
1546 // colon, so it equals the PHP dir on every port. On standard ports
1547 // $http_host has no colon, so $xspeed_host == $host == the bare domain.
1548 $lines[] = 'set $xspeed_host $http_host;'; // default: no port → unchanged (e.g. example.com)
1549 $lines[] = 'if ($http_host ~ "^([^:]+):(\\d+)$") { set $xspeed_host $1$2; }'; // host:port → hostport (matches PHP static_host())
1550 $lines[] = 'set $xspeed_no_cache "no-cache";';
1551 $lines[] = 'if ($request_method != GET) { set $xspeed_no_cache "$xspeed_no_cache-method"; }';
1552 $lines[] = 'if ($args) { set $xspeed_no_cache "$xspeed_no_cache-args"; }';
1553 $lines[] = 'if ($http_cookie ~* "(wordpress_logged_in|comment_author|wp-postpass_)") { set $xspeed_no_cache "$xspeed_no_cache-cookie"; }';
1554 $lines[] = 'if (!-f "$document_root' . $rel . '/$xspeed_host$uri/index.html") { set $xspeed_no_cache "$xspeed_no_cache-nofile"; }';
1555 // Neither `add_header` nor `access_log` is allowed inside an `if{}`
1556 // at server level (nginx rejects with "directive is not allowed
1557 // here"). The logging therefore lives in a `location` block that
1558 // matches the rewritten URI after `rewrite … last;` restarts
1559 // location matching. Every HIT lands there exactly once, every
1560 // MISS / PHP-served request never matches it.
1561 $lines[] = 'if ($xspeed_no_cache = "no-cache") {';
1562 $lines[] = ' rewrite ^ ' . $rel . '/$xspeed_host$uri/index.html last;';
1563 $lines[] = '}';
1564 $lines[] = '';
1565 $lines[] = '# Serve + log the cached HIT — `^~` is required so this beats any regex location.';
1566 $lines[] = 'location ^~ ' . $rel . '/ {';
1567 $lines[] = ' internal;';
1568 // LITERAL log path (not `set $var; access_log $var`). The variable form
1569 // makes nginx open the log lazily per-request and SILENTLY drop the
1570 // line if the open fails — so on a working host hits were served
1571 // (X-XSpeed-Cache fires regardless) but nothing was ever written and
1572 // the hit ratio sat at 0%. A literal path makes nginx open the file at
1573 // config load and actually log every hit.
1574 //
1575 // Deleting the log FILE is still safe with a literal path: nginx
1576 // recreates it on the next write/reload and `nginx -t` stays green
1577 // (verified). The only thing that [emerg]s `nginx -t` is a missing
1578 // parent DIRECTORY — and the log lives under uploads/xspeed/, which
1579 // survives cache purge + uninstall, and which ensure_hits_log_file()
1580 // (run on every admin_init via auto_heal) recreates if it ever goes
1581 // missing. So: hits are logged, and a user deleting the log can't take
1582 // nginx down.
1583 $lines[] = ' access_log ' . $hits_abs . ' combined buffer=16k flush=5s;';
1584 $lines[] = ' add_header X-XSpeed-Cache "HIT (nginx)" always;';
1585 $lines[] = '}';
1586 return implode( "\n", $lines );
1587 }
1588
1589 /**
1590 * Aggregate every enabled module's nginx_directives() into one
1591 * pasteable server-block snippet. Replaces the per-module "paste
1592 * this snippet" notices with a single consolidated paste — every
1593 * future feature toggle just regenerates this output.
1594 *
1595 * Returns null on non-nginx hosts (nothing to paste).
1596 *
1597 * Sections render in module-registration order so the layout stays
1598 * predictable; each module gets a comment header `# <slug>`.
1599 */
1600 public static function full_nginx_server_block(): ?string {
1601 if ( Server::NGINX !== Server::type() ) {
1602 return null;
1603 }
1604
1605 $blocks = array();
1606 foreach ( Module_Registry::all() as $module ) {
1607 $directives = $module->nginx_directives();
1608 if ( ! is_string( $directives ) || '' === trim( $directives ) ) {
1609 continue;
1610 }
1611 $blocks[] = "# === " . $module->slug() . " ===\n" . rtrim( $directives );
1612 }
1613
1614 if ( empty( $blocks ) ) {
1615 return null;
1616 }
1617
1618 $header = "# xSpeed unified nginx config — paste into `server { }`, above `location / { }`; re-paste after toggling features.\n";
1619
1620 return $header . "\n" . implode( "\n\n", $blocks ) . "\n";
1621 }
1622
1623 /**
1624 * Tell LiteSpeed's LSCache module to stand down on the cache-miss
1625 * render path.
1626 *
1627 * History: this method used to emit X-LiteSpeed-Cache-Control:
1628 * public,max-age=N + X-LiteSpeed-Tag, handing caching to the server's
1629 * LSCache store. That delegation backfired — once LSCache cached a
1630 * page it served every subsequent request from its OWN store and
1631 * intercepted the request before our site-root .htaccess static
1632 * rewrite could run. Net effect on LiteSpeed hosts: no X-XSpeed-Cache
1633 * header, our static-cache tree never served, the HIT log never
1634 * written (hit ratio frozen at 0%), and the Health probe reporting a
1635 * false "cache running on PHP fallback" because it never saw an
1636 * xSpeed-served response.
1637 *
1638 * xSpeed now owns the cache on LiteSpeed exactly as it does on Apache:
1639 * our `.htaccess` mod_rewrite block serves hits straight from the
1640 * static-cache tree (with the X-XSpeed-Cache header + access-log HIT
1641 * accounting), and PHP/the drop-in is the fallback. To guarantee
1642 * LSCache doesn't shadow that with its own copy — some LiteSpeed
1643 * configs cache by default — we send an explicit `no-cache` control so
1644 * the server defers to our rewrite. Skipped when the LiteSpeed Cache
1645 * plugin is active (it owns its own header policy; our Conflict
1646 * registry handles that coexistence separately).
1647 */
1648 public static function maybe_emit_lscache_headers(): void {
1649 if ( headers_sent() ) {
1650 return;
1651 }
1652 if ( Server::LITESPEED !== Server::type() ) {
1653 return;
1654 }
1655 // is_plugin_active() lives in wp-admin/includes/plugin.php which
1656 // isn't auto-loaded on front-end requests. Use the option layer
1657 // directly to avoid pulling in admin code from a render path.
1658 $active = (array) get_option( 'active_plugins', array() );
1659 if ( in_array( 'litespeed-cache/litespeed-cache.php', $active, true ) ) {
1660 return;
1661 }
1662
1663 // Explicitly opt this response OUT of LSCache so the server can't
1664 // shadow our static-rewrite cache with its own internal copy.
1665 header( 'X-LiteSpeed-Cache-Control: no-cache' );
1666 }
1667
1668 /**
1669 * Reconcile drop-in + WP_CACHE + rewrite block with the user's
1670 * saved choice. Runs on admin_init. Cheap when nothing's wrong
1671 * (one option read + a handful of file_exists / defined checks);
1672 * writes only when state has drifted (typical cause: plugin
1673 * upgrade wiped the drop-in, foreign plugin removed our WP_CACHE
1674 * define, or someone hand-edited .htaccess).
1675 *
1676 * Skipped during the WP plugin updater run so we don't race
1677 * the upgrader's own filesystem operations.
1678 */
1679 public static function auto_heal(): void {
1680 if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) {
1681 return;
1682 }
1683 if ( wp_doing_ajax() || wp_doing_cron() ) {
1684 return;
1685 }
1686
1687 $opts = get_option( 'xspeed_options', array() );
1688 if ( empty( $opts['cache_enabled'] ) ) {
1689 return;
1690 }
1691
1692 $dropin_target = WP_CONTENT_DIR . '/advanced-cache.php';
1693 $dropin_ours = false;
1694 $dropin_stale = false;
1695 if ( file_exists( $dropin_target ) ) {
1696 $contents = @file_get_contents( $dropin_target ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1697 $dropin_ours = is_string( $contents ) && false !== strpos( $contents, 'XSPEED_DROPIN' );
1698 // Reinstall when OUR drop-in is an older version than the source —
1699 // the marker alone can't distinguish an old copy from a new one, so
1700 // a serve-logic change (e.g. the .meta read for 404s/feeds) would
1701 // otherwise never reach existing cache-enabled sites until a manual
1702 // cache toggle. (FBS-82406/82407)
1703 if ( $dropin_ours ) {
1704 $dropin_stale = self::dropin_version( (string) $contents ) < self::dropin_version( @file_get_contents( XSPEED_DIR . 'includes/advanced-cache.php' ) ?: '' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1705 }
1706 }
1707
1708 if ( ! $dropin_ours || $dropin_stale ) {
1709 self::install_dropin();
1710 }
1711
1712 if ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) {
1713 self::set_wp_cache_constant( true );
1714 }
1715
1716 // Rewrite block goes last. It's what turns the static-cache
1717 // tree into a PHP-bypass — every cache hit served by the web
1718 // server directly. Without it we still cache, just at drop-in
1719 // speed (~85ms TTFB) instead of static-file speed (~25-40ms).
1720 //
1721 // Reconcile against mobile_separate: the rewrite is device-blind, so
1722 // it must be ABSENT when mobile_separate is on and PRESENT otherwise.
1723 // auto_heal() runs periodically, so it also repairs a rewrite that
1724 // was left installed before mobile_separate was switched on.
1725 if ( self::static_rewrite_allowed() ) {
1726 if ( ! self::rewrite_installed() ) {
1727 self::install_rewrite();
1728 }
1729 } elseif ( self::rewrite_installed() ) {
1730 self::remove_rewrite();
1731 }
1732
1733 // HITs log file — nginx writes one line per HIT served directly
1734 // (see nginx_snippet()), Cache::get_stats() drains the file via
1735 // Hit_Counter::collect_nginx_log_hits(). If the file vanishes
1736 // (plugin upgrade wiped wp-content/cache/), nginx errors silently
1737 // on the access_log directive and the counter stays at 0.
1738 self::ensure_hits_log_file();
1739 }
1740
1741 /**
1742 * Build the .htaccess rules that map cacheable requests to the
1743 * static-cache tree. Conditions are deliberately strict: GET only,
1744 * empty query string, no session/comment-author/post-password
1745 * cookie, and the static file must exist on disk. Anything that
1746 * fails one of these falls through to PHP and the drop-in / full
1747 * WordPress path.
1748 *
1749 * @return string[] Lines for insert_with_markers().
1750 */
1751 public static function rewrite_block_lines(): array {
1752 // Path relative to ABSPATH so the rule lives in the site-root
1753 // .htaccess regardless of where wp-content sits. WP_CONTENT_DIR
1754 // can be moved, so we compute the document-root-relative form
1755 // at install time and bake it into the rule.
1756 $rel = str_replace( ABSPATH, '/', XSPEED_CACHE_STATIC_DIR );
1757 $rel = '/' . ltrim( $rel, '/' );
1758 $rel = rtrim( $rel, '/' );
1759
1760 return array(
1761 '<IfModule mod_rewrite.c>',
1762 ' RewriteEngine On',
1763 ' RewriteCond %{REQUEST_METHOD} ^GET$',
1764 ' RewriteCond %{QUERY_STRING} ^$',
1765 ' RewriteCond %{HTTP_COOKIE} !(wordpress_logged_in|comment_author|wp-postpass_) [NC]',
1766 // Capture REQUEST_URI without its trailing slash into %1.
1767 // store_static() writes `{host}{uri-without-trailing-slash}/index.html`,
1768 // so this normalization lets `/blog/` and `/blog` both hit
1769 // the same cache file without producing the double-slash
1770 // path that would skip the -f check below.
1771 ' RewriteCond %{REQUEST_URI} ^(.*?)/?$',
1772 ' RewriteCond %{DOCUMENT_ROOT}' . $rel . '/%{HTTP_HOST}%1/index.html -f',
1773 // Pattern is `^`, NOT `.`. The per-directory rewrite engine
1774 // strips the leading slash before matching, so the HOMEPAGE
1775 // request `/` arrives here as an EMPTY path. `.` requires at
1776 // least one character and therefore never matches the homepage
1777 // — on LiteSpeed (which honors this strictly) the front page
1778 // fell through to PHP while every inner page rewrote fine.
1779 // `^` matches the empty string AND any non-empty path, so it
1780 // covers `/` and `/blog` alike. (Confirmed on OpenLiteSpeed
1781 // 1.8: `.` → homepage served by PHP drop-in; `^` → served
1782 // directly from the static file.)
1783 ' RewriteRule ^ ' . $rel . '/%{HTTP_HOST}%1/index.html [L]',
1784 '</IfModule>',
1785 );
1786 }
1787
1788 /**
1789 * Active probe that confirms the web-server static-rewrite path is
1790 * actually serving cached files. Writes a probe file with a random
1791 * nonce, fetches it over HTTP at its public URL, and checks whether
1792 * the response was served directly by the web server (Last-Modified
1793 * + ETag headers + no X-Powered-By: PHP).
1794 *
1795 * Server-agnostic: same probe works for nginx (snippet pasted) and
1796 * Apache / LiteSpeed (.htaccess block installed). If the rewrite
1797 * isn't engaged, the request falls through to WordPress and PHP
1798 * adds its own headers, which the probe detects and reports.
1799 *
1800 * Throttled via a 5-minute transient — we never want this running
1801 * on every Health card paint.
1802 *
1803 * @return array{active:bool, reason:string, code?:int, php?:bool, expires?:int}
1804 */
1805 /**
1806 * @param bool $allow_probe When false (the default), return ONLY a cached
1807 * result and never make an HTTP request — so admin page loads are never
1808 * blocked by the loopback probe. The actual HTTP probe only runs when a
1809 * caller explicitly opts in (the Health tab / cron). Previously this ran
1810 * synchronously on every dashboard bootstrap, so a slow/timing-out
1811 * loopback request added up to `timeout` seconds to admin page loads on
1812 * hosts that block self-requests. (FBS-82142)
1813 */
1814 public static function probe_static_rewrite( bool $allow_probe = false ): array {
1815 $cached = get_transient( 'xspeed_rewrite_probe' );
1816 if ( is_array( $cached ) ) {
1817 return $cached;
1818 }
1819 // No cached result yet and the caller doesn't want to pay for a live
1820 // HTTP probe (e.g. the admin bootstrap): report "pending" without
1821 // blocking. The Health tab will run the real probe on demand.
1822 if ( ! $allow_probe ) {
1823 return array( 'active' => false, 'reason' => 'probe pending', 'pending' => true );
1824 }
1825
1826 $home = home_url( '/' );
1827 $host = (string) wp_parse_url( $home, PHP_URL_HOST );
1828 if ( '' === $host ) {
1829 $result = array( 'active' => false, 'reason' => 'home_url has no host' );
1830 set_transient( 'xspeed_rewrite_probe', $result, MINUTE_IN_SECONDS );
1831 return $result;
1832 }
1833
1834 // Use a randomised path AND nonce so a stale CDN cache entry
1835 // from a prior probe can never make a broken install look
1836 // healthy. Path is namespaced under __xspeed_probe__ so the
1837 // directory listing stays obvious if cleanup misfires.
1838 $slug = wp_generate_password( 12, false, false );
1839 $nonce = wp_generate_password( 24, false, false );
1840 $probe_dir = XSPEED_CACHE_STATIC_DIR . '/' . $host . '/__xspeed_probe__/' . $slug;
1841 $probe_file = $probe_dir . '/index.html';
1842 $probe_url = trailingslashit( $home ) . '__xspeed_probe__/' . $slug . '/';
1843
1844 if ( ! file_exists( $probe_dir ) ) {
1845 wp_mkdir_p( $probe_dir );
1846 }
1847 if ( ! is_dir( $probe_dir ) ) {
1848 $result = array( 'active' => false, 'reason' => 'cannot create probe dir' );
1849 set_transient( 'xspeed_rewrite_probe', $result, MINUTE_IN_SECONDS );
1850 return $result;
1851 }
1852 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_put_contents_file_put_contents -- WP_Filesystem requires admin credentials we may not have here; the file is in our own cache dir.
1853 file_put_contents( $probe_file, $nonce, LOCK_EX );
1854
1855 // Verify TLS by default — disabling it site-wide is a needless MITM
1856 // exposure (FBS-82142). Only relax verification in local/dev
1857 // environments, where self-signed certs are common and there's no
1858 // real attacker in the loop.
1859 $is_local = function_exists( 'wp_get_environment_type' )
1860 && in_array( wp_get_environment_type(), array( 'local', 'development' ), true );
1861 $resp = wp_remote_get(
1862 $probe_url,
1863 array(
1864 // 3s cap so a host that hangs on loopback self-requests can't
1865 // stall the caller for long; the result/error is cached so we
1866 // don't repeat the wait every minute.
1867 'timeout' => 3,
1868 'sslverify' => ! $is_local,
1869 'redirection' => 0,
1870 'headers' => array( 'Cache-Control' => 'no-cache' ),
1871 )
1872 );
1873
1874 // Best-effort cleanup so we don't accumulate probe dirs even
1875 // if subsequent calls all hit the transient.
1876 if ( file_exists( $probe_file ) ) {
1877 wp_delete_file( $probe_file );
1878 }
1879 if ( is_dir( $probe_dir ) ) {
1880 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- Best-effort probe-dir cleanup; WP_Filesystem needs admin credentials we don't have here.
1881 @rmdir( $probe_dir );
1882 }
1883
1884 if ( is_wp_error( $resp ) ) {
1885 $result = array(
1886 'active' => false,
1887 'reason' => 'http error: ' . $resp->get_error_message(),
1888 );
1889 // Cache the failure for the full 5 minutes (not 1) so a host that
1890 // times out on the loopback probe isn't re-probed — and re-stalled
1891 // — on every page load within the window. (FBS-82142)
1892 set_transient( 'xspeed_rewrite_probe', $result, 5 * MINUTE_IN_SECONDS );
1893 return $result;
1894 }
1895
1896 $code = (int) wp_remote_retrieve_response_code( $resp );
1897 $body = (string) wp_remote_retrieve_body( $resp );
1898 $ua_php = '' !== (string) wp_remote_retrieve_header( $resp, 'x-powered-by' );
1899 $has_etag = '' !== (string) wp_remote_retrieve_header( $resp, 'etag' )
1900 || '' !== (string) wp_remote_retrieve_header( $resp, 'last-modified' );
1901 $match = trim( $body ) === $nonce;
1902
1903 // "Active" = the web server served our raw nonce bytes back
1904 // AND emitted the static-serve markers (ETag / Last-Modified)
1905 // AND didn't add an X-Powered-By: PHP header. All three are
1906 // individually noisy; together they're conclusive.
1907 $active = $match && $has_etag && ! $ua_php && 200 === $code;
1908
1909 if ( $active ) {
1910 $reason = 'static-served';
1911 } elseif ( 200 === $code && $match && $ua_php ) {
1912 $reason = 'php served the file instead of nginx/Apache (rewrite block missing)';
1913 } elseif ( 200 === $code && ! $match ) {
1914 $reason = 'unexpected body (CDN cached an older response?)';
1915 } elseif ( 404 === $code ) {
1916 $reason = 'probe URL returned 404 (rewrite block missing or wrong path)';
1917 } else {
1918 $reason = sprintf( 'unexpected response (HTTP %d, body %d B, php=%s)', $code, strlen( $body ), $ua_php ? 'yes' : 'no' );
1919 }
1920
1921 $result = array(
1922 'active' => $active,
1923 'reason' => $reason,
1924 'code' => $code,
1925 'php' => $ua_php,
1926 );
1927 set_transient( 'xspeed_rewrite_probe', $result, 5 * MINUTE_IN_SECONDS );
1928 return $result;
1929 }
1930
1931 public static function rewrite_installed(): bool {
1932 $htaccess = ABSPATH . '.htaccess';
1933 if ( ! file_exists( $htaccess ) ) {
1934 return false;
1935 }
1936 $existing = @file_get_contents( $htaccess ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1937 if ( ! is_string( $existing ) ) {
1938 return false;
1939 }
1940 return false !== strpos( $existing, '# BEGIN xSpeed Static Cache' );
1941 }
1942
1943 /**
1944 * Install the static-cache rewrite block at the TOP of .htaccess.
1945 *
1946 * Position matters: WordPress's own block ends with
1947 * `RewriteRule . /index.php [L]` which routes every non-file
1948 * request to PHP. The [L] flag stops the current rewrite pass,
1949 * but Apache restarts the cycle; on the second pass REQUEST_URI
1950 * is /index.php and no static-file check can match. The only
1951 * reliable position for a "serve static if it exists" rule is
1952 * before WordPress's block.
1953 *
1954 * WP's insert_with_markers() always appends, so we manage the
1955 * block manually: strip any prior xSpeed Static Cache markers,
1956 * then write our block followed by the rest of the file.
1957 */
1958 public static function install_rewrite(): bool {
1959 // The static rewrite is device-blind; never install it when
1960 // mobile_separate is on (see static_rewrite_allowed()).
1961 if ( ! self::static_rewrite_allowed() ) {
1962 return false;
1963 }
1964 $htaccess = ABSPATH . '.htaccess';
1965 $existing = file_exists( $htaccess ) ? @file_get_contents( $htaccess ) : ''; // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1966 if ( false === $existing ) {
1967 $existing = '';
1968 }
1969 // Apache/LiteSpeed only. nginx hosts: rule won't fire, drop-in
1970 // covers; we skip the write so we don't litter their root.
1971 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- Pre-flight check before file_put_contents; WP_Filesystem requires admin credentials we don't have inside a manage_options REST request.
1972 if ( file_exists( $htaccess ) && ! is_writable( $htaccess ) ) {
1973 return false;
1974 }
1975 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- See above.
1976 if ( ! file_exists( $htaccess ) && ! is_writable( ABSPATH ) ) {
1977 return false;
1978 }
1979
1980 $cleaned = self::strip_marker_block( $existing, 'xSpeed Static Cache' );
1981 $block = self::marker_block( 'xSpeed Static Cache', self::rewrite_block_lines() );
1982 $next = $block . ( '' === $cleaned ? '' : "\n" . $cleaned );
1983
1984 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_put_contents_file_put_contents, PluginCheck.CodeAnalysis.WriteFile.ABSPATHDetected -- WP_Filesystem requires admin credentials we don't have here; toggle() runs in a REST request authorized by manage_options nonce. The target is the site's .htaccess (configuration file managed by WP core itself), not user data — wp_upload_dir() doesn't apply.
1985 return false !== file_put_contents( $htaccess, $next, LOCK_EX );
1986 }
1987
1988 public static function remove_rewrite(): bool {
1989 $htaccess = ABSPATH . '.htaccess';
1990 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- See install_rewrite() rationale.
1991 if ( ! file_exists( $htaccess ) || ! is_writable( $htaccess ) ) {
1992 return false;
1993 }
1994 $existing = @file_get_contents( $htaccess ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1995 if ( false === $existing ) {
1996 return false;
1997 }
1998 $cleaned = self::strip_marker_block( $existing, 'xSpeed Static Cache' );
1999 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_put_contents_file_put_contents, PluginCheck.CodeAnalysis.WriteFile.ABSPATHDetected -- See install_rewrite() rationale.
2000 return false !== file_put_contents( $htaccess, $cleaned, LOCK_EX );
2001 }
2002
2003 /**
2004 * Strip a `# BEGIN <marker>` ... `# END <marker>` block from a
2005 * .htaccess-style file, including any blank line that immediately
2006 * follows it. Idempotent — returns the input unchanged if the
2007 * marker isn't present.
2008 */
2009 private static function strip_marker_block( string $contents, string $marker ): string {
2010 $pattern = '/# BEGIN ' . preg_quote( $marker, '/' ) . '\b.*?# END ' . preg_quote( $marker, '/' ) . "\b[^\n]*\n?\n?/s";
2011 $out = preg_replace( $pattern, '', $contents );
2012 return is_string( $out ) ? $out : $contents;
2013 }
2014
2015 private static function marker_block( string $marker, array $lines ): string {
2016 $header = "# BEGIN $marker\n";
2017 $header .= "# The directives (lines) between \"BEGIN $marker\" and \"END $marker\" are\n";
2018 $header .= "# dynamically generated, and should only be modified via WordPress filters.\n";
2019 $header .= "# Any changes to the directives between these markers will be overwritten.\n";
2020 $footer = "# END $marker\n";
2021 return $header . implode( "\n", $lines ) . "\n" . $footer;
2022 }
2023
2024 /**
2025 * Parse the `XSPEED_DROPIN_VERSION: N` stamp out of a drop-in's source.
2026 * Returns 0 when absent (an un-stamped older copy reinstalls). Used to
2027 * detect a stale installed drop-in vs the bundled source.
2028 */
2029 private static function dropin_version( string $contents ): int {
2030 if ( preg_match( '/XSPEED_DROPIN_VERSION:\s*(\d+)/', $contents, $m ) ) {
2031 return (int) $m[1];
2032 }
2033 return 0;
2034 }
2035
2036 public static function install_dropin() {
2037 $source = XSPEED_DIR . 'includes/advanced-cache.php';
2038 $target = WP_CONTENT_DIR . '/advanced-cache.php';
2039 if ( ! file_exists( $source ) ) {
2040 return false;
2041 }
2042
2043 global $wp_filesystem;
2044 if ( ! function_exists( 'WP_Filesystem' ) ) {
2045 require_once ABSPATH . 'wp-admin/includes/file.php';
2046 }
2047 WP_Filesystem();
2048 if ( ! $wp_filesystem ) {
2049 return false;
2050 }
2051
2052 $source_contents = $wp_filesystem->get_contents( $source );
2053 if ( ! is_string( $source_contents ) ) {
2054 return false;
2055 }
2056
2057 // Bake the absolute hit-log path into the drop-in. It runs before
2058 // WordPress loads, so it can't resolve wp_upload_dir() itself — we
2059 // substitute the @@XSPEED_HITS_LOG@@ token with the real uploads path
2060 // (never the cache dir; see hits_log_dir() / FBS-82478). Use a single
2061 // quoted PHP string literal so the installed file stays valid PHP.
2062 $source_contents = str_replace(
2063 '@@XSPEED_HITS_LOG@@',
2064 str_replace( "'", "\\'", self::hits_log_path() ),
2065 $source_contents
2066 );
2067
2068 if ( file_exists( $target ) ) {
2069 $existing = $wp_filesystem->get_contents( $target );
2070 $is_xspeed = is_string( $existing ) && false !== strpos( $existing, 'XSPEED_DROPIN' );
2071
2072 if ( $is_xspeed ) {
2073 if ( $existing === $source_contents ) {
2074 return true;
2075 }
2076 return (bool) $wp_filesystem->put_contents( $target, $source_contents, FS_CHMOD_FILE );
2077 }
2078
2079 // Foreign drop-in (e.g. left over from another cache plugin) — back it up
2080 // before overwriting so the user can recover if needed. Uploads dir
2081 // (not wp-content root) keeps the backup out of WordPress's reserved
2082 // drop-in location.
2083 $upload = wp_upload_dir( null, false );
2084 $basedir = isset( $upload['basedir'] ) ? trailingslashit( $upload['basedir'] ) . 'xspeed-backups' : false;
2085 if ( $basedir ) {
2086 if ( ! file_exists( $basedir ) ) {
2087 wp_mkdir_p( $basedir );
2088 self::write_silence( $basedir );
2089 }
2090 $backup = $basedir . '/advanced-cache.foreign-' . gmdate( 'Ymd-His' ) . '.php.bak';
2091 $wp_filesystem->move( $target, $backup, true );
2092 } else {
2093 $wp_filesystem->delete( $target );
2094 }
2095 }
2096
2097 return (bool) $wp_filesystem->put_contents( $target, $source_contents, FS_CHMOD_FILE );
2098 }
2099
2100 public static function remove_dropin() {
2101 $target = WP_CONTENT_DIR . '/advanced-cache.php';
2102 if ( ! file_exists( $target ) ) {
2103 return;
2104 }
2105
2106 global $wp_filesystem;
2107 if ( ! function_exists( 'WP_Filesystem' ) ) {
2108 require_once ABSPATH . 'wp-admin/includes/file.php';
2109 }
2110 WP_Filesystem();
2111 if ( ! $wp_filesystem ) {
2112 return;
2113 }
2114
2115 $contents = $wp_filesystem->get_contents( $target );
2116 if ( is_string( $contents ) && false !== strpos( $contents, 'XSPEED_DROPIN' ) ) {
2117 wp_delete_file( $target );
2118 }
2119 }
2120
2121 public static function set_wp_cache_constant( $enable ) {
2122 $wp_config = ABSPATH . 'wp-config.php';
2123 if ( ! file_exists( $wp_config ) ) {
2124 return false;
2125 }
2126
2127 global $wp_filesystem;
2128 if ( ! function_exists( 'WP_Filesystem' ) ) {
2129 require_once ABSPATH . 'wp-admin/includes/file.php';
2130 }
2131 WP_Filesystem();
2132 if ( ! $wp_filesystem || ! $wp_filesystem->is_writable( $wp_config ) ) {
2133 return false;
2134 }
2135
2136 $config = $wp_filesystem->get_contents( $wp_config );
2137
2138 if ( $enable ) {
2139 // Own the constant. A previous caching plugin (e.g. WP Rocket sets
2140 // it false on deactivate) can leave `define( 'WP_CACHE', false );`
2141 // behind — presence alone is not enough, the VALUE must be true or
2142 // WordPress never loads advanced-cache.php and our drop-in is dead.
2143 if ( preg_match( "/define\\(\\s*['\"]WP_CACHE['\"]\\s*,/", $config ) ) {
2144 $rewritten = preg_replace(
2145 "/define\\(\\s*['\"]WP_CACHE['\"]\\s*,\\s*[^)]*\\)\\s*;/",
2146 "define( 'WP_CACHE', true );",
2147 $config,
2148 1
2149 );
2150 // If an existing define was already `true`, the rewrite is a
2151 // no-op string-wise; either way we end on WP_CACHE === true.
2152 if ( null !== $rewritten ) {
2153 $config = $rewritten;
2154 }
2155 } else {
2156 $config = preg_replace( '/(<\?php)/', "$1\ndefine( 'WP_CACHE', true );", $config, 1 );
2157 }
2158 } else {
2159 $config = preg_replace( "/define\\(\\s*['\"]WP_CACHE['\"]\\s*,\\s*true\\s*\\);\\s*\\n?/", '', $config );
2160 }
2161
2162 return (bool) $wp_filesystem->put_contents( $wp_config, $config, FS_CHMOD_FILE );
2163 }
2164
2165 /**
2166 * Admin-bar purge menu — a parent node plus one child per visible cache
2167 * type (LiteSpeed-style), instead of a single "Purge All" link. Each
2168 * child posts to the same admin-post handler with its type slug. The
2169 * per-type items only appear for active/licensed modules; "Purge All"
2170 * always shows and always sweeps everything. (FBS-83114)
2171 */
2172 public function admin_bar_purge( $wp_admin_bar ) {
2173 if ( ! current_user_can( 'manage_options' ) ) {
2174 return;
2175 }
2176
2177 $wp_admin_bar->add_node(
2178 array(
2179 'id' => 'xspeed-purge',
2180 'title' => __( 'Purge xSpeed Cache', 'xspeed' ),
2181 'href' => self::purge_type_url( 'all' ),
2182 )
2183 );
2184
2185 foreach ( self::purge_types() as $slug => $type ) {
2186 if ( empty( $type['visible'] ) ) {
2187 continue;
2188 }
2189 $wp_admin_bar->add_node(
2190 array(
2191 'id' => 'xspeed-purge-' . $slug,
2192 'parent' => 'xspeed-purge',
2193 'title' => esc_html( $type['label'] ),
2194 'href' => self::purge_type_url( $slug ),
2195 )
2196 );
2197 }
2198 }
2199
2200 /**
2201 * Nonce-protected admin-post URL for purging a single type. The nonce
2202 * action is per-type so a leaked URL can't be replayed for a different
2203 * scope.
2204 */
2205 private static function purge_type_url( string $type ): string {
2206 return wp_nonce_url(
2207 admin_url( 'admin-post.php?action=xspeed_purge&type=' . rawurlencode( $type ) ),
2208 'xspeed_purge_' . $type
2209 );
2210 }
2211
2212 public function handle_admin_bar_purge() {
2213 if ( ! current_user_can( 'manage_options' ) ) {
2214 wp_die( esc_html__( 'Unauthorized.', 'xspeed' ), 403 );
2215 }
2216 $type = isset( $_GET['type'] ) ? sanitize_key( wp_unslash( $_GET['type'] ) ) : 'all';
2217 check_admin_referer( 'xspeed_purge_' . $type );
2218
2219 // Only honour known types; anything else falls back to a full purge.
2220 if ( ! array_key_exists( $type, self::purge_types() ) ) {
2221 $type = 'all';
2222 }
2223 self::purge_type( $type );
2224
2225 wp_safe_redirect( wp_get_referer() ?: admin_url() );
2226 exit;
2227 }
2228 }
2229