Completed
Pull Request — develop (#626)
by
unknown
02:40
created

class-tgm-plugin-activation.php (7 issues)

Upgrade to new PHP Analysis Engine

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

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

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

    public function myFunction() {
        // Do something
    }
}
Loading history...
651
				$body_id = 'plugin-information';
652
				// @codingStandardsIgnoreStart
653
				$tab     = 'plugin-information';
654
				// @codingStandardsIgnoreEnd
655
656
				install_plugin_information();
657
658
				exit;
659
			}
660
		}
661
662
		/**
663
		 * Enqueue thickbox scripts/styles for plugin info.
664
		 *
665
		 * Thickbox is not automatically included on all admin pages, so we must
666
		 * manually enqueue it for those pages.
667
		 *
668
		 * Thickbox is only loaded if the user has not dismissed the admin
669
		 * notice or if there are any plugins left to install and activate.
670
		 *
671
		 * @since 2.1.0
672
		 */
673
		public function thickbox() {
674
			if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
675
				add_thickbox();
676
			}
677
		}
678
679
		/**
680
		 * Adds submenu page if there are plugin actions to take.
681
		 *
682
		 * This method adds the submenu page letting users know that a required
683
		 * plugin needs to be installed.
684
		 *
685
		 * This page disappears once the plugin has been installed and activated.
686
		 *
687
		 * @since 1.0.0
688
		 *
689
		 * @see TGM_Plugin_Activation::init()
690
		 * @see TGM_Plugin_Activation::install_plugins_page()
691
		 *
692
		 * @return null Return early if user lacks capability to install a plugin.
693
		 */
694
		public function admin_menu() {
695
			// Make sure privileges are correct to see the page.
696
			if ( ! current_user_can( 'install_plugins' ) ) {
697
				return;
698
			}
699
700
			$args = apply_filters(
701
				'tgmpa_admin_menu_args',
702
				array(
703
					'parent_slug' => $this->parent_slug,                     // Parent Menu slug.
704
					'page_title'  => $this->strings['page_title'],           // Page title.
705
					'menu_title'  => $this->strings['menu_title'],           // Menu title.
706
					'capability'  => $this->capability,                      // Capability.
707
					'menu_slug'   => $this->menu,                            // Menu slug.
708
					'function'    => array( $this, 'install_plugins_page' ), // Callback.
709
				)
710
			);
711
712
			$this->add_admin_menu( $args );
713
		}
714
715
		/**
716
		 * Add the menu item.
717
		 *
718
		 * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
719
		 * generator on the website.}}
720
		 *
721
		 * @since 2.5.0
722
		 *
723
		 * @param array $args Menu item configuration.
724
		 */
725
		protected function add_admin_menu( array $args ) {
726
			if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) {
727
				_deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) );
728
			}
729
730
			if ( 'themes.php' === $this->parent_slug ) {
731
				$this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
732
			} else {
733
				$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'] );
734
			}
735
		}
736
737
		/**
738
		 * Echoes plugin installation form.
739
		 *
740
		 * This method is the callback for the admin_menu method function.
741
		 * This displays the admin page and form area where the user can select to install and activate the plugin.
742
		 * Aborts early if we're processing a plugin installation action.
743
		 *
744
		 * @since 1.0.0
745
		 *
746
		 * @return null Aborts early if we're processing a plugin installation action.
747
		 */
748
		public function install_plugins_page() {
749
			// Store new instance of plugin table in object.
750
			$plugin_table = new TGMPA_List_Table;
751
752
			// Return early if processing a plugin installation action.
753
			if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
754
				return;
755
			}
756
757
			// Force refresh of available plugin information so we'll know about manual updates/deletes.
758
			wp_clean_plugins_cache( false );
759
760
			?>
761
			<div class="tgmpa wrap">
762
				<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
763
				<?php $plugin_table->prepare_items(); ?>
764
765
				<?php
766
				if ( ! empty( $this->message ) && is_string( $this->message ) ) {
767
					echo wp_kses_post( $this->message );
768
				}
769
				?>
770
				<?php $plugin_table->views(); ?>
771
772
				<form id="tgmpa-plugins" action="" method="post">
773
					<input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
774
					<input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
775
					<?php $plugin_table->display(); ?>
776
				</form>
777
			</div>
778
			<?php
779
		}
780
781
		/**
782
		 * Installs, updates or activates a plugin depending on the action link clicked by the user.
783
		 *
784
		 * Checks the $_GET variable to see which actions have been
785
		 * passed and responds with the appropriate method.
786
		 *
787
		 * Uses WP_Filesystem to process and handle the plugin installation
788
		 * method.
789
		 *
790
		 * @since 1.0.0
791
		 *
792
		 * @uses WP_Filesystem
793
		 * @uses WP_Error
794
		 * @uses WP_Upgrader
795
		 * @uses Plugin_Upgrader
796
		 * @uses Plugin_Installer_Skin
797
		 * @uses Plugin_Upgrader_Skin
798
		 *
799
		 * @return boolean True on success, false on failure.
800
		 */
801
		protected function do_plugin_install() {
802
			if ( empty( $_GET['plugin'] ) ) {
803
				return false;
804
			}
805
806
			// All plugin information will be stored in an array for processing.
807
			$slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
808
809
			if ( ! isset( $this->plugins[ $slug ] ) ) {
810
				return false;
811
			}
812
813
			// Was an install or upgrade action link clicked?
814
			if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
815
816
				$install_type = 'install';
817
				if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
818
					$install_type = 'update';
819
				}
820
821
				check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
822
823
				// Pass necessary information via URL if WP_Filesystem is needed.
824
				$url = wp_nonce_url(
825
					add_query_arg(
826
						array(
827
							'plugin'                 => urlencode( $slug ),
828
							'tgmpa-' . $install_type => $install_type . '-plugin',
829
						),
830
						$this->get_tgmpa_url()
831
					),
832
					'tgmpa-' . $install_type,
833
					'tgmpa-nonce'
834
				);
835
836
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
837
838
				if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) {
839
					return true;
840
				}
841
842
				if ( ! WP_Filesystem( $creds ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
843
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
844
					return true;
845
				}
846
847
				/* If we arrive here, we have the filesystem. */
848
849
				// Prep variables for Plugin_Installer_Skin class.
850
				$extra         = array();
851
				$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
852
				$source        = $this->get_download_url( $slug );
853
				$api           = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
854
				$api           = ( false !== $api ) ? $api : null;
855
856
				$url = add_query_arg(
857
					array(
858
						'action' => $install_type . '-plugin',
859
						'plugin' => urlencode( $slug ),
860
					),
861
					'update.php'
862
				);
863
864
				if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
865
					require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
866
				}
867
868
				$title     = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
869
				$skin_args = array(
870
					'type'   => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
871
					'title'  => sprintf( $title, $this->plugins[ $slug ]['name'] ),
872
					'url'    => esc_url_raw( $url ),
873
					'nonce'  => $install_type . '-plugin_' . $slug,
874
					'plugin' => '',
875
					'api'    => $api,
876
					'extra'  => $extra,
877
				);
878
				unset( $title );
879
880
				if ( 'update' === $install_type ) {
881
					$skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
882
					$skin                = new Plugin_Upgrader_Skin( $skin_args );
883
				} else {
884
					$skin = new Plugin_Installer_Skin( $skin_args );
885
				}
886
887
				// Create a new instance of Plugin_Upgrader.
888
				$upgrader = new Plugin_Upgrader( $skin );
889
890
				// Perform the action and install the plugin from the $source urldecode().
891
				add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
892
893
				if ( 'update' === $install_type ) {
894
					// Inject our info into the update transient.
895
					$to_inject                    = array( $slug => $this->plugins[ $slug ] );
896
					$to_inject[ $slug ]['source'] = $source;
897
					$this->inject_update_info( $to_inject );
898
899
					$upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
900
				} else {
901
					$upgrader->install( $source );
902
				}
903
904
				remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 );
905
906
				// Make sure we have the correct file path now the plugin is installed/updated.
907
				$this->populate_file_path( $slug );
908
909
				// Only activate plugins if the config option is set to true and the plugin isn't
910
				// already active (upgrade).
911
				if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
912
					$plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
913
					if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
914
						return true; // Finish execution of the function early as we encountered an error.
915
					}
916
				}
917
918
				$this->show_tgmpa_version();
919
920
				// Display message based on if all plugins are now active or not.
921
				if ( $this->is_tgmpa_complete() ) {
922
					echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ), '</p>';
923
					echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
924
				} else {
925
					echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
926
				}
927
928
				return true;
929
			} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
930
				// Activate action link was clicked.
931
				check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
932
933
				if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
934
					return true; // Finish execution of the function early as we encountered an error.
935
				}
936
			}
937
938
			return false;
939
		}
940
941
		/**
942
		 * Inject information into the 'update_plugins' site transient as WP checks that before running an update.
943
		 *
944
		 * @since 2.5.0
945
		 *
946
		 * @param array $plugins The plugin information for the plugins which are to be updated.
947
		 */
948
		public function inject_update_info( $plugins ) {
949
			$repo_updates = get_site_transient( 'update_plugins' );
950
951
			if ( ! is_object( $repo_updates ) ) {
952
				$repo_updates = new stdClass;
953
			}
954
955
			foreach ( $plugins as $slug => $plugin ) {
956
				$file_path = $plugin['file_path'];
957
958
				if ( empty( $repo_updates->response[ $file_path ] ) ) {
959
					$repo_updates->response[ $file_path ] = new stdClass;
960
				}
961
962
				// We only really need to set package, but let's do all we can in case WP changes something.
963
				$repo_updates->response[ $file_path ]->slug        = $slug;
964
				$repo_updates->response[ $file_path ]->plugin      = $file_path;
965
				$repo_updates->response[ $file_path ]->new_version = $plugin['version'];
966
				$repo_updates->response[ $file_path ]->package     = $plugin['source'];
967
				if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
968
					$repo_updates->response[ $file_path ]->url = $plugin['external_url'];
969
				}
970
			}
971
972
			set_site_transient( 'update_plugins', $repo_updates );
973
		}
974
975
		/**
976
		 * Adjust the plugin directory name if necessary.
977
		 *
978
		 * The final destination directory of a plugin is based on the subdirectory name found in the
979
		 * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
980
		 * subdirectory name is not the same as the expected slug and the plugin will not be recognized
981
		 * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
982
		 * the expected plugin slug.
983
		 *
984
		 * @since 2.5.0
985
		 *
986
		 * @param string       $source        Path to upgrade/zip-file-name.tmp/subdirectory/.
987
		 * @param string       $remote_source Path to upgrade/zip-file-name.tmp.
988
		 * @param \WP_Upgrader $upgrader      Instance of the upgrader which installs the plugin.
989
		 * @return string $source
990
		 */
991
		public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
992
			if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
993
				return $source;
994
			}
995
996
			// Check for single file plugins.
997
			$source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
998
			if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
999
				return $source;
1000
			}
1001
1002
			// Multi-file plugin, let's see if the directory is correctly named.
1003
			$desired_slug = '';
1004
1005
			// Figure out what the slug is supposed to be.
1006
			if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
1007
				$desired_slug = $upgrader->skin->options['extra']['slug'];
1008
			} else {
1009
				// Bulk installer contains less info, so fall back on the info registered here.
1010
				foreach ( $this->plugins as $slug => $plugin ) {
1011
					if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
1012
						$desired_slug = $slug;
1013
						break;
1014
					}
1015
				}
1016
				unset( $slug, $plugin );
1017
			}
1018
1019
			if ( ! empty( $desired_slug ) ) {
1020
				$subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
1021
1022
				if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
1023
					$from_path = untrailingslashit( $source );
1024
					$to_path   = trailingslashit( $remote_source ) . $desired_slug;
1025
1026
					if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
1027
						return trailingslashit( $to_path );
1028 View Code Duplication
					} else {
1029
						return new WP_Error( 'rename_failed', esc_html__( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) );
1030
					}
1031 View Code Duplication
				} elseif ( empty( $subdir_name ) ) {
1032
					return new WP_Error( 'packaged_wrong', esc_html__( 'The remote plugin package consists of more than one file, but the files are not packaged in a folder.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) );
1033
				}
1034
			}
1035
1036
			return $source;
1037
		}
1038
1039
		/**
1040
		 * Activate a single plugin and send feedback about the result to the screen.
1041
		 *
1042
		 * @since 2.5.0
1043
		 *
1044
		 * @param string $file_path Path within wp-plugins/ to main plugin file.
1045
		 * @param string $slug      Plugin slug.
1046
		 * @param bool   $automatic Whether this is an automatic activation after an install. Defaults to false.
1047
		 *                          This determines the styling of the output messages.
1048
		 * @return bool False if an error was encountered, true otherwise.
1049
		 */
1050
		protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
