Make WordPress Core


Ignore:
Timestamp:
07/10/2020 03:12:00 PM (6 years ago)
Author:
adamsilverstein
Message:

Revisions: optimize performance when post has large number of revisions.

Improve speed and reduce the memory footprint when loading posts with many revisions.

  • Use a direct query in wp_get_post_autosave to avoid loading all revisions.
  • Query for IDs vs full objects in register_and_do_post_meta_boxes.

Props pdfernhout, johnnyb, miqrogroove, ocean90, senatorman, DBrumbaugh10Up, martijn-van-der-kooij, pavelevap, mackensen, mikeyarce, whyisjake.
Fixes #34560.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/revision.php

    r48109 r48422  
    222222 * Retrieve the autosaved data of the specified post.
    223223 *
    224  * Returns a post object containing the information that was autosaved for the
     224 * Returns an object containing the information that was autosaved for the
    225225 * specified post. If the optional $user_id is passed, returns the autosave for that user
    226226 * otherwise returns the latest autosave.
     
    233233 */
    234234function wp_get_post_autosave( $post_id, $user_id = 0 ) {
    235         $revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
    236 
    237         foreach ( $revisions as $revision ) {
    238                 if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
    239                         if ( $user_id && $user_id != $revision->post_author ) {
    240                                 continue;
    241                         }
    242 
    243                         return $revision;
    244                 }
    245         }
    246 
    247         return false;
     235        global $wpdb;
     236
     237        $autosave_name = $post_id . '-autosave-v1';
     238        $user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null;
     239
     240        // Construct the autosave query
     241        $autosave_query = "
     242                SELECT *
     243                FROM $wpdb->posts
     244                WHERE post_parent = %d
     245                AND post_type = 'revision'
     246                AND post_status = 'inherit'
     247                AND post_name   = %s " . $user_id_query . '
     248                ORDER BY post_date DESC
     249                LIMIT 1';
     250
     251        $autosave = $wpdb->get_results(
     252                $wpdb->prepare(
     253                        $autosave_query,
     254                        $post_id,
     255                        $autosave_name
     256                )
     257        );
     258
     259        if ( ! $autosave ) {
     260                return false;
     261        }
     262
     263        return $autosave[0];
    248264}
    249265
Note: See TracChangeset for help on using the changeset viewer.