feat: clean up own data on deactivation and uninstall

Deactivation now clears the endpoint-check transient and the cached rewrite
rules (forcing WordPress to regenerate them without this plugin's routes on
the next request) and activation asks mcp-oauth to re-flush its own rules.
uninstall.php removes all own options/transient/cron/table across every site
of a network, delegating to the new self-contained LIT\MCP\Uninstaller class
so it works without the plugin's autoloader and is unit-testable on its own.
mcp-oauth's data (JWT secret, rewrite marker, refresh-token meta, application
passwords) is left untouched, as is every post/attachment/revision.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
2026-09-26 04:21:23 +02:00
co-authored by Claude Opus 5.5
parent 522f5bbd19
commit 7035448f7d
7 changed files with 229 additions and 3 deletions
+1
View File
@@ -2,6 +2,7 @@
<ruleset name="wp-lit-mcp"> <ruleset name="wp-lit-mcp">
<file>src</file> <file>src</file>
<file>wp-lit-mcp.php</file> <file>wp-lit-mcp.php</file>
<file>uninstall.php</file>
<exclude-pattern>/vendor/*</exclude-pattern> <exclude-pattern>/vendor/*</exclude-pattern>
<arg value="sp"/> <arg value="sp"/>
<arg name="extensions" value="php"/> <arg name="extensions" value="php"/>
+14 -1
View File
@@ -4,12 +4,15 @@ declare(strict_types=1);
namespace LIT\MCP; namespace LIT\MCP;
use LIT\MCP\Admin\ConnectionStatus;
use LIT\MCP\ChangeLog\ChangeLog; use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\ChangeLog\ChangeLogCleanup; use LIT\MCP\ChangeLog\ChangeLogCleanup;
use LIT\MCP\ChangeLog\ChangeLogInstaller; use LIT\MCP\ChangeLog\ChangeLogInstaller;
use WPMedia\MCP\OAuth\Bootstrap as OAuthBootstrap;
/** /**
* Activation and deactivation hooks (registered in wp-lit-mcp.php). * Activation and deactivation hooks (registered in wp-lit-mcp.php). Uninstall
* lives in uninstall.php.
*/ */
final class Lifecycle final class Lifecycle
{ {
@@ -18,10 +21,20 @@ final class Lifecycle
$log = new ChangeLog(); $log = new ChangeLog();
(new ChangeLogInstaller($log))->install(); (new ChangeLogInstaller($log))->install();
(new ChangeLogCleanup($log, new Limits()))->ensureScheduled(); (new ChangeLogCleanup($log, new Limits()))->ensureScheduled();
if (class_exists(OAuthBootstrap::class)) {
// mcp-oauth's public API: rewrite its /oauth and /.well-known rules on the next init.
OAuthBootstrap::schedule_rewrite_flush();
}
} }
public static function deactivate(): void public static function deactivate(): void
{ {
wp_clear_scheduled_hook(ChangeLogCleanup::HOOK); wp_clear_scheduled_hook(ChangeLogCleanup::HOOK);
delete_transient(ConnectionStatus::ENDPOINT_TRANSIENT);
// WordPress regenerates the rules on the next request – without those of this now
// inactive plugin. Calling flush_rewrite_rules() here instead would rewrite them
// including our rules, because the plugin is still loaded during this request.
delete_option('rewrite_rules');
} }
} }
+62
View File
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace LIT\MCP;
/**
* Removes WP-LIT-MCP's own data (uninstall.php, Task 12): options, the endpoint-check
* transient, the log-cleanup cron hook and the change log table – on every site of a
* network.
*
* Deliberately self-contained (no reference to any other LIT\MCP\* class): uninstall.php
* is executed by WordPress core standalone, without loading the plugin's main file first,
* so the Composer/Jetpack autoloader is not guaranteed to have run. The option/hook/table
* names below are therefore duplicated from Access::OPTION, Abilities\ExposurePolicy::OPTION,
* Limits::OPTION, ChangeLog\ChangeLogInstaller::VERSION_OPTION, Admin\ConnectionStatus::
* ENDPOINT_TRANSIENT, ChangeLog\ChangeLogCleanup::HOOK and ChangeLog\ChangeLog::TABLE on
* purpose; tests/Unit/UninstallerTest.php and tests/Unit/UninstallTest.php keep them in
* sync with those constants.
*
* Never touches the bundled wp-media/mcp-oauth library's data (option mcp_jwt_secret,
* option wpmedia_mcp_oauth_rewrite_version, user meta mcp_refresh_jti_*, application
* passwords of connected clients) – that library may be shared with other plugins on
* this site. Never deletes posts, attachments or revisions.
*/
final class Uninstaller
{
private const OPTIONS = ['lit_mcp_allowed_roles', 'lit_mcp_exposure', 'lit_mcp_limits', 'lit_mcp_db_version'];
private const TRANSIENT = 'lit_mcp_endpoint_check';
private const CRON_HOOK = 'lit_mcp_log_cleanup';
private const TABLE = 'lit_mcp_log';
public static function run(): void
{
if (! is_multisite()) {
self::removeSiteData();
return;
}
foreach (get_sites(['fields' => 'ids', 'number' => 0]) as $site_id) {
switch_to_blog((int) $site_id);
self::removeSiteData();
restore_current_blog();
}
}
public static function removeSiteData(): void
{
global $wpdb;
foreach (self::OPTIONS as $option) {
delete_option($option);
}
delete_transient(self::TRANSIENT);
wp_clear_scheduled_hook(self::CRON_HOOK);
$table = $wpdb->prefix . self::TABLE;
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- own table name, not user input.
$wpdb->query("DROP TABLE IF EXISTS {$table}");
}
}
+5 -2
View File
@@ -16,20 +16,23 @@ final class LifecycleTest extends TestCase
parent::tearDown(); parent::tearDown();
} }
public function testActivateInstallsTheTableAndSchedulesTheCleanup(): void public function testActivateInstallsTableSchedulesCleanupAndRequestsOauthRewriteFlush(): void
{ {
$GLOBALS['wpdb'] = Mockery::mock(\wpdb::class)->makePartial(); $GLOBALS['wpdb'] = Mockery::mock(\wpdb::class)->makePartial();
Functions\expect('dbDelta')->once(); Functions\expect('dbDelta')->once();
Functions\expect('update_option')->once()->with('lit_mcp_db_version', '1'); Functions\expect('update_option')->once()->with('lit_mcp_db_version', '1');
Functions\when('wp_next_scheduled')->justReturn(false); Functions\when('wp_next_scheduled')->justReturn(false);
Functions\expect('wp_schedule_event')->once(); Functions\expect('wp_schedule_event')->once();
Functions\expect('delete_option')->once()->with('wpmedia_mcp_oauth_rewrite_version');
Lifecycle::activate(); Lifecycle::activate();
} }
public function testDeactivateUnschedulesTheCleanup(): void public function testDeactivateRemovesCronTransientAndStoredRewriteRules(): void
{ {
Functions\expect('wp_clear_scheduled_hook')->once()->with('lit_mcp_log_cleanup'); Functions\expect('wp_clear_scheduled_hook')->once()->with('lit_mcp_log_cleanup');
Functions\expect('delete_transient')->once()->with('lit_mcp_endpoint_check');
Functions\expect('delete_option')->once()->with('rewrite_rules');
Lifecycle::deactivate(); Lifecycle::deactivate();
} }
+51
View File
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit;
use Brain\Monkey\Functions;
use LIT\MCP\Abilities\ExposurePolicy;
use LIT\MCP\Access;
use LIT\MCP\Admin\ConnectionStatus;
use LIT\MCP\ChangeLog\ChangeLogCleanup;
use LIT\MCP\ChangeLog\ChangeLogInstaller;
use LIT\MCP\Limits;
use Mockery;
final class UninstallTest extends TestCase
{
protected function tearDown(): void
{
unset($GLOBALS['wpdb']);
parent::tearDown();
}
public function testRemovesOnlyOwnDataOnEverySite(): void
{
if (! defined('WP_UNINSTALL_PLUGIN')) {
define('WP_UNINSTALL_PLUGIN', 'WP-LIT-MCP/wp-lit-mcp.php');
}
$db = Mockery::mock(\wpdb::class)->makePartial();
$db->expects('query')->twice()->with('DROP TABLE IF EXISTS wp_lit_mcp_log');
$GLOBALS['wpdb'] = $db;
Functions\when('is_multisite')->justReturn(true);
Functions\when('get_sites')->justReturn([1, 2]);
Functions\expect('switch_to_blog')->twice();
Functions\expect('restore_current_blog')->twice();
Functions\expect('delete_transient')->twice()->with(ConnectionStatus::ENDPOINT_TRANSIENT);
Functions\expect('wp_clear_scheduled_hook')->twice()->with(ChangeLogCleanup::HOOK);
$deleted = [];
Functions\when('delete_option')->alias(static function (string $option) use (&$deleted): bool {
$deleted[] = $option;
return true;
});
require dirname(__DIR__, 2) . '/uninstall.php';
$own = [Access::OPTION, ExposurePolicy::OPTION, Limits::OPTION, ChangeLogInstaller::VERSION_OPTION];
self::assertSame(array_merge($own, $own), $deleted);
self::assertNotContains('mcp_jwt_secret', $deleted);
self::assertNotContains('wpmedia_mcp_oauth_rewrite_version', $deleted);
}
}
+69
View File
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit;
use Brain\Monkey\Functions;
use LIT\MCP\Abilities\ExposurePolicy;
use LIT\MCP\Access;
use LIT\MCP\Admin\ConnectionStatus;
use LIT\MCP\ChangeLog\ChangeLogCleanup;
use LIT\MCP\ChangeLog\ChangeLogInstaller;
use LIT\MCP\Limits;
use LIT\MCP\Uninstaller;
use Mockery;
/**
* Unit tests for the cleanup logic itself (extracted from uninstall.php so it is
* testable without requiring that top-level script). tests/Unit/UninstallTest.php
* additionally exercises uninstall.php's wiring to this class.
*/
final class UninstallerTest extends TestCase
{
protected function tearDown(): void
{
unset($GLOBALS['wpdb']);
parent::tearDown();
}
public function testRunRemovesOwnDataOnceOnASingleSite(): void
{
$db = Mockery::mock(\wpdb::class)->makePartial();
$db->expects('query')->once()->with('DROP TABLE IF EXISTS wp_lit_mcp_log');
$GLOBALS['wpdb'] = $db;
Functions\when('is_multisite')->justReturn(false);
Functions\expect('switch_to_blog')->never();
Functions\expect('restore_current_blog')->never();
Functions\expect('delete_transient')->once()->with(ConnectionStatus::ENDPOINT_TRANSIENT);
Functions\expect('wp_clear_scheduled_hook')->once()->with(ChangeLogCleanup::HOOK);
$deleted = [];
Functions\when('delete_option')->alias(static function (string $option) use (&$deleted): bool {
$deleted[] = $option;
return true;
});
Uninstaller::run();
self::assertSame(
[Access::OPTION, ExposurePolicy::OPTION, Limits::OPTION, ChangeLogInstaller::VERSION_OPTION],
$deleted
);
}
public function testRunRemovesDataOnEverySiteOfAMultisiteNetwork(): void
{
$db = Mockery::mock(\wpdb::class)->makePartial();
$db->expects('query')->twice()->with('DROP TABLE IF EXISTS wp_lit_mcp_log');
$GLOBALS['wpdb'] = $db;
Functions\when('is_multisite')->justReturn(true);
Functions\when('get_sites')->justReturn([1, 2]);
Functions\expect('switch_to_blog')->twice();
Functions\expect('restore_current_blog')->twice();
Functions\expect('delete_transient')->twice()->with(ConnectionStatus::ENDPOINT_TRANSIENT);
Functions\expect('wp_clear_scheduled_hook')->twice()->with(ChangeLogCleanup::HOOK);
Functions\when('delete_option')->justReturn(true);
Uninstaller::run();
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
/**
* Uninstall WP-LIT-MCP: removes its own data only, on every site of a network.
*
* Removed: options lit_mcp_allowed_roles, lit_mcp_exposure, lit_mcp_limits, lit_mcp_db_version,
* transient lit_mcp_endpoint_check, cron hook lit_mcp_log_cleanup, table {prefix}lit_mcp_log.
*
* Kept on purpose: data of the bundled wp-media/mcp-oauth library (option mcp_jwt_secret,
* option wpmedia_mcp_oauth_rewrite_version, user meta mcp_refresh_jti_*, application passwords
* of connected clients). The library may be used by other plugins on this site; see readme.txt.
* Posts, attachments and revisions are never touched.
*
* The cleanup logic lives in src/Uninstaller.php, required directly below by path rather
* than through the Composer/Jetpack autoloader: WordPress core runs uninstall.php standalone
* without loading the plugin's main file, so that autoloader is not guaranteed to have run.
* tests/Unit/UninstallerTest.php unit-tests that class; tests/Unit/UninstallTest.php exercises
* this script's wiring to it.
*/
declare(strict_types=1);
defined('WP_UNINSTALL_PLUGIN') || exit;
require_once __DIR__ . '/src/Uninstaller.php';
\LIT\MCP\Uninstaller::run();