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

TGM_Plugin_Activation::show_tgmpa_version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plugin installation and activation for WordPress themes.
4
 *
5
 * Please note that this is a drop-in library for a theme or plugin.
6
 * The authors of this library (Thomas, Gary and Juliette) are NOT responsible
7
 * for the support of your plugin or theme. Please contact the plugin
8
 * or theme author for support.
9
 *
10
 * @package   TGM-Plugin-Activation
11
 * @version   2.6.1
12
 * @link      http://tgmpluginactivation.com/
13
 * @author    Thomas Griffin, Gary Jones, Juliette Reinders Folmer
14
 * @copyright Copyright (c) 2011, Thomas Griffin
15
 * @license   GPL-2.0+
16
 */
17
18
/*
19
	Copyright 2011 Thomas Griffin (thomasgriffinmedia.com)
20
21
	This program is free software; you can redistribute it and/or modify
22
	it under the terms of the GNU General Public License, version 2, as
23
	published by the Free Software Foundation.
24
25
	This program is distributed in the hope that it will be useful,
26
	but WITHOUT ANY WARRANTY; without even the implied warranty of
27
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
	GNU General Public License for more details.
29
30
	You should have received a copy of the GNU General Public License
31
	along with this program; if not, write to the Free Software
32
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
33
*/
34
35
if ( ! class_exists( 'TGM_Plugin_Activation' ) ) {
36
37
	/**
38
	 * Automatic plugin installation and activation library.
39
	 *
40
	 * Creates a way to automatically install and activate plugins from within themes.
41
	 * The plugins can be either bundled, downloaded from the WordPress
42
	 * Plugin Repository or downloaded from another external source.
43
	 *
44
	 * @since 1.0.0
45
	 *
46
	 * @package TGM-Plugin-Activation
47
	 * @author  Thomas Griffin
48
	 * @author  Gary Jones
49
	 */
50
	class TGM_Plugin_Activation {
51
		/**
52
		 * TGMPA version number.
53
		 *
54
		 * @since 2.5.0
55
		 *
56
		 * @const string Version number.
57
		 */
58
		const TGMPA_VERSION = '2.6.1';
59
60
		/**
61
		 * Regular expression to test if a URL is a WP plugin repo URL.
62
		 *
63
		 * @const string Regex.
64
		 *
65
		 * @since 2.5.0
66
		 */
67
		const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|';
68
69
		/**
70
		 * Arbitrary regular expression to test if a string starts with a URL.
71
		 *
72
		 * @const string Regex.
73
		 *
74
		 * @since 2.5.0
75
		 */
76
		const IS_URL_REGEX = '|^http[s]?://|';
77
78
		/**
79
		 * Holds a copy of itself, so it can be referenced by the class name.
80
		 *
81
		 * @since 1.0.0
82
		 *
83
		 * @var TGM_Plugin_Activation
84
		 */
85
		public static $instance;
86
87
		/**
88
		 * Holds arrays of plugin details.
89
		 *
90
		 * @since 1.0.0
91
		 * @since 2.5.0 the array has the plugin slug as an associative key.
92
		 *
93
		 * @var array
94
		 */
95
		public $plugins = array();
96
97
		/**
98
		 * Holds arrays of plugin names to use to sort the plugins array.
99
		 *
100
		 * @since 2.5.0
101
		 *
102
		 * @var array
103
		 */
104
		protected $sort_order = array();
105
106
		/**
107
		 * Whether any plugins have the 'force_activation' setting set to true.
108
		 *
109
		 * @since 2.5.0
110
		 *
111
		 * @var bool
112
		 */
113
		protected $has_forced_activation = false;
114
115
		/**
116
		 * Whether any plugins have the 'force_deactivation' setting set to true.
117
		 *
118
		 * @since 2.5.0
119
		 *
120
		 * @var bool
121
		 */
122
		protected $has_forced_deactivation = false;
123
124
		/**
125
		 * Name of the unique ID to hash notices.
126
		 *
127
		 * @since 2.4.0
128
		 *
129
		 * @var string
130
		 */
131
		public $id = 'tgmpa';
132
133
		/**
134
		 * Name of the query-string argument for the admin page.
135
		 *
136
		 * @since 1.0.0
137
		 *
138
		 * @var string
139
		 */
140
		protected $menu = 'tgmpa-install-plugins';
141
142
		/**
143
		 * Parent menu file slug.
144
		 *
145
		 * @since 2.5.0
146
		 *
147
		 * @var string
148
		 */
149
		public $parent_slug = 'themes.php';
150
151
		/**
152
		 * Capability needed to view the plugin installation menu item.
153
		 *
154
		 * @since 2.5.0
155
		 *
156
		 * @var string
157
		 */
158
		public $capability = 'edit_theme_options';
159
160
		/**
161
		 * Default absolute path to folder containing bundled plugin zip files.
162
		 *
163
		 * @since 2.0.0
164
		 *
165
		 * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string.
166
		 */
167
		public $default_path = '';
168
169
		/**
170
		 * Flag to show admin notices or not.
171
		 *
172
		 * @since 2.1.0
173
		 *
174
		 * @var boolean
175
		 */
176
		public $has_notices = true;
177
178
		/**
179
		 * Flag to determine if the user can dismiss the notice nag.
180
		 *
181
		 * @since 2.4.0
182
		 *
183
		 * @var boolean
184
		 */
185
		public $dismissable = true;
186
187
		/**
188
		 * Message to be output above nag notice if dismissable is false.
189
		 *
190
		 * @since 2.4.0
191
		 *
192
		 * @var string
193
		 */
194
		public $dismiss_msg = '';
195
196
		/**
197
		 * Flag to set automatic activation of plugins. Off by default.
198
		 *
199
		 * @since 2.2.0
200
		 *
201
		 * @var boolean
202
		 */
203
		public $is_automatic = false;
204
205
		/**
206
		 * Optional message to display before the plugins table.
207
		 *
208
		 * @since 2.2.0
209
		 *
210
		 * @var string Message filtered by wp_kses_post(). Default is empty string.
211
		 */
212
		public $message = '';
213
214
		/**
215
		 * Holds configurable array of strings.
216
		 *
217
		 * Default values are added in the constructor.
218
		 *
219
		 * @since 2.0.0
220
		 *
221
		 * @var array
222
		 */
223
		public $strings = array();
224
225
		/**
226
		 * Holds the version of WordPress.
227
		 *
228
		 * @since 2.4.0
229
		 *
230
		 * @var int
231
		 */
232
		public $wp_version;
233
234
		/**
235
		 * Holds the hook name for the admin page.
236
		 *
237
		 * @since 2.5.0
238
		 *
239
		 * @var string
240
		 */
241
		public $page_hook;
242
243
		/**
244
		 * Adds a reference of this object to $instance, populates default strings,
245
		 * does the tgmpa_init action hook, and hooks in the interactions to init.
246
		 *
247
		 * {@internal This method should be `protected`, but as too many TGMPA implementations
248
		 * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues.
249
		 * Reverted back to public for the time being.}}
250
		 *
251
		 * @since 1.0.0
252
		 *
253
		 * @see TGM_Plugin_Activation::init()
254
		 */
255
		public function __construct() {
256
			// Set the current WordPress version.
257
			$this->wp_version = $GLOBALS['wp_version'];
258
259
			// Announce that the class is ready, and pass the object (for advanced use).
260
			do_action_ref_array( 'tgmpa_init', array( $this ) );
261
262
			/*
263
			 * Load our text domain and allow for overloading the fall-back file.
264
			 *
265
			 * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
266
			 * generator on the website.}}
267
			 */
268
			add_action( 'init', array( $this, 'load_textdomain' ), 5 );
269
			add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 );
270
271
			// When the rest of WP has loaded, kick-start the rest of the class.
272
			add_action( 'init', array( $this, 'init' ) );
273
		}
274
275
		/**
276
		 * Magic method to (not) set protected properties from outside of this class.
277
		 *
278
		 * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6  where the `menu` property
279
		 * is being assigned rather than tested in a conditional, effectively rendering it useless.
280
		 * This 'hack' prevents this from happening.}}
281
		 *
282
		 * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593
283
		 *
284
		 * @since 2.5.2
285
		 *
286
		 * @param string $name  Name of an inaccessible property.
287
		 * @param mixed  $value Value to assign to the property.
288
		 * @return void  Silently fail to set the property when this is tried from outside of this class context.
289
		 *               (Inside this class context, the __set() method if not used as there is direct access.)
290
		 */
291
		public function __set( $name, $value ) {
292
			return;
293
		}
294
295
		/**
296
		 * Magic method to get the value of a protected property outside of this class context.
297
		 *
298
		 * @since 2.5.2
299
		 *
300
		 * @param string $name Name of an inaccessible property.
301
		 * @return mixed The property value.
302
		 */
303
		public function __get( $name ) {
304
			return $this->{$name};
305
		}
306
307
		/**
308
		 * Initialise the interactions between this class and WordPress.
309
		 *
310
		 * Hooks in three new methods for the class: admin_menu, notices and styles.
311
		 *
312
		 * @since 2.0.0
313
		 *
314
		 * @see TGM_Plugin_Activation::admin_menu()
315
		 * @see TGM_Plugin_Activation::notices()
316
		 * @see TGM_Plugin_Activation::styles()
317
		 */
318
		public function init() {
319
			/**
320
			 * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter
321
			 * you can overrule that behaviour.
322
			 *
323
			 * @since 2.5.0
324
			 *
325
			 * @param bool $load Whether or not TGMPA should load.
326
			 *                   Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`.
327
			 */
328
			if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) {
329
				return;
330
			}
331
332
			// Load class strings.
333
			$this->strings = array(
334
				'page_title'                      => __( 'Install Required Plugins', 'tgmpa' ),
335
				'menu_title'                      => __( 'Install Plugins', 'tgmpa' ),
336
				/* translators: %s: plugin name. */
337
				'installing'                      => __( 'Installing Plugin: %s', 'tgmpa' ),
338
				/* translators: %s: plugin name. */
339
				'updating'                        => __( 'Updating Plugin: %s', 'tgmpa' ),
340
				'oops'                            => __( 'Something went wrong with the plugin API.', 'tgmpa' ),
341
				/* translators: 1: plugin name(s). */
342
				'notice_can_install_required'     => _n_noop(
343
					'This theme requires the following plugin: %1$s.',
344
					'This theme requires the following plugins: %1$s.',
345
					'tgmpa'
346
				),
347
				/* translators: 1: plugin name(s). */
348
				'notice_can_install_recommended'  => _n_noop(
349
					'This theme recommends the following plugin: %1$s.',
350
					'This theme recommends the following plugins: %1$s.',
351
					'tgmpa'
352
				),
353
				/* translators: 1: plugin name(s). */
354
				'notice_ask_to_update'            => _n_noop(
355
					'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',
356
					'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',
357
					'tgmpa'
358
				),
359
				/* translators: 1: plugin name(s). */
360
				'notice_ask_to_update_maybe'      => _n_noop(
361
					'There is an update available for: %1$s.',
362
					'There are updates available for the following plugins: %1$s.',
363
					'tgmpa'
364
				),
365
				/* translators: 1: plugin name(s). */
366
				'notice_can_activate_required'    => _n_noop(
367
					'The following required plugin is currently inactive: %1$s.',
368
					'The following required plugins are currently inactive: %1$s.',
369
					'tgmpa'
370
				),
371
				/* translators: 1: plugin name(s). */
372
				'notice_can_activate_recommended' => _n_noop(
373
					'The following recommended plugin is currently inactive: %1$s.',
374
					'The following recommended plugins are currently inactive: %1$s.',
375
					'tgmpa'
376
				),
377
				'install_link'                    => _n_noop(
378
					'Begin installing plugin',
379
					'Begin installing plugins',
380
					'tgmpa'
381
				),
382
				'update_link'                     => _n_noop(
383
					'Begin updating plugin',
384
					'Begin updating plugins',
385
					'tgmpa'
386
				),
387
				'activate_link'                   => _n_noop(
388
					'Begin activating plugin',
389
					'Begin activating plugins',
390
					'tgmpa'
391
				),
392
				'return'                          => __( 'Return to Required Plugins Installer', 'tgmpa' ),
393
				'dashboard'                       => __( 'Return to the Dashboard', 'tgmpa' ),
394
				'plugin_activated'                => __( 'Plugin activated successfully.', 'tgmpa' ),
395
				'activated_successfully'          => __( 'The following plugin was activated successfully:', 'tgmpa' ),
396
				/* translators: 1: plugin name. */
397
				'plugin_already_active'           => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ),
398
				/* translators: 1: plugin name. */
399
				'plugin_needs_higher_version'     => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ),
