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 int $pid |
75
|
|
|
*/ |
76
|
|
|
public function __construct($pid = null) { |
77
|
|
|
$this->ID = $this->verify_id($pid); |
78
|
|
|
|
79
|
|
|
$this->init(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function verify_id($pid) { |
83
|
|
|
return $pid; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Initialises Instance based on provided post id |
88
|
|
|
*/ |
89
|
|
|
protected function init() { |
90
|
|
|
$object = (array) $this->get_object(); |
91
|
|
|
|
92
|
|
|
$this->import($object); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns post object |
97
|
|
|
* |
98
|
|
|
* @return object |
99
|
|
|
*/ |
100
|
|
|
|
101
|
|
|
public function get_object() { |
102
|
|
|
$object = get_post($this->ID); |
103
|
|
|
|
104
|
|
|
return $object; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Checks if current user can edit this post |
109
|
|
|
* |
110
|
|
|
* @return boolean |
111
|
|
|
*/ |
112
|
|
|
public function can_edit() { |
113
|
|
|
if ( !function_exists( 'current_user_can' ) ) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
if ( current_user_can( 'edit_post', $this->ID ) ) { |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the Post Edit url |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function get_edit_url() { |
128
|
|
|
if ( $this->can_edit() ) { |
129
|
|
|
return get_edit_post_link($this->ID); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns post thumbnail |
136
|
|
|
* |
137
|
|
|
* @return ClassyImage |
138
|
|
|
*/ |
139
|
|
|
public function get_thumbnail() { |
140
|
|
|
if ( function_exists('get_post_thumbnail_id') ) { |
141
|
|
|
$image_id = get_post_thumbnail_id($this->ID); |
142
|
|
|
|
143
|
|
|
if ( $image_id ) { |
144
|
|
|
return new ClassyImage($image_id); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns post title with filters applied |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function get_title() { |
156
|
|
|
return apply_filters('the_title', $this->post_title, $this->ID); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Alias for get_title |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function title() { |
165
|
|
|
return $this->get_title(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the post content with filters applied. |
171
|
|
|
* |
172
|
|
|
* @param integer $page Page number, in case our post has <!--nextpage--> tags |
173
|
|
|
* @return string Post content |
174
|
|
|
*/ |
175
|
|
|
public function get_content( $page = 0 ) { |
176
|
|
|
if ( $page == 0 && $this->post_content ) { |
177
|
|
|
return $this->post_content; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$content = $this->post_content; |
181
|
|
|
|
182
|
|
|
if ( $page ) { |
183
|
|
|
$contents = explode('<!--nextpage-->', $content); |
184
|
|
|
|
185
|
|
|
$page--; |
186
|
|
|
|
187
|
|
|
if ( count($contents) > $page ) { |
188
|
|
|
$content = $contents[$page]; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$content = apply_filters('the_content', ($content)); |
193
|
|
|
|
194
|
|
|
return $content; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Alias for get_content |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
public function content() { |
204
|
|
|
return $this->get_content(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns post type object for current post |
209
|
|
|
* |
210
|
|
|
* @return object |
211
|
|
|
*/ |
212
|
|
|
public function get_post_type() { |
213
|
|
|
return get_post_type_object($this->post_type); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns post permalink |
218
|
|
|
* |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function get_permalink() { |
222
|
|
|
if ( isset($this->permalink) ) { |
223
|
|
|
return $this->permalink; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->permalink = get_permalink($this->ID); |
227
|
|
|
|
228
|
|
|
return $this->permalink; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Alias for get_permalink |
234
|
|
|
* |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function permalink() { |
238
|
|
|
return $this->get_permalink(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns post preview of requested length. |
243
|
|
|
* It will look for post_excerpt and will return it. |
244
|
|
|
* If post contains <!-- more --> tag it will return content until it |
245
|
|
|
* |
246
|
|
|
* @param integer $len Number of words |
247
|
|
|
* @param boolean $force If is set to true it will cut your post_excerpt to desired $len length |
248
|
|
|
* @param string $readmore The text for 'readmore' link |
249
|
|
|
* @param boolean $strip Should we strip tags? |
250
|
|
|
* @return string Post preview |
251
|
|
|
*/ |
252
|
|
|
public function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) { |
253
|
|
|
$text = ''; |
254
|
|
|
$trimmed = false; |
255
|
|
|
|
256
|
|
|
if ( isset($this->post_excerpt) && strlen($this->post_excerpt) ) { |
257
|
|
|
|
258
|
|
|
if ( $force ) { |
259
|
|
|
$text = ClassyHelper::trim_words($this->post_excerpt, $len, false); |
260
|
|
|
$trimmed = true; |
261
|
|
|
} else { |
262
|
|
|
$text = $this->post_excerpt; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
if ( !strlen($text) && preg_match('/<!--\s?more(.*?)?-->/', $this->post_content, $readmore_matches) ) { |
268
|
|
|
|
269
|
|
|
$pieces = explode($readmore_matches[0], $this->post_content); |
270
|
|
|
$text = $pieces[0]; |
271
|
|
|
|
272
|
|
|
if ( $force ) { |
273
|
|
|
$text = ClassyHelper::trim_words($text, $len, false); |
274
|
|
|
$trimmed = true; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$text = do_shortcode( $text ); |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if ( !strlen($text) ) { |
282
|
|
|
|
283
|
|
|
$text = ClassyHelper::trim_words($this->get_content(), $len, false); |
284
|
|
|
$trimmed = true; |
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if ( !strlen(trim($text)) ) { |
289
|
|
|
|
290
|
|
|
return trim($text); |
291
|
|
|
|
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
if ( $strip ) { |
295
|
|
|
|
296
|
|
|
$text = trim(strip_tags($text)); |
297
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if ( strlen($text) ) { |
301
|
|
|
|
302
|
|
|
$text = trim($text); |
303
|
|
|
$last = $text[strlen($text) - 1]; |
304
|
|
|
|
305
|
|
|
if ( $last != '.' && $trimmed ) { |
306
|
|
|
$text .= ' … '; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
if ( !$strip ) { |
310
|
|
|
$last_p_tag = strrpos($text, '</p>'); |
311
|
|
|
if ( $last_p_tag !== false ) { |
312
|
|
|
$text = substr($text, 0, $last_p_tag); |
313
|
|
|
} |
314
|
|
|
if ( $last != '.' && $trimmed ) { |
315
|
|
|
$text .= ' … '; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
if ( $readmore && isset($readmore_matches) && !empty($readmore_matches[1]) ) { |
320
|
|
|
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore_matches[1]) . '</a>'; |
321
|
|
|
} elseif ( $readmore ) { |
322
|
|
|
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore) . '</a>'; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if ( !$strip ) { |
326
|
|
|
$text .= '</p>'; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return trim($text); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
} |
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.