Complex classes like GravityView_Admin_ApproveEntries 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_ApproveEntries, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class GravityView_Admin_ApproveEntries { |
||
16 | |||
17 | // hold notification messages |
||
18 | public $bulk_update_message = ''; |
||
19 | |||
20 | /** |
||
21 | * @var array Set the prefixes here instead of spread across the class |
||
22 | * @since 1.17 |
||
23 | */ |
||
24 | private $bulk_action_prefixes = array( |
||
25 | 'approve' => 'gvapprove', |
||
26 | 'unapprove' => 'gvunapprove', |
||
27 | ); |
||
28 | |||
29 | function __construct() { |
||
34 | |||
35 | private function add_hooks() { |
||
36 | /** Edit Gravity Form page */ |
||
37 | |||
38 | // Add button to left menu |
||
39 | add_filter( 'gform_add_field_buttons', array( $this, 'add_field_buttons' ) ); |
||
40 | // Set defaults |
||
41 | add_action( 'gform_editor_js_set_default_values', array( $this, 'set_defaults' ) ); |
||
42 | |||
43 | /** gf_entries page - entries table screen */ |
||
44 | |||
45 | // capture bulk actions |
||
46 | add_action( 'gform_loaded', array( $this, 'process_bulk_action') ); |
||
47 | |||
48 | // add hidden field with approve status |
||
49 | add_action( 'gform_entries_first_column_actions', array( $this, 'add_entry_approved_hidden_input' ), 1, 5 ); |
||
50 | |||
51 | // process ajax approve entry requests |
||
52 | add_action('wp_ajax_gv_update_approved', array( $this, 'ajax_update_approved')); |
||
53 | |||
54 | // when using the User opt-in field, check on entry submission |
||
55 | add_action( 'gform_after_submission', array( $this, 'after_submission' ), 10, 2 ); |
||
56 | |||
57 | // in case entry is edited (on admin or frontend) |
||
58 | add_action( 'gform_after_update_entry', array( $this, 'after_update_entry_update_approved_meta' ), 10, 2); |
||
59 | |||
60 | |||
61 | add_filter( 'gravityview_tooltips', array( $this, 'tooltips' ) ); |
||
62 | |||
63 | // adding styles and scripts |
||
64 | add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts_and_styles') ); |
||
65 | // bypass Gravity Forms no-conflict mode |
||
66 | add_filter( 'gform_noconflict_scripts', array( $this, 'register_gform_noconflict_script' ) ); |
||
67 | add_filter( 'gform_noconflict_styles', array( $this, 'register_gform_noconflict_style' ) ); |
||
68 | |||
69 | add_filter( 'gform_filter_links_entry_list', array( $this, 'filter_links_entry_list' ), 10, 3 ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Add filter links to the Entries page |
||
74 | * |
||
75 | * Can be disabled by returning false on the `gravityview/approve_entries/show_filter_links_entry_list` filter |
||
76 | * |
||
77 | * @since 1.17.1 |
||
78 | * |
||
79 | * @param array $filter_links Array of links to include in the subsubsub filter list. Includes `id`, `field_filters`, `count`, and `label` keys |
||
80 | * @param array $form GF Form object of current form |
||
81 | * @param bool $include_counts Whether to include counts in the output |
||
82 | * |
||
83 | * @return array Filter links, with GravityView approved/disapproved links added |
||
84 | */ |
||
85 | public function filter_links_entry_list( $filter_links = array(), $form = array(), $include_counts = true ) { |
||
135 | |||
136 | /** |
||
137 | * Add the GravityView Fields group tooltip |
||
138 | * |
||
139 | * @param $tooltips |
||
140 | * |
||
141 | * @return array Tooltips array with GravityView fields tooltip |
||
142 | */ |
||
143 | function tooltips( $tooltips ) { |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Inject new add field buttons in the gravity form editor page |
||
156 | * |
||
157 | * @access public |
||
158 | * @param mixed $field_groups |
||
159 | * @return array Array of fields |
||
160 | */ |
||
161 | function add_field_buttons( $field_groups ) { |
||
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * At edit form page, set the field Approve defaults |
||
191 | * |
||
192 | * @todo Convert to a partial include file |
||
193 | * @access public |
||
194 | * @return void |
||
195 | */ |
||
196 | function set_defaults() { |
||
246 | |||
247 | /** |
||
248 | * Get the Bulk Action submitted value if it is a GravityView Approve/Unapprove action |
||
249 | * |
||
250 | * @since 1.17.1 |
||
251 | * |
||
252 | * @return string|false If the bulk action was GravityView Approve/Unapprove, return the full string (gvapprove-16, gvunapprove-16). Otherwise, return false. |
||
253 | */ |
||
254 | private function get_gv_bulk_action() { |
||
272 | |||
273 | /** |
||
274 | * Capture bulk actions - gf_entries table |
||
275 | * |
||
276 | * @uses GravityView_frontend::get_search_criteria() Convert the $_POST search request into a properly formatted request. |
||
277 | * @access public |
||
278 | * @return void|boolean |
||
279 | */ |
||
280 | public function process_bulk_action() { |
||
350 | |||
351 | |||
352 | |||
353 | |||
354 | |||
355 | /** |
||
356 | * Process a bulk of entries to update the approve field/property |
||
357 | * |
||
358 | * @access private |
||
359 | * @static |
||
360 | * @param array|boolean $entries If array, array of entry IDs that are to be updated. If true: update all entries. |
||
361 | * @param int $approved Approved status. If `0`: unapproved, if not empty, `Approved` |
||
362 | * @param int $form_id The Gravity Forms Form ID |
||
363 | * @return boolean|void |
||
364 | */ |
||
365 | private static function update_bulk( $entries, $approved, $form_id ) { |
||
386 | |||
387 | |||
388 | |||
389 | |||
390 | /** |
||
391 | * update_approved function. |
||
392 | * |
||
393 | * @access public |
||
394 | * @static |
||
395 | * @param int $entry_id (default: 0) |
||
396 | * @param int $approved (default: 0) |
||
397 | * @param int $form_id (default: 0) |
||
398 | * @param int $approvedcolumn (default: 0) |
||
399 | * @return boolean True: It worked; False: it failed |
||
400 | */ |
||
401 | public static function update_approved( $entry_id = 0, $approved = 0, $form_id = 0, $approvedcolumn = 0) { |
||
460 | |||
461 | |||
462 | /** |
||
463 | * Update the is_approved meta whenever the entry is submitted (and it contains a User Opt-in field) |
||
464 | * |
||
465 | * @since 1.16.6 |
||
466 | * |
||
467 | * @param $entry array Gravity Forms entry object |
||
468 | * @param $form array Gravity Forms form object |
||
469 | */ |
||
470 | public function after_submission( $entry, $form ) { |
||
471 | $this->after_update_entry_update_approved_meta( $form , $entry['id'] ); |
||
472 | } |
||
473 | |||
474 | |||
475 | |||
476 | /** |
||
477 | * Update the is_approved meta whenever the entry is updated |
||
478 | * |
||
479 | * @since 1.7.6.1 Was previously named `update_approved_meta` |
||
480 | * |
||
481 | * @param array $form Gravity Forms form array |
||
482 | * @param int $entry_id ID of the Gravity Forms entry |
||
483 | * @return void |
||
484 | */ |
||
485 | public function after_update_entry_update_approved_meta( $form, $entry_id = NULL ) { |
||
486 | |||
487 | $approvedcolumn = self::get_approved_column( $form['id'] ); |
||
488 | |||
489 | /** |
||
490 | * If the form doesn't contain the approve field, don't assume anything. |
||
491 | */ |
||
492 | if( empty( $approvedcolumn ) ) { |
||
493 | return; |
||
494 | } |
||
495 | |||
496 | $entry = GFAPI::get_entry( $entry_id ); |
||
497 | |||
498 | self::update_approved_meta( $entry_id, $entry[ (string)$approvedcolumn ], $form['id'] ); |
||
499 | |||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Update the `is_approved` entry meta value |
||
504 | * |
||
505 | * @since 1.7.6.1 `after_update_entry_update_approved_meta` was previously to be named `update_approved_meta` |
||
506 | * @since 1.17.1 Added $form_id parameter |
||
507 | * |
||
508 | * @param int $entry_id ID of the Gravity Forms entry |
||
509 | * @param string $is_approved String whether entry is approved or not. `0` for not approved, `Approved` for approved. |
||
510 | * @param int $form_id ID of the form of the entry being updated. Improves query performance. |
||
511 | * |
||
512 | * @return void |
||
513 | */ |
||
514 | private static function update_approved_meta( $entry_id, $is_approved, $form_id = 0 ) { |
||
515 | |||
516 | /** |
||
517 | * Make sure that the "User Opt-in" and the Admin Approve/Reject entry set the same meta value |
||
518 | * @since 1.16.6 |
||
519 | */ |
||
520 | $is_approved = empty( $is_approved ) ? 0 : 'Approved'; |
||
521 | |||
522 | // update entry meta |
||
523 | if( function_exists('gform_update_meta') ) { |
||
524 | |||
525 | gform_update_meta( $entry_id, 'is_approved', $is_approved, $form_id ); |
||
526 | |||
527 | /** |
||
528 | * @action `gravityview/approve_entries/updated` Triggered when an entry approval is updated |
||
529 | * @since 1.7.6.1 |
||
530 | * @param int $entry_id ID of the Gravity Forms entry |
||
531 | * @param string $is_approved String whether entry is approved or not. `0` for not approved, `Approved` for approved. |
||
532 | */ |
||
533 | do_action( 'gravityview/approve_entries/updated', $entry_id, $is_approved ); |
||
534 | |||
535 | if( empty( $is_approved ) ) { |
||
536 | |||
537 | /** |
||
538 | * @action `gravityview/approve_entries/disapproved` Triggered when an entry is rejected |
||
539 | * @since 1.7.6.1 |
||
540 | * @param int $entry_id ID of the Gravity Forms entry |
||
541 | */ |
||
542 | do_action( 'gravityview/approve_entries/disapproved', $entry_id ); |
||
543 | |||
544 | } else { |
||
545 | |||
546 | /** |
||
547 | * @action `gravityview/approve_entries/approved` Triggered when an entry is approved |
||
548 | * @since 1.7.6.1 |
||
549 | * @param int $entry_id ID of the Gravity Forms entry |
||
550 | */ |
||
551 | do_action( 'gravityview/approve_entries/approved', $entry_id ); |
||
552 | |||
553 | } |
||
1 ignored issue
–
show
|
|||
554 | |||
555 | } else { |
||
556 | |||
557 | do_action('gravityview_log_error', __METHOD__ . ' - `gform_update_meta` does not exist.' ); |
||
558 | |||
559 | } |
||
560 | } |
||
561 | |||
562 | |||
563 | /** |
||
564 | * Approve/Disapprove entries using the × or ✓ icons in the GF Entries screen |
||
565 | * @return void |
||
566 | */ |
||
567 | public function ajax_update_approved() { |
||
604 | |||
605 | |||
606 | /** |
||
607 | * Calculate the approve field.input id |
||
608 | * |
||
609 | * @access public |
||
610 | * @static |
||
611 | * @param mixed $form GF Form or Form ID |
||
612 | * @return false|null|string Returns the input ID of the approved field. Returns NULL if no approved fields were found. Returns false if $form_id wasn't set. |
||
613 | */ |
||
614 | static public function get_approved_column( $form ) { |
||
646 | |||
647 | /** |
||
648 | * Add a hidden input that is used in the Javascript to show approved/disapproved entries checkbox |
||
649 | * |
||
650 | * See the /assets/js/admin-entries-list.js setInitialApprovedEntries method |
||
651 | * |
||
652 | * @param $form_id |
||
653 | * @param $field_id |
||
654 | * @param $value |
||
655 | * @param $entry |
||
656 | * @param $query_string |
||
657 | * |
||
658 | * @return void |
||
659 | */ |
||
660 | static public function add_entry_approved_hidden_input( $form_id, $field_id, $value, $entry, $query_string ) { |
||
674 | |||
675 | /** |
||
676 | * Get the form ID of the form currently being displayed |
||
677 | * |
||
678 | * @since 1.17.1 |
||
679 | * |
||
680 | * @return int ID of the current form being displayed |
||
681 | */ |
||
682 | private function get_form_id() { |
||
696 | |||
697 | |||
698 | function add_scripts_and_styles( $hook ) { |
||
735 | |||
736 | /** |
||
737 | * Get an array of options to be added to the Gravity Forms "Bulk action" dropdown in a "GravityView" option group |
||
738 | * |
||
739 | * @since 1.16.3 |
||
740 | * |
||
741 | * @param int $form_id ID of the form currently being displayed |
||
742 | * |
||
743 | * @return array Array of actions to be added to the GravityView option group |
||
744 | */ |
||
745 | private function get_bulk_actions( $form_id ) { |
||
779 | |||
780 | /** |
||
781 | * Should the Approve/Reject Entry column be shown in the GF Entries page? |
||
782 | * |
||
783 | * @since 1.7.2 |
||
784 | * |
||
785 | * @param int $form_id The ID of the Gravity Forms form for which entries are being shown |
||
786 | * |
||
787 | * @return bool True: Show column; False: hide column |
||
788 | */ |
||
789 | private function show_approve_entry_column( $form_id ) { |
||
818 | |||
819 | function register_gform_noconflict_script( $scripts ) { |
||
823 | |||
824 | function register_gform_noconflict_style( $styles ) { |
||
828 | |||
829 | } |
||
830 | |||
831 | new GravityView_Admin_ApproveEntries; |
||
832 |
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.