400
				/* translators: 1: dashboard link. */
401
				'complete'                        => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ),
402
				'dismiss'                         => __( 'Dismiss this notice', 'tgmpa' ),
403
				'notice_cannot_install_activate'  => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ),
404
				'contact_admin'                   => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),
405
			);
406
407
			do_action( 'tgmpa_register' );
408
409
			/* After this point, the plugins should be registered and the configuration set. */
410
411
			// Proceed only if we have plugins to handle.
412
			if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
413
				return;
414
			}
415
416
			// Set up the menu and notices if we still have outstanding actions.
417
			if ( true !== $this->is_tgmpa_complete() ) {
418
				// Sort the plugins.
419
				array_multisort( $this->sort_order, SORT_ASC, $this->plugins );
420
421
				add_action( 'admin_menu', array( $this, 'admin_menu' ) );
422
				add_action( 'admin_head', array( $this, 'dismiss' ) );
423
424
				// Prevent the normal links from showing underneath a single install/update page.
425
				add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
426
				add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) );
427
428
				if ( $this->has_notices ) {
429
					add_action( 'admin_notices', array( $this, 'notices' ) );
430
					add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
431
					add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
432
				}
433
			}
434
435
			// If needed, filter plugin action links.
436
			add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 );
437
438
			// Make sure things get reset on switch theme.
