REST API: Expose revision count and last revision ID on Post response
So that REST API clients can show appropriate UI for a post's revisions, it needs to know how many revisions the post has, and what the latest revision ID is. Props kadamwhite, danielbachhuber, birgire, TimothyBlynJacobs. Fixes #44321. git-svn-id: https://develop.svn.wordpress.org/trunk@43439 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
81d2390d29
commit
585c862faf
@ -1675,9 +1675,23 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||
}
|
||||
|
||||
if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
|
||||
$revisions = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
|
||||
$revisions_count = count( $revisions );
|
||||
|
||||
$links['version-history'] = array(
|
||||
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
|
||||
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
|
||||
'count' => $revisions_count,
|
||||
);
|
||||
|
||||
if ( $revisions_count > 0 ) {
|
||||
$last_revision = array_shift( $revisions );
|
||||
|
||||
$links['predecessor-version'] = array(
|
||||
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
|
||||
'id' => $last_revision,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$post_type_obj = get_post_type_object( $post->post_type );
|
||||
|
@ -1283,6 +1283,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
|
||||
$this->assertEquals( $replies_url, $links['replies'][0]['href'] );
|
||||
|
||||
$this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] );
|
||||
$this->assertEquals( 0, $links['version-history'][0]['attributes']['count'] );
|
||||
$this->assertFalse( isset( $links['predecessor-version'] ) );
|
||||
|
||||
$attachments_url = rest_url( '/wp/v2/media' );
|
||||
$attachments_url = add_query_arg( 'parent', self::$post_id, $attachments_url );
|
||||
@ -1310,6 +1312,28 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
|
||||
$this->assertEquals( $category_url, $cat_link['href'] );
|
||||
}
|
||||
|
||||
public function test_get_item_links_predecessor() {
|
||||
wp_update_post(
|
||||
array(
|
||||
'post_content' => 'This content is marvelous.',
|
||||
'ID' => self::$post_id,
|
||||
)
|
||||
);
|
||||
$revisions = wp_get_post_revisions( self::$post_id );
|
||||
$revision_1 = array_pop( $revisions );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$links = $response->get_links();
|
||||
|
||||
$this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] );
|
||||
$this->assertEquals( 1, $links['version-history'][0]['attributes']['count'] );
|
||||
|
||||
$this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions/' . $revision_1->ID ), $links['predecessor-version'][0]['href'] );
|
||||
$this->assertEquals( $revision_1->ID, $links['predecessor-version'][0]['attributes']['id'] );
|
||||
}
|
||||
|
||||
public function test_get_item_links_no_author() {
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
@ -3603,9 +3603,16 @@ mockedApiResponse.PostsCollection = [
|
||||
],
|
||||
"version-history": [
|
||||
{
|
||||
"count": 1,
|
||||
"href": "http://example.org/index.php?rest_route=/wp/v2/posts/3/revisions"
|
||||
}
|
||||
],
|
||||
"predecessor-version": [
|
||||
{
|
||||
"id": 3123,
|
||||
"href": "http://example.org/index.php?rest_route=/wp/v2/posts/3122/revisions/3123"
|
||||
}
|
||||
],
|
||||
"wp:attachment": [
|
||||
{
|
||||
"href": "http://example.org/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3"
|
||||
@ -3788,9 +3795,16 @@ mockedApiResponse.PagesCollection = [
|
||||
],
|
||||
"version-history": [
|
||||
{
|
||||
"count": 1,
|
||||
"href": "http://example.org/index.php?rest_route=/wp/v2/pages/5/revisions"
|
||||
}
|
||||
],
|
||||
"predecessor-version": [
|
||||
{
|
||||
"id": 3125,
|
||||
"href": "http://example.org/index.php?rest_route=/wp/v2/pages/3124/revisions/3125"
|
||||
}
|
||||
],
|
||||
"wp:attachment": [
|
||||
{
|
||||
"href": "http://example.org/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5"
|
||||
@ -3937,22 +3951,27 @@ mockedApiResponse.MediaCollection = [
|
||||
"_links": {
|
||||
"self": [
|
||||
{
|
||||
"attributes": [],
|
||||
"href": "http://example.org/index.php?rest_route=/wp/v2/media/7"
|
||||
}
|
||||
],
|
||||
"collection": [
|
||||
{
|
||||
"attributes": [],
|
||||
"href": "http://example.org/index.php?rest_route=/wp/v2/media"
|
||||
}
|
||||
],
|
||||
"about": [
|
||||
{
|
||||
"attributes": [],
|
||||
"href": "http://example.org/index.php?rest_route=/wp/v2/types/attachment"
|
||||
}
|
||||
],
|
||||
"replies": [
|
||||
{
|
||||
"embeddable": true,
|
||||
"attributes": {
|
||||
"embeddable": true
|
||||
},
|
||||
"href": "http://example.org/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7"
|
||||
}
|
||||
]
|
||||
|
Loading…
x
Reference in New Issue
Block a user