Skip to content

Commit

Permalink
RFC silverstripe#5487 Remove Oembed in favor of embed/embed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Firesphere committed May 21, 2016
1 parent 2467553 commit 1e549fe
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 33 deletions.
1 change: 0 additions & 1 deletion _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

ShortcodeParser::get('default')
->register('file_link', array('File', 'handle_shortcode'))
->register('embed', array('Oembed', 'handle_shortcode'))
->register('image', array('Image', 'handle_shortcode'));

// Shortcode parser which only regenerates shortcodes
Expand Down
2 changes: 1 addition & 1 deletion client/src/legacy/HtmlEditorField.js
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ $.entwine('ss', function($) {


/**
* Insert an oembed object tag into the content.
* Insert an Embed object tag into the content.
* Requires the 'media' plugin for serialization of tags into <img> placeholders.
*/
$('form.htmleditorfield-mediaform .ss-htmleditorfield-file.embed').entwine({
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"composer/installers": "~1.0",
"monolog/monolog": "~1.11",
"league/flysystem": "~1.0.12",
"symfony/yaml": "~2.7"
},
"symfony/yaml": "~2.7",
"embed/embed": "^2.6"
},
"require-dev": {
"phpunit/PHPUnit": "~4.8"
},
Expand Down
23 changes: 23 additions & 0 deletions docs/en/04_Changelogs/4.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ E.g.
'mysite/js/src/main.js',
'mysite/js/src/functions.js'
]]);

### RestfulService

* `RestfulService` has been removed. Use Guzzle instead. See Upgrading notes.

### Oembed

* `Oembed` has been removed from the framework in favor of [oscarotero/Embed](https://github.com/oscarotero/Embed)


## Upgrading
Expand Down Expand Up @@ -819,3 +827,18 @@ Will become:
}

Note that string references to SS_Datetime passed to injector, or used in config values, will still work, and will refer to the updated class names.

### Upgrading from deprecated RestfulService

Install Guzzle to get an API consuming library.
`composer require guzzlehttp/guzzle` or add `guzzlehttp/guzzle: "^6.0"` to your composer.json.