439
			add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
440
441
			if ( $this->has_notices ) {
442
				add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
443
			}
444
445
			// Setup the force activation hook.
446
			if ( true === $this->has_forced_activation ) {
447
				add_action( 'admin_init', array( $this, 'force_activation' ) );
448
			}
449
450
			// Setup the force deactivation hook.
451
			if ( true === $this->has_forced_deactivation ) {
452
				add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
453
			}
454
455
			// Add CSS for the TGMPA admin page.
456
			add_action( 'admin_head', array( $this, 'admin_css' ) );
457
		}
458
459
		/**
460
		 * Load translations.
461
		 *
462
		 * @since 2.6.0
463
		 *
464
		 * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to
465
		 * get round the different ways of handling the path and deprecated notices being thrown
466
		 * and such. For plugins, the actual file name will be corrected by a filter.}}
467
		 *
468
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
469
		 * generator on the website.}}
470
		 */
471
		public function load_textdomain() {
472
			if ( is_textdomain_loaded( 'tgmpa' ) ) {
473
				return;
474
			}
475
476
			if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) {
477
				// Plugin, we'll need to adjust the file name.
478
				add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 );
479
				load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
480
				remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 );
481
			} else {
482
				load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
483
			}
484
		}
485
486
		/**
487
		 * Correct the .mo file name for (must-use) plugins.
488
		 *
489
		 * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`.
490
		 *
491
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
492
		 * generator on the website.}}
493
		 *
494
		 * @since 2.6.0
495
		 *
496
		 * @param string $mofile Full path to the target mofile.
497
		 * @param string $domain The domain for which a language file is being loaded.
498
		 * @return string $mofile
499
		 */
500
		public function correct_plugin_mofile( $mofile, $domain ) {
501
			// Exit early if not our domain (just in case).
502
			if ( 'tgmpa' !== $domain ) {
503
				return $mofile;
504
			}
505
			return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile );
