Changeset 39024
- Timestamp:
- 10/30/2016 04:43:12 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php
r38832 r39024 1 1 <?php 2 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 */ 3 17 class WP_REST_Post_Statuses_Controller extends WP_REST_Controller { 4 18 19 /** 20 * Constructor. 21 * 22 * @since 4.7.0 23 * @access public 24 */ 5 25 public function __construct() { 6 26 $this->namespace = 'wp/v2'; … … 9 29 10 30 /** 11 * Register the routes for the objects of the controller. 31 * Registers the routes for the objects of the controller. 32 * 33 * @since 4.7.0 34 * @access public 35 * 36 * @see register_rest_route() 12 37 */ 13 38 public function register_routes() { … … 15 40 register_rest_route( $this->namespace, '/' . $this->rest_base, array( 16 41 array( 17 'methods' => WP_REST_Server::READABLE,18 'callback' => array( $this, 'get_items' ),42 'methods' => WP_REST_Server::READABLE, 43 'callback' => array( $this, 'get_items' ), 19 44 'permission_callback' => array( $this, 'get_items_permissions_check' ), 20 'args' => $this->get_collection_params(),45 'args' => $this->get_collection_params(), 21 46 ), 22 47 'schema' => array( $this, 'get_public_item_schema' ), … … 25 50 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<status>[\w-]+)', array( 26 51 array( 27 'methods' => WP_REST_Server::READABLE,28 'callback' => array( $this, 'get_item' ),52 'methods' => WP_REST_Server::READABLE, 53 'callback' => array( $this, 'get_item' ), 29 54 'permission_callback' => array( $this, 'get_item_permissions_check' ), 30 'args' => array(31 'context' 55 'args' => array( 56 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 32 57 ), 33 58 ), … … 37 62 38 63 /** 39 * Check whether a given request has permission to read post statuses. 40 * 41 * @param WP_REST_Request $request Full details about the request. 42 * @return WP_Error|boolean 64 * Checks whether a given request has permission to read post statuses. 65 * 66 * @since 4.7.0 67 * @access public 68 * 69 * @param WP_REST_Request $request Full details about the request. 70 * @return WP_Error|bool True if the request has read access, WP_Error object otherwise. 43 71 */ 44 72 public function get_items_permissions_check( $request ) { 45 73 if ( 'edit' === $request['context'] ) { 46 74 $types = get_post_types( array( 'show_in_rest' => true ), 'objects' ); 75 47 76 foreach ( $types as $type ) { 48 77 if ( current_user_can( $type->cap->edit_posts ) ) { … … 52 81 return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot view this resource with edit context.' ), array( 'status' => rest_authorization_required_code() ) ); 53 82 } 83 54 84 return true; 55 85 } 56 86 57 87 /** 58 * Get all post statuses, depending on user context 59 * 60 * @param WP_REST_Request $request 61 * @return array|WP_Error 88 * Retrieves all post statuses, depending on user context. 89 * 90 * @since 4.7.0 91 * @access public 92 * 93 * @param WP_REST_Request $request Full details about the request. 94 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 62 95 */ 63 96 public function get_items( $request ) { … … 65 98 $statuses = get_post_stati( array( 'internal' => false ), 'object' ); 66 99 $statuses['trash'] = get_post_status_object( 'trash' ); 100 67 101 foreach ( $statuses as $slug => $obj ) { 68 102 $ret = $this->check_read_permission( $obj ); 103 69 104 if ( ! $ret ) { 70 105 continue; 71 106 } 107 72 108 $status = $this->prepare_item_for_response( $obj, $request ); 73 109 $data[ $obj->name ] = $this->prepare_response_for_collection( $status ); 74 110 } 111 75 112 return rest_ensure_response( $data ); 76 113 } 77 114 78 115 /** 79 * Check if a given request has access to read a post status. 80 * 81 * @param WP_REST_Request $request Full details about the request. 82 * @return WP_Error|boolean 116 * Checks if a given request has access to read a post status. 117 * 118 * @since 4.7.0 119 * @access public 120 * 121 * @param WP_REST_Request $request Full details about the request. 122 * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise. 83 123 */ 84 124 public function get_item_permissions_check( $request ) { 85 125 $status = get_post_status_object( $request['status'] ); 126 86 127 if ( empty( $status ) ) { 87 128 return new WP_Error( 'rest_status_invalid', __( 'Invalid resource.' ), array( 'status' => 404 ) ); 88 129 } 130 89 131 $check = $this->check_read_permission( $status ); 132 90 133 if ( ! $check ) { 91 134 return new WP_Error( 'rest_cannot_read_status', __( 'Cannot view resource.' ), array( 'status' => rest_authorization_required_code() ) ); 92 135 } 136 93 137 return true; 94 138 } 95 139 96 140 /** 97 * Check whether a given post status should be visible 98 * 99 * @param object $status 100 * @return boolean 141 * Checks whether a given post status should be visible. 142 * 143 * @since 4.7.0 144 * @access protected 145 * 146 * @param object $status Post status. 147 * @return bool True if the post status is visible, otherwise false. 101 148 */ 102 149 protected function check_read_permission( $status ) { … … 104 151 return true; 105 152 } 153 106 154 if ( false === $status->internal || 'trash' === $status->name ) { 107 155 $types = get_post_types( array( 'show_in_rest' => true ), 'objects' ); 156 108 157 foreach ( $types as $type ) { 109 158 if ( current_user_can( $type->cap->edit_posts ) ) { … … 112 161 } 113 162 } 163 114 164 return false; 115 165 } 116 166 117 167 /** 118 * Get a specific post status 119 * 120 * @param WP_REST_Request $request 121 * @return array|WP_Error 168 * Retrieves a specific post status. 169 * 170 * @since 4.7.0 171 * @access public 172 * 173 * @param WP_REST_Request $request Full details about the request. 174 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 122 175 */ 123 176 public function get_item( $request ) { 124 177 $obj = get_post_status_object( $request['status'] ); 178 125 179 if ( empty( $obj ) ) { 126 180 return new WP_Error( 'rest_status_invalid', __( 'Invalid resource.' ), array( 'status' => 404 ) ); 127 181 } 182 128 183 $data = $this->prepare_item_for_response( $obj, $request ); 184 129 185 return rest_ensure_response( $data ); 130 186 } 131 187 132 188 /** 133 * Prepare a post status object for serialization 134 * 135 * @param stdClass $status Post status data 136 * @param WP_REST_Request $request 137 * @return WP_REST_Response Post status data 189 * Prepares a post status object for serialization. 190 * 191 * @since 4.7.0 192 * @access public 193 * 194 * @param stdClass $status Post status data. 195 * @param WP_REST_Request $request Full details about the request. 196 * @return WP_REST_Response Post status data. 138 197 */ 139 198 public function prepare_item_for_response( $status, $request ) { … … 162 221 163 222 /** 164 * Filter a status returned from theAPI.223 * Filters a status returned from the REST API. 165 224 * 166 225 * Allows modification of the status data right before it is returned. 167 226 * 168 * @param WP_REST_Response $response The response object. 169 * @param object $status The original status object. 170 * @param WP_REST_Request $request Request used to generate the response. 227 * @since 4.7.0 228 * 229 * @param WP_REST_Response $response The response object. 230 * @param object $status The original status object. 231 * @param WP_REST_Request $request Request used to generate the response. 171 232 */ 172 233 return apply_filters( 'rest_prepare_status', $response, $status, $request ); … … 174 235 175 236 /** 176 * Get the Post status' schema, conforming to JSON Schema 177 * 178 * @return array 237 * Retrieves the post status' schema, conforming to JSON Schema. 238 * 239 * @since 4.7.0 240 * @access public 241 * 242 * @return array Item schema data. 179 243 */ 180 244 public function get_item_schema() { … … 228 292 ), 229 293 ); 294 230 295 return $this->add_additional_fields_schema( $schema ); 231 296 } 232 297 233 298 /** 234 * Get the query params for collections 235 * 236 * @return array 299 * Retrieves the query params for collections. 300 * 301 * @since 4.7.0 302 * @access public 303 * 304 * @return array Collection parameters. 237 305 */ 238 306 public function get_collection_params() { 239 307 return array( 240 'context' 308 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 241 309 ); 242 310 }
Note: See TracChangeset
for help on using the changeset viewer.