Completed
Push — add/admin-page-package ( 7976aa...cbfc2c )
by
unknown
136:41 queued 124:01
created

Post_Connection_JITM::prepare_jitms()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack's Post-Connection JITM class.
4
 *
5
 * @package automattic/jetpack-jitm
6
 */
7
8
namespace Automattic\Jetpack\JITMS;
9
10
use Automattic\Jetpack\A8c_Mc_Stats;
11
use Automattic\Jetpack\Connection\Client;
12
use Automattic\Jetpack\Connection\Manager;
13
use Automattic\Jetpack\Device_Detection;
14
use Automattic\Jetpack\Partner;
15
use Automattic\Jetpack\Redirect;
16
use Automattic\Jetpack\Status;
17
use Automattic\Jetpack\Tracking;
18
19
/**
20
 * Jetpack just in time messaging through out the admin
21
 *
22
 * @since 5.6.0
23
 */
24
class Post_Connection_JITM extends JITM {
25
26
	/**
27
	 * Tracking object.
28
	 *
29
	 * @var Automattic\Jetpack\Tracking
30
	 *
31
	 * @access private
32
	 */
33
	public $tracking;
34
35
	/**
36
	 * JITM constructor.
37
	 */
38
	public function __construct() {
39
		$this->tracking = new Tracking();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Automattic\Jetpack\Tracking() of type object<Automattic\Jetpack\Tracking> is incompatible with the declared type object<Automattic\Jetpac...attic\Jetpack\Tracking> of property $tracking.

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...
40
	}
41
42
	/**
43
	 * Prepare actions according to screen and post type.
44
	 *
45
	 * @since 3.8.2
46
	 *
47
	 * @uses Jetpack_Autoupdate::get_possible_failures()
48
	 *
49
	 * @param \WP_Screen $screen WP Core's screen object.
50
	 */
51
	public function prepare_jitms( $screen ) {
52
		parent::prepare_jitms( $screen );
53
		if ( ! in_array(
54
			$screen->id,
55
			array(
56
				'jetpack_page_akismet-key-config',
57
				'admin_page_jetpack_modules',
58
			),
59
			true
60
		) ) {
61
			// Not really a JITM. Don't know where else to put this :) .
62
			add_action( 'admin_notices', array( $this, 'delete_user_update_connection_owner_notice' ) );
63
		}
64
	}
65
66
	/**
67
	 * A special filter for WooCommerce, to set a message based on local state.
68
	 *
69
	 * @param string $content The current message.
70
	 *
71
	 * @return array The new message.
72
	 */
73
	public static function jitm_woocommerce_services_msg( $content ) {
74
		if ( ! function_exists( 'wc_get_base_location' ) ) {
75
			return $content;
76
		}
77
78
		$base_location = wc_get_base_location();
79
80
		switch ( $base_location['country'] ) {
81
			case 'US':
82
				$content->message = esc_html__( 'New free service: Show USPS shipping rates on your store! Added bonus: print shipping labels without leaving WooCommerce.', 'jetpack' );
83
				break;
84
			case 'CA':
85
				$content->message = esc_html__( 'New free service: Show Canada Post shipping rates on your store!', 'jetpack' );
86
				break;
87
			default:
88
				$content->message = '';
89
		}
90
91
		return $content;
92
	}
93
94
	/**
95
	 * A special filter for WooCommerce Call To Action button
96
	 *
97
	 * @return string The new CTA
98
	 */
99
	public static function jitm_jetpack_woo_services_install() {
100
		return wp_nonce_url(
101
			add_query_arg(
102
				array(
103
					'wc-services-action' => 'install',
104
				),
105
				admin_url( 'admin.php?page=wc-settings' )
106
			),
107
			'wc-services-install'
108
		);
109
	}
110
111
	/**
112
	 * A special filter for WooCommerce Call To Action button.
113
	 *
114
	 * @return string The new CTA
115
	 */
116
	public static function jitm_jetpack_woo_services_activate() {
117
		return wp_nonce_url(
118
			add_query_arg(
119
				array(
120
					'wc-services-action' => 'activate',
121
				),
122
				admin_url( 'admin.php?page=wc-settings' )
123
			),
124
			'wc-services-install'
125
		);
126
	}
127
128
	/**
129
	 * A special filter used in the CTA of a JITM offering to install the Creative Mail plugin.
130
	 *
131
	 * @return string The new CTA
132
	 */
133
	public static function jitm_jetpack_creative_mail_install() {
134
		return wp_nonce_url(
135
			add_query_arg(
136
				array(
137
					'creative-mail-action' => 'install',
138
				),
139
				admin_url( 'edit.php?post_type=feedback' )
140
			),
141
			'creative-mail-install'
142
		);
143
	}
144
145
	/**
146
	 * A special filter used in the CTA of a JITM offering to activate the Creative Mail plugin.
147
	 *
148
	 * @return string The new CTA
149
	 */
150
	public static function jitm_jetpack_creative_mail_activate() {
151
		return wp_nonce_url(
152
			add_query_arg(
153
				array(
154
					'creative-mail-action' => 'activate',
155
				),
156
				admin_url( 'edit.php?post_type=feedback' )
157
			),
158
			'creative-mail-install'
159
		);
160
	}
161
162
	/**
163
	 * This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
164
	 * the connection owner.
165
	 */
166
	public function delete_user_update_connection_owner_notice() {
167
		global $current_screen;
168
169
		/*
170
		 * phpcs:disable WordPress.Security.NonceVerification.Recommended
171
		 *
172
		 * This function is firing within wp-admin and checks (below) if it is in the midst of a deletion on the users
173
		 * page. Nonce will be already checked by WordPress, so we do not need to check ourselves.
174
		 */
175
176
		if ( ! isset( $current_screen->base ) || 'users' !== $current_screen->base ) {
177
			return;
178
		}
179
180
		if ( ! isset( $_REQUEST['action'] ) || 'delete' !== $_REQUEST['action'] ) {
181
			return;
182
		}
183
184
		// Get connection owner or bail.
185
		$connection_manager  = new Manager();
186
		$connection_owner_id = $connection_manager->get_connection_owner_id();
187
		if ( ! $connection_owner_id ) {
188
			return;
189
		}
190
		$connection_owner_userdata = get_userdata( $connection_owner_id );
191
192
		// Bail if we're not trying to delete connection owner.
193
		$user_ids_to_delete = array();
194 View Code Duplication
		if ( isset( $_REQUEST['users'] ) ) {
195
			$user_ids_to_delete = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['users'] ) );
196
		} elseif ( isset( $_REQUEST['user'] ) ) {
197
			$user_ids_to_delete[] = sanitize_text_field( wp_unslash( $_REQUEST['user'] ) );
198
		}
