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 |
||
3 | class Jetpack_Search { |
||
4 | |||
5 | protected $found_posts = 0; |
||
6 | |||
7 | /** |
||
8 | * The maximum offset ('from' param), since deep pages get exponentially slower. |
||
9 | * |
||
10 | * @see https://www.elastic.co/guide/en/elasticsearch/guide/current/pagination.html |
||
11 | */ |
||
12 | protected $max_offset = 200; |
||
13 | |||
14 | protected $search_result; |
||
15 | |||
16 | protected $original_blog_id; |
||
17 | protected $jetpack_blog_id; |
||
18 | |||
19 | protected $aggregations = array(); |
||
20 | protected $max_aggregations_count = 100; |
||
21 | |||
22 | // used to output query meta into page |
||
23 | protected $last_query_info; |
||
24 | protected $last_query_failure_info; |
||
25 | |||
26 | protected static $instance; |
||
27 | |||
28 | //Languages with custom analyzers, other languages are supported, |
||
29 | // but are analyzed with the default analyzer. |
||
30 | 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' ); |
||
31 | |||
32 | protected function __construct() { |
||
35 | |||
36 | public function __clone() { |
||
39 | |||
40 | public function __wakeup() { |
||
43 | |||
44 | /** |
||
45 | * Get singleton instance of Jetpack_Search |
||
46 | * |
||
47 | * Instantiates and sets up a new instance if needed, or returns the singleton |
||
48 | * |
||
49 | * @module search |
||
50 | * |
||
51 | * @return Jetpack_Search The Jetpack_Search singleton |
||
52 | */ |
||
53 | public static function instance() { |
||
62 | |||
63 | /** |
||
64 | * Perform various setup tasks for the class |
||
65 | * |
||
66 | * Checks various pre-requisites and adds hooks |
||
67 | * |
||
68 | * @module search |
||
69 | */ |
||
70 | public function setup() { |
||
71 | if ( ! Jetpack::is_active() || ! Jetpack::active_plan_supports( 'search' ) ) { |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | $this->jetpack_blog_id = Jetpack::get_option( 'id' ); |
||
76 | |||
77 | if ( ! $this->jetpack_blog_id ) { |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | require_once( dirname( __FILE__ ) . '/class.jetpack-search-helpers.php' ); |
||
82 | |||
83 | $this->init_hooks(); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Setup the various hooks needed for the plugin to take over Search duties |
||
88 | * |
||
89 | * @module search |
||
90 | */ |
||
91 | public function init_hooks() { |
||
92 | add_action( 'widgets_init', array( $this, 'action__widgets_init' ) ); |
||
93 | |||
94 | if ( ! is_admin() ) { |
||
95 | add_filter( 'posts_pre_query', array( $this, 'filter__posts_pre_query' ), 10, 2 ); |
||
96 | |||
97 | add_filter( 'jetpack_search_es_wp_query_args', array( $this, 'filter__add_date_filter_to_query' ), 10, 2 ); |
||
98 | |||
99 | add_action( 'did_jetpack_search_query', array( $this, 'store_query_success' ) ); |
||
100 | add_action( 'failed_jetpack_search_query', array( $this, 'store_query_failure' ) ); |
||
101 | |||
102 | add_action( 'init', array( $this, 'set_filters_from_widgets' ) ); |
||
103 | |||
104 | add_action( 'pre_get_posts', array( $this, 'maybe_add_post_type_as_var' ) ); |
||
105 | } else { |
||
106 | add_action( 'update_option', array( $this, 'track_widget_updates' ), 10, 3 ); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Print query info as a HTML comment in the footer |
||
112 | */ |
||
113 | |||
114 | public function store_query_failure( $meta ) { |
||
115 | $this->last_query_failure_info = $meta; |
||
116 | add_action( 'wp_footer', array( $this, 'print_query_failure' ) ); |
||
117 | } |
||
118 | |||
119 | public function print_query_failure() { |
||
120 | if ( $this->last_query_failure_info ) { |
||
121 | printf( |
||
122 | '<!-- Jetpack Search failed with code %s: %s - %s -->', |
||
123 | esc_html( $this->last_query_failure_info['response_code'] ), |
||
124 | esc_html( $this->last_query_failure_info['json']['error'] ), |
||
125 | esc_html( $this->last_query_failure_info['json']['message'] ) |
||
126 | ); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public function store_query_success( $meta ) { |
||
131 | $this->last_query_info = $meta; |
||
132 | add_action( 'wp_footer', array( $this, 'print_query_success' ) ); |
||
133 | } |
||
134 | |||
135 | public function print_query_success() { |
||
136 | if ( $this->last_query_info ) { |
||
137 | printf( |
||
138 | '<!-- Jetpack Search took %s ms, ES time %s ms -->', |
||
139 | intval( $this->last_query_info['elapsed_time'] ), |
||
140 | esc_html( $this->last_query_info['es_time'] ) |
||
141 | ); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | function are_filters_by_widget_disabled() { |
||
146 | /** |
||
147 | * Allows developers to disable filters being set by widget, in favor of manually |
||
148 | * setting filters via `Jetpack_Search::set_filters()`. |
||
149 | * |
||
150 | * @module search |
||
151 | * |
||
152 | * @since 5.7.0 |
||
153 | * |
||
154 | * @param bool false |
||
155 | */ |
||
156 | return apply_filters( 'jetpack_search_disable_widget_filters', false ); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Retrieves a list of known Jetpack search filters widget IDs, gets the filters for each widget, |
||
161 | * and applies those filters to this Jetpack_Search object. |
||
162 | * |
||
163 | * @since 5.7.0 |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | function set_filters_from_widgets() { |
||
168 | if ( $this->are_filters_by_widget_disabled() ) { |
||
169 | return; |
||
170 | } |
||
171 | |||
172 | $filters = Jetpack_Search_Helpers::get_filters_from_widgets(); |
||
173 | |||
174 | if ( ! empty( $filters ) ) { |
||
175 | $this->set_filters( $filters ); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | function maybe_add_post_type_as_var( WP_Query $query ) { |
||
180 | if ( $query->is_main_query() && $query->is_search && ! empty( $_GET['post_type'] ) ) { |
||
181 | $post_types = ( is_string( $_GET['post_type'] ) && false !== strpos( $_GET['post_type'], ',' ) ) |
||
182 | ? $post_type = explode( ',', $_GET['post_type'] ) |
||
183 | : (array) $_GET['post_type']; |
||
184 | $post_types = array_map( 'sanitize_key', $post_types ); |
||
185 | $query->set('post_type', $post_types ); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /* |
||
190 | * Run a search on the WP.com public API. |
||
191 | * |
||
192 | * @module search |
||
193 | * |
||
194 | * @param array $es_args Args conforming to the WP.com /sites/<blog_id>/search endpoint |
||
195 | * |
||
196 | * @return object|WP_Error The response from the public api, or a WP_Error |
||
197 | */ |
||
198 | public function search( array $es_args ) { |
||
199 | $endpoint = sprintf( '/sites/%s/search', $this->jetpack_blog_id ); |
||
200 | $service_url = 'https://public-api.wordpress.com/rest/v1' . $endpoint; |
||
201 | |||
202 | $do_authenticated_request = false; |
||
203 | |||
204 | if ( class_exists( 'Jetpack_Client' ) && |
||
205 | isset( $es_args['authenticated_request'] ) && |
||
206 | true === $es_args['authenticated_request'] ) { |
||
207 | $do_authenticated_request = true; |
||
208 | } |
||
209 | |||
210 | unset( $es_args['authenticated_request'] ); |
||
211 | |||
212 | $request_args = array( |
||
213 | 'headers' => array( |
||
214 | 'Content-Type' => 'application/json', |
||
215 | ), |
||
216 | 'timeout' => 10, |
||
217 | 'user-agent' => 'jetpack_search', |
||
218 | ); |
||
219 | |||
220 | $request_body = json_encode( $es_args ); |
||
221 | |||
222 | $start_time = microtime( true ); |
||
223 | |||
224 | if ( $do_authenticated_request ) { |
||
225 | $request_args['method'] = 'POST'; |
||
226 | |||
227 | $request = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $request_args, $request_body ); |
||
228 | } else { |
||
229 | $request_args = array_merge( $request_args, array( |
||
230 | 'body' => $request_body, |
||
231 | ) ); |
||
232 | |||
233 | $request = wp_remote_post( $service_url, $request_args ); |
||
234 | } |
||
235 | |||
236 | $end_time = microtime( true ); |
||
237 | |||
238 | if ( is_wp_error( $request ) ) { |
||
239 | return $request; |
||
240 | } |
||
241 | |||
242 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
243 | $response = json_decode( wp_remote_retrieve_body( $request ), true ); |
||
244 | |||
245 | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
||
246 | /** |
||
247 | * Fires after a search query request has failed |
||
248 | * |
||
249 | * @module search |
||
250 | * |
||
251 | * @since 5.6.0 |
||
252 | * |
||
253 | * @param array Array containing the response code and response from the failed search query |
||
254 | */ |
||
255 | do_action( 'failed_jetpack_search_query', array( 'response_code' => $response_code, 'json' => $response ) ); |
||
256 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
257 | } |
||
258 | |||
259 | $took = is_array( $response ) && $response['took'] ? $response['took'] : null; |
||
260 | |||
261 | $query = array( |
||
262 | 'args' => $es_args, |
||
263 | 'response' => $response, |
||
264 | 'response_code' => $response_code, |
||
265 | 'elapsed_time' => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms |
||
266 | 'es_time' => $took, |
||
267 | 'url' => $service_url, |
||
268 | ); |
||
269 | |||
270 | /** |
||
271 | * Fires after a search request has been performed |
||
272 | * |
||
273 | * Includes the following info in the $query parameter: |
||
274 | * |
||
275 | * array args Array of Elasticsearch arguments for the search |
||
276 | * array response Raw API response, JSON decoded |
||
277 | * int response_code HTTP response code of the request |
||
278 | * float elapsed_time Roundtrip time of the search request, in milliseconds |
||
279 | * float es_time Amount of time Elasticsearch spent running the request, in milliseconds |
||
280 | * string url API url that was queried |
||
281 | * |
||
282 | * @module search |
||
283 | * |
||
284 | * @since 5.0.0 |
||
285 | * |
||
286 | * @param array $query Array of information about the query performed |
||
287 | */ |
||
288 | do_action( 'did_jetpack_search_query', $query ); |
||
289 | |||
290 | return $response; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Bypass the normal Search query and offload it to Jetpack servers |
||
295 | * |
||
296 | * This is the main hook of the plugin and is responsible for returning the posts that match the search query |
||
297 | * |
||
298 | * @module search |
||
299 | * |
||
300 | * @param array $posts Current array of posts (still pre-query) |
||
301 | * @param WP_Query $query The WP_Query being filtered |
||
302 | * |
||
303 | * @return array Array of matching posts |
||
304 | */ |
||
305 | public function filter__posts_pre_query( $posts, $query ) { |
||
306 | /** |
||
307 | * Determine whether a given WP_Query should be handled by ElasticSearch |
||
308 | * |
||
309 | * @module search |
||
310 | * |
||
311 | * @since 5.6.0 |
||
312 | * @param bool $should_handle Should be handled by Jetpack Search |
||
313 | * @param WP_Query $query The wp_query object |
||
314 | */ |
||
315 | if ( ! apply_filters( 'jetpack_search_should_handle_query', ( $query->is_main_query() && $query->is_search() ), $query ) ) { |
||
316 | return $posts; |
||
317 | } |
||
318 | |||
319 | $this->do_search( $query ); |
||
320 | |||
321 | if ( ! is_array( $this->search_result ) ) { |
||
322 | return $posts; |
||
323 | } |
||
324 | |||
325 | // If no results, nothing to do |
||
326 | if ( ! count( $this->search_result['results']['hits'] ) ) { |
||
327 | return array(); |
||
328 | } |
||
329 | |||
330 | $post_ids = array(); |
||
331 | |||
332 | foreach ( $this->search_result['results']['hits'] as $result ) { |
||
333 | $post_ids[] = (int) $result['fields']['post_id']; |
||
334 | } |
||
335 | |||
336 | // Query all posts now |
||
337 | $args = array( |
||
338 | 'post__in' => $post_ids, |
||
339 | 'perm' => 'readable', |
||
340 | 'post_type' => 'any', |
||
341 | 'ignore_sticky_posts' => true, |
||
342 | ); |
||
343 | |||
344 | if ( isset( $query->query_vars['order'] ) ) { |
||
345 | $args['order'] = $query->query_vars['order']; |
||
346 | } |
||
347 | |||
348 | if ( isset( $query->query_vars['orderby'] ) ) { |
||
349 | $args['orderby'] = $query->query_vars['orderby']; |
||
350 | } |
||
351 | |||
352 | $posts_query = new WP_Query( $args ); |
||
353 | |||
354 | // 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. |
||
355 | $query->found_posts = $this->found_posts; |
||
356 | $query->max_num_pages = ceil( $this->found_posts / $query->get( 'posts_per_page' ) ); |
||
357 | |||
358 | return $posts_query->posts; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Build up the search, then run it against the Jetpack servers |
||
363 | * |
||
364 | * @param WP_Query $query The original WP_Query to use for the parameters of our search |
||
365 | */ |
||
366 | public function do_search( WP_Query $query ) { |
||
367 | $page = ( $query->get( 'paged' ) ) ? absint( $query->get( 'paged' ) ) : 1; |
||
368 | |||
369 | $posts_per_page = $query->get( 'posts_per_page' ); |
||
370 | |||
371 | // ES API does not allow more than 15 results at a time |
||
372 | if ( $posts_per_page > 15 ) { |
||
373 | $posts_per_page = 15; |
||
374 | } |
||
375 | |||
376 | // Start building the WP-style search query args |
||
377 | // They'll be translated to ES format args later |
||
378 | $es_wp_query_args = array( |
||
379 | 'query' => $query->get( 's' ), |
||
380 | 'posts_per_page' => $posts_per_page, |
||
381 | 'paged' => $page, |
||
382 | 'orderby' => $query->get( 'orderby' ), |
||
383 | 'order' => $query->get( 'order' ), |
||
384 | ); |
||
385 | |||
386 | if ( ! empty( $this->aggregations ) ) { |
||
387 | $es_wp_query_args['aggregations'] = $this->aggregations; |
||
388 | } |
||
389 | |||
390 | // Did we query for authors? |
||
391 | if ( $query->get( 'author_name' ) ) { |
||
392 | $es_wp_query_args['author_name'] = $query->get( 'author_name' ); |
||
393 | } |
||
394 | |||
395 | $es_wp_query_args['post_type'] = $this->get_es_wp_query_post_type_for_query( $query ); |
||
396 | |||
397 | $es_wp_query_args['terms'] = $this->get_es_wp_query_terms_for_query( $query ); |
||
|
|||
398 | |||
399 | |||
400 | /** |
||
401 | * Modify the search query parameters, such as controlling the post_type. |
||
402 | * |
||
403 | * These arguments are in the format of WP_Query arguments |
||
404 | * |
||
405 | * @module search |
||
406 | * |
||
407 | * @since 5.0.0 |
||
408 | * |
||
409 | * @param array $es_wp_query_args The current query args, in WP_Query format |
||
410 | * @param WP_Query $query The original query object |
||
411 | */ |
||
412 | $es_wp_query_args = apply_filters( 'jetpack_search_es_wp_query_args', $es_wp_query_args, $query ); |
||
413 | |||
414 | // If page * posts_per_page is greater than our max offset, send a 404. This is necessary because the offset is |
||
415 | // capped at $this->max_offset, so a high page would always return the last page of results otherwise |
||
416 | if ( ( $es_wp_query_args['paged'] * $es_wp_query_args['posts_per_page'] ) > $this->max_offset ) { |
||
417 | $query->set_404(); |
||
418 | |||
419 | return; |
||
420 | } |
||
421 | |||
422 | // If there were no post types returned, then 404 to avoid querying against non-public post types, which could |
||
423 | // happen if we don't add the post type restriction to the ES query |
||
424 | if ( empty( $es_wp_query_args['post_type'] ) ) { |
||
425 | $query->set_404(); |
||
426 | |||
427 | return; |
||
428 | } |
||
429 | |||
430 | // Convert the WP-style args into ES args |
||
431 | $es_query_args = $this->convert_wp_es_to_es_args( $es_wp_query_args ); |
||
432 | |||
433 | //Only trust ES to give us IDs, not the content since it is a mirror |
||
434 | $es_query_args['fields'] = array( |
||
435 | 'post_id', |
||
436 | ); |
||
437 | |||
438 | /** |
||
439 | * Modify the underlying ES query that is passed to the search endpoint. The returned args must represent a valid ES query |
||
440 | * |
||
441 | * This filter is harder to use if you're unfamiliar with ES, but allows complete control over the query |
||
442 | * |
||
443 | * @module search |
||
444 | * |
||
445 | * @since 5.0.0 |
||
446 | * |
||
447 | * @param array $es_query_args The raw ES query args |
||
448 | * @param WP_Query $query The original query object |
||
449 | */ |
||
450 | $es_query_args = apply_filters( 'jetpack_search_es_query_args', $es_query_args, $query ); |
||
451 | |||
452 | // Do the actual search query! |
||
453 | $this->search_result = $this->search( $es_query_args ); |
||
454 | |||
455 | if ( is_wp_error( $this->search_result ) || ! is_array( $this->search_result ) || empty( $this->search_result['results'] ) || empty( $this->search_result['results']['hits'] ) ) { |
||
456 | $this->found_posts = 0; |
||
457 | |||
458 | return; |
||
459 | } |
||
460 | |||
461 | // If we have aggregations, fix the ordering to match the input order (ES doesn't guarantee the return order) |
||
462 | if ( isset( $this->search_result['results']['aggregations'] ) && ! empty( $this->search_result['results']['aggregations'] ) ) { |
||
463 | $this->search_result['results']['aggregations'] = $this->fix_aggregation_ordering( $this->search_result['results']['aggregations'], $this->aggregations ); |
||
464 | } |
||
465 | |||
466 | // Total number of results for paging purposes. Capped at $this->>max_offset + $posts_per_page, as deep paging |
||
467 | // gets quite expensive |
||
468 | $this->found_posts = min( $this->search_result['results']['total'], $this->max_offset + $posts_per_page ); |
||
469 | |||
470 | return; |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * If the query has already been run before filters have been updated, then we need to re-run the query |
||
475 | * to get the latest aggregations. |
||
476 | * |
||
477 | * This is especially useful for supporting widget management in the customizer. |
||
478 | * |
||
479 | * @return bool Whether the query was successful or not. |
||
480 | */ |
||
481 | public function update_search_results_aggregations() { |
||
482 | if ( empty( $this->last_query_info ) || empty( $this->last_query_info['args'] ) ) { |
||
483 | return false; |
||
484 | } |
||
485 | |||
486 | $es_args = $this->last_query_info['args']; |
||
487 | $builder = new Jetpack_WPES_Query_Builder(); |
||
488 | $this->add_aggregations_to_es_query_builder( $this->aggregations, $builder ); |
||
489 | $es_args['aggregations'] = $builder->build_aggregation(); |
||
490 | |||
491 | $this->search_result = $this->search( $es_args ); |
||
492 | |||
493 | return ! is_wp_error( $this->search_result ); |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Given a WP_Query, convert its WP_Tax_Query (if present) into the WP-style ES term arguments for the search |
||
498 | * |
||
499 | * @module search |
||
500 | * |
||
501 | * @param WP_Query $query The original WP_Query object for which to parse the taxonomy query |
||
502 | * |
||
503 | * @return array The new WP-style ES arguments (that will be converted into 'real' ES arguments) |
||
504 | */ |
||
505 | public function get_es_wp_query_terms_for_query( WP_Query $query ) { |
||
538 | |||
539 | /** |
||
540 | * Parse out the post type from a WP_Query |
||
541 | * |
||
542 | * Only allows post types that are not marked as 'exclude_from_search' |
||
543 | * |
||
544 | * @module search |
||
545 | * |
||
546 | * @param WP_Query $query Original WP_Query object |
||
547 | * |
||
548 | * @return array Array of searchable post types corresponding to the original query |
||
549 | */ |
||
550 | public function get_es_wp_query_post_type_for_query( WP_Query $query ) { |
||
584 | |||
585 | /** |
||
586 | * Initialize widgets for the Search module |
||
587 | * |
||
588 | * @module search |
||
589 | */ |
||
590 | public function action__widgets_init() { |
||
595 | |||
596 | /** |
||
597 | * Get the Elasticsearch result |
||
598 | * |
||
599 | * @module search |
||
600 | * |
||
601 | * @param bool $raw If true, does not check for WP_Error or return the 'results' array - the JSON decoded HTTP response |
||
602 | * |
||
603 | * @return array|bool The search results, or false if there was a failure |
||
604 | */ |
||
605 | public function get_search_result( $raw = false ) { |
||
612 | |||
613 | /** |
||
614 | * Add the date portion of a WP_Query onto the query args |
||
615 | * |
||
616 | * @param array $es_wp_query_args |
||
617 | * @param WP_Query $query The original WP_Query |
||
618 | * |
||
619 | * @return array The es wp query args, with date filters added (as needed) |
||
620 | */ |
||
621 | public function filter__add_date_filter_to_query( array $es_wp_query_args, WP_Query $query ) { |
||
653 | |||
654 | /** |
||
655 | * Converts WP_Query style args to ES args |
||
656 | * |
||
657 | * @module search |
||
658 | * |
||
659 | * @param array $args Array of WP_Query style arguments |
||
660 | * |
||
661 | * @return array Array of ES style query arguments |
||
662 | */ |
||
663 | function convert_wp_es_to_es_args( array $args ) { |
||
664 | jetpack_require_lib( 'jetpack-wpes-query-builder/jetpack-wpes-query-parser' ); |
||
665 | |||
666 | $defaults = array( |
||
1013 | |||
1014 | /** |
||
1015 | * Given an array of aggregations, parse and add them onto the Jetpack_WPES_Query_Builder object for use in ES |
||
1016 | * |
||
1017 | * @module search |
||
1018 | * |
||
1019 | * @param array $aggregations Array of Aggregations (filters) to add to the Jetpack_WPES_Query_Builder |
||
1020 | * |
||
1021 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the ES query |
||
1022 | */ |
||
1023 | public function add_aggregations_to_es_query_builder( array $aggregations, Jetpack_WPES_Query_Builder $builder ) { |
||
1043 | |||
1044 | /** |
||
1045 | * Given an individual taxonomy aggregation, add it to the Jetpack_WPES_Query_Builder object for use in ES |
||
1046 | * |
||
1047 | * @module search |
||
1048 | * |
||
1049 | * @param array $aggregation The aggregation to add to the query builder |
||
1050 | * @param string $label The 'label' (unique id) for this aggregation |
||
1051 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the ES query |
||
1052 | */ |
||
1053 | public function add_taxonomy_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
1077 | |||
1078 | /** |
||
1079 | * Given an individual post_type aggregation, add it to the Jetpack_WPES_Query_Builder object for use in ES |
||
1080 | * |
||
1081 | * @module search |
||
1082 | * |
||
1083 | * @param array $aggregation The aggregation to add to the query builder |
||
1084 | * @param string $label The 'label' (unique id) for this aggregation |
||
1085 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the ES query |
||
1086 | */ |
||
1087 | public function add_post_type_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
1095 | |||
1096 | /** |
||
1097 | * Given an individual date_histogram aggregation, add it to the Jetpack_WPES_Query_Builder object for use in ES |
||
1098 | * |
||
1099 | * @module search |
||
1100 | * |
||
1101 | * @param array $aggregation The aggregation to add to the query builder |
||
1102 | * @param string $label The 'label' (unique id) for this aggregation |
||
1103 | * @param Jetpack_WPES_Query_Builder $builder The builder instance that is creating the ES query |
||
1104 | */ |
||
1105 | public function add_date_histogram_aggregation_to_es_query_builder( array $aggregation, $label, Jetpack_WPES_Query_Builder $builder ) { |
||
1121 | |||
1122 | /** |
||
1123 | * And an existing filter object with a list of additional filters. |
||
1124 | * |
||
1125 | * Attempts to optimize the filters somewhat. |
||
1126 | * |
||
1127 | * @module search |
||
1128 | * |
||
1129 | * @param array $curr_filter The existing filters to build upon |
||
1130 | * @param array $filters The new filters to add |
||
1131 | * |
||
1132 | * @return array The resulting merged filters |
||
1133 | */ |
||
1134 | public static function and_es_filters( array $curr_filter, array $filters ) { |
||
1149 | |||
1150 | /** |
||
1151 | * Set the available filters for the search |
||
1152 | * |
||
1153 | * These get rendered via the Jetpack_Search_Widget_Filters() widget |
||
1154 | * |
||
1155 | * Behind the scenes, these are implemented using Elasticsearch Aggregations. |
||
1156 | * |
||
1157 | * If you do not require counts of how many documents match each filter, please consider using regular WP Query |
||
1158 | * arguments instead, such as via the jetpack_search_es_wp_query_args filter |
||
1159 | * |
||
1160 | * @module search |
||
1161 | * |
||
1162 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
1163 | * |
||
1164 | * @param array $aggregations Array of filters (aggregations) to apply to the search |
||
1165 | */ |
||
1166 | public function set_filters( array $aggregations ) { |
||
1174 | |||
1175 | /** |
||
1176 | * Set the search's facets (deprecated) |
||
1177 | * |
||
1178 | * @module search |
||
1179 | * |
||
1180 | * @deprecated 5.0 Please use Jetpack_Search::set_filters() instead |
||
1181 | * |
||
1182 | * @see Jetpack_Search::set_filters() |
||
1183 | * |
||
1184 | * @param array $facets Array of facets to apply to the search |
||
1185 | */ |
||
1186 | public function set_facets( array $facets ) { |
||
1191 | |||
1192 | /** |
||
1193 | * Get the raw Aggregation results from the ES response |
||
1194 | * |
||
1195 | * @module search |
||
1196 | * |
||
1197 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html |
||
1198 | * |
||
1199 | * @return array Array of Aggregations performed on the search |
||
1200 | */ |
||
1201 | public function get_search_aggregations_results() { |
||
1212 | |||
1213 | /** |
||
1214 | * Get the raw Facet results from the ES response |
||
1215 | * |
||
1216 | * @module search |
||
1217 | * |
||
1218 | * @deprecated 5.0 Please use Jetpack_Search::get_search_aggregations_results() instead |
||
1219 | * |
||
1220 | * @see Jetpack_Search::get_search_aggregations_results() |
||
1221 | * |
||
1222 | * @return array Array of Facets performed on the search |
||
1223 | */ |
||
1224 | public function get_search_facets() { |
||
1229 | |||
1230 | /** |
||
1231 | * Get the results of the Filters performed, including the number of matching documents |
||
1232 | * |
||
1233 | * Returns an array of Filters (keyed by $label, as passed to Jetpack_Search::set_filters()), containing the Filter and all resulting |
||
1234 | * matching buckets, the url for applying/removing each bucket, etc. |
||
1235 | * |
||
1236 | * NOTE - if this is called before the search is performed, an empty array will be returned. Use the $aggregations class |
||
1237 | * member if you need to access the raw filters set in Jetpack_Search::set_filters() |
||
1238 | * |
||
1239 | * @module search |
||
1240 | * |
||
1241 | * @param WP_Query $query The optional original WP_Query to use for determining which filters are active. Defaults to the main query |
||
1242 | * |
||
1243 | * @return array Array of Filters applied and info about them |
||
1244 | */ |
||
1245 | public function get_filters( WP_Query $query = null ) { |
||
1494 | |||
1495 | /** |
||
1496 | * Get the results of the Facets performed |
||
1497 | * |
||
1498 | * @module search |
||
1499 | * |
||
1500 | * @deprecated 5.0 Please use Jetpack_Search::get_filters() instead |
||
1501 | * |
||
1502 | * @see Jetpack_Search::get_filters() |
||
1503 | * |
||
1504 | * @return array $facets Array of Facets applied and info about them |
||
1505 | */ |
||
1506 | public function get_search_facet_data() { |
||
1511 | |||
1512 | /** |
||
1513 | * Get the Filters that are currently applied to this search |
||
1514 | * |
||
1515 | * @module search |
||
1516 | * |
||
1517 | * @return array Array if Filters that were applied |
||
1518 | */ |
||
1519 | public function get_active_filter_buckets() { |
||
1540 | |||
1541 | /** |
||
1542 | * Get the Filters that are currently applied to this search |
||
1543 | * |
||
1544 | * @module search |
||
1545 | * |
||
1546 | * @return array Array if Filters that were applied |
||
1547 | */ |
||
1548 | public function get_current_filters() { |
||
1553 | |||
1554 | /** |
||
1555 | * Calculate the right query var to use for a given taxonomy |
||
1556 | * |
||
1557 | * Allows custom code to modify the GET var that is used to represent a given taxonomy, via the jetpack_search_taxonomy_query_var filter |
||
1558 | * |
||
1559 | * @module search |
||
1560 | * |
||
1561 | * @param string $taxonomy_name The name of the taxonomy for which to get the query var |
||
1562 | * |
||
1563 | * @return bool|string The query var to use for this taxonomy, or false if none found |
||
1564 | */ |
||
1565 | public function get_taxonomy_query_var( $taxonomy_name ) { |
||
1584 | |||
1585 | /** |
||
1586 | * Takes an array of aggregation results, and ensures the array key ordering matches the key order in $desired |
||
1587 | * which is the input order |
||
1588 | * |
||
1589 | * Necessary because ES does not always return Aggs in the same order that you pass them in, and it should be possible |
||
1590 | * to control the display order easily |
||
1591 | * |
||
1592 | * @module search |
||
1593 | * |
||
1594 | * @param array $aggregations Agg results to be reordered |
||
1595 | * @param array $desired Array with keys representing the desired ordering |
||
1596 | * |
||
1597 | * @return array A new array with reordered keys, matching those in $desired |
||
1598 | */ |
||
1599 | public function fix_aggregation_ordering( array $aggregations, array $desired ) { |
||
1614 | |||
1615 | public function track_widget_updates( $option, $old_value, $new_value ) { |
||
1631 | } |
||
1632 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.