mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API Update embed/embed to 3.0
API Better shortcode generation for embed shortcodes
This commit is contained in:
parent
9be22701fd
commit
e61257c27b
@ -18,7 +18,7 @@
|
||||
"require": {
|
||||
"php": ">=5.6.0",
|
||||
"composer/installers": "~1.0",
|
||||
"embed/embed": "^2.6",
|
||||
"embed/embed": "^3.0",
|
||||
"league/flysystem": "~1.0.12",
|
||||
"monolog/monolog": "~1.11",
|
||||
"nikic/php-parser": "^2 || ^3",
|
||||
|
@ -2,9 +2,13 @@
|
||||
|
||||
namespace SilverStripe\Forms\HtmlEditor;
|
||||
|
||||
use SilverStripe\Core\Convert;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Forms\FormField;
|
||||
use SilverStripe\View\Parsers\ShortcodeHandler;
|
||||
use Embed\Adapters\Adapter;
|
||||
use Embed\Embed;
|
||||
use SilverStripe\View\Parsers\ShortcodeParser;
|
||||
|
||||
/**
|
||||
* Provider for the [embed] shortcode tag used by the embedding service
|
||||
@ -28,47 +32,139 @@ class EmbedShortcodeProvider implements ShortcodeHandler
|
||||
* Embed shortcode parser from Oembed. This is a temporary workaround.
|
||||
* Oembed class has been replaced with the Embed external service.
|
||||
*
|
||||
* @param $arguments
|
||||
* @param $content
|
||||
* @param $parser
|
||||
* @param $shortcode
|
||||
* @param array $arguments
|
||||
* @param string $content
|
||||
* @param ShortcodeParser $parser
|
||||
* @param string $shortcode
|
||||
* @param array $extra
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
|
||||
{
|
||||
$embed = Embed::create($content, $arguments);
|
||||
if ($embed && $embed instanceof Adapter) {
|
||||
return self::embedForTemplate($embed);
|
||||
// Get service URL
|
||||
if (!empty($content)) {
|
||||
$serviceURL = $content;
|
||||
} elseif (!empty($arguments['url'])) {
|
||||
$serviceURL = $arguments['url'];
|
||||
} else {
|
||||
return '<a href="' . $content . '">' . $content . '</a>';
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to link to service
|
||||
return static::linkEmbed($arguments, $serviceURL, $serviceURL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Adapter $embed
|
||||
*
|
||||
* @param array $arguments Additional shortcode params
|
||||
* @return string
|
||||
*/
|
||||
public static function embedForTemplate($embed)
|
||||
public static function embedForTemplate($embed, $arguments)
|
||||
{
|
||||
switch ($embed->type) {
|
||||
switch ($embed->getType()) {
|
||||
case 'video':
|
||||
case 'rich':
|
||||
if ($embed->extraClass) {
|
||||
return "<div class='media $embed->extraClass'>$embed->code</div>";
|
||||
} else {
|
||||
return "<div class='media'>$embed->code</div>";
|
||||
// Attempt to inherit width (but leave height auto)
|
||||
if (empty($arguments['width']) && $embed->getWidth()) {
|
||||
$arguments['width'] = $embed->getWidth();
|
||||
}
|
||||
break;
|
||||
return self::videoEmbed($arguments, $embed->getCode());
|
||||
case 'link':
|
||||
return '<a class="' . $embed->extraClass . '" href="' . $embed->origin . '">' . $embed->title . '</a>';
|
||||
break;
|
||||
return self::linkEmbed($arguments, $embed->getUrl(), $embed->getTitle());
|
||||
case 'photo':
|
||||
return "<img src='$embed->url' width='$embed->width' height='$embed->height' class='$embed->extraClass' />";
|
||||
break;
|
||||
return self::photoEmbed($arguments, $embed->getUrl());
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
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;';
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace SilverStripe\Forms\HTMLEditor;
|
||||
|
||||
use Embed\Adapters\Adapter;
|
||||
use SilverStripe\Assets\File;
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Control\HTTPResponse_Exception;
|
||||
@ -26,7 +27,7 @@ class HTMLEditorField_Embed extends HTMLEditorField_File
|
||||
/**
|
||||
* Embed result
|
||||
*
|
||||
* @var Embed
|
||||
* @var Adapter
|
||||
*/
|
||||
protected $embed;
|
||||
|
||||
@ -60,7 +61,7 @@ class HTMLEditorField_Embed extends HTMLEditorField_File
|
||||
public function getFields()
|
||||
{
|
||||
$fields = parent::getFields();
|
||||
if ($this->Type === 'photo') {
|
||||
if ($this->getType() === 'photo') {
|
||||
$fields->insertBefore('CaptionText', new TextField(
|
||||
'AltText',
|
||||
_t('HTMLEditorField.IMAGEALTTEXT', 'Alternative text (alt) - shown if image can\'t be displayed'),
|
||||
@ -82,7 +83,7 @@ class HTMLEditorField_Embed extends HTMLEditorField_File
|
||||
*/
|
||||
public function getWidth()
|
||||
{
|
||||
return $this->embed->width ?: 100;
|
||||
return $this->embed->getWidth() ?: 100;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,7 +93,7 @@ class HTMLEditorField_Embed extends HTMLEditorField_File
|
||||
*/
|
||||
public function getHeight()
|
||||
{
|
||||
return $this->embed->height ?: 100;
|
||||
return $this->embed->getHeight() ?: 100;
|
||||
}
|
||||
|
||||
public function getPreviewURL()
|
||||
|
@ -2,10 +2,9 @@
|
||||
|
||||
namespace SilverStripe\Forms\Tests;
|
||||
|
||||
use Embed\Adapters\Webpage;
|
||||
use Embed\Embed;
|
||||
use SilverStripe\Forms\HtmlEditor\EmbedShortcodeProvider;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\framework\tests\php\Forms\EmbedShortcodeProviderTest\MockResolver;
|
||||
|
||||
/**
|
||||
* Class EmbedShortcodeProviderTest
|
||||
@ -29,27 +28,91 @@ class EmbedShortcodeProviderTest extends SapphireTest
|
||||
|
||||
public function testYoutube()
|
||||
{
|
||||
/** @var Webpage $result */
|
||||
$result = Embed::create(self::$test_youtube, array());
|
||||
self::assertEquals($result->providerName, 'YouTube');
|
||||
$embedded = EmbedShortcodeProvider::embedForTemplate($result);
|
||||
self::assertContains("<div class='media'", $embedded);
|
||||
self::assertContains('iframe', $embedded);
|
||||
self::assertContains('youtube.com', $embedded);
|
||||
self::assertContains('embed', $embedded);
|
||||
self::assertContains('dM15HfUYwF0', $embedded);
|
||||
/** @var string $result */
|
||||
$result = $this->mockRequest(
|
||||
[
|
||||
'url' => static::$test_youtube,
|
||||
'caption' => 'A nice video',
|
||||
'width' => 480,
|
||||
'height' => 360,
|
||||
],
|
||||
[
|
||||
'version' => '1.0',
|
||||
'provider_url' => 'https://www.youtube.com/',
|
||||
'title' => 'SilverStripe Platform 2 min introduction',
|
||||
'html' => '<iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>',
|
||||
'provider_name' => 'YouTube',
|
||||
'thumbnail_width' => 480,
|
||||
'type' => 'video',
|
||||
'thumbnail_url' => 'https://i.ytimg.com/vi/dM15HfUYwF0/hqdefault.jpg',
|
||||
'thumbnail_height' => 360,
|
||||
'width' => 480,
|
||||
'author_url' => 'https://www.youtube.com/user/SilverStripe',
|
||||
'author_name' => 'SilverStripe',
|
||||
'height' => 270,
|
||||
]
|
||||
);
|
||||
$this->assertEquals(
|
||||
<<<EOS
|
||||
<div style="width: 480px;"><iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>
|
||||
<p class="caption">A nice video</p></div>
|
||||
EOS
|
||||
,
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
public function testSoundcloud()
|
||||
{
|
||||
/** @var Webpage $result */
|
||||
$result = Embed::create(self::$test_soundcloud, array());
|
||||
self::assertEquals($result->providerName, 'SoundCloud');
|
||||
$embedded = EmbedShortcodeProvider::embedForTemplate($result);
|
||||
self::assertContains("<div class='media'", $embedded);
|
||||
self::assertContains('iframe', $embedded);
|
||||
self::assertContains('soundcloud.com', $embedded);
|
||||
self::assertContains('player', $embedded);
|
||||
self::assertContains('tracks%2F242518079', $embedded);
|
||||
/** @var string $result */
|
||||
$result = $this->mockRequest(
|
||||
['url' => static::$test_soundcloud],
|
||||
[
|
||||
'version' => 1,
|
||||
'type' => 'rich',
|
||||
'provider_name' => 'SoundCloud',
|
||||
'provider_url' => 'http://soundcloud.com',
|
||||
'height' => 400,
|
||||
'width' => '100%',
|
||||
'title' => 'DELAIN - Suckerpunch by Napalm Records',
|
||||
'description' => 'Taken from the EP "Lunar Prelude": http://shop.napalmrecords.com/delain',
|
||||
'thumbnail_url' => 'http://i1.sndcdn.com/artworks-000143578557-af0v6l-t500x500.jpg',
|
||||
'html' => '<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F242518079&show_artwork=true"></iframe>',
|
||||
'author_name' => 'Napalm Records',
|
||||
'author_url' => 'http://soundcloud.com/napalmrecords',
|
||||
]
|
||||
);
|
||||
$this->assertEquals(
|
||||
<<<EOS
|
||||
<div style="width: 100px;"><iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F242518079&show_artwork=true"></iframe></div>
|
||||
EOS
|
||||
,
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock an oembed request
|
||||
*
|
||||
* @param array $arguments Input arguments
|
||||
* @param array $response JSON response body
|
||||
* @return string
|
||||
*/
|
||||
protected function mockRequest($arguments, $response)
|
||||
{
|
||||
return EmbedShortcodeProvider::handle_shortcode(
|
||||
$arguments,
|
||||
'',
|
||||
null,
|
||||
'embed',
|
||||
[
|
||||
'resolver' => [
|
||||
'class' => MockResolver::class,
|
||||
'config' => [
|
||||
'expectedContent' => json_encode($response),
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
63
tests/php/Forms/EmbedShortcodeProviderTest/MockResolver.php
Normal file
63
tests/php/Forms/EmbedShortcodeProviderTest/MockResolver.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\framework\tests\php\Forms\EmbedShortcodeProviderTest;
|
||||
|
||||
use Embed\Http\DispatcherInterface;
|
||||
use Embed\Http\ImageResponse;
|
||||
use Embed\Http\Response;
|
||||
use Embed\Http\Url;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class MockResolver implements DispatcherInterface
|
||||
{
|
||||
protected $url = null;
|
||||
|
||||
protected $expectedContent = null;
|
||||
|
||||
/**
|
||||
* Constructor. Sets the url.
|
||||
*
|
||||
* @param string $url The url value
|
||||
* @param array $config The resolver configuration
|
||||
*/
|
||||
public function __construct($url, array $config)
|
||||
{
|
||||
$this->url = $url;
|
||||
if (empty($config['expectedContent'])) {
|
||||
throw new InvalidArgumentException("Mock resolvers need expectedContent");
|
||||
}
|
||||
$this->expectedContent = $config['expectedContent'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch an url.
|
||||
*
|
||||
* @param Url $url
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function dispatch(Url $url)
|
||||
{
|
||||
return new Response(
|
||||
$url,
|
||||
$url,
|
||||
200,
|
||||
'application/json',
|
||||
$this->expectedContent,
|
||||
[],
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve multiple image urls at once.
|
||||
*
|
||||
* @param Url[] $urls
|
||||
*
|
||||
* @return ImageResponse[]
|
||||
*/
|
||||
public function dispatchImages(array $urls)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user