Completed
Push — develop ( a91014...a6a0b3 )
by Gary
17:56 queued 11:08
created

TGM_Plugin_Activation::get_plugins_api()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 5
nop 1
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
				'oops'                            => __( 'Something went wrong with the plugin API.', 'tgmpa' ),
326
				'notice_can_install_required'     => _n_noop(
327
					'This theme requires the following plugin: %1$s.',
328
					'This theme requires the following plugins: %1$s.',
329
					'tgmpa'
330
				),
331
				'notice_can_install_recommended'  => _n_noop(
332
					'This theme recommends the following plugin: %1$s.',
333
					'This theme recommends the following plugins: %1$s.',
334
					'tgmpa'
335
				),
336
				'notice_cannot_install'           => _n_noop(
337
					'Sorry, but you do not have the correct permissions to install the %1$s plugin.',
338
					'Sorry, but you do not have the correct permissions to install the %1$s plugins.',
339
					'tgmpa'
340
				),
341
				'notice_ask_to_update'            => _n_noop(
342
					'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',
343
					'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',
344
					'tgmpa'
345
				),
346
				'notice_ask_to_update_maybe'      => _n_noop(
347
					'There is an update available for: %1$s.',
348
					'There are updates available for the following plugins: %1$s.',
349
					'tgmpa'
350
				),
351
				'notice_cannot_update'            => _n_noop(
352
					'Sorry, but you do not have the correct permissions to update the %1$s plugin.',
353
					'Sorry, but you do not have the correct permissions to update the %1$s plugins.',
354
					'tgmpa'
355
				),
356
				'notice_can_activate_required'    => _n_noop(
357
					'The following required plugin is currently inactive: %1$s.',
358
					'The following required plugins are currently inactive: %1$s.',
359
					'tgmpa'
360
				),
361
				'notice_can_activate_recommended' => _n_noop(
362
					'The following recommended plugin is currently inactive: %1$s.',
363
					'The following recommended plugins are currently inactive: %1$s.',
364
					'tgmpa'
365
				),
366
				'notice_cannot_activate'          => _n_noop(
367
					'Sorry, but you do not have the correct permissions to activate the %1$s plugin.',
368
					'Sorry, but you do not have the correct permissions to activate the %1$s plugins.',
369
					'tgmpa'
370
				),
371
				'install_link'                    => _n_noop(
372
					'Begin installing plugin',
373
					'Begin installing plugins',
374
					'tgmpa'
375
				),
376
				'update_link'                     => _n_noop(
377
					'Begin updating plugin',
378
					'Begin updating plugins',
379
					'tgmpa'
380
				),
381
				'activate_link'                   => _n_noop(
382
					'Begin activating plugin',
383
					'Begin activating plugins',
384
					'tgmpa'
385
				),
386
				'return'                          => __( 'Return to Required Plugins Installer', 'tgmpa' ),
387
				'dashboard'                       => __( 'Return to the dashboard', 'tgmpa' ),
388
				'plugin_activated'                => __( 'Plugin activated successfully.', 'tgmpa' ),
389
				'activated_successfully'          => __( 'The following plugin was activated successfully:', 'tgmpa' ),
390
				'plugin_already_active'           => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ),
391
				'plugin_needs_higher_version'     => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ),
392
				'complete'                        => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ),
393
				'dismiss'                         => __( 'Dismiss this notice', 'tgmpa' ),
394
				'contact_admin'                   => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),
395
			);
396
397
			do_action( 'tgmpa_register' );
398
399
			/* After this point, the plugins should be registered and the configuration set. */
400
401
			// Proceed only if we have plugins to handle.
402
			if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
403
				return;
404
			}
405
406
			// Set up the menu and notices if we still have outstanding actions.
407
			if ( true !== $this->is_tgmpa_complete() ) {
408
				// Sort the plugins.
409
				array_multisort( $this->sort_order, SORT_ASC, $this->plugins );
410
411
				add_action( 'admin_menu', array( $this, 'admin_menu' ) );
412
				add_action( 'admin_head', array( $this, 'dismiss' ) );
413
414
				// Prevent the normal links from showing underneath a single install/update page.
415
				add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
416
				add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) );
417
418
				if ( $this->has_notices ) {
419
					add_action( 'admin_notices', array( $this, 'notices' ) );
420
					add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
421
					add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
422
				}
423
			}
424
425
			// If needed, filter plugin action links.
426
			add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 );
427
428
			// Make sure things get reset on switch theme.
429
			add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
430
431
			if ( $this->has_notices ) {
432
				add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
433
			}
434
435
			// Setup the force activation hook.
436
			if ( true === $this->has_forced_activation ) {
437
				add_action( 'admin_init', array( $this, 'force_activation' ) );
438
			}
439
440
			// Setup the force deactivation hook.
441
			if ( true === $this->has_forced_deactivation ) {
442
				add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
443
			}
444
		}
445
446
		/**
447
		 * Hook in plugin action link filters for the WP native plugins page.
448
		 *
449
		 * - Prevent activation of plugins which don't meet the minimum version requirements.
450
		 * - Prevent deactivation of force-activated plugins.
451
		 * - Add update notice if update available.
452
		 *
453
		 * @since 2.5.0
454
		 */
455
		public function add_plugin_action_link_filters() {
456
			foreach ( $this->plugins as $slug => $plugin ) {
457
				if ( false === $this->can_plugin_activate( $slug ) ) {
458
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 );
459
				}
460
461
				if ( true === $plugin['force_activation'] ) {
462
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 );
463
				}
