/**
  * Get an importer Instance
  *
  * Loads the importer class and creates an instance of it.
  * This function will cache instances, so calling it for the
  * same type of importer will only create one instance and
  * return the same instance on following calls.
  *
  * @since 3.0.0
  * @param string $type Importer type.
  * @return mixed Instance of the importer class or null on failure
  */
 public function get_importer($type)
 {
     if (!$type) {
         return null;
     }
     // if importer instance is not in the cache already, load & create it
     if (!isset($this->importers[$type])) {
         $this->load_wp_importer_api();
         require_once wc_csv_import_suite()->get_plugin_path() . '/includes/class-wc-csv-import-suite-import-exception.php';
         require_once wc_csv_import_suite()->get_plugin_path() . '/includes/class-wc-csv-import-suite-parser.php';
         require_once wc_csv_import_suite()->get_plugin_path() . '/includes/class-wc-csv-import-suite-importer.php';
         $importer = null;
         switch ($type) {
             case 'woocommerce_customer_csv':
                 require_once wc_csv_import_suite()->get_plugin_path() . '/includes/class-wc-csv-import-suite-customer-import.php';
                 $importer = 'WC_CSV_Import_Suite_Customer_Import';
                 break;
             case 'woocommerce_coupon_csv':
                 require_once wc_csv_import_suite()->get_plugin_path() . '/includes/class-wc-csv-import-suite-coupon-import.php';
                 $importer = 'WC_CSV_Import_Suite_Coupon_Import';
                 break;
             case 'woocommerce_order_csv':
                 require_once wc_csv_import_suite()->get_plugin_path() . '/includes/class-wc-csv-import-suite-order-import.php';
                 $importer = 'WC_CSV_Import_Suite_Order_Import';
                 break;
         }
         /**
          * Filter the loaded importer instance
          *
          * Allows 3rd parties to load their importer instances in a streamlined manner
          *
          * @since 3.0.0
          * @param mixed $importer Importer instance, class name or null (if no matching importer was found)
          * @param string $type Importer type, such as `woocommerce_customer_csv`
          */
         $importer = apply_filters('wc_csv_import_suite_importer_class', $importer, $type);
         // create new importer instance
         if (is_string($importer)) {
             $importer = new $importer();
         }
         // cache the instance
         $this->importers[$type] = $importer;
     }
     return $this->importers[$type];
 }
