Ejemplo n.º 1
0
 /**
  * Return this emails FUE_Email_Type object
  *
  * @return FUE_Email_Type
  */
 public function get_email_type()
 {
     return Follow_Up_Emails::get_email_type($this->get_type());
 }
 /**
  * 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;
 }
Ejemplo n.º 3
0
/**
 * Create a new, or update an existing FUE_Email. If ID is passed, it will update
 * the email with the matching ID.
 *
 * $args
 *  - name: Name of the email
 *  - type (e.g. storewide, subscription, etc)
 *  - subject: Email Subject
 *  - message: Email Content
 *  - status (default: fue-inactive)
 *  - priority
 *
 * Other keys can be passed to $args and they will be saved as postmeta
 *
 * @param array $args Optional array of arguments
 * @return int|WP_Error The new email ID or WP_Error on error
 */
function fue_save_email($args)
{
    $post_args = array('post_type' => 'follow_up_email');
    $updating = false;
    // support both id and ID
    if (isset($args['id'])) {
        $args['ID'] = $args['id'];
        unset($args['id']);
    }
    if (!empty($args['ID'])) {
        // update email
        $post_args['ID'] = $args['ID'];
        $updating = true;
        $email_id = $args['ID'];
    } else {
        $args['ID'] = 0;
    }
    // only save if the email type is valid
    if (!empty($args['type'])) {
        $email_type = Follow_Up_Emails::get_email_type($args['type']);
        if ($email_type === false) {
            return new WP_Error('fue_save_email', sprintf(__('Invalid email type passed (%s)', 'follow_up_emails'), $args['type']));
        }
    }
    $args = apply_filters('fue_email_pre_save', $args, $args['ID']);
    if (isset($args['name'])) {
        $post_args['post_title'] = $args['name'];
    }
    if (isset($args['subject'])) {
        $post_args['post_excerpt'] = $args['subject'];
    }
    if (isset($args['message'])) {
        $post_args['post_content'] = $args['message'];
    }
    if (isset($args['status'])) {
        $post_args['post_status'] = $args['status'];
    }
    if (isset($args['priority'])) {
        $post_args['menu_order'] = $args['priority'];
    }
    if ($updating) {
        wp_update_post($post_args);
        $email_id = $args['ID'];
    } else {
        $email_id = wp_insert_post($post_args);
    }
    if (is_wp_error($email_id)) {
        return $email_id;
    }
    // set email type
    if (isset($args['type'])) {
        $type = $args['type'];
        wp_set_object_terms($email_id, $type, 'follow_up_email_type', false);
        // data cleanup
        switch ($args['type']) {
            case 'customer':
                $args['product_id'] = 0;
                $args['category_id'] = 0;
                break;
            case 'signup':
                $args['product_id'] = 0;
                $args['category_id'] = 0;
                $args['always_send'] = 1;
                $args['interval_type'] = 'signup';
                break;
            case 'manual':
                $args['interval_type'] = 'manual';
                $args['interval_duration'] = 0;
                break;
            case 'reminder':
                $args['always_send'] = 1;
                break;
        }
    }
    // update campaigns
    if (isset($args['campaign'])) {
        if (empty($args['campaign'])) {
            // trying to remove campaigns
            wp_set_object_terms($email_id, null, 'follow_up_email_campaign');
        } else {
            if (is_array($args['campaign'])) {
                $campaigns = $args['campaign'];
            } else {
                $campaigns = array_filter(array_map('trim', explode(',', $args['campaign'])));
            }
            wp_set_object_terms($email_id, $campaigns, 'follow_up_email_campaign');
        }
    }
    // Always Send always defaults to 0
    if (isset($args['always_send']) && empty($args['always_send'])) {
        $args['always_send'] = 0;
    }
    // empty product and category IDs must always be 0
    if (isset($args['product_id']) && empty($args['product_id'])) {
        $args['product_id'] = 0;
    }
    if (isset($args['category_id']) && empty($args['category_id'])) {
        $args['category_id'] = 0;
    }
    // unset the already processed keys and store the remaining keys as postmeta
    unset($args['name'], $args['subject'], $args['message'], $args['status'], $args['type'], $args['ID'], $args['priority']);
    if (isset($args['tracking_on']) && $args['tracking_on'] == 0) {
        $args['tracking_code'] = '';
    }
    if (isset($args['interval_duration']) && $args['interval_duration'] == 'date') {
        $args['interval_type'] = 'date';
    }
    foreach ($args as $meta_key => $meta_value) {
        // merge the meta field
        if ($meta_key == 'meta') {
            $meta_value = maybe_unserialize($meta_value);
            $old_meta = get_post_meta($email_id, '_meta', true);
            if (!is_array($old_meta)) {
                $old_meta = maybe_unserialize($old_meta);
            }
            if (is_array($old_meta)) {
                $meta_value = array_merge($old_meta, $meta_value);
            }
        }
        update_post_meta($email_id, '_' . $meta_key, $meta_value);
    }
    if ($updating) {
        do_action('fue_email_updated', $email_id, $args);
    } else {
        do_action('fue_email_created', $email_id, $args);
    }
    return $email_id;
}