1051
			if ( $this->can_plugin_activate( $slug ) ) {
1052
				$activate = activate_plugin( $file_path );
1053
1054
				if ( is_wp_error( $activate ) ) {
1055
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
1056
						'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
1057
1058
					return false; // End it here if there is an error with activation.
1059
				} else {
1060
					if ( ! $automatic ) {
1061
						// Make sure message doesn't display again if bulk activation is performed
1062
						// immediately after a single activation.
1063
						if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1064
							echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
1065
						}
1066
					} else {
1067
						// Simpler message layout for use on the plugin install page.
1068
						echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
1069
					}
1070
				}
1071 View Code Duplication
			} elseif ( $this->is_plugin_active( $slug ) ) {
1072
				// No simpler message format provided as this message should never be encountered
1073
				// on the plugin install page.
1074
				echo '<div id="message" class="error"><p>',
1075
					sprintf(
1076
						esc_html( $this->strings['plugin_already_active'] ),
1077
						'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1078
					),
1079
					'</p></div>';
1080
			} elseif ( $this->does_plugin_require_update( $slug ) ) {
1081
				if ( ! $automatic ) {
1082
					// Make sure message doesn't display again if bulk activation is performed
1083
					// immediately after a single activation.
1084 View Code Duplication
					if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1085
						echo '<div id="message" class="error"><p>',
1086
							sprintf(
1087
								esc_html( $this->strings['plugin_needs_higher_version'] ),
1088
								'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1089
							),
1090
							'</p></div>';
1091
					}
1092
				} else {
1093
					// Simpler message layout for use on the plugin install page.
1094
					echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
1095
				}
1096
			}
1097
1098
			return true;
1099
		}
1100
1101
		/**
1102
		 * Echoes required plugin notice.
1103
		 *
1104
		 * Outputs a message telling users that a specific plugin is required for
1105
		 * their theme. If appropriate, it includes a link to the form page where
1106
		 * users can install and activate the plugin.
1107
		 *
1108
		 * Returns early if we're on the Install page.
1109
		 *
1110
		 * @since 1.0.0
1111
		 *
1112
		 * @global object $current_screen
1113
		 *
1114
		 * @return null Returns early if we're on the Install page.
1115
		 */
1116
		public function notices() {
1117
			// Remove nag on the install page / Return early if the nag message has been dismissed or user < author.
1118
			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' ) ) ) {
1119
				return;
1120
			}
1121
1122
			// Store for the plugin slugs by message type.
1123
			$message = array();
1124
1125
			// Initialize counters used to determine plurality of action link texts.
1126
			$install_link_count          = 0;
1127
			$update_link_count           = 0;
1128
			$activate_link_count         = 0;
1129
			$total_required_action_count = 0;
1130
1131
			foreach ( $this->plugins as $slug => $plugin ) {
1132
				if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) {
1133
					continue;
1134
				}
1135
1136
				if ( ! $this->is_plugin_installed( $slug ) ) {
1137
					if ( current_user_can( 'install_plugins' ) ) {
1138
						$install_link_count++;
1139
1140
						if ( true === $plugin['required'] ) {
1141
							$message['notice_can_install_required'][] = $slug;
1142
						} else {
1143
							$message['notice_can_install_recommended'][] = $slug;
1144
						}
1145
					}
1146
					if ( true === $plugin['required'] ) {
1147
						$total_required_action_count++;
1148
					}
1149
				} else {
1150
					if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
1151
						if ( current_user_can( 'activate_plugins' ) ) {
1152
							$activate_link_count++;
1153
1154
							if ( true === $plugin['required'] ) {
1155
								$message['notice_can_activate_required'][] = $slug;
1156
							} else {
1157
								$message['notice_can_activate_recommended'][] = $slug;
1158
							}
1159
						}
1160
						if ( true === $plugin['required'] ) {
1161
							$total_required_action_count++;
1162
						}
1163
					}
1164
1165
					if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1166
1167
						if ( current_user_can( 'update_plugins' ) ) {
1168
							$update_link_count++;
1169
1170
							if ( $this->does_plugin_require_update( $slug ) ) {
1171
								$message['notice_ask_to_update'][] = $slug;
1172
							} elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
1173
								$message['notice_ask_to_update_maybe'][] = $slug;
1174
							}
1175
						}
1176
						if ( true === $plugin['required'] ) {
1177
							$total_required_action_count++;
1178
						}
1179
					}
1180
				}
1181
			}
1182
			unset( $slug, $plugin );
1183
1184
			// If we have notices to display, we move forward.
1185
			if ( ! empty( $message ) || $total_required_action_count > 0 ) {
1186
				krsort( $message ); // Sort messages.
1187
				$rendered = '';
1188
1189
				// As add_settings_error() wraps the final message in a <p> and as the final message can't be
1190
				// filtered, using <p>'s in our html would render invalid html output.
1191
				$line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
1192
1193
				if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
1194
					$rendered  = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] );
1195
					$rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
1196
				} else {
1197
1198
					// If dismissable is false and a message is set, output it now.
1199
					if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
1200
						$rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
1201
					}
1202
1203
					// Render the individual message lines for the notice.
1204
					foreach ( $message as $type => $plugin_group ) {
1205
						$linked_plugins = array();
1206
1207
						// Get the external info link for a plugin if one is available.
1208
						foreach ( $plugin_group as $plugin_slug ) {
1209
							$linked_plugins[] = $this->get_info_link( $plugin_slug );
1210
						}
1211
						unset( $plugin_slug );
1212
1213
						$count          = count( $plugin_group );
1214
						$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
1215
						$last_plugin    = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
1216
						$imploded       = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
1217
1218
						$rendered .= sprintf(
1219
							$line_template,
1220
							sprintf(
1221
								translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
1222
								$imploded,
1223
								$count
1224
							)
1225
						);
1226
1227
					}
1228
					unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
1229
1230
					$rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
1231
				}
1232
1233
				// Register the nag messages and prepare them to be processed.
1234
				add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
1235
			}
1236
1237
			// Admin options pages already output settings_errors, so this is to avoid duplication.
1238
			if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
1239
				$this->display_settings_errors();
1240
			}
1241
		}
1242
1243
		/**
1244
		 * Generate the user action links for the admin notice.
1245
		 *
1246
		 * @since 2.6.0
1247
		 *
1248
		 * @param int $install_count  Number of plugins to install.
1249
		 * @param int $update_count   Number of plugins to update.
1250
		 * @param int $activate_count Number of plugins to activate.
1251
		 * @param int $line_template  Template for the HTML tag to output a line.
1252
		 * @return string Action links.
1253
		 */
1254
		protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
1255
			// Setup action links.
1256
			$action_links = array(
1257
				'install'  => '',
1258
				'update'   => '',
1259
				'activate' => '',
1260
				'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>' : '',
1261
			);
1262
1263
			$link_template = '<a href="%2$s">%1$s</a>';
1264
1265
			if ( current_user_can( 'install_plugins' ) ) {
1266
				if ( $install_count > 0 ) {
1267
					$action_links['install'] = sprintf(
1268
						$link_template,
1269
						translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),
1270
						esc_url( $this->get_tgmpa_status_url( 'install' ) )
1271
					);
1272
				}
1273
				if ( $update_count > 0 ) {
1274
					$action_links['update'] = sprintf(
1275
						$link_template,
1276
						translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),
1277
						esc_url( $this->get_tgmpa_status_url( 'update' ) )
1278
					);
1279
				}
1280
			}
1281
1282
			if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
1283
				$action_links['activate'] = sprintf(
1284
					$link_template,
1285
					translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),
1286
					esc_url( $this->get_tgmpa_status_url( 'activate' ) )
1287
				);
1288
			}
1289
1290
			$action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
1291
1292
			$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
1293
1294
			if ( ! empty( $action_links ) ) {
1295
				$action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
1296
				return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
1297
			} else {
1298
				return '';
1299
			}
1300
		}
1301
1302
		/**
1303
		 * Get admin notice class.
1304
		 *
1305
		 * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1306
		 * (lowest supported version by TGMPA).
1307
		 *
1308
		 * @since 2.6.0
1309
		 *
1310
		 * @return string
1311
		 */
1312
		protected function get_admin_notice_class() {
1313
			if ( ! empty( $this->strings['nag_type'] ) ) {
1314
				return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1315
			} else {
1316
				if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1317
					return 'notice-warning';
1318
				} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1319
					return 'notice';
1320
				} else {
1321
					return 'updated';
1322
				}
1323
			}
1324
		}
1325
1326
		/**
1327
		 * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
1328
		 *
1329
		 * @since 2.5.0
1330
		 */
1331
		protected function display_settings_errors() {
1332
			global $wp_settings_errors;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

    public function myFunction() {
        // Do something
    }
}
Loading history...
1333
1334
			settings_errors( 'tgmpa' );
1335
1336
			foreach ( (array) $wp_settings_errors as $key => $details ) {
1337
				if ( 'tgmpa' === $details['setting'] ) {
1338
					unset( $wp_settings_errors[ $key ] );
1339
					break;
1340
				}
1341
			}
1342
		}
1343
1344
		/**
1345
		 * Register dismissal of admin notices.
1346
		 *
1347
		 * Acts on the dismiss link in the admin nag messages.
1348
		 * If clicked, the admin notice disappears and will no longer be visible to this user.
1349
		 *
1350
		 * @since 2.1.0
1351
		 */
1352
		public function dismiss() {
1353
			if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) {
1354
				update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 );
1355
			}
1356
		}
1357
1358
		/**
1359
		 * Add individual plugin to our collection of plugins.
1360
		 *
1361
		 * If the required keys are not set or the plugin has already
1362
		 * been registered, the plugin is not added.
1363
		 *
1364
		 * @since 2.0.0
1365
		 *
1366
		 * @param array|null $plugin Array of plugin arguments or null if invalid argument.
1367
		 * @return null Return early if incorrect argument.
1368
		 */
1369
		public function register( $plugin ) {
1370
			if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) {
1371
				return;
1372
			}
1373
1374
			if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
1375
				return;
1376
			}
1377
1378
			$defaults = array(
1379
				'name'               => '',      // String
1380
				'slug'               => '',      // String
1381
				'source'             => 'repo',  // String
1382
				'required'           => false,   // Boolean
1383
				'version'            => '',      // String
1384
				'force_activation'   => false,   // Boolean
1385
				'force_deactivation' => false,   // Boolean
1386
				'external_url'       => '',      // String
1387
				'is_callable'        => '',      // String|Array.
1388
			);
1389
1390
			// Prepare the received data.
1391
			$plugin = wp_parse_args( $plugin, $defaults );
1392
1393
			// Standardize the received slug.
1394
			$plugin['slug'] = $this->sanitize_key( $plugin['slug'] );
1395
1396
			// Forgive users for using string versions of booleans or floats for version number.
1397
			$plugin['version']            = (string) $plugin['version'];
1398
			$plugin['source']             = empty( $plugin['source'] ) ? 'repo' : $plugin['source'];
1399
			$plugin['required']           = TGMPA_Utils::validate_bool( $plugin['required'] );
1400
			$plugin['force_activation']   = TGMPA_Utils::validate_bool( $plugin['force_activation'] );
1401
			$plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] );
1402
1403
			// Enrich the received data.
1404
			$plugin['file_path']   = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
1405
			$plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] );
1406
1407
			// Set the class properties.
1408
			$this->plugins[ $plugin['slug'] ]    = $plugin;
1409
			$this->sort_order[ $plugin['slug'] ] = $plugin['name'];
1410
1411
			// Should we add the force activation hook ?
1412
			if ( true === $plugin['force_activation'] ) {
1413
				$this->has_forced_activation = true;
1414
			}
1415
1416
			// Should we add the force deactivation hook ?
1417
			if ( true === $plugin['force_deactivation'] ) {
1418
				$this->has_forced_deactivation = true;
1419
			}
1420
		}
1421
1422
		/**
1423
		 * Determine what type of source the plugin comes from.
1424
		 *
1425
		 * @since 2.5.0
1426
		 *
1427
		 * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path
1428
		 *                       (= bundled) or an external URL.
1429
		 * @return string 'repo', 'external', or 'bundled'
1430
		 */
1431
		protected function get_plugin_source_type( $source ) {
1432
			if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) {
1433
				return 'repo';
1434
			} elseif ( preg_match( self::IS_URL_REGEX, $source ) ) {
1435
				return 'external';
1436
			} else {
1437
				return 'bundled';
1438
			}
1439
		}
1440
1441
		/**
1442
		 * Sanitizes a string key.
1443
		 *
1444
		 * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are*
1445
		 * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase
1446
		 * characters in the plugin directory path/slug. Silly them.
1447
		 *
1448
		 * @see https://developer.wordpress.org/reference/hooks/sanitize_key/
1449
		 *
1450
		 * @since 2.5.0
1451
		 *
1452
		 * @param string $key String key.
1453
		 * @return string Sanitized key
1454
		 */