function init_woocommerce_csv_import_suite()
{
    /**
     * Customer/Order/Coupon CSV Import Suite Main Class.  This class is responsible
     * for registering the importers and setting up the admin start page/menu
     * items.  The actual import process is handed off to the various parse
     * and import classes.
     *
     * Adapted from the WordPress post importer by the WordPress team
     */
    class WC_CSV_Import_Suite extends SV_WC_Plugin
    {
        /** version number */
        const VERSION = '3.0.2';
        /** @var WC_CSV_Import_Suite single instance of this plugin */
        protected static $instance;
        /** string the plugin id */
        const PLUGIN_ID = 'csv_import_suite';
        /** plugin text domain, DEPRECATED as of 2.9.0 */
        const TEXT_DOMAIN = 'woocommerce-csv-import-suite';
        /** @var \WC_CSV_Import_Suite_Admin instance */
        protected $admin;
        /** @var \WC_CSV_Import_Suite_Importers instance */
        protected $importers;
        /** @var \WC_CSV_Import_Suite_Background_Import instance */
        protected $background_import;
        /** @var \WC_CSV_Import_Suite_AJAX instance */
        protected $ajax;
        /**
         * Construct and initialize the main plugin class
         */
        public function __construct()
        {
            parent::__construct(self::PLUGIN_ID, self::VERSION, array('dependencies' => array('mbstring')));
            // Initialize
            add_action('init', array($this, 'includes'));
        }
        /**
         * Include required files
         *
         * @since 3.0.0
         */
        public function includes()
        {
            // importers and background import must be loaded all the time, because
            // otherwise background jobs simply won't work
            require_once $this->get_framework_path() . '/utilities/class-sv-wp-async-request.php';
            require_once $this->get_framework_path() . '/utilities/class-sv-wp-background-job-handler.php';
            $this->background_import = $this->load_class('/includes/class-wc-csv-import-suite-background-import.php', 'WC_CSV_Import_Suite_Background_Import');
            $this->importers = $this->load_class('/includes/class-wc-csv-import-suite-importers.php', 'WC_CSV_Import_Suite_Importers');
            if (is_admin()) {
                $this->admin_includes();
            }
            if (is_ajax()) {
                $this->ajax_includes();
            }
        }
        /**
         * Include required admin files
         *
         * @since 3.0.0
         */
        private function admin_includes()
        {
            $this->admin = $this->load_class('/includes/admin/class-wc-csv-import-suite-admin.php', 'WC_CSV_Import_Suite_Admin');
        }
        /**
         * Include required AJAX files
         *
         * @since 3.0.0
         */
        private function ajax_includes()
        {
            require_once $this->get_plugin_path() . '/includes/class-wc-csv-import-suite-parser.php';
            $this->ajax = $this->load_class('/includes/class-wc-csv-import-suite-ajax.php', 'WC_CSV_Import_Suite_AJAX');
        }
        /**
         * Return admin class instance
         *
         * @since 3.0.0
         * @return \WC_CSV_Import_Suite_Admin
         */
        public function get_admin_instance()
        {
            return $this->admin;
        }
        /**
         * Return importers class instance
         *
         * @since 3.0.0
         * @return \WC_CSV_Import_Suite_Importers
         */
        public function get_importers_instance()
        {
            return $this->importers;
        }
        /**
         * Return background import class instance
         *
         * @since 3.0.0
         * @return \WC_CSV_Import_Suite_Background_Import
         */
        public function get_background_import_instance()
        {
            return $this->background_import;
        }
        /**
         * Return the ajax class instance
         *
         * @since 3.0.0
         * @return \WC_CSV_Import_Suite_AJAX
         */
        public function get_ajax_instance()
        {
            return $this->ajax;
        }
        /**
         * Backwards compat for changing the visibility of some class instances.
         *
         * @TODO Remove this as part of WC 2.7 compat {IT 2016-05-17}
         *
         * @since 3.0.0
         */
        public function __get($name)
        {
            switch ($name) {
                case 'admin':
                    _deprecated_function('wc_csv_import_suite()->admin', '3.0.0', 'wc_csv_import_suite()->get_admin_instance()');
                    return $this->get_admin_instance();
                case 'ajax':
                    _deprecated_function('wc_csv_import_suite()->ajax', '3.0.0', 'wc_csv_import_suite()->get_ajax_instance()');
                    return $this->get_ajax_instance();
            }
            // you're probably doing it wrong
            trigger_error('Call to undefined property ' . __CLASS__ . '::' . $name, E_USER_ERROR);
            return null;
        }
        /**
         * Load plugin text domain.
         *
         * @see SV_WC_Plugin::load_translation()
         */
        public function load_translation()
        {
            load_plugin_textdomain('woocommerce-csv-import-suite', false, dirname(plugin_basename($this->get_file())) . '/i18n/languages');
        }
        /**
         * Returns the "Import" plugin action link to go directly to the plugin
         * settings page (if any)
         *
         * @since 2.3
         * @see SV_WC_Plugin::get_settings_link()
         * @param string $plugin_id the plugin identifier.  Note that this can be a
         *        sub-identifier for plugins with multiple parallel settings pages
         *        (ie a gateway that supports both credit cards and echecks)
         * @return string plugin configure link
         */
        public function get_settings_link($plugin_id = null)
        {
            $settings_url = $this->get_settings_url($plugin_id);
            if ($settings_url) {
                return sprintf('<a href="%s">%s</a>', $settings_url, __('Import', 'woocommerce-csv-import-suite'));
            }
            // no settings
            return '';
        }
        /**
         * Gets the plugin configuration URL
         *
         * @since 2.3
         * @see SV_WC_Plugin::get_settings_url()
         * @param string $plugin_id the plugin identifier.
         * @return string plugin settings URL
         */
        public function get_settings_url($plugin_id = null)
        {
            // link to the import page
            return admin_url('admin.php?page=' . self::PLUGIN_ID);
        }
        /**
         * Gets the plugin documentation url, which is non-standard for this plugin
         *
         * @since 2.3.0
         * @see SV_WC_Plugin::get_documentation_url()
         * @return string documentation URL
         */
        public function get_documentation_url()
        {
            return 'http://docs.woothemes.com/document/customer-order-csv-import-suite/';
        }
        /**
         * Gets the plugin support URL
         *
         * @since VERSION
         * @see SV_WC_Plugin::get_support_url()
         * @return string
         */
        public function get_support_url()
        {
            return 'http://support.woothemes.com/';
        }
        /**
         * Returns true if on the Customer/Order/CouponImport page
         *
         * @since 2.3
         * @see SV_WC_Plugin::is_plugin_settings()
         * @return boolean true if on the plugin admin settings page
         */
        public function is_plugin_settings()
        {
            return isset($_GET['page']) && self::PLUGIN_ID == $_GET['page'];
        }
        /** Helper methods ******************************************************/
        /**
         * Main Customer/Order/Coupon CSV Import Suite Instance, ensures only one instance is/can be loaded
         *
         * @since 2.7.0
         * @see wc_csv_import_suite()
         * @return WC_CSV_Import_Suite
         */
        public static function instance()
        {
            if (is_null(self::$instance)) {
                self::$instance = new self();
            }
            return self::$instance;
        }
        /**
         * Returns the plugin name, localized
         *
         * @since 2.3
         * @see SV_WC_Payment_Gateway::get_plugin_name()
         * @return string the plugin name
         */
        public function get_plugin_name()
        {
            return __('WooCommerce Customer/Order/Coupon CSV Import', 'woocommerce-csv-import-suite');
        }
        /**
         * Returns __FILE__
         *
         * @since 2.3
         * @return string the full path and filename of the plugin file
         */
        protected function get_file()
        {
            return __FILE__;
        }
    }
    // class WC_CSV_Import_Suite
    /**
     * Returns the One True Instance of Customer/Order/Coupon CSV Import Suite
     *
     * @since 2.7.0
     * @return WC_CSV_Import_Suite
    */
    function wc_csv_import_suite()
    {
        return WC_CSV_Import_Suite::instance();
    }
    // fire it up!
    wc_csv_import_suite();
}
Пример #3
0
<div class="wrap woocommerce">
	<div class="icon32" id="icon-woocommerce-importer"><br></div>
	<h2><?php 
