class-jetpack-cxn-test-base.php
2 years ago
class-jetpack-cxn-tests.php
2 years ago
class-jetpack-debug-data.php
2 years ago
class-jetpack-debugger.php
2 years ago
debug-functions.php
2 years ago
class-jetpack-cxn-tests.php
816 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Collection of tests to run on the Jetpack connection locally. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Connection\Client; |
| 9 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 10 | use Automattic\Jetpack\Connection\Tokens; |
| 11 | use Automattic\Jetpack\Redirect; |
| 12 | use Automattic\Jetpack\Status; |
| 13 | use Automattic\Jetpack\Sync\Health as Sync_Health; |
| 14 | use Automattic\Jetpack\Sync\Settings as Sync_Settings; |
| 15 | |
| 16 | /** |
| 17 | * Class Jetpack_Cxn_Tests contains all of the actual tests. |
| 18 | */ |
| 19 | class Jetpack_Cxn_Tests extends Jetpack_Cxn_Test_Base { |
| 20 | |
| 21 | /** |
| 22 | * Jetpack_Cxn_Tests constructor. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | parent::__construct(); |
| 26 | |
| 27 | $methods = get_class_methods( 'Jetpack_Cxn_Tests' ); |
| 28 | |
| 29 | foreach ( $methods as $method ) { |
| 30 | if ( ! str_contains( $method, 'test__' ) ) { |
| 31 | continue; |
| 32 | } |
| 33 | $this->add_test( array( $this, $method ), $method, 'direct' ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Fires after loading default Jetpack Connection tests. |
| 38 | * |
| 39 | * @since 7.1.0 |
| 40 | * @since 8.3.0 Passes the Jetpack_Cxn_Tests instance. |
| 41 | */ |
| 42 | do_action( 'jetpack_connection_tests_loaded', $this ); |
| 43 | |
| 44 | /** |
| 45 | * Determines if the WP.com testing suite should be included. |
| 46 | * |
| 47 | * @since 7.1.0 |
| 48 | * @since 8.1.0 Default false. |
| 49 | * |
| 50 | * @param bool $run_test To run the WP.com testing suite. Default false. |
| 51 | */ |
| 52 | if ( apply_filters( 'jetpack_debugger_run_self_test', false ) ) { |
| 53 | /** |
| 54 | * Intentionally added last as it checks for an existing failure state before attempting. |
| 55 | * Generally, any failed location condition would result in the WP.com check to fail too, so |
| 56 | * we will skip it to avoid confusing error messages. |
| 57 | * |
| 58 | * Note: This really should be an 'async' test. |
| 59 | */ |
| 60 | $this->add_test( array( $this, 'last__wpcom_self_test' ), 'test__wpcom_self_test', 'direct' ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Helper function to look up the expected master user and return the local WP_User. |
| 66 | * |
| 67 | * @return WP_User Jetpack's expected master user. |
| 68 | */ |
| 69 | protected function helper_retrieve_local_master_user() { |
| 70 | $master_user = Jetpack_Options::get_option( 'master_user' ); |
| 71 | return new WP_User( $master_user ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Is Jetpack even connected and supposed to be talking to WP.com? |
| 76 | */ |
| 77 | protected function helper_is_jetpack_connected() { |
| 78 | return Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Retrieve the `blog_token` if it exists. |
| 83 | * |
| 84 | * @return object|false |
| 85 | */ |
| 86 | protected function helper_get_blog_token() { |
| 87 | return ( new Tokens() )->get_access_token(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns a support url based on using a development version. |
| 92 | */ |
| 93 | protected function helper_get_support_url() { |
| 94 | return Jetpack::is_development_version() |
| 95 | ? Redirect::get_url( 'jetpack-contact-support-beta-group' ) |
| 96 | : Redirect::get_url( 'jetpack-contact-support' ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the url to reconnect Jetpack. |
| 101 | * |
| 102 | * @return string The reconnect url. |
| 103 | */ |
| 104 | protected static function helper_get_reconnect_url() { |
| 105 | return admin_url( 'admin.php?page=jetpack#/reconnect' ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Gets translated support text. |
| 110 | */ |
| 111 | protected function helper_get_support_text() { |
| 112 | return __( 'Please contact Jetpack support.', 'jetpack' ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns the translated text to reconnect Jetpack. |
| 117 | * |
| 118 | * @return string The translated reconnect text. |
| 119 | */ |
| 120 | protected static function helper_get_reconnect_text() { |
| 121 | return __( 'Reconnect Jetpack now', 'jetpack' ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the translated text for failing tests due to timeouts. |
| 126 | * |
| 127 | * @return string The translated timeout text. |
| 128 | */ |
| 129 | protected static function helper_get_timeout_text() { |
| 130 | return __( 'The test timed out which may sometimes indicate a failure or may be a false failure. Please relaunch tests.', 'jetpack' ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Gets translated reconnect long description. |
| 135 | * |
| 136 | * @param string $connection_error The connection specific error. |
| 137 | * @param string $recommendation The recommendation for resolving the connection error. |
| 138 | * |
| 139 | * @return string The translated long description for reconnection recommendations. |
| 140 | */ |
| 141 | protected static function helper_get_reconnect_long_description( $connection_error, $recommendation ) { |
| 142 | |
| 143 | return sprintf( |
| 144 | '<p>%1$s</p>' . |
| 145 | '<p><span class="dashicons fail"><span class="screen-reader-text">%2$s</span></span> %3$s</p><p><strong>%4$s</strong></p>', |
| 146 | __( 'A healthy connection ensures Jetpack essential services are provided to your WordPress site, such as Stats and Site Security.', 'jetpack' ), |
| 147 | /* translators: screen reader text indicating a test failed */ |
| 148 | __( 'Error', 'jetpack' ), |
| 149 | $connection_error, |
| 150 | $recommendation |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Helper function to return consistent responses for a connection failing test. |
| 156 | * |
| 157 | * @param string $name The raw method name that runs the test. Default unnamed_test. |
| 158 | * @param string $connection_error The connection specific error. Default 'Your site is not connected to Jetpack.'. |
| 159 | * @param string $recommendation The recommendation for resolving the connection error. Default 'We recommend reconnecting Jetpack.'. |
| 160 | * |
| 161 | * @return array Test results. |
| 162 | */ |
| 163 | public static function connection_failing_test( $name, $connection_error = '', $recommendation = '' ) { |
| 164 | $connection_error = empty( $connection_error ) ? __( 'Your site is not connected to Jetpack.', 'jetpack' ) : $connection_error; |
| 165 | $recommendation = empty( $recommendation ) ? __( 'We recommend reconnecting Jetpack.', 'jetpack' ) : $recommendation; |
| 166 | |
| 167 | $args = array( |
| 168 | 'name' => $name, |
| 169 | 'short_description' => $connection_error, |
| 170 | 'action' => self::helper_get_reconnect_url(), |
| 171 | 'action_label' => self::helper_get_reconnect_text(), |
| 172 | 'long_description' => self::helper_get_reconnect_long_description( $connection_error, $recommendation ), |
| 173 | ); |
| 174 | |
| 175 | return self::failing_test( $args ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets translated text to enable outbound requests. |
| 180 | * |
| 181 | * @param string $protocol Either 'HTTP' or 'HTTPS'. |
| 182 | * |
| 183 | * @return string The translated text. |
| 184 | */ |
| 185 | protected function helper_enable_outbound_requests( $protocol ) { |
| 186 | return sprintf( |
| 187 | /* translators: %1$s - request protocol, either http or https */ |
| 188 | __( |
| 189 | 'Your server did not successfully connect to the Jetpack server using %1$s |
| 190 | Please ask your hosting provider to confirm your server can make outbound requests to jetpack.com.', |
| 191 | 'jetpack' |
| 192 | ), |
| 193 | $protocol |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Returns 30 for use with a filter. |
| 199 | * |
| 200 | * To allow time for WP.com to run upstream testing, this function exists to increase the http_request_timeout value |
| 201 | * to 30. |
| 202 | * |
| 203 | * @return int 30 |
| 204 | */ |
| 205 | public static function increase_timeout() { |
| 206 | return 30; // seconds. |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * The test verifies the blog token exists. |
| 211 | * |
| 212 | * @return array |
| 213 | */ |
| 214 | protected function test__blog_token_if_exists() { |
| 215 | $name = __FUNCTION__; |
| 216 | |
| 217 | if ( ! $this->helper_is_jetpack_connected() ) { |
| 218 | return self::skipped_test( |
| 219 | array( |
| 220 | 'name' => $name, |
| 221 | 'short_description' => __( 'Jetpack is not connected. No blog token to check.', 'jetpack' ), |
| 222 | ) |
| 223 | ); |
| 224 | } |
| 225 | $blog_token = $this->helper_get_blog_token(); |
| 226 | |
| 227 | if ( $blog_token ) { |
| 228 | $result = self::passing_test( array( 'name' => $name ) ); |
| 229 | } else { |
| 230 | $connection_error = __( 'Blog token is missing.', 'jetpack' ); |
| 231 | |
| 232 | $result = self::connection_failing_test( $name, $connection_error ); |
| 233 | } |
| 234 | |
| 235 | return $result; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Test if Jetpack is connected. |
| 240 | */ |
| 241 | protected function test__check_if_connected() { |
| 242 | $name = __FUNCTION__; |
| 243 | |
| 244 | if ( ! $this->helper_get_blog_token() ) { |
| 245 | return self::skipped_test( |
| 246 | array( |
| 247 | 'name' => $name, |
| 248 | 'short_description' => __( 'Blog token is missing.', 'jetpack' ), |
| 249 | ) |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | if ( $this->helper_is_jetpack_connected() ) { |
| 254 | $result = self::passing_test( |
| 255 | array( |
| 256 | 'name' => $name, |
| 257 | 'label' => __( 'Your site is connected to Jetpack', 'jetpack' ), |
| 258 | 'long_description' => sprintf( |
| 259 | '<p>%1$s</p>' . |
| 260 | '<p><span class="dashicons pass"><span class="screen-reader-text">%2$s</span></span> %3$s</p>', |
| 261 | __( 'A healthy connection ensures Jetpack essential services are provided to your WordPress site, such as Stats and Site Security.', 'jetpack' ), |
| 262 | /* translators: Screen reader text indicating a test has passed */ |
| 263 | __( 'Passed', 'jetpack' ), |
| 264 | __( 'Your site is connected to Jetpack.', 'jetpack' ) |
| 265 | ), |
| 266 | ) |
| 267 | ); |
| 268 | } elseif ( ( new Status() )->is_offline_mode() ) { |
| 269 | $result = self::skipped_test( |
| 270 | array( |
| 271 | 'name' => $name, |
| 272 | 'short_description' => __( 'Jetpack is in Offline Mode:', 'jetpack' ) . ' ' . Jetpack::development_mode_trigger_text(), |
| 273 | ) |
| 274 | ); |
| 275 | } else { |
| 276 | $connection_error = __( 'Your site is not connected to Jetpack', 'jetpack' ); |
| 277 | |
| 278 | $result = self::connection_failing_test( $name, $connection_error ); |
| 279 | } |
| 280 | |
| 281 | return $result; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Test that the master user still exists on this site. |
| 286 | * |
| 287 | * @return array Test results. |
| 288 | */ |
| 289 | protected function test__master_user_exists_on_site() { |
| 290 | $name = __FUNCTION__; |
| 291 | if ( ! $this->helper_is_jetpack_connected() ) { |
| 292 | return self::skipped_test( |
| 293 | array( |
| 294 | 'name' => $name, |
| 295 | 'short_description' => __( 'Jetpack is not connected. No master user to check.', 'jetpack' ), |
| 296 | ) |
| 297 | ); |
| 298 | } |
| 299 | if ( ! ( new Connection_Manager() )->get_connection_owner_id() ) { |
| 300 | return self::skipped_test( |
| 301 | array( |
| 302 | 'name' => $name, |
| 303 | 'short_description' => __( 'Jetpack is running without a connected user. No master user to check.', 'jetpack' ), |
| 304 | ) |
| 305 | ); |
| 306 | } |
| 307 | $local_user = $this->helper_retrieve_local_master_user(); |
| 308 | |
| 309 | if ( $local_user->exists() ) { |
| 310 | $result = self::passing_test( array( 'name' => $name ) ); |
| 311 | } else { |
| 312 | $connection_error = __( 'The user who setup the Jetpack connection no longer exists on this site.', 'jetpack' ); |
| 313 | |
| 314 | $result = self::connection_failing_test( $name, $connection_error ); |
| 315 | } |
| 316 | |
| 317 | return $result; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Test that the master user has the manage options capability (e.g. is an admin). |
| 322 | * |
| 323 | * Generic calls from WP.com execute on Jetpack as the master user. If it isn't an admin, random things will fail. |
| 324 | * |
| 325 | * @return array Test results. |
| 326 | */ |
| 327 | protected function test__master_user_can_manage_options() { |
| 328 | $name = __FUNCTION__; |
| 329 | if ( ! $this->helper_is_jetpack_connected() ) { |
| 330 | return self::skipped_test( |
| 331 | array( |
| 332 | 'name' => $name, |
| 333 | 'short_description' => __( 'Jetpack is not connected.', 'jetpack' ), |
| 334 | ) |
| 335 | ); |
| 336 | } |
| 337 | if ( ! ( new Connection_Manager() )->get_connection_owner_id() ) { |
| 338 | return self::skipped_test( |
| 339 | array( |
| 340 | 'name' => $name, |
| 341 | 'short_description' => __( 'Jetpack is running without a connected user. No master user to check.', 'jetpack' ), |
| 342 | ) |
| 343 | ); |
| 344 | } |
| 345 | $master_user = $this->helper_retrieve_local_master_user(); |
| 346 | |
| 347 | if ( user_can( $master_user, 'manage_options' ) ) { |
| 348 | $result = self::passing_test( array( 'name' => $name ) ); |
| 349 | } else { |
| 350 | /* translators: a WordPress username */ |
| 351 | $connection_error = sprintf( __( 'The user (%s) who setup the Jetpack connection is not an administrator.', 'jetpack' ), $master_user->user_login ); |
| 352 | /* translators: a WordPress username */ |
| 353 | $recommendation = sprintf( __( 'We recommend either upgrading the user (%s) or reconnecting Jetpack.', 'jetpack' ), $master_user->user_login ); |
| 354 | |
| 355 | $result = self::connection_failing_test( $name, $connection_error, $recommendation ); |
| 356 | } |
| 357 | |
| 358 | return $result; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Test that the PHP's XML library is installed. |
| 363 | * |
| 364 | * While it should be installed by default, increasingly in PHP 7, some OSes require an additional php-xml package. |
| 365 | * |
| 366 | * @return array Test results. |
| 367 | */ |
| 368 | protected function test__xml_parser_available() { |
| 369 | $name = __FUNCTION__; |
| 370 | if ( function_exists( 'xml_parser_create' ) ) { |
| 371 | $result = self::passing_test( array( 'name' => $name ) ); |
| 372 | } else { |
| 373 | $result = self::failing_test( |
| 374 | array( |
| 375 | 'name' => $name, |
| 376 | 'label' => __( 'PHP XML manipulation libraries are not available.', 'jetpack' ), |
| 377 | 'short_description' => __( 'Please ask your hosting provider to refer to our server requirements and enable PHP\'s XML module.', 'jetpack' ), |
| 378 | 'action_label' => __( 'View our server requirements', 'jetpack' ), |
| 379 | 'action' => Redirect::get_url( 'jetpack-support-server-requirements' ), |
| 380 | ) |
| 381 | ); |
| 382 | } |
| 383 | return $result; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Test that the server is able to send an outbound http communication. |
| 388 | * |
| 389 | * @return array Test results. |
| 390 | */ |
| 391 | protected function test__outbound_http() { |
| 392 | $name = __FUNCTION__; |
| 393 | $request = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' ); |
| 394 | $code = wp_remote_retrieve_response_code( $request ); |
| 395 | |
| 396 | if ( 200 === (int) $code ) { |
| 397 | $result = self::passing_test( array( 'name' => $name ) ); |
| 398 | } else { |
| 399 | $result = self::failing_test( |
| 400 | array( |
| 401 | 'name' => $name, |
| 402 | 'short_description' => $this->helper_enable_outbound_requests( 'HTTP' ), |
| 403 | ) |
| 404 | ); |
| 405 | } |
| 406 | |
| 407 | return $result; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Test that the server is able to send an outbound https communication. |
| 412 | * |
| 413 | * @return array Test results. |
| 414 | */ |
| 415 | protected function test__outbound_https() { |
| 416 | $name = __FUNCTION__; |
| 417 | $request = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' ); |
| 418 | $code = wp_remote_retrieve_response_code( $request ); |
| 419 | |
| 420 | if ( 200 === (int) $code ) { |
| 421 | $result = self::passing_test( array( 'name' => $name ) ); |
| 422 | } else { |
| 423 | $result = self::failing_test( |
| 424 | array( |
| 425 | 'name' => $name, |
| 426 | 'short_description' => $this->helper_enable_outbound_requests( 'HTTPS' ), |
| 427 | ) |
| 428 | ); |
| 429 | } |
| 430 | |
| 431 | return $result; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Check for an IDC. |
| 436 | * |
| 437 | * @return array Test results. |
| 438 | */ |
| 439 | protected function test__identity_crisis() { |
| 440 | $name = __FUNCTION__; |
| 441 | if ( ! $this->helper_is_jetpack_connected() ) { |
| 442 | return self::skipped_test( |
| 443 | array( |
| 444 | 'name' => $name, |
| 445 | 'short_description' => __( 'Jetpack is not connected.', 'jetpack' ), |
| 446 | ) |
| 447 | ); |
| 448 | } |
| 449 | $identity_crisis = Jetpack::check_identity_crisis(); |
| 450 | |
| 451 | if ( ! $identity_crisis ) { |
| 452 | $result = self::passing_test( array( 'name' => $name ) ); |
| 453 | } else { |
| 454 | $result = self::failing_test( |
| 455 | array( |
| 456 | 'name' => $name, |
| 457 | 'short_description' => sprintf( |
| 458 | /* translators: Two URLs. The first is the locally-recorded value, the second is the value as recorded on WP.com. */ |
| 459 | __( 'Your url is set as `%1$s`, but your WordPress.com connection lists it as `%2$s`!', 'jetpack' ), |
| 460 | $identity_crisis['home'], |
| 461 | $identity_crisis['wpcom_home'] |
| 462 | ), |
| 463 | 'action_label' => $this->helper_get_support_text(), |
| 464 | 'action' => $this->helper_get_support_url(), |
| 465 | ) |
| 466 | ); |
| 467 | } |
| 468 | return $result; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Tests the health of the Connection tokens. |
| 473 | * |
| 474 | * This will always check the blog token health. It will also check the user token health if |
| 475 | * a user is logged in and connected, or if there's a connected owner. |
| 476 | * |
| 477 | * @since 9.0.0 |
| 478 | * @since 9.6.0 Checks only blog token if current user not connected or site does not have a connected owner. |
| 479 | * |
| 480 | * @return array Test results. |
| 481 | */ |
| 482 | protected function test__connection_token_health() { |
| 483 | $name = __FUNCTION__; |
| 484 | $m = new Connection_Manager(); |
| 485 | $user_id = get_current_user_id(); |
| 486 | |
| 487 | // Check if there's a connected logged in user. |
| 488 | if ( $user_id && ! $m->is_user_connected( $user_id ) ) { |
| 489 | $user_id = false; |
| 490 | } |
| 491 | |
| 492 | // If no logged in user to check, let's see if there's a master_user set. |
| 493 | if ( ! $user_id ) { |
| 494 | $user_id = Jetpack_Options::get_option( 'master_user' ); |
| 495 | if ( $user_id && ! $m->is_user_connected( $user_id ) ) { |
| 496 | return self::connection_failing_test( $name, __( 'Missing token for the connection owner.', 'jetpack' ) ); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | if ( $user_id ) { |
| 501 | return $this->check_tokens_health( $user_id ); |
| 502 | } else { |
| 503 | return $this->check_blog_token_health(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Tests blog and user's token against wp.com's check-token-health endpoint. |
| 509 | * |
| 510 | * @since 9.6.0 |
| 511 | * |
| 512 | * @return array Test results. |
| 513 | */ |
| 514 | protected function check_blog_token_health() { |
| 515 | $name = 'test__connection_token_health'; |
| 516 | $valid = ( new Tokens() )->validate_blog_token(); |
| 517 | |
| 518 | if ( ! $valid ) { |
| 519 | return self::connection_failing_test( $name, __( 'Blog token validation failed.', 'jetpack' ) ); |
| 520 | } else { |
| 521 | return self::passing_test( array( 'name' => $name ) ); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Tests blog token against wp.com's check-token-health endpoint. |
| 527 | * |
| 528 | * @since 9.6.0 |
| 529 | * |
| 530 | * @param int $user_id The user ID to check the tokens for. |
| 531 | * |
| 532 | * @return array Test results. |
| 533 | */ |
| 534 | protected function check_tokens_health( $user_id ) { |
| 535 | $name = 'test__connection_token_health'; |
| 536 | $validated_tokens = ( new Tokens() )->validate( $user_id ); |
| 537 | |
| 538 | if ( ! is_array( $validated_tokens ) || count( array_diff_key( array_flip( array( 'blog_token', 'user_token' ) ), $validated_tokens ) ) ) { |
| 539 | return self::skipped_test( |
| 540 | array( |
| 541 | 'name' => $name, |
| 542 | 'short_description' => __( 'Token health check failed to validate tokens.', 'jetpack' ), |
| 543 | ) |
| 544 | ); |
| 545 | } |
| 546 | |
| 547 | $invalid_tokens_exist = false; |
| 548 | foreach ( $validated_tokens as $validated_token ) { |
| 549 | if ( ! $validated_token['is_healthy'] ) { |
| 550 | $invalid_tokens_exist = true; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if ( false === $invalid_tokens_exist ) { |
| 556 | return self::passing_test( array( 'name' => $name ) ); |
| 557 | } |
| 558 | |
| 559 | $connection_error = __( 'Invalid Jetpack connection tokens.', 'jetpack' ); |
| 560 | |
| 561 | return self::connection_failing_test( $name, $connection_error ); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Tests connection status against wp.com's test-connection endpoint. |
| 566 | * |
| 567 | * @todo: Compare with the wpcom_self_test. We only need one of these. |
| 568 | * |
| 569 | * @return array Test results. |
| 570 | */ |
| 571 | protected function test__wpcom_connection_test() { |
| 572 | $name = __FUNCTION__; |
| 573 | |
| 574 | $status = new Status(); |
| 575 | if ( ! Jetpack::is_connection_ready() || $status->is_offline_mode() || $status->in_safe_mode() || ! $this->pass ) { |
| 576 | return self::skipped_test( array( 'name' => $name ) ); |
| 577 | } |
| 578 | |
| 579 | add_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ) ); |
| 580 | $response = Client::wpcom_json_api_request_as_blog( |
| 581 | sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ), |
| 582 | Client::WPCOM_JSON_API_VERSION |
| 583 | ); |
| 584 | remove_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ) ); |
| 585 | |
| 586 | if ( is_wp_error( $response ) ) { |
| 587 | if ( str_contains( $response->get_error_message(), 'cURL error 28' ) ) { // Timeout. |
| 588 | $result = self::skipped_test( |
| 589 | array( |
| 590 | 'name' => $name, |
| 591 | 'short_description' => self::helper_get_timeout_text(), |
| 592 | ) |
| 593 | ); |
| 594 | } else { |
| 595 | /* translators: %1$s is the error code, %2$s is the error message */ |
| 596 | $message = sprintf( __( 'Connection test failed (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ); |
| 597 | |
| 598 | $result = self::connection_failing_test( $name, $message ); |
| 599 | } |
| 600 | |
| 601 | return $result; |
| 602 | } |
| 603 | |
| 604 | $body = wp_remote_retrieve_body( $response ); |
| 605 | if ( ! $body ) { |
| 606 | return self::failing_test( |
| 607 | array( |
| 608 | 'name' => $name, |
| 609 | 'short_description' => __( 'Connection test failed (empty response body)', 'jetpack' ) . wp_remote_retrieve_response_code( $response ), |
| 610 | 'action_label' => $this->helper_get_support_text(), |
| 611 | 'action' => $this->helper_get_support_url(), |
| 612 | ) |
| 613 | ); |
| 614 | } |
| 615 | |
| 616 | if ( 404 === wp_remote_retrieve_response_code( $response ) ) { |
| 617 | return self::skipped_test( |
| 618 | array( |
| 619 | 'name' => $name, |
| 620 | 'short_description' => __( 'The WordPress.com API returned a 404 error.', 'jetpack' ), |
| 621 | ) |
| 622 | ); |
| 623 | } |
| 624 | |
| 625 | $result = json_decode( $body ); |
| 626 | $is_connected = ! empty( $result->connected ); |
| 627 | $message = $result->message . ': ' . wp_remote_retrieve_response_code( $response ); |
| 628 | |
| 629 | if ( $is_connected ) { |
| 630 | $res = self::passing_test( array( 'name' => $name ) ); |
| 631 | } else { |
| 632 | $res = self::connection_failing_test( $name, $message ); |
| 633 | } |
| 634 | |
| 635 | return $res; |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Tests the port number to ensure it is an expected value. |
| 640 | * |
| 641 | * We expect that sites on be on one of: |
| 642 | * port 80, |
| 643 | * port 443 (https sites only), |
| 644 | * the value of JETPACK_SIGNATURE__HTTP_PORT, |
| 645 | * unless the site is intentionally on a different port (e.g. example.com:8080 is the site's URL). |
| 646 | * |
| 647 | * If the value isn't one of those and the site's URL doesn't include a port, then the signature verification will fail. |
| 648 | * |
| 649 | * This happens most commonly on sites with reverse proxies, so the edge (e.g. Varnish) is running on 80/443, but nginx |
| 650 | * or Apache is responding internally on a different port (e.g. 81). |
| 651 | * |
| 652 | * @return array Test results |
| 653 | */ |
| 654 | protected function test__server_port_value() { |
| 655 | $name = __FUNCTION__; |
| 656 | if ( ! isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) && ! isset( $_SERVER['SERVER_PORT'] ) ) { |
| 657 | return self::skipped_test( |
| 658 | array( |
| 659 | 'name' => $name, |
| 660 | 'short_description' => __( 'The server port values are not defined. This is most common when running PHP via a CLI.', 'jetpack' ), |
| 661 | ) |
| 662 | ); |
| 663 | } |
| 664 | $site_port = wp_parse_url( home_url(), PHP_URL_PORT ); |
| 665 | $server_port = isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ? (int) $_SERVER['HTTP_X_FORWARDED_PORT'] : (int) $_SERVER['SERVER_PORT']; |
| 666 | $http_ports = array( 80 ); |
| 667 | $https_ports = array( 80, 443 ); |
| 668 | |
| 669 | if ( defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) ) { |
| 670 | $http_ports[] = JETPACK_SIGNATURE__HTTP_PORT; |
| 671 | } |
| 672 | |
| 673 | if ( defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) ) { |
| 674 | $https_ports[] = JETPACK_SIGNATURE__HTTPS_PORT; |
| 675 | } |
| 676 | |
| 677 | if ( $site_port ) { |
| 678 | return self::skipped_test( array( 'name' => $name ) ); // Not currently testing for this situation. |
| 679 | } |
| 680 | |
| 681 | if ( is_ssl() && in_array( $server_port, $https_ports, true ) ) { |
| 682 | return self::passing_test( array( 'name' => $name ) ); |
| 683 | } elseif ( in_array( $server_port, $http_ports, true ) ) { |
| 684 | return self::passing_test( array( 'name' => $name ) ); |
| 685 | } else { |
| 686 | if ( is_ssl() ) { |
| 687 | $needed_constant = 'JETPACK_SIGNATURE__HTTPS_PORT'; |
| 688 | } else { |
| 689 | $needed_constant = 'JETPACK_SIGNATURE__HTTP_PORT'; |
| 690 | } |
| 691 | return self::failing_test( |
| 692 | array( |
| 693 | 'name' => $name, |
| 694 | 'short_description' => sprintf( |
| 695 | /* translators: %1$s - a PHP code snippet */ |
| 696 | __( |
| 697 | 'The server port value is unexpected. |
| 698 | Try adding the following to your wp-config.php file: %1$s', |
| 699 | 'jetpack' |
| 700 | ), |
| 701 | "define( '$needed_constant', $server_port )" |
| 702 | ), |
| 703 | ) |
| 704 | ); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Sync Health Tests. |
| 710 | * |
| 711 | * Disabled: Results in a failing test (recommended) |
| 712 | * Delayed: Results in failing test (recommended) |
| 713 | * Error: Results in failing test (critical) |
| 714 | */ |
| 715 | protected function test__sync_health() { |
| 716 | |
| 717 | $name = __FUNCTION__; |
| 718 | |
| 719 | if ( ! $this->helper_is_jetpack_connected() ) { |
| 720 | // If the site is not connected, there is no point in testing Sync health. |
| 721 | return self::skipped_test( |
| 722 | array( |
| 723 | 'name' => $name, |
| 724 | 'show_in_site_health' => false, |
| 725 | ) |
| 726 | ); |
| 727 | } |
| 728 | |
| 729 | // Sync is disabled. |
| 730 | if ( ! Sync_Settings::is_sync_enabled() ) { |
| 731 | return self::failing_test( |
| 732 | array( |
| 733 | 'name' => $name, |
| 734 | 'label' => __( 'Jetpack Sync has been disabled on your site.', 'jetpack' ), |
| 735 | 'severity' => 'recommended', |
| 736 | 'action' => 'https://github.com/Automattic/jetpack/blob/trunk/projects/packages/sync/src/class-settings.php', |
| 737 | 'action_label' => __( 'See Github for more on Sync Settings', 'jetpack' ), |
| 738 | 'short_description' => __( 'Jetpack Sync has been disabled on your site. This could be impacting some of your site’s Jetpack-powered features. Developers may enable / disable syncing using the Sync Settings API.', 'jetpack' ), |
| 739 | ) |
| 740 | ); |
| 741 | } |
| 742 | // Sync has experienced Data Loss. |
| 743 | if ( Sync_Health::get_status() === Sync_Health::STATUS_OUT_OF_SYNC ) { |
| 744 | return self::failing_test( |
| 745 | array( |
| 746 | 'name' => $name, |
| 747 | 'label' => __( 'Jetpack has detected a problem with the communication between your site and WordPress.com', 'jetpack' ), |
| 748 | 'severity' => 'critical', |
| 749 | 'action' => Redirect::get_url( 'jetpack-contact-support' ), |
| 750 | 'action_label' => __( 'Contact Jetpack Support', 'jetpack' ), |
| 751 | 'short_description' => __( 'There is a problem with the communication between your site and WordPress.com. This could be impacting some of your site’s Jetpack-powered features. If you continue to see this error, please contact support for assistance.', 'jetpack' ), |
| 752 | ) |
| 753 | ); |
| 754 | |
| 755 | } |
| 756 | |
| 757 | return self::passing_test( array( 'name' => $name ) ); |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Calls to WP.com to run the connection diagnostic testing suite. |
| 762 | * |
| 763 | * Intentionally added last as it will be skipped if any local failed conditions exist. |
| 764 | * |
| 765 | * @since 7.1.0 |
| 766 | * @since 7.9.0 Timeout waiting for a WP.com response no longer fails the test. Test is marked skipped instead. |
| 767 | * |
| 768 | * @return array Test results. |
| 769 | */ |
| 770 | protected function last__wpcom_self_test() { |
| 771 | $name = 'test__wpcom_self_test'; |
| 772 | |
| 773 | $status = new Status(); |
| 774 | if ( ! Jetpack::is_connection_ready() || $status->is_offline_mode() || $status->in_safe_mode() || ! $this->pass ) { |
| 775 | return self::skipped_test( array( 'name' => $name ) ); |
| 776 | } |
| 777 | |
| 778 | $self_xml_rpc_url = site_url( 'xmlrpc.php' ); |
| 779 | |
| 780 | $testsite_url = JETPACK__API_BASE . 'testsite/1/?url='; |
| 781 | |
| 782 | // 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. |
| 783 | add_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ), PHP_INT_MAX - 1 ); |
| 784 | |
| 785 | $response = wp_remote_get( $testsite_url . $self_xml_rpc_url ); |
| 786 | |
| 787 | remove_filter( 'http_request_timeout', array( 'Jetpack_Cxn_Tests', 'increase_timeout' ), PHP_INT_MAX - 1 ); |
| 788 | |
| 789 | if ( 200 === wp_remote_retrieve_response_code( $response ) ) { |
| 790 | $result = self::passing_test( array( 'name' => $name ) ); |
| 791 | } elseif ( is_wp_error( $response ) && str_contains( $response->get_error_message(), 'cURL error 28' ) ) { // Timeout. |
| 792 | $result = self::skipped_test( |
| 793 | array( |
| 794 | 'name' => $name, |
| 795 | 'short_description' => self::helper_get_timeout_text(), |
| 796 | ) |
| 797 | ); |
| 798 | } else { |
| 799 | $result = self::failing_test( |
| 800 | array( |
| 801 | 'name' => $name, |
| 802 | 'short_description' => sprintf( |
| 803 | /* translators: %1$s - A debugging url */ |
| 804 | __( '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' ), |
| 805 | Redirect::get_url( 'jetpack-support-debug', array( 'query' => 'url=' . rawurlencode( site_url() ) ) ) |
| 806 | ), |
| 807 | 'action_label' => $this->helper_get_support_text(), |
| 808 | 'action' => $this->helper_get_support_url(), |
| 809 | ) |
| 810 | ); |
| 811 | } |
| 812 | |
| 813 | return $result; |
| 814 | } |
| 815 | } |
| 816 |