/**
  * Processes the form data.
  */
 public static function process_form()
 {
     global $wpdb;
     $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'];
     }
     foreach ($recipients as $r) {
         //if ( $mailer->send( $r, $subject, $message ) ){ //it doesn't return a success or failure
         $mailer->send($r, stripslashes($subject), stripslashes($message));
     }
     //save log
     $emails_ = $_POST['recipients'];
     $phones_ = $_POST['phones'];
     $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, %s, %s, %s)", $created, $created_gmt, $subject, $text, $emails_, $phones_, $type, get_current_user_id());
     if (!empty($insert)) {
         $wpdb->query("INSERT INTO {$table_name} (created, created_gmt, subject, message, user_email, phone, activity_type, user_id) VALUES " . $insert);
     }
     #			$rows_affected = $wpdb->insert( $table_name, array( 'created' => current_time('mysql'), 'subject' => $subject, 'message'=>addslashes($text),  'user_email' => $emails_, 'phone' => $phones_,  'activity_type' => $type,'user_id'=>get_current_user_id() ) );
 }
 /**
  * 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');
 }