Completed
Push — branch-8.5-built ( c6fe17...616e5d )
by Jeremy
04:28
created

Jetpack_Search   F

Complexity

Total Complexity 249

Size/Duplication

Total Lines 2003
Duplicated Lines 5.09 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 249
lcom 2
cbo 7
dl 102
loc 2003
rs 0.8
c 0
b 0
f 0

49 Methods

Rating   Name   Duplication   Size   Complexity  
A update_search_results_aggregations() 0 14 3
A get_search_result() 0 7 6
A filter__add_date_filter_to_query() 0 32 4
A __construct() 0 2 1
A __clone() 0 3 1
A __wakeup() 0 3 1
A instance() 0 14 3
A setup() 0 26 4
A load_php() 0 3 1
A base_load_php() 0 5 1
A init_hooks() 18 18 2
A is_search_supported() 0 6 2
A has_vip_index() 0 3 2
A store_query_failure() 0 4 1
A print_query_failure() 0 10 2
A store_last_query_info() 0 4 1
A print_query_success() 0 16 3
A get_last_query_info() 0 3 2
A get_last_query_failure_info() 0 3 2
A are_filters_by_widget_disabled() 0 3 1
A set_filters_from_widgets() 0 11 3
A maybe_add_post_type_as_var() 0 10 6
C search() 20 111 14
B filter__posts_pre_query() 0 43 5
C do_search() 0 110 14
B get_es_wp_query_terms_for_query() 3 32 10
B get_es_wp_query_post_type_for_query() 0 38 7
A action__widgets_init() 0 5 1
F convert_wp_es_to_es_args() 32 392 35
A add_aggregations_to_es_query_builder() 0 20 5
A add_taxonomy_aggregation_to_es_query_builder() 0 27 3
A add_post_type_aggregation_to_es_query_builder() 0 11 1
A add_date_histogram_aggregation_to_es_query_builder() 0 19 4
A and_es_filters() 0 15 4
A set_filters() 0 8 3
A set_facets() 0 5 1
A get_search_aggregations_results() 0 11 3
A get_search_facets() 0 5 1
F get_filters() 29 261 48
A get_search_facet_data() 0 5 1
B get_active_filter_buckets() 0 21 8
A get_current_filters() 0 5 1
A get_taxonomy_query_var() 0 19 3
A fix_aggregation_ordering() 0 15 5
A track_widget_updates() 0 17 3
B move_search_widgets_to_inactive() 0 34 10
A should_handle_query() 0 13 2
A _get_caret_boosted_fields() 0 7 2
A _apply_boosts_multiplier() 0 17 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Search, and based on these observations, apply Extract Interface, too.

1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
/**
4
 * Jetpack Search: Main Jetpack_Search class
5
 *
6
 * @package    Jetpack
7
 * @subpackage Jetpack Search
8
 * @since      5.0.0
9
 */
10
11
use Automattic\Jetpack\Connection\Client;
12
13
require_once dirname( __FILE__ ) . '/class-jetpack-search-options.php';
14
15
/**
16
 * The main class for the Jetpack Search module.
17
 *
18
 * @since 5.0.0
19
 */
