- 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
177 lines
4.7 KiB
PHP
177 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* Plugin activation handler.
|
|
*
|
|
* @package WP_Recache
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class WP_Recache_Activator {
|
|
|
|
/**
|
|
* Run activation routines.
|
|
*/
|
|
public static function activate() {
|
|
self::create_cache_directory();
|
|
self::set_default_options();
|
|
self::create_cache_server();
|
|
self::create_advanced_cache();
|
|
self::set_wp_cache_constant(true);
|
|
self::flush_rewrite_rules();
|
|
}
|
|
|
|
/**
|
|
* Create cache directory.
|
|
*/
|
|
private static function create_cache_directory() {
|
|
if (!is_dir(WP_RECACHE_CACHE_PATH)) {
|
|
wp_mkdir_p(WP_RECACHE_CACHE_PATH);
|
|
}
|
|
|
|
$htaccess = WP_RECACHE_CACHE_PATH . '.htaccess';
|
|
if (!file_exists($htaccess)) {
|
|
file_put_contents($htaccess, "<IfModule mod_authz_core.c>\nRequire all denied\n</IfModule>\n<IfModule !mod_authz_core.c>\nDeny from all\n</IfModule>\n");
|
|
}
|
|
|
|
$index = WP_RECACHE_CACHE_PATH . 'index.php';
|
|
if (!file_exists($index)) {
|
|
file_put_contents($index, '<?php // Silence is golden.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set default options.
|
|
*/
|
|
private static function set_default_options() {
|
|
$defaults = [
|
|
'cache_enabled' => true,
|
|
'separate_mobile_cache' => false,
|
|
'exclude_urls' => ['/cart', '/checkout', '/my-account'],
|
|
'minify_css' => false,
|
|
'minify_js' => false,
|
|
'lazyload_images' => false,
|
|
'preload_enabled' => false,
|
|
];
|
|
|
|
foreach ($defaults as $key => $value) {
|
|
if (get_option("wp_recache_{$key}") === false) {
|
|
add_option("wp_recache_{$key}", $value);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Write the standalone cache server required by the advanced-cache.php
|
|
* drop-in. Must run without WordPress loaded.
|
|
*/
|
|
private static function create_cache_server() {
|
|
$content = '<?php
|
|
/**
|
|
* WP Recache early cache serving.
|
|
*
|
|
* This file is automatically generated by WP Recache.
|
|
* Do not edit this file manually.
|
|
*/
|
|
|
|
if (($_SERVER[\'REQUEST_METHOD\'] ?? \'GET\') !== \'GET\') {
|
|
return;
|
|
}
|
|
|
|
if (!empty($_SERVER[\'QUERY_STRING\'])) {
|
|
return;
|
|
}
|
|
|
|
foreach ($_COOKIE as $name => $value) {
|
|
if (strpos($name, \'wordpress_logged_in\') === 0) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
$uri = isset($_SERVER[\'REQUEST_URI\']) ? (string) parse_url($_SERVER[\'REQUEST_URI\'], PHP_URL_PATH) : \'/\';
|
|
$host = $_SERVER[\'HTTP_HOST\'] ?? \'\';
|
|
$file = WP_CONTENT_DIR . \'/cache/wp-recache/\' . md5($host . $uri) . \'.html\';
|
|
|
|
// With separate mobile cache enabled, files carry a -desktop/-mobile suffix,
|
|
// so the plain file never exists and serving falls through to WordPress.
|
|
if (file_exists($file)) {
|
|
header(\'X-WP-Recache: HIT\');
|
|
header(\'X-WP-Recache-Dropin: 1\');
|
|
readfile($file);
|
|
exit;
|
|
}
|
|
';
|
|
|
|
file_put_contents(WP_CONTENT_DIR . '/wp-recache-cache.php', $content);
|
|
}
|
|
|
|
/**
|
|
* Create advanced-cache.php drop-in.
|
|
*/
|
|
private static function create_advanced_cache() {
|
|
$advanced_cache = WP_CONTENT_DIR . '/advanced-cache.php';
|
|
|
|
if (file_exists($advanced_cache)) {
|
|
return;
|
|
}
|
|
|
|
$content = '<?php
|
|
/**
|
|
* WP Recache advanced cache.
|
|
*
|
|
* This file is automatically generated by WP Recache.
|
|
* Do not edit this file manually.
|
|
*/
|
|
|
|
defined(\'ABSPATH\') || exit;
|
|
|
|
if (file_exists(WP_CONTENT_DIR . \'/wp-recache-cache.php\')) {
|
|
require_once WP_CONTENT_DIR . \'/wp-recache-cache.php\';
|
|
}
|
|
';
|
|
|
|
file_put_contents($advanced_cache, $content);
|
|
}
|
|
|
|
/**
|
|
* Add or remove the WP_CACHE constant in wp-config.php.
|
|
*
|
|
* Without it, WordPress never loads advanced-cache.php.
|
|
*
|
|
* @param bool $enable True to add the constant, false to remove it.
|
|
*/
|
|
public static function set_wp_cache_constant($enable) {
|
|
$marker = "define('WP_CACHE', true); // Added by WP Recache.\n";
|
|
$config_path = ABSPATH . 'wp-config.php';
|
|
|
|
if (!file_exists($config_path)) {
|
|
$config_path = dirname(ABSPATH) . '/wp-config.php';
|
|
}
|
|
|
|
if (!file_exists($config_path) || !is_writable($config_path)) {
|
|
return;
|
|
}
|
|
|
|
$content = file_get_contents($config_path);
|
|
|
|
if ($enable) {
|
|
if (strpos($content, $marker) !== false || preg_match("/define\s*\(\s*['\"]WP_CACHE['\"]\s*,\s*true\s*\)/", $content)) {
|
|
return;
|
|
}
|
|
|
|
$content = preg_replace('/^<\?php/', "<?php\n" . $marker, $content, 1);
|
|
} else {
|
|
$content = str_replace($marker, '', $content);
|
|
}
|
|
|
|
file_put_contents($config_path, $content);
|
|
}
|
|
|
|
/**
|
|
* Flush rewrite rules.
|
|
*/
|
|
private static function flush_rewrite_rules() {
|
|
flush_rewrite_rules();
|
|
}
|
|
}
|