Completed
Push — develop ( 13b3c8...9c97e4 )
by Gary
03:06
created

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

Upgrade to new PHP Analysis Engine

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

1
<?php
2
/**
3
 * Plugin installation and activation for WordPress themes.
4
 *
5
 * Please note that this is a drop-in library for a theme or plugin.
6
 * The authors of this library (Thomas, Gary and Juliette) are NOT responsible
7
 * for the support of your plugin or theme. Please contact the plugin
8
 * or theme author for support.
9
 *
10
 * @package   TGM-Plugin-Activation
11
 * @version   2.5.2
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.5.2';
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
		 *
92
		 * @since 2.5.0 the array has the plugin slug as an associative key.
93
		 *
94
		 * @var array
95
		 */
96
		public $plugins = array();
97
98
		/**
99
		 * Holds arrays of plugin names to use to sort the plugins array.
100
		 *
101
		 * @since 2.5.0
102
		 *
103
		 * @var array
104
		 */
105
		protected $sort_order = array();
106
107
		/**
108
		 * Whether any plugins have the 'force_activation' setting set to true.
109
		 *
110
		 * @since 2.5.0
111
		 *
112
		 * @var bool
113
		 */
114
		protected $has_forced_activation = false;
115
116
		/**
117
		 * Whether any plugins have the 'force_deactivation' setting set to true.
118
		 *
119
		 * @since 2.5.0
120
		 *
121
		 * @var bool
122
		 */
123
		protected $has_forced_deactivation = false;
124
125
		/**
126
		 * Name of the unique ID to hash notices.
127
		 *
128
		 * @since 2.4.0
129
		 *
130
		 * @var string
131
		 */
132
		public $id = 'tgmpa';
133
134
		/**
135
		 * Name of the query-string argument for the admin page.
136
		 *
137
		 * @since 1.0.0
138
		 *
139
		 * @var string
140
		 */
141
		protected $menu = 'tgmpa-install-plugins';
142
143
		/**
144
		 * Parent menu file slug.
145
		 *
146
		 * @since 2.5.0
147
		 *
148
		 * @var string
149
		 */
150
		public $parent_slug = 'themes.php';
151
152
		/**
153
		 * Capability needed to view the plugin installation menu item.
154
		 *
155
		 * @since 2.5.0
156
		 *
157
		 * @var string
158
		 */
159
		public $capability = 'edit_theme_options';
160
161
		/**
162
		 * Default absolute path to folder containing bundled plugin zip files.
163
		 *
164
		 * @since 2.0.0
165
		 *
166
		 * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string.
167
		 */
168
		public $default_path = '';
169
170
		/**
171
		 * Flag to show admin notices or not.
172
		 *
173
		 * @since 2.1.0
174
		 *
175
		 * @var boolean
176
		 */
177
		public $has_notices = true;
178
179
		/**
180
		 * Flag to determine if the user can dismiss the notice nag.
181
		 *
182
		 * @since 2.4.0
183
		 *
184
		 * @var boolean
185
		 */
186
		public $dismissable = true;
187
188
		/**
189
		 * Message to be output above nag notice if dismissable is false.
190
		 *
191
		 * @since 2.4.0
192
		 *
193
		 * @var string
194
		 */
195
		public $dismiss_msg = '';
196
197
		/**
198
		 * Flag to set automatic activation of plugins. Off by default.
199
		 *
200
		 * @since 2.2.0
201
		 *
202
		 * @var boolean
203
		 */
204
		public $is_automatic = false;
205
206
		/**
207
		 * Optional message to display before the plugins table.
208
		 *
209
		 * @since 2.2.0
210
		 *
211
		 * @var string Message filtered by wp_kses_post(). Default is empty string.
212
		 */
213
		public $message = '';
214
215
		/**
216
		 * Holds configurable array of strings.
217
		 *
218
		 * Default values are added in the constructor.
219
		 *
220
		 * @since 2.0.0
221
		 *
222
		 * @var array
223
		 */
224
		public $strings = array();
225
226
		/**
227
		 * Holds the version of WordPress.
228
		 *
229
		 * @since 2.4.0
230
		 *
231
		 * @var int
232
		 */
233
		public $wp_version;
234
235
		/**
236
		 * Holds the hook name for the admin page.
237
		 *
238
		 * @since 2.5.0
239
		 *
240
		 * @var string
241
		 */
242
		public $page_hook;
243
244
		/**
245
		 * Adds a reference of this object to $instance, populates default strings,
246
		 * does the tgmpa_init action hook, and hooks in the interactions to init.
247
		 *
248
		 * {@internal This method should be `protected`, but as too many TGMPA implementations
249
		 * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues.
250
		 * Reverted back to public for the time being.}}
251
		 *
252
		 * @since 1.0.0
253
		 *
254
		 * @see TGM_Plugin_Activation::init()
255
		 */
256
		public function __construct() {
257
			// Set the current WordPress version.
258
			$this->wp_version = $GLOBALS['wp_version'];
259
260
			// Announce that the class is ready, and pass the object (for advanced use).
261
			do_action_ref_array( 'tgmpa_init', array( $this ) );
262
263
			/*
264
			 * Load our text domain and allow for overloading the fall-back file.
265
			 *
266
			 * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
267
			 * generator on the website.}}
268
			 */
269
			add_action( 'init', array( $this, 'load_textdomain' ), 5 );
270
			add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 );
271
272
			// When the rest of WP has loaded, kick-start the rest of the class.
273
			add_action( 'init', array( $this, 'init' ) );
274
		}
275
276
		/**
277
		 * Magic method to (not) set protected properties from outside of this class.
278
		 *
279
		 * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6  where the `menu` property
280
		 * is being assigned rather than tested in a conditional, effectively rendering it useless.
281
		 * This 'hack' prevents this from happening.}}
282
		 *
283
		 * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593
284
		 *
285
		 * @param string $name  Name of an inaccessible property.
286
		 * @param mixed  $value Value to assign to the property.
287
		 * @return void  Silently fail to set the property when this is tried from outside of this class context.
288
		 *               (Inside this class context, the __set() method if not used as there is direct access.)
289
		 */
290
		public function __set( $name, $value ) {
291
			return;
292
		}
293
294
		/**
295
		 * Magic method to get the value of a protected property outside of this class context.
296
		 *
297
		 * @param string $name Name of an inaccessible property.
298
		 * @return mixed The property value.
299
		 */
300
		public function __get( $name ) {
301
			return $this->{$name};
302
		}
303
304
		/**
305
		 * Initialise the interactions between this class and WordPress.
306
		 *
307
		 * Hooks in three new methods for the class: admin_menu, notices and styles.
308
		 *
309
		 * @since 2.0.0
310
		 *
311
		 * @see TGM_Plugin_Activation::admin_menu()
312
		 * @see TGM_Plugin_Activation::notices()
313
		 * @see TGM_Plugin_Activation::styles()
314
		 */
315
		public function init() {
316
			/**
317
			 * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter
318
			 * you can overrule that behaviour.
319
			 *
320
			 * @since 2.5.0
321
			 *
322
			 * @param bool $load Whether or not TGMPA should load.
323
			 *                   Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`.
324
			 */
325
			if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) {
326
				return;
327
			}
328
329
			// Load class strings.
330
			$this->strings = array(
331
				'page_title'                      => __( 'Install Required Plugins', 'tgmpa' ),
332
				'menu_title'                      => __( 'Install Plugins', 'tgmpa' ),
333
				'installing'                      => __( 'Installing Plugin: %s', 'tgmpa' ),
334
				'updating'                        => __( 'Updating Plugin: %s', 'tgmpa' ),
335
				'oops'                            => __( 'Something went wrong with the plugin API.', 'tgmpa' ),
336
				'notice_can_install_required'     => _n_noop(
337
					'This theme requires the following plugin: %1$s.',
338
					'This theme requires the following plugins: %1$s.',
339
					'tgmpa'
340
				),
341
				'notice_can_install_recommended'  => _n_noop(
342
					'This theme recommends the following plugin: %1$s.',
343
					'This theme recommends the following plugins: %1$s.',
344
					'tgmpa'
345
				),
346
				'notice_ask_to_update'            => _n_noop(
347
					'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',
348
					'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',
349
					'tgmpa'
350
				),
351
				'notice_ask_to_update_maybe'      => _n_noop(
352
					'There is an update available for: %1$s.',
353
					'There are updates available for the following plugins: %1$s.',
354
					'tgmpa'
355
				),
356
				'notice_can_activate_required'    => _n_noop(
357
					'The following required plugin is currently inactive: %1$s.',
358
					'The following required plugins are currently inactive: %1$s.',
359
					'tgmpa'
360
				),
361
				'notice_can_activate_recommended' => _n_noop(
362
					'The following recommended plugin is currently inactive: %1$s.',
363
					'The following recommended plugins are currently inactive: %1$s.',
364
					'tgmpa'
365
				),
366
				'install_link'                    => _n_noop(
367
					'Begin installing plugin',
368
					'Begin installing plugins',
369
					'tgmpa'
370
				),
371
				'update_link'                     => _n_noop(
372
					'Begin updating plugin',
373
					'Begin updating plugins',
374
					'tgmpa'
375
				),
376
				'activate_link'                   => _n_noop(
377
					'Begin activating plugin',
378
					'Begin activating plugins',
379
					'tgmpa'
380
				),
381
				'return'                          => __( 'Return to Required Plugins Installer', 'tgmpa' ),
382
				'dashboard'                       => __( 'Return to the Dashboard', 'tgmpa' ),
383
				'plugin_activated'                => __( 'Plugin activated successfully.', 'tgmpa' ),
384
				'activated_successfully'          => __( 'The following plugin was activated successfully:', 'tgmpa' ),
385
				'plugin_already_active'           => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ),
386
				'plugin_needs_higher_version'     => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ),
387
				'complete'                        => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ),
388
				'dismiss'                         => __( 'Dismiss this notice', 'tgmpa' ),
389
				'notice_cannot_install_activate'  => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ),
390
				'contact_admin'                   => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),
391
			);
392
393
			do_action( 'tgmpa_register' );
394
395
			/* After this point, the plugins should be registered and the configuration set. */
396
397
			// Proceed only if we have plugins to handle.
398
			if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
399
				return;
400
			}
401
402
			// Set up the menu and notices if we still have outstanding actions.
403
			if ( true !== $this->is_tgmpa_complete() ) {
404
				// Sort the plugins.
405
				array_multisort( $this->sort_order, SORT_ASC, $this->plugins );
406
407
				add_action( 'admin_menu', array( $this, 'admin_menu' ) );
408
				add_action( 'admin_head', array( $this, 'dismiss' ) );
409
410
				// Prevent the normal links from showing underneath a single install/update page.
411
				add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
412
				add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) );
413
414
				if ( $this->has_notices ) {
415
					add_action( 'admin_notices', array( $this, 'notices' ) );
416
					add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
417
					add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
418
				}
419
			}
420
421
			// If needed, filter plugin action links.
422
			add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 );
423
424
			// Make sure things get reset on switch theme.
425
			add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
426
427
			if ( $this->has_notices ) {
428
				add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
429
			}
430
431
			// Setup the force activation hook.
432
			if ( true === $this->has_forced_activation ) {
433
				add_action( 'admin_init', array( $this, 'force_activation' ) );
434
			}
435
436
			// Setup the force deactivation hook.
437
			if ( true === $this->has_forced_deactivation ) {
438
				add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
439
			}
440
		}
441
442
		/**
443
		 * Load translations.
444
		 *
445
		 * @since 2.x.x
446
		 *
447
		 * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to
448
		 * get round the different ways of handling the path and deprecated notices being thrown
449
		 * and such. For plugins, the actual file name will be corrected by a filter.}}
450
		 *
451
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
452
		 * generator on the website.}}
453
		 */
454
		public function load_textdomain() {
455
			if ( is_textdomain_loaded( 'tgmpa' ) ) {
456
				return;
457
			}
458
459
			if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) {
460
				// Plugin, we'll need to adjust the file name.
461
				add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 );
462
				load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
463
				remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 );
464
			} else {
465
				load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
466
			}
467
		}
468
469
		/**
470
		 * Correct the .mo file name for (must-use) plugins.
471
		 *
472
		 * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`.
473
		 *
474
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
475
		 * generator on the website.}}
476
		 *
477
		 * @since 2.x.x
478
		 *
479
		 * @param string $mofile Full path to the target mofile.
480
		 * @param string $domain The domain for which a language file is being loaded.
481
		 * @return string $mofile
482
		 */
483
		public function correct_plugin_mofile( $mofile, $domain ) {
484
			// Exit early if not our domain (just in case).
485
			if ( 'tgmpa' !== $domain ) {
486
				return $mofile;
487
			}
488
			return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile );
489
		}
490
491
		/**
492
		 * Potentially overload the fall-back translation file for the current language.
493
		 *
494
		 * WP, by default since WP 3.7, will load a local translation first and if none
495
		 * can be found, will try and find a translation in the /wp-content/languages/ directory.
496
		 * As this library is theme/plugin agnostic, translation files for TGMPA can exist both
497
		 * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory.
498
		 *
499
		 * This method makes sure both directories are checked.
500
		 *
501
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
502
		 * generator on the website.}}
503
		 *
504
		 * @since 2.x.x
505
		 *
506
		 * @param string $mofile Full path to the target mofile.
507
		 * @param string $domain The domain for which a language file is being loaded.
508
		 * @return string $mofile
509
		 */
510
		public function overload_textdomain_mofile( $mofile, $domain ) {
511
			// Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
512
			if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) {
513
				return $mofile;
514
			}
515
516
			// Current fallback file is not valid, let's try the alternative option.
517
			if ( false !== strpos( $mofile, '/themes/' ) ) {
518
				return str_replace( '/themes/', '/plugins/', $mofile );
519
			} elseif ( false !== strpos( $mofile, '/plugins/' ) ) {
520
				return str_replace( '/plugins/', '/themes/', $mofile );
521
			} else {
522
				return $mofile;
523
			}
524
		}
525
526
		/**
527
		 * Hook in plugin action link filters for the WP native plugins page.
528
		 *
529
		 * - Prevent activation of plugins which don't meet the minimum version requirements.
530
		 * - Prevent deactivation of force-activated plugins.
531
		 * - Add update notice if update available.
532
		 *
533
		 * @since 2.5.0
534
		 */
535
		public function add_plugin_action_link_filters() {
536
			foreach ( $this->plugins as $slug => $plugin ) {
537
				if ( false === $this->can_plugin_activate( $slug ) ) {
538
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 );
539
				}
540
541
				if ( true === $plugin['force_activation'] ) {
542
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 );
543
				}
544
545
				if ( false !== $this->does_plugin_require_update( $slug ) ) {
546
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 );
547
				}
