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 | $this->base_load_php(); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Loads the PHP common to all search. Should be called from extending classes. |
||
| 200 | */ |
||
| 201 | protected function base_load_php() { |
||
| 202 | require_once dirname( __FILE__ ) . '/class.jetpack-search-helpers.php'; |
||
| 203 | require_once dirname( __FILE__ ) . '/class.jetpack-search-template-tags.php'; |
||
| 204 | require_once JETPACK__PLUGIN_DIR . 'modules/widgets/search.php'; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Setup the various hooks needed for the plugin to take over search duties. |
||
| 209 | * |
||
| 210 | * @since 5.0.0 |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function init_hooks() { |
|
| 213 | if ( ! is_admin() ) { |
||
| 214 | add_filter( 'posts_pre_query', array( $this, 'filter__posts_pre_query' ), 10, 2 ); |
||
| 215 | |||
| 216 | add_filter( 'jetpack_search_es_wp_query_args', array( $this, 'filter__add_date_filter_to_query' ), 10, 2 ); |
||
| 217 | |||
| 218 | add_action( 'did_jetpack_search_query', array( $this, 'store_last_query_info' ) ); |
||
| 219 | add_action( 'failed_jetpack_search_query', array( $this, 'store_query_failure' ) ); |
||
| 220 | |||
| 221 | add_action( 'init', array( $this, 'set_filters_from_widgets' ) ); |
||
| 222 | |||
| 223 | add_action( 'pre_get_posts', array( $this, 'maybe_add_post_type_as_var' ) ); |
||
| 224 | } else { |
||
| 225 | add_action( 'update_option', array( $this, 'track_widget_updates' ), 10, 3 ); |
||
| 226 | } |
||
| 227 | |||
| 228 | add_action( 'jetpack_deactivate_module_search', array( $this, 'move_search_widgets_to_inactive' ) ); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Is search supported on the current plan |
||
| 233 | * |
||
| 234 | * @since 6.0 |
||
| 235 | * Loads scripts for Tracks analytics library |
||
| 236 | */ |
||
| 237 | public function is_search_supported() { |
||
| 238 | if ( method_exists( 'Jetpack_Plan', 'supports' ) ) { |
||
| 239 | return Jetpack_Plan::supports( 'search' ); |
||
| 240 | } |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Does this site have a VIP index |
||
| 246 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
| 247 | * |
||
| 248 | * @since 6.0 |
||
| 249 | * @return string $script_version Version number. |
||
| 250 | */ |
||
| 251 | public function has_vip_index() { |
||
| 252 | return defined( 'JETPACK_SEARCH_VIP_INDEX' ) && JETPACK_SEARCH_VIP_INDEX; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * When an Elasticsearch query fails, this stores it and enqueues some debug information in the footer. |
||
| 257 | * |
||
| 258 | * @since 5.6.0 |
||
| 259 | * |
||
| 260 | * @param array $meta Information about the failure. |
||
| 261 | */ |
||
| 262 | public function store_query_failure( $meta ) { |
||
| 263 | $this->last_query_failure_info = $meta; |
||
| 264 | add_action( 'wp_footer', array( $this, 'print_query_failure' ) ); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Outputs information about the last Elasticsearch failure. |
||
| 269 | * |
||
| 270 | * @since 5.6.0 |
||
| 271 | */ |
||
| 272 | public function print_query_failure() { |
||
| 273 | if ( $this->last_query_failure_info ) { |
||
|
|
|||
| 274 | printf( |
||
| 275 | '<!-- Jetpack Search failed with code %s: %s - %s -->', |
||
| 276 | esc_html( $this->last_query_failure_info['response_code'] ), |
||
| 277 | esc_html( $this->last_query_failure_info['json']['error'] ), |
||
| 278 | esc_html( $this->last_query_failure_info['json']['message'] ) |
||
| 279 | ); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Stores information about the last Elasticsearch query and enqueues some debug information in the footer. |
||
| 285 | * |
||
| 286 | * @since 5.6.0 |
||
| 287 | * |
||
| 288 | * @param array $meta Information about the query. |
||
| 289 | */ |
||
| 290 | public function store_last_query_info( $meta ) { |
||
| 291 | $this->last_query_info = $meta; |
||
| 292 | add_action( 'wp_footer', array( $this, 'print_query_success' ) ); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Outputs information about the last Elasticsearch search. |
||
| 297 | * |
||
| 298 | * @since 5.6.0 |
||
| 299 | */ |
||
| 300 | public function print_query_success() { |
||
| 301 | if ( $this->last_query_info ) { |
||
| 302 | printf( |
||
| 303 | '<!-- Jetpack Search took %s ms, ES time %s ms -->', |
||
| 304 | intval( $this->last_query_info['elapsed_time'] ), |
||
| 305 | esc_html( $this->last_query_info['es_time'] ) |
||
| 306 | ); |
||
| 307 | |||
| 308 | if ( isset( $_GET['searchdebug'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 309 | printf( |
||
| 310 | '<!-- Query response data: %s -->', |
||
| 311 | esc_html( print_r( $this->last_query_info, 1 ) ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the last query information, or false if no information was stored. |
||
| 319 | * |
||
| 320 | * @since 5.8.0 |
||
| 321 | * |
||
| 322 | * @return bool|array |
||
| 323 | */ |
||
| 324 | public function get_last_query_info() { |
||
| 325 | return empty( $this->last_query_info ) ? false : $this->last_query_info; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Returns the last query failure information, or false if no failure information was stored. |
||
| 330 | * |
||
| 331 | * @since 5.8.0 |
||
| 332 | * |
||
| 333 | * @return bool|array |
||
| 334 | */ |
||
| 335 | public function get_last_query_failure_info() { |
||
| 336 | return empty( $this->last_query_failure_info ) ? false : $this->last_query_failure_info; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Wraps a WordPress filter called "jetpack_search_disable_widget_filters" that allows |
||
| 341 | * developers to disable filters supplied by the search widget. Useful if filters are |
||
| 342 | * being defined at the code level. |
||
| 343 | * |
||
| 344 | * @since 5.7.0 |
||
| 345 | * @deprecated 5.8.0 Use Jetpack_Search_Helpers::are_filters_by_widget_disabled() directly. |
||
| 346 | * |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | public function are_filters_by_widget_disabled() { |
||
| 350 | return Jetpack_Search_Helpers::are_filters_by_widget_disabled(); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Retrieves a list of known Jetpack search filters widget IDs, gets the filters for each widget, |
||
| 355 | * and applies those filters to this Jetpack_Search object. |
||
| 356 | * |
||
| 357 | * @since 5.7.0 |
||
| 358 | */ |
||
| 359 | public function set_filters_from_widgets() { |
||
| 360 | if ( Jetpack_Search_Helpers::are_filters_by_widget_disabled() ) { |
||
| 361 | return; |
||
| 362 | } |
||
| 363 | |||
| 364 | $filters = Jetpack_Search_Helpers::get_filters_from_widgets(); |
||
| 365 | |||
| 366 | if ( ! empty( $filters ) ) { |
||
| 367 | $this->set_filters( $filters ); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Restricts search results to certain post types via a GET argument. |
||
| 373 | * |
||
| 374 | * @since 5.8.0 |
||
| 375 | * |
||
| 376 | * @param WP_Query $query A WP_Query instance. |
||
| 377 | */ |
||
| 378 | public function maybe_add_post_type_as_var( WP_Query $query ) { |
||
| 379 | $post_type = ( ! empty( $_GET['post_type'] ) ) ? $_GET['post_type'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
| 380 | if ( $this->should_handle_query( $query ) && $post_type ) { |
||
| 381 | $post_types = ( is_string( $post_type ) && false !== strpos( $post_type, ',' ) ) |
||
| 382 | ? explode( ',', $post_type ) |
||
| 383 | : (array) $post_type; |
||
| 384 | $post_types = array_map( 'sanitize_key', $post_types ); |
||
| 385 | $query->set( 'post_type', $post_types ); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Run a search on the WordPress.com public API. |
||
| 391 | * |
||
| 392 | * @since 5.0.0 |
||
| 393 | * |
||
| 394 | * @param array $es_args Args conforming to the WP.com /sites/<blog_id>/search endpoint. |
||
| 395 | * |
||
| 396 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
| 397 | */ |
||
| 398 | public function search( array $es_args ) { |
||
| 399 | $endpoint = sprintf( '/sites/%s/search', $this->jetpack_blog_id ); |
||
| 400 | $service_url = 'https://public-api.wordpress.com/rest/v1' . $endpoint; |
||
| 401 | |||
| 402 | $do_authenticated_request = false; |
||
| 403 | |||
| 404 | if ( class_exists( 'Automattic\\Jetpack\\Connection\\Client' ) && |
||
| 405 | isset( $es_args['authenticated_request'] ) && |
||
| 406 | true === $es_args['authenticated_request'] ) { |
||
| 407 | $do_authenticated_request = true; |
||
| 408 | } |
||
| 409 | |||
| 410 | unset( $es_args['authenticated_request'] ); |
||
| 411 | |||
| 412 | $request_args = array( |
||
| 413 | 'headers' => array( |
||
| 414 | 'Content-Type' => 'application/json', |
||
| 415 | ), |
||
| 416 | 'timeout' => 10, |
||
| 417 | 'user-agent' => 'jetpack_search', |
||
| 418 | ); |
||
| 419 | |||
| 420 | $request_body = wp_json_encode( $es_args ); |
||
| 421 | |||
| 422 | $start_time = microtime( true ); |
||
| 423 | |||
| 424 | if ( $do_authenticated_request ) { |
||
| 425 | $request_args['method'] = 'POST'; |
||
| 426 | |||
| 427 | $request = Client::wpcom_json_api_request_as_blog( $endpoint, Client::WPCOM_JSON_API_VERSION, $request_args, $request_body ); |
||
| 428 | } else { |
||
| 429 | $request_args = array_merge( |
||
| 430 | $request_args, |
||
| 431 | array( |
||
| 432 | 'body' => $request_body, |
||
| 433 | ) |
||
| 434 | ); |
||
| 435 | |||
| 436 | $request = wp_remote_post( $service_url, $request_args ); |
||
| 437 | } |
||
| 438 | |||
| 439 | $end_time = microtime( true ); |
||
| 440 | |||
| 441 | if ( is_wp_error( $request ) ) { |
||
| 442 | return $request; |
||
| 443 | } |
||
| 444 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
| 445 | |||
| 446 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
| 447 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
| 448 | } |
||
| 449 | |||
| 450 | $response = json_decode( wp_remote_retrieve_body( $request ), true ); |
||
| 451 | |||
| 452 | $took = is_array( $response ) && ! empty( $response['took'] ) |
||
| 453 | ? $response['took'] |
||
| 454 | : null; |
||
| 455 | |||
| 456 | $query = array( |
||
| 457 | 'args' => $es_args, |
||
| 458 | 'response' => $response, |
||
| 459 | 'response_code' => $response_code, |
||
| 460 | 'elapsed_time' => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms. |
||
| 461 | 'es_time' => $took, |
||
| 462 | 'url' => $service_url, |
||
| 463 | ); |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Fires after a search request has been performed. |
||
| 467 | * |
||
| 468 | * Includes the following info in the $query parameter: |
||
| 469 | * |
||
| 470 | * array args Array of Elasticsearch arguments for the search |
||
| 471 | * array response Raw API response, JSON decoded |
||
| 472 | * int response_code HTTP response code of the request |
||
| 473 | * float elapsed_time Roundtrip time of the search request, in milliseconds |
||
| 474 | * float es_time Amount of time Elasticsearch spent running the request, in milliseconds |
||
| 475 | * string url API url that was queried |
||
| 476 | * |
||
| 477 | * @module search |
||
| 478 | * |
||
| 479 | * @since 5.0.0 |
||
| 480 | * @since 5.8.0 This action now fires on all queries instead of just successful queries. |
||
| 481 | * |
||
| 482 | * @param array $query Array of information about the query performed |
||
| 483 | */ |
||
| 484 | do_action( 'did_jetpack_search_query', $query ); |
||
| 485 | |||
| 486 | View Code Duplication | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
|
| 487 | /** |
||
| 488 | * Fires after a search query request has failed |
||
| 489 | * |
||
| 490 | * @module search |
||
| 491 | * |
||
| 492 | * @since 5.6.0 |
||
| 493 | * |
||
| 494 | * @param array Array containing the response code and response from the failed search query |
||
| 495 | */ |
||
| 496 | do_action( |
||
| 497 | 'failed_jetpack_search_query', |
||
| 498 | array( |
||
| 499 | 'response_code' => $response_code, |
||
| 500 | 'json' => $response, |
||
| 501 | ) |
||
| 502 | ); |
||
| 503 | |||
| 504 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
| 505 | } |
||
| 506 | |||
| 507 | return $response; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Bypass the normal Search query and offload it to Jetpack servers. |
||
| 512 | * |
||
| 513 | * This is the main hook of the plugin and is responsible for returning the posts that match the search query. |
||
| 514 | * |
||
| 515 | * @since 5.0.0 |
||
| 516 | * |
||
| 517 | * @param array $posts Current array of posts (still pre-query). |
||
| 518 | * @param WP_Query $query The WP_Query being filtered. |
||
| 519 | * |
||
| 520 | * @return array Array of matching posts. |
||
| 521 | */ |
||
| 522 | public function filter__posts_pre_query( $posts, $query ) { |
||
| 523 | if ( ! $this->should_handle_query( $query ) ) { |
||
| 524 | // Intentionally not adding the 'jetpack_search_abort' action since this should fire for every request except for search. |
||
| 525 | return $posts; |
||
| 526 | } |
||
| 527 | |||
| 528 | $this->do_search( $query ); |
||
| 529 | |||
| 530 | if ( ! is_array( $this->search_result ) ) { |
||
| 531 | /** This action is documented in modules/search/class.jetpack-search.php */ |
||
| 532 | do_action( 'jetpack_search_abort', 'no_search_results_array', $this->search_result ); |
||
| 533 | return $posts; |
||
| 534 | } |
||
| 535 | |||
| 536 | // If no results, nothing to do. |
||
| 537 | if ( ! count( $this->search_result['results']['hits'] ) ) { |
||
| 538 | return array(); |
||
| 539 | } |
||
| 540 | |||
| 541 | $post_ids = array(); |
||
| 542 | |||
| 543 | foreach ( $this->search_result['results']['hits'] as $result ) { |
||
| 544 | $post_ids[] = (int) $result['fields']['post_id']; |
||
| 545 | } |
||
| 546 | |||
| 547 | // Query all posts now. |
||
| 548 | $args = array( |
||
| 549 | 'post__in' => $post_ids, |
||
| 550 | 'orderby' => 'post__in', |
||
| 551 | 'perm' => 'readable', |
||
| 552 | 'post_type' => 'any', |
||
| 553 | 'ignore_sticky_posts' => true, |
||
| 554 | 'suppress_filters' => true, |
||
| 555 | ); |
||
| 556 | |||
| 557 | $posts_query = new WP_Query( $args ); |
||
| 558 | |||
| 559 | // WP Core doesn't call the set_found_posts and its filters when filtering posts_pre_query like we do, so need to do these manually. |
||
| 560 | $query->found_posts = $this->found_posts; |
||
| 561 | $query->max_num_pages = ceil( $this->found_posts / $query->get( 'posts_per_page' ) ); |
||
| 562 | |||
| 563 | return $posts_query->posts; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Build up the search, then run it against the Jetpack servers. |
||
| 568 | * |
||
| 569 | * @since 5.0.0 |
||
| 570 | * |
||
| 571 | * @param WP_Query $query The original WP_Query to use for the parameters of our search. |
||
| 572 | */ |
||
| 573 | public function do_search( WP_Query $query ) { |
||
| 574 | if ( ! $this->should_handle_query( $query ) ) { |
||
| 575 | // If we make it here, either 'filter__posts_pre_query' somehow allowed it or a different entry to do_search. |
||
| 576 | /** This action is documented in modules/search/class.jetpack-search.php */ |
||
| 577 | do_action( 'jetpack_search_abort', 'search_attempted_non_search_query', $query ); |
||
| 578 | return; |
||
| 579 | } |
||
| 580 | |||
| 581 | $page = ( $query->get( 'paged' ) ) ? absint( $query->get( 'paged' ) ) : 1; |
||
| 582 | |||
| 583 | // Get maximum allowed offset and posts per page values for the API. |
||
| 584 | $max_offset = Jetpack_Search_Helpers::get_max_offset(); |
||
| 585 | $max_posts_per_page = Jetpack_Search_Helpers::get_max_posts_per_page(); |
||
| 586 | |||
| 587 | $posts_per_page = $query->get( 'posts_per_page' ); |
||
| 588 | if ( $posts_per_page > $max_posts_per_page ) { |
||
| 589 | $posts_per_page = $max_posts_per_page; |
||
| 590 | } |
||
| 591 | |||
| 592 | // Start building the WP-style search query args. |
||
| 593 | // They'll be translated to ES format args later. |
||
| 594 | $es_wp_query_args = array( |
||
| 595 | 'query' => $query->get( 's' ), |
||
| 596 | 'posts_per_page' => $posts_per_page, |
||
| 597 | 'paged' => $page, |
||
| 598 | 'orderby' => $query->get( 'orderby' ), |
||
| 599 | 'order' => $query->get( 'order' ), |
||
| 600 | ); |
||
| 601 | |||
| 602 | if ( ! empty( $this->aggregations ) ) { |
||
| 603 | $es_wp_query_args['aggregations'] = $this->aggregations; |
||
| 604 | } |
||
| 605 | |||
| 606 | // Did we query for authors? |
||
| 607 | if ( $query->get( 'author_name' ) ) { |
||
| 608 | $es_wp_query_args['author_name'] = $query->get( 'author_name' ); |
||
| 609 | } |
||
| 610 | |||
| 611 | $es_wp_query_args['post_type'] = $this->get_es_wp_query_post_type_for_query( $query ); |
||
| 612 | $es_wp_query_args['terms'] = $this->get_es_wp_query_terms_for_query( $query ); |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Modify the search query parameters, such as controlling the post_type. |
||
| 616 | * |
||
| 617 | * These arguments are in the format of WP_Query arguments |
||
| 618 | * |
||
| 619 | * @module search |
||
| 620 | * |
||
| 621 | * @since 5.0.0 |
||
| 622 | * |
||
| 623 | * @param array $es_wp_query_args The current query args, in WP_Query format. |
||
| 624 | * @param WP_Query $query The original WP_Query object. |
||
| 625 | */ |
||
| 626 | $es_wp_query_args = apply_filters( 'jetpack_search_es_wp_query_args', $es_wp_query_args, $query ); |
||
| 627 | |||
| 628 | // If page * posts_per_page is greater than our max offset, send a 404. This is necessary because the offset is |
||
| 629 | // capped at Jetpack_Search_Helpers::get_max_offset(), so a high page would always return the last page of results otherwise. |
||
| 630 | if ( ( $es_wp_query_args['paged'] * $es_wp_query_args['posts_per_page'] ) > $max_offset ) { |
||
| 631 | $query->set_404(); |
||
| 632 | |||
| 633 | return; |
||
| 634 | } |
||
| 635 | |||
| 636 | // If there were no post types returned, then 404 to avoid querying against non-public post types, which could |
||
| 637 | // happen if we don't add the post type restriction to the ES query. |
||
| 638 | if ( empty( $es_wp_query_args['post_type'] ) ) { |
||
| 639 | $query->set_404(); |
||
| 640 | |||
| 641 | return; |
||
| 642 | } |
||
| 643 | |||
| 644 | // Convert the WP-style args into ES args. |
||
| 645 | $es_query_args = $this->convert_wp_es_to_es_args( $es_wp_query_args ); |
||
| 646 | |||
| 647 | // Only trust ES to give us IDs, not the content since it is a mirror. |
||
| 648 | $es_query_args['fields'] = array( |
||
| 649 | 'post_id', |
||
| 650 | ); |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Modify the underlying ES query that is passed to the search endpoint. The returned args must represent a valid ES query |
||
| 654 | * |
||
| 655 | * This filter is harder to use if you're unfamiliar with ES, but allows complete control over the query |
||
| 656 | * |
||
| 657 | * @module search |
||
| 658 | * |
||
| 659 | * @since 5.0.0 |
||
| 660 | * |
||
| 661 | * @param array $es_query_args The raw Elasticsearch query args. |
||
| 662 | * @param WP_Query $query The original WP_Query object. |
||
| 663 | */ |
||
| 664 | $es_query_args = apply_filters( 'jetpack_search_es_query_args', $es_query_args, $query ); |
||
| 665 | |||
| 666 | // Do the actual search query! |
||
| 667 | $this->search_result = $this->search( $es_query_args ); |
||
| 668 | |||
| 669 | if ( is_wp_error( $this->search_result ) || ! is_array( $this->search_result ) || empty( $this->search_result['results'] ) || empty( $this->search_result['results']['hits'] ) ) { |
||
| 670 | $this->found_posts = 0; |
||
| 671 | |||
| 672 | return; |
||
| 673 | } |
||
| 674 | |||
| 675 | // If we have aggregations, fix the ordering to match the input order (ES doesn't guarantee the return order). |
||
| 676 | if ( isset( $this->search_result['results']['aggregations'] ) && ! empty( $this->search_result['results']['aggregations'] ) ) { |
||
| 677 | $this->search_result['results']['aggregations'] = $this->fix_aggregation_ordering( $this->search_result['results']['aggregations'], $this->aggregations ); |
||
| 678 | } |
||
| 679 | |||
| 680 | // Total number of results for paging purposes. Capped at $max_offset + $posts_per_page, as deep paging gets quite expensive. |
||
| 681 | $this->found_posts = min( $this->search_result['results']['total'], $max_offset + $posts_per_page ); |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * If the query has already been run before filters have been updated, then we need to re-run the query |
||
| 686 | * to get the latest aggregations. |
||
| 687 | * |
||
| 688 | * This is especially useful for supporting widget management in the customizer. |
||
| 689 | * |
||
| 690 | * @since 5.8.0 |
||
| 691 | * |
||
| 692 | * @return bool Whether the query was successful or not. |
||
| 693 | */ |
||
| 694 | public function update_search_results_aggregations() { |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Given a WP_Query, convert its WP_Tax_Query (if present) into the WP-style Elasticsearch term arguments for the search. |
||
| 711 | * |
||
| 712 | * @since 5.0.0 |
||
| 713 | * |
||
| 714 | * @param WP_Query $query The original WP_Query object for which to parse the taxonomy query. |
||
| 715 | * |
||
| 716 | * @return array The new WP-style Elasticsearch arguments (that will be converted into 'real' Elasticsearch arguments). |
||
| 717 | */ |
||
| 718 | public function get_es_wp_query_terms_for_query( WP_Query $query ) { |
||
| 719 | $args = array(); |
||
| 720 | |||
| 721 | $the_tax_query = $query->tax_query; |
||
| 722 | |||
| 723 | if ( ! $the_tax_query ) { |
||
| 724 | return $args; |
||
| 725 | } |
||
| 726 | |||
| 727 | if ( ! $the_tax_query instanceof WP_Tax_Query || empty( $the_tax_query->queried_terms ) || ! is_array( $the_tax_query->queried_terms ) ) { |
||
| 728 | return $args; |
||
| 729 | } |
||
| 730 | |||
| 731 | $args = array(); |
||
| 732 | |||
| 733 | foreach ( $the_tax_query->queries as $tax_query ) { |
||
| 734 | // Right now we only support slugs...see note above. |
||
| 735 | if ( ! is_array( $tax_query ) || 'slug' !== $tax_query['field'] ) { |
||
| 736 | continue; |
||
| 737 | } |
||
| 738 | |||
| 739 | $taxonomy = $tax_query['taxonomy']; |
||
| 740 | |||
| 741 | View Code Duplication | if ( ! isset( $args[ $taxonomy ] ) || ! is_array( $args[ $taxonomy ] ) ) { |
|
| 742 | $args[ $taxonomy ] = array(); |
||
| 743 | } |
||
| 744 | |||
| 745 | $args[ $taxonomy ] = array_merge( $args[ $taxonomy ], $tax_query['terms'] ); |
||
| 746 | } |
||
| 747 | |||
| 748 | return $args; |
||
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Parse out the post type from a WP_Query. |
||
| 753 | * |
||
| 754 | * Only allows post types that are not marked as 'exclude_from_search'. |
||
| 755 | * |
||
| 756 | * @since 5.0.0 |
||
| 757 | * |
||
| 758 | * @param WP_Query $query Original WP_Query object. |
||
| 759 | * |
||
| 760 | * @return array Array of searchable post types corresponding to the original query. |
||
| 761 | */ |
||
| 762 | public function get_es_wp_query_post_type_for_query( WP_Query $query ) { |
||
| 763 | $post_types = $query->get( 'post_type' ); |
||
| 764 | |||
| 765 | // If we're searching 'any', we want to only pass searchable post types to Elasticsearch. |
||
| 766 | if ( 'any' === $post_types ) { |
||
| 767 | $post_types = array_values( |
||
| 768 | get_post_types( |
||
| 769 | array( |
||
| 770 | 'exclude_from_search' => false, |
||
| 771 | ) |
||
| 772 | ) |
||
| 773 | ); |
||
| 774 | } |
||
| 775 | |||
| 776 | if ( ! is_array( $post_types ) ) { |
||
| 777 | $post_types = array( $post_types ); |
||
| 778 | } |
||
| 779 | |||
| 780 | $post_types = array_unique( $post_types ); |
||
| 781 | |||
| 782 | $sanitized_post_types = array(); |
||
| 783 | |||
| 784 | // Make sure the post types are queryable. |
||
| 785 | foreach ( $post_types as $post_type ) { |
||
| 786 | if ( ! $post_type ) { |
||
| 787 | continue; |
||
| 788 | } |
||
| 789 | |||
| 790 | $post_type_object = get_post_type_object( $post_type ); |
||
| 791 | if ( ! $post_type_object || $post_type_object->exclude_from_search ) { |
||
| 792 | continue; |
||
| 793 | } |
||
| 794 | |||
| 795 | $sanitized_post_types[] = $post_type; |
||
| 796 | } |
||
| 797 | |||
| 798 | return $sanitized_post_types; |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Initialize widgets for the Search module (on wp.com only). |
||
| 803 | * |
||
| 804 | * @module search |
||
| 805 | */ |
||
| 806 | public function action__widgets_init() { |
||
| 807 | require_once dirname( __FILE__ ) . '/class.jetpack-search-widget-filters.php'; |
||
| 808 | |||
| 809 | register_widget( 'Jetpack_Search_Widget_Filters' ); |
||
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Get the Elasticsearch result. |
||
| 814 | * |
||
| 815 | * @since 5.0.0 |
||
| 816 | * |
||
| 817 | * @param bool $raw If true, does not check for WP_Error or return the 'results' array - the JSON decoded HTTP response. |
||
| 818 | * |
||
| 819 | * @return array|bool The search results, or false if there was a failure. |
||
| 820 | */ |
||
| 821 | public function get_search_result( $raw = false ) { |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Add the date portion of a WP_Query onto the query args. |
||
| 831 | * |
||
| 832 | * @since 5.0.0 |
||
| 833 | * |
||
| 834 | * @param array $es_wp_query_args The Elasticsearch query arguments in WordPress form. |
||
| 835 | * @param WP_Query $query The original WP_Query. |
||
| 836 | * |
||
| 837 | * @return array The es wp query args, with date filters added (as needed). |
||
| 838 | */ |
||
| 839 | public function filter__add_date_filter_to_query( array $es_wp_query_args, WP_Query $query ) { |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Converts WP_Query style args to Elasticsearch args. |
||
| 874 | * |
||
| 875 | * @since 5.0.0 |
||
| 876 | * |
||
| 877 | * @param array $args Array of WP_Query style arguments. |
||
| 878 | * |
||
| 879 | * @return array Array of ES style query arguments. |
||
| 880 | */ |
||
| 881 | public function convert_wp_es_to_es_args( array $args ) { |
||
| 882 | jetpack_require_lib( 'jetpack-wpes-query-builder/jetpack-wpes-query-parser' ); |
||
| 883 | |||
| 884 | $defaults = array( |
||
| 885 | 'blog_id' => get_current_blog_id(), |
||
| 886 | 'query' => null, // Search phrase. |
||
| 887 | 'query_fields' => array(), // list of fields to search. |
||
| 888 | 'excess_boost' => array(), // map of field to excess boost values (multiply). |
||
| 889 | 'post_type' => null, // string or an array. |
||
| 890 | 'terms' => array(), // ex: array( 'taxonomy-1' => array( 'slug' ), 'taxonomy-2' => array( 'slug-a', 'slug-b' ) ). phpcs:ignore Squiz.PHP.CommentedOutCode.Found. |
||
| 891 | 'author' => null, // id or an array of ids. |
||
| 892 | 'author_name' => array(), // string or an array. |
||
| 893 | 'date_range' => null, // array( 'field' => 'date', 'gt' => 'YYYY-MM-dd', 'lte' => 'YYYY-MM-dd' ); date formats: 'YYYY-MM-dd' or 'YYYY-MM-dd HH:MM:SS'. phpcs:ignore Squiz.PHP.CommentedOutCode.Found. |
||
| 894 | 'orderby' => null, // Defaults to 'relevance' if query is set, otherwise 'date'. Pass an array for multiple orders. |
||
| 895 | 'order' => 'DESC', |
||
| 896 | 'posts_per_page' => 10, |
||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Given an array of aggregations, parse and add them onto the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1276 | * |
||
| 1277 | * @since 5.0.0 |
||
| 1278 | * |
||
| 1279 | * @param array $aggregations Array of aggregations (filters) to add to the Jetpack_WPES_Query_Builder. |
||
| 1280 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1281 | */ |
||
| 1282 | public function add_aggregations_to_es_query_builder( array $aggregations, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Given an individual taxonomy aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1305 | * |
||
| 1306 | * @since 5.0.0 |
||
| 1307 | * |
||
| 1308 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1309 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1310 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1311 | */ |
||
| 1312 | public function add_taxonomy_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Given an individual post_type aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1342 | * |
||
| 1343 | * @since 5.0.0 |
||
| 1344 | * |
||
| 1345 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1346 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1347 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1348 | */ |
||
| 1349 | public function add_post_type_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Given an individual date_histogram aggregation, add it to the Jetpack_WPES_Query_Builder object for use in Elasticsearch. |
||
| 1363 | * |
||
| 1364 | * @since 5.0.0 |
||
| 1365 | * |
||
| 1366 | * @param array $aggregation The aggregation to add to the query builder. |
||
| 1367 | * @param string $label The 'label' (unique id) for this aggregation. |
||
| 1368 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the Elasticsearch query. |
||
| 1369 | */ |
||
| 1370 | public function add_date_histogram_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
| 1389 | |||
| 1390 | /** |
||
| 1391 | * And an existing filter object with a list of additional filters. |
||
| 1392 | * |
||
| 1393 | * Attempts to optimize the filters somewhat. |
||
| 1394 | * |
||
| 1395 | * @since 5.0.0 |
||
| 1396 | * |
||
| 1397 | * @param array $curr_filter The existing filters to build upon. |
||
| 1398 | * @param array $filters The new filters to add. |
||
| 1399 | * |
||
| 1400 | * @return array The resulting merged filters. |
||
| 1401 | */ |
||
| 1402 | public static function and_es_filters( array $curr_filter, array $filters ) { |
||
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Set the available filters for the search. |
||
| 1420 | * |
||
| 1421 | * These get rendered via the Jetpack_Search_Widget() widget. |
||
| 1422 | * |
||
| 1423 | * Behind the scenes, these are implemented using Elasticsearch Aggregations. |
||
| 1424 | * |
||
| 1425 | * If you do not require counts of how many documents match each filter, please consider using regular WP Query |
||
| 1426 | * arguments instead, such as via the jetpack_search_es_wp_query_args filter |
||
| 1427 | * |
||
| 1428 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1429 | * |
||
| 1430 | * @since 5.0.0 |
||
| 1431 | * |
||
| 1432 | * @param array $aggregations Array of filters (aggregations) to apply to the search. |
||
| 1433 | */ |
||
| 1434 | public function set_filters( array $aggregations ) { |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * Set the search's facets (deprecated). |
||
| 1445 | * |
||
| 1446 | * @deprecated 5.0 Please use Jetpack_Search::set_filters() instead. |
||
| 1447 | * |
||
| 1448 | * @see Jetpack_Search::set_filters() |
||
| 1449 | * |
||
| 1450 | * @param array $facets Array of facets to apply to the search. |
||
| 1451 | */ |
||
| 1452 | public function set_facets( array $facets ) { |
||
| 1457 | |||
| 1458 | /** |
||
| 1459 | * Get the raw Aggregation results from the Elasticsearch response. |
||
| 1460 | * |
||
| 1461 | * @since 5.0.0 |
||
| 1462 | * |
||
| 1463 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
| 1464 | * |
||
| 1465 | * @return array Array of Aggregations performed on the search. |
||
| 1466 | */ |
||
| 1467 | public function get_search_aggregations_results() { |
||
| 1478 | |||
| 1479 | /** |
||
| 1480 | * Get the raw Facet results from the Elasticsearch response. |
||
| 1481 | * |
||
| 1482 | * @deprecated 5.0 Please use Jetpack_Search::get_search_aggregations_results() instead. |
||
| 1483 | * |
||
| 1484 | * @see Jetpack_Search::get_search_aggregations_results() |
||
| 1485 | * |
||
| 1486 | * @return array Array of Facets performed on the search. |
||
| 1487 | */ |
||
| 1488 | public function get_search_facets() { |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Get the results of the Filters performed, including the number of matching documents. |
||
| 1496 | * |
||
| 1497 | * Returns an array of Filters (keyed by $label, as passed to Jetpack_Search::set_filters()), containing the Filter and all resulting |
||
| 1498 | * matching buckets, the url for applying/removing each bucket, etc. |
||
| 1499 | * |
||
| 1500 | * NOTE - if this is called before the search is performed, an empty array will be returned. Use the $aggregations class |
||
| 1501 | * member if you need to access the raw filters set in Jetpack_Search::set_filters(). |
||
| 1502 | * |
||
| 1503 | * @since 5.0.0 |
||
| 1504 | * |
||
| 1505 | * @param WP_Query $query The optional original WP_Query to use for determining which filters are active. Defaults to the main query. |
||
| 1506 | * |
||
| 1507 | * @return array Array of filters applied and info about them. |
||
| 1508 | */ |
||
| 1509 | public function get_filters( WP_Query $query = null ) { |
||
| 1770 | |||
| 1771 | /** |
||
| 1772 | * Get the results of the facets performed. |
||
| 1773 | * |
||
| 1774 | * @deprecated 5.0 Please use Jetpack_Search::get_filters() instead. |
||
| 1775 | * |
||
| 1776 | * @see Jetpack_Search::get_filters() |
||
| 1777 | * |
||
| 1778 | * @return array $facets Array of facets applied and info about them. |
||
| 1779 | */ |
||
| 1780 | public function get_search_facet_data() { |
||
| 1785 | |||
| 1786 | /** |
||
| 1787 | * Get the filters that are currently applied to this search. |
||
| 1788 | * |
||
| 1789 | * @since 5.0.0 |
||
| 1790 | * |
||
| 1791 | * @return array Array of filters that were applied. |
||
| 1792 | */ |
||
| 1793 | public function get_active_filter_buckets() { |
||
| 1814 | |||
| 1815 | /** |
||
| 1816 | * Get the filters that are currently applied to this search. |
||
| 1817 | * |
||
| 1818 | * @deprecated 5.0 Please use Jetpack_Search::get_active_filter_buckets() instead. |
||
| 1819 | * |
||
| 1820 | * @see Jetpack_Search::get_active_filter_buckets() |
||
| 1821 | * |
||
| 1822 | * @return array Array of filters that were applied. |
||
| 1823 | */ |
||
| 1824 | public function get_current_filters() { |
||
| 1829 | |||
| 1830 | /** |
||
| 1831 | * Calculate the right query var to use for a given taxonomy. |
||
| 1832 | * |
||
| 1833 | * Allows custom code to modify the GET var that is used to represent a given taxonomy, via the jetpack_search_taxonomy_query_var filter. |
||
| 1834 | * |
||
| 1835 | * @since 5.0.0 |
||
| 1836 | * |
||
| 1837 | * @param string $taxonomy_name The name of the taxonomy for which to get the query var. |
||
| 1838 | * |
||
| 1839 | * @return bool|string The query var to use for this taxonomy, or false if none found. |
||
| 1840 | */ |
||
| 1841 | public function get_taxonomy_query_var( $taxonomy_name ) { |
||
| 1860 | |||
| 1861 | /** |
||
| 1862 | * Takes an array of aggregation results, and ensures the array key ordering matches the key order in $desired |
||
| 1863 | * which is the input order. |
||
| 1864 | * |
||
| 1865 | * Necessary because ES does not always return aggregations in the same order that you pass them in, |
||
| 1866 | * and it should be possible to control the display order easily. |
||
| 1867 | * |
||
| 1868 | * @since 5.0.0 |
||
| 1869 | * |
||
| 1870 | * @param array $aggregations Aggregation results to be reordered. |
||
| 1871 | * @param array $desired Array with keys representing the desired ordering. |
||
| 1872 | * |
||
| 1873 | * @return array A new array with reordered keys, matching those in $desired. |
||
| 1874 | */ |
||
| 1875 | public function fix_aggregation_ordering( array $aggregations, array $desired ) { |
||
| 1890 | |||
| 1891 | /** |
||
| 1892 | * Sends events to Tracks when a search filters widget is updated. |
||
| 1893 | * |
||
| 1894 | * @since 5.8.0 |
||
| 1895 | * |
||
| 1896 | * @param string $option The option name. Only "widget_jetpack-search-filters" is cared about. |
||
| 1897 | * @param array $old_value The old option value. |
||
| 1898 | * @param array $new_value The new option value. |
||
| 1899 | */ |
||
| 1900 | public function track_widget_updates( $option, $old_value, $new_value ) { |
||
| 1917 | |||
| 1918 | /** |
||
| 1919 | * Moves any active search widgets to the inactive category. |
||
| 1920 | * |
||
| 1921 | * @since 5.9.0 |
||
| 1922 | * |
||
| 1923 | * @param string $module Unused. The Jetpack module being disabled. |
||
| 1924 | */ |
||
| 1925 | public function move_search_widgets_to_inactive( $module ) { |
||
| 1959 | |||
| 1960 | /** |
||
| 1961 | * Determine whether a given WP_Query should be handled by ElasticSearch. |
||
| 1962 | * |
||
| 1963 | * @param WP_Query $query The WP_Query object. |
||
| 1964 | * |
||
| 1965 | * @return bool |
||
| 1966 | */ |
||
| 1967 | public function should_handle_query( $query ) { |
||
| 1980 | |||
| 1981 | /** |
||
| 1982 | * Transforms an array with fields name as keys and boosts as value into |
||
| 1983 | * shorthand "caret" format. |
||
| 1984 | * |
||
| 1985 | * @param array $fields_boost [ "title" => "2", "content" => "1" ]. |
||
| 1986 | * |
||
| 1987 | * @return array [ "title^2", "content^1" ] |
||
| 1988 | */ |
||
| 1989 | private function _get_caret_boosted_fields( array $fields_boost ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
||
| 1996 | |||
| 1997 | /** |
||
| 1998 | * Apply a multiplier to boost values. |
||
| 1999 | * |
||
| 2000 | * @param array $fields_boost [ "title" => 2, "content" => 1 ]. |
||
| 2001 | * @param array $fields_boost_multiplier [ "title" => 0.1234 ]. |
||
| 2002 | * |
||
| 2003 | * @return array [ "title" => "0.247", "content" => "1.000" ] |
||
| 2004 | */ |
||
| 2005 | private function _apply_boosts_multiplier( array $fields_boost, array $fields_boost_multiplier ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
||
| 2022 | } |
||
| 2023 |
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.