init_admin.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. <?php
  2. if(!class_exists('Booked_Admin_AJAX')) {
  3. class Booked_Admin_AJAX {
  4. public function __construct() {
  5. // ------------ Actions ------------ //
  6. add_action('wp_ajax_booked_admin_add_appt', array(&$this,'booked_admin_add_appt'));
  7. add_action('wp_ajax_booked_admin_edit_appt', array(&$this,'booked_admin_edit_appt'));
  8. add_action('wp_ajax_booked_admin_delete_custom_timeslot', array(&$this,'booked_admin_delete_custom_timeslot'));
  9. add_action('wp_ajax_booked_admin_adjust_custom_timeslot_count', array(&$this,'booked_admin_adjust_custom_timeslot_count'));
  10. add_action('wp_ajax_booked_admin_add_custom_timeslot', array(&$this,'booked_admin_add_custom_timeslot'));
  11. add_action('wp_ajax_booked_admin_add_custom_timeslots', array(&$this,'booked_admin_add_custom_timeslots'));
  12. add_action('wp_ajax_booked_admin_save_custom_time_slots', array(&$this,'booked_admin_save_custom_time_slots'));
  13. add_action('wp_ajax_booked_admin_save_custom_fields', array(&$this,'booked_admin_save_custom_fields'));
  14. add_action('wp_ajax_booked_admin_add_timeslots', array(&$this,'booked_admin_add_timeslots'));
  15. add_action('wp_ajax_booked_admin_add_timeslot', array(&$this,'booked_admin_add_timeslot'));
  16. add_action('wp_ajax_booked_admin_clear_timeslots', array(&$this,'booked_admin_clear_timeslots'));
  17. add_action('wp_ajax_booked_admin_adjust_default_timeslot_count', array(&$this,'booked_admin_adjust_default_timeslot_count'));
  18. add_action('wp_ajax_booked_admin_delete_timeslot', array(&$this,'booked_admin_delete_timeslot'));
  19. add_action('wp_ajax_booked_admin_delete_appt', array(&$this,'booked_admin_delete_appt'));
  20. add_action('wp_ajax_booked_admin_approve_appt', array(&$this,'booked_admin_approve_appt'));
  21. add_action('wp_ajax_booked_admin_approve_all', array(&$this,'booked_admin_approve_all'));
  22. add_action('wp_ajax_booked_admin_delete_all', array(&$this,'booked_admin_delete_all'));
  23. add_action('wp_ajax_booked_admin_delete_past', array(&$this,'booked_admin_delete_past'));
  24. add_action('wp_ajax_booked_date_formatting', array(&$this,'booked_date_formatting'));
  25. add_action('wp_ajax_booked_admin_disable_slot', array(&$this,'booked_admin_disable_slot'));
  26. // ------------ Loaders ------------ //
  27. add_action('wp_ajax_booked_admin_load_timeslots', array(&$this,'booked_admin_load_timeslots'));
  28. add_action('wp_ajax_booked_admin_load_full_timeslots', array(&$this,'booked_admin_load_full_timeslots'));
  29. add_action('wp_ajax_booked_admin_load_full_customfields', array(&$this,'booked_admin_load_full_customfields'));
  30. add_action('wp_ajax_booked_admin_calendar_picker', array(&$this,'booked_admin_calendar_picker'));
  31. add_action('wp_ajax_booked_admin_calendar_month', array(&$this,'booked_admin_calendar_month'));
  32. add_action('wp_ajax_booked_admin_calendar_date', array(&$this,'booked_admin_calendar_date'));
  33. add_action('wp_ajax_booked_admin_refresh_date_square', array(&$this,'booked_admin_refresh_date_square'));
  34. add_action('wp_ajax_booked_admin_user_info_modal', array(&$this,'booked_admin_user_info_modal'));
  35. add_action('wp_ajax_booked_admin_new_appointment_form', array(&$this,'booked_admin_new_appointment_form'));
  36. add_action('wp_ajax_booked_admin_custom_timeslots_list', array(&$this,'booked_admin_custom_timeslots_list'));
  37. add_action('wp_ajax_booked_admin_get_timeslots_select', array(&$this,'booked_admin_get_timeslots_select'));
  38. }
  39. // ------------ ACTIONS ------------ //
  40. // Enable/Disable Time Slot
  41. public function booked_admin_disable_slot(){
  42. booked_wpml_ajax();
  43. $date = esc_html( $_POST['date'] );
  44. $date = date( 'Y-m-d', strtotime( $date ) );
  45. $timeslot = esc_html( $_POST['timeslot'] );
  46. $calendar_id = esc_html( $_POST['calendar_id'] );
  47. $disabled_timeslots = get_option( 'booked_disabled_timeslots', array() );
  48. if ( $calendar_id ):
  49. if ( isset( $disabled_timeslots[$calendar_id][$date][$timeslot] ) && $disabled_timeslots[$calendar_id][$date][$timeslot] ):
  50. echo 'enabled';
  51. unset( $disabled_timeslots[$calendar_id][$date][$timeslot] );
  52. else:
  53. echo 'disabled';
  54. $disabled_timeslots[$calendar_id][$date][$timeslot] = true;
  55. endif;
  56. else:
  57. if ( isset( $disabled_timeslots[0][$date][$timeslot] ) && $disabled_timeslots[0][$date][$timeslot] ):
  58. echo 'enabled';
  59. unset( $disabled_timeslots[0][$date][$timeslot] );
  60. else:
  61. echo 'disabled';
  62. $disabled_timeslots[0][$date][$timeslot] = true;
  63. endif;
  64. endif;
  65. update_option( 'booked_disabled_timeslots', $disabled_timeslots );
  66. wp_die();
  67. }
  68. // Date Formatting
  69. public function booked_date_formatting(){
  70. booked_wpml_ajax();
  71. if (isset($_POST['date']) && $_POST['date']):
  72. $date_format = get_option('date_format');
  73. echo ucwords( date_i18n( $date_format,strtotime($_POST['date']) ) );
  74. else:
  75. echo '';
  76. endif;
  77. wp_die();
  78. }
  79. // Add Appointment
  80. public function booked_admin_add_appt(){
  81. booked_wpml_ajax();
  82. if (isset($_POST['date']) && isset($_POST['timestamp']) && isset($_POST['timeslot']) && isset($_POST['customer_type'])):
  83. include(BOOKED_AJAX_INCLUDES_DIR . 'admin/add-appointment.php');
  84. endif;
  85. wp_die();
  86. }
  87. public function booked_admin_edit_appt(){
  88. booked_wpml_ajax();
  89. if ( is_user_logged_in() && current_user_can( 'edit_booked_appointments' ) ):
  90. $email_required = get_option('booked_require_guest_email_address',false);
  91. if ( isset($_POST['appt_date']) && isset($_POST['appt_timeslot']) && isset($_POST['name']) ):
  92. if ( !$email_required || $email_required && isset($_POST['email']) ):
  93. include(BOOKED_AJAX_INCLUDES_DIR . 'admin/edit-appointment.php');
  94. endif;
  95. endif;
  96. else:
  97. echo 'error###' . esc_html__( "Your user level does not have the ability to edit appointments.", "booked" );
  98. endif;
  99. wp_die();
  100. }
  101. // Delete Custom Timeslot
  102. public function booked_admin_delete_custom_timeslot(){
  103. booked_wpml_ajax();
  104. if (isset($_POST['timeslot']) && isset($_POST['currentArray'])):
  105. $timeslot_to_delete = $_POST['timeslot'];
  106. $current_times = json_decode(stripslashes($_POST['currentArray']),true);
  107. $current_times_details = json_decode(stripslashes($_POST['currentArrayDetails']),true);
  108. do_action('booked_deleting_custom_timeslot',$_POST['start_date'],$_POST['end_date'],$_POST['timeslot'],$_POST['calendar_id']);
  109. if (isset($current_times[$timeslot_to_delete])):
  110. unset($current_times[$timeslot_to_delete]);
  111. unset($current_times_details[$timeslot_to_delete]);
  112. endif;
  113. echo json_encode(array(
  114. 'timeslot' => $current_times,
  115. 'timeslot_details' => $current_times_details
  116. ));
  117. endif;
  118. wp_die();
  119. }
  120. // Adjust Custom Timeslot Count
  121. public function booked_admin_adjust_custom_timeslot_count(){
  122. booked_wpml_ajax();
  123. if (isset($_POST['currentArray']) && isset($_POST['timeslot']) && isset($_POST['newCount'])):
  124. $current_times = json_decode(stripslashes($_POST['currentArray']),true);
  125. $timeslot = $_POST['timeslot'];
  126. $newCount = $_POST['newCount'];
  127. if (!empty($current_times[$timeslot])):
  128. $current_count = $current_times[$timeslot];
  129. if ($newCount > 0):
  130. $current_times[$timeslot] = $newCount;
  131. else :
  132. $current_times[$timeslot] = 1;
  133. endif;
  134. $current_times_details[$timeslot]['title'] = $title;
  135. endif;
  136. echo json_encode($current_times);
  137. endif;
  138. wp_die();
  139. }
  140. // Add Custom Timeslot
  141. public function booked_admin_add_custom_timeslot(){
  142. booked_wpml_ajax();
  143. if (isset($_POST['startTime']) && isset($_POST['endTime']) && isset($_POST['count']) && isset($_POST['currentTimes'])):
  144. $title = isset($_POST['title']) ? $_POST['title'] : '';
  145. $startTime = $_POST['startTime'];
  146. $endTime = $_POST['endTime'];
  147. $count = $_POST['count'];
  148. $current_times = json_decode(stripslashes($_POST['currentTimes']),true);
  149. $current_times_details = !empty($_POST['currentTimesDetails']) ? json_decode(stripslashes($_POST['currentTimesDetails']),true) : array();
  150. if ($startTime == 'allday'):
  151. $startTime = '0000';
  152. $endTime = '2400';
  153. endif;
  154. do_action('booked_creating_custom_timeslot',$_POST['start_date'],$_POST['end_date'],$startTime,$endTime,$_POST['calendar_id'],$title);
  155. if (isset($current_times[$startTime.'-'.$endTime])):
  156. $current_times[$startTime.'-'.$endTime] = $current_times[$startTime.'-'.$endTime] + $count;
  157. else :
  158. $current_times[$startTime.'-'.$endTime] = $count;
  159. endif;
  160. $current_times_details[$startTime.'-'.$endTime]['title'] = $title;
  161. echo json_encode(array(
  162. 'timeslot' => $current_times,
  163. 'timeslot_details' => $current_times_details,
  164. ));
  165. endif;
  166. wp_die();
  167. }
  168. // Add Custom Timeslots
  169. public function booked_admin_add_custom_timeslots(){
  170. booked_wpml_ajax();
  171. if (isset($_POST['startTime']) && isset($_POST['endTime']) && isset($_POST['interval']) && isset($_POST['count']) && isset($_POST['time_between'])):
  172. $title = isset($_POST['title']) ? $_POST['title'] : '';
  173. $startTime = $_POST['startTime'];
  174. $endTime = $_POST['endTime'];
  175. if ($_POST['endTime'] == '2400'):
  176. $endTime = '2400';
  177. endif;
  178. $interval = $_POST['interval'];
  179. $count = $_POST['count'];
  180. $time_between = $_POST['time_between'];
  181. $current_times = json_decode(stripslashes($_POST['currentTimes']),true);
  182. $current_times_details = !empty($_POST['currentTimesDetails']) ? json_decode(stripslashes($_POST['currentTimesDetails']),true) : array();
  183. do {
  184. $newStartTime = date_i18n("Hi", strtotime('+'.$interval.' minutes', strtotime($startTime)));
  185. if (isset($current_times[$startTime.'-'.$newStartTime])):
  186. $current_times[$startTime.'-'.$newStartTime] = $current_times[$startTime.'-'.$newStartTime] + $count;
  187. else :
  188. $current_times[$startTime.'-'.$newStartTime] = $count;
  189. endif;
  190. $current_times_details[$startTime.'-'.$newStartTime]['title'] = $title;
  191. do_action('booked_creating_custom_timeslot',$_POST['start_date'],$_POST['end_date'],$startTime,$newStartTime,$_POST['calendar_id'],$title);
  192. if ($time_between):
  193. $time_to_add = $time_between + $interval;
  194. else :
  195. $time_to_add = $interval;
  196. endif;
  197. $startTime = date_i18n("Hi", strtotime('+'.$time_to_add.' minutes', strtotime($startTime)));
  198. if ($startTime == '0000'):
  199. $startTime = '2400';
  200. endif;
  201. } while ($startTime < $endTime);
  202. echo json_encode(array(
  203. 'timeslot' => $current_times,
  204. 'timeslot_details' => $current_times_details,
  205. ));
  206. endif;
  207. wp_die();
  208. }
  209. // Save Custom Timeslots
  210. public function booked_admin_save_custom_time_slots(){
  211. booked_wpml_ajax();
  212. if (isset($_POST['custom_timeslots_encoded'])):
  213. $custom_timeslots_encoded = htmlentities( stripslashes($_POST['custom_timeslots_encoded']), ENT_NOQUOTES );
  214. update_option('booked_custom_timeslots_encoded',$custom_timeslots_encoded);
  215. endif;
  216. wp_die();
  217. }
  218. // Save Custom Fields
  219. public function booked_admin_save_custom_fields(){
  220. booked_wpml_ajax();
  221. if (isset($_POST['booked_custom_fields'])):
  222. $custom_fields = $_POST['booked_custom_fields'];
  223. $calendar_id = $_POST['booked_cf_calendar_id'];
  224. if ($custom_fields != '[]'):
  225. if ($calendar_id):
  226. update_option('booked_custom_fields_'.$calendar_id,$custom_fields);
  227. else:
  228. update_option('booked_custom_fields',$custom_fields);
  229. endif;
  230. else:
  231. if ($calendar_id):
  232. delete_option('booked_custom_fields_'.$calendar_id);
  233. else:
  234. delete_option('booked_custom_fields');
  235. endif;
  236. endif;
  237. endif;
  238. wp_die();
  239. }
  240. // Add Timeslots
  241. public function booked_admin_add_timeslots(){
  242. booked_wpml_ajax();
  243. if ( isset($_POST['day']) && isset($_POST['startTime']) && isset($_POST['endTime']) ):
  244. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  245. $title = isset($_POST['title']) ? $_POST['title'] : '';
  246. $day = esc_html( $_POST['day'] );
  247. $startTime = esc_html( $_POST['startTime'] );
  248. $endTime = esc_html( $_POST['endTime'] );
  249. $interval = esc_html( $_POST['interval'] );
  250. $count = esc_html( $_POST['count'] );
  251. $time_between = esc_html( $_POST['time_between'] );
  252. if ($calendar_id):
  253. $booked_defaults = get_option('booked_defaults_'.$calendar_id);
  254. else :
  255. $booked_defaults = get_option('booked_defaults');
  256. endif;
  257. if ( empty($booked_defaults) ): $booked_defaults = array(); endif;
  258. $temp_date = date_i18n( 'Y-m-d' );
  259. $loop_started = false;
  260. do {
  261. $newStartTime = date_i18n( "Hi", strtotime( '+' . $interval . ' minutes', strtotime( $temp_date . ' ' . $startTime ) ) );
  262. if ( $newStartTime < $endTime ):
  263. if (!empty($booked_defaults[$day][$startTime.'-'.$newStartTime])): $currentCount = $booked_defaults[$day][$startTime.'-'.$newStartTime]; else : $currentCount = 0; endif;
  264. $booked_defaults[$day][$startTime.'-'.$newStartTime] = $count + $currentCount;
  265. $booked_defaults[$day.'-details'][$startTime.'-'.$newStartTime]['title'] = $title;
  266. endif;
  267. if ( $time_between ):
  268. $time_to_add = $time_between + $interval;
  269. else :
  270. $time_to_add = $interval;
  271. endif;
  272. $startTime = date_i18n("Hi", strtotime('+'.$time_to_add.' minutes', strtotime( $temp_date . ' ' . $startTime ) ) );
  273. $mins = (int)$startTime;
  274. if ( $loop_started && $last_mins > $mins ):
  275. break;
  276. else:
  277. $last_mins = $mins;
  278. $loop_started = true;
  279. endif;
  280. } while ( $startTime < $endTime );
  281. if ($calendar_id):
  282. update_option('booked_defaults_'.$calendar_id,apply_filters('booked_update_timeslots',$booked_defaults));
  283. else :
  284. update_option('booked_defaults',apply_filters('booked_update_timeslots',$booked_defaults));
  285. endif;
  286. endif;
  287. wp_die();
  288. }
  289. // Clear Timeslots
  290. public function booked_admin_clear_timeslots(){
  291. booked_wpml_ajax();
  292. if ( isset($_POST['day']) ):
  293. $calendar_id = (isset($_POST['calendar_id']) ? esc_html( $_POST['calendar_id'] ) : false);
  294. $day = esc_html( $_POST['day'] );
  295. if ($calendar_id):
  296. $booked_defaults = get_option( 'booked_defaults_' . $calendar_id );
  297. else :
  298. $booked_defaults = get_option( 'booked_defaults' );
  299. endif;
  300. if ( isset( $booked_defaults[$day] ) ): unset( $booked_defaults[$day] ); endif;
  301. if ( isset( $booked_defaults[$day . '-details'] ) ): unset( $booked_defaults[$day . '-details'] ); endif;
  302. if ($calendar_id):
  303. update_option('booked_defaults_'.$calendar_id,apply_filters('booked_update_timeslots',$booked_defaults));
  304. else :
  305. update_option('booked_defaults',apply_filters('booked_update_timeslots',$booked_defaults));
  306. endif;
  307. endif;
  308. wp_die();
  309. }
  310. // Add Timeslot
  311. public function booked_admin_add_timeslot(){
  312. booked_wpml_ajax();
  313. if (isset($_POST['day']) && isset($_POST['startTime']) && isset($_POST['count'])):
  314. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  315. $title = isset($_POST['title']) ? $_POST['title'] : '';
  316. $day = $_POST['day'];
  317. $startTime = $_POST['startTime'];
  318. $endTime = isset($_POST['endTime']) ? $_POST['endTime'] : false;
  319. $count = $_POST['count'];
  320. if ($startTime == 'allday'):
  321. $startTime = '0000';
  322. $endTime = '2400';
  323. endif;
  324. if ($calendar_id):
  325. $booked_defaults = get_option('booked_defaults_'.$calendar_id);
  326. else :
  327. $booked_defaults = get_option('booked_defaults');
  328. endif;
  329. if (empty($booked_defaults)): $booked_defaults = array(); endif;
  330. if (!empty($booked_defaults[$day][$startTime.'-'.$endTime])): $currentCount = $booked_defaults[$day][$startTime.'-'.$endTime]; else : $currentCount = 0; endif;
  331. $booked_defaults[$day][$startTime.'-'.$endTime] = $count + $currentCount;
  332. $booked_defaults[$day.'-details'][$startTime.'-'.$endTime]['title'] = $title;
  333. do_action('booked_creating_timeslot',$day,$startTime,$endTime,$calendar_id);
  334. if ($calendar_id):
  335. update_option('booked_defaults_'.$calendar_id,apply_filters('booked_update_timeslots',$booked_defaults));
  336. else :
  337. update_option('booked_defaults',apply_filters('booked_update_timeslots',$booked_defaults));
  338. endif;
  339. endif;
  340. wp_die();
  341. }
  342. // Adjust Default Timeslot Count
  343. public function booked_admin_adjust_default_timeslot_count(){
  344. booked_wpml_ajax();
  345. if (isset($_POST['newCount']) && isset($_POST['day']) && isset($_POST['timeslot'])):
  346. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  347. $day = $_POST['day'];
  348. $timeslot = $_POST['timeslot'];
  349. $newCount = $_POST['newCount'];
  350. if ($calendar_id):
  351. $booked_defaults = get_option('booked_defaults_'.$calendar_id);
  352. else :
  353. $booked_defaults = get_option('booked_defaults');
  354. endif;
  355. if (!empty($booked_defaults[$day][$timeslot])):
  356. $booked_defaults[$day][$timeslot] = $newCount;
  357. if ($calendar_id):
  358. update_option('booked_defaults_'.$calendar_id,$booked_defaults);
  359. else :
  360. update_option('booked_defaults',$booked_defaults);
  361. endif;
  362. endif;
  363. endif;
  364. wp_die();
  365. }
  366. // Delete Timeslot
  367. public function booked_admin_delete_timeslot(){
  368. booked_wpml_ajax();
  369. if (isset($_POST['day']) && isset($_POST['timeslot'])):
  370. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  371. $day = $_POST['day'];
  372. $timeslot = $_POST['timeslot'];
  373. if ($calendar_id):
  374. $booked_defaults = get_option('booked_defaults_'.$calendar_id);
  375. else :
  376. $booked_defaults = get_option('booked_defaults');
  377. endif;
  378. do_action('booked_deleting_timeslot',$day,$timeslot,$calendar_id);
  379. if (!empty($booked_defaults[$day][$timeslot])):
  380. unset($booked_defaults[$day][$timeslot]);
  381. unset($booked_defaults[$day.'-details'][$timeslot]);
  382. $timeslot_total = 0;
  383. foreach($booked_defaults as $default):
  384. if (!empty($default)):
  385. $timeslot_total++;
  386. endif;
  387. endforeach;
  388. if ($calendar_id):
  389. if ($timeslot_total):
  390. update_option('booked_defaults_'.$calendar_id,$booked_defaults);
  391. else :
  392. delete_option('booked_defaults_'.$calendar_id);
  393. endif;
  394. else :
  395. if ($timeslot_total):
  396. update_option('booked_defaults',$booked_defaults);
  397. else :
  398. delete_option('booked_defaults');
  399. endif;
  400. endif;
  401. endif;
  402. endif;
  403. wp_die();
  404. }
  405. // Delete Appointment
  406. public function booked_admin_delete_appt(){
  407. booked_wpml_ajax();
  408. if (isset($_POST['appt_id'])):
  409. $appt_id = $_POST['appt_id'];
  410. include(BOOKED_AJAX_INCLUDES_DIR . 'admin/delete-appointment.php');
  411. endif;
  412. wp_die();
  413. }
  414. // Approve Appointment
  415. public function booked_admin_approve_appt(){
  416. booked_wpml_ajax();
  417. if (isset($_POST['appt_id'])):
  418. $appt_id = $_POST['appt_id'];
  419. booked_send_user_approved_email($appt_id);
  420. wp_publish_post($appt_id);
  421. do_action('booked_appointment_approved',$appt_id);
  422. endif;
  423. wp_die();
  424. }
  425. // Approve All Appointments
  426. public function booked_admin_approve_all(){
  427. booked_wpml_ajax();
  428. $calendars = get_terms('booked_custom_calendars','orderby=slug&hide_empty=0');
  429. $booked_none_assigned = true;
  430. $default_calendar_id = false;
  431. if (!empty($calendars)):
  432. if (!current_user_can('manage_booked_options')):
  433. $booked_current_user = wp_get_current_user();
  434. $calendars = booked_filter_agent_calendars($booked_current_user,$calendars);
  435. endif;
  436. endif;
  437. if (empty($calendars) && !current_user_can('manage_booked_options')):
  438. wp_die();
  439. elseif(current_user_can('manage_booked_options')):
  440. $args = array(
  441. 'post_type' => 'booked_appointments',
  442. 'posts_per_page' => -1,
  443. 'post_status' => apply_filters('booked_admin_pending_post_status',array('draft')),
  444. 'meta_key' => '_appointment_timestamp',
  445. 'orderby' => 'meta_value_num',
  446. 'order' => 'ASC'
  447. );
  448. else:
  449. $calendar_ids = array();
  450. if (!empty($calendars)):
  451. foreach($calendars as $cal):
  452. $calendar_ids[] = $cal->term_id;
  453. endforeach;
  454. endif;
  455. $args = array(
  456. 'post_type' => 'booked_appointments',
  457. 'posts_per_page' => -1,
  458. 'post_status' => apply_filters('booked_admin_pending_post_status',array('draft')),
  459. 'meta_key' => '_appointment_timestamp',
  460. 'orderby' => 'meta_value_num',
  461. 'order' => 'ASC'
  462. );
  463. if (!empty($calendar_ids)):
  464. $args['tax_query'] = array(
  465. array(
  466. 'taxonomy' => 'booked_custom_calendars',
  467. 'field' => 'term_id',
  468. 'terms' => $calendar_ids,
  469. )
  470. );
  471. endif;
  472. endif;
  473. $appointments_array = array();
  474. if ($args):
  475. $bookedAppointments = new WP_Query($args);
  476. if($bookedAppointments->have_posts()):
  477. while ($bookedAppointments->have_posts()):
  478. $bookedAppointments->the_post();
  479. $appt_id = $bookedAppointments->post->ID;
  480. booked_send_user_approved_email($appt_id);
  481. wp_publish_post($appt_id);
  482. do_action('booked_appointment_approved',$appt_id);
  483. endwhile;
  484. endif;
  485. endif;
  486. wp_die();
  487. }
  488. // Delete All Appointments
  489. public function booked_admin_delete_all(){
  490. booked_wpml_ajax();
  491. $calendars = get_terms('booked_custom_calendars','orderby=slug&hide_empty=0');
  492. $booked_none_assigned = true;
  493. $default_calendar_id = false;
  494. if (!empty($calendars)):
  495. if (!current_user_can('manage_booked_options')):
  496. $booked_current_user = wp_get_current_user();
  497. $calendars = booked_filter_agent_calendars($booked_current_user,$calendars);
  498. endif;
  499. endif;
  500. if (empty($calendars) && !current_user_can('manage_booked_options')):
  501. wp_die();
  502. elseif(current_user_can('manage_booked_options')):
  503. $args = array(
  504. 'post_type' => 'booked_appointments',
  505. 'posts_per_page' => -1,
  506. 'post_status' => apply_filters('booked_admin_pending_post_status',array('draft')),
  507. 'meta_key' => '_appointment_timestamp',
  508. 'orderby' => 'meta_value_num',
  509. 'order' => 'ASC'
  510. );
  511. else:
  512. $calendar_ids = array();
  513. if (!empty($calendars)):
  514. foreach($calendars as $cal):
  515. $calendar_ids[] = $cal->term_id;
  516. endforeach;
  517. endif;
  518. $args = array(
  519. 'post_type' => 'booked_appointments',
  520. 'posts_per_page' => -1,
  521. 'post_status' => apply_filters('booked_admin_pending_post_status',array('draft')),
  522. 'meta_key' => '_appointment_timestamp',
  523. 'orderby' => 'meta_value_num',
  524. 'order' => 'ASC'
  525. );
  526. if (!empty($calendar_ids)):
  527. $args['tax_query'] = array(
  528. array(
  529. 'taxonomy' => 'booked_custom_calendars',
  530. 'field' => 'term_id',
  531. 'terms' => $calendar_ids,
  532. )
  533. );
  534. endif;
  535. endif;
  536. $appointments_array = array();
  537. if ($args):
  538. $bookedAppointments = new WP_Query($args);
  539. if($bookedAppointments->have_posts()):
  540. while ($bookedAppointments->have_posts()):
  541. $bookedAppointments->the_post();
  542. global $post;
  543. $appt_id = $post->ID;
  544. include(BOOKED_AJAX_INCLUDES_DIR . 'admin/delete-appointment.php');
  545. endwhile;
  546. endif;
  547. endif;
  548. wp_die();
  549. }
  550. // Delete Past Appointments
  551. public function booked_admin_delete_past(){
  552. booked_wpml_ajax();
  553. $calendars = get_terms('booked_custom_calendars','orderby=slug&hide_empty=0');
  554. $booked_none_assigned = true;
  555. $default_calendar_id = false;
  556. if (!empty($calendars)):
  557. if (!current_user_can('manage_booked_options')):
  558. $booked_current_user = wp_get_current_user();
  559. $calendars = booked_filter_agent_calendars($booked_current_user,$calendars);
  560. endif;
  561. endif;
  562. if (empty($calendars) && !current_user_can('manage_booked_options')):
  563. wp_die();
  564. elseif(current_user_can('manage_booked_options')):
  565. $args = array(
  566. 'post_type' => 'booked_appointments',
  567. 'posts_per_page' => -1,
  568. 'post_status' => apply_filters('booked_admin_pending_post_status',array('draft')),
  569. 'meta_key' => '_appointment_timestamp',
  570. 'orderby' => 'meta_value_num',
  571. 'order' => 'ASC'
  572. );
  573. else:
  574. $calendar_ids = array();
  575. if (!empty($calendars)):
  576. foreach($calendars as $cal):
  577. $calendar_ids[] = $cal->term_id;
  578. endforeach;
  579. endif;
  580. $args = array(
  581. 'post_type' => 'booked_appointments',
  582. 'posts_per_page' => -1,
  583. 'post_status' => apply_filters('booked_admin_pending_post_status',array('draft')),
  584. 'meta_key' => '_appointment_timestamp',
  585. 'orderby' => 'meta_value_num',
  586. 'order' => 'ASC'
  587. );
  588. if (!empty($calendar_ids)):
  589. $args['tax_query'] = array(
  590. array(
  591. 'taxonomy' => 'booked_custom_calendars',
  592. 'field' => 'term_id',
  593. 'terms' => $calendar_ids,
  594. )
  595. );
  596. endif;
  597. endif;
  598. $late_date = current_time('timestamp');
  599. $appointments_array = array();
  600. if ($args):
  601. $bookedAppointments = new WP_Query($args);
  602. if($bookedAppointments->have_posts()):
  603. while ($bookedAppointments->have_posts()):
  604. $bookedAppointments->the_post();
  605. global $post;
  606. $appt_id = $post->ID;
  607. $timestamp = get_post_meta($post->ID, '_appointment_timestamp',true);
  608. $timeslot = get_post_meta($appt_id, '_appointment_timeslot',true);
  609. $timeslots = explode('-',$timeslot);
  610. $date_to_compare = strtotime(date_i18n('Y-m-d',$timestamp).' '.date_i18n('H:i:s',strtotime($timeslots[0])));
  611. if ($late_date > $date_to_compare):
  612. include(BOOKED_AJAX_INCLUDES_DIR . 'admin/delete-appointment.php');
  613. endif;
  614. endwhile;
  615. endif;
  616. endif;
  617. wp_die();
  618. }
  619. // ------------ LOADERS ------------ //
  620. // Timeslot Select Box
  621. public function booked_admin_get_timeslots_select(){
  622. booked_wpml_ajax();
  623. if ( !isset($_POST['date']) || !isset($_POST['appt_id']) )
  624. wp_die();
  625. $date = esc_html( $_POST['date'] );
  626. $year = date_i18n( 'Y', strtotime($date) );
  627. $month = date_i18n( 'm', strtotime($date) );
  628. $day = date_i18n( 'd', strtotime($date) );
  629. $appt_id = ( isset($_POST['appt_id'] ) ? esc_html( $_POST['appt_id'] ) : false );
  630. booked_timeslots_select( $appt_id, $year, $month, $day );
  631. wp_die();
  632. }
  633. // Timeslots
  634. public function booked_admin_load_timeslots(){
  635. booked_wpml_ajax();
  636. if (isset($_POST['day'])):
  637. $calendar_id = (isset($_POST['calendar_id']) ? esc_html( $_POST['calendar_id'] ) : false);
  638. if ($calendar_id):
  639. $booked_defaults = get_option('booked_defaults_'.$calendar_id);
  640. else :
  641. $booked_defaults = get_option('booked_defaults');
  642. endif;
  643. $day = esc_html( $_POST['day'] );
  644. $time_format = get_option('time_format');
  645. if (!empty($booked_defaults[$day])):
  646. ksort($booked_defaults[$day]);
  647. foreach($booked_defaults[$day] as $time => $count):
  648. echo booked_render_timeslot_info($time_format,$day,$time,$count,$calendar_id, $booked_defaults);
  649. endforeach;
  650. else :
  651. echo '<p><small>'.esc_html__('No time slots.','booked').'</small></p>';
  652. endif;
  653. endif;
  654. wp_die();
  655. }
  656. // Custom Timeslots
  657. public function booked_admin_custom_timeslots_list(){
  658. booked_wpml_ajax();
  659. if (isset($_POST['json_array'])):
  660. $this_timeslot = array();
  661. $this_timeslot['booked_custom_start_date'] = $_POST['start_date'];
  662. if ($_POST['end_date']): $this_timeslot['booked_custom_end_date'] = $_POST['end_date']; else: $this_timeslot['booked_custom_end_date'] = $_POST['start_date']; endif;
  663. $calendar_id = $_POST['calendar_id'];
  664. $timeslots = json_decode(stripslashes($_POST['json_array']),true);
  665. $timeslots_detailed = !empty($_POST['json_array_detailed']) ? json_decode(stripslashes($_POST['json_array_detailed']),true) : array();
  666. if (!empty($timeslots)):
  667. echo '<div class="cts-header"><span class="slotsTitle">'.esc_html__('Spaces Available','booked').'</span>'.esc_html__('Time Slot','booked').'</div>';
  668. foreach ($timeslots as $timeslot => $count):
  669. $time = explode('-',$timeslot);
  670. $time_format = get_option('time_format');
  671. echo '<span class="timeslot" data-timeslot="'.$timeslot.'">';
  672. echo '<span class="slotsBlock"><span class="changeCount minus" data-count="-1"><i class="booked-icon booked-icon-minus-circle"></i></span><span class="count"><em>'.$count.'</em> ' . _n('Space Available','Spaces Available',$count,'booked') . '</span><span class="changeCount add" data-count="1"><i class="booked-icon booked-icon-plus-circle"></i></span></span>';
  673. do_action( 'booked_single_custom_timeslot_start', $this_timeslot, $timeslot, $calendar_id );
  674. if ( !empty( $timeslots_detailed[$timeslot] ) ) {
  675. if ( !empty($timeslots_detailed[$timeslot]['title']) ) {
  676. echo '<span class="title">' . esc_html($timeslots_detailed[$timeslot]['title']) . '</span>';
  677. }
  678. }
  679. if ($time[0] == '0000' && $time[1] == '2400'):
  680. echo '<span class="start"><i class="booked-icon booked-icon-clock"></i>&nbsp;&nbsp;' . strtoupper(esc_html__('All day','booked')) . '</span>';
  681. else :
  682. echo '<span class="start"><i class="booked-icon booked-icon-clock"></i>&nbsp;&nbsp;' . date_i18n($time_format,strtotime('2014-01-01 '.$time[0])) . '</span> &ndash; <span class="end">' . date_i18n($time_format,strtotime('2014-01-01 '.$time[1])) . '</span>';
  683. endif;
  684. do_action( 'booked_single_custom_timeslot_end', $this_timeslot, $timeslot, $calendar_id );
  685. echo '<span class="delete"><i class="booked-icon booked-icon-close"></i></span>';
  686. echo '</span>';
  687. endforeach;
  688. echo '</div>';
  689. endif;
  690. endif;
  691. wp_die();
  692. }
  693. // Full Timeslots
  694. public function booked_admin_load_full_timeslots(){
  695. booked_wpml_ajax();
  696. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  697. booked_render_timeslots($calendar_id);
  698. wp_die();
  699. }
  700. // Full Custom Fields
  701. public function booked_admin_load_full_customfields(){
  702. booked_wpml_ajax();
  703. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  704. booked_render_custom_fields($calendar_id);
  705. wp_die();
  706. }
  707. // Calendar Picker
  708. public function booked_admin_calendar_picker(){
  709. booked_wpml_ajax();
  710. if (isset($_POST['gotoMonth'])):
  711. $timestamp = ($_POST['gotoMonth'] != 'false' ? strtotime($_POST['gotoMonth']) : current_time('timestamp'));
  712. $year = date_i18n('Y',$timestamp);
  713. $month = date_i18n('m',$timestamp);
  714. booked_admin_calendar($year,$month,false,'small');
  715. endif;
  716. wp_die();
  717. }
  718. // Calendar Month
  719. public function booked_admin_calendar_month(){
  720. booked_wpml_ajax();
  721. if (isset($_POST['gotoMonth'])):
  722. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  723. $timestamp = ($_POST['gotoMonth'] != 'false' ? strtotime($_POST['gotoMonth']) : current_time('timestamp'));
  724. $year = date_i18n('Y',$timestamp);
  725. $month = date_i18n('m',$timestamp);
  726. booked_admin_calendar($year,$month,$calendar_id);
  727. endif;
  728. wp_die();
  729. }
  730. // Calendar Day
  731. public function booked_admin_calendar_date(){
  732. booked_wpml_ajax();
  733. if (isset($_POST['date'])):
  734. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  735. booked_admin_calendar_date_content($_POST['date'],$calendar_id);
  736. endif;
  737. wp_die();
  738. }
  739. // Calendar Date Square
  740. public function booked_admin_refresh_date_square(){
  741. booked_wpml_ajax();
  742. if (isset($_POST['date'])):
  743. $calendar_id = (isset($_POST['calendar_id']) ? $_POST['calendar_id'] : false);
  744. booked_admin_calendar_date_square($_POST['date'],$calendar_id);
  745. endif;
  746. wp_die();
  747. }
  748. // User Info Modal
  749. public function booked_admin_user_info_modal(){
  750. booked_wpml_ajax();
  751. if (isset($_POST['user_id'])):
  752. include(BOOKED_AJAX_INCLUDES_DIR . 'admin/user-info.php');
  753. endif;
  754. wp_die();
  755. }
  756. // New Appointment Form
  757. public function booked_admin_new_appointment_form(){
  758. booked_wpml_ajax();
  759. if (isset($_POST['date']) && isset($_POST['timeslot'])):
  760. include(BOOKED_AJAX_INCLUDES_DIR . 'admin/appointment-form.php');
  761. endif;
  762. wp_die();
  763. }
  764. }
  765. }