Skip to content
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: 13 additions & 0 deletions lib/endpoints/class-wp-rest-terms-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public function register_routes() {
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'force' => array(
'default' => false,
'description' => __( 'Required to be true, as resource does not support trashing.' ),
),
),
),

'schema' => array( $this, 'get_public_item_schema' ),
Expand Down Expand Up @@ -290,6 +296,13 @@ public function update_item( $request ) {
*/
public function delete_item( $request ) {

$force = isset( $request['force'] ) ? (bool) $request['force'] : false;

// We don't support trashing for this type, error out
if ( ! $force ) {
return new WP_Error( 'rest_trash_not_supported', __( 'Terms do not support trashing.' ), array( 'status' => 501 ) );
}

// Get the actual term_id
$term = get_term( (int) $request['id'], $this->taxonomy );
$get_request = new WP_REST_Request;
Expand Down
10 changes: 10 additions & 0 deletions tests/test-rest-categories-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,23 @@ public function test_delete_item() {
wp_set_current_user( $this->administrator );
$term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'Deleted Category', $data['data']['name'] );
$this->assertTrue( $data['deleted'] );
}

public function test_delete_item_force_false() {
wp_set_current_user( $this->administrator );
$term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
// force defaults to false
$response = $this->server->dispatch( $request );
$this->assertEquals( 501, $response->get_status() );
}

public function test_delete_item_invalid_taxonomy() {
wp_set_current_user( $this->administrator );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
Expand Down
10 changes: 10 additions & 0 deletions tests/test-rest-tags-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,23 @@ public function test_delete_item() {
wp_set_current_user( $this->administrator );
$term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'Deleted Tag', $data['data']['name'] );
$this->assertTrue( $data['deleted'] );
}

public function test_delete_item_force_false() {
wp_set_current_user( $this->administrator );
$term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
// force defaults to false
$response = $this->server->dispatch( $request );
$this->assertEquals( 501, $response->get_status() );
}

public function test_delete_item_invalid_term() {
wp_set_current_user( $this->administrator );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
Expand Down