200 lines
5.4 KiB
PHP
200 lines
5.4 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::write_cookie_bypass_script();
|
|
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'],
|
|
'rejected_cookies' => WP_RECACHE_DEFAULT_REJECTED_COOKIES,
|
|
'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 bypass script required by advanced-cache.php.
|
|
* Must run without WordPress loaded.
|
|
*
|
|
* Public so Admin can regenerate the drop-in when options change.
|
|
*/
|
|
public static function write_cookie_bypass_script() {
|
|
$rejected_cookies = get_option('wp_recache_rejected_cookies', WP_RECACHE_DEFAULT_REJECTED_COOKIES);
|
|
|
|
$cookies_php = var_export($rejected_cookies, true);
|
|
|
|
$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;
|
|
}
|
|
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
|
|
$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();
|
|
}
|
|
}
|