mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-04 20:54:29 +00:00
REST API: Support ordering response collection by listed slugs.
Adds an "include_slug" orderby value for REST API collections to permit returning a collection filtered by slugs in the same order in which those slugs are specified. Previously, the order of slugs provided with the ?slug query parameter had no effect on the order of the returned records. Props wonderboymusic, ocean90, boonebgorges. Fixes #40826. git-svn-id: https://develop.svn.wordpress.org/trunk@41760 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -287,6 +287,22 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
|
||||
$this->assertEquals( 'Cantaloupe', $data[2]['name'] );
|
||||
}
|
||||
|
||||
public function test_get_items_orderby_slugs() {
|
||||
$this->factory->category->create( array( 'name' => 'Burrito' ) );
|
||||
$this->factory->category->create( array( 'name' => 'Taco' ) );
|
||||
$this->factory->category->create( array( 'name' => 'Chalupa' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
|
||||
$request->set_param( 'orderby', 'include_slugs' );
|
||||
$request->set_param( 'slug', array( 'taco', 'burrito', 'chalupa' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'taco', $data[0]['slug'] );
|
||||
$this->assertEquals( 'burrito', $data[1]['slug'] );
|
||||
$this->assertEquals( 'chalupa', $data[2]['slug'] );
|
||||
}
|
||||
|
||||
protected function post_with_categories() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$category1 = $this->factory->category->create( array(
|
||||
|
||||
@@ -598,6 +598,24 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
|
||||
$this->assertPostsOrderedBy( '{posts}.post_name DESC' );
|
||||
}
|
||||
|
||||
public function test_get_items_with_orderby_slugs() {
|
||||
$slugs = array( 'burrito', 'taco', 'chalupa' );
|
||||
foreach ( $slugs as $slug ) {
|
||||
$this->factory->post->create( array( 'post_title' => $slug, 'post_name' => $slug, 'post_status' => 'publish' ) );
|
||||
}
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
|
||||
$request->set_param( 'orderby', 'include_slugs' );
|
||||
$request->set_param( 'slug', array( 'taco', 'chalupa', 'burrito' ) );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'taco', $data[0]['slug'] );
|
||||
$this->assertEquals( 'chalupa', $data[1]['slug'] );
|
||||
$this->assertEquals( 'burrito', $data[2]['slug'] );
|
||||
}
|
||||
|
||||
public function test_get_items_with_orderby_relevance() {
|
||||
$id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
|
||||
$id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
|
||||
|
||||
@@ -250,6 +250,22 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
|
||||
$this->assertEquals( 'Cantaloupe', $data[2]['name'] );
|
||||
}
|
||||
|
||||
public function test_get_items_orderby_slugs() {
|
||||
$this->factory->tag->create( array( 'name' => 'Burrito' ) );
|
||||
$this->factory->tag->create( array( 'name' => 'Taco' ) );
|
||||
$this->factory->tag->create( array( 'name' => 'Chalupa' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
|
||||
$request->set_param( 'orderby', 'include_slugs' );
|
||||
$request->set_param( 'slug', array( 'taco', 'burrito', 'chalupa' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'taco', $data[0]['slug'] );
|
||||
$this->assertEquals( 'burrito', $data[1]['slug'] );
|
||||
$this->assertEquals( 'chalupa', $data[2]['slug'] );
|
||||
}
|
||||
|
||||
public function test_get_items_post_args() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$tag1 = $this->factory->tag->create( array( 'name' => 'DC' ) );
|
||||
|
||||
@@ -409,6 +409,24 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
|
||||
$this->assertEquals( $low_id, $data[0]['id'] );
|
||||
}
|
||||
|
||||
public function test_get_items_orderby_slugs() {
|
||||
wp_set_current_user( self::$user );
|
||||
|
||||
$this->factory->user->create( array( 'user_nicename' => 'burrito' ) );
|
||||
$this->factory->user->create( array( 'user_nicename' => 'taco' ) );
|
||||
$this->factory->user->create( array( 'user_nicename' => 'chalupa' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
|
||||
$request->set_param( 'orderby', 'include_slugs' );
|
||||
$request->set_param( 'slug', array( 'taco', 'burrito', 'chalupa' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'taco', $data[0]['slug'] );
|
||||
$this->assertEquals( 'burrito', $data[1]['slug'] );
|
||||
$this->assertEquals( 'chalupa', $data[2]['slug'] );
|
||||
}
|
||||
|
||||
public function test_get_items_orderby_email() {
|
||||
wp_set_current_user( self::$user );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user