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