/**
 * Adds a new ticket.
 *
 * @since	1.0
 * @param	arr		$ticket_data	Ticket data.
 * @return	mixed	Ticket ID on success, false on failure.
 */
function kbs_add_ticket($ticket_data)
{
    if (!empty($ticket_data['attachments']) && !is_array($ticket_data['attachments'])) {
        $ticket_data['attachments'] = array($ticket_data['attachments']);
    }
    $ticket_data = apply_filters('kbs_add_ticket_data', $ticket_data);
    $attachments = apply_filters('kbs_add_ticket_attachments', $ticket_data['attachments']);
    $ticket = new KBS_Ticket();
    $ticket->status = !empty($ticket_data['status']) ? $ticket_data['status'] : 'new';
    $ticket->ticket_title = $ticket_data['post_title'];
    $ticket->ticket_content = $ticket_data['post_content'];
    $ticket->agent = !empty($ticket_data['agent']) ? $ticket_data['agent'] : '';
    $ticket->user_info = $ticket_data['user_info'];
    $ticket->user_id = !empty($ticket_data['user_info']['id']) ? $ticket_data['user_info']['id'] : '';
    $ticket->email = $ticket_data['user_email'];
    $ticket->first_name = $ticket_data['user_info']['first_name'];
    $ticket->last_name = $ticket_data['user_info']['last_name'];
    $ticket->email = $ticket_data['user_info']['email'];
    $ticket->ip = kbs_get_ip();
    $ticket->sla = array('target_respond' => kbs_calculate_sla_target_response(), 'target_resolve' => kbs_calculate_sla_target_resolution());
    $ticket->source = '';
    $ticket->new_files = $ticket_data['attachments'];
    $ticket->form_data = !empty($ticket_data['form_data']) ? $ticket_data['form_data'] : '';
    if (isset($ticket_data['post_date'])) {
        $ticket->date = $ticket_data['post_date'];
    }
    do_action('kbs_before_add_ticket', $ticket->ID, $ticket_data);
    $ticket->save();
    do_action('kbs_add_ticket', $ticket->ID, $ticket_data);
    if (!empty($ticket->ID)) {
        return $ticket->ID;
    }
    // Return false if no ticket was inserted
    return false;
}
 /**
  * Create the base of a ticket.
  *
  * @since	1.0
  * @return	int|bool	False on failure, the ticket ID on success.
  */
 private function insert_ticket()
 {
     if (empty($this->ticket_title)) {
         $this->ticket_title = sprintf(__('New %s', 'kb-support'), kbs_get_ticket_label_singular());
     }
     if (empty($this->ip)) {
         $this->ip = kbs_get_ip();
         $this->pending['ip'] = $this->ip;
     }
     if (empty($this->key)) {
         $auth_key = defined('AUTH_KEY') ? AUTH_KEY : '';
         $this->key = strtolower(md5($this->email . date('Y-m-d H:i:s') . $auth_key . uniqid('kbs', true)));
         // Unique key
         $this->pending['key'] = $this->key;
     }
     if (!empty($this->form_data)) {
         $this->pending['form_data'] = $this->form_data;
     }
     $ticket_data = array('date' => $this->date, 'agent' => $this->agent, 'user_email' => $this->email, 'user_info' => array('id' => $this->user_id, 'email' => $this->email, 'first_name' => $this->first_name, 'last_name' => $this->last_name), 'sla' => $this->sla, 'status' => $this->status, 'source' => $this->source, 'files' => $this->new_files, 'form_data' => $this->form_data);
     $args = apply_filters('kbs_insert_ticket_args', array('post_status' => $this->status, 'post_title' => $this->ticket_title, 'post_content' => $this->ticket_content, 'post_type' => 'kbs_ticket', 'post_date' => !empty($this->date) ? $this->date : null, 'post_date_gmt' => !empty($this->date) ? get_gmt_from_date($this->date) : null), $ticket_data);
     // Create a blank ticket
     $ticket_id = wp_insert_post($args);
     if (!empty($ticket_id)) {
         $this->ID = $ticket_id;
         $this->_ID = $ticket_id;
         $customer = new stdClass();
         if (did_action('kbs_pre_process_ticket') && is_user_logged_in()) {
             $customer = new KBS_Customer(get_current_user_id(), true);
             // Customer is logged in but used a different email to log ticket with so assign to their customer record.
             if (!empty($customer->id) && $this->email != $customer->email) {
                 $customer->add_email($this->email);
             }
         }
         if (empty($customer->id)) {
             $customer = new KBS_Customer($this->email);
         }
         if (empty($customer->id)) {
             $customer_data = array('name' => $this->first_name . ' ' . $this->last_name, 'email' => $this->email, 'user_id' => $this->user_id);
             $customer->create($customer_data);
         }
         $this->customer_id = $customer->id;
         $this->pending['customer_id'] = $this->customer_id;
         $customer->attach_ticket($this->ID);
         if (!empty($this->new_files)) {
             $this->pending['files'] = $this->new_files;
         }
         $this->pending['sla'] = $this->sla;
         $this->ticket_meta = apply_filters('kbs_ticket_meta', $this->ticket_meta, $ticket_data);
         $this->update_meta('_ticket_data', $this->ticket_meta);
         $this->new = true;
     }
     return $this->ID;
 }