esc_html_e('Import Customers, Coupons &amp; Orders', 'woocommerce-csv-import-suite');
?>
</h2>

	<div id="message" class="updated woocommerce-message wc-connect">
		<div class="squeezer">
			<h4><?php 
printf(esc_html__('%1$sCustomer CSV Import Suite%2$s Before getting started prepare your CSV files:', 'woocommerce-csv-import-suite'), '<strong>', '</strong> &#8211;');
?>
</h4>

			<p class="submit"><a href="<?php 
echo wc_csv_import_suite()->get_documentation_url();
?>
" class="button-primary"><?php 
esc_html_e('Documentation', 'woocommerce-csv-import-suite');
?>
</a>
				<a class="docs button-primary" href="https://docs.google.com/spreadsheets/d/16ub-_xEJD9V5UL6d_rTQ4LLu0PT9jXJ0Ti-iirlKyuU/edit#gid=0"><?php 
esc_html_e('Sample Customer CSV', 'woocommerce-csv-import-suite');
?>
</a>
				<a class="docs button-primary" href="https://docs.google.com/spreadsheets/d/16ub-_xEJD9V5UL6d_rTQ4LLu0PT9jXJ0Ti-iirlKyuU/edit#gid=620764597"><?php 
esc_html_e('Sample Coupon CSV', 'woocommerce-csv-import-suite');
?>
</a>
				<a class="docs button-primary" href="https://docs.google.com/spreadsheets/d/16ub-_xEJD9V5UL6d_rTQ4LLu0PT9jXJ0Ti-iirlKyuU/edit#gid=584795629"><?php 
