-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.php
More file actions
127 lines (107 loc) · 4.21 KB
/
extension.php
File metadata and controls
127 lines (107 loc) · 4.21 KB
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
<?php
namespace PHPemTwitterAPI;
require_once dirname(__FILE__) . '/vendor/tmhOAuth.php';
use Bolt\BaseExtension;
class Extension extends BaseExtension
{
/**
* @return array
*/
public function info()
{
$data = array(
'name' => "PHPemTwitterAPI",
'description' => "Twitter API integration for the PHPem website",
'author' => "Jack Bentley (Viva IT)",
'link' => "http://www.vivait.co.uk",
'version' => "1.0",
'required_bolt_version' => "1.5",
'highest_bolt_version' => "2.0",
'type' => "Twig function",
'first_releasedate' => "0000-00-00",
'latest_releasedate' => "0000-00-00",
'allow_in_user_content' => true,
);
return $data;
}
public function initialize()
{
$this->app['twig.loader.filesystem']->addPath(__DIR__);
$this->addTwigFunction('latestTweets', 'latestTweets');
}
public function latestTweets($user, $amount = 1)
{
$api = new \tmhOAuth([
'consumer_key' => $this->config['auth']['consumer']['key'],
'consumer_secret' => $this->config['auth']['consumer']['secret'],
'token' => $this->config['auth']['access']['token'],
'secret' => $this->config['auth']['access']['secret'],
//'bearer' => 'YOUR_OAUTH2_TOKEN',
'user_agent' => 'PHPem Website',
]);
$api->apponly_request([
'url' => $api->url('1.1/statuses/user_timeline'),
'without_bearer' => true,
'params' => [
'screen_name' => $user,
'count' => $amount,
]
]);
$response = json_decode($api->response['response'], true);
$output = '';
foreach($response as $tweet) {
$this->embedTweetLinks($tweet);
if(isset($tweet['retweeted_status']))
$this->embedTweetLinks($tweet['retweeted_status']);
$output .= $this->app['render']->render($this->config['template']['tweet'], $tweet);
}
return new \Twig_Markup($output, 'UTF-8');
}
private function embedTweetLinks(&$tweet) {
$tweet['text'] = htmlentities($tweet['text']);
if(isset($tweet['entities']['user_mentions'])) {
foreach(array_reverse($tweet['entities']['user_mentions']) as $entity) {
$tweet['text'] = substr_replace(
$tweet['text'],
'<a href="https://www.twitter.com/' . urlencode($entity['screen_name']) . '" target="_blank">'
. substr($tweet['text'], $entity['indices'][0], $entity['indices'][1] - $entity['indices'][0])
. '</a>',
$entity['indices'][0], $entity['indices'][1] - $entity['indices'][0]
);
}
}
if(isset($tweet['entities']['hashtags'])) {
foreach(array_reverse($tweet['entities']['hashtags']) as $entity) {
$tweet['text'] = str_replace(
'#' . $entity['text'],
'<a href="https://www.twitter.com/hashtag/' . urlencode($entity['text']) . '" target="_blank">'
. '#' . $entity['text']
. '</a>',
$tweet['text']
);
}
}
if(isset($tweet['entities']['media'])) {
foreach(array_reverse($tweet['entities']['media']) as $entity) {
$tweet['text'] = str_replace(
$entity['url'],
'<a href="' . $entity['url'] . '" target="_blank">'
. $entity['display_url']
. '</a>',
$tweet['text']
);
}
}
if(isset($tweet['entities']['urls'])) {
foreach(array_reverse($tweet['entities']['urls']) as $entity) {
$tweet['text'] = str_replace(
$entity['url'],
'<a href="' . $entity['url'] . '" target="_blank">'
. $entity['display_url']
. '</a>',
$tweet['text']
);
}
}
}
}