Completed
Push — fix_jitm_dismiss_logic ( fb919b...f406fb )
by
unknown
51:36 queued 42:55
created

Jetpack_JITM   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3
Metric Value
wmc 49
lcom 3
cbo 3
dl 0
loc 310
rs 8.5455

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A __construct() 0 6 3
D prepare_jitms() 0 38 17
B manage_msg() 0 29 3
B photon_msg() 0 24 1
C manage_pi_msg() 0 71 15
B editor_msg() 0 29 3
B jitm_enqueue_files() 0 31 3
A is_jitm_dismissed() 0 8 2

How to fix   Complexity   

Complex Class

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
2
3
/**
4
 * Jetpack just in time messaging through out the admin
5
 *
6
 * @since 3.7.0
7
 */
8
class Jetpack_JITM {
1 ignored issue
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
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() {
30
		if ( is_null( self::$instance ) ) {
31
			self::$instance = new Jetpack_JITM;
32
		}
33
34
		return self::$instance;
35
	}
36
37
	private function __construct() {
38
		if ( ! Jetpack::is_active() || self::is_jitm_dismissed() ) {
39
			return;
40
		}
41
		add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
42
	}
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
		// Only show auto update JITM if auto updates are allowed in this installation
59
		$possible_reasons_for_failure = Jetpack_Autoupdate::get_possible_failures();
60
		self::$auto_updates_allowed = empty( $possible_reasons_for_failure );
61
62
		if ( 'media-new.php' == $pagenow && ! Jetpack::is_module_active( 'photon' ) ) {
63
			add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
64
			add_action( 'post-plupload-upload-ui', array( $this, 'photon_msg' ) );
65
		}
66
		elseif ( 'post-new.php' == $pagenow && in_array( $screen->post_type, array( 'post', 'page' ) ) ) {
67
			add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
68
			add_action( 'admin_notices', array( $this, 'editor_msg' ) );
69
		}
70
		elseif ( self::$auto_updates_allowed ) {
71
			if ( 'update-core.php' == $pagenow && ! Jetpack::is_module_active( 'manage' ) ) {
72
				add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
73
				add_action( 'admin_notices', array( $this, 'manage_msg' ) );
74
			}
75
			elseif ( 'plugins.php' == $pagenow ) {
76
				if ( ( isset( $_GET[ 'activate' ] ) && 'true' === $_GET[ 'activate' ] ) || ( isset( $_GET[ 'activate-multi' ] ) && 'true' === $_GET[ 'activate-multi' ] ) ) {
77
					add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
78
					add_action( 'pre_current_active_plugins', array( $this, 'manage_pi_msg' ) );
79
				} else {
80
					// Save plugins that are activated. This is used when one or more plugins are activated to know
81
					// what was activated and use it in Jetpack_JITM::manage_pi_msg() before deleting the option.
82
					$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
83
					$action = $wp_list_table->current_action();
84
					if ( $action && ( 'activate' == $action || 'activate-selected' == $action ) ) {
85
						update_option( 'jetpack_previously_activated', get_option( 'active_plugins', array() ) );
86
					}
87
				}
88
			}
89
		}
90
	}
91
92
	/*
93
	 * Present Manage just in time activation msg on update-core.php
94
	 *
95
	 */
96
	function manage_msg() {
97
		$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
98
		?>
99
		<div class="jp-jitm">
100
			<a href="#" data-module="manage" class="dismiss"><span class="genericon genericon-close"></span></a>
101
102
			<div class="jp-emblem">
103
				<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">
104
						<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" />
105
					</svg>
106
			</div>
107
			<p class="msg">
108
				<?php _e( 'Reduce security risks with automated plugin updates.', 'jetpack' ); ?>
109
			</p>
110
111
			<p>
112
				<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' ) ) {
113
					echo 'hide';
114
				} ?>"><?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' ) ) {
115
					echo 'hide';
116
				} ?>"><?php esc_html_e( 'Go to WordPress.com', 'jetpack' ); ?></a>
117
			</p>
118
		</div>
119
		<?php
120
		//jitm is being viewed, track it
121
		$jetpack = Jetpack::init();
122
		$jetpack->stat( 'jitm', 'manage-viewed-' . JETPACK__VERSION );
