| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cache module. |
| 4 |
* |
| 5 |
* Owns the cache_expiry and excluded_urls settings. cache_enabled is |
| 6 |
* deliberately NOT in this schema — flipping it triggers the |
| 7 |
* advanced-cache.php drop-in install + WP_CACHE constant edit in |
| 8 |
* wp-config.php, which is a sensitive single-purpose code path and lives |
| 9 |
* in Cache::toggle() with its own dedicated /xspeed/v1/cache/toggle REST |
| 10 |
* route. The dashboard's Cache page renders the special hero UI for it |
| 11 |
* above this module's schema-driven settings panel. |
| 12 |
* |
| 13 |
* Tier: Free. |
| 14 |
* |
| 15 |
* @package XSpeed |
| 16 |
*/ |
| 17 |
|
| 18 |
declare(strict_types=1); |
| 19 |
|
| 20 |
namespace XSpeed\Modules\Cache; |
| 21 |
|
| 22 |
defined( 'ABSPATH' ) || exit; |
| 23 |
|
| 24 |
use XSpeed\Module; |
| 25 |
use XSpeed\Settings_Manager; |
| 26 |
|
| 27 |
final class CacheModule extends Module { |
| 28 |
|
| 29 |
public const SLUG = 'cache'; |
| 30 |
public const TIER = self::TIER_FREE; |
| 31 |
public const VERSION = '1.0.0'; |
| 32 |
|
| 33 |
public function ui_metadata(): array { |
| 34 |
return array( |
| 35 |
'label' => 'Page Cache', |
| 36 |
'icon' => 'Database', |
| 37 |
'description' => 'Page caching for non-logged-in visitors.', |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
public function settings_schema(): array { |
| 42 |
return array( |
| 43 |
'cache_expiry' => array( |
| 44 |
'type' => 'int', |
| 45 |
'default' => 24, |
| 46 |
'min' => 1, |
| 47 |
'max' => 720, |
| 48 |
'label' => 'Cache Expiry (hours)', |
| 49 |
'description' => 'How long cached pages live before regenerating. 1 to 720 hours (30 days).', |
| 50 |
), |
| 51 |
'excluded_urls' => array( |
| 52 |
'type' => 'list', |
| 53 |
// Comprehensive LiteSpeed / WP Rocket-parity default URL |
| 54 |
// exclusions (FBS-82181). Plain text = "contains", glob via |
| 55 |
// * ? [ ], or a `~` prefix for raw regex (e.g. ~wp-.*\.php). |
| 56 |
'default' => array( |
| 57 |
'/wp-admin/', |
| 58 |
'/wp-json/', |
| 59 |
'/xmlrpc.php', |
| 60 |
'~wp-.*\.php', |
| 61 |
'/feed/', |
| 62 |
'index.php', |
| 63 |
'~sitemap(_index)?\.xml', |
| 64 |
// Bare (no trailing slash) so "contains" matches both |
| 65 |
// /cart and /cart/items — WooCommerce serves both forms. |
| 66 |
'/cart', |
| 67 |
'/checkout', |
| 68 |
'/my-account', |
| 69 |
'ao_noptirocket', |
| 70 |
'ao_speedup_cachebuster', |
| 71 |
'removed_item', |
| 72 |
'/wc-api', |
| 73 |
'/edd-api', |
| 74 |
'/wp-login', |
| 75 |
), |
| 76 |
'item_type' => 'string', |
| 77 |
'label' => 'Excluded URLs', |
| 78 |
'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).', |
| 79 |
), |
| 80 |
'excluded_cookies' => array( |
| 81 |
'type' => 'list', |
| 82 |
// Cookies that signal a logged-in / transactional visitor |
| 83 |
// whose response must not be served from a shared cache. |
| 84 |
// `~` prefix = raw regex (e.g. ~wordpress_[a-f0-9]+). (FBS-82181) |
| 85 |
'default' => array( |
| 86 |
'comment_author', |
| 87 |
'~wordpress_[a-f0-9]+', |
| 88 |
'wp-postpass', |
| 89 |
'wordpress_no_cache', |
| 90 |
'wordpress_logged_in', |
| 91 |
'edd_items_in_cart', |
| 92 |
'woocommerce_items_in_cart', |
| 93 |
'fct_cart_hash', |
| 94 |
'comment_', |
| 95 |
'woocommerce_', |
| 96 |
'wordpress', |
| 97 |
'xf_', |
| 98 |
'edd_', |
| 99 |
'jetpack', |
| 100 |
'yith_wcwl_session_', |
| 101 |
'yith_wrvp_', |
| 102 |
'wpsc_', |
| 103 |
'ecwid', |
| 104 |
'ec_', |
| 105 |
'bookly', |
| 106 |
), |
| 107 |
'item_type' => 'string', |
| 108 |
'label' => 'Excluded Cookies', |
| 109 |
'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.', |
| 110 |
), |
| 111 |
'bypass_user_agents' => array( |
| 112 |
'type' => 'list', |
| 113 |
'default' => array(), |
| 114 |
'item_type' => 'string', |
| 115 |
'label' => 'Bypass User Agents', |
| 116 |
'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.', |
| 117 |
), |
| 118 |
'ignored_query_params' => array( |
| 119 |
'type' => 'list', |
| 120 |
// Analytics / ad / session query keys stripped before the |
| 121 |
// cache key is computed, so /post?utm_source=x and /post |
| 122 |
// share one entry. `~` prefix = raw regex. (FBS-82181) |
| 123 |
'default' => array( |
| 124 |
'__s', |
| 125 |
'_ga', |
| 126 |
'_ke', |
| 127 |
'~[a-zA-Z0-9_-]+_sid', |
| 128 |
'adgroupid', |
| 129 |
'age-verified', |
| 130 |
'ao_noptimize', |
| 131 |
'campaignid', |
| 132 |
'ck_subscriber_id', |
| 133 |
'cn-reloaded', |
| 134 |
'dclid', |
| 135 |
'epik', |
| 136 |
'fb_action_ids', |
| 137 |
'fb_action_types', |
| 138 |
'fb_source', |
| 139 |
'fbclid', |
| 140 |
'gclid', |
| 141 |
'jobid', |
| 142 |
'mc_cid', |
| 143 |
'mc_eid', |
| 144 |
'mkt_tok', |
| 145 |
'msclkid', |
| 146 |
'ref', |
| 147 |
'~session_[a-zA-Z0-9_-]+_alive', |
| 148 |
'sseid', |
| 149 |
'sslid', |
| 150 |
'usqp', |
| 151 |
'~utm_[a-zA-Z0-9_-]+', |
| 152 |
), |
| 153 |
'item_type' => 'string', |
| 154 |
'label' => 'Ignored Query Parameters', |
| 155 |
'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. Glob + ~regex supported. One per line.', |
| 156 |
), |
| 157 |
'mobile_separate' => array( |
| 158 |
'type' => 'bool', |
| 159 |
'default' => false, |
| 160 |
'label' => 'Separate Mobile Cache', |
| 161 |
'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.', |
| 162 |
), |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Seed per-module option from the legacy xspeed_options blob if we |
| 168 |
* haven't done so yet. Idempotent — once xspeed_module_cache exists |
| 169 |
* or the legacy keys are gone, this is a no-op. Runs on both boot |
| 170 |
* and activate so installs on every code path are covered. |
| 171 |
*/ |
| 172 |
public function boot(): void { |
| 173 |
$this->seed_from_legacy_if_needed(); |
| 174 |
|
| 175 |
// Keep every mobile_separate-dependent artifact (the drop-in's |
| 176 |
// `.mobile-separate` flag, the device-blind server rewrite, and the |
| 177 |
// device-keyed caches) in lockstep with the setting — on boot, and |
| 178 |
// whenever the cache settings are saved. The drop-in can't read WP |
| 179 |
// options, so it reads the sidecar marker Cache maintains here. |
| 180 |
\XSpeed\Cache::reconcile_mobile_separate(); |
| 181 |
add_action( |
| 182 |
'update_option_xspeed_module_cache', |
| 183 |
static function () { |
| 184 |
\XSpeed\Cache::reconcile_mobile_separate(); |
| 185 |
} |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
public function activate(): void { |
| 190 |
$this->seed_from_legacy_if_needed(); |
| 191 |
} |
| 192 |
|
| 193 |
private function seed_from_legacy_if_needed(): void { |
| 194 |
if ( null !== get_option( 'xspeed_module_cache', null ) ) { |
| 195 |
return; |
| 196 |
} |
| 197 |
$legacy = get_option( 'xspeed_options', array() ); |
| 198 |
if ( ! is_array( $legacy ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
$seed = array( '_version' => self::VERSION ); |
| 202 |
$dirty = false; |
| 203 |
if ( array_key_exists( 'cache_expiry', $legacy ) ) { |
| 204 |
$seed['cache_expiry'] = max( 1, min( 720, (int) $legacy['cache_expiry'] ) ); |
| 205 |
unset( $legacy['cache_expiry'] ); |
| 206 |
$dirty = true; |
| 207 |
} |
| 208 |
if ( array_key_exists( 'excluded_urls', $legacy ) ) { |
| 209 |
$seed['excluded_urls'] = is_array( $legacy['excluded_urls'] ) ? array_values( array_filter( $legacy['excluded_urls'], 'is_string' ) ) : array(); |
| 210 |
unset( $legacy['excluded_urls'] ); |
| 211 |
$dirty = true; |
| 212 |
} |
| 213 |
if ( $dirty ) { |
| 214 |
update_option( 'xspeed_module_cache', $seed ); |
| 215 |
update_option( 'xspeed_options', $legacy ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
public function cli_commands(): array { |
| 220 |
return array( |
| 221 |
array( |
| 222 |
'name' => 'xspeed cache', |
| 223 |
'callback' => array( $this, 'cli_handler' ), |
| 224 |
'shortdesc' => 'Inspect Cache module settings (purge / toggle use the dedicated commands).', |
| 225 |
'synopsis' => array( |
| 226 |
array( |
| 227 |
'type' => 'positional', |
| 228 |
'name' => 'action', |
| 229 |
'options' => array( 'status' ), |
| 230 |
'optional' => true, |
| 231 |
), |
| 232 |
), |
| 233 |
), |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
public function cli_handler( array $args, array $assoc ): void { |
| 238 |
$opts = Settings_Manager::get( self::SLUG ); |
| 239 |
\WP_CLI::log( 'cache_expiry ' . $opts['cache_expiry'] . 'h' ); |
| 240 |
\WP_CLI::log( 'excluded_urls ' . count( $opts['excluded_urls'] ) . ' entries' ); |
| 241 |
foreach ( $opts['excluded_urls'] as $u ) { |
| 242 |
\WP_CLI::log( ' - ' . $u ); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Static-rewrite directives for the unified nginx server-block |
| 248 |
* snippet. Returns null when cache is disabled — there's no rewrite |
| 249 |
* to install in that state. Delegates to \XSpeed\Cache::nginx_snippet() |
| 250 |
* which already produces nginx-detection-gated output. |
| 251 |
*/ |
| 252 |
public function nginx_directives(): ?string { |
| 253 |
$opts = get_option( 'xspeed_options', array() ); |
| 254 |
if ( empty( $opts['cache_enabled'] ) ) { |
| 255 |
return null; |
| 256 |
} |
| 257 |
return \XSpeed\Cache::nginx_snippet(); |
| 258 |
} |
| 259 |
} |
| 260 |
|