PluginProbe
Stream – Activity Log & Audit Trail / 3.5.0
Stream – Activity Log & Audit Trail v3.5.0
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-cli.php

class-cli.php in Stream – Activity Log & Audit Trail 3.5.0, at classes/class-cli.php

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