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.
Built from https://develop.svn.wordpress.org/trunk@39126


git-svn-id: http://core.svn.wordpress.org/trunk@39066 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Rachel Baker
2016-11-04 17:11:29 +00:00
parent 557496ce83
commit 5564716a07
6 changed files with 50 additions and 13 deletions

View File

@@ -85,6 +85,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'force' => array(
'type' => 'boolean',
'default' => false,
'description' => __( 'Required to be true, as resource does not support trashing.' ),
),
@@ -114,6 +115,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
'permission_callback' => array( $this, 'delete_current_item_permissions_check' ),
'args' => array(
'force' => array(
'type' => 'boolean',
'default' => false,
'description' => __( 'Required to be true, as resource does not support trashing.' ),
),
@@ -653,7 +655,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
// We don't support trashing for this type, error out.
if ( ! $force ) {
return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing.' ), array( 'status' => 501 ) );
return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
}
$user = get_userdata( $id );
@@ -670,7 +672,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $user, $request );
$previous = $this->prepare_item_for_response( $user, $request );
/** Include admin user functions to get access to wp_delete_user() */
require_once ABSPATH . 'wp-admin/includes/user.php';
@@ -681,6 +683,9 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) );
}
$response = new WP_REST_Response();
$response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
/**
* Fires immediately after a user is deleted via the REST API.
*