Completed
Push — add/implement_pre_connection_j... ( c6d296 )
by
unknown
06:35
created

jitm_woocommerce_services_msg()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 20
rs 9.6
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\Connection\Client;
11
use Automattic\Jetpack\Partner;
12
use Automattic\Jetpack\Tracking;
13
use Automattic\Jetpack\JITMS\JITM;
14
15
/**
16
 * Jetpack just in time messaging through out the admin
17
 *
18
 * @since 5.6.0
19
 */
20
class Post_Connection_JITM extends JITM {
21
22
	/**
23
	 * Tracking object.
24
	 *
25
	 * @var Automattic\Jetpack\Tracking
26
	 *
27
	 * @access private
28
	 */
29
	public $tracking;
30
31
	/**
32
	 * JITM constructor.
33
	 */
34
	public function __construct() {
35
		$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...
36
	}
37
38
	/**
39
	 * Prepare actions according to screen and post type.
40
	 *
41
	 * @since 3.8.2
42
	 *
43
	 * @uses Jetpack_Autoupdate::get_possible_failures()
44
	 *
45
	 * @param \WP_Screen $screen WP Core's screen object.
46
	 */
47
	public function prepare_jitms( $screen ) {
48
		parent::prepare_jitms( $screen );
49
		if ( ! in_array(
50
			$screen->id,
51
			array(
52
				'jetpack_page_akismet-key-config',
53
				'admin_page_jetpack_modules',
54
			),
55
			true
56
		) ) {
57
			// Not really a JITM. Don't know where else to put this :) .
58
			add_action( 'admin_notices', array( $this, 'delete_user_update_connection_owner_notice' ) );
59
		}
60
	}
61
62
	/**
63
	 * A special filter for WooCommerce, to set a message based on local state.
64
	 *
65
	 * @param string $content The current message.
66
	 *
67
	 * @return array The new message.
68
	 */
69
	public static function jitm_woocommerce_services_msg( $content ) {
70
		if ( ! function_exists( 'wc_get_base_location' ) ) {
71
			return $content;
72
		}
73
74
		$base_location = wc_get_base_location();
75
76
		switch ( $base_location['country'] ) {
77
			case 'US':
78
				$content->message = esc_html__( 'New free service: Show USPS shipping rates on your store! Added bonus: print shipping labels without leaving WooCommerce.', 'jetpack' );
79
				break;
80
			case 'CA':
81
				$content->message = esc_html__( 'New free service: Show Canada Post shipping rates on your store!', 'jetpack' );
82
				break;
83
			default:
84
				$content->message = '';
85
		}
86
87
		return $content;
88
	}
89
90
	/**
91
	 * A special filter for WooCommerce Call To Action button
92
	 *
93
	 * @return string The new CTA
94
	 */
95
	public static function jitm_jetpack_woo_services_install() {
96
		return wp_nonce_url(
97
			add_query_arg(
98
				array(
99
					'wc-services-action' => 'install',
100
				),
101
				admin_url( 'admin.php?page=wc-settings' )
102
			),
103
			'wc-services-install'
104
		);
105
	}
106
107
	/**
108
	 * A special filter for WooCommerce Call To Action button.
109
	 *
110
	 * @return string The new CTA
111
	 */
112
	public static function jitm_jetpack_woo_services_activate() {
113
		return wp_nonce_url(
114
			add_query_arg(
115
				array(
116
					'wc-services-action' => 'activate',
117
				),
118
				admin_url( 'admin.php?page=wc-settings' )
119
			),
120
			'wc-services-install'
121
		);
122
	}
123
124
	/**
125
	 * This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
126
	 * the connection owner.
127
	 */
128
	public function delete_user_update_connection_owner_notice() {
129
		global $current_screen;
130
131
		/*
132
		 * phpcs:disable WordPress.Security.NonceVerification.Recommended
133
		 *
134
		 * This function is firing within wp-admin and checks (below) if it is in the midst of a deletion on the users
135
		 * page. Nonce will be already checked by WordPress, so we do not need to check ourselves.
136
		 */
137
138
		if ( ! isset( $current_screen->base ) || 'users' !== $current_screen->base ) {
139
			return;
140
		}
141
142
		if ( ! isset( $_REQUEST['action'] ) || 'delete' !== $_REQUEST['action'] ) {
143
			return;
144
		}
145
146
		// Get connection owner or bail.
147
		$connection_manager  = new Manager();
148
		$connection_owner_id = $connection_manager->get_connection_owner_id();
149
		if ( ! $connection_owner_id ) {
150
			return;
151
		}
152
		$connection_owner_userdata = get_userdata( $connection_owner_id );
153
154
		// Bail if we're not trying to delete connection owner.
155
		$user_ids_to_delete = array();
156 View Code Duplication
		if ( isset( $_REQUEST['users'] ) ) {
157
			$user_ids_to_delete = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['users'] ) );
158
		} elseif ( isset( $_REQUEST['user'] ) ) {
159
			$user_ids_to_delete[] = sanitize_text_field( wp_unslash( $_REQUEST['user'] ) );
160
		}
