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": {
|
"require": {
|
||||||
"php": ">=5.6.0",
|
"php": ">=5.6.0",
|
||||||
"composer/installers": "~1.0",
|
"composer/installers": "~1.0",
|
||||||
"embed/embed": "^2.6",
|
"embed/embed": "^3.0",
|
||||||
"league/flysystem": "~1.0.12",
|
"league/flysystem": "~1.0.12",
|
||||||
"monolog/monolog": "~1.11",
|
"monolog/monolog": "~1.11",
|
||||||
"nikic/php-parser": "^2 || ^3",
|
"nikic/php-parser": "^2 || ^3",
|
||||||
|
@ -2,9 +2,13 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Forms\HtmlEditor;
|
namespace SilverStripe\Forms\HtmlEditor;
|
||||||
|
|
||||||
|
use SilverStripe\Core\Convert;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\Forms\FormField;
|
||||||
use SilverStripe\View\Parsers\ShortcodeHandler;
|
use SilverStripe\View\Parsers\ShortcodeHandler;
|
||||||
use Embed\Adapters\Adapter;
|
use Embed\Adapters\Adapter;
|
||||||
use Embed\Embed;
|
use Embed\Embed;
|
||||||
|
use SilverStripe\View\Parsers\ShortcodeParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provider for the [embed] shortcode tag used by the embedding service
|
* 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.
|
* Embed shortcode parser from Oembed. This is a temporary workaround.
|
||||||
* Oembed class has been replaced with the Embed external service.
|
* Oembed class has been replaced with the Embed external service.
|
||||||
*
|
*
|
||||||
* @param $arguments
|
* @param array $arguments
|
||||||
* @param $content
|
* @param string $content
|
||||||
* @param $parser
|
* @param ShortcodeParser $parser
|
||||||
* @param $shortcode
|
* @param string $shortcode
|
||||||
* @param array $extra
|
* @param array $extra
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
|
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
|
||||||
{
|
{
|
||||||
$embed = Embed::create($content, $arguments);
|
// Get service URL
|
||||||
if ($embed && $embed instanceof Adapter) {
|
if (!empty($content)) {
|
||||||
return self::embedForTemplate($embed);
|
$serviceURL = $content;
|
||||||
|
} elseif (!empty($arguments['url'])) {
|
||||||
|
$serviceURL = $arguments['url'];
|
||||||
} else {
|
} 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 Adapter $embed
|
||||||
*
|
* @param array $arguments Additional shortcode params
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function embedForTemplate($embed)
|
public static function embedForTemplate($embed, $arguments)
|
||||||
{
|
{
|
||||||
switch ($embed->type) {
|
switch ($embed->getType()) {
|
||||||
case 'video':
|
case 'video':
|
||||||
case 'rich':
|
case 'rich':
|
||||||
if ($embed->extraClass) {
|
// Attempt to inherit width (but leave height auto)
|
||||||
return "<div class='media $embed->extraClass'>$embed->code</div>";
|
if (empty($arguments['width']) && $embed->getWidth()) {
|
||||||
} else {
|
$arguments['width'] = $embed->getWidth();
|
||||||
return "<div class='media'>$embed->code</div>";
|
|
||||||
}
|
}
|
||||||
break;
|
return self::videoEmbed($arguments, $embed->getCode());
|
||||||
case 'link':
|
case 'link':
|
||||||
return '<a class="' . $embed->extraClass . '" href="' . $embed->origin . '">' . $embed->title . '</a>';
|
return self::linkEmbed($arguments, $embed->getUrl(), $embed->getTitle());
|
||||||
break;
|
|
||||||
case 'photo':
|
case 'photo':
|
||||||
return "<img src='$embed->url' width='$embed->width' height='$embed->height' class='$embed->extraClass' />";
|
return self::photoEmbed($arguments, $embed->getUrl());
|
||||||
break;
|
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;
|
namespace SilverStripe\Forms\HTMLEditor;
|
||||||
|
|
||||||
|
use Embed\Adapters\Adapter;
|
||||||
use SilverStripe\Assets\File;
|
use SilverStripe\Assets\File;
|
||||||
use SilverStripe\Control\Controller;
|
use SilverStripe\Control\Controller;
|
||||||
use SilverStripe\Control\HTTPResponse_Exception;
|
use SilverStripe\Control\HTTPResponse_Exception;
|
||||||
@ -26,7 +27,7 @@ class HTMLEditorField_Embed extends HTMLEditorField_File
|
|||||||
/**
|
/**
|
||||||
* Embed result
|
* Embed result
|
||||||
*
|
*
|
||||||
* @var Embed
|
* @var Adapter
|
||||||
*/
|
*/
|
||||||
protected $embed;
|
protected $embed;
|
||||||
|
|
||||||
@ -60,7 +61,7 @@ class HTMLEditorField_Embed extends HTMLEditorField_File
|
|||||||
public function getFields()
|
public function getFields()
|
||||||
{
|
{
|
||||||
$fields = parent::getFields();
|
$fields = parent::getFields();
|
||||||
if ($this->Type === 'photo') {
|
if ($this->getType() === 'photo') {
|
||||||
$fields->insertBefore('CaptionText', new TextField(
|
$fields->insertBefore('CaptionText', new TextField(
|
||||||
'AltText',
|
'AltText',
|
||||||
_t('HTMLEditorField.IMAGEALTTEXT', 'Alternative text (alt) - shown if image can\'t be displayed'),
|
_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()
|
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()
|
public function getHeight()
|
||||||
{
|
{
|
||||||
return $this->embed->height ?: 100;
|
return $this->embed->getHeight() ?: 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPreviewURL()
|
public function getPreviewURL()
|
||||||
|
@ -2,10 +2,9 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Forms\Tests;
|
namespace SilverStripe\Forms\Tests;
|
||||||
|
|
||||||
use Embed\Adapters\Webpage;
|
|
||||||
use Embed\Embed;
|
|
||||||
use SilverStripe\Forms\HtmlEditor\EmbedShortcodeProvider;
|
use SilverStripe\Forms\HtmlEditor\EmbedShortcodeProvider;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\framework\tests\php\Forms\EmbedShortcodeProviderTest\MockResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class EmbedShortcodeProviderTest
|
* Class EmbedShortcodeProviderTest
|
||||||
@ -29,27 +28,91 @@ class EmbedShortcodeProviderTest extends SapphireTest
|
|||||||
|
|
||||||
public function testYoutube()
|
public function testYoutube()
|
||||||
{
|
{
|
||||||
/** @var Webpage $result */
|
/** @var string $result */
|
||||||
$result = Embed::create(self::$test_youtube, array());
|
$result = $this->mockRequest(
|
||||||
self::assertEquals($result->providerName, 'YouTube');
|
[
|
||||||
$embedded = EmbedShortcodeProvider::embedForTemplate($result);
|
'url' => static::$test_youtube,
|
||||||
self::assertContains("<div class='media'", $embedded);
|
'caption' => 'A nice video',
|
||||||
self::assertContains('iframe', $embedded);
|
'width' => 480,
|
||||||
self::assertContains('youtube.com', $embedded);
|
'height' => 360,
|
||||||
self::assertContains('embed', $embedded);
|
],
|
||||||
self::assertContains('dM15HfUYwF0', $embedded);
|
[
|
||||||
|
'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()
|
public function testSoundcloud()
|
||||||
{
|
{
|
||||||
/** @var Webpage $result */
|
/** @var string $result */
|
||||||
$result = Embed::create(self::$test_soundcloud, array());
|
$result = $this->mockRequest(
|
||||||
self::assertEquals($result->providerName, 'SoundCloud');
|
['url' => static::$test_soundcloud],
|
||||||
$embedded = EmbedShortcodeProvider::embedForTemplate($result);
|
[
|
||||||
self::assertContains("<div class='media'", $embedded);
|
'version' => 1,
|
||||||
self::assertContains('iframe', $embedded);
|
'type' => 'rich',
|
||||||
self::assertContains('soundcloud.com', $embedded);
|
'provider_name' => 'SoundCloud',
|
||||||
self::assertContains('player', $embedded);
|
'provider_url' => 'http://soundcloud.com',
|
||||||
self::assertContains('tracks%2F242518079', $embedded);
|
'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