This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-tags.php
389 lines (326 loc) · 14 KB
/
template-tags.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
<?php
/******************************************************************************
* =COPYRIGHT
*****************************************************************************/
/* Copyright 2013 Atte Moisio (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/******************************************************************************
* = TEMPLATE TAGS
**************************************************************************** */
/**
* Display or retrieve the current event start date.
*
* @since 1.3.0
*
* @param string $before Optional. Content to prepend to the title.
* @param string $after Optional. Content to append to the title.
* @param string $format Optional. See PHP date() for formatting options.
* @param bool $echo Optional, default to true. Whether to display or return.
* @return null|string Null on no title. String if $echo parameter is false.
*/
function am_the_startdate($format = 'Y-m-d H:i:s', $before = '', $after = '', $echo = true) {
$date = am_get_the_startdate($format);
if ( strlen($date) == 0 )
return;
$date = $before . $date . $after;
if ( $echo )
echo $date;
else
return $date;
}
/**
* Retrieve event start date.
*
* @since 1.3.0
*
* @param mixed $post Optional. Post ID or object.
* @param string $format Optional. See PHP date() for formatting options.
* @return string
*/
function am_get_the_startdate( $format = 'Y-m-d H:i:s', $post = 0 ) {
$post = get_post( $post );
$id = isset( $post->ID ) ? $post->ID : 0;
$date = get_post_meta($id, 'am_startdate', true);
if ( strlen($format) != 0)
$date = date($format, strtotime($date));
return $date;
}
/**
* Display or retrieve the current event start date.
*
* @since 1.3.0
*
* @param string $before Optional. Content to prepend to the title.
* @param string $after Optional. Content to append to the title.
* @param string $format Optional. See PHP date() for formatting options.
* @param bool $echo Optional, default to true. Whether to display or return.
* @return null|string Null on no title. String if $echo parameter is false.
*/
function am_the_enddate($format = 'Y-m-d H:i:s', $before = '', $after = '', $echo = true) {
$date = am_get_the_enddate($format);
if ( strlen($date) == 0 )
return;
$date = $before . $date . $after;
if ( $echo ) {
echo $date;
} else {
return $date;
}
}
/**
* Retrieve event start date.
*
* @since 1.3.0
*
* @param mixed $post Optional. Post ID or object.
* @param string $format Optional. See PHP date() for formatting options.
* @return string
*/
function am_get_the_enddate( $format = 'Y-m-d H:i:s', $post = 0 ) {
$post = get_post( $post );
$id = isset( $post->ID ) ? $post->ID : 0;
$date = get_post_meta($id, 'am_enddate', true);
if ( strlen($format) != 0)
$date = date($format, strtotime($date));
return $date;
}
/**
* Retrieve event venues.
*
* @since 1.3.0
* @uses $post
*
* @param int $id Optional, default to current post ID. The post ID.
* @return array
*/
function am_get_the_venue( $id = false ) {
$venues = get_the_terms( $id, 'am_venues' );
if ( ! $venues || is_wp_error( $venues ) )
$venues = array();
$venues = array_values( $venues );
// Filter name is plural because we return alot of categories (possibly more than #13237) not just one
return apply_filters( 'am_get_the_venues', $venues );
}
/**
* Check if the current event in within any of the given venues.
*
* The given venues are checked against the event's venues' term_ids, names and slugs.
* Venues given as integers will only be checked against the post's venues' term_ids.
*
* @since 1.3.0
*
* @param int|string|array $venue Venue ID, name or slug, or array of said.
* @param int|object $post Optional. Event post to check instead of the current post.
* @return bool True if the current post is in any of the given venues.
*/
function am_in_venue( $venue, $post = null ) {
if ( empty( $venue ) ) {
return false;
}
return has_term( $venue, 'am_venues', $post );
}
/**
* Retrieve venue list in either HTML list or custom format.
*
* @since 1.3.0
*
* @param string $separator Optional, default is empty string. Separator for between the venues.
* @param string $parents Optional. How to display the parents.
* @param int $post_id Optional. Post ID to retrieve venues.
* @return string
*/
function am_get_the_venue_list( $separator = '', $parents='', $post_id = false ) {
global $wp_rewrite;
if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'am_venues' ) )
return apply_filters( 'am_the_venue', '', $separator, $parents );
$venues = am_get_the_venue( $post_id );
if ( empty( $venues ) )
return apply_filters( 'am_the_venue', _x( 'Unspecified', 'Venue', 'am-events' ), $separator, $parents );
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="venue tag"' : 'rel="venue"';
$thelist = '';
if ( '' == $separator ) {
$thelist .= '<ul class="post-venues">';
foreach ( $venues as $venue ) {
$thelist .= "\n\t<li>";
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $venue->parent )
$thelist .= get_category_parents( $venue->parent, true, $separator );
$thelist .= '<a href="' . esc_url( get_category_link( $venue->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", 'Venue', 'am-events' ), $venue->name ) ) . '" ' . $rel . '>' . $venue->name.'</a></li>';
break;
case 'single':
$thelist .= '<a href="' . esc_url( get_category_link( $venue->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", 'Venue', 'am-events' ), $venue->name ) ) . '" ' . $rel . '>';
if ( $venue->parent )
$thelist .= get_category_parents( $venue->parent, false, $separator );
$thelist .= $venue->name.'</a></li>';
break;
case '':
default:
$thelist .= '<a href="' . esc_url( get_category_link( $venue->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", 'Venue', 'am-events' ), $venue->name ) ) . '" ' . $rel . '>' . $venue->name.'</a></li>';
}
}
$thelist .= '</ul>';
} else {
$i = 0;
foreach ( $venues as $venue ) {
if ( 0 < $i )
$thelist .= $separator;
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $venue->parent )
$thelist .= get_category_parents( $venue->parent, true, $separator );
$thelist .= '<a href="' . esc_url( get_category_link( $venue->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", 'Venue', 'am-events' ), $venue->name ) ) . '" ' . $rel . '>' . $venue->name.'</a>';
break;
case 'single':
$thelist .= '<a href="' . esc_url( get_category_link( $venue->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", 'Venue', 'am-events' ), $venue->name ) ) . '" ' . $rel . '>';
if ( $venue->parent )
$thelist .= get_category_parents( $venue->parent, false, $separator );
$thelist .= "$venue->name</a>";
break;
case '':
default:
$thelist .= '<a href="' . esc_url( get_category_link( $venue->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", 'Venue', 'am-events' ), $venue->name ) ) . '" ' . $rel . '>' . $venue->name.'</a>';
}
++$i;
}
}
return apply_filters( 'am_the_venue', $thelist, $separator, $parents );
}
/**
* Display the venue list for the event post.
*
* @since 1.3.0
*
* @param string $separator Optional, default is empty string. Separator for between the venues.
* @param string $parents Optional. How to display the parents.
* @param int $post_id Optional. Post ID to retrieve categories.
*/
function am_the_venue( $separator = '', $parents='', $post_id = false ) {
echo am_get_the_venue_list( $separator, $parents, $post_id );
}
/**
* Retrieve event categories.
*
* @since 1.3.0
* @uses $post
*
* @param int $id Optional, default to current event post ID. The event post ID.
* @return array
*/
function am_get_the_event_category( $id = false ) {
$categories = get_the_terms( $id, 'am_event_categories' );
if ( ! $categories || is_wp_error( $categories ) )
$categories = array();
$categories = array_values( $categories );
// Filter name is plural because we return alot of categories (possibly more than #13237) not just one
return apply_filters( 'am_get_the_event_categories', $categories );
}
/**
* Retrieve event category list in either HTML list or custom format.
*
* @since 1.3.0
*
* @param string $separator Optional, default is empty string. Separator for between the categories.
* @param string $parents Optional. How to display the parents.
* @param int $post_id Optional. Event post ID to retrieve categories.
* @return string
*/
function am_get_the_event_category_list( $separator = '', $parents='', $post_id = false ) {
global $wp_rewrite;
if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'am_event_categories' ) )
return apply_filters( 'am_the_event_category', '', $separator, $parents );
$categories = am_get_the_event_category( $post_id );
if ( empty( $categories ) )
return apply_filters( 'am_the_event_category', __( 'Uncategorized' ), $separator, $parents );
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
$thelist = '';
if ( '' == $separator ) {
$thelist .= '<ul class="event-categories">';
foreach ( $categories as $category ) {
$thelist .= "\n\t<li>";
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", "Category", "am-events" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
break;
case 'single':
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", "Category", "am-events" ), $category->name ) ) . '" ' . $rel . '>';
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= $category->name.'</a></li>';
break;
case '':
default:
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", "Category", "am-events" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
}
}
$thelist .= '</ul>';
} else {
$i = 0;
foreach ( $categories as $category ) {
if ( 0 < $i )
$thelist .= $separator;
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", "Category", "am-events" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
break;
case 'single':
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", "Category", "am-events" ), $category->name ) ) . '" ' . $rel . '>';
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= "$category->name</a>";
break;
case '':
default:
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( _x( "View all events in %s", "Category", "am-events" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
}
++$i;
}
}
return apply_filters( 'am_the_event_category', $thelist, $separator, $parents );
}
/**
* Check if the current event in within any of the given event categories.
*
* The given categories are checked against the post's categories' term_ids, names and slugs.
* Categories given as integers will only be checked against the post's categories' term_ids.
*
*
* @since 1.3.0
*
* @param int|string|array $eventCategory Event category ID, name or slug, or array of said.
* @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
* @return bool True if the current post is in any of the given categories.
*/
function am_in_event_category( $event_category, $post = null ) {
if ( empty( $event_category ) ) {
return false;
}
return has_term( $event_category, 'am_event_categories', $post );
}
/**
* Display the event category list for the event.
*
* @since 1.3.0
*
* @param string $separator Optional, default is empty string. Separator for between the event categories.
* @param string $parents Optional. How to display the parents.
* @param int $post_id Optional. Post ID to retrieve event categories.
*/
function am_the_event_category( $separator = '', $parents='', $post_id = false ) {
echo am_get_the_event_category_list( $separator, $parents, $post_id );
}
?>