Make WordPress Core

Changeset 49232


Ignore:
Timestamp:
10/20/2020 02:48:50 PM (3 years ago)
Author:
desrosj
Message:

Site Health: Improve the reliability of asynchronous tests.

This change adds additional logic to catch HTTP failures that do not return a WP_Error object (for example, a wp-json REST API error error).

This also fixes instances where REST API callbacks performed from cron do not work due to a lack of authentication by introducing a direct callback route that asynchronous tests can register.

Props dd32, clorith, timothyblynjacobs.
Fixes #51547.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-site-health.php

    r49215 r49232  
    21562156            'async'  => array(
    21572157                'dotorg_communication' => array(
    2158                     'label'    => __( 'Communication with WordPress.org' ),
    2159                     'test'     => rest_url( 'wp-site-health/v1/tests/dotorg-communication' ),
    2160                     'has_rest' => true,
     2158                    'label'             => __( 'Communication with WordPress.org' ),
     2159                    'test'              => rest_url( 'wp-site-health/v1/tests/dotorg-communication' ),
     2160                    'has_rest'          => true,
     2161                    'async_direct_test' => array( WP_Site_Health::get_instance(), 'get_test_dotorg_communication' ),
    21612162                ),
    21622163                'background_updates'   => array(
    2163                     'label'    => __( 'Background updates' ),
    2164                     'test'     => rest_url( 'wp-site-health/v1/tests/background-updates' ),
    2165                     'has_rest' => true,
     2164                    'label'             => __( 'Background updates' ),
     2165                    'test'              => rest_url( 'wp-site-health/v1/tests/background-updates' ),
     2166                    'has_rest'          => true,
     2167                    'async_direct_test' => array( WP_Site_Health::get_instance(), 'get_test_background_updates' ),
    21662168                ),
    21672169                'loopback_requests'    => array(
    2168                     'label'    => __( 'Loopback request' ),
    2169                     'test'     => rest_url( 'wp-site-health/v1/tests/loopback-requests' ),
    2170                     'has_rest' => true,
     2170                    'label'             => __( 'Loopback request' ),
     2171                    'test'              => rest_url( 'wp-site-health/v1/tests/loopback-requests' ),
     2172                    'has_rest'          => true,
     2173                    'async_direct_test' => array( WP_Site_Health::get_instance(), 'get_test_loopback_requests' ),
    21712174                ),
    21722175            ),
     
    22052208         *         to avoid any collisions between tests.
    22062209         *
    2207          *         @type string  $label       A friendly label for your test to identify it by.
    2208          *         @type mixed   $test        A callable to perform a direct test, or a string AJAX action to be
    2209          *                                    called to perform an async test.
    2210          *         @type boolean $has_rest    Optional. Denote if `$test` has a REST API endpoint.
     2210         *         @type string   $label              A friendly label for your test to identify it by.
     2211         *         @type mixed    $test               A callable to perform a direct test, or a string AJAX action to be
     2212         *                                            called to perform an async test.
     2213         *         @type boolean  $has_rest           Optional. Denote if `$test` has a REST API endpoint.
     2214         *         @type callable $async_direct_test  A manner of directly calling the test marked as asynchronous, as
     2215         *                                            the scheduled event can not authenticate, and endpoints may require
     2216         *                                            authentication.
    22112217         *     }
    22122218         * }
     
    25512557
    25522558        foreach ( $tests['async'] as $test ) {
     2559            // Local endpoints may require authentication, so asynchronous tests can pass a direct test runner as well.
     2560            if ( ! empty( $test['async_direct_test'] ) && is_callable( $test['async_direct_test'] ) ) {
     2561                // This test is callable, do so and continue to the next asynchronous check.
     2562                $results[] = $this->perform_test( $test['async_direct_test'] );
     2563                continue;
     2564            }
     2565
    25532566            if ( is_string( $test['test'] ) ) {
     2567                // Check if this test has a REST API endpoint.
    25542568                if ( isset( $test['has_rest'] ) && $test['has_rest'] ) {
    2555                     $result_fetch = wp_remote_post(
    2556                         rest_url( $test['test'] ),
     2569                    $result_fetch = wp_remote_get(
     2570                        $test['test'],
    25572571                        array(
    25582572                            'body' => array(
     
    25732587                }
    25742588
    2575                 if ( ! is_wp_error( $result_fetch ) ) {
     2589                if ( ! is_wp_error( $result_fetch ) && 200 === wp_remote_retrieve_response_code( $result_fetch ) ) {
    25762590                    $result = json_decode( wp_remote_retrieve_body( $result_fetch ), true );
    25772591                } else {
Note: See TracChangeset for help on using the changeset viewer.