Merge pull request #24759 from rafsuntaskin/patch-1

Added delimiter property with filter
This commit is contained in:
Gerhard Potgieter 2019-10-08 11:23:12 +02:00 committed by GitHub
commit af01e9374d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 2 deletions

View File

@ -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.
*/
@ -110,6 +117,16 @@ abstract class WC_CSV_Exporter {
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.
*
@ -468,20 +485,23 @@ abstract class WC_CSV_Exporter {
* @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
}
}
}