1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Concerns; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Hyde; |
6
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPost; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Generates article metadata for a MarkdownPost. |
10
|
|
|
* |
11
|
|
|
* @see \Hyde\Framework\Models\Metadata |
12
|
|
|
* @see \Hyde\Framework\Testing\Feature\Concerns\HasArticleMetadataTest |
13
|
|
|
*/ |
14
|
|
|
trait HasArticleMetadata |
15
|
|
|
{ |
16
|
|
|
public array $metadata = []; |
17
|
|
|
public array $properties = []; |
18
|
|
|
|
19
|
|
|
public function constructMetadata(): void |
20
|
|
|
{ |
21
|
|
|
$this->parseFrontMatterMetadata(); |
22
|
|
|
|
23
|
|
|
$this->makeOpenGraphPropertiesForArticle(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getMetadata(): array |
27
|
|
|
{ |
28
|
|
|
return $this->metadata; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getMetaProperties(): array |
32
|
|
|
{ |
33
|
|
|
return $this->properties; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Generate metadata from the front matter that can be used in standard <meta> tags. |
38
|
|
|
* This helper is page type agnostic and works with any kind of model having front matter. |
39
|
|
|
*/ |
40
|
|
|
protected function parseFrontMatterMetadata(): void |
41
|
|
|
{ |
42
|
|
|
if (isset($this->matter['description'])) { |
43
|
|
|
$this->metadata['description'] = $this->matter['description']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (isset($this->matter['author'])) { |
47
|
|
|
$this->metadata['author'] = $this->getAuthorName($this->matter['author']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (isset($this->matter['category'])) { |
51
|
|
|
$this->metadata['keywords'] = $this->matter['category']; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Generate opengraph metadata from front matter for an og:article such as a blog post. |
57
|
|
|
*/ |
58
|
|
|
protected function makeOpenGraphPropertiesForArticle(): void |
59
|
|
|
{ |
60
|
|
|
$this->properties['og:type'] = 'article'; |
61
|
|
|
if (Hyde::hasSiteUrl()) { |
62
|
|
|
$this->properties['og:url'] = $this->getRoute()->getQualifiedUrl(); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($this->matter['title'])) { |
66
|
|
|
$this->properties['og:title'] = $this->matter['title']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (isset($this->matter['date'])) { |
70
|
|
|
$this->properties['og:article:published_time'] = date('c', strtotime($this->matter['date'])); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (isset($this->matter['image'])) { |
74
|
|
|
$this->setImageMetadata(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Parse the author name string from front matter with support for both flat and array notation. |
80
|
|
|
* |
81
|
|
|
* @param string|array $author |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function getAuthorName(string|array $author): string |
85
|
|
|
{ |
86
|
|
|
if (is_string($author)) { |
|
|
|
|
87
|
|
|
return $author; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $author['name'] ?? $author['username'] ?? 'Guest'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function setImageMetadata(): void |
94
|
|
|
{ |
95
|
|
|
if (is_string($this->matter['image'])) { |
96
|
|
|
$this->properties['og:image'] = $this->matter['image']; |
97
|
|
|
} else { |
98
|
|
|
if (isset($this->matter['image']['path'])) { |
99
|
|
|
$this->properties['og:image'] = $this->matter['image']['path']; |
100
|
|
|
} |
101
|
|
|
if (isset($this->matter['image']['uri'])) { |
102
|
|
|
$this->properties['og:image'] = $this->matter['image']['uri']; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|