1455
		public function sanitize_key( $key ) {
1456
			$raw_key = $key;
1457
			$key     = preg_replace( '`[^A-Za-z0-9_-]`', '', $key );
1458
1459
			/**
1460
			 * Filter a sanitized key string.
1461
			 *
1462
			 * @since 2.5.0
1463
			 *
1464
			 * @param string $key     Sanitized key.
1465
			 * @param string $raw_key The key prior to sanitization.
1466
			 */
1467
			return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key );
1468
		}
1469
1470
		/**
1471
		 * Amend default configuration settings.
1472
		 *
1473
		 * @since 2.0.0
1474
		 *
1475
		 * @param array $config Array of config options to pass as class properties.
1476
		 */
1477
		public function config( $config ) {
1478
			$keys = array(
1479
				'id',
1480
				'default_path',
1481
				'has_notices',
1482
				'dismissable',
1483
				'dismiss_msg',
1484
				'menu',
1485
				'parent_slug',
1486
				'capability',
1487
				'is_automatic',
1488
				'message',
1489
				'strings',
1490
			);
1491
1492
			foreach ( $keys as $key ) {
1493
				if ( isset( $config[ $key ] ) ) {
1494
					if ( is_array( $config[ $key ] ) ) {
1495
						$this->$key = array_merge( $this->$key, $config[ $key ] );
1496
					} else {
1497
						$this->$key = $config[ $key ];
1498
					}
1499
				}
1500
			}
1501
		}
1502
1503
		/**
1504
		 * Amend action link after plugin installation.
1505
		 *
1506
		 * @since 2.0.0
1507
		 *
1508
		 * @param array $install_actions Existing array of actions.
1509
		 * @return false|array Amended array of actions.
1510
		 */
1511
		public function actions( $install_actions ) {
1512
			// Remove action links on the TGMPA install page.
1513
			if ( $this->is_tgmpa_page() ) {
1514
				return false;
1515
			}
1516
1517
			return $install_actions;
1518
		}
1519
1520
		/**
1521
		 * Flushes the plugins cache on theme switch to prevent stale entries
1522
		 * from remaining in the plugin table.
1523
		 *
1524
		 * @since 2.4.0
1525
		 *
1526
		 * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache.
1527
		 *                                 Parameter added in v2.5.0.
1528
		 */
1529
		public function flush_plugins_cache( $clear_update_cache = true ) {
1530
			wp_clean_plugins_cache( $clear_update_cache );
1531
		}
1532
1533
		/**
1534
		 * Set file_path key for each installed plugin.
1535
		 *
1536
		 * @since 2.1.0
1537
		 *
1538
		 * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
1539
		 *                            Parameter added in v2.5.0.
1540
		 */
1541
		public function populate_file_path( $plugin_slug = '' ) {
1542
			if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) {
1543
				$this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug );
1544
			} else {
1545
				// Add file_path key for all plugins.
1546
				foreach ( $this->plugins as $slug => $values ) {
1547
					$this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug );
1548
				}
1549
			}
1550
		}
1551
1552
		/**
1553
		 * Helper function to extract the file path of the plugin file from the
1554
		 * plugin slug, if the plugin is installed.
1555
		 *
1556
		 * @since 2.0.0
1557
		 *
1558
		 * @param string $slug Plugin slug (typically folder name) as provided by the developer.
1559
		 * @return string Either file path for plugin if installed, or just the plugin slug.
1560
		 */
1561
		protected function _get_plugin_basename_from_slug( $slug ) {
1562
			$keys = array_keys( $this->get_plugins() );
1563
1564
			foreach ( $keys as $key ) {
1565
				if ( preg_match( '|^' . $slug . '/|', $key ) ) {
1566
					return $key;
1567
				}
1568
			}
1569
1570
			return $slug;
1571
		}
1572
1573
		/**
1574
		 * Retrieve plugin data, given the plugin name.
1575
		 *
1576
		 * Loops through the registered plugins looking for $name. If it finds it,
1577
		 * it returns the $data from that plugin. Otherwise, returns false.
1578
		 *
1579
		 * @since 2.1.0
1580
		 *
1581
		 * @param string $name Name of the plugin, as it was registered.
1582
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
1583
		 * @return string|boolean Plugin slug if found, false otherwise.
1584
		 */
1585
		public function _get_plugin_data_from_name( $name, $data = 'slug' ) {
1586
			foreach ( $this->plugins as $values ) {
1587
				if ( $name === $values['name'] && isset( $values[ $data ] ) ) {
1588
					return $values[ $data ];
1589
				}
1590
			}
1591
1592
			return false;
1593
		}
1594
1595
		/**
1596
		 * Retrieve the download URL for a package.
1597
		 *
1598
		 * @since 2.5.0
1599
		 *
1600
		 * @param string $slug Plugin slug.
1601
		 * @return string Plugin download URL or path to local file or empty string if undetermined.
1602
		 */
1603
		public function get_download_url( $slug ) {
1604
			$dl_source = '';
1605
1606
			switch ( $this->plugins[ $slug ]['source_type'] ) {
1607
				case 'repo':
1608
					return $this->get_wp_repo_download_url( $slug );
1609
				case 'external':
1610
					return $this->plugins[ $slug ]['source'];
1611
				case 'bundled':
1612
					return $this->default_path . $this->plugins[ $slug ]['source'];
1613
			}
1614
1615
			return $dl_source; // Should never happen.
1616
		}
1617
1618
		/**
1619
		 * Retrieve the download URL for a WP repo package.
1620
		 *
1621
		 * @since 2.5.0
1622
		 *
1623
		 * @param string $slug Plugin slug.
1624
		 * @return string Plugin download URL.
1625
		 */
1626
		protected function get_wp_repo_download_url( $slug ) {
1627
			$source = '';
1628
			$api    = $this->get_plugins_api( $slug );
1629
1630
			if ( false !== $api && isset( $api->download_link ) ) {
1631
				$source = $api->download_link;
1632
			}
1633
1634
			return $source;
1635
		}
1636
1637
		/**
1638
		 * Try to grab information from WordPress API.
1639
		 *
1640
		 * @since 2.5.0
1641
		 *
1642
		 * @param string $slug Plugin slug.
1643
		 * @return object Plugins_api response object on success, WP_Error on failure.
1644
		 */
1645
		protected function get_plugins_api( $slug ) {
1646
			static $api = array(); // Cache received responses.
1647
1648
			if ( ! isset( $api[ $slug ] ) ) {
1649
				if ( ! function_exists( 'plugins_api' ) ) {
1650
					require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
1651
				}
1652
1653
				$response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
1654
1655
				$api[ $slug ] = false;
1656
1657
				if ( is_wp_error( $response ) ) {
1658
					wp_die( esc_html( $this->strings['oops'] ) );
1659
				} else {
1660
					$api[ $slug ] = $response;
1661
				}
1662
			}
1663
1664
			return $api[ $slug ];
1665
		}
1666
1667
		/**
1668
		 * Retrieve a link to a plugin information page.
1669
		 *
1670
		 * @since 2.5.0
1671
		 *
1672
		 * @param string $slug Plugin slug.
1673
		 * @return string Fully formed html link to a plugin information page if available
1674
		 *                or the plugin name if not.
1675
		 */
1676
		public function get_info_link( $slug ) {
1677
			if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) {
1678
				$link = sprintf(
1679
					'<a href="%1$s" target="_blank">%2$s</a>',
1680
					esc_url( $this->plugins[ $slug ]['external_url'] ),
1681
					esc_html( $this->plugins[ $slug ]['name'] )
1682
				);
1683
			} elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) {
1684
				$url = add_query_arg(
1685
					array(
1686
						'tab'       => 'plugin-information',
1687
						'plugin'    => urlencode( $slug ),
1688
						'TB_iframe' => 'true',
1689
						'width'     => '640',
1690
						'height'    => '500',
1691
					),
1692
					self_admin_url( 'plugin-install.php' )
1693
				);
1694
1695
				$link = sprintf(
1696
					'<a href="%1$s" class="thickbox">%2$s</a>',
1697
					esc_url( $url ),
1698
					esc_html( $this->plugins[ $slug ]['name'] )
1699
				);
1700
			} else {
1701
				$link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink.
1702
			}
1703
1704
			return $link;
1705
		}
1706
1707
		/**
1708
		 * Determine if we're on the TGMPA Install page.
1709
		 *
1710
		 * @since 2.1.0
1711
		 *
1712
		 * @return boolean True when on the TGMPA page, false otherwise.
1713
		 */
1714
		protected function is_tgmpa_page() {
1715
			return isset( $_GET['page'] ) && $this->menu === $_GET['page'];
1716
		}
1717
1718
		/**
1719
		 * Determine if we're on a WP Core installation/upgrade page.
1720
		 *
1721
		 * @since 2.6.0
1722
		 *
1723
		 * @return boolean True when on a WP Core installation/upgrade page, false otherwise.
1724
		 */
1725
		protected function is_core_update_page() {
1726
			// Current screen is not always available, most notably on the customizer screen.
1727
			if ( ! function_exists( 'get_current_screen' ) ) {
1728
				return false;
1729
			}
1730
1731
			$screen = get_current_screen();
1732
1733
			if ( 'update-core' === $screen->base ) {
1734
				// Core update screen.
1735
				return true;
1736
			} elseif ( 'plugins' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
1737
				// Plugins bulk update screen.
1738
				return true;
1739
			} elseif ( 'update' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
1740
				// Individual updates (ajax call).
1741
				return true;
1742
			}
1743
1744
			return false;
1745
		}
1746
1747
		/**
1748
		 * Retrieve the URL to the TGMPA Install page.
1749
		 *
1750
		 * I.e. depending on the config settings passed something along the lines of:
1751
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins
1752
		 *
1753
		 * @since 2.5.0
1754
		 *
1755
		 * @return string Properly encoded URL (not escaped).
1756
		 */
1757
		public function get_tgmpa_url() {
1758
			static $url;
1759
1760
			if ( ! isset( $url ) ) {
1761
				$parent = $this->parent_slug;
1762
				if ( false === strpos( $parent, '.php' ) ) {
1763
					$parent = 'admin.php';
1764
				}
1765
				$url = add_query_arg(
1766
					array(
1767
						'page' => urlencode( $this->menu ),
1768
					),
1769
					self_admin_url( $parent )
1770
				);
1771
			}
1772
1773
			return $url;
1774
		}
1775
1776
		/**
1777
		 * Retrieve the URL to the TGMPA Install page for a specific plugin status (view).
1778
		 *
1779
		 * I.e. depending on the config settings passed something along the lines of:
1780
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install
1781
		 *
1782
		 * @since 2.5.0
1783
		 *
1784
		 * @param string $status Plugin status - either 'install', 'update' or 'activate'.
1785
		 * @return string Properly encoded URL (not escaped).
1786
		 */
1787
		public function get_tgmpa_status_url( $status ) {
1788
			return add_query_arg(
1789
				array(
1790
					'plugin_status' => urlencode( $status ),
1791
				),
1792
				$this->get_tgmpa_url()
1793
			);
1794
		}
1795
1796
		/**
1797
		 * Determine whether there are open actions for plugins registered with TGMPA.
1798
		 *
1799
		 * @since 2.5.0
1800
		 *
1801
		 * @return bool True if complete, i.e. no outstanding actions. False otherwise.
1802
		 */
1803
		public function is_tgmpa_complete() {
1804
			$complete = true;
1805
			foreach ( $this->plugins as $slug => $plugin ) {
1806
				if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1807
					$complete = false;
1808
					break;
1809
				}
1810
			}
1811
1812
			return $complete;
1813
		}
1814
1815
		/**
1816
		 * Check if a plugin is installed. Does not take must-use plugins into account.
1817
		 *
1818
		 * @since 2.5.0
1819
		 *
1820
		 * @param string $slug Plugin slug.
1821
		 * @return bool True if installed, false otherwise.
1822
		 */
1823
		public function is_plugin_installed( $slug ) {
1824
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1825
1826
			return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) );
1827
		}
1828
1829
		/**
1830
		 * Check if a plugin is active.
1831
		 *
1832
		 * @since 2.5.0
1833
		 *
1834
		 * @param string $slug Plugin slug.
1835
		 * @return bool True if active, false otherwise.
1836
		 */
1837
		public function is_plugin_active( $slug ) {
1838
			return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) );
1839
		}
1840
1841
		/**
1842
		 * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required
1843
		 * available, check whether the current install meets them.
1844
		 *
1845
		 * @since 2.5.0
1846
		 *
1847
		 * @param string $slug Plugin slug.
1848
		 * @return bool True if OK to update, false otherwise.
1849
		 */
1850
		public function can_plugin_update( $slug ) {
1851
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1852
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1853
				return true;
1854
			}
1855
1856
			$api = $this->get_plugins_api( $slug );
1857
1858
			if ( false !== $api && isset( $api->requires ) ) {
1859
				return version_compare( $this->wp_version, $api->requires, '>=' );
1860
			}
