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