book-appointment.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  9. $calendar_id_for_cf = $calendar_id;
  10. if ($calendar_id):
  11. $calendar_id = array($calendar_id);
  12. $calendar_id = array_map( 'intval', $calendar_id );
  13. $calendar_id = array_unique( $calendar_id );
  14. endif;
  15. $name_requirements = get_option('booked_registration_name_requirements',array('require_name'));
  16. $name_requirements = ( isset($name_requirements[0]) ? $name_requirements[0] : false );
  17. $is_new_registration = $customer_type == 'new' && ! isset($_POST['date']) && ! isset($_POST['timestamp']) && ! isset($_POST['timeslot']);
  18. if ( !$is_new_registration && $date && $timeslot && isset($calendar_id_for_cf) ):
  19. $appt_is_available = booked_appt_is_available($date,$timeslot,$calendar_id_for_cf);
  20. else:
  21. wp_die();
  22. endif;
  23. if ($appt_is_available):
  24. $time_format = get_option('time_format');
  25. $date_format = get_option('date_format');
  26. $appointment_default_status = get_option('booked_new_appointment_default','draft');
  27. $hide_end_times = get_option('booked_hide_end_times',false);
  28. // Get custom field data (new in v1.2)
  29. $custom_fields = array();
  30. if ( $calendar_id_for_cf ) {
  31. $custom_fields = json_decode(stripslashes(get_option('booked_custom_fields_'.$calendar_id_for_cf)),true);
  32. }
  33. if ( !$custom_fields ) {
  34. $custom_fields = json_decode(stripslashes(get_option('booked_custom_fields')),true);
  35. }
  36. $custom_field_data = array();
  37. $cf_meta_value = '';
  38. if (!empty($custom_fields)):
  39. $previous_field = false;
  40. foreach($custom_fields as $key => $field):
  41. $field_name = $field['name'];
  42. $field_title = $field['value'];
  43. $field_title_parts = explode('---',$field_name);
  44. if ($field_title_parts[0] == 'radio-buttons-label' || $field_title_parts[0] == 'checkboxes-label'):
  45. $current_group_name = $field_title;
  46. elseif ($field_title_parts[0] == 'single-radio-button' || $field_title_parts[0] == 'single-checkbox'):
  47. // Don't change the group name yet
  48. else :
  49. $current_group_name = $field_title;
  50. endif;
  51. if ($field_name != $previous_field){
  52. if (isset($_POST[$field_name]) && $_POST[$field_name]):
  53. $field_value = $_POST[$field_name];
  54. if (is_array($field_value)){
  55. $field_value = implode(', ',$field_value);
  56. }
  57. $custom_field_data[$key] = array(
  58. 'label' => $current_group_name,
  59. 'value' => $field_value
  60. );
  61. endif;
  62. $previous_field = $field_name;
  63. }
  64. endforeach;
  65. $custom_field_data = apply_filters('booked_custom_field_data', $custom_field_data);
  66. if (!empty($custom_field_data)):
  67. foreach($custom_field_data as $key => $data):
  68. $cf_meta_value .= '<p class="cf-meta-value"><strong>'.$data['label'].'</strong><br>'.$data['value'].'</p>';
  69. endforeach;
  70. endif;
  71. endif;
  72. // END Get custom field data
  73. if ($customer_type == 'guest'):
  74. $name = esc_attr($_POST['guest_name']);
  75. $surname = isset($_POST['guest_surname']) && $_POST['guest_surname'] ? esc_attr($_POST['guest_surname']) : false;
  76. $fullname = ( $surname ? $name . ' ' . $surname : $name );
  77. $email = isset($_POST['guest_email']) ? esc_attr($_POST['guest_email']) : '';
  78. $email_required = get_option('booked_require_guest_email_address',false);
  79. if ( $name_requirements == 'require_surname' && !$surname ):
  80. echo 'error###'.esc_html__('Your full name is required to book an appointment.','booked');
  81. else:
  82. if ($email && is_email($email) && $name || !$email && !$email_required && $name):
  83. // Create a new appointment post for a guest customer
  84. $new_post = apply_filters('booked_new_appointment_args', array(
  85. 'post_title' => date_i18n($date_format,$timestamp).' @ '.date_i18n($time_format,$timestamp).' (User: Guest)',
  86. 'post_content' => '',
  87. 'post_status' => $appointment_default_status,
  88. 'post_date' => date_i18n('Y',strtotime($date)).'-'.date_i18n('m',strtotime($date)).'-01 00:00:00',
  89. 'post_type' => 'booked_appointments'
  90. ));
  91. $post_id = wp_insert_post($new_post);
  92. update_post_meta($post_id, '_appointment_title', $title);
  93. update_post_meta($post_id, '_appointment_guest_name', $name);
  94. update_post_meta($post_id, '_appointment_guest_surname', $surname);
  95. update_post_meta($post_id, '_appointment_guest_email', $email);
  96. update_post_meta($post_id, '_appointment_timestamp', $timestamp);
  97. update_post_meta($post_id, '_appointment_timeslot', $timeslot);
  98. if ($appointment_default_status == 'publish'): wp_publish_post($post_id); endif;
  99. if ( apply_filters('booked_update_cf_meta_value', true) ) {
  100. update_post_meta($post_id, '_cf_meta_value', $cf_meta_value);
  101. }
  102. if ( apply_filters('booked_update_appointment_calendar', true) ) {
  103. if (!empty($calendar_id)): $calendar_term = get_term_by('id',$calendar_id[0],'booked_custom_calendars'); $calendar_name = $calendar_term->name; wp_set_object_terms($post_id,$calendar_id,'booked_custom_calendars'); else: $calendar_name = false; endif;
  104. }
  105. // Send a confirmation email to the User?
  106. $email_content = get_option('booked_appt_confirmation_email_content',false);
  107. $email_subject = get_option('booked_appt_confirmation_email_subject',false);
  108. $token_replacements = booked_get_appointment_tokens( $post_id );
  109. if ( $email_content && $email_subject ):
  110. $admin_email = booked_which_admin_to_send_email( esc_html( $_POST['calendar_id'] ) );
  111. $email_content = booked_token_replacement( $email_content,$token_replacements );
  112. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  113. do_action( 'booked_confirmation_email', $token_replacements['email'], $email_subject, $email_content, $admin_email );
  114. endif;
  115. // Send an email to the Admin?
  116. $email_content = get_option('booked_admin_appointment_email_content',false);
  117. $email_subject = get_option('booked_admin_appointment_email_subject',false);
  118. if ($email_content && $email_subject):
  119. $admin_email = booked_which_admin_to_send_email( esc_html( $_POST['calendar_id'] ) );
  120. $email_content = booked_token_replacement( $email_content,$token_replacements );
  121. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  122. do_action( 'booked_admin_confirmation_email', $admin_email, $email_subject, $email_content, $token_replacements['email'], $token_replacements['name'] );
  123. endif;
  124. do_action('booked_new_appointment_created', $post_id);
  125. echo 'success###'.$date;
  126. else :
  127. if ($email && !is_email($email)):
  128. $errors[] = esc_html__('The email address you have entered doesn\'t appear to be valid.','booked');
  129. elseif ($email_required && !$email):
  130. $errors[] = esc_html__('Your name and a valid email address are required to book an appointment.','booked');
  131. elseif (!$name):
  132. $errors[] = esc_html__('Your name is required to book an appointment.','booked');
  133. else:
  134. $errors[] = esc_html__('An unknown error occured.','booked');
  135. endif;
  136. echo 'error###'.implode('
  137. ',$errors);
  138. endif;
  139. endif;
  140. elseif ($customer_type == 'current'):
  141. $user_id = ! empty($_POST['user_id']) ? intval($_POST['user_id']) : false;
  142. if ( ! $user_id && is_user_logged_in() ) {
  143. $user = wp_get_current_user();
  144. $user_id = $user->ID;
  145. }
  146. // Create a new appointment post for a current customer
  147. $new_post = apply_filters('booked_new_appointment_args', array(
  148. 'post_title' => date_i18n($date_format,$timestamp).' @ '.date_i18n($time_format,$timestamp).' (User: '.$user_id.')',
  149. 'post_content' => '',
  150. 'post_status' => $appointment_default_status,
  151. 'post_date' => date_i18n('Y',strtotime($date)).'-'.date_i18n('m',strtotime($date)).'-01 00:00:00',
  152. 'post_author' => $user_id,
  153. 'post_type' => 'booked_appointments'
  154. ));
  155. $post_id = wp_insert_post($new_post);
  156. update_post_meta($post_id, '_appointment_title', $title);
  157. update_post_meta($post_id, '_appointment_timestamp', $timestamp);
  158. update_post_meta($post_id, '_appointment_timeslot', $timeslot);
  159. update_post_meta($post_id, '_appointment_user', $user_id);
  160. if ($appointment_default_status == 'publish'): wp_publish_post($post_id); endif;
  161. if (apply_filters('booked_update_cf_meta_value', true)) {
  162. update_post_meta($post_id, '_cf_meta_value', $cf_meta_value);
  163. }
  164. if (apply_filters('booked_update_appointment_calendar', true)) {
  165. if (!empty($calendar_id)): $calendar_term = get_term_by('id',$calendar_id[0],'booked_custom_calendars'); $calendar_name = $calendar_term->name; wp_set_object_terms($post_id,$calendar_id,'booked_custom_calendars'); else: $calendar_name = false; endif;
  166. }
  167. // Send a confirmation email to the User?
  168. $email_content = get_option('booked_appt_confirmation_email_content');
  169. $email_subject = get_option('booked_appt_confirmation_email_subject');
  170. $token_replacements = booked_get_appointment_tokens( $post_id );
  171. if ($email_content && $email_subject):
  172. $admin_email = booked_which_admin_to_send_email($_POST['calendar_id']);
  173. $email_content = booked_token_replacement( $email_content,$token_replacements );
  174. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  175. do_action( 'booked_confirmation_email', $token_replacements['email'], $email_subject, $email_content, $admin_email );
  176. endif;
  177. // Send an email to the Admin?
  178. $email_content = get_option('booked_admin_appointment_email_content');
  179. $email_subject = get_option('booked_admin_appointment_email_subject');
  180. if ($email_content && $email_subject):
  181. $admin_email = booked_which_admin_to_send_email($_POST['calendar_id']);
  182. $email_content = booked_token_replacement( $email_content,$token_replacements );
  183. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  184. do_action( 'booked_admin_confirmation_email', $admin_email, $email_subject, $email_content, $token_replacements['email'], $token_replacements['name'] );
  185. endif;
  186. $_SESSION['appt_requested'] = 1;
  187. do_action('booked_new_appointment_created', $post_id);
  188. echo 'success###'.$date;
  189. elseif ($customer_type == 'new'):
  190. $name = esc_attr($_POST['booked_appt_name']);
  191. $surname = ( isset($_POST['booked_appt_surname']) && $_POST['booked_appt_surname'] ? esc_attr($_POST['booked_appt_surname']) : false );
  192. $fullname = ( $surname ? $name . ' ' . $surname : $name );
  193. $email = $_POST['booked_appt_email'];
  194. $password = $_POST['booked_appt_password'];
  195. if ( $name_requirements == 'require_surname' && !$surname ):
  196. echo 'error###'.esc_html__('Your full name is required to book an appointment.','booked');
  197. else:
  198. if (isset($_POST['captcha_word'])):
  199. $captcha_word = strtolower($_POST['captcha_word']);
  200. $captcha_code = strtolower($_POST['captcha_code']);
  201. else :
  202. $captcha_word = false;
  203. $captcha_code = false;
  204. endif;
  205. $errors = booked_registration_validation($email,$password,$captcha_word,$captcha_code);
  206. if (empty($errors)):
  207. $userdata = array(
  208. 'user_login' => $email,
  209. 'user_email' => $email,
  210. 'user_pass' => $password,
  211. 'first_name' => $name,
  212. 'last_name' => $surname
  213. );
  214. $user_id = wp_insert_user( $userdata );
  215. update_user_meta( $user_id, 'nickname', $name );
  216. wp_update_user( array ('ID' => $user_id, 'display_name' => $name ) );
  217. $creds = array();
  218. $creds['user_login'] = $email;
  219. $creds['user_password'] = $password;
  220. $creds['remember'] = true;
  221. $user_signon = wp_signon( $creds, false );
  222. if ( is_wp_error($user_signon) ){
  223. $signin_errors = $user_signon->get_error_message();
  224. }
  225. // Create a new appointment post for this new customer
  226. $new_post = apply_filters('booked_new_appointment_args', array(
  227. 'post_title' => date_i18n($date_format,$timestamp).' @ '.date_i18n($time_format,$timestamp).' (User: '.$user_id.')',
  228. 'post_content' => '',
  229. 'post_status' => $appointment_default_status,
  230. 'post_date' => date_i18n('Y',strtotime($date)).'-'.date_i18n('m',strtotime($date)).'-01 00:00:00',
  231. 'post_author' => $user_id,
  232. 'post_type' => 'booked_appointments'
  233. ));
  234. $post_id = wp_insert_post($new_post);
  235. update_post_meta( $post_id, '_appointment_title', $title );
  236. update_post_meta( $post_id, '_appointment_timestamp', $timestamp );
  237. update_post_meta( $post_id, '_appointment_timeslot', $timeslot );
  238. update_post_meta( $post_id, '_appointment_user', $user_id );
  239. if ($appointment_default_status == 'publish'): wp_publish_post( $post_id ); endif;
  240. if (apply_filters('booked_update_cf_meta_value', true)) {
  241. update_post_meta($post_id, '_cf_meta_value', $cf_meta_value);
  242. }
  243. if (apply_filters('booked_update_appointment_calendar', true)) {
  244. if (!empty($calendar_id)): wp_set_object_terms($post_id,$calendar_id,'booked_custom_calendars'); endif;
  245. }
  246. if (apply_filters('booked_update_appointment_calendar', true)) {
  247. if (!empty($calendar_id)): $calendar_term = get_term_by('id',$calendar_id[0],'booked_custom_calendars'); $calendar_name = $calendar_term->name; wp_set_object_terms($post_id,$calendar_id,'booked_custom_calendars'); else: $calendar_name = false; endif;
  248. }
  249. $token_replacements = booked_get_appointment_tokens( $post_id );
  250. // Send an email to the Admin?
  251. $email_content = get_option('booked_admin_appointment_email_content');
  252. $email_subject = get_option('booked_admin_appointment_email_subject');
  253. if ($email_content && $email_subject):
  254. $email_calendar_id = esc_html( $_POST['calendar_id'] );
  255. $admin_email = booked_which_admin_to_send_email( $email_calendar_id );
  256. $email_content = booked_token_replacement( $email_content,$token_replacements );
  257. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  258. do_action( 'booked_admin_confirmation_email', $admin_email, $email_subject, $email_content, $token_replacements['email'], $token_replacements['name'] );
  259. endif;
  260. // Send a registration welcome email to the new user?
  261. $email_content = get_option('booked_registration_email_content');
  262. $email_subject = get_option('booked_registration_email_subject');
  263. if ($email_content && $email_subject):
  264. $registration_token_replacements = array(
  265. 'name' => $fullname,
  266. 'email' => $email,
  267. 'username' => $email,
  268. 'password' => $password
  269. );
  270. $admin_email = booked_which_admin_to_send_email( esc_html( $_POST['calendar_id'] ) );
  271. $email_content = booked_token_replacement( $email_content,$registration_token_replacements,'user' );
  272. $email_subject = booked_token_replacement( $email_subject,$registration_token_replacements,'user' );
  273. do_action( 'booked_registration_email', $registration_token_replacements['email'], $email_subject, $email_content, $admin_email );
  274. endif;
  275. // Send an email to the User?
  276. $email_content = get_option('booked_appt_confirmation_email_content');
  277. $email_subject = get_option('booked_appt_confirmation_email_subject');
  278. if ($email_content && $email_subject):
  279. $admin_email = booked_which_admin_to_send_email( esc_html( $_POST['calendar_id'] ) );
  280. $email_content = booked_token_replacement( $email_content,$token_replacements );
  281. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  282. do_action( 'booked_confirmation_email', $token_replacements['email'], $email_subject, $email_content , $admin_email);
  283. endif;
  284. $_SESSION['appt_requested'] = 1;
  285. $_SESSION['new_account'] = 1;
  286. do_action('booked_new_appointment_created', $post_id);
  287. echo 'success###'.$date;
  288. else :
  289. echo 'error###'.implode('
  290. ',$errors);
  291. endif;
  292. endif;
  293. endif;
  294. // register the user only
  295. elseif ( $is_new_registration ):
  296. $name = esc_attr($_POST['booked_appt_name']);
  297. $surname = ( isset($_POST['booked_appt_surname']) && $_POST['booked_appt_surname'] ? esc_attr($_POST['booked_appt_surname']) : false );
  298. $fullname = ( $surname ? $name . ' ' . $surname : $name );
  299. $email = $_POST['booked_appt_email'];
  300. $password = $_POST['booked_appt_password'];
  301. if ( $name_requirements == 'require_surname' && !$surname ):
  302. echo 'error###'.esc_html__('Your full name is required to book an appointment.','booked');
  303. else:
  304. if (isset($_POST['captcha_word'])):
  305. $captcha_word = strtolower($_POST['captcha_word']);
  306. $captcha_code = strtolower($_POST['captcha_code']);
  307. else :
  308. $captcha_word = false;
  309. $captcha_code = false;
  310. endif;
  311. $errors = booked_registration_validation($email,$password,$captcha_word,$captcha_code);
  312. if (empty($errors)):
  313. $userdata = array(
  314. 'user_login' => $email,
  315. 'user_email' => $email,
  316. 'user_pass' => $password,
  317. 'first_name' => $name,
  318. 'last_name' => $surname
  319. );
  320. $user_id = wp_insert_user( $userdata );
  321. if ($surname): $name = $name . ' ' . $surname; endif;
  322. update_user_meta( $user_id, 'nickname', $name );
  323. wp_update_user( array ('ID' => $user_id, 'display_name' => $name ) );
  324. $creds = array();
  325. $creds['user_login'] = $email;
  326. $creds['user_password'] = $password;
  327. $creds['remember'] = true;
  328. $user_signon = wp_signon( $creds, false );
  329. if ( is_wp_error($user_signon) ){
  330. $signin_errors = $user_signon->get_error_message();
  331. }
  332. // Send a registration welcome email to the new user?
  333. $email_content = get_option('booked_registration_email_content');
  334. $email_subject = get_option('booked_registration_email_subject');
  335. if ($email_content && $email_subject):
  336. $token_replacements = array(
  337. 'name' => $fullname,
  338. 'email' => $email,
  339. 'username' => $email,
  340. 'password' => $password
  341. );
  342. $email_content = booked_token_replacement( $email_content,$token_replacements,'user' );
  343. $email_subject = booked_token_replacement( $email_subject,$token_replacements,'user' );
  344. do_action( 'booked_registration_email', $token_replacements['email'], $email_subject, $email_content );
  345. endif;
  346. $_SESSION['appt_requested'] = 1;
  347. $_SESSION['new_account'] = 1;
  348. do_action('booked_new_appointment_created', $post_id);
  349. echo 'success###' . esc_html__('Registration has been successful.','booked');
  350. else :
  351. echo 'error###'.implode('
  352. ',$errors);
  353. endif;
  354. endif;
  355. else:
  356. $error_message = apply_filters(
  357. 'booked_availability_error_message',
  358. esc_html__('Sorry, someone just booked this appointment before you could. Please choose a different booking time.','booked')
  359. );
  360. echo 'error###' . $error_message;
  361. endif;