| 1 |
<?php |
| 2 |
/** |
| 3 |
* Reclaim expired delegated-approval records. |
| 4 |
* |
| 5 |
* @package Templately |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Templately\Modules\McpServer\Cleanup; |
| 9 |
|
| 10 |
use Templately\Modules\McpServer\Auth\OAuth\RecordStore; |
| 11 |
use Templately\Modules\Utilities\Cleanup\CleanupTask; |
| 12 |
use Templately\Modules\Utilities\Cleanup\Context; |
| 13 |
use Templately\Modules\Utilities\Cleanup\RetentionKind; |
| 14 |
use Templately\Modules\Utilities\Cleanup\TaskRegistry; |
| 15 |
use Templately\Modules\Utilities\Cleanup\TaskResult; |
| 16 |
|
| 17 |
/** |
| 18 |
* This module used to run its OWN daily WP-Cron sweep |
| 19 |
* (`templately_mcp_oauth_sweep`). It now contributes to the one shared sweep |
| 20 |
* instead — two cleaners on separate cadences with no shared run claim is |
| 21 |
* exactly what spec 052 exists to end. |
| 22 |
* |
| 23 |
* INTRINSIC retention, not age: each record carries its own `expires_at`, and |
| 24 |
* expiry is enforced on read as well. Applying the shared age window here would |
| 25 |
* either be ignored or would reclaim credentials that are still valid. |
| 26 |
* |
| 27 |
* `RecordStore::sweep()` itself is unchanged and still lives in this module — |
| 28 |
* only the scheduling moved. |
| 29 |
*/ |
| 30 |
class OAuthRecordsTask implements CleanupTask { |
| 31 |
|
| 32 |
public static function register(): void { |
| 33 |
add_action( |
| 34 |
TaskRegistry::COLLECT_ACTION, |
| 35 |
static function ( TaskRegistry $registry ) { |
| 36 |
$registry->register_classes( [ self::class ] ); |
| 37 |
} |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
public function descriptor(): array { |
| 42 |
return [ |
| 43 |
'id' => 'mcp-oauth-records', |
| 44 |
'label' => __( 'Expired agent approvals', 'templately' ), |
| 45 |
'group' => 'cache', |
| 46 |
'scope' => 'records', |
| 47 |
'retention_kind' => RetentionKind::INTRINSIC, |
| 48 |
'destructive' => true, |
| 49 |
'schedulable' => true, |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
public function estimate( Context $context ): TaskResult { |
| 54 |
return TaskResult::empty()->add( RecordStore::count_expired(), 0 ); |
| 55 |
} |
| 56 |
|
| 57 |
public function run( Context $context ): TaskResult { |
| 58 |
$expired = RecordStore::count_expired(); |
| 59 |
|
| 60 |
if ( $context->is_dry_run() ) { |
| 61 |
return TaskResult::empty()->add( $expired, 0 ); |
| 62 |
} |
| 63 |
|
| 64 |
RecordStore::sweep(); |
| 65 |
|
| 66 |
return TaskResult::empty()->add( $expired, 0 ); |
| 67 |
} |
| 68 |
} |
| 69 |
|