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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
<?php
class LS_Posts {
// Stores the last query results public $post = null; public $posts = null;
/** * Returns posts that matches the query params * @param array $args Array of WP_Query attributes * @return bool Success of the query */ public static function find($args = array()) {
// Crate new instance $instance = new self;
if($instance->posts = get_posts($args)) { $instance->post = $instance->posts[0]; } return $instance; }
public static function getPostTypes() {
// Get post types $postTypes = get_post_types();
// Remove some defalt post types if(isset($postTypes['revision'])) { unset($postTypes['revision']); } if(isset($postTypes['nav_menu_item'])) { unset($postTypes['nav_menu_item']); }
// Convert names to plural foreach($postTypes as $key => $item) { if(!empty($item)) { $postTypes[$key] = array(); $postTypes[$key]['slug'] = $item; $postTypes[$key]['obj'] = get_post_type_object($item); $postTypes[$key]['name'] = $postTypes[$key]['obj']->labels->name; } }
return $postTypes; }
public function getParsedObject() {
if( ! $this->posts ) { return array(); }
foreach($this->posts as $key => $val) { $this->post = $val; $ret[$key]['post-id'] = $val->ID; $ret[$key]['post-slug'] = $val->post_name; $ret[$key]['post-url'] = get_permalink($val->ID); $ret[$key]['date-published'] = date_i18n(get_option('date_format'), strtotime($val->post_date)); $ret[$key]['date-modified'] = date_i18n(get_option('date_format'), strtotime($val->post_modified));
$ret[$key]['thumbnail'] = $this->getFeaturedImage( $val->ID, 'thumbnail' ); $ret[$key]['thumbnail-url'] = $this->getFeaturedImageURL( $val->ID, 'thumbnail' );
$ret[$key]['image'] = $this->getFeaturedImage( $val->ID ); $ret[$key]['image-url'] = $this->getFeaturedImageURL( $val->ID );
$ret[$key]['title'] = htmlspecialchars($this->getTitle()); $ret[$key]['content'] = $this->getContent(); $ret[$key]['excerpt'] = $this->getExcerpt(); $ret[$key]['author'] = get_userdata($val->post_author)->user_nicename; $ret[$key]['author-name'] = get_userdata($val->post_author)->display_name; $ret[$key]['author-id'] = $val->post_author; $ret[$key]['author-avatar'] = $this->getAuthorImage($val); $ret[$key]['categories'] = $this->getCategoryList($val); $ret[$key]['tags'] = $this->getTagList($val); $ret[$key]['comments'] = $val->comment_count; }
return $ret; }
public function getWithFormat($str, $textlength = 0) {
if(!is_object($this->post)) { return $str; }
// Post ID if(stripos($str, '[post-id]') !== false) { $str = str_replace('[post-id]', $this->post->ID, $str); }
// Post slug if(stripos($str, '[post-slug]') !== false) { $str = str_replace('[post-slug]', $this->post->post_name, $str); }
// Post URL if(stripos($str, '[post-url]') !== false) { $str = str_replace('[post-url]', get_permalink($this->post->ID), $str); }
// Date published if(stripos($str, '[date-published]') !== false) { $str = str_replace('[date-published]', date_i18n(get_option('date_format'), strtotime($this->post->post_date)), $str); }
// Date modified if(stripos($str, '[date-modified]') !== false) { $str = str_replace('%date-modified]', date_i18n(get_option('date_format'), strtotime($this->post->post_modified)), $str); }
// Featured image if(stripos($str, '[image]') !== false) { $markup = $this->getFeaturedImage( $this->post->ID ); $str = str_replace('[image]', $markup, $str); }
// Featured image URL if(stripos($str, '[image-url]') !== false) {
$url = $this->getFeaturedImageURL( $this->post->ID ); $str = str_replace('[image-url]', $url, $str); }
// Featured image thumbnail if(stripos($str, '[thumbnail]') !== false) { $markup = $this->getFeaturedImage( $this->post->ID, 'thumbnail' ); $str = str_replace('[thumbnail]', $markup, $str); }
// Featured image thumbnail URL if(stripos($str, '[thumbnail-url]') !== false) {
$url = $this->getFeaturedImageURL( $this->post->ID, 'thumbnail' ); $str = str_replace('[thumbnail-url]', $url, $str); }
// Title if(stripos($str, '[title]') !== false) { $str = str_replace('[title]', $this->getTitle($textlength), $str); }
// Content if(stripos($str, '[content]') !== false) { $str = str_replace('[content]', $this->getContent($textlength), $str); }
// Excerpt if(stripos($str, '[excerpt]') !== false) { $str = str_replace('[excerpt]', $this->getExcerpt($textlength), $str); }
// Author nickname if(stripos($str, '[author]') !== false) { $str = str_replace('[author]', $this->getAuthor(true), $str); }
// Author display name if(stripos($str, '[author-name]') !== false) { $str = str_replace('[author-name]', $this->getAuthor(false), $str); }
// Author avatar image if(stripos($str, '[author-avatar]') !== false) { $str = str_replace('[author-avatar]', $this->getAuthorImage( $this->post ), $str); }
// Author ID if(stripos($str, '[author-id]') !== false) { $str = str_replace('[author-id]', $this->post->post_author, $str); }
// Category list if(stripos($str, '[categories]') !== false) { $str = str_replace('[categories]', $this->getCategoryList(), $str); }
// Tags list if(stripos($str, '[tags]') !== false) { $str = str_replace('[tags]', $this->getTagList(), $str); }
// Number of comments if(stripos($str, '[comments]') !== false) { $str = str_replace('[comments]', $this->post->comment_count, $str); }
// Meta if(stripos($str, '[meta:') !== false) { $matches = array(); preg_match_all('/\[meta:\w(?:[-\w]*\w)?]/', $str, $matches);
foreach($matches[0] as $match) { $meta = str_replace('[meta:', '', $match); $meta = str_replace(']', '', $meta); $meta = get_post_meta($this->post->ID, $meta, true); $str = str_replace($match, $meta, $str); } }
return $str; }
/** * Returns the lastly selected post's title * @return string The title of the post */ public function getTitle($length = 0) {
if(!is_object($this->post)) { return false; }
$title = $this->post->post_title; if(!empty($length)) { $title = substr($title, 0, $length); }
return $title; }
/** * Returns the lastly selected post's excerpt * @return string The excerpt of the post */ public function getExcerpt($textlength = 0) {
global $post; $post = $this->post;
setup_postdata($post); $excerpt = get_the_excerpt(); wp_reset_postdata();
if(!empty($excerpt) && !empty($textlength)) { $excerpt = substr($excerpt, 0, $textlength); }
return $excerpt; }
public function getAuthor($nick = true) { $key = $nick ? 'user_nicename' : 'display_name'; if(is_object($this->post)) { return get_userdata($this->post->post_author)->$key; } else { return false; } }
public function getAuthorImage( $post = null ) {
if( ! empty( $post ) ) { $post = $this->post; }
if( function_exists( 'get_avatar_url' ) ) {
return '<img src="'.get_avatar_url( $post->post_author, array( 'size' => 256 )).'">'; }
return ''; }
public function getCategoryList( $post = null ) {
if(!empty($post)) { $post = $this->post; }
if(has_category(false, $this->post->ID)) { $cats = wp_get_post_categories($this->post->ID); foreach($cats as $val) { $cat = get_category($val); $list[] = '<a href="'.get_category_link($val).'">'.$cat->name.'</a>'; } return '<div>'.implode(', ', $list).'</div>'; } else { return ''; } }
public function getTagList( $post = null ) {
if(!empty($post)) { $post = $this->post; }
if(has_tag(false, $this->post->ID)) { $tags = wp_get_post_tags($this->post->ID); foreach($tags as $val) { $list[] = '<a href="/tag/'.$val->slug.'/">'.$val->name.'</a>'; } return '<div>'.implode(', ', $list).'</div>'; } else { return ''; } }
/** * Returns a subset of the post's content, * or the first paragraph if isn't specified * @param integer $length The subset's length * @return string The content */ public function getContent($length = false) {
if(!is_object($this->post)) { return false; }
$content = $this->post->post_content; if(!empty($length)) { $content = substr(wp_strip_all_tags($content), 0, $length); }
return nl2br($content); }
/** * Returns the featured image URL for the specified post ID. * Defaults to an empty GIF on error. * * @param integer $postID The ID of the post * @param string $size Attachment image size * @return string Featured image URL */ public function getFeaturedImageURL( $postID = 0, $size = 'full' ) {
if( function_exists('get_post_thumbnail_id') ) {
$attachmentID = get_post_thumbnail_id( $postID ); $attachment = wp_get_attachment_image_src( $attachmentID, $size );
if( ! empty( $attachment[0] ) ) { return $attachment[0]; } }
return LS_ROOT_URL . '/static/admin/img/blank.gif'; }
/** * Returns the featured image HTML element markup for the specified post ID. * Defaults to empty string on error. * * @param integer $postID The ID of the post * @param string $size Attachment image size * @return string <img> HTML markup or empty string on error */ public function getFeaturedImage( $postID = 0, $size = 'full' ) {
if( function_exists('get_post_thumbnail_id') ) {
$attachmentID = get_post_thumbnail_id( $postID ); $attachment = wp_get_attachment_image( $attachmentID, $size );
return $attachment; }
return ''; } }
|