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 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 |
||
19 | class Jetpack_Search { |
||
20 | |||
21 | /** |
||
22 | * The number of found posts. |
||
23 | * |
||
24 | * @since 5.0.0 |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $found_posts = 0; |
||
29 | |||
30 | /** |
||
31 | * The search result, as returned by the WordPress.com REST API. |
||
32 | * |
||
33 | * @since 5.0.0 |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $search_result; |
||
38 | |||
39 | /** |
||
40 | * This site's blog ID on WordPress.com. |
||
41 | * |
||
42 | * @since 5.0.0 |
||
43 | * |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $jetpack_blog_id; |
||
47 | |||
48 | /** |
||
49 | * The Elasticsearch aggregations (filters). |
||
50 | * |
||
51 | * @since 5.0.0 |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $aggregations = array(); |
||
56 | |||
57 | /** |
||
58 | * The maximum number of aggregations allowed. |
||
59 | * |
||
60 | * @since 5.0.0 |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | protected $max_aggregations_count = 100; |
||
65 | |||
66 | /** |
||
67 | * Statistics about the last Elasticsearch query. |
||
68 | * |
||
69 | * @since 5.6.0 |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $last_query_info = array(); |
||
74 | |||
75 | /** |
||
76 | * Statistics about the last Elasticsearch query failure. |
||
77 | * |
||
78 | * @since 5.6.0 |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $last_query_failure_info = array(); |
||
83 | |||
84 | /** |
||
85 | * The singleton instance of this class. |
||
86 | * |
||
87 | * @since 5.0.0 |
||
88 | * |
||
89 | * @var Jetpack_Search |
||
90 | */ |
||
91 | protected static $instance; |
||
92 | |||
93 | /** |
||
94 | * Languages with custom analyzers. Other languages are supported, but are analyzed with the default analyzer. |
||
95 | * |
||
96 | * @since 5.0.0 |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | 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' ); |
||
101 | |||
102 | /** |
||
103 | * Jetpack_Search constructor. |
||
104 | * |
||
105 | * @since 5.0.0 |
||
106 | * |
||
107 | * Doesn't do anything. This class needs to be initialized via the instance() method instead. |
||
108 | */ |
||
109 | protected function __construct() { |
||
111 | |||
112 | /** |
||
113 | * Prevent __clone()'ing of this class. |
||
114 | * |
||
115 | * @since 5.0.0 |
||
116 | */ |
||
117 | public function __clone() { |
||
120 | |||
121 | /** |
||
122 | * Prevent __wakeup()'ing of this class. |
||
123 | * |
||
124 | * @since 5.0.0 |
||
125 | */ |
||
126 | public function __wakeup() { |
||
129 | |||
130 | /** |
||
131 | * Get singleton instance of Jetpack_Search. |
||
132 | * |
||
133 | * Instantiates and sets up a new instance if needed, or returns the singleton. |
||
134 | * |
||
135 | * @since 5.0.0 |
||
136 | * |
||
137 | * @return Jetpack_Search The Jetpack_Search singleton. |
||
138 | */ |
||
139 | public static function instance() { |
||
148 | |||
149 | /** |
||
150 | * Perform various setup tasks for the class. |
||
151 | * |
||
152 | * Checks various pre-requisites and adds hooks. |
||
153 | * |
||
154 | * @since 5.0.0 |
||
155 | */ |
||
156 | public function setup() { |
||
185 | |||
186 | /** |
||
187 | * Setup the various hooks needed for the plugin to take over search duties. |
||
188 | * |
||
189 | * @since 5.0.0 |
||
190 | */ |
||
191 | public function init_hooks() { |
||
210 | |||
211 | /** |
||
212 | * Loads assets for Jetpack Instant Search Prototype featuring Search As You Type experience. |
||
213 | */ |
||
214 | public function load_assets() { |
||
285 | |||
286 | /** |
||
287 | * Is search supported on the current plan |
||
288 | * |
||
289 | * @since 6.0 |
||
290 | * Loads scripts for Tracks analytics library |
||
291 | */ |
||
292 | public function is_search_supported() { |
||
293 | if ( method_exists( 'Jetpack_Plan', 'supports' ) ) { |
||
294 | return Jetpack_Plan::supports( 'search' ); |
||
295 | } |
||
296 | return false; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Does this site have a VIP index |
||
301 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
302 | * |
||
303 | * @since 6.0 |
||
304 | * @return string $script_version Version number. |
||
305 | */ |
||
306 | public function has_vip_index() { |
||
307 | return defined( 'JETPACK_SEARCH_VIP_INDEX' ) && JETPACK_SEARCH_VIP_INDEX; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Loads scripts for Tracks analytics library |
||
312 | */ |
||
313 | public function load_and_initialize_tracks() { |
||
314 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
319 | * |
||
320 | * @param string $file Path of the file we are looking for. |
||
321 | * @return string $script_version Version number. |
||
322 | */ |
||
323 | public static function get_asset_version( $file ) { |
||
324 | return Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $file ) |
||
325 | ? filemtime( JETPACK__PLUGIN_DIR . $file ) |
||
326 | : JETPACK__VERSION; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * When an Elasticsearch query fails, this stores it and enqueues some debug information in the footer. |
||
331 | * |
||
332 | * @since 5.6.0 |
||
333 | * |
||
334 | * @param array $meta Information about the failure. |
||
335 | */ |
||
336 | public function store_query_failure( $meta ) { |
||
337 | $this->last_query_failure_info = $meta; |
||
338 | add_action( 'wp_footer', array( $this, 'print_query_failure' ) ); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Outputs information about the last Elasticsearch failure. |
||
343 | * |
||
344 | * @since 5.6.0 |
||
345 | */ |
||
346 | public function print_query_failure() { |
||
347 | if ( $this->last_query_failure_info ) { |
||
|
|||
348 | printf( |
||
349 | '<!-- Jetpack Search failed with code %s: %s - %s -->', |
||
350 | esc_html( $this->last_query_failure_info['response_code'] ), |
||
351 | esc_html( $this->last_query_failure_info['json']['error'] ), |
||
352 | esc_html( $this->last_query_failure_info['json']['message'] ) |
||
353 | ); |
||
354 | } |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Stores information about the last Elasticsearch query and enqueues some debug information in the footer. |
||
359 | * |
||
360 | * @since 5.6.0 |
||
361 | * |
||
362 | * @param array $meta Information about the query. |
||
363 | */ |
||
364 | public function store_last_query_info( $meta ) { |
||
365 | $this->last_query_info = $meta; |
||
366 | add_action( 'wp_footer', array( $this, 'print_query_success' ) ); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Outputs information about the last Elasticsearch search. |
||
371 | * |
||
372 | * @since 5.6.0 |
||
373 | */ |
||
374 | public function print_query_success() { |
||
375 | if ( $this->last_query_info ) { |
||
376 | printf( |
||
377 | '<!-- Jetpack Search took %s ms, ES time %s ms -->', |
||
378 | intval( $this->last_query_info['elapsed_time'] ), |
||
379 | esc_html( $this->last_query_info['es_time'] ) |
||
380 | ); |
||
381 | |||
382 | if ( isset( $_GET['searchdebug'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
383 | printf( |
||
384 | '<!-- Query response data: %s -->', |
||
385 | esc_html( print_r( $this->last_query_info, 1 ) ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
||
386 | ); |
||
387 | } |
||
388 | } |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Returns the last query information, or false if no information was stored. |
||
393 | * |
||
394 | * @since 5.8.0 |
||
395 | * |
||
396 | * @return bool|array |
||
397 | */ |
||
398 | public function get_last_query_info() { |
||
399 | return empty( $this->last_query_info ) ? false : $this->last_query_info; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Returns the last query failure information, or false if no failure information was stored. |
||
404 | * |
||
405 | * @since 5.8.0 |
||
406 | * |
||
407 | * @return bool|array |
||
408 | */ |
||
409 | public function get_last_query_failure_info() { |
||
410 | return empty( $this->last_query_failure_info ) ? false : $this->last_query_failure_info; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Wraps a WordPress filter called "jetpack_search_disable_widget_filters" that allows |
||
415 | * developers to disable filters supplied by the search widget. Useful if filters are |
||
416 | * being defined at the code level. |
||
417 | * |
||
418 | * @since 5.7.0 |
||
419 | * @deprecated 5.8.0 Use Jetpack_Search_Helpers::are_filters_by_widget_disabled() directly. |
||
420 | * |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function are_filters_by_widget_disabled() { |
||
424 | return Jetpack_Search_Helpers::are_filters_by_widget_disabled(); |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Retrieves a list of known Jetpack search filters widget IDs, gets the filters for each widget, |
||
429 | * and applies those filters to this Jetpack_Search object. |
||
430 | * |
||
431 | * @since 5.7.0 |
||
432 | */ |
||
433 | public function set_filters_from_widgets() { |
||
434 | if ( Jetpack_Search_Helpers::are_filters_by_widget_disabled() ) { |
||
435 | return; |
||
436 | } |
||
437 | |||
438 | $filters = Jetpack_Search_Helpers::get_filters_from_widgets(); |
||
439 | |||
440 | if ( ! empty( $filters ) ) { |
||
441 | $this->set_filters( $filters ); |
||
442 | } |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Restricts search results to certain post types via a GET argument. |
||
447 | * |
||
448 | * @since 5.8.0 |
||
449 | * |
||
450 | * @param WP_Query $query A WP_Query instance. |
||
451 | */ |
||
452 | public function maybe_add_post_type_as_var( WP_Query $query ) { |
||
453 | $post_type = ( ! empty( $_GET['post_type'] ) ) ? $_GET['post_type'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
454 | if ( $this->should_handle_query( $query ) && $post_type ) { |
||
455 | $post_types = ( is_string( $post_type ) && false !== strpos( $post_type, ',' ) ) |
||
456 | ? explode( ',', $post_type ) |
||
457 | : (array) $post_type; |
||
458 | $post_types = array_map( 'sanitize_key', $post_types ); |
||
459 | $query->set( 'post_type', $post_types ); |
||
460 | } |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Run a search on the WordPress.com public API. |
||
465 | * |
||
466 | * @since 5.0.0 |
||
467 | * |
||
468 | * @param array $es_args Args conforming to the WP.com /sites/<blog_id>/search endpoint. |
||
469 | * |
||
470 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
471 | */ |
||
472 | public function search( array $es_args ) { |
||
473 | $endpoint = sprintf( '/sites/%s/search', $this->jetpack_blog_id ); |
||
474 | $service_url = 'https://public-api.wordpress.com/rest/v1' . $endpoint; |
||
475 | |||
476 | $do_authenticated_request = false; |
||
477 | |||
478 | if ( class_exists( 'Automattic\\Jetpack\\Connection\\Client' ) && |
||
479 | isset( $es_args['authenticated_request'] ) && |
||
480 | true === $es_args['authenticated_request'] ) { |
||
481 | $do_authenticated_request = true; |
||
482 | } |
||
483 | |||
484 | unset( $es_args['authenticated_request'] ); |
||
485 | |||
486 | $request_args = array( |
||
487 | 'headers' => array( |
||
488 | 'Content-Type' => 'application/json', |
||
489 | ), |
||
490 | 'timeout' => 10, |
||
491 | 'user-agent' => 'jetpack_search', |
||
492 | ); |
||
493 | |||
494 | $request_body = wp_json_encode( $es_args ); |
||
495 | |||
496 | $start_time = microtime( true ); |
||
497 | |||
498 | if ( $do_authenticated_request ) { |
||
499 | $request_args['method'] = 'POST'; |
||
500 | |||
501 | $request = Client::wpcom_json_api_request_as_blog( $endpoint, Client::WPCOM_JSON_API_VERSION, $request_args, $request_body ); |
||
502 | } else { |
||
503 | $request_args = array_merge( |
||
504 | $request_args, |
||
505 | array( |
||
506 | 'body' => $request_body, |
||
507 | ) |
||
508 | ); |
||
509 | |||
510 | $request = wp_remote_post( $service_url, $request_args ); |
||
511 | } |
||
512 | |||
513 | $end_time = microtime( true ); |
||
514 | |||
515 | if ( is_wp_error( $request ) ) { |
||
516 | return $request; |
||
517 | } |
||
518 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
519 | |||
520 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
521 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
522 | } |
||
523 | |||
524 | $response = json_decode( wp_remote_retrieve_body( $request ), true ); |
||
525 | |||
526 | $took = is_array( $response ) && ! empty( $response['took'] ) |
||
527 | ? $response['took'] |
||
528 | : null; |
||
529 | |||
530 | $query = array( |
||
531 | 'args' => $es_args, |
||
532 | 'response' => $response, |
||
533 | 'response_code' => $response_code, |
||
534 | 'elapsed_time' => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms. |
||
535 | 'es_time' => $took, |
||
536 | 'url' => $service_url, |
||
537 | ); |
||
538 | |||
539 | /** |
||
540 | * Fires after a search request has been performed. |
||
541 | * |
||
542 | * Includes the following info in the $query parameter: |
||
543 | * |
||
544 | * array args Array of Elasticsearch arguments for the search |
||
545 | * array response Raw API response, JSON decoded |
||
546 | * int response_code HTTP response code of the request |
||
547 | * float elapsed_time Roundtrip time of the search request, in milliseconds |
||
548 | * float es_time Amount of time Elasticsearch spent running the request, in milliseconds |
||
549 | * string url API url that was queried |
||
550 | * |
||
551 | * @module search |
||
552 | * |
||
553 | * @since 5.0.0 |
||
554 | * @since 5.8.0 This action now fires on all queries instead of just successful queries. |
||
555 | * |
||
556 | * @param array $query Array of information about the query performed |
||
557 | */ |
||
558 | do_action( 'did_jetpack_search_query', $query ); |
||
559 | |||
560 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
561 | /** |
||
562 | * Fires after a search query request has failed |
||
563 | * |
||
564 | * @module search |
||
565 | * |
||
566 | * @since 5.6.0 |
||
567 | * |
||
568 | * @param array Array containing the response code and response from the failed search query |
||
569 | */ |
||
570 | do_action( |
||
571 | 'failed_jetpack_search_query', |
||
572 | array( |
||
573 | 'response_code' => $response_code, |
||
574 | 'json' => $response, |
||
575 | ) |
||
576 | ); |
||
577 | |||
578 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
579 | } |
||
580 | |||
581 | return $response; |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Bypass the normal Search query and offload it to Jetpack servers. |
||
586 | * |
||
587 | * This is the main hook of the plugin and is responsible for returning the posts that match the search query. |
||
588 | * |
||
589 | * @since 5.0.0 |
||
590 | * |
||
591 | * @param array $posts Current array of posts (still pre-query). |
||
592 | * @param WP_Query $query The WP_Query being filtered. |
||
593 | * |
||
594 | * @return array Array of matching posts. |
||
595 | */ |
||
596 | public function filter__posts_pre_query( $posts, $query ) { |
||
639 | |||
640 | /** |
||
641 | * Build up the search, then run it against the Jetpack servers. |
||
642 | * |
||
643 | * @since 5.0.0 |
||
644 | * |
||
645 | * @param WP_Query $query The original WP_Query to use for the parameters of our search. |
||
646 | */ |
||
647 | public function do_search( WP_Query $query ) { |
||
757 | |||
758 | /** |
||
759 | * If the query has already been run before filters have been updated, then we need to re-run the query |
||
760 | * to get the latest aggregations. |
||
761 | * |
||
762 | * This is especially useful for supporting widget management in the customizer. |
||
763 | * |
||
764 | * @since 5.8.0 |
||
765 | * |
||
766 | * @return bool Whether the query was successful or not. |
||
767 | */ |
||
768 | public function update_search_results_aggregations() { |
||
782 | |||
783 | /** |
||
784 | * Given a WP_Query, convert its WP_Tax_Query (if present) into the WP-style Elasticsearch term arguments for the search. |
||
785 | * |
||
786 | * @since 5.0.0 |
||
787 | * |
||
788 | * @param WP_Query $query The original WP_Query object for which to parse the taxonomy query. |
||
789 | * |
||
790 | * @return array The new WP-style Elasticsearch arguments (that will be converted into 'real' Elasticsearch arguments). |
||
791 | */ |
||
792 | public function get_es_wp_query_terms_for_query( WP_Query $query ) { |
||
824 | |||
825 | /** |
||
826 | * Parse out the post type from a WP_Query. |
||
827 | * |
||
828 | * Only allows post types that are not marked as 'exclude_from_search'. |
||
829 | * |
||
830 | * @since 5.0.0 |
||
831 | * |
||
832 | * @param WP_Query $query Original WP_Query object. |
||
833 | * |
||
834 | * @return array Array of searchable post types corresponding to the original query. |
||
835 | */ |
||
836 | public function get_es_wp_query_post_type_for_query( WP_Query $query ) { |
||
874 | |||
875 | /** |
||
876 | * Initialize widgets for the Search module (on wp.com only). |
||
877 | * |
||
878 | * @module search |
||
879 | */ |
||
880 | public function action__widgets_init() { |
||
885 | |||
886 | /** |
||
887 | * Get the Elasticsearch result. |
||
888 | * |
||
889 | * @since 5.0.0 |
||
890 | * |
||
891 | * @param bool $raw If true, does not check for WP_Error or return the 'results' array - the JSON decoded HTTP response. |
||
892 | * |
||
893 | * @return array|bool The search results, or false if there was a failure. |
||
894 | */ |
||
895 | public function get_search_result( $raw = false ) { |
||
902 | |||
903 | /** |
||
904 | * Add the date portion of a WP_Query onto the query args. |
||
905 | * |
||
906 | * @since 5.0.0 |
||
907 | * |
||
908 | * @param array $es_wp_query_args The Elasticsearch query arguments in WordPress form. |
||
909 | * @param WP_Query $query The original WP_Query. |
||
910 | * |
||
911 | * @return array The es wp query args, with date filters added (as needed). |
||
912 | */ |
||
913 | public function filter__add_date_filter_to_query( array $es_wp_query_args, WP_Query $query ) { |
||
945 | |||
946 | /** |
||
947 | * Converts WP_Query style args to Elasticsearch args. |
||
948 | * |
||
949 | * @since 5.0.0 |
||
950 | * |
||
951 | * @param array $args Array of WP_Query style arguments. |
||
952 | * |
||
953 | * @return array Array of ES style query arguments. |
||
954 | */ |
||
955 | public function convert_wp_es_to_es_args( array $args ) { |
||
1347 | |||
1348 | /** |
||
1349 | * Given an array of aggregations, parse and add them onto the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
1350 | * |
||
1351 | * @since 5.0.0 |
||
1352 | * |
||
1353 | * @param array $aggregations Array of aggregations (filters) to add to the Jetpack_WPES_Query_Builder. |
||
1354 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
1355 | */ |
||
1356 | public function add_aggregations_to_es_query_builder( array $aggregations, Jetpack_WPES_Query_Builder $builder ) { |
||
1376 | |||
1377 | /** |
||
1378 | * Given an individual taxonomy aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
1379 | * |
||
1380 | * @since 5.0.0 |
||
1381 | * |
||
1382 | * @param array $aggregation The aggregation to add to the query builder. |
||
1383 | * @param string $label The 'label' (unique id) for this aggregation. |
||
1384 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
1385 | */ |
||
1386 | public function add_taxonomy_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
1413 | |||
1414 | /** |
||
1415 | * Given an individual post_type aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
1416 | * |
||
1417 | * @since 5.0.0 |
||
1418 | * |
||
1419 | * @param array $aggregation The aggregation to add to the query builder. |
||
1420 | * @param string $label The 'label' (unique id) for this aggregation. |
||
1421 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
1422 | */ |
||
1423 | public function add_post_type_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
1434 | |||
1435 | /** |
||
1436 | * Given an individual date_histogram aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
1437 | * |
||
1438 | * @since 5.0.0 |
||
1439 | * |
||
1440 | * @param array $aggregation The aggregation to add to the query builder. |
||
1441 | * @param string $label The 'label' (unique id) for this aggregation. |
||
1442 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
1443 | */ |
||
1444 | public function add_date_histogram_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
1463 | |||
1464 | /** |
||
1465 | * And an existing filter object with a list of additional filters. |
||
1466 | * |
||
1467 | * Attempts to optimize the filters somewhat. |
||
1468 | * |
||
1469 | * @since 5.0.0 |
||
1470 | * |
||
1471 | * @param array $curr_filter The existing filters to build upon. |
||
1472 | * @param array $filters The new filters to add. |
||
1473 | * |
||
1474 | * @return array The resulting merged filters. |
||
1475 | */ |
||
1476 | public static function and_es_filters( array $curr_filter, array $filters ) { |
||
1491 | |||
1492 | /** |
||
1493 | * Set the available filters for the search. |
||
1494 | * |
||
1495 | * These get rendered via the Jetpack_Search_Widget() widget. |
||
1496 | * |
||
1497 | * Behind the scenes, these are implemented using Elasticsearch Aggregations. |
||
1498 | * |
||
1499 | * If you do not require counts of how many documents match each filter, please consider using regular WP Query |
||
1500 | * arguments instead, such as via the jetpack_search_es_wp_query_args filter |
||
1501 | * |
||
1502 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
1503 | * |
||
1504 | * @since 5.0.0 |
||
1505 | * |
||
1506 | * @param array $aggregations Array of filters (aggregations) to apply to the search. |
||
1507 | */ |
||
1508 | public function set_filters( array $aggregations ) { |
||
1516 | |||
1517 | /** |
||
1518 | * Set the search's facets (deprecated). |
||
1519 | * |
||
1520 | * @deprecated 5.0 Please use Jetpack_Search::set_filters() instead. |
||
1521 | * |
||
1522 | * @see Jetpack_Search::set_filters() |
||
1523 | * |
||
1524 | * @param array $facets Array of facets to apply to the search. |
||
1525 | */ |
||
1526 | public function set_facets( array $facets ) { |
||
1531 | |||
1532 | /** |
||
1533 | * Get the raw Aggregation results from the Elasticsearch response. |
||
1534 | * |
||
1535 | * @since 5.0.0 |
||
1536 | * |
||
1537 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
1538 | * |
||
1539 | * @return array Array of Aggregations performed on the search. |
||
1540 | */ |
||
1541 | public function get_search_aggregations_results() { |
||
1552 | |||
1553 | /** |
||
1554 | * Get the raw Facet results from the Elasticsearch response. |
||
1555 | * |
||
1556 | * @deprecated 5.0 Please use Jetpack_Search::get_search_aggregations_results() instead. |
||
1557 | * |
||
1558 | * @see Jetpack_Search::get_search_aggregations_results() |
||
1559 | * |
||
1560 | * @return array Array of Facets performed on the search. |
||
1561 | */ |
||
1562 | public function get_search_facets() { |
||
1567 | |||
1568 | /** |
||
1569 | * Get the results of the Filters performed, including the number of matching documents. |
||
1570 | * |
||
1571 | * Returns an array of Filters (keyed by $label, as passed to Jetpack_Search::set_filters()), containing the Filter and all resulting |
||
1572 | * matching buckets, the url for applying/removing each bucket, etc. |
||
1573 | * |
||
1574 | * NOTE - if this is called before the search is performed, an empty array will be returned. Use the $aggregations class |
||
1575 | * member if you need to access the raw filters set in Jetpack_Search::set_filters(). |
||
1576 | * |
||
1577 | * @since 5.0.0 |
||
1578 | * |
||
1579 | * @param WP_Query $query The optional original WP_Query to use for determining which filters are active. Defaults to the main query. |
||
1580 | * |
||
1581 | * @return array Array of filters applied and info about them. |
||
1582 | */ |
||
1583 | public function get_filters( WP_Query $query = null ) { |
||
1844 | |||
1845 | /** |
||
1846 | * Get the results of the facets performed. |
||
1847 | * |
||
1848 | * @deprecated 5.0 Please use Jetpack_Search::get_filters() instead. |
||
1849 | * |
||
1850 | * @see Jetpack_Search::get_filters() |
||
1851 | * |
||
1852 | * @return array $facets Array of facets applied and info about them. |
||
1853 | */ |
||
1854 | public function get_search_facet_data() { |
||
1859 | |||
1860 | /** |
||
1861 | * Get the filters that are currently applied to this search. |
||
1862 | * |
||
1863 | * @since 5.0.0 |
||
1864 | * |
||
1865 | * @return array Array of filters that were applied. |
||
1866 | */ |
||
1867 | public function get_active_filter_buckets() { |
||
1888 | |||
1889 | /** |
||
1890 | * Get the filters that are currently applied to this search. |
||
1891 | * |
||
1892 | * @deprecated 5.0 Please use Jetpack_Search::get_active_filter_buckets() instead. |
||
1893 | * |
||
1894 | * @see Jetpack_Search::get_active_filter_buckets() |
||
1895 | * |
||
1896 | * @return array Array of filters that were applied. |
||
1897 | */ |
||
1898 | public function get_current_filters() { |
||
1903 | |||
1904 | /** |
||
1905 | * Calculate the right query var to use for a given taxonomy. |
||
1906 | * |
||
1907 | * Allows custom code to modify the GET var that is used to represent a given taxonomy, via the jetpack_search_taxonomy_query_var filter. |
||
1908 | * |
||
1909 | * @since 5.0.0 |
||
1910 | * |
||
1911 | * @param string $taxonomy_name The name of the taxonomy for which to get the query var. |
||
1912 | * |
||
1913 | * @return bool|string The query var to use for this taxonomy, or false if none found. |
||
1914 | */ |
||
1915 | public function get_taxonomy_query_var( $taxonomy_name ) { |
||
1934 | |||
1935 | /** |
||
1936 | * Takes an array of aggregation results, and ensures the array key ordering matches the key order in $desired |
||
1937 | * which is the input order. |
||
1938 | * |
||
1939 | * Necessary because ES does not always return aggregations in the same order that you pass them in, |
||
1940 | * and it should be possible to control the display order easily. |
||
1941 | * |
||
1942 | * @since 5.0.0 |
||
1943 | * |
||
1944 | * @param array $aggregations Aggregation results to be reordered. |
||
1945 | * @param array $desired Array with keys representing the desired ordering. |
||
1946 | * |
||
1947 | * @return array A new array with reordered keys, matching those in $desired. |
||
1948 | */ |
||
1949 | public function fix_aggregation_ordering( array $aggregations, array $desired ) { |
||
1964 | |||
1965 | /** |
||
1966 | * Sends events to Tracks when a search filters widget is updated. |
||
1967 | * |
||
1968 | * @since 5.8.0 |
||
1969 | * |
||
1970 | * @param string $option The option name. Only "widget_jetpack-search-filters" is cared about. |
||
1971 | * @param array $old_value The old option value. |
||
1972 | * @param array $new_value The new option value. |
||
1973 | */ |
||
1974 | public function track_widget_updates( $option, $old_value, $new_value ) { |
||
1991 | |||
1992 | /** |
||
1993 | * Moves any active search widgets to the inactive category. |
||
1994 | * |
||
1995 | * @since 5.9.0 |
||
1996 | * |
||
1997 | * @param string $module Unused. The Jetpack module being disabled. |
||
1998 | */ |
||
1999 | public function move_search_widgets_to_inactive( $module ) { |
||
2033 | |||
2034 | /** |
||
2035 | * Determine whether a given WP_Query should be handled by ElasticSearch. |
||
2036 | * |
||
2037 | * @param WP_Query $query The WP_Query object. |
||
2038 | * |
||
2039 | * @return bool |
||
2040 | */ |
||
2041 | public function should_handle_query( $query ) { |
||
2054 | |||
2055 | /** |
||
2056 | * Transforms an array with fields name as keys and boosts as value into |
||
2057 | * shorthand "caret" format. |
||
2058 | * |
||
2059 | * @param array $fields_boost [ "title" => "2", "content" => "1" ]. |
||
2060 | * |
||
2061 | * @return array [ "title^2", "content^1" ] |
||
2062 | */ |
||
2063 | private function _get_caret_boosted_fields( array $fields_boost ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
||
2070 | |||
2071 | /** |
||
2072 | * Apply a multiplier to boost values. |
||
2073 | * |
||
2074 | * @param array $fields_boost [ "title" => 2, "content" => 1 ]. |
||
2075 | * @param array $fields_boost_multiplier [ "title" => 0.1234 ]. |
||
2076 | * |
||
2077 | * @return array [ "title" => "0.247", "content" => "1.000" ] |
||
2078 | */ |
||
2079 | private function _apply_boosts_multiplier( array $fields_boost, array $fields_boost_multiplier ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
||
2096 | } |
||
2097 |
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.