| 1 |
<?php |
| 2 |
/** |
| 3 |
* Health — shared diagnostic checks consumed by both Onboarding's |
| 4 |
* environment surface and the Health module's dashboard panel. |
| 5 |
* |
| 6 |
* Each check returns: |
| 7 |
* [ |
| 8 |
* 'id' => 'wp_version', |
| 9 |
* 'tone' => 'ok' | 'warn' | 'fail' | 'info', |
| 10 |
* 'label' => 'WordPress 6.6', |
| 11 |
* 'detail' => 'Meets the 6.0+ minimum.', |
| 12 |
* ] |
| 13 |
* |
| 14 |
* Pure reads — never writes to disk, never makes outbound HTTP calls. |
| 15 |
* Safe to call from any request including the loading dashboard. |
| 16 |
* |
| 17 |
* @package XSpeed |
| 18 |
*/ |
| 19 |
|
| 20 |
declare(strict_types=1); |
| 21 |
|
| 22 |
namespace XSpeed; |
| 23 |
|
| 24 |
defined( 'ABSPATH' ) || exit; |
| 25 |
|
| 26 |
final class Health { |
| 27 |
|
| 28 |
public const OK = 'ok'; |
| 29 |
public const WARN = 'warn'; |
| 30 |
public const FAIL = 'fail'; |
| 31 |
public const INFO = 'info'; |
| 32 |
|
| 33 |
private const SERVER_LABELS = array( |
| 34 |
'apache' => 'Apache', |
| 35 |
'litespeed' => 'LiteSpeed', |
| 36 |
'nginx' => 'nginx', |
| 37 |
'iis' => 'IIS', |
| 38 |
'unknown' => 'Unknown', |
| 39 |
); |
| 40 |
|
| 41 |
/** |
| 42 |
* Full check list used by the Health module's dashboard panel. |
| 43 |
* |
| 44 |
* @return array<int,array{id:string,tone:string,label:string,detail:string}> |
| 45 |
*/ |
| 46 |
public static function checks(): array { |
| 47 |
global $wp_version; |
| 48 |
|
| 49 |
$server_type = Server::type(); |
| 50 |
$gzip_mode = Server::gzip_mode(); |
| 51 |
$conflicts = Server::conflicts(); |
| 52 |
$cache_dir = defined( 'XSPEED_CACHE_DIR' ) ? XSPEED_CACHE_DIR : ( WP_CONTENT_DIR . '/cache/xspeed' ); |
| 53 |
|
| 54 |
$out = array(); |
| 55 |
|
| 56 |
// WordPress version |
| 57 |
$wp_ok = version_compare( (string) $wp_version, '6.0', '>=' ); |
| 58 |
$out[] = array( |
| 59 |
'id' => 'wp_version', |
| 60 |
'tone' => $wp_ok ? self::OK : self::FAIL, |
| 61 |
'label' => sprintf( 'WordPress %s', (string) $wp_version ), |
| 62 |
'detail' => $wp_ok ? 'Meets the 6.0+ minimum.' : 'Upgrade to WordPress 6.0 or higher.', |
| 63 |
); |
| 64 |
|
| 65 |
// PHP version |
| 66 |
$php_ok = version_compare( PHP_VERSION, '7.4', '>=' ); |
| 67 |
$php_modern = version_compare( PHP_VERSION, '8.1', '>=' ); |
| 68 |
$out[] = array( |
| 69 |
'id' => 'php_version', |
| 70 |
'tone' => $php_modern ? self::OK : ( $php_ok ? self::WARN : self::FAIL ), |
| 71 |
'label' => sprintf( 'PHP %s', PHP_VERSION ), |
| 72 |
'detail' => $php_modern |
| 73 |
? 'Modern PHP — full speed.' |
| 74 |
: ( $php_ok |
| 75 |
? 'Works, but 8.1+ is recommended for best performance.' |
| 76 |
: 'Upgrade to PHP 7.4 or higher.' ), |
| 77 |
); |
| 78 |
|
| 79 |
// Server |
| 80 |
$out[] = array( |
| 81 |
'id' => 'server', |
| 82 |
'tone' => self::INFO, |
| 83 |
'label' => sprintf( 'Server: %s', self::SERVER_LABELS[ $server_type ] ?? 'Unknown' ), |
| 84 |
'detail' => 'auto' === $gzip_mode |
| 85 |
? 'GZIP can be auto-configured via .htaccess.' |
| 86 |
: 'GZIP requires a manual server-config snippet (shown in the GZIP module).', |
| 87 |
); |
| 88 |
|
| 89 |
// Cache directory writable |
| 90 |
$dir_writable = wp_mkdir_p( $cache_dir ) && wp_is_writable( $cache_dir ); |
| 91 |
$out[] = array( |
| 92 |
'id' => 'cache_dir', |
| 93 |
'tone' => $dir_writable ? self::OK : self::FAIL, |
| 94 |
'label' => 'Cache directory writable', |
| 95 |
'detail' => $dir_writable |
| 96 |
? $cache_dir |
| 97 |
: sprintf( 'Cannot write to %s. Adjust file permissions before enabling cache.', $cache_dir ), |
| 98 |
); |
| 99 |
|
| 100 |
// Drop-in installed (only when cache is enabled — otherwise N/A) |
| 101 |
$cache_enabled = (bool) Settings::get()['cache_enabled']; |
| 102 |
if ( $cache_enabled ) { |
| 103 |
$dropin_path = WP_CONTENT_DIR . '/advanced-cache.php'; |
| 104 |
$dropin_match = file_exists( $dropin_path ) && false !== strpos( (string) file_get_contents( $dropin_path ), 'xspeed' ); |
| 105 |
$out[] = array( |
| 106 |
'id' => 'dropin', |
| 107 |
'tone' => $dropin_match ? self::OK : self::FAIL, |
| 108 |
'label' => 'advanced-cache.php drop-in', |
| 109 |
'detail' => $dropin_match |
| 110 |
? 'Installed and owned by xSpeed.' |
| 111 |
: 'Drop-in missing or owned by another plugin. Toggle Enable Cache off and on to reinstall.', |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
// WP_CACHE constant |
| 116 |
$wp_cache_const = defined( 'WP_CACHE' ) && WP_CACHE; |
| 117 |
if ( $cache_enabled ) { |
| 118 |
// Why the constant is missing decides what we tell the user to |
| 119 |
// do, so the two cases can't share one sentence. An unwritable |
| 120 |
// wp-config.php (managed hosts make it read-only by design) is |
| 121 |
// the common cause and the user must paste the line by hand. |
| 122 |
// But it is NOT the only way to land here: a leftover |
| 123 |
// `define( 'WP_CACHE', false );` from a previous cache plugin, |
| 124 |
// a missing wp-config.php, or a WP_Filesystem that wants FTP |
| 125 |
// credentials all fail set_wp_cache_constant() on a perfectly |
| 126 |
// writable file. Asserting "not writable" unconditionally told |
| 127 |
// those users something demonstrably false about their own |
| 128 |
// server and sent them hand-editing a file the plugin could |
| 129 |
// have fixed by toggling the cache off and on. (#19) |
| 130 |
// The cost is real, but it is NOT "nothing is being cached": with |
| 131 |
// the constant absent, Cache::maybe_start_cache() still serves on |
| 132 |
// template_redirect and tags the response `HIT (php)`. What the |
| 133 |
// constant buys is answering from the drop-in BEFORE WordPress |
| 134 |
// boots — worth roughly an order of magnitude on TTFB, which is |
| 135 |
// what makes this a WARN worth acting on. Claiming the cache was |
| 136 |
// idle was simply false, and it is the sentence a worried user |
| 137 |
// reads first. (#19, QA on #174) |
| 138 |
// |
| 139 |
// Ask the WRITER which branch to show, not a second oracle: Health |
| 140 |
// used to run its own wp_is_writable() against its own path |
| 141 |
// resolution, and the two disagreed with set_wp_cache_constant() |
| 142 |
// in both directions — see Cache::can_write_wp_config(). |
| 143 |
$wp_config_writable = Cache::can_write_wp_config(); |
| 144 |
$check = array( |
| 145 |
'id' => 'wp_cache_constant', |
| 146 |
'tone' => $wp_cache_const ? self::OK : self::WARN, |
| 147 |
'label' => 'WP_CACHE constant', |
| 148 |
'detail' => $wp_cache_const |
| 149 |
? 'Defined and truthy in wp-config.php.' |
| 150 |
: 'Not set — cached pages are being served the slow way. Without this constant WordPress boots fully before xSpeed can answer from cache, costing roughly 10ms per hit. ' . ( $wp_config_writable |
| 151 |
? 'wp-config.php is writable, so toggling Enable Cache off and on should set it for you; if it comes back, another plugin may have left define( \'WP_CACHE\', false ) behind — add the line below by hand instead.' |
| 152 |
: 'wp-config.php is not writable here (managed hosts often make it read-only), so add the line below by hand, above the "That\'s all, stop editing!" comment.' ), |
| 153 |
); |
| 154 |
if ( ! $wp_cache_const ) { |
| 155 |
// The line to paste, carried on the check itself so it |
| 156 |
// survives on a persistent surface. Enabling the cache |
| 157 |
// offered this only inside a toast that cleared after two |
| 158 |
// seconds with no copy button, so a user who looked away |
| 159 |
// had no way back to it anywhere in the dashboard — while |
| 160 |
// the toggle read green and the site cached nothing. |
| 161 |
// HealthCard already renders `snippet` through CopySnippet |
| 162 |
// (the nginx check proves the wiring). (#19) |
| 163 |
$check['snippet'] = "define( 'WP_CACHE', true );"; |
| 164 |
} |
| 165 |
$out[] = $check; |
| 166 |
} |
| 167 |
|
| 168 |
// Static-rewrite probe. Active end-to-end check: writes a probe |
| 169 |
// file under the static-cache dir, fetches it over HTTP, and |
| 170 |
// confirms the web server (nginx OR Apache/LiteSpeed) served |
| 171 |
// the raw file. Result is throttled to a 5-minute transient |
| 172 |
// inside Cache::probe_static_rewrite so we never hit the |
| 173 |
// network per-paint. |
| 174 |
if ( $cache_enabled ) { |
| 175 |
$server_type = Server::type(); |
| 176 |
// The live loopback probe only matters where a server-level static |
| 177 |
// rewrite is actually used (nginx snippet / Apache .htaccess). |
| 178 |
// LiteSpeed serves hits via the drop-in (see below), so skip the |
| 179 |
// probe there entirely — no needless self-request. Health is the |
| 180 |
// right place to pay for the probe when we DO run it (the admin |
| 181 |
// bootstrap reads cache-only so it never blocks); the 5-minute |
| 182 |
// transient still throttles repeat runs. (FBS-82142) |
| 183 |
$probe = ( Server::LITESPEED === $server_type ) |
| 184 |
? array( 'active' => false ) |
| 185 |
: Cache::probe_static_rewrite( true ); |
| 186 |
$is_active = (bool) ( $probe['active'] ?? false ); |
| 187 |
// An inconclusive probe (blocked loopback, TLS failure, timeout, a |
| 188 |
// CDN/WAF answering instead of the origin) proves nothing about the |
| 189 |
// rewrite. Reporting it as "not yet routing to the cache" told users |
| 190 |
// with a correct nginx config that their config was broken, and |
| 191 |
// pointed them at a snippet they had already pasted. (FBS-84012) |
| 192 |
$inconclusive = (bool) ( $probe['inconclusive'] ?? false ); |
| 193 |
$probe_reason = (string) ( $probe['reason'] ?? '' ); |
| 194 |
|
| 195 |
$block_reason = Cache::static_rewrite_block_reason(); |
| 196 |
$mobile_block = ( 'mobile_separate' === $block_reason ) |
| 197 |
? ' Note: Separate Mobile Cache is on, which disables the device-blind static rewrite — if your site serves the same HTML to all devices, turn it off (Cache settings) for much faster cache hits.' |
| 198 |
: ''; |
| 199 |
|
| 200 |
// A known refusal OUTRANKS the probe. probe_static_rewrite() writes |
| 201 |
// its own file under the static-cache dir and fetches that — which |
| 202 |
// succeeds whenever the server can serve a static file at all, even |
| 203 |
// when static_rewrite_allowed() is false and no real page is being |
| 204 |
// served that way. Checking $is_active first therefore reported |
| 205 |
// "PHP bypassed" on a site whose pages were all returning |
| 206 |
// HIT (php): the panel answered "why isn't static serving active?" |
| 207 |
// with the opposite of the truth. When we already know why the |
| 208 |
// rewrite is off, say that and ignore the probe. (FBS-83145) |
| 209 |
$refused = ( '' !== $block_reason ); |
| 210 |
$is_active = $is_active && ! $refused; |
| 211 |
// A refusal is a definite finding, so it also outranks |
| 212 |
// "inconclusive" — otherwise a blocked rewrite whose probe merely |
| 213 |
// failed to complete would be reported as INFO ("nothing to warn |
| 214 |
// about") instead of the WARN the block deserves. |
| 215 |
$inconclusive = $inconclusive && ! $refused; |
| 216 |
|
| 217 |
if ( Server::NGINX === $server_type ) { |
| 218 |
if ( $is_active ) { |
| 219 |
$nginx_detail = 'nginx is serving cache hits directly — PHP bypassed (~5-15ms TTFB).'; |
| 220 |
} elseif ( 'mobile_separate' === $block_reason ) { |
| 221 |
$nginx_detail = 'nginx detected, but the static rewrite is disabled because Separate Mobile Cache is on.' . $mobile_block; |
| 222 |
} elseif ( $inconclusive ) { |
| 223 |
$nginx_detail = sprintf( |
| 224 |
'Could not verify the static rewrite — the check itself did not complete, so this is not evidence that your config is wrong. If you have already pasted the snippet, it may well be working. Reason: %s', |
| 225 |
$probe_reason |
| 226 |
); |
| 227 |
} else { |
| 228 |
$nginx_detail = 'nginx detected but not yet routing to the cache. Paste the snippet below into your site\'s server { } block, then reload nginx.'; |
| 229 |
} |
| 230 |
|
| 231 |
$out[] = array( |
| 232 |
'id' => 'static_rewrite_nginx', |
| 233 |
// Inconclusive is INFO, not WARN — we have no finding to warn about. |
| 234 |
'tone' => $is_active ? self::OK : ( $inconclusive ? self::INFO : self::WARN ), |
| 235 |
'label' => 'Static-file rewrite (nginx server config)', |
| 236 |
'detail' => $nginx_detail, |
| 237 |
// Always ship the snippet — even when active, so the |
| 238 |
// admin has it handy for re-pasting after a server |
| 239 |
// rebuild without having to find it elsewhere. Mirror |
| 240 |
// the SAME unified block the "Server config" panel |
| 241 |
// renders (cache + gzip + browser-cache directives), |
| 242 |
// not the cache-only snippet — otherwise Health and |
| 243 |
// the Cache panel disagree on what to paste. |
| 244 |
'snippet' => Cache::full_nginx_server_block(), |
| 245 |
); |
| 246 |
} elseif ( Server::LITESPEED === $server_type ) { |
| 247 |
// LiteSpeed intentionally does NOT use the .htaccess static |
| 248 |
// rewrite: OpenLiteSpeed's .htaccess engine ignores |
| 249 |
// mod_headers (so we can't stamp X-XSpeed-Cache: HIT) and has |
| 250 |
// no per-rule access_log (so a static hit can't be counted). |
| 251 |
// We route LiteSpeed hits through the PHP drop-in instead, so |
| 252 |
// every hit is both visible (X-XSpeed-Cache: HIT) and counted |
| 253 |
// in the hit-ratio — see Cache::static_rewrite_allowed(). This |
| 254 |
// is the healthy, expected state on LiteSpeed, not a fallback. |
| 255 |
$out[] = array( |
| 256 |
'id' => 'static_rewrite_litespeed', |
| 257 |
'tone' => self::OK, |
| 258 |
'label' => 'Cache serving (LiteSpeed)', |
| 259 |
'detail' => 'Cache hits are served by xSpeed\'s drop-in and tagged X-XSpeed-Cache: HIT — so every hit is visible and counted in your hit-ratio. (LiteSpeed\'s .htaccess can\'t add that header or log static hits, so xSpeed serves them itself for accurate reporting.)', |
| 260 |
); |
| 261 |
} elseif ( Server::APACHE === $server_type ) { |
| 262 |
$installed = Cache::rewrite_installed(); |
| 263 |
if ( $is_active ) { |
| 264 |
$tone = self::OK; |
| 265 |
$detail = 'Block installed and serving cache hits directly — PHP bypassed.'; |
| 266 |
} elseif ( 'mobile_separate' === $block_reason ) { |
| 267 |
$tone = self::WARN; |
| 268 |
$detail = 'Static rewrite disabled because Separate Mobile Cache is on.' . $mobile_block; |
| 269 |
} elseif ( 'no_mod_headers' === $block_reason ) { |
| 270 |
// Not "missing" — deliberately not installed, because |
| 271 |
// Apache can't stamp X-XSpeed-Cache without mod_headers |
| 272 |
// and the hit would be invisible and uncountable. Caching |
| 273 |
// still works via the drop-in; say so, and give the one |
| 274 |
// step that actually changes the outcome. |
| 275 |
$tone = self::INFO; |
| 276 |
$detail = 'Cache hits are served by xSpeed\'s drop-in and tagged X-XSpeed-Cache: HIT (php), so every hit is visible and counted. The faster .htaccess fast path is off because Apache\'s mod_headers module is not loaded — without it a static hit could not be tagged or counted. Enable mod_headers (`a2enmod headers` on Debian/Ubuntu, then restart Apache) to shave roughly 20-30ms off each cache hit.'; |
| 277 |
} elseif ( ! $installed ) { |
| 278 |
$tone = self::WARN; |
| 279 |
$detail = 'Block missing from .htaccess. Toggle Enable Cache off and on to reinstall it.'; |
| 280 |
} elseif ( $inconclusive ) { |
| 281 |
// Same distinction as the nginx branch: the probe never |
| 282 |
// reached a verdict, so telling the user to go re-check |
| 283 |
// AllowOverride blames a config that may be perfectly |
| 284 |
// fine. Report the failure honestly instead. (FBS-84012) |
| 285 |
$tone = self::INFO; |
| 286 |
$detail = sprintf( |
| 287 |
'Block is installed, but the check could not complete (%s), so this is not evidence that anything is misconfigured. Re-run it with `wp xspeed cache recheck-rewrite` once the site can reach itself over HTTP.', |
| 288 |
$probe_reason |
| 289 |
); |
| 290 |
} else { |
| 291 |
$tone = self::WARN; |
| 292 |
$detail = sprintf( 'Block installed but probe failed (%s). Confirm the .htaccess block is at the TOP of the file, and that AllowOverride is enabled for your site so mod_rewrite reads it.', $probe_reason ); |
| 293 |
} |
| 294 |
$out[] = array( |
| 295 |
'id' => 'static_rewrite', |
| 296 |
'tone' => $tone, |
| 297 |
'label' => 'Static-file rewrite (.htaccess)', |
| 298 |
'detail' => $detail, |
| 299 |
); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// Cache expiry vs preloader schedule (deterministic rule, issue #31): |
| 304 |
// pages that expire faster than the preloader re-warms them leave the |
| 305 |
// cache cold for most real traffic — the classic "24.8% hit ratio with |
| 306 |
// everything on" misconfiguration. Pure logic in |
| 307 |
// expiry_preload_check() so it's unit-testable. |
| 308 |
if ( $cache_enabled ) { |
| 309 |
$cache_opts = Settings_Manager::get( 'cache' ); |
| 310 |
$pre_opts = Settings_Manager::get( 'preloader' ); |
| 311 |
$schedule = (string) ( $pre_opts['schedule'] ?? 'manual' ); |
| 312 |
$mismatch = self::expiry_preload_check( |
| 313 |
(int) ( $cache_opts['cache_expiry'] ?? 24 ), |
| 314 |
$schedule, |
| 315 |
! empty( $pre_opts['enabled'] ), |
| 316 |
self::schedule_interval_hours( $schedule ) |
| 317 |
); |
| 318 |
if ( null !== $mismatch ) { |
| 319 |
$out[] = $mismatch; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// Permalinks |
| 324 |
$permalinks_ok = (bool) get_option( 'permalink_structure' ); |
| 325 |
$out[] = array( |
| 326 |
'id' => 'permalinks', |
| 327 |
'tone' => $permalinks_ok ? self::OK : self::WARN, |
| 328 |
'label' => 'Permalinks', |
| 329 |
'detail' => $permalinks_ok |
| 330 |
? 'Pretty permalinks active.' |
| 331 |
: 'Set permalinks to anything other than "Plain" — page caching needs URL paths to key on.', |
| 332 |
); |
| 333 |
|
| 334 |
// Cache-poisoning Set-Cookie detection (issue #33): a plugin emitting |
| 335 |
// Set-Cookie on anonymous pageviews forces CDN/edge BYPASS for all |
| 336 |
// HTML (Cloudflare never caches a response carrying Set-Cookie). Probe |
| 337 |
// is transient-throttled inside Cookie_Inspector, same pattern as the |
| 338 |
// static-rewrite probe above — Health is the right place to pay for it. |
| 339 |
// Cached-only: Health runs inside the dashboard REST request and the |
| 340 |
// MCP get_health tool, so this must never block on an HTTP call. |
| 341 |
// A cold verdict schedules a background refresh and reports nothing |
| 342 |
// this paint. See Cookie_Inspector::probe_cached(). |
| 343 |
$cookie_probe = Cookie_Inspector::probe_cached(); |
| 344 |
if ( $cookie_probe['checked'] ) { |
| 345 |
$offenders = $cookie_probe['cookies']; |
| 346 |
if ( empty( $offenders ) ) { |
| 347 |
$out[] = array( |
| 348 |
'id' => 'set_cookie_poisoning', |
| 349 |
'tone' => self::OK, |
| 350 |
'label' => 'No cache-poisoning cookies', |
| 351 |
'detail' => 'Anonymous pages are served without Set-Cookie, so CDN/edge caches can store them.', |
| 352 |
); |
| 353 |
} else { |
| 354 |
$named = array(); |
| 355 |
foreach ( $offenders as $c ) { |
| 356 |
$named[] = null !== $c['plugin'] |
| 357 |
? sprintf( '%s is setting %s', $c['plugin'], $c['name'] ) |
| 358 |
: sprintf( 'an unidentified plugin is setting %s', $c['name'] ); |
| 359 |
} |
| 360 |
$out[] = array( |
| 361 |
'id' => 'set_cookie_poisoning', |
| 362 |
'tone' => self::WARN, |
| 363 |
'label' => 'Set-Cookie on cacheable pages', |
| 364 |
'detail' => sprintf( |
| 365 |
'%s — this prevents CDN edge caching (Cloudflare returns BYPASS for any response with Set-Cookie). Configure the plugin to set its cookie via JavaScript instead, or exclude it from anonymous pageviews.', |
| 366 |
implode( '; ', $named ) |
| 367 |
), |
| 368 |
); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
// Conflicting plugins |
| 373 |
$out[] = array( |
| 374 |
'id' => 'conflicts', |
| 375 |
'tone' => empty( $conflicts ) ? self::OK : self::WARN, |
| 376 |
'label' => 'Caching plugin conflicts', |
| 377 |
'detail' => empty( $conflicts ) |
| 378 |
? 'No other caching plugins detected.' |
| 379 |
: sprintf( 'Active: %s. Deactivate before enabling xSpeed cache to avoid double-caching.', implode( ', ', $conflicts ) ), |
| 380 |
); |
| 381 |
|
| 382 |
/* |
| 383 |
* A migration whose source is STILL RUNNING. |
| 384 |
* |
| 385 |
* Distinct from the generic `conflicts` check above, which only says |
| 386 |
* "another caching plugin is active". This one knows the user imported |
| 387 |
* from it and chose (or was refused) to leave it on, so it can name the |
| 388 |
* plugin and the decision. |
| 389 |
* |
| 390 |
* The point is persistence: the import screen's warning disappears the |
| 391 |
* moment the user navigates away, and the risk does not. Two page |
| 392 |
* caches fighting over the drop-in is exactly what breaks caching for |
| 393 |
* both, so the warning has to outlive the screen it was raised on. |
| 394 |
* (#189 AC4) |
| 395 |
*/ |
| 396 |
$pending = Migration::pending_source(); |
| 397 |
if ( null !== $pending ) { |
| 398 |
$out[] = array( |
| 399 |
'id' => 'migration_source_active', |
| 400 |
'tone' => self::WARN, |
| 401 |
'label' => sprintf( '%s is still active after import', $pending['label'] ), |
| 402 |
'detail' => sprintf( |
| 403 |
'You imported settings from %s but left it running. Two page caches fight over the cache drop-in and can break caching for both — deactivate %s on the Plugins screen once you have checked the imported settings.', |
| 404 |
$pending['label'], |
| 405 |
$pending['label'] |
| 406 |
), |
| 407 |
); |
| 408 |
} |
| 409 |
|
| 410 |
return $out; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Hours between recurring preloader crawls, per schedule option. |
| 415 |
* `twicedaily` is a WordPress core schedule — omitting it meant a site |
| 416 |
* using it got no check at all, not even a pass. |
| 417 |
*/ |
| 418 |
public const PRELOAD_INTERVALS = array( |
| 419 |
'hourly' => 1, |
| 420 |
'twicedaily' => 12, |
| 421 |
'daily' => 24, |
| 422 |
'weekly' => 168, |
| 423 |
); |
| 424 |
|
| 425 |
/** |
| 426 |
* Interval in hours for a cron schedule slug, or null when it isn't a |
| 427 |
* recurring schedule (`manual`) or can't be resolved. |
| 428 |
* |
| 429 |
* Falls back to `wp_get_schedules()` so custom crons registered by a |
| 430 |
* theme or another plugin are covered too, rather than silently |
| 431 |
* skipping the check. |
| 432 |
*/ |
| 433 |
public static function schedule_interval_hours( string $schedule ): ?int { |
| 434 |
if ( isset( self::PRELOAD_INTERVALS[ $schedule ] ) ) { |
| 435 |
return self::PRELOAD_INTERVALS[ $schedule ]; |
| 436 |
} |
| 437 |
if ( '' === $schedule || 'manual' === $schedule || ! function_exists( 'wp_get_schedules' ) ) { |
| 438 |
return null; |
| 439 |
} |
| 440 |
$schedules = wp_get_schedules(); |
| 441 |
if ( ! isset( $schedules[ $schedule ]['interval'] ) ) { |
| 442 |
return null; |
| 443 |
} |
| 444 |
$hours = (int) round( (int) $schedules[ $schedule ]['interval'] / HOUR_IN_SECONDS ); |
| 445 |
return $hours > 0 ? $hours : 1; |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* Deterministic rule: warn when cache_expiry is shorter than the |
| 450 |
* preloader's recurring interval (pages go cold between crawls). |
| 451 |
* |
| 452 |
* Pure — no WP calls — so it can be unit-tested directly. |
| 453 |
* |
| 454 |
* @param int $expiry_hours Cache expiry in hours. |
| 455 |
* @param string $schedule Preloader schedule (manual|hourly|twicedaily|daily|weekly|custom). |
| 456 |
* @param bool $preloader_enabled Whether the preloader module is on. |
| 457 |
* @param int|null $interval_hours Pre-resolved interval, for schedules |
| 458 |
* outside PRELOAD_INTERVALS. Keeps this |
| 459 |
* function pure — the caller does the |
| 460 |
* wp_get_schedules() lookup. |
| 461 |
* @return array{id:string,tone:string,label:string,detail:string}|null Check |
| 462 |
* row, or null when the rule doesn't apply (preloader off/manual). |
| 463 |
*/ |
| 464 |
public static function expiry_preload_check( int $expiry_hours, string $schedule, bool $preloader_enabled, ?int $interval_hours = null ): ?array { |
| 465 |
if ( ! $preloader_enabled ) { |
| 466 |
return null; |
| 467 |
} |
| 468 |
$interval = $interval_hours ?? ( self::PRELOAD_INTERVALS[ $schedule ] ?? null ); |
| 469 |
if ( null === $interval || $interval < 1 ) { |
| 470 |
return null; |
| 471 |
} |
| 472 |
if ( $expiry_hours < $interval ) { |
| 473 |
return array( |
| 474 |
'id' => 'expiry_preload_mismatch', |
| 475 |
'tone' => self::WARN, |
| 476 |
'label' => 'Cache expiry shorter than the preload schedule', |
| 477 |
'detail' => sprintf( |
| 478 |
'Pages expire after %dh but the preloader only re-warms them every %dh (%s), so most visits hit a cold cache. Raise Cache Expiry to at least %dh (Cache settings), or preload more often (Preloader settings).', |
| 479 |
$expiry_hours, |
| 480 |
$interval, |
| 481 |
$schedule, |
| 482 |
$interval |
| 483 |
), |
| 484 |
); |
| 485 |
} |
| 486 |
return array( |
| 487 |
'id' => 'expiry_preload_mismatch', |
| 488 |
'tone' => self::OK, |
| 489 |
'label' => 'Cache expiry covers the preload schedule', |
| 490 |
'detail' => sprintf( 'Expiry %dh ≥ preload interval %dh (%s) — preloaded pages stay warm between crawls.', $expiry_hours, $interval, $schedule ), |
| 491 |
); |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Lightweight environment payload for the onboarding wizard. Keeps |
| 496 |
* the legacy shape Onboarding::env_payload returned so the Welcome |
| 497 |
* step's HealthRow rendering doesn't change. |
| 498 |
*/ |
| 499 |
public static function env_payload(): array { |
| 500 |
global $wp_version; |
| 501 |
$cache_dir = defined( 'XSPEED_CACHE_DIR' ) ? XSPEED_CACHE_DIR : ( WP_CONTENT_DIR . '/cache/xspeed' ); |
| 502 |
return array( |
| 503 |
'wp' => array( |
| 504 |
'version' => (string) $wp_version, |
| 505 |
'ok' => version_compare( (string) $wp_version, '6.0', '>=' ), |
| 506 |
), |
| 507 |
'php' => array( |
| 508 |
'version' => PHP_VERSION, |
| 509 |
'ok' => version_compare( PHP_VERSION, '7.4', '>=' ), |
| 510 |
'modern' => version_compare( PHP_VERSION, '8.1', '>=' ), |
| 511 |
), |
| 512 |
'server' => array( |
| 513 |
'type' => Server::type(), |
| 514 |
'gzip_mode' => Server::gzip_mode(), |
| 515 |
), |
| 516 |
'cache_dir' => array( |
| 517 |
'path' => $cache_dir, |
| 518 |
'writable' => wp_mkdir_p( $cache_dir ) && wp_is_writable( $cache_dir ), |
| 519 |
), |
| 520 |
'wp_config' => array( |
| 521 |
'writable' => self::wp_config_writable(), |
| 522 |
), |
| 523 |
'permalinks_ok' => (bool) get_option( 'permalink_structure' ), |
| 524 |
'conflicts' => Server::conflicts(), |
| 525 |
); |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Cheap writability probe for the onboarding env payload only. |
| 530 |
* |
| 531 |
* Deliberately NOT the oracle behind the WP_CACHE check — that asks |
| 532 |
* Cache::can_write_wp_config(), which runs the same WP_Filesystem test |
| 533 |
* the writer runs, so advice can never contradict behaviour. This one |
| 534 |
* stays a plain filesystem read because env_payload() is documented as |
| 535 |
* making no outbound calls, and WP_Filesystem() can try to open an |
| 536 |
* FTP/SSH connection. It shares the writer's path resolution so the two |
| 537 |
* at least agree on WHICH file they are describing. (#19, QA on #174) |
| 538 |
*/ |
| 539 |
private static function wp_config_writable(): bool { |
| 540 |
$path = Cache::wp_config_path(); |
| 541 |
return '' !== $path && wp_is_writable( $path ); |
| 542 |
} |
| 543 |
} |
| 544 |
|