TGMPA_List_Table::get_primary_column_name()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
810
			if ( empty( $_GET['plugin'] ) ) {
811
				return false;
812
			}
813
814
			// All plugin information will be stored in an array for processing.
815
			$slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
816
817
			if ( ! isset( $this->plugins[ $slug ] ) ) {
818
				return false;
819
			}
820
821
			// Was an install or upgrade action link clicked?
822
			if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
823
824
				$install_type = 'install';
825
				if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
826
					$install_type = 'update';
827
				}
828
829
				check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
830
831
				// Pass necessary information via URL if WP_Filesystem is needed.
832
				$url = wp_nonce_url(
833
					add_query_arg(
834
						array(
835
							'plugin'                 => urlencode( $slug ),
836
							'tgmpa-' . $install_type => $install_type . '-plugin',
837
						),
838
						$this->get_tgmpa_url()
839
					),
840
					'tgmpa-' . $install_type,
841
					'tgmpa-nonce'
842
				);
843
844
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
845
846
				$creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() );
847
				if ( false === $creds ) {
848
					return true;
849
				}
850
851 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...
852
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
853
					return true;
854
				}
855
856
				/* If we arrive here, we have the filesystem. */
857
858
				// Prep variables for Plugin_Installer_Skin class.
859
				$extra         = array();
860
				$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
861
				$source        = $this->get_download_url( $slug );
862
				$api           = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
863
				$api           = ( false !== $api ) ? $api : null;
864
865
				$url = add_query_arg(
866
					array(
867
						'action' => $install_type . '-plugin',
868
						'plugin' => urlencode( $slug ),
869
					),
870
					'update.php'
871
				);
872
873
				if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
874
					require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
875
				}
876
877
				$title     = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
878
				$skin_args = array(
879
					'type'   => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
880
					'title'  => sprintf( $title, $this->plugins[ $slug ]['name'] ),
881
					'url'    => esc_url_raw( $url ),
882
					'nonce'  => $install_type . '-plugin_' . $slug,
883
					'plugin' => '',
884
					'api'    => $api,
885
					'extra'  => $extra,
886
				);
887
				unset( $title );
888
889
				if ( 'update' === $install_type ) {
890
					$skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
891
					$skin                = new Plugin_Upgrader_Skin( $skin_args );
892
				} else {
893
					$skin = new Plugin_Installer_Skin( $skin_args );
894
				}
895
896
				// Create a new instance of Plugin_Upgrader.
897
				$upgrader = new Plugin_Upgrader( $skin );
898
899
				// Perform the action and install the plugin from the $source urldecode().
900
				add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
901
902
				if ( 'update' === $install_type ) {
903
					// Inject our info into the update transient.
904
					$to_inject                    = array(
905
						$slug => $this->plugins[ $slug ],
906
					);
907
					$to_inject[ $slug ]['source'] = $source;
908
					$this->inject_update_info( $to_inject );
909
910
					$upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
911
				} else {
912
					$upgrader->install( $source );
913
				}
914
915
				remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 );
916
917
				// Make sure we have the correct file path now the plugin is installed/updated.
918
				$this->populate_file_path( $slug );
919
920
				// Only activate plugins if the config option is set to true and the plugin isn't
921
				// already active (upgrade).
922
				if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
923
					$plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
924
					if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
925
						return true; // Finish execution of the function early as we encountered an error.
926
					}
927
				}
928
929
				$this->show_tgmpa_version();
930
931
				// Display message based on if all plugins are now active or not.
932
				if ( $this->is_tgmpa_complete() ) {
933
					echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html( $this->strings['dashboard'] ) . '</a>' ), '</p>';
934
					echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
935
				} else {
936
					echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
937
				}
938
939
				return true;
940
			} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
941
				// Activate action link was clicked.
942
				check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
943
944
				if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
945
					return true; // Finish execution of the function early as we encountered an error.
946
				}
947
			}
948
949
			return false;
950
		}
951
952
		/**
953
		 * Inject information into the 'update_plugins' site transient as WP checks that before running an update.
954
		 *
955
		 * @since 2.5.0
956
		 *
957
		 * @param array $plugins The plugin information for the plugins which are to be updated.
958
		 */
959
		public function inject_update_info( $plugins ) {
960
			$repo_updates = get_site_transient( 'update_plugins' );
961
962
			if ( ! is_object( $repo_updates ) ) {
963
				$repo_updates = new stdClass();
964
			}
965
966
			foreach ( $plugins as $slug => $plugin ) {
967
				$file_path = $plugin['file_path'];
968
969
				if ( empty( $repo_updates->response[ $file_path ] ) ) {
970
					$repo_updates->response[ $file_path ] = new stdClass();
971
				}
972
973
				// We only really need to set package, but let's do all we can in case WP changes something.
974
				$repo_updates->response[ $file_path ]->slug        = $slug;
975
				$repo_updates->response[ $file_path ]->plugin      = $file_path;
976
				$repo_updates->response[ $file_path ]->new_version = $plugin['version'];
977
				$repo_updates->response[ $file_path ]->package     = $plugin['source'];
978
				if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
979
					$repo_updates->response[ $file_path ]->url = $plugin['external_url'];
980
				}
981
			}
982
983
			set_site_transient( 'update_plugins', $repo_updates );
984
		}
985
986
		/**
987
		 * Adjust the plugin directory name if necessary.
988
		 *
989
		 * The final destination directory of a plugin is based on the subdirectory name found in the
990
		 * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
991
		 * subdirectory name is not the same as the expected slug and the plugin will not be recognized
992
		 * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
993
		 * the expected plugin slug.
994
		 *
995
		 * @since 2.5.0
996
		 *
997
		 * @param string       $source        Path to upgrade/zip-file-name.tmp/subdirectory/.
998
		 * @param string       $remote_source Path to upgrade/zip-file-name.tmp.
999
		 * @param \WP_Upgrader $upgrader      Instance of the upgrader which installs the plugin.
1000
		 * @return string $source
1001
		 */
1002
		public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
1003
			if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
1004
				return $source;
1005
			}
1006
1007
			// Check for single file plugins.
1008
			$source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
1009
			if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
1010
				return $source;
1011
			}
1012
1013
			// Multi-file plugin, let's see if the directory is correctly named.
1014
			$desired_slug = '';
1015
1016
			// Figure out what the slug is supposed to be.
1017
			if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
1018
				$desired_slug = $upgrader->skin->options['extra']['slug'];
1019
			} else {
1020
				// Bulk installer contains less info, so fall back on the info registered here.
1021
				foreach ( $this->plugins as $slug => $plugin ) {
1022
					if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
1023
						$desired_slug = $slug;
1024
						break;
1025
					}
1026
				}
1027
				unset( $slug, $plugin );
1028
			}
1029
1030
			if ( ! empty( $desired_slug ) ) {
1031
				$subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
1032
1033
				if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
1034
					$from_path = untrailingslashit( $source );
1035
					$to_path   = trailingslashit( $remote_source ) . $desired_slug;
1036
1037
					if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
1038
						return trailingslashit( $to_path );
1039 View Code Duplication
					} else {
1040
						return new WP_Error(
1041
							'rename_failed',
1042
							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' ),
1043
							array(
1044
								'found'    => $subdir_name,
1045
								'expected' => $desired_slug,
1046
							)
1047
						);
1048
					}
1049 View Code Duplication
				} elseif ( empty( $subdir_name ) ) {
1050
					return new WP_Error(
1051
						'packaged_wrong',
1052
						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' ),
1053
						array(
1054
							'found'    => $subdir_name,
1055
							'expected' => $desired_slug,
1056
						)
1057
					);
1058
				}
1059
			}
1060
1061
			return $source;
1062
		}
1063
1064
		/**
1065
		 * Activate a single plugin and send feedback about the result to the screen.
1066
		 *
1067
		 * @since 2.5.0
1068
		 *
1069
		 * @param string $file_path Path within wp-plugins/ to main plugin file.
1070
		 * @param string $slug      Plugin slug.
1071
		 * @param bool   $automatic Whether this is an automatic activation after an install. Defaults to false.
1072
		 *                          This determines the styling of the output messages.
1073
		 * @return bool False if an error was encountered, true otherwise.
1074
		 */
1075
		protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
0 ignored issues
show
Coding Style introduced by
function activate_single_plugin() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
1076
			if ( $this->can_plugin_activate( $slug ) ) {
1077
				$activate = activate_plugin( $file_path );
1078
1079
				if ( is_wp_error( $activate ) ) {
1080
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
1081
						'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
1082
1083
					return false; // End it here if there is an error with activation.
1084
				} else {
1085
					if ( ! $automatic ) {
1086
						// Make sure message doesn't display again if bulk activation is performed
1087
						// immediately after a single activation.
1088
						// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Not using the superglobal.
1089
						if ( ! isset( $_POST['action'] ) ) {
1090
							echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
1091
						}
1092
					} else {
1093
						// Simpler message layout for use on the plugin install page.
1094
						echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
1095
					}
1096
				}
1097 View Code Duplication
			} elseif ( $this->is_plugin_active( $slug ) ) {
1098
				// No simpler message format provided as this message should never be encountered
1099
				// on the plugin install page.
1100
				echo '<div id="message" class="error"><p>',
1101
					sprintf(
1102
						esc_html( $this->strings['plugin_already_active'] ),
1103
						'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1104
					),
1105
					'</p></div>';
1106
			} elseif ( $this->does_plugin_require_update( $slug ) ) {
1107
				if ( ! $automatic ) {
1108
					// Make sure message doesn't display again if bulk activation is performed
1109
					// immediately after a single activation.
1110
					// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Not using the superglobal.
1111 View Code Duplication
					if ( ! isset( $_POST['action'] ) ) {
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...
1112
						echo '<div id="message" class="error"><p>',
1113
							sprintf(
1114
								esc_html( $this->strings['plugin_needs_higher_version'] ),
1115
								'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1116
							),
1117
							'</p></div>';
1118
					}
1119
				} else {
1120
					// Simpler message layout for use on the plugin install page.
1121
					echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
1122
				}
1123
			}
1124
1125
			return true;
1126
		}
1127
1128
		/**
1129
		 * Echoes required plugin notice.
1130
		 *
1131
		 * Outputs a message telling users that a specific plugin is required for
1132
		 * their theme. If appropriate, it includes a link to the form page where
1133
		 * users can install and activate the plugin.
1134
		 *
1135
		 * Returns early if we're on the Install page.
1136
		 *
1137
		 * @since 1.0.0
1138
		 *
1139
		 * @global object $current_screen
1140
		 *
1141
		 * @return null Returns early if we're on the Install page.
1142
		 */
1143
		public function notices() {
1144
			// Remove nag on the install page / Return early if the nag message has been dismissed or user < author.
1145
			if ( ( $this->is_tgmpa_page() || $this->is_core_update_page() ) || get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) || ! current_user_can( apply_filters( 'tgmpa_show_admin_notice_capability', 'publish_posts' ) ) ) {
1146
				return;
1147
			}
1148
1149
			// Store for the plugin slugs by message type.
1150
			$message = array();
1151
1152
			// Initialize counters used to determine plurality of action link texts.
1153
			$install_link_count          = 0;
1154
			$update_link_count           = 0;
1155
			$activate_link_count         = 0;
1156
			$total_required_action_count = 0;
1157
1158
			foreach ( $this->plugins as $slug => $plugin ) {
1159
				if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) {
1160
					continue;
1161
				}
1162
1163
				if ( ! $this->is_plugin_installed( $slug ) ) {
1164
					if ( current_user_can( 'install_plugins' ) ) {
1165
						$install_link_count++;
1166
1167
						if ( true === $plugin['required'] ) {
1168
							$message['notice_can_install_required'][] = $slug;
1169
						} else {
1170
							$message['notice_can_install_recommended'][] = $slug;
1171
						}
1172
					}
1173
					if ( true === $plugin['required'] ) {
1174
						$total_required_action_count++;
1175
					}
1176
				} else {
1177
					if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
1178
						if ( current_user_can( 'activate_plugins' ) ) {
1179
							$activate_link_count++;
1180
1181
							if ( true === $plugin['required'] ) {
1182
								$message['notice_can_activate_required'][] = $slug;
1183
							} else {
1184
								$message['notice_can_activate_recommended'][] = $slug;
1185
							}
1186
						}
1187
						if ( true === $plugin['required'] ) {
1188
							$total_required_action_count++;
1189
						}
1190
					}
1191
1192
					if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1193
