/**
  * 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 save($customer_id, $new = false)
 {
     global $the_customer, $wpdb;
     $the_customer = new WC_CRM_Customer($customer_id);
     wc_crm_clear_notices();
     $data = array('status' => $_POST['customer_status']);
     $old_user_data = array();
     if ($the_customer->user_id > 0) {
         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 (($owner_id = email_exists($_POST['user_email'])) && $owner_id != $the_customer->user_id) {
             wc_crm_add_notice(__("This email is already registered, please choose another one.", 'wc_crm'), 'error');
         }
         if (wc_crm_notice_count('error') > 0) {
             return;
         }
         $old_user_data = WP_User::get_data_by('id', $the_customer->user_id);
         $user_data_up = array('ID' => $the_customer->user_id, 'user_url' => $_POST['customer_site'], 'user_email' => $_POST['user_email']);
         wp_update_user($user_data_up);
         $the_customer->init_general_fields();
         $the_customer->init_address_fields();
         if ($the_customer->general_fields) {
             foreach ($the_customer->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($the_customer->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($the_customer->user_id, $field['meta_key'], $post_value);
             }
         }
         if ($the_customer->billing_fields) {
             foreach ($the_customer->billing_fields as $key => $field) {
                 if (!isset($_POST['_billing_' . $key])) {
                     continue;
                 }
                 $post_value = $_POST['_billing_' . $key];
                 update_user_meta($the_customer->user_id, 'billing_' . $key, $post_value);
             }
         }
         if ($the_customer->shipping_fields) {
             foreach ($the_customer->shipping_fields as $key => $field) {
                 if (!isset($_POST['_shipping_' . $key])) {
                     continue;
                 }
                 $post_value = $_POST['_shipping_' . $key];
                 update_user_meta($the_customer->user_id, 'shipping_' . $key, $post_value);
             }
         }
         update_user_meta($the_customer->user_id, 'preferred_payment_method', $_POST['_payment_method']);
         update_user_meta($the_customer->user_id, 'payment_method', $_POST['_payment_method']);
         $group_ids = array();
         if (isset($_POST['wc_crm_customer_groups'])) {
             $group_ids = $_POST['wc_crm_customer_groups'];
         }
         $the_customer->update_groups($group_ids);
         if (isset($_POST['account_name'])) {
             $account_id = $_POST['account_name'];
             add_post_meta($account_id, '_wc_crm_customer_id', $customer_id);
         }
         $data = array('email' => $_POST['user_email'], 'first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name'], 'state' => $_POST['_billing_state'], 'city' => $_POST['_billing_city'], 'country' => $_POST['_billing_country'], 'status' => $_POST['customer_status']);
         $res = $wpdb->update("{$wpdb->prefix}wc_crm_customer_list", $data, array('c_id' => $the_customer->customer_id));
         do_action('profile_update', $the_customer->user_id, $old_user_data);
         // update the post (may even be a revision / autosave preview)
         do_action('acf/save_post', 'user_' . $the_customer->user_id);
         do_action('acf/save_post', 'user_' . $the_customer->user_id);
         do_action('wc_crm_save_customer', $the_customer->customer_id, $the_customer, $old_user_data);
     } else {
         if ($the_customer->customer_id > 0) {
             $the_customer->init_general_fields();
             $disabled = array('first_name', 'last_name', 'user_email', 'customer_status');
             if ($the_customer->general_fields) {
                 foreach ($the_customer->general_fields as $key => $field) {
                     if (in_array($key, $disabled)) {
                         continue;
                     }
                     if (!isset($field['type'])) {
                         $field['type'] = 'text';
                     }
                     if (!isset($field['meta_key'])) {
                         $field['meta_key'] = $key;
                     }
                     if (!isset($_POST[$key])) {
                         if ($field['type'] == 'multiselect') {
                             wc_crm_update_cmeta($the_customer->customer_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;
                     }
                     wc_crm_update_cmeta($the_customer->customer_id, $field['meta_key'], $post_value);
                 }
             }
             if (isset($_POST['account_name'])) {
                 $account_id = $_POST['account_name'];
                 add_post_meta($account_id, '_wc_crm_customer_email', $customer_id);
             }
             $res = $wpdb->update("{$wpdb->prefix}wc_crm_customer_list", $data, array('c_id' => $the_customer->customer_id));
             do_action('guest_update', $the_customer->customer_id, $the_customer->email);
         }
     }
     if ($new === false) {
         wc_crm_add_notice(__("Customer updated.", 'wc_crm'), 'success');
     }
 }
/**
 * Prints messages and errors which are stored in the session, then clears them.
 *
 * @since 2.1
 */
function wc_crm_print_notices()
{
    if (!did_action('woocommerce_init')) {
        _doing_it_wrong(__FUNCTION__, __('This function should not be called before woocommerce_init.', 'woocommerce'), '2.3');
        return;
    }
    $all_notices = wc_crm_get_notices();
    $notice_types = apply_filters('woocommerce_notice_types', array('error', 'success', 'notice'));
    foreach ($notice_types as $notice_type) {
        if (wc_crm_notice_count($notice_type) > 0) {
            foreach ($all_notices[$notice_type] as $message) {
                wc_crm_print_notice($message, $notice_type);
            }
        }
    }
    wc_crm_clear_notices();
}