1861
1862
			// No usable info received from the plugins API, presume we can update.
1863
			return true;
1864
		}
1865
1866
		/**
1867
		 * Check to see if the plugin is 'updatetable', i.e. installed, with an update available
1868
		 * and no WP version requirements blocking it.
1869
		 *
1870
		 * @since 2.6.0
1871
		 *
1872
		 * @param string $slug Plugin slug.
1873
		 * @return bool True if OK to proceed with update, false otherwise.
1874
		 */
1875
		public function is_plugin_updatetable( $slug ) {
1876
			if ( ! $this->is_plugin_installed( $slug ) ) {
1877
				return false;
1878
			} else {
1879
				return ( false !== $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) );
1880
			}
1881
		}
1882
1883
		/**
1884
		 * Check if a plugin can be activated, i.e. is not currently active and meets the minimum
1885
		 * plugin version requirements set in TGMPA (if any).
1886
		 *
1887
		 * @since 2.5.0
1888
		 *
1889
		 * @param string $slug Plugin slug.
1890
		 * @return bool True if OK to activate, false otherwise.
1891
		 */
1892
		public function can_plugin_activate( $slug ) {
1893
			return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) );
1894
		}
1895
1896
		/**
1897
		 * Retrieve the version number of an installed plugin.
1898
		 *
1899
		 * @since 2.5.0
1900
		 *
1901
		 * @param string $slug Plugin slug.
1902
		 * @return string Version number as string or an empty string if the plugin is not installed
1903
		 *                or version unknown (plugins which don't comply with the plugin header standard).
1904
		 */
1905
		public function get_installed_version( $slug ) {
1906
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1907
1908
			if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) {
1909
				return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'];
1910
			}
1911
1912
			return '';
1913
		}
1914
1915
		/**
1916
		 * Check whether a plugin complies with the minimum version requirements.
1917
		 *
1918
		 * @since 2.5.0
1919
		 *
1920
		 * @param string $slug Plugin slug.
1921
		 * @return bool True when a plugin needs to be updated, otherwise false.
1922
		 */
1923
		public function does_plugin_require_update( $slug ) {
1924
			$installed_version = $this->get_installed_version( $slug );
1925
			$minimum_version   = $this->plugins[ $slug ]['version'];
1926
1927
			return version_compare( $minimum_version, $installed_version, '>' );
1928
		}
1929
1930
		/**
1931
		 * Check whether there is an update available for a plugin.
1932
		 *
1933
		 * @since 2.5.0
1934
		 *
1935
		 * @param string $slug Plugin slug.
1936
		 * @return false|string Version number string of the available update or false if no update available.
1937
		 */
1938
		public function does_plugin_have_update( $slug ) {
1939
			// Presume bundled and external plugins will point to a package which meets the minimum required version.
1940
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1941
				if ( $this->does_plugin_require_update( $slug ) ) {
1942
					return $this->plugins[ $slug ]['version'];
1943
				}
1944
1945
				return false;
1946
			}
1947
1948
			$repo_updates = get_site_transient( 'update_plugins' );
1949
1950 View Code Duplication
			if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) {
1951
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version;
1952
			}
1953
1954
			return false;
1955
		}
1956
1957
		/**
1958
		 * Retrieve potential upgrade notice for a plugin.
1959
		 *
1960
		 * @since 2.5.0
1961
		 *
1962
		 * @param string $slug Plugin slug.
1963
		 * @return string The upgrade notice or an empty string if no message was available or provided.
1964
		 */
1965
		public function get_upgrade_notice( $slug ) {
1966
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1967
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1968
				return '';
1969
			}
1970
1971
			$repo_updates = get_site_transient( 'update_plugins' );
1972
1973 View Code Duplication
			if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) {
1974
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice;
1975
			}
1976
1977
			return '';
1978
		}
1979
1980
		/**
1981
		 * Wrapper around the core WP get_plugins function, making sure it's actually available.
1982
		 *
1983
		 * @since 2.5.0
1984
		 *
1985
		 * @param string $plugin_folder Optional. Relative path to single plugin folder.
1986
		 * @return array Array of installed plugins with plugin information.
1987
		 */
1988
		public function get_plugins( $plugin_folder = '' ) {
1989
			if ( ! function_exists( 'get_plugins' ) ) {
1990
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
1991
			}
1992
1993
			return get_plugins( $plugin_folder );
1994
		}
1995
1996
		/**
1997
		 * Delete dismissable nag option when theme is switched.
1998
		 *
1999
		 * This ensures that the user(s) is/are again reminded via nag of required
2000
		 * and/or recommended plugins if they re-activate the theme.
2001
		 *
2002
		 * @since 2.1.1
2003
		 */
2004
		public function update_dismiss() {
2005
			delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true );
2006
		}
2007
2008
		/**
2009
		 * Forces plugin activation if the parameter 'force_activation' is
2010
		 * set to true.
2011
		 *
2012
		 * This allows theme authors to specify certain plugins that must be
2013
		 * active at all times while using the current theme.
2014
		 *
2015
		 * Please take special care when using this parameter as it has the
2016
		 * potential to be harmful if not used correctly. Setting this parameter
2017
		 * to true will not allow the specified plugin to be deactivated unless
2018
		 * the user switches themes.
2019
		 *
2020
		 * @since 2.2.0
2021
		 */
2022
		public function force_activation() {
2023
			foreach ( $this->plugins as $slug => $plugin ) {
2024
				if ( true === $plugin['force_activation'] ) {
2025
					if ( ! $this->is_plugin_installed( $slug ) ) {
2026
						// Oops, plugin isn't there so iterate to next condition.
2027
						continue;
2028
					} elseif ( $this->can_plugin_activate( $slug ) ) {
2029
						// There we go, activate the plugin.
2030
						activate_plugin( $plugin['file_path'] );
2031
					}
2032
				}
2033
			}
2034
		}
2035
2036
		/**
2037
		 * Forces plugin deactivation if the parameter 'force_deactivation'
2038
		 * is set to true and adds the plugin to the 'recently active' plugins list.
2039
		 *
2040
		 * This allows theme authors to specify certain plugins that must be
2041
		 * deactivated upon switching from the current theme to another.
2042
		 *
2043
		 * Please take special care when using this parameter as it has the
2044
		 * potential to be harmful if not used correctly.
2045
		 *
2046
		 * @since 2.2.0
2047
		 */
2048
		public function force_deactivation() {
2049
			$deactivated = array();
2050
2051
			foreach ( $this->plugins as $slug => $plugin ) {
2052
				/*
2053
				 * Only proceed forward if the parameter is set to true and plugin is active
2054
				 * as a 'normal' (not must-use) plugin.
2055
				 */
2056
				if ( true === $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) ) {
2057
					deactivate_plugins( $plugin['file_path'] );
2058
					$deactivated[ $plugin['file_path'] ] = time();
2059
				}
2060
			}
2061
2062
			if ( ! empty( $deactivated ) ) {
2063
				update_option( 'recently_activated', $deactivated + (array) get_option( 'recently_activated' ) );
2064
			}
2065
		}
2066
2067
		/**
2068
		 * Echo the current TGMPA version number to the page.
2069
		 *
2070
		 * @since 2.5.0
2071
		 */
2072
		public function show_tgmpa_version() {
2073
			echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',
2074
				esc_html(
2075
					sprintf(
2076
						/* translators: %s: version number */
2077
						__( 'TGMPA v%s', 'tgmpa' ),
2078
						self::TGMPA_VERSION
2079
					)
2080
				),
2081
				'</small></strong></p>';
2082
		}
2083
2084
		/**
2085
		 * Returns the singleton instance of the class.
2086
		 *
2087
		 * @since 2.4.0
2088
		 *
2089
		 * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object.
2090
		 */
2091
		public static function get_instance() {
2092
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
2093
				self::$instance = new self();
2094
			}
2095
2096
			return self::$instance;
2097
		}
2098
2099
		/**
2100
		 * Adds css to admin head
2101
		 *
2102
		 * @since 2.6.2
2103
		 */
2104
		function tgm_css() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
2105
			echo '<style>
2106
			#tgmpa-plugins #the-list .required > th {
2107
			border-left: 3px solid #dc3232;
2108
			}
2109
			</style>';
2110
		}
2111
	}
2112
2113
	if ( ! function_exists( 'load_tgm_plugin_activation' ) ) {
2114
		/**
2115
		 * Ensure only one instance of the class is ever invoked.
2116
		 *
2117
		 * @since 2.5.0
2118
		 */
2119
		function load_tgm_plugin_activation() {
2120
			$GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance();
2121
		}
2122
	}
2123
2124
	if ( did_action( 'plugins_loaded' ) ) {
2125
		load_tgm_plugin_activation();
2126
	} else {
2127
		add_action( 'plugins_loaded', 'load_tgm_plugin_activation' );
2128
	}
2129
}
2130
2131
if ( ! function_exists( 'tgmpa' ) ) {
2132
	/**
2133
	 * Helper function to register a collection of required plugins.
2134
	 *
2135
	 * @since 2.0.0
2136
	 * @api
2137
	 *
2138
	 * @param array $plugins An array of plugin arrays.
2139
	 * @param array $config  Optional. An array of configuration values.
2140
	 */
2141
	function tgmpa( $plugins, $config = array() ) {
2142
		$instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2143
2144
		foreach ( $plugins as $plugin ) {
2145
			call_user_func( array( $instance, 'register' ), $plugin );
2146
		}
2147
2148
		if ( ! empty( $config ) && is_array( $config ) ) {
2149
			// Send out notices for deprecated arguments passed.
2150
			if ( isset( $config['notices'] ) ) {
2151
				_deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' );
2152
				if ( ! isset( $config['has_notices'] ) ) {
2153
					$config['has_notices'] = $config['notices'];
2154
				}
2155
			}
2156
2157
			if ( isset( $config['parent_menu_slug'] ) ) {
2158
				_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.' );
2159
			}
2160
			if ( isset( $config['parent_url_slug'] ) ) {
2161
				_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.' );
2162
			}
2163
2164
			call_user_func( array( $instance, 'config' ), $config );
2165
		}
2166
	}
2167
}
2168
2169
/**
2170
 * WP_List_Table isn't always available. If it isn't available,
2171
 * we load it here.
2172
 *
2173
 * @since 2.2.0
2174
 */
2175
if ( ! class_exists( 'WP_List_Table' ) ) {
2176
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
2177
}
2178
2179
if ( ! class_exists( 'TGMPA_List_Table' ) ) {
2180
2181
	/**
2182
	 * List table class for handling plugins.
2183
	 *
2184
	 * Extends the WP_List_Table class to provide a future-compatible
2185
	 * way of listing out all required/recommended plugins.
2186
	 *
2187
	 * Gives users an interface similar to the Plugin Administration
2188
	 * area with similar (albeit stripped down) capabilities.
2189
	 *
2190
	 * This class also allows for the bulk install of plugins.
2191
	 *
2192
	 * @since 2.2.0
2193
	 *
2194
	 * @package TGM-Plugin-Activation
2195
	 * @author  Thomas Griffin
2196
	 * @author  Gary Jones
2197
	 */
2198
	class TGMPA_List_Table extends WP_List_Table {
2199
		/**
2200
		 * TGMPA instance.
2201
		 *
2202
		 * @since 2.5.0
2203
		 *
2204
		 * @var object
2205
		 */
2206
		protected $tgmpa;
2207
2208
		/**
2209
		 * The currently chosen view.
2210
		 *
2211
		 * @since 2.5.0
2212
		 *
2213
		 * @var string One of: 'all', 'install', 'update', 'activate'
2214
		 */
2215
		public $view_context = 'all';
2216
2217
		/**
2218
		 * The plugin counts for the various views.
2219
		 *
2220
		 * @since 2.5.0
2221
		 *
2222
		 * @var array
2223
		 */
2224
		protected $view_totals = array(
2225
			'all'      => 0,
2226
			'install'  => 0,
2227
			'update'   => 0,
2228
			'activate' => 0,
2229
		);
2230
2231
		/**
2232
		 * References parent constructor and sets defaults for class.
2233
		 *
2234
		 * @since 2.2.0
2235
		 */
2236
		public function __construct() {
2237
			$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2238
2239
			parent::__construct(
2240
				array(
2241
					'singular' => 'plugin',
2242
					'plural'   => 'plugins',
2243
					'ajax'     => false,
2244
				)
2245
			);
2246
2247
			if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) {
2248
				$this->view_context = sanitize_key( $_REQUEST['plugin_status'] );
2249
			}
2250
2251
			add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) );
2252
		}
2253
2254
		/**
2255
		 * Get a list of CSS classes for the <table> tag.
2256
		 *
2257
		 * Overruled to prevent the 'plural' argument from being added.
2258
		 *
2259
		 * @since 2.5.0
2260
		 *
2261
		 * @return array CSS classnames.
2262
		 */