506
		}
507
508
		/**
509
		 * Potentially overload the fall-back translation file for the current language.
510
		 *
511
		 * WP, by default since WP 3.7, will load a local translation first and if none
512
		 * can be found, will try and find a translation in the /wp-content/languages/ directory.
513
		 * As this library is theme/plugin agnostic, translation files for TGMPA can exist both
514
		 * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory.
515
		 *
516
		 * This method makes sure both directories are checked.
517
		 *
518
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
519
		 * generator on the website.}}
520
		 *
521
		 * @since 2.6.0
522
		 *
523
		 * @param string $mofile Full path to the target mofile.
524
		 * @param string $domain The domain for which a language file is being loaded.
525
		 * @return string $mofile
526
		 */
527
		public function overload_textdomain_mofile( $mofile, $domain ) {
528
			// Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
529
			if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) {
530
				return $mofile;
531
			}
532
533
			// Current fallback file is not valid, let's try the alternative option.
534
			if ( false !== strpos( $mofile, '/themes/' ) ) {
535
				return str_replace( '/themes/', '/plugins/', $mofile );
536
			} elseif ( false !== strpos( $mofile, '/plugins/' ) ) {
537
				return str_replace( '/plugins/', '/themes/', $mofile );
538
			} else {
539
				return $mofile;
540
			}
541
		}
542
543
		/**
544
		 * Hook in plugin action link filters for the WP native plugins page.
545
		 *
546
		 * - Prevent activation of plugins which don't meet the minimum version requirements.
547
		 * - Prevent deactivation of force-activated plugins.
548
		 * - Add update notice if update available.
549
		 *
550
		 * @since 2.5.0
551
		 */
552
		public function add_plugin_action_link_filters() {
553
			foreach ( $this->plugins as $slug => $plugin ) {
554
				if ( false === $this->can_plugin_activate( $slug ) ) {
555
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 );
556
				}
557
558
				if ( true === $plugin['force_activation'] ) {
559
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 );
560
				}
561
562
				if ( false !== $this->does_plugin_require_update( $slug ) ) {
563
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 );
564
				}
565
			}
566
		}
567
568
		/**
569
		 * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the
570
		 * minimum version requirements.
571
		 *
572
		 * @since 2.5.0
573
		 *
574
		 * @param array $actions Action links.
575
		 * @return array
576
		 */
577
		public function filter_plugin_action_links_activate( $actions ) {
578
			unset( $actions['activate'] );
579
580
			return $actions;
581
		}
582
583
		/**
584
		 * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate.
585
		 *
586
		 * @since 2.5.0
587
		 *
588
		 * @param array $actions Action links.
589
		 * @return array
590
		 */
591
		public function filter_plugin_action_links_deactivate( $actions ) {
592
			unset( $actions['deactivate'] );
593
594
			return $actions;
595
		}
596
597
		/**
598
		 * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the
599
		 * minimum version requirements.
600
		 *
601
		 * @since 2.5.0
602
		 *
603
		 * @param array $actions Action links.
604
		 * @return array
605
		 */
606
		public function filter_plugin_action_links_update( $actions ) {
607
			$actions['update'] = sprintf(
608
				'<a href="%1$s" title="%2$s" class="edit">%3$s</a>',
609
				esc_url( $this->get_tgmpa_status_url( 'update' ) ),
610
				esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ),
611
				esc_html__( 'Update Required', 'tgmpa' )
612
			);
613
614
			return $actions;
615
		}
616
617
		/**
618
		 * Handles calls to show plugin information via links in the notices.
619
		 *
620
		 * We get the links in the admin notices to point to the TGMPA page, rather
621
		 * than the typical plugin-install.php file, so we can prepare everything
622
		 * beforehand.
623
		 *
624
		 * WP does not make it easy to show the plugin information in the thickbox -
625
		 * here we have to require a file that includes a function that does the
626
		 * main work of displaying it, enqueue some styles, set up some globals and
627
		 * finally call that function before exiting.
628
		 *
629
		 * Down right easy once you know how...
630
		 *
631
		 * Returns early if not the TGMPA page.
632
		 *
633
		 * @since 2.1.0
634
		 *
635
		 * @global string $tab Used as iframe div class names, helps with styling
636
		 * @global string $body_id Used as the iframe body ID, helps with styling
637
		 *
638
		 * @return null Returns early if not the TGMPA page.
639
		 */
640
		public function admin_init() {
641
			if ( ! $this->is_tgmpa_page() ) {
642
				return;
643
			}
644
645
			if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) {
646
				// Needed for install_plugin_information().
647
				require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
648
649
				wp_enqueue_style( 'plugin-install' );
650
651
				global $tab, $body_id;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

    public function myFunction() {
        // Do something
    }
}
Loading history...
652
				$body_id = 'plugin-information';
653
				$tab     = 'plugin-information'; // WPCS: override ok.
654
655
				install_plugin_information();
656
657
				exit;
658
			}
659
		}
660
661
		/**
662
		 * Enqueue thickbox scripts/styles for plugin info.
663
		 *
664
		 * Thickbox is not automatically included on all admin pages, so we must
665
		 * manually enqueue it for those pages.
666
		 *
667
		 * Thickbox is only loaded if the user has not dismissed the admin
668
		 * notice or if there are any plugins left to install and activate.
669
		 *
670
		 * @since 2.1.0
671
		 */
