activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
4 months ago
auth_helper.php
3 months ago
blocks_helper.php
3 months ago
booking_helper.php
3 months ago
bricks_helper.php
3 months ago
bundles_helper.php
3 months ago
calendar_helper.php
3 months ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
3 months ago
customer_import_helper.php
3 months ago
database_helper.php
3 months ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 months ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 months ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
3 months ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
3 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
replacer_helper.php
3 months ago
resource_helper.php
3 months ago
roles_helper.php
3 months ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
3 months ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 months ago
sms_helper.php
3 months ago
steps_helper.php
3 months ago
stripe_connect_helper.php
3 months ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
3 months ago
timeline_helper.php
3 months ago
transaction_helper.php
3 months ago
transaction_intent_helper.php
3 months ago
util_helper.php
3 months ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
3 months ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
time_helper.php
590 lines
| 1 | <?php |
| 2 | |
| 3 | class OsTimeHelper { |
| 4 | |
| 5 | private static $timezone = false; |
| 6 | |
| 7 | |
| 8 | public static function get_db_weekday_by_number( $number ) { |
| 9 | $weekdays = [ 'mo', 'tu', 'we', 'th', 'fr', 'sa', 'su' ]; |
| 10 | |
| 11 | return $weekdays[ $number - 1 ]; |
| 12 | } |
| 13 | |
| 14 | public static function is_valid_date( $date_string ) { |
| 15 | if ( empty( $date_string ) ) { |
| 16 | return false; |
| 17 | } |
| 18 | return (bool) strtotime( $date_string ); |
| 19 | } |
| 20 | |
| 21 | public static function reformat_date_string( $date_string, $from_format, $to_format ) { |
| 22 | $start_date_obj = OsWpDateTime::os_createFromFormat( $from_format, $date_string ); |
| 23 | |
| 24 | return $start_date_obj->format( $to_format ); |
| 25 | } |
| 26 | |
| 27 | public static function nice_date( $date ) { |
| 28 | if ( $date == OsTimeHelper::today_date( 'Y-m-d' ) ) { |
| 29 | $nice_date = __( 'Today', 'latepoint' ); |
| 30 | } else { |
| 31 | $nice_date = self::get_nice_date_with_optional_year( $date, true ); |
| 32 | } |
| 33 | |
| 34 | return $nice_date; |
| 35 | } |
| 36 | |
| 37 | public static function date_from_db( $date_string, $format = false, string $output_timezone_name = 'UTC' ) { |
| 38 | $timezone = new DateTimeZone( 'UTC' ); |
| 39 | $date_obj = OsWpDateTime::os_createFromFormat( LATEPOINT_DATETIME_DB_FORMAT, $date_string, $timezone ); |
| 40 | if ( empty( $date_obj ) ) { |
| 41 | $date_obj = self::now_datetime_object(); |
| 42 | } |
| 43 | $date_obj->setTimezone( new DateTimeZone( $output_timezone_name ) ); |
| 44 | if ( $format ) { |
| 45 | return $date_obj->format( $format ); |
| 46 | } |
| 47 | |
| 48 | return $date_obj; |
| 49 | } |
| 50 | |
| 51 | public static function today_date( $date_format = 'Y-m-d' ) { |
| 52 | $today = new OsWpDateTime( 'today' ); |
| 53 | |
| 54 | return $today->format( $date_format ); |
| 55 | } |
| 56 | |
| 57 | public static function now_datetime_in_format( $date_format = LATEPOINT_DATETIME_DB_FORMAT, string $timezone = 'UTC' ) { |
| 58 | $now = self::now_datetime_object(); |
| 59 | $now->setTimezone( new DateTimeZone( 'UTC' ) ); |
| 60 | |
| 61 | return $now->format( $date_format ); |
| 62 | } |
| 63 | |
| 64 | public static function now_datetime_utc() { |
| 65 | $now = self::now_datetime_object(); |
| 66 | $now->setTimezone( new DateTimeZone( 'UTC' ) ); |
| 67 | |
| 68 | return $now; |
| 69 | } |
| 70 | |
| 71 | public static function time_left_to_datetime( string $datetime, DateTimeZone $timezone ) { |
| 72 | |
| 73 | $now_datetime = new OsWpDateTime( 'now', $timezone ); |
| 74 | $event_datetime = OsWpDateTime::createFromFormat( LATEPOINT_DATETIME_DB_FORMAT, $datetime, $timezone ); |
| 75 | $css_class = 'left-days'; |
| 76 | |
| 77 | $before = ( $now_datetime < $event_datetime ) ? __( 'in', 'latepoint' ) . ' ' : ''; |
| 78 | $ago = ( $now_datetime > $event_datetime ) ? ' ' . __( 'ago', 'latepoint' ) : ''; |
| 79 | $ago_class = empty( $ago ) ? '' : 'time-past'; |
| 80 | if ( $event_datetime ) { |
| 81 | $diff = $now_datetime->diff( $event_datetime ); |
| 82 | if ( $diff->d > 0 ) { |
| 83 | $left = $before . $diff->format( '%a ' . __( 'days', 'latepoint' ) ) . $ago; |
| 84 | } else { |
| 85 | if ( $diff->h > 0 ) { |
| 86 | $css_class = 'left-hours'; |
| 87 | $left = $before . $diff->format( '%h ' . __( 'hours', 'latepoint' ) ) . $ago; |
| 88 | } else { |
| 89 | $css_class = 'left-minutes'; |
| 90 | $left = $before . $diff->format( '%i ' . __( 'minutes', 'latepoint' ) ) . $ago; |
| 91 | } |
| 92 | } |
| 93 | } else { |
| 94 | $left = 'n/a'; |
| 95 | } |
| 96 | |
| 97 | return '<span class="time-left ' . esc_attr( $css_class ) . ' ' . esc_attr( $ago_class ) . '">' . esc_html( $left ) . '</span>'; |
| 98 | } |
| 99 | |
| 100 | public static function now_datetime_utc_in_db_format() { |
| 101 | return OsWpDateTime::datetime_in_utc( new OsWpDateTime( 'now' ), LATEPOINT_DATETIME_DB_FORMAT ); |
| 102 | } |
| 103 | |
| 104 | public static function custom_datetime_utc_in_db_format( string $time = 'now' ): string { |
| 105 | return OsWpDateTime::datetime_in_utc( new OsWpDateTime( $time ), LATEPOINT_DATETIME_DB_FORMAT ); |
| 106 | } |
| 107 | |
| 108 | public static function now_datetime_in_db_format() { |
| 109 | return self::now_datetime_in_format( LATEPOINT_DATETIME_DB_FORMAT ); |
| 110 | } |
| 111 | |
| 112 | public static function now_datetime_object() { |
| 113 | return new OsWpDateTime( 'now' ); |
| 114 | } |
| 115 | |
| 116 | public static function get_time_system() { |
| 117 | return OsSettingsHelper::get_time_system(); |
| 118 | } |
| 119 | |
| 120 | public static function get_time_format() { |
| 121 | return self::is_army_clock() ? 'H:i' : 'g:ia'; |
| 122 | } |
| 123 | |
| 124 | public static function is_army_clock() { |
| 125 | return ( self::get_time_system() == 24 ); |
| 126 | } |
| 127 | |
| 128 | public static function get_time_systems_list_for_select() { |
| 129 | return array( |
| 130 | array( |
| 131 | 'value' => '12', |
| 132 | 'label' => __( '12-hour clock', 'latepoint' ), |
| 133 | ), |
| 134 | array( |
| 135 | 'value' => '24', |
| 136 | 'label' => __( '24-hour clock', 'latepoint' ), |
| 137 | ), |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | public static function get_date_formats_list_for_select() { |
| 142 | return array( |
| 143 | array( |
| 144 | 'value' => 'm/d/Y', |
| 145 | 'label' => __( 'MM/DD/YYYY', 'latepoint' ), |
| 146 | ), |
| 147 | array( |
| 148 | 'value' => 'm.d.Y', |
| 149 | 'label' => __( 'MM.DD.YYYY', 'latepoint' ), |
| 150 | ), |
| 151 | array( |
| 152 | 'value' => 'd/m/Y', |
| 153 | 'label' => __( 'DD/MM/YYYY', 'latepoint' ), |
| 154 | ), |
| 155 | array( |
| 156 | 'value' => 'd.m.Y', |
| 157 | 'label' => __( 'DD.MM.YYYY', 'latepoint' ), |
| 158 | ), |
| 159 | array( |
| 160 | 'value' => 'Y-m-d', |
| 161 | 'label' => __( 'YYYY-MM-DD', 'latepoint' ), |
| 162 | ), |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | public static function get_time_systems_list() { |
| 167 | return array( |
| 168 | '12' => __( '12-hour clock', 'latepoint' ), |
| 169 | '24' => __( '24-hour clock', 'latepoint' ), |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | public static function format_date_with_locale( string $format, DateTime $date_obj ): string { |
| 175 | return OsUtilHelper::translate_months( $date_obj->format( $format ) ); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | public static function get_nice_date_with_optional_year( $date, $show_year_if_not_current = true, $short_month = false ) { |
| 180 | $d = OsWpDateTime::os_createFromFormat( 'Y-m-d', $date ); |
| 181 | if ( ! $d ) { |
| 182 | return $date; |
| 183 | } |
| 184 | if ( ! $show_year_if_not_current || ( $d->format( 'Y' ) == OsTimeHelper::today_date( 'Y' ) ) ) { |
| 185 | return OsUtilHelper::translate_months( $d->format( OsSettingsHelper::get_readable_date_format( true, $short_month ) ) ); |
| 186 | } else { |
| 187 | return OsUtilHelper::translate_months( $d->format( OsSettingsHelper::get_readable_date_format( false, $short_month ) ) ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public static function get_readable_date( DateTime $date, string $output_timezone_name = '' ): string { |
| 192 | if ( empty( $output_timezone_name ) ) { |
| 193 | $output_timezone_name = self::get_wp_timezone_name(); |
| 194 | } |
| 195 | $date->setTimezone( new DateTimeZone( $output_timezone_name ) ); |
| 196 | |
| 197 | return self::format_date_with_locale( OsSettingsHelper::get_readable_date_format(), $date ); |
| 198 | } |
| 199 | |
| 200 | public static function get_readable_date_from_string( string $date, string $input_timezone_name = '', string $output_timezone_name = '' ): string { |
| 201 | if ( empty( $input_timezone_name ) ) { |
| 202 | $input_timezone_name = 'UTC'; |
| 203 | } |
| 204 | if ( empty( $output_timezone_name ) ) { |
| 205 | $output_timezone_name = self::get_wp_timezone_name(); |
| 206 | } |
| 207 | |
| 208 | $date_obj = new OsWpDateTime( $date, new DateTimeZone( $input_timezone_name ) ); |
| 209 | |
| 210 | return self::get_readable_date( $date_obj, $output_timezone_name ); |
| 211 | } |
| 212 | |
| 213 | public static function get_wp_timezone() { |
| 214 | if ( self::$timezone ) { |
| 215 | return self::$timezone; |
| 216 | } |
| 217 | try { |
| 218 | $timezone_string = get_option( 'timezone_string' ); |
| 219 | if ( ! empty( $timezone_string ) ) { |
| 220 | return new DateTimeZone( $timezone_string ); |
| 221 | } |
| 222 | $offset = get_option( 'gmt_offset' ); |
| 223 | $hours = (int) $offset; |
| 224 | $minutes = abs( ( $offset - (int) $offset ) * 60 ); |
| 225 | $offset = sprintf( '%+03d:%02d', $hours, $minutes ); |
| 226 | self::$timezone = new DateTimeZone( $offset ); |
| 227 | } catch ( Exception $e ) { |
| 228 | return new DateTimeZone( 'UTC' ); |
| 229 | } |
| 230 | |
| 231 | return self::$timezone; |
| 232 | } |
| 233 | |
| 234 | public static function get_wp_timezone_name() { |
| 235 | $timezone_obj = self::get_wp_timezone(); |
| 236 | if ( $timezone_obj ) { |
| 237 | return $timezone_obj->getName(); |
| 238 | } else { |
| 239 | return 'America/New_York'; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | public static function get_timezone_from_session() { |
| 244 | try { |
| 245 | $timezone = new DateTimeZone( self::get_timezone_name_from_session() ); |
| 246 | } catch ( Exception $e ) { |
| 247 | $timezone = new DateTimeZone( self::get_wp_timezone_name() ); |
| 248 | } |
| 249 | |
| 250 | return $timezone; |
| 251 | } |
| 252 | |
| 253 | public static function get_timezone_name_from_session() { |
| 254 | $timezone_name = self::get_wp_timezone_name(); |
| 255 | $timezone_name = apply_filters( 'latepoint_timezone_name_from_session', $timezone_name ); |
| 256 | |
| 257 | return $timezone_name; |
| 258 | } |
| 259 | |
| 260 | public static function is_timezone_saved_in_session() { |
| 261 | return ( isset( $_COOKIE[ LATEPOINT_SELECTED_TIMEZONE_COOKIE ] ) && ! empty( $_COOKIE[ LATEPOINT_SELECTED_TIMEZONE_COOKIE ] ) ); |
| 262 | } |
| 263 | |
| 264 | public static function set_timezone_name_in_cookie( $timezone_name ) { |
| 265 | OsSessionsHelper::setcookie( LATEPOINT_SELECTED_TIMEZONE_COOKIE, $timezone_name ); |
| 266 | $_COOKIE[ LATEPOINT_SELECTED_TIMEZONE_COOKIE ] = $timezone_name; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | public static function convert_datetime_to_minutes( DateTime $datetime ) { |
| 271 | return $datetime->format( 'i' ) + ( $datetime->format( 'G' ) * 60 ); |
| 272 | } |
| 273 | |
| 274 | public static function get_current_minutes() { |
| 275 | $now = new OsWpDateTime( 'now' ); |
| 276 | return $now->format( 'i' ) + ( $now->format( 'G' ) * 60 ); |
| 277 | } |
| 278 | |
| 279 | public static function convert_time_to_minutes( $time, $ampm = false ) { |
| 280 | if ( strpos( $time, ':' ) === false ) { |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | list( $hours, $minutes ) = explode( ':', $time ); |
| 285 | if ( $hours == '12' && $ampm == 'am' ) { |
| 286 | // midnight |
| 287 | $hours = '0'; |
| 288 | } |
| 289 | if ( $ampm == 'pm' && $hours < 12 ) { |
| 290 | // convert to 24 hour format |
| 291 | $hours = $hours + 12; |
| 292 | } |
| 293 | $minutes = ( $hours * 60 ) + $minutes; |
| 294 | |
| 295 | return $minutes; |
| 296 | } |
| 297 | |
| 298 | public static function am_or_pm( $minutes ) { |
| 299 | if ( self::is_army_clock() ) { |
| 300 | return ''; |
| 301 | } |
| 302 | |
| 303 | return ( $minutes < 720 ) ? 'am' : 'pm'; |
| 304 | } |
| 305 | |
| 306 | public static function minutes_to_hours( $time ) { |
| 307 | if ( $time ) { |
| 308 | $hours = floor( $time / 60 ); |
| 309 | if ( ! self::is_army_clock() && $hours > 12 ) { |
| 310 | $hours = $hours - 12; |
| 311 | } |
| 312 | if ( ! self::is_army_clock() && ! $hours ) { |
| 313 | $hours = 12; |
| 314 | } |
| 315 | |
| 316 | return $hours; |
| 317 | } else { |
| 318 | // if am/pm - we don't show 0, we show 12 |
| 319 | return ( self::is_army_clock() ) ? 0 : 12; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | |
| 324 | public static function minutes_to_army_hours_and_minutes( $time_in_minutes ) { |
| 325 | if ( is_null( $time_in_minutes ) || $time_in_minutes === '' ) { |
| 326 | return __( 'n/a', 'latepoint' ); |
| 327 | } |
| 328 | $hours = floor( $time_in_minutes / 60 ); |
| 329 | $minutes = ( $time_in_minutes % 60 ); |
| 330 | |
| 331 | return sprintf( '%02d:%02d', $hours, $minutes ); |
| 332 | } |
| 333 | |
| 334 | public static function minutes_to_hours_and_minutes( $minutes, $format = '%02d:%02d', $add_ampm = true, $hide_if_zero_minutes = false ) { |
| 335 | if ( is_null( $minutes ) ) { |
| 336 | return 'n/a'; |
| 337 | } |
| 338 | if ( ! $format ) { |
| 339 | $format = '%02d:%02d'; |
| 340 | } |
| 341 | |
| 342 | if ( $minutes === '' ) { |
| 343 | return; |
| 344 | } |
| 345 | $ampm = ( $add_ampm ) ? self::am_or_pm( $minutes ) : ''; |
| 346 | $hours = self::minutes_to_hours( $minutes ); |
| 347 | $minutes = ( $minutes % 60 ); |
| 348 | if ( $hide_if_zero_minutes && ! $minutes ) { |
| 349 | return $hours . ' ' . $ampm; |
| 350 | } else { |
| 351 | return sprintf( $format, $hours, $minutes ) . $ampm; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | public static function timezones_options_list_styled( $selected_zone, $locale = null ) { |
| 357 | static $mo_loaded = false, $locale_loaded = null; |
| 358 | |
| 359 | $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific' ); |
| 360 | |
| 361 | // Load translations for continents and cities. |
| 362 | if ( ! $mo_loaded || $locale !== $locale_loaded ) { |
| 363 | $locale_loaded = $locale ? $locale : get_locale(); |
| 364 | $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo'; |
| 365 | unload_textdomain( 'continents-cities' ); |
| 366 | load_textdomain( 'continents-cities', $mofile ); |
| 367 | $mo_loaded = true; |
| 368 | } |
| 369 | |
| 370 | $time_format = self::get_time_format(); |
| 371 | |
| 372 | $zonen = array(); |
| 373 | foreach ( timezone_identifiers_list() as $zone ) { |
| 374 | $zone = explode( '/', $zone ); |
| 375 | if ( ! in_array( $zone[0], $continents ) ) { |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later |
| 380 | $exists = array( |
| 381 | 0 => ( isset( $zone[0] ) && $zone[0] ), |
| 382 | 1 => ( isset( $zone[1] ) && $zone[1] ), |
| 383 | 2 => ( isset( $zone[2] ) && $zone[2] ), |
| 384 | ); |
| 385 | $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] ); |
| 386 | $exists[4] = ( $exists[1] && $exists[3] ); |
| 387 | $exists[5] = ( $exists[2] && $exists[3] ); |
| 388 | |
| 389 | // phpcs:disable WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText |
| 390 | $zonen[] = array( |
| 391 | 'continent' => ( $exists[0] ? $zone[0] : '' ), |
| 392 | 'city' => ( $exists[1] ? $zone[1] : '' ), |
| 393 | 'subcity' => ( $exists[2] ? $zone[2] : '' ), |
| 394 | 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ), |
| 395 | 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ), |
| 396 | 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' ), |
| 397 | ); |
| 398 | // phpcs:enable |
| 399 | } |
| 400 | usort( $zonen, '_wp_timezone_choice_usort_callback' ); |
| 401 | |
| 402 | $structure = array(); |
| 403 | |
| 404 | foreach ( $zonen as $key => $zone ) { |
| 405 | // Build value in an array to join later |
| 406 | $value = array( $zone['continent'] ); |
| 407 | |
| 408 | if ( empty( $zone['city'] ) ) { |
| 409 | // It's at the continent level (generally won't happen) |
| 410 | $display = $zone['t_continent']; |
| 411 | } else { |
| 412 | // It's inside a continent group |
| 413 | |
| 414 | // Continent optgroup |
| 415 | if ( ! isset( $zonen[ $key - 1 ] ) || $zonen[ $key - 1 ]['continent'] !== $zone['continent'] ) { |
| 416 | $label = $zone['t_continent']; |
| 417 | $structure[] = '<div class="os-timezone-group"><div class="os-timezone-group-header">' . esc_html( $label ) . '</div>'; |
| 418 | } |
| 419 | |
| 420 | // Add the city to the value |
| 421 | $value[] = $zone['city']; |
| 422 | |
| 423 | $display = $zone['t_city']; |
| 424 | if ( ! empty( $zone['subcity'] ) ) { |
| 425 | // Add the subcity to the value |
| 426 | $value[] = $zone['subcity']; |
| 427 | $display .= ' - ' . $zone['t_subcity']; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // Build the value |
| 432 | $value = join( '/', $value ); |
| 433 | $selected = ''; |
| 434 | if ( $value === $selected_zone ) { |
| 435 | $selected = 'selected'; |
| 436 | } |
| 437 | try { |
| 438 | $local_time = new OsWpDateTime( 'now', new DateTimeZone( $value ) ); |
| 439 | $local_time_value = $local_time->format( $time_format ); |
| 440 | } catch ( Exception $e ) { |
| 441 | $local_time_value = ''; |
| 442 | } |
| 443 | $structure[] = '<div class="os-timezone-selector-option ' . $selected . '" data-value="' . esc_attr( $value ) . '"><div>' . esc_html( $display ) . '</div><div class="os-timezone-selector-option-local-time">' . $local_time_value . '</div></div>'; |
| 444 | |
| 445 | // Close continent optgroup |
| 446 | if ( ! empty( $zone['city'] ) && ( ! isset( $zonen[ $key + 1 ] ) || ( isset( $zonen[ $key + 1 ] ) && $zonen[ $key + 1 ]['continent'] !== $zone['continent'] ) ) ) { |
| 447 | $structure[] = '</div>'; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // Do UTC |
| 452 | $structure[] = '<div class="os-timezone-group"><div class="os-timezone-group-header">' . esc_attr__( 'UTC', 'latepoint' ) . '</div>'; |
| 453 | $selected = ''; |
| 454 | if ( 'UTC' === $selected_zone ) { |
| 455 | $selected = 'selected'; |
| 456 | } |
| 457 | $structure[] = '<div class="os-timezone-selector-option ' . $selected . ' data-value="' . esc_attr( 'UTC' ) . '">' . __( 'UTC', 'latepoint' ) . '</div>'; |
| 458 | $structure[] = '</div>'; |
| 459 | |
| 460 | return join( "\n", $structure ); |
| 461 | } |
| 462 | |
| 463 | public static function timezones_options_list( $selected_zone, $locale = null ) { |
| 464 | static $mo_loaded = false, $locale_loaded = null; |
| 465 | |
| 466 | $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific' ); |
| 467 | |
| 468 | // Load translations for continents and cities. |
| 469 | if ( ! $mo_loaded || $locale !== $locale_loaded ) { |
| 470 | $locale_loaded = $locale ? $locale : get_locale(); |
| 471 | $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo'; |
| 472 | unload_textdomain( 'continents-cities' ); |
| 473 | load_textdomain( 'continents-cities', $mofile ); |
| 474 | $mo_loaded = true; |
| 475 | } |
| 476 | |
| 477 | $zonen = array(); |
| 478 | foreach ( timezone_identifiers_list() as $zone ) { |
| 479 | $zone = explode( '/', $zone ); |
| 480 | if ( ! in_array( $zone[0], $continents ) ) { |
| 481 | continue; |
| 482 | } |
| 483 | |
| 484 | // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later |
| 485 | $exists = array( |
| 486 | 0 => ( isset( $zone[0] ) && $zone[0] ), |
| 487 | 1 => ( isset( $zone[1] ) && $zone[1] ), |
| 488 | 2 => ( isset( $zone[2] ) && $zone[2] ), |
| 489 | ); |
| 490 | $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] ); |
| 491 | $exists[4] = ( $exists[1] && $exists[3] ); |
| 492 | $exists[5] = ( $exists[2] && $exists[3] ); |
| 493 | |
| 494 | // phpcs:disable WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText |
| 495 | $zonen[] = array( |
| 496 | 'continent' => ( $exists[0] ? $zone[0] : '' ), |
| 497 | 'city' => ( $exists[1] ? $zone[1] : '' ), |
| 498 | 'subcity' => ( $exists[2] ? $zone[2] : '' ), |
| 499 | 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ), |
| 500 | 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ), |
| 501 | 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' ), |
| 502 | ); |
| 503 | // phpcs:enable |
| 504 | } |
| 505 | usort( $zonen, '_wp_timezone_choice_usort_callback' ); |
| 506 | |
| 507 | $structure = array(); |
| 508 | |
| 509 | if ( empty( $selected_zone ) ) { |
| 510 | $structure[] = '<option selected="selected" value="">' . __( 'Select a city', 'latepoint' ) . '</option>'; |
| 511 | } |
| 512 | |
| 513 | foreach ( $zonen as $key => $zone ) { |
| 514 | // Build value in an array to join later |
| 515 | $value = array( $zone['continent'] ); |
| 516 | |
| 517 | if ( empty( $zone['city'] ) ) { |
| 518 | // It's at the continent level (generally won't happen) |
| 519 | $display = $zone['t_continent']; |
| 520 | } else { |
| 521 | // It's inside a continent group |
| 522 | |
| 523 | // Continent optgroup |
| 524 | if ( ! isset( $zonen[ $key - 1 ] ) || $zonen[ $key - 1 ]['continent'] !== $zone['continent'] ) { |
| 525 | $label = $zone['t_continent']; |
| 526 | $structure[] = '<optgroup label="' . esc_attr( $label ) . '">'; |
| 527 | } |
| 528 | |
| 529 | // Add the city to the value |
| 530 | $value[] = $zone['city']; |
| 531 | |
| 532 | $display = $zone['t_city']; |
| 533 | if ( ! empty( $zone['subcity'] ) ) { |
| 534 | // Add the subcity to the value |
| 535 | $value[] = $zone['subcity']; |
| 536 | $display .= ' - ' . $zone['t_subcity']; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Build the value |
| 541 | $value = join( '/', $value ); |
| 542 | $selected = ''; |
| 543 | if ( $value === $selected_zone ) { |
| 544 | $selected = 'selected="selected" '; |
| 545 | } |
| 546 | $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $label ) . ', ' . esc_html( $display ) . '</option>'; |
| 547 | |
| 548 | // Close continent optgroup |
| 549 | if ( ! empty( $zone['city'] ) && ( ! isset( $zonen[ $key + 1 ] ) || ( isset( $zonen[ $key + 1 ] ) && $zonen[ $key + 1 ]['continent'] !== $zone['continent'] ) ) ) { |
| 550 | $structure[] = '</optgroup>'; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | // Do UTC |
| 555 | $structure[] = '<optgroup label="' . esc_attr__( 'UTC', 'latepoint' ) . '">'; |
| 556 | $selected = ''; |
| 557 | if ( 'UTC' === $selected_zone ) { |
| 558 | $selected = 'selected="selected" '; |
| 559 | } |
| 560 | $structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __( 'UTC', 'latepoint' ) . '</option>'; |
| 561 | $structure[] = '</optgroup>'; |
| 562 | |
| 563 | return join( "\n", $structure ); |
| 564 | } |
| 565 | |
| 566 | public static function format_to_nice_time( $datetime ): string { |
| 567 | if ( $datetime instanceof DateTime ) { |
| 568 | $format = self::is_army_clock() ? 'H:i' : 'g:ia'; |
| 569 | |
| 570 | return $datetime->format( $format ); |
| 571 | } else { |
| 572 | return 'n/a'; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | public static function format_to_nice_date( $datetime, $hide_year_if_current = false ): string { |
| 577 | if ( $datetime instanceof DateTime ) { |
| 578 | if ( $hide_year_if_current && ( $datetime->format( 'Y' ) == self::today_date( 'Y' ) ) ) { |
| 579 | $format = OsSettingsHelper::get_readable_date_format( true ); |
| 580 | } else { |
| 581 | $format = OsSettingsHelper::get_readable_date_format(); |
| 582 | } |
| 583 | |
| 584 | return OsUtilHelper::translate_months( $datetime->format( $format ) ); |
| 585 | } else { |
| 586 | return 'n/a'; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 |