Skip to content

Commit efe06cd

Browse files
Site Health, REST API: Move async tests to REST API endpoints.
This provides more flexibility when writing tests and benefits from running in a front-end context which is necessary for some tests like checking that updates are supported. Additionally, this provides a more robust interface for developers who want to integrate with Site Health tests. Because the `wp/v2` endpoint is reserved for modeling core entities, site health is registered in its own `wp-site-health/v1` namespace. The existing ajax actions have been maintained for backward compatibility. Props Clorith, chrisvanpatten, afragen, pokhriyal, TimothyBlynJacobs. Fixes #48105. git-svn-id: https://develop.svn.wordpress.org/trunk@49154 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 50fa352 commit efe06cd

12 files changed

Lines changed: 682 additions & 91 deletions

File tree

src/js/_enqueues/admin/site-health.js

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jQuery( document ).ready( function( $ ) {
1111
var __ = wp.i18n.__,
1212
_n = wp.i18n._n,
1313
sprintf = wp.i18n.sprintf,
14-
data,
1514
clipboard = new ClipboardJS( '.site-health-copy-buttons .copy-button' ),
1615
isDebugTab = $( '.health-check-body.health-check-debug-tab' ).length,
1716
pathsSizesSection = $( '#health-check-accordion-block-wp-paths-sizes' ),
@@ -78,11 +77,16 @@ jQuery( document ).ready( function( $ ) {
7877
issueWrapper = $( '#health-check-issues-' + issue.status ),
7978
heading,
8079
count;
81-
80+
8281
SiteHealth.site_status.issues[ issue.status ]++;
8382

8483
count = SiteHealth.site_status.issues[ issue.status ];
8584

85+
// If no test name is supplied, append a placeholder for markup references.
86+
if ( typeof issue.test === 'undefined' ) {
87+
issue.test = issue.status + count;
88+
}
89+
8690
if ( 'critical' === issue.status ) {
8791
heading = sprintf(
8892
_n( '%s critical issue', '%s critical issues', count ),
@@ -119,10 +123,10 @@ jQuery( document ).ready( function( $ ) {
119123
var $progressLabel = $( '.site-health-progress-label', $wrapper );
120124
var $circle = $( '.site-health-progress svg #bar' );
121125
var totalTests = parseInt( SiteHealth.site_status.issues.good, 0 ) +
122-
parseInt( SiteHealth.site_status.issues.recommended, 0 ) +
123-
( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
126+
parseInt( SiteHealth.site_status.issues.recommended, 0 ) +
127+
( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
124128
var failedTests = ( parseInt( SiteHealth.site_status.issues.recommended, 0 ) * 0.5 ) +
125-
( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
129+
( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
126130
var val = 100 - Math.ceil( ( failedTests / totalTests ) * 100 );
127131

128132
if ( 0 === totalTests ) {
@@ -206,15 +210,49 @@ jQuery( document ).ready( function( $ ) {
206210

207211
this.completed = true;
208212

209-
$.post(
210-
ajaxurl,
211-
data,
212-
function( response ) {
213+
if ( 'undefined' !== typeof( this.has_rest ) && this.has_rest ) {
214+
wp.apiRequest( {
215+
url: this.test
216+
} )
217+
.done( function( response ) {
218+
/** This filter is documented in wp-admin/includes/class-wp-site-health.php */
219+
appendIssue( wp.hooks.applyFilters( 'site_status_test_result', response ) );
220+
} )
221+
.fail( function( response ) {
222+
var description;
223+
224+
if ( 'undefined' !== typeof( response.responseJSON ) && 'undefined' !== typeof( response.responseJSON.message ) ) {
225+
description = response.responseJSON.message;
226+
} else {
227+
description = __( 'No details available' );
228+
}
229+
230+
addFailedSiteHealthCheckNotice( this.url, description );
231+
} )
232+
.always( function() {
233+
maybeRunNextAsyncTest();
234+
} );
235+
} else {
236+
$.post(
237+
ajaxurl,
238+
data
239+
).done( function( response ) {
213240
/** This filter is documented in wp-admin/includes/class-wp-site-health.php */
214241
appendIssue( wp.hooks.applyFilters( 'site_status_test_result', response.data ) );
242+
} ).fail( function( response ) {
243+
var description;
244+
245+
if ( 'undefined' !== typeof( response.responseJSON ) && 'undefined' !== typeof( response.responseJSON.message ) ) {
246+
description = response.responseJSON.message;
247+
} else {
248+
description = __( 'No details available' );
249+
}
250+
251+
addFailedSiteHealthCheckNotice( this.url, description );
252+
} ).always( function() {
215253
maybeRunNextAsyncTest();
216-
}
217-
);
254+
} );
255+
}
218256

219257
return false;
220258
} );
@@ -225,6 +263,29 @@ jQuery( document ).ready( function( $ ) {
225263
}
226264
}
227265

266+
/**
267+
* Add the details of a failed asynchronous test to the list of test results.
268+
*
269+
* @since 5.6.0
270+
*/
271+
function addFailedSiteHealthCheckNotice( url, description ) {
272+
var issue;
273+
274+
issue = {
275+
'status': 'recommended',
276+
'label': __( 'A test is unavailable' ),
277+
'badge': {
278+
'color': 'red',
279+
'label': __( 'Unavailable' )
280+
},
281+
'description': '<p>' + url + '</p><p>' + description + '</p>',
282+
'actions': ''
283+
};
284+
285+
/** This filter is documented in wp-admin/includes/class-wp-site-health.php */
286+
appendIssue( wp.hooks.applyFilters( 'site_status_test_result', issue ) );
287+
}
288+
228289
if ( 'undefined' !== typeof SiteHealth && ! isDebugTab ) {
229290
if ( 0 === SiteHealth.site_status.direct.length && 0 === SiteHealth.site_status.async.length ) {
230291
recalculateProgression();
@@ -243,53 +304,31 @@ jQuery( document ).ready( function( $ ) {
243304
}
244305

245306
if ( 0 < SiteHealth.site_status.async.length ) {
246-
data = {
247-
'action': 'health-check-' + SiteHealth.site_status.async[0].test.replace( '_', '-' ),
248-
'_wpnonce': SiteHealth.nonce.site_status
249-
};
250-
251-
SiteHealth.site_status.async[0].completed = true;
252-
253-
$.post(
254-
ajaxurl,
255-
data,
256-
function( response ) {
257-
appendIssue( response.data );
258-
maybeRunNextAsyncTest();
259-
}
260-
);
307+
maybeRunNextAsyncTest();
261308
} else {
262309
recalculateProgression();
263310
}
264311
}
265312

266313
function getDirectorySizes() {
267-
var data = {
268-
action: 'health-check-get-sizes',
269-
_wpnonce: SiteHealth.nonce.site_status_result
270-
};
271-
272314
var timestamp = ( new Date().getTime() );
273315

274316
// After 3 seconds announce that we're still waiting for directory sizes.
275317
var timeout = window.setTimeout( function() {
276318
wp.a11y.speak( __( 'Please wait...' ) );
277319
}, 3000 );
278320

279-
$.post( {
280-
type: 'POST',
281-
url: ajaxurl,
282-
data: data,
283-
dataType: 'json'
321+
wp.apiRequest( {
322+
path: '/wp-site-health/v1/directory-sizes'
284323
} ).done( function( response ) {
285-
updateDirSizes( response.data || {} );
324+
updateDirSizes( response || {} );
286325
} ).always( function() {
287326
var delay = ( new Date().getTime() ) - timestamp;
288327

289328
$( '.health-check-wp-paths-sizes.spinner' ).css( 'visibility', 'hidden' );
290329
recalculateProgression();
291330

292-
if ( delay > 3000 ) {
331+
if ( delay > 3000 ) {
293332
/*
294333
* We have announced that we're waiting.
295334
* Announce that we're ready after giving at least 3 seconds

src/wp-admin/admin-ajax.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,15 @@
143143
);
144144

145145
// Deprecated.
146-
$core_actions_post_deprecated = array( 'wp-fullscreen-save-post', 'press-this-save-post', 'press-this-add-category' );
146+
$core_actions_post_deprecated = array(
147+
'wp-fullscreen-save-post',
148+
'press-this-save-post',
149+
'press-this-add-category',
150+
'health-check-dotorg-communication',
151+
'health-check-is-in-debug-mode',
152+
'health-check-background-updates',
153+
'health-check-loopback-requests',
154+
);
147155
$core_actions_post = array_merge( $core_actions_post, $core_actions_post_deprecated );
148156

149157
// Register core Ajax calls.

src/wp-admin/includes/ajax-actions.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5142,8 +5142,21 @@ function wp_ajax_wp_privacy_erase_personal_data() {
51425142
* Ajax handler for site health checks on server communication.
51435143
*
51445144
* @since 5.2.0
5145+
* @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_dotorg_communication()
5146+
* @see WP_REST_Site_Health_Controller::test_dotorg_communication()
51455147
*/
51465148
function wp_ajax_health_check_dotorg_communication() {
5149+
_doing_it_wrong(
5150+
'wp_ajax_health_check_dotorg_communication',
5151+
sprintf(
5152+
// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
5153+
__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
5154+
'wp_ajax_health_check_dotorg_communication',
5155+
'WP_REST_Site_Health_Controller::test_dotorg_communication'
5156+
),
5157+
'5.6.0'
5158+
);
5159+
51475160
check_ajax_referer( 'health-check-site-status' );
51485161

51495162
if ( ! current_user_can( 'view_site_health_checks' ) ) {
@@ -5162,8 +5175,21 @@ function wp_ajax_health_check_dotorg_communication() {
51625175
* Ajax handler for site health checks on background updates.
51635176
*
51645177
* @since 5.2.0
5178+
* @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_background_updates()
5179+
* @see WP_REST_Site_Health_Controller::test_background_updates()
51655180
*/
51665181
function wp_ajax_health_check_background_updates() {
5182+
_doing_it_wrong(
5183+
'wp_ajax_health_check_background_updates',
5184+
sprintf(
5185+
// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
5186+
__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
5187+
'wp_ajax_health_check_background_updates',
5188+
'WP_REST_Site_Health_Controller::test_background_updates'
5189+
),
5190+
'5.6.0'
5191+
);
5192+
51675193
check_ajax_referer( 'health-check-site-status' );
51685194

51695195
if ( ! current_user_can( 'view_site_health_checks' ) ) {
@@ -5182,8 +5208,21 @@ function wp_ajax_health_check_background_updates() {
51825208
* Ajax handler for site health checks on loopback requests.
51835209
*
51845210
* @since 5.2.0
5211+
* @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_loopback_requests()
5212+
* @see WP_REST_Site_Health_Controller::test_loopback_requests()
51855213
*/
51865214
function wp_ajax_health_check_loopback_requests() {
5215+
_doing_it_wrong(
5216+
'wp_ajax_health_check_loopback_requests',
5217+
sprintf(
5218+
// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
5219+
__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
5220+
'wp_ajax_health_check_loopback_requests',
5221+
'WP_REST_Site_Health_Controller::test_loopback_requests'
5222+
),
5223+
'5.6.0'
5224+
);
5225+
51875226
check_ajax_referer( 'health-check-site-status' );
51885227

51895228
if ( ! current_user_can( 'view_site_health_checks' ) ) {
@@ -5219,8 +5258,21 @@ function wp_ajax_health_check_site_status_result() {
52195258
* Ajax handler for site health check to get directories and database sizes.
52205259
*
52215260
* @since 5.2.0
5261+
* @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::get_directory_sizes()
5262+
* @see WP_REST_Site_Health_Controller::get_directory_sizes()
52225263
*/
52235264
function wp_ajax_health_check_get_sizes() {
5265+
_doing_it_wrong(
5266+
'wp_ajax_health_check_get_sizes',
5267+
sprintf(
5268+
// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
5269+
__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
5270+
'wp_ajax_health_check_get_sizes',
5271+
'WP_REST_Site_Health_Controller::get_directory_sizes'
5272+
),
5273+
'5.6.0'
5274+
);
5275+
52245276
check_ajax_referer( 'health-check-site-status-result' );
52255277

52265278
if ( ! current_user_can( 'view_site_health_checks' ) || is_multisite() ) {

src/wp-admin/includes/class-wp-site-health-auto-updates.php

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -90,46 +90,7 @@ public function test_constants( $constant, $value ) {
9090
* @return array The test results.
9191
*/
9292
public function test_wp_version_check_attached() {
93-
if ( ! is_main_site() ) {
94-
return;
95-
}
96-
97-
$cookies = wp_unslash( $_COOKIE );
98-
$timeout = 10;
99-
$headers = array(
100-
'Cache-Control' => 'no-cache',
101-
);
102-
/** This filter is documented in wp-includes/class-wp-http-streams.php */
103-
$sslverify = apply_filters( 'https_local_ssl_verify', false );
104-
105-
// Include Basic auth in loopback requests.
106-
if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
107-
$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
108-
}
109-
110-
$url = add_query_arg(
111-
array(
112-
'health-check-test-wp_version_check' => true,
113-
),
114-
admin_url( 'site-health.php' )
115-
);
116-
117-
$test = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
118-
119-
if ( is_wp_error( $test ) ) {
120-
return array(
121-
'description' => sprintf(
122-
/* translators: %s: Name of the filter used. */
123-
__( 'Could not confirm that the %s filter is available.' ),
124-
'<code>wp_version_check()</code>'
125-
),
126-
'severity' => 'warning',
127-
);
128-
}
129-
130-
$response = wp_remote_retrieve_body( $test );
131-
132-
if ( 'yes' !== $response ) {
93+
if ( ! has_filter( 'wp_version_check', 'wp_version_check' ) ) {
13394
return array(
13495
'description' => sprintf(
13596
/* translators: %s: Name of the filter used. */
@@ -310,6 +271,11 @@ public function test_vcs_abspath() {
310271
* @return array The test results.
311272
*/
312273
function test_check_wp_filesystem_method() {
274+
// Make sure the `request_filesystem_credentials` function is available during our REST call.
275+
if ( ! function_exists( 'request_filesystem_credentials' ) ) {
276+
require_once ABSPATH . '/wp-admin/includes/file.php';
277+
}
278+
313279
$skin = new Automatic_Upgrader_Skin;
314280
$success = $skin->request_filesystem_credentials( false, ABSPATH );
315281

@@ -356,6 +322,11 @@ function test_all_files_writable() {
356322
return false;
357323
}
358324

325+
// Make sure the `get_core_checksums` function is available during our REST call.
326+
if ( ! function_exists( 'get_core_checksums' ) ) {
327+
require_once ABSPATH . '/wp-admin/includes/update.php';
328+
}
329+
359330
$checksums = get_core_checksums( $wp_version, 'en_US' );
360331
$dev = ( false !== strpos( $wp_version, '-' ) );
361332
// Get the last stable version's files and test against that.

0 commit comments

Comments
 (0)