1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SunnysideUp\ShareThis; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injectable; |
6
|
|
|
use SilverStripe\Control\Session; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SunnysideUp\ShareThis\MyTwitter; |
9
|
|
|
use SunnysideUp\ShareThis\MyTwitterData; |
10
|
|
|
use SilverStripe\Control\Director; |
11
|
|
|
use SunnysideUp\ShareThis\TwitterOAuth; |
12
|
|
|
use SilverStripe\ORM\ArrayList; |
13
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
14
|
|
|
use SilverStripe\View\ArrayData; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author romain [at] sunnys side up .co.nz + nicolaas [at] sunny side up . co .nz |
18
|
|
|
* @inspiration: https://github.com/tylerkidd/silverstripe-twitter-feed/ |
19
|
|
|
* @funding: MSO Design (www.msodesign.com) |
20
|
|
|
* |
21
|
|
|
**/ |
22
|
|
|
class MyTwitter |
23
|
|
|
{ |
24
|
|
|
use Injectable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var boolean |
28
|
|
|
*/ |
29
|
|
|
private static $debug = false; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $singletons = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var boolean |
38
|
|
|
*/ |
39
|
|
|
private static $favourites_only = false; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var boolean |
43
|
|
|
*/ |
44
|
|
|
private static $non_replies_only = false; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private static $twitter_consumer_key = ""; |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private static $twitter_consumer_secret = ""; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private static $titter_oauth_token = ""; |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private static $titter_oauth_token_secret = ""; |
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private static $twitter_config = [ |
|
|
|
|
70
|
|
|
'include_entities' => 'true', |
71
|
|
|
'include_rts' => 'true' |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* returns a DataObjetSet of the last $count tweets. |
76
|
|
|
* - saves twitter feed to dataobject |
77
|
|
|
* |
78
|
|
|
* @param String $username (e.g. mytwitterhandle) |
79
|
|
|
* @param Int $count - number of tweets to retrieve at any one time |
80
|
|
|
* @return DataObjectSet | Null |
|
|
|
|
81
|
|
|
*/ |
82
|
|
|
public static function last_statuses($username, $count = 1, $useHourlyCache = true) |
83
|
|
|
{ |
84
|
|
|
if (!$username) { |
85
|
|
|
user_error("No username provided"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$sessionName = "MyTwitterFeeds$username".date("Ymdh"); |
89
|
|
|
|
90
|
|
|
if (Session::get($sessionName) && $useHourlyCache && !Config::inst()->get(MyTwitter::class, "debug")) { |
|
|
|
|
91
|
|
|
//do nothing |
92
|
|
|
} else { |
93
|
|
|
if (empty(self::$singletons[$username])) { |
94
|
|
|
self::$singletons[$username] = new MyTwitter($username, $count); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$dataObjectSet = self::$singletons[$username]->TwitterFeed($username, $count); |
98
|
|
|
|
99
|
|
|
if ($dataObjectSet && $dataObjectSet->count()) { |
100
|
|
|
foreach ($dataObjectSet as $tweet) { |
101
|
|
|
if (!MyTwitterData::get()->filter(["TwitterID" => $tweet->ID])->count()) { |
102
|
|
|
$myTwitterData = new MyTwitterData(); |
103
|
|
|
$myTwitterData->TwitterID = $tweet->ID; |
|
|
|
|
104
|
|
|
$myTwitterData->Title = $tweet->Title; |
105
|
|
|
$myTwitterData->Date = $tweet->Date; |
|
|
|
|
106
|
|
|
$myTwitterData->write(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
Session::set($sessionName, 1); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
Config::inst()->update(MyTwitterData::class, "username", $username); |
|
|
|
|
115
|
|
|
return MyTwitterData::get()->filter(["Hide" => 0])->limit($count); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* retries latest tweets from Twitter |
121
|
|
|
* |
122
|
|
|
* @param String $username (e.g. mytwitterhandle) |
123
|
|
|
* @param Int $count - number of tweets to retrieve at any one time |
124
|
|
|
* @return DataObjectSet | Null |
125
|
|
|
*/ |
126
|
|
|
public function TwitterFeed($username, $count = 5) |
127
|
|
|
{ |
128
|
|
|
if (!$username) { |
129
|
|
|
user_error("No username provided"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
Config::inst()->update(MyTwitterData::class, "username", $username); |
133
|
|
|
|
134
|
|
|
//check settings are available |
135
|
|
|
$requiredSettings = [ |
136
|
|
|
"twitter_consumer_key", |
137
|
|
|
"twitter_consumer_secret", |
138
|
|
|
"titter_oauth_token", |
139
|
|
|
"titter_oauth_token" |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
foreach ($requiredSettings as $setting) { |
143
|
|
|
if (!Config::inst()->get(MyTwitter::class, $setting)) { |
144
|
|
|
user_error(" you must set MyTwitter::$setting", E_USER_NOTICE); |
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
require_once(Director::baseFolder().'/'.SS_SHARETHIS_DIR.'/third_party/twitter_oauth/TwitterOAuthConsumer.php'); |
150
|
|
|
|
151
|
|
|
$connection = new TwitterOAuth( |
152
|
|
|
Config::inst()->get(MyTwitter::class, "twitter_consumer_key"), |
153
|
|
|
Config::inst()->get(MyTwitter::class, "twitter_consumer_secret"), |
154
|
|
|
Config::inst()->get(MyTwitter::class, "titter_oauth_token"), |
155
|
|
|
Config::inst()->get(MyTwitter::class, "titter_oauth_token_secret") |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$config = Config::inst()->get(MyTwitter::class, "twitter_config"); |
159
|
|
|
$config['screen_name'] = $username; |
160
|
|
|
$tweets = $connection->get('statuses/user_timeline', $config); |
161
|
|
|
$tweetList = new ArrayList(); |
162
|
|
|
|
163
|
|
|
if (count($tweets) > 0 && !isset($tweets->error)) { |
164
|
|
|
$i = 0; |
165
|
|
|
|
166
|
|
|
foreach ($tweets as $tweet) { |
167
|
|
|
if (Config::inst()->get(MyTwitter::class, "favourites_only") && $tweet->favorite_count == 0) { |
168
|
|
|
break; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (Config::inst()->get(MyTwitter::class, "non_replies_only") && $tweet->in_reply_to_status_id) { |
172
|
|
|
break; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (Config::inst()->get(MyTwitter::class, "debug")) { |
176
|
|
|
print_r($tweet); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (++$i > $count) { |
180
|
|
|
break; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$date = new DBDatetime(); |
184
|
|
|
$date->setValue(strtotime($tweet->created_at)); |
185
|
|
|
$text = htmlentities($tweet->text, ENT_NOQUOTES, $encoding = "UTF-8", $doubleEncode = false); |
186
|
|
|
|
187
|
|
|
if (!empty($tweet->entities) && !empty($tweet->entities->urls)) { |
188
|
|
|
foreach ($tweet->entities->urls as $url) { |
189
|
|
|
if (!empty($url->url) && !empty($url->display_url)) { |
190
|
|
|
$text = str_replace($url->url, '<a href="'.$url->url.'" class="external">'.$url->display_url.'</a>', $text); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$tweetList->push( |
196
|
|
|
new ArrayData([ |
197
|
|
|
'ID' => $tweet->id_str, |
198
|
|
|
'Title' => $text, |
199
|
|
|
'Date' => $date |
200
|
|
|
]) |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $tweetList; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|