From 900c150569509ae09a223b1a010c1aaa6e601a9a Mon Sep 17 00:00:00 2001 From: Ron Rennick Date: Thu, 15 Nov 2018 15:47:32 -0400 Subject: [PATCH] Introduce `unescape_data` replacing `unescape_negative_number` for consistency with `escape_data` for strings in a CSV context --- .../import/abstract-wc-product-importer.php | 22 +++++++++---------- .../import/class-wc-product-csv-importer.php | 8 ++++--- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/includes/import/abstract-wc-product-importer.php b/includes/import/abstract-wc-product-importer.php index 64404ed5c60..5fbfcf504b7 100644 --- a/includes/import/abstract-wc-product-importer.php +++ b/includes/import/abstract-wc-product-importer.php @@ -765,21 +765,21 @@ abstract class WC_Product_Importer implements WC_Importer_Interface { } /** - * The exporter prepends a ' to fields that start with a - which causes - * issues with negative numbers. This removes the ' if the input is still a valid - * number after removal. + * The exporter prepends a ' to escape fields that start with =, +, - or @. + * Remove the prepended ' character preceding those characters. * - * @since 3.3.0 - * @param string $value A numeric string that may or may not have ' prepended. + * @since 3.5.2 + * @param string $value A string that may or may not have been escaped with '. * @return string */ - protected function unescape_negative_number( $value ) { - if ( 0 === strpos( $value, "'-" ) ) { - $unescaped = trim( $value, "'" ); - if ( is_numeric( $unescaped ) ) { - return $unescaped; - } + protected function unescape_data( $value ) { + $active_content_triggers = array( "'=", "'+", "'-", "'@" ); + + if ( in_array( mb_substr( $value, 0, 2 ), $active_content_triggers, true ) ) { + $value = mb_substr( $value, 1 ); } + return $value; } + } diff --git a/includes/import/class-wc-product-csv-importer.php b/includes/import/class-wc-product-csv-importer.php index eb546efafdc..972c5488eba 100644 --- a/includes/import/class-wc-product-csv-importer.php +++ b/includes/import/class-wc-product-csv-importer.php @@ -284,6 +284,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { return array(); } + $value = $this->unescape_data( $value ); return array_map( 'wc_clean', $this->explode_values( $value ) ); } @@ -318,7 +319,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { } // Remove the ' prepended to fields that start with - if needed. - $value = $this->unescape_negative_number( $value ); + $value = $this->unescape_data( $value ); return floatval( $value ); } @@ -335,7 +336,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { } // Remove the ' prepended to fields that start with - if needed. - $value = $this->unescape_negative_number( $value ); + $value = $this->unescape_data( $value ); return wc_stock_amount( $value ); } @@ -403,6 +404,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { return array(); } + $value = $this->unescape_data( $value ); $names = $this->explode_values( $value ); $tags = array(); @@ -549,7 +551,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer { */ public function parse_int_field( $value ) { // Remove the ' prepended to fields that start with - if needed. - $value = $this->unescape_negative_number( $value ); + $value = $this->unescape_data( $value ); return intval( $value ); }