548
			}
549
		}
550
551
		/**
552
		 * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the
553
		 * minimum version requirements.
554
		 *
555
		 * @since 2.5.0
556
		 *
557
		 * @param array $actions Action links.
558
		 * @return array
559
		 */
560
		public function filter_plugin_action_links_activate( $actions ) {
561
			unset( $actions['activate'] );
562
563
			return $actions;
564
		}
565
566
		/**
567
		 * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate.
568
		 *
569
		 * @since 2.5.0
570
		 *
571
		 * @param array $actions Action links.
572
		 * @return array
573
		 */
574
		public function filter_plugin_action_links_deactivate( $actions ) {
575
			unset( $actions['deactivate'] );
576
577
			return $actions;
578
		}
579
580
		/**
581
		 * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the
582
		 * minimum version requirements.
583
		 *
584
		 * @since 2.5.0
585
		 *
586
		 * @param array $actions Action links.
587
		 * @return array
588
		 */
589
		public function filter_plugin_action_links_update( $actions ) {
590
			$actions['update'] = sprintf(
591
				'<a href="%1$s" title="%2$s" class="edit">%3$s</a>',
592
				esc_url( $this->get_tgmpa_status_url( 'update' ) ),
593
				esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ),
594
				esc_html__( 'Update Required', 'tgmpa' )
595
			);
596
597
			return $actions;
598
		}
599
600
		/**
601
		 * Handles calls to show plugin information via links in the notices.
602
		 *
603
		 * We get the links in the admin notices to point to the TGMPA page, rather
604
		 * than the typical plugin-install.php file, so we can prepare everything
605
		 * beforehand.
606
		 *
607
		 * WP does not make it easy to show the plugin information in the thickbox -
608
		 * here we have to require a file that includes a function that does the
609
		 * main work of displaying it, enqueue some styles, set up some globals and
610
		 * finally call that function before exiting.
611
		 *
612
		 * Down right easy once you know how...
613
		 *
614
		 * Returns early if not the TGMPA page.
615
		 *
616
		 * @since 2.1.0
617
		 *
618
		 * @global string $tab Used as iframe div class names, helps with styling
619
		 * @global string $body_id Used as the iframe body ID, helps with styling
620
		 *
621
		 * @return null Returns early if not the TGMPA page.
622
		 */
623
		public function admin_init() {
624
			if ( ! $this->is_tgmpa_page() ) {
625
				return;
626
			}
627
628
			if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) {
629
				// Needed for install_plugin_information().
630
				require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
631
632
				wp_enqueue_style( 'plugin-install' );
633
634
				global $tab, $body_id;
635
				$body_id = 'plugin-information';
636
				// @codingStandardsIgnoreStart
637
				$tab     = 'plugin-information';
638
				// @codingStandardsIgnoreEnd
639
640
				install_plugin_information();
641
642
				exit;
643
			}
644
		}
645
646
		/**
647
		 * Enqueue thickbox scripts/styles for plugin info.
648
		 *
649
		 * Thickbox is not automatically included on all admin pages, so we must
650
		 * manually enqueue it for those pages.
651
		 *
652
		 * Thickbox is only loaded if the user has not dismissed the admin
653
		 * notice or if there are any plugins left to install and activate.
654
		 *
655
		 * @since 2.1.0
656
		 */
657
		public function thickbox() {
658
			if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
659
				add_thickbox();
660
			}
661
		}
662
663
		/**
664
		 * Adds submenu page if there are plugin actions to take.
665
		 *
666
		 * This method adds the submenu page letting users know that a required
667
		 * plugin needs to be installed.
668
		 *
669
		 * This page disappears once the plugin has been installed and activated.
670
		 *
671
		 * @since 1.0.0
672
		 *
673
		 * @see TGM_Plugin_Activation::init()
674
		 * @see TGM_Plugin_Activation::install_plugins_page()
675
		 *
676
		 * @return null Return early if user lacks capability to install a plugin.
677
		 */
678
		public function admin_menu() {
679
			// Make sure privileges are correct to see the page.
680
			if ( ! current_user_can( 'install_plugins' ) ) {
681
				return;
682
			}
683
684
			$args = apply_filters(
685
				'tgmpa_admin_menu_args',
686
				array(
687
					'parent_slug' => $this->parent_slug,                     // Parent Menu slug.
688
					'page_title'  => $this->strings['page_title'],           // Page title.
689
					'menu_title'  => $this->strings['menu_title'],           // Menu title.
690
					'capability'  => $this->capability,                      // Capability.
691
					'menu_slug'   => $this->menu,                            // Menu slug.
692
					'function'    => array( $this, 'install_plugins_page' ), // Callback.
693
				)
694
			);
695
696
			$this->add_admin_menu( $args );
697
		}
698
699
		/**
700
		 * Add the menu item.
701
		 *
702
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
703
		 * generator on the website.}}
704
		 *
705
		 * @since 2.5.0
706
		 *
707
		 * @param array $args Menu item configuration.
708
		 */
709
		protected function add_admin_menu( array $args ) {
710
			if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) {
711
				_deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) );
712
			}
713
714
			if ( 'themes.php' === $this->parent_slug ) {
715
				$this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
716
			} else {
717
				$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'] );
718
			}
719
		}
720
721
		/**
722
		 * Echoes plugin installation form.
723
		 *
724
		 * This method is the callback for the admin_menu method function.
725
		 * This displays the admin page and form area where the user can select to install and activate the plugin.
726
		 * Aborts early if we're processing a plugin installation action.
727
		 *
728
		 * @since 1.0.0
729
		 *
730
		 * @return null Aborts early if we're processing a plugin installation action.
731
		 */
732
		public function install_plugins_page() {
733
			// Store new instance of plugin table in object.
734
			$plugin_table = new TGMPA_List_Table;
735
736
			// Return early if processing a plugin installation action.
737
			if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
738
				return;
739
			}
740
741
			// Force refresh of available plugin information so we'll know about manual updates/deletes.
742
			wp_clean_plugins_cache( false );
743
744
			?>
745
			<div class="tgmpa wrap">
746
				<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
747
				<?php $plugin_table->prepare_items(); ?>
748
749
				<?php
750
				if ( ! empty( $this->message ) && is_string( $this->message ) ) {
751
					echo wp_kses_post( $this->message );
752
				}
753
				?>
754
				<?php $plugin_table->views(); ?>
755
756
				<form id="tgmpa-plugins" action="" method="post">
757
					<input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
758
					<input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
759
					<?php $plugin_table->display(); ?>
760
				</form>
761
			</div>
762
			<?php
763
		}
764
765
		/**
766
		 * Installs, updates or activates a plugin depending on the action link clicked by the user.
767
		 *
768
		 * Checks the $_GET variable to see which actions have been
769
		 * passed and responds with the appropriate method.
770
		 *
771
		 * Uses WP_Filesystem to process and handle the plugin installation
772
		 * method.
773
		 *
774
		 * @since 1.0.0
775
		 *
776
		 * @uses WP_Filesystem
777
		 * @uses WP_Error
778
		 * @uses WP_Upgrader
779
		 * @uses Plugin_Upgrader
780
		 * @uses Plugin_Installer_Skin
781
		 * @uses Plugin_Upgrader_Skin
782
		 *
783
		 * @return boolean True on success, false on failure.
784
		 */
785
		protected function do_plugin_install() {
786
			if ( empty( $_GET['plugin'] ) ) {
787
				return false;
788
			}
789
790
			// All plugin information will be stored in an array for processing.
791
			$slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
792
793
			if ( ! isset( $this->plugins[ $slug ] ) ) {
794
				return false;
795
			}
796
797
			// Was an install or upgrade action link clicked?
798
			if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
799
800
				$install_type = 'install';
801
				if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
802
					$install_type = 'update';
803
				}
804
805
				check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
806
807
				// Pass necessary information via URL if WP_Filesystem is needed.
808
				$url = wp_nonce_url(
809
					add_query_arg(
810
						array(
811
							'plugin'                 => urlencode( $slug ),
812
							'tgmpa-' . $install_type => $install_type . '-plugin',
813
						),
814
						$this->get_tgmpa_url()
815
					),
816
					'tgmpa-' . $install_type,
817
					'tgmpa-nonce'
818
				);
819
820
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
821
822
				if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) {
823
					return true;
824
				}
825
826 View Code Duplication
				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...
827
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
828
					return true;
829
				}
830
831
				/* If we arrive here, we have the filesystem. */
832
833
				// Prep variables for Plugin_Installer_Skin class.
834
				$extra         = array();
835
				$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
836
				$source        = $this->get_download_url( $slug );
837
				$api           = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
838
				$api           = ( false !== $api ) ? $api : null;
839
840
				$url = add_query_arg(
841
					array(
842
						'action' => $install_type . '-plugin',
843
						'plugin' => urlencode( $slug ),
844
					),
845
					'update.php'
846
				);
847
848
				if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
849
					require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
850
				}
851
852
				$title     = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
853
				$skin_args = array(
854
					'type'   => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
855
					'title'  => sprintf( $title, $this->plugins[ $slug ]['name'] ),
856
					'url'    => esc_url_raw( $url ),
857
					'nonce'  => $install_type . '-plugin_' . $slug,
858
					'plugin' => '',
859
					'api'    => $api,
860
					'extra'  => $extra,
861
				);
862
				unset( $title );
863
864
				if ( 'update' === $install_type ) {
865
					$skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
866
					$skin                = new Plugin_Upgrader_Skin( $skin_args );
867
				} else {
868
					$skin = new Plugin_Installer_Skin( $skin_args );
869
				}
870
871
				// Create a new instance of Plugin_Upgrader.
872
				$upgrader = new Plugin_Upgrader( $skin );
873
874
				// Perform the action and install the plugin from the $source urldecode().
875
				add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
876
877
				if ( 'update' === $install_type ) {
878
					// Inject our info into the update transient.
879
					$to_inject                    = array( $slug => $this->plugins[ $slug ] );
880
					$to_inject[ $slug ]['source'] = $source;
881
					$this->inject_update_info( $to_inject );
882
883
					$upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
884
				} else {
885
					$upgrader->install( $source );
886
				}
887
888
				remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 );
889
890
				// Make sure we have the correct file path now the plugin is installed/updated.
891
				$this->populate_file_path( $slug );
892
893
				// Only activate plugins if the config option is set to true and the plugin isn't
894
				// already active (upgrade).
895
				if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
896
					$plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
897
					if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
898
						return true; // Finish execution of the function early as we encountered an error.
899
					}
900
				}
901
902
				$this->show_tgmpa_version();
903
904
				// Display message based on if all plugins are now active or not.
905
				if ( $this->is_tgmpa_complete() ) {
906
					echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ), '</p>';
907
					echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
908
				} else {
909
					echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
910
				}
911
912
				return true;
913
			} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
914
				// Activate action link was clicked.
915
				check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
916
917
				if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
918
					return true; // Finish execution of the function early as we encountered an error.
919
				}
920
			}
921
922
			return false;
923
		}
924
925
		/**
926
		 * Inject information into the 'update_plugins' site transient as WP checks that before running an update.
927
		 *
928
		 * @since 2.5.0
929
		 *
930
		 * @param array $plugins The plugin information for the plugins which are to be updated.
931
		 */
932
		public function inject_update_info( $plugins ) {
933
			$repo_updates = get_site_transient( 'update_plugins' );
934
935
			if ( ! is_object( $repo_updates ) ) {
936
				$repo_updates = new stdClass;
937
			}
938
939
			foreach ( $plugins as $slug => $plugin ) {
940
				$file_path = $plugin['file_path'];
941
942
				if ( empty( $repo_updates->response[ $file_path ] ) ) {
943
					$repo_updates->response[ $file_path ] = new stdClass;
944
				}
945
946
				// We only really need to set package, but let's do all we can in case WP changes something.
947
				$repo_updates->response[ $file_path ]->slug        = $slug;
948
				$repo_updates->response[ $file_path ]->plugin      = $file_path;
949
				$repo_updates->response[ $file_path ]->new_version = $plugin['version'];
950
				$repo_updates->response[ $file_path ]->package     = $plugin['source'];
951
				if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
952
					$repo_updates->response[ $file_path ]->url = $plugin['external_url'];
953
				}
954
			}
955
956
			set_site_transient( 'update_plugins', $repo_updates );
957
		}
958
959
		/**
960
		 * Adjust the plugin directory name if necessary.
961
		 *
962
		 * The final destination directory of a plugin is based on the subdirectory name found in the
963
		 * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
964
		 * subdirectory name is not the same as the expected slug and the plugin will not be recognized
965
		 * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
966
		 * the expected plugin slug.
967
		 *
968
		 * @since 2.5.0
969
		 *
970
		 * @param string       $source        Path to upgrade/zip-file-name.tmp/subdirectory/.
971
		 * @param string       $remote_source Path to upgrade/zip-file-name.tmp.
972
		 * @param \WP_Upgrader $upgrader      Instance of the upgrader which installs the plugin.
973
		 * @return string $source
974
		 */
975
		public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
976
			if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
977
				return $source;
978
			}
979
980
			// Check for single file plugins.
981
			$source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
982
			if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
983
				return $source;
984
			}
985
986
			// Multi-file plugin, let's see if the directory is correctly named.
987
			$desired_slug = '';
988
989
			// Figure out what the slug is supposed to be.
990
			if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
991
				$desired_slug = $upgrader->skin->options['extra']['slug'];
992
			} else {
993
				// Bulk installer contains less info, so fall back on the info registered here.
994
				foreach ( $this->plugins as $slug => $plugin ) {
995
					if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
996
						$desired_slug = $slug;
997
						break;
998
					}
999
				}
1000
				unset( $slug, $plugin );
1001
			}
1002
1003
			if ( ! empty( $desired_slug ) ) {
1004
				$subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
1005
1006
				if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
1007
					$from = untrailingslashit( $source );
1008
					$to   = trailingslashit( $remote_source ) . $desired_slug;
1009
1010
					if ( true === $GLOBALS['wp_filesystem']->move( $from, $to ) ) {
1011
						return trailingslashit( $to );
1012 View Code Duplication
					} else {
1013
						return new WP_Error( 'rename_failed', esc_html__( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) );
1014
					}
1015 View Code Duplication
				} elseif ( empty( $subdir_name ) ) {
1016
					return new WP_Error( 'packaged_wrong', esc_html__( 'The remote plugin package consists of more than one file, but the files are not packaged in a folder.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) );
1017
				}
1018
			}
1019
1020
			return $source;
1021
		}
