/
hmp.functions.php
executable file
·458 lines (352 loc) · 10.8 KB
/
hmp.functions.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* Gets the current posts byline.
*
* @param object $post. (default: global $post)
* @return string
*/
function hmp_get_byline( $post_id = null ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
return (string) get_post_meta( $post_id, '_hmp_byline', true );
}
/**
* Gets the current posts brief.
*
* @param post ID or object $post. (default: global $post)
* @return string
*/
function hmp_get_brief( $post_id = null ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
return (string) get_post_meta( $post_id, '_hmp_brief', true );
}
/**
* Gets the current posts work url (not permalink).
*
* @param post ID or object $post. (default: global $post)
* @return string
*/
function hmp_get_url( $post_id = null ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
return (string) get_post_meta( $post_id, '_hmp_url', true );
}
/**
* Gets the current posts related work array (post ids).
*
* @param post id or object $post. (default: global $post)
* @return array (post ids)
*/
function hmp_get_related_work( $post_id = null) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
return array_filter((array) get_post_meta( $post_id, '_hmp_related_work', true ));
}
/**
* Checks whether the post has a set of meta info e.g. brief, url, related_work
*
* @param post id or $post Post Object, default global $post;
* @param string comma seperated list of meta info. (default: 'url,related_work')
* @return bool
*/
function hmp_has_info( $post = null, $string = '_url,_related_work' ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
$infos = explode( ',', $string );
$return = false;
foreach ( $infos as $meta_key ) {
//backwards compat for no underscores on meta_keys
if ( in_array( $meta_key, array( 'brief', 'byline', 'url', 'related_work', 'thumbnail_id', 'hmp_gallery_images' ) ) ) {
$meta_key = '_' . $meta_key;
}
return (bool) get_post_meta( $post_id, $meta_key, true );
}
return false;
}
// terms
/**
* @see get_the_category()
*/
function hmp_get_the_category( $post = null ) {
if ( $post === null ) global $post;
if ( ! is_numeric( $post ) )
$post = $post->ID;
$categories = get_object_term_cache( $post, 'hmp-entry-category' );
if ( false === $categories ) {
$categories = wp_get_object_terms( $post, 'hmp-entry-category' );
wp_cache_add($post, $categories, 'hmp-entry-category_relationships');
}
if ( ! empty( $categories ) )
usort( $categories, '_usort_terms_by_name' );
else
$categories = array();
foreach ( (array) array_keys( $categories ) as $key ) {
_make_cat_compat( $categories[$key] );
}
return $categories;
}
/**
* @see get_category_link()
*/
function hmp_get_category_link( $id ) {
return get_term_link( $id, 'hmp-entry-category' );
}
/**
* @see in_category()
*/
function hmp_in_category( $category, $post = null ) {
if ( $post === null ) global $post;
if ( ! is_numeric( $post ) )
$post = $post->ID;
$r = is_object_in_term( $post, 'hmp-entry-category', $category );
if ( is_wp_error( $r ) )
return false;
return $r;
}
/**
* @see the_category()
*/
function hmp_the_category( $separator = '', $parents='', $post = null ) {
if ( $post === null ) global $post;
if ( ! is_numeric( $post ) )
$post = $post->ID;
$categories = hmp_get_the_category( $post );
$thelist = '';
if ( '' == $separator ) {
$thelist .= '<ul class="post-categories">';
foreach ( $categories as $category ) {
$thelist .= "\n\t<li>";
switch ( strtolower( $parents ) ) {
case 'single':
$thelist .= '<a href="' . hmp_get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all entries in %s" ), $category->name ) ) . '">';
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= $category->name.'</a></li>';
break;
case '':
default:
$thelist .= '<a href="' . hmp_get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all entries in %s" ), $category->name ) ) . '">' . $category->cat_name.'</a></li>';
}
}
$thelist .= '</ul>';
} else {
$i = 0;
foreach ( $categories as $category ) {
if ( 0 < $i )
$thelist .= $separator . ' ';
switch ( strtolower( $parents ) ) {
case 'single':
$thelist .= '<a href="' . hmp_get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all entries in %s" ), $category->name ) ) . '">';
$thelist .= "$category->cat_name</a>";
break;
case '':
default:
$thelist .= '<a href="' . hmp_get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all entries in %s" ), $category->name ) ) . '">' . $category->name.'</a>';
}
++$i;
}
}
return apply_filters( 'the_category', $thelist, $separator, $parents );
}
/**
* @see get_the_tags
*/
function hmp_get_the_tags( $id = 0 ) {
return apply_filters( 'get_the_tags', get_the_terms( $id, 'hmp-entry-tag' ) );
}
/**
* @see get_the_tag_list
*/
function hmp_get_the_tag_list( $before = '', $sep = '', $after = '' ) {
return apply_filters( 'the_tags', get_the_term_list( 0, 'hmp-entry-tag', $before, $sep, $after ), $before, $sep, $after);
}
/**
* @see the_tags
*/
function hmp_the_tags( $before = null, $sep = ', ', $after = '' ) {
if ( null === $before )
$before = __('Tags: ');
echo hmp_get_the_tag_list($before, $sep, $after);
}
// image functions
/**
*
* Easy function
*
* Output the whole gallery.
*
* Size is either keyword or array($width, $height, $crop [true/false])
*/
function hmp_the_gallery ( $post = null, $size = 'full', $before = null, $after = null, $show_main = true ) {
if ( $post === null ) global $post;
if ( ! is_numeric( $post ) )
$post = $post->ID;
$r = '';
if ( is_array( $size ) ) {
$size = 'width=' . $size[0] . '&height=' . $size[1] . '&crop=' . $size[2];
}
if ( $show_main ) {
$r .= $before;
$r .= hmp_get_the_main_image( $post, $size, $h = null );
$r .= $after;
}
$gallery = hmp_get_the_gallery_images( $post, $size );
if ( is_array( $gallery )) {
foreach ( $gallery as $img ) {
$r .= $before;
$r .= $img;
$r .= $after;
}
}
return $r;
}
/**
* Deprecated. Use hmp_get_the_main_image() instead.
* Get main image
* $width_or_size: either a width value or a string keyword (thumbnail, medium, large or full) - if keyword, $h and $crop is ignored.
*/
function hmp_get_main_image( $post_id = null, $width_or_size = 'full', $h = 0, $crop = false ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
$attachment_id = hmp_get_main_image_id( $post_id );
if ( ! $attachment_id )
return null;
$crop = $crop ? '1' : '0';
if ( is_numeric( $width_or_size ) ) {
return reset( wp_get_attachment_image_src( $attachment_id, "width=$width_or_size&height=$h&crop=$crop" ) );
} else {
return reset( wp_get_attachment_image_src( $attachment_id, $width_or_size ) );
}
}
/**
*
* Returns Main <img>
*
*/
function hmp_get_the_main_image( $post_id = null, $width_or_size = 'full', $h = 0, $crop = false ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
$attachment_id = hmp_get_main_image_id( $post_id );
if ( ! $attachment_id )
return null;
$crop = $crop ? '1' : '0';
if ( is_numeric( $width_or_size ) ) {
return wp_get_attachment_image( $attachment_id, "width=$width_or_size&height=$h&crop=$crop" );
} else {
return wp_get_attachment_image( $attachment_id, $width_or_size );
}
}
function hmp_get_main_image_id( $post_id = null ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
return (int) get_post_meta( $post_id, '_thumbnail_id', true );
}
/**
* Deprecated.
*
* Use hmp_get_the_gallery_images() instead
*
*/
function hmp_get_gallery_images( $post_id = null, $width_or_size = 'full', $h = 0, $crop = false ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
$images = array();
$attachment_ids = hmp_get_gallery_ids( $post_id );
if ( ! $attachment_ids )
return array();
if ( is_numeric( $width_or_size ) ) {
$crop = $crop ? '1' : '0';
foreach ( $attachment_ids as $id ) {
if ( $image = wp_get_attachment_image_src( $id, "width=$width_or_size&height=$h&crop=$crop" ) )
$images[$id] = reset( $image );
}
} else {
foreach ( $attachment_ids as $id ) {
if ( $image = wp_get_attachment_image_src( $id, $width_or_size ) )
$images[$id] = reset( $image );
}
}
return $images;
}
/**
*
* Return an array of <img> for gallery.
* Does not return main image.
*
*/
function hmp_get_the_gallery_images( $post_id = null, $width_or_size = 'full', $h = 0, $crop = false ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
$images = array();
$attachment_ids = hmp_get_gallery_ids( $post_id );
if ( ! $attachment_ids )
return array();
if ( is_numeric( $width_or_size ) ) {
$crop = $crop ? '1' : '0';
foreach ( $attachment_ids as $id ) {
$images[$id] = wp_get_attachment_image( $id, "width=$width_or_size&height=$h&crop=$crop" );
}
} else {
foreach ( $attachment_ids as $id ) {
$images[$id] = wp_get_attachment_image( $id, $width_or_size );
}
}
return $images;
}
function hmp_get_gallery_image( $id, $w = 0, $h = 0, $crop = false ) {
return hm_phpthumb_it( get_attached_file( $id ), $w, $h, $crop );
}
function hmp_get_gallery_ids( $post_id = null ) {
if ( $post_id === null )
$post_id = get_the_ID();
if ( ! is_numeric( $post_id ) )
$post_id = $post_id->ID;
return array_filter( (array) get_post_meta( $post_id, '_hmp_gallery_images', true ) );
}
/**
* Get the array of wp_query vars that HMP Portfolio uses
*
* @return array
*/
function hmp_default_query_vars() {
$vars = array( 'post_type' => 'hmp-entry' );
return $vars;
}
function hmp_is_home() {
global $wp_query;
return (bool) $wp_query->is_portfolio || $wp_query->query['is_portfolio'];
}
function hmp_is_single() {
global $wp_query;
return (bool) $wp_query->is_portfolio_single || $wp_query->query['is_portfolio_single'];
}
function hmp_is_portfolio() {
return hmp_is_home() || hmp_is_single();
}
function hmp_get_single_permalink_structure() {
$single_base = get_option( 'hmp_single_base', '/%category%/' );
$single_base = '/' . trim( $single_base, '/' );
$single_base = str_replace( array( '%category%' ), '%hmp-entry-category%', $single_base );
return get_option('hmp_url_base', 'portfolio') . $single_base;
}