Completed
Push — update/debugger ( e1a4ba...fe8773 )
by
unknown
27:23 queued 20:17
created

Jetpack_Cxn_Tests::increase_timeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Collection of tests to run on the Jetpack connection locally.
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Connection\Client;
9
use Automattic\Jetpack\Status;
10
use Automattic\Jetpack\Connection\Utils as Connection_Utils;
11
use Automattic\Jetpack\Sync\Modules;
12
use Automattic\Jetpack\Sync\Settings as Sync_Settings;
13
use Automattic\Jetpack\Sync\Health as Sync_Health;
14
use Automattic\Jetpack\Sync\Sender as Sync_Sender;
15
use Automattic\Jetpack\Redirect;
16
17
/**
18
 * Class Jetpack_Cxn_Tests contains all of the actual tests.
19
 */
20
class Jetpack_Cxn_Tests extends Jetpack_Cxn_Test_Base {
21
22
	/**
23
	 * Jetpack_Cxn_Tests constructor.
24
	 */
25
	public function __construct() {
26
		parent::__construct();
27
28
		$methods = get_class_methods( 'Jetpack_Cxn_Tests' );
29
30
		foreach ( $methods as $method ) {
31
			if ( false === strpos( $method, 'test__' ) ) {
32
				continue;
33
			}
34
			$this->add_test( array( $this, $method ), $method, 'direct' );
35
		}
36
37
		/**
38
		 * Fires after loading default Jetpack Connection tests.
39
		 *
40
		 * @since 7.1.0
41
		 * @since 8.3.0 Passes the Jetpack_Cxn_Tests instance.
42
		 */
43
		do_action( 'jetpack_connection_tests_loaded', $this );
44
45
		/**
46
		 * Determines if the WP.com testing suite should be included.
47
		 *
48
		 * @since 7.1.0
49
		 * @since 8.1.0 Default false.
50
		 *
51
		 * @param bool $run_test To run the WP.com testing suite. Default false.
52
		 */
53
		if ( apply_filters( 'jetpack_debugger_run_self_test', false ) ) {
54
			/**
55
			 * Intentionally added last as it checks for an existing failure state before attempting.
56
			 * Generally, any failed location condition would result in the WP.com check to fail too, so
57
			 * we will skip it to avoid confusing error messages.
58
			 *
59
			 * Note: This really should be an 'async' test.
60
			 */
61
			$this->add_test( array( $this, 'last__wpcom_self_test' ), 'test__wpcom_self_test', 'direct' );
62
		}
63
	}
64
65
	/**
66
	 * Helper function to look up the expected master user and return the local WP_User.
67
	 *
68
	 * @return WP_User Jetpack's expected master user.
69
	 */
70
	protected function helper_retrieve_local_master_user() {
71
		$master_user = Jetpack_Options::get_option( 'master_user' );
72
		return new WP_User( $master_user );
73
	}
74
75
	/**
76
	 * Is Jetpack even connected and supposed to be talking to WP.com?
77
	 */
78
	protected function helper_is_jetpack_connected() {
79
		return ( Jetpack::is_active() && ! ( new Status() )->is_development_mode() );
80
	}
81
82
	/**
83
	 * Returns a support url based on development mode.
84
	 */
85
	protected function helper_get_support_url() {
86
		return Jetpack::is_development_version()
87
			? Redirect::get_url( 'jetpack-contact-support-beta-group' )
88
			: Redirect::get_url( 'jetpack-contact-support' );
89
	}
90
91
	/**
92
	 * Gets translated support text.
93
	 */
94
	protected function helper_get_support_text() {
95
		return __( 'Please contact Jetpack support.', 'jetpack' );
96
	}
97
98
	/**
99
	 * Gets translated text to enable outbound requests.
100
	 *
101
	 * @param string $protocol Either 'HTTP' or 'HTTPS'.
102
	 *
103
	 * @return string The translated text.
104
	 */
105
	protected function helper_enable_outbound_requests( $protocol ) {
106
		return sprintf(
107
			/* translators: %1$s - request protocol, either http or https */
108
			__(
109
				'Your server did not successfully connect to the Jetpack server using %1$s
110
				Please ask your hosting provider to confirm your server can make outbound requests to jetpack.com.',
111
				'jetpack'
112
			),
113
			$protocol
114
		);
115
	}
116
117
	/**
118
	 * Returns 30 for use with a filter.
119
	 *
120
	 * To allow time for WP.com to run upstream testing, this function exists to increase the http_request_timeout value
121
	 * to 30.
122
	 *
123
	 * @return int 30
124
	 */
125
	public static function increase_timeout() {
126
		return 30; // seconds.
127
	}
128
129
	/**
130
	 * Test if Jetpack is connected.
131
	 */
132
	protected function test__check_if_connected() {
133
		$name = __FUNCTION__;
134
		if ( $this->helper_is_jetpack_connected() ) {
135
			$result = self::passing_test(
136
				array(
137
					'name'             => $name,
138
					'label'            => __( 'Your site is connected to Jetpack', 'jetpack' ),
139
					'long_description' => sprintf(
140
						'<p>%1$s</p>' .
141
						'<p><span class="dashicons pass"><span class="screen-reader-text">%2$s</span></span> %3$s</p>',
142
						__( 'A healthy connection ensures Jetpack essential services are provided to your WordPress site, such as Stats and Site Security.', 'jetpack' ),
143
						/* translators: Screen reader text indicating a test has passed */
144
						__( 'Passed', 'jetpack' ),
145
						__( 'Your site is connected to Jetpack.', 'jetpack' )
146
					),
147
				)
148
			);
149
		} elseif ( ( new Status() )->is_development_mode() ) {
150
			$result = self::skipped_test(
151
				array(
152
					'name'              => $name,
153
					'short_description' => __( 'Jetpack is in Development Mode:', 'jetpack' ) . ' ' . Jetpack::development_mode_trigger_text(),
154
				)
155
			);
156
		} else {
157
			$result = self::failing_test(
158
				array(
159
					'name'             => $name,
160
					'label'            => __( 'Your site is not connected to Jetpack', 'jetpack' ),
161
					'action'           => admin_url( 'admin.php?page=jetpack#/dashboard' ),
162
					'action_label'     => __( 'Reconnect your site now', 'jetpack' ),
163
					'long_description' => sprintf(
164
						'<p>%1$s</p>' .
165
						'<p><span class="dashicons fail"><span class="screen-reader-text">%2$s</span></span> %3$s<strong> %4$s</strong></p>',
166
						__( 'A healthy connection ensures Jetpack essential services are provided to your WordPress site, such as Stats and Site Security.', 'jetpack' ),
167
						/* translators: screen reader text indicating a test failed */
168
						__( 'Error', 'jetpack' ),
169
						__( 'Your site is not connected to Jetpack.', 'jetpack' ),
170
						__( 'We recommend reconnecting Jetpack.', 'jetpack' )
171
					),
172
				)
173
			);
174
		}
175
176
		return $result;
177
	}
178
179
	/**
180
	 * Test that the master user still exists on this site.
181
	 *
182
	 * @return array Test results.
183
	 */
184
	protected function test__master_user_exists_on_site() {
185
		$name = __FUNCTION__;
186 View Code Duplication
		if ( ! $this->helper_is_jetpack_connected() ) {
187
			return self::skipped_test(
188
				array(
189
					'name'              => $name,
190
					'short_description' => __( 'Jetpack is not connected. No master user to check.', 'jetpack' ),
191
				)
192
			);
193
		}
194
		$local_user = $this->helper_retrieve_local_master_user();
195
196
		if ( $local_user->exists() ) {
197
			$result = self::passing_test( array( 'name' => $name ) );
198 View Code Duplication
		} else {
199
			$result = self::failing_test(
200
				array(
201
					'name'              => $name,
202
					'short_description' => __( 'The user who setup the Jetpack connection no longer exists on this site.', 'jetpack' ),
203
					'action_label'      => __( 'Please disconnect and reconnect Jetpack.', 'jetpack' ),
204
					'action'            => Redirect::get_url( 'jetpack-support-reconnecting-reinstalling-jetpack' ),
205
				)
206
			);
207
		}
208
209
		return $result;
210
	}
211
212
	/**
213
	 * Test that the master user has the manage options capability (e.g. is an admin).
214
	 *
215
	 * Generic calls from WP.com execute on Jetpack as the master user. If it isn't an admin, random things will fail.
216
	 *
217
	 * @return array Test results.
218
	 */
219
	protected function test__master_user_can_manage_options() {
220
		$name = __FUNCTION__;
221 View Code Duplication
		if ( ! $this->helper_is_jetpack_connected() ) {
222
			return self::skipped_test(
223
				array(
224
					'name'              => $name,
225
					'short_description' => __( 'Jetpack is not connected.', 'jetpack' ),
226
				)
227
			);
228
		}
229
		$master_user = $this->helper_retrieve_local_master_user();
230
231
		if ( user_can( $master_user, 'manage_options' ) ) {
232
			$result = self::passing_test( array( 'name' => $name ) );
233 View Code Duplication
		} else {
234
			$result = self::failing_test(
235
				array(
236
					'name'              => $name,
237
					/* translators: a WordPress username */
238
					'short_description' => sprintf( __( 'The user (%s) who setup the Jetpack connection is not an administrator.', 'jetpack' ), $master_user->user_login ),
239
					'action_label'      => __( 'Either upgrade the user or disconnect and reconnect Jetpack.', 'jetpack' ),
240
					'action'            => Redirect::get_url( 'jetpack-support-reconnecting-reinstalling-jetpack' ),
241
				)
242
			);
243
		}
244
245
		return $result;
246
	}
247
248
	/**
249
	 * Test that the PHP's XML library is installed.
250
	 *
251
	 * While it should be installed by default, increasingly in PHP 7, some OSes require an additional php-xml package.
252
	 *
253
	 * @return array Test results.
254
	 */
255
	protected function test__xml_parser_available() {
256
		$name = __FUNCTION__;
257
		if ( function_exists( 'xml_parser_create' ) ) {
258
			$result = self::passing_test( array( 'name' => $name ) );
259
		} else {
260
			$result = self::failing_test(
261
				array(
262
					'name'              => $name,
263
					'label'             => __( 'PHP XML manipulation libraries are not available.', 'jetpack' ),
264
					'short_description' => __( 'Please ask your hosting provider to refer to our server requirements and enable PHP\'s XML module.', 'jetpack' ),
265
					'action_label'      => __( 'View our server requirements', 'jetpack' ),
266
					'action'            => Redirect::get_url( 'jetpack-support-server-requirements' ),
267
				)
268
			);
269
		}
270
		return $result;
271
	}
272
273
	/**
274
	 * Test that the server is able to send an outbound http communication.
275
	 *
276
	 * @return array Test results.
277
	 */
278 View Code Duplication
	protected function test__outbound_http() {
279
		$name    = __FUNCTION__;
280
		$request = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' );
281
		$code    = wp_remote_retrieve_response_code( $request );
282
283
		if ( 200 === intval( $code ) ) {
284
			$result = self::passing_test( array( 'name' => $name ) );
285
		} else {
286
			$result = self::failing_test(
287
				array(
288
					'name'              => $name,
289
					'short_description' => $this->helper_enable_outbound_requests( 'HTTP' ),
290
				)
291
			);
292
		}
293
294
		return $result;
295
	}
296
297
	/**
298
	 * Test that the server is able to send an outbound https communication.
299
	 *
300
	 * @return array Test results.
301
	 */
302 View Code Duplication
	protected function test__outbound_https() {
303
		$name    = __FUNCTION__;
304
		$request = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' );
305
		$code    = wp_remote_retrieve_response_code( $request );
306
307
		if ( 200 === intval( $code ) ) {
308
			$result = self::passing_test( array( 'name' => $name ) );
309
		} else {
310
			$result = self::failing_test(
311
				array(
312
					'name'              => $name,
313
					'short_description' => $this->helper_enable_outbound_requests( 'HTTPS' ),
314
				)
315
			);
316
		}
317
318
		return $result;
319
	}
320
321
	/**
322
	 * Check for an IDC.
323
	 *
324
	 * @return array Test results.
325
	 */
326
	protected function test__identity_crisis() {
327
		$name = __FUNCTION__;
328 View Code Duplication
		if ( ! $this->helper_is_jetpack_connected() ) {
329
			return self::skipped_test(
330
				array(
331
					'name'              => $name,
332
					'short_description' => __( 'Jetpack is not connected.', 'jetpack' ),
333
				)
334
			);
335
		}
336
		$identity_crisis = Jetpack::check_identity_crisis();
337
338
		if ( ! $identity_crisis ) {
339
			$result = self::passing_test( array( 'name' => $name ) );
340
		} else {
341
			$result = self::failing_test(
342
				array(
343
					'name'              => $name,
344
					'short_description' => sprintf(
345
						/* translators: Two URLs. The first is the locally-recorded value, the second is the value as recorded on WP.com. */
346
						__( 'Your url is set as `%1$s`, but your WordPress.com connection lists it as `%2$s`!', 'jetpack' ),
347
						$identity_crisis['home'],
348
						$identity_crisis['wpcom_home']
349
					),
350
					'action_label'      => $this->helper_get_support_text(),
351
					'action'            => $this->helper_get_support_url(),
352
				)
353
			);
354
		}
355
		return $result;
356
	}
357
358
	/**
359
	 * Tests connection status against wp.com's test-connection endpoint.
360
	 *
361
	 * @todo: Compare with the wpcom_self_test. We only need one of these.
362
	 *
363
	 * @return array Test results.
364
	 */
365
	protected function test__wpcom_connection_test() {
366
		$name = __FUNCTION__;
367
368
		$status = new Status();
369 View Code Duplication
		if ( ! Jetpack::is_active() || $status->is_development_mode() || $status->is_staging_site() || ! $this->pass ) {
370
			return self::skipped_test( array( 'name' => $name ) );
371
		}
372
373
		add_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ) );
