Make WordPress Core


Ignore:
Timestamp:
11/03/2016 08:04:59 PM (8 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-comments-controller.php

    r39106 r39126  
    8484                'args'     => array(
    8585                    'force'    => array(
     86                        'type'        => 'boolean',
    8687                        'default'     => false,
    8788                        'description' => __( 'Whether to bypass trash and force deletion.' ),
     
    739740        $request->set_param( 'context', 'edit' );
    740741
    741         $response = $this->prepare_item_for_response( $comment, $request );
    742 
    743742        if ( $force ) {
     743            $previous = $this->prepare_item_for_response( $comment, $request );
    744744            $result = wp_delete_comment( $comment->comment_ID, true );
     745            $response = new WP_REST_Response();
     746            $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    745747        } else {
    746748            // If this type doesn't support trashing, error out.
    747749            if ( ! $supports_trash ) {
    748                 return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing.' ), array( 'status' => 501 ) );
     750                return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    749751            }
    750752
     
    754756
    755757            $result = wp_trash_comment( $comment->comment_ID );
     758            $comment = get_comment( $comment->comment_ID );
     759            $response = $this->prepare_item_for_response( $comment, $request );
    756760        }
    757761
Note: See TracChangeset for help on using the changeset viewer.