Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
- Changed preview controller action access to prevent anonymous access
- Refactored the preview action to use a template with wrapper to allow for future styling and updates
  • Loading branch information
reganlawton committed Nov 27, 2018
1 parent cc1ba7a commit 5cddc92
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# oEmbed Changelog

## 1.0.1 - 2018-11-26

### Updated
- Changed preview controller action access to prevent anonymous access
- Refactored the preview action to use a template with wrapper to allow for future styling and updates

## 1.0.0 - 2018-11-18

### Added
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.0.0",
"version": "1.0.1",
"keywords": [
"craft",
"cms",
Expand Down
9 changes: 9 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "1.0.1",
"downloadUrl": "https://github.com/wrav/oembed/archive/master.zip",
"date": "2018-11-26:00:00+10:30",
"notes": [
"[Updated] Change preview controller action access to prevent anonymous access",
"[Updated] Refactored the preview action to use a template with wrapper to allow for future styling and updates"
]
},
{
"version": "1.0.0",
"downloadUrl": "https://github.com/wrav/oembed/archive/master.zip",
Expand Down
12 changes: 9 additions & 3 deletions src/Oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use craft\events\RegisterComponentTypesEvent;
use craft\web\View;
use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;

use yii\base\Event;

Expand Down Expand Up @@ -116,6 +117,14 @@ function(Event $event) {
}
);

Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function(RegisterUrlRulesEvent $event) {
$event->rules['oembed/preview'] = 'oembed/default/preview';
}
);

Craft::info(
Craft::t(
'oembed',
Expand All @@ -126,7 +135,4 @@ function(Event $event) {
);
}

// Protected Methods
// =========================================================================

}
7 changes: 3 additions & 4 deletions src/assetbundles/oembed/dist/js/oembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

var oembedOnChangeTimeout = null;

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

if (oembedOnChangeTimeout != null) {
Expand All @@ -23,10 +23,11 @@ $('input.oembed-field').change(function () {
oembedOnChangeTimeout = null;

var val = that.val();

if(val) {
$.ajax({
type: "GET",
url: "/actions/oembed/default/preview?url=" + val + "&options[]=",
url: "/admin/oembed/preview?url=" + val + "&options[]=",
async: true
}).done(function (res) {
var preview = that.parent().find('.oembed-preview');
Expand All @@ -44,5 +45,3 @@ $('input.oembed-field').change(function () {

}, 500);
});

// https://www.youtube.com/watch?v=mJB83EZtAjc
22 changes: 11 additions & 11 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ class DefaultController extends Controller
// Protected Properties
// =========================================================================

protected $allowAnonymous = true;
protected $allowAnonymous = false;

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

public function actionPreview()
{
$data = Craft::$app->request->getQueryParams();
$url = Craft::$app->request->getRequiredParam('url');
$options = Craft::$app->request->getParam('options') ?? [];

try {
if (isset($data['url'])) {
echo Oembed::getInstance()->oembedService->render($data['url'], []);
}
return $this->renderTemplate(
'oembed/preview',
[
'url' => $url,
'options' => $options,
]
);
} catch(\Exception $exception) {
if (getenv('ENVIRONMENT') === 'dev') {
throw new $exception;
}
} finally {
Craft::$app->end();
throw new $exception;
}

}
}
9 changes: 9 additions & 0 deletions src/templates/preview.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% set media = craft.oembed.embed(url) %}

{% if media %}
<div class="embed-preview">
{{ media.html|raw }}
</div>
{% else %}
<p class="error">Please check your URL.</p>
{% endif %}
14 changes: 14 additions & 0 deletions src/variables/OembedVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,18 @@ public function render($url, array $options = [])
{
return Oembed::getInstance()->oembedService->render($url, $options);
}

/**
* Call it like this:
*
* {{ craft.oembed.embed(url, options) }}
*
* @param $url
* @param array $options
* @return string
*/
public function embed($url, array $options = [])
{
return Oembed::getInstance()->oembedService->embed($url, $options);
}
}

0 comments on commit 5cddc92

Please sign in to comment.