| @@ -25,8 +25,91 @@ | ||
| 25 | 25 | use XSpeed\Settings_Manager; |
| 26 | 26 | |
| 27 | 27 | final class CacheModule extends Module { |
| 28 | 28 | |
| 29 | + /** | |
| 30 | + * Default Excluded Cookies. | |
| 31 | + * | |
| 32 | + * A constant for the same reason as DEFAULT_IGNORED_QUERY_PARAMS: | |
| 33 | + * Cache::rewrite_block_lines() needs the list at boot, where building | |
| 34 | + * the settings schema would translate its labels too early. The schema | |
| 35 | + * and that fallback both read THIS, so they cannot drift. | |
| 36 | + * | |
| 37 | + * @var string[] | |
| 38 | + */ | |
| 39 | + public const DEFAULT_EXCLUDED_COOKIES = array( | |
| 40 | + 'comment_author', | |
| 41 | + '~wordpress_[a-f0-9]+', | |
| 42 | + 'wp-postpass', | |
| 43 | + 'wordpress_no_cache', | |
| 44 | + 'wordpress_logged_in', | |
| 45 | + 'edd_items_in_cart', | |
| 46 | + 'woocommerce_items_in_cart', | |
| 47 | + 'fct_cart_hash', | |
| 48 | + 'comment_', | |
| 49 | + 'woocommerce_', | |
| 50 | + 'wordpress', | |
| 51 | + 'xf_', | |
| 52 | + 'edd_', | |
| 53 | + 'jetpack', | |
| 54 | + 'yith_wcwl_session_', | |
| 55 | + 'yith_wrvp_', | |
| 56 | + 'wpsc_', | |
| 57 | + 'ecwid', | |
| 58 | + 'ec_', | |
| 59 | + 'bookly', | |
| 60 | + ); | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Default Ignored Query Parameters. | |
| 64 | + * | |
| 65 | + * A constant because Cache::sync_query_allowlist() needs this list at | |
| 66 | + * boot, where building the settings schema would translate its labels | |
| 67 | + * before WordPress allows it. Both the schema below and that boot-time | |
| 68 | + * fallback read THIS, so the two cannot drift. | |
| 69 | + * | |
| 70 | + * @var string[] | |
| 71 | + */ | |
| 72 | + public const DEFAULT_IGNORED_QUERY_PARAMS = array( | |
| 73 | + '__s', | |
| 74 | + '_ga', | |
| 75 | + '_ke', | |
| 76 | + '~[a-zA-Z0-9_-]+_sid', | |
| 77 | + 'adgroupid', | |
| 78 | + 'age-verified', | |
| 79 | + 'ao_noptimize', | |
| 80 | + 'campaignid', | |
| 81 | + 'ck_subscriber_id', | |
| 82 | + 'cn-reloaded', | |
| 83 | + 'dclid', | |
| 84 | + 'epik', | |
| 85 | + 'fb_action_ids', | |
| 86 | + 'fb_action_types', | |
| 87 | + 'fb_source', | |
| 88 | + 'fbclid', | |
| 89 | + 'gclid', | |
| 90 | + 'jobid', | |
| 91 | + 'mc_cid', | |
| 92 | + 'mc_eid', | |
| 93 | + 'mkt_tok', | |
| 94 | + 'msclkid', | |
| 95 | + 'ref', | |
| 96 | + // Twitter/X (`ref_src`, `ref_url`) and Facebook (`refid`) | |
| 97 | + // decorations. Enumerated because param names match | |
| 98 | + // whole-name: the bare `ref` above no longer absorbs them, | |
| 99 | + // and a `ref*` glob would over-match `referrer` and | |
| 100 | + // `refund_id`, which are page-selecting. | |
| 101 | + 'ref_src', | |
| 102 | + 'ref_url', | |
| 103 | + 'refid', | |
| 104 | + '~session_[a-zA-Z0-9_-]+_alive', | |
| 105 | + 'sseid', | |
| 106 | + 'sslid', | |
| 107 | + 'usqp', | |
| 108 | + '~utm_[a-zA-Z0-9_-]+', | |
| 109 | + ); | |
| 110 | + | |
| 111 | + | |
| 29 | 112 | public const SLUG = 'cache'; |
| 30 | 113 | public const TIER = self::TIER_FREE; |
| 31 | 114 | public const VERSION = '1.0.0'; |
| 32 | 115 | |
| @@ -43,11 +126,11 @@ | ||
| 43 | 126 | public const DEFAULT_EXPIRY_HOURS = 24 * 7; |
| 44 | 127 | |
| 45 | 128 | public function ui_metadata(): array { |
| 46 | 129 | return array( |
| 47 | - 'label' => 'Page Cache', | |
| 130 | + 'label' => __( 'Page Cache', 'xspeed' ), | |
| 48 | 131 | 'icon' => 'Database', |
| 49 | - 'description' => 'Page caching for non-logged-in visitors.', | |
| 132 | + 'description' => __( 'Page caching for non-logged-in visitors.', 'xspeed' ), | |
| 50 | 133 | ); |
| 51 | 134 | } |
| 52 | 135 | |
| 53 | 136 | /** |
| @@ -71,11 +154,11 @@ | ||
| 71 | 154 | // disagreed about what "default" means. (#284) |
| 72 | 155 | 'default' => self::DEFAULT_EXPIRY_HOURS, |
| 73 | 156 | 'min' => 1, |
| 74 | 157 | 'max' => 720, |
| 75 | - 'label' => 'Cache Expiry (hours)', | |
| 158 | + 'label' => __( 'Cache Expiry (hours)', 'xspeed' ), | |
| 76 | 159 | 'unit' => 'hours', |
| 77 | - 'description' => 'How long cached pages live before regenerating. 1 to 720 hours (30 days).', | |
| 160 | + 'description' => __( 'How long cached pages live before regenerating. 1 to 720 hours (30 days).', 'xspeed' ), | |
| 78 | 161 | ), |
| 79 | 162 | 'excluded_urls' => array( |
| 80 | 163 | 'type' => 'list', |
| 81 | 164 | // Comprehensive LiteSpeed / WP Rocket-parity default URL |
| @@ -87,9 +170,11 @@ | ||
| 87 | 170 | '/xmlrpc.php', |
| 88 | 171 | '~wp-.*\.php', |
| 89 | 172 | '/feed/', |
| 90 | 173 | 'index.php', |
| 91 | - '~sitemap(_index)?\.xml', | |
| 174 | + // `sitemaps?` — SEOPress generates sitemaps.xml (plural). | |
| 175 | + '~sitemaps?(_index)?\.xml', | |
| 176 | + '/robots.txt', | |
| 92 | 177 | // Bare (no trailing slash) so "contains" matches both |
| 93 | 178 | // /cart and /cart/items — WooCommerce serves both forms. |
| 94 | 179 | '/cart', |
| 95 | 180 | '/checkout', |
| @@ -101,10 +186,10 @@ | ||
| 101 | 186 | '/edd-api', |
| 102 | 187 | '/wp-login', |
| 103 | 188 | ), |
| 104 | 189 | 'item_type' => 'string', |
| 105 | - 'label' => 'Excluded URLs', | |
| 106 | - 'description' => 'One pattern per line. Plain text matches anywhere in the URL (e.g. /cart). Use glob for anchored matches (/cart/* matches /cart/items but not /foo/cart/bar; *.pdf matches PDFs). Prefix with ~ for a raw regex (e.g. ~wp-.*\.php).', | |
| 190 | + 'label' => __( 'Excluded URLs', 'xspeed' ), | |
| 191 | + 'description' => __( 'One pattern per line. Plain text matches anywhere in the URL (e.g. /cart). Use glob for anchored matches (/cart/* matches /cart/items but not /foo/cart/bar; *.pdf matches PDFs). Prefix with ~ for a raw regex (e.g. ~wp-.*\.php).', 'xspeed' ), | |
| 107 | 192 | ), |
| 108 | 193 | 'excluded_cookies' => array( |
| 109 | 194 | 'type' => 'list', |
| 110 | 195 | // Cookies that signal a logged-in / transactional visitor |
| @@ -109,40 +194,19 @@ | ||
| 109 | 194 | 'type' => 'list', |
| 110 | 195 | // Cookies that signal a logged-in / transactional visitor |
| 111 | 196 | // whose response must not be served from a shared cache. |
| 112 | 197 | // `~` prefix = raw regex (e.g. ~wordpress_[a-f0-9]+). (FBS-82181) |
| 113 | - 'default' => array( | |
| 114 | - 'comment_author', | |
| 115 | - '~wordpress_[a-f0-9]+', | |
| 116 | - 'wp-postpass', | |
| 117 | - 'wordpress_no_cache', | |
| 118 | - 'wordpress_logged_in', | |
| 119 | - 'edd_items_in_cart', | |
| 120 | - 'woocommerce_items_in_cart', | |
| 121 | - 'fct_cart_hash', | |
| 122 | - 'comment_', | |
| 123 | - 'woocommerce_', | |
| 124 | - 'wordpress', | |
| 125 | - 'xf_', | |
| 126 | - 'edd_', | |
| 127 | - 'jetpack', | |
| 128 | - 'yith_wcwl_session_', | |
| 129 | - 'yith_wrvp_', | |
| 130 | - 'wpsc_', | |
| 131 | - 'ecwid', | |
| 132 | - 'ec_', | |
| 133 | - 'bookly', | |
| 134 | - ), | |
| 198 | + 'default' => self::DEFAULT_EXCLUDED_COOKIES, | |
| 135 | 199 | 'item_type' => 'string', |
| 136 | - 'label' => 'Excluded Cookies', | |
| 137 | - 'description' => 'Skip cache for any visitor whose request carries a cookie whose NAME matches one of these patterns. Plain text = "contains"; glob (woocommerce_*) and ~regex (~wordpress_[a-f0-9]+) supported. One per line.', | |
| 200 | + 'label' => __( 'Excluded Cookies', 'xspeed' ), | |
| 201 | + 'description' => __( 'Skip cache for any visitor whose request carries a cookie whose NAME matches one of these patterns. Plain text = "contains"; glob (woocommerce_*) and ~regex (~wordpress_[a-f0-9]+) supported. One per line.', 'xspeed' ), | |
| 138 | 202 | ), |
| 139 | 203 | 'bypass_user_agents' => array( |
| 140 | 204 | 'type' => 'list', |
| 141 | 205 | 'default' => array(), |
| 142 | 206 | 'item_type' => 'string', |
| 143 | - 'label' => 'Bypass User Agents', | |
| 144 | - 'description' => 'Substring match against the visitor User-Agent. Matched UAs bypass cache (useful for screenshot bots, internal previews, monitoring). Glob + ~regex supported. One per line.', | |
| 207 | + 'label' => __( 'Bypass User Agents', 'xspeed' ), | |
| 208 | + 'description' => __( 'Substring match against the visitor User-Agent. Matched UAs bypass cache (useful for screenshot bots, internal previews, monitoring). Glob + ~regex supported. One per line.', 'xspeed' ), | |
| 145 | 209 | ), |
| 146 | 210 | 'ignored_query_params' => array( |
| 147 | 211 | 'type' => 'list', |
| 148 | 212 | // Analytics / ad / session query keys stripped before the |
| @@ -149,62 +213,70 @@ | ||
| 149 | 213 | // cache key is computed, so /post?utm_source=x and /post |
| 150 | 214 | // share one entry. `~` prefix = raw regex. (FBS-82181) |
| 151 | 215 | // Matched whole-name, so every entry here means the param |
| 152 | 216 | // it names and nothing that merely contains it. |
| 153 | - 'default' => array( | |
| 154 | - '__s', | |
| 155 | - '_ga', | |
| 156 | - '_ke', | |
| 157 | - '~[a-zA-Z0-9_-]+_sid', | |
| 158 | - 'adgroupid', | |
| 159 | - 'age-verified', | |
| 160 | - 'ao_noptimize', | |
| 161 | - 'campaignid', | |
| 162 | - 'ck_subscriber_id', | |
| 163 | - 'cn-reloaded', | |
| 164 | - 'dclid', | |
| 165 | - 'epik', | |
| 166 | - 'fb_action_ids', | |
| 167 | - 'fb_action_types', | |
| 168 | - 'fb_source', | |
| 169 | - 'fbclid', | |
| 170 | - 'gclid', | |
| 171 | - 'jobid', | |
| 172 | - 'mc_cid', | |
| 173 | - 'mc_eid', | |
| 174 | - 'mkt_tok', | |
| 175 | - 'msclkid', | |
| 176 | - 'ref', | |
| 177 | - // Twitter/X (`ref_src`, `ref_url`) and Facebook (`refid`) | |
| 178 | - // decorations. Enumerated because param names match | |
| 179 | - // whole-name: the bare `ref` above no longer absorbs them, | |
| 180 | - // and a `ref*` glob would over-match `referrer` and | |
| 181 | - // `refund_id`, which are page-selecting. | |
| 182 | - 'ref_src', | |
| 183 | - 'ref_url', | |
| 184 | - 'refid', | |
| 185 | - '~session_[a-zA-Z0-9_-]+_alive', | |
| 186 | - 'sseid', | |
| 187 | - 'sslid', | |
| 188 | - 'usqp', | |
| 189 | - '~utm_[a-zA-Z0-9_-]+', | |
| 190 | - ), | |
| 217 | + 'default' => self::DEFAULT_IGNORED_QUERY_PARAMS, | |
| 191 | 218 | 'item_type' => 'string', |
| 192 | - 'label' => 'Ignored Query Parameters', | |
| 193 | - 'description' => 'Query keys removed from the URL before computing the cache key, so /post?utm_source=x and /post share a cache entry. Defaults cover the common analytics + ad + session params. Each entry matches a whole param name — plain text is an exact name, and glob (utm_*) or ~regex are anchored too, so "ref" does not also match "preference". One per line.', | |
| 219 | + 'label' => __( 'Ignored Query Parameters', 'xspeed' ), | |
| 220 | + 'description' => __( 'Query keys removed from the URL before computing the cache key, so /post?utm_source=x and /post share a cache entry. Defaults cover the common analytics + ad + session params. Each entry matches a whole param name — plain text is an exact name, and glob (utm_*) or ~regex are anchored too, so "ref" does not also match "preference". One per line.', 'xspeed' ), | |
| 194 | 221 | ), |
| 195 | 222 | 'purge_on_upgrade' => array( |
| 196 | 223 | 'type' => 'bool', |
| 197 | 224 | 'default' => true, |
| 198 | - 'label' => 'Purge After Updates', | |
| 199 | - 'description' => 'Clear the page cache when a plugin, theme or WordPress core is updated. Cached HTML is produced by the code being replaced, so leaving it in place serves pre-update markup — and links to minified assets that no longer exist — until the cache expires. Translation updates are ignored, since a language pack changes no markup a cached page depends on. Updates to xSpeed itself always purge, regardless of this setting.', | |
| 225 | + 'label' => __( 'Purge After Updates', 'xspeed' ), | |
| 226 | + 'description' => __( 'Clear the page cache when a plugin, theme or WordPress core is updated. Cached HTML is produced by the code being replaced, so leaving it in place serves pre-update markup — and links to minified assets that no longer exist — until the cache expires. Translation updates are ignored, since a language pack changes no markup a cached page depends on. Updates to xSpeed itself always purge, regardless of this setting.', 'xspeed' ), | |
| 200 | 227 | ), |
| 201 | 228 | 'mobile_separate' => array( |
| 202 | 229 | 'type' => 'bool', |
| 203 | 230 | 'default' => false, |
| 204 | - 'label' => 'Separate Mobile Cache', | |
| 205 | - 'description' => 'Keep mobile and desktop responses in separate cache buckets. Turn on for AMP, mobile-specific themes (WPtouch / Jetpack mobile theme), or any setup that serves different HTML by device.', | |
| 231 | + 'label' => __( 'Separate Mobile Cache', 'xspeed' ), | |
| 232 | + 'description' => __( 'Keep mobile and desktop responses in separate cache buckets. Turn on for AMP, mobile-specific themes (WPtouch / Jetpack mobile theme), or any setup that serves different HTML by device.', 'xspeed' ), | |
| 206 | 233 | ), |
| 234 | + 'edge_provider' => array( | |
| 235 | + 'type' => 'enum', | |
| 236 | + 'default' => 'auto', | |
| 237 | + 'options' => array( 'auto', 'off', 'cloudflare', 'fastly', 'varnish', 'nginx', 'akamai', 'cloudfront', 'google', 'keycdn', 'bunny', 'sucuri', 'incapsula', 'generic', 'custom' ), | |
| 238 | + 'option_labels' => array( | |
| 239 | + 'auto' => 'Detect automatically', | |
| 240 | + 'off' => 'Off — send nothing', | |
| 241 | + 'cloudflare' => 'Cloudflare', | |
| 242 | + 'varnish' => 'Varnish', | |
| 243 | + 'nginx' => 'nginx proxy cache', | |
| 244 | + 'cloudfront' => 'Amazon CloudFront', | |
| 245 | + 'google' => 'Google Cloud CDN', | |
| 246 | + 'keycdn' => 'KeyCDN', | |
| 247 | + 'bunny' => 'Bunny', | |
| 248 | + // These four cannot be presented as supported on the same | |
| 249 | + // footing as the ones above. Vendor documentation either | |
| 250 | + // does not establish that they honour what we send, or | |
| 251 | + // establishes that they ignore origin cache headers until | |
| 252 | + // the property is configured to respect them — Akamai | |
| 253 | + // caches for a theoretically infinite time by default, and | |
| 254 | + // Sucuri's default caching level ignores the headers | |
| 255 | + // outright. Naming them without the caveat would promise a | |
| 256 | + // protection the CDN is not currently giving. | |
| 257 | + 'fastly' => 'Fastly (needs CDN configuration)', | |
| 258 | + 'akamai' => 'Akamai (needs CDN configuration)', | |
| 259 | + 'sucuri' => 'Sucuri (needs CDN configuration)', | |
| 260 | + 'incapsula' => 'Imperva / Incapsula (needs CDN configuration)', | |
| 261 | + 'generic' => 'Something else', | |
| 262 | + 'custom' => 'Custom headers', | |
| 263 | + ), | |
| 264 | + 'label' => __( 'Cache In Front Of This Site', 'xspeed' ), | |
| 265 | + 'description' => __( 'Ask a CDN or proxy in front of your site not to store pages xSpeed refused to cache. Leave it on Detect automatically unless you know what is in front of you; the marked providers ignore origin headers until you configure them to respect it.', 'xspeed' ), | |
| 266 | + 'info_title' => __( 'Cache in front of this site', 'xspeed' ), | |
| 267 | + 'info' => __( 'Naming your provider narrows the headers to the one it reads. Detect automatically works it out per request and otherwise sends a set every cache ignores unless it understands it, so it is safe not to know. Run "wp xspeed cache edge" to see what was detected and what gets sent.', 'xspeed' ) | |
| 268 | + ), | |
| 269 | + 'edge_custom_headers' => array( | |
| 270 | + 'type' => 'list', | |
| 271 | + 'default' => array(), | |
| 272 | + 'item_type' => 'string', | |
| 273 | + 'label' => __( 'Custom Edge Headers', 'xspeed' ), | |
| 274 | + 'dependsOn' => array( 'field' => 'edge_provider', 'value' => 'custom' ), | |
| 275 | + 'description' => __( 'One header per line, as Name: value — for example "Surrogate-Control: no-store". Lines starting with # are ignored.', 'xspeed' ), | |
| 276 | + 'info_title' => __( 'Custom edge headers', 'xspeed' ), | |
| 277 | + 'info' => __( 'These replace the headers xSpeed would have picked for your CDN. Two baselines are still added underneath: a Cache-Control, and "X-Accel-Expires: 0" for a page cache running in nginx on your own server. Name either one yourself and yours is used instead. Values containing $, % or a backslash are dropped — the same pairs go into nginx and Apache directives, where those cannot be escaped safely. Content-Length, Content-Encoding, Content-Type, Transfer-Encoding, Set-Cookie and Location are refused.', 'xspeed' ) | |
| 278 | + ), | |
| 207 | 279 | ); |
| 208 | 280 | } |
| 209 | 281 | |
| 210 | 282 | /** |
| @@ -352,19 +424,53 @@ | ||
| 352 | 424 | 'name' => 'budget', |
| 353 | 425 | 'description' => 'Seconds to spend before stopping between steps. Default 120.', |
| 354 | 426 | 'optional' => true, |
| 355 | 427 | ), |
| 428 | + array( | |
| 429 | + 'type' => 'assoc', | |
| 430 | + 'name' => 'measure-score', | |
| 431 | + 'description' => 'auto (default) measures when the stored score is stale and after changes land; never reuses the stored score; always measures even for a dry run.', | |
| 432 | + 'optional' => true, | |
| 433 | + 'options' => array( 'auto', 'never', 'always' ), | |
| 434 | + ), | |
| 356 | 435 | ), |
| 357 | 436 | ), |
| 358 | 437 | array( |
| 438 | + 'name' => 'xspeed purge', | |
| 439 | + 'callback' => array( $this, 'cli_purge' ), | |
| 440 | + 'shortdesc' => 'Clear every cache xSpeed manages — page and static files, REST responses, minified assets, the object cache and the configured edge — and report per store what was cleared, what was skipped and why. Use --type to clear just one.', | |
| 441 | + 'ai_hint' => 'Clear the cache after a change is live on the server but visitors still see the old version. Purges everything by default; --type=page for the local HTML only, --type=cloudflare for the edge only. Exits non-zero if a store that IS configured refused to purge, so its output can be trusted rather than assumed.', | |
| 442 | + 'synopsis' => array( | |
| 443 | + array( | |
| 444 | + 'type' => 'assoc', | |
| 445 | + 'name' => 'type', | |
| 446 | + 'description' => 'What to clear: all (default), page, object, cloudflare, cdn — or a group name (edge). Comma-separate to clear several.', | |
| 447 | + 'optional' => true, | |
| 448 | + ), | |
| 449 | + array( | |
| 450 | + 'type' => 'assoc', | |
| 451 | + 'name' => 'cause', | |
| 452 | + 'description' => 'Label recorded in the purge log, so `wp xspeed cache purge-log` can tell this run apart from a click. Default "CLI".', | |
| 453 | + 'optional' => true, | |
| 454 | + ), | |
| 455 | + array( | |
| 456 | + 'type' => 'assoc', | |
| 457 | + 'name' => 'format', | |
| 458 | + 'description' => 'table (default, one line per store) or json (the full report, for scripts).', | |
| 459 | + 'optional' => true, | |
| 460 | + 'options' => array( 'table', 'json' ), | |
| 461 | + ), | |
| 462 | + ), | |
| 463 | + ), | |
| 464 | + array( | |
| 359 | 465 | 'name' => 'xspeed cache', |
| 360 | 466 | 'callback' => array( $this, 'cli_handler' ), |
| 361 | - 'shortdesc' => 'Inspect the Cache module: `status` (settings), `inventory` (which pages are cached, and how old), `size` (where the disk usage goes), `purge-log` (what cleared the cache, when and why), `purge-url <url>` to clear one page, `recheck-rewrite` to re-run the static-rewrite probe, or `nginx-config` to print the unified nginx server-block for pasting into a vhost (site-wide purge / toggle use the dedicated commands).', | |
| 467 | + 'shortdesc' => 'Inspect the Cache module: `status` (settings), `inventory` (which pages are cached, and how old), `size` (where the disk usage goes), `purge-log` (what cleared the cache, when and why), `purge-url <url>` to clear one page, `recheck-rewrite` to re-run the static-rewrite probe, or `nginx-config` to print the unified nginx server-block for pasting into a vhost, or `edge` to show which cache is in front of the site and what xSpeed tells it. To clear the whole site use `wp xspeed purge`.', | |
| 362 | 468 | 'synopsis' => array( |
| 363 | 469 | array( |
| 364 | 470 | 'type' => 'positional', |
| 365 | 471 | 'name' => 'action', |
| 366 | - 'options' => array( 'status', 'inventory', 'size', 'purge-log', 'purge-url', 'recheck-rewrite', 'nginx-config' ), | |
| 472 | + 'options' => array( 'status', 'inventory', 'size', 'purge-log', 'purge-url', 'recheck-rewrite', 'nginx-config', 'edge' ), | |
| 367 | 473 | 'optional' => true, |
| 368 | 474 | ), |
| 369 | 475 | array( |
| 370 | 476 | 'type' => 'positional', |
| @@ -411,8 +517,9 @@ | ||
| 411 | 517 | array( |
| 412 | 518 | 'aggressiveness' => (string) ( $assoc['aggressiveness'] ?? 'standard' ), |
| 413 | 519 | 'dry_run' => isset( $assoc['dry-run'] ), |
| 414 | 520 | 'budget_seconds' => isset( $assoc['budget'] ) ? (int) $assoc['budget'] : 120, |
| 521 | + 'measure_score' => (string) ( $assoc['measure-score'] ?? 'auto' ), | |
| 415 | 522 | ) |
| 416 | 523 | ); |
| 417 | 524 | |
| 418 | 525 | if ( is_wp_error( $result ) ) { |
| @@ -420,8 +527,14 @@ | ||
| 420 | 527 | return; |
| 421 | 528 | } |
| 422 | 529 | |
| 423 | 530 | if ( ! empty( $result['dry_run'] ) ) { |
| 531 | + // The summary carries the score AND its age. Printing the plan | |
| 532 | + // without it left the one number a reader wants off the only | |
| 533 | + // command they run before deciding to apply anything. | |
| 534 | + if ( isset( $result['message'] ) ) { | |
| 535 | + \WP_CLI::log( (string) $result['message'] ); | |
| 536 | + } | |
| 424 | 537 | \WP_CLI::log( 'Plan (' . count( $result['plan'] ) . ' steps, nothing applied):' ); |
| 425 | 538 | foreach ( $result['plan'] as $step ) { |
| 426 | 539 | \WP_CLI::log( ' - ' . $step['change'] . ' [' . $step['tier'] . ']' ); |
| 427 | 540 | } |
| @@ -445,12 +558,149 @@ | ||
| 445 | 558 | \WP_CLI::log( ' ! ' . $row['issue'] . ( '' !== $row['fix'] ? ' — ' . $row['fix'] : '' ) ); |
| 446 | 559 | } |
| 447 | 560 | |
| 448 | 561 | if ( ! empty( $result['applied'] ) ) { |
| 449 | - \WP_CLI::success( count( $result['applied'] ) . ' change(s) applied and verified.' ); | |
| 562 | + // "applied and verified" was more than the checks earn. They read | |
| 563 | + // HTML in PHP and cannot run JavaScript, so this line was telling | |
| 564 | + // someone the site was fine when the only honest claim is that | |
| 565 | + // nothing in the markup looked broken. | |
| 566 | + \WP_CLI::success( count( $result['applied'] ) . ' change(s) applied; HTML checks passed.' ); | |
| 567 | + | |
| 568 | + if ( ! empty( $result['verify_urls'] ) ) { | |
| 569 | + \WP_CLI::log( '' ); | |
| 570 | + \WP_CLI::log( 'Now open these and check they render, with no console errors:' ); | |
| 571 | + foreach ( $result['verify_urls'] as $u ) { | |
| 572 | + \WP_CLI::log( ' ' . $u ); | |
| 573 | + } | |
| 574 | + } | |
| 450 | 575 | } |
| 451 | 576 | } |
| 452 | 577 | |
| 578 | + /** | |
| 579 | + * `wp xspeed purge` — clear every cache xSpeed owns, in one call. | |
| 580 | + * | |
| 581 | + * Reports per store rather than printing a success banner, because the | |
| 582 | + * banner was the bug: a site whose Cloudflare token had lost its purge | |
| 583 | + * permission saw "cache cleared" and kept serving stale HTML from the | |
| 584 | + * edge. What is skipped is as much of the answer as what is cleared, so | |
| 585 | + * every skip prints its reason. | |
| 586 | + * | |
| 587 | + * Exit code follows the same distinction. A store that is not configured | |
| 588 | + * has nothing to clear and does not fail the run — otherwise every CI | |
| 589 | + * pipeline on a site without Redis goes red for a purge that did exactly | |
| 590 | + * what it should. A store that IS configured and refused is a failure. | |
| 591 | + * | |
| 592 | + * There is deliberately no `--url`: WP-CLI reserves that flag for | |
| 593 | + * multisite site selection and consumes it before a handler ever sees it. | |
| 594 | + * Clearing one page is `wp xspeed cache purge-url <url>`. | |
| 595 | + * | |
| 596 | + * @param array<int,string> $args Positional args (unused). | |
| 597 | + * @param array<string,string> $assoc Flags. | |
| 598 | + */ | |
| 599 | + public function cli_purge( array $args, array $assoc ): void { | |
| 600 | + unset( $args ); | |
| 601 | + | |
| 602 | + $requested = array_values( | |
| 603 | + array_filter( | |
| 604 | + array_map( 'trim', explode( ',', (string) ( $assoc['type'] ?? 'all' ) ) ) | |
| 605 | + ) | |
| 606 | + ); | |
| 607 | + if ( ! $requested ) { | |
| 608 | + $requested = array( 'all' ); | |
| 609 | + } | |
| 610 | + | |
| 611 | + $accepted = \XSpeed\Purge_Runner::accepted_types(); | |
| 612 | + $unknown = array_diff( $requested, $accepted ); | |
| 613 | + if ( $unknown ) { | |
| 614 | + // Refuse before purging anything: a typo in --type must not | |
| 615 | + // quietly clear a DIFFERENT store than the one named. | |
| 616 | + \WP_CLI::error( | |
| 617 | + sprintf( | |
| 618 | + 'Unknown purge type: %s. Expected one of: %s', | |
| 619 | + implode( ', ', $unknown ), | |
| 620 | + implode( ', ', $accepted ) | |
| 621 | + ) | |
| 622 | + ); | |
| 623 | + return; | |
| 624 | + } | |
| 625 | + | |
| 626 | + $cause = isset( $assoc['cause'] ) && '' !== trim( (string) $assoc['cause'] ) ? trim( (string) $assoc['cause'] ) : 'CLI'; | |
| 627 | + $report = \XSpeed\Purge_Runner::run( $requested, $cause ); | |
| 628 | + | |
| 629 | + if ( 'json' === ( $assoc['format'] ?? 'table' ) ) { | |
| 630 | + // The report goes to STDOUT alone so `... --format=json | jq` works; | |
| 631 | + // the failure message goes to STDERR via ::error, which is also | |
| 632 | + // what produces the non-zero exit. | |
| 633 | + \WP_CLI::line( (string) wp_json_encode( $report ) ); | |
| 634 | + if ( ! $report['ok'] ) { | |
| 635 | + \WP_CLI::error( 'One or more cache stores failed to purge; see the report above.' ); | |
| 636 | + } | |
| 637 | + return; | |
| 638 | + } | |
| 639 | + | |
| 640 | + $cleared = 0; | |
| 641 | + $skipped = 0; | |
| 642 | + $failed = 0; | |
| 643 | + foreach ( $report['types'] as $row ) { | |
| 644 | + switch ( $row['status'] ) { | |
| 645 | + case \XSpeed\Purge_Runner::CLEARED: | |
| 646 | + ++$cleared; | |
| 647 | + \WP_CLI::log( sprintf( ' cleared %s%s', $row['label'], self::purge_amount( $row ) ) ); | |
| 648 | + break; | |
| 649 | + case \XSpeed\Purge_Runner::FAILED: | |
| 650 | + ++$failed; | |
| 651 | + \WP_CLI::log( sprintf( ' FAILED %s — %s', $row['label'], $row['reason'] ) ); | |
| 652 | + break; | |
| 653 | + default: | |
| 654 | + ++$skipped; | |
| 655 | + \WP_CLI::log( sprintf( ' skipped %s — %s', $row['label'], $row['reason'] ) ); | |
| 656 | + } | |
| 657 | + } | |
| 658 | + | |
| 659 | + if ( $failed ) { | |
| 660 | + \WP_CLI::error( | |
| 661 | + sprintf( | |
| 662 | + '%d of %d cache store(s) failed to purge; %d cleared, %d skipped.', | |
| 663 | + $failed, | |
| 664 | + count( $report['types'] ), | |
| 665 | + $cleared, | |
| 666 | + $skipped | |
| 667 | + ) | |
| 668 | + ); | |
| 669 | + return; | |
| 670 | + } | |
| 671 | + | |
| 672 | + if ( ! $cleared ) { | |
| 673 | + // Not a success banner: nothing was purged, and saying so is the | |
| 674 | + // honest answer for a --type nobody has configured. | |
| 675 | + \WP_CLI::log( sprintf( 'Nothing to purge — %d store(s) skipped.', $skipped ) ); | |
| 676 | + return; | |
| 677 | + } | |
| 678 | + | |
| 679 | + \WP_CLI::success( sprintf( 'Purged %d cache store(s); %d skipped.', $cleared, $skipped ) ); | |
| 680 | + } | |
| 681 | + | |
| 682 | + /** | |
| 683 | + * The " — 42 entries (1.3 MB)" tail on a cleared line. | |
| 684 | + * | |
| 685 | + * Entries and bytes are both optional: a Redis FLUSHALL reports neither, | |
| 686 | + * and printing "0 entries" for it would read as an empty cache rather | |
| 687 | + * than an uncountable one. | |
| 688 | + * | |
| 689 | + * @param array{entries:int|null,bytes:int|null} $row Report row. | |
| 690 | + */ | |
| 691 | + private static function purge_amount( array $row ): string { | |
| 692 | + $parts = array(); | |
| 693 | + if ( null !== $row['entries'] ) { | |
| 694 | + $parts[] = sprintf( '%d entr%s', $row['entries'], 1 === (int) $row['entries'] ? 'y' : 'ies' ); | |
| 695 | + } | |
| 696 | + if ( null !== $row['bytes'] && $row['bytes'] > 0 ) { | |
| 697 | + $parts[] = size_format( $row['bytes'], 1 ); | |
| 698 | + } | |
| 699 | + | |
| 700 | + return $parts ? ' — ' . implode( ', ', $parts ) : ''; | |
| 701 | + } | |
| 702 | + | |
| 453 | 703 | public function cli_handler( array $args, array $assoc ): void { |
| 454 | 704 | $action = isset( $args[0] ) ? (string) $args[0] : 'status'; |
| 455 | 705 | $limit = isset( $assoc['limit'] ) ? max( 1, (int) $assoc['limit'] ) : 20; |
| 456 | 706 | |
| @@ -629,13 +879,99 @@ | ||
| 629 | 879 | $this->cli_purge_log( $limit ); |
| 630 | 880 | return; |
| 631 | 881 | } |
| 632 | 882 | |
| 883 | + if ( 'edge' === $action ) { | |
| 884 | + $this->cli_edge(); | |
| 885 | + return; | |
| 886 | + } | |
| 887 | + | |
| 633 | 888 | $opts = Settings_Manager::get( self::SLUG ); |
| 634 | 889 | \WP_CLI::log( 'cache_expiry ' . $opts['cache_expiry'] . 'h' ); |
| 635 | 890 | \WP_CLI::log( 'excluded_urls ' . count( $opts['excluded_urls'] ) . ' entries' ); |
| 636 | 891 | foreach ( $opts['excluded_urls'] as $u ) { |
| 637 | 892 | \WP_CLI::log( ' - ' . $u ); |
| 893 | + } | |
| 894 | + $edge = \XSpeed\Edge_Provider::detect(); | |
| 895 | + \WP_CLI::log( 'edge ' . ( '' !== $edge['provider'] ? $edge['provider'] : $edge['confidence'] ) . ' (' . $edge['source'] . ')' ); | |
| 896 | + } | |
| 897 | + | |
| 898 | + /** | |
| 899 | + * `wp xspeed cache edge` — what we think is in front, and what we say to it. | |
| 900 | + * | |
| 901 | + * Worth printing even when nothing is held back. "You are behind | |
| 902 | + * Cloudflare, and a Cache Rule set to ignore origin headers overrides | |
| 903 | + * anything xSpeed sends" is the answer to a support question that | |
| 904 | + * otherwise costs someone a week, and it is true whether or not a hold | |
| 905 | + * ever fires. | |
| 906 | + */ | |
| 907 | + private function cli_edge(): void { | |
| 908 | + $answer = \XSpeed\Edge_Provider::detect(); | |
| 909 | + | |
| 910 | + \WP_CLI::log( 'provider ' . ( '' !== $answer['provider'] ? $answer['provider'] : '(none named)' ) ); | |
| 911 | + \WP_CLI::log( 'confidence ' . $answer['confidence'] ); | |
| 912 | + \WP_CLI::log( 'source ' . $answer['source'] ); | |
| 913 | + | |
| 914 | + // A pin outranks detection by design, so nothing re-checks it on the | |
| 915 | + // site's behalf. Saying the two disagree is the whole mechanism by | |
| 916 | + // which a site that changed CDN ever finds out. | |
| 917 | + $sniffed = \XSpeed\Edge_Provider::sniffed(); | |
| 918 | + if ( in_array( $answer['source'], array( 'setting', 'constant', 'filter' ), true ) | |
| 919 | + && '' !== $sniffed['provider'] | |
| 920 | + && $sniffed['provider'] !== $answer['provider'] ) { | |
| 921 | + \WP_CLI::warning( | |
| 922 | + sprintf( | |
| 923 | + 'This request looks like %s, but the provider is pinned to %s. If the site moved, change it — the pinned answer is also baked into the drop-in and the server rules.', | |
| 924 | + $sniffed['provider'], | |
| 925 | + '' !== $answer['provider'] ? $answer['provider'] : 'off' | |
| 926 | + ) | |
| 927 | + ); | |
| 928 | + } | |
| 929 | + | |
| 930 | + if ( \XSpeed\Edge_Provider::is_off( $answer ) ) { | |
| 931 | + \WP_CLI::log( '' ); | |
| 932 | + \WP_CLI::log( 'Nothing is sent: this is switched off.' ); | |
| 933 | + return; | |
| 934 | + } | |
| 935 | + | |
| 936 | + // Resolved through edge_headers_for() rather than straight off the | |
| 937 | + // provider, so this prints what the serve path would ACTUALLY send — | |
| 938 | + // including `X-XSpeed-Edge-Hold`, and including the evidence gate. | |
| 939 | + // Listing the provider's raw set ignored that gate and told operators | |
| 940 | + // a first render would be held on a site where it would not be. | |
| 941 | + // | |
| 942 | + // `bake`, not `request`. Two reasons, and the second one matters: | |
| 943 | + // this command answers for the site rather than for one response, and | |
| 944 | + // `request` fires `xspeed_edge_optimization_pending`, whose Pro | |
| 945 | + // listener resolves the CSS plan — which by its own description is | |
| 946 | + // what queues a build. A read-only command must not burn a build | |
| 947 | + // slot, quarantine an entry or purge a page just by being run, and | |
| 948 | + // under WP-CLI it would do all three against the home page. | |
| 949 | + $bypass = \XSpeed\Cache::edge_headers_for( 'BYPASS', 'bake', 'logged-in' ); | |
| 950 | + $miss = \XSpeed\Cache::edge_headers_for( 'MISS', 'bake' ); | |
| 951 | + | |
| 952 | + \WP_CLI::log( '' ); | |
| 953 | + \WP_CLI::log( 'On a page xSpeed refuses to cache (a cart, a logged-in view):' ); | |
| 954 | + foreach ( $bypass as $name => $value ) { | |
| 955 | + \WP_CLI::log( sprintf( ' %s: %s', $name, $value ) ); | |
| 956 | + } | |
| 957 | + | |
| 958 | + \WP_CLI::log( '' ); | |
| 959 | + if ( array() === $miss ) { | |
| 960 | + \WP_CLI::log( 'On a first render: nothing. A MISS is a performance hedge, so it is held only where a cache in front was detected — and none was. Name the provider in Cache In Front Of This Site to cover first renders too.' ); | |
| 961 | + } else { | |
| 962 | + \WP_CLI::log( 'On a first render:' ); | |
| 963 | + foreach ( $miss as $name => $value ) { | |
| 964 | + \WP_CLI::log( sprintf( ' %s: %s', $name, $value ) ); | |
| 965 | + } | |
| 966 | + } | |
| 967 | + | |
| 968 | + \WP_CLI::log( '' ); | |
| 969 | + \WP_CLI::log( 'X-XSpeed-Edge-Hold names why a response was held: bypass, bypass-shape, miss, mobile-split or pending. No header means nothing was held.' ); | |
| 970 | + | |
| 971 | + if ( 'cloudflare' === $answer['provider'] ) { | |
| 972 | + \WP_CLI::log( '' ); | |
| 973 | + \WP_CLI::log( 'A Cloudflare Cache Rule whose Edge TTL is "Ignore cache-control header and use this TTL" overrides all of the above. Use "Respect origin TTL" on that rule if pages are still being stored.' ); | |
| 638 | 974 | } |
| 639 | 975 | } |
| 640 | 976 | |
| 641 | 977 | /** `wp xspeed cache inventory [--limit=N]` — which pages are cached, and how old. */ |