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