Complex classes like GravityView_Duplicate_Entry 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_Duplicate_Entry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | final class GravityView_Duplicate_Entry { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string The location of this file. |
||
| 26 | */ |
||
| 27 | static $file; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var GravityView_Duplicate_Entry This instance. |
||
| 31 | */ |
||
| 32 | static $instance; |
||
| 33 | |||
| 34 | var $view_id; |
||
| 35 | |||
| 36 | function __construct() { |
||
|
|
|||
| 37 | |||
| 38 | self::$file = plugin_dir_path( __FILE__ ); |
||
| 39 | $this->add_hooks(); |
||
| 40 | } |
||
| 41 | 1 | ||
| 42 | /** |
||
| 43 | 1 | * @since 2.5 |
|
| 44 | 1 | */ |
|
| 45 | 1 | private function add_hooks() { |
|
| 46 | |||
| 47 | add_action( 'wp', array( $this, 'process_duplicate' ), 10000 ); |
||
| 48 | |||
| 49 | add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 ); |
||
| 50 | 1 | ||
| 51 | add_action( 'gravityview_before', array( $this, 'display_message' ) ); |
||
| 52 | 1 | ||
| 53 | // For the Duplicate Entry Link, you don't want visible to all users. |
||
| 54 | 1 | add_filter( 'gravityview_field_visibility_caps', array( $this, 'modify_visibility_caps' ), 10, 5 ); |
|
| 55 | |||
| 56 | 1 | // Modify the field options based on the name of the field type |
|
| 57 | add_filter( 'gravityview_template_duplicate_link_options', array( $this, 'duplicate_link_field_options' ), 10, 5 ); |
||
| 58 | |||
| 59 | 1 | // add template path to check for field |
|
| 60 | add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) ); |
||
| 61 | |||
| 62 | 1 | // Entry duplication in the backend |
|
| 63 | add_action( 'gform_entries_first_column_actions', array( $this, 'make_duplicate_link_row' ), 10, 5 ); |
||
| 64 | |||
| 65 | 1 | // Handle duplicate action in the backend |
|
| 66 | 1 | add_action( 'gform_pre_entry_list', array( $this, 'maybe_duplicate_list' ) ); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Return the instantiated class object |
||
| 71 | * |
||
| 72 | * @since 2.5 |
||
| 73 | * @return GravityView_Duplicate_Entry |
||
| 74 | 2 | */ |
|
| 75 | static public function getInstance() { |
||
| 76 | 2 | ||
| 77 | 1 | if ( empty( self::$instance ) ) { |
|
| 78 | self::$instance = new self; |
||
| 79 | } |
||
| 80 | 2 | ||
| 81 | return self::$instance; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Include this extension templates path |
||
| 86 | * |
||
| 87 | * @since 2.5 |
||
| 88 | * |
||
| 89 | * @param array $file_paths List of template paths ordered |
||
| 90 | * |
||
| 91 | * @return array File paths, with duplicate field path added at index 117 |
||
| 92 | 2 | */ |
|
| 93 | public function add_template_path( $file_paths ) { |
||
| 94 | |||
| 95 | // Index 100 is the default GravityView template path. |
||
| 96 | 2 | // Index 110 is Edit Entry link |
|
| 97 | $file_paths[ 117 ] = self::$file; |
||
| 98 | 2 | ||
| 99 | return $file_paths; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Add "Duplicate Link Text" setting to the edit_link field settings |
||
| 104 | * |
||
| 105 | * @since 2.5 |
||
| 106 | * |
||
| 107 | * @param array $field_options [description] |
||
| 108 | * @param [type] $template_id [description] |
||
| 109 | * @param [type] $field_id [description] |
||
| 110 | * @param [type] $context [description] |
||
| 111 | * @param [type] $input_type [description] |
||
| 112 | * |
||
| 113 | * @return array [description] |
||
| 114 | */ |
||
| 115 | public function duplicate_link_field_options( $field_options, $template_id, $field_id, $context, $input_type ) { |
||
| 116 | |||
| 117 | // Always a link, never a filter, always same window |
||
| 118 | unset( $field_options['show_as_link'], $field_options['search_filter'], $field_options['new_window'] ); |
||
| 119 | |||
| 120 | // Duplicate Entry link should only appear to visitors capable of editing entries |
||
| 121 | unset( $field_options['only_loggedin'], $field_options['only_loggedin_cap'] ); |
||
| 122 | |||
| 123 | $add_option['duplicate_link'] = array( |
||
| 124 | 'type' => 'text', |
||
| 125 | 'label' => __( 'Duplicate Link Text', 'gravityview' ), |
||
| 126 | 'desc' => NULL, |
||
| 127 | 'value' => __( 'Duplicate Entry', 'gravityview' ), |
||
| 128 | 'merge_tags' => true, |
||
| 129 | ); |
||
| 130 | |||
| 131 | $field_options['allow_duplicate_cap'] = array( |
||
| 132 | 'type' => 'select', |
||
| 133 | 'label' => __( 'Allow the following users to duplicate the entry:', 'gravityview' ), |
||
| 134 | 'choices' => GravityView_Render_Settings::get_cap_choices( $template_id, $field_id, $context, $input_type ), |
||
| 135 | 'tooltip' => 'allow_duplicate_cap', |
||
| 136 | 'class' => 'widefat', |
||
| 137 | 'value' => 'read', // Default: entry creator |
||
| 138 | ); |
||
| 139 | |||
| 140 | return array_merge( $add_option, $field_options ); |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Add Edit Link as a default field, outside those set in the Gravity Form form |
||
| 146 | * |
||
| 147 | * @since 2.5 |
||
| 148 | * |
||
| 149 | * @param array $entry_default_fields Existing fields |
||
| 150 | * @param string|array $form form_ID or form object |
||
| 151 | * @param string $zone Either 'single', 'directory', 'edit', 'header', 'footer' |
||
| 152 | * |
||
| 153 | * @return array $entry_default_fields, with `duplicate_link` added. Won't be added if in Edit Entry context. |
||
| 154 | */ |
||
| 155 | public function add_default_field( $entry_default_fields, $form = array(), $zone = '' ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Add Duplicate Entry Link to the Add Field dialog |
||
| 170 | * |
||
| 171 | * @since 2.5 |
||
| 172 | * |
||
| 173 | * @param array $available_fields |
||
| 174 | * |
||
| 175 | * @return array Fields with `duplicate_link` added |
||
| 176 | */ |
||
| 177 | public function add_available_field( $available_fields = array() ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Change wording for the Edit context to read Entry Creator |
||
| 192 | * |
||
| 193 | * @since 2.5 |
||
| 194 | * |
||
| 195 | * @param array $visibility_caps Array of capabilities to display in field dropdown. |
||
| 196 | * @param string $field_type Type of field options to render (`field` or `widget`) |
||
| 197 | * @param string $template_id Table slug |
||
| 198 | * @param float|string $field_id GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by` |
||
| 199 | * @param string $context What context are we in? Example: `single` or `directory` |
||
| 200 | * @param string $input_type (textarea, list, select, etc.) |
||
| 201 | * |
||
| 202 | * @return array Array of field options with `label`, `value`, `type`, `default` keys |
||
| 203 | */ |
||
| 204 | public function modify_visibility_caps( $visibility_caps = array(), $template_id = '', $field_id = '', $context = '', $input_type = '' ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Generate a consistent nonce key based on the Entry ID |
||
| 222 | * |
||
| 223 | * @since 2.5 |
||
| 224 | * |
||
| 225 | * @param int $entry_id Entry ID |
||
| 226 | * |
||
| 227 | * @return string Key used to validate request |
||
| 228 | */ |
||
| 229 | public static function get_nonce_key( $entry_id ) { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Generate a nonce link with the base URL of the current View embed |
||
| 236 | * |
||
| 237 | * We don't want to link to the single entry, because when duplicated, there would be nothing to return to. |
||
| 238 | * |
||
| 239 | * @since 2.5 |
||
| 240 | * |
||
| 241 | * @param array $entry Gravity Forms entry array |
||
| 242 | 2 | * @param int $view_id The View id. Not optional since 2.0 |
|
| 243 | 2 | * @param int $post_id ID of the current post/page being embedded on, if any |
|
| 244 | * |
||
| 245 | * @return string|null If directory link is valid, the URL to process the duplicate request. Otherwise, `NULL`. |
||
| 246 | */ |
||
| 247 | public static function get_duplicate_link( $entry, $view_id, $post_id = null ) { |
||
| 265 | 2 | ||
| 266 | /** |
||
| 267 | * Handle the duplication request, if $_GET['action'] is set to "duplicate" |
||
| 268 | * |
||
| 269 | * 1. Check referrer validity |
||
| 270 | 2 | * 2. Make sure there's an entry with the slug of $_GET['entry_id'] |
|
| 271 | 2 | * 3. If so, attempt to duplicate the entry. If not, set the error status |
|
| 272 | 2 | * 4. Remove `action=duplicate` from the URL |
|
| 273 | 2 | * 5. Redirect to the page using `wp_safe_redirect()` |
|
| 274 | 2 | * |
|
| 275 | 2 | * @since 2.5 |
|
| 276 | * |
||
| 277 | 2 | * @uses wp_safe_redirect() |
|
| 278 | * |
||
| 279 | * @return void|string $url URL during tests instead of redirect. |
||
| 280 | */ |
||
| 281 | public function process_duplicate() { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Duplicate the entry. |
||
| 365 | 1 | * |
|
| 366 | 1 | * Done after all the checks in self::process_duplicate. |
|
| 367 | * |
||
| 368 | 1 | * @since 2.5 |
|
| 369 | 1 | * |
|
| 370 | * @param array $entry The entry to be duplicated |
||
| 371 | * |
||
| 372 | * @return WP_Error|boolean |
||
| 373 | */ |
||
| 374 | private function duplicate_entry( $entry ) { |
||
| 470 | 1 | ||
| 471 | /** |
||
| 472 | * Is the current nonce valid for editing the entry? |
||
| 473 | * |
||
| 474 | * @since 2.5 |
||
| 475 | * |
||
| 476 | * @return boolean |
||
| 477 | */ |
||
| 478 | 1 | public function verify_nonce() { |
|
| 500 | |||
| 501 | 1 | /** |
|
| 502 | * Get the onclick attribute for the confirm dialogs that warns users before they duplicate an entry |
||
| 503 | * |
||
| 504 | * @since 2.5 |
||
| 505 | * |
||
| 506 | * @return string HTML `onclick` attribute |
||
| 507 | */ |
||
| 508 | public static function get_confirm_dialog() { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Check if the user can edit the entry |
||
| 528 | * |
||
| 529 | * - Is the nonce valid? |
||
| 530 | * - Does the user have the right caps for the entry |
||
| 531 | 1 | * - Is the entry in the trash? |
|
| 532 | * |
||
| 533 | 1 | * @since 2.5 |
|
| 534 | 1 | * |
|
| 535 | * @param array $entry Gravity Forms entry array |
||
| 536 | * @param int $view_id ID of the View being rendered |
||
| 537 | 1 | * |
|
| 538 | * @return boolean|WP_Error True: can edit form. WP_Error: nope. |
||
| 539 | */ |
||
| 540 | private function user_can_duplicate_entry( $entry = array(), $view_id = null ) { |
||
| 561 | |||
| 562 | 1 | ||
| 563 | 1 | /** |
|
| 564 | * checks if user has permissions to view the link or duplicate a specific entry |
||
| 565 | * |
||
| 566 | * @since 2.5 |
||
| 567 | 1 | * |
|
| 568 | 1 | * @param array $entry Gravity Forms entry array |
|
| 569 | * @param array $field Field settings (optional) |
||
| 570 | * @param int $view_id Pass a View ID to check caps against. If not set, check against current View |
||
| 571 | 1 | * |
|
| 572 | * @return bool |
||
| 573 | 1 | */ |
|
| 574 | public static function check_user_cap_duplicate_entry( $entry, $field = array(), $view_id = 0 ) { |
||
| 649 | 1 | ||
| 650 | |||
| 651 | /** |
||
| 652 | * After processing duplicate entry, the user will be redirected to the referring View or embedded post/page. Display a message on redirection. |
||
| 653 | * |
||
| 654 | 2 | * If success, there will be `status` URL parameters `status=>success` |
|
| 655 | * If an error, there will be `status` and `message` URL parameters `status=>error&message=example` |
||
| 656 | 1 | * |
|
| 657 | * @since 2.5 |
||
| 658 | 1 | * |
|
| 659 | * @param int $current_view_id The ID of the View being rendered |
||
| 660 | * |
||
| 661 | 2 | * @return void |
|
| 662 | */ |
||
| 663 | public function display_message( $current_view_id = 0 ) { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Add a Duplicate link to the row of actions on the entry list in the backend. |
||
| 705 | * |
||
| 706 | * @since 2.5.1 |
||
| 707 | * |
||
| 708 | * @param int $form_id The form ID. |
||
| 709 | * @param int $field_id The field ID. |
||
| 710 | * @param string $value The value. |
||
| 711 | * @param array $entry The entryvalue The value. |
||
| 712 | * @param array $entry The entry. |
||
| 713 | * @param string $query_string The query. |
||
| 714 | * |
||
| 715 | * @return void |
||
| 716 | */ |
||
| 717 | public function make_duplicate_link_row( $form_id, $field_id, $value, $entry, $query_string ) { |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Perhaps duplicate this entry if the action has been corrected. |
||
| 738 | * |
||
| 739 | * @since 2.5.1 |
||
| 740 | * |
||
| 741 | * @param int $form_id The form ID. |
||
| 742 | * |
||
| 743 | * @return void |
||
| 744 | */ |
||
| 745 | public function maybe_duplicate_list( $form_id ) { |
||
| 809 | |||
| 810 | |||
| 811 | } // end class |
||
| 812 | |||
| 815 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.