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 { |
||
|
1 ignored issue
–
show
|
|||
| 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 | global $pagenow; |
||
| 58 | |||
| 59 | // Only show auto update JITM if auto updates are allowed in this installation |
||
| 60 | $possible_reasons_for_failure = Jetpack_Autoupdate::get_possible_failures(); |
||
| 61 | self::$auto_updates_allowed = empty( $possible_reasons_for_failure ); |
||
| 62 | |||
| 63 | if ( 'media-new.php' == $pagenow && ! Jetpack::is_module_active( 'photon' ) ) { |
||
| 64 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 65 | add_action( 'post-plupload-upload-ui', array( $this, 'photon_msg' ) ); |
||
| 66 | } |
||
| 67 | elseif ( 'post-new.php' == $pagenow && in_array( $screen->post_type, array( 'post', 'page' ) ) ) { |
||
| 68 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 69 | add_action( 'admin_notices', array( $this, 'editor_msg' ) ); |
||
| 70 | } |
||
| 71 | elseif ( self::$auto_updates_allowed ) { |
||
| 72 | if ( 'update-core.php' == $pagenow && ! Jetpack::is_module_active( 'manage' ) ) { |
||
| 73 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 74 | add_action( 'admin_notices', array( $this, 'manage_msg' ) ); |
||
| 75 | } |
||
| 76 | elseif ( 'plugins.php' == $pagenow ) { |
||
| 77 | if ( ( isset( $_GET['activate'] ) && 'true' === $_GET['activate'] ) || ( isset( $_GET['activate-multi'] ) && 'true' === $_GET['activate-multi'] ) ) { |
||
| 78 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 79 | add_action( 'pre_current_active_plugins', array( $this, 'manage_pi_msg' ) ); |
||
| 80 | } else { |
||
| 81 | |||
| 82 | // Save plugins that are activated. This is used when one or more plugins are activated to know |
||
| 83 | // what was activated and use it in Jetpack_JITM::manage_pi_msg() before deleting the option. |
||
| 84 | $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' ); |
||
| 85 | $action = $wp_list_table->current_action(); |
||
| 86 | if ( $action && ( 'activate' == $action || 'activate-selected' == $action ) ) { |
||
| 87 | update_option( 'jetpack_temp_active_plugins_before', get_option( 'active_plugins', array() ) ); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /* |
||
| 95 | * Present Manage just in time activation msg on update-core.php |
||
| 96 | * |
||
| 97 | */ |
||
| 98 | function manage_msg() { |
||
| 99 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 100 | ?> |
||
| 101 | <div class="jp-jitm"> |
||
| 102 | <a href="#" data-module="manage" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 103 | |||
| 104 | <div class="jp-emblem"> |
||
| 105 | <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve"> |
||
| 106 | <path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z" /> |
||
| 107 | </svg> |
||
| 108 | </div> |
||
| 109 | <p class="msg"> |
||
| 110 | <?php _e( 'Reduce security risks with automated plugin updates.', 'jetpack' ); ?> |
||
| 111 | </p> |
||
| 112 | |||
| 113 | <p> |
||
| 114 | <img class="j-spinner hide" src="<?php echo esc_url( includes_url( 'images/spinner-2x.gif' ) ); ?>" alt="Loading ..." /><a href="#" data-module="manage" class="activate button <?php if ( Jetpack::is_module_active( 'manage' ) ) { |
||
| 115 | echo 'hide'; |
||
| 116 | } ?>"><?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' ) ) { |
||
| 117 | echo 'hide'; |
||
| 118 | } ?>"><?php esc_html_e( 'Go to WordPress.com', 'jetpack' ); ?></a> |
||
| 119 | </p> |
||
| 120 | </div> |
||
| 121 | <?php |
||
| 122 | //jitm is being viewed, track it |
||
| 123 | $jetpack = Jetpack::init(); |
||
| 124 | $jetpack->stat( 'jitm', 'manage-viewed-' . JETPACK__VERSION ); |
||
| 125 | $jetpack->do_stats( 'server_side' ); |
||
| 126 | } |
||
| 127 | |||
| 128 | /* |
||
| 129 | * Present Photon just in time activation msg |
||
| 130 | * |
||
| 131 | */ |
||
| 132 | function photon_msg() { |
||
| 133 | ?> |
||
| 134 | <div class="jp-jitm"> |
||
| 135 | <a href="#" data-module="photon" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 136 | |||
| 137 | <div class="jp-emblem"> |
||
| 138 | <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve"> |
||
| 139 | <path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z" /> |
||
| 140 | </svg> |
||
| 141 | </div> |
||
| 142 | <p class="msg"> |
||
| 143 | <?php _e( 'Speed up your photos and save bandwidth costs by using a free content delivery network.', 'jetpack' ); ?> |
||
| 144 | </p> |
||
| 145 | |||
| 146 | <p> |
||
| 147 | <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="Loading ..." /><a href="#" data-module="photon" class="activate button button-jetpack"><?php esc_html_e( 'Activate Photon', 'jetpack' ); ?></a> |
||
| 148 | </p> |
||
| 149 | </div> |
||
| 150 | <?php |
||
| 151 | //jitm is being viewed, track it |
||
| 152 | $jetpack = Jetpack::init(); |
||
| 153 | $jetpack->stat( 'jitm', 'photon-viewed-' . JETPACK__VERSION ); |
||
| 154 | $jetpack->do_stats( 'server_side' ); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Display message prompting user to enable auto-updates in WordPress.com. |
||
| 159 | * |
||
| 160 | * @since 3.8.2 |
||
| 161 | */ |
||
| 162 | function manage_pi_msg() { |
||
| 163 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 164 | $manage_active = Jetpack::is_module_active( 'manage' ); |
||
| 165 | |||
| 166 | // Check if plugin has auto update already enabled in WordPress.com and don't show JITM in such case. |
||
| 167 | $active_before = get_option( 'jetpack_temp_active_plugins_before', array() ); |
||
| 168 | delete_option( 'jetpack_temp_active_plugins_before' ); |
||
| 169 | $active_now = get_option( 'active_plugins', array() ); |
||
| 170 | $activated = array_diff( $active_now, $active_before ); |
||
| 171 | $auto_update_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); |
||
| 172 | $plugin_auto_update_disabled = false; |
||
| 173 | foreach ( $activated as $plugin ) { |
||
| 174 | if ( ! in_array( $plugin, $auto_update_plugin_list ) ) { |
||
| 175 | |||
| 176 | // Plugin doesn't have auto updates enabled in WordPress.com yet. |
||
| 177 | $plugin_auto_update_disabled = true; |
||
| 178 | |||
| 179 | // We don't need to continue checking, it's ok to show JITM for this plugin. |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | // Check if the activated plugin is in the WordPress.org repository |
||
| 185 | $plugin_can_auto_update = false; |
||
| 186 | $plugin_updates = get_site_transient( 'update_plugins' ); |
||
| 187 | if ( false === $plugin_updates ) { |
||
| 188 | |||
| 189 | // If update_plugins doesn't exist, display message anyway |
||
| 190 | $plugin_can_auto_update = true; |
||
| 191 | } else { |
||
| 192 | $plugin_updates = array_merge( $plugin_updates->response, $plugin_updates->no_update ); |
||
| 193 | foreach ( $activated as $plugin ) { |
||
| 194 | if ( isset( $plugin_updates[ $plugin ] ) ) { |
||
| 195 | |||
| 196 | // There's at least one plugin set cleared for auto updates |
||
| 197 | $plugin_can_auto_update = true; |
||
| 198 | |||
| 199 | // We don't need to continue checking, it's ok to show JITM for this round. |
||
| 200 | break; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | if ( ! $manage_active && $plugin_auto_update_disabled && $plugin_can_auto_update && self::$auto_updates_allowed ) : |
||
| 206 | ?> |
||
| 207 | <div class="jp-jitm"> |
||
| 208 | <a href="#" data-module="manage-pi" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 209 | |||
| 210 | <div class="jp-emblem"> |
||
| 211 | <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve"> |
||
| 212 | <path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z" /> |
||
| 213 | </svg> |
||
| 214 | </div> |
||
| 215 | <?php if ( ! $manage_active ) : ?> |
||
| 216 | <p class="msg"> |
||
| 217 | <?php _e( 'Save time with automated plugin updates.', 'jetpack' ); ?> |
||
| 218 | </p> |
||
| 219 | <p> |
||
| 220 | <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" data-module-success="<?php esc_attr_e( 'Success!', 'jetpack' ); ?>" class="activate button"><?php esc_html_e( 'Activate remote management', 'jetpack' ); ?></a> |
||
| 221 | </p> |
||
| 222 | <?php elseif ( $manage_active ) : ?> |
||
| 223 | <p> |
||
| 224 | <?php esc_html_e( 'Save time with auto updates on WordPress.com', 'jetpack' ); ?> |
||
| 225 | </p> |
||
| 226 | <?php endif; // manage inactive |
||
| 227 | ?> |
||
| 228 | <p class="show-after-enable <?php echo $manage_active ? '' : 'hide'; ?>"> |
||
| 229 | <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 enable auto-updates for plugins', 'jetpack' ); ?>" data-module="manage-pi" class="button button-jetpack launch show-after-enable"><?php if ( ! $manage_active ) : ?><?php esc_html_e( 'Enable auto-updates on WordPress.com', 'jetpack' ); ?><?php elseif ( $manage_active ) : ?><?php esc_html_e( 'Enable auto-updates', 'jetpack' ); ?><?php endif; // manage inactive ?></a> |
||
| 230 | </p> |
||
| 231 | </div> |
||
| 232 | <?php |
||
| 233 | //jitm is being viewed, track it |
||
| 234 | $jetpack = Jetpack::init(); |
||
| 235 | $jetpack->stat( 'jitm', 'manage-pi-viewed-' . JETPACK__VERSION ); |
||
| 236 | $jetpack->do_stats( 'server_side' ); |
||
| 237 | endif; // manage inactive |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Display message in editor prompting user to compose entry in WordPress.com. |
||
| 242 | * |
||
| 243 | * @since 3.8.2 |
||
| 244 | */ |
||
| 245 | function editor_msg() { |
||
| 274 | |||
| 275 | /* |
||
| 276 | * Function to enqueue jitm css and js |
||
| 277 | */ |
||
| 278 | function jitm_enqueue_files( $hook ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Check if a JITM was dismissed or not. Currently, dismissing one JITM will dismiss all of them. |
||
| 312 | * |
||
| 313 | * @since 3.8.2 |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | function is_jitm_dismissed() { |
||
| 327 | } |
||
| 328 | /** |
||
| 337 | } |