Completed
Pull Request — develop (#521)
by Juliette
02:50
created

TGM_Plugin_Activation::load_textdomain()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

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

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

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

Loading history...
823
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
824
					return true;
825
				}
826
827
				/* If we arrive here, we have the filesystem. */
828
829
				// Prep variables for Plugin_Installer_Skin class.
830
				$extra         = array();
831
				$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
832
				$source        = $this->get_download_url( $slug );
833
				$api           = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
834
				$api           = ( false !== $api ) ? $api : null;
835
836
				$url = add_query_arg(
837
					array(
838
						'action' => $install_type . '-plugin',
839
						'plugin' => urlencode( $slug ),
840
					),
841
					'update.php'
842
				);
843
844
				if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
845
					require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
846
				}
847
848
				$title     = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
849
				$skin_args = array(
850
					'type'   => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
851
					'title'  => sprintf( $title, $this->plugins[ $slug ]['name'] ),
852
					'url'    => esc_url_raw( $url ),
853
					'nonce'  => $install_type . '-plugin_' . $slug,
854
					'plugin' => '',
855
					'api'    => $api,
856
					'extra'  => $extra,
857
				);
858
				unset( $title );
859
860
				if ( 'update' === $install_type ) {
861
					$skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
862
					$skin                = new Plugin_Upgrader_Skin( $skin_args );
863
				} else {
864
					$skin = new Plugin_Installer_Skin( $skin_args );
865
				}
866
867
				// Create a new instance of Plugin_Upgrader.
868
				$upgrader = new Plugin_Upgrader( $skin );
869
870
				// Perform the action and install the plugin from the $source urldecode().
871
				add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
872
873
				if ( 'update' === $install_type ) {
874
					// Inject our info into the update transient.
875
					$to_inject                    = array( $slug => $this->plugins[ $slug ] );
876
					$to_inject[ $slug ]['source'] = $source;
877
					$this->inject_update_info( $to_inject );
878
879
					$upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
880
				} else {
881
					$upgrader->install( $source );
882
				}
883
884
				remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
885
886
				// Make sure we have the correct file path now the plugin is installed/updated.
887
				$this->populate_file_path( $slug );
888
889
				// Only activate plugins if the config option is set to true and the plugin isn't
890
				// already active (upgrade).
891
				if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
892
					$plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
893
					if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
894
						return true; // Finish execution of the function early as we encountered an error.
895
					}
896
				}
897
898
				$this->show_tgmpa_version();
899
900
				// Display message based on if all plugins are now active or not.
901
				if ( $this->is_tgmpa_complete() ) {
902
					echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ), '</p>';
903
					echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
904
				} else {
905
					echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
906
				}
907
908
				return true;
909
			} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
910
				// Activate action link was clicked.
911
				check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
912
913
				if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
914
					return true; // Finish execution of the function early as we encountered an error.
915
				}
916
			}
917
918
			return false;
919
		}
920
921
		/**
922
		 * Inject information into the 'update_plugins' site transient as WP checks that before running an update.
923
		 *
924
		 * @since 2.5.0
925
		 *
926
		 * @param array $plugins The plugin information for the plugins which are to be updated.
927
		 */
928
		public function inject_update_info( $plugins ) {
929
			$repo_updates = get_site_transient( 'update_plugins' );
930
931
			if ( ! is_object( $repo_updates ) ) {
932
				$repo_updates = new stdClass;
933
			}
934
935
			foreach ( $plugins as $slug => $plugin ) {
936
				$file_path = $plugin['file_path'];
937
938
				if ( empty( $repo_updates->response[ $file_path ] ) ) {
939
					$repo_updates->response[ $file_path ] = new stdClass;
940
				}
941
942
				// We only really need to set package, but let's do all we can in case WP changes something.
943
				$repo_updates->response[ $file_path ]->slug        = $slug;
944
				$repo_updates->response[ $file_path ]->plugin      = $file_path;
945
				$repo_updates->response[ $file_path ]->new_version = $plugin['version'];
946
				$repo_updates->response[ $file_path ]->package     = $plugin['source'];
947
				if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
948
					$repo_updates->response[ $file_path ]->url = $plugin['external_url'];
949
				}
950
			}
951
952
			set_site_transient( 'update_plugins', $repo_updates );
953
		}
954
955
		/**
956
		 * Adjust the plugin directory name if necessary.
957
		 *
958
		 * The final destination directory of a plugin is based on the subdirectory name found in the
959
		 * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
960
		 * subdirectory name is not the same as the expected slug and the plugin will not be recognized
961
		 * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
962
		 * the expected plugin slug.
963
		 *
964
		 * @since 2.5.0
965
		 *
966
		 * @param string       $source        Path to upgrade/zip-file-name.tmp/subdirectory/.
967
		 * @param string       $remote_source Path to upgrade/zip-file-name.tmp.
968
		 * @param \WP_Upgrader $upgrader      Instance of the upgrader which installs the plugin.
969
		 * @return string $source
970
		 */
971
		public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
972
			if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
973
				return $source;
974
			}
975
976
			// Check for single file plugins.
977
			$source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
978
			if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
979
				return $source;
980
			}
981
982
			// Multi-file plugin, let's see if the directory is correctly named.
983
			$desired_slug = '';
984
985
			// Figure out what the slug is supposed to be.
986
			if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
987
				$desired_slug = $upgrader->skin->options['extra']['slug'];
988
			} else {
989
				// Bulk installer contains less info, so fall back on the info registered here.
990
				foreach ( $this->plugins as $slug => $plugin ) {
991
					if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
992
						$desired_slug = $slug;
993
						break;
994
					}
995
				}
996
				unset( $slug, $plugin );
997
			}
998
999
			if ( ! empty( $desired_slug ) ) {
1000
				$subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
1001
1002
				if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
1003
					$from = untrailingslashit( $source );
1004
					$to   = trailingslashit( $remote_source ) . $desired_slug;
1005
1006
					if ( true === $GLOBALS['wp_filesystem']->move( $from, $to ) ) {
1007
						return trailingslashit( $to );
1008 View Code Duplication
					} else {
1009
						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 ) );
1010
					}
1011 View Code Duplication
				} elseif ( empty( $subdir_name ) ) {
1012
					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 ) );
1013
				}
1014
			}
1015
1016
			return $source;
1017
		}
1018
1019
		/**
1020
		 * Activate a single plugin and send feedback about the result to the screen.
1021
		 *
1022
		 * @since 2.5.0
1023
		 *
1024
		 * @param string $file_path Path within wp-plugins/ to main plugin file.
1025
		 * @param string $slug      Plugin slug.
1026
		 * @param bool   $automatic Whether this is an automatic activation after an install. Defaults to false.
1027
		 *                          This determines the styling of the output messages.
1028
		 * @return bool False if an error was encountered, true otherwise.
1029
		 */
1030
		protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
1031
			if ( $this->can_plugin_activate( $slug ) ) {
1032
				$activate = activate_plugin( $file_path );
1033
1034
				if ( is_wp_error( $activate ) ) {
1035
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
1036
						'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
1037
1038
					return false; // End it here if there is an error with activation.
1039
				} else {
1040
					if ( ! $automatic ) {
1041
						// Make sure message doesn't display again if bulk activation is performed
1042
						// immediately after a single activation.
1043
						if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1044
							echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
1045
						}
1046
					} else {
1047
						// Simpler message layout for use on the plugin install page.
1048
						echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
1049
					}
1050
				}
1051 View Code Duplication
			} elseif ( $this->is_plugin_active( $slug ) ) {
1052
				// No simpler message format provided as this message should never be encountered
1053
				// on the plugin install page.
1054
				echo '<div id="message" class="error"><p>',
1055
					sprintf(
1056
						esc_html( $this->strings['plugin_already_active'] ),
1057
						'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1058
					),
1059
					'</p></div>';
1060
			} elseif ( $this->does_plugin_require_update( $slug ) ) {
1061
				if ( ! $automatic ) {
1062
					// Make sure message doesn't display again if bulk activation is performed
1063
					// immediately after a single activation.
1064 View Code Duplication
					if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
1065
						echo '<div id="message" class="error"><p>',
1066
							sprintf(
1067
								esc_html( $this->strings['plugin_needs_higher_version'] ),
1068
								'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
1069
							),
1070
							'</p></div>';
1071
					}
1072
				} else {
1073
					// Simpler message layout for use on the plugin install page.
1074
					echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
1075
				}
1076
			}
1077
1078
			return true;
1079
		}
1080
1081
		/**
1082
		 * Echoes required plugin notice.
1083
		 *
1084
		 * Outputs a message telling users that a specific plugin is required for
1085
		 * their theme. If appropriate, it includes a link to the form page where
1086
		 * users can install and activate the plugin.
1087
		 *
1088
		 * Returns early if we're on the Install page.
1089
		 *
1090
		 * @since 1.0.0
1091
		 *
1092
		 * @global object $current_screen
1093
		 *
1094
		 * @return null Returns early if we're on the Install page.
1095
		 */
1096
		public function notices() {
1097
			// Remove nag on the install page / Return early if the nag message has been dismissed or user < author.
1098
			if ( $this->is_tgmpa_page() || get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) || ! current_user_can( apply_filters( 'tgmpa_show_admin_notice_capability', 'publish_posts' ) ) ) {
1099
				return;
1100
			}
1101
1102
			// Store for the plugin slugs by message type.
1103
			$message = array();
1104
1105
			// Initialize counters used to determine plurality of action link texts.
1106
			$install_link_count          = 0;
1107
			$update_link_count           = 0;
1108
			$activate_link_count         = 0;
1109
			$total_required_action_count = 0;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $total_required_action_count exceeds the maximum configured length of 25.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1110
1111
			foreach ( $this->plugins as $slug => $plugin ) {
1112
				if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) {
1113
					continue;
1114
				}
1115
1116
				if ( ! $this->is_plugin_installed( $slug ) ) {
1117
					if ( current_user_can( 'install_plugins' ) ) {
1118
						$install_link_count++;
1119
1120
						if ( true === $plugin['required'] ) {
1121
							$message['notice_can_install_required'][] = $slug;
1122
						} else {
1123
							$message['notice_can_install_recommended'][] = $slug;
1124
						}
1125
					}
1126
					if ( true === $plugin['required'] ) {
1127
						$total_required_action_count++;
1128
					}
1129
				} else {
1130
					if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
1131
						if ( current_user_can( 'activate_plugins' ) ) {
1132
							$activate_link_count++;
1133
1134
							if ( true === $plugin['required'] ) {
1135
								$message['notice_can_activate_required'][] = $slug;
1136
							} else {
1137
								$message['notice_can_activate_recommended'][] = $slug;
1138
							}
1139
						}
1140
						if ( true === $plugin['required'] ) {
1141
							$total_required_action_count++;
1142
						}
1143
					}
1144
1145
					if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1146
1147
						if ( current_user_can( 'update_plugins' ) ) {
1148
							$update_link_count++;
1149
1150
							if ( $this->does_plugin_require_update( $slug ) ) {
1151
								$message['notice_ask_to_update'][] = $slug;
1152
							} elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
1153
								$message['notice_ask_to_update_maybe'][] = $slug;
1154
							}
1155
						}
1156
						if ( true === $plugin['required'] ) {
1157
							$total_required_action_count++;
1158
						}
1159
					}
1160
				}
1161
			}
1162
			unset( $slug, $plugin );
1163
1164
			// If we have notices to display, we move forward.
