Skip to content

Commit 45da79e

Browse files
Date/Time: Correct timezone dropdown list creation in wp_timezone_choice().
This fixes a bug where if the `timezone_string` is set to a 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 is 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 that the value ''is'' a valid but outdated timezone name 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 ticket #56468. Also see: [https://www.php.net/manual/en/datetimezone.listidentifiers.php PHP Manual: timezone_identifiers_list()]. 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. Follow-up to [54207], [54217], [54227], [54229], [54230], [54232]. Props jrf, costdev, marcyoast. See #56468. git-svn-id: https://develop.svn.wordpress.org/trunk@54233 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 09e619c commit 45da79e

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)