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
This commit is contained in:
Timothy Jacobs
2020-10-15 01:58:28 +00:00
parent 50fa352cbb
commit efe06cdcea
12 changed files with 682 additions and 91 deletions
+77 -38
View File
@@ -11,7 +11,6 @@ jQuery( document ).ready( function( $ ) {
var __ = wp.i18n.__,
_n = wp.i18n._n,
sprintf = wp.i18n.sprintf,
data,
clipboard = new ClipboardJS( '.site-health-copy-buttons .copy-button' ),
isDebugTab = $( '.health-check-body.health-check-debug-tab' ).length,
pathsSizesSection = $( '#health-check-accordion-block-wp-paths-sizes' ),
@@ -78,11 +77,16 @@ jQuery( document ).ready( function( $ ) {
issueWrapper = $( '#health-check-issues-' + issue.status ),
heading,
count;
SiteHealth.site_status.issues[ issue.status ]++;
count = SiteHealth.site_status.issues[ issue.status ];
// If no test name is supplied, append a placeholder for markup references.
if ( typeof issue.test === 'undefined' ) {
issue.test = issue.status + count;
}
if ( 'critical' === issue.status ) {
heading = sprintf(
_n( '%s critical issue', '%s critical issues', count ),
@@ -119,10 +123,10 @@ jQuery( document ).ready( function( $ ) {
var $progressLabel = $( '.site-health-progress-label', $wrapper );
var $circle = $( '.site-health-progress svg #bar' );
var totalTests = parseInt( SiteHealth.site_status.issues.good, 0 ) +
parseInt( SiteHealth.site_status.issues.recommended, 0 ) +
( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
parseInt( SiteHealth.site_status.issues.recommended, 0 ) +
( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
var failedTests = ( parseInt( SiteHealth.site_status.issues.recommended, 0 ) * 0.5 ) +
( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
var val = 100 - Math.ceil( ( failedTests / totalTests ) * 100 );
if ( 0 === totalTests ) {
@@ -206,15 +210,49 @@ jQuery( document ).ready( function( $ ) {
this.completed = true;
$.post(
ajaxurl,
data,
function( response ) {
if ( 'undefined' !== typeof( this.has_rest ) && this.has_rest ) {
wp.apiRequest( {
url: this.test
} )
.done( function( response ) {
/** This filter is documented in wp-admin/includes/class-wp-site-health.php */
appendIssue( wp.hooks.applyFilters( 'site_status_test_result', response ) );
} )
.fail( function( response ) {
var description;
if ( 'undefined' !== typeof( response.responseJSON ) && 'undefined' !== typeof( response.responseJSON.message ) ) {
description = response.responseJSON.message;
} else {
description = __( 'No details available' );
}
addFailedSiteHealthCheckNotice( this.url, description );
} )
.always( function() {
maybeRunNextAsyncTest();
} );
} else {
$.post(
ajaxurl,
data
).done( function( response ) {
/** This filter is documented in wp-admin/includes/class-wp-site-health.php */
appendIssue( wp.hooks.applyFilters( 'site_status_test_result', response.data ) );
} ).fail( function( response ) {
var description;
if ( 'undefined' !== typeof( response.responseJSON ) && 'undefined' !== typeof( response.responseJSON.message ) ) {
description = response.responseJSON.message;
} else {
description = __( 'No details available' );
}
addFailedSiteHealthCheckNotice( this.url, description );
} ).always( function() {
maybeRunNextAsyncTest();
}
);
} );
}
return false;
} );
@@ -225,6 +263,29 @@ jQuery( document ).ready( function( $ ) {
}
}
/**
* Add the details of a failed asynchronous test to the list of test results.
*
* @since 5.6.0
*/
function addFailedSiteHealthCheckNotice( url, description ) {
var issue;
issue = {
'status': 'recommended',
'label': __( 'A test is unavailable' ),
'badge': {
'color': 'red',
'label': __( 'Unavailable' )
},
'description': '<p>' + url + '</p><p>' + description + '</p>',
'actions': ''
};
/** This filter is documented in wp-admin/includes/class-wp-site-health.php */
appendIssue( wp.hooks.applyFilters( 'site_status_test_result', issue ) );
}
if ( 'undefined' !== typeof SiteHealth && ! isDebugTab ) {
if ( 0 === SiteHealth.site_status.direct.length && 0 === SiteHealth.site_status.async.length ) {
recalculateProgression();
@@ -243,32 +304,13 @@ jQuery( document ).ready( function( $ ) {
}
if ( 0 < SiteHealth.site_status.async.length ) {
data = {
'action': 'health-check-' + SiteHealth.site_status.async[0].test.replace( '_', '-' ),
'_wpnonce': SiteHealth.nonce.site_status
};
SiteHealth.site_status.async[0].completed = true;
$.post(
ajaxurl,
data,
function( response ) {
appendIssue( response.data );
maybeRunNextAsyncTest();
}
);
maybeRunNextAsyncTest();
} else {
recalculateProgression();
}
}
function getDirectorySizes() {
var data = {
action: 'health-check-get-sizes',
_wpnonce: SiteHealth.nonce.site_status_result
};
var timestamp = ( new Date().getTime() );
// After 3 seconds announce that we're still waiting for directory sizes.
@@ -276,20 +318,17 @@ jQuery( document ).ready( function( $ ) {
wp.a11y.speak( __( 'Please wait...' ) );
}, 3000 );
$.post( {
type: 'POST',
url: ajaxurl,
data: data,
dataType: 'json'
wp.apiRequest( {
path: '/wp-site-health/v1/directory-sizes'
} ).done( function( response ) {
updateDirSizes( response.data || {} );
updateDirSizes( response || {} );
} ).always( function() {
var delay = ( new Date().getTime() ) - timestamp;
$( '.health-check-wp-paths-sizes.spinner' ).css( 'visibility', 'hidden' );
recalculateProgression();
if ( delay > 3000 ) {
if ( delay > 3000 ) {
/*
* We have announced that we're waiting.
* Announce that we're ready after giving at least 3 seconds