Ticket #38852: 38852.1.diff
File 38852.1.diff, 2.9 KB (added by , 8 years ago) |
---|
-
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 f4c4cc7..3265691 100644
a b class WP_REST_Posts_Controller extends WP_REST_Controller { 991 991 } 992 992 993 993 // 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 ) ); 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; 999 1003 } 1000 1001 $prepared_post->post_parent = (int) $parent->ID;1002 1004 } 1003 1005 1004 1006 // Menu order. -
tests/phpunit/tests/rest-api/rest-pages-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index a571803..b74d01c 100644
a b class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 371 371 $this->assertEquals( 0, $new_data['menu_order'] ); 372 372 } 373 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'] ); 407 } 408 374 409 public function test_get_page_with_password() { 375 410 $page_id = $this->factory->post->create( array( 376 411 'post_type' => 'page',