Skip to content

Commit

Permalink
Added delimiter property with filter
Browse files Browse the repository at this point in the history
Added support for custom delimiter woocommerce#24579
  • Loading branch information
rafsuntaskin authored Oct 3, 2019
1 parent 4d1f776 commit b3d21cb
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions includes/export/abstract-wc-csv-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ abstract class WC_CSV_Exporter {
*/
protected $columns_to_export = array();

/**
* The delimiter parameter sets the field delimiter (one character only).
*
* @var string
*/
protected $delimiter = ",";

/**
* Prepare data that will be exported.
*/
Expand Down Expand Up @@ -110,6 +117,16 @@ public function get_columns_to_export() {
return $this->columns_to_export;
}

/**
* Return the delimiter to use in CSV file
*
* @since 3.9.0
* @return String
*/
public function get_delimiter() {
return apply_filters( "woocommerce_{$this->export_type}_export_delimiter", $this->delimiter );
}

/**
* Set columns to export.
*
Expand Down Expand Up @@ -468,20 +485,23 @@ protected function implode_values( $values ) {
* @see https://bugs.php.net/bug.php?id=50686
* @see https://github.com/woocommerce/woocommerce/issues/19514
* @since 3.4.0
* @see https://github.com/woocommerce/woocommerce/issues/24579
* @since 3.9.0
* @param resource $buffer Resource we are writing to.
* @param array $export_row Row to export.
*/
protected function fputcsv( $buffer, $export_row ) {

if ( version_compare( PHP_VERSION, '5.5.4', '<' ) ) {
ob_start();
$temp = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine
fputcsv( $temp, $export_row, ",", '"' ); // @codingStandardsIgnoreLine
fputcsv( $temp, $export_row, $this->get_delimiter(), '"' ); // @codingStandardsIgnoreLine
fclose( $temp ); // @codingStandardsIgnoreLine
$row = ob_get_clean();
$row = str_replace( '\\"', '\\""', $row );
fwrite( $buffer, $row ); // @codingStandardsIgnoreLine
} else {
fputcsv( $buffer, $export_row, ",", '"', "\0" ); // @codingStandardsIgnoreLine
fputcsv( $buffer, $export_row, $this->get_delimiter(), '"', "\0" ); // @codingStandardsIgnoreLine
}
}
}

0 comments on commit b3d21cb

Please sign in to comment.