This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
class-tgm-plugin-activation.php
3775 lines (3295 loc) · 120 KB
/
class-tgm-plugin-activation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin installation and activation for WordPress themes.
*
* Please note that this is a drop-in library for a theme or plugin.
* The authors of this library (Thomas, Gary and Juliette) are NOT responsible
* for the support of your plugin or theme. Please contact the plugin
* or theme author for support.
*
* @package TGM-Plugin-Activation
* @version 2.6.1 for parent theme Zerif Lite for publication on WordPress.org
* @link http://tgmpluginactivation.com/
* @author Thomas Griffin, Gary Jones, Juliette Reinders Folmer
* @copyright Copyright (c) 2011, Thomas Griffin
* @license GPL-2.0+
*/
/*
Copyright 2011 Thomas Griffin (thomasgriffinmedia.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! class_exists( 'TGM_Plugin_Activation' ) ) {
/**
* Automatic plugin installation and activation library.
*
* Creates a way to automatically install and activate plugins from within themes.
* The plugins can be either bundled, downloaded from the WordPress
* Plugin Repository or downloaded from another external source.
*
* @since 1.0.0
*
* @package TGM-Plugin-Activation
* @author Thomas Griffin
* @author Gary Jones
*/
class TGM_Plugin_Activation {
/**
* TGMPA version number.
*
* @since 2.5.0
*
* @const string Version number.
*/
const TGMPA_VERSION = '2.6.1';
/**
* Regular expression to test if a URL is a WP plugin repo URL.
*
* @const string Regex.
*
* @since 2.5.0
*/
const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|';
/**
* Arbitrary regular expression to test if a string starts with a URL.
*
* @const string Regex.
*
* @since 2.5.0
*/
const IS_URL_REGEX = '|^http[s]?://|';
/**
* Holds a copy of itself, so it can be referenced by the class name.
*
* @since 1.0.0
*
* @var TGM_Plugin_Activation
*/
public static $instance;
/**
* Holds arrays of plugin details.
*
* @since 1.0.0
* @since 2.5.0 the array has the plugin slug as an associative key.
*
* @var array
*/
public $plugins = array();
/**
* Holds arrays of plugin names to use to sort the plugins array.
*
* @since 2.5.0
*
* @var array
*/
protected $sort_order = array();
/**
* Whether any plugins have the 'force_activation' setting set to true.
*
* @since 2.5.0
*
* @var bool
*/
protected $has_forced_activation = false;
/**
* Whether any plugins have the 'force_deactivation' setting set to true.
*
* @since 2.5.0
*
* @var bool
*/
protected $has_forced_deactivation = false;
/**
* Name of the unique ID to hash notices.
*
* @since 2.4.0
*
* @var string
*/
public $id = 'tgmpa';
/**
* Name of the query-string argument for the admin page.
*
* @since 1.0.0
*
* @var string
*/
protected $menu = 'tgmpa-install-plugins';
/**
* Parent menu file slug.
*
* @since 2.5.0
*
* @var string
*/
public $parent_slug = 'themes.php';
/**
* Capability needed to view the plugin installation menu item.
*
* @since 2.5.0
*
* @var string
*/
public $capability = 'edit_theme_options';
/**
* Default absolute path to folder containing bundled plugin zip files.
*
* @since 2.0.0
*
* @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string.
*/
public $default_path = '';
/**
* Flag to show admin notices or not.
*
* @since 2.1.0
*
* @var boolean
*/
public $has_notices = true;
/**
* Flag to determine if the user can dismiss the notice nag.
*
* @since 2.4.0
*
* @var boolean
*/
public $dismissable = true;
/**
* Message to be output above nag notice if dismissable is false.
*
* @since 2.4.0
*
* @var string
*/
public $dismiss_msg = '';
/**
* Flag to set automatic activation of plugins. Off by default.
*
* @since 2.2.0
*
* @var boolean
*/
public $is_automatic = false;
/**
* Optional message to display before the plugins table.
*
* @since 2.2.0
*
* @var string Message filtered by wp_kses_post(). Default is empty string.
*/
public $message = '';
/**
* Holds configurable array of strings.
*
* Default values are added in the constructor.
*
* @since 2.0.0
*
* @var array
*/
public $strings = array();
/**
* Holds the version of WordPress.
*
* @since 2.4.0
*
* @var int
*/
public $wp_version;
/**
* Holds the hook name for the admin page.
*
* @since 2.5.0
*
* @var string
*/
public $page_hook;
/**
* Adds a reference of this object to $instance, populates default strings,
* does the tgmpa_init action hook, and hooks in the interactions to init.
*
* {@internal This method should be `protected`, but as too many TGMPA implementations
* haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues.
* Reverted back to public for the time being.}}
*
* @since 1.0.0
*
* @see TGM_Plugin_Activation::init()
*/
public function __construct() {
// Set the current WordPress version.
$this->wp_version = $GLOBALS['wp_version'];
// Announce that the class is ready, and pass the object (for advanced use).
do_action_ref_array( 'tgmpa_init', array( $this ) );
// When the rest of WP has loaded, kick-start the rest of the class.
add_action( 'init', array( $this, 'init' ) );
}
/**
* Magic method to (not) set protected properties from outside of this class.
*
* {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6 where the `menu` property
* is being assigned rather than tested in a conditional, effectively rendering it useless.
* This 'hack' prevents this from happening.}}
*
* @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593
*
* @since 2.5.2
*
* @param string $name Name of an inaccessible property.
* @param mixed $value Value to assign to the property.
* @return void Silently fail to set the property when this is tried from outside of this class context.
* (Inside this class context, the __set() method if not used as there is direct access.)
*/
public function __set( $name, $value ) {
return;
}
/**
* Magic method to get the value of a protected property outside of this class context.
*
* @since 2.5.2
*
* @param string $name Name of an inaccessible property.
* @return mixed The property value.
*/
public function __get( $name ) {
return $this->{$name};
}
/**
* Initialise the interactions between this class and WordPress.
*
* Hooks in three new methods for the class: admin_menu, notices and styles.
*
* @since 2.0.0
*
* @see TGM_Plugin_Activation::admin_menu()
* @see TGM_Plugin_Activation::notices()
* @see TGM_Plugin_Activation::styles()
*/
public function init() {
/**
* By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter
* you can overrule that behaviour.
*
* @since 2.5.0
*
* @param bool $load Whether or not TGMPA should load.
* Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`.
*/
if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) {
return;
}
// Load class strings.
$this->strings = array(
'page_title' => __( 'Install Required Plugins', 'zerif-lite' ),
'menu_title' => __( 'Install Plugins', 'zerif-lite' ),
/* translators: %s: plugin name. */
'installing' => __( 'Installing Plugin: %s', 'zerif-lite' ),
/* translators: %s: plugin name. */
'updating' => __( 'Updating Plugin: %s', 'zerif-lite' ),
'oops' => __( 'Something went wrong with the plugin API.', 'zerif-lite' ),
'notice_can_install_required' => _n_noop(
/* translators: 1: plugin name(s). */
'This theme requires the following plugin: %1$s.',
'This theme requires the following plugins: %1$s.',
'zerif-lite'
),
'notice_can_install_recommended' => _n_noop(
/* translators: 1: plugin name(s). */
'This theme recommends the following plugin: %1$s.',
'This theme recommends the following plugins: %1$s.',
'zerif-lite'
),
'notice_ask_to_update' => _n_noop(
/* translators: 1: plugin name(s). */
'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',
'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',
'zerif-lite'
),
'notice_ask_to_update_maybe' => _n_noop(
/* translators: 1: plugin name(s). */
'There is an update available for: %1$s.',
'There are updates available for the following plugins: %1$s.',
'zerif-lite'
),
'notice_can_activate_required' => _n_noop(
/* translators: 1: plugin name(s). */
'The following required plugin is currently inactive: %1$s.',
'The following required plugins are currently inactive: %1$s.',
'zerif-lite'
),
'notice_can_activate_recommended' => _n_noop(
/* translators: 1: plugin name(s). */
'The following recommended plugin is currently inactive: %1$s.',
'The following recommended plugins are currently inactive: %1$s.',
'zerif-lite'
),
'install_link' => _n_noop(
'Begin installing plugin',
'Begin installing plugins',
'zerif-lite'
),
'update_link' => _n_noop(
'Begin updating plugin',
'Begin updating plugins',
'zerif-lite'
),
'activate_link' => _n_noop(
'Begin activating plugin',
'Begin activating plugins',
'zerif-lite'
),
'return' => __( 'Return to Required Plugins Installer', 'zerif-lite' ),
'dashboard' => __( 'Return to the Dashboard', 'zerif-lite' ),
'plugin_activated' => __( 'Plugin activated successfully.', 'zerif-lite' ),
'activated_successfully' => __( 'The following plugin was activated successfully:', 'zerif-lite' ),
/* translators: 1: plugin name. */
'plugin_already_active' => __( 'No action taken. Plugin %1$s was already active.', 'zerif-lite' ),
/* translators: 1: plugin name. */
'plugin_needs_higher_version' => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'zerif-lite' ),
/* translators: 1: dashboard link. */
'complete' => __( 'All plugins installed and activated successfully. %1$s', 'zerif-lite' ),
'dismiss' => __( 'Dismiss this notice', 'zerif-lite' ),
'notice_cannot_install_activate' => __( 'There are one or more required or recommended plugins to install, update or activate.', 'zerif-lite' ),
'contact_admin' => __( 'Please contact the administrator of this site for help.', 'zerif-lite' ),
);
do_action( 'tgmpa_register' );
/* After this point, the plugins should be registered and the configuration set. */
// Proceed only if we have plugins to handle.
if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
return;
}
// Set up the menu and notices if we still have outstanding actions.
if ( true !== $this->is_tgmpa_complete() ) {
// Sort the plugins.
array_multisort( $this->sort_order, SORT_ASC, $this->plugins );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_head', array( $this, 'dismiss' ) );
// Prevent the normal links from showing underneath a single install/update page.
add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) );
if ( $this->has_notices ) {
add_action( 'admin_notices', array( $this, 'notices' ) );
add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
}
}
// If needed, filter plugin action links.
add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 );
// Make sure things get reset on switch theme.
add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
if ( $this->has_notices ) {
add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
}
// Setup the force activation hook.
if ( true === $this->has_forced_activation ) {
add_action( 'admin_init', array( $this, 'force_activation' ) );
}
// Setup the force deactivation hook.
if ( true === $this->has_forced_deactivation ) {
add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
}
}
/**
* Hook in plugin action link filters for the WP native plugins page.
*
* - Prevent activation of plugins which don't meet the minimum version requirements.
* - Prevent deactivation of force-activated plugins.
* - Add update notice if update available.
*
* @since 2.5.0
*/
public function add_plugin_action_link_filters() {
foreach ( $this->plugins as $slug => $plugin ) {
if ( false === $this->can_plugin_activate( $slug ) ) {
add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 );
}
if ( true === $plugin['force_activation'] ) {
add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 );
}
if ( false !== $this->does_plugin_require_update( $slug ) ) {
add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 );
}
}
}
/**
* Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the
* minimum version requirements.
*
* @since 2.5.0
*
* @param array $actions Action links.
* @return array
*/
public function filter_plugin_action_links_activate( $actions ) {
unset( $actions['activate'] );
return $actions;
}
/**
* Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate.
*
* @since 2.5.0
*
* @param array $actions Action links.
* @return array
*/
public function filter_plugin_action_links_deactivate( $actions ) {
unset( $actions['deactivate'] );
return $actions;
}
/**
* Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the
* minimum version requirements.
*
* @since 2.5.0
*
* @param array $actions Action links.
* @return array
*/
public function filter_plugin_action_links_update( $actions ) {
$actions['update'] = sprintf(
'<a href="%1$s" title="%2$s" class="edit">%3$s</a>',
esc_url( $this->get_tgmpa_status_url( 'update' ) ),
esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'zerif-lite' ),
esc_html__( 'Update Required', 'zerif-lite' )
);
return $actions;
}
/**
* Handles calls to show plugin information via links in the notices.
*
* We get the links in the admin notices to point to the TGMPA page, rather
* than the typical plugin-install.php file, so we can prepare everything
* beforehand.
*
* WP does not make it easy to show the plugin information in the thickbox -
* here we have to require a file that includes a function that does the
* main work of displaying it, enqueue some styles, set up some globals and
* finally call that function before exiting.
*
* Down right easy once you know how...
*
* Returns early if not the TGMPA page.
*
* @since 2.1.0
*
* @global string $tab Used as iframe div class names, helps with styling
* @global string $body_id Used as the iframe body ID, helps with styling
*
* @return null Returns early if not the TGMPA page.
*/
public function admin_init() {
if ( ! $this->is_tgmpa_page() ) {
return;
}
if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) {
// Needed for install_plugin_information().
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
wp_enqueue_style( 'plugin-install' );
global $tab, $body_id;
$body_id = 'plugin-information';
// @codingStandardsIgnoreStart
$tab = 'plugin-information';
// @codingStandardsIgnoreEnd
install_plugin_information();
exit;
}
}
/**
* Enqueue thickbox scripts/styles for plugin info.
*
* Thickbox is not automatically included on all admin pages, so we must
* manually enqueue it for those pages.
*
* Thickbox is only loaded if the user has not dismissed the admin
* notice or if there are any plugins left to install and activate.
*
* @since 2.1.0
*/
public function thickbox() {
if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
add_thickbox();
}
}
/**
* Adds submenu page if there are plugin actions to take.
*
* This method adds the submenu page letting users know that a required
* plugin needs to be installed.
*
* This page disappears once the plugin has been installed and activated.
*
* @since 1.0.0
*
* @see TGM_Plugin_Activation::init()
* @see TGM_Plugin_Activation::install_plugins_page()
*
* @return null Return early if user lacks capability to install a plugin.
*/
public function admin_menu() {
// Make sure privileges are correct to see the page.
if ( ! current_user_can( 'install_plugins' ) ) {
return;
}
$args = apply_filters(
'tgmpa_admin_menu_args',
array(
'parent_slug' => $this->parent_slug, // Parent Menu slug.
'page_title' => $this->strings['page_title'], // Page title.
'menu_title' => $this->strings['menu_title'], // Menu title.
'capability' => $this->capability, // Capability.
'menu_slug' => $this->menu, // Menu slug.
'function' => array( $this, 'install_plugins_page' ), // Callback.
)
);
$this->add_admin_menu( $args );
}
/**
* Add the menu item.
*
* {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
* generator on the website.}}
*
* @since 2.5.0
*
* @param array $args Menu item configuration.
*/
protected function add_admin_menu( array $args ) {
$this->page_hook = add_theme_page( $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
}
/**
* Echoes plugin installation form.
*
* This method is the callback for the admin_menu method function.
* This displays the admin page and form area where the user can select to install and activate the plugin.
* Aborts early if we're processing a plugin installation action.
*
* @since 1.0.0
*
* @return null Aborts early if we're processing a plugin installation action.
*/
public function install_plugins_page() {
// Store new instance of plugin table in object.
$plugin_table = new TGMPA_List_Table;
// Return early if processing a plugin installation action.
if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
return;
}
// Force refresh of available plugin information so we'll know about manual updates/deletes.
wp_clean_plugins_cache( false );
?>
<div class="tgmpa wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<?php $plugin_table->prepare_items(); ?>
<?php
if ( ! empty( $this->message ) && is_string( $this->message ) ) {
echo wp_kses_post( $this->message );
}
?>
<?php $plugin_table->views(); ?>
<form id="tgmpa-plugins" action="" method="post">
<input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
<input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
<?php $plugin_table->display(); ?>
</form>
</div>
<?php
}
/**
* Installs, updates or activates a plugin depending on the action link clicked by the user.
*
* Checks the $_GET variable to see which actions have been
* passed and responds with the appropriate method.
*
* Uses WP_Filesystem to process and handle the plugin installation
* method.
*
* @since 1.0.0
*
* @uses WP_Filesystem
* @uses WP_Error
* @uses WP_Upgrader
* @uses Plugin_Upgrader
* @uses Plugin_Installer_Skin
* @uses Plugin_Upgrader_Skin
*
* @return boolean True on success, false on failure.
*/
protected function do_plugin_install() {
if ( empty( $_GET['plugin'] ) ) {
return false;
}
// All plugin information will be stored in an array for processing.
$slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
if ( ! isset( $this->plugins[ $slug ] ) ) {
return false;
}
// Was an install or upgrade action link clicked?
if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
$install_type = 'install';
if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
$install_type = 'update';
}
check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
// Pass necessary information via URL if WP_Filesystem is needed.
$url = wp_nonce_url(
add_query_arg(
array(
'plugin' => urlencode( $slug ),
'tgmpa-' . $install_type => $install_type . '-plugin',
),
$this->get_tgmpa_url()
),
'tgmpa-' . $install_type,
'tgmpa-nonce'
);
$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) {
return true;
}
if ( ! WP_Filesystem( $creds ) ) {
request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
return true;
}
/* If we arrive here, we have the filesystem. */
// Prep variables for Plugin_Installer_Skin class.
$extra = array();
$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
$source = $this->get_download_url( $slug );
$api = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
$api = ( false !== $api ) ? $api : null;
$url = add_query_arg(
array(
'action' => $install_type . '-plugin',
'plugin' => urlencode( $slug ),
),
'update.php'
);
if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
}
$title = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
$skin_args = array(
'type' => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
'title' => sprintf( $title, $this->plugins[ $slug ]['name'] ),
'url' => esc_url_raw( $url ),
'nonce' => $install_type . '-plugin_' . $slug,
'plugin' => '',
'api' => $api,
'extra' => $extra,
);
unset( $title );
if ( 'update' === $install_type ) {
$skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
$skin = new Plugin_Upgrader_Skin( $skin_args );
} else {
$skin = new Plugin_Installer_Skin( $skin_args );
}
// Create a new instance of Plugin_Upgrader.
$upgrader = new Plugin_Upgrader( $skin );
// Perform the action and install the plugin from the $source urldecode().
add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
if ( 'update' === $install_type ) {
// Inject our info into the update transient.
$to_inject = array( $slug => $this->plugins[ $slug ] );
$to_inject[ $slug ]['source'] = $source;
$this->inject_update_info( $to_inject );
$upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
} else {
$upgrader->install( $source );
}
remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 );
// Make sure we have the correct file path now the plugin is installed/updated.
$this->populate_file_path( $slug );
// Only activate plugins if the config option is set to true and the plugin isn't
// already active (upgrade).
if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
$plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
return true; // Finish execution of the function early as we encountered an error.
}
}
$this->show_tgmpa_version();
// Display message based on if all plugins are now active or not.
if ( $this->is_tgmpa_complete() ) {
echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'zerif-lite' ) . '</a>' ), '</p>';
echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
} else {
echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
}
return true;
} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
// Activate action link was clicked.
check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
return true; // Finish execution of the function early as we encountered an error.
}
}
return false;
}
/**
* Inject information into the 'update_plugins' site transient as WP checks that before running an update.
*
* @since 2.5.0
*
* @param array $plugins The plugin information for the plugins which are to be updated.
*/
public function inject_update_info( $plugins ) {
$repo_updates = get_site_transient( 'update_plugins' );
if ( ! is_object( $repo_updates ) ) {
$repo_updates = new stdClass;
}
foreach ( $plugins as $slug => $plugin ) {
$file_path = $plugin['file_path'];
if ( empty( $repo_updates->response[ $file_path ] ) ) {
$repo_updates->response[ $file_path ] = new stdClass;
}
// We only really need to set package, but let's do all we can in case WP changes something.
$repo_updates->response[ $file_path ]->slug = $slug;
$repo_updates->response[ $file_path ]->plugin = $file_path;
$repo_updates->response[ $file_path ]->new_version = $plugin['version'];
$repo_updates->response[ $file_path ]->package = $plugin['source'];
if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
$repo_updates->response[ $file_path ]->url = $plugin['external_url'];
}
}
set_site_transient( 'update_plugins', $repo_updates );
}
/**
* Adjust the plugin directory name if necessary.
*
* The final destination directory of a plugin is based on the subdirectory name found in the
* (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
* subdirectory name is not the same as the expected slug and the plugin will not be recognized
* as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
* the expected plugin slug.
*
* @since 2.5.0
*
* @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/.
* @param string $remote_source Path to upgrade/zip-file-name.tmp.
* @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin.
* @return string $source
*/
public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
return $source;
}
// Check for single file plugins.
$source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
return $source;
}
// Multi-file plugin, let's see if the directory is correctly named.
$desired_slug = '';
// Figure out what the slug is supposed to be.
if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
$desired_slug = $upgrader->skin->options['extra']['slug'];
} else {
// Bulk installer contains less info, so fall back on the info registered here.
foreach ( $this->plugins as $slug => $plugin ) {
if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
$desired_slug = $slug;
break;
}
}
unset( $slug, $plugin );
}
if ( ! empty( $desired_slug ) ) {
$subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
$from_path = untrailingslashit( $source );
$to_path = trailingslashit( $remote_source ) . $desired_slug;
if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
return trailingslashit( $to_path );
} else {
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.', 'zerif-lite' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'zerif-lite' ), array(
'found' => $subdir_name,
'expected' => $desired_slug,
)
);
}
} elseif ( empty( $subdir_name ) ) {
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.', 'zerif-lite' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'zerif-lite' ), array(
'found' => $subdir_name,
'expected' => $desired_slug,
)
);
}
}
return $source;
}
/**
* Activate a single plugin and send feedback about the result to the screen.
*
* @since 2.5.0
*
* @param string $file_path Path within wp-plugins/ to main plugin file.
* @param string $slug Plugin slug.
* @param bool $automatic Whether this is an automatic activation after an install. Defaults to false.
* This determines the styling of the output messages.
* @return bool False if an error was encountered, true otherwise.
*/
protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
if ( $this->can_plugin_activate( $slug ) ) {
$activate = activate_plugin( $file_path );
if ( is_wp_error( $activate ) ) {
echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
return false; // End it here if there is an error with activation.
} else {
if ( ! $automatic ) {
// Make sure message doesn't display again if bulk activation is performed
// immediately after a single activation.
if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
}
} else {
// Simpler message layout for use on the plugin install page.
echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
}
}
} elseif ( $this->is_plugin_active( $slug ) ) {
// No simpler message format provided as this message should never be encountered
// on the plugin install page.
echo '<div id="message" class="error"><p>',
sprintf(
esc_html( $this->strings['plugin_already_active'] ),
'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
),
'</p></div>';
} elseif ( $this->does_plugin_require_update( $slug ) ) {
if ( ! $automatic ) {
// Make sure message doesn't display again if bulk activation is performed
// immediately after a single activation.
if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
echo '<div id="message" class="error"><p>',
sprintf(
esc_html( $this->strings['plugin_needs_higher_version'] ),