Skip to content
Snippets Groups Projects
Commit 86300a0c authored by Andrew Dolgov's avatar Andrew Dolgov
Browse files

add urlhelper to extract youtube video id from url

parent d11718c8
No related branches found
No related tags found
No related merge requests found
...@@ -487,4 +487,26 @@ class UrlHelper { ...@@ -487,4 +487,26 @@ class UrlHelper {
} }
} }
public static function url_to_youtube_vid($url) {
$url = str_replace("youtube.com", "youtube-nocookie.com", $url);
$regexps = [
"/\/\/www\.youtube-nocookie\.com\/v\/([\w-]+)/",
"/\/\/www\.youtube-nocookie\.com\/embed\/([\w-]+)/",
"/\/\/www\.youtube-nocookie\.com\/watch?v=([\w-]+)/",
"/\/\/youtu.be\/([\w-]+)/",
];
foreach ($regexps as $re) {
$matches = [];
if (preg_match($re, $url, $matches)) {
return $matches[1];
}
}
return false;
}
} }
...@@ -16,18 +16,16 @@ class Af_Youtube_Embed extends Plugin { ...@@ -16,18 +16,16 @@ class Af_Youtube_Embed extends Plugin {
} }
function hook_iframe_whitelisted($src) { function hook_iframe_whitelisted($src) {
return in_array($src, ["www.youtube.com", "youtube.com", "youtu.be"]); return in_array($src, ["www.youtube.com", "youtube.com",
"www.youtube-nocookie.com", "youtube-nocookie.com",
"youtu.be"]);
} }
function hook_render_enclosure($entry, $hide_images) { function hook_render_enclosure($entry, $hide_images) {
$matches = array(); $url = $entry["content_url"];
if (preg_match("/\/\/www\.youtube\.com\/v\/([\w-]+)/", $entry["content_url"], $matches) || if ($vid_id = UrlHelper::url_to_youtube_vid($url)) {
preg_match("/\/\/www\.youtube\.com\/watch?v=([\w-]+)/", $entry["content_url"], $matches) ||
preg_match("/\/\/youtu.be\/([\w-]+)/", $entry["content_url"], $matches)) {
$vid_id = $matches[1];
return "<div class='embed-responsive'> return "<div class='embed-responsive'>
<iframe class='youtube-player' <iframe class='youtube-player'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment