Make WordPress Core


Ignore:
Timestamp:
09/20/2022 01:00:24 AM (2 years ago)
Author:
SergeyBiryukov
Message:

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: 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r54226 r54233  
    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 ) ) {
     
    64166418    if ( empty( $selected_zone ) ) {
    64176419        $structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>';
     6420    }
     6421
     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>';
    64186427    }
    64196428
Note: See TracChangeset for help on using the changeset viewer.