Completed
Push — fix_jitm_dismiss_logic ( dc6ee2...03d095 )
by
unknown
60:22 queued 51:11
created

Jetpack_JITM::manage_msg()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 25
Code Lines 12

Duplication

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