/** * Adds a PatchChat_Transient to a transient's array * * @author caseypatrickdriscoll * * @created 2015-07-19 20:18:54 * @edited 2015-08-04 16:13:17 - Refactors to build all given arrays * * $transient_name string The name of the transient to add to ('patchchat_new', etc) * * $patchchat array The array of information to store in transient (see 'Form' in class comments) * */ public static function add($state_name, $transient) { $transient_state = PatchChat_Transient_State::get($state_name); array_unshift($transient_state, $transient); PatchChat_Transient_State::set($state_name, $transient_state); return true; }
/** * 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); } }
/** * Returns the given agent's transient set along with the new chats * * @author caseypatrickdriscoll * * @edited 2015-08-04 15:36:31 - Adds role check for getting user chats * @edited 2015-08-27 18:38:05 - Refactors to remove get_array() * @edited 2015-08-29 18:16:26 - Refactors for situations with no user_id because the user was just created * * TODO: Add 'agent' role/capability instead of 'administrator' */ public static function get_user_state($user_id = NULL) { if ($user_id == NULL) { $user = wp_get_current_user(); $user_id = $user->ID; } else { $user = get_user_by('id', $user_id); } $user_chats = PatchChat_Transient_State::get($user_id); if (!empty($user->roles) && is_array($user->roles)) { foreach ($user->roles as $role) { if ($role == 'administrator') { $user_chats = array_merge($user_chats, PatchChat_Transient_State::get('new')); } } } return $user_chats; }