Skip to content

Commit

Permalink
Base inclusion of version-history count on existing link
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbachhuber committed Jun 25, 2018
1 parent 9823c74 commit 874d8b7
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,24 +353,24 @@ function gutenberg_add_revisions_data_to_links( $response, $post, $request ) {
$new_links = array();
$orig_links = $response->get_links();

if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
if ( ! empty( $orig_links['version-history'] ) ) {
$version_history_link = array_shift( $orig_links['version-history'] );
// 'version-history' already exists and we don't want to duplicate it.
$response->remove_link( 'version-history' );

$orig_href = ! empty( $orig_links['self'][0]['href'] ) ? $orig_links['self'][0]['href'] : null;
$revisions = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
$revisions_count = count( $revisions );

$new_links['version-history'] = array(
'href' => $orig_href . '/revisions',
'href' => $version_history_link['href'],
'count' => $revisions_count,
);

if ( $revisions_count > 0 ) {
$last_revision = array_shift( $revisions );

$new_links['predecessor-version'] = array(
'href' => $orig_href . '/revisions/' . $last_revision,
'href' => $version_history_link['href'] . '/' . $last_revision,
'id' => $last_revision,
);
}
Expand Down

0 comments on commit 874d8b7

Please sign in to comment.