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 |
||
55 | class TGM_Plugin_Activation { |
||
56 | /** |
||
57 | * TGMPA version number. |
||
58 | * |
||
59 | * @since 2.5.0 |
||
60 | * |
||
61 | * @const string Version number. |
||
62 | */ |
||
63 | const TGMPA_VERSION = '2.6.1'; |
||
64 | |||
65 | /** |
||
66 | * Regular expression to test if a URL is a WP plugin repo URL. |
||
67 | * |
||
68 | * @const string Regex. |
||
69 | * |
||
70 | * @since 2.5.0 |
||
71 | */ |
||
72 | const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|'; |
||
73 | |||
74 | /** |
||
75 | * Arbitrary regular expression to test if a string starts with a URL. |
||
76 | * |
||
77 | * @const string Regex. |
||
78 | * |
||
79 | * @since 2.5.0 |
||
80 | */ |
||
81 | const IS_URL_REGEX = '|^http[s]?://|'; |
||
82 | |||
83 | /** |
||
84 | * Holds a copy of itself, so it can be referenced by the class name. |
||
85 | * |
||
86 | * @since 1.0.0 |
||
87 | * |
||
88 | * @var TGM_Plugin_Activation |
||
89 | */ |
||
90 | public static $instance; |
||
91 | |||
92 | /** |
||
93 | * Holds arrays of plugin details. |
||
94 | * |
||
95 | * @since 1.0.0 |
||
96 | * @since 2.5.0 the array has the plugin slug as an associative key. |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | public $plugins = array(); |
||
101 | |||
102 | /** |
||
103 | * Holds arrays of plugin names to use to sort the plugins array. |
||
104 | * |
||
105 | * @since 2.5.0 |
||
106 | * |
||
107 | * @var array |
||
108 | */ |
||
109 | protected $sort_order = array(); |
||
110 | |||
111 | /** |
||
112 | * Whether any plugins have the 'force_activation' setting set to true. |
||
113 | * |
||
114 | * @since 2.5.0 |
||
115 | * |
||
116 | * @var bool |
||
117 | */ |
||
118 | protected $has_forced_activation = false; |
||
119 | |||
120 | /** |
||
121 | * Whether any plugins have the 'force_deactivation' setting set to true. |
||
122 | * |
||
123 | * @since 2.5.0 |
||
124 | * |
||
125 | * @var bool |
||
126 | */ |
||
127 | protected $has_forced_deactivation = false; |
||
128 | |||
129 | /** |
||
130 | * Name of the unique ID to hash notices. |
||
131 | * |
||
132 | * @since 2.4.0 |
||
133 | * |
||
134 | * @var string |
||
135 | */ |
||
136 | public $id = 'tgmpa'; |
||
137 | |||
138 | /** |
||
139 | * Name of the query-string argument for the admin page. |
||
140 | * |
||
141 | * @since 1.0.0 |
||
142 | * |
||
143 | * @var string |
||
144 | */ |
||
145 | protected $menu = 'tgmpa-install-plugins'; |
||
146 | |||
147 | /** |
||
148 | * Parent menu file slug. |
||
149 | * |
||
150 | * @since 2.5.0 |
||
151 | * |
||
152 | * @var string |
||
153 | */ |
||
154 | public $parent_slug = 'themes.php'; |
||
155 | |||
156 | /** |
||
157 | * Capability needed to view the plugin installation menu item. |
||
158 | * |
||
159 | * @since 2.5.0 |
||
160 | * |
||
161 | * @var string |
||
162 | */ |
||
163 | public $capability = 'edit_theme_options'; |
||
164 | |||
165 | /** |
||
166 | * Default absolute path to folder containing bundled plugin zip files. |
||
167 | * |
||
168 | * @since 2.0.0 |
||
169 | * |
||
170 | * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string. |
||
171 | */ |
||
172 | public $default_path = ''; |
||
173 | |||
174 | /** |
||
175 | * Flag to show admin notices or not. |
||
176 | * |
||
177 | * @since 2.1.0 |
||
178 | * |
||
179 | * @var boolean |
||
180 | */ |
||
181 | public $has_notices = true; |
||
182 | |||
183 | /** |
||
184 | * Flag to determine if the user can dismiss the notice nag. |
||
185 | * |
||
186 | * @since 2.4.0 |
||
187 | * |
||
188 | * @var boolean |
||
189 | */ |
||
190 | public $dismissable = true; |
||
191 | |||
192 | /** |
||
193 | * Message to be output above nag notice if dismissable is false. |
||
194 | * |
||
195 | * @since 2.4.0 |
||
196 | * |
||
197 | * @var string |
||
198 | */ |
||
199 | public $dismiss_msg = ''; |
||
200 | |||
201 | /** |
||
202 | * Flag to set automatic activation of plugins. Off by default. |
||
203 | * |
||
204 | * @since 2.2.0 |
||
205 | * |
||
206 | * @var boolean |
||
207 | */ |
||
208 | public $is_automatic = false; |
||
209 | |||
210 | /** |
||
211 | * Optional message to display before the plugins table. |
||
212 | * |
||
213 | * @since 2.2.0 |
||
214 | * |
||
215 | * @var string Message filtered by wp_kses_post(). Default is empty string. |
||
216 | */ |
||
217 | public $message = ''; |
||
218 | |||
219 | /** |
||
220 | * Holds configurable array of strings. |
||
221 | * |
||
222 | * Default values are added in the constructor. |
||
223 | * |
||
224 | * @since 2.0.0 |
||
225 | * |
||
226 | * @var array |
||
227 | */ |
||
228 | public $strings = array(); |
||
229 | |||
230 | /** |
||
231 | * Holds the version of WordPress. |
||
232 | * |
||
233 | * @since 2.4.0 |
||
234 | * |
||
235 | * @var int |
||
236 | */ |
||
237 | public $wp_version; |
||
238 | |||
239 | /** |
||
240 | * Holds the hook name for the admin page. |
||
241 | * |
||
242 | * @since 2.5.0 |
||
243 | * |
||
244 | * @var string |
||
245 | */ |
||
246 | public $page_hook; |
||
247 | |||
248 | /** |
||
249 | * Adds a reference of this object to $instance, populates default strings, |
||
250 | * does the tgmpa_init action hook, and hooks in the interactions to init. |
||
251 | * |
||
252 | * {@internal This method should be `protected`, but as too many TGMPA implementations |
||
253 | * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues. |
||
254 | * Reverted back to public for the time being.}} |
||
255 | * |
||
256 | * @since 1.0.0 |
||
257 | * |
||
258 | * @see TGM_Plugin_Activation::init() |
||
259 | */ |
||
260 | public function __construct() { |
||
279 | |||
280 | /** |
||
281 | * Magic method to (not) set protected properties from outside of this class. |
||
282 | * |
||
283 | * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6 where the `menu` property |
||
284 | * is being assigned rather than tested in a conditional, effectively rendering it useless. |
||
285 | * This 'hack' prevents this from happening.}} |
||
286 | * |
||
287 | * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593 |
||
288 | * |
||
289 | * @since 2.5.2 |
||
290 | * |
||
291 | * @param string $name Name of an inaccessible property. |
||
292 | * @param mixed $value Value to assign to the property. |
||
293 | * @return void Silently fail to set the property when this is tried from outside of this class context. |
||
294 | * (Inside this class context, the __set() method if not used as there is direct access.) |
||
295 | */ |
||
296 | public function __set( $name, $value ) { |
||
297 | // phpcs:ignore Squiz.PHP.NonExecutableCode.ReturnNotRequired -- See explanation above. |
||
298 | return; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Magic method to get the value of a protected property outside of this class context. |
||
303 | * |
||
304 | * @since 2.5.2 |
||
305 | * |
||
306 | * @param string $name Name of an inaccessible property. |
||
307 | * @return mixed The property value. |
||
308 | */ |
||
309 | public function __get( $name ) { |
||
310 | return $this->{$name}; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Initialise the interactions between this class and WordPress. |
||
315 | * |
||
316 | * Hooks in three new methods for the class: admin_menu, notices and styles. |
||
317 | * |
||
318 | * @since 2.0.0 |
||
319 | * |
||
320 | * @see TGM_Plugin_Activation::admin_menu() |
||
321 | * @see TGM_Plugin_Activation::notices() |
||
322 | * @see TGM_Plugin_Activation::styles() |
||
323 | */ |
||
324 | public function init() { |
||
325 | /** |
||
326 | * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter |
||
327 | * you can overrule that behaviour. |
||
328 | * |
||
329 | * @since 2.5.0 |
||
330 | * |
||
331 | * @param bool $load Whether or not TGMPA should load. |
||
332 | * Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`. |
||
333 | */ |
||
334 | if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) { |
||
335 | return; |
||
336 | } |
||
337 | |||
338 | // Load class strings. |
||
339 | $this->strings = array( |
||
340 | 'page_title' => __( 'Install Required Plugins', 'tgmpa' ), |
||
341 | 'menu_title' => __( 'Install Plugins', 'tgmpa' ), |
||
342 | /* translators: %s: plugin name. */ |
||
343 | 'installing' => __( 'Installing Plugin: %s', 'tgmpa' ), |
||
344 | /* translators: %s: plugin name. */ |
||
345 | 'updating' => __( 'Updating Plugin: %s', 'tgmpa' ), |
||
346 | 'oops' => __( 'Something went wrong with the plugin API.', 'tgmpa' ), |
||
347 | /* translators: 1: plugin name(s). */ |
||
348 | 'notice_can_install_required' => _n_noop( |
||
349 | 'This theme requires the following plugin: %1$s.', |
||
350 | 'This theme requires the following plugins: %1$s.', |
||
351 | 'tgmpa' |
||
352 | ), |
||
353 | /* translators: 1: plugin name(s). */ |
||
354 | 'notice_can_install_recommended' => _n_noop( |
||
355 | 'This theme recommends the following plugin: %1$s.', |
||
356 | 'This theme recommends the following plugins: %1$s.', |
||
357 | 'tgmpa' |
||
358 | ), |
||
359 | /* translators: 1: plugin name(s). */ |
||
360 | 'notice_ask_to_update' => _n_noop( |
||
361 | 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', |
||
362 | 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.', |
||
363 | 'tgmpa' |
||
364 | ), |
||
365 | /* translators: 1: plugin name(s). */ |
||
366 | 'notice_ask_to_update_maybe' => _n_noop( |
||
367 | 'There is an update available for: %1$s.', |
||
368 | 'There are updates available for the following plugins: %1$s.', |
||
369 | 'tgmpa' |
||
370 | ), |
||
371 | /* translators: 1: plugin name(s). */ |
||
372 | 'notice_can_activate_required' => _n_noop( |
||
373 | 'The following required plugin is currently inactive: %1$s.', |
||
374 | 'The following required plugins are currently inactive: %1$s.', |
||
375 | 'tgmpa' |
||
376 | ), |
||
377 | /* translators: 1: plugin name(s). */ |
||
378 | 'notice_can_activate_recommended' => _n_noop( |
||
379 | 'The following recommended plugin is currently inactive: %1$s.', |
||
380 | 'The following recommended plugins are currently inactive: %1$s.', |
||
381 | 'tgmpa' |
||
382 | ), |
||
383 | 'install_link' => _n_noop( |
||
384 | 'Begin installing plugin', |
||
385 | 'Begin installing plugins', |
||
386 | 'tgmpa' |
||
387 | ), |
||
388 | 'update_link' => _n_noop( |
||
389 | 'Begin updating plugin', |
||
390 | 'Begin updating plugins', |
||
391 | 'tgmpa' |
||
392 | ), |
||
393 | 'activate_link' => _n_noop( |
||
394 | 'Begin activating plugin', |
||
395 | 'Begin activating plugins', |
||
396 | 'tgmpa' |
||
397 | ), |
||
398 | 'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ), |
||
399 | 'dashboard' => __( 'Return to the Dashboard', 'tgmpa' ), |
||
400 | 'plugin_activated' => __( 'Plugin activated successfully.', 'tgmpa' ), |
||
401 | 'activated_successfully' => __( 'The following plugin was activated successfully:', 'tgmpa' ), |
||
402 | /* translators: 1: plugin name. */ |
||
403 | 'plugin_already_active' => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ), |
||
404 | /* translators: 1: plugin name. */ |
||
405 | 'plugin_needs_higher_version' => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ), |
||
406 | /* translators: 1: dashboard link. */ |
||
407 | 'complete' => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ), |
||
408 | 'dismiss' => __( 'Dismiss this notice', 'tgmpa' ), |
||
409 | 'notice_cannot_install_activate' => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ), |
||
410 | 'contact_admin' => __( 'Please contact the administrator of this site for help.', 'tgmpa' ), |
||
411 | ); |
||
412 | |||
413 | do_action( 'tgmpa_register' ); |
||
414 | |||
415 | /* After this point, the plugins should be registered and the configuration set. */ |
||
416 | |||
417 | // Proceed only if we have plugins to handle. |
||
418 | if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { |
||
419 | return; |
||
420 | } |
||
421 | |||
422 | // Set up the menu and notices if we still have outstanding actions. |
||
423 | if ( true !== $this->is_tgmpa_complete() ) { |
||
424 | // Sort the plugins. |
||
425 | array_multisort( $this->sort_order, SORT_ASC, $this->plugins ); |
||
426 | |||
427 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
||
428 | add_action( 'admin_head', array( $this, 'dismiss' ) ); |
||
429 | |||
430 | // Prevent the normal links from showing underneath a single install/update page. |
||
431 | add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) ); |
||
432 | add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) ); |
||
433 | |||
434 | if ( $this->has_notices ) { |
||
435 | add_action( 'admin_notices', array( $this, 'notices' ) ); |
||
436 | add_action( 'admin_init', array( $this, 'admin_init' ), 1 ); |
||
437 | add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) ); |
||
438 | } |
||
439 | } |
||
440 | |||
441 | // If needed, filter plugin action links. |
||
442 | add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 ); |
||
443 | |||
444 | // Make sure things get reset on switch theme. |
||
445 | add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) ); |
||
446 | |||
447 | if ( $this->has_notices ) { |
||
448 | add_action( 'switch_theme', array( $this, 'update_dismiss' ) ); |
||
449 | } |
||
450 | |||
451 | // Setup the force activation hook. |
||
452 | if ( true === $this->has_forced_activation ) { |
||
453 | add_action( 'admin_init', array( $this, 'force_activation' ) ); |
||
454 | } |
||
455 | |||
456 | // Setup the force deactivation hook. |
||
457 | if ( true === $this->has_forced_deactivation ) { |
||
458 | add_action( 'switch_theme', array( $this, 'force_deactivation' ) ); |
||
459 | } |
||
460 | |||
461 | // Add CSS for the TGMPA admin page. |
||
462 | add_action( 'admin_head', array( $this, 'admin_css' ) ); |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Load translations. |
||
467 | * |
||
468 | * @since 2.6.0 |
||
469 | * |
||
470 | * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to |
||
471 | * get round the different ways of handling the path and deprecated notices being thrown |
||
472 | * and such. For plugins, the actual file name will be corrected by a filter.}} |
||
473 | * |
||
474 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
475 | * generator on the website.}} |
||
476 | */ |
||
477 | public function load_textdomain() { |
||
478 | if ( is_textdomain_loaded( 'tgmpa' ) ) { |
||
479 | return; |
||
480 | } |
||
481 | |||
482 | if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) { |
||
483 | // Plugin, we'll need to adjust the file name. |
||
484 | add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 ); |
||
485 | load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' ); |
||
486 | remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 ); |
||
487 | } else { |
||
488 | load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' ); |
||
489 | } |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Correct the .mo file name for (must-use) plugins. |
||
494 | * |
||
495 | * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`. |
||
496 | * |
||
497 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
498 | * generator on the website.}} |
||
499 | * |
||
500 | * @since 2.6.0 |
||
501 | * |
||
502 | * @param string $mofile Full path to the target mofile. |
||
503 | * @param string $domain The domain for which a language file is being loaded. |
||
504 | * @return string $mofile |
||
505 | */ |
||
506 | public function correct_plugin_mofile( $mofile, $domain ) { |
||
507 | // Exit early if not our domain (just in case). |
||
508 | if ( 'tgmpa' !== $domain ) { |
||
509 | return $mofile; |
||
510 | } |
||
511 | return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile ); |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Potentially overload the fall-back translation file for the current language. |
||
516 | * |
||
517 | * WP, by default since WP 3.7, will load a local translation first and if none |
||
518 | * can be found, will try and find a translation in the /wp-content/languages/ directory. |
||
519 | * As this library is theme/plugin agnostic, translation files for TGMPA can exist both |
||
520 | * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory. |
||
521 | * |
||
522 | * This method makes sure both directories are checked. |
||
523 | * |
||
524 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
525 | * generator on the website.}} |
||
526 | * |
||
527 | * @since 2.6.0 |
||
528 | * |
||
529 | * @param string $mofile Full path to the target mofile. |
||
530 | * @param string $domain The domain for which a language file is being loaded. |
||
531 | * @return string $mofile |
||
532 | */ |
||
533 | public function overload_textdomain_mofile( $mofile, $domain ) { |
||
534 | // Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable. |
||
535 | if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) { |
||
536 | return $mofile; |
||
537 | } |
||
538 | |||
539 | // Current fallback file is not valid, let's try the alternative option. |
||
540 | if ( false !== strpos( $mofile, '/themes/' ) ) { |
||
541 | return str_replace( '/themes/', '/plugins/', $mofile ); |
||
542 | } elseif ( false !== strpos( $mofile, '/plugins/' ) ) { |
||
543 | return str_replace( '/plugins/', '/themes/', $mofile ); |
||
544 | } else { |
||
545 | return $mofile; |
||
546 | } |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Hook in plugin action link filters for the WP native plugins page. |
||
551 | * |
||
552 | * - Prevent activation of plugins which don't meet the minimum version requirements. |
||
553 | * - Prevent deactivation of force-activated plugins. |
||
554 | * - Add update notice if update available. |
||
555 | * |
||
556 | * @since 2.5.0 |
||
557 | */ |
||
558 | public function add_plugin_action_link_filters() { |
||
559 | foreach ( $this->plugins as $slug => $plugin ) { |
||
560 | if ( false === $this->can_plugin_activate( $slug ) ) { |
||
561 | add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 ); |
||
562 | } |
||
563 | |||
564 | if ( true === $plugin['force_activation'] ) { |
||
565 | add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 ); |
||
566 | } |
||
567 | |||
568 | if ( false !== $this->does_plugin_require_update( $slug ) ) { |
||
569 | add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 ); |
||
570 | } |
||
571 | } |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the |
||
576 | * minimum version requirements. |
||
577 | * |
||
578 | * @since 2.5.0 |
||
579 | * |
||
580 | * @param array $actions Action links. |
||
581 | * @return array |
||
582 | */ |
||
583 | public function filter_plugin_action_links_activate( $actions ) { |
||
584 | unset( $actions['activate'] ); |
||
585 | |||
586 | return $actions; |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate. |
||
591 | * |
||
592 | * @since 2.5.0 |
||
593 | * |
||
594 | * @param array $actions Action links. |
||
595 | * @return array |
||
596 | */ |
||
597 | public function filter_plugin_action_links_deactivate( $actions ) { |
||
598 | unset( $actions['deactivate'] ); |
||
599 | |||
600 | return $actions; |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the |
||
605 | * minimum version requirements. |
||
606 | * |
||
607 | * @since 2.5.0 |
||
608 | * |
||
609 | * @param array $actions Action links. |
||
610 | * @return array |
||
611 | */ |
||
612 | public function filter_plugin_action_links_update( $actions ) { |
||
613 | $actions['update'] = sprintf( |
||
614 | '<a href="%1$s" title="%2$s" class="edit">%3$s</a>', |
||
615 | esc_url( $this->get_tgmpa_status_url( 'update' ) ), |
||
616 | esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ), |
||
617 | esc_html__( 'Update Required', 'tgmpa' ) |
||
618 | ); |
||
619 | |||
620 | return $actions; |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Handles calls to show plugin information via links in the notices. |
||
625 | * |
||
626 | * We get the links in the admin notices to point to the TGMPA page, rather |
||
627 | * than the typical plugin-install.php file, so we can prepare everything |
||
628 | * beforehand. |
||
629 | * |
||
630 | * WP does not make it easy to show the plugin information in the thickbox - |
||
631 | * here we have to require a file that includes a function that does the |
||
632 | * main work of displaying it, enqueue some styles, set up some globals and |
||
633 | * finally call that function before exiting. |
||
634 | * |
||
635 | * Down right easy once you know how... |
||
636 | * |
||
637 | * Returns early if not the TGMPA page. |
||
638 | * |
||
639 | * @since 2.1.0 |
||
640 | * |
||
641 | * @global string $tab Used as iframe div class names, helps with styling |
||
642 | * @global string $body_id Used as the iframe body ID, helps with styling |
||
643 | * |
||
644 | * @return null Returns early if not the TGMPA page. |
||
645 | */ |
||
646 | public function admin_init() { |
||
647 | if ( ! $this->is_tgmpa_page() ) { |
||
648 | return; |
||
649 | } |
||
650 | |||
651 | if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) { |
||
652 | // Needed for install_plugin_information(). |
||
653 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
||
654 | |||
655 | wp_enqueue_style( 'plugin-install' ); |
||
656 | |||
657 | global $tab, $body_id; |
||
658 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WP requirement. |
||
659 | $body_id = 'plugin-information'; |
||
660 | |||
661 | // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Overriding the WP global is the point. |
||
662 | $tab = 'plugin-information'; |
||
663 | |||
664 | install_plugin_information(); |
||
665 | |||
666 | exit; |
||
667 | } |
||
668 | } |
||
669 | |||
670 | /** |
||
671 | * Enqueue thickbox scripts/styles for plugin info. |
||
672 | * |
||
673 | * Thickbox is not automatically included on all admin pages, so we must |
||
674 | * manually enqueue it for those pages. |
||
675 | * |
||
676 | * Thickbox is only loaded if the user has not dismissed the admin |
||
677 | * notice or if there are any plugins left to install and activate. |
||
678 | * |
||
679 | * @since 2.1.0 |
||
680 | */ |
||
681 | public function thickbox() { |
||
682 | if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) { |
||
683 | add_thickbox(); |
||
684 | } |
||
685 | } |
||
686 | |||
687 | /** |
||
688 | * Adds submenu page if there are plugin actions to take. |
||
689 | * |
||
690 | * This method adds the submenu page letting users know that a required |
||
691 | * plugin needs to be installed. |
||
692 | * |
||
693 | * This page disappears once the plugin has been installed and activated. |
||
694 | * |
||
695 | * @since 1.0.0 |
||
696 | * |
||
697 | * @see TGM_Plugin_Activation::init() |
||
698 | * @see TGM_Plugin_Activation::install_plugins_page() |
||
699 | * |
||
700 | * @return null Return early if user lacks capability to install a plugin. |
||
701 | */ |
||
702 | public function admin_menu() { |
||
703 | // Make sure privileges are correct to see the page. |
||
704 | if ( ! current_user_can( 'install_plugins' ) ) { |
||
705 | return; |
||
706 | } |
||
707 | |||
708 | $args = apply_filters( |
||
709 | 'tgmpa_admin_menu_args', |
||
710 | array( |
||
711 | 'parent_slug' => $this->parent_slug, // Parent Menu slug. |
||
712 | 'page_title' => $this->strings['page_title'], // Page title. |
||
713 | 'menu_title' => $this->strings['menu_title'], // Menu title. |
||
714 | 'capability' => $this->capability, // Capability. |
||
715 | 'menu_slug' => $this->menu, // Menu slug. |
||
716 | 'function' => array( $this, 'install_plugins_page' ), // Callback. |
||
717 | ) |
||
718 | ); |
||
719 | |||
720 | $this->add_admin_menu( $args ); |
||
721 | } |
||
722 | |||
723 | /** |
||
724 | * Add the menu item. |
||
725 | * |
||
726 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
727 | * generator on the website.}} |
||
728 | * |
||
729 | * @since 2.5.0 |
||
730 | * |
||
731 | * @param array $args Menu item configuration. |
||
732 | */ |
||
733 | protected function add_admin_menu( array $args ) { |
||
734 | if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) { |
||
735 | _deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) ); |
||
736 | } |
||
737 | |||
738 | if ( 'themes.php' === $this->parent_slug ) { |
||
739 | $this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); |
||
740 | } else { |
||
741 | $this->page_hook = call_user_func( 'add_submenu_page', $args['parent_slug'], $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); |
||
742 | } |
||
743 | } |
||
744 | |||
745 | /** |
||
746 | * Echoes plugin installation form. |
||
747 | * |
||
748 | * This method is the callback for the admin_menu method function. |
||
749 | * This displays the admin page and form area where the user can select to install and activate the plugin. |
||
750 | * Aborts early if we're processing a plugin installation action. |
||
751 | * |
||
752 | * @since 1.0.0 |
||
753 | * |
||
754 | * @return null Aborts early if we're processing a plugin installation action. |
||
755 | */ |
||
756 | public function install_plugins_page() { |
||
757 | // Store new instance of plugin table in object. |
||
758 | $plugin_table = new TGMPA_List_Table(); |
||
759 | |||
760 | // Return early if processing a plugin installation action. |
||
761 | if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) { |
||
762 | return; |
||
763 | } |
||
764 | |||
765 | // Force refresh of available plugin information so we'll know about manual updates/deletes. |
||
766 | wp_clean_plugins_cache( false ); |
||
767 | |||
768 | ?> |
||
769 | <div class="tgmpa wrap"> |
||
770 | <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
||
771 | <?php $plugin_table->prepare_items(); ?> |
||
772 | |||
773 | <?php |
||
774 | if ( ! empty( $this->message ) && is_string( $this->message ) ) { |
||
775 | echo wp_kses_post( $this->message ); |
||
776 | } |
||
777 | ?> |
||
778 | <?php $plugin_table->views(); ?> |
||
779 | |||
780 | <form id="tgmpa-plugins" action="" method="post"> |
||
781 | <input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" /> |
||
782 | <input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" /> |
||
783 | <?php $plugin_table->display(); ?> |
||
784 | </form> |
||
785 | </div> |
||
786 | <?php |
||
787 | } |
||
788 | |||
789 | /** |
||
790 | * Installs, updates or activates a plugin depending on the action link clicked by the user. |
||
791 | * |
||
792 | * Checks the $_GET variable to see which actions have been |
||
793 | * passed and responds with the appropriate method. |
||
794 | * |
||
795 | * Uses WP_Filesystem to process and handle the plugin installation |
||
796 | * method. |
||
797 | * |
||
798 | * @since 1.0.0 |
||
799 | * |
||
800 | * @uses WP_Filesystem |
||
801 | * @uses WP_Error |
||
802 | * @uses WP_Upgrader |
||
803 | * @uses Plugin_Upgrader |
||
804 | * @uses Plugin_Installer_Skin |
||
805 | * @uses Plugin_Upgrader_Skin |
||
806 | * |
||
807 | * @return boolean True on success, false on failure. |
||
808 | */ |
||
809 | protected function do_plugin_install() { |
||
|
|||
810 | if ( empty( $_GET['plugin'] ) ) { |
||
811 | return false; |
||
812 | } |
||
813 | |||
814 | // All plugin information will be stored in an array for processing. |
||
815 | $slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) ); |
||
816 | |||
817 | if ( ! isset( $this->plugins[ $slug ] ) ) { |
||
818 | return false; |
||
819 | } |
||
820 | |||
821 | // Was an install or upgrade action link clicked? |
||
822 | if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) { |
||
823 | |||
824 | $install_type = 'install'; |
||
825 | if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) { |
||
826 | $install_type = 'update'; |
||
827 | } |
||
828 | |||
829 | check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' ); |
||
830 | |||
831 | // Pass necessary information via URL if WP_Filesystem is needed. |
||
832 | $url = wp_nonce_url( |
||
833 | add_query_arg( |
||
834 | array( |
||
835 | 'plugin' => urlencode( $slug ), |
||
836 | 'tgmpa-' . $install_type => $install_type . '-plugin', |
||
837 | ), |
||
838 | $this->get_tgmpa_url() |
||
839 | ), |
||
840 | 'tgmpa-' . $install_type, |
||
841 | 'tgmpa-nonce' |
||
842 | ); |
||
843 | |||
844 | $method = ''; // Leave blank so WP_Filesystem can populate it as necessary. |
||
845 | |||
846 | $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ); |
||
847 | if ( false === $creds ) { |
||
848 | return true; |
||
849 | } |
||
850 | |||
851 | View Code Duplication | if ( ! WP_Filesystem( $creds ) ) { |
|
852 | request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem. |
||
853 | return true; |
||
854 | } |
||
855 | |||
856 | /* If we arrive here, we have the filesystem. */ |
||
857 | |||
858 | // Prep variables for Plugin_Installer_Skin class. |
||
859 | $extra = array(); |
||
860 | $extra['slug'] = $slug; // Needed for potentially renaming of directory name. |
||
861 | $source = $this->get_download_url( $slug ); |
||
862 | $api = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null; |
||
863 | $api = ( false !== $api ) ? $api : null; |
||
864 | |||
865 | $url = add_query_arg( |
||
866 | array( |
||
867 | 'action' => $install_type . '-plugin', |
||
868 | 'plugin' => urlencode( $slug ), |
||
869 | ), |
||
870 | 'update.php' |
||
871 | ); |
||
872 | |||
873 | if ( ! class_exists( 'Plugin_Upgrader', false ) ) { |
||
874 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
875 | } |
||
876 | |||
877 | $title = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing']; |
||
878 | $skin_args = array( |
||
879 | 'type' => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload', |
||
880 | 'title' => sprintf( $title, $this->plugins[ $slug ]['name'] ), |
||
881 | 'url' => esc_url_raw( $url ), |
||
882 | 'nonce' => $install_type . '-plugin_' . $slug, |
||
883 | 'plugin' => '', |
||
884 | 'api' => $api, |
||
885 | 'extra' => $extra, |
||
886 | ); |
||
887 | unset( $title ); |
||
888 | |||
889 | if ( 'update' === $install_type ) { |
||
890 | $skin_args['plugin'] = $this->plugins[ $slug ]['file_path']; |
||
891 | $skin = new Plugin_Upgrader_Skin( $skin_args ); |
||
892 | } else { |
||
893 | $skin = new Plugin_Installer_Skin( $skin_args ); |
||
894 | } |
||
895 | |||
896 | // Create a new instance of Plugin_Upgrader. |
||
897 | $upgrader = new Plugin_Upgrader( $skin ); |
||
898 | |||
899 | // Perform the action and install the plugin from the $source urldecode(). |
||
900 | add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 ); |
||
901 | |||
902 | if ( 'update' === $install_type ) { |
||
903 | // Inject our info into the update transient. |
||
904 | $to_inject = array( |
||
905 | $slug => $this->plugins[ $slug ], |
||
906 | ); |
||
907 | $to_inject[ $slug ]['source'] = $source; |
||
908 | $this->inject_update_info( $to_inject ); |
||
909 | |||
910 | $upgrader->upgrade( $this->plugins[ $slug ]['file_path'] ); |
||
911 | } else { |
||
912 | $upgrader->install( $source ); |
||
913 | } |
||
914 | |||
915 | remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 ); |
||
916 | |||
917 | // Make sure we have the correct file path now the plugin is installed/updated. |
||
918 | $this->populate_file_path( $slug ); |
||
919 | |||
920 | // Only activate plugins if the config option is set to true and the plugin isn't |
||
921 | // already active (upgrade). |
||
922 | if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) { |
||
923 | $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method. |
||
924 | if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { |
||
925 | return true; // Finish execution of the function early as we encountered an error. |
||
926 | } |
||
927 | } |
||
928 | |||
929 | $this->show_tgmpa_version(); |
||
930 | |||
931 | // Display message based on if all plugins are now active or not. |
||
932 | if ( $this->is_tgmpa_complete() ) { |
||
933 | echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html( $this->strings['dashboard'] ) . '</a>' ), '</p>'; |
||
934 | echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; |
||
935 | } else { |
||
936 | echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; |
||
937 | } |
||
938 | |||
939 | return true; |
||
940 | } elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) { |
||
941 | // Activate action link was clicked. |
||
942 | check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' ); |
||
943 | |||
944 | if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) { |
||
945 | return true; // Finish execution of the function early as we encountered an error. |
||
946 | } |
||
947 | } |
||
948 | |||
949 | return false; |
||
950 | } |
||
951 | |||
952 | /** |
||
953 | * Inject information into the 'update_plugins' site transient as WP checks that before running an update. |
||
954 | * |
||
955 | * @since 2.5.0 |
||
956 | * |
||
957 | * @param array $plugins The plugin information for the plugins which are to be updated. |
||
958 | */ |
||
959 | public function inject_update_info( $plugins ) { |
||
960 | $repo_updates = get_site_transient( 'update_plugins' ); |
||
961 | |||
962 | if ( ! is_object( $repo_updates ) ) { |
||
963 | $repo_updates = new stdClass(); |
||
964 | } |
||
965 | |||
966 | foreach ( $plugins as $slug => $plugin ) { |
||
967 | $file_path = $plugin['file_path']; |
||
968 | |||
969 | if ( empty( $repo_updates->response[ $file_path ] ) ) { |
||
970 | $repo_updates->response[ $file_path ] = new stdClass(); |
||
971 | } |
||
972 | |||
973 | // We only really need to set package, but let's do all we can in case WP changes something. |
||
974 | $repo_updates->response[ $file_path ]->slug = $slug; |
||
975 | $repo_updates->response[ $file_path ]->plugin = $file_path; |
||
976 | $repo_updates->response[ $file_path ]->new_version = $plugin['version']; |
||
977 | $repo_updates->response[ $file_path ]->package = $plugin['source']; |
||
978 | if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) { |
||
979 | $repo_updates->response[ $file_path ]->url = $plugin['external_url']; |
||
980 | } |
||
981 | } |
||
982 | |||
983 | set_site_transient( 'update_plugins', $repo_updates ); |
||
984 | } |
||
985 | |||
986 | /** |
||
987 | * Adjust the plugin directory name if necessary. |
||
988 | * |
||
989 | * The final destination directory of a plugin is based on the subdirectory name found in the |
||
990 | * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this |
||
991 | * subdirectory name is not the same as the expected slug and the plugin will not be recognized |
||
992 | * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to |
||
993 | * the expected plugin slug. |
||
994 | * |
||
995 | * @since 2.5.0 |
||
996 | * |
||
997 | * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/. |
||
998 | * @param string $remote_source Path to upgrade/zip-file-name.tmp. |
||
999 | * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin. |
||
1000 | * @return string $source |
||
1001 | */ |
||
1002 | public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { |
||
1003 | if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) { |
||
1004 | return $source; |
||
1005 | } |
||
1006 | |||
1007 | // Check for single file plugins. |
||
1008 | $source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) ); |
||
1009 | if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) { |
||
1010 | return $source; |
||
1011 | } |
||
1012 | |||
1013 | // Multi-file plugin, let's see if the directory is correctly named. |
||
1014 | $desired_slug = ''; |
||
1015 | |||
1016 | // Figure out what the slug is supposed to be. |
||
1017 | if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) { |
||
1018 | $desired_slug = $upgrader->skin->options['extra']['slug']; |
||
1019 | } else { |
||
1020 | // Bulk installer contains less info, so fall back on the info registered here. |
||
1021 | foreach ( $this->plugins as $slug => $plugin ) { |
||
1022 | if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) { |
||
1023 | $desired_slug = $slug; |
||
1024 | break; |
||
1025 | } |
||
1026 | } |
||
1027 | unset( $slug, $plugin ); |
||
1028 | } |
||
1029 | |||
1030 | if ( ! empty( $desired_slug ) ) { |
||
1031 | $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) ); |
||
1032 | |||
1033 | if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) { |
||
1034 | $from_path = untrailingslashit( $source ); |
||
1035 | $to_path = trailingslashit( $remote_source ) . $desired_slug; |
||
1036 | |||
1037 | if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) { |
||
1038 | return trailingslashit( $to_path ); |
||
1039 | View Code Duplication | } else { |
|
1040 | return new WP_Error( |
||
1041 | 'rename_failed', |
||
1042 | 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' ), |
||
1043 | array( |
||
1044 | 'found' => $subdir_name, |
||
1045 | 'expected' => $desired_slug, |
||
1046 | ) |
||
1047 | ); |
||
1048 | } |
||
1049 | View Code Duplication | } elseif ( empty( $subdir_name ) ) { |
|
1050 | return new WP_Error( |
||
1051 | 'packaged_wrong', |
||
1052 | 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' ), |
||
1053 | array( |
||
1054 | 'found' => $subdir_name, |
||
1055 | 'expected' => $desired_slug, |
||
1056 | ) |
||
1057 | ); |
||
1058 | } |
||
1059 | } |
||
1060 | |||
1061 | return $source; |
||
1062 | } |
||
1063 | |||
1064 | /** |
||
1065 | * Activate a single plugin and send feedback about the result to the screen. |
||
1066 | * |
||
1067 | * @since 2.5.0 |
||
1068 | * |
||
1069 | * @param string $file_path Path within wp-plugins/ to main plugin file. |
||
1070 | * @param string $slug Plugin slug. |
||
1071 | * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false. |
||
1072 | * This determines the styling of the output messages. |
||
1073 | * @return bool False if an error was encountered, true otherwise. |
||
1074 | */ |
||
1075 | protected function activate_single_plugin( $file_path, $slug, $automatic = false ) { |
||
1076 | if ( $this->can_plugin_activate( $slug ) ) { |
||
1077 | $activate = activate_plugin( $file_path ); |
||
1078 | |||
1079 | if ( is_wp_error( $activate ) ) { |
||
1080 | echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>', |
||
1081 | '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; |
||
1082 | |||
1083 | return false; // End it here if there is an error with activation. |
||
1084 | } else { |
||
1085 | if ( ! $automatic ) { |
||
1086 | // Make sure message doesn't display again if bulk activation is performed |
||
1087 | // immediately after a single activation. |
||
1088 | // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Not using the superglobal. |
||
1089 | if ( ! isset( $_POST['action'] ) ) { |
||
1090 | echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>'; |
||
1091 | } |
||
1092 | } else { |
||
1093 | // Simpler message layout for use on the plugin install page. |
||
1094 | echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>'; |
||
1095 | } |
||
1096 | } |
||
1097 | View Code Duplication | } elseif ( $this->is_plugin_active( $slug ) ) { |
|
1098 | // No simpler message format provided as this message should never be encountered |
||
1099 | // on the plugin install page. |
||
1100 | echo '<div id="message" class="error"><p>', |
||
1101 | sprintf( |
||
1102 | esc_html( $this->strings['plugin_already_active'] ), |
||
1103 | '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>' |
||
1104 | ), |
||
1105 | '</p></div>'; |
||
1106 | } elseif ( $this->does_plugin_require_update( $slug ) ) { |
||
1107 | if ( ! $automatic ) { |
||
1108 | // Make sure message doesn't display again if bulk activation is performed |
||
1109 | // immediately after a single activation. |
||
1110 | // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Not using the superglobal. |
||
1111 | View Code Duplication | if ( ! isset( $_POST['action'] ) ) { |
|
1112 | echo '<div id="message" class="error"><p>', |
||
1113 | sprintf( |
||
1114 | esc_html( $this->strings['plugin_needs_higher_version'] ), |
||
1115 | '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>' |
||
1116 | ), |
||
1117 | '</p></div>'; |
||
1118 | } |
||
1119 | } else { |
||
1120 | // Simpler message layout for use on the plugin install page. |
||
1121 | echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>'; |
||
1122 | } |
||
1123 | } |
||
1124 | |||
1125 | return true; |
||
1126 | } |
||
1127 | |||
1128 | /** |
||
1129 | * Echoes required plugin notice. |
||
1130 | * |
||
1131 | * Outputs a message telling users that a specific plugin is required for |
||
1132 | * their theme. If appropriate, it includes a link to the form page where |
||
1133 | * users can install and activate the plugin. |
||
1134 | * |
||
1135 | * Returns early if we're on the Install page. |
||
1136 | * |
||
1137 | * @since 1.0.0 |
||
1138 | * |
||
1139 | * @global object $current_screen |
||
1140 | * |
||
1141 | * @return null Returns early if we're on the Install page. |
||
1142 | */ |
||
1143 | public function notices() { |
||
1144 | // Remove nag on the install page / Return early if the nag message has been dismissed or user < author. |
||
1145 | 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' ) ) ) { |
||
1146 | return; |
||
1147 | } |
||
1148 | |||
1149 | // Store for the plugin slugs by message type. |
||
1150 | $message = array(); |
||
1151 | |||
1152 | // Initialize counters used to determine plurality of action link texts. |
||
1153 | $install_link_count = 0; |
||
1154 | $update_link_count = 0; |
||
1155 | $activate_link_count = 0; |
||
1156 | $total_required_action_count = 0; |
||
1157 | |||
1158 | foreach ( $this->plugins as $slug => $plugin ) { |
||
1159 | if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) { |
||
1160 | continue; |
||
1161 | } |
||
1162 | |||
1163 | if ( ! $this->is_plugin_installed( $slug ) ) { |
||
1164 | if ( current_user_can( 'install_plugins' ) ) { |
||
1165 | $install_link_count++; |
||
1166 | |||
1167 | if ( true === $plugin['required'] ) { |
||
1168 | $message['notice_can_install_required'][] = $slug; |
||
1169 | } else { |
||
1170 | $message['notice_can_install_recommended'][] = $slug; |
||
1171 | } |
||
1172 | } |
||
1173 | if ( true === $plugin['required'] ) { |
||
1174 | $total_required_action_count++; |
||
1175 | } |
||
1176 | } else { |
||
1177 | if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) { |
||
1178 | if ( current_user_can( 'activate_plugins' ) ) { |
||
1179 | $activate_link_count++; |
||
1180 | |||
1181 | if ( true === $plugin['required'] ) { |
||
1182 | $message['notice_can_activate_required'][] = $slug; |
||
1183 | } else { |
||
1184 | $message['notice_can_activate_recommended'][] = $slug; |
||
1185 | } |
||
1186 | } |
||
1187 | if ( true === $plugin['required'] ) { |
||
1188 | $total_required_action_count++; |
||
1189 | } |
||
1190 | } |
||
1191 | |||
1192 | if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) { |
||
1193 | |||
1194 | if ( current_user_can( 'update_plugins' ) ) { |
||
1195 | $update_link_count++; |
||
1196 | |||
1197 | if ( $this->does_plugin_require_update( $slug ) ) { |
||
1198 | $message['notice_ask_to_update'][] = $slug; |
||
1199 | } elseif ( false !== $this->does_plugin_have_update( $slug ) ) { |
||
1200 | $message['notice_ask_to_update_maybe'][] = $slug; |
||
1201 | } |
||
1202 | } |
||
1203 | if ( true === $plugin['required'] ) { |
||
1204 | $total_required_action_count++; |
||
1205 | } |
||
1206 | } |
||
1207 | } |
||
1208 | } |
||
1209 | unset( $slug, $plugin ); |
||
1210 | |||
1211 | // If we have notices to display, we move forward. |
||
1212 | if ( ! empty( $message ) || $total_required_action_count > 0 ) { |
||
1213 | krsort( $message ); // Sort messages. |
||
1214 | $rendered = ''; |
||
1215 | |||
1216 | // As add_settings_error() wraps the final message in a <p> and as the final message can't be |
||
1217 | // filtered, using <p>'s in our html would render invalid html output. |
||
1218 | $line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n"; |
||
1219 | |||
1220 | if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) { |
||
1221 | $rendered = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] ); |
||
1222 | $rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template ); |
||
1223 | } else { |
||
1224 | |||
1225 | // If dismissable is false and a message is set, output it now. |
||
1226 | if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) { |
||
1227 | $rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) ); |
||
1228 | } |
||
1229 | |||
1230 | // Render the individual message lines for the notice. |
||
1231 | foreach ( $message as $type => $plugin_group ) { |
||
1232 | $linked_plugins = array(); |
||
1233 | |||
1234 | // Get the external info link for a plugin if one is available. |
||
1235 | foreach ( $plugin_group as $plugin_slug ) { |
||
1236 | $linked_plugins[] = $this->get_info_link( $plugin_slug ); |
||
1237 | } |
||
1238 | unset( $plugin_slug ); |
||
1239 | |||
1240 | $count = count( $plugin_group ); |
||
1241 | $linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins ); |
||
1242 | $last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability. |
||
1243 | $imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin ); |
||
1244 | |||
1245 | $rendered .= sprintf( |
||
1246 | $line_template, |
||
1247 | sprintf( |
||
1248 | translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ), |
||
1249 | $imploded, |
||
1250 | $count |
||
1251 | ) |
||
1252 | ); |
||
1253 | |||
1254 | } |
||
1255 | unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded ); |
||
1256 | |||
1257 | $rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template ); |
||
1258 | } |
||
1259 | |||
1260 | // Register the nag messages and prepare them to be processed. |
||
1261 | add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() ); |
||
1262 | } |
||
1263 | |||
1264 | // Admin options pages already output settings_errors, so this is to avoid duplication. |
||
1265 | if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) { |
||
1266 | $this->display_settings_errors(); |
||
1267 | } |
||
1268 | } |
||
1269 | |||
1270 | /** |
||
1271 | * Generate the user action links for the admin notice. |
||
1272 | * |
||
1273 | * @since 2.6.0 |
||
1274 | * |
||
1275 | * @param int $install_count Number of plugins to install. |
||
1276 | * @param int $update_count Number of plugins to update. |
||
1277 | * @param int $activate_count Number of plugins to activate. |
||
1278 | * @param int $line_template Template for the HTML tag to output a line. |
||
1279 | * @return string Action links. |
||
1280 | */ |
||
1281 | protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) { |
||
1282 | // Setup action links. |
||
1283 | $action_links = array( |
||
1284 | 'install' => '', |
||
1285 | 'update' => '', |
||
1286 | 'activate' => '', |
||
1287 | 'dismiss' => $this->dismissable ? '<a href="' . esc_url( wp_nonce_url( add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ), 'tgmpa-dismiss-' . get_current_user_id() ) ) . '" class="dismiss-notice" target="_parent">' . esc_html( $this->strings['dismiss'] ) . '</a>' : '', |
||
1288 | ); |
||
1289 | |||
1290 | $link_template = '<a href="%2$s">%1$s</a>'; |
||
1291 | |||
1292 | if ( current_user_can( 'install_plugins' ) ) { |
||
1293 | if ( $install_count > 0 ) { |
||
1294 | $action_links['install'] = sprintf( |
||
1295 | $link_template, |
||
1296 | translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ), |
||
1297 | esc_url( $this->get_tgmpa_status_url( 'install' ) ) |
||
1298 | ); |
||
1299 | } |
||
1300 | if ( $update_count > 0 ) { |
||
1301 | $action_links['update'] = sprintf( |
||
1302 | $link_template, |
||
1303 | translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ), |
||
1304 | esc_url( $this->get_tgmpa_status_url( 'update' ) ) |
||
1305 | ); |
||
1306 | } |
||
1307 | } |
||
1308 | |||
1309 | if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) { |
||
1310 | $action_links['activate'] = sprintf( |
||
1311 | $link_template, |
||
1312 | translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ), |
||
1313 | esc_url( $this->get_tgmpa_status_url( 'activate' ) ) |
||
1314 | ); |
||
1315 | } |
||
1316 | |||
1317 | $action_links = apply_filters( 'tgmpa_notice_action_links', $action_links ); |
||
1318 | |||
1319 | $action_links = array_filter( (array) $action_links ); // Remove any empty array items. |
||
1320 | |||
1321 | if ( ! empty( $action_links ) ) { |
||
1322 | $action_links = sprintf( $line_template, implode( ' | ', $action_links ) ); |
||
1323 | return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links ); |
||
1324 | } else { |
||
1325 | return ''; |
||
1326 | } |
||
1327 | } |
||
1328 | |||
1329 | /** |
||
1330 | * Get admin notice class. |
||
1331 | * |
||
1332 | * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7 |
||
1333 | * (lowest supported version by TGMPA). |
||
1334 | * |
||
1335 | * @since 2.6.0 |
||
1336 | * |
||
1337 | * @return string |
||
1338 | */ |
||
1339 | protected function get_admin_notice_class() { |
||
1340 | if ( ! empty( $this->strings['nag_type'] ) ) { |
||
1341 | return sanitize_html_class( strtolower( $this->strings['nag_type'] ) ); |
||
1342 | } else { |
||
1343 | if ( version_compare( $this->wp_version, '4.2', '>=' ) ) { |
||
1344 | return 'notice-warning'; |
||
1345 | } elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) { |
||
1346 | return 'notice'; |
||
1347 | } else { |
||
1348 | return 'updated'; |
||
1349 | } |
||
1350 | } |
||
1351 | } |
||
1352 | |||
1353 | /** |
||
1354 | * Display settings errors and remove those which have been displayed to avoid duplicate messages showing |
||
1355 | * |
||
1356 | * @since 2.5.0 |
||
1357 | */ |
||
1358 | protected function display_settings_errors() { |
||
1359 | global $wp_settings_errors; |
||
1360 | |||
1361 | settings_errors( 'tgmpa' ); |
||
1362 | |||
1363 | foreach ( (array) $wp_settings_errors as $key => $details ) { |
||
1364 | if ( 'tgmpa' === $details['setting'] ) { |
||
1365 | unset( $wp_settings_errors[ $key ] ); |
||
1366 | break; |
||
1367 | } |
||
1368 | } |
||
1369 | } |
||
1370 | |||
1371 | /** |
||
1372 | * Register dismissal of admin notices. |
||
1373 | * |
||
1374 | * Acts on the dismiss link in the admin nag messages. |
||
1375 | * If clicked, the admin notice disappears and will no longer be visible to this user. |
||
1376 | * |
||
1377 | * @since 2.1.0 |
||
1378 | */ |
||
1379 | public function dismiss() { |
||
1380 | if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) { |
||
1381 | update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 ); |
||
1382 | } |
||
1383 | } |
||
1384 | |||
1385 | /** |
||
1386 | * Add individual plugin to our collection of plugins. |
||
1387 | * |
||
1388 | * If the required keys are not set or the plugin has already |
||
1389 | * been registered, the plugin is not added. |
||
1390 | * |
||
1391 | * @since 2.0.0 |
||
1392 | * |
||
1393 | * @param array|null $plugin Array of plugin arguments or null if invalid argument. |
||
1394 | * @return null Return early if incorrect argument. |
||
1395 | */ |
||
1396 | public function register( $plugin ) { |
||
1397 | if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) { |
||
1398 | return; |
||
1399 | } |
||
1400 | |||
1401 | if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) { |
||
1402 | return; |
||
1403 | } |
||
1404 | |||
1405 | $defaults = array( |
||
1406 | 'name' => '', // String. |
||
1407 | 'slug' => '', // String. |
||
1408 | 'source' => 'repo', // String. |
||
1409 | 'required' => false, // Boolean. |
||
1410 | 'version' => '', // String. |
||
1411 | 'force_activation' => false, // Boolean. |
||
1412 | 'force_deactivation' => false, // Boolean. |
||
1413 | 'external_url' => '', // String. |
||
1414 | 'is_callable' => '', // String or array. |
||
1415 | ); |
||
1416 | |||
1417 | // Prepare the received data. |
||
1418 | $plugin = wp_parse_args( $plugin, $defaults ); |
||
1419 | |||
1420 | // Standardize the received slug. |
||
1421 | $plugin['slug'] = $this->sanitize_key( $plugin['slug'] ); |
||
1422 | |||
1423 | // Forgive users for using string versions of booleans or floats for version number. |
||
1424 | $plugin['version'] = (string) $plugin['version']; |
||
1425 | $plugin['source'] = empty( $plugin['source'] ) ? 'repo' : $plugin['source']; |
||
1426 | $plugin['required'] = TGMPA_Utils::validate_bool( $plugin['required'] ); |
||
1427 | $plugin['force_activation'] = TGMPA_Utils::validate_bool( $plugin['force_activation'] ); |
||
1428 | $plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] ); |
||
1429 | |||
1430 | // Enrich the received data. |
||
1431 | $plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] ); |
||
1432 | $plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] ); |
||
1433 | |||
1434 | // Set the class properties. |
||
1435 | $this->plugins[ $plugin['slug'] ] = $plugin; |
||
1436 | $this->sort_order[ $plugin['slug'] ] = $plugin['name']; |
||
1437 | |||
1438 | // Should we add the force activation hook ? |
||
1439 | if ( true === $plugin['force_activation'] ) { |
||
1440 | $this->has_forced_activation = true; |
||
1441 | } |
||
1442 | |||
1443 | // Should we add the force deactivation hook ? |
||
1444 | if ( true === $plugin['force_deactivation'] ) { |
||
1445 | $this->has_forced_deactivation = true; |
||
1446 | } |
||
1447 | } |
||
1448 | |||
1449 | /** |
||
1450 | * Determine what type of source the plugin comes from. |
||
1451 | * |
||
1452 | * @since 2.5.0 |
||
1453 | * |
||
1454 | * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path |
||
1455 | * (= bundled) or an external URL. |
||
1456 | * @return string 'repo', 'external', or 'bundled' |
||
1457 | */ |
||
1458 | protected function get_plugin_source_type( $source ) { |
||
1467 | |||
1468 | /** |
||
1469 | * Sanitizes a string key. |
||
1470 | * |
||
1471 | * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are* |
||
1472 | * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase |
||
1473 | * characters in the plugin directory path/slug. Silly them. |
||
1474 | * |
||
1475 | * @see https://developer.wordpress.org/reference/hooks/sanitize_key/ |
||
1476 | * |
||
1477 | * @since 2.5.0 |
||
1478 | * |
||
1479 | * @param string $key String key. |
||
1480 | * @return string Sanitized key |
||
1481 | */ |
||
1482 | public function sanitize_key( $key ) { |
||
1496 | |||
1497 | /** |
||
1498 | * Amend default configuration settings. |
||
1499 | * |
||
1500 | * @since 2.0.0 |
||
1501 | * |
||
1502 | * @param array $config Array of config options to pass as class properties. |
||
1503 | */ |
||
1504 | public function config( $config ) { |
||
1529 | |||
1530 | /** |
||
1531 | * Amend action link after plugin installation. |
||
1532 | * |
||
1533 | * @since 2.0.0 |
||
1534 | * |
||
1535 | * @param array $install_actions Existing array of actions. |
||
1536 | * @return false|array Amended array of actions. |
||
1537 | */ |
||
1538 | public function actions( $install_actions ) { |
||
1546 | |||
1547 | /** |
||
1548 | * Flushes the plugins cache on theme switch to prevent stale entries |
||
1549 | * from remaining in the plugin table. |
||
1550 | * |
||
1551 | * @since 2.4.0 |
||
1552 | * |
||
1553 | * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache. |
||
1554 | * Parameter added in v2.5.0. |
||
1555 | */ |
||
1556 | public function flush_plugins_cache( $clear_update_cache = true ) { |
||
1559 | |||
1560 | /** |
||
1561 | * Set file_path key for each installed plugin. |
||
1562 | * |
||
1563 | * @since 2.1.0 |
||
1564 | * |
||
1565 | * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin. |
||
1566 | * Parameter added in v2.5.0. |
||
1567 | */ |
||
1568 | public function populate_file_path( $plugin_slug = '' ) { |
||
1578 | |||
1579 | /** |
||
1580 | * Helper function to extract the file path of the plugin file from the |
||
1581 | * plugin slug, if the plugin is installed. |
||
1582 | * |
||
1583 | * @since 2.0.0 |
||
1584 | * |
||
1585 | * @param string $slug Plugin slug (typically folder name) as provided by the developer. |
||
1586 | * @return string Either file path for plugin if installed, or just the plugin slug. |
||
1587 | */ |
||
1588 | protected function _get_plugin_basename_from_slug( $slug ) { |
||
1599 | |||
1600 | /** |
||
1601 | * Retrieve plugin data, given the plugin name. |
||
1602 | * |
||
1603 | * Loops through the registered plugins looking for $name. If it finds it, |
||
1604 | * it returns the $data from that plugin. Otherwise, returns false. |
||
1605 | * |
||
1606 | * @since 2.1.0 |
||
1607 | * |
||
1608 | * @param string $name Name of the plugin, as it was registered. |
||
1609 | * @param string $data Optional. Array key of plugin data to return. Default is slug. |
||
1610 | * @return string|boolean Plugin slug if found, false otherwise. |
||
1611 | */ |
||
1612 | public function _get_plugin_data_from_name( $name, $data = 'slug' ) { |
||
1621 | |||
1622 | /** |
||
1623 | * Retrieve the download URL for a package. |
||
1624 | * |
||
1625 | * @since 2.5.0 |
||
1626 | * |
||
1627 | * @param string $slug Plugin slug. |
||
1628 | * @return string Plugin download URL or path to local file or empty string if undetermined. |
||
1629 | */ |
||
1630 | public function get_download_url( $slug ) { |
||
1644 | |||
1645 | /** |
||
1646 | * Retrieve the download URL for a WP repo package. |
||
1647 | * |
||
1648 | * @since 2.5.0 |
||
1649 | * |
||
1650 | * @param string $slug Plugin slug. |
||
1651 | * @return string Plugin download URL. |
||
1652 | */ |
||
1653 | protected function get_wp_repo_download_url( $slug ) { |
||
1663 | |||
1664 | /** |
||
1665 | * Try to grab information from WordPress API. |
||
1666 | * |
||
1667 | * @since 2.5.0 |
||
1668 | * |
||
1669 | * @param string $slug Plugin slug. |
||
1670 | * @return object Plugins_api response object on success, WP_Error on failure. |
||
1671 | */ |
||
1672 | protected function get_plugins_api( $slug ) { |
||
1701 | |||
1702 | /** |
||
1703 | * Retrieve a link to a plugin information page. |
||
1704 | * |
||
1705 | * @since 2.5.0 |
||
1706 | * |
||
1707 | * @param string $slug Plugin slug. |
||
1708 | * @return string Fully formed html link to a plugin information page if available |
||
1709 | * or the plugin name if not. |
||
1710 | */ |
||
1711 | public function get_info_link( $slug ) { |
||
1741 | |||
1742 | /** |
||
1743 | * Determine if we're on the TGMPA Install page. |
||
1744 | * |
||
1745 | * @since 2.1.0 |
||
1746 | * |
||
1747 | * @return boolean True when on the TGMPA page, false otherwise. |
||
1748 | */ |
||
1749 | protected function is_tgmpa_page() { |
||
1752 | |||
1753 | /** |
||
1754 | * Determine if we're on a WP Core installation/upgrade page. |
||
1755 | * |
||
1756 | * @since 2.6.0 |
||
1757 | * |
||
1758 | * @return boolean True when on a WP Core installation/upgrade page, false otherwise. |
||
1759 | */ |
||
1760 | protected function is_core_update_page() { |
||
1781 | |||
1782 | /** |
||
1783 | * Retrieve the URL to the TGMPA Install page. |
||
1784 | * |
||
1785 | * I.e. depending on the config settings passed something along the lines of: |
||
1786 | * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins |
||
1787 | * |
||
1788 | * @since 2.5.0 |
||
1789 | * |
||
1790 | * @return string Properly encoded URL (not escaped). |
||
1791 | */ |
||
1792 | public function get_tgmpa_url() { |
||
1810 | |||
1811 | /** |
||
1812 | * Retrieve the URL to the TGMPA Install page for a specific plugin status (view). |
||
1813 | * |
||
1814 | * I.e. depending on the config settings passed something along the lines of: |
||
1815 | * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install |
||
1816 | * |
||
1817 | * @since 2.5.0 |
||
1818 | * |
||
1819 | * @param string $status Plugin status - either 'install', 'update' or 'activate'. |
||
1820 | * @return string Properly encoded URL (not escaped). |
||
1821 | */ |
||
1822 | public function get_tgmpa_status_url( $status ) { |
||
1830 | |||
1831 | /** |
||
1832 | * Determine whether there are open actions for plugins registered with TGMPA. |
||
1833 | * |
||
1834 | * @since 2.5.0 |
||
1835 | * |
||
1836 | * @return bool True if complete, i.e. no outstanding actions. False otherwise. |
||
1837 | */ |
||
1838 | public function is_tgmpa_complete() { |
||
1849 | |||
1850 | /** |
||
1851 | * Check if a plugin is installed. Does not take must-use plugins into account. |
||
1852 | * |
||
1853 | * @since 2.5.0 |
||
1854 | * |
||
1855 | * @param string $slug Plugin slug. |
||
1856 | * @return bool True if installed, false otherwise. |
||
1857 | */ |
||
1858 | public function is_plugin_installed( $slug ) { |
||
1863 | |||
1864 | /** |
||
1865 | * Check if a plugin is active. |
||
1866 | * |
||
1867 | * @since 2.5.0 |
||
1868 | * |
||
1869 | * @param string $slug Plugin slug. |
||
1870 | * @return bool True if active, false otherwise. |
||
1871 | */ |
||
1872 | public function is_plugin_active( $slug ) { |
||
1875 | |||
1876 | /** |
||
1877 | * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required |
||
1878 | * available, check whether the current install meets them. |
||
1879 | * |
||
1880 | * @since 2.5.0 |
||
1881 | * |
||
1882 | * @param string $slug Plugin slug. |
||
1883 | * @return bool True if OK to update, false otherwise. |
||
1884 | */ |
||
1885 | public function can_plugin_update( $slug ) { |
||
1900 | |||
1901 | /** |
||
1902 | * Check to see if the plugin is 'updatetable', i.e. installed, with an update available |
||
1903 | * and no WP version requirements blocking it. |
||
1904 | * |
||
1905 | * @since 2.6.0 |
||
1906 | * |
||
1907 | * @param string $slug Plugin slug. |
||
1908 | * @return bool True if OK to proceed with update, false otherwise. |
||
1909 | */ |
||
1910 | public function is_plugin_updatetable( $slug ) { |
||
1917 | |||
1918 | /** |
||
1919 | * Check if a plugin can be activated, i.e. is not currently active and meets the minimum |
||
1920 | * plugin version requirements set in TGMPA (if any). |
||
1921 | * |
||
1922 | * @since 2.5.0 |
||
1923 | * |
||
1924 | * @param string $slug Plugin slug. |
||
1925 | * @return bool True if OK to activate, false otherwise. |
||
1926 | */ |
||
1927 | public function can_plugin_activate( $slug ) { |
||
1930 | |||
1931 | /** |
||
1932 | * Retrieve the version number of an installed plugin. |
||
1933 | * |
||
1934 | * @since 2.5.0 |
||
1935 | * |
||
1936 | * @param string $slug Plugin slug. |
||
1937 | * @return string Version number as string or an empty string if the plugin is not installed |
||
1938 | * or version unknown (plugins which don't comply with the plugin header standard). |
||
1939 | */ |
||
1940 | public function get_installed_version( $slug ) { |
||
1949 | |||
1950 | /** |
||
1951 | * Check whether a plugin complies with the minimum version requirements. |
||
1952 | * |
||
1953 | * @since 2.5.0 |
||
1954 | * |
||
1955 | * @param string $slug Plugin slug. |
||
1956 | * @return bool True when a plugin needs to be updated, otherwise false. |
||
1957 | */ |
||
1958 | public function does_plugin_require_update( $slug ) { |
||
1964 | |||
1965 | /** |
||
1966 | * Check whether there is an update available for a plugin. |
||
1967 | * |
||
1968 | * @since 2.5.0 |
||
1969 | * |
||
1970 | * @param string $slug Plugin slug. |
||
1971 | * @return false|string Version number string of the available update or false if no update available. |
||
1972 | */ |
||
1973 | public function does_plugin_have_update( $slug ) { |
||
1991 | |||
1992 | /** |
||
1993 | * Retrieve potential upgrade notice for a plugin. |
||
1994 | * |
||
1995 | * @since 2.5.0 |
||
1996 | * |
||
1997 | * @param string $slug Plugin slug. |
||
1998 | * @return string The upgrade notice or an empty string if no message was available or provided. |
||
1999 | */ |
||
2000 | public function get_upgrade_notice( $slug ) { |
||
2014 | |||
2015 | /** |
||
2016 | * Wrapper around the core WP get_plugins function, making sure it's actually available. |
||
2017 | * |
||
2018 | * @since 2.5.0 |
||
2019 | * |
||
2020 | * @param string $plugin_folder Optional. Relative path to single plugin folder. |
||
2021 | * @return array Array of installed plugins with plugin information. |
||
2022 | */ |
||
2023 | public function get_plugins( $plugin_folder = '' ) { |
||
2030 | |||
2031 | /** |
||
2032 | * Delete dismissable nag option when theme is switched. |
||
2033 | * |
||
2034 | * This ensures that the user(s) is/are again reminded via nag of required |
||
2035 | * and/or recommended plugins if they re-activate the theme. |
||
2036 | * |
||
2037 | * @since 2.1.1 |
||
2038 | */ |
||
2039 | public function update_dismiss() { |
||
2042 | |||
2043 | /** |
||
2044 | * Forces plugin activation if the parameter 'force_activation' is |
||
2045 | * set to true. |
||
2046 | * |
||
2047 | * This allows theme authors to specify certain plugins that must be |
||
2048 | * active at all times while using the current theme. |
||
2049 | * |
||
2050 | * Please take special care when using this parameter as it has the |
||
2051 | * potential to be harmful if not used correctly. Setting this parameter |
||
2052 | * to true will not allow the specified plugin to be deactivated unless |
||
2053 | * the user switches themes. |
||
2054 | * |
||
2055 | * @since 2.2.0 |
||
2056 | */ |
||
2057 | public function force_activation() { |
||
2070 | |||
2071 | /** |
||
2072 | * Forces plugin deactivation if the parameter 'force_deactivation' |
||
2073 | * is set to true and adds the plugin to the 'recently active' plugins list. |
||
2074 | * |
||
2075 | * This allows theme authors to specify certain plugins that must be |
||
2076 | * deactivated upon switching from the current theme to another. |
||
2077 | * |
||
2078 | * Please take special care when using this parameter as it has the |
||
2079 | * potential to be harmful if not used correctly. |
||
2080 | * |
||
2081 | * @since 2.2.0 |
||
2082 | */ |
||
2083 | public function force_deactivation() { |
||
2101 | |||
2102 | /** |
||
2103 | * Echo the current TGMPA version number to the page. |
||
2104 | * |
||
2105 | * @since 2.5.0 |
||
2106 | */ |
||
2107 | public function show_tgmpa_version() { |
||
2118 | |||
2119 | /** |
||
2120 | * Adds CSS to admin head. |
||
2121 | * |
||
2122 | * @since 2.6.2 |
||
2123 | */ |
||
2124 | public function admin_css() { |
||
2136 | |||
2137 | /** |
||
2138 | * Returns the singleton instance of the class. |
||
2139 | * |
||
2140 | * @since 2.4.0 |
||
2141 | * |
||
2142 | * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object. |
||
2143 | */ |
||
2144 | public static function get_instance() { |
||
2151 | } |
||
2152 | |||
2153 | if ( ! function_exists( 'load_tgm_plugin_activation' ) ) { |
||
2154 | /** |
||
2155 | * Ensure only one instance of the class is ever invoked. |
||
2156 | * |
||
2157 | * @since 2.5.0 |
||
2158 | */ |
||
2159 | function load_tgm_plugin_activation() { |
||
2160 | $GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance(); |
||
2161 | } |
||
2162 | } |
||
2163 | |||
2164 | if ( did_action( 'plugins_loaded' ) ) { |
||
2165 | load_tgm_plugin_activation(); |
||
2166 | } else { |
||
2167 | add_action( 'plugins_loaded', 'load_tgm_plugin_activation' ); |
||
2168 | } |
||
2169 | } |
||
2170 | |||
2171 | if ( ! function_exists( 'tgmpa' ) ) { |
||
2172 | /** |
||
2173 | * Helper function to register a collection of required plugins. |
||
2174 | * |
||
2175 | * @since 2.0.0 |
||
2176 | * @api |
||
3923 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.