[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * REST API: WP_REST_Themes_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 5.0.0
   8   */
   9  
  10  /**
  11   * Core class used to manage themes via the REST API.
  12   *
  13   * @since 5.0.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Themes_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 5.0.0
  23       */
  24  	public function __construct() {
  25          $this->namespace = 'wp/v2';
  26          $this->rest_base = 'themes';
  27      }
  28  
  29      /**
  30       * Registers the routes for the objects of the controller.
  31       *
  32       * @since 5.0.0
  33       *
  34       * @see register_rest_route()
  35       */
  36  	public function register_routes() {
  37          register_rest_route(
  38              $this->namespace,
  39              '/' . $this->rest_base,
  40              array(
  41                  array(
  42                      'methods'             => WP_REST_Server::READABLE,
  43                      'callback'            => array( $this, 'get_items' ),
  44                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  45                      'args'                => $this->get_collection_params(),
  46                  ),
  47                  'schema' => array( $this, 'get_item_schema' ),
  48              )
  49          );
  50      }
  51  
  52      /**
  53       * Checks if a given request has access to read the theme.
  54       *
  55       * @since 5.0.0
  56       *
  57       * @param WP_REST_Request $request Full details about the request.
  58       * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
  59       */
  60  	public function get_items_permissions_check( $request ) {
  61          if ( current_user_can( 'edit_posts' ) ) {
  62              return true;
  63          }
  64  
  65          foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  66              if ( current_user_can( $post_type->cap->edit_posts ) ) {
  67                  return true;
  68              }
  69          }
  70  
  71          return new WP_Error(
  72              'rest_user_cannot_view',
  73              __( 'Sorry, you are not allowed to view themes.' ),
  74              array( 'status' => rest_authorization_required_code() )
  75          );
  76      }
  77  
  78      /**
  79       * Retrieves a collection of themes.
  80       *
  81       * @since 5.0.0
  82       *
  83       * @param WP_REST_Request $request Full details about the request.
  84       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  85       */
  86  	public function get_items( $request ) {
  87          // Retrieve the list of registered collection query parameters.
  88          $registered = $this->get_collection_params();
  89          $themes     = array();
  90  
  91          if ( isset( $registered['status'], $request['status'] ) && in_array( 'active', $request['status'], true ) ) {
  92              $active_theme = wp_get_theme();
  93              $active_theme = $this->prepare_item_for_response( $active_theme, $request );
  94              $themes[]     = $this->prepare_response_for_collection( $active_theme );
  95          }
  96  
  97          $response = rest_ensure_response( $themes );
  98  
  99          $response->header( 'X-WP-Total', count( $themes ) );
 100          $response->header( 'X-WP-TotalPages', count( $themes ) );
 101  
 102          return $response;
 103      }
 104  
 105      /**
 106       * Prepares a single theme output for response.
 107       *
 108       * @since 5.0.0
 109       *
 110       * @param WP_Theme        $theme   Theme object.
 111       * @param WP_REST_Request $request Request object.
 112       * @return WP_REST_Response Response object.
 113       */
 114  	public function prepare_item_for_response( $theme, $request ) {
 115          $data   = array();
 116          $fields = $this->get_fields_for_response( $request );
 117  
 118          if ( rest_is_field_included( 'stylesheet', $fields ) ) {
 119              $data['stylesheet'] = $theme->get_stylesheet();
 120          }
 121  
 122          if ( rest_is_field_included( 'template', $fields ) ) {
 123              /**
 124               * Use the get_template() method, not the 'Template' header, for finding the template.
 125               * The 'Template' header is only good for what was written in the style.css, while
 126               * get_template() takes into account where WordPress actually located the theme and
 127               * whether it is actually valid.
 128               */
 129              $data['template'] = $theme->get_template();
 130          }
 131  
 132          $plain_field_mappings = array(
 133              'requires_php' => 'RequiresPHP',
 134              'requires_wp'  => 'RequiresWP',
 135              'textdomain'   => 'TextDomain',
 136              'version'      => 'Version',
 137          );
 138  
 139          foreach ( $plain_field_mappings as $field => $header ) {
 140              if ( rest_is_field_included( $field, $fields ) ) {
 141                  $data[ $field ] = $theme->get( $header );
 142              }
 143          }
 144  
 145          if ( rest_is_field_included( 'screenshot', $fields ) ) {
 146              // Using $theme->get_screenshot() with no args to get absolute URL.
 147              $data['screenshot'] = $theme->get_screenshot() ? $theme->get_screenshot() : '';
 148          }
 149  
 150          $rich_field_mappings = array(
 151              'author'      => 'Author',
 152              'author_uri'  => 'AuthorURI',
 153              'description' => 'Description',
 154              'name'        => 'Name',
 155              'tags'        => 'Tags',
 156              'theme_uri'   => 'ThemeURI',
 157          );
 158  
 159          foreach ( $rich_field_mappings as $field => $header ) {
 160              if ( rest_is_field_included( "{$field}.raw", $fields ) ) {
 161                  $data[ $field ]['raw'] = $theme->display( $header, false, true );
 162              }
 163  
 164              if ( rest_is_field_included( "{$field}.rendered", $fields ) ) {
 165                  $data[ $field ]['rendered'] = $theme->display( $header );
 166              }
 167          }
 168  
 169          if ( rest_is_field_included( 'theme_supports', $fields ) ) {
 170              foreach ( get_registered_theme_features() as $feature => $config ) {
 171                  if ( ! is_array( $config['show_in_rest'] ) ) {
 172                      continue;
 173                  }
 174  
 175                  $name = $config['show_in_rest']['name'];
 176  
 177                  if ( ! rest_is_field_included( "theme_supports.{$name}", $fields ) ) {
 178                      continue;
 179                  }
 180  
 181                  if ( ! current_theme_supports( $feature ) ) {
 182                      $data['theme_supports'][ $name ] = $config['show_in_rest']['schema']['default'];
 183                      continue;
 184                  }
 185  
 186                  $support = get_theme_support( $feature );
 187  
 188                  if ( isset( $config['show_in_rest']['prepare_callback'] ) ) {
 189                      $prepare = $config['show_in_rest']['prepare_callback'];
 190                  } else {
 191                      $prepare = array( $this, 'prepare_theme_support' );
 192                  }
 193  
 194                  $prepared = $prepare( $support, $config, $feature, $request );
 195  
 196                  if ( is_wp_error( $prepared ) ) {
 197                      continue;
 198                  }
 199  
 200                  $data['theme_supports'][ $name ] = $prepared;
 201              }
 202          }
 203  
 204          $data = $this->add_additional_fields_to_object( $data, $request );
 205  
 206          // Wrap the data in a response object.
 207          $response = rest_ensure_response( $data );
 208  
 209          /**
 210           * Filters theme data returned from the REST API.
 211           *
 212           * @since 5.0.0
 213           *
 214           * @param WP_REST_Response $response The response object.
 215           * @param WP_Theme         $theme    Theme object used to create response.
 216           * @param WP_REST_Request  $request  Request object.
 217           */
 218          return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
 219      }
 220  
 221      /**
 222       * Prepares the theme support value for inclusion in the REST API response.
 223       *
 224       * @since 5.5.0
 225       *
 226       * @param mixed           $support The raw value from get_theme_support().
 227       * @param array           $args    The feature's registration args.
 228       * @param string          $feature The feature name.
 229       * @param WP_REST_Request $request The request object.
 230       * @return mixed The prepared support value.
 231       */
 232  	protected function prepare_theme_support( $support, $args, $feature, $request ) {
 233          $schema = $args['show_in_rest']['schema'];
 234  
 235          if ( 'boolean' === $schema['type'] ) {
 236              return true;
 237          }
 238  
 239          if ( is_array( $support ) ) {
 240              if ( ! $args['variadic'] ) {
 241                  $support = $support[0];
 242              }
 243  
 244              // Multi-type theme-support schema definitions always list boolean first.
 245              if ( is_array( $schema['type'] ) && 'boolean' === $schema['type'][0] ) {
 246                  // Pass the non-boolean type through to the sanitizer, which cannot itself
 247                  // determine the intended type if the value is invalid (for example if an
 248                  // object includes non-safelisted properties). See #50300.
 249                  $schema['type'] = $schema['type'][1];
 250              }
 251          }
 252  
 253          return rest_sanitize_value_from_schema( $support, $schema );
 254      }
 255  
 256      /**
 257       * Retrieves the theme's schema, conforming to JSON Schema.
 258       *
 259       * @since 5.0.0
 260       *
 261       * @return array Item schema data.
 262       */
 263  	public function get_item_schema() {
 264          if ( $this->schema ) {
 265              return $this->add_additional_fields_schema( $this->schema );
 266          }
 267  
 268          $schema = array(
 269              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 270              'title'      => 'theme',
 271              'type'       => 'object',
 272              'properties' => array(
 273                  'stylesheet'     => array(
 274                      'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ),
 275                      'type'        => 'string',
 276                      'readonly'    => true,
 277                  ),
 278                  'template'       => array(
 279                      'description' => __( 'The theme\'s template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet.' ),
 280                      'type'        => 'string',
 281                      'readonly'    => true,
 282                  ),
 283                  'author'         => array(
 284                      'description' => __( 'The theme author.' ),
 285                      'type'        => 'object',
 286                      'readonly'    => true,
 287                      'properties'  => array(
 288                          'raw'      => array(
 289                              'description' => __( 'The theme author\'s name, as found in the theme header.' ),
 290                              'type'        => 'string',
 291                          ),
 292                          'rendered' => array(
 293                              'description' => __( 'HTML for the theme author, transformed for display.' ),
 294                              'type'        => 'string',
 295                          ),
 296                      ),
 297                  ),
 298                  'author_uri'     => array(
 299                      'description' => __( 'The website of the theme author.' ),
 300                      'type'        => 'object',
 301                      'readonly'    => true,
 302                      'properties'  => array(
 303                          'raw'      => array(
 304                              'description' => __( 'The website of the theme author, as found in the theme header.' ),
 305                              'type'        => 'string',
 306                              'format'      => 'uri',
 307                          ),
 308                          'rendered' => array(
 309                              'description' => __( 'The website of the theme author, transformed for display.' ),
 310                              'type'        => 'string',
 311                              'format'      => 'uri',
 312                          ),
 313                      ),
 314                  ),
 315                  'description'    => array(
 316                      'description' => __( 'A description of the theme.' ),
 317                      'type'        => 'object',
 318                      'readonly'    => true,
 319                      'properties'  => array(
 320                          'raw'      => array(
 321                              'description' => __( 'The theme description, as found in the theme header.' ),
 322                              'type'        => 'string',
 323                          ),
 324                          'rendered' => array(
 325                              'description' => __( 'The theme description, transformed for display.' ),
 326                              'type'        => 'string',
 327                          ),
 328                      ),
 329                  ),
 330                  'name'           => array(
 331                      'description' => __( 'The name of the theme.' ),
 332                      'type'        => 'object',
 333                      'readonly'    => true,
 334                      'properties'  => array(
 335                          'raw'      => array(
 336                              'description' => __( 'The theme name, as found in the theme header.' ),
 337                              'type'        => 'string',
 338                          ),
 339                          'rendered' => array(
 340                              'description' => __( 'The theme name, transformed for display.' ),
 341                              'type'        => 'string',
 342                          ),
 343                      ),
 344                  ),
 345                  'requires_php'   => array(
 346                      'description' => __( 'The minimum PHP version required for the theme to work.' ),
 347                      'type'        => 'string',
 348                      'readonly'    => true,
 349                  ),
 350                  'requires_wp'    => array(
 351                      'description' => __( 'The minimum WordPress version required for the theme to work.' ),
 352                      'type'        => 'string',
 353                      'readonly'    => true,
 354                  ),
 355                  'screenshot'     => array(
 356                      'description' => __( 'The theme\'s screenshot URL.' ),
 357                      'type'        => 'string',
 358                      'format'      => 'uri',
 359                      'readonly'    => true,
 360                  ),
 361                  'tags'           => array(
 362                      'description' => __( 'Tags indicating styles and features of the theme.' ),
 363                      'type'        => 'object',
 364                      'readonly'    => true,
 365                      'properties'  => array(
 366                          'raw'      => array(
 367                              'description' => __( 'The theme tags, as found in the theme header.' ),
 368                              'type'        => 'array',
 369                              'items'       => array(
 370                                  'type' => 'string',
 371                              ),
 372                          ),
 373                          'rendered' => array(
 374                              'description' => __( 'The theme tags, transformed for display.' ),
 375                              'type'        => 'string',
 376                          ),
 377                      ),
 378                  ),
 379                  'textdomain'     => array(
 380                      'description' => __( 'The theme\'s textdomain.' ),
 381                      'type'        => 'string',
 382                      'readonly'    => true,
 383                  ),
 384                  'theme_supports' => array(
 385                      'description' => __( 'Features supported by this theme.' ),
 386                      'type'        => 'object',
 387                      'readonly'    => true,
 388                      'properties'  => array(),
 389                  ),
 390                  'theme_uri'      => array(
 391                      'description' => __( 'The URI of the theme\'s webpage.' ),
 392                      'type'        => 'object',
 393                      'readonly'    => true,
 394                      'properties'  => array(
 395                          'raw'      => array(
 396                              'description' => __( 'The URI of the theme\'s webpage, as found in the theme header.' ),
 397                              'type'        => 'string',
 398                              'format'      => 'uri',
 399                          ),
 400                          'rendered' => array(
 401                              'description' => __( 'The URI of the theme\'s webpage, transformed for display.' ),
 402                              'type'        => 'string',
 403                              'format'      => 'uri',
 404                          ),
 405                      ),
 406                  ),
 407                  'version'        => array(
 408                      'description' => __( 'The theme\'s current version.' ),
 409                      'type'        => 'string',
 410                      'readonly'    => true,
 411                  ),
 412              ),
 413          );
 414  
 415          foreach ( get_registered_theme_features() as $feature => $config ) {
 416              if ( ! is_array( $config['show_in_rest'] ) ) {
 417                  continue;
 418              }
 419  
 420              $name = $config['show_in_rest']['name'];
 421  
 422              $schema['properties']['theme_supports']['properties'][ $name ] = $config['show_in_rest']['schema'];
 423          }
 424  
 425          $this->schema = $schema;
 426  
 427          return $this->add_additional_fields_schema( $this->schema );
 428      }
 429  
 430      /**
 431       * Retrieves the search params for the themes collection.
 432       *
 433       * @since 5.0.0
 434       *
 435       * @return array Collection parameters.
 436       */
 437  	public function get_collection_params() {
 438          $query_params = parent::get_collection_params();
 439  
 440          $query_params['status'] = array(
 441              'description'       => __( 'Limit result set to themes assigned one or more statuses.' ),
 442              'type'              => 'array',
 443              'items'             => array(
 444                  'enum' => array( 'active' ),
 445                  'type' => 'string',
 446              ),
 447              'required'          => true,
 448              'sanitize_callback' => array( $this, 'sanitize_theme_status' ),
 449          );
 450  
 451          /**
 452           * Filter collection parameters for the themes controller.
 453           *
 454           * @since 5.0.0
 455           *
 456           * @param array $query_params JSON Schema-formatted collection parameters.
 457           */
 458          return apply_filters( 'rest_themes_collection_params', $query_params );
 459      }
 460  
 461      /**
 462       * Sanitizes and validates the list of theme status.
 463       *
 464       * @since 5.0.0
 465       *
 466       * @param string|array    $statuses  One or more theme statuses.
 467       * @param WP_REST_Request $request   Full details about the request.
 468       * @param string          $parameter Additional parameter to pass to validation.
 469       * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
 470       */
 471  	public function sanitize_theme_status( $statuses, $request, $parameter ) {
 472          $statuses = wp_parse_slug_list( $statuses );
 473  
 474          foreach ( $statuses as $status ) {
 475              $result = rest_validate_request_arg( $status, $request, $parameter );
 476  
 477              if ( is_wp_error( $result ) ) {
 478                  return $result;
 479              }
 480          }
 481  
 482          return $statuses;
 483      }
 484  }


Generated: Sun Jul 5 01:00:04 2020 Cross-referenced by PHPXref 0.7.1