Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.77% |
509 / 567 |
|
74.07% |
20 / 27 |
CRAP | |
0.00% |
0 / 1 |
| Backup_Abilities | |
90.09% |
509 / 565 |
|
74.07% |
20 / 27 |
184.93 | |
0.00% |
0 / 1 |
| enabled_by_default | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_category_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_category_definition | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| register_category | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_abilities | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| backup_is_loaded | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| get_abilities | |
100.00% |
300 / 300 |
|
100.00% |
1 / 1 |
1 | |||
| can_view_backups | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| can_manage_backups | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute_get_backup_overview | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| execute_list_backups | |
49.02% |
25 / 51 |
|
0.00% |
0 / 1 |
167.68 | |||
| execute_list_restores | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| pick_backup_near_timestamp | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
11 | |||
| extract_rewindable_items | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
8.06 | |||
| summarize_backup_event | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
12.02 | |||
| map_event_status | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| event_has_warnings | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| parse_timestamp | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
7 | |||
| execute_request_backup | |
81.82% |
18 / 22 |
|
0.00% |
0 / 1 |
7.29 | |||
| execute_get_backup_request_status | |
16.00% |
4 / 25 |
|
0.00% |
0 / 1 |
167.73 | |||
| unwrap_response | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| apply_id_or_pagination | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
7 | |||
| summarize_last_backup | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| summarize_backup | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| summarize_restore | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| summarize_schedule | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| summarize_storage | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
14 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack Backup Abilities Registration. |
| 4 | * |
| 5 | * Registers Jetpack Backup abilities with the WordPress Abilities API so AI |
| 6 | * agents can read backup status and trigger on-demand backups through the |
| 7 | * standard `wp-abilities/v1` REST surface. |
| 8 | * |
| 9 | * @package automattic/jetpack-backup |
| 10 | */ |
| 11 | |
| 12 | namespace Automattic\Jetpack\Backup\V0005\Abilities; |
| 13 | |
| 14 | use Automattic\Jetpack\Backup\V0005\Jetpack_Backup; |
| 15 | use Automattic\Jetpack\My_Jetpack\Products\Backup as My_Jetpack_Backup; |
| 16 | use Automattic\Jetpack\WP_Abilities\Registrar; |
| 17 | use WP_Error; |
| 18 | use WP_REST_Response; |
| 19 | |
| 20 | if ( ! defined( 'ABSPATH' ) ) { |
| 21 | exit( 0 ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Registers Jetpack Backup abilities with the WordPress Abilities API. |
| 26 | * |
| 27 | * Exposes a small, agent-friendly surface for site backups: |
| 28 | * |
| 29 | * - `jetpack-backup/get-backup-overview` — single-call site backup health snapshot. |
| 30 | * - `jetpack-backup/list-backups` — recent backups with optional id/pagination filters. |
| 31 | * - `jetpack-backup/list-restores` — recent restores with optional id/pagination filters. |
| 32 | * - `jetpack-backup/request-backup` — enqueue an on-demand backup. |
| 33 | * - `jetpack-backup/get-backup-request-status` — track one requested backup. |
| 34 | */ |
| 35 | class Backup_Abilities extends Registrar { |
| 36 | |
| 37 | const PER_PAGE_DEFAULT = 20; |
| 38 | const PER_PAGE_MAX = 100; |
| 39 | |
| 40 | private const BACKUP_REQUEST_ID_PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/D'; |
| 41 | |
| 42 | /** |
| 43 | * {@inheritDoc} |
| 44 | */ |
| 45 | protected static function enabled_by_default(): bool { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Return the ability category slug. |
| 51 | * |
| 52 | * @return string |
| 53 | */ |
| 54 | public static function get_category_slug(): string { |
| 55 | return 'site'; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Required by the abstract parent, but unused: the `site` category is |
| 60 | * already registered upstream (WordPress core / wpcom), so we don't |
| 61 | * re-declare it. Kept so the contract holds if a consumer ever asks for |
| 62 | * the definition we *would* use. |
| 63 | * |
| 64 | * @return array |
| 65 | */ |
| 66 | public static function get_category_definition(): array { |
| 67 | return array( |
| 68 | 'label' => __( 'Site', 'jetpack-backup-pkg' ), |
| 69 | 'description' => __( 'Site-wide management abilities (registered upstream).', 'jetpack-backup-pkg' ), |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Override the Registrar lifecycle so the backup abilities only register |
| 75 | * on sites that actually have a Jetpack Backup product provisioned. |
| 76 | * There's no point exposing tool surfaces an agent can never use, and on free |
| 77 | * sites the upstream wpcom endpoints either silently accept writes (e.g. |
| 78 | * `request-backup` reported `enqueued: true`) or return null payloads |
| 79 | * that confuse callers. |
| 80 | * |
| 81 | * The `site` category is registered upstream by WordPress core / wpcom, |
| 82 | * so this class never tries to register a category — `register_category` |
| 83 | * is a no-op even though the parent hooks it. |
| 84 | * |
| 85 | * @return void |
| 86 | */ |
| 87 | public static function register_category() { |
| 88 | // No-op: `site` is registered upstream; re-registering would either |
| 89 | // no-op or trigger "already registered" notices. |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Register every ability returned by `get_abilities()`, gated on the |
| 94 | * Backup product being loaded. See `register_category()` for why we |
| 95 | * never register a category from here. |
| 96 | * |
| 97 | * @return void |
| 98 | */ |
| 99 | public static function register_abilities() { |
| 100 | if ( ! self::backup_is_loaded() ) { |
| 101 | return; |
| 102 | } |
| 103 | parent::register_abilities(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Is the Jetpack Backup product actually loaded on this site? |
| 108 | * |
| 109 | * True when a plugin that ships Backup is active and the site has a Backup plan. Deliberately |
| 110 | * not `My_Jetpack\Products\Backup::is_active()`: next to an older Jetpack plugin that still |
| 111 | * follows the backup module, while backups keep running on WordPress.com. The plan lookup |
| 112 | * is cached in `MY_JETPACK_SITE_FEATURES_TRANSIENT_KEY`. |
| 113 | * |
| 114 | * The `jetpack_backup_abilities_should_load` filter lets consumers and |
| 115 | * tests override the answer without round-tripping through the My |
| 116 | * Jetpack product class. |
| 117 | * |
| 118 | * @return bool |
| 119 | */ |
| 120 | private static function backup_is_loaded(): bool { |
| 121 | $default = class_exists( My_Jetpack_Backup::class ) |
| 122 | && My_Jetpack_Backup::is_plugin_active() |
| 123 | && My_Jetpack_Backup::has_any_plan_for_product(); |
| 124 | |
| 125 | /** |
| 126 | * Filters whether the Jetpack Backup abilities should register on |
| 127 | * this site. Defaults to whether Backup's plugin is active and the site has a Backup plan. |
| 128 | * |
| 129 | * @since 0.1.0 |
| 130 | * |
| 131 | * @param bool $should_load Whether to register the backup abilities. |
| 132 | */ |
| 133 | return (bool) apply_filters( 'jetpack_backup_abilities_should_load', $default ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Return the abilities this Registrar exposes, keyed by slug. |
| 138 | * |
| 139 | * @return array<string, array<string, mixed>> |
| 140 | */ |
| 141 | public static function get_abilities(): array { |
| 142 | // `id` is the rewind_id (a timestamp-fractional string like |
| 143 | // "1752860369.781") — the single cross-system identifier exposed by |
| 144 | // the wpcom rewind, restore, and activity-log APIs. Use it whenever |
| 145 | // referring to a specific backup across abilities. |
| 146 | // |
| 147 | // For in-progress backup attempts the rewind_id isn't assigned yet, |
| 148 | // in which case `id` is null. Such backups can't be looked up by id |
| 149 | // anywhere — wait for completion and re-query. |
| 150 | $backup_item_schema = array( |
| 151 | 'type' => 'object', |
| 152 | 'properties' => array( |
| 153 | 'id' => array( 'type' => array( 'string', 'null' ) ), |
| 154 | 'started' => array( 'type' => array( 'string', 'null' ) ), |
| 155 | 'last_updated' => array( 'type' => array( 'string', 'null' ) ), |
| 156 | 'status' => array( 'type' => array( 'string', 'null' ) ), |
| 157 | 'period' => array( 'type' => array( 'string', 'integer', 'null' ) ), |
| 158 | 'is_rewindable' => array( 'type' => array( 'boolean', 'null' ) ), |
| 159 | 'has_warnings' => array( 'type' => array( 'boolean', 'null' ) ), |
| 160 | ), |
| 161 | ); |
| 162 | |
| 163 | // Same convention applies to restores: `id` is the rewind_id of the |
| 164 | // backup being restored to. |
| 165 | $restore_item_schema = array( |
| 166 | 'type' => 'object', |
| 167 | 'properties' => array( |
| 168 | 'id' => array( 'type' => array( 'string', 'null' ) ), |
| 169 | 'started' => array( 'type' => array( 'string', 'null' ) ), |
| 170 | 'last_updated' => array( 'type' => array( 'string', 'null' ) ), |
| 171 | 'status' => array( 'type' => array( 'string', 'null' ) ), |
| 172 | 'progress' => array( 'type' => array( 'integer', 'null' ) ), |
| 173 | ), |
| 174 | ); |
| 175 | |
| 176 | return array( |
| 177 | 'jetpack-backup/get-backup-overview' => array( |
| 178 | 'label' => __( 'Get backup overview', 'jetpack-backup-pkg' ), |
| 179 | 'description' => __( |
| 180 | 'Return a single-call snapshot of the site backup state: { last_backup, recent_backup_count, schedule, storage }. Use this to answer "is my site protected?" before deciding whether to call list-backups, list-restores, or request-backup. Read-only and idempotent. Fields whose backing service is unreachable come back as null rather than failing the call. Requires the manage_options capability.', |
| 181 | 'jetpack-backup-pkg' |
| 182 | ), |
| 183 | 'input_schema' => array( |
| 184 | 'type' => 'object', |
| 185 | 'default' => array(), |
| 186 | 'properties' => array(), |
| 187 | 'additionalProperties' => false, |
| 188 | ), |
| 189 | 'output_schema' => array( |
| 190 | 'type' => 'object', |
| 191 | 'properties' => array( |
| 192 | 'recent_backup_count' => array( 'type' => array( 'integer', 'null' ) ), |
| 193 | 'last_backup' => array( |
| 194 | 'type' => array( 'object', 'null' ), |
| 195 | 'properties' => array( |
| 196 | 'id' => array( 'type' => array( 'string', 'null' ) ), |
| 197 | 'last_updated' => array( 'type' => array( 'string', 'null' ) ), |
| 198 | 'status' => array( 'type' => array( 'string', 'null' ) ), |
| 199 | 'is_rewindable' => array( 'type' => array( 'boolean', 'null' ) ), |
| 200 | 'has_warnings' => array( 'type' => array( 'boolean', 'null' ) ), |
| 201 | ), |
| 202 | ), |
| 203 | // Hour only: no WordPress.com endpoint carries a minute, |
| 204 | // so a `minute` field could only ever be null. |
| 205 | 'schedule' => array( |
| 206 | 'type' => array( 'object', 'null' ), |
| 207 | 'properties' => array( |
| 208 | 'hour' => array( 'type' => array( 'integer', 'null' ) ), |
| 209 | ), |
| 210 | ), |
| 211 | 'storage' => array( |
| 212 | 'type' => array( 'object', 'null' ), |
| 213 | 'properties' => array( |
| 214 | 'used_bytes' => array( 'type' => array( 'integer', 'null' ) ), |
| 215 | 'limit_bytes' => array( 'type' => array( 'integer', 'null' ) ), |
| 216 | ), |
| 217 | ), |
| 218 | ), |
| 219 | ), |
| 220 | 'execute_callback' => array( __CLASS__, 'execute_get_backup_overview' ), |
| 221 | 'permission_callback' => array( __CLASS__, 'can_view_backups' ), |
| 222 | 'meta' => array( |
| 223 | 'annotations' => array( |
| 224 | 'readonly' => true, |
| 225 | 'destructive' => false, |
| 226 | 'idempotent' => true, |
| 227 | ), |
| 228 | 'public' => true, |
| 229 | 'mcp' => array( |
| 230 | 'public' => true, |
| 231 | 'type' => 'tool', |
| 232 | ), |
| 233 | 'show_in_rest' => true, |
| 234 | ), |
| 235 | ), |
| 236 | |
| 237 | 'jetpack-backup/list-backups' => array( |
| 238 | 'label' => __( 'List backups', 'jetpack-backup-pkg' ), |
| 239 | 'description' => __( |
| 240 | 'Return zero or more backups as an array. Each item summarises one backup: { id, rewind_id, started, last_updated, status, period, is_rewindable, has_warnings }. Combine filters to narrow the result without making multiple calls. `id` returns a 0- or 1-element array for a single rewind_id. `date_from` and `date_to` window the results (ISO 8601 datetimes; server-side filter). `date` + `match` ("on_or_before" default, "on_or_after", "closest") pick a single backup near a target datetime — useful for "find a restore point near this incident"; the response stays a 0- or 1-element array. `status` filters by mapped status (e.g. "finished", "error"). `page` + `per_page` paginate the result; iterate `page=1,2,...` until you get an empty array. A page may come back with fewer than `per_page` items even when more pages exist — `status` is applied client-side, and non-backup events are filtered out — so only an empty page reliably signals end of history. Read-only and idempotent. Backed by the wpcom activity-log feed.', |
| 241 | 'jetpack-backup-pkg' |
| 242 | ), |
| 243 | 'input_schema' => array( |
| 244 | 'type' => 'object', |
| 245 | 'default' => array(), |
| 246 | 'properties' => array( |
| 247 | 'id' => array( |
| 248 | 'type' => 'string', |
| 249 | 'description' => __( 'Return only the backup with this rewind_id. Unknown ids yield an empty array.', 'jetpack-backup-pkg' ), |
| 250 | 'minLength' => 1, |
| 251 | ), |
| 252 | 'date_from' => array( |
| 253 | 'type' => 'string', |
| 254 | 'format' => 'date-time', |
| 255 | 'description' => __( 'Lower bound (inclusive) on backup `started` time. ISO 8601 datetime, e.g. "2026-04-01T00:00:00Z".', 'jetpack-backup-pkg' ), |
| 256 | 'minLength' => 1, |
| 257 | ), |
| 258 | 'date_to' => array( |
| 259 | 'type' => 'string', |
| 260 | 'format' => 'date-time', |
| 261 | 'description' => __( 'Upper bound (inclusive) on backup `started` time. ISO 8601 datetime, e.g. "2026-04-30T23:59:59Z".', 'jetpack-backup-pkg' ), |
| 262 | 'minLength' => 1, |
| 263 | ), |
| 264 | 'date' => array( |
| 265 | 'type' => 'string', |
| 266 | 'format' => 'date-time', |
| 267 | 'description' => __( 'Target datetime to find a single matching backup. When set, the response is a 0- or 1-element array. Pair with `match` to choose direction.', 'jetpack-backup-pkg' ), |
| 268 | 'minLength' => 1, |
| 269 | ), |
| 270 | 'match' => array( |
| 271 | 'type' => 'string', |
| 272 | 'enum' => array( 'on_or_before', 'on_or_after', 'closest' ), |
| 273 | 'default' => 'on_or_before', |
| 274 | 'description' => __( 'How to interpret `date`. "on_or_before" (default; the latest backup at or before the target — typical for restores), "on_or_after" (earliest backup at or after), or "closest" (smallest absolute time difference). Ignored when `date` is not set.', 'jetpack-backup-pkg' ), |
| 275 | ), |
| 276 | 'status' => array( |
| 277 | 'type' => 'string', |
| 278 | 'description' => __( 'Filter by mapped status string (e.g. "finished", "error"). Applied client-side after the server query.', 'jetpack-backup-pkg' ), |
| 279 | 'minLength' => 1, |
| 280 | ), |
| 281 | 'page' => array( |
| 282 | 'type' => 'integer', |
| 283 | 'description' => __( '1-based page number. Ignored when `id` or `date` is set (those are single-result lookups).', 'jetpack-backup-pkg' ), |
| 284 | 'default' => 1, |
| 285 | 'minimum' => 1, |
| 286 | ), |
| 287 | 'per_page' => array( |
| 288 | 'type' => 'integer', |
| 289 | 'description' => __( 'Cap on items returned per page (default 20, max 100). Also bounds the server-side query window the date filters are evaluated against.', 'jetpack-backup-pkg' ), |
| 290 | 'default' => self::PER_PAGE_DEFAULT, |
| 291 | 'minimum' => 1, |
| 292 | 'maximum' => self::PER_PAGE_MAX, |
| 293 | ), |
| 294 | ), |
| 295 | 'additionalProperties' => false, |
| 296 | ), |
| 297 | 'output_schema' => array( |
| 298 | 'type' => 'array', |
| 299 | 'items' => $backup_item_schema, |
| 300 | ), |
| 301 | 'execute_callback' => array( __CLASS__, 'execute_list_backups' ), |
| 302 | 'permission_callback' => array( __CLASS__, 'can_view_backups' ), |
| 303 | 'meta' => array( |
| 304 | 'annotations' => array( |
| 305 | 'readonly' => true, |
| 306 | 'destructive' => false, |
| 307 | 'idempotent' => true, |
| 308 | ), |
| 309 | 'public' => true, |
| 310 | 'mcp' => array( |
| 311 | 'public' => true, |
| 312 | 'type' => 'tool', |
| 313 | ), |
| 314 | 'show_in_rest' => true, |
| 315 | ), |
| 316 | ), |
| 317 | |
| 318 | 'jetpack-backup/list-restores' => array( |
| 319 | 'label' => __( 'List restores', 'jetpack-backup-pkg' ), |
| 320 | 'description' => __( |
| 321 | 'Return zero or more recent restore operations as an array. Each item: { id, rewind_id, started, last_updated, status, progress }. Pass id to fetch a single restore (returns 0- or 1-element array). Otherwise paginate with page and per_page (default 20, max 100). Read-only and idempotent.', |
| 322 | 'jetpack-backup-pkg' |
| 323 | ), |
| 324 | 'input_schema' => array( |
| 325 | 'type' => 'object', |
| 326 | 'default' => array(), |
| 327 | 'properties' => array( |
| 328 | 'id' => array( |
| 329 | 'type' => 'string', |
| 330 | 'description' => __( 'Return only the restore with this id. Unknown ids yield an empty array.', 'jetpack-backup-pkg' ), |
| 331 | 'minLength' => 1, |
| 332 | ), |
| 333 | 'page' => array( |
| 334 | 'type' => 'integer', |
| 335 | 'description' => __( 'Page number, 1-based.', 'jetpack-backup-pkg' ), |
| 336 | 'default' => 1, |
| 337 | 'minimum' => 1, |
| 338 | ), |
| 339 | 'per_page' => array( |
| 340 | 'type' => 'integer', |
| 341 | 'description' => __( 'Items per page (default 20, max 100).', 'jetpack-backup-pkg' ), |
| 342 | 'default' => self::PER_PAGE_DEFAULT, |
| 343 | 'minimum' => 1, |
| 344 | 'maximum' => self::PER_PAGE_MAX, |
| 345 | ), |
| 346 | ), |
| 347 | 'additionalProperties' => false, |
| 348 | ), |
| 349 | 'output_schema' => array( |
| 350 | 'type' => 'array', |
| 351 | 'items' => $restore_item_schema, |
| 352 | ), |
| 353 | 'execute_callback' => array( __CLASS__, 'execute_list_restores' ), |
| 354 | 'permission_callback' => array( __CLASS__, 'can_view_backups' ), |
| 355 | 'meta' => array( |
| 356 | 'annotations' => array( |
| 357 | 'readonly' => true, |
| 358 | 'destructive' => false, |
| 359 | 'idempotent' => true, |
| 360 | ), |
| 361 | 'public' => true, |
| 362 | 'mcp' => array( |
| 363 | 'public' => true, |
| 364 | 'type' => 'tool', |
| 365 | ), |
| 366 | 'show_in_rest' => true, |
| 367 | ), |
| 368 | ), |
| 369 | |
| 370 | 'jetpack-backup/request-backup' => array( |
| 371 | 'label' => __( 'Request a backup', 'jetpack-backup-pkg' ), |
| 372 | 'description' => __( |
| 373 | 'Enqueue an on-demand backup of this site. Returns a request_id and queued status; pass request_id to get-backup-request-status until completed or failed. A queued response does not mean a restore point exists yet. This is a state-changing write, not idempotent. Requires the manage_options capability.', |
| 374 | 'jetpack-backup-pkg' |
| 375 | ), |
| 376 | 'input_schema' => array( |
| 377 | 'type' => 'object', |
| 378 | 'default' => array(), |
| 379 | 'properties' => array(), |
| 380 | 'additionalProperties' => false, |
| 381 | ), |
| 382 | 'output_schema' => array( |
| 383 | 'type' => 'object', |
| 384 | 'properties' => array( |
| 385 | 'enqueued' => array( 'type' => 'boolean' ), |
| 386 | 'message' => array( 'type' => 'string' ), |
| 387 | 'request_id' => array( 'type' => 'string' ), |
| 388 | 'status' => array( |
| 389 | 'type' => 'string', |
| 390 | 'enum' => array( 'queued', 'running', 'completed', 'failed' ), |
| 391 | ), |
| 392 | 'requested_at' => array( 'type' => 'string' ), |
| 393 | ), |
| 394 | ), |
| 395 | 'execute_callback' => array( __CLASS__, 'execute_request_backup' ), |
| 396 | 'permission_callback' => array( __CLASS__, 'can_manage_backups' ), |
| 397 | 'meta' => array( |
| 398 | 'annotations' => array( |
| 399 | 'readonly' => false, |
| 400 | 'destructive' => false, |
| 401 | 'idempotent' => false, |
| 402 | ), |
| 403 | 'public' => true, |
| 404 | 'mcp' => array( |
| 405 | 'public' => true, |
| 406 | 'type' => 'tool', |
| 407 | ), |
| 408 | 'show_in_rest' => true, |
| 409 | ), |
| 410 | ), |
| 411 | 'jetpack-backup/get-backup-request-status' => array( |
| 412 | 'label' => __( 'Get backup request status', 'jetpack-backup-pkg' ), |
| 413 | 'description' => __( 'Read the status of one on-demand backup request. queued and running are pending; completed includes the resulting backup identifier, while failed includes a failure reason. Requires the request_id returned by request-backup.', 'jetpack-backup-pkg' ), |
| 414 | 'input_schema' => array( |
| 415 | 'type' => 'object', |
| 416 | 'required' => array( 'request_id' ), |
| 417 | 'properties' => array( |
| 418 | 'request_id' => array( |
| 419 | 'type' => 'string', |
| 420 | 'minLength' => 1, |
| 421 | ), |
| 422 | ), |
| 423 | 'additionalProperties' => false, |
| 424 | ), |
| 425 | 'output_schema' => array( |
| 426 | 'type' => 'object', |
| 427 | 'properties' => array( |
| 428 | 'request_id' => array( 'type' => 'string' ), |
| 429 | 'status' => array( |
| 430 | 'type' => 'string', |
| 431 | 'enum' => array( 'queued', 'running', 'completed', 'failed' ), |
| 432 | ), |
| 433 | 'requested_at' => array( 'type' => 'string' ), |
| 434 | 'started_at' => array( 'type' => array( 'string', 'null' ) ), |
| 435 | 'completed_at' => array( 'type' => array( 'string', 'null' ) ), |
| 436 | 'updated_at' => array( 'type' => 'string' ), |
| 437 | 'backup_id' => array( 'type' => array( 'integer', 'null' ) ), |
| 438 | 'rewind_id' => array( 'type' => array( 'string', 'null' ) ), |
| 439 | 'failure_reason' => array( 'type' => array( 'string', 'null' ) ), |
| 440 | ), |
| 441 | ), |
| 442 | 'execute_callback' => array( __CLASS__, 'execute_get_backup_request_status' ), |
| 443 | 'permission_callback' => array( __CLASS__, 'can_view_backups' ), |
| 444 | 'meta' => array( |
| 445 | 'annotations' => array( |
| 446 | 'readonly' => true, |
| 447 | 'destructive' => false, |
| 448 | 'idempotent' => true, |
| 449 | ), |
| 450 | 'public' => true, |
| 451 | 'mcp' => array( |
| 452 | 'public' => true, |
| 453 | 'type' => 'tool', |
| 454 | ), |
| 455 | 'show_in_rest' => true, |
| 456 | ), |
| 457 | ), |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Permission check for read abilities: the REST routes' own check, |
| 463 | * Jetpack_Backup::backups_permissions_callback(), whose error names the reason. |
| 464 | * Kept separate from `can_manage_backups()` so the read and write surfaces |
| 465 | * can diverge later without touching every spec. |
| 466 | * |
| 467 | * @return bool|WP_Error |
| 468 | */ |
| 469 | public static function can_view_backups() { |
| 470 | return Jetpack_Backup::backups_permissions_callback(); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Permission check for write abilities. See `can_view_backups()`. |
| 475 | * |
| 476 | * @return bool|WP_Error |
| 477 | */ |
| 478 | public static function can_manage_backups() { |
| 479 | return Jetpack_Backup::backups_permissions_callback(); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Composite read: each subfield is null on upstream failure rather than |
| 484 | * failing the whole call, so a partial wpcom outage degrades to "missing |
| 485 | * pieces" instead of "no data." Registration is gated on a Backup product |
| 486 | * being loaded (see register_abilities), so this callback assumes the |
| 487 | * site has one and only reports on the data it can fetch. |
| 488 | * |
| 489 | * @param mixed $input Unused; ability accepts no input. Typed `mixed` because |
| 490 | * the Abilities API may pass the raw caller-supplied value |
| 491 | * (string/null/array) before our `additionalProperties:false` |
| 492 | * schema runs — a strict array type would fatal on garbage input. |
| 493 | * @return array |
| 494 | */ |
| 495 | public static function execute_get_backup_overview( $input = null ): array { |
| 496 | unset( $input ); |
| 497 | |
| 498 | $backups = self::unwrap_response( Jetpack_Backup::get_recent_backups() ); |
| 499 | $schedule_data = self::unwrap_response( Jetpack_Backup::get_site_backup_schedule_time() ); |
| 500 | $size_data = self::unwrap_response( Jetpack_Backup::get_site_backup_size() ); |
| 501 | // Two round-trips because neither route describes storage alone: `/rewind/size` |
| 502 | // reports usage, `/rewind/policies` the limit. Fetched unconditionally so a |
| 503 | // usable limit still arrives when usage cannot be measured. |
| 504 | $policies_data = self::unwrap_response( Jetpack_Backup::get_site_backup_policies() ); |
| 505 | |
| 506 | return array( |
| 507 | 'recent_backup_count' => is_array( $backups ) ? count( $backups ) : null, |
| 508 | 'last_backup' => self::summarize_last_backup( is_array( $backups ) ? ( $backups[0] ?? null ) : null ), |
| 509 | 'schedule' => self::summarize_schedule( $schedule_data ), |
| 510 | 'storage' => self::summarize_storage( $size_data, $policies_data ), |
| 511 | ); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Consolidated read: queries the wpcom activity-log rewindable feed |
| 516 | * (server-side date filtering, up to 1000 items/page) and reshapes |
| 517 | * activity events back into the backup-item schema. All input filters |
| 518 | * land here; the picker is invoked when a `date` + `match` is set. |
| 519 | * |
| 520 | * @param mixed $input See input_schema on `jetpack-backup/list-backups`. |
| 521 | * @return array|WP_Error |
| 522 | */ |
| 523 | public static function execute_list_backups( $input = null ) { |
| 524 | $input = is_array( $input ) ? $input : array(); |
| 525 | |
| 526 | // Validate `date` / `date_from` / `date_to` ahead of the round-trip so |
| 527 | // agents get a specific error rather than a 200 with mysterious empty |
| 528 | // results. Schema's `format: date-time` is advisory in WP REST. |
| 529 | foreach ( array( 'date', 'date_from', 'date_to' ) as $key ) { |
| 530 | if ( isset( $input[ $key ] ) && '' !== $input[ $key ] && null === self::parse_timestamp( $input[ $key ] ) ) { |
| 531 | return new WP_Error( |
| 532 | 'jetpack_backup_invalid_date', |
| 533 | /* translators: %s is an input parameter name. */ |
| 534 | sprintf( __( 'The `%s` parameter must be a valid ISO 8601 datetime (e.g. "2026-05-13T14:30:00Z").', 'jetpack-backup-pkg' ), $key ) |
| 535 | ); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | $per_page = min( |
| 540 | self::PER_PAGE_MAX, |
| 541 | max( 1, isset( $input['per_page'] ) ? (int) $input['per_page'] : self::PER_PAGE_DEFAULT ) |
| 542 | ); |
| 543 | $page = max( 1, isset( $input['page'] ) ? (int) $input['page'] : 1 ); |
| 544 | |
| 545 | // `page` is suppressed on the single-result lookups (id, date+match) |
| 546 | // — those resolve from page 1 and walking later pages would skip |
| 547 | // candidates without an obvious benefit. |
| 548 | $is_single_lookup = ( isset( $input['id'] ) && '' !== $input['id'] ) |
| 549 | || ( isset( $input['date'] ) && '' !== $input['date'] ); |
| 550 | |
| 551 | $query = array( |
| 552 | 'number' => $per_page, |
| 553 | 'page' => $is_single_lookup ? 1 : $page, |
| 554 | 'sort_order' => 'desc', |
| 555 | ); |
| 556 | if ( isset( $input['date_from'] ) && '' !== $input['date_from'] ) { |
| 557 | $query['after'] = (string) $input['date_from']; |
| 558 | } |
| 559 | if ( isset( $input['date_to'] ) && '' !== $input['date_to'] ) { |
| 560 | $query['before'] = (string) $input['date_to']; |
| 561 | } |
| 562 | |
| 563 | $envelope = self::unwrap_response( Jetpack_Backup::list_backup_events( $query ) ); |
| 564 | $events = self::extract_rewindable_items( $envelope ); |
| 565 | if ( ! is_array( $events ) ) { |
| 566 | return array(); |
| 567 | } |
| 568 | |
| 569 | $items = array_values( array_filter( array_map( array( __CLASS__, 'summarize_backup_event' ), $events ) ) ); |
| 570 | |
| 571 | // Single-id filter — same convention as the old endpoint: 0/1-element array. |
| 572 | if ( isset( $input['id'] ) && is_string( $input['id'] ) && '' !== $input['id'] ) { |
| 573 | foreach ( $items as $item ) { |
| 574 | if ( isset( $item['id'] ) && (string) $item['id'] === $input['id'] ) { |
| 575 | return array( $item ); |
| 576 | } |
| 577 | } |
| 578 | return array(); |
| 579 | } |
| 580 | |
| 581 | // Client-side status filter (server-side filters by event name, not status). |
| 582 | if ( isset( $input['status'] ) && is_string( $input['status'] ) && '' !== $input['status'] ) { |
| 583 | $want = $input['status']; |
| 584 | $items = array_values( |
| 585 | array_filter( |
| 586 | $items, |
| 587 | static function ( $i ) use ( $want ) { |
| 588 | return ( $i['status'] ?? null ) === $want; |
| 589 | } |
| 590 | ) |
| 591 | ); |
| 592 | } |
| 593 | |
| 594 | // Single-match shortcut. |
| 595 | if ( isset( $input['date'] ) && '' !== $input['date'] ) { |
| 596 | $target = self::parse_timestamp( $input['date'] ); |
| 597 | $match = isset( $input['match'] ) && is_string( $input['match'] ) ? $input['match'] : 'on_or_before'; |
| 598 | if ( ! in_array( $match, array( 'on_or_before', 'on_or_after', 'closest' ), true ) ) { |
| 599 | $match = 'on_or_before'; |
| 600 | } |
| 601 | $pick = self::pick_backup_near_timestamp( $items, (int) $target, $match ); |
| 602 | return null === $pick ? array() : array( $pick ); |
| 603 | } |
| 604 | |
| 605 | return array_slice( $items, 0, $per_page ); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Execute callback for `jetpack-backup/list-restores`. |
| 610 | * |
| 611 | * @param mixed $input See input_schema on the ability. |
| 612 | * @return array |
| 613 | */ |
| 614 | public static function execute_list_restores( $input = null ): array { |
| 615 | $restores = self::unwrap_response( Jetpack_Backup::get_recent_restores() ); |
| 616 | if ( ! is_array( $restores ) ) { |
| 617 | return array(); |
| 618 | } |
| 619 | |
| 620 | $summarized = array_map( array( __CLASS__, 'summarize_restore' ), $restores ); |
| 621 | return self::apply_id_or_pagination( $summarized, is_array( $input ) ? $input : array() ); |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Pure picker for the `date` + `match` shortcut. Operates on already- |
| 626 | * summarized backup items (so it works regardless of which upstream |
| 627 | * helper produced them) and uses `started` as the comparison timestamp. |
| 628 | * |
| 629 | * @param array $items Summarized backup items. |
| 630 | * @param int $target_ts Unix timestamp the caller is searching around. |
| 631 | * @param string $match 'on_or_before' | 'on_or_after' | 'closest'. |
| 632 | * @return array|null The winning item or null when nothing matches. |
| 633 | */ |
| 634 | private static function pick_backup_near_timestamp( array $items, int $target_ts, string $match ): ?array { |
| 635 | $best = null; |
| 636 | $best_score = null; |
| 637 | |
| 638 | foreach ( $items as $item ) { |
| 639 | $ts = self::parse_timestamp( $item['started'] ?? null ); |
| 640 | if ( null === $ts ) { |
| 641 | continue; |
| 642 | } |
| 643 | |
| 644 | $diff = $ts - $target_ts; |
| 645 | $score = 0; |
| 646 | switch ( $match ) { |
| 647 | case 'on_or_after': |
| 648 | if ( $diff < 0 ) { |
| 649 | continue 2; |
| 650 | } |
| 651 | $score = $diff; |
| 652 | break; |
| 653 | case 'closest': |
| 654 | $score = abs( $diff ); |
| 655 | break; |
| 656 | case 'on_or_before': |
| 657 | default: |
| 658 | if ( $diff > 0 ) { |
| 659 | continue 2; |
| 660 | } |
| 661 | $score = -$diff; |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | if ( null === $best_score || $score < $best_score ) { |
| 666 | $best = $item; |
| 667 | $best_score = $score; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | return $best; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Pull the activity-event array out of the W3C ActivityStreams envelope |
| 676 | * that `/activity/rewindable` returns. The endpoint puts the items in |
| 677 | * `current.orderedItems`; older proxy shapes used `orderedItems` at the |
| 678 | * top level, so check both before giving up. |
| 679 | * |
| 680 | * @param mixed $envelope Raw decoded response body. |
| 681 | * @return array|null |
| 682 | */ |
| 683 | private static function extract_rewindable_items( $envelope ): ?array { |
| 684 | if ( ! is_array( $envelope ) && ! is_object( $envelope ) ) { |
| 685 | return null; |
| 686 | } |
| 687 | $envelope = (array) $envelope; |
| 688 | if ( isset( $envelope['current'] ) ) { |
| 689 | $current = (array) $envelope['current']; |
| 690 | if ( isset( $current['orderedItems'] ) && is_array( $current['orderedItems'] ) ) { |
| 691 | return $current['orderedItems']; |
| 692 | } |
| 693 | } |
| 694 | if ( isset( $envelope['orderedItems'] ) && is_array( $envelope['orderedItems'] ) ) { |
| 695 | return $envelope['orderedItems']; |
| 696 | } |
| 697 | return null; |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Translate one /activity/rewindable event into the same backup-item |
| 702 | * shape `summarize_backup()` produces, so the ability's output schema |
| 703 | * stays stable across the upstream switch. Returns null for events that |
| 704 | * don't look like backups (no `rewind_id`). |
| 705 | * |
| 706 | * @param mixed $raw One element from `current.orderedItems`. |
| 707 | * @return array|null |
| 708 | */ |
| 709 | private static function summarize_backup_event( $raw ): ?array { |
| 710 | if ( ! is_array( $raw ) && ! is_object( $raw ) ) { |
| 711 | return null; |
| 712 | } |
| 713 | $raw = (array) $raw; |
| 714 | |
| 715 | $rewind_id = $raw['rewind_id'] ?? null; |
| 716 | if ( null === $rewind_id || '' === $rewind_id ) { |
| 717 | return null; |
| 718 | } |
| 719 | |
| 720 | $published = isset( $raw['published'] ) && is_string( $raw['published'] ) ? $raw['published'] : null; |
| 721 | $status_raw = isset( $raw['status'] ) && is_string( $raw['status'] ) ? $raw['status'] : null; |
| 722 | $name = isset( $raw['name'] ) && is_string( $raw['name'] ) ? $raw['name'] : ''; |
| 723 | $is_rewindable = isset( $raw['is_rewindable'] ) ? (bool) $raw['is_rewindable'] : null; |
| 724 | |
| 725 | return array( |
| 726 | 'id' => (string) $rewind_id, |
| 727 | 'started' => $published, |
| 728 | 'last_updated' => $published, |
| 729 | 'status' => self::map_event_status( $status_raw, $name ), |
| 730 | 'period' => self::parse_timestamp( $rewind_id ), |
| 731 | 'is_rewindable' => $is_rewindable, |
| 732 | 'has_warnings' => self::event_has_warnings( $status_raw, $name ), |
| 733 | ); |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Map activity-event status / action-name to the status vocabulary the |
| 738 | * ability's output schema uses (the same labels as the old |
| 739 | * `/rewind/backups` endpoint: "finished", "error", ...). Falls back to |
| 740 | * the raw status when no mapping fits so the caller still sees signal. |
| 741 | * |
| 742 | * @param string|null $status_raw Activity event status (e.g. "success", "warning", "error"). |
| 743 | * @param string $name Activity name, e.g. "rewind__backup_complete_full". |
| 744 | * @return string|null |
| 745 | */ |
| 746 | private static function map_event_status( ?string $status_raw, string $name ): ?string { |
| 747 | if ( 'success' === $status_raw || false !== strpos( $name, 'backup_complete' ) ) { |
| 748 | return 'finished'; |
| 749 | } |
| 750 | return $status_raw; |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Derive `has_warnings` from an activity event's status / name. |
| 755 | * |
| 756 | * @param string|null $status_raw Activity event status. |
| 757 | * @param string $name Activity name. |
| 758 | * @return bool|null |
| 759 | */ |
| 760 | private static function event_has_warnings( ?string $status_raw, string $name ): ?bool { |
| 761 | if ( 'warning' === $status_raw ) { |
| 762 | return true; |
| 763 | } |
| 764 | if ( 'success' === $status_raw || false !== strpos( $name, 'backup_complete' ) ) { |
| 765 | return false; |
| 766 | } |
| 767 | return null; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Coerce an ISO 8601 string, RFC-style date string, or numeric unix |
| 772 | * timestamp to an int unix timestamp. Returns null for anything that |
| 773 | * can't be unambiguously parsed (instead of strtotime's `false`, which |
| 774 | * is also a valid timestamp for 1969-12-31). |
| 775 | * |
| 776 | * Fractional numeric strings (e.g. rewind_id "1778804242.107") are |
| 777 | * accepted — the fractional part is truncated. |
| 778 | * |
| 779 | * @param mixed $value Source value (string, int, float, or anything else). |
| 780 | * @return int|null |
| 781 | */ |
| 782 | private static function parse_timestamp( $value ): ?int { |
| 783 | if ( is_int( $value ) ) { |
| 784 | return $value; |
| 785 | } |
| 786 | if ( is_float( $value ) ) { |
| 787 | return (int) $value; |
| 788 | } |
| 789 | if ( is_string( $value ) && '' !== $value ) { |
| 790 | if ( is_numeric( $value ) ) { |
| 791 | return (int) $value; |
| 792 | } |
| 793 | $ts = strtotime( $value ); |
| 794 | return false === $ts ? null : $ts; |
| 795 | } |
| 796 | return null; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Enqueue an on-demand backup. Registration is gated on a Backup product |
| 801 | * being loaded so we assume one exists by the time this runs. Returns |
| 802 | * WP_Error when the upstream request fails or accepts a backup without a |
| 803 | * tracking handle; the latter must not trigger an automatic retry. |
| 804 | * |
| 805 | * @param mixed $input Unused; see note on execute_get_backup_overview(). |
| 806 | * @return array|WP_Error |
| 807 | */ |
| 808 | public static function execute_request_backup( $input = null ) { |
| 809 | unset( $input ); |
| 810 | |
| 811 | // wpcom can return HTTP 200 with `{ success: false, error: ... }`; treat |
| 812 | // that as a failure rather than reporting the backup was enqueued. |
| 813 | $result = self::unwrap_response( Jetpack_Backup::enqueue_backup() ); |
| 814 | if ( ! is_array( $result ) || empty( $result['success'] ) ) { |
| 815 | return new WP_Error( |
| 816 | 'jetpack_backup_data_unavailable', |
| 817 | __( 'The backup service did not accept the request. The connection to WordPress.com may be temporarily unavailable; retry shortly.', 'jetpack-backup-pkg' ) |
| 818 | ); |
| 819 | } |
| 820 | if ( ! is_string( $result['request_id'] ?? null ) |
| 821 | || ! preg_match( self::BACKUP_REQUEST_ID_PATTERN, $result['request_id'] ) |
| 822 | || ! in_array( $result['status'] ?? null, array( 'queued', 'running', 'completed', 'failed' ), true ) |
| 823 | || ! is_string( $result['requested_at'] ?? null ) ) { |
| 824 | return new WP_Error( |
| 825 | 'jetpack_backup_tracking_unavailable', |
| 826 | __( 'The backup was accepted, but its status cannot be tracked. Do not request another backup automatically.', 'jetpack-backup-pkg' ) |
| 827 | ); |
| 828 | } |
| 829 | |
| 830 | return array( |
| 831 | 'enqueued' => true, |
| 832 | 'message' => __( 'Backup enqueued. Poll jetpack-backup/get-backup-request-status using request_id.', 'jetpack-backup-pkg' ), |
| 833 | 'request_id' => $result['request_id'], |
| 834 | 'status' => $result['status'], |
| 835 | 'requested_at' => $result['requested_at'], |
| 836 | ); |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Get the status of a specific on-demand backup request. |
| 841 | * |
| 842 | * @param mixed $input Ability input containing request_id. |
| 843 | * @return array|WP_Error |
| 844 | */ |
| 845 | public static function execute_get_backup_request_status( $input = null ) { |
| 846 | if ( ! is_array( $input ) |
| 847 | || ! is_string( $input['request_id'] ?? null ) |
| 848 | || ! preg_match( self::BACKUP_REQUEST_ID_PATTERN, $input['request_id'] ) ) { |
| 849 | return new WP_Error( 'jetpack_backup_invalid_request', __( 'A valid backup request ID is required.', 'jetpack-backup-pkg' ) ); |
| 850 | } |
| 851 | |
| 852 | $response = Jetpack_Backup::get_backup_request_status( $input['request_id'] ); |
| 853 | if ( is_wp_error( $response ) && 'jetpack_backup_request_not_found' === $response->get_error_code() ) { |
| 854 | return $response; |
| 855 | } |
| 856 | $result = self::unwrap_response( $response ); |
| 857 | if ( ! is_array( $result ) |
| 858 | || ( $result['request_id'] ?? null ) !== $input['request_id'] |
| 859 | || ! in_array( $result['status'] ?? null, array( 'queued', 'running', 'completed', 'failed' ), true ) |
| 860 | || ! is_string( $result['requested_at'] ?? null ) |
| 861 | || ! is_string( $result['updated_at'] ?? null ) ) { |
| 862 | return new WP_Error( 'jetpack_backup_data_unavailable', __( 'The backup request status is unavailable. Retry shortly.', 'jetpack-backup-pkg' ) ); |
| 863 | } |
| 864 | |
| 865 | return array( |
| 866 | 'request_id' => $result['request_id'], |
| 867 | 'status' => $result['status'], |
| 868 | 'requested_at' => $result['requested_at'], |
| 869 | 'started_at' => is_string( $result['started_at'] ?? null ) ? $result['started_at'] : null, |
| 870 | 'completed_at' => is_string( $result['completed_at'] ?? null ) ? $result['completed_at'] : null, |
| 871 | 'updated_at' => $result['updated_at'], |
| 872 | 'backup_id' => isset( $result['backup_id'] ) ? (int) $result['backup_id'] : null, |
| 873 | 'rewind_id' => isset( $result['rewind_id'] ) ? (string) $result['rewind_id'] : null, |
| 874 | 'failure_reason' => is_string( $result['failure_reason'] ?? null ) ? $result['failure_reason'] : null, |
| 875 | ); |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Normalize a Jetpack_Backup helper result (WP_REST_Response, array, null, |
| 880 | * or WP_Error) to a plain value or null. Jetpack_Backup uses |
| 881 | * `rest_ensure_response()` on success; on failure its routes return a |
| 882 | * WP_Error and `list_backup_events()` returns null, so abilities need |
| 883 | * every shape flattened before summarising. |
| 884 | * |
| 885 | * @param mixed $maybe_response Result of a Jetpack_Backup helper call. |
| 886 | * @return mixed |
| 887 | */ |
| 888 | private static function unwrap_response( $maybe_response ) { |
| 889 | if ( null === $maybe_response || is_wp_error( $maybe_response ) ) { |
| 890 | return null; |
| 891 | } |
| 892 | if ( $maybe_response instanceof WP_REST_Response ) { |
| 893 | return $maybe_response->get_data(); |
| 894 | } |
| 895 | return $maybe_response; |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * Slice the (already-summarized) list down to a single id, or apply |
| 900 | * page/per_page pagination. Always returns the same item shape. |
| 901 | * |
| 902 | * @param array $items Summarized items. |
| 903 | * @param array $input Sanitized input. |
| 904 | * @return array |
| 905 | */ |
| 906 | private static function apply_id_or_pagination( array $items, array $input ): array { |
| 907 | if ( isset( $input['id'] ) && is_string( $input['id'] ) && '' !== $input['id'] ) { |
| 908 | foreach ( $items as $item ) { |
| 909 | if ( isset( $item['id'] ) && (string) $item['id'] === $input['id'] ) { |
| 910 | return array( $item ); |
| 911 | } |
| 912 | } |
| 913 | return array(); |
| 914 | } |
| 915 | |
| 916 | $page = max( 1, (int) ( $input['page'] ?? 1 ) ); |
| 917 | $per_page = min( self::PER_PAGE_MAX, max( 1, (int) ( $input['per_page'] ?? self::PER_PAGE_DEFAULT ) ) ); |
| 918 | |
| 919 | return array_slice( $items, ( $page - 1 ) * $per_page, $per_page ); |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * High-signal summary used inside `last_backup` for the overview. Same as |
| 924 | * `summarize_backup` minus the `started`/`period` fields which the agent |
| 925 | * doesn't need at a glance. |
| 926 | * |
| 927 | * @param mixed $raw One element from the upstream backups list. |
| 928 | * @return array|null |
| 929 | */ |
| 930 | private static function summarize_last_backup( $raw ): ?array { |
| 931 | if ( ! is_array( $raw ) && ! is_object( $raw ) ) { |
| 932 | return null; |
| 933 | } |
| 934 | return array_diff_key( |
| 935 | self::summarize_backup( $raw ), |
| 936 | array_flip( array( 'started', 'period' ) ) |
| 937 | ); |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * Summarize a `/rewind/backups` payload item using `rewind_id` as the |
| 942 | * canonical `id`. The numeric attempt id wpcom also exposes is |
| 943 | * internal to VaultPress and can't be looked up via any other endpoint, |
| 944 | * so it's intentionally dropped from the agent-facing shape — see the |
| 945 | * note on `$backup_item_schema` in `get_abilities()`. |
| 946 | * |
| 947 | * @param mixed $raw Upstream backup item. |
| 948 | * @return array |
| 949 | */ |
| 950 | private static function summarize_backup( $raw ): array { |
| 951 | $raw = (array) $raw; |
| 952 | $rewind_id = $raw['rewind_id'] ?? null; |
| 953 | return array( |
| 954 | 'id' => ( null === $rewind_id || '' === $rewind_id ) ? null : (string) $rewind_id, |
| 955 | 'started' => $raw['started'] ?? null, |
| 956 | 'last_updated' => $raw['last_updated'] ?? null, |
| 957 | 'status' => $raw['status'] ?? null, |
| 958 | 'period' => $raw['period'] ?? null, |
| 959 | 'is_rewindable' => isset( $raw['is_rewindable'] ) ? (bool) $raw['is_rewindable'] : null, |
| 960 | 'has_warnings' => isset( $raw['has_warnings'] ) ? (bool) $raw['has_warnings'] : null, |
| 961 | ); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Summarize a `/rewind/restores` payload item. `id` is the rewind_id |
| 966 | * of the backup being restored to — same canonical id system as |
| 967 | * `summarize_backup()`. |
| 968 | * |
| 969 | * @param mixed $raw Upstream restore item. |
| 970 | * @return array |
| 971 | */ |
| 972 | private static function summarize_restore( $raw ): array { |
| 973 | $raw = (array) $raw; |
| 974 | $rewind_id = $raw['rewind_id'] ?? null; |
| 975 | return array( |
| 976 | 'id' => ( null === $rewind_id || '' === $rewind_id ) ? null : (string) $rewind_id, |
| 977 | 'started' => $raw['started'] ?? null, |
| 978 | 'last_updated' => $raw['last_updated'] ?? null, |
| 979 | 'status' => $raw['status'] ?? null, |
| 980 | 'progress' => isset( $raw['progress'] ) ? (int) $raw['progress'] : null, |
| 981 | ); |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * Summarize the `/site/backup/schedule` payload to `{ hour }`. |
| 986 | * |
| 987 | * WordPress.com answers `{ ok, scheduled_hour, scheduled_by }` — the UTC hour of the |
| 988 | * daily backup, and no minute anywhere. `ok` is its own success flag inside a 200 |
| 989 | * body, so a payload without it carries no usable hour. |
| 990 | * |
| 991 | * @param mixed $raw Upstream schedule payload. |
| 992 | * @return array|null |
| 993 | */ |
| 994 | private static function summarize_schedule( $raw ): ?array { |
| 995 | if ( ! is_array( $raw ) && ! is_object( $raw ) ) { |
| 996 | return null; |
| 997 | } |
| 998 | $raw = (array) $raw; |
| 999 | if ( empty( $raw['ok'] ) ) { |
| 1000 | return null; |
| 1001 | } |
| 1002 | return array( |
| 1003 | 'hour' => isset( $raw['scheduled_hour'] ) ? (int) $raw['scheduled_hour'] : null, |
| 1004 | ); |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Summarize storage from the two payloads that between them describe it. |
| 1009 | * |
| 1010 | * Usage is `size` on `/site/backup/size`, which despite its name carries no limit; |
| 1011 | * the limit is `policies.storage_limit_bytes` on `/site/backup/policies`. |
| 1012 | * |
| 1013 | * The two are read independently, so a site whose usage could not be measured still |
| 1014 | * reports what it is allowed. That is deliberately unlike `summarize_schedule()`, |
| 1015 | * which has nothing left to report once its hour is gone. |
| 1016 | * |
| 1017 | * @param mixed $size_raw Upstream `/site/backup/size` payload. |
| 1018 | * @param mixed $policies_raw Upstream `/site/backup/policies` payload. |
| 1019 | * @return array|null |
| 1020 | */ |
| 1021 | private static function summarize_storage( $size_raw, $policies_raw ): ?array { |
| 1022 | $size = ( is_array( $size_raw ) || is_object( $size_raw ) ) ? (array) $size_raw : null; |
| 1023 | $policies = ( is_array( $policies_raw ) || is_object( $policies_raw ) ) ? (array) $policies_raw : null; |
| 1024 | |
| 1025 | if ( null === $size && null === $policies ) { |
| 1026 | return null; |
| 1027 | } |
| 1028 | |
| 1029 | $used_bytes = ( null !== $size && ! empty( $size['ok'] ) && isset( $size['size'] ) ) |
| 1030 | ? (int) $size['size'] |
| 1031 | : null; |
| 1032 | |
| 1033 | // `policies` is itself nullable inside a 200: a plan with no retention policy |
| 1034 | // answers `{ "policies": null }`. |
| 1035 | $policy = $policies['policies'] ?? null; |
| 1036 | $policy = ( is_array( $policy ) || is_object( $policy ) ) ? (array) $policy : null; |
| 1037 | |
| 1038 | $limit_bytes = ( null !== $policy && isset( $policy['storage_limit_bytes'] ) ) |
| 1039 | ? (int) $policy['storage_limit_bytes'] |
| 1040 | : null; |
| 1041 | |
| 1042 | return array( |
| 1043 | 'used_bytes' => $used_bytes, |
| 1044 | 'limit_bytes' => $limit_bytes, |
| 1045 | ); |
| 1046 | } |
| 1047 | } |