464
465
				if ( false !== $this->does_plugin_require_update( $slug ) ) {
466
					add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 );
467
				}
468
			}
469
		}
470
471
		/**
472
		 * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the
473
		 * minimum version requirements.
474
		 *
475
		 * @since 2.5.0
476
		 *
477
		 * @param array $actions Action links.
478
		 * @return array
479
		 */
480
		public function filter_plugin_action_links_activate( $actions ) {
481
			unset( $actions['activate'] );
482
483
			return $actions;
484
		}
485
486
		/**
487
		 * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate.
488
		 *
489
		 * @since 2.5.0
490
		 *
491
		 * @param array $actions Action links.
492
		 * @return array
493
		 */
494
		public function filter_plugin_action_links_deactivate( $actions ) {
495
			unset( $actions['deactivate'] );
496
497
			return $actions;
498
		}
499
500
		/**
501
		 * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the
502
		 * minimum version requirements.
503
		 *
504
		 * @since 2.5.0
505
		 *
506
		 * @param array $actions Action links.
507
		 * @return array
508
		 */
509
		public function filter_plugin_action_links_update( $actions ) {
510
			$actions['update'] = sprintf(
511
				'<a href="%1$s" title="%2$s" class="edit">%3$s</a>',
512
				esc_url( $this->get_tgmpa_status_url( 'update' ) ),
513
				esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ),
514
				esc_html__( 'Update Required', 'tgmpa' )
515
			);
516
517
			return $actions;
518
		}
519
520
		/**
521
		 * Handles calls to show plugin information via links in the notices.
522
		 *
523
		 * We get the links in the admin notices to point to the TGMPA page, rather
524
		 * than the typical plugin-install.php file, so we can prepare everything
525
		 * beforehand.
526
		 *
527
		 * WP does not make it easy to show the plugin information in the thickbox -
528
		 * here we have to require a file that includes a function that does the
529
		 * main work of displaying it, enqueue some styles, set up some globals and
530
		 * finally call that function before exiting.
531
		 *
532
		 * Down right easy once you know how...
533
		 *
534
		 * Returns early if not the TGMPA page.
535
		 *
536
		 * @since 2.1.0
537
		 *
538
		 * @global string $tab Used as iframe div class names, helps with styling
539
		 * @global string $body_id Used as the iframe body ID, helps with styling
540
		 *
541
		 * @return null Returns early if not the TGMPA page.
542
		 */
543
		public function admin_init() {
544
			if ( ! $this->is_tgmpa_page() ) {
545
				return;
546
			}
547
548
			if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) {
549
				// Needed for install_plugin_information().
550
				require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
551
552
				wp_enqueue_style( 'plugin-install' );
553
554
				global $tab, $body_id;
555
				$body_id = 'plugin-information';
556
				// @codingStandardsIgnoreStart
557
				$tab     = 'plugin-information';
558
				// @codingStandardsIgnoreEnd
559
560
				install_plugin_information();
561
562
				exit;
563
			}
564
		}
565
566
		/**
567
		 * Enqueue thickbox scripts/styles for plugin info.
568
		 *
569
		 * Thickbox is not automatically included on all admin pages, so we must
570
		 * manually enqueue it for those pages.
571
		 *
572
		 * Thickbox is only loaded if the user has not dismissed the admin
573
		 * notice or if there are any plugins left to install and activate.
574
		 *
575
		 * @since 2.1.0
576
		 */
577
		public function thickbox() {
578
			if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
579
				add_thickbox();
580
			}
581
		}
582
583
		/**
584
		 * Adds submenu page if there are plugin actions to take.
585
		 *
586
		 * This method adds the submenu page letting users know that a required
587
		 * plugin needs to be installed.
588
		 *
589
		 * This page disappears once the plugin has been installed and activated.
590
		 *
591
		 * @since 1.0.0
592
		 *
593
		 * @see TGM_Plugin_Activation::init()
594
		 * @see TGM_Plugin_Activation::install_plugins_page()
595
		 *
596
		 * @return null Return early if user lacks capability to install a plugin.
597
		 */
598
		public function admin_menu() {
599
			// Make sure privileges are correct to see the page.
600
			if ( ! current_user_can( 'install_plugins' ) ) {
601
				return;
602
			}
603
604
			$args = apply_filters(
605
				'tgmpa_admin_menu_args',
606
				array(
607
					'parent_slug' => $this->parent_slug,                     // Parent Menu slug.
608
					'page_title'  => $this->strings['page_title'],           // Page title.
609
					'menu_title'  => $this->strings['menu_title'],           // Menu title.
610
					'capability'  => $this->capability,                      // Capability.
611
					'menu_slug'   => $this->menu,                            // Menu slug.
612
					'function'    => array( $this, 'install_plugins_page' ), // Callback.
613
				)
614
			);
615
616
			$this->add_admin_menu( $args );
617
		}
618
619
		/**
620
		 * Add the menu item.
621
		 *
622
		 * @since 2.5.0
623
		 *
624
		 * @param array $args Menu item configuration.
625
		 */
626
		protected function add_admin_menu( array $args ) {
627
			if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) {
628
				_deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) );
629
			}
630
631
			if ( 'themes.php' === $this->parent_slug ) {
632
				$this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
633
			} else {
634
				$type = 'submenu';
635
				$this->page_hook = call_user_func( "add_{$type}_page", $args['parent_slug'], $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
636
			}
637
		}
