Skip to content

Commit

Permalink
Added YouTube Shorts
Browse files Browse the repository at this point in the history
  • Loading branch information
zoglo committed Aug 17, 2023
1 parent a5eaab5 commit 9c069b8
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/Resources/contao/classes/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ public static function generateAttributeLink($link, $settings, $videoType = null

$matches = [];

if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $link, $matches))
if (
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/|youtube\.com/shorts/)([^"&?/ ]{11})%i', $link, $matches) &&
isset($matches[1])
)
{
$parsedLink = 'https://www.youtube.com/embed/'.$matches[1];
}
Expand All @@ -299,7 +302,10 @@ public static function generateAttributeLink($link, $settings, $videoType = null

$matches = [];

if (preg_match('%vimeo\.com/(?:channels/(?:\w+/)?|groups/(?:[^/]+)/videos/|album/(?:\d+)/video/)?(\d+)(?:$|/|\?)%i', $link, $matches))
if (
preg_match('%vimeo\.com/(?:channels/(?:\w+/)?|groups/(?:[^/]+)/videos/|album/(?:\d+)/video/)?(\d+)(?:$|/|\?)%i', $link, $matches) &&
isset($matches[1])
)
{
$parsedLink = 'https://player.vimeo.com/video/'.$matches[1];
}
Expand All @@ -308,18 +314,27 @@ public static function generateAttributeLink($link, $settings, $videoType = null

$arrLink = parse_url($link);

// Consider "youtu.be" short links and rewrite them
if (!isset($arrLink['query']) && 'youtube' === $videoType && $arrLink['host'] === 'youtu.be')
if (!isset($arrLink['query']) && 'youtube' === $videoType )
{
return $arrLink['scheme'].'://www.youtube.com/embed'. ($arrLink['path']) .'?'.http_build_query(array_merge($arrSettings, $defaultParams));
// Consider "youtu.be" short links and rewrite them
if ($arrLink['host'] === 'youtu.be')
{
return $arrLink['scheme'].'://www.youtube.com/embed'. ($arrLink['path']) .'?'.http_build_query(array_merge($arrSettings, $defaultParams));
}

// Consider YouTube shorts
if ($arrLink['host'] === 'youtube.com')
{
return $parsedLink.'?'.http_build_query(array_merge($arrSettings, $defaultParams));
}
}

parse_str($arrLink['query'], $query);

// merge params
$param = array_merge($query, $arrSettings, $defaultParams);

if ($parsedLink)
if (!!$parsedLink)
{
return $parsedLink.'?'.http_build_query($param);
}
Expand Down

0 comments on commit 9c069b8

Please sign in to comment.