Completed
Push — update/add-single-purpose-jetp... ( 28d890...705f7e )
by
unknown
09:19
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
12
/**
13
 * Class Jetpack_Cxn_Tests contains all of the actual tests.
14
 */
15
class Jetpack_Cxn_Tests extends Jetpack_Cxn_Test_Base {
16
17
	/**
18
	 * Jetpack_Cxn_Tests constructor.
19
	 */
20
	public function __construct() {
21
		parent::__construct();
22
23
		$methods = get_class_methods( 'Jetpack_Cxn_Tests' );
24
25
		foreach ( $methods as $method ) {
26
			if ( false === strpos( $method, 'test__' ) ) {
27
				continue;
28
			}
29
			$this->add_test( array( $this, $method ), $method, 'direct' );
30
		}
31
32
		/**
33
		 * Fires after loading default Jetpack Connection tests.
34
		 *
35
		 * @since 7.1.0
36
		 */
37
		do_action( 'jetpack_connection_tests_loaded' );
38
39
		/**
40
		 * Determines if the WP.com testing suite should be included.
41
		 *
42
		 * @since 7.1.0
43
		 *
44
		 * @param bool $run_test To run the WP.com testing suite. Default true.
45
		 */
46
		if ( apply_filters( 'jetpack_debugger_run_self_test', true ) ) {
47
			/**
48
			 * Intentionally added last as it checks for an existing failure state before attempting.
49
			 * Generally, any failed location condition would result in the WP.com check to fail too, so
50
			 * we will skip it to avoid confusing error messages.
51
			 *
52
			 * Note: This really should be an 'async' test.
53
			 */
54
			$this->add_test( array( $this, 'last__wpcom_self_test' ), 'test__wpcom_self_test', 'direct' );
55
		}
56
	}
57
58
	/**
59
	 * Helper function to look up the expected master user and return the local WP_User.
60
	 *
61
	 * @return WP_User Jetpack's expected master user.
62
	 */
63
	protected function helper_retrieve_local_master_user() {
64
		$master_user = Jetpack_Options::get_option( 'master_user' );
65
		return new WP_User( $master_user );
66
	}
67
68
	/**
69
	 * Is Jetpack even connected and supposed to be talking to WP.com?
70
	 */
71
	protected function helper_is_jetpack_connected() {
72
		return ( Jetpack::is_active() && ! ( new Status() )->is_development_mode() );
73
	}
74
75
	/**
76
	 * Returns 30 for use with a filter.
77
	 *
78
	 * To allow time for WP.com to run upstream testing, this function exists to increase the http_request_timeout value
79
	 * to 30.
80
	 *
81
	 * @return int 30
82
	 */
83
	public static function increase_timeout() {
84
		return 30; // seconds.
85
	}
86
87
	/**
88
	 * Test if Jetpack is connected.
89
	 */
90
	protected function test__check_if_connected() {
91
		$name = __FUNCTION__;
92
		if ( $this->helper_is_jetpack_connected() ) {
93
			$result = self::passing_test( $name );
94 View Code Duplication
		} elseif ( ( new Status() )->is_development_mode() ) {
95
			$result = self::skipped_test( $name, __( 'Jetpack is in Development Mode:', 'jetpack' ) . ' ' . Jetpack::development_mode_trigger_text(), __( 'Disable development mode.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Cxn_Tests::skipped_test() has too many arguments starting with __('Disable development mode.', 'jetpack').

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...
96
		} else {
97
			$result = self::failing_test( $name, __( 'Jetpack is not connected.', 'jetpack' ), 'cycle_connection' );
98
		}
99
100
		return $result;
101
	}
102
103
	/**
104
	 * Test that the master user still exists on this site.
105
	 *
106
	 * @return array Test results.
107
	 */
108
	protected function test__master_user_exists_on_site() {
109
		$name = __FUNCTION__;
110
		if ( ! $this->helper_is_jetpack_connected() ) {
111
			return self::skipped_test( $name, __( 'Jetpack is not connected. No master user to check.', 'jetpack' ) ); // Skip test.
112
		}
113
		$local_user = $this->helper_retrieve_local_master_user();
114
115
		if ( $local_user->exists() ) {
116
			$result = self::passing_test( $name );
117
		} else {
118
			$result = self::failing_test( $name, __( 'The user who setup the Jetpack connection no longer exists on this site.', 'jetpack' ), 'cycle_connection' );
119
		}
120
121
		return $result;
122
	}
123
124
	/**
125
	 * Test that the master user has the manage options capability (e.g. is an admin).
126
	 *
127
	 * Generic calls from WP.com execute on Jetpack as the master user. If it isn't an admin, random things will fail.
128
	 *
129
	 * @return array Test results.
130
	 */
131
	protected function test__master_user_can_manage_options() {
132
		$name = __FUNCTION__;
133
		if ( ! $this->helper_is_jetpack_connected() ) {
134
			return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test.
135
		}
136
		$master_user = $this->helper_retrieve_local_master_user();
137
138
		if ( user_can( $master_user, 'manage_options' ) ) {
139
			$result = self::passing_test( $name );
140
		} else {
141
			/* translators: a WordPress username */
142
			$result = self::failing_test( $name, sprintf( __( 'The user (%s) who setup the Jetpack connection is not an administrator.', 'jetpack' ), $master_user->user_login ), __( 'Either upgrade the user or disconnect and reconnect Jetpack.', 'jetpack' ) ); // @todo: Link to the right places.
143
		}
144
145
		return $result;
146
	}
147
148
	/**
149
	 * Test that the PHP's XML library is installed.
150
	 *
151
	 * While it should be installed by default, increasingly in PHP 7, some OSes require an additional php-xml package.
152
	 *
153
	 * @return array Test results.
154
	 */
155
	protected function test__xml_parser_available() {
156
		$name = __FUNCTION__;
157
		if ( function_exists( 'xml_parser_create' ) ) {
158
			$result = self::passing_test( $name );
159
		} else {
160
			$result = self::failing_test( $name, __( 'PHP XML manipluation libraries are not available.', 'jetpack' ), __( "Please ask your hosting provider to refer to our server requirements at https://jetpack.com/support/server-requirements/ and enable PHP's XML module.", 'jetpack' ) );
161
		}
162
163
		return $result;
164
	}
165
166
	/**
167
	 * Test that the server is able to send an outbound http communication.
168
	 *
169
	 * @return array Test results.
170
	 */
171 View Code Duplication
	protected function test__outbound_http() {
172
		$name    = __FUNCTION__;
173
		$request = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' );
174
		$code    = wp_remote_retrieve_response_code( $request );
175
176
		if ( 200 === intval( $code ) ) {
177
			$result = self::passing_test( $name );
178
		} else {
179
			$result = self::failing_test( $name, __( 'Your server did not successfully connect to the Jetpack server using HTTP', 'jetpack' ), 'outbound_requests' );
180
		}
181
182
		return $result;
183
	}
184
185
	/**
186
	 * Test that the server is able to send an outbound https communication.
187
	 *
188
	 * @return array Test results.
189
	 */
190 View Code Duplication
	protected function test__outbound_https() {
191
		$name    = __FUNCTION__;
192
		$request = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' );
193
		$code    = wp_remote_retrieve_response_code( $request );
194
195
		if ( 200 === intval( $code ) ) {
196
			$result = self::passing_test( $name );
197
		} else {
198
			$result = self::failing_test( $name, __( 'Your server did not successfully connect to the Jetpack server using HTTPS', 'jetpack' ), 'outbound_requests' );
199
		}
200
201
		return $result;
202
	}
203
204
	/**
205
	 * Check for an IDC.
206
	 *
207
	 * @return array Test results.
208
	 */
209
	protected function test__identity_crisis() {
210
		$name = __FUNCTION__;
211
		if ( ! $this->helper_is_jetpack_connected() ) {
212
			return self::skipped_test( $name, __( 'Jetpack is not connected.', 'jetpack' ) ); // Skip test.
213
		}
214
		$identity_crisis = Jetpack::check_identity_crisis();
215
216
		if ( ! $identity_crisis ) {
217
			$result = self::passing_test( $name );
218
		} else {
219
			$message = sprintf(
220
				/* translators: Two URLs. The first is the locally-recorded value, the second is the value as recorded on WP.com. */
221
				__( 'Your url is set as `%1$s`, but your WordPress.com connection lists it as `%2$s`!', 'jetpack' ),
222
				$identity_crisis['home'],
223
				$identity_crisis['wpcom_home']
224
			);
225
			$result = self::failing_test( $name, $message, 'support' );
226
		}
227
		return $result;
228
	}
229
230
	/**
231
	 * Tests connection status against wp.com's test-connection endpoint
232
	 *
233
	 * @todo: Compare with the wpcom_self_test. We only need one of these.
234
	 *
235
	 * @return array Test results.
236
	 */
237
	protected function test__wpcom_connection_test() {
238
		$name = __FUNCTION__;
239
240 View Code Duplication
		if ( ! Jetpack::is_active() || ( new Status() )->is_development_mode() || Jetpack::is_staging_site() || ! $this->pass ) {
241
			return self::skipped_test( $name );
242
		}
243
244
		add_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ) );
245
		$response = Client::wpcom_json_api_request_as_blog(
246
			sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
247
			Client::WPCOM_JSON_API_VERSION
248
		);
249
		remove_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ) );
250
251 View Code Duplication
		if ( is_wp_error( $response ) ) {
252
			/* translators: %1$s is the error code, %2$s is the error message */
253
			$message = 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...
254
			return self::failing_test( $name, $message );
255
		}
256
257
		$body = wp_remote_retrieve_body( $response );
258
		if ( ! $body ) {
259
			$message = __( 'Connection test failed (empty response body)', 'jetpack' ) . wp_remote_retrieve_response_code( $response );
260
			return self::failing_test( $name, $message );
261
		}
262
263
		if ( 404 === wp_remote_retrieve_response_code( $response ) ) {
264
			return self::skipped_test( $name, __( 'The WordPress.com API returned a 404 error.', 'jetpack' ) );
265
		}
266
267
		$result       = json_decode( $body );
268
		$is_connected = (bool) $result->connected;
269
		$message      = $result->message . ': ' . wp_remote_retrieve_response_code( $response );
270
271
		if ( $is_connected ) {
272
			return self::passing_test( $name );
273
		} else {
274
			return self::failing_test( $name, $message );
275
		}
276
	}
277
278
	/**
279
	 * Tests the port number to ensure it is an expected value.
280
	 *
281
	 * We expect that sites on be on one of:
282
	 * port 80,
283
	 * port 443 (https sites only),
284
	 * the value of JETPACK_SIGNATURE__HTTP_PORT,
285
	 * unless the site is intentionally on a different port (e.g. example.com:8080 is the site's URL).
286
	 *
287
	 * If the value isn't one of those and the site's URL doesn't include a port, then the signature verification will fail.
288
	 *
289
	 * This happens most commonly on sites with reverse proxies, so the edge (e.g. Varnish) is running on 80/443, but nginx
290
	 * or Apache is responding internally on a different port (e.g. 81).
291
	 *
292
	 * @return array Test results
293
	 */
