Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.2.0 #27

Merged
merged 2 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
composer.lock
.DS_Store
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -29,7 +30,8 @@
],
"require": {
"craftcms/cms": "^3.0.0-beta.23",
"embed/embed": "^3.3"
"embed/embed": "^3.3",
"laravel/helpers": "^1.1"
},
"repositories": [
{
Expand Down
2 changes: 0 additions & 2 deletions src/Oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ protected function createSettingsModel()
*/
protected function settingsHtml(): string
{
// dd($this->getSettings());

return Craft::$app->view->renderTemplate(
'oembed/settings',
[
Expand Down
39 changes: 35 additions & 4 deletions src/fields/OembedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,12 +37,15 @@ class OembedField extends Field
// =========================================================================

/**
* Some attribute
*
* @var string
*/
public $url = '';

/**
* @var array
*/
public $oembed = [];

// Static Methods
// =========================================================================

Expand Down Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/gql/OembedFieldResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* oEmbed plugin for Craft CMS 3.x
*
* A simple plugin to extract media information from websites, like youtube videos, twitter statuses or blog articles.
*
* @link https://github.com/wrav
* @copyright Copyright (c) 2017 reganlawton
*/

namespace wrav\oembed\gql;

use craft\gql\base\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;

class OembedFieldResolver extends ObjectType
{

/**
* @inheritdoc
*/
protected function resolve($source, $arguments, $context, ResolveInfo $resolveInfo)
{
$fieldName = $resolveInfo->fieldName;

return $source->$fieldName;
}
}
71 changes: 71 additions & 0 deletions src/gql/OembedFieldTypeGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* oEmbed plugin for Craft CMS 3.x
*
* A simple plugin to extract media information from websites, like youtube videos, twitter statuses or blog articles.
*
* @link https://github.com/wrav
* @copyright Copyright (c) 2017 reganlawton
*/

namespace wrav\oembed\gql;

use craft\gql\base\GeneratorInterface;
use craft\gql\GqlEntityRegistry;
use craft\gql\TypeLoader;
use GraphQL\Type\Definition\Type;

class OembedFieldTypeGenerator implements GeneratorInterface
{
/**
* @inheritdoc
*/
public static function generateTypes($context = null): array
{
/** @var OembedField $context */
$typeName = self::getName($context);

$properties = [
'title' => 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';
}
}
28 changes: 26 additions & 2 deletions src/models/OembedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace wrav\oembed\models;

use Embed\Adapters\Youtube;
use wrav\oembed\Oembed;
use craft\base\Model;

Expand All @@ -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
Expand All @@ -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.
*
Expand Down
12 changes: 8 additions & 4 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Embed\Adapters\Adapter;
use Embed\Embed;
use wrav\oembed\Oembed;
use yii\log\Logger;

/**
* OembedService Service
Expand Down Expand Up @@ -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;
}
}
Expand Down