Completed
Push — fix_jitm_dismiss_logic ( bcb9bb...03168b )
by
unknown
28:05 queued 18:24
created

Jetpack_JITM::editor_msg()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

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