dashboard-widget.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. class BookedDashboardWidget {
  3. function __construct(){
  4. if (current_user_can('edit_booked_appointments')):
  5. add_action( 'wp_dashboard_setup', array($this, 'booked_dashboard_widget') );
  6. endif;
  7. }
  8. public function booked_dashboard_widget() {
  9. wp_add_dashboard_widget(
  10. 'booked_upcoming_appointments',
  11. '<i class="booked-icon booked-icon-calendar"></i>&nbsp;&nbsp;'.esc_html__('Upcoming Appointments','booked'),
  12. array($this, 'booked_dashboard_widget_function')
  13. );
  14. }
  15. public function booked_dashboard_widget_function() {
  16. echo '<div id="data-ajax-url">'.get_admin_url().'</div>';
  17. echo '<div class="booked-pending-appt-list booked-dashboard-widget">';
  18. /*
  19. Set some variables
  20. */
  21. $time_format = get_option('time_format');
  22. $date_format = get_option('date_format');
  23. /*
  24. Grab all of the appointments for this day
  25. */
  26. $calendars = get_terms('booked_custom_calendars','orderby=slug&hide_empty=0');
  27. if (!empty($calendars) && !current_user_can('manage_booked_options')):
  28. $booked_current_user = wp_get_current_user();
  29. $calendars = booked_filter_agent_calendars($booked_current_user,$calendars);
  30. foreach($calendars as $calendar):
  31. $calendar_ids[] = $calendar->term_id;
  32. endforeach;
  33. if (count($calendar_ids) >= 1):
  34. $args = array(
  35. 'post_type' => 'booked_appointments',
  36. 'posts_per_page' => -1,
  37. 'post_status' => array( 'publish', 'future' ),
  38. 'meta_key' => '_appointment_timestamp',
  39. 'orderby' => 'meta_value_num',
  40. 'order' => 'ASC',
  41. 'tax_query' => array(
  42. array(
  43. 'taxonomy' => 'booked_custom_calendars',
  44. 'field' => 'term_id',
  45. 'terms' => $calendar_ids,
  46. ),
  47. ),
  48. );
  49. elseif (empty($calendar_ids) && !current_user_can('manage_booked_options')):
  50. $args = false;
  51. else:
  52. $args = array(
  53. 'post_type' => 'booked_appointments',
  54. 'posts_per_page' => -1,
  55. 'post_status' => array( 'publish', 'future' ),
  56. 'meta_key' => '_appointment_timestamp',
  57. 'orderby' => 'meta_value_num',
  58. 'order' => 'ASC'
  59. );
  60. endif;
  61. elseif (empty($calendars) && !current_user_can('manage_booked_options')):
  62. $args = false;
  63. else:
  64. $args = array(
  65. 'post_type' => 'booked_appointments',
  66. 'posts_per_page' => -1,
  67. 'post_status' => array( 'publish', 'future' ),
  68. 'meta_key' => '_appointment_timestamp',
  69. 'orderby' => 'meta_value_num',
  70. 'order' => 'ASC'
  71. );
  72. endif;
  73. $appointments_array = array();
  74. $counter = 0;
  75. $bookedAppointments = new WP_Query($args);
  76. if($bookedAppointments->have_posts()):
  77. while ($bookedAppointments->have_posts()):
  78. $bookedAppointments->the_post();
  79. global $post;
  80. $calendars = array();
  81. $calendar_terms = get_the_terms($post->ID,'booked_custom_calendars');
  82. if (!empty($calendar_terms)):
  83. foreach($calendar_terms as $calendar){
  84. $calendars[$calendar->term_id] = $calendar->name;
  85. }
  86. endif;
  87. $guest_name = get_post_meta($post->ID, '_appointment_guest_name',true);
  88. $guest_surname = get_post_meta($post->ID, '_appointment_guest_surname',true);
  89. $guest_email = get_post_meta($post->ID, '_appointment_guest_email',true);
  90. $timestamp = intval( get_post_meta( $post->ID, '_appointment_timestamp',true ) );
  91. $timeslot = get_post_meta( $post->ID, '_appointment_timeslot',true );
  92. $user_id = get_post_meta( $post->ID, '_appointment_user',true );
  93. $day = date_i18n('d',$timestamp);
  94. $current_timestamp = current_time('timestamp');
  95. if ($timestamp >= $current_timestamp){
  96. $counter++;
  97. if (!$guest_name):
  98. $user_id = get_post_meta($post->ID, '_appointment_user',true);
  99. $appointments_array[$timestamp.'-'.$post->ID]['user'] = $user_id;
  100. else:
  101. $appointments_array[$timestamp.'-'.$post->ID]['guest_name'] = $guest_name . ( $guest_surname ? ' ' . $guest_surname : '' );
  102. $appointments_array[$timestamp.'-'.$post->ID]['guest_email'] = $guest_email;
  103. endif;
  104. $appointments_array[$timestamp.'-'.$post->ID]['post_id'] = $post->ID;
  105. $appointments_array[$timestamp.'-'.$post->ID]['timestamp'] = $timestamp;
  106. $appointments_array[$timestamp.'-'.$post->ID]['timeslot'] = $timeslot;
  107. $appointments_array[$timestamp.'-'.$post->ID]['status'] = $post->post_status;
  108. $appointments_array[$timestamp.'-'.$post->ID]['calendar'] = implode(',',$calendars);
  109. if ($counter == 10): break; endif;
  110. }
  111. endwhile;
  112. $appointments_array = apply_filters('booked_appointments_timestamp_postid_array', $appointments_array);
  113. endif;
  114. // Sort by timestamp, just in case they aren't ordered properly.
  115. ksort($appointments_array);
  116. foreach($appointments_array as $appt):
  117. echo '<div class="pending-appt bookedClearFix" data-appt-id="'.$appt['post_id'].'">';
  118. $date_display = date_i18n($date_format,$appt['timestamp']);
  119. $day_name = date_i18n('l',$appt['timestamp']);
  120. $timeslots = explode('-',$appt['timeslot']);
  121. $time_start = date_i18n($time_format,strtotime($timeslots[0]));
  122. $time_end = date_i18n($time_format,strtotime($timeslots[1]));
  123. $date_to_compare = strtotime(date_i18n('Y-m-d',$appt['timestamp']).' '.date_i18n('H:i:s',strtotime($timeslots[0])));
  124. $late_date = current_time('timestamp');
  125. if ($timeslots[0] == '0000' && $timeslots[1] == '2400'):
  126. $timeslotText = esc_html__('All day','booked');
  127. else :
  128. $timeslotText = $time_start.'&ndash;'.$time_end;
  129. endif;
  130. $pending_statuses = apply_filters('booked_admin_pending_post_status',array('draft'));
  131. $status = (in_array($appt['status'],$pending_statuses) ? 'pending' : 'approved');
  132. echo '<span class="appt-block" data-appt-id="'.$appt['post_id'].'">';
  133. if (!isset($appt['guest_name'])):
  134. $user_info = get_userdata($appt['user']);
  135. if (isset($user_info->ID)):
  136. echo '<a href="#" class="user" data-user-id="'.$user_info->ID.'"><i class="booked-icon booked-icon-pencil"></i>&nbsp;'.booked_get_name($user_info->ID).'</a>';
  137. else :
  138. esc_html_e('(this user no longer exists)','booked');
  139. endif;
  140. else :
  141. echo '<a href="#" class="user" data-user-id="0"><i class="booked-icon booked-icon-pencil"></i>&nbsp;'.$appt['guest_name'].'</a>';
  142. endif;
  143. echo '<br>';
  144. if ($late_date > $date_to_compare): echo '<span class="late-appt">' . esc_html__('This appointment has passed.','booked') . '</span><br>'; endif;
  145. if ($appt['calendar']): echo '<strong style="color:#000">'.$appt['calendar'].'</strong><br>'; endif;
  146. echo '<i class="booked-icon booked-icon-calendar"></i>&nbsp;&nbsp;'.$day_name.', '.$date_display;
  147. echo '&nbsp;&nbsp;&nbsp;<i class="booked-icon booked-icon-clock"></i>&nbsp;&nbsp;'.$timeslotText;
  148. echo '</span>';
  149. echo '</div>';
  150. endforeach;
  151. echo '<div class="pending-appt'.(!empty($appointments_array) ? ' no-pending-message' : '').'">';
  152. echo '<p style="text-align:center;">'.esc_html__('There are no upcoming appointments.','booked').'</p>';
  153. echo '</div>';
  154. echo '</div>';
  155. wp_reset_postdata();
  156. }
  157. }
  158. new BookedDashboardWidget;