Skip to content

Commit

Permalink
fix: Fixes issue with src attribute not being set on iframe, preview …
Browse files Browse the repository at this point in the history
…handling and GQL issue due to missing `.code` prop
  • Loading branch information
reganlawton committed Dec 15, 2023
1 parent eb87b2c commit b8aea3e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# oEmbed Changelog

## 3.0.6 - 2023-12-15

### Update
- Fixed issue with src attribute not being set on iframe. Reported by @holiday-jan in issue #146. Thanks @holiday-jan
- Rework preview handling
- Updated README docs
- Fallback .code for certain providers, this completely blocked GraphQL users. Reported by @yoannisj in issue #129. Thanks @yoannisj

## 3.0.5 - 2023-12-08

### Update
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ Updating the embed URL, such as autoplay, rel, mute paramaters. This allows for

{{
entry.oembed_field.render({
params: {
autoplay: 1,
rel: 0,
mute: 0,
loop: 1,
autopause: 1,
},
autoplay: 1,
rel: 0,
mute: 0,
loop: 1,
autopause: 1,
attributes: {
title: 'Main title',
'data-title': 'Some other title',
Expand Down
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": "3.0.5",
"version": "3.0.6",
"keywords": [
"craft",
"cms",
Expand Down
3 changes: 3 additions & 0 deletions src/adapters/EmbedAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function __construct($data)
$this->$key = $this->data[$keyName] ?? null;
}

// Fallback for .code incase it's empty
$this->code = $this->code ?: $this->getCode();

// Custom mapping

// Fallback for .image
Expand Down
16 changes: 11 additions & 5 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function embed($url, array $options = [], array $cacheProps = [])
}

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

Expand All @@ -167,28 +167,34 @@ public function embed($url, array $options = [], array $cacheProps = [])
}

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

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

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

// Apply attributes to the iframe
if (!empty($options['attributes'])) {
foreach ((array)$options['attributes'] as $key => $value) {
$iframe->setAttribute($key, $value);
}
}

// Set the SRC
$iframe->setAttribute('src', $src);
$media->code = $dom->saveXML($iframe, LIBXML_NOEMPTYTAG);

// Set the code
$code = $dom->saveXML($iframe, LIBXML_NOEMPTYTAG);

$media->code = $code;
} catch (\Exception $exception) {
Craft::info($exception->getMessage(), 'oembed');
} finally {
Expand Down
10 changes: 6 additions & 4 deletions src/templates/preview.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{% set media = craft.oembed.embed(url) %}

{% if media and media.code|default(false) %}
{{ media.code|default('')|raw }}
{% if url is defined and url is not null and url is not empty or craft.oembed.valid(url) %}
{{
craft.oembed.render(
url,
)
}}
{% else %}
<p class="error">Please check your URL.</p>
{% endif %}

0 comments on commit b8aea3e

Please sign in to comment.