Completed
Push — update/jitms_track_post_enable... ( f91c62...80309a )
by
unknown
17:10 queued 09:04
created

class.jetpack-jitm.php (4 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() || 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
59
		// Only show auto update JITM if auto updates are allowed in this installation
60
		$possible_reasons_for_failure = Jetpack_Autoupdate::get_possible_failures();
61
		self::$auto_updates_allowed = empty( $possible_reasons_for_failure );
62
		$photon_inactive = ! Jetpack::is_module_active( 'photon' );
63
64
		if ( 'media-new.php' == $pagenow && $photon_inactive ) {
65
			add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
66
			add_action( 'post-plupload-upload-ui', array( $this, 'photon_msg' ) );
67
		}
68
		elseif ( 'post-new.php' == $pagenow ) {
69
			$calypso_supported_post_types = in_array( $screen->post_type, array( 'post', 'page' ) );
70
			if ( $calypso_supported_post_types || $photon_inactive ) {
71
				add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
72
			}
73
			if ( $calypso_supported_post_types ) {
74
				add_action( 'admin_notices', array( $this, 'editor_msg' ) );
75
			}
76
			if ( $photon_inactive ) {
77
				add_action( 'print_media_templates', array( $this, 'photon_tmpl' ) );
78
			}
79
		}
80
		elseif ( 'post.php' == $pagenow ) {
81
			$user_published  = isset( $_GET['message'] ) && 6 == $_GET['message'];
0 ignored issues
show
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
82
			if ( $user_published || $photon_inactive ) {
83
				add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
84
			}
85
			if ( $user_published ) {
86
				add_action( 'edit_form_top', array( $this, 'stats_msg' ) );
87
			}
88
			if ( $photon_inactive ) {
89
				add_action( 'print_media_templates', array( $this, 'photon_tmpl' ) );
90
			}
91
		}
92
		elseif ( self::$auto_updates_allowed ) {
93
			if ( 'update-core.php' == $pagenow && ! Jetpack::is_module_active( 'manage' ) ) {
94
				add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
95
				add_action( 'admin_notices', array( $this, 'manage_msg' ) );
96
			}
97
			elseif ( 'plugins.php' == $pagenow ) {
98
				if ( ( isset( $_GET['activate'] ) && 'true' === $_GET['activate'] ) || ( isset( $_GET['activate-multi'] ) && 'true' === $_GET['activate-multi'] ) ) {
99
					add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
100
					add_action( 'pre_current_active_plugins', array( $this, 'manage_pi_msg' ) );
101
				} else {
102
103
					// Save plugins that are activated. This is used when one or more plugins are activated to know
104
					// what was activated and use it in Jetpack_JITM::manage_pi_msg() before deleting the option.
105
					$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
106
					$action = $wp_list_table->current_action();
107
					if ( $action && ( 'activate' == $action || 'activate-selected' == $action ) ) {
108
						update_option( 'jetpack_temp_active_plugins_before', get_option( 'active_plugins', array() ) );
109
					}
110
				}
111
			}
112
		}
113
	}
114
115
	/*
116
	 * Present Manage just in time activation msg on update-core.php
117
	 *
118
	 */
119
	function manage_msg() {
120
		$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
121
		?>
122
		<div class="jp-jitm">
123
			<a href="#" data-module="manage" class="dismiss"><span class="genericon genericon-close"></span></a>
124
125
			<div class="jp-emblem">
126
				<?php echo self::get_jp_emblem(); ?>
127
			</div>
128
			<p class="msg">
129
				<?php esc_html_e( 'Reduce security risks with automated plugin updates.', 'jetpack' ); ?>
130
			</p>
131
132
			<p>
133
				<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" class="activate button <?php if ( Jetpack::is_module_active( 'manage' ) ) {
134
					echo 'hide';
135
				} ?>"><?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' ) ) {
136
					echo 'hide';
137
				} ?>"><?php esc_html_e( 'Go to WordPress.com', 'jetpack' ); ?></a>
138
			</p>
139
		</div>
140
		<?php
141
		//jitm is being viewed, track it
142
		$jetpack = Jetpack::init();