374
		$response = Client::wpcom_json_api_request_as_blog(
375
			sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
376
			Client::WPCOM_JSON_API_VERSION
377
		);
378
		remove_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ) );
379
380 View Code Duplication
		if ( is_wp_error( $response ) ) {
381
			return self::failing_test(
382
				array(
383
					'name'              => $name,
384
					/* translators: %1$s is the error code, %2$s is the error message */
385
					'short_description' => sprintf( __( 'Connection test failed (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ),
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
386
					'action_label'      => $this->helper_get_support_text(),
387
					'action'            => $this->helper_get_support_url(),
388
				)
389
			);
390
		}
391
392
		$body = wp_remote_retrieve_body( $response );
393 View Code Duplication
		if ( ! $body ) {
394
			return self::failing_test(
395
				array(
396
					'name'              => $name,
397
					'short_description' => __( 'Connection test failed (empty response body)', 'jetpack' ) . wp_remote_retrieve_response_code( $response ),
398
					'action_label'      => $this->helper_get_support_text(),
399
					'action'            => $this->helper_get_support_url(),
400
				)
401
			);
402
		}
403
404 View Code Duplication
		if ( 404 === wp_remote_retrieve_response_code( $response ) ) {
405
			return self::skipped_test(
406
				array(
407
					'name'              => $name,
408
					'short_description' => __( 'The WordPress.com API returned a 404 error.', 'jetpack' ),
409
				)
410
			);
411
		}
412
413
		$result       = json_decode( $body );
414
		$is_connected = (bool) $result->connected;
415
		$message      = $result->message . ': ' . wp_remote_retrieve_response_code( $response );
416
417
		if ( $is_connected ) {
418
			return self::passing_test( array( 'name' => $name ) );
419
		} else {
420
			return self::failing_test(
421
				array(
422
					'name'              => $name,
423
					'short_description' => $message,
424
					'action_label'      => $this->helper_get_support_text(),
425
					'action'            => $this->helper_get_support_url(),
426
				)
427
			);
428
		}
429
	}
430
431
	/**
432
	 * Tests the port number to ensure it is an expected value.
433
	 *
434
	 * We expect that sites on be on one of:
435
	 * port 80,
436
	 * port 443 (https sites only),
437
	 * the value of JETPACK_SIGNATURE__HTTP_PORT,
438
	 * unless the site is intentionally on a different port (e.g. example.com:8080 is the site's URL).
439
	 *
440
	 * If the value isn't one of those and the site's URL doesn't include a port, then the signature verification will fail.
441
	 *
442
	 * This happens most commonly on sites with reverse proxies, so the edge (e.g. Varnish) is running on 80/443, but nginx
443
	 * or Apache is responding internally on a different port (e.g. 81).
444
	 *
445
	 * @return array Test results
446
	 */
447
	protected function test__server_port_value() {
448
		$name = __FUNCTION__;
449
		if ( ! isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) && ! isset( $_SERVER['SERVER_PORT'] ) ) {
450
			return self::skipped_test(
451
				array(
452
					'name'              => $name,
453
					'short_description' => __( 'The server port values are not defined. This is most common when running PHP via a CLI.', 'jetpack' ),
454
				)
455
			);
456
		}
457
		$site_port   = wp_parse_url( home_url(), PHP_URL_PORT );
458
		$server_port = isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ? (int) $_SERVER['HTTP_X_FORWARDED_PORT'] : (int) $_SERVER['SERVER_PORT'];
459
		$http_ports  = array( 80 );
460
		$https_ports = array( 80, 443 );
461
462
		if ( defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) ) {
463
			$http_ports[] = JETPACK_SIGNATURE__HTTP_PORT;
464
		}
