|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | use CustomGento\Cookiebot\ViewModel\Script; |
4 | 6 | use Magento\Framework\View\Helper\SecureHtmlRenderer; |
5 | 7 |
|
6 | 8 | /** @var SecureHtmlRenderer $secureRenderer */ |
7 | 9 | /** @var Script $viewModel */ |
8 | 10 | $viewModel = $block->getData('view_model'); |
9 | 11 |
|
10 | | -// Only add scripts if video blocking is enabled |
11 | | -if ($viewModel && $viewModel->isBlockVideosUntilConsentEnabled()) { |
12 | | - $placeholderScript = ' |
13 | | - document.addEventListener("DOMContentLoaded", function() { |
14 | | - ((document, selector, consentType) => { |
15 | | - const createTextNode = text => document.createTextNode(text); |
16 | | - const createElement = element => document.createElement(element); |
17 | | - |
18 | | - document.querySelectorAll(selector).forEach(iframe => { |
19 | | - const linkElement = createElement("a"); |
20 | | - const divElement = createElement("div"); |
21 | | - const paragraphElement = createElement("p"); |
22 | | - const iframeSrc = iframe.dataset.cookieblockSrc; |
23 | | - |
24 | | - // Detect service provider type |
25 | | - const serviceProvider = |
26 | | - /google\.com\/maps\/embed/.test(iframeSrc) ? "Google Maps" : |
27 | | - /player\.vimeo\.com\/video\//.test(iframeSrc) ? "Vimeo" : |
28 | | - /youtube(-nocookie)?\.com\/embed\//.test(iframeSrc) ? "YouTube" : |
29 | | - undefined; |
30 | | - |
31 | | - if (!serviceProvider) { |
32 | | - return; |
33 | | - } |
34 | | - |
35 | | - const iframeHeight = iframe.getBoundingClientRect().height; |
36 | | - const iframeWidth = iframe.getBoundingClientRect().width; |
37 | | - |
38 | | - // Create placeholder content |
39 | | - divElement.innerHTML = ` |
40 | | - <div style="background-color:#CCC;display:inline-block;height:${iframeHeight}px;position:relative;width:${iframeWidth}px;"> |
41 | | - <div style="background-color:#848484;border-radius:15px;height:50%;position:absolute;transform:translate(50%,50%);width:50%;"> |
42 | | - <p style="color:#FFF;font-size:7.5em;position:relative;top:50%;left:50%;margin:0;text-align:center;transform:translate(-50%,-50%);">⋯</p> |
43 | | - </div> |
44 | | - </div> |
45 | | - `; |
46 | | - |
47 | | - // Setup consent message and button |
48 | | - divElement.classList.add(`cookieconsent-optout-${consentType}`); |
49 | | - linkElement.textContent = `accept ${consentType} cookies`; |
50 | | - linkElement.href = "javascript:Cookiebot.renew()"; |
51 | | - paragraphElement.append( |
52 | | - createTextNode("Please "), |
53 | | - linkElement, |
54 | | - createTextNode(` to view this ${serviceProvider} content.`) |
55 | | - ); |
56 | | - |
57 | | - divElement.style.fontSize = "1.4rem"; |
58 | | - divElement.append(paragraphElement); |
59 | | - iframe.parentNode.insertBefore(divElement, iframe); |
60 | | - }); |
61 | | - })(document, "iframe[data-cookieblock-src]", "marketing"); |
62 | | - }); |
63 | | - '; |
| 12 | +// Prepare translations |
| 13 | +$acceptText = __('accept %1 cookies', 'marketing'); |
| 14 | +$consentMessage = __('Please %1 to view this %2 content.'); |
64 | 15 |
|
65 | | - echo $secureRenderer->renderTag( |
66 | | - 'script', |
67 | | - ['type' => 'text/javascript', 'data-cookieconsent' => 'ignore'], |
68 | | - $placeholderScript, |
69 | | - false |
70 | | - ); |
71 | | -} |
| 16 | +$jsTranslations = [ |
| 17 | + 'acceptText' => $acceptText, |
| 18 | + 'consentMessage' => $consentMessage, |
| 19 | +]; |
72 | 20 | ?> |
| 21 | + |
| 22 | +<script type="text/javascript" data-cookieconsent="ignore"> |
| 23 | +document.addEventListener("DOMContentLoaded", function() { |
| 24 | + ((document, selector, consentType, translations) => { |
| 25 | + const createTextNode = text => document.createTextNode(text); |
| 26 | + const createElement = element => document.createElement(element); |
| 27 | + |
| 28 | + document.querySelectorAll(selector).forEach(iframe => { |
| 29 | + const linkElement = createElement("a"); |
| 30 | + const divElement = createElement("div"); |
| 31 | + const paragraphElement = createElement("p"); |
| 32 | + const iframeSrc = iframe.dataset.cookieblockSrc; |
| 33 | + |
| 34 | + // Detect service provider type |
| 35 | + const serviceProvider = |
| 36 | + /google\.com\/maps\/embed/.test(iframeSrc) ? "Google Maps" : |
| 37 | + /player\.vimeo\.com\/video\//.test(iframeSrc) ? "Vimeo" : |
| 38 | + /youtube(-nocookie)?\.com\/embed\//.test(iframeSrc) ? "YouTube" : |
| 39 | + undefined; |
| 40 | + |
| 41 | + if (!serviceProvider) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const iframeHeight = iframe.getBoundingClientRect().height; |
| 46 | + const iframeWidth = iframe.getBoundingClientRect().width; |
| 47 | + |
| 48 | + // Create placeholder content |
| 49 | + divElement.innerHTML = ` |
| 50 | + <div style="background-color:#CCC;display:inline-block;height:${iframeHeight}px;position:relative;width:${iframeWidth}px;"> |
| 51 | + <div style="background-color:#848484;border-radius:15px;height:50%;position:absolute;transform:translate(50%,50%);width:50%;"> |
| 52 | + <p style="color:#FFF;font-size:7.5em;position:relative;top:50%;left:50%;margin:0;text-align:center;transform:translate(-50%,-50%);">⋯</p> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + `; |
| 56 | + |
| 57 | + // Setup consent message and button |
| 58 | + divElement.classList.add(`cookieconsent-optout-${consentType}`); |
| 59 | + linkElement.textContent = translations.acceptText.replace('%1', consentType); |
| 60 | + linkElement.href = "javascript:Cookiebot.renew()"; |
| 61 | + const message = translations.consentMessage |
| 62 | + .replace('%1', linkElement.outerHTML) |
| 63 | + .replace('%2', serviceProvider); |
| 64 | + paragraphElement.innerHTML = message; |
| 65 | + |
| 66 | + divElement.style.fontSize = "1.4rem"; |
| 67 | + divElement.append(paragraphElement); |
| 68 | + iframe.parentNode.insertBefore(divElement, iframe); |
| 69 | + }); |
| 70 | + })(document, "iframe[data-cookieblock-src]", "marketing", <?php echo json_encode($jsTranslations); ?>); |
| 71 | +}); |
| 72 | +</script> |
0 commit comments