For information on how to use Guzzle, please see the extensive [Guzzle documentation](http://docs.guzzlephp.org/en/latest/)

In case you want to keep using RestfulService, you can use `Firesphere/silverstripe-restfulservice`, but it is unmaintained and deprecated.

### Upgrading from deprecated Oembed

Instead of Oembed, the framework now relies on [oscarotero/Embed](https://github.com/oscarotero/Embed) to handle getting the shortcode-data for embedding.

If you have custom embedding-code relying on Oembed, please refer to the documentation provided by oscarotero.
64 changes: 36 additions & 28 deletions forms/htmleditor/HTMLEditorField.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
use Embed\Adapters\AdapterInterface;
use Embed\Embed;

/**
* A TinyMCE-powered WYSIWYG HTML editor field with image and link insertion and tracking capabilities. Editor fields
* are created from <textarea> tags, which are then converted with JavaScript.
Expand Down Expand Up @@ -523,8 +526,8 @@ public function viewfile($request) {
));
}

// Instanciate file wrapper and get fields based on its type
// Check if appCategory is an image and exists on the local system, otherwise use oEmbed to refference a
// Instantiate file wrapper and get fields based on its type
// Check if appCategory is an image and exists on the local system, otherwise use Embed to reference a
// remote image
$fileCategory = $this->getFileCategory($url, $file);
switch($fileCategory) {
Expand All @@ -541,11 +544,11 @@ public function viewfile($request) {
if($file) {
throw $this->getErrorFor(_t(
"HTMLEditorField_Toolbar.ERROR_OEMBED_REMOTE",
"Oembed is only compatible with remote files"
"Embed is only compatible with remote files"
));
}

// Other files should fallback to oembed
// Other files should fallback to embed
$fileWrapper = new HTMLEditorField_Embed($url, $file);
break;
}
Expand Down Expand Up @@ -1031,9 +1034,9 @@ public function getInsertHeight() {
}

/**
* Encapsulation of an oembed tag, linking to an external media source.
* Encapsulation of an embed tag, linking to an external media source.
*
* @see Oembed
* @see Embed
* @package forms
* @subpackage fields-formattedinput
*/
Expand All @@ -1045,16 +1048,16 @@ class HTMLEditorField_Embed extends HTMLEditorField_File {
);

/**
* Oembed result
* Embed result
*
* @var Oembed_Result
* @var Embed
*/
protected $oembed;
protected $embed;

public function __construct($url, File $file = null) {
parent::__construct($url, $file);
$this->oembed = Oembed::get_oembed_from_url($url);
if(!$this->oembed) {
$this->embed = Embed::create($url);
if(!$this->embed) {
$controller = Controller::curr();
$response = $controller->getResponse();
$response->addHeader('X-Status',
Expand Down Expand Up @@ -1093,78 +1096,83 @@ public function getFields() {
}

/**
* Get width of this oembed
* Get width of this Embed
*
* @return int
*/
public function getWidth() {
return $this->oembed->Width ?: 100;
return $this->embed->width ?: 100;
}

/**
* Get height of this oembed
* Get height of this Embed
*
* @return int
*/
public function getHeight() {
return $this->oembed->Height ?: 100;
return $this->embed->height ?: 100;
}

public function getPreviewURL() {
// Use thumbnail url
if(!empty($this->oembed->thumbnail_url)) {
return $this->oembed->thumbnail_url;
if($this->embed->image) {
return $this->embed->image;
}

// Use direct image type
if($this->getType() == 'photo' && !empty($this->Oembed->url)) {
return $this->Oembed->url;
if($this->getType() == 'photo' && !empty($this->embed->url)) {
return $this->embed->url;
}

// Default media
return FRAMEWORK_DIR . '/images/default_media.png';
}

public function getName() {
if(isset($this->oembed->title)) {
return $this->oembed->title;
if($this->embed->title) {
return $this->embed->title;
} else {
return parent::getName();
}
}

/**
* Get OEmbed type
* Get Embed type
*
* @return string
*/
public function getType() {
return $this->oembed->type;
return $this->embed->type;
}

/**
* Get filetype
*
* @return string
*/
public function getFileType() {
return $this->getType()
?: parent::getFileType();
}

/**
* @return Oembed_Result
* @return AdapterInterface
*/
public function getOembed() {
return $this->oembed;
public function getEmbed() {
return $this->embed;
}

public function appCategory() {
return 'embed';
}

/**
* Info for this oembed
* Info for this Embed
*
* @return string
*/
public function getInfo() {
return $this->oembed->info;
return $this->embed->info;
}
}

Expand Down
56 changes: 55 additions & 1 deletion parsers/ShortcodeParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Embed\Embed;

/**
* A simple parser that allows you to map BBCode-like "shortcodes" to an arbitrary callback.
* It is a simple regex based parser that allows you to replace simple bbcode-like tags
Expand All @@ -21,7 +23,12 @@ public function img_shortcode($attrs) {

// --------------------------------------------------------------------------------------------------------------

protected $shortcodes = array();
protected $shortcodes = array(
'embed' => array(
'ShortcodeParser',
'handle_embed_shortcode'
)
);

// --------------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -612,4 +619,51 @@ function ($matches) use ($tags, $parser) {

return $content;
}

/**
* 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 $extra
*
* @return string
*/
public static function handle_embed_shortcode($arguments, $content, $parser, $shortcode, $extra = array()) {
$embed = Embed::create($content, $arguments);
if($embed && $embed instanceof \Embed\Adapters\Adapter) {
return $parser->embedForTemplate($embed);
} else {
return '<a href="' . $content . '">' . $content . '</a>';
}
}

/**
* @param Embed $embed
*
* @return string
*/
public function embedForTemplate($embed) {
switch($embed->type) {
case 'video':
case 'rich':
if($embed->extraClass) {
return "<div class='media $embed->extraClass'>$embed->code</div>";
} else {
return "<div class='media'>$embed->code</div>";
}
break;
case 'link':
return '<a class="' . $embed->extraClass . '" href="' . $embed->origin . '">' . $embed->title . '</a>';
break;
case 'photo':
return "<img src='$embed->url' width='$embed->width' height='$embed->height' class='$embed->extraClass' />";
break;
}
}


}

0 comments on commit 1e549fe

Please sign in to comment.