1194
						if ( current_user_can( 'update_plugins' ) ) {
1195
							$update_link_count++;
1196
1197
							if ( $this->does_plugin_require_update( $slug ) ) {
1198
								$message['notice_ask_to_update'][] = $slug;
1199
							} elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
1200
								$message['notice_ask_to_update_maybe'][] = $slug;
1201
							}
1202
						}
1203
						if ( true === $plugin['required'] ) {
1204
							$total_required_action_count++;
1205
						}
1206
					}
1207
				}
1208
			}
1209
			unset( $slug, $plugin );
1210
1211
			// If we have notices to display, we move forward.
1212
			if ( ! empty( $message ) || $total_required_action_count > 0 ) {
1213
				krsort( $message ); // Sort messages.
1214
				$rendered = '';
1215
1216
				// As add_settings_error() wraps the final message in a <p> and as the final message can't be
1217
				// filtered, using <p>'s in our html would render invalid html output.
1218
				$line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
1219
1220
				if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
1221
					$rendered  = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] );
1222
					$rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
1223
				} else {
1224
1225
					// If dismissable is false and a message is set, output it now.
1226
					if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
1227
						$rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
1228
					}
1229
1230
					// Render the individual message lines for the notice.
1231
					foreach ( $message as $type => $plugin_group ) {
1232
						$linked_plugins = array();
1233
1234
						// Get the external info link for a plugin if one is available.
1235
						foreach ( $plugin_group as $plugin_slug ) {
1236
							$linked_plugins[] = $this->get_info_link( $plugin_slug );
1237
						}
1238
						unset( $plugin_slug );
1239
1240
						$count          = count( $plugin_group );
1241
						$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
1242
						$last_plugin    = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
1243
						$imploded       = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
1244
1245
						$rendered .= sprintf(
1246
							$line_template,
1247
							sprintf(
1248
								translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
1249
								$imploded,
1250
								$count
1251
							)
1252
						);
1253
1254
					}
1255
					unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
1256
1257
					$rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
1258
				}
1259
1260
				// Register the nag messages and prepare them to be processed.
1261
				add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
1262
			}
1263
1264
			// Admin options pages already output settings_errors, so this is to avoid duplication.
1265
			if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
1266
				$this->display_settings_errors();
1267
			}
1268
		}
1269
1270
		/**
1271
		 * Generate the user action links for the admin notice.
1272
		 *
1273
		 * @since 2.6.0
1274
		 *
1275
		 * @param int $install_count  Number of plugins to install.
1276
		 * @param int $update_count   Number of plugins to update.
1277
		 * @param int $activate_count Number of plugins to activate.
1278
		 * @param int $line_template  Template for the HTML tag to output a line.
1279
		 * @return string Action links.
1280
		 */
1281
		protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
1282
			// Setup action links.
1283
			$action_links = array(
1284
				'install'  => '',
1285
				'update'   => '',
1286
				'activate' => '',
1287
				'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>' : '',
1288
			);
1289
1290
			$link_template = '<a href="%2$s">%1$s</a>';
1291
1292
			if ( current_user_can( 'install_plugins' ) ) {
1293
				if ( $install_count > 0 ) {
1294
					$action_links['install'] = sprintf(
1295
						$link_template,
1296
						translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),
1297
						esc_url( $this->get_tgmpa_status_url( 'install' ) )
1298
					);
1299
				}
1300
				if ( $update_count > 0 ) {
1301
					$action_links['update'] = sprintf(
1302
						$link_template,
1303
						translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),
1304
						esc_url( $this->get_tgmpa_status_url( 'update' ) )
1305
					);
1306
				}
1307
			}
1308
1309
			if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1310
				$action_links['activate'] = sprintf(
1311
					$link_template,
1312
					translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),
1313
					esc_url( $this->get_tgmpa_status_url( 'activate' ) )
1314
				);
1315
			}
1316
1317
			$action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
1318
1319
			$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
1320
1321
			if ( ! empty( $action_links ) ) {
1322
				$action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
1323
				return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
1324
			} else {
1325
				return '';
1326
			}
1327
		}
1328
1329
		/**
1330
		 * Get admin notice class.
1331
		 *
1332
		 * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1333
		 * (lowest supported version by TGMPA).
1334
		 *
1335
		 * @since 2.6.0
1336
		 *
1337
		 * @return string
1338
		 */
1339
		protected function get_admin_notice_class() {
1340
			if ( ! empty( $this->strings['nag_type'] ) ) {
1341
				return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1342
			} else {
1343
				if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1344
					return 'notice-warning';
1345
				} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1346
					return 'notice';
1347
				} else {
1348
					return 'updated';
1349
				}
1350
			}
1351
		}
1352
1353
		/**
1354
		 * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
1355
		 *
1356
		 * @since 2.5.0
1357
		 */
1358
		protected function display_settings_errors() {
1359
			global $wp_settings_errors;
1360
1361
			settings_errors( 'tgmpa' );
1362
1363
			foreach ( (array) $wp_settings_errors as $key => $details ) {
1364
				if ( 'tgmpa' === $details['setting'] ) {
1365
					unset( $wp_settings_errors[ $key ] );
1366
					break;
1367
				}
1368
			}
1369
		}
1370
1371
		/**
1372
		 * Register dismissal of admin notices.
1373
		 *
1374
		 * Acts on the dismiss link in the admin nag messages.
1375
		 * If clicked, the admin notice disappears and will no longer be visible to this user.
1376
		 *
1377
		 * @since 2.1.0
1378
		 */
1379
		public function dismiss() {
1380
			if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) {
1381
				update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 );
1382
			}
1383
		}
1384
1385
		/**
1386
		 * Add individual plugin to our collection of plugins.
1387
		 *
1388
		 * If the required keys are not set or the plugin has already
1389
		 * been registered, the plugin is not added.
1390
		 *
1391
		 * @since 2.0.0
1392
		 *
1393
		 * @param array|null $plugin Array of plugin arguments or null if invalid argument.
1394
		 * @return null Return early if incorrect argument.
1395
		 */
1396
		public function register( $plugin ) {
1397
			if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) {
1398
				return;
1399
			}
1400
1401
			if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
1402
				return;
1403
			}
1404
1405
			$defaults = array(
1406
				'name'               => '',      // String.
1407
				'slug'               => '',      // String.
1408
				'source'             => 'repo',  // String.
1409
				'required'           => false,   // Boolean.
1410
				'version'            => '',      // String.
1411
				'force_activation'   => false,   // Boolean.
1412
				'force_deactivation' => false,   // Boolean.
1413
				'external_url'       => '',      // String.
1414
				'is_callable'        => '',      // String or array.
1415
			);
1416
1417
			// Prepare the received data.
1418
			$plugin = wp_parse_args( $plugin, $defaults );
1419
1420
			// Standardize the received slug.
1421
			$plugin['slug'] = $this->sanitize_key( $plugin['slug'] );
1422
1423
			// Forgive users for using string versions of booleans or floats for version number.
1424
			$plugin['version']            = (string) $plugin['version'];
1425
			$plugin['source']             = empty( $plugin['source'] ) ? 'repo' : $plugin['source'];
1426
			$plugin['required']           = TGMPA_Utils::validate_bool( $plugin['required'] );
1427
			$plugin['force_activation']   = TGMPA_Utils::validate_bool( $plugin['force_activation'] );
1428
			$plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] );
1429
1430
			// Enrich the received data.
1431
			$plugin['file_path']   = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
1432
			$plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] );
1433
1434
			// Set the class properties.
1435
			$this->plugins[ $plugin['slug'] ]    = $plugin;
1436
			$this->sort_order[ $plugin['slug'] ] = $plugin['name'];
1437
1438
			// Should we add the force activation hook ?
1439
			if ( true === $plugin['force_activation'] ) {
1440
				$this->has_forced_activation = true;
1441
			}
1442
1443
			// Should we add the force deactivation hook ?
1444
			if ( true === $plugin['force_deactivation'] ) {
1445
				$this->has_forced_deactivation = true;
1446
			}
1447
		}
1448
1449
		/**
1450
		 * Determine what type of source the plugin comes from.
1451
		 *
1452
		 * @since 2.5.0
1453
		 *
1454
		 * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path
1455
		 *                       (= bundled) or an external URL.
1456
		 * @return string 'repo', 'external', or 'bundled'
1457
		 */
1458
		protected function get_plugin_source_type( $source ) {
1459
			if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) {
1460
				return 'repo';
1461
			} elseif ( preg_match( self::IS_URL_REGEX, $source ) ) {
1462
				return 'external';
1463
			} else {
1464
				return 'bundled';
1465
			}
1466
		}
1467
1468
		/**
1469
		 * Sanitizes a string key.
1470
		 *
1471
		 * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are*
1472
		 * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase
1473
		 * characters in the plugin directory path/slug. Silly them.
1474
		 *
1475
		 * @see https://developer.wordpress.org/reference/hooks/sanitize_key/
1476
		 *
1477
		 * @since 2.5.0
1478
		 *
1479
		 * @param string $key String key.
1480
		 * @return string Sanitized key
1481
		 */
1482
		public function sanitize_key( $key ) {
1483
			$raw_key = $key;
1484
			$key     = preg_replace( '`[^A-Za-z0-9_-]`', '', $key );
1485
1486
			/**
1487
			 * Filter a sanitized key string.
1488
			 *
1489
			 * @since 2.5.0
1490
			 *
1491
			 * @param string $key     Sanitized key.
1492
			 * @param string $raw_key The key prior to sanitization.
1493
			 */
1494
			return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key );
1495
		}
1496
1497
		/**
1498
		 * Amend default configuration settings.
1499
		 *
1500
		 * @since 2.0.0
1501
		 *
1502
		 * @param array $config Array of config options to pass as class properties.
1503
		 */
1504
		public function config( $config ) {
1505
			$keys = array(
1506
				'id',
1507
				'default_path',
1508
				'has_notices',
1509
				'dismissable',
1510
				'dismiss_msg',
1511
				'menu',
1512
				'parent_slug',
1513
				'capability',
1514
				'is_automatic',
1515
				'message',
1516
				'strings',
1517
			);
1518
1519
			foreach ( $keys as $key ) {
1520
				if ( isset( $config[ $key ] ) ) {
1521
					if ( is_array( $config[ $key ] ) ) {
1522
						$this->$key = array_merge( $this->$key, $config[ $key ] );
1523
					} else {
1524
						$this->$key = $config[ $key ];
1525
					}
1526
				}
1527
			}
1528
		}
1529
1530
		/**
1531
		 * Amend action link after plugin installation.
1532
		 *
1533
		 * @since 2.0.0
1534
		 *
1535
		 * @param array $install_actions Existing array of actions.
1536
		 * @return false|array Amended array of actions.
1537
		 */
1538
		public function actions( $install_actions ) {
1539
			// Remove action links on the TGMPA install page.
1540
			if ( $this->is_tgmpa_page() ) {
1541
				return false;
1542
			}
1543
1544
			return $install_actions;
1545
		}
1546
1547
		/**
1548
		 * Flushes the plugins cache on theme switch to prevent stale entries
1549
		 * from remaining in the plugin table.
1550
		 *
1551
		 * @since 2.4.0
1552
		 *
1553
		 * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache.
1554
		 *                                 Parameter added in v2.5.0.
1555
		 */
1556
		public function flush_plugins_cache( $clear_update_cache = true ) {
1557
			wp_clean_plugins_cache( $clear_update_cache );
1558
		}
1559
1560
		/**
1561
		 * Set file_path key for each installed plugin.
1562
		 *
1563
		 * @since 2.1.0
1564
		 *
1565
		 * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
1566
		 *                            Parameter added in v2.5.0.
1567
		 */
1568
		public function populate_file_path( $plugin_slug = '' ) {
1569
			if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) {
1570
				$this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug );
1571
			} else {
1572
				// Add file_path key for all plugins.
1573
				foreach ( $this->plugins as $slug => $values ) {
1574
					$this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug );
1575
				}
1576
			}
1577
		}
1578
1579
		/**
1580
		 * Helper function to extract the file path of the plugin file from the
1581
		 * plugin slug, if the plugin is installed.
1582
		 *
1583
		 * @since 2.0.0
1584
		 *
1585
		 * @param string $slug Plugin slug (typically folder name) as provided by the developer.
1586
		 * @return string Either file path for plugin if installed, or just the plugin slug.
1587
		 */
1588
		protected function _get_plugin_basename_from_slug( $slug ) {
0 ignored issues
show
Coding Style introduced by
function _get_plugin_basename_from_slug() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-z_0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
1589
			$keys = array_keys( $this->get_plugins() );
1590
1591
			foreach ( $keys as $key ) {
1592
				if ( preg_match( '|^' . $slug . '/|', $key ) ) {
1593
					return $key;
1594
				}
1595
			}
1596
1597
			return $slug;
1598
		}
