WordPress.org

Make WordPress Core

Ticket #38494: 38494.2.diff

File 38494.2.diff, 10.1 KB (added by rmccue, 3 years ago)

Consistently require force=true

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

     
    726726
    727727                $request->set_param( 'context', 'edit' );
    728728
    729                 $response = $this->prepare_item_for_response( $comment, $request );
     729                $previous = $this->prepare_item_for_response( $comment, $request );
    730730
    731731                if ( $force ) {
    732732                        $result = wp_delete_comment( $comment->comment_ID, true );
     
    747747                        return new WP_Error( 'rest_cannot_delete', __( 'The comment cannot be deleted.' ), array( 'status' => 500 ) );
    748748                }
    749749
     750                $response = new WP_REST_Response();
     751                $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     752
    750753                /**
    751754                 * Fires after a comment is deleted via the REST API.
    752755                 *
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

     
    749749
    750750                $request->set_param( 'context', 'edit' );
    751751
    752                 $response = $this->prepare_item_for_response( $post, $request );
    753752
    754753                // If we're forcing, then delete permanently.
    755754                if ( $force ) {
     755                        $previous = $this->prepare_item_for_response( $post, $request );
    756756                        $result = wp_delete_post( $id, true );
     757                        $response = new WP_REST_Response();
     758                        $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
    757759                } else {
    758760                        // If we don't support trashing for this type, error out.
    759761                        if ( ! $supports_trash ) {
     
    768770                        // (Note that internally this falls through to `wp_delete_post` if
    769771                        // the trash is disabled.)
    770772                        $result = wp_trash_post( $id );
     773                        $post = $this->get_post( $id );
     774                        $response = $this->prepare_item_for_response( $post, $request );
    771775                }
    772776
    773777                if ( ! $result ) {
  • src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

     
    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                                                'default'     => false,
     99                                                'description' => __( 'Required to be true, as resource does not support trashing.' ),
     100                                        ),
     101                                ),
    96102                        ),
    97103                        'schema' => array( $this, 'get_public_item_schema' ),
    98104                ));
     
    220226         * @return true|WP_Error True on success, or WP_Error object on failure.
    221227         */
    222228        public function delete_item( $request ) {
     229                $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
     230
     231                // We don't support trashing for this resource type.
     232                if ( ! $force ) {
     233                        return new WP_Error( 'rest_trash_not_supported', __( 'Resource does not support trashing.' ), array( 'status' => 501 ) );
     234                }
     235
     236                $revision = $this->get_post( $request['id'] );
     237                $previous = $this->prepare_item_for_response( $revision, $request );
     238
    223239                $result = wp_delete_post( $request['id'], true );
    224240
    225241                /**
     
    234250                 */
    235251                do_action( 'rest_delete_revision', $result, $request );
    236252
    237                 if ( $result ) {
    238                         return true;
    239                 } else {
     253                if ( ! $result ) {
    240254                        return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
    241255                }
     256
     257                $response = new WP_REST_Response();
     258                $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     259                return $response;
    242260        }
    243261
    244262        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

     
    573573
    574574                $request->set_param( 'context', 'view' );
    575575
    576                 $response = $this->prepare_item_for_response( $term, $request );
     576                $previous = $this->prepare_item_for_response( $term, $request );
    577577
    578578                $retval = wp_delete_term( $term->term_id, $term->taxonomy );
    579579
     
    581581                        return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) );
    582582                }
    583583
     584                $response = new WP_REST_Response();
     585                $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     586
    584587                /**
    585588                 * Fires after a single term is deleted via the REST API.
    586589                 *
  • src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

     
    620620
    621621                $request->set_param( 'context', 'edit' );
    622622
    623                 $response = $this->prepare_item_for_response( $user, $request );
     623                $previous = $this->prepare_item_for_response( $user, $request );
    624624
    625625                /** Include admin user functions to get access to wp_delete_user() */
    626626                require_once ABSPATH . 'wp-admin/includes/user.php';
     
    631631                        return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) );
    632632                }
    633633
     634                $response = new WP_REST_Response();
     635                $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     636
    634637                /**
    635638                 * Fires immediately after a user is deleted via the REST API.
    636639                 *
  • tests/phpunit/tests/rest-api/rest-categories-controller.php

     
    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
    722723        public function test_delete_item_force_false() {
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

     
    16221622                $response = $this->server->dispatch( $request );
    16231623                $this->assertEquals( 200, $response->get_status() );
    16241624                $data = $response->get_data();
    1625                 $this->assertEquals( self::$post_id, $data['post'] );
     1625                $this->assertTrue( $data['deleted'] );
     1626                $this->assertEquals( self::$post_id, $data['previous']['post'] );
    16261627        }
    16271628
    16281629        public function test_delete_item_skip_trash() {
     
    16391640                $response = $this->server->dispatch( $request );
    16401641                $this->assertEquals( 200, $response->get_status() );
    16411642                $data = $response->get_data();
    1642                 $this->assertEquals( self::$post_id, $data['post'] );
     1643                $this->assertTrue( $data['deleted'] );
     1644                $this->assertNotEmpty( $data['previous']['post'] );
    16431645        }
    16441646
    16451647        public function test_delete_item_already_trashed() {
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

     
    18301830                $this->assertEquals( 200, $response->get_status() );
    18311831                $data = $response->get_data();
    18321832                $this->assertEquals( 'Deleted post', $data['title']['raw'] );
     1833                $this->assertEquals( 'trash', $data['status'] );
    18331834        }
    18341835
    18351836        public function test_delete_item_skip_trash() {
     
    18431844                $this->assertNotInstanceOf( 'WP_Error', $response );
    18441845                $this->assertEquals( 200, $response->get_status() );
    18451846                $data = $response->get_data();
    1846                 $this->assertEquals( 'Deleted post', $data['title']['raw'] );
     1847                $this->assertTrue( $data['deleted'] );
     1848                $this->assertNotEmpty( $data['previous'] );
    18471849        }
    18481850
    18491851        public function test_delete_item_already_trashed() {
  • tests/phpunit/tests/rest-api/rest-tags-controller.php

     
    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
    605606        public function test_delete_item_force_false() {
     
    641642
    642643                $this->assertEquals( 200, $response->get_status() );
    643644                $data = $response->get_data();
    644                 $this->assertEquals( 'Deleted Tag', $data['name'] );
     645                $this->assertTrue( $data['deleted'] );
     646                $this->assertEquals( 'Deleted Tag', $data['previous']['name'] );
    645647        }
    646648
    647649        public function grant_delete_term( $caps, $cap ) {
  • tests/phpunit/tests/rest-api/rest-users-controller.php

     
    10631063
    10641064                $this->assertEquals( 200, $response->get_status() );
    10651065                $data = $response->get_data();
    1066                 $this->assertEquals( 'Deleted User', $data['name'] );
     1066                $this->assertTrue( $data['deleted'] );
     1067                $this->assertEquals( 'Deleted User', $data['previous']['name'] );
    10671068        }
    10681069
    10691070        public function test_delete_item_no_trash() {