function wc_customer_relationship_manager_bulk_handler() { $group = ''; if (isset($_REQUEST['group']) && !empty($_REQUEST['group']) && $_REQUEST['group'] > 0) { $group = '&group=' . $_REQUEST['group']; } if (!isset($_REQUEST['action']) && !isset($_REQUEST['action2'])) { return; } $action = ''; $statuses = wc_crm_get_statuses_slug(); if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) { $action = $_REQUEST['action']; } elseif (isset($_REQUEST['action2']) && -1 != $_REQUEST['action2']) { $action = $_REQUEST['action2']; } if (!empty($action)) { if (array_key_exists($action, $statuses)) { if (!isset($_REQUEST['user_id']) || empty($_REQUEST['user_id'])) { return; } $user_ids = $_REQUEST['user_id']; wc_crm_change_customer_status($action, $user_ids); } else { if (strstr($action, 'crm_add_to_group_')) { if ((!isset($_REQUEST['user_id']) || empty($_REQUEST['user_id'])) && (!isset($_REQUEST['order_id']) || empty($_REQUEST['order_id']))) { return; } $filer = ''; if (!empty($_REQUEST['user_id'])) { $filer .= "user_id IN(" . implode(',', $_REQUEST['user_id']) . ")"; } if (!empty($_REQUEST['order_id'])) { if (!empty($filer)) { $filer .= ' OR '; } $filer .= "order_id IN(" . implode(',', $_REQUEST['order_id']) . ")"; } $group_id = substr($action, strlen('crm_add_to_group_')); global $wpdb; $cutomers = $wpdb->get_results("SELECT email FROM {$wpdb->prefix}wc_crm_customer_list WHERE {$filer}"); if (!empty($cutomers)) { $user_emails = array(); foreach ($cutomers as $cutomer) { $user_emails[] = $cutomer->email; } wc_crm_add_to_group($group_id, $user_emails); $_REQUEST['report_action'] = "added_to_group"; } } else { return; } } wp_redirect(admin_url() . 'admin.php?page=wc-customer-relationship-manager' . $group); } }
function delete_status() { global $wpdb; if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) { $id = $_REQUEST['id']; if (is_array($_REQUEST['id'])) { $filter = " WHERE status_id IN(" . implode(',', $id) . ") "; foreach ($id as $st_id) { $_status = wc_crm_get_status($st_id); $status = $_status['status_slug']; if ($status) { $user_sql = "SELECT user_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE status = '{$status}' "; $user_ids = array(); $users = $wpdb->get_results($user_sql); if (!empty($users)) { foreach ($users as $value) { $user_ids[] = $value->user_id; } wc_crm_change_customer_status('Customer', $user_ids); } } } } else { $filter = " WHERE status_id = {$id}"; $_status = wc_crm_get_status($id); $status = $_status['status_slug']; if ($status) { $user_sql = "SELECT user_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE status = '{$status}' "; $user_ids = array(); $users = $wpdb->get_results($user_sql); if (!empty($users)) { foreach ($users as $value) { $user_ids[] = $value->user_id; } wc_crm_change_customer_status('Customer', $user_ids); } } } $table = $wpdb->prefix . "wc_crm_statuses"; $sql = "DELETE FROM {$table} {$filter}"; $wpdb->query($sql); } }
/** * 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++; } } }