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 EE_Addon 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 EE_Addon, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class EE_Addon extends EE_Configurable implements RequiresDependencyMapInterface, RequiresDomainInterface |
||
21 | { |
||
22 | |||
23 | |||
24 | /** |
||
25 | * prefix to be added onto an addon's plugin slug to make a wp option name |
||
26 | * which will be used to store the plugin's activation history |
||
27 | */ |
||
28 | const ee_addon_version_history_option_prefix = 'ee_version_history_'; |
||
29 | |||
30 | /** |
||
31 | * @var $_version |
||
32 | * @type string |
||
33 | */ |
||
34 | protected $_version = ''; |
||
35 | |||
36 | /** |
||
37 | * @var $_min_core_version |
||
38 | * @type string |
||
39 | */ |
||
40 | protected $_min_core_version = ''; |
||
41 | |||
42 | /** |
||
43 | * derived from plugin 'main_file_path using plugin_basename() |
||
44 | * |
||
45 | * @type string $_plugin_basename |
||
46 | */ |
||
47 | protected $_plugin_basename = ''; |
||
48 | |||
49 | /** |
||
50 | * A non-internationalized name to identify this addon for use in URLs, etc |
||
51 | * |
||
52 | * @type string $_plugin_slug |
||
53 | */ |
||
54 | protected $_plugin_slug = ''; |
||
55 | |||
56 | /** |
||
57 | * A non-internationalized name to identify this addon. Eg 'Calendar','MailChimp',etc/ |
||
58 | * |
||
59 | * @type string _addon_name |
||
60 | */ |
||
61 | protected $_addon_name = ''; |
||
62 | |||
63 | /** |
||
64 | * one of the EE_System::req_type_* constants |
||
65 | * |
||
66 | * @type int $_req_type |
||
67 | */ |
||
68 | protected $_req_type; |
||
69 | |||
70 | /** |
||
71 | * page slug to be used when generating the "Settings" link on the WP plugin page |
||
72 | * |
||
73 | * @type string $_plugin_action_slug |
||
74 | */ |
||
75 | protected $_plugin_action_slug = ''; |
||
76 | |||
77 | /** |
||
78 | * if not empty, inserts a new table row after this plugin's row on the WP Plugins page |
||
79 | * that can be used for adding upgrading/marketing info |
||
80 | * |
||
81 | * @type array $_plugins_page_row |
||
82 | */ |
||
83 | protected $_plugins_page_row = array(); |
||
84 | |||
85 | |||
86 | |||
87 | /** |
||
88 | * filepath to the main file, which can be used for register_activation_hook, register_deactivation_hook, etc. |
||
89 | * |
||
90 | * @type string |
||
91 | */ |
||
92 | protected $_main_plugin_file; |
||
93 | |||
94 | |||
95 | /** |
||
96 | * @var EE_Dependency_Map $dependency_map |
||
97 | */ |
||
98 | private $dependency_map; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * @var DomainInterface $domain |
||
103 | */ |
||
104 | private $domain; |
||
105 | |||
106 | |||
107 | /** |
||
108 | * @param EE_Dependency_Map $dependency_map [optional] |
||
109 | * @param DomainInterface $domain [optional] |
||
110 | */ |
||
111 | public function __construct(EE_Dependency_Map $dependency_map = null, DomainInterface $domain = null) |
||
121 | |||
122 | |||
123 | /** |
||
124 | * @param EE_Dependency_Map $dependency_map |
||
125 | */ |
||
126 | public function setDependencyMap($dependency_map) |
||
130 | |||
131 | |||
132 | /** |
||
133 | * @return EE_Dependency_Map |
||
134 | */ |
||
135 | public function dependencyMap() |
||
139 | |||
140 | |||
141 | /** |
||
142 | * @param DomainInterface $domain |
||
143 | */ |
||
144 | public function setDomain(DomainInterface $domain) |
||
148 | |||
149 | /** |
||
150 | * @return DomainInterface |
||
151 | */ |
||
152 | public function domain() |
||
156 | |||
157 | |||
158 | /** |
||
159 | * @param mixed $version |
||
160 | */ |
||
161 | public function set_version($version = null) |
||
165 | |||
166 | |||
167 | /** |
||
168 | * get__version |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | public function version() |
||
176 | |||
177 | |||
178 | /** |
||
179 | * @param mixed $min_core_version |
||
180 | */ |
||
181 | public function set_min_core_version($min_core_version = null) |
||
185 | |||
186 | |||
187 | /** |
||
188 | * get__min_core_version |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | public function min_core_version() |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Sets addon_name |
||
200 | * |
||
201 | * @param string $addon_name |
||
202 | * @return boolean |
||
203 | */ |
||
204 | public function set_name($addon_name) |
||
208 | |||
209 | |||
210 | /** |
||
211 | * Gets addon_name |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | public function name() |
||
219 | |||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | public function plugin_basename() |
||
229 | |||
230 | |||
231 | /** |
||
232 | * @param string $plugin_basename |
||
233 | */ |
||
234 | public function set_plugin_basename($plugin_basename) |
||
239 | |||
240 | |||
241 | /** |
||
242 | * @return string |
||
243 | */ |
||
244 | public function plugin_slug() |
||
249 | |||
250 | |||
251 | /** |
||
252 | * @param string $plugin_slug |
||
253 | */ |
||
254 | public function set_plugin_slug($plugin_slug) |
||
259 | |||
260 | |||
261 | /** |
||
262 | * @return string |
||
263 | */ |
||
264 | public function plugin_action_slug() |
||
269 | |||
270 | |||
271 | /** |
||
272 | * @param string $plugin_action_slug |
||
273 | */ |
||
274 | public function set_plugin_action_slug($plugin_action_slug) |
||
279 | |||
280 | |||
281 | /** |
||
282 | * @return array |
||
283 | */ |
||
284 | public function get_plugins_page_row() |
||
289 | |||
290 | |||
291 | /** |
||
292 | * @param array $plugins_page_row |
||
293 | */ |
||
294 | public function set_plugins_page_row($plugins_page_row = array()) |
||
304 | |||
305 | |||
306 | /** |
||
307 | * Called when EE core detects this addon has been activated for the first time. |
||
308 | * If the site isn't in maintenance mode, should setup the addon's database |
||
309 | * |
||
310 | * @return void |
||
311 | */ |
||
312 | View Code Duplication | public function new_install() |
|
323 | |||
324 | |||
325 | /** |
||
326 | * Called when EE core detects this addon has been reactivated. When this happens, |
||
327 | * it's good to just check that your data is still intact |
||
328 | * |
||
329 | * @return void |
||
330 | */ |
||
331 | View Code Duplication | public function reactivation() |
|
342 | |||
343 | |||
344 | /** |
||
345 | * Called when the registered deactivation hook for this addon fires. |
||
346 | * @throws EE_Error |
||
347 | */ |
||
348 | public function deactivation() |
||
357 | |||
358 | |||
359 | /** |
||
360 | * Takes care of double-checking that we're not in maintenance mode, and then |
||
361 | * initializing this addon's necessary initial data. This is called by default on new activations |
||
362 | * and reactivations. |
||
363 | * |
||
364 | * @param boolean $verify_schema whether to verify the database's schema for this addon, or just its data. |
||
365 | * This is a resource-intensive job so we prefer to only do it when necessary |
||
366 | * @return void |
||
367 | * @throws \EE_Error |
||
368 | * @throws InvalidInterfaceException |
||
369 | * @throws InvalidDataTypeException |
||
370 | * @throws InvalidArgumentException |
||
371 | */ |
||
372 | public function initialize_db_if_no_migrations_required($verify_schema = true) |
||
415 | |||
416 | |||
417 | /** |
||
418 | * Used to setup this addon's database tables, but not necessarily any default |
||
419 | * data in them. The default is to actually use the most up-to-date data migration script |
||
420 | * for this addon, and just use its schema_changes_before_migration() and schema_changes_after_migration() |
||
421 | * methods to setup the db. |
||
422 | */ |
||
423 | public function initialize_db() |
||
446 | |||
447 | |||
448 | /** |
||
449 | * If you want to setup default data for the addon, override this method, and call |
||
450 | * parent::initialize_default_data() from within it. This is normally called |
||
451 | * from EE_Addon::initialize_db_if_no_migrations_required(), just after EE_Addon::initialize_db() |
||
452 | * and should verify default data is present (but this is also called |
||
453 | * on reactivations and just after migrations, so please verify you actually want |
||
454 | * to ADD default data, because it may already be present). |
||
455 | * However, please call this parent (currently it just fires a hook which other |
||
456 | * addons may be depending on) |
||
457 | */ |
||
458 | public function initialize_default_data() |
||
471 | |||
472 | |||
473 | /** |
||
474 | * EE Core detected that this addon has been upgraded. We should check if there |
||
475 | * are any new migration scripts, and if so put the site into maintenance mode until |
||
476 | * they're ran |
||
477 | * |
||
478 | * @return void |
||
479 | */ |
||
480 | View Code Duplication | public function upgrade() |
|
492 | |||
493 | |||
494 | /** |
||
495 | * If Core detects this addon has been downgraded, you may want to invoke some special logic here. |
||
496 | */ |
||
497 | public function downgrade() |
||
508 | |||
509 | |||
510 | /** |
||
511 | * set_db_update_option_name |
||
512 | * Until we do something better, we'll just check for migration scripts upon |
||
513 | * plugin activation only. In the future, we'll want to do it on plugin updates too |
||
514 | * |
||
515 | * @return bool |
||
516 | */ |
||
517 | public function set_db_update_option_name() |
||
530 | |||
531 | |||
532 | /** |
||
533 | * Returns the name of the activation indicator option |
||
534 | * (an option which is set temporarily to indicate that this addon was just activated) |
||
535 | * |
||
536 | * @deprecated since version 4.3.0.alpha.016 |
||
537 | * @return string |
||
538 | */ |
||
539 | public function get_db_update_option_name() |
||
551 | |||
552 | |||
553 | /** |
||
554 | * When the addon is activated, this should be called to set a wordpress option that |
||
555 | * indicates it was activated. This is especially useful for detecting reactivations. |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | public function set_activation_indicator_option() |
||
564 | |||
565 | |||
566 | /** |
||
567 | * Gets the name of the wp option which is used to temporarily indicate that this addon was activated |
||
568 | * |
||
569 | * @return string |
||
570 | */ |
||
571 | public function get_activation_indicator_option_name() |
||
575 | |||
576 | |||
577 | /** |
||
578 | * Used by EE_System to set the request type of this addon. Should not be used by addon developers |
||
579 | * |
||
580 | * @param int $req_type |
||
581 | */ |
||
582 | public function set_req_type($req_type) |
||
586 | |||
587 | |||
588 | /** |
||
589 | * Returns the request type of this addon (ie, EE_System::req_type_normal, EE_System::req_type_new_activation, |
||
590 | * EE_System::req_type_reactivation, EE_System::req_type_upgrade, or EE_System::req_type_downgrade). This is set by |
||
591 | * EE_System when it is checking for new install or upgrades of addons |
||
592 | */ |
||
593 | public function detect_req_type() |
||
600 | |||
601 | |||
602 | /** |
||
603 | * Detects the request type for this addon (whether it was just activated, upgrades, a normal request, etc.) |
||
604 | * Should only be called once per request |
||
605 | * |
||
606 | * @return void |
||
607 | */ |
||
608 | public function detect_activation_or_upgrade() |
||
652 | |||
653 | /** |
||
654 | * Updates the version history for this addon |
||
655 | * |
||
656 | * @param array $version_history |
||
657 | * @param string $current_version_to_add |
||
658 | * @return boolean success |
||
659 | */ |
||
660 | public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
||
673 | |||
674 | /** |
||
675 | * Gets the name of the wp option that stores the activation history |
||
676 | * of this addon |
||
677 | * |
||
678 | * @return string |
||
679 | */ |
||
680 | public function get_activation_history_option_name() |
||
684 | |||
685 | |||
686 | /** |
||
687 | * Gets the wp option which stores the activation history for this addon |
||
688 | * |
||
689 | * @return array |
||
690 | */ |
||
691 | public function get_activation_history() |
||
695 | |||
696 | |||
697 | /** |
||
698 | * @param string $config_section |
||
699 | */ |
||
700 | public function set_config_section($config_section = '') |
||
704 | |||
705 | /** |
||
706 | * Sets the filepath to the main plugin file |
||
707 | * |
||
708 | * @param string $filepath |
||
709 | */ |
||
710 | public function set_main_plugin_file($filepath) |
||
714 | |||
715 | /** |
||
716 | * gets the filepath to teh main file |
||
717 | * |
||
718 | * @return string |
||
719 | */ |
||
720 | public function get_main_plugin_file() |
||
724 | |||
725 | /** |
||
726 | * Gets the filename (no path) of the main file (the main file loaded |
||
727 | * by WP) |
||
728 | * |
||
729 | * @return string |
||
730 | */ |
||
731 | public function get_main_plugin_file_basename() |
||
735 | |||
736 | /** |
||
737 | * Gets the folder name which contains the main plugin file |
||
738 | * |
||
739 | * @return string |
||
740 | */ |
||
741 | public function get_main_plugin_file_dirname() |
||
745 | |||
746 | |||
747 | /** |
||
748 | * sets hooks used in the admin |
||
749 | * |
||
750 | * @return void |
||
751 | */ |
||
752 | public function admin_init() |
||
760 | |||
761 | |||
762 | /** |
||
763 | * plugin_actions |
||
764 | * Add a settings link to the Plugins page, so people can go straight from the plugin page to the settings page. |
||
765 | * |
||
766 | * @param $links |
||
767 | * @param $file |
||
768 | * @return array |
||
769 | */ |
||
770 | public function plugin_action_links($links, $file) |
||
783 | |||
784 | |||
785 | /** |
||
786 | * after_plugin_row |
||
787 | * Add additional content to the plugins page plugin row |
||
788 | * Inserts another row |
||
789 | * |
||
790 | * @param $plugin_file |
||
791 | * @param $plugin_data |
||
792 | * @param $status |
||
793 | * @return void |
||
794 | */ |
||
795 | public function after_plugin_row($plugin_file, $plugin_data, $status) |
||
866 | |||
867 | |||
868 | /** |
||
869 | * A safe space for addons to add additional logic like setting hooks that need to be set early in the request. |
||
870 | * Child classes that have logic like that to run can override this method declaration. This was not made abstract |
||
871 | * for back compat reasons. |
||
872 | * |
||
873 | * This will fire on the `AHEE__EE_System__load_espresso_addons__complete` hook at priority 999. |
||
874 | * |
||
875 | * It is recommended, if client code is `de-registering` an add-on, then do it on the |
||
876 | * `AHEE__EE_System__load_espresso_addons__complete` hook before priority 999 so as to ensure any code logic in this |
||
877 | * callback does not get run/set in that request. |
||
878 | * |
||
879 | * Also, keep in mind that if a registered add-on happens to be deactivated via |
||
880 | * EE_System::_deactivate_incompatible_addons() because its incompatible, any code executed in this method |
||
881 | * (including setting hooks etc) will have executed before the plugin was deactivated. If you use |
||
882 | * `after_registration` to set any filter and/or action hooks and want to ensure they are removed on this add-on's |
||
883 | * deactivation, you can override `EE_Addon::deactivation` and unset your hooks and filters there. Just remember |
||
884 | * to call `parent::deactivation`. |
||
885 | * |
||
886 | * @since 4.9.26 |
||
887 | */ |
||
888 | public function after_registration() |
||
892 | } |
||
893 |