Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function change_location() {
- // set the timezone
- date_default_timezone_set( get_option( 'timezone_string' ) );
- // get the values
- $user_id = get_current_user_id();
- $current_time = strtotime( 'now' );
- $is_weekend = ( (date( 'N', strtotime( 'tomorrow' ) ) > 5) ? 1 : 0 );
- // setting config
- $use_tasks = get_option( 'mb_use_tasks' );
- // default - if blank fields
- $default_start = strtotime( 'today 9:00 am' );
- $default_finish = strtotime( 'today 5:00 pm' );
- $default_lunch_in = strtotime( 'today 1:00 pm' );
- $default_lunch_out = strtotime( 'today 2:00 pm' );
- $next_midnight = strtotime( 'tomorrow 12:00 am' );
- // user meta
- $user_start = get_user_meta( $user_id, 'mb_start_time', true );
- $user_finish = get_user_meta( $user_id, 'mb_finish_time', true );
- $user_lunch_in = get_user_meta( $user_id, 'mb_lunch_start', true );
- $user_lunch_out = get_user_meta( $user_id, 'mb_lunch_finish', true );
- $user_current_loc = get_user_meta( $user_id, 'mb_user_location_current', true );
- $user_previous_loc = get_user_meta( $user_id, 'mb_user_location_previous', true );
- // set times if not set in user settings
- $user_start = ( empty( $user_start ) ? $default_start : strtotime( $user_start ) );
- $user_finish = ( empty( $user_finish ) ? $default_finish : strtotime( $user_finish ) );
- $user_lunch_in = ( empty( $user_lunch_in ) ? $$default_lunch_in : strtotime( $user_lunch_in ) );
- $user_lunch_out = ( empty( $user_lunch_out ) ? $default_lunch_out : strtotime( $user_lunch_out ) );
- // can we use the task feature
- if( $use_tasks == 'yes' ) {
- // reset the manual / auto
- if( $current_time > $next_midnight ) {
- update_user_meta( $user_id, 'mb_user_automatic_change', '1' );
- delete_user_meta( $user_id, 'mb_user_manual_change' );
- }
- // is the current location able to be auto switched
- // or we have flagged it has run by auto
- if( $mb_location_array[$user_current_loc]['z'] == 'yes' || get_user_meta( $user_id, 'mb_user_automatic_change', true ) == 1 ) {
- // set user to available
- if( !($current_time < $user_start && $current_time > $user_finish) ) {
- update_user_meta( $user_id, 'mb_user_location_previous', $user_current_loc );
- update_user_meta( $user_id, 'mb_user_location_current', 'available' );
- update_user_meta( $user_id, 'mb_user_automatic_change', '1' );
- }
- // set user to unavailable
- if( ($current_time < $user_start && $current_time > $user_finish) || $is_weekend ) {
- update_user_meta( $user_id, 'mb_user_location_previous', $user_current_loc );
- update_user_meta( $user_id, 'mb_user_location_current', 'unavailable' );
- update_user_meta( $user_id, 'mb_user_automatic_change', '1' );
- }
- // set user lunch
- if( $current_time > $user_lunch_in && $current_time < $user_lunch_out ) {
- update_user_meta( $user_id, 'mb_user_location_previous', $user_current_loc );
- update_user_meta( $user_id, 'mb_user_location_current', 'at-lunch' );
- update_user_meta( $user_id, 'mb_user_automatic_change', '1' );
- }
- // if it was auto switched - flagged
- if(
- get_user_meta( $user_id, 'mb_user_automatic_change', true ) == 1 &&
- (
- // isnt in the auto task
- (!($current_time < $user_start && $current_time > $user_finish)) ||
- (($current_time < $user_start || $current_time > $user_finish) || $is_weekend) ||
- ($current_time > $user_lunch_in && $current_time < $user_lunch_out)
- )
- ) {
- delete_user_meta( $user_id, 'mb_user_automatic_change' );
- update_user_meta( $user_id, 'mb_user_location_current', $user_previous_loc );
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment