Completed
Push — develop ( 0f64a5...940a42 )
by Gary
9s
created

class-tgm-plugin-activation.php (2 issues)

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 ( ! class_exists( 'TGM_Plugin_Activation' ) ) {
36
37
	/**
38
	 * Automatic plugin installation and activation library.
39
	 *
40
	 * Creates a way to automatically install and activate plugins from within themes.
41
	 * The plugins can be either bundled, downloaded from the WordPress
42
	 * Plugin Repository or downloaded from another external source.
43
	 *
44
	 * @since 1.0.0
45
	 *
46
	 * @package TGM-Plugin-Activation
47
	 * @author  Thomas Griffin
48
	 * @author  Gary Jones
49
	 */
50
	class TGM_Plugin_Activation {
51
		/**
52
		 * TGMPA version number.
53
		 *
54
		 * @since 2.5.0
55
		 *
56
		 * @const string Version number.
57
		 */
58
		const TGMPA_VERSION = '2.6.1';
59
60
		/**
61
		 * Regular expression to test if a URL is a WP plugin repo URL.
62
		 *
63
		 * @const string Regex.
64
		 *
65
		 * @since 2.5.0
66
		 */
67
		const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|';
68
69
		/**
70
		 * Arbitrary regular expression to test if a string starts with a URL.
71
		 *
72
		 * @const string Regex.
73
		 *
74
		 * @since 2.5.0
75
		 */
76
		const IS_URL_REGEX = '|^http[s]?://|';
77
78
		/**
79
		 * Holds a copy of itself, so it can be referenced by the class name.
80
		 *
81
		 * @since 1.0.0
82
		 *
83
		 * @var TGM_Plugin_Activation
84
		 */
85
		public static $instance;
86
87
		/**
88
		 * Holds arrays of plugin details.
89
		 *
90
		 * @since 1.0.0
91
		 * @since 2.5.0 the array has the plugin slug as an associative key.
92
		 *
93
		 * @var array
94
		 */
95
		public $plugins = array();
96
97
		/**
98
		 * Holds arrays of plugin names to use to sort the plugins array.
99
		 *
100
		 * @since 2.5.0
101
		 *
102
		 * @var array
103
		 */
104
		protected $sort_order = array();
105
106
		/**
107
		 * Whether any plugins have the 'force_activation' setting set to true.
108
		 *
109
		 * @since 2.5.0
110
		 *
111
		 * @var bool
112
		 */
113
		protected $has_forced_activation = false;
114
115
		/**
116
		 * Whether any plugins have the 'force_deactivation' setting set to true.
117
		 *
118
		 * @since 2.5.0
119
		 *
120
		 * @var bool
121
		 */
122
		protected $has_forced_deactivation = false;
123
124
		/**
125
		 * Name of the unique ID to hash notices.
126
		 *
127
		 * @since 2.4.0
128
		 *
129
		 * @var string
130
		 */
131
		public $id = 'tgmpa';
132
133
		/**
134
		 * Name of the query-string argument for the admin page.
135
		 *
136
		 * @since 1.0.0
137
		 *
138
		 * @var string
139
		 */
140
		protected $menu = 'tgmpa-install-plugins';
141
142
		/**
143
		 * Parent menu file slug.
144
		 *
145
		 * @since 2.5.0
146
		 *
147
		 * @var string
148
		 */
149
		public $parent_slug = 'themes.php';
150
151
		/**
152
		 * Capability needed to view the plugin installation menu item.
153
		 *
154
		 * @since 2.5.0
155
		 *
156
		 * @var string
157
		 */
158
		public $capability = 'edit_theme_options';
159
160
		/**
161
		 * Default absolute path to folder containing bundled plugin zip files.
162
		 *
163
		 * @since 2.0.0
164
		 *
165
		 * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string.
166
		 */
167
		public $default_path = '';
168
169
		/**
170
		 * Flag to show admin notices or not.
171
		 *
172
		 * @since 2.1.0
173
		 *
174
		 * @var boolean
175
		 */
176
		public $has_notices = true;
177
178
		/**
179
		 * Flag to determine if the user can dismiss the notice nag.
180
		 *
181
		 * @since 2.4.0
182
		 *
183
		 * @var boolean
184
		 */
185
		public $dismissable = true;
186
187
		/**
188
		 * Message to be output above nag notice if dismissable is false.
189
		 *
190
		 * @since 2.4.0
191
		 *
192
		 * @var string
193
		 */
194
		public $dismiss_msg = '';
195
196
		/**
197
		 * Flag to set automatic activation of plugins. Off by default.
198
		 *
199
		 * @since 2.2.0
200
		 *
201
		 * @var boolean
202
		 */
203
		public $is_automatic = false;
204
205
		/**
206
		 * Optional message to display before the plugins table.
207
		 *
208
		 * @since 2.2.0
209
		 *
210
		 * @var string Message filtered by wp_kses_post(). Default is empty string.
211
		 */
212
		public $message = '';
213
214
		/**
215
		 * Holds configurable array of strings.
216
		 *
217
		 * Default values are added in the constructor.
218
		 *
219
		 * @since 2.0.0
220
		 *
221
		 * @var array
222
		 */
223
		public $strings = array();
224
225
		/**
226
		 * Holds the version of WordPress.
227
		 *
228
		 * @since 2.4.0
229
		 *
230
		 * @var int
231
		 */
232
		public $wp_version;
233
234
		/**
235
		 * Holds the hook name for the admin page.
236
		 *
237
		 * @since 2.5.0
238
		 *
239
		 * @var string
240
		 */
241
		public $page_hook;
242
243
		/**
244
		 * Adds a reference of this object to $instance, populates default strings,
245
		 * does the tgmpa_init action hook, and hooks in the interactions to init.
246
		 *
247
		 * {@internal This method should be `protected`, but as too many TGMPA implementations
248
		 * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues.
249
		 * Reverted back to public for the time being.}}
250
		 *
251
		 * @since 1.0.0
252
		 *
253
		 * @see TGM_Plugin_Activation::init()
254
		 */
255
		public function __construct() {
256
			// Set the current WordPress version.
257
			$this->wp_version = $GLOBALS['wp_version'];
258
259
			// Announce that the class is ready, and pass the object (for advanced use).
260
			do_action_ref_array( 'tgmpa_init', array( $this ) );
261
262
			/*
263
			 * Load our text domain and allow for overloading the fall-back file.
264
			 *
265
			 * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
266
			 * generator on the website.}}
267
			 */
268
			add_action( 'init', array( $this, 'load_textdomain' ), 5 );
269
			add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 );
270
271
			// When the rest of WP has loaded, kick-start the rest of the class.
272
			add_action( 'init', array( $this, 'init' ) );
273
		}
274
275
		/**
276
		 * Magic method to (not) set protected properties from outside of this class.
277
		 *
278
		 * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6  where the `menu` property
279
		 * is being assigned rather than tested in a conditional, effectively rendering it useless.
280
		 * This 'hack' prevents this from happening.}}
281
		 *
282
		 * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593
283
		 *
284
		 * @since 2.5.2
285
		 *
286
		 * @param string $name  Name of an inaccessible property.
287
		 * @param mixed  $value Value to assign to the property.
288
		 * @return void  Silently fail to set the property when this is tried from outside of this class context.
289
		 *               (Inside this class context, the __set() method if not used as there is direct access.)
290
		 */
291
		public function __set( $name, $value ) {
292
			return;
293
		}
294
295
		/**
296
		 * Magic method to get the value of a protected property outside of this class context.
297
		 *
298
		 * @since 2.5.2
299
		 *
300
		 * @param string $name Name of an inaccessible property.
301
		 * @return mixed The property value.
302
		 */
303
		public function __get( $name ) {
304
			return $this->{$name};
305
		}
306
307
		/**
308
		 * Initialise the interactions between this class and WordPress.
309
		 *
310
		 * Hooks in three new methods for the class: admin_menu, notices and styles.
311
		 *
312
		 * @since 2.0.0
313
		 *
314
		 * @see TGM_Plugin_Activation::admin_menu()
315
		 * @see TGM_Plugin_Activation::notices()
316
		 * @see TGM_Plugin_Activation::styles()
317
		 */
318
		public function init() {
319
			/**
320
			 * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter
321
			 * you can overrule that behaviour.
322
			 *
323
			 * @since 2.5.0
324
			 *
325
			 * @param bool $load Whether or not TGMPA should load.
326
			 *                   Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`.
327
			 */
328
			if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) {
329
				return;
330
			}
331
332
			// Load class strings.
333
			$this->strings = array(
334
				'page_title'                      => __( 'Install Required Plugins', 'tgmpa' ),
335
				'menu_title'                      => __( 'Install Plugins', 'tgmpa' ),
336
				/* translators: %s: plugin name. */
337
				'installing'                      => __( 'Installing Plugin: %s', 'tgmpa' ),
338
				/* translators: %s: plugin name. */
339
				'updating'                        => __( 'Updating Plugin: %s', 'tgmpa' ),
340
				'oops'                            => __( 'Something went wrong with the plugin API.', 'tgmpa' ),
341
				/* translators: 1: plugin name(s). */
342
				'notice_can_install_required'     => _n_noop(
343
					'This theme requires the following plugin: %1$s.',
344
					'This theme requires the following plugins: %1$s.',
345
					'tgmpa'
346
				),
347
				/* translators: 1: plugin name(s). */
348
				'notice_can_install_recommended'  => _n_noop(
349
					'This theme recommends the following plugin: %1$s.',
350
					'This theme recommends the following plugins: %1$s.',
351
					'tgmpa'
352
				),
353
				/* translators: 1: plugin name(s). */
354
				'notice_ask_to_update'            => _n_noop(
355
					'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',
356
					'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',
357
					'tgmpa'
358
				),
359
				/* translators: 1: plugin name(s). */
360
				'notice_ask_to_update_maybe'      => _n_noop(
361
					'There is an update available for: %1$s.',
362
					'There are updates available for the following plugins: %1$s.',
363
					'tgmpa'
364
				),
365
				/* translators: 1: plugin name(s). */
366
				'notice_can_activate_required'    => _n_noop(
367
					'The following required plugin is currently inactive: %1$s.',
368
					'The following required plugins are currently inactive: %1$s.',
369
					'tgmpa'
370
				),
371
				/* translators: 1: plugin name(s). */
372
				'notice_can_activate_recommended' => _n_noop(
373
					'The following recommended plugin is currently inactive: %1$s.',
374
					'The following recommended plugins are currently inactive: %1$s.',
375
					'tgmpa'
376
				),
377
				'install_link'                    => _n_noop(
378
					'Begin installing plugin',
379
					'Begin installing plugins',
380
					'tgmpa'
381
				),
382
				'update_link'                     => _n_noop(
383
					'Begin updating plugin',
384
					'Begin updating plugins',
385
					'tgmpa'
386
				),
387
				'activate_link'                   => _n_noop(
388
					'Begin activating plugin',
389
					'Begin activating plugins',
390
					'tgmpa'
391
				),
392
				'return'                          => __( 'Return to Required Plugins Installer', 'tgmpa' ),
393
				'dashboard'                       => __( 'Return to the Dashboard', 'tgmpa' ),
394
				'plugin_activated'                => __( 'Plugin activated successfully.', 'tgmpa' ),
395
				'activated_successfully'          => __( 'The following plugin was activated successfully:', 'tgmpa' ),
396
				/* translators: 1: plugin name. */
397
				'plugin_already_active'           => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ),
398
				/* translators: 1: plugin name. */
399
				'plugin_needs_higher_version'     => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ),
400
				/* translators: 1: dashboard link. */
401
				'complete'                        => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ),
402
				'dismiss'                         => __( 'Dismiss this notice', 'tgmpa' ),
403
				'notice_cannot_install_activate'  => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ),
404
				'contact_admin'                   => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),
405
			);
406
407
			do_action( 'tgmpa_register' );
408
409
			/* After this point, the plugins should be registered and the configuration set. */
410
411
			// Proceed only if we have plugins to handle.
412
			if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
413
				return;
414
			}
415
416
			// Set up the menu and notices if we still have outstanding actions.
417
			if ( true !== $this->is_tgmpa_complete() ) {
418
				// Sort the plugins.
419
				array_multisort( $this->sort_order, SORT_ASC, $this->plugins );
420
421
				add_action( 'admin_menu', array( $this, 'admin_menu' ) );
422
				add_action( 'admin_head', array( $this, 'dismiss' ) );
423
424
				// Prevent the normal links from showing underneath a single install/update page.
425
				add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
426
				add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) );
427
428
				if ( $this->has_notices ) {
429
					add_action( 'admin_notices', array( $this, 'notices' ) );
430
					add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
431
					add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
432
				}
433
			}
434
435
			// If needed, filter plugin action links.
436
			add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 );
437
438
			// Make sure things get reset on switch theme.
439
			add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
440
441
			if ( $this->has_notices ) {
442
				add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
443
			}
444
445
			// Setup the force activation hook.
446
			if ( true === $this->has_forced_activation ) {
447
				add_action( 'admin_init', array( $this, 'force_activation' ) );
448
			}
449
450
			// Setup the force deactivation hook.
451
			if ( true === $this->has_forced_deactivation ) {
452
				add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
453
			}
454
455
			// Add CSS for the TGMPA admin page.
456
			add_action( 'admin_head', array( $this, 'admin_css' ) );
457
		}
458
459
		/**
460
		 * Load translations.
461
		 *
462
		 * @since 2.6.0
463
		 *
464
		 * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to
465
		 * get round the different ways of handling the path and deprecated notices being thrown
466
		 * and such. For plugins, the actual file name will be corrected by a filter.}}
467
		 *
468
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
469
		 * generator on the website.}}
470
		 */
471
		public function load_textdomain() {
472
			if ( is_textdomain_loaded( 'tgmpa' ) ) {
473
				return;
474
			}
475
476
			if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) {
477
				// Plugin, we'll need to adjust the file name.
478
				add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 );
479
				load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
480
				remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 );
481
			} else {
482
				load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
483
			}
484
		}
485
486
		/**
487
		 * Correct the .mo file name for (must-use) plugins.
488
		 *
489
		 * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`.
490
		 *
491
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
492
		 * generator on the website.}}
493
		 *
494
		 * @since 2.6.0
495
		 *
496
		 * @param string $mofile Full path to the target mofile.
497
		 * @param string $domain The domain for which a language file is being loaded.
498
		 * @return string $mofile
499
		 */
500
		public function correct_plugin_mofile( $mofile, $domain ) {
501
			// Exit early if not our domain (just in case).
502
			if ( 'tgmpa' !== $domain ) {
503
				return $mofile;
504
			}
505
			return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile );
506
		}
507
508
		/**
509
		 * Potentially overload the fall-back translation file for the current language.
510
		 *
511
		 * WP, by default since WP 3.7, will load a local translation first and if none
512
		 * can be found, will try and find a translation in the /wp-content/languages/ directory.
513
		 * As this library is theme/plugin agnostic, translation files for TGMPA can exist both
514
		 * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory.
515
		 *
516
		 * This method makes sure both directories are checked.
517
		 *
518
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
519
		 * generator on the website.}}
520
		 *
521
		 * @since 2.6.0
522
		 *
523
		 * @param string $mofile Full path to the target mofile.
524
		 * @param string $domain The domain for which a language file is being loaded.
525
		 * @return string $mofile
526
		 */
