-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMeta.php
271 lines (235 loc) · 7 KB
/
Meta.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
<?php
/**
* Created by PhpStorm.
* User: ptheofan
* Date: 23/10/14
* Time: 17:51
*/
namespace ptheofan\meta;
use yii;
use yii\base\Component;
use yii\helpers\Json;
class Meta extends Component
{
/**
* Default meta data values. These override any other metadata set
* @var array
*/
public $defaults = [
'og:type' => 'website',
];
/**
* Component ID representing the database
* @var string
*/
public $db = 'db';
/**
* Component ID representing the cache
* @var string
*/
public $cache = 'cache';
/**
* The Component ID for this module (used to mark cache segments)
* @var string
*/
public $componentId = 'meta';
/**
* After how long (seconds) will the routes caching expire.
* @var int
*/
public $cacheDuration = 3600;
/**
* @var string
*/
private $activeRoute = null;
/**
* @param $route
* @param array $params
* @return string
*/
protected function createRouteIndexKey($route, array $params = [])
{
array_unshift($params, $route);
return Json::encode($params);
}
/**
* @return array
*/
public function getRoutes()
{
$cache = Yii::$app->{$this->cache};
$routes = $cache->get($this->componentId . '|routes');
if ($routes)
return $routes;
/** @var models\Meta[] $models */
$models = models\Meta::find()->all(Yii::$app->{$this->db});
$routes = [];
foreach($models as $model) {
$key = $this->createRouteIndexKey($model->route, $model->params);
$routes[$key] = $model->toArray([
'id_meta', 'robots_index', 'robots_follow', 'title', 'keywords', 'description'
]);
}
$cache->set($this->componentId . '|routes', $routes, $this->cacheDuration);
return $routes;
}
/**
* @param array $route
* @return array|null
*/
public function getRouteData(array $route)
{
$routes = $this->getRoutes();
$route = Json::encode($route);
// Uncomment the next line to debug routes vs activeRoute
// yii\helpers\VarDumper::dump([$routes, $route], 10);die;
return isset($routes[$route]) ? $routes[$route] : null;
}
/**
* @param array $route
* @return array
*/
public function getMeta(array $route)
{
$rVal = $this->getRouteData($route);
if (!$rVal)
return [];
if (isset($rVal['id_meta']))
unset($rVal['id_meta']);
return $rVal;
}
/**
* @param $route
* @param array $params
*/
public function setActiveRoute($route, array $params = [])
{
array_unshift($params, trim($route, '/'));
$this->activeRoute = $params;
}
/**
* @return string
*/
public function getActiveRoute()
{
if ($this->activeRoute === null) {
$route = Yii::$app->controller->getRoute();
$params = Yii::$app->controller->actionParams;
$this->setActiveRoute($route, $params);
}
return $this->activeRoute;
}
/**
* @return int|false
*/
public function getActiveRouteId()
{
$route = $this->getRouteData($this->getActiveRoute());
return $route && isset($route['id_meta']) ? $route['id_meta'] : false;
}
/**
* Register the robots meta
* $index must be index or noindex or empty/null
* $follow must be follow or nofollow or empty/null
* @param string $index
* @param string $follow
*/
public function setRobots($index = null, $follow = null)
{
$v = [];
if (!empty($index))
$v[] = $index;
if (!empty($follow))
$v[] = $follow;
if (!empty($v))
Yii::$app->view->registerMetaTag(['name' => 'robots', 'content' => strtolower(implode(',', $v))], 'robots');
}
/**
* Register the author meta
* @param string $author
*/
public function setAuthor($author)
{
if (!empty($author))
Yii::$app->view->registerMetaTag(['name' => 'author', 'content' => $author], 'author');
}
/**
* Register Open Graph Type meta
* @param string $type
*/
public function setOpenGraphType($type)
{
if (!empty($type))
Yii::$app->view->registerMetaTag(['name' => 'og:type', 'content' => $type], 'og:type');
}
/**
* Register title meta and open graph title meta
* @param string $title
*/
public function setTitle($title)
{
if (!empty($title)) {
Yii::$app->view->registerMetaTag(['name' => 'title', 'content' => $title], 'title');
Yii::$app->view->registerMetaTag(['name' => 'og:title', 'content' => $title], 'og:title');
Yii::$app->view->title = $title;
}
}
/**
* Register description meta and open graph description meta
* @param string $description
*/
public function setDescription($description)
{
if (!empty($description)) {
Yii::$app->view->registerMetaTag(['name' => 'description', 'content' => $description], 'description');
Yii::$app->view->registerMetaTag(['name' => 'og:description', 'content' => $description], 'og:description');
}
}
/**
* Register keywords meta
* @param string $keywords
*/
public function setKeywords($keywords)
{
if (!empty($keywords))
Yii::$app->view->registerMetaTag(['name' => 'keywords', 'content' => $keywords], 'keywords');
}
/**
* Register Canonical url
* @param string $url
*/
public function setCanonical($url)
{
Yii::$app->view->registerLinkTag(['href' => $url, 'rel' => 'canonical'], 'canonical');
}
/**
* Register Open Graph Page Url
* @param string $url
*/
public function setOpenGraphUrl($url)
{
Yii::$app->view->registerMetaTag(['name' => 'og:url', 'content' => $url], 'og:url');
}
/**
* @param array $metadata
*/
public function setMeta($metadata = [])
{
// Merge route meta with passed parameter meta
$metadata = array_merge($this->getMeta($this->getActiveRoute()), $metadata);
// Override meta with the defaults via merge
$metadata = array_merge($metadata, $this->defaults);
$this->setRobots(@$metadata['robots_index'], @$metadata['robots_follow']);
$this->setAuthor(@$metadata['author']);
$this->setTitle(@$metadata['title']);
$this->setDescription(@$metadata['description']);
$this->setKeywords(@$metadata['keywords']);
$this->setOpenGraphType(@$metadata['og:type']);
$params = Yii::$app->controller->actionParams;
$params[0] = Yii::$app->controller->getRoute();
$url = Yii::$app->getUrlManager()->createAbsoluteUrl($params);
if ($url !== Yii::$app->request->absoluteUrl)
$this->setCanonical($url);
$this->setOpenGraphUrl($url);
}
}