From 99de5d69d789bf8ed4a28fbb8f605607babeef6c Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 5 Apr 2017 20:24:27 +0000 Subject: [PATCH] REST API: Allow fetching multiple terms at once via the `slug` parameter. This matches a similar change previously made for posts (#38579) and an upcoming change for users (#40213). Props wonderboymusic, MatheusGimenez, curdin. Fixes #40027. git-svn-id: https://develop.svn.wordpress.org/trunk@40376 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-terms-controller.php | 5 ++- .../tests/rest-api/rest-tags-controller.php | 34 +++++++++++++++++++ tests/qunit/fixtures/wp-api-generated.js | 10 ++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index 9d33eaa475..1a316308fe 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -978,7 +978,10 @@ class WP_REST_Terms_Controller extends WP_REST_Controller { $query_params['slug'] = array( 'description' => __( 'Limit result set to terms with a specific slug.' ), - 'type' => 'string', + 'type' => 'array', + 'items' => array( + 'type' => 'string' + ), ); /** diff --git a/tests/phpunit/tests/rest-api/rest-tags-controller.php b/tests/phpunit/tests/rest-api/rest-tags-controller.php index cb81b17159..fccc4d702d 100644 --- a/tests/phpunit/tests/rest-api/rest-tags-controller.php +++ b/tests/phpunit/tests/rest-api/rest-tags-controller.php @@ -378,6 +378,40 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { $this->assertEquals( 'Apple', $data[0]['name'] ); } + public function test_get_items_slug_array_arg() { + $id1 = $this->factory->tag->create( array( 'name' => 'Taco' ) ); + $id2 = $this->factory->tag->create( array( 'name' => 'Enchilada' ) ); + $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) ); + $this->factory->tag->create( array( 'name' => 'Pizza' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); + $request->set_param( 'slug', array( + 'taco', + 'burrito', + 'enchilada', + ) ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + $data = $response->get_data(); + $names = wp_list_pluck( $data, 'name' ); + sort( $names ); + $this->assertEquals( array( 'Burrito', 'Enchilada', 'Taco' ), $names ); + } + + public function test_get_items_slug_csv_arg() { + $id1 = $this->factory->tag->create( array( 'name' => 'Taco' ) ); + $id2 = $this->factory->tag->create( array( 'name' => 'Enchilada' ) ); + $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) ); + $this->factory->tag->create( array( 'name' => 'Pizza' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); + $request->set_param( 'slug', 'taco,burrito, enchilada'); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + $data = $response->get_data(); + $names = wp_list_pluck( $data, 'name' ); + sort( $names ); + $this->assertEquals( array( 'Burrito', 'Enchilada', 'Taco' ), $names ); + } + public function test_get_terms_private_taxonomy() { register_taxonomy( 'robin', 'post', array( 'public' => false ) ); $term1 = $this->factory->term->create( array( 'name' => 'Cape', 'taxonomy' => 'robin' ) ); diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index b4c6109baf..2e47935796 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -1990,7 +1990,10 @@ mockedApiResponse.Schema = { "slug": { "required": false, "description": "Limit result set to terms with a specific slug.", - "type": "string" + "type": "array", + "items": { + "type": "string" + } } } }, @@ -2225,7 +2228,10 @@ mockedApiResponse.Schema = { "slug": { "required": false, "description": "Limit result set to terms with a specific slug.", - "type": "string" + "type": "array", + "items": { + "type": "string" + } } } },