Skip to content

Commit

Permalink
Use intval for download limit and expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jan 9, 2018
1 parent d13dfa6 commit fd2b830
Showing 1 changed file with 45 additions and 26 deletions.
71 changes: 45 additions & 26 deletions includes/import/class-wc-product-csv-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
/**
* WooCommerce Product CSV importer
*
* @author Automattic
* @category Admin
* @package WooCommerce/Import
* @version 3.1.0
*/
Expand All @@ -16,7 +14,7 @@
* Include dependencies.
*/
if ( ! class_exists( 'WC_Product_Importer', false ) ) {
include_once( dirname( __FILE__ ) . '/abstract-wc-product-importer.php' );
include_once dirname( __FILE__ ) . '/abstract-wc-product-importer.php';
}

/**
Expand Down Expand Up @@ -57,7 +55,9 @@ public function __construct( $file, $params = array() ) {
* Read file.
*/
protected function read_file() {
if ( false !== ( $handle = fopen( $this->file, 'r' ) ) ) {
$handle = fopen( $this->file, 'r' ); // @codingStandardsIgnoreLine.

if ( false !== $handle ) {
$this->raw_keys = fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'] );

// Remove BOM signature from the first item.
Expand All @@ -69,7 +69,9 @@ protected function read_file() {
fseek( $handle, (int) $this->params['start_pos'] );
}

while ( false !== ( $row = fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'] ) ) ) {
$row = fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'] );

while ( false !== $row ) {
$this->raw_data[] = $row;
$this->file_positions[ count( $this->raw_data ) ] = ftell( $handle );

Expand Down Expand Up @@ -141,14 +143,14 @@ public function parse_relative_field( $value ) {
$id = intval( $matches[1] );

// If original_id is found, use that instead of the given ID since a new placeholder must have been created already.
$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 ) ); // WPCS: db call ok, cache ok.

if ( $original_id ) {
return absint( $original_id );
}

// See if the given ID maps to a valid product allready.
$existing_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation' ) AND ID = %d;", $id ) );
$existing_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation' ) AND ID = %d;", $id ) ); // WPCS: db call ok, cache ok.

if ( $existing_id ) {
return absint( $existing_id );
Expand All @@ -166,7 +168,9 @@ public function parse_relative_field( $value ) {
return $id;
}

if ( $id = wc_get_product_id_by_sku( $value ) ) {
$id = wc_get_product_id_by_sku( $value );

if ( $id ) {
return $id;
}

Expand Down Expand Up @@ -206,16 +210,18 @@ public function parse_id_field( $value ) {
}

// See if this maps to an ID placeholder already.
$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 ) ); // WPCS: db call ok, cache ok.

if ( $original_id ) {
return absint( $original_id );
}

// Not updating? Make sure we have a new placeholder for this ID.
if ( ! $this->params['update_existing'] ) {
$id = isset( $this->raw_data['sku'] ) ? wc_get_product_id_by_sku( $this->raw_data['sku'] ) : '';

// If row has a SKU, make sure placeholder was not made already.
if ( isset( $this->raw_data['sku'] ) && $id = wc_get_product_id_by_sku( $this->raw_data['sku'] ) ) {
if ( $id ) {
return $id;
}

Expand Down Expand Up @@ -344,7 +350,7 @@ public function parse_categories_field( $value ) {
if ( is_array( $term ) ) {
$term_id = $term['term_id'];
} else {
$term = wp_insert_term( $_term, 'product_cat', array( 'parent' => intval( $parent ) ) );
$term = wp_insert_term( $_term, 'product_cat', array( 'parent' => intval( $parent ) ) );

if ( is_wp_error( $term ) ) {
break; // We cannot continue if the term cannot be inserted.
Expand Down Expand Up @@ -557,8 +563,8 @@ protected function get_formating_callback() {
'grouped_products' => array( $this, 'parse_relative_comma_field' ),
'upsell_ids' => array( $this, 'parse_relative_comma_field' ),
'cross_sell_ids' => array( $this, 'parse_relative_comma_field' ),
'download_limit' => 'absint',
'download_expiry' => 'absint',
'download_limit' => 'intval',
'download_expiry' => 'intval',
'product_url' => 'esc_url_raw',
'menu_order' => 'intval',
);
Expand Down Expand Up @@ -640,7 +646,7 @@ protected function expand_data( $data ) {

// Status is mapped from a special published field.
if ( isset( $data['published'] ) ) {
$statuses = array(
$statuses = array(
-1 => 'draft',
0 => 'private',
1 => 'publish',
Expand Down Expand Up @@ -817,9 +823,11 @@ protected function get_row_id( $parsed_data ) {
$row_data[] = $name;
}
if ( $id ) {
/* translators: %d: product ID */
$row_data[] = sprintf( __( 'ID %d', 'woocommerce' ), $id );
}
if ( $sku ) {
/* translators: %s: product SKU */
$row_data[] = sprintf( __( 'SKU %s', 'woocommerce' ), $sku );
}

Expand All @@ -840,10 +848,10 @@ public function import() {
$index = 0;
$update_existing = $this->params['update_existing'];
$data = array(
'imported' => array(),
'failed' => array(),
'updated' => array(),
'skipped' => array(),
'imported' => array(),
'failed' => array(),
'updated' => array(),
'skipped' => array(),
);

foreach ( $this->parsed_data as $parsed_data_key => $parsed_data ) {
Expand All @@ -857,33 +865,44 @@ public function import() {
if ( $id ) {
$product = wc_get_product( $id );
$id_exists = $product && 'importing' !== $product->get_status();
} elseif ( $sku && ( $id_from_sku = wc_get_product_id_by_sku( $sku ) ) ) {
$product = wc_get_product( $id_from_sku );
$sku_exists = $product && 'importing' !== $product->get_status();
} elseif ( $sku ) {
$id_from_sku = wc_get_product_id_by_sku( $sku );
$product = $id_from_sku ? wc_get_product( $id_from_sku ) : false;
$sku_exists = $product && 'importing' !== $product->get_status();
}

if ( $id_exists && ! $update_existing ) {
$data['skipped'][] = new WP_Error( 'woocommerce_product_importer_error', __( 'A product with this ID already exists.', 'woocommerce' ), array( 'id' => $id, 'row' => $this->get_row_id( $parsed_data ) ) );
$data['skipped'][] = new WP_Error( 'woocommerce_product_importer_error', __( 'A product with this ID already exists.', 'woocommerce' ), array(
'id' => $id,
'row' => $this->get_row_id( $parsed_data ),
) );
continue;
}

if ( $sku_exists && ! $update_existing ) {
$data['skipped'][] = new WP_Error( 'woocommerce_product_importer_error', __( 'A product with this SKU already exists.', 'woocommerce' ), array( 'sku' => $sku, 'row' => $this->get_row_id( $parsed_data ) ) );
$data['skipped'][] = new WP_Error( 'woocommerce_product_importer_error', __( 'A product with this SKU already exists.', 'woocommerce' ), array(
'sku' => $sku,
'row' => $this->get_row_id( $parsed_data ),
) );
continue;
}

if ( $update_existing && ( $id || $sku ) && ! $id_exists && ! $sku_exists ) {
$data['skipped'][] = new WP_Error( 'woocommerce_product_importer_error', __( 'No matching product exists to update.', 'woocommerce' ), array( 'id' => $id, 'sku' => $sku, 'row' => $this->get_row_id( $parsed_data ) ) );
$data['skipped'][] = new WP_Error( 'woocommerce_product_importer_error', __( 'No matching product exists to update.', 'woocommerce' ), array(
'id' => $id,
'sku' => $sku,
'row' => $this->get_row_id( $parsed_data ),
) );
continue;
}

$result = $this->process_item( $parsed_data );

if ( is_wp_error( $result ) ) {
$result->add_data( array( 'row' => $this->get_row_id( $parsed_data ) ) );
$data['failed'][] = $result;
$data['failed'][] = $result;
} elseif ( $result['updated'] ) {
$data['updated'][] = $result['id'];
$data['updated'][] = $result['id'];
} else {
$data['imported'][] = $result['id'];
}
Expand Down

0 comments on commit fd2b830

Please sign in to comment.