1
1
<?php
2
2
3
+ use WP_CLI \CommandWithUpgrade ;
3
4
use WP_CLI \ParsePluginNameInput ;
4
5
use WP_CLI \Utils ;
5
6
use WP_CLI \WpOrgApi ;
41
42
* Success: Installed 1 of 1 plugins.
42
43
*
43
44
* @package wp-cli
45
+ *
46
+ * @phpstan-type PluginInformation object{name: string, slug: non-empty-string, version: string, new_version: string, download_link: string, requires_php?: string, requires?: string, package: string}&\stdClass
47
+ * @extends CommandWithUpgrade<string,>
44
48
*/
45
- class Plugin_Command extends \WP_CLI \CommandWithUpgrade {
46
-
49
+ class Plugin_Command extends CommandWithUpgrade {
47
50
use ParsePluginNameInput;
48
51
49
52
protected $ item_type = 'plugin ' ;
@@ -66,13 +69,6 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
66
69
'auto_update ' ,
67
70
);
68
71
69
- /**
70
- * Plugin fetcher instance.
71
- *
72
- * @var \WP_CLI\Fetchers\Plugin
73
- */
74
- protected $ fetcher ;
75
-
76
72
public function __construct () {
77
73
require_once ABSPATH . 'wp-admin/includes/plugin.php ' ;
78
74
require_once ABSPATH . 'wp-admin/includes/plugin-install.php ' ;
@@ -209,6 +205,9 @@ public function search( $args, $assoc_args ) {
209
205
}
210
206
211
207
protected function status_single ( $ args ) {
208
+ /**
209
+ * @var object{name: string, file: string} $plugin
210
+ */
212
211
$ plugin = $ this ->fetcher ->get_check ( $ args [0 ] );
213
212
$ file = $ plugin ->file ;
214
213
@@ -345,11 +344,18 @@ protected function get_all_items() {
345
344
* Plugin 'bbpress' network activated.
346
345
* Plugin 'buddypress' network activated.
347
346
* Success: Activated 2 of 2 plugins.
347
+ *
348
+ * @param array $args
349
+ * @param array $assoc_args
348
350
*/
349
- public function activate ( $ args , $ assoc_args = array () ) {
351
+ public function activate ( $ args , $ assoc_args = [] ) {
350
352
$ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' , false );
351
353
$ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
352
- $ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' );
354
+ $ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , '' );
355
+
356
+ /**
357
+ * @var string $all_exclude
358
+ */
353
359
354
360
$ args = $ this ->check_optional_args_and_all ( $ args , $ all , 'activate ' , $ all_exclude );
355
361
if ( ! $ args ) {
@@ -358,7 +364,11 @@ public function activate( $args, $assoc_args = array() ) {
358
364
359
365
$ successes = 0 ;
360
366
$ errors = 0 ;
361
- $ plugins = $ this ->fetcher ->get_many ( $ args );
367
+
368
+ /**
369
+ * @var array<object{name: string, file: string}> $plugins
370
+ */
371
+ $ plugins = $ this ->fetcher ->get_many ( $ args );
362
372
if ( count ( $ plugins ) < count ( $ args ) ) {
363
373
$ errors = count ( $ args ) - count ( $ plugins );
364
374
}
@@ -387,7 +397,7 @@ public function activate( $args, $assoc_args = array() ) {
387
397
388
398
if ( is_wp_error ( $ result ) ) {
389
399
$ message = $ result ->get_error_message ();
390
- $ message = preg_replace ( '/<a\s[^>]+>.*<\/a>/im ' , '' , $ message );
400
+ $ message = ( string ) preg_replace ( '/<a\s[^>]+>.*<\/a>/im ' , '' , $ message );
391
401
$ message = wp_strip_all_tags ( $ message );
392
402
$ message = str_replace ( 'Error: ' , '' , $ message );
393
403
WP_CLI ::warning ( "Failed to activate plugin. {$ message }" );
@@ -437,10 +447,14 @@ public function activate( $args, $assoc_args = array() ) {
437
447
* Plugin 'ninja-forms' deactivated.
438
448
* Success: Deactivated 2 of 2 plugins.
439
449
*/
440
- public function deactivate ( $ args , $ assoc_args = array () ) {
450
+ public function deactivate ( $ args , $ assoc_args = [] ) {
441
451
$ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' );
442
452
$ disable_all = Utils \get_flag_value ( $ assoc_args , 'all ' );
443
- $ disable_all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' );
453
+ $ disable_all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , '' );
454
+
455
+ /**
456
+ * @var string $disable_all_exclude
457
+ */
444
458
445
459
$ args = $ this ->check_optional_args_and_all ( $ args , $ disable_all , 'deactivate ' , $ disable_all_exclude );
446
460
if ( ! $ args ) {
@@ -530,7 +544,7 @@ public function deactivate( $args, $assoc_args = array() ) {
530
544
* Plugin 'akismet' activated.
531
545
* Success: Toggled 1 of 1 plugins.
532
546
*/
533
- public function toggle ( $ args , $ assoc_args = array () ) {
547
+ public function toggle ( $ args , $ assoc_args ) {
534
548
$ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' );
535
549
536
550
$ successes = 0 ;
@@ -574,6 +588,9 @@ public function path( $args, $assoc_args ) {
574
588
$ path = untrailingslashit ( WP_PLUGIN_DIR );
575
589
576
590
if ( ! empty ( $ args ) ) {
591
+ /**
592
+ * @var object{name: string, file: string} $plugin
593
+ */
577
594
$ plugin = $ this ->fetcher ->get_check ( $ args [0 ] );
578
595
$ path .= '/ ' . $ plugin ->file ;
579
596
@@ -591,6 +608,9 @@ protected function install_from_repo( $slug, $assoc_args ) {
591
608
list ($ wp_core_version ) = explode ( '- ' , $ wp_version );
592
609
$ wp_core_version = implode ( '. ' , array_slice ( explode ( '. ' , $ wp_core_version ), 0 , 2 ) );
593
610
611
+ /**
612
+ * @var \WP_Error|PluginInformation $api
613
+ */
594
614
$ api = plugins_api ( 'plugin_information ' , array ( 'slug ' => $ slug ) );
595
615
596
616
if ( is_wp_error ( $ api ) ) {
@@ -755,6 +775,9 @@ protected function get_item_list() {
755
775
$ auto_updates = [];
756
776
}
757
777
778
+ /**
779
+ * @var string[] $recently_active
780
+ */
758
781
$ recently_active = is_network_admin () ? get_site_option ( 'recently_activated ' ) : get_option ( 'recently_activated ' );
759
782
760
783
if ( false === $ recently_active ) {
@@ -763,10 +786,11 @@ protected function get_item_list() {
763
786
764
787
foreach ( $ this ->get_all_plugins () as $ file => $ details ) {
765
788
$ all_update_info = $ this ->get_update_info ();
766
- $ update_info = ( isset ( $ all_update_info ->response [ $ file ] ) && null !== $ all_update_info ->response [ $ file ] ) ? (array ) $ all_update_info ->response [ $ file ] : null ;
767
- $ name = Utils \get_plugin_name ( $ file );
768
- $ wporg_info = $ this ->get_wporg_data ( $ name );
769
- $ plugin_data = get_plugin_data ( WP_PLUGIN_DIR . '/ ' . $ file , false , false );
789
+ // @phpstan-ignore notIdentical.alwaysTrue
790
+ $ update_info = ( isset ( $ all_update_info ->response [ $ file ] ) && null !== $ all_update_info ->response [ $ file ] ) ? (array ) $ all_update_info ->response [ $ file ] : null ;
791
+ $ name = Utils \get_plugin_name ( $ file );
792
+ $ wporg_info = $ this ->get_wporg_data ( $ name );
793
+ $ plugin_data = get_plugin_data ( WP_PLUGIN_DIR . '/ ' . $ file , false , false );
770
794
771
795
if ( ! isset ( $ duplicate_names [ $ name ] ) ) {
772
796
$ duplicate_names [ $ name ] = array ();
@@ -875,7 +899,7 @@ protected function get_item_list() {
875
899
876
900
if ( isset ( $ plugin_update_info ->requires ) && version_compare ( $ wp_version , $ requires , '>= ' ) ) {
877
901
$ reason = "This update requires WordPress version $ plugin_update_info ->requires , but the version installed is $ wp_version. " ;
878
- } elseif ( ! isset ( $ update_info [ ' package ' ] ) ) {
902
+ } elseif ( ! isset ( $ plugin_update_info -> package ) ) {
879
903
$ reason = 'Update file not provided. Contact author for more details ' ;
880
904
} else {
881
905
$ reason = 'Update not available ' ;
@@ -904,7 +928,7 @@ protected function get_item_list() {
904
928
*
905
929
* @param string $plugin_name The plugin slug.
906
930
*
907
- * @return string The status of the plugin, includes the last update date.
931
+ * @return array{status: string, last_updated: string|false, status?: string, last_updated?: string} The status of the plugin, includes the last update date.
908
932
*/
909
933
protected function get_wporg_data ( $ plugin_name ) {
910
934
$ data = [
@@ -947,10 +971,12 @@ protected function get_wporg_data( $plugin_name ) {
947
971
$ r_body = wp_remote_retrieve_body ( $ request );
948
972
if ( strpos ( $ r_body , 'pubDate ' ) !== false ) {
949
973
// Very raw check, not validating the format or anything else.
950
- $ xml = simplexml_load_string ( $ r_body );
951
- $ xml_pub_date = $ xml ->xpath ( '//pubDate ' );
952
- if ( $ xml_pub_date ) {
953
- $ data ['last_updated ' ] = wp_date ( 'Y-m-d ' , (string ) strtotime ( $ xml_pub_date [0 ] ) );
974
+ $ xml = simplexml_load_string ( $ r_body );
975
+ if ( false !== $ xml ) {
976
+ $ xml_pub_date = $ xml ->xpath ( '//pubDate ' );
977
+ if ( $ xml_pub_date ) {
978
+ $ data ['last_updated ' ] = wp_date ( 'Y-m-d ' , strtotime ( $ xml_pub_date [0 ] ) ?: null );
979
+ }
954
980
}
955
981
}
956
982
@@ -1115,6 +1141,9 @@ public function get( $args, $assoc_args ) {
1115
1141
'status ' ,
1116
1142
);
1117
1143
1144
+ /**
1145
+ * @var object{name: string, file: string} $plugin
1146
+ */
1118
1147
$ plugin = $ this ->fetcher ->get_check ( $ args [0 ] );
1119
1148
$ file = $ plugin ->file ;
1120
1149
@@ -1173,11 +1202,14 @@ public function get( $args, $assoc_args ) {
1173
1202
* Uninstalled and deleted 'tinymce-templates' plugin.
1174
1203
* Success: Uninstalled 2 of 2 plugins.
1175
1204
*/
1176
- public function uninstall ( $ args , $ assoc_args = array () ) {
1177
-
1205
+ public function uninstall ( $ args , $ assoc_args = [] ) {
1178
1206
$ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
1179
1207
$ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , false );
1180
1208
1209
+ /**
1210
+ * @var string $all_exclude
1211
+ */
1212
+
1181
1213
// Check if plugin names or --all is passed.
1182
1214
$ args = $ this ->check_optional_args_and_all ( $ args , $ all , 'uninstall ' , $ all_exclude );
1183
1215
if ( ! $ args ) {
@@ -1222,6 +1254,9 @@ public function uninstall( $args, $assoc_args = array() ) {
1222
1254
if ( '. ' !== $ plugin_slug && ! empty ( $ plugin_translations [ $ plugin_slug ] ) ) {
1223
1255
$ translations = $ plugin_translations [ $ plugin_slug ];
1224
1256
1257
+ /**
1258
+ * @var \WP_Filesystem_Base $wp_filesystem
1259
+ */
1225
1260
global $ wp_filesystem ;
1226
1261
require_once ABSPATH . '/wp-admin/includes/file.php ' ;
1227
1262
WP_Filesystem ();
@@ -1233,7 +1268,11 @@ public function uninstall( $args, $assoc_args = array() ) {
1233
1268
1234
1269
$ json_translation_files = glob ( WP_LANG_DIR . '/plugins/ ' . $ plugin_slug . '- ' . $ translation . '-*.json ' );
1235
1270
if ( $ json_translation_files ) {
1236
- array_map ( array ( $ wp_filesystem , 'delete ' ), $ json_translation_files );
1271
+ /**
1272
+ * @var callable $callback
1273
+ */
1274
+ $ callback = [ $ wp_filesystem , 'delete ' ];
1275
+ array_map ( $ callback , $ json_translation_files );
1237
1276
}
1238
1277
}
1239
1278
}
@@ -1257,6 +1296,10 @@ public function uninstall( $args, $assoc_args = array() ) {
1257
1296
// Remove deleted plugins from the plugin updates list.
1258
1297
$ current = get_site_transient ( $ this ->upgrade_transient );
1259
1298
if ( $ current ) {
1299
+ /**
1300
+ * @var object{response: array<string, mixed>, checked: array<string, mixed>}&\stdClass $current
1301
+ */
1302
+
1260
1303
// Don't remove the plugins that weren't deleted.
1261
1304
$ deleted = array_diff ( $ deleted_plugin_files , $ delete_errors );
1262
1305
@@ -1292,7 +1335,7 @@ public function uninstall( $args, $assoc_args = array() ) {
1292
1335
*
1293
1336
* @subcommand is-installed
1294
1337
*/
1295
- public function is_installed ( $ args , $ assoc_args = array () ) {
1338
+ public function is_installed ( $ args , $ assoc_args ) {
1296
1339
if ( $ this ->fetcher ->get ( $ args [0 ] ) ) {
1297
1340
WP_CLI ::halt ( 0 );
1298
1341
} else {
@@ -1322,7 +1365,7 @@ public function is_installed( $args, $assoc_args = array() ) {
1322
1365
*
1323
1366
* @subcommand is-active
1324
1367
*/
1325
- public function is_active ( $ args , $ assoc_args = array () ) {
1368
+ public function is_active ( $ args , $ assoc_args ) {
1326
1369
$ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' );
1327
1370
1328
1371
$ plugin = $ this ->fetcher ->get ( $ args [0 ] );
@@ -1366,10 +1409,14 @@ public function is_active( $args, $assoc_args = array() ) {
1366
1409
* Deleted 'tinymce-templates' plugin.
1367
1410
* Success: Deleted 2 of 2 plugins.
1368
1411
*/
1369
- public function delete ( $ args , $ assoc_args = array () ) {
1412
+ public function delete ( $ args , $ assoc_args ) {
1370
1413
$ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
1371
1414
$ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , false );
1372
1415
1416
+ /**
1417
+ * @var string $all_exclude
1418
+ */
1419
+
1373
1420
// Check if plugin names or --all is passed.
1374
1421
$ args = $ this ->check_optional_args_and_all ( $ args , $ all , 'delete ' , $ all_exclude );
1375
1422
if ( ! $ args ) {
@@ -1505,7 +1552,11 @@ public function delete( $args, $assoc_args = array() ) {
1505
1552
* @subcommand list
1506
1553
*/
1507
1554
public function list_ ( $ _ , $ assoc_args ) {
1555
+ /**
1556
+ * @var string $fields
1557
+ */
1508
1558
$ fields = Utils \get_flag_value ( $ assoc_args , 'fields ' );
1559
+
1509
1560
if ( ! empty ( $ fields ) ) {
1510
1561
$ fields = explode ( ', ' , $ fields );
1511
1562
$ this ->check_wporg ['status ' ] = in_array ( 'wporg_status ' , $ fields , true );
@@ -1578,7 +1629,7 @@ private static function get_template_path( $template ) {
1578
1629
/**
1579
1630
* Gets the details of a plugin.
1580
1631
*
1581
- * @param object
1632
+ * @param string $file Plugin file name.
1582
1633
* @return array
1583
1634
*/
1584
1635
private function get_details ( $ file ) {
@@ -1591,8 +1642,8 @@ private function get_details( $file ) {
1591
1642
/**
1592
1643
* Performs deletion of plugin files
1593
1644
*
1594
- * @param $plugin - Plugin fetcher object (name, file)
1595
- * @return bool - If plugin was deleted
1645
+ * @param $plugin Plugin fetcher object (name, file)
1646
+ * @return bool Whether plugin was deleted
1596
1647
*/
1597
1648
private function delete_plugin ( $ plugin ) {
1598
1649
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
0 commit comments