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
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.
  • Loading branch information
Matt Porritt committed Jul 26, 2023
1 parent 6d38972 commit d0a5e3f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
5 changes: 2 additions & 3 deletions filter/mediaplugin/tests/filter_test.php
Expand Up @@ -95,9 +95,8 @@ function test_filter_mediaplugin_link() {
'<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');

Expand Down
2 changes: 2 additions & 0 deletions media/player/youtube/lang/en/media_youtube.php
Expand Up @@ -27,3 +27,5 @@
$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.';
36 changes: 36 additions & 0 deletions media/player/youtube/settings.php
@@ -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));
}
10 changes: 5 additions & 5 deletions media/player/youtube/templates/embed.mustache
Expand Up @@ -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"
Expand All @@ -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>
23 changes: 23 additions & 0 deletions media/player/youtube/tests/player_test.php
Expand Up @@ -184,4 +184,27 @@ public function test_embed_media() {
$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);
}
}
2 changes: 1 addition & 1 deletion media/player/youtube/version.php
Expand Up @@ -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).

0 comments on commit d0a5e3f

Please sign in to comment.