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.


Built from https://develop.svn.wordpress.org/trunk@48422


git-svn-id: http://core.svn.wordpress.org/trunk@48191 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2020-07-10 15:13:07 +00:00
parent 3df6964fc8
commit 7d0162bc2e
3 changed files with 29 additions and 13 deletions

View File

@ -1437,14 +1437,14 @@ function register_and_do_post_meta_boxes( $post ) {
$publish_callback_args = array( '__back_compat_meta_box' => true );
if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' !== $post->post_status ) {
$revisions = wp_get_post_revisions( $post->ID );
$revisions = wp_get_post_revisions( $post->ID, array( 'fields' => 'ids' ) );
// We should aim to show the revisions meta box only when there are revisions.
if ( count( $revisions ) > 1 ) {
reset( $revisions ); // Reset pointer for key().
$publish_callback_args = array(
'revisions_count' => count( $revisions ),
'revision_id' => key( $revisions ),
'revision_id' => array_shift( $revisions ),
'__back_compat_meta_box' => true,
);
add_meta_box( 'revisionsdiv', __( 'Revisions' ), 'post_revisions_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );

View File

@ -221,7 +221,7 @@ function wp_save_post_revision( $post_id ) {
/**
* Retrieve the autosaved data of the specified post.
*
* Returns a post object containing the information that was autosaved for the
* Returns an object containing the information that was autosaved for the
* specified post. If the optional $user_id is passed, returns the autosave for that user
* otherwise returns the latest autosave.
*
@ -232,19 +232,35 @@ function wp_save_post_revision( $post_id ) {
* @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
*/
function wp_get_post_autosave( $post_id, $user_id = 0 ) {
$revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
global $wpdb;
foreach ( $revisions as $revision ) {
if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
if ( $user_id && $user_id != $revision->post_author ) {
continue;
}
$autosave_name = $post_id . '-autosave-v1';
$user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null;
return $revision;
}
// Construct the autosave query
$autosave_query = "
SELECT *
FROM $wpdb->posts
WHERE post_parent = %d
AND post_type = 'revision'
AND post_status = 'inherit'
AND post_name = %s " . $user_id_query . '
ORDER BY post_date DESC
LIMIT 1';
$autosave = $wpdb->get_results(
$wpdb->prepare(
$autosave_query,
$post_id,
$autosave_name
)
);
if ( ! $autosave ) {
return false;
}
return false;
return $autosave[0];
}
/**

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-beta1-48421';
$wp_version = '5.5-beta1-48422';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.