Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EEH_DTT_Helper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EEH_DTT_Helper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class EEH_DTT_Helper |
||
28 | { |
||
29 | |||
30 | |||
31 | /** |
||
32 | * return the timezone set for the WP install |
||
33 | * |
||
34 | * @return string valid timezone string for PHP DateTimeZone() class |
||
35 | */ |
||
36 | public static function get_timezone() |
||
37 | { |
||
38 | return EEH_DTT_Helper::get_valid_timezone_string(); |
||
39 | } |
||
40 | |||
41 | |||
42 | /** |
||
43 | * get_valid_timezone_string |
||
44 | * ensures that a valid timezone string is returned |
||
45 | * |
||
46 | * @access protected |
||
47 | * @param string $timezone_string |
||
48 | * @return string |
||
49 | * @throws \EE_Error |
||
50 | */ |
||
51 | public static function get_valid_timezone_string($timezone_string = '') |
||
52 | { |
||
53 | // if passed a value, then use that, else get WP option |
||
54 | $timezone_string = ! empty($timezone_string) ? $timezone_string : get_option('timezone_string'); |
||
55 | // value from above exists, use that, else get timezone string from gmt_offset |
||
56 | $timezone_string = ! empty($timezone_string) ? $timezone_string |
||
57 | : EEH_DTT_Helper::get_timezone_string_from_gmt_offset(); |
||
58 | EEH_DTT_Helper::validate_timezone($timezone_string); |
||
59 | return $timezone_string; |
||
60 | } |
||
61 | |||
62 | |||
63 | /** |
||
64 | * This only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
||
65 | * |
||
66 | * @static |
||
67 | * @access public |
||
68 | * @param string $timezone_string Timezone string to check |
||
69 | * @param bool $throw_error |
||
70 | * @return bool |
||
71 | * @throws \EE_Error |
||
72 | */ |
||
73 | public static function validate_timezone($timezone_string, $throw_error = true) |
||
74 | { |
||
75 | // easiest way to test a timezone string is just see if it throws an error when you try to create a DateTimeZone object with it |
||
76 | try { |
||
77 | new DateTimeZone($timezone_string); |
||
78 | } catch (Exception $e) { |
||
79 | // sometimes we take exception to exceptions |
||
80 | if (! $throw_error) { |
||
81 | return false; |
||
82 | } |
||
83 | throw new EE_Error( |
||
84 | sprintf( |
||
85 | __( |
||
86 | 'The timezone given (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
||
87 | 'event_espresso' |
||
88 | ), |
||
89 | $timezone_string, |
||
90 | '<a href="http://www.php.net/manual/en/timezones.php">', |
||
91 | '</a>' |
||
92 | ) |
||
93 | ); |
||
94 | } |
||
95 | return true; |
||
96 | } |
||
97 | |||
98 | |||
99 | /** |
||
100 | * _create_timezone_object_from_timezone_name |
||
101 | * |
||
102 | * @access protected |
||
103 | * @param string $gmt_offset |
||
104 | * @return string |
||
105 | */ |
||
106 | public static function get_timezone_string_from_gmt_offset($gmt_offset = '') |
||
107 | { |
||
108 | $timezone_string = 'UTC'; |
||
109 | //if there is no incoming gmt_offset, then because WP hooks in on timezone_string, we need to see if that is |
||
110 | //set because it will override `gmt_offset` via `pre_get_option` filter. If that's set, then let's just use |
||
111 | //that! Otherwise we'll leave timezone_string at the default of 'UTC' before doing other logic. |
||
112 | if ($gmt_offset === '') { |
||
113 | //autoloaded so no need to set to a variable. There will not be multiple hits to the db. |
||
114 | if (get_option('timezone_string')) { |
||
115 | return get_option('timezone_string'); |
||
116 | } |
||
117 | } |
||
118 | $gmt_offset = $gmt_offset !== '' ? $gmt_offset : get_option('gmt_offset'); |
||
119 | $gmt_offset = (float)$gmt_offset; |
||
120 | //if $gmt_offset is 0, then just return UTC |
||
121 | if ($gmt_offset === (float)0) { |
||
122 | return $timezone_string; |
||
123 | } |
||
124 | if ($gmt_offset !== '') { |
||
|
|||
125 | // convert GMT offset to seconds |
||
126 | $gmt_offset = $gmt_offset * HOUR_IN_SECONDS; |
||
127 | // although we don't know the TZ abbreviation, we know the UTC offset |
||
128 | $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
||
129 | //only use this timezone_string IF it's current offset matches the given offset |
||
130 | try { |
||
131 | $offset = self::get_timezone_offset(new DateTimeZone($timezone_string)); |
||
132 | if ($offset !== $gmt_offset) { |
||
133 | $timezone_string = false; |
||
134 | } |
||
135 | } catch (Exception $e) { |
||
136 | $timezone_string = false; |
||
137 | } |
||
138 | } |
||
139 | // better have a valid timezone string by now, but if not, sigh... loop thru the timezone_abbreviations_list()... |
||
140 | $timezone_string = $timezone_string !== false |
||
141 | ? $timezone_string |
||
142 | : EEH_DTT_Helper::get_timezone_string_from_abbreviations_list($gmt_offset); |
||
143 | return $timezone_string; |
||
144 | } |
||
145 | |||
146 | |||
147 | /** |
||
148 | * Gets the site's GMT offset based on either the timezone string |
||
149 | * (in which case teh gmt offset will vary depending on the location's |
||
150 | * observance of daylight savings time) or the gmt_offset wp option |
||
151 | * |
||
152 | * @return int seconds offset |
||
153 | */ |
||
154 | public static function get_site_timezone_gmt_offset() |
||
155 | { |
||
156 | $timezone_string = get_option('timezone_string'); |
||
157 | if ($timezone_string) { |
||
158 | try { |
||
159 | $timezone = new DateTimeZone($timezone_string); |
||
160 | return $timezone->getOffset(new DateTime()); //in WordPress DateTime defaults to UTC |
||
161 | } catch (Exception $e) { |
||
162 | } |
||
163 | } |
||
164 | $offset = get_option('gmt_offset'); |
||
165 | return (int)($offset * HOUR_IN_SECONDS); |
||
166 | } |
||
167 | |||
168 | |||
169 | /** |
||
170 | * Depending on PHP version, there might not bevalid current timezone strings to match these gmt_offsets in its |
||
171 | * timezone tables. |
||
172 | * To get around that, for these fringe timezones we bump them to a known valid offset. |
||
173 | * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
||
174 | * |
||
175 | * @access public |
||
176 | * @param int $gmt_offset |
||
177 | * @return int |
||
178 | */ |
||
179 | public static function adjust_invalid_gmt_offsets($gmt_offset = 0) |
||
180 | { |
||
181 | //make sure $gmt_offset is int |
||
182 | $gmt_offset = (int)$gmt_offset; |
||
183 | switch ($gmt_offset) { |
||
184 | //-12 |
||
185 | case -43200: |
||
186 | $gmt_offset = -39600; |
||
187 | break; |
||
188 | //-11.5 |
||
189 | case -41400: |
||
190 | $gmt_offset = -39600; |
||
191 | break; |
||
192 | //-10.5 |
||
193 | case -37800: |
||
194 | $gmt_offset = -39600; |
||
195 | break; |
||
196 | //-8.5 |
||
197 | case -30600: |
||
198 | $gmt_offset = -28800; |
||
199 | break; |
||
200 | //-7.5 |
||
201 | case -27000: |
||
202 | $gmt_offset = -25200; |
||
203 | break; |
||
204 | //-6.5 |
||
205 | case -23400: |
||
206 | $gmt_offset = -21600; |
||
207 | break; |
||
208 | //-5.5 |
||
209 | case -19800: |
||
210 | $gmt_offset = -18000; |
||
211 | break; |
||
212 | //-4.5 |
||
213 | case -16200: |
||
214 | $gmt_offset = -14400; |
||
215 | break; |
||
216 | //-3.5 |
||
217 | case -12600: |
||
218 | $gmt_offset = -10800; |
||
219 | break; |
||
220 | //-2.5 |
||
221 | case -9000: |
||
222 | $gmt_offset = -7200; |
||
223 | break; |
||
224 | //-1.5 |
||
225 | case -5400: |
||
226 | $gmt_offset = -3600; |
||
227 | break; |
||
228 | //-0.5 |
||
229 | case -1800: |
||
230 | $gmt_offset = 0; |
||
231 | break; |
||
232 | //.5 |
||
233 | case 1800: |
||
234 | $gmt_offset = 3600; |
||
235 | break; |
||
236 | //1.5 |
||
237 | case 5400: |
||
238 | $gmt_offset = 7200; |
||
239 | break; |
||
240 | //2.5 |
||
241 | case 9000: |
||
242 | $gmt_offset = 10800; |
||
243 | break; |
||
244 | //3.5 |
||
245 | case 12600: |
||
246 | $gmt_offset = 14400; |
||
247 | break; |
||
248 | //7.5 |
||
249 | case 27000: |
||
250 | $gmt_offset = 28800; |
||
251 | break; |
||
252 | //8.5 |
||
253 | case 30600: |
||
254 | $gmt_offset = 31500; |
||
255 | break; |
||
256 | //10.5 |
||
257 | case 37800: |
||
258 | $gmt_offset = 39600; |
||
259 | break; |
||
260 | //11.5 |
||
261 | case 41400: |
||
262 | $gmt_offset = 43200; |
||
263 | break; |
||
264 | //12.75 |
||
265 | case 45900: |
||
266 | $gmt_offset = 46800; |
||
267 | break; |
||
268 | //13.75 |
||
269 | case 49500: |
||
270 | $gmt_offset = 50400; |
||
271 | break; |
||
272 | } |
||
273 | return $gmt_offset; |
||
274 | } |
||
275 | |||
276 | |||
277 | /** |
||
278 | * get_timezone_string_from_abbreviations_list |
||
279 | * |
||
280 | * @access public |
||
281 | * @param int $gmt_offset |
||
282 | * @param bool $coerce If true, we attempt to coerce with our adjustment table @see self::adjust_invalid_gmt_offset. |
||
283 | * @return string |
||
284 | * @throws \EE_Error |
||
285 | */ |
||
286 | public static function get_timezone_string_from_abbreviations_list($gmt_offset = 0, $coerce = true) |
||
287 | { |
||
288 | $abbreviations = timezone_abbreviations_list(); |
||
289 | foreach ($abbreviations as $abbreviation) { |
||
290 | foreach ($abbreviation as $city) { |
||
291 | if ($city['offset'] === $gmt_offset && $city['dst'] === false) { |
||
292 | try { |
||
293 | $offset = self::get_timezone_offset(new DateTimeZone($city['timezone_id'])); |
||
294 | if ($offset !== $gmt_offset) { |
||
295 | continue; |
||
296 | } else { |
||
297 | return $city['timezone_id']; |
||
298 | } |
||
299 | } catch (Exception $e) { |
||
300 | continue; |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | } |
||
305 | //if $coerce is true, let's see if we can get a timezone string after the offset is adjusted |
||
306 | if ($coerce == true) { |
||
307 | $timezone_string = self::get_timezone_string_from_abbreviations_list( |
||
308 | self::adjust_invalid_gmt_offsets($gmt_offset), |
||
309 | false |
||
310 | ); |
||
311 | if ($timezone_string) { |
||
312 | return $timezone_string; |
||
313 | } |
||
314 | } |
||
315 | throw new EE_Error( |
||
316 | sprintf( |
||
317 | __( |
||
318 | 'The provided GMT offset (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
||
319 | 'event_espresso' |
||
320 | ), |
||
321 | $gmt_offset, |
||
322 | '<a href="http://www.php.net/manual/en/timezones.php">', |
||
323 | '</a>' |
||
324 | ) |
||
325 | ); |
||
326 | } |
||
327 | |||
328 | |||
329 | |||
330 | /** |
||
331 | * Get Timezone Transitions |
||
332 | * |
||
333 | * @param \DateTimeZone $date_time_zone |
||
334 | * @param null $time |
||
335 | * @param bool $first_only |
||
336 | * @return array|mixed |
||
337 | */ |
||
338 | public static function get_timezone_transitions(DateTimeZone $date_time_zone, $time = null, $first_only = true) |
||
339 | { |
||
340 | $time = is_int($time) || $time === null ? $time : strtotime($time); |
||
341 | $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
||
342 | $transitions = $date_time_zone->getTransitions($time); |
||
343 | return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
||
344 | } |
||
345 | |||
346 | |||
347 | /** |
||
348 | * Get Timezone Offset for given timezone object. |
||
349 | * |
||
350 | * @param \DateTimeZone $date_time_zone |
||
351 | * @param null $time |
||
352 | * @return mixed |
||
353 | * @throws \DomainException |
||
354 | */ |
||
355 | public static function get_timezone_offset(DateTimeZone $date_time_zone, $time = null) |
||
356 | { |
||
357 | $transitions = self::get_timezone_transitions($date_time_zone, $time); |
||
358 | if (! isset($transitions['offset'])) { |
||
359 | throw new DomainException(); |
||
360 | } |
||
361 | return $transitions['offset']; |
||
362 | } |
||
363 | |||
364 | |||
365 | /** |
||
366 | * @access public |
||
367 | * @param string $timezone_string |
||
368 | */ |
||
369 | public static function timezone_select_input($timezone_string = '') |
||
370 | { |
||
371 | // get WP date time format |
||
372 | $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
||
373 | // if passed a value, then use that, else get WP option |
||
374 | $timezone_string = ! empty($timezone_string) ? $timezone_string : get_option('timezone_string'); |
||
375 | // check if the timezone is valid but don't throw any errors if it isn't |
||
376 | $timezone_string = EEH_DTT_Helper::validate_timezone($timezone_string, false); |
||
377 | $gmt_offset = get_option('gmt_offset'); |
||
378 | $check_zone_info = true; |
||
379 | if (empty($timezone_string)) { |
||
380 | // Create a UTC+- zone if no timezone string exists |
||
381 | $check_zone_info = false; |
||
382 | if ($gmt_offset > 0) { |
||
383 | $timezone_string = 'UTC+' . $gmt_offset; |
||
384 | } elseif ($gmt_offset < 0) { |
||
385 | $timezone_string = 'UTC' . $gmt_offset; |
||
386 | } else { |
||
387 | $timezone_string = 'UTC'; |
||
388 | } |
||
389 | } |
||
390 | ?> |
||
391 | |||
392 | <p> |
||
393 | <label for="timezone_string"><?php _e('timezone'); ?></label> |
||
394 | <select id="timezone_string" name="timezone_string"> |
||
395 | <?php echo wp_timezone_choice($timezone_string); ?> |
||
396 | </select> |
||
397 | <br/> |
||
398 | <span class="description"><?php _e('Choose a city in the same timezone as the event.'); ?></span> |
||
399 | </p> |
||
400 | |||
401 | <p> |
||
402 | <span><?php |
||
403 | printf( |
||
404 | __('%1$sUTC%2$s time is %3$s'), |
||
405 | '<abbr title="Coordinated Universal Time">', |
||
406 | '</abbr>', |
||
407 | '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
||
408 | ); |
||
409 | ?></span> |
||
410 | <?php if (! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
||
411 | <br/><span><?php printf(__('Local time is %1$s'), '<code>' . date_i18n($datetime_format) . '</code>'); ?></span> |
||
412 | <?php endif; ?> |
||
413 | |||
414 | <?php if ($check_zone_info && $timezone_string) : ?> |
||
415 | <br/> |
||
416 | <span> |
||
417 | <?php |
||
418 | // Set TZ so localtime works. |
||
419 | date_default_timezone_set($timezone_string); |
||
420 | $now = localtime(time(), true); |
||
421 | if ($now['tm_isdst']) { |
||
422 | _e('This timezone is currently in daylight saving time.'); |
||
423 | } else { |
||
424 | _e('This timezone is currently in standard time.'); |
||
425 | } |
||
426 | ?> |
||
427 | <br/> |
||
428 | <?php |
||
429 | if (function_exists('timezone_transitions_get')) { |
||
430 | $found = false; |
||
431 | $date_time_zone_selected = new DateTimeZone($timezone_string); |
||
432 | $tz_offset = timezone_offset_get($date_time_zone_selected, date_create()); |
||
433 | $right_now = time(); |
||
434 | $tr['isdst'] = false; |
||
435 | foreach (timezone_transitions_get($date_time_zone_selected) as $tr) { |
||
436 | if ($tr['ts'] > $right_now) { |
||
437 | $found = true; |
||
438 | break; |
||
439 | } |
||
440 | } |
||
441 | if ($found) { |
||
442 | $message = $tr['isdst'] |
||
443 | ? |
||
444 | __(' Daylight saving time begins on: %s.') |
||
445 | : |
||
446 | __(' Standard time begins on: %s.'); |
||
447 | // Add the difference between the current offset and the new offset to ts to get the correct transition time from date_i18n(). |
||
448 | printf( |
||
449 | $message, |
||
450 | '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >' |
||
451 | ); |
||
452 | } else { |
||
453 | _e('This timezone does not observe daylight saving time.'); |
||
454 | } |
||
455 | } |
||
456 | // Set back to UTC. |
||
457 | date_default_timezone_set('UTC'); |
||
458 | ?> |
||
459 | </span></p> |
||
460 | <?php |
||
461 | endif; |
||
462 | } |
||
463 | |||
464 | |||
465 | /** |
||
466 | * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
||
467 | * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
||
468 | * the site is used. |
||
469 | * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
||
470 | * computed timestamp (i.e. date_i18n() ) |
||
471 | * |
||
472 | * @param int $unix_timestamp if 0, then time() will be used. |
||
473 | * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
||
474 | * site will be used. |
||
475 | * @return int $unix_timestamp with the offset applied for the given timezone. |
||
476 | */ |
||
477 | public static function get_timestamp_with_offset($unix_timestamp = 0, $timezone_string = '') |
||
478 | { |
||
479 | $unix_timestamp = $unix_timestamp === 0 ? time() : (int)$unix_timestamp; |
||
480 | $timezone_string = self::get_valid_timezone_string($timezone_string); |
||
481 | $TimeZone = new DateTimeZone($timezone_string); |
||
482 | $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
||
483 | $offset = timezone_offset_get($TimeZone, $DateTime); |
||
484 | return (int)$DateTime->format('U') + (int)$offset; |
||
485 | } |
||
486 | |||
487 | |||
488 | /** |
||
489 | * _set_date_time_field |
||
490 | * modifies EE_Base_Class EE_Datetime_Field objects |
||
491 | * |
||
492 | * @param EE_Base_Class $obj EE_Base_Class object |
||
493 | * @param DateTime $DateTime PHP DateTime object |
||
494 | * @param string $datetime_field_name the datetime fieldname to be manipulated |
||
495 | * @return EE_Base_Class |
||
496 | */ |
||
497 | protected static function _set_date_time_field(EE_Base_Class $obj, DateTime $DateTime, $datetime_field_name) |
||
511 | |||
512 | |||
513 | /** |
||
514 | * date_time_add |
||
515 | * helper for doing simple datetime calculations on a given datetime from EE_Base_Class |
||
516 | * and modifying it IN the EE_Base_Class so you don't have to do anything else. |
||
517 | * |
||
518 | * @param EE_Base_Class $obj EE_Base_Class object |
||
519 | * @param string $datetime_field_name name of the EE_Datetime_Filed datatype db column to be manipulated |
||
520 | * @param string $period what you are adding. The options are (years, months, days, hours, |
||
521 | * minutes, seconds) defaults to years |
||
522 | * @param integer $value what you want to increment the time by |
||
523 | * @return EE_Base_Class return the EE_Base_Class object so right away you can do something with it |
||
524 | * (chaining) |
||
525 | */ |
||
526 | View Code Duplication | public static function date_time_add(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1) |
|
533 | |||
534 | |||
535 | /** |
||
536 | * date_time_subtract |
||
537 | * same as date_time_add except subtracting value instead of adding. |
||
538 | * |
||
539 | * @param \EE_Base_Class $obj |
||
540 | * @param string $datetime_field_name name of the EE_Datetime_Filed datatype db column to be manipulated |
||
541 | * @param string $period |
||
542 | * @param int $value |
||
543 | * @return \EE_Base_Class |
||
544 | */ |
||
545 | View Code Duplication | public static function date_time_subtract(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1) |
|
552 | |||
553 | |||
554 | /** |
||
555 | * Simply takes an incoming DateTime object and does calculations on it based on the incoming parameters |
||
556 | * |
||
557 | * @param DateTime $DateTime DateTime object |
||
558 | * @param string $period a value to indicate what interval is being used in the calculation. The options are |
||
559 | * 'years', 'months', 'days', 'hours', 'minutes', 'seconds'. Defaults to years. |
||
560 | * @param integer $value What you want to increment the date by |
||
561 | * @param string $operand What operand you wish to use for the calculation |
||
562 | * @return \DateTime return whatever type came in. |
||
563 | * @throws \EE_Error |
||
564 | */ |
||
565 | protected static function _modify_datetime_object(DateTime $DateTime, $period = 'years', $value = 1, $operand = '+') |
||
608 | |||
609 | |||
610 | /** |
||
611 | * Simply takes an incoming Unix timestamp and does calculations on it based on the incoming parameters |
||
612 | * |
||
613 | * @param int $timestamp Unix timestamp |
||
614 | * @param string $period a value to indicate what interval is being used in the calculation. The options are |
||
615 | * 'years', 'months', 'days', 'hours', 'minutes', 'seconds'. Defaults to years. |
||
616 | * @param integer $value What you want to increment the date by |
||
617 | * @param string $operand What operand you wish to use for the calculation |
||
618 | * @return \DateTime return whatever type came in. |
||
619 | * @throws \EE_Error |
||
620 | */ |
||
621 | protected static function _modify_timestamp($timestamp, $period = 'years', $value = 1, $operand = '+') |
||
661 | |||
662 | |||
663 | /** |
||
664 | * Simply takes an incoming UTC timestamp or DateTime object and does calculations on it based on the incoming |
||
665 | * parameters and returns the new timestamp or DateTime. |
||
666 | * |
||
667 | * @param int | DateTime $DateTime_or_timestamp DateTime object or Unix timestamp |
||
668 | * @param string $period a value to indicate what interval is being used in the |
||
669 | * calculation. The options are 'years', 'months', 'days', 'hours', |
||
670 | * 'minutes', 'seconds'. Defaults to years. |
||
671 | * @param integer $value What you want to increment the date by |
||
672 | * @param string $operand What operand you wish to use for the calculation |
||
673 | * @return mixed string|DateTime return whatever type came in. |
||
674 | */ |
||
675 | public static function calc_date($DateTime_or_timestamp, $period = 'years', $value = 1, $operand = '+') |
||
686 | |||
687 | |||
688 | /** |
||
689 | * The purpose of this helper method is to receive an incoming format string in php date/time format |
||
690 | * and spit out the js and moment.js equivalent formats. |
||
691 | * Note, if no format string is given, then it is assumed the user wants what is set for WP. |
||
692 | * Note, js date and time formats are those used by the jquery-ui datepicker and the jquery-ui date- |
||
693 | * time picker. |
||
694 | * |
||
695 | * @see http://stackoverflow.com/posts/16725290/ for the code inspiration. |
||
696 | * @param null $date_format_string |
||
697 | * @param null $time_format_string |
||
698 | * @return array |
||
699 | * array( |
||
700 | * 'js' => array ( |
||
701 | * 'date' => //date format |
||
702 | * 'time' => //time format |
||
703 | * ), |
||
704 | * 'moment' => //date and time format. |
||
705 | * ) |
||
706 | */ |
||
707 | public static function convert_php_to_js_and_moment_date_formats( |
||
727 | |||
728 | |||
729 | /** |
||
730 | * This converts incoming format string into js and moment variations. |
||
731 | * |
||
732 | * @param string $format_string incoming php format string |
||
733 | * @return array js and moment formats. |
||
734 | */ |
||
735 | protected static function _php_to_js_moment_converter($format_string) |
||
922 | |||
923 | |||
924 | /** |
||
925 | * This takes an incoming format string and validates it to ensure it will work fine with PHP. |
||
926 | * |
||
927 | * @param string $format_string Incoming format string for php date(). |
||
928 | * @return mixed bool|array If all is okay then TRUE is returned. Otherwise an array of validation |
||
929 | * errors is returned. So for client code calling, check for is_array() to |
||
930 | * indicate failed validations. |
||
931 | */ |
||
932 | public static function validate_format_string($format_string) |
||
954 | |||
955 | |||
956 | /** |
||
957 | * If the the first date starts at midnight on one day, and the next date ends at midnight on the |
||
958 | * very next day then this method will return true. |
||
959 | * If $date_1 = 2015-12-15 00:00:00 and $date_2 = 2015-12-16 00:00:00 then this function will return true. |
||
960 | * If $date_1 = 2015-12-15 03:00:00 and $date_2 = 2015-12_16 03:00:00 then this function will return false. |
||
961 | * If $date_1 = 2015-12-15 00:00:00 and $date_2 = 2015-12-15 00:00:00 then this function will return true. |
||
962 | * |
||
963 | * @param mixed $date_1 |
||
964 | * @param mixed $date_2 |
||
965 | * @return bool |
||
966 | */ |
||
967 | public static function dates_represent_one_24_hour_date($date_1, $date_2) |
||
981 | |||
982 | |||
983 | /** |
||
984 | * This returns the appropriate query interval string that can be used in sql queries involving mysql Date |
||
985 | * Functions. |
||
986 | * |
||
987 | * @param string $timezone_string A timezone string in a valid format to instantiate a DateTimeZone object. |
||
988 | * @param string $field_for_interval The Database field that is the interval is applied to in the query. |
||
989 | * @return string |
||
990 | */ |
||
991 | public static function get_sql_query_interval_for_offset($timezone_string, $field_for_interval) |
||
1010 | |||
1011 | |||
1012 | /** |
||
1013 | * Retrieves the site's default timezone and returns it formatted so it's ready for display |
||
1014 | * to users. If you want to customize how its displayed feel free to fetch the 'timezone_string' |
||
1015 | * and 'gmt_offset' WordPress options directly; or use the filter |
||
1016 | * FHEE__EEH_DTT_Helper__get_timezone_string_for_display |
||
1017 | * (although note that we remove any HTML that may be added) |
||
1018 | * |
||
1019 | * @return string |
||
1020 | */ |
||
1021 | public static function get_timezone_string_for_display() |
||
1065 | |||
1066 | |||
1067 | |||
1068 | /** |
||
1069 | * So PHP does this awesome thing where if you are trying to get a timestamp |
||
1070 | * for a month using a string like "February" or "February 2017", |
||
1071 | * and you don't specify a day as part of your string, |
||
1072 | * then PHP will use whatever the current day of the month is. |
||
1073 | * IF the current day of the month happens to be the 30th or 31st, |
||
1074 | * then PHP gets really confused by a date like February 30, |
||
1075 | * so instead of saying |
||
1076 | * "Hey February only has 28 days (this year)... |
||
1077 | * ...you must have meant the last day of the month!" |
||
1078 | * PHP does the next most logical thing, and bumps the date up to March 2nd, |
||
1079 | * because someone requesting February 30th obviously meant March 1st! |
||
1080 | * The way around this is to always set the day to the first, |
||
1081 | * so that the month will stay on the month you wanted. |
||
1082 | * this method will add that "1" into your date regardless of the format. |
||
1083 | * |
||
1084 | * @param string $month |
||
1085 | * @return string |
||
1086 | */ |
||
1087 | public static function first_of_month_timestamp($month = '') |
||
1102 | |||
1103 | |||
1104 | /** |
||
1105 | * This simply returns the timestamp for tomorrow (midnight next day) in this sites timezone. So it may be midnight |
||
1106 | * for this sites timezone, but the timestamp could be some other time GMT. |
||
1107 | */ |
||
1108 | public static function tomorrow() |
||
1116 | |||
1117 | |||
1118 | /** |
||
1119 | * ** |
||
1120 | * Gives a nicely-formatted list of timezone strings. |
||
1121 | * Copied from the core wp function by the same name so we could customize to remove UTC offsets. |
||
1122 | * |
||
1123 | * @since 4.9.40.rc.008 |
||
1124 | * @staticvar bool $mo_loaded |
||
1125 | * @staticvar string $locale_loaded |
||
1126 | * @param string $selected_zone Selected timezone. |
||
1127 | * @param string $locale Optional. Locale to load the timezones in. Default current site locale. |
||
1128 | * @return string |
||
1129 | */ |
||
1130 | public static function wp_timezone_choice($selected_zone, $locale = null) |
||
1229 | |||
1230 | |||
1231 | /** |
||
1232 | * Shim for the WP function `get_user_locale` that was added in WordPress 4.7.0 |
||
1233 | * |
||
1234 | * @param int|WP_User $user_id |
||
1235 | * @return string |
||
1236 | */ |
||
1237 | public static function get_user_locale($user_id = 0) |
||
1244 | |||
1245 | }// end class EEH_DTT_Helper |
||
1246 |