Completed
Push — update/podcast-player-improve-... ( dccb93...6d0826 )
by
unknown
203:21 queued 196:23
created

debug-functions.php ➔ jetpack_debugger_full_sync_start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
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
117
	$ajax_nonce = wp_create_nonce( 'jetpack-site-health' );
118
119
	if ( 'site-health.php' === $hook ) {
120
		$wp_scripts = wp_scripts();
121
		wp_enqueue_script( 'jquery-ui-progressbar' );
122
		wp_enqueue_script(
123
			'jetpack_debug_site_health_script',
124
			plugins_url( 'jetpack-debugger-site-health.js', __FILE__ ),
125
			array( 'jquery-ui-progressbar' ),
126
			JETPACK__VERSION,
127
			false
128
		);
129
		wp_enqueue_style(
130
			'jetpack_debug_site_health_styles',
131
			plugins_url( 'jetpack-debugger-site-health.css', __FILE__ ),
132
			false,
133
			JETPACK__VERSION,
134
			false
135
		);
136
		/* WordPress is not bundled with jquery UI styles - we need to grab them from the Google API. */
137
		wp_enqueue_style(
138
			'jetpack-jquery-ui-styles',
139
			'https://code.jquery.com/ui/' . $wp_scripts->registered['jquery-ui-core']->ver . '/themes/smoothness/jquery-ui.min.css',
140
			false,
141
			JETPACK__VERSION,
142
			false
143
		);
144
		wp_localize_script(
145
			'jetpack_debug_site_health_script',
146
			'jetpackSiteHealth',
147
			array(
148
				'ajaxUrl'             => admin_url( 'admin-ajax.php' ),
149
				'syncProgressHeading' => __( 'Jetpack is performing a sync of your site', 'jetpack' ),
150
				'progressPercent'     => $progress_percent,
151
				'fullSyncNonce'       => $ajax_nonce,
152
			)
153
		);
154
	}
155
}
156
157
/**
158
 * Responds to ajax calls from the site health page. Echos a full sync percantage to update progress bar.
159
 */
160
function jetpack_debugger_sync_progress_ajax() {
161
	$full_sync_module = Modules::get_module( 'full-sync' );
162
	$progress_percent = $full_sync_module ? $full_sync_module->get_sync_progress_percentage() : null;
163
	if ( ! $progress_percent ) {
164
		echo 'done';
165
		wp_die();
166
	}
167
	echo intval( $progress_percent );
168
	wp_die();
169
}
170
171
/**
172
 * Responds to ajax calls from the site health page. Triggers a Full Sync
173
 */
174
function jetpack_debugger_full_sync_start() {
175
	check_ajax_referer( 'jetpack-site-health', 'site-health-nonce' );
176
	$full_sync_module = Modules::get_module( 'full-sync' );
177
	$full_sync_module->start();
178
	echo 'requested';
179
	wp_die();
180
}
181