Skip to content

Commit 5c55e3b

Browse files
committed
Add video blocker to cms, DEV-1111
Signed-off-by: Iman Aboheydary <[email protected]>
1 parent 5d8a8bc commit 5c55e3b

File tree

5 files changed

+269
-8
lines changed

5 files changed

+269
-8
lines changed

Model/YouTubeReplacer.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CustomGento\Cookiebot\Model;
6+
7+
class YouTubeReplacer
8+
{
9+
/**
10+
* Replace iframe sources with cookie consent attributes for YouTube, Vimeo, and Google Maps
11+
*
12+
* @param string $content
13+
* @return string
14+
*/
15+
public function replaceIframeSources(string $content): string
16+
{
17+
// Pattern to match iframe sources for various services
18+
$iframePatterns = [
19+
// YouTube patterns
20+
'/<iframe([^>]*)\s+src=["\'](https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/[^"\']+)["\']([^>]*)>/i',
21+
'/<iframe([^>]*)\s+src=["\'](https?:\/\/(?:www\.)?youtu\.be\/[^"\']+)["\']([^>]*)>/i',
22+
// Vimeo patterns
23+
'/<iframe([^>]*)\s+src=["\'](https?:\/\/(?:www\.)?vimeo\.com\/[^"\']+)["\']([^>]*)>/i',
24+
'/<iframe([^>]*)\s+src=["\'](https?:\/\/(?:www\.)?player\.vimeo\.com\/[^"\']+)["\']([^>]*)>/i',
25+
// Google Maps patterns
26+
'/<iframe([^>]*)\s+src=["\'](https?:\/\/(?:www\.)?google\.com\/maps\/[^"\']+)["\']([^>]*)>/i',
27+
'/<iframe([^>]*)\s+src=["\'](https?:\/\/(?:www\.)?maps\.google\.com\/[^"\']+)["\']([^>]*)>/i',
28+
];
29+
30+
foreach ($iframePatterns as $pattern) {
31+
$content = preg_replace_callback($pattern, function ($matches) {
32+
$beforeSrc = $matches[1];
33+
$iframeUrl = $matches[2];
34+
$afterSrc = $matches[3];
35+
36+
// Check if data-cookieconsent already exists
37+
if (preg_match('/data-cookieconsent=["\'][^"\']*["\']/', $beforeSrc . $afterSrc)) {
38+
// If data-cookieconsent already exists, just change src to data-cookieblock-src
39+
return '<iframe' . $beforeSrc . ' data-cookieblock-src="' . $iframeUrl . '"' . $afterSrc . '>';
40+
} else {
41+
// Add data-cookieconsent="marketing" and change src to data-cookieblock-src
42+
return '<iframe' . $beforeSrc . ' data-cookieblock-src="' . $iframeUrl . '" data-cookieconsent="marketing"' . $afterSrc . '>';
43+
}
44+
}, $content);
45+
}
46+
47+
return $content;
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CustomGento\Cookiebot\Plugin\PageBuilder\Model\Filter;
6+
7+
use CustomGento\Cookiebot\Model\Config;
8+
use CustomGento\Cookiebot\Model\YouTubeReplacer;
9+
use Magento\PageBuilder\Model\Filter\Template;
10+
11+
class TemplatePlugin
12+
{
13+
/**
14+
* @var Config
15+
*/
16+
private $config;
17+
18+
/**
19+
* @var YouTubeReplacer
20+
*/
21+
private $youTubeReplacer;
22+
23+
/**
24+
* @param Config $config
25+
* @param YouTubeReplacer $youTubeReplacer
26+
*/
27+
public function __construct(Config $config, YouTubeReplacer $youTubeReplacer)
28+
{
29+
$this->config = $config;
30+
$this->youTubeReplacer = $youTubeReplacer;
31+
}
32+
33+
/**
34+
* Replace YouTube iframe sources for page builder templates
35+
*/
36+
public function afterFilter(Template $subject, string $result): string
37+
{
38+
// Only process if the block_videos_until_consent feature is enabled
39+
if (!$this->config->isBlockVideosUntilConsentEnabled()) {
40+
return $result;
41+
}
42+
43+
return $this->youTubeReplacer->replaceIframeSources($result);
44+
}
45+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CustomGento\Cookiebot\Test\Unit\Model;
6+
7+
use CustomGento\Cookiebot\Model\YouTubeReplacer;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class YouTubeReplacerTest extends TestCase
11+
{
12+
/**
13+
* @var YouTubeReplacer
14+
*/
15+
private $youTubeReplacer;
16+
17+
protected function setUp(): void
18+
{
19+
$this->youTubeReplacer = new YouTubeReplacer();
20+
}
21+
22+
/**
23+
* @dataProvider iframeDataProvider
24+
*/
25+
public function testReplaceIframeSources(string $input, string $expected): void
26+
{
27+
$result = $this->youTubeReplacer->replaceIframeSources($input);
28+
$this->assertEquals($expected, $result);
29+
}
30+
31+
public function iframeDataProvider(): array
32+
{
33+
return [
34+
// YouTube test cases
35+
'youtube.com embed' => [
36+
'<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>',
37+
'<iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" width="560" height="315" data-cookieconsent="marketing"></iframe>'
38+
],
39+
'youtube-nocookie.com embed' => [
40+
'<iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>',
41+
'<iframe data-cookieblock-src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" width="560" height="315" data-cookieconsent="marketing"></iframe>'
42+
],
43+
'youtu.be URL' => [
44+
'<iframe src="https://youtu.be/dQw4w9WgXcQ" width="560" height="315"></iframe>',
45+
'<iframe data-cookieblock-src="https://youtu.be/dQw4w9WgXcQ" width="560" height="315" data-cookieconsent="marketing"></iframe>'
46+
],
47+
'youtube.com without www' => [
48+
'<iframe src="https://youtube.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>',
49+
'<iframe data-cookieblock-src="https://youtube.com/embed/dQw4w9WgXcQ" width="560" height="315" data-cookieconsent="marketing"></iframe>'
50+
],
51+
'youtube-nocookie.com without www' => [
52+
'<iframe src="https://youtube-nocookie.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>',
53+
'<iframe data-cookieblock-src="https://youtube-nocookie.com/embed/dQw4w9WgXcQ" width="560" height="315" data-cookieconsent="marketing"></iframe>'
54+
],
55+
'youtu.be without www' => [
56+
'<iframe src="https://youtu.be/dQw4w9WgXcQ" width="560" height="315"></iframe>',
57+
'<iframe data-cookieblock-src="https://youtu.be/dQw4w9WgXcQ" width="560" height="315" data-cookieconsent="marketing"></iframe>'
58+
],
59+
'http instead of https for youtube' => [
60+
'<iframe src="http://www.youtube.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>',
61+
'<iframe data-cookieblock-src="http://www.youtube.com/embed/dQw4w9WgXcQ" width="560" height="315" data-cookieconsent="marketing"></iframe>'
62+
],
63+
64+
// Vimeo test cases
65+
'vimeo.com embed' => [
66+
'<iframe src="https://vimeo.com/123456789" width="640" height="360"></iframe>',
67+
'<iframe data-cookieblock-src="https://vimeo.com/123456789" width="640" height="360" data-cookieconsent="marketing"></iframe>'
68+
],
69+
'player.vimeo.com embed' => [
70+
'<iframe src="https://player.vimeo.com/video/123456789" width="640" height="360"></iframe>',
71+
'<iframe data-cookieblock-src="https://player.vimeo.com/video/123456789" width="640" height="360" data-cookieconsent="marketing"></iframe>'
72+
],
73+
'www.vimeo.com embed' => [
74+
'<iframe src="https://www.vimeo.com/123456789" width="640" height="360"></iframe>',
75+
'<iframe data-cookieblock-src="https://www.vimeo.com/123456789" width="640" height="360" data-cookieconsent="marketing"></iframe>'
76+
],
77+
'www.player.vimeo.com embed' => [
78+
'<iframe src="https://www.player.vimeo.com/video/123456789" width="640" height="360"></iframe>',
79+
'<iframe data-cookieblock-src="https://www.player.vimeo.com/video/123456789" width="640" height="360" data-cookieconsent="marketing"></iframe>'
80+
],
81+
'http vimeo' => [
82+
'<iframe src="http://vimeo.com/123456789" width="640" height="360"></iframe>',
83+
'<iframe data-cookieblock-src="http://vimeo.com/123456789" width="640" height="360" data-cookieconsent="marketing"></iframe>'
84+
],
85+
86+
// Google Maps test cases
87+
'google.com/maps embed' => [
88+
'<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3024.2219901290355!2d-74.00369368400567!3d40.71312937933185!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89c25a23e28c1191%3A0x49f75d3281df052a!2s150%20Park%20Row%2C%20New%20York%2C%20NY%2010007!5e0!3m2!1sen!2sus!4v1640995200000!5m2!1sen!2sus" width="600" height="450"></iframe>',
89+
'<iframe data-cookieblock-src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3024.2219901290355!2d-74.00369368400567!3d40.71312937933185!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89c25a23e28c1191%3A0x49f75d3281df052a!2s150%20Park%20Row%2C%20New%20York%2C%20NY%2010007!5e0!3m2!1sen!2sus!4v1640995200000!5m2!1sen!2sus" width="600" height="450" data-cookieconsent="marketing"></iframe>'
90+
],
91+
'maps.google.com embed' => [
92+
'<iframe src="https://maps.google.com/maps?q=New+York&t=&z=13&ie=UTF8&iwloc=&output=embed" width="600" height="450"></iframe>',
93+
'<iframe data-cookieblock-src="https://maps.google.com/maps?q=New+York&t=&z=13&ie=UTF8&iwloc=&output=embed" width="600" height="450" data-cookieconsent="marketing"></iframe>'
94+
],
95+
'google.com/maps without www' => [
96+
'<iframe src="https://google.com/maps/embed?pb=test" width="600" height="450"></iframe>',
97+
'<iframe data-cookieblock-src="https://google.com/maps/embed?pb=test" width="600" height="450" data-cookieconsent="marketing"></iframe>'
98+
],
99+
'maps.google.com without www' => [
100+
'<iframe src="https://maps.google.com/maps?q=test" width="600" height="450"></iframe>',
101+
'<iframe data-cookieblock-src="https://maps.google.com/maps?q=test" width="600" height="450" data-cookieconsent="marketing"></iframe>'
102+
],
103+
'http google maps' => [
104+
'<iframe src="http://www.google.com/maps/embed?pb=test" width="600" height="450"></iframe>',
105+
'<iframe data-cookieblock-src="http://www.google.com/maps/embed?pb=test" width="600" height="450" data-cookieconsent="marketing"></iframe>'
106+
],
107+
108+
// General test cases
109+
'single quotes' => [
110+
'<iframe src=\'https://www.youtube.com/embed/dQw4w9WgXcQ\' width="560" height="315"></iframe>',
111+
'<iframe data-cookieblock-src=\'https://www.youtube.com/embed/dQw4w9WgXcQ\' width="560" height="315" data-cookieconsent="marketing"></iframe>'
112+
],
113+
'multiple iframes different services' => [
114+
'<iframe src="https://www.youtube.com/embed/video1"></iframe><iframe src="https://vimeo.com/video2"></iframe><iframe src="https://www.google.com/maps/embed?pb=test"></iframe>',
115+
'<iframe data-cookieblock-src="https://www.youtube.com/embed/video1" data-cookieconsent="marketing"></iframe><iframe data-cookieblock-src="https://vimeo.com/video2" data-cookieconsent="marketing"></iframe><iframe data-cookieblock-src="https://www.google.com/maps/embed?pb=test" data-cookieconsent="marketing"></iframe>'
116+
],
117+
'mixed content' => [
118+
'<p>Some text</p><iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe><p>More text</p><iframe src="https://vimeo.com/123456789"></iframe>',
119+
'<p>Some text</p><iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing"></iframe><p>More text</p><iframe data-cookieblock-src="https://vimeo.com/123456789" data-cookieconsent="marketing"></iframe>'
120+
],
121+
'iframe with frameborder and allowfullscreen' => [
122+
'<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen=""></iframe>',
123+
'<iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen="" data-cookieconsent="marketing"></iframe>'
124+
],
125+
'iframe with existing data-cookieconsent' => [
126+
'<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing"></iframe>',
127+
'<iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing"></iframe>'
128+
],
129+
'iframe with different data-cookieconsent value' => [
130+
'<iframe src="https://vimeo.com/123456789" data-cookieconsent="statistics"></iframe>',
131+
'<iframe data-cookieblock-src="https://vimeo.com/123456789" data-cookieconsent="statistics"></iframe>'
132+
],
133+
'no matching iframe' => [
134+
'<iframe src="https://example.com/video"></iframe>',
135+
'<iframe src="https://example.com/video"></iframe>'
136+
],
137+
'no iframe at all' => [
138+
'<p>Just some text content</p>',
139+
'<p>Just some text content</p>'
140+
],
141+
'empty content' => [
142+
'',
143+
''
144+
]
145+
];
146+
}
147+
}

etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
3+
<type name="Magento\PageBuilder\Model\Filter\Template">
4+
<plugin name="customgento_cookiebot_pagebuilder_template_filter_plugin" type="CustomGento\Cookiebot\Plugin\PageBuilder\Model\Filter\TemplatePlugin" sortOrder="10"/>
5+
</type>
6+
</config>

view/frontend/templates/iframe-handler.phtml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ if ($viewModel && $viewModel->isBlockVideosUntilConsentEnabled()) {
5353
((document, selector, consentType) => {
5454
const createTextNode = text => document.createTextNode(text);
5555
const createElement = element => document.createElement(element);
56-
console.log("here");
56+
5757
document.querySelectorAll(selector).forEach(iframe => {
5858
const linkElement = createElement("a");
5959
const divElement = createElement("div");
@@ -93,20 +93,27 @@ if ($viewModel && $viewModel->isBlockVideosUntilConsentEnabled()) {
9393
createTextNode(` to view this ${serviceProvider} content.`)
9494
);
9595
96+
divElement.style.fontSize = "1.4rem";
9697
divElement.append(paragraphElement);
9798
iframe.parentNode.insertBefore(divElement, iframe);
9899
});
99100
})(document, "iframe[data-cookieblock-src]", "marketing");
100101
});
101102
';
102103

104+
$newScript = '
105+
document.addEventListener("DOMContentLoaded", function() {
106+
alert("test");
107+
})
108+
';
109+
103110
// Render scripts with secure renderer
104-
echo $secureRenderer->renderTag(
105-
'script',
106-
['type' => 'text/javascript', 'data-cookieconsent' => 'ignore'],
107-
$addRequiredAttributesScript,
108-
false
109-
);
111+
// echo $secureRenderer->renderTag(
112+
// 'script',
113+
// ['type' => 'text/javascript', 'data-cookieconsent' => 'ignore'],
114+
// $addRequiredAttributesScript,
115+
// false
116+
// );
110117

111118
echo $secureRenderer->renderTag(
112119
'script',
@@ -115,4 +122,11 @@ if ($viewModel && $viewModel->isBlockVideosUntilConsentEnabled()) {
115122
false
116123
);
117124
}
118-
?>
125+
?>
126+
127+
<!--<script>-->
128+
<!-- document.addEventListener("DOMContentLoaded", function() {-->
129+
<!-- const gallery = document.querySelector('.fotorama__wrap .fotorama__stage');-->
130+
<!-- gallery.classList.add('fotorama-video-container');-->
131+
<!-- }-->
132+
<!--</script>-->

0 commit comments

Comments
 (0)