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