527
		public function overload_textdomain_mofile( $mofile, $domain ) {
528
			// Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
529
			if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) {
530
				return $mofile;
531
			}
532
533
			// Current fallback file is not valid, let's try the alternative option.
534
			if ( false !== strpos( $mofile, '/themes/' ) ) {
535
				return str_replace( '/themes/', '/plugins/', $mofile );
536
			} elseif ( false !== strpos( $mofile, '/plugins/' ) ) {
537
				return str_replace( '/plugins/', '/themes/', $mofile );
538
			} else {
539
				return $mofile;
540
			}
541
		}
542
543
		/**
544
		 * Hook in plugin action link filters for the WP native plugins page.
545
		 *
546
		 * - Prevent activation of plugins which don't meet the minimum version requirements.
547
		 * - Prevent deactivation of force-activated plugins.
548
		 * - Add update notice if update available.
549
		 *
550
		 * @since 2.5.0
551
		 */
552
		public function add_plugin_action_link_filters() {
553
			foreach ( $this->plugins as $slug => $plugin ) {
554
				if ( false === $this->can_plugin_activate( $slug ) ) {
555
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 );
556
				}
557
558
				if ( true === $plugin['force_activation'] ) {
559
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 );
560
				}
561
562
				if ( false !== $this->does_plugin_require_update( $slug ) ) {
563
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 );
564
				}
565
			}
566
		}
567
568
		/**
569
		 * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the
570
		 * minimum version requirements.
571
		 *
572
		 * @since 2.5.0
573
		 *
574
		 * @param array $actions Action links.
575
		 * @return array
576
		 */
577
		public function filter_plugin_action_links_activate( $actions ) {
578
			unset( $actions['activate'] );
579
580
			return $actions;
581
		}
582
583
		/**
584
		 * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate.
585
		 *
586
		 * @since 2.5.0
587
		 *
588
		 * @param array $actions Action links.
589
		 * @return array
590
		 */
591
		public function filter_plugin_action_links_deactivate( $actions ) {
592
			unset( $actions['deactivate'] );
593
594
			return $actions;
595
		}
596
597
		/**
598
		 * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the
599
		 * minimum version requirements.
600
		 *
601
		 * @since 2.5.0
602
		 *
603
		 * @param array $actions Action links.
604
		 * @return array
605
		 */
606
		public function filter_plugin_action_links_update( $actions ) {
607
			$actions['update'] = sprintf(
608
				'<a href="%1$s" title="%2$s" class="edit">%3$s</a>',
609
				esc_url( $this->get_tgmpa_status_url( 'update' ) ),
610
				esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ),
611
				esc_html__( 'Update Required', 'tgmpa' )
612
			);
613
614
			return $actions;
615
		}
616
617
		/**
618
		 * Handles calls to show plugin information via links in the notices.
619
		 *
620
		 * We get the links in the admin notices to point to the TGMPA page, rather
621
		 * than the typical plugin-install.php file, so we can prepare everything
622
		 * beforehand.
623
		 *
624
		 * WP does not make it easy to show the plugin information in the thickbox -
625
		 * here we have to require a file that includes a function that does the
626
		 * main work of displaying it, enqueue some styles, set up some globals and
627
		 * finally call that function before exiting.
628
		 *
629
		 * Down right easy once you know how...
630
		 *
631
		 * Returns early if not the TGMPA page.
632
		 *
633
		 * @since 2.1.0
634
		 *
635
		 * @global string $tab Used as iframe div class names, helps with styling
636
		 * @global string $body_id Used as the iframe body ID, helps with styling
637
		 *
638
		 * @return null Returns early if not the TGMPA page.
639
		 */
640
		public function admin_init() {
641
			if ( ! $this->is_tgmpa_page() ) {
642
				return;
643
			}
644
645
			if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) {
646
				// Needed for install_plugin_information().
647
				require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
648
649
				wp_enqueue_style( 'plugin-install' );
650
651
				global $tab, $body_id;
652
				$body_id = 'plugin-information';
653
				// @codingStandardsIgnoreStart
654
				$tab     = 'plugin-information';
655
				// @codingStandardsIgnoreEnd
656
657
				install_plugin_information();
658
659
				exit;
660
			}
661
		}
662
663
		/**
664
		 * Enqueue thickbox scripts/styles for plugin info.
665
		 *
666
		 * Thickbox is not automatically included on all admin pages, so we must
667
		 * manually enqueue it for those pages.
668
		 *
669
		 * Thickbox is only loaded if the user has not dismissed the admin
670
		 * notice or if there are any plugins left to install and activate.
671
		 *
672
		 * @since 2.1.0
673
		 */
674
		public function thickbox() {
675
			if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
676
				add_thickbox();
677
			}
678
		}
679
680
		/**
681
		 * Adds submenu page if there are plugin actions to take.
682
		 *
683
		 * This method adds the submenu page letting users know that a required
684
		 * plugin needs to be installed.
685
		 *
686
		 * This page disappears once the plugin has been installed and activated.
687
		 *
688
		 * @since 1.0.0
689
		 *
690
		 * @see TGM_Plugin_Activation::init()
691
		 * @see TGM_Plugin_Activation::install_plugins_page()
692
		 *
693
		 * @return null Return early if user lacks capability to install a plugin.
694
		 */
695
		public function admin_menu() {
696
			// Make sure privileges are correct to see the page.
697
			if ( ! current_user_can( 'install_plugins' ) ) {
698
				return;
699
			}
700
701
			$args = apply_filters(
702
				'tgmpa_admin_menu_args',
703
				array(
704
					'parent_slug' => $this->parent_slug,                     // Parent Menu slug.
705
					'page_title'  => $this->strings['page_title'],           // Page title.
706
					'menu_title'  => $this->strings['menu_title'],           // Menu title.
707
					'capability'  => $this->capability,                      // Capability.
708
					'menu_slug'   => $this->menu,                            // Menu slug.
709
					'function'    => array( $this, 'install_plugins_page' ), // Callback.
710
				)
711
			);
712
713
			$this->add_admin_menu( $args );
714
		}
715
716
		/**
717
		 * Add the menu item.
718
		 *
719
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
720
		 * generator on the website.}}
721
		 *
722
		 * @since 2.5.0
723
		 *
724
		 * @param array $args Menu item configuration.
725
		 */
726
		protected function add_admin_menu( array $args ) {
727
			if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) {
728
				_deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) );
729
			}
730
731
			if ( 'themes.php' === $this->parent_slug ) {
732
				$this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
733
			} else {
734
				$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'] );
735
			}
736
		}
737
738
		/**
739
		 * Echoes plugin installation form.
740
		 *
741
		 * This method is the callback for the admin_menu method function.
742
		 * This displays the admin page and form area where the user can select to install and activate the plugin.
743
		 * Aborts early if we're processing a plugin installation action.
744
		 *
745
		 * @since 1.0.0
746
		 *
747
		 * @return null Aborts early if we're processing a plugin installation action.
748
		 */
749
		public function install_plugins_page() {
750
			// Store new instance of plugin table in object.
751
			$plugin_table = new TGMPA_List_Table;
752
753
			// Return early if processing a plugin installation action.
754
			if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
755
				return;
756
			}
757
758
			// Force refresh of available plugin information so we'll know about manual updates/deletes.
759
			wp_clean_plugins_cache( false );
760
761
			?>
762
			<div class="tgmpa wrap">
763
				<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
764
				<?php $plugin_table->prepare_items(); ?>
765
766
				<?php
767
				if ( ! empty( $this->message ) && is_string( $this->message ) ) {
768
					echo wp_kses_post( $this->message );
769
				}
770
				?>
771
				<?php $plugin_table->views(); ?>
772
773
				<form id="tgmpa-plugins" action="" method="post">
774
					<input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
775
					<input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
776
					<?php $plugin_table->display(); ?>
777
				</form>
778
			</div>
779
			<?php
780
		}
781
782
		/**
783
		 * Installs, updates or activates a plugin depending on the action link clicked by the user.
784
		 *
785
		 * Checks the $_GET variable to see which actions have been
786
		 * passed and responds with the appropriate method.
787
		 *
788
		 * Uses WP_Filesystem to process and handle the plugin installation
789
		 * method.
790
		 *
791
		 * @since 1.0.0
792
		 *
793
		 * @uses WP_Filesystem
794
		 * @uses WP_Error
795
		 * @uses WP_Upgrader
796
		 * @uses Plugin_Upgrader
797
		 * @uses Plugin_Installer_Skin
798
		 * @uses Plugin_Upgrader_Skin
799
		 *
800
		 * @return boolean True on success, false on failure.
801
		 */
802
		protected function do_plugin_install() {
803
			if ( empty( $_GET['plugin'] ) ) {
804
				return false;
805
			}
806
807
			// All plugin information will be stored in an array for processing.
808
			$slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
809
810
			if ( ! isset( $this->plugins[ $slug ] ) ) {
811
				return false;
812
			}
813
814
			// Was an install or upgrade action link clicked?
815
			if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
816
817
				$install_type = 'install';
818
				if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
819
					$install_type = 'update';
820
				}
821
822
				check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
823
824
				// Pass necessary information via URL if WP_Filesystem is needed.
825
				$url = wp_nonce_url(
826
					add_query_arg(
827
						array(
828
							'plugin'                 => urlencode( $slug ),
829
							'tgmpa-' . $install_type => $install_type . '-plugin',
830
						),
831
						$this->get_tgmpa_url()
832
					),
833
					'tgmpa-' . $install_type,
834
					'tgmpa-nonce'
835
				);
836
837
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
838
839
				$creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() );
840
				if ( false === $creds ) {
841
					return true;
842
				}
843
844
				if ( ! WP_Filesystem( $creds ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
845
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
846
					return true;
847
				}
848
849
				/* If we arrive here, we have the filesystem. */
850
851
				// Prep variables for Plugin_Installer_Skin class.
852
				$extra         = array();
853
				$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
854
				$source        = $this->get_download_url( $slug );
855
				$api           = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
856
				$api           = ( false !== $api ) ? $api : null;
857
858
				$url = add_query_arg(
859
					array(
860
						'action' => $install_type . '-plugin',
861
						'plugin' => urlencode( $slug ),
862
					),
863
					'update.php'
864
				);
865
866
				if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
867
					require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
868
				}
869
870
				$title     = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
871
				$skin_args = array(
872
					'type'   => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
873
					'title'  => sprintf( $title, $this->plugins[ $slug ]['name'] ),
874
					'url'    => esc_url_raw( $url ),
875
					'nonce'  => $install_type . '-plugin_' . $slug,
876
					'plugin' => '',
877
					'api'    => $api,
878
					'extra'  => $extra,
879
				);
880
				unset( $title );
881
882
				if ( 'update' === $install_type ) {
883
					$skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
884
					$skin                = new Plugin_Upgrader_Skin( $skin_args );
885
				} else {
886
					$skin = new Plugin_Installer_Skin( $skin_args );
887
				}
888
889
				// Create a new instance of Plugin_Upgrader.
890
				$upgrader = new Plugin_Upgrader( $skin );
891
892
				// Perform the action and install the plugin from the $source urldecode().
893
				add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
894
895
				if ( 'update' === $install_type ) {
896
					// Inject our info into the update transient.
897
					$to_inject                    = array(
898
						$slug => $this->plugins[ $slug ],
899
					);
900
					$to_inject[ $slug ]['source'] = $source;
901
					$this->inject_update_info( $to_inject );
902
903
					$upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
904
				} else {
905
					$upgrader->install( $source );
906
				}
907
908
				remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 );
909
910
				// Make sure we have the correct file path now the plugin is installed/updated.
911
				$this->populate_file_path( $slug );
912
913
				// Only activate plugins if the config option is set to true and the plugin isn't
914
				// already active (upgrade).
915
				if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
916
					$plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
917
					if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
918
						return true; // Finish execution of the function early as we encountered an error.
919
					}
920
				}
921
922
				$this->show_tgmpa_version();
923
924
				// Display message based on if all plugins are now active or not.
925
				if ( $this->is_tgmpa_complete() ) {
926
					echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html( $this->strings['dashboard'] ) . '</a>' ), '</p>';
927
					echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
928
				} else {
929
					echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
930
				}
931
932
				return true;
933
			} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
934
				// Activate action link was clicked.
935
				check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
936
937
				if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
938
					return true; // Finish execution of the function early as we encountered an error.
939
				}
940
			} // End if().
941
942
			return false;
943
		}
944
945
		/**
946
		 * Inject information into the 'update_plugins' site transient as WP checks that before running an update.
947
		 *
948
		 * @since 2.5.0
949
		 *
950
		 * @param array $plugins The plugin information for the plugins which are to be updated.
951
		 */
952
		public function inject_update_info( $plugins ) {
953
			$repo_updates = get_site_transient( 'update_plugins' );
954
955
			if ( ! is_object( $repo_updates ) ) {
956
				$repo_updates = new stdClass;
957
			}
958
959
			foreach ( $plugins as $slug => $plugin ) {
960
				$file_path = $plugin['file_path'];
961
962
				if ( empty( $repo_updates->response[ $file_path ] ) ) {
963
					$repo_updates->response[ $file_path ] = new stdClass;
964
				}
965
966
				// We only really need to set package, but let's do all we can in case WP changes something.
967
				$repo_updates->response[ $file_path ]->slug        = $slug;
968
				$repo_updates->response[ $file_path ]->plugin      = $file_path;
969
				$repo_updates->response[ $file_path ]->new_version = $plugin['version'];
970
				$repo_updates->response[ $file_path ]->package     = $plugin['source'];
971
				if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
972
					$repo_updates->response[ $file_path ]->url = $plugin['external_url'];
973
				}
974
			}
975
976
			set_site_transient( 'update_plugins', $repo_updates );
977
		}
978
979
		/**
980
		 * Adjust the plugin directory name if necessary.
981
		 *
982
		 * The final destination directory of a plugin is based on the subdirectory name found in the
983
		 * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
984
		 * subdirectory name is not the same as the expected slug and the plugin will not be recognized
985
		 * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
986
		 * the expected plugin slug.
987
		 *
988
		 * @since 2.5.0
989
		 *
990
		 * @param string       $source        Path to upgrade/zip-file-name.tmp/subdirectory/.
991
		 * @param string       $remote_source Path to upgrade/zip-file-name.tmp.
992
		 * @param \WP_Upgrader $upgrader      Instance of the upgrader which installs the plugin.
993
		 * @return string $source
994
		 */
995
		public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
996
			if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
997
				return $source;
998
			}
999
1000
			// Check for single file plugins.
1001
			$source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
1002
			if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
1003
				return $source;
1004
			}
1005
1006
			// Multi-file plugin, let's see if the directory is correctly named.
1007
			$desired_slug = '';
1008
1009
			// Figure out what the slug is supposed to be.
1010
			if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
1011
				$desired_slug = $upgrader->skin->options['extra']['slug'];
1012
			} else {
1013
				// Bulk installer contains less info, so fall back on the info registered here.
1014
				foreach ( $this->plugins as $slug => $plugin ) {
1015
					if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
1016
						$desired_slug = $slug;
1017
						break;
1018
					}
1019
				}
1020
				unset( $slug, $plugin );
1021
			}
