Skip to content

Commit 489f7e0

Browse files
committed
Updated get js script block and templates
1 parent 861284d commit 489f7e0

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

Block/JsScript.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magefan\Community\Block;
1010

11+
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\Framework\View\Element\Template;
1213

1314
class JsScript extends Template
@@ -32,6 +33,9 @@ public function setMethod(string $method): JsScript
3233
return $this;
3334
}
3435

36+
/**
37+
* @return string
38+
*/
3539
public function getTemplate()
3640
{
3741
if (!$this->_template) {
@@ -40,15 +44,33 @@ public function getTemplate()
4044
return parent::getTemplate();
4145
}
4246

47+
/**
48+
* Get current website ID
49+
*
50+
* @return int
51+
* @throws NoSuchEntityException
52+
*/
53+
public function getWebsiteId(): int
54+
{
55+
return (int)$this->_storeManager->getStore()->getWebsiteId();
56+
}
57+
4358
/**
4459
* @return string
4560
*/
4661
public function toHtml()
4762
{
48-
if (isset(self::$rendered[$this->jsMethod])) {
63+
if ($this->getScriptAttributes()) {
64+
$hash = '_' . sha1(json_encode($this->getScriptAttributes()));
65+
} else {
66+
$hash = '';
67+
}
68+
69+
if (isset(self::$rendered[$this->jsMethod . $hash])) {
4970
return '';
5071
}
5172
self::$rendered[$this->jsMethod] = 1;
73+
self::$rendered[$this->jsMethod . $hash] = 1;
5274
return parent::toHtml();
5375
}
5476
}

view/base/templates/js/ajax.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
xmlhttp.send(options.data);
3535
};
3636
"; ?>
37-
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>
37+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', $block->getScriptAttributes() ?: [], $script, false) ?>

view/base/templates/js/getCookie.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
return '';
2929
};
3030
"; ?>
31-
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>
31+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', $block->getScriptAttributes() ?: [], $script, false) ?>
32+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
?>
7+
<?php
8+
/**
9+
* @var $block \Magefan\Community\Block\JsScript
10+
* @var $mfSecureRenderer \Magefan\Community\Api\SecureHtmlRendererInterface
11+
*/
12+
?>
13+
<?php
14+
if (!isset($escaper)) {
15+
/* Compatability fix for old Magento versions */
16+
$escaper = $block;
17+
}
18+
?>
19+
<?= $block->getLayout()->createBlock(\Magefan\Community\Block\JsScript::class)
20+
->setMethod('getCookie')
21+
->setScriptAttributes($block->getScriptAttributes())
22+
->toHtml()
23+
?>
24+
<?php $script = "
25+
window.MagefanJs = window.MagefanJs || {};
26+
window.MagefanJs.isCustomerConsentProvided = function() {
27+
if (MagefanJs.getCookie('mf_cookie_consent')) {
28+
return true;
29+
};
30+
let cookie = MagefanJs.getCookie(
31+
'{$escaper->escapeHtml(\Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE)}'
32+
);
33+
if (cookie) {
34+
cookie = JSON.parse(decodeURIComponent(cookie));
35+
if (cookie[{$escaper->escapeHtml($block->getWebsiteId())}]) {
36+
return true;
37+
}
38+
};
39+
40+
if (window.dataLayer) {
41+
var consentData;
42+
for (var i = 0; i < window.dataLayer.length; i++) {
43+
if (dataLayer[i][0] === 'consent') {
44+
consentData = dataLayer[i];
45+
}
46+
};
47+
if (consentData && consentData[2]) {
48+
var ads = consentData[2];
49+
if (ads.ad_user_data === 'granted' &&
50+
ads.ad_personalization === 'granted' &&
51+
ads.ad_storage === 'granted') {
52+
return true;
53+
}
54+
};
55+
};
56+
57+
return false;
58+
};
59+
"; ?>
60+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', $block->getScriptAttributes() ?: [], $script, false) ?>

view/base/templates/js/objToUrlParams.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
return x;
3434
};
3535
"; ?>
36-
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>
36+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', $block->getScriptAttributes() ?: [], $script, false) ?>

view/base/templates/js/setCookie.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
document.cookie = cookieString;
3535
};
3636
"; ?>
37-
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>
37+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', $block->getScriptAttributes() ?: [], $script, false) ?>

0 commit comments

Comments
 (0)