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 | 22 | static function getInstance() { |
|
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 | 1 | 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 | 22 | function set_entry( $entry = null ) { |
|
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 | * @param int $view_id The View id. Not optional since 2.0 |
||
232 | * @return string|null If directory link is valid, the URL to process the delete request. Otherwise, `NULL`. |
||
233 | */ |
||
234 | 22 | public static function get_delete_link( $entry, $view_id = 0, $post_id = null ) { |
|
263 | |||
264 | |||
265 | /** |
||
266 | * Add a Delete button to the #publishing-action section of the Delete Entry form |
||
267 | * |
||
268 | * @since 1.5.1 |
||
269 | * @since 2.0.13 Added $post_id |
||
270 | * |
||
271 | * @param array $form Gravity Forms form array |
||
272 | * @param array $entry Gravity Forms entry array |
||
273 | * @param int $view_id GravityView View ID |
||
274 | * @param int $post_id Current post ID. May be same as View ID. |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | 22 | public function add_delete_button( $form = array(), $entry = array(), $view_id = null, $post_id = null ) { |
|
279 | |||
280 | // Only show the link to those who are allowed to see it. |
||
281 | 22 | if( !self::check_user_cap_delete_entry( $entry, array(), $view_id ) ) { |
|
282 | 1 | return; |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * @filter `gravityview/delete-entry/show-delete-button` Should the Delete button be shown in the Edit Entry screen? |
||
287 | * @param boolean $show_entry Default: true |
||
288 | */ |
||
289 | 21 | $show_delete_button = apply_filters( 'gravityview/delete-entry/show-delete-button', true ); |
|
290 | |||
291 | // If the button is hidden by the filter, don't show. |
||
292 | 21 | if( !$show_delete_button ) { |
|
293 | return; |
||
294 | } |
||
295 | |||
296 | $attributes = array( |
||
297 | 21 | 'class' => 'btn btn-sm button button-small alignright pull-right btn-danger gv-button-delete', |
|
298 | 21 | 'tabindex' => ( GFCommon::$tab_index ++ ), |
|
299 | 21 | 'onclick' => self::get_confirm_dialog(), |
|
300 | ); |
||
301 | |||
302 | 21 | echo gravityview_get_link( self::get_delete_link( $entry, $view_id, $post_id ), esc_attr__( 'Delete', 'gravityview' ), $attributes ); |
|
303 | |||
304 | 21 | } |
|
305 | |||
306 | /** |
||
307 | * Handle the deletion request, if $_GET['action'] is set to "delete" |
||
308 | * |
||
309 | * 1. Check referrer validity |
||
310 | * 2. Make sure there's an entry with the slug of $_GET['entry_id'] |
||
311 | * 3. If so, attempt to delete the entry. If not, set the error status |
||
312 | * 4. Remove `action=delete` from the URL |
||
313 | * 5. Redirect to the page using `wp_safe_redirect()` |
||
314 | * |
||
315 | * @since 1.5.1 |
||
316 | * @uses wp_safe_redirect() |
||
317 | * @return void |
||
318 | */ |
||
319 | 1 | function process_delete() { |
|
320 | |||
321 | // If the form is submitted |
||
322 | 1 | if( isset( $_GET['action'] ) && 'delete' === $_GET['action'] && isset( $_GET['entry_id'] ) ) { |
|
323 | |||
324 | // Make sure it's a GravityView request |
||
325 | $valid_nonce_key = wp_verify_nonce( $_GET['delete'], self::get_nonce_key( $_GET['entry_id'] ) ); |
||
326 | |||
327 | if( ! $valid_nonce_key ) { |
||
328 | gravityview()->log->debug( 'Delete entry not processed: nonce validation failed.' ); |
||
329 | return; |
||
330 | } |
||
331 | |||
332 | // Get the entry slug |
||
333 | $entry_slug = esc_attr( $_GET['entry_id'] ); |
||
334 | |||
335 | // See if there's an entry there |
||
336 | $entry = gravityview_get_entry( $entry_slug, true, false ); |
||
337 | |||
338 | if( $entry ) { |
||
339 | |||
340 | $has_permission = $this->user_can_delete_entry( $entry ); |
||
341 | |||
342 | if( is_wp_error( $has_permission ) ) { |
||
343 | |||
344 | $messages = array( |
||
345 | 'message' => urlencode( $has_permission->get_error_message() ), |
||
346 | 'status' => 'error', |
||
347 | ); |
||
348 | |||
349 | } else { |
||
350 | |||
351 | // Delete the entry |
||
352 | $delete_response = $this->delete_or_trash_entry( $entry ); |
||
353 | |||
354 | if( is_wp_error( $delete_response ) ) { |
||
355 | |||
356 | $messages = array( |
||
357 | 'message' => urlencode( $delete_response->get_error_message() ), |
||
358 | 'status' => 'error', |
||
359 | ); |
||
360 | |||
361 | } else { |
||
362 | |||
363 | $messages = array( |
||
364 | 'status' => $delete_response, |
||
365 | ); |
||
366 | |||
367 | } |
||
368 | |||
369 | } |
||
370 | |||
371 | } else { |
||
372 | |||
373 | gravityview()->log->debug( 'Delete entry failed: there was no entry with the entry slug {entry_slug}', array( 'entry_slug' => $entry_slug ) ); |
||
374 | |||
375 | $messages = array( |
||
376 | 'message' => urlencode( __('The entry does not exist.', 'gravityview') ), |
||
377 | 'status' => 'error', |
||
378 | ); |
||
379 | } |
||
380 | |||
381 | $redirect_to_base = esc_url_raw( remove_query_arg( array( 'action', 'gvid' ) ) ); |
||
382 | $redirect_to = add_query_arg( $messages, $redirect_to_base ); |
||
383 | |||
384 | wp_safe_redirect( $redirect_to ); |
||
385 | |||
386 | exit(); |
||
387 | |||
388 | } // endif action is delete. |
||
389 | |||
390 | 1 | } |
|
391 | |||
392 | /** |
||
393 | * Delete mode: permanently delete, or move to trash? |
||
394 | * |
||
395 | * @return string `delete` or `trash` |
||
396 | */ |
||
397 | private function get_delete_mode() { |
||
408 | |||
409 | /** |
||
410 | * @since 1.13.1 |
||
411 | * @see GFAPI::delete_entry() |
||
412 | * @return WP_Error|boolean GFAPI::delete_entry() returns a WP_Error on error |
||
413 | */ |
||
414 | private function delete_or_trash_entry( $entry ) { |
||
468 | |||
469 | /** |
||
470 | * Delete or trash a post connected to an entry |
||
471 | * |
||
472 | * @since 1.17 |
||
473 | * |
||
474 | * @param int $entry_id ID of entry being deleted/trashed |
||
475 | * @param array $entry Array of the entry being deleted/trashed |
||
476 | */ |
||
477 | public function process_connected_posts( $entry_id = 0, $entry = array() ) { |
||
509 | |||
510 | /** |
||
511 | * Is the current nonce valid for editing the entry? |
||
512 | * |
||
513 | * @since 1.5.1 |
||
514 | * @return boolean |
||
515 | */ |
||
516 | public function verify_nonce() { |
||
538 | |||
539 | /** |
||
540 | * Get the onclick attribute for the confirm dialogs that warns users before they delete an entry |
||
541 | * |
||
542 | * @since 1.5.1 |
||
543 | * @return string HTML `onclick` attribute |
||
544 | */ |
||
545 | 22 | public static function get_confirm_dialog() { |
|
557 | |||
558 | /** |
||
559 | * Check if the user can edit the entry |
||
560 | * |
||
561 | * - Is the nonce valid? |
||
562 | * - Does the user have the right caps for the entry |
||
563 | * - Is the entry in the trash? |
||
564 | * |
||
565 | * @since 1.5.1 |
||
566 | * @param array $entry Gravity Forms entry array |
||
567 | * @return boolean|WP_Error True: can edit form. WP_Error: nope. |
||
568 | */ |
||
569 | function user_can_delete_entry( $entry = array(), $view_id = null ) { |
||
598 | |||
599 | |||
600 | /** |
||
601 | * checks if user has permissions to view the link or delete a specific entry |
||
602 | * |
||
603 | * @since 1.5.1 |
||
604 | * @since 1.15 Added `$view_id` param |
||
605 | * |
||
606 | * @param array $entry Gravity Forms entry array |
||
607 | * @param array $field Field settings (optional) |
||
608 | * @param int $view_id Pass a View ID to check caps against. If not set, check against current View (@deprecated no longer optional) |
||
609 | * @return bool |
||
610 | */ |
||
611 | 23 | public static function check_user_cap_delete_entry( $entry, $field = array(), $view_id = 0 ) { |
|
691 | |||
692 | |||
693 | /** |
||
694 | * After processing delete entry, the user will be redirected to the referring View or embedded post/page. Display a message on redirection. |
||
695 | * |
||
696 | * If success, there will be `status` URL parameters `status=>success` |
||
697 | * If an error, there will be `status` and `message` URL parameters `status=>error&message=example` |
||
698 | * |
||
699 | * @since 1.15.2 Only show message when the URL parameter's View ID matches the current View ID |
||
700 | * @since 1.5.1 |
||
701 | * |
||
702 | * @param int $current_view_id The ID of the View being rendered |
||
703 | * @return void |
||
704 | */ |
||
705 | 36 | public function display_message( $current_view_id = 0 ) { |
|
747 | |||
748 | |||
749 | } // end class |
||
750 | |||
753 |
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.