From 29a2c52f7df44929eb05a02f3bf71cf8527c8487 Mon Sep 17 00:00:00 2001
From: IDMarinas <35842929+idmarinas@users.noreply.github.com>
Date: Sat, 24 Dec 2022 17:56:01 +0100
Subject: [PATCH] Now can pass nonce code to TwigGeneric event
---
src/Event/TwigGeneric.php | 15 +++++++++++++++
src/Twig/Extension/AdvertisingGeneric.php | 11 ++++++++++-
2 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/src/Event/TwigGeneric.php b/src/Event/TwigGeneric.php
index 686127b..6e9dad2 100644
--- a/src/Event/TwigGeneric.php
+++ b/src/Event/TwigGeneric.php
@@ -34,6 +34,7 @@ class TwigGeneric extends Event
private string $slot = '';
private array $scripts = [];
private array $slotConfig = [];
+ private string $nonce = '';
/** Set or reemplace banner selected. */
public function setBanner(string $banner): self
@@ -104,4 +105,18 @@ public function setScripts(array $scripts): self
return $this;
}
+
+ /** Get nonce (CSP) string */
+ public function getNonce(): string
+ {
+ return $this->nonce;
+ }
+
+ /** Set a nonce string for CSP script/style */
+ public function setNonce(string $nonce): self
+ {
+ $this->nonce = $nonce;
+
+ return $this;
+ }
}
diff --git a/src/Twig/Extension/AdvertisingGeneric.php b/src/Twig/Extension/AdvertisingGeneric.php
index 75fe6bf..23b87e4 100644
--- a/src/Twig/Extension/AdvertisingGeneric.php
+++ b/src/Twig/Extension/AdvertisingGeneric.php
@@ -112,6 +112,15 @@ public function showScripts(?string $network = null): string
return '';
}
- return '';
+ $attributes = array_filter([
+ 'nonce' => (string) $event->getNonce(),
+ ]);
+
+ $attributes = urldecode(http_build_query(array_map(fn($v) => '"'.$v.'"', $attributes), '', ' '));
+
+ return sprintf(
+ '',
+ $attributes
+ );
}
}