Revisions: Use latest_id as the array key for the latest revision ID.

This updates `wp_get_latest_revision_id_and_total_count()` and its usage to be a bit more descriptive and a bit less repetitive, e.g. `$revisions['latest_id']` instead of `$revision['revision']`.

Includes updating the `@return` tag to explain when the function returns a `WP_Error`.

Follow-up to [53759], [53769], [53778], [53779].

See #55857.

git-svn-id: https://develop.svn.wordpress.org/trunk@53841 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2022-08-05 13:00:57 +00:00
parent 60284ca751
commit 81805856e2
3 changed files with 16 additions and 17 deletions
@@ -2064,8 +2064,8 @@ 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_latest_revision_id_and_total_count( $post->ID );
$revisions_count = ! is_wp_error( $revision ) ? $revision['count'] : 0;
$revisions = wp_get_latest_revision_id_and_total_count( $post->ID );
$revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0;
$links['version-history'] = array(
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
@@ -2073,11 +2073,9 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
);
if ( $revisions_count > 0 ) {
$latest_revision = $revision['revision'];
$links['predecessor-version'] = array(
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $latest_revision ),
'id' => $latest_revision,
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $revisions['latest_id'] ),
'id' => $revisions['latest_id'],
);
}
}
+9 -8
View File
@@ -533,11 +533,12 @@ function wp_get_post_revisions( $post = 0, $args = null ) {
* @since 6.1.0
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return WP_Error|array {
* Returns associative array with latest revision ID and total count.
* @return array|WP_Error {
* Returns associative array with latest revision ID and total count,
* or a WP_Error if the post does not exist or revisions are not enabled.
*
* @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.
* @type int $latest_id 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_latest_revision_id_and_total_count( $post = 0 ) {
@@ -567,14 +568,14 @@ function wp_get_latest_revision_id_and_total_count( $post = 0 ) {
if ( ! $revisions ) {
return array(
'revision' => 0,
'count' => 0,
'latest_id' => 0,
'count' => 0,
);
}
return array(
'revision' => $revisions[0],
'count' => $revision_query->found_posts,
'latest_id' => $revisions[0],
'count' => $revision_query->found_posts,
);
}
+3 -3
View File
@@ -675,17 +675,17 @@ class Tests_Post_Revisions extends WP_UnitTestCase {
$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 );
$revisions = wp_get_latest_revision_id_and_total_count( $post_id );
$this->assertSame(
$latest_post_revision->ID,
$revision['revision'],
$revisions['latest_id'],
'The latest revision ID does not match.'
);
$this->assertSame(
count( $post_revisions ),
$revision['count'],
$revisions['count'],
'The total count of revisions does not match.'
);
}