2263
		public function get_table_classes() {
2264
			return array( 'widefat', 'fixed' );
2265
		}
2266
2267
		/**
2268
		 * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table.
2269
		 *
2270
		 * @since 2.2.0
2271
		 *
2272
		 * @return array $table_data Information for use in table.
2273
		 */
2274
		protected function _gather_plugin_data() {
2275
			// Load thickbox for plugin links.
2276
			$this->tgmpa->admin_init();
2277
			$this->tgmpa->thickbox();
2278
2279
			// Categorize the plugins which have open actions.
2280
			$plugins = $this->categorize_plugins_to_views();
2281
2282
			// Set the counts for the view links.
2283
			$this->set_view_totals( $plugins );
2284
2285
			// Prep variables for use and grab list of all installed plugins.
2286
			$table_data = array();
2287
			$i          = 0;
2288
2289
			// Redirect to the 'all' view if no plugins were found for the selected view context.
2290
			if ( empty( $plugins[ $this->view_context ] ) ) {
2291
				$this->view_context = 'all';
2292
			}
2293
2294
			foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) {
2295
				$table_data[ $i ]['sanitized_plugin']  = $plugin['name'];
2296
				$table_data[ $i ]['slug']              = $slug;
2297
				$table_data[ $i ]['plugin']            = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>';
2298
				$table_data[ $i ]['source']            = $this->get_plugin_source_type_text( $plugin['source_type'] );
2299
				$table_data[ $i ]['type']              = $this->get_plugin_advise_type_text( $plugin['required'] );
2300
				$table_data[ $i ]['status']            = $this->get_plugin_status_text( $slug );
2301
				$table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug );
2302
				$table_data[ $i ]['minimum_version']   = $plugin['version'];
2303
				$table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug );
2304
2305
				// Prep the upgrade notice info.
2306
				$upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug );
2307
				if ( ! empty( $upgrade_notice ) ) {
2308
					$table_data[ $i ]['upgrade_notice'] = $upgrade_notice;
2309
2310
					add_action( "tgmpa_after_plugin_row_{$slug}", array( $this, 'wp_plugin_update_row' ), 10, 2 );
2311
				}
2312
2313
				$table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin );
2314
2315
				$i++;
2316
			}
2317
2318
			return $table_data;
2319
		}
2320
2321
		/**
2322
		 * Categorize the plugins which have open actions into views for the TGMPA page.
2323
		 *
2324
		 * @since 2.5.0
2325
		 */
2326
		protected function categorize_plugins_to_views() {
2327
			$plugins = array(
2328
				'all'      => array(), // Meaning: all plugins which still have open actions.
2329
				'install'  => array(),
2330
				'update'   => array(),
2331
				'activate' => array(),
2332
			);
2333
2334
			foreach ( $this->tgmpa->plugins as $slug => $plugin ) {
2335
				if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2336
					// No need to display plugins if they are installed, up-to-date and active.
2337
					continue;
2338
				} else {
2339
					$plugins['all'][ $slug ] = $plugin;
2340
2341
					if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2342
						$plugins['install'][ $slug ] = $plugin;
2343
					} else {
2344
						if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2345
							$plugins['update'][ $slug ] = $plugin;
2346
						}
2347
2348
						if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2349
							$plugins['activate'][ $slug ] = $plugin;
2350
						}
2351
					}
2352
				}
2353
			}
2354
2355
			return $plugins;
2356
		}
2357
2358
		/**
2359
		 * Set the counts for the view links.
2360
		 *
2361
		 * @since 2.5.0
2362
		 *
2363
		 * @param array $plugins Plugins order by view.
2364
		 */
2365
		protected function set_view_totals( $plugins ) {
2366
			foreach ( $plugins as $type => $list ) {
2367
				$this->view_totals[ $type ] = count( $list );
2368
			}
2369
		}
2370
2371
		/**
2372
		 * Get the plugin required/recommended text string.
2373
		 *
2374
		 * @since 2.5.0
2375
		 *
2376
		 * @param string $required Plugin required setting.
2377
		 * @return string
2378
		 */
2379
		protected function get_plugin_advise_type_text( $required ) {
2380
			if ( true === $required ) {
2381
				return __( 'Required', 'tgmpa' );
2382
			}
2383
2384
			return __( 'Recommended', 'tgmpa' );
2385
		}
2386
2387
		/**
2388
		 * Get the plugin source type text string.
2389
		 *
2390
		 * @since 2.5.0
2391
		 *
2392
		 * @param string $type Plugin type.
2393
		 * @return string
2394
		 */
2395
		protected function get_plugin_source_type_text( $type ) {
2396
			$string = '';
2397
2398
			switch ( $type ) {
2399
				case 'repo':
2400
					$string = __( 'WordPress Repository', 'tgmpa' );
2401
					break;
2402
				case 'external':
2403
					$string = __( 'External Source', 'tgmpa' );
2404
					break;
2405
				case 'bundled':
2406
					$string = __( 'Pre-Packaged', 'tgmpa' );
2407
					break;
2408
			}
2409
2410
			return $string;
2411
		}
2412
2413
		/**
2414
		 * Determine the plugin status message.
2415
		 *
2416
		 * @since 2.5.0
2417
		 *
2418
		 * @param string $slug Plugin slug.
2419
		 * @return string
2420
		 */
2421
		protected function get_plugin_status_text( $slug ) {
2422
			if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2423
				return __( 'Not Installed', 'tgmpa' );
2424
			}
2425
2426
			if ( ! $this->tgmpa->is_plugin_active( $slug ) ) {
2427
				$install_status = __( 'Installed But Not Activated', 'tgmpa' );
2428
			} else {
2429
				$install_status = __( 'Active', 'tgmpa' );
2430
			}
2431
2432
			$update_status = '';
2433
2434
			if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2435
				$update_status = __( 'Required Update not Available', 'tgmpa' );
2436
2437
			} elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) {
2438
				$update_status = __( 'Requires Update', 'tgmpa' );
2439
2440
			} elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2441
				$update_status = __( 'Update recommended', 'tgmpa' );
2442
			}
2443
2444
			if ( '' === $update_status ) {
2445
				return $install_status;
2446
			}
2447
2448
			return sprintf(
2449
				/* translators: 1: install status, 2: update status */
2450
				_x( '%1$s, %2$s', 'Install/Update Status', 'tgmpa' ),
2451
				$install_status,
2452
				$update_status
2453
			);
2454
		}
2455
2456
		/**
2457
		 * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type.
2458
		 *
2459
		 * @since 2.5.0
2460
		 *
2461
		 * @param array $items Prepared table items.
2462
		 * @return array Sorted table items.
2463
		 */
2464
		public function sort_table_items( $items ) {
2465
			$type = array();
2466
			$name = array();
2467
2468
			foreach ( $items as $i => $plugin ) {
2469
				$type[ $i ] = $plugin['type']; // Required / recommended.
2470
				$name[ $i ] = $plugin['sanitized_plugin'];
2471
			}
2472
2473
			array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items );
2474
2475
			return $items;
2476
		}
2477
2478
		/**
2479
		 * Get an associative array ( id => link ) of the views available on this table.
2480
		 *
2481
		 * @since 2.5.0
2482
		 *
2483
		 * @return array
2484
		 */
2485
		public function get_views() {
2486
			$status_links = array();
2487
2488
			foreach ( $this->view_totals as $type => $count ) {
2489
				if ( $count < 1 ) {
2490
					continue;
2491
				}
2492
2493
				switch ( $type ) {
2494
					case 'all':
2495
						/* translators: 1: number of plugins. */
2496
						$text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' );
2497
						break;
2498
					case 'install':
2499
						/* translators: 1: number of plugins. */
2500
						$text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' );
2501
						break;
2502
					case 'update':
2503
						/* translators: 1: number of plugins. */
2504
						$text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' );
2505
						break;
2506
					case 'activate':
2507
						/* translators: 1: number of plugins. */
2508
						$text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' );
2509
						break;
2510
					default:
2511
						$text = '';
2512
						break;
2513
				}
2514
2515
				if ( ! empty( $text ) ) {
2516
2517
					$status_links[ $type ] = sprintf(
2518
						'<a href="%s"%s>%s</a>',
2519
						esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
2520
						( $type === $this->view_context ) ? ' class="current"' : '',
2521
						sprintf( $text, number_format_i18n( $count ) )
2522
					);
2523
				}
2524
			}
2525
2526
			return $status_links;
2527
		}
2528
2529
		/**
2530
		 * Create default columns to display important plugin information
2531
		 * like type, action and status.
2532
		 *
2533
		 * @since 2.2.0
2534
		 *
2535
		 * @param array  $item        Array of item data.
2536
		 * @param string $column_name The name of the column.
2537
		 * @return string
2538
		 */
2539
		public function column_default( $item, $column_name ) {
2540
			return $item[ $column_name ];
2541
		}
2542
2543
		/**
2544
		 * Required for bulk installing.
2545
		 *
2546
		 * Adds a checkbox for each plugin.
2547
		 *
2548
		 * @since 2.2.0
2549
		 *
2550
		 * @param array $item Array of item data.
2551
		 * @return string The input checkbox with all necessary info.
2552
		 */
2553
		public function column_cb( $item ) {
2554
			return sprintf(
2555
				'<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />',
2556
				esc_attr( $this->_args['singular'] ),
2557
				esc_attr( $item['slug'] ),
2558
				esc_attr( $item['sanitized_plugin'] )
2559
			);
2560
		}
2561
2562
		/**
2563
		 * Create default title column along with the action links.
2564
		 *
2565
		 * @since 2.2.0
2566
		 *
2567
		 * @param array $item Array of item data.
2568
		 * @return string The plugin name and action links.
2569
		 */
2570
		public function column_plugin( $item ) {
2571
			return sprintf(
2572
				'%1$s %2$s',
2573
				$item['plugin'],
2574
				$this->row_actions( $this->get_row_actions( $item ), true )
2575
			);
2576
		}
2577
2578
		/**
2579
		 * Create version information column.
2580
		 *
2581
		 * @since 2.5.0
2582
		 *
2583
		 * @param array $item Array of item data.
2584
		 * @return string HTML-formatted version information.
2585
		 */
2586
		public function column_version( $item ) {
2587
			$output = array();
2588
2589
			if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2590
				$installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'tgmpa' );
2591
2592
				$color = '';
2593
				if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) {
2594
					$color = ' color: #ff0000; font-weight: bold;';
2595
				}
2596
2597
				$output[] = sprintf(
2598
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'tgmpa' ) . '</p>',
2599
					$color,
2600
					$installed
2601
				);
2602
			}
2603
2604
			if ( ! empty( $item['minimum_version'] ) ) {
2605
				$output[] = sprintf(
2606
					'<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'tgmpa' ) . '</p>',
2607
					$item['minimum_version']
2608
				);
2609
			}
2610
2611
			if ( ! empty( $item['available_version'] ) ) {
2612
				$color = '';
2613
				if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) {
2614
					$color = ' color: #71C671; font-weight: bold;';
2615
				}
2616
2617
				$output[] = sprintf(
2618
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'tgmpa' ) . '</p>',
2619
					$color,
2620
					$item['available_version']
2621
				);
2622
			}
2623
2624
			if ( empty( $output ) ) {
2625
				return '&nbsp;'; // Let's not break the table layout.
2626
			} else {
2627
				return implode( "\n", $output );
2628
			}
2629
		}
2630
2631
		/**
2632
		 * Sets default message within the plugins table if no plugins
2633
		 * are left for interaction.
2634
		 *
2635
		 * Hides the menu item to prevent the user from clicking and
2636
		 * getting a permissions error.
2637
		 *
2638
		 * @since 2.2.0
2639
		 */
2640
		public function no_items() {
2641
			echo esc_html__( 'No plugins to install, update or activate.', 'tgmpa' ) . ' <a href="' . esc_url( self_admin_url() ) . '"> ' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>';
2642
			echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
2643
		}
2644
2645
		/**
2646
		 * Output all the column information within the table.
2647
		 *
2648
		 * @since 2.2.0
2649
		 *
2650
		 * @return array $columns The column names.
2651
		 */
2652
		public function get_columns() {
2653
			$columns = array(
2654
				'cb'     => '<input type="checkbox" />',
2655
				'plugin' => __( 'Plugin', 'tgmpa' ),
2656
				'source' => __( 'Source', 'tgmpa' ),
2657
				'type'   => __( 'Type', 'tgmpa' ),
2658
			);
2659
2660
			if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
2661
				$columns['version'] = __( 'Version', 'tgmpa' );
2662
				$columns['status']  = __( 'Status', 'tgmpa' );
2663
			}
2664
2665
			return apply_filters( 'tgmpa_table_columns', $columns );
2666
		}
2667
2668
		/**
2669
		 * Get name of default primary column
2670
		 *
2671
		 * @since 2.5.0 / WP 4.3+ compatibility
2672
		 * @access protected
2673
		 *
2674
		 * @return string
2675
		 */
