|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * REST API: WP_REST_Comment_Types_Controller class |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage REST_API |
| 7 | + * @since 7.1.0 |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Core class to access comment types via the REST API. |
| 12 | + * |
| 13 | + * @since 7.1.0 |
| 14 | + * |
| 15 | + * @see WP_REST_Controller |
| 16 | + */ |
| 17 | +class WP_REST_Comment_Types_Controller extends WP_REST_Controller { |
| 18 | + |
| 19 | + /** |
| 20 | + * Constructor. |
| 21 | + * |
| 22 | + * @since 7.1.0 |
| 23 | + */ |
| 24 | + public function __construct() { |
| 25 | + $this->namespace = 'wp/v2'; |
| 26 | + $this->rest_base = 'comment-types'; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Registers the routes for comment types. |
| 31 | + * |
| 32 | + * @since 7.1.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 comment type.' ), |
| 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 comment types. |
| 77 | + * |
| 78 | + * @since 7.1.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'] && ! current_user_can( 'moderate_comments' ) ) { |
| 85 | + return new WP_Error( |
| 86 | + 'rest_cannot_view', |
| 87 | + __( 'Sorry, you are not allowed to manage comments.' ), |
| 88 | + array( 'status' => rest_authorization_required_code() ) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Retrieves all public comment types. |
| 97 | + * |
| 98 | + * @since 7.1.0 |
| 99 | + * |
| 100 | + * @param WP_REST_Request $request Full details about the request. |
| 101 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 102 | + */ |
| 103 | + public function get_items( $request ) { |
| 104 | + if ( $request->is_method( 'HEAD' ) ) { |
| 105 | + // Return early as this handler doesn't add any response headers. |
| 106 | + return new WP_REST_Response( array() ); |
| 107 | + } |
| 108 | + |
| 109 | + $data = array(); |
| 110 | + $types = get_comment_types( array( 'show_in_rest' => true ), 'objects' ); |
| 111 | + |
| 112 | + foreach ( $types as $type ) { |
| 113 | + $comment_type = $this->prepare_item_for_response( $type, $request ); |
| 114 | + $data[ $type->name ] = $this->prepare_response_for_collection( $comment_type ); |
| 115 | + } |
| 116 | + |
| 117 | + return rest_ensure_response( $data ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Checks if a given request has access to read a comment type. |
| 122 | + * |
| 123 | + * @since 7.1.0 |
| 124 | + * |
| 125 | + * @param WP_REST_Request $request Full details about the request. |
| 126 | + * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. |
| 127 | + */ |
| 128 | + public function get_item_permissions_check( $request ) { |
| 129 | + if ( 'edit' === $request['context'] && ! current_user_can( 'moderate_comments' ) ) { |
| 130 | + return new WP_Error( |
| 131 | + 'rest_forbidden_context', |
| 132 | + __( 'Sorry, you are not allowed to manage comments.' ), |
| 133 | + array( 'status' => rest_authorization_required_code() ) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Retrieves a specific comment type. |
| 142 | + * |
| 143 | + * @since 7.1.0 |
| 144 | + * |
| 145 | + * @param WP_REST_Request $request Full details about the request. |
| 146 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 147 | + */ |
| 148 | + public function get_item( $request ) { |
| 149 | + $obj = get_comment_type_object( $request['type'] ); |
| 150 | + |
| 151 | + if ( empty( $obj ) ) { |
| 152 | + return new WP_Error( |
| 153 | + 'rest_type_invalid', |
| 154 | + __( 'Invalid comment type.' ), |
| 155 | + array( 'status' => 404 ) |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + if ( empty( $obj->show_in_rest ) ) { |
| 160 | + return new WP_Error( |
| 161 | + 'rest_cannot_read_type', |
| 162 | + __( 'Cannot view comment type.' ), |
| 163 | + array( 'status' => rest_authorization_required_code() ) |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + $data = $this->prepare_item_for_response( $obj, $request ); |
| 168 | + |
| 169 | + return rest_ensure_response( $data ); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Prepares a comment type object for serialization. |
| 174 | + * |
| 175 | + * @since 7.1.0 |
| 176 | + * |
| 177 | + * @param WP_Comment_Type $item Comment type object. |
| 178 | + * @param WP_REST_Request $request Full details about the request. |
| 179 | + * @return WP_REST_Response Response object. |
| 180 | + */ |
| 181 | + public function prepare_item_for_response( $item, $request ) { |
| 182 | + // Restores the more descriptive, specific name for use within this method. |
| 183 | + $comment_type = $item; |
| 184 | + |
| 185 | + // Don't prepare the response body for HEAD requests. |
| 186 | + if ( $request->is_method( 'HEAD' ) ) { |
| 187 | + /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-comment-types-controller.php */ |
| 188 | + return apply_filters( 'rest_prepare_comment_type', new WP_REST_Response( array() ), $comment_type, $request ); |
| 189 | + } |
| 190 | + |
| 191 | + $fields = $this->get_fields_for_response( $request ); |
| 192 | + $data = array(); |
| 193 | + |
| 194 | + if ( rest_is_field_included( 'description', $fields ) ) { |
| 195 | + $data['description'] = $comment_type->description; |
| 196 | + } |
| 197 | + |
| 198 | + if ( rest_is_field_included( 'labels', $fields ) ) { |
| 199 | + $data['labels'] = $comment_type->labels; |
| 200 | + } |
| 201 | + |
| 202 | + if ( rest_is_field_included( 'name', $fields ) ) { |
| 203 | + $data['name'] = $comment_type->label; |
| 204 | + } |
| 205 | + |
| 206 | + if ( rest_is_field_included( 'slug', $fields ) ) { |
| 207 | + $data['slug'] = $comment_type->name; |
| 208 | + } |
| 209 | + |
| 210 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 211 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
| 212 | + $data = $this->filter_response_by_context( $data, $context ); |
| 213 | + |
| 214 | + // Wrap the data in a response object. |
| 215 | + $response = rest_ensure_response( $data ); |
| 216 | + |
| 217 | + if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { |
| 218 | + $response->add_links( $this->prepare_links( $comment_type ) ); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Filters a comment type returned from the REST API. |
| 223 | + * |
| 224 | + * Allows modification of the comment type data right before it is returned. |
| 225 | + * |
| 226 | + * @since 7.1.0 |
| 227 | + * |
| 228 | + * @param WP_REST_Response $response The response object. |
| 229 | + * @param WP_Comment_Type $comment_type The original comment type object. |
| 230 | + * @param WP_REST_Request $request Request used to generate the response. |
| 231 | + */ |
| 232 | + return apply_filters( 'rest_prepare_comment_type', $response, $comment_type, $request ); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Prepares links for the request. |
| 237 | + * |
| 238 | + * @since 7.1.0 |
| 239 | + * |
| 240 | + * @param WP_Comment_Type $comment_type The comment type. |
| 241 | + * @return array Links for the given comment type. |
| 242 | + */ |
| 243 | + protected function prepare_links( $comment_type ) { |
| 244 | + return array( |
| 245 | + 'collection' => array( |
| 246 | + 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
| 247 | + ), |
| 248 | + 'https://api.w.org/items' => array( |
| 249 | + 'href' => add_query_arg( 'type', $comment_type->name, rest_url( 'wp/v2/comments' ) ), |
| 250 | + ), |
| 251 | + ); |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Retrieves the comment type's schema, conforming to JSON Schema. |
| 256 | + * |
| 257 | + * @since 7.1.0 |
| 258 | + * |
| 259 | + * @return array Item schema data. |
| 260 | + */ |
| 261 | + public function get_item_schema() { |
| 262 | + if ( $this->schema ) { |
| 263 | + return $this->add_additional_fields_schema( $this->schema ); |
| 264 | + } |
| 265 | + |
| 266 | + $schema = array( |
| 267 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 268 | + 'title' => 'comment-type', |
| 269 | + 'type' => 'object', |
| 270 | + 'properties' => array( |
| 271 | + 'description' => array( |
| 272 | + 'description' => __( 'A human-readable description of the comment type.' ), |
| 273 | + 'type' => 'string', |
| 274 | + 'context' => array( 'view', 'edit', 'embed' ), |
| 275 | + 'readonly' => true, |
| 276 | + ), |
| 277 | + 'labels' => array( |
| 278 | + 'description' => __( 'Human-readable labels for the comment type for various contexts.' ), |
| 279 | + 'type' => 'object', |
| 280 | + 'context' => array( 'edit' ), |
| 281 | + 'readonly' => true, |
| 282 | + ), |
| 283 | + 'name' => array( |
| 284 | + 'description' => __( 'The title for the comment type.' ), |
| 285 | + 'type' => 'string', |
| 286 | + 'context' => array( 'view', 'edit', 'embed' ), |
| 287 | + 'readonly' => true, |
| 288 | + ), |
| 289 | + 'slug' => array( |
| 290 | + 'description' => __( 'An alphanumeric identifier for the comment type.' ), |
| 291 | + 'type' => 'string', |
| 292 | + 'context' => array( 'view', 'edit', 'embed' ), |
| 293 | + 'readonly' => true, |
| 294 | + ), |
| 295 | + ), |
| 296 | + ); |
| 297 | + |
| 298 | + $this->schema = $schema; |
| 299 | + |
| 300 | + return $this->add_additional_fields_schema( $this->schema ); |
| 301 | + } |
| 302 | + |
| 303 | + /** |
| 304 | + * Retrieves the query params for collections. |
| 305 | + * |
| 306 | + * @since 7.1.0 |
| 307 | + * |
| 308 | + * @return array Collection parameters. |
| 309 | + */ |
| 310 | + public function get_collection_params() { |
| 311 | + return array( |
| 312 | + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 313 | + ); |
| 314 | + } |
| 315 | +} |
0 commit comments