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