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 | 8 | 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 | 8 | $allowed = array(); |
|
| 116 | 8 | foreach ( $view->fields->by_position( "{$context}_*" )->by_visible( $view )->all() as $field ) { |
|
| 117 | 8 | $allowed[] = $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 The entry. |
||
| 125 | * @param \WP_REST_Request $request Request object. |
||
| 126 | * @param string $context The context (directory, single) |
||
| 127 | */ |
||
| 128 | 8 | $allowed_field_ids = apply_filters( 'gravityview/rest/entry/fields', wp_list_pluck( $allowed, 'ID' ), $view, $entry, $request, $context ); |
|
| 129 | |||
| 130 | $allowed = array_filter( $allowed, function( $field ) use ( $allowed_field_ids ) { |
||
| 131 | 8 | return in_array( $field->ID, $allowed_field_ids, true ); |
|
| 132 | 8 | } ); |
|
| 133 | |||
| 134 | // Tack on additional fields if needed |
||
| 135 | 8 | foreach ( array_diff( $allowed_field_ids, wp_list_pluck( $allowed, 'ID' ) ) as $field_id ) { |
|
| 136 | 2 | $allowed[] = is_numeric( $field_id ) ? \GV\GF_Field::by_id( $view->form, $field_id ) : \GV\Internal_Field::by_id( $field_id ); |
|
| 137 | } |
||
| 138 | |||
| 139 | 8 | $r = new Request( $request ); |
|
| 140 | 8 | $return = array(); |
|
| 141 | |||
| 142 | 8 | $renderer = new \GV\Field_Renderer(); |
|
| 143 | |||
| 144 | 8 | $used_ids = array(); |
|
| 145 | |||
| 146 | 8 | foreach ( $allowed as $field ) { |
|
| 147 | 8 | $source = is_numeric( $field->ID ) ? $view->form : new \GV\Internal_Source(); |
|
| 148 | |||
| 149 | 8 | $field_id = $field->ID; |
|
| 150 | 8 | $index = null; |
|
| 151 | |||
| 152 | 8 | if ( ! isset( $used_ids[ $field_id ] ) ) { |
|
| 153 | 8 | $used_ids[ $field_id ] = 0; |
|
| 154 | } else { |
||
| 155 | 1 | $index = ++$used_ids[ $field_id ]; |
|
| 156 | } |
||
| 157 | |||
| 158 | 8 | if ( $index ) { |
|
| 159 | /** |
||
| 160 | * Modify non-unique IDs (custom, id, etc.) to be unique and not gobbled up. |
||
| 161 | */ |
||
| 162 | 1 | $field_id = sprintf( '%s(%d)', $field_id, $index + 1 ); |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @filter `gravityview/api/field/key` Filter the key name in the results for JSON output. |
||
| 167 | * @param[in,out] string $field_id The ID. Should be unique or keys will be gobbled up. |
||
| 168 | * @param \GV\View $view The view. |
||
| 169 | * @param \GV\Entry $entry The entry. |
||
| 170 | * @param \WP_REST_Request $request Request object. |
||
| 171 | * @param string $context The context (directory, single) |
||
| 172 | */ |
||
| 173 | 8 | $field_id = apply_filters( 'gravityview/api/field/key', $field_id, $view, $entry, $request, $context ); |
|
| 174 | |||
| 175 | 8 | if ( ! $class && in_array( $field->ID, array( 'custom' ) ) ) { |
|
|
|
|||
| 176 | /** |
||
| 177 | * Custom fields (and perhaps some others) will require rendering as they don't |
||
| 178 | * contain an intrinsic value (for custom their value is stored in the view and requires a renderer). |
||
| 179 | * We force the CSV template to take over in such cases, it's good enough for most cases. |
||
| 180 | */ |
||
| 181 | 2 | $return[ $field_id ] = $renderer->render( $field, $view, $source, $entry, $r, '\GV\Field_CSV_Template' ); |
|
| 182 | 8 | } else if ( $class ) { |
|
| 183 | 4 | $return[ $field_id ] = $renderer->render( $field, $view, $source, $entry, $r, $class ); |
|
| 184 | } else { |
||
| 185 | 6 | switch ( $field->type ): |
|
| 186 | 6 | case 'list': |
|
| 187 | 1 | $return[ $field_id ] = unserialize( $field->get_value( $view, $source, $entry, $r ) ); |
|
| 188 | 1 | break; |
|
| 189 | 6 | case 'fileupload': |
|
| 190 | 6 | case 'business_hours': |
|
| 191 | 1 | $return[ $field_id ] = json_decode( $field->get_value( $view, $source, $entry, $r ) ); |
|
| 192 | 1 | break; |
|
| 193 | default; |
||
| 194 | 6 | $return[ $field_id ] = $field->get_value( $view, $source, $entry, $r ); |
|
| 195 | endswitch; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | 8 | return $return; |
|
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get entries from a view |
||
| 204 | * |
||
| 205 | * Callback for /v1/views/{id}/entries/ |
||
| 206 | * |
||
| 207 | * @since 2.0 |
||
| 208 | * @param \WP_REST_Request $request Full data about the request. |
||
| 209 | * @return \WP_Error|\WP_REST_Response |
||
| 210 | */ |
||
| 211 | 6 | public function get_sub_items( $request ) { |
|
| 212 | |||
| 213 | 6 | $url = $request->get_url_params(); |
|
| 214 | 6 | $view_id = intval( $url['id'] ); |
|
| 215 | 6 | $format = \GV\Utils::get( $url, 'format', 'json' ); |
|
| 216 | |||
| 217 | 6 | if( $post_id = $request->get_param('post_id') ) { |
|
| 218 | global $post; |
||
| 219 | |||
| 220 | $post = get_post( $post_id ); |
||
| 221 | |||
| 222 | if ( ! $post || is_wp_error( $post ) ) { |
||
| 223 | return new \WP_Error( 'gravityview-post-not-found', sprintf( 'A post with ID #%d was not found.', $post_id ) ); |
||
| 224 | } |
||
| 225 | |||
| 226 | $collection = \GV\View_Collection::from_post( $post ); |
||
| 227 | |||
| 228 | if ( ! $collection->contains( $view_id ) ) { |
||
| 229 | 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 ) ); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | 6 | $view = \GV\View::by_id( $view_id ); |
|
| 234 | |||
| 235 | 6 | if ( 'html' === $format ) { |
|
| 236 | |||
| 237 | 1 | $renderer = new \GV\View_Renderer(); |
|
| 238 | 1 | $count = $total = 0; |
|
| 239 | |||
| 240 | /** @var \GV\Template_Context $context */ |
||
| 241 | add_action( 'gravityview/template/view/render', function( $context ) use ( &$count, &$total ) { |
||
| 242 | 1 | $count = $context->entries->count(); |
|
| 243 | 1 | $total = $context->entries->total(); |
|
| 244 | 1 | } ); |
|
| 245 | |||
| 246 | 1 | $output = $renderer->render( $view, new Request( $request ) ); |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @filter `gravityview/rest/entries/html/insert_meta` Whether to include `http-equiv` meta tags in the HTML output describing the data |
||
| 250 | * @since 2.0 |
||
| 251 | * @param bool $insert_meta Add <meta> tags? [Default: true] |
||
| 252 | * @param int $count The number of entries being rendered |
||
| 253 | * @param \GV\View $view The view. |
||
| 254 | * @param \WP_REST_Request $request Request object. |
||
| 255 | * @param int $total The number of total entries for the request |
||
| 256 | */ |
||
| 257 | 1 | $insert_meta = apply_filters( 'gravityview/rest/entries/html/insert_meta', true, $count, $view, $request, $total ); |
|
| 258 | |||
| 259 | 1 | if ( $insert_meta ) { |
|
| 260 | 1 | $output = '<meta http-equiv="X-Item-Count" content="' . $count . '" />' . $output; |
|
| 261 | 1 | $output = '<meta http-equiv="X-Item-Total" content="' . $total . '" />' . $output; |
|
| 262 | } |
||
| 263 | |||
| 264 | 1 | $response = new \WP_REST_Response( $output, 200 ); |
|
| 265 | 1 | $response->header( 'X-Item-Count', $count ); |
|
| 266 | 1 | $response->header( 'X-Item-Total', $total ); |
|
| 267 | |||
| 268 | 1 | return $response; |
|
| 269 | } |
||
| 270 | |||
| 271 | 6 | $entries = $view->get_entries( new Request( $request ) ); |
|
| 272 | |||
| 273 | 6 | if ( ! $entries->all() ) { |
|
| 274 | return new \WP_Error( 'gravityview-no-entries', __( 'No Entries found.', 'gravityview' ) ); |
||
| 275 | } |
||
| 276 | |||
| 277 | 6 | if ( 'csv' === $format ) { |
|
| 278 | 4 | ob_start(); |
|
| 279 | |||
| 280 | 4 | $csv = fopen( 'php://output', 'w' ); |
|
| 281 | |||
| 282 | /** Da' BOM :) */ |
||
| 283 | 4 | if ( apply_filters( 'gform_include_bom_export_entries', true, $view->form ? $view->form->form : null ) ) { |
|
| 284 | 4 | fputs( $csv, "\xef\xbb\xbf" ); |
|
| 285 | } |
||
| 286 | |||
| 287 | 4 | $headers_done = false; |
|
| 288 | |||
| 289 | 4 | foreach ( $entries->all() as $entry ) { |
|
| 290 | 4 | $entry = $this->prepare_entry_for_response( $view, $entry, $request, 'directory', '\GV\Field_CSV_Template' ); |
|
| 291 | |||
| 292 | 4 | if ( ! $headers_done ) { |
|
| 293 | 4 | $headers_done = fputcsv( $csv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), array_keys( $entry ) ) ); |
|
| 294 | } |
||
| 295 | |||
| 296 | 4 | fputcsv( $csv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), $entry ) ); |
|
| 297 | } |
||
| 298 | |||
| 299 | 4 | $response = new \WP_REST_Response( '', 200 ); |
|
| 300 | 4 | $response->header( 'X-Item-Count', $entries->count() ); |
|
| 301 | 4 | $response->header( 'X-Item-Total', $entries->total() ); |
|
| 302 | 4 | $response->header( 'Content-Type', 'text/csv' ); |
|
| 303 | |||
| 304 | 4 | fflush( $csv ); |
|
| 305 | |||
| 306 | 4 | $data = rtrim( ob_get_clean() ); |
|
| 307 | |||
| 308 | add_filter( 'rest_pre_serve_request', function() use ( $data ) { |
||
| 309 | echo $data; |
||
| 310 | return true; |
||
| 311 | 4 | } ); |
|
| 312 | |||
| 313 | 4 | if ( defined( 'DOING_GRAVITYVIEW_TESTS' ) && DOING_GRAVITYVIEW_TESTS ) { |
|
| 314 | 4 | echo $data; // rest_pre_serve_request is not called in tests |
|
| 315 | } |
||
| 316 | |||
| 317 | 4 | return $response; |
|
| 318 | } |
||
| 319 | |||
| 320 | 4 | $data = array( 'entries' => $entries->all(), 'total' => $entries->total() ); |
|
| 321 | |||
| 322 | 4 | foreach ( $data['entries'] as &$entry ) { |
|
| 323 | 4 | $entry = $this->prepare_entry_for_response( $view, $entry, $request, 'directory' ); |
|
| 324 | } |
||
| 325 | |||
| 326 | 4 | return new \WP_REST_Response( $data, 200 ); |
|
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get one entry from view |
||
| 331 | * |
||
| 332 | * Callback for /v1/views/{id}/entries/{id}/ |
||
| 333 | * |
||
| 334 | * @uses GVCommon::get_entry |
||
| 335 | * @since 2.0 |
||
| 336 | * @param \WP_REST_Request $request Full data about the request. |
||
| 337 | * @return \WP_Error|\WP_REST_Response |
||
| 338 | */ |
||
| 339 | 4 | public function get_sub_item( $request ) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Prepare the item for the REST response |
||
| 358 | * |
||
| 359 | * @since 2.0 |
||
| 360 | * @param \WP_Post $view_post WordPress representation of the item. |
||
| 361 | * @param \WP_REST_Request $request Request object. |
||
| 362 | * @return mixed |
||
| 363 | */ |
||
| 364 | 4 | public function prepare_view_for_response( $view_post, \WP_REST_Request $request ) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @param \WP_REST_Request $request |
||
| 410 | * |
||
| 411 | * @return bool|\WP_Error |
||
| 412 | */ |
||
| 413 | 8 | public function get_item_permissions_check( $request ) { |
|
| 456 | |||
| 457 | 4 | public function get_sub_item_permissions_check( $request ) { |
|
| 495 | |||
| 496 | 6 | public function get_items_permissions_check( $request ) { |
|
| 500 | |||
| 501 | 6 | public function get_sub_items_permissions_check( $request ) { |
|
| 505 | } |
||
| 506 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: