| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
/** |
| 5 |
* Helper to retrieve the timezone string for a site until |
| 6 |
* a WP core method exists. |
| 7 |
* |
| 8 |
* @return false|mixed|string|void |
| 9 |
* @since 3.1.7 |
| 10 |
* @see https://github.com/woocommerce/woocommerce/blob/7a7b5137716623a7b8d13658d0a1a71228db9b0a/plugins/woocommerce/includes/wc-formatting-functions.php#L761 |
| 11 |
*/ |
| 12 |
function wpf_timezone_string() { |
| 13 |
// Added in WordPress 5.3 Ref https://developer.wordpress.org/reference/functions/wp_timezone_string/. |
| 14 |
if ( function_exists( 'wp_timezone_string' ) ) { |
| 15 |
return wp_timezone_string(); |
| 16 |
} |
| 17 |
|
| 18 |
// If site timezone string exists, return it. |
| 19 |
$timezone = get_option( 'timezone_string' ); |
| 20 |
if ( $timezone ) { |
| 21 |
return $timezone; |
| 22 |
} |
| 23 |
|
| 24 |
// Get UTC offset, if it isn't set then return UTC. |
| 25 |
$utc_offset = floatval( get_option( 'gmt_offset', 0 ) ); |
| 26 |
if ( ! is_numeric( $utc_offset ) || 0.0 === $utc_offset ) { |
| 27 |
return 'UTC'; |
| 28 |
} |
| 29 |
|
| 30 |
// Adjust UTC offset from hours to seconds. |
| 31 |
$utc_offset = (int) ( $utc_offset * 3600 ); |
| 32 |
|
| 33 |
// Attempt to guess the timezone string from the UTC offset. |
| 34 |
$timezone = timezone_name_from_abbr( '', $utc_offset ); |
| 35 |
if ( $timezone ) { |
| 36 |
return $timezone; |
| 37 |
} |
| 38 |
|
| 39 |
// Last try, guess timezone string manually. |
| 40 |
foreach ( timezone_abbreviations_list() as $abbr ) { |
| 41 |
foreach ( $abbr as $city ) { |
| 42 |
// WordPress restrict the use of date(), since it's affected by timezone settings, but in this case is just what we need to guess the correct timezone. |
| 43 |
if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) { // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 44 |
return $city['timezone_id']; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// Fallback to UTC. |
| 50 |
return 'UTC'; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Get timezone offset in seconds. |
| 56 |
* |
| 57 |
* @return float|int |
| 58 |
* @throws Exception |
| 59 |
* @see https://github.com/woocommerce/woocommerce/blob/7a7b5137716623a7b8d13658d0a1a71228db9b0a/plugins/woocommerce/includes/wc-formatting-functions.php#L808 |
| 60 |
*/ |
| 61 |
function wpf_timezone_offset() { |
| 62 |
$timezone = get_option( 'timezone_string' ); |
| 63 |
|
| 64 |
if ( $timezone ) { |
| 65 |
$timezone_object = new DateTimeZone( $timezone ); |
| 66 |
return $timezone_object->getOffset( new DateTime( 'now' ) ); |
| 67 |
} else { |
| 68 |
return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS; |
| 69 |
} |
| 70 |
} |
| 71 |
|