Skip to content

Commit a400e99

Browse files
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
1 parent 3fbc7e5 commit a400e99

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,24 @@ public function get_collection_params() {
331331
'sanitize_callback' => array( $this, 'sanitize_subtypes' ),
332332
);
333333

334+
$query_params['exclude'] = array(
335+
'description' => __( 'Ensure result set excludes specific IDs.' ),
336+
'type' => 'array',
337+
'items' => array(
338+
'type' => 'integer',
339+
),
340+
'default' => array(),
341+
);
342+
343+
$query_params['include'] = array(
344+
'description' => __( 'Limit result set to specific IDs.' ),
345+
'type' => 'array',
346+
'items' => array(
347+
'type' => 'integer',
348+
),
349+
'default' => array(),
350+
);
351+
334352
return $query_params;
335353
}
336354

src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ public function search_items( WP_REST_Request $request ) {
6969
$query_args['s'] = $request['search'];
7070
}
7171

72+
if ( ! empty( $request['exclude'] ) ) {
73+
$query_args['post__not_in'] = $request['exclude'];
74+
}
75+
76+
if ( ! empty( $request['include'] ) ) {
77+
$query_args['post__in'] = $request['include'];
78+
}
79+
7280
/**
7381
* Filters the query arguments for a REST API search request.
7482
*

src/wp-includes/rest-api/search/class-wp-rest-term-search-handler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public function search_items( WP_REST_Request $request ) {
7070
$query_args['search'] = $request['search'];
7171
}
7272

73+
if ( ! empty( $request['exclude'] ) ) {
74+
$query_args['exclude'] = $request['exclude'];
75+
}
76+
77+
if ( ! empty( $request['include'] ) ) {
78+
$query_args['include'] = $request['include'];
79+
}
80+
7381
/**
7482
* Filters the query arguments for a REST API search request.
7583
*

tests/phpunit/tests/rest-api/rest-search-controller.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,4 +819,78 @@ private function get_request( $params = array(), $method = 'GET' ) {
819819
return $request;
820820
}
821821

822+
/**
823+
* @ticket 56546
824+
*/
825+
public function test_get_items_search_posts_include_ids() {
826+
$response = $this->do_request_with_params(
827+
array(
828+
'include' => array_slice( self::$my_title_post_ids, 1, 2 ),
829+
)
830+
);
831+
832+
$this->assertSame( 200, $response->get_status() );
833+
$this->assertSameSets(
834+
array( self::$my_title_post_ids[1], self::$my_title_post_ids[2] ),
835+
wp_list_pluck( $response->get_data(), 'id' )
836+
);
837+
}
838+
839+
/**
840+
* @ticket 56546
841+
*/
842+
public function test_get_items_search_posts_exclude_ids() {
843+
$response = $this->do_request_with_params(
844+
array(
845+
'exclude' => self::$my_title_page_ids,
846+
)
847+
);
848+
849+
$this->assertSame( 200, $response->get_status() );
850+
$this->assertSameSets(
851+
array_merge(
852+
self::$my_title_post_ids,
853+
self::$my_content_post_ids
854+
),
855+
wp_list_pluck( $response->get_data(), 'id' )
856+
);
857+
}
858+
859+
/**
860+
* @ticket 56546
861+
*/
862+
public function test_get_items_search_terms_include_ids() {
863+
$response = $this->do_request_with_params(
864+
array(
865+
'include' => self::$my_tag_id,
866+
'type' => 'term',
867+
)
868+
);
869+
870+
$this->assertSame( 200, $response->get_status() );
871+
$this->assertSameSets(
872+
array( self::$my_tag_id ),
873+
wp_list_pluck( $response->get_data(), 'id' )
874+
);
875+
}
876+
877+
/**
878+
* @ticket 56546
879+
*/
880+
public function test_get_items_search_terms_exclude_ids() {
881+
$response = $this->do_request_with_params(
882+
array(
883+
// "1" is the default category.
884+
'exclude' => array( 1, self::$my_tag_id ),
885+
'type' => 'term',
886+
)
887+
);
888+
889+
$this->assertSame( 200, $response->get_status() );
890+
$this->assertSameSets(
891+
array( self::$my_category_id ),
892+
wp_list_pluck( $response->get_data(), 'id' )
893+
);
894+
}
895+
822896
}

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9269,6 +9269,24 @@ mockedApiResponse.Schema = {
92699269
"type": "string"
92709270
},
92719271
"required": false
9272+
},
9273+
"exclude": {
9274+
"description": "Ensure result set excludes specific IDs.",
9275+
"type": "array",
9276+
"items": {
9277+
"type": "integer"
9278+
},
9279+
"default": [],
9280+
"required": false
9281+
},
9282+
"include": {
9283+
"description": "Limit result set to specific IDs.",
9284+
"type": "array",
9285+
"items": {
9286+
"type": "integer"
9287+
},
9288+
"default": [],
9289+
"required": false
92729290
}
92739291
}
92749292
}

0 commit comments

Comments
 (0)