feat(batch17): unified caching — detect/status/purge-all/purge-url across engines
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q8t9GEyyLZbUQu26na1JQg
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Batch 17 — Caching, unified (Pro-equivalent, net-new).
|
||||
*
|
||||
* One cache family across the common engines: LiteSpeed (also Hostinger's
|
||||
* stack), W3 Total Cache, WP Rocket, WP Super Cache, WP Fastest Cache,
|
||||
* Autoptimize, plus the WordPress object cache. Each handler calls only the
|
||||
* engines actually present, so it is safe everywhere (no guard needed).
|
||||
* GPL-2.0-or-later. See NOTICE.md.
|
||||
*/
|
||||
|
||||
namespace AskTamMCP\Extensions;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class Batch17 {
|
||||
|
||||
public static function register($registry): void {
|
||||
$def = function (string $name, string $desc, array $props, array $required, string $scope, string $method) {
|
||||
$schema = ['type' => 'object', 'properties' => $props];
|
||||
if ($required) $schema['required'] = $required;
|
||||
return ['name' => $name, 'description' => $desc, 'scope' => $scope,
|
||||
'handler' => 'AskTamMCP\\Extensions\\Batch17::' . $method, 'inputSchema' => $schema];
|
||||
};
|
||||
|
||||
$registry->register($def('cache_detect', 'Detect active caching: page-cache plugins (LiteSpeed, W3TC, WP Rocket, WP Super Cache, WP Fastest Cache), Autoptimize, and whether a persistent object cache is in use.',
|
||||
[], [], 'read', 'detect'));
|
||||
|
||||
$registry->register($def('cache_status', 'Cache status summary: object-cache backend (if any), detected page-cache engine, and Autoptimize state.',
|
||||
[], [], 'read', 'status'));
|
||||
|
||||
$registry->register($def('cache_purge_all', 'Purge all caches across every detected engine (page cache + Autoptimize + object cache). Returns which engines were purged.',
|
||||
[], [], 'write', 'purgeAll'));
|
||||
|
||||
$registry->register($def('cache_purge_url', 'Purge the cache for a single URL where the engine supports it (LiteSpeed, WP Rocket). Falls back to reporting unsupported engines.',
|
||||
['url' => ['type' => 'string']], ['url'], 'write', 'purgeUrl'));
|
||||
}
|
||||
|
||||
private static function engines(): array {
|
||||
return [
|
||||
'litespeed' => defined('LSCWP_V') || defined('LSCWP_CUR_V') || has_action('litespeed_purge_all'),
|
||||
'w3tc' => function_exists('w3tc_flush_all'),
|
||||
'wp_rocket' => function_exists('rocket_clean_domain'),
|
||||
'wp_super_cache' => function_exists('wp_cache_clear_cache'),
|
||||
'wp_fastest_cache'=> class_exists('WpFastestCache'),
|
||||
'autoptimize' => class_exists('autoptimizeCache'),
|
||||
'object_cache' => wp_using_ext_object_cache(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function detect(array $args): array {
|
||||
$e = self::engines();
|
||||
return ['engines' => $e, 'active' => array_values(array_keys(array_filter($e)))];
|
||||
}
|
||||
|
||||
public static function status(array $args): array {
|
||||
global $wp_object_cache;
|
||||
$backend = 'none';
|
||||
if (wp_using_ext_object_cache()) {
|
||||
if (class_exists('Redis') && stripos(get_class($wp_object_cache), 'redis') !== false) $backend = 'redis';
|
||||
elseif (stripos(get_class($wp_object_cache), 'memcach') !== false) $backend = 'memcached';
|
||||
else $backend = 'external(' . get_class($wp_object_cache) . ')';
|
||||
}
|
||||
$e = self::engines();
|
||||
$page = array_values(array_keys(array_filter([
|
||||
'litespeed' => $e['litespeed'], 'w3tc' => $e['w3tc'], 'wp_rocket' => $e['wp_rocket'],
|
||||
'wp_super_cache' => $e['wp_super_cache'], 'wp_fastest_cache' => $e['wp_fastest_cache'],
|
||||
])));
|
||||
return ['object_cache' => $backend, 'page_cache_engines' => $page, 'autoptimize' => $e['autoptimize']];
|
||||
}
|
||||
|
||||
public static function purgeAll(array $args): array {
|
||||
$did = [];
|
||||
if (defined('LSCWP_V') || defined('LSCWP_CUR_V') || has_action('litespeed_purge_all')) { do_action('litespeed_purge_all'); $did[] = 'litespeed'; }
|
||||
if (function_exists('w3tc_flush_all')) { w3tc_flush_all(); $did[] = 'w3tc'; }
|
||||
if (function_exists('rocket_clean_domain')) { rocket_clean_domain(); $did[] = 'wp_rocket'; }
|
||||
if (function_exists('wp_cache_clear_cache')) { wp_cache_clear_cache(); $did[] = 'wp_super_cache'; }
|
||||
if (class_exists('WpFastestCache')) {
|
||||
global $wp_fastest_cache;
|
||||
if (is_object($wp_fastest_cache) && method_exists($wp_fastest_cache, 'deleteCache')) { $wp_fastest_cache->deleteCache(true); $did[] = 'wp_fastest_cache'; }
|
||||
}
|
||||
if (class_exists('autoptimizeCache') && method_exists('autoptimizeCache', 'clearall')) { \autoptimizeCache::clearall(); $did[] = 'autoptimize'; }
|
||||
if (wp_using_ext_object_cache()) { wp_cache_flush(); $did[] = 'object_cache'; }
|
||||
return ['purged' => $did, 'count' => count($did)];
|
||||
}
|
||||
|
||||
public static function purgeUrl(array $args): array {
|
||||
$url = esc_url_raw((string) ($args['url'] ?? ''));
|
||||
if ($url === '') throw new \RuntimeException('url required.');
|
||||
$did = [];
|
||||
$unsupported = [];
|
||||
if (defined('LSCWP_V') || defined('LSCWP_CUR_V') || has_action('litespeed_purge_url')) { do_action('litespeed_purge_url', $url); $did[] = 'litespeed'; }
|
||||
if (function_exists('rocket_clean_files')) { rocket_clean_files([$url]); $did[] = 'wp_rocket'; }
|
||||
foreach (['w3tc', 'wp_super_cache', 'wp_fastest_cache'] as $e) {
|
||||
if (self::engines()[$e]) $unsupported[] = $e;
|
||||
}
|
||||
if (!$did && !$unsupported) throw new \RuntimeException('No URL-capable cache engine detected.');
|
||||
return ['url' => $url, 'purged' => $did, 'no_per_url_support' => $unsupported];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user