refactor: extract DEFAULT_REJECTED_COOKIES constant and rename create_cache_server

This commit is contained in:
2026-09-13 17:21:36 +02:00
parent 94eea83d60
commit 22230d188b
6 changed files with 18 additions and 49 deletions
+6 -18
View File
@@ -15,7 +15,7 @@ class WP_Recache_Activator {
public static function activate() { public static function activate() {
self::create_cache_directory(); self::create_cache_directory();
self::set_default_options(); self::set_default_options();
self::create_cache_server(); self::write_cookie_bypass_script();
self::create_advanced_cache(); self::create_advanced_cache();
self::set_wp_cache_constant(true); self::set_wp_cache_constant(true);
self::flush_rewrite_rules(); self::flush_rewrite_rules();
@@ -48,13 +48,7 @@ class WP_Recache_Activator {
'cache_enabled' => true, 'cache_enabled' => true,
'separate_mobile_cache' => false, 'separate_mobile_cache' => false,
'exclude_urls' => ['/cart', '/checkout', '/my-account'], 'exclude_urls' => ['/cart', '/checkout', '/my-account'],
'rejected_cookies' => [ 'rejected_cookies' => WP_RECACHE_DEFAULT_REJECTED_COOKIES,
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
],
'minify_css' => false, 'minify_css' => false,
'minify_js' => false, 'minify_js' => false,
'lazyload_images' => false, 'lazyload_images' => false,
@@ -69,19 +63,13 @@ class WP_Recache_Activator {
} }
/** /**
* Write the standalone cache server required by the advanced-cache.php * Write the standalone cache bypass script required by advanced-cache.php.
* drop-in. Must run without WordPress loaded. * Must run without WordPress loaded.
* *
* Public so Admin can regenerate the drop-in when options change. * Public so Admin can regenerate the drop-in when options change.
*/ */
public static function create_cache_server() { public static function write_cookie_bypass_script() {
$rejected_cookies = get_option('wp_recache_rejected_cookies', [ $rejected_cookies = get_option('wp_recache_rejected_cookies', WP_RECACHE_DEFAULT_REJECTED_COOKIES);
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
]);
$cookies_php = var_export($rejected_cookies, true); $cookies_php = var_export($rejected_cookies, true);
+2 -8
View File
@@ -23,7 +23,7 @@ class WP_Recache_Admin {
// New exclusions must not leave stale files that the drop-in would still serve. // New exclusions must not leave stale files that the drop-in would still serve.
add_action('update_option_wp_recache_exclude_urls', 'wp_recache_delete_all_cache'); add_action('update_option_wp_recache_exclude_urls', 'wp_recache_delete_all_cache');
add_action('update_option_wp_recache_rejected_cookies', function () { add_action('update_option_wp_recache_rejected_cookies', function () {
WP_Recache_Activator::create_cache_server(); WP_Recache_Activator::write_cookie_bypass_script();
wp_recache_delete_all_cache(); wp_recache_delete_all_cache();
}); });
} }
@@ -66,13 +66,7 @@ class WP_Recache_Admin {
register_setting('wp_recache_settings', 'wp_recache_rejected_cookies', [ register_setting('wp_recache_settings', 'wp_recache_rejected_cookies', [
'type' => 'array', 'type' => 'array',
'sanitize_callback' => [$this, 'sanitize_rejected_cookies'], 'sanitize_callback' => [$this, 'sanitize_rejected_cookies'],
'default' => [ 'default' => WP_RECACHE_DEFAULT_REJECTED_COOKIES,
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
],
]); ]);
register_setting('wp_recache_settings', 'wp_recache_minify_css', [ register_setting('wp_recache_settings', 'wp_recache_minify_css', [
+1 -7
View File
@@ -55,13 +55,7 @@ $stats = WP_Recache_Admin::get_cache_stats();
<tr> <tr>
<th scope="row">Rejected Cookies</th> <th scope="row">Rejected Cookies</th>
<td> <td>
<textarea name="wp_recache_rejected_cookies" rows="5" cols="50" class="large-text"><?php echo esc_textarea(implode("\n", get_option('wp_recache_rejected_cookies', [ <textarea name="wp_recache_rejected_cookies" rows="5" cols="50" class="large-text"><?php echo esc_textarea(implode("\n", get_option('wp_recache_rejected_cookies', WP_RECACHE_DEFAULT_REJECTED_COOKIES))); ?></textarea>
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
]))); ?></textarea>
<p class="description">One cookie name per line. Visitors with these cookies bypass the cache. Use <code>*</code> as a suffix to match a prefix (e.g. <code>comment_author_*</code>).</p> <p class="description">One cookie name per line. Visitors with these cookies bypass the cache. Use <code>*</code> as a suffix to match a prefix (e.g. <code>comment_author_*</code>).</p>
</td> </td>
</tr> </tr>
+1 -7
View File
@@ -97,13 +97,7 @@ function wp_recache_has_rejected_cookie() {
return false; return false;
} }
$rejected = wp_recache_get_option('rejected_cookies', [ $rejected = wp_recache_get_option('rejected_cookies', WP_RECACHE_DEFAULT_REJECTED_COOKIES);
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
]);
foreach ($rejected as $cookie) { foreach ($rejected as $cookie) {
$cookie = trim($cookie); $cookie = trim($cookie);
+1 -9
View File
@@ -76,14 +76,6 @@ class WP_Recache_Admin_Test extends WP_UnitTestCase {
$admin->init(); $admin->init();
do_action('admin_init'); do_action('admin_init');
$default = [ $this->assertEquals(WP_RECACHE_DEFAULT_REJECTED_COOKIES, get_option('wp_recache_rejected_cookies', WP_RECACHE_DEFAULT_REJECTED_COOKIES));
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
];
$this->assertEquals($default, get_option('wp_recache_rejected_cookies', $default));
} }
} }
+7
View File
@@ -21,6 +21,13 @@ define('WP_RECACHE_PATH', plugin_dir_path(__FILE__));
define('WP_RECACHE_URL', plugin_dir_url(__FILE__)); define('WP_RECACHE_URL', plugin_dir_url(__FILE__));
define('WP_RECACHE_INC_PATH', WP_RECACHE_PATH . 'includes/'); define('WP_RECACHE_INC_PATH', WP_RECACHE_PATH . 'includes/');
define('WP_RECACHE_CACHE_PATH', WP_CONTENT_DIR . '/cache/wp-recache/'); define('WP_RECACHE_CACHE_PATH', WP_CONTENT_DIR . '/cache/wp-recache/');
define('WP_RECACHE_DEFAULT_REJECTED_COOKIES', [
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
]);
require_once WP_RECACHE_INC_PATH . 'Common/Helpers.php'; require_once WP_RECACHE_INC_PATH . 'Common/Helpers.php';