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 ) { |
|
| 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 ) { |
|
| 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 | * @param string $class The value renderer. Default: null (raw value) |
||
| 107 | * |
||
| 108 | * @since 2.1 Add value renderer override $class parameter. |
||
| 109 | * |
||
| 110 | * @return mixed The data that is sent. |
||
| 111 | */ |
||
| 112 | 5 | public function prepare_entry_for_response( $view, $entry, \WP_REST_Request $request, $context, $class = null ) { |
|
| 113 | |||
| 114 | // Only output the fields that should be displayed. |
||
| 115 | 5 | $allowed = array(); |
|
| 116 | 5 | foreach ( $view->fields->by_position( "{$context}_*" )->by_visible()->all() as $field ) { |
|
| 117 | 5 | $allowed[ $field->ID ] = $field; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @filter `gravityview/rest/entry/fields` Whitelist more entry fields that are output in regular REST requests. |
||
| 122 | * @param[in,out] array $allowed The allowed ones, default by_visible, by_position( "context_*" ), i.e. as set in the view. |
||
| 123 | * @param \GV\View $view The view. |
||
| 124 | * @param \GV\Entry $entry WordPress representation of the item. |
||
| 125 | * @param \WP_REST_Request $request Request object. |
||
| 126 | * @param string $context The context (directory, single) |
||
| 127 | */ |
||
| 128 | 5 | $allowed_field_ids = apply_filters( 'gravityview/rest/entry/fields', array_keys( $allowed ), $view, $entry, $request, $context ); |
|
| 129 | |||
| 130 | 5 | $r = new Request( $request ); |
|
| 131 | 5 | $return = array(); |
|
| 132 | 5 | $renderer = null; |
|
| 133 | |||
| 134 | 5 | if ( $class ) { |
|
| 135 | 2 | $renderer = new \GV\Field_Renderer(); |
|
| 136 | } |
||
| 137 | |||
| 138 | 5 | foreach ( $allowed_field_ids as $field_id ) { |
|
| 139 | 5 | $source = is_numeric( $field_id ) ? $view->form : new \GV\Internal_Source(); |
|
| 140 | |||
| 141 | 5 | if ( isset( $allowed[ $field_id ] ) ) { |
|
| 142 | 5 | $field = $allowed[ $field_id ]; |
|
| 143 | } else { |
||
| 144 | 2 | $field = is_numeric( $field_id ) ? \GV\GF_Field::by_id( $view->form, $field_id ) : \GV\Internal_Field::by_id( $field_id ); |
|
| 145 | } |
||
| 146 | |||
| 147 | 5 | if ( $class ) { |
|
| 148 | 2 | $return[ $field->ID ] = $renderer->render( $field, $view, $source, $entry, $r, $class ); |
|
| 149 | } else { |
||
| 150 | 5 | $return[ $field->ID ] = $field->get_value( $view, $source, $entry, $r ); |
|
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | 5 | return $return; |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get entries from a view |
||
| 159 | * |
||
| 160 | * Callback for /v1/views/{id}/entries/ |
||
| 161 | * |
||
| 162 | * @since 2.0 |
||
| 163 | * @param \WP_REST_Request $request Full data about the request. |
||
| 164 | * @return \WP_Error|\WP_REST_Response |
||
| 165 | */ |
||
| 166 | 4 | public function get_sub_items( $request ) { |
|
| 167 | |||
| 168 | 4 | $url = $request->get_url_params(); |
|
| 169 | 4 | $view_id = intval( $url['id'] ); |
|
| 170 | 4 | $format = \GV\Utils::get( $url, 'format', 'json' ); |
|
| 171 | |||
| 172 | 4 | if( $post_id = $request->get_param('post_id') ) { |
|
| 173 | global $post; |
||
| 174 | |||
| 175 | $post = get_post( $post_id ); |
||
| 176 | |||
| 177 | if ( ! $post || is_wp_error( $post ) ) { |
||
| 178 | return new \WP_Error( 'gravityview-post-not-found', sprintf( 'A post with ID #%d was not found.', $post_id ) ); |
||
| 179 | } |
||
| 180 | |||
| 181 | $collection = \GV\View_Collection::from_post( $post ); |
||
| 182 | |||
| 183 | if ( ! $collection->contains( $view_id ) ) { |
||
| 184 | return new \WP_Error( 'gravityview-post-not-contains', sprintf( 'The post with ID #%d does not contain a View with ID #%d', $post_id, $view_id ) ); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | 4 | $view = \GV\View::by_id( $view_id ); |
|
| 189 | |||
| 190 | 4 | if ( 'html' === $format ) { |
|
| 191 | |||
| 192 | 1 | $renderer = new \GV\View_Renderer(); |
|
| 193 | 1 | $count = $total = 0; |
|
| 194 | |||
| 195 | /** @var \GV\Template_Context $context */ |
||
| 196 | 1 | add_action( 'gravityview/template/view/render', function( $context ) use ( &$count, &$total ) { |
|
| 197 | 1 | $count = $context->entries->count(); |
|
| 198 | 1 | $total = $context->entries->total(); |
|
| 199 | 1 | } ); |
|
| 200 | |||
| 201 | 1 | $output = $renderer->render( $view, new Request( $request ) ); |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @filter `gravityview/rest/entries/html/insert_meta` Whether to include `http-equiv` meta tags in the HTML output describing the data |
||
| 205 | * @since 2.0 |
||
| 206 | * @param bool $insert_meta Add <meta> tags? [Default: true] |
||
| 207 | * @param int $count The number of entries being rendered |
||
| 208 | * @param \GV\View $view The view. |
||
| 209 | * @param \WP_REST_Request $request Request object. |
||
| 210 | * @param int $total The number of total entries for the request |
||
| 211 | */ |
||
| 212 | 1 | $insert_meta = apply_filters( 'gravityview/rest/entries/html/insert_meta', true, $count, $view, $request, $total ); |
|
| 213 | |||
| 214 | 1 | if ( $insert_meta ) { |
|
| 215 | 1 | $output = '<meta http-equiv="X-Item-Count" content="' . $count . '" />' . $output; |
|
| 216 | 1 | $output = '<meta http-equiv="X-Item-Total" content="' . $total . '" />' . $output; |
|
| 217 | } |
||
| 218 | |||
| 219 | 1 | $response = new \WP_REST_Response( $output, 200 ); |
|
| 220 | 1 | $response->header( 'X-Item-Count', $count ); |
|
| 221 | 1 | $response->header( 'X-Item-Total', $total ); |
|
| 222 | |||
| 223 | 1 | return $response; |
|
| 224 | } |
||
| 225 | |||
| 226 | 4 | $entries = $view->get_entries( new Request( $request ) ); |
|
| 227 | |||
| 228 | 4 | if ( ! $entries->all() ) { |
|
| 229 | return new \WP_Error( 'gravityview-no-entries', __( 'No Entries found.', 'gravityview' ) ); |
||
| 230 | } |
||
| 231 | |||
| 232 | 4 | if ( 'csv' === $format ) { |
|
| 233 | 2 | ob_start(); |
|
| 234 | |||
| 235 | 2 | $csv = fopen( 'php://output', 'w' ); |
|
| 236 | |||
| 237 | /** Da' BOM :) */ |
||
| 238 | 2 | if ( apply_filters( 'gform_include_bom_export_entries', true, $view->form ? $view->form->form : null ) ) { |
|
| 239 | 2 | fputs( $csv, "\xef\xbb\xbf" ); |
|
| 240 | } |
||
| 241 | |||
| 242 | 2 | $headers_done = false; |
|
| 243 | |||
| 244 | 2 | foreach ( $entries->all() as $entry ) { |
|
| 245 | 2 | $entry = $this->prepare_entry_for_response( $view, $entry, $request, 'directory', '\GV\Field_CSV_Template' ); |
|
| 246 | |||
| 247 | 2 | if ( ! $headers_done ) { |
|
| 248 | 2 | $headers_done = fputcsv( $csv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), array_keys( $entry ) ) ); |
|
| 249 | } |
||
| 250 | |||
| 251 | 2 | fputcsv( $csv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), $entry ) ); |
|
| 252 | } |
||
| 253 | |||
| 254 | 2 | $response = new \WP_REST_Response( rtrim( ob_get_clean() ), 200 ); |
|
| 255 | 2 | $response->header( 'X-Item-Count', $entries->count() ); |
|
| 256 | 2 | $response->header( 'X-Item-Total', $entries->total() ); |
|
| 257 | |||
| 258 | 2 | return $response; |
|
| 259 | } |
||
| 260 | |||
| 261 | 3 | $data = array( 'entries' => $entries->all(), 'total' => $entries->total() ); |
|
| 262 | |||
| 263 | 3 | foreach ( $data['entries'] as &$entry ) { |
|
| 264 | 3 | $entry = $this->prepare_entry_for_response( $view, $entry, $request, 'directory' ); |
|
| 265 | } |
||
| 266 | |||
| 267 | 3 | return new \WP_REST_Response( $data, 200 ); |
|
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get one entry from view |
||
| 272 | * |
||
| 273 | * Callback for /v1/views/{id}/entries/{id}/ |
||
| 274 | * |
||
| 275 | * @uses GVCommon::get_entry |
||
| 276 | * @since 2.0 |
||
| 277 | * @param \WP_REST_Request $request Full data about the request. |
||
| 278 | * @return \WP_Error|\WP_REST_Response |
||
| 279 | */ |
||
| 280 | 2 | public function get_sub_item( $request ) { |
|
| 281 | 2 | $url = $request->get_url_params(); |
|
| 282 | 2 | $view_id = intval( $url['id'] ); |
|
| 283 | 2 | $entry_id = intval( $url['s_id'] ); |
|
| 284 | 2 | $format = \GV\Utils::get( $url, 'format', 'json' ); |
|
| 285 | |||
| 286 | 2 | $view = \GV\View::by_id( $view_id ); |
|
| 287 | 2 | $entry = \GV\GF_Entry::by_id( $entry_id ); |
|
| 288 | |||
| 289 | 2 | if ( $format === 'html' ) { |
|
| 290 | 1 | $renderer = new \GV\Entry_Renderer(); |
|
| 291 | 1 | return $renderer->render( $entry, $view, new Request( $request ) ); |
|
| 292 | } |
||
| 293 | |||
| 294 | 2 | return $this->prepare_entry_for_response( $view, $entry, $request, 'single' ); |
|
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Prepare the item for the REST response |
||
| 299 | * |
||
| 300 | * @since 2.0 |
||
| 301 | * @param \WP_Post $view_post WordPress representation of the item. |
||
| 302 | * @param \WP_REST_Request $request Request object. |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | 4 | public function prepare_view_for_response( $view_post, \WP_REST_Request $request ) { |
|
| 306 | 4 | if ( is_wp_error( $this->get_item_permissions_check( $request, $view_post->ID ) ) ) { |
|
| 307 | // Redacted out view. |
||
| 308 | 1 | return array( 'ID' => $view_post->ID, 'post_content' => __( 'You are not allowed to access this content.', 'gravityview' ) ); |
|
| 309 | } |
||
| 310 | |||
| 311 | 4 | $view = \GV\View::from_post( $view_post ); |
|
| 312 | |||
| 313 | 4 | $item = $view->as_data(); |
|
| 314 | |||
| 315 | // Add all the WP_Post data |
||
| 316 | 4 | $view_post = $view_post->to_array(); |
|
| 317 | |||
| 318 | 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'] ); |
|
| 319 | |||
| 320 | 4 | $return = wp_parse_args( $item, $view_post ); |
|
| 321 | |||
| 322 | 4 | $return['title'] = $return['post_title']; |
|
| 323 | |||
| 324 | 4 | $return['settings'] = isset( $return['atts'] ) ? $return['atts'] : array(); |
|
| 325 | 4 | unset( $return['atts'], $return['view_id'] ); |
|
| 326 | |||
| 327 | 4 | $return['search_criteria'] = array( |
|
| 328 | 4 | 'page_size' => rgars( $return, 'settings/page_size' ), |
|
| 329 | 4 | 'sort_field' => rgars( $return, 'settings/sort_field' ), |
|
| 330 | 4 | 'sort_direction' => rgars( $return, 'settings/sort_direction' ), |
|
| 331 | 4 | 'offset' => rgars( $return, 'settings/offset' ), |
|
| 332 | ); |
||
| 333 | |||
| 334 | 4 | unset( $return['settings']['page_size'], $return['settings']['sort_field'], $return['settings']['sort_direction'] ); |
|
| 335 | |||
| 336 | // Redact for non-logged ins |
||
| 337 | 4 | if ( ! \GVCommon::has_cap( 'edit_others_gravityviews' ) ) { |
|
| 338 | 4 | unset( $return['settings'] ); |
|
| 339 | 4 | unset( $return['search_criteria'] ); |
|
| 340 | } |
||
| 341 | |||
| 342 | 4 | if ( ! \GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) { |
|
| 343 | 4 | unset( $return['form'] ); |
|
| 344 | } |
||
| 345 | |||
| 346 | 4 | return $return; |
|
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param \WP_REST_Request $request |
||
| 351 | * |
||
| 352 | * @return bool|\WP_Error |
||
| 353 | */ |
||
| 354 | 6 | public function get_item_permissions_check( $request ) { |
|
| 355 | 6 | if ( func_num_args() === 2 ) { |
|
| 356 | 4 | $view_id = func_get_arg( 1 ); // $view_id override |
|
| 357 | } else { |
||
| 358 | 5 | $url = $request->get_url_params(); |
|
| 359 | 5 | $view_id = intval( $url['id'] ); |
|
| 360 | } |
||
| 361 | |||
| 362 | 6 | if ( ! $view = \GV\View::by_id( $view_id ) ) { |
|
| 363 | return new \WP_Error( 'rest_forbidden', __( 'You are not allowed to access this content.', 'gravityview' ) ); |
||
| 364 | } |
||
| 365 | |||
| 366 | 6 | while ( $error = $view->can_render( array( 'rest' ), $request ) ) { |
|
| 367 | |||
| 368 | 6 | if ( ! is_wp_error( $error ) ) { |
|
| 369 | 6 | break; |
|
| 370 | } |
||
| 371 | |||
| 372 | 1 | switch ( str_replace( 'gravityview/', '', $error->get_error_code() ) ) { |
|
| 373 | 1 | case 'rest_disabled': |
|
| 374 | 1 | case 'post_password_required': |
|
| 375 | 1 | case 'not_public': |
|
| 376 | case 'embed_only': |
||
| 377 | case 'no_direct_access': |
||
| 378 | 1 | return new \WP_Error( 'rest_forbidden', __( 'You are not allowed to access this content.', 'gravityview' ) ); |
|
| 379 | case 'no_form_attached': |
||
| 380 | return new \WP_Error( 'rest_forbidden', __( 'This View is not configured properly.', 'gravityview' ) ); |
||
| 381 | default: |
||
| 382 | return new \WP_Error( 'rest_forbidden', __( 'You are not allowed to access this content.', 'gravityview' ) ); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @filter `gravityview/view/output/rest` Disable rest output. Final chance. |
||
| 388 | * @param[in,out] bool Enable or not. |
||
| 389 | * @param \GV\View $view The view. |
||
| 390 | */ |
||
| 391 | 6 | if ( ! apply_filters( 'gravityview/view/output/rest', true, $view ) ) { |
|
| 392 | 1 | return new \WP_Error( 'rest_forbidden', __( 'You are not allowed to access this content.', 'gravityview' ) ); |
|
| 393 | } |
||
| 394 | |||
| 395 | 6 | return true; |
|
| 396 | } |
||
| 397 | |||
| 398 | 2 | public function get_sub_item_permissions_check( $request ) { |
|
| 436 | |||
| 437 | 4 | public function get_items_permissions_check( $request ) { |
|
| 441 | |||
| 442 | 4 | public function get_sub_items_permissions_check( $request ) { |
|
| 446 | } |
||
| 447 |
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.