Skip to content

Commit

Permalink
Merge pull request #43 from wrav/feature/gdpr
Browse files Browse the repository at this point in the history
Add GDPR support for Youtube and Vimeo
  • Loading branch information
reganlawton authored May 13, 2020
2 parents 47ddc7c + 9c75423 commit 02fe15b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# oEmbed Changelog

## 1.3.1 - 2020-05-08

### Added
- *(NEW FEATURE)* Added GDPR setting to transform URL's for Youtube and Vimeo to their GDPR versions. This happen's without needing to change existing URL's.

## 1.3.0 - 2020-03-05

### Added
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
"name": "wrav/oembed",
"description": "A simple plugin to extract media information from websites, like youtube videos, twitter statuses or blog articles.",
"type": "craft-plugin",
"version": "1.3.0",
"version": "1.3.1",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"oembed",
"graphql",
"gdpr",
"iframe",
"youtube",
"vimeo",
Expand Down
10 changes: 6 additions & 4 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
*/
class Settings extends Model
{
// Public Properties
// =========================================================================

/**
* @var string
* @var bool
*/
public $enableCache;

/**
* @var bool
*/
public $enableGdpr;

/**
* @var string
*/
Expand Down
34 changes: 34 additions & 0 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function __call(string $name , array $arguments )
$iframe = $dom->getElementsByTagName('iframe')->item(0);
$src = $iframe->getAttribute('src');

$src = $this->manageGDPR($src);

if(!empty($options['params'])) {
foreach((array)$options['params'] as $key => $value) {
$src = preg_replace('/\?(.*)$/i', '?'.$key.'='. $value .'&${1}', $src);
Expand Down Expand Up @@ -128,6 +130,38 @@ public function __call(string $name , array $arguments )
}
}

private function manageGDPR($url)
{
if (Oembed::getInstance()->getSettings()->enableGdpr) {
$skip = false;
$youtubePattern = '/(?:http|https)*?:*\/\/(?:www\.|)(?:youtube\.com|m\.youtube\.com|youtu\.be|youtube-nocookie\.com)/i';
preg_match($youtubePattern, $url, $matches, PREG_OFFSET_CAPTURE);

if(count($matches)) {
$url = preg_replace($youtubePattern, 'https://www.youtube-nocookie.com', $url);
$skip = true;
}

if(!$skip) {
if (strpos($url, 'vimeo.com') !== false ) {
if (strpos($url, 'dnt=') === false) {
preg_match('/\?.*$/', $url, $matches, PREG_OFFSET_CAPTURE);
if(count($matches)) {
$url = preg_replace('/(\?(.*))$/i', '?dnt=1&${2}', $url);
} else {
$url = $url.'?dnt=1';
}
}

$url = preg_replace('/(dnt=(1|0))/i', 'dnt=1', $url);
$skip = true;
}
}
}

return $url;
}

/**
* @param $url
* @param array $options
Expand Down
8 changes: 8 additions & 0 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
on: settings.enableCache,
}) }}

{{ forms.lightswitchField({
label: "Enable GDPR"|t,
instructions: "Currently supporting Youtube & Vimeo"|t,
id: 'enableGdpr',
name: 'enableGdpr',
on: settings.enableGdpr,
}) }}

{{ forms.lightswitchField({
label: "Previews hidden by default"|t,
instructions: "This will hide preview in collapsable component"|t,
Expand Down

0 comments on commit 02fe15b

Please sign in to comment.