Make WordPress Core

Changeset 53841


Ignore:
Timestamp:
08/05/2022 01:00:57 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r53814 r53841  
    20652065
    20662066        if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
    2067             $revision        = wp_get_latest_revision_id_and_total_count( $post->ID );
    2068             $revisions_count = ! is_wp_error( $revision ) ? $revision['count'] : 0;
     2067            $revisions       = wp_get_latest_revision_id_and_total_count( $post->ID );
     2068            $revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0;
    20692069
    20702070            $links['version-history'] = array(
     
    20742074
    20752075            if ( $revisions_count > 0 ) {
    2076                 $latest_revision = $revision['revision'];
    2077 
    20782076                $links['predecessor-version'] = array(
    2079                     'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $latest_revision ),
    2080                     'id'   => $latest_revision,
     2077                    'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $revisions['latest_id'] ),
     2078                    'id'   => $revisions['latest_id'],
    20812079                );
    20822080            }
  • trunk/src/wp-includes/revision.php

    r53779 r53841  
    534534 *
    535535 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
    536  * @return WP_Error|array {
    537  *     Returns associative array with latest revision ID and total count.
    538  *
    539  *     @type int $revision The latest revision post ID or 0 if no revisions exist.
    540  *     @type int $count    The total count of revisions for the given post.
     536 * @return array|WP_Error {
     537 *     Returns associative array with latest revision ID and total count,
     538 *     or a WP_Error if the post does not exist or revisions are not enabled.
     539 *
     540 *     @type int $latest_id The latest revision post ID or 0 if no revisions exist.
     541 *     @type int $count     The total count of revisions for the given post.
    541542 * }
    542543 */
     
    568569    if ( ! $revisions ) {
    569570        return array(
    570             'revision' => 0,
    571             'count'    => 0,
     571            'latest_id' => 0,
     572            'count'     => 0,
    572573        );
    573574    }
    574575
    575576    return array(
    576         'revision' => $revisions[0],
    577         'count'    => $revision_query->found_posts,
     577        'latest_id' => $revisions[0],
     578        'count'     => $revision_query->found_posts,
    578579    );
    579580}
  • trunk/tests/phpunit/tests/post/revisions.php

    r53778 r53841  
    676676        $post_revisions       = wp_get_post_revisions( $post_id );
    677677        $latest_post_revision = current( $post_revisions );
    678         $revision             = wp_get_latest_revision_id_and_total_count( $post_id );
     678        $revisions            = wp_get_latest_revision_id_and_total_count( $post_id );
    679679
    680680        $this->assertSame(
    681681            $latest_post_revision->ID,
    682             $revision['revision'],
     682            $revisions['latest_id'],
    683683            'The latest revision ID does not match.'
    684684        );
     
    686686        $this->assertSame(
    687687            count( $post_revisions ),
    688             $revision['count'],
     688            $revisions['count'],
    689689            'The total count of revisions does not match.'
    690690        );
Note: See TracChangeset for help on using the changeset viewer.