123
		$jetpack->do_stats( 'server_side' );
124
	}
125
126
	/*
127
	 * Present Photon just in time activation msg
128
	 *
129
	 */
130
	function photon_msg() {
131
		?>
132
		<div class="jp-jitm">
133
			<a href="#" data-module="photon" class="dismiss"><span class="genericon genericon-close"></span></a>
134
135
			<div class="jp-emblem">
136
				<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">
137
						<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" />
138
					</svg>
139
			</div>
140
			<p class="msg">
141
				<?php _e( 'Speed up your photos and save bandwidth costs by using a free content delivery network.', 'jetpack' ); ?>
142
			</p>
143
144
			<p>
145
				<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>
146
			</p>
147
		</div>
148
		<?php
149
		//jitm is being viewed, track it
150
		$jetpack = Jetpack::init();
151
		$jetpack->stat( 'jitm', 'photon-viewed-' . JETPACK__VERSION );
152
		$jetpack->do_stats( 'server_side' );
153
	}
154
155
	/**
156
	 * Display message prompting user to enable auto-updates in WordPress.com.
157
	 *
158
	 * @since 3.8.2
159
	 */
160
	function manage_pi_msg() {
161
		$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
162
		$manage_active       = Jetpack::is_module_active( 'manage' );
163
		// Check if plugin has auto update already enabled in WordPress.com and don't show JITM in such case.
164
		$active_before = get_option( 'jetpack_previously_activated', array() );
165
		delete_option( 'jetpack_previously_activated' );
166
		$active_now                  = get_option( 'active_plugins', array() );
167
		$activated                   = array_diff( $active_now, $active_before );
168
		$auto_update_plugin_list     = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
169
		$plugin_auto_update_disabled = false;
170
		foreach ( $activated as $plugin ) {
171
			if ( ! in_array( $plugin, $auto_update_plugin_list ) ) {
172
				// Plugin doesn't have auto updates enabled in WordPress.com yet.
173
				$plugin_auto_update_disabled = true;
174
				// We don't need to continue checking, it's ok to show JITM for this plugin.
175
				break;
176
			}
177
		}
178
179
		// Check if the activated plugin is in the WordPress.org repository
180
		$plugin_can_auto_update = false;
181
		$plugin_updates 		= get_site_transient( 'update_plugins' );
182
		if ( false === $plugin_updates ) {
183
			// If update_plugins doesn't exist, display message anyway
184
			$plugin_can_auto_update = true;
185
		} else {
186
			$plugin_updates = array_merge( $plugin_updates->response, $plugin_updates->no_update );
187
			foreach ( $activated as $plugin ) {
188
				if ( isset( $plugin_updates[ $plugin ] ) ) {
189
					// There's at least one plugin set cleared for auto updates
190
					$plugin_can_auto_update = true;
191
					// We don't need to continue checking, it's ok to show JITM for this round.
192
					break;
193
				}
194
			}
195
		}
196
197
		if ( ! $manage_active && $plugin_auto_update_disabled && $plugin_can_auto_update && self::$auto_updates_allowed ) :
198
			?>
199
			<div class="jp-jitm">
200
				<a href="#" data-module="manage-pi" class="dismiss"><span class="genericon genericon-close"></span></a>
201
202
				<div class="jp-emblem">
203
					<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">
204
						<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" />
205
					</svg>
206
				</div>
207
				<?php if ( ! $manage_active ) : ?>
208
					<p class="msg">
209
						<?php _e( 'Save time with automated plugin updates.', 'jetpack' ); ?>
210
					</p>
211
					<p>
212
						<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>
213
					</p>
214
				<?php elseif ( $manage_active ) : ?>
215
					<p>
216
						<?php esc_html_e( 'Save time with auto updates on WordPress.com', 'jetpack' ); ?>
217
					</p>
218
				<?php endif; // manage inactive
219
				?>
220
				<p class="show-after-enable <?php echo $manage_active ? '' : 'hide'; ?>">
221
					<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>
222
				</p>
223
			</div>
224
			<?php
225
			//jitm is being viewed, track it
226
			$jetpack = Jetpack::init();
227
			$jetpack->stat( 'jitm', 'manage-pi-viewed-' . JETPACK__VERSION );
228
			$jetpack->do_stats( 'server_side' );
229
		endif; // manage inactive
230
	}
