Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Give_Updates 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 Give_Updates, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Give_Updates { |
||
|
|||
9 | |||
10 | /** |
||
11 | * Instance. |
||
12 | * |
||
13 | * @since |
||
14 | * @access static |
||
15 | * @var |
||
16 | */ |
||
17 | static private $instance; |
||
18 | |||
19 | /** |
||
20 | * Instance. |
||
21 | * |
||
22 | * @since |
||
23 | * @access public |
||
24 | * @var Give_Background_Updater |
||
25 | */ |
||
26 | static public $background_updater; |
||
27 | |||
28 | /** |
||
29 | * Updates |
||
30 | * |
||
31 | * @since 1.8.12 |
||
32 | * @access private |
||
33 | * @var array |
||
34 | */ |
||
35 | private $updates = array(); |
||
36 | |||
37 | /** |
||
38 | * Current update percentage number |
||
39 | * |
||
40 | * @since 1.8.12 |
||
41 | * @access private |
||
42 | * @var array |
||
43 | */ |
||
44 | public $percentage = 0; |
||
45 | |||
46 | /** |
||
47 | * Current update step number |
||
48 | * |
||
49 | * @since 1.8.12 |
||
50 | * @access private |
||
51 | * @var array |
||
52 | */ |
||
53 | public $step = 1; |
||
54 | |||
55 | /** |
||
56 | * Current update number |
||
57 | * |
||
58 | * @since 1.8.12 |
||
59 | * @access private |
||
60 | * @var array |
||
61 | */ |
||
62 | public $update = 1; |
||
63 | |||
64 | /** |
||
65 | * Singleton pattern. |
||
66 | * |
||
67 | * @since 1.8.12 |
||
68 | * @access private |
||
69 | * |
||
70 | * @param Give_Updates . |
||
71 | */ |
||
72 | private function __construct() { |
||
74 | |||
75 | /** |
||
76 | * Register updates |
||
77 | * |
||
78 | * @since 1.8.12 |
||
79 | * @access public |
||
80 | * |
||
81 | * @param array $args |
||
82 | */ |
||
83 | public function register( $args ) { |
||
112 | |||
113 | /** |
||
114 | * Get instance. |
||
115 | * |
||
116 | * @since |
||
117 | * @access static |
||
118 | * @return static |
||
119 | */ |
||
120 | static function get_instance() { |
||
127 | |||
128 | /** |
||
129 | * |
||
130 | * Setup hook |
||
131 | * |
||
132 | * @since 1.8.12 |
||
133 | * @access public |
||
134 | */ |
||
135 | public function setup() { |
||
162 | |||
163 | /** |
||
164 | * Register plugin add-on updates. |
||
165 | * |
||
166 | * @since 1.8.12 |
||
167 | * @access public |
||
168 | */ |
||
169 | public function __register_plugin_addon_updates() { |
||
170 | $addons = give_get_plugins( array( 'only_premium_add_ons' => true ) ); |
||
171 | $plugin_updates = get_plugin_updates(); |
||
172 | |||
173 | foreach ( $addons as $key => $info ) { |
||
174 | if ( empty( $plugin_updates[ $key ] ) ) { |
||
175 | continue; |
||
176 | } |
||
177 | |||
178 | $this->updates['plugin'][] = array_merge( $info, (array) $plugin_updates[ $key ] ); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Fire custom action hook to register updates |
||
185 | * |
||
186 | * @since 1.8.12 |
||
187 | * @access public |
||
188 | */ |
||
189 | public function __register_upgrade() { |
||
201 | |||
202 | /** |
||
203 | * Rename `Donations` menu title if updates exists |
||
204 | * |
||
205 | * @since 1.8.12 |
||
206 | * @access public |
||
207 | */ |
||
208 | function __change_donations_label() { |
||
235 | |||
236 | /** |
||
237 | * Register updates menu |
||
238 | * |
||
239 | * @since 1.8.12 |
||
240 | * @access public |
||
241 | */ |
||
242 | public function __register_menu() { |
||
284 | |||
285 | |||
286 | /** |
||
287 | * Show update related notices |
||
288 | * |
||
289 | * @since 2.0 |
||
290 | * @access public |
||
291 | */ |
||
292 | public function __redirect_admin() { |
||
306 | |||
307 | |||
308 | /** |
||
309 | * Pause db upgrade |
||
310 | * |
||
311 | * @since 2.0.1 |
||
312 | * @access public |
||
313 | * |
||
314 | * @param bool $force |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function __pause_db_update( $force = false ) { |
||
371 | |||
372 | /** |
||
373 | * Restart db upgrade |
||
374 | * |
||
375 | * @since 2.0.1 |
||
376 | * @access public |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | public function __restart_db_update() { |
||
415 | |||
416 | /** |
||
417 | * Health check for updates. |
||
418 | * |
||
419 | * @since 2.0 |
||
420 | * @access public |
||
421 | * |
||
422 | * @param Give_Updates $give_updates |
||
423 | */ |
||
424 | public function __health_background_update( $give_updates ) { |
||
579 | |||
580 | |||
581 | /** |
||
582 | * Show update related notices |
||
583 | * |
||
584 | * @since 2.0 |
||
585 | * @access public |
||
586 | */ |
||
587 | public function __show_notice() { |
||
588 | $current_screen = get_current_screen(); |
||
589 | $hide_on_pages = array( |
||
590 | 'give_forms_page_give-updates', |
||
591 | 'update-core', |
||
592 | 'give_forms_page_give-addons' |
||
593 | ); |
||
594 | |||
595 | // Bailout. |
||
596 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
||
597 | return; |
||
598 | } |
||
599 | |||
600 | // Run DB updates. |
||
601 | if ( ! empty( $_GET['give-run-db-update'] ) ) { |
||
602 | $this->run_db_update(); |
||
603 | } |
||
604 | |||
605 | // Bailout. |
||
606 | if ( in_array( $current_screen->base, $hide_on_pages ) ) { |
||
607 | return; |
||
608 | } |
||
609 | |||
610 | // Show notice if upgrade paused. |
||
611 | if ( self::$background_updater->is_paused_process() ) { |
||
612 | ob_start(); |
||
613 | |||
614 | $upgrade_error = get_option( 'give_upgrade_error' ); |
||
615 | if ( ! $upgrade_error ) : ?> |
||
616 | <strong><?php _e( 'Database Update', 'give' ); ?></strong> |
||
617 | – <?php _e( 'GiveWP needs to update your database to the latest version. The following process will make updates to your site\'s database. Please create a backup before proceeding.', 'give' ); ?> |
||
618 | <br> |
||
619 | <br> |
||
620 | <a href="<?php echo esc_url( add_query_arg( array( 'give-restart-db-upgrades' => 1 ), admin_url( 'edit.php?post_type=give_forms&page=give-updates' ) ) ); ?>" class="button button-primary give-restart-updater-btn"> |
||
621 | <?php _e( 'Restart the updater', 'give' ); ?> |
||
622 | </a> |
||
623 | <?php else: ?> |
||
624 | <strong><?php _e( 'Database Update', 'give' ); ?></strong> |
||
625 | – <?php _e( 'An unexpected issue occurred during the database update which caused it to stop automatically. Please contact support for assistance.', 'give' ); ?> |
||
626 | <a href="<?php echo esc_url('http://docs.givewp.com/troubleshooting-db-updates')?>" target="_blank"><?php _e( 'Read More', 'give' ); ?> »</a> |
||
627 | <?php |
||
628 | endif; |
||
629 | $desc_html = ob_get_clean(); |
||
630 | |||
631 | Give()->notices->register_notice( array( |
||
632 | 'id' => 'give_upgrade_db', |
||
633 | 'type' => 'error', |
||
634 | 'dismissible' => false, |
||
635 | 'description' => $desc_html, |
||
636 | ) ); |
||
637 | } |
||
638 | |||
639 | // Bailout if doing upgrades. |
||
640 | if ( $this->is_doing_updates() ) { |
||
641 | return; |
||
642 | } |
||
643 | |||
644 | // Show db upgrade completed notice. |
||
645 | if ( ! empty( $_GET['give-db-update-completed'] ) ) { |
||
646 | Give()->notices->register_notice( array( |
||
647 | 'id' => 'give_db_upgrade_completed', |
||
648 | 'type' => 'updated', |
||
649 | 'description' => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ), |
||
650 | 'show' => true, |
||
651 | ) ); |
||
652 | |||
653 | // Start update. |
||
654 | } elseif ( ! empty( $_GET['give-run-db-update'] ) ) { |
||
655 | $this->run_db_update(); |
||
656 | |||
657 | // Show run the update notice. |
||
658 | } elseif ( $this->get_total_new_db_update_count() ) { |
||
659 | ob_start(); |
||
660 | ?> |
||
661 | <p> |
||
662 | <strong><?php _e( 'Database Update', 'give' ); ?></strong> |
||
663 | – <?php _e( 'GiveWP needs to update your database to the latest version. The following process will make updates to your site\'s database. Please create a complete backup before proceeding.', 'give' ); ?> |
||
664 | </p> |
||
665 | <p class="submit"> |
||
666 | <a href="<?php echo esc_url( add_query_arg( array( 'give-run-db-update' => 1 ), admin_url( 'edit.php?post_type=give_forms&page=give-updates' ) ) ); ?>" class="button button-primary give-run-update-now"> |
||
667 | <?php _e( 'Run the updater', 'give' ); ?> |
||
668 | </a> |
||
669 | </p> |
||
670 | <?php |
||
671 | $desc_html = ob_get_clean(); |
||
672 | |||
673 | Give()->notices->register_notice( array( |
||
674 | 'id' => 'give_upgrade_db', |
||
675 | 'type' => 'updated', |
||
676 | 'dismissible' => false, |
||
677 | 'description' => $desc_html, |
||
678 | ) ); |
||
679 | } |
||
680 | } |
||
681 | |||
682 | /** |
||
683 | * Render Give Updates Completed page |
||
684 | * |
||
685 | * @since 1.8.12 |
||
686 | * @access public |
||
687 | */ |
||
688 | public function render_complete_page() { |
||
689 | include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades-complete.php'; |
||
690 | } |
||
691 | |||
692 | /** |
||
693 | * Render Give Updates page |
||
694 | * |
||
695 | * @since 1.8.12 |
||
696 | * @access public |
||
697 | */ |
||
698 | public function render_page() { |
||
699 | include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades.php'; |
||
700 | } |
||
701 | |||
702 | /** |
||
703 | * Run database upgrades |
||
704 | * |
||
705 | * @since 2.0 |
||
706 | * @access private |
||
707 | */ |
||
708 | private function run_db_update() { |
||
709 | // Bailout. |
||
710 | if ( $this->is_doing_updates() || ! $this->get_total_new_db_update_count() ) { |
||
711 | return; |
||
712 | } |
||
713 | |||
714 | $updates = $this->get_updates( 'database', 'new' ); |
||
715 | |||
716 | foreach ( $updates as $update ) { |
||
717 | self::$background_updater->push_to_queue( $update ); |
||
718 | } |
||
719 | |||
720 | add_option( 'give_db_update_count', count( $updates ), '', false ); |
||
721 | |||
722 | add_option( 'give_doing_upgrade', array( |
||
723 | 'update_info' => $updates[0], |
||
724 | 'step' => 1, |
||
725 | 'update' => 1, |
||
726 | 'heading' => sprintf( 'Update %s of %s', 1, count( $updates ) ), |
||
727 | 'percentage' => 0, |
||
728 | 'total_percentage' => 0, |
||
729 | ), '', false ); |
||
730 | |||
731 | self::$background_updater->save()->dispatch(); |
||
732 | } |
||
733 | |||
734 | |||
735 | /** |
||
736 | * Delete resume updates |
||
737 | * |
||
738 | * @since 1.8.12 |
||
739 | * @access public |
||
740 | */ |
||
741 | public function __flush_resume_updates() { |
||
742 | $this->step = $this->percentage = 0; |
||
743 | |||
744 | $this->update = ( $this->get_total_db_update_count() > $this->update ) ? |
||
745 | ( $this->update + 1 ) : |
||
746 | $this->update; |
||
747 | } |
||
748 | |||
749 | |||
750 | /** |
||
751 | * Initialize updates |
||
752 | * |
||
753 | * @since 2.0 |
||
754 | * @access public |
||
755 | * |
||
756 | * @return void |
||
757 | */ |
||
758 | public function __give_start_updating() { |
||
780 | |||
781 | |||
782 | /** |
||
783 | * This function handle ajax query for dn update status. |
||
784 | * |
||
785 | * @since 2.0 |
||
786 | * @access public |
||
787 | * |
||
788 | * @return string |
||
789 | */ |
||
790 | public function __give_db_updates_info() { |
||
825 | |||
826 | /** |
||
827 | * Send ajax response |
||
828 | * |
||
829 | * @since 1.8.12 |
||
830 | * @access public |
||
831 | * |
||
832 | * @param $data |
||
833 | * @param string $type |
||
834 | */ |
||
835 | public function send_ajax_response( $data, $type = '' ) { |
||
866 | |||
867 | /** |
||
868 | * Set current update percentage. |
||
869 | * |
||
870 | * @since 1.8.12 |
||
871 | * @access public |
||
872 | * |
||
873 | * @param $total |
||
874 | * @param $current_total |
||
875 | */ |
||
876 | public function set_percentage( $total, $current_total ) { |
||
883 | |||
884 | /** |
||
885 | * Check if parent update completed or not. |
||
886 | * |
||
887 | * @since 2.0 |
||
888 | * @access private |
||
889 | * |
||
890 | * @param array $update |
||
891 | * |
||
892 | * @return bool|null |
||
893 | */ |
||
894 | public function is_parent_updates_completed( $update ) { |
||
917 | |||
918 | /** |
||
919 | * Flag to check if DB updates running or not. |
||
920 | * |
||
921 | * @since 2.0 |
||
922 | * @access public |
||
923 | * @return bool |
||
924 | */ |
||
925 | public function is_doing_updates() { |
||
928 | |||
929 | |||
930 | /** |
||
931 | * Check if update has valid dependency or not. |
||
932 | * |
||
933 | * @since 2.0 |
||
934 | * @access public |
||
935 | * |
||
936 | * @param $update |
||
937 | * |
||
938 | * @return bool |
||
939 | */ |
||
940 | public function has_valid_dependency( $update ) { |
||
954 | |||
955 | /** |
||
956 | * Get updates. |
||
957 | * |
||
958 | * @since 1.8.12 |
||
959 | * @access public |
||
960 | * |
||
961 | * @param string $update_type Tye of update. |
||
962 | * @param string $status Tye of update. |
||
963 | * |
||
964 | * @return array |
||
965 | */ |
||
966 | public function get_updates( $update_type = '', $status = 'all' ) { |
||
1000 | |||
1001 | /** |
||
1002 | * Get addon update count. |
||
1003 | * |
||
1004 | * @since 1.8.12 |
||
1005 | * @access public |
||
1006 | * @return int |
||
1007 | */ |
||
1008 | public function get_total_plugin_update_count() { |
||
1011 | |||
1012 | /** |
||
1013 | * Get total update count |
||
1014 | * |
||
1015 | * @since 1.8.12 |
||
1016 | * @access public |
||
1017 | * |
||
1018 | * @return int |
||
1019 | */ |
||
1020 | public function get_total_update_count() { |
||
1026 | |||
1027 | /** |
||
1028 | * Get total pending updates count |
||
1029 | * |
||
1030 | * @since 1.8.12 |
||
1031 | * @access public |
||
1032 | * |
||
1033 | * @return int |
||
1034 | */ |
||
1035 | public function get_pending_db_update_count() { |
||
1038 | |||
1039 | /** |
||
1040 | * Get total updates count |
||
1041 | * |
||
1042 | * @since 1.8.18 |
||
1043 | * @access public |
||
1044 | * |
||
1045 | * @return int |
||
1046 | */ |
||
1047 | public function get_total_db_update_count() { |
||
1050 | |||
1051 | /** |
||
1052 | * Get total new updates count |
||
1053 | * |
||
1054 | * @since 2.0 |
||
1055 | * @access public |
||
1056 | * |
||
1057 | * @param bool $refresh |
||
1058 | * |
||
1059 | * @return int |
||
1060 | */ |
||
1061 | public function get_total_new_db_update_count( $refresh = false ) { |
||
1068 | |||
1069 | /** |
||
1070 | * Get total new updates count |
||
1071 | * |
||
1072 | * @since 2.0 |
||
1073 | * @access public |
||
1074 | * |
||
1075 | * @param bool $refresh |
||
1076 | * |
||
1077 | * @return int |
||
1078 | */ |
||
1079 | public function get_running_db_update( $refresh = false ) { |
||
1089 | |||
1090 | /** |
||
1091 | * Get database update processing percentage. |
||
1092 | * |
||
1093 | * @since 2.0 |
||
1094 | * @access public |
||
1095 | * |
||
1096 | * @param bool $refresh |
||
1097 | * |
||
1098 | * @return float|int |
||
1099 | */ |
||
1100 | public function get_db_update_processing_percentage( $refresh = false ) { |
||
1120 | |||
1121 | |||
1122 | /** |
||
1123 | * Get all update ids. |
||
1124 | * |
||
1125 | * @since 2.0.3 |
||
1126 | * |
||
1127 | * @return array |
||
1128 | */ |
||
1129 | public function get_update_ids() { |
||
1135 | |||
1136 | /** |
||
1137 | * Get offset count |
||
1138 | * |
||
1139 | * @since 2.0.5 |
||
1140 | * @access public |
||
1141 | * |
||
1142 | * @param int $process_item_count |
||
1143 | * |
||
1144 | * @return float|int |
||
1145 | */ |
||
1146 | public function get_offset( $process_item_count ) { |
||
1151 | } |
||
1152 | |||
1153 | Give_Updates::get_instance()->setup(); |
||
1154 |