2676
		protected function get_default_primary_column_name() {
2677
			return 'plugin';
2678
		}
2679
2680
		/**
2681
		 * Get the name of the primary column.
2682
		 *
2683
		 * @since 2.5.0 / WP 4.3+ compatibility
2684
		 * @access protected
2685
		 *
2686
		 * @return string The name of the primary column.
2687
		 */
2688
		protected function get_primary_column_name() {
2689
			if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) {
2690
				return parent::get_primary_column_name();
2691
			} else {
2692
				return $this->get_default_primary_column_name();
2693
			}
2694
		}
2695
2696
		/**
2697
		 * Get the actions which are relevant for a specific plugin row.
2698
		 *
2699
		 * @since 2.5.0
2700
		 *
2701
		 * @param array $item Array of item data.
2702
		 * @return array Array with relevant action links.
2703
		 */
2704
		protected function get_row_actions( $item ) {
2705
			$actions      = array();
2706
			$action_links = array();
2707
2708
			// Display the 'Install' action link if the plugin is not yet available.
2709
			if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2710
				/* translators: %2$s: plugin name in screen reader markup */
2711
				$actions['install'] = __( 'Install %2$s', 'tgmpa' );
2712
			} else {
2713
				// Display the 'Update' action link if an update is available and WP complies with plugin minimum.
2714
				if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) {
2715
					/* translators: %2$s: plugin name in screen reader markup */
2716
					$actions['update'] = __( 'Update %2$s', 'tgmpa' );
2717
				}
2718
2719
				// Display the 'Activate' action link, but only if the plugin meets the minimum version.
2720
				if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) {
2721
					/* translators: %2$s: plugin name in screen reader markup */
2722
					$actions['activate'] = __( 'Activate %2$s', 'tgmpa' );
2723
				}
2724
			}
2725
2726
			// Create the actual links.
2727
			foreach ( $actions as $action => $text ) {
2728
				$nonce_url = wp_nonce_url(
2729
					add_query_arg(
2730
						array(
2731
							'plugin'           => urlencode( $item['slug'] ),
2732
							'tgmpa-' . $action => $action . '-plugin',
2733
						),
2734
						$this->tgmpa->get_tgmpa_url()
2735
					),
2736
					'tgmpa-' . $action,
2737
					'tgmpa-nonce'
2738
				);
2739
2740
				$action_links[ $action ] = sprintf(
2741
					'<a href="%1$s">' . esc_html( $text ) . '</a>', // $text contains the second placeholder.
2742
					esc_url( $nonce_url ),
2743
					'<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>'
2744
				);
2745
			}
2746
2747
			$prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : '';
2748
			return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context );
2749
		}
2750
2751
		/**
2752
		 * Generates content for a single row of the table.
2753
		 *
2754
		 * @since 2.5.0
2755
		 *
2756
		 * @param object $item The current item.
2757
		 */
2758
		public function single_row( $item ) {
2759
			echo '<tr class="' . esc_attr( strtolower( $item['type'] ) ) . '">';
2760
			parent::single_row_columns( $item );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (single_row_columns() instead of single_row()). Are you sure this is correct? If so, you might want to change this to $this->single_row_columns().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
2761
			echo '</tr>';
2762
2763
			/**
2764
			 * Fires after each specific row in the TGMPA Plugins list table.
2765
			 *
2766
			 * The dynamic portion of the hook name, `$item['slug']`, refers to the slug
2767
			 * for the plugin.
2768
			 *
2769
			 * @since 2.5.0
2770
			 */
2771
			do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context );
2772
		}
2773
2774
		/**
2775
		 * Show the upgrade notice below a plugin row if there is one.
2776
		 *
2777
		 * @since 2.5.0
2778
		 *
2779
		 * @see /wp-admin/includes/update.php
2780
		 *
2781
		 * @param string $slug Plugin slug.
2782
		 * @param array  $item The information available in this table row.
2783
		 * @return null Return early if upgrade notice is empty.
2784
		 */
2785
		public function wp_plugin_update_row( $slug, $item ) {
2786
			if ( empty( $item['upgrade_notice'] ) ) {
2787
				return;
2788
			}
2789
2790
			echo '
2791
				<tr class="plugin-update-tr">
2792
					<td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange">
2793
						<div class="update-message">',
2794
							esc_html__( 'Upgrade message from the plugin author:', 'tgmpa' ),
2795
							' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong>
2796
						</div>
2797
					</td>
2798
				</tr>';
2799
		}
2800
2801
		/**
2802
		 * Extra controls to be displayed between bulk actions and pagination.
2803
		 *
2804
		 * @since 2.5.0
2805
		 *
2806
		 * @param string $which 'top' or 'bottom' table navigation.
2807
		 */
2808
		public function extra_tablenav( $which ) {
2809
			if ( 'bottom' === $which ) {
2810
				$this->tgmpa->show_tgmpa_version();
2811
			}
2812
		}
2813
2814
		/**
2815
		 * Defines the bulk actions for handling registered plugins.
2816
		 *
2817
		 * @since 2.2.0
2818
		 *
2819
		 * @return array $actions The bulk actions for the plugin install table.
2820
		 */
2821
		public function get_bulk_actions() {
2822
2823
			$actions = array();
2824
2825
			if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) {
2826
				if ( current_user_can( 'install_plugins' ) ) {
2827
					$actions['tgmpa-bulk-install'] = __( 'Install', 'tgmpa' );
2828
				}
2829
			}
2830
2831
			if ( 'install' !== $this->view_context ) {
2832
				if ( current_user_can( 'update_plugins' ) ) {
2833
					$actions['tgmpa-bulk-update'] = __( 'Update', 'tgmpa' );
2834
				}
2835
				if ( current_user_can( 'activate_plugins' ) ) {
2836
					$actions['tgmpa-bulk-activate'] = __( 'Activate', 'tgmpa' );
2837
				}
2838
			}
2839
2840
			return $actions;
2841
		}
2842
2843
		/**
2844
		 * Processes bulk installation and activation actions.
2845
		 *
2846
		 * The bulk installation process looks for the $_POST information and passes that
2847
		 * through if a user has to use WP_Filesystem to enter their credentials.
2848
		 *
2849
		 * @since 2.2.0
2850
		 */
2851
		public function process_bulk_actions() {
2852
			// Bulk installation process.
2853
			if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) {
2854
2855
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
2856
2857
				$install_type = 'install';
2858
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2859
					$install_type = 'update';
2860
				}
2861
2862
				$plugins_to_install = array();
2863
2864
				// Did user actually select any plugins to install/update ?
2865 View Code Duplication
				if ( empty( $_POST['plugin'] ) ) {
2866
					if ( 'install' === $install_type ) {
2867
						$message = __( 'No plugins were selected to be installed. No action taken.', 'tgmpa' );
2868
					} else {
2869
						$message = __( 'No plugins were selected to be updated. No action taken.', 'tgmpa' );
2870
					}
2871
2872
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2873
2874
					return false;
2875
				}
2876
2877
				if ( is_array( $_POST['plugin'] ) ) {
2878
					$plugins_to_install = (array) $_POST['plugin'];
2879
				} elseif ( is_string( $_POST['plugin'] ) ) {
2880
					// Received via Filesystem page - un-flatten array (WP bug #19643).
2881
					$plugins_to_install = explode( ',', $_POST['plugin'] );
2882
				}
2883
2884
				// Sanitize the received input.
2885
				$plugins_to_install = array_map( 'urldecode', $plugins_to_install );
2886
				$plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install );
2887
2888
				// Validate the received input.
2889
				foreach ( $plugins_to_install as $key => $slug ) {
2890
					// Check if the plugin was registered with TGMPA and remove if not.
2891
					if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) {
2892
						unset( $plugins_to_install[ $key ] );
2893
						continue;
2894
					}
2895
2896
					// For install: make sure this is a plugin we *can* install and not one already installed.
2897
					if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) {
2898
						unset( $plugins_to_install[ $key ] );
2899
					}
2900
2901
					// For updates: make sure this is a plugin we *can* update (update available and WP version ok).
2902
					if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) {
2903
						unset( $plugins_to_install[ $key ] );
2904
					}
2905
				}
2906
2907
				// No need to proceed further if we have no plugins to handle.
2908 View Code Duplication
				if ( empty( $plugins_to_install ) ) {
2909
					if ( 'install' === $install_type ) {
2910
						$message = __( 'No plugins are available to be installed at this time.', 'tgmpa' );
2911
					} else {
2912
						$message = __( 'No plugins are available to be updated at this time.', 'tgmpa' );
2913
					}
2914
2915
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2916
2917
					return false;
2918
				}
2919
2920
				// Pass all necessary information if WP_Filesystem is needed.
2921
				$url = wp_nonce_url(
2922
					$this->tgmpa->get_tgmpa_url(),
2923
					'bulk-' . $this->_args['plural']
2924
				);
2925
2926
				// Give validated data back to $_POST which is the only place the filesystem looks for extra fields.
2927
				$_POST['plugin'] = implode( ',', $plugins_to_install ); // Work around for WP bug #19643.
2928
2929
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
2930
				$fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
2931
2932
				if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) {
2933
					return true; // Stop the normal page form from displaying, credential request form will be shown.
2934
				}
2935
2936
				// Now we have some credentials, setup WP_Filesystem.
2937
				if ( ! WP_Filesystem( $creds ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
2938
					// Our credentials were no good, ask the user for them again.
2939
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
2940
2941
					return true;
2942
				}
2943
2944
				/* If we arrive here, we have the filesystem */
2945
2946
				// Store all information in arrays since we are processing a bulk installation.
2947
				$names      = array();
2948
				$sources    = array(); // Needed for installs.
2949
				$file_paths = array(); // Needed for upgrades.
2950
				$to_inject  = array(); // Information to inject into the update_plugins transient.
2951
2952
				// Prepare the data for validated plugins for the install/upgrade.
2953
				foreach ( $plugins_to_install as $slug ) {
2954
					$name   = $this->tgmpa->plugins[ $slug ]['name'];
2955
					$source = $this->tgmpa->get_download_url( $slug );
2956
2957
					if ( ! empty( $name ) && ! empty( $source ) ) {
2958
						$names[] = $name;
2959
2960
						switch ( $install_type ) {
2961
2962
							case 'install':
2963
								$sources[] = $source;
2964
								break;
2965
2966
							case 'update':
2967
								$file_paths[]                 = $this->tgmpa->plugins[ $slug ]['file_path'];
2968
								$to_inject[ $slug ]           = $this->tgmpa->plugins[ $slug ];
2969
								$to_inject[ $slug ]['source'] = $source;
2970
								break;
2971
						}
2972
					}
2973
				}
2974
				unset( $slug, $name, $source );
2975
2976
				// Create a new instance of TGMPA_Bulk_Installer.
2977
				$installer = new TGMPA_Bulk_Installer(
2978
					new TGMPA_Bulk_Installer_Skin(
2979
						array(
2980
							'url'          => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),
2981
							'nonce'        => 'bulk-' . $this->_args['plural'],
2982
							'names'        => $names,
2983
							'install_type' => $install_type,
2984
						)
2985
					)
2986
				);
2987
2988
				// Wrap the install process with the appropriate HTML.
2989
				echo '<div class="tgmpa">',
2990
					'<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>
2991
					<div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">';
2992
2993
				// Process the bulk installation submissions.
2994
				add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
2995
2996
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2997
					// Inject our info into the update transient.
2998
					$this->tgmpa->inject_update_info( $to_inject );
2999
3000
					$installer->bulk_upgrade( $file_paths );
3001
				} else {
3002
					$installer->bulk_install( $sources );
3003
				}
3004
3005
				remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1 );
3006
3007
				echo '</div></div>';
3008
3009
				return true;
3010
			}
3011
3012
			// Bulk activation process.
3013
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3014
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
3015
3016
				// Did user actually select any plugins to activate ?
3017
				if ( empty( $_POST['plugin'] ) ) {
3018
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>';
3019
3020
					return false;
3021
				}
3022
3023
				// Grab plugin data from $_POST.
3024
				$plugins = array();
3025
				if ( isset( $_POST['plugin'] ) ) {
3026
					$plugins = array_map( 'urldecode', (array) $_POST['plugin'] );
3027
					$plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins );
3028
				}
3029
3030
				$plugins_to_activate = array();
3031
				$plugin_names        = array();
3032
3033
				// Grab the file paths for the selected & inactive plugins from the registration array.
3034
				foreach ( $plugins as $slug ) {
3035
					if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
3036
						$plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path'];
3037
						$plugin_names[]        = $this->tgmpa->plugins[ $slug ]['name'];
3038
					}
3039
				}
3040
				unset( $slug );
3041
3042
				// Return early if there are no plugins to activate.