465
466
		if ( defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) ) {
467
			$https_ports[] = JETPACK_SIGNATURE__HTTPS_PORT;
468
		}
469
470
		if ( $site_port ) {
471
			return self::skipped_test( array( 'name' => $name ) ); // Not currently testing for this situation.
472
		}
473
474
		if ( is_ssl() && in_array( $server_port, $https_ports, true ) ) {
475
			return self::passing_test( array( 'name' => $name ) );
476
		} elseif ( in_array( $server_port, $http_ports, true ) ) {
477
			return self::passing_test( array( 'name' => $name ) );
478
		} else {
479
			if ( is_ssl() ) {
480
				$needed_constant = 'JETPACK_SIGNATURE__HTTPS_PORT';
481
			} else {
482
				$needed_constant = 'JETPACK_SIGNATURE__HTTP_PORT';
483
			}
484
			return self::failing_test(
485
				array(
486
					'name'              => $name,
487
					'short_description' => sprintf(
488
						/* translators: %1$s - a PHP code snippet */
489
						__(
490
							'The server port value is unexpected.
491
						Try adding the following to your wp-config.php file: %1$s',
492
							'jetpack'
493
						),
494
						"define( '$needed_constant', $server_port )"
495
					),
496
				)
497
			);
498
		}
499
	}
