Completed
Push — fix_jitm_dismiss_logic ( 03168b...61648d )
by
unknown
34:42 queued 25:09
created

Jetpack_JITM::manage_pi_msg()   C

Complexity

Conditions 16
Paths 156

Size

Total Lines 73
Code Lines 48

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 73
rs 5.0146
cc 16
eloc 48
nc 156
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	static function init() {
23
		if ( is_null( self::$instance ) ) {
24
			self::$instance = new Jetpack_JITM;
25
		}
26
27
		return self::$instance;
28
	}
29
30
	private function __construct() {
31
		if ( ! Jetpack::is_active() ) {
32
			return;
33
		}
34
		add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
35
		add_action( 'load-plugins.php', array( $this, 'previously_activated_plugins' ) );
36
	}
37
38
	/**
39
	 * Prepare actions according to screen and post type.
40
	 *
41
	 * @since 3.8.2
42
	 *
43
	 * @param object $screen
44
	 */
45
	function prepare_jitms( $screen ) {
46
		if ( current_user_can( 'jetpack_manage_modules' ) ) {
47
			global $pagenow;
48
			// Only show auto update JITM if auto updates are allowed in this installation
49
			$auto_updates_enabled = ! ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED );
50
			if ( ! self::is_jitm_dismissed() ) {
51
				if ( 'media-new.php' == $pagenow && ! Jetpack::is_module_active( 'photon' ) ) {
52
					add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
53
					add_action( 'post-plupload-upload-ui', array( $this, 'photon_msg' ) );
54
				}
55
				else if ( 'update-core.php' == $pagenow && $auto_updates_enabled && ! Jetpack::is_module_active( 'manage' ) ) {
56
					add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
57
					add_action( 'admin_notices', array( $this, 'manage_msg' ) );
58
				}
59
				elseif ( 'plugins.php' == $pagenow && $auto_updates_enabled && ( isset( $_GET['activate'] ) && 'true' === $_GET['activate'] || isset( $_GET['activate-multi'] ) && 'true' === $_GET['activate-multi'] ) ) {
60
					add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
61
					add_action( 'pre_current_active_plugins', array( $this, 'manage_pi_msg' ) );
62
				}
63
				elseif ( 'post-new.php' == $pagenow && in_array( $screen->post_type, array( 'post', 'page' ) ) ) {
64
					add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
65
					add_action( 'admin_notices', array( $this, 'editor_msg' ) );
66
				}
67
			}
68
		}
69
	}
70
71
	/**
72
	 * Save plugins that are activated. This is used when one or more plugins are activated to know
73
	 * what was activated and use it in Jetpack_JITM::manage_pi_msg() before deleting the option.
74
	 *
75
	 * @since 3.8.2
76
	 */
77
	function previously_activated_plugins() {
78
		$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
79
		$action = $wp_list_table->current_action();
80
		if ( $action && ( 'activate' == $action || 'activate-selected' == $action ) ) {
81
			update_option( 'jetpack_previously_activated', get_option( 'active_plugins', array() ) );
82
		}
83
	}
84
85
	/*
86
	 * Present Manage just in time activation msg on update-core.php
87
	 *
88
	 */
89
	function manage_msg() {
90
		$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
91
		?>
92
		<div class="jp-jitm">
93
			<a href="#" data-module="manage" class="dismiss"><span class="genericon genericon-close"></span></a>
94
95
			<div class="jp-emblem">
96
				<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">
97
						<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" />
98
					</svg>
99
			</div>
100
			<p class="msg">
101
				<?php _e( 'Reduce security risks with automated plugin updates.', 'jetpack' ); ?>
102
			</p>
103
104
			<p>
105
				<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' ) ) {
106
					echo 'hide';
107
				} ?>"><?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' ) ) {
108
					echo 'hide';
109
				} ?>"><?php esc_html_e( 'Go to WordPress.com', 'jetpack' ); ?></a>
110
			</p>
111
		</div>
112
		<?php
113
		//jitm is being viewed, track it
114
		$jetpack = Jetpack::init();
115
		$jetpack->stat( 'jitm', 'manage-viewed-' . JETPACK__VERSION );
116
		$jetpack->do_stats( 'server_side' );