672
		public function thickbox() {
673
			if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
674
				add_thickbox();
675
			}
676
		}
677
678
		/**
679
		 * Adds submenu page if there are plugin actions to take.
680
		 *
681
		 * This method adds the submenu page letting users know that a required
682
		 * plugin needs to be installed.
683
		 *
684
		 * This page disappears once the plugin has been installed and activated.
685
		 *
686
		 * @since 1.0.0
687
		 *
688
		 * @see TGM_Plugin_Activation::init()
689
		 * @see TGM_Plugin_Activation::install_plugins_page()
690
		 *
691
		 * @return null Return early if user lacks capability to install a plugin.
692
		 */
693
		public function admin_menu() {
694
			// Make sure privileges are correct to see the page.
695
			if ( ! current_user_can( 'install_plugins' ) ) {
696
				return;
697
			}
698
699
			$args = apply_filters(
700
				'tgmpa_admin_menu_args',
701
				array(
702
					'parent_slug' => $this->parent_slug,                     // Parent Menu slug.
703
					'page_title'  => $this->strings['page_title'],           // Page title.
704
					'menu_title'  => $this->strings['menu_title'],           // Menu title.
705
					'capability'  => $this->capability,                      // Capability.
706
					'menu_slug'   => $this->menu,                            // Menu slug.
707
					'function'    => array( $this, 'install_plugins_page' ), // Callback.
708
				)
709
			);
710
711
			$this->add_admin_menu( $args );
712
		}
713
714
		/**
715
		 * Add the menu item.
716
		 *
717
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
718
		 * generator on the website.}}
719
		 *
720
		 * @since 2.5.0
721
		 *
722
		 * @param array $args Menu item configuration.
723
		 */
724
		protected function add_admin_menu( array $args ) {
725
			if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) {
726
				_deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) );
727
			}
728
729
			if ( 'themes.php' === $this->parent_slug ) {
730
				$this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
731
			} else {
732
				$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'] );
733
			}
734
		}
735
736
		/**
737
		 * Echoes plugin installation form.
738
		 *
739
		 * This method is the callback for the admin_menu method function.
740
		 * This displays the admin page and form area where the user can select to install and activate the plugin.
741
		 * Aborts early if we're processing a plugin installation action.
742
		 *
743
		 * @since 1.0.0
744
		 *
745
		 * @return null Aborts early if we're processing a plugin installation action.
746
		 */
747
		public function install_plugins_page() {
748
			// Store new instance of plugin table in object.
749
			$plugin_table = new TGMPA_List_Table;
750
751
			// Return early if processing a plugin installation action.
752
			if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
753
				return;
754
			}
755
756
			// Force refresh of available plugin information so we'll know about manual updates/deletes.
757
			wp_clean_plugins_cache( false );
758
759
			?>
760
			<div class="tgmpa wrap">
761
				<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
762
				<?php $plugin_table->prepare_items(); ?>
763
764
				<?php
765
				if ( ! empty( $this->message ) && is_string( $this->message ) ) {
766
					echo wp_kses_post( $this->message );
767
				}
768
				?>
769
				<?php $plugin_table->views(); ?>
770
771
				<form id="tgmpa-plugins" action="" method="post">
772
					<input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
773
					<input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
774
					<?php $plugin_table->display(); ?>
775
				</form>
776
			</div>
777
			<?php
778
		}
779
780
		/**
781
		 * Installs, updates or activates a plugin depending on the action link clicked by the user.
782
		 *
783
		 * Checks the $_GET variable to see which actions have been
784
		 * passed and responds with the appropriate method.
785
		 *
786
		 * Uses WP_Filesystem to process and handle the plugin installation
787
		 * method.
788
		 *
789
		 * @since 1.0.0
790
		 *
791
		 * @uses WP_Filesystem
792
		 * @uses WP_Error
793
		 * @uses WP_Upgrader
794
		 * @uses Plugin_Upgrader
795
		 * @uses Plugin_Installer_Skin
796
		 * @uses Plugin_Upgrader_Skin
797
		 *
798
		 * @return boolean True on success, false on failure.
799
		 */
800
		protected function do_plugin_install() {
801
			if ( empty( $_GET['plugin'] ) ) {
802
				return false;
803
			}
804
805
			// All plugin information will be stored in an array for processing.
806
			$slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
807
808
			if ( ! isset( $this->plugins[ $slug ] ) ) {
809
				return false;
810
			}
811
812
			// Was an install or upgrade action link clicked?
813
			if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
814
815
				$install_type = 'install';
816
				if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
817
					$install_type = 'update';
818
				}
819
820
				check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
821
822
				// Pass necessary information via URL if WP_Filesystem is needed.
823
				$url = wp_nonce_url(
824
					add_query_arg(
825
						array(
826
							'plugin'                 => urlencode( $slug ),
827
							'tgmpa-' . $install_type => $install_type . '-plugin',
828
						),
829
						$this->get_tgmpa_url()
830
					),
831
					'tgmpa-' . $install_type,
832
					'tgmpa-nonce'
833
				);
834
835
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
836
837
				$creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() );
838
				if ( false === $creds ) {
839
					return true;
840
				}
841
842
				if ( ! WP_Filesystem( $creds ) ) {
0 ignored issues
show
Duplication introduced by
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...
843
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
844
					return true;
845
				}