500
501
	/**
502
	 * Full Sync Health Test.
503
	 *
504
	 * Disabled: Results in a skipped test (recommended)
505
	 * In Progress: Results in failing test (recommended)
506
	 * Delayed: Results in failing test (recommended)
507
	 * Error: Results in failing test (critical)
508
	 */
509
	protected function test__full_sync_health() {
510
511
		$name = __FUNCTION__;
512
513
		if ( ! $this->helper_is_jetpack_connected() ) {
514
			// If the site is not connected, there is no point in testing Sync health.
515
			return self::skipped_test(
516
				array(
517
					'name'                => $name,
518
					'show_in_site_health' => false,
519
				)
520
			);
521
		}
522
523
		// Sync is enabled.
524
		if ( Sync_Settings::is_sync_enabled() ) {
525
526
			// Get Full Sync Progress.
527
			$full_sync_module = Modules::get_module( 'full-sync' );
528
			$progress_percent = $full_sync_module ? $full_sync_module->get_sync_progress_percentage() : null;
529
530
			// Full Sync in Progress.
531
			if ( $progress_percent ) {
532
533
				return self::skipped_test(
534
					array(
535
						'name'              => $name,
536
						'label'             => __( 'Jetpack is performing a full sync of your site', 'jetpack' ),
537
						'severity'          => 'recommended',
538
						/* translators: placeholder is a percentage number. */
539
						'short_description' => sprintf( __( 'Jetpack is performing a full sync of your site. Current Progress: %1$d %', 'jetpack' ), $progress_percent ),
540
						'long_description'  => sprintf(
541
							'<p>%1$s</p><p><span class="dashicons dashicons-update"><span class="screen-reader-text">%2$s</span></span> %3$s</p><div class="jetpack-sync-progress-ui"><div class="jetpack-sync-progress-label"></div><div class="jetpack-sync-progress-bar"></div></div>',
542
							__( 'The information synced by Jetpack ensures that Jetpack Search, Related Posts and other features are aligned with your site’s current content.', 'jetpack' ), /* translators: screen reader text indicating data is updating. */
543
							__( 'Updating', 'jetpack' ),
544
							__( 'Jetpack is currently performing a full sync of your site data.', 'jetpack' )
545
						),
546
					)
547
				);
548
549
			} else {
550
551
				// no Full Sync in Progress.
552
				return self::skipped_test(
553
					array(
554
						'name'                => $name,
555
						'show_in_site_health' => false,
556
					)
557
				);
558
559
			}
560
		} else {
561
562
			// If sync is not enabled no Full Sync can occur.
563
			return self::skipped_test(
564
				array(
565
					'name'                => $name,
566
					'show_in_site_health' => false,
567
				)
568
			);
569
570
		}
571
572
	}
