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


Generated: Sun Feb 23 01:00:03 2020 Cross-referenced by PHPXref 0.7.1