| 1 |
<?php |
| 2 |
/** |
| 3 |
* MCP Tool: Cache Purge |
| 4 |
* |
| 5 |
* Exposes the plugin's existing cache purge infrastructure (Metasync_Cache_Purge |
| 6 |
* and Metasync_Edge_Cache_Purge) as MCP tools so AI agents can clear caches |
| 7 |
* after updating content. |
| 8 |
* |
| 9 |
* @package MetaSync |
| 10 |
* @subpackage MCP_Server/Tools |
| 11 |
*/ |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Purge all WordPress caches and edge/CDN caches. |
| 19 |
* |
| 20 |
* Wraps Metasync_Cache_Purge::clear_all_caches() for WordPress-level caches |
| 21 |
* and Metasync_Edge_Cache_Purge::purge() for CDN providers. Passes home_url |
| 22 |
* to the edge purge so full-flush providers (Sucuri, Sevalla, Flywheel) fire, |
| 23 |
* and URL-based providers (Cloudflare, Cloudways) purge at least the homepage. |
| 24 |
*/ |
| 25 |
class MCP_Tool_Cache_Purge_All extends MCP_Tool_Base { |
| 26 |
|
| 27 |
public function get_name() { |
| 28 |
return 'wordpress_cache_purge_all'; |
| 29 |
} |
| 30 |
|
| 31 |
public function get_description() { |
| 32 |
return 'Purge all WordPress caches (object cache + every detected cache plugin) and trigger edge/CDN cache invalidation for configured providers.'; |
| 33 |
} |
| 34 |
|
| 35 |
public function get_input_schema() { |
| 36 |
return [ |
| 37 |
'type' => 'object', |
| 38 |
'properties' => [ |
| 39 |
'source' => [ |
| 40 |
'type' => 'string', |
| 41 |
'description' => 'Optional source identifier used for logging (defaults to "mcp").', |
| 42 |
'default' => 'mcp' |
| 43 |
] |
| 44 |
], |
| 45 |
'required' => [] |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
public function execute($params) { |
| 50 |
$this->validate_params($params); |
| 51 |
$this->require_capability('manage_options'); |
| 52 |
|
| 53 |
$source = isset($params['source']) && is_string($params['source']) |
| 54 |
? $this->sanitize_string($params['source']) |
| 55 |
: 'mcp'; |
| 56 |
|
| 57 |
try { |
| 58 |
$results = Metasync_Cache_Purge::get_instance()->clear_all_caches($source); |
| 59 |
Metasync_Edge_Cache_Purge::purge([home_url('/')]); |
| 60 |
|
| 61 |
return $this->success([ |
| 62 |
'cleared' => isset($results['cleared']) ? $results['cleared'] : [], |
| 63 |
'failed' => isset($results['failed']) ? $results['failed'] : [], |
| 64 |
'not_active' => isset($results['not_active']) ? $results['not_active'] : [], |
| 65 |
], 'All WordPress and edge caches purged.'); |
| 66 |
} catch (Exception $e) { |
| 67 |
$this->error('Failed to purge caches: ' . $e->getMessage()); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Purge cache for a specific URL (including CDN/edge cache). |
| 74 |
*/ |
| 75 |
class MCP_Tool_Cache_Purge_URL extends MCP_Tool_Base { |
| 76 |
|
| 77 |
public function get_name() { |
| 78 |
return 'wordpress_cache_purge_url'; |
| 79 |
} |
| 80 |
|
| 81 |
public function get_description() { |
| 82 |
return 'Purge cache for a specific URL across every detected cache plugin and configured CDN/edge provider.'; |
| 83 |
} |
| 84 |
|
| 85 |
public function get_input_schema() { |
| 86 |
return [ |
| 87 |
'type' => 'object', |
| 88 |
'properties' => [ |
| 89 |
'url' => [ |
| 90 |
'type' => 'string', |
| 91 |
'description' => 'Absolute URL to purge from every cache layer.' |
| 92 |
] |
| 93 |
], |
| 94 |
'required' => ['url'] |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
public function execute($params) { |
| 99 |
$this->validate_params($params); |
| 100 |
$this->require_capability('manage_options'); |
| 101 |
|
| 102 |
$url = $this->sanitize_url($params['url']); |
| 103 |
|
| 104 |
if (empty($url)) { |
| 105 |
$this->error('Invalid URL: must be an absolute URL with scheme (e.g. https://example.com/page/).'); |
| 106 |
} |
| 107 |
|
| 108 |
try { |
| 109 |
$page_cleared = Metasync_Cache_Purge::get_instance()->clear_url_cache($url); |
| 110 |
Metasync_Edge_Cache_Purge::purge([$url]); |
| 111 |
|
| 112 |
return $this->success([ |
| 113 |
'url' => $url, |
| 114 |
'page_cleared' => (bool) $page_cleared |
| 115 |
], 'Cache cleared for URL.'); |
| 116 |
} catch (Exception $e) { |
| 117 |
$this->error('Failed to purge cache for URL: ' . $e->getMessage()); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|