mirror of
git://develop.git.wordpress.org/
synced 2025-04-13 16:42:00 +02:00
REST API: Improve post cache priming in WP_REST_Post_Search_Handler
class.
In the `WP_REST_Post_Search_Handler` class, ensure that post, post meta and term caches are correctly primed when performing a search. Props furi3r, spacedmonkey, TimothyBlynJacobs, audrasjb, peterwilsoncc. Fixes #55674. git-svn-id: https://develop.svn.wordpress.org/trunk@53485 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
8903b4a981
commit
b855e63693
@ -58,12 +58,11 @@ class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
|
||||
}
|
||||
|
||||
$query_args = array(
|
||||
'post_type' => $post_types,
|
||||
'post_status' => 'publish',
|
||||
'paged' => (int) $request['page'],
|
||||
'posts_per_page' => (int) $request['per_page'],
|
||||
'ignore_sticky_posts' => true,
|
||||
'fields' => 'ids',
|
||||
'post_type' => $post_types,
|
||||
'post_status' => 'publish',
|
||||
'paged' => (int) $request['page'],
|
||||
'posts_per_page' => (int) $request['per_page'],
|
||||
'ignore_sticky_posts' => true,
|
||||
);
|
||||
|
||||
if ( ! empty( $request['search'] ) ) {
|
||||
@ -83,7 +82,9 @@ class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
|
||||
$query_args = apply_filters( 'rest_post_search_query', $query_args, $request );
|
||||
|
||||
$query = new WP_Query();
|
||||
$found_ids = $query->query( $query_args );
|
||||
$posts = $query->query( $query_args );
|
||||
// Querying the whole post object will warm the object cache, avoiding an extra query per result.
|
||||
$found_ids = wp_list_pluck( $posts, 'ID' );
|
||||
$total = $query->found_posts;
|
||||
|
||||
return array(
|
||||
|
@ -332,6 +332,36 @@ class WP_Test_REST_Search_Controller extends WP_Test_REST_Controller_Testcase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55674
|
||||
*/
|
||||
public function test_get_items_search_prime_ids() {
|
||||
$action = new MockAction();
|
||||
add_filter( 'query', array( $action, 'filter' ), 10, 2 );
|
||||
|
||||
$query_args = array(
|
||||
'per_page' => 100,
|
||||
'search' => 'foocontent',
|
||||
);
|
||||
$response = $this->do_request_with_params( $query_args );
|
||||
$this->assertSame( 200, $response->get_status(), 'Request Status Response is not 200.' );
|
||||
|
||||
$ids = wp_list_pluck( $response->get_data(), 'id' );
|
||||
$this->assertSameSets( self::$my_content_post_ids, $ids, 'Query result posts ids do not match with expected ones.' );
|
||||
|
||||
$args = $action->get_args();
|
||||
$primed_query_found = false;
|
||||
foreach ( $args as $arg ) {
|
||||
// Primed query will use WHERE ID IN clause.
|
||||
if ( str_contains( $arg[0], 'WHERE ID IN (' . implode( ',', $ids ) ) ) {
|
||||
$primed_query_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertTrue( $primed_query_found, 'Prime query was not executed.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieving a single item isn't possible.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user