forked from wp-cli/extension-command
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin_Command.php
1408 lines (1275 loc) · 44.7 KB
/
Plugin_Command.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
use WP_CLI\ParsePluginNameInput;
use WP_CLI\Utils;
use WP_CLI\WpOrgApi;
/**
* Manages plugins, including installs, activations, and updates.
*
* See the WordPress [Plugin Handbook](https://developer.wordpress.org/plugins/) developer resource for more information on plugins.
*
* ## EXAMPLES
*
* # Activate plugin
* $ wp plugin activate hello
* Plugin 'hello' activated.
* Success: Activated 1 of 1 plugins.
*
* # Deactivate plugin
* $ wp plugin deactivate hello
* Plugin 'hello' deactivated.
* Success: Deactivated 1 of 1 plugins.
*
* # Delete plugin
* $ wp plugin delete hello
* Deleted 'hello' plugin.
* Success: Deleted 1 of 1 plugins.
*
* # Install the latest version from wordpress.org and activate
* $ wp plugin install bbpress --activate
* Installing bbPress (2.5.9)
* Downloading install package from https://downloads.wordpress.org/plugin/bbpress.2.5.9.zip...
* Using cached file '/home/vagrant/.wp-cli/cache/plugin/bbpress-2.5.9.zip'...
* Unpacking the package...
* Installing the plugin...
* Plugin installed successfully.
* Activating 'bbpress'...
* Plugin 'bbpress' activated.
* Success: Installed 1 of 1 plugins.
*
* @package wp-cli
*/
class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
use ParsePluginNameInput;
protected $item_type = 'plugin';
protected $upgrade_refresh = 'wp_update_plugins';
protected $upgrade_transient = 'update_plugins';
protected $check_wporg = [
'status' => false,
'last_updated' => false,
];
protected $obj_fields = array(
'name',
'status',
'update',
'version',
'update_version',
'auto_update',
);
/**
* Plugin fetcher instance.
*
* @var \WP_CLI\Fetchers\Plugin
*/
protected $fetcher;
public function __construct() {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
parent::__construct();
$this->fetcher = new WP_CLI\Fetchers\Plugin();
}
protected function get_upgrader_class( $force ) {
return $force ? '\\WP_CLI\\DestructivePluginUpgrader' : 'Plugin_Upgrader';
}
/**
* Reveals the status of one or all plugins.
*
* ## OPTIONS
*
* [<plugin>]
* : A particular plugin to show the status for.
*
* ## EXAMPLES
*
* # Displays status of all plugins
* $ wp plugin status
* 5 installed plugins:
* I akismet 3.1.11
* I easy-digital-downloads 2.5.16
* A theme-check 20160523.1
* I wen-logo-slider 2.0.3
* M ns-pack 1.0.0
* Legend: I = Inactive, A = Active, M = Must Use
*
* # Displays status of a plugin
* $ wp plugin status theme-check
* Plugin theme-check details:
* Name: Theme Check
* Status: Active
* Version: 20160523.1
* Author: Otto42, pross
* Description: A simple and easy way to test your theme for all the latest WordPress standards and practices. A great theme development tool!
*/
public function status( $args ) {
parent::status( $args );
}
/**
* Searches the WordPress.org plugin directory.
*
* Displays plugins in the WordPress.org plugin directory matching a given
* search query.
*
* ## OPTIONS
*
* <search>
* : The string to search for.
*
* [--page=<page>]
* : Optional page to display.
* ---
* default: 1
* ---
*
* [--per-page=<per-page>]
* : Optional number of results to display.
* ---
* default: 10
* ---
*
* [--field=<field>]
* : Prints the value of a single field for each plugin.
*
* [--fields=<fields>]
* : Ask for specific fields from the API. Defaults to name,slug,author_profile,rating. Acceptable values:
*
* **name**: Plugin Name
* **slug**: Plugin Slug
* **version**: Current Version Number
* **author**: Plugin Author
* **author_profile**: Plugin Author Profile
* **contributors**: Plugin Contributors
* **requires**: Plugin Minimum Requirements
* **tested**: Plugin Tested Up To
* **compatibility**: Plugin Compatible With
* **rating**: Plugin Rating in Percent and Total Number
* **ratings**: Plugin Ratings for each star (1-5)
* **num_ratings**: Number of Plugin Ratings
* **homepage**: Plugin Author's Homepage
* **description**: Plugin's Description
* **short_description**: Plugin's Short Description
* **sections**: Plugin Readme Sections: description, installation, FAQ, screenshots, other notes, and changelog
* **downloaded**: Plugin Download Count
* **last_updated**: Plugin's Last Update
* **added**: Plugin's Date Added to wordpress.org Repository
* **tags**: Plugin's Tags
* **versions**: Plugin's Available Versions with D/L Link
* **donate_link**: Plugin's Donation Link
* **banners**: Plugin's Banner Image Link
* **icons**: Plugin's Icon Image Link
* **active_installs**: Plugin's Number of Active Installs
* **contributors**: Plugin's List of Contributors
* **url**: Plugin's URL on wordpress.org
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - count
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* $ wp plugin search dsgnwrks --per-page=20 --format=json
* Success: Showing 3 of 3 plugins.
* [{"name":"DsgnWrks Instagram Importer Debug","slug":"dsgnwrks-instagram-importer-debug","rating":0},{"name":"DsgnWrks Instagram Importer","slug":"dsgnwrks-instagram-importer","rating":84},{"name":"DsgnWrks Twitter Importer","slug":"dsgnwrks-twitter-importer","rating":80}]
*
* $ wp plugin search dsgnwrks --fields=name,version,slug,rating,num_ratings
* Success: Showing 3 of 3 plugins.
* +-----------------------------------+---------+-----------------------------------+--------+-------------+
* | name | version | slug | rating | num_ratings |
* +-----------------------------------+---------+-----------------------------------+--------+-------------+
* | DsgnWrks Instagram Importer Debug | 0.1.6 | dsgnwrks-instagram-importer-debug | 0 | 0 |
* | DsgnWrks Instagram Importer | 1.3.7 | dsgnwrks-instagram-importer | 84 | 23 |
* | DsgnWrks Twitter Importer | 1.1.1 | dsgnwrks-twitter-importer | 80 | 1 |
* +-----------------------------------+---------+-----------------------------------+--------+-------------+
*/
public function search( $args, $assoc_args ) {
parent::_search( $args, $assoc_args );
}
protected function status_single( $args ) {
$plugin = $this->fetcher->get_check( $args[0] );
$file = $plugin->file;
$details = $this->get_details( $file );
$status = $this->format_status( $this->get_status( $file ), 'long' );
$version = $details['Version'];
if ( $this->has_update( $file ) ) {
$version .= ' (%gUpdate available%n)';
}
echo WP_CLI::colorize(
Utils\mustache_render(
self::get_template_path( 'plugin-status.mustache' ),
[
'slug' => Utils\get_plugin_name( $file ),
'status' => $status,
'version' => $version,
'name' => $details['Name'],
'author' => $details['Author'],
'description' => $details['Description'],
]
)
);
}
protected function get_all_items() {
$items = $this->get_item_list();
foreach ( get_mu_plugins() as $file => $mu_plugin ) {
$mu_version = '';
if ( ! empty( $mu_plugin['Version'] ) ) {
$mu_version = $mu_plugin['Version'];
}
$mu_title = '';
if ( ! empty( $mu_plugin['Name'] ) ) {
$mu_title = $mu_plugin['Title'];
}
$mu_description = '';
if ( ! empty( $mu_plugin['Description'] ) ) {
$mu_description = $mu_plugin['Description'];
}
$mu_name = Utils\get_plugin_name( $file );
$wporg_info = $this->get_wporg_data( $mu_name );
$items[ $file ] = array(
'name' => $mu_name,
'status' => 'must-use',
'update' => false,
'update_version' => null,
'update_package' => null,
'version' => $mu_version,
'update_id' => '',
'title' => $mu_title,
'description' => $mu_description,
'file' => $file,
'auto_update' => false,
'wporg_status' => $wporg_info['status'],
'wporg_last_updated' => $wporg_info['last_updated'],
);
}
$raw_items = get_dropins();
$raw_data = _get_dropins();
foreach ( $raw_items as $name => $item_data ) {
$description = ! empty( $raw_data[ $name ][0] ) ? $raw_data[ $name ][0] : '';
$items[ $name ] = [
'name' => $name,
'title' => $item_data['Title'],
'description' => $description,
'status' => 'dropin',
'update' => false,
'update_version' => null,
'update_package' => null,
'update_id' => '',
'file' => $name,
'auto_update' => false,
'author' => $item_data['Author'],
'wporg_status' => '',
'wporg_last_updated' => '',
];
}
return $items;
}
/**
* Activates one or more plugins.
*
* ## OPTIONS
*
* [<plugin>...]
* : One or more plugins to activate.
*
* [--all]
* : If set, all plugins will be activated.
*
* [--exclude=<name>]
* : Comma separated list of plugin slugs to be excluded from activation.
*
* [--network]
* : If set, the plugin will be activated for the entire multisite network.
*
* ## EXAMPLES
*
* # Activate plugin
* $ wp plugin activate hello
* Plugin 'hello' activated.
* Success: Activated 1 of 1 plugins.
*
* # Activate plugin in entire multisite network
* $ wp plugin activate hello --network
* Plugin 'hello' network activated.
* Success: Network activated 1 of 1 plugins.
*/
public function activate( $args, $assoc_args = array() ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network', false );
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
$args = $this->check_optional_args_and_all( $args, $all, 'activate', $all_exclude );
if ( ! $args ) {
return;
}
$successes = 0;
$errors = 0;
$plugins = $this->fetcher->get_many( $args );
if ( count( $plugins ) < count( $args ) ) {
$errors = count( $args ) - count( $plugins );
}
foreach ( $plugins as $plugin ) {
$status = $this->get_status( $plugin->file );
if ( $all && in_array( $status, [ 'active', 'active-network' ], true ) ) {
continue;
}
// Network-active is the highest level of activation status.
if ( 'active-network' === $status ) {
WP_CLI::warning( "Plugin '{$plugin->name}' is already network active." );
continue;
}
// Don't reactivate active plugins, but do let them become network-active.
if ( ! $network_wide && 'active' === $status ) {
WP_CLI::warning( "Plugin '{$plugin->name}' is already active." );
continue;
}
// Plugins need to be deactivated before being network activated.
if ( $network_wide && 'active' === $status ) {
deactivate_plugins( $plugin->file, false, false );
}
$result = activate_plugin( $plugin->file, '', $network_wide );
if ( is_wp_error( $result ) ) {
$message = $result->get_error_message();
$message = preg_replace( '/<a\s[^>]+>.*<\/a>/im', '', $message );
$message = wp_strip_all_tags( $message );
$message = str_replace( 'Error: ', '', $message );
WP_CLI::warning( "Failed to activate plugin. {$message}" );
++$errors;
} else {
$this->active_output( $plugin->name, $plugin->file, $network_wide, 'activate' );
++$successes;
}
}
if ( ! $this->chained_command ) {
$verb = $network_wide ? 'network activate' : 'activate';
Utils\report_batch_operation_results( 'plugin', $verb, count( $args ), $successes, $errors );
}
}
/**
* Deactivates one or more plugins.
*
* ## OPTIONS
*
* [<plugin>...]
* : One or more plugins to deactivate.
*
* [--uninstall]
* : Uninstall the plugin after deactivation.
*
* [--all]
* : If set, all plugins will be deactivated.
*
* [--exclude=<name>]
* : Comma separated list of plugin slugs that should be excluded from deactivation.
*
* [--network]
* : If set, the plugin will be deactivated for the entire multisite network.
*
* ## EXAMPLES
*
* # Deactivate plugin
* $ wp plugin deactivate hello
* Plugin 'hello' deactivated.
* Success: Deactivated 1 of 1 plugins.
*
* # Deactivate all plugins with exclusion
* $ wp plugin deactivate --all --exclude=hello,wordpress-seo
* Plugin 'contact-form-7' deactivated.
* Plugin 'ninja-forms' deactivated.
* Success: Deactivated 2 of 2 plugins.
*/
public function deactivate( $args, $assoc_args = array() ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );
$disable_all = Utils\get_flag_value( $assoc_args, 'all' );
$disable_all_exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
$args = $this->check_optional_args_and_all( $args, $disable_all, 'deactivate', $disable_all_exclude );
if ( ! $args ) {
return;
}
$successes = 0;
$errors = 0;
$plugins = $this->fetcher->get_many( $args );
if ( count( $plugins ) < count( $args ) ) {
$errors = count( $args ) - count( $plugins );
}
foreach ( $plugins as $plugin ) {
$status = $this->get_status( $plugin->file );
if ( $disable_all && ! in_array( $status, [ 'active', 'active-network' ], true ) ) {
continue;
}
// Network active plugins must be explicitly deactivated.
if ( ! $network_wide && 'active-network' === $status ) {
WP_CLI::warning( "Plugin '{$plugin->name}' is network active and must be deactivated with --network flag." );
++$errors;
continue;
}
if ( ! in_array( $status, [ 'active', 'active-network' ], true ) ) {
WP_CLI::warning( "Plugin '{$plugin->name}' isn't active." );
continue;
}
deactivate_plugins( $plugin->file, false, $network_wide );
if ( ! is_network_admin() ) {
update_option(
'recently_activated',
array( $plugin->file => time() ) + (array) get_option( 'recently_activated' )
);
} else {
update_site_option(
'recently_activated',
array( $plugin->file => time() ) + (array) get_site_option( 'recently_activated' )
);
}
$this->active_output( $plugin->name, $plugin->file, $network_wide, 'deactivate' );
++$successes;
if ( Utils\get_flag_value( $assoc_args, 'uninstall' ) ) {
WP_CLI::log( "Uninstalling '{$plugin->name}'..." );
$this->chained_command = true;
$this->uninstall( array( $plugin->name ) );
$this->chained_command = false;
}
}
if ( ! $this->chained_command ) {
$verb = $network_wide ? 'network deactivate' : 'deactivate';
Utils\report_batch_operation_results( 'plugin', $verb, count( $args ), $successes, $errors );
}
}
/**
* Toggles a plugin's activation state.
*
* If the plugin is active, then it will be deactivated. If the plugin is
* inactive, then it will be activated.
*
* ## OPTIONS
*
* <plugin>...
* : One or more plugins to toggle.
*
* [--network]
* : If set, the plugin will be toggled for the entire multisite network.
*
* ## EXAMPLES
*
* # Akismet is currently activated
* $ wp plugin toggle akismet
* Plugin 'akismet' deactivated.
* Success: Toggled 1 of 1 plugins.
*
* # Akismet is currently deactivated
* $ wp plugin toggle akismet
* Plugin 'akismet' activated.
* Success: Toggled 1 of 1 plugins.
*/
public function toggle( $args, $assoc_args = array() ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );
$successes = 0;
$errors = 0;
$plugins = $this->fetcher->get_many( $args );
if ( count( $plugins ) < count( $args ) ) {
$errors = count( $args ) - count( $plugins );
}
$this->chained_command = true;
foreach ( $plugins as $plugin ) {
if ( $this->check_active( $plugin->file, $network_wide ) ) {
$this->deactivate( array( $plugin->name ), $assoc_args );
} else {
$this->activate( array( $plugin->name ), $assoc_args );
}
++$successes;
}
$this->chained_command = false;
Utils\report_batch_operation_results( 'plugin', 'toggle', count( $args ), $successes, $errors );
}
/**
* Gets the path to a plugin or to the plugin directory.
*
* ## OPTIONS
*
* [<plugin>]
* : The plugin to get the path to. If not set, will return the path to the
* plugins directory.
*
* [--dir]
* : If set, get the path to the closest parent directory, instead of the
* plugin file.
*
* ## EXAMPLES
*
* $ cd $(wp plugin path) && pwd
* /var/www/wordpress/wp-content/plugins
*/
public function path( $args, $assoc_args ) {
$path = untrailingslashit( WP_PLUGIN_DIR );
if ( ! empty( $args ) ) {
$plugin = $this->fetcher->get_check( $args[0] );
$path .= '/' . $plugin->file;
if ( Utils\get_flag_value( $assoc_args, 'dir' ) ) {
$path = dirname( $path );
}
}
WP_CLI::line( $path );
}
protected function install_from_repo( $slug, $assoc_args ) {
$api = plugins_api( 'plugin_information', array( 'slug' => $slug ) );
if ( is_wp_error( $api ) ) {
return $api;
}
if ( isset( $assoc_args['version'] ) ) {
self::alter_api_response( $api, $assoc_args['version'] );
}
$status = install_plugin_install_status( $api );
if ( ! Utils\get_flag_value( $assoc_args, 'force' ) && 'install' !== $status['status'] ) {
// We know this will fail, so avoid a needless download of the package.
return new WP_Error( 'already_installed', 'Plugin already installed.' );
}
WP_CLI::log( sprintf( 'Installing %s (%s)', html_entity_decode( $api->name, ENT_QUOTES ), $api->version ) );
if ( Utils\get_flag_value( $assoc_args, 'version' ) !== 'dev' ) {
WP_CLI::get_http_cache_manager()->whitelist_package( $api->download_link, $this->item_type, $api->slug, $api->version );
}
// Ignore failures on accessing SSL "https://api.wordpress.org/plugins/update-check/1.1/" in `wp_update_plugins()` which seem to occur intermittently.
set_error_handler( array( __CLASS__, 'error_handler' ), E_USER_WARNING | E_USER_NOTICE );
$result = $this->get_upgrader( $assoc_args )->install( $api->download_link );
restore_error_handler();
return $result;
}
/**
* Updates one or more plugins.
*
* ## OPTIONS
*
* [<plugin>...]
* : One or more plugins to update.
*
* [--all]
* : If set, all plugins that have updates will be updated.
*
* [--exclude=<name>]
* : Comma separated list of plugin names that should be excluded from updating.
*
* [--minor]
* : Only perform updates for minor releases (e.g. from 1.3 to 1.4 instead of 2.0)
*
* [--patch]
* : Only perform updates for patch releases (e.g. from 1.3 to 1.3.3 instead of 1.4)
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* [--version=<version>]
* : If set, the plugin will be updated to the specified version.
*
* [--dry-run]
* : Preview which plugins would be updated.
*
* [--insecure]
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
* ## EXAMPLES
*
* $ wp plugin update bbpress --version=dev
* Installing bbPress (Development Version)
* Downloading install package from https://downloads.wordpress.org/plugin/bbpress.zip...
* Unpacking the package...
* Installing the plugin...
* Removing the old version of the plugin...
* Plugin updated successfully.
* Success: Updated 1 of 2 plugins.
*
* $ wp plugin update --all
* Enabling Maintenance mode...
* Downloading update from https://downloads.wordpress.org/plugin/akismet.3.1.11.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the plugin...
* Plugin updated successfully.
* Downloading update from https://downloads.wordpress.org/plugin/nginx-champuru.3.2.0.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the plugin...
* Plugin updated successfully.
* Disabling Maintenance mode...
* +------------------------+-------------+-------------+---------+
* | name | old_version | new_version | status |
* +------------------------+-------------+-------------+---------+
* | akismet | 3.1.3 | 3.1.11 | Updated |
* | nginx-cache-controller | 3.1.1 | 3.2.0 | Updated |
* +------------------------+-------------+-------------+---------+
* Success: Updated 2 of 2 plugins.
*
* $ wp plugin update --all --exclude=akismet
* Enabling Maintenance mode...
* Downloading update from https://downloads.wordpress.org/plugin/nginx-champuru.3.2.0.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the plugin...
* Plugin updated successfully.
* Disabling Maintenance mode...
* +------------------------+-------------+-------------+---------+
* | name | old_version | new_version | status |
* +------------------------+-------------+-------------+---------+
* | nginx-cache-controller | 3.1.1 | 3.2.0 | Updated |
* +------------------------+-------------+-------------+---------+
*
* @alias upgrade
*/
public function update( $args, $assoc_args ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$args = $this->check_optional_args_and_all( $args, $all );
if ( ! $args ) {
return;
}
if ( isset( $assoc_args['version'] ) ) {
foreach ( $this->fetcher->get_many( $args ) as $plugin ) {
$assoc_args['force'] = 1;
$this->install( array( $plugin->name ), $assoc_args );
}
} else {
parent::update_many( $args, $assoc_args );
}
}
protected function get_item_list() {
$items = [];
$duplicate_names = [];
$auto_updates = get_site_option( Plugin_AutoUpdates_Command::SITE_OPTION );
if ( false === $auto_updates ) {
$auto_updates = [];
}
foreach ( $this->get_all_plugins() as $file => $details ) {
$all_update_info = $this->get_update_info();
$update_info = ( isset( $all_update_info->response[ $file ] ) && null !== $all_update_info->response[ $file ] ) ? (array) $all_update_info->response[ $file ] : null;
$name = Utils\get_plugin_name( $file );
$wporg_info = $this->get_wporg_data( $name );
if ( ! isset( $duplicate_names[ $name ] ) ) {
$duplicate_names[ $name ] = array();
}
$duplicate_names[ $name ][] = $file;
$items[ $file ] = [
'name' => $name,
'status' => $this->get_status( $file ),
'update' => (bool) $update_info,
'update_version' => isset( $update_info ) && isset( $update_info['new_version'] ) ? $update_info['new_version'] : null,
'update_package' => isset( $update_info ) && isset( $update_info['package'] ) ? $update_info['package'] : null,
'version' => $details['Version'],
'update_id' => $file,
'title' => $details['Name'],
'description' => wordwrap( $details['Description'] ),
'file' => $file,
'auto_update' => in_array( $file, $auto_updates, true ),
'author' => $details['Author'],
'wporg_status' => $wporg_info['status'],
'wporg_last_updated' => $wporg_info['last_updated'],
];
if ( null === $update_info ) {
// Get info for all plugins that don't have an update.
$plugin_update_info = isset( $all_update_info->no_update[ $file ] ) ? $all_update_info->no_update[ $file ] : null;
// Compare version and update information in plugin list.
if ( null !== $plugin_update_info && version_compare( $details['Version'], $plugin_update_info->new_version, '>' ) ) {
$items[ $file ]['update'] = static::INVALID_VERSION_MESSAGE;
}
}
}
foreach ( $duplicate_names as $name => $files ) {
if ( count( $files ) <= 1 ) {
continue;
}
foreach ( $files as $file ) {
$items[ $file ]['name'] = str_replace( '.' . pathinfo( $file, PATHINFO_EXTENSION ), '', $file );
}
}
return $items;
}
/**
* Get the wordpress.org status of a plugin.
*
* @param string $plugin_name The plugin slug.
*
* @return string The status of the plugin, includes the last update date.
*/
protected function get_wporg_data( $plugin_name ) {
$data = [
'status' => '',
'last_updated' => '',
];
if ( ! $this->check_wporg['status'] && ! $this->check_wporg['last_updated'] ) {
return $data;
}
if ( $this->check_wporg ) {
try {
$plugin_data = ( new WpOrgApi() )->get_plugin_info( $plugin_name );
} catch ( Exception $e ) {
// Request failed. The plugin is not (active) on .org.
$plugin_data = false;
}
if ( $plugin_data ) {
$data['status'] = 'active';
if ( ! $this->check_wporg['last_updated'] ) {
return $data; // The plugin is active on .org, but we don't need the date.
}
}
// Just because the plugin is not in the api, does not mean it was never on .org.
}
$request = wp_remote_get( "https://plugins.trac.wordpress.org/log/{$plugin_name}/?limit=1&mode=stop_on_copy&format=rss" );
$response_code = wp_remote_retrieve_response_code( $request );
if ( 404 === $response_code ) {
return $data; // This plugin was never on .org, there is no date to check.
}
if ( 'active' !== $data['status'] ) {
$data['status'] = 'closed'; // This plugin was on .org at some point, but not anymore.
}
if ( ! class_exists( 'SimpleXMLElement' ) ) {
WP_CLI::error( "The PHP extension 'SimpleXMLElement' is not available but is required for XML-formatted output." );
}
// Check the last update date.
$r_body = wp_remote_retrieve_body( $request );
if ( str_contains( $r_body, 'pubDate' ) ) {
// Very raw check, not validating the format or anything else.
$xml = simplexml_load_string( $r_body );
$xml_pub_date = $xml->xpath( '//pubDate' );
if ( $xml_pub_date ) {
$data['last_updated'] = wp_date( 'Y-m-d', (string) strtotime( $xml_pub_date[0] ) );
}
}
return $data;
}
protected function filter_item_list( $items, $args ) {
$basenames = wp_list_pluck( $this->fetcher->get_many( $args ), 'file' );
return Utils\pick_fields( $items, $basenames );
}
/**
* Installs one or more plugins.
*
* ## OPTIONS
*
* <plugin|zip|url>...
* : One or more plugins to install. Accepts a plugin slug, the path to a local zip file, or a URL to a remote zip file.
*
* [--version=<version>]
* : If set, get that particular version from wordpress.org, instead of the
* stable version.
*
* [--force]
* : If set, the command will overwrite any installed version of the plugin, without prompting
* for confirmation.
*
* [--activate]
* : If set, the plugin will be activated immediately after install.
*
* [--activate-network]
* : If set, the plugin will be network activated immediately after install
*
* [--insecure]
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
* ## EXAMPLES
*
* # Install the latest version from wordpress.org and activate
* $ wp plugin install bbpress --activate
* Installing bbPress (2.5.9)
* Downloading install package from https://downloads.wordpress.org/plugin/bbpress.2.5.9.zip...
* Using cached file '/home/vagrant/.wp-cli/cache/plugin/bbpress-2.5.9.zip'...
* Unpacking the package...
* Installing the plugin...
* Plugin installed successfully.
* Activating 'bbpress'...
* Plugin 'bbpress' activated.
* Success: Installed 1 of 1 plugins.
*
* # Install the development version from wordpress.org
* $ wp plugin install bbpress --version=dev
* Installing bbPress (Development Version)
* Downloading install package from https://downloads.wordpress.org/plugin/bbpress.zip...
* Unpacking the package...
* Installing the plugin...
* Plugin installed successfully.
* Success: Installed 1 of 1 plugins.
*
* # Install from a local zip file
* $ wp plugin install ../my-plugin.zip
* Unpacking the package...
* Installing the plugin...
* Plugin installed successfully.
* Success: Installed 1 of 1 plugins.
*
* # Install from a remote zip file
* $ wp plugin install http://s3.amazonaws.com/bucketname/my-plugin.zip?AWSAccessKeyId=123&Expires=456&Signature=abcdef
* Downloading install package from http://s3.amazonaws.com/bucketname/my-plugin.zip?AWSAccessKeyId=123&Expires=456&Signature=abcdef
* Unpacking the package...
* Installing the plugin...
* Plugin installed successfully.
* Success: Installed 1 of 1 plugins.
*
* # Update from a remote zip file
* $ wp plugin install https://github.com/envato/wp-envato-market/archive/master.zip --force
* Downloading install package from https://github.com/envato/wp-envato-market/archive/master.zip
* Unpacking the package...
* Installing the plugin...
* Renamed Github-based project from 'wp-envato-market-master' to 'wp-envato-market'.
* Plugin updated successfully
* Success: Installed 1 of 1 plugins.
*
* # Forcefully re-install all installed plugins
* $ wp plugin install $(wp plugin list --field=name) --force
* Installing Akismet (3.1.11)
* Downloading install package from https://downloads.wordpress.org/plugin/akismet.3.1.11.zip...
* Unpacking the package...
* Installing the plugin...
* Removing the old version of the plugin...
* Plugin updated successfully
* Success: Installed 1 of 1 plugins.
*/
public function install( $args, $assoc_args ) {
if ( ! is_dir( WP_PLUGIN_DIR ) ) {
wp_mkdir_p( WP_PLUGIN_DIR );
}
parent::install( $args, $assoc_args );
}
/**
* Gets details about an installed plugin.
*
* ## OPTIONS
*
* <plugin>
* : The plugin to get.
*
* [--field=<field>]
* : Instead of returning the whole plugin, returns the value of a single field.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to all fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* $ wp plugin get bbpress --format=json
* {"name":"bbpress","title":"bbPress","author":"The bbPress Contributors","version":"2.6-alpha","description":"bbPress is forum software with a twist from the creators of WordPress.","status":"active"}
*/
public function get( $args, $assoc_args ) {
$plugin = $this->fetcher->get_check( $args[0] );
$file = $plugin->file;
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );
$plugin_obj = (object) [
'name' => Utils\get_plugin_name( $file ),
'title' => $plugin_data['Name'],
'author' => $plugin_data['Author'],
'version' => $plugin_data['Version'],
'description' => wordwrap( $plugin_data['Description'] ),
'status' => $this->get_status( $file ),
];
if ( isset( $plugin_data['RequiresPlugins'] ) && ! empty( $plugin_data['RequiresPlugins'] ) ) {
$plugin_obj->requires_plugins = $plugin_data['RequiresPlugins'];
}
if ( empty( $assoc_args['fields'] ) ) {
$plugin_array = get_object_vars( $plugin_obj );
$assoc_args['fields'] = array_keys( $plugin_array );
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $plugin_obj );
}
/**
* Uninstalls one or more plugins.
*
* ## OPTIONS
*
* [<plugin>...]
* : One or more plugins to uninstall.
*
* [--deactivate]
* : Deactivate the plugin before uninstalling. Default behavior is to warn and skip if the plugin is active.
*
* [--skip-delete]
* : If set, the plugin files will not be deleted. Only the uninstall procedure
* will be run.
*
* [--all]
* : If set, all plugins will be uninstalled.
*
* [--exclude=<name>]
* : Comma separated list of plugin slugs to be excluded from uninstall.
*
* ## EXAMPLES
*
* $ wp plugin uninstall hello