From 1ba43a9c6fb1e3790762773f314bdb6a5b035600 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Gaudreau Date: Wed, 22 Aug 2012 09:05:11 -0400 Subject: [PATCH] MDL-34997 - Allow shortened url youtu.be and y2u.be for Youtube filter --- filter/mediaplugin/tests/filter_test.php | 3 +++ lib/medialib.php | 18 +++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/filter/mediaplugin/tests/filter_test.php b/filter/mediaplugin/tests/filter_test.php index 86876102204..c0044fb6ab5 100644 --- a/filter/mediaplugin/tests/filter_test.php +++ b/filter/mediaplugin/tests/filter_test.php @@ -57,6 +57,9 @@ class filter_mediaplugin_testcase extends advanced_testcase { 'test mpg', 'test', 'test file', + 'test file', + 'test file', + 'test file', 'test file', 'test flv', 'test file', diff --git a/lib/medialib.php b/lib/medialib.php index fb8cdf6b7ae..e75e07743be 100644 --- a/lib/medialib.php +++ b/lib/medialib.php @@ -526,8 +526,7 @@ OET; */ class core_media_player_youtube extends core_media_player_external { protected function embed_external(moodle_url $url, $name, $width, $height, $options) { - $site = $this->matches[1]; - $videoid = $this->matches[3]; + $videoid = end($this->matches); $info = trim($name); if (empty($info) or strpos($info, 'http') === 0) { @@ -540,17 +539,22 @@ class core_media_player_youtube extends core_media_player_external { return << + src="https://www.youtube.com/embed/$videoid?rel=0&wmode=transparent" frameborder="0" allowfullscreen="1"> OET; } protected function get_regex() { + // Regex for standard youtube link + $link = '(youtube(-nocookie)?\.com/(?:watch\?v=|v/))'; + // Regex for shortened youtube link + $shortlink = '((youtu|y2u)\.be/)'; + // Initial part of link. - $start = '~^https?://(www\.youtube(-nocookie)?\.com)/'; - // Middle bit: either watch?v= or v/. - $middle = '(?:watch\?v=|v/)([a-z0-9\-_]+)'; + $start = '~^https?://(www\.)?(' . $link . '|' . $shortlink . ')'; + // Middle bit: Video key value + $middle = '([a-z0-9\-_]+)'; return $start . $middle . core_media_player_external::END_LINK_REGEX_PART; } @@ -561,7 +565,7 @@ OET; } public function get_embeddable_markers() { - return array('youtube'); + return array('youtube.com', 'youtube-nocookie.com', 'youtu.be', 'y2u.be'); } }