Skip to content

Commit d9e75a0

Browse files
committed
Improvements in Sources and added Embedly support
1 parent 373fd7c commit d9e75a0

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

Embed/Adapters/Adapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ abstract class Adapter {
1717
'getBiggerImage' => false,
1818
'getBiggerIcon' => false,
1919
'facebookAccessToken' => null,
20-
'soundcloudClientId' => null
20+
'soundcloudClientId' => null,
21+
'embedlyKey' => null
2122
);
2223

2324
abstract protected function initProviders (Url $Url);

Embed/Adapters/Webpage.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Embed\Providers\TwitterCards;
1515
use Embed\Providers\Dcterms;
1616
use Embed\Providers\Facebook;
17+
use Embed\Providers\Embedly;
1718

1819
class Webpage extends Adapter implements AdapterInterface {
1920
static public function check (Url $Url) {
@@ -34,6 +35,8 @@ protected function initProviders (Url $Url) {
3435
$this->providers['OEmbed'] = new OEmbed(new Url($Url->getAbsolute($this->providers['Html']->get('oembed'))));
3536
} else if (($OEmbed = OEmbedImplementations::create($Url))) {
3637
$this->providers['OEmbed'] = $OEmbed;
38+
} else if ($this->options['embedlyKey'] && ($OEmbed = Embedly::create($Url, $this->options['embedlyKey']))) {
39+
$this->providers['OEmbed'] = $OEmbed;
3740
}
3841

3942
$this->providers = array_reverse($this->providers);

Embed/Providers/Embedly.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Embed\Url;
55

66
class Embedly {
7-
static public $api_key;
87
static public $patterns = array(
98
'http://*yfrog.*/*',
109
'http://twitter.com/*/status/*/photo/*',
@@ -640,12 +639,8 @@ class Embedly {
640639
'http://www.gogoyoko.com/song/*'
641640
);
642641

643-
static public function setApiKey ($key) {
644-
static::$api_key = $key;
645-
}
646-
647-
static public function create (Url $Url) {
648-
if (!static::$api_key) {
642+
static public function create (Url $Url, $api_key) {
643+
if (!$api_key) {
649644
return false;
650645
}
651646

@@ -655,7 +650,7 @@ static public function create (Url $Url) {
655650
$EndPoint->setParameter(array(
656651
'url' => $Url->getUrl(),
657652
'format' => 'json',
658-
'key' => static::$api_key
653+
'key' => $api_key
659654
));
660655

661656
return new OEmbed($EndPoint);

Embed/Sources/Feed.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,21 @@ static protected function getRssItems (\SimpleXMLElement $Items) {
6969
foreach ($Items as $Item) {
7070
$item = array(
7171
'url' => null,
72+
'originUrl' => null,
7273
'pubdate' => null
7374
);
7475

7576
$item['url'] = (string)$Item->link;
77+
$item['originUrl'] = ((string)$Item->origLink ?: (string)$Item->comments);
78+
7679
$item['pubdate'] = ((string)$Item->pubdate ?: (string)$Item->pubDate);
7780

7881
if (!$item['pubdate'] && isset($namespaces['dc']) && ($Children = $Item->children($namespaces['dc']))) {
79-
$item['pubdate'] = $Children->date;
82+
$item['pubdate'] = (string)$Children->date;
83+
}
84+
85+
if (!$item['originUrl'] && isset($namespaces['feedburner']) && ($Children = $Item->children($namespaces['feedburner']))) {
86+
$item['originUrl'] = (string)$Children->origLink;
8087
}
8188

8289
if ($item['url']) {
@@ -118,6 +125,7 @@ static protected function getAtomEntries (\SimpleXMLElement $Entries) {
118125
foreach ($Entries as $Entry) {
119126
$item = array(
120127
'url' => null,
128+
'originUrl' => null,
121129
'pubdate' => null
122130
);
123131

@@ -138,6 +146,15 @@ static protected function getAtomEntries (\SimpleXMLElement $Entries) {
138146
}
139147
}
140148

149+
foreach ($Entry->link as $link) {
150+
$attributes = $link->attributes();
151+
152+
if (!empty($attributes->href) && ((string)$attributes->rel === 'comments')) {
153+
$item['originUrl'] = (string)$attributes->href;
154+
break;
155+
}
156+
}
157+
141158
if (!$item['url']) {
142159
if ($Entry->link) {
143160
$attributes = $link->attributes();

test-sources.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
<?php foreach ($Source->items as $url): ?>
7575
<li>
7676
<a href="<?php echo $url['url']; ?>"><?php echo $url['url']; ?></a> | <a href="test.php?url=<?php echo urlencode($url['url']); ?>" target="_blank">Test</a><br>
77+
<?php if ($url['originUrl']): ?>
78+
<a href="<?php echo $url['originUrl']; ?>"><?php echo $url['originUrl']; ?></a> | <a href="test.php?url=<?php echo urlencode($url['originUrl']); ?>" target="_blank">Test origin Url</a><br>
79+
<?php endif; ?>
7780
<time><?php echo $url['pubdate']; ?></time>
7881
</li>
7982
<?php endforeach ?>

test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function getOption ($name, $default = null) {
7171
</p>
7272
<p>
7373
<label>Facebook access token: <input type="text" name="options[facebookAccessToken]" value="<?php echo getOption('facebookAccessToken'); ?>"></label><br>
74+
<label>Embedly key: <input type="text" name="options[embedlyKey]" value="<?php echo getOption('embedlyKey'); ?>"></label><br>
7475
<label>Soundcloud client id: <input type="text" name="options[soundcloudClientId]" value="<?php echo getOption('soundcloudClientId', 'YOUR_CLIENT_ID'); ?>"></label>
7576
</p>
7677
</fieldset>

0 commit comments

Comments
 (0)