Ticket #41287: 41287.2.diff
File 41287.2.diff, 6.9 KB (added by , 6 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 b7b2dd79f6..ab53cd8d9b 100644
class WP_REST_Posts_Controller extends WP_REST_Controller { 270 270 foreach ( $taxonomies as $taxonomy ) { 271 271 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; 272 272 $tax_exclude = $base . '_exclude'; 273 $tax_and = $base . '_and'; 273 274 274 275 if ( ! empty( $request[ $base ] ) ) { 275 276 $query_args['tax_query'][] = array( … … class WP_REST_Posts_Controller extends WP_REST_Controller { 289 290 'operator' => 'NOT IN', 290 291 ); 291 292 } 293 294 if ( ! empty( $request[ $tax_and ] ) ) { 295 $query_args['tax_query'][] = array( 296 'taxonomy' => $taxonomy->name, 297 'field' => 'term_id', 298 'terms' => $request[ $tax_and ], 299 'include_children' => false, 300 'operator' => 'AND', 301 ); 302 } 292 303 } 293 304 294 305 $posts_query = new WP_Query(); … … class WP_REST_Posts_Controller extends WP_REST_Controller { 2239 2250 ), 2240 2251 'default' => array(), 2241 2252 ); 2253 2254 $query_params[ $base . '_and' ] = array( 2255 /* translators: %s: taxonomy name */ 2256 'description' => sprintf( __( 'Limit result set to all items that have all the specified terms assigned in the %s taxonomy.' ), $base ), 2257 'type' => 'array', 2258 'items' => array( 2259 'type' => 'integer', 2260 ), 2261 'default' => array(), 2262 ); 2242 2263 } 2243 2264 2244 2265 if ( 'post' === $this->post_type ) { -
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 721b08292a..64e085a497 100644
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 146 146 'author_exclude', 147 147 'before', 148 148 'categories', 149 'categories_and', 149 150 'categories_exclude', 150 151 'context', 151 152 'exclude', … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 160 161 'status', 161 162 'sticky', 162 163 'tags', 164 'tags_and', 163 165 'tags_exclude', 164 166 ), $keys 165 167 ); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 865 867 $this->assertEquals( $id1, $data[0]['id'] ); 866 868 } 867 869 870 public function test_get_items_tags_and_query() { 871 $id1 = self::$post_id; 872 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 873 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 874 $tag1 = wp_insert_term( 'Tag1', 'post_tag' ); 875 $tag2 = wp_insert_term( 'Tag2', 'post_tag' ); 876 877 wp_set_object_terms( $id1, array( $tag1['term_id'] ), 'post_tag' ); 878 wp_set_object_terms( $id2, array( $tag2['term_id'] ), 'post_tag' ); 879 wp_set_object_terms( $id3, array( $tag1['term_id'], $tag2['term_id'] ), 'post_tag' ); 880 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 881 $request->set_param( 'tags_and', array( $tag1['term_id'], $tag2['term_id'] ) ); 882 883 $response = rest_get_server()->dispatch( $request ); 884 $data = $response->get_data(); 885 $this->assertCount( 1, $data ); 886 $this->assertEquals( $id3, $data[0]['id'] ); 887 } 888 868 889 public function test_get_items_tags_exclude_query() { 869 890 $id1 = self::$post_id; 870 891 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 934 955 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 935 956 } 936 957 958 public function test_get_items_tags_and_and_categories_query() { 959 $id1 = self::$post_id; 960 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 961 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 962 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 963 $tag1 = wp_insert_term( 'Tag1', 'post_tag' ); 964 $tag2 = wp_insert_term( 'Tag2', 'post_tag' ); 965 $category = wp_insert_term( 'My Category', 'category' ); 966 967 wp_set_object_terms( $id1, array( $tag1['term_id'], $tag2['term_id'] ), 'post_tag' ); 968 wp_set_object_terms( $id1, array( $category['term_id'] ), 'category' ); 969 wp_set_object_terms( $id2, array( $tag1['term_id'] ), 'post_tag' ); 970 wp_set_object_terms( $id2, array( $category['term_id'] ), 'category' ); 971 wp_set_object_terms( $id3, array( $category['term_id'] ), 'category' ); 972 973 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 974 $request->set_param( 'tags_and', array( $tag1['term_id'], $tag2['term_id'] ) ); 975 $request->set_param( 'categories', array( $category['term_id'] ) ); 976 977 $response = rest_get_server()->dispatch( $request ); 978 $data = $response->get_data(); 979 $this->assertCount( 1, $data ); 980 $this->assertEquals( $id1, $data[0]['id'] ); 981 } 982 937 983 public function test_get_items_sticky() { 938 984 $id1 = self::$post_id; 939 985 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); -
tests/qunit/fixtures/wp-api-generated.js
diff --git tests/qunit/fixtures/wp-api-generated.js tests/qunit/fixtures/wp-api-generated.js index fd20726c9d..49f1d6b930 100644
mockedApiResponse.Schema = { 340 340 "type": "integer" 341 341 } 342 342 }, 343 "categories_and": { 344 "required": false, 345 "default": [], 346 "description": "Limit result set to all items that have all the specified terms assigned in the categories taxonomy.", 347 "type": "array", 348 "items": { 349 "type": "integer" 350 } 351 }, 343 352 "tags": { 344 353 "required": false, 345 354 "default": [], … … mockedApiResponse.Schema = { 358 367 "type": "integer" 359 368 } 360 369 }, 370 "tags_and": { 371 "required": false, 372 "default": [], 373 "description": "Limit result set to all items that have all the specified terms assigned in the tags taxonomy.", 374 "type": "array", 375 "items": { 376 "type": "integer" 377 } 378 }, 361 379 "sticky": { 362 380 "required": false, 363 381 "description": "Limit result set to items that are sticky.",