MDL-62401 Media: Embed Youtube Videos with nocookie extension

Add config option to embed YouTube links using the youtube-nocookie.com
YouTube domain. This will stop extra YouTube cookies from being added
to the users computer it also stops calls to certain add tracking sites.
This commit is contained in:
Matt Porritt 2023-06-24 16:10:32 +10:00
parent 6d38972cbc
commit d0a5e3fb39
6 changed files with 69 additions and 9 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

@ -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

@ -15,19 +15,19 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template media/embed
@template media_youtube/embed
This template will render the YouTube embeded player.
Variables required for this template:
* info: The title of the video.
* 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):
{
"info": "YouTube video",
"title": "YouTube video",
"width": 640,
"height": 360,
"embedurl": "https://www.youtube.com/embed/9bZkp7q19f0?rel=0&amp;wmode=transparent"
@ -36,6 +36,6 @@
}}
<span class="mediaplugin mediaplugin_youtube">
<iframe title="{{info}}" width="{{width}}" height="{{height}}" style="border:0;"
src="{{{embedurl}}}" allow="fullscreen"></iframe>
<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).