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_Instant_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_Instant_Search, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Jetpack_Instant_Search extends Jetpack_Search { |
||
19 | |||
20 | /** |
||
21 | * Loads the php for this version of search |
||
22 | * |
||
23 | * @since 8.3.0 |
||
24 | */ |
||
25 | public function load_php() { |
||
26 | $this->base_load_php(); |
||
27 | |||
28 | if ( class_exists( 'WP_Customize_Manager' ) ) { |
||
29 | require_once dirname( __FILE__ ) . '/class-jetpack-search-customize.php'; |
||
30 | new Jetpack_Search_Customize(); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Setup the various hooks needed for the plugin to take over search duties. |
||
36 | * |
||
37 | * @since 5.0.0 |
||
38 | */ |
||
39 | View Code Duplication | public function init_hooks() { |
|
55 | |||
56 | /** |
||
57 | * Loads assets for Jetpack Instant Search Prototype featuring Search As You Type experience. |
||
58 | */ |
||
59 | public function load_assets() { |
||
76 | |||
77 | /** |
||
78 | * Passes all options to the JS app. |
||
79 | */ |
||
80 | protected function inject_javascript_options() { |
||
81 | $widget_options = Jetpack_Search_Helpers::get_widgets_from_option(); |
||
82 | if ( is_array( $widget_options ) ) { |
||
83 | $widget_options = end( $widget_options ); |
||
|
|||
84 | } |
||
85 | |||
86 | $overlay_widget_ids = is_active_sidebar( 'jetpack-instant-search-sidebar' ) ? |
||
87 | wp_get_sidebars_widgets()['jetpack-instant-search-sidebar'] : array(); |
||
88 | $filters = Jetpack_Search_Helpers::get_filters_from_widgets(); |
||
89 | $widgets = array(); |
||
90 | $widgets_outside_overlay = array(); |
||
91 | foreach ( $filters as $key => &$filter ) { |
||
92 | $filter['filter_id'] = $key; |
||
93 | |||
94 | if ( in_array( $filter['widget_id'], $overlay_widget_ids, true ) ) { |
||
95 | View Code Duplication | if ( ! isset( $widgets[ $filter['widget_id'] ] ) ) { |
|
96 | $widgets[ $filter['widget_id'] ]['filters'] = array(); |
||
97 | $widgets[ $filter['widget_id'] ]['widget_id'] = $filter['widget_id']; |
||
98 | } |
||
99 | $widgets[ $filter['widget_id'] ]['filters'][] = $filter; |
||
100 | } else { |
||
101 | View Code Duplication | if ( ! isset( $widgets_outside_overlay[ $filter['widget_id'] ] ) ) { |
|
102 | $widgets_outside_overlay[ $filter['widget_id'] ]['filters'] = array(); |
||
103 | $widgets_outside_overlay[ $filter['widget_id'] ]['widget_id'] = $filter['widget_id']; |
||
104 | } |
||
105 | $widgets_outside_overlay[ $filter['widget_id'] ]['filters'][] = $filter; |
||
106 | } |
||
107 | } |
||
108 | unset( $filter ); |
||
109 | |||
110 | $post_type_objs = get_post_types( array( 'exclude_from_search' => false ), 'objects' ); |
||
111 | $post_type_labels = array(); |
||
112 | foreach ( $post_type_objs as $key => $obj ) { |
||
113 | $post_type_labels[ $key ] = array( |
||
114 | 'singular_name' => $obj->labels->singular_name, |
||
115 | 'name' => $obj->labels->name, |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | $prefix = Jetpack_Search_Options::OPTION_PREFIX; |
||
120 | $posts_per_page = (int) get_option( 'posts_per_page' ); |
||
121 | if ( ( $posts_per_page > 20 ) || ( $posts_per_page <= 0 ) ) { |
||
122 | $posts_per_page = 20; |
||
123 | } |
||
124 | |||
125 | $options = array( |
||
126 | 'overlayOptions' => array( |
||
127 | 'colorTheme' => get_option( $prefix . 'color_theme', 'light' ), |
||
128 | 'enableInfScroll' => (bool) get_option( $prefix . 'inf_scroll', false ), |
||
129 | 'enableSort' => (bool) get_option( $prefix . 'enable_sort', true ), |
||
130 | 'highlightColor' => get_option( $prefix . 'highlight_color', '#FFC' ), |
||
131 | 'opacity' => (int) get_option( $prefix . 'opacity', 97 ), |
||
132 | 'overlayTrigger' => get_option( $prefix . 'overlay_trigger', 'immediate' ), |
||
133 | 'showPoweredBy' => (bool) get_option( $prefix . 'show_powered_by', true ), |
||
134 | ), |
||
135 | |||
136 | // core config. |
||
137 | 'homeUrl' => home_url(), |
||
138 | 'locale' => str_replace( '_', '-', Jetpack_Search_Helpers::is_valid_locale( get_locale() ) ? get_locale() : 'en_US' ), |
||
139 | 'postsPerPage' => $posts_per_page, |
||
140 | 'siteId' => Jetpack::get_option( 'id' ), |
||
141 | 'postTypes' => $post_type_labels, |
||
142 | |||
143 | // widget info. |
||
144 | 'widgets' => array_values( $widgets ), |
||
145 | 'widgetsOutsideOverlay' => array_values( $widgets_outside_overlay ), |
||
146 | ); |
||
147 | |||
148 | /** |
||
149 | * Customize Instant Search Options. |
||
150 | * |
||
151 | * @module search |
||
152 | * |
||
153 | * @since 7.7.0 |
||
154 | * |
||
155 | * @param array $options Array of parameters used in Instant Search queries. |
||
156 | */ |
||
157 | $options = apply_filters( 'jetpack_instant_search_options', $options ); |
||
158 | |||
159 | // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280. |
||
160 | wp_add_inline_script( 'jetpack-instant-search', 'var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $options ) ) . '"));' ); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Registers a widget sidebar for Instant Search. |
||
165 | */ |
||
166 | public function register_jetpack_instant_sidebar() { |
||
167 | $args = array( |
||
168 | 'name' => __( 'Jetpack Search Sidebar', 'jetpack' ), |
||
169 | 'id' => 'jetpack-instant-search-sidebar', |
||
170 | 'description' => __( 'Customize the sidebar inside the Jetpack Search overlay', 'jetpack' ), |
||
171 | 'class' => '', |
||
172 | 'before_widget' => '<div id="%1$s" class="widget %2$s">', |
||
173 | 'after_widget' => '</div>', |
||
174 | 'before_title' => '<h2 class="widgettitle">', |
||
175 | 'after_title' => '</h2>', |
||
176 | ); |
||
177 | register_sidebar( $args ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Prints Instant Search sidebar. |
||
182 | */ |
||
183 | public function print_instant_search_sidebar() { |
||
184 | ?> |
||
185 | <div class="jetpack-instant-search__widget-area" style="display: none"> |
||
186 | <?php if ( is_active_sidebar( 'jetpack-instant-search-sidebar' ) ) { ?> |
||
187 | <?php dynamic_sidebar( 'jetpack-instant-search-sidebar' ); ?> |
||
188 | <?php } ?> |
||
189 | </div> |
||
190 | <?php |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Loads scripts for Tracks analytics library |
||
195 | */ |
||
196 | public function load_and_initialize_tracks() { |
||
197 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Bypass the normal Search query since we will run it with instant search. |
||
202 | * |
||
203 | * @since 8.3.0 |
||
204 | * |
||
205 | * @param array $posts Current array of posts (still pre-query). |
||
206 | * @param WP_Query $query The WP_Query being filtered. |
||
207 | * |
||
208 | * @return array Array of matching posts. |
||
209 | */ |
||
210 | public function filter__posts_pre_query( $posts, $query ) { |
||
211 | if ( ! $this->should_handle_query( $query ) ) { |
||
212 | // Intentionally not adding the 'jetpack_search_abort' action since this should fire for every request except for search. |
||
213 | return $posts; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Bypass the main query and return dummy data |
||
218 | * WP Core doesn't call the set_found_posts and its filters when filtering |
||
219 | * posts_pre_query like we do, so need to do these manually. |
||
220 | */ |
||
221 | $query->found_posts = 1; |
||
222 | $query->max_num_pages = 1; |
||
223 | |||
224 | return array(); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Run the aggregations API query for any filtering |
||
229 | * |
||
230 | * @since 8.3.0 |
||
231 | * |
||
232 | * @param WP_Query $query The WP_Query being filtered. |
||
233 | */ |
||
234 | public function action__parse_query( $query ) { |
||
235 | if ( ! empty( $this->search_result ) ) { |
||
236 | return; |
||
237 | } |
||
238 | |||
239 | if ( is_admin() ) { |
||
240 | return; |
||
241 | } |
||
242 | |||
243 | if ( empty( $this->aggregations ) ) { |
||
244 | return; |
||
245 | } |
||
246 | |||
247 | jetpack_require_lib( 'jetpack-wpes-query-builder/jetpack-wpes-query-builder' ); |
||
248 | |||
249 | $builder = new Jetpack_WPES_Query_Builder(); |
||
250 | $this->add_aggregations_to_es_query_builder( $this->aggregations, $builder ); |
||
251 | $this->search_result = $this->instant_api( |
||
252 | array( |
||
253 | 'aggregations' => $builder->build_aggregation(), |
||
254 | 'size' => 0, |
||
255 | 'from' => 0, |
||
256 | ) |
||
257 | ); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Run an instant search on the WordPress.com public API. |
||
262 | * |
||
263 | * @since 8.3.0 |
||
264 | * |
||
265 | * @param array $args Args conforming to the WP.com v1.3/sites/<blog_id>/search endpoint. |
||
266 | * |
||
267 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
268 | */ |
||
269 | public function instant_api( array $args ) { |
||
270 | global $wp_version; |
||
271 | $start_time = microtime( true ); |
||
272 | |||
273 | // Cache locally to avoid remote request slowing the page. |
||
274 | $transient_name = 'jetpack_instant_search_cache_' . md5( wp_json_encode( $args ) ); |
||
275 | $cache = get_transient( $transient_name ); |
||
276 | if ( false !== $cache ) { |
||
277 | return $cache; |
||
278 | } |
||
279 | |||
280 | $service_url = add_query_arg( |
||
281 | $args, |
||
282 | sprintf( |
||
283 | 'https://public-api.wordpress.com/rest/v1.3/sites/%d/search', |
||
284 | $this->jetpack_blog_id |
||
285 | ) |
||
286 | ); |
||
287 | |||
288 | $request_args = array( |
||
289 | 'timeout' => 10, |
||
290 | 'user-agent' => "WordPress/{$wp_version} | Jetpack/" . constant( 'JETPACK__VERSION' ), |
||
291 | ); |
||
292 | |||
293 | $request = wp_remote_get( esc_url_raw( $service_url ), $request_args ); |
||
294 | $end_time = microtime( true ); |
||
295 | |||
296 | if ( is_wp_error( $request ) ) { |
||
297 | return $request; |
||
298 | } |
||
299 | |||
300 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
301 | $response = json_decode( wp_remote_retrieve_body( $request ), true ); |
||
302 | |||
303 | View Code Duplication | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
|
304 | /** |
||
305 | * Fires after a search query request has failed |
||
306 | * |
||
307 | * @module search |
||
308 | * |
||
309 | * @since 5.6.0 |
||
310 | * |
||
311 | * @param array Array containing the response code and response from the failed search query |
||
312 | */ |
||
313 | do_action( |
||
314 | 'failed_jetpack_search_query', |
||
315 | array( |
||
316 | 'response_code' => $response_code, |
||
317 | 'json' => $response, |
||
318 | ) |
||
319 | ); |
||
320 | |||
321 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
322 | } |
||
323 | |||
324 | $took = is_array( $response ) && ! empty( $response['took'] ) |
||
325 | ? $response['took'] |
||
326 | : null; |
||
327 | |||
328 | $query = array( |
||
329 | 'args' => $args, |
||
330 | 'response' => $response, |
||
331 | 'response_code' => $response_code, |
||
332 | 'elapsed_time' => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms. |
||
333 | 'es_time' => $took, |
||
334 | 'url' => $service_url, |
||
335 | ); |
||
336 | |||
337 | /** |
||
338 | * Fires after a search request has been performed. |
||
339 | * |
||
340 | * Includes the following info in the $query parameter: |
||
341 | * |
||
342 | * array args Array of Elasticsearch arguments for the search |
||
343 | * array response Raw API response, JSON decoded |
||
344 | * int response_code HTTP response code of the request |
||
345 | * float elapsed_time Roundtrip time of the search request, in milliseconds |
||
346 | * float es_time Amount of time Elasticsearch spent running the request, in milliseconds |
||
347 | * string url API url that was queried |
||
348 | * |
||
349 | * @module search |
||
350 | * |
||
351 | * @since 5.0.0 |
||
352 | * @since 5.8.0 This action now fires on all queries instead of just successful queries. |
||
353 | * |
||
354 | * @param array $query Array of information about the query performed |
||
355 | */ |
||
356 | do_action( 'did_jetpack_search_query', $query ); |
||
357 | |||
358 | // Update local cache. |
||
359 | set_transient( $transient_name, $response, 1 * HOUR_IN_SECONDS ); |
||
360 | |||
361 | return $response; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Get the raw Aggregation results from the Elasticsearch response. |
||
366 | * |
||
367 | * @since 8.4.0 |
||
368 | * |
||
369 | * @return array Array of Aggregations performed on the search. |
||
370 | */ |
||
371 | public function get_search_aggregations_results() { |
||
372 | if ( empty( $this->search_result ) || is_wp_error( $this->search_result ) || ! isset( $this->search_result['aggregations'] ) ) { |
||
373 | return array(); |
||
374 | } |
||
375 | |||
376 | return $this->search_result['aggregations']; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Autoconfig search by adding filter widgets |
||
381 | * |
||
382 | * @since 8.3.0 |
||
383 | */ |
||
384 | public function auto_config_search() { |
||
385 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
||
386 | return; |
||
387 | } |
||
388 | |||
389 | global $wp_registered_sidebars; |
||
390 | $sidebars = get_option( 'sidebars_widgets', array() ); |
||
391 | $slug = Jetpack_Search_Helpers::FILTER_WIDGET_BASE; |
||
392 | |||
393 | if ( isset( $sidebars['jetpack-instant-search-sidebar'] ) ) { |
||
394 | foreach ( (array) $sidebars['jetpack-instant-search-sidebar'] as $widget_id ) { |
||
395 | if ( 0 === strpos( $widget_id, $slug ) ) { |
||
396 | // Already configured. |
||
397 | return; |
||
398 | } |
||
399 | } |
||
400 | } |
||
401 | |||
402 | $has_sidebar = isset( $wp_registered_sidebars['sidebar-1'] ); |
||
403 | $sidebar_id = false; |
||
404 | $sidebar_searchbox_idx = false; |
||
405 | if ( $has_sidebar ) { |
||
406 | if ( empty( $sidebars['sidebar-1'] ) ) { |
||
407 | // Adding to an empty sidebar is generally a bad idea. |
||
408 | $has_sidebar = false; |
||
409 | } |
||
410 | foreach ( (array) $sidebars['sidebar-1'] as $idx => $widget_id ) { |
||
411 | if ( 0 === strpos( $widget_id, 'search-' ) ) { |
||
412 | $sidebar_searchbox_idx = $idx; |
||
413 | } |
||
414 | if ( 0 === strpos( $widget_id, $slug ) ) { |
||
415 | $sidebar_id = (int) str_replace( Jetpack_Search_Helpers::FILTER_WIDGET_BASE . '-', '', $widget_id ); |
||
416 | break; |
||
417 | } |
||
418 | } |
||
419 | } |
||
420 | |||
421 | $next_id = 1; |
||
422 | $widget_opt_name = Jetpack_Search_Helpers::get_widget_option_name(); |
||
423 | $widget_options = get_option( $widget_opt_name, array() ); |
||
424 | foreach ( $widget_options as $id => $w ) { |
||
425 | if ( $id >= $next_id ) { |
||
426 | $next_id = $id + 1; |
||
427 | } |
||
428 | } |
||
429 | |||
430 | // Copy sidebar settings to overlay. |
||
431 | if ( ( false !== $sidebar_id ) && isset( $widget_options[ $sidebar_id ] ) ) { |
||
432 | $widget_options[ $next_id ] = $widget_options[ $sidebar_id ]; |
||
433 | update_option( $widget_opt_name, $widget_options ); |
||
434 | |||
435 | if ( ! isset( $sidebars['jetpack-instant-search-sidebar'] ) ) { |
||
436 | $sidebars['jetpack-instant-search-sidebar'] = array(); |
||
437 | } |
||
438 | array_unshift( $sidebars['jetpack-instant-search-sidebar'], Jetpack_Search_Helpers::build_widget_id( $next_id ) ); |
||
439 | update_option( 'sidebars_widgets', $sidebars ); |
||
440 | |||
441 | return; |
||
442 | } |
||
443 | |||
444 | // Configure overlay and sidebar (if it exists). |
||
445 | $preconfig_opts = $this->get_preconfig_widget_options(); |
||
446 | if ( ! isset( $sidebars['jetpack-instant-search-sidebar'] ) ) { |
||
447 | $sidebars['jetpack-instant-search-sidebar'] = array(); |
||
448 | } |
||
449 | if ( $has_sidebar ) { |
||
450 | $widget_options[ $next_id ] = $preconfig_opts; |
||
451 | if ( false !== $sidebar_searchbox_idx ) { |
||
452 | // Replace Core search box. |
||
453 | $sidebars['sidebar-1'][ $sidebar_searchbox_idx ] = Jetpack_Search_Helpers::build_widget_id( $next_id ); |
||
454 | } else { |
||
455 | // Add to top. |
||
456 | array_unshift( $sidebars['sidebar-1'], Jetpack_Search_Helpers::build_widget_id( $next_id ) ); |
||
457 | } |
||
458 | $next_id++; |
||
459 | } |
||
460 | $widget_options[ $next_id ] = $preconfig_opts; |
||
461 | array_unshift( $sidebars['jetpack-instant-search-sidebar'], Jetpack_Search_Helpers::build_widget_id( $next_id ) ); |
||
462 | |||
463 | update_option( $widget_opt_name, $widget_options ); |
||
464 | update_option( 'sidebars_widgets', $sidebars ); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Autoconfig search by adding filter widgets |
||
469 | * |
||
470 | * @since 8.4.0 |
||
471 | * |
||
472 | * @return array Array of config settings for search widget. |
||
473 | */ |
||
474 | protected function get_preconfig_widget_options() { |
||
537 | |||
538 | } |
||
539 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.