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
← All changes | classes/class-cli.php +45 -31 3.2.04.0.2 View file →
@@ -2,12 +2,18 @@
2 2 /**
3 3 * Stream command for WP-CLI
4 4 *
5 5 * @see https://github.com/wp-cli/wp-cli
6 + * @package WP_Stream
6 7 */
8 +
7 9 namespace WP_Stream;
8 10
11 +/**
12 + * Class - CLI
13 + */
9 14 class CLI extends \WP_CLI_Command {
15 +
10 16 /**
11 17 * Query a set of Stream records.
12 18 *
13 19 * ## OPTIONS
@@ -83,10 +89,13 @@
83 89 * wp stream query --user_role__not_in=administrator --date_after=2015-01-01T12:00:00
84 90 * wp stream query --user_id=1 --action=login --records_per_page=50 --fields=created
85 91 *
86 92 * @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
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.
89 98 */
90 99 public function query( $args, $assoc_args ) {
91 100 unset( $args );
92 101
@@ -95,9 +104,15 @@
95 104
96 105 $this->connection();
97 106
98 107 if ( empty( $assoc_args['fields'] ) ) {
99 - $fields = array( 'created', 'ip', 'user_id', 'user_role', 'summary' );
108 + $fields = array(
109 + 'created',
110 + 'ip',
111 + 'user_id',
112 + 'user_role',
113 + 'summary',
114 + );
100 115 } else {
101 116 $fields = explode( ',', $assoc_args['fields'] );
102 117 }
103 118
@@ -112,15 +127,15 @@
112 127 $query_args['fields'] = implode( ',', $fields );
113 128
114 129 $records = wp_stream_get_instance()->db->query( $query_args );
115 130
116 - // Make structure Formatter compatible
131 + // Make structure Formatter compatible.
117 132 foreach ( (array) $records as $key => $record ) {
118 133 $formatted_records[ $key ] = array();
119 134
120 - // Catch any fields missing in records
135 + // Catch any fields missing in records.
121 136 foreach ( $fields as $field ) {
122 - if ( ! array_key_exists( $field, $record ) ) {
137 + if ( ! property_exists( $record, $field ) ) {
123 138 $record->$field = null;
124 139 }
125 140 }
126 141
@@ -134,25 +149,21 @@
134 149 }
135 150
136 151 if ( isset( $assoc_args['format'] ) && 'table' !== $assoc_args['format'] ) {
137 152 if ( 'count' === $assoc_args['format'] ) {
138 - WP_CLI::line( count( $records ) );
153 + \WP_CLI::line( count( $records ) );
139 154 }
140 155
141 156 if ( 'json' === $assoc_args['format'] ) {
142 - WP_CLI::line( wp_stream_json_encode( $formatted_records ) );
157 + \WP_CLI::line( wp_json_encode( $formatted_records ) );
143 158 }
144 159
145 160 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 - }
161 + \WP_CLI::line( wp_json_encode( $formatted_records, JSON_PRETTY_PRINT ) );
151 162 }
152 163
153 164 if ( 'csv' === $assoc_args['format'] ) {
154 - WP_CLI::line( $this->csv_format( $formatted_records ) );
165 + \WP_CLI::line( $this->csv_format( $formatted_records ) );
155 166 }
156 167
157 168 return;
158 169 }
@@ -167,24 +178,24 @@
167 178
168 179 /**
169 180 * Convert any field to a flat array.
170 181 *
171 - * @param string $name The output array element name
172 - * @param mixed $object Any value to be converted to an array
182 + * @param string $name The output array element name.
183 + * @param mixed $value Any value to be converted to an array.
173 184 *
174 185 * @return array The flat array
175 186 */
176 - private function format_field( $name, $object ) {
187 + private function format_field( $name, $value ) {
177 188 $array = array();
178 189
179 - if ( is_object( $object ) ) {
180 - foreach ( $object as $key => $property ) {
190 + if ( is_object( $value ) ) {
191 + foreach ( $value as $key => $property ) {
181 192 $array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) );
182 193 }
183 - } elseif ( is_array( $object ) ) {
184 - $array[ $name ] = $object[0];
194 + } elseif ( is_array( $value ) ) {
195 + $array[ $name ] = $value[0];
185 196 } else {
186 - $array[ $name ] = $object;
197 + $array[ $name ] = $value;
187 198 }
188 199
189 200 return $array;
190 201 }
@@ -191,20 +202,18 @@
191 202
192 203 /**
193 204 * Convert an array of flat records to CSV
194 205 *
195 - * @param array $array The input array of records
196 - *
197 - * @return string The CSV output
206 + * @param array $records The input array of records.
198 207 */
199 - private function csv_format( $array ) {
200 - $output = fopen( 'php://output', 'w' );
208 + private function csv_format( $records ) {
209 + $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output
201 210
202 - foreach ( $array as $line ) {
211 + foreach ( $records as $line ) {
203 212 fputcsv( $output, $line );
204 213 }
205 214
206 - fclose( $output );
215 + fclose( $output ); // @codingStandardsIgnoreLine
207 216 }
208 217
209 218 /**
210 219 * Checks for a Stream connection and displays an error or success message.
@@ -211,11 +220,16 @@
211 220 *
212 221 * @return void
213 222 */
214 223 private function connection() {
215 - $query = wp_stream_get_instance()->db->query( array( 'records_per_page' => 1, 'fields' => 'created' ) );
224 + $query = wp_stream_get_instance()->db->query(
225 + array(
226 + 'records_per_page' => 1,
227 + 'fields' => 'created',
228 + )
229 + );
216 230
217 231 if ( ! $query ) {
218 - WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) );
232 + \WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) );
219 233 }
220 234 }
221 235 }