[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * REST API: WP_REST_Post_Statuses_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 4.7.0
   8   */
   9  
  10  /**
  11   * Core class used to access post statuses via the REST API.
  12   *
  13   * @since 4.7.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Post_Statuses_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 = 'statuses';
  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<status>[\w-]+)',
  55              array(
  56                  'args'   => array(
  57                      'status' => array(
  58                          'description' => __( 'An alphanumeric identifier for the status.' ),
  59                          'type'        => 'string',
  60                      ),
  61                  ),
  62                  array(
  63                      'methods'             => WP_REST_Server::READABLE,
  64                      'callback'            => array( $this, 'get_item' ),
  65                      'permission_callback' => array( $this, 'get_item_permissions_check' ),
  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 post statuses.
  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              return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage post statuses.' ), array( 'status' => rest_authorization_required_code() ) );
  93          }
  94  
  95          return true;
  96      }
  97  
  98      /**
  99       * Retrieves all post statuses, depending on user context.
 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          $statuses          = get_post_stati( array( 'internal' => false ), 'object' );
 109          $statuses['trash'] = get_post_status_object( 'trash' );
 110  
 111          foreach ( $statuses as $slug => $obj ) {
 112              $ret = $this->check_read_permission( $obj );
 113  
 114              if ( ! $ret ) {
 115                  continue;
 116              }
 117  
 118              $status             = $this->prepare_item_for_response( $obj, $request );
 119              $data[ $obj->name ] = $this->prepare_response_for_collection( $status );
 120          }
 121  
 122          return rest_ensure_response( $data );
 123      }
 124  
 125      /**
 126       * Checks if a given request has access to read a post status.
 127       *
 128       * @since 4.7.0
 129       *
 130       * @param WP_REST_Request $request Full details about the request.
 131       * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
 132       */
 133  	public function get_item_permissions_check( $request ) {
 134          $status = get_post_status_object( $request['status'] );
 135  
 136          if ( empty( $status ) ) {
 137              return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) );
 138          }
 139  
 140          $check = $this->check_read_permission( $status );
 141  
 142          if ( ! $check ) {
 143              return new WP_Error( 'rest_cannot_read_status', __( 'Cannot view status.' ), array( 'status' => rest_authorization_required_code() ) );
 144          }
 145  
 146          return true;
 147      }
 148  
 149      /**
 150       * Checks whether a given post status should be visible.
 151       *
 152       * @since 4.7.0
 153       *
 154       * @param object $status Post status.
 155       * @return bool True if the post status is visible, otherwise false.
 156       */
 157  	protected function check_read_permission( $status ) {
 158          if ( true === $status->public ) {
 159              return true;
 160          }
 161  
 162          if ( false === $status->internal || 'trash' === $status->name ) {
 163              $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
 164  
 165              foreach ( $types as $type ) {
 166                  if ( current_user_can( $type->cap->edit_posts ) ) {
 167                      return true;
 168                  }
 169              }
 170          }
 171  
 172          return false;
 173      }
 174  
 175      /**
 176       * Retrieves a specific post status.
 177       *
 178       * @since 4.7.0
 179       *
 180       * @param WP_REST_Request $request Full details about the request.
 181       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 182       */
 183  	public function get_item( $request ) {
 184          $obj = get_post_status_object( $request['status'] );
 185  
 186          if ( empty( $obj ) ) {
 187              return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) );
 188          }
 189  
 190          $data = $this->prepare_item_for_response( $obj, $request );
 191  
 192          return rest_ensure_response( $data );
 193      }
 194  
 195      /**
 196       * Prepares a post status object for serialization.
 197       *
 198       * @since 4.7.0
 199       *
 200       * @param stdClass        $status  Post status data.
 201       * @param WP_REST_Request $request Full details about the request.
 202       * @return WP_REST_Response Post status data.
 203       */
 204  	public function prepare_item_for_response( $status, $request ) {
 205  
 206          $fields = $this->get_fields_for_response( $request );
 207          $data   = array();
 208  
 209          if ( in_array( 'name', $fields, true ) ) {
 210              $data['name'] = $status->label;
 211          }
 212  
 213          if ( in_array( 'private', $fields, true ) ) {
 214              $data['private'] = (bool) $status->private;
 215          }
 216  
 217          if ( in_array( 'protected', $fields, true ) ) {
 218              $data['protected'] = (bool) $status->protected;
 219          }
 220  
 221          if ( in_array( 'public', $fields, true ) ) {
 222              $data['public'] = (bool) $status->public;
 223          }
 224  
 225          if ( in_array( 'queryable', $fields, true ) ) {
 226              $data['queryable'] = (bool) $status->publicly_queryable;
 227          }
 228  
 229          if ( in_array( 'show_in_list', $fields, true ) ) {
 230              $data['show_in_list'] = (bool) $status->show_in_admin_all_list;
 231          }
 232  
 233          if ( in_array( 'slug', $fields, true ) ) {
 234              $data['slug'] = $status->name;
 235          }
 236  
 237          if ( in_array( 'date_floating', $fields, true ) ) {
 238              $data['date_floating'] = $status->date_floating;
 239          }
 240  
 241          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 242          $data    = $this->add_additional_fields_to_object( $data, $request );
 243          $data    = $this->filter_response_by_context( $data, $context );
 244  
 245          $response = rest_ensure_response( $data );
 246  
 247          if ( 'publish' === $status->name ) {
 248              $response->add_link( 'archives', rest_url( 'wp/v2/posts' ) );
 249          } else {
 250              $response->add_link( 'archives', add_query_arg( 'status', $status->name, rest_url( 'wp/v2/posts' ) ) );
 251          }
 252  
 253          /**
 254           * Filters a status returned from the REST API.
 255           *
 256           * Allows modification of the status data right before it is returned.
 257           *
 258           * @since 4.7.0
 259           *
 260           * @param WP_REST_Response $response The response object.
 261           * @param object           $status   The original status object.
 262           * @param WP_REST_Request  $request  Request used to generate the response.
 263           */
 264          return apply_filters( 'rest_prepare_status', $response, $status, $request );
 265      }
 266  
 267      /**
 268       * Retrieves the post status' schema, conforming to JSON Schema.
 269       *
 270       * @since 4.7.0
 271       *
 272       * @return array Item schema data.
 273       */
 274  	public function get_item_schema() {
 275          if ( $this->schema ) {
 276              return $this->add_additional_fields_schema( $this->schema );
 277          }
 278  
 279          $schema = array(
 280              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 281              'title'      => 'status',
 282              'type'       => 'object',
 283              'properties' => array(
 284                  'name'          => array(
 285                      'description' => __( 'The title for the status.' ),
 286                      'type'        => 'string',
 287                      'context'     => array( 'embed', 'view', 'edit' ),
 288                      'readonly'    => true,
 289                  ),
 290                  'private'       => array(
 291                      'description' => __( 'Whether posts with this status should be private.' ),
 292                      'type'        => 'boolean',
 293                      'context'     => array( 'edit' ),
 294                      'readonly'    => true,
 295                  ),
 296                  'protected'     => array(
 297                      'description' => __( 'Whether posts with this status should be protected.' ),
 298                      'type'        => 'boolean',
 299                      'context'     => array( 'edit' ),
 300                      'readonly'    => true,
 301                  ),
 302                  'public'        => array(
 303                      'description' => __( 'Whether posts of this status should be shown in the front end of the site.' ),
 304                      'type'        => 'boolean',
 305                      'context'     => array( 'view', 'edit' ),
 306                      'readonly'    => true,
 307                  ),
 308                  'queryable'     => array(
 309                      'description' => __( 'Whether posts with this status should be publicly-queryable.' ),
 310                      'type'        => 'boolean',
 311                      'context'     => array( 'view', 'edit' ),
 312                      'readonly'    => true,
 313                  ),
 314                  'show_in_list'  => array(
 315                      'description' => __( 'Whether to include posts in the edit listing for their post type.' ),
 316                      'type'        => 'boolean',
 317                      'context'     => array( 'edit' ),
 318                      'readonly'    => true,
 319                  ),
 320                  'slug'          => array(
 321                      'description' => __( 'An alphanumeric identifier for the status.' ),
 322                      'type'        => 'string',
 323                      'context'     => array( 'embed', 'view', 'edit' ),
 324                      'readonly'    => true,
 325                  ),
 326                  'date_floating' => array(
 327                      'description' => __( 'Whether posts of this status may have floating published dates.' ),
 328                      'type'        => 'boolean',
 329                      'context'     => array( 'view', 'edit' ),
 330                      'readonly'    => true,
 331                  ),
 332              ),
 333          );
 334  
 335          $this->schema = $schema;
 336          return $this->add_additional_fields_schema( $this->schema );
 337      }
 338  
 339      /**
 340       * Retrieves the query params for collections.
 341       *
 342       * @since 4.7.0
 343       *
 344       * @return array Collection parameters.
 345       */
 346  	public function get_collection_params() {
 347          return array(
 348              'context' => $this->get_context_param( array( 'default' => 'view' ) ),
 349          );
 350      }
 351  
 352  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7