Completed
Push — add/idc-migrate-endpoint ( 89d760...9d6e66 )
by
unknown
210:39 queued 203:42
created

Jetpack_IDC::wordpress_init()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 5
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This class will handle everything involved with fixing an Identity Crisis.
5
 *
6
 * @since 4.4.0
7
 */
8
class Jetpack_IDC {
9
10
	/**
11
	 * @var Jetpack_IDC
12
	 **/
13
	private static $instance = null;
14
15
	/**
16
	 * The wpcom value of the home URL
17
	 * @var string
18
	 */
19
	static $wpcom_home_url;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $wpcom_home_url.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
20
21
	/**
22
	 * Has safe mode been confirmed?
23
	 * @var bool
24
	 */
25
	static $is_safe_mode_confirmed;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_safe_mode_confirmed.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
26
27
	/**
28
	 * The link to the support document used to explain Safe Mode to users
29
	 * @var string
30
	 */
31
	const SAFE_MODE_DOC_LINK = 'https://jetpack.com/support/safe-mode';
32
33
	static function init() {
34
		if ( is_null( self::$instance ) ) {
35
			self::$instance = new Jetpack_IDC;
36
		}
37
38
		return self::$instance;
39
	}
40
41
	private function __construct() {
42
		add_action( 'jetpack_sync_processed_actions', array( $this, 'maybe_clear_migrate_option' ) );
43
44
		if ( false === $urls_in_crisis = Jetpack::check_identity_crisis() ) {
45
			return;
46
		}
47
48
		self::$wpcom_home_url = $urls_in_crisis['wpcom_home'];
49
		add_action( 'init', array( $this, 'wordpress_init' ) );
50
	}
51
52
	/**
53
	 * This method loops through the array of processed items from sync and checks if one of the items was the
54
	 * home_url or site_url callable. If so, then we delete the jetpack_migrate_for_idc option.
55
	 *
56
	 * @param $processed_items array Array of processed items that were synced to WordPress.com
57
	 */
58
	function maybe_clear_migrate_option( $processed_items ) {
59
		foreach ( (array) $processed_items as $item ) {
60
61
			// First, is this item a jetpack_sync_callable action? If so, then proceed.
62
			$callable_args = ( is_array( $item ) && isset( $item[0], $item[1] ) && 'jetpack_sync_callable' === $item[0] )
63
				? $item[1]
64
				: null;
65
66
			// Second, if $callable_args is set, check if the callable was home_url or site_url. If so,
67
			// clear the migrate option.
68
			if (
69
				isset( $callable_args, $callable_args[0] )
70
				&& ( 'home_url' === $callable_args[0] || 'site_url' === $callable_args[1] )
71
			) {
72
				Jetpack_Options::delete_option( 'migrate_for_idc' );
73
				break;
74
			}
75
		}
76
	}
77
78
	function wordpress_init() {
79
		if ( ! current_user_can( 'jetpack_disconnect' ) ) {
80
			return;
81
		}
82
83
		if (
84
			isset( $_GET['jetpack_idc_clear_confirmation'], $_GET['_wpnonce'] ) &&
85
			wp_verify_nonce( $_GET['_wpnonce'], 'jetpack_idc_clear_confirmation' )
86
		) {
87
			Jetpack_Options::delete_option( 'safe_mode_confirmed' );
88
			self::$is_safe_mode_confirmed = false;
89
		} else {
90
			self::$is_safe_mode_confirmed = (bool) Jetpack_Options::get_option( 'safe_mode_confirmed' );
91
		}
92
93
		// 121 Priority so that it's the most inner Jetpack item in the admin bar.
94
		add_action( 'admin_bar_menu', array( $this, 'display_admin_bar_button' ), 121 );
95
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_bar_css' ) );
96
97
		if ( is_admin() && ! self::$is_safe_mode_confirmed ) {
98
			add_action( 'admin_notices', array( $this, 'display_idc_notice' ) );
99
			add_action( 'admin_enqueue_scripts', array( $this,'enqueue_idc_notice_files' ) );
100
		}
101
	}
102
103
	function display_admin_bar_button() {
104
		global $wp_admin_bar;
105
106
		$href = is_admin()
107
			? add_query_arg( 'jetpack_idc_clear_confirmation', '1' )
108
			: add_query_arg( 'jetpack_idc_clear_confirmation', '1', admin_url() );
109
110
		$href = wp_nonce_url( $href, 'jetpack_idc_clear_confirmation' );
111
112
		$title = sprintf(
113
			'<span class="jp-idc-admin-bar">%s %s</span>',
114
			'<span class="dashicons dashicons-warning"></span>',
115
			esc_html__( 'Jetpack Safe Mode', 'jetpack' )
116
		);
117
118
		$menu = array(
119
			'id'     => 'jetpack-idc',
120
			'title'  => $title,
121
			'href'   => esc_url( $href ),
122
			'parent' => 'top-secondary',
123
		);
124
125
		if ( ! self::$is_safe_mode_confirmed ) {
126
			$menu['meta'] = array(
127
				'class' => 'hide'
128
			);
129
		}
130
131
		$wp_admin_bar->add_node( $menu );
132
	}
133
134
	static function prepare_url_for_display( $url ) {
135
		return untrailingslashit( Jetpack::normalize_url_protocol_agnostic( $url ) );
136
	}
137
138
	/**
139
	 * First "step" of the IDC mitigation. Will provide some messaging and two options/buttons.
140
	 * "Confirm Staging" - Dismiss the notice and continue on with our lives in staging mode.
141
	 * "Fix Jetpack Connection" - Will disconnect the site and start the mitigation...
142
	 */
143
	function display_idc_notice() { ?>
144
		<div class="jp-idc-notice notice notice-warning">
145
			<div class="jp-idc-notice__header">
146
				<div class="jp-idc-notice__header__emblem">
147
					<?php echo Jetpack::get_jp_emblem(); ?>
148
				</div>
149
				<p class="jp-idc-notice__header__text">
150
					<?php esc_html_e( 'Jetpack Safe Mode', 'jetpack' ); ?>
151
				</p>
152
			</div>
153
154
			<div class="jp-idc-notice__separator"></div>
155
156
			<?php $this->render_notice_first_step(); ?>
157
			<?php $this->render_notice_second_step(); ?>
158
		</div>
159
	<?php }
160
161
	function enqueue_admin_bar_css() {
162
		wp_enqueue_style(
163
			'jetpack-idc-admin-bar-css',
164
			plugins_url( 'css/jetpack-idc-admin-bar.css', JETPACK__PLUGIN_FILE ),
165
			array( 'dashicons' ),
166
			JETPACK__VERSION
167
		);
168
	}
169
170
	/**
171
	 * Enqueue scripts for the notice
172
	 */
173
	function enqueue_idc_notice_files() {
174
175
		wp_enqueue_script(
176
			'jetpack-idc-js',
177
			plugins_url( '_inc/idc-notice.js', JETPACK__PLUGIN_FILE ),
178
			array( 'jquery' ),
179
			JETPACK__VERSION,
180
			true
181
		);
182
183
		wp_localize_script(
184
			'jetpack-idc-js',
185
			'idcL10n',
186
			array(
187
				'apiRoot' => esc_url_raw( rest_url() ),
188
				'nonce' => wp_create_nonce( 'wp_rest' ),
189
				'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
190
				'currentUrl' => remove_query_arg( '_wpnonce', remove_query_arg( 'jetpack_idc_clear_confirmation' ) ),
191
			)
192
		);
193
194
		wp_register_style(
195
			'jetpack-dops-style',
196
			plugins_url( '_inc/build/admin.dops-style.css', JETPACK__PLUGIN_FILE ),
197
			array(),
198
			JETPACK__VERSION
199
		);
200
201
		wp_enqueue_style(
202
			'jetpack-idc-css',
203
			plugins_url( 'css/jetpack-idc.css', JETPACK__PLUGIN_FILE ),
204
			array( 'jetpack-dops-style' ),
205
			JETPACK__VERSION
206
		);
207
208
		// Required for Tracks
209
		wp_enqueue_script(
210
			'jp-tracks',
211
			'//stats.wp.com/w.js',
212
			array(),
213
			gmdate( 'YW' ),
214
			true
215
		);
216
217
		wp_enqueue_script(
218
			'jp-tracks-functions',
219
			plugins_url( '_inc/lib/tracks/tracks-callables.js', JETPACK__PLUGIN_FILE ),
220
			array(),
221
			JETPACK__VERSION,
222
			false
223
		);
224
	}
225
226
	function render_notice_first_step() { ?>
227
		<div class="jp-idc-notice__first-step">
228
			<div class="jp-idc-notice__content-header">
229
				<h3 class="jp-idc-notice__content-header__lead">
230
					<?php echo $this->get_first_step_header_lead(); ?>
231
				</h3>
232
233
				<p class="jp-idc-notice__content-header__explanation">
234
					<?php echo $this->get_first_step_header_explanation(); ?>
235
				</p>
236
			</div>
237
238
			<div class="jp-idc-notice__actions">
239
				<div class="jp-idc-notice__action">
240
					<p class="jp-idc-notice__action__explanation">
241
						<?php echo $this->get_confirm_safe_mode_action_explanation(); ?>
242
					</p>
243
					<button id="jp-idc-confirm-safe-mode-action" class="dops-button">
244
						<?php echo $this->get_confirm_safe_mode_button_text(); ?>
245
					</button>
246
				</div>
247
248
				<div class="jp-idc-notice__action">
249
					<p class="jp-idc-notice__action__explanation">
250
						<?php echo $this->get_first_step_fix_connection_action_explanation(); ?>
251
					</p>
252
					<button id="jp-idc-fix-connection-action" class="dops-button">
253
						<?php echo $this->get_first_step_fix_connection_button_text(); ?>
254
					</button>
255
				</div>
256
			</div>
257
		</div>
258
	<?php }
259
260
	function render_notice_second_step() { ?>
261
		<div class="jp-idc-notice__second-step">
262
			<div class="jp-idc-notice__content-header">
263
				<h3 class="jp-idc-notice__content-header__lead">
264
					<?php echo $this->get_second_step_header_lead(); ?>
265
				</h3>
266
			</div>
267
268
			<div class="jp-idc-notice__actions">
269
				<div class="jp-idc-notice__action">
270
					<p class="jp-idc-notice__action__explanation">
271
						<?php echo $this->get_migrate_site_action_explanation(); ?>
272
					</p>
273
					<button id="jp-idc-migrate-action" class="dops-button">
274
						<?php echo $this->get_migrate_site_button_text(); ?>
275
					</button>
276
				</div>
277
278
				<div class="jp-idc-notice__action">
279
					<p class="jp-idc-notice__action__explanation">
280
						<?php echo $this->get_start_fresh_action_explanation(); ?>
281
					</p>
282
					<button id="jp-idc-reconnect-site-action" class="dops-button">
283
						<?php echo $this->get_start_fresh_button_text(); ?>
284
					</button>
285
				</div>
286
287
			</div>
288
289
			<p class="jp-idc-notice__unsure-prompt">
290
				<?php echo $this->get_unsure_prompt(); ?>
291
			</p>
292
		</div>
293
	<?php }
294
295 View Code Duplication
	function get_first_step_header_lead() {
296
		$html = wp_kses(
297
			sprintf(
298
				__(
299
					'Jetpack has been placed into <a href="%1$s">Safe mode</a> because we noticed this is an exact copy of <a href="%2$s">%3$s</a>.',
300
					'jetpack'
301
				),
302
				esc_url( self::SAFE_MODE_DOC_LINK ),
303
				esc_url( self::$wpcom_home_url ),
304
				self::prepare_url_for_display( esc_url_raw( self::$wpcom_home_url ) )
305
			),
306
			array( 'a' => array( 'href' => array() ) )
307
		);
308
309
		/**
310
		 * Allows overriding of the default header text in the first step of the Safe Mode notice.
311
		 *
312
		 * @since 4.4.0
313
		 *
314
		 * @param string $html The HTML to be displayed
315
		 */
316
		return apply_filters( 'jetpack_idc_first_step_header_lead', $html );
317
	}
318
319 View Code Duplication
	function get_first_step_header_explanation() {
320
		$html = wp_kses(
321
			sprintf(
322
				__(
323
					'Please confirm Safe Mode or fix the Jetpack connection. Select one of the options below or <a href="%1$s">learn 
324
					more about Safe Mode</a>.',
325
					'jetpack'
326
				),
327
				esc_url( self::SAFE_MODE_DOC_LINK )
328
			),
329
			array( 'a' => array( 'href' => array() ) )
330
		);
331
332
		/**
333
		 * Allows overriding of the default header explanation text in the first step of the Safe Mode notice.
334
		 *
335
		 * @since 4.4.0
336
		 *
337
		 * @param string $html The HTML to be displayed
338
		 */
339
		return apply_filters( 'jetpack_idc_first_step_header_explanation', $html );
340
	}
341
342 View Code Duplication
	function get_confirm_safe_mode_action_explanation() {
343
		$html = wp_kses(
344
			sprintf(
345
				__(
346
					'Is this website a temporary duplicate of <a href="%1$s">%2$s</a> for the purposes 
347
					of testing, staging or development? If so, we recommend keeping it in Safe Mode.',
348
					'jetpack'
349
				),
350
				esc_url( untrailingslashit( self::$wpcom_home_url ) ),
351
				self::prepare_url_for_display( esc_url( self::$wpcom_home_url ) )
352
			),
353
			array( 'a' => array( 'href' => array() ) )
354
		);
355
356
		/**
357
		 * Allows overriding of the default text used to explain the confirm safe mode action.
358
		 *
359
		 * @since 4.4.0
360
		 *
361
		 * @param string $html The HTML to be displayed
362
		 */
363
		return apply_filters( 'jetpack_idc_confirm_safe_mode_explanation', $html );
364
	}
365
366
	function get_confirm_safe_mode_button_text() {
367
		$string =  esc_html__( 'Confirm Safe Mode', 'jetpack' );
368
369
		/**
370
		 * Allows overriding of the default text used for the confirm safe mode action button.
371
		 *
372
		 * @since 4.4.0
373
		 *
374
		 * @param string $string The string to be displayed
375
		 */
376
		return apply_filters( 'jetpack_idc_confirm_safe_mode_button_text', $string );
377
	}
378
379 View Code Duplication
	function get_first_step_fix_connection_action_explanation() {
380
		$html = wp_kses(
381
			sprintf(
382
				__(
383
					'If this is a separate and new website, or the new home of <a href="%1$s">%2$s</a>, 
384
					we recommend turning Safe Mode off, and re-establishing your connection to WordPress.com.',
385
					'jetpack'
386
				),
387
				esc_url( untrailingslashit( self::$wpcom_home_url ) ),
388
				self::prepare_url_for_display( esc_url( self::$wpcom_home_url ) )
389
			),
390
			array( 'a' => array( 'href' => array() ) )
391
		);
392
393
		/**
394
		 * Allows overriding of the default text used to explain the fix Jetpack connection action.
395
		 *
396
		 * @since 4.4.0
397
		 *
398
		 * @param string $html The HTML to be displayed
399
		 */
400
		return apply_filters( 'jetpack_idc_first_fix_connection_explanation', $html );
401
	}
402
403
	function get_first_step_fix_connection_button_text() {
404
		$string = esc_html__( "Fix Jetpack's Connection", 'jetpack' );
405
406
		/**
407
		 * Allows overriding of the default text used for the fix Jetpack connection action button.
408
		 *
409
		 * @since 4.4.0
410
		 *
411
		 * @param string $string The string to be displayed
412
		 */
413
		return apply_filters( 'jetpack_idc_first_step_fix_connection_button_text', $string );
414
	}
415
416
	function get_second_step_header_lead() {
417
		$string = sprintf(
418
			esc_html__(
419
				'Is %1$s the new home of %2$s?',
420
				'jetpack'
421
			),
422
			untrailingslashit( Jetpack::normalize_url_protocol_agnostic( get_home_url() ) ),
423
			untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) )
424
		);
