Complex classes like WP_REST_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_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | abstract class WP_REST_Controller { |
||
|
|||
5 | |||
6 | /** |
||
7 | * The namespace of this controller's route. |
||
8 | * |
||
9 | * @var string |
||
10 | */ |
||
11 | protected $namespace; |
||
12 | |||
13 | /** |
||
14 | * The base of this controller's route. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $rest_base; |
||
19 | |||
20 | /** |
||
21 | * Register the routes for the objects of the controller. |
||
22 | */ |
||
23 | public function register_routes() { |
||
26 | |||
27 | /** |
||
28 | * Check if a given request has access to get items. |
||
29 | * |
||
30 | * @param WP_REST_Request $request Full data about the request. |
||
31 | * @return WP_Error|boolean |
||
32 | */ |
||
33 | public function get_items_permissions_check( $request ) { |
||
38 | |||
39 | /** |
||
40 | * Get a collection of items. |
||
41 | * |
||
42 | * @param WP_REST_Request $request Full data about the request. |
||
43 | * @return WP_Error|WP_REST_Response |
||
44 | */ |
||
45 | public function get_items( $request ) { |
||
50 | |||
51 | /** |
||
52 | * Check if a given request has access to get a specific item. |
||
53 | * |
||
54 | * @param WP_REST_Request $request Full data about the request. |
||
55 | * @return WP_Error|boolean |
||
56 | */ |
||
57 | public function get_item_permissions_check( $request ) { |
||
62 | |||
63 | /** |
||
64 | * Get one item from the collection. |
||
65 | * |
||
66 | * @param WP_REST_Request $request Full data about the request. |
||
67 | * @return WP_Error|WP_REST_Response |
||
68 | */ |
||
69 | public function get_item( $request ) { |
||
74 | |||
75 | /** |
||
76 | * Check if a given request has access to create items. |
||
77 | * |
||
78 | * @param WP_REST_Request $request Full data about the request. |
||
79 | * @return WP_Error|boolean |
||
80 | */ |
||
81 | public function create_item_permissions_check( $request ) { |
||
86 | |||
87 | /** |
||
88 | * Create one item from the collection. |
||
89 | * |
||
90 | * @param WP_REST_Request $request Full data about the request. |
||
91 | * @return WP_Error|WP_REST_Response |
||
92 | */ |
||
93 | public function create_item( $request ) { |
||
98 | |||
99 | /** |
||
100 | * Check if a given request has access to update a specific item. |
||
101 | * |
||
102 | * @param WP_REST_Request $request Full data about the request. |
||
103 | * @return WP_Error|boolean |
||
104 | */ |
||
105 | public function update_item_permissions_check( $request ) { |
||
110 | |||
111 | /** |
||
112 | * Update one item from the collection. |
||
113 | * |
||
114 | * @param WP_REST_Request $request Full data about the request. |
||
115 | * @return WP_Error|WP_REST_Response |
||
116 | */ |
||
117 | public function update_item( $request ) { |
||
122 | |||
123 | /** |
||
124 | * Check if a given request has access to delete a specific item. |
||
125 | * |
||
126 | * @param WP_REST_Request $request Full data about the request. |
||
127 | * @return WP_Error|boolean |
||
128 | */ |
||
129 | public function delete_item_permissions_check( $request ) { |
||
134 | |||
135 | /** |
||
136 | * Delete one item from the collection. |
||
137 | * |
||
138 | * @param WP_REST_Request $request Full data about the request. |
||
139 | * @return WP_Error|WP_REST_Response |
||
140 | */ |
||
141 | public function delete_item( $request ) { |
||
146 | |||
147 | /** |
||
148 | * Prepare the item for create or update operation. |
||
149 | * |
||
150 | * @param WP_REST_Request $request Request object. |
||
151 | * @return WP_Error|object $prepared_item |
||
152 | */ |
||
153 | protected function prepare_item_for_database( $request ) { |
||
158 | |||
159 | /** |
||
160 | * Prepare the item for the REST response. |
||
161 | * |
||
162 | * @param mixed $item WordPress representation of the item. |
||
163 | * @param WP_REST_Request $request Request object. |
||
164 | * @return WP_REST_Response $response |
||
165 | */ |
||
166 | public function prepare_item_for_response( $item, $request ) { |
||
171 | |||
172 | /** |
||
173 | * Prepare a response for inserting into a collection. |
||
174 | * |
||
175 | * @param WP_REST_Response $response Response object. |
||
176 | * @return array Response data, ready for insertion into collection data. |
||
177 | */ |
||
178 | public function prepare_response_for_collection( $response ) { |
||
198 | |||
199 | /** |
||
200 | * Filter a response based on the context defined in the schema. |
||
201 | * |
||
202 | * @param array $data |
||
203 | * @param string $context |
||
204 | * @return array |
||
205 | */ |
||
206 | public function filter_response_by_context( $data, $context ) { |
||
235 | |||
236 | /** |
||
237 | * Get the item's schema, conforming to JSON Schema. |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | public function get_item_schema() { |
||
244 | |||
245 | /** |
||
246 | * Get the item's schema for display / public consumption purposes. |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | public function get_public_item_schema() { |
||
262 | |||
263 | /** |
||
264 | * Get the query params for collections. |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | public function get_collection_params() { |
||
296 | |||
297 | /** |
||
298 | * Get the magical context param. |
||
299 | * |
||
300 | * Ensures consistent description between endpoints, and populates enum from schema. |
||
301 | * |
||
302 | * @param array $args |
||
303 | * @return array |
||
304 | */ |
||
305 | public function get_context_param( $args = array() ) { |
||
328 | |||
329 | /** |
||
330 | * Add the values from additional fields to a data object. |
||
331 | * |
||
332 | * @param array $object |
||
333 | * @param WP_REST_Request $request |
||
334 | * @return array modified object with additional fields. |
||
335 | */ |
||
336 | protected function add_additional_fields_to_object( $object, $request ) { |
||
351 | |||
352 | /** |
||
353 | * Update the values of additional fields added to a data object. |
||
354 | * |
||
355 | * @param array $object |
||
356 | * @param WP_REST_Request $request |
||
357 | */ |
||
358 | protected function update_additional_fields_for_object( $object, $request ) { |
||
376 | |||
377 | /** |
||
378 | * Add the schema from additional fields to an schema array. |
||
379 | * |
||
380 | * The type of object is inferred from the passed schema. |
||
381 | * |
||
382 | * @param array $schema Schema array. |
||
383 | */ |
||
384 | protected function add_additional_fields_schema( $schema ) { |
||
406 | |||
407 | /** |
||
408 | * Get all the registered additional fields for a given object-type. |
||
409 | * |
||
410 | * @param string $object_type |
||
411 | * @return array |
||
412 | */ |
||
413 | protected function get_additional_fields( $object_type = null ) { |
||
431 | |||
432 | /** |
||
433 | * Get the object type this controller is responsible for managing. |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | protected function get_object_type() { |
||
446 | |||
447 | /** |
||
448 | * Get an array of endpoint arguments from the item schema for the controller. |
||
449 | * |
||
450 | * @param string $method HTTP method of the request. The arguments |
||
451 | * for `CREATABLE` requests are checked for required |
||
452 | * values and may fall-back to a given default, this |
||
453 | * is not done on `EDITABLE` requests. Default is |
||
454 | * WP_REST_Server::CREATABLE. |
||
455 | * @return array $endpoint_args |
||
456 | */ |
||
457 | public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { |
||
510 | |||
511 | /** |
||
512 | * Retrieves post data given a post ID or post object. |
||
513 | * |
||
514 | * This is a subset of the functionality of the `get_post()` function, with |
||
515 | * the additional functionality of having `the_post` action done on the |
||
516 | * resultant post object. This is done so that plugins may manipulate the |
||
517 | * post that is used in the REST API. |
||
518 | * |
||
519 | * @see get_post() |
||
520 | * @global WP_Query $wp_query |
||
521 | * |
||
522 | * @param int|WP_Post $post Post ID or post object. Defaults to global $post. |
||
523 | * @return WP_Post|null A `WP_Post` object when successful. |
||
524 | */ |
||
525 | public function get_post( $post ) { |
||
540 | } |
||
541 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.