Example #1
0
 /**
  * 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);
 }