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

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