1022
1023
		/**
1024
		 * Activate a single plugin and send feedback about the result to the screen.
1025
		 *
1026
		 * @since 2.5.0
1027
		 *
1028
		 * @param string $file_path Path within wp-plugins/ to main plugin file.
1029
		 * @param string $slug      Plugin slug.
1030
		 * @param bool   $automatic Whether this is an automatic activation after an install. Defaults to false.
1031
		 *                          This determines the styling of the output messages.
1032
		 * @return bool False if an error was encountered, true otherwise.
1033
		 */
1034
		protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
1035
			if ( $this->can_plugin_activate( $slug ) ) {
1036
				$activate = activate_plugin( $file_path );
1037
1038
				if ( is_wp_error( $activate ) ) {
1039
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
1040
						'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
1041
1042
					return false; // End it here if there is an error with activation.
1043
				} else {
1044
					if ( ! $automatic ) {
1045
						// Make sure message doesn't display again if bulk activation is performed
1046
						// immediately after a single activation.
1047
						if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1048
							echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
1049
						}
1050
					} else {
1051
						// Simpler message layout for use on the plugin install page.
1052
						echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
1053
					}
1054
				}
1055 View Code Duplication
			} elseif ( $this->is_plugin_active( $slug ) ) {
1056
				// No simpler message format provided as this message should never be encountered
1057
				// on the plugin install page.
1058
				echo '<div id="message" class="error"><p>',
1059
					sprintf(
1060
						esc_html( $this->strings['plugin_already_active'] ),
1061
						'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1062
					),
1063
					'</p></div>';
1064
			} elseif ( $this->does_plugin_require_update( $slug ) ) {
1065
				if ( ! $automatic ) {
1066
					// Make sure message doesn't display again if bulk activation is performed
1067
					// immediately after a single activation.
1068 View Code Duplication
					if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1069
						echo '<div id="message" class="error"><p>',
1070
							sprintf(
1071
								esc_html( $this->strings['plugin_needs_higher_version'] ),
1072
								'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1073
							),
1074
							'</p></div>';
1075
					}
1076
				} else {
1077
					// Simpler message layout for use on the plugin install page.
1078
					echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
1079
				}
1080
			}
1081
1082
			return true;
1083
		}
1084
1085
		/**
1086
		 * Echoes required plugin notice.
1087
		 *
1088
		 * Outputs a message telling users that a specific plugin is required for
1089
		 * their theme. If appropriate, it includes a link to the form page where
1090
		 * users can install and activate the plugin.
1091
		 *
1092
		 * Returns early if we're on the Install page.
1093
		 *
1094
		 * @since 1.0.0
1095
		 *
1096
		 * @global object $current_screen
1097
		 *
1098
		 * @return null Returns early if we're on the Install page.
1099
		 */
1100
		public function notices() {
1101
			// Remove nag on the install page / Return early if the nag message has been dismissed or user < author.
1102
			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' ) ) ) {
1103
				return;
1104
			}
1105
1106
			// Store for the plugin slugs by message type.
1107
			$message = array();
1108
1109
			// Initialize counters used to determine plurality of action link texts.
1110
			$install_link_count          = 0;
1111
			$update_link_count           = 0;
1112
			$activate_link_count         = 0;
1113
			$total_required_action_count = 0;
1114
1115
			foreach ( $this->plugins as $slug => $plugin ) {
1116
				if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) {
1117
					continue;
1118
				}
1119
1120
				if ( ! $this->is_plugin_installed( $slug ) ) {
1121
					if ( current_user_can( 'install_plugins' ) ) {
1122
						$install_link_count++;
1123
1124
						if ( true === $plugin['required'] ) {
1125
							$message['notice_can_install_required'][] = $slug;
1126
						} else {
1127
							$message['notice_can_install_recommended'][] = $slug;
1128
						}
1129
					}
1130
					if ( true === $plugin['required'] ) {
1131
						$total_required_action_count++;
1132
					}
1133
				} else {
1134
					if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
1135
						if ( current_user_can( 'activate_plugins' ) ) {
1136
							$activate_link_count++;
1137
1138
							if ( true === $plugin['required'] ) {
1139
								$message['notice_can_activate_required'][] = $slug;
1140
							} else {
1141
								$message['notice_can_activate_recommended'][] = $slug;
1142
							}
1143
						}
1144
						if ( true === $plugin['required'] ) {
1145
							$total_required_action_count++;
1146
						}
1147
					}
1148
1149
					if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1150
1151
						if ( current_user_can( 'update_plugins' ) ) {
1152
							$update_link_count++;
1153
1154
							if ( $this->does_plugin_require_update( $slug ) ) {
1155
								$message['notice_ask_to_update'][] = $slug;
1156
							} elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
1157
								$message['notice_ask_to_update_maybe'][] = $slug;
1158
							}
1159
						}
1160
						if ( true === $plugin['required'] ) {
1161
							$total_required_action_count++;
1162
						}
1163
					}
1164
				}
1165
			}
1166
			unset( $slug, $plugin );
1167
1168
			// If we have notices to display, we move forward.
1169
			if ( ! empty( $message ) || $total_required_action_count > 0 ) {
1170
				krsort( $message ); // Sort messages.
1171
				$rendered = '';
1172
1173
				// As add_settings_error() wraps the final message in a <p> and as the final message can't be
1174
				// filtered, using <p>'s in our html would render invalid html output.
1175
				$line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
1176
1177
				if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
1178
					$rendered = esc_html__( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html__( $this->strings['contact_admin'] );
1179
					$rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
1180
				} else {
1181
1182
					// If dismissable is false and a message is set, output it now.
1183
					if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
1184
						$rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
1185
					}
1186
1187
					// Render the individual message lines for the notice.
1188
					foreach ( $message as $type => $plugin_group ) {
1189
						$linked_plugins = array();
1190
1191
						// Get the external info link for a plugin if one is available.
1192
						foreach ( $plugin_group as $plugin_slug ) {
1193
							$linked_plugins[] = $this->get_info_link( $plugin_slug );
1194
						}
1195
						unset( $plugin_slug );
1196
1197
						$count          = count( $plugin_group );
1198
						$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
1199
						$last_plugin    = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
1200
						$imploded       = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
1201
1202
						$rendered .= sprintf(
1203
							$line_template,
1204
							sprintf(
1205
								translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
1206
								$imploded,
1207
								$count
1208
							)
1209
						);
1210
1211
					}
1212
					unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
1213
1214
					$rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
1215
				}
1216
1217
				// Register the nag messages and prepare them to be processed.
1218
				add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
1219
			}
1220
1221
			// Admin options pages already output settings_errors, so this is to avoid duplication.
1222
			if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
1223
				$this->display_settings_errors();
1224
			}
1225
		}
1226
1227
		/**
1228
		 * Generate the user action links for the admin notice.
1229
		 *
1230
		 * @since 2.x.x
1231
		 *
1232
		 * @param int $install_count  Number of plugins to install.
1233
		 * @param int $update_count   Number of plugins to update.
1234
		 * @param int $activate_count Number of plugins to activate.
1235
		 * @param int $line_template  Template for the HTML tag to output a line.
1236
		 * @return string Action links.
1237
		 */
1238
		protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
1239
			// Setup action links.
1240
			$action_links = array(
1241
				'install'  => '',
1242
				'update'   => '',
1243
				'activate' => '',
1244
				'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>' : '',
1245
			);
1246
1247
			$link_template = '<a href="%2$s">%1$s</a>';
1248
1249
			if ( current_user_can( 'install_plugins' ) ) {
1250 View Code Duplication
				if ( $install_count > 0 ) {
1251
					$action_links['install'] = sprintf(
1252
						$link_template,
1253
						translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),
1254
						esc_url( $this->get_tgmpa_status_url( 'install' ) )
1255
					);
1256
				}
1257 View Code Duplication
				if ( $update_count > 0 ) {
1258
					$action_links['update'] = sprintf(
1259
						$link_template,
1260
						translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),
1261
						esc_url( $this->get_tgmpa_status_url( 'update' ) )
1262
					);
1263
				}
1264
			}
1265
1266 View Code Duplication
			if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
1267
				$action_links['activate'] = sprintf(
1268
					$link_template,
1269
					translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),
1270
					esc_url( $this->get_tgmpa_status_url( 'activate' ) )
1271
				);
1272
			}
1273
1274
			$action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
1275
1276
			$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
1277
1278
			if ( ! empty( $action_links ) ) {
1279
				$action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
1280
				return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
1281
			} else {
1282
				return '';
1283
			}
1284
		}
1285
1286
		/**
1287
		 * Get admin notice class.
1288
		 *
1289
		 * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1290
		 * (lowest supported version by TGMPA).
1291
		 *
1292
		 * @since 2.x.x
1293
		 *
1294
		 * @return string
1295
		 */
1296
		protected function get_admin_notice_class() {
1297
			if ( ! empty( $this->strings['nag_type'] ) ) {
1298
				return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1299
			} else {
1300
				if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1301
					return 'notice-warning';
1302
				} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1303
					return 'notice';
1304
				} else {
1305
					return 'updated';
1306
				}
1307
			}
1308
		}
1309
1310
		/**
1311
		 * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
1312
		 *
1313
		 * @since 2.5.0
1314
		 */
1315
		protected function display_settings_errors() {
1316
			global $wp_settings_errors;
1317
1318
			settings_errors( 'tgmpa' );
1319
1320
			foreach ( (array) $wp_settings_errors as $key => $details ) {
1321
				if ( 'tgmpa' === $details['setting'] ) {
1322
					unset( $wp_settings_errors[ $key ] );
1323
					break;
1324
				}
1325
			}
1326
		}
1327
1328
		/**
1329
		 * Register dismissal of admin notices.
1330
		 *
1331
		 * Acts on the dismiss link in the admin nag messages.
1332
		 * If clicked, the admin notice disappears and will no longer be visible to this user.
1333
		 *
1334
		 * @since 2.1.0
1335
		 */
1336
		public function dismiss() {
1337
			if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) {
1338
				update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 );
1339
			}
1340
		}
1341
1342
		/**
1343
		 * Add individual plugin to our collection of plugins.
1344
		 *
1345
		 * If the required keys are not set or the plugin has already
1346
		 * been registered, the plugin is not added.
1347
		 *
1348
		 * @since 2.0.0
1349
		 *
1350
		 * @param array|null $plugin Array of plugin arguments or null if invalid argument.
1351
		 * @return null Return early if incorrect argument.
1352
		 */
1353
		public function register( $plugin ) {
1354
			if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) {
1355
				return;
1356
			}
1357
1358
			if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
1359
				return;
1360
			}
1361
1362
			$defaults = array(
1363
				'name'               => '',      // String
1364
				'slug'               => '',      // String
1365
				'source'             => 'repo',  // String
1366
				'required'           => false,   // Boolean
1367
				'version'            => '',      // String
1368
				'force_activation'   => false,   // Boolean
1369
				'force_deactivation' => false,   // Boolean
1370
				'external_url'       => '',      // String
1371
				'is_callable'        => '',      // String|Array.
1372
			);
1373
1374
			// Prepare the received data.
1375
			$plugin = wp_parse_args( $plugin, $defaults );
1376
1377
			// Standardize the received slug.
1378
			$plugin['slug'] = $this->sanitize_key( $plugin['slug'] );
1379
1380
			// Forgive users for using string versions of booleans or floats for version number.
1381
			$plugin['version']            = (string) $plugin['version'];
1382
			$plugin['source']             = empty( $plugin['source'] ) ? 'repo' : $plugin['source'];
1383
			$plugin['required']           = TGMPA_Utils::validate_bool( $plugin['required'] );
1384
			$plugin['force_activation']   = TGMPA_Utils::validate_bool( $plugin['force_activation'] );
1385
			$plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] );
1386
1387
			// Enrich the received data.
1388
			$plugin['file_path']   = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
1389
			$plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] );
1390
1391
			// Set the class properties.
1392
			$this->plugins[ $plugin['slug'] ]    = $plugin;
1393
			$this->sort_order[ $plugin['slug'] ] = $plugin['name'];
1394
1395
			// Should we add the force activation hook ?
1396
			if ( true === $plugin['force_activation'] ) {
1397
				$this->has_forced_activation = true;
1398
			}
1399
1400
			// Should we add the force deactivation hook ?
1401
			if ( true === $plugin['force_deactivation'] ) {
1402
				$this->has_forced_deactivation = true;
1403
			}
1404
		}
1405
1406
		/**
1407
		 * Determine what type of source the plugin comes from.
1408
		 *
1409
		 * @since 2.5.0
1410
		 *
1411
		 * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path
1412
		 *                       (= bundled) or an external URL.
1413
		 * @return string 'repo', 'external', or 'bundled'
1414
		 */
1415
		protected function get_plugin_source_type( $source ) {
1416
			if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) {
1417
				return 'repo';
1418
			} elseif ( preg_match( self::IS_URL_REGEX, $source ) ) {
1419
				return 'external';
1420
			} else {
1421
				return 'bundled';
1422
			}
1423
		}
1424
1425
		/**
1426
		 * Sanitizes a string key.
1427
		 *
1428
		 * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are*
1429
		 * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase
1430
		 * characters in the plugin directory path/slug. Silly them.
1431
		 *
1432
		 * @see https://developer.wordpress.org/reference/hooks/sanitize_key/
1433
		 *
1434
		 * @since 2.5.0
1435
		 *
1436
		 * @param string $key String key.
1437
		 * @return string Sanitized key
1438
		 */
1439
		public function sanitize_key( $key ) {
1440
			$raw_key = $key;
1441
			$key     = preg_replace( '`[^A-Za-z0-9_-]`', '', $key );
1442
1443
			/**
1444
			* Filter a sanitized key string.
1445
			*
1446
			* @since 2.5.0
1447
			*
1448
			* @param string $key     Sanitized key.
1449
			* @param string $raw_key The key prior to sanitization.
1450
			*/
1451
			return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key );
1452
		}
1453
1454
		/**
1455
		 * Amend default configuration settings.
1456
		 *
1457
		 * @since 2.0.0
1458
		 *
1459
		 * @param array $config Array of config options to pass as class properties.
1460
		 */
1461
		public function config( $config ) {
1462
			$keys = array(
1463
				'id',
1464
				'default_path',
1465
				'has_notices',
1466
				'dismissable',
1467
				'dismiss_msg',
1468
				'menu',
1469
				'parent_slug',
1470
				'capability',
1471
				'is_automatic',
1472
				'message',
1473
				'strings',
1474
			);
1475
1476
			foreach ( $keys as $key ) {
1477
				if ( isset( $config[ $key ] ) ) {
1478
					if ( is_array( $config[ $key ] ) ) {
1479
						$this->$key = array_merge( $this->$key, $config[ $key ] );
1480
					} else {
1481
						$this->$key = $config[ $key ];
1482
					}
1483
				}
1484
			}
1485
		}
