class-jetpack-cxn-test-base.php
1 year ago
class-jetpack-cxn-tests.php
2 years ago
class-jetpack-debug-data.php
2 years ago
class-jetpack-debugger.php
1 year ago
debug-functions.php
2 years ago
debug-functions.php
119 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WP Site Health debugging functions. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Test runner for Core's Site Health module. |
| 10 | * |
| 11 | * @since 7.3.0 |
| 12 | */ |
| 13 | function jetpack_debugger_ajax_local_testing_suite() { |
| 14 | check_ajax_referer( 'health-check-site-status' ); |
| 15 | if ( ! current_user_can( 'jetpack_manage_modules' ) ) { |
| 16 | wp_send_json_error(); |
| 17 | } |
| 18 | $tests = new Jetpack_Cxn_Tests(); |
| 19 | wp_send_json_success( $tests->output_results_for_core_async_site_health() ); |
| 20 | } |
| 21 | /** |
| 22 | * Adds the Jetpack Local Testing Suite to the Core Site Health system. |
| 23 | * |
| 24 | * @since 7.3.0 |
| 25 | * |
| 26 | * @param array $core_tests Array of tests from Core's Site Health. |
| 27 | * |
| 28 | * @return array $core_tests Array of tests for Core's Site Health. |
| 29 | */ |
| 30 | function jetpack_debugger_site_status_tests( $core_tests ) { |
| 31 | $cxn_tests = new Jetpack_Cxn_Tests(); |
| 32 | $tests = $cxn_tests->list_tests( 'direct' ); |
| 33 | foreach ( $tests as $test ) { |
| 34 | |
| 35 | $core_tests['direct'][ $test['name'] ] = array( |
| 36 | 'label' => __( 'Jetpack: ', 'jetpack' ) . $test['name'], |
| 37 | /** |
| 38 | * Callable for Core's Site Health system to execute. |
| 39 | * |
| 40 | * @var array $test A Jetpack Testing Suite test array. |
| 41 | * @var Jetpack_Cxn_Tests $cxn_tests An instance of the Jetpack Test Suite. |
| 42 | * |
| 43 | * @return array { |
| 44 | * A results array to match the format expected by WordPress Core. |
| 45 | * |
| 46 | * @type string $label Name for the test. |
| 47 | * @type string $status 'critical', 'recommended', or 'good'. |
| 48 | * @type array $badge Array for Site Health status. Keys label and color. |
| 49 | * @type string $description Description of the test result. |
| 50 | * @type string $action HTML to a link to resolve issue. |
| 51 | * @type string $test Unique test identifier. |
| 52 | * } |
| 53 | */ |
| 54 | 'test' => function () use ( $test, $cxn_tests ) { |
| 55 | $results = $cxn_tests->run_test( $test['name'] ); |
| 56 | if ( is_wp_error( $results ) ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | $label = $results['label'] ? |
| 61 | $results['label'] : |
| 62 | ucwords( |
| 63 | str_replace( |
| 64 | '_', |
| 65 | ' ', |
| 66 | str_replace( 'test__', '', $test['name'] ) |
| 67 | ) |
| 68 | ); |
| 69 | if ( $results['long_description'] ) { |
| 70 | $description = $results['long_description']; |
| 71 | } elseif ( $results['short_description'] ) { |
| 72 | $description = sprintf( |
| 73 | '<p>%s</p>', |
| 74 | $results['short_description'] |
| 75 | ); |
| 76 | } else { |
| 77 | $description = sprintf( |
| 78 | '<p>%s</p>', |
| 79 | __( 'This test successfully passed!', 'jetpack' ) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | $return = array( |
| 84 | 'label' => $label, |
| 85 | 'status' => 'good', |
| 86 | 'badge' => array( |
| 87 | 'label' => __( 'Jetpack', 'jetpack' ), |
| 88 | 'color' => 'green', |
| 89 | ), |
| 90 | 'description' => $description, |
| 91 | 'actions' => '', |
| 92 | 'test' => 'jetpack_' . $test['name'], |
| 93 | ); |
| 94 | |
| 95 | if ( false === $results['pass'] ) { |
| 96 | $return['status'] = $results['severity']; |
| 97 | if ( ! empty( $results['action'] ) ) { |
| 98 | $return['actions'] = sprintf( |
| 99 | '<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
| 100 | esc_url( $results['action'] ), |
| 101 | $results['action_label'], |
| 102 | /* translators: accessibility text */ |
| 103 | __( '(opens in a new tab)', 'jetpack' ) |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $return; |
| 109 | }, |
| 110 | ); |
| 111 | } |
| 112 | $core_tests['async']['jetpack_test_suite'] = array( |
| 113 | 'label' => __( 'Jetpack Tests', 'jetpack' ), |
| 114 | 'test' => 'jetpack_local_testing_suite', |
| 115 | ); |
| 116 | |
| 117 | return $core_tests; |
| 118 | } |
| 119 |