WordPress.org

Make WordPress Core

Ticket #38494: 38494.4.diff

File 38494.4.diff, 20.1 KB (added by jnylen0, 3 years ago)

Several fixes and improvements

  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
    index 3454dc1..3981050 100644
    a b class WP_REST_Comments_Controller extends WP_REST_Controller { 
    8383                                'permission_callback' => array( $this, 'delete_item_permissions_check' ),
    8484                                'args'     => array(
    8585                                        'force'    => array(
     86                                                'type'        => 'boolean',
    8687                                                'default'     => false,
    8788                                                'description' => __( 'Whether to bypass trash and force deletion.' ),
    8889                                        ),
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    726727
    727728                $request->set_param( 'context', 'edit' );
    728729
    729                 $response = $this->prepare_item_for_response( $comment, $request );
    730 
    731730                if ( $force ) {
     731                        $previous = $this->prepare_item_for_response( $comment, $request );
    732732                        $result = wp_delete_comment( $comment->comment_ID, true );
     733                        $response = new WP_REST_Response();
     734                        $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    733735                } else {
    734736                        // If this type doesn't support trashing, error out.
    735737                        if ( ! $supports_trash ) {
    736                                 return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing.' ), array( 'status' => 501 ) );
     738                                return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    737739                        }
    738740
    739741                        if ( 'trash' === $comment->comment_approved ) {
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    741743                        }
    742744
    743745                        $result = wp_trash_comment( $comment->comment_ID );
     746                        $comment = get_comment( $comment->comment_ID );
     747                        $response = $this->prepare_item_for_response( $comment, $request );
    744748                }
    745749
    746750                if ( ! $result ) {
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 0122f9a..24fd100 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    101101                                'permission_callback' => array( $this, 'delete_item_permissions_check' ),
    102102                                'args'                => array(
    103103                                        'force' => array(
     104                                                'type'        => 'boolean',
    104105                                                'default'     => false,
    105106                                                'description' => __( 'Whether to bypass trash and force deletion.' ),
    106107                                        ),
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    749750
    750751                $request->set_param( 'context', 'edit' );
    751752
    752                 $response = $this->prepare_item_for_response( $post, $request );
    753753
    754754                // If we're forcing, then delete permanently.
    755755                if ( $force ) {
     756                        $previous = $this->prepare_item_for_response( $post, $request );
    756757                        $result = wp_delete_post( $id, true );
     758                        $response = new WP_REST_Response();
     759                        $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    757760                } else {
    758761                        // If we don't support trashing for this type, error out.
    759762                        if ( ! $supports_trash ) {
    760                                 return new WP_Error( 'rest_trash_not_supported', __( 'The post does not support trashing.' ), array( 'status' => 501 ) );
     763                                return new WP_Error( 'rest_trash_not_supported', __( 'The post does not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    761764                        }
    762765
    763766                        // Otherwise, only trash if we haven't already.
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    768771                        // (Note that internally this falls through to `wp_delete_post` if
    769772                        // the trash is disabled.)
    770773                        $result = wp_trash_post( $id );
     774                        $post = $this->get_post( $id );
     775                        $response = $this->prepare_item_for_response( $post, $request );
    771776                }
    772777
    773778                if ( ! $result ) {
  • src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
    index 9ecd0ad..9373a9b 100644
    a b class WP_REST_Revisions_Controller extends WP_REST_Controller { 
    9393                                'methods'             => WP_REST_Server::DELETABLE,
    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' ),
    98105                ));
    class WP_REST_Revisions_Controller extends WP_REST_Controller { 
    220227         * @return true|WP_Error True on success, or WP_Error object on failure.
    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
    225242                /**
    class WP_REST_Revisions_Controller extends WP_REST_Controller { 
    234251                 */
    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
    244263        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
    index ceaa26b..13962a7 100644
    a b class WP_REST_Terms_Controller extends WP_REST_Controller { 
    116116                                'permission_callback' => array( $this, 'delete_item_permissions_check' ),
    117117                                'args'                => array(
    118118                                        'force' => array(
     119                                                'type'        => 'boolean',
    119120                                                'default'     => false,
    120121                                                'description' => __( 'Required to be true, as resource does not support trashing.' ),
    121122                                        ),
    class WP_REST_Terms_Controller extends WP_REST_Controller { 
    566567
    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
    572573                $term = get_term( (int) $request['id'], $this->taxonomy );
    573574
    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 );
    579580
    class WP_REST_Terms_Controller extends WP_REST_Controller { 
    581582                        return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) );
    582583                }
    583584
     585                $response = new WP_REST_Response();
     586                $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     587
    584588                /**
    585589                 * Fires after a single term is deleted via the REST API.
    586590                 *
  • src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
    index 9a8cc21..06b608f 100644
    a b class WP_REST_Users_Controller extends WP_REST_Controller { 
    8585                                'permission_callback' => array( $this, 'delete_item_permissions_check' ),
    8686                                'args'                => array(
    8787                                        'force'    => array(
     88                                                'type'        => 'boolean',
    8889                                                'default'     => false,
    8990                                                'description' => __( 'Required to be true, as resource does not support trashing.' ),
    9091                                        ),
    class WP_REST_Users_Controller extends WP_REST_Controller { 
    653654
    654655                // We don't support trashing for this type, error out.
    655656                if ( ! $force ) {
    656                         return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing.' ), array( 'status' => 501 ) );
     657                        return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
    657658                }
    658659
    659660                $user = get_userdata( $id );
    class WP_REST_Users_Controller extends WP_REST_Controller { 
    670671
    671672                $request->set_param( 'context', 'edit' );
    672673
    673                 $response = $this->prepare_item_for_response( $user, $request );
     674                $previous = $this->prepare_item_for_response( $user, $request );
    674675
    675676                /** Include admin user functions to get access to wp_delete_user() */
    676677                require_once ABSPATH . 'wp-admin/includes/user.php';
    class WP_REST_Users_Controller extends WP_REST_Controller { 
    681682                        return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) );
    682683                }
    683684
     685                $response = new WP_REST_Response();
     686                $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     687
    684688                /**
    685689                 * Fires immediately after a user is deleted via the REST API.
    686690                 *
  • tests/phpunit/tests/rest-api/rest-attachments-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php
    index 7960312..747f1a0 100644
    a b class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    699699                $response = $this->server->dispatch( $request );
    700700                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    701701
     702                $request->set_param( 'force', 'false' );
     703                $response = $this->server->dispatch( $request );
     704                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     705
    702706                // Ensure the post still exists
    703707                $post = get_post( $attachment_id );
    704708                $this->assertNotEmpty( $post );
  • tests/phpunit/tests/rest-api/rest-categories-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-categories-controller.php b/tests/phpunit/tests/rest-api/rest-categories-controller.php
    index 440cb45..11754f9 100644
    a b class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas 
    716716                $response = $this->server->dispatch( $request );
    717717                $this->assertEquals( 200, $response->get_status() );
    718718                $data = $response->get_data();
    719                 $this->assertEquals( 'Deleted Category', $data['name'] );
     719                $this->assertTrue( $data['deleted'] );
     720                $this->assertEquals( 'Deleted Category', $data['previous']['name'] );
    720721        }
    721722
    722         public function test_delete_item_force_false() {
     723        public function test_delete_item_no_trash() {
    723724                wp_set_current_user( self::$administrator );
    724725                $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
     726
    725727                $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
    726                 // force defaults to false
    727728                $response = $this->server->dispatch( $request );
    728                 $this->assertEquals( 501, $response->get_status() );
     729                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     730
     731                $request->set_param( 'force', 'false' );
     732                $response = $this->server->dispatch( $request );
     733                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    729734        }
    730735
    731736        public function test_delete_item_invalid_taxonomy() {
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
    index 8dc8c4c..4316fbd 100644
    a b class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    16171617                        'comment_post_ID'  => self::$post_id,
    16181618                        'user_id'          => self::$subscriber_id,
    16191619                ));
    1620                 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
    16211620
     1621                $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
     1622                $request->set_param( 'force', 'false' );
    16221623                $response = $this->server->dispatch( $request );
    16231624                $this->assertEquals( 200, $response->get_status() );
     1625
    16241626                $data = $response->get_data();
    1625                 $this->assertEquals( self::$post_id, $data['post'] );
     1627                $this->assertEquals( 'trash', $data['status'] );
    16261628        }
    16271629
    16281630        public function test_delete_item_skip_trash() {
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    16391641                $response = $this->server->dispatch( $request );
    16401642                $this->assertEquals( 200, $response->get_status() );
    16411643                $data = $response->get_data();
    1642                 $this->assertEquals( self::$post_id, $data['post'] );
     1644                $this->assertTrue( $data['deleted'] );
     1645                $this->assertNotEmpty( $data['previous']['post'] );
    16431646        }
    16441647
    16451648        public function test_delete_item_already_trashed() {
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index dcebfe8..4fca5df 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    18601860                wp_set_current_user( self::$editor_id );
    18611861
    18621862                $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1863                $request->set_param( 'force', 'false' );
    18631864                $response = $this->server->dispatch( $request );
    18641865
    18651866                $this->assertNotInstanceOf( 'WP_Error', $response );
    18661867                $this->assertEquals( 200, $response->get_status() );
    18671868                $data = $response->get_data();
    18681869                $this->assertEquals( 'Deleted post', $data['title']['raw'] );
     1870                $this->assertEquals( 'trash', $data['status'] );
    18691871        }
    18701872
    18711873        public function test_delete_item_skip_trash() {
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    18791881                $this->assertNotInstanceOf( 'WP_Error', $response );
    18801882                $this->assertEquals( 200, $response->get_status() );
    18811883                $data = $response->get_data();
    1882                 $this->assertEquals( 'Deleted post', $data['title']['raw'] );
     1884                $this->assertTrue( $data['deleted'] );
     1885                $this->assertNotEmpty( $data['previous'] );
    18831886        }
    18841887
    18851888        public function test_delete_item_already_trashed() {
  • tests/phpunit/tests/rest-api/rest-revisions-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-revisions-controller.php b/tests/phpunit/tests/rest-api/rest-revisions-controller.php
    index 5d08366..3c88c6f 100644
    a b class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase 
    184184        public function test_delete_item() {
    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 ) );
    190191        }
    191192
     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 ) );
     206        }
     207
    192208        public function test_delete_item_no_permission() {
    193209                wp_set_current_user( self::$contributor_id );
    194210                $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
  • tests/phpunit/tests/rest-api/rest-tags-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-tags-controller.php b/tests/phpunit/tests/rest-api/rest-tags-controller.php
    index 0bdae85..e94f113 100644
    a b class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { 
    599599                $response = $this->server->dispatch( $request );
    600600                $this->assertEquals( 200, $response->get_status() );
    601601                $data = $response->get_data();
    602                 $this->assertEquals( 'Deleted Tag', $data['name'] );
     602                $this->assertTrue( $data['deleted'] );
     603                $this->assertEquals( 'Deleted Tag', $data['previous']['name'] );
    603604        }
    604605
    605         public function test_delete_item_force_false() {
     606        public function test_delete_item_no_trash() {
    606607                wp_set_current_user( self::$administrator );
    607608                $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
     609
    608610                $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
    609                 // force defaults to false
    610611                $response = $this->server->dispatch( $request );
    611                 $this->assertEquals( 501, $response->get_status() );
     612                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     613
     614                $request->set_param( 'force', 'false' );
     615                $response = $this->server->dispatch( $request );
     616                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    612617        }
    613618
    614619        public function test_delete_item_invalid_term() {
    class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { 
    641646
    642647                $this->assertEquals( 200, $response->get_status() );
    643648                $data = $response->get_data();
    644                 $this->assertEquals( 'Deleted Tag', $data['name'] );
     649                $this->assertTrue( $data['deleted'] );
     650                $this->assertEquals( 'Deleted Tag', $data['previous']['name'] );
    645651        }
    646652
    647653        public function grant_delete_term( $caps, $cap ) {
  • tests/phpunit/tests/rest-api/rest-users-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php
    index 574e571..947ebf0 100644
    a b class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 
    11131113
    11141114                $this->assertEquals( 200, $response->get_status() );
    11151115                $data = $response->get_data();
    1116                 $this->assertEquals( 'Deleted User', $data['name'] );
     1116                $this->assertTrue( $data['deleted'] );
     1117                $this->assertEquals( 'Deleted User', $data['previous']['name'] );
    11171118        }
    11181119
    11191120        public function test_delete_current_item() {
    class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 
    11291130
    11301131                $this->assertEquals( 200, $response->get_status() );
    11311132                $data = $response->get_data();
    1132                 $this->assertEquals( 'Deleted User', $data['name'] );
     1133                $this->assertTrue( $data['deleted'] );
     1134                $this->assertEquals( 'Deleted User', $data['previous']['name'] );
    11331135        }
    11341136
    11351137        public function test_delete_item_no_trash() {
    class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 
    11391141                wp_set_current_user( self::$user );
    11401142
    11411143                $userdata = get_userdata( $user_id ); // cache for later
     1144
    11421145                $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
    11431146                $response = $this->server->dispatch( $request );
    11441147                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    11451148
     1149                $request->set_param( 'force', 'false' );
     1150                $response = $this->server->dispatch( $request );
     1151                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     1152
    11461153                // Ensure the user still exists
    11471154                $user = get_user_by( 'id', $user_id );
    11481155                $this->assertNotEmpty( $user );