846
847
				/* If we arrive here, we have the filesystem. */
848
849
				// Prep variables for Plugin_Installer_Skin class.
850
				$extra         = array();
851
				$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
852
				$source        = $this->get_download_url( $slug );
853
				$api           = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
854
				$api           = ( false !== $api ) ? $api : null;
855
856
				$url = add_query_arg(
857
					array(
858
						'action' => $install_type . '-plugin',
859
						'plugin' => urlencode( $slug ),
860
					),
861
					'update.php'
862
				);
863
864
				if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
865
					require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
866
				}
867
868
				$title     = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
869
				$skin_args = array(
870
					'type'   => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
871
					'title'  => sprintf( $title, $this->plugins[ $slug ]['name'] ),
872
					'url'    => esc_url_raw( $url ),
873
					'nonce'  => $install_type . '-plugin_' . $slug,
874
					'plugin' => '',
875
					'api'    => $api,
876
					'extra'  => $extra,
877
				);
878
				unset( $title );
879
880
				if ( 'update' === $install_type ) {
881
					$skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
882
					$skin                = new Plugin_Upgrader_Skin( $skin_args );
883
				} else {
884
					$skin = new Plugin_Installer_Skin( $skin_args );
885
				}
886
887
				// Create a new instance of Plugin_Upgrader.
888
				$upgrader = new Plugin_Upgrader( $skin );
889
890
				// Perform the action and install the plugin from the $source urldecode().
891
				add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
892
893
				if ( 'update' === $install_type ) {
894
					// Inject our info into the update transient.
895
					$to_inject                    = array(
896
						$slug => $this->plugins[ $slug ],
897
					);
898
					$to_inject[ $slug ]['source'] = $source;
899
					$this->inject_update_info( $to_inject );
900
901
					$upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
902
				} else {
903
					$upgrader->install( $source );
904
				}
905
906
				remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 );
907
908
				// Make sure we have the correct file path now the plugin is installed/updated.
909
				$this->populate_file_path( $slug );
910
911
				// Only activate plugins if the config option is set to true and the plugin isn't
912
				// already active (upgrade).
913
				if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
914
					$plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
915
					if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
916
						return true; // Finish execution of the function early as we encountered an error.
917
					}
918
				}
919
920
				$this->show_tgmpa_version();
921
922
				// Display message based on if all plugins are now active or not.
923
				if ( $this->is_tgmpa_complete() ) {
924
					echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html( $this->strings['dashboard'] ) . '</a>' ), '</p>';
925
					echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
926
				} else {
927
					echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
928
				}
929
930
				return true;
931
			} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
932
				// Activate action link was clicked.
933
				check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
934
935
				if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
936
					return true; // Finish execution of the function early as we encountered an error.
937
				}
938
			} // End if().
939
940
			return false;
941
		}
942
943
		/**
944
		 * Inject information into the 'update_plugins' site transient as WP checks that before running an update.
945
		 *
946
		 * @since 2.5.0
947
		 *
948
		 * @param array $plugins The plugin information for the plugins which are to be updated.
949
		 */
950
		public function inject_update_info( $plugins ) {
951
			$repo_updates = get_site_transient( 'update_plugins' );
952
953
			if ( ! is_object( $repo_updates ) ) {
954
				$repo_updates = new stdClass;
955
			}
956
957
			foreach ( $plugins as $slug => $plugin ) {
958
				$file_path = $plugin['file_path'];
959
960
				if ( empty( $repo_updates->response[ $file_path ] ) ) {
961
					$repo_updates->response[ $file_path ] = new stdClass;
962
				}
963
964
				// We only really need to set package, but let's do all we can in case WP changes something.
965
				$repo_updates->response[ $file_path ]->slug        = $slug;
966
				$repo_updates->response[ $file_path ]->plugin      = $file_path;
967
				$repo_updates->response[ $file_path ]->new_version = $plugin['version'];
968
				$repo_updates->response[ $file_path ]->package     = $plugin['source'];
969
				if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
970
					$repo_updates->response[ $file_path ]->url = $plugin['external_url'];
971
				}
972
			}
973
974
			set_site_transient( 'update_plugins', $repo_updates );
975
		}
976
977
		/**
978
		 * Adjust the plugin directory name if necessary.
979
		 *
980
		 * The final destination directory of a plugin is based on the subdirectory name found in the
981
		 * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
982
		 * subdirectory name is not the same as the expected slug and the plugin will not be recognized
983
		 * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
984
		 * the expected plugin slug.
985
		 *
986
		 * @since 2.5.0
987
		 *
988
		 * @param string       $source        Path to upgrade/zip-file-name.tmp/subdirectory/.
989
		 * @param string       $remote_source Path to upgrade/zip-file-name.tmp.
990
		 * @param \WP_Upgrader $upgrader      Instance of the upgrader which installs the plugin.
991
		 * @return string $source
992
		 */
993
		public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
994
			if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
995
				return $source;
996
			}
997
998
			// Check for single file plugins.
999
			$source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
1000
			if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
1001
				return $source;
1002
			}
1003
1004
			// Multi-file plugin, let's see if the directory is correctly named.
1005
			$desired_slug = '';
1006
1007
			// Figure out what the slug is supposed to be.
1008
			if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
1009
				$desired_slug = $upgrader->skin->options['extra']['slug'];
