array( 'required' => true, 'validate_callback' => static fn( $v ) => Rollbakr_Storage::is_valid_id( $v ), ), ); register_rest_route( self::NS, '/status', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'status' ), 'permission_callback' => array( __CLASS__, 'allowed' ), ) ); register_rest_route( self::NS, '/storage/check', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'check' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => array( 'dir' => array( 'type' => 'string', 'default' => '', ), ), ) ); register_rest_route( self::NS, '/sizes', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'sizes' ), 'permission_callback' => array( __CLASS__, 'allowed' ), ) ); register_rest_route( self::NS, '/logs', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'logs' ), 'permission_callback' => array( __CLASS__, 'allowed' ), ) ); register_rest_route( self::NS, '/logs/(?P[^/]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'log' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ), // DELETE, never GET: a record that is removed is gone. array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( __CLASS__, 'delete_log' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ), ) ); register_rest_route( self::NS, '/backups', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'backups' ), 'permission_callback' => array( __CLASS__, 'allowed' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'start_backup' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => array( 'parts' => array( 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'string', 'enum' => Rollbakr_Files::PARTS, ), ), // What an add-on's fields on the backup form sent (name => value, strings only). 'extra' => array( 'type' => 'object', 'default' => array(), ), ), ), ) ); register_rest_route( self::NS, '/backups/(?P[^/]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'backup' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( __CLASS__, 'delete_backup' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ), ) ); // The "I saved it on my computer" mark (N1, "Download to your computer"): put on, taken off. register_rest_route( self::NS, '/backups/(?P[^/]+)/saved', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'mark_saved' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( __CLASS__, 'unmark_saved' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ), ) ); register_rest_route( self::NS, '/restores', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'start_restore' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => array( 'backup' => array( 'required' => true, 'validate_callback' => static fn( $v ) => Rollbakr_Storage::is_valid_id( $v ), ), 'parts' => array( 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'string', 'enum' => Rollbakr_Files::PARTS, ), ), 'pre_backup' => array( 'type' => 'boolean', 'default' => true, ), ), ) ); // "Restore from files": begin, pieces, finish, discard. register_rest_route( self::NS, '/uploads', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'upload_begin' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => array( 'id' => $id['id'], 'files' => array( 'required' => true, 'type' => 'array', ), ), ) ); register_rest_route( self::NS, '/uploads/(?P[^/]+)/piece', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'upload_piece' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => array_merge( $id, array( 'name' => array( 'required' => true, 'validate_callback' => static fn( $v ) => Rollbakr_Upload::is_valid_name( $v ), ), 'offset' => array( 'required' => true, 'type' => 'integer', 'minimum' => 0, ), ) ), ) ); register_rest_route( self::NS, '/uploads/(?P[^/]+)/finish', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'upload_finish' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ) ); register_rest_route( self::NS, '/uploads/(?P[^/]+)', array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( __CLASS__, 'upload_discard' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ) ); // A new pass phrase for the running restore (an administrator who continues it from another // tab or a new login, whose tab has none): the old one stops working. register_rest_route( self::NS, '/jobs/(?P[^/]+)/token', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'token' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ) ); // Step and cancel also accept the restore's one-time pass phrase: the database swap can log // the administrator out while the restore still has to go on. register_rest_route( self::NS, '/jobs/(?P[^/]+)/step', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'step' ), 'permission_callback' => array( __CLASS__, 'allowed_or_token' ), 'args' => $id, ) ); register_rest_route( self::NS, '/jobs/(?P[^/]+)/cancel', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'cancel' ), 'permission_callback' => array( __CLASS__, 'allowed_or_token' ), 'args' => $id, ) ); register_rest_route( self::NS, '/jobs/(?P[^/]+)/dismiss', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'dismiss' ), 'permission_callback' => array( __CLASS__, 'allowed' ), 'args' => $id, ) ); } /** * Only administrators. * * @return bool|WP_Error */ public static function allowed() { if ( current_user_can( 'manage_options' ) ) { return true; } return new WP_Error( 'rollbakr_forbidden', __( 'You are not allowed to do this.', 'rollbakr' ), array( 'status' => rest_authorization_required_code() ) ); } /** * Administrators, or the holder of the running restore's pass phrase (header X-Rollbakr-Token), * for that job only. * * @param WP_REST_Request $request Request. * @return bool|WP_Error */ public static function allowed_or_token( WP_REST_Request $request ) { if ( current_user_can( 'manage_options' ) ) { return true; } $job = Rollbakr_Job::current(); if ( $job && $job->id === (string) $request['id'] && Rollbakr_Restore::token_ok( $job, (string) $request->get_header( 'x_rollbakr_token' ) ) ) { return true; } return new WP_Error( 'rollbakr_forbidden', __( 'You are not allowed to do this.', 'rollbakr' ), array( 'status' => rest_authorization_required_code() ) ); } /** * A job for the screen: to_public() plus, for a restore in maintenance mode, the query arguments * that let the screen's requests through. * * @param Rollbakr_Job $job Job. * @return array */ public static function job_data( Rollbakr_Job $job ) { $out = $job->to_public(); if ( Rollbakr_Restore::TYPE === $job->type ) { if ( ! empty( $job->state['maint'] ) ) { $out['bypass'] = Rollbakr_Maintenance::bypass( (int) $job->state['maint'] ); } $s = $job->state; $sub = $s['sub'] ?? null; $fmt = Rollbakr_Admin::datetime_format(); // What the result screen says, without handing out the whole state. $out['restore'] = array( 'backup' => (string) ( $s['backup']['id'] ?? $job->args['backup'] ?? '' ), 'backup_created' => isset( $s['backup']['created'] ) ? wp_date( $fmt, (int) $s['backup']['created'] ) : '', 'tables' => count( (array) ( $s['db']['tables'] ?? array() ) ), 'files' => (int) ( $s['place']['done'] ?? 0 ), 'pre_backup' => $sub && 'done' === ( $sub['status'] ?? '' ) ? array( 'id' => (string) $sub['id'], 'created' => wp_date( $fmt, (int) $sub['created'] ), ) : null, 'took' => isset( $s['finished'], $s['started'] ) ? Rollbakr_Admin::duration( (int) $s['finished'] - (int) $s['started'] ) : '', ); } return $out; } /** * GET /status: the folder, its check, free space and the current job. * * @return WP_REST_Response */ public static function status() { // Always a fresh look: a request that asks for the status asks for it once. return rest_ensure_response( self::status_data( true ) ); } /** * What /status returns (also used by the admin screen on load). * * @param bool $fresh Look again instead of answering from this request's cache (tests, and a * caller that started a job in the same request). * @return array */ public static function status_data( $fresh = false ) { // Walking the backup folder for used_space() is not cheap; one request needs it at most once. static $cache = null; if ( null !== $cache && ! $fresh ) { return $cache; } $job = Rollbakr_Job::current(); $check = Rollbakr_Storage::last_check(); if ( $check ) { // The time as the screens show it (the settings screen draws the mark without reloading). $check['checked_text'] = wp_date( Rollbakr_Admin::time_format(), (int) $check['checked'] ); } $cache = array( 'dir' => Rollbakr_Storage::dir(), 'source' => Rollbakr_Storage::source(), 'destination' => Rollbakr_Storage::is_local() ? 'local' : 'server', 'check' => $check, 'safe' => Rollbakr_Storage::is_safe(), 'free' => Rollbakr_Storage::free_space(), 'used' => Rollbakr_Storage::used_space(), 'job' => $job ? self::job_data( $job ) : null, 'budget' => Rollbakr_Job::budget(), 'site_id' => Rollbakr_Storage::site_id(), ); return $cache; } /** * A manifest as the screen wants it: plus the downloads (name => URL), their labels and what each * holds ('bundles'), and the texts of the row. * * @param array $m Manifest. * @return array */ public static function backup_data( array $m ) { // What is downloaded: one .tar holding the archives, or several for a big site (Rollbakr_Backups::bundles()). $bundles = Rollbakr_Backups::bundles( $m ); $downloads = array(); foreach ( $bundles as $key => $b ) { $downloads[ $key ] = Rollbakr_Download::url( $m['id'], $key ); $bundles[ $key ]['filename'] = Rollbakr_Download::bundle_filename( $m['id'], $b ); } $done = Rollbakr_Backups::downloads( $m['id'] ); $m['downloads'] = $downloads; $m['labels'] = Rollbakr_Admin::bundle_labels( $bundles ); $m['bundles'] = $bundles; $m['parts_label'] = Rollbakr_Backups::parts_label( $m ); $m['kind_label'] = Rollbakr_Backups::kind_label( (string) ( $m['kind'] ?? 'manual' ) ); $m['created_text'] = wp_date( Rollbakr_Admin::datetime_format(), (int) $m['created'] ); $m['bytes_text'] = Rollbakr_Admin::size( (int) $m['bytes'] ); // Whether every archive has been downloaded in full, and when the last one was. $m['downloaded'] = Rollbakr_Backups::fully_downloaded( $m['id'] ); $m['downloaded_at'] = $done ? max( $done ) : 0; // When the administrator marked it "I saved it on my computer" (0 = not marked). $m['saved_at'] = Rollbakr_Backups::saved( $m['id'] ); return $m; } /** * GET /sizes: what each part would take, each with a text. When they are not cached they are * counted for one step's time (Rollbakr_Files::sizes_step()); a big site needs more than one: * the answer is then { pending: true, files: n } and the screen asks again. * * @return WP_REST_Response */ public static function sizes() { $sizes = Rollbakr_Files::sizes_cached() ?? Rollbakr_Files::sizes_step( microtime( true ) + Rollbakr_Job::budget() ); if ( null === $sizes ) { return rest_ensure_response( array( 'pending' => true, 'files' => Rollbakr_Files::sizes_counted(), ) ); } foreach ( $sizes as $part => &$s ) { $s['text'] = Rollbakr_Admin::size( (int) $s['bytes'] ); if ( 'database' === $part ) { $s['text'] .= ' · ' . sprintf( /* translators: %d: tables */ _n( '%d table', '%d tables', (int) $s['files'], 'rollbakr' ), (int) $s['files'] ); } elseif ( 'uploads' === $part ) { $s['text'] .= ' · ' . sprintf( /* translators: %s: files */ __( '%s files', 'rollbakr' ), number_format_i18n( (int) $s['files'] ) ); } } unset( $s ); return rest_ensure_response( $sizes ); } /** * A record as the Logs screen wants it: plus labels and the log download link. * * @param array $r Record. * @return array */ public static function record_data( array $r ) { $labels = Rollbakr_Logs::labels( $r ); $r['labels'] = $labels; $r['created_text'] = wp_date( Rollbakr_Admin::datetime_format(), (int) $r['created'] ); $r['took_text'] = Rollbakr_Admin::duration( (int) ( $r['finished'] ?? $r['created'] ) - (int) $r['created'] ); $r['download'] = Rollbakr_Download::url( (string) $r['id'], 'log.txt' ); return $r; } /** * GET /logs: the records, newest first. * * @return WP_REST_Response */ public static function logs() { return rest_ensure_response( array_map( array( __CLASS__, 'record_data' ), Rollbakr_Logs::all() ) ); } /** * GET /logs/{id}: one record with its log text. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function log( WP_REST_Request $request ) { $r = Rollbakr_Logs::get( (string) $request['id'] ); if ( ! $r ) { return new WP_Error( 'rollbakr_log_missing', __( 'There is no such record.', 'rollbakr' ), array( 'status' => 404 ) ); } $r = self::record_data( $r ); $r['log'] = Rollbakr_Logs::log( (string) $r['id'] ); return rest_ensure_response( $r ); } /** * DELETE /logs/{id}: remove one record of a job that left no backup (Rollbakr_Logs::remove()). * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function delete_log( WP_REST_Request $request ) { $done = Rollbakr_Logs::remove( (string) $request['id'] ); if ( is_wp_error( $done ) ) { $codes = array( 'rollbakr_log_missing' => 404, 'rollbakr_log_backup' => 409, 'rollbakr_job_exists' => 409, ); $done->add_data( array( 'status' => $codes[ $done->get_error_code() ] ?? 500 ) ); return $done; } return rest_ensure_response( array( 'deleted' => true ) ); } /** * GET /backups. * * @return WP_REST_Response */ public static function backups() { return rest_ensure_response( array_map( array( __CLASS__, 'backup_data' ), Rollbakr_Backups::all() ) ); } /** * GET /backups/{id}. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function backup( WP_REST_Request $request ) { $m = Rollbakr_Backups::get( (string) $request['id'] ); if ( ! $m ) { return new WP_Error( 'rollbakr_backup_missing', __( 'There is no such backup.', 'rollbakr' ), array( 'status' => 404 ) ); } return rest_ensure_response( self::backup_data( $m ) ); } /** * POST /backups: start a backup job. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function start_backup( WP_REST_Request $request ) { /** * What a backup started from the screen is started with. An add-on reads its own fields * from 'extra' (the form's inputs named rollbakr_extra…, cleaned to short strings) and may * change 'parts', 'kind' or 'extra' before the job starts. * * @param array{parts:string[],kind:string,extra:array} $args Arguments for Rollbakr_Backup::start(). * @param WP_REST_Request $request The request. */ $args = apply_filters( 'rollbakr_backup_start_args', array( 'parts' => (array) $request['parts'], 'kind' => 'manual', 'extra' => Rollbakr_Backup::clean_extra( $request['extra'] ), ), $request ); $job = Rollbakr_Backup::start( (array) ( $args['parts'] ?? array() ), (string) ( $args['kind'] ?? 'manual' ), (array) ( $args['extra'] ?? array() ) ); if ( is_wp_error( $job ) ) { $codes = array( 'rollbakr_job_busy' => 409, 'rollbakr_job_exists' => 409, 'rollbakr_storage_unsafe' => 409, ); $job->add_data( array( 'status' => $codes[ $job->get_error_code() ] ?? 400 ) ); return $job; } return rest_ensure_response( $job->to_public() ); } /** * POST /restores: start a restore job. The answer carries the one-time pass phrase once. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function start_restore( WP_REST_Request $request ) { $result = Rollbakr_Restore::start( (string) $request['backup'], (array) $request['parts'], (bool) $request['pre_backup'] ); if ( is_wp_error( $result ) ) { $codes = array( 'rollbakr_job_busy' => 409, 'rollbakr_job_exists' => 409, 'rollbakr_storage_unsafe' => 409, 'rollbakr_backup_missing' => 404, ); $result->add_data( array( 'status' => $codes[ $result->get_error_code() ] ?? 400 ) ); return $result; } $out = self::job_data( $result['job'] ); $out['token'] = $result['token']; return rest_ensure_response( $out ); } /** * POST /jobs/{id}/token: a new pass phrase for the running restore. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function token( WP_REST_Request $request ) { $token = Rollbakr_Restore::renew_token( (string) $request['id'] ); if ( is_wp_error( $token ) ) { $token->add_data( array( 'status' => 'rollbakr_job_busy' === $token->get_error_code() ? 409 : 404 ) ); return $token; } return rest_ensure_response( array( 'token' => $token ) ); } /** * POST /uploads: begin receiving a backup's files. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function upload_begin( WP_REST_Request $request ) { $files = array(); foreach ( (array) $request['files'] as $f ) { $files[] = array( 'name' => (string) ( $f['name'] ?? '' ), 'size' => (int) ( $f['size'] ?? -1 ), ); } $r = Rollbakr_Upload::begin( (string) $request['id'], $files ); if ( is_wp_error( $r ) ) { $r->add_data( array( 'status' => in_array( $r->get_error_code(), array( 'rollbakr_upload_exists', 'rollbakr_storage_unsafe', 'rollbakr_job_exists' ), true ) ? 409 : 400 ) ); return $r; } return rest_ensure_response( array( 'started' => true ) ); } /** * POST /uploads/{id}/piece?name=…&offset=…: one piece of a file (the request body, raw). * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function upload_piece( WP_REST_Request $request ) { $r = Rollbakr_Upload::append( (string) $request['id'], (string) $request['name'], (int) $request['offset'], (string) $request->get_body() ); if ( is_wp_error( $r ) ) { $r->add_data( array( 'status' => 'rollbakr_upload_offset' === $r->get_error_code() ? 409 : ( 'rollbakr_upload_missing' === $r->get_error_code() ? 404 : 400 ) ) ); return $r; } return rest_ensure_response( $r ); } /** * POST /uploads/{id}/finish: every file has arrived; start the checking job. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function upload_finish( WP_REST_Request $request ) { $job = Rollbakr_Upload::finish( (string) $request['id'] ); if ( is_wp_error( $job ) ) { $codes = array( 'rollbakr_job_busy' => 409, 'rollbakr_job_exists' => 409, 'rollbakr_storage_unsafe' => 409, 'rollbakr_upload_missing' => 404, ); $job->add_data( array( 'status' => $codes[ $job->get_error_code() ] ?? 400 ) ); return $job; } return rest_ensure_response( self::job_data( $job ) ); } /** * DELETE /uploads/{id}: throw the pieces away. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public static function upload_discard( WP_REST_Request $request ) { return rest_ensure_response( array( 'discarded' => Rollbakr_Upload::discard( (string) $request['id'] ) ) ); } /** * DELETE /backups/{id}. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function delete_backup( WP_REST_Request $request ) { $id = (string) $request['id']; if ( ! Rollbakr_Backups::get( $id ) ) { return new WP_Error( 'rollbakr_backup_missing', __( 'There is no such backup.', 'rollbakr' ), array( 'status' => 404 ) ); } if ( ! Rollbakr_Backups::delete( $id ) ) { return new WP_Error( 'rollbakr_backup_delete', __( 'The backup could not be removed.', 'rollbakr' ), array( 'status' => 500 ) ); } // The job that made it, if it is still the current one, has nothing left to announce. $job = Rollbakr_Job::current(); if ( $job && $job->id === $id && 'done' === $job->status ) { Rollbakr_Job::dismiss( $id ); } return rest_ensure_response( array( 'deleted' => true ) ); } /** * POST /backups/{id}/saved: mark the backup "I saved it on my computer". * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function mark_saved( WP_REST_Request $request ) { $id = (string) $request['id']; if ( ! Rollbakr_Backups::get( $id ) ) { return new WP_Error( 'rollbakr_backup_missing', __( 'There is no such backup.', 'rollbakr' ), array( 'status' => 404 ) ); } if ( ! Rollbakr_Backups::mark_saved( $id ) ) { return new WP_Error( 'rollbakr_saved_mark', __( 'The mark could not be written.', 'rollbakr' ), array( 'status' => 500 ) ); } return rest_ensure_response( array( 'saved_at' => Rollbakr_Backups::saved( $id ) ) ); } /** * DELETE /backups/{id}/saved: take the "I saved it on my computer" mark off. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function unmark_saved( WP_REST_Request $request ) { $id = (string) $request['id']; if ( ! Rollbakr_Backups::get( $id ) ) { return new WP_Error( 'rollbakr_backup_missing', __( 'There is no such backup.', 'rollbakr' ), array( 'status' => 404 ) ); } if ( ! Rollbakr_Backups::unmark_saved( $id ) ) { return new WP_Error( 'rollbakr_saved_mark', __( 'The mark could not be removed.', 'rollbakr' ), array( 'status' => 500 ) ); } return rest_ensure_response( array( 'saved_at' => 0 ) ); } /** * POST /storage/check: check again now; with 'dir', look at that folder instead (not saved). * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public static function check( WP_REST_Request $request ) { $dir = trim( (string) $request->get_param( 'dir' ) ); if ( '' !== $dir ) { // A folder somebody is about to choose (the "Check this folder" button): looked at, not saved. return rest_ensure_response( array( 'probe' => Rollbakr_Storage::probe_dir( $dir ) ) ); } $result = Rollbakr_Storage::check( true ); $result['checked_text'] = wp_date( Rollbakr_Admin::time_format(), (int) $result['checked'] ); return rest_ensure_response( array_merge( self::status_data(), array( 'check' => $result, 'safe' => Rollbakr_Storage::is_safe( $result ) ) ) ); } /** * POST /jobs/{id}/step. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function step( WP_REST_Request $request ) { $job = Rollbakr_Job::step( (string) $request['id'] ); if ( is_wp_error( $job ) ) { $job->add_data( array( 'status' => 'rollbakr_job_busy' === $job->get_error_code() ? 409 : 404 ) ); return $job; } return rest_ensure_response( self::job_data( $job ) ); } /** * POST /jobs/{id}/cancel. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function cancel( WP_REST_Request $request ) { $result = Rollbakr_Job::cancel( (string) $request['id'] ); if ( is_wp_error( $result ) ) { $result->add_data( array( 'status' => in_array( $result->get_error_code(), array( 'rollbakr_job_busy', 'rollbakr_job_done' ), true ) ? 409 : 404 ) ); return $result; } return rest_ensure_response( array( 'cancelled' => true ) ); } /** * POST /jobs/{id}/dismiss: forget a finished or failed job. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function dismiss( WP_REST_Request $request ) { $result = Rollbakr_Job::dismiss( (string) $request['id'] ); if ( is_wp_error( $result ) ) { $result->add_data( array( 'status' => in_array( $result->get_error_code(), array( 'rollbakr_job_busy', 'rollbakr_job_running' ), true ) ? 409 : 404 ) ); return $result; } return rest_ensure_response( array( 'dismissed' => true ) ); } }