20
class Jetpack_Search {
21
22
	/**
23
	 * The number of found posts.
24
	 *
25
	 * @since 5.0.0
26
	 *
27
	 * @var int
28
	 */
29
	protected $found_posts = 0;
30
31
	/**
32
	 * The search result, as returned by the WordPress.com REST API.
33
	 *
34
	 * @since 5.0.0
35
	 *
36
	 * @var array
37
	 */
38
	protected $search_result;
39
40
	/**
41
	 * This site's blog ID on WordPress.com.
42
	 *
43
	 * @since 5.0.0
44
	 *
45
	 * @var int
46
	 */
47
	protected $jetpack_blog_id;
48
49
	/**
50
	 * The Elasticsearch aggregations (filters).
51
	 *
52
	 * @since 5.0.0
53
	 *
54
	 * @var array
55
	 */
56
	protected $aggregations = array();
57
58
	/**
59
	 * The maximum number of aggregations allowed.
60
	 *
61
	 * @since 5.0.0
62
	 *
63
	 * @var int
64
	 */
65
	protected $max_aggregations_count = 100;
66
67
	/**
68
	 * Statistics about the last Elasticsearch query.
69
	 *
70
	 * @since 5.6.0
71
	 *
72
	 * @var array
73
	 */
74
	protected $last_query_info = array();
75
76
	/**
77
	 * Statistics about the last Elasticsearch query failure.
78
	 *
79
	 * @since 5.6.0
80
	 *
81
	 * @var array
82
	 */
83
	protected $last_query_failure_info = array();
84
85
	/**
86
	 * The singleton instance of this class.
87
	 *
88
	 * @since 5.0.0
89
	 *
90
	 * @var Jetpack_Search
91
	 */
92
	protected static $instance;
93
94
	/**
95
	 * Languages with custom analyzers. Other languages are supported, but are analyzed with the default analyzer.
96
	 *
97
	 * @since 5.0.0
98
	 *
99
	 * @var array
100
	 */
101
	public static $analyzed_langs = array( 'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'eu', 'fa', 'fi', 'fr', 'he', 'hi', 'hu', 'hy', 'id', 'it', 'ja', 'ko', 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh' );
102
103
	/**
104
	 * Jetpack_Search constructor.
105
	 *
106
	 * @since 5.0.0
107
	 *
108
	 * Doesn't do anything. This class needs to be initialized via the instance() method instead.
109
	 */
110
	protected function __construct() {
111
	}
112
113
	/**
114
	 * Prevent __clone()'ing of this class.
115
	 *
116
	 * @since 5.0.0
117
	 */
118
	public function __clone() {
119
		wp_die( "Please don't __clone Jetpack_Search" );
120
	}
121
122
	/**
123
	 * Prevent __wakeup()'ing of this class.
124
	 *
125
	 * @since 5.0.0
126
	 */
127
	public function __wakeup() {
128
		wp_die( "Please don't __wakeup Jetpack_Search" );
129
	}
130
131
	/**
132
	 * Get singleton instance of Jetpack_Search.
133
	 *
134
	 * Instantiates and sets up a new instance if needed, or returns the singleton.
135
	 *
136
	 * @since 5.0.0
137
	 *
138
	 * @return Jetpack_Search The Jetpack_Search singleton.
139
	 */
140
	public static function instance() {
141
		if ( ! isset( self::$instance ) ) {
142
			if ( Jetpack_Search_Options::is_instant_enabled() ) {
143
				require_once dirname( __FILE__ ) . '/class-jetpack-instant-search.php';
144
				self::$instance = new Jetpack_Instant_Search();
145
			} else {
146
				self::$instance = new Jetpack_Search();
147
			}
148
149
			self::$instance->setup();
150
		}
151
152
		return self::$instance;
153
	}
154
155
	/**
156
	 * Perform various setup tasks for the class.
157
	 *
158
	 * Checks various pre-requisites and adds hooks.
159
	 *
160
	 * @since 5.0.0
161
	 */
162
	public function setup() {
163
		if ( ! Jetpack::is_active() || ! $this->is_search_supported() ) {
164
			/**
165
			 * Fires when the Jetpack Search fails and would fallback to MySQL.
166
			 *
167
			 * @module search
168
			 * @since 7.9.0
169
			 *
170
			 * @param string $reason Reason for Search fallback.
171
			 * @param mixed  $data   Data associated with the request, such as attempted search parameters.
172
			 */
173
			do_action( 'jetpack_search_abort', 'inactive', null );
174
			return;
175
		}
176
177
		$this->jetpack_blog_id = Jetpack::get_option( 'id' );
178
179
		if ( ! $this->jetpack_blog_id ) {
180
			/** This action is documented in modules/search/class.jetpack-search.php */
181
			do_action( 'jetpack_search_abort', 'no_blog_id', null );
182
			return;
183
		}
184
185
		$this->load_php();
186
		$this->init_hooks();
187
	}
188
189
	/**
190
	 * Loads the php for this version of search
191
	 *
192
	 * @since 8.3.0
193
	 */
194
	public function load_php() {
195
		$this->base_load_php();
196
	}
197
198
	/**
199
	 * Loads the PHP common to all search. Should be called from extending classes.
200
	 */
201
	protected function base_load_php() {
202
		require_once dirname( __FILE__ ) . '/class.jetpack-search-helpers.php';
203
		require_once dirname( __FILE__ ) . '/class.jetpack-search-template-tags.php';
204
		require_once JETPACK__PLUGIN_DIR . 'modules/widgets/search.php';
205
	}
206
207
	/**
208
	 * Setup the various hooks needed for the plugin to take over search duties.
209
	 *
210
	 * @since 5.0.0
211
	 */
212 View Code Duplication
	public function init_hooks() {
213
		if ( ! is_admin() ) {
214
			add_filter( 'posts_pre_query', array( $this, 'filter__posts_pre_query' ), 10, 2 );
215
216
			add_filter( 'jetpack_search_es_wp_query_args', array( $this, 'filter__add_date_filter_to_query' ), 10, 2 );
217
218
			add_action( 'did_jetpack_search_query', array( $this, 'store_last_query_info' ) );
219
			add_action( 'failed_jetpack_search_query', array( $this, 'store_query_failure' ) );
220
221
			add_action( 'init', array( $this, 'set_filters_from_widgets' ) );
222
223
			add_action( 'pre_get_posts', array( $this, 'maybe_add_post_type_as_var' ) );
224
		} else {
225
			add_action( 'update_option', array( $this, 'track_widget_updates' ), 10, 3 );
226
		}
227
228
		add_action( 'jetpack_deactivate_module_search', array( $this, 'move_search_widgets_to_inactive' ) );
229
	}
230
231
	/**
232
	 * Is search supported on the current plan
233
	 *
234
	 * @since 6.0
235
	 * Loads scripts for Tracks analytics library
236
	 */
237
	public function is_search_supported() {
238
		if ( method_exists( 'Jetpack_Plan', 'supports' ) ) {
239
			return Jetpack_Plan::supports( 'search' );
240
		}
241
		return false;
242
	}
243
244
	/**
245
	 * Does this site have a VIP index
246
	 * Get the version number to use when loading the file. Allows us to bypass cache when developing.
247
	 *
248
	 * @since 6.0
249
	 * @return string $script_version Version number.
250
	 */
251
	public function has_vip_index() {
252
		return defined( 'JETPACK_SEARCH_VIP_INDEX' ) && JETPACK_SEARCH_VIP_INDEX;
253
	}
254
255
	/**
256
	 * When an Elasticsearch query fails, this stores it and enqueues some debug information in the footer.
257
	 *
258
	 * @since 5.6.0
259
	 *
260
	 * @param array $meta Information about the failure.
261
	 */
262
	public function store_query_failure( $meta ) {
263
		$this->last_query_failure_info = $meta;
264
		add_action( 'wp_footer', array( $this, 'print_query_failure' ) );
265
	}
266
267
	/**
268
	 * Outputs information about the last Elasticsearch failure.
269
	 *
270
	 * @since 5.6.0
271
	 */
272
	public function print_query_failure() {
273
		if ( $this->last_query_failure_info ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->last_query_failure_info of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
274
			printf(
275
				'<!-- Jetpack Search failed with code %s: %s - %s -->',
276
				esc_html( $this->last_query_failure_info['response_code'] ),
277
				esc_html( $this->last_query_failure_info['json']['error'] ),
278
				esc_html( $this->last_query_failure_info['json']['message'] )
279
			);
280
		}
281
	}
282
283
	/**
284
	 * Stores information about the last Elasticsearch query and enqueues some debug information in the footer.
285
	 *
286
	 * @since 5.6.0
287
	 *
288
	 * @param array $meta Information about the query.
289
	 */
290
	public function store_last_query_info( $meta ) {
291
		$this->last_query_info = $meta;
292
		add_action( 'wp_footer', array( $this, 'print_query_success' ) );
293
	}
294
295
	/**
296
	 * Outputs information about the last Elasticsearch search.
297
	 *
298
	 * @since 5.6.0
299
	 */
300
	public function print_query_success() {
301
		if ( $this->last_query_info ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->last_query_info of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
302
			printf(
303
				'<!-- Jetpack Search took %s ms, ES time %s ms -->',
304
				intval( $this->last_query_info['elapsed_time'] ),
305
				esc_html( $this->last_query_info['es_time'] )
306
			);
307
308
			if ( isset( $_GET['searchdebug'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
309
				printf(
310
					'<!-- Query response data: %s -->',
311
					esc_html( print_r( $this->last_query_info, 1 ) ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
312
				);
313
			}
314
		}
315
	}
316
317
	/**
318
	 * Returns the last query information, or false if no information was stored.
319
	 *
320
	 * @since 5.8.0
321
	 *
322
	 * @return bool|array
323
	 */
324
	public function get_last_query_info() {
325
		return empty( $this->last_query_info ) ? false : $this->last_query_info;
326
	}
327
328
	/**
329
	 * Returns the last query failure information, or false if no failure information was stored.
330
	 *
331
	 * @since 5.8.0
332
	 *
333
	 * @return bool|array
334
	 */
335
	public function get_last_query_failure_info() {
336
		return empty( $this->last_query_failure_info ) ? false : $this->last_query_failure_info;
337
	}
338
339
	/**
340
	 * Wraps a WordPress filter called "jetpack_search_disable_widget_filters" that allows
341
	 * developers to disable filters supplied by the search widget. Useful if filters are
342
	 * being defined at the code level.
343
	 *
344
	 * @since      5.7.0
345
	 * @deprecated 5.8.0 Use Jetpack_Search_Helpers::are_filters_by_widget_disabled() directly.
346
	 *
347
	 * @return bool
348
	 */
349
	public function are_filters_by_widget_disabled() {
350
		return Jetpack_Search_Helpers::are_filters_by_widget_disabled();
351
	}
352
353
	/**
354
	 * Retrieves a list of known Jetpack search filters widget IDs, gets the filters for each widget,
355
	 * and applies those filters to this Jetpack_Search object.
356
	 *
357
	 * @since 5.7.0
358
	 */
359
	public function set_filters_from_widgets() {
360
		if ( Jetpack_Search_Helpers::are_filters_by_widget_disabled() ) {
361
			return;
362
		}
363
364
		$filters = Jetpack_Search_Helpers::get_filters_from_widgets();
365
366
		if ( ! empty( $filters ) ) {
367
			$this->set_filters( $filters );
368
		}
369
	}
370
371
	/**
372
	 * Restricts search results to certain post types via a GET argument.
373
	 *
374
	 * @since 5.8.0
375
	 *
376
	 * @param WP_Query $query A WP_Query instance.
377
	 */
378
	public function maybe_add_post_type_as_var( WP_Query $query ) {
379
		$post_type = ( ! empty( $_GET['post_type'] ) ) ? $_GET['post_type'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
380
		if ( $this->should_handle_query( $query ) && $post_type ) {
381
			$post_types = ( is_string( $post_type ) && false !== strpos( $post_type, ',' ) )
382
				? explode( ',', $post_type )
383
				: (array) $post_type;
384
			$post_types = array_map( 'sanitize_key', $post_types );
385
			$query->set( 'post_type', $post_types );
386
		}
387
	}
388
389
	/**
390
	 * Run a search on the WordPress.com public API.
391
	 *
392
	 * @since 5.0.0
393
	 *
394
	 * @param array $es_args Args conforming to the WP.com /sites/<blog_id>/search endpoint.
395
	 *
396
	 * @return object|WP_Error The response from the public API, or a WP_Error.
397
	 */
398
	public function search( array $es_args ) {
399
		$endpoint    = sprintf( '/sites/%s/search', $this->jetpack_blog_id );
400
		$service_url = 'https://public-api.wordpress.com/rest/v1' . $endpoint;
401
402
		$do_authenticated_request = false;
403
404
		if ( class_exists( 'Automattic\\Jetpack\\Connection\\Client' ) &&
405
			isset( $es_args['authenticated_request'] ) &&
406
			true === $es_args['authenticated_request'] ) {
407
			$do_authenticated_request = true;
408
		}
409
410
		unset( $es_args['authenticated_request'] );
411
412
		$request_args = array(
413
			'headers'    => array(
414
				'Content-Type' => 'application/json',
415
			),
416
			'timeout'    => 10,
417
			'user-agent' => 'jetpack_search',
418
		);
419
420
		$request_body = wp_json_encode( $es_args );
421
422
		$start_time = microtime( true );
423
424
		if ( $do_authenticated_request ) {
425
			$request_args['method'] = 'POST';
426
427
			$request = Client::wpcom_json_api_request_as_blog( $endpoint, Client::WPCOM_JSON_API_VERSION, $request_args, $request_body );
428
		} else {
429
			$request_args = array_merge(
430
				$request_args,
431
				array(
432
					'body' => $request_body,
433
				)
434
			);
435
436
			$request = wp_remote_post( $service_url, $request_args );
437
		}
438
439
		$end_time = microtime( true );
440
441
		if ( is_wp_error( $request ) ) {
442
			return $request;
443
		}
444
		$response_code = wp_remote_retrieve_response_code( $request );
445
446
		if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) {
447
			return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code );
448
		}
449
450
		$response = json_decode( wp_remote_retrieve_body( $request ), true );
451
452
		$took = is_array( $response ) && ! empty( $response['took'] )
453
			? $response['took']
454
			: null;
455
456
		$query = array(
457
			'args'          => $es_args,
458
			'response'      => $response,
459
			'response_code' => $response_code,
460
			'elapsed_time'  => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms.
461
			'es_time'       => $took,
462
			'url'           => $service_url,
463
		);
464
465
		/**
466
		 * Fires after a search request has been performed.
467
		 *
468
		 * Includes the following info in the $query parameter:
469
		 *
470
		 * array args Array of Elasticsearch arguments for the search
471
		 * array response Raw API response, JSON decoded
472
		 * int response_code HTTP response code of the request
473
		 * float elapsed_time Roundtrip time of the search request, in milliseconds
474
		 * float es_time Amount of time Elasticsearch spent running the request, in milliseconds
475
		 * string url API url that was queried
476
		 *
477
		 * @module search
478
		 *
479
		 * @since  5.0.0
480
		 * @since  5.8.0 This action now fires on all queries instead of just successful queries.
481
		 *
482
		 * @param array $query Array of information about the query performed
483
		 */
484
		do_action( 'did_jetpack_search_query', $query );
485
486 View Code Duplication
		if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) {
487
			/**
488
			 * Fires after a search query request has failed
489
			 *
490
			 * @module search
491
			 *
492
			 * @since  5.6.0
493
			 *
494
			 * @param array Array containing the response code and response from the failed search query
495
			 */
496
			do_action(
497
				'failed_jetpack_search_query',
498
				array(
499
					'response_code' => $response_code,
500
					'json'          => $response,
501
				)
502
			);
503
504
			return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code );
505
		}
506
507
		return $response;
508
	}
509
510
	/**
511
	 * Bypass the normal Search query and offload it to Jetpack servers.
512
	 *
513
	 * This is the main hook of the plugin and is responsible for returning the posts that match the search query.
514
	 *
515
	 * @since 5.0.0
516
	 *
517
	 * @param array    $posts Current array of posts (still pre-query).
518
	 * @param WP_Query $query The WP_Query being filtered.
519
	 *
520
	 * @return array Array of matching posts.
521
	 */
522
	public function filter__posts_pre_query( $posts, $query ) {
523
		if ( ! $this->should_handle_query( $query ) ) {
524
			// Intentionally not adding the 'jetpack_search_abort' action since this should fire for every request except for search.
525
			return $posts;
526
		}
527
528
		$this->do_search( $query );
529
530
		if ( ! is_array( $this->search_result ) ) {
531
			/** This action is documented in modules/search/class.jetpack-search.php */
532
			do_action( 'jetpack_search_abort', 'no_search_results_array', $this->search_result );
533
			return $posts;
534
		}
535
536
		// If no results, nothing to do.
537
		if ( ! count( $this->search_result['results']['hits'] ) ) {
538
			return array();
539
		}
540
541
		$post_ids = array();
542
543
		foreach ( $this->search_result['results']['hits'] as $result ) {
544
			$post_ids[] = (int) $result['fields']['post_id'];
545
		}
546
547
		// Query all posts now.
548
		$args = array(
549
			'post__in'            => $post_ids,
550
			'orderby'             => 'post__in',
551
			'perm'                => 'readable',
552
			'post_type'           => 'any',
553
			'ignore_sticky_posts' => true,
554
			'suppress_filters'    => true,
555
		);
556
557
		$posts_query = new WP_Query( $args );
558
559
		// WP Core doesn't call the set_found_posts and its filters when filtering posts_pre_query like we do, so need to do these manually.
560
		$query->found_posts   = $this->found_posts;
561
		$query->max_num_pages = ceil( $this->found_posts / $query->get( 'posts_per_page' ) );
562
563
		return $posts_query->posts;
564
	}
565
566
	/**
567
	 * Build up the search, then run it against the Jetpack servers.
568
	 *
569
	 * @since 5.0.0
570
	 *
571
	 * @param WP_Query $query The original WP_Query to use for the parameters of our search.
572
	 */
573
	public function do_search( WP_Query $query ) {
574
		if ( ! $this->should_handle_query( $query ) ) {
575
			// If we make it here, either 'filter__posts_pre_query' somehow allowed it or a different entry to do_search.
576
			/** This action is documented in modules/search/class.jetpack-search.php */
577
			do_action( 'jetpack_search_abort', 'search_attempted_non_search_query', $query );
578
			return;
579
		}
580
581
		$page = ( $query->get( 'paged' ) ) ? absint( $query->get( 'paged' ) ) : 1;
582
583
		// Get maximum allowed offset and posts per page values for the API.
584
		$max_offset         = Jetpack_Search_Helpers::get_max_offset();
585
		$max_posts_per_page = Jetpack_Search_Helpers::get_max_posts_per_page();
586
587
		$posts_per_page = $query->get( 'posts_per_page' );
588
		if ( $posts_per_page > $max_posts_per_page ) {
589
			$posts_per_page = $max_posts_per_page;
590
		}
591
592
		// Start building the WP-style search query args.
593
		// They'll be translated to ES format args later.
594
		$es_wp_query_args = array(
595
			'query'          => $query->get( 's' ),
596
			'posts_per_page' => $posts_per_page,
597
			'paged'          => $page,
598
			'orderby'        => $query->get( 'orderby' ),
599
			'order'          => $query->get( 'order' ),
600
		);
601
602
		if ( ! empty( $this->aggregations ) ) {
603
			$es_wp_query_args['aggregations'] = $this->aggregations;
604
		}
605
606
		// Did we query for authors?
607
		if ( $query->get( 'author_name' ) ) {
608
			$es_wp_query_args['author_name'] = $query->get( 'author_name' );
609
		}
610
611
		$es_wp_query_args['post_type'] = $this->get_es_wp_query_post_type_for_query( $query );
612
		$es_wp_query_args['terms']     = $this->get_es_wp_query_terms_for_query( $query );
613
614
		/**
615
		 * Modify the search query parameters, such as controlling the post_type.
616
		 *
617
		 * These arguments are in the format of WP_Query arguments
618
		 *
619
		 * @module search
620
		 *
621
		 * @since  5.0.0
622
		 *
623
		 * @param array    $es_wp_query_args The current query args, in WP_Query format.
624
		 * @param WP_Query $query            The original WP_Query object.
625
		 */
626
		$es_wp_query_args = apply_filters( 'jetpack_search_es_wp_query_args', $es_wp_query_args, $query );
627
628
		// If page * posts_per_page is greater than our max offset, send a 404. This is necessary because the offset is
629
		// capped at Jetpack_Search_Helpers::get_max_offset(), so a high page would always return the last page of results otherwise.
630
		if ( ( $es_wp_query_args['paged'] * $es_wp_query_args['posts_per_page'] ) > $max_offset ) {
631
			$query->set_404();
632
633
			return;
634
		}
635
636
		// If there were no post types returned, then 404 to avoid querying against non-public post types, which could
637
		// happen if we don't add the post type restriction to the ES query.
638
		if ( empty( $es_wp_query_args['post_type'] ) ) {
639
			$query->set_404();
640
641
			return;
642
		}
643
644
		// Convert the WP-style args into ES args.
645
		$es_query_args = $this->convert_wp_es_to_es_args( $es_wp_query_args );
646
647
		// Only trust ES to give us IDs, not the content since it is a mirror.
648
		$es_query_args['fields'] = array(
649
			'post_id',
650
		);
651
652
		/**
653
		 * Modify the underlying ES query that is passed to the search endpoint. The returned args must represent a valid ES query
654
		 *
655
		 * This filter is harder to use if you're unfamiliar with ES, but allows complete control over the query
656
		 *
657
		 * @module search
658
		 *
659
		 * @since  5.0.0
660
		 *
661
		 * @param array    $es_query_args The raw Elasticsearch query args.
662
		 * @param WP_Query $query         The original WP_Query object.
663
		 */
664
		$es_query_args = apply_filters( 'jetpack_search_es_query_args', $es_query_args, $query );
665
666
		// Do the actual search query!
667
		$this->search_result = $this->search( $es_query_args );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->search($es_query_args) of type object is incompatible with the declared type array of property $search_result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
668
669
		if ( is_wp_error( $this->search_result ) || ! is_array( $this->search_result ) || empty( $this->search_result['results'] ) || empty( $this->search_result['results']['hits'] ) ) {
670
			$this->found_posts = 0;
671
672
			return;
673
		}
674
675
		// If we have aggregations, fix the ordering to match the input order (ES doesn't guarantee the return order).
676
		if ( isset( $this->search_result['results']['aggregations'] ) && ! empty( $this->search_result['results']['aggregations'] ) ) {
677
			$this->search_result['results']['aggregations'] = $this->fix_aggregation_ordering( $this->search_result['results']['aggregations'], $this->aggregations );
678
		}
679
680
		// Total number of results for paging purposes. Capped at $max_offset + $posts_per_page, as deep paging gets quite expensive.
681
		$this->found_posts = min( $this->search_result['results']['total'], $max_offset + $posts_per_page );
682
	}
683
684
	/**
685
	 * If the query has already been run before filters have been updated, then we need to re-run the query
686
	 * to get the latest aggregations.
687
	 *
688
	 * This is especially useful for supporting widget management in the customizer.
689
	 *
690
	 * @since 5.8.0
691
	 *
692
	 * @return bool Whether the query was successful or not.
693
	 */
694
	public function update_search_results_aggregations() {
695
		if ( empty( $this->last_query_info ) || empty( $this->last_query_info['args'] ) ) {
696
			return false;
697
		}
698
699
		$es_args = $this->last_query_info['args'];
700
		$builder = new Jetpack_WPES_Query_Builder();
701
		$this->add_aggregations_to_es_query_builder( $this->aggregations, $builder );
702
		$es_args['aggregations'] = $builder->build_aggregation();
703
704
		$this->search_result = $this->search( $es_args );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->search($es_args) of type object is incompatible with the declared type array of property $search_result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
705
706
		return ! is_wp_error( $this->search_result );
707
	}
708
709
	/**
710
	 * Given a WP_Query, convert its WP_Tax_Query (if present) into the WP-style Elasticsearch term arguments for the search.
711
	 *
712
	 * @since 5.0.0
713
	 *
714
	 * @param WP_Query $query The original WP_Query object for which to parse the taxonomy query.
715
	 *
716
	 * @return array The new WP-style Elasticsearch arguments (that will be converted into 'real' Elasticsearch arguments).
717
	 */
718
	public function get_es_wp_query_terms_for_query( WP_Query $query ) {
719
		$args = array();
720
721
		$the_tax_query = $query->tax_query;
722
723
		if ( ! $the_tax_query ) {
724
			return $args;
725
		}
726
727
		if ( ! $the_tax_query instanceof WP_Tax_Query || empty( $the_tax_query->queried_terms ) || ! is_array( $the_tax_query->queried_terms ) ) {
0 ignored issues
show
Bug introduced by
The class WP_Tax_Query does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
728
			return $args;
729
		}
730
731
		$args = array();
732
733
		foreach ( $the_tax_query->queries as $tax_query ) {
734
			// Right now we only support slugs...see note above.
735
			if ( ! is_array( $tax_query ) || 'slug' !== $tax_query['field'] ) {
736
				continue;
737
			}
738
739
			$taxonomy = $tax_query['taxonomy'];
740
741 View Code Duplication
			if ( ! isset( $args[ $taxonomy ] ) || ! is_array( $args[ $taxonomy ] ) ) {
742
				$args[ $taxonomy ] = array();
743
			}
744
745
			$args[ $taxonomy ] = array_merge( $args[ $taxonomy ], $tax_query['terms'] );
746
		}
747
748
		return $args;
749
	}
750
751
	/**
752
	 * Parse out the post type from a WP_Query.
753
	 *
754
	 * Only allows post types that are not marked as 'exclude_from_search'.
755
	 *
756
	 * @since 5.0.0
757
	 *
758
	 * @param WP_Query $query Original WP_Query object.
759
	 *
760
	 * @return array Array of searchable post types corresponding to the original query.
761
	 */
762
	public function get_es_wp_query_post_type_for_query( WP_Query $query ) {
763
		$post_types = $query->get( 'post_type' );
764
765
		// If we're searching 'any', we want to only pass searchable post types to Elasticsearch.
766
		if ( 'any' === $post_types ) {
767
			$post_types = array_values(
768
				get_post_types(
769
					array(
770
						'exclude_from_search' => false,
771
					)
772
				)
773
			);
774
		}
775
776
		if ( ! is_array( $post_types ) ) {
777
			$post_types = array( $post_types );
778
		}
779
780
		$post_types = array_unique( $post_types );
781
782
		$sanitized_post_types = array();
783
784
		// Make sure the post types are queryable.
785
		foreach ( $post_types as $post_type ) {
786
			if ( ! $post_type ) {
787
				continue;
788
			}
789
790
			$post_type_object = get_post_type_object( $post_type );
791
			if ( ! $post_type_object || $post_type_object->exclude_from_search ) {
792
				continue;
793
			}
794
795
			$sanitized_post_types[] = $post_type;
796
		}
797
798
		return $sanitized_post_types;
799
	}
800
801
	/**
802
	 * Initialize widgets for the Search module (on wp.com only).
803
	 *
804
	 * @module search
805
	 */
806
	public function action__widgets_init() {
807
		require_once dirname( __FILE__ ) . '/class.jetpack-search-widget-filters.php';
808
809
		register_widget( 'Jetpack_Search_Widget_Filters' );
810
	}
811
812
	/**
813
	 * Get the Elasticsearch result.
814
	 *
815
	 * @since 5.0.0
816
	 *
817
	 * @param bool $raw If true, does not check for WP_Error or return the 'results' array - the JSON decoded HTTP response.
818
	 *
819
	 * @return array|bool The search results, or false if there was a failure.
820
	 */
821
	public function get_search_result( $raw = false ) {
822
		if ( $raw ) {
823
			return $this->search_result;
824
		}
825
826
		return ( ! empty( $this->search_result ) && ! is_wp_error( $this->search_result ) && is_array( $this->search_result ) && ! empty( $this->search_result['results'] ) ) ? $this->search_result['results'] : false;
827
	}
828
829
	/**
830
	 * Add the date portion of a WP_Query onto the query args.
831
	 *
832
	 * @since 5.0.0
833
	 *
834
	 * @param array    $es_wp_query_args The Elasticsearch query arguments in WordPress form.
835
	 * @param WP_Query $query            The original WP_Query.
836
	 *
837
	 * @return array The es wp query args, with date filters added (as needed).
838
	 */
839
	public function filter__add_date_filter_to_query( array $es_wp_query_args, WP_Query $query ) {
840
		if ( $query->get( 'year' ) ) {
841
			if ( $query->get( 'monthnum' ) ) {
842
				// Padding.
843
				$date_monthnum = sprintf( '%02d', $query->get( 'monthnum' ) );
844
845
				if ( $query->get( 'day' ) ) {
846
					// Padding.
847
					$date_day = sprintf( '%02d', $query->get( 'day' ) );
848
849
					$date_start = $query->get( 'year' ) . '-' . $date_monthnum . '-' . $date_day . ' 00:00:00';
850
					$date_end   = $query->get( 'year' ) . '-' . $date_monthnum . '-' . $date_day . ' 23:59:59';
851
				} else {
852
					$days_in_month = gmdate( 't', mktime( 0, 0, 0, $query->get( 'monthnum' ), 14, $query->get( 'year' ) ) ); // 14 = middle of the month so no chance of DST issues
853
854
					$date_start = $query->get( 'year' ) . '-' . $date_monthnum . '-01 00:00:00';
855
					$date_end   = $query->get( 'year' ) . '-' . $date_monthnum . '-' . $days_in_month . ' 23:59:59';
856
				}
857
			} else {
858
				$date_start = $query->get( 'year' ) . '-01-01 00:00:00';
859
				$date_end   = $query->get( 'year' ) . '-12-31 23:59:59';
860
			}
861
862
			$es_wp_query_args['date_range'] = array(
863
				'field' => 'date',
864
				'gte'   => $date_start,
865
				'lte'   => $date_end,
866
			);
867
		}
868
869
		return $es_wp_query_args;
870
	}
871
872
	/**
873
	 * Converts WP_Query style args to Elasticsearch args.
874
	 *
875
	 * @since 5.0.0
876
	 *
877
	 * @param array $args Array of WP_Query style arguments.
878
	 *
879
	 * @return array Array of ES style query arguments.
880
	 */
881
	public function convert_wp_es_to_es_args( array $args ) {
882
		jetpack_require_lib( 'jetpack-wpes-query-builder/jetpack-wpes-query-parser' );
883
884
		$defaults = array(
885
			'blog_id'        => get_current_blog_id(),
886
			'query'          => null,    // Search phrase.
887
			'query_fields'   => array(), // list of fields to search.
888
			'excess_boost'   => array(), // map of field to excess boost values (multiply).
889
			'post_type'      => null,    // string or an array.
890
			'terms'          => array(), // ex: array( 'taxonomy-1' => array( 'slug' ), 'taxonomy-2' => array( 'slug-a', 'slug-b' ) ). phpcs:ignore Squiz.PHP.CommentedOutCode.Found.
891
			'author'         => null,    // id or an array of ids.
892
			'author_name'    => array(), // string or an array.
893
			'date_range'     => null,    // array( 'field' => 'date', 'gt' => 'YYYY-MM-dd', 'lte' => 'YYYY-MM-dd' ); date formats: 'YYYY-MM-dd' or 'YYYY-MM-dd HH:MM:SS'. phpcs:ignore Squiz.PHP.CommentedOutCode.Found.
894
			'orderby'        => null,    // Defaults to 'relevance' if query is set, otherwise 'date'. Pass an array for multiple orders.
895
			'order'          => 'DESC',
896
			'posts_per_page' => 10,
897
			'offset'         => null,
898
			'paged'          => null,
899
			/**
900
			 * Aggregations. Examples:
901
			 * array(
902
			 *     'Tag'       => array( 'type' => 'taxonomy', 'taxonomy' => 'post_tag', 'count' => 10 ) ),
903
			 *     'Post Type' => array( 'type' => 'post_type', 'count' => 10 ) ),
904
			 * );
905
			 */
906
			'aggregations'   => null,
907
		);
908
909
		$args = wp_parse_args( $args, $defaults );
910
911
		$parser = new Jetpack_WPES_Search_Query_Parser(
912
			$args['query'],
913
			/**
914
			 * Filter the languages used by Jetpack Search's Query Parser.
915
			 *
916
			 * @module search
917
			 *
918
			 * @since  7.9.0
919
			 *
920
			 * @param array $languages The array of languages. Default is value of get_locale().
921
			 */
922
			apply_filters( 'jetpack_search_query_languages', array( get_locale() ) )
923
		);
924
925
		if ( empty( $args['query_fields'] ) ) {
926
			if ( $this->has_vip_index() ) {
927
				// VIP indices do not have per language fields.
928
				$match_fields = $this->_get_caret_boosted_fields(
929
					array(
930
						'title'         => 0.1,
931
						'content'       => 0.1,
932
						'excerpt'       => 0.1,
933
						'tag.name'      => 0.1,
934
						'category.name' => 0.1,
935
						'author_login'  => 0.1,
936
						'author'        => 0.1,
937
					)
938
				);
939
940
				$boost_fields = $this->_get_caret_boosted_fields(
941
					$this->_apply_boosts_multiplier(
942
						array(
943
							'title'         => 2,
944
							'tag.name'      => 1,
945
							'category.name' => 1,
946
							'author_login'  => 1,
947
							'author'        => 1,
948
						),
949
						$args['excess_boost']
950
					)
951
				);
952
953
				$boost_phrase_fields = $this->_get_caret_boosted_fields(
954
					array(
955
						'title'         => 1,
956
						'content'       => 1,
957
						'excerpt'       => 1,
958
						'tag.name'      => 1,
959
						'category.name' => 1,
960
						'author'        => 1,
961
					)
962
				);
963
			} else {
964
				$match_fields = $parser->merge_ml_fields(
965
					array(
966
						'title'         => 0.1,
967
						'content'       => 0.1,
968
						'excerpt'       => 0.1,
969
						'tag.name'      => 0.1,
970
						'category.name' => 0.1,
971
					),
972
					$this->_get_caret_boosted_fields(
973
						array(
974
							'author_login' => 0.1,
975
							'author'       => 0.1,
976
						)
977
					)
978
				);
979
980
				$boost_fields = $parser->merge_ml_fields(
981
					$this->_apply_boosts_multiplier(
982
						array(
983
							'title'         => 2,
984
							'tag.name'      => 1,
985
							'category.name' => 1,
986
						),
987
						$args['excess_boost']
988
					),
989
					$this->_get_caret_boosted_fields(
990
						$this->_apply_boosts_multiplier(
991
							array(
992
								'author_login' => 1,
993
								'author'       => 1,
994
							),
995
							$args['excess_boost']
996
						)
997
					)
998
				);
999
1000
				$boost_phrase_fields = $parser->merge_ml_fields(
1001
					array(
1002
						'title'         => 1,
1003
						'content'       => 1,
1004
						'excerpt'       => 1,
1005
						'tag.name'      => 1,
1006
						'category.name' => 1,
1007
					),
1008
					$this->_get_caret_boosted_fields(
1009
						array(
1010
							'author' => 1,
1011
						)
1012
					)
1013
				);
1014
			}
1015
		} else {
1016
			// If code is overriding the fields, then use that. Important for backwards compatibility.
1017
			$match_fields        = $args['query_fields'];
1018
			$boost_phrase_fields = $match_fields;
1019
			$boost_fields        = null;
1020
		}
1021
1022
		$parser->phrase_filter(
1023
			array(
1024
				'must_query_fields'  => $match_fields,
1025
				'boost_query_fields' => null,
1026
			)
1027
		);
1028
		$parser->remaining_query(
1029
			array(
1030
				'must_query_fields'  => $match_fields,
1031
				'boost_query_fields' => $boost_fields,
1032
			)
1033
		);
1034
1035
		// Boost on phrase matches.
1036
		$parser->remaining_query(
1037
			array(
1038
				'boost_query_fields' => $boost_phrase_fields,
1039
				'boost_query_type'   => 'phrase',
1040
			)
1041
		);
1042
1043
		/**
1044
		 * Modify the recency decay parameters for the search query.
1045
		 *
1046
		 * The recency decay lowers the search scores based on the age of a post relative to an origin date. Basic adjustments:
1047
		 *  - origin: A date. Posts with this date will have the highest score and no decay applied. Default is today.
1048
		 *  - offset: Number of days/months/years (eg 30d). All posts within this time range of the origin (before and after) will have no decay applied. Default is no offset.
1049
		 *  - scale: The number of days/months/years from the origin+offset at which the decay will equal the decay param. Default 360d
1050
		 *  - decay: The amount of decay applied at offset+scale. Default 0.9.
1051
		 *
1052
		 * The curve applied is a Gaussian. More details available at {@see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-decay}
1053
		 *
1054
		 * @module search
1055
		 *
1056
		 * @since  5.8.0
1057
		 *
1058
		 * @param array $decay_params The decay parameters.
1059
		 * @param array $args         The WP query parameters.
1060
		 */
1061
		$decay_params = apply_filters(
1062
			'jetpack_search_recency_score_decay',
1063
			array(
1064
				'origin' => gmdate( 'Y-m-d' ),
1065
				'scale'  => '360d',
1066
				'decay'  => 0.9,
1067
			),
1068
			$args
1069
		);
1070
1071
		if ( ! empty( $decay_params ) ) {
1072
			// Newer content gets weighted slightly higher.
1073
			$parser->add_decay(
1074
				'gauss',
1075
				array(
1076
					'date_gmt' => $decay_params,
1077
				)
1078
			);
1079
		}
1080
1081
		$es_query_args = array(
1082
			'blog_id' => absint( $args['blog_id'] ),
1083
			'size'    => absint( $args['posts_per_page'] ),
1084
		);
1085
1086
		// ES "from" arg (offset).
1087
		if ( $args['offset'] ) {
1088
			$es_query_args['from'] = absint( $args['offset'] );
1089
		} elseif ( $args['paged'] ) {
1090
			$es_query_args['from'] = max( 0, ( absint( $args['paged'] ) - 1 ) * $es_query_args['size'] );
1091
		}
1092
1093
		$es_query_args['from'] = min( $es_query_args['from'], Jetpack_Search_Helpers::get_max_offset() );
1094
1095
		if ( ! is_array( $args['author_name'] ) ) {
1096
			$args['author_name'] = array( $args['author_name'] );
1097
		}
1098
1099
		// ES stores usernames, not IDs, so transform.
1100
		if ( ! empty( $args['author'] ) ) {
1101
			if ( ! is_array( $args['author'] ) ) {
1102
				$args['author'] = array( $args['author'] );
1103
			}
1104
1105
			foreach ( $args['author'] as $author ) {
1106
				$user = get_user_by( 'id', $author );
1107
1108
				if ( $user && ! empty( $user->user_login ) ) {
1109
					$args['author_name'][] = $user->user_login;
1110
				}
1111
			}
1112
		}
1113
1114
		/*
1115
		 * Build the filters from the query elements.
1116
		 * Filters rock because they are cached from one query to the next
1117
		 * but they are cached as individual filters, rather than all combined together.
1118
		 * May get performance boost by also caching the top level boolean filter too.
1119
		 */
1120
1121
		if ( $args['post_type'] ) {
1122
			if ( ! is_array( $args['post_type'] ) ) {
1123
				$args['post_type'] = array( $args['post_type'] );
1124
			}
1125
1126
			$parser->add_filter(
1127
				array(
1128
					'terms' => array(
1129
						'post_type' => $args['post_type'],
1130
					),
1131
				)
1132
			);
1133
		}
1134
1135
		if ( $args['author_name'] ) {
1136
			$parser->add_filter(
1137
				array(
1138
					'terms' => array(
1139
						'author_login' => $args['author_name'],
1140
					),
1141
				)
1142
			);
1143
		}
1144
1145
		if ( ! empty( $args['date_range'] ) && isset( $args['date_range']['field'] ) ) {
1146
			$field = $args['date_range']['field'];
1147
1148
			unset( $args['date_range']['field'] );
1149
1150
			$parser->add_filter(
1151
				array(
1152
					'range' => array(
1153
						$field => $args['date_range'],
1154
					),
1155
				)
1156
			);
1157
		}
1158
1159
		if ( is_array( $args['terms'] ) ) {
1160
			foreach ( $args['terms'] as $tax => $terms ) {
1161
				$terms = (array) $terms;
1162
1163
				if ( count( $terms ) && mb_strlen( $tax ) ) {
1164 View Code Duplication
					switch ( $tax ) {
1165
						case 'post_tag':
1166
							$tax_fld = 'tag.slug';
1167
1168
							break;
1169
1170
						case 'category':
1171
							$tax_fld = 'category.slug';
1172
1173
							break;
1174
1175
						default:
1176
							$tax_fld = 'taxonomy.' . $tax . '.slug';
1177
1178
							break;
1179
					}
1180
1181
					foreach ( $terms as $term ) {
1182
						$parser->add_filter(
1183
							array(
1184
								'term' => array(
1185
									$tax_fld => $term,
1186
								),
1187
							)
1188
						);
1189
					}
1190
				}
1191
			}
1192
		}
1193
1194
		if ( ! $args['orderby'] ) {
1195
			if ( $args['query'] ) {
1196
				$args['orderby'] = array( 'relevance' );
1197
			} else {
1198
				$args['orderby'] = array( 'date' );
1199
			}
1200
		}
1201
1202
		// Validate the "order" field.
1203
		switch ( strtolower( $args['order'] ) ) {
1204
			case 'asc':
1205
				$args['order'] = 'asc';
1206
				break;
1207
1208
			case 'desc':
1209
			default:
1210
				$args['order'] = 'desc';
1211
				break;
1212
		}
1213
1214
		$es_query_args['sort'] = array();
1215
1216
		foreach ( (array) $args['orderby'] as $orderby ) {
1217
			// Translate orderby from WP field to ES field.
1218
			switch ( $orderby ) {
1219
				case 'relevance':
1220
					// never order by score ascending.
1221
					$es_query_args['sort'][] = array(
1222
						'_score' => array(
1223
							'order' => 'desc',
1224
						),
1225
					);
1226
1227
					break;
1228
1229 View Code Duplication
				case 'date':
1230
					$es_query_args['sort'][] = array(
1231
						'date' => array(
1232
							'order' => $args['order'],
1233
						),
1234
					);
1235
1236
					break;
1237
1238 View Code Duplication
				case 'ID':
1239
					$es_query_args['sort'][] = array(
1240
						'id' => array(
1241
							'order' => $args['order'],
1242
						),
1243
					);
1244
1245
					break;
1246
1247
				case 'author':
1248
					$es_query_args['sort'][] = array(
1249
						'author.raw' => array(
1250
							'order' => $args['order'],
1251
						),
1252
					);
1253
1254
					break;
1255
			} // End switch.
1256
		} // End foreach.
1257
1258
		if ( empty( $es_query_args['sort'] ) ) {
1259
			unset( $es_query_args['sort'] );
1260
		}
1261
1262
		// Aggregations.
1263
		if ( ! empty( $args['aggregations'] ) ) {
1264
			$this->add_aggregations_to_es_query_builder( $args['aggregations'], $parser );
0 ignored issues
show
Documentation introduced by
$args['aggregations'] is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1265
		}
1266
1267
		$es_query_args['filter']       = $parser->build_filter();
1268
		$es_query_args['query']        = $parser->build_query();
1269
		$es_query_args['aggregations'] = $parser->build_aggregation();
1270
1271
		return $es_query_args;
1272
	}
1273
1274
	/**
1275
	 * Given an array of aggregations, parse and add them onto the Jetpack_WPES_Query_Builder object for use in Elasticsearch.
1276
	 *
1277
	 * @since 5.0.0
1278
	 *
1279
	 * @param array                      $aggregations Array of aggregations (filters) to add to the Jetpack_WPES_Query_Builder.
1280
	 * @param Jetpack_WPES_Query_Builder $builder      The builder instance that is creating the Elasticsearch query.
1281
	 */
1282
	public function add_aggregations_to_es_query_builder( array $aggregations, Jetpack_WPES_Query_Builder $builder ) {
1283
		foreach ( $aggregations as $label => $aggregation ) {
1284
			switch ( $aggregation['type'] ) {
1285
				case 'taxonomy':
1286
					$this->add_taxonomy_aggregation_to_es_query_builder( $aggregation, $label, $builder );
1287
1288
					break;
1289
1290
				case 'post_type':
1291
					$this->add_post_type_aggregation_to_es_query_builder( $aggregation, $label, $builder );
1292
1293
					break;
1294
1295
				case 'date_histogram':
1296
					$this->add_date_histogram_aggregation_to_es_query_builder( $aggregation, $label, $builder );
1297
1298
					break;
1299
			}
1300
		}
1301
	}
1302
1303
	/**
1304
	 * Given an individual taxonomy aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch.
1305
	 *
1306
	 * @since 5.0.0
1307
	 *
1308
	 * @param array                      $aggregation The aggregation to add to the query builder.
1309
	 * @param string                     $label       The 'label' (unique id) for this aggregation.
1310
	 * @param Jetpack_WPES_Query_Builder $builder     The builder instance that is creating the Elasticsearch query.
1311
	 */
1312
	public function add_taxonomy_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) {
1313
		$field = null;
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1314
1315
		switch ( $aggregation['taxonomy'] ) {
1316
			case 'post_tag':
1317
				$field = 'tag';
1318
				break;
1319
1320
			case 'category':
1321
				$field = 'category';
1322
				break;
1323
1324
			default:
1325
				$field = 'taxonomy.' . $aggregation['taxonomy'];
1326
				break;
1327
		}
1328
1329
		$builder->add_aggs(
1330
			$label,
1331
			array(
1332
				'terms' => array(
1333
					'field' => $field . '.slug',
1334
					'size'  => min( (int) $aggregation['count'], $this->max_aggregations_count ),
1335
				),
1336
			)
1337
		);
1338
	}
