Skip to content

Commit

Permalink
Now can pass nonce code to TwigGeneric event
Browse files Browse the repository at this point in the history
  • Loading branch information
idmarinas committed Dec 24, 2022
1 parent 774aeca commit 29a2c52
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Event/TwigGeneric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
11 changes: 10 additions & 1 deletion src/Twig/Extension/AdvertisingGeneric.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ public function showScripts(?string $network = null): string
return '';
}

return '<script async src="'.implode('"></script><script async src="', $scripts).'"></script>';
$attributes = array_filter([
'nonce' => (string) $event->getNonce(),
]);

$attributes = urldecode(http_build_query(array_map(fn($v) => '"'.$v.'"', $attributes), '', ' '));

return sprintf(
'<script %1$s async src="'.implode('"></script><script %1$s async src="', $scripts).'"></script>',
$attributes
);
}
}

0 comments on commit 29a2c52

Please sign in to comment.