86400, 'hours' => 3600, 'minutes' => 60, 'seconds' => 1]; public function register(): void { add_filter('zadex/editor/config', [self::class, 'editor_config'], 10, 1); } /** * @param mixed $config * @return mixed */ public static function editor_config($config) { if (is_array($config)) { $tz = self::timezone()->getName(); $config['data'] = is_array($config['data'] ?? null) ? $config['data'] : []; $config['data']['timezone'] = [['value' => $tz, 'label' => $tz]]; } return $config; } public static function timezone(): \DateTimeZone { if (function_exists('wp_timezone')) { $tz = wp_timezone(); if ($tz instanceof \DateTimeZone) { return $tz; } } return new \DateTimeZone('UTC'); } /** `YYYY-MM-DD[T| ]HH:MM[:SS]` in the site timezone -> UTC timestamp (seconds); null when it does not parse. */ public static function due_timestamp(string $value, ?\DateTimeZone $tz = null): ?int { if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/', trim($value), $m)) { return null; } if (!checkdate((int) $m[2], (int) $m[3], (int) $m[1])) { return null; } $date = sprintf('%s-%s-%s %s:%s:%s', $m[1], $m[2], $m[3], $m[4] ?? '00', $m[5] ?? '00', ($m[6] ?? '') !== '' ? $m[6] : '00'); $parsed = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $date, $tz ?? self::timezone()); return $parsed instanceof \DateTimeImmutable ? $parsed->getTimestamp() : null; } /** * Remaining seconds split over the shown units; the largest shown unit absorbs hidden larger ones. * * @param string[] $shown * @return array */ public static function breakdown(int $seconds, array $shown): array { $rest = max(0, $seconds); $out = []; foreach (self::UNITS as $key => $size) { if (!in_array($key, $shown, true)) { continue; } $out[$key] = intdiv($rest, $size); $rest -= $out[$key] * $size; } return $out; } public static function pad(int $n): string { return $n < 10 ? '0' . $n : (string) $n; } }