| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves the WordPress site's configured timezone (Settings > General). |
| 9 |
* |
| 10 |
* wp_timezone() (WP 5.3+) already implements this; the fallback below |
| 11 |
* replicates wp_timezone_string()'s logic (timezone_string option, else a |
| 12 |
* fixed offset from gmt_offset) for older installs, since this plugin's |
| 13 |
* readme declares WP 5.0 compatibility. |
| 14 |
* |
| 15 |
* Shared by every caller that needs to interpret or produce a WP-site-local |
| 16 |
* calendar date/time as a true UTC epoch: redirect scheduling |
| 17 |
* (RedirectScheduleTimezone) and feedback log-timestamp parsing |
| 18 |
* (FeedbackEnvironmentExtras_DebugLogSignatures). Deliberately NOT used by |
| 19 |
* LogTimestampFormatter, which needs a self-contained, never-throws, |
| 20 |
* never-logs implementation to avoid recursing back into the logger it is |
| 21 |
* formatting a timestamp for (see that class's docblock). |
| 22 |
*/ |
| 23 |
final class ABJ_404_Solution_SiteTimezone { |
| 24 |
|
| 25 |
/** |
| 26 |
* @return DateTimeZone |
| 27 |
*/ |
| 28 |
public static function resolve(): DateTimeZone { |
| 29 |
if (function_exists('wp_timezone')) { |
| 30 |
return wp_timezone(); |
| 31 |
} |
| 32 |
|
| 33 |
$timezoneStringRaw = function_exists('get_option') ? get_option('timezone_string') : ''; |
| 34 |
$timezoneString = is_string($timezoneStringRaw) ? $timezoneStringRaw : ''; |
| 35 |
$offsetRaw = function_exists('get_option') ? get_option('gmt_offset') : 0.0; |
| 36 |
$offset = is_scalar($offsetRaw) ? (float)$offsetRaw : 0.0; |
| 37 |
return self::resolveFallback($timezoneString, $offset); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Pure computation half of the WP<5.3 fallback, split out so it is |
| 42 |
* unit-testable without depending on wp_timezone()'s function_exists() |
| 43 |
* state -- once a test stubs wp_timezone() via Brain\Monkey, Patchwork |
| 44 |
* keeps it defined for the rest of that PHP process/worker (see |
| 45 |
* .claude/rules/testing.md R6), so function_exists('wp_timezone') |
| 46 |
* cannot be relied on to be false later in the same run. |
| 47 |
* |
| 48 |
* @param string $timezoneString get_option('timezone_string') value |
| 49 |
* @param float $gmtOffset get_option('gmt_offset') value |
| 50 |
* @return DateTimeZone |
| 51 |
*/ |
| 52 |
public static function resolveFallback(string $timezoneString, float $gmtOffset): DateTimeZone { |
| 53 |
if ($timezoneString !== '') { |
| 54 |
try { |
| 55 |
return new DateTimeZone($timezoneString); |
| 56 |
} catch (Exception $e) { |
| 57 |
if (function_exists('abj_service_optional')) { |
| 58 |
$logging = abj_service_optional('logging'); |
| 59 |
if ($logging !== null) { |
| 60 |
$logging->warn('SiteTimezone: invalid timezone_string option "' . |
| 61 |
$timezoneString . '", falling back to gmt_offset: ' . $e->getMessage()); |
| 62 |
} |
| 63 |
} |
| 64 |
// Falls through to the offset-based fallback below. |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$hours = (int)$gmtOffset; |
| 69 |
$minutes = abs(($gmtOffset - (int)$gmtOffset) * 60); |
| 70 |
return new DateTimeZone(sprintf('%+03d:%02d', $hours, $minutes)); |
| 71 |
} |
| 72 |
} |
| 73 |
|