Merge pull request #16403 from woocommerce/fix/16195

[Importer] Allow "unfiltered_html" for name, description and short description fields
This commit is contained in:
Claudiu Lodromanean 2017-08-10 13:55:46 -07:00 committed by GitHub
commit a5ffbe6e7f
1 changed files with 73 additions and 60 deletions

View File

@ -125,17 +125,17 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
* If mapping to a SKU and the product ID does not exist, a temporary object * If mapping to a SKU and the product ID does not exist, a temporary object
* will be created so it can be updated later. * will be created so it can be updated later.
* *
* @param string $field Field value. * @param string $value Field value.
* @return int|string * @return int|string
*/ */
public function parse_relative_field( $field ) { public function parse_relative_field( $value ) {
global $wpdb; global $wpdb;
if ( empty( $field ) ) { if ( empty( $value ) ) {
return ''; return '';
} }
if ( preg_match( '/^id:(\d+)$/', $field, $matches ) ) { if ( preg_match( '/^id:(\d+)$/', $value, $matches ) ) {
$id = intval( $matches[1] ); $id = intval( $matches[1] );
$original_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_original_id' AND meta_value = %s;", $id ) ); $original_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_original_id' AND meta_value = %s;", $id ) );
@ -154,15 +154,15 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
return $id; return $id;
} }
if ( $id = wc_get_product_id_by_sku( $field ) ) { if ( $id = wc_get_product_id_by_sku( $value ) ) {
return $id; return $id;
} }
try { try {
$product = new WC_Product_Simple(); $product = new WC_Product_Simple();
$product->set_name( 'Import placeholder for ' . $field ); $product->set_name( 'Import placeholder for ' . $value );
$product->set_status( 'importing' ); $product->set_status( 'importing' );
$product->set_sku( $field ); $product->set_sku( $value );
$id = $product->save(); $id = $product->save();
if ( $id && ! is_wp_error( $id ) ) { if ( $id && ! is_wp_error( $id ) ) {
@ -181,13 +181,13 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
* If we're not doing an update, create a placeholder product so mapping works * If we're not doing an update, create a placeholder product so mapping works
* for rows following this one. * for rows following this one.
* *
* @param stirng $field * @param string $value Field value.
* @return int * @return int
*/ */
public function parse_id_field( $field ) { public function parse_id_field( $value ) {
global $wpdb; global $wpdb;
$id = absint( $field ); $id = absint( $value );
if ( ! $id ) { if ( ! $id ) {
return 0; return 0;
@ -225,77 +225,77 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
/** /**
* Parse relative comma-delineated field and return product ID. * Parse relative comma-delineated field and return product ID.
* *
* @param string $field Field value. * @param string $value Field value.
* @return array * @return array
*/ */
public function parse_relative_comma_field( $field ) { public function parse_relative_comma_field( $value ) {
if ( empty( $field ) ) { if ( empty( $value ) ) {
return array(); return array();
} }
return array_filter( array_map( array( $this, 'parse_relative_field' ), $this->explode_values( $field ) ) ); return array_filter( array_map( array( $this, 'parse_relative_field' ), $this->explode_values( $value ) ) );
} }
/** /**
* Parse a comma-delineated field from a CSV. * Parse a comma-delineated field from a CSV.
* *
* @param string $field Field value. * @param string $value Field value.
* @return array * @return array
*/ */
public function parse_comma_field( $field ) { public function parse_comma_field( $value ) {
if ( empty( $field ) ) { if ( empty( $value ) ) {
return array(); return array();
} }
return array_map( 'wc_clean', $this->explode_values( $field ) ); return array_map( 'wc_clean', $this->explode_values( $value ) );
} }
/** /**
* Parse a field that is generally '1' or '0' but can be something else. * Parse a field that is generally '1' or '0' but can be something else.
* *
* @param string $field Field value. * @param string $value Field value.
* @return bool|string * @return bool|string
*/ */
public function parse_bool_field( $field ) { public function parse_bool_field( $value ) {
if ( '0' === $field ) { if ( '0' === $value ) {
return false; return false;
} }
if ( '1' === $field ) { if ( '1' === $value ) {
return true; return true;
} }
// Don't return explicit true or false for empty fields or values like 'notify'. // Don't return explicit true or false for empty fields or values like 'notify'.
return wc_clean( $field ); return wc_clean( $value );
} }
/** /**
* Parse a float value field. * Parse a float value field.
* *
* @param string $field Field value. * @param string $value Field value.
* @return float|string * @return float|string
*/ */
public function parse_float_field( $field ) { public function parse_float_field( $value ) {
if ( '' === $field ) { if ( '' === $value ) {
return $field; return $value;
} }
return floatval( $field ); return floatval( $value );
} }
/** /**
* Parse a category field from a CSV. * Parse a category field from a CSV.
* Categories are separated by commas and subcategories are "parent > subcategory". * Categories are separated by commas and subcategories are "parent > subcategory".
* *
* @param string $field Field value. * @param string $value Field value.
* @return array of arrays with "parent" and "name" keys. * @return array of arrays with "parent" and "name" keys.
*/ */
public function parse_categories_field( $field ) { public function parse_categories_field( $value ) {
if ( empty( $field ) ) { if ( empty( $value ) ) {
return array(); return array();
} }
$row_terms = $this->explode_values( $field ); $row_terms = $this->explode_values( $value );
$categories = array(); $categories = array();
foreach ( $row_terms as $row_term ) { foreach ( $row_terms as $row_term ) {
@ -332,15 +332,15 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
/** /**
* Parse a tag field from a CSV. * Parse a tag field from a CSV.
* *
* @param string $field Field value. * @param string $value Field value.
* @return array * @return array
*/ */
public function parse_tags_field( $field ) { public function parse_tags_field( $value ) {
if ( empty( $field ) ) { if ( empty( $value ) ) {
return array(); return array();
} }
$names = $this->explode_values( $field ); $names = $this->explode_values( $value );
$tags = array(); $tags = array();
foreach ( $names as $name ) { foreach ( $names as $name ) {
@ -359,18 +359,18 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
/** /**
* Parse a shipping class field from a CSV. * Parse a shipping class field from a CSV.
* *
* @param string $field Field value. * @param string $value Field value.
* @return int * @return int
*/ */
public function parse_shipping_class_field( $field ) { public function parse_shipping_class_field( $value ) {
if ( empty( $field ) ) { if ( empty( $value ) ) {
return 0; return 0;
} }
$term = get_term_by( 'name', $field, 'product_shipping_class' ); $term = get_term_by( 'name', $value, 'product_shipping_class' );
if ( ! $term || is_wp_error( $term ) ) { if ( ! $term || is_wp_error( $term ) ) {
$term = (object) wp_insert_term( $field, 'product_shipping_class' ); $term = (object) wp_insert_term( $value, 'product_shipping_class' );
} }
return $term->term_id; return $term->term_id;
@ -379,17 +379,17 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
/** /**
* Parse images list from a CSV. Images can be filenames or URLs. * Parse images list from a CSV. Images can be filenames or URLs.
* *
* @param string $field Field value. * @param string $value Field value.
* @return array * @return array
*/ */
public function parse_images_field( $field ) { public function parse_images_field( $value ) {
if ( empty( $field ) ) { if ( empty( $value ) ) {
return array(); return array();
} }
$images = array(); $images = array();
foreach ( $this->explode_values( $field ) as $image ) { foreach ( $this->explode_values( $value ) as $image ) {
if ( stristr( $image, '://' ) ) { if ( stristr( $image, '://' ) ) {
$images[] = esc_url_raw( $image ); $images[] = esc_url_raw( $image );
} else { } else {
@ -404,17 +404,17 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
* Parse dates from a CSV. * Parse dates from a CSV.
* Dates requires the format YYYY-MM-DD and time is optional. * Dates requires the format YYYY-MM-DD and time is optional.
* *
* @param string $field Field value. * @param string $value Field value.
* @return string|null * @return string|null
*/ */
public function parse_date_field( $field ) { public function parse_date_field( $value ) {
if ( empty( $field ) ) { if ( empty( $value ) ) {
return null; return null;
} }
if ( preg_match( '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])([ 01-9:]*)$/', $field ) ) { if ( preg_match( '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])([ 01-9:]*)$/', $value ) ) {
// Don't include the time if the field had time in it. // Don't include the time if the field had time in it.
return current( explode( ' ', $field ) ); return current( explode( ' ', $value ) );
} }
return null; return null;
@ -423,25 +423,38 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
/** /**
* Parse backorders from a CSV. * Parse backorders from a CSV.
* *
* @param string $field Field value. * @param string $value Field value.
* @return string * @return string
*/ */
public function parse_backorders_field( $field ) { public function parse_backorders_field( $value ) {
if ( empty( $field ) ) { if ( empty( $value ) ) {
return ''; return '';
} }
$field = $this->parse_bool_field( $field ); $value = $this->parse_bool_field( $value );
if ( 'notify' === $field ) { if ( 'notify' === $value ) {
return 'notify'; return 'notify';
} elseif ( is_bool( $field ) ) { } elseif ( is_bool( $value ) ) {
return $field ? 'yes' : 'no'; return $value ? 'yes' : 'no';
} }
return ''; return '';
} }
/**
* Just skip current field.
*
* By default is applied wc_clean() to all not listed fields
* in self::get_formating_callback(), use this method to skip any formating.
*
* @param string $value Field value.
* @return string
*/
public function parse_skip_field( $value ) {
return $value;
}
/** /**
* Get formatting callback. * Get formatting callback.
* *
@ -460,9 +473,9 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
'featured' => array( $this, 'parse_bool_field' ), 'featured' => array( $this, 'parse_bool_field' ),
'date_on_sale_from' => array( $this, 'parse_date_field' ), 'date_on_sale_from' => array( $this, 'parse_date_field' ),
'date_on_sale_to' => array( $this, 'parse_date_field' ), 'date_on_sale_to' => array( $this, 'parse_date_field' ),
'name' => 'wp_filter_post_kses', 'name' => array( $this, 'parse_skip_field' ),
'short_description' => 'wp_filter_post_kses', 'short_description' => array( $this, 'parse_skip_field' ),
'description' => 'wp_filter_post_kses', 'description' => array( $this, 'parse_skip_field' ),
'manage_stock' => array( $this, 'parse_bool_field' ), 'manage_stock' => array( $this, 'parse_bool_field' ),
'backorders' => array( $this, 'parse_backorders_field' ), 'backorders' => array( $this, 'parse_backorders_field' ),
'stock_status' => array( $this, 'parse_bool_field' ), 'stock_status' => array( $this, 'parse_bool_field' ),