1165
			if ( ! empty( $message ) || $total_required_action_count > 0 ) {
1166
				krsort( $message ); // Sort messages.
1167
				$rendered = '';
1168
1169
				// As add_settings_error() wraps the final message in a <p> and as the final message can't be
1170
				// filtered, using <p>'s in our html would render invalid html output.
1171
				$line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
1172
1173
				if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
1174
					$rendered = esc_html__( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html__( $this->strings['contact_admin'] );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1175
					$rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
1176
				} else {
1177
1178
					// If dismissable is false and a message is set, output it now.
1179
					if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
1180
						$rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
1181
					}
1182
1183
					// Render the individual message lines for the notice.
1184
					foreach ( $message as $type => $plugin_group ) {
1185
						$linked_plugins = array();
1186
1187
						// Get the external info link for a plugin if one is available.
1188
						foreach ( $plugin_group as $plugin_slug ) {
1189
							$linked_plugins[] = $this->get_info_link( $plugin_slug );
1190
						}
1191
						unset( $plugin_slug );
1192
1193
						$count          = count( $plugin_group );
1194
						$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
1195
						$last_plugin    = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
1196
						$imploded       = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
1197
1198
						$rendered .= sprintf(
1199
							$line_template,
1200
							sprintf(
1201
								translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
1202
								$imploded,
1203
								$count
1204
							)
1205
						);
1206
1207
					}
1208
					unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
1209
1210
					$rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
1211
				}
1212
1213
				// Register the nag messages and prepare them to be processed.
1214
				add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
1215
			}
1216
1217
			// Admin options pages already output settings_errors, so this is to avoid duplication.
1218
			if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
1219
				$this->display_settings_errors();
1220
			}
1221
		}
1222
1223
		/**
1224
		 * Generate the user action links for the admin notice.
1225
		 *
1226
		 * @since 2.x.x
1227
		 *
1228
		 * @param int $install_count  Number of plugins to install.
1229
		 * @param int $update_count   Number of plugins to update.
1230
		 * @param int $activate_count Number of plugins to activate.
1231
		 * @param int $line_template  Template for the HTML tag to output a line.
1232
		 * @return string Action links.
1233
		 */
1234
		protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
1235
			// Setup action links.
1236
			$action_links = array(
1237
				'install'  => '',
1238
				'update'   => '',
1239
				'activate' => '',
1240
				'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>' : '',
1241
			);
1242
1243
			$link_template = '<a href="%2$s">%1$s</a>';
1244
1245
			if ( current_user_can( 'install_plugins' ) ) {
1246 View Code Duplication
				if ( $install_count > 0 ) {
1247
					$action_links['install'] = sprintf(
1248
						$link_template,
1249
						translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),
1250
						esc_url( $this->get_tgmpa_status_url( 'install' ) )
1251
					);
1252
				}
1253 View Code Duplication
				if ( $update_count > 0 ) {
1254
					$action_links['update'] = sprintf(
1255
						$link_template,
1256
						translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),
1257
						esc_url( $this->get_tgmpa_status_url( 'update' ) )
1258
					);
1259
				}
1260
			}
1261
1262 View Code Duplication
			if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1263
				$action_links['activate'] = sprintf(
1264
					$link_template,
1265
					translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),
1266
					esc_url( $this->get_tgmpa_status_url( 'activate' ) )
1267
				);
1268
			}
1269
1270
			$action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
1271
1272
			$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
1273
1274
			if ( ! empty( $action_links ) ) {
1275
				$action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
1276
				return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
1277
			} else {
1278
				return '';
1279
			}
1280
		}
1281
1282
		/**
1283
		 * Get admin notice class.
1284
		 *
1285
		 * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1286
		 * (lowest supported version by TGMPA).
1287
		 *
1288
		 * @since 2.x.x
1289
		 *
1290
		 * @return string
1291
		 */
1292
		protected function get_admin_notice_class() {
1293
			if ( ! empty( $this->strings['nag_type'] ) ) {
1294
				return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1295
			} else {
1296
				if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1297
					return 'notice-warning';
1298
				} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1299
					return 'notice';
1300
				} else {
1301
					return 'updated';
1302
				}
1303
			}
1304
		}
1305
1306
		/**
1307
		 * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
1308
		 *
1309
		 * @since 2.5.0
1310
		 */
1311
		protected function display_settings_errors() {
1312
			global $wp_settings_errors;
1313
1314
			settings_errors( 'tgmpa' );
1315
1316
			foreach ( (array) $wp_settings_errors as $key => $details ) {
1317
				if ( 'tgmpa' === $details['setting'] ) {
1318
					unset( $wp_settings_errors[ $key ] );
1319
					break;
1320
				}
1321
			}
1322
		}
1323
1324
		/**
1325
		 * Add dismissable admin notices.
1326
		 *
1327
		 * Appends a link to the admin nag messages. If clicked, the admin notice disappears and no longer is visible to users.
1328
		 *
1329
		 * @since 2.1.0
1330
		 */
1331
		public function dismiss() {
1332
			if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismis-' . get_current_user_id() ) ) {
1333
				update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 );
1334
			}
1335
		}
1336
1337
		/**
1338
		 * Add individual plugin to our collection of plugins.
1339
		 *
1340
		 * If the required keys are not set or the plugin has already
1341
		 * been registered, the plugin is not added.
1342
		 *
1343
		 * @since 2.0.0
1344
		 *
1345
		 * @param array|null $plugin Array of plugin arguments or null if invalid argument.
1346
		 * @return null Return early if incorrect argument.
1347
		 */
1348
		public function register( $plugin ) {
1349
			if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) {
1350
				return;
1351
			}
1352
1353
			if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
1354
				return;
1355
			}
1356
1357
			$defaults = array(
1358
				'name'               => '',      // String
1359
				'slug'               => '',      // String
1360
				'source'             => 'repo',  // String
1361
				'required'           => false,   // Boolean
1362
				'version'            => '',      // String
1363
				'force_activation'   => false,   // Boolean
1364
				'force_deactivation' => false,   // Boolean
1365
				'external_url'       => '',      // String
1366
				'is_callable'        => '',      // String|Array.
1367
			);
1368
1369
			// Prepare the received data.
1370
			$plugin = wp_parse_args( $plugin, $defaults );
1371
1372
			// Standardize the received slug.
1373
			$plugin['slug'] = $this->sanitize_key( $plugin['slug'] );
1374
1375
			// Forgive users for using string versions of booleans or floats for version number.
1376
			$plugin['version']            = (string) $plugin['version'];
1377
			$plugin['source']             = empty( $plugin['source'] ) ? 'repo' : $plugin['source'];
1378
			$plugin['required']           = TGMPA_Utils::validate_bool( $plugin['required'] );
1379
			$plugin['force_activation']   = TGMPA_Utils::validate_bool( $plugin['force_activation'] );
1380
			$plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] );
1381
1382
			// Enrich the received data.
1383
			$plugin['file_path']   = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
1384
			$plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] );
1385
1386
			// Set the class properties.
1387
			$this->plugins[ $plugin['slug'] ]    = $plugin;
1388
			$this->sort_order[ $plugin['slug'] ] = $plugin['name'];
1389
1390
			// Should we add the force activation hook ?
1391
			if ( true === $plugin['force_activation'] ) {
1392
				$this->has_forced_activation = true;
1393
			}
1394
1395
			// Should we add the force deactivation hook ?
1396
			if ( true === $plugin['force_deactivation'] ) {
1397
				$this->has_forced_deactivation = true;
1398
			}
1399
		}
1400
1401
		/**
1402
		 * Determine what type of source the plugin comes from.
1403
		 *
1404
		 * @since 2.5.0
1405
		 *
1406
		 * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path
1407
		 *                       (= bundled) or an external URL.
1408
		 * @return string 'repo', 'external', or 'bundled'
1409
		 */
1410
		protected function get_plugin_source_type( $source ) {
1411
			if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) {
1412
				return 'repo';
1413
			} elseif ( preg_match( self::IS_URL_REGEX, $source ) ) {
1414
				return 'external';
1415
			} else {
1416
				return 'bundled';
1417
			}
1418
		}
1419
1420
		/**
1421
		 * Sanitizes a string key.
1422
		 *
1423
		 * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are*
1424
		 * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase
1425
		 * characters in the plugin directory path/slug. Silly them.
1426
		 *
1427
		 * @see https://developer.wordpress.org/reference/hooks/sanitize_key/
1428
		 *
1429
		 * @since 2.5.0
1430
		 *
1431
		 * @param string $key String key.
1432
		 * @return string Sanitized key
1433
		 */
1434
		public function sanitize_key( $key ) {
1435
			$raw_key = $key;
1436
			$key     = preg_replace( '`[^A-Za-z0-9_-]`', '', $key );
1437
1438
			/**
1439
			* Filter a sanitized key string.
1440
			*
1441
			* @since 3.0.0
1442
			*
1443
			* @param string $key     Sanitized key.
1444
			* @param string $raw_key The key prior to sanitization.
1445
			*/
1446
			return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key );
1447
		}
1448
1449
		/**
1450
		 * Amend default configuration settings.
1451
		 *
1452
		 * @since 2.0.0
1453
		 *
1454
		 * @param array $config Array of config options to pass as class properties.
1455
		 */
1456
		public function config( $config ) {
1457
			$keys = array(
1458
				'id',
1459
				'default_path',
1460
				'has_notices',
1461
				'dismissable',
1462
				'dismiss_msg',
1463
				'menu',
1464
				'parent_slug',
1465
				'capability',
1466
				'is_automatic',
1467
				'message',
1468
				'strings',
1469
			);
1470
1471
			foreach ( $keys as $key ) {
1472
				if ( isset( $config[ $key ] ) ) {
1473
					if ( is_array( $config[ $key ] ) ) {
1474
						$this->$key = array_merge( $this->$key, $config[ $key ] );
1475
					} else {
1476
						$this->$key = $config[ $key ];
1477
					}
1478
				}
1479
			}
1480
		}
1481
1482
		/**
1483
		 * Amend action link after plugin installation.
1484
		 *
1485
		 * @since 2.0.0
1486
		 *
1487
		 * @param array $install_actions Existing array of actions.
1488
		 * @return array Amended array of actions.
1489
		 */
1490
		public function actions( $install_actions ) {
1491
			// Remove action links on the TGMPA install page.
1492
			if ( $this->is_tgmpa_page() ) {
1493
				return false;
1494
			}
1495
1496
			return $install_actions;
1497
		}
1498
1499
		/**
1500
		 * Flushes the plugins cache on theme switch to prevent stale entries
1501
		 * from remaining in the plugin table.
1502
		 *
1503
		 * @since 2.4.0
1504
		 *
1505
		 * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache.
1506
		 *                                 Parameter added in v2.5.0.
1507
		 */
1508
		public function flush_plugins_cache( $clear_update_cache = true ) {
1509
			wp_clean_plugins_cache( $clear_update_cache );
1510
		}
1511
1512
		/**
1513
		 * Set file_path key for each installed plugin.
1514
		 *
1515
		 * @since 2.1.0
1516
		 *
1517
		 * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
1518
		 *                            Parameter added in v2.5.0.
1519
		 */
1520
		public function populate_file_path( $plugin_slug = '' ) {
1521
			if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) {
1522
				$this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug );
1523
			} else {
1524
				// Add file_path key for all plugins.
1525
				foreach ( $this->plugins as $slug => $values ) {
1526
					$this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug );
1527
				}
1528
			}
1529
		}
1530
1531
		/**
1532
		 * Helper function to extract the file path of the plugin file from the
1533
		 * plugin slug, if the plugin is installed.
1534
		 *
1535
		 * @since 2.0.0
1536
		 *
1537
		 * @param string $slug Plugin slug (typically folder name) as provided by the developer.
1538
		 * @return string Either file path for plugin if installed, or just the plugin slug.
1539
		 */
1540
		protected function _get_plugin_basename_from_slug( $slug ) {
1541
			$keys = array_keys( $this->get_plugins() );
1542
1543
			foreach ( $keys as $key ) {
1544
				if ( preg_match( '|^' . $slug . '/|', $key ) ) {
1545
					return $key;
1546
				}
1547
			}
1548
1549
			return $slug;
1550
		}
