export-csv.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. if ($_POST['appointment_time'] && $_POST['appointment_time'] == 'upcoming'):
  3. $current_timestamp = current_time('timestamp');
  4. $meta_query = array(
  5. array(
  6. 'key' => '_appointment_timestamp',
  7. 'value' => $current_timestamp,
  8. 'compare' => '>='
  9. )
  10. );
  11. elseif ($_POST['appointment_time'] && $_POST['appointment_time'] == 'past'):
  12. $current_timestamp = current_time('timestamp');
  13. $meta_query = array(
  14. array(
  15. 'key' => '_appointment_timestamp',
  16. 'value' => $current_timestamp,
  17. 'compare' => '<'
  18. )
  19. );
  20. else:
  21. $meta_query = array();
  22. endif;
  23. header('Content-Type: text/csv; charset=utf-8');
  24. header('Content-Disposition: attachment; filename=booked_appointments_export.csv');
  25. $output = fopen('php://output', 'w');
  26. $export_columns = apply_filters( 'booked_csv_export_columns', array(
  27. 'First Name',
  28. 'Last Name',
  29. 'Email',
  30. 'Calendar',
  31. 'Date',
  32. 'Start Time',
  33. 'End Time',
  34. 'Combined Date/Time',
  35. 'Custom Field Data' ));
  36. fputcsv( $output, $export_columns );
  37. $args = array(
  38. 'post_type' => 'booked_appointments',
  39. 'posts_per_page' => -1,
  40. 'post_status' => $_POST['appointment_type'],
  41. 'meta_key' => '_appointment_timestamp',
  42. 'orderby' => 'meta_value_num',
  43. 'order' => 'ASC',
  44. 'meta_query' => $meta_query
  45. );
  46. if ($_POST['calendar_id']):
  47. $args['tax_query'] = array(
  48. array(
  49. 'taxonomy' => 'booked_custom_calendars',
  50. 'field' => 'term_id',
  51. 'terms' => $_POST['calendar_id'],
  52. )
  53. );
  54. endif;
  55. $appointments_array = array();
  56. $date_format = get_option('date_format');
  57. $time_format = get_option('time_format');
  58. $bookedAppointments = new WP_Query( apply_filters('booked_fe_date_content_query',$args) );
  59. if($bookedAppointments->have_posts()):
  60. while ($bookedAppointments->have_posts()):
  61. $bookedAppointments->the_post();
  62. global $post;
  63. $calendars = array();
  64. $calendar_terms = get_the_terms($post->ID,'booked_custom_calendars');
  65. if (!empty($calendar_terms)):
  66. foreach($calendar_terms as $calendar){
  67. $calendars[$calendar->term_id] = $calendar->name;
  68. }
  69. endif;
  70. $timestamp = get_post_meta($post->ID, '_appointment_timestamp',true);
  71. $timeslot = get_post_meta($post->ID, '_appointment_timeslot',true);
  72. $timeslot = explode('-',$timeslot);
  73. $user_id = get_post_meta($post->ID, '_appointment_user',true);
  74. $customer_name = false;
  75. $customer_surname = false;
  76. $customer_email = false;
  77. if ($user_id):
  78. $user_info = get_userdata($user_id);
  79. if (!empty($user_info)):
  80. $customer_name = booked_get_name( $user_id, 'first' );
  81. $customer_surname = booked_get_name( $user_id, 'last' );
  82. $customer_email = $user_info->user_email;
  83. else:
  84. continue;
  85. endif;
  86. endif;
  87. if (!$customer_name):
  88. $customer_name = get_post_meta($post->ID, '_appointment_guest_name',true);
  89. $customer_surname = get_post_meta($post->ID, '_appointment_guest_surname',true);
  90. $customer_email = get_post_meta($post->ID, '_appointment_guest_email',true);
  91. endif;
  92. $cf_meta_value = get_post_meta($post->ID, '_cf_meta_value',true);
  93. if ($cf_meta_value):
  94. $cf_meta_value = rtrim(strip_tags(str_replace(array('</p>','<br>'),array("\n\n","\n"),$cf_meta_value)),"\n\n");
  95. else:
  96. $cf_meta_value = '';
  97. endif;
  98. $date_start = date_i18n($date_format,$timestamp);
  99. $time_start = date_i18n($time_format,strtotime(date_i18n('Y-m-d',$timestamp).' '.$timeslot[0]));
  100. $time_end = date_i18n($time_format,strtotime(date_i18n('Y-m-d',$timestamp).' '.$timeslot[1]));
  101. $appointments_array[$post->ID]['customer_name'] = esc_html( $customer_name );
  102. $appointments_array[$post->ID]['customer_surname'] = esc_html( $customer_surname );
  103. $appointments_array[$post->ID]['customer_email'] = esc_html( $customer_email );
  104. $appointments_array[$post->ID]['calendar'] = implode(',',$calendars);
  105. $appointments_array[$post->ID]['appointment_date'] = $date_start;
  106. $appointments_array[$post->ID]['appointment_start_time'] = $time_start;
  107. $appointments_array[$post->ID]['appointment_end_time'] = $time_end;
  108. $appointments_array[$post->ID]['appointment_combined_date_time'] = $date_start.' from '.$time_start.' to '.$time_end;
  109. $appointments_array[$post->ID]['custom_field_data'] = $cf_meta_value;
  110. endwhile;
  111. endif;
  112. foreach($appointments_array as $appt_id => $appointment):
  113. $appointment = apply_filters( 'booked_csv_row_data', $appointment, $appt_id );
  114. fputcsv( $output, $appointment );
  115. endforeach;
  116. die;