REST API: Add support for searching resources by id.

This brings support for the `include` and `exclude` collection parameters to the Search Controller. This can be used to find an item by id when it's subtype is unknown.

Props kadamwhite.
Fixes #56546.


git-svn-id: https://develop.svn.wordpress.org/trunk@54123 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs 2022-09-11 21:10:31 +00:00
parent 3fbc7e5aea
commit a400e99225
5 changed files with 126 additions and 0 deletions

View File

@ -331,6 +331,24 @@ class WP_REST_Search_Controller extends WP_REST_Controller {
'sanitize_callback' => array( $this, 'sanitize_subtypes' ),
);
$query_params['exclude'] = array(
'description' => __( 'Ensure result set excludes specific IDs.' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
);
$query_params['include'] = array(
'description' => __( 'Limit result set to specific IDs.' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
);
return $query_params;
}

View File

@ -69,6 +69,14 @@ class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
$query_args['s'] = $request['search'];
}
if ( ! empty( $request['exclude'] ) ) {
$query_args['post__not_in'] = $request['exclude'];
}
if ( ! empty( $request['include'] ) ) {
$query_args['post__in'] = $request['include'];
}
/**
* Filters the query arguments for a REST API search request.
*

View File

@ -70,6 +70,14 @@ class WP_REST_Term_Search_Handler extends WP_REST_Search_Handler {
$query_args['search'] = $request['search'];
}
if ( ! empty( $request['exclude'] ) ) {
$query_args['exclude'] = $request['exclude'];
}
if ( ! empty( $request['include'] ) ) {
$query_args['include'] = $request['include'];
}
/**
* Filters the query arguments for a REST API search request.
*

View File

@ -819,4 +819,78 @@ class WP_Test_REST_Search_Controller extends WP_Test_REST_Controller_Testcase {
return $request;
}
/**
* @ticket 56546
*/
public function test_get_items_search_posts_include_ids() {
$response = $this->do_request_with_params(
array(
'include' => array_slice( self::$my_title_post_ids, 1, 2 ),
)
);
$this->assertSame( 200, $response->get_status() );
$this->assertSameSets(
array( self::$my_title_post_ids[1], self::$my_title_post_ids[2] ),
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* @ticket 56546
*/
public function test_get_items_search_posts_exclude_ids() {
$response = $this->do_request_with_params(
array(
'exclude' => self::$my_title_page_ids,
)
);
$this->assertSame( 200, $response->get_status() );
$this->assertSameSets(
array_merge(
self::$my_title_post_ids,
self::$my_content_post_ids
),
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* @ticket 56546
*/
public function test_get_items_search_terms_include_ids() {
$response = $this->do_request_with_params(
array(
'include' => self::$my_tag_id,
'type' => 'term',
)
);
$this->assertSame( 200, $response->get_status() );
$this->assertSameSets(
array( self::$my_tag_id ),
wp_list_pluck( $response->get_data(), 'id' )
);
}
/**
* @ticket 56546
*/
public function test_get_items_search_terms_exclude_ids() {
$response = $this->do_request_with_params(
array(
// "1" is the default category.
'exclude' => array( 1, self::$my_tag_id ),
'type' => 'term',
)
);
$this->assertSame( 200, $response->get_status() );
$this->assertSameSets(
array( self::$my_category_id ),
wp_list_pluck( $response->get_data(), 'id' )
);
}
}

View File

@ -9269,6 +9269,24 @@ mockedApiResponse.Schema = {
"type": "string"
},
"required": false
},
"exclude": {
"description": "Ensure result set excludes specific IDs.",
"type": "array",
"items": {
"type": "integer"
},
"default": [],
"required": false
},
"include": {
"description": "Limit result set to specific IDs.",
"type": "array",
"items": {
"type": "integer"
},
"default": [],
"required": false
}
}
}