functions.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. function booked_profile_content_fea_appointments(){
  3. echo do_shortcode('[booked-fea-appointments remove_wrapper=1]');
  4. }
  5. function booked_profile_content_fea_history(){
  6. echo do_shortcode('[booked-fea-appointments remove_wrapper=1 historic=1]');
  7. }
  8. function booked_profile_content_fea_pending(){
  9. echo do_shortcode('[booked-fea-appointments remove_wrapper=1 pending=1]');
  10. }
  11. function booked_agent_appointments($user_id,$only_count = false,$time_format = false,$date_format = false,$calendar_ids = array(),$pending = false,$historic = false){
  12. if (!$date_format || !$time_format){
  13. $time_format = get_option('time_format');
  14. $date_format = get_option('date_format');
  15. }
  16. if ($pending):
  17. $statuses = apply_filters('booked_admin_pending_post_status',array('draft'));
  18. else:
  19. $statuses = apply_filters('booked_admin_approved_post_status',array('publish','future'));
  20. endif;
  21. $order = $historic ? 'DESC' : 'ASC';
  22. $count = $historic ? 50 : -1;
  23. $calendars = get_terms('booked_custom_calendars','orderby=slug&hide_empty=0');
  24. $default_calendar_id = false;
  25. if (!empty($calendars)):
  26. if (!current_user_can('manage_booked_options')):
  27. $booked_current_user = wp_get_current_user();
  28. $calendars = booked_filter_agent_calendars($booked_current_user,$calendars);
  29. if (empty($calendars)):
  30. $booked_none_assigned = true;
  31. else:
  32. $first_calendar = array_slice($calendars, 0, 1);
  33. $default_calendar_id = array_shift($first_calendar)->term_id;
  34. $booked_none_assigned = false;
  35. endif;
  36. else:
  37. $booked_none_assigned = false;
  38. endif;
  39. endif;
  40. if (empty($calendars) && !current_user_can('manage_booked_options')):
  41. $args = false;
  42. elseif(current_user_can('manage_booked_options')):
  43. $args = array(
  44. 'post_type' => 'booked_appointments',
  45. 'posts_per_page' => -1,
  46. 'post_status' => $statuses,
  47. 'meta_key' => '_appointment_timestamp',
  48. 'orderby' => 'meta_value_num',
  49. 'order' => 'ASC'
  50. );
  51. else:
  52. $calendar_ids = array();
  53. if (!empty($calendars)):
  54. foreach($calendars as $cal):
  55. $calendar_ids[] = $cal->term_id;
  56. endforeach;
  57. endif;
  58. $args = array(
  59. 'post_type' => 'booked_appointments',
  60. 'posts_per_page' => $count,
  61. 'post_status' => $statuses,
  62. 'meta_key' => '_appointment_timestamp',
  63. 'orderby' => 'meta_value_num',
  64. 'order' => 'ASC'
  65. );
  66. if (!empty($calendar_ids)):
  67. $args['tax_query'] = array(
  68. array(
  69. 'taxonomy' => 'booked_custom_calendars',
  70. 'field' => 'term_id',
  71. 'terms' => $calendar_ids,
  72. )
  73. );
  74. endif;
  75. endif;
  76. $appointments_array = array();
  77. if ($args):
  78. $bookedAppointments = new WP_Query($args);
  79. if($bookedAppointments->have_posts()):
  80. while ($bookedAppointments->have_posts()):
  81. $bookedAppointments->the_post();
  82. global $post;
  83. $appt_date_value = date('Y-m-d',get_post_meta($post->ID, '_appointment_timestamp',true));
  84. $appt_timeslot = get_post_meta($post->ID, '_appointment_timeslot',true);
  85. $appt_timeslots = explode('-',$appt_timeslot);
  86. $appt_time_start = date('H:i:s',strtotime($appt_timeslots[0]));
  87. $appt_timestamp = strtotime($appt_date_value.' '.$appt_time_start);
  88. $current_timestamp = current_time('timestamp');
  89. $day = date('d',$appt_timestamp);
  90. $category = get_the_category();
  91. $calendar_id = wp_get_post_terms( $post->ID, 'booked_custom_calendars' );
  92. $guest_name = get_post_meta($post->ID, '_appointment_guest_name',true);
  93. $guest_surname = get_post_meta($post->ID, '_appointment_guest_surname',true);
  94. $guest_email = get_post_meta($post->ID, '_appointment_guest_email',true);
  95. if (!$historic && $appt_timestamp >= $current_timestamp || $historic && $appt_timestamp < $current_timestamp){
  96. if (!$guest_name):
  97. $user_id = get_post_meta($post->ID, '_appointment_user',true);
  98. $appointments_array[$post->ID]['user'] = $user_id;
  99. else:
  100. $appointments_array[$post->ID]['guest_name'] = $guest_name . ( $guest_surname ? ' ' . $guest_surname : '' );
  101. $appointments_array[$post->ID]['guest_email'] = $guest_email;
  102. endif;
  103. $appointments_array[$post->ID]['post_id'] = $post->ID;
  104. $appointments_array[$post->ID]['timestamp'] = $appt_timestamp;
  105. $appointments_array[$post->ID]['timeslot'] = $appt_timeslot;
  106. $appointments_array[$post->ID]['calendar_id'] = $calendar_id;
  107. $appointments_array[$post->ID]['status'] = $post->post_status;
  108. }
  109. endwhile;
  110. $appointments_array = apply_filters('booked_appointments_array', $appointments_array);
  111. endif;
  112. wp_reset_query();
  113. if ($only_count):
  114. return count($appointments_array);
  115. else :
  116. return $appointments_array;
  117. endif;
  118. else :
  119. if ($only_count):
  120. return 0;
  121. else:
  122. return array();
  123. endif;
  124. endif;
  125. }