From 6d38972cbc7755828bcc553ba09503852fa8cfb3 Mon Sep 17 00:00:00 2001 From: Matt Porritt Date: Sat, 24 Jun 2023 15:23:36 +1000 Subject: [PATCH] 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. --- media/player/youtube/classes/plugin.php | 58 +++++++++++++------ media/player/youtube/templates/embed.mustache | 41 +++++++++++++ 2 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 media/player/youtube/templates/embed.mustache diff --git a/media/player/youtube/classes/plugin.php b/media/player/youtube/classes/plugin.php index 10216231903..acc71f488d5 100644 --- a/media/player/youtube/classes/plugin.php +++ b/media/player/youtube/classes/plugin.php @@ -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; - } 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; + // 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); } } diff --git a/media/player/youtube/templates/embed.mustache b/media/player/youtube/templates/embed.mustache new file mode 100644 index 00000000000..f4f64fe56fe --- /dev/null +++ b/media/player/youtube/templates/embed.mustache @@ -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 . +}} +{{! + @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" + } + +}} + + + +