WordPress.org

Make WordPress Core

Changeset 39126


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

Location:
trunk
Files:
12 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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39108 r39126  
    102102                'args'                => array(
    103103                    'force' => array(
     104                        'type'        => 'boolean',
    104105                        'default'     => false,
    105106                        'description' => __( 'Whether to bypass trash and force deletion.' ),
     
    758759        $request->set_param( 'context', 'edit' );
    759760
    760         $response = $this->prepare_item_for_response( $post, $request );
    761761
    762762        // If we're forcing, then delete permanently.
    763763        if ( $force ) {
     764            $previous = $this->prepare_item_for_response( $post, $request );
    764765            $result = wp_delete_post( $id, true );
     766            $response = new WP_REST_Response();
     767            $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    765768        } else {
    766769            // If we don't support trashing for this type, error out.
    767770            if ( ! $supports_trash ) {
    768                 return new WP_Error( 'rest_trash_not_supported', __( 'The post does not support trashing.' ), array( 'status' => 501 ) );
     771                return new WP_Error( 'rest_trash_not_supported', __( 'The post does not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    769772            }
    770773
     
    777780            // the trash is disabled.)
    778781            $result = wp_trash_post( $id );
     782            $post = $this->get_post( $id );
     783            $response = $this->prepare_item_for_response( $post, $request );
    779784        }
    780785
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r39106 r39126  
    9494                'callback'            => array( $this, 'delete_item' ),
    9595                'permission_callback' => array( $this, 'delete_item_permissions_check' ),
     96                'args'                => array(
     97                    'force' => array(
     98                        'type'        => 'boolean',
     99                        'default'     => false,
     100                        'description' => __( 'Required to be true, as resource does not support trashing.' ),
     101                    ),
     102                ),
    96103            ),
    97104            'schema' => array( $this, 'get_public_item_schema' ),
     
    221228     */
    222229    public function delete_item( $request ) {
     230        $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
     231
     232        // We don't support trashing for this resource type.
     233        if ( ! $force ) {
     234            return new WP_Error( 'rest_trash_not_supported', __( 'Revisions do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
     235        }
     236
     237        $revision = $this->get_post( $request['id'] );
     238        $previous = $this->prepare_item_for_response( $revision, $request );
     239
    223240        $result = wp_delete_post( $request['id'], true );
    224241
     
    235252        do_action( 'rest_delete_revision', $result, $request );
    236253
    237         if ( $result ) {
    238             return true;
    239         } else {
     254        if ( ! $result ) {
    240255            return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
    241256        }
     257
     258        $response = new WP_REST_Response();
     259        $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     260        return $response;
    242261    }
    243262
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r39106 r39126  
    117117                'args'                => array(
    118118                    'force' => array(
     119                        'type'        => 'boolean',
    119120                        'default'     => false,
    120121                        'description' => __( 'Required to be true, as resource does not support trashing.' ),
     
    567568        // We don't support trashing for this resource type.
    568569        if ( ! $force ) {
    569             return new WP_Error( 'rest_trash_not_supported', __( 'Resource does not support trashing.' ), array( 'status' => 501 ) );
     570            return new WP_Error( 'rest_trash_not_supported', __( 'Terms do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    570571        }
    571572
     
    574575        $request->set_param( 'context', 'view' );
    575576
    576         $response = $this->prepare_item_for_response( $term, $request );
     577        $previous = $this->prepare_item_for_response( $term, $request );
    577578
    578579        $retval = wp_delete_term( $term->term_id, $term->taxonomy );
     
    581582            return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) );
    582583        }
     584
     585        $response = new WP_REST_Response();
     586        $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    583587
    584588        /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r39106 r39126  
    8686                'args'                => array(
    8787                    'force'    => array(
     88                        'type'        => 'boolean',
    8889                        'default'     => false,
    8990                        'description' => __( 'Required to be true, as resource does not support trashing.' ),
     
    115116                'args'                => array(
    116117                    'force'    => array(
     118                        'type'        => 'boolean',
    117119                        'default'     => false,
    118120                        'description' => __( 'Required to be true, as resource does not support trashing.' ),
     
    654656        // We don't support trashing for this type, error out.
    655657        if ( ! $force ) {
    656             return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing.' ), array( 'status' => 501 ) );
     658            return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    657659        }
    658660
     
    671673        $request->set_param( 'context', 'edit' );
    672674
    673         $response = $this->prepare_item_for_response( $user, $request );
     675        $previous = $this->prepare_item_for_response( $user, $request );
    674676
    675677        /** Include admin user functions to get access to wp_delete_user() */
     
    681683            return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) );
    682684        }
     685
     686        $response = new WP_REST_Response();
     687        $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    683688
    684689        /**
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r39104 r39126  
    731731        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    732732
     733        $request->set_param( 'force', 'false' );
     734        $response = $this->server->dispatch( $request );
     735        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     736
    733737        // Ensure the post still exists
    734738        $post = get_post( $attachment_id );
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r39105 r39126  
    726726        $this->assertEquals( 200, $response->get_status() );
    727727        $data = $response->get_data();
    728         $this->assertEquals( 'Deleted Category', $data['name'] );
    729     }
    730 
    731     public function test_delete_item_force_false() {
     728        $this->assertTrue( $data['deleted'] );
     729        $this->assertEquals( 'Deleted Category', $data['previous']['name'] );
     730    }
     731
     732    public function test_delete_item_no_trash() {
    732733        wp_set_current_user( self::$administrator );
    733734        $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
     735
    734736        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
    735         // force defaults to false
    736         $response = $this->server->dispatch( $request );
    737         $this->assertEquals( 501, $response->get_status() );
     737        $response = $this->server->dispatch( $request );
     738        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     739
     740        $request->set_param( 'force', 'false' );
     741        $response = $this->server->dispatch( $request );
     742        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    738743    }
    739744
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39105 r39126  
    18891889            'user_id'          => self::$subscriber_id,
    18901890        ));
     1891
    18911892        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
    1892 
    1893         $response = $this->server->dispatch( $request );
    1894         $this->assertEquals( 200, $response->get_status() );
    1895         $data = $response->get_data();
    1896         $this->assertEquals( self::$post_id, $data['post'] );
     1893        $request->set_param( 'force', 'false' );
     1894        $response = $this->server->dispatch( $request );
     1895        $this->assertEquals( 200, $response->get_status() );
     1896
     1897        $data = $response->get_data();
     1898        $this->assertEquals( 'trash', $data['status'] );
    18971899    }
    18981900
     
    19111913        $this->assertEquals( 200, $response->get_status() );
    19121914        $data = $response->get_data();
    1913         $this->assertEquals( self::$post_id, $data['post'] );
     1915        $this->assertTrue( $data['deleted'] );
     1916        $this->assertNotEmpty( $data['previous']['post'] );
    19141917    }
    19151918
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39108 r39126  
    20092009
    20102010        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2011        $request->set_param( 'force', 'false' );
    20112012        $response = $this->server->dispatch( $request );
    20122013
     
    20152016        $data = $response->get_data();
    20162017        $this->assertEquals( 'Deleted post', $data['title']['raw'] );
     2018        $this->assertEquals( 'trash', $data['status'] );
    20172019    }
    20182020
     
    20282030        $this->assertEquals( 200, $response->get_status() );
    20292031        $data = $response->get_data();
    2030         $this->assertEquals( 'Deleted post', $data['title']['raw'] );
     2032        $this->assertTrue( $data['deleted'] );
     2033        $this->assertNotEmpty( $data['previous'] );
    20312034    }
    20322035
  • trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r38975 r39126  
    185185        wp_set_current_user( self::$editor_id );
    186186        $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
     187        $request->set_param( 'force', true );
    187188        $response = $this->server->dispatch( $request );
    188189        $this->assertEquals( 200, $response->get_status() );
    189190        $this->assertNull( get_post( $this->revision_id1 ) );
     191    }
     192
     193    public function test_delete_item_no_trash() {
     194        wp_set_current_user( self::$editor_id );
     195
     196        $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
     197        $response = $this->server->dispatch( $request );
     198        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     199
     200        $request->set_param( 'force', 'false' );
     201        $response = $this->server->dispatch( $request );
     202        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     203
     204        // Ensure the revision still exists
     205        $this->assertNotNull( get_post( $this->revision_id1 ) );
    190206    }
    191207
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r39105 r39126  
    626626        $this->assertEquals( 200, $response->get_status() );
    627627        $data = $response->get_data();
    628         $this->assertEquals( 'Deleted Tag', $data['name'] );
    629     }
    630 
    631     public function test_delete_item_force_false() {
     628        $this->assertTrue( $data['deleted'] );
     629        $this->assertEquals( 'Deleted Tag', $data['previous']['name'] );
     630    }
     631
     632    public function test_delete_item_no_trash() {
    632633        wp_set_current_user( self::$administrator );
    633634        $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
     635
    634636        $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
    635         // force defaults to false
    636         $response = $this->server->dispatch( $request );
    637         $this->assertEquals( 501, $response->get_status() );
     637        $response = $this->server->dispatch( $request );
     638        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     639
     640        $request->set_param( 'force', 'false' );
     641        $response = $this->server->dispatch( $request );
     642        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    638643    }
    639644
     
    668673        $this->assertEquals( 200, $response->get_status() );
    669674        $data = $response->get_data();
    670         $this->assertEquals( 'Deleted Tag', $data['name'] );
     675        $this->assertTrue( $data['deleted'] );
     676        $this->assertEquals( 'Deleted Tag', $data['previous']['name'] );
    671677    }
    672678
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r39105 r39126  
    11551155        $this->assertEquals( 200, $response->get_status() );
    11561156        $data = $response->get_data();
    1157         $this->assertEquals( 'Deleted User', $data['name'] );
     1157        $this->assertTrue( $data['deleted'] );
     1158        $this->assertEquals( 'Deleted User', $data['previous']['name'] );
     1159    }
     1160
     1161    public function test_delete_item_no_trash() {
     1162        $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) );
     1163
     1164        $this->allow_user_to_manage_multisite();
     1165        wp_set_current_user( self::$user );
     1166
     1167        $userdata = get_userdata( $user_id ); // cache for later
     1168
     1169        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
     1170        $response = $this->server->dispatch( $request );
     1171        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     1172
     1173        $request->set_param( 'force', 'false' );
     1174        $response = $this->server->dispatch( $request );
     1175        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     1176
     1177        // Ensure the user still exists
     1178        $user = get_user_by( 'id', $user_id );
     1179        $this->assertNotEmpty( $user );
    11581180    }
    11591181
     
    11711193        $this->assertEquals( 200, $response->get_status() );
    11721194        $data = $response->get_data();
    1173         $this->assertEquals( 'Deleted User', $data['name'] );
    1174     }
    1175 
    1176     public function test_delete_item_no_trash() {
    1177         $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) );
    1178 
    1179         $this->allow_user_to_manage_multisite();
    1180         wp_set_current_user( self::$user );
    1181 
    1182         $userdata = get_userdata( $user_id ); // cache for later
    1183         $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
    1184         $response = $this->server->dispatch( $request );
    1185         $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    1186 
    1187         // Ensure the user still exists
    1188         $user = get_user_by( 'id', $user_id );
    1189         $this->assertNotEmpty( $user );
     1195        $this->assertTrue( $data['deleted'] );
     1196        $this->assertEquals( 'Deleted User', $data['previous']['name'] );
    11901197    }
    11911198
    11921199    public function test_delete_current_item_no_trash() {
    1193         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     1200        $user_id = $this->factory->user->create( array( 'role' => 'administrator', 'display_name' => 'Deleted User' ) );
    11941201
    11951202        wp_set_current_user( $user_id );
     
    11971204        update_site_option( 'site_admins', array( $user->user_login ) );
    11981205
    1199         $userdata = get_userdata( $user_id ); // cache for later
    12001206        $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/me' );
     1207        $response = $this->server->dispatch( $request );
     1208        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     1209
     1210        $request->set_param( 'force', 'false' );
    12011211        $response = $this->server->dispatch( $request );
    12021212        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
Note: See TracChangeset for help on using the changeset viewer.