Completed
Push — add/cron-api-endpoint ( 84e706...763b17 )
by
unknown
80:55 queued 70:58
created

class.jetpack-idc.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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