143
		$jetpack->stat( 'jitm', 'manage-viewed-' . JETPACK__VERSION );
144
		$jetpack->do_stats( 'server_side' );
145
	}
146
147
	/*
148
	 * Present Photon just in time activation msg
149
	 *
150
	 */
151
	function photon_msg() {
152
		?>
153
		<div class="jp-jitm">
154
			<a href="#" data-module="photon" class="dismiss"><span class="genericon genericon-close"></span></a>
155
156
			<div class="jp-emblem">
157
				<?php echo self::get_jp_emblem(); ?>
158
			</div>
159
			<p class="msg">
160
				<?php esc_html_e( 'Speed up your photos and save bandwidth costs by using a free content delivery network.', 'jetpack' ); ?>
161
			</p>
162
163
			<p>
164
				<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="<?php echo esc_attr__( 'Loading...', 'jetpack' ); ?>" /><a href="#" data-module="photon" class="activate button button-jetpack"><?php esc_html_e( 'Activate Photon', 'jetpack' ); ?></a>
165
			</p>
166
		</div>
167
		<?php
168
		//jitm is being viewed, track it
169
		$jetpack = Jetpack::init();
170
		$jetpack->stat( 'jitm', 'photon-viewed-' . JETPACK__VERSION );
171
		$jetpack->do_stats( 'server_side' );
172
	}
173
174
	/**
175
	 * Display Photon JITM template in Media Library after user uploads an image.
176
	 *
177
	 * @since 3.9.0
178
	 */
179
	function photon_tmpl() {
180
		?>
181
		<script id="tmpl-jitm-photon" type="text/html">
182
			<div class="jp-jitm" data-track="photon-modal">
183
				<a href="#" data-module="photon" class="dismiss"><span class="genericon genericon-close"></span></a>
184
185
				<div class="jp-emblem">
186
					<?php echo self::get_jp_emblem(); ?>
187
				</div>
188
				<p class="msg">
189
					<?php esc_html_e( 'Let Jetpack deliver your images optimized and faster than ever.', 'jetpack' ); ?>
190
				</p>
191
192
				<p>
193
					<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="<?php echo esc_attr__( 'Loading...', 'jetpack' ); ?>" /><a href="#" data-module="photon" class="activate button button-jetpack"><?php esc_html_e( 'Activate Photon', 'jetpack' ); ?></a>
194
				</p>
195
			</div>
196
		</script>
197
		<?php
198
	}
199
200
	/**
201
	 * Display message prompting user to enable auto-updates in WordPress.com.
202
	 *
203
	 * @since 3.8.2
204
	 */
