/**
  * This builds a Transient Array by getting all the transients of user or type.
  *
  * There are two types of Transient Arrays
  *   - 'new'     is an array of all current chats with a 'new' status.
  *   - 'user_id' is an array of all current chats belonging to a user.
  *
  * It needs to know which transients to grab, which it can't do on name only
  *
  * We need a record of which transients are 'new' chats, which chats an agent belongs to, etc
  *
  * But absolutely, this function does not build single transients from WP_Querys
  *
  * @author caseypatrickdriscoll
  *
  * @edited 2015-08-04 13:35:48 - Refactors to query only user_id
  * @edited 2015-08-04 14:44:06 - Adds building of transient if missing
  *
  * @param $state_name - The 'name' of the state (new or user_id)
  *
  * @return array
  */
 public static function build($state_name)
 {
     // TODO: Should better design to differentiate between
     //       array_name == user_id and array_name == 'new'
     //       Need to generally design to build all sorts of array types
     $transient_state = array();
     $args = array('post_type' => 'patchchat', 'nopaging' => true, 'fields' => 'ids');
     // A 'new' array is indifferent to the author, just needs a new status
     if ($state_name == 'new') {
         $args['post_status'] = 'new';
     } else {
         // If it's not 'new' it's a user array
         $args['author'] = $state_name;
         $args['post_status'] = array('new', 'open');
     }
     $query = new WP_Query($args);
     $list = $query->get_posts();
     foreach ($list as $chat_id) {
         $transient = PatchChat_Transient::get($chat_id);
         array_push($transient_state, $transient);
     }
     PatchChat_Transient_State::set($state_name, $transient_state);
     return $transient_state;
 }
 /**
  * Updates a PatchChat_Transient, building it if it doesn't exist
  *
  * @author caseypatrickdriscoll
  *
  * @edited 2015-08-04 16:24:30 - Adds updating of 'new' Transient Array
  * @edited 2015-08-04 17:48:09 - Adds updating of user Transient State
  * @edited 2015-08-29 20:38:44 - Refactors to fix escaped quote problem
  *
  */
 public static function add_comment($comment)
 {
     $chat_id = $comment['comment_post_ID'];
     $user_id = $comment['user_id'];
     $transient = PatchChat_Transient::get($chat_id);
     // TODO: Really? Get to the bottom of this quote problem
     $comment_content = str_replace(array("\\\\", "\\'", "\\\""), array("\\", "'", "\""), $comment['comment_content']);
     array_push($transient['comments'], array('id' => $comment['comment_ID'], 'text' => $comment_content, 'time' => $comment['comment_date'], 'type' => $comment['comment_type'], 'user' => $comment['user_id']));
     if (!isset($transient['users'][$user_id])) {
         $transient['users'][$user_id] = self::generate_user_data($user_id);
     }
     PatchChat_Transient::set($chat_id, $transient);
     return $transient;
 }
 /**
  * Updates a patchchat by adding a comment
  *
  * @author caseypatrickdriscoll
  *
  * @edited 2015-08-04 15:02:36 - Adds updating of transient array
  * @edited 2015-08-29 16:43:15 - Refactors for more specialized performance and reusability
  *
  * @param Array $comment
  */
 private static function add_comment($comment)
 {
     $current_user = wp_get_current_user();
     if ($current_user->ID == 0) {
         return array('error' => 'User is not logged in');
     }
     $comment['user_id'] = $current_user->ID;
     $comment['comment_date'] = current_time('mysql');
     $comment['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
     $comment['comment_agent'] = $_SERVER['HTTP_USER_AGENT'];
     $comment_id = wp_insert_comment($comment);
     $comment['comment_ID'] = $comment_id;
     $transient = PatchChat_Transient::add_comment($comment);
     return true;
 }