Make WordPress Core

Changeset 43439


Ignore:
Timestamp:
07/13/2018 04:06:23 AM (8 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r43437 r43439  
    16761676
    16771677                if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
     1678                        $revisions       = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
     1679                        $revisions_count = count( $revisions );
     1680
    16781681                        $links['version-history'] = array(
    1679                                 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
    1680                         );
     1682                                'href'  => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
     1683                                'count' => $revisions_count,
     1684                        );
     1685
     1686                        if ( $revisions_count > 0 ) {
     1687                                $last_revision = array_shift( $revisions );
     1688
     1689                                $links['predecessor-version'] = array(
     1690                                        'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
     1691                                        'id'   => $last_revision,
     1692                                );
     1693                        }
     1694
    16811695                }
    16821696
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r43437 r43439  
    12841284
    12851285                $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] );
     1286                $this->assertEquals( 0, $links['version-history'][0]['attributes']['count'] );
     1287                $this->assertFalse( isset( $links['predecessor-version'] ) );
    12861288
    12871289                $attachments_url = rest_url( '/wp/v2/media' );
     
    13091311                $category_url = add_query_arg( 'post', self::$post_id, rest_url( '/wp/v2/categories' ) );
    13101312                $this->assertEquals( $category_url, $cat_link['href'] );
     1313        }
     1314
     1315        public function test_get_item_links_predecessor() {
     1316                wp_update_post(
     1317                        array(
     1318                                'post_content' => 'This content is marvelous.',
     1319                                'ID'           => self::$post_id,
     1320                        )
     1321                );
     1322                $revisions  = wp_get_post_revisions( self::$post_id );
     1323                $revision_1 = array_pop( $revisions );
     1324
     1325                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1326                $response = rest_get_server()->dispatch( $request );
     1327
     1328                $links = $response->get_links();
     1329
     1330                $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] );
     1331                $this->assertEquals( 1, $links['version-history'][0]['attributes']['count'] );
     1332
     1333                $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions/' . $revision_1->ID ), $links['predecessor-version'][0]['href'] );
     1334                $this->assertEquals( $revision_1->ID, $links['predecessor-version'][0]['attributes']['id'] );
    13111335        }
    13121336
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r43359 r43439  
    36043604            "version-history": [
    36053605                {
     3606                    "count": 1,
    36063607                    "href": "http://example.org/index.php?rest_route=/wp/v2/posts/3/revisions"
     3608                }
     3609            ],
     3610            "predecessor-version": [
     3611                {
     3612                    "id": 3123,
     3613                    "href": "http://example.org/index.php?rest_route=/wp/v2/posts/3122/revisions/3123"
    36073614                }
    36083615            ],
     
    37893796            "version-history": [
    37903797                {
     3798                    "count": 1,
    37913799                    "href": "http://example.org/index.php?rest_route=/wp/v2/pages/5/revisions"
     3800                }
     3801            ],
     3802            "predecessor-version": [
     3803                {
     3804                    "id": 3125,
     3805                    "href": "http://example.org/index.php?rest_route=/wp/v2/pages/3124/revisions/3125"
    37923806                }
    37933807            ],
     
    39383952            "self": [
    39393953                {
     3954                    "attributes": [],
    39403955                    "href": "http://example.org/index.php?rest_route=/wp/v2/media/7"
    39413956                }
     
    39433958            "collection": [
    39443959                {
     3960                    "attributes": [],
    39453961                    "href": "http://example.org/index.php?rest_route=/wp/v2/media"
    39463962                }
     
    39483964            "about": [
    39493965                {
     3966                    "attributes": [],
    39503967                    "href": "http://example.org/index.php?rest_route=/wp/v2/types/attachment"
    39513968                }
     
    39533970            "replies": [
    39543971                {
    3955                     "embeddable": true,
     3972                    "attributes": {
     3973                        "embeddable": true
     3974                    },
    39563975                    "href": "http://example.org/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7"
    39573976                }
Note: See TracChangeset for help on using the changeset viewer.