1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of sonata-project. |
5
|
|
|
* |
6
|
|
|
* (c) 2010 Thomas Rabaix |
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\Twig\Extension; |
13
|
|
|
|
14
|
|
|
use Sonata\SeoBundle\Seo\SeoPageInterface; |
15
|
|
|
|
16
|
|
|
class SeoExtension extends \Twig_Extension |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var SeoPageInterface |
20
|
|
|
*/ |
21
|
|
|
protected $page; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $encoding; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param SeoPageInterface $page |
30
|
|
|
* @param string $encoding |
31
|
|
|
*/ |
32
|
|
|
public function __construct(SeoPageInterface $page, $encoding) |
33
|
|
|
{ |
34
|
|
|
$this->page = $page; |
35
|
|
|
$this->encoding = $encoding; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function getFunctions() |
42
|
|
|
{ |
43
|
|
|
return array( |
44
|
|
|
new \Twig_SimpleFunction('sonata_seo_title', array($this, 'getTitle'), array('is_safe' => array('html'))), |
45
|
|
|
new \Twig_SimpleFunction('sonata_seo_metadatas', array($this, 'getMetadatas'), array('is_safe' => array('html'))), |
46
|
|
|
new \Twig_SimpleFunction('sonata_seo_html_attributes', array($this, 'getHtmlAttributes'), array('is_safe' => array('html'))), |
47
|
|
|
new \Twig_SimpleFunction('sonata_seo_head_attributes', array($this, 'getHeadAttributes'), array('is_safe' => array('html'))), |
48
|
|
|
new \Twig_SimpleFunction('sonata_seo_link_canonical', array($this, 'getLinkCanonical'), array('is_safe' => array('html'))), |
49
|
|
|
new \Twig_SimpleFunction('sonata_seo_lang_alternates', array($this, 'getLangAlternates'), array('is_safe' => array('html'))), |
50
|
|
|
new \Twig_SimpleFunction('sonata_seo_oembed_links', array($this, 'getOembedLinks'), array('is_safe' => array('html'))), |
51
|
|
|
new \Twig_SimpleFunction('sonata_seo_links', array($this, 'getLinks'), array('is_safe' => array('html'))), |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getName() |
59
|
|
|
{ |
60
|
|
|
return 'sonata_seo'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @deprecated Deprecated as of 1.2, echo the return value of getTitle() instead. |
65
|
|
|
*/ |
66
|
|
|
public function renderTitle() |
67
|
|
|
{ |
68
|
|
|
echo $this->getTitle(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getTitle() |
75
|
|
|
{ |
76
|
|
|
return sprintf('<title>%s</title>', strip_tags($this->page->getTitle())); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @deprecated Deprecated as of 1.2, echo the return value of getMetadatas() instead. |
81
|
|
|
*/ |
82
|
|
|
public function renderMetadatas() |
83
|
|
|
{ |
84
|
|
|
echo $this->getMetadatas(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getMetadatas() |
91
|
|
|
{ |
92
|
|
|
$html = ''; |
93
|
|
|
foreach ($this->page->getMetas() as $type => $metas) { |
94
|
|
|
foreach ((array) $metas as $name => $meta) { |
95
|
|
|
list($content, $extras) = $meta; |
|
|
|
|
96
|
|
|
|
97
|
|
|
if (!empty($content)) { |
98
|
|
|
$html .= sprintf("<meta %s=\"%s\" content=\"%s\" />\n", |
99
|
|
|
$type, |
100
|
|
|
$this->normalize($name), |
101
|
|
|
$this->normalize($content) |
102
|
|
|
); |
103
|
|
|
} else { |
104
|
|
|
$html .= sprintf("<meta %s=\"%s\" />\n", |
105
|
|
|
$type, |
106
|
|
|
$this->normalize($name) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $html; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @deprecated Deprecated as of 1.2, echo the return value of getHtmlAttributes() instead. |
117
|
|
|
*/ |
118
|
|
|
public function renderHtmlAttributes() |
119
|
|
|
{ |
120
|
|
|
echo $this->getHtmlAttributes(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getHtmlAttributes() |
127
|
|
|
{ |
128
|
|
|
$attributes = ''; |
129
|
|
|
foreach ($this->page->getHtmlAttributes() as $name => $value) { |
130
|
|
|
$attributes .= sprintf('%s="%s" ', $name, $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return rtrim($attributes); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @deprecated Deprecated as of 1.2, echo the return value of getHeadAttributes() instead. |
138
|
|
|
*/ |
139
|
|
|
public function renderHeadAttributes() |
140
|
|
|
{ |
141
|
|
|
echo $this->getHeadAttributes(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getHeadAttributes() |
148
|
|
|
{ |
149
|
|
|
$attributes = ''; |
150
|
|
|
foreach ($this->page->getHeadAttributes() as $name => $value) { |
151
|
|
|
$attributes .= sprintf('%s="%s" ', $name, $value); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return rtrim($attributes); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @deprecated Deprecated as of 1.2, echo the return value of getLinkCanonical() instead. |
159
|
|
|
*/ |
160
|
|
|
public function renderLinkCanonical() |
161
|
|
|
{ |
162
|
|
|
echo $this->getLinkCanonical(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getLinkCanonical() |
169
|
|
|
{ |
170
|
|
|
if ($this->page->getLinkCanonical()) { |
171
|
|
|
return sprintf("<link rel=\"canonical\" href=\"%s\"/>\n", $this->page->getLinkCanonical()); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return links HTML tags. |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getLinks() |
181
|
|
|
{ |
182
|
|
|
$html = ''; |
183
|
|
|
$exclude = array('alternate', 'canonical'); |
184
|
|
|
|
185
|
|
|
foreach ($this->page->getLinks() as $rel => $attributes) { |
186
|
|
|
// Prevent from duplicate links |
187
|
|
|
if (in_array($rel, $exclude)) { |
188
|
|
|
continue; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$html .= sprintf('<link rel="%s"', $rel); |
192
|
|
|
|
193
|
|
|
foreach ($attributes as $key => $attribute) { |
194
|
|
|
$attribute = $this->normalize($attribute); |
195
|
|
|
$html .= sprintf(' %s="%s"', $key, $attribute); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$html .= sprintf(" />\n"); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $html; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @deprecated Deprecated as of 1.2, echo the return value of getLangAlternates() instead. |
206
|
|
|
*/ |
207
|
|
|
public function renderLangAlternates() |
208
|
|
|
{ |
209
|
|
|
echo $this->getLangAlternates(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function getLangAlternates() |
216
|
|
|
{ |
217
|
|
|
$html = ''; |
218
|
|
|
foreach ($this->page->getLangAlternates() as $href => $hrefLang) { |
219
|
|
|
$html .= sprintf("<link rel=\"alternate\" href=\"%s\" hreflang=\"%s\"/>\n", $href, $hrefLang); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $html; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public function getOembedLinks() |
229
|
|
|
{ |
230
|
|
|
$html = ''; |
231
|
|
|
foreach ($this->page->getOEmbedLinks() as $title => $link) { |
232
|
|
|
$html .= sprintf("<link rel=\"alternate\" type=\"application/json+oembed\" href=\"%s\" title=\"%s\" />\n", $link, $title); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $html; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $string |
240
|
|
|
* |
241
|
|
|
* @return mixed |
242
|
|
|
*/ |
243
|
|
|
private function normalize($string) |
244
|
|
|
{ |
245
|
|
|
return htmlentities(strip_tags($string), ENT_COMPAT, $this->encoding); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.