Completed
Pull Request — develop (#689)
by
unknown
01:46
created

class-tgm-plugin-activation.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 ( ! defined( 'ABSPATH' ) ) {
36
	return;
37
}
38
39
if ( ! class_exists( 'TGM_Plugin_Activation' ) ) {
40
41
	/**
42
	 * Automatic plugin installation and activation library.
43
	 *
44
	 * Creates a way to automatically install and activate plugins from within themes.
45
	 * The plugins can be either bundled, downloaded from the WordPress
46
	 * Plugin Repository or downloaded from another external source.
47
	 *
48
	 * @since 1.0.0
49
	 *
50
	 * @package TGM-Plugin-Activation
51
	 * @author  Thomas Griffin
52
	 * @author  Gary Jones
53
	 */
54
	class TGM_Plugin_Activation {
55
		/**
56
		 * TGMPA version number.
57
		 *
58
		 * @since 2.5.0
59
		 *
60
		 * @const string Version number.
61
		 */
62
		const TGMPA_VERSION = '2.6.1';
63
64
		/**
65
		 * Regular expression to test if a URL is a WP plugin repo URL.
66
		 *
67
		 * @const string Regex.
68
		 *
69
		 * @since 2.5.0
70
		 */
71
		const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|';
72
73
		/**
74
		 * Arbitrary regular expression to test if a string starts with a URL.
75
		 *
76
		 * @const string Regex.
77
		 *
78
		 * @since 2.5.0
79
		 */
80
		const IS_URL_REGEX = '|^http[s]?://|';
81
82
		/**
83
		 * Holds a copy of itself, so it can be referenced by the class name.
84
		 *
85
		 * @since 1.0.0
86
		 *
87
		 * @var TGM_Plugin_Activation
88
		 */
89
		public static $instance;
90
91
		/**
92
		 * Holds arrays of plugin details.
93
		 *
94
		 * @since 1.0.0
95
		 * @since 2.5.0 the array has the plugin slug as an associative key.
96
		 *
97
		 * @var array
98
		 */
99
		public $plugins = array();
100
101
		/**
102
		 * Holds arrays of plugin names to use to sort the plugins array.
103
		 *
104
		 * @since 2.5.0
105
		 *
106
		 * @var array
107
		 */
108
		protected $sort_order = array();
109
110
		/**
111
		 * Whether any plugins have the 'force_activation' setting set to true.
112
		 *
113
		 * @since 2.5.0
114
		 *
115
		 * @var bool
116
		 */
117
		protected $has_forced_activation = false;
118
119
		/**
120
		 * Whether any plugins have the 'force_deactivation' setting set to true.
121
		 *
122
		 * @since 2.5.0
123
		 *
124
		 * @var bool
125
		 */
126
		protected $has_forced_deactivation = false;
127
128
		/**
129
		 * Name of the unique ID to hash notices.
130
		 *
131
		 * @since 2.4.0
132
		 *
133
		 * @var string
134
		 */
135
		public $id = 'tgmpa';
136
137
		/**
138
		 * Name of the query-string argument for the admin page.
139
		 *
140
		 * @since 1.0.0
141
		 *
142
		 * @var string
143
		 */
144
		protected $menu = 'tgmpa-install-plugins';
145
146
		/**
147
		 * Parent menu file slug.
148
		 *
149
		 * @since 2.5.0
150
		 *
151
		 * @var string
152
		 */
153
		public $parent_slug = 'themes.php';
154
155
		/**
156
		 * Capability needed to view the plugin installation menu item.
157
		 *
158
		 * @since 2.5.0
159
		 *
160
		 * @var string
161
		 */
162
		public $capability = 'edit_theme_options';
163
164
		/**
165
		 * Default absolute path to folder containing bundled plugin zip files.
166
		 *
167
		 * @since 2.0.0
168
		 *
169
		 * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string.
170
		 */
171
		public $default_path = '';
172
173
		/**
174
		 * Flag to show admin notices or not.
175
		 *
176
		 * @since 2.1.0
177
		 *
178
		 * @var boolean
179
		 */
180
		public $has_notices = true;
181
182
		/**
183
		 * Flag to determine if the user can dismiss the notice nag.
184
		 *
185
		 * @since 2.4.0
186
		 *
187
		 * @var boolean
188
		 */
189
		public $dismissable = true;
190
191
		/**
192
		 * Message to be output above nag notice if dismissable is false.
193
		 *
194
		 * @since 2.4.0
195
		 *
196
		 * @var string
197
		 */
198
		public $dismiss_msg = '';
199
200
		/**
201
		 * Flag to set automatic activation of plugins. Off by default.
202
		 *
203
		 * @since 2.2.0
204
		 *
205
		 * @var boolean
206
		 */
207
		public $is_automatic = false;
208
209
		/**
210
		 * Optional message to display before the plugins table.
211
		 *
212
		 * @since 2.2.0
213
		 *
214
		 * @var string Message filtered by wp_kses_post(). Default is empty string.
215
		 */
216
		public $message = '';
217
218
		/**
219
		 * Holds configurable array of strings.
220
		 *
221
		 * Default values are added in the constructor.
222
		 *
223
		 * @since 2.0.0
224
		 *
225
		 * @var array
226
		 */
227
		public $strings = array();
228
229
		/**
230
		 * Holds the version of WordPress.
231
		 *
232
		 * @since 2.4.0
233
		 *
234
		 * @var int
235
		 */
236
		public $wp_version;
237
238
		/**
239
		 * Holds the hook name for the admin page.
240
		 *
241
		 * @since 2.5.0
242
		 *
243
		 * @var string
244
		 */
245
		public $page_hook;
246
247
		/**
248
		 * Adds a reference of this object to $instance, populates default strings,
249
		 * does the tgmpa_init action hook, and hooks in the interactions to init.
250
		 *
251
		 * {@internal This method should be `protected`, but as too many TGMPA implementations
252
		 * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues.
253
		 * Reverted back to public for the time being.}}
254
		 *
255
		 * @since 1.0.0
256
		 *
257
		 * @see TGM_Plugin_Activation::init()
258
		 */
259
		public function __construct() {
260
			// Set the current WordPress version.
261
			$this->wp_version = $GLOBALS['wp_version'];
262
263
			// Announce that the class is ready, and pass the object (for advanced use).
264
			do_action_ref_array( 'tgmpa_init', array( $this ) );
265
266
			/*
267
			 * Load our text domain and allow for overloading the fall-back file.
268
			 *
269
			 * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
270
			 * generator on the website.}}
271
			 */
272
			add_action( 'init', array( $this, 'load_textdomain' ), 5 );
273
			add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 );
274
275
			// When the rest of WP has loaded, kick-start the rest of the class.
276
			add_action( 'init', array( $this, 'init' ) );
277
		}
278
279
		/**
280
		 * Magic method to (not) set protected properties from outside of this class.
281
		 *
282
		 * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6  where the `menu` property
283
		 * is being assigned rather than tested in a conditional, effectively rendering it useless.
284
		 * This 'hack' prevents this from happening.}}
285
		 *
286
		 * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593
287
		 *
288
		 * @since 2.5.2
289
		 *
290
		 * @param string $name  Name of an inaccessible property.
291
		 * @param mixed  $value Value to assign to the property.
292
		 * @return void  Silently fail to set the property when this is tried from outside of this class context.
293
		 *               (Inside this class context, the __set() method if not used as there is direct access.)
294
		 */
295
		public function __set( $name, $value ) {
296
			return;
297
		}
298
299
		/**
300
		 * Magic method to get the value of a protected property outside of this class context.
301
		 *
302
		 * @since 2.5.2
303
		 *
304
		 * @param string $name Name of an inaccessible property.
305
		 * @return mixed The property value.
306
		 */
307
		public function __get( $name ) {
308
			return $this->{$name};
309
		}
310
311
		/**
312
		 * Initialise the interactions between this class and WordPress.
313
		 *
314
		 * Hooks in three new methods for the class: admin_menu, notices and styles.
315
		 *
316
		 * @since 2.0.0
317
		 *
318
		 * @see TGM_Plugin_Activation::admin_menu()
319
		 * @see TGM_Plugin_Activation::notices()
320
		 * @see TGM_Plugin_Activation::styles()
321
		 */
322
		public function init() {
323
			/**
324
			 * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter
325
			 * you can overrule that behaviour.
326
			 *
327
			 * @since 2.5.0
328
			 *
329
			 * @param bool $load Whether or not TGMPA should load.
330
			 *                   Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`.
331
			 */
332
			if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) {
333
				return;
334
			}
335
336
			// Load class strings.
337
			$this->strings = array(
338
				'page_title'                      => __( 'Install Required Plugins', 'tgmpa' ),
339
				'menu_title'                      => __( 'Install Plugins', 'tgmpa' ),
340
				/* translators: %s: plugin name. */
341
				'installing'                      => __( 'Installing Plugin: %s', 'tgmpa' ),
342
				/* translators: %s: plugin name. */
343
				'updating'                        => __( 'Updating Plugin: %s', 'tgmpa' ),
344
				'oops'                            => __( 'Something went wrong with the plugin API.', 'tgmpa' ),
345
				/* translators: 1: plugin name(s). */
346
				'notice_can_install_required'     => _n_noop(
347
					'This theme requires the following plugin: %1$s.',
348
					'This theme requires the following plugins: %1$s.',
349
					'tgmpa'
350
				),
351
				/* translators: 1: plugin name(s). */
352
				'notice_can_install_recommended'  => _n_noop(
353
					'This theme recommends the following plugin: %1$s.',
354
					'This theme recommends the following plugins: %1$s.',
355
					'tgmpa'
356
				),
357
				/* translators: 1: plugin name(s). */
358
				'notice_ask_to_update'            => _n_noop(
359
					'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',
360
					'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',
361
					'tgmpa'
362
				),
363
				/* translators: 1: plugin name(s). */
364
				'notice_ask_to_update_maybe'      => _n_noop(
365
					'There is an update available for: %1$s.',
366
					'There are updates available for the following plugins: %1$s.',
367
					'tgmpa'
368
				),
369
				/* translators: 1: plugin name(s). */
370
				'notice_can_activate_required'    => _n_noop(
371
					'The following required plugin is currently inactive: %1$s.',
372
					'The following required plugins are currently inactive: %1$s.',
373
					'tgmpa'
374
				),
375
				/* translators: 1: plugin name(s). */
376
				'notice_can_activate_recommended' => _n_noop(
377
					'The following recommended plugin is currently inactive: %1$s.',
378
					'The following recommended plugins are currently inactive: %1$s.',
379
					'tgmpa'
380
				),
381
				'install_link'                    => _n_noop(
382
					'Begin installing plugin',
383
					'Begin installing plugins',
384
					'tgmpa'
385
				),
386
				'update_link'                     => _n_noop(
387
					'Begin updating plugin',
388
					'Begin updating plugins',
389
					'tgmpa'
390
				),
391
				'activate_link'                   => _n_noop(
392
					'Begin activating plugin',
393
					'Begin activating plugins',
394
					'tgmpa'
395
				),
396
				'return'                          => __( 'Return to Required Plugins Installer', 'tgmpa' ),
397
				'dashboard'                       => __( 'Return to the Dashboard', 'tgmpa' ),
398
				'plugin_activated'                => __( 'Plugin activated successfully.', 'tgmpa' ),
399
				'activated_successfully'          => __( 'The following plugin was activated successfully:', 'tgmpa' ),
400
				/* translators: 1: plugin name. */
401
				'plugin_already_active'           => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ),
402
				/* translators: 1: plugin name. */
403
				'plugin_needs_higher_version'     => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ),
404
				/* translators: 1: dashboard link. */
405
				'complete'                        => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ),
