general-functions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <?php
  2. function booked_avatar($user_id,$size = 150){
  3. if (get_user_meta($user_id, 'avatar',true)):
  4. if (wp_get_attachment_image( get_user_meta($user_id,'avatar',true), array($size,$size) )):
  5. return wp_get_attachment_image( get_user_meta($user_id,'avatar',true), array($size,$size) );
  6. else:
  7. return get_avatar($user_id, $size);
  8. endif;
  9. else :
  10. return get_avatar($user_id, $size);
  11. endif;
  12. }
  13. function booked_compress_css($css){
  14. // Remove Comments
  15. $regex = array("`^([\t\s]+)`ism"=>'',"`^\/\*(.+?)\*\/`ism"=>"","`([\n\A;]+)\/\*(.+?)\*\/`ism"=>"$1","`([\n\A;\s]+)//(.+?)[\n\r]`ism"=>"$1\n","`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n");
  16. $css = preg_replace(array_keys($regex),$regex,$css);
  17. // Remove tabs, spaces, newlines, etc.
  18. $css = str_replace( array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $css );
  19. return $css;
  20. }
  21. function booked_add_to_calendar_button($dates,$cf_meta_value){
  22. if (!get_option('booked_hide_google_link',false)):
  23. // Convert BRs to line breaks
  24. $cf_meta_value = preg_replace('#<br\s*?/?>#i', "\n", $cf_meta_value);
  25. wp_enqueue_script('booked-atc');
  26. ?><div title="<?php esc_attr_e('Add to Calendar','booked'); ?>" class="addeventatc atc-style-booked google-cal-button">
  27. <?php esc_html_e('Add to Calendar','booked'); ?>
  28. <span class="start"><?php echo $dates['atc_date_startend']; ?> <?php echo $dates['atc_time_start']; ?></span>
  29. <span class="end"><?php echo $dates['atc_date_startend_end']; ?> <?php echo $dates['atc_time_end']; ?></span>
  30. <span class="timezone"><?php echo booked_get_timezone_string(); ?></span>
  31. <span class="title"><?php echo sprintf(esc_html__('Appointment with %s','booked'),get_bloginfo('name')); ?></span>
  32. <span class="description"><?php echo strip_tags(str_replace('<br>','
  33. ',$cf_meta_value)); ?></span>
  34. <span class="location"><?php echo get_bloginfo('name'); ?></span>
  35. </div><?php
  36. endif;
  37. }
  38. function booked_get_timezone_string() {
  39. if ( $timezone = get_option( 'timezone_string' ) )
  40. return $timezone;
  41. if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )
  42. return 'UTC';
  43. $utc_offset *= 3600;
  44. if ( $timezone = timezone_name_from_abbr( '', $utc_offset, 0 ) ) {
  45. return $timezone;
  46. }
  47. $is_dst = date_i18n( 'I' );
  48. foreach ( timezone_abbreviations_list() as $abbr ) {
  49. foreach ( $abbr as $city ) {
  50. if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset )
  51. return $city['timezone_id'];
  52. }
  53. }
  54. return 'UTC';
  55. }
  56. function booked_get_profile_page(){
  57. global $wpdb;
  58. $shortcode = '[booked-profile';
  59. $query = "SELECT * FROM `{$wpdb->posts}` WHERE `post_status` != 'trash' AND `post_type` = 'page' AND `post_content` LIKE '%{$shortcode}%'";
  60. $pages = $wpdb->get_results($query);
  61. if (!$pages){
  62. return;
  63. }
  64. return $pages[0];
  65. }
  66. function booked_home_url(){
  67. if (function_exists('pll_home_url')):
  68. $home_url = rtrim(pll_home_url(), "/");
  69. else:
  70. $home_url = rtrim(home_url(), "/");
  71. endif;
  72. return $home_url;
  73. }
  74. function booked_get_name( $user_id, $part = 'full' ){
  75. switch ($part):
  76. case 'full':
  77. $username = get_user_meta( $user_id, 'first_name', true ) ? get_user_meta( $user_id, 'first_name', true ).(get_user_meta( $user_id, 'last_name', true ) ? ' '.get_user_meta( $user_id, 'last_name', true ) : '') : false;
  78. if (!$username):
  79. $user_info = get_userdata($user_id);
  80. if (!empty($user_info)): $username = $user_info->display_name; else: return false; endif;
  81. endif;
  82. if (!$username):
  83. $user_info = get_userdata($user_id);
  84. if (!empty($user_info)): $username = $user_info->user_login; else: return false; endif;
  85. endif;
  86. return $username;
  87. break;
  88. case 'first':
  89. $username = get_user_meta( $user_id, 'first_name', true ) ? get_user_meta( $user_id, 'first_name', true ) : false;
  90. if (!$username):
  91. $username = booked_get_name( $user_id );
  92. endif;
  93. return $username;
  94. break;
  95. case 'last':
  96. $username = get_user_meta( $user_id, 'last_name', true ) ? get_user_meta( $user_id, 'last_name', true ) : false;
  97. if (!$username):
  98. return '';
  99. endif;
  100. return $username;
  101. break;
  102. endswitch;
  103. }
  104. function booked_user_role()
  105. {
  106. $booked_current_user = wp_get_current_user();
  107. $roles = $booked_current_user->roles;
  108. $role = array_shift($roles);
  109. return $role;
  110. }
  111. function booked_filter_agent_calendars($this_user,$calendars)
  112. {
  113. $booked_current_user_email = $this_user->data->user_email;
  114. foreach($calendars as $key => $calendar):
  115. $term_meta = get_option( "taxonomy_".$calendar->term_id );
  116. $calendar_owner = $term_meta['notifications_user_id'];
  117. if ($calendar_owner != $booked_current_user_email):
  118. unset($calendars[$key]);
  119. endif;
  120. endforeach;
  121. return $calendars;
  122. }
  123. function booked_convertTime($time)
  124. {
  125. settype($time, 'integer');
  126. if ($time < 1) {
  127. return;
  128. }
  129. $hours = lz(floor($time / 60));
  130. $minutes = lz(($time % 60));
  131. return $hours.':'.$minutes;
  132. }
  133. // lz = leading zero
  134. function lz($num)
  135. {
  136. return (strlen($num) < 2) ? "0{$num}" : $num;
  137. }
  138. function booked_pending_appts_count(){
  139. $calendars = get_terms('booked_custom_calendars','orderby=slug&hide_empty=0');
  140. if (!empty($calendars) && !current_user_can('manage_booked_options')):
  141. $booked_current_user = wp_get_current_user();
  142. $calendars = booked_filter_agent_calendars($booked_current_user,$calendars);
  143. if (!empty($calendars)):
  144. foreach($calendars as $calendar):
  145. $calendar_ids[] = $calendar->term_id;
  146. endforeach;
  147. $args = array(
  148. 'posts_per_page' => -1,
  149. 'post_status' => apply_filters('booked_admin_pending_post_status',array('draft')),
  150. 'post_type' => 'booked_appointments',
  151. 'tax_query' => array(
  152. array(
  153. 'taxonomy' => 'booked_custom_calendars',
  154. 'field' => 'term_id',
  155. 'terms' => $calendar_ids,
  156. ),
  157. ),
  158. );
  159. else:
  160. return 0;
  161. endif;
  162. elseif (empty($calendars) && !current_user_can('manage_booked_options')):
  163. return 0;
  164. else:
  165. $args = array(
  166. 'posts_per_page' => -1,
  167. 'post_status' => apply_filters('booked_admin_pending_post_status',array('draft')),
  168. 'post_type' => 'booked_appointments',
  169. );
  170. endif;
  171. $pending_count_query = new WP_Query($args);
  172. return $pending_count_query->found_posts;
  173. }
  174. function booked_registration_validation( $email, $password, $captcha_value = false, $captcha_from_user = false ) {
  175. global $reg_errors;
  176. $reg_errors = new WP_Error;
  177. $errors = array();
  178. if ($captcha_value):
  179. if ($captcha_value != $captcha_from_user):
  180. $reg_errors->add('captcha', esc_html__('The text you\'ve entered does not match the image.','booked'));
  181. else :
  182. $captcha = new ReallySimpleCaptcha();
  183. $captcha->remove($captcha_value);
  184. endif;
  185. endif;
  186. if ( !$email || !$password ) {
  187. $reg_errors->add('field', esc_html__('All fields are required to register.','booked'));
  188. }
  189. if ( username_exists( $email ) ) {
  190. $reg_errors->add('user_name', esc_html__('That username already exists.','booked'));
  191. }
  192. if ( ! validate_username( $email ) ) {
  193. $reg_errors->add( 'username_invalid', esc_html__('That name is not valid.','booked'));
  194. }
  195. if ( !is_email( $email ) ) {
  196. $reg_errors->add( 'email_invalid', esc_html__('That email address is not valid.','booked'));
  197. }
  198. if ( email_exists( $email ) ) {
  199. $reg_errors->add( 'email', esc_html__('That email is already in use.','booked'));
  200. }
  201. if ( is_wp_error( $reg_errors ) ) {
  202. foreach ( $reg_errors->get_error_messages() as $error ) {
  203. $errors[] = $error;
  204. }
  205. }
  206. return $errors;
  207. }
  208. function booked_complete_registration() {
  209. global $reg_errors, $name, $surname, $display_name, $username, $password, $email;
  210. if ( 1 > count( $reg_errors->get_error_messages() ) ) {
  211. $userdata = array(
  212. 'user_login' => $email,
  213. 'user_email' => $email,
  214. 'user_pass' => $password,
  215. 'first_name' => $name,
  216. 'last_name' => $surname
  217. );
  218. $user_id = wp_insert_user( $userdata );
  219. update_user_meta( $user_id, 'nickname', $display_name );
  220. wp_update_user( array ('ID' => $user_id, 'display_name' => $display_name ) );
  221. // Send a registration welcome email to the new user?
  222. $email_content = get_option('booked_registration_email_content');
  223. $email_subject = get_option('booked_registration_email_subject');
  224. if ($email_content && $email_subject):
  225. $token_replacements = array(
  226. 'name' => $display_name,
  227. 'username' => $email,
  228. 'password' => $password,
  229. 'email' => $email
  230. );
  231. $email_content = booked_token_replacement( $email_content,$token_replacements,'user' );
  232. $email_subject = booked_token_replacement( $email_subject,$token_replacements );
  233. do_action('booked_registration_email',$email, $email_subject, $email_content);
  234. endif;
  235. return '<p class="booked-form-notice"><strong>'.esc_html__('Success!','booked').'</strong><br />'.esc_html__('Registration complete, please check your email for login information.','booked').'</p>';
  236. } else {
  237. return false;
  238. }
  239. }
  240. function booked_which_admin_to_send_email($calendar_id = false){
  241. $admin_email = false;
  242. if ($calendar_id):
  243. $term_meta = get_option( "taxonomy_$calendar_id" );
  244. $selected_value = $term_meta['notifications_user_id'];
  245. if ($selected_value):
  246. $admin_email = $selected_value;
  247. endif;
  248. endif;
  249. if (!$admin_email && get_option('booked_default_email_user')):
  250. $admin_email = get_option('booked_default_email_user');
  251. endif;
  252. if (!$admin_email):
  253. $admin_email = get_option( 'admin_email' );
  254. endif;
  255. return $admin_email;
  256. }
  257. function booked_registration_form($name, $surname, $email, $password){
  258. $name_requirements = get_option('booked_registration_name_requirements',array('require_name'));
  259. $name_requirements = ( isset($name_requirements[0]) ? $name_requirements[0] : false );
  260. ?><form action="" method="post" class="wp-user-form">
  261. <p class="name">
  262. <label for="name"><?php echo ( $name_requirements == 'require_surname' ? esc_html__('First Name','booked') : esc_html__('Your Name','booked') ); ?></label>
  263. <input type="text" name="booked_reg_name" value="<?php echo ( isset( $name ) ? $name : null ); ?>" id="name" tabindex="101" />
  264. </p>
  265. <?php if ($name_requirements == 'require_surname'): ?>
  266. <p class="surname">
  267. <label for="surname"><?php esc_html_e('Last Name','booked'); ?></label>
  268. <input type="text" name="booked_reg_surname" value="<?php echo ( isset( $surname ) ? $surname : null ); ?>" id="surname" tabindex="102" />
  269. </p>
  270. <?php endif; ?>
  271. <p class="email">
  272. <label for="email"><?php esc_html_e('Email Address','booked'); ?></label>
  273. <input type="email" name="booked_reg_email" value="<?php echo ( isset( $email ) ? $email : null ); ?>" id="email" tabindex="103" />
  274. </p>
  275. <p class="password">
  276. <label for="password"><?php esc_html_e('Choose a Password','booked'); ?></label>
  277. <input type="password" name="booked_reg_password" value="<?php echo ( isset( $password ) ? $password : null ); ?>" id="password" tabindex="104" />
  278. </p>
  279. <?php if (class_exists('ReallySimpleCaptcha')) :
  280. ?><p class="captcha">
  281. <label for="captcha_code"><?php esc_html_e('Please enter the following text:','booked'); ?></label><?php
  282. $rsc_url = WP_PLUGIN_URL . '/really-simple-captcha/';
  283. $captcha = new ReallySimpleCaptcha();
  284. $captcha->fg = array(150,150,150);
  285. $captcha_word = $captcha->generate_random_word(); //generate a random string with letters
  286. $captcha_prefix = mt_rand(); //random number
  287. $captcha_image = $captcha->generate_image($captcha_prefix, $captcha_word); //generate the image file. it returns the file name
  288. $captcha_file = rtrim(get_bloginfo('wpurl'), '/') . '/wp-content/plugins/really-simple-captcha/tmp/' . $captcha_image; //construct the absolute URL of the captcha image
  289. echo '<img class="captcha-image" src="'.$rsc_url.'tmp/'.$captcha_image.'">';
  290. ?><input type="text" name="captcha_code" value="" tabindex="104" />
  291. <input type="hidden" name="captcha_word" value="<?php echo $captcha_word; ?>" />
  292. </p><?php
  293. endif; ?>
  294. <input type="submit" name="booked_reg_submit" value="<?php esc_html_e('Register','booked'); ?>" class="user-submit button-primary" tabindex="105" />
  295. </form><?php
  296. }
  297. /* Custom Time Slot Functions */
  298. function booked_apply_custom_timeslots_filter($booked_defaults = false,$calendar_id = false){
  299. $custom_timeslots_array = array();
  300. $booked_custom_timeslots_encoded = get_option('booked_custom_timeslots_encoded');
  301. $booked_custom_timeslots_decoded = json_decode($booked_custom_timeslots_encoded,true);
  302. if (!empty($booked_custom_timeslots_decoded)):
  303. $custom_timeslots_array = booked_custom_timeslots_reconfigured($booked_custom_timeslots_decoded);
  304. foreach($custom_timeslots_array as $key => $value):
  305. if ($value['booked_custom_start_date']):
  306. $formatted_date = date_i18n('Ymd',strtotime($value['booked_custom_start_date']));
  307. $formatted_end_date = date_i18n('Ymd',strtotime($value['booked_custom_end_date']));
  308. // To include or not to include?
  309. if (!isset($value['booked_custom_calendar_id']) || $calendar_id && isset($value['booked_custom_calendar_id']) && $value['booked_custom_calendar_id'] == $calendar_id || !$calendar_id && !$value['booked_custom_calendar_id']){
  310. if (!$value['booked_custom_end_date']){
  311. // Single Date
  312. if ($value['vacationDayCheckbox']){
  313. // Time slots disabled
  314. $booked_defaults[$formatted_date] = array();
  315. } else {
  316. // Add time slots to this date
  317. $booked_defaults[$formatted_date] = $value['booked_this_custom_timelots'];
  318. }
  319. } else {
  320. // Multiple Dates
  321. $tempDate = $formatted_date;
  322. do {
  323. if ($value['vacationDayCheckbox']){
  324. // Time slots disabled
  325. $booked_defaults[$tempDate] = array();
  326. } else {
  327. // Add time slots to this date
  328. $booked_defaults[$tempDate] = $value['booked_this_custom_timelots'];
  329. }
  330. $tempDate = date_i18n('Ymd',strtotime($tempDate . ' +1 day'));
  331. } while ($tempDate <= $formatted_end_date);
  332. }
  333. }
  334. endif;
  335. endforeach;
  336. endif;
  337. return $booked_defaults;
  338. }
  339. /* Custom Time Slot Functions */
  340. function booked_apply_custom_timeslots_details_filter($booked_defaults = false,$calendar_id = false){
  341. $custom_timeslots_array = array();
  342. $booked_custom_timeslots_encoded = get_option('booked_custom_timeslots_encoded');
  343. $booked_custom_timeslots_decoded = json_decode($booked_custom_timeslots_encoded,true);
  344. if (!empty($booked_custom_timeslots_decoded)):
  345. $custom_timeslots_array = booked_custom_timeslots_reconfigured($booked_custom_timeslots_decoded);
  346. foreach($custom_timeslots_array as $key => $value):
  347. if ($value['booked_custom_start_date']):
  348. $formatted_date = date_i18n('Ymd',strtotime($value['booked_custom_start_date']));
  349. $formatted_end_date = date_i18n('Ymd',strtotime($value['booked_custom_end_date']));
  350. // To include or not to include?
  351. if (!isset($value['booked_custom_calendar_id']) || $calendar_id && isset($value['booked_custom_calendar_id']) && $value['booked_custom_calendar_id'] == $calendar_id || !$calendar_id && !$value['booked_custom_calendar_id']){
  352. if (!$value['booked_custom_end_date']){
  353. // Single Date
  354. if ($value['vacationDayCheckbox']){
  355. // Time slots disabled
  356. $booked_defaults[$formatted_date] = array();
  357. $booked_defaults[$formatted_date.'-details'] = array();
  358. } else {
  359. // Add time slots to this date
  360. $booked_defaults[$formatted_date] = $value['booked_this_custom_timelots'];
  361. $booked_defaults[$formatted_date.'-details'] = !empty($value['booked_this_custom_timelots_details']) ? $value['booked_this_custom_timelots_details'] : array();
  362. }
  363. } else {
  364. // Multiple Dates
  365. $tempDate = $formatted_date;
  366. do {
  367. if ($value['vacationDayCheckbox']){
  368. // Time slots disabled
  369. $booked_defaults[$tempDate] = array();
  370. $booked_defaults[$tempDate.'-details'] = array();
  371. } else {
  372. // Add time slots to this date
  373. $booked_defaults[$tempDate] = $value['booked_this_custom_timelots'];
  374. $booked_defaults[$tempDate.'-details'] = !empty($value['booked_this_custom_timelots_details']) ? $value['booked_this_custom_timelots_details'] : array();
  375. }
  376. $tempDate = date_i18n('Ymd',strtotime($tempDate . ' +1 day'));
  377. } while ($tempDate <= $formatted_end_date);
  378. }
  379. }
  380. endif;
  381. endforeach;
  382. endif;
  383. return $booked_defaults;
  384. }
  385. function booked_custom_timeslots_reconfigured($booked_custom_timeslots_decoded){
  386. $total_fields = ( isset($booked_custom_timeslots_decoded['booked_custom_start_date']) && is_array($booked_custom_timeslots_decoded['booked_custom_start_date']) ? count( $booked_custom_timeslots_decoded['booked_custom_start_date'] ) - 1 : 0 );
  387. $custom_timeslots_array = array();
  388. $counter = 0;
  389. if ($total_fields):
  390. do {
  391. foreach($booked_custom_timeslots_decoded as $key => $values):
  392. if ($key == 'booked_this_custom_timelots' || $key == 'booked_this_custom_timelots_details'):
  393. $values = json_decode($values[$counter],true);
  394. $custom_timeslots_array[$counter][$key] = $values;
  395. else:
  396. $custom_timeslots_array[$counter][$key] = (isset($values[$counter]) ? $values[$counter] : $values);
  397. endif;
  398. endforeach;
  399. $counter++;
  400. } while($total_fields >= $counter);
  401. else :
  402. $custom_timeslots_array[0] = $booked_custom_timeslots_decoded;
  403. endif;
  404. return $custom_timeslots_array;
  405. }
  406. /* End Custom Time Slot Functions */