/**
  * Handles output of the Customer Groups page in admin.
  *
  * Shows the created groups and lets you add new ones or edit existing ones.
  * The added groups are stored in the database and can be used for layered navigation.
  */
 public static function do_actions()
 {
     global $wpdb;
     // Action to perform: add, edit, delete or none
     $action = '';
     if (!empty($_POST['wc_crm_add_new_group'])) {
         $action = 'add';
     } elseif (!empty($_POST['wc_crm_save_group']) && !empty($_GET['id'])) {
         $action = 'edit';
     } elseif (!empty($_GET['action']) && $_GET['action'] == 'delete') {
         $action = 'delete';
     } elseif (!empty($_POST['action']) && $_POST['action'] == 'delete' || !empty($_POST['action2']) && $_POST['action2'] == 'delete') {
         $action = 'delete_groups';
     }
     // Add or edit an group
     if ('add' === $action || 'edit' === $action) {
         // Security check
         if ('add' === $action) {
             check_admin_referer('wc-crm-add-new-group');
         }
         if ('edit' === $action) {
             $group_id = absint($_GET['id']);
         }
         // Grab the submitted data
         $group_name = isset($_POST['group_name']) ? (string) stripslashes($_POST['group_name']) : '';
         $group_slug = isset($_POST['group_slug']) ? wc_sanitize_taxonomy_name(stripslashes((string) $_POST['group_slug'])) : '';
         $group_type = isset($_POST['group_type']) ? (string) stripslashes($_POST['group_type']) : '';
         $group_total_spent_mark = isset($_POST['group_total_spent_mark']) ? (string) stripslashes($_POST['group_total_spent_mark']) : '';
         $group_total_spent = isset($_POST['group_total_spent']) ? (string) stripslashes($_POST['group_total_spent']) : '';
         $group_user_role = isset($_POST['group_user_role']) ? (string) stripslashes($_POST['group_user_role']) : '';
         $group_customer_status = isset($_POST['group_customer_status']) ? $_POST['group_customer_status'] : array();
         $group_product_categories = isset($_POST['group_product_categories']) ? $_POST['group_product_categories'] : array();
         $group_order_status = isset($_POST['group_order_status']) ? $_POST['group_order_status'] : array();
         $group_last_order = isset($_POST['group_last_order']) ? (string) stripslashes($_POST['group_last_order']) : '';
         $group_last_order_from = isset($_POST['group_last_order_from']) ? (string) stripslashes($_POST['group_last_order_from']) : '';
         $group_last_order_to = isset($_POST['group_last_order_to']) ? (string) stripslashes($_POST['group_last_order_to']) : '';
         // Auto-generate the label or slug if only one of both was provided
         if (!$group_name && $group_slug) {
             $group_name = ucfirst($group_slug);
         }
         if (!$group_slug && $group_name) {
             $group_slug = wc_sanitize_taxonomy_name(stripslashes($group_name));
         }
         // Forbidden group names
         // http://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms
         $reserved_terms = array('attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'cpage', 'day', 'debug', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'type', 'w', 'withcomments', 'withoutcomments', 'year');
         // Error checking
         if ('add' === $action) {
             if (!$group_name || !$group_slug || !$group_type) {
                 $error = __('Please, provide a group name, slug and type.', 'wc_crm');
             } elseif (strlen($group_name) >= 28) {
                 $error = sprintf(__('Slug “%s” is too long (28 characters max). Shorten it, please.', 'woocommerce'), sanitize_title($group_name));
             } elseif (in_array($group_name, $reserved_terms)) {
                 $error = sprintf(__('Slug “%s” is not allowed because it is a reserved term. Change it, please.', 'woocommerce'), sanitize_title($group_name));
             } elseif (in_array($group_name, $reserved_terms)) {
                 $error = sprintf(__('Slug “%s” is not allowed because it is a reserved term. Change it, please.', 'woocommerce'), sanitize_title($group_name));
             } else {
                 $group_exists = wc_crm_group_exists($group_slug);
                 if ('add' === $action && $group_exists) {
                     $error = sprintf(__('Slug “%s” is already in use. Change it, please.', 'woocommerce'), sanitize_title($group_name));
                 }
             }
         }
         /*if ( $group_type == 'dynamic' ) {
         			if( ! $group_total_spent ){
         				$error = __( 'Please, provide a Total Spent.', 'wc_crm' );
         			}else if( $group_last_order == 'between' && (!$group_last_order_from || !$group_last_order_to) ){
         				$error = __( 'Please, provide a Date.', 'wc_crm' );
         			}else if( $group_last_order != 'between' && !$group_last_order_from ){
         				$error = __( 'Please, provide a Date.', 'wc_crm' );
         			}
         		}*/
         // Show the error message if any
         if (!empty($error)) {
             wc_crm_add_notice($error, 'error');
         } else {
             // Add new group
             $group = array('group_type' => $group_type, 'group_total_spent_mark' => $group_total_spent_mark, 'group_total_spent' => $group_total_spent, 'group_user_role' => $group_user_role, 'group_customer_status' => serialize($group_customer_status), 'group_product_categories' => serialize($group_product_categories), 'group_order_status' => serialize($group_order_status), 'group_last_order' => $group_last_order, 'group_last_order_from' => $group_last_order_from, 'group_last_order_to' => $group_last_order_to);
             if ('add' === $action) {
                 $group['group_slug'] = $group_slug;
                 $group['group_name'] = $group_name;
                 $wpdb->insert($wpdb->prefix . 'wc_crm_groups', $group);
                 do_action('wc_crm_group_added', $wpdb->insert_id, $group);
                 wc_crm_add_notice(__('Group successfully added.', 'wc_crm'), 'success');
             }
             // Edit existing group
             if ('edit' === $action) {
                 $wpdb->update($wpdb->prefix . 'wc_crm_groups', $group, array('ID' => $group_id));
                 do_action('wc_crm_group_updated', $group_id, $group);
                 wc_crm_add_notice(__('Group successfully updated.', 'wc_crm'), 'success');
             }
             flush_rewrite_rules();
         }
     }
     // Delete an group
     if ('delete' === $action) {
         // Security check
         $group_id = absint($_GET['id']);
         $wpdb->query("DELETE FROM {$wpdb->prefix}wc_crm_groups WHERE ID = {$group_id}");
         do_action('wc_crm_group_deleted', $group_id);
         wc_crm_add_notice(__('Group deleted', 'wc_crm'), 'success');
     }
     // Delete an groups
     if ('delete_groups' === $action) {
         // Security check
         $ids = $_POST['id'];
         $count_groups = count($ids);
         $ids = implode(',', $ids);
         $wpdb->query("DELETE FROM {$wpdb->prefix}wc_crm_groups WHERE ID IN ({$ids})");
         do_action('wc_crm_group_deleted', $group_id);
         wc_crm_add_notice(sprintf(_n('%d Groups deleted.', '%d Groups deleted.', $count_groups, 'wc_crm'), $count_groups), 'success');
     }
 }
Example #2
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++;
         }
     }
 }