Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Model/ExternalVideoReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
class ExternalVideoReplacer
{
public function replaceIframeSources(string $content): string
{
$content = $this->replaceIframeTags($content);
$content = $this->replaceVideoBackgroundAttributes($content);

return $content;
}

private function replaceIframeTags(string $content): string
{
$iframePatterns = [
// YouTube patterns
Expand Down Expand Up @@ -36,4 +44,35 @@ public function replaceIframeSources(string $content): string

return $content;
}

private function replaceVideoBackgroundAttributes(string $content): string
{
// Pattern to match elements with data-video-src attribute containing YouTube or Vimeo URLs
$videoBackgroundPatterns = [
// YouTube patterns in data-video-src
'/(<[^>]+)data-video-src=["\'](https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/[^"\']+)["\']([^>]*>)/i',
'/(<[^>]+)data-video-src=["\'](https?:\/\/(?:www\.)?youtu\.be\/[^"\']+)["\']([^>]*>)/i',
// Vimeo patterns in data-video-src
'/(<[^>]+)data-video-src=["\'](https?:\/\/(?:www\.)?vimeo\.com\/[^"\']+)["\']([^>]*>)/i',
'/(<[^>]+)data-video-src=["\'](https?:\/\/(?:www\.)?player\.vimeo\.com\/[^"\']+)["\']([^>]*>)/i'
];

foreach ($videoBackgroundPatterns as $pattern) {
$content = preg_replace_callback($pattern, function (array $matches) {
$beforeAttr = $matches[1];
$videoUrl = $matches[2];
$afterAttr = $matches[3];

// Check if data-cookieconsent already exists
if (preg_match('/data-cookieconsent=["\'][^"\']*["\']/', $beforeAttr . $afterAttr)) {
return $beforeAttr . 'data-cookieblock-src="' . $videoUrl . '"' . $afterAttr;
}

return $beforeAttr . 'data-cookieblock-src="' . $videoUrl
. '" data-cookieconsent="marketing"' . $afterAttr;
}, $content);
}

return $content;
}
}
7 changes: 5 additions & 2 deletions view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ var config = {
'Magento_GoogleAnalytics/js/google-analytics': {
'CustomGento_Cookiebot/js/google-analytics-mixin': true
},
'Magento_ProductVideo/js/fotorama-add-video-events': {
'CustomGento_Cookiebot/js/fotorama-video-events-mixin': true
'Magento_PageBuilder/js/widget/video-background': {
'CustomGento_Cookiebot/js/video-background-mixin': true
},
'Magento_PageBuilder/js/content-type/slide/appearance/default/widget': {
'CustomGento_Cookiebot/js/slide-widget-mixin': true
},
'Magento_ProductVideo/js/fotorama-add-video-events': {
'CustomGento_Cookiebot/js/fotorama-video-events-mixin': true
}
}
}
};
22 changes: 4 additions & 18 deletions view/frontend/web/js/slide-widget-mixin.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
define([
'jquery',
'underscore',
'CustomGento_Cookiebot/js/video-blocker-widget',
'CustomGento_Cookiebot/js/video-platform-validator',
'Magento_PageBuilder/js/widget/show-on-hover',
'Magento_PageBuilder/js/widget/video-background',
'CustomGento_Cookiebot/js/video-blocker-widget'
], function ($, _, showOnHover, videoBackground, createVideoBlocker) {
'Magento_PageBuilder/js/widget/video-background'
], function ($, _, createVideoBlocker, isSupportedVideoPlatform, showOnHover, videoBackground) {
'use strict';

return function (originalWidget) {
function isSupportedVideoPlatform(url) {
if (!url) {
return false;
}

// Regex patterns for supported video platforms
const youtubePattern = /^https?:\/\/(www\.)?(youtube\.com|youtu\.be)\//i;
const youtubeNocookiePattern = /^https?:\/\/(www\.)?youtube-nocookie\.com\//i;
const vimeoPattern = /^https?:\/\/(www\.)?(vimeo\.com|player\.vimeo\.com)\//i;

return youtubePattern.test(url) ||
youtubeNocookiePattern.test(url) ||
vimeoPattern.test(url);
}

return function (config, element) {
const videoElement = element[0].querySelector('[data-background-type=video]');

Expand Down
79 changes: 79 additions & 0 deletions view/frontend/web/js/video-background-mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
define([
'jquery',
'CustomGento_Cookiebot/js/video-blocker-widget',
'CustomGento_Cookiebot/js/video-platform-validator'
], function ($, createVideoBlocker, isSupportedVideoPlatform) {
'use strict';

return function (originalWidget) {
function getValidDimension(value, fallback) {
if (!value || parseFloat(value) === 0) {
return fallback;
}
return value;
}

return function (config, element) {
const videoElementContainer = $(element);
const videoElement = videoElementContainer[0];
const videoElementStyles = window.getComputedStyle(element);
const height = getValidDimension(videoElementStyles.minHeight, '300px');
const width = getValidDimension(videoElementStyles.width, '400px');

if (videoElementContainer.data('background-type') !== 'video') {
originalWidget(config, element);
return;
}

const blockVideoConsentConfig = window.cookiebotConfig && window.cookiebotConfig.blockVideosUntilConsent;
const videoSrc = videoElement.getAttribute('data-video-src');
const cookieblockSrc = videoElement.getAttribute('data-cookieblock-src');
const src = videoSrc || cookieblockSrc;
let previousStatus = '';
let blockerElement = null;

if (!blockVideoConsentConfig || !isSupportedVideoPlatform(src)) {
originalWidget(config, element);
return;
}

addEventListener('CookiebotOnLoad', videoBackgroundBlocker);

function videoBackgroundBlocker() {
if (previousStatus === 'blocked' && (!window.Cookiebot?.consent?.marketing)) {
return;
}

if (!window.Cookiebot?.consent?.marketing) {
if (videoSrc) {
videoElement.setAttribute('data-cookieblock-src', videoSrc);
videoElement.removeAttribute('data-video-src');
}
videoElement.style.display = 'none';
blockerElement = createVideoBlocker(videoElement);
const blockerElementContent = blockerElement.querySelector('div');
blockerElementContent.style.height = height;
blockerElementContent.style.width = width;
previousStatus = 'blocked';
return;
}

if (!videoElement.getAttribute('data-video-src') && cookieblockSrc) {
videoElement.setAttribute('data-video-src', cookieblockSrc);
videoElement.removeAttribute('data-cookieblock-src');
}
videoElement.style.display = 'block';

if (blockerElement) {
blockerElement.remove();
blockerElement = null;
}

originalWidget(config, element);
previousStatus = 'unblocked';
}

videoBackgroundBlocker();
};
};
});
17 changes: 17 additions & 0 deletions view/frontend/web/js/video-platform-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
define([], function () {
'use strict';

return function isSupportedVideoPlatform(url) {
if (!url) {
return false;
}

const youtubePattern = /^https?:\/\/(www\.)?(youtube\.com|youtu\.be)\//i;
const youtubeNocookiePattern = /^https?:\/\/(www\.)?youtube-nocookie\.com\//i;
const vimeoPattern = /^https?:\/\/(www\.)?(vimeo\.com|player\.vimeo\.com)\//i;

return youtubePattern.test(url) ||
youtubeNocookiePattern.test(url) ||
vimeoPattern.test(url);
};
});
Loading