1339
1340
	/**
1341
	 * Given an individual post_type aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch.
1342
	 *
1343
	 * @since 5.0.0
1344
	 *
1345
	 * @param array                      $aggregation The aggregation to add to the query builder.
1346
	 * @param string                     $label       The 'label' (unique id) for this aggregation.
1347
	 * @param Jetpack_WPES_Query_Builder $builder     The builder instance that is creating the Elasticsearch query.
1348
	 */
1349
	public function add_post_type_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) {
1350
		$builder->add_aggs(
1351
			$label,
1352
			array(
1353
				'terms' => array(
1354
					'field' => 'post_type',
1355
					'size'  => min( (int) $aggregation['count'], $this->max_aggregations_count ),
1356
				),
1357
			)
1358
		);
1359
	}
1360
1361
	/**
1362
	 * Given an individual date_histogram aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch.
1363
	 *
1364
	 * @since 5.0.0
1365
	 *
1366
	 * @param array                      $aggregation The aggregation to add to the query builder.
1367
	 * @param string                     $label       The 'label' (unique id) for this aggregation.
1368
	 * @param Jetpack_WPES_Query_Builder $builder     The builder instance that is creating the Elasticsearch query.
1369
	 */
1370
	public function add_date_histogram_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) {
1371
		$args = array(
1372
			'interval' => $aggregation['interval'],
1373
			'field'    => ( ! empty( $aggregation['field'] ) && 'post_date_gmt' === $aggregation['field'] ) ? 'date_gmt' : 'date',
1374
		);
1375
1376
		if ( isset( $aggregation['min_doc_count'] ) ) {
1377
			$args['min_doc_count'] = intval( $aggregation['min_doc_count'] );
1378
		} else {
1379
			$args['min_doc_count'] = 1;
1380
		}
1381
1382
		$builder->add_aggs(
1383
			$label,
1384
			array(
1385
				'date_histogram' => $args,
1386
			)
1387
		);
1388
	}
1389
1390
	/**
1391
	 * And an existing filter object with a list of additional filters.
1392
	 *
1393
	 * Attempts to optimize the filters somewhat.
1394
	 *
1395
	 * @since 5.0.0
1396
	 *
1397
	 * @param array $curr_filter The existing filters to build upon.
1398
	 * @param array $filters     The new filters to add.
1399
	 *
1400
	 * @return array The resulting merged filters.
1401
	 */
1402
	public static function and_es_filters( array $curr_filter, array $filters ) {
1403
		if ( ! is_array( $curr_filter ) || isset( $curr_filter['match_all'] ) ) {
1404
			if ( 1 === count( $filters ) ) {
1405
				return $filters[0];
1406
			}
1407
1408
			return array(
1409
				'and' => $filters,
1410
			);
1411
		}
1412
1413
		return array(
1414
			'and' => array_merge( array( $curr_filter ), $filters ),
1415
		);
1416
	}
1417
1418
	/**
1419
	 * Set the available filters for the search.
1420
	 *
1421
	 * These get rendered via the Jetpack_Search_Widget() widget.
1422
	 *
1423
	 * Behind the scenes, these are implemented using Elasticsearch Aggregations.
1424
	 *
1425
	 * If you do not require counts of how many documents match each filter, please consider using regular WP Query
1426
	 * arguments instead, such as via the jetpack_search_es_wp_query_args filter
1427
	 *
1428
	 * @see    https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
1429
	 *
1430
	 * @since  5.0.0
1431
	 *
1432
	 * @param array $aggregations Array of filters (aggregations) to apply to the search.
1433
	 */
