Skip to content

Commit

Permalink
Reuse default cookie restriction mode
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Sep 5, 2022
1 parent c8ae934 commit f18ce7c
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 197 deletions.
26 changes: 13 additions & 13 deletions Model/Config/PrivacyMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@

namespace Opengento\Gdpr\Model\Config;

use Magento\Cms\Block\Block;
use Magento\Cms\Block\BlockByIdentifier;
use Magento\Cms\Helper\Page as HelperPage;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\BlockFactory;
use Magento\Store\Model\ScopeInterface;

final class PrivacyMessage
{
private const CONFIG_PATH_COOKIE_INFORMATION_ENABLED = 'gdpr/cookie/enabled';
private const CONFIG_PATH_COOKIE_INFORMATION_BLOCK = 'gdpr/cookie/block_id';
private const CONFIG_PATH_GENERAL_INFORMATION_PAGE = 'gdpr/general/page_id';
private const CONFIG_PATH_COOKIE_INFORMATION_PAGE = 'gdpr/cookie/page_id';

private ScopeConfigInterface $scopeConfig;

private BlockFactory $blockFactory;

/**
* @var HelperPage
*/
private HelperPage $helperPage;

/**
* @var string|null
*/
private ?string $blockHtml;

public function __construct(
Expand All @@ -42,26 +37,31 @@ public function __construct(
$this->helperPage = $helperPage;
}

public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_COOKIE_INFORMATION_ENABLED, ScopeInterface::SCOPE_STORE);
}

public function getDisclosureInformationHtml(): string
{
return $this->blockHtml ??= $this->createDisclosureInformationBlockHtml();
}

public function getLearnMoreUrl(): string
public function getLearnMoreUrl(): ?string
{
return $this->helperPage->getPageUrl((string) $this->scopeConfig->getValue(
self::CONFIG_PATH_GENERAL_INFORMATION_PAGE,
self::CONFIG_PATH_COOKIE_INFORMATION_PAGE,
ScopeInterface::SCOPE_STORE
)) ?? '#';
));
}

private function createDisclosureInformationBlockHtml(): string
{
return $this->blockFactory->createBlock(
Block::class,
BlockByIdentifier::class,
[
'data' => [
'block_id' => (string) $this->scopeConfig->getValue(
'identifier' => (string) $this->scopeConfig->getValue(
self::CONFIG_PATH_COOKIE_INFORMATION_BLOCK,
ScopeInterface::SCOPE_STORE
),
Expand Down
36 changes: 36 additions & 0 deletions ViewModel/Cookie/NoticeDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\Gdpr\ViewModel\Cookie;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Opengento\Gdpr\Model\Config\PrivacyMessage;

final class NoticeDataProvider implements ArgumentInterface
{
private PrivacyMessage $privacyMessage;

public function __construct(PrivacyMessage $privacyMessage)
{
$this->privacyMessage = $privacyMessage;
}

public function getTemplate(string $defaultTemplate, string $customTemplate): string
{
return $this->privacyMessage->isEnabled() ? $customTemplate : $defaultTemplate;
}

public function getLearnMoreUrl(): ?string
{
return $this->privacyMessage->getLearnMoreUrl();
}

public function getNoticeHtml(): string
{
return $this->privacyMessage->getDisclosureInformationHtml();
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"magento/framework": "^103.0",
"magento/module-backend": "^101.0||^102.0",
"magento/module-config": "^101.1",
"magento/module-cookie": "^100.0",
"magento/module-customer": "^102.0||^103.0",
"magento/module-quote": "^101.1",
"magento/module-sales": "^102.0||^103.0",
Expand Down
14 changes: 11 additions & 3 deletions etc/adminhtml/system/cookie.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@
<group id="cookie" translate="label" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="10">
<label>Cookie Settings</label>
<field id="enabled" type="select" translate="label comment" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="10">
<label>Cookie Disclosure</label>
<comment>It will enable the cookie disclosure popin to the storefront.</comment>
<label>Improve Cookie Restriction Message</label>
<comment>It will allows to replace the default cookie restriction popin with content of your own. Make sure that the following configuration is enabled: "General / Web / Default Cookie Settings / Cookie Restriction Mode".</comment>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
<config_path>gdpr/cookie/enabled</config_path>
</field>
<field id="block_id" type="select" translate="label" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="20">
<label>Information</label>
<label>Information Block</label>
<source_model>Magento\Cms\Model\Config\Source\Block</source_model>
<depends>
<field id="opengento_gdpr_cookie/cookie/enabled">1</field>
</depends>
<config_path>gdpr/cookie/block_id</config_path>
</field>
<field id="page_id" type="select" translate="label" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="30">
<label>Information Page</label>
<source_model>Magento\Cms\Model\Config\Source\Page</source_model>
<depends>
<field id="opengento_gdpr_cookie/cookie/enabled">1</field>
</depends>
<config_path>gdpr/cookie/page_id</config_path>
</field>
</group>
</section>
</include>
29 changes: 9 additions & 20 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,14 @@
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="before.body.end">
<block name="privacy_message_popup" after="-" ifconfig="gdpr/cookie/enabled">
<arguments>
<argument name="template" xsi:type="string">Opengento_Gdpr::messages/popup.phtml</argument>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="enhanced-privacy-cookie-policy" xsi:type="array">
<item name="component" xsi:type="string">Opengento_Gdpr/js/view/popup</item>
<item name="displayArea" xsi:type="string">enhanced-privacy-cookie-policy</item>
<item name="config" xsi:type="array">
<item name="cookieName" xsi:type="string">cookies-policy</item>
<item name="learnMore" xsi:type="helper" helper="Opengento\Gdpr\Model\Config\PrivacyMessage::getLearnMoreUrl"/>
<item name="notificationText" xsi:type="helper" helper="Opengento\Gdpr\Model\Config\PrivacyMessage::getDisclosureInformationHtml"/>
</item>
</item>
</item>
</argument>
</arguments>
</block>
</referenceContainer>
<referenceBlock name="cookie_notices" template="">
<arguments>
<argument name="template" xsi:type="helper" helper="Opengento\Gdpr\ViewModel\Cookie\NoticeDataProvider::getTemplate">
<param name="defaultTemplate">Magento_Cookie::html/notices.phtml</param>
<param name="customTemplate">Opengento_Gdpr::cookie/notices.phtml</param>
</argument>
<argument name="noticeDataProvider" xsi:type="object">Opengento\Gdpr\ViewModel\Cookie\NoticeDataProvider</argument>
</arguments>
</referenceBlock>
</body>
</page>
59 changes: 59 additions & 0 deletions view/frontend/templates/cookie/notices.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/

use Magento\Cookie\Block\Html\Notices;
use Magento\Cookie\Helper\Cookie;
use Magento\Framework\Escaper;
use Magento\Framework\View\Helper\SecureHtmlRenderer;
use Opengento\Gdpr\ViewModel\Cookie\NoticeDataProvider;

/** @var Notices $block */
/** @var Escaper $escaper */
/** @var SecureHtmlRenderer $secureRenderer */
/** @var Cookie $cookieHelper */
$cookieHelper = $block->getData('cookieHelper');
/** @var NoticeDataProvider $noticeDataProvider */
$noticeDataProvider = $block->getData('noticeDataProvider');

$learnMoreUrl = $noticeDataProvider->getLearnMoreUrl() ?: $block->getPrivacyPolicyLink()
?>
<?php
if ($cookieHelper->isCookieRestrictionModeEnabled()): ?>
<div role="alertdialog"
tabindex="-1"
class="message global cookie"
id="notice-cookie-block">
<div role="document" class="content" tabindex="0">
<div class="details">
<?= /* @noEscape */ $noticeDataProvider->getNoticeHtml() ?>
<p>
<strong><?= $block->escapeHtml(__('We use cookies to make your experience better.')) ?></strong>
<?= $block->escapeHtml(__('<a href="%1">Learn more</a>.', $learnMoreUrl), ['a']) ?>
</p>
</div>
<div class="actions">
<button id="btn-cookie-allow" class="action allow primary">
<span><?= $block->escapeHtml(__('Allow Cookies')) ?></span>
</button>
</div>
</div>
</div>
<?= /* @noEscape */ $secureRenderer->renderStyleAsTag('display: none;', 'div#notice-cookie-block') ?>
<script type="text/x-magento-init">
{
"#notice-cookie-block": {
"cookieNotices": {
"cookieAllowButtonSelector": "#btn-cookie-allow",
"cookieName": "<?= /* @noEscape */ Cookie::IS_USER_ALLOWED_SAVE_COOKIE ?>",
"cookieValue": <?= /* @noEscape */ $cookieHelper->getAcceptedSaveCookiesWebsiteIds() ?>,
"cookieLifetime": <?= /* @noEscape */ $cookieHelper->getCookieRestrictionLifetime() ?>,
"noCookiesUrl": "<?= $escaper->escapeJs($block->getUrl('cookie/index/noCookies')) ?>"
}
}
}
</script>
<?php endif; ?>
21 changes: 0 additions & 21 deletions view/frontend/templates/messages/popup.phtml

This file was deleted.

33 changes: 0 additions & 33 deletions view/frontend/web/css/source/_module.less
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,4 @@
}
}
}

.enhanced-privacy-popup {
position: fixed;
right: 0;
bottom: 0;
left: 0;
background: @color-gray-light01;
overflow: hidden;

.popup {
&.content {
margin: auto;
padding: 10px 0;
width: @screen__m;
}
}
}
}

//
// Mobile
// _____________________________________________

.media-width(@extremum, @break) when (@extremum = 'max') and (@break = @screen__m) {
.enhanced-privacy-popup {
.popup {
&.content {
margin: 0;
padding: 10px;
width: auto;
}
}
}
}
75 changes: 0 additions & 75 deletions view/frontend/web/js/view/popup.js

This file was deleted.

Loading

0 comments on commit f18ce7c

Please sign in to comment.