diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c8b0ac..d6e3ff3 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # oEmbed Changelog +## 2.3.0 - 2023-02-02 + +### Update +- Added default caching keys to resolve missing field data while cached + ## 2.2.2 - 2022-08-26 ### Update diff --git a/README.md b/README.md index 44d0c6b..398461c 100755 --- a/README.md +++ b/README.md @@ -56,10 +56,10 @@ To use simply call one of the following methods on your field type 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) %} + {{ craft.oembed.valid(url, options, cacheFields) }} + {{ craft.oembed.render(url, options, cacheFields) }} + {% set embed = craft.oembed.embed(url, options, cacheFields) %} + {% set media = craft.oembed.media(url, options, cacheFields) %} Updating the embed URL, such as autoplay, rel, mute paramaters. This allows for you to support features the provider might not yet support @@ -127,6 +127,48 @@ You can access additional media details using the examples below. Additional Embed information can be found [here](https://github.com/oscarotero/Embed) +## Cache + +By default, the plugin will cache the following keys on the oembed object. The plugin can cache additional missing fields using the cache prop parameter which will take an array of strings. + + {{ + entry.oembed_field.render( + { + width: 640, + height: 480, + }, + [ + 'cacheable_key' + ] + ) + }} + +### Default Keys + +- title +- description +- url +- type +- tags +- images +- image +- imageWidth +- imageHeight +- code +- width +- height +- aspectRatio +- authorName +- authorUrl +- providerName +- providerUrl +- providerIcons +- providerIcon +- publishedDate +- license +- linkedData +- feeds + ## GraphQL I recommend enabling caching in the plugin settings menu to speed up the API resolve timing. diff --git a/composer.json b/composer.json index 64068cc..a4ad4f9 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": "2.2.2", + "version": "2.3.0", "keywords": [ "craft", "cms", diff --git a/src/models/OembedModel.php b/src/models/OembedModel.php index a89089f..f3a768b 100644 --- a/src/models/OembedModel.php +++ b/src/models/OembedModel.php @@ -132,13 +132,13 @@ public function getUrl() * @param array $options * @return string */ - public function render(array $options = []) + public function render(array $options = [], array $cacheProps = []) { if (empty($this->getUrl())) { return null; } - return Oembed::getInstance()->oembedService->render($this->getUrl(), $options); + return Oembed::getInstance()->oembedService->render($this->getUrl(), $options, $cacheProps); } /** @@ -147,38 +147,38 @@ public function render(array $options = []) * @param array $options * @return string */ - public function embed(array $options = []) + public function embed(array $options = [], array $cacheProps = []) { if (empty($this->getUrl())) { return null; } - return Oembed::getInstance()->oembedService->embed($this->getUrl(), $options); + return Oembed::getInstance()->oembedService->embed($this->getUrl(), $options, $cacheProps); } /** * @param array $options * @return string */ - public function media(array $options = []) + public function media(array $options = [], array $cacheProps = []) { if (empty($this->getUrl())) { return null; } - return $this->embed($options); + return $this->embed($options, $cacheProps); } /** * @return boolean */ - public function valid(array $options = []) + public function valid(array $options = [], array $cacheProps = []) { if (empty($this->getUrl())) { return false; } - $media = $this->embed($options); + $media = $this->embed($options, $cacheProps); return (!empty($media) && isset($media->code)); } } diff --git a/src/services/OembedService.php b/src/services/OembedService.php index 2a8880c..f18bf64 100755 --- a/src/services/OembedService.php +++ b/src/services/OembedService.php @@ -39,10 +39,10 @@ class OembedService extends Component * @param array $options * @return string */ - public function render($url, array $options = []) + public function render($url, array $options = [], array $cacheProps = []) { /** @var Media $media */ - $media = $this->embed($url, $options); + $media = $this->embed($url, $options, $cacheProps); if (!empty($media)) { return Template::raw(isset($media->code) ? $media->code : ''); @@ -56,10 +56,10 @@ public function render($url, array $options = []) * @param array $options * @return Media|string */ - public function embed($url, array $options = []) + public function embed($url, array $options = [], array $cacheProps = []) { try { - $hash = md5(json_encode($options)); + $hash = md5(json_encode($options)).md5(json_encode($cacheProps)); } catch (\Exception $exception) { $hash = ''; } @@ -167,6 +167,42 @@ public function embed($url, array $options = []) // Cache failed requests only for 15 minutes $duration = $media instanceof FallbackAdapter ? 15 * 60 : 60 * 60; + $defaultCacheKeys = [ + 'title', + 'description', + 'url', + 'type', + 'tags', + 'images', + 'image', + 'imageWidth', + 'imageHeight', + 'code', + 'width', + 'height', + 'aspectRatio', + 'authorName', + 'authorUrl', + 'providerName', + 'providerUrl', + 'providerIcons', + 'providerIcon', + 'publishedDate', + 'license', + 'linkedData', + 'feeds', + ]; + + $cacheKeys = array_unique(array_merge($defaultCacheKeys, $cacheProps)); + + foreach ($cacheKeys as $key) { + try { + $media->{$key} = $media->{$key}; + } catch (\Exception $e) { + $media->{$key} = null; + } + } + Craft::$app->cache->set($cacheKey, json_decode(json_encode($media)), $duration); } return $media; diff --git a/src/variables/OembedVariable.php b/src/variables/OembedVariable.php index 599a1fa..012d262 100755 --- a/src/variables/OembedVariable.php +++ b/src/variables/OembedVariable.php @@ -30,13 +30,13 @@ class OembedVariable * @param array $options * @return string */ - public function render($url, array $options = []) + public function render($url, array $options = [], array $cacheProps = []) { if (empty($url)) { return null; } - return Oembed::getInstance()->oembedService->render($url, $options); + return Oembed::getInstance()->oembedService->render($url, $options, $cacheProps); } /** @@ -48,13 +48,13 @@ public function render($url, array $options = []) * @param array $options * @return string */ - public function embed($url, array $options = []) + public function embed($url, array $options = [], array $cacheProps = []) { if (empty($url)) { return null; } - return Oembed::getInstance()->oembedService->embed($url, $options); + return Oembed::getInstance()->oembedService->embed($url, $options, $cacheProps); } /** @@ -66,13 +66,13 @@ public function embed($url, array $options = []) * @param array $options * @return bool */ - public function valid($url, array $options = []) + public function valid($url, array $options = [], array $cacheProps = []) { if (empty($url)) { return false; } - $media = $this->embed($url, $options); + $media = $this->embed($url, $options, $cacheProps); return (!empty($media) && isset($media->code)); } }