calendar-feed.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. if ( !isset($_GET['sh']) || isset($_GET['sh']) && $_GET['sh'] != BOOKEDICAL_SECURE_HASH )
  3. wp_die('<strong>Calendar Feed Requirements:</strong><br>The Booked calendar feeds now require a secure hash to access. Please take a look at your "Appointments > Calendar Feeds" page for the updated feed URLs.');
  4. header('Content-type: text/calendar; charset=utf-8');
  5. header('Content-Disposition: attachment; filename=appointment-feed-'.BOOKED_VERSION.'.ics');
  6. if (isset($_GET['calendar']) && $_GET['calendar']):
  7. $calendar_id = $_GET['calendar'];
  8. else:
  9. $calendar_id = false;
  10. endif;
  11. // 1 year ago to 5 years later.
  12. $from_timestamp = strtotime(date('Y-m-01', strtotime("-1 year")));
  13. $to_timestamp = strtotime(date('Y-m-d', strtotime("+5 years")));
  14. $args = array(
  15. 'post_type' => 'booked_appointments',
  16. 'posts_per_page' => -1,
  17. 'post_status' => array('publish', 'future'),
  18. 'meta_query' => array(
  19. array(
  20. 'key' => '_appointment_timestamp',
  21. 'value' => array( $from_timestamp, $to_timestamp ),
  22. 'compare' => 'BETWEEN',
  23. )
  24. )
  25. );
  26. if ($calendar_id):
  27. $args['tax_query'] = array(
  28. array(
  29. 'taxonomy' => 'booked_custom_calendars',
  30. 'field' => 'id',
  31. 'terms' => $calendar_id,
  32. )
  33. );
  34. endif;
  35. if ($calendar_id):
  36. $calendar_name = get_term_by('id',$calendar_id,'booked_custom_calendars');
  37. $calendar_name = $calendar_name->name;
  38. else :
  39. $calendar_name = 'Appointments';
  40. endif;
  41. $appts_in_this_timeslot = array();
  42. $bookedAppointments = new WP_Query($args);
  43. if($bookedAppointments->have_posts()):
  44. while ($bookedAppointments->have_posts()):
  45. $bookedAppointments->the_post();
  46. global $post;
  47. $timestamp = get_post_meta($post->ID, '_appointment_timestamp',true);
  48. $timeslot = get_post_meta($post->ID, '_appointment_timeslot',true);
  49. $user_id = get_post_meta($post->ID, '_appointment_user',true);
  50. $day = date('d',$timestamp);
  51. $appointments_array[$post->ID]['post_id'] = $post->ID;
  52. $appointments_array[$post->ID]['timestamp'] = $timestamp;
  53. $appointments_array[$post->ID]['timeslot'] = $timeslot;
  54. $appointments_array[$post->ID]['status'] = $post->post_status;
  55. $appointments_array[$post->ID]['user'] = $user_id;
  56. $appts_in_this_timeslot[] = $post->ID;
  57. endwhile;
  58. endif;
  59. ?>BEGIN:VCALENDAR<?php echo "\r\n"; ?>
  60. VERSION:2.0<?php echo "\r\n"; ?>
  61. PRODID:-//getbooked.io//Booked Calendar<?php echo "\r\n"; ?>
  62. CALSCALE:GREGORIAN<?php echo "\r\n"; ?>
  63. <?php if (!empty($appts_in_this_timeslot)):
  64. foreach($appts_in_this_timeslot as $appt_id):
  65. $guest_name = get_post_meta($appt_id, '_appointment_guest_name',true);
  66. $guest_surname = get_post_meta($appt_id, '_appointment_guest_surname',true);
  67. $guest_name = $guest_name . ( $guest_surname ? ' ' . $guest_surname : '' );
  68. $guest_email = get_post_meta($appt_id, '_appointment_guest_email',true);
  69. if (!$guest_name):
  70. // Customer Information
  71. $user_id = $appointments_array[$appt_id]['user'];
  72. if ($user_id):
  73. $user_info = get_userdata($user_id);
  74. $display_name = booked_get_name($user_id);
  75. if ( !empty($user_info) ):
  76. $email = $user_info->user_email;
  77. else:
  78. $display_name = esc_html__('[No name]','booked-calendar-feeds');
  79. $email = esc_html__('[No email]','booked-calendar-feeds');
  80. endif;
  81. else:
  82. $display_name = esc_html__('[No name]','booked-calendar-feeds');
  83. $email = esc_html__('[No email]','booked-calendar-feeds');
  84. endif;
  85. else:
  86. $display_name = $guest_name;
  87. $email = $guest_email;
  88. endif;
  89. $display_name = clean_calendarString($display_name);
  90. $email = clean_calendarString($email);
  91. // Appointment Information
  92. if ( isset($appt_id) ):
  93. $time_format = get_option('time_format');
  94. $date_format = get_option('date_format');
  95. $appt_id = $appt_id;
  96. $timestamp = get_post_meta($appt_id, '_appointment_timestamp',true);
  97. $timeslot = get_post_meta($appt_id, '_appointment_timeslot',true);
  98. $cf_meta_value = get_post_meta($appt_id, '_cf_meta_value',true);
  99. $timeslots = explode('-',$timeslot);
  100. if ($timeslots[0] == '0000' && $timeslots[1] == '2400'):
  101. $formatted_start_date = dateToCal(get_post_meta($appt_id, '_appointment_timestamp',true),true);
  102. $formatted_end_date = false;
  103. else :
  104. $end_date = date('Y-m-d',strtotime(get_gmt_from_date(date('Y-m-d H:i:s',get_post_meta($appt_id, '_appointment_timestamp',true)))));
  105. $end_date_time = $end_date . date('H:i:s',strtotime(get_gmt_from_date(date('Y-m-d H:i:s',strtotime($end_date.' '.$timeslots[1])))));
  106. $formatted_start_date = dateToCal(get_post_meta($appt_id, '_appointment_timestamp',true));
  107. $formatted_end_date = date('Ymd\THis',strtotime($end_date_time));
  108. endif;
  109. $cf_fields = array(); $cf_counter = 0;
  110. preg_match_all('/<p class=\"cf-meta-value\">([\\s\\S]*?)<\/p>/s', $cf_meta_value, $cf_meta_matches);
  111. if ( isset($cf_meta_matches[1]) ):
  112. foreach($cf_meta_matches[1] as $cf_meta_match):
  113. $p_content = ( isset( $cf_meta_match ) ? $cf_meta_match : false );
  114. if ($p_content):
  115. $p_content = explode('<br>',$p_content);
  116. preg_match('/<strong>(.*?)<\/strong>/s', $p_content[0], $s_matches);
  117. if ( isset($s_matches[1]) && $s_matches[1] && isset($p_content[1]) && $p_content[1] ):
  118. $cf_fields[$cf_counter]['title'] = ( isset( $s_matches[1] ) ? clean_calendarString( $s_matches[1] ) : false );
  119. $cf_fields[$cf_counter]['content'] = ( isset( $p_content[1] ) ? clean_calendarString( $p_content[1] ) : false );
  120. $cf_counter++;
  121. endif;
  122. endif;
  123. endforeach;
  124. endif;
  125. $description = ( $email ? $email . "\r\n" . ( !empty($cf_fields) ? display_customFields($cf_fields) : "" ) : "" );
  126. $description = str_replace("\r\n", "\\n\\n", $description);
  127. ?>BEGIN:VEVENT<?php echo "\r\n"; ?>
  128. DTSTAMP:<?php echo $formatted_start_date; ?>Z<?php echo "\r\n"; ?><?php
  129. if ($formatted_end_date):
  130. ?>DTSTART:<?php echo $formatted_start_date; ?>Z<?php echo "\r\n"; ?>
  131. DTEND:<?php echo $formatted_end_date; ?>Z<?php echo "\r\n"; ?><?php
  132. else:
  133. ?>DTSTART;VALUE=DATE:<?php echo $formatted_start_date; ?><?php echo "\r\n"; ?>
  134. DTEND;VALUE=DATE:<?php echo $formatted_start_date; ?><?php echo "\r\n"; ?><?php
  135. endif;
  136. ?>SUMMARY:<?php echo apply_filters( 'booked_calendar_feed_display_name', $display_name, $appt_id ); ?><?php echo "\r\n"; ?>
  137. <?php echo ( $description ? "DESCRIPTION:" . $description . "\r\n" : "" ); ?>
  138. UID:booked-appointment-<?php echo $appt_id; ?><?php echo "\r\n"; ?>
  139. END:VEVENT<?php echo "\r\n"; ?>
  140. <?php
  141. endif;
  142. endforeach;
  143. endif;
  144. ?>END:VCALENDAR<?php
  145. /* Convert Dates */
  146. function dateToCal($timestamp,$all_day = false) {
  147. if ($all_day):
  148. return date('Ymd',strtotime(get_gmt_from_date(date('Y-m-d H:i:s',$timestamp))));
  149. else:
  150. return date('Ymd\THis',strtotime(get_gmt_from_date(date('Y-m-d H:i:s',$timestamp))));
  151. endif;
  152. }
  153. function clean_calendarString($string = false){
  154. if ($string):
  155. preg_match_all( "/<\!--([\\s\\S]*?)-->/", $string, $matches );
  156. if ( isset($matches[0]) && !empty($matches[0]) ):
  157. foreach ($matches[0] as $match ):
  158. $string = str_replace( $match, '', $string );
  159. endforeach;
  160. endif;
  161. if (function_exists('mb_convert_encoding')):
  162. $string = mb_convert_encoding( $string, 'UTF-8' );
  163. else:
  164. $string = htmlspecialchars_decode(utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8', false)));
  165. endif;
  166. return preg_replace( "/([\,\;])/","\\\$1", $string);
  167. else:
  168. return false;
  169. endif;
  170. }
  171. /* Convert Custom Fields */
  172. function display_customFields($cf_fields) {
  173. ob_start();
  174. foreach($cf_fields as $field):
  175. echo trim( $field['title'] ) . "\\n";
  176. echo trim( $field['content'] ) . "\r\n";
  177. endforeach;
  178. return ob_get_clean();
  179. }