1434
	public function set_filters( array $aggregations ) {
1435
		foreach ( (array) $aggregations as $key => $agg ) {
1436
			if ( empty( $agg['name'] ) ) {
1437
				$aggregations[ $key ]['name'] = $key;
1438
			}
1439
		}
1440
		$this->aggregations = $aggregations;
1441
	}
1442
1443
	/**
1444
	 * Set the search's facets (deprecated).
1445
	 *
1446
	 * @deprecated 5.0 Please use Jetpack_Search::set_filters() instead.
1447
	 *
1448
	 * @see        Jetpack_Search::set_filters()
1449
	 *
1450
	 * @param array $facets Array of facets to apply to the search.
1451
	 */
1452
	public function set_facets( array $facets ) {
1453
		_deprecated_function( __METHOD__, 'jetpack-5.0', 'Jetpack_Search::set_filters()' );
1454
1455
		$this->set_filters( $facets );
1456
	}
1457
1458
	/**
1459
	 * Get the raw Aggregation results from the Elasticsearch response.
1460
	 *
1461
	 * @since  5.0.0
1462
	 *
1463
	 * @see    https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
1464
	 *
1465
	 * @return array Array of Aggregations performed on the search.
1466
	 */
1467
	public function get_search_aggregations_results() {
1468
		$aggregations = array();
1469
1470
		$search_result = $this->get_search_result();
1471
1472
		if ( ! empty( $search_result ) && ! empty( $search_result['aggregations'] ) ) {
1473
			$aggregations = $search_result['aggregations'];
1474
		}
1475
1476
		return $aggregations;
1477
	}
