Guest User

Untitled

a guest
Dec 12th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. function change_location() {
  2.  
  3. // set the timezone
  4. date_default_timezone_set( get_option( 'timezone_string' ) );
  5.  
  6. // get the values
  7. $user_id = get_current_user_id();
  8. $current_time = strtotime( 'now' );
  9. $is_weekend = ( (date( 'N', strtotime( 'tomorrow' ) ) > 5) ? 1 : 0 );
  10.  
  11. // setting config
  12. $use_tasks = get_option( 'mb_use_tasks' );
  13.  
  14. // default - if blank fields
  15. $default_start = strtotime( 'today 9:00 am' );
  16. $default_finish = strtotime( 'today 5:00 pm' );
  17. $default_lunch_in = strtotime( 'today 1:00 pm' );
  18. $default_lunch_out = strtotime( 'today 2:00 pm' );
  19. $next_midnight = strtotime( 'tomorrow 12:00 am' );
  20.  
  21. // user meta
  22. $user_start = get_user_meta( $user_id, 'mb_start_time', true );
  23. $user_finish = get_user_meta( $user_id, 'mb_finish_time', true );
  24. $user_lunch_in = get_user_meta( $user_id, 'mb_lunch_start', true );
  25. $user_lunch_out = get_user_meta( $user_id, 'mb_lunch_finish', true );
  26. $user_current_loc = get_user_meta( $user_id, 'mb_user_location_current', true );
  27. $user_previous_loc = get_user_meta( $user_id, 'mb_user_location_previous', true );
  28.  
  29.  
  30. // set times if not set in user settings
  31. $user_start = ( empty( $user_start ) ? $default_start : strtotime( $user_start ) );
  32. $user_finish = ( empty( $user_finish ) ? $default_finish : strtotime( $user_finish ) );
  33. $user_lunch_in = ( empty( $user_lunch_in ) ? $$default_lunch_in : strtotime( $user_lunch_in ) );
  34. $user_lunch_out = ( empty( $user_lunch_out ) ? $default_lunch_out : strtotime( $user_lunch_out ) );
  35.  
  36.  
  37. // can we use the task feature
  38. if( $use_tasks == 'yes' ) {
  39.  
  40. // reset the manual / auto
  41. if( $current_time > $next_midnight ) {
  42. update_user_meta( $user_id, 'mb_user_automatic_change', '1' );
  43. delete_user_meta( $user_id, 'mb_user_manual_change' );
  44. }
  45.  
  46. // is the current location able to be auto switched
  47. // or we have flagged it has run by auto
  48. if( $mb_location_array[$user_current_loc]['z'] == 'yes' || get_user_meta( $user_id, 'mb_user_automatic_change', true ) == 1 ) {
  49.  
  50. // set user to available
  51. if( !($current_time < $user_start && $current_time > $user_finish) ) {
  52. update_user_meta( $user_id, 'mb_user_location_previous', $user_current_loc );
  53. update_user_meta( $user_id, 'mb_user_location_current', 'available' );
  54. update_user_meta( $user_id, 'mb_user_automatic_change', '1' );
  55. }
  56.  
  57.  
  58. // set user to unavailable
  59. if( ($current_time < $user_start && $current_time > $user_finish) || $is_weekend ) {
  60. update_user_meta( $user_id, 'mb_user_location_previous', $user_current_loc );
  61. update_user_meta( $user_id, 'mb_user_location_current', 'unavailable' );
  62. update_user_meta( $user_id, 'mb_user_automatic_change', '1' );
  63. }
  64.  
  65.  
  66. // set user lunch
  67. if( $current_time > $user_lunch_in && $current_time < $user_lunch_out ) {
  68. update_user_meta( $user_id, 'mb_user_location_previous', $user_current_loc );
  69. update_user_meta( $user_id, 'mb_user_location_current', 'at-lunch' );
  70. update_user_meta( $user_id, 'mb_user_automatic_change', '1' );
  71. }
  72.  
  73.  
  74. // if it was auto switched - flagged
  75. if(
  76. get_user_meta( $user_id, 'mb_user_automatic_change', true ) == 1 &&
  77.  
  78. (
  79. // isnt in the auto task
  80. (!($current_time < $user_start && $current_time > $user_finish)) ||
  81. (($current_time < $user_start || $current_time > $user_finish) || $is_weekend) ||
  82. ($current_time > $user_lunch_in && $current_time < $user_lunch_out)
  83. )
  84.  
  85. ) {
  86. delete_user_meta( $user_id, 'mb_user_automatic_change' );
  87. update_user_meta( $user_id, 'mb_user_location_current', $user_previous_loc );
  88. }
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment