/**
  * Get a simple listing of the items in the queue.
  *
  * Supported filters:
  *  * user_id
  *  * user_email
  *  * order_id
  *  * product_id
  *  * email_id
  *  * cart
  *  * sent
  *  * status
  *  * date_sent_from RFC3339 date/time format
  *  * date_sent_to
  *  * date_scheduled_from
  *  * date_scheduled_to
  *  * limit
  *
  * @since 4.1
  * @param array $filter
  * @param int $page
  * @return array
  */
 public function get_queue_items($filter = array(), $page = 1)
 {
     $filter['page'] = $page;
     $args = array();
     $defaults = array('fields' => 'ids', 'user_id' => '', 'user_email' => '', 'order_id' => '', 'product_id' => '', 'email_id' => '', 'cart' => '', 'sent' => '', 'status' => '', 'date_sent_from' => '', 'date_sent_to' => '', 'date_scheduled_from' => '', 'date_scheduled_to' => '', 'limit' => get_option('posts_per_page'), 'page' => 1);
     $filter = wp_parse_args($filter, $defaults);
     foreach ($filter as $key => $value) {
         if ($value !== '') {
             switch ($key) {
                 case 'date_sent_from':
                     $args['date_sent']['from'] = get_date_from_gmt($this->server->parse_datetime($value));
                     break;
                 case 'date_sent_to':
                     $args['date_sent']['to'] = get_date_from_gmt($this->server->parse_datetime($value));
                     break;
                 case 'date_scheduled_from':
                     $args['send_on']['from'] = get_date_from_gmt($this->server->parse_datetime($value), 'U');
                     break;
                 case 'date_scheduled_to':
                     $args['send_on']['to'] = get_date_from_gmt($this->server->parse_datetime($value), 'U');
                     break;
                 default:
                     $args[$key] = $value;
             }
         }
     }
     $items = $this->scheduler->get_items($args);
     $total_rows = Follow_Up_Emails::instance()->wpdb->get_var("SELECT FOUND_ROWS()");
     $results = array();
     foreach ($items as $item_id) {
         $results[] = $this->get_queue_item($item_id);
     }
     // set the pagination data
     $query = array('page' => $page, 'single' => count($results) == 1, 'total' => $total_rows, 'total_pages' => ceil($total_rows / $args['limit']));
     $this->server->add_pagination_headers($query);
     return $results;
 }
 /**
  * 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;
 }