|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class ClassyPost extends ClassyBasis { |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Current post id |
|
7
|
|
|
* @var int |
|
8
|
|
|
*/ |
|
9
|
|
|
public $ID; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Stores current post object |
|
13
|
|
|
* @var object |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $object; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Post title |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $post_title; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Post name |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $post_name; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Post content (raw) |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
public $post_content; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Post type |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
public $post_type; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Post author id |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
public $post_author; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Post date. String as stored in the WP database, ex: 2012-04-23 08:11:23 |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
public $post_date; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Post excerpt (raw) |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
public $post_excerpt; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Post status. It can be draft, publish, pending, private, trash, etc. |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
public $post_status; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Post permalink |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
public $permalink; |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Main constructor function. If ID won't be provided we will try to find it, based on your query |
|
74
|
|
|
* @param object/int $post |
|
|
|
|
|
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct($post = null) { |
|
77
|
|
|
if (is_integer($post)) { |
|
78
|
|
|
$this->ID = $post; |
|
79
|
|
|
$this->init(); |
|
80
|
|
|
} elseif (is_a($post, 'WP_Post')) { |
|
81
|
|
|
$this->import($post); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Initialises Instance based on provided post id |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function init() { |
|
89
|
|
|
$object = (array) $this->get_object(); |
|
90
|
|
|
|
|
91
|
|
|
if ($object) { |
|
|
|
|
|
|
92
|
|
|
$this->import($object); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns post object |
|
98
|
|
|
* |
|
99
|
|
|
* @return object |
|
100
|
|
|
*/ |
|
101
|
|
|
|
|
102
|
|
|
public function get_object() { |
|
103
|
|
|
$object = get_post($this->ID); |
|
104
|
|
|
|
|
105
|
|
|
return $object; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Checks if current user can edit this post |
|
110
|
|
|
* |
|
111
|
|
|
* @return boolean |
|
112
|
|
|
*/ |
|
113
|
|
|
public function can_edit() { |
|
114
|
|
|
if ( !function_exists( 'current_user_can' ) ) { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
if ( current_user_can( 'edit_post', $this->ID ) ) { |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the Post Edit url |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function get_edit_url() { |
|
129
|
|
|
if ( $this->can_edit() ) { |
|
130
|
|
|
return get_edit_post_link($this->ID); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns post thumbnail |
|
137
|
|
|
* |
|
138
|
|
|
* @return ClassyImage |
|
139
|
|
|
*/ |
|
140
|
|
|
public function get_thumbnail() { |
|
141
|
|
|
if ( function_exists('get_post_thumbnail_id') ) { |
|
142
|
|
|
$image_id = get_post_thumbnail_id($this->ID); |
|
143
|
|
|
|
|
144
|
|
|
if ( $image_id ) { |
|
145
|
|
|
return new ClassyImage($image_id); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns post title with filters applied |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function get_title() { |
|
157
|
|
|
return apply_filters('the_title', $this->post_title, $this->ID); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Alias for get_title |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function title() { |
|
166
|
|
|
return $this->get_title(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Returns the post content with filters applied. |
|
172
|
|
|
* |
|
173
|
|
|
* @param integer $page Page number, in case our post has <!--nextpage--> tags |
|
174
|
|
|
* @return string Post content |
|
175
|
|
|
*/ |
|
176
|
|
|
public function get_content( $page = 0 ) { |
|
177
|
|
|
if ( $page == 0 && $this->post_content ) { |
|
178
|
|
|
return $this->post_content; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$content = $this->post_content; |
|
182
|
|
|
|
|
183
|
|
|
if ( $page ) { |
|
184
|
|
|
$contents = explode('<!--nextpage-->', $content); |
|
185
|
|
|
|
|
186
|
|
|
$page--; |
|
187
|
|
|
|
|
188
|
|
|
if ( count($contents) > $page ) { |
|
189
|
|
|
$content = $contents[$page]; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$content = apply_filters('the_content', ($content)); |
|
194
|
|
|
|
|
195
|
|
|
return $content; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Alias for get_content |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
public function content() { |
|
205
|
|
|
return $this->get_content(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Returns post type object for current post |
|
210
|
|
|
* |
|
211
|
|
|
* @return object |
|
212
|
|
|
*/ |
|
213
|
|
|
public function get_post_type() { |
|
214
|
|
|
return get_post_type_object($this->post_type); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Returns post permalink |
|
219
|
|
|
* |
|
220
|
|
|
* @return string |
|
221
|
|
|
*/ |
|
222
|
|
|
public function get_permalink() { |
|
223
|
|
|
if ( isset($this->permalink) ) { |
|
224
|
|
|
return $this->permalink; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$this->permalink = get_permalink($this->ID); |
|
228
|
|
|
|
|
229
|
|
|
return $this->permalink; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Alias for get_permalink |
|
235
|
|
|
* |
|
236
|
|
|
* @return string |
|
237
|
|
|
*/ |
|
238
|
|
|
public function permalink() { |
|
239
|
|
|
return $this->get_permalink(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Returns post preview of requested length. |
|
244
|
|
|
* It will look for post_excerpt and will return it. |
|
245
|
|
|
* If post contains <!-- more --> tag it will return content until it |
|
246
|
|
|
* |
|
247
|
|
|
* @param integer $len Number of words |
|
248
|
|
|
* @param boolean $force If is set to true it will cut your post_excerpt to desired $len length |
|
249
|
|
|
* @param string $readmore The text for 'readmore' link |
|
250
|
|
|
* @param boolean $strip Should we strip tags? |
|
251
|
|
|
* @return string Post preview |
|
252
|
|
|
*/ |
|
253
|
|
|
public function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) { |
|
254
|
|
|
$text = ''; |
|
255
|
|
|
$trimmed = false; |
|
256
|
|
|
|
|
257
|
|
|
if ( isset($this->post_excerpt) && strlen($this->post_excerpt) ) { |
|
258
|
|
|
|
|
259
|
|
|
if ( $force ) { |
|
260
|
|
|
$text = ClassyHelper::trim_words($this->post_excerpt, $len, false); |
|
261
|
|
|
$trimmed = true; |
|
262
|
|
|
} else { |
|
263
|
|
|
$text = $this->post_excerpt; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if ( !strlen($text) && preg_match('/<!--\s?more(.*?)?-->/', $this->post_content, $readmore_matches) ) { |
|
269
|
|
|
|
|
270
|
|
|
$pieces = explode($readmore_matches[0], $this->post_content); |
|
271
|
|
|
$text = $pieces[0]; |
|
272
|
|
|
|
|
273
|
|
|
if ( $force ) { |
|
274
|
|
|
$text = ClassyHelper::trim_words($text, $len, false); |
|
275
|
|
|
$trimmed = true; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
$text = do_shortcode( $text ); |
|
279
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
if ( !strlen($text) ) { |
|
283
|
|
|
|
|
284
|
|
|
$text = ClassyHelper::trim_words($this->get_content(), $len, false); |
|
285
|
|
|
$trimmed = true; |
|
286
|
|
|
|
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
if ( !strlen(trim($text)) ) { |
|
290
|
|
|
|
|
291
|
|
|
return trim($text); |
|
292
|
|
|
|
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
if ( $strip ) { |
|
296
|
|
|
|
|
297
|
|
|
$text = trim(strip_tags($text)); |
|
298
|
|
|
|
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
if ( strlen($text) ) { |
|
302
|
|
|
|
|
303
|
|
|
$text = trim($text); |
|
304
|
|
|
$last = $text[strlen($text) - 1]; |
|
305
|
|
|
|
|
306
|
|
|
if ( $last != '.' && $trimmed ) { |
|
307
|
|
|
$text .= ' … '; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
if ( !$strip ) { |
|
311
|
|
|
$last_p_tag = strrpos($text, '</p>'); |
|
312
|
|
|
if ( $last_p_tag !== false ) { |
|
313
|
|
|
$text = substr($text, 0, $last_p_tag); |
|
314
|
|
|
} |
|
315
|
|
|
if ( $last != '.' && $trimmed ) { |
|
316
|
|
|
$text .= ' … '; |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
if ( $readmore && isset($readmore_matches) && !empty($readmore_matches[1]) ) { |
|
321
|
|
|
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore_matches[1]) . '</a>'; |
|
322
|
|
|
} elseif ( $readmore ) { |
|
323
|
|
|
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore) . '</a>'; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
if ( !$strip ) { |
|
327
|
|
|
$text .= '</p>'; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
return trim($text); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Returns comments array |
|
337
|
|
|
* @return array |
|
338
|
|
|
*/ |
|
339
|
|
|
public function get_comments($status = 'approve', $order = 'DESC') { |
|
340
|
|
|
|
|
341
|
|
|
$_return = array(); |
|
342
|
|
|
|
|
343
|
|
|
$args = array( |
|
344
|
|
|
'post_id' => $this->ID, |
|
345
|
|
|
'status' => $status, |
|
346
|
|
|
'order' => $order |
|
347
|
|
|
); |
|
348
|
|
|
|
|
349
|
|
|
$comments = get_comments($args); |
|
350
|
|
|
|
|
351
|
|
|
foreach ($comments as $comment) { |
|
352
|
|
|
|
|
353
|
|
|
$_return[$comment->comment_ID] = new ClassyComment($comment); |
|
354
|
|
|
|
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
foreach ($_return as $key => $comment) { |
|
358
|
|
|
|
|
359
|
|
|
if ($comment->has_parent()) { |
|
360
|
|
|
|
|
361
|
|
|
$_return[$comment->comment_parent]->add_child($comment); |
|
362
|
|
|
|
|
363
|
|
|
unset($_return[$key]); |
|
364
|
|
|
|
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
return array_values($_return); |
|
370
|
|
|
|
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.