1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace AloiaCms\Models; |
5
|
|
|
|
6
|
|
|
use AloiaCms\HtmlParser; |
7
|
|
|
use AloiaCms\Models\Contracts\ModelInterface; |
8
|
|
|
use AloiaCms\Models\Contracts\PublishInterface; |
9
|
|
|
use AloiaCms\Models\Traits\Postable; |
10
|
|
|
use AloiaCms\Models\Traits\Publishable; |
11
|
|
|
use AloiaCms\Models\Traits\Updatable; |
12
|
|
|
use Illuminate\Support\Facades\Config; |
13
|
|
|
|
14
|
|
|
class Article extends Model implements ModelInterface, PublishInterface |
15
|
|
|
{ |
16
|
|
|
use Publishable, Postable, Updatable; |
17
|
|
|
|
18
|
|
|
protected $folder = 'articles'; |
19
|
|
|
|
20
|
|
|
protected $required_fields = [ |
21
|
|
|
'post_date' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the title of this article |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
2 |
|
public function title(): string |
31
|
|
|
{ |
32
|
2 |
|
if (isset($this->matter['title'])) { |
33
|
2 |
|
return $this->matter['title']; |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
$file_content = $this->body(); |
37
|
|
|
|
38
|
1 |
|
$titles = []; |
39
|
|
|
|
40
|
1 |
|
if (!empty($file_content)) { |
41
|
|
|
$titles = HtmlParser::getTextBetweenTags($file_content, 'h1'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $titles[0] ?? "Untitled article"; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
1 |
|
* Get the slug of this article |
49
|
|
|
* |
50
|
1 |
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function slug(): string |
53
|
|
|
{ |
54
|
|
|
return $this->file_name; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the main image of this article |
59
|
4 |
|
* |
60
|
|
|
* @return string |
61
|
4 |
|
* @throws \Exception |
62
|
1 |
|
*/ |
63
|
|
|
public function image(): string |
64
|
|
|
{ |
65
|
3 |
|
if (isset($this->matter['image'])) { |
66
|
|
|
return $this->matter['image']; |
67
|
3 |
|
} |
68
|
|
|
|
69
|
3 |
|
$file_content = $this->body(); |
70
|
|
|
|
71
|
|
|
$images = []; |
72
|
|
|
|
73
|
|
|
if (!empty($file_content)) { |
74
|
|
|
$images = HtmlParser::getTagAttribute($file_content, 'img', 'src'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $images[0] ?? ""; |
78
|
3 |
|
} |
79
|
|
|
|
80
|
3 |
|
/** |
81
|
1 |
|
* Get the path to the thumbnail of this article |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
2 |
|
* @throws \Exception |
85
|
1 |
|
*/ |
86
|
|
|
public function thumbnail(): string |
87
|
|
|
{ |
88
|
1 |
|
return $this->matter['thumbnail'] ?? ""; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the description of this article |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
* @throws \Exception |
96
|
1 |
|
*/ |
97
|
|
|
public function description(): string |
98
|
1 |
|
{ |
99
|
1 |
|
return $this->matter['description'] ?? $this->getDescriptionFromContent(); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
/** |
103
|
|
|
* Generate a description for the content |
104
|
|
|
* |
105
|
|
|
* @return bool|string |
106
|
|
|
* @throws \Exception |
107
|
|
|
*/ |
108
|
|
|
protected function getDescriptionFromContent(): string |
109
|
|
|
{ |
110
|
1 |
|
$paragraphs = HtmlParser::getTextBetweenTags($this->body(), 'p'); |
111
|
|
|
|
112
|
1 |
|
$paragraphs_with_text_content = array_filter($paragraphs, function ($paragraph) { |
113
|
|
|
return !empty(strip_tags($paragraph)); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
if (count($paragraphs_with_text_content) > 0) { |
117
|
|
|
return substr(head($paragraphs_with_text_content), 0, 160); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return ""; |
121
|
2 |
|
} |
122
|
|
|
|
123
|
2 |
|
/** |
124
|
|
|
* Get the canonical if it's set |
125
|
|
|
* |
126
|
|
|
* @return null|string |
127
|
|
|
*/ |
128
|
|
|
public function canonicalLink(): ?string |
129
|
|
|
{ |
130
|
|
|
return $this->matter['canonical'] ?? null; |
131
|
|
|
} |
132
|
2 |
|
|
133
|
|
|
/** |
134
|
2 |
|
* Get the external URL if it's set |
135
|
|
|
* |
136
|
2 |
|
* @return null|string |
137
|
1 |
|
*/ |
138
|
2 |
|
public function externalUrl(): ?string |
139
|
|
|
{ |
140
|
2 |
|
return $this->matter['external_url'] ?? null; |
141
|
1 |
|
} |
142
|
|
|
} |
143
|
|
|
|