406
				'dismiss'                         => __( 'Dismiss this notice', 'tgmpa' ),
407
				'notice_cannot_install_activate'  => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ),
408
				'contact_admin'                   => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),
409
			);
410
411
			do_action( 'tgmpa_register' );
412
413
			/* After this point, the plugins should be registered and the configuration set. */
414
415
			// Proceed only if we have plugins to handle.
416
			if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
417
				return;
418
			}
419
420
			// Set up the menu and notices if we still have outstanding actions.
421
			if ( true !== $this->is_tgmpa_complete() ) {
422
				// Sort the plugins.
423
				array_multisort( $this->sort_order, SORT_ASC, $this->plugins );
424
425
				add_action( 'admin_menu', array( $this, 'admin_menu' ) );
426
				add_action( 'admin_head', array( $this, 'dismiss' ) );
427
428
				// Prevent the normal links from showing underneath a single install/update page.
429
				add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
430
				add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) );
431
432
				if ( $this->has_notices ) {
433
					add_action( 'admin_notices', array( $this, 'notices' ) );
434
					add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
435
					add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
436
				}
437
			}
438
439
			// If needed, filter plugin action links.
440
			add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 );
441
442
			// Make sure things get reset on switch theme.
443
			add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
444
445
			if ( $this->has_notices ) {
446
				add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
447
			}