1478
1479
	/**
1480
	 * Get the raw Facet results from the Elasticsearch response.
1481
	 *
1482
	 * @deprecated 5.0 Please use Jetpack_Search::get_search_aggregations_results() instead.
1483
	 *
1484
	 * @see        Jetpack_Search::get_search_aggregations_results()
1485
	 *
1486
	 * @return array Array of Facets performed on the search.
1487
	 */
1488
	public function get_search_facets() {
1489
		_deprecated_function( __METHOD__, 'jetpack-5.0', 'Jetpack_Search::get_search_aggregations_results()' );
1490
1491
		return $this->get_search_aggregations_results();
1492
	}
1493
1494
	/**
1495
	 * Get the results of the Filters performed, including the number of matching documents.
1496
	 *
1497
	 * Returns an array of Filters (keyed by $label, as passed to Jetpack_Search::set_filters()), containing the Filter and all resulting
1498
	 * matching buckets, the url for applying/removing each bucket, etc.
1499
	 *
1500
	 * NOTE - if this is called before the search is performed, an empty array will be returned. Use the $aggregations class
1501
	 * member if you need to access the raw filters set in Jetpack_Search::set_filters().
1502
	 *
1503
	 * @since 5.0.0
1504
	 *
1505
	 * @param WP_Query $query The optional original WP_Query to use for determining which filters are active. Defaults to the main query.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $query not be null|WP_Query?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1506
	 *
1507
	 * @return array Array of filters applied and info about them.
1508
	 */
1509
	public function get_filters( WP_Query $query = null ) {
1510
		if ( ! $query instanceof WP_Query ) {
0 ignored issues
show
Bug introduced by
The class WP_Query does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
1511
			global $wp_query;
1512
1513
			$query = $wp_query;
1514
		}
1515
1516
		$aggregation_data = $this->aggregations;
1517
1518
		if ( empty( $aggregation_data ) ) {
1519
			return $aggregation_data;
1520
		}
1521
1522
		$aggregation_results = $this->get_search_aggregations_results();
1523
1524
		if ( ! $aggregation_results ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $aggregation_results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1525
			return $aggregation_data;
1526
		}
1527
1528
		// NOTE - Looping over the _results_, not the original configured aggregations, so we get the 'real' data from ES.
1529
		foreach ( $aggregation_results as $label => $aggregation ) {
1530
			if ( empty( $aggregation ) ) {
1531
				continue;
1532
			}
1533
1534
			$type = $this->aggregations[ $label ]['type'];
1535
1536
			$aggregation_data[ $label ]['buckets'] = array();
1537
1538
			$existing_term_slugs = array();
1539
1540
			$tax_query_var = null;
1541
1542
			// Figure out which terms are active in the query, for this taxonomy.
1543
			if ( 'taxonomy' === $this->aggregations[ $label ]['type'] ) {
1544
				$tax_query_var = $this->get_taxonomy_query_var( $this->aggregations[ $label ]['taxonomy'] );
1545
1546
				if ( ! empty( $query->tax_query ) && ! empty( $query->tax_query->queries ) && is_array( $query->tax_query->queries ) ) {
1547
					foreach ( $query->tax_query->queries as $tax_query ) {
1548
						if ( is_array( $tax_query ) && $this->aggregations[ $label ]['taxonomy'] === $tax_query['taxonomy'] &&
1549
							'slug' === $tax_query['field'] &&
1550
							is_array( $tax_query['terms'] ) ) {
1551
							$existing_term_slugs = array_merge( $existing_term_slugs, $tax_query['terms'] );
1552
						}
1553
					}
1554
				}
1555
			}
1556
1557
			// Now take the resulting found aggregation items and generate the additional info about them, such as activation/deactivation url, name, count, etc.
1558
			$buckets = array();
1559
1560
			if ( ! empty( $aggregation['buckets'] ) ) {
1561
				$buckets = (array) $aggregation['buckets'];
1562
			}
1563
1564
			if ( 'date_histogram' === $type ) {
1565
				// re-order newest to oldest.
1566
				$buckets = array_reverse( $buckets );
1567
			}
1568
1569
			// Some aggregation types like date_histogram don't support the max results parameter.
1570
			if ( is_int( $this->aggregations[ $label ]['count'] ) && count( $buckets ) > $this->aggregations[ $label ]['count'] ) {
1571
				$buckets = array_slice( $buckets, 0, $this->aggregations[ $label ]['count'] );
1572
			}
1573
1574
			foreach ( $buckets as $item ) {
1575
				$query_vars = array();
1576
				$active     = false;
1577
				$remove_url = null;
1578
				$name       = '';
1579
1580
				// What type was the original aggregation?
1581
				switch ( $type ) {
1582
					case 'taxonomy':
1583
						$taxonomy = $this->aggregations[ $label ]['taxonomy'];
1584
1585
						$term = get_term_by( 'slug', $item['key'], $taxonomy );
1586
1587
						if ( ! $term || ! $tax_query_var ) {
1588
							continue 2; // switch() is considered a looping structure.
1589
						}
1590
1591
						$query_vars = array(
1592
							$tax_query_var => implode( '+', array_merge( $existing_term_slugs, array( $term->slug ) ) ),
1593
						);
1594
1595
						$name = $term->name;
1596
1597
						// Let's determine if this term is active or not.
1598
1599 View Code Duplication
						if ( in_array( $item['key'], $existing_term_slugs, true ) ) {
1600
							$active = true;
1601
1602
							$slug_count = count( $existing_term_slugs );
1603
1604
							if ( $slug_count > 1 ) {
1605
								$remove_url = Jetpack_Search_Helpers::add_query_arg(
1606
									$tax_query_var,
0 ignored issues
show
Bug introduced by
It seems like $tax_query_var can also be of type boolean; however, Jetpack_Search_Helpers::add_query_arg() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1607
									rawurlencode( implode( '+', array_diff( $existing_term_slugs, array( $item['key'] ) ) ) )
1608
								);
1609
							} else {
1610
								$remove_url = Jetpack_Search_Helpers::remove_query_arg( $tax_query_var );
0 ignored issues
show
Bug introduced by
It seems like $tax_query_var can also be of type boolean; however, Jetpack_Search_Helpers::remove_query_arg() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1611
							}
1612
						}
1613
1614
						break;
1615
1616
					case 'post_type':
1617
						$post_type = get_post_type_object( $item['key'] );
1618
1619
						if ( ! $post_type || $post_type->exclude_from_search ) {
1620
							continue 2;  // switch() is considered a looping structure.
1621
						}
1622
1623
						$query_vars = array(
1624
							'post_type' => $item['key'],
1625
						);
1626
1627
						$name = $post_type->labels->singular_name;
1628
1629
						// Is this post type active on this search?
1630
						$post_types = $query->get( 'post_type' );
1631
1632
						if ( ! is_array( $post_types ) ) {
1633
							$post_types = array( $post_types );
1634
						}
1635
1636 View Code Duplication
						if ( in_array( $item['key'], $post_types, true ) ) {
1637
							$active = true;
1638
1639
							$post_type_count = count( $post_types );
1640
1641
							// For the right 'remove filter' url, we need to remove the post type from the array, or remove the param entirely if it's the only one.
1642
							if ( $post_type_count > 1 ) {
1643
								$remove_url = Jetpack_Search_Helpers::add_query_arg(
1644
									'post_type',
1645
									rawurlencode( implode( ',', array_diff( $post_types, array( $item['key'] ) ) ) )
1646
								);
1647
							} else {
1648
								$remove_url = Jetpack_Search_Helpers::remove_query_arg( 'post_type' );
1649
							}
1650
						}
1651
1652
						break;
1653
1654
					case 'date_histogram':
1655
						$timestamp = $item['key'] / 1000;
1656
1657
						$current_year  = $query->get( 'year' );
1658
						$current_month = $query->get( 'monthnum' );
1659
						$current_day   = $query->get( 'day' );
1660
1661
						switch ( $this->aggregations[ $label ]['interval'] ) {
1662
							case 'year':
1663
								$year = (int) gmdate( 'Y', $timestamp );
1664
1665
								$query_vars = array(
1666
									'year'     => $year,
1667
									'monthnum' => false,
1668
									'day'      => false,
1669
								);
1670
1671
								$name = $year;
1672
1673
								// Is this year currently selected?
1674
								if ( ! empty( $current_year ) && (int) $current_year === $year ) {
1675
									$active = true;
1676
1677
									$remove_url = Jetpack_Search_Helpers::remove_query_arg( array( 'year', 'monthnum', 'day' ) );
1678
								}
1679
1680
								break;
1681
1682
							case 'month':
1683
								$year  = (int) gmdate( 'Y', $timestamp );
1684
								$month = (int) gmdate( 'n', $timestamp );
1685
1686
								$query_vars = array(
1687
									'year'     => $year,
1688
									'monthnum' => $month,
1689
									'day'      => false,
1690
								);
1691
1692
								$name = gmdate( 'F Y', $timestamp );
1693
1694
								// Is this month currently selected?
1695
								if ( ! empty( $current_year ) && (int) $current_year === $year &&
1696
									! empty( $current_month ) && (int) $current_month === $month ) {
1697
									$active = true;
1698
1699
									$remove_url = Jetpack_Search_Helpers::remove_query_arg( array( 'year', 'monthnum' ) );
1700
								}
1701
1702
								break;
1703
1704
							case 'day':
1705
								$year  = (int) gmdate( 'Y', $timestamp );
1706
								$month = (int) gmdate( 'n', $timestamp );
1707
								$day   = (int) gmdate( 'j', $timestamp );
1708
1709
								$query_vars = array(
1710
									'year'     => $year,
1711
									'monthnum' => $month,
1712
									'day'      => $day,
1713
								);
1714
1715
								$name = gmdate( 'F jS, Y', $timestamp );
1716
1717
								// Is this day currently selected?
1718
								if ( ! empty( $current_year ) && (int) $current_year === $year &&
1719
									! empty( $current_month ) && (int) $current_month === $month &&
1720
									! empty( $current_day ) && (int) $current_day === $day ) {
1721
									$active = true;
1722
1723
									$remove_url = Jetpack_Search_Helpers::remove_query_arg( array( 'day' ) );
1724
								}
1725
1726
								break;
1727
1728
							default:
1729
								continue 3; // switch() is considered a looping structure.
1730
						} // End switch.
1731
1732
						break;
1733
1734
					default:
1735
						// continue 2; // switch() is considered a looping structure.
1736
				} // End switch.
1737
1738
				// Need to urlencode param values since add_query_arg doesn't.
1739
				$url_params = urlencode_deep( $query_vars );
1740
1741
				$aggregation_data[ $label ]['buckets'][] = array(
1742
					'url'        => Jetpack_Search_Helpers::add_query_arg( $url_params ),
1743
					'query_vars' => $query_vars,
1744
					'name'       => $name,
1745
					'count'      => $item['doc_count'],
1746
					'active'     => $active,
1747
					'remove_url' => $remove_url,
1748
					'type'       => $type,
1749
					'type_label' => $aggregation_data[ $label ]['name'],
1750
					'widget_id'  => ! empty( $aggregation_data[ $label ]['widget_id'] ) ? $aggregation_data[ $label ]['widget_id'] : 0,
1751
				);
1752
			} // End foreach.
1753
		} // End foreach.
1754
1755
		/**
1756
		 * Modify the aggregation filters returned by get_filters().
1757
		 *
1758
		 * Useful if you are setting custom filters outside of the supported filters (taxonomy, post_type etc.) and
1759
		 * want to hook them up so they're returned when you call `get_filters()`.
1760
		 *
1761
		 * @module search
1762
		 *
1763
		 * @since  6.9.0
1764
		 *
1765
		 * @param array    $aggregation_data The array of filters keyed on label.
1766
		 * @param WP_Query $query            The WP_Query object.
1767
		 */
1768
		return apply_filters( 'jetpack_search_get_filters', $aggregation_data, $query );
1769
	}
1770
1771
	/**
1772
	 * Get the results of the facets performed.
1773
	 *
1774
	 * @deprecated 5.0 Please use Jetpack_Search::get_filters() instead.
1775
	 *
1776
	 * @see        Jetpack_Search::get_filters()
1777
	 *
1778
	 * @return array $facets Array of facets applied and info about them.
1779
	 */
1780
	public function get_search_facet_data() {
1781
		_deprecated_function( __METHOD__, 'jetpack-5.0', 'Jetpack_Search::get_filters()' );
1782
1783
		return $this->get_filters();
1784
	}
1785
1786
	/**
1787
	 * Get the filters that are currently applied to this search.
1788
	 *
1789
	 * @since 5.0.0
1790
	 *
1791
	 * @return array Array of filters that were applied.
1792
	 */
1793
	public function get_active_filter_buckets() {
1794
		$active_buckets = array();
1795
1796
		$filters = $this->get_filters();
1797
1798
		if ( ! is_array( $filters ) ) {
1799
			return $active_buckets;
1800
		}
1801
1802
		foreach ( $filters as $filter ) {
1803
			if ( isset( $filter['buckets'] ) && is_array( $filter['buckets'] ) ) {
1804
				foreach ( $filter['buckets'] as $item ) {
1805
					if ( isset( $item['active'] ) && $item['active'] ) {
1806
						$active_buckets[] = $item;
1807
					}
1808
				}
1809
			}
1810
		}
1811
1812
		return $active_buckets;
1813
	}
1814
1815
	/**
1816
	 * Get the filters that are currently applied to this search.
1817
	 *
1818
	 * @deprecated 5.0 Please use Jetpack_Search::get_active_filter_buckets() instead.
1819
	 *
1820
	 * @see        Jetpack_Search::get_active_filter_buckets()
1821
	 *
1822
	 * @return array Array of filters that were applied.
1823
	 */
1824
	public function get_current_filters() {
1825
		_deprecated_function( __METHOD__, 'jetpack-5.0', 'Jetpack_Search::get_active_filter_buckets()' );
1826
1827
		return $this->get_active_filter_buckets();
1828
	}
1829
1830
	/**
1831
	 * Calculate the right query var to use for a given taxonomy.
1832
	 *
1833
	 * Allows custom code to modify the GET var that is used to represent a given taxonomy, via the jetpack_search_taxonomy_query_var filter.
1834
	 *
1835
	 * @since 5.0.0
1836
	 *
1837
	 * @param string $taxonomy_name The name of the taxonomy for which to get the query var.
1838
	 *
1839
	 * @return bool|string The query var to use for this taxonomy, or false if none found.
1840
	 */
1841
	public function get_taxonomy_query_var( $taxonomy_name ) {
1842
		$taxonomy = get_taxonomy( $taxonomy_name );
1843
1844
		if ( ! $taxonomy || is_wp_error( $taxonomy ) ) {
1845
			return false;
1846
		}
1847
1848
		/**
1849
		 * Modify the query var to use for a given taxonomy
1850
		 *
1851
		 * @module search
1852
		 *
1853
		 * @since  5.0.0
1854
		 *
1855
		 * @param string $query_var     The current query_var for the taxonomy
1856
		 * @param string $taxonomy_name The taxonomy name
1857
		 */
1858
		return apply_filters( 'jetpack_search_taxonomy_query_var', $taxonomy->query_var, $taxonomy_name );
1859
	}
1860
1861
	/**
1862
	 * Takes an array of aggregation results, and ensures the array key ordering matches the key order in $desired
1863
	 * which is the input order.
1864
	 *
1865
	 * Necessary because ES does not always return aggregations in the same order that you pass them in,
1866
	 * and it should be possible to control the display order easily.
1867
	 *
1868
	 * @since 5.0.0
1869
	 *
1870
	 * @param array $aggregations Aggregation results to be reordered.
1871
	 * @param array $desired      Array with keys representing the desired ordering.
1872
	 *
1873
	 * @return array A new array with reordered keys, matching those in $desired.
1874
	 */
1875
	public function fix_aggregation_ordering( array $aggregations, array $desired ) {
1876
		if ( empty( $aggregations ) || empty( $desired ) ) {
1877
			return $aggregations;
1878
		}
1879
1880
		$reordered = array();
1881
1882
		foreach ( array_keys( $desired ) as $agg_name ) {
1883
			if ( isset( $aggregations[ $agg_name ] ) ) {
1884
				$reordered[ $agg_name ] = $aggregations[ $agg_name ];
1885
			}
1886
		}
1887
1888
		return $reordered;
1889
	}
1890
1891
	/**
1892
	 * Sends events to Tracks when a search filters widget is updated.
1893
	 *
1894
	 * @since 5.8.0
1895
	 *
1896
	 * @param string $option    The option name. Only "widget_jetpack-search-filters" is cared about.
1897
	 * @param array  $old_value The old option value.
1898
	 * @param array  $new_value The new option value.
1899
	 */
1900
	public function track_widget_updates( $option, $old_value, $new_value ) {
1901
		if ( 'widget_jetpack-search-filters' !== $option ) {
1902
			return;
1903
		}
1904
1905
		$event = Jetpack_Search_Helpers::get_widget_tracks_value( $old_value, $new_value );
1906
		if ( ! $event ) {
1907
			return;
1908
		}
1909
1910
		$tracking = new Automattic\Jetpack\Tracking();
1911
		$tracking->tracks_record_event(
1912
			wp_get_current_user(),
1913
			sprintf( 'jetpack_search_widget_%s', $event['action'] ),
1914
			$event['widget']
1915
		);
1916
	}
1917
1918
	/**
1919
	 * Moves any active search widgets to the inactive category.
1920
	 *
1921
	 * @since 5.9.0
1922
	 *
1923
	 * @param string $module Unused. The Jetpack module being disabled.
1924
	 */
1925
	public function move_search_widgets_to_inactive( $module ) {
1926
		if ( ! is_active_widget( false, false, Jetpack_Search_Helpers::FILTER_WIDGET_BASE, true ) ) {
1927
			return;
1928
		}
1929
1930
		$sidebars_widgets = wp_get_sidebars_widgets();
1931
1932
		if ( ! is_array( $sidebars_widgets ) ) {
1933
			return;
1934
		}
1935
1936
		$changed = false;
1937
1938
		foreach ( $sidebars_widgets as $sidebar => $widgets ) {
1939
			if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
1940
				continue;
1941
			}
1942
1943
			if ( is_array( $widgets ) ) {
1944
				foreach ( $widgets as $key => $widget ) {
1945
					if ( _get_widget_id_base( $widget ) === Jetpack_Search_Helpers::FILTER_WIDGET_BASE ) {
1946
						$changed = true;
1947
1948
						array_unshift( $sidebars_widgets['wp_inactive_widgets'], $widget );
1949
						unset( $sidebars_widgets[ $sidebar ][ $key ] );
1950
					}
1951
				}
1952
			}
1953
		}
1954
1955
		if ( $changed ) {
1956
			wp_set_sidebars_widgets( $sidebars_widgets );
1957
		}
1958
	}
1959
1960
	/**
1961
	 * Determine whether a given WP_Query should be handled by ElasticSearch.
1962
	 *
1963
	 * @param WP_Query $query The WP_Query object.
1964
	 *
1965
	 * @return bool
1966
	 */
1967
	public function should_handle_query( $query ) {
1968
		/**
1969
		 * Determine whether a given WP_Query should be handled by ElasticSearch.
1970
		 *
1971
		 * @module search
1972
		 *
1973
		 * @since  5.6.0
1974
		 *
1975
		 * @param bool     $should_handle Should be handled by Jetpack Search.
1976
		 * @param WP_Query $query         The WP_Query object.
1977
		 */
1978
		return apply_filters( 'jetpack_search_should_handle_query', $query->is_main_query() && $query->is_search(), $query );
1979
	}
1980
1981
	/**
1982
	 * Transforms an array with fields name as keys and boosts as value into
1983
	 * shorthand "caret" format.
1984
	 *
1985
	 * @param array $fields_boost [ "title" => "2", "content" => "1" ].
1986
	 *
1987
	 * @return array [ "title^2", "content^1" ]
1988
	 */
1989
	private function _get_caret_boosted_fields( array $fields_boost ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
1990
		$caret_boosted_fields = array();
1991
		foreach ( $fields_boost as $field => $boost ) {
1992
			$caret_boosted_fields[] = "$field^$boost";
1993
		}
1994
		return $caret_boosted_fields;
1995
	}
1996
1997
	/**
1998
	 * Apply a multiplier to boost values.
1999
	 *
2000
	 * @param array $fields_boost [ "title" => 2, "content" => 1 ].
2001
	 * @param array $fields_boost_multiplier [ "title" => 0.1234 ].
2002
	 *
2003
	 * @return array [ "title" => "0.247", "content" => "1.000" ]
2004
	 */
2005
	private function _apply_boosts_multiplier( array $fields_boost, array $fields_boost_multiplier ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
2006
		foreach ( $fields_boost as $field_name => $field_boost ) {
2007
			if ( isset( $fields_boost_multiplier[ $field_name ] ) ) {
2008
				$fields_boost[ $field_name ] *= $fields_boost_multiplier[ $field_name ];
2009
			}
2010
2011
			// Set a floor and format the number as string.
2012
			$fields_boost[ $field_name ] = number_format(
2013
				max( 0.001, $fields_boost[ $field_name ] ),
2014
				3,
2015
				'.',
2016
				''
2017
			);
2018
		}
2019
2020
		return $fields_boost;
2021
	}
2022
}
2023