1022
1023
			if ( ! empty( $desired_slug ) ) {
1024
				$subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
1025
1026
				if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
1027
					$from_path = untrailingslashit( $source );
1028
					$to_path   = trailingslashit( $remote_source ) . $desired_slug;
1029
1030
					if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
1031
						return trailingslashit( $to_path );
1032 View Code Duplication
					} else {
1033
						return new WP_Error(
1034
							'rename_failed',
1035
							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' ),
1036
							array(
1037
								'found'    => $subdir_name,
1038
								'expected' => $desired_slug,
1039
							)
1040
						);
1041
					}
1042 View Code Duplication
				} elseif ( empty( $subdir_name ) ) {
1043
					return new WP_Error(
1044
						'packaged_wrong',
1045
						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' ),
1046
						array(
1047
							'found'    => $subdir_name,
1048
							'expected' => $desired_slug,
1049
						)
1050
					);
1051
				}
1052
			}
1053
1054
			return $source;
1055
		}
1056
1057
		/**
1058
		 * Activate a single plugin and send feedback about the result to the screen.
1059
		 *
1060
		 * @since 2.5.0
1061
		 *
1062
		 * @param string $file_path Path within wp-plugins/ to main plugin file.
1063
		 * @param string $slug      Plugin slug.
1064
		 * @param bool   $automatic Whether this is an automatic activation after an install. Defaults to false.
1065
		 *                          This determines the styling of the output messages.
1066
		 * @return bool False if an error was encountered, true otherwise.
1067
		 */
1068
		protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
1069
			if ( $this->can_plugin_activate( $slug ) ) {
1070
				$activate = activate_plugin( $file_path );
1071
1072
				if ( is_wp_error( $activate ) ) {
1073
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
1074
						'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
1075
1076
					return false; // End it here if there is an error with activation.
1077
				} else {
1078
					if ( ! $automatic ) {
1079
						// Make sure message doesn't display again if bulk activation is performed
1080
						// immediately after a single activation.
1081
						if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1082
							echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
1083
						}
1084
					} else {
1085
						// Simpler message layout for use on the plugin install page.
1086
						echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
1087
					}
1088
				}
1089 View Code Duplication
			} elseif ( $this->is_plugin_active( $slug ) ) {
1090
				// No simpler message format provided as this message should never be encountered
1091
				// on the plugin install page.
1092
				echo '<div id="message" class="error"><p>',
1093
					sprintf(
1094
						esc_html( $this->strings['plugin_already_active'] ),
1095
						'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1096
					),
1097
					'</p></div>';
1098
			} elseif ( $this->does_plugin_require_update( $slug ) ) {
1099
				if ( ! $automatic ) {
1100
					// Make sure message doesn't display again if bulk activation is performed
1101
					// immediately after a single activation.
1102 View Code Duplication
					if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1103
						echo '<div id="message" class="error"><p>',
1104
							sprintf(
1105
								esc_html( $this->strings['plugin_needs_higher_version'] ),
1106
								'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1107
							),
1108
							'</p></div>';
1109
					}
1110
				} else {
1111
					// Simpler message layout for use on the plugin install page.
1112
					echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
1113
				}
1114
			} // End if().
1115
1116
			return true;
1117
		}
1118
1119
		/**
1120
		 * Echoes required plugin notice.
1121
		 *
1122
		 * Outputs a message telling users that a specific plugin is required for
1123
		 * their theme. If appropriate, it includes a link to the form page where
1124
		 * users can install and activate the plugin.
1125
		 *
1126
		 * Returns early if we're on the Install page.
1127
		 *
1128
		 * @since 1.0.0
1129
		 *
1130
		 * @global object $current_screen
1131
		 *
1132
		 * @return null Returns early if we're on the Install page.
1133
		 */
1134
		public function notices() {
1135
			// Remove nag on the install page / Return early if the nag message has been dismissed or user < author.
1136
			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' ) ) ) {
1137
				return;
1138
			}
1139
1140
			// Store for the plugin slugs by message type.
1141
			$message = array();
1142
1143
			// Initialize counters used to determine plurality of action link texts.
1144
			$install_link_count          = 0;
1145
			$update_link_count           = 0;
1146
			$activate_link_count         = 0;
1147
			$total_required_action_count = 0;
1148
1149
			foreach ( $this->plugins as $slug => $plugin ) {
1150
				if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) {
1151
					continue;
1152
				}
1153
1154
				if ( ! $this->is_plugin_installed( $slug ) ) {
1155
					if ( current_user_can( 'install_plugins' ) ) {
1156
						$install_link_count++;
1157
1158
						if ( true === $plugin['required'] ) {
1159
							$message['notice_can_install_required'][] = $slug;
1160
						} else {
1161
							$message['notice_can_install_recommended'][] = $slug;
1162
						}
1163
					}
1164
					if ( true === $plugin['required'] ) {
1165
						$total_required_action_count++;
1166
					}
1167
				} else {
1168
					if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
1169
						if ( current_user_can( 'activate_plugins' ) ) {
1170
							$activate_link_count++;
1171
1172
							if ( true === $plugin['required'] ) {
1173
								$message['notice_can_activate_required'][] = $slug;
1174
							} else {
1175
								$message['notice_can_activate_recommended'][] = $slug;
1176
							}
1177
						}
1178
						if ( true === $plugin['required'] ) {
1179
							$total_required_action_count++;
1180
						}
1181
					}
1182
1183
					if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1184
1185
						if ( current_user_can( 'update_plugins' ) ) {
1186
							$update_link_count++;
1187
1188
							if ( $this->does_plugin_require_update( $slug ) ) {
1189
								$message['notice_ask_to_update'][] = $slug;
1190
							} elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
1191
								$message['notice_ask_to_update_maybe'][] = $slug;
1192
							}
1193
						}
1194
						if ( true === $plugin['required'] ) {
1195
							$total_required_action_count++;
1196
						}
1197
					}
1198
				} // End if().
1199
			} // End foreach().
1200
			unset( $slug, $plugin );
1201
1202
			// If we have notices to display, we move forward.
1203
			if ( ! empty( $message ) || $total_required_action_count > 0 ) {
1204
				krsort( $message ); // Sort messages.
1205
				$rendered = '';
1206
1207
				// As add_settings_error() wraps the final message in a <p> and as the final message can't be
1208
				// filtered, using <p>'s in our html would render invalid html output.
1209
				$line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
1210
1211
				if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
1212
					$rendered  = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] );
1213
					$rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
1214
				} else {
1215
1216
					// If dismissable is false and a message is set, output it now.
1217
					if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
1218
						$rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
1219
					}
1220
1221
					// Render the individual message lines for the notice.
1222
					foreach ( $message as $type => $plugin_group ) {
1223
						$linked_plugins = array();
1224
1225
						// Get the external info link for a plugin if one is available.
1226
						foreach ( $plugin_group as $plugin_slug ) {
1227
							$linked_plugins[] = $this->get_info_link( $plugin_slug );
1228
						}
1229
						unset( $plugin_slug );
1230
1231
						$count          = count( $plugin_group );
1232
						$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
1233
						$last_plugin    = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
1234
						$imploded       = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
1235
1236
						$rendered .= sprintf(
1237
							$line_template,
1238
							sprintf(
1239
								translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
1240
								$imploded,
1241
								$count
1242
							)
1243
						);
1244
1245
					}
1246
					unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
1247
1248
					$rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
1249
				} // End if().
1250
1251
				// Register the nag messages and prepare them to be processed.
1252
				add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
1253
			} // End if().
1254
1255
			// Admin options pages already output settings_errors, so this is to avoid duplication.
1256
			if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
1257
				$this->display_settings_errors();
1258
			}
1259
		}
1260
1261
		/**
1262
		 * Generate the user action links for the admin notice.
1263
		 *
1264
		 * @since 2.6.0
1265
		 *
1266
		 * @param int $install_count  Number of plugins to install.
1267
		 * @param int $update_count   Number of plugins to update.
1268
		 * @param int $activate_count Number of plugins to activate.
1269
		 * @param int $line_template  Template for the HTML tag to output a line.
1270
		 * @return string Action links.
1271
		 */
1272
		protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
1273
			// Setup action links.
1274
			$action_links = array(
1275
				'install'  => '',
1276
				'update'   => '',
1277
				'activate' => '',
1278
				'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>' : '',
1279
			);
1280
1281
			$link_template = '<a href="%2$s">%1$s</a>';
1282
1283
			if ( current_user_can( 'install_plugins' ) ) {
1284
				if ( $install_count > 0 ) {
1285
					$action_links['install'] = sprintf(
1286
						$link_template,
1287
						translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),
1288
						esc_url( $this->get_tgmpa_status_url( 'install' ) )
1289
					);
1290
				}
1291
				if ( $update_count > 0 ) {
1292
					$action_links['update'] = sprintf(
1293
						$link_template,
1294
						translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),
1295
						esc_url( $this->get_tgmpa_status_url( 'update' ) )
1296
					);
1297
				}
1298
			}
1299
1300
			if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
1301
				$action_links['activate'] = sprintf(
1302
					$link_template,
1303
					translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),
1304
					esc_url( $this->get_tgmpa_status_url( 'activate' ) )
1305
				);
1306
			}
1307
1308
			$action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
1309
1310
			$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
1311
1312
			if ( ! empty( $action_links ) ) {
1313
				$action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
1314
				return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
1315
			} else {
1316
				return '';
1317
			}
1318
		}
1319
1320
		/**
1321
		 * Get admin notice class.
1322
		 *
1323
		 * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1324
		 * (lowest supported version by TGMPA).
1325
		 *
1326
		 * @since 2.6.0
1327
		 *
1328
		 * @return string
1329
		 */
1330
		protected function get_admin_notice_class() {
1331
			if ( ! empty( $this->strings['nag_type'] ) ) {
1332
				return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1333
			} else {
1334
				if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1335
					return 'notice-warning';
1336
				} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1337
					return 'notice';
1338
				} else {
1339
					return 'updated';
1340
				}
1341
			}
1342
		}
1343
1344
		/**
1345
		 * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
1346
		 *
1347
		 * @since 2.5.0
1348
		 */
1349
		protected function display_settings_errors() {
1350
			global $wp_settings_errors;
1351
1352
			settings_errors( 'tgmpa' );
1353
1354
			foreach ( (array) $wp_settings_errors as $key => $details ) {
1355
				if ( 'tgmpa' === $details['setting'] ) {
1356
					unset( $wp_settings_errors[ $key ] );
1357
					break;
1358
				}
1359
			}
1360
		}
1361
1362
		/**
1363
		 * Register dismissal of admin notices.
1364
		 *
1365
		 * Acts on the dismiss link in the admin nag messages.
1366
		 * If clicked, the admin notice disappears and will no longer be visible to this user.
1367
		 *
1368
		 * @since 2.1.0
1369
		 */
1370
		public function dismiss() {
1371
			if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) {
1372
				update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 );
1373
			}
1374
		}
1375
1376
		/**
1377
		 * Add individual plugin to our collection of plugins.
1378
		 *
1379
		 * If the required keys are not set or the plugin has already
1380
		 * been registered, the plugin is not added.
1381
		 *
1382
		 * @since 2.0.0
1383
		 *
1384
		 * @param array|null $plugin Array of plugin arguments or null if invalid argument.
1385
		 * @return null Return early if incorrect argument.
1386
		 */
1387
		public function register( $plugin ) {
1388
			if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) {
1389
				return;
1390
			}
1391
1392
			if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
1393
				return;
1394
			}
1395
1396
			$defaults = array(
1397
				'name'               => '',      // String
1398
				'slug'               => '',      // String
1399
				'source'             => 'repo',  // String
1400
				'required'           => false,   // Boolean
1401
				'version'            => '',      // String
1402
				'force_activation'   => false,   // Boolean
1403
				'force_deactivation' => false,   // Boolean
1404
				'external_url'       => '',      // String
1405
				'is_callable'        => '',      // String|Array.
1406
			);
1407
1408
			// Prepare the received data.
1409
			$plugin = wp_parse_args( $plugin, $defaults );
1410
1411
			// Standardize the received slug.
1412
			$plugin['slug'] = $this->sanitize_key( $plugin['slug'] );
1413
1414
			// Forgive users for using string versions of booleans or floats for version number.
1415
			$plugin['version']            = (string) $plugin['version'];
1416
			$plugin['source']             = empty( $plugin['source'] ) ? 'repo' : $plugin['source'];
1417
			$plugin['required']           = TGMPA_Utils::validate_bool( $plugin['required'] );
1418
			$plugin['force_activation']   = TGMPA_Utils::validate_bool( $plugin['force_activation'] );
1419
			$plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] );
1420
1421
			// Enrich the received data.
1422
			$plugin['file_path']   = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
1423
			$plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] );
1424
1425
			// Set the class properties.
1426
			$this->plugins[ $plugin['slug'] ]    = $plugin;
1427
			$this->sort_order[ $plugin['slug'] ] = $plugin['name'];
1428
1429
			// Should we add the force activation hook ?
1430
			if ( true === $plugin['force_activation'] ) {
1431
				$this->has_forced_activation = true;
1432
			}
1433
1434
			// Should we add the force deactivation hook ?
1435
			if ( true === $plugin['force_deactivation'] ) {
1436
				$this->has_forced_deactivation = true;
1437
			}
1438
		}
1439
1440
		/**
1441
		 * Determine what type of source the plugin comes from.
1442
		 *
1443
		 * @since 2.5.0
1444
		 *
1445
		 * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path
1446
		 *                       (= bundled) or an external URL.
1447
		 * @return string 'repo', 'external', or 'bundled'
1448
		 */
1449
		protected function get_plugin_source_type( $source ) {
1450
			if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) {
1451
				return 'repo';
1452
			} elseif ( preg_match( self::IS_URL_REGEX, $source ) ) {
1453
				return 'external';
1454
			} else {
1455
				return 'bundled';
1456
			}
1457
		}
1458
1459
		/**
1460
		 * Sanitizes a string key.
1461
		 *
1462
		 * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are*
1463
		 * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase
1464
		 * characters in the plugin directory path/slug. Silly them.
1465
		 *
1466
		 * @see https://developer.wordpress.org/reference/hooks/sanitize_key/
1467
		 *
1468
		 * @since 2.5.0
1469
		 *
1470
		 * @param string $key String key.
1471
		 * @return string Sanitized key
1472
		 */
1473
		public function sanitize_key( $key ) {
1474
			$raw_key = $key;
1475
			$key     = preg_replace( '`[^A-Za-z0-9_-]`', '', $key );
1476
1477
			/**
1478
			 * Filter a sanitized key string.
1479
			 *
1480
			 * @since 2.5.0
1481
			 *
1482
			 * @param string $key     Sanitized key.
1483
			 * @param string $raw_key The key prior to sanitization.
1484
			 */
1485
			return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key );
1486
		}
1487
1488
		/**
1489
		 * Amend default configuration settings.
1490
		 *
1491
		 * @since 2.0.0
1492
		 *
1493
		 * @param array $config Array of config options to pass as class properties.
1494
		 */
