Skip to content

Commit 7091fcd

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@59715 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ed8cb2a commit 7091fcd

File tree

2 files changed

+183
-26
lines changed

2 files changed

+183
-26
lines changed

src/wp-includes/revision.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -270,42 +270,34 @@ function wp_save_post_revision( $post_id ) {
270270
*
271271
* @since 2.6.0
272272
*
273-
* @global wpdb $wpdb WordPress database abstraction object.
274-
*
275273
* @param int $post_id The post ID.
276274
* @param int $user_id Optional. The post author ID. Default 0.
277275
* @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
278276
*/
279277
function wp_get_post_autosave( $post_id, $user_id = 0 ) {
280-
global $wpdb;
281-
282-
$autosave_name = $post_id . '-autosave-v1';
283-
$user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null;
284-
285-
// Construct the autosave query.
286-
$autosave_query = "
287-
SELECT *
288-
FROM $wpdb->posts
289-
WHERE post_parent = %d
290-
AND post_type = 'revision'
291-
AND post_status = 'inherit'
292-
AND post_name = %s " . $user_id_query . '
293-
ORDER BY post_date DESC
294-
LIMIT 1';
295-
296-
$autosave = $wpdb->get_results(
297-
$wpdb->prepare(
298-
$autosave_query,
299-
$post_id,
300-
$autosave_name
301-
)
278+
$args = array(
279+
'post_type' => 'revision',
280+
'post_status' => 'inherit',
281+
'post_parent' => $post_id,
282+
'name' => $post_id . '-autosave-v1',
283+
'posts_per_page' => 1,
284+
'orderby' => 'date',
285+
'order' => 'DESC',
286+
'fields' => 'ids',
287+
'no_found_rows' => true,
302288
);
303289

304-
if ( ! $autosave ) {
290+
if ( 0 !== $user_id ) {
291+
$args['author'] = $user_id;
292+
}
293+
294+
$query = new WP_Query( $args );
295+
296+
if ( ! $query->have_posts() ) {
305297
return false;
306298
}
307299

308-
return get_post( $autosave[0] );
300+
return get_post( $query->posts[0] );
309301
}
310302

311303
/**
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
/**
4+
* @group post
5+
*/
6+
class Tests_Post_wpGetPostAutosave extends WP_UnitTestCase {
7+
8+
/**
9+
* Admin user ID.
10+
*
11+
* @var int
12+
*/
13+
protected static $admin_id;
14+
15+
/**
16+
* Editor user ID.
17+
*
18+
* @var int
19+
*/
20+
protected static $editor_id;
21+
22+
/**
23+
* Post ID.
24+
*
25+
* @var int
26+
*/
27+
protected static $post_id;
28+
29+
/**
30+
* Set up before class.
31+
*
32+
* @param WP_UnitTest_Factory $factory Factory.
33+
*/
34+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
35+
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
36+
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
37+
38+
wp_set_current_user( self::$admin_id );
39+
self::$post_id = $factory->post->create( array( 'post_status' => 'publish' ) );
40+
}
41+
42+
/**
43+
* Test when no autosave exists for a post.
44+
*
45+
* @ticket 62658
46+
*/
47+
public function test_no_autosave_exists() {
48+
$autosave = wp_get_post_autosave( self::$post_id );
49+
$this->assertFalse( $autosave, 'Expected no autosave.' );
50+
}
51+
52+
/**
53+
* Test when an autosave exists for a post.
54+
*
55+
* @ticket 62658
56+
*/
57+
public function test_autosave_exists() {
58+
$autosave_id = $this->factory()->post->create(
59+
array(
60+
'post_type' => 'revision',
61+
'post_status' => 'inherit',
62+
'post_parent' => self::$post_id,
63+
'post_author' => self::$admin_id,
64+
'post_content' => 'Autosaved content',
65+
'post_name' => self::$post_id . '-autosave-v1',
66+
)
67+
);
68+
69+
$autosave = wp_get_post_autosave( self::$post_id );
70+
71+
$this->assertInstanceOf( 'WP_Post', $autosave );
72+
$this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' );
73+
$this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' );
74+
}
75+
76+
/**
77+
* Test when an autosave exists for a specific user.
78+
*
79+
* @ticket 62658
80+
*/
81+
public function test_autosave_for_specific_user() {
82+
$autosave_id = $this->factory()->post->create(
83+
array(
84+
'post_type' => 'revision',
85+
'post_status' => 'inherit',
86+
'post_parent' => self::$post_id,
87+
'post_author' => self::$editor_id,
88+
'post_content' => 'Editor-specific autosave',
89+
'post_name' => self::$post_id . '-autosave-v1',
90+
)
91+
);
92+
93+
$autosave = wp_get_post_autosave( self::$post_id, self::$editor_id );
94+
95+
$this->assertInstanceOf( 'WP_Post', $autosave );
96+
$this->assertSame( self::$editor_id, (int) $autosave->post_author, 'Post author does not match.' );
97+
$this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' );
98+
}
99+
100+
/**
101+
* Test when an autosave is updated.
102+
*
103+
* @ticket 62658
104+
*/
105+
public function test_autosave_exists_update_caches() {
106+
$autosave_id = $this->factory()->post->create(
107+
array(
108+
'post_type' => 'revision',
109+
'post_status' => 'inherit',
110+
'post_parent' => self::$post_id,
111+
'post_author' => self::$admin_id,
112+
'post_content' => 'Autosaved content',
113+
'post_name' => self::$post_id . '-autosave-v1',
114+
)
115+
);
116+
117+
$autosave = wp_get_post_autosave( self::$post_id );
118+
119+
$this->assertInstanceOf( 'WP_Post', $autosave );
120+
$this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' );
121+
$this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' );
122+
$this->assertSame( 'Autosaved content', $autosave->post_content, 'Post content does not match.' );
123+
124+
wp_update_post(
125+
array(
126+
'ID' => $autosave->ID,
127+
'post_content' => 'Autosaved content updated',
128+
)
129+
);
130+
131+
$autosave = wp_get_post_autosave( self::$post_id );
132+
$this->assertInstanceOf( 'WP_Post', $autosave );
133+
$this->assertSame( 'Autosaved content updated', $autosave->post_content, 'Post content does not match.' );
134+
}
135+
136+
/**
137+
* Test when an autosave is deleted
138+
*
139+
* @ticket 62658
140+
*/
141+
public function test_autosave_exists_and_deleted() {
142+
$autosave_id = $this->factory()->post->create(
143+
array(
144+
'post_type' => 'revision',
145+
'post_status' => 'inherit',
146+
'post_parent' => self::$post_id,
147+
'post_author' => self::$admin_id,
148+
'post_content' => 'Autosaved content',
149+
'post_name' => self::$post_id . '-autosave-v1',
150+
)
151+
);
152+
153+
$autosave = wp_get_post_autosave( self::$post_id );
154+
155+
$this->assertInstanceOf( 'WP_Post', $autosave );
156+
$this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' );
157+
$this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' );
158+
$this->assertSame( 'Autosaved content', $autosave->post_content, 'Post content does not match.' );
159+
160+
wp_delete_post( $autosave->ID, true );
161+
162+
$autosave = wp_get_post_autosave( self::$post_id );
163+
$this->assertFalse( $autosave, 'Autosave should not exist' );
164+
}
165+
}

0 commit comments

Comments
 (0)