425
426
		/**
427
		 * Allows overriding of the default header text in the second step of the Safe Mode notice.
428
		 *
429
		 * @since 4.4.0
430
		 *
431
		 * @param string $html The HTML to be displayed
432
		 */
433
		return apply_filters( 'jetpack_idc_second_step_header_lead', $string );
434
	}
435
436 View Code Duplication
	function get_migrate_site_action_explanation() {
437
		$html = wp_kses(
438
			sprintf(
439
				__(
440
					'Yes. <a href="%1$s">%2$s</a> is replacing <a href="%3$s">%4$s</a>. I would like to
441
					migrate my stats and subscribers from <a href="%3$s">%4$s</a> to <a href="%1$s">%2$s</a>.',
442
					'jetpack'
443
				),
444
				esc_url( get_home_url() ),
445
				self::prepare_url_for_display( get_home_url() ),
446
				esc_url( self::$wpcom_home_url ),
447
				untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) )
448
			),
449
			array( 'a' => array( 'href' => array() ) )
450
		);
451
452
		/**
453
		 * Allows overriding of the default text for explaining the migrate site action.
454
		 *
455
		 * @since 4.4.0
456
		 *
457
		 * @param string $html The HTML to be displayed
458
		 */
459
		return apply_filters( 'jetpack_idc_migrate_site_explanation', $html );
