class-fl-builder-timezones.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Helper class for font settings.
  4. *
  5. * @class FLBuilderFonts
  6. * @since 1.6.3
  7. */
  8. final class FLBuilderTimezones {
  9. // We are adding the phpcs ignore here as this code uses translate() and will fail tests.
  10. // The function was originally copied from WordPress Core wp-includes/functions.php
  11. // Original function wp_timezone_choice() is not pluggable or filterable and will
  12. // return data that breaks the module options.
  13. // @codingStandardsIgnoreStart
  14. /**
  15. * An array of continents.
  16. * @var array
  17. */
  18. static private $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
  19. /**
  20. * Generates a list of timezones.
  21. *
  22. * @since 1.6.5
  23. *
  24. * @var bool $mo_loaded
  25. *
  26. * @param string $selected_zone Selected timezone.
  27. * @return string
  28. */
  29. static public function build_timezones( $selected_zone ) {
  30. static $mo_loaded = false;
  31. $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
  32. // Load translations for continents and cities
  33. if ( !$mo_loaded ) {
  34. $locale = get_locale();
  35. $mofile = WP_LANG_DIR . '/continents-cities-' . $locale . '.mo';
  36. load_textdomain( 'continents-cities', $mofile );
  37. $mo_loaded = true;
  38. }
  39. $zonen = array();
  40. foreach ( timezone_identifiers_list() as $zone ) {
  41. $zone = explode( '/', $zone );
  42. if ( !in_array( $zone[0], $continents ) ) {
  43. continue;
  44. }
  45. // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
  46. $exists = array(
  47. 0 => ( isset( $zone[0] ) && $zone[0] ),
  48. 1 => ( isset( $zone[1] ) && $zone[1] ),
  49. 2 => ( isset( $zone[2] ) && $zone[2] ),
  50. );
  51. $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] );
  52. $exists[4] = ( $exists[1] && $exists[3] );
  53. $exists[5] = ( $exists[2] && $exists[3] );
  54. $zonen[] = array(
  55. 'continent' => ( $exists[0] ? $zone[0] : '' ),
  56. 'city' => ( $exists[1] ? $zone[1] : '' ),
  57. 'subcity' => ( $exists[2] ? $zone[2] : '' ),
  58. 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
  59. 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
  60. 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' )
  61. );
  62. }
  63. usort( $zonen, '_wp_timezone_choice_usort_callback' );
  64. $structure = array();
  65. if ( empty( $selected_zone ) ) {
  66. $structure[] = '<option selected="selected" value="">' . __( 'Select a city', 'fl-builder' ) . '</option>';
  67. }
  68. foreach ( $zonen as $key => $zone ) {
  69. // Build value in an array to join later
  70. $value = array( $zone['continent'] );
  71. if ( empty( $zone['city'] ) ) {
  72. // It's at the continent level (generally won't happen)
  73. $display = $zone['t_continent'];
  74. } else {
  75. // It's inside a continent group
  76. // Continent optgroup
  77. if ( !isset( $zonen[$key - 1] ) || $zonen[$key - 1]['continent'] !== $zone['continent'] ) {
  78. $label = $zone['t_continent'];
  79. $structure[] = '<optgroup label="'. esc_attr( $label ) .'">';
  80. }
  81. // Add the city to the value
  82. $value[] = $zone['city'];
  83. $display = $zone['t_city'];
  84. if ( !empty( $zone['subcity'] ) ) {
  85. // Add the subcity to the value
  86. $value[] = $zone['subcity'];
  87. $display .= ' - ' . $zone['t_subcity'];
  88. }
  89. }
  90. // Build the value
  91. $value = join( '/', $value );
  92. $selected = '';
  93. if ( $value === $selected_zone ) {
  94. $selected = 'selected="selected" ';
  95. }
  96. $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . "</option>";
  97. // Close continent optgroup
  98. if ( !empty( $zone['city'] ) && ( !isset($zonen[$key + 1]) || (isset( $zonen[$key + 1] ) && $zonen[$key + 1]['continent'] !== $zone['continent']) ) ) {
  99. $structure[] = '</optgroup>';
  100. }
  101. }
  102. // Do UTC
  103. $structure[] = '<optgroup label="'. esc_attr__( 'UTC', 'fl-builder' ) .'">';
  104. $selected = '';
  105. if ( 'UTC' === $selected_zone )
  106. $selected = 'selected="selected" ';
  107. $structure[] = '<option ' . $selected . 'value="' . esc_attr__( 'UTC', 'fl-builder' ) . '">' . __( 'UTC', 'fl-builder' ) . '</option>';
  108. $structure[] = '</optgroup>';
  109. return join( "\n", $structure );
  110. }
  111. // @codingStandardsIgnoreEnd
  112. }