Site Health: Allow any callable added via `site_status_tests` filter to return test results for direct tests.

Async tests still need to be a string for the AJAX action.

Props kraftbj.
Fixes #46836.

git-svn-id: https://develop.svn.wordpress.org/trunk@45234 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-04-17 14:34:12 +00:00
parent 4206fe3e05
commit 60fd53ef96
1 changed files with 25 additions and 15 deletions

View File

@ -71,11 +71,14 @@ class WP_Site_Health {
if ( 'site-health' === $screen->id && ! isset( $_GET['tab'] ) ) { if ( 'site-health' === $screen->id && ! isset( $_GET['tab'] ) ) {
$tests = WP_Site_Health::get_tests(); $tests = WP_Site_Health::get_tests();
// Don't run https test on localhost // Don't run https test on localhost
if ( 'localhost' === preg_replace( '|https?://|', '', get_site_url() ) ) { if ( 'localhost' === preg_replace( '|https?://|', '', get_site_url() ) ) {
unset( $tests['direct']['https_status'] ); unset( $tests['direct']['https_status'] );
} }
foreach ( $tests['direct'] as $test ) { foreach ( $tests['direct'] as $test ) {
if ( is_string( $test['test'] ) ) {
$test_function = sprintf( $test_function = sprintf(
'get_test_%s', 'get_test_%s',
$test['test'] $test['test']
@ -83,18 +86,24 @@ class WP_Site_Health {
if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) { if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
$health_check_js_variables['site_status']['direct'][] = call_user_func( array( $this, $test_function ) ); $health_check_js_variables['site_status']['direct'][] = call_user_func( array( $this, $test_function ) );
} else { continue;
}
}
if ( is_callable( $test['test'] ) ) {
$health_check_js_variables['site_status']['direct'][] = call_user_func( $test['test'] ); $health_check_js_variables['site_status']['direct'][] = call_user_func( $test['test'] );
} }
} }
foreach ( $tests['async'] as $test ) { foreach ( $tests['async'] as $test ) {
if ( is_string( $test['test'] ) ) {
$health_check_js_variables['site_status']['async'][] = array( $health_check_js_variables['site_status']['async'][] = array(
'test' => $test['test'], 'test' => $test['test'],
'completed' => false, 'completed' => false,
); );
} }
} }
}
wp_localize_script( 'site-health', 'SiteHealth', $health_check_js_variables ); wp_localize_script( 'site-health', 'SiteHealth', $health_check_js_variables );
} }
@ -1798,7 +1807,7 @@ class WP_Site_Health {
} }
/** /**
* Add or modify which site status tests are ran on a site. * Add or modify which site status tests are run on a site.
* *
* The site health is determined by a set of tests based on best practices from * The site health is determined by a set of tests based on best practices from
* both the WordPress Hosting Team, but also web standards in general. * both the WordPress Hosting Team, but also web standards in general.
@ -1807,7 +1816,7 @@ class WP_Site_Health {
* checks may be handled by a host, and are therefore disabled in core. * checks may be handled by a host, and are therefore disabled in core.
* Or maybe you want to introduce a new test, is caching enabled/disabled/stale for example. * Or maybe you want to introduce a new test, is caching enabled/disabled/stale for example.
* *
* Test may be added either as direct, or asynchronous ones. Any test that may require some time * Tests may be added either as direct, or asynchronous ones. Any test that may require some time
* to complete should run asynchronously, to avoid extended loading periods within wp-admin. * to complete should run asynchronously, to avoid extended loading periods within wp-admin.
* *
* @since 5.2.0 * @since 5.2.0
@ -1822,7 +1831,8 @@ class WP_Site_Health {
* to avoid any collisions between tests. * to avoid any collisions between tests.
* *
* @type string $label A friendly label for your test to identify it by. * @type string $label A friendly label for your test to identify it by.
* @type string $test The ajax action to be called to perform the tests. * @type mixed $test A callable to perform a direct test, or a string AJAX action to be called
* to perform an async test.
* } * }
* } * }
*/ */