448
449
			// Setup the force activation hook.
450
			if ( true === $this->has_forced_activation ) {
451
				add_action( 'admin_init', array( $this, 'force_activation' ) );
452
			}
453
454
			// Setup the force deactivation hook.
455
			if ( true === $this->has_forced_deactivation ) {
456
				add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
457
			}
458
459
			// Add CSS for the TGMPA admin page.
460
			add_action( 'admin_head', array( $this, 'admin_css' ) );
461
		}
462
463
		/**
464
		 * Load translations.
465
		 *
466
		 * @since 2.6.0
467
		 *
468
		 * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to
469
		 * get round the different ways of handling the path and deprecated notices being thrown
470
		 * and such. For plugins, the actual file name will be corrected by a filter.}}
471
		 *
472
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
473
		 * generator on the website.}}
474
		 */
475
		public function load_textdomain() {
476
			if ( is_textdomain_loaded( 'tgmpa' ) ) {
477
				return;
478
			}
479
480
			if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) {
481
				// Plugin, we'll need to adjust the file name.
482
				add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 );
483
				load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
484
				remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 );
485
			} else {
486
				load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
487
			}
488
		}
489
490
		/**
491
		 * Correct the .mo file name for (must-use) plugins.
492
		 *
493
		 * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`.
494
		 *
495
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
496
		 * generator on the website.}}
497
		 *
498
		 * @since 2.6.0
499
		 *
500
		 * @param string $mofile Full path to the target mofile.
501
		 * @param string $domain The domain for which a language file is being loaded.
502
		 * @return string $mofile
503
		 */
504
		public function correct_plugin_mofile( $mofile, $domain ) {
505
			// Exit early if not our domain (just in case).
506
			if ( 'tgmpa' !== $domain ) {
507
				return $mofile;
508
			}
509
			return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile );
510
		}
511
512
		/**
513
		 * Potentially overload the fall-back translation file for the current language.
514
		 *
515
		 * WP, by default since WP 3.7, will load a local translation first and if none
516
		 * can be found, will try and find a translation in the /wp-content/languages/ directory.
517
		 * As this library is theme/plugin agnostic, translation files for TGMPA can exist both
518
		 * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory.
519
		 *
520
		 * This method makes sure both directories are checked.
521
		 *
522
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
523
		 * generator on the website.}}
524
		 *
525
		 * @since 2.6.0
526
		 *
527
		 * @param string $mofile Full path to the target mofile.
528
		 * @param string $domain The domain for which a language file is being loaded.
529
		 * @return string $mofile
530
		 */
