Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored wp_get_post_autosave function to use WP_Query for query #7965

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,42 +270,35 @@ function wp_save_post_revision( $post_id ) {
*
* @since 2.6.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_id The post ID.
* @param int $user_id Optional. The post author ID. Default 0.
* @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 ) {
global $wpdb;

$autosave_name = $post_id . '-autosave-v1';
$user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null;

// 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
)

$args = array(
'post_type' => 'revision',
'post_parent' => $post_id,
'post_status' => 'inherit',
'post_name' => $autosave_name,
'posts_per_page' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'fields' => 'ids',
);

if ( ! $autosave ) {
if ( $user_id ) {
$args['author'] = $user_id;
}

$query = new WP_Query( $args );

if ( ! $query->have_posts() ) {
return false;
}

return get_post( $autosave[0] );
return get_post( $query->posts[0] );
}

/**
Expand Down
Loading