[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * REST API: WP_REST_Post_Types_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 4.7.0
   8   */
   9  
  10  /**
  11   * Core class to access post types via the REST API.
  12   *
  13   * @since 4.7.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Post_Types_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 4.7.0
  23       */
  24  	public function __construct() {
  25          $this->namespace = 'wp/v2';
  26          $this->rest_base = 'types';
  27      }
  28  
  29      /**
  30       * Registers the routes for post types.
  31       *
  32       * @since 4.7.0
  33       *
  34       * @see register_rest_route()
  35       */
  36  	public function register_routes() {
  37  
  38          register_rest_route(
  39              $this->namespace,
  40              '/' . $this->rest_base,
  41              array(
  42                  array(
  43                      'methods'             => WP_REST_Server::READABLE,
  44                      'callback'            => array( $this, 'get_items' ),
  45                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  46                      'args'                => $this->get_collection_params(),
  47                  ),
  48                  'schema' => array( $this, 'get_public_item_schema' ),
  49              )
  50          );
  51  
  52          register_rest_route(
  53              $this->namespace,
  54              '/' . $this->rest_base . '/(?P<type>[\w-]+)',
  55              array(
  56                  'args'   => array(
  57                      'type' => array(
  58                          'description' => __( 'An alphanumeric identifier for the post type.' ),
  59                          'type'        => 'string',
  60                      ),
  61                  ),
  62                  array(
  63                      'methods'             => WP_REST_Server::READABLE,
  64                      'callback'            => array( $this, 'get_item' ),
  65                      'permission_callback' => '__return_true',
  66                      'args'                => array(
  67                          'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  68                      ),
  69                  ),
  70                  'schema' => array( $this, 'get_public_item_schema' ),
  71              )
  72          );
  73      }
  74  
  75      /**
  76       * Checks whether a given request has permission to read types.
  77       *
  78       * @since 4.7.0
  79       *
  80       * @param WP_REST_Request $request Full details about the request.
  81       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  82       */
  83  	public function get_items_permissions_check( $request ) {
  84          if ( 'edit' === $request['context'] ) {
  85              $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
  86  
  87              foreach ( $types as $type ) {
  88                  if ( current_user_can( $type->cap->edit_posts ) ) {
  89                      return true;
  90                  }
  91              }
  92  
  93              return new WP_Error(
  94                  'rest_cannot_view',
  95                  __( 'Sorry, you are not allowed to edit posts in this post type.' ),
  96                  array( 'status' => rest_authorization_required_code() )
  97              );
  98          }
  99  
 100          return true;
 101      }
 102  
 103      /**
 104       * Retrieves all public post types.
 105       *
 106       * @since 4.7.0
 107       *
 108       * @param WP_REST_Request $request Full details about the request.
 109       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 110       */
 111  	public function get_items( $request ) {
 112          $data  = array();
 113          $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
 114  
 115          foreach ( $types as $type ) {
 116              if ( 'edit' === $request['context'] && ! current_user_can( $type->cap->edit_posts ) ) {
 117                  continue;
 118              }
 119  
 120              $post_type           = $this->prepare_item_for_response( $type, $request );
 121              $data[ $type->name ] = $this->prepare_response_for_collection( $post_type );
 122          }
 123  
 124          return rest_ensure_response( $data );
 125      }
 126  
 127      /**
 128       * Retrieves a specific post type.
 129       *
 130       * @since 4.7.0
 131       *
 132       * @param WP_REST_Request $request Full details about the request.
 133       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 134       */
 135  	public function get_item( $request ) {
 136          $obj = get_post_type_object( $request['type'] );
 137  
 138          if ( empty( $obj ) ) {
 139              return new WP_Error(
 140                  'rest_type_invalid',
 141                  __( 'Invalid post type.' ),
 142                  array( 'status' => 404 )
 143              );
 144          }
 145  
 146          if ( empty( $obj->show_in_rest ) ) {
 147              return new WP_Error(
 148                  'rest_cannot_read_type',
 149                  __( 'Cannot view post type.' ),
 150                  array( 'status' => rest_authorization_required_code() )
 151              );
 152          }
 153  
 154          if ( 'edit' === $request['context'] && ! current_user_can( $obj->cap->edit_posts ) ) {
 155              return new WP_Error(
 156                  'rest_forbidden_context',
 157                  __( 'Sorry, you are not allowed to edit posts in this post type.' ),
 158                  array( 'status' => rest_authorization_required_code() )
 159              );
 160          }
 161  
 162          $data = $this->prepare_item_for_response( $obj, $request );
 163  
 164          return rest_ensure_response( $data );
 165      }
 166  
 167      /**
 168       * Prepares a post type object for serialization.
 169       *
 170       * @since 4.7.0
 171       * @since 5.9.0 Renamed `$post_type` to `$item` to match parent class for PHP 8 named parameter support.
 172       *
 173       * @param WP_Post_Type    $item    Post type object.
 174       * @param WP_REST_Request $request Full details about the request.
 175       * @return WP_REST_Response Response object.
 176       */
 177  	public function prepare_item_for_response( $item, $request ) {
 178          // Restores the more descriptive, specific name for use within this method.
 179          $post_type  = $item;
 180          $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
 181          $taxonomies = wp_list_pluck( $taxonomies, 'name' );
 182          $base       = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
 183          $supports   = get_all_post_type_supports( $post_type->name );
 184  
 185          $fields = $this->get_fields_for_response( $request );
 186          $data   = array();
 187  
 188          if ( in_array( 'capabilities', $fields, true ) ) {
 189              $data['capabilities'] = $post_type->cap;
 190          }
 191  
 192          if ( in_array( 'description', $fields, true ) ) {
 193              $data['description'] = $post_type->description;
 194          }
 195  
 196          if ( in_array( 'hierarchical', $fields, true ) ) {
 197              $data['hierarchical'] = $post_type->hierarchical;
 198          }
 199  
 200          if ( in_array( 'viewable', $fields, true ) ) {
 201              $data['viewable'] = is_post_type_viewable( $post_type );
 202          }
 203  
 204          if ( in_array( 'labels', $fields, true ) ) {
 205              $data['labels'] = $post_type->labels;
 206          }
 207  
 208          if ( in_array( 'name', $fields, true ) ) {
 209              $data['name'] = $post_type->label;
 210          }
 211  
 212          if ( in_array( 'slug', $fields, true ) ) {
 213              $data['slug'] = $post_type->name;
 214          }
 215  
 216          if ( in_array( 'supports', $fields, true ) ) {
 217              $data['supports'] = $supports;
 218          }
 219  
 220          if ( in_array( 'taxonomies', $fields, true ) ) {
 221              $data['taxonomies'] = array_values( $taxonomies );
 222          }
 223  
 224          if ( in_array( 'rest_base', $fields, true ) ) {
 225              $data['rest_base'] = $base;
 226          }
 227  
 228          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 229          $data    = $this->add_additional_fields_to_object( $data, $request );
 230          $data    = $this->filter_response_by_context( $data, $context );
 231  
 232          // Wrap the data in a response object.
 233          $response = rest_ensure_response( $data );
 234  
 235          $response->add_links(
 236              array(
 237                  'collection'              => array(
 238                      'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
 239                  ),
 240                  'https://api.w.org/items' => array(
 241                      'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
 242                  ),
 243              )
 244          );
 245  
 246          /**
 247           * Filters a post type returned from the REST API.
 248           *
 249           * Allows modification of the post type data right before it is returned.
 250           *
 251           * @since 4.7.0
 252           *
 253           * @param WP_REST_Response $response  The response object.
 254           * @param WP_Post_Type     $post_type The original post type object.
 255           * @param WP_REST_Request  $request   Request used to generate the response.
 256           */
 257          return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request );
 258      }
 259  
 260      /**
 261       * Retrieves the post type's schema, conforming to JSON Schema.
 262       *
 263       * @since 4.7.0
 264       *
 265       * @return array Item schema data.
 266       */
 267  	public function get_item_schema() {
 268          if ( $this->schema ) {
 269              return $this->add_additional_fields_schema( $this->schema );
 270          }
 271  
 272          $schema = array(
 273              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 274              'title'      => 'type',
 275              'type'       => 'object',
 276              'properties' => array(
 277                  'capabilities' => array(
 278                      'description' => __( 'All capabilities used by the post type.' ),
 279                      'type'        => 'object',
 280                      'context'     => array( 'edit' ),
 281                      'readonly'    => true,
 282                  ),
 283                  'description'  => array(
 284                      'description' => __( 'A human-readable description of the post type.' ),
 285                      'type'        => 'string',
 286                      'context'     => array( 'view', 'edit' ),
 287                      'readonly'    => true,
 288                  ),
 289                  'hierarchical' => array(
 290                      'description' => __( 'Whether or not the post type should have children.' ),
 291                      'type'        => 'boolean',
 292                      'context'     => array( 'view', 'edit' ),
 293                      'readonly'    => true,
 294                  ),
 295                  'viewable'     => array(
 296                      'description' => __( 'Whether or not the post type can be viewed.' ),
 297                      'type'        => 'boolean',
 298                      'context'     => array( 'edit' ),
 299                      'readonly'    => true,
 300                  ),
 301                  'labels'       => array(
 302                      'description' => __( 'Human-readable labels for the post type for various contexts.' ),
 303                      'type'        => 'object',
 304                      'context'     => array( 'edit' ),
 305                      'readonly'    => true,
 306                  ),
 307                  'name'         => array(
 308                      'description' => __( 'The title for the post type.' ),
 309                      'type'        => 'string',
 310                      'context'     => array( 'view', 'edit', 'embed' ),
 311                      'readonly'    => true,
 312                  ),
 313                  'slug'         => array(
 314                      'description' => __( 'An alphanumeric identifier for the post type.' ),
 315                      'type'        => 'string',
 316                      'context'     => array( 'view', 'edit', 'embed' ),
 317                      'readonly'    => true,
 318                  ),
 319                  'supports'     => array(
 320                      'description' => __( 'All features, supported by the post type.' ),
 321                      'type'        => 'object',
 322                      'context'     => array( 'edit' ),
 323                      'readonly'    => true,
 324                  ),
 325                  'taxonomies'   => array(
 326                      'description' => __( 'Taxonomies associated with post type.' ),
 327                      'type'        => 'array',
 328                      'items'       => array(
 329                          'type' => 'string',
 330                      ),
 331                      'context'     => array( 'view', 'edit' ),
 332                      'readonly'    => true,
 333                  ),
 334                  'rest_base'    => array(
 335                      'description' => __( 'REST base route for the post type.' ),
 336                      'type'        => 'string',
 337                      'context'     => array( 'view', 'edit', 'embed' ),
 338                      'readonly'    => true,
 339                  ),
 340              ),
 341          );
 342  
 343          $this->schema = $schema;
 344  
 345          return $this->add_additional_fields_schema( $this->schema );
 346      }
 347  
 348      /**
 349       * Retrieves the query params for collections.
 350       *
 351       * @since 4.7.0
 352       *
 353       * @return array Collection parameters.
 354       */
 355  	public function get_collection_params() {
 356          return array(
 357              'context' => $this->get_context_param( array( 'default' => 'view' ) ),
 358          );
 359      }
 360  
 361  }


Generated: Thu Oct 28 01:00:03 2021 Cross-referenced by PHPXref 0.7.1