[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-site-health-controller.php (source)

   1  <?php
   2  /**
   3   * REST API: WP_REST_Site_Health_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 5.6.0
   8   */
   9  
  10  /**
  11   * Core class for interacting with Site Health tests.
  12   *
  13   * @since 5.6.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Site_Health_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * An instance of the site health class.
  21       *
  22       * @since 5.6.0
  23       *
  24       * @var WP_Site_Health
  25       */
  26      private $site_health;
  27  
  28      /**
  29       * Site Health controller constructor.
  30       *
  31       * @since 5.6.0
  32       *
  33       * @param WP_Site_Health $site_health An instance of the site health class.
  34       */
  35  	public function __construct( $site_health ) {
  36          $this->namespace = 'wp-site-health/v1';
  37          $this->rest_base = 'tests';
  38  
  39          $this->site_health = $site_health;
  40      }
  41  
  42      /**
  43       * Registers API routes.
  44       *
  45       * @since 5.6.0
  46       *
  47       * @see register_rest_route()
  48       */
  49  	public function register_routes() {
  50          register_rest_route(
  51              $this->namespace,
  52              sprintf(
  53                  '/%s/%s',
  54                  $this->rest_base,
  55                  'background-updates'
  56              ),
  57              array(
  58                  array(
  59                      'methods'             => 'GET',
  60                      'callback'            => array( $this, 'test_background_updates' ),
  61                      'permission_callback' => function () {
  62                          return $this->validate_request_permission( 'background_updates' );
  63                      },
  64                  ),
  65                  'schema' => array( $this, 'get_public_item_schema' ),
  66              )
  67          );
  68  
  69          register_rest_route(
  70              $this->namespace,
  71              sprintf(
  72                  '/%s/%s',
  73                  $this->rest_base,
  74                  'loopback-requests'
  75              ),
  76              array(
  77                  array(
  78                      'methods'             => 'GET',
  79                      'callback'            => array( $this, 'test_loopback_requests' ),
  80                      'permission_callback' => function () {
  81                          return $this->validate_request_permission( 'loopback_requests' );
  82                      },
  83                  ),
  84                  'schema' => array( $this, 'get_public_item_schema' ),
  85              )
  86          );
  87  
  88          register_rest_route(
  89              $this->namespace,
  90              sprintf(
  91                  '/%s/%s',
  92                  $this->rest_base,
  93                  'dotorg-communication'
  94              ),
  95              array(
  96                  array(
  97                      'methods'             => 'GET',
  98                      'callback'            => array( $this, 'test_dotorg_communication' ),
  99                      'permission_callback' => function () {
 100                          return $this->validate_request_permission( 'dotorg_communication' );
 101                      },
 102                  ),
 103                  'schema' => array( $this, 'get_public_item_schema' ),
 104              )
 105          );
 106  
 107          register_rest_route(
 108              $this->namespace,
 109              sprintf(
 110                  '/%s/%s',
 111                  $this->rest_base,
 112                  'authorization-header'
 113              ),
 114              array(
 115                  array(
 116                      'methods'             => 'GET',
 117                      'callback'            => array( $this, 'test_authorization_header' ),
 118                      'permission_callback' => function () {
 119                          return $this->validate_request_permission( 'authorization_header' );
 120                      },
 121                  ),
 122                  'schema' => array( $this, 'get_public_item_schema' ),
 123              )
 124          );
 125  
 126          register_rest_route(
 127              $this->namespace,
 128              sprintf(
 129                  '/%s',
 130                  'directory-sizes'
 131              ),
 132              array(
 133                  'methods'             => 'GET',
 134                  'callback'            => array( $this, 'get_directory_sizes' ),
 135                  'permission_callback' => function() {
 136                      return $this->validate_request_permission( 'debug_enabled' ) && ! is_multisite();
 137                  },
 138              )
 139          );
 140      }
 141  
 142      /**
 143       * Validates if the current user can request this REST endpoint.
 144       *
 145       * @since 5.6.0
 146       *
 147       * @param string $check The endpoint check being ran.
 148       * @return bool
 149       */
 150  	protected function validate_request_permission( $check ) {
 151          $default_capability = 'view_site_health_checks';
 152  
 153          /**
 154           * Filters the capability needed to run a given Site Health check.
 155           *
 156           * @since 5.6.0
 157           *
 158           * @param string $default_capability The default capability required for this check.
 159           * @param string $check              The Site Health check being performed.
 160           */
 161          $capability = apply_filters( "site_health_test_rest_capability_{$check}", $default_capability, $check );
 162  
 163          return current_user_can( $capability );
 164      }
 165  
 166      /**
 167       * Checks if background updates work as expected.
 168       *
 169       * @since 5.6.0
 170       *
 171       * @return array
 172       */
 173  	public function test_background_updates() {
 174          $this->load_admin_textdomain();
 175          return $this->site_health->get_test_background_updates();
 176      }
 177  
 178      /**
 179       * Checks that the site can reach the WordPress.org API.
 180       *
 181       * @since 5.6.0
 182       *
 183       * @return array
 184       */
 185  	public function test_dotorg_communication() {
 186          $this->load_admin_textdomain();
 187          return $this->site_health->get_test_dotorg_communication();
 188      }
 189  
 190      /**
 191       * Checks that loopbacks can be performed.
 192       *
 193       * @since 5.6.0
 194       *
 195       * @return array
 196       */
 197  	public function test_loopback_requests() {
 198          $this->load_admin_textdomain();
 199          return $this->site_health->get_test_loopback_requests();
 200      }
 201  
 202      /**
 203       * Checks that the authorization header is valid.
 204       *
 205       * @since 5.6.0
 206       *
 207       * @return array
 208       */
 209  	public function test_authorization_header() {
 210          $this->load_admin_textdomain();
 211          return $this->site_health->get_test_authorization_header();
 212      }
 213  
 214      /**
 215       * Gets the current directory sizes for this install.
 216       *
 217       * @since 5.6.0
 218       *
 219       * @return array|WP_Error
 220       */
 221  	public function get_directory_sizes() {
 222          if ( ! class_exists( 'WP_Debug_Data' ) ) {
 223              require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
 224          }
 225  
 226          $this->load_admin_textdomain();
 227  
 228          $sizes_data = WP_Debug_Data::get_sizes();
 229          $all_sizes  = array( 'raw' => 0 );
 230  
 231          foreach ( $sizes_data as $name => $value ) {
 232              $name = sanitize_text_field( $name );
 233              $data = array();
 234  
 235              if ( isset( $value['size'] ) ) {
 236                  if ( is_string( $value['size'] ) ) {
 237                      $data['size'] = sanitize_text_field( $value['size'] );
 238                  } else {
 239                      $data['size'] = (int) $value['size'];
 240                  }
 241              }
 242  
 243              if ( isset( $value['debug'] ) ) {
 244                  if ( is_string( $value['debug'] ) ) {
 245                      $data['debug'] = sanitize_text_field( $value['debug'] );
 246                  } else {
 247                      $data['debug'] = (int) $value['debug'];
 248                  }
 249              }
 250  
 251              if ( ! empty( $value['raw'] ) ) {
 252                  $data['raw'] = (int) $value['raw'];
 253              }
 254  
 255              $all_sizes[ $name ] = $data;
 256          }
 257  
 258          if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
 259              return new WP_Error( 'not_available', __( 'Directory sizes could not be returned.' ), array( 'status' => 500 ) );
 260          }
 261  
 262          return $all_sizes;
 263      }
 264  
 265      /**
 266       * Loads the admin textdomain for Site Health tests.
 267       *
 268       * The {@see WP_Site_Health} class is defined in WP-Admin, while the REST API operates in a front-end context.
 269       * This means that the translations for Site Health won't be loaded by default in {@see load_default_textdomain()}.
 270       *
 271       * @since 5.6.0
 272       */
 273  	protected function load_admin_textdomain() {
 274          // Accounts for inner REST API requests in the admin.
 275          if ( ! is_admin() ) {
 276              $locale = determine_locale();
 277              load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
 278          }
 279      }
 280  
 281      /**
 282       * Gets the schema for each site health test.
 283       *
 284       * @since 5.6.0
 285       *
 286       * @return array The test schema.
 287       */
 288  	public function get_item_schema() {
 289          if ( $this->schema ) {
 290              return $this->schema;
 291          }
 292  
 293          $this->schema = array(
 294              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 295              'title'      => 'wp-site-health-test',
 296              'type'       => 'object',
 297              'properties' => array(
 298                  'test'        => array(
 299                      'type'        => 'string',
 300                      'description' => __( 'The name of the test being run.' ),
 301                      'readonly'    => true,
 302                  ),
 303                  'label'       => array(
 304                      'type'        => 'string',
 305                      'description' => __( 'A label describing the test.' ),
 306                      'readonly'    => true,
 307                  ),
 308                  'status'      => array(
 309                      'type'        => 'string',
 310                      'description' => __( 'The status of the test.' ),
 311                      'enum'        => array( 'good', 'recommended', 'critical' ),
 312                      'readonly'    => true,
 313                  ),
 314                  'badge'       => array(
 315                      'type'        => 'object',
 316                      'description' => __( 'The category this test is grouped in.' ),
 317                      'properties'  => array(
 318                          'label' => array(
 319                              'type'     => 'string',
 320                              'readonly' => true,
 321                          ),
 322                          'color' => array(
 323                              'type'     => 'string',
 324                              'enum'     => array( 'blue', 'orange', 'red', 'green', 'purple', 'gray' ),
 325                              'readonly' => true,
 326                          ),
 327                      ),
 328                      'readonly'    => true,
 329                  ),
 330                  'description' => array(
 331                      'type'        => 'string',
 332                      'description' => __( 'A more descriptive explanation of what the test looks for, and why it is important for the user.' ),
 333                      'readonly'    => true,
 334                  ),
 335                  'actions'     => array(
 336                      'type'        => 'string',
 337                      'description' => __( 'HTML containing an action to direct the user to where they can resolve the issue.' ),
 338                      'readonly'    => true,
 339                  ),
 340              ),
 341          );
 342  
 343          return $this->schema;
 344      }
 345  }


Generated: Wed Jan 27 01:00:08 2021 Cross-referenced by PHPXref 0.7.1