/** * If the user has logged in then replace anonymous username with WordPress user name. * If WordPress user logs out then the anonymous username is restored. * * @return null */ public function switchUser() { $currentWPUser = $this->usersDAO->getCurrentWpUser(); if (!$this->authentication->isAuthenticated()) { if ($currentWPUser !== null && strlen($currentWPUser->display_name) > 0) { $user = $this->authentication->authenticateAnonymously(); $this->authentication->setOriginalUserName($user->getName()); // update username and WP user ID: $user->setWordPressId(intval($currentWPUser->ID)); $user->setName($currentWPUser->display_name); $this->usersDAO->save($user); } return; } else { $user = $this->authentication->getUser(); if ($user === null) { $this->authentication->dropAuthentication(); $user = $this->authentication->authenticateAnonymously(); } $userName = $user->getName(); if ($currentWPUser !== null) { $displayName = $currentWPUser->display_name; if (strlen($displayName) > 0 && $userName != $displayName) { if ($this->authentication->getOriginalUserName() === null) { $this->authentication->setOriginalUserName($userName); } // update username and WP user ID: $user->setName($displayName); $user->setWordPressId(intval($currentWPUser->ID)); $this->usersDAO->save($user); $this->refreshUserName($user); } } else { $originalUserName = $this->authentication->getOriginalUserName(); if ($originalUserName !== null && $userName != $originalUserName) { // update username and WP user ID: $user->setName($originalUserName); $user->setWordPressId(null); $this->usersDAO->save($user); $this->refreshUserName($user); } } } }