1599
1600
		/**
1601
		 * Retrieve plugin data, given the plugin name.
1602
		 *
1603
		 * Loops through the registered plugins looking for $name. If it finds it,
1604
		 * it returns the $data from that plugin. Otherwise, returns false.
1605
		 *
1606
		 * @since 2.1.0
1607
		 *
1608
		 * @param string $name Name of the plugin, as it was registered.
1609
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
1610
		 * @return string|boolean Plugin slug if found, false otherwise.
1611
		 */
1612
		public function _get_plugin_data_from_name( $name, $data = 'slug' ) {
0 ignored issues
show
Coding Style introduced by
function _get_plugin_data_from_name() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-z_0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
1613
			foreach ( $this->plugins as $values ) {
1614
				if ( $name === $values['name'] && isset( $values[ $data ] ) ) {
1615
					return $values[ $data ];
1616
				}
1617
			}
1618
1619
			return false;
1620
		}
1621
1622
		/**
1623
		 * Retrieve the download URL for a package.
1624
		 *
1625
		 * @since 2.5.0
1626
		 *
1627
		 * @param string $slug Plugin slug.
1628
		 * @return string Plugin download URL or path to local file or empty string if undetermined.
1629
		 */
1630
		public function get_download_url( $slug ) {
1631
			$dl_source = '';
1632
1633
			switch ( $this->plugins[ $slug ]['source_type'] ) {
1634
				case 'repo':
1635
					return $this->get_wp_repo_download_url( $slug );
1636
				case 'external':
1637
					return $this->plugins[ $slug ]['source'];
1638
				case 'bundled':
1639
					return $this->default_path . $this->plugins[ $slug ]['source'];
1640
			}
1641
1642
			return $dl_source; // Should never happen.
1643
		}
1644
1645
		/**
1646
		 * Retrieve the download URL for a WP repo package.
1647
		 *
1648
		 * @since 2.5.0
1649
		 *
1650
		 * @param string $slug Plugin slug.
1651
		 * @return string Plugin download URL.
1652
		 */
1653
		protected function get_wp_repo_download_url( $slug ) {
1654
			$source = '';
1655
			$api    = $this->get_plugins_api( $slug );
1656
1657
			if ( false !== $api && isset( $api->download_link ) ) {
1658
				$source = $api->download_link;
1659
			}
1660
1661
			return $source;
1662
		}
1663
1664
		/**
1665
		 * Try to grab information from WordPress API.
1666
		 *
1667
		 * @since 2.5.0
1668
		 *
1669
		 * @param string $slug Plugin slug.
1670
		 * @return object Plugins_api response object on success, WP_Error on failure.
1671
		 */
1672
		protected function get_plugins_api( $slug ) {
1673
			static $api = array(); // Cache received responses.
1674
1675
			if ( ! isset( $api[ $slug ] ) ) {
1676
				if ( ! function_exists( 'plugins_api' ) ) {
1677
					require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
1678
				}
1679
1680
				$response = plugins_api(
1681
					'plugin_information',
1682
					array(
1683
						'slug'   => $slug,
1684
						'fields' => array(
1685
							'sections' => false,
1686
						),
1687
					)
1688
				);
1689
1690
				$api[ $slug ] = false;
1691
1692
				if ( is_wp_error( $response ) ) {
1693
					wp_die( esc_html( $this->strings['oops'] ) );
1694
				} else {
1695
					$api[ $slug ] = $response;
1696
				}
1697
			}
1698
1699
			return $api[ $slug ];
1700
		}
1701
1702
		/**
1703
		 * Retrieve a link to a plugin information page.
1704
		 *
1705
		 * @since 2.5.0
1706
		 *
1707
		 * @param string $slug Plugin slug.
1708
		 * @return string Fully formed html link to a plugin information page if available
1709
		 *                or the plugin name if not.
1710
		 */
1711
		public function get_info_link( $slug ) {
1712
			if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) {
1713
				$link = sprintf(
1714
					'<a href="%1$s" target="_blank">%2$s</a>',
1715
					esc_url( $this->plugins[ $slug ]['external_url'] ),
1716
					esc_html( $this->plugins[ $slug ]['name'] )
1717
				);
1718
			} elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) {
1719
				$url = add_query_arg(
1720
					array(
1721
						'tab'       => 'plugin-information',
1722
						'plugin'    => urlencode( $slug ),
1723
						'TB_iframe' => 'true',
1724
						'width'     => '640',
1725
						'height'    => '500',
1726
					),
1727
					self_admin_url( 'plugin-install.php' )
1728
				);
1729
1730
				$link = sprintf(
1731
					'<a href="%1$s" class="thickbox">%2$s</a>',
1732
					esc_url( $url ),
1733
					esc_html( $this->plugins[ $slug ]['name'] )
1734
				);
1735
			} else {
1736
				$link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink.
1737
			}
1738
1739
			return $link;
1740
		}
1741
1742
		/**
1743
		 * Determine if we're on the TGMPA Install page.
1744
		 *
1745
		 * @since 2.1.0
1746
		 *
1747
		 * @return boolean True when on the TGMPA page, false otherwise.
1748
		 */
1749
		protected function is_tgmpa_page() {
1750
			return isset( $_GET['page'] ) && $this->menu === $_GET['page'];
1751
		}
1752
1753
		/**
1754
		 * Determine if we're on a WP Core installation/upgrade page.
1755
		 *
1756
		 * @since 2.6.0
1757
		 *
1758
		 * @return boolean True when on a WP Core installation/upgrade page, false otherwise.
1759
		 */
1760
		protected function is_core_update_page() {
1761
			// Current screen is not always available, most notably on the customizer screen.
1762
			if ( ! function_exists( 'get_current_screen' ) ) {
1763
				return false;
1764
			}
1765
1766
			$screen = get_current_screen();
1767
1768
			if ( 'update-core' === $screen->base ) {
1769
				// Core update screen.
1770
				return true;
1771
			} elseif ( 'plugins' === $screen->base && ! empty( $_POST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1772
				// Plugins bulk update screen.
1773
				return true;
1774
			} elseif ( 'update' === $screen->base && ! empty( $_POST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1775
				// Individual updates (ajax call).
1776
				return true;
1777
			}
1778
1779
			return false;
1780
		}
1781
1782
		/**
1783
		 * Retrieve the URL to the TGMPA Install page.
1784
		 *
1785
		 * I.e. depending on the config settings passed something along the lines of:
1786
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins
1787
		 *
1788
		 * @since 2.5.0
1789
		 *
1790
		 * @return string Properly encoded URL (not escaped).
1791
		 */
1792
		public function get_tgmpa_url() {
1793
			static $url;
1794
1795
			if ( ! isset( $url ) ) {
1796
				$parent = $this->parent_slug;
1797
				if ( false === strpos( $parent, '.php' ) ) {
1798
					$parent = 'admin.php';
1799
				}
1800
				$url = add_query_arg(
1801
					array(
1802
						'page' => urlencode( $this->menu ),
1803
					),
1804
					self_admin_url( $parent )
1805
				);
1806
			}
1807
1808
			return $url;
1809
		}
1810
1811
		/**
1812
		 * Retrieve the URL to the TGMPA Install page for a specific plugin status (view).
1813
		 *
1814
		 * I.e. depending on the config settings passed something along the lines of:
1815
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install
1816
		 *
1817
		 * @since 2.5.0
1818
		 *
1819
		 * @param string $status Plugin status - either 'install', 'update' or 'activate'.
1820
		 * @return string Properly encoded URL (not escaped).
1821
		 */
1822
		public function get_tgmpa_status_url( $status ) {
1823
			return add_query_arg(
1824
				array(
1825
					'plugin_status' => urlencode( $status ),
1826
				),
1827
				$this->get_tgmpa_url()
1828
			);
1829
		}
1830
1831
		/**
1832
		 * Determine whether there are open actions for plugins registered with TGMPA.
1833
		 *
1834
		 * @since 2.5.0
1835
		 *
1836
		 * @return bool True if complete, i.e. no outstanding actions. False otherwise.
1837
		 */
1838
		public function is_tgmpa_complete() {
1839
			$complete = true;
1840
			foreach ( $this->plugins as $slug => $plugin ) {
1841
				if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1842
					$complete = false;
1843
					break;
1844
				}
1845
			}
1846
1847
			return $complete;
1848
		}
1849
1850
		/**
1851
		 * Check if a plugin is installed. Does not take must-use plugins into account.
1852
		 *
1853
		 * @since 2.5.0
1854
		 *
1855
		 * @param string $slug Plugin slug.
1856
		 * @return bool True if installed, false otherwise.
1857
		 */
1858
		public function is_plugin_installed( $slug ) {
1859
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1860
1861
			return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) );
1862
		}
1863
1864
		/**
1865
		 * Check if a plugin is active.
1866
		 *
1867
		 * @since 2.5.0
1868
		 *
1869
		 * @param string $slug Plugin slug.
1870
		 * @return bool True if active, false otherwise.
1871
		 */
1872
		public function is_plugin_active( $slug ) {
1873
			return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) );
1874
		}
1875
1876
		/**
1877
		 * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required
1878
		 * available, check whether the current install meets them.
1879
		 *
1880
		 * @since 2.5.0
1881
		 *
1882
		 * @param string $slug Plugin slug.
1883
		 * @return bool True if OK to update, false otherwise.
1884
		 */
1885
		public function can_plugin_update( $slug ) {
0 ignored issues
show
Coding Style introduced by
function can_plugin_update() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
1886
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1887
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1888
				return true;
1889
			}
1890
1891
			$api = $this->get_plugins_api( $slug );
1892
1893
			if ( false !== $api && isset( $api->requires ) ) {
1894
				return version_compare( $this->wp_version, $api->requires, '>=' );
1895
			}
1896
1897
			// No usable info received from the plugins API, presume we can update.
1898
			return true;
1899
		}
1900
1901
		/**
1902
		 * Check to see if the plugin is 'updatetable', i.e. installed, with an update available
1903
		 * and no WP version requirements blocking it.
1904
		 *
1905
		 * @since 2.6.0
1906
		 *
1907
		 * @param string $slug Plugin slug.
1908
		 * @return bool True if OK to proceed with update, false otherwise.
1909
		 */
1910
		public function is_plugin_updatetable( $slug ) {
1911
			if ( ! $this->is_plugin_installed( $slug ) ) {
1912
				return false;
1913
			} else {
1914
				return ( false !== $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) );
1915
			}
1916
		}
1917
1918
		/**
1919
		 * Check if a plugin can be activated, i.e. is not currently active and meets the minimum
1920
		 * plugin version requirements set in TGMPA (if any).
1921
		 *
1922
		 * @since 2.5.0
1923
		 *
1924
		 * @param string $slug Plugin slug.
1925
		 * @return bool True if OK to activate, false otherwise.
1926
		 */
1927
		public function can_plugin_activate( $slug ) {
0 ignored issues
show
Coding Style introduced by
function can_plugin_activate() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
1928
			return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) );
1929
		}
1930
1931
		/**
1932
		 * Retrieve the version number of an installed plugin.
1933
		 *
1934
		 * @since 2.5.0
1935
		 *
1936
		 * @param string $slug Plugin slug.
1937
		 * @return string Version number as string or an empty string if the plugin is not installed
1938
		 *                or version unknown (plugins which don't comply with the plugin header standard).
1939
		 */
1940
		public function get_installed_version( $slug ) {
1941
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1942
1943
			if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) {
1944
				return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'];
1945
			}
1946
1947
			return '';
1948
		}
1949
1950
		/**
1951
		 * Check whether a plugin complies with the minimum version requirements.
1952
		 *
1953
		 * @since 2.5.0
1954
		 *
1955
		 * @param string $slug Plugin slug.
1956
		 * @return bool True when a plugin needs to be updated, otherwise false.
1957
		 */
1958
		public function does_plugin_require_update( $slug ) {
0 ignored issues
show
Coding Style introduced by
function does_plugin_require_update() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
1959
			$installed_version = $this->get_installed_version( $slug );
1960
			$minimum_version   = $this->plugins[ $slug ]['version'];
1961
1962
			return version_compare( $minimum_version, $installed_version, '>' );
1963
		}
1964
1965
		/**
1966
		 * Check whether there is an update available for a plugin.
1967
		 *
1968
		 * @since 2.5.0
1969
		 *
1970
		 * @param string $slug Plugin slug.
1971
		 * @return false|string Version number string of the available update or false if no update available.
1972
		 */
