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 ) { | ||
| 69 | |||
| 70 | /* | ||
| 71 | * Present Manage just in time activation msg on update-core.php | ||
| 72 | * | ||
| 73 | */ | ||
| 74 | 	function manage_msg() { | ||
| 75 | 		if ( current_user_can( 'jetpack_manage_modules' ) ) { | ||
| 76 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); | ||
| 77 | $manage_active = Jetpack::is_module_active( 'manage' ); | ||
| 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() { | ||
| 106 | 		if ( current_user_can( 'jetpack_manage_modules' ) ) { ?> | ||
| 107 | <div class="jp-jitm"> | ||
| 108 | <a href="#" data-module="photon" class="dismiss"><span class="genericon genericon-close"></span></a> | ||
| 109 | <div class="jp-emblem"> | ||
| 110 | <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"> | ||
| 111 | <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"/> | ||
| 112 | </svg> | ||
| 113 | </div> | ||
| 114 | <p class="msg"> | ||
| 115 | <?php _e( 'Speed up your photos and save bandwidth costs by using a free content delivery network.', 'jetpack' ); ?> | ||
| 116 | </p> | ||
| 117 | <p> | ||
| 118 | <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> | ||
| 119 | </p> | ||
| 120 | </div> | ||
| 121 | <?php | ||
| 122 | //jitm is being viewed, track it | ||
| 123 | $jetpack = Jetpack::init(); | ||
| 124 | $jetpack->stat( 'jitm', 'photon-viewed-' . JETPACK__VERSION ); | ||
| 125 | $jetpack->do_stats( 'server_side' ); | ||
| 126 | } | ||
| 127 | } | ||
| 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 | $manage_pi_dismissed = isset( self::$jetpack_hide_jitm['manage-pi'] ); | ||
| 139 | |||
| 140 | if ( ! $manage_active || ! $manage_pi_dismissed ) : | ||
| 141 | ?> | ||
| 142 | <div class="jp-jitm"> | ||
| 143 | <a href="#" data-module="manage-pi" class="dismiss"><span class="genericon genericon-close"></span></a> | ||
| 144 | <div class="jp-emblem"> | ||
| 145 | <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"> | ||
| 146 | <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"/> | ||
| 147 | </svg> | ||
| 148 | </div> | ||
| 149 | <?php if ( ! $manage_active ) : ?> | ||
| 150 | <p class="msg"> | ||
| 151 | <?php _e( 'Save time with automated plugin updates.', 'jetpack' ); ?> | ||
| 152 | </p> | ||
| 153 | <p> | ||
| 154 | <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> | ||
| 155 | </p> | ||
| 156 | <?php elseif ( $manage_active ) : ?> | ||
| 157 | <p> | ||
| 158 | <?php esc_html_e( 'Save time with auto updates on WordPress.com', 'jetpack' ); ?> | ||
| 159 | </p> | ||
| 160 | <?php endif; // manage inactive ?> | ||
| 161 | <?php if ( ! $manage_pi_dismissed ) : ?> | ||
| 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 esc_html_e( 'Enable auto-updates on WordPress.com', 'jetpack' ); ?></a> | ||
| 164 | </p> | ||
| 165 | <?php endif; // manage-pi inactive ?> | ||
| 166 | </div> | ||
| 167 | <?php | ||
| 168 | //jitm is being viewed, track it | ||
| 169 | $jetpack = Jetpack::init(); | ||
| 170 | $jetpack->stat( 'jitm', 'manage-pi-viewed-' . JETPACK__VERSION ); | ||
| 171 | $jetpack->do_stats( 'server_side' ); | ||
| 172 | endif; // manage or manage-pi inactive | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Display message in editor prompting user to compose entry in WordPress.com. | ||
| 178 | * | ||
| 179 | * @since 3.8.2 | ||
| 180 | */ | ||
| 181 | 	function editor_msg() { | ||
| 182 | global $typenow; | ||
| 183 | 		if ( current_user_can( 'jetpack_manage_modules' ) && current_user_can( 'manage_options' ) ) { | ||
| 184 | $normalized_site_url = Jetpack::build_raw_urls( get_home_url() ); | ||
| 185 | $editor_dismissed = isset( self::$jetpack_hide_jitm['editor'] ); | ||
| 186 | if ( ! $editor_dismissed ) : | ||
| 187 | ?> | ||
| 188 | <div class="jp-jitm"> | ||
| 189 | <a href="#" data-module="editor" class="dismiss"><span class="genericon genericon-close"></span></a> | ||
| 190 | <div class="jp-emblem"> | ||
| 191 | <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"> | ||
| 192 | <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"/> | ||
| 193 | </svg> | ||
| 194 | </div> | ||
| 195 | <p class="msg"> | ||
| 196 | <?php esc_html_e( 'Try the brand new editor.', 'jetpack' ); ?> | ||
| 197 | </p> | ||
| 198 | <p> | ||
| 199 | <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> | ||
| 200 | </p> | ||
| 201 | </div> | ||
| 202 | <?php | ||
| 203 | //jitm is being viewed, track it | ||
| 204 | $jetpack = Jetpack::init(); | ||
| 205 | $jetpack->stat( 'jitm', 'editor-viewed-' . JETPACK__VERSION ); | ||
| 206 | $jetpack->do_stats( 'server_side' ); | ||
| 207 | endif; // manage or editor inactive | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | /* | ||
| 212 | * Function to enqueue jitm css and js | ||
| 213 | */ | ||
| 214 | 	function jitm_enqueue_files( $hook ) { | ||
| 245 | } | ||
| 246 | /** | ||
| 247 | * Filter to turn off all just in time messages | ||
| 248 | * | ||
| 249 | * @since 3.7.0 | ||
| 255 | } |