2016-05-27 03:09:03 +02:00
|
|
|
<?php
|
|
|
|
|
2017-06-29 08:45:17 +02:00
|
|
|
namespace SilverStripe\View\Shortcodes;
|
2016-05-27 03:09:03 +02:00
|
|
|
|
2022-03-23 22:27:15 +01:00
|
|
|
use Embed\Http\NetworkException;
|
|
|
|
use Embed\Http\RequestException;
|
2020-08-12 02:14:05 +02:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
2017-04-03 06:30:01 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2019-08-19 17:35:40 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
|
|
|
use SilverStripe\View\ArrayData;
|
2018-06-20 01:40:28 +02:00
|
|
|
use SilverStripe\View\Embed\Embeddable;
|
2017-04-18 00:34:18 +02:00
|
|
|
use SilverStripe\View\HTML;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Parsers\ShortcodeHandler;
|
2017-04-03 06:30:01 +02:00
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser;
|
2020-07-03 03:04:54 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2022-03-23 22:27:15 +01:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\View\Embed\EmbedContainer;
|
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()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
return ['embed'];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
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
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = [])
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
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 '';
|
|
|
|
}
|
|
|
|
|
2021-06-29 02:17:07 +02:00
|
|
|
$class = $arguments['class'] ?? '';
|
|
|
|
$width = $arguments['width'] ?? '';
|
|
|
|
$height = $arguments['height'] ?? '';
|
|
|
|
|
2020-08-12 02:14:05 +02:00
|
|
|
// Try to use cached result
|
|
|
|
$cache = static::getCache();
|
2021-06-29 02:17:07 +02:00
|
|
|
$key = static::deriveCacheKey($serviceURL, $class, $width, $height);
|
2020-08-12 02:14:05 +02:00
|
|
|
try {
|
|
|
|
if ($cache->has($key)) {
|
|
|
|
return $cache->get($key);
|
|
|
|
}
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:30:01 +02:00
|
|
|
// 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'];
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:27:15 +01:00
|
|
|
/** @var EmbedContainer $embeddable */
|
|
|
|
$embeddable = Injector::inst()->create(Embeddable::class, $serviceURL);
|
2018-06-20 01:40:28 +02:00
|
|
|
|
2022-03-23 22:27:15 +01:00
|
|
|
// Only EmbedContainer is currently supported
|
|
|
|
if (!($embeddable instanceof EmbedContainer)) {
|
|
|
|
throw new \RuntimeException('Emeddable must extend EmbedContainer');
|
2017-04-03 06:30:01 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 22:27:15 +01:00
|
|
|
if (!empty($serviceArguments)) {
|
|
|
|
$embeddable->setOptions(array_merge($serviceArguments, (array) $embeddable->getOptions()));
|
2017-04-03 06:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process embed
|
2020-07-03 03:04:54 +02:00
|
|
|
try {
|
2022-03-23 22:27:15 +01:00
|
|
|
// this will trigger a request/response which will then be cached within $embeddable
|
|
|
|
$embeddable->getExtractor();
|
|
|
|
} catch (NetworkException | RequestException $e) {
|
2020-07-03 03:04:54 +02:00
|
|
|
$message = (Director::isDev())
|
|
|
|
? $e->getMessage()
|
|
|
|
: _t(__CLASS__ . '.INVALID_URL', 'There was a problem loading the media.');
|
|
|
|
|
|
|
|
$attr = [
|
|
|
|
'class' => 'ss-media-exception embed'
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = HTML::createTag(
|
|
|
|
'div',
|
|
|
|
$attr,
|
|
|
|
HTML::createTag('p', [], $message)
|
|
|
|
);
|
|
|
|
return $result;
|
|
|
|
}
|
2017-04-03 06:30:01 +02:00
|
|
|
|
|
|
|
// Convert embed object into HTML
|
2022-03-23 22:27:15 +01:00
|
|
|
$html = static::embeddableToHtml($embeddable, $arguments);
|
2017-04-03 06:30:01 +02:00
|
|
|
// Fallback to link to service
|
2022-03-23 22:27:15 +01:00
|
|
|
if (!$html) {
|
2020-08-12 02:14:05 +02:00
|
|
|
$result = static::linkEmbed($arguments, $serviceURL, $serviceURL);
|
|
|
|
}
|
|
|
|
// Cache result
|
2022-03-23 22:27:15 +01:00
|
|
|
if ($html) {
|
2020-08-12 02:14:05 +02:00
|
|
|
try {
|
2022-03-23 22:27:15 +01:00
|
|
|
$cache->set($key, $html);
|
2020-08-12 02:14:05 +02:00
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
}
|
|
|
|
}
|
2022-03-23 22:27:15 +01:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function embeddableToHtml(Embeddable $embeddable, array $arguments): string
|
|
|
|
{
|
|
|
|
// Only EmbedContainer is supported
|
|
|
|
if (!($embeddable instanceof EmbedContainer)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$extractor = $embeddable->getExtractor();
|
|
|
|
$type = $embeddable->getType();
|
|
|
|
if ($type === 'video' || $type === 'rich') {
|
|
|
|
// Attempt to inherit width (but leave height auto)
|
|
|
|
if (empty($arguments['width']) && $embeddable->getWidth()) {
|
|
|
|
$arguments['width'] = $embeddable->getWidth();
|
|
|
|
}
|
|
|
|
return static::videoEmbed($arguments, $extractor->code->html);
|
|
|
|
}
|
|
|
|
if ($type === 'photo') {
|
|
|
|
return static::photoEmbed($arguments, (string) $extractor->url);
|
|
|
|
}
|
|
|
|
if ($type === 'link') {
|
|
|
|
return static::linkEmbed($arguments, (string) $extractor->url, $extractor->title);
|
|
|
|
}
|
|
|
|
return '';
|
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
|
2022-03-23 22:27:15 +01:00
|
|
|
* @deprecated 4.11..5.0 Use embeddableToHtml instead
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-04-03 06:30:01 +02:00
|
|
|
public static function embedForTemplate($embed, $arguments)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2022-03-23 22:27:15 +01:00
|
|
|
Deprecation::notice('4.11', 'Use embeddableToHtml() instead');
|
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
|
|
|
}
|
2019-01-28 15:51:34 +01:00
|
|
|
return static::videoEmbed($arguments, $embed->getCode());
|
2016-11-29 00:31:16 +01:00
|
|
|
case 'link':
|
2019-01-28 15:51:34 +01:00
|
|
|
return static::linkEmbed($arguments, $embed->getUrl(), $embed->getTitle());
|
2016-11-29 00:31:16 +01:00
|
|
|
case 'photo':
|
2019-01-28 15:51:34 +01:00
|
|
|
return static::photoEmbed($arguments, $embed->getUrl());
|
2017-04-03 06:30:01 +02:00
|
|
|
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
|
|
|
|
2020-07-27 08:04:03 +02:00
|
|
|
// override iframe dimension attributes provided by webservice with ones specified in shortcode arguments
|
|
|
|
foreach (['width', 'height'] as $attr) {
|
|
|
|
if (!($value = $arguments[$attr] ?? false)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach (['"', "'"] as $quote) {
|
|
|
|
$rx = "/(<iframe .*?)$attr=$quote([0-9]+)$quote([^>]+>)/";
|
2022-04-14 03:12:59 +02:00
|
|
|
$content = preg_replace($rx ?? '', "$1{$attr}={$quote}{$value}{$quote}$3", $content ?? '');
|
2020-07-27 08:04:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 17:35:40 +02:00
|
|
|
$data = [
|
|
|
|
'Arguments' => $arguments,
|
|
|
|
'Attributes' => static::buildAttributeListFromArguments($arguments, ['width', 'height', 'url', 'caption']),
|
|
|
|
'Content' => DBField::create_field('HTMLFragment', $content)
|
|
|
|
];
|
|
|
|
|
|
|
|
return ArrayData::create($data)->renderWith(self::class . '_video')->forTemplate();
|
2017-04-03 06:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build <a> embed tag
|
|
|
|
*
|
|
|
|
* @param array $arguments
|
|
|
|
* @param string $href
|
|
|
|
* @param string $title Default title
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function linkEmbed($arguments, $href, $title)
|
|
|
|
{
|
2019-08-19 17:35:40 +02:00
|
|
|
$data = [
|
|
|
|
'Arguments' => $arguments,
|
|
|
|
'Attributes' => static::buildAttributeListFromArguments($arguments, ['width', 'height', 'url', 'caption']),
|
|
|
|
'Href' => $href,
|
|
|
|
'Title' => !empty($arguments['caption']) ? ($arguments['caption']) : $title
|
|
|
|
];
|
|
|
|
|
|
|
|
return ArrayData::create($data)->renderWith(self::class . '_link')->forTemplate();
|
2017-04-03 06:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build img embed tag
|
|
|
|
*
|
|
|
|
* @param array $arguments
|
|
|
|
* @param string $src
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function photoEmbed($arguments, $src)
|
|
|
|
{
|
2019-08-19 17:35:40 +02:00
|
|
|
$data = [
|
|
|
|
'Arguments' => $arguments,
|
|
|
|
'Attributes' => static::buildAttributeListFromArguments($arguments, ['url']),
|
|
|
|
'Src' => $src
|
|
|
|
];
|
|
|
|
|
|
|
|
return ArrayData::create($data)->renderWith(self::class . '_photo')->forTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a list of HTML attributes from embed arguments - used to preserve backward compatibility
|
|
|
|
*
|
|
|
|
* @deprecated 4.5.0 Use {$Arguments.name} directly in shortcode templates to access argument values
|
|
|
|
* @param array $arguments List of embed arguments
|
|
|
|
* @param array $exclude List of attribute names to exclude from the resulting list
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
private static function buildAttributeListFromArguments(array $arguments, array $exclude = []): ArrayList
|
|
|
|
{
|
|
|
|
$attributes = ArrayList::create();
|
|
|
|
foreach ($arguments as $key => $value) {
|
2022-04-14 03:12:59 +02:00
|
|
|
if (in_array($key, $exclude ?? [])) {
|
2019-08-19 17:35:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attributes->push(ArrayData::create([
|
|
|
|
'Name' => $key,
|
|
|
|
'Value' => Convert::raw2att($value)
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attributes;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2020-08-12 02:14:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ShortcodeParser $parser
|
|
|
|
* @param string $content
|
|
|
|
*/
|
|
|
|
public static function flushCachedShortcodes(ShortcodeParser $parser, string $content): void
|
|
|
|
{
|
|
|
|
$cache = static::getCache();
|
|
|
|
$tags = $parser->extractTags($content);
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if (!isset($tag['open']) || $tag['open'] != 'embed') {
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-29 02:17:07 +02:00
|
|
|
$url = $tag['content'] ?? $tag['attrs']['url'] ?? '';
|
|
|
|
$class = $tag['attrs']['class'] ?? '';
|
|
|
|
$width = $tag['attrs']['width'] ?? '';
|
|
|
|
$height = $tag['attrs']['height'] ?? '';
|
2020-08-12 02:14:05 +02:00
|
|
|
if (!$url) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-29 02:17:07 +02:00
|
|
|
$key = static::deriveCacheKey($url, $class, $width, $height);
|
2020-08-12 02:14:05 +02:00
|
|
|
try {
|
|
|
|
if (!$cache->has($key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$cache->delete($key);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return CacheInterface
|
|
|
|
*/
|
|
|
|
private static function getCache(): CacheInterface
|
|
|
|
{
|
|
|
|
return Injector::inst()->get(CacheInterface::class . '.EmbedShortcodeProvider');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-06-29 02:17:07 +02:00
|
|
|
private static function deriveCacheKey(string $url, string $class, string $width, string $height): string
|
|
|
|
{
|
|
|
|
return implode('-', array_filter([
|
|
|
|
'embed-shortcode',
|
|
|
|
self::cleanKeySegment($url),
|
|
|
|
self::cleanKeySegment($class),
|
|
|
|
self::cleanKeySegment($width),
|
|
|
|
self::cleanKeySegment($height)
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $str
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function cleanKeySegment(string $str): string
|
2020-08-12 02:14:05 +02:00
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return preg_replace('/[^a-zA-Z0-9\-]/', '', $str ?? '');
|
2020-08-12 02:14:05 +02:00
|
|
|
}
|
2016-05-27 03:09:03 +02:00
|
|
|
}
|