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

236 lines 4.9 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 $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 ( ! property_exists( $record, $field ) ) {
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_json_encode( $formatted_records ) );
158 }
159
160 if ( 'json_pretty' === $assoc_args['format'] ) {
161 \WP_CLI::line( wp_json_encode( $formatted_records, JSON_PRETTY_PRINT ) );
162 }
163
164 if ( 'csv' === $assoc_args['format'] ) {
165 \WP_CLI::line( $this->csv_format( $formatted_records ) );
166 }
167
168 return;
169 }
170
171 $formatter = new \WP_CLI\Formatter(
172 $assoc_args,
173 $fields
174 );
175
176 $formatter->display_items( $formatted_records );
177 }
178
179 /**
180 * Convert any field to a flat array.
181 *
182 * @param string $name The output array element name.
183 * @param mixed $value Any value to be converted to an array.
184 *
185 * @return array The flat array
186 */
187 private function format_field( $name, $value ) {
188 $array = array();
189
190 if ( is_object( $value ) ) {
191 foreach ( $value as $key => $property ) {
192 $array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) );
193 }
194 } elseif ( is_array( $value ) ) {
195 $array[ $name ] = $value[0];
196 } else {
197 $array[ $name ] = $value;
198 }
199
200 return $array;
201 }
202
203 /**
204 * Convert an array of flat records to CSV
205 *
206 * @param array $records The input array of records.
207 */
208 private function csv_format( $records ) {
209 $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output
210
211 foreach ( $records as $line ) {
212 fputcsv( $output, $line );
213 }
214
215 fclose( $output ); // @codingStandardsIgnoreLine
216 }
217
218 /**
219 * Checks for a Stream connection and displays an error or success message.
220 *
221 * @return void
222 */
223 private function connection() {
224 $query = wp_stream_get_instance()->db->query(
225 array(
226 'records_per_page' => 1,
227 'fields' => 'created',
228 )
229 );
230
231 if ( ! $query ) {
232 \WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) );
233 }
234 }
235 }
236