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 |
||
| 17 | abstract class EE_Addon extends EE_Configurable implements RequiresDependencyMapInterface, RequiresDomainInterface |
||
| 18 | { |
||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * prefix to be added onto an addon's plugin slug to make a wp option name |
||
| 23 | * which will be used to store the plugin's activation history |
||
| 24 | */ |
||
| 25 | const ee_addon_version_history_option_prefix = 'ee_version_history_'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var $_version |
||
| 29 | * @type string |
||
| 30 | */ |
||
| 31 | protected $_version = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var $_min_core_version |
||
| 35 | * @type string |
||
| 36 | */ |
||
| 37 | protected $_min_core_version = ''; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * derived from plugin 'main_file_path using plugin_basename() |
||
| 41 | * |
||
| 42 | * @type string $_plugin_basename |
||
| 43 | */ |
||
| 44 | protected $_plugin_basename = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * A non-internationalized name to identify this addon for use in URLs, etc |
||
| 48 | * |
||
| 49 | * @type string $_plugin_slug |
||
| 50 | */ |
||
| 51 | protected $_plugin_slug = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * A non-internationalized name to identify this addon. Eg 'Calendar','MailChimp',etc/ |
||
| 55 | * |
||
| 56 | * @type string _addon_name |
||
| 57 | */ |
||
| 58 | protected $_addon_name = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * one of the EE_System::req_type_* constants |
||
| 62 | * |
||
| 63 | * @type int $_req_type |
||
| 64 | */ |
||
| 65 | protected $_req_type; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * page slug to be used when generating the "Settings" link on the WP plugin page |
||
| 69 | * |
||
| 70 | * @type string $_plugin_action_slug |
||
| 71 | */ |
||
| 72 | protected $_plugin_action_slug = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * if not empty, inserts a new table row after this plugin's row on the WP Plugins page |
||
| 76 | * that can be used for adding upgrading/marketing info |
||
| 77 | * |
||
| 78 | * @type array $_plugins_page_row |
||
| 79 | */ |
||
| 80 | protected $_plugins_page_row = array(); |
||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * filepath to the main file, which can be used for register_activation_hook, register_deactivation_hook, etc. |
||
| 86 | * |
||
| 87 | * @type string |
||
| 88 | */ |
||
| 89 | protected $_main_plugin_file; |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * @var EE_Dependency_Map $dependency_map |
||
| 94 | */ |
||
| 95 | private $dependency_map; |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @var DomainInterface $domain |
||
| 100 | */ |
||
| 101 | private $domain; |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @param DomainInterface $domain |
||
| 106 | * @param EE_Dependency_Map $dependency_map |
||
| 107 | */ |
||
| 108 | public function __construct(EE_Dependency_Map $dependency_map, DomainInterface $domain = null) |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * @param EE_Dependency_Map $dependency_map |
||
| 118 | */ |
||
| 119 | public function setDependencyMap($dependency_map) |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @return EE_Dependency_Map |
||
| 127 | */ |
||
| 128 | public function dependencyMap() |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * @param DomainInterface $domain |
||
| 136 | */ |
||
| 137 | public function setDomain(DomainInterface $domain = null) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return DomainInterface |
||
| 144 | */ |
||
| 145 | public function domain() |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @param mixed $version |
||
| 153 | */ |
||
| 154 | public function set_version($version = null) |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * get__version |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function version() |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * @param mixed $min_core_version |
||
| 173 | */ |
||
| 174 | public function set_min_core_version($min_core_version = null) |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * get__min_core_version |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function min_core_version() |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Sets addon_name |
||
| 193 | * |
||
| 194 | * @param string $addon_name |
||
| 195 | * @return boolean |
||
| 196 | */ |
||
| 197 | public function set_name($addon_name) |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * Gets addon_name |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function name() |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function plugin_basename() |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $plugin_basename |
||
| 226 | */ |
||
| 227 | public function set_plugin_basename($plugin_basename) |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function plugin_slug() |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $plugin_slug |
||
| 246 | */ |
||
| 247 | public function set_plugin_slug($plugin_slug) |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function plugin_action_slug() |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $plugin_action_slug |
||
| 266 | */ |
||
| 267 | public function set_plugin_action_slug($plugin_action_slug) |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | public function get_plugins_page_row() |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * @param array $plugins_page_row |
||
| 286 | */ |
||
| 287 | public function set_plugins_page_row($plugins_page_row = array()) |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Called when EE core detects this addon has been activated for the first time. |
||
| 301 | * If the site isn't in maintenance mode, should setup the addon's database |
||
| 302 | * |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | View Code Duplication | public function new_install() |
|
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Called when EE core detects this addon has been reactivated. When this happens, |
||
| 320 | * it's good to just check that your data is still intact |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function reactivation() |
|
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Called when the registered deactivation hook for this addon fires. |
||
| 339 | * @throws EE_Error |
||
| 340 | */ |
||
| 341 | public function deactivation() |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * Takes care of double-checking that we're not in maintenance mode, and then |
||
| 354 | * initializing this addon's necessary initial data. This is called by default on new activations |
||
| 355 | * and reactivations. |
||
| 356 | * |
||
| 357 | * @param boolean $verify_schema whether to verify the database's schema for this addon, or just its data. |
||
| 358 | * This is a resource-intensive job so we prefer to only do it when necessary |
||
| 359 | * @return void |
||
| 360 | * @throws \EE_Error |
||
| 361 | */ |
||
| 362 | public function initialize_db_if_no_migrations_required($verify_schema = true) |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Used to setup this addon's database tables, but not necessarily any default |
||
| 405 | * data in them. The default is to actually use the most up-to-date data migration script |
||
| 406 | * for this addon, and just use its schema_changes_before_migration() and schema_changes_after_migration() |
||
| 407 | * methods to setup the db. |
||
| 408 | */ |
||
| 409 | public function initialize_db() |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * If you want to setup default data for the addon, override this method, and call |
||
| 436 | * parent::initialize_default_data() from within it. This is normally called |
||
| 437 | * from EE_Addon::initialize_db_if_no_migrations_required(), just after EE_Addon::initialize_db() |
||
| 438 | * and should verify default data is present (but this is also called |
||
| 439 | * on reactivations and just after migrations, so please verify you actually want |
||
| 440 | * to ADD default data, because it may already be present). |
||
| 441 | * However, please call this parent (currently it just fires a hook which other |
||
| 442 | * addons may be depending on) |
||
| 443 | */ |
||
| 444 | public function initialize_default_data() |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * EE Core detected that this addon has been upgraded. We should check if there |
||
| 461 | * are any new migration scripts, and if so put the site into maintenance mode until |
||
| 462 | * they're ran |
||
| 463 | * |
||
| 464 | * @return void |
||
| 465 | */ |
||
| 466 | View Code Duplication | public function upgrade() |
|
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * If Core detects this addon has been downgraded, you may want to invoke some special logic here. |
||
| 482 | */ |
||
| 483 | public function downgrade() |
||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * set_db_update_option_name |
||
| 498 | * Until we do something better, we'll just check for migration scripts upon |
||
| 499 | * plugin activation only. In the future, we'll want to do it on plugin updates too |
||
| 500 | * |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | public function set_db_update_option_name() |
||
| 516 | |||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns the name of the activation indicator option |
||
| 520 | * (an option which is set temporarily to indicate that this addon was just activated) |
||
| 521 | * |
||
| 522 | * @deprecated since version 4.3.0.alpha.016 |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | public function get_db_update_option_name() |
||
| 537 | |||
| 538 | |||
| 539 | /** |
||
| 540 | * When the addon is activated, this should be called to set a wordpress option that |
||
| 541 | * indicates it was activated. This is especially useful for detecting reactivations. |
||
| 542 | * |
||
| 543 | * @return bool |
||
| 544 | */ |
||
| 545 | public function set_activation_indicator_option() |
||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * Gets the name of the wp option which is used to temporarily indicate that this addon was activated |
||
| 554 | * |
||
| 555 | * @return string |
||
| 556 | */ |
||
| 557 | public function get_activation_indicator_option_name() |
||
| 561 | |||
| 562 | |||
| 563 | /** |
||
| 564 | * Used by EE_System to set the request type of this addon. Should not be used by addon developers |
||
| 565 | * |
||
| 566 | * @param int $req_type |
||
| 567 | */ |
||
| 568 | public function set_req_type($req_type) |
||
| 572 | |||
| 573 | |||
| 574 | /** |
||
| 575 | * Returns the request type of this addon (ie, EE_System::req_type_normal, EE_System::req_type_new_activation, |
||
| 576 | * EE_System::req_type_reactivation, EE_System::req_type_upgrade, or EE_System::req_type_downgrade). This is set by |
||
| 577 | * EE_System when it is checking for new install or upgrades of addons |
||
| 578 | */ |
||
| 579 | public function detect_req_type() |
||
| 586 | |||
| 587 | |||
| 588 | /** |
||
| 589 | * Detects the request type for this addon (whether it was just activated, upgrades, a normal request, etc.) |
||
| 590 | * Should only be called once per request |
||
| 591 | * |
||
| 592 | * @return void |
||
| 593 | */ |
||
| 594 | public function detect_activation_or_upgrade() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Updates the version history for this addon |
||
| 641 | * |
||
| 642 | * @param array $version_history |
||
| 643 | * @param string $current_version_to_add |
||
| 644 | * @return boolean success |
||
| 645 | */ |
||
| 646 | public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Gets the name of the wp option that stores the activation history |
||
| 662 | * of this addon |
||
| 663 | * |
||
| 664 | * @return string |
||
| 665 | */ |
||
| 666 | public function get_activation_history_option_name() |
||
| 670 | |||
| 671 | |||
| 672 | /** |
||
| 673 | * Gets the wp option which stores the activation history for this addon |
||
| 674 | * |
||
| 675 | * @return array |
||
| 676 | */ |
||
| 677 | public function get_activation_history() |
||
| 681 | |||
| 682 | |||
| 683 | /** |
||
| 684 | * @param string $config_section |
||
| 685 | */ |
||
| 686 | public function set_config_section($config_section = '') |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Sets the filepath to the main plugin file |
||
| 693 | * |
||
| 694 | * @param string $filepath |
||
| 695 | */ |
||
| 696 | public function set_main_plugin_file($filepath) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * gets the filepath to teh main file |
||
| 703 | * |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | public function get_main_plugin_file() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Gets the filename (no path) of the main file (the main file loaded |
||
| 713 | * by WP) |
||
| 714 | * |
||
| 715 | * @return string |
||
| 716 | */ |
||
| 717 | public function get_main_plugin_file_basename() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Gets the folder name which contains the main plugin file |
||
| 724 | * |
||
| 725 | * @return string |
||
| 726 | */ |
||
| 727 | public function get_main_plugin_file_dirname() |
||
| 731 | |||
| 732 | |||
| 733 | /** |
||
| 734 | * sets hooks used in the admin |
||
| 735 | * |
||
| 736 | * @return void |
||
| 737 | */ |
||
| 738 | public function admin_init() |
||
| 746 | |||
| 747 | |||
| 748 | /** |
||
| 749 | * plugin_actions |
||
| 750 | * Add a settings link to the Plugins page, so people can go straight from the plugin page to the settings page. |
||
| 751 | * |
||
| 752 | * @param $links |
||
| 753 | * @param $file |
||
| 754 | * @return array |
||
| 755 | */ |
||
| 756 | public function plugin_action_links($links, $file) |
||
| 769 | |||
| 770 | |||
| 771 | /** |
||
| 772 | * after_plugin_row |
||
| 773 | * Add additional content to the plugins page plugin row |
||
| 774 | * Inserts another row |
||
| 775 | * |
||
| 776 | * @param $plugin_file |
||
| 777 | * @param $plugin_data |
||
| 778 | * @param $status |
||
| 779 | * @return void |
||
| 780 | */ |
||
| 781 | public function after_plugin_row($plugin_file, $plugin_data, $status) |
||
| 852 | |||
| 853 | |||
| 854 | /** |
||
| 855 | * A safe space for addons to add additional logic like setting hooks that need to be set early in the request. |
||
| 856 | * Child classes that have logic like that to run can override this method declaration. This was not made abstract |
||
| 857 | * for back compat reasons. |
||
| 858 | * |
||
| 859 | * This will fire on the `AHEE__EE_System__load_espresso_addons__complete` hook at priority 999. |
||
| 860 | * |
||
| 861 | * It is recommended, if client code is `de-registering` an add-on, then do it on the |
||
| 862 | * `AHEE__EE_System__load_espresso_addons__complete` hook before priority 999 so as to ensure any code logic in this |
||
| 863 | * callback does not get run/set in that request. |
||
| 864 | * |
||
| 865 | * Also, keep in mind that if a registered add-on happens to be deactivated via |
||
| 866 | * EE_System::_deactivate_incompatible_addons() because its incompatible, any code executed in this method |
||
| 867 | * (including setting hooks etc) will have executed before the plugin was deactivated. If you use |
||
| 868 | * `after_registration` to set any filter and/or action hooks and want to ensure they are removed on this add-on's |
||
| 869 | * deactivation, you can override `EE_Addon::deactivation` and unset your hooks and filters there. Just remember |
||
| 870 | * to call `parent::deactivation`. |
||
| 871 | * |
||
| 872 | * @since 4.9.26 |
||
| 873 | */ |
||
| 874 | public function after_registration() |
||
| 878 | } |
||
| 879 |