Skip to content

Commit

Permalink
Use search_semantics enum parameter instead of exact_search boolean p…
Browse files Browse the repository at this point in the history
…arameter.
  • Loading branch information
felixarntz committed Sep 17, 2024
1 parent 9bc7287 commit 15ee682
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ public function get_items( $request ) {
$parameter_mappings = array(
'author' => 'author__in',
'author_exclude' => 'author__not_in',
'exact_search' => 'exact',
'exclude' => 'post__not_in',
'include' => 'post__in',
'menu_order' => 'menu_order',
Expand Down Expand Up @@ -338,6 +337,13 @@ public function get_items( $request ) {
}
}

if (
isset( $registered['search_semantics'], $request['search_semantics'] )
&& 'exact' === $request['search_semantics']
) {
$args['exact'] = true;
}

$args = $this->prepare_tax_query( $args, $request );

// Force the post_type argument, since it's not a user input variable.
Expand Down Expand Up @@ -2887,9 +2893,10 @@ public function get_collection_params() {
);
}

$query_params['exact_search'] = array(
'description' => __( 'Use exact search instead of full search.' ),
'type' => 'boolean',
$query_params['search_semantics'] = array(
'description' => __( 'How to interpret the search input.' ),
'type' => 'string',
'enum' => array( 'exact' ),
);

$query_params['offset'] = array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public function test_registered_query_params() {
'author_exclude',
'before',
'context',
'exact_search',
'exclude',
'include',
'media_type',
Expand All @@ -230,6 +229,7 @@ public function test_registered_query_params() {
'per_page',
'search',
'search_columns',
'search_semantics',
'slug',
'status',
),
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/rest-api/rest-pages-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public function test_registered_query_params() {
'author_exclude',
'before',
'context',
'exact_search',
'exclude',
'include',
'menu_order',
Expand All @@ -86,6 +85,7 @@ public function test_registered_query_params() {
'per_page',
'search',
'search_columns',
'search_semantics',
'slug',
'status',
),
Expand Down
12 changes: 7 additions & 5 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ public function test_registered_query_params() {
'categories',
'categories_exclude',
'context',
'exact_search',
'exclude',
'include',
'modified_after',
Expand All @@ -207,6 +206,7 @@ public function test_registered_query_params() {
'per_page',
'search',
'search_columns',
'search_semantics',
'slug',
'status',
'sticky',
Expand Down Expand Up @@ -790,10 +790,12 @@ public function test_get_items_exact_search( $search_term, $exact_search, $expec
)
);

$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request['search'] = $search_term;
$request['exact_search'] = $exact_search;
$response = rest_get_server()->dispatch( $request );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request['search'] = $search_term;
if ( $exact_search ) {
$request['search_semantics'] = 'exact';
}
$response = rest_get_server()->dispatch( $request );
$this->assertCount( $expected, $response->get_data() );
}

Expand Down
72 changes: 48 additions & 24 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,12 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
Expand Down Expand Up @@ -1724,9 +1727,12 @@ mockedApiResponse.Schema = {
"type": "integer",
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
Expand Down Expand Up @@ -2830,9 +2836,12 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
Expand Down Expand Up @@ -3586,9 +3595,12 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
Expand Down Expand Up @@ -4402,9 +4414,12 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
Expand Down Expand Up @@ -7020,9 +7035,12 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
Expand Down Expand Up @@ -7842,9 +7860,12 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
Expand Down Expand Up @@ -8052,9 +8073,12 @@ mockedApiResponse.Schema = {
"default": [],
"required": false
},
"exact_search": {
"description": "Use exact search instead of full search.",
"type": "boolean",
"search_semantics": {
"description": "How to interpret the search input.",
"type": "string",
"enum": [
"exact"
],
"required": false
},
"offset": {
Expand Down

0 comments on commit 15ee682

Please sign in to comment.