/** * Process request for sending a manual email * * @see FUE_Scheduler::queue_manual_emails */ public static function send_manual() { $post = array_map('stripslashes_deep', $_POST); $send_type = $post['send_type']; $recipients = array(); //format: array(user_id, email_address, name) if ($send_type == 'email') { $key = '0|' . $post['recipient_email'] . '|'; $recipients[$key] = array(0, $post['recipient_email'], ''); } elseif ($send_type == 'subscribers') { $subscribers = fue_get_subscribers(); foreach ($subscribers as $subscriber) { $key = '0|' . $subscriber . '|'; $recipients[$key] = array(0, $subscriber, ''); } } $recipients = apply_filters('fue_manual_email_recipients', $recipients, $post); if (!empty($recipients)) { $args = apply_filters('fue_manual_email_args', array('email_id' => $post['id'], 'recipients' => $recipients, 'subject' => $post['email_subject'], 'message' => $post['email_message'], 'tracking' => $post['tracking'], 'schedule_email' => isset($post['schedule_email']) && $post['schedule_email'] == 1 ? true : false, 'schedule_date' => $post['sending_schedule_date'], 'schedule_hour' => $post['sending_schedule_hour'], 'schedule_minute' => $post['sending_schedule_minute'], 'schedule_ampm' => $post['sending_schedule_ampm'], 'send_again' => isset($post['send_again']) && $post['send_again'] == 1 ? true : false, 'interval' => $post['interval'], 'interval_duration' => $post['interval_duration']), $post); // if the number of recipients exceed 50 and the email is set // to send immediately, use an AJAX worker to avoid timeouts if (!$args['schedule_email'] && count($recipients) > 50) { $key = $args['email_id'] . '-' . time(); set_transient('fue_manual_email_' . $key, $args, 86400); wp_redirect('admin.php?page=followup-emails&tab=send_manual_emails&key=' . $key); exit; } else { // clean up the schedule if enabled if ($args['schedule_email']) { if ($args['schedule_hour'] < 10) { $args['schedule_hour'] = '0' . $args['schedule_hour']; } if ($args['schedule_minute'] < 10) { $args['schedule_minute'] = '0' . $args['schedule_minute']; } $args['schedule_ampm'] = strtoupper($args['schedule_ampm']); } FUE_Sending_Scheduler::queue_manual_emails($args); do_action('sfn_followup_emails'); } } wp_redirect('admin.php?page=followup-emails&manual_sent=1#manual_mails'); exit; }
/** * Send manual emails in batches */ public static function send_manual_emails() { $wpdb = Follow_Up_Emails::instance()->wpdb; if (empty($_POST['cmd'])) { self::send_response(array('error' => 'CMD is missing')); } $cmd = $_POST['cmd']; $key = !empty($_POST['key']) ? $_POST['key'] : ''; $data = get_transient('fue_manual_email_' . $key); if (!$data) { self::send_response(array('error' => 'Data could not be loaded')); } if ($cmd == 'start') { self::send_response(array('total_emails' => count($data['recipients']))); } else { $recipients = $data['recipients']; // the number of emails to send in this batch $length = round(count($recipients) * 0.1); if ($length < 10) { $length = 10; } elseif ($length > 50) { $length = 50; } $recipients_part = array_splice($recipients, 0, $length); $args = $data; $args['recipients'] = $recipients_part; $queue_ids = FUE_Sending_Scheduler::queue_manual_emails($args); $send_data = array(); foreach ($queue_ids as $queue_id) { $queue_item = new FUE_Sending_Queue_Item($queue_id); $sending_result = Follow_Up_Emails::instance()->mailer->send_queue_item($queue_item); if (is_wp_error($sending_result)) { $send_data[] = array('status' => 'error', 'email' => $queue_item->user_email, 'error' => $sending_result->get_error_message()); } else { $send_data[] = array('status' => 'success', 'email' => $queue_item->user_email); } } $status = count($recipients) > 0 ? 'partial' : 'completed'; if ($status == 'completed') { delete_transient('fue_manual_email_' . $key); } else { // save the modified data $data['recipients'] = $recipients; set_transient('fue_manual_email_' . $key, $data, 86400); } self::send_response(array('status' => $status, 'data' => $send_data)); } }