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 TGM_Plugin_Activation 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 TGM_Plugin_Activation, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 18 | 	class TGM_Plugin_Activation { | ||
| 19 | /** | ||
| 20 | * TGMPA version number. | ||
| 21 | * | ||
| 22 | * @since 2.5.0 | ||
| 23 | * | ||
| 24 | * @const string Version number. | ||
| 25 | */ | ||
| 26 | const TGMPA_VERSION = '2.6.2'; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Regular expression to test if a URL is a WP plugin repo URL. | ||
| 30 | * | ||
| 31 | * @const string Regex. | ||
| 32 | * | ||
| 33 | * @since 2.5.0 | ||
| 34 | */ | ||
| 35 | const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|'; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * Arbitrary regular expression to test if a string starts with a URL. | ||
| 39 | * | ||
| 40 | * @const string Regex. | ||
| 41 | * | ||
| 42 | * @since 2.5.0 | ||
| 43 | */ | ||
| 44 | const IS_URL_REGEX = '|^http[s]?://|'; | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Holds a copy of itself, so it can be referenced by the class name. | ||
| 48 | * | ||
| 49 | * @since 1.0.0 | ||
| 50 | * | ||
| 51 | * @var TGM_Plugin_Activation | ||
| 52 | */ | ||
| 53 | public static $instance; | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Holds arrays of plugin details. | ||
| 57 | * | ||
| 58 | * @since 1.0.0 | ||
| 59 | * @since 2.5.0 the array has the plugin slug as an associative key. | ||
| 60 | * | ||
| 61 | * @var array | ||
| 62 | */ | ||
| 63 | public $plugins = array(); | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Holds arrays of plugin names to use to sort the plugins array. | ||
| 67 | * | ||
| 68 | * @since 2.5.0 | ||
| 69 | * | ||
| 70 | * @var array | ||
| 71 | */ | ||
| 72 | protected $sort_order = array(); | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Whether any plugins have the 'force_activation' setting set to true. | ||
| 76 | * | ||
| 77 | * @since 2.5.0 | ||
| 78 | * | ||
| 79 | * @var bool | ||
| 80 | */ | ||
| 81 | protected $has_forced_activation = false; | ||
| 82 | |||
| 83 | /** | ||
| 84 | * Whether any plugins have the 'force_deactivation' setting set to true. | ||
| 85 | * | ||
| 86 | * @since 2.5.0 | ||
| 87 | * | ||
| 88 | * @var bool | ||
| 89 | */ | ||
| 90 | protected $has_forced_deactivation = false; | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Name of the unique ID to hash notices. | ||
| 94 | * | ||
| 95 | * @since 2.4.0 | ||
| 96 | * | ||
| 97 | * @var string | ||
| 98 | */ | ||
| 99 | public $id = 'tgmpa'; | ||
| 100 | |||
| 101 | /** | ||
| 102 | * Name of the query-string argument for the admin page. | ||
| 103 | * | ||
| 104 | * @since 1.0.0 | ||
| 105 | * | ||
| 106 | * @var string | ||
| 107 | */ | ||
| 108 | protected $menu = 'tgmpa-install-plugins'; | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Parent menu file slug. | ||
| 112 | * | ||
| 113 | * @since 2.5.0 | ||
| 114 | * | ||
| 115 | * @var string | ||
| 116 | */ | ||
| 117 | public $parent_slug = 'themes.php'; | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Capability needed to view the plugin installation menu item. | ||
| 121 | * | ||
| 122 | * @since 2.5.0 | ||
| 123 | * | ||
| 124 | * @var string | ||
| 125 | */ | ||
| 126 | public $capability = 'edit_theme_options'; | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Default absolute path to folder containing bundled plugin zip files. | ||
| 130 | * | ||
| 131 | * @since 2.0.0 | ||
| 132 | * | ||
| 133 | * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string. | ||
| 134 | */ | ||
| 135 | public $default_path = ''; | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Flag to show admin notices or not. | ||
| 139 | * | ||
| 140 | * @since 2.1.0 | ||
| 141 | * | ||
| 142 | * @var boolean | ||
| 143 | */ | ||
| 144 | public $has_notices = true; | ||
| 145 | |||
| 146 | /** | ||
| 147 | * Flag to determine if the user can dismiss the notice nag. | ||
| 148 | * | ||
| 149 | * @since 2.4.0 | ||
| 150 | * | ||
| 151 | * @var boolean | ||
| 152 | */ | ||
| 153 | public $dismissable = true; | ||
| 154 | |||
| 155 | /** | ||
| 156 | * Message to be output above nag notice if dismissable is false. | ||
| 157 | * | ||
| 158 | * @since 2.4.0 | ||
| 159 | * | ||
| 160 | * @var string | ||
| 161 | */ | ||
| 162 | public $dismiss_msg = ''; | ||
| 163 | |||
| 164 | /** | ||
| 165 | * Flag to set automatic activation of plugins. Off by default. | ||
| 166 | * | ||
| 167 | * @since 2.2.0 | ||
| 168 | * | ||
| 169 | * @var boolean | ||
| 170 | */ | ||
| 171 | public $is_automatic = false; | ||
| 172 | |||
| 173 | /** | ||
| 174 | * Optional message to display before the plugins table. | ||
| 175 | * | ||
| 176 | * @since 2.2.0 | ||
| 177 | * | ||
| 178 | * @var string Message filtered by wp_kses_post(). Default is empty string. | ||
| 179 | */ | ||
| 180 | public $message = ''; | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Holds configurable array of strings. | ||
| 184 | * | ||
| 185 | * Default values are added in the constructor. | ||
| 186 | * | ||
| 187 | * @since 2.0.0 | ||
| 188 | * | ||
| 189 | * @var array | ||
| 190 | */ | ||
| 191 | public $strings = array(); | ||
| 192 | |||
| 193 | /** | ||
| 194 | * Holds the version of WordPress. | ||
| 195 | * | ||
| 196 | * @since 2.4.0 | ||
| 197 | * | ||
| 198 | * @var int | ||
| 199 | */ | ||
| 200 | public $wp_version; | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Holds the hook name for the admin page. | ||
| 204 | * | ||
| 205 | * @since 2.5.0 | ||
| 206 | * | ||
| 207 | * @var string | ||
| 208 | */ | ||
| 209 | public $page_hook; | ||
| 210 | |||
| 211 | /** | ||
| 212 | * Adds a reference of this object to $instance, populates default strings, | ||
| 213 | * does the tgmpa_init action hook, and hooks in the interactions to init. | ||
| 214 | * | ||
| 215 | 		 * {@internal This method should be `protected`, but as too many TGMPA implementations | ||
| 216 | * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues. | ||
| 217 | * Reverted back to public for the time being.}} | ||
| 218 | * | ||
| 219 | * @since 1.0.0 | ||
| 220 | * | ||
| 221 | * @see TGM_Plugin_Activation::init() | ||
| 222 | */ | ||
| 223 | 		public function __construct() { | ||
| 242 | |||
| 243 | /** | ||
| 244 | * Magic method to (not) set protected properties from outside of this class. | ||
| 245 | * | ||
| 246 | 		 * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6  where the `menu` property | ||
| 247 | * is being assigned rather than tested in a conditional, effectively rendering it useless. | ||
| 248 | * This 'hack' prevents this from happening.}} | ||
| 249 | * | ||
| 250 | * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593 | ||
| 251 | * | ||
| 252 | * @since 2.5.2 | ||
| 253 | * | ||
| 254 | * @param string $name Name of an inaccessible property. | ||
| 255 | * @param mixed $value Value to assign to the property. | ||
| 256 | * @return void Silently fail to set the property when this is tried from outside of this class context. | ||
| 257 | * (Inside this class context, the __set() method if not used as there is direct access.) | ||
| 258 | */ | ||
| 259 | 		public function __set( $name, $value ) { | ||
| 262 | |||
| 263 | /** | ||
| 264 | * Magic method to get the value of a protected property outside of this class context. | ||
| 265 | * | ||
| 266 | * @since 2.5.2 | ||
| 267 | * | ||
| 268 | * @param string $name Name of an inaccessible property. | ||
| 269 | * @return mixed The property value. | ||
| 270 | */ | ||
| 271 | 		public function __get( $name ) { | ||
| 274 | |||
| 275 | /** | ||
| 276 | * Initialise the interactions between this class and WordPress. | ||
| 277 | * | ||
| 278 | * Hooks in three new methods for the class: admin_menu, notices and styles. | ||
| 279 | * | ||
| 280 | * @since 2.0.0 | ||
| 281 | * | ||
| 282 | * @see TGM_Plugin_Activation::admin_menu() | ||
| 283 | * @see TGM_Plugin_Activation::notices() | ||
| 284 | * @see TGM_Plugin_Activation::styles() | ||
| 285 | */ | ||
| 286 | 		public function init() { | ||
| 426 | |||
| 427 | /** | ||
| 428 | * Load translations. | ||
| 429 | * | ||
| 430 | * @since 2.6.0 | ||
| 431 | * | ||
| 432 | * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to | ||
| 433 | * get round the different ways of handling the path and deprecated notices being thrown | ||
| 434 | * and such. For plugins, the actual file name will be corrected by a filter.}} | ||
| 435 | * | ||
| 436 | 		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA | ||
| 437 | * generator on the website.}} | ||
| 438 | */ | ||
| 439 | 		public function load_textdomain() { | ||
| 453 | |||
| 454 | /** | ||
| 455 | * Correct the .mo file name for (must-use) plugins. | ||
| 456 | * | ||
| 457 | 		 * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`. | ||
| 458 | * | ||
| 459 | 		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA | ||
| 460 | * generator on the website.}} | ||
| 461 | * | ||
| 462 | * @since 2.6.0 | ||
| 463 | * | ||
| 464 | * @param string $mofile Full path to the target mofile. | ||
| 465 | * @param string $domain The domain for which a language file is being loaded. | ||
| 466 | * @return string $mofile | ||
| 467 | */ | ||
| 468 | 		public function correct_plugin_mofile( $mofile, $domain ) { | ||
| 475 | |||
| 476 | /** | ||
| 477 | * Potentially overload the fall-back translation file for the current language. | ||
| 478 | * | ||
| 479 | * WP, by default since WP 3.7, will load a local translation first and if none | ||
| 480 | * can be found, will try and find a translation in the /wp-content/languages/ directory. | ||
| 481 | * As this library is theme/plugin agnostic, translation files for TGMPA can exist both | ||
| 482 | * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory. | ||
| 483 | * | ||
| 484 | * This method makes sure both directories are checked. | ||
| 485 | * | ||
| 486 | 		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA | ||
| 487 | * generator on the website.}} | ||
| 488 | * | ||
| 489 | * @since 2.6.0 | ||
| 490 | * | ||
| 491 | * @param string $mofile Full path to the target mofile. | ||
| 492 | * @param string $domain The domain for which a language file is being loaded. | ||
| 493 | * @return string $mofile | ||
| 494 | */ | ||
| 495 | 		public function overload_textdomain_mofile( $mofile, $domain ) { | ||
| 510 | |||
| 511 | /** | ||
| 512 | * Hook in plugin action link filters for the WP native plugins page. | ||
| 513 | * | ||
| 514 | * - Prevent activation of plugins which don't meet the minimum version requirements. | ||
| 515 | * - Prevent deactivation of force-activated plugins. | ||
| 516 | * - Add update notice if update available. | ||
| 517 | * | ||
| 518 | * @since 2.5.0 | ||
| 519 | */ | ||
| 520 | 		public function add_plugin_action_link_filters() { | ||
| 535 | |||
| 536 | /** | ||
| 537 | * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the | ||
| 538 | * minimum version requirements. | ||
| 539 | * | ||
| 540 | * @since 2.5.0 | ||
| 541 | * | ||
| 542 | * @param array $actions Action links. | ||
| 543 | * @return array | ||
| 544 | */ | ||
| 545 | 		public function filter_plugin_action_links_activate( $actions ) { | ||
| 550 | |||
| 551 | /** | ||
| 552 | * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate. | ||
| 553 | * | ||
| 554 | * @since 2.5.0 | ||
| 555 | * | ||
| 556 | * @param array $actions Action links. | ||
| 557 | * @return array | ||
| 558 | */ | ||
| 559 | 		public function filter_plugin_action_links_deactivate( $actions ) { | ||
| 564 | |||
| 565 | /** | ||
| 566 | * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the | ||
| 567 | * minimum version requirements. | ||
| 568 | * | ||
| 569 | * @since 2.5.0 | ||
| 570 | * | ||
| 571 | * @param array $actions Action links. | ||
| 572 | * @return array | ||
| 573 | */ | ||
| 574 | 		public function filter_plugin_action_links_update( $actions ) { | ||
| 584 | |||
| 585 | /** | ||
| 586 | * Handles calls to show plugin information via links in the notices. | ||
| 587 | * | ||
| 588 | * We get the links in the admin notices to point to the TGMPA page, rather | ||
| 589 | * than the typical plugin-install.php file, so we can prepare everything | ||
| 590 | * beforehand. | ||
| 591 | * | ||
| 592 | * WP does not make it easy to show the plugin information in the thickbox - | ||
| 593 | * here we have to require a file that includes a function that does the | ||
| 594 | * main work of displaying it, enqueue some styles, set up some globals and | ||
| 595 | * finally call that function before exiting. | ||
| 596 | * | ||
| 597 | * Down right easy once you know how... | ||
| 598 | * | ||
| 599 | * Returns early if not the TGMPA page. | ||
| 600 | * | ||
| 601 | * @since 2.1.0 | ||
| 602 | * | ||
| 603 | * @global string $tab Used as iframe div class names, helps with styling | ||
| 604 | * @global string $body_id Used as the iframe body ID, helps with styling | ||
| 605 | * | ||
| 606 | * @return null Returns early if not the TGMPA page. | ||
| 607 | */ | ||
| 608 | 		public function admin_init() { | ||
| 628 | |||
| 629 | /** | ||
| 630 | * Enqueue thickbox scripts/styles for plugin info. | ||
| 631 | * | ||
| 632 | * Thickbox is not automatically included on all admin pages, so we must | ||
| 633 | * manually enqueue it for those pages. | ||
| 634 | * | ||
| 635 | * Thickbox is only loaded if the user has not dismissed the admin | ||
| 636 | * notice or if there are any plugins left to install and activate. | ||
| 637 | * | ||
| 638 | * @since 2.1.0 | ||
| 639 | */ | ||
| 640 | 		public function thickbox() { | ||
| 645 | |||
| 646 | /** | ||
| 647 | * Adds submenu page if there are plugin actions to take. | ||
| 648 | * | ||
| 649 | * This method adds the submenu page letting users know that a required | ||
| 650 | * plugin needs to be installed. | ||
| 651 | * | ||
| 652 | * This page disappears once the plugin has been installed and activated. | ||
| 653 | * | ||
| 654 | * @since 1.0.0 | ||
| 655 | * | ||
| 656 | * @see TGM_Plugin_Activation::init() | ||
| 657 | * @see TGM_Plugin_Activation::install_plugins_page() | ||
| 658 | * | ||
| 659 | * @return null Return early if user lacks capability to install a plugin. | ||
| 660 | */ | ||
| 661 | 		public function admin_menu() { | ||
| 681 | |||
| 682 | /** | ||
| 683 | * Add the menu item. | ||
| 684 | * | ||
| 685 | 		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA | ||
| 686 | * generator on the website.}} | ||
| 687 | * | ||
| 688 | * @since 2.5.0 | ||
| 689 | * | ||
| 690 | * @param array $args Menu item configuration. | ||
| 691 | */ | ||
| 692 | 		protected function add_admin_menu( array $args ) { | ||
| 703 | |||
| 704 | /** | ||
| 705 | * Echoes plugin installation form. | ||
| 706 | * | ||
| 707 | * This method is the callback for the admin_menu method function. | ||
| 708 | * This displays the admin page and form area where the user can select to install and activate the plugin. | ||
| 709 | * Aborts early if we're processing a plugin installation action. | ||
| 710 | * | ||
| 711 | * @since 1.0.0 | ||
| 712 | * | ||
| 713 | * @return null Aborts early if we're processing a plugin installation action. | ||
| 714 | */ | ||
| 715 | 		public function install_plugins_page() { | ||
| 758 | |||
| 759 | /** | ||
| 760 | * Installs, updates or activates a plugin depending on the action link clicked by the user. | ||
| 761 | * | ||
| 762 | * Checks the $_GET variable to see which actions have been | ||
| 763 | * passed and responds with the appropriate method. | ||
| 764 | * | ||
| 765 | * Uses WP_Filesystem to process and handle the plugin installation | ||
| 766 | * method. | ||
| 767 | * | ||
| 768 | * @since 1.0.0 | ||
| 769 | * | ||
| 770 | * @uses WP_Filesystem | ||
| 771 | * @uses WP_Error | ||
| 772 | * @uses WP_Upgrader | ||
| 773 | * @uses Plugin_Upgrader | ||
| 774 | * @uses Plugin_Installer_Skin | ||
| 775 | * @uses Plugin_Upgrader_Skin | ||
| 776 | * | ||
| 777 | * @return boolean True on success, false on failure. | ||
| 778 | */ | ||
| 779 | 		protected function do_plugin_install() { | ||
| 921 | |||
| 922 | /** | ||
| 923 | * Inject information into the 'update_plugins' site transient as WP checks that before running an update. | ||
| 924 | * | ||
| 925 | * @since 2.5.0 | ||
| 926 | * | ||
| 927 | * @param array $plugins The plugin information for the plugins which are to be updated. | ||
| 928 | */ | ||
| 929 | 		public function inject_update_info( $plugins ) { | ||
| 955 | |||
| 956 | /** | ||
| 957 | * Adjust the plugin directory name if necessary. | ||
| 958 | * | ||
| 959 | * The final destination directory of a plugin is based on the subdirectory name found in the | ||
| 960 | * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this | ||
| 961 | * subdirectory name is not the same as the expected slug and the plugin will not be recognized | ||
| 962 | * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to | ||
| 963 | * the expected plugin slug. | ||
| 964 | * | ||
| 965 | * @since 2.5.0 | ||
| 966 | * | ||
| 967 | * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/. | ||
| 968 | * @param string $remote_source Path to upgrade/zip-file-name.tmp. | ||
| 969 | * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin. | ||
| 970 | * @return string $source | ||
| 971 | */ | ||
| 972 | 		public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { | ||
| 1033 | |||
| 1034 | /** | ||
| 1035 | * Activate a single plugin and send feedback about the result to the screen. | ||
| 1036 | * | ||
| 1037 | * @since 2.5.0 | ||
| 1038 | * | ||
| 1039 | * @param string $file_path Path within wp-plugins/ to main plugin file. | ||
| 1040 | * @param string $slug Plugin slug. | ||
| 1041 | * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false. | ||
| 1042 | * This determines the styling of the output messages. | ||
| 1043 | * @return bool False if an error was encountered, true otherwise. | ||
| 1044 | */ | ||
| 1045 | 		protected function activate_single_plugin( $file_path, $slug, $automatic = false ) { | ||
| 1095 | |||
| 1096 | /** | ||
| 1097 | * Echoes required plugin notice. | ||
| 1098 | * | ||
| 1099 | * Outputs a message telling users that a specific plugin is required for | ||
| 1100 | * their theme. If appropriate, it includes a link to the form page where | ||
| 1101 | * users can install and activate the plugin. | ||
| 1102 | * | ||
| 1103 | * Returns early if we're on the Install page. | ||
| 1104 | * | ||
| 1105 | * @since 1.0.0 | ||
| 1106 | * | ||
| 1107 | * @global object $current_screen | ||
| 1108 | * | ||
| 1109 | * @return null Returns early if we're on the Install page. | ||
| 1110 | */ | ||
| 1111 | 		public function notices() { | ||
| 1237 | |||
| 1238 | /** | ||
| 1239 | * Generate the user action links for the admin notice. | ||
| 1240 | * | ||
| 1241 | * @since 2.6.0 | ||
| 1242 | * | ||
| 1243 | * @param int $install_count Number of plugins to install. | ||
| 1244 | * @param int $update_count Number of plugins to update. | ||
| 1245 | * @param int $activate_count Number of plugins to activate. | ||
| 1246 | * @param int $line_template Template for the HTML tag to output a line. | ||
| 1247 | * @return string Action links. | ||
| 1248 | */ | ||
| 1249 | 		protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) { | ||
| 1296 | |||
| 1297 | /** | ||
| 1298 | * Get admin notice class. | ||
| 1299 | * | ||
| 1300 | * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7 | ||
| 1301 | * (lowest supported version by TGMPA). | ||
| 1302 | * | ||
| 1303 | * @since 2.6.0 | ||
| 1304 | * | ||
| 1305 | * @return string | ||
| 1306 | */ | ||
| 1307 | 		protected function get_admin_notice_class() { | ||
| 1320 | |||
| 1321 | /** | ||
| 1322 | * Display settings errors and remove those which have been displayed to avoid duplicate messages showing | ||
| 1323 | * | ||
| 1324 | * @since 2.5.0 | ||
| 1325 | */ | ||
| 1326 | 		protected function display_settings_errors() { | ||
| 1338 | |||
| 1339 | /** | ||
| 1340 | * Register dismissal of admin notices. | ||
| 1341 | * | ||
| 1342 | * Acts on the dismiss link in the admin nag messages. | ||
| 1343 | * If clicked, the admin notice disappears and will no longer be visible to this user. | ||
| 1344 | * | ||
| 1345 | * @since 2.1.0 | ||
| 1346 | */ | ||
| 1347 | 		public function dismiss() { | ||
| 1352 | |||
| 1353 | /** | ||
| 1354 | * Add individual plugin to our collection of plugins. | ||
| 1355 | * | ||
| 1356 | * If the required keys are not set or the plugin has already | ||
| 1357 | * been registered, the plugin is not added. | ||
| 1358 | * | ||
| 1359 | * @since 2.0.0 | ||
| 1360 | * | ||
| 1361 | * @param array|null $plugin Array of plugin arguments or null if invalid argument. | ||
| 1362 | * @return null Return early if incorrect argument. | ||
| 1363 | */ | ||
| 1364 | 		public function register( $plugin ) { | ||
| 1416 | |||
| 1417 | /** | ||
| 1418 | * Determine what type of source the plugin comes from. | ||
| 1419 | * | ||
| 1420 | * @since 2.5.0 | ||
| 1421 | * | ||
| 1422 | * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path | ||
| 1423 | * (= bundled) or an external URL. | ||
| 1424 | * @return string 'repo', 'external', or 'bundled' | ||
| 1425 | */ | ||
| 1426 | 		protected function get_plugin_source_type( $source ) { | ||
| 1435 | |||
| 1436 | /** | ||
| 1437 | * Sanitizes a string key. | ||
| 1438 | * | ||
| 1439 | * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are* | ||
| 1440 | * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase | ||
| 1441 | * characters in the plugin directory path/slug. Silly them. | ||
| 1442 | * | ||
| 1443 | * @see https://developer.wordpress.org/reference/hooks/sanitize_key/ | ||
| 1444 | * | ||
| 1445 | * @since 2.5.0 | ||
| 1446 | * | ||
| 1447 | * @param string $key String key. | ||
| 1448 | * @return string Sanitized key | ||
| 1449 | */ | ||
| 1450 | 		public function sanitize_key( $key ) { | ||
| 1464 | |||
| 1465 | /** | ||
| 1466 | * Amend default configuration settings. | ||
| 1467 | * | ||
| 1468 | * @since 2.0.0 | ||
| 1469 | * | ||
| 1470 | * @param array $config Array of config options to pass as class properties. | ||
| 1471 | */ | ||
| 1472 | 		public function config( $config ) { | ||
| 1497 | |||
| 1498 | /** | ||
| 1499 | * Amend action link after plugin installation. | ||
| 1500 | * | ||
| 1501 | * @since 2.0.0 | ||
| 1502 | * | ||
| 1503 | * @param array $install_actions Existing array of actions. | ||
| 1504 | * @return false|array Amended array of actions. | ||
| 1505 | */ | ||
| 1506 | 		public function actions( $install_actions ) { | ||
| 1514 | |||
| 1515 | /** | ||
| 1516 | * Flushes the plugins cache on theme switch to prevent stale entries | ||
| 1517 | * from remaining in the plugin table. | ||
| 1518 | * | ||
| 1519 | * @since 2.4.0 | ||
| 1520 | * | ||
| 1521 | * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache. | ||
| 1522 | * Parameter added in v2.5.0. | ||
| 1523 | */ | ||
| 1524 | 		public function flush_plugins_cache( $clear_update_cache = true ) { | ||
| 1527 | |||
| 1528 | /** | ||
| 1529 | * Set file_path key for each installed plugin. | ||
| 1530 | * | ||
| 1531 | * @since 2.1.0 | ||
| 1532 | * | ||
| 1533 | * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin. | ||
| 1534 | * Parameter added in v2.5.0. | ||
| 1535 | */ | ||
| 1536 | 		public function populate_file_path( $plugin_slug = '' ) { | ||
| 1546 | |||
| 1547 | /** | ||
| 1548 | * Helper function to extract the file path of the plugin file from the | ||
| 1549 | * plugin slug, if the plugin is installed. | ||
| 1550 | * | ||
| 1551 | * @since 2.0.0 | ||
| 1552 | * | ||
| 1553 | * @param string $slug Plugin slug (typically folder name) as provided by the developer. | ||
| 1554 | * @return string Either file path for plugin if installed, or just the plugin slug. | ||
| 1555 | */ | ||
| 1556 | 		protected function _get_plugin_basename_from_slug( $slug ) { | ||
| 1567 | |||
| 1568 | /** | ||
| 1569 | * Retrieve plugin data, given the plugin name. | ||
| 1570 | * | ||
| 1571 | * Loops through the registered plugins looking for $name. If it finds it, | ||
| 1572 | * it returns the $data from that plugin. Otherwise, returns false. | ||
| 1573 | * | ||
| 1574 | * @since 2.1.0 | ||
| 1575 | * | ||
| 1576 | * @param string $name Name of the plugin, as it was registered. | ||
| 1577 | * @param string $data Optional. Array key of plugin data to return. Default is slug. | ||
| 1578 | * @return string|boolean Plugin slug if found, false otherwise. | ||
| 1579 | */ | ||
| 1580 | 		public function _get_plugin_data_from_name( $name, $data = 'slug' ) { | ||
| 1589 | |||
| 1590 | /** | ||
| 1591 | * Retrieve the download URL for a package. | ||
| 1592 | * | ||
| 1593 | * @since 2.5.0 | ||
| 1594 | * | ||
| 1595 | * @param string $slug Plugin slug. | ||
| 1596 | * @return string Plugin download URL or path to local file or empty string if undetermined. | ||
| 1597 | */ | ||
| 1598 | 		public function get_download_url( $slug ) { | ||
| 1612 | |||
| 1613 | /** | ||
| 1614 | * Retrieve the download URL for a WP repo package. | ||
| 1615 | * | ||
| 1616 | * @since 2.5.0 | ||
| 1617 | * | ||
| 1618 | * @param string $slug Plugin slug. | ||
| 1619 | * @return string Plugin download URL. | ||
| 1620 | */ | ||
| 1621 | 		protected function get_wp_repo_download_url( $slug ) { | ||
| 1631 | |||
| 1632 | /** | ||
| 1633 | * Try to grab information from WordPress API. | ||
| 1634 | * | ||
| 1635 | * @since 2.5.0 | ||
| 1636 | * | ||
| 1637 | * @param string $slug Plugin slug. | ||
| 1638 | * @return object Plugins_api response object on success, WP_Error on failure. | ||
| 1639 | */ | ||
| 1640 | 		protected function get_plugins_api( $slug ) { | ||
| 1669 | |||
| 1670 | /** | ||
| 1671 | * Retrieve a link to a plugin information page. | ||
| 1672 | * | ||
| 1673 | * @since 2.5.0 | ||
| 1674 | * | ||
| 1675 | * @param string $slug Plugin slug. | ||
| 1676 | * @return string Fully formed html link to a plugin information page if available | ||
| 1677 | * or the plugin name if not. | ||
| 1678 | */ | ||
| 1679 | 		public function get_info_link( $slug ) { | ||
| 1709 | |||
| 1710 | /** | ||
| 1711 | * Determine if we're on the TGMPA Install page. | ||
| 1712 | * | ||
| 1713 | * @since 2.1.0 | ||
| 1714 | * | ||
| 1715 | * @return boolean True when on the TGMPA page, false otherwise. | ||
| 1716 | */ | ||
| 1717 | 		protected function is_tgmpa_page() { | ||
| 1720 | |||
| 1721 | /** | ||
| 1722 | * Determine if we're on a WP Core installation/upgrade page. | ||
| 1723 | * | ||
| 1724 | * @since 2.6.0 | ||
| 1725 | * | ||
| 1726 | * @return boolean True when on a WP Core installation/upgrade page, false otherwise. | ||
| 1727 | */ | ||
| 1728 | 		protected function is_core_update_page() { | ||
| 1749 | |||
| 1750 | /** | ||
| 1751 | * Retrieve the URL to the TGMPA Install page. | ||
| 1752 | * | ||
| 1753 | * I.e. depending on the config settings passed something along the lines of: | ||
| 1754 | * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins | ||
| 1755 | * | ||
| 1756 | * @since 2.5.0 | ||
| 1757 | * | ||
| 1758 | * @return string Properly encoded URL (not escaped). | ||
| 1759 | */ | ||
| 1760 | 		public function get_tgmpa_url() { | ||
| 1778 | |||
| 1779 | /** | ||
| 1780 | * Retrieve the URL to the TGMPA Install page for a specific plugin status (view). | ||
| 1781 | * | ||
| 1782 | * I.e. depending on the config settings passed something along the lines of: | ||
| 1783 | * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install | ||
| 1784 | * | ||
| 1785 | * @since 2.5.0 | ||
| 1786 | * | ||
| 1787 | * @param string $status Plugin status - either 'install', 'update' or 'activate'. | ||
| 1788 | * @return string Properly encoded URL (not escaped). | ||
| 1789 | */ | ||
| 1790 | 		public function get_tgmpa_status_url( $status ) { | ||
| 1798 | |||
| 1799 | /** | ||
| 1800 | * Determine whether there are open actions for plugins registered with TGMPA. | ||
| 1801 | * | ||
| 1802 | * @since 2.5.0 | ||
| 1803 | * | ||
| 1804 | * @return bool True if complete, i.e. no outstanding actions. False otherwise. | ||
| 1805 | */ | ||
| 1806 | 		public function is_tgmpa_complete() { | ||
| 1817 | |||
| 1818 | /** | ||
| 1819 | * Check if a plugin is installed. Does not take must-use plugins into account. | ||
| 1820 | * | ||
| 1821 | * @since 2.5.0 | ||
| 1822 | * | ||
| 1823 | * @param string $slug Plugin slug. | ||
| 1824 | * @return bool True if installed, false otherwise. | ||
| 1825 | */ | ||
| 1826 | 		public function is_plugin_installed( $slug ) { | ||
| 1831 | |||
| 1832 | /** | ||
| 1833 | * Check if a plugin is active. | ||
| 1834 | * | ||
| 1835 | * @since 2.5.0 | ||
| 1836 | * | ||
| 1837 | * @param string $slug Plugin slug. | ||
| 1838 | * @return bool True if active, false otherwise. | ||
| 1839 | */ | ||
| 1840 | 		public function is_plugin_active( $slug ) { | ||
| 1843 | |||
| 1844 | /** | ||
| 1845 | * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required | ||
| 1846 | * available, check whether the current install meets them. | ||
| 1847 | * | ||
| 1848 | * @since 2.5.0 | ||
| 1849 | * | ||
| 1850 | * @param string $slug Plugin slug. | ||
| 1851 | * @return bool True if OK to update, false otherwise. | ||
| 1852 | */ | ||
| 1853 | 		public function can_plugin_update( $slug ) { | ||
| 1868 | |||
| 1869 | /** | ||
| 1870 | * Check to see if the plugin is 'updatetable', i.e. installed, with an update available | ||
| 1871 | * and no WP version requirements blocking it. | ||
| 1872 | * | ||
| 1873 | * @since 2.6.0 | ||
| 1874 | * | ||
| 1875 | * @param string $slug Plugin slug. | ||
| 1876 | * @return bool True if OK to proceed with update, false otherwise. | ||
| 1877 | */ | ||
| 1878 | 		public function is_plugin_updatetable( $slug ) { | ||
| 1885 | |||
| 1886 | /** | ||
| 1887 | * Check if a plugin can be activated, i.e. is not currently active and meets the minimum | ||
| 1888 | * plugin version requirements set in TGMPA (if any). | ||
| 1889 | * | ||
| 1890 | * @since 2.5.0 | ||
| 1891 | * | ||
| 1892 | * @param string $slug Plugin slug. | ||
| 1893 | * @return bool True if OK to activate, false otherwise. | ||
| 1894 | */ | ||
| 1895 | 		public function can_plugin_activate( $slug ) { | ||
| 1898 | |||
| 1899 | /** | ||
| 1900 | * Retrieve the version number of an installed plugin. | ||
| 1901 | * | ||
| 1902 | * @since 2.5.0 | ||
| 1903 | * | ||
| 1904 | * @param string $slug Plugin slug. | ||
| 1905 | * @return string Version number as string or an empty string if the plugin is not installed | ||
| 1906 | * or version unknown (plugins which don't comply with the plugin header standard). | ||
| 1907 | */ | ||
| 1908 | 		public function get_installed_version( $slug ) { | ||
| 1917 | |||
| 1918 | /** | ||
| 1919 | * Check whether a plugin complies with the minimum version requirements. | ||
| 1920 | * | ||
| 1921 | * @since 2.5.0 | ||
| 1922 | * | ||
| 1923 | * @param string $slug Plugin slug. | ||
| 1924 | * @return bool True when a plugin needs to be updated, otherwise false. | ||
| 1925 | */ | ||
| 1926 | 		public function does_plugin_require_update( $slug ) { | ||
| 1932 | |||
| 1933 | /** | ||
| 1934 | * Check whether there is an update available for a plugin. | ||
| 1935 | * | ||
| 1936 | * @since 2.5.0 | ||
| 1937 | * | ||
| 1938 | * @param string $slug Plugin slug. | ||
| 1939 | * @return false|string Version number string of the available update or false if no update available. | ||
| 1940 | */ | ||
| 1941 | 		public function does_plugin_have_update( $slug ) { | ||
| 1959 | |||
| 1960 | /** | ||
| 1961 | * Retrieve potential upgrade notice for a plugin. | ||
| 1962 | * | ||
| 1963 | * @since 2.5.0 | ||
| 1964 | * | ||
| 1965 | * @param string $slug Plugin slug. | ||
| 1966 | * @return string The upgrade notice or an empty string if no message was available or provided. | ||
| 1967 | */ | ||
| 1968 | 		public function get_upgrade_notice( $slug ) { | ||
| 1982 | |||
| 1983 | /** | ||
| 1984 | * Wrapper around the core WP get_plugins function, making sure it's actually available. | ||
| 1985 | * | ||
| 1986 | * @since 2.5.0 | ||
| 1987 | * | ||
| 1988 | * @param string $plugin_folder Optional. Relative path to single plugin folder. | ||
| 1989 | * @return array Array of installed plugins with plugin information. | ||
| 1990 | */ | ||
| 1991 | 		public function get_plugins( $plugin_folder = '' ) { | ||
| 1998 | |||
| 1999 | /** | ||
| 2000 | * Delete dismissable nag option when theme is switched. | ||
| 2001 | * | ||
| 2002 | * This ensures that the user(s) is/are again reminded via nag of required | ||
| 2003 | * and/or recommended plugins if they re-activate the theme. | ||
| 2004 | * | ||
| 2005 | * @since 2.1.1 | ||
| 2006 | */ | ||
| 2007 | 		public function update_dismiss() { | ||
| 2010 | |||
| 2011 | /** | ||
| 2012 | * Forces plugin activation if the parameter 'force_activation' is | ||
| 2013 | * set to true. | ||
| 2014 | * | ||
| 2015 | * This allows theme authors to specify certain plugins that must be | ||
| 2016 | * active at all times while using the current theme. | ||
| 2017 | * | ||
| 2018 | * Please take special care when using this parameter as it has the | ||
| 2019 | * potential to be harmful if not used correctly. Setting this parameter | ||
| 2020 | * to true will not allow the specified plugin to be deactivated unless | ||
| 2021 | * the user switches themes. | ||
| 2022 | * | ||
| 2023 | * @since 2.2.0 | ||
| 2024 | */ | ||
| 2025 | 		public function force_activation() { | ||
| 2038 | |||
| 2039 | /** | ||
| 2040 | * Forces plugin deactivation if the parameter 'force_deactivation' | ||
| 2041 | * is set to true and adds the plugin to the 'recently active' plugins list. | ||
| 2042 | * | ||
| 2043 | * This allows theme authors to specify certain plugins that must be | ||
| 2044 | * deactivated upon switching from the current theme to another. | ||
| 2045 | * | ||
| 2046 | * Please take special care when using this parameter as it has the | ||
| 2047 | * potential to be harmful if not used correctly. | ||
| 2048 | * | ||
| 2049 | * @since 2.2.0 | ||
| 2050 | */ | ||
| 2051 | 		public function force_deactivation() { | ||
| 2069 | |||
| 2070 | /** | ||
| 2071 | * Echo the current TGMPA version number to the page. | ||
| 2072 | * | ||
| 2073 | * @since 2.5.0 | ||
| 2074 | */ | ||
| 2075 | 		public function show_tgmpa_version() { | ||
| 2086 | |||
| 2087 | /** | ||
| 2088 | * Adds CSS to admin head. | ||
| 2089 | * | ||
| 2090 | * @since 2.6.2 | ||
| 2091 | */ | ||
| 2092 | 		public function admin_css() { | ||
| 2104 | |||
| 2105 | /** | ||
| 2106 | * Returns the singleton instance of the class. | ||
| 2107 | * | ||
| 2108 | * @since 2.4.0 | ||
| 2109 | * | ||
| 2110 | * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object. | ||
| 2111 | */ | ||
| 2112 | 		public static function get_instance() { | ||
| 2119 | } | ||
| 2120 | 
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state