From d802d709c7357e8f310f7f6569666a5ff7187fab Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 13 Jul 2018 06:14:25 +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. Merge of [43439] and [43441] to the 4.9 branch. Props kadamwhite, danielbachhuber, birgire, TimothyBlynJacobs, pento. Fixes #44321. Built from https://develop.svn.wordpress.org/branches/4.9@43442 git-svn-id: http://core.svn.wordpress.org/branches/4.9@43269 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../endpoints/class-wp-rest-posts-controller.php | 16 +++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 334d1a2db6..e93ccfbb4a 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1667,9 +1667,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/wp-includes/version.php b/wp-includes/version.php index c994f34b7d..ad72c1e2a9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.9.8-alpha-43438'; +$wp_version = '4.9.8-alpha-43442'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.