@@ -904,6 +904,24 @@ public function tables( $args, $assoc_args ) {
904904 * [--all-tables]
905905 * : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix.
906906 *
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+ *
907925 * ## EXAMPLES
908926 *
909927 * $ wp db size
@@ -943,7 +961,6 @@ public function tables( $args, $assoc_args ) {
943961 * @when after_wp_load
944962 */
945963 public function size ( $ args , $ assoc_args ) {
946-
947964 global $ wpdb ;
948965
949966 $ format = Utils \get_flag_value ( $ assoc_args , 'format ' );
@@ -953,6 +970,8 @@ public function size( $args, $assoc_args ) {
953970 $ tables = ! empty ( $ tables );
954971 $ all_tables = Utils \get_flag_value ( $ assoc_args , 'all-tables ' );
955972 $ 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 );
956975
957976 if ( ! is_null ( $ size_format ) && $ human_readable ) {
958977 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 ) {
9891008
9901009 // Add the table size to the list.
9911010 $ 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 ),
9941014 ];
9951015 }
9961016 } else {
@@ -1005,8 +1025,9 @@ public function size( $args, $assoc_args ) {
10051025
10061026 // Add the database size to the list.
10071027 $ 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 ),
10101031 ];
10111032 }
10121033
@@ -1088,6 +1109,25 @@ public function size( $args, $assoc_args ) {
10881109 if ( ! empty ( $ size_format ) && ! $ tables && ! $ format && ! $ human_readable && true !== $ all_tables && true !== $ all_tables_with_prefix ) {
10891110 WP_CLI ::line ( str_replace ( " {$ size_format_display }" , '' , $ rows [0 ]['Size ' ] ) );
10901111 } 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+
10911131 // Display the rows.
10921132 $ args = [
10931133 'format ' => $ format ,
0 commit comments