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 ) { |
||
36 | |||
37 | /** |
||
38 | * Get a collection of items. |
||
39 | * |
||
40 | * @param WP_REST_Request $request Full data about the request. |
||
41 | * @return WP_Error|WP_REST_Response |
||
42 | */ |
||
43 | public function get_items( $request ) { |
||
46 | |||
47 | /** |
||
48 | * Check if a given request has access to get a specific item. |
||
49 | * |
||
50 | * @param WP_REST_Request $request Full data about the request. |
||
51 | * @return WP_Error|boolean |
||
52 | */ |
||
53 | public function get_item_permissions_check( $request ) { |
||
56 | |||
57 | /** |
||
58 | * Get one item from the collection. |
||
59 | * |
||
60 | * @param WP_REST_Request $request Full data about the request. |
||
61 | * @return WP_Error|WP_REST_Response |
||
62 | */ |
||
63 | public function get_item( $request ) { |
||
66 | |||
67 | /** |
||
68 | * Check if a given request has access to create items. |
||
69 | * |
||
70 | * @param WP_REST_Request $request Full data about the request. |
||
71 | * @return WP_Error|boolean |
||
72 | */ |
||
73 | public function create_item_permissions_check( $request ) { |
||
76 | |||
77 | /** |
||
78 | * Create one item from the collection. |
||
79 | * |
||
80 | * @param WP_REST_Request $request Full data about the request. |
||
81 | * @return WP_Error|WP_REST_Response |
||
82 | */ |
||
83 | public function create_item( $request ) { |
||
86 | |||
87 | /** |
||
88 | * Check if a given request has access to update a specific item. |
||
89 | * |
||
90 | * @param WP_REST_Request $request Full data about the request. |
||
91 | * @return WP_Error|boolean |
||
92 | */ |
||
93 | public function update_item_permissions_check( $request ) { |
||
96 | |||
97 | /** |
||
98 | * Update one item from the collection. |
||
99 | * |
||
100 | * @param WP_REST_Request $request Full data about the request. |
||
101 | * @return WP_Error|WP_REST_Response |
||
102 | */ |
||
103 | public function update_item( $request ) { |
||
106 | |||
107 | /** |
||
108 | * Check if a given request has access to delete a specific item. |
||
109 | * |
||
110 | * @param WP_REST_Request $request Full data about the request. |
||
111 | * @return WP_Error|boolean |
||
112 | */ |
||
113 | public function delete_item_permissions_check( $request ) { |
||
116 | |||
117 | /** |
||
118 | * Delete one item from the collection. |
||
119 | * |
||
120 | * @param WP_REST_Request $request Full data about the request. |
||
121 | * @return WP_Error|WP_REST_Response |
||
122 | */ |
||
123 | public function delete_item( $request ) { |
||
126 | |||
127 | /** |
||
128 | * Prepare the item for create or update operation. |
||
129 | * |
||
130 | * @param WP_REST_Request $request Request object. |
||
131 | * @return WP_Error|object $prepared_item |
||
132 | */ |
||
133 | protected function prepare_item_for_database( $request ) { |
||
136 | |||
137 | /** |
||
138 | * Prepare the item for the REST response. |
||
139 | * |
||
140 | * @param mixed $item WordPress representation of the item. |
||
141 | * @param WP_REST_Request $request Request object. |
||
142 | * @return WP_REST_Response $response |
||
143 | */ |
||
144 | public function prepare_item_for_response( $item, $request ) { |
||
147 | |||
148 | /** |
||
149 | * Prepare a response for inserting into a collection. |
||
150 | * |
||
151 | * @param WP_REST_Response $response Response object. |
||
152 | * @return array Response data, ready for insertion into collection data. |
||
153 | */ |
||
154 | public function prepare_response_for_collection( $response ) { |
||
174 | |||
175 | /** |
||
176 | * Filter a response based on the context defined in the schema. |
||
177 | * |
||
178 | * @param array $data |
||
179 | * @param string $context |
||
180 | * @return array |
||
181 | */ |
||
182 | public function filter_response_by_context( $data, $context ) { |
||
211 | |||
212 | /** |
||
213 | * Get the item's schema, conforming to JSON Schema. |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | public function get_item_schema() { |
||
220 | |||
221 | /** |
||
222 | * Get the item's schema for display / public consumption purposes. |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | public function get_public_item_schema() { |
||
238 | |||
239 | /** |
||
240 | * Get the query params for collections. |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | public function get_collection_params() { |
||
272 | |||
273 | /** |
||
274 | * Get the magical context param. |
||
275 | * |
||
276 | * Ensures consistent description between endpoints, and populates enum from schema. |
||
277 | * |
||
278 | * @param array $args |
||
279 | * @return array |
||
280 | */ |
||
281 | public function get_context_param( $args = array() ) { |
||
304 | |||
305 | /** |
||
306 | * Add the values from additional fields to a data object. |
||
307 | * |
||
308 | * @param array $object |
||
309 | * @param WP_REST_Request $request |
||
310 | * @return array modified object with additional fields. |
||
311 | */ |
||
312 | protected function add_additional_fields_to_object( $object, $request ) { |
||
327 | |||
328 | /** |
||
329 | * Update the values of additional fields added to a data object. |
||
330 | * |
||
331 | * @param array $object |
||
332 | * @param WP_REST_Request $request |
||
333 | */ |
||
334 | protected function update_additional_fields_for_object( $object, $request ) { |
||
352 | |||
353 | /** |
||
354 | * Add the schema from additional fields to an schema array. |
||
355 | * |
||
356 | * The type of object is inferred from the passed schema. |
||
357 | * |
||
358 | * @param array $schema Schema array. |
||
359 | */ |
||
360 | protected function add_additional_fields_schema( $schema ) { |
||
382 | |||
383 | /** |
||
384 | * Get all the registered additional fields for a given object-type. |
||
385 | * |
||
386 | * @param string $object_type |
||
387 | * @return array |
||
388 | */ |
||
389 | protected function get_additional_fields( $object_type = null ) { |
||
407 | |||
408 | /** |
||
409 | * Get the object type this controller is responsible for managing. |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | protected function get_object_type() { |
||
422 | |||
423 | /** |
||
424 | * Get an array of endpoint arguments from the item schema for the controller. |
||
425 | * |
||
426 | * @param string $method HTTP method of the request. The arguments |
||
427 | * for `CREATABLE` requests are checked for required |
||
428 | * values and may fall-back to a given default, this |
||
429 | * is not done on `EDITABLE` requests. Default is |
||
430 | * WP_REST_Server::CREATABLE. |
||
431 | * @return array $endpoint_args |
||
432 | */ |
||
433 | public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { |
||
483 | |||
484 | /** |
||
485 | * Retrieves post data given a post ID or post object. |
||
486 | * |
||
487 | * This is a subset of the functionality of the `get_post()` function, with |
||
488 | * the additional functionality of having `the_post` action done on the |
||
489 | * resultant post object. This is done so that plugins may manipulate the |
||
490 | * post that is used in the REST API. |
||
491 | * |
||
492 | * @see get_post() |
||
493 | * @global WP_Query $wp_query |
||
494 | * |
||
495 | * @param int|WP_Post $post Post ID or post object. Defaults to global $post. |
||
496 | * @return WP_Post|null A `WP_Post` object when successful. |
||
497 | */ |
||
498 | public function get_post( $post ) { |
||
513 | } |
||
514 |
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.