47 lines
1.8 KiB
PHP
47 lines
1.8 KiB
PHP
@php
|
|
$url = $block->content['url'] ?? '';
|
|
$caption = $block->content['caption'] ?? '';
|
|
$title = e($block->content['title'] ?? 'Video');
|
|
|
|
$allowedEmbedHosts = ['www.youtube.com', 'youtube.com', 'youtu.be', 'www.vimeo.com', 'vimeo.com', 'player.vimeo.com'];
|
|
$parsed = parse_url($url);
|
|
$host = strtolower($parsed['host'] ?? '');
|
|
$isEmbed = in_array($host, $allowedEmbedHosts, true);
|
|
|
|
// Normalise YouTube watch URLs to embed URLs
|
|
if (in_array($host, ['www.youtube.com', 'youtube.com'], true) && str_contains($url, '/watch')) {
|
|
parse_str($parsed['query'] ?? '', $qs);
|
|
if (!empty($qs['v'])) {
|
|
$url = 'https://www.youtube.com/embed/' . rawurlencode($qs['v']);
|
|
}
|
|
} elseif ($host === 'youtu.be') {
|
|
$videoId = ltrim($parsed['path'] ?? '', '/');
|
|
$url = 'https://www.youtube.com/embed/' . rawurlencode($videoId);
|
|
} elseif (in_array($host, ['www.vimeo.com', 'vimeo.com'], true)) {
|
|
$videoId = ltrim($parsed['path'] ?? '', '/');
|
|
$url = 'https://player.vimeo.com/video/' . rawurlencode($videoId);
|
|
}
|
|
@endphp
|
|
|
|
<figure class="my-4">
|
|
@if($isEmbed)
|
|
<div class="relative w-full" style="padding-bottom: 56.25%;">
|
|
<iframe
|
|
src="{{ $url }}"
|
|
title="{{ $title }}"
|
|
class="absolute inset-0 w-full h-full rounded"
|
|
frameborder="0"
|
|
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
|
allowfullscreen
|
|
></iframe>
|
|
</div>
|
|
@elseif($url)
|
|
<video src="{{ e($url) }}" controls class="w-full rounded">
|
|
Your browser does not support the video tag.
|
|
</video>
|
|
@endif
|
|
|
|
@if($caption)
|
|
<figcaption class="mt-2 text-sm text-center text-gray-500">{{ $caption }}</figcaption>
|
|
@endif
|
|
</figure>
|