Completed
Push — fix/site-health ( 2772ac )
by
unknown
06:45
created

debug-functions.php ➔ jetpack_debugger_site_status_tests()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 72

Duplication

Lines 9
Ratio 12.5 %

Importance

Changes 0
Metric Value
cc 8
nc 2
nop 1
dl 9
loc 72
rs 7.3664
c 0
b 0
f 0

How to fix   Long Method   

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
use Automattic\Jetpack\Sync\Modules;
9
/**
10
 * Test runner for Core's Site Health module.
11
 *
12
 * @since 7.3.0
13
 */
14
function jetpack_debugger_ajax_local_testing_suite() {
15
	check_ajax_referer( 'health-check-site-status' );
16
	if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
17
		wp_send_json_error();
18
	}
19
	$tests = new Jetpack_Cxn_Tests();
20
	wp_send_json_success( $tests->output_results_for_core_async_site_health() );
21
}
22
/**
23
 * Adds the Jetpack Local Testing Suite to the Core Site Health system.
24
 *
25
 * @since 7.3.0
26
 *
27
 * @param array $core_tests Array of tests from Core's Site Health.
28
 *
29
 * @return array $core_tests Array of tests for Core's Site Health.
30
 */
31
function jetpack_debugger_site_status_tests( $core_tests ) {
32
	$cxn_tests = new Jetpack_Cxn_Tests();
33
	$tests     = $cxn_tests->list_tests( 'direct' );
34
	foreach ( $tests as $test ) {
35
36
		$core_tests['direct'][ $test['name'] ] = array(
37
			'label' => __( 'Jetpack: ', 'jetpack' ) . $test['name'],
38
			'test'  => function() use ( $test, $cxn_tests ) { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewClosure.Found
39
				$results = $cxn_tests->run_test( $test['name'] );
40
				if ( is_wp_error( $results ) ) {
41
					return;
42
				}
43
44
				$label = $results['label'] ?
45
					$results['label'] :
46
					ucwords(
47
						str_replace(
48
							'_',
49
							' ',
50
							str_replace( 'test__', '', $test['name'] )
51
						)
52
					);
53
				if ( $results['long_description'] ) {
54
					$description = $results['long_description'];
55
				} elseif ( $results['short_description'] ) {
56
					$description = sprintf(
57
						'<p>%s</p>',
58
						$results['short_description']
59
					);
60
				} else {
61
					$description = sprintf(
62
						'<p>%s</p>',
63
						__( 'This test successfully passed!', 'jetpack' )
64
					);
65
				}
66
67
				$return = array(
68
					'label'       => $label,
69
					'status'      => 'good',
70
					'badge'       => array(
71
						'label' => __( 'Jetpack', 'jetpack' ),
72
						'color' => 'green',
73
					),
74
					'description' => $description,
75
					'actions'     => '',
76
					'test'        => 'jetpack_' . $test['name'],
77
				);
78
79
				if ( false === $results['pass'] ) {
80
					$return['status'] = $results['severity'];
81 View Code Duplication
					if ( ! empty( $results['action'] ) ) {
82
						$return['actions'] = sprintf(
83
							'<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>',
84
							esc_url( $results['action'] ),
85
							$results['action_label'],
86
							/* translators: accessibility text */
87
							__( '(opens in a new tab)', 'jetpack' )
88
						);
89
					}
90
				}
91
92
				return $return;
93
			},
94
		);
95
	}
96
	$core_tests['async']['jetpack_test_suite'] = array(
97
		'label' => __( 'Jetpack Tests', 'jetpack' ),
98
		'test'  => 'jetpack_local_testing_suite',
99
	);
100
101
	return $core_tests;
102
}
103
104
/**
105
 * Loads site health scripts if we are on the site health page.
106
 *
107
 * @param string $hook The current admin page hook.
108
 */
109
function jetpack_debugger_enqueue_site_health_scripts( $hook ) {
110
	$full_sync_module = Modules::get_module( 'full-sync' );
111
	$progress_percent = $full_sync_module ? $full_sync_module->get_sync_progress_percentage() : false;
112
113
	$ajax_nonce = wp_create_nonce( 'jetpack-site-health' );
114
115
	if ( 'site-health.php' === $hook ) {
116
		$wp_scripts = wp_scripts();
117
		wp_enqueue_script( 'jquery-ui-progressbar' );
118
		wp_enqueue_script(
119
			'jetpack_debug_site_health_script',
120
			plugins_url( 'jetpack-debugger-site-health.js', __FILE__ ),
121
			array( 'jquery-ui-progressbar' ),
122
			JETPACK__VERSION,
123
			false
124
		);
125
		wp_enqueue_style(
126
			'jetpack_debug_site_health_styles',
127
			plugins_url( 'jetpack-debugger-site-health.css', __FILE__ ),
128
			false,
129
			JETPACK__VERSION,
130
			false
131
		);
132
		/* WordPress is not bundled with jquery UI styles - we need to grab them from the Google API. */
133
		wp_enqueue_style(
134
			'jetpack-jquery-ui-styles',
135
			'https://code.jquery.com/ui/' . $wp_scripts->registered['jquery-ui-core']->ver . '/themes/smoothness/jquery-ui.min.css',
136
			false,
137
			JETPACK__VERSION,
138
			false
139
		);
140
		wp_localize_script(
141
			'jetpack_debug_site_health_script',
142
			'jetpackSiteHealth',
143
			array(
144
				'ajaxUrl'             => admin_url( 'admin-ajax.php' ),
145
				'syncProgressHeading' => __( 'Jetpack is performing a sync of your site', 'jetpack' ),
146
				'progressPercent'     => $progress_percent,
147
				'fullSyncNonce'       => $ajax_nonce,
148
			)
149
		);
150
	}
151
}
152
153
/**
154
 * Responds to ajax calls from the site health page. Echos a full sync percantage to update progress bar.
155
 */
156
function jetpack_debugger_sync_progress_ajax() {
157
	$full_sync_module = Modules::get_module( 'full-sync' );
158
	$progress_percent = $full_sync_module ? $full_sync_module->get_sync_progress_percentage() : null;
159
	if ( ! $progress_percent ) {
160
		echo 'done';
161
		wp_die();
162
	}
163
	echo intval( $progress_percent );
164
	wp_die();
165
}
166
167
/**
168
 * Responds to ajax calls from the site health page. Triggers a Full Sync
169
 */
170
function jetpack_debugger_full_sync_start() {
171
	check_ajax_referer( 'jetpack-site-health', 'site-health-nonce' );
172
	$full_sync_module = Modules::get_module( 'full-sync' );
173
	$full_sync_module->start();
174
	echo 'requested';
175
	wp_die();
176
}
177