Complex classes like GravityView_Admin_Views 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 GravityView_Admin_Views, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class GravityView_Admin_Views { |
||
20 | |||
21 | |||
22 | |||
23 | function __construct() { |
||
61 | |||
62 | /** |
||
63 | * @since 1.15 |
||
64 | * @param WP_Query $query |
||
65 | */ |
||
66 | public function filter_pre_get_posts_by_gravityview_form_id( &$query ) { |
||
86 | |||
87 | function add_view_dropdown() { |
||
106 | |||
107 | |||
108 | /** |
||
109 | * @deprecated since 1.2 |
||
110 | * Start using GravityView_Render_Settings::render_setting_row |
||
111 | */ |
||
112 | public static function render_setting_row( $key = '', $current_settings = array(), $override_input = null, $name = 'template_settings[%s]', $id = 'gravityview_se_%s' ) { |
||
116 | |||
117 | /** |
||
118 | * @deprecated since 1.2 |
||
119 | * Start using GravityView_Render_Settings::render_field_option |
||
120 | */ |
||
121 | public static function render_field_option( $name = '', $option, $curr_value = NULL ) { |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Add a GravityView menu to the Form Toolbar with connected views |
||
129 | * @param array $menu_items Menu items, as set in GFForms::top_toolbar() |
||
130 | * @param int $id ID of the current Gravity form |
||
131 | * @return array Modified array |
||
132 | */ |
||
133 | static function gform_toolbar_menu( $menu_items = array(), $id = NULL ) { |
||
183 | |||
184 | /** |
||
185 | * List the field types without presentation properties (on a View context) |
||
186 | * |
||
187 | * @param array $array Existing field types to add to a blacklist |
||
188 | * @param string|null $context Context for the blacklist. Default: NULL. |
||
189 | * @access public |
||
190 | * @return array Default blacklist fields merged with existing blacklist fields |
||
191 | */ |
||
192 | function default_field_blacklist( $array = array(), $context = NULL ) { |
||
205 | |||
206 | /** |
||
207 | * Add tooltip text for use throughout the UI |
||
208 | * @param array $tooltips Array of Gravity Forms tooltips |
||
209 | * @return array Modified tooltips array |
||
210 | */ |
||
211 | public function tooltips( $tooltips = array() ) { |
||
264 | |||
265 | /** |
||
266 | * Add the Data Source information |
||
267 | * |
||
268 | * @param null $column_name |
||
269 | * @param $post_id |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | public function add_custom_column_content( $column_name = NULL, $post_id ) { |
||
274 | |||
275 | $output = ''; |
||
276 | |||
277 | switch ( $column_name ) { |
||
278 | case 'gv_template': |
||
279 | |||
280 | $template_id = gravityview_get_template_id( $post_id ); |
||
281 | |||
282 | // All Views should have a connected form. If it doesn't, that's not right. |
||
283 | if ( empty( $template_id ) ) { |
||
284 | do_action( 'gravityview_log_error', sprintf( __METHOD__ . ' View ID %s does not have a connected template.', $post_id ) ); |
||
285 | break; |
||
286 | } |
||
287 | |||
288 | $templates = gravityview_get_registered_templates(); |
||
289 | |||
290 | $template = isset( $templates[ $template_id ] ) ? $templates[ $template_id ] : false; |
||
291 | |||
292 | // Generate backup if label doesn't exist: `example_name` => `Example Name` |
||
293 | $template_id_pretty = ucwords( implode( ' ', explode( '_', $template_id ) ) ); |
||
294 | |||
295 | $output = $template ? $template['label'] : $template_id_pretty; |
||
296 | |||
297 | break; |
||
298 | |||
299 | case 'gv_connected_form': |
||
300 | |||
301 | $form_id = gravityview_get_form_id( $post_id ); |
||
302 | |||
303 | // All Views should have a connected form. If it doesn't, that's not right. |
||
304 | if ( empty( $form_id ) ) { |
||
305 | do_action( 'gravityview_log_error', sprintf( '[add_data_source_column_content] View ID %s does not have a connected GF form.', $post_id ) ); |
||
306 | $output = __( 'Not connected.', 'gravityview' ); |
||
307 | break; |
||
308 | } |
||
309 | |||
310 | $form = gravityview_get_form( $form_id ); |
||
311 | |||
312 | if ( ! $form ) { |
||
313 | do_action( 'gravityview_log_error', sprintf( '[add_data_source_column_content] Connected form not found: Form #%d', $form_id ) ); |
||
314 | |||
315 | $output = __( 'The connected form can not be found; it may no longer exist.', 'gravityview' ); |
||
316 | } else { |
||
317 | $output = self::get_connected_form_links( $form ); |
||
318 | } |
||
319 | |||
320 | break; |
||
321 | } |
||
322 | |||
323 | echo $output; |
||
324 | } |
||
325 | |||
326 | |||
327 | /** |
||
328 | * Get HTML links relating to a connected form, like Edit, Entries, Settings, Preview |
||
329 | * @param array|int $form Gravity Forms forms array, or the form ID |
||
330 | * @param boolean $include_form_link Whether to include the bold name of the form in the output |
||
331 | * @return string HTML links |
||
332 | */ |
||
333 | static public function get_connected_form_links( $form, $include_form_link = true ) { |
||
389 | |||
390 | /** |
||
391 | * Add the Data Source column to the Views page |
||
392 | * @param array $columns Columns array |
||
393 | */ |
||
394 | public function add_post_type_columns( $columns ) { |
||
422 | |||
423 | /** |
||
424 | * Save View configuration |
||
425 | * |
||
426 | * @access public |
||
427 | * @param int $post_id Currently saved Post ID |
||
428 | * @return void |
||
429 | */ |
||
430 | function save_postdata( $post_id ) { |
||
431 | |||
432 | if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){ |
||
433 | return; |
||
434 | } |
||
435 | |||
436 | // validate post_type |
||
437 | if ( ! isset( $_POST['post_type'] ) || 'gravityview' != $_POST['post_type'] ) { |
||
438 | return; |
||
439 | } |
||
440 | |||
441 | // validate user can edit and save View |
||
442 | if ( ! GVCommon::has_cap( 'edit_gravityview', $post_id ) ) { |
||
443 | do_action( 'gravityview_log_error', __METHOD__ . ' - Current user does not have the capability to edit View #' . $post_id, wp_get_current_user() ); |
||
444 | return; |
||
445 | } |
||
446 | |||
447 | do_action( 'gravityview_log_debug', '[save_postdata] Saving View post type.', $_POST ); |
||
448 | |||
449 | $statii = array(); |
||
450 | |||
451 | // check if this is a start fresh View |
||
452 | if ( isset( $_POST['gravityview_select_form_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_form_nonce'], 'gravityview_select_form' ) ) { |
||
453 | |||
454 | $form_id = !empty( $_POST['gravityview_form_id'] ) ? $_POST['gravityview_form_id'] : ''; |
||
455 | // save form id |
||
456 | $statii['form_id'] = update_post_meta( $post_id, '_gravityview_form_id', $form_id ); |
||
457 | |||
458 | } |
||
459 | |||
460 | if( false === GVCommon::has_cap( 'gravityforms_create_form' ) && empty( $statii['form_id'] ) ) { |
||
461 | do_action( 'gravityview_log_error', __METHOD__ . ' - Current user does not have the capability to create a new Form.', wp_get_current_user() ); |
||
462 | return; |
||
463 | } |
||
464 | |||
465 | // Was this a start fresh? |
||
466 | if ( ! empty( $_POST['gravityview_form_id_start_fresh'] ) ) { |
||
467 | $statii['start_fresh'] = add_post_meta( $post_id, '_gravityview_start_fresh', 1 ); |
||
468 | } else { |
||
469 | $statii['start_fresh'] = delete_post_meta( $post_id, '_gravityview_start_fresh' ); |
||
470 | } |
||
471 | |||
472 | // Check if we have a template id |
||
473 | if ( isset( $_POST['gravityview_select_template_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_template_nonce'], 'gravityview_select_template' ) ) { |
||
474 | |||
475 | $template_id = !empty( $_POST['gravityview_directory_template'] ) ? $_POST['gravityview_directory_template'] : ''; |
||
476 | |||
477 | // now save template id |
||
478 | $statii['directory_template'] = update_post_meta( $post_id, '_gravityview_directory_template', $template_id ); |
||
479 | } |
||
480 | |||
481 | |||
482 | // save View Configuration metabox |
||
483 | if ( isset( $_POST['gravityview_view_configuration_nonce'] ) && wp_verify_nonce( $_POST['gravityview_view_configuration_nonce'], 'gravityview_view_configuration' ) ) { |
||
484 | |||
485 | // template settings |
||
486 | if( empty( $_POST['template_settings'] ) ) { |
||
487 | $_POST['template_settings'] = array(); |
||
488 | } |
||
489 | $statii['template_settings'] = update_post_meta( $post_id, '_gravityview_template_settings', $_POST['template_settings'] ); |
||
490 | |||
491 | $fields = array(); |
||
492 | |||
493 | // Directory&single Visible Fields |
||
494 | if( !empty( $preset_fields ) ) { |
||
495 | |||
496 | $fields = $preset_fields; |
||
497 | |||
498 | } elseif( !empty( $_POST['fields'] ) ) { |
||
499 | $fields = _gravityview_process_posted_fields(); |
||
500 | } |
||
501 | |||
502 | $statii['directory_fields'] = update_post_meta( $post_id, '_gravityview_directory_fields', $fields ); |
||
503 | |||
504 | // Directory Visible Widgets |
||
505 | if( empty( $_POST['widgets'] ) ) { |
||
506 | $_POST['widgets'] = array(); |
||
507 | } |
||
508 | $statii['directory_widgets'] = update_post_meta( $post_id, '_gravityview_directory_widgets', $_POST['widgets'] ); |
||
509 | |||
510 | } // end save view configuration |
||
511 | |||
512 | do_action('gravityview_log_debug', '[save_postdata] Update Post Meta Statuses (also returns false if nothing changed)', array_map( 'intval', $statii ) ); |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * @deprecated 1.1.6 |
||
517 | */ |
||
518 | function render_label() { |
||
519 | _deprecated_function( 'GravityView_Admin_Views::render_label()', '1.1.6', 'Use the GravityView_Admin_View_Field class instead.' ); |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * Render html for displaying available fields based on a Form ID |
||
524 | * $blacklist_field_types - contains the field types which are not proper to be shown in a directory. |
||
525 | * |
||
526 | * @access public |
||
527 | * @param int $form Gravity Forms Form ID (default: '') |
||
528 | * @param string $context (default: 'single') |
||
529 | * @return void |
||
530 | */ |
||
531 | function render_available_fields( $form = 0, $context = 'single' ) { |
||
571 | |||
572 | /** |
||
573 | * Render html for displaying additional fields based on a Form ID |
||
574 | * |
||
575 | * @access public |
||
576 | * @param int $form Gravity Forms Form ID (default: '') |
||
577 | * @param string $context (default: 'single') |
||
578 | * @return void |
||
579 | */ |
||
580 | public function render_additional_fields( $form = 0, $context = 'single' ) { |
||
624 | |||
625 | /** |
||
626 | * Retrieve the default fields id, label and type |
||
627 | * @param string|array $form form_ID or form object |
||
628 | * @param string $zone Either 'single', 'directory', 'header', 'footer' |
||
629 | * @return array |
||
630 | */ |
||
631 | function get_entry_default_fields($form, $zone) { |
||
632 | |||
633 | $entry_default_fields = array(); |
||
634 | |||
635 | if( in_array( $zone, array( 'directory', 'single' ) ) ) { |
||
636 | |||
637 | $entry_default_fields = array( |
||
638 | 'id' => array( |
||
639 | 'label' => __('Entry ID', 'gravityview'), |
||
640 | 'type' => 'id', |
||
641 | 'desc' => __('The unique ID of the entry.', 'gravityview'), |
||
642 | ), |
||
643 | 'date_created' => array( |
||
644 | 'label' => __('Entry Date', 'gravityview'), |
||
645 | 'desc' => __('The date the entry was created.', 'gravityview'), |
||
646 | 'type' => 'date_created', |
||
647 | ), |
||
648 | 'source_url' => array( |
||
649 | 'label' => __('Source URL', 'gravityview'), |
||
650 | 'type' => 'source_url', |
||
651 | 'desc' => __('The URL of the page where the form was submitted.', 'gravityview'), |
||
652 | ), |
||
653 | 'ip' => array( |
||
654 | 'label' => __('User IP', 'gravityview'), |
||
655 | 'type' => 'ip', |
||
656 | 'desc' => __('The IP Address of the user who created the entry.', 'gravityview'), |
||
657 | ), |
||
658 | 'created_by' => array( |
||
659 | 'label' => __('User', 'gravityview'), |
||
660 | 'type' => 'created_by', |
||
661 | 'desc' => __('Details of the logged-in user who created the entry (if any).', 'gravityview'), |
||
662 | ), |
||
663 | |||
664 | /** |
||
665 | * @since 1.2 |
||
666 | */ |
||
667 | 'custom' => array( |
||
668 | 'label' => __('Custom Content', 'gravityview'), |
||
669 | 'type' => 'custom', |
||
670 | 'desc' => __('Insert custom text or HTML.', 'gravityview'), |
||
671 | ), |
||
672 | |||
673 | /** |
||
674 | * @since 1.7.2 |
||
675 | */ |
||
676 | 'other_entries' => array( |
||
677 | 'label' => __('Other Entries', 'gravityview'), |
||
678 | 'type' => 'other_entries', |
||
679 | 'desc' => __('Display other entries created by the entry creator.', 'gravityview'), |
||
680 | ), |
||
681 | ); |
||
682 | |||
683 | if( 'single' !== $zone) { |
||
684 | |||
685 | $entry_default_fields['entry_link'] = array( |
||
686 | 'label' => __('Link to Entry', 'gravityview'), |
||
687 | 'desc' => __('A dedicated link to the single entry with customizable text.', 'gravityview'), |
||
688 | 'type' => 'entry_link', |
||
689 | ); |
||
690 | } |
||
1 ignored issue
–
show
|
|||
691 | |||
692 | } // if not zone directory or single |
||
693 | |||
694 | |||
695 | /** |
||
696 | * @filter `gravityview_entry_default_fields` Modify the default fields for each zone and context |
||
697 | * @param array $entry_default_fields Array of fields shown by default |
||
698 | * @param string|array $form form_ID or form object |
||
699 | * @param string $zone Either 'single', 'directory', 'header', 'footer' |
||
700 | */ |
||
701 | return apply_filters( 'gravityview_entry_default_fields', $entry_default_fields, $form, $zone); |
||
702 | } |
||
703 | |||
704 | /** |
||
705 | * Calculate the available fields |
||
706 | * @param string|array $form form_ID or form object |
||
707 | * @param string $zone Either 'single', 'directory', 'header', 'footer' |
||
708 | * @return array fields |
||
709 | */ |
||
710 | function get_available_fields( $form = '', $zone = NULL ) { |
||
735 | |||
736 | |||
737 | /** |
||
738 | * Render html for displaying available widgets |
||
739 | * @return string html |
||
740 | */ |
||
741 | function render_available_widgets() { |
||
755 | |||
756 | /** |
||
757 | * Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object |
||
758 | * @since 1.13.1 |
||
759 | * @return array |
||
760 | */ |
||
761 | function get_registered_widgets() { |
||
770 | |||
771 | /** |
||
772 | * Generic function to render rows and columns of active areas for widgets & fields |
||
773 | * @param string $template_id The current slug of the selected View template |
||
774 | * @param string $type Either 'widget' or 'field' |
||
775 | * @param string $zone Either 'single', 'directory', 'header', 'footer' |
||
776 | * @param array $rows The layout structure: rows, columns and areas |
||
777 | * @param array $values Saved objects |
||
778 | * @return void |
||
779 | */ |
||
780 | function render_active_areas( $template_id, $type, $zone, $rows, $values ) { |
||
882 | |||
883 | /** |
||
884 | * Render the widget active areas |
||
885 | * @param string $zone Either 'header' or 'footer' |
||
886 | * @param string $post_id Current Post ID (view) |
||
887 | * @return string html |
||
888 | */ |
||
889 | function render_widgets_active_areas( $template_id = '', $zone, $post_id = '' ) { |
||
913 | |||
914 | /** |
||
915 | * Render the Template Active Areas and configured active fields for a given template id and post id |
||
916 | * |
||
917 | * @access public |
||
918 | * @param string $template_id (default: '') |
||
919 | * @param string $post_id (default: '') |
||
920 | * @param string $context (default: 'single') |
||
921 | * @return string HTML of the active areas |
||
922 | */ |
||
923 | function render_directory_active_areas( $template_id = '', $context = 'single', $post_id = '', $echo = false ) { |
||
958 | |||
959 | /** |
||
960 | * Enqueue scripts and styles at Views editor |
||
961 | * |
||
962 | * @access public |
||
963 | * @param mixed $hook |
||
964 | * @return void |
||
965 | */ |
||
966 | static function add_scripts_and_styles( $hook ) { |
||
1011 | |||
1012 | static function enqueue_gravity_forms_scripts() { |
||
1031 | |||
1032 | function register_no_conflict( $registered ) { |
||
1047 | |||
1048 | |||
1049 | } |
||
1050 | |||
1052 |
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.