|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/twitter/ |
|
4
|
|
|
* @copyright Copyright (c) 2018, Dukt |
|
5
|
|
|
* @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\twitter\web\twig; |
|
9
|
|
|
|
|
10
|
|
|
use craft\helpers\Template; |
|
11
|
|
|
use dukt\twitter\helpers\TwitterHelper; |
|
12
|
|
|
use dukt\twitter\Plugin; |
|
13
|
|
|
use Twig_Extension; |
|
14
|
|
|
use Twig_SimpleFunction; |
|
15
|
|
|
use Twig_SimpleFilter; |
|
16
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Twitter Twig Extension |
|
20
|
|
|
*/ |
|
21
|
|
|
class Extension extends Twig_Extension |
|
22
|
|
|
{ |
|
23
|
|
|
// Public Methods |
|
24
|
|
|
// ========================================================================= |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get Name |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getName() |
|
32
|
|
|
{ |
|
33
|
|
|
return 'Twitter'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get Filters |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getFilters() |
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
44
|
|
|
new Twig_SimpleFilter('autoLinkTweet', [$this, 'autoLinkTweet']), |
|
45
|
|
|
new Twig_SimpleFilter('twitterTimeAgo', [$this, 'timeAgo']) |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get Functions |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getFunctions() |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
new Twig_SimpleFunction('twitterGrid', [$this, 'twitterGrid']), |
|
58
|
|
|
new Twig_SimpleFunction('twitterTimeline', [$this, 'twitterTimeline']), |
|
59
|
|
|
new Twig_SimpleFunction('twitterTweet', [$this, 'twitterTweet']), |
|
60
|
|
|
new Twig_SimpleFunction('twitterVideo', [$this, 'twitterVideo']), |
|
61
|
|
|
new Twig_SimpleFunction('twitterMoment', [$this, 'twitterMoment']), |
|
62
|
|
|
new Twig_SimpleFunction('twitterFollowButton', [$this, 'twitterFollowButton']), |
|
63
|
|
|
new Twig_SimpleFunction('twitterMessageButton', [$this, 'twitterMessageButton']), |
|
64
|
|
|
new Twig_SimpleFunction('twitterTweetButton', [$this, 'twitterTweetButton']), |
|
65
|
|
|
new Twig_SimpleFunction('embedTweet', [$this, 'embedTweet']), |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the HTML of a Timeline widget as grid. |
|
71
|
|
|
* |
|
72
|
|
|
* @param $url |
|
73
|
|
|
* @param array $options |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Twig_Markup |
|
76
|
|
|
* @throws GuzzleException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function twitterGrid($url, $options = []) |
|
79
|
|
|
{ |
|
80
|
|
|
$html = Plugin::getInstance()->getPublish()->grid($url, $options); |
|
81
|
|
|
|
|
82
|
|
|
return Template::raw($html); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the HTML of a Moment widget. |
|
87
|
|
|
* |
|
88
|
|
|
* @param $url |
|
89
|
|
|
* @param array $options |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Twig_Markup |
|
92
|
|
|
* @throws GuzzleException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function twitterMoment($url, $options = []) |
|
95
|
|
|
{ |
|
96
|
|
|
$html = Plugin::getInstance()->getPublish()->moment($url, $options); |
|
97
|
|
|
|
|
98
|
|
|
return Template::raw($html); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the HTML of a Timeline widget. |
|
103
|
|
|
* |
|
104
|
|
|
* @param $url |
|
105
|
|
|
* @param array $options |
|
106
|
|
|
* |
|
107
|
|
|
* @return \Twig_Markup |
|
108
|
|
|
* @throws GuzzleException |
|
109
|
|
|
*/ |
|
110
|
|
|
public function twitterTimeline($url, $options = []) |
|
111
|
|
|
{ |
|
112
|
|
|
$html = Plugin::getInstance()->getPublish()->timeline($url, $options); |
|
113
|
|
|
|
|
114
|
|
|
return Template::raw($html); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the HTML of a Tweet widget. |
|
119
|
|
|
* |
|
120
|
|
|
* @param $url |
|
121
|
|
|
* @param array $options |
|
122
|
|
|
* |
|
123
|
|
|
* @return \Twig_Markup |
|
124
|
|
|
* @throws GuzzleException |
|
125
|
|
|
*/ |
|
126
|
|
|
public function twitterTweet($url, $options = []) |
|
127
|
|
|
{ |
|
128
|
|
|
$html = Plugin::getInstance()->getPublish()->tweet($url, $options); |
|
129
|
|
|
|
|
130
|
|
|
return Template::raw($html); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns the HTML of a Video Tweet widget. |
|
135
|
|
|
* |
|
136
|
|
|
* @param $url |
|
137
|
|
|
* @param array $options |
|
138
|
|
|
* |
|
139
|
|
|
* @return \Twig_Markup |
|
140
|
|
|
* @throws GuzzleException |
|
141
|
|
|
*/ |
|
142
|
|
|
public function twitterVideo($url, $options = []) |
|
143
|
|
|
{ |
|
144
|
|
|
$html = Plugin::getInstance()->getPublish()->video($url, $options); |
|
145
|
|
|
|
|
146
|
|
|
return Template::raw($html); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Returns the HTML of a Follow Button. |
|
151
|
|
|
* |
|
152
|
|
|
* @param $username |
|
153
|
|
|
* @param array $options |
|
154
|
|
|
* |
|
155
|
|
|
* @return \Twig_Markup |
|
156
|
|
|
*/ |
|
157
|
|
|
public function twitterFollowButton($username, $options = []) |
|
158
|
|
|
{ |
|
159
|
|
|
$html = Plugin::getInstance()->getPublish()->followButton($username, $options); |
|
160
|
|
|
|
|
161
|
|
|
return Template::raw($html); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the HTML of a Message Button. |
|
166
|
|
|
* |
|
167
|
|
|
* @param $recipientId |
|
168
|
|
|
* @param $screenName |
|
169
|
|
|
* @param null $text |
|
|
|
|
|
|
170
|
|
|
* @param array $options |
|
171
|
|
|
* |
|
172
|
|
|
* @return \Twig_Markup |
|
173
|
|
|
*/ |
|
174
|
|
|
public function twitterMessageButton($recipientId, $screenName, $text = null, $options = []) |
|
175
|
|
|
{ |
|
176
|
|
|
$html = Plugin::getInstance()->getPublish()->messageButton($recipientId, $screenName, $text, $options); |
|
177
|
|
|
|
|
178
|
|
|
return Template::raw($html); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Returns the HTML of a Tweet Button. |
|
183
|
|
|
* |
|
184
|
|
|
* @param array $options |
|
185
|
|
|
* |
|
186
|
|
|
* @return \Twig_Markup |
|
187
|
|
|
*/ |
|
188
|
|
|
public function twitterTweetButton($options = []) |
|
189
|
|
|
{ |
|
190
|
|
|
$html = Plugin::getInstance()->getPublish()->tweetButton($options); |
|
191
|
|
|
|
|
192
|
|
|
return Template::raw($html); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Auto Link Tweet |
|
197
|
|
|
* |
|
198
|
|
|
* @param $text |
|
199
|
|
|
* @param array $options |
|
200
|
|
|
* |
|
201
|
|
|
* @return \Twig_Markup |
|
202
|
|
|
*/ |
|
203
|
|
|
public function autoLinkTweet($text, $options = []) |
|
204
|
|
|
{ |
|
205
|
|
|
$html = Plugin::getInstance()->getTwitter()->autoLinkTweet($text, $options); |
|
206
|
|
|
|
|
207
|
|
|
return Template::raw($html); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Time Ago |
|
212
|
|
|
* |
|
213
|
|
|
* @param $date |
|
214
|
|
|
* |
|
215
|
|
|
* @return \Twig_Markup |
|
216
|
|
|
*/ |
|
217
|
|
|
public function timeAgo($date) |
|
218
|
|
|
{ |
|
219
|
|
|
$html = TwitterHelper::timeAgo($date); |
|
220
|
|
|
|
|
221
|
|
|
return Template::raw($html); |
|
222
|
|
|
} |
|
223
|
|
|
} |