Skip to content

Commit

Permalink
v1.1.3
Browse files Browse the repository at this point in the history
- Added `valid` method to handle errors gracefully (Thanks @iparr).
- Added data caching for parsed URLs to help increase page response time.
- Updated docs
  • Loading branch information
reganlawton committed Mar 29, 2019
1 parent 27af9a9 commit 9a142fb
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# oEmbed Changelog

``
- Updated docs.

## 1.1.2 - 2019-02-18

### Updated
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ To install the plugin, follow these instructions.

To use simply call one of the following methods on your field type

{{ entry.field.embed }}
{{ entry.field.valid }}
{{ entry.field.render }}
{{ entry.field.embed }}
{{ entry.field.media }}

We also provide option to use as a Twig variable

{{ craft.oembed.valid(url, options) }}
{{ craft.oembed.render(url, options) }}
{% set embed = craft.oembed.embed(url, options) %}
{% set media = craft.oembed.media(url, options) %}

You can access additional media details using the examples below.

Expand Down Expand Up @@ -63,7 +67,7 @@ You can access additional media details using the examples below.
entry.field.media.linkedData
entry.field.media.feeds

Additional Essense information can be found [here](https://github.com/essence/essence)
Additional Embed information can be found [here](https://github.com/oscarotero/Embed)

## Credits

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": "1.1.2",
"version": "1.1.3",
"keywords": [
"craft",
"cms",
Expand Down
10 changes: 10 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
[
{
"version": "1.1.3",
"downloadUrl": "https://github.com/wrav/oembed/archive/master.zip",
"date": "2019-03-29:00:00+10:30",
"notes": [
"[Added] Added `valid` method to handle errors gracefully (Thanks @iparr).",
"[Added] Added data caching for parsed URLs to help increase page response time.",
"[Updated] Updated docs."
]
},
{
"version": "1.1.2",
"downloadUrl": "https://github.com/wrav/oembed/archive/master.zip",
Expand Down
9 changes: 9 additions & 0 deletions src/models/OembedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,13 @@ public function media(array $options = [])
{
return $this->embed($options);
}

/**
* @return boolean
*/
public function valid()
{
$media = $this->embed();
return (!empty($media) && isset($media->code));
}
}
34 changes: 26 additions & 8 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace wrav\oembed\services;

use craft;
use craft\helpers\Template;
use craft\base\Component;
use Embed\Adapters\Adapter;
Expand All @@ -31,16 +32,29 @@ class OembedService extends Component
*/
public function embed($url, array $options = [])
{
array_multisort($options);
if (Craft::$app->cache->exists($url)) {
return \Craft::$app->cache->get($url);
}

/** @var Adapter $media */
$media = Embed::create($url, $options);
try {
array_multisort($options);

if ($media) {
return $media;
}
/** @var Adapter $media */
$media = Embed::create($url, $options);
} finally {
if (!empty($media)) {
Craft::$app->cache->set($url, $media, 'P1H');
return $media;
}

return null;
return new class {
// Returns NULL for calls to props
public function __call(string $name , array $arguments )
{
return null;
}
};
}
}

/**
Expand All @@ -53,6 +67,10 @@ public function render($url, array $options = [])
/** @var Media $media */
$media = $this->embed($url, $options);

return Template::raw(isset($media->code) ? $media->code : '');
if (!empty($media)) {
return Template::raw(isset($media->code) ? $media->code : '');
} else {
return null;
}
}
}
15 changes: 15 additions & 0 deletions src/variables/OembedVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@ public function embed($url, array $options = [])
{
return Oembed::getInstance()->oembedService->embed($url, $options);
}

/**
* Call it like this:
*
* {{ craft.oembed.valid(url, options) }}
*
* @param $url
* @param array $options
* @return string
*/
public function valid($url, array $options = [])
{
$media = $this->embed($url, $options);
return (!empty($media) && isset($media->code));
}
}

0 comments on commit 9a142fb

Please sign in to comment.