/** * Get json from the server * * Previously, there were many types of things to GET * * Now, the app is built in a way that only needs to 'get' the current state, * an array of chats the user belongs to * * The controller handles what that is, depending on the user * * TODO: Is a nonce needed here? I'm not sure how it will help * * @author caseypatrickdriscoll * * @edited 2015-08-03 14:47:59 - Adds logged in validation * @edited 2015-08-03 14:52:01 - Adds current_user validation * @edited 2015-08-20 15:34:17 - Refactors to clean up AJAX get * */ public static function get() { if (!is_user_logged_in()) { wp_send_json_error('Not logged in'); } $current_user = wp_get_current_user(); if ($current_user->ID == 0) { wp_send_json_error('Not a user'); } switch ($_POST['method']) { case 'get_user_state': // Return current chats for current user $chats = PatchChat_Controller::get_user_state(); break; default: $chats = array('error' => 'No method with name ' . $_POST['method']); } if (isset($chats['error'])) { wp_send_json_error($chats); } else { wp_send_json_success($chats); } }
/** * Changes the post_status of the given chat * * @author caseypatrickdriscoll * * @created 2015-08-27 15:13:52 * @edited 2015-08-28 17:58:30 - Refactors to prevent open and closed transient states * @edited 2015-08-28 18:14:09 - Adds fast update by returning user state on change_status * @edited 2015-08-28 20:38:24 - Adds auto comment for every status change * @edited 2015-08-29 18:45:34 - Adds PatchChat_Settings::status_change_message() for dynamic messages * */ public static function change_chat_status($chat) { // TODO: Assign agent when becomes 'open' // TODO: Create idea of assigned agents // TODO: Figure out security and sanitized stuff (although low priority as comes from select in admin POST) // TODO: Handle error situations, including missing data and error on update // TODO: Bulk status change in bulk editor // TODO: Style the selector based on status // TODO: Add thumbs up or signal if POST is success // 1. Update the post status and appropriate transients wp_update_post($chat); PatchChat_Transient::update($chat['ID'], 'status', $chat['post_status']); // 2. Add the comment $user = wp_get_current_user(); $options = array('chatid' => $chat['ID'], 'agentname' => $user->display_name, 'status' => $chat['post_status']); $comment = array('comment_type' => 'auto', 'comment_post_ID' => $chat['ID'], 'comment_content' => PatchChat_Settings::status_change_message($options)); if (self::add_comment($comment)) { return PatchChat_Controller::get_user_state(); } }