Skip to content

Commit 172abc0

Browse files
committed
wp_timezone_choice(): fix bug in timezone dropdown list creation
This fixes a bug where if the `timezone_string` would be set to timezone name which has since been deprecated, no option would be (pre-)selected in the generated dropdown list and when the form using the dropdown list would be submitted, the "old", originally saved value would be lost as the form would submit without a value being selected for the `timezone_string` field. The fix is a little hacky: it basically checks ahead of generating the actual dropdown list whether the `$selected_zone` value would be recognized and set to "selected" and if not, verifies the value _is_ a valid value, but outdated timezone name value and if so, adds an extra dropdown entry to the top of the list with the original value and sets this value to "selected". See the extensive write-up about this in Trac#56468. Also see: https://www.php.net/manual/en/datetimezone.listidentifiers.php Note: there are no pre-existing tests at all for this method and adding a complete set of tests for this method is outside the scope of this ticket, so this fix does not contain any tests.
1 parent e0e2541 commit 172abc0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/wp-includes/functions.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6381,8 +6381,10 @@ function wp_timezone_choice( $selected_zone, $locale = null ) {
63816381
$mo_loaded = true;
63826382
}
63836383

6384-
$zonen = array();
6385-
foreach ( timezone_identifiers_list() as $zone ) {
6384+
$tz_identifiers = timezone_identifiers_list();
6385+
$zonen = array();
6386+
6387+
foreach ( $tz_identifiers as $zone ) {
63866388
$zone = explode( '/', $zone );
63876389
if ( ! in_array( $zone[0], $continents, true ) ) {
63886390
continue;
@@ -6417,6 +6419,13 @@ function wp_timezone_choice( $selected_zone, $locale = null ) {
64176419
$structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>';
64186420
}
64196421

6422+
// If this is a deprecated, but valid, timezone string, display it at the top of the list as-is.
6423+
if ( in_array( $selected_zone, $tz_identifiers, true ) === false
6424+
&& in_array( $selected_zone, timezone_identifiers_list( DateTimeZone::ALL_WITH_BC ), true )
6425+
) {
6426+
$structure[] = '<option selected="selected" value="' . esc_attr( $selected_zone ) . '">' . esc_html( $selected_zone ) . '</option>';
6427+
}
6428+
64206429
foreach ( $zonen as $key => $zone ) {
64216430
// Build value in an array to join later.
64226431
$value = array( $zone['continent'] );

0 commit comments

Comments
 (0)