forked from isaacrankin/silverstripe-social-feed
/
SocialFeedProviderFacebook.php
199 lines (163 loc) · 5.42 KB
/
SocialFeedProviderFacebook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace IsaacRankin\SocialFeed\Providers;
use League\OAuth2\Client\Provider\Facebook;
use IsaacRankin\SocialFeed\SocialFeedProviderInterface;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\ORM\Exception;
use SilverStripe\ORM\FieldType\DBField;
class SocialFeedProviderFacebook extends SocialFeedProvider implements SocialFeedProviderInterface
{
private static $db = array(
'FacebookPageID' => 'Varchar(100)',
'FacebookAppID' => 'Varchar(400)',
'FacebookAppSecret' => 'Varchar(400)',
'AccessToken' => 'Varchar(400)',
'FacebookType' => 'Int',
);
private static $table_name = 'SocialFeedProviderFacebook';
private static $singular_name = 'Facebook Provider';
private static $plural_name = 'Facebook Providers';
private static $summary_fields = array(
'Label',
'Enabled',
'FacebookPageID'
);
const POSTS_AND_COMMENTS = 0;
const POSTS_ONLY = 1;
private static $facebook_types = array(
self::POSTS_AND_COMMENTS => 'Page Posts and Comments',
self::POSTS_ONLY => 'Page Posts Only'
);
private $type = 'facebook';
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Main', LiteralField::create('sf_html_1', '<h4>To get the necessary Facebook API credentials you\'ll need to create a <a href="https://developers.facebook.com/apps" target="_blank">Facebook App.</a></h4><p> </p>'), 'Label');
$fields->replaceField('FacebookType', DropdownField::create('FacebookType', 'Facebook Type', $this->config()->facebook_types));
$fields->removeByName('AccessToken');
return $fields;
}
public function getCMSValidator()
{
return new RequiredFields(array('FacebookPageID', 'FacebookAppID', 'FacebookAppSecret'));
}
public function onBeforeWrite()
{
if ($this->FacebookAppID && $this->FacebookAppSecret) {
$this->AccessToken = $this->FacebookAppID . '|' . $this->FacebookAppSecret;
} else if ($this->AccessToken) {
$this->AccessToken = '';
}
parent::onBeforeWrite();
}
/**
* Return the type of provider
*
* @return string
*/
public function getType()
{
return $this->type;
}
public function getFeedUncached()
{
$providerOptions = [
'clientId' => $this->FacebookAppID,
'clientSecret' => $this->FacebookAppSecret,
// https://github.com/thephpleague/oauth2-facebook#graph-api-version
'graphApiVersion' => 'v2.6'
];
if (\SilverStripe\Core\Environment::getEnv('SS_OUTBOUND_PROXY')) {
$providerOptions['proxy'] = sprintf('%s:%s', \SilverStripe\Core\Environment::getEnv('SS_OUTBOUND_PROXY'), \SilverStripe\Core\Environment::getEnv('SS_OUTBOUND_PROXY_PORT'));
}
if (isset($_GET['feedtest'])) {
info($providerOptions);
die;
}
$provider = new Facebook($providerOptions);
// For an App Access Token we can just use our App ID and App Secret pipped together
// https://developers.facebook.com/docs/facebook-login/access-tokens#apptokens
$accessToken = ($this->AccessToken) ? $this->AccessToken : $this->siteConfig->SocialFeedFacebookAppID . '|' . $this->siteConfig->SocialFeedFacebookAppSecret;
// Setup query params for FB query
$queryParameters = array(
// Get Facebook timestamps in Unix timestamp format
'date_format' => 'U',
// Explicitly supply all known 'fields' as the API was returning a minimal fieldset by default.
'fields' => 'from,message,message_tags,story,story_tags,full_picture,source,link,object_id,name,caption,description,icon,privacy,type,status_type,created_time,updated_time,shares,is_hidden,is_expired,likes,comments',
'access_token' => $accessToken,
);
$queryParameters = http_build_query($queryParameters);
// Get all data for the FB page
switch ($this->FacebookType) {
case self::POSTS_AND_COMMENTS:
$request = $provider->getRequest('GET', 'https://graph.facebook.com/' . $this->FacebookPageID . '/feed?'.$queryParameters);
break;
case self::POSTS_ONLY:
$request = $provider->getRequest('GET', 'https://graph.facebook.com/' . $this->FacebookPageID . '/posts?'.$queryParameters);
break;
default:
throw new Exception('Invalid FacebookType ('.$this->FacebookType.')');
break;
}
$result = $provider->getResponse($request);
return $result['data'];
$output = json_decode($result->getBody(), 1);
return $output['data'];
}
/**
* @return HTMLText
*/
public function getPostContent($post) {
$text = isset($post['message']) ? $post['message'] : '';
$result = DBField::create_field('HTMLText', $text);
return $result;
}
/**
* Get the creation time from a post
*
* @param $post
* @return mixed
*/
public function getPostCreated($post)
{
return $post['created_time'];
}
/**
* Get the post URL from a post
*
* @param $post
* @return mixed
*/
public function getPostUrl($post)
{
if (isset($post['actions'][0]['name']) && $post['actions'][0]['name'] === 'Share') {
return $post['actions'][0]['link'];
} else if (isset($post['link']) && $post['link']) {
// For $post['type'] === 'link' && $post['status_type'] === 'shared_story'
return $post['link'];
}
return null;
}
/**
* Get the user who made the post
*
* @param $post
* @return mixed
*/
public function getUserName($post)
{
return $post['from']['name'];
}
/**
* Get the primary image for the post
*
* @param $post
* @return mixed
*/
public function getImage($post)
{
return (isset($post['full_picture'])) ? $post['full_picture'] : null;
}
}