Completed
Push — fix_jitm_dismiss_logic ( db6a46...444aaf )
by
unknown
78:23 queued 68:59
created

class.jetpack-jitm.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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