Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit 3debab3

Browse files
committed
Merge pull request #1254 from WP-API/1224-return-term
Return full term object when deleting term
2 parents 18c3e4e + 7dc2595 commit 3debab3

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/endpoints/class-wp-rest-terms-controller.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,16 @@ public function delete_item( $request ) {
281281

282282
// Get the actual term_id
283283
$term = get_term_by( 'term_taxonomy_id', (int) $request['id'], $this->taxonomy );
284+
$get_request = new WP_REST_Request( 'GET', rest_url( 'wp/v2/terms/' . $this->get_taxonomy_base( $term->taxonomy ) . '/' . (int) $request['id'] ) );
285+
$get_request->set_param( 'context', 'view' );
286+
$response = $this->prepare_item_for_response( $term, $get_request );
284287

285-
wp_delete_term( $term->term_id, $term->taxonomy );
288+
$retval = wp_delete_term( $term->term_id, $term->taxonomy );
289+
if ( ! $retval ) {
290+
return new WP_Error( 'rest_cannot_delete', __( 'The term cannot be deleted.' ), array( 'status' => 500 ) );
291+
}
292+
293+
return $response;
286294
}
287295

288296
/**

tests/test-rest-terms-controller.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,12 @@ public function test_update_item_parent_non_hierarchical_taxonomy() {
413413

414414
public function test_delete_item() {
415415
wp_set_current_user( $this->administrator );
416-
$term = get_term_by( 'id', $this->factory->category->create(), 'category' );
416+
$term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
417417
$request = new WP_REST_Request( 'DELETE', '/wp/v2/terms/category/' . $term->term_taxonomy_id );
418418
$response = $this->server->dispatch( $request );
419419
$this->assertEquals( 200, $response->get_status() );
420+
$data = $response->get_data();
421+
$this->assertEquals( 'Deleted Category', $data['name'] );
420422
}
421423

422424
public function test_delete_item_invalid_taxonomy() {

0 commit comments

Comments
 (0)