1
|
|
|
<?php namespace Arcanedev\SeoHelper; |
2
|
|
|
|
3
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoHelper as SeoHelperContract; |
4
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoMeta as SeoMetaContract; |
5
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoOpenGraph as SeoOpenGraphContract; |
6
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoTwitter as SeoTwitterContract; |
7
|
|
|
use Illuminate\Support\HtmlString; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SeoHelper |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\SeoHelper |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class SeoHelper implements SeoHelperContract |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The SeoMeta instance. |
24
|
|
|
* |
25
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\SeoMeta |
26
|
|
|
*/ |
27
|
|
|
private $seoMeta; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The SeoOpenGraph instance. |
31
|
|
|
* |
32
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\SeoOpenGraph |
33
|
|
|
*/ |
34
|
|
|
private $seoOpenGraph; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The SeoTwitter instance. |
38
|
|
|
* |
39
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\SeoTwitter |
40
|
|
|
*/ |
41
|
|
|
private $seoTwitter; |
42
|
|
|
|
43
|
|
|
/* ----------------------------------------------------------------- |
44
|
|
|
| Constructor |
45
|
|
|
| ----------------------------------------------------------------- |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Make SeoHelper instance. |
50
|
|
|
* |
51
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoMeta $seoMeta |
52
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoOpenGraph $seoOpenGraph |
53
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoTwitter $seoTwitter |
54
|
|
|
*/ |
55
|
84 |
|
public function __construct( |
56
|
|
|
SeoMetaContract $seoMeta, |
57
|
|
|
SeoOpenGraphContract $seoOpenGraph, |
58
|
|
|
SeoTwitterContract $seoTwitter |
59
|
|
|
) { |
60
|
84 |
|
$this->setSeoMeta($seoMeta); |
61
|
84 |
|
$this->setSeoOpenGraph($seoOpenGraph); |
62
|
84 |
|
$this->setSeoTwitter($seoTwitter); |
63
|
84 |
|
} |
64
|
|
|
|
65
|
|
|
/* ----------------------------------------------------------------- |
66
|
|
|
| Getters & Setters |
67
|
|
|
| ----------------------------------------------------------------- |
68
|
|
|
*/ |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get SeoMeta instance. |
72
|
|
|
* |
73
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoMeta |
74
|
|
|
*/ |
75
|
60 |
|
public function meta() |
76
|
|
|
{ |
77
|
60 |
|
return $this->seoMeta; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set SeoMeta instance. |
82
|
|
|
* |
83
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoMeta $seoMeta |
84
|
|
|
* |
85
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
86
|
|
|
*/ |
87
|
84 |
|
public function setSeoMeta(SeoMetaContract $seoMeta) |
88
|
|
|
{ |
89
|
84 |
|
$this->seoMeta = $seoMeta; |
90
|
|
|
|
91
|
84 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get SeoOpenGraph instance. |
96
|
|
|
* |
97
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoOpenGraph |
98
|
|
|
*/ |
99
|
60 |
|
public function openGraph() |
100
|
|
|
{ |
101
|
60 |
|
return $this->seoOpenGraph; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get SeoOpenGraph instance (alias). |
106
|
|
|
* |
107
|
|
|
* @see openGraph() |
108
|
|
|
* |
109
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoOpenGraph |
110
|
|
|
*/ |
111
|
4 |
|
public function og() |
112
|
|
|
{ |
113
|
4 |
|
return $this->openGraph(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get SeoOpenGraph instance. |
118
|
|
|
* |
119
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoOpenGraph $seoOpenGraph |
120
|
|
|
* |
121
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
122
|
|
|
*/ |
123
|
84 |
|
public function setSeoOpenGraph(SeoOpenGraphContract $seoOpenGraph) |
124
|
|
|
{ |
125
|
84 |
|
$this->seoOpenGraph = $seoOpenGraph; |
126
|
|
|
|
127
|
84 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get SeoTwitter instance. |
132
|
|
|
* |
133
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoTwitter |
134
|
|
|
*/ |
135
|
56 |
|
public function twitter() |
136
|
|
|
{ |
137
|
56 |
|
return $this->seoTwitter; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set SeoTwitter instance. |
142
|
|
|
* |
143
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoTwitter $seoTwitter |
144
|
|
|
* |
145
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
146
|
|
|
*/ |
147
|
84 |
|
public function setSeoTwitter(SeoTwitterContract $seoTwitter) |
148
|
|
|
{ |
149
|
84 |
|
$this->seoTwitter = $seoTwitter; |
150
|
|
|
|
151
|
84 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set title. |
156
|
|
|
* |
157
|
|
|
* @param string $title |
158
|
|
|
* @param string|null $siteName |
159
|
|
|
* @param string|null $separator |
160
|
|
|
* |
161
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
162
|
|
|
*/ |
163
|
16 |
|
public function setTitle($title, $siteName = null, $separator = null) |
164
|
|
|
{ |
165
|
16 |
|
$this->meta()->setTitle($title, null, $separator); |
166
|
16 |
|
$this->openGraph()->setTitle($title); |
167
|
16 |
|
$this->twitter()->setTitle($title); |
168
|
|
|
|
169
|
16 |
|
return $this->setSiteName($siteName); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the site name. |
174
|
|
|
* |
175
|
|
|
* @param string $siteName |
176
|
|
|
* |
177
|
|
|
* @return self |
178
|
|
|
*/ |
179
|
16 |
|
public function setSiteName($siteName) |
180
|
|
|
{ |
181
|
16 |
|
$this->meta()->setSiteName($siteName); |
182
|
16 |
|
$this->openGraph()->setSiteName($siteName); |
183
|
|
|
|
184
|
16 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Hide the site name. |
189
|
|
|
* |
190
|
|
|
* @return self |
191
|
|
|
*/ |
192
|
4 |
|
public function hideSiteName() |
193
|
|
|
{ |
194
|
4 |
|
$this->meta()->hideSiteName(); |
195
|
|
|
|
196
|
4 |
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Show the site name. |
201
|
|
|
* |
202
|
|
|
* @return self |
203
|
|
|
*/ |
204
|
4 |
|
public function showSiteName() |
205
|
|
|
{ |
206
|
4 |
|
$this->meta()->showSiteName(); |
207
|
|
|
|
208
|
4 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set description. |
213
|
|
|
* |
214
|
|
|
* @param string $description |
215
|
|
|
* |
216
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoHelper |
217
|
|
|
*/ |
218
|
8 |
|
public function setDescription($description) |
219
|
|
|
{ |
220
|
8 |
|
$this->meta()->setDescription($description); |
221
|
8 |
|
$this->openGraph()->setDescription($description); |
222
|
8 |
|
$this->twitter()->setDescription($description); |
223
|
|
|
|
224
|
8 |
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set keywords. |
229
|
|
|
* |
230
|
|
|
* @param array|string $keywords |
231
|
|
|
* |
232
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
233
|
|
|
*/ |
234
|
8 |
|
public function setKeywords($keywords) |
235
|
|
|
{ |
236
|
8 |
|
$this->meta()->setKeywords($keywords); |
237
|
|
|
|
238
|
8 |
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set Image. |
243
|
|
|
* |
244
|
|
|
* @param string $imageUrl |
245
|
|
|
* |
246
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
247
|
|
|
*/ |
248
|
4 |
|
public function setImage($imageUrl) |
249
|
|
|
{ |
250
|
4 |
|
$this->openGraph()->setImage($imageUrl); |
251
|
4 |
|
$this->twitter()->addImage($imageUrl); |
252
|
|
|
|
253
|
4 |
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Set the current URL. |
258
|
|
|
* |
259
|
|
|
* @param string $url |
260
|
|
|
* |
261
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
262
|
|
|
*/ |
263
|
|
|
public function setUrl($url) |
264
|
|
|
{ |
265
|
|
|
$this->meta()->setUrl($url); |
266
|
52 |
|
$this->openGraph()->setUrl($url); |
267
|
|
|
|
268
|
52 |
|
return $this; |
269
|
52 |
|
} |
270
|
52 |
|
|
271
|
52 |
|
/* ----------------------------------------------------------------- |
272
|
|
|
| Main Methods |
273
|
|
|
| ----------------------------------------------------------------- |
274
|
|
|
*/ |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Render all seo tags. |
278
|
|
|
* |
279
|
|
|
* @return string |
280
|
4 |
|
*/ |
281
|
|
|
public function render() |
282
|
4 |
|
{ |
283
|
4 |
|
return implode(PHP_EOL, array_filter([ |
284
|
|
|
$this->meta()->render(), |
285
|
|
|
$this->openGraph()->render(), |
286
|
|
|
$this->twitter()->render(), |
287
|
|
|
])); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Render all seo tags with HtmlString object. |
292
|
28 |
|
* |
293
|
|
|
* @return \Illuminate\Support\HtmlString |
294
|
28 |
|
*/ |
295
|
|
|
public function renderHtml() |
296
|
|
|
{ |
297
|
|
|
return new HtmlString( |
298
|
|
|
$this->render() |
299
|
|
|
); |
300
|
|
|
} |
301
|
|
|
|
302
|
4 |
|
/** |
303
|
|
|
* Render the tag. |
304
|
4 |
|
* |
305
|
|
|
* @return string |
306
|
4 |
|
*/ |
307
|
|
|
public function __toString() |
308
|
|
|
{ |
309
|
|
|
return $this->render(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Enable the OpenGraph. |
314
|
4 |
|
* |
315
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
316
|
4 |
|
*/ |
317
|
|
|
public function enableOpenGraph() |
318
|
4 |
|
{ |
319
|
|
|
$this->openGraph()->enable(); |
320
|
|
|
|
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Disable the OpenGraph. |
326
|
4 |
|
* |
327
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
328
|
4 |
|
*/ |
329
|
|
|
public function disableOpenGraph() |
330
|
4 |
|
{ |
331
|
|
|
$this->openGraph()->disable(); |
332
|
|
|
|
333
|
|
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Enable the Twitter Card. |
338
|
4 |
|
* |
339
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
340
|
4 |
|
*/ |
341
|
|
|
public function enableTwitter() |
342
|
4 |
|
{ |
343
|
|
|
$this->twitter()->enable(); |
344
|
|
|
|
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Disable the Twitter Card. |
350
|
|
|
* |
351
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
352
|
|
|
*/ |
353
|
|
|
public function disableTwitter() |
354
|
|
|
{ |
355
|
|
|
$this->twitter()->disable(); |
356
|
|
|
|
357
|
|
|
return $this; |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|