mirror of
https://github.com/humhub/humhub.git
synced 2025-02-24 19:23:21 +01:00
Advanced OEmbed providers functionality has been refactored.
This commit is contained in:
parent
bfc151c1f1
commit
b2bd3d8c02
@ -18,15 +18,6 @@ use Yii;
|
||||
*/
|
||||
class OembedController extends Controller
|
||||
{
|
||||
const OEMBED_INIT_EVENT = 'oembed_init';
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
$this->trigger(self::OEMBED_INIT_EVENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
@ -2,21 +2,31 @@ Oembed providers
|
||||
======
|
||||
|
||||
HumHub provides an option for adding of additional OEmbed providers or override existing one.
|
||||
Advanced OEmbed providers can be configured, by changing application parameters within your common.php:
|
||||
Advanced OEmbed providers can be configured, by setting up event listener in your module config.php:
|
||||
|
||||
### Catching an Event
|
||||
|
||||
Example event section of the config.php file:
|
||||
|
||||
```php
|
||||
'modules' => [
|
||||
'user' => [
|
||||
'advancedOembedProviders' => [
|
||||
'providers' => [
|
||||
'twitter.com' => 'https://publish.twitter.com/oembed?url=%url%&format=json'
|
||||
],
|
||||
'override' => false
|
||||
]
|
||||
]
|
||||
]
|
||||
'events' => [
|
||||
[
|
||||
'class' => \humhub\models\UrlOembed::class,
|
||||
'event' => \humhub\models\UrlOembed::FETCH,
|
||||
'callback' => [Events::class, 'onFetchOembed'],
|
||||
],
|
||||
]
|
||||
```
|
||||
|
||||
In case if override = false your providers will be added to existing OEmbed providers.
|
||||
### Processing
|
||||
|
||||
Also you can configure additional providers by setting up event listener in your module.
|
||||
Example of event callback:
|
||||
|
||||
```php
|
||||
public static function onFetchOembed($event)
|
||||
{
|
||||
$event->setProviders([
|
||||
'twitter.com' => 'https://publish.twitter.com/oembed?url=%url%&format=json'
|
||||
]);
|
||||
}
|
||||
```
|
44
protected/humhub/events/OembedFetchEvent.php
Normal file
44
protected/humhub/events/OembedFetchEvent.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\events;
|
||||
|
||||
use humhub\models\UrlOembed;
|
||||
use yii\base\Event;
|
||||
|
||||
class OembedFetchEvent extends Event
|
||||
{
|
||||
public $url;
|
||||
private $providers;
|
||||
private $result = null;
|
||||
|
||||
public function getResult()
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
public function setProviders($providers)
|
||||
{
|
||||
$this->providers = $providers;
|
||||
$this->setResult();
|
||||
}
|
||||
|
||||
public function setResult()
|
||||
{
|
||||
$urlOembed = UrlOembed::findOne(['url' => $this->url]);
|
||||
if ($urlOembed !== null) {
|
||||
$this->result = trim(preg_replace('/\s+/', ' ', $urlOembed->preview));
|
||||
} else {
|
||||
$this->result = trim(preg_replace('/\s+/', ' ', UrlOembed::loadUrl($this->url, $this->getProviderUrl())));
|
||||
}
|
||||
}
|
||||
|
||||
private function getProviderUrl()
|
||||
{
|
||||
foreach ($this->providers as $url => $endpoint) {
|
||||
if (strpos($this->url, $url) !== false) {
|
||||
return str_replace("%url%", urlencode($this->url), $endpoint);
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@
|
||||
|
||||
namespace humhub\models;
|
||||
|
||||
use humhub\events\OembedFetchEvent;
|
||||
use yii\base\InvalidArgumentException;
|
||||
use yii\helpers\Html;
|
||||
use yii\helpers\Json;
|
||||
@ -22,6 +23,7 @@ use Yii;
|
||||
*/
|
||||
class UrlOembed extends ActiveRecord
|
||||
{
|
||||
const FETCH = 'fetch';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
@ -64,6 +66,12 @@ class UrlOembed extends ActiveRecord
|
||||
*/
|
||||
public static function GetOEmbed($url)
|
||||
{
|
||||
$oembedFetchEvent = new OembedFetchEvent(['url' => $url]);
|
||||
(new UrlOembed())->trigger(static::FETCH, $oembedFetchEvent);
|
||||
if ($result = $oembedFetchEvent->getResult()) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$url = trim($url);
|
||||
|
||||
// Check if the given URL has OEmbed Support
|
||||
@ -105,17 +113,18 @@ class UrlOembed extends ActiveRecord
|
||||
*
|
||||
* @param string $url
|
||||
*
|
||||
* @param string $customProviderUrl
|
||||
* @return string
|
||||
*/
|
||||
public static function loadUrl($url)
|
||||
public static function loadUrl($url, $customProviderUrl = '')
|
||||
{
|
||||
$urlOembed = new UrlOembed();
|
||||
$urlOembed->url = $url;
|
||||
$html = '';
|
||||
|
||||
if ($urlOembed->getProviderUrl() != '') {
|
||||
$providerUrl = $customProviderUrl != '' ? $customProviderUrl : $urlOembed->getProviderUrl();
|
||||
if ($providerUrl != '') {
|
||||
// Build OEmbed Preview
|
||||
$jsonOut = UrlOembed::fetchUrl($urlOembed->getProviderUrl());
|
||||
$jsonOut = UrlOembed::fetchUrl($providerUrl);
|
||||
if ($jsonOut != '' && $jsonOut != 'Unauthorized') {
|
||||
try {
|
||||
$data = Json::decode($jsonOut);
|
||||
@ -213,8 +222,12 @@ class UrlOembed extends ActiveRecord
|
||||
*/
|
||||
public static function getProviders()
|
||||
{
|
||||
$userModule = Yii::$app->getModule('user');
|
||||
return $userModule->getOembedProviders();
|
||||
$providers = Yii::$app->settings->get('oembedProviders');
|
||||
if ($providers != '') {
|
||||
return Json::decode($providers);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -96,36 +96,6 @@ class Module extends \humhub\components\Module
|
||||
*/
|
||||
public $softDeleteKeepProfileFields = ['firstname', 'lastname'];
|
||||
|
||||
public $advancedOembedProviders = [];
|
||||
|
||||
public function getOembedProviders()
|
||||
{
|
||||
$providers = Yii::$app->settings->get('oembedProviders');
|
||||
$providers = ($providers != '') ? Json::decode($providers) : [];
|
||||
if ($this->validAdvancedOembedProviders()) {
|
||||
if ($this->advancedOembedProviders['override']) {
|
||||
$providers = $this->advancedOembedProviders['providers'];
|
||||
} else {
|
||||
foreach ($this->advancedOembedProviders['providers'] as $url => $endpoint) {
|
||||
$providers[$url] = $endpoint;
|
||||
}
|
||||
}
|
||||
return $providers;
|
||||
} else {
|
||||
return $providers;
|
||||
}
|
||||
}
|
||||
|
||||
private function validAdvancedOembedProviders()
|
||||
{
|
||||
if (! empty($this->advancedOembedProviders) &&
|
||||
isset($this->advancedOembedProviders['providers']) &&
|
||||
isset($this->advancedOembedProviders['override'])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user