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 |
||
| 15 | class Jetpack_Search { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The number of found posts. |
||
| 19 | * |
||
| 20 | * @since 5.0.0 |
||
| 21 | * |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | protected $found_posts = 0; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The search result, as returned by the WordPress.com REST API. |
||
| 28 | * |
||
| 29 | * @since 5.0.0 |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $search_result; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * This site's blog ID on WordPress.com. |
||
| 37 | * |
||
| 38 | * @since 5.0.0 |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | protected $jetpack_blog_id; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The Elasticsearch aggregations (filters). |
||
| 46 | * |
||
| 47 | * @since 5.0.0 |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $aggregations = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The maximum number of aggregations allowed. |
||
| 55 | * |
||
| 56 | * @since 5.0.0 |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | protected $max_aggregations_count = 100; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Statistics about the last Elasticsearch query. |
||
| 64 | * |
||
| 65 | * @since 5.6.0 |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $last_query_info = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Statistics about the last Elasticsearch query failure. |
||
| 73 | * |
||
| 74 | * @since 5.6.0 |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $last_query_failure_info = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The singleton instance of this class. |
||
| 82 | * |
||
| 83 | * @since 5.0.0 |
||
| 84 | * |
||
| 85 | * @var Jetpack_Search |
||
| 86 | */ |
||
| 87 | protected static $instance; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Languages with custom analyzers. Other languages are supported, but are analyzed with the default analyzer. |
||
| 91 | * |
||
| 92 | * @since 5.0.0 |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | 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' ); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Jetpack_Search constructor. |
||
| 100 | * |
||
| 101 | * @since 5.0.0 |
||
| 102 | * |
||
| 103 | * Doesn't do anything. This class needs to be initialized via the instance() method instead. |
||
| 104 | */ |
||
| 105 | protected function __construct() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Prevent __clone()'ing of this class. |
||
| 110 | * |
||
| 111 | * @since 5.0.0 |
||
| 112 | */ |
||
| 113 | public function __clone() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Prevent __wakeup()'ing of this class. |
||
| 119 | * |
||
| 120 | * @since 5.0.0 |
||
| 121 | */ |
||
| 122 | public function __wakeup() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get singleton instance of Jetpack_Search. |
||
| 128 | * |
||
| 129 | * Instantiates and sets up a new instance if needed, or returns the singleton. |
||
| 130 | * |
||
| 131 | * @since 5.0.0 |
||
| 132 | * |
||
| 133 | * @return Jetpack_Search The Jetpack_Search singleton. |
||
| 134 | */ |
||
| 135 | public static function instance() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Perform various setup tasks for the class. |
||
| 147 | * |
||
| 148 | * Checks various pre-requisites and adds hooks. |
||
| 149 | * |
||
| 150 | * @since 5.0.0 |
||
| 151 | */ |
||
| 152 | public function setup() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Setup the various hooks needed for the plugin to take over search duties. |
||
| 171 | * |
||
| 172 | * @since 5.0.0 |
||
| 173 | */ |
||
| 174 | public function init_hooks() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * When an Elasticsearch query fails, this stores it and enqueues some debug information in the footer. |
||
| 195 | * |
||
| 196 | * @since 5.6.0 |
||
| 197 | * |
||
| 198 | * @param array $meta Information about the failure. |
||
| 199 | */ |
||
| 200 | public function store_query_failure( $meta ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Outputs information about the last Elasticsearch failure. |
||
| 207 | * |
||
| 208 | * @since 5.6.0 |
||
| 209 | */ |
||
| 210 | public function print_query_failure() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Stores information about the last Elasticsearch query and enqueues some debug information in the footer. |
||
| 223 | * |
||
| 224 | * @since 5.6.0 |
||
| 225 | * |
||
| 226 | * @param array $meta Information about the query. |
||
| 227 | */ |
||
| 228 | public function store_last_query_info( $meta ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Outputs information about the last Elasticsearch search. |
||
| 235 | * |
||
| 236 | * @since 5.6.0 |
||
| 237 | */ |
||
| 238 | public function print_query_success() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns the last query information, or false if no information was stored. |
||
| 250 | * |
||
| 251 | * @since 5.8.0 |
||
| 252 | * |
||
| 253 | * @return bool|array |
||
| 254 | */ |
||
| 255 | public function get_last_query_info() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns the last query failure information, or false if no failure information was stored. |
||
| 261 | * |
||
| 262 | * @since 5.8.0 |
||
| 263 | * |
||
| 264 | * @return bool|array |
||
| 265 | */ |
||
| 266 | public function get_last_query_failure_info() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Wraps a WordPress filter called "jetpack_search_disable_widget_filters" that allows |
||
| 272 | * developers to disable filters supplied by the search widget. Useful if filters are |
||
| 273 | * being defined at the code level. |
||
| 274 | * |
||
| 275 | * @since 5.7.0 |
||
| 276 | * @deprecated 5.8.0 Use Jetpack_Search_Helpers::are_filters_by_widget_disabled() directly. |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public function are_filters_by_widget_disabled() { |
||
| 281 | return Jetpack_Search_Helpers::are_filters_by_widget_disabled(); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Retrieves a list of known Jetpack search filters widget IDs, gets the filters for each widget, |
||
| 286 | * and applies those filters to this Jetpack_Search object. |
||
| 287 | * |
||
| 288 | * @since 5.7.0 |
||
| 289 | */ |
||
| 290 | public function set_filters_from_widgets() { |
||
| 291 | if ( Jetpack_Search_Helpers::are_filters_by_widget_disabled() ) { |
||
| 292 | return; |
||
| 293 | } |
||
| 294 | |||
| 295 | $filters = Jetpack_Search_Helpers::get_filters_from_widgets(); |
||
| 296 | |||
| 297 | if ( ! empty( $filters ) ) { |
||
| 298 | $this->set_filters( $filters ); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Restricts search results to certain post types via a GET argument. |
||
| 304 | * |
||
| 305 | * @since 5.8.0 |
||
| 306 | * |
||
| 307 | * @param WP_Query $query A WP_Query instance. |
||
| 308 | */ |
||
| 309 | public function maybe_add_post_type_as_var( WP_Query $query ) { |
||
| 310 | if ( $query->is_main_query() && $query->is_search && ! empty( $_GET['post_type'] ) ) { |
||
| 311 | $post_types = ( is_string( $_GET['post_type'] ) && false !== strpos( $_GET['post_type'], ',' ) ) |
||
| 312 | ? $post_type = explode( ',', $_GET['post_type'] ) |
||
| 313 | : (array) $_GET['post_type']; |
||
| 314 | $post_types = array_map( 'sanitize_key', $post_types ); |
||
| 315 | $query->set( 'post_type', $post_types ); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | /* |
||
| 320 | * Run a search on the WordPress.com public API. |
||
| 321 | * |
||
| 322 | * @since 5.0.0 |
||
| 323 | * |
||
| 324 | * @param array $es_args Args conforming to the WP.com /sites/<blog_id>/search endpoint. |
||
| 325 | * |
||
| 326 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
| 327 | */ |
||
| 328 | public function search( array $es_args ) { |
||
| 329 | $endpoint = sprintf( '/sites/%s/search', $this->jetpack_blog_id ); |
||
| 330 | $service_url = 'https://public-api.wordpress.com/rest/v1' . $endpoint; |
||
| 331 | |||
| 332 | $do_authenticated_request = false; |
||
| 333 | |||
| 334 | if ( class_exists( 'Jetpack_Client' ) && |
||
| 335 | isset( $es_args['authenticated_request'] ) && |
||
| 336 | true === $es_args['authenticated_request'] ) { |
||
| 337 | $do_authenticated_request = true; |
||
| 338 | } |
||
| 339 | |||
| 340 | unset( $es_args['authenticated_request'] ); |
||
| 341 | |||
| 342 | $request_args = array( |
||
| 343 | 'headers' => array( |
||
| 344 | 'Content-Type' => 'application/json', |
||
| 345 | ), |
||
| 346 | 'timeout' => 10, |
||
| 347 | 'user-agent' => 'jetpack_search', |
||
| 348 | ); |
||
| 349 | |||
| 350 | $request_body = wp_json_encode( $es_args ); |
||
| 351 | |||
| 352 | $start_time = microtime( true ); |
||
| 353 | |||
| 354 | if ( $do_authenticated_request ) { |
||
| 355 | $request_args['method'] = 'POST'; |
||
| 356 | |||
| 357 | $request = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $request_args, $request_body ); |
||
| 358 | } else { |
||
| 359 | $request_args = array_merge( $request_args, array( |
||
| 360 | 'body' => $request_body, |
||
| 361 | ) ); |
||
| 362 | |||
| 363 | $request = wp_remote_post( $service_url, $request_args ); |
||
| 364 | } |
||
| 365 | |||
| 366 | $end_time = microtime( true ); |
||
| 367 | |||
| 368 | if ( is_wp_error( $request ) ) { |
||
| 369 | return $request; |
||
| 370 | } |
||
| 371 | |||
| 372 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
| 373 | |||
| 374 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
| 375 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
| 376 | } |
||
| 377 | |||
| 378 | $response = json_decode( wp_remote_retrieve_body( $request ), true ); |
||
| 379 | |||
| 380 | $took = is_array( $response ) && ! empty( $response['took'] ) |
||
| 381 | ? $response['took'] |
||
| 382 | : null; |
||
| 383 | |||
| 384 | $query = array( |
||
| 385 | 'args' => $es_args, |
||
| 386 | 'response' => $response, |
||
| 387 | 'response_code' => $response_code, |
||
| 388 | 'elapsed_time' => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms. |
||
| 389 | 'es_time' => $took, |
||
| 390 | 'url' => $service_url, |
||
| 391 | ); |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Fires after a search request has been performed. |
||
| 395 | * |
||
| 396 | * Includes the following info in the $query parameter: |
||
| 397 | * |
||
| 398 | * array args Array of Elasticsearch arguments for the search |
||
| 399 | * array response Raw API response, JSON decoded |
||
| 400 | * int response_code HTTP response code of the request |
||
| 401 | * float elapsed_time Roundtrip time of the search request, in milliseconds |
||
| 402 | * float es_time Amount of time Elasticsearch spent running the request, in milliseconds |
||
| 403 | * string url API url that was queried |
||
| 404 | * |
||
| 405 | * @module search |
||
| 406 | * |
||
| 407 | * @since 5.0.0 |
||
| 408 | * @since 5.8.0 This action now fires on all queries instead of just successful queries. |
||
| 409 | * |
||
| 410 | * @param array $query Array of information about the query performed |
||
| 411 | */ |
||
| 412 | do_action( 'did_jetpack_search_query', $query ); |
||
| 413 | |||
| 414 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
| 415 | /** |
||
| 416 | * Fires after a search query request has failed |
||
| 417 | * |
||
| 418 | * @module search |
||
| 419 | * |
||
| 420 | * @since 5.6.0 |
||
| 421 | * |
||
| 422 | * @param array Array containing the response code and response from the failed search query |
||
| 423 | */ |
||
| 424 | do_action( 'failed_jetpack_search_query', array( |
||
| 425 | 'response_code' => $response_code, |
||
| 426 | 'json' => $response, |
||
| 427 | ) ); |
||
| 428 | |||
| 429 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
| 430 | } |
||
| 431 | |||
| 432 | return $response; |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Bypass the normal Search query and offload it to Jetpack servers. |
||
| 437 | * |
||
| 438 | * This is the main hook of the plugin and is responsible for returning the posts that match the search query. |
||
| 439 | * |
||
| 440 | * @since 5.0.0 |
||
| 441 | * |
||
| 442 | * @param array $posts Current array of posts (still pre-query). |
||
| 443 | * @param WP_Query $query The WP_Query being filtered. |
||
| 444 | * |
||
| 445 | * @return array Array of matching posts. |
||
| 446 | */ |
||
| 447 | public function filter__posts_pre_query( $posts, $query ) { |
||
| 448 | /** |
||
| 449 | * Determine whether a given WP_Query should be handled by ElasticSearch. |
||
| 450 | * |
||
| 451 | * @module search |
||
| 452 | * |
||
| 453 | * @since 5.6.0 |
||
| 454 | * |
||
| 455 | * @param bool $should_handle Should be handled by Jetpack Search. |
||
| 456 | * @param WP_Query $query The WP_Query object. |
||
| 457 | */ |
||
| 458 | if ( ! apply_filters( 'jetpack_search_should_handle_query', ( $query->is_main_query() && $query->is_search() ), $query ) ) { |
||
| 459 | return $posts; |
||
| 460 | } |
||
| 461 | |||
| 462 | $this->do_search( $query ); |
||
| 463 | |||
| 464 | if ( ! is_array( $this->search_result ) ) { |
||
| 465 | return $posts; |
||
| 466 | } |
||
| 467 | |||
| 468 | // If no results, nothing to do |
||
| 469 | if ( ! count( $this->search_result['results']['hits'] ) ) { |
||
| 470 | return array(); |
||
| 471 | } |
||
| 472 | |||
| 473 | $post_ids = array(); |
||
| 474 | |||
| 475 | foreach ( $this->search_result['results']['hits'] as $result ) { |
||
| 476 | $post_ids[] = (int) $result['fields']['post_id']; |
||
| 477 | } |
||
| 478 | |||
| 479 | // Query all posts now |
||
| 480 | $args = array( |
||
| 481 | 'post__in' => $post_ids, |
||
| 482 | 'perm' => 'readable', |
||
| 483 | 'post_type' => 'any', |
||
| 484 | 'ignore_sticky_posts' => true, |
||
| 485 | 'suppress_filters' => true, |
||
| 486 | ); |
||
| 487 | |||
| 488 | if ( isset( $query->query_vars['order'] ) ) { |
||
| 489 | $args['order'] = $query->query_vars['order']; |
||
| 490 | } |
||
| 491 | |||
| 492 | if ( isset( $query->query_vars['orderby'] ) ) { |
||
| 493 | $args['orderby'] = $query->query_vars['orderby']; |
||
| 494 | } |
||
| 495 | |||
| 496 | $posts_query = new WP_Query( $args ); |
||
| 497 | |||
| 498 | // 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. |
||
| 499 | $query->found_posts = $this->found_posts; |
||
| 500 | $query->max_num_pages = ceil( $this->found_posts / $query->get( 'posts_per_page' ) ); |
||
| 501 | |||
| 502 | return $posts_query->posts; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Build up the search, then run it against the Jetpack servers. |
||
| 507 | * |
||
| 508 | * @since 5.0.0 |
||
| 509 | * |
||
| 510 | * @param WP_Query $query The original WP_Query to use for the parameters of our search. |
||
| 511 | */ |
||
| 512 | public function do_search( WP_Query $query ) { |
||
| 513 | if ( ! $query->is_main_query() || ! $query->is_search() ) { |
||
| 514 | return; |
||
| 515 | } |
||
| 516 | |||
| 517 | $page = ( $query->get( 'paged' ) ) ? absint( $query->get( 'paged' ) ) : 1; |
||
| 518 | |||
| 519 | // Get maximum allowed offset and posts per page values for the API. |
||
| 520 | $max_offset = Jetpack_Search_Helpers::get_max_offset(); |
||
| 521 | $max_posts_per_page = Jetpack_Search_Helpers::get_max_posts_per_page(); |
||
| 522 | |||
| 523 | $posts_per_page = $query->get( 'posts_per_page' ); |
||
| 524 | if ( $posts_per_page > $max_posts_per_page ) { |
||
| 525 | $posts_per_page = $max_posts_per_page; |
||
| 526 | } |
||
| 527 | |||
| 528 | // Start building the WP-style search query args. |
||
| 529 | // They'll be translated to ES format args later. |
||
| 530 | $es_wp_query_args = array( |
||
| 531 | 'query' => $query->get( 's' ), |
||
| 532 | 'posts_per_page' => $posts_per_page, |
||
| 533 | 'paged' => $page, |
||
| 534 | 'orderby' => $query->get( 'orderby' ), |
||
| 535 | 'order' => $query->get( 'order' ), |
||
| 536 | ); |
||
| 537 | |||
| 538 | if ( ! empty( $this->aggregations ) ) { |
||
| 539 | $es_wp_query_args['aggregations'] = $this->aggregations; |
||
| 540 | } |
||
| 541 | |||
| 542 | // Did we query for authors? |
||
| 543 | if ( $query->get( 'author_name' ) ) { |
||
| 544 | $es_wp_query_args['author_name'] = $query->get( 'author_name' ); |
||
| 545 | } |
||
| 546 | |||
| 547 | $es_wp_query_args['post_type'] = $this->get_es_wp_query_post_type_for_query( $query ); |
||
| 548 | $es_wp_query_args['terms'] = $this->get_es_wp_query_terms_for_query( $query ); |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Modify the search query parameters, such as controlling the post_type. |
||
| 552 | * |
||
| 553 | * These arguments are in the format of WP_Query arguments |
||
| 554 | * |
||
| 555 | * @module search |
||
| 556 | * |
||
| 557 | * @since 5.0.0 |
||
| 558 | * |
||
| 559 | * @param array $es_wp_query_args The current query args, in WP_Query format. |
||
| 560 | * @param WP_Query $query The original WP_Query object. |
||
| 561 | */ |
||
| 562 | $es_wp_query_args = apply_filters( 'jetpack_search_es_wp_query_args', $es_wp_query_args, $query ); |
||
| 563 | |||
| 564 | // If page * posts_per_page is greater than our max offset, send a 404. This is necessary because the offset is |
||
| 565 | // capped at Jetpack_Search_Helpers::get_max_offset(), so a high page would always return the last page of results otherwise. |
||
| 566 | if ( ( $es_wp_query_args['paged'] * $es_wp_query_args['posts_per_page'] ) > $max_offset ) { |
||
| 567 | $query->set_404(); |
||
| 568 | |||
| 569 | return; |
||
| 570 | } |
||
| 571 | |||
| 572 | // If there were no post types returned, then 404 to avoid querying against non-public post types, which could |
||
| 573 | // happen if we don't add the post type restriction to the ES query. |
||
| 574 | if ( empty( $es_wp_query_args['post_type'] ) ) { |
||
| 575 | $query->set_404(); |
||
| 576 | |||
| 577 | return; |
||
| 578 | } |
||
| 579 | |||
| 580 | // Convert the WP-style args into ES args. |
||
| 581 | $es_query_args = $this->convert_wp_es_to_es_args( $es_wp_query_args ); |
||
| 582 | |||
| 583 | //Only trust ES to give us IDs, not the content since it is a mirror |
||
| 584 | $es_query_args['fields'] = array( |
||
| 585 | 'post_id', |
||
| 586 | ); |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Modify the underlying ES query that is passed to the search endpoint. The returned args must represent a valid ES query |
||
| 590 | * |
||
| 591 | * This filter is harder to use if you're unfamiliar with ES, but allows complete control over the query |
||
| 592 | * |
||
| 593 | * @module search |
||
| 594 | * |
||
| 595 | * @since 5.0.0 |
||
| 596 | * |
||
| 597 | * @param array $es_query_args The raw Elasticsearch query args. |
||
| 598 | * @param WP_Query $query The original WP_Query object. |
||
| 599 | */ |
||
| 600 | $es_query_args = apply_filters( 'jetpack_search_es_query_args', $es_query_args, $query ); |
||
| 601 | |||
| 602 | // Do the actual search query! |
||
| 603 | $this->search_result = $this->search( $es_query_args ); |
||
| 604 | |||
| 605 | if ( is_wp_error( $this->search_result ) || ! is_array( $this->search_result ) || empty( $this->search_result['results'] ) || empty( $this->search_result['results']['hits'] ) ) { |
||
| 606 | $this->found_posts = 0; |
||
| 607 | |||
| 608 | return; |
||
| 609 | } |
||
| 610 | |||
| 611 | // If we have aggregations, fix the ordering to match the input order (ES doesn't guarantee the return order). |
||
| 612 | if ( isset( $this->search_result['results']['aggregations'] ) && ! empty( $this->search_result['results']['aggregations'] ) ) { |
||
| 613 | $this->search_result['results']['aggregations'] = $this->fix_aggregation_ordering( $this->search_result['results']['aggregations'], $this->aggregations ); |
||
| 614 | } |
||
| 615 | |||
| 616 | // Total number of results for paging purposes. Capped at $max_offset + $posts_per_page, as deep paging gets quite expensive. |
||
| 617 | $this->found_posts = min( $this->search_result['results']['total'], $max_offset + $posts_per_page ); |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * If the query has already been run before filters have been updated, then we need to re-run the query |
||
| 622 | * to get the latest aggregations. |
||
| 623 | * |
||
| 624 | * This is especially useful for supporting widget management in the customizer. |
||
| 625 | * |
||
| 626 | * @since 5.8.0 |
||
| 627 | * |
||
| 628 | * @return bool Whether the query was successful or not. |
||
| 629 | */ |
||
| 630 | public function update_search_results_aggregations() { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Given a WP_Query, convert its WP_Tax_Query (if present) into the WP-style Elasticsearch term arguments for the search. |
||
| 647 | * |
||
| 648 | * @since 5.0.0 |
||
| 649 | * |
||
| 650 | * @param WP_Query $query The original WP_Query object for which to parse the taxonomy query. |
||
| 651 | * |
||
| 652 | * @return array The new WP-style Elasticsearch arguments (that will be converted into 'real' Elasticsearch arguments). |
||
| 653 | */ |
||
| 654 | public function get_es_wp_query_terms_for_query( WP_Query $query ) { |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Parse out the post type from a WP_Query. |
||
| 690 | * |
||
| 691 | * Only allows post types that are not marked as 'exclude_from_search'. |
||
| 692 | * |
||
| 693 | * @since 5.0.0 |
||
| 694 | * |
||
| 695 | * @param WP_Query $query Original WP_Query object. |
||
| 696 | * |
||
| 697 | * @return array Array of searchable post types corresponding to the original query. |
||
| 698 | */ |
||
| 699 | public function get_es_wp_query_post_type_for_query( WP_Query $query ) { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Initialze widgets for the Search module |
||
| 736 | * |
||
| 737 | * @module search |
||
| 738 | */ |
||
| 739 | public function action__widgets_init() { |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Get the Elasticsearch result. |
||
| 747 | * |
||
| 748 | * @since 5.0.0 |
||
| 749 | * |
||
| 750 | * @param bool $raw If true, does not check for WP_Error or return the 'results' array - the JSON decoded HTTP response. |
||
| 751 | * |
||
| 752 | * @return array|bool The search results, or false if there was a failure. |
||
| 753 | */ |
||
| 754 | public function get_search_result( $raw = false ) { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Add the date portion of a WP_Query onto the query args. |
||
| 764 | * |
||
| 765 | * @since 5.0.0 |
||
| 766 | * |
||
| 767 | * @param array $es_wp_query_args The Elasticsearch query arguments in WordPress form. |
||
| 768 | * @param WP_Query $query The original WP_Query. |
||
| 769 | * |
||
| 770 | * @return array The es wp query args, with date filters added (as needed). |
||
| 771 | */ |
||
| 772 | public function filter__add_date_filter_to_query( array $es_wp_query_args, WP_Query $query ) { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Converts WP_Query style args to Elasticsearch args. |
||
| 807 | * |
||
| 808 | * @since 5.0.0 |
||
| 809 | * |
||
| 810 | * @param array $args Array of WP_Query style arguments. |
||
| 811 | * |
||
| 812 | * @return array Array of ES style query arguments. |
||
| 813 | */ |
||
| 814 | public function convert_wp_es_to_es_args( array $args ) { |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Given an array of aggregations, parse and add them onto the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1163 | * |
||
| 1164 | * @since 5.0.0 |
||
| 1165 | * |
||
| 1166 | * @param array $aggregations Array of aggregations (filters) to add to the Jetpack_WPES_Query_Builder. |
||
| 1167 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1168 | */ |
||
| 1169 | public function add_aggregations_to_es_query_builder( array $aggregations, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Given an individual taxonomy aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1192 | * |
||
| 1193 | * @since 5.0.0 |
||
| 1194 | * |
||
| 1195 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1196 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1197 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1198 | */ |
||
| 1199 | public function add_taxonomy_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Given an individual post_type aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1226 | * |
||
| 1227 | * @since 5.0.0 |
||
| 1228 | * |
||
| 1229 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1230 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1231 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1232 | */ |
||
| 1233 | public function add_post_type_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1241 | |||
| 1242 | /** |
||
| 1243 | * Given an individual date_histogram aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1244 | * |
||
| 1245 | * @since 5.0.0 |
||
| 1246 | * |
||
| 1247 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1248 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1249 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1250 | */ |
||
| 1251 | public function add_date_histogram_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1267 | |||
| 1268 | /** |
||
| 1269 | * And an existing filter object with a list of additional filters. |
||
| 1270 | * |
||
| 1271 | * Attempts to optimize the filters somewhat. |
||
| 1272 | * |
||
| 1273 | * @since 5.0.0 |
||
| 1274 | * |
||
| 1275 | * @param array $curr_filter The existing filters to build upon. |
||
| 1276 | * @param array $filters The new filters to add. |
||
| 1277 | * |
||
| 1278 | * @return array The resulting merged filters. |
||
| 1279 | */ |
||
| 1280 | public static function and_es_filters( array $curr_filter, array $filters ) { |
||
| 1295 | |||
| 1296 | /** |
||
| 1297 | * Set the available filters for the search. |
||
| 1298 | * |
||
| 1299 | * These get rendered via the Jetpack_Search_Widget() widget. |
||
| 1300 | * |
||
| 1301 | * Behind the scenes, these are implemented using Elasticsearch Aggregations. |
||
| 1302 | * |
||
| 1303 | * If you do not require counts of how many documents match each filter, please consider using regular WP Query |
||
| 1304 | * arguments instead, such as via the jetpack_search_es_wp_query_args filter |
||
| 1305 | * |
||
| 1306 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1307 | * |
||
| 1308 | * @since 5.0.0 |
||
| 1309 | * |
||
| 1310 | * @param array $aggregations Array of filters (aggregations) to apply to the search |
||
| 1311 | */ |
||
| 1312 | public function set_filters( array $aggregations ) { |
||
| 1320 | |||
| 1321 | /** |
||
| 1322 | * Set the search's facets (deprecated). |
||
| 1323 | * |
||
| 1324 | * @deprecated 5.0 Please use Jetpack_Search::set_filters() instead. |
||
| 1325 | * |
||
| 1326 | * @see Jetpack_Search::set_filters() |
||
| 1327 | * |
||
| 1328 | * @param array $facets Array of facets to apply to the search. |
||
| 1329 | */ |
||
| 1330 | public function set_facets( array $facets ) { |
||
| 1335 | |||
| 1336 | /** |
||
| 1337 | * Get the raw Aggregation results from the Elasticsearch response. |
||
| 1338 | * |
||
| 1339 | * @since 5.0.0 |
||
| 1340 | * |
||
| 1341 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1342 | * |
||
| 1343 | * @return array Array of Aggregations performed on the search. |
||
| 1344 | */ |
||
| 1345 | public function get_search_aggregations_results() { |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * Get the raw Facet results from the Elasticsearch response. |
||
| 1359 | * |
||
| 1360 | * @deprecated 5.0 Please use Jetpack_Search::get_search_aggregations_results() instead. |
||
| 1361 | * |
||
| 1362 | * @see Jetpack_Search::get_search_aggregations_results() |
||
| 1363 | * |
||
| 1364 | * @return array Array of Facets performed on the search. |
||
| 1365 | */ |
||
| 1366 | public function get_search_facets() { |
||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Get the results of the Filters performed, including the number of matching documents. |
||
| 1374 | * |
||
| 1375 | * Returns an array of Filters (keyed by $label, as passed to Jetpack_Search::set_filters()), containing the Filter and all resulting |
||
| 1376 | * matching buckets, the url for applying/removing each bucket, etc. |
||
| 1377 | * |
||
| 1378 | * NOTE - if this is called before the search is performed, an empty array will be returned. Use the $aggregations class |
||
| 1379 | * member if you need to access the raw filters set in Jetpack_Search::set_filters(). |
||
| 1380 | * |
||
| 1381 | * @since 5.0.0 |
||
| 1382 | * |
||
| 1383 | * @param WP_Query $query The optional original WP_Query to use for determining which filters are active. Defaults to the main query. |
||
| 1384 | * |
||
| 1385 | * @return array Array of filters applied and info about them. |
||
| 1386 | */ |
||
| 1387 | public function get_filters( WP_Query $query = null ) { |
||
| 1635 | |||
| 1636 | /** |
||
| 1637 | * Get the results of the facets performed. |
||
| 1638 | * |
||
| 1639 | * @deprecated 5.0 Please use Jetpack_Search::get_filters() instead. |
||
| 1640 | * |
||
| 1641 | * @see Jetpack_Search::get_filters() |
||
| 1642 | * |
||
| 1643 | * @return array $facets Array of facets applied and info about them. |
||
| 1644 | */ |
||
| 1645 | public function get_search_facet_data() { |
||
| 1650 | |||
| 1651 | /** |
||
| 1652 | * Get the filters that are currently applied to this search. |
||
| 1653 | * |
||
| 1654 | * @since 5.0.0 |
||
| 1655 | * |
||
| 1656 | * @return array Array of filters that were applied. |
||
| 1657 | */ |
||
| 1658 | public function get_active_filter_buckets() { |
||
| 1679 | |||
| 1680 | /** |
||
| 1681 | * Get the filters that are currently applied to this search. |
||
| 1682 | * |
||
| 1683 | * @deprecated 5.0 Please use Jetpack_Search::get_active_filter_buckets() instead. |
||
| 1684 | * |
||
| 1685 | * @see Jetpack_Search::get_active_filter_buckets() |
||
| 1686 | * |
||
| 1687 | * @return array Array of filters that were applied. |
||
| 1688 | */ |
||
| 1689 | public function get_current_filters() { |
||
| 1694 | |||
| 1695 | /** |
||
| 1696 | * Calculate the right query var to use for a given taxonomy. |
||
| 1697 | * |
||
| 1698 | * Allows custom code to modify the GET var that is used to represent a given taxonomy, via the jetpack_search_taxonomy_query_var filter. |
||
| 1699 | * |
||
| 1700 | * @since 5.0.0 |
||
| 1701 | * |
||
| 1702 | * @param string $taxonomy_name The name of the taxonomy for which to get the query var. |
||
| 1703 | * |
||
| 1704 | * @return bool|string The query var to use for this taxonomy, or false if none found. |
||
| 1705 | */ |
||
| 1706 | public function get_taxonomy_query_var( $taxonomy_name ) { |
||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * Takes an array of aggregation results, and ensures the array key ordering matches the key order in $desired |
||
| 1728 | * which is the input order. |
||
| 1729 | * |
||
| 1730 | * Necessary because ES does not always return aggregations in the same order that you pass them in, |
||
| 1731 | * and it should be possible to control the display order easily. |
||
| 1732 | * |
||
| 1733 | * @since 5.0.0 |
||
| 1734 | * |
||
| 1735 | * @param array $aggregations Aggregation results to be reordered. |
||
| 1736 | * @param array $desired Array with keys representing the desired ordering. |
||
| 1737 | * |
||
| 1738 | * @return array A new array with reordered keys, matching those in $desired. |
||
| 1739 | */ |
||
| 1740 | public function fix_aggregation_ordering( array $aggregations, array $desired ) { |
||
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Sends events to Tracks when a search filters widget is updated. |
||
| 1758 | * |
||
| 1759 | * @since 5.8.0 |
||
| 1760 | * |
||
| 1761 | * @param string $option The option name. Only "widget_jetpack-search-filters" is cared about. |
||
| 1762 | * @param array $old_value The old option value. |
||
| 1763 | * @param array $new_value The new option value. |
||
| 1764 | */ |
||
| 1765 | public function track_widget_updates( $option, $old_value, $new_value ) { |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * Moves any active search widgets to the inactive category. |
||
| 1784 | * |
||
| 1785 | * @since 5.9.0 |
||
| 1786 | * |
||
| 1787 | * @param string $module Unused. The Jetpack module being disabled. |
||
| 1788 | */ |
||
| 1789 | public function move_search_widgets_to_inactive( $module ) { |
||
| 1823 | } |
||
| 1824 |
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.