531
		public function overload_textdomain_mofile( $mofile, $domain ) {
532
			// Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
533
			if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) {
534
				return $mofile;
535
			}
536
537
			// Current fallback file is not valid, let's try the alternative option.
538
			if ( false !== strpos( $mofile, '/themes/' ) ) {
539
				return str_replace( '/themes/', '/plugins/', $mofile );
540
			} elseif ( false !== strpos( $mofile, '/plugins/' ) ) {
541
				return str_replace( '/plugins/', '/themes/', $mofile );
542
			} else {
543
				return $mofile;
544
			}
545
		}
546
547
		/**
548
		 * Hook in plugin action link filters for the WP native plugins page.
549
		 *
550
		 * - Prevent activation of plugins which don't meet the minimum version requirements.
551
		 * - Prevent deactivation of force-activated plugins.
552
		 * - Add update notice if update available.
553
		 *
554
		 * @since 2.5.0
555
		 */
556
		public function add_plugin_action_link_filters() {
557
			foreach ( $this->plugins as $slug => $plugin ) {
558
				if ( false === $this->can_plugin_activate( $slug ) ) {
559
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 );
560
				}
561
562
				if ( true === $plugin['force_activation'] ) {
563
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 );
564
				}
565
566
				if ( false !== $this->does_plugin_require_update( $slug ) ) {
567
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 );
568
				}
569
			}
570
		}
571
572
		/**
573
		 * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the
574
		 * minimum version requirements.
575
		 *
576
		 * @since 2.5.0
577
		 *
578
		 * @param array $actions Action links.
579
		 * @return array
580
		 */
581
		public function filter_plugin_action_links_activate( $actions ) {
582
			unset( $actions['activate'] );
583
584
			return $actions;
585
		}
586
587
		/**
588
		 * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate.
589
		 *
590
		 * @since 2.5.0
591
		 *
592
		 * @param array $actions Action links.
593
		 * @return array
594
		 */
595
		public function filter_plugin_action_links_deactivate( $actions ) {
596
			unset( $actions['deactivate'] );
597
598
			return $actions;
599
		}
600
601
		/**
602
		 * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the
603
		 * minimum version requirements.
604
		 *
605
		 * @since 2.5.0
606
		 *
607
		 * @param array $actions Action links.
608
		 * @return array
609
		 */
610
		public function filter_plugin_action_links_update( $actions ) {
611
			$actions['update'] = sprintf(
612
				'<a href="%1$s" title="%2$s" class="edit">%3$s</a>',
613
				esc_url( $this->get_tgmpa_status_url( 'update' ) ),
614
				esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ),
615
				esc_html__( 'Update Required', 'tgmpa' )
616
			);
617
618
			return $actions;
619
		}
620
621
		/**
622
		 * Handles calls to show plugin information via links in the notices.
623
		 *
624
		 * We get the links in the admin notices to point to the TGMPA page, rather
625
		 * than the typical plugin-install.php file, so we can prepare everything
626
		 * beforehand.
627
		 *
628
		 * WP does not make it easy to show the plugin information in the thickbox -
629
		 * here we have to require a file that includes a function that does the
630
		 * main work of displaying it, enqueue some styles, set up some globals and
631
		 * finally call that function before exiting.
632
		 *
633
		 * Down right easy once you know how...
634
		 *
635
		 * Returns early if not the TGMPA page.
636
		 *
637
		 * @since 2.1.0
638
		 *
639
		 * @global string $tab Used as iframe div class names, helps with styling
640
		 * @global string $body_id Used as the iframe body ID, helps with styling
641
		 *
642
		 * @return null Returns early if not the TGMPA page.
643
		 */
644
		public function admin_init() {
645
			if ( ! $this->is_tgmpa_page() ) {
646
				return;
647
			}
648
649
			if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) {
650
				// Needed for install_plugin_information().
651
				require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
652
653
				wp_enqueue_style( 'plugin-install' );
654
655
				global $tab, $body_id;
656
				$body_id = 'plugin-information'; // WPCS: override ok, prefix ok.
657
				$tab     = 'plugin-information'; // WPCS: override ok.
658
659
				install_plugin_information();
660
661
				exit;
662
			}
663
		}
664
665
		/**
666
		 * Enqueue thickbox scripts/styles for plugin info.
667
		 *
668
		 * Thickbox is not automatically included on all admin pages, so we must
669
		 * manually enqueue it for those pages.
670
		 *
671
		 * Thickbox is only loaded if the user has not dismissed the admin
672
		 * notice or if there are any plugins left to install and activate.
673
		 *
674
		 * @since 2.1.0
675
		 */
676
		public function thickbox() {
677
			if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
678
				add_thickbox();
679
			}
680
		}
681
682
		/**
683
		 * Adds submenu page if there are plugin actions to take.
684
		 *
685
		 * This method adds the submenu page letting users know that a required
686
		 * plugin needs to be installed.
687
		 *
688
		 * This page disappears once the plugin has been installed and activated.
689
		 *
690
		 * @since 1.0.0
691
		 *
692
		 * @see TGM_Plugin_Activation::init()
693
		 * @see TGM_Plugin_Activation::install_plugins_page()
694
		 *
695
		 * @return null Return early if user lacks capability to install a plugin.
696
		 */
