/**
  * Change the send date of the email for 'before_renewal' and 'before_expire' triggers
  * @param array $insert
  * @param FUE_Email $email
  * @return array
  */
 public function modify_insert_send_date($insert, $email)
 {
     if ($email->type != 'subscription') {
         return $insert;
     }
     $order_id = $insert['order_id'];
     $order = WC_FUE_Compatibility::wc_get_order($order_id);
     if (!WC_Subscriptions_Order::order_contains_subscription($order)) {
         return $insert;
     }
     $subs_key = WC_Subscriptions_Manager::get_subscription_key($order_id);
     if ($email->trigger == 'subs_before_renewal') {
         $renewal_date = WC_Subscriptions_Manager::get_next_payment_date($subs_key);
         if (!$renewal_date) {
             // return false to tell FUE to skip importing this email/order
             return false;
         }
         // convert to local time
         $local_renewal_date = get_date_from_gmt($renewal_date, 'U');
         $add = FUE_Sending_Scheduler::get_time_to_add($email->interval, $email->duration);
         $insert['send_on'] = $local_renewal_date - $add;
     } elseif ($email->trigger == 'subs_before_expire') {
         $expiry_date = WC_Subscriptions_Manager::get_subscription_expiration_date($subs_key);
         if (!$expiry_date) {
             return false;
         }
         // convert to local time
         $expiry_timestamp = get_date_from_gmt($expiry_date, 'U');
         $add = FUE_Sending_Scheduler::get_time_to_add($email->interval, $email->duration);
         $insert['send_on'] = $expiry_timestamp - $add;
     }
     // Add the subscription key if it is not present in the meta
     if (!isset($insert['meta']) || empty($insert['meta']['subs_key'])) {
         $insert['meta']['subs_key'] = $subs_key;
     }
     return $insert;
 }
 /**
  * Add manual emails to the email queue
  *
  * $args keys:
  *  email_id    - ID of FUE_Email
  *  recipients  - Array of recipients ( format: [ [user_id, user_email, user_name] ] )
  *  subject     - Email Subject
  *  message     - Email Message
  *  tracking    - Analytics tracking code (e.g. ?utm_campaign=xxx)
  *  send_again  - Whether or not to send another email after a specific interval (bool)
  *  interval    - If send_again is true, the time to wait before sending the next email (int)
  *  interval_duration - If send_again is true, the duration type of interval (e.g. minutes/hours/weeks/months)
  *
  * @param array $args
  * @return array Array of Queue Item IDs created
  */
 public static function queue_manual_emails($args = array())
 {
     ignore_user_abort(true);
     set_time_limit(0);
     $item_ids = array();
     $args = wp_parse_args($args, array('email_id' => 0, 'recipients' => array(), 'subject' => '', 'message' => '', 'tracking' => '', 'schedule_email' => false, 'schedule_date' => '', 'schedule_hour' => '', 'schedule_minute' => '', 'schedule_ampm' => '', 'send_again' => false, 'interval' => '', 'interval_duration' => ''));
     extract($args);
     if (empty($recipients)) {
         return;
     }
     // process variable replacements
     $codes = array();
     if (!empty($tracking)) {
         parse_str($tracking, $codes);
         foreach ($codes as $key => $val) {
             $codes[$key] = urlencode($val);
         }
     }
     $store_url = home_url();
     $store_name = get_bloginfo('name');
     $orig_message = $message;
     $orig_subject = $subject;
     $recipient_num = 0;
     $send_time = current_time('timestamp');
     $email = new FUE_Email($email_id);
     $email_batch_enabled = get_option('fue_email_batches', 0);
     $emails_per_batch = get_option('fue_emails_per_batch', 100);
     $email_batch_interval = get_option('fue_batch_interval', 10);
     $schedule_emails = false;
     // figure out the sending schedule
     if ($schedule_email) {
         $send_time = strtotime($schedule_date . ' ' . $schedule_hour . ':' . $schedule_minute . ' ' . $schedule_ampm);
         $schedule_emails = true;
     }
     // if splitting emails into batches is enabled and more than one
     // batch is going to be queued, schedule the emails
     if ($email_batch_enabled && count($recipients) > $emails_per_batch) {
         $schedule_emails = true;
     }
     foreach ($recipients as $recipient) {
         $recipient_num++;
         // determine when to send this email
         if (1 == $email_batch_enabled) {
             if ($recipient_num == $emails_per_batch) {
                 $send_time += $email_batch_interval * 60;
                 $recipient_num = 0;
             }
         }
         // create an email order
         $user_id = $recipient[0];
         $email_address = $recipient[1];
         $user_name = $recipient[2];
         $unsubscribe = add_query_arg('fue', $email_address, fue_get_unsubscribe_url());
         $_message = $orig_message;
         $_subject = $orig_subject;
         $meta = array('recipient' => $recipient, 'user_id' => $recipient[0], 'email_address' => $recipient[1], 'user_name' => $recipient[2], 'subject' => $_subject, 'message' => $_message, 'codes' => $codes);
         $insert = array('user_id' => $user_id, 'email_id' => $email_id, 'user_email' => $email_address, 'send_on' => $send_time, 'email_trigger' => 'Manual Email', 'meta' => $meta);
         $queue_id = FUE_Sending_Scheduler::queue_email($insert, $email, $schedule_emails);
         if (!is_wp_error($queue_id)) {
             $item_ids[] = $queue_id;
         }
         if ($send_again && !empty($interval) && $interval > 0) {
             $add = FUE_Sending_Scheduler::get_time_to_add($interval, $interval_duration);
             // create an email order
             $email_data = array('user_id' => $user_id, 'email_id' => $email_id, 'user_email' => $email_address, 'send_on' => $send_time + $add, 'email_trigger' => 'Manual Email', 'meta' => serialize($meta));
             $queue_id = FUE_Sending_Scheduler::queue_email($email_data, $email, true);
             if (!is_wp_error($queue_id)) {
                 $item_ids[] = $queue_id;
             }
         }
     }
     return $item_ids;
 }
 /**
  * Queue reminder emails
  *
  * @param WC_Order $order
  * @return array
  */
 public function queue_reminder_emails($order)
 {
     $queued = array();
     $item_ids = $this->get_product_ids_from_order($order);
     $triggers = $this->get_order_triggers($order, Follow_Up_Emails::get_email_type('reminder'));
     $product_ids = array();
     $variation_ids = array();
     foreach ($item_ids as $item_id) {
         $product_ids[] = $item_id['product_id'];
         if ($item_id['variation_id']) {
             $variation_ids[] = $item_id['variation_id'];
         }
     }
     $product_ids = array_merge(array_unique($product_ids), array_unique($variation_ids));
     $args = array('meta_query' => array('relation' => 'AND', array('key' => '_interval_type', 'value' => $triggers, 'compare' => 'IN')));
     $emails = fue_get_emails('reminder', FUE_Email::STATUS_ACTIVE, $args);
     foreach ($emails as $email) {
         if ($email->product_id > 0 && !in_array($email->product_id, $product_ids)) {
             // Product ID does not match
             continue;
         }
         $queue_items = Follow_Up_Emails::instance()->scheduler->get_items(array('order_id' => $order->id, 'email_id' => $email->id));
         // only queue reminders once per order and email
         if (count($queue_items) == 0) {
             $interval = $email->interval;
             $interval_duration = $email->interval_duration;
             // get the item's quantity
             $qty = 0;
             $num_products = false;
             foreach ($order->get_items() as $item) {
                 $variation_id = $item['variation_id'];
                 $item_id = $item['product_id'];
                 if ($email->product_id == 0 || ($item_id == $email->product_id || $variation_id == $email->product_id)) {
                     $qty = $item['qty'];
                     if (isset($item['item_meta']) && !empty($item['item_meta'])) {
                         foreach ($item['item_meta'] as $meta_key => $meta_value) {
                             if ($meta_key == 'Filters/Case') {
                                 $num_products = $meta_value[0];
                                 break;
                             }
                         }
                     }
                 }
             }
             // look for a lifespan product variable
             $lifespan = get_post_meta($email->product_id, 'filter_lifespan', true);
             if ($lifespan && $lifespan > 0) {
                 $interval = (int) $lifespan;
                 $interval_duration = 'months';
             }
             if ($num_products !== false && $num_products > 0) {
                 $qty = $qty * $num_products;
             }
             if ($qty == 1) {
                 // only send the first email
                 $add = FUE_Sending_Scheduler::get_time_to_add($interval, $interval_duration);
                 $send_on = current_time('timestamp') + $add;
                 $insert = array('send_on' => $send_on, 'email_id' => $email->id, 'product_id' => $email->product_id, 'order_id' => $order->id);
                 if (!is_wp_error(FUE_Sending_Scheduler::queue_email($insert, $email))) {
                     $queued[] = $insert;
                 }
             } elseif ($qty == 2) {
                 // only send the first and last emails
                 $add = FUE_Sending_Scheduler::get_time_to_add($interval, $interval_duration);
                 $send_on = current_time('timestamp') + $add;
                 $insert = array('send_on' => $send_on, 'email_id' => $email->id, 'product_id' => $email->product_id, 'order_id' => $order->id);
                 if (!is_wp_error(FUE_Sending_Scheduler::queue_email($insert, $email))) {
                     $queued[] = $insert;
                 }
                 $last = FUE_Sending_Scheduler::get_time_to_add($interval, $interval_duration);
                 $send_on = current_time('timestamp') + $add + $last;
                 $insert = array('send_on' => $send_on, 'email_id' => $email->id, 'product_id' => $email->product_id, 'order_id' => $order->id);
                 if (!is_wp_error(FUE_Sending_Scheduler::queue_email($insert, $email))) {
                     $queued[] = $insert;
                 }
             } else {
                 // send all emails
                 $add = FUE_Sending_Scheduler::get_time_to_add($interval, $interval_duration);
                 $last = 0;
                 for ($x = 1; $x <= $qty; $x++) {
                     $send_on = current_time('timestamp') + $add + $last;
                     $last += $add;
                     $insert = array('send_on' => $send_on, 'email_id' => $email->id, 'product_id' => $email->product_id, 'order_id' => $order->id);
                     if (!is_wp_error(FUE_Sending_Scheduler::queue_email($insert, $email))) {
                         $queued[] = $insert;
                     }
                 }
             }
         }
     }
     return $queued;
 }