1551
1552
		/**
1553
		 * Retrieve plugin data, given the plugin name.
1554
		 *
1555
		 * Loops through the registered plugins looking for $name. If it finds it,
1556
		 * it returns the $data from that plugin. Otherwise, returns false.
1557
		 *
1558
		 * @since 2.1.0
1559
		 *
1560
		 * @param string $name Name of the plugin, as it was registered.
1561
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
1562
		 * @return string|boolean Plugin slug if found, false otherwise.
1563
		 */
1564
		public function _get_plugin_data_from_name( $name, $data = 'slug' ) {
1565
			foreach ( $this->plugins as $values ) {
1566
				if ( $name === $values['name'] && isset( $values[ $data ] ) ) {
1567
					return $values[ $data ];
1568
				}
1569
			}
1570
1571
			return false;
1572
		}
1573
1574
		/**
1575
		 * Retrieve the download URL for a package.
1576
		 *
1577
		 * @since 2.5.0
1578
		 *
1579
		 * @param string $slug Plugin slug.
1580
		 * @return string Plugin download URL or path to local file or empty string if undetermined.
1581
		 */
1582
		public function get_download_url( $slug ) {
1583
			$dl_source = '';
1584
1585
			switch ( $this->plugins[ $slug ]['source_type'] ) {
1586
				case 'repo':
1587
					return $this->get_wp_repo_download_url( $slug );
1588
				case 'external':
1589
					return $this->plugins[ $slug ]['source'];
1590
				case 'bundled':
1591
					return $this->default_path . $this->plugins[ $slug ]['source'];
1592
			}
1593
1594
			return $dl_source; // Should never happen.
1595
		}
1596
1597
		/**
1598
		 * Retrieve the download URL for a WP repo package.
1599
		 *
1600
		 * @since 2.5.0
1601
		 *
1602
		 * @param string $slug Plugin slug.
1603
		 * @return string Plugin download URL.
1604
		 */
1605
		protected function get_wp_repo_download_url( $slug ) {
1606
			$source = '';
1607
			$api    = $this->get_plugins_api( $slug );
1608
1609
			if ( false !== $api && isset( $api->download_link ) ) {
1610
				$source = $api->download_link;
1611
			}
1612
1613
			return $source;
1614
		}
1615
1616
		/**
1617
		 * Try to grab information from WordPress API.
1618
		 *
1619
		 * @since 2.5.0
1620
		 *
1621
		 * @param string $slug Plugin slug.
1622
		 * @return object Plugins_api response object on success, WP_Error on failure.
1623
		 */
1624
		protected function get_plugins_api( $slug ) {
1625
			static $api = array(); // Cache received responses.
1626
1627
			if ( ! isset( $api[ $slug ] ) ) {
1628
				if ( ! function_exists( 'plugins_api' ) ) {
1629
					require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
1630
				}
1631
1632
				$response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
1633
1634
				$api[ $slug ] = false;
1635
1636
				if ( is_wp_error( $response ) ) {
1637
					wp_die( esc_html( $this->strings['oops'] ) );
1638
				} else {
1639
					$api[ $slug ] = $response;
1640
				}
1641
			}
1642
1643
			return $api[ $slug ];
1644
		}
1645
1646
		/**
1647
		 * Retrieve a link to a plugin information page.
1648
		 *
1649
		 * @since 2.5.0
1650
		 *
1651
		 * @param string $slug Plugin slug.
1652
		 * @return string Fully formed html link to a plugin information page if available
1653
		 *                or the plugin name if not.
1654
		 */
1655
		public function get_info_link( $slug ) {
1656
			if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) {
1657
				$link = sprintf(
1658
					'<a href="%1$s" target="_blank">%2$s</a>',
1659
					esc_url( $this->plugins[ $slug ]['external_url'] ),
1660
					esc_html( $this->plugins[ $slug ]['name'] )
1661
				);
1662
			} elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) {
1663
				$url = add_query_arg(
1664
					array(
1665
						'tab'       => 'plugin-information',
1666
						'plugin'    => urlencode( $slug ),
1667
						'TB_iframe' => 'true',
1668
						'width'     => '640',
1669
						'height'    => '500',
1670
					),
1671
					self_admin_url( 'plugin-install.php' )
1672
				);
1673
1674
				$link = sprintf(
1675
					'<a href="%1$s" class="thickbox">%2$s</a>',
1676
					esc_url( $url ),
1677
					esc_html( $this->plugins[ $slug ]['name'] )
1678
				);
1679
			} else {
1680
				$link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink.
1681
			}
1682
1683
			return $link;
1684
		}
1685
1686
		/**
1687
		 * Determine if we're on the TGMPA Install page.
1688
		 *
1689
		 * @since 2.1.0
1690
		 *
1691
		 * @return boolean True when on the TGMPA page, false otherwise.
1692
		 */
1693
		protected function is_tgmpa_page() {
1694
			return isset( $_GET['page'] ) && $this->menu === $_GET['page'];
1695
		}
1696
1697
		/**
1698
		 * Retrieve the URL to the TGMPA Install page.
1699
		 *
1700
		 * I.e. depending on the config settings passed something along the lines of:
1701
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins
1702
		 *
1703
		 * @since 2.5.0
1704
		 *
1705
		 * @return string Properly encoded URL (not escaped).
1706
		 */
1707
		public function get_tgmpa_url() {
1708
			static $url;
1709
1710
			if ( ! isset( $url ) ) {
1711
				$parent = $this->parent_slug;
1712
				if ( false === strpos( $parent, '.php' ) ) {
1713
					$parent = 'admin.php';
1714
				}
1715
				$url = add_query_arg(
1716
					array(
1717
						'page' => urlencode( $this->menu ),
1718
					),
1719
					self_admin_url( $parent )
1720
				);
1721
			}
1722
1723
			return $url;
1724
		}
1725
1726
		/**
1727
		 * Retrieve the URL to the TGMPA Install page for a specific plugin status (view).
1728
		 *
1729
		 * I.e. depending on the config settings passed something along the lines of:
1730
		 * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install
1731
		 *
1732
		 * @since 2.5.0
1733
		 *
1734
		 * @param string $status Plugin status - either 'install', 'update' or 'activate'.
1735
		 * @return string Properly encoded URL (not escaped).
1736
		 */
1737
		public function get_tgmpa_status_url( $status ) {
1738
			return add_query_arg(
1739
				array(
1740
					'plugin_status' => urlencode( $status ),
1741
				),
1742
				$this->get_tgmpa_url()
1743
			);
1744
		}
1745
1746
		/**
1747
		 * Determine whether there are open actions for plugins registered with TGMPA.
1748
		 *
1749
		 * @since 2.5.0
1750
		 *
1751
		 * @return bool True if complete, i.e. no outstanding actions. False otherwise.
1752
		 */
1753
		public function is_tgmpa_complete() {
1754
			$complete = true;
1755
			foreach ( $this->plugins as $slug => $plugin ) {
1756
				if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
1757
					$complete = false;
1758
					break;
1759
				}
1760
			}
1761
1762
			return $complete;
1763
		}
1764
1765
		/**
1766
		 * Check if a plugin is installed. Does not take must-use plugins into account.
1767
		 *
1768
		 * @since 2.5.0
1769
		 *
1770
		 * @param string $slug Plugin slug.
1771
		 * @return bool True if installed, false otherwise.
1772
		 */
1773
		public function is_plugin_installed( $slug ) {
1774
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1775
1776
			return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) );
1777
		}
1778
1779
		/**
1780
		 * Check if a plugin is active.
1781
		 *
1782
		 * @since 2.5.0
1783
		 *
1784
		 * @param string $slug Plugin slug.
1785
		 * @return bool True if active, false otherwise.
1786
		 */
1787
		public function is_plugin_active( $slug ) {
1788
			return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) );
1789
		}
1790
1791
		/**
1792
		 * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required
1793
		 * available, check whether the current install meets them.
1794
		 *
1795
		 * @since 2.5.0
1796
		 *
1797
		 * @param string $slug Plugin slug.
1798
		 * @return bool True if OK to update, false otherwise.
1799
		 */
1800
		public function can_plugin_update( $slug ) {
1801
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1802
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1803
				return true;
1804
			}
1805
1806
			$api = $this->get_plugins_api( $slug );
1807
1808
			if ( false !== $api && isset( $api->requires ) ) {
1809
				return version_compare( $this->wp_version, $api->requires, '>=' );
1810
			}
1811
1812
			// No usable info received from the plugins API, presume we can update.
1813
			return true;
1814
		}
1815
1816
		/**
1817
		 * Check to see if the plugin is 'updatetable', i.e. installed, with an update available
1818
		 * and no WP version requirements blocking it.
1819
		 *
1820
		 * @since 2.x.x
1821
		 *
1822
		 * @param string $slug Plugin slug.
1823
		 * @return bool True if OK to proceed with update, false otherwise.
1824
		 */
1825
		public function is_plugin_updatetable( $slug ) {
1826
			if ( ! $this->is_plugin_installed( $slug ) ) {
1827
				return false;
1828
			} else {
1829
				return ( $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) );
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->does_plugin_have_update($slug) of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
1830
			}
1831
		}
1832
1833
		/**
1834
		 * Check if a plugin can be activated, i.e. is not currently active and meets the minimum
1835
		 * plugin version requirements set in TGMPA (if any).
1836
		 *
1837
		 * @since 2.5.0
1838
		 *
1839
		 * @param string $slug Plugin slug.
1840
		 * @return bool True if OK to activate, false otherwise.
1841
		 */
1842
		public function can_plugin_activate( $slug ) {
1843
			return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) );
1844
		}
1845
1846
		/**
1847
		 * Retrieve the version number of an installed plugin.
1848
		 *
1849
		 * @since 2.5.0
1850
		 *
1851
		 * @param string $slug Plugin slug.
1852
		 * @return string Version number as string or an empty string if the plugin is not installed
1853
		 *                or version unknown (plugins which don't comply with the plugin header standard).
1854
		 */
1855
		public function get_installed_version( $slug ) {
1856
			$installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
1857
1858
			if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) {
1859
				return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'];
1860
			}
1861
1862
			return '';
1863
		}
1864
1865
		/**
1866
		 * Check whether a plugin complies with the minimum version requirements.
1867
		 *
1868
		 * @since 2.5.0
1869
		 *
1870
		 * @param string $slug Plugin slug.
1871
		 * @return bool True when a plugin needs to be updated, otherwise false.
1872
		 */
1873
		public function does_plugin_require_update( $slug ) {
1874
			$installed_version = $this->get_installed_version( $slug );
1875
			$minimum_version   = $this->plugins[ $slug ]['version'];
1876
1877
			return version_compare( $minimum_version, $installed_version, '>' );
1878
		}
1879
1880
		/**
1881
		 * Check whether there is an update available for a plugin.
1882
		 *
1883
		 * @since 2.5.0
1884
		 *
1885
		 * @param string $slug Plugin slug.
1886
		 * @return false|string Version number string of the available update or false if no update available.
1887
		 */
1888
		public function does_plugin_have_update( $slug ) {
1889
			// Presume bundled and external plugins will point to a package which meets the minimum required version.
1890
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1891
				if ( $this->does_plugin_require_update( $slug ) ) {
1892
					return $this->plugins[ $slug ]['version'];
1893
				}
1894
1895
				return false;
1896
			}
1897
1898
			$repo_updates = get_site_transient( 'update_plugins' );
1899
1900 View Code Duplication
			if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) {
1901
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version;
1902
			}
1903
1904
			return false;
1905
		}
1906
1907
		/**
1908
		 * Retrieve potential upgrade notice for a plugin.
1909
		 *
1910
		 * @since 2.5.0
1911
		 *
1912
		 * @param string $slug Plugin slug.
1913
		 * @return string The upgrade notice or an empty string if no message was available or provided.
1914
		 */
1915
		public function get_upgrade_notice( $slug ) {
1916
			// We currently can't get reliable info on non-WP-repo plugins - issue #380.
1917
			if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
1918
				return '';
1919
			}
1920
1921
			$repo_updates = get_site_transient( 'update_plugins' );
1922
1923 View Code Duplication
			if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) {
1924
				return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice;
1925
			}
1926
1927
			return '';
1928
		}
1929
1930
		/**
1931
		 * Wrapper around the core WP get_plugins function, making sure it's actually available.
1932
		 *
1933
		 * @since 2.5.0
1934
		 *
1935
		 * @param string $plugin_folder Optional. Relative path to single plugin folder.
1936
		 * @return array Array of installed plugins with plugin information.
1937
		 */
1938
		public function get_plugins( $plugin_folder = '' ) {
1939
			if ( ! function_exists( 'get_plugins' ) ) {
1940
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
1941
			}
1942
1943
			return get_plugins( $plugin_folder );
1944
		}
1945
1946
		/**
1947
		 * Delete dismissable nag option when theme is switched.
1948
		 *
1949
		 * This ensures that the user(s) is/are again reminded via nag of required
1950
		 * and/or recommended plugins if they re-activate the theme.
1951
		 *
1952
		 * @since 2.1.1
1953
		 */
1954
		public function update_dismiss() {
1955
			delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true );
1956
		}
1957
1958
		/**
1959
		 * Forces plugin activation if the parameter 'force_activation' is
1960
		 * set to true.
1961
		 *
1962
		 * This allows theme authors to specify certain plugins that must be
1963
		 * active at all times while using the current theme.
1964
		 *
1965
		 * Please take special care when using this parameter as it has the
1966
		 * potential to be harmful if not used correctly. Setting this parameter
1967
		 * to true will not allow the specified plugin to be deactivated unless
1968
		 * the user switches themes.
1969
		 *
1970
		 * @since 2.2.0
1971
		 */
1972
		public function force_activation() {
1973
			foreach ( $this->plugins as $slug => $plugin ) {
1974
				if ( true === $plugin['force_activation'] ) {
1975
					if ( ! $this->is_plugin_installed( $slug ) ) {
1976
						// Oops, plugin isn't there so iterate to next condition.
1977
						continue;
1978
					} elseif ( $this->can_plugin_activate( $slug ) ) {
1979
						// There we go, activate the plugin.
1980
						activate_plugin( $plugin['file_path'] );
1981
					}
1982
				}
1983
			}
1984
		}
1985
1986
		/**
1987
		 * Forces plugin deactivation if the parameter 'force_deactivation'
1988
		 * is set to true.
1989
		 *
1990
		 * This allows theme authors to specify certain plugins that must be
1991
		 * deactivated upon switching from the current theme to another.
1992
		 *
1993
		 * Please take special care when using this parameter as it has the
1994
		 * potential to be harmful if not used correctly.
1995
		 *
1996
		 * @since 2.2.0
1997
		 */
1998
		public function force_deactivation() {
1999
			foreach ( $this->plugins as $slug => $plugin ) {
2000
				// Only proceed forward if the parameter is set to true and plugin is active.
2001
				if ( true === $plugin['force_deactivation'] && $this->is_plugin_active( $slug ) ) {
2002
					deactivate_plugins( $plugin['file_path'] );
2003
				}
2004
			}
2005
		}
2006
2007
		/**
2008
		 * Echo the current TGMPA version number to the page.
2009
		 */
2010
		public function show_tgmpa_version() {
2011
			echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',
2012
				esc_html( sprintf( _x( 'TGMPA v%s', '%s = version number', 'tgmpa' ), self::TGMPA_VERSION ) ),
2013
				'</small></strong></p>';
2014
		}
2015
2016
		/**
2017
		 * Returns the singleton instance of the class.
2018
		 *
2019
		 * @since 2.4.0
2020
		 *
2021
		 * @return object The TGM_Plugin_Activation object.
2022
		 */
2023
		public static function get_instance() {
2024
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
2025
				self::$instance = new self();
2026
			}
2027
2028
			return self::$instance;
2029
		}
2030
	}
2031
2032
	if ( ! function_exists( 'load_tgm_plugin_activation' ) ) {
2033
		/**
2034
		 * Ensure only one instance of the class is ever invoked.
2035
		 */
2036
		function load_tgm_plugin_activation() {
2037
			$GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance();
2038
		}
2039
	}
2040
2041
	if ( did_action( 'plugins_loaded' ) ) {
2042
		load_tgm_plugin_activation();
2043
	} else {
2044
		add_action( 'plugins_loaded', 'load_tgm_plugin_activation' );
2045
	}
2046
}
2047
2048
if ( ! function_exists( 'tgmpa' ) ) {
2049
	/**
2050
	 * Helper function to register a collection of required plugins.
2051
	 *
2052
	 * @since 2.0.0
2053
	 * @api
2054
	 *
2055
	 * @param array $plugins An array of plugin arrays.
2056
	 * @param array $config  Optional. An array of configuration values.
2057
	 */
2058
	function tgmpa( $plugins, $config = array() ) {
2059
		$instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2060
2061
		foreach ( $plugins as $plugin ) {
2062
			call_user_func( array( $instance, 'register' ), $plugin );
2063
		}
2064
2065
		if ( ! empty( $config ) && is_array( $config ) ) {
2066
			// Send out notices for deprecated arguments passed.
2067
			if ( isset( $config['notices'] ) ) {
2068
				_deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' );
2069
				if ( ! isset( $config['has_notices'] ) ) {
2070
					$config['has_notices'] = $config['notices'];
2071
				}
2072
			}
2073
2074
			if ( isset( $config['parent_menu_slug'] ) ) {
2075
				_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.' );
2076
			}
2077
			if ( isset( $config['parent_url_slug'] ) ) {
2078
				_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.' );
2079
			}
2080
2081
			call_user_func( array( $instance, 'config' ), $config );
2082
		}
2083
	}
2084
}
2085
2086
/**
2087
 * WP_List_Table isn't always available. If it isn't available,
2088
 * we load it here.
2089
 *
2090
 * @since 2.2.0
2091
 */
2092
if ( ! class_exists( 'WP_List_Table' ) ) {
2093
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
2094
}
2095
2096
if ( ! class_exists( 'TGMPA_List_Table' ) ) {
2097
2098
	/**
2099
	 * List table class for handling plugins.
2100
	 *
2101
	 * Extends the WP_List_Table class to provide a future-compatible
2102
	 * way of listing out all required/recommended plugins.
2103
	 *
2104
	 * Gives users an interface similar to the Plugin Administration
2105
	 * area with similar (albeit stripped down) capabilities.
2106
	 *
2107
	 * This class also allows for the bulk install of plugins.
2108
	 *
2109
	 * @since 2.2.0
2110
	 *
2111
	 * @package TGM-Plugin-Activation
2112
	 * @author  Thomas Griffin
2113
	 * @author  Gary Jones
2114
	 */
2115
	class TGMPA_List_Table extends WP_List_Table {
2116
		/**
2117
		 * TGMPA instance.
2118
		 *
2119
		 * @since 2.5.0
2120
		 *
2121
		 * @var object
2122
		 */
2123
		protected $tgmpa;
2124
2125
		/**
2126
		 * The currently chosen view.
2127
		 *
2128
		 * @since 2.5.0
2129
		 *
2130
		 * @var string One of: 'all', 'install', 'update', 'activate'
2131
		 */
2132
		public $view_context = 'all';
2133
2134
		/**
2135
		 * The plugin counts for the various views.
2136
		 *
2137
		 * @since 2.5.0
2138
		 *
2139
		 * @var array
2140
		 */
2141
		protected $view_totals = array(
2142
			'all'      => 0,
2143
			'install'  => 0,
2144
			'update'   => 0,
2145
			'activate' => 0,
2146
		);
2147
2148
		/**
2149
		 * References parent constructor and sets defaults for class.
2150
		 *
2151
		 * @since 2.2.0
2152
		 */
2153
		public function __construct() {
2154
			$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
2155
2156
			parent::__construct(
2157
				array(
2158
					'singular' => 'plugin',
2159
					'plural'   => 'plugins',
2160
					'ajax'     => false,
2161
				)
2162
			);
2163
2164
			if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) {
2165
				$this->view_context = sanitize_key( $_REQUEST['plugin_status'] );
2166
			}
2167
2168
			add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) );
2169
		}
2170
2171
		/**
2172
		 * Get a list of CSS classes for the <table> tag.
2173
		 *
2174
		 * Overruled to prevent the 'plural' argument from being added.
2175
		 *
2176
		 * @since 2.5.0
2177
		 *
2178
		 * @return array CSS classnames.
2179
		 */
2180
		public function get_table_classes() {
2181
			return array( 'widefat', 'fixed' );
2182
		}
2183
2184
		/**
2185
		 * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table.
2186
		 *
2187
		 * @since 2.2.0
2188
		 *
2189
		 * @return array $table_data Information for use in table.
2190
		 */
2191
		protected function _gather_plugin_data() {
2192
			// Load thickbox for plugin links.
2193
			$this->tgmpa->admin_init();
2194
			$this->tgmpa->thickbox();
2195
2196
			// Categorize the plugins which have open actions.
2197
			$plugins = $this->categorize_plugins_to_views();
2198
2199
			// Set the counts for the view links.
2200
			$this->set_view_totals( $plugins );
2201
2202
			// Prep variables for use and grab list of all installed plugins.
2203
			$table_data = array();
2204
			$i          = 0;
2205
2206
			// Redirect to the 'all' view if no plugins were found for the selected view context.
2207
			if ( empty( $plugins[ $this->view_context ] ) ) {
2208
				$this->view_context = 'all';
2209
			}
2210
2211
			foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) {
2212
				$table_data[ $i ]['sanitized_plugin']  = $plugin['name'];
2213
				$table_data[ $i ]['slug']              = $slug;
2214
				$table_data[ $i ]['plugin']            = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>';
2215
				$table_data[ $i ]['source']            = $this->get_plugin_source_type_text( $plugin['source_type'] );
2216
				$table_data[ $i ]['type']              = $this->get_plugin_advise_type_text( $plugin['required'] );
2217
				$table_data[ $i ]['status']            = $this->get_plugin_status_text( $slug );
2218
				$table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug );
2219
				$table_data[ $i ]['minimum_version']   = $plugin['version'];
2220
				$table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug );
2221
2222
				// Prep the upgrade notice info.
2223
				$upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug );
2224
				if ( ! empty( $upgrade_notice ) ) {
2225
					$table_data[ $i ]['upgrade_notice'] = $upgrade_notice;
2226
2227
					add_action( "tgmpa_after_plugin_row_$slug", array( $this, 'wp_plugin_update_row' ), 10, 2 );
2228
				}
2229
2230
				$table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin );
2231
2232
				$i++;
2233
			}
2234
2235
			return $table_data;
2236
		}
2237
2238
		/**
2239
		 * Categorize the plugins which have open actions into views for the TGMPA page.
2240
		 *
2241
		 * @since 2.5.0
2242
		 */
2243
		protected function categorize_plugins_to_views() {
2244
			$plugins = array(
2245
				'all'      => array(), // Meaning: all plugins which still have open actions.
2246
				'install'  => array(),
2247
				'update'   => array(),
2248
				'activate' => array(),
2249
			);
2250
2251
			foreach ( $this->tgmpa->plugins as $slug => $plugin ) {
2252
				if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2253
					// No need to display plugins if they are installed, up-to-date and active.
2254
					continue;
2255
				} else {
2256
					$plugins['all'][ $slug ] = $plugin;
2257
2258
					if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2259
						$plugins['install'][ $slug ] = $plugin;
2260
					} else {
2261
						if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2262
							$plugins['update'][ $slug ] = $plugin;
2263
						}
2264
2265
						if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2266
							$plugins['activate'][ $slug ] = $plugin;
2267
						}
2268
					}
2269
				}
2270
			}
2271
2272
			return $plugins;
2273
		}
2274
2275
		/**
2276
		 * Set the counts for the view links.
2277
		 *
2278
		 * @since 2.5.0
2279
		 *
2280
		 * @param array $plugins Plugins order by view.
2281
		 */
2282
		protected function set_view_totals( $plugins ) {
2283
			foreach ( $plugins as $type => $list ) {
2284
				$this->view_totals[ $type ] = count( $list );
2285
			}
2286
		}
2287
2288
		/**
2289
		 * Get the plugin required/recommended text string.
2290
		 *
2291
		 * @since 2.5.0
2292
		 *
2293
		 * @param string $required Plugin required setting.
2294
		 * @return string
2295
		 */
2296
		protected function get_plugin_advise_type_text( $required ) {
2297
			if ( true === $required ) {
2298
				return __( 'Required', 'tgmpa' );
2299
			}
2300
2301
			return __( 'Recommended', 'tgmpa' );
2302
		}
2303
2304
		/**
2305
		 * Get the plugin source type text string.
2306
		 *
2307
		 * @since 2.5.0
2308
		 *
2309
		 * @param string $type Plugin type.
2310
		 * @return string
2311
		 */
2312
		protected function get_plugin_source_type_text( $type ) {
2313
			$string = '';
2314
2315
			switch ( $type ) {
2316
				case 'repo':
2317
					$string = __( 'WordPress Repository', 'tgmpa' );
2318
					break;
2319
				case 'external':
2320
					$string = __( 'External Source', 'tgmpa' );
2321
					break;
2322
				case 'bundled':
2323
					$string = __( 'Pre-Packaged', 'tgmpa' );
2324
					break;
2325
			}
2326
2327
			return $string;
2328
		}
2329
2330
		/**
2331
		 * Determine the plugin status message.
2332
		 *
2333
		 * @since 2.5.0
2334
		 *
2335
		 * @param string $slug Plugin slug.
2336
		 * @return string
2337
		 */
2338
		protected function get_plugin_status_text( $slug ) {
2339
			if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
2340
				return __( 'Not Installed', 'tgmpa' );
2341
			}
2342
2343
			if ( ! $this->tgmpa->is_plugin_active( $slug ) ) {
2344
				$install_status = __( 'Installed But Not Activated', 'tgmpa' );
2345
			} else {
2346
				$install_status = __( 'Active', 'tgmpa' );
2347
			}
2348
2349
			$update_status = '';
2350
2351
			if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
2352
				$update_status = __( 'Required Update not Available', 'tgmpa' );
2353
2354
			} elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) {
2355
				$update_status = __( 'Requires Update', 'tgmpa' );
2356
2357
			} elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
2358
				$update_status = __( 'Update recommended', 'tgmpa' );
2359
			}
2360
2361
			if ( '' === $update_status ) {
2362
				return $install_status;
2363
			}
2364
2365
			return sprintf(
2366
				_x( '%1$s, %2$s', '%1$s = install status, %2$s = update status', 'tgmpa' ),
2367
				$install_status,
2368
				$update_status
2369
			);
2370
		}
2371
2372
		/**
2373
		 * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type.
2374
		 *
2375
		 * @since 2.5.0
2376
		 *
2377
		 * @param array $items Prepared table items.
2378
		 * @return array Sorted table items.
2379
		 */
2380
		public function sort_table_items( $items ) {
2381
			$type = array();
2382
			$name = array();
2383
2384
			foreach ( $items as $i => $plugin ) {
2385
				$type[ $i ] = $plugin['type']; // Required / recommended.
2386
				$name[ $i ] = $plugin['sanitized_plugin'];
2387
			}
2388
2389
			array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items );
2390
2391
			return $items;
2392
		}
2393
2394
		/**
2395
		 * Get an associative array ( id => link ) of the views available on this table.
2396
		 *
2397
		 * @since 2.5.0
2398
		 *
2399
		 * @return array
2400
		 */
2401
		public function get_views() {
2402
			$status_links = array();
2403
2404
			foreach ( $this->view_totals as $type => $count ) {
2405
				if ( $count < 1 ) {
2406
					continue;
2407
				}
2408
2409
				switch ( $type ) {
2410
					case 'all':
2411
						$text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' );
2412
						break;
2413
					case 'install':
2414
						$text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' );
2415
						break;
2416
					case 'update':
2417
						$text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' );
2418
						break;
2419
					case 'activate':
2420
						$text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' );
2421
						break;
2422
					default:
2423
						$text = '';
2424
						break;
2425
				}
2426
2427
				if ( ! empty( $text ) ) {
2428
2429
					$status_links[ $type ] = sprintf(
2430
						'<a href="%s"%s>%s</a>',
2431
						esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
2432
						( $type === $this->view_context ) ? ' class="current"' : '',
2433
						sprintf( $text, number_format_i18n( $count ) )
2434
					);
2435
				}
2436
			}
2437
2438
			return $status_links;
2439
		}
2440
2441
		/**
2442
		 * Create default columns to display important plugin information
2443
		 * like type, action and status.
2444
		 *
2445
		 * @since 2.2.0
2446
		 *
2447
		 * @param array  $item        Array of item data.
2448
		 * @param string $column_name The name of the column.
2449
		 * @return string
2450
		 */
2451
		public function column_default( $item, $column_name ) {
2452
			return $item[ $column_name ];
2453
		}
2454
2455
		/**
2456
		 * Required for bulk installing.
2457
		 *
2458
		 * Adds a checkbox for each plugin.
2459
		 *
2460
		 * @since 2.2.0
2461
		 *
2462
		 * @param array $item Array of item data.
2463
		 * @return string The input checkbox with all necessary info.
2464
		 */
2465
		public function column_cb( $item ) {
2466
			return sprintf(
2467
				'<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />',
2468
				esc_attr( $this->_args['singular'] ),
2469
				esc_attr( $item['slug'] ),
2470
				esc_attr( $item['sanitized_plugin'] )
2471
			);
2472
		}
2473
2474
		/**
2475
		 * Create default title column along with the action links.
2476
		 *
2477
		 * @since 2.2.0
2478
		 *
2479
		 * @param array $item Array of item data.
2480
		 * @return string The plugin name and action links.
2481
		 */
2482
		public function column_plugin( $item ) {
2483
			return sprintf(
2484
				'%1$s %2$s',
2485
				$item['plugin'],
2486
				$this->row_actions( $this->get_row_actions( $item ), true )
2487
			);
2488
		}
2489
2490
		/**
2491
		 * Create version information column.
2492
		 *
2493
		 * @since 2.5.0
2494
		 *
2495
		 * @param array $item Array of item data.
2496
		 * @return string HTML-formatted version information.
2497
		 */
2498
		public function column_version( $item ) {
2499
			$output = array();
2500
2501
			if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2502
				$installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'tgmpa' );
2503
2504
				$color = '';
2505
				if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) {
2506
					$color = ' color: #ff0000; font-weight: bold;';
2507
				}
2508
2509
				$output[] = sprintf(
2510
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'tgmpa' ) . '</p>',
2511
					$color,
2512
					$installed
2513
				);
2514
			}
2515
2516
			if ( ! empty( $item['minimum_version'] ) ) {
2517
				$output[] = sprintf(
2518
					'<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'tgmpa' ) . '</p>',
2519
					$item['minimum_version']
2520
				);
2521
			}
2522
2523
			if ( ! empty( $item['available_version'] ) ) {
2524
				$color = '';
2525
				if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) {
2526
					$color = ' color: #71C671; font-weight: bold;';
2527
				}
2528
2529
				$output[] = sprintf(
2530
					'<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'tgmpa' ) . '</p>',
2531
					$color,
2532
					$item['available_version']
2533
				);
2534
			}
2535
2536
			if ( empty( $output ) ) {
2537
				return '&nbsp;'; // Let's not break the table layout.
2538
			} else {
2539
				return implode( "\n", $output );
2540
			}
2541
		}
2542
2543
		/**
2544
		 * Sets default message within the plugins table if no plugins
2545
		 * are left for interaction.
2546
		 *
2547
		 * Hides the menu item to prevent the user from clicking and
2548
		 * getting a permissions error.
2549
		 *
2550
		 * @since 2.2.0
2551
		 */
2552
		public function no_items() {
2553
			printf( wp_kses_post( __( 'No plugins to install, update or activate. <a href="%1$s">Return to the Dashboard</a>', 'tgmpa' ) ), esc_url( self_admin_url() ) );
2554
			echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
2555
		}
2556
2557
		/**
2558
		 * Output all the column information within the table.
2559
		 *
2560
		 * @since 2.2.0
2561
		 *
2562
		 * @return array $columns The column names.
2563
		 */
2564
		public function get_columns() {
2565
			$columns = array(
2566
				'cb'     => '<input type="checkbox" />',
2567
				'plugin' => __( 'Plugin', 'tgmpa' ),
2568
				'source' => __( 'Source', 'tgmpa' ),
2569
				'type'   => __( 'Type', 'tgmpa' ),
2570
			);
2571
2572
			if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
2573
				$columns['version'] = __( 'Version', 'tgmpa' );
2574
				$columns['status']  = __( 'Status', 'tgmpa' );
2575
			}
2576
2577
			return apply_filters( 'tgmpa_table_columns', $columns );
2578
		}
2579
2580
		/**
2581
		 * Get name of default primary column
2582
		 *
2583
		 * @since 2.5.0 / WP 4.3+ compatibility
2584
		 * @access protected
2585
		 *
2586
		 * @return string
2587
		 */
2588
		protected function get_default_primary_column_name() {
2589
			return 'plugin';
2590
		}
2591
2592
		/**
2593
		 * Get the name of the primary column.
2594
		 *
2595
		 * @since 2.5.0 / WP 4.3+ compatibility
2596
		 * @access protected
2597
		 *
2598
		 * @return string The name of the primary column.
2599
		 */
2600
		protected function get_primary_column_name() {
2601
			if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) {
2602
				return parent::get_primary_column_name();
2603
			} else {
2604
				return $this->get_default_primary_column_name();
2605
			}
2606
		}
2607
2608
		/**
2609
		 * Get the actions which are relevant for a specific plugin row.
2610
		 *
2611
		 * @since 2.5.0
2612
		 *
2613
		 * @param array $item Array of item data.
2614
		 * @return array Array with relevant action links.
2615
		 */
2616
		protected function get_row_actions( $item ) {
2617
			$actions      = array();
2618
			$action_links = array();
2619
2620
			// Display the 'Install' action link if the plugin is not yet available.
2621
			if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
2622
				$actions['install'] = _x( 'Install %2$s', '%2$s = plugin name in screen reader markup', 'tgmpa' );
2623
			} else {
2624
				// Display the 'Update' action link if an update is available and WP complies with plugin minimum.
2625
				if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) {
2626
					$actions['update'] = _x( 'Update %2$s', '%2$s = plugin name in screen reader markup', 'tgmpa' );
2627
				}
2628
2629
				// Display the 'Activate' action link, but only if the plugin meets the minimum version.
2630
				if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) {
2631
					$actions['activate'] = _x( 'Activate %2$s', '%2$s = plugin name in screen reader markup', 'tgmpa' );
2632
				}
2633
			}
2634
2635
			// Create the actual links.
2636
			foreach ( $actions as $action => $text ) {
2637
				$nonce_url = wp_nonce_url(
2638
					add_query_arg(
2639
						array(
2640
							'plugin'           => urlencode( $item['slug'] ),
2641
							'tgmpa-' . $action => $action . '-plugin',
2642
						),
2643
						$this->tgmpa->get_tgmpa_url()
2644
					),
2645
					'tgmpa-' . $action,
2646
					'tgmpa-nonce'
2647
				);
2648
2649
				$action_links[ $action ] = sprintf(
2650
					'<a href="%1$s">' . esc_html( $text ) . '</a>',
2651
					esc_url( $nonce_url ),
2652
					'<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>'
2653
				);
2654
			}
