40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
|
|
<?php
|
||
|
|
/** @var \Kirby\Cms\Block $block */
|
||
|
|
$url = $block->url()->value();
|
||
|
|
$caption = $block->caption()->value();
|
||
|
|
|
||
|
|
// Fonction pour détecter le type de vidéo
|
||
|
|
function getVideoEmbedCode($url) {
|
||
|
|
// YouTube
|
||
|
|
if (preg_match('/youtube\.com\/watch\?v=([^&]+)/', $url, $matches) ||
|
||
|
|
preg_match('/youtu\.be\/([^?]+)/', $url, $matches)) {
|
||
|
|
$videoId = $matches[1];
|
||
|
|
return '<iframe src="https://www.youtube.com/embed/' . $videoId . '" frameborder="0" allowfullscreen></iframe>';
|
||
|
|
}
|
||
|
|
|
||
|
|
// Vimeo
|
||
|
|
if (preg_match('/vimeo\.com\/(\d+)/', $url, $matches)) {
|
||
|
|
$videoId = $matches[1];
|
||
|
|
return '<iframe src="https://player.vimeo.com/video/' . $videoId . '" frameborder="0" allowfullscreen></iframe>';
|
||
|
|
}
|
||
|
|
|
||
|
|
// Vidéo directe (mp4, webm, etc.)
|
||
|
|
if (preg_match('/\.(mp4|webm|ogg)$/i', $url)) {
|
||
|
|
return '<video controls><source src="' . $url . '" type="video/' . pathinfo($url, PATHINFO_EXTENSION) . '"></video>';
|
||
|
|
}
|
||
|
|
|
||
|
|
// Par défaut, iframe
|
||
|
|
return '<iframe src="' . $url . '" frameborder="0" allowfullscreen></iframe>';
|
||
|
|
}
|
||
|
|
?>
|
||
|
|
|
||
|
|
<?php if ($url): ?>
|
||
|
|
<div class="container-video">
|
||
|
|
<?= getVideoEmbedCode($url) ?>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<?php if ($caption): ?>
|
||
|
|
<p class="caption"><?= $caption ?></p>
|
||
|
|
<?php endif ?>
|
||
|
|
<?php endif ?>
|