From ce154a4b66f5a3446aa4d997966f46e9119bb626 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 25 Jul 2022 19:18:41 +0000 Subject: [PATCH] Revisions: Rename the function for retrieving the latest revision ID and total count. The new name is `wp_get_latest_revision_id_and_total_count()`. This aims to reduce ambiguity about what exactly is the "first" or "last" revision, and bring more consistency with similar wording elsewhere in core, e.g. latest posts, latest comments, etc. Follow-up to [53759], [53769]. Props peterwilsoncc. See #55857. git-svn-id: https://develop.svn.wordpress.org/trunk@53778 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-posts-controller.php | 8 +++---- src/wp-includes/revision.php | 6 ++--- tests/phpunit/tests/post/revisions.php | 24 ++++++++++--------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 72e938cf83..008124d582 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -2026,7 +2026,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) { - $revision = wp_get_last_revision_id_and_total_count( $post->ID ); + $revision = wp_get_latest_revision_id_and_total_count( $post->ID ); $revisions_count = ! is_wp_error( $revision ) ? $revision['count'] : 0; $links['version-history'] = array( @@ -2035,11 +2035,11 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { ); if ( $revisions_count > 0 ) { - $last_revision = $revision['revision']; + $latest_revision = $revision['revision']; $links['predecessor-version'] = array( - 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ), - 'id' => $last_revision, + 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $latest_revision ), + 'id' => $latest_revision, ); } } diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php index 7874a897fb..f1a888f6dd 100644 --- a/src/wp-includes/revision.php +++ b/src/wp-includes/revision.php @@ -534,13 +534,13 @@ function wp_get_post_revisions( $post = 0, $args = null ) { * * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. * @return WP_Error|array { - * Returns associative array with last revision ID and total count. + * Returns associative array with latest revision ID and total count. * - * @type int $revision The last revision post ID or 0 if no revisions exist. + * @type int $revision The latest revision post ID or 0 if no revisions exist. * @type int $count The total count of revisions for the given post. * } */ -function wp_get_last_revision_id_and_total_count( $post = 0 ) { +function wp_get_latest_revision_id_and_total_count( $post = 0 ) { $post = get_post( $post ); if ( ! $post ) { diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 66ac54a412..e95f08f927 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -656,12 +656,13 @@ class Tests_Post_Revisions extends WP_UnitTestCase { } /** - * Tests that wp_get_last_revision_id_and_total_count() returns the last revision ID and total count. + * Tests that wp_get_latest_revision_id_and_total_count() returns the latest revision ID and total count. * + * @covers ::wp_get_latest_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 ) { + public function test_wp_get_latest_revision_id_and_total_count( $revisions ) { $post_id = self::factory()->post->create(); for ( $i = 0; $i < $revisions; ++$i ) { wp_update_post( @@ -672,14 +673,14 @@ class Tests_Post_Revisions extends WP_UnitTestCase { ); } - $post_revisions = wp_get_post_revisions( $post_id ); - $last_post_revision = current( $post_revisions ); - $revision = wp_get_last_revision_id_and_total_count( $post_id ); + $post_revisions = wp_get_post_revisions( $post_id ); + $latest_post_revision = current( $post_revisions ); + $revision = wp_get_latest_revision_id_and_total_count( $post_id ); $this->assertSame( - $last_post_revision->ID, + $latest_post_revision->ID, $revision['revision'], - 'The last revision ID does not match.' + 'The latest revision ID does not match.' ); $this->assertSame( @@ -690,19 +691,20 @@ class Tests_Post_Revisions extends WP_UnitTestCase { } /** - * Tests that wp_get_last_revision_id_and_total_count() returns a WP_Error when no revisions exist. + * Tests that wp_get_latest_revision_id_and_total_count() returns a WP_Error when no revisions exist. * + * @covers ::wp_get_latest_revision_id_and_total_count * @ticket 55857 */ - public function test_wp_get_last_revision_id_and_total_count_no_revisions() { - $revision = wp_get_last_revision_id_and_total_count( null ); + public function test_wp_get_latest_revision_id_and_total_count_no_revisions() { + $revision = wp_get_latest_revision_id_and_total_count( null ); $this->assertWPError( $revision, 'Invalid post, no revisions should exist.' ); $this->assertSame( $revision->get_error_code(), 'invalid_post' ); add_filter( 'wp_revisions_to_keep', '__return_zero' ); $post_id = self::factory()->post->create(); - $revision = wp_get_last_revision_id_and_total_count( $post_id ); + $revision = wp_get_latest_revision_id_and_total_count( $post_id ); $this->assertWPError( $revision, 'Revisions should not be enabled.' ); $this->assertSame( $revision->get_error_code(), 'revisions_not_enabled' );