Revisions: Use WP_Query in wp_get_post_autosave.

Replaced the raw SQL query in the `wp_get_post_autosave` function with a `WP_Query` call. This change improves code maintainability and replaces the raw SQL query with a cacheable query via `WP_Query`.

Props narenin, swissspidy, mukesh27, spacedmonkey, im3dabasia1.
Fixes #62658.
Built from https://develop.svn.wordpress.org/trunk@59715


git-svn-id: http://core.svn.wordpress.org/trunk@59057 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2025-01-27 23:07:23 +00:00
parent 2011617834
commit c32b20fa60
2 changed files with 19 additions and 27 deletions

View File

@ -270,42 +270,34 @@ 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_status' => 'inherit',
'post_parent' => $post_id,
'name' => $post_id . '-autosave-v1',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'fields' => 'ids',
'no_found_rows' => true,
);
if ( ! $autosave ) {
if ( 0 !== $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] );
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.8-alpha-59714';
$wp_version = '6.8-alpha-59715';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.