/**
  * Routes the request to the correct page/file
  */
 public static function admin_controller()
 {
     $tab = isset($_GET['tab']) ? $_GET['tab'] : 'list';
     switch ($tab) {
         case 'list':
             self::list_emails_page();
             break;
         case 'edit':
             self::email_form(1, $_GET['id']);
             break;
         case 'send':
             $id = $_GET['id'];
             $email = new FUE_Email($id);
             if (!$email->exists()) {
                 wp_die("The requested data could not be found!");
             }
             self::send_manual_form($email);
             break;
         case 'send_manual_emails':
             self::send_manual_emails();
             break;
         case 'updater':
             self::updater_page();
             break;
         default:
             // allow add-ons to add tabs
             do_action('fue_admin_controller', $tab);
             break;
     }
 }
 /**
  * Trigger string for custom events
  *
  * @param string $string
  * @param FUE_Email $email
  * @return string
  */
 public function trigger_string($string, $email)
 {
     if ($email->trigger == 'before_tribe_event_starts' || $email->trigger == 'after_tribe_event_ends') {
         $type = $email->get_email_type();
         $string = sprintf(__('%d %s %s'), $email->interval, Follow_Up_Emails::get_duration($email->duration), $type->get_trigger_name($email->trigger));
     }
     return $string;
 }
 /**
  * Get a single email
  *
  * @since 4.1
  * @param int $id
  * @param array $fields
  * @return array
  */
 public function get_email($id, $fields = array())
 {
     // validate the email ID
     $id = $this->validate_request($id, 'follow_up_email', 'read');
     // Return the validate error.
     if (is_wp_error($id)) {
         return $id;
     }
     $email = new FUE_Email($id);
     $email_data = array('id' => $email->id, 'created_at' => $email->post->post_date, 'type' => $email->get_type(), 'template' => $email->template, 'name' => $email->name, 'subject' => $email->subject, 'message' => $email->message, 'status' => $this->fix_status_string($email->status, true), 'trigger' => $email->trigger, 'trigger_string' => $email->get_trigger_string(), 'interval' => $email->interval, 'duration' => $email->duration, 'always_send' => $email->always_send, 'product_id' => $email->product_id, 'category_id' => $email->category_id, 'campaigns' => wp_get_object_terms($email->id, 'follow_up_email_campaign', array('fields' => 'slugs')));
     return array('email' => apply_filters('fue_api_email_response', $email_data, $email, $fields, $this->server));
 }
 /**
  * Scan through the keys of $variables and apply the replacement if one is found
  * @param array     $variables
  * @param array     $email_data
  * @param object    $queue_item
  * @param FUE_Email $email
  * @return array
  */
 protected function add_variable_replacements($variables, $email_data, $queue_item, $email)
 {
     global $wpdb;
     $order = $queue_item->order_id != 0 ? WC_FUE_Compatibility::wc_get_order($queue_item->order_id) : false;
     $email = new FUE_Email($queue_item->email_id);
     $send_coupon = $email->send_coupon;
     $email_coupon_id = $email->coupon_id;
     if (!$email->exists() || !$send_coupon) {
         return $variables;
     }
     if ($queue_item->order_id != 0) {
         // order
         $order = WC_FUE_Compatibility::wc_get_order($queue_item->order_id);
         if (isset($order->user_id) && $order->user_id > 0) {
             $wp_user = new WP_User($order->user_id);
             $email_to = $wp_user->user_email;
         } else {
             $email_to = $order->billing_email;
         }
     } else {
         $wp_user = new WP_User($queue_item->user_id);
         $email_to = $wp_user->user_email;
     }
     $coupon_code = '';
     $coupon = false;
     if ($send_coupon == 1 && $email_coupon_id != 0) {
         $coupon = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}followup_coupons WHERE `id` = %d", $email_coupon_id));
         if (!$coupon) {
             $variables['coupon_code'] = '';
             $queue_item->add_note(sprintf('Warning: The email coupon (ID #%s) could not be found', $email_coupon_id));
             return $variables;
         }
         $coupon_code = FUE_Coupons::add_prefix(self::generate_coupon_code(), $coupon, $order);
         $coupon_array = array('post_title' => $coupon_code, 'post_author' => 1, 'post_date' => date("Y-m-d H:i:s"), 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_name' => $coupon_code, 'post_parent' => 0, 'menu_order' => 0, 'post_type' => 'shop_coupon');
         $coupon_id = wp_insert_post($coupon_array);
         $wpdb->query("UPDATE {$wpdb->prefix}posts SET post_status = 'publish' WHERE ID = {$coupon_id}");
         $expiry = '';
         if ($coupon->expiry_value > 0 && !empty($coupon->expiry_type)) {
             $exp = $coupon->expiry_value . ' ' . $coupon->expiry_type;
             $now = current_time('mysql');
             $ts = strtotime("{$now} +{$exp}");
             if ($ts !== false) {
                 $expiry = date('Y-m-d', $ts);
             }
         }
         update_post_meta($coupon_id, 'discount_type', $coupon->coupon_type);
         update_post_meta($coupon_id, 'coupon_amount', $coupon->amount);
         update_post_meta($coupon_id, 'individual_use', $coupon->individual == 0 ? 'no' : 'yes');
         update_post_meta($coupon_id, 'product_ids', $coupon->product_ids);
         update_post_meta($coupon_id, 'exclude_product_ids', $coupon->exclude_product_ids);
         update_post_meta($coupon_id, 'usage_limit', $coupon->usage_limit);
         update_post_meta($coupon_id, 'usage_limit_per_user', $coupon->usage_limit_per_user);
         update_post_meta($coupon_id, 'expiry_date', $expiry);
         update_post_meta($coupon_id, 'apply_before_tax', $coupon->before_tax == 0 ? 'no' : 'yes');
         update_post_meta($coupon_id, 'free_shipping', $coupon->free_shipping == 0 ? 'no' : 'yes');
         update_post_meta($coupon_id, 'exclude_sale_items', $coupon->exclude_sale_items == 0 ? 'no' : 'yes');
         update_post_meta($coupon_id, 'product_categories', maybe_unserialize($coupon->product_categories));
         update_post_meta($coupon_id, 'exclude_product_categories', maybe_unserialize($coupon->exclude_product_categories));
         update_post_meta($coupon_id, 'minimum_amount', $coupon->minimum_amount);
         update_post_meta($coupon_id, 'maximum_amount', $coupon->maximum_amount);
         $product_categories = '';
         $exclude_product_categories = '';
         if (!empty($coupon->product_categories)) {
             $product_categories = unserialize($coupon->product_categories);
         }
         update_post_meta($coupon_id, 'product_categories', $product_categories);
         if (!empty($coupon->exclude_product_categories)) {
             $exclude_product_categories = unserialize($coupon->exclude_product_categories);
         }
         update_post_meta($coupon_id, 'exclude_product_categories', $exclude_product_categories);
         $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}followup_coupons SET `usage_count` = `usage_count` + 1 WHERE `id` = %d", $coupon->id));
         FUE_Coupons::coupon_log($coupon_id, $coupon->coupon_name, $email->name, $email_to, $coupon_code);
         // record into the email_orders table
         $wpdb->query($wpdb->prepare("UPDATE `{$wpdb->prefix}followup_email_order_coupons` SET `coupon_name` = %s, `coupon_code` = %s WHERE `email_order_id` = %d", $coupon->coupon_name, $coupon_code, $queue_item->id));
         $variables['coupon_code'] = $coupon_code;
     }
     return $variables;
 }
 /**
  * Execute the requested bulk action on the selected emails
  * @param string    $action
  * @param array     $emails
  */
 public static function execute_bulk_action($action, $emails)
 {
     if (!is_array($emails) || empty($emails)) {
         return;
     }
     foreach ($emails as $email_id) {
         $email = new FUE_Email($email_id);
         switch ($action) {
             case 'activate':
                 $email->update_status(FUE_Email::STATUS_ACTIVE);
                 break;
             case 'deactivate':
                 $email->update_status(FUE_Email::STATUS_INACTIVE);
                 break;
             case 'archive':
                 $email->update_status(FUE_Email::STATUS_ARCHIVED);
                 break;
             case 'unarchive':
                 $email->update_status(FUE_Email::STATUS_ACTIVE);
                 break;
             case 'delete':
                 wp_delete_post($email_id, true);
                 break;
         }
     }
     do_action('fue_execute_bulk_action', $action, $emails);
 }