199
200
		// phpcs:enable
201
		$user_ids_to_delete        = array_map( 'absint', $user_ids_to_delete );
202
		$deleting_connection_owner = in_array( $connection_owner_id, (array) $user_ids_to_delete, true );
203
		if ( ! $deleting_connection_owner ) {
204
			return;
205
		}
206
207
		// Bail if they're trying to delete themselves to avoid confusion.
208
		if ( get_current_user_id() === $connection_owner_id ) {
209
			return;
210
		}
211
212
		// Track it!
213
		if ( method_exists( $this->tracking, 'record_user_event' ) ) {
214
			$this->tracking->record_user_event( 'delete_connection_owner_notice_view' );
215
		}
216
217
		$connected_admins = $connection_manager->get_connected_users( 'jetpack_disconnect' );
218
		$user             = is_a( $connection_owner_userdata, 'WP_User' ) ? esc_html( $connection_owner_userdata->data->user_login ) : '';
219
220
		echo "<div class='notice notice-warning' id='jetpack-notice-switch-connection-owner'>";
221
		echo '<h2>' . esc_html__( 'Important notice about your Jetpack connection:', 'jetpack' ) . '</h2>';
222
		echo '<p>' . sprintf(
223
			/* translators: WordPress User, if available. */
224
			esc_html__( 'Warning! You are about to delete the Jetpack connection owner (%s) for this site, which may cause some of your Jetpack features to stop working.', 'jetpack' ),
225
			esc_html( $user )
226
		) . '</p>';
