PluginProbe
WP Directory Kit / 1.3.1
WP Directory Kit v1.3.1
1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 1.4.1 All 35 releases
wpdirectorykit / tgm-pa / class-tgm-plugin-activation.php

class-tgm-plugin-activation.php in WP Directory Kit 1.3.1, at tgm-pa/class-tgm-plugin-activation.php

3,858 lines 128.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin installation and activation for WordPress themes.
4 *
5 * Please note that this is a drop-in library for a theme or plugin.
6 * The authors of this library (Thomas, Gary and Juliette) are NOT responsible
7 * for the support of your plugin or theme. Please contact the plugin
8 * or theme author for support.
9 *
10 * @package TGM-Plugin-Activation
11 * @version 2.6.1
12 * @link http://tgmpluginactivation.com/
13 * @author Thomas Griffin, Gary Jones, Juliette Reinders Folmer
14 * @copyright Copyright (c) 2011, Thomas Griffin
15 * @license GPL-2.0+
16 */
17
18 /*
19 Copyright 2011 Thomas Griffin (thomasgriffinmedia.com)
20
21 This program is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License, version 2, as
23 published by the Free Software Foundation.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 */
34
35 if ( ! class_exists( 'TGM_Plugin_Activation' ) ) {
36
37 /**
38 * Automatic plugin installation and activation library.
39 *
40 * Creates a way to automatically install and activate plugins from within themes.
41 * The plugins can be either bundled, downloaded from the WordPress
42 * Plugin Repository or downloaded from another external source.
43 *
44 * @since 1.0.0
45 *
46 * @package TGM-Plugin-Activation
47 * @author Thomas Griffin
48 * @author Gary Jones
49 */
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.1';
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 = 'wpdirectorykit';
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() {
256 // Set the current WordPress version.
257 $this->wp_version = $GLOBALS['wp_version'];
258
259 // Announce that the class is ready, and pass the object (for advanced use).
260 do_action_ref_array( 'tgmpa_init', array( $this ) );
261
262 /*
263 * Load our text domain and allow for overloading the fall-back file.
264 *
265 * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
266 * generator on the website.}}
267 */
268 add_action( 'init', array( $this, 'load_textdomain' ), 5 );
269 add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 );
270
271 // When the rest of WP has loaded, kick-start the rest of the class.
272 add_action( 'init', array( $this, 'init' ) );
273 }
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 ) {
292 return;
293 }
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 ) {
304 return $this->{$name};
305 }
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', 'wpdirectorykit' ),
335 'menu_title' => __( 'Install Plugins', 'wpdirectorykit' ),
336 /* translators: %s: plugin name. */
337 'installing' => __( 'Installing Plugin: %s', 'wpdirectorykit' ),
338 /* translators: %s: plugin name. */
339 'updating' => __( 'Updating Plugin: %s', 'wpdirectorykit' ),
340 'oops' => __( 'Something went wrong with the plugin API.', 'wpdirectorykit' ),
341 'notice_can_install_required' => _n_noop(
342 /* translators: 1: plugin name(s). */
343 'This plugin requires the following plugin: %1$s.',
344 'This plugin requires the following plugins: %1$s.',
345 'wpdirectorykit'
346 ),
347 'notice_can_install_recommended' => _n_noop(
348 /* translators: 1: plugin name(s). */
349 'This plugin recommends the following plugin: %1$s.',
350 'This plugin recommends the following plugins: %1$s.',
351 'wpdirectorykit'
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 plugin: %1$s.',
356 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this plugin: %1$s.',
357 'wpdirectorykit'
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 'wpdirectorykit'
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 'wpdirectorykit'
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 'wpdirectorykit'
376 ),
377 'install_link' => _n_noop(
378 'Begin installing plugin',
379 'Begin installing plugins',
380 'wpdirectorykit'
381 ),
382 'update_link' => _n_noop(
383 'Begin updating plugin',
384 'Begin updating plugins',
385 'wpdirectorykit'
386 ),
387 'activate_link' => _n_noop(
388 'Begin activating plugin',
389 'Begin activating plugins',
390 'wpdirectorykit'
391 ),
392 'return' => __( 'Return to Required Plugins Installer', 'wpdirectorykit' ),
393 'dashboard' => __( 'Return to the Dashboard', 'wpdirectorykit' ),
394 'plugin_activated' => __( 'Plugin activated successfully.', 'wpdirectorykit' ),
395 'activated_successfully' => __( 'The following plugin was activated successfully:', 'wpdirectorykit' ),
396 /* translators: 1: plugin name. */
397 'plugin_already_active' => __( 'No action taken. Plugin %1$s was already active.', 'wpdirectorykit' ),
398 /* translators: 1: plugin name. */
399 'plugin_needs_higher_version' => __( 'Plugin not activated. A higher version of %s is needed for this plugin. Please update the plugin.', 'wpdirectorykit' ),
400 /* translators: 1: dashboard link. */
401 'complete' => __( 'All plugins installed and activated successfully. %1$s', 'wpdirectorykit' ),
402 'dismiss' => __( 'Dismiss this notice', 'wpdirectorykit' ),
403 'notice_cannot_install_activate' => __( 'There are one or more required or recommended plugins to install, update or activate.', 'wpdirectorykit' ),
404 'contact_admin' => __( 'Please contact the administrator of this site for help.', 'wpdirectorykit' ),
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() {
469 if ( is_textdomain_loaded( 'wpdirectorykit' ) ) {
470 return;
471 }
472
473 if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) {
474 // Plugin, we'll need to adjust the file name.
475 add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 );
476 load_theme_textdomain( 'wpdirectorykit', dirname( __FILE__ ) . '/languages' );
477 remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 );
478 } else {
479 load_theme_textdomain( 'wpdirectorykit', dirname( __FILE__ ) . '/languages' );
480 }
481 }
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 ) {
498 // Exit early if not our domain (just in case).
499 if ( 'wpdirectorykit' !== $domain ) {
500 return $mofile;
501 }
502 return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile );
503 }
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 ) {
525 // Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
526 if ( 'wpdirectorykit' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) {
527 return $mofile;
528 }
529
530 // Current fallback file is not valid, let's try the alternative option.
531 if ( false !== strpos( $mofile, '/themes/' ) ) {
532 return str_replace( '/themes/', '/plugins/', $mofile );
533 } elseif ( false !== strpos( $mofile, '/plugins/' ) ) {
534 return str_replace( '/plugins/', '/themes/', $mofile );
535 } else {
536 return $mofile;
537 }
538 }
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() {
550 foreach ( $this->plugins as $slug => $plugin ) {
551 if ( false === $this->can_plugin_activate( $slug ) ) {
552 add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 );
553 }
554
555 if ( true === $plugin['force_activation'] ) {
556 add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 );
557 }
558
559 if ( false !== $this->does_plugin_require_update( $slug ) ) {
560 add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 );
561 }
562 }
563 }
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 ) {
575 unset( $actions['activate'] );
576
577 return $actions;
578 }
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 ) {
589 unset( $actions['deactivate'] );
590
591 return $actions;
592 }
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 ) {
604 $actions['update'] = sprintf(
605 '<a href="%1$s" title="%2$s" class="edit">%3$s</a>',
606 esc_url( $this->get_tgmpa_status_url( 'update' ) ),
607 esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'wpdirectorykit' ),
608 esc_html__( 'Update Required', 'wpdirectorykit' )
609 );
610
611 return $actions;
612 }
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() {
638 if ( ! $this->is_tgmpa_page() ) {
639 return;
640 }
641
642 if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) {
643 // Needed for install_plugin_information().
644 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
645
646 wp_enqueue_style( 'plugin-install' );
647
648 global $tab, $body_id;
649 $body_id = 'plugin-information';
650 // @codingStandardsIgnoreStart
651 $tab = 'plugin-information';
652 // @codingStandardsIgnoreEnd
653
654 install_plugin_information();
655
656 exit;
657 }
658 }
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() {
672 if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
673 add_thickbox();
674 }
675 }
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() {
693 // Make sure privileges are correct to see the page.
694 if ( ! current_user_can( 'install_plugins' ) ) {
695 return;
696 }
697
698 $args = apply_filters(
699 'tgmpa_admin_menu_args',
700 array(
701 'parent_slug' => $this->parent_slug, // Parent Menu slug.
702 'page_title' => $this->strings['page_title'], // Page title.
703 'menu_title' => $this->strings['menu_title'], // Menu title.
704 'capability' => $this->capability, // Capability.
705 'menu_slug' => $this->menu, // Menu slug.
706 'function' => array( $this, 'install_plugins_page' ), // Callback.
707 )
708 );
709
710 $this->add_admin_menu( $args );
711 }
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 ) {
724 if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) {
725 _deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'wpdirectorykit' ) );
726 }
727
728 if ( 'themes.php' === $this->parent_slug ) {
729 $this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
730 } else {
731 $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'] );
732 }
733 }
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() {
747 // Store new instance of plugin table in object.
748 $plugin_table = new TGMPA_List_Table;
749
750 // Return early if processing a plugin installation action.
751 if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
752 return;
753 }
754
755 // Force refresh of available plugin information so we'll know about manual updates/deletes.
756 wp_clean_plugins_cache( false );
757
758 ?>
759 <div class="tgmpa wrap">
760 <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
761 <?php $plugin_table->prepare_items(); ?>
762
763 <?php
764 if ( ! empty( $this->message ) && is_string( $this->message ) ) {
765 echo wp_kses_post( $this->message );
766 }
767 ?>
768 <?php $plugin_table->views(); ?>
769
770 <form id="tgmpa-plugins" action="" method="post">
771 <input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
772 <input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
773 <?php $plugin_table->display(); ?>
774 </form>
775 </div>
776 <?php
777 }
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', 'wpdirectorykit' ) . '</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 } 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.', 'wpdirectorykit' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'wpdirectorykit' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) );
1028 }
1029 } 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.', 'wpdirectorykit' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'wpdirectorykit' ), 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 ) {
1049 if ( $this->can_plugin_activate( $slug ) ) {
1050 $activate = activate_plugin( $file_path );
1051
1052 if ( is_wp_error( $activate ) ) {
1053 echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
1054 '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
1055
1056 return false; // End it here if there is an error with activation.
1057 } else {
1058 if ( ! $automatic ) {
1059 // Make sure message doesn't display again if bulk activation is performed
1060 // immediately after a single activation.
1061 if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1062 echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
1063 }
1064 } else {
1065 // Simpler message layout for use on the plugin install page.
1066 echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
1067 }
1068 }
1069 } elseif ( $this->is_plugin_active( $slug ) ) {
1070 // No simpler message format provided as this message should never be encountered
1071 // on the plugin install page.
1072 echo '<div id="message" class="error"><p>',
1073 sprintf(
1074 esc_html( $this->strings['plugin_already_active'] ),
1075 '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1076 ),
1077 '</p></div>';
1078 } elseif ( $this->does_plugin_require_update( $slug ) ) {
1079 if ( ! $automatic ) {
1080 // Make sure message doesn't display again if bulk activation is performed
1081 // immediately after a single activation.
1082 if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1083 echo '<div id="message" class="error"><p>',
1084 sprintf(
1085 esc_html( $this->strings['plugin_needs_higher_version'] ),
1086 '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1087 ),
1088 '</p></div>';
1089 }
1090 } else {
1091 // Simpler message layout for use on the plugin install page.
1092 echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
1093 }
1094 }
1095
1096 return true;
1097 }
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(isset($plugin['has_notices']) && false === $plugin['has_notices']){
1135 continue;
1136 }
1137
1138 if ( ! $this->is_plugin_installed( $slug ) ) {
1139 if ( current_user_can( 'install_plugins' ) ) {
1140 $install_link_count++;
1141
1142 if ( true === $plugin['required'] ) {
1143 $message['notice_can_install_required'][] = $slug;
1144 } else {
1145 $message['notice_can_install_recommended'][] = $slug;
1146 }
1147 }
1148 if ( true === $plugin['required'] ) {
1149 $total_required_action_count++;
1150 }
1151 } else {
1152 if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
1153 if ( current_user_can( 'activate_plugins' ) ) {
1154 $activate_link_count++;
1155
1156 if ( true === $plugin['required'] ) {
1157 $message['notice_can_activate_required'][] = $slug;
1158 } else {
1159 $message['notice_can_activate_recommended'][] = $slug;
1160 }
1161 }
1162 if ( true === $plugin['required'] ) {
1163 $total_required_action_count++;
1164 }
1165 }
1166
1167 if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1168
1169 if ( current_user_can( 'update_plugins' ) ) {
1170 $update_link_count++;
1171
1172 if ( $this->does_plugin_require_update( $slug ) ) {
1173 $message['notice_ask_to_update'][] = $slug;
1174 } elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
1175 $message['notice_ask_to_update_maybe'][] = $slug;
1176 }
1177 }
1178 if ( true === $plugin['required'] ) {
1179 $total_required_action_count++;
1180 }
1181 }
1182 }
1183 }
1184 unset( $slug, $plugin );
1185
1186 // If we have notices to display, we move forward.
1187 if ( ! empty( $message ) || $total_required_action_count > 0 ) {
1188 krsort( $message ); // Sort messages.
1189 $rendered = '';
1190
1191 // As add_settings_error() wraps the final message in a <p> and as the final message can't be
1192 // filtered, using <p>'s in our html would render invalid html output.
1193 $line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
1194
1195 if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
1196 $rendered = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] );
1197 $rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
1198 } else {
1199
1200 // If dismissable is false and a message is set, output it now.
1201 if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
1202 $rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
1203 }
1204
1205 // Render the individual message lines for the notice.
1206 foreach ( $message as $type => $plugin_group ) {
1207 $linked_plugins = array();
1208
1209 // Get the external info link for a plugin if one is available.
1210 foreach ( $plugin_group as $plugin_slug ) {
1211 $linked_plugins[] = $this->get_info_link( $plugin_slug );
1212 }
1213 unset( $plugin_slug );
1214
1215 $count = count( $plugin_group );
1216 $linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
1217 $last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
1218 $imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'wpdirectorykit' ) . ' ' . $last_plugin );
1219
1220 $rendered .= sprintf(
1221 $line_template,
1222 sprintf(
1223 translate_nooped_plural( $this->strings[ $type ], $count, 'wpdirectorykit' ),
1224 $imploded,
1225 $count
1226 )
1227 );
1228
1229 }
1230 unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
1231
1232 $rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
1233 }
1234
1235 // Register the nag messages and prepare them to be processed.
1236 add_settings_error( 'tgmpa', 'wpdirectorykit', $rendered, $this->get_admin_notice_class() );
1237 }
1238
1239 // Admin options pages already output settings_errors, so this is to avoid duplication.
1240 if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
1241 $this->display_settings_errors();
1242 }
1243 }
1244
1245 /**
1246 * Generate the user action links for the admin notice.
1247 *
1248 * @since 2.6.0
1249 *
1250 * @param int $install_count Number of plugins to install.
1251 * @param int $update_count Number of plugins to update.
1252 * @param int $activate_count Number of plugins to activate.
1253 * @param int $line_template Template for the HTML tag to output a line.
1254 * @return string Action links.
1255 */
1256 protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
1257 // Setup action links.
1258 $action_links = array(
1259 'install' => '',
1260 'update' => '',
1261 'activate' => '',
1262 '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>' : '',
1263 );
1264
1265 $link_template = '<a href="%2$s">%1$s</a>';
1266
1267 if ( current_user_can( 'install_plugins' ) ) {
1268 if ( $install_count > 0 ) {
1269 $action_links['install'] = sprintf(
1270 $link_template,
1271 translate_nooped_plural( $this->strings['install_link'], $install_count, 'wpdirectorykit' ),
1272 esc_url( $this->get_tgmpa_status_url( 'install' ) )
1273 );
1274 }
1275 if ( $update_count > 0 ) {
1276 $action_links['update'] = sprintf(
1277 $link_template,
1278 translate_nooped_plural( $this->strings['update_link'], $update_count, 'wpdirectorykit' ),
1279 esc_url( $this->get_tgmpa_status_url( 'update' ) )
1280 );
1281 }
1282 }
1283
1284 if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
1285 $action_links['activate'] = sprintf(
1286 $link_template,
1287 translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'wpdirectorykit' ),
1288 esc_url( $this->get_tgmpa_status_url( 'activate' ) )
1289 );
1290 }
1291
1292 $action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
1293
1294 $action_links = array_filter( (array) $action_links ); // Remove any empty array items.
1295
1296 if ( ! empty( $action_links ) ) {
1297 $action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
1298 return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
1299 } else {
1300 return '';
1301 }
1302 }
1303
1304 /**
1305 * Get admin notice class.
1306 *
1307 * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1308 * (lowest supported version by TGMPA).
1309 *
1310 * @since 2.6.0
1311 *
1312 * @return string
1313 */
1314 protected function get_admin_notice_class() {
1315 if ( ! empty( $this->strings['nag_type'] ) ) {
1316 return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1317 } else {
1318 if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1319 return 'notice-warning';
1320 } elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1321 return 'notice';
1322 } else {
1323 return 'updated';
1324 }
1325 }
1326 }
1327
1328 /**
1329 * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
1330 *
1331 * @since 2.5.0
1332 */
1333 protected function display_settings_errors() {
1334 global $wp_settings_errors;
1335
1336 settings_errors( 'tgmpa' );
1337
1338 foreach ( (array) $wp_settings_errors as $key => $details ) {
1339 if ( 'tgmpa' === $details['setting'] ) {
1340 unset( $wp_settings_errors[ $key ] );
1341 break;
1342 }
1343 }
1344 }
1345
1346 /**
1347 * Register dismissal of admin notices.
1348 *
1349 * Acts on the dismiss link in the admin nag messages.
1350 * If clicked, the admin notice disappears and will no longer be visible to this user.
1351 *
1352 * @since 2.1.0
1353 */
1354 public function dismiss() {
1355 if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) {
1356 update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 );
1357 }
1358 }
1359
1360 /**
1361 * Add individual plugin to our collection of plugins.
1362 *
1363 * If the required keys are not set or the plugin has already
1364 * been registered, the plugin is not added.
1365 *
1366 * @since 2.0.0
1367 *
1368 * @param array|null $plugin Array of plugin arguments or null if invalid argument.
1369 * @return null Return early if incorrect argument.
1370 */
1371 public function register( $plugin ) {
1372 if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) {
1373 return;
1374 }
1375
1376 if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
1377 return;
1378 }
1379
1380 $defaults = array(
1381 'name' => '', // String
1382 'slug' => '', // String
1383 'source' => 'repo', // String
1384 'required' => false, // Boolean
1385 'version' => '', // String
1386 'force_activation' => false, // Boolean
1387 'force_deactivation' => false, // Boolean
1388 'external_url' => '', // String
1389 'is_callable' => '', // String|Array.
1390 );
1391
1392 // Prepare the received data.
1393 $plugin = wp_parse_args( $plugin, $defaults );
1394
1395 // Standardize the received slug.
1396 $plugin['slug'] = $this->sanitize_key( $plugin['slug'] );
1397
1398 // Forgive users for using string versions of booleans or floats for version number.
1399 $plugin['version'] = (string) $plugin['version'];
1400 $plugin['source'] = empty( $plugin['source'] ) ? 'repo' : $plugin['source'];
1401 $plugin['required'] = TGMPA_Utils::validate_bool( $plugin['required'] );
1402 $plugin['force_activation'] = TGMPA_Utils::validate_bool( $plugin['force_activation'] );
1403 $plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] );
1404
1405 // Enrich the received data.
1406 $plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
1407 $plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] );
1408
1409 // Set the class properties.
1410 $this->plugins[ $plugin['slug'] ] = $plugin;
1411 $this->sort_order[ $plugin['slug'] ] = $plugin['name'];
1412
1413 // Should we add the force activation hook ?
1414 if ( true === $plugin['force_activation'] ) {
1415 $this->has_forced_activation = true;
1416 }
1417
1418 // Should we add the force deactivation hook ?
1419 if ( true === $plugin['force_deactivation'] ) {
1420 $this->has_forced_deactivation = true;
1421 }
1422 }
1423
1424 /**
1425 * Determine what type of source the plugin comes from.
1426 *
1427 * @since 2.5.0
1428 *
1429 * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path
1430 * (= bundled) or an external URL.
1431 * @return string 'repo', 'external', or 'bundled'
1432 */
1433 protected function get_plugin_source_type( $source ) {
1434 if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) {
1435 return 'repo';
1436 } elseif ( preg_match( self::IS_URL_REGEX, $source ) ) {
1437 return 'external';
1438 } else {
1439 return 'bundled';
1440 }
1441 }
1442
1443 /**
1444 * Sanitizes a string key.
1445 *
1446 * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are*
1447 * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase
1448 * characters in the plugin directory path/slug. Silly them.
1449 *
1450 * @see https://developer.wordpress.org/reference/hooks/sanitize_key/
1451 *
1452 * @since 2.5.0
1453 *
1454 * @param string $key String key.
1455 * @return string Sanitized key
1456 */
1457 public function sanitize_key( $key ) {
1458 $raw_key = $key;
1459 $key = preg_replace( '`[^A-Za-z0-9_-]`', '', $key );
1460
1461 /**
1462 * Filter a sanitized key string.
1463 *
1464 * @since 2.5.0
1465 *
1466 * @param string $key Sanitized key.
1467 * @param string $raw_key The key prior to sanitization.
1468 */
1469 return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key );
1470 }
1471
1472 /**
1473 * Amend default configuration settings.
1474 *
1475 * @since 2.0.0
1476 *
1477 * @param array $config Array of config options to pass as class properties.
1478 */
1479 public function config( $config ) {
1480 $keys = array(
1481 'id',
1482 'default_path',
1483 'has_notices',
1484 'dismissable',
1485 'dismiss_msg',
1486 'menu',
1487 'parent_slug',
1488 'capability',
1489 'is_automatic',
1490 'message',
1491 'strings',
1492 );
1493
1494 foreach ( $keys as $key ) {
1495 if ( isset( $config[ $key ] ) ) {
1496 if ( is_array( $config[ $key ] ) ) {
1497 $this->$key = array_merge( $this->$key, $config[ $key ] );
1498 } else {
1499 $this->$key = $config[ $key ];
1500 }
1501 }
1502 }
1503 }
1504
1505 /**
1506 * Amend action link after plugin installation.
1507 *
1508 * @since 2.0.0
1509 *
1510 * @param array $install_actions Existing array of actions.
1511 * @return false|array Amended array of actions.
1512 */
1513 public function actions( $install_actions ) {
1514 // Remove action links on the TGMPA install page.
1515 if ( $this->is_tgmpa_page() ) {
1516 return false;
1517 }
1518
1519 return $install_actions;
1520 }
1521
1522 /**
1523 * Flushes the plugins cache on theme switch to prevent stale entries
1524 * from remaining in the plugin table.
1525 *
1526 * @since 2.4.0
1527 *
1528 * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache.
1529 * Parameter added in v2.5.0.
1530 */
1531 public function flush_plugins_cache( $clear_update_cache = true ) {
1532 wp_clean_plugins_cache( $clear_update_cache );
1533 }
1534
1535 /**
1536 * Set file_path key for each installed plugin.
1537 *
1538 * @since 2.1.0
1539 *
1540 * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
1541 * Parameter added in v2.5.0.
1542 */
1543 public function populate_file_path( $plugin_slug = '' ) {
1544 if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) {
1545 $this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug );
1546 } else {
1547 // Add file_path key for all plugins.
1548 foreach ( $this->plugins as $slug => $values ) {
1549 $this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug );
1550 }
1551 }
1552 }
1553
1554 /**
1555 * Helper function to extract the file path of the plugin file from the
1556 * plugin slug, if the plugin is installed.
1557 *
1558 * @since 2.0.0
1559 *
1560 * @param string $slug Plugin slug (typically folder name) as provided by the developer.
1561 * @return string Either file path for plugin if installed, or just the plugin slug.
1562 */
1563 protected function _get_plugin_basename_from_slug( $slug ) {
1564 $keys = array_keys( $this->get_plugins() );
1565
1566 foreach ( $keys as $key ) {
1567 if ( preg_match( '|^' . $slug . '/|', $key ) ) {
1568 return $key;
1569 }
1570 }
1571
1572 return $slug;
1573 }
1574
1575 /**
1576 * Retrieve plugin data, given the plugin name.
1577 *
1578 * Loops through the registered plugins looking for $name. If it finds it,
1579 * it returns the $data from that plugin. Otherwise, returns false.
1580 *
1581 * @since 2.1.0
1582 *
1583 * @param string $name Name of the plugin, as it was registered.
1584 * @param string $data Optional. Array key of plugin data to return. Default is slug.
1585 * @return string|boolean Plugin slug if found, false otherwise.
1586 */
1587 public function _get_plugin_data_from_name( $name, $data = 'slug' ) {
1588 foreach ( $this->plugins as $values ) {
1589 if ( $name === $values['name'] && isset( $values[ $data ] ) ) {
1590 return $values[ $data ];
1591 }
1592 }
1593
1594 return false;
1595 }
1596
1597 /**
1598 * Retrieve the download URL for a package.
1599 *
1600 * @since 2.5.0
1601 *
1602 * @param string $slug Plugin slug.
1603 * @return string Plugin download URL or path to local file or empty string if undetermined.
1604 */
1605 public function get_download_url( $slug ) {
1606 $dl_source = '';
1607
1608 switch ( $this->plugins[ $slug ]['source_type'] ) {
1609 case 'repo':
1610 return $this->get_wp_repo_download_url( $slug );
1611 case 'external':
1612 return $this->plugins[ $slug ]['source'];
1613 case 'bundled':
1614 return $this->default_path . $this->plugins[ $slug ]['source'];
1615 }
1616
1617 return $dl_source; // Should never happen.
1618 }
1619
1620 /**
1621 * Retrieve the download URL for a WP repo package.
1622 *
1623 * @since 2.5.0
1624 *
1625 * @param string $slug Plugin slug.
1626 * @return string Plugin download URL.
1627 */
1628 protected function get_wp_repo_download_url( $slug ) {
1629 $source = '';
1630 $api = $this->get_plugins_api( $slug );
1631
1632 if ( false !== $api && isset( $api->download_link ) ) {
1633 $source = $api->download_link;
1634 }
1635
1636 return $source;
1637 }
1638
1639 /**
1640 * Try to grab information from WordPress API.
1641 *
1642 * @since 2.5.0
1643 *
1644 * @param string $slug Plugin slug.
1645 * @return object Plugins_api response object on success, WP_Error on failure.
1646 */
1647 protected function get_plugins_api( $slug ) {
1648 static $api = array(); // Cache received responses.
1649
1650 if ( ! isset( $api[ $slug ] ) ) {
1651 if ( ! function_exists( 'plugins_api' ) ) {
1652 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
1653 }
1654
1655 $response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
1656
1657 $api[ $slug ] = false;
1658
1659 if ( is_wp_error( $response ) ) {
1660 wp_die( esc_html( $this->strings['oops'] ) );
1661 } else {
1662 $api[ $slug ] = $response;
1663 }
1664 }
1665
1666 return $api[ $slug ];
1667 }
1668
1669 /**
1670 * Retrieve a link to a plugin information page.
1671 *
1672 * @since 2.5.0
1673 *
1674 * @param string $slug Plugin slug.
1675 * @return string Fully formed html link to a plugin information page if available
1676 * or the plugin name if not.
1677 */
1678 public function get_info_link( $slug ) {
1679 if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) {
1680 $link = sprintf(
1681 '<a href="%1$s" target="_blank">%2$s</a>',
1682 esc_url( $this->plugins[ $slug ]['external_url'] ),
1683 esc_html( $this->plugins[ $slug ]['name'] )
1684 );
1685 } elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) {
1686 $url = add_query_arg(
1687 array(
1688 'tab' => 'plugin-information',
1689 'plugin' => urlencode( $slug ),
1690 'TB_iframe' => 'true',
1691 'width' => '640',
1692 'height' => '500',
1693 ),
1694 self_admin_url( 'plugin-install.php' )
1695 );
1696
1697 $link = sprintf(
1698 '<a href="%1$s" class="thickbox">%2$s</a>',
1699 esc_url( $url ),
1700 esc_html( $this->plugins[ $slug ]['name'] )
1701 );
1702 } else {
1703 $link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink.
1704 }
1705
1706 return $link;
1707 }
1708
1709 /**
1710 * Determine if we're on the TGMPA Install page.
1711 *
1712 * @since 2.1.0
1713 *
1714 * @return boolean True when on the TGMPA page, false otherwise.
1715 */
1716 protected function is_tgmpa_page() {
1717 return isset( $_GET['page'] ) && $this->menu === $_GET['page'];
1718 }
1719
1720 /**
1721 * Determine if we're on a WP Core installation/upgrade page.
1722 *
1723 * @since 2.6.0
1724 *
1725 * @return boolean True when on a WP Core installation/upgrade page, false otherwise.
1726 */
1727 protected function is_core_update_page() {
1728 // Current screen is not always available, most notably on the customizer screen.
1729 if ( ! function_exists( 'get_current_screen' ) ) {
1730 return false;
1731 }
1732
1733 $screen = get_current_screen();
1734
1735 if ( 'update-core' === $screen->base ) {
1736 // Core update screen.
1737 return true;
1738 } elseif ( 'plugins' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
1739 // Plugins bulk update screen.
1740 return true;
1741 } elseif ( 'update' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
1742 // Individual updates (ajax call).
1743 return true;
1744 }
1745
1746 return false;
1747 }
1748
1749 /**
1750 * Retrieve the URL to the TGMPA Install page.
1751 *
1752 * I.e. depending on the config settings passed something along the lines of:
1753 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins
1754 *
1755 * @since 2.5.0
1756 *
1757 * @return string Properly encoded URL (not escaped).
1758 */
1759 public function get_tgmpa_url() {
1760 static $url;
1761
1762 if ( ! isset( $url ) ) {
1763 $parent = $this->parent_slug;
1764 if ( false === strpos( $parent, '.php' ) ) {
1765 $parent = 'admin.php';
1766 }
1767 $url = add_query_arg(
1768 array(
1769 'page' => urlencode( $this->menu ),
1770 ),
1771 self_admin_url( $parent )
1772 );
1773 }
1774
1775 return $url;
1776 }
1777
1778 /**
1779 * Retrieve the URL to the TGMPA Install page for a specific plugin status (view).
1780 *
1781 * I.e. depending on the config settings passed something along the lines of:
1782 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install
1783 *
1784 * @since 2.5.0
1785 *
1786 * @param string $status Plugin status - either 'install', 'update' or 'activate'.
1787 * @return string Properly encoded URL (not escaped).
1788 */
1789 public function get_tgmpa_status_url( $status ) {
1790 return add_query_arg(
1791 array(
1792 'plugin_status' => urlencode( $status ),
1793 ),
1794 $this->get_tgmpa_url()
1795 );
1796 }
1797
1798 /**
1799 * Determine whether there are open actions for plugins registered with TGMPA.
1800 *
1801 * @since 2.5.0
1802 *
1803 * @return bool True if complete, i.e. no outstanding actions. False otherwise.
1804 */
1805 public function is_tgmpa_complete() {
1806 $complete = true;
1807 foreach ( $this->plugins as $slug => $plugin ) {
1808 if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1809 $complete = false;
1810 break;
1811 }
1812 }
1813
1814 return $complete;
1815 }
1816
1817 /**
1818 * Check if a plugin is installed. Does not take must-use plugins into account.
1819 *
1820 * @since 2.5.0
1821 *
1822 * @param string $slug Plugin slug.
1823 * @return bool True if installed, false otherwise.
1824 */
1825 public function is_plugin_installed( $slug ) {
1826 $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1827
1828 return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) );
1829 }
1830
1831 /**
1832 * Check if a plugin is active.
1833 *
1834 * @since 2.5.0
1835 *
1836 * @param string $slug Plugin slug.
1837 * @return bool True if active, false otherwise.
1838 */
1839 public function is_plugin_active( $slug ) {
1840 return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) );
1841 }
1842
1843 /**
1844 * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required
1845 * available, check whether the current install meets them.
1846 *
1847 * @since 2.5.0
1848 *
1849 * @param string $slug Plugin slug.
1850 * @return bool True if OK to update, false otherwise.
1851 */
1852 public function can_plugin_update( $slug ) {
1853 // We currently can't get reliable info on non-WP-repo plugins - issue #380.
1854 if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1855 return true;
1856 }
1857
1858 $api = $this->get_plugins_api( $slug );
1859
1860 if ( false !== $api && isset( $api->requires ) ) {
1861 return version_compare( $this->wp_version, $api->requires, '>=' );
1862 }
1863
1864 // No usable info received from the plugins API, presume we can update.
1865 return true;
1866 }
1867
1868 /**
1869 * Check to see if the plugin is 'updatetable', i.e. installed, with an update available
1870 * and no WP version requirements blocking it.
1871 *
1872 * @since 2.6.0
1873 *
1874 * @param string $slug Plugin slug.
1875 * @return bool True if OK to proceed with update, false otherwise.
1876 */
1877 public function is_plugin_updatetable( $slug ) {
1878 if ( ! $this->is_plugin_installed( $slug ) ) {
1879 return false;
1880 } else {
1881 return ( false !== $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) );
1882 }
1883 }
1884
1885 /**
1886 * Check if a plugin can be activated, i.e. is not currently active and meets the minimum
1887 * plugin version requirements set in TGMPA (if any).
1888 *
1889 * @since 2.5.0
1890 *
1891 * @param string $slug Plugin slug.
1892 * @return bool True if OK to activate, false otherwise.
1893 */
1894 public function can_plugin_activate( $slug ) {
1895 return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) );
1896 }
1897
1898 /**
1899 * Retrieve the version number of an installed plugin.
1900 *
1901 * @since 2.5.0
1902 *
1903 * @param string $slug Plugin slug.
1904 * @return string Version number as string or an empty string if the plugin is not installed
1905 * or version unknown (plugins which don't comply with the plugin header standard).
1906 */
1907 public function get_installed_version( $slug ) {
1908 $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1909
1910 if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) {
1911 return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'];
1912 }
1913
1914 return '';
1915 }
1916
1917 /**
1918 * Check whether a plugin complies with the minimum version requirements.
1919 *
1920 * @since 2.5.0
1921 *
1922 * @param string $slug Plugin slug.
1923 * @return bool True when a plugin needs to be updated, otherwise false.
1924 */
1925 public function does_plugin_require_update( $slug ) {
1926 $installed_version = $this->get_installed_version( $slug );
1927 $minimum_version = $this->plugins[ $slug ]['version'];
1928
1929 return version_compare( $minimum_version, $installed_version, '>' );
1930 }
1931
1932 /**
1933 * Check whether there is an update available for a plugin.
1934 *
1935 * @since 2.5.0
1936 *
1937 * @param string $slug Plugin slug.
1938 * @return false|string Version number string of the available update or false if no update available.
1939 */
1940 public function does_plugin_have_update( $slug ) {
1941 // Presume bundled and external plugins will point to a package which meets the minimum required version.
1942 if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1943 if ( $this->does_plugin_require_update( $slug ) ) {
1944 return $this->plugins[ $slug ]['version'];
1945 }
1946
1947 return false;
1948 }
1949
1950 $repo_updates = get_site_transient( 'update_plugins' );
1951
1952 if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) {
1953 return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version;
1954 }
1955
1956 return false;
1957 }
1958
1959 /**
1960 * Retrieve potential upgrade notice for a plugin.
1961 *
1962 * @since 2.5.0
1963 *
1964 * @param string $slug Plugin slug.
1965 * @return string The upgrade notice or an empty string if no message was available or provided.
1966 */
1967 public function get_upgrade_notice( $slug ) {
1968 // We currently can't get reliable info on non-WP-repo plugins - issue #380.
1969 if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1970 return '';
1971 }
1972
1973 $repo_updates = get_site_transient( 'update_plugins' );
1974
1975 if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) {
1976 return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice;
1977 }
1978
1979 return '';
1980 }
1981
1982 /**
1983 * Wrapper around the core WP get_plugins function, making sure it's actually available.
1984 *
1985 * @since 2.5.0
1986 *
1987 * @param string $plugin_folder Optional. Relative path to single plugin folder.
1988 * @return array Array of installed plugins with plugin information.
1989 */
1990 public function get_plugins( $plugin_folder = '' ) {
1991 if ( ! function_exists( 'get_plugins' ) ) {
1992 require_once ABSPATH . 'wp-admin/includes/plugin.php';
1993 }
1994
1995 return get_plugins( $plugin_folder );
1996 }
1997
1998 /**
1999 * Delete dismissable nag option when theme is switched.
2000 *
2001 * This ensures that the user(s) is/are again reminded via nag of required
2002 * and/or recommended plugins if they re-activate the theme.
2003 *
2004 * @since 2.1.1
2005 */
2006 public function update_dismiss() {
2007 delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true );
2008 }
2009
2010 /**
2011 * Forces plugin activation if the parameter 'force_activation' is
2012 * set to true.
2013 *
2014 * This allows theme authors to specify certain plugins that must be
2015 * active at all times while using the current theme.
2016 *
2017 * Please take special care when using this parameter as it has the
2018 * potential to be harmful if not used correctly. Setting this parameter
2019 * to true will not allow the specified plugin to be deactivated unless
2020 * the user switches themes.
2021 *
2022 * @since 2.2.0
2023 */
2024 public function force_activation() {
2025 foreach ( $this->plugins as $slug => $plugin ) {
2026 if ( true === $plugin['force_activation'] ) {
2027 if ( ! $this->is_plugin_installed( $slug ) ) {
2028 // Oops, plugin isn't there so iterate to next condition.
2029 continue;
2030 } elseif ( $this->can_plugin_activate( $slug ) ) {
2031 // There we go, activate the plugin.
2032 activate_plugin( $plugin['file_path'] );
2033 }
2034 }
2035 }
2036 }
2037
2038 /**
2039 * Forces plugin deactivation if the parameter 'force_deactivation'
2040 * is set to true and adds the plugin to the 'recently active' plugins list.
2041 *
2042 * This allows theme authors to specify certain plugins that must be
2043 * deactivated upon switching from the current theme to another.
2044 *
2045 * Please take special care when using this parameter as it has the
2046 * potential to be harmful if not used correctly.
2047 *
2048 * @since 2.2.0
2049 */
2050 public function force_deactivation() {
2051 $deactivated = array();
2052
2053 foreach ( $this->plugins as $slug => $plugin ) {
2054 /*
2055 * Only proceed forward if the parameter is set to true and plugin is active
2056 * as a 'normal' (not must-use) plugin.
2057 */
2058 if ( true === $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) ) {
2059 deactivate_plugins( $plugin['file_path'] );
2060 $deactivated[ $plugin['file_path'] ] = time();
2061 }
2062 }
2063
2064 if ( ! empty( $deactivated ) ) {
2065 update_option( 'recently_activated', $deactivated + (array) get_option( 'recently_activated' ) );
2066 }
2067 }
2068
2069 /**
2070 * Echo the current TGMPA version number to the page.
2071 *
2072 * @since 2.5.0
2073 */
2074 public function show_tgmpa_version() {
2075 echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',
2076 esc_html(
2077 sprintf(
2078 /* translators: %s: version number */
2079 __( 'TGMPA v%s', 'wpdirectorykit' ),
2080 self::TGMPA_VERSION
2081 )
2082 ),
2083 '</small></strong></p>';
2084 }
2085
2086 /**
2087 * Returns the singleton instance of the class.
2088 *
2089 * @since 2.4.0
2090 *
2091 * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object.
2092 */
2093 public static function get_instance() {
2094 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
2095 self::$instance = new self();
2096 }
2097
2098 return self::$instance;
2099 }
2100 }
2101
2102 if ( ! function_exists( 'load_tgm_plugin_activation' ) ) {
2103 /**
2104 * Ensure only one instance of the class is ever invoked.
2105 *
2106 * @since 2.5.0
2107 */
2108 function load_tgm_plugin_activation() {
2109 $GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance();
2110 }
2111 }
2112
2113 if ( did_action( 'plugins_loaded' ) ) {
2114 load_tgm_plugin_activation();
2115 } else {
2116 add_action( 'plugins_loaded', 'load_tgm_plugin_activation' );
2117 }
2118 }
2119
2120 if ( ! function_exists( 'tgmpa' ) ) {
2121 /**
2122 * Helper function to register a collection of required plugins.
2123 *
2124 * @since 2.0.0
2125 * @api
2126 *
2127 * @param array $plugins An array of plugin arrays.
2128 * @param array $config Optional. An array of configuration values.
2129 */
2130 function tgmpa( $plugins, $config = array() ) {
2131 $instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2132
2133 foreach ( $plugins as $plugin ) {
2134 call_user_func( array( $instance, 'register' ), $plugin );
2135 }
2136
2137 if ( ! empty( $config ) && is_array( $config ) ) {
2138 // Send out notices for deprecated arguments passed.
2139 if ( isset( $config['notices'] ) ) {
2140 _deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' );
2141 if ( ! isset( $config['has_notices'] ) ) {
2142 $config['has_notices'] = $config['notices'];
2143 }
2144 }
2145
2146 if ( isset( $config['parent_menu_slug'] ) ) {
2147 _deprecated_argument( __FUNCTION__, '2.4.0', 'The `parent_menu_slug` config parameter was removed in TGMPA 2.4.0. In TGMPA 2.5.0 an alternative was (re-)introduced. Please adjust your configuration. For more information visit the website: http://tgmpluginactivation.com/configuration/#h-configuration-options.' );
2148 }
2149 if ( isset( $config['parent_url_slug'] ) ) {
2150 _deprecated_argument( __FUNCTION__, '2.4.0', 'The `parent_url_slug` config parameter was removed in TGMPA 2.4.0. In TGMPA 2.5.0 an alternative was (re-)introduced. Please adjust your configuration. For more information visit the website: http://tgmpluginactivation.com/configuration/#h-configuration-options.' );
2151 }
2152
2153 call_user_func( array( $instance, 'config' ), $config );
2154 }
2155 }
2156 }
2157
2158 /**
2159 * WP_List_Table isn't always available. If it isn't available,
2160 * we load it here.
2161 *
2162 * @since 2.2.0
2163 */
2164 if ( ! class_exists( 'WP_List_Table' ) ) {
2165 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
2166 }
2167
2168 if ( ! class_exists( 'TGMPA_List_Table' ) ) {
2169
2170 /**
2171 * List table class for handling plugins.
2172 *
2173 * Extends the WP_List_Table class to provide a future-compatible
2174 * way of listing out all required/recommended plugins.
2175 *
2176 * Gives users an interface similar to the Plugin Administration
2177 * area with similar (albeit stripped down) capabilities.
2178 *
2179 * This class also allows for the bulk install of plugins.
2180 *
2181 * @since 2.2.0
2182 *
2183 * @package TGM-Plugin-Activation
2184 * @author Thomas Griffin
2185 * @author Gary Jones
2186 */
2187 class TGMPA_List_Table extends WP_List_Table {
2188 /**
2189 * TGMPA instance.
2190 *
2191 * @since 2.5.0
2192 *
2193 * @var object
2194 */
2195 protected $tgmpa;
2196
2197 /**
2198 * The currently chosen view.
2199 *
2200 * @since 2.5.0
2201 *
2202 * @var string One of: 'all', 'install', 'update', 'activate'
2203 */
2204 public $view_context = 'all';
2205
2206 /**
2207 * The plugin counts for the various views.
2208 *
2209 * @since 2.5.0
2210 *
2211 * @var array
2212 */
2213 protected $view_totals = array(
2214 'all' => 0,
2215 'install' => 0,
2216 'update' => 0,
2217 'activate' => 0,
2218 );
2219
2220 /**
2221 * References parent constructor and sets defaults for class.
2222 *
2223 * @since 2.2.0
2224 */
2225 public function __construct() {
2226 $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2227
2228 parent::__construct(
2229 array(
2230 'singular' => 'plugin',
2231 'plural' => 'plugins',
2232 'ajax' => false,
2233 )
2234 );
2235
2236 if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) {
2237 $this->view_context = sanitize_key( $_REQUEST['plugin_status'] );
2238 }
2239
2240 add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) );
2241 }
2242
2243 /**
2244 * Get a list of CSS classes for the <table> tag.
2245 *
2246 * Overruled to prevent the 'plural' argument from being added.
2247 *
2248 * @since 2.5.0
2249 *
2250 * @return array CSS classnames.
2251 */
2252 public function get_table_classes() {
2253 return array( 'widefat', 'fixed' );
2254 }
2255
2256 /**
2257 * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table.
2258 *
2259 * @since 2.2.0
2260 *
2261 * @return array $table_data Information for use in table.
2262 */
2263 protected function _gather_plugin_data() {
2264 // Load thickbox for plugin links.
2265 $this->tgmpa->admin_init();
2266 $this->tgmpa->thickbox();
2267
2268 // Categorize the plugins which have open actions.
2269 $plugins = $this->categorize_plugins_to_views();
2270
2271 // Set the counts for the view links.
2272 $this->set_view_totals( $plugins );
2273
2274 // Prep variables for use and grab list of all installed plugins.
2275 $table_data = array();
2276 $i = 0;
2277
2278 // Redirect to the 'all' view if no plugins were found for the selected view context.
2279 if ( empty( $plugins[ $this->view_context ] ) ) {
2280 $this->view_context = 'all';
2281 }
2282
2283 foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) {
2284 $table_data[ $i ]['sanitized_plugin'] = $plugin['name'];
2285 $table_data[ $i ]['slug'] = $slug;
2286 $table_data[ $i ]['plugin'] = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>';
2287 $table_data[ $i ]['source'] = $this->get_plugin_source_type_text( $plugin['source_type'] );
2288 $table_data[ $i ]['type'] = $this->get_plugin_advise_type_text( $plugin['required'] );
2289 $table_data[ $i ]['status'] = $this->get_plugin_status_text( $slug );
2290 $table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug );
2291 $table_data[ $i ]['minimum_version'] = $plugin['version'];
2292 $table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug );
2293
2294 // Prep the upgrade notice info.
2295 $upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug );
2296 if ( ! empty( $upgrade_notice ) ) {
2297 $table_data[ $i ]['upgrade_notice'] = $upgrade_notice;
2298
2299 add_action( "tgmpa_after_plugin_row_{$slug}", array( $this, 'wp_plugin_update_row' ), 10, 2 );
2300 }
2301
2302 $table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin );
2303
2304 $i++;
2305 }
2306
2307 return $table_data;
2308 }
2309
2310 /**
2311 * Categorize the plugins which have open actions into views for the TGMPA page.
2312 *
2313 * @since 2.5.0
2314 */
2315 protected function categorize_plugins_to_views() {
2316 $plugins = array(
2317 'all' => array(), // Meaning: all plugins which still have open actions.
2318 'install' => array(),
2319 'update' => array(),
2320 'activate' => array(),
2321 );
2322
2323 foreach ( $this->tgmpa->plugins as $slug => $plugin ) {
2324 if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2325 // No need to display plugins if they are installed, up-to-date and active.
2326 continue;
2327 } else {
2328 $plugins['all'][ $slug ] = $plugin;
2329
2330 if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2331 $plugins['install'][ $slug ] = $plugin;
2332 } else {
2333 if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2334 $plugins['update'][ $slug ] = $plugin;
2335 }
2336
2337 if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2338 $plugins['activate'][ $slug ] = $plugin;
2339 }
2340 }
2341 }
2342 }
2343
2344 return $plugins;
2345 }
2346
2347 /**
2348 * Set the counts for the view links.
2349 *
2350 * @since 2.5.0
2351 *
2352 * @param array $plugins Plugins order by view.
2353 */
2354 protected function set_view_totals( $plugins ) {
2355 foreach ( $plugins as $type => $list ) {
2356 $this->view_totals[ $type ] = count( $list );
2357 }
2358 }
2359
2360 /**
2361 * Get the plugin required/recommended text string.
2362 *
2363 * @since 2.5.0
2364 *
2365 * @param string $required Plugin required setting.
2366 * @return string
2367 */
2368 protected function get_plugin_advise_type_text( $required ) {
2369 if ( true === $required ) {
2370 return __( 'Required', 'wpdirectorykit' );
2371 }
2372
2373 return __( 'Recommended', 'wpdirectorykit' );
2374 }
2375
2376 /**
2377 * Get the plugin source type text string.
2378 *
2379 * @since 2.5.0
2380 *
2381 * @param string $type Plugin type.
2382 * @return string
2383 */
2384 protected function get_plugin_source_type_text( $type ) {
2385 $string = '';
2386
2387 switch ( $type ) {
2388 case 'repo':
2389 $string = __( 'WordPress Repository', 'wpdirectorykit' );
2390 break;
2391 case 'external':
2392 $string = __( 'External Source', 'wpdirectorykit' );
2393 break;
2394 case 'bundled':
2395 $string = __( 'Pre-Packaged', 'wpdirectorykit' );
2396 break;
2397 }
2398
2399 return $string;
2400 }
2401
2402 /**
2403 * Determine the plugin status message.
2404 *
2405 * @since 2.5.0
2406 *
2407 * @param string $slug Plugin slug.
2408 * @return string
2409 */
2410 protected function get_plugin_status_text( $slug ) {
2411 if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2412 return __( 'Not Installed', 'wpdirectorykit' );
2413 }
2414
2415 if ( ! $this->tgmpa->is_plugin_active( $slug ) ) {
2416 $install_status = __( 'Installed But Not Activated', 'wpdirectorykit' );
2417 } else {
2418 $install_status = __( 'Active', 'wpdirectorykit' );
2419 }
2420
2421 $update_status = '';
2422
2423 if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2424 $update_status = __( 'Required Update not Available', 'wpdirectorykit' );
2425
2426 } elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) {
2427 $update_status = __( 'Requires Update', 'wpdirectorykit' );
2428
2429 } elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2430 $update_status = __( 'Update recommended', 'wpdirectorykit' );
2431 }
2432
2433 if ( '' === $update_status ) {
2434 return $install_status;
2435 }
2436
2437 return sprintf(
2438 /* translators: 1: install status, 2: update status */
2439 _x( '%1$s, %2$s', 'Install/Update Status', 'wpdirectorykit' ),
2440 $install_status,
2441 $update_status
2442 );
2443 }
2444
2445 /**
2446 * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type.
2447 *
2448 * @since 2.5.0
2449 *
2450 * @param array $items Prepared table items.
2451 * @return array Sorted table items.
2452 */
2453 public function sort_table_items( $items ) {
2454 $type = array();
2455 $name = array();
2456
2457 foreach ( $items as $i => $plugin ) {
2458 $type[ $i ] = $plugin['type']; // Required / recommended.
2459 $name[ $i ] = $plugin['sanitized_plugin'];
2460 }
2461
2462 array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items );
2463
2464 return $items;
2465 }
2466
2467 /**
2468 * Get an associative array ( id => link ) of the views available on this table.
2469 *
2470 * @since 2.5.0
2471 *
2472 * @return array
2473 */
2474 public function get_views() {
2475 $status_links = array();
2476
2477 foreach ( $this->view_totals as $type => $count ) {
2478 if ( $count < 1 ) {
2479 continue;
2480 }
2481
2482 switch ( $type ) {
2483 case 'all':
2484 /* translators: 1: number of plugins. */
2485 $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'wpdirectorykit' );
2486 break;
2487 case 'install':
2488 /* translators: 1: number of plugins. */
2489 $text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'wpdirectorykit' );
2490 break;
2491 case 'update':
2492 /* translators: 1: number of plugins. */
2493 $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'wpdirectorykit' );
2494 break;
2495 case 'activate':
2496 /* translators: 1: number of plugins. */
2497 $text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'wpdirectorykit' );
2498 break;
2499 default:
2500 $text = '';
2501 break;
2502 }
2503
2504 if ( ! empty( $text ) ) {
2505
2506 $status_links[ $type ] = sprintf(
2507 '<a href="%s"%s>%s</a>',
2508 esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
2509 ( $type === $this->view_context ) ? ' class="current"' : '',
2510 sprintf( $text, number_format_i18n( $count ) )
2511 );
2512 }
2513 }
2514
2515 return $status_links;
2516 }
2517
2518 /**
2519 * Create default columns to display important plugin information
2520 * like type, action and status.
2521 *
2522 * @since 2.2.0
2523 *
2524 * @param array $item Array of item data.
2525 * @param string $column_name The name of the column.
2526 * @return string
2527 */
2528 public function column_default( $item, $column_name ) {
2529 return $item[ $column_name ];
2530 }
2531
2532 /**
2533 * Required for bulk installing.
2534 *
2535 * Adds a checkbox for each plugin.
2536 *
2537 * @since 2.2.0
2538 *
2539 * @param array $item Array of item data.
2540 * @return string The input checkbox with all necessary info.
2541 */
2542 public function column_cb( $item ) {
2543 return sprintf(
2544 '<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />',
2545 esc_attr( $this->_args['singular'] ),
2546 esc_attr( $item['slug'] ),
2547 esc_attr( $item['sanitized_plugin'] )
2548 );
2549 }
2550
2551 /**
2552 * Create default title column along with the action links.
2553 *
2554 * @since 2.2.0
2555 *
2556 * @param array $item Array of item data.
2557 * @return string The plugin name and action links.
2558 */
2559 public function column_plugin( $item ) {
2560 return sprintf(
2561 '%1$s %2$s',
2562 $item['plugin'],
2563 $this->row_actions( $this->get_row_actions( $item ), true )
2564 );
2565 }
2566
2567 /**
2568 * Create version information column.
2569 *
2570 * @since 2.5.0
2571 *
2572 * @param array $item Array of item data.
2573 * @return string HTML-formatted version information.
2574 */
2575 public function column_version( $item ) {
2576 $output = array();
2577
2578 if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2579 $installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'wpdirectorykit' );
2580
2581 $color = '';
2582 if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) {
2583 $color = ' color: #ff0000; font-weight: bold;';
2584 }
2585
2586 $output[] = sprintf(
2587 '<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'wpdirectorykit' ) . '</p>',
2588 $color,
2589 $installed
2590 );
2591 }
2592
2593 if ( ! empty( $item['minimum_version'] ) ) {
2594 $output[] = sprintf(
2595 '<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'wpdirectorykit' ) . '</p>',
2596 $item['minimum_version']
2597 );
2598 }
2599
2600 if ( ! empty( $item['available_version'] ) ) {
2601 $color = '';
2602 if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) {
2603 $color = ' color: #71C671; font-weight: bold;';
2604 }
2605
2606 $output[] = sprintf(
2607 '<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'wpdirectorykit' ) . '</p>',
2608 $color,
2609 $item['available_version']
2610 );
2611 }
2612
2613 if ( empty( $output ) ) {
2614 return '&nbsp;'; // Let's not break the table layout.
2615 } else {
2616 return implode( "\n", $output );
2617 }
2618 }
2619
2620 /**
2621 * Sets default message within the plugins table if no plugins
2622 * are left for interaction.
2623 *
2624 * Hides the menu item to prevent the user from clicking and
2625 * getting a permissions error.
2626 *
2627 * @since 2.2.0
2628 */
2629 public function no_items() {
2630 echo esc_html__( 'No plugins to install, update or activate.', 'wpdirectorykit' ) . ' <a href="' . esc_url( self_admin_url() ) . '"> ' . esc_html__( 'Return to the Dashboard', 'wpdirectorykit' ) . '</a>';
2631 echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
2632 }
2633
2634 /**
2635 * Output all the column information within the table.
2636 *
2637 * @since 2.2.0
2638 *
2639 * @return array $columns The column names.
2640 */
2641 public function get_columns() {
2642 $columns = array(
2643 'cb' => '<input type="checkbox" />',
2644 'plugin' => __( 'Plugin', 'wpdirectorykit' ),
2645 'source' => __( 'Source', 'wpdirectorykit' ),
2646 'type' => __( 'Type', 'wpdirectorykit' ),
2647 );
2648
2649 if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
2650 $columns['version'] = __( 'Version', 'wpdirectorykit' );
2651 $columns['status'] = __( 'Status', 'wpdirectorykit' );
2652 }
2653
2654 return apply_filters( 'tgmpa_table_columns', $columns );
2655 }
2656
2657 /**
2658 * Get name of default primary column
2659 *
2660 * @since 2.5.0 / WP 4.3+ compatibility
2661 * @access protected
2662 *
2663 * @return string
2664 */
2665 protected function get_default_primary_column_name() {
2666 return 'plugin';
2667 }
2668
2669 /**
2670 * Get the name of the primary column.
2671 *
2672 * @since 2.5.0 / WP 4.3+ compatibility
2673 * @access protected
2674 *
2675 * @return string The name of the primary column.
2676 */
2677 protected function get_primary_column_name() {
2678 if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) {
2679 return parent::get_primary_column_name();
2680 } else {
2681 return $this->get_default_primary_column_name();
2682 }
2683 }
2684
2685 /**
2686 * Get the actions which are relevant for a specific plugin row.
2687 *
2688 * @since 2.5.0
2689 *
2690 * @param array $item Array of item data.
2691 * @return array Array with relevant action links.
2692 */
2693 protected function get_row_actions( $item ) {
2694 $actions = array();
2695 $action_links = array();
2696
2697 // Display the 'Install' action link if the plugin is not yet available.
2698 if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2699 /* translators: %2$s: plugin name in screen reader markup */
2700 $actions['install'] = __( 'Install %2$s', 'wpdirectorykit' );
2701 } else {
2702 // Display the 'Update' action link if an update is available and WP complies with plugin minimum.
2703 if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) {
2704 /* translators: %2$s: plugin name in screen reader markup */
2705 $actions['update'] = __( 'Update %2$s', 'wpdirectorykit' );
2706 }
2707
2708 // Display the 'Activate' action link, but only if the plugin meets the minimum version.
2709 if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) {
2710 /* translators: %2$s: plugin name in screen reader markup */
2711 $actions['activate'] = __( 'Activate %2$s', 'wpdirectorykit' );
2712 }
2713 }
2714
2715 // Create the actual links.
2716 foreach ( $actions as $action => $text ) {
2717 $nonce_url = wp_nonce_url(
2718 add_query_arg(
2719 array(
2720 'plugin' => urlencode( $item['slug'] ),
2721 'tgmpa-' . $action => $action . '-plugin',
2722 ),
2723 $this->tgmpa->get_tgmpa_url()
2724 ),
2725 'tgmpa-' . $action,
2726 'tgmpa-nonce'
2727 );
2728
2729 $action_links[ $action ] = sprintf(
2730 '<a href="%1$s">' . esc_html( $text ) . '</a>', // $text contains the second placeholder.
2731 esc_url( $nonce_url ),
2732 '<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>'
2733 );
2734 }
2735
2736 $prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : '';
2737 return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context );
2738 }
2739
2740 /**
2741 * Generates content for a single row of the table.
2742 *
2743 * @since 2.5.0
2744 *
2745 * @param object $item The current item.
2746 */
2747 public function single_row( $item ) {
2748 parent::single_row( $item );
2749
2750 /**
2751 * Fires after each specific row in the TGMPA Plugins list table.
2752 *
2753 * The dynamic portion of the hook name, `$item['slug']`, refers to the slug
2754 * for the plugin.
2755 *
2756 * @since 2.5.0
2757 */
2758 do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context );
2759 }
2760
2761 /**
2762 * Show the upgrade notice below a plugin row if there is one.
2763 *
2764 * @since 2.5.0
2765 *
2766 * @see /wp-admin/includes/update.php
2767 *
2768 * @param string $slug Plugin slug.
2769 * @param array $item The information available in this table row.
2770 * @return null Return early if upgrade notice is empty.
2771 */
2772 public function wp_plugin_update_row( $slug, $item ) {
2773 if ( empty( $item['upgrade_notice'] ) ) {
2774 return;
2775 }
2776
2777 echo '
2778 <tr class="plugin-update-tr">
2779 <td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange">
2780 <div class="update-message">',
2781 esc_html__( 'Upgrade message from the plugin author:', 'wpdirectorykit' ),
2782 ' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong>
2783 </div>
2784 </td>
2785 </tr>';
2786 }
2787
2788 /**
2789 * Extra controls to be displayed between bulk actions and pagination.
2790 *
2791 * @since 2.5.0
2792 *
2793 * @param string $which 'top' or 'bottom' table navigation.
2794 */
2795 public function extra_tablenav( $which ) {
2796 if ( 'bottom' === $which ) {
2797 $this->tgmpa->show_tgmpa_version();
2798 }
2799 }
2800
2801 /**
2802 * Defines the bulk actions for handling registered plugins.
2803 *
2804 * @since 2.2.0
2805 *
2806 * @return array $actions The bulk actions for the plugin install table.
2807 */
2808 public function get_bulk_actions() {
2809
2810 $actions = array();
2811
2812 if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) {
2813 if ( current_user_can( 'install_plugins' ) ) {
2814 $actions['tgmpa-bulk-install'] = __( 'Install', 'wpdirectorykit' );
2815 }
2816 }
2817
2818 if ( 'install' !== $this->view_context ) {
2819 if ( current_user_can( 'update_plugins' ) ) {
2820 $actions['tgmpa-bulk-update'] = __( 'Update', 'wpdirectorykit' );
2821 }
2822 if ( current_user_can( 'activate_plugins' ) ) {
2823 $actions['tgmpa-bulk-activate'] = __( 'Activate', 'wpdirectorykit' );
2824 }
2825 }
2826
2827 return $actions;
2828 }
2829
2830 /**
2831 * Processes bulk installation and activation actions.
2832 *
2833 * The bulk installation process looks for the $_POST information and passes that
2834 * through if a user has to use WP_Filesystem to enter their credentials.
2835 *
2836 * @since 2.2.0
2837 */
2838 public function process_bulk_actions() {
2839 // Bulk installation process.
2840 if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) {
2841
2842 check_admin_referer( 'bulk-' . $this->_args['plural'] );
2843
2844 $install_type = 'install';
2845 if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2846 $install_type = 'update';
2847 }
2848
2849 $plugins_to_install = array();
2850
2851 // Did user actually select any plugins to install/update ?
2852 if ( empty( $_POST['plugin'] ) ) {
2853 if ( 'install' === $install_type ) {
2854 $message = __( 'No plugins were selected to be installed. No action taken.', 'wpdirectorykit' );
2855 } else {
2856 $message = __( 'No plugins were selected to be updated. No action taken.', 'wpdirectorykit' );
2857 }
2858
2859 echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2860
2861 return false;
2862 }
2863
2864 if ( is_array( $_POST['plugin'] ) ) {
2865 $plugins_to_install = (array) $_POST['plugin'];
2866 } elseif ( is_string( $_POST['plugin'] ) ) {
2867 // Received via Filesystem page - un-flatten array (WP bug #19643).
2868 $plugins_to_install = explode( ',', $_POST['plugin'] );
2869 }
2870
2871 // Sanitize the received input.
2872 $plugins_to_install = array_map( 'urldecode', $plugins_to_install );
2873 $plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install );
2874
2875 // Validate the received input.
2876 foreach ( $plugins_to_install as $key => $slug ) {
2877 // Check if the plugin was registered with TGMPA and remove if not.
2878 if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) {
2879 unset( $plugins_to_install[ $key ] );
2880 continue;
2881 }
2882
2883 // For install: make sure this is a plugin we *can* install and not one already installed.
2884 if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) {
2885 unset( $plugins_to_install[ $key ] );
2886 }
2887
2888 // For updates: make sure this is a plugin we *can* update (update available and WP version ok).
2889 if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) {
2890 unset( $plugins_to_install[ $key ] );
2891 }
2892 }
2893
2894 // No need to proceed further if we have no plugins to handle.
2895 if ( empty( $plugins_to_install ) ) {
2896 if ( 'install' === $install_type ) {
2897 $message = __( 'No plugins are available to be installed at this time.', 'wpdirectorykit' );
2898 } else {
2899 $message = __( 'No plugins are available to be updated at this time.', 'wpdirectorykit' );
2900 }
2901
2902 echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2903
2904 return false;
2905 }
2906
2907 // Pass all necessary information if WP_Filesystem is needed.
2908 $url = wp_nonce_url(
2909 $this->tgmpa->get_tgmpa_url(),
2910 'bulk-' . $this->_args['plural']
2911 );
2912
2913 // Give validated data back to $_POST which is the only place the filesystem looks for extra fields.
2914 $_POST['plugin'] = implode( ',', $plugins_to_install ); // Work around for WP bug #19643.
2915
2916 $method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
2917 $fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
2918
2919 if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) {
2920 return true; // Stop the normal page form from displaying, credential request form will be shown.
2921 }
2922
2923 // Now we have some credentials, setup WP_Filesystem.
2924 if ( ! WP_Filesystem( $creds ) ) {
2925 // Our credentials were no good, ask the user for them again.
2926 request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
2927
2928 return true;
2929 }
2930
2931 /* If we arrive here, we have the filesystem */
2932
2933 // Store all information in arrays since we are processing a bulk installation.
2934 $names = array();
2935 $sources = array(); // Needed for installs.
2936 $file_paths = array(); // Needed for upgrades.
2937 $to_inject = array(); // Information to inject into the update_plugins transient.
2938
2939 // Prepare the data for validated plugins for the install/upgrade.
2940 foreach ( $plugins_to_install as $slug ) {
2941 $name = $this->tgmpa->plugins[ $slug ]['name'];
2942 $source = $this->tgmpa->get_download_url( $slug );
2943
2944 if ( ! empty( $name ) && ! empty( $source ) ) {
2945 $names[] = $name;
2946
2947 switch ( $install_type ) {
2948
2949 case 'install':
2950 $sources[] = $source;
2951 break;
2952
2953 case 'update':
2954 $file_paths[] = $this->tgmpa->plugins[ $slug ]['file_path'];
2955 $to_inject[ $slug ] = $this->tgmpa->plugins[ $slug ];
2956 $to_inject[ $slug ]['source'] = $source;
2957 break;
2958 }
2959 }
2960 }
2961 unset( $slug, $name, $source );
2962
2963 // Create a new instance of TGMPA_Bulk_Installer.
2964 $installer = new TGMPA_Bulk_Installer(
2965 new TGMPA_Bulk_Installer_Skin(
2966 array(
2967 'url' => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),
2968 'nonce' => 'bulk-' . $this->_args['plural'],
2969 'names' => $names,
2970 'install_type' => $install_type,
2971 )
2972 )
2973 );
2974
2975 // Wrap the install process with the appropriate HTML.
2976 echo '<div class="tgmpa">',
2977 '<h2 style="font-size: 23px; font-weight: 400; line-height: 29px; margin: 0; padding: 9px 15px 4px 0;">', esc_html( get_admin_page_title() ), '</h2>
2978 <div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">';
2979
2980 // Process the bulk installation submissions.
2981 add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
2982
2983 if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2984 // Inject our info into the update transient.
2985 $this->tgmpa->inject_update_info( $to_inject );
2986
2987 $installer->bulk_upgrade( $file_paths );
2988 } else {
2989 $installer->bulk_install( $sources );
2990 }
2991
2992 remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1 );
2993
2994 echo '</div></div>';
2995
2996 return true;
2997 }
2998
2999 // Bulk activation process.
3000 if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3001 check_admin_referer( 'bulk-' . $this->_args['plural'] );
3002
3003 // Did user actually select any plugins to activate ?
3004 if ( empty( $_POST['plugin'] ) ) {
3005 echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'wpdirectorykit' ), '</p></div>';
3006
3007 return false;
3008 }
3009
3010 // Grab plugin data from $_POST.
3011 $plugins = array();
3012 if ( isset( $_POST['plugin'] ) ) {
3013 $plugins = array_map( 'urldecode', (array) $_POST['plugin'] );
3014 $plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins );
3015 }
3016
3017 $plugins_to_activate = array();
3018 $plugin_names = array();
3019
3020 // Grab the file paths for the selected & inactive plugins from the registration array.
3021 foreach ( $plugins as $slug ) {
3022 if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
3023 $plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path'];
3024 $plugin_names[] = $this->tgmpa->plugins[ $slug ]['name'];
3025 }
3026 }
3027 unset( $slug );
3028
3029 // Return early if there are no plugins to activate.
3030 if ( empty( $plugins_to_activate ) ) {
3031 echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'wpdirectorykit' ), '</p></div>';
3032
3033 return false;
3034 }
3035
3036 // Now we are good to go - let's start activating plugins.
3037 $activate = activate_plugins( $plugins_to_activate );
3038
3039 if ( is_wp_error( $activate ) ) {
3040 echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
3041 } else {
3042 $count = count( $plugin_names ); // Count so we can use _n function.
3043 $plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names );
3044 $last_plugin = array_pop( $plugin_names ); // Pop off last name to prep for readability.
3045 $imploded = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'wpdirectorykit' ) . ' ' . $last_plugin );
3046
3047 printf( // WPCS: xss ok.
3048 '<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
3049 esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'wpdirectorykit' ) ),
3050 $imploded
3051 );
3052
3053 // Update recently activated plugins option.
3054 $recent = (array) get_option( 'recently_activated' );
3055 foreach ( $plugins_to_activate as $plugin => $time ) {
3056 if ( isset( $recent[ $plugin ] ) ) {
3057 unset( $recent[ $plugin ] );
3058 }
3059 }
3060 update_option( 'recently_activated', $recent );
3061 }
3062
3063 unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another.
3064
3065 return true;
3066 }
3067
3068 return false;
3069 }
3070
3071 /**
3072 * Prepares all of our information to be outputted into a usable table.
3073 *
3074 * @since 2.2.0
3075 */
3076 public function prepare_items() {
3077 $columns = $this->get_columns(); // Get all necessary column information.
3078 $hidden = array(); // No columns to hide, but we must set as an array.
3079 $sortable = array(); // No reason to make sortable columns.
3080 $primary = $this->get_primary_column_name(); // Column which has the row actions.
3081 $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers.
3082
3083 // Process our bulk activations here.
3084 if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3085 $this->process_bulk_actions();
3086 }
3087
3088 // Store all of our plugin data into $items array so WP_List_Table can use it.
3089 $this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() );
3090 }
3091
3092 /* *********** DEPRECATED METHODS *********** */
3093
3094 /**
3095 * Retrieve plugin data, given the plugin name.
3096 *
3097 * @since 2.2.0
3098 * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead.
3099 * @see TGM_Plugin_Activation::_get_plugin_data_from_name()
3100 *
3101 * @param string $name Name of the plugin, as it was registered.
3102 * @param string $data Optional. Array key of plugin data to return. Default is slug.
3103 * @return string|boolean Plugin slug if found, false otherwise.
3104 */
3105 protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
3106 _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' );
3107
3108 return $this->tgmpa->_get_plugin_data_from_name( $name, $data );
3109 }
3110 }
3111 }
3112
3113
3114 if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
3115
3116 /**
3117 * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+.
3118 *
3119 * @since 2.5.2
3120 *
3121 * {@internal The TGMPA_Bulk_Installer class was originally called TGM_Bulk_Installer.
3122 * For more information, see that class.}}
3123 */
3124 class TGM_Bulk_Installer {
3125 }
3126 }
3127 if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
3128
3129 /**
3130 * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+.
3131 *
3132 * @since 2.5.2
3133 *
3134 * {@internal The TGMPA_Bulk_Installer_Skin class was originally called TGM_Bulk_Installer_Skin.
3135 * For more information, see that class.}}
3136 */
3137 class TGM_Bulk_Installer_Skin {
3138 }
3139 }
3140
3141 /**
3142 * The WP_Upgrader file isn't always available. If it isn't available,
3143 * we load it here.
3144 *
3145 * We check to make sure no action or activation keys are set so that WordPress
3146 * does not try to re-include the class when processing upgrades or installs outside
3147 * of the class.
3148 *
3149 * @since 2.2.0
3150 */
3151 add_action( 'admin_init', 'tgmpa_load_bulk_installer' );
3152 if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
3153 /**
3154 * Load bulk installer
3155 */
3156 function tgmpa_load_bulk_installer() {
3157 // Silently fail if 2.5+ is loaded *after* an older version.
3158 if ( ! isset( $GLOBALS['tgmpa'] ) ) {
3159 return;
3160 }
3161
3162 // Get TGMPA class instance.
3163 $tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3164
3165 if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) {
3166 if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
3167 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
3168 }
3169
3170 if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) {
3171
3172 /**
3173 * Installer class to handle bulk plugin installations.
3174 *
3175 * Extends WP_Upgrader and customizes to suit the installation of multiple
3176 * plugins.
3177 *
3178 * @since 2.2.0
3179 *
3180 * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}}
3181 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
3182 * This was done to prevent backward compatibility issues with v2.3.6.}}
3183 *
3184 * @package TGM-Plugin-Activation
3185 * @author Thomas Griffin
3186 * @author Gary Jones
3187 */
3188 class TGMPA_Bulk_Installer extends Plugin_Upgrader {
3189 /**
3190 * Holds result of bulk plugin installation.
3191 *
3192 * @since 2.2.0
3193 *
3194 * @var string
3195 */
3196 public $result;
3197
3198 /**
3199 * Flag to check if bulk installation is occurring or not.
3200 *
3201 * @since 2.2.0
3202 *
3203 * @var boolean
3204 */
3205 public $bulk = false;
3206
3207 /**
3208 * TGMPA instance
3209 *
3210 * @since 2.5.0
3211 *
3212 * @var object
3213 */
3214 protected $tgmpa;
3215
3216 /**
3217 * Whether or not the destination directory needs to be cleared ( = on update).
3218 *
3219 * @since 2.5.0
3220 *
3221 * @var bool
3222 */
3223 protected $clear_destination = false;
3224
3225 /**
3226 * References parent constructor and sets defaults for class.
3227 *
3228 * @since 2.2.0
3229 *
3230 * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
3231 */
3232 public function __construct( $skin = null ) {
3233 // Get TGMPA class instance.
3234 $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3235
3236 parent::__construct( $skin );
3237
3238 if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
3239 $this->clear_destination = true;
3240 }
3241
3242 if ( $this->tgmpa->is_automatic ) {
3243 $this->activate_strings();
3244 }
3245
3246 add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
3247 }
3248
3249 /**
3250 * Sets the correct activation strings for the installer skin to use.
3251 *
3252 * @since 2.2.0
3253 */
3254 public function activate_strings() {
3255 $this->strings['activation_failed'] = __( 'Plugin activation failed.', 'wpdirectorykit' );
3256 $this->strings['activation_success'] = __( 'Plugin activated successfully.', 'wpdirectorykit' );
3257 }
3258
3259 /**
3260 * Performs the actual installation of each plugin.
3261 *
3262 * @since 2.2.0
3263 *
3264 * @see WP_Upgrader::run()
3265 *
3266 * @param array $options The installation config options.
3267 * @return null|array Return early if error, array of installation data on success.
3268 */
3269 public function run( $options ) {
3270 $result = parent::run( $options );
3271
3272 // Reset the strings in case we changed one during automatic activation.
3273 if ( $this->tgmpa->is_automatic ) {
3274 if ( 'update' === $this->skin->options['install_type'] ) {
3275 $this->upgrade_strings();
3276 } else {
3277 $this->install_strings();
3278 }
3279 }
3280
3281 return $result;
3282 }
3283
3284 /**
3285 * Processes the bulk installation of plugins.
3286 *
3287 * @since 2.2.0
3288 *
3289 * {@internal This is basically a near identical copy of the WP Core
3290 * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with
3291 * new installs instead of upgrades.
3292 * For ease of future synchronizations, the adjustments are clearly commented, but no other
3293 * comments are added. Code style has been made to comply.}}
3294 *
3295 * @see Plugin_Upgrader::bulk_upgrade()
3296 * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
3297 * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}}
3298 *
3299 * @param array $plugins The plugin sources needed for installation.
3300 * @param array $args Arbitrary passed extra arguments.
3301 * @return array|false Install confirmation messages on success, false on failure.
3302 */
3303 public function bulk_install( $plugins, $args = array() ) {
3304 // [TGMPA + ] Hook auto-activation in.
3305 add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3306
3307 $defaults = array(
3308 'clear_update_cache' => true,
3309 );
3310 $parsed_args = wp_parse_args( $args, $defaults );
3311
3312 $this->init();
3313 $this->bulk = true;
3314
3315 $this->install_strings(); // [TGMPA + ] adjusted.
3316
3317 /* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
3318
3319 /* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
3320
3321 $this->skin->header();
3322
3323 // Connect to the Filesystem first.
3324 $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
3325 if ( ! $res ) {
3326 $this->skin->footer();
3327 return false;
3328 }
3329
3330 $this->skin->bulk_header();
3331
3332 /*
3333 * Only start maintenance mode if:
3334 * - running Multisite and there are one or more plugins specified, OR
3335 * - a plugin with an update available is currently active.
3336 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
3337 */
3338 $maintenance = ( is_multisite() && ! empty( $plugins ) );
3339
3340 /*
3341 [TGMPA - ]
3342 foreach ( $plugins as $plugin )
3343 $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
3344 */
3345 if ( $maintenance ) {
3346 $this->maintenance_mode( true );
3347 }
3348
3349 $results = array();
3350
3351 $this->update_count = count( $plugins );
3352 $this->update_current = 0;
3353 foreach ( $plugins as $plugin ) {
3354 $this->update_current++;
3355
3356 /*
3357 [TGMPA - ]
3358 $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
3359
3360 if ( !isset( $current->response[ $plugin ] ) ) {
3361 $this->skin->set_result('up_to_date');
3362 $this->skin->before();
3363 $this->skin->feedback('up_to_date');
3364 $this->skin->after();
3365 $results[$plugin] = true;
3366 continue;
3367 }
3368
3369 // Get the URL to the zip file.
3370 $r = $current->response[ $plugin ];
3371
3372 $this->skin->plugin_active = is_plugin_active($plugin);
3373 */
3374
3375 $result = $this->run(
3376 array(
3377 'package' => $plugin, // [TGMPA + ] adjusted.
3378 'destination' => WP_PLUGIN_DIR,
3379 'clear_destination' => false, // [TGMPA + ] adjusted.
3380 'clear_working' => true,
3381 'is_multi' => true,
3382 'hook_extra' => array(
3383 'plugin' => $plugin,
3384 ),
3385 )
3386 );
3387
3388 $results[ $plugin ] = $this->result;
3389
3390 // Prevent credentials auth screen from displaying multiple times.
3391 if ( false === $result ) {
3392 break;
3393 }
3394 } //end foreach $plugins
3395
3396 $this->maintenance_mode( false );
3397
3398 /**
3399 * Fires when the bulk upgrader process is complete.
3400 *
3401 * @since WP 3.6.0 / TGMPA 2.5.0
3402 *
3403 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
3404 * be a Theme_Upgrader or Core_Upgrade instance.
3405 * @param array $data {
3406 * Array of bulk item update data.
3407 *
3408 * @type string $action Type of action. Default 'update'.
3409 * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'.
3410 * @type bool $bulk Whether the update process is a bulk update. Default true.
3411 * @type array $packages Array of plugin, theme, or core packages to update.
3412 * }
3413 */
3414 do_action( 'upgrader_process_complete', $this, array(
3415 'action' => 'install', // [TGMPA + ] adjusted.
3416 'type' => 'plugin',
3417 'bulk' => true,
3418 'plugins' => $plugins,
3419 ) );
3420
3421 $this->skin->bulk_footer();
3422
3423 $this->skin->footer();
3424
3425 // Cleanup our hooks, in case something else does a upgrade on this connection.
3426 /* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
3427
3428 // [TGMPA + ] Remove our auto-activation hook.
3429 remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3430
3431 // Force refresh of plugin update information.
3432 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
3433
3434 return $results;
3435 }
3436
3437 /**
3438 * Handle a bulk upgrade request.
3439 *
3440 * @since 2.5.0
3441 *
3442 * @see Plugin_Upgrader::bulk_upgrade()
3443 *
3444 * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
3445 * @param array $args Arbitrary passed extra arguments.
3446 * @return string|bool Install confirmation messages on success, false on failure.
3447 */
3448 public function bulk_upgrade( $plugins, $args = array() ) {
3449
3450 add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3451
3452 $result = parent::bulk_upgrade( $plugins, $args );
3453
3454 remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3455
3456 return $result;
3457 }
3458
3459 /**
3460 * Abuse a filter to auto-activate plugins after installation.
3461 *
3462 * Hooked into the 'upgrader_post_install' filter hook.
3463 *
3464 * @since 2.5.0
3465 *
3466 * @param bool $bool The value we need to give back (true).
3467 * @return bool
3468 */
3469 public function auto_activate( $bool ) {
3470 // Only process the activation of installed plugins if the automatic flag is set to true.
3471 if ( $this->tgmpa->is_automatic ) {
3472 // Flush plugins cache so the headers of the newly installed plugins will be read correctly.
3473 wp_clean_plugins_cache();
3474
3475 // Get the installed plugin file.
3476 $plugin_info = $this->plugin_info();
3477
3478 // Don't try to activate on upgrade of active plugin as WP will do this already.
3479 if ( ! is_plugin_active( $plugin_info ) ) {
3480 $activate = activate_plugin( $plugin_info );
3481
3482 // Adjust the success string based on the activation result.
3483 $this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
3484
3485 if ( is_wp_error( $activate ) ) {
3486 $this->skin->error( $activate );
3487 $this->strings['process_success'] .= $this->strings['activation_failed'];
3488 } else {
3489 $this->strings['process_success'] .= $this->strings['activation_success'];
3490 }
3491 }
3492 }
3493
3494 return $bool;
3495 }
3496 }
3497 }
3498
3499 if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) {
3500
3501 /**
3502 * Installer skin to set strings for the bulk plugin installations..
3503 *
3504 * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
3505 * plugins.
3506 *
3507 * @since 2.2.0
3508 *
3509 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to
3510 * TGMPA_Bulk_Installer_Skin.
3511 * This was done to prevent backward compatibility issues with v2.3.6.}}
3512 *
3513 * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php
3514 *
3515 * @package TGM-Plugin-Activation
3516 * @author Thomas Griffin
3517 * @author Gary Jones
3518 */
3519 class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
3520 /**
3521 * Holds plugin info for each individual plugin installation.
3522 *
3523 * @since 2.2.0
3524 *
3525 * @var array
3526 */
3527 public $plugin_info = array();
3528
3529 /**
3530 * Holds names of plugins that are undergoing bulk installations.
3531 *
3532 * @since 2.2.0
3533 *
3534 * @var array
3535 */
3536 public $plugin_names = array();
3537
3538 /**
3539 * Integer to use for iteration through each plugin installation.
3540 *
3541 * @since 2.2.0
3542 *
3543 * @var integer
3544 */
3545 public $i = 0;
3546
3547 /**
3548 * TGMPA instance
3549 *
3550 * @since 2.5.0
3551 *
3552 * @var object
3553 */
3554 protected $tgmpa;
3555
3556 /**
3557 * Constructor. Parses default args with new ones and extracts them for use.
3558 *
3559 * @since 2.2.0
3560 *
3561 * @param array $args Arguments to pass for use within the class.
3562 */
3563 public function __construct( $args = array() ) {
3564 // Get TGMPA class instance.
3565 $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3566
3567 // Parse default and new args.
3568 $defaults = array(
3569 'url' => '',
3570 'nonce' => '',
3571 'names' => array(),
3572 'install_type' => 'install',
3573 );
3574 $args = wp_parse_args( $args, $defaults );
3575
3576 // Set plugin names to $this->plugin_names property.
3577 $this->plugin_names = $args['names'];
3578
3579 // Extract the new args.
3580 parent::__construct( $args );
3581 }
3582
3583 /**
3584 * Sets install skin strings for each individual plugin.
3585 *
3586 * Checks to see if the automatic activation flag is set and uses the
3587 * the proper strings accordingly.
3588 *
3589 * @since 2.2.0
3590 */
3591 public function add_strings() {
3592 if ( 'update' === $this->options['install_type'] ) {
3593 parent::add_strings();
3594 /* translators: 1: plugin name, 2: action number 3: total number of actions. */
3595 $this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'wpdirectorykit' );
3596 } else {
3597 /* translators: 1: plugin name, 2: error message. */
3598 $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'wpdirectorykit' );
3599 /* translators: 1: plugin name. */
3600 $this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'wpdirectorykit' );
3601
3602 if ( $this->tgmpa->is_automatic ) {
3603 // Automatic activation strings.
3604 $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation and activation process is starting. This process may take a while on some hosts, so please be patient.', 'wpdirectorykit' );
3605 /* translators: 1: plugin name. */
3606 $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'wpdirectorykit' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'wpdirectorykit' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'wpdirectorykit' ) . '</span>.</a>';
3607 $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', 'wpdirectorykit' );
3608 /* translators: 1: plugin name, 2: action number 3: total number of actions. */
3609 $this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'wpdirectorykit' );
3610 } else {
3611 // Default installation strings.
3612 $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation process is starting. This process may take a while on some hosts, so please be patient.', 'wpdirectorykit' );
3613 /* translators: 1: plugin name. */
3614 $this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'wpdirectorykit' );
3615 $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations have been completed.', 'wpdirectorykit' );
3616 /* translators: 1: plugin name, 2: action number 3: total number of actions. */
3617 $this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'wpdirectorykit' );
3618 }
3619 }
3620 }
3621
3622 /**
3623 * Outputs the header strings and necessary JS before each plugin installation.
3624 *
3625 * @since 2.2.0
3626 *
3627 * @param string $title Unused in this implementation.
3628 */
3629 public function before( $title = '' ) {
3630 if ( empty( $title ) ) {
3631 $title = esc_html( $this->plugin_names[ $this->i ] );
3632 }
3633 parent::before( $title );
3634 }
3635
3636 /**
3637 * Outputs the footer strings and necessary JS after each plugin installation.
3638 *
3639 * Checks for any errors and outputs them if they exist, else output
3640 * success strings.
3641 *
3642 * @since 2.2.0
3643 *
3644 * @param string $title Unused in this implementation.
3645 */
3646 public function after( $title = '' ) {
3647 if ( empty( $title ) ) {
3648 $title = esc_html( $this->plugin_names[ $this->i ] );
3649 }
3650 parent::after( $title );
3651
3652 $this->i++;
3653 }
3654
3655 /**
3656 * Outputs links after bulk plugin installation is complete.
3657 *
3658 * @since 2.2.0
3659 */
3660 public function bulk_footer() {
3661 // Serve up the string to say installations (and possibly activations) are complete.
3662 parent::bulk_footer();
3663
3664 // Flush plugins cache so we can make sure that the installed plugins list is always up to date.
3665 wp_clean_plugins_cache();
3666
3667 $this->tgmpa->show_tgmpa_version();
3668
3669 // Display message based on if all plugins are now active or not.
3670 $update_actions = array();
3671
3672 if ( $this->tgmpa->is_tgmpa_complete() ) {
3673 // All plugins are active, so we display the complete string and hide the menu to protect users.
3674 echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
3675 $update_actions['dashboard'] = sprintf(
3676 esc_html( $this->tgmpa->strings['complete'] ),
3677 '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'wpdirectorykit' ) . '</a>'
3678 );
3679 } else {
3680 $update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>';
3681 }
3682
3683 /**
3684 * Filter the list of action links available following bulk plugin installs/updates.
3685 *
3686 * @since 2.5.0
3687 *
3688 * @param array $update_actions Array of plugin action links.
3689 * @param array $plugin_info Array of information for the last-handled plugin.
3690 */
3691 $update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
3692
3693 if ( ! empty( $update_actions ) ) {
3694 $this->feedback( implode( ' | ', (array) $update_actions ) );
3695 }
3696 }
3697
3698 /* *********** DEPRECATED METHODS *********** */
3699
3700 /**
3701 * Flush header output buffer.
3702 *
3703 * @since 2.2.0
3704 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3705 * @see Bulk_Upgrader_Skin::flush_output()
3706 */
3707 public function before_flush_output() {
3708 _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3709 $this->flush_output();
3710 }
3711
3712 /**
3713 * Flush footer output buffer and iterate $this->i to make sure the
3714 * installation strings reference the correct plugin.
3715 *
3716 * @since 2.2.0
3717 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3718 * @see Bulk_Upgrader_Skin::flush_output()
3719 */
3720 public function after_flush_output() {
3721 _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3722 $this->flush_output();
3723 $this->i++;
3724 }
3725 }
3726 }
3727 }
3728 }
3729 }
3730
3731 if ( ! class_exists( 'TGMPA_Utils' ) ) {
3732
3733 /**
3734 * Generic utilities for TGMPA.
3735 *
3736 * All methods are static, poor-dev name-spacing class wrapper.
3737 *
3738 * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy.
3739 *
3740 * @since 2.5.0
3741 *
3742 * @package TGM-Plugin-Activation
3743 * @author Juliette Reinders Folmer
3744 */
3745 class TGMPA_Utils {
3746 /**
3747 * Whether the PHP filter extension is enabled.
3748 *
3749 * @see http://php.net/book.filter
3750 *
3751 * @since 2.5.0
3752 *
3753 * @static
3754 *
3755 * @var bool $has_filters True is the extension is enabled.
3756 */
3757 public static $has_filters;
3758
3759 /**
3760 * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map().
3761 *
3762 * @since 2.5.0
3763 *
3764 * @static
3765 *
3766 * @param string $string Text to be wrapped.
3767 * @return string
3768 */
3769 public static function wrap_in_em( $string ) {
3770 return '<em>' . wp_kses_post( $string ) . '</em>';
3771 }
3772
3773 /**
3774 * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map().
3775 *
3776 * @since 2.5.0
3777 *
3778 * @static
3779 *
3780 * @param string $string Text to be wrapped.
3781 * @return string
3782 */
3783 public static function wrap_in_strong( $string ) {
3784 return '<strong>' . wp_kses_post( $string ) . '</strong>';
3785 }
3786
3787 /**
3788 * Helper function: Validate a value as boolean
3789 *
3790 * @since 2.5.0
3791 *
3792 * @static
3793 *
3794 * @param mixed $value Arbitrary value.
3795 * @return bool
3796 */
3797 public static function validate_bool( $value ) {
3798 if ( ! isset( self::$has_filters ) ) {
3799 self::$has_filters = extension_loaded( 'filter' );
3800 }
3801
3802 if ( self::$has_filters ) {
3803 return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
3804 } else {
3805 return self::emulate_filter_bool( $value );
3806 }
3807 }
3808
3809 /**
3810 * Helper function: Cast a value to bool
3811 *
3812 * @since 2.5.0
3813 *
3814 * @static
3815 *
3816 * @param mixed $value Value to cast.
3817 * @return bool
3818 */
3819 protected static function emulate_filter_bool( $value ) {
3820 // @codingStandardsIgnoreStart
3821 static $true = array(
3822 '1',
3823 'true', 'True', 'TRUE',
3824 'y', 'Y',
3825 'yes', 'Yes', 'YES',
3826 'on', 'On', 'ON',
3827 );
3828 static $false = array(
3829 '0',
3830 'false', 'False', 'FALSE',
3831 'n', 'N',
3832 'no', 'No', 'NO',
3833 'off', 'Off', 'OFF',
3834 );
3835 // @codingStandardsIgnoreEnd
3836
3837 if ( is_bool( $value ) ) {
3838 return $value;
3839 } elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
3840 return (bool) $value;
3841 } elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
3842 return (bool) $value;
3843 } elseif ( is_string( $value ) ) {
3844 $value = trim( $value );
3845 if ( in_array( $value, $true, true ) ) {
3846 return true;
3847 } elseif ( in_array( $value, $false, true ) ) {
3848 return false;
3849 } else {
3850 return false;
3851 }
3852 }
3853
3854 return false;
3855 }
3856 } // End of class TGMPA_Utils
3857 } // End of class_exists wrapper
3858