697
		public function admin_menu() {
698
			// Make sure privileges are correct to see the page.
699
			if ( ! current_user_can( 'install_plugins' ) ) {
700
				return;
701
			}
702
703
			$args = apply_filters(
704
				'tgmpa_admin_menu_args',
705
				array(
706
					'parent_slug' => $this->parent_slug,                     // Parent Menu slug.
707
					'page_title'  => $this->strings['page_title'],           // Page title.
708
					'menu_title'  => $this->strings['menu_title'],           // Menu title.
709
					'capability'  => $this->capability,                      // Capability.
710
					'menu_slug'   => $this->menu,                            // Menu slug.
711
					'function'    => array( $this, 'install_plugins_page' ), // Callback.
712
				)
713
			);
714
715
			$this->add_admin_menu( $args );
716
		}
717
718
		/**
719
		 * Add the menu item.
720
		 *
721
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
722
		 * generator on the website.}}
723
		 *
724
		 * @since 2.5.0
725
		 *
726
		 * @param array $args Menu item configuration.
727
		 */
728
		protected function add_admin_menu( array $args ) {
729
			if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) {
730
				_deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) );
731
			}
732
733
			if ( 'themes.php' === $this->parent_slug ) {
734
				$this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
735
			} else {
736
				$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'] );
737
			}
738
		}
739
740
		/**
741
		 * Echoes plugin installation form.
742
		 *
743
		 * This method is the callback for the admin_menu method function.
744
		 * This displays the admin page and form area where the user can select to install and activate the plugin.
745
		 * Aborts early if we're processing a plugin installation action.
746
		 *
747
		 * @since 1.0.0
748
		 *
749
		 * @return null Aborts early if we're processing a plugin installation action.
750
		 */
751
		public function install_plugins_page() {
752
			// Store new instance of plugin table in object.
753
			$plugin_table = new TGMPA_List_Table();
754
755
			// Return early if processing a plugin installation action.
756
			if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
757
				return;
758
			}
759
760
			// Force refresh of available plugin information so we'll know about manual updates/deletes.
761
			wp_clean_plugins_cache( false );
762
763
			?>
764
			<div class="tgmpa wrap">
765
				<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
766
				<?php $plugin_table->prepare_items(); ?>
767
768
				<?php
769
				if ( ! empty( $this->message ) && is_string( $this->message ) ) {
770
					echo wp_kses_post( $this->message );
771
				}
772
				?>
773
				<?php $plugin_table->views(); ?>
774
775
				<form id="tgmpa-plugins" action="" method="post">
776
					<input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
777
					<input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
778
					<?php $plugin_table->display(); ?>
779
				</form>
780
			</div>
781
			<?php
782
		}
783
784
		/**
785
		 * Installs, updates or activates a plugin depending on the action link clicked by the user.
786
		 *
787
		 * Checks the $_GET variable to see which actions have been
788
		 * passed and responds with the appropriate method.
789
		 *
790
		 * Uses WP_Filesystem to process and handle the plugin installation
791
		 * method.
792
		 *
793
		 * @since 1.0.0
794
		 *
795
		 * @uses WP_Filesystem
796
		 * @uses WP_Error
797
		 * @uses WP_Upgrader
798
		 * @uses Plugin_Upgrader
799
		 * @uses Plugin_Installer_Skin
800
		 * @uses Plugin_Upgrader_Skin
801
		 *
802
		 * @return boolean True on success, false on failure.
803
		 */
804
		protected function do_plugin_install() {
805
			if ( empty( $_GET['plugin'] ) ) {
806
				return false;
807
			}
808
809
			// All plugin information will be stored in an array for processing.
810
			$slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
811
812
			if ( ! isset( $this->plugins[ $slug ] ) ) {
813
				return false;
814
			}
815
816
			// Was an install or upgrade action link clicked?
817
			if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
818
819
				$install_type = 'install';
820
				if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
821
					$install_type = 'update';
822
				}
823
824
				check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
825
826
				// Pass necessary information via URL if WP_Filesystem is needed.
827
				$url = wp_nonce_url(
828
					add_query_arg(
829
						array(
830
							'plugin'                 => urlencode( $slug ),
831
							'tgmpa-' . $install_type => $install_type . '-plugin',
832
						),
833
						$this->get_tgmpa_url()
834
					),
835
					'tgmpa-' . $install_type,
836
					'tgmpa-nonce'
837
				);
838
839
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
840
841
				$creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() );
842
				if ( false === $creds ) {
843
					return true;
844
				}
845
846
				if ( ! WP_Filesystem( $creds ) ) {
847
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
848
					return true;
849
				}
850
851
				/* If we arrive here, we have the filesystem. */
852
853
				// Prep variables for Plugin_Installer_Skin class.
854
				$extra         = array();
855
				$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
856
				$source        = $this->get_download_url( $slug );
857
				$api           = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
858
				$api           = ( false !== $api ) ? $api : null;
859
860
				$url = add_query_arg(
861
					array(
862
						'action' => $install_type . '-plugin',
863
						'plugin' => urlencode( $slug ),
864
					),
865
					'update.php'
866
				);