esc_html_e('Sample Order CSV', 'woocommerce-csv-import-suite');
 /**
  * Process a coupon
  *
  * @since 3.0.0
  * @param mixed $data Parsed coupon data
  * @param array $options Optional. Options
  * @param array $raw_headers Optional. Raw headers
  * @return int|null
  */
 protected function process_item($data, $options = array(), $raw_headers = array())
 {
     $merging = $options['merge'] && isset($data['id']) && $data['id'];
     $dry_run = isset($options['dry_run']) && $options['dry_run'];
     wc_csv_import_suite()->log(__('Processing coupon.', 'woocommerce-csv-import-suite'));
     $coupon_id = null;
     // merging
     if ($merging) {
         wc_csv_import_suite()->log(sprintf(__("> Merging coupon '%s'.", 'woocommerce-csv-import-suite'), esc_html($data['code'])));
         if (!$dry_run) {
             $coupon_id = $this->update_coupon($data['id'], $data, $options);
         }
     } else {
         // insert coupon
         wc_csv_import_suite()->log(sprintf(__("> Inserting coupon '%s'", 'woocommerce-csv-import-suite'), esc_html($data['code'])));
         if (!$dry_run) {
             $coupon_id = $this->create_coupon($data, $options);
         }
     }
     // import failed
     if (!$dry_run && is_wp_error($coupon_id)) {
         $this->add_import_result('failed', $coupon_id->get_error_message());
         return null;
     }
     // TODO: is that OK to log and return as coupon_id in case of dry run?
     if ($dry_run) {
         $coupon_id = $merging ? $data['id'] : 9999;
     }
     $result = $merging ? 'merged' : 'inserted';
     $message = $merging ? __('> Finished merging coupon %s.', 'woocommerce-csv-import-suite') : __('> Finished importing coupon %s.', 'woocommerce-csv-import-suite');
     wc_csv_import_suite()->log(sprintf($message, esc_html($data['code'])));
     $this->add_import_result($result);
     return $coupon_id;
 }
 /**
  * Load an importer and start processing the import queue
  *
  * @since 3.0.0
  */
 public function load_importer()
 {
     if (!defined('WP_LOAD_IMPORTERS')) {
         return;
     }
     $type = isset($_REQUEST['import']) ? esc_attr($_REQUEST['import']) : null;
     $importer = wc_csv_import_suite()->get_importers_instance()->get_importer($type);
     if ($importer) {
         $importer->dispatch();
     }
 }
 /**
  * Log a row's import status
  *
  * @since 3.0.0
  * @param int $line_num Line number from CSV file
  * @param string $status Status
  * @param string $message Optional
  * @param bool $log Optional. Whether to log the result or not. Defaults to true
  */
 protected function add_import_result($status, $message = '', $log = true)
 {
     $this->import_results[$this->line_num] = array('status' => $status, 'message' => $message);
     if ($log) {
         $labels = array('inserted' => esc_html__('Inserted', 'woocommerce-csv-import-suite'), 'merged' => esc_html__('Merged', 'woocommerce-csv-import-suite'), 'skipped' => esc_html__('Skipped', 'woocommerce-csv-import-suite'), 'failed' => esc_html__('Failed', 'woocommerce-csv-import-suite'));
         $status_label = isset($labels[$status]) ? $labels[$status] : $status;
         $log_message = sprintf("> > %s. %s", $status_label, $message);
         wc_csv_import_suite()->log($log_message);
     }
 }
 /**
  * Update an order item
  *
  * @since 3.0.0
  * @param \WC_Order $order WC_Order instance
  * @param int $order_item_id Order item ID to update
  * @param array $item Parsed item data from CSV
  * @param string $type Line item type
  * @return int|false ID of the updated order item, false on failure
  */
 private function update_order_item(WC_Order $order, $order_item_id, $item, $type)
 {
     $result = false;
     switch ($type) {
         case 'line_item':
             $product = $this->get_product_for_item($item);
             $args = $this->prepare_product_args($item);
             $result = $order->update_product($order_item_id, $product, $args);
             if (!$result) {
                 wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot update order item %d.', 'woocommerce-csv-import-suite'), $order_item_id));
             }
             break;
         case 'shipping':
             $args = array('order_item_name' => $item['method_title']);
             $result = wc_update_order_item($order_item_id, $args);
             if (!$result) {
                 wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot update shipping method "%s".', 'woocommerce-csv-import-suite'), esc_html($item['title'])));
             }
             break;
         case 'tax':
             $args = array('order_item_name' => $item['code']);
             $result = wc_update_order_item($order_item_id, $args);
             if (!$result) {
                 wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot update tax "%s".', 'woocommerce-csv-import-suite'), esc_html($item['label'])));
             }
             break;
         case 'coupon':
             $args = array('code' => $item['code'], 'discount_amount' => $item['amount']);
             $result = $order->update_coupon($order_item_id, $args);
             if (!$result) {
                 wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot merge coupon "%s".', 'woocommerce-csv-import-suite'), esc_html($item['code'])));
             }
             break;
         case 'fee':
             $args = array('name' => $item['title'], 'line_total' => $item['total'], 'line_tax' => $item['total_tax'], 'tax_class' => isset($item['tax_class']) ? $item['tax_class'] : '');
             $result = $order->update_fee($order_item_id, $args);
             if (!$result) {
                 wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot merge fee "%s".', 'woocommerce-csv-import-suite'), esc_html($item['title'])));
             }
             break;
     }
     return $result;
 }