Merge branch 'master_MDL-62401' of https://github.com/mattporritt/moodle

This commit is contained in:
Ilya Tregubov 2023-07-31 12:00:42 +08:00
commit aa06409241
No known key found for this signature in database
GPG Key ID: 0F58186F748E55C1
7 changed files with 145 additions and 22 deletions

View File

@ -95,9 +95,8 @@ class filter_test extends \advanced_testcase {
'<a href="https://www.youtube.com/watch?v=uUhWl9Lm3OM">Valid link</a></pre><pre style="color: rgb(0, 0, 0); line-height: normal;">';
$paddedurl = str_pad($originalurl, 6000, 'z');
$validpaddedurl = '<p>Some text.</p><pre style="color: rgb(0, 0, 0); line-height: normal;"><span class="mediaplugin mediaplugin_youtube">
<iframe title="Valid link" width="640" height="360"
src="https://www.youtube.com/embed/uUhWl9Lm3OM?rel=0&amp;wmode=transparent" frameborder="0"
allowfullscreen="1" style="max-width: 100%;"></iframe>
<iframe title="Valid link" width="640" height="360" style="border:0;"
src="https://www.youtube.com/embed/uUhWl9Lm3OM?rel=0&wmode=transparent" allow="fullscreen" loading="lazy"></iframe>
</span></pre><pre style="color: rgb(0, 0, 0); line-height: normal;">';
$validpaddedurl = str_pad($validpaddedurl, 6000 + (strlen($validpaddedurl) - strlen($originalurl)), 'z');

View File

@ -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&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);
}
}

View File

@ -27,3 +27,5 @@ $string['pluginname_help'] = 'The video-sharing website youtube.com. Video and p
$string['privacy:metadata'] = 'The Youtube media plugin does not store any personal data.';
$string['supportsvideo'] = 'YouTube videos';
$string['supportsplaylist'] = 'YouTube playlists';
$string['nocookie'] = 'Use no cookie domain';
$string['nocookie_desc'] = 'Use youtube-nocookie.com domain for embedding videos. This reduces the number of third party cookies used in embedding. This domain is also not blocked by some adblockers.';

View File

@ -0,0 +1,36 @@
<?php
// 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/>.
/**
* Settings file for plugin 'media_youtube'
*
* @package media_youtube
* @copyright 2023 Matt Porritt <matt.porritt@moodle.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if ($ADMIN->fulltree) {
// Add the settings page.
$settings->add(new admin_setting_heading('media_youtube_settings',
get_string('pluginname', 'media_youtube'),
get_string('pluginname_help', 'media_youtube')));
// Add a settings checkbox to enable or disable no cookie YouTube links.
$settings->add(new admin_setting_configcheckbox('media_youtube/nocookie',
new lang_string('nocookie', 'media_youtube'),
new lang_string('nocookie_desc', 'media_youtube'), 0));
}

View 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_youtube/embed
This template will render the YouTube embeded player.
Variables required for this template:
* title: 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):
{
"title": "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="{{title}}" width="{{width}}" height="{{height}}" style="border:0;"
src="{{{embedurl}}}" allow="fullscreen" loading="lazy"></iframe>
</span>

View File

@ -184,4 +184,27 @@ class player_test extends \advanced_testcase {
$this->assertMatchesRegularExpression('~</iframe>~', $content);
$this->assertMatchesRegularExpression('~width="123" height="35"~', $content);
}
/**
* Test that YouTube media plugin renders embed code correctly
* when the "nocookie" config options is set to true.
*
* @covers \media_youtube_plugin::embed_external
*/
public function test_youtube_nocookie() {
// Turn on the no cookie option.
set_config('nocookie', true, 'media_youtube');
// Test that the embed code contains the no cookie domain.
$url = new \moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
$text = \html_writer::link($url, 'Watch this one');
$content = format_text($text, FORMAT_HTML);
$this->assertMatchesRegularExpression('~youtube-nocookie~', $content);
// Next test for a playlist.
$url = new \moodle_url('https://www.youtube.com/playlist?list=PL59FEE129ADFF2B12');
$text = \html_writer::link($url, 'Great Playlist');
$content = format_text($text, FORMAT_HTML);
$this->assertMatchesRegularExpression('~youtube-nocookie~', $content);
}
}

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2023042400; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2023062400; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2023041800; // Requires this Moodle version.
$plugin->component = 'media_youtube'; // Full name of the plugin (used for diagnostics).