*/ protected function input_properties(): array { return array( 'id' => array( 'type' => 'string', 'description' => __( 'Backup identifier from updraftplus/list-backups.', 'acrossai-abilities-manager' ), ), 'components' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ), 'description' => __( 'Restrict the restore to these components of the set. Omit to restore all of them.', 'acrossai-abilities-manager' ), ), ); } /** * @since 0.0.35 * @return array */ protected function required_input(): array { return array('id'); } /** * @since 0.0.35 * @return array */ protected function output_properties(): array { return array( 'backup_id' => array( 'type' => 'string' ), 'entities' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ), 'job_id' => array( 'type' => array( 'string', 'null' ) ), 'replaced_state' => array( 'type' => 'object', 'additionalProperties' => true, 'description' => __( 'What the site looked like immediately before the restore, recorded so the loss is visible.', 'acrossai-abilities-manager' ), ), 'irreversible' => array( 'type' => 'boolean' ), 'note' => array( 'type' => 'string' ), ); } /** * @since 0.0.35 * @return array */ protected function annotations(): array { return array( 'readonly' => false, 'destructive' => true, 'idempotent' => false, ); } /** * @since 0.0.35 * @param array $input Input. * @return array|WP_Error */ protected function run( array $input ) { $id = (string) $input['id']; $backup = Backup_Repository::get_backup( $id ); if ( is_wp_error( $backup ) ) { return $backup; } // Recorded BEFORE the restore, because afterwards there is nothing left to read it from. $before = self::current_state( isset( $backup['created'] ) ? (int) $backup['created'] : 0 ); $result = Backup_Repository::restore_backup( $id, $input ); if ( is_wp_error( $result ) ) { return $result; } $result['replaced_state'] = $before; $result['irreversible'] = true; $result['note'] = __( 'The restore has run and cannot be undone. Anything created or changed since the backup was taken is gone. Expect to sign in again if the users table was part of the set.', 'acrossai-abilities-manager' ); return $result; } /** * What is about to be overwritten. * * Counts rather than contents: enough for an operator to recognise the scale of what a restore * discards, without dumping the site into a response. * * @since 0.0.35 * @param int $backup_time When the backup being restored was taken. * @return array */ private static function current_state( int $backup_time ): array { $state = array( 'checked_at' => time(), 'backup_time' => $backup_time > 0 ? $backup_time : null, 'users' => (int) count_users()['total_users'], ); $posts = wp_count_posts( 'post' ); $pages = wp_count_posts( 'page' ); $state['published_posts'] = isset( $posts->publish ) ? (int) $posts->publish : 0; $state['published_pages'] = isset( $pages->publish ) ? (int) $pages->publish : 0; if ( $backup_time > 0 ) { $since = new \WP_Query( array( 'post_type' => 'any', 'post_status' => 'any', 'date_query' => array( array( 'after' => gmdate( 'Y-m-d H:i:s', $backup_time ), 'inclusive' => false ) ), 'posts_per_page' => 1, 'fields' => 'ids', 'no_found_rows' => false, ) ); $state['content_written_since_backup'] = (int) $since->found_posts; } return $state; } }