v3.0.0.0
FlatlyPage
- <?php
-
- if (!function_exists('get_publish_timezone_list')) {
- function get_publish_timezone_list(): array
- {
- $priority = [
- 'Europe/Warsaw',
- 'Europe/London',
- 'Europe/Berlin',
- 'Europe/Paris',
- 'Europe/Madrid',
- 'Europe/Rome',
- 'Europe/Kiev',
- 'Europe/Moscow',
- 'UTC',
- 'America/New_York',
- 'America/Chicago',
- 'America/Denver',
- 'America/Los_Angeles',
- 'Asia/Dubai',
- 'Asia/Kolkata',
- 'Asia/Shanghai',
- 'Asia/Tokyo',
- 'Australia/Sydney',
- ];
-
- $all = timezone_identifiers_list();
- $rest = array_values(array_diff($all, $priority));
- sort($rest);
-
- return array_merge($priority, $rest);
- }
- }
-
- if (!function_exists('build_publish_fields_from_post')) {
- function build_publish_fields_from_post(array $post): array
- {
- $enabled = isset($post['publish_enabled']) && $post['publish_enabled'] === '1';
- $date = trim($post['publish_date'] ?? '');
- $time = trim($post['publish_time'] ?? '');
- $ampm = strtoupper(trim($post['publish_ampm'] ?? ''));
- $time_format = ($post['publish_time_format'] ?? '24h') === '12h' ? '12h' : '24h';
- $timezone = trim($post['publish_timezone'] ?? 'Europe/Warsaw');
-
- $valid_timezones = timezone_identifiers_list();
- if (!in_array($timezone, $valid_timezones, true)) {
- $timezone = 'Europe/Warsaw';
- }
-
- $result = [
- 'publish_enabled' => false,
- 'publish_date' => '',
- 'publish_time' => '',
- 'publish_ampm' => '',
- 'publish_time_format' => $time_format,
- 'publish_timezone' => $timezone,
- 'publish_at_utc' => null,
- ];
-
- if (!$enabled || $date === '' || $time === '') {
- return $result;
- }
-
- if (!preg_match('/^(\d{1,2}):(\d{2})$/', $time, $m)) {
- return $result;
- }
-
- $hour = (int)$m[1];
- $minute = (int)$m[2];
-
- if ($hour < 0 || $hour > 23 || $minute < 0 || $minute > 59) {
- return $result;
- }
-
- $normalized_time = sprintf('%02d:%02d', $hour, $minute);
-
- try {
- $tz = new DateTimeZone($timezone);
- $dt = DateTime::createFromFormat('Y-m-d H:i', $date . ' ' . $normalized_time, $tz);
- if ($dt === false) {
- return $result;
- }
- $dt->setTimezone(new DateTimeZone('UTC'));
-
- $result['publish_enabled'] = true;
- $result['publish_date'] = $date;
- $result['publish_time'] = $normalized_time;
- $result['publish_ampm'] = $time_format === '12h' ? ($ampm ?: ($hour >= 12 ? 'PM' : 'AM')) : '';
- $result['publish_at_utc'] = $dt->format('Y-m-d H:i:s');
- } catch (Exception $e) {
- return $result;
- }
-
- return $result;
- }
- }
-
- if (!function_exists('is_page_published')) {
- function is_page_published(array $page): bool
- {
- if (empty($page['publish_enabled']) || empty($page['publish_at_utc'])) {
- return true;
- }
-
- try {
- $publish_at = new DateTime($page['publish_at_utc'], new DateTimeZone('UTC'));
- $now = new DateTime('now', new DateTimeZone('UTC'));
- return $now >= $publish_at;
- } catch (Exception $e) {
- return true;
- }
- }
- }
-
- if (!function_exists('format_publish_datetime')) {
-
- function format_publish_datetime(array $page, bool $include_timezone = true): ?string
- {
- if (empty($page['publish_enabled']) || empty($page['publish_at_utc'])) {
- return null;
- }
-
- try {
- $timezone = $page['publish_timezone'] ?? 'Europe/Warsaw';
- if (!in_array($timezone, timezone_identifiers_list(), true)) {
- $timezone = 'Europe/Warsaw';
- }
- $dt = new DateTime($page['publish_at_utc'], new DateTimeZone('UTC'));
- $dt->setTimezone(new DateTimeZone($timezone));
-
- $time_format = ($page['publish_time_format'] ?? '24h') === '12h' ? 'M j, Y, g:i A' : 'M j, Y, H:i';
- $formatted = $dt->format($time_format);
- return $include_timezone ? $formatted . ' (' . $timezone . ')' : $formatted;
- } catch (Exception $e) {
- return null;
- }
- }
- }
-
- if (!function_exists('get_publish_date_only')) {
- function get_publish_date_only(array $page): ?string
- {
- if (empty($page['publish_enabled']) || empty($page['publish_at_utc'])) {
- return null;
- }
-
- try {
- $timezone = $page['publish_timezone'] ?? 'Europe/Warsaw';
- if (!in_array($timezone, timezone_identifiers_list(), true)) {
- $timezone = 'Europe/Warsaw';
- }
- $dt = new DateTime($page['publish_at_utc'], new DateTimeZone('UTC'));
- $dt->setTimezone(new DateTimeZone($timezone));
-
- return $dt->format('d.m.Y');
- } catch (Exception $e) {
- return null;
- }
- }
- }
-