460
	}
461
462
	function get_migrate_site_button_text() {
463
		$string = esc_html__( 'Migrate stats &amp; and Subscribers', 'jetpack' );
464
465
		/**
466
		 * Allows overriding of the default text used for the migrate site action button.
467
		 *
468
		 * @since 4.4.0
469
		 *
470
		 * @param string $string The string to be displayed
471
		 */
472
		return apply_filters( 'jetpack_idc_migrate_site_button_text', $string );
473
	}
474
475 View Code Duplication
	function get_start_fresh_action_explanation() {
476
		$html = wp_kses(
477
			sprintf(
478
				__(
479
					'No. <a href="%1$s">%2$s</a> is a new and different website that\'s separate from 
480
					<a href="%3$s">%4$s</a>. It requires  a new connection to WordPress.com for new stats and subscribers.',
481
					'jetpack'
482
				),
483
				esc_url( get_home_url() ),
484
				self::prepare_url_for_display( get_home_url() ),
485
				esc_url( self::$wpcom_home_url ),
486
				untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) )
487
			),
488
			array( 'a' => array( 'href' => array() ) )
489
		);
490
491
		/**
492
		 * Allows overriding of the default text for explaining the start fresh action.
493
		 *
494
		 * @since 4.4.0
495
		 *
496
		 * @param string $html The HTML to be displayed
497
		 */
