@@ -904,6 +904,24 @@ public function tables( $args, $assoc_args ) {
904
904
* [--all-tables]
905
905
* : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix.
906
906
*
907
+ * [--order=<order>]
908
+ * : Ascending or Descending order.
909
+ * ---
910
+ * default: asc
911
+ * options:
912
+ * - asc
913
+ * - desc
914
+ * ---
915
+ *
916
+ * [--orderby=<orderby>]
917
+ * : Order by fields.
918
+ * ---
919
+ * default: name
920
+ * options:
921
+ * - name
922
+ * - size
923
+ * ---
924
+ *
907
925
* ## EXAMPLES
908
926
*
909
927
* $ wp db size
@@ -943,7 +961,6 @@ public function tables( $args, $assoc_args ) {
943
961
* @when after_wp_load
944
962
*/
945
963
public function size ( $ args , $ assoc_args ) {
946
-
947
964
global $ wpdb ;
948
965
949
966
$ format = Utils \get_flag_value ( $ assoc_args , 'format ' );
@@ -953,6 +970,8 @@ public function size( $args, $assoc_args ) {
953
970
$ tables = ! empty ( $ tables );
954
971
$ all_tables = Utils \get_flag_value ( $ assoc_args , 'all-tables ' );
955
972
$ all_tables_with_prefix = Utils \get_flag_value ( $ assoc_args , 'all-tables-with-prefix ' );
973
+ $ order = Utils \get_flag_value ( $ assoc_args , 'order ' , 'asc ' );
974
+ $ orderby = Utils \get_flag_value ( $ assoc_args , 'orderby ' , null );
956
975
957
976
if ( ! is_null ( $ size_format ) && $ human_readable ) {
958
977
WP_CLI ::error ( 'Cannot use --size_format and --human-readable arguments at the same time. ' );
@@ -989,8 +1008,9 @@ public function size( $args, $assoc_args ) {
989
1008
990
1009
// Add the table size to the list.
991
1010
$ rows [] = [
992
- 'Name ' => $ table_name ,
993
- 'Size ' => strtoupper ( $ table_bytes ) . $ default_unit ,
1011
+ 'Name ' => $ table_name ,
1012
+ 'Size ' => strtoupper ( $ table_bytes ) . $ default_unit ,
1013
+ 'Bytes ' => strtoupper ( $ table_bytes ),
994
1014
];
995
1015
}
996
1016
} else {
@@ -1005,8 +1025,9 @@ public function size( $args, $assoc_args ) {
1005
1025
1006
1026
// Add the database size to the list.
1007
1027
$ rows [] = [
1008
- 'Name ' => DB_NAME ,
1009
- 'Size ' => strtoupper ( $ db_bytes ) . $ default_unit ,
1028
+ 'Name ' => DB_NAME ,
1029
+ 'Size ' => strtoupper ( $ db_bytes ) . $ default_unit ,
1030
+ 'Bytes ' => strtoupper ( $ db_bytes ),
1010
1031
];
1011
1032
}
1012
1033
@@ -1088,6 +1109,25 @@ public function size( $args, $assoc_args ) {
1088
1109
if ( ! empty ( $ size_format ) && ! $ tables && ! $ format && ! $ human_readable && true !== $ all_tables && true !== $ all_tables_with_prefix ) {
1089
1110
WP_CLI ::line ( str_replace ( " {$ size_format_display }" , '' , $ rows [0 ]['Size ' ] ) );
1090
1111
} else {
1112
+
1113
+ // Sort the rows by user input
1114
+ if ( $ orderby ) {
1115
+ usort (
1116
+ $ rows ,
1117
+ function ( $ a , $ b ) use ( $ order , $ orderby ) {
1118
+
1119
+ $ orderby_array = 'asc ' === $ order ? array ( $ a , $ b ) : array ( $ b , $ a );
1120
+ list ( $ first , $ second ) = $ orderby_array ;
1121
+
1122
+ if ( 'size ' === $ orderby ) {
1123
+ return $ first ['Bytes ' ] > $ second ['Bytes ' ];
1124
+ }
1125
+
1126
+ return strcmp ( $ first ['Name ' ], $ second ['Name ' ] );
1127
+ }
1128
+ );
1129
+ }
1130
+
1091
1131
// Display the rows.
1092
1132
$ args = [
1093
1133
'format ' => $ format ,
0 commit comments