/** * 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; } }
/** * 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; }
/** * 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); }
/** * 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; }