867
868
				if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
869
					require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
870
				}
871
872
				$title     = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
873
				$skin_args = array(
874
					'type'   => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
875
					'title'  => sprintf( $title, $this->plugins[ $slug ]['name'] ),
876
					'url'    => esc_url_raw( $url ),
877
					'nonce'  => $install_type . '-plugin_' . $slug,
878
					'plugin' => '',
879
					'api'    => $api,
880
					'extra'  => $extra,
881
				);
882
				unset( $title );
883
884
				if ( 'update' === $install_type ) {
885
					$skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
886
					$skin                = new Plugin_Upgrader_Skin( $skin_args );
887
				} else {
888
					$skin = new Plugin_Installer_Skin( $skin_args );
889
				}
890
891
				// Create a new instance of Plugin_Upgrader.
892
				$upgrader = new Plugin_Upgrader( $skin );
893
894
				// Perform the action and install the plugin from the $source urldecode().
895
				add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
896
897
				if ( 'update' === $install_type ) {
898
					// Inject our info into the update transient.
899
					$to_inject                    = array(
900
						$slug => $this->plugins[ $slug ],
901
					);
902
					$to_inject[ $slug ]['source'] = $source;
903
					$this->inject_update_info( $to_inject );
904
905
					$upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
906
				} else {
907
					$upgrader->install( $source );
908
				}
909
910
				remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 );
911
912
				// Make sure we have the correct file path now the plugin is installed/updated.
913
				$this->populate_file_path( $slug );
914
915
				// Only activate plugins if the config option is set to true and the plugin isn't
916
				// already active (upgrade).
917
				if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
918
					$plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
919
					if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
920
						return true; // Finish execution of the function early as we encountered an error.
921
					}
922
				}
923
924
				$this->show_tgmpa_version();
925
926
				// Display message based on if all plugins are now active or not.
927
				if ( $this->is_tgmpa_complete() ) {
928
					echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html( $this->strings['dashboard'] ) . '</a>' ), '</p>';
929
					echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
930
				} else {
931
					echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
932
				}
933
934
				return true;
935
			} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
936
				// Activate action link was clicked.
937
				check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
938
939
				if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
940
					return true; // Finish execution of the function early as we encountered an error.
941
				}
942
			}
943
944
			return false;
945
		}
946
947
		/**
948
		 * Inject information into the 'update_plugins' site transient as WP checks that before running an update.
949
		 *
950
		 * @since 2.5.0
951
		 *
952
		 * @param array $plugins The plugin information for the plugins which are to be updated.
953
		 */
954
		public function inject_update_info( $plugins ) {
955
			$repo_updates = get_site_transient( 'update_plugins' );
956
957
			if ( ! is_object( $repo_updates ) ) {
958
				$repo_updates = new stdClass();
959
			}
960
961
			foreach ( $plugins as $slug => $plugin ) {
962
				$file_path = $plugin['file_path'];
963
964
				if ( empty( $repo_updates->response[ $file_path ] ) ) {
965
					$repo_updates->response[ $file_path ] = new stdClass();
966
				}
967
968
				// We only really need to set package, but let's do all we can in case WP changes something.
969
				$repo_updates->response[ $file_path ]->slug        = $slug;
970
				$repo_updates->response[ $file_path ]->plugin      = $file_path;
971
				$repo_updates->response[ $file_path ]->new_version = $plugin['version'];
972
				$repo_updates->response[ $file_path ]->package     = $plugin['source'];
973
				if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
974
					$repo_updates->response[ $file_path ]->url = $plugin['external_url'];
975
				}
976
			}
977
978
			set_site_transient( 'update_plugins', $repo_updates );
979
		}
980
981
		/**
982
		 * Adjust the plugin directory name if necessary.
983
		 *
984
		 * The final destination directory of a plugin is based on the subdirectory name found in the
985
		 * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
986
		 * subdirectory name is not the same as the expected slug and the plugin will not be recognized
987
		 * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
988
		 * the expected plugin slug.
989
		 *
990
		 * @since 2.5.0
991
		 *
992
		 * @param string       $source        Path to upgrade/zip-file-name.tmp/subdirectory/.
993
		 * @param string       $remote_source Path to upgrade/zip-file-name.tmp.
994
		 * @param \WP_Upgrader $upgrader      Instance of the upgrader which installs the plugin.
995
		 * @return string $source
996
		 */
997
		public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
998
			if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
999
				return $source;
1000
			}
1001
1002
			// Check for single file plugins.
1003
			$source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
1004
			if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
1005
				return $source;
1006
			}
1007
1008
			// Multi-file plugin, let's see if the directory is correctly named.
1009
			$desired_slug = '';
1010
1011
			// Figure out what the slug is supposed to be.
1012
			if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
1013
				$desired_slug = $upgrader->skin->options['extra']['slug'];
1014
			} else {
1015
				// Bulk installer contains less info, so fall back on the info registered here.
1016
				foreach ( $this->plugins as $slug => $plugin ) {
1017
					if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
1018
						$desired_slug = $slug;
1019
						break;
1020
					}
1021
				}
1022
				unset( $slug, $plugin );
1023
			}