1010
			} else {
1011
				// Bulk installer contains less info, so fall back on the info registered here.
1012
				foreach ( $this->plugins as $slug => $plugin ) {
1013
					if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
1014
						$desired_slug = $slug;
1015
						break;
1016
					}
1017
				}
1018
				unset( $slug, $plugin );
1019
			}
1020
1021
			if ( ! empty( $desired_slug ) ) {
1022
				$subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
1023
1024
				if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
1025
					$from_path = untrailingslashit( $source );
1026
					$to_path   = trailingslashit( $remote_source ) . $desired_slug;
1027
1028
					if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
1029
						return trailingslashit( $to_path );
1030 View Code Duplication
					} else {
1031
						return new WP_Error(
1032
							'rename_failed',
1033
							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' ),
1034
							array(
1035
								'found'    => $subdir_name,
1036
								'expected' => $desired_slug,
1037
							)
1038
						);
1039
					}
1040 View Code Duplication
				} elseif ( empty( $subdir_name ) ) {
1041
					return new WP_Error(
1042
						'packaged_wrong',
1043
						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' ),
1044
						array(
1045
							'found'    => $subdir_name,
1046
							'expected' => $desired_slug,
1047
						)
1048
					);
1049
				}
1050
			}
1051
1052
			return $source;
1053
		}
1054
1055
		/**
1056
		 * Activate a single plugin and send feedback about the result to the screen.
1057
		 *
1058
		 * @since 2.5.0
1059
		 *
1060
		 * @param string $file_path Path within wp-plugins/ to main plugin file.
1061
		 * @param string $slug      Plugin slug.
1062
		 * @param bool   $automatic Whether this is an automatic activation after an install. Defaults to false.
1063
		 *                          This determines the styling of the output messages.
1064
		 * @return bool False if an error was encountered, true otherwise.
1065
		 */
1066
		protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
1067
			if ( $this->can_plugin_activate( $slug ) ) {
1068
				$activate = activate_plugin( $file_path );
1069
1070
				if ( is_wp_error( $activate ) ) {
1071
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
1072
						'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
1073
1074
					return false; // End it here if there is an error with activation.
1075
				} else {
1076
					if ( ! $automatic ) {
1077
						// Make sure message doesn't display again if bulk activation is performed
1078
						// immediately after a single activation.
1079
						if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1080
							echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
1081
						}
1082
					} else {
1083
						// Simpler message layout for use on the plugin install page.
1084
						echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
1085
					}
1086
				}
1087 View Code Duplication
			} elseif ( $this->is_plugin_active( $slug ) ) {
1088
				// No simpler message format provided as this message should never be encountered
1089
				// on the plugin install page.
1090
				echo '<div id="message" class="error"><p>',
1091
					sprintf(
1092
						esc_html( $this->strings['plugin_already_active'] ),
1093
						'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1094
					),
1095
					'</p></div>';
1096
			} elseif ( $this->does_plugin_require_update( $slug ) ) {
1097
				if ( ! $automatic ) {
1098
					// Make sure message doesn't display again if bulk activation is performed
1099
					// immediately after a single activation.
1100 View Code Duplication
					if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1101
						echo '<div id="message" class="error"><p>',
1102
							sprintf(
1103
								esc_html( $this->strings['plugin_needs_higher_version'] ),
1104
								'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1105
							),
1106
							'</p></div>';
1107
					}
1108
				} else {
1109
					// Simpler message layout for use on the plugin install page.
1110
					echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
1111
				}
1112
			} // End if().
1113
1114
			return true;
1115
		}
1116
1117
		/**
1118
		 * Echoes required plugin notice.
1119
		 *
1120
		 * Outputs a message telling users that a specific plugin is required for
1121
		 * their theme. If appropriate, it includes a link to the form page where
1122
		 * users can install and activate the plugin.
1123
		 *
1124
		 * Returns early if we're on the Install page.
1125
		 *
1126
		 * @since 1.0.0
1127
		 *
1128
		 * @global object $current_screen
1129
		 *
1130
		 * @return null Returns early if we're on the Install page.
1131
		 */
1132
		public function notices() {
1133
			// Remove nag on the install page / Return early if the nag message has been dismissed or user < author.
1134
			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' ) ) ) {
1135
				return;
1136
			}
1137
1138
			// Store for the plugin slugs by message type.
1139
			$message = array();
1140
1141
			// Initialize counters used to determine plurality of action link texts.
1142
			$install_link_count          = 0;
1143
			$update_link_count           = 0;
1144
			$activate_link_count         = 0;
1145
			$total_required_action_count = 0;
1146
1147
			foreach ( $this->plugins as $slug => $plugin ) {
1148
				if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) {
1149
					continue;
1150
				}
1151
1152
				if ( ! $this->is_plugin_installed( $slug ) ) {
1153
					if ( current_user_can( 'install_plugins' ) ) {
1154
						$install_link_count++;
1155
1156
						if ( true === $plugin['required'] ) {
1157
							$message['notice_can_install_required'][] = $slug;
1158
						} else {
1159
							$message['notice_can_install_recommended'][] = $slug;
1160
						}
1161
					}
1162
					if ( true === $plugin['required'] ) {
1163
						$total_required_action_count++;
1164
					}
1165
				} else {
1166
					if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
1167
						if ( current_user_can( 'activate_plugins' ) ) {
1168
							$activate_link_count++;
1169
1170
							if ( true === $plugin['required'] ) {
1171
								$message['notice_can_activate_required'][] = $slug;
1172
							} else {
1173
								$message['notice_can_activate_recommended'][] = $slug;
1174
							}
1175
						}
1176
						if ( true === $plugin['required'] ) {
1177
							$total_required_action_count++;
1178
						}
1179
					}
