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 |
||
| 20 | class Jetpack_Search { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The number of found posts. |
||
| 24 | * |
||
| 25 | * @since 5.0.0 |
||
| 26 | * |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected $found_posts = 0; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The search result, as returned by the WordPress.com REST API. |
||
| 33 | * |
||
| 34 | * @since 5.0.0 |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $search_result; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * This site's blog ID on WordPress.com. |
||
| 42 | * |
||
| 43 | * @since 5.0.0 |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $jetpack_blog_id; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The Elasticsearch aggregations (filters). |
||
| 51 | * |
||
| 52 | * @since 5.0.0 |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $aggregations = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The maximum number of aggregations allowed. |
||
| 60 | * |
||
| 61 | * @since 5.0.0 |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | protected $max_aggregations_count = 100; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Statistics about the last Elasticsearch query. |
||
| 69 | * |
||
| 70 | * @since 5.6.0 |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $last_query_info = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Statistics about the last Elasticsearch query failure. |
||
| 78 | * |
||
| 79 | * @since 5.6.0 |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $last_query_failure_info = array(); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The singleton instance of this class. |
||
| 87 | * |
||
| 88 | * @since 5.0.0 |
||
| 89 | * |
||
| 90 | * @var Jetpack_Search |
||
| 91 | */ |
||
| 92 | protected static $instance; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Languages with custom analyzers. Other languages are supported, but are analyzed with the default analyzer. |
||
| 96 | * |
||
| 97 | * @since 5.0.0 |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | public static $analyzed_langs = array( 'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'eu', 'fa', 'fi', 'fr', 'he', 'hi', 'hu', 'hy', 'id', 'it', 'ja', 'ko', 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh' ); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Jetpack_Search constructor. |
||
| 105 | * |
||
| 106 | * @since 5.0.0 |
||
| 107 | * |
||
| 108 | * Doesn't do anything. This class needs to be initialized via the instance() method instead. |
||
| 109 | */ |
||
| 110 | protected function __construct() { |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Prevent __clone()'ing of this class. |
||
| 115 | * |
||
| 116 | * @since 5.0.0 |
||
| 117 | */ |
||
| 118 | public function __clone() { |
||
| 119 | wp_die( "Please don't __clone Jetpack_Search" ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Prevent __wakeup()'ing of this class. |
||
| 124 | * |
||
| 125 | * @since 5.0.0 |
||
| 126 | */ |
||
| 127 | public function __wakeup() { |
||
| 128 | wp_die( "Please don't __wakeup Jetpack_Search" ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get singleton instance of Jetpack_Search. |
||
| 133 | * |
||
| 134 | * Instantiates and sets up a new instance if needed, or returns the singleton. |
||
| 135 | * |
||
| 136 | * @since 5.0.0 |
||
| 137 | * |
||
| 138 | * @return Jetpack_Search The Jetpack_Search singleton. |
||
| 139 | */ |
||
| 140 | public static function instance() { |
||
| 141 | if ( ! isset( self::$instance ) ) { |
||
| 142 | if ( Jetpack_Search_Options::is_instant_enabled() ) { |
||
| 143 | require_once dirname( __FILE__ ) . '/class-jetpack-instant-search.php'; |
||
| 144 | self::$instance = new Jetpack_Instant_Search(); |
||
| 145 | } else { |
||
| 146 | self::$instance = new Jetpack_Search(); |
||
| 147 | } |
||
| 148 | |||
| 149 | self::$instance->setup(); |
||
| 150 | } |
||
| 151 | |||
| 152 | return self::$instance; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Perform various setup tasks for the class. |
||
| 157 | * |
||
| 158 | * Checks various pre-requisites and adds hooks. |
||
| 159 | * |
||
| 160 | * @since 5.0.0 |
||
| 161 | */ |
||
| 162 | public function setup() { |
||
| 163 | if ( ! Jetpack::is_active() || ! $this->is_search_supported() ) { |
||
| 164 | /** |
||
| 165 | * Fires when the Jetpack Search fails and would fallback to MySQL. |
||
| 166 | * |
||
| 167 | * @module search |
||
| 168 | * @since 7.9.0 |
||
| 169 | * |
||
| 170 | * @param string $reason Reason for Search fallback. |
||
| 171 | * @param mixed $data Data associated with the request, such as attempted search parameters. |
||
| 172 | */ |
||
| 173 | do_action( 'jetpack_search_abort', 'inactive', null ); |
||
| 174 | return; |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->jetpack_blog_id = Jetpack::get_option( 'id' ); |
||
| 178 | |||
| 179 | if ( ! $this->jetpack_blog_id ) { |
||
| 180 | /** This action is documented in modules/search/class.jetpack-search.php */ |
||
| 181 | do_action( 'jetpack_search_abort', 'no_blog_id', null ); |
||
| 182 | return; |
||
| 183 | } |
||
| 184 | |||
| 185 | $this->load_php(); |
||
| 186 | $this->init_hooks(); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Loads the php for this version of search |
||
| 191 | * |
||
| 192 | * @since 8.3.0 |
||
| 193 | */ |
||
| 194 | public function load_php() { |
||
| 195 | require_once dirname( __FILE__ ) . '/class.jetpack-search-helpers.php'; |
||
| 196 | require_once dirname( __FILE__ ) . '/class.jetpack-search-template-tags.php'; |
||
| 197 | require_once JETPACK__PLUGIN_DIR . 'modules/widgets/search.php'; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Setup the various hooks needed for the plugin to take over search duties. |
||
| 202 | * |
||
| 203 | * @since 5.0.0 |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function init_hooks() { |
|
| 206 | if ( ! is_admin() ) { |
||
| 207 | add_filter( 'posts_pre_query', array( $this, 'filter__posts_pre_query' ), 10, 2 ); |
||
| 208 | |||
| 209 | add_filter( 'jetpack_search_es_wp_query_args', array( $this, 'filter__add_date_filter_to_query' ), 10, 2 ); |
||
| 210 | |||
| 211 | add_action( 'did_jetpack_search_query', array( $this, 'store_last_query_info' ) ); |
||
| 212 | add_action( 'failed_jetpack_search_query', array( $this, 'store_query_failure' ) ); |
||
| 213 | |||
| 214 | add_action( 'init', array( $this, 'set_filters_from_widgets' ) ); |
||
| 215 | |||
| 216 | add_action( 'pre_get_posts', array( $this, 'maybe_add_post_type_as_var' ) ); |
||
| 217 | } else { |
||
| 218 | add_action( 'update_option', array( $this, 'track_widget_updates' ), 10, 3 ); |
||
| 219 | } |
||
| 220 | |||
| 221 | add_action( 'jetpack_deactivate_module_search', array( $this, 'move_search_widgets_to_inactive' ) ); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Is search supported on the current plan |
||
| 226 | * |
||
| 227 | * @since 6.0 |
||
| 228 | * Loads scripts for Tracks analytics library |
||
| 229 | */ |
||
| 230 | public function is_search_supported() { |
||
| 231 | if ( method_exists( 'Jetpack_Plan', 'supports' ) ) { |
||
| 232 | return Jetpack_Plan::supports( 'search' ); |
||
| 233 | } |
||
| 234 | return false; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Does this site have a VIP index |
||
| 239 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
| 240 | * |
||
| 241 | * @since 6.0 |
||
| 242 | * @return string $script_version Version number. |
||
| 243 | */ |
||
| 244 | public function has_vip_index() { |
||
| 245 | return defined( 'JETPACK_SEARCH_VIP_INDEX' ) && JETPACK_SEARCH_VIP_INDEX; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * When an Elasticsearch query fails, this stores it and enqueues some debug information in the footer. |
||
| 250 | * |
||
| 251 | * @since 5.6.0 |
||
| 252 | * |
||
| 253 | * @param array $meta Information about the failure. |
||
| 254 | */ |
||
| 255 | public function store_query_failure( $meta ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Outputs information about the last Elasticsearch failure. |
||
| 262 | * |
||
| 263 | * @since 5.6.0 |
||
| 264 | */ |
||
| 265 | public function print_query_failure() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Stores information about the last Elasticsearch query and enqueues some debug information in the footer. |
||
| 278 | * |
||
| 279 | * @since 5.6.0 |
||
| 280 | * |
||
| 281 | * @param array $meta Information about the query. |
||
| 282 | */ |
||
| 283 | public function store_last_query_info( $meta ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Outputs information about the last Elasticsearch search. |
||
| 290 | * |
||
| 291 | * @since 5.6.0 |
||
| 292 | */ |
||
| 293 | public function print_query_success() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the last query information, or false if no information was stored. |
||
| 312 | * |
||
| 313 | * @since 5.8.0 |
||
| 314 | * |
||
| 315 | * @return bool|array |
||
| 316 | */ |
||
| 317 | public function get_last_query_info() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns the last query failure information, or false if no failure information was stored. |
||
| 323 | * |
||
| 324 | * @since 5.8.0 |
||
| 325 | * |
||
| 326 | * @return bool|array |
||
| 327 | */ |
||
| 328 | public function get_last_query_failure_info() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Wraps a WordPress filter called "jetpack_search_disable_widget_filters" that allows |
||
| 334 | * developers to disable filters supplied by the search widget. Useful if filters are |
||
| 335 | * being defined at the code level. |
||
| 336 | * |
||
| 337 | * @since 5.7.0 |
||
| 338 | * @deprecated 5.8.0 Use Jetpack_Search_Helpers::are_filters_by_widget_disabled() directly. |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function are_filters_by_widget_disabled() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Retrieves a list of known Jetpack search filters widget IDs, gets the filters for each widget, |
||
| 348 | * and applies those filters to this Jetpack_Search object. |
||
| 349 | * |
||
| 350 | * @since 5.7.0 |
||
| 351 | */ |
||
| 352 | public function set_filters_from_widgets() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Restricts search results to certain post types via a GET argument. |
||
| 366 | * |
||
| 367 | * @since 5.8.0 |
||
| 368 | * |
||
| 369 | * @param WP_Query $query A WP_Query instance. |
||
| 370 | */ |
||
| 371 | public function maybe_add_post_type_as_var( WP_Query $query ) { |
||
| 372 | $post_type = ( ! empty( $_GET['post_type'] ) ) ? $_GET['post_type'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 373 | if ( $this->should_handle_query( $query ) && $post_type ) { |
||
| 374 | $post_types = ( is_string( $post_type ) && false !== strpos( $post_type, ',' ) ) |
||
| 375 | ? explode( ',', $post_type ) |
||
| 376 | : (array) $post_type; |
||
| 377 | $post_types = array_map( 'sanitize_key', $post_types ); |
||
| 378 | $query->set( 'post_type', $post_types ); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Run a search on the WordPress.com public API. |
||
| 384 | * |
||
| 385 | * @since 5.0.0 |
||
| 386 | * |
||
| 387 | * @param array $es_args Args conforming to the WP.com /sites/<blog_id>/search endpoint. |
||
| 388 | * |
||
| 389 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
| 390 | */ |
||
| 391 | public function search( array $es_args ) { |
||
| 392 | $endpoint = sprintf( '/sites/%s/search', $this->jetpack_blog_id ); |
||
| 393 | $service_url = 'https://public-api.wordpress.com/rest/v1' . $endpoint; |
||
| 394 | |||
| 395 | $do_authenticated_request = false; |
||
| 396 | |||
| 397 | if ( class_exists( 'Automattic\\Jetpack\\Connection\\Client' ) && |
||
| 398 | isset( $es_args['authenticated_request'] ) && |
||
| 399 | true === $es_args['authenticated_request'] ) { |
||
| 400 | $do_authenticated_request = true; |
||
| 401 | } |
||
| 402 | |||
| 403 | unset( $es_args['authenticated_request'] ); |
||
| 404 | |||
| 405 | $request_args = array( |
||
| 406 | 'headers' => array( |
||
| 407 | 'Content-Type' => 'application/json', |
||
| 408 | ), |
||
| 409 | 'timeout' => 10, |
||
| 410 | 'user-agent' => 'jetpack_search', |
||
| 411 | ); |
||
| 412 | |||
| 413 | $request_body = wp_json_encode( $es_args ); |
||
| 414 | |||
| 415 | $start_time = microtime( true ); |
||
| 416 | |||
| 417 | if ( $do_authenticated_request ) { |
||
| 418 | $request_args['method'] = 'POST'; |
||
| 419 | |||
| 420 | $request = Client::wpcom_json_api_request_as_blog( $endpoint, Client::WPCOM_JSON_API_VERSION, $request_args, $request_body ); |
||
| 421 | } else { |
||
| 422 | $request_args = array_merge( |
||
| 423 | $request_args, |
||
| 424 | array( |
||
| 425 | 'body' => $request_body, |
||
| 426 | ) |
||
| 427 | ); |
||
| 428 | |||
| 429 | $request = wp_remote_post( $service_url, $request_args ); |
||
| 430 | } |
||
| 431 | |||
| 432 | $end_time = microtime( true ); |
||
| 433 | |||
| 434 | if ( is_wp_error( $request ) ) { |
||
| 435 | return $request; |
||
| 436 | } |
||
| 437 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
| 438 | |||
| 439 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
| 440 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
| 441 | } |
||
| 442 | |||
| 443 | $response = json_decode( wp_remote_retrieve_body( $request ), true ); |
||
| 444 | |||
| 445 | $took = is_array( $response ) && ! empty( $response['took'] ) |
||
| 446 | ? $response['took'] |
||
| 447 | : null; |
||
| 448 | |||
| 449 | $query = array( |
||
| 450 | 'args' => $es_args, |
||
| 451 | 'response' => $response, |
||
| 452 | 'response_code' => $response_code, |
||
| 453 | 'elapsed_time' => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms. |
||
| 454 | 'es_time' => $took, |
||
| 455 | 'url' => $service_url, |
||
| 456 | ); |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Fires after a search request has been performed. |
||
| 460 | * |
||
| 461 | * Includes the following info in the $query parameter: |
||
| 462 | * |
||
| 463 | * array args Array of Elasticsearch arguments for the search |
||
| 464 | * array response Raw API response, JSON decoded |
||
| 465 | * int response_code HTTP response code of the request |
||
| 466 | * float elapsed_time Roundtrip time of the search request, in milliseconds |
||
| 467 | * float es_time Amount of time Elasticsearch spent running the request, in milliseconds |
||
| 468 | * string url API url that was queried |
||
| 469 | * |
||
| 470 | * @module search |
||
| 471 | * |
||
| 472 | * @since 5.0.0 |
||
| 473 | * @since 5.8.0 This action now fires on all queries instead of just successful queries. |
||
| 474 | * |
||
| 475 | * @param array $query Array of information about the query performed |
||
| 476 | */ |
||
| 477 | do_action( 'did_jetpack_search_query', $query ); |
||
| 478 | |||
| 479 | View Code Duplication | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
|
| 480 | /** |
||
| 481 | * Fires after a search query request has failed |
||
| 482 | * |
||
| 483 | * @module search |
||
| 484 | * |
||
| 485 | * @since 5.6.0 |
||
| 486 | * |
||
| 487 | * @param array Array containing the response code and response from the failed search query |
||
| 488 | */ |
||
| 489 | do_action( |
||
| 490 | 'failed_jetpack_search_query', |
||
| 491 | array( |
||
| 492 | 'response_code' => $response_code, |
||
| 493 | 'json' => $response, |
||
| 494 | ) |
||
| 495 | ); |
||
| 496 | |||
| 497 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
| 498 | } |
||
| 499 | |||
| 500 | return $response; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Bypass the normal Search query and offload it to Jetpack servers. |
||
| 505 | * |
||
| 506 | * This is the main hook of the plugin and is responsible for returning the posts that match the search query. |
||
| 507 | * |
||
| 508 | * @since 5.0.0 |
||
| 509 | * |
||
| 510 | * @param array $posts Current array of posts (still pre-query). |
||
| 511 | * @param WP_Query $query The WP_Query being filtered. |
||
| 512 | * |
||
| 513 | * @return array Array of matching posts. |
||
| 514 | */ |
||
| 515 | public function filter__posts_pre_query( $posts, $query ) { |
||
| 516 | if ( ! $this->should_handle_query( $query ) ) { |
||
| 517 | // Intentionally not adding the 'jetpack_search_abort' action since this should fire for every request except for search. |
||
| 518 | return $posts; |
||
| 519 | } |
||
| 520 | |||
| 521 | $this->do_search( $query ); |
||
| 522 | |||
| 523 | if ( ! is_array( $this->search_result ) ) { |
||
| 524 | /** This action is documented in modules/search/class.jetpack-search.php */ |
||
| 525 | do_action( 'jetpack_search_abort', 'no_search_results_array', $this->search_result ); |
||
| 526 | return $posts; |
||
| 527 | } |
||
| 528 | |||
| 529 | // If no results, nothing to do. |
||
| 530 | if ( ! count( $this->search_result['results']['hits'] ) ) { |
||
| 531 | return array(); |
||
| 532 | } |
||
| 533 | |||
| 534 | $post_ids = array(); |
||
| 535 | |||
| 536 | foreach ( $this->search_result['results']['hits'] as $result ) { |
||
| 537 | $post_ids[] = (int) $result['fields']['post_id']; |
||
| 538 | } |
||
| 539 | |||
| 540 | // Query all posts now. |
||
| 541 | $args = array( |
||
| 542 | 'post__in' => $post_ids, |
||
| 543 | 'orderby' => 'post__in', |
||
| 544 | 'perm' => 'readable', |
||
| 545 | 'post_type' => 'any', |
||
| 546 | 'ignore_sticky_posts' => true, |
||
| 547 | 'suppress_filters' => true, |
||
| 548 | ); |
||
| 549 | |||
| 550 | $posts_query = new WP_Query( $args ); |
||
| 551 | |||
| 552 | // 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. |
||
| 553 | $query->found_posts = $this->found_posts; |
||
| 554 | $query->max_num_pages = ceil( $this->found_posts / $query->get( 'posts_per_page' ) ); |
||
| 555 | |||
| 556 | return $posts_query->posts; |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Build up the search, then run it against the Jetpack servers. |
||
| 561 | * |
||
| 562 | * @since 5.0.0 |
||
| 563 | * |
||
| 564 | * @param WP_Query $query The original WP_Query to use for the parameters of our search. |
||
| 565 | */ |
||
| 566 | public function do_search( WP_Query $query ) { |
||
| 567 | if ( ! $this->should_handle_query( $query ) ) { |
||
| 568 | // If we make it here, either 'filter__posts_pre_query' somehow allowed it or a different entry to do_search. |
||
| 569 | /** This action is documented in modules/search/class.jetpack-search.php */ |
||
| 570 | do_action( 'jetpack_search_abort', 'search_attempted_non_search_query', $query ); |
||
| 571 | return; |
||
| 572 | } |
||
| 573 | |||
| 574 | $page = ( $query->get( 'paged' ) ) ? absint( $query->get( 'paged' ) ) : 1; |
||
| 575 | |||
| 576 | // Get maximum allowed offset and posts per page values for the API. |
||
| 577 | $max_offset = Jetpack_Search_Helpers::get_max_offset(); |
||
| 578 | $max_posts_per_page = Jetpack_Search_Helpers::get_max_posts_per_page(); |
||
| 579 | |||
| 580 | $posts_per_page = $query->get( 'posts_per_page' ); |
||
| 581 | if ( $posts_per_page > $max_posts_per_page ) { |
||
| 582 | $posts_per_page = $max_posts_per_page; |
||
| 583 | } |
||
| 584 | |||
| 585 | // Start building the WP-style search query args. |
||
| 586 | // They'll be translated to ES format args later. |
||
| 587 | $es_wp_query_args = array( |
||
| 588 | 'query' => $query->get( 's' ), |
||
| 589 | 'posts_per_page' => $posts_per_page, |
||
| 590 | 'paged' => $page, |
||
| 591 | 'orderby' => $query->get( 'orderby' ), |
||
| 592 | 'order' => $query->get( 'order' ), |
||
| 593 | ); |
||
| 594 | |||
| 595 | if ( ! empty( $this->aggregations ) ) { |
||
| 596 | $es_wp_query_args['aggregations'] = $this->aggregations; |
||
| 597 | } |
||
| 598 | |||
| 599 | // Did we query for authors? |
||
| 600 | if ( $query->get( 'author_name' ) ) { |
||
| 601 | $es_wp_query_args['author_name'] = $query->get( 'author_name' ); |
||
| 602 | } |
||
| 603 | |||
| 604 | $es_wp_query_args['post_type'] = $this->get_es_wp_query_post_type_for_query( $query ); |
||
| 605 | $es_wp_query_args['terms'] = $this->get_es_wp_query_terms_for_query( $query ); |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Modify the search query parameters, such as controlling the post_type. |
||
| 609 | * |
||
| 610 | * These arguments are in the format of WP_Query arguments |
||
| 611 | * |
||
| 612 | * @module search |
||
| 613 | * |
||
| 614 | * @since 5.0.0 |
||
| 615 | * |
||
| 616 | * @param array $es_wp_query_args The current query args, in WP_Query format. |
||
| 617 | * @param WP_Query $query The original WP_Query object. |
||
| 618 | */ |
||
| 619 | $es_wp_query_args = apply_filters( 'jetpack_search_es_wp_query_args', $es_wp_query_args, $query ); |
||
| 620 | |||
| 621 | // If page * posts_per_page is greater than our max offset, send a 404. This is necessary because the offset is |
||
| 622 | // capped at Jetpack_Search_Helpers::get_max_offset(), so a high page would always return the last page of results otherwise. |
||
| 623 | if ( ( $es_wp_query_args['paged'] * $es_wp_query_args['posts_per_page'] ) > $max_offset ) { |
||
| 624 | $query->set_404(); |
||
| 625 | |||
| 626 | return; |
||
| 627 | } |
||
| 628 | |||
| 629 | // If there were no post types returned, then 404 to avoid querying against non-public post types, which could |
||
| 630 | // happen if we don't add the post type restriction to the ES query. |
||
| 631 | if ( empty( $es_wp_query_args['post_type'] ) ) { |
||
| 632 | $query->set_404(); |
||
| 633 | |||
| 634 | return; |
||
| 635 | } |
||
| 636 | |||
| 637 | // Convert the WP-style args into ES args. |
||
| 638 | $es_query_args = $this->convert_wp_es_to_es_args( $es_wp_query_args ); |
||
| 639 | |||
| 640 | // Only trust ES to give us IDs, not the content since it is a mirror. |
||
| 641 | $es_query_args['fields'] = array( |
||
| 642 | 'post_id', |
||
| 643 | ); |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Modify the underlying ES query that is passed to the search endpoint. The returned args must represent a valid ES query |
||
| 647 | * |
||
| 648 | * This filter is harder to use if you're unfamiliar with ES, but allows complete control over the query |
||
| 649 | * |
||
| 650 | * @module search |
||
| 651 | * |
||
| 652 | * @since 5.0.0 |
||
| 653 | * |
||
| 654 | * @param array $es_query_args The raw Elasticsearch query args. |
||
| 655 | * @param WP_Query $query The original WP_Query object. |
||
| 656 | */ |
||
| 657 | $es_query_args = apply_filters( 'jetpack_search_es_query_args', $es_query_args, $query ); |
||
| 658 | |||
| 659 | // Do the actual search query! |
||
| 660 | $this->search_result = $this->search( $es_query_args ); |
||
| 661 | |||
| 662 | if ( is_wp_error( $this->search_result ) || ! is_array( $this->search_result ) || empty( $this->search_result['results'] ) || empty( $this->search_result['results']['hits'] ) ) { |
||
| 663 | $this->found_posts = 0; |
||
| 664 | |||
| 665 | return; |
||
| 666 | } |
||
| 667 | |||
| 668 | // If we have aggregations, fix the ordering to match the input order (ES doesn't guarantee the return order). |
||
| 669 | if ( isset( $this->search_result['results']['aggregations'] ) && ! empty( $this->search_result['results']['aggregations'] ) ) { |
||
| 670 | $this->search_result['results']['aggregations'] = $this->fix_aggregation_ordering( $this->search_result['results']['aggregations'], $this->aggregations ); |
||
| 671 | } |
||
| 672 | |||
| 673 | // Total number of results for paging purposes. Capped at $max_offset + $posts_per_page, as deep paging gets quite expensive. |
||
| 674 | $this->found_posts = min( $this->search_result['results']['total'], $max_offset + $posts_per_page ); |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * If the query has already been run before filters have been updated, then we need to re-run the query |
||
| 679 | * to get the latest aggregations. |
||
| 680 | * |
||
| 681 | * This is especially useful for supporting widget management in the customizer. |
||
| 682 | * |
||
| 683 | * @since 5.8.0 |
||
| 684 | * |
||
| 685 | * @return bool Whether the query was successful or not. |
||
| 686 | */ |
||
| 687 | public function update_search_results_aggregations() { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Given a WP_Query, convert its WP_Tax_Query (if present) into the WP-style Elasticsearch term arguments for the search. |
||
| 704 | * |
||
| 705 | * @since 5.0.0 |
||
| 706 | * |
||
| 707 | * @param WP_Query $query The original WP_Query object for which to parse the taxonomy query. |
||
| 708 | * |
||
| 709 | * @return array The new WP-style Elasticsearch arguments (that will be converted into 'real' Elasticsearch arguments). |
||
| 710 | */ |
||
| 711 | public function get_es_wp_query_terms_for_query( WP_Query $query ) { |
||
| 712 | $args = array(); |
||
| 713 | |||
| 714 | $the_tax_query = $query->tax_query; |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Parse out the post type from a WP_Query. |
||
| 746 | * |
||
| 747 | * Only allows post types that are not marked as 'exclude_from_search'. |
||
| 748 | * |
||
| 749 | * @since 5.0.0 |
||
| 750 | * |
||
| 751 | * @param WP_Query $query Original WP_Query object. |
||
| 752 | * |
||
| 753 | * @return array Array of searchable post types corresponding to the original query. |
||
| 754 | */ |
||
| 755 | public function get_es_wp_query_post_type_for_query( WP_Query $query ) { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Initialize widgets for the Search module (on wp.com only). |
||
| 796 | * |
||
| 797 | * @module search |
||
| 798 | */ |
||
| 799 | public function action__widgets_init() { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Get the Elasticsearch result. |
||
| 807 | * |
||
| 808 | * @since 5.0.0 |
||
| 809 | * |
||
| 810 | * @param bool $raw If true, does not check for WP_Error or return the 'results' array - the JSON decoded HTTP response. |
||
| 811 | * |
||
| 812 | * @return array|bool The search results, or false if there was a failure. |
||
| 813 | */ |
||
| 814 | public function get_search_result( $raw = false ) { |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Add the date portion of a WP_Query onto the query args. |
||
| 824 | * |
||
| 825 | * @since 5.0.0 |
||
| 826 | * |
||
| 827 | * @param array $es_wp_query_args The Elasticsearch query arguments in WordPress form. |
||
| 828 | * @param WP_Query $query The original WP_Query. |
||
| 829 | * |
||
| 830 | * @return array The es wp query args, with date filters added (as needed). |
||
| 831 | */ |
||
| 832 | public function filter__add_date_filter_to_query( array $es_wp_query_args, WP_Query $query ) { |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Converts WP_Query style args to Elasticsearch args. |
||
| 867 | * |
||
| 868 | * @since 5.0.0 |
||
| 869 | * |
||
| 870 | * @param array $args Array of WP_Query style arguments. |
||
| 871 | * |
||
| 872 | * @return array Array of ES style query arguments. |
||
| 873 | */ |
||
| 874 | public function convert_wp_es_to_es_args( array $args ) { |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Given an array of aggregations, parse and add them onto the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1269 | * |
||
| 1270 | * @since 5.0.0 |
||
| 1271 | * |
||
| 1272 | * @param array $aggregations Array of aggregations (filters) to add to the Jetpack_WPES_Query_Builder. |
||
| 1273 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1274 | */ |
||
| 1275 | public function add_aggregations_to_es_query_builder( array $aggregations, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1295 | |||
| 1296 | /** |
||
| 1297 | * Given an individual taxonomy aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1298 | * |
||
| 1299 | * @since 5.0.0 |
||
| 1300 | * |
||
| 1301 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1302 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1303 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1304 | */ |
||
| 1305 | public function add_taxonomy_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1332 | |||
| 1333 | /** |
||
| 1334 | * Given an individual post_type aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1335 | * |
||
| 1336 | * @since 5.0.0 |
||
| 1337 | * |
||
| 1338 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1339 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1340 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1341 | */ |
||
| 1342 | public function add_post_type_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Given an individual date_histogram 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_date_histogram_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * And an existing filter object with a list of additional filters. |
||
| 1385 | * |
||
| 1386 | * Attempts to optimize the filters somewhat. |
||
| 1387 | * |
||
| 1388 | * @since 5.0.0 |
||
| 1389 | * |
||
| 1390 | * @param array $curr_filter The existing filters to build upon. |
||
| 1391 | * @param array $filters The new filters to add. |
||
| 1392 | * |
||
| 1393 | * @return array The resulting merged filters. |
||
| 1394 | */ |
||
| 1395 | public static function and_es_filters( array $curr_filter, array $filters ) { |
||
| 1410 | |||
| 1411 | /** |
||
| 1412 | * Set the available filters for the search. |
||
| 1413 | * |
||
| 1414 | * These get rendered via the Jetpack_Search_Widget() widget. |
||
| 1415 | * |
||
| 1416 | * Behind the scenes, these are implemented using Elasticsearch Aggregations. |
||
| 1417 | * |
||
| 1418 | * If you do not require counts of how many documents match each filter, please consider using regular WP Query |
||
| 1419 | * arguments instead, such as via the jetpack_search_es_wp_query_args filter |
||
| 1420 | * |
||
| 1421 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1422 | * |
||
| 1423 | * @since 5.0.0 |
||
| 1424 | * |
||
| 1425 | * @param array $aggregations Array of filters (aggregations) to apply to the search. |
||
| 1426 | */ |
||
| 1427 | public function set_filters( array $aggregations ) { |
||
| 1435 | |||
| 1436 | /** |
||
| 1437 | * Set the search's facets (deprecated). |
||
| 1438 | * |
||
| 1439 | * @deprecated 5.0 Please use Jetpack_Search::set_filters() instead. |
||
| 1440 | * |
||
| 1441 | * @see Jetpack_Search::set_filters() |
||
| 1442 | * |
||
| 1443 | * @param array $facets Array of facets to apply to the search. |
||
| 1444 | */ |
||
| 1445 | public function set_facets( array $facets ) { |
||
| 1450 | |||
| 1451 | /** |
||
| 1452 | * Get the raw Aggregation results from the Elasticsearch response. |
||
| 1453 | * |
||
| 1454 | * @since 5.0.0 |
||
| 1455 | * |
||
| 1456 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1457 | * |
||
| 1458 | * @return array Array of Aggregations performed on the search. |
||
| 1459 | */ |
||
| 1460 | public function get_search_aggregations_results() { |
||
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Get the raw Facet results from the Elasticsearch response. |
||
| 1474 | * |
||
| 1475 | * @deprecated 5.0 Please use Jetpack_Search::get_search_aggregations_results() instead. |
||
| 1476 | * |
||
| 1477 | * @see Jetpack_Search::get_search_aggregations_results() |
||
| 1478 | * |
||
| 1479 | * @return array Array of Facets performed on the search. |
||
| 1480 | */ |
||
| 1481 | public function get_search_facets() { |
||
| 1486 | |||
| 1487 | /** |
||
| 1488 | * Get the results of the Filters performed, including the number of matching documents. |
||
| 1489 | * |
||
| 1490 | * Returns an array of Filters (keyed by $label, as passed to Jetpack_Search::set_filters()), containing the Filter and all resulting |
||
| 1491 | * matching buckets, the url for applying/removing each bucket, etc. |
||
| 1492 | * |
||
| 1493 | * NOTE - if this is called before the search is performed, an empty array will be returned. Use the $aggregations class |
||
| 1494 | * member if you need to access the raw filters set in Jetpack_Search::set_filters(). |
||
| 1495 | * |
||
| 1496 | * @since 5.0.0 |
||
| 1497 | * |
||
| 1498 | * @param WP_Query $query The optional original WP_Query to use for determining which filters are active. Defaults to the main query. |
||
| 1499 | * |
||
| 1500 | * @return array Array of filters applied and info about them. |
||
| 1501 | */ |
||
| 1502 | public function get_filters( WP_Query $query = null ) { |
||
| 1763 | |||
| 1764 | /** |
||
| 1765 | * Get the results of the facets performed. |
||
| 1766 | * |
||
| 1767 | * @deprecated 5.0 Please use Jetpack_Search::get_filters() instead. |
||
| 1768 | * |
||
| 1769 | * @see Jetpack_Search::get_filters() |
||
| 1770 | * |
||
| 1771 | * @return array $facets Array of facets applied and info about them. |
||
| 1772 | */ |
||
| 1773 | public function get_search_facet_data() { |
||
| 1778 | |||
| 1779 | /** |
||
| 1780 | * Get the filters that are currently applied to this search. |
||
| 1781 | * |
||
| 1782 | * @since 5.0.0 |
||
| 1783 | * |
||
| 1784 | * @return array Array of filters that were applied. |
||
| 1785 | */ |
||
| 1786 | public function get_active_filter_buckets() { |
||
| 1807 | |||
| 1808 | /** |
||
| 1809 | * Get the filters that are currently applied to this search. |
||
| 1810 | * |
||
| 1811 | * @deprecated 5.0 Please use Jetpack_Search::get_active_filter_buckets() instead. |
||
| 1812 | * |
||
| 1813 | * @see Jetpack_Search::get_active_filter_buckets() |
||
| 1814 | * |
||
| 1815 | * @return array Array of filters that were applied. |
||
| 1816 | */ |
||
| 1817 | public function get_current_filters() { |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * Calculate the right query var to use for a given taxonomy. |
||
| 1825 | * |
||
| 1826 | * Allows custom code to modify the GET var that is used to represent a given taxonomy, via the jetpack_search_taxonomy_query_var filter. |
||
| 1827 | * |
||
| 1828 | * @since 5.0.0 |
||
| 1829 | * |
||
| 1830 | * @param string $taxonomy_name The name of the taxonomy for which to get the query var. |
||
| 1831 | * |
||
| 1832 | * @return bool|string The query var to use for this taxonomy, or false if none found. |
||
| 1833 | */ |
||
| 1834 | public function get_taxonomy_query_var( $taxonomy_name ) { |
||
| 1853 | |||
| 1854 | /** |
||
| 1855 | * Takes an array of aggregation results, and ensures the array key ordering matches the key order in $desired |
||
| 1856 | * which is the input order. |
||
| 1857 | * |
||
| 1858 | * Necessary because ES does not always return aggregations in the same order that you pass them in, |
||
| 1859 | * and it should be possible to control the display order easily. |
||
| 1860 | * |
||
| 1861 | * @since 5.0.0 |
||
| 1862 | * |
||
| 1863 | * @param array $aggregations Aggregation results to be reordered. |
||
| 1864 | * @param array $desired Array with keys representing the desired ordering. |
||
| 1865 | * |
||
| 1866 | * @return array A new array with reordered keys, matching those in $desired. |
||
| 1867 | */ |
||
| 1868 | public function fix_aggregation_ordering( array $aggregations, array $desired ) { |
||
| 1883 | |||
| 1884 | /** |
||
| 1885 | * Sends events to Tracks when a search filters widget is updated. |
||
| 1886 | * |
||
| 1887 | * @since 5.8.0 |
||
| 1888 | * |
||
| 1889 | * @param string $option The option name. Only "widget_jetpack-search-filters" is cared about. |
||
| 1890 | * @param array $old_value The old option value. |
||
| 1891 | * @param array $new_value The new option value. |
||
| 1892 | */ |
||
| 1893 | public function track_widget_updates( $option, $old_value, $new_value ) { |
||
| 1910 | |||
| 1911 | /** |
||
| 1912 | * Moves any active search widgets to the inactive category. |
||
| 1913 | * |
||
| 1914 | * @since 5.9.0 |
||
| 1915 | * |
||
| 1916 | * @param string $module Unused. The Jetpack module being disabled. |
||
| 1917 | */ |
||
| 1918 | public function move_search_widgets_to_inactive( $module ) { |
||
| 1952 | |||
| 1953 | /** |
||
| 1954 | * Determine whether a given WP_Query should be handled by ElasticSearch. |
||
| 1955 | * |
||
| 1956 | * @param WP_Query $query The WP_Query object. |
||
| 1957 | * |
||
| 1958 | * @return bool |
||
| 1959 | */ |
||
| 1960 | public function should_handle_query( $query ) { |
||
| 1973 | |||
| 1974 | /** |
||
| 1975 | * Transforms an array with fields name as keys and boosts as value into |
||
| 1976 | * shorthand "caret" format. |
||
| 1977 | * |
||
| 1978 | * @param array $fields_boost [ "title" => "2", "content" => "1" ]. |
||
| 1979 | * |
||
| 1980 | * @return array [ "title^2", "content^1" ] |
||
| 1981 | */ |
||
| 1982 | private function _get_caret_boosted_fields( array $fields_boost ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
||
| 1989 | |||
| 1990 | /** |
||
| 1991 | * Apply a multiplier to boost values. |
||
| 1992 | * |
||
| 1993 | * @param array $fields_boost [ "title" => 2, "content" => 1 ]. |
||
| 1994 | * @param array $fields_boost_multiplier [ "title" => 0.1234 ]. |
||
| 1995 | * |
||
| 1996 | * @return array [ "title" => "0.247", "content" => "1.000" ] |
||
| 1997 | */ |
||
| 1998 | private function _apply_boosts_multiplier( array $fields_boost, array $fields_boost_multiplier ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
||
| 2015 | } |
||
| 2016 |
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.