[ 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          $namespace  = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
 184          $supports   = get_all_post_type_supports( $post_type->name );
 185  
 186          $fields = $this->get_fields_for_response( $request );
 187          $data   = array();
 188  
 189          if ( in_array( 'capabilities', $fields, true ) ) {
 190              $data['capabilities'] = $post_type->cap;
 191          }
 192  
 193          if ( in_array( 'description', $fields, true ) ) {
 194              $data['description'] = $post_type->description;
 195          }
 196  
 197          if ( in_array( 'hierarchical', $fields, true ) ) {
 198              $data['hierarchical'] = $post_type->hierarchical;
 199          }
 200  
 201          if ( in_array( 'visibility', $fields, true ) ) {
 202              $data['visibility'] = array(
 203                  'show_in_nav_menus' => (bool) $post_type->show_in_nav_menus,
 204                  'show_ui'           => (bool) $post_type->show_ui,
 205              );
 206          }
 207  
 208          if ( in_array( 'viewable', $fields, true ) ) {
 209              $data['viewable'] = is_post_type_viewable( $post_type );
 210          }
 211  
 212          if ( in_array( 'labels', $fields, true ) ) {
 213              $data['labels'] = $post_type->labels;
 214          }
 215  
 216          if ( in_array( 'name', $fields, true ) ) {
 217              $data['name'] = $post_type->label;
 218          }
 219  
 220          if ( in_array( 'slug', $fields, true ) ) {
 221              $data['slug'] = $post_type->name;
 222          }
 223  
 224          if ( in_array( 'supports', $fields, true ) ) {
 225              $data['supports'] = $supports;
 226          }
 227  
 228          if ( in_array( 'taxonomies', $fields, true ) ) {
 229              $data['taxonomies'] = array_values( $taxonomies );
 230          }
 231  
 232          if ( in_array( 'rest_base', $fields, true ) ) {
 233              $data['rest_base'] = $base;
 234          }
 235  
 236          if ( in_array( 'rest_namespace', $fields, true ) ) {
 237              $data['rest_namespace'] = $namespace;
 238          }
 239  
 240          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 241          $data    = $this->add_additional_fields_to_object( $data, $request );
 242          $data    = $this->filter_response_by_context( $data, $context );
 243  
 244          // Wrap the data in a response object.
 245          $response = rest_ensure_response( $data );
 246  
 247          $response->add_links(
 248              array(
 249                  'collection'              => array(
 250                      'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
 251                  ),
 252                  'https://api.w.org/items' => array(
 253                      'href' => rest_url( rest_get_route_for_post_type_items( $post_type->name ) ),
 254                  ),
 255              )
 256          );
 257  
 258          /**
 259           * Filters a post type returned from the REST API.
 260           *
 261           * Allows modification of the post type data right before it is returned.
 262           *
 263           * @since 4.7.0
 264           *
 265           * @param WP_REST_Response $response  The response object.
 266           * @param WP_Post_Type     $post_type The original post type object.
 267           * @param WP_REST_Request  $request   Request used to generate the response.
 268           */
 269          return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request );
 270      }
 271  
 272      /**
 273       * Retrieves the post type's schema, conforming to JSON Schema.
 274       *
 275       * @since 4.7.0
 276       * @since 4.8.0 The `supports` property was added.
 277       * @since 5.9.0 The `visibility` and `rest_namespace` properties were added.
 278       *
 279       * @return array Item schema data.
 280       */
 281  	public function get_item_schema() {
 282          if ( $this->schema ) {
 283              return $this->add_additional_fields_schema( $this->schema );
 284          }
 285  
 286          $schema = array(
 287              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 288              'title'      => 'type',
 289              'type'       => 'object',
 290              'properties' => array(
 291                  'capabilities'   => array(
 292                      'description' => __( 'All capabilities used by the post type.' ),
 293                      'type'        => 'object',
 294                      'context'     => array( 'edit' ),
 295                      'readonly'    => true,
 296                  ),
 297                  'description'    => array(
 298                      'description' => __( 'A human-readable description of the post type.' ),
 299                      'type'        => 'string',
 300                      'context'     => array( 'view', 'edit' ),
 301                      'readonly'    => true,
 302                  ),
 303                  'hierarchical'   => array(
 304                      'description' => __( 'Whether or not the post type should have children.' ),
 305                      'type'        => 'boolean',
 306                      'context'     => array( 'view', 'edit' ),
 307                      'readonly'    => true,
 308                  ),
 309                  'viewable'       => array(
 310                      'description' => __( 'Whether or not the post type can be viewed.' ),
 311                      'type'        => 'boolean',
 312                      'context'     => array( 'edit' ),
 313                      'readonly'    => true,
 314                  ),
 315                  'labels'         => array(
 316                      'description' => __( 'Human-readable labels for the post type for various contexts.' ),
 317                      'type'        => 'object',
 318                      'context'     => array( 'edit' ),
 319                      'readonly'    => true,
 320                  ),
 321                  'name'           => array(
 322                      'description' => __( 'The title for the post type.' ),
 323                      'type'        => 'string',
 324                      'context'     => array( 'view', 'edit', 'embed' ),
 325                      'readonly'    => true,
 326                  ),
 327                  'slug'           => array(
 328                      'description' => __( 'An alphanumeric identifier for the post type.' ),
 329                      'type'        => 'string',
 330                      'context'     => array( 'view', 'edit', 'embed' ),
 331                      'readonly'    => true,
 332                  ),
 333                  'supports'       => array(
 334                      'description' => __( 'All features, supported by the post type.' ),
 335                      'type'        => 'object',
 336                      'context'     => array( 'edit' ),
 337                      'readonly'    => true,
 338                  ),
 339                  'taxonomies'     => array(
 340                      'description' => __( 'Taxonomies associated with post type.' ),
 341                      'type'        => 'array',
 342                      'items'       => array(
 343                          'type' => 'string',
 344                      ),
 345                      'context'     => array( 'view', 'edit' ),
 346                      'readonly'    => true,
 347                  ),
 348                  'rest_base'      => array(
 349                      'description' => __( 'REST base route for the post type.' ),
 350                      'type'        => 'string',
 351                      'context'     => array( 'view', 'edit', 'embed' ),
 352                      'readonly'    => true,
 353                  ),
 354                  'rest_namespace' => array(
 355                      'description' => __( 'REST route\'s namespace for the post type.' ),
 356                      'type'        => 'string',
 357                      'context'     => array( 'view', 'edit', 'embed' ),
 358                      'readonly'    => true,
 359                  ),
 360                  'visibility'     => array(
 361                      'description' => __( 'The visibility settings for the post type.' ),
 362                      'type'        => 'object',
 363                      'context'     => array( 'edit' ),
 364                      'readonly'    => true,
 365                      'properties'  => array(
 366                          'show_ui'           => array(
 367                              'description' => __( 'Whether to generate a default UI for managing this post type.' ),
 368                              'type'        => 'boolean',
 369                          ),
 370                          'show_in_nav_menus' => array(
 371                              'description' => __( 'Whether to make the post type available for selection in navigation menus.' ),
 372                              'type'        => 'boolean',
 373                          ),
 374                      ),
 375                  ),
 376              ),
 377          );
 378  
 379          $this->schema = $schema;
 380  
 381          return $this->add_additional_fields_schema( $this->schema );
 382      }
 383  
 384      /**
 385       * Retrieves the query params for collections.
 386       *
 387       * @since 4.7.0
 388       *
 389       * @return array Collection parameters.
 390       */
 391  	public function get_collection_params() {
 392          return array(
 393              'context' => $this->get_context_param( array( 'default' => 'view' ) ),
 394          );
 395      }
 396  
 397  }


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1