1486
1487
		/**
1488
		 * Amend action link after plugin installation.
1489
		 *
1490
		 * @since 2.0.0
1491
		 *
1492
		 * @param array $install_actions Existing array of actions.
1493
		 * @return array Amended array of actions.
1494
		 */
1495
		public function actions( $install_actions ) {
1496
			// Remove action links on the TGMPA install page.
1497
			if ( $this->is_tgmpa_page() ) {
1498
				return false;
1499
			}
1500
1501
			return $install_actions;
1502
		}
1503
1504
		/**
1505
		 * Flushes the plugins cache on theme switch to prevent stale entries
1506
		 * from remaining in the plugin table.
1507
		 *
1508
		 * @since 2.4.0
1509
		 *
1510
		 * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache.
1511
		 *                                 Parameter added in v2.5.0.
1512
		 */
1513
		public function flush_plugins_cache( $clear_update_cache = true ) {
1514
			wp_clean_plugins_cache( $clear_update_cache );
1515
		}
1516
1517
		/**
1518
		 * Set file_path key for each installed plugin.
1519
		 *
1520
		 * @since 2.1.0
1521
		 *
1522
		 * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
1523
		 *                            Parameter added in v2.5.0.
1524
		 */
1525
		public function populate_file_path( $plugin_slug = '' ) {
1526
			if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) {
1527
				$this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug );
1528
			} else {
1529
				// Add file_path key for all plugins.
1530
				foreach ( $this->plugins as $slug => $values ) {
1531
					$this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug );
1532
				}
1533
			}
1534
		}
1535
1536
		/**
1537
		 * Helper function to extract the file path of the plugin file from the
1538
		 * plugin slug, if the plugin is installed.
1539
		 *
1540
		 * @since 2.0.0
1541
		 *
1542
		 * @param string $slug Plugin slug (typically folder name) as provided by the developer.
1543
		 * @return string Either file path for plugin if installed, or just the plugin slug.
1544
		 */
1545
		protected function _get_plugin_basename_from_slug( $slug ) {
1546
			$keys = array_keys( $this->get_plugins() );
1547
1548
			foreach ( $keys as $key ) {
1549
				if ( preg_match( '|^' . $slug . '/|', $key ) ) {
1550
					return $key;
1551
				}
1552
			}
1553
1554
			return $slug;
1555
		}
1556
1557
		/**
1558
		 * Retrieve plugin data, given the plugin name.
1559
		 *
1560
		 * Loops through the registered plugins looking for $name. If it finds it,
1561
		 * it returns the $data from that plugin. Otherwise, returns false.
1562
		 *
1563
		 * @since 2.1.0
1564
		 *
1565
		 * @param string $name Name of the plugin, as it was registered.
1566
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
1567
		 * @return string|boolean Plugin slug if found, false otherwise.
1568
		 */
1569
		public function _get_plugin_data_from_name( $name, $data = 'slug' ) {
1570
			foreach ( $this->plugins as $values ) {
1571
				if ( $name === $values['name'] && isset( $values[ $data ] ) ) {
1572
					return $values[ $data ];
1573
				}
1574
			}
1575
1576
			return false;
1577
		}
1578
1579
		/**
1580
		 * Retrieve the download URL for a package.
1581
		 *
1582
		 * @since 2.5.0
1583
		 *
1584
		 * @param string $slug Plugin slug.
1585
		 * @return string Plugin download URL or path to local file or empty string if undetermined.
1586
		 */
1587
		public function get_download_url( $slug ) {
1588
			$dl_source = '';
1589
1590
			switch ( $this->plugins[ $slug ]['source_type'] ) {
1591
				case 'repo':
1592
					return $this->get_wp_repo_download_url( $slug );
1593
				case 'external':
1594
					return $this->plugins[ $slug ]['source'];
1595
				case 'bundled':
1596
					return $this->default_path . $this->plugins[ $slug ]['source'];
1597
			}
1598
1599
			return $dl_source; // Should never happen.
1600
		}
1601
1602
		/**
1603
		 * Retrieve the download URL for a WP repo package.
1604
		 *
1605
		 * @since 2.5.0
1606
		 *
1607
		 * @param string $slug Plugin slug.
1608
		 * @return string Plugin download URL.
1609
		 */
1610
		protected function get_wp_repo_download_url( $slug ) {
1611
			$source = '';
1612
			$api    = $this->get_plugins_api( $slug );
1613
1614
			if ( false !== $api && isset( $api->download_link ) ) {
1615
				$source = $api->download_link;
1616
			}
1617
1618
			return $source;
1619
		}
1620
1621
		/**
1622
		 * Try to grab information from WordPress API.
1623
		 *
1624
		 * @since 2.5.0
1625
		 *
1626
		 * @param string $slug Plugin slug.
1627
		 * @return object Plugins_api response object on success, WP_Error on failure.
1628
		 */
1629
		protected function get_plugins_api( $slug ) {
1630
			static $api = array(); // Cache received responses.
1631
1632
			if ( ! isset( $api[ $slug ] ) ) {
1633
				if ( ! function_exists( 'plugins_api' ) ) {
1634
					require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
1635
				}
1636
1637
				$response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
1638
1639
				$api[ $slug ] = false;
1640
1641
				if ( is_wp_error( $response ) ) {
1642
					wp_die( esc_html( $this->strings['oops'] ) );
1643
				} else {
1644
					$api[ $slug ] = $response;
1645
				}
1646
			}
1647
1648
			return $api[ $slug ];
1649
		}
1650
1651
		/**
1652
		 * Retrieve a link to a plugin information page.
1653
		 *
1654
		 * @since 2.5.0
1655
		 *
1656
		 * @param string $slug Plugin slug.
1657
		 * @return string Fully formed html link to a plugin information page if available
1658
		 *                or the plugin name if not.
1659
		 */
1660
		public function get_info_link( $slug ) {
1661
			if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) {
1662
				$link = sprintf(
1663
					'<a href="%1$s" target="_blank">%2$s</a>',
1664
					esc_url( $this->plugins[ $slug ]['external_url'] ),
1665
					esc_html( $this->plugins[ $slug ]['name'] )
1666
				);
1667
			} elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) {
1668
				$url = add_query_arg(
1669
					array(
1670
						'tab'       => 'plugin-information',
1671
						'plugin'    => urlencode( $slug ),
1672
						'TB_iframe' => 'true',
1673
						'width'     => '640',
1674
						'height'    => '500',
1675
					),
1676
					self_admin_url( 'plugin-install.php' )
1677
				);
1678
1679
				$link = sprintf(
1680
					'<a href="%1$s" class="thickbox">%2$s</a>',
1681
					esc_url( $url ),
1682
					esc_html( $this->plugins[ $slug ]['name'] )
1683
				);
1684
			} else {
1685
				$link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink.
1686
			}
1687
1688
			return $link;
1689
		}
1690
1691
		/**
1692
		 * Determine if we're on the TGMPA Install page.
1693
		 *
1694
		 * @since 2.1.0
1695
		 *
1696
		 * @return boolean True when on the TGMPA page, false otherwise.
1697
		 */
1698
		protected function is_tgmpa_page() {
1699
			return isset( $_GET['page'] ) && $this->menu === $_GET['page'];
1700
		}
1701
1702
		/**
1703
		 * Determine if we're on a WP Core installation/upgrade page.
1704
		 *
1705
		 * @since 2.x.x
1706
		 *
1707
		 * @return boolean True when on a WP Core installation/upgrade page, false otherwise.
1708
		 */
1709
		protected function is_core_update_page() {
1710
			// Current screen is not always available, most notably on the customizer screen.
1711
			if ( ! function_exists( 'get_current_screen' ) ) {
1712
				return false;
1713
			}
1714
1715
			$screen = get_current_screen();
1716
1717
			if ( 'update-core' === $screen->base ) {
1718
				// Core update screen.
1719
				return true;
1720
			} elseif ( 'plugins' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
1721
				// Plugins bulk update screen.
1722
				return true;
1723
			} elseif ( 'update' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
1724
				// Individual updates (ajax call).
1725
				return true;
1726
			}
1727
1728
			return false;
1729
		}
1730
1731
		/**
1732
		 * Retrieve the URL to the TGMPA Install page.
1733
		 *
1734
		 * I.e. depending on the config settings passed something along the lines of:
1735
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins
1736
		 *
1737
		 * @since 2.5.0
1738
		 *
1739
		 * @return string Properly encoded URL (not escaped).
1740
		 */
1741
		public function get_tgmpa_url() {
1742
			static $url;
1743
1744
			if ( ! isset( $url ) ) {
1745
				$parent = $this->parent_slug;
1746
				if ( false === strpos( $parent, '.php' ) ) {
1747
					$parent = 'admin.php';
1748
				}
1749
				$url = add_query_arg(
1750
					array(
1751
						'page' => urlencode( $this->menu ),
1752
					),
1753
					self_admin_url( $parent )
1754
				);
1755
			}
1756
1757
			return $url;
1758
		}
1759
1760
		/**
1761
		 * Retrieve the URL to the TGMPA Install page for a specific plugin status (view).
1762
		 *
1763
		 * I.e. depending on the config settings passed something along the lines of:
1764
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install
1765
		 *
1766
		 * @since 2.5.0
1767
		 *
1768
		 * @param string $status Plugin status - either 'install', 'update' or 'activate'.
1769
		 * @return string Properly encoded URL (not escaped).
1770
		 */
1771
		public function get_tgmpa_status_url( $status ) {
1772
			return add_query_arg(
1773
				array(
1774
					'plugin_status' => urlencode( $status ),
1775
				),
1776
				$this->get_tgmpa_url()
1777
			);
1778
		}
1779
1780
		/**
1781
		 * Determine whether there are open actions for plugins registered with TGMPA.
1782
		 *
1783
		 * @since 2.5.0
1784
		 *
1785
		 * @return bool True if complete, i.e. no outstanding actions. False otherwise.
1786
		 */
1787
		public function is_tgmpa_complete() {
1788
			$complete = true;
1789
			foreach ( $this->plugins as $slug => $plugin ) {
1790
				if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1791
					$complete = false;
1792
					break;
1793
				}
1794
			}
1795
1796
			return $complete;
1797
		}
1798
1799
		/**
1800
		 * Check if a plugin is installed. Does not take must-use plugins into account.
1801
		 *
1802
		 * @since 2.5.0
1803
		 *
1804
		 * @param string $slug Plugin slug.
1805
		 * @return bool True if installed, false otherwise.
1806
		 */
1807
		public function is_plugin_installed( $slug ) {
1808
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1809
1810
			return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) );
1811
		}
1812
1813
		/**
1814
		 * Check if a plugin is active.
1815
		 *
1816
		 * @since 2.5.0
1817
		 *
1818
		 * @param string $slug Plugin slug.
1819
		 * @return bool True if active, false otherwise.
1820
		 */
1821
		public function is_plugin_active( $slug ) {
1822
			return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) );
1823
		}
1824
1825
		/**
1826
		 * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required
1827
		 * available, check whether the current install meets them.
1828
		 *
1829
		 * @since 2.5.0
1830
		 *
1831
		 * @param string $slug Plugin slug.
1832
		 * @return bool True if OK to update, false otherwise.
1833
		 */
1834
		public function can_plugin_update( $slug ) {
1835
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1836
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1837
				return true;
1838
			}
1839
1840
			$api = $this->get_plugins_api( $slug );
1841
1842
			if ( false !== $api && isset( $api->requires ) ) {
1843
				return version_compare( $this->wp_version, $api->requires, '>=' );
1844
			}
1845
1846
			// No usable info received from the plugins API, presume we can update.
1847
			return true;
1848
		}
1849
1850
		/**
1851
		 * Check to see if the plugin is 'updatetable', i.e. installed, with an update available
1852
		 * and no WP version requirements blocking it.
1853
		 *
1854
		 * @since 2.x.x
1855
		 *
1856
		 * @param string $slug Plugin slug.
1857
		 * @return bool True if OK to proceed with update, false otherwise.
1858
		 */
1859
		public function is_plugin_updatetable( $slug ) {
1860
			if ( ! $this->is_plugin_installed( $slug ) ) {
1861
				return false;
1862
			} else {
1863
				return ( $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) );
1864
			}
1865
		}
1866
1867
		/**
1868
		 * Check if a plugin can be activated, i.e. is not currently active and meets the minimum
1869
		 * plugin version requirements set in TGMPA (if any).
1870
		 *
1871
		 * @since 2.5.0
1872
		 *
1873
		 * @param string $slug Plugin slug.
1874
		 * @return bool True if OK to activate, false otherwise.
1875
		 */
1876
		public function can_plugin_activate( $slug ) {
1877
			return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) );
1878
		}
1879
1880
		/**
1881
		 * Retrieve the version number of an installed plugin.
1882
		 *
1883
		 * @since 2.5.0
1884
		 *
1885
		 * @param string $slug Plugin slug.
1886
		 * @return string Version number as string or an empty string if the plugin is not installed
1887
		 *                or version unknown (plugins which don't comply with the plugin header standard).
1888
		 */
1889
		public function get_installed_version( $slug ) {
1890
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1891
1892
			if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) {
1893
				return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'];
1894
			}
1895
1896
			return '';
1897
		}
1898
1899
		/**
1900
		 * Check whether a plugin complies with the minimum version requirements.
1901
		 *
1902
		 * @since 2.5.0
1903
		 *
1904
		 * @param string $slug Plugin slug.
1905
		 * @return bool True when a plugin needs to be updated, otherwise false.
1906
		 */
1907
		public function does_plugin_require_update( $slug ) {
1908
			$installed_version = $this->get_installed_version( $slug );
1909
			$minimum_version   = $this->plugins[ $slug ]['version'];
1910
1911
			return version_compare( $minimum_version, $installed_version, '>' );
1912
		}
1913
1914
		/**
1915
		 * Check whether there is an update available for a plugin.
1916
		 *
1917
		 * @since 2.5.0
1918
		 *
1919
		 * @param string $slug Plugin slug.
1920
		 * @return false|string Version number string of the available update or false if no update available.
1921
		 */
1922
		public function does_plugin_have_update( $slug ) {
1923
			// Presume bundled and external plugins will point to a package which meets the minimum required version.
1924
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1925
				if ( $this->does_plugin_require_update( $slug ) ) {
1926
					return $this->plugins[ $slug ]['version'];
1927
				}
1928
1929
				return false;
1930
			}
1931
1932
			$repo_updates = get_site_transient( 'update_plugins' );
1933
1934 View Code Duplication
			if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) {
1935
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version;
1936
			}
1937
1938
			return false;
1939
		}
1940
1941
		/**
1942
		 * Retrieve potential upgrade notice for a plugin.
1943
		 *
1944
		 * @since 2.5.0
1945
		 *
1946
		 * @param string $slug Plugin slug.
1947
		 * @return string The upgrade notice or an empty string if no message was available or provided.
1948
		 */
