shortcodes.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. if (!shortcode_exists('booked-calendar')) {
  3. add_shortcode('booked-calendar', 'booked_calendar_shortcode');
  4. }
  5. /* CALENDAR SWITCHER SHORTCODE */
  6. class BookedShortcodes {
  7. function __construct(){
  8. // Shortcodes
  9. add_shortcode('booked-calendar-switcher', array($this, 'booked_calendar_switcher_shortcode') );
  10. add_shortcode('booked-calendar', array($this, 'booked_calendar_shortcode') );
  11. add_shortcode('booked-appointments', array($this, 'booked_appointments_shortcode') );
  12. add_shortcode('booked-profile', array($this, 'booked_profile_template') );
  13. add_shortcode('booked-login', array($this, 'booked_login_form') );
  14. // Shortcode Actions
  15. add_action('template_redirect', array($this, 'booked_registration_redirect') );
  16. }
  17. public function booked_registration_redirect(){
  18. $name_requirements = get_option('booked_registration_name_requirements',array('require_name'));
  19. $name_requirements = ( isset($name_requirements[0]) ? $name_requirements[0] : false );
  20. if ( get_option('users_can_register') && !is_user_logged_in() && isset($_POST['booked_reg_submit'] ) ) {
  21. global $registration_complete, $booked_reg_errors, $name, $display_name, $surname, $email, $password;
  22. if ( $name_requirements == 'require_surname' && isset($_POST['booked_reg_surname']) && !$_POST['booked_reg_surname'] ):
  23. $registration_complete = 'error';
  24. $booked_reg_errors[] = esc_html__('A first and last name are required to register.','booked');
  25. elseif ( !isset($_POST['booked_reg_name']) || isset($_POST['booked_reg_name']) && !$_POST['booked_reg_name'] ):
  26. $registration_complete = 'error';
  27. $booked_reg_errors[] = esc_html__('A name is required to register.','booked');
  28. else:
  29. $name = isset($_POST['booked_reg_name']) ? esc_attr($_POST['booked_reg_name']) : false;
  30. $surname = isset($_POST['booked_reg_surname']) ? esc_attr($_POST['booked_reg_surname']) : false;
  31. $display_name = $name . ( $surname ? ' ' . $surname : '' );
  32. $combined_name = $name . ( $surname ? '_' . $surname : '' );
  33. $password = isset($_POST['booked_reg_password']) ? $_POST['booked_reg_password'] : false;
  34. $email = isset($_POST['booked_reg_email']) ? sanitize_email(esc_html($_POST['booked_reg_email'])) : '';
  35. if (isset($_POST['captcha_word'])):
  36. $captcha_word = strtolower($_POST['captcha_word']);
  37. $captcha_code = strtolower($_POST['captcha_code']);
  38. else :
  39. $captcha_word = false;
  40. $captcha_code = false;
  41. endif;
  42. $booked_reg_errors = booked_registration_validation($email,$password,$captcha_word,$captcha_code);
  43. if (empty($booked_reg_errors)):
  44. $registration_complete = booked_complete_registration();
  45. else :
  46. $registration_complete = 'error';
  47. endif;
  48. endif;
  49. } else {
  50. $registration_complete = false;
  51. }
  52. if ($registration_complete && $registration_complete != 'error'){
  53. $user = get_user_by('email', $email);
  54. $creds = array();
  55. if ($user && wp_check_password( $password, $user->data->user_pass, $user->ID)) {
  56. $creds = array('user_login' => $user->data->user_login, 'user_password' => $password);
  57. $creds['remember'] = true;
  58. }
  59. $user = wp_signon( $creds, false );
  60. $page_id = get_queried_object_id();
  61. wp_redirect(get_the_permalink($page_id));
  62. exit;
  63. }
  64. }
  65. public function booked_profile_template(){
  66. if (!is_user_logged_in()) {
  67. return do_shortcode('[booked-login]');
  68. } else {
  69. ob_start();
  70. require(BOOKED_PLUGIN_TEMPLATES_DIR . 'profile.php');
  71. return ob_get_clean();
  72. }
  73. }
  74. /* CALENDAR SWITCHER SHORTCODE */
  75. public function booked_calendar_switcher_shortcode( $attrs ){
  76. if( $attrs ){
  77. extract( $attrs );
  78. }
  79. $rand = rand(0000000,9999999);
  80. $args = array(
  81. 'taxonomy' => 'booked_custom_calendars',
  82. 'hide_empty' => 0,
  83. 'echo' => 0,
  84. 'class' => 'booked_calendar_chooser',
  85. 'id' => 'booked_calendar_chooser_'.$rand,
  86. 'name' => 'booked_calendar_chooser_'.$rand
  87. );
  88. if( isset($id) ){
  89. $args['include'] = wp_parse_id_list( $id );
  90. $args['orderby'] = 'include';
  91. }
  92. if (!get_option('booked_hide_default_calendar')): $args['show_option_all'] = esc_html__('Default Calendar','booked'); endif;
  93. return str_replace( "\n", '', wp_dropdown_categories( $args ) );
  94. }
  95. /* CALENDAR SHORTCODE */
  96. public function booked_calendar_shortcode($atts, $content = null){
  97. $local_time = current_time('timestamp');
  98. $calendars = get_terms('booked_custom_calendars',array('orderby'=>'name','order'=>'ASC','hide_empty'=>false));
  99. $atts = shortcode_atts(
  100. array(
  101. 'size' => 'large',
  102. 'calendar' => false,
  103. 'year' => false,
  104. 'month' => false,
  105. 'day' => false,
  106. 'switcher' => false,
  107. 'style' => 'calendar',
  108. 'members-only' => false
  109. ), $atts );
  110. if ($atts['members-only'] && is_user_logged_in() || !$atts['members-only']):
  111. ob_start();
  112. $atts = apply_filters('booked_calendar_shortcode_atts', $atts );
  113. $rand = rand(0000000,9999999);
  114. echo '<div class="booked-calendar-shortcode-wrap">';
  115. $home_url = booked_home_url();
  116. echo '<div id="data-ajax-url">'.$home_url.'/</div>';
  117. if ($atts['switcher']):
  118. $args = array(
  119. 'taxonomy' => 'booked_custom_calendars',
  120. 'hide_empty' => 0,
  121. 'echo' => 0,
  122. 'id' => 'booked_calendar_chooser_'.$rand,
  123. 'name' => 'booked_calendar_chooser_'.$rand,
  124. 'class' => 'booked_calendar_chooser',
  125. 'selected' => $atts['calendar'],
  126. 'orderby' => 'name',
  127. 'order' => 'ASC'
  128. );
  129. if (!get_option('booked_hide_default_calendar')): $args['show_option_all'] = esc_html__('Default Calendar','booked'); endif;
  130. echo '<div class="booked-calendarSwitcher '.$atts['style'].'"><p><i class="booked-icon booked-icon-calendar"></i>' . str_replace( "\n", '', wp_dropdown_categories( $args ) ) . '</p></div>';
  131. endif;
  132. if (get_option('booked_hide_default_calendar') && $atts['switcher'] && !$atts['calendar'] && !empty($calendars)):
  133. $atts['calendar'] = $calendars[0]->term_id;
  134. endif;
  135. if ($atts['year'] || $atts['month'] || $atts['day']):
  136. $force_calendar = true;
  137. $year = ($atts['year'] ? $atts['year'] : date_i18n('Y',$local_time));
  138. $month = ($atts['month'] ? date_i18n('m',strtotime($year.'-'.$atts['month'].'-01')) : date_i18n('m',$local_time));
  139. $day = ($atts['day'] ? date_i18n('d',strtotime($year.'-'.$month.'-'.$atts['day'])) : date_i18n('d',$local_time));
  140. $default_date = $year.'-'.$month.'-'.$day;
  141. else:
  142. $default_date = date_i18n('Y-m-d',$local_time);
  143. $force_calendar = false;
  144. endif;
  145. if (!$atts['style'] || $atts['style'] != 'list'):
  146. echo '<div class="booked-calendar-wrap '.$atts['size'].'"'.($force_calendar ? ' data-default="'.$default_date.'"' : '').'>';
  147. booked_fe_calendar($atts['year'],$atts['month'],$atts['calendar'],$force_calendar);
  148. echo '</div>';
  149. elseif ($atts['style'] == 'list'):
  150. echo '<div class="booked-list-view booked-calendar-wrap '.$atts['size'].'"'.($force_calendar ? ' data-default="'.$default_date.'"' : '').'>';
  151. booked_fe_appointment_list_content($default_date,$atts['calendar'],$force_calendar);
  152. echo '</div>';
  153. endif;
  154. echo '</div>';
  155. wp_reset_postdata();
  156. return ob_get_clean();
  157. else:
  158. return false;
  159. endif;
  160. }
  161. /* APPOINTMENTS SHORTCODE */
  162. public function booked_appointments_shortcode($atts = null, $content = null) {
  163. ob_start();
  164. if ( is_user_logged_in() ):
  165. $booked_current_user = wp_get_current_user();
  166. $my_id = $booked_current_user->ID;
  167. $historic = isset($atts['historic']) && $atts['historic'] ? true : false;
  168. $time_format = get_option('time_format');
  169. $date_format = get_option('date_format');
  170. $appointments_array = booked_user_appointments($my_id,false,$time_format,$date_format,$historic);
  171. $total_appts = count($appointments_array);
  172. $appointment_default_status = get_option('booked_new_appointment_default','draft');
  173. $only_titles = get_option('booked_show_only_titles',false);
  174. if (!isset($atts['remove_wrapper'])): echo '<div id="booked-profile-page" class="booked-shortcode">'; endif;
  175. echo '<div class="booked-profile-appt-list">';
  176. if ($historic):
  177. if ($total_appts):
  178. echo '<h4><span class="count">' . number_format($total_appts) . '</span> ' . _n('Past Appointment','Past Appointments',$total_appts,'booked') . '</h4>';
  179. else:
  180. echo '<p class="booked-no-margin">'.esc_html__('No past appointments.','booked').'</p>';
  181. endif;
  182. else:
  183. if ($total_appts):
  184. echo '<h4><span class="count">' . number_format($total_appts) . '</span> ' . _n('Upcoming Appointment','Upcoming Appointments',$total_appts,'booked') . '</h4>';
  185. else:
  186. echo '<p class="booked-no-margin">'.esc_html__('No upcoming appointments.','booked').'</p>';
  187. endif;
  188. endif;
  189. foreach($appointments_array as $appt):
  190. $today = date_i18n($date_format);
  191. $date_display = date_i18n($date_format,$appt['timestamp']);
  192. if ($date_display == $today){
  193. $date_display = esc_html__('Today','booked');
  194. $day_name = '';
  195. } else {
  196. $day_name = date_i18n('l',$appt['timestamp']).', ';
  197. }
  198. $date_to_convert = date_i18n('Y-m-d',$appt['timestamp']);
  199. $cf_meta_value = get_post_meta($appt['post_id'], '_cf_meta_value',true);
  200. $timeslots = explode('-',$appt['timeslot']);
  201. $time_start = date_i18n($time_format,strtotime($timeslots[0]));
  202. $time_end = date_i18n($time_format,strtotime($timeslots[1]));
  203. $appt_date_time = strtotime($date_to_convert.' '.date_i18n('H:i:s',strtotime($timeslots[0])));
  204. $atc_date_startend = date_i18n('Y-m-d',$appt['timestamp']);
  205. $atc_time_start = date_i18n('H:i:s',strtotime($timeslots[0]));
  206. $atc_time_end = date_i18n('H:i:s',strtotime($timeslots[1]));
  207. $current_timestamp = current_time('timestamp');
  208. $cancellation_buffer = get_option('booked_cancellation_buffer',0);
  209. if ($cancellation_buffer):
  210. if ($cancellation_buffer < 1){
  211. $time_type = 'minutes';
  212. $time_count = $cancellation_buffer * 60;
  213. } else {
  214. $time_type = 'hours';
  215. $time_count = $cancellation_buffer;
  216. }
  217. $buffered_timestamp = strtotime('+'.$time_count.' '.$time_type,$current_timestamp);
  218. $date_to_compare = $buffered_timestamp;
  219. else:
  220. $date_to_compare = current_time('timestamp');
  221. endif;
  222. $timeslotText = '';
  223. $status = ($appt['status'] != 'publish' && $appt['status'] != 'future' ? esc_html__('pending','booked') : esc_html__('approved','booked'));
  224. $status_class = $appt['status'] != 'publish' && $appt['status'] != 'future' ? 'pending' : 'approved';
  225. $ts_title = get_post_meta($appt['post_id'], '_appointment_title',true);
  226. if ($timeslots[0] == '0000' && $timeslots[1] == '2400'):
  227. if ($only_titles && !$ts_title || !$only_titles):
  228. $timeslotText = esc_html__('All day','booked');
  229. endif;
  230. $atc_date_startend_end = date_i18n('Y-m-d',strtotime(date_i18n('Y-m-d',$appt['timestamp']) . '+ 1 Day'));
  231. $atc_time_end = '00:00:00';
  232. else :
  233. if ($only_titles && !$ts_title || !$only_titles):
  234. $timeslotText = (!get_option('booked_hide_end_times') ? esc_html__('from','booked').' ' : esc_html__('at','booked').' ') . $time_start . (!get_option('booked_hide_end_times') ? ' &ndash; '.$time_end : '');
  235. endif;
  236. $atc_date_startend_end = $atc_date_startend;
  237. endif;
  238. echo '<span class="appt-block bookedClearFix '.(!$historic ? $status_class : 'approved').'" data-appt-id="'.$appt['post_id'].'">';
  239. if (!$historic):
  240. if ($appointment_default_status !== 'publish' && $appt['status'] !== 'future' || $appointment_default_status == 'publish' && $status_class == 'pending'):
  241. echo '<span class="status-block">'.($status_class == 'pending' ? '<i class="booked-icon booked-icon-radio-unchecked"></i>' : '<i class="booked-icon booked-icon-radio-checked"></i>').'&nbsp;&nbsp;'.$status.'</span>';
  242. endif;
  243. endif;
  244. echo (!empty($appt['calendar_id']) ? '<i class="booked-icon booked-icon-calendar"></i><strong>'.esc_html__('Calendar','booked').':</strong> '.$appt['calendar_id'][0]->name.'<br>' : '');
  245. echo '<i class="booked-icon booked-icon-clock"></i>'.($ts_title ? '<strong>'.$ts_title.':</strong>&nbsp;&nbsp;' : '').$day_name.$date_display.'&nbsp;&nbsp;' . $timeslotText;
  246. do_action('booked_shortcode_appointments_additional_information', $appt['post_id']);
  247. echo ($cf_meta_value ? '<br><i class="booked-icon booked-icon-info"></i><a href="#" class="booked-show-cf">'.esc_html__('Additional information','booked').'</a><div class="cf-meta-values-hidden">'.$cf_meta_value.'</div>' : '');
  248. if (!$historic):
  249. $calendar_button_array = array(
  250. 'atc_date_startend' => $atc_date_startend,
  251. 'atc_time_start' => $atc_time_start,
  252. 'atc_date_startend_end' => $atc_date_startend_end,
  253. 'atc_time_end' => $atc_time_end,
  254. );
  255. ob_start();
  256. booked_add_to_calendar_button($calendar_button_array,$cf_meta_value);
  257. if ( apply_filters('booked_shortcode_appointments_allow_cancel', true, $appt['post_id']) && !get_option('booked_dont_allow_user_cancellations',false) ) { if ( $appt_date_time >= $date_to_compare ) { echo '<a href="#" data-appt-id="'.$appt['post_id'].'" class="cancel">'.esc_html__('Cancel Appointment','booked').'</a>'; } }
  258. do_action('booked_shortcode_appointments_buttons', $appt['post_id']);
  259. $buttons_content = ob_get_clean();
  260. if ($buttons_content):
  261. echo '<div class="booked-cal-buttons">';
  262. echo $buttons_content;
  263. echo '</div>';
  264. endif;
  265. endif;
  266. echo '</span>';
  267. endforeach;
  268. echo '</div>';
  269. if (!isset($atts['remove_wrapper'])): echo '</div>'; endif;
  270. wp_reset_postdata();
  271. else :
  272. return '<p>'.esc_html__('Please log in to view your upcoming appointments.','booked').'</p>';
  273. endif;
  274. return ob_get_clean();
  275. }
  276. /* LOGIN SHORTCODE */
  277. public function booked_login_form( $atts, $content = null ) {
  278. global $post;
  279. if (!is_user_logged_in()) {
  280. ob_start();
  281. ?><div id="booked-profile-page">
  282. <div id="booked-page-form">
  283. <ul class="booked-tabs login bookedClearFix">
  284. <li<?php if ( !isset($_POST['booked_reg_submit'] ) ) { ?> class="active"<?php } ?>><a href="#login"><i class="booked-icon booked-icon-lock"></i><?php esc_html_e('Sign In','booked'); ?></a></li>
  285. <?php if ( get_option('users_can_register') ): ?><li<?php if ( isset($_POST['booked_reg_submit'] ) ) { ?> class="active"<?php } ?>><a href="#register"><i class="booked-icon booked-icon-pencil"></i><?php esc_html_e('Register','booked'); ?></a></li><?php endif; ?>
  286. <li><a href="#forgot"><i class="booked-icon booked-icon-question-circle"></i><?php esc_html_e('Forgot Password','booked'); ?></a></li>
  287. </ul>
  288. <div id="profile-login" class="booked-tab-content">
  289. <?php if (isset($reset) && $reset == true) { ?>
  290. <p class="booked-form-notice">
  291. <strong><?php esc_html_e('Success!','booked'); ?></strong><br />
  292. <?php esc_html_e('Check your email to reset your password.','booked'); ?>
  293. </p>
  294. <?php } ?>
  295. <?php $login_redirect = get_option('booked_login_redirect_page') ? get_option('booked_login_redirect_page') : $post->ID; ?>
  296. <div class="booked-form-wrap bookedClearFix">
  297. <div class="booked-custom-error"><?php esc_html_e('Both fields are required to log in.','booked'); ?></div>
  298. <?php if (isset($_GET['loginfailed'])): ?><div class="booked-custom-error not-hidden"><?php esc_html_e('Sorry, those login credentials are incorrect.','booked'); ?></div><?php endif; ?>
  299. <?php $custom_login_form_message = get_option('booked_custom_login_message',false);
  300. if ($custom_login_form_message):
  301. echo do_shortcode(wpautop($custom_login_form_message));
  302. endif; ?>
  303. <?php echo wp_login_form( array( 'echo' => false, 'redirect' => get_the_permalink($login_redirect), 'label_username' => esc_html__( 'Email Address','booked' ) ) ); ?>
  304. </div>
  305. </div>
  306. <?php if (get_option('users_can_register')): ?>
  307. <div id="profile-register" class="booked-tab-content">
  308. <div class="booked-form-wrap bookedClearFix">
  309. <?php global $registration_complete,$booked_reg_errors;
  310. if ($registration_complete == 'error'){
  311. ?><div class="booked-custom-error" style="display:block"><?php echo implode('<br>', $booked_reg_errors); ?></div><?php
  312. }
  313. $name = (isset($_POST['booked_reg_name']) ? $_POST['booked_reg_name'] : '');
  314. $surname = (isset($_POST['booked_reg_surname']) ? $_POST['booked_reg_surname'] : '');
  315. $email = (isset($_POST['booked_reg_email']) ? $_POST['booked_reg_email'] : '');
  316. $password = (isset($_POST['booked_reg_password']) ? $_POST['booked_reg_password'] : '');
  317. booked_registration_form($name,$surname,$email,$password);
  318. ?>
  319. </div>
  320. </div>
  321. <?php endif; ?>
  322. <div id="profile-forgot" class="booked-tab-content">
  323. <div class="booked-form-wrap bookedClearFix">
  324. <div class="booked-custom-error"><?php esc_html_e('An email address is required to reset your password.','booked'); ?></div>
  325. <form method="post" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" class="wp-user-form">
  326. <p class="username">
  327. <label for="user_login"><?php esc_html_e('What is your email address?','booked'); ?></label>
  328. <input type="text" name="user_login" value="" size="20" id="user_login" tabindex="1001" />
  329. </p>
  330. <?php do_action('login_form', 'resetpass'); ?>
  331. <input type="submit" name="user-submit" value="<?php esc_html_e('Reset my password','booked'); ?>" class="user-submit button-primary" tabindex="1002" />
  332. <input type="hidden" name="redirect_to" value="<?php the_permalink(); ?>?reset=true" />
  333. <input type="hidden" name="user-cookie" value="1" />
  334. </form>
  335. </div>
  336. </div>
  337. </div><!-- END #booked-page-form -->
  338. </div><?php
  339. $content = ob_get_clean();
  340. }
  341. return $content;
  342. }
  343. }
  344. new BookedShortcodes;