Completed
Push — develop ( 5dbb01...0193ea )
by Gary
03:21 queued 55s
created

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

Upgrade to new PHP Analysis Engine

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

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

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

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

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