Make WordPress Core

Changeset 53769


Ignore:
Timestamp:
07/23/2022 03:40:10 PM (22 months ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

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

    r53760 r53769  
    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
     
    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 ),
  • trunk/src/wp-includes/revision.php

    r53759 r53769  
    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.
    538  *
    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.
     537 *     Returns associative array with last revision ID and total count.
     538 *
     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 ) {
     543function 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
  • trunk/tests/phpunit/tests/post/revisions.php

    r53759 r53769  
    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
     
    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
     
    686686            count( $post_revisions ),
    687687            $revision['count'],
    688             'Failed asserting total count of revision.'
    689         );
    690     }
    691 
    692     /**
    693      * Tests that wp_get_lastest_revision_id_and_total_count() when no revisions.
     688            'The total count of revisions does not match.'
     689        );
     690    }
     691
     692    /**
     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
Note: See TracChangeset for help on using the changeset viewer.