Complex classes like Jetpack_Plugin_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_Plugin_Search, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Jetpack_Plugin_Search { |
||
| 33 | |||
| 34 | static $slug = 'jetpack-plugin-search'; |
||
| 35 | |||
| 36 | public static function init() { |
||
| 37 | static $instance = null; |
||
| 38 | |||
| 39 | if ( ! $instance ) { |
||
| 40 | $instance = new Jetpack_Plugin_Search(); |
||
| 41 | } |
||
| 42 | |||
| 43 | return $instance; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function __construct() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Add actions and filters only if this is the plugin installation screen and it's the first page. |
||
| 52 | * |
||
| 53 | * @param object $screen |
||
| 54 | * |
||
| 55 | * @since 7.1.0 |
||
| 56 | */ |
||
| 57 | public function start( $screen ) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Modify URL used to fetch to plugin information so it pulls Jetpack plugin page. |
||
| 68 | * |
||
| 69 | * @param string $url URL to load in dialog pulling the plugin page from wporg. |
||
| 70 | * |
||
| 71 | * @since 7.1.0 |
||
| 72 | * |
||
| 73 | * @return string The URL with 'jetpack' instead of 'jetpack-plugin-search'. |
||
| 74 | */ |
||
| 75 | public function plugin_details( $url ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Register REST API endpoints. |
||
| 83 | * |
||
| 84 | * @since 7.1.0 |
||
| 85 | */ |
||
| 86 | public static function register_endpoints() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * A WordPress REST API permission callback method that accepts a request object and |
||
| 104 | * decides if the current user has enough privileges to act. |
||
| 105 | * |
||
| 106 | * @since 7.1.0 |
||
| 107 | * |
||
| 108 | * @return bool does a current user have enough privileges. |
||
| 109 | */ |
||
| 110 | public static function can_request() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Validates that the ID of the hint to dismiss is a string. |
||
| 116 | * |
||
| 117 | * @since 7.1.0 |
||
| 118 | * |
||
| 119 | * @param string|bool $value Value to check. |
||
| 120 | * @param WP_REST_Request $request The request sent to the WP REST API. |
||
| 121 | * @param string $param Name of the parameter passed to endpoint holding $value. |
||
| 122 | * |
||
| 123 | * @return bool|WP_Error |
||
| 124 | */ |
||
| 125 | public static function is_hint_id( $value, $request, $param ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * A WordPress REST API callback method that accepts a request object and decides what to do with it. |
||
| 133 | * |
||
| 134 | * @param WP_REST_Request $request { |
||
| 135 | * Array of parameters received by request. |
||
| 136 | * |
||
| 137 | * @type string $hint Slug of card to dismiss. |
||
| 138 | * } |
||
| 139 | * |
||
| 140 | * @since 7.1.0 |
||
| 141 | * |
||
| 142 | * @return bool|array|WP_Error a resulting value or object, or an error. |
||
| 143 | */ |
||
| 144 | public static function dismiss( WP_REST_Request $request ) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns a list of previously dismissed hints. |
||
| 152 | * |
||
| 153 | * @since 7.1.0 |
||
| 154 | * |
||
| 155 | * @return array List of dismissed hints. |
||
| 156 | */ |
||
| 157 | protected static function get_dismissed_hints() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Save the hint in the list of dismissed hints. |
||
| 166 | * |
||
| 167 | * @since 7.1.0 |
||
| 168 | * |
||
| 169 | * @param string $hint The hint id, which is a Jetpack module slug. |
||
| 170 | * |
||
| 171 | * @return bool Whether the card was added to the list and hence dismissed. |
||
| 172 | */ |
||
| 173 | protected static function add_to_dismissed_hints( $hint ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Checks that the module slug passed should be displayed. |
||
| 179 | * |
||
| 180 | * A feature hint will be displayed if it has not been dismissed before or if 2 or fewer other hints have been dismissed. |
||
| 181 | * |
||
| 182 | * @since 7.2.1 |
||
| 183 | * |
||
| 184 | * @param string $hint The hint id, which is a Jetpack module slug. |
||
| 185 | * |
||
| 186 | * @return bool True if $hint should be displayed. |
||
| 187 | */ |
||
| 188 | protected function should_display_hint( $hint ) { |
||
| 189 | $dismissed_hints = $this->get_dismissed_hints(); |
||
| 190 | // If more than 2 hints have been dismissed, then show no more. |
||
| 191 | if ( 2 < count( $dismissed_hints ) ) { |
||
| 192 | return false; |
||
| 193 | } |
||
| 194 | |||
| 195 | $plan = Jetpack_Plan::get(); |
||
| 196 | if ( isset( $plan['class'] ) && ( 'free' === $plan['class'] || 'personal' === $plan['class'] ) && 'vaultpress' === $hint ) { |
||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | return ! in_array( $hint, $dismissed_hints, true ); |
||
| 201 | } |
||
| 202 | |||
| 203 | public function load_plugins_search_script() { |
||
| 204 | wp_enqueue_script( self::$slug, plugins_url( 'modules/plugin-search/plugin-search.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), JETPACK__VERSION, true ); |
||
| 205 | wp_localize_script( |
||
| 206 | self::$slug, |
||
| 207 | 'jetpackPluginSearch', |
||
| 208 | array( |
||
| 209 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
||
| 210 | 'base_rest_url' => rest_url( '/jetpack/v4' ), |
||
| 211 | 'poweredBy' => esc_html__( 'by Jetpack (installed)', 'jetpack' ), |
||
| 212 | 'manageSettings' => esc_html__( 'Configure', 'jetpack' ), |
||
| 213 | 'activateModule' => esc_html__( 'Activate Module', 'jetpack' ), |
||
| 214 | 'getStarted' => esc_html__( 'Get started', 'jetpack' ), |
||
| 215 | 'activated' => esc_html__( 'Activated', 'jetpack' ), |
||
| 216 | 'activating' => esc_html__( 'Activating', 'jetpack' ), |
||
| 217 | 'logo' => 'https://ps.w.org/jetpack/assets/icon.svg?rev=1791404', |
||
| 218 | 'legend' => esc_html__( |
||
| 219 | 'This suggestion was made by Jetpack, the security and performance plugin already installed on your site.', |
||
| 220 | 'jetpack' |
||
| 221 | ), |
||
| 222 | 'supportText' => esc_html__( |
||
| 223 | 'Learn more about these suggestions.', |
||
| 224 | 'jetpack' |
||
| 225 | ), |
||
| 226 | 'supportLink' => 'https://jetpack.com/redirect/?source=plugin-hint-learn-support', |
||
| 227 | 'hideText' => esc_html__( 'Hide this suggestion', 'jetpack' ), |
||
| 228 | ) |
||
| 229 | ); |
||
| 230 | |||
| 231 | wp_enqueue_style( self::$slug, plugins_url( 'modules/plugin-search/plugin-search.css', JETPACK__PLUGIN_FILE ) ); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the plugin repo's data for Jetpack to populate the fields with. |
||
| 236 | * |
||
| 237 | * @return array|mixed|object|WP_Error |
||
| 238 | */ |
||
| 239 | public static function get_jetpack_plugin_data() { |
||
| 240 | $data = get_transient( 'jetpack_plugin_data' ); |
||
| 241 | |||
| 242 | if ( false === $data || is_wp_error( $data ) ) { |
||
| 243 | include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
||
| 244 | $data = plugins_api( 'plugin_information', array( |
||
| 245 | 'slug' => 'jetpack', |
||
| 246 | 'is_ssl' => is_ssl(), |
||
| 247 | 'fields' => array( |
||
| 248 | 'banners' => true, |
||
| 249 | 'reviews' => true, |
||
| 250 | 'active_installs' => true, |
||
| 251 | 'versions' => false, |
||
| 252 | 'sections' => false, |
||
| 253 | ), |
||
| 254 | ) ); |
||
| 255 | set_transient( 'jetpack_plugin_data', $data, DAY_IN_SECONDS ); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $data; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Create a list with additional features for those we don't have a module, like Akismet. |
||
| 263 | * |
||
| 264 | * @since 7.1.0 |
||
| 265 | * |
||
| 266 | * @return array List of features. |
||
| 267 | */ |
||
| 268 | public function get_extra_features() { |
||
| 269 | return array( |
||
| 270 | 'akismet' => array( |
||
| 271 | 'name' => 'Akismet', |
||
| 272 | 'search_terms' => 'akismet, anti-spam, antispam, comments, spam, spam protection, form spam, captcha, no captcha, nocaptcha, recaptcha, phising, google', |
||
| 273 | 'short_description' => esc_html__( 'Keep your visitors and search engines happy by stopping comment and contact form spam with Akismet.', 'jetpack' ), |
||
| 274 | 'requires_connection' => true, |
||
| 275 | 'module' => 'akismet', |
||
| 276 | 'sort' => '16', |
||
| 277 | 'learn_more_button' => 'https://jetpack.com/features/security/spam-filtering/', |
||
| 278 | 'configure_url' => admin_url( 'admin.php?page=akismet-key-config' ), |
||
| 279 | ), |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Intercept the plugins API response and add in an appropriate card for Jetpack |
||
| 285 | */ |
||
| 286 | public function inject_jetpack_module_suggestion( $result, $action, $args ) { |
||
| 287 | // Looks like a search query; it's matching time |
||
| 288 | if ( ! empty( $args->search ) ) { |
||
| 289 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php'; |
||
| 290 | $tracking = new Tracking(); |
||
| 291 | $jetpack_modules_list = array_intersect_key( |
||
| 292 | array_merge( $this->get_extra_features(), Jetpack_Admin::init()->get_modules() ), |
||
| 293 | array_flip( array( |
||
| 294 | 'contact-form', |
||
| 295 | 'lazy-images', |
||
| 296 | 'monitor', |
||
| 297 | 'photon', |
||
| 298 | 'photon-cdn', |
||
| 299 | 'protect', |
||
| 300 | 'publicize', |
||
| 301 | 'related-posts', |
||
| 302 | 'sharedaddy', |
||
| 303 | 'akismet', |
||
| 304 | 'vaultpress', |
||
| 305 | 'videopress', |
||
| 306 | 'search', |
||
| 307 | ) ) |
||
| 308 | ); |
||
| 309 | uasort( $jetpack_modules_list, array( $this, 'by_sorting_option' ) ); |
||
| 310 | |||
| 311 | // Record event when user searches for a term over 3 chars (less than 3 is not very useful.) |
||
| 312 | if ( strlen( $args->search ) >= 3 ) { |
||
| 313 | $tracking->record_user_event( 'wpa_plugin_search_term', array( 'search_term' => $args->search ) ); |
||
| 314 | } |
||
| 315 | |||
| 316 | // Lowercase, trim, remove punctuation/special chars, decode url, remove 'jetpack' |
||
| 317 | $normalized_term = $this->sanitize_search_term( $args->search ); |
||
| 318 | |||
| 319 | $matching_module = null; |
||
| 320 | |||
| 321 | // Try to match a passed search term with module's search terms |
||
| 322 | foreach ( $jetpack_modules_list as $module_slug => $module_opts ) { |
||
| 323 | /* |
||
| 324 | * Does the site's current plan support the feature? |
||
| 325 | * We don't use Jetpack_Plan::supports() here because |
||
| 326 | * that check always returns Akismet as supported, |
||
| 327 | * since Akismet has a free version. |
||
| 328 | */ |
||
| 329 | $current_plan = Jetpack_Plan::get(); |
||
| 330 | $is_supported_by_plan = in_array( $module_slug, $current_plan['supports'], true ); |
||
| 331 | |||
| 332 | if ( |
||
| 333 | false !== stripos( $module_opts['search_terms'] . ', ' . $module_opts['name'], $normalized_term ) |
||
| 334 | && $is_supported_by_plan |
||
| 335 | ) { |
||
| 336 | $matching_module = $module_slug; |
||
| 337 | break; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | if ( isset( $matching_module ) && $this->should_display_hint( $matching_module ) ) { |
||
| 342 | // Record event when a matching feature is found |
||
| 343 | $tracking->record_user_event( 'wpa_plugin_search_match_found', array( 'feature' => $matching_module ) ); |
||
| 344 | |||
| 345 | $inject = (array) self::get_jetpack_plugin_data(); |
||
| 346 | $image_url = plugins_url( 'modules/plugin-search/psh', JETPACK__PLUGIN_FILE ); |
||
| 347 | $overrides = array( |
||
| 348 | 'plugin-search' => true, // Helps to determine if that an injected card. |
||
| 349 | 'name' => sprintf( // Supplement name/description so that they clearly indicate this was added. |
||
| 350 | esc_html_x( 'Jetpack: %s', 'Jetpack: Module Name', 'jetpack' ), |
||
| 351 | $jetpack_modules_list[ $matching_module ]['name'] |
||
| 352 | ), |
||
| 353 | 'short_description' => $jetpack_modules_list[ $matching_module ]['short_description'], |
||
| 354 | 'requires_connection' => (bool) $jetpack_modules_list[ $matching_module ]['requires_connection'], |
||
| 355 | 'slug' => self::$slug, |
||
| 356 | 'version' => JETPACK__VERSION, |
||
| 357 | 'icons' => array( |
||
| 358 | '1x' => "$image_url-128.png", |
||
| 359 | '2x' => "$image_url-256.png", |
||
| 360 | 'svg' => "$image_url.svg", |
||
| 361 | ), |
||
| 362 | ); |
||
| 363 | |||
| 364 | // Splice in the base module data |
||
| 365 | $inject = array_merge( $inject, $jetpack_modules_list[ $matching_module ], $overrides ); |
||
| 366 | |||
| 367 | // Add it to the top of the list |
||
| 368 | $result->plugins = array_filter( $result->plugins, array( $this, 'filter_cards' ) ); |
||
| 369 | array_unshift( $result->plugins, $inject ); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | return $result; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Remove cards for Jetpack plugins since we don't want duplicates. |
||
| 377 | * |
||
| 378 | * @since 7.1.0 |
||
| 379 | * @since 7.2.0 Only remove Jetpack. |
||
| 380 | * @since 7.4.0 Simplify for WordPress 5.1+. |
||
| 381 | * |
||
| 382 | * @param array|object $plugin |
||
| 383 | * |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | function filter_cards( $plugin ) { |
||
| 387 | return ! in_array( $plugin['slug'], array( 'jetpack' ), true ); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Take a raw search query and return something a bit more standardized and |
||
| 392 | * easy to work with. |
||
| 393 | * |
||
| 394 | * @param String $term The raw search term |
||
| 395 | * @return String A simplified/sanitized version. |
||
| 396 | */ |
||
| 397 | private function sanitize_search_term( $term ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Callback function to sort the array of modules by the sort option. |
||
| 411 | */ |
||
| 412 | private function by_sorting_option( $m1, $m2 ) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Builds a URL to purchase and upgrade inserting the site fragment and the affiliate code if it exists. |
||
| 418 | * |
||
| 419 | * @param string $feature Module slug (or forged one for extra features). |
||
| 420 | * |
||
| 421 | * @since 7.1.0 |
||
| 422 | * |
||
| 423 | * @return string URL to upgrade. |
||
| 424 | */ |
||
| 425 | private function get_upgrade_url( $feature ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Modify the URL to the feature settings, for example Publicize. |
||
| 435 | * Sharing is included here because while we still have a page in WP Admin, |
||
| 436 | * we prefer to send users to Calypso. |
||
| 437 | * |
||
| 438 | * @param string $feature |
||
| 439 | * @param string $configure_url |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | * @since 7.1.0 |
||
| 443 | * |
||
| 444 | */ |
||
| 445 | private function get_configure_url( $feature, $configure_url ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Put some more appropriate links on our custom result cards. |
||
| 467 | */ |
||
| 468 | public function insert_module_related_links( $links, $plugin ) { |
||
| 544 | |||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Master control that checks if Plugin search hints is active. |
||
| 549 | * |
||
| 550 | * @since 7.1.1 |
||
| 551 | * |
||
| 604 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.