2016-05-27 03:09:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\HtmlEditor;
|
|
|
|
|
2017-04-03 06:30:01 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Forms\FormField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Parsers\ShortcodeHandler;
|
2016-05-27 03:09:03 +02:00
|
|
|
use Embed\Adapters\Adapter;
|
|
|
|
use Embed\Embed;
|
2017-04-03 06:30:01 +02:00
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser;
|
2016-05-27 03:09:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provider for the [embed] shortcode tag used by the embedding service
|
|
|
|
* in the HTML Editor field.
|
|
|
|
* Provides the html needed for the frontend and the editor field itself.
|
|
|
|
*/
|
|
|
|
class EmbedShortcodeProvider implements ShortcodeHandler
|
|
|
|
{
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Gets the list of shortcodes provided by this handler
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function get_shortcodes()
|
|
|
|
{
|
|
|
|
return array('embed');
|
|
|
|
}
|
2016-05-27 03:09:03 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Embed shortcode parser from Oembed. This is a temporary workaround.
|
|
|
|
* Oembed class has been replaced with the Embed external service.
|
|
|
|
*
|
2017-04-03 06:30:01 +02:00
|
|
|
* @param array $arguments
|
|
|
|
* @param string $content
|
|
|
|
* @param ShortcodeParser $parser
|
|
|
|
* @param string $shortcode
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param array $extra
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
|
|
|
|
{
|
2017-04-03 06:30:01 +02:00
|
|
|
// Get service URL
|
|
|
|
if (!empty($content)) {
|
|
|
|
$serviceURL = $content;
|
|
|
|
} elseif (!empty($arguments['url'])) {
|
|
|
|
$serviceURL = $arguments['url'];
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
2017-04-03 06:30:01 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// See https://github.com/oscarotero/Embed#example-with-all-options for service arguments
|
|
|
|
$serviceArguments = [];
|
|
|
|
if (!empty($arguments['width'])) {
|
|
|
|
$serviceArguments['min_image_width'] = $arguments['width'];
|
|
|
|
}
|
|
|
|
if (!empty($arguments['height'])) {
|
|
|
|
$serviceArguments['min_image_height'] = $arguments['height'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow resolver to be mocked
|
|
|
|
$dispatcher = null;
|
|
|
|
if (isset($extra['resolver'])) {
|
|
|
|
$dispatcher = Injector::inst()->create(
|
|
|
|
$extra['resolver']['class'],
|
|
|
|
$serviceURL,
|
|
|
|
$extra['resolver']['config']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process embed
|
|
|
|
$embed = Embed::create($serviceURL, $serviceArguments, $dispatcher);
|
|
|
|
|
|
|
|
// Convert embed object into HTML
|
|
|
|
if ($embed && $embed instanceof Adapter) {
|
|
|
|
$result = static::embedForTemplate($embed, $arguments);
|
|
|
|
if ($result) {
|
|
|
|
return $result;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-04-03 06:30:01 +02:00
|
|
|
|
|
|
|
// Fallback to link to service
|
|
|
|
return static::linkEmbed($arguments, $serviceURL, $serviceURL);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2016-05-27 03:09:03 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param Adapter $embed
|
2017-04-03 06:30:01 +02:00
|
|
|
* @param array $arguments Additional shortcode params
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-04-03 06:30:01 +02:00
|
|
|
public static function embedForTemplate($embed, $arguments)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-04-03 06:30:01 +02:00
|
|
|
switch ($embed->getType()) {
|
2016-11-29 00:31:16 +01:00
|
|
|
case 'video':
|
|
|
|
case 'rich':
|
2017-04-03 06:30:01 +02:00
|
|
|
// Attempt to inherit width (but leave height auto)
|
|
|
|
if (empty($arguments['width']) && $embed->getWidth()) {
|
|
|
|
$arguments['width'] = $embed->getWidth();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-04-03 06:30:01 +02:00
|
|
|
return self::videoEmbed($arguments, $embed->getCode());
|
2016-11-29 00:31:16 +01:00
|
|
|
case 'link':
|
2017-04-03 06:30:01 +02:00
|
|
|
return self::linkEmbed($arguments, $embed->getUrl(), $embed->getTitle());
|
2016-11-29 00:31:16 +01:00
|
|
|
case 'photo':
|
2017-04-03 06:30:01 +02:00
|
|
|
return self::photoEmbed($arguments, $embed->getUrl());
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build video embed tag
|
|
|
|
*
|
|
|
|
* @param array $arguments
|
|
|
|
* @param string $content Raw HTML content
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function videoEmbed($arguments, $content)
|
|
|
|
{
|
|
|
|
// Ensure outer div has given width (but leave height auto)
|
|
|
|
if (!empty($arguments['width'])) {
|
|
|
|
$arguments['style'] = 'width: ' . intval($arguments['width']) . 'px;';
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-04-03 06:30:01 +02:00
|
|
|
|
|
|
|
// Convert caption to <p>
|
|
|
|
if (!empty($arguments['caption'])) {
|
|
|
|
$xmlCaption = Convert::raw2xml($arguments['caption']);
|
|
|
|
$content .= "\n<p class=\"caption\">{$xmlCaption}</p>";
|
|
|
|
}
|
|
|
|
unset($arguments['width']);
|
|
|
|
unset($arguments['height']);
|
|
|
|
unset($arguments['url']);
|
|
|
|
unset($arguments['caption']);
|
|
|
|
return FormField::create_tag('div', $arguments, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build <a> embed tag
|
|
|
|
*
|
|
|
|
* @param array $arguments
|
|
|
|
* @param string $href
|
|
|
|
* @param string $title Default title
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function linkEmbed($arguments, $href, $title)
|
|
|
|
{
|
|
|
|
$title = !empty($arguments['caption']) ? ($arguments['caption']) : $title;
|
|
|
|
unset($arguments['caption']);
|
|
|
|
unset($arguments['width']);
|
|
|
|
unset($arguments['height']);
|
|
|
|
unset($arguments['url']);
|
|
|
|
$arguments['href'] = $href;
|
|
|
|
return Formfield::create_tag('a', $arguments, Convert::raw2xml($title));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build img embed tag
|
|
|
|
*
|
|
|
|
* @param array $arguments
|
|
|
|
* @param string $src
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function photoEmbed($arguments, $src)
|
|
|
|
{
|
|
|
|
$arguments['src'] = $src;
|
|
|
|
unset($arguments['url']);
|
|
|
|
return FormField::create_tag('img', $arguments);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2016-05-27 03:09:03 +02:00
|
|
|
}
|