1949
		public function get_upgrade_notice( $slug ) {
1950
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1951
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1952
				return '';
1953
			}
1954
1955
			$repo_updates = get_site_transient( 'update_plugins' );
1956
1957 View Code Duplication
			if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) {
1958
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice;
1959
			}
1960
1961
			return '';
1962
		}
1963
1964
		/**
1965
		 * Wrapper around the core WP get_plugins function, making sure it's actually available.
1966
		 *
1967
		 * @since 2.5.0
1968
		 *
1969
		 * @param string $plugin_folder Optional. Relative path to single plugin folder.
1970
		 * @return array Array of installed plugins with plugin information.
1971
		 */
1972
		public function get_plugins( $plugin_folder = '' ) {
1973
			if ( ! function_exists( 'get_plugins' ) ) {
1974
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
1975
			}
1976
1977
			return get_plugins( $plugin_folder );
1978
		}
1979
1980
		/**
1981
		 * Delete dismissable nag option when theme is switched.
1982
		 *
1983
		 * This ensures that the user(s) is/are again reminded via nag of required
1984
		 * and/or recommended plugins if they re-activate the theme.
1985
		 *
1986
		 * @since 2.1.1
1987
		 */
1988
		public function update_dismiss() {
1989
			delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true );
1990
		}
1991
1992
		/**
1993
		 * Forces plugin activation if the parameter 'force_activation' is
1994
		 * set to true.
1995
		 *
1996
		 * This allows theme authors to specify certain plugins that must be
1997
		 * active at all times while using the current theme.
1998
		 *
1999
		 * Please take special care when using this parameter as it has the
2000
		 * potential to be harmful if not used correctly. Setting this parameter
2001
		 * to true will not allow the specified plugin to be deactivated unless
2002
		 * the user switches themes.
2003
		 *
2004
		 * @since 2.2.0
2005
		 */
2006
		public function force_activation() {
2007
			foreach ( $this->plugins as $slug => $plugin ) {
2008
				if ( true === $plugin['force_activation'] ) {
2009
					if ( ! $this->is_plugin_installed( $slug ) ) {
2010
						// Oops, plugin isn't there so iterate to next condition.
2011
						continue;
2012
					} elseif ( $this->can_plugin_activate( $slug ) ) {
2013
						// There we go, activate the plugin.
2014
						activate_plugin( $plugin['file_path'] );
2015
					}
2016
				}
2017
			}
2018
		}
2019
2020
		/**
2021
		 * Forces plugin deactivation if the parameter 'force_deactivation'
2022
		 * is set to true.
2023
		 *
2024
		 * This allows theme authors to specify certain plugins that must be
2025
		 * deactivated upon switching from the current theme to another.
2026
		 *
2027
		 * Please take special care when using this parameter as it has the
2028
		 * potential to be harmful if not used correctly.
2029
		 *
2030
		 * @since 2.2.0
2031
		 */
2032
		public function force_deactivation() {
2033
			foreach ( $this->plugins as $slug => $plugin ) {
2034
				// Only proceed forward if the parameter is set to true and plugin is active.
2035
				if ( true === $plugin['force_deactivation'] && $this->is_plugin_active( $slug ) ) {
2036
					deactivate_plugins( $plugin['file_path'] );
2037
				}
2038
			}
2039
		}
2040
2041
		/**
2042
		 * Echo the current TGMPA version number to the page.
2043
		 */
2044
		public function show_tgmpa_version() {
2045
			echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',
2046
				esc_html(
2047
					sprintf(
2048
						/* translators: %s: version number */
2049
						__( 'TGMPA v%s', 'tgmpa' ),
2050
						self::TGMPA_VERSION
2051
					)
2052
				),
2053
				'</small></strong></p>';
2054
		}
2055
2056
		/**
2057
		 * Returns the singleton instance of the class.
2058
		 *
2059
		 * @since 2.4.0
2060
		 *
2061
		 * @return object The TGM_Plugin_Activation object.
2062
		 */
2063
		public static function get_instance() {
2064
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
2065
				self::$instance = new self();
2066
			}
2067
2068
			return self::$instance;
2069
		}
2070
	}
2071
2072
	if ( ! function_exists( 'load_tgm_plugin_activation' ) ) {
2073
		/**
2074
		 * Ensure only one instance of the class is ever invoked.
2075
		 */
2076
		function load_tgm_plugin_activation() {
2077
			$GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance();
2078
		}
2079
	}
2080
2081
	if ( did_action( 'plugins_loaded' ) ) {
2082
		load_tgm_plugin_activation();
2083
	} else {
2084
		add_action( 'plugins_loaded', 'load_tgm_plugin_activation' );
2085
	}
2086
}
2087
2088
if ( ! function_exists( 'tgmpa' ) ) {
2089
	/**
2090
	 * Helper function to register a collection of required plugins.
2091
	 *
2092
	 * @since 2.0.0
2093
	 * @api
2094
	 *
2095
	 * @param array $plugins An array of plugin arrays.
2096
	 * @param array $config  Optional. An array of configuration values.
2097
	 */
2098
	function tgmpa( $plugins, $config = array() ) {
2099
		$instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2100
2101
		foreach ( $plugins as $plugin ) {
2102
			call_user_func( array( $instance, 'register' ), $plugin );
2103
		}
2104
2105
		if ( ! empty( $config ) && is_array( $config ) ) {
2106
			// Send out notices for deprecated arguments passed.
2107
			if ( isset( $config['notices'] ) ) {
2108
				_deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' );
2109
				if ( ! isset( $config['has_notices'] ) ) {
2110
					$config['has_notices'] = $config['notices'];
2111
				}
2112
			}
2113
2114
			if ( isset( $config['parent_menu_slug'] ) ) {
2115
				_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.' );
2116
			}
2117
			if ( isset( $config['parent_url_slug'] ) ) {
2118
				_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.' );
2119
			}
2120
2121
			call_user_func( array( $instance, 'config' ), $config );
2122
		}
2123
	}
2124
}
2125
2126
/**
2127
 * WP_List_Table isn't always available. If it isn't available,
2128
 * we load it here.
2129
 *
2130
 * @since 2.2.0
2131
 */
2132
if ( ! class_exists( 'WP_List_Table' ) ) {
2133
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
2134
}
2135
2136
if ( ! class_exists( 'TGMPA_List_Table' ) ) {
2137
2138
	/**
2139
	 * List table class for handling plugins.
2140
	 *
2141
	 * Extends the WP_List_Table class to provide a future-compatible
2142
	 * way of listing out all required/recommended plugins.
2143
	 *
2144
	 * Gives users an interface similar to the Plugin Administration
2145
	 * area with similar (albeit stripped down) capabilities.
2146
	 *
2147
	 * This class also allows for the bulk install of plugins.
2148
	 *
2149
	 * @since 2.2.0
2150
	 *
2151
	 * @package TGM-Plugin-Activation
2152
	 * @author  Thomas Griffin
2153
	 * @author  Gary Jones
2154
	 */
2155
	class TGMPA_List_Table extends WP_List_Table {
2156
		/**
2157
		 * TGMPA instance.
2158
		 *
2159
		 * @since 2.5.0
2160
		 *
2161
		 * @var object
2162
		 */
2163
		protected $tgmpa;
2164
2165
		/**
2166
		 * The currently chosen view.
2167
		 *
2168
		 * @since 2.5.0
2169
		 *
2170
		 * @var string One of: 'all', 'install', 'update', 'activate'
2171
		 */
2172
		public $view_context = 'all';
2173
2174
		/**
2175
		 * The plugin counts for the various views.
2176
		 *
2177
		 * @since 2.5.0
2178
		 *
2179
		 * @var array
2180
		 */
2181
		protected $view_totals = array(
2182
			'all'      => 0,
2183
			'install'  => 0,
2184
			'update'   => 0,
2185
			'activate' => 0,
2186
		);
2187
2188
		/**
2189
		 * References parent constructor and sets defaults for class.
2190
		 *
2191
		 * @since 2.2.0
2192
		 */
2193
		public function __construct() {
2194
			$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2195
2196
			parent::__construct(
2197
				array(
2198
					'singular' => 'plugin',
2199
					'plural'   => 'plugins',
2200
					'ajax'     => false,
2201
				)
2202
			);
2203
2204
			if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) {
2205
				$this->view_context = sanitize_key( $_REQUEST['plugin_status'] );
2206
			}
2207
2208
			add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) );
2209
		}
2210
2211
		/**
2212
		 * Get a list of CSS classes for the <table> tag.
2213
		 *
2214
		 * Overruled to prevent the 'plural' argument from being added.
2215
		 *
2216
		 * @since 2.5.0
2217
		 *
2218
		 * @return array CSS classnames.
2219
		 */
2220
		public function get_table_classes() {
2221
			return array( 'widefat', 'fixed' );
2222
		}
2223
2224
		/**
2225
		 * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table.
2226
		 *
2227
		 * @since 2.2.0
2228
		 *
2229
		 * @return array $table_data Information for use in table.
2230
		 */
2231
		protected function _gather_plugin_data() {
2232
			// Load thickbox for plugin links.
2233
			$this->tgmpa->admin_init();
2234
			$this->tgmpa->thickbox();
2235
2236
			// Categorize the plugins which have open actions.
2237
			$plugins = $this->categorize_plugins_to_views();
2238
2239
			// Set the counts for the view links.
2240
			$this->set_view_totals( $plugins );
2241
2242
			// Prep variables for use and grab list of all installed plugins.
2243
			$table_data = array();
2244
			$i          = 0;
2245
2246
			// Redirect to the 'all' view if no plugins were found for the selected view context.
2247
			if ( empty( $plugins[ $this->view_context ] ) ) {
2248
				$this->view_context = 'all';
2249
			}
2250
2251
			foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) {
2252
				$table_data[ $i ]['sanitized_plugin']  = $plugin['name'];
2253
				$table_data[ $i ]['slug']              = $slug;
2254
				$table_data[ $i ]['plugin']            = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>';
2255
				$table_data[ $i ]['source']            = $this->get_plugin_source_type_text( $plugin['source_type'] );
2256
				$table_data[ $i ]['type']              = $this->get_plugin_advise_type_text( $plugin['required'] );
2257
				$table_data[ $i ]['status']            = $this->get_plugin_status_text( $slug );
2258
				$table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug );
2259
				$table_data[ $i ]['minimum_version']   = $plugin['version'];
2260
				$table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug );
2261
2262
				// Prep the upgrade notice info.
2263
				$upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug );
2264
				if ( ! empty( $upgrade_notice ) ) {
2265
					$table_data[ $i ]['upgrade_notice'] = $upgrade_notice;
2266
2267
					add_action( "tgmpa_after_plugin_row_$slug", array( $this, 'wp_plugin_update_row' ), 10, 2 );
2268
				}
2269
2270
				$table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin );
2271
2272
				$i++;
2273
			}
2274
2275
			return $table_data;
2276
		}
2277
2278
		/**
2279
		 * Categorize the plugins which have open actions into views for the TGMPA page.
2280
		 *
2281
		 * @since 2.5.0
2282
		 */
2283
		protected function categorize_plugins_to_views() {
2284
			$plugins = array(
2285
				'all'      => array(), // Meaning: all plugins which still have open actions.
2286
				'install'  => array(),
2287
				'update'   => array(),
2288
				'activate' => array(),
2289
			);
2290
2291
			foreach ( $this->tgmpa->plugins as $slug => $plugin ) {
2292
				if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2293
					// No need to display plugins if they are installed, up-to-date and active.
2294
					continue;
2295
				} else {
2296
					$plugins['all'][ $slug ] = $plugin;
2297
2298
					if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2299
						$plugins['install'][ $slug ] = $plugin;
2300
					} else {
2301
						if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2302
							$plugins['update'][ $slug ] = $plugin;
2303
						}
2304
2305
						if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2306
							$plugins['activate'][ $slug ] = $plugin;
2307
						}
2308
					}
2309
				}
2310
			}
2311
2312
			return $plugins;
2313
		}
2314
2315
		/**
2316
		 * Set the counts for the view links.
2317
		 *
2318
		 * @since 2.5.0
2319
		 *
2320
		 * @param array $plugins Plugins order by view.
2321
		 */
2322
		protected function set_view_totals( $plugins ) {
2323
			foreach ( $plugins as $type => $list ) {
2324
				$this->view_totals[ $type ] = count( $list );
2325
			}
2326
		}
2327
2328
		/**
2329
		 * Get the plugin required/recommended text string.
2330
		 *
2331
		 * @since 2.5.0
2332
		 *
2333
		 * @param string $required Plugin required setting.
2334
		 * @return string
2335
		 */
2336
		protected function get_plugin_advise_type_text( $required ) {
2337
			if ( true === $required ) {
2338
				return __( 'Required', 'tgmpa' );
2339
			}
2340
2341
			return __( 'Recommended', 'tgmpa' );
2342
		}
2343
2344
		/**
2345
		 * Get the plugin source type text string.
2346
		 *
2347
		 * @since 2.5.0
2348
		 *
2349
		 * @param string $type Plugin type.
2350
		 * @return string
2351
		 */
2352
		protected function get_plugin_source_type_text( $type ) {
2353
			$string = '';
2354
2355
			switch ( $type ) {
2356
				case 'repo':
2357
					$string = __( 'WordPress Repository', 'tgmpa' );
2358
					break;
2359
				case 'external':
2360
					$string = __( 'External Source', 'tgmpa' );
2361
					break;
2362
				case 'bundled':
2363
					$string = __( 'Pre-Packaged', 'tgmpa' );
2364
					break;
2365
			}
2366
2367
			return $string;
2368
		}
2369
2370
		/**
2371
		 * Determine the plugin status message.
2372
		 *
2373
		 * @since 2.5.0
2374
		 *
2375
		 * @param string $slug Plugin slug.
2376
		 * @return string
2377
		 */
2378
		protected function get_plugin_status_text( $slug ) {
2379
			if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2380
				return __( 'Not Installed', 'tgmpa' );
2381
			}
2382
2383
			if ( ! $this->tgmpa->is_plugin_active( $slug ) ) {
2384
				$install_status = __( 'Installed But Not Activated', 'tgmpa' );
2385
			} else {
2386
				$install_status = __( 'Active', 'tgmpa' );
2387
			}
2388
2389
			$update_status = '';
2390
2391
			if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2392
				$update_status = __( 'Required Update not Available', 'tgmpa' );
2393
2394
			} elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) {
2395
				$update_status = __( 'Requires Update', 'tgmpa' );
2396
2397
			} elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2398
				$update_status = __( 'Update recommended', 'tgmpa' );
2399
			}
