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_JITM 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_JITM, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Jetpack_JITM { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var Jetpack_JITM |
||
| 12 | **/ |
||
| 13 | private static $instance = null; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Get user dismissed messages. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private static $jetpack_hide_jitm = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Whether plugin auto updates are allowed in this WordPress installation or not. |
||
| 24 | * |
||
| 25 | * @var bool |
||
| 26 | */ |
||
| 27 | private static $auto_updates_allowed = false; |
||
| 28 | |||
| 29 | static function init() { |
||
| 36 | |||
| 37 | private function __construct() { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Prepare actions according to screen and post type. |
||
| 46 | * |
||
| 47 | * @since 3.8.2 |
||
| 48 | * |
||
| 49 | * @uses Jetpack_Autoupdate::get_possible_failures() |
||
| 50 | * |
||
| 51 | * @param object $screen |
||
| 52 | */ |
||
| 53 | function prepare_jitms( $screen ) { |
||
| 54 | if ( ! current_user_can( 'jetpack_manage_modules' ) ) { |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | if ( 'edit-comments' == $screen->base && ! Jetpack::is_plugin_active( 'akismet/akismet.php' ) ) { |
||
| 59 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 60 | add_action( 'admin_notices', array( $this, 'akismet_msg' ) ); |
||
| 61 | } |
||
| 62 | elseif ( |
||
| 63 | 'post' == $screen->base |
||
| 64 | && ( isset( $_GET['message'] ) && 6 == $_GET['message'] ) |
||
| 65 | && ! Jetpack::is_plugin_active( 'vaultpress/vaultpress.php' ) |
||
| 66 | ) { |
||
| 67 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 68 | add_action( 'edit_form_top', array( $this, 'backups_after_publish_msg' ) ); |
||
| 69 | } |
||
| 70 | elseif ( 'update-core' == $screen->base && ! Jetpack::is_plugin_active( 'vaultpress/vaultpress.php' ) ) { |
||
| 71 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 72 | add_action( 'admin_notices', array( $this, 'backups_updates_msg' ) ); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /* |
||
| 77 | * Present Manage just in time activation msg on update-core.php |
||
| 78 | * |
||
| 79 | */ |
||
| 80 | function manage_msg() { |
||
| 81 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 82 | ?> |
||
| 83 | <div class="jp-jitm"> |
||
| 84 | <a href="#" data-module="manage" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 85 | |||
| 86 | <div class="jp-emblem"> |
||
| 87 | <?php echo self::get_jp_emblem(); ?> |
||
| 88 | </div> |
||
| 89 | <p class="msg"> |
||
| 90 | <?php esc_html_e( 'Reduce security risks with automated plugin updates.', 'jetpack' ); ?> |
||
| 91 | </p> |
||
| 92 | |||
| 93 | <p> |
||
| 94 | <img class="j-spinner hide" src="<?php echo esc_url( includes_url( 'images/spinner-2x.gif' ) ); ?>" alt="<?php echo esc_attr__( 'Loading...', 'jetpack' ); ?>" /><a href="#" data-module="manage" class="activate button <?php if ( Jetpack::is_module_active( 'manage' ) ) { |
||
| 95 | echo 'hide'; |
||
| 96 | } ?>"><?php esc_html_e( 'Activate Now', 'jetpack' ); ?></a><a href="<?php echo esc_url( 'https://wordpress.com/plugins/' . $normalized_site_url ); ?>" target="_blank" title="<?php esc_attr_e( 'Go to WordPress.com to try these features', 'jetpack' ); ?>" id="jetpack-wordpressdotcom" class="button button-jetpack <?php if ( ! Jetpack::is_module_active( 'manage' ) ) { |
||
| 97 | echo 'hide'; |
||
| 98 | } ?>"><?php esc_html_e( 'Go to WordPress.com', 'jetpack' ); ?></a> |
||
| 99 | </p> |
||
| 100 | </div> |
||
| 101 | <?php |
||
| 102 | //jitm is being viewed, track it |
||
| 103 | $jetpack = Jetpack::init(); |
||
| 104 | $jetpack->stat( 'jitm', 'manage-viewed-' . JETPACK__VERSION ); |
||
| 105 | $jetpack->do_stats( 'server_side' ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /* |
||
| 109 | * Present Photon just in time activation msg |
||
| 110 | * |
||
| 111 | */ |
||
| 112 | function photon_msg() { |
||
| 113 | ?> |
||
| 114 | <div class="jp-jitm"> |
||
| 115 | <a href="#" data-module="photon" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 116 | |||
| 117 | <div class="jp-emblem"> |
||
| 118 | <?php echo self::get_jp_emblem(); ?> |
||
| 119 | </div> |
||
| 120 | <p class="msg"> |
||
| 121 | <?php esc_html_e( 'Speed up your photos and save bandwidth costs by using a free content delivery network.', 'jetpack' ); ?> |
||
| 122 | </p> |
||
| 123 | |||
| 124 | <p> |
||
| 125 | <img class="j-spinner hide" style="margin-top: 13px;" width="17" height="17" src="<?php echo esc_url( includes_url( 'images/spinner-2x.gif' ) ); ?>" alt="<?php echo esc_attr__( 'Loading...', 'jetpack' ); ?>" /><a href="#" data-module="photon" class="activate button button-jetpack"><?php esc_html_e( 'Activate Photon', 'jetpack' ); ?></a> |
||
| 126 | </p> |
||
| 127 | </div> |
||
| 128 | <?php |
||
| 129 | //jitm is being viewed, track it |
||
| 130 | $jetpack = Jetpack::init(); |
||
| 131 | $jetpack->stat( 'jitm', 'photon-viewed-' . JETPACK__VERSION ); |
||
| 132 | $jetpack->do_stats( 'server_side' ); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Display Photon JITM template in Media Library after user uploads an image. |
||
| 137 | * |
||
| 138 | * @since 3.9.0 |
||
| 139 | */ |
||
| 140 | function photon_tmpl() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Display message prompting user to enable auto-updates in WordPress.com. |
||
| 163 | * |
||
| 164 | * @since 3.8.2 |
||
| 165 | */ |
||
| 166 | function manage_pi_msg() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Display message in editor prompting user to compose entry in WordPress.com. |
||
| 244 | * |
||
| 245 | * @since 3.8.2 |
||
| 246 | */ |
||
| 247 | function editor_msg() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Display message in editor prompting user to enable stats. |
||
| 277 | * |
||
| 278 | * @since 3.9.0 |
||
| 279 | */ |
||
| 280 | function stats_msg() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Display JITM in Updates screen prompting user to enable Backups. |
||
| 312 | * |
||
| 313 | * @since 3.9.5 |
||
| 314 | */ |
||
| 315 | View Code Duplication | function backups_updates_msg() { |
|
| 316 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 317 | $url = 'https://wordpress.com/plans/' . $normalized_site_url; |
||
| 318 | $jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => 'vaultpress' ) ); |
||
| 319 | ?> |
||
| 320 | <div class="jp-jitm" data-track="vaultpress-updates" data-stats_url="<?php echo esc_url( $jitm_stats_url ); ?>"> |
||
| 321 | <a href="#" data-module="vaultpress" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 322 | |||
| 323 | <div class="jp-emblem"> |
||
| 324 | <?php echo self::get_jp_emblem(); ?> |
||
| 325 | </div> |
||
| 326 | <p class="msg"> |
||
| 327 | <?php esc_html_e( 'Backups are recommended to protect your site before you make any changes.', 'jetpack' ); ?> |
||
| 328 | </p> |
||
| 329 | <p> |
||
| 330 | <a href="<?php echo esc_url( $url ); ?>" target="_blank" title="<?php esc_attr_e( 'Enable VaultPress Backups', 'jetpack' ); ?>" data-module="vaultpress" data-jptracks-name="nudge_click" data-jptracks-prop="jitm-vault" class="button button-jetpack launch jptracks"><?php esc_html_e( 'Enable VaultPress Backups', 'jetpack' ); ?></a> |
||
| 331 | </p> |
||
| 332 | </div> |
||
| 333 | <?php |
||
| 334 | //jitm is being viewed, track it |
||
| 335 | $jetpack = Jetpack::init(); |
||
| 336 | $jetpack->stat( 'jitm', 'vaultpress-updates-viewed-' . JETPACK__VERSION ); |
||
| 337 | $jetpack->do_stats( 'server_side' ); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Display JITM in Comments screen prompting user to enable Akismet. |
||
| 342 | * |
||
| 343 | * @since 3.9.5 |
||
| 344 | */ |
||
| 345 | View Code Duplication | function akismet_msg() { |
|
| 346 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 347 | $url = 'https://wordpress.com/plans/' . $normalized_site_url; |
||
| 348 | $jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => 'akismet' ) ); |
||
| 349 | ?> |
||
| 350 | <div class="jp-jitm" data-stats_url="<?php echo esc_url( $jitm_stats_url ); ?>"> |
||
| 351 | <a href="#" data-module="akismet" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 352 | |||
| 353 | <div class="jp-emblem"> |
||
| 354 | <?php echo self::get_jp_emblem(); ?> |
||
| 355 | </div> |
||
| 356 | <p class="msg"> |
||
| 357 | <?php esc_html_e( "Spam affects your site's legitimacy, protect your site with Akismet.", 'jetpack' ); ?> |
||
| 358 | </p> |
||
| 359 | <p> |
||
| 360 | <a href="<?php echo esc_url( $url ); ?>" target="_blank" title="<?php esc_attr_e( 'Automate Spam Blocking', 'jetpack' ); ?>" data-module="akismet" data-jptracks-name="nudge_click" data-jptracks-prop="jitm-akismet" class="button button-jetpack launch jptracks"><?php esc_html_e( 'Automate Spam Blocking', 'jetpack' ); ?></a> |
||
| 361 | </p> |
||
| 362 | </div> |
||
| 363 | <?php |
||
| 364 | //jitm is being viewed, track it |
||
| 365 | $jetpack = Jetpack::init(); |
||
| 366 | $jetpack->stat( 'jitm', 'akismet-viewed-' . JETPACK__VERSION ); |
||
| 367 | $jetpack->do_stats( 'server_side' ); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Display JITM after a post is published prompting user to enable Backups. |
||
| 372 | * |
||
| 373 | * @since 3.9.5 |
||
| 374 | */ |
||
| 375 | View Code Duplication | function backups_after_publish_msg() { |
|
| 376 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 377 | $url = 'https://wordpress.com/plans/' . $normalized_site_url; |
||
| 378 | $jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => 'vaultpress' ) ); |
||
| 379 | ?> |
||
| 380 | <div class="jp-jitm" data-track="vaultpress-publish" data-stats_url="<?php echo esc_url( $jitm_stats_url ); ?>"> |
||
| 381 | <a href="#" data-module="vaultpress" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 382 | |||
| 383 | <div class="jp-emblem"> |
||
| 384 | <?php echo self::get_jp_emblem(); ?> |
||
| 385 | </div> |
||
| 386 | <p class="msg"> |
||
| 387 | <?php esc_html_e( "Great job! Now let's make sure your hard work is never lost, backup everything with VaultPress.", 'jetpack' ); ?> |
||
| 388 | </p> |
||
| 389 | <p> |
||
| 390 | <a href="<?php echo esc_url( $url ); ?>" target="_blank" title="<?php esc_attr_e( 'Enable Backups', 'jetpack' ); ?>" data-module="vaultpress" data-jptracks-name="nudge_click" data-jptracks-prop="jitm-vault-post" class="button button-jetpack launch jptracks"><?php esc_html_e( 'Enable Backups', 'jetpack' ); ?></a> |
||
| 391 | </p> |
||
| 392 | </div> |
||
| 393 | <?php |
||
| 394 | //jitm is being viewed, track it |
||
| 395 | $jetpack = Jetpack::init(); |
||
| 396 | $jetpack->stat( 'jitm', 'vaultpress-publish-viewed-' . JETPACK__VERSION ); |
||
| 397 | $jetpack->do_stats( 'server_side' ); |
||
| 398 | } |
||
| 399 | |||
| 400 | /* |
||
| 401 | * Function to enqueue jitm css and js |
||
| 402 | */ |
||
| 403 | function jitm_enqueue_files( $hook ) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Check if a JITM was dismissed or not. Currently, dismissing one JITM will dismiss all of them. |
||
| 441 | * |
||
| 442 | * @since 3.8.2 |
||
| 443 | * |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | function is_jitm_dismissed() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Return string containing the Jetpack logo. |
||
| 459 | * |
||
| 460 | * @since 3.9.0 |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | function get_jp_emblem() { |
||
| 467 | } |
||
| 468 | /** |
||
| 469 | * Filter to turn off all just in time messages |
||
| 470 | * |
||
| 471 | * @since 3.7.0 |
||
| 472 | * |
||
| 473 | * @param bool true Whether to show just in time messages. |
||
| 478 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.