-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.php
executable file
·276 lines (221 loc) · 7.38 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* @copyright Gabriel Alejandro Lopez Lopez
* @author Gabriel Alejandro Lopez Lopez <glpz@daxslab.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @package yii2-taggedview
*/
namespace daxslab\taggedview;
use Yii;
use yii\web\View as BaseView;
/**
* Tagged View for Yii2.
* Helps setup the standard HTML meta tags besides the ones defined by Facebook's OpenGraph
* and Twitter Cards in order to contribute to website SEO.
*
* @author Gabriel Alejandro Lopez Lopez <glpz@daxslab.com>
*/
class View extends BaseView
{
/**
* @var bool toggles if the component will register the standard HTML tags.
*/
public $registerStandardTags = true;
/**
* @var bool toggles if the component will register the Facebook's OpenGraph tags.
*/
public $registerOpenGraphTags = true;
/**
* @var bool toggles if the component will register the Twitter Cards tags.
*/
public $registerTwitterCardTags = true;
/**
* @var array specifies which properties the component should try to translate.
*/
public $translate = ['title', 'site_name', 'description', 'author', 'keywords'];
/**
* @var string specifies the current page content's author.
*/
public $author;
/**
* @var string specifies the website name
* It is set to Yii::app->name by default
* @see yii\base\Application::$name
*/
public $site_name;
/**
* @var string specifies the current page url.
* Normally the component will set this with Yii::$app->request->absoluteUrl
* @see yii\web\Request::$absoluteUrl
*/
public $url;
/**
* @var string description of the current page.
* It's normally used for the small portion of text that Google shows on the SERP,
* and Facebook or Twitter under the title of the shared content.
*/
public $description;
public $type;
public $locale;
/**
* @var string image that will be used to represent the current page.
* Facebook and Twitter attach this image to the shared content.
*/
public $image;
public $robots;
/**
* @var array keywords for the current page.
*/
public $keywords = [];
public $creator;
/**
* @var string software used to create the current page.
* By default set to our favorite one ;-)
*/
public $generator = "Yii2 PHP Framework (www.yiiframework.com)";
public $date;
public $data_type;
public $card;
/**
* @var string specifies the website name
* This one is used by Twitter. It is set to Yii::app->name by default
* @see yii\base\Application::$name
*/
public $site;
public $label1;
public $data1;
public $label2;
public $data2;
private $updated_time;
/**
* Sets some basic metatags according to app configuration if they have no been
* set in the main configuration.
*/
public function init()
{
parent::init();
if ($this->site_name == null) {
$this->site_name = Yii::$app->name;
$this->site = Yii::$app->name;
}
if($this->site == null){
$this->site = $this->site_name;
}
if ($this->url == null) {
$this->url = Yii::$app->request->absoluteUrl;
}
if ($this->date == null){
$this->date = Yii::$app->formatter->asDatetime(time());
}
$this->translateProperties();
}
/**
* @inheritdoc
*/
protected function renderHeadHtml()
{
if(!Yii::$app->request->isAjax){
if ($this->locale == null) {
$this->locale = str_replace('-', '_', Yii::$app->language);
}
if ($this->registerStandardTags) {
$this->registerStandardMetaTags();
}
if ($this->registerOpenGraphTags) {
$this->registerOpenGraphMetaTags();
}
if ($this->registerTwitterCardTags) {
$this->registerTwitterCardMetaTags();
}
}
array_multisort($this->metaTags);
return parent::renderHeadHtml();
}
/**
* Registers the standard HTML metatags.
*/
protected function registerStandardMetaTags()
{
foreach ($this->keywords as $keyword) {
$this->registerMetaTag(['name' => 'article:tag', 'content' => trim($keyword)]);
}
$this->keywords = empty($this->keywords) ? null : join(', ', $this->keywords);
foreach (['author', 'description', 'robots', 'keywords', 'generator'] as $property) {
if ($this->$property) {
$this->registerMetaTag(['name' => $property, 'content' => $this->$property]);
}
}
$this->registerLinkTag(['rel' => 'canonical', 'href' => $this->url]);
}
/**
* Registers the Facebook's OpenGraph metatags.
*/
protected function registerOpenGraphMetaTags()
{
$this->updated_time = $this->date;
foreach (['title', 'url', 'site_name', 'type', 'description', 'locale', 'updated_time'] as $property) {
if ($this->$property) {
$this->registerMetaTag(['property' => "og:" . $property, 'content' => $this->$property]);
}
}
if ($this->image !== null) {
if (is_array($this->image)) {
foreach ($this->image as $key => $value) {
$this->registerMetaTag(['property' => 'og:image', 'content' => $value], 'og:image' . $key);
}
} else {
$this->registerMetaTag(['property' => 'og:image', 'content' => $this->image], 'og:image');
}
}
}
/**
* Registers the Twitter Cards metatags.
*/
protected function registerTwitterCardMetaTags()
{
foreach ([
'card',
'title',
'url',
'creator',
'site',
'type',
'description',
'label1',
'data1',
'label2',
'data2'
] as $property) {
if ($this->$property) {
$this->registerMetaTag(['name' => "twitter:" . $property, 'content' => $this->$property]);
}
}
if ($this->image !== null) {
if (is_array($this->image)) {
$this->registerMetaTag(['name' => 'twitter:image', 'content' => $this->image[0]], 'twitter:image');
} else {
$this->registerMetaTag(['name' => 'twitter:image', 'content' => $this->image], 'twitter:image');
}
}
}
/**
* Executes the translation tool to find out if there is a translation registered for
* the original property value
*/
protected function translateProperties()
{
foreach ($this->translate as $property) {
if ($this->$property != null) {
if (is_array($this->$property)) {
$the_array = $this->$property;
foreach ($the_array as $i => $word) {
$the_array[$i] = Yii::t('app', $word);
}
$this->$property = $the_array;
} else {
$this->$property = Yii::t('app', $this->$property);
}
}
}
}
}