Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Sep 24, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions 13 lib/endpoints/class-wp-rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function update_item( $request ) {
* Delete a comment.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|array
* @return WP_Error|WP_REST_Response
*/
public function delete_item( $request ) {
$id = (int) $request['id'];
Expand All @@ -275,15 +275,24 @@ public function delete_item( $request ) {

if ( $force ) {
$result = wp_delete_comment( $comment->comment_ID, true );
$status = 'deleted';
} else {
// If we don't support trashing for this type, error out
if ( ! $supports_trash ) {
return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing.' ), array( 'status' => 501 ) );
}

$result = wp_trash_comment( $comment->comment_ID );
$status = 'trashed';
}

$data = $response->get_data();
$data = array(
'data' => $data,
$status => true,
);
$response->set_data( $data );

if ( ! $result ) {
return new WP_Error( 'rest_cannot_delete', __( 'The comment cannot be deleted.' ), array( 'status' => 500 ) );
}
Expand Down Expand Up @@ -422,7 +431,7 @@ public function delete_item_permissions_check( $request ) {
*
* @param object $comment Comment object.
* @param WP_REST_Request $request Request object.
* @return array $fields
* @return WP_REST_Response
*/
public function prepare_item_for_response( $comment, $request ) {
$data = array(
Expand Down
6 changes: 3 additions & 3 deletions 6 lib/endpoints/class-wp-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function get_item( $request ) {
* Create one item from the collection
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Request
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
return new WP_Error( 'invalid-method', __( 'Method not implemented. Must be over-ridden in subclass.' ), array( 'status' => 405 ) );
Expand All @@ -44,7 +44,7 @@ public function create_item( $request ) {
* Update one item from the collection
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Request
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
return new WP_Error( 'invalid-method', __( 'Method not implemented. Must be over-ridden in subclass.' ), array( 'status' => 405 ) );
Expand All @@ -54,7 +54,7 @@ public function update_item( $request ) {
* Delete one item from the collection
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Request
* @return WP_Error|WP_REST_Response
*/
public function delete_item( $request ) {
return new WP_Error( 'invalid-method', __( 'Method not implemented. Must be over-ridden in subclass.' ), array( 'status' => 405 ) );
Expand Down
11 changes: 10 additions & 1 deletion 11 lib/endpoints/class-wp-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function update_item( $request ) {
* Delete a single post
*
* @param WP_REST_Request $request Full details about the request
* @return array|WP_Error
* @return WP_REST_Response|WP_Error
*/
public function delete_item( $request ) {
$id = (int) $request['id'];
Expand Down Expand Up @@ -344,6 +344,7 @@ public function delete_item( $request ) {
// If we're forcing, then delete permanently
if ( $force ) {
$result = wp_delete_post( $id, true );
$status = 'deleted';
} else {
// If we don't support trashing for this type, error out
if ( ! $supports_trash ) {
Expand All @@ -358,12 +359,20 @@ public function delete_item( $request ) {
// (Note that internally this falls through to `wp_delete_post` if
// the trash is disabled.)
$result = wp_trash_post( $id );
$status = 'trashed';
}

if ( ! $result ) {
return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
}

$data = $response->get_data();
$data = array(
'data' => $data,
$status => true,
);
$response->set_data( $data );

return $response;
}

Expand Down
9 changes: 8 additions & 1 deletion 9 lib/endpoints/class-wp-rest-terms-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function update_item( $request ) {
* Delete a single term from a taxonomy
*
* @param WP_REST_Request $request Full details about the request
* @return null
* @return WP_REST_Response|WP_Error
*/
public function delete_item( $request ) {

Expand All @@ -260,6 +260,13 @@ public function delete_item( $request ) {
$get_request->set_param( 'context', 'view' );
$response = $this->prepare_item_for_response( $term, $get_request );

$data = $response->get_data();
$data = array(
'data' => $data,
'deleted' => true,
);
$response->set_data( $data );

$retval = wp_delete_term( $term->term_id, $term->taxonomy );
if ( ! $retval ) {
return new WP_Error( 'rest_cannot_delete', __( 'The term cannot be deleted.' ), array( 'status' => 500 ) );
Expand Down
9 changes: 8 additions & 1 deletion 9 lib/endpoints/class-wp-rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ public function delete_item( $request ) {
$get_request->set_param( 'context', 'edit' );
$orig_user = $this->prepare_item_for_response( $user, $get_request );

$data = $orig_user->get_data();
$data = array(
'data' => $data,
'deleted' => true,
);
$orig_user->set_data( $data );

$result = wp_delete_user( $id, $reassign );

if ( ! $result ) {
Expand Down Expand Up @@ -440,7 +447,7 @@ public function delete_item_permissions_check( $request ) {
*
* @param object $user User object.
* @param WP_REST_Request $request Request object.
* @return array $data Response data.
* @return WP_REST_Response Response data.
*/
public function prepare_item_for_response( $user, $request ) {
$data = array(
Expand Down
22 changes: 21 additions & 1 deletion 22 tests/test-rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,27 @@ public function test_delete_item() {
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( $this->post_id, $data['post'] );
$this->assertEquals( $this->post_id, $data['data']['post'] );
$this->assertTrue( $data['trashed'] );
}

public function test_delete_item_skip_trash() {
wp_set_current_user( $this->admin_id );

$comment_id = $this->factory->comment->create( array(
'comment_approved' => 1,
'comment_post_ID' => $this->post_id,
'user_id' => $this->subscriber_id,
));
$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
$request['force'] = true;

$response = $this->server->dispatch( $request );
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( $this->post_id, $data['data']['post'] );
$this->assertTrue( $data['deleted'] );
}

public function test_delete_comment_invalid_id() {
Expand Down
19 changes: 18 additions & 1 deletion 19 tests/test-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,24 @@ public function test_delete_item() {
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'Deleted post', $data['title']['raw'] );
$this->assertEquals( 'Deleted post', $data['data']['title']['raw'] );
$this->assertTrue( $data['trashed'] );
}

public function test_delete_item_skip_trash() {
$post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
wp_set_current_user( $this->editor_id );

$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
$request['force'] = true;
$response = $this->server->dispatch( $request );

$this->assertNotInstanceOf( 'WP_Error', $response );
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'Deleted post', $data['data']['title']['raw'] );
$this->assertTrue( $data['deleted'] );
}

public function test_delete_post_invalid_id() {
Expand Down
3 changes: 2 additions & 1 deletion 3 tests/test-rest-terms-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ public function test_delete_item() {
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'Deleted Category', $data['name'] );
$this->assertEquals( 'Deleted Category', $data['data']['name'] );
$this->assertTrue( $data['deleted'] );
}

public function test_delete_item_invalid_taxonomy() {
Expand Down
3 changes: 2 additions & 1 deletion 3 tests/test-rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@ public function test_delete_item() {
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'Deleted User', $data['name'] );
$this->assertEquals( 'Deleted User', $data['data']['name'] );
$this->assertTrue( $data['deleted'] );
}

public function test_delete_item_no_trash() {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.