WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/13/2018 04:06:23 AM (17 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.