| 1 |
<?php |
| 2 |
/** |
| 3 |
* Migration — read settings from other popular caching plugins and |
| 4 |
* translate them into xSpeed equivalents. |
| 5 |
* |
| 6 |
* Each source plugin has its own importer that returns a single |
| 7 |
* normalized patch shape: |
| 8 |
* |
| 9 |
* array<string,array> // module-slug → settings patch |
| 10 |
* |
| 11 |
* which feeds straight into update_option('xspeed_module_<slug>') |
| 12 |
* via the same write path the Recommendations module uses. |
| 13 |
* |
| 14 |
* Importers are pure: detect() reads the options table, returns |
| 15 |
* what it found (or null if the source plugin's options aren't |
| 16 |
* present). plan() turns that raw read into the patch. apply() |
| 17 |
* writes it. preview() returns plan() without writing — used by |
| 18 |
* the React panel for the "what would import" diff view. |
| 19 |
* |
| 20 |
* Adding a new source = adding a private detect_*() + plan_*() |
| 21 |
* pair, then wiring them in `sources()`. |
| 22 |
* |
| 23 |
* @package XSpeed |
| 24 |
*/ |
| 25 |
|
| 26 |
declare(strict_types=1); |
| 27 |
|
| 28 |
namespace XSpeed; |
| 29 |
|
| 30 |
defined( 'ABSPATH' ) || exit; |
| 31 |
|
| 32 |
final class Migration { |
| 33 |
|
| 34 |
/** |
| 35 |
* Public list of source plugins with metadata for the UI: |
| 36 |
* [ id => [ label, detect_cb, plan_cb ] ] |
| 37 |
*/ |
| 38 |
public static function sources(): array { |
| 39 |
return array( |
| 40 |
'wp-rocket' => array( |
| 41 |
'label' => 'WP Rocket', |
| 42 |
'detect' => array( __CLASS__, 'detect_wp_rocket' ), |
| 43 |
'plan' => array( __CLASS__, 'plan_wp_rocket' ), |
| 44 |
), |
| 45 |
'w3-total-cache' => array( |
| 46 |
'label' => 'W3 Total Cache', |
| 47 |
'detect' => array( __CLASS__, 'detect_w3tc' ), |
| 48 |
'plan' => array( __CLASS__, 'plan_w3tc' ), |
| 49 |
), |
| 50 |
'wp-super-cache' => array( |
| 51 |
'label' => 'WP Super Cache', |
| 52 |
'detect' => array( __CLASS__, 'detect_wpsc' ), |
| 53 |
'plan' => array( __CLASS__, 'plan_wpsc' ), |
| 54 |
), |
| 55 |
'litespeed-cache' => array( |
| 56 |
'label' => 'LiteSpeed Cache', |
| 57 |
'detect' => array( __CLASS__, 'detect_litespeed' ), |
| 58 |
'plan' => array( __CLASS__, 'plan_litespeed' ), |
| 59 |
), |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** Site option holding the list of source ids already imported. */ |
| 64 |
private const IMPORTED_OPTION = 'xspeed_migration_imported'; |
| 65 |
|
| 66 |
/** Source id → plugin file, for the active-state check. */ |
| 67 |
private const PLUGIN_FILE = array( |
| 68 |
'wp-rocket' => 'wp-rocket/wp-rocket.php', |
| 69 |
'w3-total-cache' => 'w3-total-cache/w3-total-cache.php', |
| 70 |
'wp-super-cache' => 'wp-super-cache/wp-cache.php', |
| 71 |
'litespeed-cache' => 'litespeed-cache/litespeed-cache.php', |
| 72 |
); |
| 73 |
|
| 74 |
/** Record a source as imported (idempotent). */ |
| 75 |
public static function mark_imported( string $id ): void { |
| 76 |
$done = (array) get_option( self::IMPORTED_OPTION, array() ); |
| 77 |
if ( ! in_array( $id, $done, true ) ) { |
| 78 |
$done[] = $id; |
| 79 |
update_option( self::IMPORTED_OPTION, array_values( $done ) ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* For each source, return { id, label, detected, value_count, mapped_count, |
| 85 |
* imported, active }. |
| 86 |
* - `detected` true when the source plugin's settings are present. |
| 87 |
* - `value_count` raw number of keys in the source's own config — NOT |
| 88 |
* how many we import; kept for diagnostics only. |
| 89 |
* - `mapped_count` how many settings the importer ACTUALLY writes into |
| 90 |
* xSpeed (the honest number to show users). |
| 91 |
* - `imported` true once this source has been imported (so the panel |
| 92 |
* shows it as done, not as a fresh Import target). |
| 93 |
* - `active` whether the source plugin is still active. |
| 94 |
*/ |
| 95 |
public static function status(): array { |
| 96 |
$imported = (array) get_option( self::IMPORTED_OPTION, array() ); |
| 97 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 98 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 99 |
} |
| 100 |
$out = array(); |
| 101 |
foreach ( self::sources() as $id => $spec ) { |
| 102 |
$raw = call_user_func( $spec['detect'] ); |
| 103 |
$detected = is_array( $raw ); |
| 104 |
$mapped = 0; |
| 105 |
if ( $detected ) { |
| 106 |
$patch = call_user_func( $spec['plan'], $raw ); |
| 107 |
if ( is_array( $patch ) ) { |
| 108 |
$mapped = self::count_meaningful( $patch ); |
| 109 |
} |
| 110 |
} |
| 111 |
$file = self::PLUGIN_FILE[ $id ] ?? ''; |
| 112 |
$out[] = array( |
| 113 |
'id' => $id, |
| 114 |
'label' => $spec['label'], |
| 115 |
'detected' => $detected, |
| 116 |
'value_count' => $detected ? count( $raw ) : 0, |
| 117 |
'mapped_count' => $mapped, |
| 118 |
'imported' => in_array( $id, $imported, true ), |
| 119 |
'active' => '' !== $file && is_plugin_active( $file ), |
| 120 |
); |
| 121 |
} |
| 122 |
return $out; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Count the settings in a plan patch that will MEANINGFULLY change the |
| 127 |
* config — i.e. the ones actually enabled / non-empty in the source. |
| 128 |
* |
| 129 |
* A `plan_*` patch always emits every mapped field, including the ones the |
| 130 |
* source has turned OFF (`false`) or left empty. Counting those inflated |
| 131 |
* the "settings available to migrate" number — a source with 2 settings on |
| 132 |
* still reported ~12 because the importer listed every false mapping. |
| 133 |
* Disabled (`false`) booleans and empty arrays/strings/zeros contribute |
| 134 |
* nothing on import, so they're excluded from the count. (FBS-82449) |
| 135 |
* |
| 136 |
* @param array<string,mixed> $patch Plan patch (module slug => values). |
| 137 |
* @return int Number of enabled / non-empty settings. |
| 138 |
*/ |
| 139 |
private static function count_meaningful( array $patch ): int { |
| 140 |
$count = 0; |
| 141 |
foreach ( $patch as $vals ) { |
| 142 |
if ( is_array( $vals ) ) { |
| 143 |
$count += count( self::meaningful_values( $vals ) ); |
| 144 |
} |
| 145 |
} |
| 146 |
return $count; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Filter one module's plan values down to the ones that meaningfully change |
| 151 |
* the config: enabled (`true`) booleans, non-empty arrays, and non-empty |
| 152 |
* scalars. Disabled toggles, empty lists, and zero/empty scalars are |
| 153 |
* dropped — they represent "nothing to import" for that setting. Shared by |
| 154 |
* the count (status) and the write (apply) so both agree. (FBS-82449) |
| 155 |
* |
| 156 |
* @param array<string,mixed> $values One module's mapped values. |
| 157 |
* @return array<string,mixed> Only the meaningful entries. |
| 158 |
*/ |
| 159 |
private static function meaningful_values( array $values ): array { |
| 160 |
$out = array(); |
| 161 |
foreach ( $values as $key => $value ) { |
| 162 |
if ( is_bool( $value ) ) { |
| 163 |
if ( $value ) { |
| 164 |
$out[ $key ] = $value; // Only an enabled toggle imports. |
| 165 |
} |
| 166 |
} elseif ( is_array( $value ) ) { |
| 167 |
if ( ! empty( $value ) ) { |
| 168 |
$out[ $key ] = $value; // Non-empty list (e.g. excluded_urls). |
| 169 |
} |
| 170 |
} elseif ( '' !== $value && null !== $value && 0 !== $value && '0' !== $value ) { |
| 171 |
$out[ $key ] = $value; // Non-empty scalar (e.g. cache_expiry). |
| 172 |
} |
| 173 |
} |
| 174 |
return $out; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Map a source plugin's "separate mobile cache" flag onto the cache patch |
| 179 |
* WITHOUT enabling xSpeed's mobile_separate. The xSpeed static-file fast |
| 180 |
* path is device-blind, so mobile_separate=ON disables it — and a source |
| 181 |
* site that had the flag on very often serves identical HTML to every |
| 182 |
* device (it was on by habit). Rather than silently kill the fast path on |
| 183 |
* import, we keep mobile_separate off and, when the source had it on, set |
| 184 |
* `mobile_separate_review` so the dashboard can prompt the user to turn it |
| 185 |
* back on only if their site really differs per device. |
| 186 |
* (FBS-83144 / FBS-83145) |
| 187 |
* |
| 188 |
* @param array $patch The plan patch (modified by reference). |
| 189 |
* @param bool $source_on Whether the source plugin had mobile-separate on. |
| 190 |
*/ |
| 191 |
private static function map_mobile_separate( array &$patch, bool $source_on ): void { |
| 192 |
if ( ! isset( $patch['cache'] ) || ! is_array( $patch['cache'] ) ) { |
| 193 |
$patch['cache'] = array(); |
| 194 |
} |
| 195 |
// Never import as ON; leave the fast path intact. |
| 196 |
$patch['cache']['mobile_separate'] = false; |
| 197 |
if ( $source_on ) { |
| 198 |
$patch['cache']['mobile_separate_review'] = true; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Return the patch that `apply()` would write, without writing. |
| 204 |
*/ |
| 205 |
public static function preview( string $source_id ): ?array { |
| 206 |
$src = self::sources()[ $source_id ] ?? null; |
| 207 |
if ( null === $src ) { |
| 208 |
return null; |
| 209 |
} |
| 210 |
$raw = call_user_func( $src['detect'] ); |
| 211 |
if ( ! is_array( $raw ) ) { |
| 212 |
return null; |
| 213 |
} |
| 214 |
return call_user_func( $src['plan'], $raw ); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Read source settings + write the translated patch. Returns the |
| 219 |
* per-module write results, same shape as Recommendations::apply. |
| 220 |
*/ |
| 221 |
public static function apply( string $source_id ): array { |
| 222 |
$patch = self::preview( $source_id ); |
| 223 |
if ( null === $patch ) { |
| 224 |
return array(); |
| 225 |
} |
| 226 |
$results = array(); |
| 227 |
foreach ( $patch as $slug => $values ) { |
| 228 |
if ( ! is_string( $slug ) || ! is_array( $values ) ) { |
| 229 |
continue; |
| 230 |
} |
| 231 |
// Import only the settings that are actually enabled / non-empty in |
| 232 |
// the source. A plan patch emits every mapped field including the |
| 233 |
// ones the source turned OFF; merging those `false`/empty values |
| 234 |
// would silently DISABLE settings the user already had on in xSpeed. |
| 235 |
// Migration is additive — it never clobbers existing config with a |
| 236 |
// source's disabled value. (FBS-82449) |
| 237 |
$meaningful = self::meaningful_values( $values ); |
| 238 |
if ( empty( $meaningful ) ) { |
| 239 |
continue; |
| 240 |
} |
| 241 |
$option = 'xspeed_module_' . $slug; |
| 242 |
$cur = (array) get_option( $option, array() ); |
| 243 |
$next = array_merge( $cur, $meaningful ); |
| 244 |
$ok = update_option( $option, $next ); |
| 245 |
$results[ $slug ] = array( |
| 246 |
'ok' => (bool) $ok, |
| 247 |
'applied' => array_keys( $meaningful ), |
| 248 |
); |
| 249 |
} |
| 250 |
if ( ! empty( $results ) ) { |
| 251 |
self::mark_imported( $source_id ); |
| 252 |
} |
| 253 |
return $results; |
| 254 |
} |
| 255 |
|
| 256 |
// ─────────────────────────── WP Rocket ─────────────────────────── |
| 257 |
|
| 258 |
public static function detect_wp_rocket(): ?array { |
| 259 |
$opt = get_option( 'wp_rocket_settings', null ); |
| 260 |
return is_array( $opt ) ? $opt : null; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Translate WP Rocket's `wp_rocket_settings` array into our module |
| 265 |
* settings. Only safe-to-port booleans + counts; behaviorally |
| 266 |
* different toggles (Critical CSS, RUCSS) skip — Pro handles those. |
| 267 |
* |
| 268 |
* @param array $r raw wp_rocket_settings. |
| 269 |
*/ |
| 270 |
public static function plan_wp_rocket( array $r ): array { |
| 271 |
$patch = array(); |
| 272 |
// Page caching. |
| 273 |
$patch['cache'] = array( |
| 274 |
'enabled' => ! empty( $r['cache_logged_user'] ) || ! isset( $r['cache_logged_user'] ), |
| 275 |
// The Cache module's TTL setting is `cache_expiry` (hours), NOT |
| 276 |
// `expiry_hours` — the latter is a dead key nothing reads, so the |
| 277 |
// imported lifetime was silently dropped. Clamp to the same 1–720h |
| 278 |
// range the Cache schema + LiteSpeed importer use. (FBS-83144) |
| 279 |
'cache_expiry' => isset( $r['purge_cron_interval'] ) ? max( 1, min( 720, (int) ( (int) $r['purge_cron_interval'] / 3600 ) ) ) : 24, |
| 280 |
); |
| 281 |
// Excluded URLs / cookies — both are arrays of strings in WP Rocket. |
| 282 |
if ( ! empty( $r['cache_reject_uri'] ) && is_array( $r['cache_reject_uri'] ) ) { |
| 283 |
$patch['cache']['excluded_urls'] = array_values( array_filter( array_map( 'strval', $r['cache_reject_uri'] ) ) ); |
| 284 |
} |
| 285 |
if ( ! empty( $r['cache_reject_cookies'] ) && is_array( $r['cache_reject_cookies'] ) ) { |
| 286 |
$patch['cache']['excluded_cookies'] = array_values( array_filter( array_map( 'strval', $r['cache_reject_cookies'] ) ) ); |
| 287 |
} |
| 288 |
|
| 289 |
// Minify. |
| 290 |
$patch['minify'] = array( |
| 291 |
'minify_html' => ! empty( $r['minify_html'] ), |
| 292 |
'minify_css' => ! empty( $r['minify_css'] ), |
| 293 |
'minify_js' => ! empty( $r['minify_js'] ), |
| 294 |
'combine_css' => ! empty( $r['minify_concatenate_css'] ), |
| 295 |
'combine_js' => ! empty( $r['minify_concatenate_js'] ), |
| 296 |
'defer_js' => ! empty( $r['defer_all_js'] ), |
| 297 |
); |
| 298 |
|
| 299 |
// Lazy load. |
| 300 |
$patch['lazy'] = array( |
| 301 |
'lazy_images' => ! empty( $r['lazyload'] ), |
| 302 |
'lazy_iframes' => ! empty( $r['lazyload_iframes'] ), |
| 303 |
'lazy_videos' => ! empty( $r['lazyload_youtube'] ), |
| 304 |
); |
| 305 |
|
| 306 |
// Separate Mobile Cache — do NOT import this as ON. WP Rocket's |
| 307 |
// "separate cache files for mobile" is frequently left on by habit even |
| 308 |
// when the site serves identical HTML to every device, and xSpeed's |
| 309 |
// static-file fast path is device-blind — enabling mobile_separate |
| 310 |
// DISABLES it, silently dropping the site from HIT (nginx) to HIT (php). |
| 311 |
// Instead, keep the fast path (mobile_separate stays false) and flag it |
| 312 |
// for review so the dashboard can prompt the user to re-enable it only |
| 313 |
// if their site really differs per device. (FBS-83144 / FBS-83145) |
| 314 |
self::map_mobile_separate( $patch, ! empty( $r['do_caching_mobile_files'] ) ); |
| 315 |
|
| 316 |
// Preloader. |
| 317 |
if ( ! empty( $r['manual_preload'] ) || ! empty( $r['sitemap_preload'] ) ) { |
| 318 |
$patch['preloader'] = array( |
| 319 |
'enabled' => true, |
| 320 |
'schedule' => 'daily', |
| 321 |
); |
| 322 |
if ( ! empty( $r['sitemap_preload_url'] ) && is_array( $r['sitemap_preload_url'] ) ) { |
| 323 |
$patch['preloader']['sitemap_urls'] = array_values( array_filter( array_map( 'strval', $r['sitemap_preload_url'] ) ) ); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
// CDN — WP Rocket stores CDN hosts in cdn_cnames (array). |
| 328 |
if ( ! empty( $r['cdn'] ) && ! empty( $r['cdn_cnames'] ) && is_array( $r['cdn_cnames'] ) ) { |
| 329 |
$first = (string) ( $r['cdn_cnames'][0] ?? '' ); |
| 330 |
if ( '' !== $first ) { |
| 331 |
$patch['cdn'] = array( |
| 332 |
'enabled' => true, |
| 333 |
'cdn_url' => $first, |
| 334 |
); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
return $patch; |
| 339 |
} |
| 340 |
|
| 341 |
// ─────────────────────────── W3 Total Cache ────────────────────── |
| 342 |
|
| 343 |
public static function detect_w3tc(): ?array { |
| 344 |
// W3 Total Cache does NOT store its config in the options table — it |
| 345 |
// writes a PHP file at wp-content/w3tc-config/master.php whose body |
| 346 |
// is a short PHP guard followed by a JSON blob of dotted-key settings |
| 347 |
// (pgcache.enabled, minify.html.enable, …). Reading w3tc_config / |
| 348 |
// w3tc_master_settings options always returned null, so detection |
| 349 |
// failed on every install. Read + parse the config file instead. |
| 350 |
$cfg = self::read_w3tc_config_file(); |
| 351 |
if ( is_array( $cfg ) && ! empty( $cfg ) ) { |
| 352 |
return $cfg; |
| 353 |
} |
| 354 |
// Defensive fallback for any build that did persist an options blob. |
| 355 |
$opt = get_option( 'w3tc_config', null ); |
| 356 |
if ( ! is_array( $opt ) ) { |
| 357 |
$opt = get_option( 'w3tc_master_settings', null ); |
| 358 |
} |
| 359 |
return is_array( $opt ) ? $opt : null; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Parse W3TC's master config file into a flat dotted-key array. |
| 364 |
* Format: a short PHP guard (a php-open, exit, php-close) immediately |
| 365 |
* followed by a JSON object. We strip everything up to and including the |
| 366 |
* PHP closing tag, then JSON-decode the remainder. |
| 367 |
* |
| 368 |
* @return array|null parsed config, or null if the file is missing/unreadable. |
| 369 |
*/ |
| 370 |
private static function read_w3tc_config_file(): ?array { |
| 371 |
if ( ! defined( 'WP_CONTENT_DIR' ) ) { |
| 372 |
return null; |
| 373 |
} |
| 374 |
$path = WP_CONTENT_DIR . '/w3tc-config/master.php'; |
| 375 |
if ( ! is_readable( $path ) ) { |
| 376 |
return null; |
| 377 |
} |
| 378 |
$raw = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- reading another plugin's local config file; WP_Filesystem is overkill for a one-shot read. |
| 379 |
if ( false === $raw || '' === $raw ) { |
| 380 |
return null; |
| 381 |
} |
| 382 |
// Drop the leading PHP guard and decode the JSON tail. The pattern |
| 383 |
// matches up to the first PHP closing tag; built from a char-code so |
| 384 |
// no literal close tag appears in this source file. |
| 385 |
$close_tag = '?' . '>'; |
| 386 |
$json = preg_replace( '/^.*?' . preg_quote( $close_tag, '/' ) . '/s', '', $raw ); |
| 387 |
$cfg = json_decode( trim( (string) $json ), true ); |
| 388 |
return is_array( $cfg ) ? $cfg : null; |
| 389 |
} |
| 390 |
|
| 391 |
public static function plan_w3tc( array $r ): array { |
| 392 |
$patch = array(); |
| 393 |
$patch['cache'] = array( |
| 394 |
'enabled' => ! empty( $r['pgcache.enabled'] ), |
| 395 |
// Cache module reads `cache_expiry` (hours), not the dead |
| 396 |
// `expiry_hours` key — see plan_wp_rocket. (FBS-83144) |
| 397 |
'cache_expiry' => isset( $r['pgcache.lifetime'] ) ? max( 1, min( 720, (int) ( (int) $r['pgcache.lifetime'] / 3600 ) ) ) : 24, |
| 398 |
); |
| 399 |
if ( ! empty( $r['pgcache.reject.uri'] ) && is_array( $r['pgcache.reject.uri'] ) ) { |
| 400 |
$patch['cache']['excluded_urls'] = array_values( array_filter( array_map( 'strval', $r['pgcache.reject.uri'] ) ) ); |
| 401 |
} |
| 402 |
|
| 403 |
$patch['minify'] = array( |
| 404 |
'minify_html' => ! empty( $r['minify.html.enable'] ), |
| 405 |
'minify_css' => ! empty( $r['minify.css.enable'] ), |
| 406 |
'minify_js' => ! empty( $r['minify.js.enable'] ), |
| 407 |
); |
| 408 |
|
| 409 |
if ( ! empty( $r['objectcache.enabled'] ) && ! empty( $r['objectcache.engine'] ) ) { |
| 410 |
$patch['object-cache'] = array( |
| 411 |
'backend' => 'memcached' === $r['objectcache.engine'] ? 'memcached' : 'redis', |
| 412 |
); |
| 413 |
if ( ! empty( $r['objectcache.servers'] ) && is_array( $r['objectcache.servers'] ) ) { |
| 414 |
$first = (string) ( $r['objectcache.servers'][0] ?? '' ); |
| 415 |
if ( false !== strpos( $first, ':' ) ) { |
| 416 |
[ $host, $port ] = explode( ':', $first, 2 ); |
| 417 |
if ( 'redis' === ( $patch['object-cache']['backend'] ?? '' ) ) { |
| 418 |
$patch['object-cache']['redis_host'] = $host; |
| 419 |
$patch['object-cache']['redis_port'] = (int) $port; |
| 420 |
} else { |
| 421 |
$patch['object-cache']['memcached_host'] = $host; |
| 422 |
$patch['object-cache']['memcached_port'] = (int) $port; |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
if ( ! empty( $r['browsercache.enabled'] ) ) { |
| 429 |
$patch['browser-cache'] = array( |
| 430 |
'enabled' => true, |
| 431 |
); |
| 432 |
} |
| 433 |
|
| 434 |
return $patch; |
| 435 |
} |
| 436 |
|
| 437 |
// ─────────────────────────── WP Super Cache ────────────────────── |
| 438 |
|
| 439 |
public static function detect_wpsc(): ?array { |
| 440 |
// WP Super Cache stores its settings as PHP globals in |
| 441 |
// wp-content/wp-cache-config.php (NOT the options table — the old |
| 442 |
// get_option('wp_cache_enabled') reads always returned null). Parse |
| 443 |
// the config file for the globals plan_wpsc() needs. If the file |
| 444 |
// doesn't exist yet (plugin active but never configured), fall back |
| 445 |
// to a minimal "active" marker so the source still appears in the UI |
| 446 |
// and a default import is possible. |
| 447 |
$cfg = self::read_wpsc_config_file(); |
| 448 |
if ( is_array( $cfg ) && ! empty( $cfg ) ) { |
| 449 |
return $cfg; |
| 450 |
} |
| 451 |
if ( self::plugin_active( 'wp-super-cache/wp-cache.php' ) ) { |
| 452 |
// Active but unconfigured — expose the on/off intent only. |
| 453 |
return array( 'wp_cache_enabled' => defined( 'WPCACHEHOME' ) ); |
| 454 |
} |
| 455 |
return null; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Parse the WP Super Cache config file for the globals we map. The file |
| 460 |
* is plain PHP assigning `$wp_cache_* = …;` lines; we extract them with a |
| 461 |
* regex rather than including the file (including it would define |
| 462 |
* constants / run code in our request). |
| 463 |
* |
| 464 |
* @return array|null name => value for the recognised globals, or null. |
| 465 |
*/ |
| 466 |
private static function read_wpsc_config_file(): ?array { |
| 467 |
if ( ! defined( 'WP_CONTENT_DIR' ) ) { |
| 468 |
return null; |
| 469 |
} |
| 470 |
$path = WP_CONTENT_DIR . '/wp-cache-config.php'; |
| 471 |
if ( ! is_readable( $path ) ) { |
| 472 |
return null; |
| 473 |
} |
| 474 |
$raw = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- one-shot read of another plugin's config file. |
| 475 |
if ( false === $raw || '' === $raw ) { |
| 476 |
return null; |
| 477 |
} |
| 478 |
$out = array(); |
| 479 |
$keys = array( 'wp_cache_enabled', 'wp_cache_mod_rewrite', 'wp_cache_mobile_enabled', 'wp_cache_not_logged_in', 'wp_cache_make_known_anon' ); |
| 480 |
foreach ( $keys as $key ) { |
| 481 |
// Match `$key = 1;` / `$key = '1';` / `$key = true;` etc. |
| 482 |
if ( preg_match( '/\$' . preg_quote( $key, '/' ) . '\s*=\s*([^;]+);/', $raw, $m ) ) { |
| 483 |
$val = trim( $m[1], " \t'\"" ); |
| 484 |
$out[ $key ] = in_array( strtolower( $val ), array( '1', 'true' ), true ); |
| 485 |
} |
| 486 |
} |
| 487 |
return ! empty( $out ) ? $out : null; |
| 488 |
} |
| 489 |
|
| 490 |
/** Thin wrapper so detection works before admin plugin.php is loaded. */ |
| 491 |
private static function plugin_active( string $plugin ): bool { |
| 492 |
$active = (array) get_option( 'active_plugins', array() ); |
| 493 |
if ( in_array( $plugin, $active, true ) ) { |
| 494 |
return true; |
| 495 |
} |
| 496 |
// Network-activated (multisite). |
| 497 |
$network = (array) get_site_option( 'active_sitewide_plugins', array() ); |
| 498 |
return isset( $network[ $plugin ] ); |
| 499 |
} |
| 500 |
|
| 501 |
public static function plan_wpsc( array $r ): array { |
| 502 |
$plan = array( |
| 503 |
'cache' => array( |
| 504 |
'enabled' => ! empty( $r['wp_cache_enabled'] ), |
| 505 |
), |
| 506 |
); |
| 507 |
// See plan_wp_rocket: never import Separate Mobile Cache as ON — it |
| 508 |
// disables the device-blind static fast path. Flag for review instead. |
| 509 |
self::map_mobile_separate( $plan, ! empty( $r['wp_cache_mobile_enabled'] ) ); |
| 510 |
return $plan; |
| 511 |
} |
| 512 |
|
| 513 |
// ─────────────────────────── LiteSpeed Cache ───────────────────── |
| 514 |
|
| 515 |
/** |
| 516 |
* Read LiteSpeed Cache settings into a flat `name => value` array keyed |
| 517 |
* by LiteSpeed's dotted setting names (cache, cache-mobile, optm-*, |
| 518 |
* media-*, object-*, cdn-*, …) — the shape plan_litespeed() expects. |
| 519 |
* |
| 520 |
* Storage has changed across LiteSpeed versions: |
| 521 |
* - v4+ (current): ONE option PER setting, named `litespeed.conf.<name>` |
| 522 |
* (e.g. litespeed.conf.cache, litespeed.conf.cache-mobile). There is |
| 523 |
* NO single `litespeed.conf` blob — reading that key returns null, |
| 524 |
* which is why detection used to fail on every modern install. |
| 525 |
* - v3 and earlier: a single serialized array under `litespeed.conf` |
| 526 |
* (or the legacy `litespeed-cache-conf`). |
| 527 |
* We handle all three: try the per-option family first (the common case |
| 528 |
* today), then fall back to the legacy single-blob options. |
| 529 |
* |
| 530 |
* @return array|null raw conf (name => value), or null when absent. |
| 531 |
*/ |
| 532 |
public static function detect_litespeed(): ?array { |
| 533 |
global $wpdb; |
| 534 |
|
| 535 |
// v4+: individual `litespeed.conf.<name>` options. Pull them all and |
| 536 |
// strip the prefix so keys match what plan_litespeed() reads. |
| 537 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-time settings-import scan of another plugin's option rows by name prefix; no WP API bulk-reads by option_name LIKE, and caching a single migration-time read is pointless. |
| 538 |
$rows = $wpdb->get_results( |
| 539 |
"SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE 'litespeed.conf.%'", |
| 540 |
ARRAY_A |
| 541 |
); |
| 542 |
if ( ! empty( $rows ) ) { |
| 543 |
$conf = array(); |
| 544 |
foreach ( $rows as $row ) { |
| 545 |
$name = substr( (string) $row['option_name'], strlen( 'litespeed.conf.' ) ); |
| 546 |
if ( '' === $name || '_version' === $name ) { |
| 547 |
continue; |
| 548 |
} |
| 549 |
// option_value is stored serialized by WP; maybe_unserialize |
| 550 |
// gives back arrays (list settings) or scalars as-is. |
| 551 |
$conf[ $name ] = maybe_unserialize( $row['option_value'] ); |
| 552 |
} |
| 553 |
if ( ! empty( $conf ) ) { |
| 554 |
return $conf; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
// v3 / legacy: a single serialized array. |
| 559 |
$opt = get_option( 'litespeed.conf', null ); |
| 560 |
if ( ! is_array( $opt ) ) { |
| 561 |
$opt = get_option( 'litespeed-cache-conf', null ); |
| 562 |
} |
| 563 |
return is_array( $opt ) && ! empty( $opt ) ? $opt : null; |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Translate LiteSpeed's `litespeed.conf` into xSpeed module patches. |
| 568 |
* LiteSpeed uses dotted keys; values are mostly bool-ish (1/0/'1'). |
| 569 |
* We map only the settings that have a clean xSpeed equivalent and |
| 570 |
* leave the rest untouched so nothing is silently mis-imported. |
| 571 |
* |
| 572 |
* @param array $r raw litespeed.conf. |
| 573 |
*/ |
| 574 |
public static function plan_litespeed( array $r ): array { |
| 575 |
$on = static function ( $key ) use ( $r ): bool { |
| 576 |
return isset( $r[ $key ] ) && ! empty( $r[ $key ] ); |
| 577 |
}; |
| 578 |
// LiteSpeed list fields are stored as either a newline-delimited |
| 579 |
// string or an array. Normalize to a clean string[] either way. |
| 580 |
$list = static function ( $key ) use ( $r ): array { |
| 581 |
$v = $r[ $key ] ?? null; |
| 582 |
if ( is_string( $v ) ) { |
| 583 |
$v = preg_split( '/\r\n|\r|\n/', $v ); |
| 584 |
} |
| 585 |
if ( ! is_array( $v ) ) { |
| 586 |
return array(); |
| 587 |
} |
| 588 |
return array_values( array_filter( array_map( 'trim', array_map( 'strval', $v ) ) ) ); |
| 589 |
}; |
| 590 |
$set_list = static function ( array &$dest, string $dest_key, array $vals ): void { |
| 591 |
if ( $vals ) { |
| 592 |
$dest[ $dest_key ] = $vals; |
| 593 |
} |
| 594 |
}; |
| 595 |
|
| 596 |
$patch = array(); |
| 597 |
|
| 598 |
// ── Page cache ──────────────────────────────────────────────── |
| 599 |
$patch['cache'] = array( |
| 600 |
'enabled' => $on( 'cache' ) || $on( 'cache-priv' ), |
| 601 |
); |
| 602 |
// See plan_wp_rocket: never import Separate Mobile Cache as ON — it |
| 603 |
// disables the device-blind static fast path. Flag for review instead. |
| 604 |
self::map_mobile_separate( $patch, $on( 'cache-mobile' ) ); |
| 605 |
// TTL: LiteSpeed stores cache-ttl_pub in seconds → xSpeed wants hours. |
| 606 |
if ( isset( $r['cache-ttl_pub'] ) && (int) $r['cache-ttl_pub'] > 0 ) { |
| 607 |
$patch['cache']['cache_expiry'] = max( 1, min( 720, (int) ( (int) $r['cache-ttl_pub'] / 3600 ) ) ); |
| 608 |
} |
| 609 |
// Excluded URIs / cookies / user-agents / dropped query strings. |
| 610 |
$set_list( $patch['cache'], 'excluded_urls', $list( 'cache-exc' ) ); |
| 611 |
$set_list( $patch['cache'], 'excluded_cookies', $list( 'cache-exc_cookies' ) ); |
| 612 |
$set_list( $patch['cache'], 'bypass_user_agents', $list( 'cache-exc_useragent' ) ); |
| 613 |
// LiteSpeed "drop query string" list ≈ xSpeed ignored_query_params. |
| 614 |
$set_list( $patch['cache'], 'ignored_query_params', $list( 'cache-drop_qs' ) ); |
| 615 |
|
| 616 |
// ── Minify / optimization ───────────────────────────────────── |
| 617 |
$patch['minify'] = array( |
| 618 |
'minify_html' => $on( 'optm-html_min' ), |
| 619 |
'minify_css' => $on( 'optm-css_min' ), |
| 620 |
'minify_js' => $on( 'optm-js_min' ), |
| 621 |
'combine_css' => $on( 'optm-css_comb' ), |
| 622 |
'combine_js' => $on( 'optm-js_comb' ), |
| 623 |
'defer_js' => $on( 'optm-js_defer' ), |
| 624 |
// LiteSpeed "Delay JS" (optm-js_defer === 2 in some versions, or |
| 625 |
// the dedicated optm-js_delay flag) → xSpeed delay_js. |
| 626 |
'delay_js' => $on( 'optm-js_delay' ) || ( isset( $r['optm-js_defer'] ) && (int) $r['optm-js_defer'] === 2 ), |
| 627 |
// Async/“load CSS asynchronously” — LiteSpeed CCSS async. |
| 628 |
'async_css' => $on( 'optm-css_async' ), |
| 629 |
// Remove query strings from static resources. |
| 630 |
'remove_query_strings' => $on( 'optm-qs_rm' ), |
| 631 |
); |
| 632 |
// Defer/delay exclusion list — merge LiteSpeed's JS defer + delay |
| 633 |
// exclude lists into xSpeed's single defer_js_excluded. |
| 634 |
$defer_exc = array_values( array_unique( array_merge( $list( 'optm-js_defer_exc' ), $list( 'optm-js_delay_exc' ) ) ) ); |
| 635 |
$set_list( $patch['minify'], 'defer_js_excluded', $defer_exc ); |
| 636 |
|
| 637 |
// ── Lazy load (media) ───────────────────────────────────────── |
| 638 |
$patch['lazy'] = array( |
| 639 |
'lazy_images' => $on( 'media-lazy' ), |
| 640 |
'lazy_iframes' => $on( 'media-iframe_lazy' ), |
| 641 |
// LiteSpeed has no separate HTML5-video lazy toggle; mirror the |
| 642 |
// image setting so video preload follows the same intent. |
| 643 |
'lazy_videos' => $on( 'media-lazy' ), |
| 644 |
// "Add Missing Sizes" → add_missing_dimensions (anti-CLS). |
| 645 |
'add_missing_dimensions' => $on( 'media-add_missing_sizes' ), |
| 646 |
); |
| 647 |
$set_list( $patch['lazy'], 'excluded_images', $list( 'media-lazy_exc' ) ); |
| 648 |
|
| 649 |
// ── Fonts ───────────────────────────────────────────────────── |
| 650 |
// LiteSpeed "Font Display Optimization" (optm-localize_style / |
| 651 |
// optm-css_font_display) → xSpeed font-display: swap. |
| 652 |
if ( $on( 'optm-css_font_display' ) || $on( 'optm-localize' ) ) { |
| 653 |
$patch['fonts'] = array( 'font_display_swap' => true ); |
| 654 |
} |
| 655 |
|
| 656 |
// ── Disable bloat ───────────────────────────────────────────── |
| 657 |
// Only map the one LiteSpeed "remove" toggle with a clean xSpeed |
| 658 |
// equivalent: removing the emoji + oEmbed scripts ≈ disable_oembed. |
| 659 |
// (LiteSpeed's optm-emoji_rm strips the wp-emoji + wp-embed pair.) |
| 660 |
// jQuery-migrate / dashicons / XML-RPC / RSS / REST aren't |
| 661 |
// LiteSpeed-managed, so we don't guess at them. |
| 662 |
if ( $on( 'optm-emoji_rm' ) ) { |
| 663 |
$patch['bloat'] = array( 'disable_oembed' => true ); |
| 664 |
} |
| 665 |
|
| 666 |
// ── Browser cache (LiteSpeed: cache-browser) ────────────────── |
| 667 |
if ( $on( 'cache-browser' ) ) { |
| 668 |
$patch['browser-cache'] = array( 'enabled' => true ); |
| 669 |
if ( isset( $r['cache-ttl_browser'] ) && (int) $r['cache-ttl_browser'] > 0 ) { |
| 670 |
$patch['browser-cache']['asset_ttl'] = (int) $r['cache-ttl_browser']; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
// ── Object cache ────────────────────────────────────────────── |
| 675 |
if ( $on( 'object' ) ) { |
| 676 |
$kind = isset( $r['object-kind'] ) && (int) $r['object-kind'] === 1 ? 'redis' : 'memcached'; |
| 677 |
$patch['object-cache'] = array( 'backend' => $kind ); |
| 678 |
if ( ! empty( $r['object-host'] ) ) { |
| 679 |
$host_key = 'redis' === $kind ? 'redis_host' : 'memcached_host'; |
| 680 |
$patch['object-cache'][ $host_key ] = (string) $r['object-host']; |
| 681 |
} |
| 682 |
if ( ! empty( $r['object-port'] ) ) { |
| 683 |
$port_key = 'redis' === $kind ? 'redis_port' : 'memcached_port'; |
| 684 |
$patch['object-cache'][ $port_key ] = (int) $r['object-port']; |
| 685 |
} |
| 686 |
if ( 'redis' === $kind && isset( $r['object-db_id'] ) ) { |
| 687 |
$patch['object-cache']['redis_database'] = max( 0, min( 15, (int) $r['object-db_id'] ) ); |
| 688 |
} |
| 689 |
if ( ! empty( $r['object-pswd'] ) && 'redis' === $kind ) { |
| 690 |
$patch['object-cache']['redis_password'] = (string) $r['object-pswd']; |
| 691 |
} |
| 692 |
if ( ! empty( $r['object-global_groups'] ) || ! empty( $r['object-persistent'] ) ) { |
| 693 |
$patch['object-cache']['persistent'] = $on( 'object-persistent' ); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
// ── Image conversion (Pro Images module) ────────────────────── |
| 698 |
// LiteSpeed media-webp / next-gen image generation → xSpeed Images. |
| 699 |
if ( $on( 'img_optm-webp' ) || $on( 'media-webp' ) || $on( 'img_optm-auto' ) ) { |
| 700 |
$patch['images'] = array( |
| 701 |
'webp' => $on( 'img_optm-webp' ) || $on( 'media-webp' ), |
| 702 |
'avif' => $on( 'img_optm-avif' ), |
| 703 |
); |
| 704 |
} |
| 705 |
|
| 706 |
// ── CDN ─────────────────────────────────────────────────────── |
| 707 |
if ( $on( 'cdn' ) ) { |
| 708 |
$cdn_url = ''; |
| 709 |
if ( ! empty( $r['cdn-mapping'] ) && is_array( $r['cdn-mapping'] ) ) { |
| 710 |
$first = $r['cdn-mapping'][0] ?? array(); |
| 711 |
// LiteSpeed cdn-mapping rows use the 'url' sub-key (array form) |
| 712 |
// or a bare URL string (legacy). |
| 713 |
$cdn_url = is_array( $first ) ? (string) ( $first['url'] ?? ( $first['cdn_url'] ?? '' ) ) : (string) $first; |
| 714 |
} |
| 715 |
if ( '' !== $cdn_url ) { |
| 716 |
$patch['cdn'] = array( |
| 717 |
'enabled' => true, |
| 718 |
'cdn_url' => $cdn_url, |
| 719 |
); |
| 720 |
$set_list( $patch['cdn'], 'excluded_patterns', $list( 'cdn-exclude' ) ); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
return $patch; |
| 725 |
} |
| 726 |
} |
| 727 |
|