1495
		public function config( $config ) {
1496
			$keys = array(
1497
				'id',
1498
				'default_path',
1499
				'has_notices',
1500
				'dismissable',
1501
				'dismiss_msg',
1502
				'menu',
1503
				'parent_slug',
1504
				'capability',
1505
				'is_automatic',
1506
				'message',
1507
				'strings',
1508
			);
1509
1510
			foreach ( $keys as $key ) {
1511
				if ( isset( $config[ $key ] ) ) {
1512
					if ( is_array( $config[ $key ] ) ) {
1513
						$this->$key = array_merge( $this->$key, $config[ $key ] );
1514
					} else {
1515
						$this->$key = $config[ $key ];
1516
					}
1517
				}
1518
			}
1519
		}
1520
1521
		/**
1522
		 * Amend action link after plugin installation.
1523
		 *
1524
		 * @since 2.0.0
1525
		 *
1526
		 * @param array $install_actions Existing array of actions.
1527
		 * @return false|array Amended array of actions.
1528
		 */
1529
		public function actions( $install_actions ) {
1530
			// Remove action links on the TGMPA install page.
1531
			if ( $this->is_tgmpa_page() ) {
1532
				return false;
1533
			}
1534
1535
			return $install_actions;
1536
		}
1537
1538
		/**
1539
		 * Flushes the plugins cache on theme switch to prevent stale entries
1540
		 * from remaining in the plugin table.
1541
		 *
1542
		 * @since 2.4.0
1543
		 *
1544
		 * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache.
1545
		 *                                 Parameter added in v2.5.0.
1546
		 */
1547
		public function flush_plugins_cache( $clear_update_cache = true ) {
1548
			wp_clean_plugins_cache( $clear_update_cache );
1549
		}
1550
1551
		/**
1552
		 * Set file_path key for each installed plugin.
1553
		 *
1554
		 * @since 2.1.0
1555
		 *
1556
		 * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
1557
		 *                            Parameter added in v2.5.0.
1558
		 */
1559
		public function populate_file_path( $plugin_slug = '' ) {
1560
			if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) {
1561
				$this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug );
1562
			} else {
1563
				// Add file_path key for all plugins.
1564
				foreach ( $this->plugins as $slug => $values ) {
1565
					$this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug );
1566
				}
1567
			}
1568
		}
1569
1570
		/**
1571
		 * Helper function to extract the file path of the plugin file from the
1572
		 * plugin slug, if the plugin is installed.
1573
		 *
1574
		 * @since 2.0.0
1575
		 *
1576
		 * @param string $slug Plugin slug (typically folder name) as provided by the developer.
1577
		 * @return string Either file path for plugin if installed, or just the plugin slug.
1578
		 */
1579
		protected function _get_plugin_basename_from_slug( $slug ) {
1580
			$keys = array_keys( $this->get_plugins() );
1581
1582
			foreach ( $keys as $key ) {
1583
				if ( preg_match( '|^' . $slug . '/|', $key ) ) {
1584
					return $key;
1585
				}
1586
			}
1587
1588
			return $slug;
1589
		}
1590
1591
		/**
1592
		 * Retrieve plugin data, given the plugin name.
1593
		 *
1594
		 * Loops through the registered plugins looking for $name. If it finds it,
1595
		 * it returns the $data from that plugin. Otherwise, returns false.
1596
		 *
1597
		 * @since 2.1.0
1598
		 *
1599
		 * @param string $name Name of the plugin, as it was registered.
1600
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
1601
		 * @return string|boolean Plugin slug if found, false otherwise.
1602
		 */
1603
		public function _get_plugin_data_from_name( $name, $data = 'slug' ) {
1604
			foreach ( $this->plugins as $values ) {
1605
				if ( $name === $values['name'] && isset( $values[ $data ] ) ) {
1606
					return $values[ $data ];
1607
				}
1608
			}
1609
1610
			return false;
1611
		}
1612
1613
		/**
1614
		 * Retrieve the download URL for a package.
1615
		 *
1616
		 * @since 2.5.0
1617
		 *
1618
		 * @param string $slug Plugin slug.
1619
		 * @return string Plugin download URL or path to local file or empty string if undetermined.
1620
		 */
1621
		public function get_download_url( $slug ) {
1622
			$dl_source = '';
1623
1624
			switch ( $this->plugins[ $slug ]['source_type'] ) {
1625
				case 'repo':
1626
					return $this->get_wp_repo_download_url( $slug );
1627
				case 'external':
1628
					return $this->plugins[ $slug ]['source'];
1629
				case 'bundled':
1630
					return $this->default_path . $this->plugins[ $slug ]['source'];
1631
			}
1632
1633
			return $dl_source; // Should never happen.
1634
		}
1635
1636
		/**
1637
		 * Retrieve the download URL for a WP repo package.
1638
		 *
1639
		 * @since 2.5.0
1640
		 *
1641
		 * @param string $slug Plugin slug.
1642
		 * @return string Plugin download URL.
1643
		 */
1644
		protected function get_wp_repo_download_url( $slug ) {
1645
			$source = '';
1646
			$api    = $this->get_plugins_api( $slug );
1647
1648
			if ( false !== $api && isset( $api->download_link ) ) {
1649
				$source = $api->download_link;
1650
			}
1651
1652
			return $source;
1653
		}
1654
1655
		/**
1656
		 * Try to grab information from WordPress API.
1657
		 *
1658
		 * @since 2.5.0
1659
		 *
1660
		 * @param string $slug Plugin slug.
1661
		 * @return object Plugins_api response object on success, WP_Error on failure.
1662
		 */
1663
		protected function get_plugins_api( $slug ) {
1664
			static $api = array(); // Cache received responses.
1665
1666
			if ( ! isset( $api[ $slug ] ) ) {
1667
				if ( ! function_exists( 'plugins_api' ) ) {
1668
					require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
1669
				}
1670
1671
				$response = plugins_api(
1672
					'plugin_information',
1673
					array(
1674
						'slug'   => $slug,
1675
						'fields' => array(
1676
							'sections' => false,
1677
						),
1678
					)
1679
				);
1680
1681
				$api[ $slug ] = false;
1682
1683
				if ( is_wp_error( $response ) ) {
1684
					wp_die( esc_html( $this->strings['oops'] ) );
1685
				} else {
1686
					$api[ $slug ] = $response;
1687
				}
1688
			}
1689
1690
			return $api[ $slug ];
1691
		}
1692
1693
		/**
1694
		 * Retrieve a link to a plugin information page.
1695
		 *
1696
		 * @since 2.5.0
1697
		 *
1698
		 * @param string $slug Plugin slug.
1699
		 * @return string Fully formed html link to a plugin information page if available
1700
		 *                or the plugin name if not.
1701
		 */
1702
		public function get_info_link( $slug ) {
1703
			if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) {
1704
				$link = sprintf(
1705
					'<a href="%1$s" target="_blank">%2$s</a>',
1706
					esc_url( $this->plugins[ $slug ]['external_url'] ),
1707
					esc_html( $this->plugins[ $slug ]['name'] )
1708
				);
1709
			} elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) {
1710
				$url = add_query_arg(
1711
					array(
1712
						'tab'       => 'plugin-information',
1713
						'plugin'    => urlencode( $slug ),
1714
						'TB_iframe' => 'true',
1715
						'width'     => '640',
1716
						'height'    => '500',
1717
					),
1718
					self_admin_url( 'plugin-install.php' )
1719
				);
1720
1721
				$link = sprintf(
1722
					'<a href="%1$s" class="thickbox">%2$s</a>',
1723
					esc_url( $url ),
1724
					esc_html( $this->plugins[ $slug ]['name'] )
1725
				);
1726
			} else {
1727
				$link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink.
1728
			}
1729
1730
			return $link;
1731
		}
1732
1733
		/**
1734
		 * Determine if we're on the TGMPA Install page.
1735
		 *
1736
		 * @since 2.1.0
1737
		 *
1738
		 * @return boolean True when on the TGMPA page, false otherwise.
1739
		 */
1740
		protected function is_tgmpa_page() {
1741
			return isset( $_GET['page'] ) && $this->menu === $_GET['page'];
1742
		}
1743
1744
		/**
1745
		 * Determine if we're on a WP Core installation/upgrade page.
1746
		 *
1747
		 * @since 2.6.0
1748
		 *
1749
		 * @return boolean True when on a WP Core installation/upgrade page, false otherwise.
1750
		 */
1751
		protected function is_core_update_page() {
1752
			// Current screen is not always available, most notably on the customizer screen.
1753
			if ( ! function_exists( 'get_current_screen' ) ) {
1754
				return false;
1755
			}
1756
1757
			$screen = get_current_screen();
1758
1759
			if ( 'update-core' === $screen->base ) {
1760
				// Core update screen.
1761
				return true;
1762
			} elseif ( 'plugins' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
1763
				// Plugins bulk update screen.
1764
				return true;
1765
			} elseif ( 'update' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
1766
				// Individual updates (ajax call).
1767
				return true;
1768
			}
1769
1770
			return false;
1771
		}
1772
1773
		/**
1774
		 * Retrieve the URL to the TGMPA Install page.
1775
		 *
1776
		 * I.e. depending on the config settings passed something along the lines of:
1777
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins
1778
		 *
1779
		 * @since 2.5.0
1780
		 *
1781
		 * @return string Properly encoded URL (not escaped).
1782
		 */
1783
		public function get_tgmpa_url() {
1784
			static $url;
1785
1786
			if ( ! isset( $url ) ) {
1787
				$parent = $this->parent_slug;
1788
				if ( false === strpos( $parent, '.php' ) ) {
1789
					$parent = 'admin.php';
1790
				}
1791
				$url = add_query_arg(
1792
					array(
1793
						'page' => urlencode( $this->menu ),
1794
					),
1795
					self_admin_url( $parent )
1796
				);
1797
			}
1798
1799
			return $url;
1800
		}
1801
1802
		/**
1803
		 * Retrieve the URL to the TGMPA Install page for a specific plugin status (view).
1804
		 *
1805
		 * I.e. depending on the config settings passed something along the lines of:
1806
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install
1807
		 *
1808
		 * @since 2.5.0
1809
		 *
1810
		 * @param string $status Plugin status - either 'install', 'update' or 'activate'.
1811
		 * @return string Properly encoded URL (not escaped).
1812
		 */
1813
		public function get_tgmpa_status_url( $status ) {
1814
			return add_query_arg(
1815
				array(
1816
					'plugin_status' => urlencode( $status ),
1817
				),
1818
				$this->get_tgmpa_url()
1819
			);
1820
		}
1821
1822
		/**
1823
		 * Determine whether there are open actions for plugins registered with TGMPA.
1824
		 *
1825
		 * @since 2.5.0
1826
		 *
1827
		 * @return bool True if complete, i.e. no outstanding actions. False otherwise.
1828
		 */
1829
		public function is_tgmpa_complete() {
1830
			$complete = true;
1831
			foreach ( $this->plugins as $slug => $plugin ) {
1832
				if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1833
					$complete = false;
1834
					break;
1835
				}
1836
			}
1837
1838
			return $complete;
1839
		}
1840
1841
		/**
1842
		 * Check if a plugin is installed. Does not take must-use plugins into account.
1843
		 *
1844
		 * @since 2.5.0
1845
		 *
1846
		 * @param string $slug Plugin slug.
1847
		 * @return bool True if installed, false otherwise.
1848
		 */
1849
		public function is_plugin_installed( $slug ) {
1850
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1851
1852
			return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) );
1853
		}
1854
1855
		/**
1856
		 * Check if a plugin is active.
1857
		 *
1858
		 * @since 2.5.0
1859
		 *
1860
		 * @param string $slug Plugin slug.
1861
		 * @return bool True if active, false otherwise.
1862
		 */
1863
		public function is_plugin_active( $slug ) {
1864
			return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) );
1865
		}
1866
1867
		/**
1868
		 * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required
1869
		 * available, check whether the current install meets them.
1870
		 *
1871
		 * @since 2.5.0
1872
		 *
1873
		 * @param string $slug Plugin slug.
1874
		 * @return bool True if OK to update, false otherwise.
1875
		 */
1876
		public function can_plugin_update( $slug ) {
1877
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1878
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1879
				return true;
1880
			}
1881
1882
			$api = $this->get_plugins_api( $slug );
1883
1884
			if ( false !== $api && isset( $api->requires ) ) {
1885
				return version_compare( $this->wp_version, $api->requires, '>=' );
1886
			}
1887
1888
			// No usable info received from the plugins API, presume we can update.
1889
			return true;
1890
		}
1891
1892
		/**
1893
		 * Check to see if the plugin is 'updatetable', i.e. installed, with an update available
1894
		 * and no WP version requirements blocking it.
1895
		 *
1896
		 * @since 2.6.0
1897
		 *
1898
		 * @param string $slug Plugin slug.
1899
		 * @return bool True if OK to proceed with update, false otherwise.
1900
		 */
1901
		public function is_plugin_updatetable( $slug ) {
1902
			if ( ! $this->is_plugin_installed( $slug ) ) {
1903
				return false;
1904
			} else {
1905
				return ( false !== $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) );
1906
			}
1907
		}
1908
1909
		/**
1910
		 * Check if a plugin can be activated, i.e. is not currently active and meets the minimum
1911
		 * plugin version requirements set in TGMPA (if any).
1912
		 *
1913
		 * @since 2.5.0
1914
		 *
1915
		 * @param string $slug Plugin slug.
1916
		 * @return bool True if OK to activate, false otherwise.
1917
		 */
1918
		public function can_plugin_activate( $slug ) {
1919
			return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) );
1920
		}
1921
1922
		/**
1923
		 * Retrieve the version number of an installed plugin.
1924
		 *
1925
		 * @since 2.5.0
1926
		 *
1927
		 * @param string $slug Plugin slug.
1928
		 * @return string Version number as string or an empty string if the plugin is not installed
1929
		 *                or version unknown (plugins which don't comply with the plugin header standard).
1930
		 */
1931
		public function get_installed_version( $slug ) {
1932
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1933
1934
			if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) {
1935
				return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'];
1936
			}
1937
1938
			return '';
1939
		}
1940
1941
		/**
1942
		 * Check whether a plugin complies with the minimum version requirements.
1943
		 *
1944
		 * @since 2.5.0
1945
		 *
1946
		 * @param string $slug Plugin slug.
1947
		 * @return bool True when a plugin needs to be updated, otherwise false.
1948
		 */
1949
		public function does_plugin_require_update( $slug ) {
1950
			$installed_version = $this->get_installed_version( $slug );
1951
			$minimum_version   = $this->plugins[ $slug ]['version'];
1952
1953
			return version_compare( $minimum_version, $installed_version, '>' );
1954
		}
1955
1956
		/**
1957
		 * Check whether there is an update available for a plugin.
1958
		 *
1959
		 * @since 2.5.0
1960
		 *
1961
		 * @param string $slug Plugin slug.
1962
		 * @return false|string Version number string of the available update or false if no update available.
1963
		 */
1964
		public function does_plugin_have_update( $slug ) {
1965
			// Presume bundled and external plugins will point to a package which meets the minimum required version.
1966
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1967
				if ( $this->does_plugin_require_update( $slug ) ) {
1968
					return $this->plugins[ $slug ]['version'];
1969
				}
1970
1971
				return false;
1972
			}
1973
1974
			$repo_updates = get_site_transient( 'update_plugins' );
1975
1976 View Code Duplication
			if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) {
1977
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version;
1978
			}
1979
1980
			return false;
1981
		}
