[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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       * @since 6.1.0 Adds page-cache async test.
  47       *
  48       * @see register_rest_route()
  49       */
  50  	public function register_routes() {
  51          register_rest_route(
  52              $this->namespace,
  53              sprintf(
  54                  '/%s/%s',
  55                  $this->rest_base,
  56                  'background-updates'
  57              ),
  58              array(
  59                  array(
  60                      'methods'             => 'GET',
  61                      'callback'            => array( $this, 'test_background_updates' ),
  62                      'permission_callback' => function () {
  63                          return $this->validate_request_permission( 'background_updates' );
  64                      },
  65                  ),
  66                  'schema' => array( $this, 'get_public_item_schema' ),
  67              )
  68          );
  69  
  70          register_rest_route(
  71              $this->namespace,
  72              sprintf(
  73                  '/%s/%s',
  74                  $this->rest_base,
  75                  'loopback-requests'
  76              ),
  77              array(
  78                  array(
  79                      'methods'             => 'GET',
  80                      'callback'            => array( $this, 'test_loopback_requests' ),
  81                      'permission_callback' => function () {
  82                          return $this->validate_request_permission( 'loopback_requests' );
  83                      },
  84                  ),
  85                  'schema' => array( $this, 'get_public_item_schema' ),
  86              )
  87          );
  88  
  89          register_rest_route(
  90              $this->namespace,
  91              sprintf(
  92                  '/%s/%s',
  93                  $this->rest_base,
  94                  'https-status'
  95              ),
  96              array(
  97                  array(
  98                      'methods'             => 'GET',
  99                      'callback'            => array( $this, 'test_https_status' ),
 100                      'permission_callback' => function () {
 101                          return $this->validate_request_permission( 'https_status' );
 102                      },
 103                  ),
 104                  'schema' => array( $this, 'get_public_item_schema' ),
 105              )
 106          );
 107  
 108          register_rest_route(
 109              $this->namespace,
 110              sprintf(
 111                  '/%s/%s',
 112                  $this->rest_base,
 113                  'dotorg-communication'
 114              ),
 115              array(
 116                  array(
 117                      'methods'             => 'GET',
 118                      'callback'            => array( $this, 'test_dotorg_communication' ),
 119                      'permission_callback' => function () {
 120                          return $this->validate_request_permission( 'dotorg_communication' );
 121                      },
 122                  ),
 123                  'schema' => array( $this, 'get_public_item_schema' ),
 124              )
 125          );
 126  
 127          register_rest_route(
 128              $this->namespace,
 129              sprintf(
 130                  '/%s/%s',
 131                  $this->rest_base,
 132                  'authorization-header'
 133              ),
 134              array(
 135                  array(
 136                      'methods'             => 'GET',
 137                      'callback'            => array( $this, 'test_authorization_header' ),
 138                      'permission_callback' => function () {
 139                          return $this->validate_request_permission( 'authorization_header' );
 140                      },
 141                  ),
 142                  'schema' => array( $this, 'get_public_item_schema' ),
 143              )
 144          );
 145  
 146          register_rest_route(
 147              $this->namespace,
 148              sprintf(
 149                  '/%s',
 150                  'directory-sizes'
 151              ),
 152              array(
 153                  'methods'             => 'GET',
 154                  'callback'            => array( $this, 'get_directory_sizes' ),
 155                  'permission_callback' => function () {
 156                      return $this->validate_request_permission( 'directory_sizes' ) && ! is_multisite();
 157                  },
 158              )
 159          );
 160  
 161          register_rest_route(
 162              $this->namespace,
 163              sprintf(
 164                  '/%s/%s',
 165                  $this->rest_base,
 166                  'page-cache'
 167              ),
 168              array(
 169                  array(
 170                      'methods'             => 'GET',
 171                      'callback'            => array( $this, 'test_page_cache' ),
 172                      'permission_callback' => function () {
 173                          return $this->validate_request_permission( 'page_cache' );
 174                      },
 175                  ),
 176              )
 177          );
 178      }
 179  
 180      /**
 181       * Validates if the current user can request this REST endpoint.
 182       *
 183       * @since 5.6.0
 184       *
 185       * @param string $check The endpoint check being ran.
 186       * @return bool
 187       */
 188  	protected function validate_request_permission( $check ) {
 189          $default_capability = 'view_site_health_checks';
 190  
 191          /**
 192           * Filters the capability needed to run a given Site Health check.
 193           *
 194           * @since 5.6.0
 195           *
 196           * @param string $default_capability The default capability required for this check.
 197           * @param string $check              The Site Health check being performed.
 198           */
 199          $capability = apply_filters( "site_health_test_rest_capability_{$check}", $default_capability, $check );
 200  
 201          return current_user_can( $capability );
 202      }
 203  
 204      /**
 205       * Checks if background updates work as expected.
 206       *
 207       * @since 5.6.0
 208       *
 209       * @return array
 210       */
 211  	public function test_background_updates() {
 212          $this->load_admin_textdomain();
 213          return $this->site_health->get_test_background_updates();
 214      }
 215  
 216      /**
 217       * Checks that the site can reach the WordPress.org API.
 218       *
 219       * @since 5.6.0
 220       *
 221       * @return array
 222       */
 223  	public function test_dotorg_communication() {
 224          $this->load_admin_textdomain();
 225          return $this->site_health->get_test_dotorg_communication();
 226      }
 227  
 228      /**
 229       * Checks that loopbacks can be performed.
 230       *
 231       * @since 5.6.0
 232       *
 233       * @return array
 234       */
 235  	public function test_loopback_requests() {
 236          $this->load_admin_textdomain();
 237          return $this->site_health->get_test_loopback_requests();
 238      }
 239  
 240      /**
 241       * Checks that the site's frontend can be accessed over HTTPS.
 242       *
 243       * @since 5.7.0
 244       *
 245       * @return array
 246       */
 247  	public function test_https_status() {
 248          $this->load_admin_textdomain();
 249          return $this->site_health->get_test_https_status();
 250      }
 251  
 252      /**
 253       * Checks that the authorization header is valid.
 254       *
 255       * @since 5.6.0
 256       *
 257       * @return array
 258       */
 259  	public function test_authorization_header() {
 260          $this->load_admin_textdomain();
 261          return $this->site_health->get_test_authorization_header();
 262      }
 263  
 264      /**
 265       * Checks that full page cache is active.
 266       *
 267       * @since 6.1.0
 268       *
 269       * @return array The test result.
 270       */
 271  	public function test_page_cache() {
 272          $this->load_admin_textdomain();
 273          return $this->site_health->get_test_page_cache();
 274      }
 275  
 276      /**
 277       * Gets the current directory sizes for this install.
 278       *
 279       * @since 5.6.0
 280       *
 281       * @return array|WP_Error
 282       */
 283  	public function get_directory_sizes() {
 284          if ( ! class_exists( 'WP_Debug_Data' ) ) {
 285              require_once  ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
 286          }
 287  
 288          $this->load_admin_textdomain();
 289  
 290          $sizes_data = WP_Debug_Data::get_sizes();
 291          $all_sizes  = array( 'raw' => 0 );
 292  
 293          foreach ( $sizes_data as $name => $value ) {
 294              $name = sanitize_text_field( $name );
 295              $data = array();
 296  
 297              if ( isset( $value['size'] ) ) {
 298                  if ( is_string( $value['size'] ) ) {
 299                      $data['size'] = sanitize_text_field( $value['size'] );
 300                  } else {
 301                      $data['size'] = (int) $value['size'];
 302                  }
 303              }
 304  
 305              if ( isset( $value['debug'] ) ) {
 306                  if ( is_string( $value['debug'] ) ) {
 307                      $data['debug'] = sanitize_text_field( $value['debug'] );
 308                  } else {
 309                      $data['debug'] = (int) $value['debug'];
 310                  }
 311              }
 312  
 313              if ( ! empty( $value['raw'] ) ) {
 314                  $data['raw'] = (int) $value['raw'];
 315              }
 316  
 317              $all_sizes[ $name ] = $data;
 318          }
 319  
 320          if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
 321              return new WP_Error( 'not_available', __( 'Directory sizes could not be returned.' ), array( 'status' => 500 ) );
 322          }
 323  
 324          return $all_sizes;
 325      }
 326  
 327      /**
 328       * Loads the admin textdomain for Site Health tests.
 329       *
 330       * The {@see WP_Site_Health} class is defined in WP-Admin, while the REST API operates in a front-end context.
 331       * This means that the translations for Site Health won't be loaded by default in {@see load_default_textdomain()}.
 332       *
 333       * @since 5.6.0
 334       */
 335  	protected function load_admin_textdomain() {
 336          // Accounts for inner REST API requests in the admin.
 337          if ( ! is_admin() ) {
 338              $locale = determine_locale();
 339              load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo", $locale );
 340          }
 341      }
 342  
 343      /**
 344       * Gets the schema for each site health test.
 345       *
 346       * @since 5.6.0
 347       *
 348       * @return array The test schema.
 349       */
 350  	public function get_item_schema() {
 351          if ( $this->schema ) {
 352              return $this->schema;
 353          }
 354  
 355          $this->schema = array(
 356              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 357              'title'      => 'wp-site-health-test',
 358              'type'       => 'object',
 359              'properties' => array(
 360                  'test'        => array(
 361                      'type'        => 'string',
 362                      'description' => __( 'The name of the test being run.' ),
 363                      'readonly'    => true,
 364                  ),
 365                  'label'       => array(
 366                      'type'        => 'string',
 367                      'description' => __( 'A label describing the test.' ),
 368                      'readonly'    => true,
 369                  ),
 370                  'status'      => array(
 371                      'type'        => 'string',
 372                      'description' => __( 'The status of the test.' ),
 373                      'enum'        => array( 'good', 'recommended', 'critical' ),
 374                      'readonly'    => true,
 375                  ),
 376                  'badge'       => array(
 377                      'type'        => 'object',
 378                      'description' => __( 'The category this test is grouped in.' ),
 379                      'properties'  => array(
 380                          'label' => array(
 381                              'type'     => 'string',
 382                              'readonly' => true,
 383                          ),
 384                          'color' => array(
 385                              'type'     => 'string',
 386                              'enum'     => array( 'blue', 'orange', 'red', 'green', 'purple', 'gray' ),
 387                              'readonly' => true,
 388                          ),
 389                      ),
 390                      'readonly'    => true,
 391                  ),
 392                  'description' => array(
 393                      'type'        => 'string',
 394                      'description' => __( 'A more descriptive explanation of what the test looks for, and why it is important for the user.' ),
 395                      'readonly'    => true,
 396                  ),
 397                  'actions'     => array(
 398                      'type'        => 'string',
 399                      'description' => __( 'HTML containing an action to direct the user to where they can resolve the issue.' ),
 400                      'readonly'    => true,
 401                  ),
 402              ),
 403          );
 404  
 405          return $this->schema;
 406      }
 407  }


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref