Make WordPress Core

Changeset 53842


Ignore:
Timestamp:
08/05/2022 01:37:47 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Revisions: Use wp_get_latest_revision_id_and_total_count() where appropriate.

The function executes an optimized query to get the last revision ID and total count. It was originally introduced for WP_REST_Posts_Controller::prepare_links(), and is now used in a few more places in core:

  • register_and_do_post_meta_boxes()
  • wp_get_post_revisions_url()
  • wp_update_custom_css_post()

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

Props peterwilsoncc, mukesh27, SergeyBiryukov.
Fixes #56279.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/meta-boxes.php

    r53201 r53842  
    14601460
    14611461    if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' !== $post->post_status ) {
    1462         $revisions = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
     1462        $revisions = wp_get_latest_revision_id_and_total_count( $post->ID );
    14631463
    14641464        // We should aim to show the revisions meta box only when there are revisions.
    1465         if ( count( $revisions ) > 1 ) {
     1465        if ( ! is_wp_error( $revisions ) && $revisions['count'] > 1 ) {
    14661466            $publish_callback_args = array(
    1467                 'revisions_count'        => count( $revisions ),
    1468                 'revision_id'            => reset( $revisions ),
     1467                'revisions_count'        => $revisions['count'],
     1468                'revision_id'            => $revisions['latest_id'],
    14691469                '__back_compat_meta_box' => true,
    14701470            );
  • trunk/src/wp-includes/revision.php

    r53841 r53842  
    604604    }
    605605
    606     $revisions = wp_get_post_revisions( $post->ID, array( 'posts_per_page' => 1 ) );
    607 
    608     if ( 0 === count( $revisions ) ) {
     606    $revisions = wp_get_latest_revision_id_and_total_count( $post->ID );
     607
     608    if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) {
    609609        return null;
    610610    }
    611611
    612     $revision = reset( $revisions );
    613     return get_edit_post_link( $revision );
     612    return get_edit_post_link( $revisions['latest_id'] );
    614613}
    615614
  • trunk/src/wp-includes/theme.php

    r53741 r53842  
    20692069
    20702070            // Trigger creation of a revision. This should be removed once #30854 is resolved.
    2071             if ( 0 === count( wp_get_post_revisions( $r ) ) ) {
     2071            $revisions = wp_get_latest_revision_id_and_total_count( $r );
     2072            if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) {
    20722073                wp_save_post_revision( $r );
    20732074            }
Note: See TracChangeset for help on using the changeset viewer.