205
	function manage_pi_msg() {
206
		$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
207
		$manage_active       = Jetpack::is_module_active( 'manage' );
208
209
		// Check if plugin has auto update already enabled in WordPress.com and don't show JITM in such case.
210
		$active_before = get_option( 'jetpack_temp_active_plugins_before', array() );
211
		delete_option( 'jetpack_temp_active_plugins_before' );
212
		$active_now                  = get_option( 'active_plugins', array() );
213
		$activated                   = array_diff( $active_now, $active_before );
214
		$auto_update_plugin_list     = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
215
		$plugin_auto_update_disabled = false;
216
		foreach ( $activated as $plugin ) {
217
			if ( ! in_array( $plugin, $auto_update_plugin_list ) ) {
218
219
				// Plugin doesn't have auto updates enabled in WordPress.com yet.
220
				$plugin_auto_update_disabled = true;
221
222
				// We don't need to continue checking, it's ok to show JITM for this plugin.
223
				break;
224
			}
225
		}
226
227
		// Check if the activated plugin is in the WordPress.org repository
228
		$plugin_can_auto_update = false;
229
		$plugin_updates 		= get_site_transient( 'update_plugins' );
230
		if ( false === $plugin_updates ) {
231
232
			// If update_plugins doesn't exist, display message anyway
233
			$plugin_can_auto_update = true;
234
		} else {
235
			$plugin_updates = array_merge( $plugin_updates->response, $plugin_updates->no_update );
236
			foreach ( $activated as $plugin ) {
237
				if ( isset( $plugin_updates[ $plugin ] ) ) {
238
239
					// There's at least one plugin set cleared for auto updates
240
					$plugin_can_auto_update = true;
241
242
					// We don't need to continue checking, it's ok to show JITM for this round.
243
					break;
244
				}
245
			}
246
		}
247
248
		if ( ! $manage_active && $plugin_auto_update_disabled && $plugin_can_auto_update && self::$auto_updates_allowed ) :
249
			?>
250
			<div class="jp-jitm">
251
				<a href="#" data-module="manage-pi" class="dismiss"><span class="genericon genericon-close"></span></a>
252
253
				<div class="jp-emblem">
254
					<?php echo self::get_jp_emblem(); ?>
255
				</div>
256
				<?php if ( ! $manage_active ) : ?>
257
					<p class="msg">
258
						<?php esc_html_e( 'Save time with automated plugin updates.', 'jetpack' ); ?>
259
					</p>
260
					<p>
261
						<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>
262
					</p>
263
				<?php elseif ( $manage_active ) : ?>
264
					<p>
265
						<?php esc_html_e( 'Save time with auto updates on WordPress.com', 'jetpack' ); ?>
266
					</p>
267
				<?php endif; // manage inactive
268
				?>
269
				<p class="show-after-enable <?php echo $manage_active ? '' : 'hide'; ?>">
270
					<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>
271
				</p>
272
			</div>
273
			<?php
274
			//jitm is being viewed, track it
275
			$jetpack = Jetpack::init();
276
			$jetpack->stat( 'jitm', 'manage-pi-viewed-' . JETPACK__VERSION );
277
			$jetpack->do_stats( 'server_side' );
278
		endif; // manage inactive
279
	}
280
281
	/**
282
	 * Display message in editor prompting user to compose entry in WordPress.com.
283
	 *
284
	 * @since 3.8.2
285
	 */
286
	function editor_msg() {
287
		global $typenow;
288
		if ( current_user_can( 'manage_options' ) ) {
289
			$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
290
			$editor_dismissed = isset( self::$jetpack_hide_jitm['editor'] );
291
			if ( ! $editor_dismissed ) :
292
			?>
293
			<div class="jp-jitm">
294
				<a href="#"  data-module="editor" class="dismiss"><span class="genericon genericon-close"></span></a>
295
				<div class="jp-emblem">
296
					<?php echo self::get_jp_emblem(); ?>
297
				</div>
298
				<p class="msg">
299
					<?php esc_html_e( 'Try the brand new editor.', 'jetpack' ); ?>
300
				</p>
301
				<p>
302
					<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>
303
				</p>
304
			</div>
305
			<?php
306
			//jitm is being viewed, track it
307
			$jetpack = Jetpack::init();
308
			$jetpack->stat( 'jitm', 'editor-viewed-' . JETPACK__VERSION );
309
			$jetpack->do_stats( 'server_side' );
310
			endif; // manage or editor inactive
311
		}
312
	}
313
314
	/**
315
	 * Display message in editor prompting user to enable stats.
316
	 *
317
	 * @since 3.9.0
318
	 */
319
	function stats_msg() {
320
		$stats_active        = Jetpack::is_module_active( 'stats' );
321
		$normalized_site_url = Jetpack::build_raw_urls( get_home_url() );
322
		?>
323
		<div class="jp-jitm">
324
			<a href="#" data-module="stats" class="dismiss"><span class="genericon genericon-close"></span></a>
325
326
			<div class="jp-emblem">
327
				<?php echo self::get_jp_emblem(); ?>
328
			</div>
329
			<p class="msg">
330
				<?php esc_html_e( 'Track detailed stats on this post and the rest of your site.', 'jetpack' ); ?>
331
			</p>
332
			<?php if ( ! $stats_active ) : ?>
333
				<p>
334
					<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="stats" data-module-success="<?php esc_attr_e( 'Success! Jetpack Stats is now activated.', 'jetpack' ); ?>" class="activate button"><?php esc_html_e( 'Enable Jetpack Stats', 'jetpack' ); ?></a>
335
				</p>
336
			<?php endif; // stats inactive
337
			?>
338
			<p class="show-after-enable <?php echo $stats_active ? '' : 'hide'; ?>">
339
				<a href="<?php echo esc_url( 'https://wordpress.com/stats/insights/' . $normalized_site_url ); ?>" target="_blank" title="<?php esc_attr_e( 'Go to WordPress.com', 'jetpack' ); ?>" data-module="stats" class="button button-jetpack launch show-after-enable"><?php esc_html_e( 'Go to WordPress.com', 'jetpack' ); ?></a>
340
			</p>
341
		</div>
342
		<?php
343
		//jitm is being viewed, track it
344
		$jetpack = Jetpack::init();
345
		$jetpack->stat( 'jitm', 'post-stats-viewed-' . JETPACK__VERSION );
346
		$jetpack->do_stats( 'server_side' );
347
	}
