Make WordPress Core

Ticket #48105: 48105.2.patch

File 48105.2.patch, 20.4 KB (added by Clorith, 6 years ago)
  • src/js/_enqueues/admin/site-health.js

     
    162162
    163163                if ( 1 <= SiteHealth.site_status.async.length ) {
    164164                        $.each( SiteHealth.site_status.async, function() {
     165                                var url = ajaxurl;
    165166                                var data = {
    166167                                        'action': 'health-check-' + this.test.replace( '_', '-' ),
    167168                                        '_wpnonce': SiteHealth.nonce.site_status
    168169                                };
    169170
     171                                if ( "undefined" !== typeof( this.has_rest ) && this.has_rest ) {
     172                                        url = SiteHealth.rest_base + this.test;
     173                                        data = {
     174                                                '_wpnonce': SiteHealth.nonce.rest_api
     175                                        };
     176                                }
     177
    170178                                if ( this.completed ) {
    171179                                        return true;
    172180                                }
     
    176184                                this.completed = true;
    177185
    178186                                $.post(
    179                                         ajaxurl,
    180                                         data,
    181                                         function( response ) {
    182                                                 /** This filter is documented in wp-admin/includes/class-wp-site-health.php */
    183                                                 AppendIssue( wp.hooks.applyFilters( 'site_status_test_result', response.data ) );
    184                                                 maybeRunNextAsyncTest();
    185                                         }
    186                                 );
     187                                        url,
     188                                        data
     189                                ).done( function( response ) {
     190                                        /** This filter is documented in wp-admin/includes/class-wp-site-health.php */
     191                                        AppendIssue( wp.hooks.applyFilters( 'site_status_test_result', response.data ) );
     192                                } ).fail( function( response ) {
     193                                        var description, issue;
     194
     195                                        if ( "undefined" !== typeof( response.responseJSON ) && "undefined" !== typeof( response.responseJSON.message ) ) {
     196                                                description = response.responseJSON.message;
     197                                        } else {
     198                                                description = __( 'No details available' );
     199                                        }
     200
     201                                        issue = {
     202                                                'status': 'recommended',
     203                                                'label': __( 'A test is unavailable' ),
     204                                                'badge': {
     205                                                        'color': 'red',
     206                                                        'label': __( 'Unavailable' )
     207                                                },
     208                                                'description': '<p>' + this.url + '</p><p>' + description + '</p>',
     209                                                'actions': ''
     210                                        };
     211
     212                                        /** This filter is documented in wp-admin/includes/class-wp-site-health.php */
     213                                        AppendIssue( wp.hooks.applyFilters( 'site_status_test_result', issue ) );
     214                                } ).always( function() {
     215                                        maybeRunNextAsyncTest();
     216                                } );
    187217
    188218                                return false;
    189219                        } );
     
    212242                }
    213243
    214244                if ( 0 < SiteHealth.site_status.async.length ) {
    215                         data = {
    216                                 'action': 'health-check-' + SiteHealth.site_status.async[0].test.replace( '_', '-' ),
    217                                 '_wpnonce': SiteHealth.nonce.site_status
    218                         };
    219 
    220                         SiteHealth.site_status.async[0].completed = true;
    221 
    222                         $.post(
    223                                 ajaxurl,
    224                                 data,
    225                                 function( response ) {
    226                                         AppendIssue( response.data );
    227                                         maybeRunNextAsyncTest();
    228                                 }
    229                         );
     245                        maybeRunNextAsyncTest();
    230246                } else {
    231247                        RecalculateProgression();
    232248                }
     
    234250
    235251        function getDirectorySizes() {
    236252                var data = {
    237                         action: 'health-check-get-sizes',
    238                         _wpnonce: SiteHealth.nonce.site_status_result
     253                        _wpnonce: SiteHealth.nonce.rest_api
    239254                };
    240255
    241256                var timestamp = ( new Date().getTime() );
     
    246261                }, 3000 );
    247262
    248263                $.post( {
    249                         type: 'POST',
    250                         url: ajaxurl,
     264                        url: SiteHealth.rest_base + 'wp/v2/site-health/get-directory-sizes',
    251265                        data: data,
    252266                        dataType: 'json'
    253267                } ).done( function( response ) {
    254                         updateDirSizes( response.data || {} );
     268                        updateDirSizes( response || {} );
    255269                } ).always( function() {
    256270                        var delay = ( new Date().getTime() ) - timestamp;
    257271
  • src/wp-admin/includes/ajax-actions.php

     
    51065106 * Ajax handler for site health checks on server communication.
    51075107 *
    51085108 * @since 5.2.0
     5109 * @deprecated 5.4.0 Use WP_REST_Site_Health_Controller::test_dotorg_communication()
     5110 * @see WP_REST_Site_Health_Controller::test_dotorg_communication()
    51095111 */
    51105112function wp_ajax_health_check_dotorg_communication() {
     5113        _doing_it_wrong(
     5114                'wp_ajax_health_check_dotorg_communication',
     5115                sprintf(
     5116                        // translators: 1: The Site Health action that is no longer used by core. 2: The new function taht replaces it.
     5117                        __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
     5118                        'wp_ajax_health_check_dotorg_communication',
     5119                        'WP_REST_Site_Health_Controller::test_dotorg_communication'
     5120                ),
     5121                '5.4.0'
     5122        );
     5123
    51115124        check_ajax_referer( 'health-check-site-status' );
    51125125
    51135126        if ( ! current_user_can( 'view_site_health_checks' ) ) {
     
    51265139 * Ajax handler for site health checks on debug mode.
    51275140 *
    51285141 * @since 5.2.0
     5142 * @deprecated 5.4.0 Use WP_REST_Site_Health_Controller::test_is_in_debug_mode()
     5143 * @see WP_REST_Site_Health_Controller::test_is_in_debug_mode()
    51295144 */
    51305145function wp_ajax_health_check_is_in_debug_mode() {
     5146        _doing_it_wrong(
     5147                'wp_ajax_health_check_is_in_debug_mode',
     5148                sprintf(
     5149                        // translators: 1: The Site Health action that is no longer used by core. 2: The new function taht replaces it.
     5150                        __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
     5151                        'wp_ajax_health_check_is_in_debug_mode',
     5152                        'WP_REST_Site_Health_Controller::test_is_in_debug_mode'
     5153                ),
     5154                '5.4.0'
     5155        );
     5156
    51315157        wp_verify_nonce( 'health-check-site-status' );
    51325158
    51335159        if ( ! current_user_can( 'view_site_health_checks' ) ) {
     
    51465172 * Ajax handler for site health checks on background updates.
    51475173 *
    51485174 * @since 5.2.0
     5175 * @deprecated 5.4.0 Use WP_REST_Site_Health_Controller::test_background_updates()
     5176 * @see WP_REST_Site_Health_Controller::test_background_updates()
    51495177 */
    51505178function wp_ajax_health_check_background_updates() {
     5179        _doing_it_wrong(
     5180                'wp_ajax_health_check_background_updates',
     5181                sprintf(
     5182                // translators: 1: The Site Health action that is no longer used by core. 2: The new function taht replaces it.
     5183                        __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
     5184                        'wp_ajax_health_check_background_updates',
     5185                        'WP_REST_Site_Health_Controller::test_background_updates'
     5186                ),
     5187                '5.4.0'
     5188        );
     5189
    51515190        check_ajax_referer( 'health-check-site-status' );
    51525191
    51535192        if ( ! current_user_can( 'view_site_health_checks' ) ) {
     
    51675206 * Ajax handler for site health checks on loopback requests.
    51685207 *
    51695208 * @since 5.2.0
     5209 * @deprecated 5.4.0 Use WP_REST_Site_Health_Controller::test_loopback_requests()
     5210 * @see WP_REST_Site_Health_Controller::test_loopback_requests()
    51705211 */
    51715212function wp_ajax_health_check_loopback_requests() {
     5213        _doing_it_wrong(
     5214                'wp_ajax_health_check_loopback_requests',
     5215                sprintf(
     5216                // translators: 1: The Site Health action that is no longer used by core. 2: The new function taht replaces it.
     5217                        __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
     5218                        'wp_ajax_health_check_loopback_requests',
     5219                        'WP_REST_Site_Health_Controller::test_loopback_requests'
     5220                ),
     5221                '5.4.0'
     5222        );
     5223
    51725224        check_ajax_referer( 'health-check-site-status' );
    51735225
    51745226        if ( ! current_user_can( 'view_site_health_checks' ) ) {
     
    52045256 * Ajax handler for site health check to get directories and database sizes.
    52055257 *
    52065258 * @since 5.2.0
     5259 * @deprecated 5.4.0 Use WP_REST_Site_Health_Controller::get_directory_sizes()
     5260 * @see WP_REST_Site_Health_Controller::get_directory_sizes()
    52075261 */
    52085262function wp_ajax_health_check_get_sizes() {
     5263        _doing_it_wrong(
     5264                'wp_ajax_health_check_get_sizes',
     5265                sprintf(
     5266                // translators: 1: The Site Health action that is no longer used by core. 2: The new function taht replaces it.
     5267                        __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
     5268                        'wp_ajax_health_check_get_sizes',
     5269                        'WP_REST_Site_Health_Controller::get_directory_sizes'
     5270                ),
     5271                '5.4.0'
     5272        );
     5273
    52095274        check_ajax_referer( 'health-check-site-status-result' );
    52105275
    52115276        if ( ! current_user_can( 'view_site_health_checks' ) || is_multisite() ) {
  • src/wp-includes/rest-api/endpoints/class-wp-rest-site-health-controller.php

     
     1<?php
     2/**
     3 * REST API: WP_REST_Site_Health_Controller class
     4 *
     5 * @package WordPress
     6 * @subpackage REST_API
     7 * @since 5.4.0
     8 */
     9
     10/**
     11 * Core class for interacting with Site Health elements.
     12 *
     13 * @since 5.4.0
     14 *
     15 * @see WP_REST_Controller
     16 */
     17class WP_REST_Site_Health_Controller extends WP_REST_Controller {
     18
     19        private $site_health;
     20
     21        /**
     22         * Constructor.
     23         */
     24        public function __construct() {
     25                if ( ! class_exists( 'WP_Site_Health' ) ) {
     26                        require_once( ABSPATH . 'wp-admin/includes/class-wp-site-health.php' );
     27                }
     28
     29                $this->site_health = new WP_Site_Health();
     30
     31                $this->namespace = 'wp/v2';
     32                $this->rest_base = 'site-health';
     33        }
     34
     35        /**
     36         * Register API routes.
     37         *
     38         * @since 5.4.0
     39         *
     40         * @see register_rest_route()
     41         */
     42        public function register_routes() {
     43                register_rest_route(
     44                        $this->namespace,
     45                        sprintf(
     46                                '/%s/%s',
     47                                $this->rest_base,
     48                                'test-background-updates'
     49                        ),
     50                        array(
     51                                'methods'             => 'POST',
     52                                'callback'            => array( $this, 'test_background_updates' ),
     53                                'permission_callback' => function() {
     54                                        return $this->validate_request_permission( 'test-background-updates' );
     55                                },
     56                        )
     57                );
     58
     59                register_rest_route(
     60                        $this->namespace,
     61                        sprintf(
     62                                '/%s/%s',
     63                                $this->rest_base,
     64                                'test-loopback-requests'
     65                        ),
     66                        array(
     67                                'methods'             => 'POST',
     68                                'callback'            => array( $this, 'test_loopback_requests' ),
     69                                'permission_callback' => function() {
     70                                        return $this->validate_request_permission( 'test-dotorg-communication' );
     71                                },
     72                        )
     73                );
     74
     75                register_rest_route(
     76                        $this->namespace,
     77                        sprintf(
     78                                '/%s/%s',
     79                                $this->rest_base,
     80                                'test-dotorg-communication'
     81                        ),
     82                        array(
     83                                'methods'             => 'POST',
     84                                'callback'            => array( $this, 'test_dotorg_communication' ),
     85                                'permission_callback' => function() {
     86                                        return $this->validate_request_permission( 'test-dotorg-communication' );
     87                                },
     88                        )
     89                );
     90
     91                register_rest_route(
     92                        $this->namespace,
     93                        sprintf(
     94                                '/%s/%s',
     95                                $this->rest_base,
     96                                'test-is-in-debug-mode'
     97                        ),
     98                        array(
     99                                'methods'             => 'POST',
     100                                'callback'            => array( $this, 'test_is_in_debug_mode' ),
     101                                'permission_callback' => function() {
     102                                        return $this->validate_request_permission( 'test-is-in-debug-mode' );
     103                                },
     104                        )
     105                );
     106
     107                register_rest_route(
     108                        $this->namespace,
     109                        sprintf(
     110                                '/%s/%s',
     111                                $this->rest_base,
     112                                'get-directory-sizes'
     113                        ),
     114                        array(
     115                                'methods'             => 'POST',
     116                                'callback'            => array( $this, 'get_directory_sizes' ),
     117                                'permission_callback' => function() {
     118                                        return $this->validate_request_permission( 'test-is-in-debug-mode' ) && ! is_multisite();
     119                                },
     120                        )
     121                );
     122        }
     123
     124        public function validate_request_permission( $check ) {
     125                $capability = apply_filters( "site_health_test_rest_capability_{$check}", 'view_site_health_checks', $check );
     126
     127                return current_user_can( $capability );
     128        }
     129
     130        /**
     131         * Check if background updates work as expected.
     132         *
     133         * @since 5.4.0
     134         *
     135         * @return array
     136         */
     137        public function test_background_updates() {
     138                return array(
     139                        'data' => $this->site_health->get_test_background_updates()
     140                );
     141        }
     142
     143        /**
     144         * Check that the site can reach the WordPress.org API.
     145         *
     146         * @since 5.4.0
     147         *
     148         * @return array
     149         */
     150        public function test_dotorg_communication() {
     151                return array(
     152                        'data' => $this->site_health->get_test_dotorg_communication()
     153                );
     154        }
     155
     156        /**
     157         * Check that loopbacks can be performed.
     158         *
     159         * @since 5.4.0
     160         *
     161         * @return array
     162         */
     163        public function test_loopback_requests() {
     164                return array(
     165                        'data' => $this->site_health->get_test_loopback_requests()
     166                );
     167        }
     168
     169        /**
     170         * Check if the site has debug mode enabled.
     171         *
     172         * @since 5.4.0
     173         *
     174         * @return array
     175         */
     176        public function test_is_in_debug_mode() {
     177                return array(
     178                        'data' => $this->site_health->get_test_is_in_debug_mode()
     179                );
     180        }
     181
     182        /**
     183         * Get the current directory sizes for this install.
     184         *
     185         * @since 5.4.0
     186         */
     187        public function get_directory_sizes() {
     188                if ( ! class_exists( 'WP_Debug_Data' ) ) {
     189                        require_once( ABSPATH . 'wp-admin/includes/class-wp-debug-data.php' );
     190                }
     191
     192                $sizes_data = WP_Debug_Data::get_sizes();
     193                $all_sizes  = array( 'raw' => 0 );
     194
     195                foreach ( $sizes_data as $name => $value ) {
     196                        $name = sanitize_text_field( $name );
     197                        $data = array();
     198
     199                        if ( isset( $value['size'] ) ) {
     200                                if ( is_string( $value['size'] ) ) {
     201                                        $data['size'] = sanitize_text_field( $value['size'] );
     202                                } else {
     203                                        $data['size'] = (int) $value['size'];
     204                                }
     205                        }
     206
     207                        if ( isset( $value['debug'] ) ) {
     208                                if ( is_string( $value['debug'] ) ) {
     209                                        $data['debug'] = sanitize_text_field( $value['debug'] );
     210                                } else {
     211                                        $data['debug'] = (int) $value['debug'];
     212                                }
     213                        }
     214
     215                        if ( ! empty( $value['raw'] ) ) {
     216                                $data['raw'] = (int) $value['raw'];
     217                        }
     218
     219                        $all_sizes[ $name ] = $data;
     220                }
     221
     222                if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
     223                        return new WP_Error( 'not_available', 'not_available', $all_sizes );
     224                }
     225
     226                return $all_sizes;
     227        }
     228}
  • src/wp-settings.php

     
    248248require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-renderer-controller.php' );
    249249require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-settings-controller.php' );
    250250require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php' );
     251require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-site-health-controller.php' );
    251252require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-meta-fields.php' );
    252253require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-comment-meta-fields.php' );
    253254require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-post-meta-fields.php' );
  • src/wp-admin/site-health.php

     
    2727        require_once( ABSPATH . 'wp-admin/includes/class-wp-site-health.php' );
    2828}
    2929
    30 $health_check_site_status = new WP_Site_Health();
    31 
    32 // Start by checking if this is a special request checking for the existence of certain filters.
    33 $health_check_site_status->check_wp_version_check_exists();
     30new WP_Site_Health();
    3431
    3532require_once( ABSPATH . 'wp-admin/admin-header.php' );
    3633?>
  • src/wp-admin/includes/class-wp-site-health-auto-updates.php

     
    8585         * @return array The test results.
    8686         */
    8787        public function test_wp_version_check_attached() {
    88                 if ( ! is_main_site() ) {
    89                         return;
    90                 }
    91 
    92                 $cookies = wp_unslash( $_COOKIE );
    93                 $timeout = 10;
    94                 $headers = array(
    95                         'Cache-Control' => 'no-cache',
    96                 );
    97                 /** This filter is documented in wp-includes/class-wp-http-streams.php */
    98                 $sslverify = apply_filters( 'https_local_ssl_verify', false );
    99 
    100                 // Include Basic auth in loopback requests.
    101                 if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
    102                         $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
    103                 }
    104 
    105                 $url = add_query_arg(
    106                         array(
    107                                 'health-check-test-wp_version_check' => true,
    108                         ),
    109                         admin_url( 'site-health.php' )
    110                 );
    111 
    112                 $test = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
    113 
    114                 if ( is_wp_error( $test ) ) {
    115                         return array(
    116                                 'description' => sprintf(
    117                                         /* translators: %s: Name of the filter used. */
    118                                         __( 'Could not confirm that the %s filter is available.' ),
    119                                         '<code>wp_version_check()</code>'
    120                                 ),
    121                                 'severity'    => 'warning',
    122                         );
    123                 }
    124 
    125                 $response = wp_remote_retrieve_body( $test );
    126 
    127                 if ( 'yes' !== $response ) {
     88                if ( ! has_filter( 'wp_version_check', 'wp_version_check' ) ) {
    12889                        return array(
    12990                                'description' => sprintf(
    13091                                        /* translators: %s: Name of the filter used. */
     
    305266         * @return array The test results.
    306267         */
    307268        function test_check_wp_filesystem_method() {
     269                // Make sure the `request_filesystem_credentials` function is available during our REST call.
     270                if ( ! function_exists( 'request_filesystem_credentials' ) ) {
     271                        require_once( ABSPATH . '/wp-admin/includes/file.php' );
     272                }
     273
    308274                $skin    = new Automatic_Upgrader_Skin;
    309275                $success = $skin->request_filesystem_credentials( false, ABSPATH );
    310276
     
    351317                        return false;
    352318                }
    353319
     320                // Make sure the `get_core_checksums` function is available during our REST call.
     321                if ( ! function_exists( 'get_core_checksums' ) ) {
     322                        require_once( ABSPATH . '/wp-admin/includes/update.php' );
     323                }
     324
    354325                $checksums = get_core_checksums( $wp_version, 'en_US' );
    355326                $dev       = ( false !== strpos( $wp_version, '-' ) );
    356327                // Get the last stable version's files and test against that
  • src/wp-admin/admin-ajax.php

     
    134134        'wp-privacy-export-personal-data',
    135135        'wp-privacy-erase-personal-data',
    136136        'health-check-site-status-result',
    137         'health-check-dotorg-communication',
    138         'health-check-is-in-debug-mode',
    139         'health-check-background-updates',
    140         'health-check-loopback-requests',
    141         'health-check-get-sizes',
    142137);
    143138
    144139// Deprecated
  • src/wp-admin/includes/class-wp-site-health.php

     
    5757
    5858                $health_check_js_variables = array(
    5959                        'screen'      => $screen->id,
     60                        'rest_base'   => rest_url( '/' ),
    6061                        'nonce'       => array(
     62                                'rest_api'           => wp_create_nonce( 'wp_rest' ),
    6163                                'site_status'        => wp_create_nonce( 'health-check-site-status' ),
    6264                                'site_status_result' => wp_create_nonce( 'health-check-site-status-result' ),
    6365                        ),
     
    132134                                if ( is_string( $test['test'] ) ) {
    133135                                        $health_check_js_variables['site_status']['async'][] = array(
    134136                                                'test'      => $test['test'],
     137                                                'has_rest'  => ( isset( $test['has_rest'] ) ? $test['has_rest'] : false ),
    135138                                                'completed' => false,
    136139                                        );
    137140                                }
     
    19371940                        ),
    19381941                        'async'  => array(
    19391942                                'dotorg_communication' => array(
    1940                                         'label' => __( 'Communication with WordPress.org' ),
    1941                                         'test'  => 'dotorg_communication',
     1943                                        'label'    => __( 'Communication with WordPress.org' ),
     1944                                        'test'     => 'wp/v2/site-health/test-dotorg-communication',
     1945                                        'has_rest' => true,
    19421946                                ),
    19431947                                'background_updates'   => array(
    1944                                         'label' => __( 'Background updates' ),
    1945                                         'test'  => 'background_updates',
     1948                                        'label'    => __( 'Background updates' ),
     1949                                        'test'     => 'wp/v2/site-health/test-background-updates',
     1950                                        'has_rest' => true,
    19461951                                ),
    19471952                                'loopback_requests'    => array(
    1948                                         'label' => __( 'Loopback request' ),
    1949                                         'test'  => 'loopback_requests',
     1953                                        'label'    => __( 'Loopback request' ),
     1954                                        'test'     => 'wp/v2/site-health/test-loopback-requests',
     1955                                        'has_rest' => true,
    19501956                                ),
    19511957                        ),
    19521958                );
     
    20052011         * @return string The modified body class string.
    20062012         */
    20072013        public function admin_body_class( $body_class ) {
     2014                $screen = get_current_screen();
     2015                if ( 'site-health' !== $screen->id ) {
     2016                        return $body_class;
     2017                }
     2018
    20082019                $body_class .= ' site-health';
    20092020
    20102021                return $body_class;
  • src/wp-includes/rest-api.php

     
    272272        $controller = new WP_REST_Themes_Controller;
    273273        $controller->register_routes();
    274274
     275        // Site Health
     276        $controller = new WP_REST_Site_Health_Controller;
     277        $controller->register_routes();
     278
    275279}
    276280
    277281/**