1024
1025
			if ( ! empty( $desired_slug ) ) {
1026
				$subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
1027
1028
				if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
1029
					$from_path = untrailingslashit( $source );
1030
					$to_path   = trailingslashit( $remote_source ) . $desired_slug;
1031
1032
					if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
1033
						return trailingslashit( $to_path );
1034 View Code Duplication
					} else {
1035
						return new WP_Error(
1036
							'rename_failed',
1037
							esc_html__( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ),
1038
							array(
1039
								'found'    => $subdir_name,
1040
								'expected' => $desired_slug,
1041
							)
1042
						);
1043
					}
1044 View Code Duplication
				} elseif ( empty( $subdir_name ) ) {
1045
					return new WP_Error(
1046
						'packaged_wrong',
1047
						esc_html__( 'The remote plugin package consists of more than one file, but the files are not packaged in a folder.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ),
1048
						array(
1049
							'found'    => $subdir_name,
1050
							'expected' => $desired_slug,
1051
						)
1052
					);
1053
				}
1054
			}
1055
1056
			return $source;
1057
		}
1058
1059
		/**
1060
		 * Activate a single plugin and send feedback about the result to the screen.
1061
		 *
1062
		 * @since 2.5.0
1063
		 *
1064
		 * @param string $file_path Path within wp-plugins/ to main plugin file.
1065
		 * @param string $slug      Plugin slug.
1066
		 * @param bool   $automatic Whether this is an automatic activation after an install. Defaults to false.
1067
		 *                          This determines the styling of the output messages.
1068
		 * @return bool False if an error was encountered, true otherwise.
1069
		 */
1070
		protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
1071
			if ( $this->can_plugin_activate( $slug ) ) {
1072
				$activate = activate_plugin( $file_path );
1073
1074
				if ( is_wp_error( $activate ) ) {
1075
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
1076
						'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
1077
1078
					return false; // End it here if there is an error with activation.
1079
				} else {
1080
					if ( ! $automatic ) {
1081
						// Make sure message doesn't display again if bulk activation is performed
1082
						// immediately after a single activation.
1083
						if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1084
							echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
1085
						}
1086
					} else {
1087
						// Simpler message layout for use on the plugin install page.
1088
						echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
1089
					}
1090
				}
1091 View Code Duplication
			} elseif ( $this->is_plugin_active( $slug ) ) {
1092
				// No simpler message format provided as this message should never be encountered
1093
				// on the plugin install page.
1094
				echo '<div id="message" class="error"><p>',
1095
					sprintf(
1096
						esc_html( $this->strings['plugin_already_active'] ),
1097
						'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1098
					),
1099
					'</p></div>';
1100
			} elseif ( $this->does_plugin_require_update( $slug ) ) {
1101
				if ( ! $automatic ) {
1102
					// Make sure message doesn't display again if bulk activation is performed
1103
					// immediately after a single activation.
1104 View Code Duplication
					if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1105
						echo '<div id="message" class="error"><p>',
1106
							sprintf(
1107
								esc_html( $this->strings['plugin_needs_higher_version'] ),
1108
								'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1109
							),
1110
							'</p></div>';
1111
					}
1112
				} else {
1113
					// Simpler message layout for use on the plugin install page.
1114
					echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
1115
				}
1116
			}
1117
1118
			return true;
1119
		}
1120
1121
		/**
1122
		 * Echoes required plugin notice.
1123
		 *
1124
		 * Outputs a message telling users that a specific plugin is required for
1125
		 * their theme. If appropriate, it includes a link to the form page where
1126
		 * users can install and activate the plugin.
1127
		 *
1128
		 * Returns early if we're on the Install page.
1129
		 *
1130
		 * @since 1.0.0
1131
		 *
1132
		 * @global object $current_screen
1133
		 *
1134
		 * @return null Returns early if we're on the Install page.
1135
		 */
1136
		public function notices() {
1137
			// Remove nag on the install page / Return early if the nag message has been dismissed or user < author.
1138
			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' ) ) ) {
1139
				return;
1140
			}
1141
1142
			// Store for the plugin slugs by message type.
1143
			$message = array();
1144
1145
			// Initialize counters used to determine plurality of action link texts.
1146
			$install_link_count          = 0;
1147
			$update_link_count           = 0;
1148
			$activate_link_count         = 0;
1149
			$total_required_action_count = 0;
1150
1151
			foreach ( $this->plugins as $slug => $plugin ) {
1152
				if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) {
1153
					continue;
1154
				}
1155
1156
				if ( ! $this->is_plugin_installed( $slug ) ) {
1157
					if ( current_user_can( 'install_plugins' ) ) {
1158
						$install_link_count++;
1159
1160
						if ( true === $plugin['required'] ) {
1161
							$message['notice_can_install_required'][] = $slug;
1162
						} else {
1163
							$message['notice_can_install_recommended'][] = $slug;
1164
						}
1165
					}
1166
					if ( true === $plugin['required'] ) {
1167
						$total_required_action_count++;
1168
					}
1169
				} else {
1170
					if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
1171
						if ( current_user_can( 'activate_plugins' ) ) {
1172
							$activate_link_count++;
1173
1174
							if ( true === $plugin['required'] ) {
1175
								$message['notice_can_activate_required'][] = $slug;
1176
							} else {
1177
								$message['notice_can_activate_recommended'][] = $slug;
1178
							}
1179
						}
1180
						if ( true === $plugin['required'] ) {
1181
							$total_required_action_count++;
1182
						}
1183
					}
