add-appointment.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. do_action('booked_before_creating_appointment');
  3. $date = isset($_POST['date']) ? $_POST['date'] : '';
  4. $title = isset($_POST['title']) ? $_POST['title'] : '';
  5. $timestamp = isset($_POST['timestamp']) ? $_POST['timestamp'] : '';
  6. $timeslot = isset($_POST['timeslot']) ? $_POST['timeslot'] : '';
  7. $customer_type = isset($_POST['customer_type']) ? $_POST['customer_type'] : '';
  8. $hide_end_times = get_option('booked_hide_end_times',false);
  9. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  10. $calendar_id_for_cf = $calendar_id;
  11. if ($calendar_id):
  12. $calendar_id = array($calendar_id);
  13. $calendar_id = array_map( 'intval', $calendar_id );
  14. $calendar_id = array_unique( $calendar_id );
  15. endif;
  16. $time_format = get_option('time_format');
  17. $date_format = get_option('date_format');
  18. // Get custom field data (new in v1.2)
  19. $custom_fields = array();
  20. if ( $calendar_id_for_cf ) {
  21. $custom_fields = json_decode(stripslashes(get_option('booked_custom_fields_'.$calendar_id_for_cf)),true);
  22. }
  23. if ( !$custom_fields ) {
  24. $custom_fields = json_decode(stripslashes(get_option('booked_custom_fields')),true);
  25. }
  26. $custom_field_data = array();
  27. $cf_meta_value = '';
  28. if (!empty($custom_fields)):
  29. $previous_field = false;
  30. foreach($custom_fields as $key => $field):
  31. $field_name = $field['name'];
  32. $field_title = $field['value'];
  33. $field_title_parts = explode('---',$field_name);
  34. if ($field_title_parts[0] == 'radio-buttons-label' || $field_title_parts[0] == 'checkboxes-label'):
  35. $current_group_name = $field_title;
  36. elseif ($field_title_parts[0] == 'single-radio-button' || $field_title_parts[0] == 'single-checkbox'):
  37. // Don't change the group name yet
  38. else :
  39. $current_group_name = $field_title;
  40. endif;
  41. if ($field_name != $previous_field){
  42. if (isset($_POST[$field_name]) && $_POST[$field_name]):
  43. $field_value = $_POST[$field_name];
  44. if (is_array($field_value)){
  45. $field_value = implode(', ',$field_value);
  46. }
  47. $custom_field_data[$key] = array(
  48. 'label' => $current_group_name,
  49. 'value' => $field_value
  50. );
  51. endif;
  52. $previous_field = $field_name;
  53. }
  54. endforeach;
  55. $custom_field_data = apply_filters('booked_custom_field_data', $custom_field_data);
  56. if (!empty($custom_field_data)):
  57. foreach($custom_field_data as $key => $data):
  58. $cf_meta_value .= '<p class="cf-meta-value"><strong>'.$data['label'].'</strong><br>'.$data['value'].'</p>';
  59. endforeach;
  60. endif;
  61. endif;
  62. // END Get custom field data
  63. if ($customer_type == 'guest'):
  64. $name = esc_attr($_POST['guest_name']);
  65. $surname = isset($_POST['guest_surname']) ? esc_attr($_POST['guest_surname']) : false;
  66. $fullname = ( $surname ? $name . ' ' . $surname : $name );
  67. $email = isset($_POST['guest_email']) ? esc_attr($_POST['guest_email']) : false;
  68. $email_required = get_option('booked_require_guest_email_address',false);
  69. if ( $email_required && $email && is_email($email) && $name || !$email_required && !$email && $name):
  70. // Create a new appointment post for a current customer
  71. $new_post = apply_filters('booked_new_appointment_args', array(
  72. 'post_title' => date_i18n($date_format,$timestamp).' @ '.date_i18n($time_format,$timestamp).' (User: Guest)',
  73. 'post_content' => '',
  74. 'post_status' => 'publish',
  75. 'post_date' => date_i18n('Y',strtotime($date)).'-'.date_i18n('m',strtotime($date)).'-01 00:00:00',
  76. 'post_type' => 'booked_appointments'
  77. ));
  78. $post_id = wp_insert_post($new_post);
  79. update_post_meta($post_id, '_appointment_title', $title);
  80. update_post_meta($post_id, '_appointment_guest_name', $name);
  81. update_post_meta($post_id, '_appointment_guest_surname', $surname);
  82. update_post_meta($post_id, '_appointment_guest_email', $email);
  83. update_post_meta($post_id, '_appointment_timestamp', $timestamp);
  84. update_post_meta($post_id, '_appointment_timeslot', $timeslot);
  85. wp_publish_post($post_id);
  86. if (apply_filters('booked_update_cf_meta_value', true)) {
  87. update_post_meta($post_id, '_cf_meta_value', $cf_meta_value);
  88. }
  89. if (apply_filters('booked_update_appointment_calendar', true)) {
  90. if (isset($calendar_id) && $calendar_id): wp_set_object_terms($post_id,$calendar_id,'booked_custom_calendars'); endif;
  91. }
  92. $email_content = get_option('booked_approval_email_content');
  93. $email_subject = get_option('booked_approval_email_subject');
  94. if ($email && $email_content && $email_subject):
  95. $token_replacements = booked_get_appointment_tokens( $post_id );
  96. $email_content = booked_token_replacement( $email_content,$token_replacements );
  97. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  98. do_action( 'booked_approved_email', $email, $email_subject, $email_content );
  99. endif;
  100. echo 'success###'.$date;
  101. do_action('booked_new_appointment_created', $post_id);
  102. else:
  103. if ( !is_email($email) ):
  104. echo 'error###' . esc_html__( 'That email does not appear to be valid.','booked');
  105. endif;
  106. endif;
  107. elseif ($customer_type == 'current'):
  108. $user_id = esc_html( $_POST['user_id'] );
  109. // Create a new appointment post for a current customer
  110. $new_post = apply_filters('booked_new_appointment_args', array(
  111. 'post_title' => date_i18n($date_format,$timestamp).' @ '.date_i18n($time_format,$timestamp).' (User: '.$user_id.')',
  112. 'post_content' => '',
  113. 'post_status' => 'publish',
  114. 'post_date' => date_i18n('Y',strtotime($date)).'-'.date_i18n('m',strtotime($date)).'-01 00:00:00',
  115. 'post_author' => $user_id,
  116. 'post_type' => 'booked_appointments'
  117. ));
  118. $post_id = wp_insert_post($new_post);
  119. update_post_meta($post_id, '_appointment_title', $title);
  120. update_post_meta($post_id, '_appointment_timestamp', $timestamp);
  121. update_post_meta($post_id, '_appointment_timeslot', $timeslot);
  122. update_post_meta($post_id, '_appointment_user', $user_id);
  123. wp_publish_post($post_id);
  124. if (apply_filters('booked_update_cf_meta_value', true)) {
  125. update_post_meta($post_id, '_cf_meta_value', $cf_meta_value);
  126. }
  127. if (apply_filters('booked_update_appointment_calendar', true)) {
  128. if (isset($calendar_id) && $calendar_id): wp_set_object_terms($post_id,$calendar_id,'booked_custom_calendars'); endif;
  129. }
  130. // Send an email to the User?
  131. $email_content = get_option('booked_approval_email_content');
  132. $email_subject = get_option('booked_approval_email_subject');
  133. if ($email_content && $email_subject):
  134. $token_replacements = booked_get_appointment_tokens( $post_id );
  135. $email_content = booked_token_replacement( $email_content,$token_replacements );
  136. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  137. do_action( 'booked_approved_email', $email, $email_subject, $email_content );
  138. endif;
  139. echo 'success###'.$date;
  140. do_action('booked_new_appointment_created', $post_id);
  141. else:
  142. $name = esc_attr($_POST['name']);
  143. $surname = ( isset($_POST['surname']) && $_POST['surname'] ? esc_attr($_POST['surname']) : false );
  144. $fullname = ( $surname ? $name . ' ' . $surname : $name );
  145. $email = esc_attr( $_POST['email'] );
  146. $password = ($_POST['password'] ? esc_attr( $_POST['password'] ) : wp_generate_password());
  147. $errors = booked_registration_validation($email,$password);
  148. if (empty($errors)):
  149. $userdata = array(
  150. 'user_login' => $email,
  151. 'user_email' => $email,
  152. 'user_pass' => $password,
  153. 'first_name' => $name,
  154. 'last_name' => $surname
  155. );
  156. $user_id = wp_insert_user( $userdata );
  157. update_user_meta( $user_id, 'nickname', $name );
  158. wp_update_user( array ('ID' => $user_id, 'display_name' => $name ) );
  159. // Send a registration welcome email to the new user?
  160. $email_content = get_option('booked_registration_email_content');
  161. $email_subject = get_option('booked_registration_email_subject');
  162. if ($email_content && $email_subject):
  163. $token_replacements = array(
  164. 'name' => $fullname,
  165. 'email' => $email,
  166. 'password' => $password
  167. );
  168. $email_content = booked_token_replacement( $email_content,$token_replacements,'user' );
  169. $email_subject = booked_token_replacement( $email_subject,$token_replacements,'user' );
  170. do_action( 'booked_registration_email',$email, $email_subject, $email_content );
  171. endif;
  172. // Create a new appointment post for this new customer
  173. $new_post = apply_filters('booked_new_appointment_args', array(
  174. 'post_title' => date_i18n($date_format,$timestamp).' @ '.date_i18n($time_format,$timestamp).' (User: '.$user_id.')',
  175. 'post_content' => '',
  176. 'post_status' => 'publish',
  177. 'post_date' => date_i18n('Y',strtotime($date)).'-'.date_i18n('m',strtotime($date)).'-01 00:00:00',
  178. 'post_author' => $user_id,
  179. 'post_type' => 'booked_appointments'
  180. ));
  181. $post_id = wp_insert_post($new_post);
  182. update_post_meta($post_id, '_appointment_title', $title);
  183. update_post_meta($post_id, '_appointment_timestamp', $timestamp);
  184. update_post_meta($post_id, '_appointment_timeslot', $timeslot);
  185. update_post_meta($post_id, '_appointment_user', $user_id);
  186. wp_publish_post($post_id);
  187. if (apply_filters('booked_update_cf_meta_value', true)) {
  188. update_post_meta($post_id, '_cf_meta_value', $cf_meta_value);
  189. }
  190. if (apply_filters('booked_update_appointment_calendar', true)) {
  191. if (isset($calendar_id) && $calendar_id): wp_set_object_terms($post_id,$calendar_id,'booked_custom_calendars'); endif;
  192. }
  193. // Send an email to the user?
  194. $email_content = get_option('booked_approval_email_content');
  195. $email_subject = get_option('booked_approval_email_subject');
  196. if ($email_content && $email_subject):
  197. $token_replacements = booked_get_appointment_tokens( $post_id );
  198. $email_content = booked_token_replacement( $email_content,$token_replacements );
  199. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  200. do_action( 'booked_approved_email',$email, $email_subject, $email_content );
  201. endif;
  202. echo 'success###'.$date;
  203. do_action('booked_new_appointment_created', $post_id);
  204. else :
  205. echo 'error###'.implode('
  206. ',$errors);
  207. endif;
  208. endif;