forked from gravityflow/gravityflowlibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule-step-business-hours.php
89 lines (68 loc) · 3.19 KB
/
schedule-step-business-hours.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
Plugin Name: Schedule Step By Business Hours
Description: Adjust step schedule to start during business hours
Version: 1.0
Author: Steven Henty
Author URI: http://www.idealienstudios.com
License: GPL-3.0+
Copyright 2018 Steven Henty
*/
/*
*** Instructions ***
- Set step schedule with a default delay (5 minutes)
- Update the check of Step ID to match your use case
- Update the start_hour and end_hour
*** Note ***
This functionality does not take into consideration minutes, only the hours and day such that:
- If the workflow step was requested at 11:43PM on a Tuesday it would be scheduled to send out at 9:43AM Wednesday.
- If the workflow step was requested at 6:12AM on a Saturday It would be scheduled to send out at 9:12AM Monday.
*/
add_filter( 'gravityflow_step_schedule_timestamp', 'schedule_business_hours', 10, 3 );
function schedule_business_hours( $schedule_timestamp, $schedule_type, $step ) {
//Ensure you are only adjusting the desired form/step
if ( $step->get_id() !== 76 ) {
return $schedule_timestamp;
}
gravity_flow()->log_debug( __METHOD__ . '(): Default Schedule: ' . date( 'Y-m-d H:i:s', $schedule_timestamp ) );
//Define for your business irrespective of time-zone
$start_hour = 9;
$end_hour = 17;
$current_timestamp = time();
$tz = get_option('timezone_string');
$dt = new DateTime("now", new DateTimeZone($tz));
$dt->setTimestamp($current_timestamp);
$current_hour = $dt->format('H');
$current_day_of_week = $dt->format('N');
//Modify the value of $current_timestamp if you want to test the step arriving separate from actual time.
gravity_flow()->log_debug( __METHOD__ . '(): Comparing '. $current_hour . ' against ' . $start_hour . ' - ' . $end_hour);
//Weekday + Business Hours
if ( in_array( $current_day_of_week, array( 1, 2, 3, 4, 5 ) ) && $current_hour >= $start_hour && $current_hour <= $end_hour ) {
gravity_flow()->log_debug( __METHOD__ . '(): Business Hour Request - Proceeding with default schedule');
return $schedule_timestamp;
}
//Weekday + Before Business Hours
if( in_array( $current_day_of_week, array( 1, 2, 3, 4, 5 ) ) && $current_hour <= $start_hour ) {
$delay_hours = $start_hour - $current_hour;
//Mon to Thurs + After Business Hours
} else if( in_array( $current_day_of_week, array( 1, 2, 3, 4 ) ) && $current_hour >= $end_hour ) {
$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour;
//Friday + After Business Hours
} else if( in_array( $current_day_of_week, array( 5 ) ) && $current_hour >= $end_hour ) {
$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour + 48;
//Saturday
} else if( in_array( $current_day_of_week, array( 6 ) ) ) {
$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour + 24;
//Sunday
} else if( in_array( $current_day_of_week, array( 7 ) ) ) {
$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour;
//Edge case handling
} else {
$delay_hours = 0;
}
$delayed_timestamp = $schedule_timestamp + ( $delay_hours * 60 * 60 );
$dt->setTimestamp($delayed_timestamp);
gravity_flow()->log_debug( __METHOD__ . '(): Outside Business Hour Request');
gravity_flow()->log_debug( __METHOD__ . '(): Delaying by ' . $delay_hours . ' hour(s) to ' . $dt->format('Y-m-d h:m:s') );
return $delayed_timestamp;
}