1973
		public function does_plugin_have_update( $slug ) {
1974
			// Presume bundled and external plugins will point to a package which meets the minimum required version.
1975
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1976
				if ( $this->does_plugin_require_update( $slug ) ) {
1977
					return $this->plugins[ $slug ]['version'];
1978
				}
1979
1980
				return false;
1981
			}
1982
1983
			$repo_updates = get_site_transient( 'update_plugins' );
1984
1985 View Code Duplication
			if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) {
1986
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version;
1987
			}
1988
1989
			return false;
1990
		}
1991
1992
		/**
1993
		 * Retrieve potential upgrade notice for a plugin.
1994
		 *
1995
		 * @since 2.5.0
1996
		 *
1997
		 * @param string $slug Plugin slug.
1998
		 * @return string The upgrade notice or an empty string if no message was available or provided.
1999
		 */
2000
		public function get_upgrade_notice( $slug ) {
2001
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
2002
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
2003
				return '';
2004
			}
2005
2006
			$repo_updates = get_site_transient( 'update_plugins' );
2007
2008 View Code Duplication
			if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) {
2009
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice;
2010
			}
2011
2012
			return '';
2013
		}
2014
2015
		/**
2016
		 * Wrapper around the core WP get_plugins function, making sure it's actually available.
2017
		 *
2018
		 * @since 2.5.0
2019
		 *
2020
		 * @param string $plugin_folder Optional. Relative path to single plugin folder.
2021
		 * @return array Array of installed plugins with plugin information.
2022
		 */
2023
		public function get_plugins( $plugin_folder = '' ) {
2024
			if ( ! function_exists( 'get_plugins' ) ) {
2025
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
2026
			}
2027
2028
			return get_plugins( $plugin_folder );
2029
		}
2030
2031
		/**
2032
		 * Delete dismissable nag option when theme is switched.
2033
		 *
2034
		 * This ensures that the user(s) is/are again reminded via nag of required
2035
		 * and/or recommended plugins if they re-activate the theme.
2036
		 *
2037
		 * @since 2.1.1
2038
		 */
2039
		public function update_dismiss() {
2040
			delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true );
2041
		}
2042
2043
		/**
2044
		 * Forces plugin activation if the parameter 'force_activation' is
2045
		 * set to true.
2046
		 *
2047
		 * This allows theme authors to specify certain plugins that must be
2048
		 * active at all times while using the current theme.
2049
		 *
2050
		 * Please take special care when using this parameter as it has the
2051
		 * potential to be harmful if not used correctly. Setting this parameter
2052
		 * to true will not allow the specified plugin to be deactivated unless
2053
		 * the user switches themes.
2054
		 *
2055
		 * @since 2.2.0
2056
		 */
2057
		public function force_activation() {
2058
			foreach ( $this->plugins as $slug => $plugin ) {
2059
				if ( true === $plugin['force_activation'] ) {
2060
					if ( ! $this->is_plugin_installed( $slug ) ) {
2061
						// Oops, plugin isn't there so iterate to next condition.
2062
						continue;
2063
					} elseif ( $this->can_plugin_activate( $slug ) ) {
2064
						// There we go, activate the plugin.
2065
						activate_plugin( $plugin['file_path'] );
2066
					}
2067
				}
2068
			}
2069
		}
2070
2071
		/**
2072
		 * Forces plugin deactivation if the parameter 'force_deactivation'
2073
		 * is set to true and adds the plugin to the 'recently active' plugins list.
2074
		 *
2075
		 * This allows theme authors to specify certain plugins that must be
2076
		 * deactivated upon switching from the current theme to another.
2077
		 *
2078
		 * Please take special care when using this parameter as it has the
2079
		 * potential to be harmful if not used correctly.
2080
		 *
2081
		 * @since 2.2.0
2082
		 */
2083
		public function force_deactivation() {
2084
			$deactivated = array();
2085
2086
			foreach ( $this->plugins as $slug => $plugin ) {
2087
				/*
2088
				 * Only proceed forward if the parameter is set to true and plugin is active
2089
				 * as a 'normal' (not must-use) plugin.
2090
				 */
2091
				if ( true === $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) ) {
2092
					deactivate_plugins( $plugin['file_path'] );
2093
					$deactivated[ $plugin['file_path'] ] = time();
2094
				}
2095
			}
2096
2097
			if ( ! empty( $deactivated ) ) {
2098
				update_option( 'recently_activated', $deactivated + (array) get_option( 'recently_activated' ) );
2099
			}
2100
		}
2101
2102
		/**
2103
		 * Echo the current TGMPA version number to the page.
2104
		 *
2105
		 * @since 2.5.0
2106
		 */
2107
		public function show_tgmpa_version() {
2108
			echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',
2109
				esc_html(
2110
					sprintf(
2111
						/* translators: %s: version number */
2112
						__( 'TGMPA v%s', 'tgmpa' ),
2113
						self::TGMPA_VERSION
2114
					)
2115
				),
2116
				'</small></strong></p>';
2117
		}
2118
2119
		/**
2120
		 * Adds CSS to admin head.
2121
		 *
2122
		 * @since 2.6.2
2123
		 */
2124
		public function admin_css() {
2125
			if ( ! $this->is_tgmpa_page() ) {
2126
				return;
2127
			}
2128
2129
			echo '
2130
			<style>
2131
			#tgmpa-plugins .tgmpa-type-required > th {
2132
				border-left: 3px solid #dc3232;
2133
			}
2134
			</style>';
2135
		}
2136
2137
		/**
2138
		 * Returns the singleton instance of the class.
2139
		 *
2140
		 * @since 2.4.0
2141
		 *
2142
		 * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object.
2143
		 */
2144
		public static function get_instance() {
2145
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
2146
				self::$instance = new self();
2147
			}
2148
2149
			return self::$instance;
2150
		}
2151
	}
2152
2153
	if ( ! function_exists( 'load_tgm_plugin_activation' ) ) {
2154
		/**
2155
		 * Ensure only one instance of the class is ever invoked.
2156
		 *
2157
		 * @since 2.5.0
2158
		 */
2159
		function load_tgm_plugin_activation() {
2160
			$GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance();
2161
		}
2162
	}
2163
2164
	if ( did_action( 'plugins_loaded' ) ) {
2165
		load_tgm_plugin_activation();
2166
	} else {
2167
		add_action( 'plugins_loaded', 'load_tgm_plugin_activation' );
2168
	}
2169
}
2170
2171
if ( ! function_exists( 'tgmpa' ) ) {
2172
	/**
2173
	 * Helper function to register a collection of required plugins.
2174
	 *
2175
	 * @since 2.0.0
2176
	 * @api
2177
	 *
2178
	 * @param array $plugins An array of plugin arrays.
2179
	 * @param array $config  Optional. An array of configuration values.
2180
	 */
2181
	function tgmpa( $plugins, $config = array() ) {
2182
		$instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2183
2184
		foreach ( $plugins as $plugin ) {
2185
			call_user_func( array( $instance, 'register' ), $plugin );
2186
		}
2187
2188
		if ( ! empty( $config ) && is_array( $config ) ) {
2189
			// Send out notices for deprecated arguments passed.
2190
			if ( isset( $config['notices'] ) ) {
2191
				_deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' );
2192
				if ( ! isset( $config['has_notices'] ) ) {
2193
					$config['has_notices'] = $config['notices'];
2194
				}
2195
			}
2196
2197
			if ( isset( $config['parent_menu_slug'] ) ) {
2198
				_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.' );
2199
			}
2200
			if ( isset( $config['parent_url_slug'] ) ) {
2201
				_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.' );
2202
			}
2203
2204
			call_user_func( array( $instance, 'config' ), $config );
2205
		}
2206
	}
2207
}
2208
2209
/**
2210
 * WP_List_Table isn't always available. If it isn't available,
2211
 * we load it here.
2212
 *
2213
 * @since 2.2.0
2214
 */
2215
if ( ! class_exists( 'WP_List_Table' ) ) {
2216
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
2217
}
2218
2219
if ( ! class_exists( 'TGMPA_List_Table' ) ) {
2220
2221
	/**
2222
	 * List table class for handling plugins.
2223
	 *
2224
	 * Extends the WP_List_Table class to provide a future-compatible
2225
	 * way of listing out all required/recommended plugins.
2226
	 *
2227
	 * Gives users an interface similar to the Plugin Administration
2228
	 * area with similar (albeit stripped down) capabilities.
2229
	 *
2230
	 * This class also allows for the bulk install of plugins.
2231
	 *
2232
	 * @since 2.2.0
2233
	 *
2234
	 * @package TGM-Plugin-Activation
2235
	 * @author  Thomas Griffin
2236
	 * @author  Gary Jones
2237
	 */
2238
	class TGMPA_List_Table extends WP_List_Table {
2239
		/**
2240
		 * TGMPA instance.
2241
		 *
2242
		 * @since 2.5.0
2243
		 *
2244
		 * @var object
2245
		 */
2246
		protected $tgmpa;
2247
2248
		/**
2249
		 * The currently chosen view.
2250
		 *
2251
		 * @since 2.5.0
2252
		 *
2253
		 * @var string One of: 'all', 'install', 'update', 'activate'
2254
		 */
2255
		public $view_context = 'all';
2256
2257
		/**
2258
		 * The plugin counts for the various views.
2259
		 *
2260
		 * @since 2.5.0
2261
		 *
2262
		 * @var array
2263
		 */
2264
		protected $view_totals = array(
2265
			'all'      => 0,
2266
			'install'  => 0,
2267
			'update'   => 0,
2268
			'activate' => 0,
2269
		);
2270
2271
		/**
2272
		 * References parent constructor and sets defaults for class.
2273
		 *
2274
		 * @since 2.2.0
2275
		 */
2276
		public function __construct() {
2277
			$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2278
2279
			parent::__construct(
2280
				array(
2281
					'singular' => 'plugin',
2282
					'plural'   => 'plugins',
2283
					'ajax'     => false,
2284
				)
2285
			);
2286
2287
			if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) {
2288
				$this->view_context = sanitize_key( $_REQUEST['plugin_status'] );
2289
			}
2290
2291
			add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) );
2292
		}
2293
2294
		/**
2295
		 * Get a list of CSS classes for the <table> tag.
2296
		 *
2297
		 * Overruled to prevent the 'plural' argument from being added.
2298
		 *
2299
		 * @since 2.5.0
2300
		 *
2301
		 * @return array CSS classnames.
2302
		 */
2303
		public function get_table_classes() {
2304
			return array( 'widefat', 'fixed' );
2305
		}
2306
2307
		/**
2308
		 * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table.
2309
		 *
2310
		 * @since 2.2.0
2311
		 *
2312
		 * @return array $table_data Information for use in table.
2313
		 */
2314
		protected function _gather_plugin_data() {
0 ignored issues
show
Coding Style introduced by
function _gather_plugin_data() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-z_0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
2315
			// Load thickbox for plugin links.
2316
			$this->tgmpa->admin_init();
2317
			$this->tgmpa->thickbox();
2318
2319
			// Categorize the plugins which have open actions.
2320
			$plugins = $this->categorize_plugins_to_views();
2321
2322
			// Set the counts for the view links.
2323
			$this->set_view_totals( $plugins );
2324
2325
			// Prep variables for use and grab list of all installed plugins.
2326
			$table_data = array();
2327
			$i          = 0;
2328
2329
			// Redirect to the 'all' view if no plugins were found for the selected view context.
2330
			if ( empty( $plugins[ $this->view_context ] ) ) {
2331
				$this->view_context = 'all';
2332
			}
2333
2334
			foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) {
2335
				$table_data[ $i ]['sanitized_plugin']  = $plugin['name'];
2336
				$table_data[ $i ]['slug']              = $slug;
2337
				$table_data[ $i ]['plugin']            = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>';
2338
				$table_data[ $i ]['source']            = $this->get_plugin_source_type_text( $plugin['source_type'] );
2339
				$table_data[ $i ]['type']              = $this->get_plugin_advise_type_text( $plugin['required'] );
2340
				$table_data[ $i ]['status']            = $this->get_plugin_status_text( $slug );
2341
				$table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug );
2342
				$table_data[ $i ]['minimum_version']   = $plugin['version'];
2343
				$table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug );
2344
2345
				// Prep the upgrade notice info.
2346
				$upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug );
2347
				if ( ! empty( $upgrade_notice ) ) {
2348
					$table_data[ $i ]['upgrade_notice'] = $upgrade_notice;
2349
2350
					add_action( "tgmpa_after_plugin_row_{$slug}", array( $this, 'wp_plugin_update_row' ), 10, 2 );
2351
				}
