diff --git a/src/wp-admin/includes/meta-boxes.php b/src/wp-admin/includes/meta-boxes.php index 4584fff555..95513ae74e 100644 --- a/src/wp-admin/includes/meta-boxes.php +++ b/src/wp-admin/includes/meta-boxes.php @@ -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 ) ); diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php index 9eb74b98a9..119bef816b 100644 --- a/src/wp-includes/revision.php +++ b/src/wp-includes/revision.php @@ -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]; } /** diff --git a/tests/phpunit/tests/ajax/CustomizeManager.php b/tests/phpunit/tests/ajax/CustomizeManager.php index 1126e8e667..1ed20de31f 100644 --- a/tests/phpunit/tests/ajax/CustomizeManager.php +++ b/tests/phpunit/tests/ajax/CustomizeManager.php @@ -472,7 +472,7 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase { $this->assertTrue( $this->_last_response_parsed['success'] ); $this->assertEquals( 'draft', $this->_last_response_parsed['data']['changeset_status'] ); $autosave_revision = wp_get_post_autosave( $post_id ); - $this->assertInstanceOf( 'WP_Post', $autosave_revision ); + $this->assertInstanceOf( 'stdClass', $autosave_revision ); $this->assertContains( 'New Site Title', get_post( $post_id )->post_content ); $this->assertContains( 'Autosaved Site Title', $autosave_revision->post_content ); @@ -699,7 +699,7 @@ class Tests_Ajax_CustomizeManager extends WP_Ajax_UnitTestCase { ); $this->assertNotWPError( $r ); $autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id() ); - $this->assertInstanceOf( 'WP_Post', $autosave_revision ); + $this->assertInstanceOf( 'stdClass', $autosave_revision ); $this->assertContains( 'Foo', get_post( $wp_customize->changeset_post_id() )->post_content ); $this->assertContains( 'Bar', $autosave_revision->post_content ); diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index 00dcd333c6..dda069dfc1 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -1891,7 +1891,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { // Verify that autosave happened. $autosave_revision = wp_get_post_autosave( $changeset_post_id, get_current_user_id() ); - $this->assertInstanceOf( 'WP_Post', $autosave_revision ); + $this->assertInstanceOf( 'stdClass', $autosave_revision ); $this->assertContains( 'Draft Title', get_post( $changeset_post_id )->post_content ); $this->assertContains( 'Autosave Title', $autosave_revision->post_content ); }