REST API: Use wp_get_lastest_revision_id_and_total_count function in 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
This commit is contained in:
Jonny Harris
2022-07-22 13:22:04 +00:00
parent e822a9a209
commit 3506684f2a
4 changed files with 108 additions and 5 deletions
+51
View File
@@ -655,6 +655,57 @@ class Tests_Post_Revisions extends WP_UnitTestCase {
$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.
*
@@ -121,7 +121,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
}
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;
}