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
+9 -1
View File
@@ -143,7 +143,15 @@ $core_actions_post = array(
);
// Deprecated.
$core_actions_post_deprecated = array( 'wp-fullscreen-save-post', 'press-this-save-post', 'press-this-add-category' );
$core_actions_post_deprecated = array(
'wp-fullscreen-save-post',
'press-this-save-post',
'press-this-add-category',
'health-check-dotorg-communication',
'health-check-is-in-debug-mode',
'health-check-background-updates',
'health-check-loopback-requests',
);
$core_actions_post = array_merge( $core_actions_post, $core_actions_post_deprecated );
// Register core Ajax calls.
+52
View File
@@ -5142,8 +5142,21 @@ function wp_ajax_wp_privacy_erase_personal_data() {
* Ajax handler for site health checks on server communication.
*
* @since 5.2.0
* @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_dotorg_communication()
* @see WP_REST_Site_Health_Controller::test_dotorg_communication()
*/
function wp_ajax_health_check_dotorg_communication() {
_doing_it_wrong(
'wp_ajax_health_check_dotorg_communication',
sprintf(
// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
'wp_ajax_health_check_dotorg_communication',
'WP_REST_Site_Health_Controller::test_dotorg_communication'
),
'5.6.0'
);
check_ajax_referer( 'health-check-site-status' );
if ( ! current_user_can( 'view_site_health_checks' ) ) {
@@ -5162,8 +5175,21 @@ function wp_ajax_health_check_dotorg_communication() {
* Ajax handler for site health checks on background updates.
*
* @since 5.2.0
* @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_background_updates()
* @see WP_REST_Site_Health_Controller::test_background_updates()
*/
function wp_ajax_health_check_background_updates() {
_doing_it_wrong(
'wp_ajax_health_check_background_updates',
sprintf(
// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
'wp_ajax_health_check_background_updates',
'WP_REST_Site_Health_Controller::test_background_updates'
),
'5.6.0'
);
check_ajax_referer( 'health-check-site-status' );
if ( ! current_user_can( 'view_site_health_checks' ) ) {
@@ -5182,8 +5208,21 @@ function wp_ajax_health_check_background_updates() {
* Ajax handler for site health checks on loopback requests.
*
* @since 5.2.0
* @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_loopback_requests()
* @see WP_REST_Site_Health_Controller::test_loopback_requests()
*/
function wp_ajax_health_check_loopback_requests() {
_doing_it_wrong(
'wp_ajax_health_check_loopback_requests',
sprintf(
// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
'wp_ajax_health_check_loopback_requests',
'WP_REST_Site_Health_Controller::test_loopback_requests'
),
'5.6.0'
);
check_ajax_referer( 'health-check-site-status' );
if ( ! current_user_can( 'view_site_health_checks' ) ) {
@@ -5219,8 +5258,21 @@ function wp_ajax_health_check_site_status_result() {
* Ajax handler for site health check to get directories and database sizes.
*
* @since 5.2.0
* @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::get_directory_sizes()
* @see WP_REST_Site_Health_Controller::get_directory_sizes()
*/
function wp_ajax_health_check_get_sizes() {
_doing_it_wrong(
'wp_ajax_health_check_get_sizes',
sprintf(
// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
'wp_ajax_health_check_get_sizes',
'WP_REST_Site_Health_Controller::get_directory_sizes'
),
'5.6.0'
);
check_ajax_referer( 'health-check-site-status-result' );
if ( ! current_user_can( 'view_site_health_checks' ) || is_multisite() ) {
@@ -90,46 +90,7 @@ class WP_Site_Health_Auto_Updates {
* @return array The test results.
*/
public function test_wp_version_check_attached() {
if ( ! is_main_site() ) {
return;
}
$cookies = wp_unslash( $_COOKIE );
$timeout = 10;
$headers = array(
'Cache-Control' => 'no-cache',
);
/** This filter is documented in wp-includes/class-wp-http-streams.php */
$sslverify = apply_filters( 'https_local_ssl_verify', false );
// Include Basic auth in loopback requests.
if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
}
$url = add_query_arg(
array(
'health-check-test-wp_version_check' => true,
),
admin_url( 'site-health.php' )
);
$test = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
if ( is_wp_error( $test ) ) {
return array(
'description' => sprintf(
/* translators: %s: Name of the filter used. */
__( 'Could not confirm that the %s filter is available.' ),
'<code>wp_version_check()</code>'
),
'severity' => 'warning',
);
}
$response = wp_remote_retrieve_body( $test );
if ( 'yes' !== $response ) {
if ( ! has_filter( 'wp_version_check', 'wp_version_check' ) ) {
return array(
'description' => sprintf(
/* translators: %s: Name of the filter used. */
@@ -310,6 +271,11 @@ class WP_Site_Health_Auto_Updates {
* @return array The test results.
*/
function test_check_wp_filesystem_method() {
// Make sure the `request_filesystem_credentials` function is available during our REST call.
if ( ! function_exists( 'request_filesystem_credentials' ) ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
}
$skin = new Automatic_Upgrader_Skin;
$success = $skin->request_filesystem_credentials( false, ABSPATH );
@@ -356,6 +322,11 @@ class WP_Site_Health_Auto_Updates {
return false;
}
// Make sure the `get_core_checksums` function is available during our REST call.
if ( ! function_exists( 'get_core_checksums' ) ) {
require_once ABSPATH . '/wp-admin/includes/update.php';
}
$checksums = get_core_checksums( $wp_version, 'en_US' );
$dev = ( false !== strpos( $wp_version, '-' ) );
// Get the last stable version's files and test against that.
+15 -9
View File
@@ -133,6 +133,7 @@ class WP_Site_Health {
if ( is_string( $test['test'] ) ) {
$health_check_js_variables['site_status']['async'][] = array(
'test' => $test['test'],
'has_rest' => ( isset( $test['has_rest'] ) ? $test['has_rest'] : false ),
'completed' => false,
);
}
@@ -2080,6 +2081,7 @@ class WP_Site_Health {
* experiences.
*
* @since 5.2.0
* @since 5.6.0 Added support for `has_rest` and `permissions`.
*
* @return array The list of tests to run.
*/
@@ -2153,16 +2155,19 @@ class WP_Site_Health {
),
'async' => array(
'dotorg_communication' => array(
'label' => __( 'Communication with WordPress.org' ),
'test' => 'dotorg_communication',
'label' => __( 'Communication with WordPress.org' ),
'test' => rest_url( 'wp-site-health/v1/tests/dotorg-communication' ),
'has_rest' => true,
),
'background_updates' => array(
'label' => __( 'Background updates' ),
'test' => 'background_updates',
'label' => __( 'Background updates' ),
'test' => rest_url( 'wp-site-health/v1/tests/background-updates' ),
'has_rest' => true,
),
'loopback_requests' => array(
'label' => __( 'Loopback request' ),
'test' => 'loopback_requests',
'label' => __( 'Loopback request' ),
'test' => rest_url( 'wp-site-health/v1/tests/loopback-requests' ),
'has_rest' => true,
),
),
);
@@ -2199,9 +2204,10 @@ class WP_Site_Health {
* Plugins and themes are encouraged to prefix test identifiers with their slug
* to avoid any collisions between tests.
*
* @type string $label A friendly label for your test to identify it by.
* @type mixed $test A callable to perform a direct test, or a string Ajax action to be called
* to perform an async test.
* @type string $label A friendly label for your test to identify it by.
* @type mixed $test A callable to perform a direct test, or a string AJAX action to be
* called to perform an async test.
* @type boolean $has_rest Optional. Denote if `$test` has a REST API endpoint.
* }
* }
*/