| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class ActionScheduler_TimezoneHelper |
| 5 |
*/ |
| 6 |
abstract class ActionScheduler_TimezoneHelper { |
| 7 |
private static $local_timezone = NULL; |
| 8 |
|
| 9 |
/** |
| 10 |
* Set a DateTime's timezone to the WordPress site's timezone, or a UTC offset |
| 11 |
* if no timezone string is available. |
| 12 |
* |
| 13 |
* @since 2.1.0 |
| 14 |
* |
| 15 |
* @param DateTime $date |
| 16 |
* @return ActionScheduler_DateTime |
| 17 |
*/ |
| 18 |
public static function set_local_timezone( DateTime $date ) { |
| 19 |
|
| 20 |
// Accept a DateTime for easier backward compatibility, even though we require methods on ActionScheduler_DateTime |
| 21 |
if ( ! is_a( $date, 'ActionScheduler_DateTime' ) ) { |
| 22 |
$date = as_get_datetime_object( $date->format( 'U' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
if ( get_option( 'timezone_string' ) ) { |
| 26 |
$date->setTimezone( new DateTimeZone( self::get_local_timezone_string() ) ); |
| 27 |
} else { |
| 28 |
$date->setUtcOffset( self::get_local_timezone_offset() ); |
| 29 |
} |
| 30 |
|
| 31 |
return $date; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Helper to retrieve the timezone string for a site until a WP core method exists |
| 36 |
* (see https://core.trac.wordpress.org/ticket/24730). |
| 37 |
* |
| 38 |
* Adapted from wc_timezone_string() and https://secure.php.net/manual/en/function.timezone-name-from-abbr.php#89155. |
| 39 |
* |
| 40 |
* If no timezone string is set, and its not possible to match the UTC offset set for the site to a timezone |
| 41 |
* string, then an empty string will be returned, and the UTC offset should be used to set a DateTime's |
| 42 |
* timezone. |
| 43 |
* |
| 44 |
* @since 2.1.0 |
| 45 |
* @return string PHP timezone string for the site or empty if no timezone string is available. |
| 46 |
*/ |
| 47 |
protected static function get_local_timezone_string( $reset = false ) { |
| 48 |
// If site timezone string exists, return it. |
| 49 |
$timezone = get_option( 'timezone_string' ); |
| 50 |
if ( $timezone ) { |
| 51 |
return $timezone; |
| 52 |
} |
| 53 |
|
| 54 |
// Get UTC offset, if it isn't set then return UTC. |
| 55 |
$utc_offset = intval( get_option( 'gmt_offset', 0 ) ); |
| 56 |
if ( 0 === $utc_offset ) { |
| 57 |
return 'UTC'; |
| 58 |
} |
| 59 |
|
| 60 |
// Adjust UTC offset from hours to seconds. |
| 61 |
$utc_offset *= 3600; |
| 62 |
|
| 63 |
// Attempt to guess the timezone string from the UTC offset. |
| 64 |
$timezone = timezone_name_from_abbr( '', $utc_offset ); |
| 65 |
if ( $timezone ) { |
| 66 |
return $timezone; |
| 67 |
} |
| 68 |
|
| 69 |
// Last try, guess timezone string manually. |
| 70 |
foreach ( timezone_abbreviations_list() as $abbr ) { |
| 71 |
foreach ( $abbr as $city ) { |
| 72 |
if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) { |
| 73 |
return $city['timezone_id']; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// No timezone string |
| 79 |
return ''; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get timezone offset in seconds. |
| 84 |
* |
| 85 |
* @since 2.1.0 |
| 86 |
* @return float |
| 87 |
*/ |
| 88 |
protected static function get_local_timezone_offset() { |
| 89 |
$timezone = get_option( 'timezone_string' ); |
| 90 |
|
| 91 |
if ( $timezone ) { |
| 92 |
$timezone_object = new DateTimeZone( $timezone ); |
| 93 |
return $timezone_object->getOffset( new DateTime( 'now' ) ); |
| 94 |
} else { |
| 95 |
return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @deprecated 2.1.0 |
| 101 |
*/ |
| 102 |
public static function get_local_timezone( $reset = FALSE ) { |
| 103 |
_deprecated_function( __FUNCTION__, '2.1.0', 'ActionScheduler_TimezoneHelper::set_local_timezone()' ); |
| 104 |
if ( $reset ) { |
| 105 |
self::$local_timezone = NULL; |
| 106 |
} |
| 107 |
if ( !isset(self::$local_timezone) ) { |
| 108 |
$tzstring = get_option('timezone_string'); |
| 109 |
|
| 110 |
if ( empty($tzstring) ) { |
| 111 |
$gmt_offset = get_option('gmt_offset'); |
| 112 |
if ( $gmt_offset == 0 ) { |
| 113 |
$tzstring = 'UTC'; |
| 114 |
} else { |
| 115 |
$gmt_offset *= HOUR_IN_SECONDS; |
| 116 |
$tzstring = timezone_name_from_abbr( '', $gmt_offset, 1 ); |
| 117 |
|
| 118 |
// If there's no timezone string, try again with no DST. |
| 119 |
if ( false === $tzstring ) { |
| 120 |
$tzstring = timezone_name_from_abbr( '', $gmt_offset, 0 ); |
| 121 |
} |
| 122 |
|
| 123 |
// Try mapping to the first abbreviation we can find. |
| 124 |
if ( false === $tzstring ) { |
| 125 |
$is_dst = date( 'I' ); |
| 126 |
foreach ( timezone_abbreviations_list() as $abbr ) { |
| 127 |
foreach ( $abbr as $city ) { |
| 128 |
if ( $city['dst'] == $is_dst && $city['offset'] == $gmt_offset ) { |
| 129 |
// If there's no valid timezone ID, keep looking. |
| 130 |
if ( null === $city['timezone_id'] ) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
$tzstring = $city['timezone_id']; |
| 135 |
break 2; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// If we still have no valid string, then fall back to UTC. |
| 142 |
if ( false === $tzstring ) { |
| 143 |
$tzstring = 'UTC'; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
self::$local_timezone = new DateTimeZone($tzstring); |
| 149 |
} |
| 150 |
return self::$local_timezone; |
| 151 |
} |
| 152 |
} |
| 153 |
|