/** * Step #1: Choose File */ public function index() { $default = PMWI_Plugin::get_default_import_options(); $this->data['id'] = $id = $this->input->get('id'); $this->data['import'] = $import = new PMXI_Import_Record(); if (!$id or $import->getById($id)->isEmpty()) { // specified import is not found $DefaultOptions = (!empty(PMXI_Plugin::$session->options) ? PMXI_Plugin::$session->options : array()) + $default; $post = $this->input->post(apply_filters('pmxi_options_options', $DefaultOptions, true)); } else { $post = $this->input->post($this->data['import']->options + $default); } $this->data['is_loaded_template'] = !empty(PMXI_Plugin::$session->is_loaded_template) ? PMXI_Plugin::$session->is_loaded_template : false; $load_options = $this->input->post('load_template'); if ($load_options) { // init form with template selected $template = new PMXI_Template_Record(); if (!$template->getById($this->data['is_loaded_template'])->isEmpty()) { $post = (!empty($template->options) ? $template->options : array()) + $default; } } elseif ($load_options == -1) { $post = $default; } $this->data['post'] =& $post; $this->render(); }
public static function generateImportTemplate(&$export, $file_path = '', $foundPosts = 0, $link_to_import = true) { $exportOptions = $export->options; $custom_type = empty($exportOptions['cpt']) ? 'post' : $exportOptions['cpt'][0]; // Do not create an import template for WooCommerce Refunds // if ( empty($export->parent_id) and in_array($custom_type, array('shop_order_refund'))) return false; // Generate template for WP All Import if ($exportOptions['is_generate_templates']) { self::$templateOptions = array('type' => (!empty($exportOptions['cpt']) and $exportOptions['cpt'][0] == 'page') ? 'page' : 'post', 'wizard_type' => 'new', 'deligate' => 'wpallexport', 'custom_type' => XmlExportEngine::$is_user_export ? 'import_users' : $custom_type, 'status' => 'xpath', 'is_multiple_page_parent' => 'no', 'unique_key' => '', 'acf' => array(), 'fields' => array(), 'is_multiple_field_value' => array(), 'multiple_value' => array(), 'fields_delimiter' => array(), 'update_all_data' => 'no', 'is_update_status' => 0, 'is_update_title' => 0, 'is_update_author' => 0, 'is_update_slug' => 0, 'is_update_content' => 0, 'is_update_excerpt' => 0, 'is_update_dates' => 0, 'is_update_menu_order' => 0, 'is_update_parent' => 0, 'is_update_attachments' => 0, 'is_update_acf' => 0, 'update_acf_logic' => 'only', 'acf_list' => '', 'is_update_product_type' => 1, 'is_update_attributes' => 0, 'update_attributes_logic' => 'only', 'attributes_list' => '', 'is_update_images' => 0, 'is_update_custom_fields' => 0, 'update_custom_fields_logic' => 'only', 'custom_fields_list' => '', 'is_update_categories' => 0, 'update_categories_logic' => 'only', 'taxonomies_list' => '', 'export_id' => $export->id); if (in_array('product', $exportOptions['cpt']) and class_exists('PMWI_Plugin')) { $default = PMWI_Plugin::get_default_import_options(); self::$templateOptions = array_replace_recursive(self::$templateOptions, $default); self::$templateOptions['_virtual'] = 1; self::$templateOptions['_downloadable'] = 1; self::$templateOptions['put_variation_image_to_gallery'] = 1; self::$templateOptions['disable_auto_sku_generation'] = 1; } if (in_array('shop_order', $exportOptions['cpt']) and class_exists('PMWI_Plugin')) { self::$templateOptions['update_all_data'] = 'no'; self::$templateOptions['is_update_status'] = 0; self::$templateOptions['is_update_dates'] = 0; self::$templateOptions['is_update_excerpt'] = 0; $default = PMWI_Plugin::get_default_import_options(); self::$templateOptions['pmwi_order'] = $default['pmwi_order']; self::$templateOptions['pmwi_order']['is_update_billing_details'] = 0; self::$templateOptions['pmwi_order']['is_update_shipping_details'] = 0; self::$templateOptions['pmwi_order']['is_update_payment'] = 0; self::$templateOptions['pmwi_order']['is_update_notes'] = 0; self::$templateOptions['pmwi_order']['is_update_products'] = 0; self::$templateOptions['pmwi_order']['is_update_fees'] = 0; self::$templateOptions['pmwi_order']['is_update_coupons'] = 0; self::$templateOptions['pmwi_order']['is_update_shipping'] = 0; self::$templateOptions['pmwi_order']['is_update_taxes'] = 0; self::$templateOptions['pmwi_order']['is_update_refunds'] = 0; self::$templateOptions['pmwi_order']['is_update_total'] = 0; } if (XmlExportEngine::$is_user_export) { self::$templateOptions['is_update_first_name'] = 0; self::$templateOptions['is_update_last_name'] = 0; self::$templateOptions['is_update_role'] = 0; self::$templateOptions['is_update_nickname'] = 0; self::$templateOptions['is_update_description'] = 0; self::$templateOptions['is_update_login'] = 0; self::$templateOptions['is_update_password'] = 0; self::$templateOptions['is_update_nicename'] = 0; self::$templateOptions['is_update_email'] = 0; self::$templateOptions['is_update_registered'] = 0; self::$templateOptions['is_update_display_name'] = 0; self::$templateOptions['is_update_url'] = 0; } self::prepare_import_template($exportOptions); $tpl_options = self::$templateOptions; if ('csv' == $exportOptions['export_to']) { $tpl_options['delimiter'] = $exportOptions['delimiter']; $tpl_options['root_element'] = 'node'; } else { $tpl_options['root_element'] = $exportOptions['record_xml_tag']; } $tpl_options['update_all_data'] = 'yes'; $tpl_options['is_update_status'] = 1; $tpl_options['is_update_title'] = 1; $tpl_options['is_update_author'] = 1; $tpl_options['is_update_slug'] = 1; $tpl_options['is_update_content'] = 1; $tpl_options['is_update_excerpt'] = 1; $tpl_options['is_update_dates'] = 1; $tpl_options['is_update_menu_order'] = 1; $tpl_options['is_update_parent'] = 1; $tpl_options['is_update_attachments'] = 1; $tpl_options['is_update_acf'] = 1; $tpl_options['update_acf_logic'] = 'full_update'; $tpl_options['acf_list'] = ''; $tpl_options['is_update_product_type'] = 1; $tpl_options['is_update_attributes'] = 1; $tpl_options['update_attributes_logic'] = 'full_update'; $tpl_options['attributes_list'] = ''; $tpl_options['is_update_images'] = 1; $tpl_options['is_update_custom_fields'] = 1; $tpl_options['update_custom_fields_logic'] = 'full_update'; $tpl_options['custom_fields_list'] = ''; $tpl_options['is_update_categories'] = 1; $tpl_options['update_categories_logic'] = 'full_update'; $tpl_options['taxonomies_list'] = ''; $tpl_data = array('name' => $exportOptions['template_name'], 'is_keep_linebreaks' => 0, 'is_leave_html' => 0, 'fix_characters' => 0, 'options' => $tpl_options); $exportOptions['tpl_data'] = $tpl_data; $export->set(array('options' => $exportOptions))->save(); } $link_to_import and $export->options['is_generate_import'] and self::link_template_to_import($export, $file_path, $foundPosts); }
public function parse() { $cxpath = $this->xpath . $this->import->xpath; $this->data = array(); $records = array(); $tmp_files = array(); $this->chunk == 1 and $this->logger and call_user_func($this->logger, __('Composing shop order data...', 'wpai_woocommerce_addon_plugin')); $default = PMWI_Plugin::get_default_import_options(); foreach ($default['pmwi_order'] as $option => $default_value) { if (in_array($option, array('status_xpath', 'payment_method_xpath', 'order_note_visibility_xpath', 'billing_source', 'billing_source_match_by', 'shipping_source', 'products_source', 'order_taxes_logic', 'order_refund_issued_source', 'order_refund_issued_match_by', 'order_total_logic', 'order_note_separate_logic', 'order_note_separator')) or strpos($option, 'is_update_') !== false or strpos($option, '_repeater_mode') !== false) { continue; } switch ($option) { case 'date': case 'order_refund_date': if (!empty($this->import->options['pmwi_order'][$option])) { $dates = XmlImportParser::factory($this->xml, $cxpath, $this->import->options['pmwi_order'][$option], $file)->parse($records); $tmp_files[] = $file; $warned = array(); // used to prevent the same notice displaying several times foreach ($dates as $i => $d) { if ($d == 'now') { $d = current_time('mysql'); } // Replace 'now' with the WordPress local time to account for timezone offsets (WordPress references its local time during publishing rather than the server’s time so it should use that) $time = strtotime($d); if (FALSE === $time) { $time = time(); } $this->data['pmwi_order'][$option][$i] = date('Y-m-d H:i:s', $time); } } else { $this->count and $this->data['pmwi_order'][$option] = array_fill(0, $this->count, date('Y-m-d H:i:s')); } break; case 'status': case 'payment_method': case 'order_note_visibility': if ($this->import->options['pmwi_order'][$option] == 'xpath' && $this->import->options['pmwi_order'][$option . '_xpath'] != "") { $this->data['pmwi_order'][$option] = XmlImportParser::factory($this->xml, $cxpath, $this->import->options['pmwi_order'][$option . '_xpath'], $file)->parse($records); $tmp_files[] = $file; } else { $this->count and $this->data['pmwi_order'][$option] = array_fill(0, $this->count, $this->import->options['pmwi_order'][$option]); } break; case 'products': case 'manual_products': $this->data['pmwi_order'][$option] = array(); switch ($this->import->options['pmwi_order']['products_repeater_mode']) { case 'xml': foreach ($this->import->options['pmwi_order'][$option] as $key => $row) { for ($k = 0; $k < $this->count; $k++) { $base_xpath = '[' . ($k + 1) . ']/' . ltrim(trim($this->import->options['pmwi_order']['products_repeater_mode_foreach'], '{}!'), '/'); $rows = XmlImportParser::factory($this->xml, $cxpath . $base_xpath, "{.}", $file)->parse(); $tmp_files[] = $file; $row_data = $this->parse_item_row($row, $cxpath . $base_xpath, count($rows)); $products = array(); if (!empty($row_data)) { for ($j = 0; $j < count($rows); $j++) { $products[] = array('sku' => $row_data['sku'][$j], 'qty' => $row_data['qty'][$j], 'price_per_unit' => isset($row_data['price_per_unit'][$j]) ? $row_data['price_per_unit'][$j] : 0, 'tax_rates' => array()); if (!empty($row_data['tax_rates'])) { foreach ($row_data['tax_rates'] as $tax_rate) { $products[$j]['tax_rates'][] = array('code' => $tax_rate['code'][$j], 'calculate_logic' => $tax_rate['calculate_logic'][$j], 'percentage_value' => $tax_rate['percentage_value'][$j], 'amount_per_unit' => $tax_rate['amount_per_unit'][$j]); } } if (!empty($row_data['meta_name'])) { foreach ($row_data['meta_name'] as $meta_name) { $products[$j]['meta_name'][] = $meta_name[$j]; } } if (!empty($row_data['meta_value'])) { foreach ($row_data['meta_value'] as $meta_value) { $products[$j]['meta_value'][] = $meta_value[$j]; } } } } $this->data['pmwi_order'][$option][] = $products; } break; } break; case 'csv': foreach ($this->import->options['pmwi_order'][$option] as $key => $row) { if (empty($this->import->options['pmwi_order']['products_repeater_mode_separator'])) { break; } $row_data = $this->parse_item_row($row, $cxpath, $this->count); for ($k = 0; $k < $this->count; $k++) { $products = array(); $skus = explode($this->import->options['pmwi_order']['products_repeater_mode_separator'], $row_data['sku'][$k]); $qtys = explode($this->import->options['pmwi_order']['products_repeater_mode_separator'], $row_data['qty'][$k]); $prices = isset($row_data['price_per_unit'][$k]) ? explode($this->import->options['pmwi_order']['products_repeater_mode_separator'], $row_data['price_per_unit'][$k]) : array(); if (!empty($skus)) { for ($j = 0; $j < count($skus); $j++) { $products[] = array('sku' => $skus[$j], 'qty' => $qtys[$j], 'price_per_unit' => isset($prices[$j]) ? $prices[$j] : 0, 'tax_rates' => array()); if (!empty($row_data['tax_rates'])) { foreach ($row_data['tax_rates'] as $tax_rate) { $products[$j]['tax_rates'][] = array('code' => $tax_rate['code'][$k], 'calculate_logic' => $tax_rate['calculate_logic'][$k], 'percentage_value' => $tax_rate['percentage_value'][$k], 'amount_per_unit' => $tax_rate['amount_per_unit'][$k]); } } if (!empty($row_data['meta_name'])) { foreach ($row_data['meta_name'] as $meta_name) { $products[$j]['meta_name'][] = $meta_name[$j]; } } if (!empty($row_data['meta_value'])) { foreach ($row_data['meta_value'] as $meta_value) { $products[$j]['meta_value'][] = $meta_value[$j]; } } } } $this->data['pmwi_order'][$option][] = $products; } break; } break; default: $row_data = array(); foreach ($this->import->options['pmwi_order'][$option] as $key => $row) { $row_data[] = $this->parse_item_row($row, $cxpath, $this->count); } for ($j = 0; $j < $this->count; $j++) { $products = array(); foreach ($row_data as $k => $product) { $products[] = array('sku' => $product['sku'][$j], 'qty' => $product['qty'][$j], 'price_per_unit' => isset($product['price_per_unit'][$j]) ? $product['price_per_unit'][$j] : 0, 'tax_rates' => array()); if (!empty($product['tax_rates'])) { foreach ($product['tax_rates'] as $tax_rate) { $products[$k]['tax_rates'][] = array('code' => $tax_rate['code'][$j], 'calculate_logic' => $tax_rate['calculate_logic'][$j], 'percentage_value' => $tax_rate['percentage_value'][$j], 'amount_per_unit' => $tax_rate['amount_per_unit'][$j]); } } if (!empty($product['meta_name'])) { foreach ($product['meta_name'] as $meta_name) { $products[$k]['meta_name'][] = $meta_name[$j]; } } if (!empty($product['meta_value'])) { foreach ($product['meta_value'] as $meta_value) { $products[$k]['meta_value'][] = $meta_value[$j]; } } } $this->data['pmwi_order'][$option][] = $products; } break; } break; case 'fees': case 'coupons': case 'shipping': case 'taxes': case 'notes': $this->data['pmwi_order'][$option] = array(); switch ($this->import->options['pmwi_order'][$option . '_repeater_mode']) { case 'xml': foreach ($this->import->options['pmwi_order'][$option] as $key => $row) { for ($k = 0; $k < $this->count; $k++) { $base_xpath = '[' . ($k + 1) . ']/' . ltrim(trim($this->import->options['pmwi_order'][$option . '_repeater_mode_foreach'], '{}!'), '/'); $rows = XmlImportParser::factory($this->xml, $cxpath . $base_xpath, "{.}", $file)->parse(); $tmp_files[] = $file; $row_data = $this->parse_item_row($row, $cxpath . $base_xpath, count($rows)); $items = array(); if (!empty($row_data)) { for ($j = 0; $j < count($rows); $j++) { foreach ($row_data as $itemkey => $values) { $items[$j][$itemkey] = $values[$j]; } } } $this->data['pmwi_order'][$option][] = $items; } break; } break; case 'csv': $separator = $this->import->options['pmwi_order'][$option . '_repeater_mode_separator']; foreach ($this->import->options['pmwi_order'][$option] as $key => $row) { if (empty($separator)) { break; } $row_data = $this->parse_item_row($row, $cxpath, $this->count); for ($k = 0; $k < $this->count; $k++) { $items = array(); $maxCountRows = 0; foreach ($row_data as $itemkey => $values) { $itemIndex = 0; $rows = explode($separator, $values[$k]); if (!empty($rows)) { if (count($rows) > $maxCountRows) { $maxCountRows = count($rows); } if (count($rows) == 1) { for ($j = 0; $j < $maxCountRows; $j++) { $items[$itemIndex][$itemkey] = trim($rows[0]); $itemIndex++; } } else { foreach ($rows as $val) { $items[$itemIndex][$itemkey] = trim($val); $itemIndex++; } } } // else // { // for ($j = 0; $j < $maxCountRows; $j++) // { // $items[$itemIndex][$itemkey] = ''; // $itemIndex++; // } // } } $this->data['pmwi_order'][$option][] = $items; } break; } break; default: $row_data = array(); foreach ($this->import->options['pmwi_order'][$option] as $key => $row) { $row_data[] = $this->parse_item_row($row, $cxpath, $this->count); } for ($j = 0; $j < $this->count; $j++) { $items = array(); $itemIndex = 0; foreach ($row_data as $k => $item) { foreach ($item as $itemkey => $values) { $items[$itemIndex][$itemkey] = $values[$j]; } $itemIndex++; } $this->data['pmwi_order'][$option][] = $items; } break; } break; default: if (!empty($this->import->options['pmwi_order'][$option])) { $this->data['pmwi_order'][$option] = XmlImportParser::factory($this->xml, $cxpath, $this->import->options['pmwi_order'][$option], $file)->parse($records); $tmp_files[] = $file; } else { $this->count and $this->data['pmwi_order'][$option] = array_fill(0, $this->count, $default_value); } break; } } foreach ($tmp_files as $file) { // remove all temporary files created unlink($file); } // file_put_contents(PMWI_ROOT_DIR . '/order_parse.txt', json_encode($this->data)); return $this->data; }