PluginProbe
Stream – Activity Log & Audit Trail / 3.9.0
Stream – Activity Log & Audit Trail v3.9.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
← All changes | classes/class-cli.php +38 -20 3.0.23.9.0 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,13 +127,13 @@
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 137 if ( ! array_key_exists( $field, $record ) ) {
123 138 $record->$field = null;
124 139 }
@@ -134,25 +149,25 @@
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_stream_json_encode( $formatted_records ) );
143 158 }
144 159
145 160 if ( 'json_pretty' === $assoc_args['format'] ) {
146 161 if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
147 - WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); // xss ok
162 + \WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); // xss ok.
148 163 } else {
149 - WP_CLI::line( wp_stream_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); // xss ok
164 + \WP_CLI::line( wp_stream_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); // xss ok.
150 165 }
151 166 }
152 167
153 168 if ( 'csv' === $assoc_args['format'] ) {
154 - WP_CLI::line( $this->csv_format( $formatted_records ) );
169 + \WP_CLI::line( $this->csv_format( $formatted_records ) );
155 170 }
156 171
157 172 return;
158 173 }
@@ -167,10 +182,10 @@
167 182
168 183 /**
169 184 * Convert any field to a flat array.
170 185 *
171 - * @param string $name The output array element name
172 - * @param mixed $object Any value to be converted to an array
186 + * @param string $name The output array element name.
187 + * @param mixed $object Any value to be converted to an array.
173 188 *
174 189 * @return array The flat array
175 190 */
176 191 private function format_field( $name, $object ) {
@@ -191,20 +206,18 @@
191 206
192 207 /**
193 208 * Convert an array of flat records to CSV
194 209 *
195 - * @param array $array The input array of records
196 - *
197 - * @return string The CSV output
210 + * @param array $array The input array of records.
198 211 */
199 212 private function csv_format( $array ) {
200 - $output = fopen( 'php://output', 'w' );
213 + $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output
201 214
202 215 foreach ( $array as $line ) {
203 - fputcsv( $output, $line );
216 + fputcsv( $output, $line ); // @codingStandardsIgnoreLine
204 217 }
205 218
206 - fclose( $output );
219 + fclose( $output ); // @codingStandardsIgnoreLine
207 220 }
208 221
209 222 /**
210 223 * Checks for a Stream connection and displays an error or success message.
@@ -211,11 +224,16 @@
211 224 *
212 225 * @return void
213 226 */
214 227 private function connection() {
215 - $query = wp_stream_get_instance()->db->query( array( 'records_per_page' => 1, 'fields' => 'created' ) );
228 + $query = wp_stream_get_instance()->db->query(
229 + array(
230 + 'records_per_page' => 1,
231 + 'fields' => 'created',
232 + )
233 + );
216 234
217 235 if ( ! $query ) {
218 - WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) );
236 + \WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) );
219 237 }
220 238 }
221 239 }