PluginProbe
Stream – Activity Log & Audit Trail / 2.0.5
Stream – Activity Log & Audit Trail v2.0.5
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 / includes / wp-cli.php

wp-cli.php in Stream – Activity Log & Audit Trail 2.0.5, at includes/wp-cli.php

224 lines 4.8 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 * @since 2.0.3
6 * @see https://github.com/wp-cli/wp-cli
7 */
8 class WP_Stream_WP_CLI_Command extends WP_CLI_Command {
9
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 * * author
29 * * author__in
30 * * author__not_in
31 * * author_role
32 * * author_role__in
33 * * author_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 * * author
68 * * author_meta.user_login
69 * * author_role
70 * * summary
71 *
72 * These fields are optionally available:
73 *
74 * * ID
75 * * site_id
76 * * blog_id
77 * * object_id
78 * * connector
79 * * context
80 * * action
81 * * author_meta
82 * * stream_meta
83 * * meta.links.self
84 * * meta.links.collection
85 * * meta.score
86 * * meta.sort
87 *
88 * ## EXAMPLES
89 *
90 * wp stream query --author_role__not_in=administrator --date_after=2015-01-01T12:00:00
91 * wp stream query --author=1 --action=login --records_per_page=50 --fields=created
92 *
93 * @see WP_Stream_Query
94 * @see https://github.com/wp-stream/stream/wiki/WP-CLI-Command
95 * @see https://github.com/wp-stream/stream/wiki/Query-Reference
96 */
97 public function query( $args, $assoc_args ) {
98 $query_args = array();
99 $formatted_records = array();
100
101 $this->connection();
102
103 if ( empty( $assoc_args['fields'] ) ) {
104 $fields = array( 'created', 'ip', 'author', 'author_meta.user_login', 'author_role', 'summary' );
105 } else {
106 $fields = explode( ',', $assoc_args['fields'] );
107 }
108
109 foreach ( $assoc_args as $key => $value ) {
110 if ( 'format' === $key ) {
111 continue;
112 }
113
114 $query_args[ $key ] = $value;
115 }
116
117 $query_args['fields'] = implode( ',', $fields );
118
119 $records = wp_stream_query( $query_args );
120
121 // Make structure Formatter compatible
122 foreach ( (array) $records as $key => $record ) {
123 $formatted_records[ $key ] = array();
124
125 // Catch any fields missing in records
126 foreach ( $fields as $field ) {
127 if ( ! array_key_exists( $field, $record ) ) {
128 $record->$field = null;
129 }
130 }
131
132 foreach ( $record as $field_name => $field ) {
133
134 $formatted_records[ $key ] = array_merge(
135 $formatted_records[ $key ],
136 $this->format_field( $field_name, $field )
137 );
138 }
139 }
140
141 if ( isset( $assoc_args['format'] ) && 'table' !== $assoc_args['format'] ) {
142 if ( 'count' === $assoc_args['format'] ) {
143 WP_CLI::line( count( $records ) );
144 }
145
146 if ( 'json' === $assoc_args['format'] ) {
147 WP_CLI::line( json_encode( $formatted_records ) );
148 }
149
150 if ( 'json_pretty' === $assoc_args['format'] ) {
151 WP_CLI::line( json_encode( $formatted_records, JSON_PRETTY_PRINT ) );
152 }
153
154 if ( 'csv' === $assoc_args['format'] ) {
155 WP_CLI::line( $this->csv_format( $formatted_records ) );
156 }
157
158 return;
159 }
160
161 $formatter = new \WP_CLI\Formatter(
162 $assoc_args,
163 $fields
164 );
165
166 $formatter->display_items( $formatted_records );
167 }
168
169 /**
170 * Convert any field to a flat array.
171 *
172 * @param string $name The output array element name
173 * @param mixed $object Any value to be converted to an array
174 *
175 * @return array The flat array
176 */
177 private function format_field( $name, $object ) {
178 $array = array();
179
180 if ( is_object( $object ) ) {
181 foreach ( $object as $key => $property ) {
182 $array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) );
183 }
184 } elseif ( is_array( $object ) ) {
185 $array[ $name ] = $object[0];
186 } else {
187 $array[ $name ] = $object;
188 }
189
190 return $array;
191 }
192
193 /**
194 * Convert an array of flat records to CSV
195 *
196 * @param array $array The input array of records
197 *
198 * @return string The CSV output
199 */
200 private function csv_format( $array ) {
201 $output = fopen( 'php://output', 'w' );
202
203 foreach ( $array as $line ) {
204 fputcsv( $output, $line );
205 }
206
207 fclose( $output );
208 }
209
210 /**
211 * Checks for a Stream connection and displays an error or success message.
212 *
213 * @return void
214 */
215 private function connection() {
216 $query = wp_stream_query( array( 'records_per_page' => 1, 'fields' => 'created' ) );
217
218 if ( ! $query ) {
219 WP_CLI::error( __( 'SITE IS DISCONNECTED', 'stream' ) );
220 }
221 }
222
223 }
224