2655
2656
			$prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : '';
2657
			return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context );
2658
		}
2659
2660
		/**
2661
		 * Generates content for a single row of the table.
2662
		 *
2663
		 * @since 2.5.0
2664
		 *
2665
		 * @param object $item The current item.
2666
		 */
2667
		public function single_row( $item ) {
2668
			parent::single_row( $item );
2669
2670
			/**
2671
			 * Fires after each specific row in the TGMPA Plugins list table.
2672
			 *
2673
			 * The dynamic portion of the hook name, `$item['slug']`, refers to the slug
2674
			 * for the plugin.
2675
			 *
2676
			 * @since 2.5.0
2677
			 */
2678
			do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context );
2679
		}
2680
2681
		/**
2682
		 * Show the upgrade notice below a plugin row if there is one.
2683
		 *
2684
		 * @since 2.5.0
2685
		 *
2686
		 * @see /wp-admin/includes/update.php
2687
		 *
2688
		 * @param string $slug Plugin slug.
2689
		 * @param array  $item The information available in this table row.
2690
		 * @return null Return early if upgrade notice is empty.
2691
		 */
2692
		public function wp_plugin_update_row( $slug, $item ) {
2693
			if ( empty( $item['upgrade_notice'] ) ) {
2694
				return;
2695
			}
2696
2697
			echo '
2698
				<tr class="plugin-update-tr">
2699
					<td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange">
2700
						<div class="update-message">',
2701
							esc_html__( 'Upgrade message from the plugin author:', 'tgmpa' ),
2702
							' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong>
2703
						</div>
2704
					</td>
2705
				</tr>';
2706
		}
2707
2708
		/**
2709
		 * Extra controls to be displayed between bulk actions and pagination.
2710
		 *
2711
		 * @since 2.5.0
2712
		 *
2713
		 * @param string $which 'top' or 'bottom' table navigation.
2714
		 */
2715
		public function extra_tablenav( $which ) {
2716
			if ( 'bottom' === $which ) {
2717
				$this->tgmpa->show_tgmpa_version();
2718
			}
2719
		}
2720
2721
		/**
2722
		 * Defines the bulk actions for handling registered plugins.
2723
		 *
2724
		 * @since 2.2.0
2725
		 *
2726
		 * @return array $actions The bulk actions for the plugin install table.
2727
		 */
2728
		public function get_bulk_actions() {
2729
2730
			$actions = array();
2731
2732
			if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) {
2733
				if ( current_user_can( 'install_plugins' ) ) {
2734
					$actions['tgmpa-bulk-install'] = __( 'Install', 'tgmpa' );
2735
				}
2736
			}
2737
2738
			if ( 'install' !== $this->view_context ) {
2739
				if ( current_user_can( 'update_plugins' ) ) {
2740
					$actions['tgmpa-bulk-update'] = __( 'Update', 'tgmpa' );
2741
				}
2742
				if ( current_user_can( 'activate_plugins' ) ) {
2743
					$actions['tgmpa-bulk-activate'] = __( 'Activate', 'tgmpa' );
2744
				}
2745
			}
2746
2747
			return $actions;
2748
		}
2749
2750
		/**
2751
		 * Processes bulk installation and activation actions.
2752
		 *
2753
		 * The bulk installation process looks for the $_POST information and passes that
2754
		 * through if a user has to use WP_Filesystem to enter their credentials.
2755
		 *
2756
		 * @since 2.2.0
2757
		 */
2758
		public function process_bulk_actions() {
2759
			// Bulk installation process.
2760
			if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) {
2761
2762
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
2763
2764
				$install_type = 'install';
2765
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2766
					$install_type = 'update';
2767
				}
2768
2769
				$plugins_to_install = array();
2770
2771
				// Did user actually select any plugins to install/update ?
2772 View Code Duplication
				if ( empty( $_POST['plugin'] ) ) {
2773
					if ( 'install' === $install_type ) {
2774
						$message = __( 'No plugins were selected to be installed. No action taken.', 'tgmpa' );
2775
					} else {
2776
						$message = __( 'No plugins were selected to be updated. No action taken.', 'tgmpa' );
2777
					}
2778
2779
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2780
2781
					return false;
2782
				}
2783
2784
				if ( is_array( $_POST['plugin'] ) ) {
2785
					$plugins_to_install = (array) $_POST['plugin'];
2786
				} elseif ( is_string( $_POST['plugin'] ) ) {
2787
					// Received via Filesystem page - un-flatten array (WP bug #19643).
2788
					$plugins_to_install = explode( ',', $_POST['plugin'] );
2789
				}
2790
2791
				// Sanitize the received input.
2792
				$plugins_to_install = array_map( 'urldecode', $plugins_to_install );
2793
				$plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install );
2794
2795
				// Validate the received input.
2796
				foreach ( $plugins_to_install as $key => $slug ) {
2797
					// Check if the plugin was registered with TGMPA and remove if not.
2798
					if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) {
2799
						unset( $plugins_to_install[ $key ] );
2800
						continue;
2801
					}
2802
2803
					// For install: make sure this is a plugin we *can* install and not one already installed.
2804
					if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) {
2805
						unset( $plugins_to_install[ $key ] );
2806
					}
2807
2808
					// For updates: make sure this is a plugin we *can* update (update available and WP version ok).
2809
					if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) {
2810
						unset( $plugins_to_install[ $key ] );
2811
					}
2812
				}
2813
2814
				// No need to proceed further if we have no plugins to handle.
2815 View Code Duplication
				if ( empty( $plugins_to_install ) ) {
2816
					if ( 'install' === $install_type ) {
2817
						$message = __( 'No plugins are available to be installed at this time.', 'tgmpa' );
2818
					} else {
2819
						$message = __( 'No plugins are available to be updated at this time.', 'tgmpa' );
2820
					}
2821
2822
					echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
2823
2824
					return false;
2825
				}
2826
2827
				// Pass all necessary information if WP_Filesystem is needed.
2828
				$url = wp_nonce_url(
2829
					$this->tgmpa->get_tgmpa_url(),
2830
					'bulk-' . $this->_args['plural']
2831
				);
2832
2833
				// Give validated data back to $_POST which is the only place the filesystem looks for extra fields.
2834
				$_POST['plugin'] = implode( ',', $plugins_to_install ); // Work around for WP bug #19643.
2835
2836
				$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
2837
				$fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
2838
2839 View Code Duplication
				if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) {
2840
					return true; // Stop the normal page form from displaying, credential request form will be shown.
2841
				}
2842
2843
				// Now we have some credentials, setup WP_Filesystem.
2844 View Code Duplication
				if ( ! WP_Filesystem( $creds ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2845
					// Our credentials were no good, ask the user for them again.
2846
					request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
2847
2848
					return true;
2849
				}
2850
2851
				/* If we arrive here, we have the filesystem */
2852
2853
				// Store all information in arrays since we are processing a bulk installation.
2854
				$names      = array();
2855
				$sources    = array(); // Needed for installs.
2856
				$file_paths = array(); // Needed for upgrades.
2857
				$to_inject  = array(); // Information to inject into the update_plugins transient.
2858
2859
				// Prepare the data for validated plugins for the install/upgrade.
2860
				foreach ( $plugins_to_install as $slug ) {
2861
					$name   = $this->tgmpa->plugins[ $slug ]['name'];
2862
					$source = $this->tgmpa->get_download_url( $slug );
2863
2864
					if ( ! empty( $name ) && ! empty( $source ) ) {
2865
						$names[] = $name;
2866
2867
						switch ( $install_type ) {
2868
2869
							case 'install':
2870
								$sources[] = $source;
2871
								break;
2872
2873
							case 'update':
2874
								$file_paths[]                 = $this->tgmpa->plugins[ $slug ]['file_path'];
2875
								$to_inject[ $slug ]           = $this->tgmpa->plugins[ $slug ];
2876
								$to_inject[ $slug ]['source'] = $source;
2877
								break;
2878
						}
2879
					}
2880
				}
2881
				unset( $slug, $name, $source );
2882
2883
				// Create a new instance of TGMPA_Bulk_Installer.
2884
				$installer = new TGMPA_Bulk_Installer(
2885
					new TGMPA_Bulk_Installer_Skin(
2886
						array(
2887
							'url'          => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),
2888
							'nonce'        => 'bulk-' . $this->_args['plural'],
2889
							'names'        => $names,
2890
							'install_type' => $install_type,
2891
						)
2892
					)
2893
				);
2894
2895
				// Wrap the install process with the appropriate HTML.
2896
				echo '<div class="tgmpa">',
2897
					'<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>
2898
					<div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">';
2899
2900
				// Process the bulk installation submissions.
2901
				add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
2902
2903
				if ( 'tgmpa-bulk-update' === $this->current_action() ) {
2904
					// Inject our info into the update transient.
2905
					$this->tgmpa->inject_update_info( $to_inject );
2906
2907
					$installer->bulk_upgrade( $file_paths );
2908
				} else {
2909
					$installer->bulk_install( $sources );
2910
				}
2911
2912
				remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
2913
2914
				echo '</div></div>';
2915
2916
				return true;
2917
			}
2918
2919
			// Bulk activation process.
2920
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
2921
				check_admin_referer( 'bulk-' . $this->_args['plural'] );
2922
2923
				// Did user actually select any plugins to activate ?
2924
				if ( empty( $_POST['plugin'] ) ) {
2925
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>';
2926
2927
					return false;
2928
				}
2929
2930
				// Grab plugin data from $_POST.
2931
				$plugins = array();
2932
				if ( isset( $_POST['plugin'] ) ) {
2933
					$plugins = array_map( 'urldecode', (array) $_POST['plugin'] );
2934
					$plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins );
2935
				}
2936
2937
				$plugins_to_activate = array();
2938
				$plugin_names        = array();
2939
2940
				// Grab the file paths for the selected & inactive plugins from the registration array.
2941
				foreach ( $plugins as $slug ) {
2942
					if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
2943
						$plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path'];
2944
						$plugin_names[]        = $this->tgmpa->plugins[ $slug ]['name'];
2945
					}
2946
				}
2947
				unset( $slug );
2948
2949
				// Return early if there are no plugins to activate.
2950
				if ( empty( $plugins_to_activate ) ) {
2951
					echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>';
2952
2953
					return false;
2954
				}
2955
2956
				// Now we are good to go - let's start activating plugins.
2957
				$activate = activate_plugins( $plugins_to_activate );
2958
2959
				if ( is_wp_error( $activate ) ) {
2960
					echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
2961
				} else {
2962
					$count        = count( $plugin_names ); // Count so we can use _n function.
2963
					$plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names );
2964
					$last_plugin  = array_pop( $plugin_names ); // Pop off last name to prep for readability.
2965
					$imploded     = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
2966
2967
					printf( // WPCS: xss ok.
2968
						'<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
2969
						esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),
2970
						$imploded
2971
					);
2972
2973
					// Update recently activated plugins option.
2974
					$recent = (array) get_option( 'recently_activated' );
2975
					foreach ( $plugins_to_activate as $plugin => $time ) {
2976
						if ( isset( $recent[ $plugin ] ) ) {
2977
							unset( $recent[ $plugin ] );
2978
						}
2979
					}
2980
					update_option( 'recently_activated', $recent );
2981
				}
2982
2983
				unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another.
2984
2985
				return true;
2986
			}
2987
2988
			return false;
2989
		}
2990
2991
		/**
2992
		 * Prepares all of our information to be outputted into a usable table.
2993
		 *
2994
		 * @since 2.2.0
2995
		 */
