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 Sonata\AdminBundle\Form\FormMapper; |
16
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
17
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
18
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class TwitterEmbedTweetBlockService. |
24
|
|
|
* |
25
|
|
|
* This block service allows to embed a tweet by requesting the Twitter API. |
26
|
|
|
* |
27
|
|
|
* @see https://dev.twitter.com/docs/api/1/get/statuses/oembed |
28
|
|
|
* |
29
|
|
|
* @author Hugo Briand <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class TwitterEmbedTweetBlockService extends BaseTwitterButtonBlockService |
32
|
|
|
{ |
33
|
|
|
const TWITTER_OEMBED_URI = 'https://api.twitter.com/1/statuses/oembed.json'; |
34
|
|
|
const TWEET_URL_PATTERN = '%^(https://)(www.)?(twitter.com/)(.*)(/status)(es)?(/)([0-9]*)$%i'; |
35
|
|
|
const TWEET_ID_PATTERN = '%^([0-9]*)$%'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
41
|
|
|
{ |
42
|
|
|
$tweet = $blockContext->getSetting('tweet'); |
43
|
|
|
|
44
|
|
|
if (($uriMatched = preg_match(self::TWEET_URL_PATTERN, $tweet)) |
45
|
|
|
|| preg_match(self::TWEET_ID_PATTERN, $tweet)) { |
46
|
|
|
// We matched an URL or an ID, we'll need to ask the API |
47
|
|
|
if (class_exists('Guzzle\Http\Client') === false) { |
48
|
|
|
throw new \RuntimeException('The guzzle http client library is required to call the Twitter API. Make sure to add guzzle/guzzle to your composer.json.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// TODO cache API result |
52
|
|
|
$client = new \Guzzle\Http\Client(); |
53
|
|
|
$client->setConfig(array('curl.options' => array(CURLOPT_CONNECTTIMEOUT_MS => 1000))); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$request = $client->get($this->buildUri($uriMatched, $blockContext->getSettings())); |
|
|
|
|
57
|
|
|
$apiTweet = json_decode($request->send()->getBody(true), true); |
58
|
|
|
|
59
|
|
|
$tweet = $apiTweet['html']; |
60
|
|
|
} catch (CurlException $e) { |
61
|
|
|
// log error |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->renderResponse($blockContext->getTemplate(), array( |
66
|
|
|
'block' => $blockContext->getBlock(), |
67
|
|
|
'tweet' => $tweet, |
68
|
|
|
), $response); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function configureSettings(OptionsResolver $resolver) |
75
|
|
|
{ |
76
|
|
|
$resolver->setDefaults(array( |
77
|
|
|
'template' => 'SonataSeoBundle:Block:block_twitter_embed.html.twig', |
78
|
|
|
'tweet' => '', |
79
|
|
|
'maxwidth' => null, |
80
|
|
|
'hide_media' => false, |
81
|
|
|
'hide_thread' => false, |
82
|
|
|
'omit_script' => false, |
83
|
|
|
'align' => 'none', |
84
|
|
|
'related' => null, |
85
|
|
|
'lang' => $this->languageList, |
86
|
|
|
)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function buildEditForm(FormMapper $form, BlockInterface $block) |
93
|
|
|
{ |
94
|
|
|
$form->add('settings', 'sonata_type_immutable_array', array( |
95
|
|
|
'keys' => array( |
96
|
|
|
array('tweet', 'textarea', array( |
97
|
|
|
'required' => true, |
98
|
|
|
'label' => 'form.label_tweet', |
99
|
|
|
'help_block' => 'form.help_tweet', |
100
|
|
|
)), |
101
|
|
|
array('maxwidth', 'integer', array( |
102
|
|
|
'required' => false, |
103
|
|
|
'label' => 'form.label_maxwidth', |
104
|
|
|
'help_block' => 'form.help_maxwidth', |
105
|
|
|
)), |
106
|
|
|
array('hide_media', 'checkbox', array( |
107
|
|
|
'required' => false, |
108
|
|
|
'label' => 'form.label_hide_media', |
109
|
|
|
'help_block' => 'form.help_hide_media', |
110
|
|
|
)), |
111
|
|
|
array('hide_thread', 'checkbox', array( |
112
|
|
|
'required' => false, |
113
|
|
|
'label' => 'form.label_hide_thread', |
114
|
|
|
'help_block' => 'form.help_hide_thread', |
115
|
|
|
)), |
116
|
|
|
array('omit_script', 'checkbox', array( |
117
|
|
|
'required' => false, |
118
|
|
|
'label' => 'form.label_omit_script', |
119
|
|
|
'help_block' => 'form.help_omit_script', |
120
|
|
|
)), |
121
|
|
|
array('align', 'choice', array( |
122
|
|
|
'required' => false, |
123
|
|
|
'choices' => array( |
124
|
|
|
'left' => 'form.label_align_left', |
125
|
|
|
'right' => 'form.label_align_right', |
126
|
|
|
'center' => 'form.label_align_center', |
127
|
|
|
'none' => 'form.label_align_none', |
128
|
|
|
), |
129
|
|
|
'label' => 'form.label_align', |
130
|
|
|
)), |
131
|
|
|
array('related', 'text', array( |
132
|
|
|
'required' => false, |
133
|
|
|
'label' => 'form.label_related', |
134
|
|
|
'help_block' => 'form.help_related', |
135
|
|
|
)), |
136
|
|
|
array('lang', 'choice', array( |
137
|
|
|
'required' => true, |
138
|
|
|
'choices' => $this->languageList, |
139
|
|
|
'label' => 'form.label_lang', |
140
|
|
|
)), |
141
|
|
|
), |
142
|
|
|
'translation_domain' => 'SonataSeoBundle', |
143
|
|
|
)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function getBlockMetadata($code = null) |
150
|
|
|
{ |
151
|
|
|
return new Metadata($this->getName(), (!is_null($code) ? $code : $this->getName()), false, 'SonataSeoBundle', array( |
152
|
|
|
'class' => 'fa fa-twitter', |
153
|
|
|
)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns supported API parameters from settings. |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
protected function getSupportedApiParams() |
162
|
|
|
{ |
163
|
|
|
return array( |
164
|
|
|
'maxwidth', |
165
|
|
|
'hide_media', |
166
|
|
|
'hide_thread', |
167
|
|
|
'omit_script', |
168
|
|
|
'align', |
169
|
|
|
'related', |
170
|
|
|
'lang', |
171
|
|
|
'url', |
172
|
|
|
'id', |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Builds the API query URI based on $settings. |
178
|
|
|
* |
179
|
|
|
* @param bool $uriMatched |
180
|
|
|
* @param array $settings |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function buildUri($uriMatched, array $settings) |
185
|
|
|
{ |
186
|
|
|
$apiParams = $settings; |
187
|
|
|
$supportedParams = $this->getSupportedApiParams(); |
188
|
|
|
|
189
|
|
|
if ($uriMatched) { |
190
|
|
|
// We matched the uri |
191
|
|
|
$apiParams['url'] = $settings['tweet']; |
192
|
|
|
} else { |
193
|
|
|
$apiParams['id'] = $settings['tweet']; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
unset($apiParams['tweet']); |
197
|
|
|
|
198
|
|
|
$parameters = array(); |
199
|
|
|
foreach ($apiParams as $key => $value) { |
200
|
|
|
if ($value && in_array($key, $supportedParams)) { |
201
|
|
|
$parameters[] = $key.'='.$value; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return sprintf('%s?%s', self::TWITTER_OEMBED_URI, implode('&', $parameters)); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
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: