From fc4485c17faf4ffa93bc878308e8eca04ff75041 Mon Sep 17 00:00:00 2001 From: TimothyBlynJacobs Date: Sun, 11 Sep 2022 21:12:11 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@54123 git-svn-id: http://core.svn.wordpress.org/trunk@53682 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-search-controller.php | 18 ++++++++++++++++++ .../class-wp-rest-post-search-handler.php | 8 ++++++++ .../class-wp-rest-term-search-handler.php | 8 ++++++++ wp-includes/version.php | 2 +- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php index 76021ff0b0..93502d60f4 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php @@ -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; } diff --git a/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php b/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php index 5d20661b05..5809245acf 100644 --- a/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php +++ b/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php @@ -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. * diff --git a/wp-includes/rest-api/search/class-wp-rest-term-search-handler.php b/wp-includes/rest-api/search/class-wp-rest-term-search-handler.php index af9b2e5663..eed35e7470 100644 --- a/wp-includes/rest-api/search/class-wp-rest-term-search-handler.php +++ b/wp-includes/rest-api/search/class-wp-rest-term-search-handler.php @@ -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. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 01b7aebdec..1bb1b9750c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-54122'; +$wp_version = '6.1-alpha-54123'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.