atarim-visual-collaboration
/
third-party
/
backup
/
jetbackup
/
class-avcf-abilities-jetbackup-schedules.php
class-avcf-abilities-jetbackup-backups.php
2 days 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 days ago
class-avcf-abilities-jetbackup-restore-point.php
2 days ago
class-avcf-abilities-jetbackup-restore.php
2 days 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 days ago
class-avcf-abilities-jetbackup-schedules.php
203 lines
| 1 | <?php |
| 2 | /** |
| 3 | * JetBackup — Schedules ability cluster ("back up every hour/day/week"). |
| 4 | * |
| 5 | * Namespaced atarim/jetbackup-* over JetBackup's schedule AJAX Calls: |
| 6 | * list/get schedules, save (create/edit), delete. |
| 7 | * |
| 8 | * ManageSchedule is isset-gated, so save-schedule is safe for partial edits. |
| 9 | * Omit id (or 0) to create; pass a real id to edit. |
| 10 | * |
| 11 | * Gated by manage_options; delete requires confirm:true. Built on JetBackup |
| 12 | * 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_Schedules 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_schedules(); |
| 35 | $this->register_get_schedule(); |
| 36 | $this->register_save_schedule(); |
| 37 | $this->register_delete_schedule(); |
| 38 | } |
| 39 | |
| 40 | /* ------------------------------ shared ----------------------------- */ |
| 41 | |
| 42 | private function ro_meta() { |
| 43 | return [ 'mcp' => [ 'public' => true, 'type' => 'tool' ], 'annotations' => [ 'readonly' => true, 'destructive' => false, 'idempotent' => true ] ]; |
| 44 | } |
| 45 | private function write_meta( $d = false ) { |
| 46 | return [ 'mcp' => [ 'public' => true, 'type' => 'tool' ], 'annotations' => [ 'readonly' => false, 'destructive' => (bool) $d, 'idempotent' => false ] ]; |
| 47 | } |
| 48 | public function can() { |
| 49 | return AVCF_JetBackup_Helpers::can_manage(); |
| 50 | } |
| 51 | private function out_schema() { |
| 52 | return [ |
| 53 | 'type' => 'object', |
| 54 | 'properties' => [ |
| 55 | 'success' => [ 'type' => 'boolean' ], |
| 56 | 'message' => [ 'type' => 'string' ], |
| 57 | 'data' => [ 'type' => 'object' ], |
| 58 | ], |
| 59 | 'required' => [ 'success', 'message' ], |
| 60 | ]; |
| 61 | } |
| 62 | private function id_schema( $desc ) { |
| 63 | return [ |
| 64 | 'type' => 'object', |
| 65 | 'properties' => [ 'id' => [ 'type' => 'integer', 'minimum' => 1, 'description' => $desc ] ], |
| 66 | 'required' => [ 'id' ], |
| 67 | 'additionalProperties' => false, |
| 68 | ]; |
| 69 | } |
| 70 | private function missing_id( $what ) { |
| 71 | return [ 'success' => false, 'message' => sprintf( 'Missing id (%s).', $what ), 'data' => [] ]; |
| 72 | } |
| 73 | |
| 74 | /* --------------------------- list-schedules ------------------------- */ |
| 75 | |
| 76 | private function register_list_schedules() { |
| 77 | $self = $this; |
| 78 | wp_register_ability( 'atarim/jetbackup-list-schedules', [ |
| 79 | 'label' => 'List JetBackup Schedules', |
| 80 | 'category' => 'atarim', |
| 81 | 'description' => 'List configured JetBackup backup schedules (paginated). Optional skip, limit (1-100, default 50).', |
| 82 | 'input_schema' => [ |
| 83 | 'type' => 'object', |
| 84 | 'properties' => [ |
| 85 | 'skip' => [ 'type' => 'integer', 'minimum' => 0, 'default' => 0 ], |
| 86 | 'limit' => [ 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 50 ], |
| 87 | ], |
| 88 | 'additionalProperties' => false, |
| 89 | ], |
| 90 | 'output_schema' => $this->out_schema(), |
| 91 | 'execute_callback' => function( $input = [] ) { |
| 92 | return AVCF_JetBackup_Helpers::invoke( 'ListSchedules', AVCF_JetBackup_Helpers::paginate( (array) $input ) ); |
| 93 | }, |
| 94 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 95 | 'meta' => $this->ro_meta(), |
| 96 | ] ); |
| 97 | } |
| 98 | |
| 99 | /* ---------------------------- get-schedule -------------------------- */ |
| 100 | |
| 101 | private function register_get_schedule() { |
| 102 | $self = $this; |
| 103 | wp_register_ability( 'atarim/jetbackup-get-schedule', [ |
| 104 | 'label' => 'Get JetBackup Schedule', |
| 105 | 'category' => 'atarim', |
| 106 | 'description' => 'Get a single JetBackup schedule by id.', |
| 107 | 'input_schema' => $this->id_schema( 'Schedule id.' ), |
| 108 | 'output_schema' => $this->out_schema(), |
| 109 | 'execute_callback' => function( $input = [] ) { |
| 110 | $input = (array) $input; |
| 111 | if ( empty( $input['id'] ) ) { return $this->missing_id( 'schedule id' ); } |
| 112 | return AVCF_JetBackup_Helpers::invoke( 'GetSchedule', AVCF_JetBackup_Helpers::id_payload( $input ) ); |
| 113 | }, |
| 114 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 115 | 'meta' => $this->ro_meta(), |
| 116 | ] ); |
| 117 | } |
| 118 | |
| 119 | /* ---------------------------- save-schedule ------------------------- */ |
| 120 | |
| 121 | private function register_save_schedule() { |
| 122 | $self = $this; |
| 123 | wp_register_ability( 'atarim/jetbackup-save-schedule', [ |
| 124 | 'label' => 'Save JetBackup Schedule', |
| 125 | 'category' => 'atarim', |
| 126 | 'description' => 'Create or update a backup schedule. Omit id (or 0) to create; pass a real id to edit. Only fields provided are changed. ' |
| 127 | . 'type: 1=Hourly, 2=Daily, 3=Weekly, 4=Monthly, 6=After Job Done. ' |
| 128 | . 'intervals depends on type — Hourly: one of 1,2,3,4,6,8,12 (every N hours); ' |
| 129 | . 'Daily: array of weekday numbers 1-7; Monthly: one of 1,7,14,21,28 (day of month); Weekly: 1. ' |
| 130 | . 'backup_id attaches the schedule to that backup job.', |
| 131 | 'input_schema' => [ |
| 132 | 'type' => 'object', |
| 133 | 'properties' => [ |
| 134 | 'id' => [ 'type' => 'integer', 'minimum' => 0, 'description' => 'Schedule id to edit; omit or 0 to create.' ], |
| 135 | 'name' => [ 'type' => 'string' ], |
| 136 | 'type' => [ 'type' => 'integer', 'enum' => [ 1, 2, 3, 4, 6 ], 'description' => '1 Hourly, 2 Daily, 3 Weekly, 4 Monthly, 6 After Job Done.' ], |
| 137 | 'intervals' => [ 'oneOf' => [ [ 'type' => 'integer' ], [ 'type' => 'array', 'items' => [ 'type' => 'integer' ] ] ], 'description' => 'Interval for the type (see description).' ], |
| 138 | 'backup_id' => [ 'type' => 'integer', 'description' => 'Backup job id this schedule runs.' ], |
| 139 | ], |
| 140 | 'additionalProperties' => false, |
| 141 | ], |
| 142 | 'output_schema' => $this->out_schema(), |
| 143 | 'execute_callback' => function( $input = [] ) { |
| 144 | $input = (array) $input; |
| 145 | $payload = []; |
| 146 | |
| 147 | if ( ! empty( $input['id'] ) ) { |
| 148 | $payload[ AVCF_JetBackup_Helpers::ID_FIELD ] = (int) $input['id']; |
| 149 | } |
| 150 | if ( array_key_exists( 'name', $input ) ) { |
| 151 | $payload['name'] = (string) $input['name']; |
| 152 | } |
| 153 | if ( array_key_exists( 'type', $input ) ) { |
| 154 | $payload['type'] = (int) $input['type']; |
| 155 | } |
| 156 | if ( array_key_exists( 'intervals', $input ) ) { |
| 157 | $iv = $input['intervals']; |
| 158 | $payload['intervals'] = is_array( $iv ) ? array_values( array_map( 'intval', $iv ) ) : (int) $iv; |
| 159 | } |
| 160 | if ( array_key_exists( 'backup_id', $input ) ) { |
| 161 | $payload['backup_id'] = (int) $input['backup_id']; |
| 162 | } |
| 163 | |
| 164 | if ( empty( $payload ) ) { |
| 165 | return [ 'success' => false, 'message' => 'No schedule fields provided.', 'data' => [] ]; |
| 166 | } |
| 167 | return AVCF_JetBackup_Helpers::invoke( 'ManageSchedule', $payload ); |
| 168 | }, |
| 169 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 170 | 'meta' => $this->write_meta( false ), |
| 171 | ] ); |
| 172 | } |
| 173 | |
| 174 | /* --------------------------- delete-schedule ------------------------ */ |
| 175 | |
| 176 | private function register_delete_schedule() { |
| 177 | $self = $this; |
| 178 | wp_register_ability( 'atarim/jetbackup-delete-schedule', [ |
| 179 | 'label' => 'Delete JetBackup Schedule', |
| 180 | 'category' => 'atarim', |
| 181 | 'description' => 'Delete a backup schedule by id. Destructive: requires confirm:true.', |
| 182 | 'input_schema' => [ |
| 183 | 'type' => 'object', |
| 184 | 'properties' => [ |
| 185 | 'id' => [ 'type' => 'integer', 'minimum' => 1, 'description' => 'Schedule id to delete.' ], |
| 186 | 'confirm' => [ 'type' => 'boolean', 'description' => 'Must be true to proceed.' ], |
| 187 | ], |
| 188 | 'required' => [ 'id' ], |
| 189 | 'additionalProperties' => false, |
| 190 | ], |
| 191 | 'output_schema' => $this->out_schema(), |
| 192 | 'execute_callback' => function( $input = [] ) { |
| 193 | $input = (array) $input; |
| 194 | if ( empty( $input['id'] ) ) { return $this->missing_id( 'schedule id to delete' ); } |
| 195 | if ( ! AVCF_JetBackup_Helpers::confirmed( $input ) ) { return AVCF_JetBackup_Helpers::confirm_required_response( 'delete schedule' ); } |
| 196 | return AVCF_JetBackup_Helpers::invoke( 'DeleteSchedule', AVCF_JetBackup_Helpers::id_payload( $input ) ); |
| 197 | }, |
| 198 | 'permission_callback' => function() use ( $self ) { return $self->can(); }, |
| 199 | 'meta' => $this->write_meta( true ), |
| 200 | ] ); |
| 201 | } |
| 202 | } |
| 203 |