| 1 |
<?php |
| 2 |
/** |
| 3 |
* utilities module — the plugin's one cleanup service, and the generic |
| 4 |
* "Utilities" settings tab that surfaces it. |
| 5 |
* |
| 6 |
* ## Two halves, one wall |
| 7 |
* |
| 8 |
* `Cleanup/` is the engine: a contribution registry, the single cleanup |
| 9 |
* schedule, a guarded remover, the retention policy, and a run journal. It has |
| 10 |
* ZERO knowledge of the tab — no UI imports, no asset code — and works headless |
| 11 |
* if the JS entry is removed. `REST/` is the only bridge to `assets/js/`. |
| 12 |
* |
| 13 |
* ## Why the engine names no contributor |
| 14 |
* |
| 15 |
* full-site-import ─┐ |
| 16 |
* block-patterns ───┼─register_classes()─► utilities (TaskRegistry) |
| 17 |
* mcp-server ───────┘ │ |
| 18 |
* └─► one daily schedule |
| 19 |
* |
| 20 |
* A direct call list would invert that arrow: this module would depend on every |
| 21 |
* feature that produces disposable data, could not boot where one of them is |
| 22 |
* disabled, and would need editing for every future feature. Same reasoning, |
| 23 |
* and the same shape, as `modules/mcp-core/`. This module therefore declares NO |
| 24 |
* dependencies; contributors declare `'utilities'`. |
| 25 |
* |
| 26 |
* ## Nothing is deleted until someone says so |
| 27 |
* |
| 28 |
* Cleanup runs in dry-run until an administrator confirms activation once per |
| 29 |
* site. The first run on an existing site would otherwise reclaim a backlog of |
| 30 |
* abandoned imports — often several gigabytes — with no warning. |
| 31 |
* |
| 32 |
* @package Templately |
| 33 |
*/ |
| 34 |
|
| 35 |
namespace Templately\Modules\Utilities; |
| 36 |
|
| 37 |
use Templately\Core\Module_Base; |
| 38 |
use Templately\Modules\Utilities\Cleanup\Scheduler; |
| 39 |
use Templately\Modules\Utilities\Notice\ActivationNotice; |
| 40 |
use Templately\Modules\Utilities\REST\UtilitiesController; |
| 41 |
use Templately\Modules\Utilities\Cleanup\TaskRegistry; |
| 42 |
use Templately\Modules\Utilities\Cleanup\Tasks\TransientsTask; |
| 43 |
|
| 44 |
class Module extends Module_Base { |
| 45 |
|
| 46 |
public function get_name(): string { |
| 47 |
return 'utilities'; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Deliberately empty. Everything points AT this module; it points at |
| 52 |
* nothing. See the file header. |
| 53 |
*/ |
| 54 |
public function get_dependencies(): array { |
| 55 |
return []; |
| 56 |
} |
| 57 |
|
| 58 |
protected function init_hooks(): void { |
| 59 |
// The one cleanup schedule, plus retirement of the schedules this |
| 60 |
// module supersedes. Registers hooks only — the registry stays lazy, so |
| 61 |
// a request that never runs cleanup constructs no tasks. |
| 62 |
Scheduler::boot(); |
| 63 |
|
| 64 |
$this->inject_settings(); |
| 65 |
|
| 66 |
// The gate is only useful if it can be found. This prompt lives outside |
| 67 |
// the settings page precisely because the sites that leak hardest are |
| 68 |
// the ones whose administrator never opens it. |
| 69 |
ActivationNotice::boot(); |
| 70 |
|
| 71 |
// The engine's own built-in task. Registered through the same public seam |
| 72 |
// every other contributor uses — the registry has no privileged list. |
| 73 |
add_action( |
| 74 |
TaskRegistry::COLLECT_ACTION, |
| 75 |
static function ( TaskRegistry $registry ) { |
| 76 |
$registry->register_classes( [ TransientsTask::class ] ); |
| 77 |
} |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Tell the SPA this module booted. |
| 83 |
* |
| 84 |
* A field on the EXISTING localized settings object, not a new window |
| 85 |
* carrier — constitution XIII permits exactly two of those, and this is |
| 86 |
* injected data rather than a first-party runtime singleton. The JS entry |
| 87 |
* self-gates on it, so the tab is inert wherever the module did not boot. |
| 88 |
*/ |
| 89 |
private function inject_settings(): void { |
| 90 |
add_filter( |
| 91 |
'templately_admin_localized_data', |
| 92 |
static function ( $data ) { |
| 93 |
if ( ! is_array( $data ) ) { |
| 94 |
return $data; |
| 95 |
} |
| 96 |
|
| 97 |
$data['utilities'] = [ |
| 98 |
'canManage' => current_user_can( 'manage_options' ), |
| 99 |
]; |
| 100 |
|
| 101 |
return $data; |
| 102 |
} |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Routes are registered from here — `rest_api_init` — never the constructor. |
| 108 |
* `register_rest_route()` silently drops a route registered on |
| 109 |
* `plugins_loaded` with a `_doing_it_wrong` notice. |
| 110 |
*/ |
| 111 |
public function register_rest_routes(): void { |
| 112 |
UtilitiesController::get_instance()->register_routes(); |
| 113 |
} |
| 114 |
} |
| 115 |
|