/**
  * 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);
 }
 /**
  * Sets the transient by chat_id
  *
  * Also responsible for pushing the updates to the respective Transient States
  *
  * In theory, this should be the only method that updates any PatchChat_Transient_State
  *
  * @author  caseypatrickdriscoll
  *
  * @created 2015-08-27 18:59:19
  * @edited  2015-08-28 12:30:12 - Refactors to push transient updates to state without move function
  * @edited  2015-08-29 13:57:00 - Adds bot for auto comments
  */
 public static function set($chat_id, $transient, $update_state = 1)
 {
     // 1. Set the individual chat transient
     set_transient('patchchat_' . $chat_id, $transient);
     if (!$update_state) {
         return;
     }
     // 2. Update all the 'new' transient state, which is an outlier
     if ($transient['status'] == 'new') {
         PatchChat_Transient_State::update('new', $transient);
     } else {
         PatchChat_Transient_State::trim('new', $chat_id);
     }
     // 3. Update all the individual user transients
     foreach ($transient['users'] as $user_id => $user) {
         if ($user_id == PatchChat_Settings::bot()) {
             continue;
         }
         PatchChat_Transient_State::update($user_id, $transient);
     }
 }
Ejemplo n.º 3
0
 /**
  * Permorms a number of housekeeping tasks at time of plugin installation
  *
  * 1. Creates a 'bot' user that will own auto comments
  *
  * @author  caseypatrickdriscoll
  *
  * @created  2015-08-29 13:57:00
  * 
  */
 public static function install()
 {
     PatchChat_Settings::bot();
 }