638
639
		/**
640
		 * Echoes plugin installation form.
641
		 *
642
		 * This method is the callback for the admin_menu method function.
643
		 * This displays the admin page and form area where the user can select to install and activate the plugin.
644
		 * Aborts early if we're processing a plugin installation action.
645
		 *
646
		 * @since 1.0.0
647
		 *
648
		 * @return null Aborts early if we're processing a plugin installation action.
649
		 */
650
		public function install_plugins_page() {
651
			// Store new instance of plugin table in object.
652
			$plugin_table = new TGMPA_List_Table;
653
654
			// Return early if processing a plugin installation action.
655
			if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
656
				return;
657
			}
658
659
			// Force refresh of available plugin information so we'll know about manual updates/deletes.
660
			wp_clean_plugins_cache( false );
661
662
			?>
663
			<div class="tgmpa wrap">
664
				<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
665
				<?php $plugin_table->prepare_items(); ?>
666
667
				<?php
668
				if ( ! empty( $this->message ) && is_string( $this->message ) ) {
669
					echo wp_kses_post( $this->message );
670
				}
671
				?>
672
				<?php $plugin_table->views(); ?>
673
674
				<form id="tgmpa-plugins" action="" method="post">
675
					<input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
676
					<input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
677
					<?php $plugin_table->display(); ?>
678
				</form>
679
			</div>
680
			<?php
681
		}
682
683
		/**
684
		 * Installs, updates or activates a plugin depending on the action link clicked by the user.
685
		 *
686
		 * Checks the $_GET variable to see which actions have been
687
		 * passed and responds with the appropriate method.
688
		 *
689
		 * Uses WP_Filesystem to process and handle the plugin installation
690
		 * method.
691
		 *
692
		 * @since 1.0.0
693
		 *
694
		 * @uses WP_Filesystem
695
		 * @uses WP_Error
696
		 * @uses WP_Upgrader
697
		 * @uses Plugin_Upgrader
698
		 * @uses Plugin_Installer_Skin
699
		 * @uses Plugin_Upgrader_Skin
700
		 *
701
		 * @return boolean True on success, false on failure.
702
		 */
703
		protected function do_plugin_install() {
704
			if ( empty( $_GET['plugin'] ) ) {
705
				return false;
706
			}
707
708
			// All plugin information will be stored in an array for processing.
709
			$slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
710
711
			if ( ! isset( $this->plugins[ $slug ] ) ) {
712
				return false;
713
			}
714
715
			// Was an install or upgrade action link clicked?
716
			if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
717
718
				$install_type = 'install';
719
				if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
720
					$install_type = 'update';
721
				}
722
723
				check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
724
725
				// Pass necessary information via URL if WP_Filesystem is needed.
726
				$url = wp_nonce_url(
727
					add_query_arg(
728
						array(
729
							'plugin'                 => urlencode( $slug ),
730
							'tgmpa-' . $install_type => $install_type . '-plugin',
731
						),
732
						$this->get_tgmpa_url()
733
					),
734
					'tgmpa-' . $install_type,
735
					'tgmpa-nonce'
736
				);
737
738
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
739
740
				if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) {
741
					return true;
742
				}
743
744 View Code Duplication
				if ( ! WP_Filesystem( $creds ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

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

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

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

Loading history...
2727
					// Our credentials were no good, ask the user for them again.
2728
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
2729
2730
					return true;
2731
				}
2732
2733
				/* If we arrive here, we have the filesystem */
2734
2735
				// Store all information in arrays since we are processing a bulk installation.
2736
				$names      = array();
2737
				$sources    = array(); // Needed for installs.
2738
				$file_paths = array(); // Needed for upgrades.
2739
				$to_inject  = array(); // Information to inject into the update_plugins transient.
2740
2741
				// Prepare the data for validated plugins for the install/upgrade.
2742
				foreach ( $plugins_to_install as $slug ) {
2743
					$name   = $this->tgmpa->plugins[ $slug ]['name'];
2744
					$source = $this->tgmpa->get_download_url( $slug );
2745
2746
					if ( ! empty( $name ) && ! empty( $source ) ) {
2747
						$names[] = $name;
2748
2749
						switch ( $install_type ) {
2750
2751
							case 'install':
2752
								$sources[] = $source;
2753
								break;
2754
2755
							case 'update':
2756
								$file_paths[]                 = $this->tgmpa->plugins[ $slug ]['file_path'];
2757
								$to_inject[ $slug ]           = $this->tgmpa->plugins[ $slug ];
2758
								$to_inject[ $slug ]['source'] = $source;
2759
								break;
2760
						}
2761
					}
2762
				}
2763
				unset( $slug, $name, $source );
2764
2765
				// Create a new instance of TGMPA_Bulk_Installer.
2766
				$installer = new TGMPA_Bulk_Installer(
2767
					new TGMPA_Bulk_Installer_Skin(
2768
						array(
2769
							'url'          => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),
2770
							'nonce'        => 'bulk-' . $this->_args['plural'],
2771
							'names'        => $names,
2772
							'install_type' => $install_type,
2773
						)
2774
					)
2775
				);
2776
2777
				// Wrap the install process with the appropriate HTML.
2778
				echo '<div class="tgmpa wrap">',
2779
					'<h2>', esc_html( get_admin_page_title() ), '</h2>';