2352
2353
				$table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin );
2354
2355
				$i++;
2356
			}
2357
2358
			return $table_data;
2359
		}
2360
2361
		/**
2362
		 * Categorize the plugins which have open actions into views for the TGMPA page.
2363
		 *
2364
		 * @since 2.5.0
2365
		 */
2366
		protected function categorize_plugins_to_views() {
2367
			$plugins = array(
2368
				'all'      => array(), // Meaning: all plugins which still have open actions.
2369
				'install'  => array(),
2370
				'update'   => array(),
2371
				'activate' => array(),
2372
			);
2373
2374
			foreach ( $this->tgmpa->plugins as $slug => $plugin ) {
2375
				if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2376
					// No need to display plugins if they are installed, up-to-date and active.
2377
					continue;
2378
				} else {
2379
					$plugins['all'][ $slug ] = $plugin;
2380
2381
					if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2382
						$plugins['install'][ $slug ] = $plugin;
2383
					} else {
2384
						if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2385
							$plugins['update'][ $slug ] = $plugin;
2386
						}
2387
2388
						if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2389
							$plugins['activate'][ $slug ] = $plugin;
2390
						}
2391
					}
2392
				}
2393
			}
2394
2395
			return $plugins;
2396
		}
2397
2398
		/**
2399
		 * Set the counts for the view links.
2400
		 *
2401
		 * @since 2.5.0
2402
		 *
2403
		 * @param array $plugins Plugins order by view.
2404
		 */
2405
		protected function set_view_totals( $plugins ) {
2406
			foreach ( $plugins as $type => $list ) {
2407
				$this->view_totals[ $type ] = count( $list );
2408
			}
2409
		}
2410
2411
		/**
2412
		 * Get the plugin required/recommended text string.
2413
		 *
2414
		 * @since 2.5.0
2415
		 *
2416
		 * @param string $required Plugin required setting.
2417
		 * @return string
2418
		 */
2419
		protected function get_plugin_advise_type_text( $required ) {
2420
			if ( true === $required ) {
2421
				return __( 'Required', 'tgmpa' );
2422
			}
2423
2424
			return __( 'Recommended', 'tgmpa' );
2425
		}
2426
2427
		/**
2428
		 * Get the plugin source type text string.
2429
		 *
2430
		 * @since 2.5.0
2431
		 *
2432
		 * @param string $type Plugin type.
2433
		 * @return string
2434
		 */
2435
		protected function get_plugin_source_type_text( $type ) {
2436
			$string = '';
2437
2438
			switch ( $type ) {
2439
				case 'repo':
2440
					$string = __( 'WordPress Repository', 'tgmpa' );
2441
					break;
2442
				case 'external':
2443
					$string = __( 'External Source', 'tgmpa' );
2444
					break;
2445
				case 'bundled':
2446
					$string = __( 'Pre-Packaged', 'tgmpa' );
2447
					break;
2448
			}
2449
2450
			return $string;
2451
		}
2452
2453
		/**
2454
		 * Determine the plugin status message.
2455
		 *
2456
		 * @since 2.5.0
2457
		 *
2458
		 * @param string $slug Plugin slug.
2459
		 * @return string
2460
		 */
2461
		protected function get_plugin_status_text( $slug ) {
2462
			if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2463
				return __( 'Not Installed', 'tgmpa' );
2464
			}
2465
2466
			if ( ! $this->tgmpa->is_plugin_active( $slug ) ) {
2467
				$install_status = __( 'Installed But Not Activated', 'tgmpa' );
2468
			} else {
2469
				$install_status = __( 'Active', 'tgmpa' );
2470
			}
2471
2472
			$update_status = '';
2473
2474
			if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2475
				$update_status = __( 'Required Update not Available', 'tgmpa' );
2476
2477
			} elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) {
2478
				$update_status = __( 'Requires Update', 'tgmpa' );
2479
2480
			} elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2481
				$update_status = __( 'Update recommended', 'tgmpa' );
2482
			}
2483
2484
			if ( '' === $update_status ) {
2485
				return $install_status;
2486
			}
2487
2488
			return sprintf(
2489
				/* translators: 1: install status, 2: update status */
2490
				_x( '%1$s, %2$s', 'Install/Update Status', 'tgmpa' ),
2491
				$install_status,
2492
				$update_status
2493
			);
2494
		}
2495
2496
		/**
2497
		 * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type.
2498
		 *
2499
		 * @since 2.5.0
2500
		 *
2501
		 * @param array $items Prepared table items.
2502
		 * @return array Sorted table items.
2503
		 */
2504
		public function sort_table_items( $items ) {
2505
			$type = array();
2506
			$name = array();
2507
2508
			foreach ( $items as $i => $plugin ) {
2509
				$type[ $i ] = $plugin['type']; // Required / recommended.
2510
				$name[ $i ] = $plugin['sanitized_plugin'];
2511
			}
2512
2513
			array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items );
2514
2515
			return $items;
2516
		}
2517
2518
		/**
2519
		 * Get an associative array ( id => link ) of the views available on this table.
2520
		 *
2521
		 * @since 2.5.0
2522
		 *
2523
		 * @return array
2524
		 */
2525
		public function get_views() {
2526
			$status_links = array();
2527
2528
			foreach ( $this->view_totals as $type => $count ) {
2529
				if ( $count < 1 ) {
2530
					continue;
2531
				}
2532
2533
				switch ( $type ) {
2534
					case 'all':
2535
						/* translators: 1: number of plugins. */
2536
						$text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' );
2537
						break;
2538
					case 'install':
2539
						/* translators: 1: number of plugins. */
2540
						$text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' );
2541
						break;
2542
					case 'update':
2543
						/* translators: 1: number of plugins. */
2544
						$text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' );
2545
						break;
2546
					case 'activate':
2547
						/* translators: 1: number of plugins. */
2548
						$text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' );
2549
						break;
2550
					default:
2551
						$text = '';
2552
						break;
2553
				}
2554
2555
				if ( ! empty( $text ) ) {
2556
2557
					$status_links[ $type ] = sprintf(
2558
						'<a href="%s"%s>%s</a>',
2559
						esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
2560
						( $type === $this->view_context ) ? ' class="current"' : '',
2561
						sprintf( $text, number_format_i18n( $count ) )
2562
					);
2563
				}
2564
			}
2565
2566
			return $status_links;
2567
		}
2568
2569
		/**
2570
		 * Create default columns to display important plugin information
2571
		 * like type, action and status.
2572
		 *
2573
		 * @since 2.2.0
2574
		 *
2575
		 * @param array  $item        Array of item data.
2576
		 * @param string $column_name The name of the column.
2577
		 * @return string
2578
		 */
2579
		public function column_default( $item, $column_name ) {
2580
			return $item[ $column_name ];
2581
		}
2582
2583
		/**
2584
		 * Required for bulk installing.
2585
		 *
2586
		 * Adds a checkbox for each plugin.
2587
		 *
2588
		 * @since 2.2.0
2589
		 *
2590
		 * @param array $item Array of item data.
2591
		 * @return string The input checkbox with all necessary info.
2592
		 */
2593
		public function column_cb( $item ) {
2594
			return sprintf(
2595
				'<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />',
2596
				esc_attr( $this->_args['singular'] ),
2597
				esc_attr( $item['slug'] ),
2598
				esc_attr( $item['sanitized_plugin'] )
2599
			);
2600
		}
2601
2602
		/**
2603
		 * Create default title column along with the action links.
2604
		 *
2605
		 * @since 2.2.0
2606
		 *
2607
		 * @param array $item Array of item data.
2608
		 * @return string The plugin name and action links.
2609
		 */
2610
		public function column_plugin( $item ) {
2611
			return sprintf(
2612
				'%1$s %2$s',
2613
				$item['plugin'],
2614
				$this->row_actions( $this->get_row_actions( $item ), true )
2615
			);
2616
		}
2617
2618
		/**
2619
		 * Create version information column.
2620
		 *
2621
		 * @since 2.5.0
2622
		 *
2623
		 * @param array $item Array of item data.
2624
		 * @return string HTML-formatted version information.
2625
		 */
2626
		public function column_version( $item ) {
2627
			$output = array();
2628
2629
			if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2630
				$installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'tgmpa' );
2631
2632
				$color = '';
2633
				if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) {
2634
					$color = ' color: #ff0000; font-weight: bold;';
2635
				}
2636
2637
				$output[] = sprintf(
2638
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'tgmpa' ) . '</p>',
2639
					$color,
2640
					$installed
2641
				);
2642
			}
2643
2644
			if ( ! empty( $item['minimum_version'] ) ) {
2645
				$output[] = sprintf(
2646
					'<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'tgmpa' ) . '</p>',
2647
					$item['minimum_version']
2648
				);
2649
			}
2650
2651
			if ( ! empty( $item['available_version'] ) ) {
2652
				$color = '';
2653
				if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) {
2654
					$color = ' color: #71C671; font-weight: bold;';
2655
				}
2656
2657
				$output[] = sprintf(
2658
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'tgmpa' ) . '</p>',
2659
					$color,
2660
					$item['available_version']
2661
				);
2662
			}
2663
2664
			if ( empty( $output ) ) {
2665
				return '&nbsp;'; // Let's not break the table layout.
2666
			} else {
2667
				return implode( "\n", $output );
2668
			}
2669
		}
2670
2671
		/**
2672
		 * Sets default message within the plugins table if no plugins
2673
		 * are left for interaction.
2674
		 *
2675
		 * Hides the menu item to prevent the user from clicking and
2676
		 * getting a permissions error.
2677
		 *
2678
		 * @since 2.2.0
2679
		 */
2680
		public function no_items() {
2681
			echo esc_html__( 'No plugins to install, update or activate.', 'tgmpa' ) . ' <a href="' . esc_url( self_admin_url() ) . '"> ' . esc_html( $this->tgmpa->strings['dashboard'] ) . '</a>';
2682
			echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
2683
		}
2684
2685
		/**
2686
		 * Output all the column information within the table.
2687
		 *
2688
		 * @since 2.2.0
2689
		 *
2690
		 * @return array $columns The column names.
2691
		 */
2692
		public function get_columns() {
2693
			$columns = array(
2694
				'cb'     => '<input type="checkbox" />',
2695
				'plugin' => __( 'Plugin', 'tgmpa' ),
2696
				'source' => __( 'Source', 'tgmpa' ),
2697
				'type'   => __( 'Type', 'tgmpa' ),
2698
			);
2699
2700
			if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
2701
				$columns['version'] = __( 'Version', 'tgmpa' );
2702
				$columns['status']  = __( 'Status', 'tgmpa' );
2703
			}
2704
2705
			return apply_filters( 'tgmpa_table_columns', $columns );
2706
		}
2707
2708
		/**
2709
		 * Get name of default primary column
2710
		 *
2711
		 * @since 2.5.0 / WP 4.3+ compatibility
2712
		 * @access protected
2713
		 *
2714
		 * @return string
2715
		 */
2716
		protected function get_default_primary_column_name() {
2717
			return 'plugin';
2718
		}
2719
2720
		/**
2721
		 * Get the name of the primary column.
2722
		 *
2723
		 * @since 2.5.0 / WP 4.3+ compatibility
2724
		 * @access protected
2725
		 *
2726
		 * @return string The name of the primary column.
2727
		 */
2728
		protected function get_primary_column_name() {
2729
			if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) {
2730
				return parent::get_primary_column_name();
2731
			} else {
2732
				return $this->get_default_primary_column_name();
2733
			}
2734
		}
2735
2736
		/**
2737
		 * Get the actions which are relevant for a specific plugin row.
2738
		 *
2739
		 * @since 2.5.0
2740
		 *
2741
		 * @param array $item Array of item data.
2742
		 * @return array Array with relevant action links.
2743
		 */
2744
		protected function get_row_actions( $item ) {
2745
			$actions      = array();
2746
			$action_links = array();
2747
2748
			// Display the 'Install' action link if the plugin is not yet available.
2749
			if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2750
				/* translators: %2$s: plugin name in screen reader markup */
2751
				$actions['install'] = __( 'Install %2$s', 'tgmpa' );
2752
			} else {
2753
				// Display the 'Update' action link if an update is available and WP complies with plugin minimum.
2754
				if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) {
2755
					/* translators: %2$s: plugin name in screen reader markup */
2756
					$actions['update'] = __( 'Update %2$s', 'tgmpa' );
2757
				}
2758
2759
				// Display the 'Activate' action link, but only if the plugin meets the minimum version.
