41
41
* Success: Installed 1 of 1 plugins.
42
42
*
43
43
* @package wp-cli
44
+ *
45
+ * @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
44
46
*/
45
47
class Plugin_Command extends \WP_CLI \CommandWithUpgrade {
46
48
@@ -209,6 +211,9 @@ public function search( $args, $assoc_args ) {
209
211
}
210
212
211
213
protected function status_single ( $ args ) {
214
+ /**
215
+ * @var object{name: string, file: string} $plugin
216
+ */
212
217
$ plugin = $ this ->fetcher ->get_check ( $ args [0 ] );
213
218
$ file = $ plugin ->file ;
214
219
@@ -345,11 +350,18 @@ protected function get_all_items() {
345
350
* Plugin 'bbpress' network activated.
346
351
* Plugin 'buddypress' network activated.
347
352
* Success: Activated 2 of 2 plugins.
353
+ *
354
+ * @param array $args
355
+ * @param array $assoc_args
348
356
*/
349
357
public function activate ( $ args , $ assoc_args = array () ) {
350
- $ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' , false );
351
- $ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
352
- $ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' );
358
+ $ network_wide = (bool ) Utils \get_flag_value ( $ assoc_args , 'network ' , false );
359
+ $ all = (bool ) Utils \get_flag_value ( $ assoc_args , 'all ' , false );
360
+ $ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , '' );
361
+
362
+ /**
363
+ * @var string $all_exclude
364
+ */
353
365
354
366
$ args = $ this ->check_optional_args_and_all ( $ args , $ all , 'activate ' , $ all_exclude );
355
367
if ( ! $ args ) {
@@ -358,7 +370,11 @@ public function activate( $args, $assoc_args = array() ) {
358
370
359
371
$ successes = 0 ;
360
372
$ errors = 0 ;
361
- $ plugins = $ this ->fetcher ->get_many ( $ args );
373
+
374
+ /**
375
+ * @var array<object{name: string, file: string}> $plugins
376
+ */
377
+ $ plugins = $ this ->fetcher ->get_many ( $ args );
362
378
if ( count ( $ plugins ) < count ( $ args ) ) {
363
379
$ errors = count ( $ args ) - count ( $ plugins );
364
380
}
@@ -387,7 +403,7 @@ public function activate( $args, $assoc_args = array() ) {
387
403
388
404
if ( is_wp_error ( $ result ) ) {
389
405
$ message = $ result ->get_error_message ();
390
- $ message = preg_replace ( '/<a\s[^>]+>.*<\/a>/im ' , '' , $ message );
406
+ $ message = ( string ) preg_replace ( '/<a\s[^>]+>.*<\/a>/im ' , '' , $ message );
391
407
$ message = wp_strip_all_tags ( $ message );
392
408
$ message = str_replace ( 'Error: ' , '' , $ message );
393
409
WP_CLI ::warning ( "Failed to activate plugin. {$ message }" );
@@ -438,9 +454,13 @@ public function activate( $args, $assoc_args = array() ) {
438
454
* Success: Deactivated 2 of 2 plugins.
439
455
*/
440
456
public function deactivate ( $ args , $ assoc_args = array () ) {
441
- $ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' );
442
- $ disable_all = Utils \get_flag_value ( $ assoc_args , 'all ' );
443
- $ disable_all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' );
457
+ $ network_wide = (bool ) Utils \get_flag_value ( $ assoc_args , 'network ' );
458
+ $ disable_all = (bool ) Utils \get_flag_value ( $ assoc_args , 'all ' );
459
+ $ disable_all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , '' );
460
+
461
+ /**
462
+ * @var string $disable_all_exclude
463
+ */
444
464
445
465
$ args = $ this ->check_optional_args_and_all ( $ args , $ disable_all , 'deactivate ' , $ disable_all_exclude );
446
466
if ( ! $ args ) {
@@ -574,6 +594,9 @@ public function path( $args, $assoc_args ) {
574
594
$ path = untrailingslashit ( WP_PLUGIN_DIR );
575
595
576
596
if ( ! empty ( $ args ) ) {
597
+ /**
598
+ * @var object{name: string, file: string} $plugin
599
+ */
577
600
$ plugin = $ this ->fetcher ->get_check ( $ args [0 ] );
578
601
$ path .= '/ ' . $ plugin ->file ;
579
602
@@ -591,6 +614,9 @@ protected function install_from_repo( $slug, $assoc_args ) {
591
614
list ($ wp_core_version ) = explode ( '- ' , $ wp_version );
592
615
$ wp_core_version = implode ( '. ' , array_slice ( explode ( '. ' , $ wp_core_version ), 0 , 2 ) );
593
616
617
+ /**
618
+ * @var \WP_Error|PluginInformation $api
619
+ */
594
620
$ api = plugins_api ( 'plugin_information ' , array ( 'slug ' => $ slug ) );
595
621
596
622
if ( is_wp_error ( $ api ) ) {
@@ -726,7 +752,7 @@ protected function install_from_repo( $slug, $assoc_args ) {
726
752
* @alias upgrade
727
753
*/
728
754
public function update ( $ args , $ assoc_args ) {
729
- $ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
755
+ $ all = ( bool ) Utils \get_flag_value ( $ assoc_args , 'all ' , false );
730
756
731
757
$ args = $ this ->check_optional_args_and_all ( $ args , $ all );
732
758
if ( ! $ args ) {
@@ -755,6 +781,9 @@ protected function get_item_list() {
755
781
$ auto_updates = [];
756
782
}
757
783
784
+ /**
785
+ * @var string[] $recently_active
786
+ */
758
787
$ recently_active = is_network_admin () ? get_site_option ( 'recently_activated ' ) : get_option ( 'recently_activated ' );
759
788
760
789
if ( false === $ recently_active ) {
@@ -875,7 +904,7 @@ protected function get_item_list() {
875
904
876
905
if ( isset ( $ plugin_update_info ->requires ) && version_compare ( $ wp_version , $ requires , '>= ' ) ) {
877
906
$ reason = "This update requires WordPress version $ plugin_update_info ->requires , but the version installed is $ wp_version. " ;
878
- } elseif ( ! isset ( $ update_info [ ' package ' ] ) ) {
907
+ } elseif ( ! isset ( $ plugin_update_info -> package ) ) {
879
908
$ reason = 'Update file not provided. Contact author for more details ' ;
880
909
} else {
881
910
$ reason = 'Update not available ' ;
@@ -904,7 +933,7 @@ protected function get_item_list() {
904
933
*
905
934
* @param string $plugin_name The plugin slug.
906
935
*
907
- * @return string The status of the plugin, includes the last update date.
936
+ * @return array{status: string, last_updated: string|false, status?: string, last_updated?: string} The status of the plugin, includes the last update date.
908
937
*/
909
938
protected function get_wporg_data ( $ plugin_name ) {
910
939
$ data = [
@@ -947,10 +976,12 @@ protected function get_wporg_data( $plugin_name ) {
947
976
$ r_body = wp_remote_retrieve_body ( $ request );
948
977
if ( strpos ( $ r_body , 'pubDate ' ) !== false ) {
949
978
// 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 ] ) );
979
+ $ xml = simplexml_load_string ( $ r_body );
980
+ if ( false !== $ xml ) {
981
+ $ xml_pub_date = $ xml ->xpath ( '//pubDate ' );
982
+ if ( $ xml_pub_date ) {
983
+ $ data ['last_updated ' ] = wp_date ( 'Y-m-d ' , strtotime ( $ xml_pub_date [0 ] ) ?: null );
984
+ }
954
985
}
955
986
}
956
987
@@ -1115,6 +1146,9 @@ public function get( $args, $assoc_args ) {
1115
1146
'status ' ,
1116
1147
);
1117
1148
1149
+ /**
1150
+ * @var object{name: string, file: string} $plugin
1151
+ */
1118
1152
$ plugin = $ this ->fetcher ->get_check ( $ args [0 ] );
1119
1153
$ file = $ plugin ->file ;
1120
1154
@@ -1174,10 +1208,13 @@ public function get( $args, $assoc_args ) {
1174
1208
* Success: Uninstalled 2 of 2 plugins.
1175
1209
*/
1176
1210
public function uninstall ( $ args , $ assoc_args = array () ) {
1177
-
1178
- $ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
1211
+ $ all = (bool ) Utils \get_flag_value ( $ assoc_args , 'all ' , false );
1179
1212
$ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , false );
1180
1213
1214
+ /**
1215
+ * @var string $all_exclude
1216
+ */
1217
+
1181
1218
// Check if plugin names or --all is passed.
1182
1219
$ args = $ this ->check_optional_args_and_all ( $ args , $ all , 'uninstall ' , $ all_exclude );
1183
1220
if ( ! $ args ) {
@@ -1222,6 +1259,9 @@ public function uninstall( $args, $assoc_args = array() ) {
1222
1259
if ( '. ' !== $ plugin_slug && ! empty ( $ plugin_translations [ $ plugin_slug ] ) ) {
1223
1260
$ translations = $ plugin_translations [ $ plugin_slug ];
1224
1261
1262
+ /**
1263
+ * @var \WP_Filesystem_Base $wp_filesystem
1264
+ */
1225
1265
global $ wp_filesystem ;
1226
1266
require_once ABSPATH . '/wp-admin/includes/file.php ' ;
1227
1267
WP_Filesystem ();
@@ -1233,7 +1273,11 @@ public function uninstall( $args, $assoc_args = array() ) {
1233
1273
1234
1274
$ json_translation_files = glob ( WP_LANG_DIR . '/plugins/ ' . $ plugin_slug . '- ' . $ translation . '-*.json ' );
1235
1275
if ( $ json_translation_files ) {
1236
- array_map ( array ( $ wp_filesystem , 'delete ' ), $ json_translation_files );
1276
+ /**
1277
+ * @var callable $callback
1278
+ */
1279
+ $ callback = array ( $ wp_filesystem , 'delete ' );
1280
+ array_map ( $ callback , $ json_translation_files );
1237
1281
}
1238
1282
}
1239
1283
}
@@ -1257,6 +1301,10 @@ public function uninstall( $args, $assoc_args = array() ) {
1257
1301
// Remove deleted plugins from the plugin updates list.
1258
1302
$ current = get_site_transient ( $ this ->upgrade_transient );
1259
1303
if ( $ current ) {
1304
+ /**
1305
+ * @var object{response: array<string, mixed>, checked: array<string, mixed>}&\stdClass $current
1306
+ */
1307
+
1260
1308
// Don't remove the plugins that weren't deleted.
1261
1309
$ deleted = array_diff ( $ deleted_plugin_files , $ delete_errors );
1262
1310
@@ -1323,7 +1371,7 @@ public function is_installed( $args, $assoc_args = array() ) {
1323
1371
* @subcommand is-active
1324
1372
*/
1325
1373
public function is_active ( $ args , $ assoc_args = array () ) {
1326
- $ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' );
1374
+ $ network_wide = ( bool ) Utils \get_flag_value ( $ assoc_args , 'network ' );
1327
1375
1328
1376
$ plugin = $ this ->fetcher ->get ( $ args [0 ] );
1329
1377
@@ -1367,9 +1415,13 @@ public function is_active( $args, $assoc_args = array() ) {
1367
1415
* Success: Deleted 2 of 2 plugins.
1368
1416
*/
1369
1417
public function delete ( $ args , $ assoc_args = array () ) {
1370
- $ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
1418
+ $ all = ( bool ) Utils \get_flag_value ( $ assoc_args , 'all ' , false );
1371
1419
$ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , false );
1372
1420
1421
+ /**
1422
+ * @var string $all_exclude
1423
+ */
1424
+
1373
1425
// Check if plugin names or --all is passed.
1374
1426
$ args = $ this ->check_optional_args_and_all ( $ args , $ all , 'delete ' , $ all_exclude );
1375
1427
if ( ! $ args ) {
@@ -1505,7 +1557,11 @@ public function delete( $args, $assoc_args = array() ) {
1505
1557
* @subcommand list
1506
1558
*/
1507
1559
public function list_ ( $ _ , $ assoc_args ) {
1560
+ /**
1561
+ * @var string $fields
1562
+ */
1508
1563
$ fields = Utils \get_flag_value ( $ assoc_args , 'fields ' );
1564
+
1509
1565
if ( ! empty ( $ fields ) ) {
1510
1566
$ fields = explode ( ', ' , $ fields );
1511
1567
$ this ->check_wporg ['status ' ] = in_array ( 'wporg_status ' , $ fields , true );
@@ -1578,7 +1634,7 @@ private static function get_template_path( $template ) {
1578
1634
/**
1579
1635
* Gets the details of a plugin.
1580
1636
*
1581
- * @param object
1637
+ * @param string $file Plugin file name.
1582
1638
* @return array
1583
1639
*/
1584
1640
private function get_details ( $ file ) {
@@ -1591,8 +1647,8 @@ private function get_details( $file ) {
1591
1647
/**
1592
1648
* Performs deletion of plugin files
1593
1649
*
1594
- * @param $plugin - Plugin fetcher object (name, file)
1595
- * @return bool - If plugin was deleted
1650
+ * @param $plugin Plugin fetcher object (name, file)
1651
+ * @return bool Whether plugin was deleted
1596
1652
*/
1597
1653
private function delete_plugin ( $ plugin ) {
1598
1654
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
0 commit comments