1982
1983
		/**
1984
		 * Retrieve potential upgrade notice for a plugin.
1985
		 *
1986
		 * @since 2.5.0
1987
		 *
1988
		 * @param string $slug Plugin slug.
1989
		 * @return string The upgrade notice or an empty string if no message was available or provided.
1990
		 */
1991
		public function get_upgrade_notice( $slug ) {
1992
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1993
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1994
				return '';
1995
			}
1996
1997
			$repo_updates = get_site_transient( 'update_plugins' );
1998
1999 View Code Duplication
			if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) {
2000
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice;
2001
			}
2002
2003
			return '';
2004
		}
2005
2006
		/**
2007
		 * Wrapper around the core WP get_plugins function, making sure it's actually available.
2008
		 *
2009
		 * @since 2.5.0
2010
		 *
2011
		 * @param string $plugin_folder Optional. Relative path to single plugin folder.
2012
		 * @return array Array of installed plugins with plugin information.
2013
		 */
2014
		public function get_plugins( $plugin_folder = '' ) {
2015
			if ( ! function_exists( 'get_plugins' ) ) {
2016
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
2017
			}
2018
2019
			return get_plugins( $plugin_folder );
2020
		}
2021
2022
		/**
2023
		 * Delete dismissable nag option when theme is switched.
2024
		 *
2025
		 * This ensures that the user(s) is/are again reminded via nag of required
2026
		 * and/or recommended plugins if they re-activate the theme.
2027
		 *
2028
		 * @since 2.1.1
2029
		 */
2030
		public function update_dismiss() {
2031
			delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true );
2032
		}
2033
2034
		/**
2035
		 * Forces plugin activation if the parameter 'force_activation' is
2036
		 * set to true.
2037
		 *
2038
		 * This allows theme authors to specify certain plugins that must be
2039
		 * active at all times while using the current theme.
2040
		 *
2041
		 * Please take special care when using this parameter as it has the
2042
		 * potential to be harmful if not used correctly. Setting this parameter
2043
		 * to true will not allow the specified plugin to be deactivated unless
2044
		 * the user switches themes.
2045
		 *
2046
		 * @since 2.2.0
2047
		 */
2048
		public function force_activation() {
2049
			foreach ( $this->plugins as $slug => $plugin ) {
2050
				if ( true === $plugin['force_activation'] ) {
2051
					if ( ! $this->is_plugin_installed( $slug ) ) {
2052
						// Oops, plugin isn't there so iterate to next condition.
2053
						continue;
2054
					} elseif ( $this->can_plugin_activate( $slug ) ) {
2055
						// There we go, activate the plugin.
2056
						activate_plugin( $plugin['file_path'] );
2057
					}
2058
				}
2059
			}
2060
		}
2061
2062
		/**
2063
		 * Forces plugin deactivation if the parameter 'force_deactivation'
2064
		 * is set to true and adds the plugin to the 'recently active' plugins list.
2065
		 *
2066
		 * This allows theme authors to specify certain plugins that must be
2067
		 * deactivated upon switching from the current theme to another.
2068
		 *
2069
		 * Please take special care when using this parameter as it has the
2070
		 * potential to be harmful if not used correctly.
2071
		 *
2072
		 * @since 2.2.0
2073
		 */
2074
		public function force_deactivation() {
2075
			$deactivated = array();
2076
2077
			foreach ( $this->plugins as $slug => $plugin ) {
2078
				/*
2079
				 * Only proceed forward if the parameter is set to true and plugin is active
2080
				 * as a 'normal' (not must-use) plugin.
2081
				 */
2082
				if ( true === $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) ) {
2083
					deactivate_plugins( $plugin['file_path'] );
2084
					$deactivated[ $plugin['file_path'] ] = time();
2085
				}
2086
			}
2087
2088
			if ( ! empty( $deactivated ) ) {
2089
				update_option( 'recently_activated', $deactivated + (array) get_option( 'recently_activated' ) );
2090
			}
2091
		}
2092
2093
		/**
2094
		 * Echo the current TGMPA version number to the page.
2095
		 *
2096
		 * @since 2.5.0
2097
		 */
2098
		public function show_tgmpa_version() {
2099
			echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',
2100
				esc_html(
2101
					sprintf(
2102
						/* translators: %s: version number */
2103
						__( 'TGMPA v%s', 'tgmpa' ),
2104
						self::TGMPA_VERSION
2105
					)
2106
				),
2107
				'</small></strong></p>';
2108
		}
2109
2110
		/**
2111
		 * Adds CSS to admin head.
2112
		 *
2113
		 * @since 2.6.2
2114
		 */
2115
		public function admin_css() {
2116
			if ( ! $this->is_tgmpa_page() ) {
2117
				return;
2118
			}
2119
2120
			echo '
2121
			<style>
2122
			#tgmpa-plugins .tgmpa-type-required > th {
2123
				border-left: 3px solid #dc3232;
2124
			}
2125
			</style>';
2126
		}
2127
2128
		/**
2129
		 * Returns the singleton instance of the class.
2130
		 *
2131
		 * @since 2.4.0
2132
		 *
2133
		 * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object.
2134
		 */
2135
		public static function get_instance() {
2136
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
2137
				self::$instance = new self();
2138
			}
2139
2140
			return self::$instance;
2141
		}
2142
	}
2143
2144
	if ( ! function_exists( 'load_tgm_plugin_activation' ) ) {
2145
		/**
2146
		 * Ensure only one instance of the class is ever invoked.
2147
		 *
2148
		 * @since 2.5.0
2149
		 */
2150
		function load_tgm_plugin_activation() {
2151
			$GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance();
2152
		}
2153
	}
2154
2155
	if ( did_action( 'plugins_loaded' ) ) {
2156
		load_tgm_plugin_activation();
2157
	} else {
2158
		add_action( 'plugins_loaded', 'load_tgm_plugin_activation' );
2159
	}
2160
} // End if().
2161
2162
if ( ! function_exists( 'tgmpa' ) ) {
2163
	/**
2164
	 * Helper function to register a collection of required plugins.
2165
	 *
2166
	 * @since 2.0.0
2167
	 * @api
2168
	 *
2169
	 * @param array $plugins An array of plugin arrays.
2170
	 * @param array $config  Optional. An array of configuration values.
2171
	 */
2172
	function tgmpa( $plugins, $config = array() ) {
2173
		$instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2174
2175
		foreach ( $plugins as $plugin ) {
2176
			call_user_func( array( $instance, 'register' ), $plugin );
2177
		}
2178
2179
		if ( ! empty( $config ) && is_array( $config ) ) {
2180
			// Send out notices for deprecated arguments passed.
2181
			if ( isset( $config['notices'] ) ) {
2182
				_deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' );
2183
				if ( ! isset( $config['has_notices'] ) ) {
2184
					$config['has_notices'] = $config['notices'];
2185
				}
2186
			}
2187
2188
			if ( isset( $config['parent_menu_slug'] ) ) {
2189
				_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.' );
2190
			}
2191
			if ( isset( $config['parent_url_slug'] ) ) {
2192
				_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.' );
2193
			}
2194
2195
			call_user_func( array( $instance, 'config' ), $config );
2196
		}
2197
	}
2198
} // End if().
2199
2200
/**
2201
 * WP_List_Table isn't always available. If it isn't available,
2202
 * we load it here.
2203
 *
2204
 * @since 2.2.0
2205
 */
2206
if ( ! class_exists( 'WP_List_Table' ) ) {
2207
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
2208
}
2209
2210
if ( ! class_exists( 'TGMPA_List_Table' ) ) {
2211
2212
	/**
2213
	 * List table class for handling plugins.
2214
	 *
2215
	 * Extends the WP_List_Table class to provide a future-compatible
2216
	 * way of listing out all required/recommended plugins.
2217
	 *
2218
	 * Gives users an interface similar to the Plugin Administration
2219
	 * area with similar (albeit stripped down) capabilities.
2220
	 *
2221
	 * This class also allows for the bulk install of plugins.
2222
	 *
2223
	 * @since 2.2.0
2224
	 *
2225
	 * @package TGM-Plugin-Activation
2226
	 * @author  Thomas Griffin
2227
	 * @author  Gary Jones
2228
	 */
2229
	class TGMPA_List_Table extends WP_List_Table {
2230
		/**
2231
		 * TGMPA instance.
2232
		 *
2233
		 * @since 2.5.0
2234
		 *
2235
		 * @var object
2236
		 */
2237
		protected $tgmpa;
2238
2239
		/**
2240
		 * The currently chosen view.
2241
		 *
2242
		 * @since 2.5.0
2243
		 *
2244
		 * @var string One of: 'all', 'install', 'update', 'activate'
2245
		 */
2246
		public $view_context = 'all';
2247
2248
		/**
2249
		 * The plugin counts for the various views.
2250
		 *
2251
		 * @since 2.5.0
2252
		 *
2253
		 * @var array
2254
		 */
2255
		protected $view_totals = array(
2256
			'all'      => 0,
2257
			'install'  => 0,
2258
			'update'   => 0,
2259
			'activate' => 0,
2260
		);
2261
2262
		/**
2263
		 * References parent constructor and sets defaults for class.
2264
		 *
2265
		 * @since 2.2.0
2266
		 */
2267
		public function __construct() {
2268
			$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2269
2270
			parent::__construct(
2271
				array(
2272
					'singular' => 'plugin',
2273
					'plural'   => 'plugins',
2274
					'ajax'     => false,
2275
				)
2276
			);
2277
2278
			if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) {
2279
				$this->view_context = sanitize_key( $_REQUEST['plugin_status'] );
2280
			}
2281
2282
			add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) );
2283
		}
2284
2285
		/**
2286
		 * Get a list of CSS classes for the <table> tag.
2287
		 *
2288
		 * Overruled to prevent the 'plural' argument from being added.
2289
		 *
2290
		 * @since 2.5.0
2291
		 *
2292
		 * @return array CSS classnames.
2293
		 */
2294
		public function get_table_classes() {
2295
			return array( 'widefat', 'fixed' );
2296
		}
2297
2298
		/**
2299
		 * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table.
2300
		 *
2301
		 * @since 2.2.0
2302
		 *
2303
		 * @return array $table_data Information for use in table.
2304
		 */
2305
		protected function _gather_plugin_data() {
2306
			// Load thickbox for plugin links.
2307
			$this->tgmpa->admin_init();
2308
			$this->tgmpa->thickbox();
2309
2310
			// Categorize the plugins which have open actions.
2311
			$plugins = $this->categorize_plugins_to_views();
2312
2313
			// Set the counts for the view links.
2314
			$this->set_view_totals( $plugins );
2315
2316
			// Prep variables for use and grab list of all installed plugins.
2317
			$table_data = array();
2318
			$i          = 0;
2319
2320
			// Redirect to the 'all' view if no plugins were found for the selected view context.
2321
			if ( empty( $plugins[ $this->view_context ] ) ) {
2322
				$this->view_context = 'all';
2323
			}
2324
2325
			foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) {
2326
				$table_data[ $i ]['sanitized_plugin']  = $plugin['name'];
2327
				$table_data[ $i ]['slug']              = $slug;
2328
				$table_data[ $i ]['plugin']            = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>';
2329
				$table_data[ $i ]['source']            = $this->get_plugin_source_type_text( $plugin['source_type'] );
2330
				$table_data[ $i ]['type']              = $this->get_plugin_advise_type_text( $plugin['required'] );
2331
				$table_data[ $i ]['status']            = $this->get_plugin_status_text( $slug );
2332
				$table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug );
2333
				$table_data[ $i ]['minimum_version']   = $plugin['version'];
2334
				$table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug );
2335
2336
				// Prep the upgrade notice info.
2337
				$upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug );
2338
				if ( ! empty( $upgrade_notice ) ) {
2339
					$table_data[ $i ]['upgrade_notice'] = $upgrade_notice;
2340
2341
					add_action( "tgmpa_after_plugin_row_{$slug}", array( $this, 'wp_plugin_update_row' ), 10, 2 );
2342
				}
2343
2344
				$table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin );
2345
2346
				$i++;
2347
			}
2348
2349
			return $table_data;
2350
		}
2351
2352
		/**
2353
		 * Categorize the plugins which have open actions into views for the TGMPA page.
2354
		 *
2355
		 * @since 2.5.0
2356
		 */
2357
		protected function categorize_plugins_to_views() {
2358
			$plugins = array(
2359
				'all'      => array(), // Meaning: all plugins which still have open actions.
2360
				'install'  => array(),
2361
				'update'   => array(),
2362
				'activate' => array(),
2363
			);
2364
2365
			foreach ( $this->tgmpa->plugins as $slug => $plugin ) {
2366
				if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2367
					// No need to display plugins if they are installed, up-to-date and active.
2368
					continue;
2369
				} else {
2370
					$plugins['all'][ $slug ] = $plugin;
2371
2372
					if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2373
						$plugins['install'][ $slug ] = $plugin;
2374
					} else {
2375
						if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2376
							$plugins['update'][ $slug ] = $plugin;
2377
						}
2378
2379
						if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2380
							$plugins['activate'][ $slug ] = $plugin;
2381
						}
2382
					}
2383
				}
2384
			}
2385
2386
			return $plugins;
2387
		}
2388
2389
		/**
2390
		 * Set the counts for the view links.
2391
		 *
2392
		 * @since 2.5.0
2393
		 *
2394
		 * @param array $plugins Plugins order by view.
2395
		 */
2396
		protected function set_view_totals( $plugins ) {
2397
			foreach ( $plugins as $type => $list ) {
2398
				$this->view_totals[ $type ] = count( $list );
2399
			}
2400
		}
2401
2402
		/**
2403
		 * Get the plugin required/recommended text string.
2404
		 *
2405
		 * @since 2.5.0
2406
		 *
2407
		 * @param string $required Plugin required setting.
2408
		 * @return string
2409
		 */
2410
		protected function get_plugin_advise_type_text( $required ) {
2411
			if ( true === $required ) {
2412
				return __( 'Required', 'tgmpa' );
2413
			}
2414
2415
			return __( 'Recommended', 'tgmpa' );
2416
		}
2417
2418
		/**
2419
		 * Get the plugin source type text string.
2420
		 *
2421
		 * @since 2.5.0
2422
		 *
2423
		 * @param string $type Plugin type.
2424
		 * @return string
2425
		 */
2426
		protected function get_plugin_source_type_text( $type ) {
2427
			$string = '';
2428
2429
			switch ( $type ) {
2430
				case 'repo':
2431
					$string = __( 'WordPress Repository', 'tgmpa' );
2432
					break;
2433
				case 'external':
2434
					$string = __( 'External Source', 'tgmpa' );
2435
					break;
2436
				case 'bundled':
2437
					$string = __( 'Pre-Packaged', 'tgmpa' );
2438
					break;
2439
			}
2440
2441
			return $string;
2442
		}
2443
2444
		/**
2445
		 * Determine the plugin status message.
2446
		 *
2447
		 * @since 2.5.0
2448
		 *
2449
		 * @param string $slug Plugin slug.
2450
		 * @return string
2451
		 */
2452
		protected function get_plugin_status_text( $slug ) {
2453
			if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2454
				return __( 'Not Installed', 'tgmpa' );
2455
			}
