1
|
|
|
<?php namespace Arcanedev\SeoHelper\Entities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\SeoHelper\Contracts\Entities\MiscTags as MiscTagsContract; |
4
|
|
|
use Arcanedev\Support\Traits\Configurable; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class MiscTags |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\SeoHelper\Entities |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class MiscTags implements MiscTagsContract |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Traits |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Configurable; |
20
|
|
|
|
21
|
|
|
/* ----------------------------------------------------------------- |
22
|
|
|
| Properties |
23
|
|
|
| ----------------------------------------------------------------- |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Current URL. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $currentUrl = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Meta collection. |
35
|
|
|
* |
36
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\MetaCollection |
37
|
|
|
*/ |
38
|
|
|
protected $metas; |
39
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
41
|
|
|
| Constructor |
42
|
|
|
| ----------------------------------------------------------------- |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Make MiscTags instance. |
47
|
|
|
* |
48
|
|
|
* @param array $configs |
49
|
|
|
*/ |
50
|
114 |
|
public function __construct(array $configs = []) |
51
|
|
|
{ |
52
|
114 |
|
$this->setConfigs($configs); |
53
|
114 |
|
$this->metas = new MetaCollection; |
54
|
|
|
|
55
|
114 |
|
$this->init(); |
56
|
114 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Start the engine. |
60
|
|
|
*/ |
61
|
114 |
|
private function init() |
62
|
|
|
{ |
63
|
114 |
|
$this->addCanonical(); |
64
|
114 |
|
$this->addRobotsMeta(); |
65
|
114 |
|
$this->addMany($this->getConfig('default', [])); |
66
|
114 |
|
} |
67
|
|
|
|
68
|
|
|
/* ----------------------------------------------------------------- |
69
|
|
|
| Getters & Setters |
70
|
|
|
| ----------------------------------------------------------------- |
71
|
|
|
*/ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the current URL. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
114 |
|
public function getUrl() |
79
|
|
|
{ |
80
|
114 |
|
return $this->currentUrl; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the current URL. |
85
|
|
|
* |
86
|
|
|
* @param string $url |
87
|
|
|
* |
88
|
|
|
* @return \Arcanedev\SeoHelper\Entities\MiscTags |
89
|
|
|
*/ |
90
|
51 |
|
public function setUrl($url) |
91
|
|
|
{ |
92
|
51 |
|
$this->currentUrl = $url; |
93
|
51 |
|
$this->addCanonical(); |
94
|
|
|
|
95
|
51 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get all the metas collection. |
100
|
|
|
* |
101
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\Entities\MetaCollection |
102
|
|
|
*/ |
103
|
3 |
|
public function all() |
104
|
|
|
{ |
105
|
3 |
|
return $this->metas; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/* ----------------------------------------------------------------- |
109
|
|
|
| Main Methods |
110
|
|
|
| ----------------------------------------------------------------- |
111
|
|
|
*/ |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Make MiscTags instance. |
115
|
|
|
* |
116
|
|
|
* @param array $defaults |
117
|
|
|
* |
118
|
|
|
* @return \Arcanedev\SeoHelper\Entities\MiscTags |
119
|
|
|
*/ |
120
|
3 |
|
public static function make(array $defaults = []) |
121
|
|
|
{ |
122
|
3 |
|
return new self(['default' => $defaults]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Add a meta tag. |
127
|
|
|
* |
128
|
|
|
* @param string $name |
129
|
|
|
* @param string $content |
130
|
|
|
* |
131
|
|
|
* @return \Arcanedev\SeoHelper\Entities\MiscTags |
132
|
|
|
*/ |
133
|
114 |
|
public function add($name, $content) |
134
|
|
|
{ |
135
|
114 |
|
$this->metas->add($name, $content); |
136
|
|
|
|
137
|
114 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add many meta tags. |
142
|
|
|
* |
143
|
|
|
* @param array $metas |
144
|
|
|
* |
145
|
|
|
* @return \Arcanedev\SeoHelper\Entities\MiscTags |
146
|
|
|
*/ |
147
|
114 |
|
public function addMany(array $metas) |
148
|
|
|
{ |
149
|
114 |
|
$this->metas->addMany($metas); |
150
|
|
|
|
151
|
114 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Remove a meta from the meta collection by key. |
156
|
|
|
* |
157
|
|
|
* @param array|string $names |
158
|
|
|
* |
159
|
|
|
* @return \Arcanedev\SeoHelper\Entities\MiscTags |
160
|
|
|
*/ |
161
|
9 |
|
public function remove($names) |
162
|
|
|
{ |
163
|
9 |
|
$this->metas->remove($names); |
164
|
|
|
|
165
|
9 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Reset the meta collection. |
170
|
|
|
* |
171
|
|
|
* @return \Arcanedev\SeoHelper\Entities\MiscTags |
172
|
|
|
*/ |
173
|
6 |
|
public function reset() |
174
|
|
|
{ |
175
|
6 |
|
$this->metas->reset(); |
176
|
|
|
|
177
|
6 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Render the tag. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
87 |
|
public function render() |
186
|
|
|
{ |
187
|
87 |
|
return $this->metas->render(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Render the tag. |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
12 |
|
public function __toString() |
196
|
|
|
{ |
197
|
12 |
|
return $this->render(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/* ----------------------------------------------------------------- |
201
|
|
|
| Check Methods |
202
|
|
|
| ----------------------------------------------------------------- |
203
|
|
|
*/ |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Check if has the current URL. |
207
|
|
|
* |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
114 |
|
private function hasUrl() |
211
|
|
|
{ |
212
|
114 |
|
return ! empty($this->getUrl()); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Check if canonical is enabled. |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
114 |
|
private function isCanonicalEnabled() |
221
|
|
|
{ |
222
|
114 |
|
return (bool) $this->getConfig('canonical', false); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Check if blocking robots is enabled. |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
114 |
|
private function isRobotsEnabled() |
231
|
|
|
{ |
232
|
114 |
|
return (bool) $this->getConfig('robots', false); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/* ----------------------------------------------------------------- |
236
|
|
|
| Other Methods |
237
|
|
|
| ----------------------------------------------------------------- |
238
|
|
|
*/ |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Add the robots meta. |
242
|
|
|
* |
243
|
|
|
* @return \Arcanedev\SeoHelper\Entities\MiscTags |
244
|
|
|
*/ |
245
|
114 |
|
private function addRobotsMeta() |
246
|
|
|
{ |
247
|
114 |
|
if ($this->isRobotsEnabled()) |
248
|
114 |
|
$this->add('robots', 'noindex, nofollow'); |
249
|
|
|
|
250
|
114 |
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Add the canonical link. |
255
|
|
|
* |
256
|
|
|
* @return \Arcanedev\SeoHelper\Entities\MiscTags |
257
|
|
|
*/ |
258
|
114 |
|
private function addCanonical() |
259
|
|
|
{ |
260
|
114 |
|
if ($this->isCanonicalEnabled() && $this->hasUrl()) |
261
|
51 |
|
$this->add('canonical', $this->currentUrl); |
262
|
|
|
|
263
|
114 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|