Added method to escape commas in CSV values
This commit is contained in:
parent
748c39d966
commit
eb5c6d688b
|
@ -405,6 +405,25 @@ abstract class WC_CSV_Exporter {
|
|||
}
|
||||
}
|
||||
|
||||
return implode( ', ', $formatted_terms );
|
||||
return $this->implode_values( $formatted_terms );
|
||||
}
|
||||
|
||||
/**
|
||||
* Implode CSV cell values using commas by default, and wrapping values
|
||||
* which contain the separator.
|
||||
*
|
||||
* @since 3.2.0
|
||||
* @param array $values
|
||||
* @return string
|
||||
*/
|
||||
protected function implode_values( $values ) {
|
||||
$values_to_implode = array();
|
||||
|
||||
foreach ( $values as $value ) {
|
||||
$value = (string) is_scalar( $value ) ? $value : '';
|
||||
$values_to_implode[] = str_replace( ',', '\\,', $value );
|
||||
}
|
||||
|
||||
return implode( ', ', $values_to_implode );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -240,7 +240,7 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
|
|||
}
|
||||
}
|
||||
|
||||
return implode( ', ', $images );
|
||||
return $this->implode_values( $images );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -261,7 +261,7 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
|
|||
}
|
||||
}
|
||||
|
||||
return implode( ',', $product_list );
|
||||
return $this->implode_values( $product_list );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -327,7 +327,7 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
|
|||
|
||||
$grouped_products[] = $child->get_sku( 'edit' ) ? $child->get_sku( 'edit' ) : 'id:' . $child_id;
|
||||
}
|
||||
return implode( ',', $grouped_products );
|
||||
return $this->implode_values( $grouped_products );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -421,7 +421,7 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
|
|||
$types[] = 'virtual';
|
||||
}
|
||||
|
||||
return implode( ', ', $types );
|
||||
return $this->implode_values( $types );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -479,10 +479,10 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
|
|||
$values[] = $term->name;
|
||||
}
|
||||
|
||||
$row[ 'attributes:value' . $i ] = implode( ', ', $values );
|
||||
$row[ 'attributes:value' . $i ] = $this->implode_values( $values );
|
||||
$row[ 'attributes:taxonomy' . $i ] = 1;
|
||||
} else {
|
||||
$row[ 'attributes:value' . $i ] = implode( ', ', $attribute->get_options() );
|
||||
$row[ 'attributes:value' . $i ] = $this->implode_values( $attribute->get_options() );
|
||||
$row[ 'attributes:taxonomy' . $i ] = 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -683,4 +683,31 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
|
|||
}
|
||||
return apply_filters( 'woocommerce_product_importer_time_exceeded', $return );
|
||||
}
|
||||
|
||||
/**
|
||||
* Explode CSV cell values using commas by default, and handling escaped
|
||||
* separators.
|
||||
*
|
||||
* @since 3.2.0
|
||||
* @param string $value
|
||||
* @return array
|
||||
*/
|
||||
protected function explode_values( $value ) {
|
||||
$value = str_replace( '\\,', '::separator::', $value );
|
||||
$values = explode( ',', $value );
|
||||
$values = array_map( array( $this, 'explode_values_formatter' ), $values );
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove formatting and trim each value.
|
||||
*
|
||||
* @since 3.2.0
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
protected function explode_values_formatter( $value ) {
|
||||
return trim( str_replace( '::separator::', ',', $value ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -221,7 +221,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
|||
return array();
|
||||
}
|
||||
|
||||
return array_filter( array_map( array( $this, 'parse_relative_field' ), array_map( 'trim', explode( ',', $field ) ) ) );
|
||||
return array_filter( array_map( array( $this, 'parse_relative_field' ), $this->explode_values( $field ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -235,7 +235,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
|||
return array();
|
||||
}
|
||||
|
||||
return array_map( 'wc_clean', array_map( 'trim', explode( ',', $field ) ) );
|
||||
return array_map( 'wc_clean', $this->explode_values( $field ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -283,7 +283,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
|||
return array();
|
||||
}
|
||||
|
||||
$row_terms = array_map( 'trim', explode( ',', $field ) );
|
||||
$row_terms = $this->explode_values( $field );
|
||||
$categories = array();
|
||||
|
||||
foreach ( $row_terms as $row_term ) {
|
||||
|
@ -328,7 +328,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
|||
return array();
|
||||
}
|
||||
|
||||
$names = array_map( 'trim', explode( ',', $field ) );
|
||||
$names = $this->explode_values( $field );
|
||||
$tags = array();
|
||||
|
||||
foreach ( $names as $name ) {
|
||||
|
@ -375,7 +375,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
|||
return array();
|
||||
}
|
||||
|
||||
return array_map( 'esc_url_raw', array_map( 'trim', explode( ',', $field ) ) );
|
||||
return array_map( 'esc_url_raw', $this->explode_values( $field ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue