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