2456
2457
			if ( ! $this->tgmpa->is_plugin_active( $slug ) ) {
2458
				$install_status = __( 'Installed But Not Activated', 'tgmpa' );
2459
			} else {
2460
				$install_status = __( 'Active', 'tgmpa' );
2461
			}
2462
2463
			$update_status = '';
2464
2465
			if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2466
				$update_status = __( 'Required Update not Available', 'tgmpa' );
2467
2468
			} elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) {
2469
				$update_status = __( 'Requires Update', 'tgmpa' );
2470
2471
			} elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2472
				$update_status = __( 'Update recommended', 'tgmpa' );
2473
			}
2474
2475
			if ( '' === $update_status ) {
2476
				return $install_status;
2477
			}
2478
2479
			return sprintf(
2480
				/* translators: 1: install status, 2: update status */
2481
				_x( '%1$s, %2$s', 'Install/Update Status', 'tgmpa' ),
2482
				$install_status,
2483
				$update_status
2484
			);
2485
		}
2486
2487
		/**
2488
		 * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type.
2489
		 *
2490
		 * @since 2.5.0
2491
		 *
2492
		 * @param array $items Prepared table items.
2493
		 * @return array Sorted table items.
2494
		 */
2495
		public function sort_table_items( $items ) {
2496
			$type = array();
2497
			$name = array();
2498
2499
			foreach ( $items as $i => $plugin ) {
2500
				$type[ $i ] = $plugin['type']; // Required / recommended.
2501
				$name[ $i ] = $plugin['sanitized_plugin'];
2502
			}
2503
2504
			array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items );
2505
2506
			return $items;
2507
		}
2508
2509
		/**
2510
		 * Get an associative array ( id => link ) of the views available on this table.
2511
		 *
2512
		 * @since 2.5.0
2513
		 *
2514
		 * @return array
2515
		 */
2516
		public function get_views() {
2517
			$status_links = array();
2518
2519
			foreach ( $this->view_totals as $type => $count ) {
2520
				if ( $count < 1 ) {
2521
					continue;
2522
				}
2523
2524
				switch ( $type ) {
2525
					case 'all':
2526
						/* translators: 1: number of plugins. */
2527
						$text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' );
2528
						break;
2529
					case 'install':
2530
						/* translators: 1: number of plugins. */
2531
						$text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' );
2532
						break;
2533
					case 'update':
2534
						/* translators: 1: number of plugins. */
2535
						$text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' );
2536
						break;
2537
					case 'activate':
2538
						/* translators: 1: number of plugins. */
2539
						$text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' );
2540
						break;
2541
					default:
2542
						$text = '';
2543
						break;
2544
				}
2545
2546
				if ( ! empty( $text ) ) {
2547
2548
					$status_links[ $type ] = sprintf(
2549
						'<a href="%s"%s>%s</a>',
2550
						esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
2551
						( $type === $this->view_context ) ? ' class="current"' : '',
2552
						sprintf( $text, number_format_i18n( $count ) )
2553
					);
2554
				}
2555
			} // End foreach().
2556
2557
			return $status_links;
2558
		}
2559
2560
		/**
2561
		 * Create default columns to display important plugin information
2562
		 * like type, action and status.
2563
		 *
2564
		 * @since 2.2.0
2565
		 *
2566
		 * @param array  $item        Array of item data.
2567
		 * @param string $column_name The name of the column.
2568
		 * @return string
2569
		 */
2570
		public function column_default( $item, $column_name ) {
2571
			return $item[ $column_name ];
2572
		}
2573
2574
		/**
2575
		 * Required for bulk installing.
2576
		 *
2577
		 * Adds a checkbox for each plugin.
2578
		 *
2579
		 * @since 2.2.0
2580
		 *
2581
		 * @param array $item Array of item data.
2582
		 * @return string The input checkbox with all necessary info.
2583
		 */
2584
		public function column_cb( $item ) {
2585
			return sprintf(
2586
				'<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />',
2587
				esc_attr( $this->_args['singular'] ),
2588
				esc_attr( $item['slug'] ),
2589
				esc_attr( $item['sanitized_plugin'] )
2590
			);
2591
		}
2592
2593
		/**
2594
		 * Create default title column along with the action links.
2595
		 *
2596
		 * @since 2.2.0
2597
		 *
2598
		 * @param array $item Array of item data.
2599
		 * @return string The plugin name and action links.
2600
		 */
2601
		public function column_plugin( $item ) {
2602
			return sprintf(
2603
				'%1$s %2$s',
2604
				$item['plugin'],
2605
				$this->row_actions( $this->get_row_actions( $item ), true )
2606
			);
2607
		}
2608
2609
		/**
2610
		 * Create version information column.
2611
		 *
2612
		 * @since 2.5.0
2613
		 *
2614
		 * @param array $item Array of item data.
2615
		 * @return string HTML-formatted version information.
2616
		 */
2617
		public function column_version( $item ) {
2618
			$output = array();
2619
2620
			if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2621
				$installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'tgmpa' );
2622
2623
				$color = '';
2624
				if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) {
2625
					$color = ' color: #ff0000; font-weight: bold;';
2626
				}
2627
2628
				$output[] = sprintf(
2629
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'tgmpa' ) . '</p>',
2630
					$color,
2631
					$installed
2632
				);
2633
			}
2634
2635
			if ( ! empty( $item['minimum_version'] ) ) {
2636
				$output[] = sprintf(
2637
					'<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'tgmpa' ) . '</p>',
2638
					$item['minimum_version']
2639
				);
2640
			}
2641
2642
			if ( ! empty( $item['available_version'] ) ) {
2643
				$color = '';
2644
				if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) {
2645
					$color = ' color: #71C671; font-weight: bold;';
2646
				}
2647
2648
				$output[] = sprintf(
2649
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'tgmpa' ) . '</p>',
2650
					$color,
2651
					$item['available_version']
2652
				);
2653
			}
2654
2655
			if ( empty( $output ) ) {
2656
				return '&nbsp;'; // Let's not break the table layout.
2657
			} else {
2658
				return implode( "\n", $output );
2659
			}
2660
		}
2661
2662
		/**
2663
		 * Sets default message within the plugins table if no plugins
2664
		 * are left for interaction.
2665
		 *
2666
		 * Hides the menu item to prevent the user from clicking and
2667
		 * getting a permissions error.
2668
		 *
2669
		 * @since 2.2.0
2670
		 */
2671
		public function no_items() {
2672
			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>';
2673
			echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
2674
		}
2675
2676
		/**
2677
		 * Output all the column information within the table.
2678
		 *
2679
		 * @since 2.2.0
2680
		 *
2681
		 * @return array $columns The column names.
2682
		 */
2683
		public function get_columns() {
2684
			$columns = array(
2685
				'cb'     => '<input type="checkbox" />',
2686
				'plugin' => __( 'Plugin', 'tgmpa' ),
2687
				'source' => __( 'Source', 'tgmpa' ),
2688
				'type'   => __( 'Type', 'tgmpa' ),
2689
			);
2690
2691
			if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
2692
				$columns['version'] = __( 'Version', 'tgmpa' );
2693
				$columns['status']  = __( 'Status', 'tgmpa' );
2694
			}
2695
2696
			return apply_filters( 'tgmpa_table_columns', $columns );
2697
		}
2698
2699
		/**
2700
		 * Get name of default primary column
2701
		 *
2702
		 * @since 2.5.0 / WP 4.3+ compatibility
2703
		 * @access protected
2704
		 *
2705
		 * @return string
2706
		 */
2707
		protected function get_default_primary_column_name() {
2708
			return 'plugin';
2709
		}
2710
2711
		/**
2712
		 * Get the name of the primary column.
2713
		 *
2714
		 * @since 2.5.0 / WP 4.3+ compatibility
2715
		 * @access protected
2716
		 *
2717
		 * @return string The name of the primary column.
2718
		 */
2719
		protected function get_primary_column_name() {
2720
			if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) {
2721
				return parent::get_primary_column_name();
2722
			} else {
2723
				return $this->get_default_primary_column_name();
2724
			}
2725
		}
2726
2727
		/**
2728
		 * Get the actions which are relevant for a specific plugin row.
2729
		 *
2730
		 * @since 2.5.0
2731
		 *
2732
		 * @param array $item Array of item data.
2733
		 * @return array Array with relevant action links.
2734
		 */
2735
		protected function get_row_actions( $item ) {
2736
			$actions      = array();
2737
			$action_links = array();
2738
2739
			// Display the 'Install' action link if the plugin is not yet available.
2740
			if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2741
				/* translators: %2$s: plugin name in screen reader markup */
2742
				$actions['install'] = __( 'Install %2$s', 'tgmpa' );
2743
			} else {
2744
				// Display the 'Update' action link if an update is available and WP complies with plugin minimum.
2745
				if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) {
2746
					/* translators: %2$s: plugin name in screen reader markup */
2747
					$actions['update'] = __( 'Update %2$s', 'tgmpa' );
2748
				}
2749
2750
				// Display the 'Activate' action link, but only if the plugin meets the minimum version.
2751
				if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) {
2752
					/* translators: %2$s: plugin name in screen reader markup */
2753
					$actions['activate'] = __( 'Activate %2$s', 'tgmpa' );
2754
				}
2755
			}
2756
2757
			// Create the actual links.
2758
			foreach ( $actions as $action => $text ) {
2759
				$nonce_url = wp_nonce_url(
2760
					add_query_arg(
2761
						array(
2762
							'plugin'           => urlencode( $item['slug'] ),
2763
							'tgmpa-' . $action => $action . '-plugin',
2764
						),
2765
						$this->tgmpa->get_tgmpa_url()
2766
					),
2767
					'tgmpa-' . $action,
2768
					'tgmpa-nonce'
2769
				);
2770
2771
				$action_links[ $action ] = sprintf(
2772
					'<a href="%1$s">' . esc_html( $text ) . '</a>', // $text contains the second placeholder.
2773
					esc_url( $nonce_url ),
2774
					'<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>'
2775
				);
2776
			}
2777
2778
			$prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : '';
2779
			return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context );
2780
		}
2781
2782
		/**
2783
		 * Generates content for a single row of the table.
2784
		 *
2785
		 * @since 2.5.0
2786
		 *
2787
		 * @param object $item The current item.
2788
		 */
2789
		public function single_row( $item ) {
2790
			echo '<tr class="' . esc_attr( 'tgmpa-type-' . strtolower( $item['type'] ) ) . '">';
2791
			$this->single_row_columns( $item );
2792
			echo '</tr>';
2793
2794
			/**
2795
			 * Fires after each specific row in the TGMPA Plugins list table.
2796
			 *
2797
			 * The dynamic portion of the hook name, `$item['slug']`, refers to the slug
2798
			 * for the plugin.
2799
			 *
2800
			 * @since 2.5.0
2801
			 */
2802
			do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context );
2803
		}
2804
2805
		/**
2806
		 * Show the upgrade notice below a plugin row if there is one.
2807
		 *
2808
		 * @since 2.5.0
2809
		 *
2810
		 * @see /wp-admin/includes/update.php
2811
		 *
2812
		 * @param string $slug Plugin slug.
2813
		 * @param array  $item The information available in this table row.
2814
		 * @return null Return early if upgrade notice is empty.
2815
		 */
2816
		public function wp_plugin_update_row( $slug, $item ) {
2817
			if ( empty( $item['upgrade_notice'] ) ) {
2818
				return;
2819
			}
2820
2821
			echo '
2822
				<tr class="plugin-update-tr">
2823
					<td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange">
2824
						<div class="update-message">',
2825
							esc_html__( 'Upgrade message from the plugin author:', 'tgmpa' ),
2826
							' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong>
2827
						</div>
2828
					</td>
2829
				</tr>';
2830
		}
2831
2832
		/**
2833
		 * Extra controls to be displayed between bulk actions and pagination.
2834
		 *
2835
		 * @since 2.5.0
2836
		 *
2837
		 * @param string $which 'top' or 'bottom' table navigation.
2838
		 */
2839
		public function extra_tablenav( $which ) {
2840
			if ( 'bottom' === $which ) {
2841
				$this->tgmpa->show_tgmpa_version();
2842
			}
2843
		}
2844
2845
		/**
2846
		 * Defines the bulk actions for handling registered plugins.
2847
		 *
2848
		 * @since 2.2.0
2849
		 *
2850
		 * @return array $actions The bulk actions for the plugin install table.
2851
		 */
2852
		public function get_bulk_actions() {
2853
2854
			$actions = array();
2855
2856
			if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) {
2857
				if ( current_user_can( 'install_plugins' ) ) {
2858
					$actions['tgmpa-bulk-install'] = __( 'Install', 'tgmpa' );
2859
				}
2860
			}
2861
2862
			if ( 'install' !== $this->view_context ) {
2863
				if ( current_user_can( 'update_plugins' ) ) {
2864
					$actions['tgmpa-bulk-update'] = __( 'Update', 'tgmpa' );
2865
				}
2866
				if ( current_user_can( 'activate_plugins' ) ) {
2867
					$actions['tgmpa-bulk-activate'] = __( 'Activate', 'tgmpa' );
2868
				}
2869
			}
2870
2871
			return $actions;
2872
		}
2873
2874
		/**
2875
		 * Processes bulk installation and activation actions.
2876
		 *
2877
		 * The bulk installation process looks for the $_POST information and passes that
2878
		 * through if a user has to use WP_Filesystem to enter their credentials.
2879
		 *
2880
		 * @since 2.2.0
2881
		 */
2882
		public function process_bulk_actions() {
2883
			// Bulk installation process.
2884
			if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) {
2885
2886
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
2887
2888
				$install_type = 'install';
2889
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2890
					$install_type = 'update';
2891
				}
2892
2893
				$plugins_to_install = array();
2894
2895
				// Did user actually select any plugins to install/update ?
2896 View Code Duplication
				if ( empty( $_POST['plugin'] ) ) {
2897
					if ( 'install' === $install_type ) {
2898
						$message = __( 'No plugins were selected to be installed. No action taken.', 'tgmpa' );
2899
					} else {
2900
						$message = __( 'No plugins were selected to be updated. No action taken.', 'tgmpa' );
2901
					}
2902
2903
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2904
2905
					return false;
2906
				}
2907
2908
				if ( is_array( $_POST['plugin'] ) ) {
2909
					$plugins_to_install = (array) $_POST['plugin'];
2910
				} elseif ( is_string( $_POST['plugin'] ) ) {
2911
					// Received via Filesystem page - un-flatten array (WP bug #19643).
2912
					$plugins_to_install = explode( ',', $_POST['plugin'] );
2913
				}
2914
2915
				// Sanitize the received input.
2916
				$plugins_to_install = array_map( 'urldecode', $plugins_to_install );
2917
				$plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install );
2918
2919
				// Validate the received input.
2920
				foreach ( $plugins_to_install as $key => $slug ) {
2921
					// Check if the plugin was registered with TGMPA and remove if not.
2922
					if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) {
2923
						unset( $plugins_to_install[ $key ] );
2924
						continue;
2925
					}
2926
2927
					// For install: make sure this is a plugin we *can* install and not one already installed.
2928
					if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) {
2929
						unset( $plugins_to_install[ $key ] );
2930
					}
2931
2932
					// For updates: make sure this is a plugin we *can* update (update available and WP version ok).
2933
					if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) {
2934
						unset( $plugins_to_install[ $key ] );
2935
					}
