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 ff399cc60e..527a8bc199 100644
|
|
|
class WP_REST_Terms_Controller extends WP_REST_Controller { |
| 685 | 685 | } |
| 686 | 686 | |
| 687 | 687 | if ( isset( $request['parent'] ) && ! empty( $schema['properties']['parent'] ) ) { |
| 688 | | $parent_term_id = 0; |
| 689 | | $parent_term = get_term( (int) $request['parent'], $this->taxonomy ); |
| | 688 | $parent_term_id = 0; |
| | 689 | $requested_parent = (int) $request['parent']; |
| 690 | 690 | |
| 691 | | if ( $parent_term ) { |
| 692 | | $parent_term_id = $parent_term->term_id; |
| | 691 | if ( $requested_parent ) { |
| | 692 | $parent_term = get_term( $requested_parent, $this->taxonomy ); |
| | 693 | |
| | 694 | if ( $parent_term instanceof WP_Term ) { |
| | 695 | $parent_term_id = $parent_term->term_id; |
| | 696 | } |
| 693 | 697 | } |
| 694 | 698 | |
| 695 | 699 | $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 3c9543833b..8d44e282db 100644
|
|
|
class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas |
| 859 | 859 | $this->assertErrorResponse( 'rest_term_invalid', $response, 400 ); |
| 860 | 860 | } |
| 861 | 861 | |
| | 862 | public function test_create_item_with_no_parent() { |
| | 863 | wp_set_current_user( self::$administrator ); |
| | 864 | $parent = 0; |
| | 865 | $request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); |
| | 866 | $request->set_param( 'name', 'My Awesome Term' ); |
| | 867 | $request->set_param( 'parent', $parent ); |
| | 868 | $response = rest_get_server()->dispatch( $request ); |
| | 869 | $this->assertEquals( 201, $response->get_status() ); |
| | 870 | $data = $response->get_data(); |
| | 871 | $this->assertEquals( $parent, $data['parent'] ); |
| | 872 | } |
| | 873 | |
| 862 | 874 | public function test_update_item() { |
| 863 | 875 | wp_set_current_user( self::$administrator ); |
| 864 | 876 | $orig_args = array( |
| … |
… |
class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas |
| 929 | 941 | $this->assertEquals( $parent->term_id, $data['parent'] ); |
| 930 | 942 | } |
| 931 | 943 | |
| | 944 | public function test_update_item_remove_parent() { |
| | 945 | wp_set_current_user( self::$administrator ); |
| | 946 | |
| | 947 | $old_parent_term = get_term_by( 'id', $this->factory->category->create(), 'category' ); |
| | 948 | $new_parent_id = 0; |
| | 949 | |
| | 950 | $term = get_term_by( |
| | 951 | 'id', |
| | 952 | $this->factory->category->create( |
| | 953 | [ |
| | 954 | 'parent' => $old_parent_term->term_id, |
| | 955 | ] |
| | 956 | ), |
| | 957 | 'category' |
| | 958 | ); |
| | 959 | |
| | 960 | $this->assertEquals( $old_parent_term->term_id, $term->parent ); |
| | 961 | |
| | 962 | $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id ); |
| | 963 | $request->set_param( 'parent', $new_parent_id ); |
| | 964 | $response = rest_get_server()->dispatch( $request ); |
| | 965 | $this->assertEquals( 200, $response->get_status() ); |
| | 966 | |
| | 967 | $data = $response->get_data(); |
| | 968 | $this->assertEquals( $new_parent_id, $data['parent'] ); |
| | 969 | } |
| | 970 | |
| 932 | 971 | public function test_update_item_invalid_parent() { |
| 933 | 972 | wp_set_current_user( self::$administrator ); |
| 934 | 973 | $term = get_term_by( 'id', $this->factory->category->create(), 'category' ); |