227
228
		if ( ! empty( $connected_admins ) && count( $connected_admins ) > 1 ) {
229
			echo '<form id="jp-switch-connection-owner" action="" method="post">';
230
			echo "<label for='owner'>" . esc_html__( 'You can choose to transfer connection ownership to one of these already-connected admins:', 'jetpack' ) . ' </label>';
231
232
			$connected_admin_ids = array_map(
233
				function ( $connected_admin ) {
234
						return $connected_admin->ID;
235
				},
236
				$connected_admins
237
			);
238
239
			wp_dropdown_users(
240
				array(
241
					'name'    => 'owner',
242
					'include' => array_diff( $connected_admin_ids, array( $connection_owner_id ) ),
243
					'show'    => 'display_name_with_login',
244
				)
245
			);
246
247
			echo '<p>';
248
			submit_button( esc_html__( 'Set new connection owner', 'jetpack' ), 'primary', 'jp-switch-connection-owner-submit', false );
249
			echo '</p>';
250
251
			echo "<div id='jp-switch-user-results'></div>";
252
			echo '</form>';
253
			?>
254
			<script type="text/javascript">
255
				jQuery( document ).ready( function( $ ) {
256
					$( '#jp-switch-connection-owner' ).on( 'submit', function( e ) {
257
						var formData = $( this ).serialize();
258
						var submitBtn = document.getElementById( 'jp-switch-connection-owner-submit' );
259
						var results = document.getElementById( 'jp-switch-user-results' );
260
261
						submitBtn.disabled = true;
262
263
						$.ajax( {
264
							type        : "POST",
265
							url         : "<?php echo esc_url( get_rest_url() . 'jetpack/v4/connection/owner' ); ?>",
266
							data        : formData,
267
							headers     : {
268
								'X-WP-Nonce': "<?php echo esc_js( wp_create_nonce( 'wp_rest' ) ); ?>",
269
							},
270
							success: function() {
271
								results.innerHTML = "<?php esc_html_e( 'Success!', 'jetpack' ); ?>";
272
								setTimeout( function() {
273
									$( '#jetpack-notice-switch-connection-owner' ).hide( 'slow' );
274
								}, 1000 );
275
							}
276
						} ).done( function() {
277
							submitBtn.disabled = false;
278
						} );
279
280
						e.preventDefault();
281
						return false;
282
					} );
283
				} );
284
			</script>
285
			<?php
286
		} else {
287
			echo '<p>' . esc_html__( 'Every Jetpack site needs at least one connected admin for the features to work properly. Please connect to your WordPress.com account via the button below. Once you connect, you may refresh this page to see an option to change the connection owner.', 'jetpack' ) . '</p>';
288
			$connect_url = $connection_manager->get_authorization_url();
289
			$connect_url = add_query_arg( 'from', 'delete_connection_owner_notice', $connect_url );
290
			echo "<a href='" . esc_url( $connect_url ) . "' target='_blank' rel='noopener noreferrer' class='button-primary'>" . esc_html__( 'Connect to WordPress.com', 'jetpack' ) . '</a>';
291
		}
292
293
		echo '<p>';
294
		printf(
295
			wp_kses(
296
				/* translators: URL to Jetpack support doc regarding the primary user. */
297
				__( "<a href='%s' target='_blank' rel='noopener noreferrer'>Learn more</a> about the connection owner and what will break if you do not have one.", 'jetpack' ),
298
				array(
299
					'a' => array(
300
						'href'   => true,
301
						'target' => true,
302
						'rel'    => true,
303
					),
304
				)
305
			),
306
			esc_url( Redirect::get_url( 'jetpack-support-primary-user' ) )
307
		);
308
		echo '</p>';
309
		echo '<p>';
310
		printf(
311
			wp_kses(
312
				/* translators: URL to contact Jetpack support. */
313
				__( 'As always, feel free to <a href="%s" target="_blank" rel="noopener noreferrer">contact our support team</a> if you have any questions.', 'jetpack' ),
314
				array(
315
					'a' => array(
316
						'href'   => true,
317
						'target' => true,
318
						'rel'    => true,
319
					),
320
				)
321
			),
322
			esc_url( Redirect::get_url( 'jetpack-contact-support' ) )
323
		);
324
		echo '</p>';
325
		echo '</div>';
326
	}
327
328
	/**
329
	 * Dismisses a JITM feature class so that it will no longer be shown.
330
	 *
331
	 * @param string $id The id of the JITM that was dismissed.
332
	 * @param string $feature_class The feature class of the JITM that was dismissed.
333
	 *
334
	 * @return bool Always true.
335
	 */
336
	public function dismiss( $id, $feature_class ) {
337
		$this->tracking->record_user_event(
338
			'jitm_dismiss_client',
339
			array(
340
				'jitm_id'       => $id,
341
				'feature_class' => $feature_class,
342
			)
343
		);
344
		$this->save_dismiss( $feature_class );
345
		return true;
346
	}
347
348
	/**
349
	 * Asks the wpcom API for the current message to display keyed on query string and message path
350
	 *
351
	 * @param string $message_path The message path to ask for.
352
	 * @param string $query The query string originally from the front end.
353
	 * @param bool   $full_jp_logo_exists If there is a full Jetpack logo already on the page.
354
	 *
355
	 * @return array The JITM's to show, or an empty array if there is nothing to show
356
	 */
357
	public function get_messages( $message_path, $query, $full_jp_logo_exists ) {
358
		// WooCommerce Services.
359
		add_filter( 'jitm_woocommerce_services_msg', array( $this, 'jitm_woocommerce_services_msg' ) );
360
		add_filter( 'jitm_jetpack_woo_services_install', array( $this, 'jitm_jetpack_woo_services_install' ) );
361
		add_filter( 'jitm_jetpack_woo_services_activate', array( $this, 'jitm_jetpack_woo_services_activate' ) );
362
363
		// Creative Mail.
364
		add_filter( 'jitm_jetpack_creative_mail_install', array( $this, 'jitm_jetpack_creative_mail_install' ) );
365
		add_filter( 'jitm_jetpack_creative_mail_activate', array( $this, 'jitm_jetpack_creative_mail_activate' ) );
366
367
		$user = wp_get_current_user();
368
369
		// Unauthenticated or invalid requests just bail.
370
		if ( ! $user ) {
371
			return array();
372
		}
373
374
		$user_roles = implode( ',', $user->roles );
375
		$site_id    = \Jetpack_Options::get_option( 'id' );
376
377
		// Build our jitm request.
378
		$path = add_query_arg(
379
			array(
380
				'external_user_id' => urlencode_deep( $user->ID ),
381
				'user_roles'       => urlencode_deep( $user_roles ),
382
				'query_string'     => urlencode_deep( $query ),
383
				'mobile_browser'   => Device_Detection::is_smartphone() ? 1 : 0,
384
				'_locale'          => get_user_locale(),
385
			),
386
			sprintf( '/sites/%d/jitm/%s', $site_id, $message_path )
387
		);
388
389
		// Attempt to get from cache.
390
		$envelopes = get_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ) );
391
392
		// If something is in the cache and it was put in the cache after the last sync we care about, use it.
393
		$use_cache = false;
394
395
		/**
396
		 * Filter to turn off jitm caching
397
		 *
398
		 * @since 5.4.0
399
		 *
400
		 * @param bool true Whether to cache just in time messages
401
		 */
402
		if ( apply_filters( 'jetpack_just_in_time_msg_cache', true ) ) {
403
			$use_cache = true;
404
		}
405
406
		if ( $use_cache ) {
407
			$last_sync  = (int) get_transient( 'jetpack_last_plugin_sync' );
408
			$from_cache = $envelopes && $last_sync > 0 && $last_sync < $envelopes['last_response_time'];
409
		} else {
410
			$from_cache = false;
411
		}
412
413
		// Otherwise, ask again.
414
		if ( ! $from_cache ) {
415
			$wpcom_response = Client::wpcom_json_api_request_as_blog(
416
				$path,
417
				'2',
418
				array(
419
					'user_id'    => $user->ID,
420
					'user_roles' => implode( ',', $user->roles ),
421
				),
422
				null,
423
				'wpcom'
424
			);
425
426
			// silently fail...might be helpful to track it?
427
			if ( is_wp_error( $wpcom_response ) ) {
428
				return array();
429
			}
430
431
			$envelopes = json_decode( $wpcom_response['body'] );
432
433
			if ( ! is_array( $envelopes ) ) {
434
				return array();
435
			}
436
437
			$expiration = isset( $envelopes[0] ) ? $envelopes[0]->ttl : 300;
438
439
			// Do not cache if expiration is 0 or we're not using the cache.
440
			if ( 0 !== $expiration && $use_cache ) {
441
				$envelopes['last_response_time'] = time();
442
443
				set_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ), $envelopes, $expiration );
444
			}
445
		}
446
447
		$hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' );
448
		unset( $envelopes['last_response_time'] );
449
450
		/**
451
		 * Allow adding your own custom JITMs after a set of JITMs has been received.
452
		 *
453
		 * @since 6.9.0
454
		 * @since 8.3.0 - Added Message path.
455
		 *
456
		 * @param array  $envelopes    array of existing JITMs.
457
		 * @param string $message_path The message path to ask for.
458
		 */
459
		$envelopes = apply_filters( 'jetpack_jitm_received_envelopes', $envelopes, $message_path );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $message_path.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
460
461
		foreach ( $envelopes as $idx => &$envelope ) {
462
463
			$dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null;
464
465
			// If the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed.
466
			if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) {
467
				unset( $envelopes[ $idx ] );
468
				continue;
469
			}
470
471
			$this->tracking->record_user_event(
472
				'jitm_view_client',
473
				array(
474
					'jitm_id' => $envelope->id,
475
				)
476
			);
477
478
			$url_params = array(
479
				'source' => "jitm-$envelope->id",
480
				'site'   => ( new Status() )->get_site_suffix(),
481
				'u'      => $user->ID,
482
			);
483
484
			// Get affiliate code and add it to the array of URL parameters.
485
			$aff = Partner::init()->get_partner_code( Partner::AFFILIATE_CODE );
486
			if ( '' !== $aff ) {
487
				$url_params['aff'] = $aff;
488
			}
489
490
			// Check if the current user has connected their WP.com account
491
			// and if not add this information to the the array of URL parameters.
492
			if ( ! ( new Manager() )->is_user_connected( $user->ID ) ) {
493
				$url_params['unlinked'] = 1;
494
			}
495
			$envelope->url = add_query_arg( $url_params, 'https://jetpack.com/redirect/' );
496
497
			$stats = new A8c_Mc_Stats();
498
499
			$envelope->jitm_stats_url = $stats->build_stats_url( array( 'x_jetpack-jitm' => $envelope->id ) );
500
501
			// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
502
			// $CTA is not valid per PHPCS, but it is part of the return from WordPress.com, so allowing.
503
			if ( $envelope->CTA->hook ) {
504
				$envelope->url = apply_filters( 'jitm_' . $envelope->CTA->hook, $envelope->url );
505
				unset( $envelope->CTA->hook );
506
			}
507
			// phpcs:enable
508
509
			if ( isset( $envelope->content->hook ) ) {
510
				$envelope->content = apply_filters( 'jitm_' . $envelope->content->hook, $envelope->content );
511
				unset( $envelope->content->hook );
512
			}
513
514
			// No point in showing an empty message.
515
			if ( empty( $envelope->content->message ) ) {
516
				unset( $envelopes[ $idx ] );
517
				continue;
518
			}
519
520
			$envelope->content->icon = $this->generate_icon( $envelope->content->icon, $full_jp_logo_exists );
521
522
			$stats->add( 'jitm', $envelope->id . '-viewed' );
523
			$stats->do_server_side_stats();
524
		}
525
526
		return $envelopes;
527
	}
528
529
}
530