|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/twitter/ |
|
4
|
|
|
* @copyright Copyright (c) Dukt |
|
5
|
|
|
* @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\twitter\services; |
|
9
|
|
|
|
|
10
|
|
|
use Craft; |
|
11
|
|
|
use dukt\twitter\Plugin; |
|
12
|
|
|
use yii\base\Component; |
|
13
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Publish Service |
|
17
|
|
|
* |
|
18
|
|
|
* @author Dukt <[email protected]> |
|
19
|
|
|
* @since 3.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class Publish extends Component |
|
22
|
|
|
{ |
|
23
|
|
|
// Public Methods |
|
24
|
|
|
// ========================================================================= |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Returns the HTML of a Timeline widget as grid. |
|
28
|
|
|
* |
|
29
|
|
|
* @param $url |
|
30
|
|
|
* @param array $options |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
* @throws GuzzleException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function grid($url, $options = []) |
|
36
|
|
|
{ |
|
37
|
|
|
$dataAttributes = $this->getOptionsAsDataAttributes($options); |
|
38
|
|
|
|
|
39
|
|
|
$response = $this->oEmbed($url); |
|
40
|
|
|
|
|
41
|
|
|
if (!$response) { |
|
42
|
|
|
return null; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$html = $response['html']; |
|
46
|
|
|
$html = str_replace('<a class="twitter-timeline"', '<a class="twitter-grid"'.$dataAttributes, $html); |
|
47
|
|
|
|
|
48
|
|
|
return $html; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns the HTML of a Moment widget. |
|
53
|
|
|
* |
|
54
|
|
|
* @param $url |
|
55
|
|
|
* @param array $options |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
* @throws GuzzleException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function moment($url, $options = []) |
|
61
|
|
|
{ |
|
62
|
|
|
$dataAttributes = $this->getOptionsAsDataAttributes($options); |
|
63
|
|
|
|
|
64
|
|
|
$response = $this->oEmbed($url); |
|
65
|
|
|
|
|
66
|
|
|
if (!$response) { |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$html = $response['html']; |
|
71
|
|
|
$html = str_replace('<a class="twitter-moment"', '<a class="twitter-moment"'.$dataAttributes, $html); |
|
72
|
|
|
|
|
73
|
|
|
return $html; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the HTML of a Timeline widget. |
|
78
|
|
|
* |
|
79
|
|
|
* @param $url |
|
80
|
|
|
* @param array $options |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
* @throws GuzzleException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function timeline($url, $options = []) |
|
86
|
|
|
{ |
|
87
|
|
|
$dataAttributes = $this->getOptionsAsDataAttributes($options); |
|
88
|
|
|
|
|
89
|
|
|
$response = $this->oEmbed($url); |
|
90
|
|
|
|
|
91
|
|
|
if (!$response) { |
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$html = $response['html']; |
|
96
|
|
|
$html = str_replace('<a class="twitter-timeline"', '<a class="twitter-timeline"'.$dataAttributes, $html); |
|
97
|
|
|
|
|
98
|
|
|
return $html; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the HTML of a Tweet widget. |
|
103
|
|
|
* |
|
104
|
|
|
* @param $url |
|
105
|
|
|
* @param array $options |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
* @throws GuzzleException |
|
109
|
|
|
*/ |
|
110
|
|
|
public function tweet($url, $options = []) |
|
111
|
|
|
{ |
|
112
|
|
|
$dataAttributes = $this->getOptionsAsDataAttributes($options); |
|
113
|
|
|
|
|
114
|
|
|
$response = $this->oEmbed($url); |
|
115
|
|
|
|
|
116
|
|
|
if (!$response) { |
|
117
|
|
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$html = $response['html']; |
|
121
|
|
|
$html = str_replace('<blockquote class="twitter-tweet">', '<blockquote class="twitter-tweet"'.$dataAttributes.'>', $html); |
|
122
|
|
|
|
|
123
|
|
|
return $html; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns the HTML of a Video Tweet widget. |
|
128
|
|
|
* |
|
129
|
|
|
* @param $url |
|
130
|
|
|
* @param array $options |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
* @throws GuzzleException |
|
134
|
|
|
*/ |
|
135
|
|
|
public function video($url, $options = []) |
|
136
|
|
|
{ |
|
137
|
|
|
$dataAttributes = $this->getOptionsAsDataAttributes($options); |
|
138
|
|
|
|
|
139
|
|
|
$response = $this->oEmbed($url, ['widget_type' => 'video']); |
|
140
|
|
|
|
|
141
|
|
|
if (!$response) { |
|
142
|
|
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$html = $response['html']; |
|
146
|
|
|
$html = str_replace('<blockquote class="twitter-video">', '<blockquote class="twitter-video"'.$dataAttributes.'>', $html); |
|
147
|
|
|
|
|
148
|
|
|
return $html; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns the HTML of a Follow Button. |
|
153
|
|
|
* |
|
154
|
|
|
* @param $username |
|
155
|
|
|
* @param array $options |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
public function followButton($username, $options = []) |
|
160
|
|
|
{ |
|
161
|
|
|
$dataAttributes = $this->getOptionsAsDataAttributes($options); |
|
162
|
|
|
|
|
163
|
|
|
return '<a class="twitter-follow-button" href="https://twitter.com/'.$username.'"'.$dataAttributes.'>Follow @'.$username.'</a>'; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Returns the HTML of a Message Button. |
|
168
|
|
|
* |
|
169
|
|
|
* @param $recipientId |
|
170
|
|
|
* @param $screenName |
|
171
|
|
|
* @param null $text |
|
|
|
|
|
|
172
|
|
|
* @param array $options |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
public function messageButton($recipientId, $screenName, $text = null, $options = []) |
|
177
|
|
|
{ |
|
178
|
|
|
$options['screenName'] = $screenName; |
|
179
|
|
|
|
|
180
|
|
|
$dataAttributes = $this->getOptionsAsDataAttributes($options); |
|
181
|
|
|
|
|
182
|
|
|
return '<a class="twitter-dm-button" href="https://twitter.com/messages/compose?recipient_id='.$recipientId.'&text='.rawurlencode($text).'"'.$dataAttributes.'>Message @'.$screenName.'</a>'; |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns the HTML of a Tweet Button. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $options |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
public function tweetButton($options = []) |
|
193
|
|
|
{ |
|
194
|
|
|
$dataAttributes = $this->getOptionsAsDataAttributes($options); |
|
195
|
|
|
|
|
196
|
|
|
return '<a class="twitter-share-button" href="https://twitter.com/share"'.$dataAttributes.'>Tweet</a>'; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns an array of options as HTML data attributes. |
|
201
|
|
|
* |
|
202
|
|
|
* @param array $options |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getOptionsAsDataAttributes($options) |
|
207
|
|
|
{ |
|
208
|
|
|
// Options Aliases (camel to kebab case) |
|
209
|
|
|
|
|
210
|
|
|
$aliases = [ |
|
211
|
|
|
'linkColor' => 'link-color', |
|
212
|
|
|
'tweetLimit' => 'tweet-limit', |
|
213
|
|
|
'showReplies' => 'show-replies', |
|
214
|
|
|
'borderColor' => 'border-color', |
|
215
|
|
|
'ariaPolite' => 'aria-polite', |
|
216
|
|
|
'screenName' => 'screen-name', |
|
217
|
|
|
'showScreenName' => 'show-screen-name', |
|
218
|
|
|
'showCount' => 'show-count', |
|
219
|
|
|
]; |
|
220
|
|
|
|
|
221
|
|
|
foreach ($aliases as $key => $alias) { |
|
222
|
|
|
if (!empty($options[$key])) { |
|
223
|
|
|
$options[$alias] = $options[$key]; |
|
224
|
|
|
unset($options[$key]); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
// HTML Attributes |
|
230
|
|
|
|
|
231
|
|
|
$dataAttributes = ''; |
|
232
|
|
|
|
|
233
|
|
|
foreach ($options as $key => $value) { |
|
234
|
|
|
$dataAttributes .= ' data-'.$key.'="'.$value.'"'; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return $dataAttributes; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
// Private Methods |
|
241
|
|
|
// ========================================================================= |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Returns an oEmbed object from a Twitter URL. |
|
245
|
|
|
* |
|
246
|
|
|
* @param $url |
|
247
|
|
|
* @param array $query |
|
248
|
|
|
* |
|
249
|
|
|
* @return array|bool|float|int|string |
|
250
|
|
|
* @throws \Exception |
|
251
|
|
|
*/ |
|
252
|
|
|
private function oEmbed($url, $query = []) |
|
253
|
|
|
{ |
|
254
|
|
|
if (!isset($query['omit_script'])) { |
|
255
|
|
|
$query['omit_script'] = true; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$query['url'] = $url; |
|
259
|
|
|
|
|
260
|
|
|
$options = [ |
|
261
|
|
|
'query' => $query |
|
262
|
|
|
]; |
|
263
|
|
|
|
|
264
|
|
|
$oembed = Plugin::getInstance()->getCache()->get(['twitter.publish.oEmbed', $url, $options]); |
|
265
|
|
|
|
|
266
|
|
|
if (!$oembed) { |
|
267
|
|
|
$client = Craft::createGuzzleClient(); |
|
268
|
|
|
|
|
269
|
|
|
$response = $client->request('GET', 'https://publish.twitter.com/oembed', $options); |
|
270
|
|
|
|
|
271
|
|
|
$oembed = json_decode($response->getBody(), true); |
|
272
|
|
|
|
|
273
|
|
|
Plugin::getInstance()->getCache()->set(['twitter.publish.oEmbed', $url, $options], $oembed); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return $oembed; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|