| @@ -2,12 +2,16 @@ | ||
| 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 | */ |
| 7 | 8 | |
| 8 | 9 | namespace WP_Stream; |
| 9 | 10 | |
| 11 | +/** | |
| 12 | + * Class - CLI | |
| 13 | + */ | |
| 10 | 14 | class CLI extends \WP_CLI_Command { |
| 11 | 15 | |
| 12 | 16 | /** |
| 13 | 17 | * Query a set of Stream records. |
| @@ -85,10 +89,13 @@ | ||
| 85 | 89 | * wp stream query --user_role__not_in=administrator --date_after=2015-01-01T12:00:00 |
| 86 | 90 | * wp stream query --user_id=1 --action=login --records_per_page=50 --fields=created |
| 87 | 91 | * |
| 88 | 92 | * @see WP_Stream_Query |
| 89 | - * @see https://github.com/wp-stream/stream/wiki/WP-CLI-Command | |
| 90 | - * @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. | |
| 91 | 98 | */ |
| 92 | 99 | public function query( $args, $assoc_args ) { |
| 93 | 100 | unset( $args ); |
| 94 | 101 | |
| @@ -120,15 +127,15 @@ | ||
| 120 | 127 | $query_args['fields'] = implode( ',', $fields ); |
| 121 | 128 | |
| 122 | 129 | $records = wp_stream_get_instance()->db->query( $query_args ); |
| 123 | 130 | |
| 124 | - // Make structure Formatter compatible | |
| 131 | + // Make structure Formatter compatible. | |
| 125 | 132 | foreach ( (array) $records as $key => $record ) { |
| 126 | 133 | $formatted_records[ $key ] = array(); |
| 127 | 134 | |
| 128 | - // Catch any fields missing in records | |
| 135 | + // Catch any fields missing in records. | |
| 129 | 136 | foreach ( $fields as $field ) { |
| 130 | - if ( ! array_key_exists( $field, $record ) ) { | |
| 137 | + if ( ! property_exists( $record, $field ) ) { | |
| 131 | 138 | $record->$field = null; |
| 132 | 139 | } |
| 133 | 140 | } |
| 134 | 141 | |
| @@ -146,17 +153,13 @@ | ||
| 146 | 153 | \WP_CLI::line( count( $records ) ); |
| 147 | 154 | } |
| 148 | 155 | |
| 149 | 156 | if ( 'json' === $assoc_args['format'] ) { |
| 150 | - \WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); | |
| 157 | + \WP_CLI::line( wp_json_encode( $formatted_records ) ); | |
| 151 | 158 | } |
| 152 | 159 | |
| 153 | 160 | if ( 'json_pretty' === $assoc_args['format'] ) { |
| 154 | - if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { | |
| 155 | - \WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); // xss ok | |
| 156 | - } else { | |
| 157 | - \WP_CLI::line( wp_stream_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); // xss ok | |
| 158 | - } | |
| 161 | + \WP_CLI::line( wp_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); | |
| 159 | 162 | } |
| 160 | 163 | |
| 161 | 164 | if ( 'csv' === $assoc_args['format'] ) { |
| 162 | 165 | \WP_CLI::line( $this->csv_format( $formatted_records ) ); |
| @@ -175,24 +178,24 @@ | ||
| 175 | 178 | |
| 176 | 179 | /** |
| 177 | 180 | * Convert any field to a flat array. |
| 178 | 181 | * |
| 179 | - * @param string $name The output array element name | |
| 180 | - * @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. | |
| 181 | 184 | * |
| 182 | 185 | * @return array The flat array |
| 183 | 186 | */ |
| 184 | - private function format_field( $name, $object ) { | |
| 187 | + private function format_field( $name, $value ) { | |
| 185 | 188 | $array = array(); |
| 186 | 189 | |
| 187 | - if ( is_object( $object ) ) { | |
| 188 | - foreach ( $object as $key => $property ) { | |
| 190 | + if ( is_object( $value ) ) { | |
| 191 | + foreach ( $value as $key => $property ) { | |
| 189 | 192 | $array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) ); |
| 190 | 193 | } |
| 191 | - } elseif ( is_array( $object ) ) { | |
| 192 | - $array[ $name ] = $object[0]; | |
| 194 | + } elseif ( is_array( $value ) ) { | |
| 195 | + $array[ $name ] = $value[0]; | |
| 193 | 196 | } else { |
| 194 | - $array[ $name ] = $object; | |
| 197 | + $array[ $name ] = $value; | |
| 195 | 198 | } |
| 196 | 199 | |
| 197 | 200 | return $array; |
| 198 | 201 | } |
| @@ -199,17 +202,15 @@ | ||
| 199 | 202 | |
| 200 | 203 | /** |
| 201 | 204 | * Convert an array of flat records to CSV |
| 202 | 205 | * |
| 203 | - * @param array $array The input array of records | |
| 204 | - * | |
| 205 | - * @return string The CSV output | |
| 206 | + * @param array $records The input array of records. | |
| 206 | 207 | */ |
| 207 | - private function csv_format( $array ) { | |
| 208 | + private function csv_format( $records ) { | |
| 208 | 209 | $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output |
| 209 | 210 | |
| 210 | - foreach ( $array as $line ) { | |
| 211 | - fputcsv( $output, $line ); // @codingStandardsIgnoreLine | |
| 211 | + foreach ( $records as $line ) { | |
| 212 | + fputcsv( $output, $line ); | |
| 212 | 213 | } |
| 213 | 214 | |
| 214 | 215 | fclose( $output ); // @codingStandardsIgnoreLine |
| 215 | 216 | } |