PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / abstracts / ActionScheduler_TimezoneHelper.php

ActionScheduler_TimezoneHelper.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.4, at vendor/woocommerce/action-scheduler/classes/abstracts/ActionScheduler_TimezoneHelper.php

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