[ 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 the objects of the controller.
  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                      'args'     => array(
  66                          'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  67                      ),
  68                  ),
  69                  'schema' => array( $this, 'get_public_item_schema' ),
  70              )
  71          );
  72      }
  73  
  74      /**
  75       * Checks whether a given request has permission to read types.
  76       *
  77       * @since 4.7.0
  78       *
  79       * @param WP_REST_Request $request Full details about the request.
  80       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  81       */
  82  	public function get_items_permissions_check( $request ) {
  83          if ( 'edit' === $request['context'] ) {
  84              $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
  85  
  86              foreach ( $types as $type ) {
  87                  if ( current_user_can( $type->cap->edit_posts ) ) {
  88                      return true;
  89                  }
  90              }
  91  
  92              return new WP_Error(
  93                  'rest_cannot_view',
  94                  __( 'Sorry, you are not allowed to edit posts in this post type.' ),
  95                  array( 'status' => rest_authorization_required_code() )
  96              );
  97          }
  98  
  99          return true;
 100      }
 101  
 102      /**
 103       * Retrieves all public post types.
 104       *
 105       * @since 4.7.0
 106       *
 107       * @param WP_REST_Request $request Full details about the request.
 108       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 109       */
 110  	public function get_items( $request ) {
 111          $data  = array();
 112          $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
 113  
 114          foreach ( $types as $type ) {
 115              if ( 'edit' === $request['context'] && ! current_user_can( $type->cap->edit_posts ) ) {
 116                  continue;
 117              }
 118  
 119              $post_type           = $this->prepare_item_for_response( $type, $request );
 120              $data[ $type->name ] = $this->prepare_response_for_collection( $post_type );
 121          }
 122  
 123          return rest_ensure_response( $data );
 124      }
 125  
 126      /**
 127       * Retrieves a specific post type.
 128       *
 129       * @since 4.7.0
 130       *
 131       * @param WP_REST_Request $request Full details about the request.
 132       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 133       */
 134  	public function get_item( $request ) {
 135          $obj = get_post_type_object( $request['type'] );
 136  
 137          if ( empty( $obj ) ) {
 138              return new WP_Error(
 139                  'rest_type_invalid',
 140                  __( 'Invalid post type.' ),
 141                  array( 'status' => 404 )
 142              );
 143          }
 144  
 145          if ( empty( $obj->show_in_rest ) ) {
 146              return new WP_Error(
 147                  'rest_cannot_read_type',
 148                  __( 'Cannot view post type.' ),
 149                  array( 'status' => rest_authorization_required_code() )
 150              );
 151          }
 152  
 153          if ( 'edit' === $request['context'] && ! current_user_can( $obj->cap->edit_posts ) ) {
 154              return new WP_Error(
 155                  'rest_forbidden_context',
 156                  __( 'Sorry, you are not allowed to edit posts in this post type.' ),
 157                  array( 'status' => rest_authorization_required_code() )
 158              );
 159          }
 160  
 161          $data = $this->prepare_item_for_response( $obj, $request );
 162  
 163          return rest_ensure_response( $data );
 164      }
 165  
 166      /**
 167       * Prepares a post type object for serialization.
 168       *
 169       * @since 4.7.0
 170       *
 171       * @param WP_Post_Type    $post_type Post type object.
 172       * @param WP_REST_Request $request   Full details about the request.
 173       * @return WP_REST_Response Response object.
 174       */
 175  	public function prepare_item_for_response( $post_type, $request ) {
 176          $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
 177          $taxonomies = wp_list_pluck( $taxonomies, 'name' );
 178          $base       = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
 179          $supports   = get_all_post_type_supports( $post_type->name );
 180  
 181          $fields = $this->get_fields_for_response( $request );
 182          $data   = array();
 183  
 184          if ( in_array( 'capabilities', $fields, true ) ) {
 185              $data['capabilities'] = $post_type->cap;
 186          }
 187  
 188          if ( in_array( 'description', $fields, true ) ) {
 189              $data['description'] = $post_type->description;
 190          }
 191  
 192          if ( in_array( 'hierarchical', $fields, true ) ) {
 193              $data['hierarchical'] = $post_type->hierarchical;
 194          }
 195  
 196          if ( in_array( 'viewable', $fields, true ) ) {
 197              $data['viewable'] = is_post_type_viewable( $post_type );
 198          }
 199  
 200          if ( in_array( 'labels', $fields, true ) ) {
 201              $data['labels'] = $post_type->labels;
 202          }
 203  
 204          if ( in_array( 'name', $fields, true ) ) {
 205              $data['name'] = $post_type->label;
 206          }
 207  
 208          if ( in_array( 'slug', $fields, true ) ) {
 209              $data['slug'] = $post_type->name;
 210          }
 211  
 212          if ( in_array( 'supports', $fields, true ) ) {
 213              $data['supports'] = $supports;
 214          }
 215  
 216          if ( in_array( 'taxonomies', $fields, true ) ) {
 217              $data['taxonomies'] = array_values( $taxonomies );
 218          }
 219  
 220          if ( in_array( 'rest_base', $fields, true ) ) {
 221              $data['rest_base'] = $base;
 222          }
 223  
 224          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 225          $data    = $this->add_additional_fields_to_object( $data, $request );
 226          $data    = $this->filter_response_by_context( $data, $context );
 227  
 228          // Wrap the data in a response object.
 229          $response = rest_ensure_response( $data );
 230  
 231          $response->add_links(
 232              array(
 233                  'collection'              => array(
 234                      'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
 235                  ),
 236                  'https://api.w.org/items' => array(
 237                      'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
 238                  ),
 239              )
 240          );
 241  
 242          /**
 243           * Filters a post type returned from the API.
 244           *
 245           * Allows modification of the post type data right before it is returned.
 246           *
 247           * @since 4.7.0
 248           *
 249           * @param WP_REST_Response $response  The response object.
 250           * @param WP_Post_Type     $post_type The original post type object.
 251           * @param WP_REST_Request  $request   Request used to generate the response.
 252           */
 253          return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request );
 254      }
 255  
 256      /**
 257       * Retrieves the post type's schema, conforming to JSON Schema.
 258       *
 259       * @since 4.7.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'      => 'type',
 271              'type'       => 'object',
 272              'properties' => array(
 273                  'capabilities' => array(
 274                      'description' => __( 'All capabilities used by the post type.' ),
 275                      'type'        => 'object',
 276                      'context'     => array( 'edit' ),
 277                      'readonly'    => true,
 278                  ),
 279                  'description'  => array(
 280                      'description' => __( 'A human-readable description of the post type.' ),
 281                      'type'        => 'string',
 282                      'context'     => array( 'view', 'edit' ),
 283                      'readonly'    => true,
 284                  ),
 285                  'hierarchical' => array(
 286                      'description' => __( 'Whether or not the post type should have children.' ),
 287                      'type'        => 'boolean',
 288                      'context'     => array( 'view', 'edit' ),
 289                      'readonly'    => true,
 290                  ),
 291                  'viewable'     => array(
 292                      'description' => __( 'Whether or not the post type can be viewed.' ),
 293                      'type'        => 'boolean',
 294                      'context'     => array( 'edit' ),
 295                      'readonly'    => true,
 296                  ),
 297                  'labels'       => array(
 298                      'description' => __( 'Human-readable labels for the post type for various contexts.' ),
 299                      'type'        => 'object',
 300                      'context'     => array( 'edit' ),
 301                      'readonly'    => true,
 302                  ),
 303                  'name'         => array(
 304                      'description' => __( 'The title for the post type.' ),
 305                      'type'        => 'string',
 306                      'context'     => array( 'view', 'edit', 'embed' ),
 307                      'readonly'    => true,
 308                  ),
 309                  'slug'         => array(
 310                      'description' => __( 'An alphanumeric identifier for the post type.' ),
 311                      'type'        => 'string',
 312                      'context'     => array( 'view', 'edit', 'embed' ),
 313                      'readonly'    => true,
 314                  ),
 315                  'supports'     => array(
 316                      'description' => __( 'All features, supported by the post type.' ),
 317                      'type'        => 'object',
 318                      'context'     => array( 'edit' ),
 319                      'readonly'    => true,
 320                  ),
 321                  'taxonomies'   => array(
 322                      'description' => __( 'Taxonomies associated with post type.' ),
 323                      'type'        => 'array',
 324                      'items'       => array(
 325                          'type' => 'string',
 326                      ),
 327                      'context'     => array( 'view', 'edit' ),
 328                      'readonly'    => true,
 329                  ),
 330                  'rest_base'    => array(
 331                      'description' => __( 'REST base route for the post type.' ),
 332                      'type'        => 'string',
 333                      'context'     => array( 'view', 'edit', 'embed' ),
 334                      'readonly'    => true,
 335                  ),
 336              ),
 337          );
 338  
 339          $this->schema = $schema;
 340  
 341          return $this->add_additional_fields_schema( $this->schema );
 342      }
 343  
 344      /**
 345       * Retrieves the query params for collections.
 346       *
 347       * @since 4.7.0
 348       *
 349       * @return array Collection parameters.
 350       */
 351  	public function get_collection_params() {
 352          return array(
 353              'context' => $this->get_context_param( array( 'default' => 'view' ) ),
 354          );
 355      }
 356  
 357  }


Generated: Mon Jul 6 01:00:03 2020 Cross-referenced by PHPXref 0.7.1