[ 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 ( in_array( 'theme_supports', $fields, true ) ) {
 119              $item_schemas   = $this->get_item_schema();
 120              $theme_supports = $item_schemas['properties']['theme_supports']['properties'];
 121              foreach ( $theme_supports as $name => $schema ) {
 122                  if ( 'formats' === $name ) {
 123                      continue;
 124                  }
 125  
 126                  if ( ! current_theme_supports( $name ) ) {
 127                      $data['theme_supports'][ $name ] = false;
 128                      continue;
 129                  }
 130  
 131                  if ( 'boolean' === $schema['type'] ) {
 132                      $data['theme_supports'][ $name ] = true;
 133                      continue;
 134                  }
 135  
 136                  $support = get_theme_support( $name );
 137  
 138                  if ( is_array( $support ) ) {
 139                      // None of the Core theme supports have variadic args.
 140                      $support = $support[0];
 141  
 142                      // Core multi-type theme-support schema definitions always list boolean first.
 143                      if ( is_array( $schema['type'] ) && 'boolean' === $schema['type'][0] ) {
 144                          // Pass the non-boolean type through to the sanitizer, which cannot itself
 145                          // determine the intended type if the value is invalid (for example if an
 146                          // object includes non-safelisted properties).
 147                          $schema['type'] = $schema['type'][1];
 148                      }
 149                  }
 150  
 151                  $data['theme_supports'][ $name ] = rest_sanitize_value_from_schema( $support, $schema );
 152              }
 153  
 154              $formats = get_theme_support( 'post-formats' );
 155              $formats = is_array( $formats ) ? array_values( $formats[0] ) : array();
 156              $formats = array_merge( array( 'standard' ), $formats );
 157  
 158              $data['theme_supports']['formats'] = $formats;
 159          }
 160  
 161          $data = $this->add_additional_fields_to_object( $data, $request );
 162  
 163          // Wrap the data in a response object.
 164          $response = rest_ensure_response( $data );
 165  
 166          /**
 167           * Filters theme data returned from the REST API.
 168           *
 169           * @since 5.0.0
 170           *
 171           * @param WP_REST_Response $response The response object.
 172           * @param WP_Theme         $theme    Theme object used to create response.
 173           * @param WP_REST_Request  $request  Request object.
 174           */
 175          return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
 176      }
 177  
 178      /**
 179       * Retrieves the theme's schema, conforming to JSON Schema.
 180       *
 181       * @since 5.0.0
 182       *
 183       * @return array Item schema data.
 184       */
 185  	public function get_item_schema() {
 186          if ( $this->schema ) {
 187              return $this->add_additional_fields_schema( $this->schema );
 188          }
 189  
 190          $schema = array(
 191              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 192              'title'      => 'theme',
 193              'type'       => 'object',
 194              'properties' => array(
 195                  'theme_supports' => array(
 196                      'description' => __( 'Features supported by this theme.' ),
 197                      'type'        => 'object',
 198                      'readonly'    => true,
 199                      'properties'  => array(
 200                          'align-wide'                => array(
 201                              'description' => __( 'Whether theme opts in to wide alignment CSS class.' ),
 202                              'type'        => 'boolean',
 203                          ),
 204                          'automatic-feed-links'      => array(
 205                              'description' => __( 'Whether posts and comments RSS feed links are added to head.' ),
 206                              'type'        => 'boolean',
 207                          ),
 208                          'custom-header'             => array(
 209                              'description'          => __( 'Custom header if defined by the theme.' ),
 210                              'type'                 => array( 'boolean', 'object' ),
 211                              'properties'           => array(
 212                                  'default-image'      => array(
 213                                      'type'   => 'string',
 214                                      'format' => 'uri',
 215                                  ),
 216                                  'random-default'     => array(
 217                                      'type' => 'boolean',
 218                                  ),
 219                                  'width'              => array(
 220                                      'type' => 'integer',
 221                                  ),
 222                                  'height'             => array(
 223                                      'type' => 'integer',
 224                                  ),
 225                                  'flex-height'        => array(
 226                                      'type' => 'boolean',
 227                                  ),
 228                                  'flex-width'         => array(
 229                                      'type' => 'boolean',
 230                                  ),
 231                                  'default-text-color' => array(
 232                                      'type' => 'string',
 233                                  ),
 234                                  'header-text'        => array(
 235                                      'type' => 'boolean',
 236                                  ),
 237                                  'uploads'            => array(
 238                                      'type' => 'boolean',
 239                                  ),
 240                                  'video'              => array(
 241                                      'type' => 'boolean',
 242                                  ),
 243                              ),
 244                              'additionalProperties' => false,
 245                          ),
 246                          'custom-background'         => array(
 247                              'description'          => __( 'Custom background if defined by the theme.' ),
 248                              'type'                 => array( 'boolean', 'object' ),
 249                              'properties'           => array(
 250                                  'default-image'      => array(
 251                                      'type'   => 'string',
 252                                      'format' => 'uri',
 253                                  ),
 254                                  'default-preset'     => array(
 255                                      'type' => 'string',
 256                                      'enum' => array(
 257                                          'default',
 258                                          'fill',
 259                                          'fit',
 260                                          'repeat',
 261                                          'custom',
 262                                      ),
 263                                  ),
 264                                  'default-position-x' => array(
 265                                      'type' => 'string',
 266                                      'enum' => array(
 267                                          'left',
 268                                          'center',
 269                                          'right',
 270                                      ),
 271                                  ),
 272                                  'default-position-y' => array(
 273                                      'type' => 'string',
 274                                      'enum' => array(
 275                                          'left',
 276                                          'center',
 277                                          'right',
 278                                      ),
 279                                  ),
 280                                  'default-size'       => array(
 281                                      'type' => 'string',
 282                                      'enum' => array(
 283                                          'auto',
 284                                          'contain',
 285                                          'cover',
 286                                      ),
 287                                  ),
 288                                  'default-repeat'     => array(
 289                                      'type' => 'string',
 290                                      'enum' => array(
 291                                          'repeat-x',
 292                                          'repeat-y',
 293                                          'repeat',
 294                                          'no-repeat',
 295                                      ),
 296                                  ),
 297                                  'default-attachment' => array(
 298                                      'type' => 'string',
 299                                      'enum' => array(
 300                                          'scroll',
 301                                          'fixed',
 302                                      ),
 303                                  ),
 304                                  'default-color'      => array(
 305                                      'type' => 'string',
 306                                  ),
 307                              ),
 308                              'additionalProperties' => false,
 309                          ),
 310                          'custom-logo'               => array(
 311                              'description'          => __( 'Custom logo if defined by the theme.' ),
 312                              'type'                 => array( 'boolean', 'object' ),
 313                              'properties'           => array(
 314                                  'width'       => array(
 315                                      'type' => 'integer',
 316                                  ),
 317                                  'height'      => array(
 318                                      'type' => 'integer',
 319                                  ),
 320                                  'flex-width'  => array(
 321                                      'type' => 'boolean',
 322                                  ),
 323                                  'flex-height' => array(
 324                                      'type' => 'boolean',
 325                                  ),
 326                                  'header-text' => array(
 327                                      'type'  => 'array',
 328                                      'items' => array(
 329                                          'type' => 'string',
 330                                      ),
 331                                  ),
 332                              ),
 333                              'additionalProperties' => false,
 334                          ),
 335                          'customize-selective-refresh-widgets' => array(
 336                              'description' => __( 'Whether the theme enables Selective Refresh for Widgets being managed with the Customizer.' ),
 337                              'type'        => 'boolean',
 338                          ),
 339                          'dark-editor-style'         => array(
 340                              'description' => __( 'Whether theme opts in to the dark editor style UI.' ),
 341                              'type'        => 'boolean',
 342                          ),
 343                          'disable-custom-colors'     => array(
 344                              'description' => __( 'Whether the theme disables custom colors.' ),
 345                              'type'        => 'boolean',
 346                          ),
 347                          'disable-custom-font-sizes' => array(
 348                              'description' => __( 'Whether the theme disables custom font sizes.' ),
 349                              'type'        => 'boolean',
 350                          ),
 351                          'disable-custom-gradients'  => array(
 352                              'description' => __( 'Whether the theme disables custom gradients.' ),
 353                              'type'        => 'boolean',
 354                          ),
 355                          'editor-color-palette'      => array(
 356                              'description' => __( 'Custom color palette if defined by the theme.' ),
 357                              'type'        => array( 'boolean', 'array' ),
 358                              'items'       => array(
 359                                  'type'                 => 'object',
 360                                  'properties'           => array(
 361                                      'name'  => array(
 362                                          'type' => 'string',
 363                                      ),
 364                                      'slug'  => array(
 365                                          'type' => 'string',
 366                                      ),
 367                                      'color' => array(
 368                                          'type' => 'string',
 369                                      ),
 370                                  ),
 371                                  'additionalProperties' => false,
 372                              ),
 373                          ),
 374                          'editor-font-sizes'         => array(
 375                              'description' => __( 'Custom font sizes if defined by the theme.' ),
 376                              'type'        => array( 'boolean', 'array' ),
 377                              'items'       => array(
 378                                  'type'                 => 'object',
 379                                  'properties'           => array(
 380                                      'name' => array(
 381                                          'type' => 'string',
 382                                      ),
 383                                      'size' => array(
 384                                          'type' => 'number',
 385                                      ),
 386                                      'slug' => array(
 387                                          'type' => 'string',
 388                                      ),
 389                                  ),
 390                                  'additionalProperties' => false,
 391                              ),
 392                          ),
 393                          'editor-gradient-presets'   => array(
 394                              'description' => __( 'Custom gradient presets if defined by the theme.' ),
 395                              'type'        => array( 'boolean', 'array' ),
 396                              'items'       => array(
 397                                  'type'                 => 'object',
 398                                  'properties'           => array(
 399                                      'name'     => array(
 400                                          'type' => 'string',
 401                                      ),
 402                                      'gradient' => array(
 403                                          'type' => 'string',
 404                                      ),
 405                                      'slug'     => array(
 406                                          'type' => 'string',
 407                                      ),
 408                                  ),
 409                                  'additionalProperties' => false,
 410                              ),
 411                          ),
 412                          'editor-styles'             => array(
 413                              'description' => __( 'Whether theme opts in to the editor styles CSS wrapper.' ),
 414                              'type'        => 'boolean',
 415                          ),
 416                          'formats'                   => array(
 417                              'description' => __( 'Post formats supported.' ),
 418                              'type'        => 'array',
 419                              'items'       => array(
 420                                  'type' => 'string',
 421                                  'enum' => get_post_format_slugs(),
 422                              ),
 423                          ),
 424                          'html5'                     => array(
 425                              'description' => __( 'Allows use of html5 markup for search forms, comment forms, comment lists, gallery, and caption.' ),
 426                              'type'        => array( 'boolean', 'array' ),
 427                              'items'       => array(
 428                                  'type' => 'string',
 429                                  'enum' => array(
 430                                      'search-form',
 431                                      'comment-form',
 432                                      'comment-list',
 433                                      'gallery',
 434                                      'caption',
 435                                      'script',
 436                                      'style',
 437                                  ),
 438                              ),
 439                          ),
 440                          'post-thumbnails'           => array(
 441                              'description' => __( 'Whether the theme supports post thumbnails.' ),
 442                              'type'        => array( 'boolean', 'array' ),
 443                              'items'       => array(
 444                                  'type' => 'string',
 445                              ),
 446                          ),
 447                          'responsive-embeds'         => array(
 448                              'description' => __( 'Whether the theme supports responsive embedded content.' ),
 449                              'type'        => 'boolean',
 450                          ),
 451                          'title-tag'                 => array(
 452                              'description' => __( 'Whether the theme can manage the document title tag.' ),
 453                              'type'        => 'boolean',
 454                          ),
 455                          'wp-block-styles'           => array(
 456                              'description' => __( 'Whether theme opts in to default WordPress block styles for viewing.' ),
 457                              'type'        => 'boolean',
 458                          ),
 459                      ),
 460                  ),
 461              ),
 462          );
 463  
 464          $this->schema = $schema;
 465  
 466          return $this->add_additional_fields_schema( $this->schema );
 467      }
 468  
 469      /**
 470       * Retrieves the search params for the themes collection.
 471       *
 472       * @since 5.0.0
 473       *
 474       * @return array Collection parameters.
 475       */
 476  	public function get_collection_params() {
 477          $query_params = parent::get_collection_params();
 478  
 479          $query_params['status'] = array(
 480              'description'       => __( 'Limit result set to themes assigned one or more statuses.' ),
 481              'type'              => 'array',
 482              'items'             => array(
 483                  'enum' => array( 'active' ),
 484                  'type' => 'string',
 485              ),
 486              'required'          => true,
 487              'sanitize_callback' => array( $this, 'sanitize_theme_status' ),
 488          );
 489  
 490          /**
 491           * Filter collection parameters for the themes controller.
 492           *
 493           * @since 5.0.0
 494           *
 495           * @param array $query_params JSON Schema-formatted collection parameters.
 496           */
 497          return apply_filters( 'rest_themes_collection_params', $query_params );
 498      }
 499  
 500      /**
 501       * Sanitizes and validates the list of theme status.
 502       *
 503       * @since 5.0.0
 504       *
 505       * @param string|array    $statuses  One or more theme statuses.
 506       * @param WP_REST_Request $request   Full details about the request.
 507       * @param string          $parameter Additional parameter to pass to validation.
 508       * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
 509       */
 510  	public function sanitize_theme_status( $statuses, $request, $parameter ) {
 511          $statuses = wp_parse_slug_list( $statuses );
 512  
 513          foreach ( $statuses as $status ) {
 514              $result = rest_validate_request_arg( $status, $request, $parameter );
 515  
 516              if ( is_wp_error( $result ) ) {
 517                  return $result;
 518              }
 519          }
 520  
 521          return $statuses;
 522      }
 523  }


Generated: Sun Mar 29 01:00:04 2020 Cross-referenced by PHPXref 0.7.1