-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-adapter.php
More file actions
708 lines (643 loc) · 26 KB
/
class-adapter.php
File metadata and controls
708 lines (643 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
<?php
/**
* Elasticsearch Extensions Adapters: Adapter Abstract Class
*
* @package Elasticsearch_Extensions
*/
namespace Elasticsearch_Extensions\Adapters;
use Elasticsearch_Extensions\Aggregations\Aggregation;
use Elasticsearch_Extensions\Aggregations\CAP_Author;
use Elasticsearch_Extensions\Aggregations\Custom_Date_Range;
use Elasticsearch_Extensions\Aggregations\Post_Date;
use Elasticsearch_Extensions\Aggregations\Post_Meta;
use Elasticsearch_Extensions\Aggregations\Post_Type;
use Elasticsearch_Extensions\Aggregations\Relative_Date;
use Elasticsearch_Extensions\Aggregations\Taxonomy;
use Elasticsearch_Extensions\Aggregations\Term;
use Elasticsearch_Extensions\DSL;
use Elasticsearch_Extensions\Interfaces\Hookable;
/**
* An abstract class that establishes base functionality and sets requirements
* for implementing classes.
*
* @package Elasticsearch_Extensions
*/
abstract class Adapter implements Hookable {
/**
* Stores aggregation data from the Elasticsearch response.
*
* @var Aggregation[]
*/
private array $aggregations = [];
/**
* Whether to allow empty searches (no keyword set).
*
* @var bool
*/
private bool $allow_empty_search = false;
/**
* Whether to enable phrase matching in queries.
*
* Under the hood, this uses a multi_match query with the type set to
* "phrase" for each phrase matched part of the search string. This
* allows for more precise matching of phrases in the search string. For
* example, when active, a search for "foo bar" will match "foo bar" but not "foo
* baz bar".
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
*
* @var bool
*/
private bool $enable_phrase_matching = false;
/**
* Whether to index search suggestions.
*
* @var bool
*/
private bool $enable_search_suggestions = false;
/**
* Whether to make search suggestions available over the REST API.
*
* @var bool
*/
private bool $show_search_suggestions_in_rest = true;
/**
* Holds an instance of the DSL class with the field map from this adapter
* injected into it.
*
* @var DSL
*/
protected DSL $dsl;
/**
* An optional array of meta fields to restrict search to.
*
* @var string[]
*/
private array $restricted_post_meta = [];
/**
* An optional array of post types to restrict search to.
*
* @var string[]
*/
private array $restricted_post_types = [];
/**
* An optional array of indexable post statuses to restrict to.
*
* @var string[]
*/
private array $restricted_post_statuses = [];
/**
* An optional array of taxonomies to restrict search to.
*
* @var string[]
*/
private array $restricted_taxonomies = [];
/**
* An optional array of post types to restrict search suggestions to.
*
* @var string[]
*/
private array $restricted_search_suggestions_post_types = [];
/**
* Constructor. Sets up the DSL object with this adapter's field map.
*/
public function __construct() {
$this->dsl = new DSL( $this->get_field_map() );
}
/**
* Adds an Aggregation to the list of active aggregations.
*
* @param Aggregation $aggregation The aggregation to add.
*/
public function add_aggregation( Aggregation $aggregation ): void {
$this->aggregations[ $aggregation->get_query_var() ] = $aggregation;
}
/**
* Adds a new Co-Authors Plus author aggregation to the list of active aggregations.
*
* @param array{label?: string, order?: 'ASC'|'DESC', orderby?: 'count'|'display_name'|'first_name'|'key'|'label'|'last_name', query_var?: string, relation?: 'AND'|'OR', term_field?: string} $args {
* Optional. Arguments to pass to the adapter's aggregation configuration.
*
* @type string $label Optional. The human-readable name for this aggregation. Defaults to 'Author'.
* @type string $order Optional. How to sort by the `orderby` field. Valid options are 'ASC', 'DESC'.
* Defaults to 'DESC'.
* @type string $orderby Optional. The field to order results by. Valid options are 'count', 'display_name',
* 'first_name', 'key', 'label', 'last_name'. Defaults to 'count'.
* @type string $query_var Optional. The query var to use in the URL. Accepts any URL-safe string. Defaults to
* 'taxonomy_author'.
* @type string $relation Optional. The logical relationship between each selected author when there is more
* than one. Valid options are 'AND', 'OR'. Defaults to 'AND'.
* @type string $term_field Optional. The term field to use in the DSL for this aggregation. Defaults to the
* value of the taxonomy name for the 'author' taxonomy, as looked up in the DSL map.
* }
*/
public function add_cap_author_aggregation( array $args = [] ): void {
$this->add_aggregation( new CAP_Author( $this->dsl, $args ) );
}
/**
* Adds a new custom date range aggregation to the list of active aggregations.
*
* @param array{label?: string, query_var?: string} $args {
* Optional. Arguments to pass to the adapter's aggregation configuration.
*
* @type string $label Optional. The human-readable name for this aggregation. Defaults to 'Custom Date
* Range'.
* @type string $query_var Optional. The query var to use in the URL. Accepts any URL-safe string. Defaults to
* 'custom_date_range'.
* }
*/
public function add_custom_date_range_aggregation( array $args = [] ): void {
$this->add_aggregation( new Custom_Date_Range( $this->dsl, $args ) );
}
/**
* A callback to filter es args.
* Adds phrase matching to the request args if it is enabled.
*
* @param array $es_args The request args to be filtered.
* @return array The filtered request args.
*/
public function add_phrase_matching_to_es_args( array $es_args ): array {
if ( ! $this->get_enable_phrase_matching() ) {
return $es_args;
}
// Get search query from query vars.
$search = get_query_var( 's' );
// Bail early if this isn't a search.
if ( empty( $search ) ) {
return $es_args;
}
// Break down the search string into the desired "phrase matched" parts.
$phrase_match_delineator = '"';
if ( ! preg_match_all( '/' . $phrase_match_delineator . '(.*?)' . $phrase_match_delineator . '/', $search, $matches ) ) {
return $es_args;
}
// Get the search query without the "phrase matched" parts.
$unmatched = implode(
' ',
array_filter(
explode( ' ', preg_replace( '/' . $phrase_match_delineator . '.*?' . $phrase_match_delineator . '/', '', $search ) )
)
);
// Replace the main multi_match query with the "unmatched" string.
$es_arg = $this->find_multimatch_query( $es_args );
if ( ! empty( $es_arg['multi_match']['query'] ) ) {
if ( ! empty( $unmatched ) ) {
$es_arg['multi_match']['query'] = $unmatched;
} else {
unset( $es_arg );
}
}
/**
* Filters the list of multimatch fields.
*
* @since 0.1.0
*
* @param array $default_multi_match_fields The array of multimatch fields with weighting to be included in phrase matching queries.
*/
$default_multi_match_fields = apply_filters(
'elasticsearch_extensions_phrase_match_multimatch_fields',
[
'post_title^3',
'post_excerpt^2',
'post_content',
'post_author.display_name',
'terms.author.name',
]
);
// Loop over phrase matches and add each.
foreach ( $matches[1] as $query ) {
$es_args['query']['function_score']['query']['bool']['must'][] = [
'multi_match' => [
'fields' => $default_multi_match_fields,
'query' => $query,
'type' => 'phrase',
],
];
}
return $es_args;
}
/**
* Adds a new post date aggregation to the list of active aggregations.
*
* @param array{interval?: 'year'|'quarter'|'month'|'week'|'day'|'hour'|'minute', label?: string, order?: 'ASC'|'DESC', orderby?: 'count'|'key'|'label', query_var?: string} $args {
* Optional. Arguments to pass to the adapter's aggregation configuration.
*
* @type string $interval Optional. The unit of time to aggregate results by. Valid options are 'year',
* 'quarter', 'month', 'week', 'day', 'hour', 'minute'. Defaults to 'year'.
* @type string $label Optional. The human-readable name for this aggregation. Defaults to 'Date'.
* @type string $order Optional. How to sort by the `orderby` field. Valid options are 'ASC', 'DESC'.
* Defaults to 'DESC'.
* @type string $orderby Optional. The field to order results by. Valid options are 'count', 'key', 'label'.
* Defaults to 'count'.
* @type string $query_var Optional. The query var to use in the URL. Accepts any URL-safe string. Defaults to
* 'post_date'.
* }
*/
public function add_post_date_aggregation( array $args = [] ): void {
$this->add_aggregation( new Post_Date( $this->dsl, $args ) );
}
/**
* Adds a new post meta aggregation to the list of active aggregations.
*
* @param string $meta_key The meta key to aggregate on.
* @param array{data_type?: string, label?: string, order?: 'ASC'|'DESC', orderby?: 'count'|'key'|'label', query_var?: string, relation?: 'AND'|'OR', term_field?: string} $args {
* Optional. Arguments to pass to the adapter's aggregation configuration.
*
* @type string $data_type Optional. The data type of the meta key, if the meta key is indexed using multiple
* data types (e.g., 'long'). Defaults to empty and uses the "raw" postmeta value.
* @type string $label Optional. The human-readable name for this aggregation. Defaults to a halfhearted
* attempt at turning the meta key into a title case string.
* @type string $order Optional. How to sort by the `orderby` field. Valid options are 'ASC', 'DESC'.
* Defaults to 'DESC'.
* @type string $orderby Optional. The field to order results by. Valid options are 'count', 'key', 'label'.
* Defaults to 'count'.
* @type string $query_var Optional. The query var to use in the URL. Accepts any URL-safe string. Defaults to
* 'post_meta_%s' where %s is the meta key.
* @type string $relation Optional. The logical relationship between each selected meta value when there is
* more than one. Valid options are 'AND', 'OR'. Defaults to 'AND'.
* @type string $term_field Optional. The term field to use in the DSL for this aggregation. Defaults to the
* value of the post meta key, as looked up in the DSL map.
* }
*/
public function add_post_meta_aggregation( string $meta_key, array $args = [] ): void {
$this->add_aggregation( new Post_Meta( $this->dsl, wp_parse_args( $args, [ 'meta_key' => $meta_key ] ) ) );
}
/**
* Adds a new post type aggregation to the list of active aggregations.
*
* @param array{label?: string, order?: 'ASC'|'DESC', orderby?: 'count'|'key'|'label', query_var?: string, relation?: 'AND'|'OR', term_field?: string} $args {
* Optional. Arguments to pass to the adapter's aggregation configuration.
*
* @type string $label Optional. The human-readable name for this aggregation. Defaults to 'Content Type'.
* @type string $order Optional. How to sort by the `orderby` field. Valid options are 'ASC', 'DESC'.
* Defaults to 'DESC'.
* @type string $orderby Optional. The field to order results by. Valid options are 'count', 'key', 'label'.
* Defaults to 'count'.
* @type string $query_var Optional. The query var to use in the URL. Accepts any URL-safe string. Defaults to
* 'post_type'.
* @type string $relation Optional. The logical relationship between each selected author when there is more
* than one. Valid options are 'AND', 'OR'. Defaults to 'AND'.
* @type string $term_field Optional. The term field to use in the DSL for this aggregation. Defaults to the
* 'post_type' field, as looked up in the DSL map.
* }
*/
public function add_post_type_aggregation( array $args = [] ): void {
$this->add_aggregation( new Post_Type( $this->dsl, $args ) );
}
/**
* Adds a new relative date aggregation to the list of active aggregations.
*
* @param array{intervals?: int[], label?: string, order?: 'ASC'|'DESC', orderby?: 'count'|'key'|'label', query_var?: string} $args {
* Optional. Arguments to pass to the adapter's aggregation configuration.
*
* @type int[] $intervals Optional. The number of days prior to the current date to include in each bucket.
* Accepts an array of integers. Defaults to `[7, 30, 90]`.
* @type string $label Optional. The human-readable name for this aggregation. Defaults to 'Relative Date'.
* @type string $order Optional. How to sort by the `orderby` field. Valid options are 'ASC', 'DESC'.
* Defaults to 'DESC'.
* @type string $orderby Optional. The field to order results by. Valid options are 'count', 'key', 'label'.
* Defaults to 'count'.
* @type string $query_var Optional. The query var to use in the URL. Accepts any URL-safe string. Defaults to
* 'relative_date'.
* }
*/
public function add_relative_date_aggregation( array $args = [] ): void {
$this->add_aggregation( new Relative_Date( $this->dsl, $args ) );
}
/**
* Adds a new taxonomy aggregation to the list of active aggregations.
*
* @param string $taxonomy The taxonomy slug for which to enable an aggregation.
* @param array{label?: string, order?: 'ASC'|'DESC', orderby?: 'count'|'key'|'label', query_var?: string, relation?: 'AND'|'OR', term_field?: string} $args {
* Optional. Arguments to pass to the adapter's aggregation configuration.
*
* @type string $label Optional. The human-readable name for this aggregation. Defaults to the singular
* name of the taxonomy (e.g., 'Category').
* @type string $order Optional. How to sort by the `orderby` field. Valid options are 'ASC', 'DESC'.
* Defaults to 'DESC'.
* @type string $orderby Optional. The field to order results by. Valid options are 'count', 'key', 'label'.
* Defaults to 'count'.
* @type string $query_var Optional. The query var to use in the URL. Accepts any URL-safe string. Defaults to
* 'taxonomy_%s' where %s is the taxonomy slug.
* @type string $relation Optional. The logical relationship between each term when there is more than one.
* Valid options are 'AND', 'OR'. Defaults to 'AND'.
* @type string $term_field Optional. The term field to use in the DSL for this aggregation. Defaults to the
* taxonomy's slug field, as looked up in the DSL map.
* }
*/
public function add_taxonomy_aggregation( string $taxonomy, array $args = [] ): void {
$this->add_aggregation( new Taxonomy( $this->dsl, wp_parse_args( $args, [ 'taxonomy' => $taxonomy ] ) ) );
}
/**
* Adds a new generic term aggregation to the list of active aggregations.
*
* @param string $label The human-readable label for this aggregation.
* @param string $term_field The term field to aggregate on.
* @param string $query_var The query var to use for this aggregation for filters on the front-end.
* @param array{label?: string, order?: 'ASC'|'DESC', orderby?: 'count'|'key'|'label', relation?: 'AND'|'OR'} $args {
* Optional. Arguments to pass to the adapter's aggregation configuration.
*
* @type string $order Optional. How to sort by the `orderby` field. Valid options are 'ASC', 'DESC'.
* Defaults to 'DESC'.
* @type string $orderby Optional. The field to order results by. Valid options are 'count', 'key', 'label'.
* Defaults to 'count'.
* @type string $relation Optional. The logical relationship between each term when there is more than one.
* Valid options are 'AND', 'OR'. Defaults to 'AND'.
* }
*/
public function add_term_aggregation( string $label, string $term_field, string $query_var, array $args = [] ): void {
$this->add_aggregation(
new Term(
$this->dsl,
wp_parse_args(
$args,
[
'label' => $label,
'query_var' => $query_var,
'term_field' => $this->dsl->map_field( $term_field ),
]
)
)
);
}
/**
* Finds the first multi_match query and returns a reference to it.
*
* @param array $es_args Elasticsearch DSL to filter.
*
* @return ?array A reference to the multi_match array on success, null on failure.
*/
protected function find_multimatch_query( array $es_args ): ?array {
// Skip if this is the wrong type of query.
if (
empty( $es_args['query']['function_score']['query']['bool']['must'] )
|| ! is_array( $es_args['query']['function_score']['query']['bool']['must'] )
) {
return null;
}
// Loop over the query arguments to try to find multi_match queries.
foreach ( $es_args['query']['function_score']['query']['bool']['must'] as &$es_arg ) {
// If this is a multi_match query, return a reference to it.
if ( ! empty( $es_arg['multi_match'] ) ) {
return $es_arg;
}
}
return null;
}
/**
* Get an aggregation by its label.
*
* @param string $label Aggregation label.
*
* @return ?Aggregation The aggregation, if found, or null if not.
*/
public function get_aggregation_by_label( string $label ): ?Aggregation {
foreach ( $this->aggregations as $aggregation ) {
if ( $label === $aggregation->get_label() ) {
return $aggregation;
}
}
return null;
}
/**
* Get an aggregation by its query var.
*
* @param string $query_var Aggregation query var.
*
* @return ?Aggregation The aggregation, if found, or null if not.
*/
public function get_aggregation_by_query_var( string $query_var ): ?Aggregation {
foreach ( $this->aggregations as $aggregation ) {
if ( $query_var === $aggregation->get_query_var() ) {
return $aggregation;
}
}
return null;
}
/**
* Get the aggregation configuration.
*
* @return Aggregation[]
*/
public function get_aggregations(): array {
return $this->aggregations;
}
/**
* Gets the value for allow_empty_search.
*
* @return bool Whether to allow empty search or not.
*/
public function get_allow_empty_search(): bool {
return $this->allow_empty_search;
}
/**
* Gets the value for enable_phrase_matching.
*
* @return bool Whether phrase matching is enabled.
*/
public function get_enable_phrase_matching(): bool {
return $this->enable_phrase_matching;
}
/**
* Gets the value for enable_search_suggestions.
*
* @return bool Whether search suggestions are enabled.
*/
public function get_enable_search_suggestions(): bool {
return $this->enable_search_suggestions;
}
/**
* Returns a map of generic field names and types to the specific field
* path used in the mapping of the Elasticsearch plugin that is in use.
* Implementing classes need to provide this map, as it will be different
* between each plugin's Elasticsearch implementation, and use the result
* of this function when initializing the DSL class in the constructor.
*
* @return array<string, string> The field map.
*/
abstract protected function get_field_map(): array;
/**
* Gets the list of restricted meta.
*
* @return string[] The list of restricted meta slugs.
*/
protected function get_restricted_post_meta(): array {
return $this->restricted_post_meta;
}
/**
* Gets the list of restricted post types.
*
* @return string[] The list of restricted post type slugs.
*/
protected function get_restricted_post_types(): array {
return $this->restricted_post_types;
}
/**
* Gets the list of restricted post statuses.
*
* @return string[] The list of restricted post statuses.
*/
protected function get_restricted_post_statuses(): array {
return $this->restricted_post_statuses;
}
/**
* Gets the list of restricted taxonomies.
*
* @return string[] The list of restricted taxonomy slugs.
*/
protected function get_restricted_taxonomies(): array {
return $this->restricted_taxonomies;
}
/**
* Gets the list of restricted post types for search suggestions.
*
* @return string[] The list of restricted post type slugs.
*/
protected function get_restricted_search_suggestions_post_types(): array {
return $this->restricted_search_suggestions_post_types;
}
/**
* Returns a list of searchable post types. Defaults come from the filter
* function used in the adapter. Allows for applying different logic
* depending on the context (e.g., main search vs. custom search
* interfaces).
*
* @param string[] $post_types The default list of post type slugs from the adapter.
*
* @return string[] An array of searchable post type slugs.
*/
protected function get_searchable_post_types( array $post_types ): array {
/**
* Filters the list of searchable post types.
*
* Defaults to the list of searchable post type slugs from the adapter.
*
* @since 0.1.0
*
* @param array $post_types The array of post type slugs to include in search.
*/
return apply_filters( 'elasticsearch_extensions_searchable_post_types', $post_types );
}
/**
* Returns whether support for a REST API search handler for suggestions is enabled.
*
* @return bool
*/
public function is_show_search_suggestions_in_rest_enabled(): bool {
return $this->get_enable_search_suggestions() && $this->show_search_suggestions_in_rest;
}
/**
* Parses aggregations from an aggregations object in an Elasticsearch
* response into the loaded aggregations.
*
* @param array<string, mixed> $aggregations Aggregations from the Elasticsearch response.
*/
protected function parse_aggregations( array $aggregations ): void {
foreach ( $aggregations as $aggregation_key => $aggregation ) {
if ( isset( $this->aggregations[ $aggregation_key ] ) ) {
$this->aggregations[ $aggregation_key ]->parse_buckets( $aggregation['buckets'] ?? [] );
}
}
}
/**
* Query Elasticsearch directly. Must be implemented in child classes for specific adapters.
*
* @param array $es_args Arguments to pass to the Elasticsearch server.
*
* @return array The response from the Elasticsearch server.
*/
abstract public function search( array $es_args ): array;
/**
* Suggest posts that match the given search term.
*
* @param string $search Search string.
* @param array{subtypes?: string[], page?: int, per_page?: int, include?: int[], exclude?: int[]} $args {
* Optional. An array of arguments.
*
* @type string[] $subtypes Limit suggestions to this subset of all post
* types that support search suggestions.
* @type int $page Page of results.
* @type int $per_page Results per page. Default 10.
* @type int[] $include Search within these post IDs.
* @type int[] $exclude Exclude these post IDs from results.
* }
* @return int[] Post IDs in this page of results and total number of results.
* @phpstan-return array{0: int[], 1: int}
*/
public function query_post_suggestions( string $search, array $args = [] ): array {
return [ [], 0 ];
}
/**
* Restricts indexable post meta to the provided list.
*
* @param string[] $post_meta The array of meta fields to restrict to.
*/
public function restrict_post_meta( array $post_meta ): void {
$this->restricted_post_meta = $post_meta;
}
/**
* Restricts indexable post types to the provided list.
*
* @param string[] $post_types The array of post types to restrict to.
*/
public function restrict_post_types( array $post_types ): void {
$this->restricted_post_types = $post_types;
}
/**
* Restricts indexable post statuses to the provided list.
*
* @param string[] $post_statuses The array of indexabled post statuses to restrict to.
*/
public function restrict_post_statuses( array $post_statuses ): void {
$this->restricted_post_statuses = $post_statuses;
}
/**
* Restricts searchable taxonomies to the provided list.
*
* @param string[] $taxonomies The array of taxonomies to restrict search to.
*/
public function restrict_taxonomies( array $taxonomies ): void {
$this->restricted_taxonomies = $taxonomies;
}
/**
* Restricts post types indexed for search suggestions to the provided list.
*
* @param string[] $post_types The array of post types to restrict to.
*/
public function restrict_search_suggestions_post_types( array $post_types ): void {
$this->restricted_search_suggestions_post_types = $post_types;
}
/**
* Sets the value for allow_empty_search.
*
* @param bool $allow_empty_search Whether to allow empty search or not.
*/
public function set_allow_empty_search( bool $allow_empty_search ): void {
$this->allow_empty_search = $allow_empty_search;
}
/**
* Enables phrase matching for the main search query.
*
* @param bool $enable_phrase_matching Whether to enable phrase matching.
*/
public function set_enable_phrase_matching( bool $enable_phrase_matching ): void {
$this->enable_phrase_matching = $enable_phrase_matching;
}
/**
* Sets the value for enable_search_suggestions.
*
* @param bool $enable_search_suggestions Whether to enable search suggestions.
*/
public function set_enable_search_suggestions( bool $enable_search_suggestions ): void {
$this->enable_search_suggestions = $enable_search_suggestions;
}
/**
* Sets the value for show_search_suggestions_in_rest.
*
* @param bool $show_in_rest Whether to make search suggestions available over the REST API.
*/
public function set_show_search_suggestions_in_rest( bool $show_in_rest ): void {
$this->show_search_suggestions_in_rest = $show_in_rest;
}
}