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 ) { |
||
134 | |||
135 | $connected_views = gravityview_get_connected_views( $id ); |
||
136 | |||
137 | if( empty( $connected_views ) ) { |
||
138 | |||
139 | $menu_items['gravityview'] = array( |
||
140 | 'label' => esc_attr__( 'Create a View', 'gravityview' ), |
||
141 | 'icon' => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>', |
||
142 | 'title' => esc_attr__( 'Create a View using this form as a data source', 'gravityview' ), |
||
143 | 'url' => admin_url( 'post-new.php?post_type=gravityview&form_id=' . $id ), |
||
144 | 'menu_class' => 'gv_connected_forms gf_form_toolbar_settings', |
||
145 | 'priority' => 0, |
||
146 | 'capabilities' => array( 'edit_gravityviews' ), |
||
147 | ); |
||
148 | |||
149 | return $menu_items; |
||
150 | } |
||
151 | |||
152 | $sub_menu_items = array(); |
||
153 | foreach ( (array)$connected_views as $view ) { |
||
154 | |||
155 | if( ! GVCommon::has_cap( 'edit_gravityview', $view->ID ) ) { |
||
156 | continue; |
||
157 | } |
||
158 | |||
159 | $label = empty( $view->post_title ) ? sprintf( __('No Title (View #%d)', 'gravityview' ), $view->ID ) : $view->post_title; |
||
160 | |||
161 | $sub_menu_items[] = array( |
||
162 | 'label' => esc_attr( $label ), |
||
163 | 'url' => admin_url( 'post.php?action=edit&post='.$view->ID ), |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | // If there were no items added, then let's create the parent menu |
||
168 | if( $sub_menu_items ) { |
||
169 | |||
170 | $sub_menu_items[] = array( |
||
171 | 'label' => esc_attr__( 'Create a View', 'gravityview' ), |
||
172 | 'link_class' => 'gv-create-view', |
||
173 | 'title' => esc_attr__( 'Create a View using this form as a data source', 'gravityview' ), |
||
174 | 'url' => admin_url( 'post-new.php?post_type=gravityview&form_id=' . $id ), |
||
175 | 'capabilities' => array( 'edit_gravityviews' ), |
||
176 | ); |
||
177 | |||
178 | // Make sure Gravity Forms uses the submenu; if there's only one item, it uses a link instead of a dropdown |
||
179 | $sub_menu_items[] = array( |
||
180 | 'url' => '#', |
||
181 | 'label' => '', |
||
182 | 'menu_class' => 'hidden', |
||
183 | 'capabilities' => '', |
||
184 | ); |
||
185 | |||
186 | $menu_items['gravityview'] = array( |
||
187 | 'label' => __( 'Connected Views', 'gravityview' ), |
||
188 | 'icon' => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>', |
||
189 | 'title' => __( 'GravityView Views using this form as a data source', 'gravityview' ), |
||
190 | 'url' => '#', |
||
191 | 'onclick' => 'return false;', |
||
192 | 'menu_class' => 'gv_connected_forms gf_form_toolbar_settings', |
||
193 | 'sub_menu_items' => $sub_menu_items, |
||
194 | 'priority' => 0, |
||
195 | 'capabilities' => array( 'edit_gravityviews' ), |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | return $menu_items; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * List the field types without presentation properties (on a View context) |
||
204 | * |
||
205 | * @param array $array Existing field types to add to a blacklist |
||
206 | * @param string|null $context Context for the blacklist. Default: NULL. |
||
207 | * @access public |
||
208 | * @return array Default blacklist fields merged with existing blacklist fields |
||
209 | */ |
||
210 | function default_field_blacklist( $array = array(), $context = NULL ) { |
||
223 | |||
224 | /** |
||
225 | * Add tooltip text for use throughout the UI |
||
226 | * @param array $tooltips Array of Gravity Forms tooltips |
||
227 | * @return array Modified tooltips array |
||
228 | */ |
||
229 | public function tooltips( $tooltips = array() ) { |
||
282 | |||
283 | /** |
||
284 | * Add the Data Source information |
||
285 | * |
||
286 | * @param null $column_name |
||
287 | * @param $post_id |
||
288 | * |
||
289 | * @return void |
||
290 | */ |
||
291 | public function add_custom_column_content( $column_name = NULL, $post_id ) { |
||
292 | |||
293 | $output = ''; |
||
294 | |||
295 | switch ( $column_name ) { |
||
296 | case 'gv_template': |
||
297 | |||
298 | $template_id = gravityview_get_template_id( $post_id ); |
||
299 | |||
300 | // All Views should have a connected form. If it doesn't, that's not right. |
||
301 | if ( empty( $template_id ) ) { |
||
302 | do_action( 'gravityview_log_error', sprintf( __METHOD__ . ' View ID %s does not have a connected template.', $post_id ) ); |
||
303 | break; |
||
304 | } |
||
305 | |||
306 | $templates = gravityview_get_registered_templates(); |
||
307 | |||
308 | $template = isset( $templates[ $template_id ] ) ? $templates[ $template_id ] : false; |
||
309 | |||
310 | // Generate backup if label doesn't exist: `example_name` => `Example Name` |
||
311 | $template_id_pretty = ucwords( implode( ' ', explode( '_', $template_id ) ) ); |
||
312 | |||
313 | $output = $template ? $template['label'] : $template_id_pretty; |
||
314 | |||
315 | break; |
||
316 | |||
317 | case 'gv_connected_form': |
||
318 | |||
319 | $form_id = gravityview_get_form_id( $post_id ); |
||
320 | |||
321 | // All Views should have a connected form. If it doesn't, that's not right. |
||
322 | if ( empty( $form_id ) ) { |
||
323 | do_action( 'gravityview_log_error', sprintf( '[add_data_source_column_content] View ID %s does not have a connected GF form.', $post_id ) ); |
||
324 | $output = __( 'Not connected.', 'gravityview' ); |
||
325 | break; |
||
326 | } |
||
327 | |||
328 | $form = gravityview_get_form( $form_id ); |
||
329 | |||
330 | if ( ! $form ) { |
||
331 | do_action( 'gravityview_log_error', sprintf( '[add_data_source_column_content] Connected form not found: Form #%d', $form_id ) ); |
||
332 | |||
333 | $output = __( 'The connected form can not be found; it may no longer exist.', 'gravityview' ); |
||
334 | } else { |
||
335 | $output = self::get_connected_form_links( $form ); |
||
336 | } |
||
337 | |||
338 | break; |
||
339 | } |
||
340 | |||
341 | echo $output; |
||
342 | } |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Get HTML links relating to a connected form, like Edit, Entries, Settings, Preview |
||
347 | * @param array|int $form Gravity Forms forms array, or the form ID |
||
348 | * @param boolean $include_form_link Whether to include the bold name of the form in the output |
||
349 | * @return string HTML links |
||
350 | */ |
||
351 | static public function get_connected_form_links( $form, $include_form_link = true ) { |
||
407 | |||
408 | /** |
||
409 | * Add the Data Source column to the Views page |
||
410 | * @param array $columns Columns array |
||
411 | */ |
||
412 | public function add_post_type_columns( $columns ) { |
||
440 | |||
441 | /** |
||
442 | * Save View configuration |
||
443 | * |
||
444 | * @access public |
||
445 | * @param int $post_id Currently saved Post ID |
||
446 | * @return void |
||
447 | */ |
||
448 | function save_postdata( $post_id ) { |
||
449 | |||
450 | if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){ |
||
451 | return; |
||
452 | } |
||
453 | |||
454 | // validate post_type |
||
455 | if ( ! isset( $_POST['post_type'] ) || 'gravityview' != $_POST['post_type'] ) { |
||
456 | return; |
||
457 | } |
||
458 | |||
459 | // validate user can edit and save View |
||
460 | if ( ! GVCommon::has_cap( 'edit_gravityview', $post_id ) ) { |
||
461 | do_action( 'gravityview_log_error', __METHOD__ . ' - Current user does not have the capability to edit View #' . $post_id, wp_get_current_user() ); |
||
462 | return; |
||
463 | } |
||
464 | |||
465 | do_action( 'gravityview_log_debug', '[save_postdata] Saving View post type.', $_POST ); |
||
466 | |||
467 | $statii = array(); |
||
468 | |||
469 | // check if this is a start fresh View |
||
470 | if ( isset( $_POST['gravityview_select_form_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_form_nonce'], 'gravityview_select_form' ) ) { |
||
471 | |||
472 | $form_id = !empty( $_POST['gravityview_form_id'] ) ? $_POST['gravityview_form_id'] : ''; |
||
473 | // save form id |
||
474 | $statii['form_id'] = update_post_meta( $post_id, '_gravityview_form_id', $form_id ); |
||
475 | |||
476 | } |
||
477 | |||
478 | if( false === GVCommon::has_cap( 'gravityforms_create_form' ) && empty( $statii['form_id'] ) ) { |
||
479 | do_action( 'gravityview_log_error', __METHOD__ . ' - Current user does not have the capability to create a new Form.', wp_get_current_user() ); |
||
480 | return; |
||
481 | } |
||
482 | |||
483 | // Was this a start fresh? |
||
484 | if ( ! empty( $_POST['gravityview_form_id_start_fresh'] ) ) { |
||
485 | $statii['start_fresh'] = add_post_meta( $post_id, '_gravityview_start_fresh', 1 ); |
||
486 | } else { |
||
487 | $statii['start_fresh'] = delete_post_meta( $post_id, '_gravityview_start_fresh' ); |
||
488 | } |
||
489 | |||
490 | // Check if we have a template id |
||
491 | if ( isset( $_POST['gravityview_select_template_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_template_nonce'], 'gravityview_select_template' ) ) { |
||
492 | |||
493 | $template_id = !empty( $_POST['gravityview_directory_template'] ) ? $_POST['gravityview_directory_template'] : ''; |
||
494 | |||
495 | // now save template id |
||
496 | $statii['directory_template'] = update_post_meta( $post_id, '_gravityview_directory_template', $template_id ); |
||
497 | } |
||
498 | |||
499 | |||
500 | // save View Configuration metabox |
||
501 | if ( isset( $_POST['gravityview_view_configuration_nonce'] ) && wp_verify_nonce( $_POST['gravityview_view_configuration_nonce'], 'gravityview_view_configuration' ) ) { |
||
502 | |||
503 | // template settings |
||
504 | if( empty( $_POST['template_settings'] ) ) { |
||
505 | $_POST['template_settings'] = array(); |
||
506 | } |
||
507 | $statii['template_settings'] = update_post_meta( $post_id, '_gravityview_template_settings', $_POST['template_settings'] ); |
||
508 | |||
509 | $fields = array(); |
||
510 | |||
511 | // Directory&single Visible Fields |
||
512 | if( !empty( $preset_fields ) ) { |
||
513 | |||
514 | $fields = $preset_fields; |
||
515 | |||
516 | } elseif( !empty( $_POST['gv_fields'] ) ) { |
||
517 | $fields = _gravityview_process_posted_fields(); |
||
518 | } |
||
519 | |||
520 | $statii['directory_fields'] = update_post_meta( $post_id, '_gravityview_directory_fields', $fields ); |
||
521 | |||
522 | // Directory Visible Widgets |
||
523 | if( empty( $_POST['widgets'] ) ) { |
||
524 | $_POST['widgets'] = array(); |
||
525 | } |
||
526 | $statii['directory_widgets'] = gravityview_set_directory_widgets( $post_id, $_POST['widgets'] ); |
||
527 | |||
528 | } // end save view configuration |
||
529 | |||
530 | /** |
||
531 | * @action `gravityview_view_saved` After a View has been saved in the admin |
||
532 | * @param int $post_id ID of the View that has been saved |
||
533 | * @param array $statii Array of statuses of the post meta saving processes. If saving worked, each key should be mapped to a value of the post ID (`directory_widgets` => `124`). If failed (or didn't change), the value will be false. |
||
534 | * @since 1.17.2 |
||
535 | */ |
||
536 | do_action('gravityview_view_saved', $post_id, $statii ); |
||
537 | |||
538 | do_action('gravityview_log_debug', '[save_postdata] Update Post Meta Statuses (also returns false if nothing changed)', array_map( 'intval', $statii ) ); |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * @deprecated 1.1.6 |
||
543 | */ |
||
544 | function render_label() { |
||
547 | |||
548 | /** |
||
549 | * Render html for displaying available fields based on a Form ID |
||
550 | * $blacklist_field_types - contains the field types which are not proper to be shown in a directory. |
||
551 | * |
||
552 | * @see GravityView_Ajax::get_available_fields_html() Triggers `gravityview_render_available_fields` action |
||
553 | * @access public |
||
554 | * |
||
555 | * @param int $form Gravity Forms Form ID (default: '') |
||
556 | * @param string $context (default: 'single') |
||
557 | * |
||
558 | * @return void |
||
559 | */ |
||
560 | function render_available_fields( $form = 0, $context = 'single' ) { |
||
600 | |||
601 | /** |
||
602 | * Render html for displaying additional fields based on a Form ID |
||
603 | * |
||
604 | * @access public |
||
605 | * @param int $form Gravity Forms Form ID (default: '') |
||
606 | * @param string $context (default: 'single') |
||
607 | * @return void |
||
608 | */ |
||
609 | public function render_additional_fields( $form = 0, $context = 'single' ) { |
||
653 | |||
654 | /** |
||
655 | * Retrieve the default fields id, label and type |
||
656 | * @param string|array $form form_ID or form object |
||
657 | * @param string $zone Either 'single', 'directory', 'header', 'footer' |
||
658 | * @return array |
||
659 | */ |
||
660 | function get_entry_default_fields($form, $zone) { |
||
732 | |||
733 | /** |
||
734 | * Calculate the available fields |
||
735 | * @param string|array $form form_ID or form object |
||
736 | * @param string $zone Either 'single', 'directory', 'header', 'footer' |
||
737 | * @return array fields |
||
738 | */ |
||
739 | function get_available_fields( $form = '', $zone = NULL ) { |
||
764 | |||
765 | |||
766 | /** |
||
767 | * Render html for displaying available widgets |
||
768 | * @return string html |
||
769 | */ |
||
770 | function render_available_widgets() { |
||
784 | |||
785 | /** |
||
786 | * Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object |
||
787 | * @since 1.13.1 |
||
788 | * @return array |
||
789 | */ |
||
790 | function get_registered_widgets() { |
||
799 | |||
800 | /** |
||
801 | * Generic function to render rows and columns of active areas for widgets & fields |
||
802 | * @param string $template_id The current slug of the selected View template |
||
803 | * @param string $type Either 'widget' or 'field' |
||
804 | * @param string $zone Either 'single', 'directory', 'header', 'footer' |
||
805 | * @param array $rows The layout structure: rows, columns and areas |
||
806 | * @param array $values Saved objects |
||
807 | * @return void |
||
808 | */ |
||
809 | function render_active_areas( $template_id, $type, $zone, $rows, $values ) { |
||
911 | |||
912 | /** |
||
913 | * Render the widget active areas |
||
914 | * @param string $zone Either 'header' or 'footer' |
||
915 | * @param string $post_id Current Post ID (view) |
||
916 | * @return string html |
||
917 | */ |
||
918 | function render_widgets_active_areas( $template_id = '', $zone, $post_id = '' ) { |
||
941 | |||
942 | /** |
||
943 | * Render the Template Active Areas and configured active fields for a given template id and post id |
||
944 | * |
||
945 | * @access public |
||
946 | * @param string $template_id (default: '') Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()} |
||
947 | * @param string $post_id (default: '') |
||
948 | * @param string $context (default: 'single') |
||
949 | * @return string HTML of the active areas |
||
950 | */ |
||
951 | function render_directory_active_areas( $template_id = '', $context = 'single', $post_id = '', $echo = false ) { |
||
993 | |||
994 | /** |
||
995 | * Enqueue scripts and styles at Views editor |
||
996 | * |
||
997 | * @access public |
||
998 | * @param mixed $hook |
||
999 | * @return void |
||
1000 | */ |
||
1001 | static function add_scripts_and_styles( $hook ) { |
||
1047 | |||
1048 | static function enqueue_gravity_forms_scripts() { |
||
1067 | |||
1068 | /** |
||
1069 | * Add GravityView scripts and styles to Gravity Forms and GravityView No-Conflict modes |
||
1070 | * |
||
1071 | * @param array $registered Existing scripts or styles that have been registered (array of the handles) |
||
1072 | * |
||
1073 | * @return array |
||
1074 | */ |
||
1075 | function register_no_conflict( $registered ) { |
||
1115 | |||
1116 | |||
1117 | } |
||
1118 | |||
1120 |
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.