161
162
		// phpcs:enable
163
		$user_ids_to_delete        = array_map( 'absint', $user_ids_to_delete );
164
		$deleting_connection_owner = in_array( $connection_owner_id, (array) $user_ids_to_delete, true );
165
		if ( ! $deleting_connection_owner ) {
166
			return;
167
		}
168
169
		// Bail if they're trying to delete themselves to avoid confusion.
170
		if ( get_current_user_id() === $connection_owner_id ) {
171
			return;
172
		}
173
174
		// Track it!
175
		if ( method_exists( $this->tracking, 'record_user_event' ) ) {
176
			$this->tracking->record_user_event( 'delete_connection_owner_notice_view' );
177
		}
178
179
		$connection_manager = new Manager();
180
		$connected_admins   = $connection_manager->get_connected_users( 'jetpack_disconnect' );
181
		$user               = is_a( $connection_owner_userdata, 'WP_User' ) ? esc_html( $connection_owner_userdata->data->user_login ) : '';
182
183
		echo "<div class='notice notice-warning' id='jetpack-notice-switch-connection-owner'>";
184
		echo '<h2>' . esc_html__( 'Important notice about your Jetpack connection:', 'jetpack' ) . '</h2>';
185
		echo '<p>' . sprintf(
186
			/* translators: WordPress User, if available. */
187
			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' ),
188
			esc_html( $user )
189
		) . '</p>';
190
191
		if ( ! empty( $connected_admins ) && count( $connected_admins ) > 1 ) {
192
			echo '<form id="jp-switch-connection-owner" action="" method="post">';
193
			echo "<label for='owner'>" . esc_html__( 'You can choose to transfer connection ownership to one of these already-connected admins:', 'jetpack' ) . ' </label>';
194
195
			$connected_admin_ids = array_map(
196
				function( $connected_admin ) {
197
						return $connected_admin->ID;
198
				},
199
				$connected_admins
200
			);
201
202
			wp_dropdown_users(
203
				array(
204
					'name'    => 'owner',
205
					'include' => array_diff( $connected_admin_ids, array( $connection_owner_id ) ),
206
					'show'    => 'display_name_with_login',
207
				)
208
			);
209
210
			echo '<p>';
211
			submit_button( esc_html__( 'Set new connection owner', 'jetpack' ), 'primary', 'jp-switch-connection-owner-submit', false );
212
			echo '</p>';
213
214
			echo "<div id='jp-switch-user-results'></div>";
215
			echo '</form>';
216
			?>
217
			<script type="text/javascript">
218
				jQuery( document ).ready( function( $ ) {
219
					$( '#jp-switch-connection-owner' ).on( 'submit', function( e ) {
220
						var formData = $( this ).serialize();
221
						var submitBtn = document.getElementById( 'jp-switch-connection-owner-submit' );
222
						var results = document.getElementById( 'jp-switch-user-results' );
223
224
						submitBtn.disabled = true;
225
226
						$.ajax( {
227
							type        : "POST",
228
							url         : "<?php echo esc_url( get_rest_url() . 'jetpack/v4/connection/owner' ); ?>",
229
							data        : formData,
230
							headers     : {
231
								'X-WP-Nonce': "<?php echo esc_js( wp_create_nonce( 'wp_rest' ) ); ?>",
232
							},
233
							success: function() {
234
								results.innerHTML = "<?php esc_html_e( 'Success!', 'jetpack' ); ?>";
235
								setTimeout( function() {
236
									$( '#jetpack-notice-switch-connection-owner' ).hide( 'slow' );
237
								}, 1000 );
238
							}
239
						} ).done( function() {
240
							submitBtn.disabled = false;
241
						} );
242
243
						e.preventDefault();
244
						return false;
245
					} );
246
				} );
247
			</script>
248
			<?php
249
		} else {
250
			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>';
251
			$connect_url = \Jetpack::init()->build_connect_url( false, false, 'delete_connection_owner_notice' );
252
			echo "<a href='" . esc_url( $connect_url ) . "' target='_blank' rel='noopener noreferrer' class='button-primary'>" . esc_html__( 'Connect to WordPress.com', 'jetpack' ) . '</a>';
253
		}
