Skip to content

Commit

Permalink
tidy and pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell committed Feb 12, 2025
1 parent 92a95b7 commit 87921e1
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/Tokens/Handlers/LivePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Closure;
use Facades\Statamic\CP\LivePreview as Facade;
use Illuminate\Support\Collection;
use Statamic\Contracts\Tokens\Token;
use Statamic\Facades\Site;
use Statamic\Facades\Site as Sites;
use Statamic\Sites\Site;

class LivePreview
{
Expand All @@ -17,20 +19,26 @@ public function handle(Token $token, $request, Closure $next)

$response = $next($request);

if (Site::multiEnabled()) {
$validURLs = Site::all()
->map(function ($site) {
$parts = parse_url($site->absoluteUrl());

return $parts['scheme'].'://'.$parts['host'];
})->values()
if (Sites::multiEnabled()) {
/** @var Collection */
$siteURLs = Sites::all()
->map(fn (Site $site) => $this->getSchemeAndHost($site))
->values()
->unique()
->join(' ');
$response->headers->set('Content-Security-Policy "frame-ancestors '.$validURLs.'"');

$response->headers->set('Content-Security-Policy', "frame-ancestors '$siteURLs'");
}

$response->headers->set('X-Statamic-Live-Preview', true);

return $response;
}

private function getSchemeAndHost(Site $site): string
{
$parts = parse_url($site->absoluteUrl());

return $parts['scheme'].'://'.$parts['host'];
}
}

0 comments on commit 87921e1

Please sign in to comment.