Completed
Push — add/sync-health-tests ( f3209b...3765ce )
by
unknown
199:58 queued 193:25
created

debug-functions.php ➔ jetpack_debugger_sync_progress_ajax()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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
				if ( isset( $results['show_in_site_health'] ) && false === $results['show_in_site_health'] ) {
45
					return;
46
				}
47
48
				$label = $results['label'] ?
49
					$results['label'] :
50
					ucwords(
51
						str_replace(
52
							'_',
53
							' ',
54
							str_replace( 'test__', '', $test['name'] )
55
						)
56
					);
57
				if ( $results['long_description'] ) {
58
					$description = $results['long_description'];
59
				} elseif ( $results['short_description'] ) {
60
					$description = sprintf(
61
						'<p>%s</p>',
62
						$results['short_description']
63
					);
64
				} else {
65
					$description = sprintf(
66
						'<p>%s</p>',
67
						__( 'This test successfully passed!', 'jetpack' )
68
					);
69
				}
70
71
				$return = array(
72
					'label'       => $label,
73
					'status'      => 'good',
74
					'badge'       => array(
75
						'label' => __( 'Jetpack', 'jetpack' ),
76
						'color' => 'green',
77
					),
78
					'description' => $description,
79
					'actions'     => '',
80
					'test'        => 'jetpack_' . $test['name'],
81
				);
82
83
				if ( false === $results['pass'] ) {
84
					$return['status'] = $results['severity'];
85 View Code Duplication
					if ( ! empty( $results['action'] ) ) {
86
						$return['actions'] = sprintf(
87
							'<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>',
88
							esc_url( $results['action'] ),
89
							$results['action_label'],
90
							/* translators: accessibility text */
91
							__( '(opens in a new tab)', 'jetpack' )
92
						);
93
					}
94
				}
95
96
				return $return;
97
			},
98
		);
99
	}
100
	$core_tests['async']['jetpack_test_suite'] = array(
101
		'label' => __( 'Jetpack Tests', 'jetpack' ),
102
		'test'  => 'jetpack_local_testing_suite',
103
	);
104
105
	return $core_tests;
106
}
107
108
/**
109
 * Loads site health scripts if we are on the site health page.
110
 *
111
 * @param string $hook The current admin page hook.
112
 */
113
function jetpack_debugger_enqueue_site_health_scripts( $hook ) {
114
	$full_sync_module = Modules::get_module( 'full-sync' );
115
	$progress_percent = $full_sync_module ? $full_sync_module->get_sync_progress_percentage() : false;
116
	if ( 'site-health.php' === $hook ) {
117
		$wp_scripts = wp_scripts();
118
		wp_enqueue_script( 'jquery-ui-progressbar' );
119
		wp_enqueue_script(
120
			'jetpack_debug_site_health_script',
121
			plugins_url( 'jetpack-debugger-site-health.js', __FILE__ ),
122
			array( 'jquery-ui-progressbar' ),
123
			JETPACK__VERSION,
124
			false
125
		);
126
		wp_enqueue_style(
127
			'jetpack_debug_site_health_styles',
128
			plugins_url( 'jetpack-debugger-site-health.css', __FILE__ ),
129
			false,
130
			JETPACK__VERSION,
131
			false
132
		);
133
		/* WordPress is not bundled with jquery UI styles - we need to grab them from the Google API. */
134
		wp_enqueue_style(
135
			'jetpack-jquery-ui-styles',
136
			'https://code.jquery.com/ui/' . $wp_scripts->registered['jquery-ui-core']->ver . '/themes/smoothness/jquery-ui.min.css',
137
			false,
138
			JETPACK__VERSION,
139
			false
140
		);
141
		wp_localize_script(
142
			'jetpack_debug_site_health_script',
143
			'jetpackSiteHealth',
144
			array(
145
				'ajaxUrl'             => admin_url( 'admin-ajax.php' ),
146
				'syncProgressHeading' => __( 'Jetpack is performing a sync of your site', 'jetpack' ),
147
				'progressPercent'     => $progress_percent,
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