feat: rejected-cookie cache exclusions for WooCommerce cart

Add wp_recache_rejected_cookies option (defaults: woocommerce_items_in_cart,
wp_woocommerce_session_*, wp-postpass, comment_author_*, wordpress_*) that
bypasses page cache when any listed cookie is present. Both the WP-level
check (wp_recache_should_exclude) and the standalone drop-in apply the
same exact/prefix matching logic. Editing the option regenerates the
drop-in and purges all cache.
This commit is contained in:
2026-09-13 15:43:20 +02:00
parent 43081d6e5d
commit 65657276f6
7 changed files with 206 additions and 4 deletions
+34
View File
@@ -52,4 +52,38 @@ class WP_Recache_Admin_Test extends WP_UnitTestCase {
$this->assertGreaterThanOrEqual(2, $stats['files']);
}
public function test_sanitize_rejected_cookies_trims_whitespace() {
$admin = new WP_Recache_Admin();
$result = $admin->sanitize_rejected_cookies(" wp-postpass \n comment_author_* ");
$this->assertEquals(['wp-postpass', 'comment_author_*'], $result);
}
public function test_sanitize_rejected_cookies_filters_empty_lines() {
$admin = new WP_Recache_Admin();
$result = $admin->sanitize_rejected_cookies("wp-postpass\n\n\ncomment_author_*\n");
$this->assertEquals(['wp-postpass', 'comment_author_*'], $result);
}
public function test_sanitize_rejected_cookies_handles_array_input() {
$admin = new WP_Recache_Admin();
$result = $admin->sanitize_rejected_cookies(['wp-postpass', 'comment_author_*']);
$this->assertEquals(['wp-postpass', 'comment_author_*'], $result);
}
public function test_rejected_cookies_option_is_registered() {
$admin = new WP_Recache_Admin();
$admin->init();
do_action('admin_init');
$default = [
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
];
$this->assertEquals($default, get_option('wp_recache_rejected_cookies', $default));
}
}
+42
View File
@@ -88,6 +88,48 @@ class WP_Recache_Helpers_Test extends WP_UnitTestCase {
$this->assertTrue(wp_recache_should_exclude());
}
public function test_wp_recache_has_rejected_cookie_returns_true_for_exact_match() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['wp-postpass']);
$_COOKIE = ['wp-postpass' => 'abc123'];
$this->assertTrue(wp_recache_has_rejected_cookie());
$_COOKIE = [];
}
public function test_wp_recache_has_rejected_cookie_returns_true_for_prefix_match() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['comment_author_*']);
$_COOKIE = ['comment_author_12345' => 'test'];
$this->assertTrue(wp_recache_has_rejected_cookie());
$_COOKIE = [];
}
public function test_wp_recache_has_rejected_cookie_returns_false_when_no_match() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['wp-postpass']);
$_COOKIE = ['unrelated_cookie' => 'value'];
$this->assertFalse(wp_recache_has_rejected_cookie());
$_COOKIE = [];
}
public function test_wp_recache_has_rejected_cookie_returns_false_when_empty_cookies() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['wp-postpass']);
$_COOKIE = [];
$this->assertFalse(wp_recache_has_rejected_cookie());
}
public function test_wp_recache_should_exclude_returns_true_for_rejected_cookie() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['woocommerce_items_in_cart']);
$_COOKIE = ['woocommerce_items_in_cart' => 'yes'];
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/test-page/';
unset($_SERVER['HTTP_USER_AGENT']);
$this->assertTrue(wp_recache_should_exclude());
$_COOKIE = [];
}
public function test_wp_recache_delete_cache_removes_file() {
$url = 'https://example.com/test-page/';
$path = wp_recache_get_cache_path($url);