Beispiel #1
0
 /**
  * Creates or updates the user and returns it.
  *
  * @param WiseChatUser $user
  *
  * @return WiseChatUser
  * @throws Exception On validation error
  */
 public function save($user)
 {
     global $wpdb;
     // low-level validation:
     if ($user->getName() === null) {
         throw new Exception('Name of the user cannot equal null');
     }
     if ($user->getSessionId() === null) {
         throw new Exception('Session ID of the user cannot equal null');
     }
     // prepare user data:
     $table = WiseChatInstaller::getUsersTable();
     $columns = array('name' => $user->getName(), 'session_id' => $user->getSessionId(), 'data' => json_encode($user->getData()), 'ip' => $user->getIp());
     // update or insert:
     if ($user->getId() !== null) {
         $columns['wp_id'] = $user->getWordPressId();
         $wpdb->update($table, $columns, array('id' => $user->getId()), '%s', '%d');
     } else {
         if ($user->getWordPressId() > 0) {
             $columns['wp_id'] = $user->getWordPressId();
         }
         $columns['created'] = time();
         $wpdb->insert($table, $columns);
         $user->setId($wpdb->insert_id);
     }
     return $user;
 }