Make WordPress Core

Changeset 53759


Ignore:
Timestamp:
07/22/2022 01:22:04 PM (4 years ago)
Author:
spacedmonkey
Message:

REST API: Use wp_get_lastest_revision_id_and_total_count function in WP_REST_Posts_Controller class.

Add new function called wp_get_lastest_revision_id_and_total_count, that performs an optimized query to get the last revision and total and use it in WP_REST_Posts_Controller class.

Props Spacedmonkey, timothyblynjacobs, furi3r, peterwilsoncc.
Fixes #55857.

Location:
trunk
Files:
4 edited

Legend:

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

    r53507 r53759  
    20252025
    20262026                if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
    2027                         $revisions       = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
    2028                         $revisions_count = count( $revisions );
     2027                        $revision        = wp_get_lastest_revision_id_and_total_count( $post->ID );
     2028                        $revisions_count = ! is_wp_error( $revision ) ? $revision['count'] : 0;
    20292029
    20302030                        $links['version-history'] = array(
     
    20342034
    20352035                        if ( $revisions_count > 0 ) {
    2036                                 $last_revision = array_shift( $revisions );
    2037 
     2036                                $last_revision                = $revision['revision'];
    20382037                                $links['predecessor-version'] = array(
    20392038                                        'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $last_revision ),
  • trunk/src/wp-includes/revision.php

    r53715 r53759  
    529529
    530530/**
     531 * Get latest revision and count of revisions for a post.
     532 *
     533 * @since 6.1.0
     534 *
     535 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
     536 * @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.
     541 * }
     542 */
     543function wp_get_lastest_revision_id_and_total_count( $post = null ) {
     544        $post = get_post( $post );
     545
     546        if ( ! $post ) {
     547                return new WP_Error( 'revision_error', __( 'Invalid post.' ) );
     548        }
     549
     550        if ( ! wp_revisions_enabled( $post ) ) {
     551                return new WP_Error( 'revision_error', __( 'Revisions not enabled.' ) );
     552        }
     553
     554        $args = array(
     555                'post_parent'         => $post->ID,
     556                'fields'              => 'ids',
     557                'post_type'           => 'revision',
     558                'post_status'         => 'inherit',
     559                'order'               => 'DESC',
     560                'orderby'             => 'date ID',
     561                'posts_per_page'      => 1,
     562                'ignore_sticky_posts' => true,
     563        );
     564
     565        $revision_query = new WP_Query();
     566        $revisions      = $revision_query->query( $args );
     567
     568        if ( ! $revisions ) {
     569                return array(
     570                        'revision' => 0,
     571                        'count'    => 0,
     572                );
     573        }
     574
     575        return array(
     576                'revision' => $revisions[0],
     577                'count'    => $revision_query->found_posts,
     578        );
     579}
     580
     581/**
    531582 * Returns the url for viewing and potentially restoring revisions of a given post.
    532583 *
  • trunk/tests/phpunit/tests/post/revisions.php

    r52389 r53759  
    657657
    658658        /**
     659         * Tests that wp_get_lastest_revision_id_and_total_count() returns last revision id and total count.
     660         *
     661         * @ticket 55857
     662         * @dataProvider data_wp_get_post_revisions_url
     663         */
     664        public function test_wp_get_last_revision_id_and_total_count( $revisions ) {
     665                $post_id = self::factory()->post->create();
     666                for ( $i = 0; $i < $revisions; ++$i ) {
     667                        wp_update_post(
     668                                array(
     669                                        'ID'         => $post_id,
     670                                        'post_title' => 'Some Post',
     671                                )
     672                        );
     673                }
     674
     675                $post_revisions     = wp_get_post_revisions( $post_id );
     676                $last_post_revision = current( $post_revisions );
     677                $revision           = wp_get_lastest_revision_id_and_total_count( $post_id );
     678
     679                $this->assertSame(
     680                        $last_post_revision->ID,
     681                        $revision['revision'],
     682                        'Failed asserting latest revision id.'
     683                );
     684
     685                $this->assertSame(
     686                        count( $post_revisions ),
     687                        $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.
     694         *
     695         * @ticket 55857
     696         */
     697        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.' );
     701
     702                add_filter( 'wp_revisions_to_keep', '__return_zero' );
     703                $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.' );
     707        }
     708
     709        /**
    659710         * Tests that wp_get_post_revisions_url() returns the revisions URL.
    660711         *
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r53512 r53759  
    122122
    123123        public function save_posts_clauses( $orderby, $query ) {
    124                 array_push( $this->posts_clauses, $orderby );
     124                if ( 'revision' !== $query->query_vars['post_type'] ) {
     125                        array_push( $this->posts_clauses, $orderby );
     126                }
    125127                return $orderby;
    126128        }
Note: See TracChangeset for help on using the changeset viewer.