Skip to content

Commit a3c2f01

Browse files
Comments: Add a REST API endpoint for comment types.
Expose registered comment types through a read-only `/wp/v2/comment-types` controller, mirroring the post types controller (`/wp/v2/types`). This lets REST clients discover the registered types and their labels, which the block editor's inline-commenting work needs in order to add and query comments by type. Add a `show_in_rest` argument to `WP_Comment_Type` (defaulting to the value of `public`, as `show_ui` does) to gate which types the endpoint exposes. The built-in `comment`, `pingback`, and `trackback` types are public and therefore visible; the internal `note` type is not. Builds on the registration API in WordPress#12311. See #35214.
1 parent 81eceff commit a3c2f01

4 files changed

Lines changed: 343 additions & 6 deletions

File tree

‎src/wp-includes/class-wp-comment-type.php‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ final class WP_Comment_Type {
9494
*/
9595
public $show_ui;
9696

97+
/**
98+
* Whether to include this comment type in the REST API.
99+
*
100+
* Comment types with this enabled are exposed by the `/wp/v2/comment-types`
101+
* endpoint. Default is the value of $public.
102+
*
103+
* @since 7.1.0
104+
* @var bool
105+
*/
106+
public $show_in_rest;
107+
97108
/**
98109
* Whether this comment type is a native or "built-in" comment type.
99110
*
@@ -187,12 +198,13 @@ public function set_props( $args ) {
187198
* treated as a provided value and overwrite the default name with false.
188199
*/
189200
$defaults = array(
190-
'labels' => array(),
191-
'description' => '',
192-
'public' => true,
193-
'internal' => false,
194-
'show_ui' => null,
195-
'_builtin' => false,
201+
'labels' => array(),
202+
'description' => '',
203+
'public' => true,
204+
'internal' => false,
205+
'show_ui' => null,
206+
'show_in_rest' => null,
207+
'_builtin' => false,
196208
);
197209

198210
$args = array_merge( $defaults, $args );
@@ -202,6 +214,11 @@ public function set_props( $args ) {
202214
$args['show_ui'] = $args['public'];
203215
}
204216

217+
// If not set, default to the setting for 'public'.
218+
if ( null === $args['show_in_rest'] ) {
219+
$args['show_in_rest'] = $args['public'];
220+
}
221+
205222
$args['name'] = $this->name;
206223

207224
foreach ( $args as $property_name => $property_value ) {

‎src/wp-includes/rest-api.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ function create_initial_rest_routes() {
324324
$controller = new WP_REST_Comments_Controller();
325325
$controller->register_routes();
326326

327+
// Comment types.
328+
$controller = new WP_REST_Comment_Types_Controller();
329+
$controller->register_routes();
330+
327331
$search_handlers = array(
328332
new WP_REST_Post_Search_Handler(),
329333
new WP_REST_Term_Search_Handler(),
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
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+
}

‎src/wp-settings.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@
334334
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-menu-locations-controller.php';
335335
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-users-controller.php';
336336
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-comments-controller.php';
337+
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-comment-types-controller.php';
337338
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-search-controller.php';
338339
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-blocks-controller.php';
339340
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-types-controller.php';

0 commit comments

Comments
 (0)