Example #4
0
 /**
  * Get the timestamp when the email will be sent that is relative to the current date/time
  * @param string $start_date The date to base the calculation on. Leave empty to use the current date
  *
  * @return int
  */
 public function get_send_timestamp($start_date = null)
 {
     $send_on = 0;
     if ($this->interval_type == 'date') {
         if (!empty($this->send_date_hour) && !empty($this->send_date_minute)) {
             $send_on = strtotime($this->send_date . ' ' . $this->send_date_hour . ':' . $this->send_date_minute);
             if (false === $send_on) {
                 // fallback to only using the date
                 $send_on = strtotime($this->send_date);
             }
         } else {
             $send_on = strtotime($this->send_date);
         }
     } else {
         $add = FUE_Sending_Scheduler::get_time_to_add($this->interval_num, $this->interval_duration);
         if (!is_null($start_date)) {
             $time_from = strtotime($start_date);
         } else {
             $time_from = current_time('timestamp');
         }
         $send_on = $time_from + $add;
     }
     return $send_on;
 }
 /**
  * Queue emails after an order is marked as completed
  * @param int $order_id
  */
 public function set_reminders($order_id)
 {
     global $woocommerce, $wpdb;
     // load reminder emails
     $emails = fue_get_emails('wootickets', FUE_Email::STATUS_ACTIVE, array('meta_query' => array(array('key' => '_interval_type', 'value' => array('before_tribe_event_starts', 'after_tribe_event_ends'), 'compare' => 'IN'))));
     $tickets = array();
     if (empty($emails)) {
         return;
     }
     $has_tickets = get_post_meta($order_id, '_tribe_has_tickets', true);
     $order = WC_FUE_Compatibility::wc_get_order($order_id);
     $items = $order->get_items();
     foreach ($items as $item) {
         $ticket_id = isset($item['id']) ? $item['id'] : $item['product_id'];
         // if $item is a ticket, load the event where the ticket is attached to
         $event_id = get_post_meta($ticket_id, '_tribe_wooticket_for_event', true);
         if (!$event_id) {
             continue;
         }
         if (!in_array($ticket_id, $tickets)) {
             $tickets[] = $ticket_id;
         }
     }
     $now = current_time('timestamp');
     foreach ($emails as $email) {
         $interval = (int) $email->interval_num;
         $add = FUE_Sending_Scheduler::get_time_to_add($interval, $email->interval_duration);
         foreach ($tickets as $ticket_id) {
             // if this email is for a specific ticket, make sure the IDs match
             if (!empty($email->product_id) && $email->product_id != $ticket_id) {
                 continue;
             }
             // check for category matching
             if (!empty($email->category_id)) {
                 $ticket_terms = get_the_terms($ticket_id, 'product_cat');
                 $product_categories = array();
                 if ($ticket_terms && !is_wp_error($ticket_terms)) {
                     foreach ($ticket_terms as $ticket_term) {
                         $product_categories[$ticket_term->term_id] = $ticket_term->name;
                     }
                 }
                 if (!array_key_exists($email->category_id, $product_categories)) {
                     continue;
                 }
             }
             $event_id = get_post_meta($ticket_id, '_tribe_wooticket_for_event', true);
             if ($email->interval_type == 'before_tribe_event_starts') {
                 $start = get_post_meta($event_id, '_EventStartDate', true);
                 if (empty($start)) {
                     continue;
                 }
                 $start = strtotime($start);
                 // check if a limit is in place
                 $email_meta = maybe_unserialize($email->meta);
                 if (isset($email_meta['tribe_limit'], $email_meta['tribe_limit_days']) && !empty($email_meta['tribe_limit_days'])) {
                     $days = ($start - $now) / 86400;
                     if ($days <= $email_meta['tribe_limit_days']) {
                         // $days is within limit - skip
                         continue;
                     }
                 }
                 $send_on = $start - $add;
                 // if send_on is in the past, do not queue it
                 if ($now > $send_on) {
                     continue;
                 }
             } else {
                 $end = get_post_meta($event_id, '_EventEndDate', true);
                 if (empty($end)) {
                     continue;
                 }
                 $end = strtotime($end);
                 $send_on = $end + $add;
                 // if send_on is in the past, do not queue it
                 if ($now > $send_on) {
                     continue;
                 }
             }
             $insert = array('user_id' => $order->user_id, 'order_id' => $order_id, 'product_id' => $ticket_id, 'email_id' => $email->id, 'send_on' => $send_on);
             FUE_Sending_Scheduler::queue_email($insert, $email);
         }
     }
 }
 /**
  * Send emails that matches the provided triggers to the queue
  * @param int $booking_id
  * @param array $triggers
  */
 private function create_email_order($booking_id, $triggers = array())
 {
     /**
      * @var $booking WC_Booking
      * @var $order WC_Order
      */
     $booking = get_wc_booking($booking_id);
     $last_status = get_post_meta($booking_id, '_last_status', true);
     $order = WC_FUE_Compatibility::wc_get_order($booking->order_id);
     $emails = fue_get_emails('wc_bookings', FUE_Email::STATUS_ACTIVE, array('meta_query' => array(array('key' => '_interval_type', 'value' => $triggers, 'compare' => 'IN'))));
     foreach ($emails as $email) {
         if (!empty($email->meta['bookings_last_status']) && $email->meta['bookings_last_status'] != $last_status) {
             continue;
         }
         if ($this->is_category_excluded($booking, $email)) {
             continue;
         }
         // A booking can have no order linked to it
         if ($order) {
             $customer = fue_get_customer_from_order($order);
             if (Follow_Up_Emails::instance()->fue_wc->wc_scheduler->exclude_customer_based_on_purchase_history($customer, $email)) {
                 continue;
             }
         }
         if ($email->interval_type == 'before_booking_event') {
             $start = strtotime(get_post_meta($booking_id, '_booking_start', true));
             $time = FUE_Sending_Scheduler::get_time_to_add($email->interval_num, $email->interval_duration);
             $send_on = $start - $time;
         } elseif ($email->interval_type == 'after_booking_event') {
             $start = strtotime(get_post_meta($booking_id, '_booking_end', true));
             $time = FUE_Sending_Scheduler::get_time_to_add($email->interval_num, $email->interval_duration);
             $send_on = $start + $time;
         } else {
             $send_on = $email->get_send_timestamp();
         }
         $insert = array('send_on' => $send_on, 'email_id' => $email->id, 'product_id' => $booking->product_id, 'order_id' => $booking->order_id, 'meta' => array('booking_id' => $booking_id));
         if ($order) {
             $user_id = WC_FUE_Compatibility::get_order_user_id($order);
             if ($user_id) {
                 $user = new WP_User($user_id);
                 $insert['user_id'] = $user_id;
                 $insert['user_email'] = $user->user_email;
             }
         }
         // Remove the nonce to avoid infinite loop because doing a
         // remove_action on WC_Bookings_Details_Meta_Box doesnt work
         unset($_POST['wc_bookings_details_meta_box_nonce']);
         if (!is_wp_error(FUE_Sending_Scheduler::queue_email($insert, $email))) {
             // Tell FUE that an email order has been created
             // to stop it from sending storewide emails
             if (!defined('FUE_ORDER_CREATED')) {
                 define('FUE_ORDER_CREATED', true);
             }
         }
     }
 }