254
255
		echo '<p>';
256
		printf(
257
			wp_kses(
258
				/* translators: URL to Jetpack support doc regarding the primary user. */
259
				__( "<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' ),
260
				array(
261
					'a' => array(
262
						'href'   => true,
263
						'target' => true,
264
						'rel'    => true,
265
					),
266
				)
267
			),
268
			esc_url( Redirect::get_url( 'jetpack-support-primary-user' ) )
269
		);
270
		echo '</p>';
271
		echo '<p>';
272
		printf(
273
			wp_kses(
274
				/* translators: URL to contact Jetpack support. */
275
				__( 'As always, feel free to <a href="%s" target="_blank" rel="noopener noreferrer">contact our support team</a> if you have any questions.', 'jetpack' ),
276
				array(
277
					'a' => array(
278
						'href'   => true,
279
						'target' => true,
280
						'rel'    => true,
281
					),
282
				)
283
			),
284
			esc_url( Redirect::get_url( 'jetpack-contact-support' ) )
285
		);
286
		echo '</p>';
287
		echo '</div>';
288
	}
289
290
	/**
291
	 * Dismisses a JITM feature class so that it will no longer be shown.
292
	 *
293
	 * @param string $id The id of the JITM that was dismissed.
294
	 * @param string $feature_class The feature class of the JITM that was dismissed.
295
	 *
296
	 * @return bool Always true.
297
	 */
298
	public function dismiss( $id, $feature_class ) {
299
		$this->tracking->record_user_event(
300
			'jitm_dismiss_client',
301
			array(
302
				'jitm_id'       => $id,
303
				'feature_class' => $feature_class,
304
			)
305
		);
306
307
		$hide_jitm = \Jetpack_Options::get_option( 'hide_jitm' );
308
		if ( ! is_array( $hide_jitm ) ) {
309
			$hide_jitm = array();
310
		}
311
312
		if ( isset( $hide_jitm[ $feature_class ] ) ) {
313
			if ( ! is_array( $hide_jitm[ $feature_class ] ) ) {
314
				$hide_jitm[ $feature_class ] = array(
315
					'last_dismissal' => 0,
316
					'number'         => 0,
317
				);
318
			}
319
		} else {
320
			$hide_jitm[ $feature_class ] = array(
321
				'last_dismissal' => 0,
322
				'number'         => 0,
323
			);
324
		}
325
326
		$number = $hide_jitm[ $feature_class ]['number'];
327
328
		$hide_jitm[ $feature_class ] = array(
329
			'last_dismissal' => time(),
330
			'number'         => $number + 1,
331
		);
332
333
		\Jetpack_Options::update_option( 'hide_jitm', $hide_jitm );
334
335
		return true;
336
	}
337
338
	/**
339
	 * Asks the wpcom API for the current message to display keyed on query string and message path
340
	 *
341
	 * @param string $message_path The message path to ask for.
342
	 * @param string $query The query string originally from the front end.
343
	 * @param bool   $full_jp_logo_exists If there is a full Jetpack logo already on the page.
344
	 *
345
	 * @return array The JITM's to show, or an empty array if there is nothing to show
346
	 */
347
	public function get_messages( $message_path, $query, $full_jp_logo_exists ) {
348
		// Custom filters go here.
349
		add_filter( 'jitm_woocommerce_services_msg', array( $this, 'jitm_woocommerce_services_msg' ) );
350
		add_filter( 'jitm_jetpack_woo_services_install', array( $this, 'jitm_jetpack_woo_services_install' ) );
351
		add_filter( 'jitm_jetpack_woo_services_activate', array( $this, 'jitm_jetpack_woo_services_activate' ) );
352
353
		$user = wp_get_current_user();
354
355
		// Unauthenticated or invalid requests just bail.
356
		if ( ! $user ) {
357
			return array();
358
		}
359
360
		$user_roles = implode( ',', $user->roles );
361
		$site_id    = \Jetpack_Options::get_option( 'id' );
362
363
		// Build our jitm request.
364
		$path = add_query_arg(
365
			array(
366
				'external_user_id' => urlencode_deep( $user->ID ),
367
				'user_roles'       => urlencode_deep( $user_roles ),
368
				'query_string'     => urlencode_deep( $query ),
369
				'mobile_browser'   => jetpack_is_mobile( 'smart' ) ? 1 : 0,
370
				'_locale'          => get_user_locale(),
371
			),
372
			sprintf( '/sites/%d/jitm/%s', $site_id, $message_path )
373
		);
374
375
		// Attempt to get from cache.
376
		$envelopes = get_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ) );