2760
				if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) {
2761
					/* translators: %2$s: plugin name in screen reader markup */
2762
					$actions['activate'] = __( 'Activate %2$s', 'tgmpa' );
2763
				}
2764
			}
2765
2766
			// Create the actual links.
2767
			foreach ( $actions as $action => $text ) {
2768
				$nonce_url = wp_nonce_url(
2769
					add_query_arg(
2770
						array(
2771
							'plugin'           => urlencode( $item['slug'] ),
2772
							'tgmpa-' . $action => $action . '-plugin',
2773
						),
2774
						$this->tgmpa->get_tgmpa_url()
2775
					),
2776
					'tgmpa-' . $action,
2777
					'tgmpa-nonce'
2778
				);
2779
2780
				$action_links[ $action ] = sprintf(
2781
					'<a href="%1$s">' . esc_html( $text ) . '</a>', // $text contains the second placeholder.
2782
					esc_url( $nonce_url ),
2783
					'<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>'
2784
				);
2785
			}
2786
2787
			$prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : '';
2788
			return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context );
2789
		}
2790
2791
		/**
2792
		 * Generates content for a single row of the table.
2793
		 *
2794
		 * @since 2.5.0
2795
		 *
2796
		 * @param object $item The current item.
2797
		 */
2798
		public function single_row( $item ) {
2799
			echo '<tr class="' . esc_attr( 'tgmpa-type-' . strtolower( $item['type'] ) ) . '">';
2800
			$this->single_row_columns( $item );
2801
			echo '</tr>';
2802
2803
			/**
2804
			 * Fires after each specific row in the TGMPA Plugins list table.
2805
			 *
2806
			 * The dynamic portion of the hook name, `$item['slug']`, refers to the slug
2807
			 * for the plugin.
2808
			 *
2809
			 * @since 2.5.0
2810
			 */
2811
			do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context );
2812
		}
2813
2814
		/**
2815
		 * Show the upgrade notice below a plugin row if there is one.
2816
		 *
2817
		 * @since 2.5.0
2818
		 *
2819
		 * @see /wp-admin/includes/update.php
2820
		 *
2821
		 * @param string $slug Plugin slug.
2822
		 * @param array  $item The information available in this table row.
2823
		 * @return null Return early if upgrade notice is empty.
2824
		 */
2825
		public function wp_plugin_update_row( $slug, $item ) {
2826
			if ( empty( $item['upgrade_notice'] ) ) {
2827
				return;
2828
			}
2829
2830
			echo '
2831
				<tr class="plugin-update-tr">
2832
					<td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange">
2833
						<div class="update-message">',
2834
							esc_html__( 'Upgrade message from the plugin author:', 'tgmpa' ),
2835
							' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong>
2836
						</div>
2837
					</td>
2838
				</tr>';
2839
		}
2840
2841
		/**
2842
		 * Extra controls to be displayed between bulk actions and pagination.
2843
		 *
2844
		 * @since 2.5.0
2845
		 *
2846
		 * @param string $which Either 'top' or 'bottom' table navigation.
2847
		 */
2848
		public function extra_tablenav( $which ) {
2849
			if ( 'bottom' === $which ) {
2850
				$this->tgmpa->show_tgmpa_version();
2851
			}
2852
		}
2853
2854
		/**
2855
		 * Defines the bulk actions for handling registered plugins.
2856
		 *
2857
		 * @since 2.2.0
2858
		 *
2859
		 * @return array $actions The bulk actions for the plugin install table.
2860
		 */
2861
		public function get_bulk_actions() {
2862
2863
			$actions = array();
2864
2865
			if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) {
2866
				if ( current_user_can( 'install_plugins' ) ) {
2867
					$actions['tgmpa-bulk-install'] = __( 'Install', 'tgmpa' );
2868
				}
2869
			}
2870
2871
			if ( 'install' !== $this->view_context ) {
2872
				if ( current_user_can( 'update_plugins' ) ) {
2873
					$actions['tgmpa-bulk-update'] = __( 'Update', 'tgmpa' );
2874
				}
2875
				if ( current_user_can( 'activate_plugins' ) ) {
2876
					$actions['tgmpa-bulk-activate'] = __( 'Activate', 'tgmpa' );
2877
				}
2878
			}
2879
2880
			return $actions;
2881
		}
2882
2883
		/**
2884
		 * Processes bulk installation and activation actions.
2885
		 *
2886
		 * The bulk installation process looks for the $_POST information and passes that
2887
		 * through if a user has to use WP_Filesystem to enter their credentials.
2888
		 *
2889
		 * @since 2.2.0
2890
		 */
2891
		public function process_bulk_actions() {
0 ignored issues
show
Coding Style introduced by
function process_bulk_actions() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
2892
			// Bulk installation process.
2893
			if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) {
2894
2895
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
2896
2897
				$install_type = 'install';
2898
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2899
					$install_type = 'update';
2900
				}
2901
2902
				$plugins_to_install = array();
2903
2904
				// Did user actually select any plugins to install/update ?
2905 View Code Duplication
				if ( empty( $_POST['plugin'] ) ) {
2906
					if ( 'install' === $install_type ) {
2907
						$message = __( 'No plugins were selected to be installed. No action taken.', 'tgmpa' );
2908
					} else {
2909
						$message = __( 'No plugins were selected to be updated. No action taken.', 'tgmpa' );
2910
					}
2911
2912
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2913
2914
					return false;
2915
				}
2916
2917
				if ( is_array( $_POST['plugin'] ) ) {
2918
					$plugins_to_install = (array) $_POST['plugin'];
2919
				} elseif ( is_string( $_POST['plugin'] ) ) {
2920
					// Received via Filesystem page - un-flatten array (WP bug #19643).
2921
					$plugins_to_install = explode( ',', $_POST['plugin'] );
2922
				}
2923
2924
				// Sanitize the received input.
2925
				$plugins_to_install = array_map( 'urldecode', $plugins_to_install );
2926
				$plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install );
2927
2928
				// Validate the received input.
2929
				foreach ( $plugins_to_install as $key => $slug ) {
2930
					// Check if the plugin was registered with TGMPA and remove if not.
2931
					if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) {
2932
						unset( $plugins_to_install[ $key ] );
2933
						continue;
2934
					}
2935
2936
					// For install: make sure this is a plugin we *can* install and not one already installed.
2937
					if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) {
2938
						unset( $plugins_to_install[ $key ] );
2939
					}
2940
2941
					// For updates: make sure this is a plugin we *can* update (update available and WP version ok).
2942
					if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) {
2943
						unset( $plugins_to_install[ $key ] );
2944
					}
2945
				}
2946
2947
				// No need to proceed further if we have no plugins to handle.
2948 View Code Duplication
				if ( empty( $plugins_to_install ) ) {
2949
					if ( 'install' === $install_type ) {
2950
						$message = __( 'No plugins are available to be installed at this time.', 'tgmpa' );
2951
					} else {
2952
						$message = __( 'No plugins are available to be updated at this time.', 'tgmpa' );
2953
					}
2954
2955
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2956
2957
					return false;
2958
				}
2959
2960
				// Pass all necessary information if WP_Filesystem is needed.
2961
				$url = wp_nonce_url(
2962
					$this->tgmpa->get_tgmpa_url(),
2963
					'bulk-' . $this->_args['plural']
2964
				);
2965
2966
				// Give validated data back to $_POST which is the only place the filesystem looks for extra fields.
2967
				$_POST['plugin'] = implode( ',', $plugins_to_install ); // Work around for WP bug #19643.
2968
2969
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
2970
				$fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
2971
2972
				$creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields );
2973
				if ( false === $creds ) {
2974
					return true; // Stop the normal page form from displaying, credential request form will be shown.
2975
				}
2976
2977
				// Now we have some credentials, setup WP_Filesystem.
2978 View Code Duplication
				if ( ! WP_Filesystem( $creds ) ) {
2979
					// Our credentials were no good, ask the user for them again.
2980
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
2981
2982
					return true;
2983
				}
2984
2985
				/* If we arrive here, we have the filesystem */
2986
2987
				// Store all information in arrays since we are processing a bulk installation.
2988
				$names      = array();
2989
				$sources    = array(); // Needed for installs.
2990
				$file_paths = array(); // Needed for upgrades.
2991
				$to_inject  = array(); // Information to inject into the update_plugins transient.
2992
2993
				// Prepare the data for validated plugins for the install/upgrade.
2994
				foreach ( $plugins_to_install as $slug ) {
2995
					$name   = $this->tgmpa->plugins[ $slug ]['name'];
2996
					$source = $this->tgmpa->get_download_url( $slug );
2997
2998
					if ( ! empty( $name ) && ! empty( $source ) ) {
2999
						$names[] = $name;
3000
3001
						switch ( $install_type ) {
3002
3003
							case 'install':
3004
								$sources[] = $source;
3005
								break;
3006
3007
							case 'update':
3008
								$file_paths[]                 = $this->tgmpa->plugins[ $slug ]['file_path'];
3009
								$to_inject[ $slug ]           = $this->tgmpa->plugins[ $slug ];
3010
								$to_inject[ $slug ]['source'] = $source;
3011
								break;
3012
						}
3013
					}
3014
				}
3015
				unset( $slug, $name, $source );
3016
3017
				// Create a new instance of TGMPA_Bulk_Installer.
3018
				$installer = new TGMPA_Bulk_Installer(
3019
					new TGMPA_Bulk_Installer_Skin(
3020
						array(
3021
							'url'          => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),
3022
							'nonce'        => 'bulk-' . $this->_args['plural'],
3023
							'names'        => $names,
3024
							'install_type' => $install_type,
3025
						)
3026
					)
3027
				);
3028
3029
				// Wrap the install process with the appropriate HTML.
3030
				echo '<div class="tgmpa">',
3031
					'<h2 style="font-size: 23px; font-weight: 400; line-height: 29px; margin: 0; padding: 9px 15px 4px 0;">', esc_html( get_admin_page_title() ), '</h2>
3032
					<div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">';
3033
3034
				// Process the bulk installation submissions.
3035
				add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
3036
3037
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
3038
					// Inject our info into the update transient.
3039
					$this->tgmpa->inject_update_info( $to_inject );
3040
3041
					$installer->bulk_upgrade( $file_paths );
3042
				} else {
3043
					$installer->bulk_install( $sources );
3044
				}
3045
3046
				remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1 );
3047
3048
				echo '</div></div>';
3049
3050
				return true;
3051
			}
3052
3053
			// Bulk activation process.
3054
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3055
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
3056
3057
				// Did user actually select any plugins to activate ?
3058
				if ( empty( $_POST['plugin'] ) ) {
3059
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>';
3060
3061
					return false;
3062
				}
3063
3064
				// Grab plugin data from $_POST.
3065
				$plugins = array();
3066
				if ( isset( $_POST['plugin'] ) ) {
3067
					$plugins = array_map( 'urldecode', (array) $_POST['plugin'] );
3068
					$plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins );
3069
				}
3070
3071
				$plugins_to_activate = array();
3072
				$plugin_names        = array();
3073
3074
				// Grab the file paths for the selected & inactive plugins from the registration array.
3075
				foreach ( $plugins as $slug ) {
3076
					if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
3077
						$plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path'];
3078
						$plugin_names[]        = $this->tgmpa->plugins[ $slug ]['name'];
3079
					}
3080
				}
3081
				unset( $slug );
3082
3083
				// Return early if there are no plugins to activate.
3084
				if ( empty( $plugins_to_activate ) ) {
3085
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>';
3086
3087
					return false;
3088
				}
3089
3090
				// Now we are good to go - let's start activating plugins.
3091
				$activate = activate_plugins( $plugins_to_activate );
3092
3093
				if ( is_wp_error( $activate ) ) {
3094
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
3095
				} else {
3096
					$count        = count( $plugin_names ); // Count so we can use _n function.
3097
					$plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names );
3098
					$last_plugin  = array_pop( $plugin_names ); // Pop off last name to prep for readability.
3099
					$imploded     = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
3100
3101
					printf(
3102
						'<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
3103
						esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),
3104
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped via wrap_in_strong() method above.
3105
						$imploded
3106
					);
3107
3108
					// Update recently activated plugins option.
3109
					$recent = (array) get_option( 'recently_activated' );
3110
					foreach ( $plugins_to_activate as $plugin => $time ) {
3111
						if ( isset( $recent[ $plugin ] ) ) {
3112
							unset( $recent[ $plugin ] );
3113
						}
3114
					}
3115
					update_option( 'recently_activated', $recent );
3116
				}
3117
3118
				unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another.
3119
3120
				return true;
3121
			}
3122
3123
			return false;
3124
		}
