Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active September 16, 2017 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/5bbed2489795b806b1048827a5504599 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/5bbed2489795b806b1048827a5504599 to your computer and use it in GitHub Desktop.
[Appointments +] Set the weekly calendar time slots based on worker start hours for each day
<?php
/*
Plugin Name: [Appointments] - Workers weekly hours
Plugin URI: https://premium.wpmudev.org/
Description: Sets the weekly calendar time slots based on worker start hours for each day
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
add_filter( 'appointments_get_weekly_schedule_slots', function( $slots, $now, $service_id, $worker_id, $location_id ){
$appointments = appointments();
if ( ! $now ) {
$now = current_time( 'timestamp' );
}
// Get the start/end hours
$hour_start = 8;
$hour_end = 18;
if ( $min_max = $appointments->min_max_wh() ) {
$hour_start = $min_max["min"];
$hour_end = $min_max["max"];
}
if ( $hour_start > $hour_end ) {
$hour_start = $min_max["max"];
$hour_end = $min_max["min"];
}
$hour_start = apply_filters( 'app_schedule_starting_hour', $hour_start, $now, 'week' );
$hour_end = apply_filters( 'app_schedule_ending_hour', $hour_end, $now, 'week' );
$step = $appointments->get_min_time() * 60; // Timestamp increase interval to one cell below
if ( ! appointments_use_legacy_duration_calculus() ) {
$service = appointments_get_service( $service_id );
if ( $service ) {
$step = $service->duration * 60;
}
}
// Allow direct step increment manipulation,
// mainly for service duration based calculus start/stop times
$step = apply_filters( 'app-timetable-step_increment', $step, 'week' );
// Get the last day that was a start of week. We'll start from there
$week_start = appointments_week_start();
$week_start_string = appointments_number_to_weekday( $week_start );
$now_weekday = absint( date( 'N', $now ) );
// By default we'll start the schedule by today
$day_start = date( 'Y-m-d', $now );
if ( $week_start != $now_weekday ) {
// Today is not the same day that the week start
// Get the latest start weekday
$day_start = date( 'Y-m-d', strtotime( 'Last ' . $week_start_string, $now ) );
}
// Timestamps to start and end each day
$day_start_timestamp = strtotime( $day_start . ' ' . zeroise( $hour_start, 2 ) . ':00:00' );
$day_end_timestamp = strtotime( $day_start . ' ' . zeroise( $hour_end, 2 ) . ':00:00' );
$the_week = array();
for ( $day = $day_start_timestamp; $day < ( $day_start_timestamp + ( 24 * 3600 * 7 ) ); $day = $day + ( 24 * 3600 ) ) {
// Increase one day in every loop until we completed 7 days
$the_week[] = date( 'Y-m-d', $day );
}
$time_slots = $start_hours = array();
if( $worker_id && appointments_is_worker( $worker_id ) ){
$start_hours = wpmudev_appointments_get_worker_weekly_start_hours( $service_id, $worker_id, $location_id );
}else{
if( $service_id ){
$workers = appointments_get_workers_by_service( $service_id );
}
else{
$workers = appointments_get_all_workers();
}
foreach ( $workers as $worker ) {
$start_hours = array_merge( $time_slots , wpmudev_appointments_get_worker_weekly_start_hours( $service_id, $worker_id, $location_id ) );
}
}
if( ! empty( $start_hours ) ){
sort( $start_hours );
foreach ( $start_hours as $start_time ) {
$start_dt = strtotime( $start_time );
$end_time = date("H:i", strtotime('+' . $step . ' seconds', $start_dt));
if( apply_filters( 'appointments_get_weekly_schedule_slots/skip_after_midnight', true ) ){
if( $end_time < $start_time ){
continue;
}
}
$time_slots[] = array(
'from' => $start_time,
'to' => $end_time
);
}
}
return array(
'the_week' => $the_week,
'time_slots' => $time_slots
);
}, 20, 5 );
function wpmudev_appointments_get_worker_weekly_start_hours( $service_id = 0, $worker_id = 0, $location_id = 0 ){
if( ! $worker_id || ! appointments_is_worker( $worker_id ) ){
return array();
}
$appointments = appointments();
$step = $duration = $appointments->get_min_time() * 60;
$worker = appointments_get_worker( $worker_id );
$service = appointments_get_service( $service_id );
if ( $service ) {
$duration = $service->duration * 60;
}
$worker_working_hours = appointments_get_worker_working_hours( 'open', $worker_id, $location_id );
if( ! empty( $worker_working_hours ) && isset( $worker_working_hours->hours ) && ! empty( $worker_working_hours->hours ) ){
$slot_starts = array();
//The starting hours set in Working Hours settings page
foreach( $worker_working_hours->hours as $dayname => $open_hours ) {
if( $open_hours['active'] != 'yes' ){
continue;
}
//We make a loop using the $step set in General > Time base settings page
for ( $start_time = $open_hours['start']; $start_time < $open_hours['end']; $start_time = date("H:i", strtotime('+' . $step . ' seconds', strtotime( $start_time ) )) ) {
$end_slot = date("H:i", strtotime('+' . $duration . ' seconds', strtotime( $start_time ) ) );
if( $end_slot > $open_hours['end'] ){
break;
}
if( ! in_array( $start_time, $slot_starts ) ){
$slot_starts[] = $start_time;
}
}
}
return $slot_starts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment