Skip to content

Commit 7f4f151

Browse files
committed
Add LokiCookies API and cookie notice
1 parent eee8f36 commit 7f4f151

6 files changed

Lines changed: 138 additions & 1 deletion

File tree

ViewModel/CookieConfig.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Loki\Theme\ViewModel;
5+
6+
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
use Magento\Framework\View\Element\Block\ArgumentInterface;
8+
use Magento\Store\Model\ScopeInterface;
9+
10+
class CookieConfig implements ArgumentInterface
11+
{
12+
public function __construct(
13+
private readonly ScopeConfigInterface $scopeConfig,
14+
) {
15+
}
16+
17+
public function getLifetime(): int
18+
{
19+
return (int)$this->get('web/cookie/cookie_lifetime');
20+
}
21+
22+
public function getPath(): string
23+
{
24+
return (string)$this->get('web/cookie/cookie_path');
25+
}
26+
27+
public function getDomain(): string
28+
{
29+
return (string)$this->get('web/cookie/cookie_domain');
30+
}
31+
32+
public function isHttpOnly(): int
33+
{
34+
return (int)$this->get('web/cookie/cookie_httponly');
35+
}
36+
37+
public function getSameSite(): string
38+
{
39+
return 'Strict'; // @todo Strict, Lax or None
40+
}
41+
private function get(string $path): mixed
42+
{
43+
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE);
44+
}
45+
}

ViewModel/FormKeyValue.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
namespace Loki\Theme\ViewModel;
55

6+
use Magento\Framework\Data\Form\FormKey;
67
use Magento\Framework\View\Element\Block\ArgumentInterface;
78

89
class FormKeyValue implements ArgumentInterface
910
{
1011
public function __construct(
11-
private \Magento\Framework\Data\Form\FormKey $formKey,
12+
private FormKey $formKey,
1213
) {
1314
}
1415

view/frontend/layout/loki_theme_default.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010
</arguments>
1111
</block>
1212

13+
<block name="loki.script.cookies" template="Loki_Theme::script/cookies.phtml">
14+
<arguments>
15+
<argument name="cookie_config" xsi:type="object">Loki\Theme\ViewModel\CookieConfig</argument>
16+
</arguments>
17+
</block>
18+
1319
<block name="loki.script.store.local-storage" template="Loki_Theme::script/store/local-storage.phtml"/>
1420

1521
<block name="loki.script.components.main-menu" template="Loki_Theme::script/components/main-menu.phtml"/>
1622
<block name="loki.script.components.newsletter" template="Loki_Theme::script/components/newsletter.phtml"/>
1723
<block name="loki.script.components.messages" template="Loki_Theme::script/components/messages.phtml"/>
1824
<block name="loki.script.components.minicart" template="Loki_Theme::script/components/minicart.phtml"/>
1925
<block name="loki.script.components.top-links" template="Loki_Theme::script/components/top-links.phtml"/>
26+
<block name="loki.script.components.cookie-notice" template="Loki_Theme::script/components/cookie-notice.phtml"/>
2027
</referenceContainer>
2128

2229
<referenceContainer name="minicart.addons">
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Magento\Framework\View\Element\Template;
5+
6+
/** @var Template $block */
7+
?>
8+
<script>
9+
(function() {
10+
const cookieNoticeBlock = document.getElementById('notice-cookie-block');
11+
if (!cookieNoticeBlock) {
12+
return;
13+
}
14+
15+
const userAllowedSaveCookie = LokiCookies.get('user_allowed_save_cookie');
16+
if (!userAllowedSaveCookie) {
17+
cookieNoticeBlock.style.display = 'block';
18+
}
19+
20+
document.getElementById('btn-cookie-allow').addEventListener('click', (e) => {
21+
LokiCookies.set('user_allowed_save_cookie', 1);
22+
cookieNoticeBlock.style.display = 'none';
23+
});
24+
})();
25+
</script>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Loki\Theme\ViewModel\CookieConfig;
5+
use Magento\Framework\View\Element\Template;
6+
7+
/** @var Template $block */
8+
/** @var CookieConfig $cookieConfig */
9+
10+
$cookieConfig = $block->getCookieConfig();
11+
?>
12+
<script>
13+
const LokiCookies = {
14+
lifetime: <?= /* @noEscape */ $cookieConfig->getLifetime() ?>,
15+
domain: '<?= /* @noEscape */ $cookieConfig->getDomain() ?>',
16+
path: '<?= /* @noEscape */ $cookieConfig->getPath() ?>',
17+
httpOnly: <?= /* @noEscape */ $cookieConfig->isHttpOnly() ?>,
18+
sameSite: '<?= /* @noEscape */ $cookieConfig->getSameSite() ?>',
19+
getAll() {
20+
const cookies = {};
21+
document.cookie.split(';').forEach(cookie => {
22+
const parts = cookie.split('=');
23+
const name = parts[0].trim();
24+
let value = parts[1].trim();
25+
26+
if (value.startsWith('{') && value.endsWith('}')) {
27+
value = JSON.parse(value);
28+
} else {
29+
value = decodeURI(value);
30+
}
31+
32+
cookies[name] = value;
33+
})
34+
35+
return cookies;
36+
},
37+
get(name) {
38+
return this.getAll()[name];
39+
},
40+
set(name, value) {
41+
let cookieParts = [];
42+
cookieParts.push(name + '=' + value);
43+
cookieParts.push('max-age=' + this.lifetime + 20000);
44+
cookieParts.push('path=' + this.path);
45+
46+
if (this.domain) {
47+
cookieParts.push('domain=' + this.domain);
48+
}
49+
50+
if (this.httpOnly) {
51+
cookieParts.push('secure');
52+
}
53+
54+
cookieParts.push('SameSite=' + this.sameSite);
55+
document.cookie = cookieParts.join('; ');
56+
}
57+
}
58+
</script>

view/frontend/templates/script/variables.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare(strict_types=1);
44
/** @version 0.0.4 */
55

66
use Loki\Theme\ViewModel\FormKeyValue;
7+
use Loki\Theme\ViewModel\StoreConfig;
78
use Magento\Framework\View\Element\Template;
89

910
/** @var Template $block */

0 commit comments

Comments
 (0)