294
	protected function test__server_port_value() {
295
		$name = __FUNCTION__;
296
		if ( ! isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) && ! isset( $_SERVER['SERVER_PORT'] ) ) {
297
			$message = 'The server port values are not defined. This is most common when running PHP via a CLI.';
298
			return self::skipped_test( $name, $message );
299
		}
300
		$site_port   = wp_parse_url( home_url(), PHP_URL_PORT );
301
		$server_port = isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ? (int) $_SERVER['HTTP_X_FORWARDED_PORT'] : (int) $_SERVER['SERVER_PORT'];
302
		$http_ports  = array( 80 );
303
		$https_ports = array( 80, 443 );
304
305
		if ( defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) ) {
306
			$http_ports[] = JETPACK_SIGNATURE__HTTP_PORT;
307
		}
308
309
		if ( defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) ) {
310
			$https_ports[] = JETPACK_SIGNATURE__HTTPS_PORT;
311
		}
312
313
		if ( $site_port ) {
314
			return self::skipped_test( $name ); // Not currently testing for this situation.
315
		}
316
317
		if ( is_ssl() && in_array( $server_port, $https_ports, true ) ) {
318
			return self::passing_test( $name );
319
		} elseif ( in_array( $server_port, $http_ports, true ) ) {
320
			return self::passing_test( $name );
321
		} else {
322
			if ( is_ssl() ) {
323
				$needed_constant = 'JETPACK_SIGNATURE__HTTPS_PORT';
324
			} else {
325
				$needed_constant = 'JETPACK_SIGNATURE__HTTP_PORT';
326
			}
327
			$message    = __( 'The server port value is unexpected.', 'jetpack' );
328
			$resolution = __( 'Try adding the following to your wp-config.php file:', 'jetpack' ) . " define( '$needed_constant', $server_port );";
329
			return self::failing_test( $name, $message, $resolution );
330
		}
