Complex classes like Views_Route 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 Views_Route, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Views_Route extends Route { |
||
19 | /** |
||
20 | * Route Name |
||
21 | * |
||
22 | * @since 2.0 |
||
23 | * |
||
24 | * @access protected |
||
25 | * @string |
||
26 | */ |
||
27 | protected $route_name = 'views'; |
||
28 | |||
29 | /** |
||
30 | * Sub type, forms {$namespace}/route_name/{id}/sub_type type endpoints |
||
31 | * |
||
32 | * @since 2.0 |
||
33 | * @access protected |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $sub_type = 'entries'; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Get a collection of views |
||
41 | * |
||
42 | * Callback for GET /v1/views/ |
||
43 | * |
||
44 | * @param \WP_REST_Request $request Full data about the request. |
||
45 | * @return \WP_Error|\WP_REST_Response |
||
46 | */ |
||
47 | 3 | public function get_items( $request ) { |
|
48 | |||
49 | 3 | $page = $request->get_param( 'page' ); |
|
50 | 3 | $limit = $request->get_param( 'limit' ); |
|
51 | |||
52 | 3 | $items = \GVCommon::get_all_views( array( |
|
53 | 3 | 'posts_per_page' => $limit, |
|
54 | 3 | 'paged' => $page, |
|
55 | ) ); |
||
56 | |||
57 | 3 | if ( empty( $items ) ) { |
|
58 | return new \WP_Error( 'gravityview-no-views', __( 'No Views found.', 'gravityview' ) ); //@todo message |
||
59 | } |
||
60 | |||
61 | $data = array( |
||
62 | 3 | 'views' => array(), |
|
63 | 3 | 'total' => wp_count_posts( 'gravityview' )->publish, |
|
64 | ); |
||
65 | 3 | foreach ( $items as $item ) { |
|
66 | 3 | $data['views'][] = $this->prepare_view_for_response( $item, $request ); |
|
67 | } |
||
68 | |||
69 | 3 | return new \WP_REST_Response( $data, 200 ); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Get one view |
||
74 | * |
||
75 | * Callback for /v1/views/{id}/ |
||
76 | * |
||
77 | * @since 2.0 |
||
78 | * @param \WP_REST_Request $request Full data about the request. |
||
79 | * @return \WP_Error|\WP_REST_Response |
||
80 | */ |
||
81 | 2 | public function get_item( $request ) { |
|
82 | |||
83 | 2 | $url = $request->get_url_params(); |
|
84 | |||
85 | 2 | $view_id = intval( $url['id'] ); |
|
86 | |||
87 | 2 | $item = get_post( $view_id ); |
|
88 | |||
89 | //return a response or error based on some conditional |
||
90 | 2 | if ( $item && ! is_wp_error( $item ) ) { |
|
91 | 2 | $data = $this->prepare_view_for_response( $item, $request ); |
|
92 | 2 | return new \WP_REST_Response( $data, 200 ); |
|
93 | } |
||
94 | |||
95 | return new \WP_Error( 'code', sprintf( 'A View with ID #%d was not found.', $view_id ) ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Prepare the item for the REST response |
||
100 | * |
||
101 | * @since 2.0 |
||
102 | * @param \GV\View $view The view. |
||
103 | * @param \GV\Entry $entry WordPress representation of the item. |
||
104 | * @param \WP_REST_Request $request Request object. |
||
105 | * @param string $context The context (directory, single) |
||
106 | * @return mixed The data that is sent. |
||
107 | */ |
||
108 | 4 | public function prepare_entry_for_response( $view, $entry, \WP_REST_Request $request, $context ) { |
|
109 | 4 | $return = $entry->as_entry(); |
|
110 | |||
111 | // Only output the fields that should be displayed. |
||
112 | 4 | $allowed = array(); |
|
113 | 4 | foreach ( $view->fields->by_position( "{$context}_*" )->by_visible()->all() as $field ) { |
|
114 | 4 | $allowed[] = $field->ID; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @filter `gravityview/rest/entry/fields` Whitelist more entry fields that are output in regular REST requests. |
||
119 | * @param[in,out] array $allowed The allowed ones, default by_visible, by_position( "context_*" ), i.e. as set in the view. |
||
120 | * @param \GV\View $view The view. |
||
121 | * @param \GV\Entry $entry WordPress representation of the item. |
||
122 | * @param \WP_REST_Request $request Request object. |
||
123 | * @param string $context The context (directory, single) |
||
124 | */ |
||
125 | 4 | $allowed = apply_filters( 'gravityview/rest/entry/fields', $allowed, $view, $entry, $request, $context ); |
|
126 | |||
127 | 4 | foreach ( $return as $key => $value ) { |
|
128 | 4 | if ( ! in_array( $key, $allowed ) ) { |
|
129 | 4 | unset( $return[ $key ] ); |
|
130 | } |
||
131 | } |
||
132 | |||
133 | 4 | $r = new Request( $request ); |
|
134 | |||
135 | 4 | foreach ( $allowed as $field ) { |
|
136 | 4 | $source = is_numeric( $field ) ? $view->form : new \GV\Internal_Source(); |
|
137 | 4 | $field = is_numeric( $field ) ? \GV\GF_Field::by_id( $view->form, $field ) : \GV\Internal_Field::by_id( $field ); |
|
138 | |||
139 | 4 | $return[ $field->ID ] = $field->get_value( $view, $source, $entry, $r ); |
|
140 | } |
||
141 | |||
142 | // @todo Set the labels! |
||
143 | |||
144 | 4 | return $return; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Get entries from a view |
||
149 | * |
||
150 | * Callback for /v1/views/{id}/entries/ |
||
151 | * |
||
152 | * @since 2.0 |
||
153 | * @param \WP_REST_Request $request Full data about the request. |
||
154 | * @return \WP_Error|\WP_REST_Response |
||
155 | */ |
||
156 | 3 | public function get_sub_items( $request ) { |
|
157 | |||
158 | 3 | $url = $request->get_url_params(); |
|
159 | 3 | $view_id = intval( $url['id'] ); |
|
160 | 3 | $format = \GV\Utils::get( $url, 'format', 'json' ); |
|
161 | |||
162 | 3 | $view = \GV\View::by_id( $view_id ); |
|
163 | |||
164 | 3 | if ( $format == 'html' ) { |
|
165 | |||
166 | 1 | $renderer = new \GV\View_Renderer(); |
|
167 | 1 | $total = 0; |
|
168 | |||
169 | 1 | add_action( 'gravityview/template/view/render', function( $context ) use ( &$total ) { |
|
170 | 1 | $total = $context->entries->count(); |
|
171 | 1 | } ); |
|
172 | |||
173 | 1 | $output = $renderer->render( $view, new Request( $request ) ); |
|
174 | |||
175 | /** |
||
176 | * @filter `gravityview/rest/entries/html/insert_meta` Whether to include `http-equiv` meta tags in the HTML output describing the data |
||
177 | * @param bool $insert_meta Add <meta> tags? [Default: true] |
||
178 | * @param int $total The number of entries being rendered |
||
179 | * @param \GV\View $view The view. |
||
180 | * @param \WP_REST_Request $request Request object. |
||
181 | */ |
||
182 | 1 | $insert_meta = apply_filters( 'gravityview/rest/entries/html/insert_meta', true, $total, $view, $request ); |
|
183 | |||
184 | 1 | if ( $insert_meta ) { |
|
185 | 1 | $output = '<meta http-equiv="X-Item-Count" content="' . $total . '" />' . $output; |
|
186 | } |
||
187 | |||
188 | 1 | $response = new \WP_REST_Response( $output, 200 ); |
|
189 | 1 | $response->header( 'X-Item-Count', $total ); |
|
190 | |||
191 | 1 | return $response; |
|
192 | } |
||
193 | |||
194 | 3 | $entries = $view->get_entries( new Request( $request ) ); |
|
195 | |||
196 | 3 | if ( ! $entries->all() ) { |
|
197 | return new \WP_Error( 'gravityview-no-entries', __( 'No Entries found.', 'gravityview' ) ); |
||
198 | } |
||
199 | |||
200 | 3 | $data = array( 'entries' => $entries->all(), 'total' => $entries->total() ); |
|
201 | |||
202 | 3 | foreach ( $data['entries'] as &$entry ) { |
|
203 | 3 | $entry = $this->prepare_entry_for_response( $view, $entry, $request, 'directory' ); |
|
204 | } |
||
205 | |||
206 | 3 | return new \WP_REST_Response( $data, 200 ); |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * Get one entry from view |
||
211 | * |
||
212 | * Callback for /v1/views/{id}/entries/{id}/ |
||
213 | * |
||
214 | * @uses GVCommon::get_entry |
||
215 | * @since 2.0 |
||
216 | * @param \WP_REST_Request $request Full data about the request. |
||
217 | * @return \WP_Error|\WP_REST_Response |
||
218 | */ |
||
219 | 2 | public function get_sub_item( $request ) { |
|
220 | 2 | $url = $request->get_url_params(); |
|
221 | 2 | $view_id = intval( $url['id'] ); |
|
222 | 2 | $entry_id = intval( $url['s_id'] ); |
|
223 | 2 | $format = \GV\Utils::get( $url, 'format', 'json' ); |
|
224 | |||
225 | 2 | $view = \GV\View::by_id( $view_id ); |
|
226 | 2 | $entry = \GV\GF_Entry::by_id( $entry_id ); |
|
227 | |||
228 | 2 | if ( $format == 'html' ) { |
|
229 | 1 | $renderer = new \GV\Entry_Renderer(); |
|
230 | 1 | return $renderer->render( $entry, $view, new Request( $request ) ); |
|
231 | } |
||
232 | |||
233 | 2 | return $this->prepare_entry_for_response( $view, $entry, $request, 'single' ); |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Prepare the item for the REST response |
||
238 | * |
||
239 | * @since 2.0 |
||
240 | * @param \WP_Post $view_post WordPress representation of the item. |
||
241 | * @param \WP_REST_Request $request Request object. |
||
242 | * @return mixed |
||
243 | */ |
||
244 | 4 | public function prepare_view_for_response( $view_post, \WP_REST_Request $request ) { |
|
245 | 4 | if ( is_wp_error( $this->get_item_permissions_check( $request, $view_post->ID ) ) ) { |
|
246 | // Redacted out view. |
||
247 | 1 | return array( 'ID' => $view_post->ID, 'post_content' => __( 'You are not allowed to access this content.', 'gravityview' ) ); |
|
248 | } |
||
249 | |||
250 | 4 | $view = \GV\View::from_post( $view_post ); |
|
251 | |||
252 | 4 | $item = $view->as_data(); |
|
253 | |||
254 | // Add all the WP_Post data |
||
255 | 4 | $view_post = $view_post->to_array(); |
|
256 | |||
257 | 4 | unset( $view_post['to_ping'], $view_post['ping_status'], $view_post['pinged'], $view_post['post_type'], $view_post['filter'], $view_post['post_category'], $view_post['tags_input'], $view_post['post_content'], $view_post['post_content_filtered'] ); |
|
258 | |||
259 | 4 | $return = wp_parse_args( $item, $view_post ); |
|
260 | |||
261 | 4 | $return['title'] = $return['post_title']; |
|
262 | |||
263 | 4 | $return['settings'] = isset( $return['atts'] ) ? $return['atts'] : array(); |
|
264 | 4 | unset( $return['atts'], $return['view_id'] ); |
|
265 | |||
266 | 4 | $return['search_criteria'] = array( |
|
267 | 4 | 'page_size' => rgars( $return, 'settings/page_size' ), |
|
268 | 4 | 'sort_field' => rgars( $return, 'settings/sort_field' ), |
|
269 | 4 | 'sort_direction' => rgars( $return, 'settings/sort_direction' ), |
|
270 | 4 | 'offset' => rgars( $return, 'settings/offset' ), |
|
271 | ); |
||
272 | |||
273 | 4 | unset( $return['settings']['page_size'], $return['settings']['sort_field'], $return['settings']['sort_direction'] ); |
|
274 | |||
275 | // Redact for non-logged ins |
||
276 | 4 | if ( ! \GVCommon::has_cap( 'edit_others_gravityviews' ) ) { |
|
277 | 4 | unset( $return['settings'] ); |
|
278 | 4 | unset( $return['search_criteria'] ); |
|
279 | } |
||
280 | |||
281 | 4 | if ( ! \GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) { |
|
282 | 4 | unset( $return['form'] ); |
|
283 | } |
||
284 | |||
285 | 4 | return $return; |
|
286 | } |
||
287 | |||
288 | 5 | public function get_item_permissions_check( $request ) { |
|
338 | |||
339 | 2 | public function get_sub_item_permissions_check( $request ) { |
|
340 | // Accessing a single entry needs the View access permissions. |
||
341 | 2 | if ( is_wp_error( $error = $this->get_items_permissions_check( $request ) ) ) { |
|
342 | return $error; |
||
343 | } |
||
344 | |||
377 | |||
378 | 4 | public function get_items_permissions_check( $request ) { |
|
382 | |||
383 | 3 | public function get_sub_items_permissions_check( $request ) { |
|
387 | } |
||
388 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.