2400
2401
			if ( '' === $update_status ) {
2402
				return $install_status;
2403
			}
2404
2405
			return sprintf(
2406
				/* translators: %1$s: install status, %2$s: update status */
2407
				_x( '%1$s, %2$s', 'Install/Update Status', 'tgmpa' ),
2408
				$install_status,
2409
				$update_status
2410
			);
2411
		}
2412
2413
		/**
2414
		 * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type.
2415
		 *
2416
		 * @since 2.5.0
2417
		 *
2418
		 * @param array $items Prepared table items.
2419
		 * @return array Sorted table items.
2420
		 */
2421
		public function sort_table_items( $items ) {
2422
			$type = array();
2423
			$name = array();
2424
2425
			foreach ( $items as $i => $plugin ) {
2426
				$type[ $i ] = $plugin['type']; // Required / recommended.
2427
				$name[ $i ] = $plugin['sanitized_plugin'];
2428
			}
2429
2430
			array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items );
2431
2432
			return $items;
2433
		}
2434
2435
		/**
2436
		 * Get an associative array ( id => link ) of the views available on this table.
2437
		 *
2438
		 * @since 2.5.0
2439
		 *
2440
		 * @return array
2441
		 */
2442
		public function get_views() {
2443
			$status_links = array();
2444
2445
			foreach ( $this->view_totals as $type => $count ) {
2446
				if ( $count < 1 ) {
2447
					continue;
2448
				}
2449
2450
				switch ( $type ) {
2451
					case 'all':
2452
						$text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' );
2453
						break;
2454
					case 'install':
2455
						$text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' );
2456
						break;
2457
					case 'update':
2458
						$text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' );
2459
						break;
2460
					case 'activate':
2461
						$text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' );
2462
						break;
2463
					default:
2464
						$text = '';
2465
						break;
2466
				}
2467
2468
				if ( ! empty( $text ) ) {
2469
2470
					$status_links[ $type ] = sprintf(
2471
						'<a href="%s"%s>%s</a>',
2472
						esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
2473
						( $type === $this->view_context ) ? ' class="current"' : '',
2474
						sprintf( $text, number_format_i18n( $count ) )
2475
					);
2476
				}
2477
			}
2478
2479
			return $status_links;
2480
		}
2481
2482
		/**
2483
		 * Create default columns to display important plugin information
2484
		 * like type, action and status.
2485
		 *
2486
		 * @since 2.2.0
2487
		 *
2488
		 * @param array  $item        Array of item data.
2489
		 * @param string $column_name The name of the column.
2490
		 * @return string
2491
		 */
2492
		public function column_default( $item, $column_name ) {
2493
			return $item[ $column_name ];
2494
		}
2495
2496
		/**
2497
		 * Required for bulk installing.
2498
		 *
2499
		 * Adds a checkbox for each plugin.
2500
		 *
2501
		 * @since 2.2.0
2502
		 *
2503
		 * @param array $item Array of item data.
2504
		 * @return string The input checkbox with all necessary info.
2505
		 */
2506
		public function column_cb( $item ) {
2507
			return sprintf(
2508
				'<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />',
2509
				esc_attr( $this->_args['singular'] ),
2510
				esc_attr( $item['slug'] ),
2511
				esc_attr( $item['sanitized_plugin'] )
2512
			);
2513
		}
2514
2515
		/**
2516
		 * Create default title column along with the action links.
2517
		 *
2518
		 * @since 2.2.0
2519
		 *
2520
		 * @param array $item Array of item data.
2521
		 * @return string The plugin name and action links.
2522
		 */
2523
		public function column_plugin( $item ) {
2524
			return sprintf(
2525
				'%1$s %2$s',
2526
				$item['plugin'],
2527
				$this->row_actions( $this->get_row_actions( $item ), true )
2528
			);
2529
		}
2530
2531
		/**
2532
		 * Create version information column.
2533
		 *
2534
		 * @since 2.5.0
2535
		 *
2536
		 * @param array $item Array of item data.
2537
		 * @return string HTML-formatted version information.
2538
		 */
2539
		public function column_version( $item ) {
2540
			$output = array();
2541
2542
			if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2543
				$installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'tgmpa' );
2544
2545
				$color = '';
2546
				if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) {
2547
					$color = ' color: #ff0000; font-weight: bold;';
2548
				}
2549
2550
				$output[] = sprintf(
2551
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'tgmpa' ) . '</p>',
2552
					$color,
2553
					$installed
2554
				);
2555
			}
2556
2557
			if ( ! empty( $item['minimum_version'] ) ) {
2558
				$output[] = sprintf(
2559
					'<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'tgmpa' ) . '</p>',
2560
					$item['minimum_version']
2561
				);
2562
			}
2563
2564
			if ( ! empty( $item['available_version'] ) ) {
2565
				$color = '';
2566
				if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) {
2567
					$color = ' color: #71C671; font-weight: bold;';
2568
				}
2569
2570
				$output[] = sprintf(
2571
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'tgmpa' ) . '</p>',
2572
					$color,
2573
					$item['available_version']
2574
				);
2575
			}
2576
2577
			if ( empty( $output ) ) {
2578
				return '&nbsp;'; // Let's not break the table layout.
2579
			} else {
2580
				return implode( "\n", $output );
2581
			}
2582
		}
2583
2584
		/**
2585
		 * Sets default message within the plugins table if no plugins
2586
		 * are left for interaction.
2587
		 *
2588
		 * Hides the menu item to prevent the user from clicking and
2589
		 * getting a permissions error.
2590
		 *
2591
		 * @since 2.2.0
2592
		 */
2593
		public function no_items() {
2594
			echo esc_html__( 'No plugins to install, update or activate.', 'tgmpa' ) . ' <a href="' . esc_url( self_admin_url() ) . '"> ' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>';
2595
			echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
2596
		}
2597
2598
		/**
2599
		 * Output all the column information within the table.
2600
		 *
2601
		 * @since 2.2.0
2602
		 *
2603
		 * @return array $columns The column names.
2604
		 */
2605
		public function get_columns() {
2606
			$columns = array(
2607
				'cb'     => '<input type="checkbox" />',
2608
				'plugin' => __( 'Plugin', 'tgmpa' ),
2609
				'source' => __( 'Source', 'tgmpa' ),
2610
				'type'   => __( 'Type', 'tgmpa' ),
2611
			);
2612
2613
			if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
2614
				$columns['version'] = __( 'Version', 'tgmpa' );
2615
				$columns['status']  = __( 'Status', 'tgmpa' );
2616
			}
2617
2618
			return apply_filters( 'tgmpa_table_columns', $columns );
2619
		}
2620
2621
		/**
2622
		 * Get name of default primary column
2623
		 *
2624
		 * @since 2.5.0 / WP 4.3+ compatibility
2625
		 * @access protected
2626
		 *
2627
		 * @return string
2628
		 */
2629
		protected function get_default_primary_column_name() {
2630
			return 'plugin';
2631
		}
2632
2633
		/**
2634
		 * Get the name of the primary column.
2635
		 *
2636
		 * @since 2.5.0 / WP 4.3+ compatibility
2637
		 * @access protected
2638
		 *
2639
		 * @return string The name of the primary column.
2640
		 */
2641
		protected function get_primary_column_name() {
2642
			if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) {
2643
				return parent::get_primary_column_name();
2644
			} else {
2645
				return $this->get_default_primary_column_name();
2646
			}
2647
		}
2648
2649
		/**
2650
		 * Get the actions which are relevant for a specific plugin row.
2651
		 *
2652
		 * @since 2.5.0
2653
		 *
2654
		 * @param array $item Array of item data.
2655
		 * @return array Array with relevant action links.
2656
		 */
2657
		protected function get_row_actions( $item ) {
2658
			$actions      = array();
2659
			$action_links = array();
2660
2661
			// Display the 'Install' action link if the plugin is not yet available.
2662
			if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2663
				/* translators: %s: plugin name in screen reader markup */
2664
				$actions['install'] = __( 'Install %s', 'tgmpa' );
2665
			} else {
2666
				// Display the 'Update' action link if an update is available and WP complies with plugin minimum.
2667
				if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) {
2668
					/* translators: %s: plugin name in screen reader markup */
2669
					$actions['update'] = __( 'Update %s', 'tgmpa' );
2670
				}
2671
2672
				// Display the 'Activate' action link, but only if the plugin meets the minimum version.
2673
				if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) {
2674
					/* translators: %s: plugin name in screen reader markup */
2675
					$actions['activate'] = __( 'Activate %s', 'tgmpa' );
2676
				}
2677
			}
2678
2679
			// Create the actual links.
2680
			foreach ( $actions as $action => $text ) {
2681
				$nonce_url = wp_nonce_url(
2682
					add_query_arg(
2683
						array(
2684
							'plugin'           => urlencode( $item['slug'] ),
2685
							'tgmpa-' . $action => $action . '-plugin',
2686
						),
2687
						$this->tgmpa->get_tgmpa_url()
2688
					),
2689
					'tgmpa-' . $action,
2690
					'tgmpa-nonce'
2691
				);
2692
2693
				$action_links[ $action ] = sprintf(
2694
					'<a href="%1$s">' . esc_html( $text ) . '</a>',
2695
					esc_url( $nonce_url ),
2696
					'<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>'
2697
				);
2698
			}
2699
2700
			$prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : '';
2701
			return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context );
2702
		}
2703
2704
		/**
2705
		 * Generates content for a single row of the table.
2706
		 *
2707
		 * @since 2.5.0
2708
		 *
2709
		 * @param object $item The current item.
2710
		 */
2711
		public function single_row( $item ) {
2712
			parent::single_row( $item );
2713
2714
			/**
2715
			 * Fires after each specific row in the TGMPA Plugins list table.
2716
			 *
2717
			 * The dynamic portion of the hook name, `$item['slug']`, refers to the slug
2718
			 * for the plugin.
2719
			 *
2720
			 * @since 2.5.0
2721
			 */
2722
			do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context );
2723
		}
2724
2725
		/**
2726
		 * Show the upgrade notice below a plugin row if there is one.
2727
		 *
2728
		 * @since 2.5.0
2729
		 *
2730
		 * @see /wp-admin/includes/update.php
2731
		 *
2732
		 * @param string $slug Plugin slug.
2733
		 * @param array  $item The information available in this table row.
2734
		 * @return null Return early if upgrade notice is empty.
2735
		 */
2736
		public function wp_plugin_update_row( $slug, $item ) {
2737
			if ( empty( $item['upgrade_notice'] ) ) {
2738
				return;
2739
			}
2740
2741
			echo '
2742
				<tr class="plugin-update-tr">
2743
					<td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange">
2744
						<div class="update-message">',
2745
							esc_html__( 'Upgrade message from the plugin author:', 'tgmpa' ),
2746
							' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong>
2747
						</div>
2748
					</td>
2749
				</tr>';
2750
		}
2751
2752
		/**
2753
		 * Extra controls to be displayed between bulk actions and pagination.
2754
		 *
2755
		 * @since 2.5.0
2756
		 *
2757
		 * @param string $which 'top' or 'bottom' table navigation.
2758
		 */
2759
		public function extra_tablenav( $which ) {
2760
			if ( 'bottom' === $which ) {
2761
				$this->tgmpa->show_tgmpa_version();
2762
			}
2763
		}
2764
2765
		/**
2766
		 * Defines the bulk actions for handling registered plugins.
2767
		 *
2768
		 * @since 2.2.0
2769
		 *
2770
		 * @return array $actions The bulk actions for the plugin install table.
2771
		 */
2772
		public function get_bulk_actions() {
2773
2774
			$actions = array();
2775
2776
			if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) {
2777
				if ( current_user_can( 'install_plugins' ) ) {
2778
					$actions['tgmpa-bulk-install'] = __( 'Install', 'tgmpa' );
2779
				}
2780
			}
2781
2782
			if ( 'install' !== $this->view_context ) {
2783
				if ( current_user_can( 'update_plugins' ) ) {
2784
					$actions['tgmpa-bulk-update'] = __( 'Update', 'tgmpa' );
2785
				}
2786
				if ( current_user_can( 'activate_plugins' ) ) {
2787
					$actions['tgmpa-bulk-activate'] = __( 'Activate', 'tgmpa' );
2788
				}
2789
			}
2790
2791
			return $actions;
2792
		}
2793
2794
		/**
2795
		 * Processes bulk installation and activation actions.
2796
		 *
2797
		 * The bulk installation process looks for the $_POST information and passes that
2798
		 * through if a user has to use WP_Filesystem to enter their credentials.
2799
		 *
2800
		 * @since 2.2.0
2801
		 */
2802
		public function process_bulk_actions() {
2803
			// Bulk installation process.
2804
			if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) {
2805
2806
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
2807
2808
				$install_type = 'install';
2809
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2810
					$install_type = 'update';
2811
				}
2812
2813
				$plugins_to_install = array();
2814
2815
				// Did user actually select any plugins to install/update ?
2816 View Code Duplication
				if ( empty( $_POST['plugin'] ) ) {
2817
					if ( 'install' === $install_type ) {
2818
						$message = __( 'No plugins were selected to be installed. No action taken.', 'tgmpa' );
2819
					} else {
2820
						$message = __( 'No plugins were selected to be updated. No action taken.', 'tgmpa' );
2821
					}
2822
2823
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2824
2825
					return false;
2826
				}
2827
2828
				if ( is_array( $_POST['plugin'] ) ) {
2829
					$plugins_to_install = (array) $_POST['plugin'];
2830
				} elseif ( is_string( $_POST['plugin'] ) ) {
2831
					// Received via Filesystem page - un-flatten array (WP bug #19643).
2832
					$plugins_to_install = explode( ',', $_POST['plugin'] );
2833
				}
2834
2835
				// Sanitize the received input.
2836
				$plugins_to_install = array_map( 'urldecode', $plugins_to_install );
2837
				$plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install );
2838
2839
				// Validate the received input.
2840
				foreach ( $plugins_to_install as $key => $slug ) {
2841
					// Check if the plugin was registered with TGMPA and remove if not.
2842
					if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) {
2843
						unset( $plugins_to_install[ $key ] );
2844
						continue;
2845
					}
2846
2847
					// For install: make sure this is a plugin we *can* install and not one already installed.
2848
					if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) {
2849
						unset( $plugins_to_install[ $key ] );
2850
					}
2851
2852
					// For updates: make sure this is a plugin we *can* update (update available and WP version ok).
2853
					if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) {
2854
						unset( $plugins_to_install[ $key ] );
2855
					}
2856
				}
2857
2858
				// No need to proceed further if we have no plugins to handle.