498
		return apply_filters( 'jetpack_idc_start_fresh_explanation', $html );
499
	}
500
501
	function get_start_fresh_button_text() {
502
		$string = esc_html__( 'Start fresh &amp; create new connection', 'jetpack' );
503
504
		/**
505
		 * Allows overriding of the default text used for the start fresh action button.
506
		 *
507
		 * @since 4.4.0
508
		 *
509
		 * @param string $string The string to be displayed
510
		 */
511
		return apply_filters( 'jetpack_idc_start_fresh_button_text', $string );
512
	}
513
514 View Code Duplication
	function get_unsure_prompt() {
515
		$html = wp_kses(
516
			sprintf(
517
				__(
518
					'Unsure what to do? <a href="%1$s">Read more about Jetpack Safe Mode</a>',
519
					'jetpack'
520
				),
521
				esc_url( self::SAFE_MODE_DOC_LINK )
522
			),
523
			array( 'a' => array( 'href' => array() ) )
524
		);
525
526
		/**
527
		 * Allows overriding of the default text using in the "Unsure what to do?" prompt.
528
		 *
529
		 * @since 4.4.0
530
		 *
531
		 * @param string $html The HTML to be displayed
532
		 */
533
		return apply_filters( 'jetpack_idc_unsure_prompt', $html );
534
	}
535
}
536
537
Jetpack_IDC::init();
538