mirror of
git://develop.git.wordpress.org/
synced 2025-01-17 12:58:25 +01:00
REST API: Add author
, modified
, and parent
sort order options for posts.
These (and a few others that can be revisited later if needed) were present in beta versions of the WP REST API but were removed during the merge to WP 4.7. Props ChopinBach, jnylen0. Fixes #38693. git-svn-id: https://develop.svn.wordpress.org/trunk@40605 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
93ddc497f0
commit
0fab6c9fdc
@ -2130,12 +2130,15 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||
'type' => 'string',
|
||||
'default' => 'date',
|
||||
'enum' => array(
|
||||
'author',
|
||||
'date',
|
||||
'relevance',
|
||||
'id',
|
||||
'include',
|
||||
'title',
|
||||
'modified',
|
||||
'parent',
|
||||
'relevance',
|
||||
'slug',
|
||||
'title',
|
||||
),
|
||||
);
|
||||
|
||||
|
@ -868,4 +868,28 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
|
||||
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* There's no way to change post_modified through WP functions.
|
||||
*/
|
||||
protected function update_post_modified( $post_id, $date ) {
|
||||
global $wpdb;
|
||||
return $wpdb->update(
|
||||
$wpdb->posts,
|
||||
array(
|
||||
'post_modified' => $date,
|
||||
'post_modified_gmt' => $date,
|
||||
),
|
||||
array(
|
||||
'ID' => $post_id,
|
||||
),
|
||||
array(
|
||||
'%s',
|
||||
'%s',
|
||||
),
|
||||
array(
|
||||
'%d',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -998,30 +998,4 @@ class Tests_Query_DateQuery extends WP_UnitTestCase {
|
||||
$expected = array( $p1, $p4, $p5, );
|
||||
$this->assertEqualSets( $expected, $q->posts );
|
||||
}
|
||||
|
||||
/** Helpers **********************************************************/
|
||||
|
||||
/**
|
||||
* There's no way to change post_modified through the API.
|
||||
*/
|
||||
protected function update_post_modified( $post_id, $date ) {
|
||||
global $wpdb;
|
||||
return $wpdb->update(
|
||||
$wpdb->posts,
|
||||
array(
|
||||
'post_modified' => $date,
|
||||
'post_modified_gmt' => $date,
|
||||
),
|
||||
array(
|
||||
'ID' => $post_id,
|
||||
),
|
||||
array(
|
||||
'%s',
|
||||
'%s',
|
||||
),
|
||||
array(
|
||||
'%d',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -267,6 +267,72 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
}
|
||||
|
||||
public function test_get_items_orderby_author_query() {
|
||||
$id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) );
|
||||
$id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$editor_id ) );
|
||||
$id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_author' => self::$author_id ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
|
||||
$request->set_param( 'include', array( $id1, $id2, $id3 ) );
|
||||
$request->set_param( 'orderby', 'author' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( self::$author_id, $data[0]['author'] );
|
||||
$this->assertEquals( self::$editor_id, $data[1]['author'] );
|
||||
$this->assertEquals( self::$editor_id, $data[2]['author'] );
|
||||
|
||||
$this->assertPostsOrderedBy( '{posts}.post_author DESC' );
|
||||
}
|
||||
|
||||
public function test_get_items_orderby_modified_query() {
|
||||
$id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
|
||||
$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
|
||||
$id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
|
||||
|
||||
$this->update_post_modified( $id1, '2016-04-20 4:26:20' );
|
||||
$this->update_post_modified( $id2, '2016-02-01 20:24:02' );
|
||||
$this->update_post_modified( $id3, '2016-02-21 12:24:02' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
|
||||
$request->set_param( 'include', array( $id1, $id2, $id3 ) );
|
||||
$request->set_param( 'orderby', 'modified' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $id1, $data[0]['id'] );
|
||||
$this->assertEquals( $id3, $data[1]['id'] );
|
||||
$this->assertEquals( $id2, $data[2]['id'] );
|
||||
|
||||
$this->assertPostsOrderedBy( '{posts}.post_modified DESC' );
|
||||
}
|
||||
|
||||
public function test_get_items_orderby_parent_query() {
|
||||
$id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
|
||||
$id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
|
||||
$id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page', 'post_parent' => $id1 ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
|
||||
$request->set_param( 'include', array( $id1, $id2, $id3 ) );
|
||||
$request->set_param( 'orderby', 'parent' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $id3, $data[0]['id'] );
|
||||
// Check ordering. Default ORDER is DESC.
|
||||
$this->assertEquals( $id1, $data[0]['parent'] );
|
||||
$this->assertEquals( 0, $data[1]['parent'] );
|
||||
$this->assertEquals( 0, $data[2]['parent'] );
|
||||
|
||||
$this->assertPostsOrderedBy( '{posts}.post_parent DESC' );
|
||||
}
|
||||
|
||||
public function test_get_items_exclude_query() {
|
||||
$id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
|
||||
$id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
|
||||
|
@ -226,12 +226,15 @@ mockedApiResponse.Schema = {
|
||||
"required": false,
|
||||
"default": "date",
|
||||
"enum": [
|
||||
"author",
|
||||
"date",
|
||||
"relevance",
|
||||
"id",
|
||||
"include",
|
||||
"title",
|
||||
"slug"
|
||||
"modified",
|
||||
"parent",
|
||||
"relevance",
|
||||
"slug",
|
||||
"title"
|
||||
],
|
||||
"description": "Sort collection by object attribute.",
|
||||
"type": "string"
|
||||
@ -844,12 +847,15 @@ mockedApiResponse.Schema = {
|
||||
"required": false,
|
||||
"default": "date",
|
||||
"enum": [
|
||||
"author",
|
||||
"date",
|
||||
"relevance",
|
||||
"id",
|
||||
"include",
|
||||
"title",
|
||||
"modified",
|
||||
"parent",
|
||||
"relevance",
|
||||
"slug",
|
||||
"title",
|
||||
"menu_order"
|
||||
],
|
||||
"description": "Sort collection by object attribute.",
|
||||
@ -1379,12 +1385,15 @@ mockedApiResponse.Schema = {
|
||||
"required": false,
|
||||
"default": "date",
|
||||
"enum": [
|
||||
"author",
|
||||
"date",
|
||||
"relevance",
|
||||
"id",
|
||||
"include",
|
||||
"title",
|
||||
"slug"
|
||||
"modified",
|
||||
"parent",
|
||||
"relevance",
|
||||
"slug",
|
||||
"title"
|
||||
],
|
||||
"description": "Sort collection by object attribute.",
|
||||
"type": "string"
|
||||
|
Loading…
x
Reference in New Issue
Block a user