-
Notifications
You must be signed in to change notification settings - Fork 4
/
Item.php
306 lines (269 loc) · 6.62 KB
/
Item.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
namespace OWC\PDC\Base\Models;
/**
* Model for the item
*/
class Item
{
public const PREFIX = '_owc_';
/**
* Type of model.
*
* @var string
*/
protected $posttype = 'pdc-item';
/**
* Data of the Post.
*
* @var array
*/
protected $data;
/**
* Metadata of the post.
*
* @var array
*/
protected $meta;
/**
* Post constructor.
*
* @param array $data
* @param array|null $meta
*/
public function __construct(array $data, array $meta = null)
{
$this->data = $data;
$this->meta = is_null($meta) ? get_post_meta($data['ID']) : $meta;
}
/**
* Make Post model from WP_Post object
*
* @param \WP_Post $post
*
* @return Post
*/
public static function makeFrom(\WP_Post $post)
{
return new static($post->to_array());
}
/**
* Get the ID of the post.
*
* @return int
*/
public function getID(): int
{
return $this->data['ID'] ?? 0;
}
/**
* Get the title of the post.
*
* @return string
*/
public function getTitle(): string
{
return $this->data['post_title'];
}
/**
* Get the title of the post.
*
* @return string
*/
public function getPostName(): string
{
return $this->data['post_name'];
}
/**
* Get the date of the post as a DateTime object.
*
* @return \DateTime
*/
public function getDate(): \DateTime
{
return \DateTime::createFromFormat('Y-m-d g:i:s', get_the_date('Y-m-d g:i:s', $this->getID()));
}
/**
* Get the modified date of the post as a DateTime object.
*
* @return \DateTime
*/
public function getPostModified($gmt = false): \DateTime
{
$timezone = $gmt ? 'post_modified_gmt' : 'post_modified';
return (false !== \DateTime::createFromFormat('Y-m-d G:i:s', $this->data[$timezone])) ? \DateTime::createFromFormat('Y-m-d G:i:s', $this->data[$timezone]) : new \DateTime();
}
/**
* Retrieve the date in localized format.
*
* @param string $format
*
* @return string
*/
public function getDateI18n(string $format): string
{
return date_i18n($format, $this->getDate()->getTimestamp());
}
/**
* Returns the type of the post.
*
* @return string|false
*/
public function getPostType(): string
{
return get_post_type($this->getID());
}
/**
* Get the permalink to the post.
*
* @return string
*/
public function getLink(): string
{
return get_permalink($this->getID()) ?? '';
}
/**
* Get the thumbnail URL of the author.
*
* @param string $size
*
* @return string
*/
public function getThumbnail($size = 'post-thumbnail'): string
{
return get_the_post_thumbnail_url($this->getID(), $size) ?? '';
}
/**
* Determines if the post has a thumbnail
*
* @return bool
*/
public function hasThumbnail(): bool
{
return has_post_thumbnail($this->getID());
}
/**
* Get the excerpt of the post, else fallback to the post content.
*
* @return string
*/
public function getExcerpt($length = 20): string
{
if (empty($this->getKey('post_excerpt'))) {
return wp_trim_words(strip_shortcodes($this->getKey('post_content')), $length);
}
return $this->getKey('post_excerpt');
}
/**
* Get the content of the post.
*
* @return string
*/
public function getContent(): string
{
return apply_filters('the_content', $this->getKey('post_content'));
}
/**
* Determines if the post has content.
*
* @return bool
*/
public function hasContent(): bool
{
return ! empty($this->getKey('post_content'));
}
/**
* Get the taxonomies of a post.
*
* @return array
*/
public function getTaxonomies(): array
{
return get_post_taxonomies($this->getID());
}
/**
* Get the terms of a particular taxonomy.
*
* @param string $taxonomy
*
* @return \WP_Term[]
*/
public function getTerms(string $taxonomy)
{
return get_the_terms($this->getID(), $taxonomy);
}
/**
* Get a particular key from the data.
*
* @param string $value
* @param string $default
*
* @return mixed
*/
protected function getKey(string $value, string $default = '')
{
return $this->data[$value] ?? $default;
}
/**
* Get a meta value from the metadata.
*
* @param string $value
* @param string $default
* @param bool $single
* @param null|string $prefix
*
* @return mixed
*/
public function getMeta(string $value, $default = '', $single = true, $prefix = null)
{
$prefix = is_null($prefix) ? self::PREFIX : $prefix . $value;
if (empty($this->meta[$prefix])) {
return $default;
}
return $single ? current($this->meta[$prefix]) : $this->meta[$prefix];
}
public function getEscapeElement(): bool
{
$value = $this->getMeta('escape_element_active', '0', true, '_owc_');
return boolval($value);
}
public function useTableOfContents(): bool
{
$value = $this->getMeta('pdc_use_table_of_contents', '0', true, '_owc_');
return boolval($value);
}
/**
* URL contains ONLY a connected theme and subtheme.
* Is used in 'post_type_link' filter registered in '\OWC\PDC\Base\Admin\AdminServiceProvider::class'.
*/
public function getBasePortalURL(): string
{
return PortalLinkGenerator::make($this)->generateBasePortalLink();
}
/**
* URL contains connected theme, connected subtheme, post slug and ID.
*/
public function getPortalURL(): string
{
return PortalLinkGenerator::make($this)->generateFullPortalLink();
}
public function getConnected($connection): ?\WP_Post
{
$connected = new \WP_Query([
'connected_type' => $connection,
'connected_items' => $this->getID(),
'nopaging' => true,
'post_status' => 'publish',
'connected_query' => ['post_status' => ['publish', 'draft']]
]);
return ! empty($connected->post) ? $connected->post : null;
}
/**
* @param array $array
*
* @return array
*/
public function arrayUnique($array): array
{
return is_array($array) ? array_unique($array) : [];
}
}