3043
				if ( empty( $plugins_to_activate ) ) {
3044
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>';
3045
3046
					return false;
3047
				}
3048
3049
				// Now we are good to go - let's start activating plugins.
3050
				$activate = activate_plugins( $plugins_to_activate );
3051
3052
				if ( is_wp_error( $activate ) ) {
3053
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
3054
				} else {
3055
					$count        = count( $plugin_names ); // Count so we can use _n function.
3056
					$plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names );
3057
					$last_plugin  = array_pop( $plugin_names ); // Pop off last name to prep for readability.
3058
					$imploded     = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
3059
3060
					printf( // WPCS: xss ok.
3061
						'<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
3062
						esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),
3063
						$imploded
3064
					);
3065
3066
					// Update recently activated plugins option.
3067
					$recent = (array) get_option( 'recently_activated' );
3068
					foreach ( $plugins_to_activate as $plugin => $time ) {
3069
						if ( isset( $recent[ $plugin ] ) ) {
3070
							unset( $recent[ $plugin ] );
3071
						}
3072
					}
3073
					update_option( 'recently_activated', $recent );
3074
				}
3075
3076
				unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another.
3077
3078
				return true;
3079
			}
3080
3081
			return false;
3082
		}
3083
3084
		/**
3085
		 * Prepares all of our information to be outputted into a usable table.
3086
		 *
3087
		 * @since 2.2.0
3088
		 */
3089
		public function prepare_items() {
3090
			$columns               = $this->get_columns(); // Get all necessary column information.
3091
			$hidden                = array(); // No columns to hide, but we must set as an array.
3092
			$sortable              = array(); // No reason to make sortable columns.
3093
			$primary               = $this->get_primary_column_name(); // Column which has the row actions.
3094
			$this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers.
3095
3096
			// Process our bulk activations here.
3097
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3098
				$this->process_bulk_actions();
3099
			}
3100
3101
			// Store all of our plugin data into $items array so WP_List_Table can use it.
3102
			$this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() );
3103
		}
3104
3105
		/* *********** DEPRECATED METHODS *********** */
3106
3107
		/**
3108
		 * Retrieve plugin data, given the plugin name.
3109
		 *
3110
		 * @since      2.2.0
3111
		 * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead.
3112
		 * @see        TGM_Plugin_Activation::_get_plugin_data_from_name()
3113
		 *
3114
		 * @param string $name Name of the plugin, as it was registered.
3115
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
3116
		 * @return string|boolean Plugin slug if found, false otherwise.
3117
		 */
3118
		protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
3119
			_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' );
3120
3121
			return $this->tgmpa->_get_plugin_data_from_name( $name, $data );
3122
		}
3123
	}
3124
}
3125
3126
3127
if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
3128
3129
	/**
3130
	 * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+.
3131
	 *
3132
	 * @since 2.5.2
3133
	 *
3134
	 * {@internal The TGMPA_Bulk_Installer class was originally called TGM_Bulk_Installer.
3135
	 *            For more information, see that class.}}
3136
	 */
3137
	class TGM_Bulk_Installer {
3138
	}
3139
}
3140
if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
3141
3142
	/**
3143
	 * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+.
3144
	 *
3145
	 * @since 2.5.2
3146
	 *
3147
	 * {@internal The TGMPA_Bulk_Installer_Skin class was originally called TGM_Bulk_Installer_Skin.
3148
	 *            For more information, see that class.}}
3149
	 */
3150
	class TGM_Bulk_Installer_Skin {
3151
	}
3152
}
3153
3154
/**
3155
 * The WP_Upgrader file isn't always available. If it isn't available,
3156
 * we load it here.
3157
 *
3158
 * We check to make sure no action or activation keys are set so that WordPress
3159
 * does not try to re-include the class when processing upgrades or installs outside
3160
 * of the class.
3161
 *
3162
 * @since 2.2.0
3163
 */
3164
add_action( 'admin_init', 'tgmpa_load_bulk_installer' );
3165
if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
3166
	/**
3167
	 * Load bulk installer
3168
	 */
3169
	function tgmpa_load_bulk_installer() {
3170
		// Silently fail if 2.5+ is loaded *after* an older version.
3171
		if ( ! isset( $GLOBALS['tgmpa'] ) ) {
3172
			return;
3173
		}
3174
3175
		// Get TGMPA class instance.
3176
		$tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3177
3178
		if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) {
3179
			if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
3180
				require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
3181
			}
3182
3183
			if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) {
3184
3185
				/**
3186
				 * Installer class to handle bulk plugin installations.
3187
				 *
3188
				 * Extends WP_Upgrader and customizes to suit the installation of multiple
3189
				 * plugins.
3190
				 *
3191
				 * @since 2.2.0
3192
				 *
3193
				 * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}}
3194
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
3195
				 *            This was done to prevent backward compatibility issues with v2.3.6.}}
3196
				 *
3197
				 * @package TGM-Plugin-Activation
3198
				 * @author  Thomas Griffin
3199
				 * @author  Gary Jones
3200
				 */
3201
				class TGMPA_Bulk_Installer extends Plugin_Upgrader {
3202
					/**
3203
					 * Holds result of bulk plugin installation.
3204
					 *
3205
					 * @since 2.2.0
3206
					 *
3207
					 * @var string
3208
					 */
3209
					public $result;
3210
3211
					/**
3212
					 * Flag to check if bulk installation is occurring or not.
3213
					 *
3214
					 * @since 2.2.0
3215
					 *
3216
					 * @var boolean
3217
					 */
3218
					public $bulk = false;
3219
3220
					/**
3221
					 * TGMPA instance
3222
					 *
3223
					 * @since 2.5.0
3224
					 *
3225
					 * @var object
3226
					 */
3227
					protected $tgmpa;
3228
3229
					/**
3230
					 * Whether or not the destination directory needs to be cleared ( = on update).
3231
					 *
3232
					 * @since 2.5.0
3233
					 *
3234
					 * @var bool
3235
					 */
3236
					protected $clear_destination = false;
3237
3238
					/**
3239
					 * References parent constructor and sets defaults for class.
3240
					 *
3241
					 * @since 2.2.0
3242
					 *
3243
					 * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
3244
					 */
3245
					public function __construct( $skin = null ) {
3246
						// Get TGMPA class instance.
3247
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3248
3249
						parent::__construct( $skin );
3250
3251
						if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
3252
							$this->clear_destination = true;
3253
						}
3254
3255
						if ( $this->tgmpa->is_automatic ) {
3256
							$this->activate_strings();
3257
						}
3258
3259
						add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
3260
					}
3261
3262
					/**
3263
					 * Sets the correct activation strings for the installer skin to use.
3264
					 *
3265
					 * @since 2.2.0
3266
					 */
3267
					public function activate_strings() {
3268
						$this->strings['activation_failed']  = __( 'Plugin activation failed.', 'tgmpa' );
3269
						$this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' );
3270
					}
3271
3272
					/**
3273
					 * Performs the actual installation of each plugin.
3274
					 *
3275
					 * @since 2.2.0
3276
					 *
3277
					 * @see WP_Upgrader::run()
3278
					 *
3279
					 * @param array $options The installation config options.
3280
					 * @return null|array Return early if error, array of installation data on success.
3281
					 */
3282
					public function run( $options ) {
3283
						$result = parent::run( $options );
3284
3285
						// Reset the strings in case we changed one during automatic activation.
3286
						if ( $this->tgmpa->is_automatic ) {
3287
							if ( 'update' === $this->skin->options['install_type'] ) {
3288
								$this->upgrade_strings();
3289
							} else {
3290
								$this->install_strings();
3291
							}
3292
						}
3293
3294
						return $result;
3295
					}
3296
3297
					/**
3298
					 * Processes the bulk installation of plugins.
3299
					 *
3300
					 * @since 2.2.0
3301
					 *
3302
					 * {@internal This is basically a near identical copy of the WP Core
3303
					 * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with
3304
					 * new installs instead of upgrades.
3305
					 * For ease of future synchronizations, the adjustments are clearly commented, but no other
3306
					 * comments are added. Code style has been made to comply.}}
3307
					 *
3308
					 * @see Plugin_Upgrader::bulk_upgrade()
3309
					 * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
3310
					 * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}}
3311
					 *
3312
					 * @param array $plugins The plugin sources needed for installation.
3313
					 * @param array $args    Arbitrary passed extra arguments.
3314
					 * @return array|false   Install confirmation messages on success, false on failure.
3315
					 */
3316
					public function bulk_install( $plugins, $args = array() ) {
3317
						// [TGMPA + ] Hook auto-activation in.
3318
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3319
3320
						$defaults    = array(
3321
							'clear_update_cache' => true,
3322
						);
3323
						$parsed_args = wp_parse_args( $args, $defaults );
3324
3325
						$this->init();
3326
						$this->bulk = true;
3327
3328
						$this->install_strings(); // [TGMPA + ] adjusted.
3329
3330
						/* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
3331
3332
						/* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
3333
3334
						$this->skin->header();
3335
3336
						// Connect to the Filesystem first.
3337
						$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
3338
						if ( ! $res ) {
3339
							$this->skin->footer();
3340
							return false;
3341
						}
3342
3343
						$this->skin->bulk_header();
3344
3345
						/*
3346
						 * Only start maintenance mode if:
3347
						 * - running Multisite and there are one or more plugins specified, OR
3348
						 * - a plugin with an update available is currently active.
3349
						 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
3350
						 */
3351
						$maintenance = ( is_multisite() && ! empty( $plugins ) );
3352
3353
						/*
3354
						[TGMPA - ]
3355
						foreach ( $plugins as $plugin )
3356
							$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
3357
						*/
3358
						if ( $maintenance ) {
3359
							$this->maintenance_mode( true );
3360
						}
3361
3362
						$results = array();
3363
3364
						$this->update_count   = count( $plugins );
3365
						$this->update_current = 0;
3366
						foreach ( $plugins as $plugin ) {
3367
							$this->update_current++;
3368
3369
							/*
3370
							[TGMPA - ]
3371
							$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
3372
3373
							if ( !isset( $current->response[ $plugin ] ) ) {
3374
								$this->skin->set_result('up_to_date');
3375
								$this->skin->before();
3376
								$this->skin->feedback('up_to_date');
3377
								$this->skin->after();
3378
								$results[$plugin] = true;
3379
								continue;
3380
							}
3381
3382
							// Get the URL to the zip file.
3383
							$r = $current->response[ $plugin ];
3384
3385
							$this->skin->plugin_active = is_plugin_active($plugin);
3386
							*/
3387
3388
							$result = $this->run(
3389
								array(
3390
									'package'           => $plugin, // [TGMPA + ] adjusted.
3391
									'destination'       => WP_PLUGIN_DIR,
3392
									'clear_destination' => false, // [TGMPA + ] adjusted.
3393
									'clear_working'     => true,
3394
									'is_multi'          => true,
3395
									'hook_extra'        => array(
3396
										'plugin' => $plugin,
3397
									),
3398
								)
3399
							);
3400
3401
							$results[ $plugin ] = $this->result;
3402
3403
							// Prevent credentials auth screen from displaying multiple times.
3404
							if ( false === $result ) {
3405
								break;
3406
							}
3407
						} //end foreach $plugins
3408
3409
						$this->maintenance_mode( false );
3410
3411
						/**
3412
						 * Fires when the bulk upgrader process is complete.
3413
						 *
3414
						 * @since WP 3.6.0 / TGMPA 2.5.0
3415
						 *
3416
						 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
3417
						 *                              be a Theme_Upgrader or Core_Upgrade instance.
3418
						 * @param array           $data {
3419
						 *     Array of bulk item update data.
3420
						 *
3421
						 *     @type string $action   Type of action. Default 'update'.
3422
						 *     @type string $type     Type of update process. Accepts 'plugin', 'theme', or 'core'.
3423
						 *     @type bool   $bulk     Whether the update process is a bulk update. Default true.
3424
						 *     @type array  $packages Array of plugin, theme, or core packages to update.
3425
						 * }
3426
						 */
3427
						do_action( 'upgrader_process_complete', $this, array(
3428
							'action'  => 'install', // [TGMPA + ] adjusted.
3429
							'type'    => 'plugin',
3430
							'bulk'    => true,
3431
							'plugins' => $plugins,
3432
						) );
3433
3434
						$this->skin->bulk_footer();
3435
3436
						$this->skin->footer();
3437
3438
						// Cleanup our hooks, in case something else does a upgrade on this connection.
3439
						/* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
3440
3441
						// [TGMPA + ] Remove our auto-activation hook.
3442
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3443
3444
						// Force refresh of plugin update information.
3445
						wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
3446
3447
						return $results;
3448
					}
3449
3450
					/**
3451
					 * Handle a bulk upgrade request.
3452
					 *
3453
					 * @since 2.5.0
3454
					 *
3455
					 * @see Plugin_Upgrader::bulk_upgrade()
3456
					 *
3457
					 * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
3458
					 * @param array $args    Arbitrary passed extra arguments.
3459
					 * @return string|bool Install confirmation messages on success, false on failure.
3460
					 */
3461
					public function bulk_upgrade( $plugins, $args = array() ) {
3462
3463
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3464
3465
						$result = parent::bulk_upgrade( $plugins, $args );
3466
3467
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3468
3469
						return $result;
3470
					}
3471
3472
					/**
3473
					 * Abuse a filter to auto-activate plugins after installation.
3474
					 *
3475
					 * Hooked into the 'upgrader_post_install' filter hook.
3476
					 *
3477
					 * @since 2.5.0
3478
					 *
3479
					 * @param bool $bool The value we need to give back (true).
3480
					 * @return bool
3481
					 */
3482
					public function auto_activate( $bool ) {
3483
						// Only process the activation of installed plugins if the automatic flag is set to true.
3484
						if ( $this->tgmpa->is_automatic ) {
3485
							// Flush plugins cache so the headers of the newly installed plugins will be read correctly.
3486
							wp_clean_plugins_cache();
3487
3488
							// Get the installed plugin file.
3489
							$plugin_info = $this->plugin_info();
3490
3491
							// Don't try to activate on upgrade of active plugin as WP will do this already.
3492
							if ( ! is_plugin_active( $plugin_info ) ) {
3493
								$activate = activate_plugin( $plugin_info );
3494
3495
								// Adjust the success string based on the activation result.
3496
								$this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
3497
3498
								if ( is_wp_error( $activate ) ) {
3499
									$this->skin->error( $activate );
3500
									$this->strings['process_success'] .= $this->strings['activation_failed'];
3501
								} else {
3502
									$this->strings['process_success'] .= $this->strings['activation_success'];
3503
								}
3504
							}
3505
						}
3506
3507
						return $bool;
3508
					}
3509
				}
3510
			}