2996
		public function prepare_items() {
2997
			$columns               = $this->get_columns(); // Get all necessary column information.
2998
			$hidden                = array(); // No columns to hide, but we must set as an array.
2999
			$sortable              = array(); // No reason to make sortable columns.
3000
			$primary               = $this->get_primary_column_name(); // Column which has the row actions.
3001
			$this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers.
3002
3003
			// Process our bulk activations here.
3004
			if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
3005
				$this->process_bulk_actions();
3006
			}
3007
3008
			// Store all of our plugin data into $items array so WP_List_Table can use it.
3009
			$this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() );
3010
		}
3011
3012
		/* *********** DEPRECATED METHODS *********** */
3013
3014
		/**
3015
		 * Retrieve plugin data, given the plugin name.
3016
		 *
3017
		 * @since      2.2.0
3018
		 * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead.
3019
		 * @see        TGM_Plugin_Activation::_get_plugin_data_from_name()
3020
		 *
3021
		 * @param string $name Name of the plugin, as it was registered.
3022
		 * @param string $data Optional. Array key of plugin data to return. Default is slug.
3023
		 * @return string|boolean Plugin slug if found, false otherwise.
3024
		 */
3025
		protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
3026
			_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' );
3027
3028
			return $this->tgmpa->_get_plugin_data_from_name( $name, $data );
3029
		}
3030
	}
3031
}
3032
3033
3034
if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
3035
3036
	/**
3037
	 * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+.
3038
	 */
3039
	class TGM_Bulk_Installer {
3040
	}
3041
}
3042
if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
3043
3044
	/**
3045
	 * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+.
3046
	 */
3047
	class TGM_Bulk_Installer_Skin {
3048
	}
3049
}
3050
3051
/**
3052
 * The WP_Upgrader file isn't always available. If it isn't available,
3053
 * we load it here.
3054
 *
3055
 * We check to make sure no action or activation keys are set so that WordPress
3056
 * does not try to re-include the class when processing upgrades or installs outside
3057
 * of the class.
3058
 *
3059
 * @since 2.2.0
3060
 */
3061
add_action( 'admin_init', 'tgmpa_load_bulk_installer' );
3062
if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
3063
	/**
3064
	 * Load bulk installer
3065
	 */
3066
	function tgmpa_load_bulk_installer() {
3067
		// Silently fail if 2.5+ is loaded *after* an older version.
3068
		if ( ! isset( $GLOBALS['tgmpa'] ) ) {
3069
			return;
3070
		}
3071
3072
		// Get TGMPA class instance.
3073
		$tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3074
3075
		if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) {
3076
			if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
3077
				require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
3078
			}
3079
3080
			if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) {
3081
3082
				/**
3083
				 * Installer class to handle bulk plugin installations.
3084
				 *
3085
				 * Extends WP_Upgrader and customizes to suit the installation of multiple
3086
				 * plugins.
3087
				 *
3088
				 * @since 2.2.0
3089
				 *
3090
				 * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}}
3091
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
3092
				 *            This was done to prevent backward compatibility issues with v2.3.6.}}
3093
				 *
3094
				 * @package TGM-Plugin-Activation
3095
				 * @author  Thomas Griffin
3096
				 * @author  Gary Jones
3097
				 */
3098
				class TGMPA_Bulk_Installer extends Plugin_Upgrader {
3099
					/**
3100
					 * Holds result of bulk plugin installation.
3101
					 *
3102
					 * @since 2.2.0
3103
					 *
3104
					 * @var string
3105
					 */
3106
					public $result;
3107
3108
					/**
3109
					 * Flag to check if bulk installation is occurring or not.
3110
					 *
3111
					 * @since 2.2.0
3112
					 *
3113
					 * @var boolean
3114
					 */
3115
					public $bulk = false;
3116
3117
					/**
3118
					 * TGMPA instance
3119
					 *
3120
					 * @since 2.5.0
3121
					 *
3122
					 * @var object
3123
					 */
3124
					protected $tgmpa;
3125
3126
					/**
3127
					 * Whether or not the destination directory needs to be cleared ( = on update).
3128
					 *
3129
					 * @since 2.5.0
3130
					 *
3131
					 * @var bool
3132
					 */
3133
					protected $clear_destination = false;
3134
3135
					/**
3136
					 * References parent constructor and sets defaults for class.
3137
					 *
3138
					 * @since 2.2.0
3139
					 *
3140
					 * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
3141
					 */
3142
					public function __construct( $skin = null ) {
3143
						// Get TGMPA class instance.
3144
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3145
3146
						parent::__construct( $skin );
3147
3148
						if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
3149
							$this->clear_destination = true;
3150
						}
3151
3152
						if ( $this->tgmpa->is_automatic ) {
3153
							$this->activate_strings();
3154
						}
3155
3156
						add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
3157
					}
3158
3159
					/**
3160
					 * Sets the correct activation strings for the installer skin to use.
3161
					 *
3162
					 * @since 2.2.0
3163
					 */
3164
					public function activate_strings() {
3165
						$this->strings['activation_failed']  = __( 'Plugin activation failed.', 'tgmpa' );
3166
						$this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' );
3167
					}
3168
3169
					/**
3170
					 * Performs the actual installation of each plugin.
3171
					 *
3172
					 * @since 2.2.0
3173
					 *
3174
					 * @see WP_Upgrader::run()
3175
					 *
3176
					 * @param array $options The installation config options.
3177
					 * @return null|array Return early if error, array of installation data on success.
3178
					 */
3179
					public function run( $options ) {
3180
						$result = parent::run( $options );
3181
3182
						// Reset the strings in case we changed one during automatic activation.
3183
						if ( $this->tgmpa->is_automatic ) {
3184
							if ( 'update' === $this->skin->options['install_type'] ) {
3185
								$this->upgrade_strings();
3186
							} else {
3187
								$this->install_strings();
3188
							}
3189
						}
3190
3191
						return $result;
3192
					}
3193
3194
					/**
3195
					 * Processes the bulk installation of plugins.
3196
					 *
3197
					 * @since 2.2.0
3198
					 *
3199
					 * {@internal This is basically a near identical copy of the WP Core
3200
					 * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with
3201
					 * new installs instead of upgrades.
3202
					 * For ease of future synchronizations, the adjustments are clearly commented, but no other
3203
					 * comments are added. Code style has been made to comply.}}
3204
					 *
3205
					 * @see Plugin_Upgrader::bulk_upgrade()
3206
					 * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
3207
					 * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}}
3208
					 *
3209
					 * @param array $plugins The plugin sources needed for installation.
3210
					 * @param array $args    Arbitrary passed extra arguments.
3211
					 * @return string|bool Install confirmation messages on success, false on failure.
3212
					 */
3213
					public function bulk_install( $plugins, $args = array() ) {
3214
						// [TGMPA + ] Hook auto-activation in.
3215
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3216
3217
						$defaults    = array(
3218
							'clear_update_cache' => true,
3219
						);
3220
						$parsed_args = wp_parse_args( $args, $defaults );
3221
3222
						$this->init();
3223
						$this->bulk = true;
3224
3225
						$this->install_strings(); // [TGMPA + ] adjusted.
3226
3227
						/* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
3228
3229
						/* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
3230
3231
						$this->skin->header();
3232
3233
						// Connect to the Filesystem first.
3234
						$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
3235
						if ( ! $res ) {
3236
							$this->skin->footer();
3237
							return false;
3238
						}
3239
3240
						$this->skin->bulk_header();
3241
3242
						/*
3243
						 * Only start maintenance mode if:
3244
						 * - running Multisite and there are one or more plugins specified, OR
3245
						 * - a plugin with an update available is currently active.
3246
						 * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
3247
						 */
3248
						$maintenance = ( is_multisite() && ! empty( $plugins ) );
3249
3250
						/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
3251
						[TGMPA - ]
3252
						foreach ( $plugins as $plugin )
3253
							$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
3254
						*/
3255
						if ( $maintenance ) {
3256
							$this->maintenance_mode( true );
3257
						}
3258
3259
						$results = array();
3260
3261
						$this->update_count   = count( $plugins );
3262
						$this->update_current = 0;
3263
						foreach ( $plugins as $plugin ) {
3264
							$this->update_current++;
3265
3266
							/*
1 ignored issue
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
3267
							[TGMPA - ]
3268
							$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
3269
3270
							if ( !isset( $current->response[ $plugin ] ) ) {
3271
								$this->skin->set_result('up_to_date');
3272
								$this->skin->before();
3273
								$this->skin->feedback('up_to_date');
3274
								$this->skin->after();
3275
								$results[$plugin] = true;
3276
								continue;
3277
							}
3278
3279
							// Get the URL to the zip file.
3280
							$r = $current->response[ $plugin ];
3281
3282
							$this->skin->plugin_active = is_plugin_active($plugin);
3283
							*/
3284
3285
							$result = $this->run(
3286
								array(
3287
									'package'           => $plugin, // [TGMPA + ] adjusted.
3288
									'destination'       => WP_PLUGIN_DIR,
3289
									'clear_destination' => false, // [TGMPA + ] adjusted.
3290
									'clear_working'     => true,
3291
									'is_multi'          => true,
3292
									'hook_extra'        => array(
3293
										'plugin' => $plugin,
3294
									),
3295
								)
3296
							);
3297
3298
							$results[ $plugin ] = $this->result;
3299
3300
							// Prevent credentials auth screen from displaying multiple times.
3301
							if ( false === $result ) {
3302
								break;
3303
							}
3304
						} //end foreach $plugins
3305
3306
						$this->maintenance_mode( false );
3307
3308
						/**
3309
						 * Fires when the bulk upgrader process is complete.
3310
						 *
3311
						 * @since WP 3.6.0 / TGMPA 2.5.0
3312
						 *
3313
						 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
3314
						 *                              be a Theme_Upgrader or Core_Upgrade instance.
3315
						 * @param array           $data {
3316
						 *     Array of bulk item update data.
3317
						 *
3318
						 *     @type string $action   Type of action. Default 'update'.
3319
						 *     @type string $type     Type of update process. Accepts 'plugin', 'theme', or 'core'.
3320
						 *     @type bool   $bulk     Whether the update process is a bulk update. Default true.
3321
						 *     @type array  $packages Array of plugin, theme, or core packages to update.
3322
						 * }
3323
						 */
3324
						do_action( 'upgrader_process_complete', $this, array(
3325
							'action'  => 'install', // [TGMPA + ] adjusted.
3326
							'type'    => 'plugin',
3327
							'bulk'    => true,
3328
							'plugins' => $plugins,
3329
						) );
3330
3331
						$this->skin->bulk_footer();
3332
3333
						$this->skin->footer();
3334
3335
						// Cleanup our hooks, in case something else does a upgrade on this connection.
3336
						/* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
3337
3338
						// [TGMPA + ] Remove our auto-activation hook.
3339
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3340
3341
						// Force refresh of plugin update information.
3342
						wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
3343
3344
						return $results;
3345
					}
3346
3347
					/**
3348
					 * Handle a bulk upgrade request.
3349
					 *
3350
					 * @since 2.5.0
3351
					 *
3352
					 * @see Plugin_Upgrader::bulk_upgrade()
3353
					 *
3354
					 * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
3355
					 * @param array $args    Arbitrary passed extra arguments.
3356
					 * @return string|bool Install confirmation messages on success, false on failure.
3357
					 */
3358
					public function bulk_upgrade( $plugins, $args = array() ) {
3359
3360
						add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3361
3362
						$result = parent::bulk_upgrade( $plugins, $args );
3363
3364
						remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
3365
3366
						return $result;
3367
					}
3368
3369
					/**
3370
					 * Abuse a filter to auto-activate plugins after installation.
3371
					 *
3372
					 * Hooked into the 'upgrader_post_install' filter hook.
3373
					 *
3374
					 * @since 2.5.0
3375
					 *
3376
					 * @param bool $bool The value we need to give back (true).
3377
					 * @return bool
3378
					 */
3379
					public function auto_activate( $bool ) {
3380
						// Only process the activation of installed plugins if the automatic flag is set to true.
3381
						if ( $this->tgmpa->is_automatic ) {
3382
							// Flush plugins cache so the headers of the newly installed plugins will be read correctly.
3383
							wp_clean_plugins_cache();
3384
3385
							// Get the installed plugin file.
3386
							$plugin_info = $this->plugin_info();
3387
3388
							// Don't try to activate on upgrade of active plugin as WP will do this already.
3389
							if ( ! is_plugin_active( $plugin_info ) ) {
3390
								$activate = activate_plugin( $plugin_info );
3391
3392
								// Adjust the success string based on the activation result.
3393
								$this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
3394
3395
								if ( is_wp_error( $activate ) ) {
3396
									$this->skin->error( $activate );
3397
									$this->strings['process_success'] .= $this->strings['activation_failed'];
3398
								} else {
3399
									$this->strings['process_success'] .= $this->strings['activation_success'];
3400
								}
3401
							}
3402
						}
3403
3404
						return $bool;
3405
					}
3406
				}
3407
			}
