diff --git a/composer.json b/composer.json index 09f321a..b623a40 100755 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/services/OembedService.php b/src/services/OembedService.php index 2b64e1c..ff6f3fc 100755 --- a/src/services/OembedService.php +++ b/src/services/OembedService.php @@ -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; @@ -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 @@ -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); @@ -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'); @@ -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 @@ -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); } } @@ -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; @@ -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'; } } @@ -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; - } - } }