Make WordPress Core


Ignore:
Timestamp:
07/13/2018 06:13:27 AM (6 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.

Merge of [43439] and [43441] to the 4.9 branch.

Props kadamwhite, danielbachhuber, birgire, TimothyBlynJacobs, pento.
Fixes #44321.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r43438 r43442  
    10911091
    10921092        $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] );
     1093        $this->assertEquals( 0, $links['version-history'][0]['attributes']['count'] );
     1094        $this->assertFalse( isset( $links['predecessor-version'] ) );
    10931095
    10941096        $attachments_url = rest_url( '/wp/v2/media' );
     
    11161118        $category_url = add_query_arg( 'post', self::$post_id, rest_url( '/wp/v2/categories' ) );
    11171119        $this->assertEquals( $category_url, $cat_link['href'] );
     1120    }
     1121
     1122    public function test_get_item_links_predecessor() {
     1123        wp_update_post(
     1124            array(
     1125                'post_content' => 'This content is marvelous.',
     1126                'ID'           => self::$post_id,
     1127            )
     1128        );
     1129        $revisions  = wp_get_post_revisions( self::$post_id );
     1130        $revision_1 = array_pop( $revisions );
     1131
     1132        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1133        $response = rest_get_server()->dispatch( $request );
     1134
     1135        $links = $response->get_links();
     1136
     1137        $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions' ), $links['version-history'][0]['href'] );
     1138        $this->assertEquals( 1, $links['version-history'][0]['attributes']['count'] );
     1139
     1140        $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id . '/revisions/' . $revision_1->ID ), $links['predecessor-version'][0]['href'] );
     1141        $this->assertEquals( $revision_1->ID, $links['predecessor-version'][0]['attributes']['id'] );
    11181142    }
    11191143
Note: See TracChangeset for help on using the changeset viewer.