Skip to content

Commit

Permalink
Merge pull request #102 from Simplon-Prod/fix-bug-101
Browse files Browse the repository at this point in the history
Fix for issue #101
  • Loading branch information
reganlawton authored Feb 27, 2022
2 parents b52e78c + bdd31b7 commit a75cc4e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"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.13",
"version": "1.3.14",
"keywords": [
"craft",
"cms",
Expand Down
75 changes: 39 additions & 36 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
namespace wrav\oembed\services;

use craft;
use craft\base\Component;
use craft\helpers\Template;
use craft\helpers\UrlHelper;
use craft\base\Component;
use DOMDocument;
use Embed\Adapters\Adapter;
use Embed\Embed;
Expand All @@ -33,6 +33,23 @@
*/
class OembedService extends Component
{
/**
* @param $url
* @param array $options
* @return string
*/
public function render($url, array $options = [])
{
/** @var Media $media */
$media = $this->embed($url, $options);

if (!empty($media)) {
return Template::raw(isset($media->code) ? $media->code : '');
} else {
return null;
}
}

/**
* @param $url
* @param array $options
Expand All @@ -45,7 +62,7 @@ public function embed($url, array $options = [])
} catch (\Exception $exception) {
$hash = '';
}
$cacheKey = $url.'_'.$hash;
$cacheKey = $url . '_' . $hash;

if (Oembed::getInstance()->getSettings()->enableCache && Craft::$app->cache->exists($cacheKey)) {
return \Craft::$app->cache->get($cacheKey);
Expand Down Expand Up @@ -82,7 +99,11 @@ public function embed($url, array $options = [])
// Wrapping to be safe :)
try {
$dom = new DOMDocument;
$dom->loadHTML($media->getCode());
$code = $media->getCode();
if (empty($code)) {
$code = Utils::iframe($media->url);
}
$dom->loadHTML($code);

$iframe = $dom->getElementsByTagName('iframe')->item(0);
$src = $iframe->getAttribute('src');
Expand All @@ -94,15 +115,15 @@ public function embed($url, array $options = [])
$src .= "?";
}

if(!empty($options['params'])) {
foreach((array)$options['params'] as $key => $value) {
$src = preg_replace('/\?(.*)$/i', '?'.$key.'='. $value .'&${1}', $src);
if (!empty($options['params'])) {
foreach ((array)$options['params'] as $key => $value) {
$src = preg_replace('/\?(.*)$/i', '?' . $key . '=' . $value . '&${1}', $src);
}
}

// Autoplay
if (!empty($options['autoplay']) && strpos($html, 'autoplay=') === false && $src) {
$src = preg_replace('/\?(.*)$/i', '?autoplay='. (!!$options['autoplay'] ? '1' : '0') .'&${1}', $src);
$src = preg_replace('/\?(.*)$/i', '?autoplay=' . (!!$options['autoplay'] ? '1' : '0') . '&${1}', $src);
}

// Width - Override
Expand All @@ -117,21 +138,21 @@ public function embed($url, array $options = [])

// Looping
if (!empty($options['loop']) && strpos($html, 'loop=') === false && $src) {
$src = preg_replace('/\?(.*)$/i', '?loop='. (!!$options['loop'] ? '1' : '0') .'&${1}', $src);
$src = preg_replace('/\?(.*)$/i', '?loop=' . (!!$options['loop'] ? '1' : '0') . '&${1}', $src);
}

// Autopause
if (!empty($options['autopause']) && strpos($html, 'autopause=') === false && $src) {
$src = preg_replace('/\?(.*)$/i', '?autopause='. (!!$options['autopause'] ? '1' : '0') .'&${1}', $src);
$src = preg_replace('/\?(.*)$/i', '?autopause=' . (!!$options['autopause'] ? '1' : '0') . '&${1}', $src);
}

// Rel
if (!empty($options['rel']) && strpos($html, 'rel=') === false && $src) {
$src = preg_replace('/\?(.*)$/i', '?rel='. (!!$options['rel'] ? '1' : '0') .'&${1}', $src);
$src = preg_replace('/\?(.*)$/i', '?rel=' . (!!$options['rel'] ? '1' : '0') . '&${1}', $src);
}

if(!empty($options['attributes'])) {
foreach((array)$options['attributes'] as $key => $value) {
if (!empty($options['attributes'])) {
foreach ((array)$options['attributes'] as $key => $value) {
$iframe->setAttribute($key, $value);
}
}
Expand All @@ -141,8 +162,7 @@ public function embed($url, array $options = [])

} catch (\Exception $exception) {
Craft::info($exception->getMessage(), 'oembed');
}
finally {
} finally {
if (Oembed::getInstance()->getSettings()->enableCache) {
// Cache failed requests only for 15 minutes
$duration = $media instanceof FallbackAdapter ? 15 * 60 : 60 * 60;
Expand All @@ -161,19 +181,19 @@ private function manageGDPR($url)
$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)) {
if (count($matches)) {
$url = preg_replace($youtubePattern, 'https://www.youtube-nocookie.com', $url);
$skip = true;
}

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

Expand All @@ -185,21 +205,4 @@ private function manageGDPR($url)

return $url;
}

/**
* @param $url
* @param array $options
* @return string
*/
public function render($url, array $options = [])
{
/** @var Media $media */
$media = $this->embed($url, $options);

if (!empty($media)) {
return Template::raw(isset($media->code) ? $media->code : '');
} else {
return null;
}
}
}

0 comments on commit a75cc4e

Please sign in to comment.