1180
1181
					if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1182
1183
						if ( current_user_can( 'update_plugins' ) ) {
1184
							$update_link_count++;
1185
1186
							if ( $this->does_plugin_require_update( $slug ) ) {
1187
								$message['notice_ask_to_update'][] = $slug;
1188
							} elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
1189
								$message['notice_ask_to_update_maybe'][] = $slug;
1190
							}
1191
						}
1192
						if ( true === $plugin['required'] ) {
1193
							$total_required_action_count++;
1194
						}
1195
					}
1196
				} // End if().
1197
			} // End foreach().
1198
			unset( $slug, $plugin );
1199
1200
			// If we have notices to display, we move forward.
1201
			if ( ! empty( $message ) || $total_required_action_count > 0 ) {
1202
				krsort( $message ); // Sort messages.
1203
				$rendered = '';
1204
1205
				// As add_settings_error() wraps the final message in a <p> and as the final message can't be
1206
				// filtered, using <p>'s in our html would render invalid html output.
1207
				$line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
1208
1209
				if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
1210
					$rendered  = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] );
1211
					$rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
1212
				} else {
1213
1214
					// If dismissable is false and a message is set, output it now.
1215
					if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
1216
						$rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
1217
					}
1218
1219
					// Render the individual message lines for the notice.
1220
					foreach ( $message as $type => $plugin_group ) {
1221
						$linked_plugins = array();
1222
1223
						// Get the external info link for a plugin if one is available.
1224
						foreach ( $plugin_group as $plugin_slug ) {
1225
							$linked_plugins[] = $this->get_info_link( $plugin_slug );
1226
						}
1227
						unset( $plugin_slug );
1228
1229
						$count          = count( $plugin_group );
1230
						$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
1231
						$last_plugin    = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
1232
						$imploded       = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
1233
1234
						$rendered .= sprintf(
1235
							$line_template,
1236
							sprintf(
1237
								translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
1238
								$imploded,
1239
								$count
1240
							)
1241
						);
1242
1243
					}
1244
					unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
1245
1246
					$rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
1247
				} // End if().
1248
1249
				// Register the nag messages and prepare them to be processed.
1250
				add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
1251
			} // End if().
1252
1253
			// Admin options pages already output settings_errors, so this is to avoid duplication.
1254
			if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
1255
				$this->display_settings_errors();
1256
			}
1257
		}
1258
1259
		/**
1260
		 * Generate the user action links for the admin notice.
1261
		 *
1262
		 * @since 2.6.0
1263
		 *
1264
		 * @param int $install_count  Number of plugins to install.
1265
		 * @param int $update_count   Number of plugins to update.
1266
		 * @param int $activate_count Number of plugins to activate.
1267
		 * @param int $line_template  Template for the HTML tag to output a line.
1268
		 * @return string Action links.
1269
		 */
1270
		protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
1271
			// Setup action links.
1272
			$action_links = array(
1273
				'install'  => '',
1274
				'update'   => '',
1275
				'activate' => '',
1276
				'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>' : '',
1277
			);
1278
1279
			$link_template = '<a href="%2$s">%1$s</a>';
1280
1281
			if ( current_user_can( 'install_plugins' ) ) {
1282
				if ( $install_count > 0 ) {
1283
					$action_links['install'] = sprintf(
1284
						$link_template,
1285
						translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),
1286
						esc_url( $this->get_tgmpa_status_url( 'install' ) )
1287
					);
1288
				}
1289
				if ( $update_count > 0 ) {
1290
					$action_links['update'] = sprintf(
1291
						$link_template,
1292
						translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),
1293
						esc_url( $this->get_tgmpa_status_url( 'update' ) )
1294
					);
1295
				}
1296
			}
1297
1298
			if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
0 ignored issues
show
Duplication introduced by
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...
1299
				$action_links['activate'] = sprintf(
1300
					$link_template,
1301
					translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),
1302
					esc_url( $this->get_tgmpa_status_url( 'activate' ) )
1303
				);
1304
			}
1305
1306
			$action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
1307
1308
			$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
1309
1310
			if ( ! empty( $action_links ) ) {
1311
				$action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
1312
				return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
1313
			} else {
1314
				return '';
1315
			}
1316
		}
1317
1318
		/**
1319
		 * Get admin notice class.
1320
		 *
1321
		 * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1322
		 * (lowest supported version by TGMPA).
1323
		 *
1324
		 * @since 2.6.0
1325
		 *
1326
		 * @return string
1327
		 */
1328
		protected function get_admin_notice_class() {
1329
			if ( ! empty( $this->strings['nag_type'] ) ) {
1330
				return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1331
			} else {
1332
				if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1333
					return 'notice-warning';
1334
				} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1335
					return 'notice';
1336
				} else {
1337
					return 'updated';
1338
				}
1339
			}
1340
		}
1341
1342
		/**
1343
		 * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
1344
		 *
1345
		 * @since 2.5.0
1346
		 */
1347
		protected function display_settings_errors() {
1348
			global $wp_settings_errors;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

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