1184
1185
					if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1186
1187
						if ( current_user_can( 'update_plugins' ) ) {
1188
							$update_link_count++;
1189
1190
							if ( $this->does_plugin_require_update( $slug ) ) {
1191
								$message['notice_ask_to_update'][] = $slug;
1192
							} elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
1193
								$message['notice_ask_to_update_maybe'][] = $slug;
1194
							}
1195
						}
1196
						if ( true === $plugin['required'] ) {
1197
							$total_required_action_count++;
1198
						}
1199
					}
1200
				}
1201
			}
1202
			unset( $slug, $plugin );
1203
1204
			// If we have notices to display, we move forward.
1205
			if ( ! empty( $message ) || $total_required_action_count > 0 ) {
1206
				krsort( $message ); // Sort messages.
1207
				$rendered = '';
1208
1209
				// As add_settings_error() wraps the final message in a <p> and as the final message can't be
1210
				// filtered, using <p>'s in our html would render invalid html output.
1211
				$line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
1212
1213
				if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
1214
					$rendered  = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] );
1215
					$rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
1216
				} else {
1217
1218
					// If dismissable is false and a message is set, output it now.
1219
					if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
1220
						$rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
1221
					}
1222
1223
					// Render the individual message lines for the notice.
1224
					foreach ( $message as $type => $plugin_group ) {
1225
						$linked_plugins = array();
1226
1227
						// Get the external info link for a plugin if one is available.
1228
						foreach ( $plugin_group as $plugin_slug ) {
1229
							$linked_plugins[] = $this->get_info_link( $plugin_slug );
1230
						}
1231
						unset( $plugin_slug );
1232
1233
						$count          = count( $plugin_group );
1234
						$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
1235
						$last_plugin    = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
1236
						$imploded       = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
1237
1238
						$rendered .= sprintf(
1239
							$line_template,
1240
							sprintf(
1241
								translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
1242
								$imploded,
1243
								$count
1244
							)
1245
						);
1246
1247
					}
1248
					unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
1249
1250
					$rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
1251
				}
1252
1253
				// Register the nag messages and prepare them to be processed.
1254
				add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
1255
			}
1256
1257
			// Admin options pages already output settings_errors, so this is to avoid duplication.
1258
			if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
1259
				$this->display_settings_errors();
1260
			}
1261
		}
1262
1263
		/**
1264
		 * Generate the user action links for the admin notice.
1265
		 *
1266
		 * @since 2.6.0
1267
		 *
1268
		 * @param int $install_count  Number of plugins to install.
1269
		 * @param int $update_count   Number of plugins to update.
1270
		 * @param int $activate_count Number of plugins to activate.
1271
		 * @param int $line_template  Template for the HTML tag to output a line.
1272
		 * @return string Action links.
1273
		 */
1274
		protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
1275
			// Setup action links.
1276
			$action_links = array(
1277
				'install'  => '',
1278
				'update'   => '',
1279
				'activate' => '',
1280
				'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>' : '',
1281
			);
1282
1283
			$link_template = '<a href="%2$s">%1$s</a>';
1284
1285
			if ( current_user_can( 'install_plugins' ) ) {
1286
				if ( $install_count > 0 ) {
1287
					$action_links['install'] = sprintf(
1288
						$link_template,
1289
						translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),
1290
						esc_url( $this->get_tgmpa_status_url( 'install' ) )
1291
					);
1292
				}
1293
				if ( $update_count > 0 ) {
1294
					$action_links['update'] = sprintf(
1295
						$link_template,
1296
						translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),
1297
						esc_url( $this->get_tgmpa_status_url( 'update' ) )
1298
					);
1299
				}
1300
			}
1301
1302
			if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
1303
				$action_links['activate'] = sprintf(
1304
					$link_template,
1305
					translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),
1306
					esc_url( $this->get_tgmpa_status_url( 'activate' ) )
1307
				);
1308
			}
1309
1310
			$action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
1311
1312
			$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
1313
1314
			if ( ! empty( $action_links ) ) {
1315
				$action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
1316
				return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
1317
			} else {
1318
				return '';
1319
			}
1320
		}
1321
1322
		/**
1323
		 * Get admin notice class.
1324
		 *
1325
		 * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1326
		 * (lowest supported version by TGMPA).
1327
		 *
1328
		 * @since 2.6.0
1329
		 *
1330
		 * @return string
1331
		 */
1332
		protected function get_admin_notice_class() {
1333
			if ( ! empty( $this->strings['nag_type'] ) ) {
1334
				return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1335
			} else {
1336
				if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1337
					return 'notice-warning';
1338
				} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1339
					return 'notice';
1340
				} else {
1341
					return 'updated';
1342
				}
1343
			}
1344
		}
1345
1346
		/**
1347
		 * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
1348
		 *
1349
		 * @since 2.5.0
1350
		 */
1351
		protected function display_settings_errors() {
1352
			global $wp_settings_errors;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

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