Нет описания

class-jetpack-cxn-tests.php 33KB

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