diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
index 884e5f60f3..a9565d91d3 100644
|
|
|
class WP_REST_Terms_Controller extends WP_REST_Controller { |
| 669 | 669 | } |
| 670 | 670 | |
| 671 | 671 | if ( isset( $request['parent'] ) && ! empty( $schema['properties']['parent'] ) ) { |
| 672 | | $parent_term_id = 0; |
| 673 | | $parent_term = get_term( (int) $request['parent'], $this->taxonomy ); |
| | 672 | $parent_term_id = 0; |
| | 673 | $requested_parent = (int) $request['parent']; |
| 674 | 674 | |
| 675 | | if ( $parent_term ) { |
| 676 | | $parent_term_id = $parent_term->term_id; |
| | 675 | if ( 0 === $requested_parent ) { |
| | 676 | $parent_term_id = $requested_parent; |
| | 677 | } |
| | 678 | |
| | 679 | if ( $requested_parent ) { |
| | 680 | $parent_term = get_term( $requested_parent, $this->taxonomy ); |
| | 681 | |
| | 682 | if ( $parent_term instanceof WP_Term ) { |
| | 683 | $parent_term_id = $parent_term->term_id; |
| | 684 | } |
| 677 | 685 | } |
| 678 | 686 | |
| 679 | 687 | $prepared_term->parent = $parent_term_id; |
diff --git tests/phpunit/tests/rest-api/rest-categories-controller.php tests/phpunit/tests/rest-api/rest-categories-controller.php
index 79add82cde..6d9a29dfc0 100644
|
|
|
class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas |
| 854 | 854 | $this->assertErrorResponse( 'rest_term_invalid', $response, 400 ); |
| 855 | 855 | } |
| 856 | 856 | |
| | 857 | public function test_create_item_with_no_parent() { |
| | 858 | wp_set_current_user( self::$administrator ); |
| | 859 | $parent = 0; |
| | 860 | $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); |
| | 861 | $request->set_param( 'name', 'My Awesome Term' ); |
| | 862 | $request->set_param( 'parent', $parent ); |
| | 863 | $response = rest_get_server()->dispatch( $request ); |
| | 864 | $this->assertEquals( 201, $response->get_status() ); |
| | 865 | $data = $response->get_data(); |
| | 866 | $this->assertEquals( $parent, $data['parent'] ); |
| | 867 | } |
| | 868 | |
| 857 | 869 | public function test_update_item() { |
| 858 | 870 | wp_set_current_user( self::$administrator ); |
| 859 | 871 | $orig_args = array( |
| … |
… |
class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas |
| 924 | 936 | $this->assertEquals( $parent->term_id, $data['parent'] ); |
| 925 | 937 | } |
| 926 | 938 | |
| | 939 | public function test_update_item_remove_parent() { |
| | 940 | wp_set_current_user( self::$administrator ); |
| | 941 | |
| | 942 | $old_parent_term = get_term_by( 'id', $this->factory->category->create(), 'category' ); |
| | 943 | $new_parent_id = 0; |
| | 944 | |
| | 945 | $term = get_term_by( |
| | 946 | 'id', |
| | 947 | $this->factory->category->create( |
| | 948 | [ |
| | 949 | 'parent' => $old_parent_term->term_id, |
| | 950 | ] |
| | 951 | ), |
| | 952 | 'category' |
| | 953 | ); |
| | 954 | |
| | 955 | $this->assertEquals( $old_parent_term->term_id, $term->parent ); |
| | 956 | |
| | 957 | $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); |
| | 958 | $request->set_param( 'parent', $new_parent_id ); |
| | 959 | $response = rest_get_server()->dispatch( $request ); |
| | 960 | $this->assertEquals( 200, $response->get_status() ); |
| | 961 | |
| | 962 | $data = $response->get_data(); |
| | 963 | $this->assertEquals( $new_parent_id, $data['parent'] ); |
| | 964 | } |
| | 965 | |
| 927 | 966 | public function test_update_item_invalid_parent() { |
| 928 | 967 | wp_set_current_user( self::$administrator ); |
| 929 | 968 | $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); |