Skip to content

Commit

Permalink
MDL-62401 Media: Embed Youtube Videos with nocookie extension
Browse files Browse the repository at this point in the history
Refactor YouTube media player to use templates.
Use mustache templates for rendering YouTube embed
iframe code.
  • Loading branch information
Matt Porritt committed Jul 26, 2023
1 parent b4cd637 commit 6d38972
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 18 deletions.
58 changes: 40 additions & 18 deletions media/player/youtube/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function list_supported_urls(array $urls, array $options = array()) {
}

protected function embed_external(moodle_url $url, $name, $width, $height, $options) {
global $OUTPUT;
$nocookie = get_config('media_youtube', 'nocookie');

$info = trim($name ?? '');
if (empty($info) or strpos($info, 'http') === 0) {
Expand All @@ -71,40 +73,60 @@ protected function embed_external(moodle_url $url, $name, $width, $height, $opti

self::pick_video_size($width, $height);

if ($this->isplaylist) {
// Template context.
$context = [
'width' => $width,
'height' => $height,
'title' => $info
];

if ($this->isplaylist) {
$site = $this->matches[1];
$playlist = $this->matches[3];

return <<<OET
<span class="mediaplugin mediaplugin_youtube">
<iframe width="$width" height="$height" src="https://$site/embed/videoseries?list=$playlist" frameborder="0"
allowfullscreen="1" style="max-width: 100%;"></iframe>
</span>
OET;
} else {
$params = ['list' => $playlist];

// Handle no cookie option.
if (!$nocookie) {
$embedurl = new moodle_url("https://$site/embed/videoseries", $params);
} else {
$embedurl = new moodle_url('https://www.youtube-nocookie.com/embed/videoseries', $params );
}
$context['embedurl'] = $embedurl->out(false);

// Return the rendered template.
return $OUTPUT->render_from_template('media_youtube/embed', $context);

} else {
$videoid = end($this->matches);
$params = '';
$params = [];
$start = self::get_start_time($url);
if ($start > 0) {
$params .= "start=$start&amp;";
$params['start'] = $start;
}

$listid = $url->param('list');
// Check for non-empty but valid playlist ID.
if (!empty($listid) && !preg_match('/[^a-zA-Z0-9\-_]/', $listid)) {
// This video is part of a playlist, and we want to embed it as such.
$params .= "list=$listid&amp;";
$params['list'] = $listid;
}

return <<<OET
<span class="mediaplugin mediaplugin_youtube">
<iframe title="$info" width="$width" height="$height"
src="https://www.youtube.com/embed/$videoid?{$params}rel=0&amp;wmode=transparent" frameborder="0"
allowfullscreen="1" style="max-width: 100%;"></iframe>
</span>
OET;
// Add parameters to object to be passed to the mustache template.
$params['rel'] = 0;
$params['wmode'] = 'transparent';

// Handle no cookie option.
if (!$nocookie) {
$embedurl = new moodle_url('https://www.youtube.com/embed/' . $videoid, $params );
} else {
$embedurl = new moodle_url('https://www.youtube-nocookie.com/embed/' . $videoid, $params );
}

$context['embedurl'] = $embedurl->out(false);

// Return the rendered template.
return $OUTPUT->render_from_template('media_youtube/embed', $context);
}

}
Expand Down
41 changes: 41 additions & 0 deletions media/player/youtube/templates/embed.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template media/embed
This template will render the YouTube embeded player.
Variables required for this template:
* info: The title of the video.
* width: The width of the video.
* height: The height of the video.
* embedurl: The URL to the video.
Example context (json):
{
"info": "YouTube video",
"width": 640,
"height": 360,
"embedurl": "https://www.youtube.com/embed/9bZkp7q19f0?rel=0&amp;wmode=transparent"
}

}}

<span class="mediaplugin mediaplugin_youtube">
<iframe title="{{info}}" width="{{width}}" height="{{height}}" style="border:0;"
src="{{{embedurl}}}" allow="fullscreen"></iframe>
</span>

0 comments on commit 6d38972

Please sign in to comment.