Make WordPress Core


Ignore:
Timestamp:
07/22/2022 01:22:04 PM (21 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 *
Note: See TracChangeset for help on using the changeset viewer.