shortcodes.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. class Booked_FEA_Shortcodes {
  3. function __construct(){
  4. add_shortcode('booked-fea-appointments', array($this, 'booked_fea_appointments_shortcode') );
  5. }
  6. /* FEA APPOINTMENTS SHORTCODE */
  7. public function booked_fea_appointments_shortcode($atts = null, $content = null) {
  8. ob_start();
  9. if (is_user_logged_in() && current_user_can('edit_booked_appointments')):
  10. $booked_current_user = wp_get_current_user();
  11. $my_id = $booked_current_user->ID;
  12. $calendars = get_terms('booked_custom_calendars',array('order_by' => 'slug','hide_empty' => false));
  13. $calendar_ids = array();
  14. if (!empty($calendars)):
  15. foreach($calendars as $calendar):
  16. $calendar_id = $calendar->term_id;
  17. $term_meta = get_option( "taxonomy_$calendar_id" );
  18. if ($booked_current_user->user_email == $term_meta['notifications_user_id']):
  19. $calendar_ids[] = $calendar_id;
  20. endif;
  21. endforeach;
  22. endif;
  23. $historic = isset($atts['historic']) && $atts['historic'] ? true : false;
  24. $pending = isset($atts['pending']) && $atts['pending'] ? true : false;
  25. $time_format = get_option('time_format');
  26. $date_format = get_option('date_format');
  27. $appointments_array = booked_agent_appointments($my_id,false,$time_format,$date_format,$calendar_ids,$pending,$historic);
  28. $total_appts = count($appointments_array);
  29. $appointment_default_status = get_option('booked_new_appointment_default','draft');
  30. if (!isset($atts['remove_wrapper'])): echo '<div id="booked-profile-page" class="booked-shortcode">'; endif;
  31. echo '<div class="booked-fea-appt-list">';
  32. $booked_light_color = get_option('booked_light_color','#44535B');
  33. $booked_button_color = get_option('booked_button_color','#56C477');
  34. echo '<style type="text/css">';
  35. echo "body #booked-profile-page .booked-fea-appt-list button.button-primary { background:$booked_button_color; border-color:$booked_button_color; color:#fff; }\n";
  36. echo "body #booked-profile-page .booked-fea-appt-list button.button-primary:hover { background:$booked_light_color; border-color:$booked_light_color; }";
  37. echo "body #booked-profile-page .booked-fea-appt-list .appt-block .booked-wc_status-text.paid { color:$booked_button_color; }";
  38. echo '</style>';
  39. if ($pending):
  40. if ($total_appts):
  41. echo '<h4>'.sprintf(_n('%s Pending Appointment','%s Pending Appointments',$total_appts,'booked-frontend-agents'),'<span class="count">'.number_format($total_appts).'</span>').'</h4>';
  42. else:
  43. echo '<p class="booked-no-margin">'.__('No pending appointments.','booked-frontend-agents').'</p>';
  44. endif;
  45. elseif ($historic):
  46. if ($total_appts):
  47. echo '<h4>'.sprintf(_n('%s Past Appointment','%s Past Appointments',$total_appts,'booked-frontend-agents'),'<span class="count">'.number_format($total_appts).'</span>').'</h4>';
  48. else:
  49. echo '<p class="booked-no-margin">'.__('No past appointments.','booked-frontend-agents').'</p>';
  50. endif;
  51. else:
  52. if ($total_appts):
  53. echo '<h4>'.sprintf(_n('%s Upcoming Appointment','%s Upcoming Appointments',$total_appts,'booked-frontend-agents'),'<span class="count">'.number_format($total_appts).'</span>').'</h4>';
  54. else:
  55. echo '<p class="booked-no-margin">'.__('No upcoming appointments.','booked-frontend-agents').'</p>';
  56. endif;
  57. endif;
  58. foreach($appointments_array as $appt):
  59. $calendar_id = isset($appt['calendar_id'][0]->term_id) ? $appt['calendar_id'][0]->term_id : false;
  60. $today = date_i18n($date_format);
  61. $date_display = date_i18n($date_format,$appt['timestamp']);
  62. if ($date_display == $today){
  63. $date_display = __('Today','booked-frontend-agents');
  64. $day_name = '';
  65. } else {
  66. $day_name = date_i18n('l',$appt['timestamp']).', ';
  67. }
  68. $date_to_convert = date('F j, Y',$appt['timestamp']);
  69. $cf_meta_value = get_post_meta($appt['post_id'], '_cf_meta_value',true);
  70. $timeslots = explode('-',$appt['timeslot']);
  71. $time_start = date($time_format,strtotime($timeslots[0]));
  72. $time_end = date($time_format,strtotime($timeslots[1]));
  73. $appt_date_time = strtotime($date_to_convert.' '.date('H:i:s',strtotime($timeslots[0])));
  74. $current_timestamp = current_time('timestamp');
  75. $google_date_startend = date('Ymd',$appt['timestamp']);
  76. $google_time_start = date('Hi',strtotime($timeslots[0]));
  77. $google_time_end = date('Hi',strtotime($timeslots[1]));
  78. $cancellation_buffer = get_option('booked_cancellation_buffer',0);
  79. if ($cancellation_buffer):
  80. if ($cancellation_buffer < 1){
  81. $time_type = 'minutes';
  82. $time_count = $cancellation_buffer * 60;
  83. } else {
  84. $time_type = 'hours';
  85. $time_count = $cancellation_buffer;
  86. }
  87. $buffered_timestamp = strtotime('+'.$time_count.' '.$time_type,$current_timestamp);
  88. $date_to_compare = $buffered_timestamp;
  89. else:
  90. $date_to_compare = current_time('timestamp');
  91. endif;
  92. if ($timeslots[0] == '0000' && $timeslots[1] == '2400'):
  93. $timeslotText = __('All day','booked-frontend-agents');
  94. $google_date_startend_end = date('Ymd',strtotime(date('Y-m-d',$appt['timestamp']) . '+ 1 Day'));
  95. $google_time_end = '0000';
  96. else :
  97. $timeslotText = (!get_option('booked_hide_end_times') ? __('from','booked-frontend-agents').' ' : __('at','booked-frontend-agents').' ') . $time_start . (!get_option('booked_hide_end_times') ? ' ' . __('to','booked-frontend-agents').' '.$time_end : '');
  98. $google_date_startend_end = $google_date_startend;
  99. endif;
  100. $status = ($appt['status'] !== 'publish' && $appt['status'] !== 'future' ? __('pending','booked-frontend-agents') : __('approved','booked-frontend-agents'));
  101. $status_class = $appt['status'] !== 'publish' && $appt['status'] !== 'future' ? 'pending' : 'approved';
  102. echo '<div class="appt-block bookedClearFix" data-appt-id="'.$appt['post_id'].'">';
  103. $default_button_html = '<div class="booked-fea-buttons">';
  104. $default_button_html .= '<a href="#" class="delete"'.($calendar_id ? ' data-calendar-id="'.$calendar_id.'"' : '').'><i class="booked-icon booked-icon-close"></i></a>';
  105. $default_button_html .= ($status_class == 'pending' ? '<button data-appt-id="'.$appt['post_id'].'" class="approve button button-primary">'.__('Approve','booked-frontend-agents').'</button>' : '');
  106. $default_button_html .= '</div>';
  107. $default_button_html = apply_filters('booked_fea_shortcode_appointments_buttons', $default_button_html, $appt['post_id']);
  108. echo $default_button_html;
  109. $date_display = date_i18n($date_format,$appt['timestamp']);
  110. $day_name = date_i18n('l',$appt['timestamp']);
  111. $timeslots = explode('-',$appt['timeslot']);
  112. $time_start = date_i18n($time_format,strtotime($timeslots[0]));
  113. $time_end = date_i18n($time_format,strtotime($timeslots[1]));
  114. $atc_date_startend = date('Y-m-d',$appt['timestamp']);
  115. $atc_time_start = date('H:i:s',strtotime($timeslots[0]));
  116. $atc_time_end = date('H:i:s',strtotime($timeslots[1]));
  117. $date_to_compare = strtotime(date('F j, Y',$appt['timestamp']).' '.date('H:i:s',strtotime($timeslots[0])));
  118. $late_date = current_time('timestamp');
  119. if ($timeslots[0] == '0000' && $timeslots[1] == '2400'):
  120. $timeslotText = __('All day','booked');
  121. $atc_date_startend_end = date('Y-m-d',strtotime(date('Y-m-d',$appt['timestamp']) . '+ 1 Day'));
  122. $atc_time_end = '00:00:00';
  123. else :
  124. $timeslotText = (!get_option('booked_hide_end_times') ? __('from','booked').' ' : __('at','booked').' ') . $time_start . (!get_option('booked_hide_end_times') ? ' ' . __('to','booked').' '.$time_end : '');
  125. $atc_date_startend_end = $atc_date_startend;
  126. endif;
  127. $pending_statuses = apply_filters('booked_admin_pending_post_status',array('draft'));
  128. $status = (in_array($appt['status'],$pending_statuses) ? 'pending' : 'approved');
  129. $display_name = false;
  130. if (!isset($appt['guest_name'])):
  131. $user_info = get_userdata($appt['user']);
  132. if (isset($user_info->ID)):
  133. if ($user_info->user_firstname):
  134. $user_display = '<a href="#" class="user" data-user-id="'.$appt['user'].'">'.$user_info->user_firstname.($user_info->user_lastname ? ' '.$user_info->user_lastname : '').'</a>';
  135. $display_name = $user_info->user_firstname.($user_info->user_lastname ? ' '.$user_info->user_lastname : '');
  136. elseif ($user_info->display_name):
  137. $user_display = '<a href="#" class="user" data-user-id="'.$appt['user'].'">'.$user_info->display_name.'</a>';
  138. $display_name = $user_info->display_name;
  139. else:
  140. $user_display = '<a href="#" class="user" data-user-id="'.$appt['user'].'">'.$user_info->user_login.'</a>';
  141. $display_name = $user_info->user_login;
  142. endif;
  143. else :
  144. $user_display = __('(this user no longer exists)','booked-frontend-agents');
  145. endif;
  146. else :
  147. $user_display = '<a href="#" class="user" data-user-id="0">'.$appt['guest_name'].'</a>';
  148. $display_name = $appt['guest_name'];
  149. endif;
  150. echo $user_display;
  151. echo '<br>';
  152. if ($late_date > $date_to_compare): echo '<span class="late-appt">' . __('This appointment has passed.','booked-frontend-agents') . '</span><br>'; endif;
  153. if ($appt['calendar_id']): echo '<strong style="color:#000">'.$appt['calendar_id'][0]->name.'</strong><br>'; endif;
  154. echo '<i class="booked-icon booked-icon-calendar"></i>'.$day_name.', '.$date_display;
  155. echo '&nbsp;&nbsp;&nbsp;<i class="booked-icon booked-icon-clock"></i>'.$timeslotText;
  156. do_action('booked_shortcode_appointments_additional_information', $appt['post_id']);
  157. $cf_meta_value = apply_filters('booked_fea_cf_metavalue',$cf_meta_value);
  158. echo ($cf_meta_value ? '<br><i class="booked-icon booked-icon-info"></i><a href="#" class="booked-show-cf">'.__('Additional information','booked-frontend-agents').'</a><div class="cf-meta-values-hidden">'.$cf_meta_value.'</div>' : '');
  159. if (!$historic):
  160. if ($appt_date_time >= $date_to_compare):
  161. $calendar_button_array = array(
  162. 'atc_date_startend' => $atc_date_startend,
  163. 'atc_time_start' => $atc_time_start,
  164. 'atc_date_startend_end' => $atc_date_startend_end,
  165. 'atc_time_end' => $atc_time_end,
  166. );
  167. ob_start();
  168. booked_add_to_calendar_button($calendar_button_array,$cf_meta_value);
  169. $buttons_content = ob_get_clean();
  170. if ($buttons_content):
  171. echo '<div class="booked-cal-buttons">';
  172. echo $buttons_content;
  173. echo '</div>';
  174. endif;
  175. endif;
  176. endif;
  177. echo '</div>';
  178. endforeach;
  179. echo '</div>';
  180. if (!isset($atts['remove_wrapper'])): echo '</div>'; endif;
  181. wp_reset_postdata();
  182. else :
  183. return '<p>'.__('Please log in to view your upcoming appointments.','booked-frontend-agents').'</p>';
  184. endif;
  185. return ob_get_clean();
  186. }
  187. }
  188. $booked_fea_shortcodes = new Booked_FEA_Shortcodes;