Skip to content

Commit 7e8feef

Browse files
Revisions: Correct the function name for retrieving the last revision ID and total count.
Includes: * Renaming the function to `wp_get_last_revision_id_and_total_count()`. * Making the default value for `$post` consistent with `wp_get_post_revisions()`. * Making `WP_Error` codes more specific and using them in test assertions. * Adjusting the function description per the documentation standards. Follow-up to [53759]. Props JustinSainton, SergeyBiryukov. See #55857. git-svn-id: https://develop.svn.wordpress.org/trunk@53769 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a560641 commit 7e8feef

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ protected function prepare_links( $post ) {
20262026
}
20272027

20282028
if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
2029-
$revision = wp_get_lastest_revision_id_and_total_count( $post->ID );
2029+
$revision = wp_get_last_revision_id_and_total_count( $post->ID );
20302030
$revisions_count = ! is_wp_error( $revision ) ? $revision['count'] : 0;
20312031

20322032
$links['version-history'] = array(
@@ -2035,7 +2035,8 @@ protected function prepare_links( $post ) {
20352035
);
20362036

20372037
if ( $revisions_count > 0 ) {
2038-
$last_revision = $revision['revision'];
2038+
$last_revision = $revision['revision'];
2039+
20392040
$links['predecessor-version'] = array(
20402041
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
20412042
'id' => $last_revision,

src/wp-includes/revision.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -528,27 +528,27 @@ function wp_get_post_revisions( $post = 0, $args = null ) {
528528
}
529529

530530
/**
531-
* Get latest revision and count of revisions for a post.
531+
* Returns the latest revision ID and count of revisions for a post.
532532
*
533533
* @since 6.1.0
534534
*
535-
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
535+
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
536536
* @return WP_Error|array {
537-
* Returns associative array with last revision and total count.
537+
* Returns associative array with last revision ID and total count.
538538
*
539-
* @type int $revision The last revision post id or 0 if non existing.
540-
* @type int $count The total count of revisions for $post_id.
539+
* @type int $revision The last revision post ID or 0 if no revisions exist.
540+
* @type int $count The total count of revisions for the given post.
541541
* }
542542
*/
543-
function wp_get_lastest_revision_id_and_total_count( $post = null ) {
543+
function wp_get_last_revision_id_and_total_count( $post = 0 ) {
544544
$post = get_post( $post );
545545

546546
if ( ! $post ) {
547-
return new WP_Error( 'revision_error', __( 'Invalid post.' ) );
547+
return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
548548
}
549549

550550
if ( ! wp_revisions_enabled( $post ) ) {
551-
return new WP_Error( 'revision_error', __( 'Revisions not enabled.' ) );
551+
return new WP_Error( 'revisions_not_enabled', __( 'Revisions not enabled.' ) );
552552
}
553553

554554
$args = array(

tests/phpunit/tests/post/revisions.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ public function test_wp_save_post_revision_error() {
656656
}
657657

658658
/**
659-
* Tests that wp_get_lastest_revision_id_and_total_count() returns last revision id and total count.
659+
* Tests that wp_get_last_revision_id_and_total_count() returns the last revision ID and total count.
660660
*
661661
* @ticket 55857
662662
* @dataProvider data_wp_get_post_revisions_url
@@ -674,36 +674,38 @@ public function test_wp_get_last_revision_id_and_total_count( $revisions ) {
674674

675675
$post_revisions = wp_get_post_revisions( $post_id );
676676
$last_post_revision = current( $post_revisions );
677-
$revision = wp_get_lastest_revision_id_and_total_count( $post_id );
677+
$revision = wp_get_last_revision_id_and_total_count( $post_id );
678678

679679
$this->assertSame(
680680
$last_post_revision->ID,
681681
$revision['revision'],
682-
'Failed asserting latest revision id.'
682+
'The last revision ID does not match.'
683683
);
684684

685685
$this->assertSame(
686686
count( $post_revisions ),
687687
$revision['count'],
688-
'Failed asserting total count of revision.'
688+
'The total count of revisions does not match.'
689689
);
690690
}
691691

692692
/**
693-
* Tests that wp_get_lastest_revision_id_and_total_count() when no revisions.
693+
* Tests that wp_get_last_revision_id_and_total_count() returns a WP_Error when no revisions exist.
694694
*
695695
* @ticket 55857
696696
*/
697697
public function test_wp_get_last_revision_id_and_total_count_no_revisions() {
698-
$revision = wp_get_lastest_revision_id_and_total_count( null );
699-
$this->assertWPError( $revision, 'Invalid Post, non existing revisions.' );
700-
$this->assertSame( $revision->get_error_message(), 'Invalid post.' );
698+
$revision = wp_get_last_revision_id_and_total_count( null );
699+
700+
$this->assertWPError( $revision, 'Invalid post, no revisions should exist.' );
701+
$this->assertSame( $revision->get_error_code(), 'invalid_post' );
701702

702703
add_filter( 'wp_revisions_to_keep', '__return_zero' );
703704
$post_id = self::factory()->post->create();
704-
$revision = wp_get_lastest_revision_id_and_total_count( $post_id );
705-
$this->assertWPError( $revision, 'Revisions should be not enabled.' );
706-
$this->assertSame( $revision->get_error_message(), 'Revisions not enabled.' );
705+
$revision = wp_get_last_revision_id_and_total_count( $post_id );
706+
707+
$this->assertWPError( $revision, 'Revisions should not be enabled.' );
708+
$this->assertSame( $revision->get_error_code(), 'revisions_not_enabled' );
707709
}
708710

709711
/**

0 commit comments

Comments
 (0)