573
574
	/**
575
	 * Sync Health Tests.
576
	 *
577
	 * Disabled: Results in a failing test (recommended)
578
	 * Delayed: Results in failing test (recommended)
579
	 * Error: Results in failing test (critical)
580
	 */
581
	protected function test__sync_health() {
582
583
		$name = __FUNCTION__;
584
585
		if ( ! $this->helper_is_jetpack_connected() ) {
586
			// If the site is not connected, there is no point in testing Sync health.
587
			return self::skipped_test(
588
				array(
589
					'name'                => $name,
590
					'show_in_site_health' => false,
591
				)
592
			);
593
		}
594
595
		// Sync is enabled.
596
		if ( Sync_Settings::is_sync_enabled() ) {
597
598
			if ( Sync_Health::get_status() === Sync_Health::STATUS_OUT_OF_SYNC ) {
599
				/*
600
				 * Sync has experienced Data Loss.
601
				 */
602
				$description  = '<p>';
603
				$description .= esc_html__( 'The information synced by Jetpack ensures that Jetpack Search, Related Posts and other features are aligned with your site’s current content.', 'jetpack' );
604
				$description .= '</p>';
605
				$description .= '<p>';
606
				$description .= sprintf(
607
					'<span class="dashicons fail"><span class="screen-reader-text">%1$s</span></span> ',
608
					esc_html__( 'Error', 'jetpack' )
609
				);
610
				$description .= wp_kses(
611
					__( 'Jetpack has detected an error while syncing your site. <strong>We recommend <a id="full_sync_request_link" href="#">a full sync</a> to align Jetpack with your site data.</strong>', 'jetpack' ),
612
					array(
613
						'a'      => array(
614
							'id'   => array(),
615
							'href' => array(),
616
						),
617
						'strong' => array(),
618
					)
619
				);
620
				$description .= '</p>';
621
622
				return self::skipped_test(
623
					array(
624
						'name'              => $name,
625
						'label'             => __( 'Jetpack has detected an error syncing your site.', 'jetpack' ),
626
						'severity'          => 'critical',
627
						'action'            => Redirect::get_url( 'jetpack-contact-support' ),
628
						'action_label'      => __( 'Contact Jetpack Support', 'jetpack' ),
629
						'short_description' => __( 'Jetpack has detected an error while syncing your site. We recommend a full sync to align Jetpack with your site data.', 'jetpack' ),
630
						'long_description'  => $description,
631
					)
632
				);
633
634
			} else {
635
				// Get the Sync Queue.
636
				$sender     = Sync_Sender::get_instance();
637
				$sync_queue = $sender->get_sync_queue();
638
639
				// lag exceeds 5 minutes.
640
				if ( $sync_queue->lag() > 5 * MINUTE_IN_SECONDS ) {
641
642
					$description  = '<p>';
643
					$description .= esc_html__( 'The information synced by Jetpack ensures that Jetpack Search, Related Posts and other features are aligned with your site’s current content.', 'jetpack' );
644
					$description .= '</p>';
645
					$description .= '<p>';
646
					$description .= sprintf(
647
						'<span class="dashicons dashicons-clock" style="color: orange;"><span class="screen-reader-text">%1$s</span></span> ',
648
						/* translators: name, used to describe a clock icon. */
649
						esc_html__( 'Clock', 'jetpack' )
650
					);
651
					$description .= wp_kses(
652
						sprintf(
653
							/* translators: placeholder is a number of minutes. */
654
							_n(
655
								'Jetpack has identified a delay while syncing individual content updates. Certain features might be slower than usual, but this is only temporary while sync catches up with recent changes to your site. <strong>We’re seeing a current delay of %1$d minute.</strong>',
656
								'Jetpack has identified a delay while syncing individual content updates. Certain features might be slower than usual, but this is only temporary while sync catches up with recent changes to your site. <strong>We’re seeing a current delay of %1$d minutes.</strong>',
657
								intval( $sync_queue->lag() / MINUTE_IN_SECONDS ),
658
								'jetpack'
659
							),
660
							number_format_i18n( $sync_queue->lag() / MINUTE_IN_SECONDS )
661
						),
662
						array( 'strong' => array() )
663
					);
664
					$description .= '</p>';
665
666
					return self::skipped_test(
667
						array(
668
							'name'              => $name,
669
							'label'             => __( 'Jetpack is experiencing a delay syncing your site.', 'jetpack' ),
670
							'severity'          => 'recommended',
671
							'action'            => null,
672
							'action_label'      => null,
673
							'short_description' => __( 'Jetpack is experiencing a delay syncing your site.', 'jetpack' ),
674
							'long_description'  => $description,
675
						)
676
					);
677
678
				} else {
679
680
					// Sync is Healthy.
681
					return self::passing_test( array( 'name' => $name ) );
682
683
				}
684
			}
685
		} else {
686
			/*
687
			 * Sync is disabled.
688
			 */
689
690
			$description  = '<p>';
691
			$description .= esc_html__( 'The information synced by Jetpack ensures that Jetpack Search, Related Posts and other features are aligned with your site’s current content.', 'jetpack' );
692
			$description .= '</p>';
693
			$description .= '<p>';
694
			$description .= __( 'Developers may enable / disable syncing using the Sync Settings API.', 'jetpack' );
695
			$description .= '</p>';
696
			$description .= '<p>';
697
			$description .= sprintf(
698
				'<span class="dashicons fail"><span class="screen-reader-text">%1$s</span></span> ',
699
				esc_html__( 'Error', 'jetpack' )
700
			);
701
			$description .= wp_kses(
702
				__( 'Jetpack Sync has been disabled on your site. Without it, certain Jetpack features will not work. <strong>We recommend enabling Sync.</strong>', 'jetpack' ),
703
				array( 'strong' => array() )
704
			);
705
			$description .= '</p>';
706
707
			return self::failing_test(
708
				array(
709
					'name'              => $name,
710
					'label'             => __( 'Jetpack Sync has been disabled on your site.', 'jetpack' ),
711
					'severity'          => 'recommended',
712
					'action'            => 'https://github.com/Automattic/jetpack/blob/master/packages/sync/src/class-settings.php',
713
					'action_label'      => __( 'See Github for more on Sync Settings', 'jetpack' ),
714
					'short_description' => __( 'Jetpack Sync has been disabled on your site.', 'jetpack' ),
715
					'long_description'  => $description,
716
				)
717
			);
718
719
		}
720
721
	}
