mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 04:22:07 +02:00
MDL-62401 Media: Embed Youtube Videos with nocookie extension
Refactor YouTube media player to use templates. Use mustache templates for rendering YouTube embed iframe code.
This commit is contained in:
parent
b4cd63746a
commit
6d38972cbc
@ -62,6 +62,8 @@ class media_youtube_plugin extends core_media_player_external {
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -71,40 +73,60 @@ class media_youtube_plugin extends core_media_player_external {
|
||||
|
||||
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&";
|
||||
$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&";
|
||||
$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&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);
|
||||
}
|
||||
|
||||
}
|
||||
|
41
media/player/youtube/templates/embed.mustache
Normal file
41
media/player/youtube/templates/embed.mustache
Normal file
@ -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&wmode=transparent"
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
<span class="mediaplugin mediaplugin_youtube">
|
||||
<iframe title="{{info}}" width="{{width}}" height="{{height}}" style="border:0;"
|
||||
src="{{{embedurl}}}" allow="fullscreen"></iframe>
|
||||
</span>
|
Loading…
x
Reference in New Issue
Block a user