3125
3126
		/**
3127
		 * Prepares all of our information to be outputted into a usable table.
3128
		 *
3129
		 * @since 2.2.0
3130
		 */
3131
		public function prepare_items() {
3132
			$columns               = $this->get_columns(); // Get all necessary column information.
3133
			$hidden                = array(); // No columns to hide, but we must set as an array.
3134
			$sortable              = array(); // No reason to make sortable columns.
3135
			$primary               = $this->get_primary_column_name(); // Column which has the row actions.
3136
			$this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers.
3137
3138
			// Process our bulk activations here.
3139
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3140
				$this->process_bulk_actions();
3141
			}
3142
3143
			// Store all of our plugin data into $items array so WP_List_Table can use it.
3144
			$this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() );
3145
		}
3146
3147
		/* *********** DEPRECATED METHODS *********** */
3148
3149
		/**
3150
		 * Retrieve plugin data, given the plugin name.
3151
		 *
3152
		 * @since      2.2.0
3153
		 * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead.
3154
		 * @see        TGM_Plugin_Activation::_get_plugin_data_from_name()
3155
		 *
3156
		 * @param string $name Name of the plugin, as it was registered.
3157
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
3158
		 * @return string|boolean Plugin slug if found, false otherwise.
3159
		 */
3160
		protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
0 ignored issues
show
Coding Style introduced by
function _get_plugin_data_from_name() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-z_0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
3161
			_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' );
3162
3163
			return $this->tgmpa->_get_plugin_data_from_name( $name, $data );
3164
		}
3165
	}
3166
}
3167
3168
3169
if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
3170
3171
	/**
3172
	 * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+.
3173
	 *
3174
	 * @since 2.5.2
3175
	 *
3176
	 * {@internal The TGMPA_Bulk_Installer class was originally called TGM_Bulk_Installer.
3177
	 *            For more information, see that class.}}
3178
	 */
3179
	class TGM_Bulk_Installer {
3180
	}
3181
}
3182
if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
3183
3184
	/**
3185
	 * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+.
3186
	 *
3187
	 * @since 2.5.2
3188
	 *
3189
	 * {@internal The TGMPA_Bulk_Installer_Skin class was originally called TGM_Bulk_Installer_Skin.
3190
	 *            For more information, see that class.}}
3191
	 */
3192
	class TGM_Bulk_Installer_Skin {
3193
	}
3194
}
3195
3196
/**
3197
 * The WP_Upgrader file isn't always available. If it isn't available,
3198
 * we load it here.
3199
 *
3200
 * We check to make sure no action or activation keys are set so that WordPress
3201
 * does not try to re-include the class when processing upgrades or installs outside
3202
 * of the class.
3203
 *
3204
 * @since 2.2.0
3205
 */
3206
add_action( 'admin_init', 'tgmpa_load_bulk_installer' );
3207
if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
3208
	/**
3209
	 * Load bulk installer
3210
	 */
3211
	function tgmpa_load_bulk_installer() {
3212
		// Silently fail if 2.5+ is loaded *after* an older version.
3213
		if ( ! isset( $GLOBALS['tgmpa'] ) ) {
3214
			return;
3215
		}
3216
3217
		// Get TGMPA class instance.
3218
		$tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3219
3220
		if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) {
3221
			if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
3222
				require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
3223
			}
3224
3225
			if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) {
3226
3227
				/**
3228
				 * Installer class to handle bulk plugin installations.
3229
				 *
3230
				 * Extends WP_Upgrader and customizes to suit the installation of multiple
3231
				 * plugins.
3232
				 *
3233
				 * @since 2.2.0
3234
				 *
3235
				 * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}}
3236
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
3237
				 *            This was done to prevent backward compatibility issues with v2.3.6.}}
3238
				 *
3239
				 * @package TGM-Plugin-Activation
3240
				 * @author  Thomas Griffin
3241
				 * @author  Gary Jones
3242
				 */
3243
				class TGMPA_Bulk_Installer extends Plugin_Upgrader {
3244
					/**
3245
					 * Holds result of bulk plugin installation.
3246
					 *
3247
					 * @since 2.2.0
3248
					 *
3249
					 * @var string
3250
					 */
3251
					public $result;
3252
3253
					/**
3254
					 * Flag to check if bulk installation is occurring or not.
3255
					 *
3256
					 * @since 2.2.0
3257
					 *
3258
					 * @var boolean
3259
					 */
3260
					public $bulk = false;
3261
3262
					/**
3263
					 * TGMPA instance
3264
					 *
3265
					 * @since 2.5.0
3266
					 *
3267
					 * @var object
3268
					 */
3269
					protected $tgmpa;
3270
3271
					/**
3272
					 * Whether or not the destination directory needs to be cleared ( = on update).
3273
					 *
3274
					 * @since 2.5.0
3275
					 *
3276
					 * @var bool
3277
					 */
3278
					protected $clear_destination = false;
3279
3280
					/**
3281
					 * References parent constructor and sets defaults for class.
3282
					 *
3283
					 * @since 2.2.0
3284
					 *
3285
					 * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
3286
					 */
3287
					public function __construct( $skin = null ) {
3288
						// Get TGMPA class instance.
3289
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3290
3291
						parent::__construct( $skin );
3292
3293
						if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
3294
							$this->clear_destination = true;
3295
						}
3296
3297
						if ( $this->tgmpa->is_automatic ) {
3298
							$this->activate_strings();
3299
						}
3300
3301
						add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
3302
					}
3303
3304
					/**
3305
					 * Sets the correct activation strings for the installer skin to use.
3306
					 *
3307
					 * @since 2.2.0
3308
					 */
3309
					public function activate_strings() {
3310
						$this->strings['activation_failed']  = __( 'Plugin activation failed.', 'tgmpa' );
3311
						$this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' );
3312
					}
3313
3314
					/**
3315
					 * Performs the actual installation of each plugin.
3316
					 *
3317
					 * @since 2.2.0
3318
					 *
3319
					 * @see WP_Upgrader::run()
3320
					 *
3321
					 * @param array $options The installation config options.
3322
					 * @return null|array Return early if error, array of installation data on success.
3323
					 */
3324
					public function run( $options ) {
3325
						$result = parent::run( $options );
3326
3327
						// Reset the strings in case we changed one during automatic activation.
3328
						if ( $this->tgmpa->is_automatic ) {
3329
							if ( 'update' === $this->skin->options['install_type'] ) {
3330
								$this->upgrade_strings();
3331
							} else {
3332
								$this->install_strings();
3333
							}
3334
						}
3335
3336
						return $result;
3337
					}
3338
3339
					/**
3340
					 * Processes the bulk installation of plugins.
3341
					 *
3342
					 * @since 2.2.0
3343
					 *
3344
					 * {@internal This is basically a near identical copy of the WP Core
3345
					 * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with
3346
					 * new installs instead of upgrades.
3347
					 * For ease of future synchronizations, the adjustments are clearly commented, but no other
3348
					 * comments are added. Code style has been made to comply.}}
3349
					 *
3350
					 * @see Plugin_Upgrader::bulk_upgrade()
3351
					 * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
3352
					 * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}}
3353
					 *
3354
					 * @param array $plugins The plugin sources needed for installation.
3355
					 * @param array $args    Arbitrary passed extra arguments.
3356
					 * @return array|false   Install confirmation messages on success, false on failure.
3357
					 */
3358
					public function bulk_install( $plugins, $args = array() ) {
3359
						// [TGMPA + ] Hook auto-activation in.
3360
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3361
3362
						$defaults    = array(
3363
							'clear_update_cache' => true,
3364
						);
3365
						$parsed_args = wp_parse_args( $args, $defaults );
3366
3367
						$this->init();
3368
						$this->bulk = true;
3369
3370
						$this->install_strings(); // [TGMPA + ] adjusted.
3371
3372
						/* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
3373
3374
						/* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
3375
3376
						$this->skin->header();
3377
3378
						// Connect to the Filesystem first.
3379
						$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
3380
						if ( ! $res ) {
3381
							$this->skin->footer();
3382
							return false;
3383
						}
3384
3385
						$this->skin->bulk_header();
3386
3387
						/*
3388
						 * Only start maintenance mode if:
3389
						 * - running Multisite and there are one or more plugins specified, OR
3390
						 * - a plugin with an update available is currently active.
3391
						 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
3392
						 */
3393
						$maintenance = ( is_multisite() && ! empty( $plugins ) );
3394
3395
						/*
3396
						[TGMPA - ]
3397
						foreach ( $plugins as $plugin )
3398
							$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
3399
						*/
3400
						if ( $maintenance ) {
3401
							$this->maintenance_mode( true );
3402
						}
3403
3404
						$results = array();
3405
3406
						$this->update_count   = count( $plugins );
3407
						$this->update_current = 0;
3408
						foreach ( $plugins as $plugin ) {
3409
							$this->update_current++;
3410
3411
							/*
3412
							[TGMPA - ]
3413
							$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
3414
3415
							if ( !isset( $current->response[ $plugin ] ) ) {
3416
								$this->skin->set_result('up_to_date');
3417
								$this->skin->before();
3418
								$this->skin->feedback('up_to_date');
3419
								$this->skin->after();
3420
								$results[$plugin] = true;
3421
								continue;
3422
							}
3423
3424
							// Get the URL to the zip file.
3425
							$r = $current->response[ $plugin ];
3426
3427
							$this->skin->plugin_active = is_plugin_active($plugin);
3428
							*/
3429
3430
							$result = $this->run(
3431
								array(
3432
									'package'           => $plugin, // [TGMPA + ] adjusted.
3433
									'destination'       => WP_PLUGIN_DIR,
3434
									'clear_destination' => false, // [TGMPA + ] adjusted.
3435
									'clear_working'     => true,
3436
									'is_multi'          => true,
3437
									'hook_extra'        => array(
3438
										'plugin' => $plugin,
3439
									),
3440
								)
3441
							);
3442
3443
							$results[ $plugin ] = $this->result;
3444
3445
							// Prevent credentials auth screen from displaying multiple times.
3446
							if ( false === $result ) {
3447
								break;
3448
							}
3449
						}
3450
3451
						$this->maintenance_mode( false );
3452
3453
						/**
3454
						 * Fires when the bulk upgrader process is complete.
3455
						 *
3456
						 * @since WP 3.6.0 / TGMPA 2.5.0
3457
						 *
3458
						 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
3459
						 *                              be a Theme_Upgrader or Core_Upgrade instance.
3460
						 * @param array           $data {
3461
						 *     Array of bulk item update data.
3462
						 *
3463
						 *     @type string $action   Type of action. Default 'update'.
3464
						 *     @type string $type     Type of update process. Accepts 'plugin', 'theme', or 'core'.
3465
						 *     @type bool   $bulk     Whether the update process is a bulk update. Default true.
3466
						 *     @type array  $packages Array of plugin, theme, or core packages to update.
3467
						 * }
3468
						 */
3469
						do_action(
3470
							// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WP core hook.
3471
							'upgrader_process_complete',
3472
							$this,
3473
							array(
3474
								'action'  => 'install', // [TGMPA + ] adjusted.
3475
								'type'    => 'plugin',
3476
								'bulk'    => true,
3477
								'plugins' => $plugins,
3478
							)
3479
						);
3480
3481
						$this->skin->bulk_footer();
3482
3483
						$this->skin->footer();
3484
3485
						// Cleanup our hooks, in case something else does a upgrade on this connection.
3486
						/* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
3487
3488
						// [TGMPA + ] Remove our auto-activation hook.
3489
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3490
3491
						// Force refresh of plugin update information.
3492
						wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
3493
3494
						return $results;
3495
					}
3496
3497
					/**
3498
					 * Handle a bulk upgrade request.
3499
					 *
3500
					 * @since 2.5.0
3501
					 *
3502
					 * @see Plugin_Upgrader::bulk_upgrade()
3503
					 *
3504
					 * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
3505
					 * @param array $args    Arbitrary passed extra arguments.
3506
					 * @return string|bool Install confirmation messages on success, false on failure.
3507
					 */
3508
					public function bulk_upgrade( $plugins, $args = array() ) {
3509
3510
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3511
3512
						$result = parent::bulk_upgrade( $plugins, $args );
3513
3514
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3515
3516
						return $result;
3517
					}
3518
3519
					/**
3520
					 * Abuse a filter to auto-activate plugins after installation.
3521
					 *
3522
					 * Hooked into the 'upgrader_post_install' filter hook.
3523
					 *
3524
					 * @since 2.5.0
3525
					 *
3526
					 * @param bool $bool The value we need to give back (true).
3527
					 * @return bool
3528
					 */
3529
					public function auto_activate( $bool ) {
0 ignored issues
show
Coding Style introduced by
function auto_activate() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
3530
						// Only process the activation of installed plugins if the automatic flag is set to true.
3531
						if ( $this->tgmpa->is_automatic ) {
3532
							// Flush plugins cache so the headers of the newly installed plugins will be read correctly.
3533
							wp_clean_plugins_cache();
3534
3535
							// Get the installed plugin file.
3536
							$plugin_info = $this->plugin_info();
3537
3538
							// Don't try to activate on upgrade of active plugin as WP will do this already.
3539
							if ( ! is_plugin_active( $plugin_info ) ) {
3540
								$activate = activate_plugin( $plugin_info );
3541
3542
								// Adjust the success string based on the activation result.
3543
								$this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
3544
3545
								if ( is_wp_error( $activate ) ) {
3546
									$this->skin->error( $activate );
3547
									$this->strings['process_success'] .= $this->strings['activation_failed'];
3548
								} else {
3549
									$this->strings['process_success'] .= $this->strings['activation_success'];
3550
								}
3551
							}
3552
						}
3553
3554
						return $bool;
3555
					}
3556
				}
3557
			}
