Completed
Pull Request — develop (#593)
by
unknown
02:22
created

TGM_Plugin_Activation::notices()   F

Complexity

Conditions 34
Paths 569

Size

Total Lines 126
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 4 Features 0
Metric Value
cc 34
eloc 70
c 6
b 4
f 0
nc 569
nop 0
dl 0
loc 126
rs 2.3116

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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