3408
3409
			if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) {
3410
3411
				/**
3412
				 * Installer skin to set strings for the bulk plugin installations..
3413
				 *
3414
				 * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
3415
				 * plugins.
3416
				 *
3417
				 * @since 2.2.0
3418
				 *
3419
				 * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to
3420
				 *           TGMPA_Bulk_Installer_Skin.
3421
				 *           This was done to prevent backward compatibility issues with v2.3.6.}}
3422
				 *
3423
				 * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php
3424
				 *
3425
				 * @package TGM-Plugin-Activation
3426
				 * @author  Thomas Griffin
3427
				 * @author  Gary Jones
3428
				 */
3429
				class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
3430
					/**
3431
					 * Holds plugin info for each individual plugin installation.
3432
					 *
3433
					 * @since 2.2.0
3434
					 *
3435
					 * @var array
3436
					 */
3437
					public $plugin_info = array();
3438
3439
					/**
3440
					 * Holds names of plugins that are undergoing bulk installations.
3441
					 *
3442
					 * @since 2.2.0
3443
					 *
3444
					 * @var array
3445
					 */
3446
					public $plugin_names = array();
3447
3448
					/**
3449
					 * Integer to use for iteration through each plugin installation.
3450
					 *
3451
					 * @since 2.2.0
3452
					 *
3453
					 * @var integer
3454
					 */
3455
					public $i = 0;
3456
3457
					/**
3458
					 * TGMPA instance
3459
					 *
3460
					 * @since 2.5.0
3461
					 *
3462
					 * @var object
3463
					 */
3464
					protected $tgmpa;
3465
3466
					/**
3467
					 * Constructor. Parses default args with new ones and extracts them for use.
3468
					 *
3469
					 * @since 2.2.0
3470
					 *
3471
					 * @param array $args Arguments to pass for use within the class.
3472
					 */
3473
					public function __construct( $args = array() ) {
3474
						// Get TGMPA class instance.
3475
						$this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
3476
3477
						// Parse default and new args.
3478
						$defaults = array(
3479
							'url'          => '',
3480
							'nonce'        => '',
3481
							'names'        => array(),
3482
							'install_type' => 'install',
3483
						);
3484
						$args     = wp_parse_args( $args, $defaults );
3485
3486
						// Set plugin names to $this->plugin_names property.
3487
						$this->plugin_names = $args['names'];
3488
3489
						// Extract the new args.
3490
						parent::__construct( $args );
3491
					}
3492
3493
					/**
3494
					 * Sets install skin strings for each individual plugin.
3495
					 *
3496
					 * Checks to see if the automatic activation flag is set and uses the
3497
					 * the proper strings accordingly.
3498
					 *
3499
					 * @since 2.2.0
3500
					 */
3501
					public function add_strings() {
3502
						if ( 'update' === $this->options['install_type'] ) {
3503
							parent::add_strings();
3504
							$this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3505
						} else {
3506
							$this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
3507
							$this->upgrader->strings['skin_update_failed']       = __( 'The installation of %1$s failed.', 'tgmpa' );
3508
3509
							if ( $this->tgmpa->is_automatic ) {
3510
								// Automatic activation strings.
3511
								$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' );
3512
								$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>';
3513
								$this->upgrader->strings['skin_upgrade_end']          = __( 'All installations and activations have been completed.', 'tgmpa' );
3514
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3515
							} else {
3516
								// Default installation strings.
3517
								$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' );
3518
								$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>';
3519
								$this->upgrader->strings['skin_upgrade_end']          = __( 'All installations have been completed.', 'tgmpa' );
3520
								$this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
3521
							}
3522
						}
3523
					}
3524
3525
					/**
3526
					 * Outputs the header strings and necessary JS before each plugin installation.
3527
					 *
3528
					 * @since 2.2.0
3529
					 *
3530
					 * @param string $title Unused in this implementation.
3531
					 */
3532
					public function before( $title = '' ) {
3533
						if ( empty( $title ) ) {
3534
							$title = esc_html( $this->plugin_names[ $this->i ] );
3535
						}
3536
						parent::before( $title );
3537
					}
3538
3539
					/**
3540
					 * Outputs the footer strings and necessary JS after each plugin installation.
3541
					 *
3542
					 * Checks for any errors and outputs them if they exist, else output
3543
					 * success strings.
3544
					 *
3545
					 * @since 2.2.0
3546
					 *
3547
					 * @param string $title Unused in this implementation.
3548
					 */
3549
					public function after( $title = '' ) {
3550
						if ( empty( $title ) ) {
3551
							$title = esc_html( $this->plugin_names[ $this->i ] );
3552
						}
3553
						parent::after( $title );
3554
3555
						$this->i++;
3556
					}
3557
3558
					/**
3559
					 * Outputs links after bulk plugin installation is complete.
3560
					 *
3561
					 * @since 2.2.0
3562
					 */
3563
					public function bulk_footer() {
3564
						// Serve up the string to say installations (and possibly activations) are complete.
3565
						parent::bulk_footer();
3566
3567
						// Flush plugins cache so we can make sure that the installed plugins list is always up to date.
3568
						wp_clean_plugins_cache();
3569
3570
						$this->tgmpa->show_tgmpa_version();
3571
3572
						// Display message based on if all plugins are now active or not.
3573
						$update_actions = array();
3574
3575
						if ( $this->tgmpa->is_tgmpa_complete() ) {
3576
							// All plugins are active, so we display the complete string and hide the menu to protect users.
3577
							echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
3578
							$update_actions['dashboard'] = sprintf(
3579
								esc_html( $this->tgmpa->strings['complete'] ),
3580
								'<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>'
3581
							);
3582
						} else {
3583
							$update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>';
3584
						}
3585
3586
						/**
3587
						 * Filter the list of action links available following bulk plugin installs/updates.
3588
						 *
3589
						 * @since 2.5.0
3590
						 *
3591
						 * @param array $update_actions Array of plugin action links.
3592
						 * @param array $plugin_info    Array of information for the last-handled plugin.
3593
						 */
3594
						$update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
3595
3596
						if ( ! empty( $update_actions ) ) {
3597
							$this->feedback( implode( ' | ', (array) $update_actions ) );
3598
						}
3599
					}
3600
3601
					/* *********** DEPRECATED METHODS *********** */
3602
3603
					/**
3604
					 * Flush header output buffer.
3605
					 *
3606
					 * @since      2.2.0
3607
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3608
					 * @see        Bulk_Upgrader_Skin::flush_output()
3609
					 */
3610
					public function before_flush_output() {
3611
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3612
						$this->flush_output();
3613
					}
3614
3615
					/**
3616
					 * Flush footer output buffer and iterate $this->i to make sure the
3617
					 * installation strings reference the correct plugin.
3618
					 *
3619
					 * @since      2.2.0
3620
					 * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
3621
					 * @see        Bulk_Upgrader_Skin::flush_output()
3622
					 */
3623
					public function after_flush_output() {
3624
						_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
3625
						$this->flush_output();
3626
						$this->i++;
3627
					}
3628
				}
3629
			}
3630
		}
3631
	}
3632
}
3633
3634
if ( ! class_exists( 'TGMPA_Utils' ) ) {
3635
3636
	/**
3637
	 * Generic utilities for TGMPA.
3638
	 *
3639
	 * All methods are static, poor-dev name-spacing class wrapper.
3640
	 *
3641
	 * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy.
3642
	 *
3643
	 * @since 2.5.0
3644
	 *
3645
	 * @package TGM-Plugin-Activation
3646
	 * @author  Juliette Reinders Folmer
3647
	 */
3648
	class TGMPA_Utils {
3649
		/**
3650
		 * Whether the PHP filter extension is enabled.
3651
		 *
3652
		 * @see http://php.net/book.filter
3653
		 *
3654
		 * @since 2.5.0
3655
		 *
3656
		 * @static
3657
		 *
3658
		 * @var bool $has_filters True is the extension is enabled.
3659
		 */
3660
		public static $has_filters;
3661
3662
		/**
3663
		 * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map().
3664
		 *
3665
		 * @since 2.5.0
3666
		 *
3667
		 * @static
3668
		 *
3669
		 * @param string $string Text to be wrapped.
3670
		 * @return string
3671
		 */
3672
		public static function wrap_in_em( $string ) {
3673
			return '<em>' . wp_kses_post( $string ) . '</em>';
3674
		}
3675
3676
		/**
3677
		 * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map().
3678
		 *
3679
		 * @since 2.5.0
3680
		 *
3681
		 * @static
3682
		 *
3683
		 * @param string $string Text to be wrapped.
3684
		 * @return string
3685
		 */
3686
		public static function wrap_in_strong( $string ) {
3687
			return '<strong>' . wp_kses_post( $string ) . '</strong>';
3688
		}
3689
3690
		/**
3691
		 * Helper function: Validate a value as boolean
3692
		 *
3693
		 * @since 2.5.0
3694
		 *
3695
		 * @static
3696
		 *
3697
		 * @param mixed $value Arbitrary value.
3698
		 * @return bool
3699
		 */
3700
		public static function validate_bool( $value ) {
3701
			if ( ! isset( self::$has_filters ) ) {
3702
				self::$has_filters = extension_loaded( 'filter' );
3703
			}
3704
3705
			if ( self::$has_filters ) {
3706
				return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
3707
			} else {
3708
				return self::emulate_filter_bool( $value );
3709
			}
3710
		}
3711
3712
		/**
3713
		 * Helper function: Cast a value to bool
3714
		 *
3715
		 * @since 2.5.0
3716
		 *
3717
		 * @static
3718
		 *
3719
		 * @param mixed $value Value to cast.
3720
		 * @return bool
3721
		 */
3722
		protected static function emulate_filter_bool( $value ) {
3723
			// @codingStandardsIgnoreStart
3724
			static $true  = array(
3725
				'1',
3726
				'true', 'True', 'TRUE',
3727
				'y', 'Y',
3728
				'yes', 'Yes', 'YES',
3729
				'on', 'On', 'ON',
3730
			);
3731
			static $false = array(
3732
				'0',
3733
				'false', 'False', 'FALSE',
3734
				'n', 'N',
3735
				'no', 'No', 'NO',
3736
				'off', 'Off', 'OFF',
3737
			);
3738
			// @codingStandardsIgnoreEnd
3739
3740
			if ( is_bool( $value ) ) {
3741
				return $value;
3742
			} else if ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
3743
				return (bool) $value;
3744
			} else if ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
3745
				return (bool) $value;
3746
			} else if ( is_string( $value ) ) {
3747
				$value = trim( $value );
3748
				if ( in_array( $value, $true, true ) ) {
3749
					return true;
3750
				} else if ( in_array( $value, $false, true ) ) {
3751
					return false;
3752
				} else {
3753
					return false;
3754
				}
3755
			}
3756
3757
			return false;
3758
		}
3759
	} // End of class TGMPA_Utils
3760
} // End of class_exists wrapper
3761