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-posts-controller.php

    r39108 r39126  
    102102                'args'                => array(
    103103                    'force' => array(
     104                        'type'        => 'boolean',
    104105                        'default'     => false,
    105106                        'description' => __( 'Whether to bypass trash and force deletion.' ),
     
    758759        $request->set_param( 'context', 'edit' );
    759760
    760         $response = $this->prepare_item_for_response( $post, $request );
    761761
    762762        // If we're forcing, then delete permanently.
    763763        if ( $force ) {
     764            $previous = $this->prepare_item_for_response( $post, $request );
    764765            $result = wp_delete_post( $id, true );
     766            $response = new WP_REST_Response();
     767            $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    765768        } else {
    766769            // If we don't support trashing for this type, error out.
    767770            if ( ! $supports_trash ) {
    768                 return new WP_Error( 'rest_trash_not_supported', __( 'The post does not support trashing.' ), array( 'status' => 501 ) );
     771                return new WP_Error( 'rest_trash_not_supported', __( 'The post does not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    769772            }
    770773
     
    777780            // the trash is disabled.)
    778781            $result = wp_trash_post( $id );
     782            $post = $this->get_post( $id );
     783            $response = $this->prepare_item_for_response( $post, $request );
    779784        }
    780785
Note: See TracChangeset for help on using the changeset viewer.