2780
2781
				// Process the bulk installation submissions.
2782
				add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
2783
2784
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2785
					// Inject our info into the update transient.
2786
					$this->tgmpa->inject_update_info( $to_inject );
2787
2788
					$installer->bulk_upgrade( $file_paths );
2789
				} else {
2790
					$installer->bulk_install( $sources );
2791
				}
2792
2793
				remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
2794
2795
				echo '</div>';
2796
2797
				return true;
2798
			}
2799
2800
			// Bulk activation process.
2801
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
2802
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
2803
2804
				// Did user actually select any plugins to activate ?
2805
				if ( empty( $_POST['plugin'] ) ) {
2806
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>';
2807
2808
					return false;
2809
				}
2810
2811
				// Grab plugin data from $_POST.
2812
				$plugins = array();
2813
				if ( isset( $_POST['plugin'] ) ) {
2814
					$plugins = array_map( 'urldecode', (array) $_POST['plugin'] );
2815
					$plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins );
2816
				}
2817
2818
				$plugins_to_activate = array();
2819
				$plugin_names        = array();
2820
2821
				// Grab the file paths for the selected & inactive plugins from the registration array.
2822
				foreach ( $plugins as $slug ) {
2823
					if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2824
						$plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path'];
2825
						$plugin_names[]        = $this->tgmpa->plugins[ $slug ]['name'];
2826
					}
2827
				}
2828
				unset( $slug );
2829
2830
				// Return early if there are no plugins to activate.
2831
				if ( empty( $plugins_to_activate ) ) {
2832
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>';
2833
2834
					return false;
2835
				}
2836
2837
				// Now we are good to go - let's start activating plugins.
2838
				$activate = activate_plugins( $plugins_to_activate );
2839
2840
				if ( is_wp_error( $activate ) ) {
2841
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
2842
				} else {
2843
					$count        = count( $plugin_names ); // Count so we can use _n function.
2844
					$plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names );
2845
					$last_plugin  = array_pop( $plugin_names ); // Pop off last name to prep for readability.
2846
					$imploded     = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
2847
2848
					printf( // WPCS: xss ok.
2849
						'<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
2850
						esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),
2851
						$imploded
2852
					);
2853
2854
					// Update recently activated plugins option.
2855
					$recent = (array) get_option( 'recently_activated' );
2856
					foreach ( $plugins_to_activate as $plugin => $time ) {
2857
						if ( isset( $recent[ $plugin ] ) ) {
2858
							unset( $recent[ $plugin ] );
2859
						}
2860
					}
2861
					update_option( 'recently_activated', $recent );
2862
				}
2863
2864
				unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another.
2865
2866
				return true;
2867
			}
2868
2869
			return false;
2870
		}
2871
2872
		/**
2873
		 * Prepares all of our information to be outputted into a usable table.
2874
		 *
2875
		 * @since 2.2.0
2876
		 */
2877
		public function prepare_items() {
2878
			$columns               = $this->get_columns(); // Get all necessary column information.
2879
			$hidden                = array(); // No columns to hide, but we must set as an array.
2880
			$sortable              = array(); // No reason to make sortable columns.
2881
			$primary               = $this->get_primary_column_name(); // Column which has the row actions.
2882
			$this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers.
2883
2884
			// Process our bulk activations here.
2885
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
2886
				$this->process_bulk_actions();
2887
			}
2888
2889
			// Store all of our plugin data into $items array so WP_List_Table can use it.
2890
			$this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() );
2891
		}
2892
2893
		/* *********** DEPRECATED METHODS *********** */
2894
2895
		/**
2896
		 * Retrieve plugin data, given the plugin name.
2897
		 *
2898
		 * @since      2.2.0
2899
		 * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead.
2900
		 * @see        TGM_Plugin_Activation::_get_plugin_data_from_name()
2901
		 *
2902
		 * @param string $name Name of the plugin, as it was registered.
2903
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
2904
		 * @return string|boolean Plugin slug if found, false otherwise.
2905
		 */
2906
		protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
2907
			_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' );
2908
2909
			return $this->tgmpa->_get_plugin_data_from_name( $name, $data );
2910
		}
2911
	}
2912
}
2913
2914
2915
if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
2916
2917
	/**
2918
	 * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+.
2919
	 */
2920
	class TGM_Bulk_Installer {
2921
	}
2922
}
2923
if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
2924
2925
	/**
2926
	 * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+.
2927
	 */
2928
	class TGM_Bulk_Installer_Skin {
2929
	}
2930
}
2931
2932
/**
2933
 * The WP_Upgrader file isn't always available. If it isn't available,
2934
 * we load it here.
2935
 *
2936
 * We check to make sure no action or activation keys are set so that WordPress
2937
 * does not try to re-include the class when processing upgrades or installs outside
2938
 * of the class.
2939
 *
2940
 * @since 2.2.0
2941
 */
2942
add_action( 'admin_init', 'tgmpa_load_bulk_installer' );
2943
if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
2944
	/**
2945
	 * Load bulk installer
2946
	 */
2947
	function tgmpa_load_bulk_installer() {
2948
		// Silently fail if 2.5+ is loaded *after* an older version.
2949
		if ( ! isset( $GLOBALS['tgmpa'] ) ) {
2950
			return;
2951
		}
2952
2953
		// Get TGMPA class instance.
2954
		$tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2955
2956
		if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) {
2957
			if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
2958
				require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
2959
			}
