💥 TRENDING: Changeset/ - Full Gallery 2025

Changeset 49154


Ignore:
Timestamp:
10/15/2020 01:58:28 AM (5 years ago)
Author:
TimothyBlynJacobs
Message:

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.

Location:
trunk
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/admin/site-health.js

    r48301 r49154  
    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,
     
    7978            heading,
    8079            count;
    81        
     80
    8281        SiteHealth.site_status.issues[ issue.status ]++;
    8382
    8483        count = SiteHealth.site_status.issues[ issue.status ];
     84
     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        }
    8589
    8690        if ( 'critical' === issue.status ) {
     
    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
     
    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;
     
    224262            recalculateProgression();
    225263        }
     264    }
     265
     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 ) );
    226287    }
    227288
     
    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();
     
    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
     
    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;
     
    290329            recalculateProgression();
    291330
    292             if ( delay > 3000  ) {
     331            if ( delay > 3000 ) {
    293332                /*
    294333                 * We have announced that we're waiting.
  • trunk/src/wp-admin/admin-ajax.php

    r47835 r49154  
    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
  • trunk/src/wp-admin/includes/ajax-actions.php

    r49122 r49154  
    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 */
    51465148function 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
     
    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 */
    51665181function 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
     
    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 */
    51865214function 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
     
    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 */
    52235264function 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
  • trunk/src/wp-admin/includes/class-wp-site-health-auto-updates.php

    r48792 r49154  
    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             );