Make WordPress Core

Changeset 39289


Ignore:
Timestamp:
11/18/2016 06:11:49 PM (10 years ago)
Author:
rachelbaker
Message:

REST API: Allow parent property to be explicitly set to 0 when creating or updating a Post.

Props lucasstark, danielbachhuber.
Fixes #38852.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39278 r39289  
    992992
    993993                // Parent.
    994                 if ( ! empty( $schema['properties']['parent'] ) && ! empty( $request['parent'] ) ) {
    995                         $parent = get_post( (int) $request['parent'] );
    996 
    997                         if ( empty( $parent ) ) {
    998                                 return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post parent ID.' ), array( 'status' => 400 ) );
    999                         }
    1000 
    1001                         $prepared_post->post_parent = (int) $parent->ID;
     994                if ( ! empty( $schema['properties']['parent'] ) && isset( $request['parent'] ) ) {
     995                        if ( 0 === (int) $request['parent'] ) {
     996                                $prepared_post->post_parent = 0;
     997                        } else {
     998                                $parent = get_post( (int) $request['parent'] );
     999                                if ( empty( $parent ) ) {
     1000                                        return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post parent ID.' ), array( 'status' => 400 ) );
     1001                                }
     1002                                $prepared_post->post_parent = (int) $parent->ID;
     1003                        }
    10021004                }
    10031005
  • trunk/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r39105 r39289  
    370370                $new_data = $response->get_data();
    371371                $this->assertEquals( 0, $new_data['menu_order'] );
     372        }
     373
     374        public function test_update_page_parent_non_zero() {
     375                $page_id1 = $this->factory->post->create( array(
     376                        'post_type' => 'page',
     377                ) );
     378                $page_id2 = $this->factory->post->create( array(
     379                        'post_type' => 'page',
     380                ) );
     381                wp_set_current_user( self::$editor_id );
     382                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $page_id2 ) );
     383                $request->set_body_params( array(
     384                        'parent' => $page_id1,
     385                ) );
     386                $response = $this->server->dispatch( $request );
     387                $new_data = $response->get_data();
     388                $this->assertEquals( $page_id1, $new_data['parent'] );
     389        }
     390
     391        public function test_update_page_parent_zero() {
     392                $page_id1 = $this->factory->post->create( array(
     393                        'post_type' => 'page',
     394                ) );
     395                $page_id2 = $this->factory->post->create( array(
     396                        'post_type'    => 'page',
     397                        'post_parent'  => $page_id1,
     398                ) );
     399                wp_set_current_user( self::$editor_id );
     400                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/pages/%d', $page_id2 ) );
     401                $request->set_body_params( array(
     402                        'parent' => 0,
     403                ) );
     404                $response = $this->server->dispatch( $request );
     405                $new_data = $response->get_data();
     406                $this->assertEquals( 0, $new_data['parent'] );
    372407        }
    373408
Note: See TracChangeset for help on using the changeset viewer.