-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper-functions.php
251 lines (193 loc) · 6.28 KB
/
helper-functions.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* Generate array of times with key in `H:i` format and after-midnight in `+H:i` format
*
* @since 2.0
*
* @param boolean $with_after_midnight Include times for next day
*
* @return array Array of times, with keys as `H:i`-formatted time, and values as `g:i a` formatted time
*/
function gf_business_hours_get_times( $with_after_midnight = false ) {
$key_format = 'H:i';
/**
* Modify the time format for the displayed value
* @param string
*/
$value_format = apply_filters( 'gravityforms_business_hours_time_format', get_option( 'time_format' ) );
$starttime = '00:00';
$time = new DateTime( $starttime );
/**
* Time interval for the time dropdown options
* @var int
*/
$interval_minutes = apply_filters( 'gravityforms_business_hours_interval', 30 );
$interval_minutes = intval( $interval_minutes );
$interval = new DateInterval('PT'.$interval_minutes.'M');
$temptime = '';
$times = array();
do {
$key = $time->format( $key_format );
// 12:30 am
$value = $time->format( $value_format );
$times[ $key ] = $value;
// Increase by 30 minute intervals
$time->add($interval);
$temptime = $time->format( $key_format );
} while( $temptime !== $starttime );
// Build additional times for the next day closing times
if( $with_after_midnight ) {
$next_day = __('next day', 'gravity-forms-business-hours');
foreach( $times as $key => $time ) {
$times[ '+'.$key ] = sprintf( '%s (%s)', $time, $next_day );
// Only show "Next day" times until 7am
if( $key === '07:00' ) {
break;
}
}
}
return $times;
}
/**
* Sort the list by the day and times entered
*
* @since 2.0
*
* @param array $a Item 1 to be compared
* @param array $b Item 2 to be compared
*
* @return int 0 no change; -1 move down; +1 move up
*/
function gf_business_hours_sort_days( $a, $b ) {
// Generate a timestamp for the different options
$a_time = gf_business_hours_get_timestamp_from_time_span( $a );
$b_time = gf_business_hours_get_timestamp_from_time_span( $b );
// If same time, don't up/down sort
if( $a_time === $b_time ) {
return 0;
}
// If A > B, move down the list (+1). Otherwise, move up (-1).
return ( $a_time > $b_time ) ? +1 : -1;
}
/**
* Convert a timespan item into a timestamp for the blog's timezone
*
* @since 2.0
*
* @param array $time_span Timespan array with at least day, fromtime keys
*
* @return float Timestamp in float format, since that's what WP's `current_time()` returns
*/
function gf_business_hours_get_timestamp_from_time_span( $time_span, $from_or_to = 'from' ) {
// Only allow from or to
if( $from_or_to !== 'from' ) {
$from_or_to = 'to';
}
// `fromtime` or `totime`
$time_value = $time_span[$from_or_to.'time'];
// Full weekday in English
$day_value = $time_span['day'];
// After midnight!
// We add a day to the strtotime value
// And strip the + from the time to use the standard `H:i` value
if( substr( $time_value, 0, 1 ) === '+' ) {
$day_value .= ' +1 day';
$time_value = str_replace('+', '', $time_value);
}
// strtotime sentence
$str_to_time_string = $day_value .' this week '.$time_value;
// Blog timestamp
$current_time = current_time( 'timestamp' );
$timestamp = strtotime($str_to_time_string , $current_time );
return floatval( $timestamp );
}
/**
* Get an array of days
*
* @since 2.0
*
* @filter gravityforms_business_hours_days Modify the days array
*
* @return array Array of days of the week (displayed using PHP "D" formatting)
*/
function gf_business_hours_get_days() {
/**
* Modify the date format for how the days appear, in PHP Date formatting
* @param string $date PHP Date format
*/
$day_format = apply_filters( 'gravityforms_business_hours_day_format', 'D' );
$days = array(
'Monday' => date_i18n($day_format, strtotime('Monday this week')),
'Tuesday' => date_i18n($day_format, strtotime('Tuesday this week')),
'Wednesday' => date_i18n($day_format, strtotime('Wednesday this week')),
'Thursday' => date_i18n($day_format, strtotime('Thursday this week')),
'Friday' => date_i18n($day_format, strtotime('Friday this week')),
'Saturday' => date_i18n($day_format, strtotime('Saturday this week')),
'Sunday' => date_i18n($day_format, strtotime('Sunday this week')),
);
/**
* Modify the day values. Don't change the keys!
* @var array
*/
$days = apply_filters( 'gravityforms_business_hours_days', $days );
return $days;
}
/**
* Is the business open now for the passed time span?
*
* @since 2.0
*
* @param array $time_span Time span with `day` `fromtime` and `totime`
*
* @return boolean True: open; False: not open
*/
function gf_business_hours_is_open_now( $time_span ) {
// Blog timestamp
$current_time = current_time( 'timestamp' );
$from_time = gf_business_hours_get_timestamp_from_time_span( $time_span, 'from' );
$to_time = gf_business_hours_get_timestamp_from_time_span( $time_span, 'to' );
if( $current_time < $from_time ) {
return false;
}
if( $current_time > $to_time ) {
return false;
}
return true;
}
/**
* Generate the HTML for the times select
*
* @param string $class Class for the select input
* @param string $default Default value
* @param boolean $with_after_midnight Include the hours after midnight of the next day?
*
* @return string HTML <select> of time options
*/
function gf_business_hours_get_times_select( $class = 'item_fromtime', $default = '', $with_after_midnight = false ) {
$output_times = gf_business_hours_get_times( $with_after_midnight );
$output = '<select class="'.sanitize_html_class( $class ).'">';
foreach( $output_times as $value => $label ) {
$selected = selected( $default, $value, false );
$output .= '<option value="' . esc_attr( $value ) .'"'.$selected.'>' . $label . '</option>';
}
$output .= '</select>';
return $output;
}
/**
* Convert the field value into an array of days
* @param string $value Value of the field
* @return array|NULL NULL if not valid or empty; array if exists and is JSON
*/
function gf_business_hours_get_list_from_value( $value ) {
$list = json_decode( html_entity_decode( $value ), true );
// Sometimes it's double-encoded
if( is_string( $list ) ) {
$list = json_decode( $list, true );
}
if( empty( $list ) ) {
return NULL;
}
// Sort the days of the week
usort( $list, 'gf_business_hours_sort_days' );
return $list;
}