117
	}
118
119
	/*
120
	 * Present Photon just in time activation msg
121
	 *
122
	 */
123
	function photon_msg() {
124
		?>
125
		<div class="jp-jitm">
126
			<a href="#" data-module="photon" class="dismiss"><span class="genericon genericon-close"></span></a>
127
128
			<div class="jp-emblem">
129
				<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">
130
						<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" />
131
					</svg>
132
			</div>
133
			<p class="msg">
134
				<?php _e( 'Speed up your photos and save bandwidth costs by using a free content delivery network.', 'jetpack' ); ?>
135
			</p>
136
137
			<p>
138
				<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>
139
			</p>
140
		</div>
141
		<?php
142
		//jitm is being viewed, track it
143
		$jetpack = Jetpack::init();
144
		$jetpack->stat( 'jitm', 'photon-viewed-' . JETPACK__VERSION );
145
		$jetpack->do_stats( 'server_side' );
146
	}
147
148
	/**
149
	 * Display message prompting user to enable auto-updates in WordPress.com.
150
	 *
151
	 * @since 3.8.2
152
	 */
153
	function manage_pi_msg() {
154
		$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
155
		$manage_active       = Jetpack::is_module_active( 'manage' );
156
		// If it's not an array, it means no JITM was dismissed
157
		$manage_pi_dismissed = self::is_jitm_dismissed();
158
		// Check if plugin has auto update already enabled in WordPress.com and don't show JITM in such case.
159
		$active_before = get_option( 'jetpack_previously_activated', array() );
160
		delete_option( 'jetpack_previously_activated' );
161
		$active_now                  = get_option( 'active_plugins', array() );
162
		$activated                   = array_diff( $active_now, $active_before );
163
		$auto_update_plugin_list     = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
164
		$plugin_auto_update_disabled = false;
165
		foreach ( $activated as $plugin ) {
166
			if ( ! in_array( $plugin, $auto_update_plugin_list ) ) {
167
				// Plugin doesn't have auto updates enabled in WordPress.com yet.
168
				$plugin_auto_update_disabled = true;
169
				// We don't need to continue checking, it's ok to show JITM for this plugin.
170
				break;
171
			}
172
		}
173
		// Check if there isn't an auto_update_plugin filter set to false
174
		$plugin_updates                      = get_site_transient( 'update_plugins' );
175
		$plugin_updates                      = array_merge( $plugin_updates->response, $plugin_updates->no_update );
176
		$auto_update_not_disabled_for_plugin = false;
177
		foreach ( $activated as $plugin ) {
178
			if ( ! isset( $plugin_updates[$plugin] ) ) {
179
				continue;
180
			}
181
			if ( apply_filters( 'auto_update_plugin', true, $plugin_updates[$plugin] ) ) {
182
				// There's at least one plugin set cleared for auto updates
183
				$auto_update_not_disabled_for_plugin = true;
184
				// We don't need to continue checking, it's ok to show JITM for this round.
185
				break;
186
			}
187
		}
188
189
		if ( ( ! $manage_active || ! $manage_pi_dismissed ) && $plugin_auto_update_disabled && $auto_update_not_disabled_for_plugin ) :
190
			?>
191
			<div class="jp-jitm">
192
				<a href="#" data-module="manage-pi" class="dismiss"><span class="genericon genericon-close"></span></a>
193
194
				<div class="jp-emblem">
195
					<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">
196
						<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" />
197
					</svg>
198
				</div>
199
				<?php if ( ! $manage_active ) : ?>
200
					<p class="msg">
201
						<?php _e( 'Save time with automated plugin updates.', 'jetpack' ); ?>
202
					</p>
203
					<p>
204
						<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>
205
					</p>
206
				<?php elseif ( $manage_active ) : ?>
207
					<p>
208
						<?php esc_html_e( 'Save time with auto updates on WordPress.com', 'jetpack' ); ?>
209
					</p>
210
				<?php endif; // manage inactive
211
				?>
212
				<?php if ( ! $manage_pi_dismissed ) : ?>
213
					<p class="show-after-enable <?php echo $manage_active ? '' : 'hide'; ?>">
214
						<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>
215
					</p>
216
				<?php endif; // manage-pi inactive
217
				?>
218
			</div>
219
			<?php
220
			//jitm is being viewed, track it
221
			$jetpack = Jetpack::init();
222
			$jetpack->stat( 'jitm', 'manage-pi-viewed-' . JETPACK__VERSION );
223
			$jetpack->do_stats( 'server_side' );
224
		endif; // manage inactive
225
	}