348
349
	/*
350
	* Function to enqueue jitm css and js
351
	*/
352
	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...
353
354
		$wp_styles = new WP_Styles();
355
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
356
		wp_enqueue_style( 'jetpack-jitm-css', plugins_url( "css/jetpack-admin-jitm{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION . '-201243242' );
357
		$wp_styles->add_data( 'jetpack-jitm-css', 'rtl', true );
358
359
		//Build stats url for tracking manage button
360
		$jitm_stats_url = Jetpack::build_stats_url( array( 'x_jetpack-jitm' => 'wordpresstools' ) );
361
362
		// Enqueue javascript to handle jitm notice events
363
		wp_enqueue_script( 'jetpack-jitm-js', plugins_url( '_inc/jetpack-jitm.js', JETPACK__PLUGIN_FILE ),
364
			array( 'jquery' ), JETPACK__VERSION, true );
365
		wp_localize_script(
366
			'jetpack-jitm-js',
367
			'jitmL10n',
368
			array(
369
				'ajaxurl'     => admin_url( 'admin-ajax.php' ),
370
				'jitm_nonce'  => wp_create_nonce( 'jetpack-jitm-nonce' ),
371
				'photon_msgs' => array(
372
					'success' => esc_html__( 'Success! Photon is now actively optimizing and serving your images for free.', 'jetpack' ),
373
					'fail'    => esc_html__( 'We are sorry but unfortunately Photon did not activate.', 'jetpack' )
374
				),
375
				'manage_msgs' => array(
376
					'success' => esc_html__( 'Success! WordPress.com tools are now active.', 'jetpack' ),
377
					'fail'    => esc_html__( 'We are sorry but unfortunately Manage did not activate.', 'jetpack' )
378
				),
379
				'stats_msgs' => array(
380
					'success' => esc_html__( 'Success! Stats are now active.', 'jetpack' ),
381
					'fail'    => esc_html__( 'We are sorry but unfortunately Stats did not activate.', 'jetpack' )
382
				),
383
				'jitm_stats_url' => $jitm_stats_url
384
			)
385
		);
386
	}
387
388
	/**
389
	 * Check if a JITM was dismissed or not. Currently, dismissing one JITM will dismiss all of them.
390
	 *
391
	 * @since 3.8.2
392
	 *
393
	 * @return bool
394
	 */
395
	function is_jitm_dismissed() {
396
		if ( is_null( self::$jetpack_hide_jitm ) ) {
397
398
			// The option returns false when nothing was dismissed
399
			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...
400
		}
401
402
		// so if it's not an array, it means no JITM was dismissed
403
		return is_array( self::$jetpack_hide_jitm );
404
	}
405
406
	/**
407
	 * Return string containing the Jetpack logo.
408
	 *
409
	 * @since 3.9.0
410
	 *
411
	 * @return string
412
	 */
413
	function get_jp_emblem() {
414
		return '<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">	<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" /></svg>';
415
	}
416
}
417
/**
418
 * Filter to turn off all just in time messages
419
 *
420
 * @since 3.7.0
421
 *
422
 * @param bool true Whether to show just in time messages.
423
 */
424
if ( apply_filters( 'jetpack_just_in_time_msgs', false ) ) {
425
	Jetpack_JITM::init();
426
}