11<?php
22
33use Magento \Framework \View \Helper \SecureHtmlRenderer ;
4+ use CustomGento \Cookiebot \ViewModel \Script ;
45
56/** @var SecureHtmlRenderer $secureRenderer */
7+ /** @var Script $viewModel */
8+ $ viewModel = $ block ->getData ('view_model ' );
69
7- $ addRequiredAttributesScript = '
8- document.addEventListener("DOMContentLoaded", function() {
9- const videoIframes = document.querySelectorAll(".pagebuilder-video-container iframe");
10-
11- videoIframes.forEach((iframe) => {
12- if (!Cookiebot?.consent?.marketing) {
13- iframe.setAttribute("data-cookieblock-src", iframe.src);
14- iframe.setAttribute("data-cookieconsent", "marketing");
15- iframe.removeAttribute("src");
16- iframe.closest("[data-content-type=\"video\"]").setAttribute("data-content-type", "image");
17- }
10+ // Only add scripts if video blocking is enabled
11+ if ($ viewModel && $ viewModel ->isBlockVideosUntilConsentEnabled ()) {
12+ $ addRequiredAttributesScript = '
13+ document.addEventListener("DOMContentLoaded", function() {
14+ const videoIframes = document.querySelectorAll(".pagebuilder-video-container iframe");
15+
16+ videoIframes.forEach((iframe) => {
17+ if (!Cookiebot?.consent?.marketing) {
18+ iframe.setAttribute("data-cookieblock-src", iframe.src);
19+ iframe.setAttribute("data-cookieconsent", "marketing");
20+ iframe.removeAttribute("src");
21+ iframe.closest("[data-content-type=\"video\"]").setAttribute("data-content-type", "image");
22+ }
23+ });
1824 });
19- });
2025
21- addEventListener("CookiebotOnAccept", () => {
22- const videoIframes = document.querySelectorAll(".pagebuilder-video-container iframe");
23-
24- videoIframes.forEach((iframe) => {
25- if (Cookiebot?.consent?.marketing) {
26- if (iframe.closest("[data-content-type=\"image\"]")) {
27- iframe.closest("[data-content-type=\"image\"]").setAttribute("data-content-type", "video");
26+ addEventListener("CookiebotOnAccept", () => {
27+ const videoIframes = document.querySelectorAll(".pagebuilder-video-container iframe");
28+
29+ videoIframes.forEach((iframe) => {
30+ if (Cookiebot?.consent?.marketing) {
31+ if (iframe.closest("[data-content-type=\"image\"]")) {
32+ iframe.closest("[data-content-type=\"image\"]").setAttribute("data-content-type", "video");
33+ }
2834 }
29- }
35+ });
3036 });
31- });
3237
33- addEventListener("CookiebotOnDecline", () => {
34- const videoIframes = document.querySelectorAll(".pagebuilder-video-container iframe");
35-
36- videoIframes.forEach((iframe) => {
37- if (!Cookiebot?.consent?.marketing) {
38- if (iframe.closest("[data-content-type=\"video\"]")) {
39- iframe.closest("[data-content-type=\"video\"]").setAttribute("data-content-type", "image");
38+ addEventListener("CookiebotOnDecline", () => {
39+ const videoIframes = document.querySelectorAll(".pagebuilder-video-container iframe");
40+
41+ videoIframes.forEach((iframe) => {
42+ if (!Cookiebot?.consent?.marketing) {
43+ if (iframe.closest("[data-content-type=\"video\"]")) {
44+ iframe.closest("[data-content-type=\"video\"]").setAttribute("data-content-type", "image");
45+ }
4046 }
41- }
47+ });
4248 });
43- });
44- ' ;
49+ ' ;
4550
46- $ placeholderScript = '
47- document.addEventListener("DOMContentLoaded", function() {
48- ((document, selector, consentType) => {
49- const createTextNode = text => document.createTextNode(text);
50- const createElement = element => document.createElement(element);
51- console.log("here");
52- document.querySelectorAll(selector).forEach(iframe => {
53- const linkElement = createElement("a");
54- const divElement = createElement("div");
55- const paragraphElement = createElement("p");
56- const iframeSrc = iframe.dataset.cookieblockSrc;
57-
58- // Detect service provider type
59- const serviceProvider =
60- /google\.com\/maps\/embed/.test(iframeSrc) ? "Google Maps" :
61- /player\.vimeo\.com\/video\//.test(iframeSrc) ? "Vimeo" :
62- /youtube(-nocookie)?\.com\/embed\//.test(iframeSrc) ? "YouTube" :
63- undefined;
64-
65- if (!serviceProvider) {
66- return;
67- }
68-
69- const iframeHeight = iframe.getBoundingClientRect().height;
70- const iframeWidth = iframe.getBoundingClientRect().width;
71-
72- // Create placeholder content
73- divElement.innerHTML = `
74- <div style="background-color:#CCC;display:inline-block;height:${iframeHeight}px;position:relative;width:${iframeWidth}px;">
75- <div style="background-color:#848484;border-radius:15px;height:50%;position:absolute;transform:translate(50%,50%);width:50%;">
76- <p style="color:#FFF;font-size:7.5em;position:relative;top:50%;left:50%;margin:0;text-align:center;transform:translate(-50%,-50%);">⋯</p>
51+ $ placeholderScript = '
52+ document.addEventListener("DOMContentLoaded", function() {
53+ ((document, selector, consentType) => {
54+ const createTextNode = text => document.createTextNode(text);
55+ const createElement = element => document.createElement(element);
56+ console.log("here");
57+ document.querySelectorAll(selector).forEach(iframe => {
58+ const linkElement = createElement("a");
59+ const divElement = createElement("div");
60+ const paragraphElement = createElement("p");
61+ const iframeSrc = iframe.dataset.cookieblockSrc;
62+
63+ // Detect service provider type
64+ const serviceProvider =
65+ /google\.com\/maps\/embed/.test(iframeSrc) ? "Google Maps" :
66+ /player\.vimeo\.com\/video\//.test(iframeSrc) ? "Vimeo" :
67+ /youtube(-nocookie)?\.com\/embed\//.test(iframeSrc) ? "YouTube" :
68+ undefined;
69+
70+ if (!serviceProvider) {
71+ return;
72+ }
73+
74+ const iframeHeight = iframe.getBoundingClientRect().height;
75+ const iframeWidth = iframe.getBoundingClientRect().width;
76+
77+ // Create placeholder content
78+ divElement.innerHTML = `
79+ <div style="background-color:#CCC;display:inline-block;height:${iframeHeight}px;position:relative;width:${iframeWidth}px;">
80+ <div style="background-color:#848484;border-radius:15px;height:50%;position:absolute;transform:translate(50%,50%);width:50%;">
81+ <p style="color:#FFF;font-size:7.5em;position:relative;top:50%;left:50%;margin:0;text-align:center;transform:translate(-50%,-50%);">⋯</p>
82+ </div>
7783 </div>
78- </div>
79- `;
80-
81- // Setup consent message and button
82- divElement.classList.add(`cookieconsent-optout-${consentType}`);
83- linkElement.textContent = `accept ${consentType} cookies`;
84- linkElement.href = "javascript:Cookiebot.renew()";
85- paragraphElement.append(
86- createTextNode("Please "),
87- linkElement,
88- createTextNode(` to view this ${serviceProvider} content.`)
89- );
90-
91- divElement.append(paragraphElement);
92- iframe.parentNode.insertBefore(divElement, iframe);
93- });
94- })(document, "iframe[data-cookieblock-src]", "marketing");
95- });
96- ' ;
84+ `;
85+
86+ // Setup consent message and button
87+ divElement.classList.add(`cookieconsent-optout-${consentType}`);
88+ linkElement.textContent = `accept ${consentType} cookies`;
89+ linkElement.href = "javascript:Cookiebot.renew()";
90+ paragraphElement.append(
91+ createTextNode("Please "),
92+ linkElement,
93+ createTextNode(` to view this ${serviceProvider} content.`)
94+ );
95+
96+ divElement.append(paragraphElement);
97+ iframe.parentNode.insertBefore(divElement, iframe);
98+ });
99+ })(document, "iframe[data-cookieblock-src]", "marketing");
100+ });
101+ ' ;
97102
98- // Render scripts with secure renderer
99- echo $ secureRenderer ->renderTag (
100- 'script ' ,
101- ['type ' => 'text/javascript ' , 'data-cookieconsent ' => 'ignore ' ],
102- $ addRequiredAttributesScript ,
103- false
104- );
103+ // Render scripts with secure renderer
104+ echo $ secureRenderer ->renderTag (
105+ 'script ' ,
106+ ['type ' => 'text/javascript ' , 'data-cookieconsent ' => 'ignore ' ],
107+ $ addRequiredAttributesScript ,
108+ false
109+ );
105110
106- echo $ secureRenderer ->renderTag (
107- 'script ' ,
108- ['type ' => 'text/javascript ' , 'data-cookieconsent ' => 'ignore ' ],
109- $ placeholderScript ,
110- false
111- );
111+ echo $ secureRenderer ->renderTag (
112+ 'script ' ,
113+ ['type ' => 'text/javascript ' , 'data-cookieconsent ' => 'ignore ' ],
114+ $ placeholderScript ,
115+ false
116+ );
117+ }
112118?>
0 commit comments