Example #1
0
 /**
  * Parses the WXR file and prepares us for the task of processing parsed data
  *
  * @param string $file Path to the WXR file for importing
  */
 function import_start($file)
 {
     global $wpdb;
     if (!is_file($file)) {
         echo '<p><strong>' . __('Sorry, there has been an error.', 'wc_customer_relationship_manager') . '</strong><br />';
         echo __('The file does not exist, please try again.', 'wc_customer_relationship_manager') . '</p>';
         die;
     }
     if (in_array('user_email', $_POST['import_options'])) {
         $this->key_email = array_search('user_email', $_POST['import_options']);
     }
     if (empty($this->key_email) && in_array('billing_email', $_POST['import_options'])) {
         $this->key_email = array_search('billing_email', $_POST['import_options']);
     }
     if (empty($this->key_email) && $this->key_email !== 0) {
         echo '<p><strong>' . __('Sorry, there has been an error.', 'wc_customer_relationship_manager') . '</strong><br />';
         echo __('Please select user email and please try again.', 'wc_customer_relationship_manager') . '</p>';
         wp_import_cleanup($this->id);
         wp_cache_flush();
         die;
     }
     $import_data = $this->parse($file);
     if (is_wp_error($import_data)) {
         echo '<p><strong>' . __('Sorry, there has been an error.', 'wc_customer_relationship_manager') . '</strong><br />';
         echo esc_html($import_data->get_error_message()) . '</p>';
         wp_import_cleanup($this->id);
         wp_cache_flush();
         die;
     }
     if (in_array('first_name', $_POST['import_options'])) {
         $this->key_fname = array_search('first_name', $_POST['import_options']);
     }
     if (empty($this->key_fname) && in_array('billing_first_name', $_POST['import_options'])) {
         $this->key_fname = array_search('billing_first_name', $_POST['import_options']);
     }
     if (in_array('last_name', $_POST['import_options'])) {
         $this->key_lname = array_search('last_name', $_POST['import_options']);
     }
     if (empty($this->key_lname) && in_array('billing_last_name', $_POST['import_options'])) {
         $this->key_lname = array_search('billing_last_name', $_POST['import_options']);
     }
     if (in_array('user_nicename', $_POST['import_options'])) {
         $this->key_nice = array_search('user_nicename', $_POST['import_options']);
     }
     if (in_array('user_role', $_POST['import_options'])) {
         $this->key_role = array_search('user_role', $_POST['import_options']);
     }
     if (in_array('customer_status', $_POST['import_options'])) {
         $this->key_status = array_search('customer_status', $_POST['import_options']);
     }
     $skiped = false;
     while (($data = fgetcsv($import_data, 1000, ",")) !== FALSE) {
         if (isset($_POST['skip_first']) && $_POST['skip_first'] == 'yes' && !$skiped) {
             $skiped = true;
             continue;
         }
         $user_email = trim($data[$this->key_email]);
         if (empty($user_email) || email_exists($user_email)) {
             $this->not_import[] = $data;
             continue;
         }
         $nickname = '';
         if (empty($this->key_nice)) {
             if (isset($data[$this->key_fname])) {
                 $nickname .= sanitize_title($data[$this->key_fname]);
             }
             if (isset($data[$this->key_lname])) {
                 $nickname .= '_' . sanitize_title($data[$this->key_lname]);
             }
         } else {
             $nickname .= sanitize_title($data[$this->key_nice]);
         }
         $user_login = '';
         if (in_array('user_login', $_POST['import_options'])) {
             $key = array_search('user_login', $_POST['import_options']);
             $user_login = $data[$key];
         } else {
             $user_login = $this->get_user_login($user_email, $nickname);
         }
         //$password = wp_generate_password();
         add_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password');
         $user_id = wc_create_new_customer($user_email, $user_login);
         remove_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password');
         if (!empty($user_id) && !is_wp_error($user_id)) {
             if (empty($this->key_role) && isset($_POST['customer_role'])) {
                 wp_update_user(array('ID' => $user_id, 'role' => $_POST['customer_role']));
             }
             if (empty($this->key_status) && isset($_POST['customer_status'])) {
                 $status = $_POST['customer_status'];
                 wc_crm_change_customer_status($status, array($user_id));
             }
             foreach ($_POST['import_options'] as $f_key => $meta_key) {
                 if (empty($meta_key)) {
                     continue;
                 }
                 if ($meta_key == 'user_login' || $meta_key == 'user_email') {
                     continue;
                 }
                 if ($meta_key == 'url') {
                     wp_update_user(array('ID' => $user_id, 'user_url' => $data[$f_key]));
                     continue;
                 }
                 if ($meta_key == 'display_name') {
                     wp_update_user(array('ID' => $user_id, 'display_name' => $data[$f_key]));
                     continue;
                 }
                 if ($meta_key == 'wcrm_custom_meta') {
                     $custom_meta_key = $_POST['import_options_custom_meta'][$f_key];
                     update_user_meta($user_id, $custom_meta_key, $data[$f_key]);
                     continue;
                 }
                 if ($meta_key == 'user_nicename') {
                     wp_update_user(array('ID' => $user_id, 'user_nicename' => $data[$f_key]));
                     continue;
                 }
                 if ($meta_key == 'user_role') {
                     wp_update_user(array('ID' => $user_id, 'role' => $data[$f_key]));
                     continue;
                 }
                 if ($meta_key == 'customer_status') {
                     $status = $this->check_customer_status($data[$f_key]);
                     if (!$status) {
                         $status = $_POST['customer_status'];
                     }
                     wc_crm_change_customer_status($status, array($user_id));
                     continue;
                 }
                 if ($meta_key == 'industry') {
                     $industries = wc_crm_get_industries();
                     if (!in_array($data[$f_key], $industries)) {
                         continue;
                     }
                 }
                 if ($meta_key == 'user_group') {
                     //global $wpdb
                     $groups = $data[$f_key];
                     $groups = explode(',', $groups);
                     if (!empty($groups)) {
                         $group_ids = array();
                         foreach ($groups as $group_name) {
                             $group_slug = wc_sanitize_taxonomy_name(stripslashes($group_name));
                             $group_exists = wc_crm_group_exists($group_slug);
                             if (!$group_exists) {
                                 $group = array('group_name' => $group_name, 'group_slug' => $group_slug, 'group_type' => 'static');
                                 $wpdb->insert($wpdb->prefix . 'wc_crm_groups', $group);
                                 $group_ids[] = $wpdb->insert_id;
                                 $this->groups_added[] = $group_name;
                                 do_action('wc_crm_group_added', $wpdb->insert_id, $group);
                             } else {
                                 $group_ids[] = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->prefix}wc_crm_groups WHERE group_slug = %s LIMIT 1", $group_slug));
                             }
                         }
                         wc_crm_update_user_groups($group_ids, $user_email);
                     }
                     continue;
                 }
                 update_user_meta($user_id, $meta_key, $data[$f_key]);
             }
             $this->row++;
         }
     }
 }
 function save($user_id, $new = false, $order = false)
 {
     if (!$order) {
         if (!empty($user_id) && !is_wp_error($user_id)) {
             $ID = (int) $user_id;
             $old_user_data = WP_User::get_data_by('id', $ID);
             $user_data_up = array('ID' => $user_id, 'user_url' => $_POST['customer_site']);
             $errors = new WP_Error();
             if (empty($_POST['user_email'])) {
                 $errors->add('empty_email', __('<strong>ERROR</strong>: Please enter an e-mail address.'), array('form-field' => 'user_email'));
             } elseif (!is_email($_POST['user_email'])) {
                 $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.'), array('form-field' => 'user_email'));
             } elseif (($owner_id = email_exists($_POST['user_email'])) && $owner_id != $user_id) {
                 $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array('form-field' => 'user_email'));
             }
             $err = $errors->get_error_codes();
             if (!$err) {
                 $user_data_up['user_email'] = $_POST['user_email'];
                 $_SESSION['customer_save_errors'] = '';
             } else {
                 $_SESSION['customer_save_errors'] = $errors;
             }
             wp_update_user($user_data_up);
             $this->init_general_fields($user_id);
             if ($this->general_fields) {
                 foreach ($this->general_fields as $key => $field) {
                     if (!isset($field['type'])) {
                         $field['type'] = 'text';
                     }
                     if (!isset($field['meta_key'])) {
                         $field['meta_key'] = $key;
                     }
                     if (!isset($_POST[$key])) {
                         if ($field['type'] == 'multiselect') {
                             update_user_meta($user_id, $field['meta_key'], array());
                         }
                         continue;
                     }
                     switch ($key) {
                         case "customer_twitter":
                             $post_value = str_replace('@', '', $_POST['customer_twitter']);
                             break;
                         default:
                             $post_value = $_POST[$key];
                             break;
                     }
                     update_user_meta($user_id, $field['meta_key'], $post_value);
                 }
             }
             update_user_meta($user_id, 'preferred_payment_method', $_POST['_payment_method']);
             update_user_meta($user_id, 'billing_first_name', $_POST['_billing_first_name']);
             update_user_meta($user_id, 'billing_last_name', $_POST['_billing_last_name']);
             update_user_meta($user_id, 'billing_company', $_POST['_billing_company']);
             update_user_meta($user_id, 'billing_address_1', $_POST['_billing_address_1']);
             update_user_meta($user_id, 'billing_address_2', $_POST['_billing_address_2']);
             update_user_meta($user_id, 'billing_city', $_POST['_billing_city']);
             update_user_meta($user_id, 'billing_postcode', $_POST['_billing_postcode']);
             update_user_meta($user_id, 'billing_country', $_POST['_billing_country']);
             update_user_meta($user_id, 'billing_state', $_POST['_billing_state']);
             update_user_meta($user_id, 'billing_email', $_POST['_billing_email']);
             update_user_meta($user_id, 'billing_phone', $_POST['_billing_phone']);
             update_user_meta($user_id, 'payment_method', $_POST['_payment_method']);
             update_user_meta($user_id, 'shipping_first_name', $_POST['_shipping_first_name']);
             update_user_meta($user_id, 'shipping_last_name', $_POST['_shipping_last_name']);
             update_user_meta($user_id, 'shipping_company', $_POST['_shipping_company']);
             update_user_meta($user_id, 'shipping_address_1', $_POST['_shipping_address_1']);
             update_user_meta($user_id, 'shipping_address_2', $_POST['_shipping_address_2']);
             update_user_meta($user_id, 'shipping_city', $_POST['_shipping_city']);
             update_user_meta($user_id, 'shipping_postcode', $_POST['_shipping_postcode']);
             update_user_meta($user_id, 'shipping_country', $_POST['_shipping_country']);
             update_user_meta($user_id, 'shipping_state', $_POST['_shipping_state']);
             $group_ids = array();
             if (isset($_POST['wc_crm_customer_groups'])) {
                 $group_ids = $_POST['wc_crm_customer_groups'];
             }
             $user_email = $user_data_up['user_email'];
             wc_crm_update_user_groups($group_ids, $user_email);
             if (isset($_POST['account_name'])) {
                 $account_id = $_POST['account_name'];
                 add_post_meta($account_id, '_wc_crm_customer_email', $user_email);
             }
             // update the post (may even be a revision / autosave preview)
             do_action('acf/save_post', 'user_' . $user_id);
             do_action('acf/save_post', 'user_' . $user_id);
             do_action('profile_update', $user_id, $old_user_data);
             do_action('wc_crm_save_customer', $user_id, $old_user_data);
             if ($errors->get_error_codes()) {
                 var_dump($errors);
                 die;
                 return wp_redirect(add_query_arg(array("page" => "wc_new_customer", "user_id" => $user_id, "message" => 7), 'admin.php'));
             } elseif ($new) {
                 return wp_redirect(add_query_arg(array("page" => "wc-customer-relationship-manager", "message" => 1), 'admin.php'));
             } else {
                 return wp_redirect(add_query_arg(array("page" => "wc_new_customer", "user_id" => $user_id, "message" => 2), 'admin.php'));
             }
         } else {
             if (is_wp_error($user_id) && $user_id->get_error_codes()) {
                 $_SESSION['customer_save_errors'] = $user_id;
                 return wp_redirect(add_query_arg(array("page" => "wc_new_customer", "message" => 7), 'admin.php'));
             }
         }
     } else {
         global $wpdb;
         $ID = (int) $user_id;
         $group_ids = array();
         if (isset($_POST['wc_crm_customer_groups'])) {
             $group_ids = $_POST['wc_crm_customer_groups'];
         }
         $user_email = $wpdb->get_var("SELECT email FROM {$wpdb->prefix}wc_crm_customer_list WHERE order_id = {$ID} LIMIT 1");
         wc_crm_update_user_groups($group_ids, $user_email);
         if (isset($_POST['account_name'])) {
             $account_id = $_POST['account_name'];
             add_post_meta($account_id, '_wc_crm_customer_email', $user_email);
         }
         do_action('guest_update', $ID, $user_email);
         return wp_redirect(add_query_arg(array("page" => "wc_new_customer", "order_id" => $user_id, "message" => 2), 'admin.php'));
     }
 }