Skip to content

Commit 41a1690

Browse files
committed
sanitize_option(): fix bug in sanitization of timezone_string
This fixes a bug where if the `timezone_string` would be set to timezone name which has since been deprecated, the option value would be "lost" when saving the value again, as the comparison being done to verify whether it is a valid timezone name would only take "current" timezone names into account and would invalidate deprecated timezone names. By passing the `DateTimeZone::ALL_WITH_BC` constant as the `$timezoneGroup` parameter to the PHP native `timezone_identifiers_list()` function, a timezone name list is retrieved containing both current and deprecated timezone names, preventing the invalidation of the option value. See the extensive write-up about this in Trac#56468. Also see: https://www.php.net/manual/en/datetimezone.listidentifiers.php Includes adding a dedicated test to the data provider used in the `Tests_Option_SanitizeOption` test class. Note: I have made this a _named data set_, even though the other data sets are unnamed, to make sure it is clear what this data set is testing. Adding test names for the original data sets in this data provider would be a great future improvement, but is outside of the scope of this commit/ticket.
1 parent 24003e5 commit 41a1690

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

src/wp-includes/formatting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4923,7 +4923,7 @@ function sanitize_option( $option, $value ) {
49234923
break;
49244924

49254925
case 'timezone_string':
4926-
$allowed_zones = timezone_identifiers_list();
4926+
$allowed_zones = timezone_identifiers_list( DateTimeZone::ALL_WITH_BC );
49274927
if ( ! in_array( $value, $allowed_zones, true ) && ! empty( $value ) ) {
49284928
$error = __( 'The timezone you have entered is not valid. Please select a valid timezone.' );
49294929
}

tests/phpunit/tests/option/sanitizeOption.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function sanitize_option_provider() {
6969
array( 'timezone_string', 0, 0 ),
7070
array( 'timezone_string', 'Europe/London', 'Europe/London' ),
7171
array( 'timezone_string', get_option( 'timezone_string' ), 'invalid' ),
72+
'Deprecated timezone string is accepted as valid (Trac#56468)' => array( 'timezone_string', 'America/Buenos_Aires', 'America/Buenos_Aires' ),
7273
array( 'permalink_structure', '', '' ),
7374
array( 'permalink_structure', '/%year%/%20%postname%', '/%year%/ %postname%' ),
7475
array( 'default_role', 'subscriber', 'subscriber' ),

0 commit comments

Comments
 (0)