2960
2961
			if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) {
2962
2963
				/**
2964
				 * Installer class to handle bulk plugin installations.
2965
				 *
2966
				 * Extends WP_Upgrader and customizes to suit the installation of multiple
2967
				 * plugins.
2968
				 *
2969
				 * @since 2.2.0
2970
				 *
2971
				 * @internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader
2972
				 * @internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
2973
				 *           This was done to prevent backward compatibility issues with v2.3.6.
2974
				 *
2975
				 * @package TGM-Plugin-Activation
2976
				 * @author  Thomas Griffin
2977
				 * @author  Gary Jones
2978
				 */
2979
				class TGMPA_Bulk_Installer extends Plugin_Upgrader {
2980
					/**
2981
					 * Holds result of bulk plugin installation.
2982
					 *
2983
					 * @since 2.2.0
2984
					 *
2985
					 * @var string
2986
					 */
2987
					public $result;
2988
2989
					/**
2990
					 * Flag to check if bulk installation is occurring or not.
2991
					 *
2992
					 * @since 2.2.0
2993
					 *
2994
					 * @var boolean
2995
					 */
2996
					public $bulk = false;
2997
2998
					/**
2999
					 * TGMPA instance
3000
					 *
3001
					 * @since 2.5.0
3002
					 *
3003
					 * @var object
3004
					 */
3005
					protected $tgmpa;
3006
3007
					/**
3008
					 * Whether or not the destination directory needs to be cleared ( = on update).
3009
					 *
3010
					 * @since 2.5.0
3011
					 *
3012
					 * @var bool
3013
					 */
3014
					protected $clear_destination = false;
3015
3016
					/**
3017
					 * References parent constructor and sets defaults for class.
3018
					 *
3019
					 * @since 2.2.0
3020
					 *
3021
					 * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
3022
					 */
3023
					public function __construct( $skin = null ) {
3024
						// Get TGMPA class instance.
3025
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3026
3027
						parent::__construct( $skin );
3028
3029
						if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
3030
							$this->clear_destination = true;
3031
						}
3032
3033
						if ( $this->tgmpa->is_automatic ) {
3034
							$this->activate_strings();
3035
						}
3036
3037
						add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
3038
					}
3039
3040
					/**
3041
					 * Sets the correct activation strings for the installer skin to use.
3042
					 *
3043
					 * @since 2.2.0
3044
					 */
3045
					public function activate_strings() {
3046
						$this->strings['activation_failed']  = __( 'Plugin activation failed.', 'tgmpa' );
3047
						$this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' );
3048
					}
3049
3050
					/**
3051
					 * Performs the actual installation of each plugin.
3052
					 *
3053
					 * @since 2.2.0
3054
					 *
3055
					 * @see WP_Upgrader::run()
3056
					 *
3057
					 * @param array $options The installation config options.
3058
					 * @return null|array Return early if error, array of installation data on success.
3059
					 */
3060
					public function run( $options ) {
3061
						$result = parent::run( $options );
3062
3063
						// Reset the strings in case we changed one during automatic activation.
3064
						if ( $this->tgmpa->is_automatic ) {
3065
							if ( 'update' === $this->skin->options['install_type'] ) {
3066
								$this->upgrade_strings();
3067
							} else {
3068
								$this->install_strings();
3069
							}
3070
						}
3071
3072
						return $result;
3073
					}
3074
3075
					/**
3076
					 * Processes the bulk installation of plugins.
3077
					 *
3078
					 * @since 2.2.0
3079
					 *
3080
					 * @internal This is basically a near identical copy of the WP Core Plugin_Upgrader::bulk_upgrade()
3081
					 * method, with minor adjustments to deal with new installs instead of upgrades.
3082
					 * For ease of future synchronizations, the adjustments are clearly commented, but no other
3083
					 * comments are added. Code style has been made to comply.
3084
					 *
3085
					 * @see Plugin_Upgrader::bulk_upgrade()
3086
					 * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
3087
					 *
3088
					 * @param array $plugins The plugin sources needed for installation.
3089
					 * @param array $args    Arbitrary passed extra arguments.
3090
					 * @return string|bool Install confirmation messages on success, false on failure.
3091
					 */
3092
					public function bulk_install( $plugins, $args = array() ) {
3093
						// [TGMPA + ] Hook auto-activation in.
3094
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3095
3096
						$defaults    = array(
3097
							'clear_update_cache' => true,
3098
						);
3099
						$parsed_args = wp_parse_args( $args, $defaults );
3100
3101
						$this->init();
3102
						$this->bulk = true;
3103
3104
						$this->install_strings(); // [TGMPA + ] adjusted.
3105
3106
						/* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
3107
3108
						/* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
3109
3110
						$this->skin->header();
3111
3112
						// Connect to the Filesystem first.
3113
						$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
3114
						if ( ! $res ) {
3115
							$this->skin->footer();
3116
3117
							return false;
3118
						}
3119
3120
						$this->skin->bulk_header();
3121
3122
						// Only start maintenance mode if:
3123
						// - running Multisite and there are one or more plugins specified, OR
3124
						// - a plugin with an update available is currently active.
3125
						// @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
3126
						$maintenance = ( is_multisite() && ! empty( $plugins ) );
3127
3128
						/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3129
						[TGMPA - ]
3130
						foreach ( $plugins as $plugin )
3131
							$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
3132
						*/
3133
						if ( $maintenance ) {
3134
							$this->maintenance_mode( true );
3135
						}
3136
3137
						$results = array();
3138
3139
						$this->update_count   = count( $plugins );
3140
						$this->update_current = 0;
3141
						foreach ( $plugins as $plugin ) {
3142
							$this->update_current++;
3143
3144
							/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3145
							[TGMPA - ]
3146
							$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
3147
3148
							if ( !isset( $current->response[ $plugin ] ) ) {
3149
								$this->skin->set_result('up_to_date');
3150
								$this->skin->before();
3151
								$this->skin->feedback('up_to_date');
3152
								$this->skin->after();
3153
								$results[$plugin] = true;
3154
								continue;
3155
							}
3156
3157
							// Get the URL to the zip file
3158
							$r = $current->response[ $plugin ];
3159
3160
							$this->skin->plugin_active = is_plugin_active($plugin);
3161
							*/
3162
3163
							$result = $this->run(
3164
								array(
3165
									'package'           => $plugin, // [TGMPA + ] adjusted.
3166
									'destination'       => WP_PLUGIN_DIR,
3167
									'clear_destination' => false, // [TGMPA + ] adjusted.
3168
									'clear_working'     => true,
3169
									'is_multi'          => true,
3170
									'hook_extra'        => array(
3171
										'plugin' => $plugin,
3172
									),
3173
								)
3174
							);
3175
3176
							$results[ $plugin ] = $this->result;
3177
3178
							// Prevent credentials auth screen from displaying multiple times.
3179
							if ( false === $result ) {
3180
								break;
3181
							}
3182
						} //end foreach $plugins
3183
3184
						$this->maintenance_mode( false );
3185
3186
						/**
3187
						 * Fires when the bulk upgrader process is complete.
3188
						 *
3189
						 * @since WP 3.6.0 / TGMPA 2.5.0
3190
						 *
3191
						 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
3192
						 *                              be a Theme_Upgrader or Core_Upgrade instance.
3193
						 * @param array           $data {
3194
						 *     Array of bulk item update data.
3195
						 *
3196
						 *     @type string $action   Type of action. Default 'update'.
3197
						 *     @type string $type     Type of update process. Accepts 'plugin', 'theme', or 'core'.
3198
						 *     @type bool   $bulk     Whether the update process is a bulk update. Default true.
3199
						 *     @type array  $packages Array of plugin, theme, or core packages to update.
3200
						 * }
3201
						 */
3202
						do_action( 'upgrader_process_complete', $this, array(
3203
							'action'  => 'install', // [TGMPA + ] adjusted.
3204
							'type'    => 'plugin',
3205
							'bulk'    => true,
3206
							'plugins' => $plugins,
3207
						) );
3208
3209
						$this->skin->bulk_footer();
3210
3211
						$this->skin->footer();
3212
3213
						// Cleanup our hooks, in case something else does a upgrade on this connection.
3214
						/* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
3215
3216
						// [TGMPA + ] Remove our auto-activation hook.
3217
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3218
3219
						// Force refresh of plugin update information.
3220
						wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
3221
3222
						return $results;
3223
					}
3224
3225
					/**
3226
					 * Handle a bulk upgrade request.
3227
					 *
3228
					 * @since 2.5.0
3229
					 *
3230
					 * @see Plugin_Upgrader::bulk_upgrade()
3231
					 *
3232
					 * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
3233
					 * @param array $args    Arbitrary passed extra arguments.
3234
					 * @return string|bool Install confirmation messages on success, false on failure.
3235
					 */
3236
					public function bulk_upgrade( $plugins, $args = array() ) {
3237
3238
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3239
3240
						$result = parent::bulk_upgrade( $plugins, $args );
3241
3242
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3243
3244
						return $result;
3245
					}
3246
3247
					/**
3248
					 * Abuse a filter to auto-activate plugins after installation.
3249
					 *
3250
					 * Hooked into the 'upgrader_post_install' filter hook.
3251
					 *
3252
					 * @since 2.5.0
3253
					 *
3254
					 * @param bool $bool The value we need to give back (true).
3255
					 * @return bool
3256
					 */
3257
					public function auto_activate( $bool ) {
3258
						// Only process the activation of installed plugins if the automatic flag is set to true.
3259
						if ( $this->tgmpa->is_automatic ) {
3260
							// Flush plugins cache so the headers of the newly installed plugins will be read correctly.
3261
							wp_clean_plugins_cache();
3262
3263
							// Get the installed plugin file.
3264
							$plugin_info = $this->plugin_info();
3265
3266
							// Don't try to activate on upgrade of active plugin as WP will do this already.
3267
							if ( ! is_plugin_active( $plugin_info ) ) {
3268
								$activate = activate_plugin( $plugin_info );
3269
3270
								// Adjust the success string based on the activation result.
3271
								$this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
3272
3273
								if ( is_wp_error( $activate ) ) {
3274
									$this->skin->error( $activate );
3275
									$this->strings['process_success'] .= $this->strings['activation_failed'];
3276
								} else {
3277
									$this->strings['process_success'] .= $this->strings['activation_success'];
3278
								}
3279
							}
3280
						}
3281
3282
						return $bool;
3283
					}
3284
				}
3285
			}
