/**
  * Default query arguments.
  *
  * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before
  * the query is run to convert them to the proper syntax.
  *
  * @access	public
  * @since	1.0
  * @param	arr		$args	The array of arguments that can be passed in and used for setting up this ticket query.
  */
 public function __construct($args = array())
 {
     $defaults = array('output' => 'tickets', 'post_type' => array('kbs_ticket'), 'start_date' => false, 'end_date' => false, 'number' => 20, 'page' => null, 'orderby' => 'ID', 'order' => 'DESC', 'user' => null, 'customer' => null, 'status' => kbs_get_ticket_status_keys(), 'meta_key' => null, 'year' => null, 'month' => null, 'day' => null, 's' => null, 'search_in_notes' => false, 'children' => false, 'fields' => null);
     $this->args = wp_parse_args($args, $defaults);
     $this->init();
 }
/**
 * Count Tickets
 *
 * Returns the total number of tickets.
 *
 * @since	1.0
 * @param	arr	$args	List of arguments to base the ticket count on
 * @return	arr	$count	Number of tickets sorted by ticket date
 */
function kbs_count_tickets($args = array())
{
    global $wpdb;
    $defaults = array('agent' => null, 'user' => null, 'customer' => null, 's' => null, 'start-date' => null, 'end-date' => null);
    $args = wp_parse_args($args, $defaults);
    $select = "SELECT p.post_status,count( * ) AS num_posts";
    $join = '';
    $where = "WHERE p.post_type = 'kbs_ticket'";
    // Count tickets for a search
    if (!empty($args['s'])) {
        if (is_email($args['s']) || strlen($args['s']) == 32) {
            if (is_email($args['s'])) {
                $field = '_kbs_ticket_client';
            }
            $join = "LEFT JOIN {$wpdb->postmeta} m ON (p.ID = m.post_id)";
            $where .= $wpdb->prepare("\n\t\t\t\tAND m.meta_key = %s\n\t\t\t\tAND m.meta_value = %s", $field, $args['s']);
        } elseif (is_numeric($args['s'])) {
            $join = "LEFT JOIN {$wpdb->postmeta} m ON (p.ID = m.post_id)";
            $where .= $wpdb->prepare("\n\t\t\t\tAND m.meta_key = '_mdjm_event_client'\n\t\t\t\tAND m.meta_value = %d", $args['s']);
        } else {
            $search = $wpdb->esc_like($args['s']);
            $search = '%' . $search . '%';
            $where .= $wpdb->prepare("AND ((p.post_title LIKE %s) OR (p.post_content LIKE %s))", $search, $search);
        }
    }
    // Limit ticket count by received date
    if (!empty($args['start-date']) && false !== strpos($args['start-date'], '-')) {
        $date_parts = explode('-', $args['start-date']);
        $year = !empty($date_parts[0]) && is_numeric($date_parts[0]) ? $date_parts[0] : 0;
        $month = !empty($date_parts[1]) && is_numeric($date_parts[1]) ? $date_parts[1] : 0;
        $day = !empty($date_parts[2]) && is_numeric($date_parts[2]) ? $date_parts[2] : 0;
        $is_date = checkdate($month, $day, $year);
        if (false !== $is_date) {
            $date = new DateTime($args['start-date']);
            $where .= $wpdb->prepare(" AND p.post_date >= '%s'", $date->format('Y-m-d'));
        }
        // Fixes an issue with the tickets list table counts when no end date is specified (partly with stats class)
        if (empty($args['end-date'])) {
            $args['end-date'] = $args['start-date'];
        }
    }
    if (!empty($args['end-date']) && false !== strpos($args['end-date'], '-')) {
        $date_parts = explode('-', $args['end-date']);
        $year = !empty($date_parts[0]) && is_numeric($date_parts[0]) ? $date_parts[0] : 0;
        $month = !empty($date_parts[1]) && is_numeric($date_parts[1]) ? $date_parts[1] : 0;
        $day = !empty($date_parts[2]) && is_numeric($date_parts[2]) ? $date_parts[2] : 0;
        $is_date = checkdate($month, $day, $year);
        if (false !== $is_date) {
            $date = new DateTime($args['end-date']);
            $where .= $wpdb->prepare(" AND p.post_date <= '%s'", $date->format('Y-m-d'));
        }
    }
    $where = apply_filters('kbs_count_tickets_where', $where);
    $join = apply_filters('kbs_count_tickets_join', $join);
    $query = "{$select}\n\t\tFROM {$wpdb->posts} p\n\t\t{$join}\n\t\t{$where}\n\t\tGROUP BY p.post_status\n\t";
    $cache_key = md5($query);
    $count = wp_cache_get($cache_key, 'counts');
    if (false !== $count) {
        return $count;
    }
    $count = $wpdb->get_results($query, ARRAY_A);
    $stats = array();
    $total = 0;
    $statuses = kbs_get_ticket_status_keys();
    foreach ($statuses as $state) {
        $stats[$state] = 0;
    }
    foreach ((array) $count as $row) {
        if (!in_array($row['post_status'], $statuses)) {
            continue;
        }
        $stats[$row['post_status']] = $row['num_posts'];
    }
    $stats = (object) $stats;
    wp_cache_set($cache_key, $stats, 'counts');
    return $stats;
}