722
723
	/**
724
	 * Calls to WP.com to run the connection diagnostic testing suite.
725
	 *
726
	 * Intentionally added last as it will be skipped if any local failed conditions exist.
727
	 *
728
	 * @since 7.1.0
729
	 * @since 7.9.0 Timeout waiting for a WP.com response no longer fails the test. Test is marked skipped instead.
730
	 *
731
	 * @return array Test results.
732
	 */
733
	protected function last__wpcom_self_test() {
734
		$name = 'test__wpcom_self_test';
735
736
		$status = new Status();
737 View Code Duplication
		if ( ! Jetpack::is_active() || $status->is_development_mode() || $status->is_staging_site() || ! $this->pass ) {
738
			return self::skipped_test( array( 'name' => $name ) );
739
		}
740
741
		$self_xml_rpc_url = site_url( 'xmlrpc.php' );
742
743
		$testsite_url = Connection_Utils::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' );
744
745
		// Using PHP_INT_MAX - 1 so that there is still a way to override this if needed and since it only impacts this one call.
746
		add_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ), PHP_INT_MAX - 1 );
747
748
		$response = wp_remote_get( $testsite_url . $self_xml_rpc_url );
749
750
		remove_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ), PHP_INT_MAX - 1 );
751
752
		if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
753
			return self::passing_test( array( 'name' => $name ) );
754
		} elseif ( is_wp_error( $response ) && false !== strpos( $response->get_error_message(), 'cURL error 28' ) ) { // Timeout.
755
			return self::skipped_test(
756
				array(
757
					'name'              => $name,
758
					'short_description' => __( 'The test timed out which may sometimes indicate a failure or may be a false failure.', 'jetpack' ),
759
				)
760
			);
761
		} else {
762
			return self::failing_test(
763
				array(
764
					'name'              => $name,
765
					'short_description' => sprintf(
766
						/* translators: %1$s - A debugging url */
767
						__( 'Jetpack.com detected an error on the WP.com Self Test. Visit the Jetpack Debug page for more info: %1$s, or contact support.', 'jetpack' ),
768
						Redirect::get_url( 'jetpack-support-debug', array( 'query' => 'url=' . rawurlencode( site_url() ) ) )
769
					),
770
					'action_label'      => $this->helper_get_support_text(),
771
					'action'            => $this->helper_get_support_url(),
772
				)
773
			);
774
		}
775
	}
776
}
777