|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\SeoBundle\Block\Social; |
|
13
|
|
|
|
|
14
|
|
|
use Guzzle\Http\Exception\CurlException; |
|
15
|
|
|
use Http\Client\HttpClient; |
|
16
|
|
|
use Http\Message\MessageFactory; |
|
17
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
|
18
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
19
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
|
20
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
|
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This block service allows to embed a tweet by requesting the Twitter API. |
|
27
|
|
|
* |
|
28
|
|
|
* @see https://dev.twitter.com/docs/api/1/get/statuses/oembed |
|
29
|
|
|
* |
|
30
|
|
|
* @author Hugo Briand <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class TwitterEmbedTweetBlockService extends BaseTwitterButtonBlockService |
|
33
|
|
|
{ |
|
34
|
|
|
const TWITTER_OEMBED_URI = 'https://api.twitter.com/1/statuses/oembed.json'; |
|
35
|
|
|
const TWEET_URL_PATTERN = '%^(https://)(www.)?(twitter.com/)(.*)(/status)(es)?(/)([0-9]*)$%i'; |
|
36
|
|
|
const TWEET_ID_PATTERN = '%^([0-9]*)$%'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var HttpClient |
|
40
|
|
|
*/ |
|
41
|
|
|
private $httpClient; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var MessageFactory |
|
45
|
|
|
*/ |
|
46
|
|
|
private $messageFactory; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* @param EngineInterface $templating |
|
51
|
|
|
* @param HttpClient $httpClient |
|
52
|
|
|
* @param MessageFactory $messageFactory |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct($name, EngineInterface $templating, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
|
55
|
|
|
{ |
|
56
|
|
|
parent::__construct($name, $templating); |
|
57
|
|
|
|
|
58
|
|
|
$this->httpClient = $httpClient; |
|
59
|
|
|
$this->messageFactory = $messageFactory; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->renderResponse($blockContext->getTemplate(), array( |
|
68
|
|
|
'block' => $blockContext->getBlock(), |
|
69
|
|
|
'tweet' => $this->loadTweet($blockContext), |
|
70
|
|
|
), $response); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function configureSettings(OptionsResolver $resolver) |
|
77
|
|
|
{ |
|
78
|
|
|
$resolver->setDefaults(array( |
|
79
|
|
|
'template' => 'SonataSeoBundle:Block:block_twitter_embed.html.twig', |
|
80
|
|
|
'tweet' => '', |
|
81
|
|
|
'maxwidth' => null, |
|
82
|
|
|
'hide_media' => false, |
|
83
|
|
|
'hide_thread' => false, |
|
84
|
|
|
'omit_script' => false, |
|
85
|
|
|
'align' => 'none', |
|
86
|
|
|
'related' => null, |
|
87
|
|
|
'lang' => null, |
|
88
|
|
|
)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function buildEditForm(FormMapper $form, BlockInterface $block) |
|
95
|
|
|
{ |
|
96
|
|
|
$form->add('settings', 'sonata_type_immutable_array', array( |
|
97
|
|
|
'keys' => array( |
|
98
|
|
|
array('tweet', 'textarea', array( |
|
99
|
|
|
'required' => true, |
|
100
|
|
|
'label' => 'form.label_tweet', |
|
101
|
|
|
'help_block' => 'form.help_tweet', |
|
102
|
|
|
)), |
|
103
|
|
|
array('maxwidth', 'integer', array( |
|
104
|
|
|
'required' => false, |
|
105
|
|
|
'label' => 'form.label_maxwidth', |
|
106
|
|
|
'help_block' => 'form.help_maxwidth', |
|
107
|
|
|
)), |
|
108
|
|
|
array('hide_media', 'checkbox', array( |
|
109
|
|
|
'required' => false, |
|
110
|
|
|
'label' => 'form.label_hide_media', |
|
111
|
|
|
'help_block' => 'form.help_hide_media', |
|
112
|
|
|
)), |
|
113
|
|
|
array('hide_thread', 'checkbox', array( |
|
114
|
|
|
'required' => false, |
|
115
|
|
|
'label' => 'form.label_hide_thread', |
|
116
|
|
|
'help_block' => 'form.help_hide_thread', |
|
117
|
|
|
)), |
|
118
|
|
|
array('omit_script', 'checkbox', array( |
|
119
|
|
|
'required' => false, |
|
120
|
|
|
'label' => 'form.label_omit_script', |
|
121
|
|
|
'help_block' => 'form.help_omit_script', |
|
122
|
|
|
)), |
|
123
|
|
|
array('align', 'choice', array( |
|
124
|
|
|
'required' => false, |
|
125
|
|
|
'choices' => array( |
|
126
|
|
|
'left' => 'form.label_align_left', |
|
127
|
|
|
'right' => 'form.label_align_right', |
|
128
|
|
|
'center' => 'form.label_align_center', |
|
129
|
|
|
'none' => 'form.label_align_none', |
|
130
|
|
|
), |
|
131
|
|
|
'label' => 'form.label_align', |
|
132
|
|
|
)), |
|
133
|
|
|
array('related', 'text', array( |
|
134
|
|
|
'required' => false, |
|
135
|
|
|
'label' => 'form.label_related', |
|
136
|
|
|
'help_block' => 'form.help_related', |
|
137
|
|
|
)), |
|
138
|
|
|
array('lang', 'choice', array( |
|
139
|
|
|
'required' => true, |
|
140
|
|
|
'choices' => $this->languageList, |
|
141
|
|
|
'label' => 'form.label_lang', |
|
142
|
|
|
)), |
|
143
|
|
|
), |
|
144
|
|
|
'translation_domain' => 'SonataSeoBundle', |
|
145
|
|
|
)); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getBlockMetadata($code = null) |
|
152
|
|
|
{ |
|
153
|
|
|
return new Metadata($this->getName(), (!is_null($code) ? $code : $this->getName()), false, 'SonataSeoBundle', array( |
|
154
|
|
|
'class' => 'fa fa-twitter', |
|
155
|
|
|
)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns supported API parameters from settings. |
|
160
|
|
|
* |
|
161
|
|
|
* @return array |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getSupportedApiParams() |
|
164
|
|
|
{ |
|
165
|
|
|
return array( |
|
166
|
|
|
'maxwidth', |
|
167
|
|
|
'hide_media', |
|
168
|
|
|
'hide_thread', |
|
169
|
|
|
'omit_script', |
|
170
|
|
|
'align', |
|
171
|
|
|
'related', |
|
172
|
|
|
'lang', |
|
173
|
|
|
'url', |
|
174
|
|
|
'id', |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Builds the API query URI based on $settings. |
|
180
|
|
|
* |
|
181
|
|
|
* @param bool $uriMatched |
|
182
|
|
|
* @param array $settings |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function buildUri($uriMatched, array $settings) |
|
187
|
|
|
{ |
|
188
|
|
|
$apiParams = $settings; |
|
189
|
|
|
$supportedParams = $this->getSupportedApiParams(); |
|
190
|
|
|
|
|
191
|
|
|
if ($uriMatched) { |
|
192
|
|
|
// We matched the uri |
|
193
|
|
|
$apiParams['url'] = $settings['tweet']; |
|
194
|
|
|
} else { |
|
195
|
|
|
$apiParams['id'] = $settings['tweet']; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
unset($apiParams['tweet']); |
|
199
|
|
|
|
|
200
|
|
|
$parameters = array(); |
|
201
|
|
|
foreach ($apiParams as $key => $value) { |
|
202
|
|
|
if ($value && in_array($key, $supportedParams)) { |
|
203
|
|
|
$parameters[] = $key.'='.$value; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return sprintf('%s?%s', self::TWITTER_OEMBED_URI, implode('&', $parameters)); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Loads twitter tweet. |
|
212
|
|
|
* |
|
213
|
|
|
* @param BlockContextInterface $blockContext |
|
214
|
|
|
* |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
|
|
final protected function loadTweet(BlockContextInterface $blockContext) |
|
218
|
|
|
{ |
|
219
|
|
|
if (($uriMatched = preg_match(self::TWEET_URL_PATTERN, $blockContext->getSetting('tweet'))) || preg_match(self::TWEET_ID_PATTERN, $blockContext->getSetting('tweet'))) { |
|
220
|
|
|
if (null != $this->httpClient && null != $this->messageFactory) { |
|
221
|
|
|
$response = $this->httpClient->sendRequest( |
|
222
|
|
|
$this->messageFactory->createRequest('GET', $this->buildUri($uriMatched, $blockContext->getSettings())) |
|
|
|
|
|
|
223
|
|
|
); |
|
224
|
|
|
|
|
225
|
|
|
$apiTweet = json_decode($response->getBody(), true); |
|
226
|
|
|
|
|
227
|
|
|
return $apiTweet['html']; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// NEXT_MAJOR: Remove the old guzzle implementation |
|
231
|
|
|
|
|
232
|
|
|
// We matched an URL or an ID, we'll need to ask the API |
|
233
|
|
|
if (class_exists('Guzzle\Http\Client') === false) { |
|
234
|
|
|
throw new \RuntimeException( |
|
235
|
|
|
'The guzzle http client library is required to call the Twitter API. Make sure to add guzzle/guzzle to your composer.json.' |
|
236
|
|
|
); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
@trigger_error('The direct Guzzle implementation is deprecated since 2.x and will be removed with the next major release.', E_USER_DEPRECATED); |
|
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
// TODO cache API result |
|
242
|
|
|
$client = new \Guzzle\Http\Client(); |
|
243
|
|
|
$client->setConfig(array('curl.options' => array(CURLOPT_CONNECTTIMEOUT_MS => 1000))); |
|
244
|
|
|
|
|
245
|
|
|
try { |
|
246
|
|
|
$request = $client->get($this->buildUri($uriMatched, $blockContext->getSettings())); |
|
|
|
|
|
|
247
|
|
|
$apiTweet = json_decode($request->send()->getBody(true), true); |
|
248
|
|
|
|
|
249
|
|
|
return $apiTweet['html']; |
|
250
|
|
|
} catch (CurlException $e) { |
|
251
|
|
|
// log error |
|
252
|
|
|
} |
|
253
|
|
|
// END NEXT_MAJOR |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: