This commit is contained in:
Mike Jolley 2017-12-15 13:19:23 +00:00
parent ea8681280a
commit 47b39caf83
1 changed files with 39 additions and 19 deletions

View File

@ -7,6 +7,7 @@
* @package WooCommerce/Import
* @version 3.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
@ -15,7 +16,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* Include dependencies.
*/
if ( ! class_exists( 'WC_Importer_Interface', false ) ) {
include_once( WC_ABSPATH . 'includes/interfaces/class-wc-importer-interface.php' );
include_once WC_ABSPATH . 'includes/interfaces/class-wc-importer-interface.php';
}
/**
@ -186,7 +187,15 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
$product = wc_get_product( $id );
if ( ! $product ) {
return new WP_Error( 'woocommerce_product_csv_importer_invalid_id', sprintf( __( 'Invalid product ID %d.', 'woocommerce' ), $id ), array( 'id' => $id, 'status' => 401 ) );
return new WP_Error(
'woocommerce_product_csv_importer_invalid_id',
/* translators: %d: product ID */
sprintf( __( 'Invalid product ID %d.', 'woocommerce' ), $id ),
array(
'id' => $id,
'status' => 401,
)
);
}
} else {
$product = new WC_Product_Simple( $id );
@ -198,6 +207,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
/**
* Process a single item and save.
*
* @throws Exception If item cannot be processed.
* @param array $data Raw CSV data.
* @return array|WC_Error
*/
@ -206,9 +216,13 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
do_action( 'woocommerce_product_import_before_process_item', $data );
// Get product ID from SKU if created during the importation.
if ( empty( $data['id'] ) && ! empty( $data['sku'] ) && ( $product_id = wc_get_product_id_by_sku( $data['sku'] ) ) ) {
if ( empty( $data['id'] ) && ! empty( $data['sku'] ) ) {
$product_id = wc_get_product_id_by_sku( $data['sku'] );
if ( $product_id ) {
$data['id'] = $product_id;
}
}
$object = $this->get_product_object( $data );
$updating = false;
@ -301,8 +315,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
*
* @param WC_Product $product Product instance.
* @param array $data Item data.
*
* @throws Exception
* @throws Exception If data cannot be set.
*/
protected function set_product_data( &$product, $data ) {
if ( isset( $data['raw_attributes'] ) ) {
@ -338,7 +351,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
}
// Check for default attributes and set "is_variation".
if ( ! empty( $attribute['default'] ) && in_array( $attribute['default'], $options ) ) {
if ( ! empty( $attribute['default'] ) && in_array( $attribute['default'], $options, true ) ) {
$default_term = get_term_by( 'name', $attribute['default'], $attribute_name );
if ( $default_term && ! is_wp_error( $default_term ) ) {
@ -363,7 +376,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
}
} elseif ( isset( $attribute['value'] ) ) {
// Check for default attributes and set "is_variation".
if ( ! empty( $attribute['default'] ) && in_array( $attribute['default'], $attribute['value'] ) ) {
if ( ! empty( $attribute['default'] ) && in_array( $attribute['default'], $attribute['value'], true ) ) {
$default_attributes[ sanitize_title( $attribute['name'] ) ] = $attribute['default'];
$is_variation = 1;
}
@ -392,9 +405,8 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
*
* @param WC_Product $variation Product instance.
* @param array $data Item data.
*
* @return WC_Product|WP_Error
* @throws Exception
* @throws Exception If data cannot be set.
*/
protected function set_variation_data( &$variation, $data ) {
$parent = false;
@ -506,6 +518,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
* @param string $url Attachment URL.
* @param int $product_id Product ID.
* @return int
* @throws Exception If attachment cannot be loaded.
*/
public function get_attachment_id_from_url( $url, $product_id ) {
if ( empty( $url ) ) {
@ -524,7 +537,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
'post_type' => 'attachment',
'post_status' => 'any',
'fields' => 'ids',
'meta_query' => array(
'meta_query' => array( // @codingStandardsIgnoreLine.
'relation' => 'OR',
array(
'key' => '_wp_attached_file',
@ -549,7 +562,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
'post_type' => 'attachment',
'post_status' => 'any',
'fields' => 'ids',
'meta_query' => array(
'meta_query' => array( // @codingStandardsIgnoreLine.
array(
'value' => $url,
'key' => '_wc_attachment_source',
@ -558,7 +571,9 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
);
}
if ( $ids = get_posts( $args ) ) {
$ids = get_posts( $args ); // @codingStandardsIgnoreLine.
if ( $ids ) {
$id = current( $ids );
}
@ -573,6 +588,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
$id = wc_rest_set_uploaded_image_as_attachment( $upload, $product_id );
if ( ! wp_attachment_is_image( $id ) ) {
/* translators: %s: image URL */
throw new Exception( sprintf( __( 'Not able to attach "%s".', 'woocommerce' ), $url ), 400 );
}
@ -581,6 +597,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
}
if ( ! $id ) {
/* translators: %s: image URL */
throw new Exception( sprintf( __( 'Unable to use image "%s".', 'woocommerce' ), $url ), 400 );
}
@ -593,20 +610,23 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
*
* @param string $raw_name Attribute name.
* @return int
* @throws Exception If taxonomy cannot be loaded.
*/
public function get_attribute_taxonomy_id( $raw_name ) {
global $wpdb, $wc_product_attributes;
// These are exported as labels, so convert the label to a name if possible first.
$attribute_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' );
$attribute_name = '';
$attribute_name = array_search( $raw_name, $attribute_labels, true );
if ( ! $attribute_name = array_search( $raw_name, $attribute_labels ) ) {
if ( ! $attribute_name ) {
$attribute_name = wc_sanitize_taxonomy_name( $raw_name );
}
$attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name );
// Get the ID from the name.
if ( $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name ) ) {
if ( $attribute_id ) {
return $attribute_id;
}
@ -709,7 +729,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
* separators.
*
* @since 3.2.0
* @param string $value
* @param string $value Value to explode.
* @return array
*/
protected function explode_values( $value ) {
@ -724,7 +744,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
* Remove formatting and trim each value.
*
* @since 3.2.0
* @param string $value
* @param string $value Value to format.
* @return string
*/
protected function explode_values_formatter( $value ) {