3511
3512
			if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) {
3513
3514
				/**
3515
				 * Installer skin to set strings for the bulk plugin installations..
3516
				 *
3517
				 * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
3518
				 * plugins.
3519
				 *
3520
				 * @since 2.2.0
3521
				 *
3522
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to
3523
				 *            TGMPA_Bulk_Installer_Skin.
3524
				 *            This was done to prevent backward compatibility issues with v2.3.6.}}
3525
				 *
3526
				 * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php
3527
				 *
3528
				 * @package TGM-Plugin-Activation
3529
				 * @author  Thomas Griffin
3530
				 * @author  Gary Jones
3531
				 */
3532
				class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
3533
					/**
3534
					 * Holds plugin info for each individual plugin installation.
3535
					 *
3536
					 * @since 2.2.0
3537
					 *
3538
					 * @var array
3539
					 */
3540
					public $plugin_info = array();
3541
3542
					/**
3543
					 * Holds names of plugins that are undergoing bulk installations.
3544
					 *
3545
					 * @since 2.2.0
3546
					 *
3547
					 * @var array
3548
					 */
3549
					public $plugin_names = array();
3550
3551
					/**
3552
					 * Integer to use for iteration through each plugin installation.
3553
					 *
3554
					 * @since 2.2.0
3555
					 *
3556
					 * @var integer
3557
					 */
3558
					public $i = 0;
3559
3560
					/**
3561
					 * TGMPA instance
3562
					 *
3563
					 * @since 2.5.0
3564
					 *
3565
					 * @var object
3566
					 */
3567
					protected $tgmpa;
3568
3569
					/**
3570
					 * Constructor. Parses default args with new ones and extracts them for use.
3571
					 *
3572
					 * @since 2.2.0
3573
					 *
3574
					 * @param array $args Arguments to pass for use within the class.
3575
					 */
3576
					public function __construct( $args = array() ) {
3577
						// Get TGMPA class instance.
3578
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3579
3580
						// Parse default and new args.
3581
						$defaults = array(
3582
							'url'          => '',
3583
							'nonce'        => '',
3584
							'names'        => array(),
3585
							'install_type' => 'install',
3586
						);
3587
						$args     = wp_parse_args( $args, $defaults );
3588
3589
						// Set plugin names to $this->plugin_names property.
3590
						$this->plugin_names = $args['names'];
3591
3592
						// Extract the new args.
3593
						parent::__construct( $args );
3594
					}
3595
3596
					/**
3597
					 * Sets install skin strings for each individual plugin.
3598
					 *
3599
					 * Checks to see if the automatic activation flag is set and uses the
3600
					 * the proper strings accordingly.
3601
					 *
3602
					 * @since 2.2.0
3603
					 */
3604
					public function add_strings() {
3605
						if ( 'update' === $this->options['install_type'] ) {
3606
							parent::add_strings();
3607
							/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3608
							$this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3609
						} else {
3610
							/* translators: 1: plugin name, 2: error message. */
3611
							$this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
3612
							/* translators: 1: plugin name. */
3613
							$this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'tgmpa' );
3614
3615
							if ( $this->tgmpa->is_automatic ) {
3616
								// Automatic activation strings.
3617
								$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' );
3618
								/* translators: 1: plugin name. */
3619
								$this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>';
3620
								$this->upgrader->strings['skin_upgrade_end']       = __( 'All installations and activations have been completed.', 'tgmpa' );
3621
								/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3622
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3623
							} else {
3624
								// Default installation strings.
3625
								$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' );
3626
								/* translators: 1: plugin name. */
3627
								$this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>';
3628
								$this->upgrader->strings['skin_upgrade_end']       = __( 'All installations have been completed.', 'tgmpa' );
3629
								/* translators: 1: plugin name, 2: action number 3: total number of actions. */
3630
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3631
							}
3632
						}
3633
					}
3634
3635
					/**
3636
					 * Outputs the header strings and necessary JS before each plugin installation.
3637
					 *
3638
					 * @since 2.2.0
3639
					 *
3640
					 * @param string $title Unused in this implementation.
3641
					 */
3642
					public function before( $title = '' ) {
3643
						if ( empty( $title ) ) {
3644
							$title = esc_html( $this->plugin_names[ $this->i ] );
3645
						}
3646
						parent::before( $title );
3647
					}
3648
3649
					/**
3650
					 * Outputs the footer strings and necessary JS after each plugin installation.
3651
					 *
3652
					 * Checks for any errors and outputs them if they exist, else output
3653
					 * success strings.
3654
					 *
3655
					 * @since 2.2.0
3656
					 *
3657
					 * @param string $title Unused in this implementation.
3658
					 */
3659
					public function after( $title = '' ) {
3660
						if ( empty( $title ) ) {
3661
							$title = esc_html( $this->plugin_names[ $this->i ] );
3662
						}
3663
						parent::after( $title );
3664
3665
						$this->i++;
3666
					}
3667
3668
					/**
3669
					 * Outputs links after bulk plugin installation is complete.
3670
					 *
3671
					 * @since 2.2.0
3672
					 */
3673
					public function bulk_footer() {
3674
						// Serve up the string to say installations (and possibly activations) are complete.
3675
						parent::bulk_footer();
3676
3677
						// Flush plugins cache so we can make sure that the installed plugins list is always up to date.
3678
						wp_clean_plugins_cache();
3679
3680
						$this->tgmpa->show_tgmpa_version();
3681
3682
						// Display message based on if all plugins are now active or not.
3683
						$update_actions = array();
3684
3685
						if ( $this->tgmpa->is_tgmpa_complete() ) {
3686
							// All plugins are active, so we display the complete string and hide the menu to protect users.
3687
							echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
3688
							$update_actions['dashboard'] = sprintf(
3689
								esc_html( $this->tgmpa->strings['complete'] ),
3690
								'<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>'
3691
							);
3692
						} else {
3693
							$update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>';
3694
						}
3695
3696
						/**
3697
						 * Filter the list of action links available following bulk plugin installs/updates.
3698
						 *
3699
						 * @since 2.5.0
3700
						 *
3701
						 * @param array $update_actions Array of plugin action links.
3702
						 * @param array $plugin_info    Array of information for the last-handled plugin.
3703
						 */
3704
						$update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
3705
3706
						if ( ! empty( $update_actions ) ) {
3707
							$this->feedback( implode( ' | ', (array) $update_actions ) );
3708
						}
3709
					}
3710
3711
					/* *********** DEPRECATED METHODS *********** */
3712
3713
					/**
3714
					 * Flush header output buffer.
3715
					 *
3716
					 * @since      2.2.0
3717
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3718
					 * @see        Bulk_Upgrader_Skin::flush_output()
3719
					 */
3720
					public function before_flush_output() {
3721
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3722
						$this->flush_output();
3723
					}
3724
3725
					/**
3726
					 * Flush footer output buffer and iterate $this->i to make sure the
3727
					 * installation strings reference the correct plugin.
3728
					 *
3729
					 * @since      2.2.0
3730
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3731
					 * @see        Bulk_Upgrader_Skin::flush_output()
3732
					 */
3733
					public function after_flush_output() {
3734
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3735
						$this->flush_output();
3736
						$this->i++;
3737
					}
3738
				}
3739
			}
3740
		}
3741
	}
3742
}
3743
3744
if ( ! class_exists( 'TGMPA_Utils' ) ) {
3745
3746
	/**
3747
	 * Generic utilities for TGMPA.
3748
	 *
3749
	 * All methods are static, poor-dev name-spacing class wrapper.
3750
	 *
3751
	 * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy.
3752
	 *
3753
	 * @since 2.5.0
3754
	 *
3755
	 * @package TGM-Plugin-Activation
3756
	 * @author  Juliette Reinders Folmer
3757
	 */
3758
	class TGMPA_Utils {
3759
		/**
3760
		 * Whether the PHP filter extension is enabled.
3761
		 *
3762
		 * @see http://php.net/book.filter
3763
		 *
3764
		 * @since 2.5.0
3765
		 *
3766
		 * @static
3767
		 *
3768
		 * @var bool $has_filters True is the extension is enabled.
3769
		 */
3770
		public static $has_filters;
3771
3772
		/**
3773
		 * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map().
3774
		 *
3775
		 * @since 2.5.0
3776
		 *
3777
		 * @static
3778
		 *
3779
		 * @param string $string Text to be wrapped.
3780
		 * @return string
3781
		 */
3782
		public static function wrap_in_em( $string ) {
3783
			return '<em>' . wp_kses_post( $string ) . '</em>';
3784
		}
3785
3786
		/**
3787
		 * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map().
3788
		 *
3789
		 * @since 2.5.0
3790
		 *
3791
		 * @static
3792
		 *
3793
		 * @param string $string Text to be wrapped.
3794
		 * @return string
3795
		 */
3796
		public static function wrap_in_strong( $string ) {
3797
			return '<strong>' . wp_kses_post( $string ) . '</strong>';
3798
		}
3799
3800
		/**
3801
		 * Helper function: Validate a value as boolean
3802
		 *
3803
		 * @since 2.5.0
3804
		 *
3805
		 * @static
3806
		 *
3807
		 * @param mixed $value Arbitrary value.
3808
		 * @return bool
3809
		 */
3810
		public static function validate_bool( $value ) {
3811
			if ( ! isset( self::$has_filters ) ) {
3812
				self::$has_filters = extension_loaded( 'filter' );
3813
			}
3814
3815
			if ( self::$has_filters ) {
3816
				return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
3817
			} else {
3818
				return self::emulate_filter_bool( $value );
3819
			}
3820
		}
3821
3822
		/**
3823
		 * Helper function: Cast a value to bool
3824
		 *
3825
		 * @since 2.5.0
3826
		 *
3827
		 * @static
3828
		 *
3829
		 * @param mixed $value Value to cast.
3830
		 * @return bool
3831
		 */
3832
		protected static function emulate_filter_bool( $value ) {
3833
			// @codingStandardsIgnoreStart
3834
			static $true  = array(
3835
				'1',
3836
				'true', 'True', 'TRUE',
3837
				'y', 'Y',
3838
				'yes', 'Yes', 'YES',
3839
				'on', 'On', 'ON',
3840
			);
3841
			static $false = array(
3842
				'0',
3843
				'false', 'False', 'FALSE',
3844
				'n', 'N',
3845
				'no', 'No', 'NO',
3846
				'off', 'Off', 'OFF',
3847
			);
3848
			// @codingStandardsIgnoreEnd
3849
3850
			if ( is_bool( $value ) ) {
3851
				return $value;
3852
			} elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
3853
				return (bool) $value;
3854
			} elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
3855
				return (bool) $value;
3856
			} elseif ( is_string( $value ) ) {
3857
				$value = trim( $value );
3858
				if ( in_array( $value, $true, true ) ) {
3859
					return true;
3860
				} elseif ( in_array( $value, $false, true ) ) {
3861
					return false;
3862
				} else {
3863
					return false;
3864
				}
3865
			}
3866
3867
			return false;
3868
		}
3869
	} // End of class TGMPA_Utils
3870
} // End of class_exists wrapper
3871