3558
3559
			if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) {
3560
3561
				/**
3562
				 * Installer skin to set strings for the bulk plugin installations..
3563
				 *
3564
				 * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
3565
				 * plugins.
3566
				 *
3567
				 * @since 2.2.0
3568
				 *
3569
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to
3570
				 *            TGMPA_Bulk_Installer_Skin.
3571
				 *            This was done to prevent backward compatibility issues with v2.3.6.}}
3572
				 *
3573
				 * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php
3574
				 *
3575
				 * @package TGM-Plugin-Activation
3576
				 * @author  Thomas Griffin
3577
				 * @author  Gary Jones
3578
				 */
3579
				class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
3580
					/**
3581
					 * Holds plugin info for each individual plugin installation.
3582
					 *
3583
					 * @since 2.2.0
3584
					 *
3585
					 * @var array
3586
					 */
3587
					public $plugin_info = array();
3588
3589
					/**
3590
					 * Holds names of plugins that are undergoing bulk installations.
3591
					 *
3592
					 * @since 2.2.0
3593
					 *
3594
					 * @var array
3595
					 */
3596
					public $plugin_names = array();
3597
3598
					/**
3599
					 * Integer to use for iteration through each plugin installation.
3600
					 *
3601
					 * @since 2.2.0
3602
					 *
3603
					 * @var integer
3604
					 */
3605
					public $i = 0;
3606
3607
					/**
3608
					 * TGMPA instance
3609
					 *
3610
					 * @since 2.5.0
3611
					 *
3612
					 * @var object
3613
					 */
3614
					protected $tgmpa;
3615
3616
					/**
3617
					 * Constructor. Parses default args with new ones and extracts them for use.
3618
					 *
3619
					 * @since 2.2.0
3620
					 *
3621
					 * @param array $args Arguments to pass for use within the class.
3622
					 */
3623
					public function __construct( $args = array() ) {
3624
						// Get TGMPA class instance.
3625
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3626
3627
						// Parse default and new args.
3628
						$defaults = array(
3629
							'url'          => '',
3630
							'nonce'        => '',
3631
							'names'        => array(),
3632
							'install_type' => 'install',
3633
						);
3634
						$args     = wp_parse_args( $args, $defaults );
3635
3636
						// Set plugin names to $this->plugin_names property.
3637
						$this->plugin_names = $args['names'];
3638
3639
						// Extract the new args.
3640
						parent::__construct( $args );
3641
					}
3642
3643
					/**
3644
					 * Sets install skin strings for each individual plugin.
3645
					 *
3646
					 * Checks to see if the automatic activation flag is set and uses the
3647
					 * the proper strings accordingly.
3648
					 *
3649
					 * @since 2.2.0
3650
					 */
3651
					public function add_strings() {
3652
						if ( 'update' === $this->options['install_type'] ) {
3653
							parent::add_strings();
3654
							/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3655
							$this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3656
						} else {
3657
							/* translators: 1: plugin name, 2: error message. */
3658
							$this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
3659
							/* translators: 1: plugin name. */
3660
							$this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'tgmpa' );
3661
3662
							if ( $this->tgmpa->is_automatic ) {
3663
								// Automatic activation strings.
3664
								$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' );
3665
								/* translators: 1: plugin name. */
3666
								$this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'tgmpa' );
3667
								$this->upgrader->strings['skin_upgrade_end']       = __( 'All installations and activations have been completed.', 'tgmpa' );
3668
								/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3669
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3670
							} else {
3671
								// Default installation strings.
3672
								$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' );
3673
								/* translators: 1: plugin name. */
3674
								$this->upgrader->strings['skin_update_successful'] = __( '%1$s installed successfully.', 'tgmpa' );
3675
								$this->upgrader->strings['skin_upgrade_end']       = __( 'All installations have been completed.', 'tgmpa' );
3676
								/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3677
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3678
							}
3679
3680
							// Add "read more" link only for WP < 4.8.
3681
							if ( version_compare( $this->tgmpa->wp_version, '4.8', '<' ) ) {
3682
								$this->upgrader->strings['skin_update_successful'] .= ' <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>';
3683
							}
3684
						}
3685
					}
3686
3687
					/**
3688
					 * Outputs the header strings and necessary JS before each plugin installation.
3689
					 *
3690
					 * @since 2.2.0
3691
					 *
3692
					 * @param string $title Unused in this implementation.
3693
					 */
3694
					public function before( $title = '' ) {
3695
						if ( empty( $title ) ) {
3696
							$title = esc_html( $this->plugin_names[ $this->i ] );
3697
						}
3698
						parent::before( $title );
3699
					}
3700
3701
					/**
3702
					 * Outputs the footer strings and necessary JS after each plugin installation.
3703
					 *
3704
					 * Checks for any errors and outputs them if they exist, else output
3705
					 * success strings.
3706
					 *
3707
					 * @since 2.2.0
3708
					 *
3709
					 * @param string $title Unused in this implementation.
3710
					 */
3711
					public function after( $title = '' ) {
3712
						if ( empty( $title ) ) {
3713
							$title = esc_html( $this->plugin_names[ $this->i ] );
3714
						}
3715
						parent::after( $title );
3716
3717
						$this->i++;
3718
					}
3719
3720
					/**
3721
					 * Outputs links after bulk plugin installation is complete.
3722
					 *
3723
					 * @since 2.2.0
3724
					 */
3725
					public function bulk_footer() {
3726
						// Serve up the string to say installations (and possibly activations) are complete.
3727
						parent::bulk_footer();
3728
3729
						// Flush plugins cache so we can make sure that the installed plugins list is always up to date.
3730
						wp_clean_plugins_cache();
3731
3732
						$this->tgmpa->show_tgmpa_version();
3733
3734
						// Display message based on if all plugins are now active or not.
3735
						$update_actions = array();
3736
3737
						if ( $this->tgmpa->is_tgmpa_complete() ) {
3738
							// All plugins are active, so we display the complete string and hide the menu to protect users.
3739
							echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
3740
							$update_actions['dashboard'] = sprintf(
3741
								esc_html( $this->tgmpa->strings['complete'] ),
3742
								'<a href="' . esc_url( self_admin_url() ) . '">' . esc_html( $this->tgmpa->strings['dashboard'] ) . '</a>'
3743
							);
3744
						} else {
3745
							$update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>';
3746
						}
3747
3748
						/**
3749
						 * Filter the list of action links available following bulk plugin installs/updates.
3750
						 *
3751
						 * @since 2.5.0
3752
						 *
3753
						 * @param array $update_actions Array of plugin action links.
3754
						 * @param array $plugin_info    Array of information for the last-handled plugin.
3755
						 */
3756
						$update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
3757
3758
						if ( ! empty( $update_actions ) ) {
3759
							$this->feedback( implode( ' | ', (array) $update_actions ) );
3760
						}
3761
					}
3762
3763
					/* *********** DEPRECATED METHODS *********** */
3764
3765
					/**
3766
					 * Flush header output buffer.
3767
					 *
3768
					 * @since      2.2.0
3769
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3770
					 * @see        Bulk_Upgrader_Skin::flush_output()
3771
					 */
3772
					public function before_flush_output() {
3773
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3774
						$this->flush_output();
3775
					}
3776
3777
					/**
3778
					 * Flush footer output buffer and iterate $this->i to make sure the
3779
					 * installation strings reference the correct plugin.
3780
					 *
3781
					 * @since      2.2.0
3782
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3783
					 * @see        Bulk_Upgrader_Skin::flush_output()
3784
					 */
3785
					public function after_flush_output() {
3786
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3787
						$this->flush_output();
3788
						$this->i++;
3789
					}
3790
				}
3791
			}
3792
		}
3793
	}
3794
}
3795
3796
if ( ! class_exists( 'TGMPA_Utils' ) ) {
3797
3798
	/**
3799
	 * Generic utilities for TGMPA.
3800
	 *
3801
	 * All methods are static, poor-dev name-spacing class wrapper.
3802
	 *
3803
	 * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy.
3804
	 *
3805
	 * @since 2.5.0
3806
	 *
3807
	 * @package TGM-Plugin-Activation
3808
	 * @author  Juliette Reinders Folmer
3809
	 */
3810
	class TGMPA_Utils {
3811
		/**
3812
		 * Whether the PHP filter extension is enabled.
3813
		 *
3814
		 * @see http://php.net/book.filter
3815
		 *
3816
		 * @since 2.5.0
3817
		 *
3818
		 * @static
3819
		 *
3820
		 * @var bool $has_filters True is the extension is enabled.
3821
		 */
3822
		public static $has_filters;
3823
3824
		/**
3825
		 * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map().
3826
		 *
3827
		 * @since 2.5.0
3828
		 *
3829
		 * @static
3830
		 *
3831
		 * @param string $string Text to be wrapped.
3832
		 * @return string
3833
		 */
3834
		public static function wrap_in_em( $string ) {
3835
			return '<em>' . wp_kses_post( $string ) . '</em>';
3836
		}
3837
3838
		/**
3839
		 * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map().
3840
		 *
3841
		 * @since 2.5.0
3842
		 *
3843
		 * @static
3844
		 *
3845
		 * @param string $string Text to be wrapped.
3846
		 * @return string
3847
		 */
3848
		public static function wrap_in_strong( $string ) {
3849
			return '<strong>' . wp_kses_post( $string ) . '</strong>';
3850
		}
3851
3852
		/**
3853
		 * Helper function: Validate a value as boolean
3854
		 *
3855
		 * @since 2.5.0
3856
		 *
3857
		 * @static
3858
		 *
3859
		 * @param mixed $value Arbitrary value.
3860
		 * @return bool
3861
		 */
3862
		public static function validate_bool( $value ) {
3863
			if ( ! isset( self::$has_filters ) ) {
3864
				self::$has_filters = extension_loaded( 'filter' );
3865
			}
3866
3867
			if ( self::$has_filters ) {
3868
				return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
3869
			} else {
3870
				return self::emulate_filter_bool( $value );
3871
			}
3872
		}
3873
3874
		/**
3875
		 * Helper function: Cast a value to bool
3876
		 *
3877
		 * @since 2.5.0
3878
		 *
3879
		 * @static
3880
		 *
3881
		 * @param mixed $value Value to cast.
3882
		 * @return bool
3883
		 */
3884
		protected static function emulate_filter_bool( $value ) {
3885
			// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
3886
			static $true  = array(
3887
				'1',
3888
				'true', 'True', 'TRUE',
3889
				'y', 'Y',
3890
				'yes', 'Yes', 'YES',
3891
				'on', 'On', 'ON',
3892
			);
3893
			static $false = array(
3894
				'0',
3895
				'false', 'False', 'FALSE',
3896
				'n', 'N',
3897
				'no', 'No', 'NO',
3898
				'off', 'Off', 'OFF',
3899
			);
3900
			// phpcs:enable
3901
3902
			if ( is_bool( $value ) ) {
3903
				return $value;
3904
			} elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
3905
				return (bool) $value;
3906
			} elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
3907
				return (bool) $value;
3908
			} elseif ( is_string( $value ) ) {
3909
				$value = trim( $value );
3910
				if ( in_array( $value, $true, true ) ) {
3911
					return true;
3912
				} elseif ( in_array( $value, $false, true ) ) {
3913
					return false;
3914
				} else {
3915
					return false;
3916
				}
3917
			}
3918
3919
			return false;
3920
		}
3921
	} // End of class TGMPA_Utils
3922
}
3923