2859 View Code Duplication
				if ( empty( $plugins_to_install ) ) {
2860
					if ( 'install' === $install_type ) {
2861
						$message = __( 'No plugins are available to be installed at this time.', 'tgmpa' );
2862
					} else {
2863
						$message = __( 'No plugins are available to be updated at this time.', 'tgmpa' );
2864
					}
2865
2866
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2867
2868
					return false;
2869
				}
2870
2871
				// Pass all necessary information if WP_Filesystem is needed.
2872
				$url = wp_nonce_url(
2873
					$this->tgmpa->get_tgmpa_url(),
2874
					'bulk-' . $this->_args['plural']
2875
				);
2876
2877
				// Give validated data back to $_POST which is the only place the filesystem looks for extra fields.
2878
				$_POST['plugin'] = implode( ',', $plugins_to_install ); // Work around for WP bug #19643.
2879
2880
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
2881
				$fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
2882
2883 View Code Duplication
				if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) {
2884
					return true; // Stop the normal page form from displaying, credential request form will be shown.
2885
				}
2886
2887
				// Now we have some credentials, setup WP_Filesystem.
2888 View Code Duplication
				if ( ! WP_Filesystem( $creds ) ) {
2889
					// Our credentials were no good, ask the user for them again.
2890
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
2891
2892
					return true;
2893
				}
2894
2895
				/* If we arrive here, we have the filesystem */
2896
2897
				// Store all information in arrays since we are processing a bulk installation.
2898
				$names      = array();
2899
				$sources    = array(); // Needed for installs.
2900
				$file_paths = array(); // Needed for upgrades.
2901
				$to_inject  = array(); // Information to inject into the update_plugins transient.
2902
2903
				// Prepare the data for validated plugins for the install/upgrade.
2904
				foreach ( $plugins_to_install as $slug ) {
2905
					$name   = $this->tgmpa->plugins[ $slug ]['name'];
2906
					$source = $this->tgmpa->get_download_url( $slug );
2907
2908
					if ( ! empty( $name ) && ! empty( $source ) ) {
2909
						$names[] = $name;
2910
2911
						switch ( $install_type ) {
2912
2913
							case 'install':
2914
								$sources[] = $source;
2915
								break;
2916
2917
							case 'update':
2918
								$file_paths[]                 = $this->tgmpa->plugins[ $slug ]['file_path'];
2919
								$to_inject[ $slug ]           = $this->tgmpa->plugins[ $slug ];
2920
								$to_inject[ $slug ]['source'] = $source;
2921
								break;
2922
						}
2923
					}
2924
				}
2925
				unset( $slug, $name, $source );
2926
2927
				// Create a new instance of TGMPA_Bulk_Installer.
2928
				$installer = new TGMPA_Bulk_Installer(
2929
					new TGMPA_Bulk_Installer_Skin(
2930
						array(
2931
							'url'          => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),
2932
							'nonce'        => 'bulk-' . $this->_args['plural'],
2933
							'names'        => $names,
2934
							'install_type' => $install_type,
2935
						)
2936
					)
2937
				);
2938
2939
				// Wrap the install process with the appropriate HTML.
2940
				echo '<div class="tgmpa">',
2941
					'<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>
2942
					<div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">';
2943
2944
				// Process the bulk installation submissions.
2945
				add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
2946
2947
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2948
					// Inject our info into the update transient.
2949
					$this->tgmpa->inject_update_info( $to_inject );
2950
2951
					$installer->bulk_upgrade( $file_paths );
2952
				} else {
2953
					$installer->bulk_install( $sources );
2954
				}
2955
2956
				remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1 );
2957
2958
				echo '</div></div>';
2959
2960
				return true;
2961
			}
2962
2963
			// Bulk activation process.
2964
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
2965
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
2966
2967
				// Did user actually select any plugins to activate ?
2968
				if ( empty( $_POST['plugin'] ) ) {
2969
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>';
2970
2971
					return false;
2972
				}
2973
2974
				// Grab plugin data from $_POST.
2975
				$plugins = array();
2976
				if ( isset( $_POST['plugin'] ) ) {
2977
					$plugins = array_map( 'urldecode', (array) $_POST['plugin'] );
2978
					$plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins );
2979
				}
2980
2981
				$plugins_to_activate = array();
2982
				$plugin_names        = array();
2983
2984
				// Grab the file paths for the selected & inactive plugins from the registration array.
2985
				foreach ( $plugins as $slug ) {
2986
					if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2987
						$plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path'];
2988
						$plugin_names[]        = $this->tgmpa->plugins[ $slug ]['name'];
2989
					}
2990
				}
2991
				unset( $slug );
2992
2993
				// Return early if there are no plugins to activate.
2994
				if ( empty( $plugins_to_activate ) ) {
2995
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>';
2996
2997
					return false;
2998
				}
2999
3000
				// Now we are good to go - let's start activating plugins.
3001
				$activate = activate_plugins( $plugins_to_activate );
3002
3003
				if ( is_wp_error( $activate ) ) {
3004
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
3005
				} else {
3006
					$count        = count( $plugin_names ); // Count so we can use _n function.
3007
					$plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names );
3008
					$last_plugin  = array_pop( $plugin_names ); // Pop off last name to prep for readability.
3009
					$imploded     = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
3010
3011
					printf( // WPCS: xss ok.
3012
						'<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
3013
						esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),
3014
						$imploded
3015
					);
3016
3017
					// Update recently activated plugins option.
3018
					$recent = (array) get_option( 'recently_activated' );
3019
					foreach ( $plugins_to_activate as $plugin => $time ) {
3020
						if ( isset( $recent[ $plugin ] ) ) {
3021
							unset( $recent[ $plugin ] );
3022
						}
3023
					}
3024
					update_option( 'recently_activated', $recent );
3025
				}
3026
3027
				unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another.
3028
3029
				return true;
3030
			}
3031
3032
			return false;
3033
		}
3034
3035
		/**
3036
		 * Prepares all of our information to be outputted into a usable table.
3037
		 *
3038
		 * @since 2.2.0
3039
		 */
3040
		public function prepare_items() {
3041
			$columns               = $this->get_columns(); // Get all necessary column information.
3042
			$hidden                = array(); // No columns to hide, but we must set as an array.
3043
			$sortable              = array(); // No reason to make sortable columns.
3044
			$primary               = $this->get_primary_column_name(); // Column which has the row actions.
3045
			$this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers.
3046
3047
			// Process our bulk activations here.
3048
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3049
				$this->process_bulk_actions();
3050
			}
3051
3052
			// Store all of our plugin data into $items array so WP_List_Table can use it.
3053
			$this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() );
3054
		}
3055
3056
		/* *********** DEPRECATED METHODS *********** */
3057
3058
		/**
3059
		 * Retrieve plugin data, given the plugin name.
3060
		 *
3061
		 * @since      2.2.0
3062
		 * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead.
3063
		 * @see        TGM_Plugin_Activation::_get_plugin_data_from_name()
3064
		 *
3065
		 * @param string $name Name of the plugin, as it was registered.
3066
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
3067
		 * @return string|boolean Plugin slug if found, false otherwise.
3068
		 */
3069
		protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
3070
			_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' );
3071
3072
			return $this->tgmpa->_get_plugin_data_from_name( $name, $data );
3073
		}
3074
	}
3075
}
3076
3077
3078
if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
3079
3080
	/**
3081
	 * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+.
3082
	 */
3083
	class TGM_Bulk_Installer {
3084
	}
3085
}
3086
if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
3087
3088
	/**
3089
	 * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+.
3090
	 */
3091
	class TGM_Bulk_Installer_Skin {
3092
	}
3093
}
3094
3095
/**
3096
 * The WP_Upgrader file isn't always available. If it isn't available,
3097
 * we load it here.
3098
 *
3099
 * We check to make sure no action or activation keys are set so that WordPress
3100
 * does not try to re-include the class when processing upgrades or installs outside
3101
 * of the class.
3102
 *
3103
 * @since 2.2.0
3104
 */
3105
add_action( 'admin_init', 'tgmpa_load_bulk_installer' );
3106
if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
3107
	/**
3108
	 * Load bulk installer
3109
	 */
3110
	function tgmpa_load_bulk_installer() {
3111
		// Silently fail if 2.5+ is loaded *after* an older version.
3112
		if ( ! isset( $GLOBALS['tgmpa'] ) ) {
3113
			return;
3114
		}
3115
3116
		// Get TGMPA class instance.
3117
		$tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3118
3119
		if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) {
3120
			if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
3121
				require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
3122
			}
3123
3124
			if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) {
3125
3126
				/**
3127
				 * Installer class to handle bulk plugin installations.
3128
				 *
3129
				 * Extends WP_Upgrader and customizes to suit the installation of multiple
3130
				 * plugins.
3131
				 *
3132
				 * @since 2.2.0
3133
				 *
3134
				 * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}}
3135
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
3136
				 *            This was done to prevent backward compatibility issues with v2.3.6.}}
3137
				 *
3138
				 * @package TGM-Plugin-Activation
3139
				 * @author  Thomas Griffin
3140
				 * @author  Gary Jones
3141
				 */
3142
				class TGMPA_Bulk_Installer extends Plugin_Upgrader {
3143
					/**
3144
					 * Holds result of bulk plugin installation.
3145
					 *
3146
					 * @since 2.2.0
3147
					 *
3148
					 * @var string
3149
					 */
3150
					public $result;
3151
3152
					/**
3153
					 * Flag to check if bulk installation is occurring or not.
3154
					 *
3155
					 * @since 2.2.0
3156
					 *
3157
					 * @var boolean
3158
					 */
3159
					public $bulk = false;
3160
3161
					/**
3162
					 * TGMPA instance
3163
					 *
3164
					 * @since 2.5.0
3165
					 *
3166
					 * @var object
3167
					 */
3168
					protected $tgmpa;
3169
3170
					/**
3171
					 * Whether or not the destination directory needs to be cleared ( = on update).
3172
					 *
3173
					 * @since 2.5.0
3174
					 *
3175
					 * @var bool
3176
					 */
3177
					protected $clear_destination = false;
3178
3179
					/**
3180
					 * References parent constructor and sets defaults for class.
3181
					 *
3182
					 * @since 2.2.0
3183
					 *
3184
					 * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
3185
					 */
3186
					public function __construct( $skin = null ) {
3187
						// Get TGMPA class instance.
3188
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3189
3190
						parent::__construct( $skin );
3191
3192
						if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
3193
							$this->clear_destination = true;
3194
						}
3195
3196
						if ( $this->tgmpa->is_automatic ) {
3197
							$this->activate_strings();
3198
						}
3199
3200
						add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
3201
					}
3202
3203
					/**
3204
					 * Sets the correct activation strings for the installer skin to use.
3205
					 *
3206
					 * @since 2.2.0
3207
					 */
3208
					public function activate_strings() {
3209
						$this->strings['activation_failed']  = __( 'Plugin activation failed.', 'tgmpa' );
3210
						$this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' );
3211
					}
3212
3213
					/**
3214
					 * Performs the actual installation of each plugin.
3215
					 *
3216
					 * @since 2.2.0
3217
					 *
3218
					 * @see WP_Upgrader::run()
3219
					 *
3220
					 * @param array $options The installation config options.
3221
					 * @return null|array Return early if error, array of installation data on success.
3222
					 */
3223
					public function run( $options ) {
3224
						$result = parent::run( $options );
3225
3226
						// Reset the strings in case we changed one during automatic activation.
3227
						if ( $this->tgmpa->is_automatic ) {
3228
							if ( 'update' === $this->skin->options['install_type'] ) {
3229
								$this->upgrade_strings();
3230
							} else {
3231
								$this->install_strings();
3232
							}
3233
						}
3234
3235
						return $result;
3236
					}
3237
3238
					/**
3239
					 * Processes the bulk installation of plugins.
3240
					 *
3241
					 * @since 2.2.0
3242
					 *
3243
					 * {@internal This is basically a near identical copy of the WP Core
3244
					 * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with
3245
					 * new installs instead of upgrades.
3246
					 * For ease of future synchronizations, the adjustments are clearly commented, but no other
3247
					 * comments are added. Code style has been made to comply.}}
3248
					 *
3249
					 * @see Plugin_Upgrader::bulk_upgrade()
3250
					 * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
3251
					 * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}}
3252
					 *
3253
					 * @param array $plugins The plugin sources needed for installation.
3254
					 * @param array $args    Arbitrary passed extra arguments.
3255
					 * @return string|bool Install confirmation messages on success, false on failure.
3256
					 */
3257
					public function bulk_install( $plugins, $args = array() ) {
3258
						// [TGMPA + ] Hook auto-activation in.
3259
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3260
3261
						$defaults    = array(
3262
							'clear_update_cache' => true,
3263
						);
3264
						$parsed_args = wp_parse_args( $args, $defaults );
3265
3266
						$this->init();
3267
						$this->bulk = true;
3268
3269
						$this->install_strings(); // [TGMPA + ] adjusted.
3270
3271
						/* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
3272
3273
						/* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
3274
3275
						$this->skin->header();
3276
3277
						// Connect to the Filesystem first.
3278
						$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
3279
						if ( ! $res ) {
3280
							$this->skin->footer();
3281
							return false;
3282
						}
3283
3284
						$this->skin->bulk_header();
3285
3286
						/*
3287
						 * Only start maintenance mode if:
3288
						 * - running Multisite and there are one or more plugins specified, OR
3289
						 * - a plugin with an update available is currently active.
3290
						 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
3291
						 */
3292
						$maintenance = ( is_multisite() && ! empty( $plugins ) );
3293
3294
						/*
3295
						[TGMPA - ]
3296
						foreach ( $plugins as $plugin )
3297
							$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
3298
						*/
3299
						if ( $maintenance ) {
3300
							$this->maintenance_mode( true );
3301
						}
3302
3303
						$results = array();
3304
3305
						$this->update_count   = count( $plugins );
3306
						$this->update_current = 0;
3307
						foreach ( $plugins as $plugin ) {
3308
							$this->update_current++;
3309
3310
							/*
3311
							[TGMPA - ]
3312
							$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
3313
3314
							if ( !isset( $current->response[ $plugin ] ) ) {
3315
								$this->skin->set_result('up_to_date');
3316
								$this->skin->before();
3317
								$this->skin->feedback('up_to_date');
3318
								$this->skin->after();
3319
								$results[$plugin] = true;
3320
								continue;
3321
							}
3322
3323
							// Get the URL to the zip file.
3324
							$r = $current->response[ $plugin ];
3325
3326
							$this->skin->plugin_active = is_plugin_active($plugin);
3327
							*/
3328
3329
							$result = $this->run(
3330
								array(
3331
									'package'           => $plugin, // [TGMPA + ] adjusted.
3332
									'destination'       => WP_PLUGIN_DIR,
3333
									'clear_destination' => false, // [TGMPA + ] adjusted.
3334
									'clear_working'     => true,
3335
									'is_multi'          => true,
3336
									'hook_extra'        => array(
3337
										'plugin' => $plugin,
3338
									),
3339
								)
3340
							);
3341
3342
							$results[ $plugin ] = $this->result;
3343
3344
							// Prevent credentials auth screen from displaying multiple times.
3345
							if ( false === $result ) {
3346
								break;
3347
							}
3348
						} //end foreach $plugins
3349
3350
						$this->maintenance_mode( false );
3351
3352
						/**
3353
						 * Fires when the bulk upgrader process is complete.
3354
						 *
3355
						 * @since WP 3.6.0 / TGMPA 2.5.0
3356
						 *
3357
						 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
3358
						 *                              be a Theme_Upgrader or Core_Upgrade instance.
3359
						 * @param array           $data {
3360
						 *     Array of bulk item update data.
3361
						 *
3362
						 *     @type string $action   Type of action. Default 'update'.
3363
						 *     @type string $type     Type of update process. Accepts 'plugin', 'theme', or 'core'.
3364
						 *     @type bool   $bulk     Whether the update process is a bulk update. Default true.
3365
						 *     @type array  $packages Array of plugin, theme, or core packages to update.
3366
						 * }
3367
						 */
3368
						do_action( 'upgrader_process_complete', $this, array(
3369
							'action'  => 'install', // [TGMPA + ] adjusted.
3370
							'type'    => 'plugin',
3371
							'bulk'    => true,
3372
							'plugins' => $plugins,
3373
						) );
3374
3375
						$this->skin->bulk_footer();
3376
3377
						$this->skin->footer();
3378
3379
						// Cleanup our hooks, in case something else does a upgrade on this connection.
3380
						/* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
3381
3382
						// [TGMPA + ] Remove our auto-activation hook.
3383
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3384
3385
						// Force refresh of plugin update information.
3386
						wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
3387
3388
						return $results;
3389
					}
3390
3391
					/**
3392
					 * Handle a bulk upgrade request.
3393
					 *
3394
					 * @since 2.5.0
3395
					 *
3396
					 * @see Plugin_Upgrader::bulk_upgrade()
3397
					 *
3398
					 * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
3399
					 * @param array $args    Arbitrary passed extra arguments.
3400
					 * @return string|bool Install confirmation messages on success, false on failure.
3401
					 */
3402
					public function bulk_upgrade( $plugins, $args = array() ) {
3403
3404
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3405
3406
						$result = parent::bulk_upgrade( $plugins, $args );
3407
3408
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3409
3410
						return $result;
3411
					}
3412
3413
					/**
3414
					 * Abuse a filter to auto-activate plugins after installation.
3415
					 *
3416
					 * Hooked into the 'upgrader_post_install' filter hook.
3417
					 *
3418
					 * @since 2.5.0
3419
					 *
3420
					 * @param bool $bool The value we need to give back (true).
3421
					 * @return bool
3422
					 */
3423
					public function auto_activate( $bool ) {
3424
						// Only process the activation of installed plugins if the automatic flag is set to true.
3425
						if ( $this->tgmpa->is_automatic ) {
3426
							// Flush plugins cache so the headers of the newly installed plugins will be read correctly.
3427
							wp_clean_plugins_cache();
3428
3429
							// Get the installed plugin file.
3430
							$plugin_info = $this->plugin_info();
3431
3432
							// Don't try to activate on upgrade of active plugin as WP will do this already.
3433
							if ( ! is_plugin_active( $plugin_info ) ) {
3434
								$activate = activate_plugin( $plugin_info );
3435
3436
								// Adjust the success string based on the activation result.
3437
								$this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
3438
3439
								if ( is_wp_error( $activate ) ) {
3440
									$this->skin->error( $activate );
3441
									$this->strings['process_success'] .= $this->strings['activation_failed'];
3442
								} else {
3443
									$this->strings['process_success'] .= $this->strings['activation_success'];
3444
								}
3445
							}
3446
						}
3447
3448
						return $bool;
3449
					}
3450
				}
3451
			}
