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
+39 -4
View File
@@ -48,6 +48,13 @@ class WP_Recache_Activator {
'cache_enabled' => true,
'separate_mobile_cache' => false,
'exclude_urls' => ['/cart', '/checkout', '/my-account'],
'rejected_cookies' => [
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
],
'minify_css' => false,
'minify_js' => false,
'lazyload_images' => false,
@@ -64,8 +71,20 @@ class WP_Recache_Activator {
/**
* Write the standalone cache server required by the advanced-cache.php
* drop-in. Must run without WordPress loaded.
*
* Public so Admin can regenerate the drop-in when options change.
*/
private static function create_cache_server() {
public static function create_cache_server() {
$rejected_cookies = get_option('wp_recache_rejected_cookies', [
'woocommerce_items_in_cart',
'wp_woocommerce_session_*',
'wp-postpass',
'comment_author_*',
'wordpress_*',
]);
$cookies_php = var_export($rejected_cookies, true);
$content = '<?php
/**
* WP Recache early cache serving.
@@ -82,9 +101,25 @@ if (!empty($_SERVER[\'QUERY_STRING\'])) {
return;
}
foreach ($_COOKIE as $name => $value) {
if (strpos($name, \'wordpress_logged_in\') === 0) {
return;
$rejected_cookies = ' . $cookies_php . ';
foreach ($rejected_cookies as $cookie) {
$cookie = trim($cookie);
if ($cookie === \'\') {
continue;
}
if (substr($cookie, -1) === \'*\') {
$prefix = substr($cookie, 0, -1);
foreach ($_COOKIE as $name => $value) {
if (strpos($name, $prefix) === 0) {
return;
}
}
} else {
if (isset($_COOKIE[$cookie])) {
return;
}
}
}