/** * 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'); } }
public static function create_user() { if (empty($_POST['user_email'])) { wc_crm_add_notice(__('Please enter an e-mail address.', 'wc_crm'), 'error'); } elseif (!is_email($_POST['user_email'])) { wc_crm_add_notice(__("The email address isn't correct.", 'wc_crm'), 'error'); } elseif (email_exists($_POST['user_email'])) { wc_crm_add_notice(__("This email is already registered, please choose another one.", 'wc_crm'), 'error'); } if (wc_crm_notice_count('error') > 0) { return; } global $wpdb; $nickname = str_replace(' ', '', ucfirst(strtolower($_POST['first_name']))) . str_replace(' ', '', ucfirst(strtolower($_POST['last_name']))); $username_opt = get_option('wc_crm_username_add_customer'); switch ($username_opt) { case 2: $username = str_replace(' ', '', strtolower($_POST['first_name'])) . '-' . str_replace(' ', '', strtolower($_POST['last_name'])); break; case 3: $username = $_POST['user_email']; break; default: $username = strtolower($nickname); break; } $username = _truncate_post_slug($username, 60); $check_sql = "SELECT user_login FROM {$wpdb->users} WHERE user_login = '******' LIMIT 1"; $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $username)); if ($user_name_check) { $suffix = 1; do { $alt_user_name = _truncate_post_slug($username, 60 - (strlen($suffix) + 1)) . "-{$suffix}"; $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_user_name)); $suffix++; } while ($user_name_check); $username = $alt_user_name; } add_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password'); $user_id = wc_create_new_customer($_POST['user_email'], $username); remove_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password'); do_action('wc_crm_create_customer', $user_id); if (!is_wp_error($user_id)) { update_user_meta($user_id, 'nickname', $nickname); wp_update_user(array('ID' => $user_id, 'role' => 'customer')); $customer_id = $wpdb->get_var("SELECT c_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE user_id = {$user_id} "); if ($customer_id) { WC_CRM_Screen_Customers_Edit::save($customer_id, true); } wc_crm_add_notice(__("Customer created.", 'wc_crm'), 'success'); wp_safe_redirect(admin_url() . 'admin.php?page=' . WC_CRM_TOKEN); } else { wc_crm_add_notice($user_id->get_error_message(), 'error'); } }
/** * Add notices for WP Errors * @param WP_Error $errors */ function wc_crm_add_wp_error_notices($errors) { if (is_wp_error($errors) && $errors->get_error_messages()) { foreach ($errors->get_error_messages() as $error) { wc_crm_add_notice($error, 'error'); } } }
/** * Processes the form data. */ public static function process_email_form() { global $wpdb; wc_crm_clear_notices(); $recipients = explode(',', $_POST['recipients']); $text = wpautop($_POST['emaileditor']); $subject = $_POST['subject']; if (!empty($_POST['from_email']) && filter_var($_POST['from_email'], FILTER_VALIDATE_EMAIL)) { add_filter('wp_mail_from', __CLASS__ . '::change_from_email', 9999); } if (!empty($_POST['from_name'])) { add_filter('wp_mail_from_name', __CLASS__ . '::change_from_name', 9999); } $mailer = WC()->mailer(); ob_start(); wc_crm_custom_woocommerce_get_template('emails/customer-send-email.php', array('email_heading' => $subject, 'email_message' => $text)); $message = ob_get_clean(); $order_ID = ''; if (isset($_GET['order_id']) && $_GET['order_id'] != '') { $order_ID = $_GET['order_id']; } //save log $emails_ = $_POST['recipients']; $type = "email"; $table_name = $wpdb->prefix . "wc_crm_log"; $created = current_time('mysql'); $created_gmt = get_gmt_from_date($created); $insert = $wpdb->prepare("(%s, %s, %s, %s, %s, %d)", $created, $created_gmt, $subject, $text, $type, get_current_user_id()); $wpdb->query("INSERT INTO {$table_name} (created, created_gmt, subject, message, activity_type, user_id) VALUES " . $insert); $log_id = $wpdb->insert_id; foreach ($recipients as $r) { $mailer->send($r, stripslashes($subject), stripslashes($message)); $result = $wpdb->get_results("SELECT c_id, user_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE email = '{$r}' LIMIT 1"); if ($result) { $customer = $result[0]; if ($customer->user_id > 0) { add_user_meta($customer->user_id, 'wc_crm_log_id', $log_id); } else { wc_crm_add_cmeta($customer->c_id, 'wc_crm_log_id', $log_id); } } } wc_crm_add_notice(__("Email sent.", 'wc_crm'), 'success'); }
public static function move_to_trash_activity($ids) { global $wpdb; $table_name = $wpdb->prefix . "wc_crm_log"; $logs_string = ''; if (is_array($ids)) { $count = count($ids); $logs_string = $id = implode(',', $ids); $wpdb->query("UPDATE {$table_name} SET log_status = 'trash' WHERE ID IN({$id}) "); } else { $logs_string = $ids; $n_ids = explode(',', $ids); $count = count($n_ids); $wpdb->query("UPDATE {$table_name} SET log_status = 'trash' WHERE ID IN({$ids}) "); } $undo_url = sprintf('<a href="?page=%s&action=untrash&log=%s' . (isset($_GET['c_id']) ? '&c_id=' . $_GET['c_id'] : '') . '">' . __('Undo', 'wc_crm') . '</a>', $_GET['page'], $logs_string); wc_crm_add_notice(sprintf(_n('%d post moved to the Trash.', '%d posts moved to the Trash.', $count, 'wc_crm'), $count) . ' ' . $undo_url, 'success'); }