Add enclosure and escape arguments to importer. Introduce filter to change importer arguments (#16053)

* Introduce woocommerce_product_csv_importer_args filter.

* Add extra args to WC_Product_CSV_Importer. Have fgetcsv use extra args

* Update to follow coding standards.
This commit is contained in:
webspecnick 2017-07-12 06:11:44 -05:00 committed by Mike Jolley
parent 58338354e5
commit 49ba871795
2 changed files with 5 additions and 2 deletions

View File

@ -68,6 +68,7 @@ class WC_Product_CSV_Importer_Controller {
*/
public static function get_importer( $file, $args = array() ) {
$importer_class = apply_filters( 'woocommerce_product_csv_importer_class', 'WC_Product_CSV_Importer' );
$args = apply_filters( 'woocommerce_product_csv_importer_args', $args, $importer_class );
return new $importer_class( $file, $args );
}

View File

@ -39,6 +39,8 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
'update_existing' => false, // Whether to update existing items.
'delimiter' => ',', // CSV delimiter.
'prevent_timeouts' => true, // Check memory and time usage and abort if reaching limit.
'enclosure' => '"', // The character used to wrap text in the CSV.
'escape' => '\\',
);
$this->params = wp_parse_args( $params, $default_args );
@ -58,7 +60,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
*/
protected function read_file() {
if ( false !== ( $handle = fopen( $this->file, 'r' ) ) ) {
$this->raw_keys = fgetcsv( $handle, 0, $this->params['delimiter'] );
$this->raw_keys = fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'], $this->params['escape'] );
// Remove BOM signature from the first item.
if ( isset( $this->raw_keys[0] ) ) {
@ -69,7 +71,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
fseek( $handle, (int) $this->params['start_pos'] );
}
while ( false !== ( $row = fgetcsv( $handle, 0, $this->params['delimiter'] ) ) ) {
while ( false !== ( $row = fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'], $this->params['escape'] ) ) ) {
$this->raw_data[] = $row;
$this->file_positions[ count( $this->raw_data ) ] = ftell( $handle );