| 1 |
<?php |
| 2 |
/** |
| 3 |
* The WP_CACHE define, and how to strip it out of wp-config.php. |
| 4 |
* |
| 5 |
* Deliberately a plain function in its own file rather than a method on |
| 6 |
* Cache: `uninstall.php` runs standalone — WordPress loads it without |
| 7 |
* bootstrapping the plugin, so no class of ours exists there. Keeping the |
| 8 |
* pattern in one requirable file is what stops the two call sites drifting. |
| 9 |
* They already had, which is the bug this fixes: the removal regex was |
| 10 |
* copy-pasted, and hardening one copy would have left the other broken. |
| 11 |
* |
| 12 |
* @package XSpeed |
| 13 |
*/ |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
if ( ! function_exists( 'xspeed_strip_wp_cache_define' ) ) { |
| 18 |
/** |
| 19 |
* Remove every `define( 'WP_CACHE', … );` statement from wp-config source. |
| 20 |
* |
| 21 |
* The old pattern hardcoded a lowercase `true` with no `/i`, so |
| 22 |
* `TRUE`, `True`, `1`, `'1'` and `"1"` never matched — disabling the |
| 23 |
* cache (or uninstalling) silently left the constant behind. Those |
| 24 |
* spellings are common: several hosts' one-click stacks and older |
| 25 |
* tutorials write `TRUE` or `1`, and any plugin that previously owned |
| 26 |
* the constant may have written it in its own style. The orphan then |
| 27 |
* confuses the next caching plugin the site installs — W3 Total Cache |
| 28 |
* and WP Rocket both branch on it — and makes a "clean uninstall" not |
| 29 |
* clean. (#9) |
| 30 |
* |
| 31 |
* Matches the VALUE as `[^)]*`, the same shape the enable branch in |
| 32 |
* Cache::set_wp_cache_constant() already uses, so any spelling goes: |
| 33 |
* true / TRUE / 1 / '1' / false / 0, with or without inner spaces. |
| 34 |
* `false` is removed too — leaving `define( 'WP_CACHE', false );` |
| 35 |
* behind is the same orphan problem, just quieter. |
| 36 |
* |
| 37 |
* Only our own statement shape is targeted. A commented-out line keeps |
| 38 |
* its `#`/`//` prefix and is left alone by the leading-boundary match; |
| 39 |
* a define built dynamically (concatenation, a variable) is not a |
| 40 |
* literal statement and is out of scope for a regex — those are |
| 41 |
* vanishingly rare in wp-config.php and unsafe to rewrite blind. |
| 42 |
* |
| 43 |
* @param string $config Raw wp-config.php contents. |
| 44 |
* @return string Contents with the define(s) removed. |
| 45 |
*/ |
| 46 |
function xspeed_strip_wp_cache_define( $config ) { |
| 47 |
if ( ! is_string( $config ) || '' === $config ) { |
| 48 |
return $config; |
| 49 |
} |
| 50 |
|
| 51 |
$stripped = preg_replace( |
| 52 |
'/^[ \t]*define\(\s*[\'"]WP_CACHE[\'"]\s*,\s*[^)]*\)\s*;[ \t]*\r?\n?/mi', |
| 53 |
'', |
| 54 |
$config |
| 55 |
); |
| 56 |
|
| 57 |
// preg_replace() returns null on failure (catastrophic backtracking, |
| 58 |
// bad UTF-8). Returning that would blank wp-config.php — hand back |
| 59 |
// the untouched source instead and leave the constant in place. |
| 60 |
return is_string( $stripped ) ? $stripped : $config; |
| 61 |
} |
| 62 |
} |
| 63 |
|