Ticket #39494: 39494.2.diff
File 39494.2.diff, 4.8 KB (added by , 7 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 0661152..ebd9aa2 100644
class WP_REST_Posts_Controller extends WP_REST_Controller { 272 272 $tax_exclude = $base . '_exclude'; 273 273 274 274 if ( ! empty( $request[ $base ] ) ) { 275 if ( isset( $request[ $base ]['ids'] ) ) { 276 $terms = $request[ $base ]['ids']; 277 } else { 278 $terms = $request[ $base ]; 279 } 280 281 if ( isset( $request[ $base ]['include_children'] ) ) { 282 $include_children = $request[ $base ]['include_children']; 283 } else { 284 $include_children = false; 285 } 286 275 287 $query_args['tax_query'][] = array( 276 288 'taxonomy' => $taxonomy->name, 277 289 'field' => 'term_id', 278 'terms' => $ request[ $base ],279 'include_children' => false,290 'terms' => $terms, 291 'include_children' => $include_children, 280 292 ); 281 293 } 282 294 … … class WP_REST_Posts_Controller extends WP_REST_Controller { 2223 2235 $query_params[ $base ] = array( 2224 2236 /* translators: %s: taxonomy name */ 2225 2237 'description' => sprintf( __( 'Limit result set to all items that have the specified term assigned in the %s taxonomy.' ), $base ), 2226 'type' => 'array', 2227 'items' => array( 2228 'type' => 'integer', 2238 'type' => 'object', 2239 'properties' => array( 2240 'ids' => array( 2241 'description' => __( 'The term ids to limit the post to' ), 2242 'type' => 'array', 2243 'items' => array( 2244 'type' => 'integer', 2245 ), 2246 'default' => array(), 2247 ), 2248 'include_children' => array( 2249 'description' => __( 'Whether or not to include the term children' ), 2250 'type' => 'boolean', 2251 'default' => false, 2252 ), 2229 2253 ), 2230 'default' => array(),2231 2254 ); 2232 2255 2233 2256 $query_params[ $base . '_exclude' ] = array( -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php index bc820a8..ce509b6 100644
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 934 934 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 935 935 } 936 936 937 /** 938 * Testing updated taxonomy schema and including category children. 939 * 940 * @ticket 39494 941 */ 942 public function test_get_items_categories_with_updated_taxonomy_schema_and_include_children_true() { 943 $id1 = self::$post_id; 944 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 945 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 946 947 $cat1 = wp_insert_term( 'My Parent Category', 'category' ); 948 $cat2 = wp_insert_term( 'My Child Category', 'category', array( 'parent' => $cat1['term_id'] ) ); 949 950 wp_set_object_terms( $id1, array( $cat1['term_id'] ), 'category' ); 951 wp_set_object_terms( $id2, array( $cat2['term_id'] ), 'category' ); 952 953 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 954 $request->set_param( 'categories', array( 955 'ids' => array( $cat1['term_id'] ), 956 'include_children' => true, 957 ) ); 958 $response = rest_get_server()->dispatch( $request ); 959 $data = $response->get_data(); 960 961 $this->assertCount( 2, $data ); 962 $this->assertEquals( $id1, $data[0]['id'] ); 963 $this->assertEquals( $id2, $data[1]['id'] ); 964 } 965 966 /** 967 * Testing updated taxonomy schema without including category children. 968 * 969 * @ticket 39494 970 */ 971 public function test_get_items_categories_with_updated_taxonomy_schema_and_include_children_false() { 972 $id1 = self::$post_id; 973 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 974 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 975 976 $cat1 = wp_insert_term( 'My Parent Category', 'category' ); 977 $cat2 = wp_insert_term( 'My Child Category', 'category', array( 'parent' => $cat1['term_id'] ) ); 978 979 wp_set_object_terms( $id1, array( $cat1['term_id'] ), 'category' ); 980 wp_set_object_terms( $id2, array( $cat2['term_id'] ), 'category' ); 981 982 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 983 $request->set_param( 'categories', array( 984 'ids' => array( $cat1['term_id'] ), 985 'include_children' => false, 986 ) ); 987 $response = rest_get_server()->dispatch( $request ); 988 $data = $response->get_data(); 989 990 $this->assertCount( 1, $data ); 991 $this->assertEquals( $id1, $data[0]['id'] ); 992 } 993 937 994 public function test_get_items_sticky() { 938 995 $id1 = self::$post_id; 939 996 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );