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 |
||
| 18 | class Jetpack_Search { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The number of found posts. |
||
| 22 | * |
||
| 23 | * @since 5.0.0 |
||
| 24 | * |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | protected $found_posts = 0; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The search result, as returned by the WordPress.com REST API. |
||
| 31 | * |
||
| 32 | * @since 5.0.0 |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $search_result; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * This site's blog ID on WordPress.com. |
||
| 40 | * |
||
| 41 | * @since 5.0.0 |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $jetpack_blog_id; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The Elasticsearch aggregations (filters). |
||
| 49 | * |
||
| 50 | * @since 5.0.0 |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $aggregations = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The maximum number of aggregations allowed. |
||
| 58 | * |
||
| 59 | * @since 5.0.0 |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $max_aggregations_count = 100; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Statistics about the last Elasticsearch query. |
||
| 67 | * |
||
| 68 | * @since 5.6.0 |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $last_query_info = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Statistics about the last Elasticsearch query failure. |
||
| 76 | * |
||
| 77 | * @since 5.6.0 |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $last_query_failure_info = array(); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The singleton instance of this class. |
||
| 85 | * |
||
| 86 | * @since 5.0.0 |
||
| 87 | * |
||
| 88 | * @var Jetpack_Search |
||
| 89 | */ |
||
| 90 | protected static $instance; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Languages with custom analyzers. Other languages are supported, but are analyzed with the default analyzer. |
||
| 94 | * |
||
| 95 | * @since 5.0.0 |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | 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' ); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Jetpack_Search constructor. |
||
| 103 | * |
||
| 104 | * @since 5.0.0 |
||
| 105 | * |
||
| 106 | * Doesn't do anything. This class needs to be initialized via the instance() method instead. |
||
| 107 | */ |
||
| 108 | protected function __construct() { |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Prevent __clone()'ing of this class. |
||
| 113 | * |
||
| 114 | * @since 5.0.0 |
||
| 115 | */ |
||
| 116 | public function __clone() { |
||
| 117 | wp_die( "Please don't __clone Jetpack_Search" ); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Prevent __wakeup()'ing of this class. |
||
| 122 | * |
||
| 123 | * @since 5.0.0 |
||
| 124 | */ |
||
| 125 | public function __wakeup() { |
||
| 126 | wp_die( "Please don't __wakeup Jetpack_Search" ); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get singleton instance of Jetpack_Search. |
||
| 131 | * |
||
| 132 | * Instantiates and sets up a new instance if needed, or returns the singleton. |
||
| 133 | * |
||
| 134 | * @since 5.0.0 |
||
| 135 | * |
||
| 136 | * @return Jetpack_Search The Jetpack_Search singleton. |
||
| 137 | */ |
||
| 138 | public static function instance() { |
||
| 139 | if ( ! isset( self::$instance ) ) { |
||
| 140 | self::$instance = new Jetpack_Search(); |
||
| 141 | |||
| 142 | self::$instance->setup(); |
||
| 143 | } |
||
| 144 | |||
| 145 | return self::$instance; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Perform various setup tasks for the class. |
||
| 150 | * |
||
| 151 | * Checks various pre-requisites and adds hooks. |
||
| 152 | * |
||
| 153 | * @since 5.0.0 |
||
| 154 | */ |
||
| 155 | public function setup() { |
||
| 156 | if ( ! Jetpack::is_active() || ! Jetpack_Plan::supports( 'search' ) ) { |
||
| 157 | /** |
||
| 158 | * Fires when the Jetpack Search fails and would fallback to MySQL. |
||
| 159 | * |
||
| 160 | * @module search |
||
| 161 | * @since 7.9.0 |
||
| 162 | * |
||
| 163 | * @param string $reason Reason for Search fallback. |
||
| 164 | * @param mixed $data Data associated with the request, such as attempted search parameters. |
||
| 165 | */ |
||
| 166 | do_action( 'jetpack_search_abort', 'inactive', null ); |
||
| 167 | return; |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->jetpack_blog_id = Jetpack::get_option( 'id' ); |
||
| 171 | |||
| 172 | if ( ! $this->jetpack_blog_id ) { |
||
| 173 | /** This action is documented in modules/search/class.jetpack-search.php */ |
||
| 174 | do_action( 'jetpack_search_abort', 'no_blog_id', null ); |
||
| 175 | return; |
||
| 176 | } |
||
| 177 | |||
| 178 | require_once dirname( __FILE__ ) . '/class.jetpack-search-helpers.php'; |
||
| 179 | require_once dirname( __FILE__ ) . '/class.jetpack-search-template-tags.php'; |
||
| 180 | require_once JETPACK__PLUGIN_DIR . 'modules/widgets/search.php'; |
||
| 181 | |||
| 182 | $this->init_hooks(); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Setup the various hooks needed for the plugin to take over search duties. |
||
| 187 | * |
||
| 188 | * @since 5.0.0 |
||
| 189 | */ |
||
| 190 | public function init_hooks() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Loads assets for Jetpack Instant Search Prototype featuring Search As You Type experience. |
||
| 212 | */ |
||
| 213 | public function load_assets() { |
||
| 214 | if ( Constants::is_true( 'JETPACK_SEARCH_PROTOTYPE' ) ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Loads scripts for Tracks analytics library |
||
| 287 | */ |
||
| 288 | public function load_and_initialize_tracks() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
| 294 | * |
||
| 295 | * @param string $file Path of the file we are looking for. |
||
| 296 | * @return string $script_version Version number. |
||
| 297 | */ |
||
| 298 | public static function get_asset_version( $file ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * When an Elasticsearch query fails, this stores it and enqueues some debug information in the footer. |
||
| 306 | * |
||
| 307 | * @since 5.6.0 |
||
| 308 | * |
||
| 309 | * @param array $meta Information about the failure. |
||
| 310 | */ |
||
| 311 | public function store_query_failure( $meta ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Outputs information about the last Elasticsearch failure. |
||
| 318 | * |
||
| 319 | * @since 5.6.0 |
||
| 320 | */ |
||
| 321 | public function print_query_failure() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Stores information about the last Elasticsearch query and enqueues some debug information in the footer. |
||
| 334 | * |
||
| 335 | * @since 5.6.0 |
||
| 336 | * |
||
| 337 | * @param array $meta Information about the query. |
||
| 338 | */ |
||
| 339 | public function store_last_query_info( $meta ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Outputs information about the last Elasticsearch search. |
||
| 346 | * |
||
| 347 | * @since 5.6.0 |
||
| 348 | */ |
||
| 349 | public function print_query_success() { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Returns the last query information, or false if no information was stored. |
||
| 368 | * |
||
| 369 | * @since 5.8.0 |
||
| 370 | * |
||
| 371 | * @return bool|array |
||
| 372 | */ |
||
| 373 | public function get_last_query_info() { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Returns the last query failure information, or false if no failure information was stored. |
||
| 379 | * |
||
| 380 | * @since 5.8.0 |
||
| 381 | * |
||
| 382 | * @return bool|array |
||
| 383 | */ |
||
| 384 | public function get_last_query_failure_info() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Wraps a WordPress filter called "jetpack_search_disable_widget_filters" that allows |
||
| 390 | * developers to disable filters supplied by the search widget. Useful if filters are |
||
| 391 | * being defined at the code level. |
||
| 392 | * |
||
| 393 | * @since 5.7.0 |
||
| 394 | * @deprecated 5.8.0 Use Jetpack_Search_Helpers::are_filters_by_widget_disabled() directly. |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | public function are_filters_by_widget_disabled() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Retrieves a list of known Jetpack search filters widget IDs, gets the filters for each widget, |
||
| 404 | * and applies those filters to this Jetpack_Search object. |
||
| 405 | * |
||
| 406 | * @since 5.7.0 |
||
| 407 | */ |
||
| 408 | public function set_filters_from_widgets() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Restricts search results to certain post types via a GET argument. |
||
| 422 | * |
||
| 423 | * @since 5.8.0 |
||
| 424 | * |
||
| 425 | * @param WP_Query $query A WP_Query instance. |
||
| 426 | */ |
||
| 427 | public function maybe_add_post_type_as_var( WP_Query $query ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Run a search on the WordPress.com public API. |
||
| 440 | * |
||
| 441 | * @since 5.0.0 |
||
| 442 | * |
||
| 443 | * @param array $es_args Args conforming to the WP.com /sites/<blog_id>/search endpoint. |
||
| 444 | * |
||
| 445 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
| 446 | */ |
||
| 447 | public function search( array $es_args ) { |
||
| 448 | $endpoint = sprintf( '/sites/%s/search', $this->jetpack_blog_id ); |
||
| 449 | $service_url = 'https://public-api.wordpress.com/rest/v1' . $endpoint; |
||
| 450 | |||
| 451 | $do_authenticated_request = false; |
||
| 452 | |||
| 453 | if ( class_exists( 'Automattic\\Jetpack\\Connection\\Client' ) && |
||
| 454 | isset( $es_args['authenticated_request'] ) && |
||
| 455 | true === $es_args['authenticated_request'] ) { |
||
| 456 | $do_authenticated_request = true; |
||
| 457 | } |
||
| 458 | |||
| 459 | unset( $es_args['authenticated_request'] ); |
||
| 460 | |||
| 461 | $request_args = array( |
||
| 462 | 'headers' => array( |
||
| 463 | 'Content-Type' => 'application/json', |
||
| 464 | ), |
||
| 465 | 'timeout' => 10, |
||
| 466 | 'user-agent' => 'jetpack_search', |
||
| 467 | ); |
||
| 468 | |||
| 469 | $request_body = wp_json_encode( $es_args ); |
||
| 470 | |||
| 471 | $start_time = microtime( true ); |
||
| 472 | |||
| 473 | if ( $do_authenticated_request ) { |
||
| 474 | $request_args['method'] = 'POST'; |
||
| 475 | |||
| 476 | $request = Client::wpcom_json_api_request_as_blog( $endpoint, Client::WPCOM_JSON_API_VERSION, $request_args, $request_body ); |
||
| 477 | } else { |
||
| 478 | $request_args = array_merge( |
||
| 479 | $request_args, |
||
| 480 | array( |
||
| 481 | 'body' => $request_body, |
||
| 482 | ) |
||
| 483 | ); |
||
| 484 | |||
| 485 | $request = wp_remote_post( $service_url, $request_args ); |
||
| 486 | } |
||
| 487 | |||
| 488 | $end_time = microtime( true ); |
||
| 489 | |||
| 490 | if ( is_wp_error( $request ) ) { |
||
| 491 | return $request; |
||
| 492 | } |
||
| 493 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
| 494 | |||
| 495 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
| 496 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
| 497 | } |
||
| 498 | |||
| 499 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
| 500 | |||
| 501 | $response = json_decode( wp_remote_retrieve_body( $request ), true ); |
||
| 502 | |||
| 503 | $took = is_array( $response ) && ! empty( $response['took'] ) |
||
| 504 | ? $response['took'] |
||
| 505 | : null; |
||
| 506 | |||
| 507 | $query = array( |
||
| 508 | 'args' => $es_args, |
||
| 509 | 'response' => $response, |
||
| 510 | 'response_code' => $response_code, |
||
| 511 | 'elapsed_time' => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms. |
||
| 512 | 'es_time' => $took, |
||
| 513 | 'url' => $service_url, |
||
| 514 | ); |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Fires after a search request has been performed. |
||
| 518 | * |
||
| 519 | * Includes the following info in the $query parameter: |
||
| 520 | * |
||
| 521 | * array args Array of Elasticsearch arguments for the search |
||
| 522 | * array response Raw API response, JSON decoded |
||
| 523 | * int response_code HTTP response code of the request |
||
| 524 | * float elapsed_time Roundtrip time of the search request, in milliseconds |
||
| 525 | * float es_time Amount of time Elasticsearch spent running the request, in milliseconds |
||
| 526 | * string url API url that was queried |
||
| 527 | * |
||
| 528 | * @module search |
||
| 529 | * |
||
| 530 | * @since 5.0.0 |
||
| 531 | * @since 5.8.0 This action now fires on all queries instead of just successful queries. |
||
| 532 | * |
||
| 533 | * @param array $query Array of information about the query performed |
||
| 534 | */ |
||
| 535 | do_action( 'did_jetpack_search_query', $query ); |
||
| 536 | |||
| 537 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
| 538 | /** |
||
| 539 | * Fires after a search query request has failed |
||
| 540 | * |
||
| 541 | * @module search |
||
| 542 | * |
||
| 543 | * @since 5.6.0 |
||
| 544 | * |
||
| 545 | * @param array Array containing the response code and response from the failed search query |
||
| 546 | */ |
||
| 547 | do_action( |
||
| 548 | 'failed_jetpack_search_query', |
||
| 549 | array( |
||
| 550 | 'response_code' => $response_code, |
||
| 551 | 'json' => $response, |
||
| 552 | ) |
||
| 553 | ); |
||
| 554 | |||
| 555 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
| 556 | } |
||
| 557 | |||
| 558 | return $response; |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Bypass the normal Search query and offload it to Jetpack servers. |
||
| 563 | * |
||
| 564 | * This is the main hook of the plugin and is responsible for returning the posts that match the search query. |
||
| 565 | * |
||
| 566 | * @since 5.0.0 |
||
| 567 | * |
||
| 568 | * @param array $posts Current array of posts (still pre-query). |
||
| 569 | * @param WP_Query $query The WP_Query being filtered. |
||
| 570 | * |
||
| 571 | * @return array Array of matching posts. |
||
| 572 | */ |
||
| 573 | public function filter__posts_pre_query( $posts, $query ) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Build up the search, then run it against the Jetpack servers. |
||
| 619 | * |
||
| 620 | * @since 5.0.0 |
||
| 621 | * |
||
| 622 | * @param WP_Query $query The original WP_Query to use for the parameters of our search. |
||
| 623 | */ |
||
| 624 | public function do_search( WP_Query $query ) { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * If the query has already been run before filters have been updated, then we need to re-run the query |
||
| 737 | * to get the latest aggregations. |
||
| 738 | * |
||
| 739 | * This is especially useful for supporting widget management in the customizer. |
||
| 740 | * |
||
| 741 | * @since 5.8.0 |
||
| 742 | * |
||
| 743 | * @return bool Whether the query was successful or not. |
||
| 744 | */ |
||
| 745 | public function update_search_results_aggregations() { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Given a WP_Query, convert its WP_Tax_Query (if present) into the WP-style Elasticsearch term arguments for the search. |
||
| 762 | * |
||
| 763 | * @since 5.0.0 |
||
| 764 | * |
||
| 765 | * @param WP_Query $query The original WP_Query object for which to parse the taxonomy query. |
||
| 766 | * |
||
| 767 | * @return array The new WP-style Elasticsearch arguments (that will be converted into 'real' Elasticsearch arguments). |
||
| 768 | */ |
||
| 769 | public function get_es_wp_query_terms_for_query( WP_Query $query ) { |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Parse out the post type from a WP_Query. |
||
| 804 | * |
||
| 805 | * Only allows post types that are not marked as 'exclude_from_search'. |
||
| 806 | * |
||
| 807 | * @since 5.0.0 |
||
| 808 | * |
||
| 809 | * @param WP_Query $query Original WP_Query object. |
||
| 810 | * |
||
| 811 | * @return array Array of searchable post types corresponding to the original query. |
||
| 812 | */ |
||
| 813 | public function get_es_wp_query_post_type_for_query( WP_Query $query ) { |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Initialize widgets for the Search module. |
||
| 854 | * |
||
| 855 | * @module search |
||
| 856 | */ |
||
| 857 | public function action__widgets_init() { |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Get the Elasticsearch result. |
||
| 865 | * |
||
| 866 | * @since 5.0.0 |
||
| 867 | * |
||
| 868 | * @param bool $raw If true, does not check for WP_Error or return the 'results' array - the JSON decoded HTTP response. |
||
| 869 | * |
||
| 870 | * @return array|bool The search results, or false if there was a failure. |
||
| 871 | */ |
||
| 872 | public function get_search_result( $raw = false ) { |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Add the date portion of a WP_Query onto the query args. |
||
| 882 | * |
||
| 883 | * @since 5.0.0 |
||
| 884 | * |
||
| 885 | * @param array $es_wp_query_args The Elasticsearch query arguments in WordPress form. |
||
| 886 | * @param WP_Query $query The original WP_Query. |
||
| 887 | * |
||
| 888 | * @return array The es wp query args, with date filters added (as needed). |
||
| 889 | */ |
||
| 890 | public function filter__add_date_filter_to_query( array $es_wp_query_args, WP_Query $query ) { |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Converts WP_Query style args to Elasticsearch args. |
||
| 925 | * |
||
| 926 | * @since 5.0.0 |
||
| 927 | * |
||
| 928 | * @param array $args Array of WP_Query style arguments. |
||
| 929 | * |
||
| 930 | * @return array Array of ES style query arguments. |
||
| 931 | */ |
||
| 932 | public function convert_wp_es_to_es_args( array $args ) { |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Given an array of aggregations, parse and add them onto the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1327 | * |
||
| 1328 | * @since 5.0.0 |
||
| 1329 | * |
||
| 1330 | * @param array $aggregations Array of aggregations (filters) to add to the Jetpack_WPES_Query_Builder. |
||
| 1331 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1332 | */ |
||
| 1333 | public function add_aggregations_to_es_query_builder( array $aggregations, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Given an individual taxonomy aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1356 | * |
||
| 1357 | * @since 5.0.0 |
||
| 1358 | * |
||
| 1359 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1360 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1361 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1362 | */ |
||
| 1363 | public function add_taxonomy_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * Given an individual post_type aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1393 | * |
||
| 1394 | * @since 5.0.0 |
||
| 1395 | * |
||
| 1396 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1397 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1398 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1399 | */ |
||
| 1400 | public function add_post_type_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * Given an individual date_histogram aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1414 | * |
||
| 1415 | * @since 5.0.0 |
||
| 1416 | * |
||
| 1417 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1418 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1419 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1420 | */ |
||
| 1421 | public function add_date_histogram_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * And an existing filter object with a list of additional filters. |
||
| 1443 | * |
||
| 1444 | * Attempts to optimize the filters somewhat. |
||
| 1445 | * |
||
| 1446 | * @since 5.0.0 |
||
| 1447 | * |
||
| 1448 | * @param array $curr_filter The existing filters to build upon. |
||
| 1449 | * @param array $filters The new filters to add. |
||
| 1450 | * |
||
| 1451 | * @return array The resulting merged filters. |
||
| 1452 | */ |
||
| 1453 | public static function and_es_filters( array $curr_filter, array $filters ) { |
||
| 1468 | |||
| 1469 | /** |
||
| 1470 | * Set the available filters for the search. |
||
| 1471 | * |
||
| 1472 | * These get rendered via the Jetpack_Search_Widget() widget. |
||
| 1473 | * |
||
| 1474 | * Behind the scenes, these are implemented using Elasticsearch Aggregations. |
||
| 1475 | * |
||
| 1476 | * If you do not require counts of how many documents match each filter, please consider using regular WP Query |
||
| 1477 | * arguments instead, such as via the jetpack_search_es_wp_query_args filter |
||
| 1478 | * |
||
| 1479 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1480 | * |
||
| 1481 | * @since 5.0.0 |
||
| 1482 | * |
||
| 1483 | * @param array $aggregations Array of filters (aggregations) to apply to the search. |
||
| 1484 | */ |
||
| 1485 | public function set_filters( array $aggregations ) { |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Set the search's facets (deprecated). |
||
| 1496 | * |
||
| 1497 | * @deprecated 5.0 Please use Jetpack_Search::set_filters() instead. |
||
| 1498 | * |
||
| 1499 | * @see Jetpack_Search::set_filters() |
||
| 1500 | * |
||
| 1501 | * @param array $facets Array of facets to apply to the search. |
||
| 1502 | */ |
||
| 1503 | public function set_facets( array $facets ) { |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Get the raw Aggregation results from the Elasticsearch response. |
||
| 1511 | * |
||
| 1512 | * @since 5.0.0 |
||
| 1513 | * |
||
| 1514 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1515 | * |
||
| 1516 | * @return array Array of Aggregations performed on the search. |
||
| 1517 | */ |
||
| 1518 | public function get_search_aggregations_results() { |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Get the raw Facet results from the Elasticsearch response. |
||
| 1532 | * |
||
| 1533 | * @deprecated 5.0 Please use Jetpack_Search::get_search_aggregations_results() instead. |
||
| 1534 | * |
||
| 1535 | * @see Jetpack_Search::get_search_aggregations_results() |
||
| 1536 | * |
||
| 1537 | * @return array Array of Facets performed on the search. |
||
| 1538 | */ |
||
| 1539 | public function get_search_facets() { |
||
| 1544 | |||
| 1545 | /** |
||
| 1546 | * Get the results of the Filters performed, including the number of matching documents. |
||
| 1547 | * |
||
| 1548 | * Returns an array of Filters (keyed by $label, as passed to Jetpack_Search::set_filters()), containing the Filter and all resulting |
||
| 1549 | * matching buckets, the url for applying/removing each bucket, etc. |
||
| 1550 | * |
||
| 1551 | * NOTE - if this is called before the search is performed, an empty array will be returned. Use the $aggregations class |
||
| 1552 | * member if you need to access the raw filters set in Jetpack_Search::set_filters(). |
||
| 1553 | * |
||
| 1554 | * @since 5.0.0 |
||
| 1555 | * |
||
| 1556 | * @param WP_Query $query The optional original WP_Query to use for determining which filters are active. Defaults to the main query. |
||
| 1557 | * |
||
| 1558 | * @return array Array of filters applied and info about them. |
||
| 1559 | */ |
||
| 1560 | public function get_filters( WP_Query $query = null ) { |
||
| 1821 | |||
| 1822 | /** |
||
| 1823 | * Get the results of the facets performed. |
||
| 1824 | * |
||
| 1825 | * @deprecated 5.0 Please use Jetpack_Search::get_filters() instead. |
||
| 1826 | * |
||
| 1827 | * @see Jetpack_Search::get_filters() |
||
| 1828 | * |
||
| 1829 | * @return array $facets Array of facets applied and info about them. |
||
| 1830 | */ |
||
| 1831 | public function get_search_facet_data() { |
||
| 1836 | |||
| 1837 | /** |
||
| 1838 | * Get the filters that are currently applied to this search. |
||
| 1839 | * |
||
| 1840 | * @since 5.0.0 |
||
| 1841 | * |
||
| 1842 | * @return array Array of filters that were applied. |
||
| 1843 | */ |
||
| 1844 | public function get_active_filter_buckets() { |
||
| 1865 | |||
| 1866 | /** |
||
| 1867 | * Get the filters that are currently applied to this search. |
||
| 1868 | * |
||
| 1869 | * @deprecated 5.0 Please use Jetpack_Search::get_active_filter_buckets() instead. |
||
| 1870 | * |
||
| 1871 | * @see Jetpack_Search::get_active_filter_buckets() |
||
| 1872 | * |
||
| 1873 | * @return array Array of filters that were applied. |
||
| 1874 | */ |
||
| 1875 | public function get_current_filters() { |
||
| 1880 | |||
| 1881 | /** |
||
| 1882 | * Calculate the right query var to use for a given taxonomy. |
||
| 1883 | * |
||
| 1884 | * Allows custom code to modify the GET var that is used to represent a given taxonomy, via the jetpack_search_taxonomy_query_var filter. |
||
| 1885 | * |
||
| 1886 | * @since 5.0.0 |
||
| 1887 | * |
||
| 1888 | * @param string $taxonomy_name The name of the taxonomy for which to get the query var. |
||
| 1889 | * |
||
| 1890 | * @return bool|string The query var to use for this taxonomy, or false if none found. |
||
| 1891 | */ |
||
| 1892 | public function get_taxonomy_query_var( $taxonomy_name ) { |
||
| 1911 | |||
| 1912 | /** |
||
| 1913 | * Takes an array of aggregation results, and ensures the array key ordering matches the key order in $desired |
||
| 1914 | * which is the input order. |
||
| 1915 | * |
||
| 1916 | * Necessary because ES does not always return aggregations in the same order that you pass them in, |
||
| 1917 | * and it should be possible to control the display order easily. |
||
| 1918 | * |
||
| 1919 | * @since 5.0.0 |
||
| 1920 | * |
||
| 1921 | * @param array $aggregations Aggregation results to be reordered. |
||
| 1922 | * @param array $desired Array with keys representing the desired ordering. |
||
| 1923 | * |
||
| 1924 | * @return array A new array with reordered keys, matching those in $desired. |
||
| 1925 | */ |
||
| 1926 | public function fix_aggregation_ordering( array $aggregations, array $desired ) { |
||
| 1941 | |||
| 1942 | /** |
||
| 1943 | * Sends events to Tracks when a search filters widget is updated. |
||
| 1944 | * |
||
| 1945 | * @since 5.8.0 |
||
| 1946 | * |
||
| 1947 | * @param string $option The option name. Only "widget_jetpack-search-filters" is cared about. |
||
| 1948 | * @param array $old_value The old option value. |
||
| 1949 | * @param array $new_value The new option value. |
||
| 1950 | */ |
||
| 1951 | public function track_widget_updates( $option, $old_value, $new_value ) { |
||
| 1968 | |||
| 1969 | /** |
||
| 1970 | * Moves any active search widgets to the inactive category. |
||
| 1971 | * |
||
| 1972 | * @since 5.9.0 |
||
| 1973 | * |
||
| 1974 | * @param string $module Unused. The Jetpack module being disabled. |
||
| 1975 | */ |
||
| 1976 | public function move_search_widgets_to_inactive( $module ) { |
||
| 2010 | |||
| 2011 | /** |
||
| 2012 | * Determine whether a given WP_Query should be handled by ElasticSearch. |
||
| 2013 | * |
||
| 2014 | * @param WP_Query $query The WP_Query object. |
||
| 2015 | * |
||
| 2016 | * @return bool |
||
| 2017 | */ |
||
| 2018 | public function should_handle_query( $query ) { |
||
| 2031 | |||
| 2032 | /** |
||
| 2033 | * Transforms an array with fields name as keys and boosts as value into |
||
| 2034 | * shorthand "caret" format. |
||
| 2035 | * |
||
| 2036 | * @param array $fields_boost [ "title" => "2", "content" => "1" ]. |
||
| 2037 | * |
||
| 2038 | * @return array [ "title^2", "content^1" ] |
||
| 2039 | */ |
||
| 2040 | private function _get_caret_boosted_fields( array $fields_boost ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
||
| 2047 | |||
| 2048 | /** |
||
| 2049 | * Apply a multiplier to boost values. |
||
| 2050 | * |
||
| 2051 | * @param array $fields_boost [ "title" => 2, "content" => 1 ]. |
||
| 2052 | * @param array $fields_boost_multiplier [ "title" => 0.1234 ]. |
||
| 2053 | * |
||
| 2054 | * @return array [ "title" => "0.247", "content" => "1.000" ] |
||
| 2055 | */ |
||
| 2056 | private function _apply_boosts_multiplier( array $fields_boost, array $fields_boost_multiplier ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
||
| 2073 | } |
||
| 2074 |
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.