From 585c862faf18afdccb6dc964b4740062d55d6809 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 13 Jul 2018 04:06:23 +0000 Subject: [PATCH] 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 --- .../class-wp-rest-posts-controller.php | 16 ++++++++++++- .../tests/rest-api/rest-posts-controller.php | 24 +++++++++++++++++++ tests/qunit/fixtures/wp-api-generated.js | 21 +++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 7cd6497c0b..43a00d660a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -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 ); diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index c01ca6cd51..eab7bba8c3 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -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 ); diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 4e7c172421..beebbc5270 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -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" } ]