Skip to content

Commit 0569c76

Browse files
committed
Add observer, DEV-1111
Signed-off-by: Iman Aboheydary <iman@customgento.com>
1 parent a4e1fd0 commit 0569c76

File tree

6 files changed

+74
-79
lines changed

6 files changed

+74
-79
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CustomGento\Cookiebot\Observer;
6+
7+
use CustomGento\Cookiebot\Model\Config;
8+
use CustomGento\Cookiebot\Model\ExternalVideoReplacer;
9+
use Magento\Framework\Event\Observer;
10+
use Magento\Framework\Event\ObserverInterface;
11+
use Magento\Framework\App\Response\Http;
12+
use Magento\Framework\App\RequestInterface;
13+
use Psr\Log\LoggerInterface;
14+
15+
class HtmlContentFilterObserver implements ObserverInterface
16+
{
17+
public function __construct(
18+
private readonly LoggerInterface $logger,
19+
private readonly Config $config,
20+
private readonly ExternalVideoReplacer $externalVideoReplacer
21+
) {
22+
}
23+
24+
public function execute(Observer $observer): void
25+
{
26+
try {
27+
/** @var Http $response */
28+
$response = $observer->getData('response');
29+
30+
if (!$response instanceof Http) {
31+
return;
32+
}
33+
34+
$content = $response->getBody();
35+
36+
if (empty($content) || !is_string($content)) {
37+
return;
38+
}
39+
40+
// Only process if the block_videos_until_consent feature is enabled
41+
if (!$this->config->isBlockVideosUntilConsentEnabled()) {
42+
return;
43+
}
44+
45+
$modifiedContent = $this->externalVideoReplacer->replaceIframeSources($content);
46+
47+
// Filter and modify the content
48+
// $modifiedContent = $this->filterHtmlContent($content);
49+
50+
// Update the response body
51+
$response->setBody($modifiedContent);
52+
53+
} catch (\Exception $e) {
54+
$this->logger->error('Error in HtmlContentFilterObserver: ' . $e->getMessage());
55+
}
56+
}
57+
}

Plugin/Cms/Model/AddVideoBlockerToCmsContentPlugin.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

Plugin/PageBuilder/Model/Filter/AddVideoBlockerFilterPlugin.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

etc/di.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

etc/events.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:Event/etc/events.xsd">
3+
<event name="controller_front_send_response_before">
4+
<observer name="customgento_cookiebot_html_content_filter" instance="CustomGento\Cookiebot\Observer\HtmlContentFilterObserver" />
5+
</event>
6+
</config>

view/frontend/web/js/fotorama-video-events-mixin.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ define([
2020
const iframeHeight = videoElement?.getBoundingClientRect().height || 300;
2121
const iframeWidth = videoElement?.getBoundingClientRect().width || 400;
2222

23+
2324
divElement.innerHTML = `
2425
<div style="background-color:#CCC;display:inline-block;height:${iframeHeight}px;position:relative;width:${iframeWidth}px; z-index: 1000;">
2526
<div style="background-color:#848484;border-radius:15px;height:50%;position:absolute;transform:translate(50%,50%);width:50%;">
@@ -47,21 +48,17 @@ define([
4748
_initialize: function () {
4849
this._super();
4950
addEventListener("CookiebotOnAccept", () => {
50-
const videoIframes = document.querySelectorAll(".pagebuilder-video-container iframe");
51-
52-
videoIframes.forEach((iframe) => {
53-
if (Cookiebot?.consent?.marketing) {
54-
const cookiebotOutput = document?.querySelector('.cookieconsent-optout-marketing');
55-
const videoElement = cookiebotOutput.closest('.video-unplayed[aria-hidden="false"]');
51+
if (Cookiebot?.consent?.marketing) {
52+
const cookiebotOutput = document?.querySelector('.cookieconsent-optout-marketing');
53+
const videoElement = cookiebotOutput.closest('.video-unplayed[aria-hidden="false"]');
5654

57-
const event = new PointerEvent('click', {
58-
bubbles: true,
59-
cancelable: true,
60-
view: window
61-
});
62-
videoElement?.dispatchEvent(event);
63-
}
64-
});
55+
const event = new PointerEvent('click', {
56+
bubbles: true,
57+
cancelable: true,
58+
view: window
59+
});
60+
videoElement?.dispatchEvent(event);
61+
}
6562
});
6663
}
6764
});

0 commit comments

Comments
 (0)