Completed
Push — instant-search-master ( 404687...5ddf14 )
by
unknown
10:34 queued 04:11
created

debug-functions.php ➔ jetpack_debugger_site_status_tests()   C

Complexity

Conditions 10
Paths 2

Size

Total Lines 83

Duplication

Lines 9
Ratio 10.84 %

Importance

Changes 0
Metric Value
cc 10
nc 2
nop 1
dl 9
loc 83
rs 6.5042
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * WP Site Health functionality temporarily stored in this file until all of Jetpack is PHP 5.3+
4
 *
5
 * @package 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
		$core_tests['direct'][ $test['name'] ] = array(
35
			'label' => __( 'Jetpack: ', 'jetpack' ) . $test['name'],
36
			'test'  => function() use ( $test, $cxn_tests ) { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewClosure.Found
37
				$results = $cxn_tests->run_test( $test['name'] );
38
				// Test names are, by default, `test__some_string_of_text`. Let's convert to "Some String Of Text" for humans.
39
				$label = ucwords(
40
					str_replace(
41
						'_',
42
						' ',
43
						str_replace( 'test__', '', $test['name'] )
44
					)
45
				);
46
				$return = array(
47
					'label'       => $label,
48
					'status'      => 'good',
49
					'badge'       => array(
50
						'label' => __( 'Jetpack', 'jetpack' ),
51
						'color' => 'green',
52
					),
53
					'description' => sprintf(
54
						'<p>%s</p>',
55
						__( 'This test successfully passed!', 'jetpack' )
56
					),
57
					'actions'     => '',
58
					'test'        => 'jetpack_' . $test['name'],
59
				);
60
				if ( is_wp_error( $results ) ) {
61
					return;
62
				}
63
				if ( false === $results['pass'] ) {
64
					$return['label'] = $results['message'];
65
					if ( $results['label'] ) {
66
						// Allow tests to override the strange message => label logic with an actual label.
67
						$return['label'] = $results['label'];
68
					}
69
70
					// Most tests pass a `resolution` property to use as a description.
71
					$return['description'] = sprintf(
72
						'<p>%s</p>',
73
						$results['resolution']
74
					);
75
76
					if ( $results['description'] ) {
77
						// Allow tests to override 'resolution' with their own HTML description.
78
						$return['description'] = $results['description'];
79
					}
80
81
					$return['status'] = $results['severity'];
82 View Code Duplication
					if ( ! empty( $results['action'] ) ) {
83
						$return['actions'] = sprintf(
84
							'<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>',
85
							esc_url( $results['action'] ),
86
							$results['action_label'],
87
							/* translators: accessibility text */
88
							__( '(opens in a new tab)', 'jetpack' )
89
						);
90
					}
91
				} elseif ( true === $results['pass'] ) {
92
					// Passing tests can chose to override defaults.
93
					if ( $results['label'] ) {
94
						$return['label'] = $results['label'];
95
					}
96
97
					if ( $results['description'] ) {
98
						$return['description'] = $results['description'];
99
					}
100
				}
101
102
				return $return;
103
			},
104
		);
105
	}
106
	$core_tests['async']['jetpack_test_suite'] = array(
107
		'label' => __( 'Jetpack Tests', 'jetpack' ),
108
		'test'  => 'jetpack_local_testing_suite',
109
	);
110
111
	return $core_tests;
112
}
113
114