/** * 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); }
/** * Listen for changes in payment dates and adjust the sending schedule of matching emails * * @param bool $is_set TRUE if a schedule has been set for the new date * @param int $next_payment Timestamp of the new payment date * @param string $subscription_key * @param int $user_id * @return bool The unchanged value of $is_set */ public function payment_date_changed($is_set, $next_payment, $subscription_key, $user_id) { // look for unsent emails in the queue matching this subscription $serialized_key = serialize(array('subs_key' => $subscription_key)); $serialized_key = str_replace('a:1:{', '', $serialized_key); $serialized_key = str_replace('}', '', $serialized_key); $scheduler = new FUE_Sending_Scheduler(Follow_Up_Emails::instance()); $items = $scheduler->get_items(array('is_sent' => 0, 'meta' => $serialized_key)); foreach ($items as $item) { $email = new FUE_Email($item->email_id); if ($email->trigger == 'subs_before_expire' || $email->trigger == 'subs_before_renewal') { // unschedule the email first $param = array('email_order_id' => $item->id); wc_unschedule_action('sfn_followup_emails', $param, 'fue'); // get the new sending schedule $new_timestamp = 0; if ($email->trigger == 'subs_before_expire') { $expiry_date = WC_Subscriptions_Manager::get_subscription_expiration_date($subscription_key, $user_id); if ($expiry_date) { // convert to local time $new_timestamp = get_date_from_gmt($expiry_date, 'U'); } } else { $renewal_date = WC_Subscriptions_Manager::get_next_payment_date($subscription_key, $user_id); if ($renewal_date) { // convert to local time $new_timestamp = get_date_from_gmt($renewal_date, 'U'); } } if ($new_timestamp) { // add this email to the queue $interval = (int) $email->interval_num; $add = FUE_Sending_Scheduler::get_time_to_add($interval, $email->interval_duration); $send_on = $new_timestamp - $add; // update the send_on value of the queue item $item->send_on = $send_on; $item->save(); // set a schedule using the new timestamp $scheduler->schedule_email($item->id, $send_on); } } } return $is_set; }