Complex classes like GravityView_Delete_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_Delete_Entry, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | final class GravityView_Delete_Entry { |
||
23 | |||
24 | static $file; |
||
25 | static $instance; |
||
26 | var $entry; |
||
27 | var $form; |
||
28 | var $view_id; |
||
29 | var $is_valid = NULL; |
||
30 | |||
31 | function __construct() { |
||
37 | |||
38 | /** |
||
39 | * @since 1.9.2 |
||
40 | */ |
||
41 | private function add_hooks() { |
||
63 | |||
64 | /** |
||
65 | * Return the instantiated class object |
||
66 | * |
||
67 | * @since 1.5.1 |
||
68 | * @return GravityView_Delete_Entry |
||
69 | */ |
||
70 | 3 | static function getInstance() { |
|
71 | |||
72 | 3 | if( empty( self::$instance ) ) { |
|
73 | self::$instance = new self; |
||
74 | } |
||
75 | |||
76 | 3 | return self::$instance; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Include this extension templates path |
||
81 | * |
||
82 | * @since 1.5.1 |
||
83 | * @param array $file_paths List of template paths ordered |
||
84 | */ |
||
85 | function add_template_path( $file_paths ) { |
||
93 | |||
94 | /** |
||
95 | * Add "Delete Link Text" setting to the edit_link field settings |
||
96 | * |
||
97 | * @since 1.5.1 |
||
98 | * @param [type] $field_options [description] |
||
99 | * @param [type] $template_id [description] |
||
100 | * @param [type] $field_id [description] |
||
101 | * @param [type] $context [description] |
||
102 | * @param [type] $input_type [description] |
||
103 | * @return [type] [description] |
||
104 | */ |
||
105 | function delete_link_field_options( $field_options, $template_id, $field_id, $context, $input_type ) { |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Add Edit Link as a default field, outside those set in the Gravity Form form |
||
137 | * |
||
138 | * @since 1.5.1 |
||
139 | * @param array $entry_default_fields Existing fields |
||
140 | * @param string|array $form form_ID or form object |
||
141 | * @param string $zone Either 'single', 'directory', 'edit', 'header', 'footer' |
||
142 | */ |
||
143 | function add_default_field( $entry_default_fields, $form = array(), $zone = '' ) { |
||
155 | |||
156 | /** |
||
157 | * Add Delete Entry Link to the Add Field dialog |
||
158 | * @since 1.5.1 |
||
159 | * @param array $available_fields |
||
160 | */ |
||
161 | function add_available_field( $available_fields = array() ) { |
||
173 | |||
174 | /** |
||
175 | * Change wording for the Edit context to read Entry Creator |
||
176 | * |
||
177 | * @since 1.5.1 |
||
178 | * @param array $visibility_caps Array of capabilities to display in field dropdown. |
||
179 | * @param string $field_type Type of field options to render (`field` or `widget`) |
||
180 | * @param string $template_id Table slug |
||
181 | * @param float $field_id GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by` |
||
182 | * @param string $context What context are we in? Example: `single` or `directory` |
||
183 | * @param string $input_type (textarea, list, select, etc.) |
||
184 | * @return array Array of field options with `label`, `value`, `type`, `default` keys |
||
185 | */ |
||
186 | public function modify_visibility_caps( $visibility_caps = array(), $template_id = '', $field_id = '', $context = '', $input_type = '' ) { |
||
201 | |||
202 | /** |
||
203 | * Make sure there's an entry |
||
204 | * |
||
205 | * @since 1.5.1 |
||
206 | * @param [type] $entry [description] |
||
207 | */ |
||
208 | 3 | function set_entry( $entry = null ) { |
|
209 | 3 | $this->entry = empty( $entry ) ? GravityView_View::getInstance()->entries[0] : $entry; |
|
210 | 3 | } |
|
211 | |||
212 | /** |
||
213 | * Generate a consistent nonce key based on the Entry ID |
||
214 | * |
||
215 | * @since 1.5.1 |
||
216 | * @param int $entry_id Entry ID |
||
217 | * @return string Key used to validate request |
||
218 | */ |
||
219 | public static function get_nonce_key( $entry_id ) { |
||
222 | |||
223 | |||
224 | /** |
||
225 | * Generate a nonce link with the base URL of the current View embed |
||
226 | * |
||
227 | * We don't want to link to the single entry, because when deleted, there would be nothing to return to. |
||
228 | * |
||
229 | * @since 1.5.1 |
||
230 | * @param array $entry Gravity Forms entry array |
||
231 | * @return string|null If directory link is valid, the URL to process the delete request. Otherwise, `NULL`. |
||
232 | */ |
||
233 | 3 | public static function get_delete_link( $entry, $view_id = 0, $post_id = null ) { |
|
234 | |||
235 | 3 | self::getInstance()->set_entry( $entry ); |
|
236 | |||
237 | 3 | $base = GravityView_API::directory_link( $post_id, true ); |
|
238 | |||
239 | 3 | if( empty( $base ) ) { |
|
240 | do_action( 'gravityview_log_error', __METHOD__ . ' - Post ID does not exist: '.$post_id ); |
||
241 | return NULL; |
||
242 | } |
||
243 | |||
244 | // Use the slug instead of the ID for consistent security |
||
245 | 3 | $entry_slug = GravityView_API::get_entry_slug( $entry['id'], $entry ); |
|
246 | |||
247 | 3 | $view_id = empty( $view_id ) ? gravityview_get_view_id() : $view_id; |
|
248 | |||
249 | 3 | $actionurl = add_query_arg( array( |
|
250 | 3 | 'action' => 'delete', |
|
251 | 3 | 'entry_id' => $entry_slug, |
|
252 | 3 | 'gvid' => $view_id, |
|
253 | 3 | 'view_id' => $view_id, |
|
254 | 3 | ), $base ); |
|
255 | |||
256 | 3 | $url = wp_nonce_url( $actionurl, 'delete_'.$entry_slug, 'delete' ); |
|
257 | |||
258 | 3 | return $url; |
|
259 | } |
||
260 | |||
261 | |||
262 | /** |
||
263 | * Add a Delete button to the #publishing-action section of the Delete Entry form |
||
264 | * |
||
265 | * @since 1.5.1 |
||
266 | * @param array $form Gravity Forms form array |
||
267 | * @param array $entry Gravity Forms entry array |
||
268 | * @param int $view_id GravityView View ID |
||
269 | */ |
||
270 | 3 | function add_delete_button( $form = array(), $entry = array(), $view_id = NULL ) { |
|
271 | |||
272 | // Only show the link to those who are allowed to see it. |
||
273 | 3 | if( !self::check_user_cap_delete_entry( $entry ) ) { |
|
274 | return; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @filter `gravityview/delete-entry/show-delete-button` Should the Delete button be shown in the Edit Entry screen? |
||
279 | * @param boolean $show_entry Default: true |
||
280 | */ |
||
281 | 3 | $show_delete_button = apply_filters( 'gravityview/delete-entry/show-delete-button', true ); |
|
282 | |||
283 | // If the button is hidden by the filter, don't show. |
||
284 | 3 | if( !$show_delete_button ) { |
|
285 | return; |
||
286 | } |
||
287 | |||
288 | $attributes = array( |
||
289 | 3 | 'class' => 'btn btn-sm button button-small alignright pull-right btn-danger gv-button-delete', |
|
290 | 3 | 'tabindex' => '5', |
|
291 | 3 | 'onclick' => self::get_confirm_dialog(), |
|
292 | ); |
||
293 | |||
294 | 3 | echo gravityview_get_link( self::get_delete_link( $entry, $view_id ), esc_attr__( 'Delete', 'gravityview' ), $attributes ); |
|
295 | |||
296 | 3 | } |
|
297 | |||
298 | /** |
||
299 | * Handle the deletion request, if $_GET['action'] is set to "delete" |
||
300 | * |
||
301 | * 1. Check referrer validity |
||
302 | * 2. Make sure there's an entry with the slug of $_GET['entry_id'] |
||
303 | * 3. If so, attempt to delete the entry. If not, set the error status |
||
304 | * 4. Remove `action=delete` from the URL |
||
305 | * 5. Redirect to the page using `wp_safe_redirect()` |
||
306 | * |
||
307 | * @since 1.5.1 |
||
308 | * @uses wp_safe_redirect() |
||
309 | * @return void |
||
310 | */ |
||
311 | function process_delete() { |
||
383 | |||
384 | /** |
||
385 | * Delete mode: permanently delete, or move to trash? |
||
386 | * |
||
387 | * @return string `delete` or `trash` |
||
388 | */ |
||
389 | private function get_delete_mode() { |
||
400 | |||
401 | /** |
||
402 | * @since 1.13.1 |
||
403 | * @see GFAPI::delete_entry() |
||
404 | * @return WP_Error|boolean GFAPI::delete_entry() returns a WP_Error on error |
||
405 | */ |
||
406 | private function delete_or_trash_entry( $entry ) { |
||
460 | |||
461 | /** |
||
462 | * Delete or trash a post connected to an entry |
||
463 | * |
||
464 | * @since 1.17 |
||
465 | * |
||
466 | * @param int $entry_id ID of entry being deleted/trashed |
||
467 | * @param array $entry Array of the entry being deleted/trashed |
||
468 | */ |
||
469 | public function process_connected_posts( $entry_id = 0, $entry = array() ) { |
||
501 | |||
502 | /** |
||
503 | * Is the current nonce valid for editing the entry? |
||
504 | * |
||
505 | * @since 1.5.1 |
||
506 | * @return boolean |
||
507 | */ |
||
508 | public function verify_nonce() { |
||
530 | |||
531 | /** |
||
532 | * Get the onclick attribute for the confirm dialogs that warns users before they delete an entry |
||
533 | * |
||
534 | * @since 1.5.1 |
||
535 | * @return string HTML `onclick` attribute |
||
536 | */ |
||
537 | 3 | public static function get_confirm_dialog() { |
|
538 | |||
539 | 3 | $confirm = __('Are you sure you want to delete this entry? This cannot be undone.', 'gravityview'); |
|
540 | |||
541 | /** |
||
542 | * @filter `gravityview/delete-entry/confirm-text` Modify the Delete Entry Javascript confirmation text |
||
543 | * @param string $confirm Default: "Are you sure you want to delete this entry? This cannot be undone." |
||
544 | */ |
||
545 | 3 | $confirm = apply_filters( 'gravityview/delete-entry/confirm-text', $confirm ); |
|
546 | |||
547 | 3 | return 'return window.confirm(\''. esc_js( $confirm ) .'\');'; |
|
548 | } |
||
549 | |||
550 | /** |
||
551 | * Check if the user can edit the entry |
||
552 | * |
||
553 | * - Is the nonce valid? |
||
554 | * - Does the user have the right caps for the entry |
||
555 | * - Is the entry in the trash? |
||
556 | * |
||
557 | * @since 1.5.1 |
||
558 | * @param array $entry Gravity Forms entry array |
||
559 | * @return boolean|WP_Error True: can edit form. WP_Error: nope. |
||
560 | */ |
||
561 | function user_can_delete_entry( $entry = array() ) { |
||
590 | |||
591 | |||
592 | /** |
||
593 | * checks if user has permissions to view the link or delete a specific entry |
||
594 | * |
||
595 | * @since 1.5.1 |
||
596 | * @since 1.15 Added `$view_id` param |
||
597 | * |
||
598 | * @param array $entry Gravity Forms entry array |
||
599 | * @param array $field Field settings (optional) |
||
600 | * @param int $view_id Pass a View ID to check caps against. If not set, check against current View (optional) |
||
601 | * @return bool |
||
602 | */ |
||
603 | 3 | public static function check_user_cap_delete_entry( $entry, $field = array(), $view_id = 0 ) { |
|
680 | |||
681 | |||
682 | /** |
||
683 | * After processing delete entry, the user will be redirected to the referring View or embedded post/page. Display a message on redirection. |
||
684 | * |
||
685 | * If success, there will be `status` URL parameters `status=>success` |
||
686 | * If an error, there will be `status` and `message` URL parameters `status=>error&message=example` |
||
687 | * |
||
688 | * @since 1.15.2 Only show message when the URL parameter's View ID matches the current View ID |
||
689 | * @since 1.5.1 |
||
690 | * |
||
691 | * @param int $current_view_id The ID of the View being rendered |
||
692 | * @return void |
||
693 | */ |
||
694 | public function display_message( $current_view_id = 0 ) { |
||
736 | |||
737 | |||
738 | } // end class |
||
739 | |||
742 |
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.