WebOrbiton
v3.0.0.0

FlatlyPage

161 lines · 5.1 KB
  1. <?php
  2. ​
  3. if (!function_exists('get_publish_timezone_list')) {
  4. function get_publish_timezone_list(): array
  5. {
  6. $priority = [
  7. 'Europe/Warsaw',
  8. 'Europe/London',
  9. 'Europe/Berlin',
  10. 'Europe/Paris',
  11. 'Europe/Madrid',
  12. 'Europe/Rome',
  13. 'Europe/Kiev',
  14. 'Europe/Moscow',
  15. 'UTC',
  16. 'America/New_York',
  17. 'America/Chicago',
  18. 'America/Denver',
  19. 'America/Los_Angeles',
  20. 'Asia/Dubai',
  21. 'Asia/Kolkata',
  22. 'Asia/Shanghai',
  23. 'Asia/Tokyo',
  24. 'Australia/Sydney',
  25. ];
  26. ​
  27. $all = timezone_identifiers_list();
  28. $rest = array_values(array_diff($all, $priority));
  29. sort($rest);
  30. ​
  31. return array_merge($priority, $rest);
  32. }
  33. }
  34. ​
  35. if (!function_exists('build_publish_fields_from_post')) {
  36. function build_publish_fields_from_post(array $post): array
  37. {
  38. $enabled = isset($post['publish_enabled']) && $post['publish_enabled'] === '1';
  39. $date = trim($post['publish_date'] ?? '');
  40. $time = trim($post['publish_time'] ?? '');
  41. $ampm = strtoupper(trim($post['publish_ampm'] ?? ''));
  42. $time_format = ($post['publish_time_format'] ?? '24h') === '12h' ? '12h' : '24h';
  43. $timezone = trim($post['publish_timezone'] ?? 'Europe/Warsaw');
  44. ​
  45. $valid_timezones = timezone_identifiers_list();
  46. if (!in_array($timezone, $valid_timezones, true)) {
  47. $timezone = 'Europe/Warsaw';
  48. }
  49. ​
  50. $result = [
  51. 'publish_enabled' => false,
  52. 'publish_date' => '',
  53. 'publish_time' => '',
  54. 'publish_ampm' => '',
  55. 'publish_time_format' => $time_format,
  56. 'publish_timezone' => $timezone,
  57. 'publish_at_utc' => null,
  58. ];
  59. ​
  60. if (!$enabled || $date === '' || $time === '') {
  61. return $result;
  62. }
  63. ​
  64. if (!preg_match('/^(\d{1,2}):(\d{2})$/', $time, $m)) {
  65. return $result;
  66. }
  67. ​
  68. $hour = (int)$m[1];
  69. $minute = (int)$m[2];
  70. ​
  71. if ($hour < 0 || $hour > 23 || $minute < 0 || $minute > 59) {
  72. return $result;
  73. }
  74. ​
  75. $normalized_time = sprintf('%02d:%02d', $hour, $minute);
  76. ​
  77. try {
  78. $tz = new DateTimeZone($timezone);
  79. $dt = DateTime::createFromFormat('Y-m-d H:i', $date . ' ' . $normalized_time, $tz);
  80. if ($dt === false) {
  81. return $result;
  82. }
  83. $dt->setTimezone(new DateTimeZone('UTC'));
  84. ​
  85. $result['publish_enabled'] = true;
  86. $result['publish_date'] = $date;
  87. $result['publish_time'] = $normalized_time;
  88. $result['publish_ampm'] = $time_format === '12h' ? ($ampm ?: ($hour >= 12 ? 'PM' : 'AM')) : '';
  89. $result['publish_at_utc'] = $dt->format('Y-m-d H:i:s');
  90. } catch (Exception $e) {
  91. return $result;
  92. }
  93. ​
  94. return $result;
  95. }
  96. }
  97. ​
  98. if (!function_exists('is_page_published')) {
  99. function is_page_published(array $page): bool
  100. {
  101. if (empty($page['publish_enabled']) || empty($page['publish_at_utc'])) {
  102. return true;
  103. }
  104. ​
  105. try {
  106. $publish_at = new DateTime($page['publish_at_utc'], new DateTimeZone('UTC'));
  107. $now = new DateTime('now', new DateTimeZone('UTC'));
  108. return $now >= $publish_at;
  109. } catch (Exception $e) {
  110. return true;
  111. }
  112. }
  113. }
  114. ​
  115. if (!function_exists('format_publish_datetime')) {
  116. ​
  117. function format_publish_datetime(array $page, bool $include_timezone = true): ?string
  118. {
  119. if (empty($page['publish_enabled']) || empty($page['publish_at_utc'])) {
  120. return null;
  121. }
  122. ​
  123. try {
  124. $timezone = $page['publish_timezone'] ?? 'Europe/Warsaw';
  125. if (!in_array($timezone, timezone_identifiers_list(), true)) {
  126. $timezone = 'Europe/Warsaw';
  127. }
  128. $dt = new DateTime($page['publish_at_utc'], new DateTimeZone('UTC'));
  129. $dt->setTimezone(new DateTimeZone($timezone));
  130. ​
  131. $time_format = ($page['publish_time_format'] ?? '24h') === '12h' ? 'M j, Y, g:i A' : 'M j, Y, H:i';
  132. $formatted = $dt->format($time_format);
  133. return $include_timezone ? $formatted . ' (' . $timezone . ')' : $formatted;
  134. } catch (Exception $e) {
  135. return null;
  136. }
  137. }
  138. }
  139. ​
  140. if (!function_exists('get_publish_date_only')) {
  141. function get_publish_date_only(array $page): ?string
  142. {
  143. if (empty($page['publish_enabled']) || empty($page['publish_at_utc'])) {
  144. return null;
  145. }
  146. ​
  147. try {
  148. $timezone = $page['publish_timezone'] ?? 'Europe/Warsaw';
  149. if (!in_array($timezone, timezone_identifiers_list(), true)) {
  150. $timezone = 'Europe/Warsaw';
  151. }
  152. $dt = new DateTime($page['publish_at_utc'], new DateTimeZone('UTC'));
  153. $dt->setTimezone(new DateTimeZone($timezone));
  154. ​
  155. return $dt->format('d.m.Y');
  156. } catch (Exception $e) {
  157. return null;
  158. }
  159. }
  160. }
  161. ​