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


Generated: Mon Jan 27 01:00:03 2020 Cross-referenced by PHPXref 0.7.1