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 |
||
50 | class TGM_Plugin_Activation { |
||
51 | /** |
||
52 | * TGMPA version number. |
||
53 | * |
||
54 | * @since 2.5.0 |
||
55 | * |
||
56 | * @const string Version number. |
||
57 | */ |
||
58 | const TGMPA_VERSION = '2.6.0'; |
||
59 | |||
60 | /** |
||
61 | * Regular expression to test if a URL is a WP plugin repo URL. |
||
62 | * |
||
63 | * @const string Regex. |
||
64 | * |
||
65 | * @since 2.5.0 |
||
66 | */ |
||
67 | const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|'; |
||
68 | |||
69 | /** |
||
70 | * Arbitrary regular expression to test if a string starts with a URL. |
||
71 | * |
||
72 | * @const string Regex. |
||
73 | * |
||
74 | * @since 2.5.0 |
||
75 | */ |
||
76 | const IS_URL_REGEX = '|^http[s]?://|'; |
||
77 | |||
78 | /** |
||
79 | * Holds a copy of itself, so it can be referenced by the class name. |
||
80 | * |
||
81 | * @since 1.0.0 |
||
82 | * |
||
83 | * @var TGM_Plugin_Activation |
||
84 | */ |
||
85 | public static $instance; |
||
86 | |||
87 | /** |
||
88 | * Holds arrays of plugin details. |
||
89 | * |
||
90 | * @since 1.0.0 |
||
91 | * @since 2.5.0 the array has the plugin slug as an associative key. |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | public $plugins = array(); |
||
96 | |||
97 | /** |
||
98 | * Holds arrays of plugin names to use to sort the plugins array. |
||
99 | * |
||
100 | * @since 2.5.0 |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $sort_order = array(); |
||
105 | |||
106 | /** |
||
107 | * Whether any plugins have the 'force_activation' setting set to true. |
||
108 | * |
||
109 | * @since 2.5.0 |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | protected $has_forced_activation = false; |
||
114 | |||
115 | /** |
||
116 | * Whether any plugins have the 'force_deactivation' setting set to true. |
||
117 | * |
||
118 | * @since 2.5.0 |
||
119 | * |
||
120 | * @var bool |
||
121 | */ |
||
122 | protected $has_forced_deactivation = false; |
||
123 | |||
124 | /** |
||
125 | * Name of the unique ID to hash notices. |
||
126 | * |
||
127 | * @since 2.4.0 |
||
128 | * |
||
129 | * @var string |
||
130 | */ |
||
131 | public $id = 'tgmpa'; |
||
132 | |||
133 | /** |
||
134 | * Name of the query-string argument for the admin page. |
||
135 | * |
||
136 | * @since 1.0.0 |
||
137 | * |
||
138 | * @var string |
||
139 | */ |
||
140 | protected $menu = 'tgmpa-install-plugins'; |
||
141 | |||
142 | /** |
||
143 | * Parent menu file slug. |
||
144 | * |
||
145 | * @since 2.5.0 |
||
146 | * |
||
147 | * @var string |
||
148 | */ |
||
149 | public $parent_slug = 'themes.php'; |
||
150 | |||
151 | /** |
||
152 | * Capability needed to view the plugin installation menu item. |
||
153 | * |
||
154 | * @since 2.5.0 |
||
155 | * |
||
156 | * @var string |
||
157 | */ |
||
158 | public $capability = 'edit_theme_options'; |
||
159 | |||
160 | /** |
||
161 | * Default absolute path to folder containing bundled plugin zip files. |
||
162 | * |
||
163 | * @since 2.0.0 |
||
164 | * |
||
165 | * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string. |
||
166 | */ |
||
167 | public $default_path = ''; |
||
168 | |||
169 | /** |
||
170 | * Flag to show admin notices or not. |
||
171 | * |
||
172 | * @since 2.1.0 |
||
173 | * |
||
174 | * @var boolean |
||
175 | */ |
||
176 | public $has_notices = true; |
||
177 | |||
178 | /** |
||
179 | * Flag to determine if the user can dismiss the notice nag. |
||
180 | * |
||
181 | * @since 2.4.0 |
||
182 | * |
||
183 | * @var boolean |
||
184 | */ |
||
185 | public $dismissable = true; |
||
186 | |||
187 | /** |
||
188 | * Message to be output above nag notice if dismissable is false. |
||
189 | * |
||
190 | * @since 2.4.0 |
||
191 | * |
||
192 | * @var string |
||
193 | */ |
||
194 | public $dismiss_msg = ''; |
||
195 | |||
196 | /** |
||
197 | * Flag to set automatic activation of plugins. Off by default. |
||
198 | * |
||
199 | * @since 2.2.0 |
||
200 | * |
||
201 | * @var boolean |
||
202 | */ |
||
203 | public $is_automatic = false; |
||
204 | |||
205 | /** |
||
206 | * Optional message to display before the plugins table. |
||
207 | * |
||
208 | * @since 2.2.0 |
||
209 | * |
||
210 | * @var string Message filtered by wp_kses_post(). Default is empty string. |
||
211 | */ |
||
212 | public $message = ''; |
||
213 | |||
214 | /** |
||
215 | * Holds configurable array of strings. |
||
216 | * |
||
217 | * Default values are added in the constructor. |
||
218 | * |
||
219 | * @since 2.0.0 |
||
220 | * |
||
221 | * @var array |
||
222 | */ |
||
223 | public $strings = array(); |
||
224 | |||
225 | /** |
||
226 | * Holds the version of WordPress. |
||
227 | * |
||
228 | * @since 2.4.0 |
||
229 | * |
||
230 | * @var int |
||
231 | */ |
||
232 | public $wp_version; |
||
233 | |||
234 | /** |
||
235 | * Holds the hook name for the admin page. |
||
236 | * |
||
237 | * @since 2.5.0 |
||
238 | * |
||
239 | * @var string |
||
240 | */ |
||
241 | public $page_hook; |
||
242 | |||
243 | /** |
||
244 | * Adds a reference of this object to $instance, populates default strings, |
||
245 | * does the tgmpa_init action hook, and hooks in the interactions to init. |
||
246 | * |
||
247 | * {@internal This method should be `protected`, but as too many TGMPA implementations |
||
248 | * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues. |
||
249 | * Reverted back to public for the time being.}} |
||
250 | * |
||
251 | * @since 1.0.0 |
||
252 | * |
||
253 | * @see TGM_Plugin_Activation::init() |
||
254 | */ |
||
255 | public function __construct() { |
||
274 | |||
275 | /** |
||
276 | * Magic method to (not) set protected properties from outside of this class. |
||
277 | * |
||
278 | * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6 where the `menu` property |
||
279 | * is being assigned rather than tested in a conditional, effectively rendering it useless. |
||
280 | * This 'hack' prevents this from happening.}} |
||
281 | * |
||
282 | * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593 |
||
283 | * |
||
284 | * @since 2.5.2 |
||
285 | * |
||
286 | * @param string $name Name of an inaccessible property. |
||
287 | * @param mixed $value Value to assign to the property. |
||
288 | * @return void Silently fail to set the property when this is tried from outside of this class context. |
||
289 | * (Inside this class context, the __set() method if not used as there is direct access.) |
||
290 | */ |
||
291 | public function __set( $name, $value ) { |
||
294 | |||
295 | /** |
||
296 | * Magic method to get the value of a protected property outside of this class context. |
||
297 | * |
||
298 | * @since 2.5.2 |
||
299 | * |
||
300 | * @param string $name Name of an inaccessible property. |
||
301 | * @return mixed The property value. |
||
302 | */ |
||
303 | public function __get( $name ) { |
||
306 | |||
307 | /** |
||
308 | * Initialise the interactions between this class and WordPress. |
||
309 | * |
||
310 | * Hooks in three new methods for the class: admin_menu, notices and styles. |
||
311 | * |
||
312 | * @since 2.0.0 |
||
313 | * |
||
314 | * @see TGM_Plugin_Activation::admin_menu() |
||
315 | * @see TGM_Plugin_Activation::notices() |
||
316 | * @see TGM_Plugin_Activation::styles() |
||
317 | */ |
||
318 | public function init() { |
||
319 | /** |
||
320 | * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter |
||
321 | * you can overrule that behaviour. |
||
322 | * |
||
323 | * @since 2.5.0 |
||
324 | * |
||
325 | * @param bool $load Whether or not TGMPA should load. |
||
326 | * Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`. |
||
327 | */ |
||
328 | if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) { |
||
329 | return; |
||
330 | } |
||
331 | |||
332 | // Load class strings. |
||
333 | $this->strings = array( |
||
334 | 'page_title' => __( 'Install Required Plugins', 'tgmpa' ), |
||
335 | 'menu_title' => __( 'Install Plugins', 'tgmpa' ), |
||
336 | /* translators: %s: plugin name. */ |
||
337 | 'installing' => __( 'Installing Plugin: %s', 'tgmpa' ), |
||
338 | /* translators: %s: plugin name. */ |
||
339 | 'updating' => __( 'Updating Plugin: %s', 'tgmpa' ), |
||
340 | 'oops' => __( 'Something went wrong with the plugin API.', 'tgmpa' ), |
||
341 | 'notice_can_install_required' => _n_noop( |
||
342 | /* translators: 1: plugin name(s). */ |
||
343 | 'This theme requires the following plugin: %1$s.', |
||
344 | 'This theme requires the following plugins: %1$s.', |
||
345 | 'tgmpa' |
||
346 | ), |
||
347 | 'notice_can_install_recommended' => _n_noop( |
||
348 | /* translators: 1: plugin name(s). */ |
||
349 | 'This theme recommends the following plugin: %1$s.', |
||
350 | 'This theme recommends the following plugins: %1$s.', |
||
351 | 'tgmpa' |
||
352 | ), |
||
353 | 'notice_ask_to_update' => _n_noop( |
||
354 | /* translators: 1: plugin name(s). */ |
||
355 | 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', |
||
356 | 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.', |
||
357 | 'tgmpa' |
||
358 | ), |
||
359 | 'notice_ask_to_update_maybe' => _n_noop( |
||
360 | /* translators: 1: plugin name(s). */ |
||
361 | 'There is an update available for: %1$s.', |
||
362 | 'There are updates available for the following plugins: %1$s.', |
||
363 | 'tgmpa' |
||
364 | ), |
||
365 | 'notice_can_activate_required' => _n_noop( |
||
366 | /* translators: 1: plugin name(s). */ |
||
367 | 'The following required plugin is currently inactive: %1$s.', |
||
368 | 'The following required plugins are currently inactive: %1$s.', |
||
369 | 'tgmpa' |
||
370 | ), |
||
371 | 'notice_can_activate_recommended' => _n_noop( |
||
372 | /* translators: 1: plugin name(s). */ |
||
373 | 'The following recommended plugin is currently inactive: %1$s.', |
||
374 | 'The following recommended plugins are currently inactive: %1$s.', |
||
375 | 'tgmpa' |
||
376 | ), |
||
377 | 'install_link' => _n_noop( |
||
378 | 'Begin installing plugin', |
||
379 | 'Begin installing plugins', |
||
380 | 'tgmpa' |
||
381 | ), |
||
382 | 'update_link' => _n_noop( |
||
383 | 'Begin updating plugin', |
||
384 | 'Begin updating plugins', |
||
385 | 'tgmpa' |
||
386 | ), |
||
387 | 'activate_link' => _n_noop( |
||
388 | 'Begin activating plugin', |
||
389 | 'Begin activating plugins', |
||
390 | 'tgmpa' |
||
391 | ), |
||
392 | 'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ), |
||
393 | 'dashboard' => __( 'Return to the Dashboard', 'tgmpa' ), |
||
394 | 'plugin_activated' => __( 'Plugin activated successfully.', 'tgmpa' ), |
||
395 | 'activated_successfully' => __( 'The following plugin was activated successfully:', 'tgmpa' ), |
||
396 | /* translators: 1: plugin name. */ |
||
397 | 'plugin_already_active' => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ), |
||
398 | /* translators: 1: plugin name. */ |
||
399 | 'plugin_needs_higher_version' => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ), |
||
400 | /* translators: 1: dashboard link. */ |
||
401 | 'complete' => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ), |
||
402 | 'dismiss' => __( 'Dismiss this notice', 'tgmpa' ), |
||
403 | 'notice_cannot_install_activate' => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ), |
||
404 | 'contact_admin' => __( 'Please contact the administrator of this site for help.', 'tgmpa' ), |
||
405 | ); |
||
406 | |||
407 | do_action( 'tgmpa_register' ); |
||
408 | |||
409 | /* After this point, the plugins should be registered and the configuration set. */ |
||
410 | |||
411 | // Proceed only if we have plugins to handle. |
||
412 | if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { |
||
413 | return; |
||
414 | } |
||
415 | |||
416 | // Set up the menu and notices if we still have outstanding actions. |
||
417 | if ( true !== $this->is_tgmpa_complete() ) { |
||
418 | // Sort the plugins. |
||
419 | array_multisort( $this->sort_order, SORT_ASC, $this->plugins ); |
||
420 | |||
421 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
||
422 | add_action( 'admin_head', array( $this, 'dismiss' ) ); |
||
423 | |||
424 | // Prevent the normal links from showing underneath a single install/update page. |
||
425 | add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) ); |
||
426 | add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) ); |
||
427 | |||
428 | if ( $this->has_notices ) { |
||
429 | add_action( 'admin_notices', array( $this, 'notices' ) ); |
||
430 | add_action( 'admin_init', array( $this, 'admin_init' ), 1 ); |
||
431 | add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) ); |
||
432 | } |
||
433 | } |
||
434 | |||
435 | // If needed, filter plugin action links. |
||
436 | add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 ); |
||
437 | |||
438 | // Make sure things get reset on switch theme. |
||
439 | add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) ); |
||
440 | |||
441 | if ( $this->has_notices ) { |
||
442 | add_action( 'switch_theme', array( $this, 'update_dismiss' ) ); |
||
443 | } |
||
444 | |||
445 | // Setup the force activation hook. |
||
446 | if ( true === $this->has_forced_activation ) { |
||
447 | add_action( 'admin_init', array( $this, 'force_activation' ) ); |
||
448 | } |
||
449 | |||
450 | // Setup the force deactivation hook. |
||
451 | if ( true === $this->has_forced_deactivation ) { |
||
452 | add_action( 'switch_theme', array( $this, 'force_deactivation' ) ); |
||
453 | } |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Load translations. |
||
458 | * |
||
459 | * @since 2.6.0 |
||
460 | * |
||
461 | * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to |
||
462 | * get round the different ways of handling the path and deprecated notices being thrown |
||
463 | * and such. For plugins, the actual file name will be corrected by a filter.}} |
||
464 | * |
||
465 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
466 | * generator on the website.}} |
||
467 | */ |
||
468 | public function load_textdomain() { |
||
482 | |||
483 | /** |
||
484 | * Correct the .mo file name for (must-use) plugins. |
||
485 | * |
||
486 | * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`. |
||
487 | * |
||
488 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
489 | * generator on the website.}} |
||
490 | * |
||
491 | * @since 2.6.0 |
||
492 | * |
||
493 | * @param string $mofile Full path to the target mofile. |
||
494 | * @param string $domain The domain for which a language file is being loaded. |
||
495 | * @return string $mofile |
||
496 | */ |
||
497 | public function correct_plugin_mofile( $mofile, $domain ) { |
||
504 | |||
505 | /** |
||
506 | * Potentially overload the fall-back translation file for the current language. |
||
507 | * |
||
508 | * WP, by default since WP 3.7, will load a local translation first and if none |
||
509 | * can be found, will try and find a translation in the /wp-content/languages/ directory. |
||
510 | * As this library is theme/plugin agnostic, translation files for TGMPA can exist both |
||
511 | * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory. |
||
512 | * |
||
513 | * This method makes sure both directories are checked. |
||
514 | * |
||
515 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
516 | * generator on the website.}} |
||
517 | * |
||
518 | * @since 2.6.0 |
||
519 | * |
||
520 | * @param string $mofile Full path to the target mofile. |
||
521 | * @param string $domain The domain for which a language file is being loaded. |
||
522 | * @return string $mofile |
||
523 | */ |
||
524 | public function overload_textdomain_mofile( $mofile, $domain ) { |
||
539 | |||
540 | /** |
||
541 | * Hook in plugin action link filters for the WP native plugins page. |
||
542 | * |
||
543 | * - Prevent activation of plugins which don't meet the minimum version requirements. |
||
544 | * - Prevent deactivation of force-activated plugins. |
||
545 | * - Add update notice if update available. |
||
546 | * |
||
547 | * @since 2.5.0 |
||
548 | */ |
||
549 | public function add_plugin_action_link_filters() { |
||
564 | |||
565 | /** |
||
566 | * Remove the 'Activate' 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_activate( $actions ) { |
||
579 | |||
580 | /** |
||
581 | * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate. |
||
582 | * |
||
583 | * @since 2.5.0 |
||
584 | * |
||
585 | * @param array $actions Action links. |
||
586 | * @return array |
||
587 | */ |
||
588 | public function filter_plugin_action_links_deactivate( $actions ) { |
||
593 | |||
594 | /** |
||
595 | * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the |
||
596 | * minimum version requirements. |
||
597 | * |
||
598 | * @since 2.5.0 |
||
599 | * |
||
600 | * @param array $actions Action links. |
||
601 | * @return array |
||
602 | */ |
||
603 | public function filter_plugin_action_links_update( $actions ) { |
||
613 | |||
614 | /** |
||
615 | * Handles calls to show plugin information via links in the notices. |
||
616 | * |
||
617 | * We get the links in the admin notices to point to the TGMPA page, rather |
||
618 | * than the typical plugin-install.php file, so we can prepare everything |
||
619 | * beforehand. |
||
620 | * |
||
621 | * WP does not make it easy to show the plugin information in the thickbox - |
||
622 | * here we have to require a file that includes a function that does the |
||
623 | * main work of displaying it, enqueue some styles, set up some globals and |
||
624 | * finally call that function before exiting. |
||
625 | * |
||
626 | * Down right easy once you know how... |
||
627 | * |
||
628 | * Returns early if not the TGMPA page. |
||
629 | * |
||
630 | * @since 2.1.0 |
||
631 | * |
||
632 | * @global string $tab Used as iframe div class names, helps with styling |
||
633 | * @global string $body_id Used as the iframe body ID, helps with styling |
||
634 | * |
||
635 | * @return null Returns early if not the TGMPA page. |
||
636 | */ |
||
637 | public function admin_init() { |
||
659 | |||
660 | /** |
||
661 | * Enqueue thickbox scripts/styles for plugin info. |
||
662 | * |
||
663 | * Thickbox is not automatically included on all admin pages, so we must |
||
664 | * manually enqueue it for those pages. |
||
665 | * |
||
666 | * Thickbox is only loaded if the user has not dismissed the admin |
||
667 | * notice or if there are any plugins left to install and activate. |
||
668 | * |
||
669 | * @since 2.1.0 |
||
670 | */ |
||
671 | public function thickbox() { |
||
676 | |||
677 | /** |
||
678 | * Adds submenu page if there are plugin actions to take. |
||
679 | * |
||
680 | * This method adds the submenu page letting users know that a required |
||
681 | * plugin needs to be installed. |
||
682 | * |
||
683 | * This page disappears once the plugin has been installed and activated. |
||
684 | * |
||
685 | * @since 1.0.0 |
||
686 | * |
||
687 | * @see TGM_Plugin_Activation::init() |
||
688 | * @see TGM_Plugin_Activation::install_plugins_page() |
||
689 | * |
||
690 | * @return null Return early if user lacks capability to install a plugin. |
||
691 | */ |
||
692 | public function admin_menu() { |
||
712 | |||
713 | /** |
||
714 | * Add the menu item. |
||
715 | * |
||
716 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
717 | * generator on the website.}} |
||
718 | * |
||
719 | * @since 2.5.0 |
||
720 | * |
||
721 | * @param array $args Menu item configuration. |
||
722 | */ |
||
723 | protected function add_admin_menu( array $args ) { |
||
734 | |||
735 | /** |
||
736 | * Echoes plugin installation form. |
||
737 | * |
||
738 | * This method is the callback for the admin_menu method function. |
||
739 | * This displays the admin page and form area where the user can select to install and activate the plugin. |
||
740 | * Aborts early if we're processing a plugin installation action. |
||
741 | * |
||
742 | * @since 1.0.0 |
||
743 | * |
||
744 | * @return null Aborts early if we're processing a plugin installation action. |
||
745 | */ |
||
746 | public function install_plugins_page() { |
||
778 | |||
779 | /** |
||
780 | * Installs, updates or activates a plugin depending on the action link clicked by the user. |
||
781 | * |
||
782 | * Checks the $_GET variable to see which actions have been |
||
783 | * passed and responds with the appropriate method. |
||
784 | * |
||
785 | * Uses WP_Filesystem to process and handle the plugin installation |
||
786 | * method. |
||
787 | * |
||
788 | * @since 1.0.0 |
||
789 | * |
||
790 | * @uses WP_Filesystem |
||
791 | * @uses WP_Error |
||
792 | * @uses WP_Upgrader |
||
793 | * @uses Plugin_Upgrader |
||
794 | * @uses Plugin_Installer_Skin |
||
795 | * @uses Plugin_Upgrader_Skin |
||
796 | * |
||
797 | * @return boolean True on success, false on failure. |
||
798 | */ |
||
799 | protected function do_plugin_install() { |
||
800 | if ( empty( $_GET['plugin'] ) ) { |
||
801 | return false; |
||
802 | } |
||
803 | |||
804 | // All plugin information will be stored in an array for processing. |
||
805 | $slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) ); |
||
806 | |||
807 | if ( ! isset( $this->plugins[ $slug ] ) ) { |
||
808 | return false; |
||
809 | } |
||
810 | |||
811 | // Was an install or upgrade action link clicked? |
||
812 | if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) { |
||
813 | |||
814 | $install_type = 'install'; |
||
815 | if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) { |
||
816 | $install_type = 'update'; |
||
817 | } |
||
818 | |||
819 | check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' ); |
||
820 | |||
821 | // Pass necessary information via URL if WP_Filesystem is needed. |
||
822 | $url = wp_nonce_url( |
||
823 | add_query_arg( |
||
824 | array( |
||
825 | 'plugin' => urlencode( $slug ), |
||
826 | 'tgmpa-' . $install_type => $install_type . '-plugin', |
||
827 | ), |
||
828 | $this->get_tgmpa_url() |
||
829 | ), |
||
830 | 'tgmpa-' . $install_type, |
||
831 | 'tgmpa-nonce' |
||
832 | ); |
||
833 | |||
834 | $method = ''; // Leave blank so WP_Filesystem can populate it as necessary. |
||
835 | |||
836 | if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) { |
||
837 | return true; |
||
838 | } |
||
839 | |||
840 | if ( ! WP_Filesystem( $creds ) ) { |
||
841 | request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem. |
||
842 | return true; |
||
843 | } |
||
844 | |||
845 | /* If we arrive here, we have the filesystem. */ |
||
846 | |||
847 | // Prep variables for Plugin_Installer_Skin class. |
||
848 | $extra = array(); |
||
849 | $extra['slug'] = $slug; // Needed for potentially renaming of directory name. |
||
850 | $source = $this->get_download_url( $slug ); |
||
851 | $api = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null; |
||
852 | $api = ( false !== $api ) ? $api : null; |
||
853 | |||
854 | $url = add_query_arg( |
||
855 | array( |
||
856 | 'action' => $install_type . '-plugin', |
||
857 | 'plugin' => urlencode( $slug ), |
||
858 | ), |
||
859 | 'update.php' |
||
860 | ); |
||
861 | |||
862 | if ( ! class_exists( 'Plugin_Upgrader', false ) ) { |
||
863 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
864 | } |
||
865 | |||
866 | $title = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing']; |
||
867 | $skin_args = array( |
||
868 | 'type' => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload', |
||
869 | 'title' => sprintf( $title, $this->plugins[ $slug ]['name'] ), |
||
870 | 'url' => esc_url_raw( $url ), |
||
871 | 'nonce' => $install_type . '-plugin_' . $slug, |
||
872 | 'plugin' => '', |
||
873 | 'api' => $api, |
||
874 | 'extra' => $extra, |
||
875 | ); |
||
876 | unset( $title ); |
||
877 | |||
878 | if ( 'update' === $install_type ) { |
||
879 | $skin_args['plugin'] = $this->plugins[ $slug ]['file_path']; |
||
880 | $skin = new Plugin_Upgrader_Skin( $skin_args ); |
||
881 | } else { |
||
882 | $skin = new Plugin_Installer_Skin( $skin_args ); |
||
883 | } |
||
884 | |||
885 | // Create a new instance of Plugin_Upgrader. |
||
886 | $upgrader = new Plugin_Upgrader( $skin ); |
||
887 | |||
888 | // Perform the action and install the plugin from the $source urldecode(). |
||
889 | add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 ); |
||
890 | |||
891 | if ( 'update' === $install_type ) { |
||
892 | // Inject our info into the update transient. |
||
893 | $to_inject = array( $slug => $this->plugins[ $slug ] ); |
||
894 | $to_inject[ $slug ]['source'] = $source; |
||
895 | $this->inject_update_info( $to_inject ); |
||
896 | |||
897 | $upgrader->upgrade( $this->plugins[ $slug ]['file_path'] ); |
||
898 | } else { |
||
899 | $upgrader->install( $source ); |
||
900 | } |
||
901 | |||
902 | remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 ); |
||
903 | |||
904 | // Make sure we have the correct file path now the plugin is installed/updated. |
||
905 | $this->populate_file_path( $slug ); |
||
906 | |||
907 | // Only activate plugins if the config option is set to true and the plugin isn't |
||
908 | // already active (upgrade). |
||
909 | if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) { |
||
910 | $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method. |
||
911 | if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { |
||
912 | return true; // Finish execution of the function early as we encountered an error. |
||
913 | } |
||
914 | } |
||
915 | |||
916 | $this->show_tgmpa_version(); |
||
917 | |||
918 | // Display message based on if all plugins are now active or not. |
||
919 | if ( $this->is_tgmpa_complete() ) { |
||
920 | echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ), '</p>'; |
||
921 | echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; |
||
922 | } else { |
||
923 | echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; |
||
924 | } |
||
925 | |||
926 | return true; |
||
927 | } elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) { |
||
928 | // Activate action link was clicked. |
||
929 | check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' ); |
||
930 | |||
931 | if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) { |
||
932 | return true; // Finish execution of the function early as we encountered an error. |
||
933 | } |
||
934 | } |
||
935 | |||
936 | return false; |
||
937 | } |
||
938 | |||
939 | /** |
||
940 | * Inject information into the 'update_plugins' site transient as WP checks that before running an update. |
||
941 | * |
||
942 | * @since 2.5.0 |
||
943 | * |
||
944 | * @param array $plugins The plugin information for the plugins which are to be updated. |
||
945 | */ |
||
946 | public function inject_update_info( $plugins ) { |
||
947 | $repo_updates = get_site_transient( 'update_plugins' ); |
||
948 | |||
949 | if ( ! is_object( $repo_updates ) ) { |
||
950 | $repo_updates = new stdClass; |
||
951 | } |
||
952 | |||
953 | foreach ( $plugins as $slug => $plugin ) { |
||
954 | $file_path = $plugin['file_path']; |
||
955 | |||
956 | if ( empty( $repo_updates->response[ $file_path ] ) ) { |
||
957 | $repo_updates->response[ $file_path ] = new stdClass; |
||
958 | } |
||
959 | |||
960 | // We only really need to set package, but let's do all we can in case WP changes something. |
||
961 | $repo_updates->response[ $file_path ]->slug = $slug; |
||
962 | $repo_updates->response[ $file_path ]->plugin = $file_path; |
||
963 | $repo_updates->response[ $file_path ]->new_version = $plugin['version']; |
||
964 | $repo_updates->response[ $file_path ]->package = $plugin['source']; |
||
965 | if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) { |
||
966 | $repo_updates->response[ $file_path ]->url = $plugin['external_url']; |
||
967 | } |
||
968 | } |
||
969 | |||
970 | set_site_transient( 'update_plugins', $repo_updates ); |
||
971 | } |
||
972 | |||
973 | /** |
||
974 | * Adjust the plugin directory name if necessary. |
||
975 | * |
||
976 | * The final destination directory of a plugin is based on the subdirectory name found in the |
||
977 | * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this |
||
978 | * subdirectory name is not the same as the expected slug and the plugin will not be recognized |
||
979 | * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to |
||
980 | * the expected plugin slug. |
||
981 | * |
||
982 | * @since 2.5.0 |
||
983 | * |
||
984 | * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/. |
||
985 | * @param string $remote_source Path to upgrade/zip-file-name.tmp. |
||
986 | * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin. |
||
987 | * @return string $source |
||
988 | */ |
||
989 | public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { |
||
990 | if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) { |
||
991 | return $source; |
||
992 | } |
||
993 | |||
994 | // Check for single file plugins. |
||
995 | $source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) ); |
||
996 | if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) { |
||
997 | return $source; |
||
998 | } |
||
999 | |||
1000 | // Multi-file plugin, let's see if the directory is correctly named. |
||
1001 | $desired_slug = ''; |
||
1002 | |||
1003 | // Figure out what the slug is supposed to be. |
||
1004 | if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) { |
||
1005 | $desired_slug = $upgrader->skin->options['extra']['slug']; |
||
1006 | } else { |
||
1007 | // Bulk installer contains less info, so fall back on the info registered here. |
||
1008 | foreach ( $this->plugins as $slug => $plugin ) { |
||
1009 | if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) { |
||
1010 | $desired_slug = $slug; |
||
1011 | break; |
||
1012 | } |
||
1013 | } |
||
1014 | unset( $slug, $plugin ); |
||
1015 | } |
||
1016 | |||
1017 | if ( ! empty( $desired_slug ) ) { |
||
1018 | $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) ); |
||
1019 | |||
1020 | if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) { |
||
1021 | $from_path = untrailingslashit( $source ); |
||
1022 | $to_path = trailingslashit( $remote_source ) . $desired_slug; |
||
1023 | |||
1024 | if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) { |
||
1025 | return trailingslashit( $to_path ); |
||
1026 | View Code Duplication | } else { |
|
1027 | return new WP_Error( 'rename_failed', esc_html__( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) ); |
||
1028 | } |
||
1029 | View Code Duplication | } elseif ( empty( $subdir_name ) ) { |
|
1030 | return new WP_Error( 'packaged_wrong', esc_html__( 'The remote plugin package consists of more than one file, but the files are not packaged in a folder.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) ); |
||
1031 | } |
||
1032 | } |
||
1033 | |||
1034 | return $source; |
||
1035 | } |
||
1036 | |||
1037 | /** |
||
1038 | * Activate a single plugin and send feedback about the result to the screen. |
||
1039 | * |
||
1040 | * @since 2.5.0 |
||
1041 | * |
||
1042 | * @param string $file_path Path within wp-plugins/ to main plugin file. |
||
1043 | * @param string $slug Plugin slug. |
||
1044 | * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false. |
||
1045 | * This determines the styling of the output messages. |
||
1046 | * @return bool False if an error was encountered, true otherwise. |
||
1047 | */ |
||
1048 | protected function activate_single_plugin( $file_path, $slug, $automatic = false ) { |
||
1098 | |||
1099 | /** |
||
1100 | * Echoes required plugin notice. |
||
1101 | * |
||
1102 | * Outputs a message telling users that a specific plugin is required for |
||
1103 | * their theme. If appropriate, it includes a link to the form page where |
||
1104 | * users can install and activate the plugin. |
||
1105 | * |
||
1106 | * Returns early if we're on the Install page. |
||
1107 | * |
||
1108 | * @since 1.0.0 |
||
1109 | * |
||
1110 | * @global object $current_screen |
||
1111 | * |
||
1112 | * @return null Returns early if we're on the Install page. |
||
1113 | */ |
||
1114 | public function notices() { |
||
1115 | // Remove nag on the install page / Return early if the nag message has been dismissed or user < author. |
||
1116 | if ( ( $this->is_tgmpa_page() || $this->is_core_update_page() ) || get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) || ! current_user_can( apply_filters( 'tgmpa_show_admin_notice_capability', 'publish_posts' ) ) ) { |
||
1117 | return; |
||
1118 | } |
||
1119 | |||
1120 | // Store for the plugin slugs by message type. |
||
1121 | $message = array(); |
||
1122 | |||
1123 | // Initialize counters used to determine plurality of action link texts. |
||
1124 | $install_link_count = 0; |
||
1125 | $update_link_count = 0; |
||
1126 | $activate_link_count = 0; |
||
1127 | $total_required_action_count = 0; |
||
1128 | |||
1129 | foreach ( $this->plugins as $slug => $plugin ) { |
||
1130 | if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) { |
||
1131 | continue; |
||
1132 | } |
||
1133 | |||
1134 | if ( ! $this->is_plugin_installed( $slug ) ) { |
||
1135 | if ( current_user_can( 'install_plugins' ) ) { |
||
1136 | $install_link_count++; |
||
1137 | |||
1138 | if ( true === $plugin['required'] ) { |
||
1139 | $message['notice_can_install_required'][] = $slug; |
||
1140 | } else { |
||
1141 | $message['notice_can_install_recommended'][] = $slug; |
||
1142 | } |
||
1143 | } |
||
1144 | if ( true === $plugin['required'] ) { |
||
1145 | $total_required_action_count++; |
||
1146 | } |
||
1147 | } else { |
||
1148 | if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) { |
||
1149 | if ( current_user_can( 'activate_plugins' ) ) { |
||
1150 | $activate_link_count++; |
||
1151 | |||
1152 | if ( true === $plugin['required'] ) { |
||
1153 | $message['notice_can_activate_required'][] = $slug; |
||
1154 | } else { |
||
1155 | $message['notice_can_activate_recommended'][] = $slug; |
||
1156 | } |
||
1157 | } |
||
1158 | if ( true === $plugin['required'] ) { |
||
1159 | $total_required_action_count++; |
||
1160 | } |
||
1161 | } |
||
1162 | |||
1163 | if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) { |
||
1164 | |||
1165 | if ( current_user_can( 'update_plugins' ) ) { |
||
1166 | $update_link_count++; |
||
1167 | |||
1168 | if ( $this->does_plugin_require_update( $slug ) ) { |
||
1169 | $message['notice_ask_to_update'][] = $slug; |
||
1170 | } elseif ( false !== $this->does_plugin_have_update( $slug ) ) { |
||
1171 | $message['notice_ask_to_update_maybe'][] = $slug; |
||
1172 | } |
||
1173 | } |
||
1174 | if ( true === $plugin['required'] ) { |
||
1175 | $total_required_action_count++; |
||
1176 | } |
||
1177 | } |
||
1178 | } |
||
1179 | } |
||
1180 | unset( $slug, $plugin ); |
||
1181 | |||
1182 | // If we have notices to display, we move forward. |
||
1183 | if ( ! empty( $message ) || $total_required_action_count > 0 ) { |
||
1184 | krsort( $message ); // Sort messages. |
||
1185 | $rendered = ''; |
||
1186 | |||
1187 | // As add_settings_error() wraps the final message in a <p> and as the final message can't be |
||
1188 | // filtered, using <p>'s in our html would render invalid html output. |
||
1189 | $line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n"; |
||
1190 | |||
1191 | if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) { |
||
1192 | $rendered = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] ); |
||
1193 | $rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template ); |
||
1194 | } else { |
||
1195 | |||
1196 | // If dismissable is false and a message is set, output it now. |
||
1197 | if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) { |
||
1198 | $rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) ); |
||
1199 | } |
||
1200 | |||
1201 | // Render the individual message lines for the notice. |
||
1202 | foreach ( $message as $type => $plugin_group ) { |
||
1203 | $linked_plugins = array(); |
||
1204 | |||
1205 | // Get the external info link for a plugin if one is available. |
||
1206 | foreach ( $plugin_group as $plugin_slug ) { |
||
1207 | $linked_plugins[] = $this->get_info_link( $plugin_slug ); |
||
1208 | } |
||
1209 | unset( $plugin_slug ); |
||
1210 | |||
1211 | $count = count( $plugin_group ); |
||
1212 | $linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins ); |
||
1213 | $last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability. |
||
1214 | $imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin ); |
||
1215 | |||
1216 | $rendered .= sprintf( |
||
1217 | $line_template, |
||
1218 | sprintf( |
||
1219 | translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ), |
||
1220 | $imploded, |
||
1221 | $count |
||
1222 | ) |
||
1223 | ); |
||
1224 | |||
1225 | } |
||
1226 | unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded ); |
||
1227 | |||
1228 | $rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template ); |
||
1229 | } |
||
1230 | |||
1231 | // Register the nag messages and prepare them to be processed. |
||
1232 | add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() ); |
||
1233 | } |
||
1234 | |||
1235 | // Admin options pages already output settings_errors, so this is to avoid duplication. |
||
1236 | if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) { |
||
1237 | $this->display_settings_errors(); |
||
1238 | } |
||
1239 | } |
||
1240 | |||
1241 | /** |
||
1242 | * Generate the user action links for the admin notice. |
||
1243 | * |
||
1244 | * @since 2.6.0 |
||
1245 | * |
||
1246 | * @param int $install_count Number of plugins to install. |
||
1247 | * @param int $update_count Number of plugins to update. |
||
1248 | * @param int $activate_count Number of plugins to activate. |
||
1249 | * @param int $line_template Template for the HTML tag to output a line. |
||
1250 | * @return string Action links. |
||
1251 | */ |
||
1252 | protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) { |
||
1299 | |||
1300 | /** |
||
1301 | * Get admin notice class. |
||
1302 | * |
||
1303 | * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7 |
||
1304 | * (lowest supported version by TGMPA). |
||
1305 | * |
||
1306 | * @since 2.6.0 |
||
1307 | * |
||
1308 | * @return string |
||
1309 | */ |
||
1310 | protected function get_admin_notice_class() { |
||
1323 | |||
1324 | /** |
||
1325 | * Display settings errors and remove those which have been displayed to avoid duplicate messages showing |
||
1326 | * |
||
1327 | * @since 2.5.0 |
||
1328 | */ |
||
1329 | protected function display_settings_errors() { |
||
1341 | |||
1342 | /** |
||
1343 | * Register dismissal of admin notices. |
||
1344 | * |
||
1345 | * Acts on the dismiss link in the admin nag messages. |
||
1346 | * If clicked, the admin notice disappears and will no longer be visible to this user. |
||
1347 | * |
||
1348 | * @since 2.1.0 |
||
1349 | */ |
||
1350 | public function dismiss() { |
||
1355 | |||
1356 | /** |
||
1357 | * Add individual plugin to our collection of plugins. |
||
1358 | * |
||
1359 | * If the required keys are not set or the plugin has already |
||
1360 | * been registered, the plugin is not added. |
||
1361 | * |
||
1362 | * @since 2.0.0 |
||
1363 | * |
||
1364 | * @param array|null $plugin Array of plugin arguments or null if invalid argument. |
||
1365 | * @return null Return early if incorrect argument. |
||
1366 | */ |
||
1367 | public function register( $plugin ) { |
||
1368 | if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) { |
||
1369 | return; |
||
1370 | } |
||
1371 | |||
1372 | if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) { |
||
1373 | return; |
||
1374 | } |
||
1375 | |||
1376 | $defaults = array( |
||
1377 | 'name' => '', // String |
||
1378 | 'slug' => '', // String |
||
1379 | 'source' => 'repo', // String |
||
1380 | 'required' => false, // Boolean |
||
1381 | 'version' => '', // String |
||
1382 | 'force_activation' => false, // Boolean |
||
1383 | 'force_deactivation' => false, // Boolean |
||
1384 | 'external_url' => '', // String |
||
1385 | 'is_callable' => '', // String|Array. |
||
1386 | ); |
||
1387 | |||
1388 | // Prepare the received data. |
||
1389 | $plugin = wp_parse_args( $plugin, $defaults ); |
||
1390 | |||
1391 | // Standardize the received slug. |
||
1392 | $plugin['slug'] = $this->sanitize_key( $plugin['slug'] ); |
||
1393 | |||
1394 | // Forgive users for using string versions of booleans or floats for version number. |
||
1395 | $plugin['version'] = (string) $plugin['version']; |
||
1396 | $plugin['source'] = empty( $plugin['source'] ) ? 'repo' : $plugin['source']; |
||
1397 | $plugin['required'] = TGMPA_Utils::validate_bool( $plugin['required'] ); |
||
1398 | $plugin['force_activation'] = TGMPA_Utils::validate_bool( $plugin['force_activation'] ); |
||
1399 | $plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] ); |
||
1400 | |||
1401 | // Enrich the received data. |
||
1402 | $plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] ); |
||
1403 | $plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] ); |
||
1404 | |||
1405 | // Set the class properties. |
||
1406 | $this->plugins[ $plugin['slug'] ] = $plugin; |
||
1407 | $this->sort_order[ $plugin['slug'] ] = $plugin['name']; |
||
1408 | |||
1409 | // Should we add the force activation hook ? |
||
1410 | if ( true === $plugin['force_activation'] ) { |
||
1411 | $this->has_forced_activation = true; |
||
1412 | } |
||
1413 | |||
1414 | // Should we add the force deactivation hook ? |
||
1415 | if ( true === $plugin['force_deactivation'] ) { |
||
1416 | $this->has_forced_deactivation = true; |
||
1417 | } |
||
1418 | } |
||
1419 | |||
1420 | /** |
||
1421 | * Determine what type of source the plugin comes from. |
||
1422 | * |
||
1423 | * @since 2.5.0 |
||
1424 | * |
||
1425 | * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path |
||
1426 | * (= bundled) or an external URL. |
||
1427 | * @return string 'repo', 'external', or 'bundled' |
||
1428 | */ |
||
1429 | protected function get_plugin_source_type( $source ) { |
||
1438 | |||
1439 | /** |
||
1440 | * Sanitizes a string key. |
||
1441 | * |
||
1442 | * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are* |
||
1443 | * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase |
||
1444 | * characters in the plugin directory path/slug. Silly them. |
||
1445 | * |
||
1446 | * @see https://developer.wordpress.org/reference/hooks/sanitize_key/ |
||
1447 | * |
||
1448 | * @since 2.5.0 |
||
1449 | * |
||
1450 | * @param string $key String key. |
||
1451 | * @return string Sanitized key |
||
1452 | */ |
||
1453 | public function sanitize_key( $key ) { |
||
1467 | |||
1468 | /** |
||
1469 | * Amend default configuration settings. |
||
1470 | * |
||
1471 | * @since 2.0.0 |
||
1472 | * |
||
1473 | * @param array $config Array of config options to pass as class properties. |
||
1474 | */ |
||
1475 | public function config( $config ) { |
||
1500 | |||
1501 | /** |
||
1502 | * Amend action link after plugin installation. |
||
1503 | * |
||
1504 | * @since 2.0.0 |
||
1505 | * |
||
1506 | * @param array $install_actions Existing array of actions. |
||
1507 | * @return false|array Amended array of actions. |
||
1508 | */ |
||
1509 | public function actions( $install_actions ) { |
||
1517 | |||
1518 | /** |
||
1519 | * Flushes the plugins cache on theme switch to prevent stale entries |
||
1520 | * from remaining in the plugin table. |
||
1521 | * |
||
1522 | * @since 2.4.0 |
||
1523 | * |
||
1524 | * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache. |
||
1525 | * Parameter added in v2.5.0. |
||
1526 | */ |
||
1527 | public function flush_plugins_cache( $clear_update_cache = true ) { |
||
1528 | wp_clean_plugins_cache( $clear_update_cache ); |
||
1529 | } |
||
1530 | |||
1531 | /** |
||
1532 | * Set file_path key for each installed plugin. |
||
1533 | * |
||
1534 | * @since 2.1.0 |
||
1535 | * |
||
1536 | * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin. |
||
1537 | * Parameter added in v2.5.0. |
||
1538 | */ |
||
1539 | public function populate_file_path( $plugin_slug = '' ) { |
||
1549 | |||
1550 | /** |
||
1551 | * Helper function to extract the file path of the plugin file from the |
||
1552 | * plugin slug, if the plugin is installed. |
||
1553 | * |
||
1554 | * @since 2.0.0 |
||
1555 | * |
||
1556 | * @param string $slug Plugin slug (typically folder name) as provided by the developer. |
||
1557 | * @return string Either file path for plugin if installed, or just the plugin slug. |
||
1558 | */ |
||
1559 | protected function _get_plugin_basename_from_slug( $slug ) { |
||
1570 | |||
1571 | /** |
||
1572 | * Retrieve plugin data, given the plugin name. |
||
1573 | * |
||
1574 | * Loops through the registered plugins looking for $name. If it finds it, |
||
1575 | * it returns the $data from that plugin. Otherwise, returns false. |
||
1576 | * |
||
1577 | * @since 2.1.0 |
||
1578 | * |
||
1579 | * @param string $name Name of the plugin, as it was registered. |
||
1580 | * @param string $data Optional. Array key of plugin data to return. Default is slug. |
||
1581 | * @return string|boolean Plugin slug if found, false otherwise. |
||
1582 | */ |
||
1583 | public function _get_plugin_data_from_name( $name, $data = 'slug' ) { |
||
1584 | foreach ( $this->plugins as $values ) { |
||
1585 | if ( $name === $values['name'] && isset( $values[ $data ] ) ) { |
||
1586 | return $values[ $data ]; |
||
1587 | } |
||
1588 | } |
||
1589 | |||
1590 | return false; |
||
1591 | } |
||
1592 | |||
1593 | /** |
||
1594 | * Retrieve the download URL for a package. |
||
1595 | * |
||
1596 | * @since 2.5.0 |
||
1597 | * |
||
1598 | * @param string $slug Plugin slug. |
||
1599 | * @return string Plugin download URL or path to local file or empty string if undetermined. |
||
1600 | */ |
||
1601 | public function get_download_url( $slug ) { |
||
1602 | $dl_source = ''; |
||
1603 | |||
1604 | switch ( $this->plugins[ $slug ]['source_type'] ) { |
||
1605 | case 'repo': |
||
1606 | return $this->get_wp_repo_download_url( $slug ); |
||
1607 | case 'external': |
||
1608 | return $this->plugins[ $slug ]['source']; |
||
1609 | case 'bundled': |
||
1610 | return $this->default_path . $this->plugins[ $slug ]['source']; |
||
1611 | } |
||
1612 | |||
1613 | return $dl_source; // Should never happen. |
||
1614 | } |
||
1615 | |||
1616 | /** |
||
1617 | * Retrieve the download URL for a WP repo package. |
||
1618 | * |
||
1619 | * @since 2.5.0 |
||
1620 | * |
||
1621 | * @param string $slug Plugin slug. |
||
1622 | * @return string Plugin download URL. |
||
1623 | */ |
||
1624 | protected function get_wp_repo_download_url( $slug ) { |
||
1625 | $source = ''; |
||
1626 | $api = $this->get_plugins_api( $slug ); |
||
1627 | |||
1628 | if ( false !== $api && isset( $api->download_link ) ) { |
||
1629 | $source = $api->download_link; |
||
1630 | } |
||
1631 | |||
1632 | return $source; |
||
1633 | } |
||
1634 | |||
1635 | /** |
||
1636 | * Try to grab information from WordPress API. |
||
1637 | * |
||
1638 | * @since 2.5.0 |
||
1639 | * |
||
1640 | * @param string $slug Plugin slug. |
||
1641 | * @return object Plugins_api response object on success, WP_Error on failure. |
||
1642 | */ |
||
1643 | protected function get_plugins_api( $slug ) { |
||
1644 | static $api = array(); // Cache received responses. |
||
1645 | |||
1646 | if ( ! isset( $api[ $slug ] ) ) { |
||
1647 | if ( ! function_exists( 'plugins_api' ) ) { |
||
1648 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
||
1649 | } |
||
1650 | |||
1651 | $response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); |
||
1652 | |||
1653 | $api[ $slug ] = false; |
||
1654 | |||
1655 | if ( is_wp_error( $response ) ) { |
||
1656 | wp_die( esc_html( $this->strings['oops'] ) ); |
||
1657 | } else { |
||
1658 | $api[ $slug ] = $response; |
||
1659 | } |
||
1660 | } |
||
1661 | |||
1662 | return $api[ $slug ]; |
||
1663 | } |
||
1664 | |||
1665 | /** |
||
1666 | * Retrieve a link to a plugin information page. |
||
1667 | * |
||
1668 | * @since 2.5.0 |
||
1669 | * |
||
1670 | * @param string $slug Plugin slug. |
||
1671 | * @return string Fully formed html link to a plugin information page if available |
||
1672 | * or the plugin name if not. |
||
1673 | */ |
||
1674 | public function get_info_link( $slug ) { |
||
1675 | if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) { |
||
1676 | $link = sprintf( |
||
1677 | '<a href="%1$s" target="_blank">%2$s</a>', |
||
1678 | esc_url( $this->plugins[ $slug ]['external_url'] ), |
||
1679 | esc_html( $this->plugins[ $slug ]['name'] ) |
||
1680 | ); |
||
1681 | } elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) { |
||
1682 | $url = add_query_arg( |
||
1683 | array( |
||
1684 | 'tab' => 'plugin-information', |
||
1685 | 'plugin' => urlencode( $slug ), |
||
1686 | 'TB_iframe' => 'true', |
||
1687 | 'width' => '640', |
||
1688 | 'height' => '500', |
||
1689 | ), |
||
1690 | self_admin_url( 'plugin-install.php' ) |
||
1691 | ); |
||
1692 | |||
1693 | $link = sprintf( |
||
1694 | '<a href="%1$s" class="thickbox">%2$s</a>', |
||
1695 | esc_url( $url ), |
||
1696 | esc_html( $this->plugins[ $slug ]['name'] ) |
||
1697 | ); |
||
1698 | } else { |
||
1699 | $link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink. |
||
1700 | } |
||
1701 | |||
1702 | return $link; |
||
1703 | } |
||
1704 | |||
1705 | /** |
||
1706 | * Determine if we're on the TGMPA Install page. |
||
1707 | * |
||
1708 | * @since 2.1.0 |
||
1709 | * |
||
1710 | * @return boolean True when on the TGMPA page, false otherwise. |
||
1711 | */ |
||
1712 | protected function is_tgmpa_page() { |
||
1715 | |||
1716 | /** |
||
1717 | * Determine if we're on a WP Core installation/upgrade page. |
||
1718 | * |
||
1719 | * @since 2.6.0 |
||
1720 | * |
||
1721 | * @return boolean True when on a WP Core installation/upgrade page, false otherwise. |
||
1722 | */ |
||
1723 | protected function is_core_update_page() { |
||
1744 | |||
1745 | /** |
||
1746 | * Retrieve the URL to the TGMPA Install page. |
||
1747 | * |
||
1748 | * I.e. depending on the config settings passed something along the lines of: |
||
1749 | * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins |
||
1750 | * |
||
1751 | * @since 2.5.0 |
||
1752 | * |
||
1753 | * @return string Properly encoded URL (not escaped). |
||
1754 | */ |
||
1755 | public function get_tgmpa_url() { |
||
1773 | |||
1774 | /** |
||
1775 | * Retrieve the URL to the TGMPA Install page for a specific plugin status (view). |
||
1776 | * |
||
1777 | * I.e. depending on the config settings passed something along the lines of: |
||
1778 | * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install |
||
1779 | * |
||
1780 | * @since 2.5.0 |
||
1781 | * |
||
1782 | * @param string $status Plugin status - either 'install', 'update' or 'activate'. |
||
1783 | * @return string Properly encoded URL (not escaped). |
||
1784 | */ |
||
1785 | public function get_tgmpa_status_url( $status ) { |
||
1786 | return add_query_arg( |
||
1787 | array( |
||
1788 | 'plugin_status' => urlencode( $status ), |
||
1789 | ), |
||
1790 | $this->get_tgmpa_url() |
||
1791 | ); |
||
1792 | } |
||
1793 | |||
1794 | /** |
||
1795 | * Determine whether there are open actions for plugins registered with TGMPA. |
||
1796 | * |
||
1797 | * @since 2.5.0 |
||
1798 | * |
||
1799 | * @return bool True if complete, i.e. no outstanding actions. False otherwise. |
||
1800 | */ |
||
1801 | public function is_tgmpa_complete() { |
||
1812 | |||
1813 | /** |
||
1814 | * Check if a plugin is installed. Does not take must-use plugins into account. |
||
1815 | * |
||
1816 | * @since 2.5.0 |
||
1817 | * |
||
1818 | * @param string $slug Plugin slug. |
||
1819 | * @return bool True if installed, false otherwise. |
||
1820 | */ |
||
1821 | public function is_plugin_installed( $slug ) { |
||
1822 | $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached). |
||
1823 | |||
1824 | return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) ); |
||
1825 | } |
||
1826 | |||
1827 | /** |
||
1828 | * Check if a plugin is active. |
||
1829 | * |
||
1830 | * @since 2.5.0 |
||
1831 | * |
||
1832 | * @param string $slug Plugin slug. |
||
1833 | * @return bool True if active, false otherwise. |
||
1834 | */ |
||
1835 | public function is_plugin_active( $slug ) { |
||
1836 | return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) ); |
||
1837 | } |
||
1838 | |||
1839 | /** |
||
1840 | * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required |
||
1841 | * available, check whether the current install meets them. |
||
1842 | * |
||
1843 | * @since 2.5.0 |
||
1844 | * |
||
1845 | * @param string $slug Plugin slug. |
||
1846 | * @return bool True if OK to update, false otherwise. |
||
1847 | */ |
||
1848 | public function can_plugin_update( $slug ) { |
||
1849 | // We currently can't get reliable info on non-WP-repo plugins - issue #380. |
||
1850 | if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { |
||
1851 | return true; |
||
1852 | } |
||
1853 | |||
1854 | $api = $this->get_plugins_api( $slug ); |
||
1855 | |||
1856 | if ( false !== $api && isset( $api->requires ) ) { |
||
1857 | return version_compare( $this->wp_version, $api->requires, '>=' ); |
||
1858 | } |
||
1859 | |||
1860 | // No usable info received from the plugins API, presume we can update. |
||
1861 | return true; |
||
1862 | } |
||
1863 | |||
1864 | /** |
||
1865 | * Check to see if the plugin is 'updatetable', i.e. installed, with an update available |
||
1866 | * and no WP version requirements blocking it. |
||
1867 | * |
||
1868 | * @since 2.6.0 |
||
1869 | * |
||
1870 | * @param string $slug Plugin slug. |
||
1871 | * @return bool True if OK to proceed with update, false otherwise. |
||
1872 | */ |
||
1873 | public function is_plugin_updatetable( $slug ) { |
||
1880 | |||
1881 | /** |
||
1882 | * Check if a plugin can be activated, i.e. is not currently active and meets the minimum |
||
1883 | * plugin version requirements set in TGMPA (if any). |
||
1884 | * |
||
1885 | * @since 2.5.0 |
||
1886 | * |
||
1887 | * @param string $slug Plugin slug. |
||
1888 | * @return bool True if OK to activate, false otherwise. |
||
1889 | */ |
||
1890 | public function can_plugin_activate( $slug ) { |
||
1891 | return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) ); |
||
1892 | } |
||
1893 | |||
1894 | /** |
||
1895 | * Retrieve the version number of an installed plugin. |
||
1896 | * |
||
1897 | * @since 2.5.0 |
||
1898 | * |
||
1899 | * @param string $slug Plugin slug. |
||
1900 | * @return string Version number as string or an empty string if the plugin is not installed |
||
1901 | * or version unknown (plugins which don't comply with the plugin header standard). |
||
1902 | */ |
||
1903 | public function get_installed_version( $slug ) { |
||
1904 | $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached). |
||
1905 | |||
1906 | if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) { |
||
1907 | return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version']; |
||
1908 | } |
||
1909 | |||
1910 | return ''; |
||
1911 | } |
||
1912 | |||
1913 | /** |
||
1914 | * Check whether a plugin complies with the minimum version requirements. |
||
1915 | * |
||
1916 | * @since 2.5.0 |
||
1917 | * |
||
1918 | * @param string $slug Plugin slug. |
||
1919 | * @return bool True when a plugin needs to be updated, otherwise false. |
||
1920 | */ |
||
1921 | public function does_plugin_require_update( $slug ) { |
||
1922 | $installed_version = $this->get_installed_version( $slug ); |
||
1923 | $minimum_version = $this->plugins[ $slug ]['version']; |
||
1924 | |||
1925 | return version_compare( $minimum_version, $installed_version, '>' ); |
||
1926 | } |
||
1927 | |||
1928 | /** |
||
1929 | * Check whether there is an update available for a plugin. |
||
1930 | * |
||
1931 | * @since 2.5.0 |
||
1932 | * |
||
1933 | * @param string $slug Plugin slug. |
||
1934 | * @return false|string Version number string of the available update or false if no update available. |
||
1935 | */ |
||
1936 | public function does_plugin_have_update( $slug ) { |
||
1937 | // Presume bundled and external plugins will point to a package which meets the minimum required version. |
||
1938 | if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { |
||
1939 | if ( $this->does_plugin_require_update( $slug ) ) { |
||
1940 | return $this->plugins[ $slug ]['version']; |
||
1941 | } |
||
1942 | |||
1943 | return false; |
||
1944 | } |
||
1945 | |||
1946 | $repo_updates = get_site_transient( 'update_plugins' ); |
||
1947 | |||
1948 | View Code Duplication | if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) { |
|
1949 | return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version; |
||
1950 | } |
||
1951 | |||
1952 | return false; |
||
1953 | } |
||
1954 | |||
1955 | /** |
||
1956 | * Retrieve potential upgrade notice for a plugin. |
||
1957 | * |
||
1958 | * @since 2.5.0 |
||
1959 | * |
||
1960 | * @param string $slug Plugin slug. |
||
1961 | * @return string The upgrade notice or an empty string if no message was available or provided. |
||
1962 | */ |
||
1963 | public function get_upgrade_notice( $slug ) { |
||
1964 | // We currently can't get reliable info on non-WP-repo plugins - issue #380. |
||
1965 | if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { |
||
1966 | return ''; |
||
1967 | } |
||
1968 | |||
1969 | $repo_updates = get_site_transient( 'update_plugins' ); |
||
1970 | |||
1971 | View Code Duplication | if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) { |
|
1972 | return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice; |
||
1973 | } |
||
1974 | |||
1975 | return ''; |
||
1976 | } |
||
1977 | |||
1978 | /** |
||
1979 | * Wrapper around the core WP get_plugins function, making sure it's actually available. |
||
1980 | * |
||
1981 | * @since 2.5.0 |
||
1982 | * |
||
1983 | * @param string $plugin_folder Optional. Relative path to single plugin folder. |
||
1984 | * @return array Array of installed plugins with plugin information. |
||
1985 | */ |
||
1986 | public function get_plugins( $plugin_folder = '' ) { |
||
1987 | if ( ! function_exists( 'get_plugins' ) ) { |
||
1988 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
1989 | } |
||
1990 | |||
1991 | return get_plugins( $plugin_folder ); |
||
1992 | } |
||
1993 | |||
1994 | /** |
||
1995 | * Delete dismissable nag option when theme is switched. |
||
1996 | * |
||
1997 | * This ensures that the user(s) is/are again reminded via nag of required |
||
1998 | * and/or recommended plugins if they re-activate the theme. |
||
1999 | * |
||
2000 | * @since 2.1.1 |
||
2001 | */ |
||
2002 | public function update_dismiss() { |
||
2005 | |||
2006 | /** |
||
2007 | * Forces plugin activation if the parameter 'force_activation' is |
||
2008 | * set to true. |
||
2009 | * |
||
2010 | * This allows theme authors to specify certain plugins that must be |
||
2011 | * active at all times while using the current theme. |
||
2012 | * |
||
2013 | * Please take special care when using this parameter as it has the |
||
2014 | * potential to be harmful if not used correctly. Setting this parameter |
||
2015 | * to true will not allow the specified plugin to be deactivated unless |
||
2016 | * the user switches themes. |
||
2017 | * |
||
2018 | * @since 2.2.0 |
||
2019 | */ |
||
2020 | public function force_activation() { |
||
2033 | |||
2034 | /** |
||
2035 | * Forces plugin deactivation if the parameter 'force_deactivation' |
||
2036 | * is set to true and adds the plugin to the 'recently active' plugins list. |
||
2037 | * |
||
2038 | * This allows theme authors to specify certain plugins that must be |
||
2039 | * deactivated upon switching from the current theme to another. |
||
2040 | * |
||
2041 | * Please take special care when using this parameter as it has the |
||
2042 | * potential to be harmful if not used correctly. |
||
2043 | * |
||
2044 | * @since 2.2.0 |
||
2045 | */ |
||
2046 | public function force_deactivation() { |
||
2064 | |||
2065 | /** |
||
2066 | * Echo the current TGMPA version number to the page. |
||
2067 | * |
||
2068 | * @since 2.5.0 |
||
2069 | */ |
||
2070 | public function show_tgmpa_version() { |
||
2081 | |||
2082 | /** |
||
2083 | * Returns the singleton instance of the class. |
||
2084 | * |
||
2085 | * @since 2.4.0 |
||
2086 | * |
||
2087 | * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object. |
||
2088 | */ |
||
2089 | public static function get_instance() { |
||
2096 | } |
||
2097 | |||
2098 | if ( ! function_exists( 'load_tgm_plugin_activation' ) ) { |
||
2099 | /** |
||
2100 | * Ensure only one instance of the class is ever invoked. |
||
2101 | * |
||
2102 | * @since 2.5.0 |
||
2103 | */ |
||
2104 | function load_tgm_plugin_activation() { |
||
2105 | $GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance(); |
||
2106 | } |
||
2107 | } |
||
2108 | |||
2109 | if ( did_action( 'plugins_loaded' ) ) { |
||
2110 | load_tgm_plugin_activation(); |
||
2111 | } else { |
||
2112 | add_action( 'plugins_loaded', 'load_tgm_plugin_activation' ); |
||
2113 | } |
||
2114 | } |
||
2115 | |||
2116 | if ( ! function_exists( 'tgmpa' ) ) { |
||
2117 | /** |
||
2118 | * Helper function to register a collection of required plugins. |
||
3854 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state