[ Index ]

PHP Cross Reference of WordPress

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


Generated: Wed Jan 22 01:00:03 2020 Cross-referenced by PHPXref 0.7.1