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 |
||
| 17 | class Jetpack_Instant_Search extends Jetpack_Search { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Loads the php for this version of search |
||
| 21 | * |
||
| 22 | * @since 8.3.0 |
||
| 23 | */ |
||
| 24 | public function load_php() { |
||
| 25 | $this->base_load_php(); |
||
| 26 | |||
| 27 | if ( class_exists( 'WP_Customize_Manager' ) ) { |
||
| 28 | require_once dirname( __FILE__ ) . '/class-jetpack-search-customize.php'; |
||
| 29 | new Jetpack_Search_Customize(); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Setup the various hooks needed for the plugin to take over search duties. |
||
| 35 | * |
||
| 36 | * @since 5.0.0 |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function init_hooks() { |
|
| 39 | if ( ! is_admin() ) { |
||
| 40 | add_filter( 'posts_pre_query', array( $this, 'filter__posts_pre_query' ), 10, 2 ); |
||
| 41 | add_action( 'parse_query', array( $this, 'action__parse_query' ), 10, 1 ); |
||
| 42 | |||
| 43 | add_action( 'init', array( $this, 'set_filters_from_widgets' ) ); |
||
| 44 | |||
| 45 | add_action( 'wp_enqueue_scripts', array( $this, 'load_assets' ) ); |
||
| 46 | add_action( 'wp_footer', array( $this, 'print_instant_search_sidebar' ) ); |
||
| 47 | } else { |
||
| 48 | add_action( 'update_option', array( $this, 'track_widget_updates' ), 10, 3 ); |
||
| 49 | } |
||
| 50 | |||
| 51 | add_action( 'widgets_init', array( $this, 'register_jetpack_instant_sidebar' ) ); |
||
| 52 | add_action( 'jetpack_deactivate_module_search', array( $this, 'move_search_widgets_to_inactive' ) ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Loads assets for Jetpack Instant Search Prototype featuring Search As You Type experience. |
||
| 57 | */ |
||
| 58 | public function load_assets() { |
||
| 59 | $script_relative_path = '_inc/build/instant-search/jp-search.bundle.js'; |
||
| 60 | $style_relative_path = '_inc/build/instant-search/instant-search.min.css'; |
||
| 61 | if ( ! file_exists( JETPACK__PLUGIN_DIR . $script_relative_path ) || ! file_exists( JETPACK__PLUGIN_DIR . $style_relative_path ) ) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | $script_version = Jetpack_Search_Helpers::get_asset_version( $script_relative_path ); |
||
| 66 | $script_path = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
||
| 67 | wp_enqueue_script( 'jetpack-instant-search', $script_path, array(), $script_version, true ); |
||
| 68 | wp_set_script_translations( 'jetpack-instant-search', 'jetpack' ); |
||
| 69 | $this->load_and_initialize_tracks(); |
||
| 70 | $this->inject_javascript_options(); |
||
| 71 | |||
| 72 | $style_version = Jetpack_Search_Helpers::get_asset_version( $style_relative_path ); |
||
| 73 | $style_path = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); |
||
| 74 | wp_enqueue_style( 'jetpack-instant-search', $style_path, array(), $style_version ); |
||
| 75 | } |
||
| 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 | if ( 'immediate' === get_option( $prefix . 'overlay_trigger' ) ) { |
||
| 126 | update_option( $prefix . 'overlay_trigger', 'focus' ); |
||
| 127 | } elseif ( 'results' === get_option( $prefix . 'overlay_trigger' ) ) { |
||
| 128 | update_option( $prefix . 'overlay_trigger', 'submit' ); |
||
| 129 | } |
||
| 130 | |||
| 131 | $options = array( |
||
| 132 | 'overlayOptions' => array( |
||
| 133 | 'colorTheme' => get_option( $prefix . 'color_theme', 'light' ), |
||
| 134 | 'enableInfScroll' => get_option( $prefix . 'inf_scroll', '1' ) === '1', |
||
| 135 | 'enableSort' => get_option( $prefix . 'enable_sort', '1' ) === '1', |
||
| 136 | 'highlightColor' => get_option( $prefix . 'highlight_color', '#FFC' ), |
||
| 137 | 'opacity' => (int) get_option( $prefix . 'opacity', 97 ), |
||
| 138 | 'overlayTrigger' => get_option( $prefix . 'overlay_trigger', 'submit' ), |
||
| 139 | 'showPoweredBy' => get_option( $prefix . 'show_powered_by', '1' ) === '1', |
||
| 140 | ), |
||
| 141 | |||
| 142 | // core config. |
||
| 143 | 'homeUrl' => home_url(), |
||
| 144 | 'locale' => str_replace( '_', '-', Jetpack_Search_Helpers::is_valid_locale( get_locale() ) ? get_locale() : 'en_US' ), |
||
| 145 | 'postsPerPage' => $posts_per_page, |
||
| 146 | 'siteId' => $this->jetpack_blog_id, |
||
| 147 | 'postTypes' => $post_type_labels, |
||
| 148 | |||
| 149 | // search options. |
||
| 150 | 'defaultSort' => get_option( $prefix . 'default_sort', 'relevance' ), |
||
| 151 | |||
| 152 | // widget info. |
||
| 153 | 'hasOverlayWidgets' => count( $overlay_widget_ids ) > 0, |
||
| 154 | 'widgets' => array_values( $widgets ), |
||
| 155 | 'widgetsOutsideOverlay' => array_values( $widgets_outside_overlay ), |
||
| 156 | ); |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Customize Instant Search Options. |
||
| 160 | * |
||
| 161 | * @module search |
||
| 162 | * |
||
| 163 | * @since 7.7.0 |
||
| 164 | * |
||
| 165 | * @param array $options Array of parameters used in Instant Search queries. |
||
| 166 | */ |
||
| 167 | $options = apply_filters( 'jetpack_instant_search_options', $options ); |
||
| 168 | |||
| 169 | // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280. |
||
| 170 | wp_add_inline_script( 'jetpack-instant-search', 'var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $options ) ) . '"));' ); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Registers a widget sidebar for Instant Search. |
||
| 175 | */ |
||
| 176 | public function register_jetpack_instant_sidebar() { |
||
| 177 | $args = array( |
||
| 178 | 'name' => __( 'Jetpack Search Sidebar', 'jetpack' ), |
||
| 179 | 'id' => 'jetpack-instant-search-sidebar', |
||
| 180 | 'description' => __( 'Customize the sidebar inside the Jetpack Search overlay', 'jetpack' ), |
||
| 181 | 'class' => '', |
||
| 182 | 'before_widget' => '<div id="%1$s" class="widget %2$s">', |
||
| 183 | 'after_widget' => '</div>', |
||
| 184 | 'before_title' => '<h2 class="widgettitle">', |
||
| 185 | 'after_title' => '</h2>', |
||
| 186 | ); |
||
| 187 | register_sidebar( $args ); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Prints Instant Search sidebar. |
||
| 192 | */ |
||
| 193 | public function print_instant_search_sidebar() { |
||
| 194 | ?> |
||
| 195 | <div class="jetpack-instant-search__widget-area" style="display: none"> |
||
| 196 | <?php if ( is_active_sidebar( 'jetpack-instant-search-sidebar' ) ) { ?> |
||
| 197 | <?php dynamic_sidebar( 'jetpack-instant-search-sidebar' ); ?> |
||
| 198 | <?php } ?> |
||
| 199 | </div> |
||
| 200 | <?php |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Loads scripts for Tracks analytics library |
||
| 205 | */ |
||
| 206 | public function load_and_initialize_tracks() { |
||
| 207 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Bypass the normal Search query since we will run it with instant search. |
||
| 212 | * |
||
| 213 | * @since 8.3.0 |
||
| 214 | * |
||
| 215 | * @param array $posts Current array of posts (still pre-query). |
||
| 216 | * @param WP_Query $query The WP_Query being filtered. |
||
| 217 | * |
||
| 218 | * @return array Array of matching posts. |
||
| 219 | */ |
||
| 220 | public function filter__posts_pre_query( $posts, $query ) { |
||
| 221 | if ( ! $this->should_handle_query( $query ) ) { |
||
| 222 | // Intentionally not adding the 'jetpack_search_abort' action since this should fire for every request except for search. |
||
| 223 | return $posts; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Bypass the main query and return dummy data |
||
| 228 | * WP Core doesn't call the set_found_posts and its filters when filtering |
||
| 229 | * posts_pre_query like we do, so need to do these manually. |
||
| 230 | */ |
||
| 231 | $query->found_posts = 1; |
||
| 232 | $query->max_num_pages = 1; |
||
| 233 | |||
| 234 | return array(); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Run the aggregations API query for any filtering |
||
| 239 | * |
||
| 240 | * @since 8.3.0 |
||
| 241 | * |
||
| 242 | * @param WP_Query $query The WP_Query being filtered. |
||
| 243 | */ |
||
| 244 | public function action__parse_query( $query ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Run an instant search on the WordPress.com public API. |
||
| 272 | * |
||
| 273 | * @since 8.3.0 |
||
| 274 | * |
||
| 275 | * @param array $args Args conforming to the WP.com v1.3/sites/<blog_id>/search endpoint. |
||
| 276 | * |
||
| 277 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
| 278 | */ |
||
| 279 | public function instant_api( array $args ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get the raw Aggregation results from the Elasticsearch response. |
||
| 376 | * |
||
| 377 | * @since 8.4.0 |
||
| 378 | * |
||
| 379 | * @return array Array of Aggregations performed on the search. |
||
| 380 | */ |
||
| 381 | public function get_search_aggregations_results() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Autoconfig search by adding filter widgets |
||
| 391 | * |
||
| 392 | * @since 8.3.0 |
||
| 393 | */ |
||
| 394 | public function auto_config_search() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Autoconfig search by adding filter widgets |
||
| 479 | * |
||
| 480 | * @since 8.4.0 |
||
| 481 | * |
||
| 482 | * @return array Array of config settings for search widget. |
||
| 483 | */ |
||
| 484 | protected function get_preconfig_widget_options() { |
||
| 547 | |||
| 548 | } |
||
| 549 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.