Completed
Push — update/add_grunion_after_feedb... ( 614f06...9f6c1a )
by
unknown
07:04
created

Post_Connection_JITM   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 458
Duplicated Lines 1.09 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 5
loc 458
rs 8.64
c 0
b 0
f 0
wmc 47
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A prepare_jitms() 0 14 2
A jitm_woocommerce_services_msg() 0 20 4
A jitm_jetpack_woo_services_install() 0 11 1
A jitm_jetpack_woo_services_activate() 0 11 1
C delete_user_update_connection_owner_notice() 5 161 14
A dismiss() 0 11 1
F get_messages() 0 157 23

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Post_Connection_JITM often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Post_Connection_JITM, and based on these observations, apply Extract Interface, too.

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
		$this->save_dismiss( $feature_class );
307
		return true;
308
	}
309
310
	/**
311
	 * Asks the wpcom API for the current message to display keyed on query string and message path
312
	 *
313
	 * @param string $message_path The message path to ask for.
314
	 * @param string $query The query string originally from the front end.
315
	 * @param bool   $full_jp_logo_exists If there is a full Jetpack logo already on the page.
316
	 *
317
	 * @return array The JITM's to show, or an empty array if there is nothing to show
318
	 */
319
	public function get_messages( $message_path, $query, $full_jp_logo_exists ) {
320
		// Custom filters go here.
321
		add_filter( 'jitm_woocommerce_services_msg', array( $this, 'jitm_woocommerce_services_msg' ) );
322
		add_filter( 'jitm_jetpack_woo_services_install', array( $this, 'jitm_jetpack_woo_services_install' ) );
323
		add_filter( 'jitm_jetpack_woo_services_activate', array( $this, 'jitm_jetpack_woo_services_activate' ) );
324
325
		$user = wp_get_current_user();
326
327
		// Unauthenticated or invalid requests just bail.
328
		if ( ! $user ) {
329
			return array();
330
		}
331
332
		$user_roles = implode( ',', $user->roles );
333
		$site_id    = \Jetpack_Options::get_option( 'id' );
334
335
		// Build our jitm request.
336
		$path = add_query_arg(
337
			array(
338
				'external_user_id' => urlencode_deep( $user->ID ),
339
				'user_roles'       => urlencode_deep( $user_roles ),
340
				'query_string'     => urlencode_deep( $query ),
341
				'mobile_browser'   => jetpack_is_mobile( 'smart' ) ? 1 : 0,
342
				'_locale'          => get_user_locale(),
343
			),
344
			sprintf( '/sites/%d/jitm/%s', $site_id, $message_path )
345
		);
346
347
		// Attempt to get from cache.
348
		$envelopes = get_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ) );
349
350
		// If something is in the cache and it was put in the cache after the last sync we care about, use it.
351
		$use_cache = false;
352
353
		/** This filter is documented in class.jetpack.php */
354
		if ( apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) {
355
			$use_cache = true;
356
		}
357
358
		if ( $use_cache ) {
359
			$last_sync  = (int) get_transient( 'jetpack_last_plugin_sync' );
360
			$from_cache = $envelopes && $last_sync > 0 && $last_sync < $envelopes['last_response_time'];
361
		} else {
362
			$from_cache = false;
363
		}
364
365
		// Otherwise, ask again.
366
		if ( ! $from_cache ) {
367
			$wpcom_response = Client::wpcom_json_api_request_as_blog(
368
				$path,
369
				'2',
370
				array(
371
					'user_id'    => $user->ID,
372
					'user_roles' => implode( ',', $user->roles ),
373
				),
374
				null,
375
				'wpcom'
376
			);
377
378
			// silently fail...might be helpful to track it?
379
			if ( is_wp_error( $wpcom_response ) ) {
380
				return array();
381
			}
382
383
			$envelopes = json_decode( $wpcom_response['body'] );
384
385
			if ( ! is_array( $envelopes ) ) {
386
				return array();
387
			}
388
389
			$expiration = isset( $envelopes[0] ) ? $envelopes[0]->ttl : 300;
390
391
			// Do not cache if expiration is 0 or we're not using the cache.
392
			if ( 0 !== $expiration && $use_cache ) {
393
				$envelopes['last_response_time'] = time();
394
395
				set_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ), $envelopes, $expiration );
396
			}
397
		}
398
399
		$hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' );
400
		unset( $envelopes['last_response_time'] );
401
402
		/**
403
		 * Allow adding your own custom JITMs after a set of JITMs has been received.
404
		 *
405
		 * @since 6.9.0
406
		 * @since 8.3.0 - Added Message path.
407
		 *
408
		 * @param array  $envelopes    array of existing JITMs.
409
		 * @param string $message_path The message path to ask for.
410
		 */
411
		$envelopes = apply_filters( 'jetpack_jitm_received_envelopes', $envelopes, $message_path );
412
413
		foreach ( $envelopes as $idx => &$envelope ) {
414
415
			$dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null;
416
417
			// If the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed.
418
			if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) {
419
				unset( $envelopes[ $idx ] );
420
				continue;
421
			}
422
423
			$this->tracking->record_user_event(
424
				'jitm_view_client',
425
				array(
426
					'jitm_id' => $envelope->id,
427
				)
428
			);
429
430
			$normalized_site_url = \Jetpack::build_raw_urls( get_home_url() );
431
432
			$url_params = array(
433
				'source' => "jitm-$envelope->id",
434
				'site'   => $normalized_site_url,
435
				'u'      => $user->ID,
436
			);
437
438
			// Get affiliate code and add it to the array of URL parameters.
439
			$aff = Partner::init()->get_partner_code( Partner::AFFILIATE_CODE );
440
			if ( '' !== $aff ) {
441
				$url_params['aff'] = $aff;
442
			}
443
444
			$envelope->url = add_query_arg( $url_params, 'https://jetpack.com/redirect/' );
445
446
			$envelope->jitm_stats_url = \Jetpack::build_stats_url( array( 'x_jetpack-jitm' => $envelope->id ) );
447
448
			// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
449
			// $CTA is not valid per PHPCS, but it is part of the return from WordPress.com, so allowing.
450
			if ( $envelope->CTA->hook ) {
451
				$envelope->url = apply_filters( 'jitm_' . $envelope->CTA->hook, $envelope->url );
452
				unset( $envelope->CTA->hook );
453
			}
454
			// phpcs:enable
455
456
			if ( isset( $envelope->content->hook ) ) {
457
				$envelope->content = apply_filters( 'jitm_' . $envelope->content->hook, $envelope->content );
458
				unset( $envelope->content->hook );
459
			}
460
461
			// No point in showing an empty message.
462
			if ( empty( $envelope->content->message ) ) {
463
				unset( $envelopes[ $idx ] );
464
				continue;
465
			}
466
467
			$envelope->content->icon = $this->generate_icon( $envelope->content->icon, $full_jp_logo_exists );
468
469
			$jetpack = \Jetpack::init();
470
			$jetpack->stat( 'jitm', $envelope->id . '-viewed-' . JETPACK__VERSION );
471
			$jetpack->do_stats( 'server_side' );
472
		}
473
474
		return $envelopes;
475
	}
476
477
}
478