[ 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( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to view themes.' ), array( 'status' => rest_authorization_required_code() ) );
  63          }
  64  
  65          return true;
  66      }
  67  
  68      /**
  69       * Retrieves a collection of themes.
  70       *
  71       * @since 5.0.0
  72       *
  73       * @param WP_REST_Request $request Full details about the request.
  74       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  75       */
  76  	public function get_items( $request ) {
  77          // Retrieve the list of registered collection query parameters.
  78          $registered = $this->get_collection_params();
  79          $themes     = array();
  80  
  81          if ( isset( $registered['status'], $request['status'] ) && in_array( 'active', $request['status'], true ) ) {
  82              $active_theme = wp_get_theme();
  83              $active_theme = $this->prepare_item_for_response( $active_theme, $request );
  84              $themes[]     = $this->prepare_response_for_collection( $active_theme );
  85          }
  86  
  87          $response = rest_ensure_response( $themes );
  88  
  89          $response->header( 'X-WP-Total', count( $themes ) );
  90          $response->header( 'X-WP-TotalPages', count( $themes ) );
  91  
  92          return $response;
  93      }
  94  
  95      /**
  96       * Prepares a single theme output for response.
  97       *
  98       * @since 5.0.0
  99       *
 100       * @param WP_Theme        $theme   Theme object.
 101       * @param WP_REST_Request $request Request object.
 102       * @return WP_REST_Response Response object.
 103       */
 104  	public function prepare_item_for_response( $theme, $request ) {
 105          $data   = array();
 106          $fields = $this->get_fields_for_response( $request );
 107  
 108          if ( in_array( 'theme_supports', $fields, true ) ) {
 109              $formats                           = get_theme_support( 'post-formats' );
 110              $formats                           = is_array( $formats ) ? array_values( $formats[0] ) : array();
 111              $formats                           = array_merge( array( 'standard' ), $formats );
 112              $data['theme_supports']['formats'] = $formats;
 113  
 114              $data['theme_supports']['post-thumbnails']   = false;
 115              $data['theme_supports']['responsive-embeds'] = (bool) get_theme_support( 'responsive-embeds' );
 116              $post_thumbnails                             = get_theme_support( 'post-thumbnails' );
 117  
 118              if ( $post_thumbnails ) {
 119                  // $post_thumbnails can contain a nested array of post types.
 120                  // e.g. array( array( 'post', 'page' ) ).
 121                  $data['theme_supports']['post-thumbnails'] = is_array( $post_thumbnails ) ? $post_thumbnails[0] : true;
 122              }
 123          }
 124  
 125          $data = $this->add_additional_fields_to_object( $data, $request );
 126  
 127          // Wrap the data in a response object.
 128          $response = rest_ensure_response( $data );
 129  
 130          /**
 131           * Filters theme data returned from the REST API.
 132           *
 133           * @since 5.0.0
 134           *
 135           * @param WP_REST_Response $response The response object.
 136           * @param WP_Theme         $theme    Theme object used to create response.
 137           * @param WP_REST_Request  $request  Request object.
 138           */
 139          return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
 140      }
 141  
 142      /**
 143       * Retrieves the theme's schema, conforming to JSON Schema.
 144       *
 145       * @since 5.0.0
 146       *
 147       * @return array Item schema data.
 148       */
 149  	public function get_item_schema() {
 150          if ( $this->schema ) {
 151              return $this->add_additional_fields_schema( $this->schema );
 152          }
 153  
 154          $schema = array(
 155              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 156              'title'      => 'theme',
 157              'type'       => 'object',
 158              'properties' => array(
 159                  'theme_supports' => array(
 160                      'description' => __( 'Features supported by this theme.' ),
 161                      'type'        => 'array',
 162                      'readonly'    => true,
 163                      'properties'  => array(
 164                          'formats'           => array(
 165                              'description' => __( 'Post formats supported.' ),
 166                              'type'        => 'array',
 167                              'readonly'    => true,
 168                          ),
 169                          'post-thumbnails'   => array(
 170                              'description' => __( 'Whether the theme supports post thumbnails.' ),
 171                              'type'        => array( 'array', 'bool' ),
 172                              'readonly'    => true,
 173                          ),
 174                          'responsive-embeds' => array(
 175                              'description' => __( 'Whether the theme supports responsive embedded content.' ),
 176                              'type'        => 'bool',
 177                              'readonly'    => true,
 178                          ),
 179                      ),
 180                  ),
 181              ),
 182          );
 183  
 184          $this->schema = $schema;
 185          return $this->add_additional_fields_schema( $this->schema );
 186      }
 187  
 188      /**
 189       * Retrieves the search params for the themes collection.
 190       *
 191       * @since 5.0.0
 192       *
 193       * @return array Collection parameters.
 194       */
 195  	public function get_collection_params() {
 196          $query_params = parent::get_collection_params();
 197  
 198          $query_params['status'] = array(
 199              'description'       => __( 'Limit result set to themes assigned one or more statuses.' ),
 200              'type'              => 'array',
 201              'items'             => array(
 202                  'enum' => array( 'active' ),
 203                  'type' => 'string',
 204              ),
 205              'required'          => true,
 206              'sanitize_callback' => array( $this, 'sanitize_theme_status' ),
 207          );
 208  
 209          /**
 210           * Filter collection parameters for the themes controller.
 211           *
 212           * @since 5.0.0
 213           *
 214           * @param array $query_params JSON Schema-formatted collection parameters.
 215           */
 216          return apply_filters( 'rest_themes_collection_params', $query_params );
 217      }
 218  
 219      /**
 220       * Sanitizes and validates the list of theme status.
 221       *
 222       * @since 5.0.0
 223       *
 224       * @param string|array    $statuses  One or more theme statuses.
 225       * @param WP_REST_Request $request   Full details about the request.
 226       * @param string          $parameter Additional parameter to pass to validation.
 227       * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
 228       */
 229  	public function sanitize_theme_status( $statuses, $request, $parameter ) {
 230          $statuses = wp_parse_slug_list( $statuses );
 231  
 232          foreach ( $statuses as $status ) {
 233              $result = rest_validate_request_arg( $status, $request, $parameter );
 234  
 235              if ( is_wp_error( $result ) ) {
 236                  return $result;
 237              }
 238          }
 239  
 240          return $statuses;
 241      }
 242  }


Generated: Mon Sep 23 01:00:03 2019 Cross-referenced by PHPXref 0.7.1