woocommerce/admin/includes/class-wc-csv-exporter.php

131 lines
2.2 KiB
PHP
Raw Normal View History

2012-09-10 11:26:26 +00:00
<?php
/**
* WC_CSV_Exporter class.
*
* Takes any data and turns it into a CSV for output.
*
* @class WC_Cart
* @version 1.6.4
* @package WooCommerce/Classes
* @author WooThemes
*/
2012-11-27 16:22:47 +00:00
2012-10-15 10:32:24 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2012-09-10 11:26:26 +00:00
class WC_CSV_Exporter {
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
/**
* @var mixed
* @access private
*/
private $_csv = '';
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
/**
* @var string
* @access private
*/
private $_filename = '';
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
/**
* @var string
* @access private
*/
private $_output = true;
/**
* __construct function.
2012-11-27 16:22:47 +00:00
*
2012-09-10 11:26:26 +00:00
* @access public
* @param bool $output (default: true)
2012-11-27 16:22:47 +00:00
* @param mixed $filename
2012-09-10 11:26:26 +00:00
* @return void
*/
function __construct( $columns = array(), $output = true, $filename = '' ) {
$this->_csv = '';
$this->_filename = $filename ? $filename : 'export.csv';
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
if ( $this->_output = $output )
$this->start();
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
if ( $columns )
$this->set_columns( $columns );
}
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
function set_filename( $filename ) {
$this->_filename = $filename ? $filename : 'export.csv';
}
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
/**
* set_columns function.
2012-11-27 16:22:47 +00:00
*
2012-09-10 11:26:26 +00:00
* @access public
* @return void
*/
function set_columns( $columns = array() ) {
2012-11-27 16:22:47 +00:00
$this->add_row( $columns );
2012-09-10 11:26:26 +00:00
unset( $columns );
}
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
/**
* add_row function.
2012-11-27 16:22:47 +00:00
*
2012-09-10 11:26:26 +00:00
* @access public
* @return void
*/
function add_row( $row ) {
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
$row = implode( ',', array_map( array( &$this, 'wrap_column' ), $row ) ) . "\n";
2012-11-27 16:22:47 +00:00
if ( $this->_output )
2012-09-10 11:26:26 +00:00
fwrite( $this->_csv, $row );
else
$this->_csv += $row;
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
unset( $row );
}
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
/**
* start function.
2012-11-27 16:22:47 +00:00
*
2012-09-10 11:26:26 +00:00
* @access public
* @return void
*/
function start() {
if ( headers_sent() )
wp_die( 'Headers already sent' );
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
@set_time_limit(0);
@ob_clean();
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
header( "Content-Type: text/csv; charset=UTF-8" );
header( "Content-Disposition: attachment; filename={$this->_filename}" );
header( "Pragma: no-cache" );
header( "Expires: 0" );
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
$this->_csv = fopen( 'php://output', 'w') ;
}
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
/**
* end function.
2012-11-27 16:22:47 +00:00
*
2012-09-10 11:26:26 +00:00
* @access public
* @return void
*/
function end() {
fclose( $this->_csv );
exit;
}
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
/**
* wrap_column function.
2012-11-27 16:22:47 +00:00
*
2012-09-10 11:26:26 +00:00
* @access public
* @param mixed $data
* @return void
*/
function wrap_column( $data ) {
return '"' . str_replace( '"', '""', $data ) . '"';
}
2012-11-27 16:22:47 +00:00
2012-09-10 11:26:26 +00:00
}