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 | /** |
||
35 | * @var array Global entry state. |
||
36 | */ |
||
37 | var $entry; |
||
38 | |||
39 | var $view_id; |
||
40 | |||
41 | function __construct() { |
||
46 | |||
47 | /** |
||
48 | * @since 2.5 |
||
49 | */ |
||
50 | private function add_hooks() { |
||
67 | |||
68 | /** |
||
69 | * Return the instantiated class object |
||
70 | * |
||
71 | * @since 2.5 |
||
72 | * @return GravityView_Duplicate_Entry |
||
73 | */ |
||
74 | 1 | static public function getInstance() { |
|
75 | |||
76 | 1 | if ( empty( self::$instance ) ) { |
|
77 | self::$instance = new self; |
||
78 | } |
||
79 | |||
80 | 1 | return self::$instance; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Include this extension templates path |
||
85 | * |
||
86 | * @since 2.5 |
||
87 | * |
||
88 | * @param array $file_paths List of template paths ordered |
||
89 | * |
||
90 | * @return array File paths, with duplicate field path added at index 117 |
||
91 | */ |
||
92 | 2 | public function add_template_path( $file_paths ) { |
|
100 | |||
101 | /** |
||
102 | * Add "Duplicate Link Text" setting to the edit_link field settings |
||
103 | * |
||
104 | * @since 2.5 |
||
105 | * |
||
106 | * @param array $field_options [description] |
||
107 | * @param [type] $template_id [description] |
||
108 | * @param [type] $field_id [description] |
||
109 | * @param [type] $context [description] |
||
110 | * @param [type] $input_type [description] |
||
111 | * |
||
112 | * @return array [description] |
||
113 | */ |
||
114 | public function duplicate_link_field_options( $field_options, $template_id, $field_id, $context, $input_type ) { |
||
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 | * Make sure there's an entry |
||
222 | * |
||
223 | * @since 2.5 |
||
224 | * |
||
225 | * @param array $entry Current entry array |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | public function set_entry( $entry ) { |
||
232 | |||
233 | /** |
||
234 | * Generate a consistent nonce key based on the Entry ID |
||
235 | * |
||
236 | * @since 2.5 |
||
237 | * |
||
238 | * @param int $entry_id Entry ID |
||
239 | * |
||
240 | * @return string Key used to validate request |
||
241 | */ |
||
242 | public static function get_nonce_key( $entry_id ) { |
||
245 | |||
246 | |||
247 | /** |
||
248 | * Generate a nonce link with the base URL of the current View embed |
||
249 | * |
||
250 | * We don't want to link to the single entry, because when duplicated, there would be nothing to return to. |
||
251 | * |
||
252 | * @since 2.5 |
||
253 | * |
||
254 | * @param array $entry Gravity Forms entry array |
||
255 | * @param int $view_id The View id. Not optional since 2.0 |
||
256 | * @param int $post_id ID of the current post/page being embedded on, if any |
||
257 | * |
||
258 | * @return string|null If directory link is valid, the URL to process the duplicate request. Otherwise, `NULL`. |
||
259 | */ |
||
260 | 2 | public static function get_duplicate_link( $entry, $view_id, $post_id = null ) { |
|
261 | 2 | self::getInstance()->set_entry( $entry ); |
|
262 | |||
263 | 2 | $base = GravityView_API::directory_link( $post_id ? : $view_id, true ); |
|
264 | |||
265 | 2 | if ( empty( $base ) ) { |
|
266 | gravityview()->log->error( 'Post ID does not exist: {post_id}', array( 'post_id' => $post_id ) ); |
||
267 | return NULL; |
||
268 | } |
||
269 | |||
270 | 2 | $actionurl = add_query_arg( array( |
|
271 | 2 | 'action' => 'duplicate', |
|
272 | 2 | 'entry_id' => $entry['id'], |
|
273 | 2 | 'gvid' => $view_id, |
|
274 | 2 | 'view_id' => $view_id, |
|
275 | 2 | ), $base ); |
|
276 | |||
277 | 2 | return add_query_arg( 'duplicate', wp_create_nonce( self::get_nonce_key( $entry['id'] ) ), $actionurl ); |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * Handle the duplication request, if $_GET['action'] is set to "duplicate" |
||
282 | * |
||
283 | * 1. Check referrer validity |
||
284 | * 2. Make sure there's an entry with the slug of $_GET['entry_id'] |
||
285 | * 3. If so, attempt to duplicate the entry. If not, set the error status |
||
286 | * 4. Remove `action=duplicate` from the URL |
||
287 | * 5. Redirect to the page using `wp_safe_redirect()` |
||
288 | * |
||
289 | * @since 2.5 |
||
290 | * |
||
291 | * @uses wp_safe_redirect() |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | 1 | public function process_duplicate() { |
|
372 | |||
373 | /** |
||
374 | * Duplicate the entry. |
||
375 | * |
||
376 | * Done after all the checks in self::process_duplicate. |
||
377 | * |
||
378 | * @since 2.5 |
||
379 | * |
||
380 | * @param array $entry The entry to be duplicated |
||
381 | * |
||
382 | * @return WP_Error|boolean |
||
383 | */ |
||
384 | private function duplicate_entry( $entry ) { |
||
480 | |||
481 | /** |
||
482 | * Is the current nonce valid for editing the entry? |
||
483 | * |
||
484 | * @since 2.5 |
||
485 | * |
||
486 | * @return boolean |
||
487 | */ |
||
488 | public function verify_nonce() { |
||
510 | |||
511 | /** |
||
512 | * Get the onclick attribute for the confirm dialogs that warns users before they duplicate an entry |
||
513 | * |
||
514 | * @since 2.5 |
||
515 | * |
||
516 | * @return string HTML `onclick` attribute |
||
517 | */ |
||
518 | public static function get_confirm_dialog() { |
||
530 | |||
531 | /** |
||
532 | * Check if the user can edit the entry |
||
533 | * |
||
534 | * - Is the nonce valid? |
||
535 | * - Does the user have the right caps for the entry |
||
536 | * - Is the entry in the trash? |
||
537 | * |
||
538 | * @since 2.5 |
||
539 | * |
||
540 | * @param array $entry Gravity Forms entry array |
||
541 | * @param int $view_id ID of the View being rendered |
||
542 | * |
||
543 | * @return boolean|WP_Error True: can edit form. WP_Error: nope. |
||
544 | */ |
||
545 | private function user_can_duplicate_entry( $entry = array(), $view_id = null ) { |
||
566 | |||
567 | |||
568 | /** |
||
569 | * checks if user has permissions to view the link or duplicate a specific entry |
||
570 | * |
||
571 | * @since 2.5 |
||
572 | * |
||
573 | * @param array $entry Gravity Forms entry array |
||
574 | * @param array $field Field settings (optional) |
||
575 | * @param int $view_id Pass a View ID to check caps against. If not set, check against current View (@deprecated no longer optional) |
||
576 | * |
||
577 | * @return bool |
||
578 | */ |
||
579 | public static function check_user_cap_duplicate_entry( $entry, $field, $view_id ) { |
||
651 | |||
652 | |||
653 | /** |
||
654 | * After processing duplicate entry, the user will be redirected to the referring View or embedded post/page. Display a message on redirection. |
||
655 | * |
||
656 | * If success, there will be `status` URL parameters `status=>success` |
||
657 | * If an error, there will be `status` and `message` URL parameters `status=>error&message=example` |
||
658 | * |
||
659 | * @since 2.5 |
||
660 | * |
||
661 | * @param int $current_view_id The ID of the View being rendered |
||
662 | * |
||
663 | * @return void |
||
664 | */ |
||
665 | public function display_message( $current_view_id = 0 ) { |
||
704 | |||
705 | |||
706 | } // end class |
||
707 | |||
710 |
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.