-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathclass-cli.php
More file actions
235 lines (206 loc) · 4.93 KB
/
class-cli.php
File metadata and controls
235 lines (206 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Stream command for WP-CLI
*
* @see https://github.com/wp-cli/wp-cli
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - CLI
*/
class CLI extends \WP_CLI_Command {
/**
* Query a set of Stream records.
*
* ## OPTIONS
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--<field>=<value>]
* : One or more args to pass to WP_Stream_Query.
*
* [--format=<format>]
* : Accepted values: table, count, json, json_pretty, csv. Default: table
*
* ## AVAILABLE FIELDS TO QUERY
*
* You can build a query from these fields:
*
* * user_id
* * user_id__in
* * user_id__not_in
* * user_role
* * user_role__in
* * user_role__not_in
* * date
* * date_from
* * date_to
* * date_after
* * date_before
* * ip
* * ip__in
* * ip__not_in
* * connector
* * connector__in
* * connector__not_in
* * context
* * context__in
* * context__not_in
* * action
* * action__in
* * action__not_in
* * search
* * search_field
* * record
* * record__in
* * record__not_in
* * records_per_page
* * paged
* * order
* * orderby
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each post:
*
* * created
* * ip
* * user_id
* * user_role
* * summary
*
* These fields are optionally available:
*
* * ID
* * site_id
* * blog_id
* * object_id
* * connector
* * context
* * action
*
* ## EXAMPLES
*
* wp stream query --user_role__not_in=administrator --date_after=2015-01-01T12:00:00
* wp stream query --user_id=1 --action=login --records_per_page=50 --fields=created
*
* @see WP_Stream_Query
* @see https://github.com/xwp/stream/wiki/WP-CLI-Command
* @see https://github.com/xwp/stream/wiki/Query-Reference
*
* @param array $args Unused.
* @param array $assoc_args Fields to return data for.
*/
public function query( $args, $assoc_args ) {
unset( $args );
$query_args = array();
$formatted_records = array();
$this->connection();
if ( empty( $assoc_args['fields'] ) ) {
$fields = array(
'created',
'ip',
'user_id',
'user_role',
'summary',
);
} else {
$fields = explode( ',', $assoc_args['fields'] );
}
foreach ( $assoc_args as $key => $value ) {
if ( 'format' === $key ) {
continue;
}
$query_args[ $key ] = $value;
}
$query_args['fields'] = implode( ',', $fields );
$records = wp_stream_get_instance()->db->query( $query_args );
// Make structure Formatter compatible.
foreach ( (array) $records as $key => $record ) {
$formatted_records[ $key ] = array();
// Catch any fields missing in records.
foreach ( $fields as $field ) {
if ( ! property_exists( $record, $field ) ) {
$record->$field = null;
}
}
foreach ( $record as $field_name => $field ) {
$formatted_records[ $key ] = array_merge(
$formatted_records[ $key ],
$this->format_field( $field_name, $field )
);
}
}
if ( isset( $assoc_args['format'] ) && 'table' !== $assoc_args['format'] ) {
if ( 'count' === $assoc_args['format'] ) {
\WP_CLI::line( count( $records ) );
}
if ( 'json' === $assoc_args['format'] ) {
\WP_CLI::line( wp_json_encode( $formatted_records ) );
}
if ( 'json_pretty' === $assoc_args['format'] ) {
\WP_CLI::line( wp_json_encode( $formatted_records, JSON_PRETTY_PRINT ) );
}
if ( 'csv' === $assoc_args['format'] ) {
\WP_CLI::line( $this->csv_format( $formatted_records ) );
}
return;
}
$formatter = new \WP_CLI\Formatter(
$assoc_args,
$fields
);
$formatter->display_items( $formatted_records );
}
/**
* Convert any field to a flat array.
*
* @param string $name The output array element name.
* @param mixed $value Any value to be converted to an array.
*
* @return array The flat array
*/
private function format_field( $name, $value ) {
$array = array();
if ( is_object( $value ) ) {
foreach ( $value as $key => $property ) {
$array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) );
}
} elseif ( is_array( $value ) ) {
$array[ $name ] = $value[0];
} else {
$array[ $name ] = $value;
}
return $array;
}
/**
* Convert an array of flat records to CSV
*
* @param array $records The input array of records.
*/
private function csv_format( $records ) {
$output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output
foreach ( $records as $line ) {
fputcsv( $output, $line );
}
fclose( $output ); // @codingStandardsIgnoreLine
}
/**
* Checks for a Stream connection and displays an error or success message.
*
* @return void
*/
private function connection() {
$query = wp_stream_get_instance()->db->query(
array(
'records_per_page' => 1,
'fields' => 'created',
)
);
if ( ! $query ) {
\WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) );
}
}
}