PluginProbe
Themify Event Post / 1.3.2
Themify Event Post v1.3.2
1.3.7 trunk 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6
themify-event-post / includes / functions.php

functions.php in Themify Event Post 1.3.2, at includes/functions.php

1,045 lines 31.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! function_exists( 'themify_event_post_map' ) ) :
4 /**
5 * Render a Google Maps instance
6 *
7 * @return string
8 */
9 function themify_event_post_map( $args = array() ) {
10
11 $args = wp_parse_args( $args, array(
12 'address' => '99 Blue Jays Way, Toronto, Ontario, Canada',
13 'width' => '100%',
14 'height' => '300px',
15 'zoom' => 15,
16 'type' => 'ROADMAP',
17 'scroll_wheel' => 'yes',
18 'draggable_disable_mobile_map' => 'yes',
19 'draggable' => 'yes',
20 ) );
21
22 if ( class_exists( 'Themify_Google_Maps',false ) ) {
23 ob_start();
24 the_widget( 'Themify_Google_Maps', array(
25 'address_map' => $args['address'],
26 'width' => str_replace( 'px', '', $args['width'] ),
27 'height' => str_replace( 'px', '', $args['height'] ),
28 'zoom_map' => $args['zoom'],
29 'type_map' => $args['type'],
30 'scrollwheel_map' => $args['scroll_wheel'] === 'no' ? 'disable' : 'enable',
31 'draggable_map' => $args['draggable'] === 'no' ? 'disable' : 'enable',
32 'draggable_disable_mobile_map' => $args['draggable_disable_mobile_map'],
33 ) );
34 return ob_get_clean();
35 }
36
37 extract( $args );
38
39 /* if no unit is provided for width and height, use "px" */
40 if( ! preg_match( '/[px|%]$/', $width ) ) {
41 $width = $width . 'px';
42 }
43 if( ! preg_match( '/[px|%]$/', $height ) ) {
44 $height = $height . 'px';
45 }
46
47 if ( 'yes' === $draggable && 'yes' === $draggable_disable_mobile_map && wp_is_mobile() ) {
48 $draggable = 'disable';
49 }
50 $num = rand( 0, 10000 );
51 $data['address'] = $address;
52 $data['num'] = $num;
53 $data['zoom'] = $zoom;
54 $data['type'] = $type;
55 $data['scroll'] = $scroll_wheel ==='yes';
56 $data['drag'] = $draggable==='yes';
57 return '
58 <div class="tep_map_container">
59 <div data-map=\''.esc_attr( json_encode( $data ) ).'\' id="themify_map_canvas_' . esc_attr( $num ) . '" style="display: block;width:' . esc_attr( $width ) . ';height:' . esc_attr( $height ) . ';" class="map-container tep_map"></div>
60 </div>';
61 }
62 endif;
63
64 if ( ! function_exists( 'themify_event_post_get_repeat_date' ) ) :
65 /**
66 * Displays repeated dates
67 *
68 * @since 1.0.0
69 */
70 function themify_event_post_get_repeat_date( $type, $number, $date, $time ) {
71 $result = '';
72 switch( $type ) {
73 case 'day':
74 $result = '<span class="event-day">'. sprintf( _n( 'Every day','Every %s days', $number, 'themify-event-post' ), $number ).'</span>';
75 break;
76 case 'week':
77 $result = '<span class="event-day">'. sprintf( _n( 'Every week on %2$s', 'Every %s weeks on %s', $number, 'themify-event-post' ), $number , date_i18n( 'l', strtotime( $date ) ) ).'</span>';
78 break;
79 case 'year':
80 $result = '<span class="event-day">'. sprintf( _n( 'Every year on %2$s', 'Every %s years on %s', $number, 'themify-event-post' ), $number, date_i18n( 'M d', strtotime( $date ) ) ).'</span>';
81 break;
82 }
83 if ( $result && $time ) {
84 $result .= '<span class="event-time-at">' ._x( ' @ ', 'Connector between date and time (with spaces around itself) in event date and time.', 'themify-event-post' ). '</span> <span class="event-time">'.date_i18n( get_option( 'time_format' ), strtotime( $time ) ).'</span>' ;
85 }
86
87 return apply_filters( 'themify_event_date_repeat', $result, $type, $number, $date, $time );
88 }
89 endif;
90
91 function themify_event_is_repeated() {
92 $repeat = get_post_meta( get_the_id(), 'repeat', true );
93 if ( $repeat ) {
94 $repeat_x = intval( get_post_meta( get_the_id(), 'repeat_x', true ) );
95 if ( $repeat_x <= 0 ) {
96 $repeat = false;
97 }
98 }
99
100 return $repeat;
101 }
102
103 if ( ! function_exists( 'themify_event_post_date' ) ) :
104 /**
105 * Displays Start Date and End Date properly formatted
106 *
107 * @since 1.0.0
108 */
109 function themify_event_post_date( $date_format = null, $time_format = null ) {
110 if(get_post_meta( get_the_id(), 'to_be_announced', true )==='on'){
111 echo '<time class="tep_date"><span class="event-start-date">'.__('To be announced','themify-event-post').'</span></time>';
112 return;
113 }
114 $start_date = get_post_meta( get_the_id(), 'start_date', true );
115 $end_date = get_post_meta( get_the_id(), 'end_date', true );
116 $repeat = themify_event_is_repeated();
117 $hide_event_end = get_post_meta( get_the_id(), 'event_end_date_hide', true );
118
119 if ( $start_date || $end_date ) {
120 $date_format = $date_format ?: get_option( 'date_format' );
121 $time_format = $time_format ?: get_option( 'time_format' );
122 echo '<time class="tep_date">';
123
124 if( $start_date ) {
125 $stamp = date_create( $start_date, new DateTimeZone( 'UTC' ) )->format( 'U' );
126 echo '<span class="event-start-date">';
127 $start_date_parts = explode( ' ', $start_date );
128 if( $repeat ) {
129 echo themify_event_post_get_repeat_date( $repeat, intval( get_post_meta( get_the_id(), 'repeat_x', true ) ), $start_date_parts[0], $start_date_parts[1] );
130 } else {
131 echo '<span class="event-day"> '.date_i18n( $date_format, $stamp ) .'</span> <span class="event-time-at">'. _x( ' @ ', 'Connector between date and time (with spaces around itself) in event date and time.', 'themify-event-post' ) .'</span> <span class="event-time">' . date_i18n( $time_format, $stamp ) .'</span>';
132 }
133 echo '</span>';
134 }
135 if( !$hide_event_end && ! $repeat && $end_date ) {
136 $stamp = date_create( $end_date, new DateTimeZone( 'UTC' ) )->format( 'U' );
137 echo '<span class="event-end-date">';
138 $end_date_parts = explode( ' ', $end_date );
139 echo ! isset( $start_date_parts ) || $start_date_parts[0] != $end_date_parts[0] ?'<span class="event-date-separator">'. _x( ' - ', 'Character to provide a hint that this is the event end date and time.', 'themify-event-post' ) .'</span>
140 <span class="event-day">' . date_i18n( $date_format, $stamp ) .'</span>' :' <span class="event-date-separator"> - </span>';
141 if ( isset( $start_date_parts[0] ) && $start_date_parts[0] != $end_date_parts[0] ) {
142 echo '<span class="event-time-at">'. _x( ' @ ', 'Connector between date and time (with spaces around itself) in event date and time.', 'themify-event-post' ).'</span>';
143 }
144 echo '<span class="event-time">' . date_i18n( $time_format, $stamp ) . '</span>';
145 echo '</span>';
146 }
147
148 echo '</time>';
149 }
150 }
151 endif;
152
153 if ( ! function_exists( 'themify_event_type' ) ) :
154 /**
155 * Displays event type if set
156 *
157 * @since 1.1.2
158 */
159 function themify_event_type() {
160 $post_id = get_the_ID();
161 $e_type=get_post_meta( $post_id, 'event_attendance', true );
162 if(empty($e_type)){
163 return;
164 }
165 echo sprintf('<div class="tep_type">%s</div>',$e_type);
166 }
167 endif;
168
169 if ( ! function_exists( 'themify_event_organizer' ) ) :
170 /**
171 * Displays organizer meta
172 *
173 * @since 1.1.2
174 */
175 function themify_event_organizer() {
176 $post_id = get_the_ID();
177 $name=get_post_meta( $post_id, 'organizer_name', true );
178 if(empty($name)){
179 return;
180 }
181 $url=esc_url(get_post_meta( $post_id, 'organizer_url', true ));
182 ob_start();
183 ?>
184 <div class="tep_organizer">
185 <?php if(!empty($url)): ?>
186 <a href="<?php echo $url ?>" target="_blank">
187 <?php endif; ?>
188 <?php echo $name ?>
189 <?php if(!empty($url)): ?>
190 </a>
191 <?php endif; ?>
192 </div>
193 <?php
194 ob_end_flush();
195 }
196 endif;
197
198 if ( ! function_exists( 'themify_event_performer' ) ) :
199 /**
200 * Displays organizer meta
201 *
202 * @since 1.1.2
203 */
204 function themify_event_performer() {
205 $post_id = get_the_ID();
206 $name=get_post_meta( $post_id, 'performer_name', true );
207 if(empty($name)){
208 return;
209 }
210 ob_start();
211 ?>
212 <div class="tep_performer">
213 <?php echo $name ?>
214 </div>
215 <?php
216 ob_end_flush();
217 }
218 endif;
219
220 if ( ! function_exists( 'themify_event_post_pagenav' ) ) :
221 /**
222 * Renders page navigation for Event posts
223 *
224 * @return string
225 */
226 function themify_event_post_pagenav( $args = array() ) {
227 $args = wp_parse_args( $args, array(
228 'paged' => 1,
229 'total_posts' => 0,
230 'posts_per_page' => 0,
231 'pages_to_show' => 5,
232 'offset' => 0,
233 'before' => '',
234 'after' => '',
235 ) );
236 extract( $args );
237
238 // $query->found_posts does not take offset into account, we need to manually adjust that
239 if ( (int) $offset) {
240 $total_posts = $total_posts - (int) $offset;
241 }
242
243 $max_page = ceil( $total_posts / $posts_per_page );
244 $out = '';
245
246 if ( empty( $paged ) ) {
247 $paged = 1;
248 }
249 $pages_to_show_minus_1 = $pages_to_show - 1;
250 $half_page_start = floor($pages_to_show_minus_1 / 2);
251 $half_page_end = ceil($pages_to_show_minus_1 / 2);
252 $start_page = $paged - $half_page_start;
253 if ($start_page <= 0) {
254 $start_page = 1;
255 }
256 $end_page = $paged + $half_page_end;
257 if (($end_page - $start_page) != $pages_to_show_minus_1) {
258 $end_page = $start_page + $pages_to_show_minus_1;
259 }
260 if ($end_page > $max_page) {
261 $start_page = $max_page - $pages_to_show_minus_1;
262 $end_page = $max_page;
263 }
264 if ($start_page <= 0) {
265 $start_page = 1;
266 }
267
268 if ($max_page > 1) {
269
270 if ( class_exists( 'Themify_Enqueue_Assets',false ) && ! empty( Themify_Enqueue_Assets::$themeVersion ) && Themify_Enqueue_Assets::$themeVersion !== null ) {
271 Themify_Enqueue_Assets::loadThemeStyleModule( 'pagenav' );
272 }
273
274 $out .= $before . '<div class="pagenav tf_clearfix">';
275 if ($start_page >= 2 && $pages_to_show < $max_page) {
276 $first_page_text = "&laquo;";
277 $out .= '<a href="' . esc_url(get_pagenum_link()) . '" title="' . esc_attr($first_page_text) . '" class="number">' . $first_page_text . '</a>';
278 }
279 if ($pages_to_show < $max_page)
280 $out .= get_previous_posts_link('&lt;');
281 for ($i = $start_page; $i <= $end_page; $i++) {
282 if ($i == $paged) {
283 $out .= ' <span class="number current">' . $i . '</span> ';
284 } else {
285 $out .= ' <a href="' . esc_url(get_pagenum_link($i)) . '" class="number">' . $i . '</a> ';
286 }
287 }
288 if ($pages_to_show < $max_page)
289 $out .= get_next_posts_link('&gt;');
290 if ($end_page < $max_page) {
291 $last_page_text = "&raquo;";
292 $out .= '<a href="' . esc_url(get_pagenum_link($max_page)) . '" title="' . esc_attr($last_page_text) . '" class="number">' . $last_page_text . '</a>';
293 }
294 $out .= '</div>' . $after;
295 }
296 return $out;
297 }
298 endif;
299
300 if ( ! function_exists( 'themify_event_post_get_paged_query' ) ) :
301 /**
302 * Gets "paged" query var
303 *
304 * @return int
305 */
306 function themify_event_post_get_paged_query() {
307 global $wp;
308 $page = 1;
309 $qpaged = get_query_var( 'paged' );
310 if ( ! empty( $qpaged ) ) {
311 $page = $qpaged;
312 } else {
313 $qpaged = wp_parse_args( $wp->matched_query );
314 if ( isset( $qpaged['paged'] ) && $qpaged['paged'] > 0 ) {
315 $page = $qpaged['paged'];
316 }
317 }
318 return $page;
319 }
320 endif;
321
322 if ( ! function_exists( 'themify_event_post_get_image' ) ) :
323 /**
324 * Display post thumbnail
325 *
326 * @return string
327 * @since 1.0.8
328 */
329 function themify_event_post_get_image( $args = array() ) {
330 if ( ! has_post_thumbnail() ) {
331 return '';
332 }
333
334 global $wp_version;
335
336 /**
337 * List of parameters
338 * @var array
339 */
340 $args = wp_parse_args( $args, array(
341 'id' => '',
342 'ignore' => '',
343 'width' => '',
344 'height' => '',
345 'before' => '<figure class="tep_image">',
346 'after' => '</figure>',
347 'class' => '',
348 'alt' => '',
349 'title' => '',
350 'unlink' => false,
351 'image_meta' => true,
352 'crop' => true,
353 ) );
354 extract( $args );
355
356 $id = (int) get_post_thumbnail_id(); /* Image script works with thumbnail IDs as well as URLs, use ID which is faster */
357
358 $temp = themify_events_do_img( $id, $width, $height, (bool) $args['crop'] );
359 $img_url = $temp['url'];
360
361 // Build final image
362 $out = '';
363 if ( $args['image_meta'] == true ) {
364 $out .= "<meta itemprop=\"width\" content=\"{$width}\">";
365 $out .= "<meta itemprop=\"height\" content=\"{$height}\">";
366 $out .= "<meta itemprop=\"url\" content=\"{$img_url}\">";
367 }
368 $out .= "<img src=\"{$img_url}\"";
369 if ( $width ) {
370 $out .= " width=\"{$width}\"";
371 }
372 if ( $height ) {
373 $out .= " height=\"{$height}\"";
374 }
375 $args['class'] .= ' wp-post-image wp-image-' . $id; /* add attachment_id class to img tag */
376
377 if ( ! empty( $args['class'] ) ) {
378 $out .= " class=\"{$args['class']}\"";
379 }
380
381 // Add title attribute only if explicitly set in $args
382 if ( ! empty( $args['title'] ) ) {
383 $out .= ' title="' . esc_attr( $args['title'] ) . '"';
384 }
385
386 // If alt was passed as parameter, use it. Otherwise use alt text by attachment id if it was fetched or post title.
387 $out_alt = '';
388 if ( ! empty( $args['alt'] ) ) {
389 $out_alt = $args['alt'] === 'false' ? '' : $args['alt'];
390 } elseif ( ! empty( $img_alt ) ) {
391 $out_alt = $img_alt;
392 } else {
393 if ( ! empty( $args['title'] ) ) {
394 $out_alt = $args['title'];
395 } elseif ( $id ) {
396 $p = get_post( $id );
397 if ( $p ) {
398 $out_alt = $p->post_title;
399 }
400 } else {
401 $out_alt = the_title_attribute( 'echo=0' );
402 }
403 }
404 $out .= ' alt="' . esc_attr( $out_alt ) . '" />';
405
406 if ( ! $unlink ) {
407 $args['before'] .= sprintf( '<a href="%s">', get_permalink() );
408 $args['after'] = '</a>' . $args['after'];
409 }
410
411 $out = $args['before'] . $out . $args['after'];
412
413 if( version_compare( $wp_version, '4.4', '>=' ) ) {
414 $out = function_exists('wp_filter_content_tags') ? wp_filter_content_tags($out) : wp_make_content_images_responsive( $out );
415 }
416
417 return $out;
418 }
419 endif;
420
421 if ( ! function_exists( 'themify_event_post_title' ) ) :
422 /**
423 * Display title for event posts
424 *
425 * @return string|null
426 * @since 1.0.1
427 */
428 function themify_event_post_title( $args = array() ) {
429 extract( wp_parse_args( $args, array(
430 'post' => 0,
431 'tag' => 'h2',
432 'class' => 'tep_post_title entry-title',
433 'before' => '',
434 'after' => '',
435 'before_title' => '',
436 'after_title' => '',
437 'echo' => true,
438 'unlink' => false,
439 ), 'post_title' ) );
440
441 $post = get_post( $post );
442 $title = get_the_title( $post );
443 if ( strlen( $title ) == 0 ) {
444 return;
445 }
446
447 $link_before = $unlink ? '' : '<a href="' . get_permalink() .'">';
448 $link_after = $unlink ? '' : '</a>';
449
450 $before = "{$before} <{$tag} class=\"{$class}\">{$before_title}{$link_before}";
451 $after = "{$link_after}{$after_title} </{$tag}>{$after}";
452
453 $title = $before . $title . $after;
454
455 if ( $echo ) {
456 echo $title;
457 } else {
458 return $title;
459 }
460 }
461 endif;
462
463 /**
464 * Get a WP_Query parameters from the shortcode attributes
465 *
466 * @param $atts the array of shortcode parameters supplied by user
467 * @param $shortcode the name of shortcode calling, provides "shortcode_atts_$shortcode" filter
468 * @param $defaults allows overriding the default shortcode atts, before being replaced by $atts
469 *
470 * @return array
471 */
472 function themify_event_post_parse_shortcode_ids( $category ) {
473
474 if ( ! is_array( $category ) ) {
475 $category = array_map( 'trim', explode( ',', $category ) );
476 }
477
478 $ids_in = $ids_not_in = $slugs_in = $slugs_not_in = array();
479 foreach ( $category as $v ) {
480 $v = trim( $v );
481 $except = '-' !== $v[0];
482 if ( is_numeric( $v ) ) {
483 if( $except === true ) {
484 $ids_in[] = $v;
485 } else {
486 $ids_not_in[] = abs( $v );
487 }
488 }
489 else{
490 if( $except === true ) {
491 $slugs_in[] = $v;
492 } else {
493 $slugs_not_in[] = substr( $v, 1 );
494 }
495 }
496 }
497 return array( $ids_in, $ids_not_in, $slugs_in, $slugs_not_in );
498 }
499
500 /**
501 * Parses $args to return a formatted array for WP_Query object
502 *
503 * @return array
504 * @since 1.0.2
505 */
506 function themify_event_post_parse_query( $args = array() ) {
507 $defaults = array(
508 'post_type' => 'event',
509 'taxonomy' => 'event-category',
510 'taxonomy_relation' => 'AND',
511 'limit' => 3,
512 'offset' => 0,
513 'category' => '0', // integer category ID
514 'orderby' => 'event_date', // date, title, rand, event_date
515 'order' => 'DESC', // ASC
516 'show' => 'upcoming',
517 'id' => '',
518 );
519 $args = shortcode_atts( $defaults, $args );
520 extract( $args );
521
522 $paged = themify_event_post_get_paged_query();
523 $query_args = array(
524 'post_type' => $post_type,
525 'offset' => ( ( $paged - 1 ) * (int) $limit ) + (int) $offset,
526 'posts_per_page' => $limit,
527 'suppress_filters' => false,
528 'orderby' => $orderby,
529 'order' => $order,
530 );
531
532 if ( ! empty( $args['id'] ) ) {
533 $query_args['p'] = $args['id'];
534 return $query_args;
535 }
536
537 // handle weird way Builder saves query_cat field types
538 $category = preg_replace( '/\|[multiple|single]*$/', '', $category );
539
540 if ( '0' !== $category && ! empty( $category ) ) {
541
542 list( $ids_in, $ids_not_in, $slugs_in, $slugs_not_in ) = themify_event_post_parse_shortcode_ids( $category );
543
544 $query_args['tax_query'] = array(
545 'relation' => $taxonomy_relation
546 );
547 if ( ! empty( $ids_in ) ) {
548 $query_args['tax_query'][] = array(
549 'taxonomy' => $taxonomy,
550 'field' => 'id',
551 'terms' => $ids_in
552 );
553 }
554 if ( ! empty( $ids_not_in ) ) {
555 $query_args['tax_query'][] = array(
556 'taxonomy' => $taxonomy,
557 'field' => 'id',
558 'terms' => array_map( 'abs', $ids_not_in ),
559 'operator' => 'NOT IN'
560 );
561 }
562 if ( ! empty( $slugs_in ) ) {
563 $query_args['tax_query'][] = array(
564 'taxonomy' => $taxonomy,
565 'field' => 'slug',
566 'terms' => $slugs_in
567 );
568 }
569 if ( ! empty( $slugs_not_in ) ) {
570 $query_args['tax_query'][] = array(
571 'taxonomy' => $taxonomy,
572 'field' => 'slug',
573 'terms' => $slugs_not_in,
574 'operator' => 'NOT IN'
575 );
576 }
577 }
578
579 if ( $orderby == 'event_date' ) {
580 $query_args['orderby'] = 'meta_value';
581 $query_args['meta_key'] = 'start_date';
582 }
583
584 if ( $show === 'upcoming' ) {
585 $query_args['meta_query'] = array(
586 'relation' => 'OR',
587 array(
588 'key' => 'end_date',
589 'value' => date_i18n( 'Y-m-d H:i' ),
590 'compare' => '>='
591 ),
592 array(
593 'key' => 'start_date',
594 'value' => date_i18n( 'Y-m-d H:i' ),
595 'compare' => '>='
596 ),
597 array(
598 'key' => 'repeat',
599 'value' => 'none',
600 'compare' => '!=')
601 );
602 } elseif ( $show === 'past' ) {
603 $query_args['meta_query'] = array(
604 'relation' => 'AND',
605 array(
606 'key' => 'end_date',
607 'value' => date_i18n( 'Y-m-d H:i' ),
608 'compare' => '<'
609 ),
610 array(
611 'key' => 'end_date',
612 'value' => '',
613 'compare' => '!='
614 ),
615 );
616 $query_args['order'] = $order;
617 }
618
619 return $query_args;
620 }
621
622 if ( ! function_exists( 'themify_event_post_json_ld_generator' ) ) :
623 /**
624 * Generate the JSON-LD structure data
625 *
626 * @return string|null
627 * @since 1.0.1
628 */
629 function themify_event_post_json_ld_generator( $args = array() ) {
630 $schema = array(
631 '@context'=>'https://schema.org',
632 '@type'=>'Event',
633 'name'=>$args['name'],
634 'startDate'=>$args['start_date'],
635 'endDate'=>$args['end_date'],
636 'location'=>array(
637 '@type'=>'Place',
638 'name'=>$args['place'],
639 'address'=>array(
640 '@type'=>'PostalAddress',
641 'streetAddress'=>$args['address']
642 )
643 ),
644 'image'=>!empty($args['image']) ? array($args['image']) : array(),
645 'description'=>$args['decription'],
646 'offers'=>array(
647 '@type'=>'Offer',
648 'url'=>$args['buy_ticket'],
649 'price'=>$args['ticket_price'],
650 'priceCurrency'=>$args['ticket_currency'],
651 'validFrom'=>$args['ticket_purchase_start'],
652 'validThrough'=>$args['ticket_purchase_end'],
653 'availability'=>''
654 ),
655 'performer'=>array(
656 '@type'=>'',
657 'name'=>''
658 ),
659 'organizer'=>array(
660 '@type'=>'',
661 'name'=>'',
662 'url'=>''
663 ),
664 'eventAttendanceMode'=>'',
665 'eventStatus'=>'',
666 );
667
668 $timezone = new DateTimeZone( wp_timezone_string() );
669 if (!empty($args['event_attendance'])) $schema['eventAttendanceMode'] = 'https://schema.org/' . $args['event_attendance'] . 'EventAttendanceMode';
670 else unset($schema['eventAttendanceMode']);
671
672 if (empty($args['event_status'])) $args['event_status'] = 'Scheduled';
673 $schema['eventStatus'] = 'https://schema.org/Event' . $args['event_status'];
674
675 if (!empty($args['organizer_name']) || !empty($args['organizer_url'])){
676 $schema['organizer']['@type'] = $args['organizer'];
677 $schema['organizer']['name'] = $args['organizer_name'];
678 $schema['organizer']['url'] = $args['organizer_url'];
679 } else unset($schema['organizer']);
680
681 if (!empty($args['performer_name'])){
682 $schema['performer']['@type'] = $args['performer'];
683 $schema['performer']['name'] = $args['performer_name'];
684 } else unset($schema['performer']);
685
686 if (!empty($args['ticket_availability'])) $schema['offers']['availability'] = 'https://schema.org/' . $args['ticket_availability'];
687 else unset($schema['offers']['availability']);
688
689 if ( ! empty( $schema['startDate'] ) ) {
690 $schema['startDate'] = date_create( $schema['startDate'], $timezone )->format( 'c' );
691 }
692
693 if ( ! empty( $schema['endDate'] ) ) {
694 $schema['endDate'] = date_create( $schema['endDate'], $timezone )->format( 'c' );
695 }
696
697 if ( ! empty( $schema['validFrom'] ) ) {
698 $schema['validFrom'] = date_create( $schema['validFrom'], $timezone )->format( 'c' );
699 }
700
701 if ( ! empty( $schema['validThrough'] ) ) {
702 $schema['validThrough'] = date_create( $schema['validThrough'], $timezone )->format( 'c' );
703 }
704
705 $output =
706 '<script type="application/ld+json">'
707 . json_encode( array( $schema ) )
708 . '</script>';
709
710 return $output;
711 }
712 endif;
713
714 /**
715 * Resize images dynamically using wp built in functions
716 *
717 * @param string|int $image Image URL or an attachment ID
718 * @param int $width
719 * @param int $height
720 * @param bool $crop
721 * @return array
722 */
723 function themify_events_do_img( $image, $width, $height, $crop = false ) {
724 $attachment_id = null;
725 $img_url = null;
726
727 $width = is_numeric( $width ) ? $width : '';
728 $height = is_numeric( $height ) ? $height : '';
729 // if an attachment ID has been sent
730 if( is_int( $image ) ) {
731 $post = get_post( $image );
732 if( $post ) {
733 $attachment_id = $post->ID;
734 $img_url = wp_get_attachment_url( $attachment_id );
735 }
736 } else {
737 // URL has been passed to the function
738 $img_url = esc_url( $image );
739
740 // Check if the image is an attachment. If it's external return url, width and height.
741 $upload_dir = wp_get_upload_dir();
742 $base_url = preg_replace( '/https?:\/\/(www\.)?/', '', $upload_dir['baseurl'] ); // Removes protocol and WWW
743 if( ! preg_match( '/' . str_replace( '/', '\/', $base_url ) . '/', $img_url ) ) {
744 return array(
745 'url' =>$img_url,
746 'width' => $width,
747 'height' => $height,
748 );
749 }
750
751 // Finally, run a custom database query to get the attachment ID from the modified attachment URL
752 $attachment_id = themify_events_get_attachment_id_from_url( $img_url, $base_url );
753 }
754 // Fetch attachment meta data. Up to this point we know the attachment ID is valid.
755 $meta = $attachment_id ?wp_get_attachment_metadata( $attachment_id ):null;
756
757 // missing metadata. bail.
758 if ( ! is_array( $meta ) ) {
759 return array(
760 'url' => $img_url,
761 'width' => $width,
762 'height' => $height,
763 );
764 }
765
766 // Perform calculations when height or width = 0
767 if( empty( $width ) ) {
768 $width = 0;
769 }
770 if ( empty( $height ) ) {
771 // If width and height or original image are available as metadata
772 if ( !empty( $meta['width'] ) && !empty( $meta['height'] ) ) {
773 // Divide width by original image aspect ratio to obtain projected height
774 // The floor function is used so it returns an int and metadata can be written
775 $height = floor( $width / ( $meta['width'] / $meta['height'] ) );
776 } else {
777 $height = 0;
778 }
779 }
780 // Check if resized image already exists
781 if ( is_array( $meta ) && isset( $meta['sizes']["resized-{$width}x{$height}"] ) ) {
782 $size = $meta['sizes']["resized-{$width}x{$height}"];
783 if( isset( $size['width'],$size['height'] )) {
784 setlocale( LC_CTYPE, get_locale() . '.UTF-8' );
785 $split_url = explode( '/', $img_url );
786
787 if( ! isset( $size['mime-type'] ) || $size['mime-type'] !== 'image/gif' ) {
788 $split_url[ count( $split_url ) - 1 ] = $size['file'];
789 }
790
791 return array(
792 'url' => implode( '/', $split_url ),
793 'width' => $width,
794 'height' => $height,
795 'attachment_id' => $attachment_id,
796 );
797 }
798 }
799
800 // Requested image size doesn't exists, so let's create one
801 if ( true == $crop ) {
802 add_filter( 'image_resize_dimensions', 'themify_events_img_resize_dimensions', 10, 5 );
803 }
804 // Patch meta because if we're here, there's a valid attachment ID for sure, but maybe the meta data is not ok.
805 if ( empty( $meta ) ) {
806 $meta['sizes'] = array( 'large' => array() );
807 }
808 // Generate image returning an array with image url, width and height. If image can't generated, original url, width and height are used.
809 $image = themify_events_make_image_size( $attachment_id, $width, $height, $meta, $img_url );
810
811 if ( true == $crop ) {
812 remove_filter( 'image_resize_dimensions', 'themify_events_img_resize_dimensions', 10 );
813 }
814 $image['attachment_id'] = $attachment_id;
815 return $image;
816 }
817
818 /**
819 * Creates new image size.
820 *
821 * @uses get_attached_file()
822 * @uses image_make_intermediate_size()
823 * @uses wp_update_attachment_metadata()
824 * @uses get_post_meta()
825 * @uses update_post_meta()
826 *
827 * @param int $attachment_id
828 * @param int $width
829 * @param int $height
830 * @param array $meta
831 * @param string $img_url
832 *
833 * @return array
834 */
835 function themify_events_make_image_size( $attachment_id, $width, $height, $meta, $img_url ) {
836 if($width!==0 || $height!==0){
837 setlocale( LC_CTYPE, get_locale() . '.UTF-8' );
838 $attached_file = get_attached_file( $attachment_id );
839
840 $default_size = function_exists( 'themify_get' )
841 ? themify_get( 'setting-img_php_base_size', 'large', true )
842 : 'large';
843 $source_size = apply_filters( 'themify_image_script_source_size', $default_size );
844 if ( $source_size !== 'full' && isset( $meta['sizes'][ $source_size ]['file'] ) )
845 $attached_file = str_replace( $meta['file'], trailingslashit( dirname( $meta['file'] ) ) . $meta['sizes'][ $source_size ]['file'], $attached_file );
846
847 $resized = image_make_intermediate_size( $attached_file, $width, $height, true );
848 if ( $resized && ! is_wp_error( $resized ) ) {
849
850 // Save the new size in meta data
851 $key = sprintf( 'resized-%dx%d', $width, $height );
852 $meta['sizes'][$key] = $resized;
853 $img_url = str_replace( basename( $img_url ), $resized['file'], $img_url );
854
855 wp_update_attachment_metadata( $attachment_id, $meta );
856
857 // Save size in backup sizes so it's deleted when original attachment is deleted.
858 $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
859 if ( ! is_array( $backup_sizes ) ) $backup_sizes = array();
860 $backup_sizes[$key] = $resized;
861 update_post_meta( $attachment_id, '_wp_attachment_backup_sizes', $backup_sizes );
862 $img_url=esc_url($img_url);
863 }
864 }
865 // Return original image url, width and height.
866 return array(
867 'url' => $img_url,
868 'width' => $width,
869 'height' => $height
870 );
871 }
872
873 /**
874 * Disable the min commands to choose the minimum dimension, thus enabling image enlarging.
875 *
876 * @param $default
877 * @param $orig_w
878 * @param $orig_h
879 * @param $dest_w
880 * @param $dest_h
881 * @return array
882 */
883 function themify_events_img_resize_dimensions( $default, $orig_w, $orig_h, $dest_w, $dest_h ) {
884 // set portion of the original image that we can size to $dest_w x $dest_h
885 $aspect_ratio = $orig_w / $orig_h;
886 $new_w = $dest_w;
887 $new_h = $dest_h;
888
889 if ( !$new_w ) {
890 $new_w = (int)( $new_h * $aspect_ratio );
891 }
892
893 if ( !$new_h ) {
894 $new_h = (int)( $new_w / $aspect_ratio );
895 }
896
897 $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h );
898
899 $crop_w = round( $new_w / $size_ratio );
900 $crop_h = round( $new_h / $size_ratio );
901
902 $s_x = floor( ( $orig_w - $crop_w ) / 2 );
903 $s_y = floor( ( $orig_h - $crop_h ) / 2 );
904
905 // the return array matches the parameters to imagecopyresampled()
906 // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
907 return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
908 }
909
910 /**
911 * Get attachment ID for image from its url.
912 *
913 * @param string $url
914 * @param string $base_url
915 * @return bool|null|string
916 */
917 function themify_events_get_attachment_id_from_url( $url = '', $base_url = '' ) {
918 // If this is the URL of an auto-generated thumbnail, get the URL of the original image
919 $url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif|webp)$)/i', '', $url );
920
921 $id = attachment_url_to_postid( $url );
922 return $id;
923 }
924
925 /**
926 * Template function to display the ticket price for an event.
927 * Must be used inside the loop, or provide a post_id|WP_Post object for $post param.
928 *
929 * @since 1.2.5
930 */
931 function themify_event_post_get_price( $post = null, $before = '', $after = '' ) {
932 $post = get_post( $post );
933 $price = get_post_meta( $post->ID, 'buy_tickets_price', true );
934 if ( empty( $price ) ) {
935 return '';
936 }
937
938 $currency = get_post_meta( $post->ID, 'buy_tickets_currency', true );
939 if ( empty( $currency ) ) {
940 $currency = Themify_Event_Post::get_instance()->get_option( 'currency' );
941 }
942 $currency_pos = Themify_Event_Post::get_instance()->get_option( 'currency_pos', 'right_space' );
943 switch ( $currency_pos ) {
944 case 'left':
945 $format = '%2$s%1$s';
946 break;
947 case 'right':
948 $format = '%1$s%2$s';
949 break;
950 case 'left_space':
951 $format = '%2$s %1$s';
952 break;
953 default:
954 $format = '%1$s %2$s';
955 }
956
957 return $before . sprintf( $format, $price, $currency ) . $after;
958 }
959
960 if ( ! function_exists( 'themify_event_post_edit_link' ) ) :
961 function themify_event_post_edit_link() {
962 if ( function_exists( 'themify_post_edit_link' ) ) {
963 themify_post_edit_link();
964 } else {
965 edit_post_link();
966 }
967 }
968 endif;
969
970 if ( ! function_exists( 'themify_event_get_buy_ticket' ) ) :
971 function themify_event_get_buy_ticket( $post = null ) : string {
972 $post = get_post( $post );
973 $output = '';
974 if ( $buy_ticket = get_post_meta( $post->ID, 'buy_tickets', true ) ) {
975 $buy_tickets_label = get_post_meta( $post->ID, 'buy_tickets_label', true );
976 if ( ! $buy_tickets_label ) {
977 $buy_tickets_label = __( 'Buy Tickets', 'themify-event-post' );
978 }
979
980 $is_visible = true;
981 $start_date = get_post_meta( $post->ID, 'ticket_purchase_start_date', true );
982 $end_date = get_post_meta( $post->ID, 'ticket_purchase_end_date', true );
983 $time = time();
984 if (
985 ( $start_date && $time < strtotime( $start_date ) )
986 || ( $end_date && $time > strtotime( $end_date ) )
987 ) {
988 $is_visible = false;
989 }
990
991 $attr = [
992 'target' => '_blank',
993 'class' => 'tep_ticket_link'
994 ];
995 if ( $is_visible ) {
996 $attr['href'] = $buy_ticket;
997 } else {
998 $attr['href'] = '#';
999 $attr['class'] .= ' tep_ticket_disabled';
1000 $attr['data-tep_tooltip'] = __( 'Purchase is not available at the moment.', 'themify-event-post' );
1001 }
1002
1003 $buy_tickets_label .= themify_event_post_get_price( null, ' - ' );
1004 $output = sprintf( '<a %s>%s</a>', themify_event_build_atts( $attr ), esc_html( $buy_tickets_label ) );
1005 }
1006
1007 return $output;
1008 }
1009 endif;
1010
1011 if ( ! function_exists( 'themify_event_buy_ticket' ) ) :
1012 function themify_event_buy_ticket( $before = '<p class="tep_ticket">', $after = '</p>' ) {
1013 $buy_button = themify_event_get_buy_ticket();
1014 if ( $buy_button ) {
1015 echo $before, $buy_button, $after;
1016 }
1017 }
1018 endif;
1019
1020 function themify_event_is_visible( $post = null ) : bool {
1021 $post = get_post( $post );
1022
1023 $start_date = get_post_meta( $post->ID, 'start_date', true );
1024 $end_date = get_post_meta( $post->ID, 'end_date', true );
1025 $time = time();
1026 if (
1027 ( ! $start_date || $time > strtotime( $start_date ) )
1028 && ( ! $end_date || $time < strtotime( $end_date ) )
1029 ) {
1030 return true;
1031 }
1032
1033 return false;
1034 }
1035
1036 function themify_event_build_atts( $atts = array() ) : string {
1037 $attribute_string = '';
1038 foreach ( $atts as $attr => $value ) {
1039 if ( false !== $value && '' !== $value && is_scalar( $value ) ) {
1040 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
1041 $attribute_string .= ' ' . $attr . '="' . $value . '"';
1042 }
1043 }
1044 return $attribute_string;
1045 }