Changeset 46646
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r46609 r46646 270 270 271 271 $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); 272 273 if ( ! empty( $request['tax_relation'] ) ) { 274 $query_args['tax_query'] = array( 'relation' => $request['tax_relation'] ); 275 } 272 276 273 277 foreach ( $taxonomies as $taxonomy ) { … … 2532 2536 $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); 2533 2537 2538 if ( ! empty( $taxonomies ) ) { 2539 $query_params['tax_relation'] = array( 2540 'description' => __( 'Limit result set based on relationship between multiple taxonomies.' ), 2541 'type' => 'string', 2542 'enum' => array( 'AND', 'OR' ), 2543 ); 2544 } 2545 2534 2546 foreach ( $taxonomies as $taxonomy ) { 2535 2547 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; -
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r46586 r46646 176 176 'tags', 177 177 'tags_exclude', 178 'tax_relation', 178 179 ), 179 180 $keys … … 960 961 } 961 962 963 /** 964 * @ticket 44326 965 */ 966 public function test_get_items_tags_or_categories_query() { 967 $id1 = self::$post_id; 968 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 969 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 970 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 971 $tag = wp_insert_term( 'My Tag', 'post_tag' ); 972 $category = wp_insert_term( 'My Category', 'category' ); 973 974 wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); 975 wp_set_object_terms( $id2, array( $category['term_id'] ), 'category' ); 976 977 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 978 $request->set_param( 'tax_relation', 'OR' ); 979 $request->set_param( 'tags', array( $tag['term_id'] ) ); 980 $request->set_param( 'categories', array( $category['term_id'] ) ); 981 982 $response = rest_get_server()->dispatch( $request ); 983 $data = $response->get_data(); 984 $this->assertCount( 2, $data ); 985 $this->assertEquals( $id2, $data[0]['id'] ); 986 $this->assertEquals( $id1, $data[1]['id'] ); 987 } 988 962 989 public function test_get_items_tags_and_categories_exclude_query() { 963 990 $id1 = self::$post_id; … … 984 1011 $response = rest_get_server()->dispatch( $request ); 985 1012 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 1013 } 1014 1015 /** 1016 * @ticket 44326 1017 */ 1018 public function test_get_items_tags_or_categories_exclude_query() { 1019 $id1 = self::$post_id; 1020 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1021 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1022 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 1023 $tag = wp_insert_term( 'My Tag', 'post_tag' ); 1024 $category = wp_insert_term( 'My Category', 'category' ); 1025 1026 wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' ); 1027 wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' ); 1028 wp_set_object_terms( $id2, array( $category['term_id'] ), 'category' ); 1029 wp_set_object_terms( $id3, array( $category['term_id'] ), 'category' ); 1030 1031 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1032 $request->set_param( 'tags', array( $tag['term_id'] ) ); 1033 $request->set_param( 'categories_exclude', array( $category['term_id'] ) ); 1034 $request->set_param( 'tax_relation', 'OR' ); 1035 1036 $response = rest_get_server()->dispatch( $request ); 1037 $data = $response->get_data(); 1038 $this->assertCount( 3, $data ); 1039 $this->assertEquals( $id2, $data[0]['id'] ); 1040 $this->assertEquals( $id4, $data[1]['id'] ); 1041 $this->assertEquals( $id1, $data[2]['id'] ); 1042 } 1043 1044 /** 1045 * @ticket 44326 1046 */ 1047 public function test_get_items_relation_with_no_tax_query() { 1048 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1049 $request->set_param( 'tax_relation', 'OR' ); 1050 $request->set_param( 'include', self::$post_id ); 1051 1052 $response = rest_get_server()->dispatch( $request ); 1053 $this->assertEquals( 200, $response->get_status() ); 1054 $this->assertCount( 1, $response->get_data() ); 1055 $this->assertEquals( self::$post_id, $response->get_data()[0]['id'] ); 986 1056 } 987 1057 -
trunk/tests/qunit/fixtures/wp-api-generated.js
r46586 r46646 322 322 "type": "string" 323 323 } 324 }, 325 "tax_relation": { 326 "required": false, 327 "enum": [ 328 "AND", 329 "OR" 330 ], 331 "description": "Limit result set based on relationship between multiple taxonomies.", 332 "type": "string" 324 333 }, 325 334 "categories": {
Note: See TracChangeset
for help on using the changeset viewer.