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.



git-svn-id: https://develop.svn.wordpress.org/trunk@48422 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein
2020-07-10 15:12:00 +00:00
parent 73d3e9cfd9
commit 4adb926ce8
4 changed files with 31 additions and 15 deletions
+26 -10
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];
}
/**