3452
3453
			if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) {
3454
3455
				/**
3456
				 * Installer skin to set strings for the bulk plugin installations..
3457
				 *
3458
				 * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
3459
				 * plugins.
3460
				 *
3461
				 * @since 2.2.0
3462
				 *
3463
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to
3464
				 *           TGMPA_Bulk_Installer_Skin.
3465
				 *           This was done to prevent backward compatibility issues with v2.3.6.}}
3466
				 *
3467
				 * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php
3468
				 *
3469
				 * @package TGM-Plugin-Activation
3470
				 * @author  Thomas Griffin
3471
				 * @author  Gary Jones
3472
				 */
3473
				class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
3474
					/**
3475
					 * Holds plugin info for each individual plugin installation.
3476
					 *
3477
					 * @since 2.2.0
3478
					 *
3479
					 * @var array
3480
					 */
3481
					public $plugin_info = array();
3482
3483
					/**
3484
					 * Holds names of plugins that are undergoing bulk installations.
3485
					 *
3486
					 * @since 2.2.0
3487
					 *
3488
					 * @var array
3489
					 */
3490
					public $plugin_names = array();
3491
3492
					/**
3493
					 * Integer to use for iteration through each plugin installation.
3494
					 *
3495
					 * @since 2.2.0
3496
					 *
3497
					 * @var integer
3498
					 */
3499
					public $i = 0;
3500
3501
					/**
3502
					 * TGMPA instance
3503
					 *
3504
					 * @since 2.5.0
3505
					 *
3506
					 * @var object
3507
					 */
3508
					protected $tgmpa;
3509
3510
					/**
3511
					 * Constructor. Parses default args with new ones and extracts them for use.
3512
					 *
3513
					 * @since 2.2.0
3514
					 *
3515
					 * @param array $args Arguments to pass for use within the class.
3516
					 */
3517
					public function __construct( $args = array() ) {
3518
						// Get TGMPA class instance.
3519
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3520
3521
						// Parse default and new args.
3522
						$defaults = array(
3523
							'url'          => '',
3524
							'nonce'        => '',
3525
							'names'        => array(),
3526
							'install_type' => 'install',
3527
						);
3528
						$args     = wp_parse_args( $args, $defaults );
3529
3530
						// Set plugin names to $this->plugin_names property.
3531
						$this->plugin_names = $args['names'];
3532
3533
						// Extract the new args.
3534
						parent::__construct( $args );
3535
					}
3536
3537
					/**
3538
					 * Sets install skin strings for each individual plugin.
3539
					 *
3540
					 * Checks to see if the automatic activation flag is set and uses the
3541
					 * the proper strings accordingly.
3542
					 *
3543
					 * @since 2.2.0
3544
					 */
3545
					public function add_strings() {
3546
						if ( 'update' === $this->options['install_type'] ) {
3547
							parent::add_strings();
3548
							$this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3549
						} else {
3550
							$this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
3551
							$this->upgrader->strings['skin_update_failed']       = __( 'The installation of %1$s failed.', 'tgmpa' );
3552
3553
							if ( $this->tgmpa->is_automatic ) {
3554
								// Automatic activation strings.
3555
								$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' );
3556
								$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>';
3557
								$this->upgrader->strings['skin_upgrade_end']          = __( 'All installations and activations have been completed.', 'tgmpa' );
3558
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3559
							} else {
3560
								// Default installation strings.
3561
								$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' );
3562
								$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>';
3563
								$this->upgrader->strings['skin_upgrade_end']          = __( 'All installations have been completed.', 'tgmpa' );
3564
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3565
							}
3566
						}
3567
					}
3568
3569
					/**
3570
					 * Outputs the header strings and necessary JS before each plugin installation.
3571
					 *
3572
					 * @since 2.2.0
3573
					 *
3574
					 * @param string $title Unused in this implementation.
3575
					 */
3576
					public function before( $title = '' ) {
3577
						if ( empty( $title ) ) {
3578
							$title = esc_html( $this->plugin_names[ $this->i ] );
3579
						}
3580
						parent::before( $title );
3581
					}
3582
3583
					/**
3584
					 * Outputs the footer strings and necessary JS after each plugin installation.
3585
					 *
3586
					 * Checks for any errors and outputs them if they exist, else output
3587
					 * success strings.
3588
					 *
3589
					 * @since 2.2.0
3590
					 *
3591
					 * @param string $title Unused in this implementation.
3592
					 */
3593
					public function after( $title = '' ) {
3594
						if ( empty( $title ) ) {
3595
							$title = esc_html( $this->plugin_names[ $this->i ] );
3596
						}
3597
						parent::after( $title );
3598
3599
						$this->i++;
3600
					}
3601
3602
					/**
3603
					 * Outputs links after bulk plugin installation is complete.
3604
					 *
3605
					 * @since 2.2.0
3606
					 */
3607
					public function bulk_footer() {
3608
						// Serve up the string to say installations (and possibly activations) are complete.
3609
						parent::bulk_footer();
3610
3611
						// Flush plugins cache so we can make sure that the installed plugins list is always up to date.
3612
						wp_clean_plugins_cache();
3613
3614
						$this->tgmpa->show_tgmpa_version();
3615
3616
						// Display message based on if all plugins are now active or not.
3617
						$update_actions = array();
3618
3619
						if ( $this->tgmpa->is_tgmpa_complete() ) {
3620
							// All plugins are active, so we display the complete string and hide the menu to protect users.
3621
							echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
3622
							$update_actions['dashboard'] = sprintf(
3623
								esc_html( $this->tgmpa->strings['complete'] ),
3624
								'<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>'
3625
							);
3626
						} else {
3627
							$update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>';
3628
						}
3629
3630
						/**
3631
						 * Filter the list of action links available following bulk plugin installs/updates.
3632
						 *
3633
						 * @since 2.5.0
3634
						 *
3635
						 * @param array $update_actions Array of plugin action links.
3636
						 * @param array $plugin_info    Array of information for the last-handled plugin.
3637
						 */
3638
						$update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
3639
3640
						if ( ! empty( $update_actions ) ) {
3641
							$this->feedback( implode( ' | ', (array) $update_actions ) );
3642
						}
3643
					}
3644
3645
					/* *********** DEPRECATED METHODS *********** */
3646
3647
					/**
3648
					 * Flush header output buffer.
3649
					 *
3650
					 * @since      2.2.0
3651
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3652
					 * @see        Bulk_Upgrader_Skin::flush_output()
3653
					 */
3654
					public function before_flush_output() {
3655
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3656
						$this->flush_output();
3657
					}
3658
3659
					/**
3660
					 * Flush footer output buffer and iterate $this->i to make sure the
3661
					 * installation strings reference the correct plugin.
3662
					 *
3663
					 * @since      2.2.0
3664
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3665
					 * @see        Bulk_Upgrader_Skin::flush_output()
3666
					 */
3667
					public function after_flush_output() {
3668
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3669
						$this->flush_output();
3670
						$this->i++;
3671
					}
3672
				}
3673
			}
3674
		}
3675
	}
3676
}
3677
3678
if ( ! class_exists( 'TGMPA_Utils' ) ) {
3679
3680
	/**
3681
	 * Generic utilities for TGMPA.
3682
	 *
3683
	 * All methods are static, poor-dev name-spacing class wrapper.
3684
	 *
3685
	 * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy.
3686
	 *
3687
	 * @since 2.5.0
3688
	 *
3689
	 * @package TGM-Plugin-Activation
3690
	 * @author  Juliette Reinders Folmer
3691
	 */
3692
	class TGMPA_Utils {
3693
		/**
3694
		 * Whether the PHP filter extension is enabled.
3695
		 *
3696
		 * @see http://php.net/book.filter
3697
		 *
3698
		 * @since 2.5.0
3699
		 *
3700
		 * @static
3701
		 *
3702
		 * @var bool $has_filters True is the extension is enabled.
3703
		 */
3704
		public static $has_filters;
3705
3706
		/**
3707
		 * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map().
3708
		 *
3709
		 * @since 2.5.0
3710
		 *
3711
		 * @static
3712
		 *
3713
		 * @param string $string Text to be wrapped.
3714
		 * @return string
3715
		 */
3716
		public static function wrap_in_em( $string ) {
3717
			return '<em>' . wp_kses_post( $string ) . '</em>';
3718
		}
3719
3720
		/**
3721
		 * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map().
3722
		 *
3723
		 * @since 2.5.0
3724
		 *
3725
		 * @static
3726
		 *
3727
		 * @param string $string Text to be wrapped.
3728
		 * @return string
3729
		 */
3730
		public static function wrap_in_strong( $string ) {
3731
			return '<strong>' . wp_kses_post( $string ) . '</strong>';
3732
		}
3733
3734
		/**
3735
		 * Helper function: Validate a value as boolean
3736
		 *
3737
		 * @since 2.5.0
3738
		 *
3739
		 * @static
3740
		 *
3741
		 * @param mixed $value Arbitrary value.
3742
		 * @return bool
3743
		 */
3744
		public static function validate_bool( $value ) {
3745
			if ( ! isset( self::$has_filters ) ) {
3746
				self::$has_filters = extension_loaded( 'filter' );
3747
			}
3748
3749
			if ( self::$has_filters ) {
3750
				return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
3751
			} else {
3752
				return self::emulate_filter_bool( $value );
3753
			}
3754
		}
3755
3756
		/**
3757
		 * Helper function: Cast a value to bool
3758
		 *
3759
		 * @since 2.5.0
3760
		 *
3761
		 * @static
3762
		 *
3763
		 * @param mixed $value Value to cast.
3764
		 * @return bool
3765
		 */
3766
		protected static function emulate_filter_bool( $value ) {
3767
			// @codingStandardsIgnoreStart
3768
			static $true  = array(
3769
				'1',
3770
				'true', 'True', 'TRUE',
3771
				'y', 'Y',
3772
				'yes', 'Yes', 'YES',
3773
				'on', 'On', 'ON',
3774
			);
3775
			static $false = array(
3776
				'0',
3777
				'false', 'False', 'FALSE',
3778
				'n', 'N',
3779
				'no', 'No', 'NO',
3780
				'off', 'Off', 'OFF',
3781
			);
3782
			// @codingStandardsIgnoreEnd
3783
3784
			if ( is_bool( $value ) ) {
3785
				return $value;
3786
			} else if ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
3787
				return (bool) $value;
3788
			} else if ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
3789
				return (bool) $value;
3790
			} else if ( is_string( $value ) ) {
3791
				$value = trim( $value );
3792
				if ( in_array( $value, $true, true ) ) {
3793
					return true;
3794
				} else if ( in_array( $value, $false, true ) ) {
3795
					return false;
3796
				} else {
3797
					return false;
3798
				}
3799
			}
3800
3801
			return false;
3802
		}
3803
	} // End of class TGMPA_Utils
3804
} // End of class_exists wrapper
3805