Skip to content

Commit 0a84082

Browse files
Add support for --format flag, which can be table, json or json_pretty
1 parent 1377185 commit 0a84082

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

includes/wp-cli.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class WP_Stream_WP_CLI_Command extends WP_CLI_Command {
1919
* : One or more args to pass to wp_stream_query.
2020
*
2121
* [--format=<format>]
22-
* : Accepted values: table, json, csv. Default: table
22+
* : Accepted values: table, json, json_pretty. Default: table
2323
*
2424
* ## AVAILABLE FIELDS TO QUERY
2525
*
@@ -106,13 +106,29 @@ public function query( $args, $assoc_args ) {
106106
}
107107

108108
foreach ( $assoc_args as $key => $value ) {
109+
if ( 'format' === $key ) {
110+
continue;
111+
}
112+
109113
$query_args[ $key ] = $value;
110114
}
111115

112116
$query_args['fields'] = implode( ',', $fields );
113117

114118
$records = wp_stream_query( $query_args );
115119

120+
if ( isset( $assoc_args['format'] ) ) {
121+
if ( 'json' === $assoc_args['format'] ) {
122+
$output = json_encode( $records );
123+
} elseif ( 'json_pretty' === $assoc_args['format'] ) {
124+
$output = json_encode( $records, JSON_PRETTY_PRINT );
125+
}
126+
127+
echo $output . "\n";
128+
129+
return;
130+
}
131+
116132
// Make structure Formatter compatible
117133
foreach ( (array) $records as $key => $record ) {
118134
$formatted_records[ $key ] = array();
@@ -133,7 +149,7 @@ public function query( $args, $assoc_args ) {
133149
$formatter->display_items( $formatted_records );
134150

135151
if ( 0 === ( $found = count( $records ) ) ) {
136-
WP_CLI::line( 'No records found.' );
152+
WP_CLI::line( __( 'No records found.', 'stream' ) );
137153
} else {
138154
WP_CLI::line( sprintf( _n( '1 record found.', '%s records found.', $found, 'stream' ), number_format( $found ) ) );
139155
}

0 commit comments

Comments
 (0)