2936
				}
2937
2938
				// No need to proceed further if we have no plugins to handle.
2939 View Code Duplication
				if ( empty( $plugins_to_install ) ) {
2940
					if ( 'install' === $install_type ) {
2941
						$message = __( 'No plugins are available to be installed at this time.', 'tgmpa' );
2942
					} else {
2943
						$message = __( 'No plugins are available to be updated at this time.', 'tgmpa' );
2944
					}
2945
2946
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2947
2948
					return false;
2949
				}
2950
2951
				// Pass all necessary information if WP_Filesystem is needed.
2952
				$url = wp_nonce_url(
2953
					$this->tgmpa->get_tgmpa_url(),
2954
					'bulk-' . $this->_args['plural']
2955
				);
2956
2957
				// Give validated data back to $_POST which is the only place the filesystem looks for extra fields.
2958
				$_POST['plugin'] = implode( ',', $plugins_to_install ); // Work around for WP bug #19643.
2959
2960
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
2961
				$fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
2962
2963
				$creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields );
2964
				if ( false === $creds ) {
2965
					return true; // Stop the normal page form from displaying, credential request form will be shown.
2966
				}
2967
2968
				// Now we have some credentials, setup WP_Filesystem.
2969
				if ( ! WP_Filesystem( $creds ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2970
					// Our credentials were no good, ask the user for them again.
2971
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
2972
2973
					return true;
2974
				}
2975
2976
				/* If we arrive here, we have the filesystem */
2977
2978
				// Store all information in arrays since we are processing a bulk installation.
2979
				$names      = array();
2980
				$sources    = array(); // Needed for installs.
2981
				$file_paths = array(); // Needed for upgrades.
2982
				$to_inject  = array(); // Information to inject into the update_plugins transient.
2983
2984
				// Prepare the data for validated plugins for the install/upgrade.
2985
				foreach ( $plugins_to_install as $slug ) {
2986
					$name   = $this->tgmpa->plugins[ $slug ]['name'];
2987
					$source = $this->tgmpa->get_download_url( $slug );
2988
2989
					if ( ! empty( $name ) && ! empty( $source ) ) {
2990
						$names[] = $name;
2991
2992
						switch ( $install_type ) {
2993
2994
							case 'install':
2995
								$sources[] = $source;
2996
								break;
2997
2998
							case 'update':
2999
								$file_paths[]                 = $this->tgmpa->plugins[ $slug ]['file_path'];
3000
								$to_inject[ $slug ]           = $this->tgmpa->plugins[ $slug ];
3001
								$to_inject[ $slug ]['source'] = $source;
3002
								break;
3003
						}
3004
					}
3005
				}
3006
				unset( $slug, $name, $source );
3007
3008
				// Create a new instance of TGMPA_Bulk_Installer.
3009
				$installer = new TGMPA_Bulk_Installer(
3010
					new TGMPA_Bulk_Installer_Skin(
3011
						array(
3012
							'url'          => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),
3013
							'nonce'        => 'bulk-' . $this->_args['plural'],
3014
							'names'        => $names,
3015
							'install_type' => $install_type,
3016
						)
3017
					)
3018
				);
3019
3020
				// Wrap the install process with the appropriate HTML.
3021
				echo '<div class="tgmpa">',
3022
					'<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>
3023
					<div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">';
3024
3025
				// Process the bulk installation submissions.
3026
				add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
3027
3028
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
3029
					// Inject our info into the update transient.
3030
					$this->tgmpa->inject_update_info( $to_inject );
3031
3032
					$installer->bulk_upgrade( $file_paths );
3033
				} else {
3034
					$installer->bulk_install( $sources );
3035
				}
3036
3037
				remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1 );
3038
3039
				echo '</div></div>';
3040
3041
				return true;
3042
			} // End if().
3043
3044
			// Bulk activation process.
3045
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3046
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
3047
3048
				// Did user actually select any plugins to activate ?
3049
				if ( empty( $_POST['plugin'] ) ) {
3050
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>';
3051
3052
					return false;
3053
				}
3054
3055
				// Grab plugin data from $_POST.
3056
				$plugins = array();
3057
				if ( isset( $_POST['plugin'] ) ) {
3058
					$plugins = array_map( 'urldecode', (array) $_POST['plugin'] );
3059
					$plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins );
3060
				}
3061
3062
				$plugins_to_activate = array();
3063
				$plugin_names        = array();
3064
3065
				// Grab the file paths for the selected & inactive plugins from the registration array.
3066
				foreach ( $plugins as $slug ) {
3067
					if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
3068
						$plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path'];
3069
						$plugin_names[]        = $this->tgmpa->plugins[ $slug ]['name'];
3070
					}
3071
				}
3072
				unset( $slug );
3073
3074
				// Return early if there are no plugins to activate.
3075
				if ( empty( $plugins_to_activate ) ) {
3076
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>';
3077
3078
					return false;
3079
				}
3080
3081
				// Now we are good to go - let's start activating plugins.
3082
				$activate = activate_plugins( $plugins_to_activate );
3083
3084
				if ( is_wp_error( $activate ) ) {
3085
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
3086
				} else {
3087
					$count        = count( $plugin_names ); // Count so we can use _n function.
3088
					$plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names );
3089
					$last_plugin  = array_pop( $plugin_names ); // Pop off last name to prep for readability.
3090
					$imploded     = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
3091
3092
					printf( // WPCS: xss ok.
3093
						'<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
3094
						esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),
3095
						$imploded
3096
					);
3097
3098
					// Update recently activated plugins option.
3099
					$recent = (array) get_option( 'recently_activated' );
3100
					foreach ( $plugins_to_activate as $plugin => $time ) {
3101
						if ( isset( $recent[ $plugin ] ) ) {
3102
							unset( $recent[ $plugin ] );
3103
						}
3104
					}
3105
					update_option( 'recently_activated', $recent );
3106
				}
3107
3108
				unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another.
3109
3110
				return true;
3111
			} // End if().
3112
3113
			return false;
3114
		}
3115
3116
		/**
3117
		 * Prepares all of our information to be outputted into a usable table.
3118
		 *
3119
		 * @since 2.2.0
3120
		 */
3121
		public function prepare_items() {
3122
			$columns               = $this->get_columns(); // Get all necessary column information.
3123
			$hidden                = array(); // No columns to hide, but we must set as an array.
3124
			$sortable              = array(); // No reason to make sortable columns.
3125
			$primary               = $this->get_primary_column_name(); // Column which has the row actions.
3126
			$this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers.
3127
3128
			// Process our bulk activations here.
3129
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3130
				$this->process_bulk_actions();
3131
			}
3132
3133
			// Store all of our plugin data into $items array so WP_List_Table can use it.
3134
			$this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() );
3135
		}
3136
3137
		/* *********** DEPRECATED METHODS *********** */
3138
3139
		/**
3140
		 * Retrieve plugin data, given the plugin name.
3141
		 *
3142
		 * @since      2.2.0
3143
		 * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead.
3144
		 * @see        TGM_Plugin_Activation::_get_plugin_data_from_name()
3145
		 *
3146
		 * @param string $name Name of the plugin, as it was registered.
3147
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
3148
		 * @return string|boolean Plugin slug if found, false otherwise.
3149
		 */
3150
		protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
3151
			_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' );
3152
3153
			return $this->tgmpa->_get_plugin_data_from_name( $name, $data );
3154
		}
3155
	}
3156
} // End if().
3157
3158
3159
if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
3160
3161
	/**
3162
	 * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+.
3163
	 *
3164
	 * @since 2.5.2
3165
	 *
3166
	 * {@internal The TGMPA_Bulk_Installer class was originally called TGM_Bulk_Installer.
3167
	 *            For more information, see that class.}}
3168
	 */
3169
	class TGM_Bulk_Installer {
3170
	}
3171
}
3172
if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
3173
3174
	/**
3175
	 * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+.
3176
	 *
3177
	 * @since 2.5.2
3178
	 *
3179
	 * {@internal The TGMPA_Bulk_Installer_Skin class was originally called TGM_Bulk_Installer_Skin.
3180
	 *            For more information, see that class.}}
3181
	 */
3182
	class TGM_Bulk_Installer_Skin {
3183
	}
3184
}
3185
3186
/**
3187
 * The WP_Upgrader file isn't always available. If it isn't available,
3188
 * we load it here.
3189
 *
3190
 * We check to make sure no action or activation keys are set so that WordPress
3191
 * does not try to re-include the class when processing upgrades or installs outside
3192
 * of the class.
3193
 *
3194
 * @since 2.2.0
3195
 */
3196
add_action( 'admin_init', 'tgmpa_load_bulk_installer' );
3197
if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
3198
	/**
3199
	 * Load bulk installer
3200
	 */
3201
	function tgmpa_load_bulk_installer() {
3202
		// Silently fail if 2.5+ is loaded *after* an older version.
3203
		if ( ! isset( $GLOBALS['tgmpa'] ) ) {
3204
			return;
3205
		}
3206
3207
		// Get TGMPA class instance.
3208
		$tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3209
3210
		if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) {
3211
			if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
3212
				require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
3213
			}
3214
3215
			if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) {
3216
3217
				/**
3218
				 * Installer class to handle bulk plugin installations.
3219
				 *
3220
				 * Extends WP_Upgrader and customizes to suit the installation of multiple
3221
				 * plugins.
3222
				 *
3223
				 * @since 2.2.0
3224
				 *
3225
				 * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}}
3226
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
3227
				 *            This was done to prevent backward compatibility issues with v2.3.6.}}
3228
				 *
3229
				 * @package TGM-Plugin-Activation
3230
				 * @author  Thomas Griffin
3231
				 * @author  Gary Jones
3232
				 */
3233
				class TGMPA_Bulk_Installer extends Plugin_Upgrader {
3234
					/**
3235
					 * Holds result of bulk plugin installation.
3236
					 *
3237
					 * @since 2.2.0
3238
					 *
3239
					 * @var string
3240
					 */
3241
					public $result;
3242
3243
					/**
3244
					 * Flag to check if bulk installation is occurring or not.
3245
					 *
3246
					 * @since 2.2.0
3247
					 *
3248
					 * @var boolean
3249
					 */
3250
					public $bulk = false;
3251
3252
					/**
3253
					 * TGMPA instance
3254
					 *
3255
					 * @since 2.5.0
3256
					 *
3257
					 * @var object
3258
					 */
3259
					protected $tgmpa;
3260
3261
					/**
3262
					 * Whether or not the destination directory needs to be cleared ( = on update).
3263
					 *
3264
					 * @since 2.5.0
3265
					 *
3266
					 * @var bool
3267
					 */
3268
					protected $clear_destination = false;
3269
3270
					/**
3271
					 * References parent constructor and sets defaults for class.
3272
					 *
3273
					 * @since 2.2.0
3274
					 *
3275
					 * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
3276
					 */
3277
					public function __construct( $skin = null ) {
3278
						// Get TGMPA class instance.
3279
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3280
3281
						parent::__construct( $skin );
3282
3283
						if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
3284
							$this->clear_destination = true;
3285
						}
3286
3287
						if ( $this->tgmpa->is_automatic ) {
3288
							$this->activate_strings();
3289
						}
3290
3291
						add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
3292
					}
3293
3294
					/**
3295
					 * Sets the correct activation strings for the installer skin to use.
3296
					 *
3297
					 * @since 2.2.0
3298
					 */
3299
					public function activate_strings() {
3300
						$this->strings['activation_failed']  = __( 'Plugin activation failed.', 'tgmpa' );
3301
						$this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' );
3302
					}
3303
3304
					/**
3305
					 * Performs the actual installation of each plugin.
3306
					 *
3307
					 * @since 2.2.0
3308
					 *
3309
					 * @see WP_Upgrader::run()
3310
					 *
3311
					 * @param array $options The installation config options.
3312
					 * @return null|array Return early if error, array of installation data on success.
3313
					 */
3314
					public function run( $options ) {
3315
						$result = parent::run( $options );
3316
3317
						// Reset the strings in case we changed one during automatic activation.
3318
						if ( $this->tgmpa->is_automatic ) {
3319
							if ( 'update' === $this->skin->options['install_type'] ) {
3320
								$this->upgrade_strings();
3321
							} else {
3322
								$this->install_strings();
3323
							}
3324
						}
3325
3326
						return $result;
3327
					}
3328
3329
					/**
3330
					 * Processes the bulk installation of plugins.
3331
					 *
3332
					 * @since 2.2.0
3333
					 *
3334
					 * {@internal This is basically a near identical copy of the WP Core
3335
					 * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with
3336
					 * new installs instead of upgrades.
3337
					 * For ease of future synchronizations, the adjustments are clearly commented, but no other
3338
					 * comments are added. Code style has been made to comply.}}
3339
					 *
3340
					 * @see Plugin_Upgrader::bulk_upgrade()
3341
					 * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
3342
					 * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}}
3343
					 *
3344
					 * @param array $plugins The plugin sources needed for installation.
3345
					 * @param array $args    Arbitrary passed extra arguments.
3346
					 * @return array|false   Install confirmation messages on success, false on failure.
3347
					 */
3348
					public function bulk_install( $plugins, $args = array() ) {
3349
						// [TGMPA + ] Hook auto-activation in.
3350
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3351
3352
						$defaults    = array(
3353
							'clear_update_cache' => true,
3354
						);
3355
						$parsed_args = wp_parse_args( $args, $defaults );
3356
3357
						$this->init();
3358
						$this->bulk = true;
3359
3360
						$this->install_strings(); // [TGMPA + ] adjusted.
3361
3362
						/* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
3363
3364
						/* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
3365
3366
						$this->skin->header();
3367
3368
						// Connect to the Filesystem first.
3369
						$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
3370
						if ( ! $res ) {
3371
							$this->skin->footer();
3372
							return false;
3373
						}
3374
3375
						$this->skin->bulk_header();
3376
3377
						/*
3378
						 * Only start maintenance mode if:
3379
						 * - running Multisite and there are one or more plugins specified, OR
3380
						 * - a plugin with an update available is currently active.
3381
						 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
3382
						 */
3383
						$maintenance = ( is_multisite() && ! empty( $plugins ) );
3384
3385
						/*
3386
						[TGMPA - ]
3387
						foreach ( $plugins as $plugin )
3388
							$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
3389
						*/
3390
						if ( $maintenance ) {
3391
							$this->maintenance_mode( true );
3392
						}
3393
3394
						$results = array();
3395
3396
						$this->update_count   = count( $plugins );
3397
						$this->update_current = 0;
3398
						foreach ( $plugins as $plugin ) {
3399
							$this->update_current++;
3400
3401
							/*
3402
							[TGMPA - ]
3403
							$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
3404
3405
							if ( !isset( $current->response[ $plugin ] ) ) {
3406
								$this->skin->set_result('up_to_date');
3407
								$this->skin->before();
3408
								$this->skin->feedback('up_to_date');
3409
								$this->skin->after();
3410
								$results[$plugin] = true;
3411
								continue;
3412
							}
3413
3414
							// Get the URL to the zip file.
3415
							$r = $current->response[ $plugin ];
3416
3417
							$this->skin->plugin_active = is_plugin_active($plugin);
3418
							*/
3419
3420
							$result = $this->run(
3421
								array(
3422
									'package'           => $plugin, // [TGMPA + ] adjusted.
3423
									'destination'       => WP_PLUGIN_DIR,
3424
									'clear_destination' => false, // [TGMPA + ] adjusted.
3425
									'clear_working'     => true,
3426
									'is_multi'          => true,
3427
									'hook_extra'        => array(
3428
										'plugin' => $plugin,
3429
									),
3430
								)
3431
							);
3432
3433
							$results[ $plugin ] = $this->result;
3434
3435
							// Prevent credentials auth screen from displaying multiple times.
3436
							if ( false === $result ) {
3437
								break;
3438
							}
3439
						} // End foreach().
3440
3441
						$this->maintenance_mode( false );
3442
3443
						/**
3444
						 * Fires when the bulk upgrader process is complete.
3445
						 *
3446
						 * @since WP 3.6.0 / TGMPA 2.5.0
3447
						 *
3448
						 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
3449
						 *                              be a Theme_Upgrader or Core_Upgrade instance.
3450
						 * @param array           $data {
3451
						 *     Array of bulk item update data.
3452
						 *
3453
						 *     @type string $action   Type of action. Default 'update'.
3454
						 *     @type string $type     Type of update process. Accepts 'plugin', 'theme', or 'core'.
3455
						 *     @type bool   $bulk     Whether the update process is a bulk update. Default true.
3456
						 *     @type array  $packages Array of plugin, theme, or core packages to update.
3457
						 * }
3458
						 */
3459
						do_action( 'upgrader_process_complete', $this, array(
3460
							'action'  => 'install', // [TGMPA + ] adjusted.
3461
							'type'    => 'plugin',
3462
							'bulk'    => true,
3463
							'plugins' => $plugins,
3464
						) );
3465
3466
						$this->skin->bulk_footer();
3467
3468
						$this->skin->footer();
3469
3470
						// Cleanup our hooks, in case something else does a upgrade on this connection.
3471
						/* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
3472
3473
						// [TGMPA + ] Remove our auto-activation hook.
3474
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3475
3476
						// Force refresh of plugin update information.
3477
						wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
3478
3479
						return $results;
3480
					}
3481
3482
					/**
3483
					 * Handle a bulk upgrade request.
3484
					 *
3485
					 * @since 2.5.0
3486
					 *
3487
					 * @see Plugin_Upgrader::bulk_upgrade()
3488
					 *
3489
					 * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
3490
					 * @param array $args    Arbitrary passed extra arguments.
3491
					 * @return string|bool Install confirmation messages on success, false on failure.
3492
					 */
3493
					public function bulk_upgrade( $plugins, $args = array() ) {
3494
3495
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3496
3497
						$result = parent::bulk_upgrade( $plugins, $args );
3498
3499
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3500
3501
						return $result;
3502
					}
3503
3504
					/**
3505
					 * Abuse a filter to auto-activate plugins after installation.
3506
					 *
3507
					 * Hooked into the 'upgrader_post_install' filter hook.
3508
					 *
3509
					 * @since 2.5.0
3510
					 *
3511
					 * @param bool $bool The value we need to give back (true).
3512
					 * @return bool
3513
					 */
3514
					public function auto_activate( $bool ) {
3515
						// Only process the activation of installed plugins if the automatic flag is set to true.
3516
						if ( $this->tgmpa->is_automatic ) {
3517
							// Flush plugins cache so the headers of the newly installed plugins will be read correctly.
3518
							wp_clean_plugins_cache();
3519
3520
							// Get the installed plugin file.
3521
							$plugin_info = $this->plugin_info();
3522
3523
							// Don't try to activate on upgrade of active plugin as WP will do this already.
3524
							if ( ! is_plugin_active( $plugin_info ) ) {
3525
								$activate = activate_plugin( $plugin_info );
3526
3527
								// Adjust the success string based on the activation result.
3528
								$this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
3529
3530
								if ( is_wp_error( $activate ) ) {
3531
									$this->skin->error( $activate );
3532
									$this->strings['process_success'] .= $this->strings['activation_failed'];
3533
								} else {
3534
									$this->strings['process_success'] .= $this->strings['activation_success'];
3535
								}
3536
							}
3537
						}
3538
3539
						return $bool;
3540
					}
3541
				}
3542
			} // End if().
