Make WordPress Core


Ignore:
Timestamp:
11/03/2016 08:04:59 PM (9 years ago)
Author:
rachelbaker
Message:

REST API: Modify the structure of our DELETE responses to be more explicit.

Add the deleted property to the root of the Response object to communicate if the delete action was successful. Move the state of the resource prior to the delete request under a new previous property. As a result DELETE responses are now structured like so:

{ deleted: true, previous: { ... } }

Also includes helpful information to DELETE requests for resources that are not trashable.

Props timmydcrawford, rmccue, jnylen0.
Fixes #38494.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r39106 r39126  
    9494                'callback'            => array( $this, 'delete_item' ),
    9595                'permission_callback' => array( $this, 'delete_item_permissions_check' ),
     96                'args'                => array(
     97                    'force' => array(
     98                        'type'        => 'boolean',
     99                        'default'     => false,
     100                        'description' => __( 'Required to be true, as resource does not support trashing.' ),
     101                    ),
     102                ),
    96103            ),
    97104            'schema' => array( $this, 'get_public_item_schema' ),
     
    221228     */
    222229    public function delete_item( $request ) {
     230        $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
     231
     232        // We don't support trashing for this resource type.
     233        if ( ! $force ) {
     234            return new WP_Error( 'rest_trash_not_supported', __( 'Revisions do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
     235        }
     236
     237        $revision = $this->get_post( $request['id'] );
     238        $previous = $this->prepare_item_for_response( $revision, $request );
     239
    223240        $result = wp_delete_post( $request['id'], true );
    224241
     
    235252        do_action( 'rest_delete_revision', $result, $request );
    236253
    237         if ( $result ) {
    238             return true;
    239         } else {
     254        if ( ! $result ) {
    240255            return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
    241256        }
     257
     258        $response = new WP_REST_Response();
     259        $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     260        return $response;
    242261    }
    243262
Note: See TracChangeset for help on using the changeset viewer.