public function interval_string($string, $email)
 {
     if ($email->interval_type == 'before_tribe_event_starts' || $email->interval_type == 'after_tribe_event_ends') {
         $string = sprintf(__('%d %s %s'), $email->interval_num, FollowUpEmails::get_duration($email->interval_duration), FollowUpEmails::get_trigger_name($email->interval_type));
     }
     return $string;
 }
                ?>
</a></span>
                            </div>
                        </td>
                        <td>
                            <?php 
                $interval_str = '';
                $meta = maybe_unserialize($email->meta);
                if ($email->interval_type == 'signup') {
                    $interval_str = sprintf(__('%d %s after user signs up', 'follow_up_emails'), $email->interval_num, FollowUpEmails::get_duration($email->interval_duration, $email->interval_num));
                } elseif ($email->interval_type == 'order_total_above') {
                    $interval_str = sprintf(__('%d %s when %s %s%s'), $email->interval_num, FollowUpEmails::get_duration($email->interval_duration), FollowUpEmails::get_trigger_name($email->interval_type), get_woocommerce_currency_symbol(), $meta['order_total_above']);
                } elseif ($email->interval_type == 'order_total_below') {
                    $interval_str = sprintf(__('%d %s when %s %s%s'), $email->interval_num, FollowUpEmails::get_duration($email->interval_duration), FollowUpEmails::get_trigger_name($email->interval_type), get_woocommerce_currency_symbol(), $meta['order_total_below']);
                } elseif ($email->interval_duration != 'date') {
                    $interval_str = sprintf(__('%d %s %s'), $email->interval_num, FollowUpEmails::get_duration($email->interval_duration), FollowUpEmails::get_trigger_name($email->interval_type));
                } else {
                    $send_date = !empty($email->send_date_hour) ? $email->send_date . ' ' . $email->send_date_hour . ':' . $email->send_date_minute . ' ' . $meta['send_date_ampm'] : $email->send_date;
                    $interval_str = sprintf(__('Send on %s'), $send_date);
                }
                echo apply_filters('fue_interval_str', $interval_str, $email);
                ?>
                        </td>
                        <td>
                            <?php 
                echo $email->usage_count;
                ?>
                        </td>
                        <td>
                            <?php 
                echo $email->always_send == 1 ? __('Yes', 'follow_up_emails') : __('No', 'follow_up_emails');
 public function interval_string($string, $email)
 {
     if ($email->interval_type == 'points_greater_than') {
         $meta = maybe_unserialize($email->meta);
         $string = sprintf(__('%d %s %s %d'), $email->interval_num, FollowUpEmails::get_duration($email->interval_duration), FollowUpEmails::get_trigger_name($email->interval_type), $meta['points_greater_than']);
     }
     return $string;
 }
 /**
  * Send emails that are in the email queue
  */
 public static function send_emails()
 {
     global $wpdb;
     global $fue;
     // get start and end times
     $to = current_time('timestamp');
     $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM `{$wpdb->prefix}followup_email_orders` WHERE `is_sent` = 0 AND `send_on` <= %s", $to));
     foreach ($results as $email_order) {
         $sfn_report = array();
         $user_id = 0;
         if (!$email_order->email_id || $email_order->email_id == 0) {
             $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}followup_email_orders WHERE id = %d", $email_order->id));
             continue;
         }
         $email = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$wpdb->prefix}followup_emails` WHERE `id` = '%d'", $email_order->email_id));
         // email must be active
         if ($email->status != 1) {
             continue;
         }
         $email_meta = isset($email->meta) && !empty($email->meta) ? maybe_unserialize($email->meta) : array();
         $email_data = array('user_id' => '', 'email_to' => '', 'meta' => array());
         if ($email_order->user_id == 0 && $email->email_type == 'manual') {
             $email_data['meta'] = maybe_unserialize($email_order->meta);
             $email_data['email_to'] = $email_data['meta']['email_address'];
             $email_data['order'] = false;
             $email_data['cname'] = '';
         } else {
             $email_data['order'] = false;
             $email_data['user_id'] = $email_order->user_id;
             $wp_user = new WP_User($email_order->user_id);
             $email_data['email_to'] = $wp_user->user_email;
             $email_data['first_name'] = $wp_user->first_name;
             $email_data['last_name'] = $wp_user->last_name;
             $email_data['cname'] = $email_data['first_name'] . ' ' . $email_data['last_name'];
             if (empty($email_data['first_name']) && empty($email_data['last_name'])) {
                 $email_data['first_name'] = $wp_user->display_name;
                 $email_data['cname'] = $wp_user->display_name;
             }
             // non-order related email. make sure user is not opted-out
             $opt_out = get_user_meta($email_order->user_id, 'wcfu_opted_out', true);
             $opt_out = apply_filters('fue_user_opt_out', $opt_out, $email_order->user_id);
             if ($opt_out) {
                 // user opted out, delete this email_order
                 $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}followup_email_orders WHERE `id` = %d", $email_order->id));
                 continue;
             }
         }
         $email_data = apply_filters('fue_send_email_data', $email_data, $email_order, $email);
         // check if the email address is on the excludes list
         $sql = $wpdb->prepare("SELECT COUNT(*) FROM `{$wpdb->prefix}followup_email_excludes` WHERE `email` = '%s'", $email_data['email_to']);
         if ($wpdb->get_var($sql) > 0) {
             // delete and go to the next entry
             do_action('fue_email_excluded', $email_data['email_to'], $email_order->id);
             $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}followup_email_orders WHERE `id` = %d", $email_order->id));
             continue;
         }
         // allow other extensions to "skip" sending this email
         $skip = apply_filters('fue_skip_email_sending', false, $email, $email_order);
         if ($skip) {
             continue;
         }
         // process variable replacements
         $tracking = $email->tracking_code;
         $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');
         $page_id = fue_get_page_id('followup_unsubscribe');
         $unsubscribe = add_query_arg('fue', $email_data['email_to'], get_permalink($page_id));
         // convert urls
         $store_url = self::create_email_url($email_order->id, $email->id, $email_data['user_id'], $email_data['email_to'], $store_url);
         $unsubscribe = self::create_email_url($email_order->id, $email->id, $email_data['user_id'], $email_data['email_to'], $unsubscribe);
         if (!empty($codes)) {
             $store_url = add_query_arg($codes, $store_url);
             $unsubscribe = add_query_arg($codes, $unsubscribe);
         }
         $subject = $email->subject;
         $message = $email->message;
         if ($email->email_type == 'signup') {
             $vars = apply_filters('fue_email_signup_variables', array('{store_url}', '{store_name}', '{customer_first_name}', '{customer_name}', '{customer_email}', '{unsubscribe_url}'), $email_data, $email_order, $email);
             $reps = apply_filters('fue_email_signup_replacements', array($store_url, $store_name, $email_data['first_name'], $email_data['cname'], $email_data['email_to'], $unsubscribe), $email_data, $email_order, $email);
         } elseif ($email->email_type == 'manual') {
             $meta = maybe_unserialize($email_order->meta);
             $store_url = home_url();
             $store_name = get_bloginfo('name');
             $page_id = fue_get_page_id('followup_unsubscribe');
             $unsubscribe = add_query_arg('fue', $email_data['email_to'], get_permalink($page_id));
             // convert urls
             $store_url = self::create_email_url($email_order->id, $email->id, $email_order->user_id, $email_data['email_to'], $store_url);
             $unsubscribe = self::create_email_url($email_order->id, $email->id, $email_order->user_id, $email_data['email_to'], $unsubscribe);
             if (!empty($codes)) {
                 $store_url = add_query_arg($codes, $store_url);
                 $unsubscribe = add_query_arg($codes, $unsubscribe);
             }
             if ($email_order->user_id > 0) {
                 $first_name = get_user_meta($email_order->user_id, 'billing_first_name', true);
             } else {
                 // try to guess the first name
                 $names = explode(' ', $meta['user_name']);
                 $first_name = isset($names[0]) ? $names[0] : $meta['user_name'];
             }
             $vars = apply_filters('fue_email_manual_variables', array('{store_url}', '{store_name}', '{customer_first_name}', '{customer_name}', '{customer_email}', '{unsubscribe_url}'), $email_data, $email_order, $email);
             $reps = apply_filters('fue_email_manual_replacements', array($store_url, $store_name, $first_name, $meta['user_name'], $email_data['email_to'], $unsubscribe), $email_data, $email_order, $email);
             $subject = $meta['subject'];
             $message = $meta['message'];
         } else {
             $vars = apply_filters('fue_email_' . $email->email_type . '_variables', array('{store_url}', '{store_name}', '{unsubscribe_url}'), $email_data, $email_order, $email);
             $reps = apply_filters('fue_email_' . $email->email_type . '_replacements', array($store_url, $store_name, $unsubscribe), $email_data, $email_order, $email);
         }
         $subject = apply_filters('fue_email_subject', $subject, $email, $email_order);
         $message = apply_filters('fue_email_message', $message, $email, $email_order);
         $subject = strip_tags(str_replace($vars, $reps, $subject));
         $message = str_replace($vars, $reps, $message);
         $message = do_shortcode($message);
         // hook to variable replacement
         $subject = apply_filters('fue_send_email_subject', $subject, $email_order);
         $message = apply_filters('fue_send_email_message', $message, $email_order);
         // look for custom fields
         $message = preg_replace_callback('|\\{cf ([0-9]+) ([^}]*)\\}|', 'fue_add_custom_fields', $message);
         // look for post id
         $message = preg_replace_callback('|\\{post_id=([^}]+)\\}|', 'fue_add_post', $message);
         // look for links
         $replacer = new FUE_Link_Replacement($email_order->id, $email->id, $email_data['user_id'], $email_data['email_to']);
         $message = preg_replace_callback('|\\{link url=([^}]+)\\}|', array($replacer, 'replace'), $message);
         // look for store_url with path
         $fue->link_meta = array('email_order_id' => $email_order->id, 'email_id' => $email->id, 'user_id' => $email_data['user_id'], 'user_email' => $email_data['email_to'], 'codes' => $codes);
         $message = preg_replace_callback('|\\{store_url=([^}]+)\\}|', 'FUE::add_store_url', $message);
         $headers = array();
         $global_bcc = get_option('fue_bcc', '');
         $types_bcc = get_option('fue_bcc_types', array());
         $email_bcc = isset($email_meta['bcc']) && is_email($email_meta['bcc']) ? $email_meta['bcc'] : false;
         if ($email_bcc) {
             $headers[] = "Bcc: {$email_bcc}";
         } elseif (isset($types_bcc[$email->email_type]) && !empty($types_bcc[$email->email_type])) {
             $bcc = $types_bcc[$email->email_type];
             $headers[] = "Bcc: {$bcc}";
         } elseif (!empty($global_bcc) && is_email($global_bcc)) {
             $headers[] = "Bcc: {$global_bcc}";
         }
         // send the email
         do_action('fue_before_email_send', $subject, $message, $headers, $email_order);
         self::mail($email_data['email_to'], $subject, $message, $headers);
         do_action('fue_after_email_sent', $subject, $message, $headers, $email_order);
         // log this email
         if ($email->email_type == 'manual') {
             $email_trigger = __('Manual Email', 'follow_up_emails');
         } else {
             if ($email->interval_type == 'date') {
                 $email_trigger = sprintf(__('Send on %s'), $email->send_date . ' ' . $email->send_date_hour . ':' . $email->send_date_minute . ' ' . $email_meta['send_date_ampm']);
             } elseif ($email->interval_type == 'signup') {
                 $email_trigger = sprintf(__('%d %s after user signs up', 'follow_up_emails'), $email->interval_num, $email->interval_duration);
             } else {
                 $email_trigger = sprintf(__('%d %s %s'), $email->interval_num, $email->interval_duration, FollowUpEmails::get_trigger_name($email->interval_type));
             }
         }
         $email_trigger = apply_filters('fue_interval_str', $email_trigger, $email);
         do_action('fue_after_email_sent', $subject, $message, $email_order);
         do_action('fue_email_sent_details', $email_order, $email_order->user_id, $email, $email_data['email_to'], $email_data['cname'], $email_trigger);
         // increment usage count
         $wpdb->query($wpdb->prepare("UPDATE `{$wpdb->prefix}followup_emails` SET `usage_count` = `usage_count` + 1 WHERE `id` = %d", $email->id));
         // update the email order
         $now = date('Y-m-d H:i:s');
         $wpdb->query($wpdb->prepare("UPDATE `{$wpdb->prefix}followup_email_orders` SET `is_sent` = 1, `date_sent` = %s, `email_trigger` = %s WHERE `id` = %d", $now, $email_trigger, $email_order->id));
         do_action('fue_email_order_sent', $email_order->id);
     }
 }