/** * Creates a patchchat post by * creating a user, * creating a new patchchat post, * creating first comment to post, * adding an 'instant reply' comment from admin, * building a new transient, * return new transient to new user * * @author caseypatrickdriscoll * * @edited 2015-08-03 16:32:16 - Adds user signon after creation * @edited 2015-08-28 20:11:39 - Adds PatchChat_Settings::instant_reply * @edited 2015-08-28 20:19:22 - Adds PatchChat_Settings::bot */ public static function create($patchchat) { $email = $patchchat['email']; $text = $patchchat['text']; $username = substr($email, 0, strpos($email, "@")); $password = wp_generate_password(10, false); $title = substr($text, 0, 40); $time = current_time('mysql'); $text = wp_strip_all_tags($text); /* Create User */ $user_id = wp_create_user($username, $password, $email); // TODO: Add the user's name to the user // TODO: Check to see if user logged in, no need to create again wp_new_user_notification($user_id, $password); $user = get_user_by('id', $user_id); $creds = array('user_login' => $user->user_login, 'user_password' => $password, 'remember' => true); $user_signon = wp_signon($creds, false); /* Create PatchChat Post */ $post = array('post_title' => $title, 'post_type' => 'patchchat', 'post_author' => $user_id, 'post_status' => 'new', 'post_date' => $time); $post_id = wp_insert_post($post); /* Create First Comment */ $comment = array('comment_post_ID' => $post_id, 'user_id' => $user_id, 'comment_content' => $text, 'comment_date' => $time, 'comment_author_IP' => $_SERVER['REMOTE_ADDR'], 'comment_agent' => $_SERVER['HTTP_USER_AGENT']); $comment_id = wp_insert_comment($comment); /* Insert default action comment reply */ $options = array('chatid' => $post_id, 'displayname' => $user->display_name); $comment = array('comment_post_ID' => $post_id, 'user_id' => PatchChat_Settings::bot(), 'comment_content' => PatchChat_Settings::instant_reply($options), 'comment_type' => 'auto', 'comment_date' => current_time('mysql')); $comment_id = wp_insert_comment($comment); // Will build the Transient PatchChat_Transient::get($post_id); return PatchChat_Controller::get_user_state($user_id); }
/** * 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; }
/** * 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; }