Skip to content

Commit

Permalink
Merge pull request #1238 from helsingborg-stad/feat/user-prefers-grou…
Browse files Browse the repository at this point in the history
…p-url

feat: user prefers group url
  • Loading branch information
sebastianthulin authored Jan 14, 2025
2 parents aa51b40 + 28cbf71 commit 784f3ea
Show file tree
Hide file tree
Showing 19 changed files with 686 additions and 181 deletions.
Binary file modified languages/sv_SE.mo
Binary file not shown.
48 changes: 36 additions & 12 deletions languages/sv_SE.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ msgid ""
msgstr ""
"Project-Id-Version: Municipio\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/theme/municipio\n"
"POT-Creation-Date: 2025-01-09 14:47+0100\n"
"PO-Revision-Date: 2025-01-09 14:47+0100\n"
"POT-Creation-Date: 2025-01-10 20:48+0100\n"
"PO-Revision-Date: 2025-01-10 20:53+0100\n"
"Last-Translator: \n"
"Language-Team: Svenska\n"
"Language: sv_SE\n"
Expand Down Expand Up @@ -2532,7 +2532,7 @@ msgid "history"
msgstr "history"

#: library/AcfFields/php/options-menu-icon.php:474
#: library/Admin/Login/AddLoginAndLogoutNotices.php:39
#: library/Admin/Login/AddLoginAndLogoutNotices.php:163
msgid "home"
msgstr "hem"

Expand Down Expand Up @@ -28144,16 +28144,40 @@ msgstr "Behållare-block"
msgid "Please fill in all required fields."
msgstr "Vänligen fyll i alla obligatoriska fält."

#: library/Admin/Login/AddLoginAndLogoutNotices.php:37
#: library/Admin/Login/AddLoginAndLogoutNotices.php:42
#: library/Admin/Login/AddLoginAndLogoutNotices.php:46
#: library/Admin/Login/AddLoginAndLogoutNotices.php:65
msgid "Save this page as default home"
msgstr "Ställ in den här sidan som startsida för mig"

#: library/Admin/Login/AddLoginAndLogoutNotices.php:85
#: library/Admin/Login/AddLoginAndLogoutNotices.php:106
msgid "Option saved"
msgstr "Sparat"

#: library/Admin/Login/AddLoginAndLogoutNotices.php:86
msgid "Option already saved"
msgstr "Sparat"

#: library/Admin/Login/AddLoginAndLogoutNotices.php:107
msgid "Option could not be saved at the moment"
msgstr "Inställningen kunde inte sparas"

#: library/Admin/Login/AddLoginAndLogoutNotices.php:129
#: library/Admin/Login/AddLoginAndLogoutNotices.php:153
#: library/Admin/Login/AddLoginAndLogoutNotices.php:176
msgid "Login successful"
msgstr "Inloggad!"
msgstr "Inloggad, välkommen tillbaka!"

#: library/Admin/Login/AddLoginAndLogoutNotices.php:162
#, php-format
msgid "Go to %s"
msgstr "Gå till %s"

#: library/Admin/Login/AddLoginAndLogoutNotices.php:39
msgid "Go to"
msgstr "Gå till"
#: library/Admin/Login/AddLoginAndLogoutNotices.php:184
msgid "Go to default home"
msgstr "Gå till gemensamma sidor"

#: library/Admin/Login/AddLoginAndLogoutNotices.php:53
#: library/Admin/Login/AddLoginAndLogoutNotices.php:198
msgid "Logout successful"
msgstr "Utloggning lyckades"

Expand Down Expand Up @@ -28403,7 +28427,7 @@ msgstr "Välj en annan startsida"
msgid "View data."
msgstr "Visa data."

#: library/App.php:794 library/Customizer/Sections/Header/LoginLogout.php:22
#: library/App.php:797 library/Customizer/Sections/Header/LoginLogout.php:22
msgid "Never"
msgstr "Aldrig"

Expand Down Expand Up @@ -30869,7 +30893,7 @@ msgstr "Kunde inte skapa en identifierare för den sidoinladdade filen."
msgid "Tailor"
msgstr "Specialanpassa"

#: library/Helper/Post.php:246
#: library/Helper/Post.php:255
msgid "Item is missing content"
msgstr "Objektet saknar innehåll"

