| @@ -133,9 +133,9 @@ | ||
| 133 | 133 | $formatted_records[ $key ] = array(); |
| 134 | 134 | |
| 135 | 135 | // Catch any fields missing in records. |
| 136 | 136 | foreach ( $fields as $field ) { |
| 137 | - if ( ! array_key_exists( $field, $record ) ) { | |
| 137 | + if ( ! property_exists( $record, $field ) ) { | |
| 138 | 138 | $record->$field = null; |
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | |
| @@ -153,17 +153,13 @@ | ||
| 153 | 153 | \WP_CLI::line( count( $records ) ); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | if ( 'json' === $assoc_args['format'] ) { |
| 157 | - \WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); | |
| 157 | + \WP_CLI::line( wp_json_encode( $formatted_records ) ); | |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | if ( 'json_pretty' === $assoc_args['format'] ) { |
| 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 | - } | |
| 161 | + \WP_CLI::line( wp_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); | |
| 166 | 162 | } |
| 167 | 163 | |
| 168 | 164 | if ( 'csv' === $assoc_args['format'] ) { |
| 169 | 165 | \WP_CLI::line( $this->csv_format( $formatted_records ) ); |
| @@ -182,24 +178,24 @@ | ||
| 182 | 178 | |
| 183 | 179 | /** |
| 184 | 180 | * Convert any field to a flat array. |
| 185 | 181 | * |
| 186 | - * @param string $name The output array element name. | |
| 187 | - * @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. | |
| 188 | 184 | * |
| 189 | 185 | * @return array The flat array |
| 190 | 186 | */ |
| 191 | - private function format_field( $name, $object ) { | |
| 187 | + private function format_field( $name, $value ) { | |
| 192 | 188 | $array = array(); |
| 193 | 189 | |
| 194 | - if ( is_object( $object ) ) { | |
| 195 | - foreach ( $object as $key => $property ) { | |
| 190 | + if ( is_object( $value ) ) { | |
| 191 | + foreach ( $value as $key => $property ) { | |
| 196 | 192 | $array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) ); |
| 197 | 193 | } |
| 198 | - } elseif ( is_array( $object ) ) { | |
| 199 | - $array[ $name ] = $object[0]; | |
| 194 | + } elseif ( is_array( $value ) ) { | |
| 195 | + $array[ $name ] = $value[0]; | |
| 200 | 196 | } else { |
| 201 | - $array[ $name ] = $object; | |
| 197 | + $array[ $name ] = $value; | |
| 202 | 198 | } |
| 203 | 199 | |
| 204 | 200 | return $array; |
| 205 | 201 | } |
| @@ -206,15 +202,15 @@ | ||
| 206 | 202 | |
| 207 | 203 | /** |
| 208 | 204 | * Convert an array of flat records to CSV |
| 209 | 205 | * |
| 210 | - * @param array $array The input array of records. | |
| 206 | + * @param array $records The input array of records. | |
| 211 | 207 | */ |
| 212 | - private function csv_format( $array ) { | |
| 208 | + private function csv_format( $records ) { | |
| 213 | 209 | $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output |
| 214 | 210 | |
| 215 | - foreach ( $array as $line ) { | |
| 216 | - fputcsv( $output, $line ); // @codingStandardsIgnoreLine | |
| 211 | + foreach ( $records as $line ) { | |
| 212 | + fputcsv( $output, $line ); | |
| 217 | 213 | } |
| 218 | 214 | |
| 219 | 215 | fclose( $output ); // @codingStandardsIgnoreLine |
| 220 | 216 | } |