Merge pull request #19378 from woocommerce/fix/19217

Deletes orphan variations after successful import.
This commit is contained in:
Claudiu Lodromanean 2018-03-14 08:27:30 -07:00 committed by GitHub
commit feba3bd3ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 21 deletions

View File

@ -2,15 +2,10 @@
/**
* Init WooCommerce data importers.
*
* @author Automattic
* @category Admin
* @package WooCommerce/Admin
* @version 3.1.0
* @package WooCommerce/Admin
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;
/**
* WC_Admin_Importers Class.
@ -111,7 +106,6 @@ class WC_Admin_Importers {
* The tax rate importer which extends WP_Importer.
*/
public function tax_rates_importer() {
// Load Importer API
require_once ABSPATH . 'wp-admin/includes/import.php';
if ( ! class_exists( 'WP_Importer' ) ) {
@ -122,10 +116,8 @@ class WC_Admin_Importers {
}
}
// includes
require dirname( __FILE__ ) . '/importers/class-wc-tax-rate-importer.php';
// Dispatch
$importer = new WC_Tax_Rate_Importer();
$importer->dispatch();
}
@ -139,11 +131,11 @@ class WC_Admin_Importers {
public function post_importer_compatibility() {
global $wpdb;
if ( empty( $_POST['import_id'] ) || ! class_exists( 'WXR_Parser' ) ) {
if ( empty( $_POST['import_id'] ) || ! class_exists( 'WXR_Parser' ) ) { // PHPCS: input var ok, CSRF ok.
return;
}
$id = absint( $_POST['import_id'] );
$id = absint( $_POST['import_id'] ); // PHPCS: input var ok.
$file = get_attached_file( $id );
$parser = new WXR_Parser();
$import_data = $parser->parse( $file );
@ -156,8 +148,8 @@ class WC_Admin_Importers {
if ( ! taxonomy_exists( $term['domain'] ) ) {
$attribute_name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $term['domain'] ) );
// Create the taxonomy
if ( ! in_array( $attribute_name, wc_get_attribute_taxonomies() ) ) {
// Create the taxonomy.
if ( ! in_array( $attribute_name, wc_get_attribute_taxonomies(), true ) ) {
wc_create_attribute(
array(
'name' => $attribute_name,
@ -198,19 +190,19 @@ class WC_Admin_Importers {
check_ajax_referer( 'wc-product-import', 'security' );
if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['file'] ) ) {
if ( ! current_user_can( 'edit_products' ) || ! isset( $_POST['file'] ) ) { // PHPCS: input var ok.
wp_die( -1 );
}
include_once WC_ABSPATH . 'includes/admin/importers/class-wc-product-csv-importer-controller.php';
include_once WC_ABSPATH . 'includes/import/class-wc-product-csv-importer.php';
$file = wc_clean( $_POST['file'] );
$file = wc_clean( wp_unslash( $_POST['file'] ) ); // PHPCS: input var ok.
$params = array(
'delimiter' => ! empty( $_POST['delimiter'] ) ? wc_clean( $_POST['delimiter'] ) : ',',
'start_pos' => isset( $_POST['position'] ) ? absint( $_POST['position'] ) : 0,
'mapping' => isset( $_POST['mapping'] ) ? (array) $_POST['mapping'] : array(),
'update_existing' => isset( $_POST['update_existing'] ) ? (bool) $_POST['update_existing'] : false,
'delimiter' => ! empty( $_POST['delimiter'] ) ? wc_clean( wp_unslash( $_POST['delimiter'] ) ) : ',', // PHPCS: input var ok.
'start_pos' => isset( $_POST['position'] ) ? absint( $_POST['position'] ) : 0, // PHPCS: input var ok.
'mapping' => isset( $_POST['mapping'] ) ? (array) wc_clean( wp_unslash( $_POST['mapping'] ) ) : array(), // PHPCS: input var ok.
'update_existing' => isset( $_POST['update_existing'] ) ? (bool) $_POST['update_existing'] : false, // PHPCS: input var ok.
'lines' => apply_filters( 'woocommerce_product_import_batch_size', 30 ),
'parse' => true,
);
@ -231,7 +223,7 @@ class WC_Admin_Importers {
if ( 100 === $percent_complete ) {
// Clear temp meta.
$wpdb->delete( $wpdb->postmeta, array( 'meta_key' => '_original_id' ) );
$wpdb->delete( $wpdb->postmeta, array( 'meta_key' => '_original_id' ) ); // @codingStandardsIgnoreLine.
$wpdb->query(
"DELETE {$wpdb->posts}, {$wpdb->postmeta}, {$wpdb->term_relationships}
FROM {$wpdb->posts}
@ -243,6 +235,14 @@ class WC_Admin_Importers {
AND {$wpdb->posts}.post_status = 'importing'"
);
// Clear orphan variations.
$wpdb->query(
"DELETE products
FROM {$wpdb->posts} products
LEFT JOIN {$wpdb->posts} wp ON wp.ID = products.post_parent
WHERE wp.ID IS NULL AND products.post_type = 'product_variation';"
);
// Send success.
wp_send_json_success(
array(