function send_manual()
 {
     global $wpdb;
     $post = array_map('stripslashes_deep', $_POST);
     $send_type = $post['send_type'];
     $recipients = array();
     //format: array(user_id, email_address, name)
     if ($send_type == 'storewide') {
         // Send to all customers
         $users = $wpdb->get_results("SELECT u.ID, u.user_email, u.display_name FROM {$wpdb->prefix}users u, {$wpdb->prefix}usermeta um WHERE u.ID = um.user_id AND um.meta_key = 'paying_customer' AND um.meta_value = 1");
         foreach ($users as $user) {
             $key = $user->ID . '|' . $user->user_email . '|' . $user->display_name;
             $value = array($user->ID, $user->user_email, $user->display_name);
             if (!isset($recipients[$key])) {
                 $recipients[$key] = $value;
             }
         }
     } elseif ($send_type == 'customer') {
         // individual email addresses
         if (count($post['recipients']) > 0) {
             foreach ($post['recipients'] as $key) {
                 $data = explode('|', $key);
                 if (3 == count($data)) {
                     $value = array($data[0], $data[1], $data[2]);
                     if (!isset($recipients[$key])) {
                         $recipients[$key] = $value;
                     }
                 }
             }
         }
     } elseif ($send_type == 'email') {
         $key = '0|' . $post['recipient_email'] . '|';
         $recipients[$key] = array(0, $post['recipient_email'], '');
     } elseif ($send_type == 'product') {
         // customers who bought the selected products
         if (is_array($post['product_ids'])) {
             if (function_exists('get_product')) {
                 // if WC >= 2.0, do a direct query
                 foreach ($post['product_ids'] as $product_id) {
                     $order_ids = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT order_id FROM {$wpdb->prefix}followup_order_items WHERE product_id = %d", $product_id));
                     foreach ($order_ids as $row) {
                         $order = new WC_Order($row->order_id);
                         // only on processing and completed orders
                         if ($order->status != 'processing' && $order->status != 'completed') {
                             continue;
                         }
                         $order_user_id = $order->user_id > 0 ? $order->user_id : 0;
                         $key = $order_user_id . '|' . $order->billing_email . '|' . $order->billing_first_name . ' ' . $order->billing_last_name;
                         $value = array($order_user_id, $order->billing_email, $order->billing_first_name . ' ' . $order->billing_last_name);
                         if (!isset($recipients[$key])) {
                             $recipients[$key] = $value;
                         }
                     }
                 }
             } else {
                 foreach ($post['product_ids'] as $product_id) {
                     $order_ids = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT order_id FROM {$wpdb->prefix}followup_order_items WHERE product_id = %d", $product_id));
                     foreach ($order_ids as $order_id) {
                         // load the order and check the status
                         $order = new WC_Order($order_id->order_id);
                         // only on processing and completed orders
                         if ($order->status != 'processing' && $order->status != 'completed') {
                             continue;
                         }
                         $order_user_id = $order->user_id > 0 ? $order->user_id : 0;
                         $key = $order_user_id . '|' . $order->billing_email . '|' . $order->billing_first_name . ' ' . $order->billing_last_name;
                         $value = array($order_user_id, $order->billing_email, $order->billing_first_name . ' ' . $order->billing_last_name);
                         if (!isset($recipients[$key])) {
                             $recipients[$key] = $value;
                             continue;
                         }
                     }
                     // endforeach ( $order_items_result as $order_items )
                 }
             }
             // endif: function_exists('get_product')
         }
         // endif: is_array($post['product_ids'])
     } elseif ($send_type == 'category') {
         // customers who bought products from the selected categories
         if (is_array($post['category_ids'])) {
             foreach ($post['category_ids'] as $category_id) {
                 $order_ids = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT order_id FROM {$wpdb->prefix}followup_order_categories WHERE category_id = %d", $category_id));
                 foreach ($order_ids as $order_id) {
                     // load the order and check the status
                     $order = new WC_Order($order_id->order_id);
                     // only on processing and completed orders
                     if ($order->status != 'processing' && $order->status != 'completed') {
                         continue;
                     }
                     $order_user_id = $order->user_id > 0 ? $order->user_id : 0;
                     $key = $order_user_id . '|' . $order->billing_email . '|' . $order->billing_first_name . ' ' . $order->billing_last_name;
                     $value = array($order_user_id, $order->billing_email, $order->billing_first_name . ' ' . $order->billing_last_name);
                     if (!isset($recipients[$key])) {
                         $recipients[$key] = $value;
                         continue;
                     }
                 }
                 // endforeach ( $order_items_result as $order_items )
             }
         }
         // endif: is_array($post['product_ids'])
     } elseif ($send_type == 'timeframe') {
         $from_ts = strtotime($post['timeframe_from']);
         $to_ts = strtotime($post['timeframe_to']);
         $from = date('Y-m-d', $from_ts) . ' 00:00:00';
         $to = date('Y-m-d', $to_ts) . ' 23:59:59';
         $order_ids = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_type = 'shop_order' AND post_status = 'publish' AND post_date BETWEEN %s AND %s", $from, $to));
         foreach ($order_ids as $order_id) {
             $order = new WC_Order($order_id->ID);
             $order_user_id = $order->user_id > 0 ? $order->user_id : 0;
             $key = $order_user_id . '|' . $order->billing_email . '|' . $order->billing_first_name . ' ' . $order->billing_last_name;
             $value = array($order_user_id, $order->billing_email, $order->billing_first_name . ' ' . $order->billing_last_name);
             if (!isset($recipients[$key])) {
                 $recipients[$key] = $value;
                 continue;
             }
         }
     }
     if (!empty($recipients)) {
         $args = array('email_id' => $post['id'], 'recipients' => $recipients, 'subject' => $post['email_subject'], 'message' => $post['email_message'], 'tracking' => $post['tracking'], 'interval' => $post['interval'], 'interval_duration' => $post['interval_duration']);
         FUE::send_manual_emails($args);
     }
     wp_redirect('admin.php?page=wc-followup-emails&manual_sent=1#manual_mails');
 }
 public static function send_manual()
 {
     global $wpdb;
     $post = array_map('stripslashes_deep', $_POST);
     $send_type = $post['send_type'];
     $recipients = array();
     //format: array(user_id, email_address, name)
     if ($send_type == 'email') {
         $key = '0|' . $post['recipient_email'] . '|';
         $recipients[$key] = array(0, $post['recipient_email'], '');
     }
     $recipients = apply_filters('fue_send_manual_emails', $recipients, $post);
     if (!empty($recipients)) {
         $args = apply_filters('fue_manual_email_args', array('email_id' => $post['id'], 'recipients' => $recipients, 'subject' => $post['email_subject'], 'message' => $post['email_message'], 'tracking' => $post['tracking'], 'send_again' => isset($post['send_again']) && $post['send_again'] == 1 ? true : false, 'interval' => $post['interval'], 'interval_duration' => $post['interval_duration']), $post);
         FUE::send_manual_emails($args);
     }
     wp_redirect('admin.php?page=followup-emails&manual_sent=1#manual_mails');
 }