/**
  * Checks if there is a match in the order's status and the email's trigger
  *
  * @param int|WC_Order  $order
  * @param int|FUE_Email $email
  * @return bool
  */
 private function order_status_matches_email_trigger($order, $email)
 {
     if (is_numeric($order)) {
         $order = WC_FUE_Compatibility::wc_get_order($order);
     }
     if (is_numeric($email)) {
         $email = new FUE_Email($email);
     }
     return WC_FUE_Compatibility::get_order_status($order) == $email->trigger;
 }
 /**
  * Get the triggers available for the given order based
  * on its status and the email type
  *
  * @param int|WC_Order      $order
  * @param FUE_Email_Type    $email_type
  * @return array
  */
 protected function get_order_triggers($order, $email_type = null)
 {
     if (is_numeric($order)) {
         $order = WC_FUE_Compatibility::wc_get_order($order);
     }
     $order_status = WC_FUE_Compatibility::get_order_status($order);
     $triggers = array($order_status);
     $triggers = apply_filters('fue_order_triggers', $triggers, $order->id, $email_type);
     return $triggers;
 }
 /**
  * Load all recipients matching the provided send type
  *
  * @param array $recipients
  * @param array $post
  *
  * @return array
  */
 public function get_manual_email_recipients($recipients, $post)
 {
     global $wpdb;
     $send_type = $post['send_type'];
     if ($send_type == 'users') {
         // Send to all users
         $users = get_users();
         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 == 'storewide') {
         // Send to all customers
         $users = get_users(array('role' => 'customer'));
         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 (explode(',', $post['recipients'][0]) 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 == 'product') {
         // customers who bought the selected products
         if (is_array($post['product_ids'])) {
             $post['product_ids'] = array_filter(array_map('intval', explode(',', $post['product_ids'][0])));
             if (WC_FUE_Compatibility::is_wc_version_gt('2.0')) {
                 // 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 = WC_FUE_Compatibility::wc_get_order($row->order_id);
                         if (!$order) {
                             continue;
                         }
                         // only on processing and completed orders
                         $order_status = WC_FUE_Compatibility::get_order_status($order);
                         if ($order_status != 'processing' && $order_status != 'completed') {
                             continue;
                         }
                         $order_user_id = WC_FUE_Compatibility::get_order_user_id($order) > 0 ? WC_FUE_Compatibility::get_order_user_id($order) : 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 = WC_FUE_Compatibility::wc_get_order($order_id);
                         if (!$order) {
                             continue;
                         }
                         // only on processing and completed orders
                         $order_status = WC_FUE_Compatibility::get_order_status($order);
                         if ($order_status != 'processing' && $order_status != 'completed') {
                             continue;
                         }
                         $order_user_id = WC_FUE_Compatibility::get_order_user_id($order) > 0 ? WC_FUE_Compatibility::get_order_user_id($order) : 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;
                             break;
                         }
                     }
                     // 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_row) {
                     // load the order and check the status
                     $order_id = $order_id_row->order_id;
                     $order = WC_FUE_Compatibility::wc_get_order($order_id);
                     if (!$order) {
                         continue;
                     }
                     // only on processing and completed orders
                     $order_status = WC_FUE_Compatibility::get_order_status($order);
                     if ($order_status != 'processing' && $order_status != 'completed') {
                         continue;
                     }
                     $order_user_id = WC_FUE_Compatibility::get_order_user_id($order) > 0 ? WC_FUE_Compatibility::get_order_user_id($order) : 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;
                     }
                 }
                 // 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) {
             $order_id = $order->ID;
             $order = WC_FUE_Compatibility::wc_get_order($order_id);
             if (!$order) {
                 continue;
             }
             $order_user_id = WC_FUE_Compatibility::get_order_user_id($order) > 0 ? WC_FUE_Compatibility::get_order_user_id($order) : 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;
             }
         }
     }
     return $recipients;
 }