Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_REST_Comments_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_REST_Comments_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class WP_REST_Comments_Controller extends WP_REST_Controller { |
||
7 | |||
8 | public function __construct() { |
||
12 | |||
13 | /** |
||
14 | * Register the routes for the objects of the controller. |
||
15 | */ |
||
16 | public function register_routes() { |
||
63 | |||
64 | /** |
||
65 | * Check if a given request has access to read comments |
||
66 | * |
||
67 | * @param WP_REST_Request $request Full details about the request. |
||
68 | * @return WP_Error|boolean |
||
69 | */ |
||
70 | public function get_items_permissions_check( $request ) { |
||
110 | |||
111 | /** |
||
112 | * Get a list of comments. |
||
113 | * |
||
114 | * @param WP_REST_Request $request Full details about the request. |
||
115 | * @return WP_Error|WP_REST_Response |
||
116 | */ |
||
117 | public function get_items( $request ) { |
||
118 | $prepared_args = array( |
||
119 | 'author_email' => isset( $request['author_email'] ) ? $request['author_email'] : '', |
||
120 | 'comment__in' => $request['include'], |
||
121 | 'comment__not_in' => $request['exclude'], |
||
122 | 'karma' => isset( $request['karma'] ) ? $request['karma'] : '', |
||
123 | 'number' => $request['per_page'], |
||
124 | 'post__in' => $request['post'], |
||
125 | 'parent__in' => $request['parent'], |
||
126 | 'parent__not_in' => $request['parent_exclude'], |
||
127 | 'search' => $request['search'], |
||
128 | 'offset' => $request['offset'], |
||
129 | 'orderby' => $this->normalize_query_param( $request['orderby'] ), |
||
130 | 'order' => $request['order'], |
||
131 | 'status' => $request['status'], |
||
132 | 'type' => $request['type'], |
||
133 | 'no_found_rows' => false, |
||
134 | 'author__in' => $request['author'], |
||
135 | 'author__not_in' => $request['author_exclude'], |
||
136 | ); |
||
137 | |||
138 | $prepared_args['date_query'] = array(); |
||
139 | // Set before into date query. Date query must be specified as an array of an array. |
||
140 | if ( isset( $request['before'] ) ) { |
||
141 | $prepared_args['date_query'][0]['before'] = $request['before']; |
||
142 | } |
||
143 | |||
144 | // Set after into date query. Date query must be specified as an array of an array. |
||
145 | View Code Duplication | if ( isset( $request['after'] ) ) { |
|
146 | $prepared_args['date_query'][0]['after'] = $request['after']; |
||
147 | } |
||
148 | |||
149 | if ( empty( $request['offset'] ) ) { |
||
150 | $prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 ); |
||
151 | } |
||
152 | if ( empty( $request['search'] ) ) { |
||
153 | $prepared_args['search'] = ''; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Filter arguments, before passing to WP_Comment_Query, when querying comments via the REST API. |
||
158 | * |
||
159 | * @see https://developer.wordpress.org/reference/classes/wp_comment_query/ |
||
160 | * |
||
161 | * @param array $prepared_args Array of arguments for WP_Comment_Query. |
||
162 | * @param WP_REST_Request $request The current request. |
||
163 | */ |
||
164 | $prepared_args = apply_filters( 'rest_comment_query', $prepared_args, $request ); |
||
165 | |||
166 | $query = new WP_Comment_Query; |
||
167 | $query_result = $query->query( $prepared_args ); |
||
168 | |||
169 | $comments = array(); |
||
170 | View Code Duplication | foreach ( $query_result as $comment ) { |
|
171 | if ( ! $this->check_read_permission( $comment ) ) { |
||
172 | continue; |
||
173 | } |
||
174 | |||
175 | $data = $this->prepare_item_for_response( $comment, $request ); |
||
176 | $comments[] = $this->prepare_response_for_collection( $data ); |
||
177 | } |
||
178 | |||
179 | $total_comments = (int) $query->found_comments; |
||
180 | $max_pages = (int) $query->max_num_pages; |
||
181 | if ( $total_comments < 1 ) { |
||
182 | // Out-of-bounds, run the query again without LIMIT for total count |
||
183 | unset( $prepared_args['number'] ); |
||
184 | unset( $prepared_args['offset'] ); |
||
185 | $query = new WP_Comment_Query; |
||
186 | $prepared_args['count'] = true; |
||
187 | |||
188 | $total_comments = $query->query( $prepared_args ); |
||
189 | $max_pages = ceil( $total_comments / $request['per_page'] ); |
||
190 | } |
||
191 | |||
192 | $response = rest_ensure_response( $comments ); |
||
193 | $response->header( 'X-WP-Total', $total_comments ); |
||
194 | $response->header( 'X-WP-TotalPages', $max_pages ); |
||
195 | |||
196 | $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); |
||
197 | View Code Duplication | if ( $request['page'] > 1 ) { |
|
198 | $prev_page = $request['page'] - 1; |
||
199 | if ( $prev_page > $max_pages ) { |
||
200 | $prev_page = $max_pages; |
||
201 | } |
||
202 | $prev_link = add_query_arg( 'page', $prev_page, $base ); |
||
203 | $response->link_header( 'prev', $prev_link ); |
||
204 | } |
||
205 | View Code Duplication | if ( $max_pages > $request['page'] ) { |
|
206 | $next_page = $request['page'] + 1; |
||
207 | $next_link = add_query_arg( 'page', $next_page, $base ); |
||
208 | $response->link_header( 'next', $next_link ); |
||
209 | } |
||
210 | |||
211 | return $response; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Check if a given request has access to read the comment |
||
216 | * |
||
217 | * @param WP_REST_Request $request Full details about the request. |
||
218 | * @return WP_Error|boolean |
||
219 | */ |
||
220 | public function get_item_permissions_check( $request ) { |
||
245 | |||
246 | /** |
||
247 | * Get a comment. |
||
248 | * |
||
249 | * @param WP_REST_Request $request Full details about the request. |
||
250 | * @return WP_Error|WP_REST_Response |
||
251 | */ |
||
252 | public function get_item( $request ) { |
||
272 | |||
273 | /** |
||
274 | * Check if a given request has access to create a comment |
||
275 | * |
||
276 | * @param WP_REST_Request $request Full details about the request. |
||
277 | * @return WP_Error|boolean |
||
278 | */ |
||
279 | public function create_item_permissions_check( $request ) { |
||
309 | |||
310 | /** |
||
311 | * Create a comment. |
||
312 | * |
||
313 | * @param WP_REST_Request $request Full details about the request. |
||
314 | * @return WP_Error|WP_REST_Response |
||
315 | */ |
||
316 | public function create_item( $request ) { |
||
317 | if ( ! empty( $request['id'] ) ) { |
||
318 | return new WP_Error( 'rest_comment_exists', __( 'Cannot create existing comment.' ), array( 'status' => 400 ) ); |
||
319 | } |
||
320 | |||
321 | $prepared_comment = $this->prepare_item_for_database( $request ); |
||
322 | |||
323 | // Setting remaining values before wp_insert_comment so we can |
||
324 | // use wp_allow_comment(). |
||
325 | if ( ! isset( $prepared_comment['comment_date_gmt'] ) ) { |
||
326 | $prepared_comment['comment_date_gmt'] = current_time( 'mysql', true ); |
||
327 | } |
||
328 | |||
329 | // Set author data if the user's logged in |
||
330 | $missing_author = empty( $prepared_comment['user_id'] ) |
||
331 | && empty( $prepared_comment['comment_author'] ) |
||
332 | && empty( $prepared_comment['comment_author_email'] ) |
||
333 | && empty( $prepared_comment['comment_author_url'] ); |
||
334 | |||
335 | if ( is_user_logged_in() && $missing_author ) { |
||
336 | $user = wp_get_current_user(); |
||
337 | $prepared_comment['user_id'] = $user->ID; |
||
338 | $prepared_comment['comment_author'] = $user->display_name; |
||
339 | $prepared_comment['comment_author_email'] = $user->user_email; |
||
340 | $prepared_comment['comment_author_url'] = $user->user_url; |
||
341 | } |
||
342 | |||
343 | if ( ! isset( $prepared_comment['comment_author_email'] ) ) { |
||
344 | $prepared_comment['comment_author_email'] = ''; |
||
345 | } |
||
346 | if ( ! isset( $prepared_comment['comment_author_url'] ) ) { |
||
347 | $prepared_comment['comment_author_url'] = ''; |
||
348 | } |
||
349 | |||
350 | $prepared_comment['comment_agent'] = ''; |
||
351 | $prepared_comment['comment_approved'] = wp_allow_comment( $prepared_comment ); |
||
352 | |||
353 | /** |
||
354 | * Filter a comment before it is inserted via the REST API. |
||
355 | * |
||
356 | * Allows modification of the comment right before it is inserted via `wp_insert_comment`. |
||
357 | * |
||
358 | * @param array $prepared_comment The prepared comment data for `wp_insert_comment`. |
||
359 | * @param WP_REST_Request $request Request used to insert the comment. |
||
360 | */ |
||
361 | $prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request ); |
||
362 | |||
363 | $comment_id = wp_insert_comment( $prepared_comment ); |
||
364 | if ( ! $comment_id ) { |
||
365 | return new WP_Error( 'rest_comment_failed_create', __( 'Creating comment failed.' ), array( 'status' => 500 ) ); |
||
366 | } |
||
367 | |||
368 | if ( isset( $request['status'] ) ) { |
||
369 | $comment = get_comment( $comment_id ); |
||
370 | $this->handle_status_param( $request['status'], $comment ); |
||
371 | } |
||
372 | |||
373 | $comment = get_comment( $comment_id ); |
||
374 | $fields_update = $this->update_additional_fields_for_object( $comment, $request ); |
||
|
|||
375 | if ( is_wp_error( $fields_update ) ) { |
||
376 | return $fields_update; |
||
377 | } |
||
378 | |||
379 | $context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view'; |
||
380 | $request->set_param( 'context', $context ); |
||
381 | $response = $this->prepare_item_for_response( $comment, $request ); |
||
382 | $response = rest_ensure_response( $response ); |
||
383 | $response->set_status( 201 ); |
||
384 | $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment_id ) ) ); |
||
385 | |||
386 | /** |
||
387 | * Fires after a comment is created or updated via the REST API. |
||
388 | * |
||
389 | * @param array $comment Comment as it exists in the database. |
||
390 | * @param WP_REST_Request $request The request sent to the API. |
||
391 | * @param boolean $creating True when creating a comment, false when updating. |
||
392 | */ |
||
393 | do_action( 'rest_insert_comment', $comment, $request, true ); |
||
394 | |||
395 | return $response; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Check if a given request has access to update a comment |
||
400 | * |
||
401 | * @param WP_REST_Request $request Full details about the request. |
||
402 | * @return WP_Error|boolean |
||
403 | */ |
||
404 | public function update_item_permissions_check( $request ) { |
||
405 | |||
406 | $id = (int) $request['id']; |
||
407 | |||
408 | $comment = get_comment( $id ); |
||
409 | |||
410 | if ( $comment && ! $this->check_edit_permission( $comment ) ) { |
||
411 | return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you can not edit this comment.' ), array( 'status' => rest_authorization_required_code() ) ); |
||
412 | } |
||
413 | |||
414 | return true; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Edit a comment |
||
419 | * |
||
420 | * @param WP_REST_Request $request Full details about the request. |
||
421 | * @return WP_Error|WP_REST_Response |
||
422 | */ |
||
423 | public function update_item( $request ) { |
||
424 | $id = (int) $request['id']; |
||
425 | |||
426 | $comment = get_comment( $id ); |
||
427 | if ( empty( $comment ) ) { |
||
428 | return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) ); |
||
429 | } |
||
430 | |||
431 | if ( isset( $request['type'] ) && $request['type'] !== $comment->comment_type ) { |
||
432 | return new WP_Error( 'rest_comment_invalid_type', __( 'Sorry, you cannot change the comment type.' ), array( 'status' => 404 ) ); |
||
433 | } |
||
434 | |||
435 | $prepared_args = $this->prepare_item_for_database( $request ); |
||
436 | |||
437 | if ( empty( $prepared_args ) && isset( $request['status'] ) ) { |
||
438 | // Only the comment status is being changed. |
||
439 | $change = $this->handle_status_param( $request['status'], $comment ); |
||
440 | if ( ! $change ) { |
||
441 | return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment status failed.' ), array( 'status' => 500 ) ); |
||
442 | } |
||
443 | } else { |
||
444 | $prepared_args['comment_ID'] = $id; |
||
445 | |||
446 | $updated = wp_update_comment( $prepared_args ); |
||
447 | if ( 0 === $updated ) { |
||
448 | return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), array( 'status' => 500 ) ); |
||
449 | } |
||
450 | |||
451 | if ( isset( $request['status'] ) ) { |
||
452 | $this->handle_status_param( $request['status'], $comment ); |
||
453 | } |
||
454 | } |
||
455 | |||
456 | $comment = get_comment( $id ); |
||
457 | $fields_update = $this->update_additional_fields_for_object( $comment, $request ); |
||
458 | if ( is_wp_error( $fields_update ) ) { |
||
459 | return $fields_update; |
||
460 | } |
||
461 | |||
462 | $request->set_param( 'context', 'edit' ); |
||
463 | $response = $this->prepare_item_for_response( $comment, $request ); |
||
464 | |||
465 | /* This action is documented in lib/endpoints/class-wp-rest-comments-controller.php */ |
||
466 | do_action( 'rest_insert_comment', $comment, $request, false ); |
||
467 | |||
468 | return rest_ensure_response( $response ); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Check if a given request has access to delete a comment |
||
473 | * |
||
474 | * @param WP_REST_Request $request Full details about the request. |
||
475 | * @return WP_Error|boolean |
||
476 | */ |
||
477 | View Code Duplication | public function delete_item_permissions_check( $request ) { |
|
488 | |||
489 | /** |
||
490 | * Delete a comment. |
||
491 | * |
||
492 | * @param WP_REST_Request $request Full details about the request. |
||
493 | * @return WP_Error|WP_REST_Response |
||
494 | */ |
||
495 | public function delete_item( $request ) { |
||
547 | |||
548 | /** |
||
549 | * Prepare a single comment output for response. |
||
550 | * |
||
551 | * @param object $comment Comment object. |
||
552 | * @param WP_REST_Request $request Request object. |
||
553 | * @return WP_REST_Response $response |
||
554 | */ |
||
555 | public function prepare_item_for_response( $comment, $request ) { |
||
604 | |||
605 | /** |
||
606 | * Prepare links for the request. |
||
607 | * |
||
608 | * @param object $comment Comment object. |
||
609 | * @return array Links for the given comment. |
||
610 | */ |
||
611 | protected function prepare_links( $comment ) { |
||
662 | |||
663 | /** |
||
664 | * Prepend internal property prefix to query parameters to match our response fields. |
||
665 | * |
||
666 | * @param string $query_param |
||
667 | * @return string $normalized |
||
668 | */ |
||
669 | protected function normalize_query_param( $query_param ) { |
||
692 | |||
693 | /** |
||
694 | * Check comment_approved to set comment status for single comment output. |
||
695 | * |
||
696 | * @param string|int $comment_approved |
||
697 | * @return string $status |
||
698 | */ |
||
699 | protected function prepare_status_response( $comment_approved ) { |
||
721 | |||
722 | /** |
||
723 | * Prepare a single comment to be inserted into the database. |
||
724 | * |
||
725 | * @param WP_REST_Request $request Request object. |
||
726 | * @return array|WP_Error $prepared_comment |
||
727 | */ |
||
728 | protected function prepare_item_for_database( $request ) { |
||
787 | |||
788 | /** |
||
789 | * Get the Comment's schema, conforming to JSON Schema |
||
790 | * |
||
791 | * @return array |
||
792 | */ |
||
793 | public function get_item_schema() { |
||
951 | |||
952 | /** |
||
953 | * Get the query params for collections |
||
954 | * |
||
955 | * @return array |
||
956 | */ |
||
957 | public function get_collection_params() { |
||
1085 | |||
1086 | /** |
||
1087 | * Set the comment_status of a given comment object when creating or updating a comment. |
||
1088 | * |
||
1089 | * @param string|int $new_status |
||
1090 | * @param object $comment |
||
1091 | * @return boolean $changed |
||
1092 | */ |
||
1093 | protected function handle_status_param( $new_status, $comment ) { |
||
1129 | |||
1130 | /** |
||
1131 | * Check if we can read a post. |
||
1132 | * |
||
1133 | * Correctly handles posts with the inherit status. |
||
1134 | * |
||
1135 | * @param WP_Post $post Post Object. |
||
1136 | * @return boolean Can we read it? |
||
1137 | */ |
||
1138 | protected function check_read_post_permission( $post ) { |
||
1143 | |||
1144 | /** |
||
1145 | * Check if we can read a comment. |
||
1146 | * |
||
1147 | * @param object $comment Comment object. |
||
1148 | * @return boolean Can we read it? |
||
1149 | */ |
||
1150 | protected function check_read_permission( $comment ) { |
||
1177 | |||
1178 | /** |
||
1179 | * Check if we can edit or delete a comment. |
||
1180 | * |
||
1181 | * @param object $comment Comment object. |
||
1182 | * @return boolean Can we edit or delete it? |
||
1183 | */ |
||
1184 | protected function check_edit_permission( $comment ) { |
||
1195 | } |
||
1196 |