3286
3287
			if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) {
3288
3289
				/**
3290
				 * Installer skin to set strings for the bulk plugin installations..
3291
				 *
3292
				 * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
3293
				 * plugins.
3294
				 *
3295
				 * @since 2.2.0
3296
				 *
3297
				 * @internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to
3298
				 *           TGMPA_Bulk_Installer_Skin.
3299
				 *           This was done to prevent backward compatibility issues with v2.3.6.
3300
				 *
3301
				 * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php
3302
				 *
3303
				 * @package TGM-Plugin-Activation
3304
				 * @author  Thomas Griffin
3305
				 * @author  Gary Jones
3306
				 */
3307
				class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
3308
					/**
3309
					 * Holds plugin info for each individual plugin installation.
3310
					 *
3311
					 * @since 2.2.0
3312
					 *
3313
					 * @var array
3314
					 */
3315
					public $plugin_info = array();
3316
3317
					/**
3318
					 * Holds names of plugins that are undergoing bulk installations.
3319
					 *
3320
					 * @since 2.2.0
3321
					 *
3322
					 * @var array
3323
					 */
3324
					public $plugin_names = array();
3325
3326
					/**
3327
					 * Integer to use for iteration through each plugin installation.
3328
					 *
3329
					 * @since 2.2.0
3330
					 *
3331
					 * @var integer
3332
					 */
3333
					public $i = 0;
3334
3335
					/**
3336
					 * TGMPA instance
3337
					 *
3338
					 * @since 2.5.0
3339
					 *
3340
					 * @var object
3341
					 */
3342
					protected $tgmpa;
3343
3344
					/**
3345
					 * Constructor. Parses default args with new ones and extracts them for use.
3346
					 *
3347
					 * @since 2.2.0
3348
					 *
3349
					 * @param array $args Arguments to pass for use within the class.
3350
					 */
3351
					public function __construct( $args = array() ) {
3352
						// Get TGMPA class instance.
3353
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3354
3355
						// Parse default and new args.
3356
						$defaults = array(
3357
							'url'          => '',
3358
							'nonce'        => '',
3359
							'names'        => array(),
3360
							'install_type' => 'install',
3361
						);
3362
						$args     = wp_parse_args( $args, $defaults );
3363
3364
						// Set plugin names to $this->plugin_names property.
3365
						$this->plugin_names = $args['names'];
3366
3367
						// Extract the new args.
3368
						parent::__construct( $args );
3369
					}
3370
3371
					/**
3372
					 * Sets install skin strings for each individual plugin.
3373
					 *
3374
					 * Checks to see if the automatic activation flag is set and uses the
3375
					 * the proper strings accordingly.
3376
					 *
3377
					 * @since 2.2.0
3378
					 */
3379
					public function add_strings() {
3380
						if ( 'update' === $this->options['install_type'] ) {
3381
							parent::add_strings();
3382
							$this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3383
						} else {
3384
							$this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
3385
							$this->upgrader->strings['skin_update_failed']       = __( 'The installation of %1$s failed.', 'tgmpa' );
3386
3387
							if ( $this->tgmpa->is_automatic ) {
3388
								// Automatic activation strings.
3389
								$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' );
3390
								$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>';
3391
								$this->upgrader->strings['skin_upgrade_end']          = __( 'All installations and activations have been completed.', 'tgmpa' );
3392
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3393
							} else {
3394
								// Default installation strings.
3395
								$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' );
3396
								$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>';
3397
								$this->upgrader->strings['skin_upgrade_end']          = __( 'All installations have been completed.', 'tgmpa' );
3398
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3399
							}
3400
						}
3401
					}
3402
3403
					/**
3404
					 * Outputs the header strings and necessary JS before each plugin installation.
3405
					 *
3406
					 * @since 2.2.0
3407
					 *
3408
					 * @param string $title Unused in this implementation.
3409
					 */
3410
					public function before( $title = '' ) {
3411
						if ( empty( $title ) ) {
3412
							$title = esc_html( $this->plugin_names[ $this->i ] );
3413
						}
3414
						parent::before( $title );
3415
					}
3416
3417
					/**
3418
					 * Outputs the footer strings and necessary JS after each plugin installation.
3419
					 *
3420
					 * Checks for any errors and outputs them if they exist, else output
3421
					 * success strings.
3422
					 *
3423
					 * @since 2.2.0
3424
					 *
3425
					 * @param string $title Unused in this implementation.
3426
					 */
3427
					public function after( $title = '' ) {
3428
						if ( empty( $title ) ) {
3429
							$title = esc_html( $this->plugin_names[ $this->i ] );
3430
						}
3431
						parent::after( $title );
3432
3433
						$this->i++;
3434
					}
3435
3436
					/**
3437
					 * Outputs links after bulk plugin installation is complete.
3438
					 *
3439
					 * @since 2.2.0
3440
					 */
3441
					public function bulk_footer() {
3442
						// Serve up the string to say installations (and possibly activations) are complete.
3443
						parent::bulk_footer();
3444
3445
						// Flush plugins cache so we can make sure that the installed plugins list is always up to date.
3446
						wp_clean_plugins_cache();
3447
3448
						$this->tgmpa->show_tgmpa_version();
3449
3450
						// Display message based on if all plugins are now active or not.
3451
						$update_actions = array();
3452
3453
						if ( $this->tgmpa->is_tgmpa_complete() ) {
3454
							// All plugins are active, so we display the complete string and hide the menu to protect users.
3455
							echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
3456
							$update_actions['dashboard'] = sprintf(
3457
								esc_html( $this->tgmpa->strings['complete'] ),
3458
								'<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>'
3459
							);
3460
						} else {
3461
							$update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>';
3462
						}
3463
3464
						/**
3465
						 * Filter the list of action links available following bulk plugin installs/updates.
3466
						 *
3467
						 * @since 2.5.0
3468
						 *
3469
						 * @param array $update_actions Array of plugin action links.
3470
						 * @param array $plugin_info    Array of information for the last-handled plugin.
3471
						 */
3472
						$update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
3473
3474
						if ( ! empty( $update_actions ) ) {
3475
							$this->feedback( implode( ' | ', (array) $update_actions ) );
3476
						}
3477
					}
3478
3479
					/* *********** DEPRECATED METHODS *********** */
3480
3481
					/**
3482
					 * Flush header output buffer.
3483
					 *
3484
					 * @since      2.2.0
3485
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3486
					 * @see        Bulk_Upgrader_Skin::flush_output()
3487
					 */
3488
					public function before_flush_output() {
3489
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3490
						$this->flush_output();
3491
					}
3492
3493
					/**
3494
					 * Flush footer output buffer and iterate $this->i to make sure the
3495
					 * installation strings reference the correct plugin.
3496
					 *
3497
					 * @since      2.2.0
3498
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3499
					 * @see        Bulk_Upgrader_Skin::flush_output()
3500
					 */
3501
					public function after_flush_output() {
3502
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3503
						$this->flush_output();
3504
						$this->i++;
3505
					}
3506
				}