231
232
	/**
233
	 * Display message in editor prompting user to compose entry in WordPress.com.
234
	 *
235
	 * @since 3.8.2
236
	 */
237
	function editor_msg() {
238
		global $typenow;
239
		if ( current_user_can( 'manage_options' ) ) {
240
			$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
241
			$editor_dismissed = isset( self::$jetpack_hide_jitm[ 'editor' ] );
242
			if ( ! $editor_dismissed ) :
243
			?>
244
			<div class="jp-jitm">
245
				<a href="#"  data-module="editor" class="dismiss"><span class="genericon genericon-close"></span></a>
246
				<div class="jp-emblem">
247
					<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">
248
						<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"/>
249
					</svg>
250
				</div>
251
				<p class="msg">
252
					<?php esc_html_e( 'Try the brand new editor.', 'jetpack' ); ?>
253
				</p>
254
				<p>
255
					<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>
256
				</p>
257
			</div>
258
			<?php
259
			//jitm is being viewed, track it
260
			$jetpack = Jetpack::init();
261
			$jetpack->stat( 'jitm', 'editor-viewed-' . JETPACK__VERSION );
262
			$jetpack->do_stats( 'server_side' );
263
			endif; // manage or editor inactive
264
		}
265
	}
266
267
	/*
268
	* Function to enqueue jitm css and js
269
	*/
270
	function jitm_enqueue_files( $hook ) {
0 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
271
272
		$wp_styles = new WP_Styles();
273
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
274
		wp_enqueue_style( 'jetpack-jitm-css', plugins_url( "css/jetpack-admin-jitm{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION . '-201243242' );
275
		$wp_styles->add_data( 'jetpack-jitm-css', 'rtl', true );
276
277
		//Build stats url for tracking manage button
278
		$jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => 'wordpresstools' ) );
279
280
		// Enqueue javascript to handle jitm notice events
281
		wp_enqueue_script( 'jetpack-jitm-js', plugins_url( '_inc/jetpack-jitm.js', JETPACK__PLUGIN_FILE ),
282
			array( 'jquery' ), JETPACK__VERSION, true );
283
		wp_localize_script(
284
			'jetpack-jitm-js',
285
			'jitmL10n',
286
			array(
287
				'ajaxurl'     => admin_url( 'admin-ajax.php' ),
288
				'jitm_nonce'  => wp_create_nonce( 'jetpack-jitm-nonce' ),
289
				'photon_msgs' => array(
290
					'success' => __( 'Success! Photon is now actively optimizing and serving your images for free.', 'jetpack' ),
291
					'fail'    => __( 'We are sorry but unfortunately Photon did not activate.', 'jetpack' )
292
				),
293
				'manage_msgs' => array(
294
					'success' => __( 'Success! WordPress.com tools are now active.', 'jetpack' ),
295
					'fail'    => __( 'We are sorry but unfortunately Manage did not activate.', 'jetpack' )
296
				),
297
				'jitm_stats_url' => $jitm_stats_url
298
			)
299
		);
300
	}
301
302
	/**
303
	 * Check if a JITM was dismissed or not. Currently, dismissing one JITM will dismiss all of them.
304
	 *
305
	 * @since 3.8.2
306
	 *
307
	 * @return bool
308
	 */
309
	function is_jitm_dismissed() {
310
		if ( is_null( self::$jetpack_hide_jitm ) ) {
311
			// The option returns false when nothing was dismissed
312
			self::$jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' );
0 ignored issues
show
Documentation Bug introduced by
It seems like \Jetpack_Options::get_option('hide_jitm') of type * is incompatible with the declared type array of property $jetpack_hide_jitm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
313
		}
314
		// so if it's not an array, it means no JITM was dismissed
315
		return is_array( self::$jetpack_hide_jitm );
316
	}
317
}
318
/**
319
 * Filter to turn off all just in time messages
320
 *
321
 * @since 3.7.0
322
 *
323
 * @param bool true Whether to show just in time messages.
324
 */
325
if ( apply_filters( 'jetpack_just_in_time_msgs', false ) ) {
326
	Jetpack_JITM::init();
327
}