atarim-visual-collaboration
/
third-party
/
backup
/
jetbackup
/
class-avcf-abilities-jetbackup-queue.php
class-avcf-abilities-jetbackup-backups.php
2 weeks ago
class-avcf-abilities-jetbackup-destinations.php
2 weeks ago
class-avcf-abilities-jetbackup-jobs.php
2 weeks ago
class-avcf-abilities-jetbackup-queue.php
2 weeks ago
class-avcf-abilities-jetbackup-restore.php
2 weeks ago
class-avcf-abilities-jetbackup-schedules.php
2 weeks ago
class-avcf-abilities-jetbackup-settings.php
2 weeks ago
class-avcf-abilities-jetbackup-system.php
2 weeks ago
class-avcf-jetbackup-detector.php
2 weeks ago
class-avcf-jetbackup-helpers.php
2 weeks ago
class-avcf-abilities-jetbackup-queue.php
250 lines
| 1 | <?php |
| 2 | /** |
| 3 | * JetBackup — Queue, Downloads & Alerts ability cluster. |
| 4 | * |
| 5 | * Namespaced atarim/jetbackup-* over JetBackup's queue/download/alert AJAX Calls: |
| 6 | * list queue items, abort / start-over / clear-completed queue items, |
| 7 | * list / delete downloads, list / clear alerts. |
| 8 | * |
| 9 | * (get-queue-item lives in the restore cluster since that's its primary use.) |
| 10 | * |
| 11 | * Gated by manage_options; delete-download requires confirm:true. Built on |
| 12 | * JetBackup 3.1.23.x; not runtime-tested here. |
| 13 | * |
| 14 | * @package atarim-visual-collaboration |
| 15 | */ |
| 16 | |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | class AVCF_Abilities_JetBackup_Queue extends AVCF_Abilities_Base { |
| 22 | |
| 23 | /** @var AVCF_JetBackup_Detector */ |
| 24 | private $detector; |
| 25 | |
| 26 | public function __construct() { |
| 27 | $this->detector = new AVCF_JetBackup_Detector(); |
| 28 | } |
| 29 | |
| 30 | public function register() { |
| 31 | if ( ! $this->detector->avcf_jetbackup_is_available() ) { |
| 32 | return; |
| 33 | } |
| 34 | $this->register_list_queue_items(); |
| 35 | $this->register_abort_queue_item(); |
| 36 | $this->register_start_over_queue_item(); |
| 37 | $this->register_clear_completed_queue_items(); |
| 38 | $this->register_list_downloads(); |
| 39 | $this->register_delete_download(); |
| 40 | $this->register_list_alerts(); |
| 41 | $this->register_clear_alerts(); |
| 42 | } |
| 43 | |
| 44 | /* ------------------------------ shared ----------------------------- */ |
| 45 | |
| 46 | private function ro_meta() { |
| 47 | return [ 'mcp' => [ 'public' => true, 'type' => 'tool' ], 'annotations' => [ 'readonly' => true, 'destructive' => false, 'idempotent' => true ] ]; |
| 48 | } |
| 49 | private function write_meta( $d = false ) { |
| 50 | return [ 'mcp' => [ 'public' => true, 'type' => 'tool' ], 'annotations' => [ 'readonly' => false, 'destructive' => (bool) $d, 'idempotent' => false ] ]; |
| 51 | } |
| 52 | public function can() { |
| 53 | return AVCF_JetBackup_Helpers::can_manage(); |
| 54 | } |
| 55 | private function out_schema() { |
| 56 | return [ |
| 57 | 'type' => 'object', |
| 58 | 'properties' => [ |
| 59 | 'success' => [ 'type' => 'boolean' ], |
| 60 | 'message' => [ 'type' => 'string' ], |
| 61 | 'data' => [ 'type' => 'object' ], |
| 62 | ], |
| 63 | 'required' => [ 'success', 'message' ], |
| 64 | ]; |
| 65 | } |
| 66 | private function pagination_schema() { |
| 67 | return [ |
| 68 | 'type' => 'object', |
| 69 | 'properties' => [ |
| 70 | 'skip' => [ 'type' => 'integer', 'minimum' => 0, 'default' => 0 ], |
| 71 | 'limit' => [ 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 50 ], |
| 72 | ], |
| 73 | 'additionalProperties' => false, |
| 74 | ]; |
| 75 | } |
| 76 | private function id_schema( $desc ) { |
| 77 | return [ |
| 78 | 'type' => 'object', |
| 79 | 'properties' => [ 'id' => [ 'type' => 'integer', 'minimum' => 1, 'description' => $desc ] ], |
| 80 | 'required' => [ 'id' ], |
| 81 | 'additionalProperties' => false, |
| 82 | ]; |
| 83 | } |
| 84 | private function no_input_schema() { |
| 85 | return [ 'type' => 'object', 'properties' => new \stdClass(), 'additionalProperties' => false ]; |
| 86 | } |
| 87 | private function missing_id( $what ) { |
| 88 | return [ 'success' => false, 'message' => sprintf( 'Missing id (%s).', $what ), 'data' => [] ]; |
| 89 | } |
| 90 | |
| 91 | /* -------------------------- list-queue-items ------------------------ */ |
| 92 | |
| 93 | private function register_list_queue_items() { |
| 94 | $self = $this; |
| 95 | wp_register_ability( 'atarim/jetbackup-list-queue-items', [ |
| 96 | 'label' => 'List JetBackup Queue Items', |
| 97 | 'category' => 'atarim', |
| 98 | 'description' => 'List JetBackup queue items — running and recent backup/restore/download/etc. tasks (paginated). Optional skip, limit (1-100, default 50).', |
| 99 | 'input_schema' => $this->pagination_schema(), |
| 100 | 'output_schema' => $this->out_schema(), |
| 101 | 'execute_callback' => function( $input = [] ) { |
| 102 | return AVCF_JetBackup_Helpers::invoke( 'ListQueueItems', AVCF_JetBackup_Helpers::paginate( (array) $input ) ); |
| 103 | }, |
| 104 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 105 | 'meta' => $this->ro_meta(), |
| 106 | ] ); |
| 107 | } |
| 108 | |
| 109 | /* -------------------------- abort-queue-item ------------------------ */ |
| 110 | |
| 111 | private function register_abort_queue_item() { |
| 112 | $self = $this; |
| 113 | wp_register_ability( 'atarim/jetbackup-abort-queue-item', [ |
| 114 | 'label' => 'Abort JetBackup Queue Item', |
| 115 | 'category' => 'atarim', |
| 116 | 'description' => 'Abort (cancel) a running or pending queue item by id.', |
| 117 | 'input_schema' => $this->id_schema( 'Queue item id to abort.' ), |
| 118 | 'output_schema' => $this->out_schema(), |
| 119 | 'execute_callback' => function( $input = [] ) { |
| 120 | $input = (array) $input; |
| 121 | if ( empty( $input['id'] ) ) { return $this->missing_id( 'queue item id to abort' ); } |
| 122 | return AVCF_JetBackup_Helpers::invoke( 'AbortQueueItem', AVCF_JetBackup_Helpers::id_payload( $input ) ); |
| 123 | }, |
| 124 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 125 | 'meta' => $this->write_meta( false ), |
| 126 | ] ); |
| 127 | } |
| 128 | |
| 129 | /* ------------------------ start-over-queue-item --------------------- */ |
| 130 | |
| 131 | private function register_start_over_queue_item() { |
| 132 | $self = $this; |
| 133 | wp_register_ability( 'atarim/jetbackup-start-over-queue-item', [ |
| 134 | 'label' => 'Restart JetBackup Queue Item', |
| 135 | 'category' => 'atarim', |
| 136 | 'description' => 'Restart a queue item from the beginning by id (e.g. retry a failed task).', |
| 137 | 'input_schema' => $this->id_schema( 'Queue item id to restart.' ), |
| 138 | 'output_schema' => $this->out_schema(), |
| 139 | 'execute_callback' => function( $input = [] ) { |
| 140 | $input = (array) $input; |
| 141 | if ( empty( $input['id'] ) ) { return $this->missing_id( 'queue item id to restart' ); } |
| 142 | return AVCF_JetBackup_Helpers::invoke( 'StartOverQueueItem', AVCF_JetBackup_Helpers::id_payload( $input ) ); |
| 143 | }, |
| 144 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 145 | 'meta' => $this->write_meta( false ), |
| 146 | ] ); |
| 147 | } |
| 148 | |
| 149 | /* -------------------- clear-completed-queue-items ------------------- */ |
| 150 | |
| 151 | private function register_clear_completed_queue_items() { |
| 152 | $self = $this; |
| 153 | wp_register_ability( 'atarim/jetbackup-clear-completed-queue-items', [ |
| 154 | 'label' => 'Clear Completed JetBackup Queue Items', |
| 155 | 'category' => 'atarim', |
| 156 | 'description' => 'Clear all completed items from the JetBackup queue.', |
| 157 | 'input_schema' => $this->no_input_schema(), |
| 158 | 'output_schema' => $this->out_schema(), |
| 159 | 'execute_callback' => function( $input = [] ) { |
| 160 | return AVCF_JetBackup_Helpers::invoke( 'ClearCompletedQueueItems', [] ); |
| 161 | }, |
| 162 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 163 | 'meta' => $this->write_meta( false ), |
| 164 | ] ); |
| 165 | } |
| 166 | |
| 167 | /* --------------------------- list-downloads ------------------------- */ |
| 168 | |
| 169 | private function register_list_downloads() { |
| 170 | $self = $this; |
| 171 | wp_register_ability( 'atarim/jetbackup-list-downloads', [ |
| 172 | 'label' => 'List JetBackup Downloads', |
| 173 | 'category' => 'atarim', |
| 174 | 'description' => 'List prepared backup downloads (from jetbackup-download-backup), paginated. Optional skip, limit (1-100, default 50).', |
| 175 | 'input_schema' => $this->pagination_schema(), |
| 176 | 'output_schema' => $this->out_schema(), |
| 177 | 'execute_callback' => function( $input = [] ) { |
| 178 | return AVCF_JetBackup_Helpers::invoke( 'ListDownloads', AVCF_JetBackup_Helpers::paginate( (array) $input ) ); |
| 179 | }, |
| 180 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 181 | 'meta' => $this->ro_meta(), |
| 182 | ] ); |
| 183 | } |
| 184 | |
| 185 | /* --------------------------- delete-download ------------------------ */ |
| 186 | |
| 187 | private function register_delete_download() { |
| 188 | $self = $this; |
| 189 | wp_register_ability( 'atarim/jetbackup-delete-download', [ |
| 190 | 'label' => 'Delete JetBackup Download', |
| 191 | 'category' => 'atarim', |
| 192 | 'description' => 'Delete a prepared download by id. Destructive: requires confirm:true.', |
| 193 | 'input_schema' => [ |
| 194 | 'type' => 'object', |
| 195 | 'properties' => [ |
| 196 | 'id' => [ 'type' => 'integer', 'minimum' => 1, 'description' => 'Download id to delete.' ], |
| 197 | 'confirm' => [ 'type' => 'boolean', 'description' => 'Must be true to proceed.' ], |
| 198 | ], |
| 199 | 'required' => [ 'id' ], |
| 200 | 'additionalProperties' => false, |
| 201 | ], |
| 202 | 'output_schema' => $this->out_schema(), |
| 203 | 'execute_callback' => function( $input = [] ) { |
| 204 | $input = (array) $input; |
| 205 | if ( empty( $input['id'] ) ) { return $this->missing_id( 'download id to delete' ); } |
| 206 | if ( ! AVCF_JetBackup_Helpers::confirmed( $input ) ) { return AVCF_JetBackup_Helpers::confirm_required_response( 'delete download' ); } |
| 207 | return AVCF_JetBackup_Helpers::invoke( 'DeleteDownload', AVCF_JetBackup_Helpers::id_payload( $input ) ); |
| 208 | }, |
| 209 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 210 | 'meta' => $this->write_meta( true ), |
| 211 | ] ); |
| 212 | } |
| 213 | |
| 214 | /* ---------------------------- list-alerts --------------------------- */ |
| 215 | |
| 216 | private function register_list_alerts() { |
| 217 | $self = $this; |
| 218 | wp_register_ability( 'atarim/jetbackup-list-alerts', [ |
| 219 | 'label' => 'List JetBackup Alerts', |
| 220 | 'category' => 'atarim', |
| 221 | 'description' => 'List JetBackup alerts/notifications (paginated). Optional skip, limit (1-100, default 50).', |
| 222 | 'input_schema' => $this->pagination_schema(), |
| 223 | 'output_schema' => $this->out_schema(), |
| 224 | 'execute_callback' => function( $input = [] ) { |
| 225 | return AVCF_JetBackup_Helpers::invoke( 'ListAlerts', AVCF_JetBackup_Helpers::paginate( (array) $input ) ); |
| 226 | }, |
| 227 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 228 | 'meta' => $this->ro_meta(), |
| 229 | ] ); |
| 230 | } |
| 231 | |
| 232 | /* ---------------------------- clear-alerts -------------------------- */ |
| 233 | |
| 234 | private function register_clear_alerts() { |
| 235 | $self = $this; |
| 236 | wp_register_ability( 'atarim/jetbackup-clear-alerts', [ |
| 237 | 'label' => 'Clear JetBackup Alerts', |
| 238 | 'category' => 'atarim', |
| 239 | 'description' => 'Clear all JetBackup alerts.', |
| 240 | 'input_schema' => $this->no_input_schema(), |
| 241 | 'output_schema' => $this->out_schema(), |
| 242 | 'execute_callback' => function( $input = [] ) { |
| 243 | return AVCF_JetBackup_Helpers::invoke( 'ClearAlerts', [] ); |
| 244 | }, |
| 245 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 246 | 'meta' => $this->write_meta( false ), |
| 247 | ] ); |
| 248 | } |
| 249 | } |
| 250 |