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 b7b2dd79f6..5fa5c80de8 100644
a
|
b
|
class WP_REST_Posts_Controller extends WP_REST_Controller { |
1664 | 1664 | } |
1665 | 1665 | |
1666 | 1666 | if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) { |
| 1667 | $revisions = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) ); |
| 1668 | $revisions_count = count( $revisions ); |
| 1669 | |
1667 | 1670 | $links['version-history'] = array( |
1668 | | 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ), |
| 1671 | 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ), |
| 1672 | 'count' => $revisions_count, |
1669 | 1673 | ); |
| 1674 | |
| 1675 | if ( $revisions_count > 0 ) { |
| 1676 | $last_revision = array_shift( $revisions ); |
| 1677 | |
| 1678 | $links['predecessor-version'] = array( |
| 1679 | 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ), |
| 1680 | 'id' => $last_revision, |
| 1681 | ); |
| 1682 | } |
| 1683 | |
1670 | 1684 | } |
1671 | 1685 | |
1672 | 1686 | $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 721b08292a..7d39f9197e 100644
a
|
b
|
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
1283 | 1283 | $this->assertEquals( $replies_url, $links['replies'][0]['href'] ); |
1284 | 1284 | |
1285 | 1285 | $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'] ) ); |
1286 | 1288 | |
1287 | 1289 | $attachments_url = rest_url( '/wp/v2/media' ); |
1288 | 1290 | $attachments_url = add_query_arg( 'parent', self::$post_id, $attachments_url ); |
… |
… |
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
1310 | 1312 | $this->assertEquals( $category_url, $cat_link['href'] ); |
1311 | 1313 | } |
1312 | 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'] ); |
| 1335 | } |
| 1336 | |
1313 | 1337 | public function test_get_item_links_no_author() { |
1314 | 1338 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
1315 | 1339 | $response = rest_get_server()->dispatch( $request ); |