| @@ -89,10 +89,10 @@ | ||
| 89 | 89 | * wp stream query --user_role__not_in=administrator --date_after=2015-01-01T12:00:00 |
| 90 | 90 | * wp stream query --user_id=1 --action=login --records_per_page=50 --fields=created |
| 91 | 91 | * |
| 92 | 92 | * @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 | |
| 93 | + * @see https://github.com/wp-stream/stream/wiki/WP-CLI-Command | |
| 94 | + * @see https://github.com/wp-stream/stream/wiki/Query-Reference | |
| 95 | 95 | * |
| 96 | 96 | * @param array $args Unused. |
| 97 | 97 | * @param array $assoc_args Fields to return data for. |
| 98 | 98 | */ |
| @@ -123,11 +123,9 @@ | ||
| 123 | 123 | |
| 124 | 124 | $query_args[ $key ] = $value; |
| 125 | 125 | } |
| 126 | 126 | |
| 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; | |
| 127 | + $query_args['fields'] = implode( ',', $fields ); | |
| 130 | 128 | |
| 131 | 129 | $records = wp_stream_get_instance()->db->query( $query_args ); |
| 132 | 130 | |
| 133 | 131 | // Make structure Formatter compatible. |
| @@ -135,9 +133,9 @@ | ||
| 135 | 133 | $formatted_records[ $key ] = array(); |
| 136 | 134 | |
| 137 | 135 | // Catch any fields missing in records. |
| 138 | 136 | foreach ( $fields as $field ) { |
| 139 | - if ( ! property_exists( $record, $field ) ) { | |
| 137 | + if ( ! array_key_exists( $field, $record ) ) { | |
| 140 | 138 | $record->$field = null; |
| 141 | 139 | } |
| 142 | 140 | } |
| 143 | 141 | |
| @@ -155,13 +153,17 @@ | ||
| 155 | 153 | \WP_CLI::line( count( $records ) ); |
| 156 | 154 | } |
| 157 | 155 | |
| 158 | 156 | if ( 'json' === $assoc_args['format'] ) { |
| 159 | - \WP_CLI::line( wp_json_encode( $formatted_records ) ); | |
| 157 | + \WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); | |
| 160 | 158 | } |
| 161 | 159 | |
| 162 | 160 | if ( 'json_pretty' === $assoc_args['format'] ) { |
| 163 | - \WP_CLI::line( wp_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); | |
| 161 | + if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { | |
| 162 | + \WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); // xss ok. | |
| 163 | + } else { | |
| 164 | + \WP_CLI::line( wp_stream_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); // xss ok. | |
| 165 | + } | |
| 164 | 166 | } |
| 165 | 167 | |
| 166 | 168 | if ( 'csv' === $assoc_args['format'] ) { |
| 167 | 169 | \WP_CLI::line( $this->csv_format( $formatted_records ) ); |
| @@ -180,24 +182,24 @@ | ||
| 180 | 182 | |
| 181 | 183 | /** |
| 182 | 184 | * Convert any field to a flat array. |
| 183 | 185 | * |
| 184 | - * @param string $name The output array element name. | |
| 185 | - * @param mixed $value 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. | |
| 186 | 188 | * |
| 187 | 189 | * @return array The flat array |
| 188 | 190 | */ |
| 189 | - private function format_field( $name, $value ) { | |
| 191 | + private function format_field( $name, $object ) { | |
| 190 | 192 | $array = array(); |
| 191 | 193 | |
| 192 | - if ( is_object( $value ) ) { | |
| 193 | - foreach ( $value as $key => $property ) { | |
| 194 | + if ( is_object( $object ) ) { | |
| 195 | + foreach ( $object as $key => $property ) { | |
| 194 | 196 | $array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) ); |
| 195 | 197 | } |
| 196 | - } elseif ( is_array( $value ) ) { | |
| 197 | - $array[ $name ] = $value[0]; | |
| 198 | + } elseif ( is_array( $object ) ) { | |
| 199 | + $array[ $name ] = $object[0]; | |
| 198 | 200 | } else { |
| 199 | - $array[ $name ] = $value; | |
| 201 | + $array[ $name ] = $object; | |
| 200 | 202 | } |
| 201 | 203 | |
| 202 | 204 | return $array; |
| 203 | 205 | } |
| @@ -204,15 +206,15 @@ | ||
| 204 | 206 | |
| 205 | 207 | /** |
| 206 | 208 | * Convert an array of flat records to CSV |
| 207 | 209 | * |
| 208 | - * @param array $records The input array of records. | |
| 210 | + * @param array $array The input array of records. | |
| 209 | 211 | */ |
| 210 | - private function csv_format( $records ) { | |
| 212 | + private function csv_format( $array ) { | |
| 211 | 213 | $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output |
| 212 | 214 | |
| 213 | - foreach ( $records as $line ) { | |
| 214 | - fputcsv( $output, $line, ',', '"', '\\' ); | |
| 215 | + foreach ( $array as $line ) { | |
| 216 | + fputcsv( $output, $line ); // @codingStandardsIgnoreLine | |
| 215 | 217 | } |
| 216 | 218 | |
| 217 | 219 | fclose( $output ); // @codingStandardsIgnoreLine |
| 218 | 220 | } |
| @@ -222,11 +224,9 @@ | ||
| 222 | 224 | * |
| 223 | 225 | * @return void |
| 224 | 226 | */ |
| 225 | 227 | private function connection() { |
| 226 | - global $wpdb; | |
| 227 | - | |
| 228 | - wp_stream_get_instance()->db->query( | |
| 228 | + $query = wp_stream_get_instance()->db->query( | |
| 229 | 229 | array( |
| 230 | 230 | 'records_per_page' => 1, |
| 231 | 231 | 'fields' => 'created', |
| 232 | 232 | ) |
| @@ -231,12 +231,9 @@ | ||
| 231 | 231 | 'fields' => 'created', |
| 232 | 232 | ) |
| 233 | 233 | ); |
| 234 | 234 | |
| 235 | - // An empty result set is valid (e.g. a fresh site with no logged | |
| 236 | - // activity yet); only a genuine database error means the site is | |
| 237 | - // disconnected. | |
| 238 | - if ( ! empty( $wpdb->last_error ) ) { | |
| 235 | + if ( ! $query ) { | |
| 239 | 236 | \WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) ); |
| 240 | 237 | } |
| 241 | 238 | } |
| 242 | 239 | } |