Make WordPress Core

Ticket #48105: 48105.patch

File 48105.patch, 12.5 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;
     166                                var active_test = this.test;
    165167                                var data = {
    166                                         'action': 'health-check-' + this.test.replace( '_', '-' ),
     168                                        'action': 'health-check-' + active_test.replace( '_', '-' ),
    167169                                        '_wpnonce': SiteHealth.nonce.site_status
    168170                                };
    169171
     172                                if ( "undefined" !== typeof( this.has_rest ) && this.has_rest ) {
     173                                        url = SiteHealth.rest_base + this.test;
     174                                        data = {
     175                                                '_wpnonce': SiteHealth.nonce.rest_api
     176                                        };
     177                                }
     178
    170179                                if ( this.completed ) {
    171180                                        return true;
    172181                                }
     
    176185                                this.completed = true;
    177186
    178187                                $.post(
    179                                         ajaxurl,
    180                                         data,
    181                                         function( response ) {
    182                                                 AppendIssue( response.data );
    183                                                 maybeRunNextAsyncTest();
    184                                         }
    185                                 );
     188                                        url,
     189                                        data
     190                                ).done( function( response ) {
     191                                        AppendIssue( response.data );
     192                                } ).fail( function( response ) {
     193                                        var description = __( 'No details available' );
     194
     195                                        if ( "undefined" !== typeof( response.responseJSON ) && "undefined" !== typeof( response.responseJSON.message ) ) {
     196                                                description = response.responseJSON.message;
     197                                        }
     198
     199                                        var issue = {
     200                                                'status': 'recommended',
     201                                                'label': sprintf( __( 'Unavailable test: %s' ), active_test ),
     202                                                'test': active_test.replace( /\//g, '-' ),
     203                                                'badge': {
     204                                                        'color': 'blue',
     205                                                        'label': __( 'Failed test' )
     206                                                },
     207                                                'description': '<p>' + description + '</p>',
     208                                                'actions': ''
     209                                        };
     210
     211                                        AppendIssue( issue );
     212                                } ).always(function() {
     213                                        maybeRunNextAsyncTest();
     214                                });
    186215
    187216                                return false;
    188217                        } );
     
    211240                }
    212241
    213242                if ( 0 < SiteHealth.site_status.async.length ) {
    214                         data = {
    215                                 'action': 'health-check-' + SiteHealth.site_status.async[0].test.replace( '_', '-' ),
    216                                 '_wpnonce': SiteHealth.nonce.site_status
    217                         };
    218 
    219                         SiteHealth.site_status.async[0].completed = true;
    220 
    221                         $.post(
    222                                 ajaxurl,
    223                                 data,
    224                                 function( response ) {
    225                                         AppendIssue( response.data );
    226                                         maybeRunNextAsyncTest();
    227                                 }
    228                         );
     243                        maybeRunNextAsyncTest();
    229244                } else {
    230245                        RecalculateProgression();
    231246                }
  • 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

     
    8686         * @return array The test results.
    8787         */
    8888        public function test_wp_version_check_attached() {
    89                 if ( ! is_main_site() ) {
    90                         return;
    91                 }
    92 
    93                 $cookies = wp_unslash( $_COOKIE );
    94                 $timeout = 10;
    95                 $headers = array(
    96                         'Cache-Control' => 'no-cache',
    97                 );
    98                 /** This filter is documented in wp-includes/class-wp-http-streams.php */
    99                 $sslverify = apply_filters( 'https_local_ssl_verify', false );
    100 
    101                 // Include Basic auth in loopback requests.
    102                 if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
    103                         $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
    104                 }
    105 
    106                 $url = add_query_arg(
    107                         array(
    108                                 'health-check-test-wp_version_check' => true,
    109                         ),
    110                         admin_url( 'site-health.php' )
    111                 );
    112 
    113                 $test = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
    114 
    115                 if ( is_wp_error( $test ) ) {
    116                         return array(
    117                                 'description' => sprintf(
    118                                         /* translators: %s: Name of the filter used. */
    119                                         __( 'Could not confirm that the %s filter is available.' ),
    120                                         '<code>wp_version_check()</code>'
    121                                 ),
    122                                 'severity'    => 'warning',
    123                         );
    124                 }
    125 
    126                 $response = wp_remote_retrieve_body( $test );
    127 
    128                 if ( 'yes' !== $response ) {
     89                if ( ! has_filter( 'wp_version_check', 'wp_version_check' ) ) {
    12990                        return array(
    13091                                'description' => sprintf(
    13192                                        /* translators: %s: Name of the filter used. */
     
    282243         * @return array The test results.
    283244         */
    284245        function test_check_wp_filesystem_method() {
     246                // Make sure the `request_filesystem_credentials` function is available during our REST call.
     247                if ( ! function_exists( 'request_filesystem_credentials' ) ) {
     248                        require_once( ABSPATH . '/wp-admin/includes/file.php' );
     249                }
     250
    285251                $skin    = new Automatic_Upgrader_Skin;
    286252                $success = $skin->request_filesystem_credentials( false, ABSPATH );
    287253
     
    328294                        return false;
    329295                }
    330296
     297                // Make sure the `get_core_checksums` function is available during our REST call.
     298                if ( ! function_exists( 'get_core_checksums' ) ) {
     299                        require_once( ABSPATH . '/wp-admin/includes/update.php' );
     300                }
     301
    331302                $checksums = get_core_checksums( $wp_version, 'en_US' );
    332303                $dev       = ( false !== strpos( $wp_version, '-' ) );
    333304                // Get the last stable version's files and test against that
  • 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                        ),
     
    110112                                if ( is_string( $test['test'] ) ) {
    111113                                        $health_check_js_variables['site_status']['async'][] = array(
    112114                                                'test'      => $test['test'],
     115                                                'has_rest'  => ( isset( $test['has_rest'] ) ? $test['has_rest'] : false ),
    113116                                                'completed' => false,
    114117                                        );
    115118                                }
     
    18161819                        ),
    18171820                        'async'  => array(
    18181821                                'dotorg_communication' => array(
    1819                                         'label' => __( 'Communication with WordPress.org' ),
    1820                                         'test'  => 'dotorg_communication',
     1822                                        'label'    => __( 'Communication with WordPress.org' ),
     1823                                        'test'     => 'wp/v2/site-health/test-dotorg-communication',
     1824                                        'has_rest' => true,
    18211825                                ),
    18221826                                'background_updates'   => array(
    1823                                         'label' => __( 'Background updates' ),
    1824                                         'test'  => 'background_updates',
     1827                                        'label'    => __( 'Background updates' ),
     1828                                        'test'     => 'wp/v2/site-health/test-background-updates',
     1829                                        'has_rest' => true,
    18251830                                ),
    18261831                                'loopback_requests'    => array(
    1827                                         'label' => __( 'Loopback request' ),
    1828                                         'test'  => 'loopback_requests',
     1832                                        'label'    => __( 'Loopback request' ),
     1833                                        'test'     => 'wp/v2/site-health/test-loopback-requests',
     1834                                        'has_rest' => true,
    18291835                                ),
    18301836                        ),
    18311837                );
     
    18841890         * @return string The modified body class string.
    18851891         */
    18861892        public function admin_body_class( $body_class ) {
     1893                $screen = get_current_screen();
     1894                if ( 'site-health' !== $screen->id ) {
     1895                        return $body_class;
     1896                }
     1897
    18871898                $body_class .= ' site-health';
    18881899
    18891900                return $body_class;
  • 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.3.0
     8 */
     9
     10/**
     11 * Core class for interacting with Site Health elements.
     12 *
     13 * @since 5.3.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.3.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
     92        public function validate_request_permission( $check ) {
     93                $capability = apply_filters( "site_health_test_rest_capability_{$check}", 'view_site_health_checks', $check );
     94
     95                return current_user_can( $capability );
     96        }
     97
     98        /**
     99         * Check if background updates work as expected.
     100         *
     101         * @since 5.3.0
     102         *
     103         * @return array
     104         */
     105        public function test_background_updates() {
     106                return array(
     107                        'data' => $this->site_health->get_test_background_updates()
     108                );
     109        }
     110
     111        /**
     112         * Check that the site can reach the WordPress.org API.
     113         *
     114         * @since 5.3.0
     115         *
     116         * @return array
     117         */
     118        public function test_dotorg_communication() {
     119                return array(
     120                        'data' => $this->site_health->get_test_dotorg_communication()
     121                );
     122        }
     123
     124        /**
     125         * Check that loopbacks can be performed.
     126         *
     127         * @since 5.3.0
     128         *
     129         * @return array
     130         */
     131        public function test_loopback_requests() {
     132                return array(
     133                        'data' => $this->site_health->get_test_loopback_requests()
     134                );
     135        }
     136}
  • src/wp-includes/rest-api.php

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