WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/13/2018 04:06:23 AM (17 months 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.

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r43437 r43439  
    16761676
    16771677        if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
     1678            $revisions       = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
     1679            $revisions_count = count( $revisions );
     1680
    16781681            $links['version-history'] = array(
    1679                 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
    1680             );
     1682                'href'  => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
     1683                'count' => $revisions_count,
     1684            );
     1685
     1686            if ( $revisions_count > 0 ) {
     1687                $last_revision = array_shift( $revisions );
     1688
     1689                $links['predecessor-version'] = array(
     1690                    'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
     1691                    'id'   => $last_revision,
     1692                );
     1693            }
     1694
    16811695        }
    16821696
Note: See TracChangeset for help on using the changeset viewer.