1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SunnySideUp\ShareThis; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injectable; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Dev\Debug; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* https://developers.facebook.com/tools-and-support/ |
11
|
|
|
*/ |
12
|
|
|
class SilverstripeFacebookConnector |
13
|
|
|
{ |
14
|
|
|
use Injectable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Facebook Connection |
18
|
|
|
*/ |
19
|
|
|
private static $connection = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* settings for connection |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $connection_config = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* application ID - get from FB |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private static $app_id = ""; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* application secret - get from FB |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private static $app_secret = ""; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* debug |
42
|
|
|
* @var boolean |
43
|
|
|
*/ |
44
|
|
|
protected static $debug = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* keep track of errors |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected static $error = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* set additional connection details - e.g. default_access_token |
54
|
|
|
* @param array |
55
|
|
|
*/ |
56
|
|
|
public static function set_connection_config($connectionConfig) |
57
|
|
|
{ |
58
|
|
|
self::$connection_config = $connectionConfig; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* create FB connection... |
63
|
|
|
* @return Facebook\Facebook |
64
|
|
|
*/ |
65
|
|
|
protected static function get_connection() |
66
|
|
|
{ |
67
|
|
|
if (!self::$connection) { |
68
|
|
|
self::$connection_config += array( |
69
|
|
|
'app_id' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_id"), |
70
|
|
|
'app_secret' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret"), |
71
|
|
|
'default_graph_version' => 'v2.4', |
72
|
|
|
//'default_access_token' => '{access-token}', // optional |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
self::$connection = new Facebook\Facebook(self::$connection_config); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
return self::$connection; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
* @param string $openGraphCommand |
83
|
|
|
* |
84
|
|
|
* @return FacebookResponse | false |
85
|
|
|
*/ |
86
|
|
|
public static function run_command($openGraphCommand = "") |
87
|
|
|
{ |
88
|
|
|
$fb = self::get_connection(); |
89
|
|
|
$accessToken = Config::inst()->get(SilverstripeFacebookConnector::class, "app_id")."|".Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret"); |
90
|
|
|
//$helper = $fb->getPageTabHelper(); |
91
|
|
|
try { |
92
|
|
|
$response = $fb->get($openGraphCommand, $accessToken); |
93
|
|
|
} catch (Facebook\Exceptions\FacebookResponseException $e) { |
|
|
|
|
94
|
|
|
// When Graph returns an error |
95
|
|
|
self::$error[] = 'Graph returned an error: ' . $e->getMessage(); |
96
|
|
|
return false; |
97
|
|
|
} catch (Facebook\Exceptions\FacebookSDKException $e) { |
|
|
|
|
98
|
|
|
// When validation fails or other local issues |
99
|
|
|
self::$error[] = 'Facebook SDK returned an error: ' . $e->getMessage(); |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
if (self::$debug) { |
103
|
|
|
Debug::log(implode(" | ", self::$error)); |
104
|
|
|
} |
105
|
|
|
return $response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return details about logged in person |
110
|
|
|
*/ |
111
|
|
|
public static function whoami() |
112
|
|
|
{ |
113
|
|
|
$response = self::run_command("/me"); |
114
|
|
|
if ($response) { |
115
|
|
|
return $response->getGraphUser(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* returns an array of recent posts for a page |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public static function get_feed($pageID) |
125
|
|
|
{ |
126
|
|
|
$response = self::run_command($pageID . "/posts?fields=message,created_time,id,full_picture,link,from,name,description"); |
127
|
|
|
if ($response) { |
128
|
|
|
$list = $response->getDecodedBody(); |
129
|
|
|
if (isset($list["data"])) { |
130
|
|
|
return $list["data"]; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* returns an array of recent posts for a page |
137
|
|
|
* @return array |
|
|
|
|
138
|
|
|
*/ |
139
|
|
|
public static function check_if_posts_exists($UID) |
140
|
|
|
{ |
141
|
|
|
$response = self::run_command('/Post/'.$UID); |
142
|
|
|
print_r($response); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..