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/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r43438 r43442  
    16681668
    16691669        if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
     1670            $revisions       = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
     1671            $revisions_count = count( $revisions );
     1672
    16701673            $links['version-history'] = array(
    1671                 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
    1672             );
     1674                'href'  => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
     1675                'count' => $revisions_count,
     1676            );
     1677
     1678            if ( $revisions_count > 0 ) {
     1679                $last_revision = array_shift( $revisions );
     1680
     1681                $links['predecessor-version'] = array(
     1682                    'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
     1683                    'id'   => $last_revision,
     1684                );
     1685            }
     1686
    16731687        }
    16741688
Note: See TracChangeset for help on using the changeset viewer.