MDL-78571 Media: Allow Vimeo do not track option

Add config option to embed Vimeo links using the "do not track"
Vimeo option. This will block the player from tracking any
session data, including all cookies and analytics.
This commit is contained in:
Matt Porritt 2023-06-25 11:29:30 +10:00
parent 0d0e543007
commit 04f0448d26
8 changed files with 81 additions and 14 deletions

View File

@ -35,21 +35,29 @@ defined('MOODLE_INTERNAL') || die();
class media_vimeo_plugin extends core_media_player_external {
protected function embed_external(moodle_url $url, $name, $width, $height, $options) {
global $OUTPUT;
$donottrack = get_config('media_vimeo', 'donottrack');
$videoid = $this->get_video_id();
$info = s($name);
$params = [];
// Note: resizing via url is not supported, user can click the fullscreen
// button instead. iframe embedding is not xhtml strict but it is the only
// option that seems to work on most devices.
self::pick_video_size($width, $height);
$embedurl = new moodle_url("https://player.vimeo.com/video/$videoid");
// Add do not track parameter.
if ($donottrack) {
$params['dnt'] = 1;
}
$embedurl = new moodle_url("https://player.vimeo.com/video/$videoid", $params);
// Template context.
$context = [
'width' => $width,
'height' => $height,
'title' => $info,
'embedurl' => $embedurl->out(false),
'width' => $width,
'height' => $height,
'title' => $info,
'embedurl' => $embedurl->out(false),
];
// Return the rendered template.

View File

@ -25,3 +25,5 @@
$string['pluginname'] = 'Vimeo';
$string['pluginname_help'] = 'The video-sharing website vimeo.com.';
$string['privacy:metadata'] = 'The Vimeo media plugin does not store any personal data.';
$string['donottrack'] = 'Enable do not track Vimeo links';
$string['donottrack_desc'] = 'If enabled, the player will not track any session data, including all cookies and analytics.';

View File

@ -0,0 +1,32 @@
<?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_vimeo'
*
* @package media_vimeo
* @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 a settings checkbox to enable or disable do not track vimeo links.
$settings->add(new admin_setting_configcheckbox('media_vimeo/donottrack',
new lang_string('donottrack', 'media_vimeo'),
new lang_string('donottrack_desc', 'media_vimeo'), 0));
}

View File

@ -21,6 +21,7 @@
in a special format for the Moodle mobile app.
Variables required for this template:
* lang: The language of the user.
* width: The width of the video.
* height: The height of the video.
* style: The style info for iframe.
@ -28,6 +29,7 @@
Example context (json):
{
"lang": "en",
"width": 640,
"height": 360,
"style": "position:absolute; top:0; left:0; width:100%; height:100%;",
@ -35,10 +37,11 @@
}
}}
<html>
<!DOCTYPE html>
<html lang="{{lang}}">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>{{#str}} pluginname, media_vimeo {{/str}}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body style="margin:0; padding:0">
<iframe {{#style}}style="{{style}}" {{/style}}

View File

@ -20,14 +20,14 @@
This template will render the Vimeo 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": "Vimeo video",
"width": 640,
"height": 360,
"embedurl": "https://player.vimeo.com/video/226053498"
@ -36,6 +36,6 @@
}}
<span class="mediaplugin mediaplugin_vimeo">
<iframe title="{{info}}" width="{{width}}" height="{{height}}" style="border:0;"
<iframe title="{{title}}" width="{{width}}" height="{{height}}" style="border:0;"
src="{{{embedurl}}}" allow="fullscreen" loading="lazy"></iframe>
</span>

View File

@ -225,4 +225,21 @@ class player_test extends \advanced_testcase {
$this->assertDoesNotMatchRegularExpression('~mediaplugin_vimeo~', $content);
$this->assertDoesNotMatchRegularExpression('~</iframe>~', $content);
}
/**
* Test that Vimeo media plugin renders embed code correctly
* when the "do not track" config options is set to true.
*
* @covers \media_vimeo_plugin::embed_external
*/
public function test_vimeo_donottrack(): void {
// Turn on the do not track option.
set_config('donottrack', true, 'media_vimeo');
// Test that the embed code contains the do not track param in the url.
$url = new \moodle_url('https://vimeo.com/226053498');
$text = \html_writer::link($url, 'Watch this one');
$content = format_text($text, FORMAT_HTML);
$this->assertMatchesRegularExpression('~dnt=1~', $content);
}
}

View File

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

View File

@ -48,11 +48,16 @@ $h = optional_param('h', '', PARAM_ALPHANUM); // Security hash for restricted vi
$webservicelib = new webservice();
$webservicelib->authenticate_user($token);
$params = [];
$params = ['lang' => current_language()];
if (!empty($h)) {
$params['h'] = $h;
}
// Add do not track parameter.
if (get_config('media_vimeo', 'donottrack')) {
$params['dnt'] = 1;
}
$embedurl = new moodle_url("https://player.vimeo.com/video/$video", $params);
$context = ['embedurl' => $embedurl->out(false)]; // Template context.
@ -65,4 +70,4 @@ if (empty($width) && empty($height)) {
}
// Output the rendered template.
echo $OUTPUT->render_from_template('media_youtube/appembed', $context);
echo $OUTPUT->render_from_template('media_vimeo/appembed', $context);