From b3d21cb8ae3295b7e09f09725b9ba9049cb57525 Mon Sep 17 00:00:00 2001 From: Rafsun Chowdhury Date: Thu, 3 Oct 2019 08:43:04 +0600 Subject: [PATCH 1/2] Added delimiter property with filter Added support for custom delimiter #24579 --- includes/export/abstract-wc-csv-exporter.php | 24 ++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/includes/export/abstract-wc-csv-exporter.php b/includes/export/abstract-wc-csv-exporter.php index 7a37d5d1232..6c8c9eed224 100644 --- a/includes/export/abstract-wc-csv-exporter.php +++ b/includes/export/abstract-wc-csv-exporter.php @@ -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 } } } From 59c503b1c77f4cf65055465933c1f91348f92d1e Mon Sep 17 00:00:00 2001 From: Rafsun Chowdhury Date: Sat, 5 Oct 2019 19:47:16 +0600 Subject: [PATCH 2/2] Update with suggested review changes --- includes/export/abstract-wc-csv-exporter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/export/abstract-wc-csv-exporter.php b/includes/export/abstract-wc-csv-exporter.php index 6c8c9eed224..cda963538db 100644 --- a/includes/export/abstract-wc-csv-exporter.php +++ b/includes/export/abstract-wc-csv-exporter.php @@ -76,7 +76,7 @@ abstract class WC_CSV_Exporter { * * @var string */ - protected $delimiter = ","; + protected $delimiter = ','; /** * Prepare data that will be exported. @@ -121,7 +121,7 @@ abstract class WC_CSV_Exporter { * Return the delimiter to use in CSV file * * @since 3.9.0 - * @return String + * @return string */ public function get_delimiter() { return apply_filters( "woocommerce_{$this->export_type}_export_delimiter", $this->delimiter );