fix: address Phase 1 code review findings
- Serve cache via template_redirect so feeds/404/REST are excluded and query conditionals work - Bypass cache on query strings; key files by md5(host+path) to avoid collisions and support multisite - Write wp-content/wp-recache-cache.php drop-in server and set WP_CACHE in wp-config.php so advanced-cache.php actually runs - Only remove advanced-cache.php on deactivation when it is ours - Respect DONOTCACHEPAGE, non-200 responses; bots read but never warm cache - Fix comment purge (comment_post_ID) and heartbeat purge ($data arg) - Fix exclude URLs textarea parsing; purge all when exclusions change - Enqueue admin purge script on every admin-bar page, not just settings - Sync tests, add TESTING.md manual test guide
This commit is contained in:
+25
-27
@@ -58,11 +58,12 @@ function wp_recache_should_exclude() {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (wp_recache_is_robots()) {
|
||||
// Dynamic query-string requests are never cached (story 11).
|
||||
if (!empty($_SERVER['QUERY_STRING'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (is_preview()) {
|
||||
if (is_preview() || is_feed() || is_404()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -93,40 +94,22 @@ function wp_recache_is_robots() {
|
||||
* Check if request is a POST action.
|
||||
*/
|
||||
function wp_recache_is_post_action() {
|
||||
return $_SERVER['REQUEST_METHOD'] === 'POST';
|
||||
return ($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache file path for URL.
|
||||
*
|
||||
* @param string $url URL to cache.
|
||||
*/
|
||||
function wp_recache_get_cache_path($url) {
|
||||
$path = wp_parse_url($url, PHP_URL_PATH);
|
||||
$path = trim($path, '/');
|
||||
|
||||
if (empty($path)) {
|
||||
$path = 'index';
|
||||
}
|
||||
|
||||
$path = sanitize_file_name($path);
|
||||
|
||||
return WP_RECACHE_CACHE_PATH . $path . '.html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache key for URL.
|
||||
* Keyed by md5(host + path) so nested paths, hosts and multisite
|
||||
* sub-directories never collide. Query strings are bypassed upstream.
|
||||
*
|
||||
* @param string $url URL to cache.
|
||||
*/
|
||||
function wp_recache_get_cache_key($url) {
|
||||
$key = wp_parse_url($url, PHP_URL_PATH);
|
||||
function wp_recache_get_cache_path($url) {
|
||||
$host = (string) wp_parse_url($url, PHP_URL_HOST);
|
||||
$path = (string) wp_parse_url($url, PHP_URL_PATH);
|
||||
|
||||
if (empty($key)) {
|
||||
$key = '/';
|
||||
}
|
||||
|
||||
return md5($key);
|
||||
return WP_RECACHE_CACHE_PATH . md5($host . $path) . '.html';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,3 +197,18 @@ function wp_recache_get_cache_path_with_device($url) {
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue the admin-bar purge script (admin and front-end admin bar).
|
||||
*/
|
||||
function wp_recache_enqueue_admin_bar_script() {
|
||||
if (!is_admin_bar_showing() || !current_user_can('manage_options')) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_enqueue_script('wp-recache-admin', WP_RECACHE_URL . 'assets/admin.js', ['jquery'], WP_RECACHE_VERSION, true);
|
||||
wp_localize_script('wp-recache-admin', 'wpRecache', [
|
||||
'ajaxUrl' => admin_url('admin-ajax.php'),
|
||||
'nonce' => wp_create_nonce('wp_recache_nonce'),
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user