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 = array(); |
||
| 21 | |||
| 22 | static function init() { |
||
| 29 | |||
| 30 | private function __construct() { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Prepare actions according to screen and post type. |
||
| 39 | * |
||
| 40 | * @since 3.8.2 |
||
| 41 | * |
||
| 42 | * @param object $screen |
||
| 43 | */ |
||
| 44 | function prepare_jitms( $screen ) { |
||
| 45 | global $pagenow; |
||
| 46 | // Only show auto update JITM if auto updates are allowed in this installation |
||
| 47 | $auto_updates_disabled = defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED; |
||
| 48 | // The option returns false when nothing was dismissed |
||
| 49 | self::$jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
| 50 | // so if it's not an array, it means no JITM was dismissed |
||
| 51 | if ( ! is_array( self::$jetpack_hide_jitm ) ) { |
||
| 52 | if ( 'media-new.php' == $pagenow && ! Jetpack::is_module_active( 'photon' ) ) { |
||
| 53 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 54 | add_action( 'post-plupload-upload-ui', array( $this, 'photon_msg' ) ); |
||
| 55 | } |
||
| 56 | else if ( 'update-core.php' == $pagenow && ! $auto_updates_disabled && ! Jetpack::is_module_active( 'manage' ) ) { |
||
| 57 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 58 | add_action( 'admin_notices', array( $this, 'manage_msg' ) ); |
||
| 59 | } |
||
| 60 | elseif ( 'plugins.php' == $pagenow && ! $auto_updates_disabled && isset( $_GET['activate'] ) && 'true' === $_GET['activate'] ) { |
||
| 61 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 62 | add_action( 'pre_current_active_plugins', array( $this, 'manage_pi_msg' ) ); |
||
| 63 | } |
||
| 64 | elseif ( 'post-new.php' == $pagenow && in_array( $screen->post_type, array( 'post', 'page' ) ) ) { |
||
| 65 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 66 | add_action( 'admin_notices', array( $this, 'editor_msg' ) ); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /* |
||
| 72 | * Present Manage just in time activation msg on update-core.php |
||
| 73 | * |
||
| 74 | */ |
||
| 75 | function manage_msg() { |
||
| 76 | if ( current_user_can( 'jetpack_manage_modules' ) ) { |
||
| 77 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 78 | ?> |
||
| 79 | <div class="jp-jitm"> |
||
| 80 | <a href="#" data-module="manage" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 81 | <div class="jp-emblem"> |
||
| 82 | <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"> |
||
| 83 | <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"/> |
||
| 84 | </svg> |
||
| 85 | </div> |
||
| 86 | <p class="msg"> |
||
| 87 | <?php _e( 'Reduce security risks with automated plugin updates.', 'jetpack' ); ?> |
||
| 88 | </p> |
||
| 89 | <p> |
||
| 90 | <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' ) ) { echo 'hide'; } ?>"><?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' ) ) { echo 'hide'; } ?>"><?php esc_html_e( 'Go to WordPress.com', 'jetpack' ); ?></a> |
||
| 91 | </p> |
||
| 92 | </div> |
||
| 93 | <?php |
||
| 94 | //jitm is being viewed, track it |
||
| 95 | $jetpack = Jetpack::init(); |
||
| 96 | $jetpack->stat( 'jitm', 'manage-viewed-' . JETPACK__VERSION ); |
||
| 97 | $jetpack->do_stats( 'server_side' ); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /* |
||
| 102 | * Present Photon just in time activation msg |
||
| 103 | * |
||
| 104 | */ |
||
| 105 | function photon_msg() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Display message prompting user to enable auto-updates in WordPress.com. |
||
| 131 | * |
||
| 132 | * @since 3.8.2 |
||
| 133 | */ |
||
| 134 | function manage_pi_msg() { |
||
| 135 | if ( current_user_can( 'jetpack_manage_modules' ) ) { |
||
| 136 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 137 | $manage_active = Jetpack::is_module_active( 'manage' ); |
||
| 138 | /** This filter is documented in wp-admin/includes/class-wp-upgrader.php */ |
||
| 139 | /*+ TODO: pass correct second parameter to filter */ |
||
| 140 | $auto_update_plugin = apply_filters( 'auto_update_plugin', true, null ); |
||
| 141 | if ( ! $manage_active && $auto_update_plugin ) : |
||
| 142 | ?> |
||
| 143 | <div class="jp-jitm"> |
||
| 144 | <a href="#" data-module="manage-pi" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 145 | <div class="jp-emblem"> |
||
| 146 | <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"> |
||
| 147 | <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"/> |
||
| 148 | </svg> |
||
| 149 | </div> |
||
| 150 | <?php if ( ! $manage_active ) : ?> |
||
| 151 | <p class="msg"> |
||
| 152 | <?php _e( 'Save time with automated plugin updates.', 'jetpack' ); ?> |
||
| 153 | </p> |
||
| 154 | <p> |
||
| 155 | <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> |
||
| 156 | </p> |
||
| 157 | <?php elseif ( $manage_active ) : ?> |
||
| 158 | <p> |
||
| 159 | <?php esc_html_e( 'Save time with auto updates on WordPress.com', 'jetpack' ); ?> |
||
| 160 | </p> |
||
| 161 | <?php endif; // manage inactive ?> |
||
| 162 | <p class="show-after-enable <?php echo $manage_active ? '' : 'hide' ; ?>"> |
||
| 163 | <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> |
||
| 164 | </p> |
||
| 165 | </div> |
||
| 166 | <?php |
||
| 167 | //jitm is being viewed, track it |
||
| 168 | $jetpack = Jetpack::init(); |
||
| 169 | $jetpack->stat( 'jitm', 'manage-pi-viewed-' . JETPACK__VERSION ); |
||
| 170 | $jetpack->do_stats( 'server_side' ); |
||
| 171 | endif; // manage inactive |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Display message in editor prompting user to compose entry in WordPress.com. |
||
| 177 | * |
||
| 178 | * @since 3.8.2 |
||
| 179 | */ |
||
| 180 | function editor_msg() { |
||
| 181 | global $typenow; |
||
| 182 | if ( current_user_can( 'jetpack_manage_modules' ) && current_user_can( 'manage_options' ) ) { |
||
| 183 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); |
||
| 184 | $editor_dismissed = isset( self::$jetpack_hide_jitm['editor'] ); |
||
| 185 | if ( ! $editor_dismissed ) : |
||
| 186 | ?> |
||
| 187 | <div class="jp-jitm"> |
||
| 188 | <a href="#" data-module="editor" class="dismiss"><span class="genericon genericon-close"></span></a> |
||
| 189 | <div class="jp-emblem"> |
||
| 190 | <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"> |
||
| 191 | <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"/> |
||
| 192 | </svg> |
||
| 193 | </div> |
||
| 194 | <p class="msg"> |
||
| 195 | <?php esc_html_e( 'Try the brand new editor.', 'jetpack' ); ?> |
||
| 196 | </p> |
||
| 197 | <p> |
||
| 198 | <a href="<?php echo esc_url( 'https://wordpress.com/' . $typenow . '/' . $normalized_site_url ); ?>" target="_blank" title="<?php esc_attr_e( 'Write on WordPress.com', 'jetpack' ); ?>" data-module="editor" class="button button-jetpack launch show-after-enable"><?php esc_html_e( 'Write on WordPress.com', 'jetpack' ); ?></a> |
||
| 199 | </p> |
||
| 200 | </div> |
||
| 201 | <?php |
||
| 202 | //jitm is being viewed, track it |
||
| 203 | $jetpack = Jetpack::init(); |
||
| 204 | $jetpack->stat( 'jitm', 'editor-viewed-' . JETPACK__VERSION ); |
||
| 205 | $jetpack->do_stats( 'server_side' ); |
||
| 206 | endif; // manage or editor inactive |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /* |
||
| 211 | * Function to enqueue jitm css and js |
||
| 212 | */ |
||
| 213 | function jitm_enqueue_files( $hook ) { |
||
| 244 | } |
||
| 245 | /** |
||
| 246 | * Filter to turn off all just in time messages |
||
| 247 | * |
||
| 248 | * @since 3.7.0 |
||
| 254 | } |