Skip to content

Commit

Permalink
Merge pull request #39 from wrav/release/1.3.0
Browse files Browse the repository at this point in the history
Changes for v1.3.0
  • Loading branch information
reganlawton authored Mar 5, 2020
2 parents 9271b77 + 1bd3ebd commit 47ddc7c
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 14 deletions.
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.3.0 - 2020-03-05

### Added
- *(NEW FEATURE)* Added setting to hide preview iframe in the new a collapsable UI component.
- *(NEW FEATURE)* Added notification by email for broken URLs which can be set up in plugin settings. This feature is still under development and new feature's like Slack, Microsoft Team notification channels will be added including support to update the notify message copy.

### Updated
- Preview iframe is now rendered into a collapsable component to save space.
- Add `overflow:hidden` to iframe to prevent overflow ([#37](https://github.com/wrav/oembed/issues/37)).

## 1.2.5 - 2020-02-10

### Updated
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.2.5",
"version": "1.3.0",
"keywords": [
"craft",
"cms",
Expand Down
22 changes: 22 additions & 0 deletions src/Oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

namespace wrav\oembed;

use wrav\oembed\events\BrokenUrlEvent;
use wrav\oembed\fields\OembedField;
use wrav\oembed\jobs\BrokenUrlNotify;
use wrav\oembed\services\OembedService;
use wrav\oembed\variables\OembedVariable;
use wrav\oembed\models\Settings;
Expand All @@ -37,6 +39,8 @@
*/
class Oembed extends Plugin
{
const EVENT_BROKEN_URL_DETECTED = 'oembedBrokenUrlDetected';

// Static Properties
// =========================================================================

Expand All @@ -48,6 +52,11 @@ class Oembed extends Plugin
*/
public static $plugin;

/**
* @var string|null The plugin’s schema version number
*/
public $schemaVersion = '1.0.1';

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -116,6 +125,9 @@ function(Event $event) {
$url = Craft::$app->assetManager->getPublishedUrl('@wrav/oembed/assetbundles/oembed/dist/js/oembed.js', true);

echo "<script src='$url'></script>";

$url = Craft::$app->assetManager->getPublishedUrl('@wrav/oembed/assetbundles/oembed/dist/css/oembed.css', true);
echo "<link rel='stylesheet' href='$url'>";
}
}
);
Expand All @@ -128,6 +140,16 @@ function(RegisterUrlRulesEvent $event) {
}
);

Event::on(
Oembed::class,
Oembed::EVENT_BROKEN_URL_DETECTED,
function (BrokenUrlEvent $event) {
Craft::$app->getQueue()->push(new BrokenUrlNotify([
'url' => $event->url,
]));
}
);

Craft::info(
Craft::t(
'oembed',
Expand Down
1 change: 1 addition & 0 deletions src/assetbundles/oembed/OembedAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function init()
];

$this->css = [
'css/oembed.css',
];

parent::init();
Expand Down
15 changes: 15 additions & 0 deletions src/assetbundles/oembed/dist/css/oembed.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.oembed-header {
margin: 0.5rem 0;
background: #f3f7fc;
padding: 10px 15px;
cursor: pointer
}

.oembed-preview {
width: 100%;
overflow: hidden;
}

.oembed-preview iframe {
max-width: 100%;
}
13 changes: 13 additions & 0 deletions src/assetbundles/oembed/dist/js/oembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@

var oembedOnChangeTimeout = null;

$('body').on('click', '.oembed-header', function () {
var oembedPreview = $(this).parent().find('.oembed-preview');
var icon = $(this).parent().find('.oembed-header *[data-icon-after]');

oembedPreview.toggleClass('hidden');

if(oembedPreview.hasClass('hidden')) {
icon.attr('data-icon-after', 'expand')
} else {
icon.attr('data-icon-after', 'collapse')
}
});

$('body').on('keyup blur change', 'input.oembed-field', function () {
var that = $(this);

Expand Down
1 change: 1 addition & 0 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function actionPreview()
[
'url' => $url,
'options' => $options,
'settings' => Oembed::getInstance()->getSettings(),
]
);
} catch(\Exception $exception) {
Expand Down
21 changes: 21 additions & 0 deletions src/events/BrokenUrlEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace wrav\oembed\events;

use craft;
use craft\base\ElementInterface;
use craft\behaviors\DraftBehavior;
use yii\base\Event;

/**
* Broken Url event class.
*
* @since 1.2.6
*/
class BrokenUrlEvent extends Event
{
/**
* @var string The url which is broken
*/
public $url;

}
16 changes: 13 additions & 3 deletions src/fields/OembedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use craft\helpers\Gql as GqlHelper;
use GraphQL\Type\Definition\Type;
use wrav\oembed\gql\OembedFieldTypeGenerator;
use wrav\oembed\Oembed;
use yii\db\Schema;
use wrav\oembed\models\OembedModel;

Expand Down Expand Up @@ -146,18 +147,27 @@ public function getSettingsHtml()
*/
public function getInputHtml($value, ElementInterface $element = null): string
{
$settings = Oembed::getInstance()->getSettings();
$hidden = $settings['previewHidden'];
$previewIcon = $hidden ? 'expand' : 'collapse';


$input = '<input name="'.$this->handle.'" class="text nicetext fullwidth oembed-field" value="'.$value.'" />';
$preview = '<p><strong>Preview</strong></p>';
$preview = '<div class="oembed-header">
<p class="fullwidth"><strong>Preview</strong> <span class="right" data-icon-after="'.$previewIcon.'"></span></p>
</div>';

if ($value) {
try {
if ($embed = new OembedModel($value)) {
$embed = $embed->embed();

$hiddenClass = $hidden ? 'hidden' : '';

if (!empty($embed)) {
$preview .= '<div class="oembed-preview">'.$embed->code.'</div>';
$preview .= '<div class="oembed-preview '.$hiddenClass.'">'.$embed->code.'</div>';
} else {
$preview .= '<div class="oembed-preview"><p class="error">Please check your URL.</p></div>';
$preview .= '<div class="oembed-preview '.$hiddenClass.'"><p class="error">Please check your URL.</p></div>';
}
}
} catch (\Exception $exception) {
Expand Down
52 changes: 52 additions & 0 deletions src/jobs/BrokenUrlNotify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace wrav\oembed\jobs;

use craft;
use craft\base\ElementInterface;
use craft\behaviors\DraftBehavior;
use craft\queue\BaseJob;
use wrav\oembed\Oembed;

/**
* BrokenUrlNotify job class.
*
* @since 1.2.6
*/
class BrokenUrlNotify extends BaseJob
{
public $url = null;

/**
* @inheritdoc
*/
public function execute($queue)
{
$email = Oembed::getInstance()->getSettings()->notificationEmail;
$subject = Craft::$app->getSystemName() . ' :: oEmbed detected broken URL';

if (!$email) {
$email = \craft\helpers\App::mailSettings()->fromEmail;
}

if (!$email || !$this->url) {
return false;
}

return Craft::$app
->getMailer()
->compose()
->setTo($email)
->setSubject($subject)
->setHtmlBody('The following URL is invalid: '.$this->url)
->send();
}

/**
* @inheritdoc
*/
protected function defaultDescription(): string
{
return Craft::t('app', 'Send notification of broken URL');
}

}
15 changes: 15 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ class Settings extends Model
*/
public $enableCache;

/**
* @var string
*/
public $enableNotifications;

/**
* @var string
*/
public $notificationEmail;

/**
* @var string
*/
public $previewHidden;

}
22 changes: 15 additions & 7 deletions src/services/OembedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use DOMDocument;
use Embed\Adapters\Adapter;
use Embed\Embed;
use wrav\oembed\events\BrokenUrlEvent;
use wrav\oembed\Oembed;
use yii\log\Logger;

Expand Down Expand Up @@ -46,14 +47,18 @@ public function embed($url, array $options = [])
$media = Embed::create($url, $options);

if (!empty($media) && !isset($media->code)) {
if (Oembed::getInstance()->getSettings()->enableNotifications) {
if (!empty($media->getUrl())) {
Oembed::getInstance()->trigger(Oembed::EVENT_BROKEN_URL_DETECTED, new BrokenUrlEvent([
'url' => $media->getUrl()
]));
}
}

$media->code = "<iframe src='$url' width='100%' frameborder='0' scrolling='no'></iframe>";
}
} finally {
if (!empty($media)) {
if (Oembed::getInstance()->getSettings()->enableCache) {
Craft::$app->cache->set($url, $media, 'P1H');
}
} else {
if (empty($media)) {
$media = new class {
// Returns NULL for calls to props
public function __call(string $name , array $arguments )
Expand All @@ -63,7 +68,6 @@ public function __call(string $name , array $arguments )
};
}


// Wrapping to be safe :)
try {
$html = $media->code;
Expand All @@ -88,7 +92,7 @@ public function __call(string $name , array $arguments )
if (!empty($options['width']) && is_int($options['width'])) {
$iframe->setAttribute('width', $options['width']);
}

// Height - Override
if (!empty($options['height']) && is_int($options['height'])) {
$iframe->setAttribute('height', $options['height']);
Expand All @@ -115,6 +119,10 @@ public function __call(string $name , array $arguments )
Craft::info($exception->getMessage(), 'oembed');
}
finally {
if (Oembed::getInstance()->getSettings()->enableCache) {
Craft::$app->cache->set($url, $media, 'P1H');
}

return $media;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/templates/preview.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{% set media = craft.oembed.embed(url) %}

{% if media and media.code|default(false) %}
<div class="embed-preview">
{{ media.code|default('')|raw }}
</div>
{{ media.code|default('')|raw }}
{% else %}
<p class="error">Please check your URL.</p>
{% endif %}
24 changes: 24 additions & 0 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,27 @@
name: 'enableCache',
on: settings.enableCache,
}) }}

{{ forms.lightswitchField({
label: "Previews hidden by default"|t,
instructions: "This will hide preview in collapsable component"|t,
id: 'previewHidden',
name: 'previewHidden',
on: settings.previewHidden,
}) }}

{{ forms.lightswitchField({
label: "Enable notifications"|t,
instructions: ""|t,
id: 'enableNotifications',
name: 'enableNotifications',
on: settings.enableNotifications,
}) }}

{{ forms.textField({
label: "Notification Email"|t,
instructions: "Will default to system email sender"|t,
id: 'notificationEmail',
name: 'notificationEmail',
on: settings.notificationEmail,
}) }}

0 comments on commit 47ddc7c

Please sign in to comment.