1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Cms; |
4
|
|
|
|
5
|
|
|
// From 'charcoal-translator' |
6
|
|
|
use Charcoal\Translator\Translation; |
7
|
|
|
use Charcoal\Translator\Translator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
trait MetatagTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Translation|string|null |
16
|
|
|
*/ |
17
|
|
|
protected $metaTitle; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Translation|string|null |
21
|
|
|
*/ |
22
|
|
|
protected $metaDescription; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Translation|string|null |
26
|
|
|
*/ |
27
|
|
|
protected $metaImage; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Translation|string|null |
31
|
|
|
*/ |
32
|
|
|
protected $metaAuthor; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Translation|string|null |
36
|
|
|
*/ |
37
|
|
|
protected $metaTags; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string $facebookAppId |
41
|
|
|
*/ |
42
|
|
|
protected $facebookAppId; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Translation|string|null |
46
|
|
|
*/ |
47
|
|
|
protected $opengraphTitle; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Translation|string|null |
51
|
|
|
*/ |
52
|
|
|
protected $opengraphSiteName; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Translation|string|null |
56
|
|
|
*/ |
57
|
|
|
protected $opengraphDescription; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $opengraphType; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var Translation|string|null |
66
|
|
|
*/ |
67
|
|
|
protected $opengraphImage; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var Translation|string|null |
71
|
|
|
*/ |
72
|
|
|
protected $opengraphAuthor; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var Translation|string|null |
76
|
|
|
*/ |
77
|
|
|
protected $opengraphPublisher; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var Translation|string|null |
81
|
|
|
*/ |
82
|
|
|
protected $opengraphTags; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var Translation|string|null |
86
|
|
|
*/ |
87
|
|
|
protected $twitterCardImage; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var Translation|string|null |
91
|
|
|
*/ |
92
|
|
|
protected $twitterCardType; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
abstract public function canonicalUrl(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Translation|string|null |
101
|
|
|
*/ |
102
|
|
|
abstract public function defaultMetaTitle(); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Translation|string|null |
106
|
|
|
*/ |
107
|
|
|
abstract public function defaultMetaDescription(); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Translation|string|null |
111
|
|
|
*/ |
112
|
|
|
abstract public function defaultMetaImage(); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $title The meta tile (localized). |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
|
|
public function setMetaTitle($title) |
119
|
|
|
{ |
120
|
|
|
$this->metaTitle = $this->translator()->translation($title); |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Translation|string|null |
126
|
|
|
*/ |
127
|
|
|
public function metaTitle() |
128
|
|
|
{ |
129
|
|
|
return $this->metaTitle; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param mixed $description The meta description (localized). |
134
|
|
|
* @return self |
135
|
|
|
*/ |
136
|
|
|
public function setMetaDescription($description) |
137
|
|
|
{ |
138
|
|
|
$this->metaDescription = $this->translator()->translation($description); |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return Translation|string|null |
144
|
|
|
*/ |
145
|
|
|
public function metaDescription() |
146
|
|
|
{ |
147
|
|
|
return $this->metaDescription; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param mixed $image The meta image (localized). |
152
|
|
|
* @return self |
153
|
|
|
*/ |
154
|
|
|
public function setMetaImage($image) |
155
|
|
|
{ |
156
|
|
|
$this->metaImage = $this->translator()->translation($image); |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return Translation|string|null |
162
|
|
|
*/ |
163
|
|
|
public function metaImage() |
164
|
|
|
{ |
165
|
|
|
return $this->metaImage; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param mixed $author The meta author (localized). |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
|
|
public function setMetaAuthor($author) |
173
|
|
|
{ |
174
|
|
|
$this->metaAuthor = $this->translator()->translation($author); |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return Translation|string|null |
180
|
|
|
*/ |
181
|
|
|
public function metaAuthor() |
182
|
|
|
{ |
183
|
|
|
return $this->metaAuthor; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $tags Comma separated list of tags. |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function setMetaTags($tags) |
191
|
|
|
{ |
192
|
|
|
$this->metaTags = $this->translator()->translation($tags); |
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function metaTags() |
200
|
|
|
{ |
201
|
|
|
return $this->metaTags; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $appId The facebook App ID (numeric string). |
206
|
|
|
* @return self |
207
|
|
|
*/ |
208
|
|
|
public function setFacebookAppId($appId) |
209
|
|
|
{ |
210
|
|
|
$this->facebookAppId = $appId; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function facebookAppId() |
218
|
|
|
{ |
219
|
|
|
return $this->facebookAppId; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param mixed $title The opengraph title (localized). |
224
|
|
|
* @return self |
225
|
|
|
*/ |
226
|
|
|
public function setOpengraphTitle($title) |
227
|
|
|
{ |
228
|
|
|
$this->opengraphTitle = $this->translator()->translation($title); |
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get the opengraph title. |
234
|
|
|
* |
235
|
|
|
* If not expilicitely defined, use the meta title as opengraph title. |
236
|
|
|
* |
237
|
|
|
* @return Translation|string|null |
238
|
|
|
*/ |
239
|
|
|
public function opengraphTitle() |
240
|
|
|
{ |
241
|
|
|
if (!$this->opengraphTitle) { |
242
|
|
|
return $this->metaTitle(); |
243
|
|
|
} |
244
|
|
|
return $this->opengraphTitle; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param mixed $siteName The site name (localized). |
249
|
|
|
* @return self |
250
|
|
|
*/ |
251
|
|
|
public function setOpengraphSiteName($siteName) |
252
|
|
|
{ |
253
|
|
|
$this->opengraphSiteName = $this->translator()->translation($siteName); |
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return Translation|string|null |
259
|
|
|
*/ |
260
|
|
|
public function opengraphSiteName() |
261
|
|
|
{ |
262
|
|
|
return $this->opengraphSiteName; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param mixed $description The opengraph description (localized). |
267
|
|
|
* @return self |
268
|
|
|
*/ |
269
|
|
|
public function setOpengraphDescription($description) |
270
|
|
|
{ |
271
|
|
|
$this->opengraphDescription = $this->translator()->translation($description); |
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return Translation|string|null |
277
|
|
|
*/ |
278
|
|
|
public function opengraphDescription() |
279
|
|
|
{ |
280
|
|
|
if (!$this->opengraphDescription) { |
281
|
|
|
return $this->metaDescription(); |
282
|
|
|
} |
283
|
|
|
return $this->opengraphDescription; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param string $type The opengraph type. |
288
|
|
|
* @return self |
289
|
|
|
*/ |
290
|
|
|
public function setOpengraphType($type) |
291
|
|
|
{ |
292
|
|
|
$this->opengraphType = $type; |
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return string |
298
|
|
|
*/ |
299
|
|
|
public function opengraphType() |
300
|
|
|
{ |
301
|
|
|
return $this->opengraphType; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param mixed $image The opengraph image (localized). |
306
|
|
|
* @return self |
307
|
|
|
*/ |
308
|
|
|
public function setOpengraphImage($image) |
309
|
|
|
{ |
310
|
|
|
$this->opengraphImage = $this->translator()->translation($image); |
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return Translation|string|null |
316
|
|
|
*/ |
317
|
|
|
public function opengraphImage() |
318
|
|
|
{ |
319
|
|
|
if (!$this->opengraphImage) { |
320
|
|
|
return $this->metaImage(); |
321
|
|
|
} |
322
|
|
|
return $this->opengraphImage; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param mixed $author The opengraph author (localized). |
327
|
|
|
* @return self |
328
|
|
|
*/ |
329
|
|
|
public function setOpengraphAuthor($author) |
330
|
|
|
{ |
331
|
|
|
$this->opengraphAuthor = $this->translator()->translation($author); |
332
|
|
|
return $this; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @return Translation|string|null |
337
|
|
|
*/ |
338
|
|
|
public function opengraphAuthor() |
339
|
|
|
{ |
340
|
|
|
if (!$this->opengraphAuthor) { |
341
|
|
|
return $this->metaAuthor(); |
342
|
|
|
} |
343
|
|
|
return $this->opengraphAuthor; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @param mixed $publisher The opengraph publisher (localized). |
348
|
|
|
* @return self |
349
|
|
|
*/ |
350
|
|
|
public function setOpengraphPublisher($publisher) |
351
|
|
|
{ |
352
|
|
|
$this->opengraphPublisher = $this->translator()->translation($publisher); |
353
|
|
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return Translation|string|null |
358
|
|
|
*/ |
359
|
|
|
public function opengraphPublisher() |
360
|
|
|
{ |
361
|
|
|
if (!$this->opengraphPublisher) { |
362
|
|
|
return $this->opengraphAuthor(); |
363
|
|
|
} |
364
|
|
|
return $this->opengraphPublisher; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param string $type The twitter card type. |
369
|
|
|
* @return self |
370
|
|
|
*/ |
371
|
|
|
public function setTwitterCardType($type) |
372
|
|
|
{ |
373
|
|
|
$this->twitterCardType = $type; |
374
|
|
|
return $this; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Retrieve the object's {@link https://dev.twitter.com/cards/types card type}, |
379
|
|
|
* for the "twitter:card" meta-property. |
380
|
|
|
* |
381
|
|
|
* @return string|null |
382
|
|
|
*/ |
383
|
|
|
public function twitterCardType() |
384
|
|
|
{ |
385
|
|
|
return $this->twitterCardType; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param mixed $image The twitter card image (localized). |
390
|
|
|
* @return self |
391
|
|
|
*/ |
392
|
|
|
public function setTwitterCardImage($image) |
393
|
|
|
{ |
394
|
|
|
$this->twitterCardImage = $image; |
395
|
|
|
return $this; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Retrieve the URL to the object's social image for the "twitter:image" meta-property. |
400
|
|
|
* |
401
|
|
|
* @return string|null |
402
|
|
|
*/ |
403
|
|
|
public function twitterCardImage() |
404
|
|
|
{ |
405
|
|
|
return $this->twitterCardImage; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Generates the default metatags for the current object. |
410
|
|
|
* Prevents some problem where the defaultMetaTag method |
411
|
|
|
* content isn't set at the moment of setting the meta. |
412
|
|
|
* Should be called on preSave and preUpdate of the object. |
413
|
|
|
* |
414
|
|
|
* @return self $this. |
415
|
|
|
*/ |
416
|
|
|
public function generateDefaultMetaTags() |
417
|
|
|
{ |
418
|
|
|
if ($this->isEmptyMeta($this->metaTitle)) { |
419
|
|
|
$this->setMetaTitle($this->defaultMetaTitle()); |
420
|
|
|
} |
421
|
|
|
if ($this->isEmptyMeta($this->metaDescription)) { |
422
|
|
|
$this->setMetaDescription($this->defaultMetaDescription()); |
423
|
|
|
} |
424
|
|
|
if ($this->isEmptyMeta($this->metaImage)) { |
425
|
|
|
$this->setMetaImage($this->defaultMetaImage()); |
426
|
|
|
} |
427
|
|
|
return $this; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Check if the meta is empty. Method exists |
432
|
|
|
* because at this point we don't really know |
433
|
|
|
* what's in the meta. |
434
|
|
|
* Possible param type: |
435
|
|
|
* - [ lang => value, lang => value ] |
436
|
|
|
* - Translation |
437
|
|
|
* - null |
438
|
|
|
* |
439
|
|
|
* @param mixed $meta Current meta value. |
440
|
|
|
* @return boolean Empty or not. |
441
|
|
|
*/ |
442
|
|
|
public function isEmptyMeta($meta) |
443
|
|
|
{ |
444
|
|
|
if (!$meta) { |
445
|
|
|
return true; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
// From back-end form which post meta_description[lang]=value |
449
|
|
|
// Gives [ lang => value ] as value |
450
|
|
|
if (is_array($meta)) { |
451
|
|
|
$meta = $this->translator()->translation($meta); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
// If one value is set in whatever language, |
455
|
|
|
// this is NOT empty. |
456
|
|
|
if ($meta instanceof Translation) { |
457
|
|
|
$empty = true; |
458
|
|
|
foreach ($meta->data() as $lang => $val) { |
459
|
|
|
if ($val && $val != '') { |
460
|
|
|
$empty = false; |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
return $empty; |
464
|
|
|
} |
465
|
|
|
return true; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @return Translator |
470
|
|
|
*/ |
471
|
|
|
abstract protected function translator(); |
472
|
|
|
} |
473
|
|
|
|