377
378
		// If something is in the cache and it was put in the cache after the last sync we care about, use it.
379
		$use_cache = false;
380
381
		/** This filter is documented in class.jetpack.php */
382
		if ( apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) {
383
			$use_cache = true;
384
		}
385
386
		if ( $use_cache ) {
387
			$last_sync  = (int) get_transient( 'jetpack_last_plugin_sync' );
388
			$from_cache = $envelopes && $last_sync > 0 && $last_sync < $envelopes['last_response_time'];
389
		} else {
390
			$from_cache = false;
391
		}
392
393
		// Otherwise, ask again.
394
		if ( ! $from_cache ) {
395
			$wpcom_response = Client::wpcom_json_api_request_as_blog(
396
				$path,
397
				'2',
398
				array(
399
					'user_id'    => $user->ID,
400
					'user_roles' => implode( ',', $user->roles ),
401
				),
402
				null,
403
				'wpcom'
404
			);
405
406
			// silently fail...might be helpful to track it?
407
			if ( is_wp_error( $wpcom_response ) ) {
408
				return array();
409
			}
410
411
			$envelopes = json_decode( $wpcom_response['body'] );
412
413
			if ( ! is_array( $envelopes ) ) {
414
				return array();
415
			}
416
417
			$expiration = isset( $envelopes[0] ) ? $envelopes[0]->ttl : 300;
418
419
			// Do not cache if expiration is 0 or we're not using the cache.
420
			if ( 0 !== $expiration && $use_cache ) {
421
				$envelopes['last_response_time'] = time();
422
423
				set_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ), $envelopes, $expiration );
424
			}
425
		}
426
427
		$hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' );
428
		unset( $envelopes['last_response_time'] );
429
430
		/**
431
		 * Allow adding your own custom JITMs after a set of JITMs has been received.
432
		 *
433
		 * @since 6.9.0
434
		 * @since 8.3.0 - Added Message path.
435
		 *
436
		 * @param array  $envelopes    array of existing JITMs.
437
		 * @param string $message_path The message path to ask for.
438
		 */
439
		$envelopes = apply_filters( 'jetpack_jitm_received_envelopes', $envelopes, $message_path );
440
441
		foreach ( $envelopes as $idx => &$envelope ) {
442
443
			$dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null;
444
445
			// If the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed.
446
			if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) {
447
				unset( $envelopes[ $idx ] );
448
				continue;
449
			}
450
451
			$this->tracking->record_user_event(
452
				'jitm_view_client',
453
				array(
454
					'jitm_id' => $envelope->id,
455
				)
456
			);
457
458
			$normalized_site_url = \Jetpack::build_raw_urls( get_home_url() );
459
460
			$url_params = array(
461
				'source' => "jitm-$envelope->id",
462
				'site'   => $normalized_site_url,
463
				'u'      => $user->ID,
464
			);
465
466
			// Get affiliate code and add it to the array of URL parameters.
467
			$aff = Partner::init()->get_partner_code( Partner::AFFILIATE_CODE );
468
			if ( '' !== $aff ) {
469
				$url_params['aff'] = $aff;
470
			}
471
472
			$envelope->url = add_query_arg( $url_params, 'https://jetpack.com/redirect/' );
473
474
			$envelope->jitm_stats_url = \Jetpack::build_stats_url( array( 'x_jetpack-jitm' => $envelope->id ) );
475
476
			// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
477
			// $CTA is not valid per PHPCS, but it is part of the return from WordPress.com, so allowing.
478
			if ( $envelope->CTA->hook ) {
479
				$envelope->url = apply_filters( 'jitm_' . $envelope->CTA->hook, $envelope->url );
480
				unset( $envelope->CTA->hook );
481
			}
482
			// phpcs:enable
483
484
			if ( isset( $envelope->content->hook ) ) {
485
				$envelope->content = apply_filters( 'jitm_' . $envelope->content->hook, $envelope->content );
486
				unset( $envelope->content->hook );
487
			}
488
489
			// No point in showing an empty message.
490
			if ( empty( $envelope->content->message ) ) {
491
				unset( $envelopes[ $idx ] );
492
				continue;
493
			}
494
495
			$envelope->content->icon = $this->generate_icon( $envelope->content->icon, $full_jp_logo_exists );
496
497
			$jetpack = \Jetpack::init();
498
			$jetpack->stat( 'jitm', $envelope->id . '-viewed-' . JETPACK__VERSION );
499
			$jetpack->do_stats( 'server_side' );
500
		}
501
502
		return $envelopes;
503
	}
504
505
}
506