| 1 |
<?php |
| 2 |
/** |
| 3 |
* CSV Exporter |
| 4 |
* |
| 5 |
* @package WP_Stream |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Stream; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class - Exporter_CSV |
| 12 |
*/ |
| 13 |
class Exporter_CSV extends Exporter { |
| 14 |
/** |
| 15 |
* Exporter name |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
public $name = 'CSV'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Exporter slug |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public $slug = 'csv'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Outputs CSV data for download |
| 30 |
* |
| 31 |
* @param array $data Array of data to output. |
| 32 |
* @param array $columns Column names included in data set. |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function output_file( $data, $columns ) { |
| 36 |
if ( ! defined( 'WP_STREAM_TESTS' ) || ( defined( 'WP_STREAM_TESTS' ) && ! WP_STREAM_TESTS ) ) { |
| 37 |
header( 'Content-type: text/csv' ); |
| 38 |
header( 'Content-Disposition: attachment; filename="stream.csv"' ); |
| 39 |
} |
| 40 |
|
| 41 |
$output = join( ',', array_values( $columns ) ) . "\n"; |
| 42 |
foreach ( $data as $row ) { |
| 43 |
$output .= join( ',', $row ) . "\n"; |
| 44 |
} |
| 45 |
|
| 46 |
echo $output; // @codingStandardsIgnoreLine text-only output |
| 47 |
if ( ! defined( 'WP_STREAM_TESTS' ) || ( defined( 'WP_STREAM_TESTS' ) && ! WP_STREAM_TESTS ) ) { |
| 48 |
exit; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|