Skip to content

Commit

Permalink
Revisions: Use WP_Query in wp_get_post_autosave.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/trunk@59715 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
spacedmonkey committed Jan 27, 2025
1 parent ed8cb2a commit 7091fcd
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 26 deletions.
44 changes: 18 additions & 26 deletions src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}

/**
Expand Down
165 changes: 165 additions & 0 deletions tests/phpunit/tests/post/wpGetAutoSave.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

/**
* @group post
*/
class Tests_Post_wpGetPostAutosave extends WP_UnitTestCase {

/**
* Admin user ID.
*
* @var int
*/
protected static $admin_id;

/**
* Editor user ID.
*
* @var int
*/
protected static $editor_id;

/**
* Post ID.
*
* @var int
*/
protected static $post_id;

/**
* Set up before class.
*
* @param WP_UnitTest_Factory $factory Factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );

wp_set_current_user( self::$admin_id );
self::$post_id = $factory->post->create( array( 'post_status' => 'publish' ) );
}

/**
* Test when no autosave exists for a post.
*
* @ticket 62658
*/
public function test_no_autosave_exists() {
$autosave = wp_get_post_autosave( self::$post_id );
$this->assertFalse( $autosave, 'Expected no autosave.' );
}

/**
* Test when an autosave exists for a post.
*
* @ticket 62658
*/
public function test_autosave_exists() {
$autosave_id = $this->factory()->post->create(
array(
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => self::$post_id,
'post_author' => self::$admin_id,
'post_content' => 'Autosaved content',
'post_name' => self::$post_id . '-autosave-v1',
)
);

$autosave = wp_get_post_autosave( self::$post_id );

$this->assertInstanceOf( 'WP_Post', $autosave );
$this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' );
$this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' );
}

/**
* Test when an autosave exists for a specific user.
*
* @ticket 62658
*/
public function test_autosave_for_specific_user() {
$autosave_id = $this->factory()->post->create(
array(
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => self::$post_id,
'post_author' => self::$editor_id,
'post_content' => 'Editor-specific autosave',
'post_name' => self::$post_id . '-autosave-v1',
)
);

$autosave = wp_get_post_autosave( self::$post_id, self::$editor_id );

$this->assertInstanceOf( 'WP_Post', $autosave );
$this->assertSame( self::$editor_id, (int) $autosave->post_author, 'Post author does not match.' );
$this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' );
}

/**
* Test when an autosave is updated.
*
* @ticket 62658
*/
public function test_autosave_exists_update_caches() {
$autosave_id = $this->factory()->post->create(
array(
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => self::$post_id,
'post_author' => self::$admin_id,
'post_content' => 'Autosaved content',
'post_name' => self::$post_id . '-autosave-v1',
)
);

$autosave = wp_get_post_autosave( self::$post_id );

$this->assertInstanceOf( 'WP_Post', $autosave );
$this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' );
$this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' );
$this->assertSame( 'Autosaved content', $autosave->post_content, 'Post content does not match.' );

wp_update_post(
array(
'ID' => $autosave->ID,
'post_content' => 'Autosaved content updated',
)
);

$autosave = wp_get_post_autosave( self::$post_id );
$this->assertInstanceOf( 'WP_Post', $autosave );
$this->assertSame( 'Autosaved content updated', $autosave->post_content, 'Post content does not match.' );
}

/**
* Test when an autosave is deleted
*
* @ticket 62658
*/
public function test_autosave_exists_and_deleted() {
$autosave_id = $this->factory()->post->create(
array(
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => self::$post_id,
'post_author' => self::$admin_id,
'post_content' => 'Autosaved content',
'post_name' => self::$post_id . '-autosave-v1',
)
);

$autosave = wp_get_post_autosave( self::$post_id );

$this->assertInstanceOf( 'WP_Post', $autosave );
$this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' );
$this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' );
$this->assertSame( 'Autosaved content', $autosave->post_content, 'Post content does not match.' );

wp_delete_post( $autosave->ID, true );

$autosave = wp_get_post_autosave( self::$post_id );
$this->assertFalse( $autosave, 'Autosave should not exist' );
}
}

0 comments on commit 7091fcd

Please sign in to comment.