1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Concerns; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Helpers\Meta; |
6
|
|
|
use Hyde\Framework\Hyde; |
7
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPost; |
8
|
|
|
use Hyde\Framework\Services\RssFeedService; |
9
|
|
|
use Hyde\Framework\Services\SitemapService; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @todo Move logic into service class to make it easier to test. |
13
|
|
|
* |
14
|
|
|
* @see \Hyde\Framework\Testing\Feature\Concerns\HasPageMetadataTest |
15
|
|
|
*/ |
16
|
|
|
trait HasPageMetadata |
17
|
|
|
{ |
18
|
|
|
abstract public function htmlTitle(?string $title = null): string; |
19
|
|
|
|
20
|
|
|
public function getCanonicalUrl(): string |
21
|
|
|
{ |
22
|
|
|
return $this->getRoute()->getQualifiedUrl(); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getDynamicMetadata(): array |
26
|
|
|
{ |
27
|
|
|
$array = []; |
28
|
|
|
|
29
|
|
|
if ($this->canUseCanonicalUrl()) { |
30
|
|
|
$array[] = '<link rel="canonical" href="'.$this->getCanonicalUrl().'" />'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($this->canUseSitemapLink()) { |
34
|
|
|
$array[] = '<link rel="sitemap" type="application/xml" title="Sitemap" href="'.Hyde::url('sitemap.xml').'" />'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($this->canUseRssFeedLink()) { |
38
|
|
|
$array[] = $this->makeRssFeedLink(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (isset($this->title)) { |
42
|
|
|
if ($this->hasTwitterTitleInConfig()) { |
43
|
|
|
$array[] = '<meta name="twitter:title" content="'.$this->htmlTitle().'" />'; |
44
|
|
|
} |
45
|
|
|
if ($this->hasOpenGraphTitleInConfig()) { |
46
|
|
|
$array[] = '<meta property="og:title" content="'.$this->htmlTitle().'" />'; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($this instanceof MarkdownPost) { |
51
|
|
|
$array[] = "\n<!-- Blog Post Meta Tags -->"; |
52
|
|
|
foreach ($this->getMetadata() as $name => $content) { |
53
|
|
|
$array[] = Meta::name($name, $content); |
54
|
|
|
} |
55
|
|
|
foreach ($this->getMetaProperties() as $property => $content) { |
56
|
|
|
$array[] = Meta::property($property, $content); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $array; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function renderPageMetadata(): string |
64
|
|
|
{ |
65
|
|
|
return Meta::render( |
66
|
|
|
withMergedData: $this->getDynamicMetadata() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function canUseCanonicalUrl(): bool |
71
|
|
|
{ |
72
|
|
|
return Hyde::hasSiteUrl() && isset($this->slug); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function canUseSitemapLink(): bool |
76
|
|
|
{ |
77
|
|
|
return SitemapService::canGenerateSitemap(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function canUseRssFeedLink(): bool |
81
|
|
|
{ |
82
|
|
|
if (RssFeedService::canGenerateFeed() && isset($this->slug)) { |
83
|
|
|
if ($this instanceof MarkdownPost) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (str_starts_with($this->getCurrentPagePath(), 'post')) { |
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->getCurrentPagePath() === 'index') { |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function hasTwitterTitleInConfig(): bool |
100
|
|
|
{ |
101
|
|
|
return str_contains(json_encode(config('hyde.meta', [])), 'twitter:title'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function hasOpenGraphTitleInConfig(): bool |
105
|
|
|
{ |
106
|
|
|
return str_contains(json_encode(config('hyde.meta', [])), 'og:title'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function makeRssFeedLink(): string |
110
|
|
|
{ |
111
|
|
|
return sprintf('<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />', |
112
|
|
|
RssFeedService::getDescription(), |
113
|
|
|
Hyde::url(RssFeedService::getDefaultOutputFilename()) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|