convert newlines to/from \n on export/import

This commit is contained in:
Ron Rennick 2018-12-26 15:30:40 -04:00
parent c1cb73c00f
commit 9b200b213d
2 changed files with 36 additions and 2 deletions

View File

@ -235,6 +235,10 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
$value = $product->{"get_{$column_id}"}( 'edit' );
}
if ( 'description' == $column_id || 'short_description' == $column_id ) {
$value = $this->filter_description_field( $value );
}
$row[ $column_id ] = $value;
}
@ -557,6 +561,20 @@ class WC_Product_CSV_Exporter extends WC_CSV_Batch_Exporter {
return $this->implode_values( $types );
}
/**
* Filter description field for export.
* Convert newlines to '\n'.
*
* @param string $description Product description text to filter.
*
* @since 3.5.4
* @return string
*/
protected function filter_description_field( $description ) {
$description = str_replace( '\n', "\\\\n", $description );
$description = str_replace( "\n", '\n', $description );
return $description;
}
/**
* Export downloads.
*

View File

@ -571,6 +571,22 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
return intval( $value );
}
/**
* Parse a description value field
*
* @param string $description field value.
*
* @return string
*/
public function parse_description_field( $description ) {
$parts = explode( "\\\\n", $description );
foreach ( $parts as $key => $part ) {
$parts[ $key ] = str_replace( '\n', "\n", $part );
}
return implode( '\\\n', $parts );
}
/**
* Get formatting callback.
*
@ -590,8 +606,8 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
'date_on_sale_from' => array( $this, 'parse_date_field' ),
'date_on_sale_to' => array( $this, 'parse_date_field' ),
'name' => array( $this, 'parse_skip_field' ),
'short_description' => array( $this, 'parse_skip_field' ),
'description' => array( $this, 'parse_skip_field' ),
'short_description' => array( $this, 'parse_description_field' ),
'description' => array( $this, 'parse_description_field' ),
'manage_stock' => array( $this, 'parse_bool_field' ),
'low_stock_amount' => array( $this, 'parse_stock_quantity_field' ),
'backorders' => array( $this, 'parse_backorders_field' ),