Skip to content

Commit f8ccc75

Browse files
committed
improvements in facebook adapter
1 parent 204b221 commit f8ccc75

File tree

6 files changed

+91
-44
lines changed

6 files changed

+91
-44
lines changed

Embed/Adapters/Facebook.php

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,69 @@ class Facebook extends Webpage implements AdapterInterface
1212
{
1313
public $api;
1414

15+
private $isPost = false;
16+
1517
public static function check(Request $request)
1618
{
1719
return $request->match(array(
1820
'https://www.facebook.com/*'
1921
));
2022
}
2123

24+
private function getId($url)
25+
{
26+
$url = new Url($url);
27+
28+
if ($url->hasParameter('story_fbid')) {
29+
$this->isPost = true;
30+
31+
return $url->getParameter('story_fbid');
32+
}
33+
34+
if ($url->hasParameter('fbid')) {
35+
return $url->getParameter('fbid');
36+
}
37+
38+
if ($url->hasParameter('id')) {
39+
return $url->getParameter('id');
40+
}
41+
42+
if ($url->getDirectory(0) === 'events') {
43+
return $url->getDirectory(1);
44+
}
45+
46+
if ($url->getDirectory(0) === 'pages') {
47+
return $url->getDirectory(2);
48+
}
49+
50+
if ($url->getDirectory(1) === 'posts') {
51+
return $url->getDirectory(2);
52+
}
53+
54+
if ($url->getDirectory(2) === 'posts') {
55+
return $url->getDirectory(3);
56+
}
57+
58+
return $url->getDirectory(0);
59+
}
60+
2261
protected function initProviders(Request $request)
2362
{
2463
parent::initProviders($request);
2564

2665
$this->api = new Provider();
2766

28-
if ($this->options['facebookAccessToken']) {
29-
$url = new Url($request->getStartingUrl());
30-
31-
if ($url->hasParameter('fbid')) {
32-
$id = $url->getParameter('fbid');
33-
} elseif ($url->hasParameter('story_fbid')) {
34-
$id = $url->getParameter('story_fbid');
35-
} elseif ($url->getDirectory(0) === 'events') {
36-
$id = $url->getDirectory(1);
37-
} elseif ($url->getDirectory(0) === 'pages') {
38-
$id = $url->getDirectory(2);
39-
} elseif ($url->getDirectory(1) === 'posts') {
40-
$id = $url->getDirectory(2);
41-
} elseif ($url->getDirectory(2) === 'posts') {
42-
$id = $url->getDirectory(3);
43-
} else {
44-
$id = $url->getDirectory(0);
45-
}
46-
47-
if ($id) {
67+
if (($id = $this->getId($request->getStartingUrl()))) {
68+
if ($this->options['facebookAccessToken']) {
4869
$api = new Request('https://graph.facebook.com/'.$id);
4970
$api->setParameter('access_token', $this->options['facebookAccessToken']);
5071

5172
if ($json = $api->getJsonContent()) {
5273
$this->api->set($json);
5374
}
5475
}
76+
77+
$this->api->set('id', $id);
5578
}
5679
}
5780

@@ -62,12 +85,34 @@ public function getTitle()
6285

6386
public function getDescription()
6487
{
65-
return $this->api->get('description') ?: $this->api->get('about') ?: parent::getTitle();
88+
return $this->api->get('description') ?: $this->api->get('about') ?: parent::getDescription();
6689
}
6790

6891
public function getUrl()
6992
{
70-
return $this->api->get('link') ?: $this->request->getStartingUrl();
93+
if ($this->isPost) {
94+
return $this->request->getStartingUrl();
95+
}
96+
97+
return $this->api->get('url') ?: $this->request->getStartingUrl();
98+
}
99+
100+
public function getCode()
101+
{
102+
if ($this->isPost) {
103+
return <<<EOT
104+
<div id="fb-root"></div>
105+
<script>(function(d, s, id) {
106+
var js, fjs = d.getElementsByTagName(s)[0];
107+
if (d.getElementById(id)) return;
108+
js = d.createElement(s); js.id = id;
109+
js.src = "//connect.facebook.net/gl_ES/all.js#xfbml=1";
110+
fjs.parentNode.insertBefore(js, fjs);
111+
}(document, 'script', 'facebook-jssdk'));</script>
112+
113+
<div class="fb-post" data-href="{$this->url}" data-width="500"></div>
114+
EOT;
115+
}
71116
}
72117

73118
public function getProviderName()

Embed/Providers/OEmbed.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ public function __construct(Request $request)
1616
switch ($format) {
1717
case 'json':
1818
if (($parameters = $request->getJsonContent()) && empty($parameters['Error'])) {
19-
foreach ($parameters as $name => $value) {
20-
$this->set($name, $value);
21-
}
19+
$this->set($parameters);
2220
}
2321
break;
2422

Embed/Providers/OpenGraph.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ public function __construct(Request $request)
1818

1919
$images = array();
2020

21-
foreach ($html->getElementsByTagName('meta') as $tag) {
22-
if ($tag->hasAttribute('property') && (strpos($tag->getAttribute('property'), 'og:') === 0)) {
23-
$name = substr($tag->getAttribute('property'), 3);
24-
} elseif ($tag->hasAttribute('name') && (strpos($tag->getAttribute('name'), 'og:') === 0)) {
25-
$name = substr($tag->getAttribute('name'), 3);
21+
foreach ($html->getElementsByTagName('meta') as $meta) {
22+
if (strpos($meta->getAttribute('property'), 'og:') === 0) {
23+
$name = substr($meta->getAttribute('property'), 3);
24+
} elseif (strpos($meta->getAttribute('name'), 'og:') === 0) {
25+
$name = substr($meta->getAttribute('name'), 3);
2626
} else {
2727
continue;
2828
}
2929

30-
$value = $tag->getAttribute('content') ?: $tag->getAttribute('value');
30+
$value = $meta->getAttribute('content') ?: $meta->getAttribute('value');
3131

3232
if ($name === 'image') {
3333
$images[] = $value;

Embed/Request.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function getStartingUrl()
110110
/**
111111
* Get the http code of the url
112112
*
113-
* @return int The http code
113+
* @return integer The http code
114114
*/
115115
public function getHttpCode()
116116
{
@@ -143,7 +143,7 @@ public function getContent()
143143
/**
144144
* Get the content of the url as a DOMDocument object
145145
*
146-
* @return DOMDocument The content or false
146+
* @return \DOMDocument The content or false
147147
*/
148148
public function getHtmlContent()
149149
{
@@ -179,7 +179,7 @@ public function getHtmlContent()
179179
/**
180180
* Get the content of the url as an array from json
181181
*
182-
* @return array The content or false
182+
* @return false|array The content or false
183183
*/
184184
public function getJsonContent()
185185
{
@@ -202,7 +202,7 @@ public function getJsonContent()
202202
/**
203203
* Get the content of the url as an XML element
204204
*
205-
* @return SimpleXMLElement The content or false
205+
* @return false|\SimpleXMLElement The content or false
206206
*/
207207
public function getXMLContent()
208208
{

Embed/RequestResolvers/Curl.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function getRequestInfo()
112112
*
113113
* @param string $name Parameter name
114114
*
115-
* @return array|string The result info
115+
* @return null|string The result info
116116
*/
117117
protected function getResult($name)
118118
{
@@ -159,18 +159,20 @@ protected function resolve()
159159

160160
curl_close($connection);
161161

162-
if (strpos($this->getResult('content_type'), ';') !== false) {
163-
list($mimeType, $charset) = explode(';', $this->result['content_type']);
162+
if (($content_type = $this->getResult('content_type'))) {
163+
if (strpos($content_type, ';') !== false) {
164+
list($mimeType, $charset) = explode(';', $content_type);
164165

165-
$this->result['mime_type'] = $mimeType;
166+
$this->result['mime_type'] = $mimeType;
166167

167-
$charset = substr(strtoupper(strstr($charset, '=')), 1);
168+
$charset = substr(strtoupper(strstr($charset, '=')), 1);
168169

169-
if (!empty($charset) && !empty($this->content) && ($charset !== 'UTF-8')) {
170-
$this->content = @mb_convert_encoding($this->content, 'UTF-8', $charset);
170+
if (!empty($charset) && !empty($this->content) && ($charset !== 'UTF-8')) {
171+
$this->content = @mb_convert_encoding($this->content, 'UTF-8', $charset);
172+
}
173+
} elseif (strpos($content_type, '/') !== false) {
174+
$this->result['mime_type'] = $content_type;
171175
}
172-
} elseif (strpos($this->getResult('content_type'), '/') !== false) {
173-
$this->result['mime_type'] = $this->result['content_type'];
174176
}
175177
}
176178
}

test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ function getResolverOption($name, $default = null)
152152
<button type="submit">Test</button>
153153
&nbsp;&nbsp;&nbsp;
154154
<a href="https://github.com/oscarotero/Embed/">Get the source code from Github</a>
155+
&nbsp;&nbsp; - &nbsp;&nbsp;
156+
<a href="javascript:(function(){window.open('http://oscarotero.com/embed/test.php?url='+document.location)})();">or the bookmarklet</a>
155157
</fieldset>
156158
</form>
157159

0 commit comments

Comments
 (0)