Exemple #6
0
 /**
  * Action that fires when the email type is changed in the email form
  */
 public static function update_email_type()
 {
     $id = absint($_POST['id']);
     $email = new FUE_Email($id);
     if ($email->exists()) {
         $args = array('ID' => $id, 'type' => $_POST['type']);
         fue_update_email($args);
     }
     self::send_response(array('status' => 'success'));
 }
 /**
  * Edit a queue item
  *
  * @since 4.1
  * @param int   $id
  * @param array $data
  * @return array
  */
 public function edit_queue_item($id, $data)
 {
     // validate the ID
     $id = $this->validate_request($id);
     // Return the validate error.
     if (is_wp_error($id)) {
         return $id;
     }
     $item = new FUE_Sending_Queue_Item($id);
     $data = apply_filters('fue_api_edit_queue_data', $data, $this);
     $schedule_changed = false;
     foreach ($data as $field => $value) {
         if ($field == 'send_date') {
             $item->send_on = get_date_from_gmt($this->server->parse_datetime($value), 'U');
             $schedule_changed = true;
         } elseif ($field == 'date_sent') {
             $item->date_sent = get_date_from_gmt($this->server->parse_datetime($value));
         } elseif ($field == 'email_id') {
             $email = new FUE_Email($value);
             if (!$email->exists()) {
                 return new WP_Error('fue_api_invalid_email_id', __('Invalid email ID', 'follow_up_emails'), array('status' => 400));
             }
         } else {
             if (property_exists($item, $field)) {
                 $item->{$field} = $value;
             }
         }
     }
     $id = $item->save();
     if ($schedule_changed && $item->status == 1) {
         // update the action-scheduler schedule
         $this->scheduler->unschedule_email($item->id);
         $this->scheduler->schedule_email($item->id, $item->send_on);
     }
     // Checks for an error in the saving process
     if (is_wp_error($id)) {
         return $id;
     }
     do_action('fue_api_edited_queue', $id, $data);
     $this->server->send_status(201);
     return $this->get_queue_item($id);
 }
 /**
  * Add an email to the queue
  *
  * @param array     $values
  * @param FUE_Email $email
  * @param bool      $schedule_event Pass false to not register a new scheduled event
  * @return int|WP_Error Queue ID or WP_Error on error
  */
 public static function queue_email($values, FUE_Email $email = null, $schedule_event = true)
 {
     $defaults = array('user_id' => '', 'user_email' => '', 'is_cart' => 0, 'email_id' => 0, 'meta' => '');
     $values = wp_parse_args($values, $defaults);
     if (empty($values['send_on'])) {
         $values['send_on'] = $email->get_send_timestamp();
     }
     if (!is_null($email)) {
         $values['email_id'] = $email->id;
     }
     return Follow_Up_Emails::instance()->scheduler->insert_email_order($values, $schedule_event);
 }
 /**
  * Get the name and trigger of the follow-up email
  * @param array $item
  * @return string
  */
 public function get_email_value($item)
 {
     $email = new FUE_Email($item['email_id']);
     if (!$email->exists()) {
         return '<em>deleted</em>';
     }
     if ($email->status != FUE_Email::STATUS_ACTIVE) {
         return sprintf(__('<a href="%s">#%d %s</a> - Inactive<br/><small>(%s)</small>', 'follow_up_emails'), admin_url('post.php?post=' . $item['email_id'] . '&action=edit'), $item['email_id'], $email->name, $email->get_trigger_string());
     } else {
         return sprintf(__('<a href="%s">#%d %s</a><br/><small>(%s)</small>', 'follow_up_emails'), admin_url('post.php?post=' . $item['email_id'] . '&action=edit'), $item['email_id'], $email->name, $email->get_trigger_string());
     }
     return sprintf(__('<a href="%s">#%d %s</a><br/><small>(%s)</small>', 'follow_up_emails'), 'admin.php?page=followup-emails-form&id=' . $item['email_id'], $item['email_id'], $email->name, $email->get_trigger_string());
 }
 /**
  * Set a flag to import existing orders that match the email after the email is created/activated
  *
  * The flag will be set if the following conditions are met:
  * - $data['meta']['import_order'] is set to 'yes'
  * - FUE_Email->meta['import_order'] is not 'yes'
  * - postmeta _imported_order is not 'yes'
  *
  * @param array $data
  * @param array $post
  * @return array
  */
 public function schedule_email_order_import($data)
 {
     if (empty($data['meta']['import_orders']) || $data['meta']['import_orders'] != 'yes') {
         return $data;
     }
     $email = new FUE_Email($data['ID']);
     if (!$email->exists()) {
         return $data;
     }
     if (!empty($email->imported_order) && $email->imported_order == 'yes') {
         return $data;
     }
     update_post_meta($email->id, '_import_order_flag', true);
     return $data;
 }
 /**
  * Archive a Date FUE_Email if there are no more emails of the same
  * kind that's unsent in the queue.
  *
  * The logic behind the archiving being that because it is date-based
  * and have passed its sending date already, there will be no
  * more new emails that can be created using this FUE_Email
  *
  * @param FUE_Email $email
  */
 public static function maybe_archive_email($email)
 {
     if ($email->interval_type != 'date' || $email->type == 'manual') {
         return;
     }
     // if there are no more unsent emails in the queue, archive this email
     $items = Follow_Up_Emails::instance()->scheduler->get_items(array('email_id' => $email->id, 'is_sent' => 0));
     if (count($items) == 0) {
         $email->update_status(FUE_Email::STATUS_ARCHIVED);
     }
 }
 /**
  * Format the trigger string that is displayed in the email reports
  *
  * @param string    $string
  * @param FUE_Email $email
  *
  * @return string
  */
 public function trigger_string($string, $email)
 {
     if ($email->trigger == 'points_greater_than') {
         $email_type = $email->get_email_type();
         $meta = maybe_unserialize($email->meta);
         $string = sprintf(__('%d %s %s %d'), $email->interval, Follow_Up_Emails::get_duration($email->duration, $email->interval), $email_type->get_trigger_name($email->trigger), $meta['points_greater_than']);
     }
     return $string;
 }