3543
3544
			if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) {
3545
3546
				/**
3547
				 * Installer skin to set strings for the bulk plugin installations..
3548
				 *
3549
				 * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
3550
				 * plugins.
3551
				 *
3552
				 * @since 2.2.0
3553
				 *
3554
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to
3555
				 *            TGMPA_Bulk_Installer_Skin.
3556
				 *            This was done to prevent backward compatibility issues with v2.3.6.}}
3557
				 *
3558
				 * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php
3559
				 *
3560
				 * @package TGM-Plugin-Activation
3561
				 * @author  Thomas Griffin
3562
				 * @author  Gary Jones
3563
				 */
3564
				class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
3565
					/**
3566
					 * Holds plugin info for each individual plugin installation.
3567
					 *
3568
					 * @since 2.2.0
3569
					 *
3570
					 * @var array
3571
					 */
3572
					public $plugin_info = array();
3573
3574
					/**
3575
					 * Holds names of plugins that are undergoing bulk installations.
3576
					 *
3577
					 * @since 2.2.0
3578
					 *
3579
					 * @var array
3580
					 */
3581
					public $plugin_names = array();
3582
3583
					/**
3584
					 * Integer to use for iteration through each plugin installation.
3585
					 *
3586
					 * @since 2.2.0
3587
					 *
3588
					 * @var integer
3589
					 */
3590
					public $i = 0;
3591
3592
					/**
3593
					 * TGMPA instance
3594
					 *
3595
					 * @since 2.5.0
3596
					 *
3597
					 * @var object
3598
					 */
3599
					protected $tgmpa;
3600
3601
					/**
3602
					 * Constructor. Parses default args with new ones and extracts them for use.
3603
					 *
3604
					 * @since 2.2.0
3605
					 *
3606
					 * @param array $args Arguments to pass for use within the class.
3607
					 */
3608
					public function __construct( $args = array() ) {
3609
						// Get TGMPA class instance.
3610
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3611
3612
						// Parse default and new args.
3613
						$defaults = array(
3614
							'url'          => '',
3615
							'nonce'        => '',
3616
							'names'        => array(),
3617
							'install_type' => 'install',
3618
						);
3619
						$args     = wp_parse_args( $args, $defaults );
3620
3621
						// Set plugin names to $this->plugin_names property.
3622
						$this->plugin_names = $args['names'];
3623
3624
						// Extract the new args.
3625
						parent::__construct( $args );
3626
					}
3627
3628
					/**
3629
					 * Sets install skin strings for each individual plugin.
3630
					 *
3631
					 * Checks to see if the automatic activation flag is set and uses the
3632
					 * the proper strings accordingly.
3633
					 *
3634
					 * @since 2.2.0
3635
					 */
3636
					public function add_strings() {
3637
						if ( 'update' === $this->options['install_type'] ) {
3638
							parent::add_strings();
3639
							/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3640
							$this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3641
						} else {
3642
							/* translators: 1: plugin name, 2: error message. */
3643
							$this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
3644
							/* translators: 1: plugin name. */
3645
							$this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'tgmpa' );
3646
3647
							if ( $this->tgmpa->is_automatic ) {
3648
								// Automatic activation strings.
3649
								$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' );
3650
								/* translators: 1: plugin name. */
3651
								$this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'tgmpa' ) . ' <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>';
3652
								$this->upgrader->strings['skin_upgrade_end']       = __( 'All installations and activations have been completed.', 'tgmpa' );
3653
								/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3654
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3655
							} else {
3656
								// Default installation strings.
3657
								$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' );
3658
								/* translators: 1: plugin name. */
3659
								$this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'tgmpa' ) . ' <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>';
3660
								$this->upgrader->strings['skin_upgrade_end']       = __( 'All installations have been completed.', 'tgmpa' );
3661
								/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3662
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3663
							}
3664
						}
3665
					}
3666
3667
					/**
3668
					 * Outputs the header strings and necessary JS before each plugin installation.
3669
					 *
3670
					 * @since 2.2.0
3671
					 *
3672
					 * @param string $title Unused in this implementation.
3673
					 */
3674
					public function before( $title = '' ) {
3675
						if ( empty( $title ) ) {
3676
							$title = esc_html( $this->plugin_names[ $this->i ] );
3677
						}
3678
						parent::before( $title );
3679
					}
3680
3681
					/**
3682
					 * Outputs the footer strings and necessary JS after each plugin installation.
3683
					 *
3684
					 * Checks for any errors and outputs them if they exist, else output
3685
					 * success strings.
3686
					 *
3687
					 * @since 2.2.0
3688
					 *
3689
					 * @param string $title Unused in this implementation.
3690
					 */
3691
					public function after( $title = '' ) {
3692
						if ( empty( $title ) ) {
3693
							$title = esc_html( $this->plugin_names[ $this->i ] );
3694
						}
3695
						parent::after( $title );
3696
3697
						$this->i++;
3698
					}
3699
3700
					/**
3701
					 * Outputs links after bulk plugin installation is complete.
3702
					 *
3703
					 * @since 2.2.0
3704
					 */
3705
					public function bulk_footer() {
3706
						// Serve up the string to say installations (and possibly activations) are complete.
3707
						parent::bulk_footer();
3708
3709
						// Flush plugins cache so we can make sure that the installed plugins list is always up to date.
3710
						wp_clean_plugins_cache();
3711
3712
						$this->tgmpa->show_tgmpa_version();
3713
3714
						// Display message based on if all plugins are now active or not.
3715
						$update_actions = array();
3716
3717
						if ( $this->tgmpa->is_tgmpa_complete() ) {
3718
							// All plugins are active, so we display the complete string and hide the menu to protect users.
3719
							echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
3720
							$update_actions['dashboard'] = sprintf(
3721
								esc_html( $this->tgmpa->strings['complete'] ),
3722
								'<a href="' . esc_url( self_admin_url() ) . '">' . esc_html( $this->tgmpa->strings['dashboard'] ) . '</a>'
3723
							);
3724
						} else {
3725
							$update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>';
3726
						}
3727
3728
						/**
3729
						 * Filter the list of action links available following bulk plugin installs/updates.
3730
						 *
3731
						 * @since 2.5.0
3732
						 *
3733
						 * @param array $update_actions Array of plugin action links.
3734
						 * @param array $plugin_info    Array of information for the last-handled plugin.
3735
						 */
3736
						$update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
3737
3738
						if ( ! empty( $update_actions ) ) {
3739
							$this->feedback( implode( ' | ', (array) $update_actions ) );
3740
						}
3741
					}
3742
3743
					/* *********** DEPRECATED METHODS *********** */
3744
3745
					/**
3746
					 * Flush header output buffer.
3747
					 *
3748
					 * @since      2.2.0
3749
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3750
					 * @see        Bulk_Upgrader_Skin::flush_output()
3751
					 */
3752
					public function before_flush_output() {
3753
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3754
						$this->flush_output();
3755
					}
3756
3757
					/**
3758
					 * Flush footer output buffer and iterate $this->i to make sure the
3759
					 * installation strings reference the correct plugin.
3760
					 *
3761
					 * @since      2.2.0
3762
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3763
					 * @see        Bulk_Upgrader_Skin::flush_output()
3764
					 */
3765
					public function after_flush_output() {
3766
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3767
						$this->flush_output();
3768
						$this->i++;
3769
					}
3770
				}
3771
			} // End if().
3772
		} // End if().
3773
	} // End if().
3774
} // End if().
3775
3776
if ( ! class_exists( 'TGMPA_Utils' ) ) {
3777
3778
	/**
3779
	 * Generic utilities for TGMPA.
3780
	 *
3781
	 * All methods are static, poor-dev name-spacing class wrapper.
3782
	 *
3783
	 * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy.
3784
	 *
3785
	 * @since 2.5.0
3786
	 *
3787
	 * @package TGM-Plugin-Activation
3788
	 * @author  Juliette Reinders Folmer
3789
	 */
3790
	class TGMPA_Utils {
3791
		/**
3792
		 * Whether the PHP filter extension is enabled.
3793
		 *
3794
		 * @see http://php.net/book.filter
3795
		 *
3796
		 * @since 2.5.0
3797
		 *
3798
		 * @static
3799
		 *
3800
		 * @var bool $has_filters True is the extension is enabled.
3801
		 */
3802
		public static $has_filters;
3803
3804
		/**
3805
		 * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map().
3806
		 *
3807
		 * @since 2.5.0
3808
		 *
3809
		 * @static
3810
		 *
3811
		 * @param string $string Text to be wrapped.
3812
		 * @return string
3813
		 */
3814
		public static function wrap_in_em( $string ) {
3815
			return '<em>' . wp_kses_post( $string ) . '</em>';
3816
		}
3817
3818
		/**
3819
		 * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map().
3820
		 *
3821
		 * @since 2.5.0
3822
		 *
3823
		 * @static
3824
		 *
3825
		 * @param string $string Text to be wrapped.
3826
		 * @return string
3827
		 */
3828
		public static function wrap_in_strong( $string ) {
3829
			return '<strong>' . wp_kses_post( $string ) . '</strong>';
3830
		}
3831
3832
		/**
3833
		 * Helper function: Validate a value as boolean
3834
		 *
3835
		 * @since 2.5.0
3836
		 *
3837
		 * @static
3838
		 *
3839
		 * @param mixed $value Arbitrary value.
3840
		 * @return bool
3841
		 */
3842
		public static function validate_bool( $value ) {
3843
			if ( ! isset( self::$has_filters ) ) {
3844
				self::$has_filters = extension_loaded( 'filter' );
3845
			}
3846
3847
			if ( self::$has_filters ) {
3848
				return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
3849
			} else {
3850
				return self::emulate_filter_bool( $value );
3851
			}
3852
		}
3853
3854
		/**
3855
		 * Helper function: Cast a value to bool
3856
		 *
3857
		 * @since 2.5.0
3858
		 *
3859
		 * @static
3860
		 *
3861
		 * @param mixed $value Value to cast.
3862
		 * @return bool
3863
		 */
3864
		protected static function emulate_filter_bool( $value ) {
3865
			// @codingStandardsIgnoreStart
3866
			static $true  = array(
3867
				'1',
3868
				'true', 'True', 'TRUE',
3869
				'y', 'Y',
3870
				'yes', 'Yes', 'YES',
3871
				'on', 'On', 'ON',
3872
			);
3873
			static $false = array(
3874
				'0',
3875
				'false', 'False', 'FALSE',
3876
				'n', 'N',
3877
				'no', 'No', 'NO',
3878
				'off', 'Off', 'OFF',
3879
			);
3880
			// @codingStandardsIgnoreEnd
3881
3882
			if ( is_bool( $value ) ) {
3883
				return $value;
3884
			} elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
3885
				return (bool) $value;
3886
			} elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
3887
				return (bool) $value;
3888
			} elseif ( is_string( $value ) ) {
3889
				$value = trim( $value );
3890
				if ( in_array( $value, $true, true ) ) {
3891
					return true;
3892
				} elseif ( in_array( $value, $false, true ) ) {
3893
					return false;
3894
				} else {
3895
					return false;
3896
				}
3897
			}
3898
3899
			return false;
3900
		}
3901
	} // End of class TGMPA_Utils
3902
}  // End if().
3903