forked from stephenharris/Event-Organiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-organiser-venue-functions.php
552 lines (448 loc) · 14.2 KB
/
event-organiser-venue-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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
/**
* Venue related functions
*/
/**
* Returns the id of the venue of an event.
* Can be used inside the loop to output the
* venue id of the current event.
*
* @param (int) post id
* @return (int) venue id
*
* @since 1.0.0
*/
function eo_get_venue($event_id=''){
global $post;
$event = $post;
if( !empty($event_id) ){
$post_id = $event_id;
}else{
$post_id = (isset($post->ID) ? $post->ID : 0);
}
if( empty($post_id) )
return false;
$venue = get_the_terms($post_id,'event-venue');
if ( empty($venue) || is_wp_error( $venue ) )
return false;
$venue = array_pop($venue);
return (int) $venue->term_id;
}
/**
* Returns the slug of the venue of an event.
* Can be used inside the loop to output the
* venue slug of the current event.
*
* @param (int) post id
* @return (string) venue slug
*
* @since 1.0.0
*/
function eo_get_venue_slug($event_id=''){
global $post;
$event = $post;
if( !empty($event_id) ){
$post_id = $event_id;
}else{
$post_id = (isset($post->ID) ? $post->ID : 0);
}
$venue = get_the_terms($post_id,'event-venue');
if ( empty($venue) || is_wp_error( $venue ) )
return false;
$venue = array_pop($venue);
return $venue->slug;
}
/*
* Venue Utility Functions
*/
function eo_get_venue_id_by_slugorid($venue_slug_or_id=''){
$venue = $venue_slug_or_id;
if( empty($venue) )
return eo_get_venue();
if( is_int($venue) )
return (int) $venue;
$venue = get_term_by('slug', $venue, 'event-venue');
return (int) $venue->term_id;
}
// Calls WordPress' get_term_by
function eo_get_venue_by($field,$value,$output = OBJECT){
$venue = get_term_by($field, $value, 'event-venue');
return $venue;
}
/*
* Venue meta
*/
function eo_get_venue_meta($venue_id, $key, $single=true){
return get_metadata('eo_venue', $venue_id, $key, $single);
}
function eo_add_venue_meta($venue_id, $key, $value, $unique = false ){
return add_metadata('eo_venue',$venue_id, $key, $value, $unique);
}
function eo_update_venue_meta($venue_id, $key, $value, $prev_value=''){
return update_metadata('eo_venue', $venue_id, $key, $value, $prev_value);
}
function eo_delete_venue_meta($venue_id, $key, $value = '', $delete_all = false ){
return delete_metadata('eo_venue',$venue_id, $key, $value, $delete_all);
}
/**
* Returns the name of the venue of the event.
* Can be used inside the loop to output the
* venue's name of the current event.
*
* @param (int) venue id or (string) venue slug
*
* @since 1.0.0
*/
function eo_get_venue_name($venue_slug_or_id=''){
$venue_id = eo_get_venue_id_by_slugorid($venue_slug_or_id);
$venue = get_term($venue_id,'event-venue');
if ( empty($venue) || is_wp_error( $venue ) )
return false;
return $venue->name;
}
/**
* Echos the venue of the event
*
* @uses eo_get_venue_name
* @param (int) venue id or (string) venue slug
*
* @since 1.0.0
*/
function eo_venue_name($venue_slug_or_id=''){
echo eo_get_venue_name($venue_slug_or_id);
}
/**
* Returns the description of the venue
* Can be used inside the loop to output the
* venue's description of the current event.
*
* @param (int) venue id or (string) venue slug
*
* @since 1.0.0
*/
function eo_get_venue_description($venue_slug_or_id=''){
$venue_id = eo_get_venue_id_by_slugorid($venue_slug_or_id);
$description = eo_get_venue_meta($venue_id,'_description');
$description = wptexturize($description);
$description = convert_chars($description);
$description = wpautop($description);
$description = shortcode_unautop($description);
$description = do_shortcode($description);
return $description;
}
/**
* Echos the description of the venue
* Can be used inside the loop to output the
* venue's description of the current event.
*
* @param (int) venue id or (string) venue slug
*
* @uses eo_get_venue_description
* @since 1.0.0
*/
function eo_venue_description($venue_slug_or_id=''){
echo eo_get_venue_description($venue_slug_or_id);
}
/**
* Returns an array (latitude, longitude) of the venue
* Can be used inside the loop to return an array of the
* latitude and longitude of the venue of the current event.
*
* @param (int) venue id or (string) venue slug
* @return (array of floats) (latitude,longitude)
*
* @since 1.0.0
*/
function eo_get_venue_latlng($venue_slug_or_id=''){
$lat = eo_get_venue_lat($venue_slug_or_id);
$lng = eo_get_venue_lng($venue_slug_or_id);
return array('lat'=>$lat,'lng'=>$lng);
}
function eo_get_venue_lat($venue_slug_or_id=''){
$venue_id = eo_get_venue_id_by_slugorid($venue_slug_or_id);
$lat = eo_get_venue_meta($venue_id,'_lat');
$lat = ! empty($lat) ? $lat : 0;
return $lat;
}
function eo_get_venue_lng($venue_slug_or_id=''){
$venue_id = eo_get_venue_id_by_slugorid($venue_slug_or_id);
$lng = eo_get_venue_meta($venue_id,'_lng');
$lng = ! empty($lng) ? $lng : 0;
return $lng;
}
/**
* Echos the latitude of the venue of the event
* Can be used inside the loop to output the
* latitude of the venue of the current event.
*
* @param (int) venue id or (string) venue slug
*
* @since 1.0.0
*/
function eo_venue_lat($venue_slug_or_id=''){
echo eo_get_venue_lat($venue_slug_or_id);
}
/**
* Echos the longtitude of the venue of the event
* Can be used inside the loop to output the
* longitude of the venue of the current event.
*
* @param (int) venue id or (string) venue slug
*
* @since 1.0.0
*/
function eo_venue_lng($venue_slug_or_id=''){
echo eo_get_venue_lng($venue_slug_or_id);
}
/**
* Returns the permalink of a venue
* Can be used inside the loop to output the
* venue's link of the current event.
*
* @param (int) venue id or (string) venue slug
* @return (string) link of venue
*
* @since 1.0.0
*/
function eo_get_venue_link($venue_slug_or_id=''){
$venue_id = eo_get_venue_id_by_slugorid($venue_slug_or_id);
return get_term_link( $venue_id, 'event-venue' );
}
/**
* Echos the permalink of the event's venue
* Can be used inside the loop to output the
* venue's link of the current event.
*
* @param (int) venue id or (string) venue slug
*
* @since 1.0.0
*/
function eo_venue_link($venue_slug_or_id=''){
$venue_id = eo_get_venue_id_by_slugorid($venue_slug_or_id);
echo eo_get_venue_link($venue_slug_or_id);
}
/**
* Returns an array with address details of the event's venue
* Can be used inside the loop to return an array of venue address
* of the current event.
*
* @param (int) venue id or (string) venue slug
*
* @since 1.0.0
*/
function eo_get_venue_address($venue_slug_or_id=''){
$address=array();
$venue_id = eo_get_venue_id_by_slugorid($venue_slug_or_id);
$address['address'] = eo_get_venue_meta($venue_id,'_address');
$address['postcode'] = eo_get_venue_meta($venue_id,'_postcode');
$address['country'] = eo_get_venue_meta($venue_id,'_country');
return $address;
}
/**
* Wrapper for get_terms. Maybe depcreciate in favour of eo_get_venues?
*/
function eo_get_the_venues($args=array()){
return eo_get_venues($args);
}
//Used in ajax file
function eo_get_venues($args=array()){
$args = wp_parse_args( $args, array('hide_empty'=>0 ) );
return get_terms('event-venue',$args);
}
function eo_event_venue_dropdown( $args = '' ) {
$defaults = array(
'show_option_all' =>'',
'echo' => 1,
'selected' => 0,
'name' => 'event-venue',
'id' => '',
'class' => 'postform event-organiser event-venue-dropdown event-dropdown',
'tab_index' => 0,
);
$defaults['selected'] = (is_tax('event-venue') ? get_query_var('event-venue') : 0);
$r = wp_parse_args( $args, $defaults );
$r['taxonomy']='event-venue';
extract( $r );
$tab_index_attribute = '';
if ( (int) $tab_index > 0 )
$tab_index_attribute = " tabindex=\"$tab_index\"";
$categories = get_terms($taxonomy, $r );
$name = esc_attr( $name );
$class = esc_attr( $class );
$id = $id ? esc_attr( $id ) : $name;
$output = "<select style='width:150px' name='$name' id='$id' class='$class' $tab_index_attribute>\n";
if ( $show_option_all ) {
$output .= '<option '.selected($selected,0,false).' value="0">'.$show_option_all.'</option>';
}
if ( ! empty( $categories ) ) {
foreach ($categories as $term):
$output .= '<option value="'.$term->slug.'"'.selected($selected,$term->slug,false).'>'.$term->name.'</option>';
endforeach;
}
$output .= "</select>\n";
if ( $echo )
echo $output;
return $output;
}
/**
* Updates new venue in the database.
*
* Calls wp_insert_term to create the taxonomy term
* Adds venue meta data to database (for 'core' meta keys)
*
* The $args is an array - the same as that accepted by wp_update_term
* The $args array can also accept the following keys:
* * description, address, postcode, country, latitude, longtitude
*
* @since 1.4.0
*
* @uses wp_update_term to update venue (taxonomy) term
* @uses do_action() Calls 'eventorganiser_save_venue' hook with the venue id
*
* @param (int) the Term ID of the venue to update
* @param array $args as accepted by wp_insert_term and including the default meta data
* @return array|WP_Error array of term ID and term-taxonomy ID or a WP_Error
*/
function eo_update_venue($venue_id, $args=array()){
$term_args = array_intersect_key($args, array('name'=>'','term_id'=>'','term_group'=>'','term_taxonomy_id'=>'','alias_of'=>'','parent'=>0,'slug'=>'','count'=>''));
$meta_args = array_intersect_key($args, array('description'=>'','address'=>'','postcode'=>'','country'=>'','latitude'=>'','longtitude'=>''));
$venue_id = (int) $venue_id;
//Update taxonomy table
$resp = wp_update_term($venue_id,'event-venue', $term_args);
if( is_wp_error($resp) ){
return $resp;
}
$venue_id = (int) $resp['term_id'];
foreach( $meta_args as $key => $value ){
switch($key):
case 'latitude':
$meta_key = '_lat';
break;
case 'longtitude':
$meta_key = '_lng';
break;
default:
$meta_key = '_'.$key;
break;
endswitch;
$validated_value = eventorganiser_sanitize_meta($meta_key, $value);
update_metadata('eo_venue', $venue_id, $meta_key, $validated_value);
}
do_action('eventorganiser_save_venue',$venue_id);
return array('term_id' => $venue_id, 'term_taxonomy_id' => $resp['term_taxonomy_id']);
}
/**
* Adds a new venue to the database.
*
* Calls wp_insert_term to create the taxonomy term
* Adds venue meta data to database (for 'core' meta keys)
*
* The $args is an array - the same as that accepted by wp_insert_term
* The $args array can also accept the following keys:
* * description, address, postcode, country, latitude, longtitude
*
* @since 1.4.0
*
* @uses wp_insert_term to create venue (taxonomy) term
* @uses do_action() Calls 'eventorganiser_insert_venue' hook with the venue id
* @uses do_action() Calls 'eventorganiser_save_venue' hook with the venue id
*
* @param string $name the venue to insert
* @param array $args as accepted by wp_insert_term and including the default meta data
* @return array|WP_Error array of term ID and term-taxonomy ID or a WP_Error
*/
function eo_insert_venue($name, $args=array()){
$term_args = array_intersect_key($args, array('name'=>'','term_id'=>'','term_group'=>'','term_taxonomy_id'=>'','alias_of'=>'','parent'=>0,'slug'=>'','count'=>''));
$meta_args = array_intersect_key($args, array('description'=>'','address'=>'','postcode'=>'','country'=>'','latitude'=>'','longtitude'=>''));
$resp = wp_insert_term($name,'event-venue',$term_args);
if(is_wp_error($resp)){
return $resp;
}
$venue_id = (int) $resp['term_id'];
foreach( $meta_args as $key => $value ){
switch($key):
case 'latitude':
$meta_key = '_lat';
break;
case 'longtitude':
$meta_key = '_lng';
break;
default:
$meta_key = '_'.$key;
break;
endswitch;
$validated_value = eventorganiser_sanitize_meta($meta_key, $value);
if( !empty($validated_value) )
add_metadata('eo_venue', $venue_id, $meta_key, $validated_value, true);
}
do_action('eventorganiser_insert_venue',$venue_id);
do_action('eventorganiser_save_venue',$venue_id);
return array('term_id' => $venue_id, 'term_taxonomy_id' => $resp['term_taxonomy_id']);
}
/**
* Deletes a venue in the database.
*
* Calls wp_delete_term to delete the taxonomy term
* Deletes all the venue's meta
*
* @since 1.4.0
*
* @uses wp_delete_term to delete venue (taxonomy) term
* @uses do_action() Calls 'eventorganiser_delete_venue' hook with the venue id
*
* @param (int) the Term ID of the venue to update
* @return bool|WP_Error false or error on failure. True after sucessfully deleting the venue and its meta data.
*/
function eo_delete_venue($venue_id){
global $wpdb;
$resp =wp_delete_term( $venue_id, 'event-venue');
if( is_wp_error($resp) || false === $resp ){
return $resp;
}
$venue_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->eo_venuemeta WHERE eo_venue_id = %d ", $venue_id ));
if ( !empty($venue_meta_ids) ) {
$in_venue_meta_ids = "'" . implode("', '", $venue_meta_ids) . "'";
$wpdb->query( "DELETE FROM $wpdb->eo_venuemeta WHERE meta_id IN($in_venue_meta_ids)" );
}
do_action('eventorganiser_delete_venue',$venue_id);
return true;
}
function eventorganiser_sanitize_meta($key,$value){
switch($key):
case '_address':
case '_postcode':
case '_country':
$value = sanitize_text_field($value);
break;
case '_description':
$value = wp_filter_post_kses($value);
break;
case '_lat':
case '_lng':
//Cast as float and then string: make sure string uses . not , for decimal point
$value = floatval($value);
$value = number_format($value, 6);
break;
default:
$value = false;
endswitch;
return $value;
}
function eventorganiser_venue_dropdown($post_id=0,$args){
$venues = get_terms('event-venue', array('hide_empty'=>false));
$current = (int) eo_get_venue($post_id);
$id = (!empty($args['id']) ? 'id="'.esc_attr($args['id']).'"' : '');
$name = (!empty($args['name']) ? 'name="'.esc_attr($args['name']).'"' : '');
?>
<select <?php echo $id.' '.$name; ?>>
<option><?php _e("Select a venue",'eventorganiser');?></option>
<?php foreach ($venues as $venue):?>
<option <?php selected($venue->term_id,$current);?> value="<?php echo $venue->term_id;?>"><?php echo $venue->name; ?></option>
<?php endforeach;?>
</select><?php
}
function eo_get_venue_map($venue_slug_or_id='', $args=array()){
$venue_id = eo_get_venue_id_by_slugorid($venue_slug_or_id);
return EventOrganiser_Shortcodes::get_venue_map($venue_id, $args=array());
}
?>