From 067706bdbe5f2e2af879d6834d4951ab419441b1 Mon Sep 17 00:00:00 2001 From: reganlawton Date: Sun, 19 Jan 2020 21:12:06 +1100 Subject: [PATCH 1/2] v1.2.0 ### Updated - Support `rel` URL propety via DOM manipulation. ([#24](https://github.com/wrav/oembed/issues/24)) - Added GraphQL support. ([#25](https://github.com/wrav/oembed/issues/25)) - Updated docs ### Fixed - Fixed "Undefined index: autoplay" warnings. ([#26](https://github.com/wrav/oembed/issues/26)) --- .gitignore | 3 +- CHANGELOG.md | 10 ++++ README.md | 29 ++++++++++-- composer.json | 3 +- src/Oembed.php | 2 - src/fields/OembedField.php | 39 +++++++++++++-- src/gql/OembedFieldResolver.php | 28 +++++++++++ src/gql/OembedFieldTypeGenerator.php | 71 ++++++++++++++++++++++++++++ src/models/OembedModel.php | 28 ++++++++++- src/services/OembedService.php | 12 +++-- 10 files changed, 207 insertions(+), 18 deletions(-) create mode 100644 src/gql/OembedFieldResolver.php create mode 100644 src/gql/OembedFieldTypeGenerator.php diff --git a/.gitignore b/.gitignore index 88e99d5..e5294f4 100755 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor -composer.lock \ No newline at end of file +composer.lock +.DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c73a50..6885173 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # oEmbed Changelog +## 1.2.0 - 2020-01-19 + +### Updated +- Support `rel` URL propety via DOM manipulation. ([#24](https://github.com/wrav/oembed/issues/24)) +- Added GraphQL support. ([#25](https://github.com/wrav/oembed/issues/25)) +- Updated docs + +### Fixed +- Fixed "Undefined index: autoplay" warnings. ([#26](https://github.com/wrav/oembed/issues/26)) + ## 1.1.8 - 2019-09-16 ### Updated diff --git a/README.md b/README.md index d66d064..6433afc 100755 --- a/README.md +++ b/README.md @@ -29,10 +29,10 @@ To install the plugin, follow these instructions. To use simply call one of the following methods on your field type - {{ entry.field.valid }} - {{ entry.field.render }} - {{ entry.field.embed }} - {{ entry.field.media }} + {{ entry.field.valid }} # Get the embed object + {{ entry.field.render }} # Renders HTML + {{ entry.field.embed }} # Get the embed object + {{ entry.field.media }} # Get the embed object We also provide option to use as a Twig variable @@ -69,6 +69,27 @@ You can access additional media details using the examples below. Additional Embed information can be found [here](https://github.com/oscarotero/Embed) +## GraphQl + +I recommend enabling caching in the plugin settings menu to speed up the API resolve timing. + +Below is an example of a Oembed field called "foobar" add accessing properties from the embed object. + +``` +{ + entries { + id, + ... on page_page_Entry { + foobar { + code, + providerUrl, + aspectRatio + } + } + } +} +``` + ## Credits Original built while working at [HutSix](https://hutsix.com.au/) I've since been granted permission to continue development here. diff --git a/composer.json b/composer.json index bda2bd0..2458e70 100755 --- a/composer.json +++ b/composer.json @@ -29,7 +29,8 @@ ], "require": { "craftcms/cms": "^3.0.0-beta.23", - "embed/embed": "^3.3" + "embed/embed": "^3.3", + "laravel/helpers": "^1.1" }, "repositories": [ { diff --git a/src/Oembed.php b/src/Oembed.php index 9222708..d029c7d 100755 --- a/src/Oembed.php +++ b/src/Oembed.php @@ -154,8 +154,6 @@ protected function createSettingsModel() */ protected function settingsHtml(): string { -// dd($this->getSettings()); - return Craft::$app->view->renderTemplate( 'oembed/settings', [ diff --git a/src/fields/OembedField.php b/src/fields/OembedField.php index ef36ad5..d6f303d 100755 --- a/src/fields/OembedField.php +++ b/src/fields/OembedField.php @@ -13,6 +13,14 @@ use Craft; use craft\base\ElementInterface; use craft\base\Field; +use craft\elements\MatrixBlock as MatrixBlockElement; +use craft\gql\arguments\elements\MatrixBlock as MatrixBlockArguments; +use craft\gql\resolvers\elements\MatrixBlock as MatrixBlockResolver; +use craft\gql\types\generators\MatrixBlockType as MatrixBlockTypeGenerator; +use craft\gql\types\QueryArgument; +use craft\helpers\Gql as GqlHelper; +use GraphQL\Type\Definition\Type; +use wrav\oembed\gql\OembedFieldTypeGenerator; use yii\db\Schema; use wrav\oembed\models\OembedModel; @@ -29,12 +37,15 @@ class OembedField extends Field // ========================================================================= /** - * Some attribute - * * @var string */ public $url = ''; + /** + * @var array + */ + public $oembed = []; + // Static Methods // ========================================================================= @@ -70,6 +81,21 @@ public function getContentColumnType(): string return Schema::TYPE_TEXT; } + /** + * @inheritdoc + * @since 3.3.0 + */ + public function getContentGqlType() + { + $typeArray = OembedFieldTypeGenerator::generateTypes($this); + + return [ + 'name' => $this->handle, + 'description' => "Oembed field", + 'type' => array_shift($typeArray), + ]; + } + /** * @param mixed $value The raw field value * @param ElementInterface|null $element The element the field is associated with, if there is one @@ -78,12 +104,17 @@ public function getContentColumnType(): string */ public function normalizeValue($value, ElementInterface $element = null) { - if (is_string($value) && $decValue = json_decode($value, true)) { + if (is_string($value) && $decValue = json_decode($value, true)) { if (isset($decValue['url'])) { return new OembedModel($decValue['url']); } } - return $value ? new OembedModel($value) : null; + + $oembed = $value ? new OembedModel($value) : null; + + $this->oembed = $oembed; + + return $oembed; } /** diff --git a/src/gql/OembedFieldResolver.php b/src/gql/OembedFieldResolver.php new file mode 100644 index 0000000..7089138 --- /dev/null +++ b/src/gql/OembedFieldResolver.php @@ -0,0 +1,28 @@ +fieldName; + + return $source->$fieldName; + } +} diff --git a/src/gql/OembedFieldTypeGenerator.php b/src/gql/OembedFieldTypeGenerator.php new file mode 100644 index 0000000..2cafc34 --- /dev/null +++ b/src/gql/OembedFieldTypeGenerator.php @@ -0,0 +1,71 @@ + Type::string(), + 'description' => Type::string(), + 'url' => Type::string(), + 'type' => Type::string(), + 'images' => Type::string(), + 'image' => Type::string(), + 'imageWidth' => Type::string(), + 'imageHeight' => Type::string(), + 'code' => Type::string(), + 'width' => Type::string(), + 'height' => Type::string(), + 'aspectRatio' => Type::string(), + 'authorName' => Type::string(), + 'authorUrl' => Type::string(), + 'providerName' => Type::string(), + 'providerUrl' => Type::string(), + ]; + + $property = GqlEntityRegistry::getEntity($typeName) + ?: GqlEntityRegistry::createEntity($typeName, new OembedFieldResolver([ + 'name' => $typeName, + 'description' => 'This entity has all the Oembed Field properties', + 'fields' => function () use ($properties) { + return $properties; + }, + ])); + + TypeLoader::registerType($typeName, function () use ($property) { + return $property; + }); + + return [$property]; + } + + /** + * @inheritdoc + */ + public static function getName($context = null): string + { + /** @var OembedField $context */ + return $context->handle . '_OembedField'; + } +} diff --git a/src/models/OembedModel.php b/src/models/OembedModel.php index e4ddc6d..8568f62 100755 --- a/src/models/OembedModel.php +++ b/src/models/OembedModel.php @@ -10,6 +10,7 @@ namespace wrav\oembed\models; +use Embed\Adapters\Youtube; use wrav\oembed\Oembed; use craft\base\Model; @@ -26,12 +27,15 @@ class OembedModel extends Model // ========================================================================= /** - * Some model attribute - * * @var string */ public $url = ''; + /** + * @var mixed + */ + public $oembed = null; + /** * OembedModel constructor. * @param string $url @@ -49,6 +53,26 @@ public function __toString() return "".$this->getUrl(); } + public function __get($name) + { + if (property_exists($this , $name)) { + return $this->$name; + } + + if ($this->oembed === null) { + $oembed = Oembed::getInstance()->oembedService->embed($this->url); + + if (!$oembed) { + $this->embed = []; + } + + $this->oembed = $oembed; + } + + return $this->oembed->$name; + } + + /** * Returns the validation rules for attributes. * diff --git a/src/services/OembedService.php b/src/services/OembedService.php index 43e5171..2d954a3 100755 --- a/src/services/OembedService.php +++ b/src/services/OembedService.php @@ -17,6 +17,7 @@ use Embed\Adapters\Adapter; use Embed\Embed; use wrav\oembed\Oembed; +use yii\log\Logger; /** * OembedService Service @@ -72,23 +73,26 @@ public function __call(string $name , array $arguments ) $src = $iframe->getAttribute('src'); // Autoplay - if ($options['autoplay'] && strpos($html, 'autoplay=') === false && $src) { + if (!empty($options['autoplay']) && strpos($html, 'autoplay=') === false && $src) { $src = preg_replace('/\?(.*)$/i', '?autoplay='. (!!$options['autoplay'] ? '1' : '0') .'&${1}', $src); } // Looping - if ($options['loop'] && strpos($html, 'loop=') === false && $src) { + if (!empty($options['loop']) && strpos($html, 'loop=') === false && $src) { $src = preg_replace('/\?(.*)$/i', '?loop='. (!!$options['loop'] ? '1' : '0') .'&${1}', $src); } // Autopause - if ($options['autopause'] && strpos($html, 'autopause=') === false && $src) { + if (!empty($options['autopause']) && strpos($html, 'autopause=') === false && $src) { $src = preg_replace('/\?(.*)$/i', '?autopause='. (!!$options['autopause'] ? '1' : '0') .'&${1}', $src); } $iframe->setAttribute('src', $src); $media->code = $dom->saveXML($iframe); - } finally { + } catch (\Exception $exception) { + Craft::info($exception->getMessage(), 'oembed'); + } + finally { return $media; } } From 3adb509f29898800b15f53be0f40e2236023ef2e Mon Sep 17 00:00:00 2001 From: reganlawton Date: Sun, 19 Jan 2020 21:14:56 +1100 Subject: [PATCH 2/2] Add new tag info --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2458e70..2f2f417 100755 --- a/composer.json +++ b/composer.json @@ -2,13 +2,14 @@ "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.8", + "version": "1.2.0", "keywords": [ "craft", "cms", "craftcms", "craft-plugin", "oembed", + "graphql", "iframe", "youtube", "vimeo",