Completed
Push — fix_jitm_dismiss_logic ( 61648d...db6a46 )
by
unknown
30:51 queued 21:59
created

Jetpack_JITM::jitm_enqueue_files()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 21

Duplication

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