3507
			}
3508
		}
3509
	}
3510
}
3511
3512
if ( ! class_exists( 'TGMPA_Utils' ) ) {
3513
3514
	/**
3515
	 * Generic utilities for TGMPA.
3516
	 *
3517
	 * All methods are static, poor-dev name-spacing class wrapper.
3518
	 *
3519
	 * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy.
3520
	 *
3521
	 * @since 2.5.0
3522
	 *
3523
	 * @package TGM-Plugin-Activation
3524
	 * @author  Juliette Reinders Folmer
3525
	 */
3526
	class TGMPA_Utils {
3527
		/**
3528
		 * Whether the PHP filter extension is enabled.
3529
		 *
3530
		 * @see http://php.net/book.filter
3531
		 *
3532
		 * @since 2.5.0
3533
		 *
3534
		 * @static
3535
		 *
3536
		 * @var bool $has_filters True is the extension is enabled.
3537
		 */
3538
		public static $has_filters;
3539
3540
		/**
3541
		 * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map().
3542
		 *
3543
		 * @since 2.5.0
3544
		 *
3545
		 * @static
3546
		 *
3547
		 * @param string $string Text to be wrapped.
3548
		 * @return string
3549
		 */
3550
		public static function wrap_in_em( $string ) {
3551
			return '<em>' . wp_kses_post( $string ) . '</em>';
3552
		}
3553
3554
		/**
3555
		 * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map().
3556
		 *
3557
		 * @since 2.5.0
3558
		 *
3559
		 * @static
3560
		 *
3561
		 * @param string $string Text to be wrapped.
3562
		 * @return string
3563
		 */
3564
		public static function wrap_in_strong( $string ) {
3565
			return '<strong>' . wp_kses_post( $string ) . '</strong>';
3566
		}
3567
3568
		/**
3569
		 * Helper function: Validate a value as boolean
3570
		 *
3571
		 * @since 2.5.0
3572
		 *
3573
		 * @static
3574
		 *
3575
		 * @param mixed $value Arbitrary value.
3576
		 * @return bool
3577
		 */
3578
		public static function validate_bool( $value ) {
3579
			if ( ! isset( self::$has_filters ) ) {
3580
				self::$has_filters = extension_loaded( 'filter' );
3581
			}
3582
3583
			if ( self::$has_filters ) {
3584
				return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
3585
			} else {
3586
				return self::emulate_filter_bool( $value );
3587
			}
3588
		}
3589
3590
		/**
3591
		 * Helper function: Cast a value to bool
3592
		 *
3593
		 * @since 2.5.0
3594
		 *
3595
		 * @static
3596
		 *
3597
		 * @param mixed $value Value to cast.
3598
		 * @return bool
3599
		 */
3600
		protected static function emulate_filter_bool( $value ) {
3601
			// @codingStandardsIgnoreStart
3602
			static $true  = array(
3603
				'1',
3604
				'true', 'True', 'TRUE',
3605
				'y', 'Y',
3606
				'yes', 'Yes', 'YES',
3607
				'on', 'On', 'ON',
3608
			);
3609
			static $false = array(
3610
				'0',
3611
				'false', 'False', 'FALSE',
3612
				'n', 'N',
3613
				'no', 'No', 'NO',
3614
				'off', 'Off', 'OFF',
3615
			);
3616
			// @codingStandardsIgnoreEnd
3617
3618
			if ( is_bool( $value ) ) {
3619
				return $value;
3620
			} else if ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
3621
				return (bool) $value;
3622
			} else if ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
3623
				return (bool) $value;
3624
			} else if ( is_string( $value ) ) {
3625
				$value = trim( $value );
3626
				if ( in_array( $value, $true, true ) ) {
3627
					return true;
3628
				} else if ( in_array( $value, $false, true ) ) {
3629
					return false;
3630
				} else {
3631
					return false;
3632
				}
3633
			}
3634
3635
			return false;
3636
		}
3637
	} // End of class TGMPA_Utils
3638
} // End of class_exists wrapper
3639