226
227
	/**
228
	 * Display message in editor prompting user to compose entry in WordPress.com.
229
	 *
230
	 * @since 3.8.2
231
	 */
232
	function editor_msg() {
233
		global $typenow;
234
		if ( current_user_can( 'manage_options' ) ) {
235
			$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
236
			$editor_dismissed = isset( self::$jetpack_hide_jitm['editor'] );
237
			if ( ! $editor_dismissed ) :
238
			?>
239
			<div class="jp-jitm">
240
				<a href="#"  data-module="editor" class="dismiss"><span class="genericon genericon-close"></span></a>
241
				<div class="jp-emblem">
242
					<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">
243
						<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"/>
244
					</svg>
245
				</div>
246
				<p class="msg">
247
					<?php esc_html_e( 'Try the brand new editor.', 'jetpack' ); ?>
248
				</p>
249
				<p>
250
					<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>
251
				</p>
252
			</div>
253
			<?php
254
			//jitm is being viewed, track it
255
			$jetpack = Jetpack::init();
256
			$jetpack->stat( 'jitm', 'editor-viewed-' . JETPACK__VERSION );
257
			$jetpack->do_stats( 'server_side' );
258
			endif; // manage or editor inactive
259
		}
260
	}
261
262
	/*
263
	* Function to enqueue jitm css and js
264
	*/
265
	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...
266
267
		$wp_styles = new WP_Styles();
268
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
269
		wp_enqueue_style( 'jetpack-jitm-css', plugins_url( "css/jetpack-admin-jitm{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION . '-201243242' );
270
		$wp_styles->add_data( 'jetpack-jitm-css', 'rtl', true );
271
272
		//Build stats url for tracking manage button
273
		$jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => 'wordpresstools' ) );
274
275
		// Enqueue javascript to handle jitm notice events
276
		wp_enqueue_script( 'jetpack-jitm-js', plugins_url( '_inc/jetpack-jitm.js', JETPACK__PLUGIN_FILE ),
277
			array( 'jquery' ), JETPACK__VERSION, true );
278
		wp_localize_script(
279
			'jetpack-jitm-js',
280
			'jitmL10n',
281
			array(
282
				'ajaxurl'     => admin_url( 'admin-ajax.php' ),
283
				'jitm_nonce'  => wp_create_nonce( 'jetpack-jitm-nonce' ),
284
				'photon_msgs' => array(
285
					'success' => __( 'Success! Photon is now actively optimizing and serving your images for free.', 'jetpack' ),
286
					'fail'    => __( 'We are sorry but unfortunately Photon did not activate.', 'jetpack' )
287
				),
288
				'manage_msgs' => array(
289
					'success' => __( 'Success! WordPress.com tools are now active.', 'jetpack' ),
290
					'fail'    => __( 'We are sorry but unfortunately Manage did not activate.', 'jetpack' )
291
				),
292
				'jitm_stats_url' => $jitm_stats_url
293
			)
294
		);
295
	}
296
297
	/**
298
	 * Check if a JITM was dismissed or not. Currently, dismissing one JITM will dismiss all of them.
299
	 *
300
	 * @since 3.8.2
301
	 *
302
	 * @return bool
303
	 */
304
	function is_jitm_dismissed() {
305
		if ( is_null( self::$jetpack_hide_jitm ) ) {
306
			// The option returns false when nothing was dismissed
307
			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...
308
		}
309
		// so if it's not an array, it means no JITM was dismissed
310
		return is_array( self::$jetpack_hide_jitm );
311
	}
312
}
313
/**
314
 * Filter to turn off all just in time messages
315
 *
316
 * @since 3.7.0
317
 *
318
 * @param bool true Whether to show just in time messages.
319
 */
320
if ( apply_filters( 'jetpack_just_in_time_msgs', false ) ) {
321
	Jetpack_JITM::init();
322
}