Skip to content

Commit

Permalink
REST API: Use wp_get_lastest_revision_id_and_total_count function i…
Browse files Browse the repository at this point in the history
…n `WP_REST_Posts_Controller` class.

Add new function called `wp_get_lastest_revision_id_and_total_count`, that performs an optimized query to get the last revision and total and use it in `WP_REST_Posts_Controller` class. 

Props Spacedmonkey, timothyblynjacobs, furi3r, peterwilsoncc.
Fixes #55857.

git-svn-id: https://develop.svn.wordpress.org/trunk@53759 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
spacedmonkey committed Jul 22, 2022
1 parent e822a9a commit 3506684
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2024,17 +2024,16 @@ protected function prepare_links( $post ) {
}

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 );
$revision = wp_get_lastest_revision_id_and_total_count( $post->ID );
$revisions_count = ! is_wp_error( $revision ) ? $revision['count'] : 0;

$links['version-history'] = array(
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
'count' => $revisions_count,
);

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

$last_revision = $revision['revision'];
$links['predecessor-version'] = array(
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
'id' => $last_revision,
Expand Down
51 changes: 51 additions & 0 deletions src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,57 @@ function wp_get_post_revisions( $post = 0, $args = null ) {
return $revisions;
}

/**
* Get latest revision and count of revisions for a post.
*
* @since 6.1.0
*
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
* @return WP_Error|array {
* Returns associative array with last revision and total count.
*
* @type int $revision The last revision post id or 0 if non existing.
* @type int $count The total count of revisions for $post_id.
* }
*/
function wp_get_lastest_revision_id_and_total_count( $post = null ) {
$post = get_post( $post );

if ( ! $post ) {
return new WP_Error( 'revision_error', __( 'Invalid post.' ) );
}

if ( ! wp_revisions_enabled( $post ) ) {
return new WP_Error( 'revision_error', __( 'Revisions not enabled.' ) );
}

$args = array(
'post_parent' => $post->ID,
'fields' => 'ids',
'post_type' => 'revision',
'post_status' => 'inherit',
'order' => 'DESC',
'orderby' => 'date ID',
'posts_per_page' => 1,
'ignore_sticky_posts' => true,
);

$revision_query = new WP_Query();
$revisions = $revision_query->query( $args );

if ( ! $revisions ) {
return array(
'revision' => 0,
'count' => 0,
);
}

return array(
'revision' => $revisions[0],
'count' => $revision_query->found_posts,
);
}

/**
* Returns the url for viewing and potentially restoring revisions of a given post.
*
Expand Down
51 changes: 51 additions & 0 deletions tests/phpunit/tests/post/revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,57 @@ public function test_wp_save_post_revision_error() {
$this->assertWPError( $revision );
}

/**
* Tests that wp_get_lastest_revision_id_and_total_count() returns last revision id and total count.
*
* @ticket 55857
* @dataProvider data_wp_get_post_revisions_url
*/
public function test_wp_get_last_revision_id_and_total_count( $revisions ) {
$post_id = self::factory()->post->create();
for ( $i = 0; $i < $revisions; ++$i ) {
wp_update_post(
array(
'ID' => $post_id,
'post_title' => 'Some Post',
)
);
}

$post_revisions = wp_get_post_revisions( $post_id );
$last_post_revision = current( $post_revisions );
$revision = wp_get_lastest_revision_id_and_total_count( $post_id );

$this->assertSame(
$last_post_revision->ID,
$revision['revision'],
'Failed asserting latest revision id.'
);

$this->assertSame(
count( $post_revisions ),
$revision['count'],
'Failed asserting total count of revision.'
);
}

/**
* Tests that wp_get_lastest_revision_id_and_total_count() when no revisions.
*
* @ticket 55857
*/
public function test_wp_get_last_revision_id_and_total_count_no_revisions() {
$revision = wp_get_lastest_revision_id_and_total_count( null );
$this->assertWPError( $revision, 'Invalid Post, non existing revisions.' );
$this->assertSame( $revision->get_error_message(), 'Invalid post.' );

add_filter( 'wp_revisions_to_keep', '__return_zero' );
$post_id = self::factory()->post->create();
$revision = wp_get_lastest_revision_id_and_total_count( $post_id );
$this->assertWPError( $revision, 'Revisions should be not enabled.' );
$this->assertSame( $revision->get_error_message(), 'Revisions not enabled.' );
}

/**
* Tests that wp_get_post_revisions_url() returns the revisions URL.
*
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ public function wpSetUpBeforeRequest( $result, $server, $request ) {
}

public function save_posts_clauses( $orderby, $query ) {
array_push( $this->posts_clauses, $orderby );
if ( 'revision' !== $query->query_vars['post_type'] ) {
array_push( $this->posts_clauses, $orderby );
}
return $orderby;
}

Expand Down

0 comments on commit 3506684

Please sign in to comment.