331
	}
332
333
	/**
334
	 * Calls to WP.com to run the connection diagnostic testing suite.
335
	 *
336
	 * Intentionally added last as it will be skipped if any local failed conditions exist.
337
	 *
338
	 * @since 7.1.0
339
	 * @since 7.9.0 Timeout waiting for a WP.com response no longer fails the test. Test is marked skipped instead.
340
	 *
341
	 * @return array Test results.
342
	 */
343
	protected function last__wpcom_self_test() {
344
		$name = 'test__wpcom_self_test';
345
346 View Code Duplication
		if ( ! Jetpack::is_active() || ( new Status() )->is_development_mode() || Jetpack::is_staging_site() || ! $this->pass ) {
347
			return self::skipped_test( $name );
348
		}
349
350
		$self_xml_rpc_url = site_url( 'xmlrpc.php' );
351
352
		$testsite_url = Connection_Utils::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' );
353
354
		add_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ) );
355
356
		$response = wp_remote_get( $testsite_url . $self_xml_rpc_url );
357
358
		remove_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ) );
359
360
		$error_msg = wp_kses(
361
			sprintf(
362
				/* translators: Placeholder is a link to site's Jetpack debug page. */
363
				__(
364
					'<a target="_blank" rel="noopener noreferrer" href="%s">Visit the Jetpack.com debug page</a> for more information or <a target="_blank" rel="noopener noreferrer" href="https://jetpack.com/contact-support/">contact support</a>.',
365
					'jetpack'
366
				),
367
				esc_url( add_query_arg( 'url', rawurlencode( site_url() ), 'https://jetpack.com/support/debug/' ) )
368
			),
369
			array(
370
				'a' => array(
371
					'href'   => array(),
372
					'target' => array(),
373
					'rel'    => array(),
374
				),
375
			)
376
		);
377
378
		if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
379
			return self::passing_test( $name );
380
		} elseif ( is_wp_error( $response ) && false !== strpos( $response->get_error_message(), 'cURL error 28' ) ) { // Timeout.
381
			return self::skipped_test( $name, __( 'The test timed out which may sometimes indicate a failure or may be a false failure.', 'jetpack' ) );
382
		} else {
383
			return self::failing_test( $name, __( 'Jetpack.com detected an error on the WPcom Self Test.', 'jetpack' ), $error_msg );
384
		}
385
	}
386
}
387