PluginProbe
Stream – Activity Log & Audit Trail / 4.2.2
Stream – Activity Log & Audit Trail v4.2.2
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 4.2.2, at classes/class-cli.php

238 lines 5.1 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/xwp/stream/wiki/WP-CLI-Command
94 * @see https://github.com/xwp/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 // Pass fields as an array so the query builder can validate each
128 // column against its allowlist (column names cannot be prepared).
129 $query_args['fields'] = $fields;
130
131 $records = wp_stream_get_instance()->db->query( $query_args );
132
133 // Make structure Formatter compatible.
134 foreach ( (array) $records as $key => $record ) {
135 $formatted_records[ $key ] = array();
136
137 // Catch any fields missing in records.
138 foreach ( $fields as $field ) {
139 if ( ! property_exists( $record, $field ) ) {
140 $record->$field = null;
141 }
142 }
143
144 foreach ( $record as $field_name => $field ) {
145
146 $formatted_records[ $key ] = array_merge(
147 $formatted_records[ $key ],
148 $this->format_field( $field_name, $field )
149 );
150 }
151 }
152
153 if ( isset( $assoc_args['format'] ) && 'table' !== $assoc_args['format'] ) {
154 if ( 'count' === $assoc_args['format'] ) {
155 \WP_CLI::line( count( $records ) );
156 }
157
158 if ( 'json' === $assoc_args['format'] ) {
159 \WP_CLI::line( wp_json_encode( $formatted_records ) );
160 }
161
162 if ( 'json_pretty' === $assoc_args['format'] ) {
163 \WP_CLI::line( wp_json_encode( $formatted_records, JSON_PRETTY_PRINT ) );
164 }
165
166 if ( 'csv' === $assoc_args['format'] ) {
167 \WP_CLI::line( $this->csv_format( $formatted_records ) );
168 }
169
170 return;
171 }
172
173 $formatter = new \WP_CLI\Formatter(
174 $assoc_args,
175 $fields
176 );
177
178 $formatter->display_items( $formatted_records );
179 }
180
181 /**
182 * Convert any field to a flat array.
183 *
184 * @param string $name The output array element name.
185 * @param mixed $value Any value to be converted to an array.
186 *
187 * @return array The flat array
188 */
189 private function format_field( $name, $value ) {
190 $array = array();
191
192 if ( is_object( $value ) ) {
193 foreach ( $value as $key => $property ) {
194 $array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) );
195 }
196 } elseif ( is_array( $value ) ) {
197 $array[ $name ] = $value[0];
198 } else {
199 $array[ $name ] = $value;
200 }
201
202 return $array;
203 }
204
205 /**
206 * Convert an array of flat records to CSV
207 *
208 * @param array $records The input array of records.
209 */
210 private function csv_format( $records ) {
211 $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output
212
213 foreach ( $records as $line ) {
214 fputcsv( $output, $line );
215 }
216
217 fclose( $output ); // @codingStandardsIgnoreLine
218 }
219
220 /**
221 * Checks for a Stream connection and displays an error or success message.
222 *
223 * @return void
224 */
225 private function connection() {
226 $query = wp_stream_get_instance()->db->query(
227 array(
228 'records_per_page' => 1,
229 'fields' => 'created',
230 )
231 );
232
233 if ( ! $query ) {
234 \WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) );
235 }
236 }
237 }
238