Skip to content

Commit

Permalink
MDL-34997 - Allow shortened url youtu.be and y2u.be for Youtube filter
Browse files Browse the repository at this point in the history
  • Loading branch information
gaudreaj authored and Frederic Massart committed Sep 3, 2012
1 parent 569f1ad commit 1ba43a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 3 additions & 0 deletions filter/mediaplugin/tests/filter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ function test_filter_mediaplugin_link() {
'<a id="movie player" class="center" href="http://moodle.org/testfile/test.mpg">test mpg</a>',
'<a href="http://moodle.org/testfile/test.ram">test</a>',
'<a href="http://www.youtube.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>',
'<a href="http://www.youtube-nocookie.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>',
'<a href="http://youtu.be/JghQgA2HMX8" class="href=css">test file</a>',
'<a href="http://y2u.be/JghQgA2HMX8" class="href=css">test file</a>',
'<a class="youtube" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>',
'<a class="_blanktarget" href="http://moodle.org/testfile/test.flv?d=100x100">test flv</a>',
'<a class="hrefcss" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>',
Expand Down
18 changes: 11 additions & 7 deletions lib/medialib.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,7 @@ public function get_embeddable_markers() {
*/
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) {
Expand All @@ -540,17 +539,22 @@ protected function embed_external(moodle_url $url, $name, $width, $height, $opti
return <<<OET
<span class="mediaplugin mediaplugin_youtube">
<iframe title="$info" width="$width" height="$height"
src="https://$site/embed/$videoid?rel=0&wmode=transparent" frameborder="0" allowfullscreen="1"></iframe>
src="https://www.youtube.com/embed/$videoid?rel=0&wmode=transparent" frameborder="0" allowfullscreen="1"></iframe>
</span>
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;
}

Expand All @@ -561,7 +565,7 @@ public function get_rank() {
}

public function get_embeddable_markers() {
return array('youtube');
return array('youtube.com', 'youtube-nocookie.com', 'youtu.be', 'y2u.be');
}
}

Expand Down

0 comments on commit 1ba43a9

Please sign in to comment.