Expand Down
249 changes: 220 additions & 29 deletions library/Admin/Login/AddLoginAndLogoutNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,249 @@
namespace Municipio\Admin\Login;

use AcfService\AcfService;
use Municipio\Helper\User\Config\UserConfig;
use Municipio\HooksRegistrar\Hookable;
use WpService\WpService;
use Municipio\Helper\User;
use Municipio\Helper\User\User;

class AddLoginAndLogoutNotices implements Hookable
{
public function __construct(private WpService $wpService, private AcfService $acfService)
public function __construct(private WpService $wpService, private AcfService $acfService, private User $userHelper, private UserConfig $userConfig)
{
}

/**
* Add hooks
*/
/**
* Add hooks
*/
public function addHooks(): void
{
//Set current user
add_action('init', [$this->userHelper, 'setUser'], 5, 0);

// Logon and logout notices
$this->wpService->addAction('init', array($this, 'addNoticeWhenUserLogsIn'));
$this->wpService->addAction('init', array($this, 'addNoticeWhenUserLogsOut'));

// Offer persistant
$this->wpService->addAction('init', array($this, 'offerPersistantGroupUrl'));
$this->wpService->addAction('init', array($this, 'offerPersistantHomeUrl'));

// Set persistant url
$this->wpService->addAction('init', array($this, 'setPersistantGroupUrl'));
$this->wpService->addAction('init', array($this, 'setPersistantHomeUrl'));
}

/**
* Offer persistant group url
*/
public function offerPersistantGroupUrl(): void
{
if ((bool)($_GET['offerPersistantGroupUrl'] ?? false)) {
\Municipio\Helper\Notice::add(
'',
'info',
'save',
[
'url' => './?setPersistantGroupUrl=true&uid=' . $this->wpService->getCurrentUserId(),
'text' => $this->wpService->__('Save this page as default home', 'municipio'),
],
'session'
);
}
}

/**
* Offer persistant home url
*/
public function offerPersistantHomeUrl(): void
{
if ((bool)($_GET['offerPersistantHomeUrl'] ?? false)) {
\Municipio\Helper\Notice::add(
'',
'info',
'save',
[
'url' => './?setPersistantHomeUrl=true&uid=' . $this->wpService->getCurrentUserId(),
'text' => $this->wpService->__('Save this page as default home', 'municipio'),
],
'session'
);
}
}

/**
* Set persistant group url
*/
public function setPersistantGroupUrl(): void
{
if ((bool)($_GET['setPersistantGroupUrl'] ?? false) && $this->wpService->isUserLoggedIn()) {
$result = $this->wpService->updateUserMeta(
$this->wpService->getCurrentUserId(),
$this->userConfig->getUserPrefersGroupUrlMetaKey(),
true
);

$message = $result
? $this->wpService->__('Option saved', 'municipio')
: $this->wpService->__('Option already saved', 'municipio');

$icon = $result ? 'check_circle' : 'preliminary';

\Municipio\Helper\Notice::add($message, 'info', $icon);
}
}

/**
* Set persistant home url
*/
public function setPersistantHomeUrl(): void
{
if ((bool)($_GET['setPersistantHomeUrl'] ?? false) && $this->wpService->isUserLoggedIn()) {
$result = $this->wpService->deleteUserMeta(
$this->wpService->getCurrentUserId(),
$this->userConfig->getUserPrefersGroupUrlMetaKey(),
);

$message = $result
? $this->wpService->__('Option saved', 'municipio')
: $this->wpService->__('Option could not be saved at the moment', 'municipio');

$type = $result ? 'info' : 'warning';
$icon = $result ? 'check_circle' : 'preliminary';

\Municipio\Helper\Notice::add($message, $type, $icon);
}
}

/**
* Add notice when user logs in
*/
public function addNoticeWhenUserLogsIn()
/**
* Add notice when user logs in
*/
public function addNoticeWhenUserLogsIn(): void
{
if ((bool)($_GET['loggedin'] ?? false) && $this->wpService->isUserLoggedIn()) {
$currentUserGroup = User::getCurrentUserGroup();
$currentUserGroupUrl = User::getCurrentUserGroupUrl($currentUserGroup);

if ($currentUserGroupUrl) {
\Municipio\Helper\Notice::add(
__('Login successful', 'municipio'),
'info',
'login',
[
'url' => $currentUserGroupUrl,
'text' => __('Go to', 'municipio') . ' ' . $currentUserGroup->name ?? __('home', 'municipio')
],
'session'
$currentUserGroup = $this->userHelper->getUserGroup();
$currentUserGroupUrl = $this->userHelper->getUserGroupUrl();
$userPrefersGroupUrl = $this->userHelper->getUserPrefersGroupUrl();
$userPrefersGroupUrlType = $this->userHelper->getUserGroupUrlType();

// No url to prefer
if (!$currentUserGroupUrl) {
\Municipio\Helper\Notice::add($this->wpService->__('Login successful', 'municipio'), 'info', 'login');
return;
}

// User prefers group url, and are given option to remove this setting
if ($this->shouldOfferRemovingGroupUrlAsHome($currentUserGroupUrl, $userPrefersGroupUrl)) {
$this->messageWhenUserPrefersUserGroupUrl($currentUserGroup, $currentUserGroupUrl);
return;
}

// User does not prefer group url, and are given option to set this as home
if ($this->shouldOfferSettingGroupUrlAsHome($currentUserGroupUrl, $userPrefersGroupUrl)) {
$this->messageWhenUserDoesNotPreferUserGroupUrl(
$userPrefersGroupUrlType
);
} else {
\Municipio\Helper\Notice::add(__('Login successful', 'municipio'), 'info', 'login');
return;
}
}
}

/**
* Add notice when user logs out
*/
public function addNoticeWhenUserLogsOut()
/**
* Show message with user group
*/
private function messageWhenUserPrefersUserGroupUrl(\WP_Term $currentUserGroup, string $currentUserGroupUrl): void
{
\Municipio\Helper\Notice::add(
$this->wpService->__('Login successful', 'municipio'),
'info',
'login',
[
'url' => $this->addQueryParamsToUrl(
$currentUserGroupUrl,
['offerPersistantHomeUrl' => 'true']
),
'text' => sprintf(
$this->wpService->__('Go to %s', 'municipio'),
$currentUserGroup->name ?? $this->wpService->__('home', 'municipio')
),
],
'session'
);
}

/**
* Show message without user group
*/
private function messageWhenUserDoesNotPreferUserGroupUrl(string $urlType): void
{
//Get network main site url if link type is blog_id
if ($urlType == 'blog_id' && $this->wpService->isMultisite()) {
$url = $this->wpService->getHomeUrl($this->wpService->getMainSiteId() ?? null);
} else {
$url = $this->wpService->getHomeUrl();
}

//Add notice
\Municipio\Helper\Notice::add(
$this->wpService->__('Login successful', 'municipio'),
'info',
'login',
[
'url' => $this->addQueryParamsToUrl(
$url,
[
'offerPersistantGroupUrl' => 'true'
]
),
'text' => $this->wpService->__('Go to default home', 'municipio'),
],
'session'
);
}

/**
* Add notice when user logs out
*
* @return void
*/
public function addNoticeWhenUserLogsOut(): void
{
if ((bool)($_GET['loggedout'] ?? false) && !$this->wpService->isUserLoggedIn()) {
\Municipio\Helper\Notice::add(__('Logout successful', 'municipio'), 'info', 'logout');
\Municipio\Helper\Notice::add($this->wpService->__('Logout successful', 'municipio'), 'info', 'logout');
}
}

/**
* Add query params to url
*
* @param string $url
* @param array $params
*
* @return string
*/
private function addQueryParamsToUrl(string $url, array $params): string
{
return add_query_arg($params, $url);
}

/**
* Should offer setting group url as home
*
* @return bool
*/
private function shouldOfferSettingGroupUrlAsHome($currentUserGroupUrl, $userPrefersGroupUrl): bool
{
return ($currentUserGroupUrl && !$userPrefersGroupUrl);
}

/**
* Should offer removing group url as home,
* a negative of shouldOfferSettingGroupUrlAsHome
*
* @return bool
*/
private function shouldOfferRemovingGroupUrlAsHome($currentUserGroupUrl, $userPrefersGroupUrl): bool
{
return !$this->shouldOfferSettingGroupUrlAsHome($currentUserGroupUrl, $userPrefersGroupUrl);
}
}
Loading

0 comments on commit 784f3ea

Please sign in to comment.