Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/wp-admin/options-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,7 @@ class="<?php echo esc_attr( $classes_for_button ); ?>"

if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists.
$check_zone_info = false;
if ( 0 === (int) $current_offset ) {
$tzstring = 'UTC+0';
} elseif ( $current_offset < 0 ) {
$tzstring = 'UTC' . $current_offset;
} else {
$tzstring = 'UTC+' . $current_offset;
}
$tzstring = wp_get_timezone_offset_choice( $current_offset );
}

?>
Expand Down
6 changes: 3 additions & 3 deletions src/wp-admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@
}

// Map UTC+- timezones to gmt_offsets and set timezone_string to empty.
if ( ! empty( $_POST['timezone_string'] ) && preg_match( '/^UTC[+-]/', $_POST['timezone_string'] ) ) {
$_POST['gmt_offset'] = $_POST['timezone_string'];
$_POST['gmt_offset'] = preg_replace( '/UTC\+?/', '', $_POST['gmt_offset'] );
$gmt_offset = isset( $_POST['timezone_string'] ) ? wp_parse_timezone_offset_choice( $_POST['timezone_string'] ) : null;
if ( null !== $gmt_offset ) {
$_POST['gmt_offset'] = $gmt_offset;
$_POST['timezone_string'] = '';
} elseif ( isset( $_POST['timezone_string'] ) && ! in_array( $_POST['timezone_string'], timezone_identifiers_list( DateTimeZone::ALL_WITH_BC ), true ) ) {
// Reset to the current value.
Expand Down
32 changes: 31 additions & 1 deletion src/wp-includes/abilities/class-wp-abilities-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ private function execute_settings_update( $input ) {
continue;
}

/*
* A manual UTC offset, such as `UTC+2`, is stored in `gmt_offset`, and the timezone string
* is emptied, as Settings > General does. The offset is stored first: the timezone string
* overrides it until it is emptied, so a call stopped between the two writes leaves the
* site on its current timezone.
*/
$gmt_offset = 'timezone_string' === strtolower( $option ) ? wp_parse_timezone_offset_choice( $value ) : null;
if ( null !== $gmt_offset ) {
update_option( 'gmt_offset', $gmt_offset );
$value = '';
}

update_option( $option, $value );

/*
Expand All @@ -362,6 +374,11 @@ private function execute_settings_update( $input ) {
if ( null === get_option( $option, null ) ) {
add_option( $option, get_option( $option ) );
}

// While the timezone string was set, update_option() read the offset it overrode, and could not add a missing row.
if ( null !== $gmt_offset && null === get_option( 'gmt_offset', null ) ) {
add_option( 'gmt_offset', $gmt_offset );
}
}

return $this->get_setting_values( $named );
Expand Down Expand Up @@ -415,7 +432,11 @@ private function get_refused_settings_error( string $code, array $params, int $s
private function get_invalid_value_message( string $option, $value, string $param ): ?string {
switch ( strtolower( $option ) ) {
case 'timezone_string':
if ( '' !== $value && ! in_array( $value, timezone_identifiers_list( DateTimeZone::ALL_WITH_BC ), true ) ) {
if (
'' !== $value
&& ! in_array( $value, timezone_identifiers_list( DateTimeZone::ALL_WITH_BC ), true )
&& ! in_array( $value, wp_get_timezone_offset_choices(), true )
) {
return __( 'The timezone you have entered is not valid. Please select a valid timezone.' );
}
break;
Expand Down Expand Up @@ -490,6 +511,15 @@ private function get_setting_values( array $settings ): array {
* boolean schema accepts as a value and an array schema reads as an empty list.
*/
$value = get_option( $setting['option'], $setting['default'] );

/*
* A site without a timezone string runs on the manual UTC offset stored in `gmt_offset`,
* read as the value of the timezone list that sets it, such as `UTC+2`.
*/
if ( 'timezone_string' === strtolower( $setting['option'] ) && ( '' === $value || null === $value ) ) {
$value = wp_get_timezone_offset_choice( get_option( 'gmt_offset' ) );
}

if ( null === $value ) {
continue;
}
Expand Down
93 changes: 74 additions & 19 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7026,8 +7026,33 @@ function wp_timezone_choice( $selected_zone, $locale = null ) {
$structure[] = '</optgroup>';

// Do manual UTC offsets.
$structure[] = '<optgroup label="' . esc_attr__( 'Manual Offsets' ) . '" dir="auto">';
$offset_range = array(
$structure[] = '<optgroup label="' . esc_attr__( 'Manual Offsets' ) . '" dir="auto">';
foreach ( wp_get_timezone_offset_choices() as $offset_value ) {
$offset_name = str_replace( array( '.25', '.5', '.75' ), array( ':15', ':30', ':45' ), $offset_value );
$selected = '';
if ( $offset_value === $selected_zone ) {
$selected = 'selected="selected" ';
}
$structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '" dir="auto">' . esc_html( $offset_name ) . '</option>';
}
$structure[] = '</optgroup>';

return implode( "\n", $structure );
}

/**
* Retrieves the manual UTC offsets that the timezone list offers.
*
* Each offset is returned as the value of its option in wp_timezone_choice(), such as `UTC+2`
* or `UTC-5.5`. Choosing one of them in Settings > General stores the offset in the
* `gmt_offset` option and empties the `timezone_string` option.
*
* @since 7.3.0
*
* @return string[] The manual UTC offsets, from `UTC-12` to `UTC+14`.
*/
function wp_get_timezone_offset_choices() {
$offsets = array(
-12,
-11.5,
-11,
Expand Down Expand Up @@ -7084,26 +7109,56 @@ function wp_timezone_choice( $selected_zone, $locale = null ) {
13.75,
14,
);
foreach ( $offset_range as $offset ) {
if ( 0 <= $offset ) {
$offset_name = '+' . $offset;
} else {
$offset_name = (string) $offset;
}

$offset_value = $offset_name;
$offset_name = str_replace( array( '.25', '.5', '.75' ), array( ':15', ':30', ':45' ), $offset_name );
$offset_name = 'UTC' . $offset_name;
$offset_value = 'UTC' . $offset_value;
$selected = '';
if ( $offset_value === $selected_zone ) {
$selected = 'selected="selected" ';
}
$structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '" dir="auto">' . esc_html( $offset_name ) . '</option>';
return array_map( 'wp_get_timezone_offset_choice', $offsets );
}

/**
* Retrieves the timezone list value of a UTC offset.
*
* Returns the value the timezone list in wp_timezone_choice() uses for a manual UTC offset,
* such as `UTC+2` for an offset of 2 hours, `UTC-5.5` for -5.5 hours, and `UTC+0` for no offset.
* Settings > General selects this value for a site without a timezone string.
*
* @since 7.3.0
*
* @param mixed $offset The UTC offset in hours, as stored in the `gmt_offset` option. A value that
* is not numeric, such as an empty string, is read as no offset.
* @return string The timezone list value of the offset.
*/
function wp_get_timezone_offset_choice( $offset ) {
$offset = is_numeric( $offset ) ? (float) $offset : 0.0;

// Also matches -0.0, which would read as `UTC+-0`.
if ( 0.0 === $offset ) {
return 'UTC+0';
}
$structure[] = '</optgroup>';

return implode( "\n", $structure );
return $offset < 0 ? 'UTC' . $offset : 'UTC+' . $offset;
}

/**
* Parses the UTC offset of a manual offset timezone value.
*
* Returns the offset that Settings > General stores in the `gmt_offset` option for a timezone
* value that starts with `UTC+` or `UTC-`, such as `2` for `UTC+2` and `-5.5` for `UTC-5.5`.
* Other values, such as `UTC` and timezone identifiers, are stored in the `timezone_string`
* option instead.
*
* The value is not checked against the offsets that the timezone list offers,
* see wp_get_timezone_offset_choices().
*
* @since 7.3.0
*
* @param mixed $choice The timezone value, such as `UTC+2` or `Europe/Lisbon`.
* @return string|null The UTC offset, or null when the value is not a manual offset.
*/
function wp_parse_timezone_offset_choice( $choice ) {
if ( ! is_string( $choice ) || ! preg_match( '/^UTC[+-]/', $choice ) ) {
return null;
}

return preg_replace( '/UTC\+?/', '', $choice );
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/wp-includes/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -2814,7 +2814,12 @@ function register_initial_settings() {
'show_in_rest' => array(
'name' => 'timezone',
),
'show_in_abilities' => true,
'show_in_abilities' => array(
'name' => 'timezone',
'schema' => array(
'description' => __( 'A city in the same timezone as you, such as Europe/Lisbon, or a UTC offset, such as UTC+2 or UTC-5.5.' ),
),
),
'type' => 'string',
'description' => __( 'A city in the same timezone as you.' ),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,58 @@ public function test_core_settings_get_skips_a_setting_with_an_unsupported_type(
$this->assertArrayNotHasKey( $option, $ability->get_output_schema()['properties'], 'A setting of an unsupported type should not be described in the output schema.' );
$this->assertArrayNotHasKey( $option, $ability->execute( array() ), 'A setting of an unsupported type should not be returned.' );
}

/**
* A site without a timezone string reads as its manual UTC offset, the value of the timezone
* list that sets it. A timezone identifier is read as stored.
*
* @ticket 66902
*
* @since 7.3.0
*
* @dataProvider data_timezones
*
* @param string|null $timezone_string The stored timezone string, or null for no stored value.
* @param string|null $gmt_offset The stored UTC offset, or null for no stored value.
* @param string $expected The expected timezone.
*/
public function test_core_settings_get_reads_the_timezone( ?string $timezone_string, ?string $gmt_offset, string $expected ): void {
foreach ( array(
'timezone_string' => $timezone_string,
'gmt_offset' => $gmt_offset,
) as $option => $value ) {
if ( null === $value ) {
delete_option( $option );
} else {
update_option( $option, $value );
}
}
$this->become_admin();

$result = wp_get_ability( 'core/settings-get' )->execute( array( 'fields' => array( 'timezone' ) ) );

$this->assertSame( array( 'timezone' => $expected ), $result );
}

/**
* Data provider.
*
* @return array<string, array{0: string|null, 1: string|null, 2: string}> Stored timezone strings and UTC offsets, and the expected timezones.
*/
public static function data_timezones(): array {
return array(
'a positive offset' => array( '', '2', 'UTC+2' ),
'a negative offset' => array( '', '-5.5', 'UTC-5.5' ),
'an offset of 45 minutes' => array( '', '5.75', 'UTC+5.75' ),
'an offset of half an hour' => array( '', '0.5', 'UTC+0.5' ),
'a negative offset of half an hour' => array( '', '-0.5', 'UTC-0.5' ),
'no offset' => array( '', '0', 'UTC+0' ),
'an offset with a trailing zero' => array( '', '5.50', 'UTC+5.5' ),
'an empty offset' => array( '', '', 'UTC+0' ),
'no stored offset' => array( '', null, 'UTC+0' ),
'no stored timezone string' => array( null, '3', 'UTC+3' ),
'a timezone identifier' => array( 'Europe/Lisbon', '5', 'Europe/Lisbon' ),
'UTC' => array( 'UTC', '3', 'UTC' ),
);
}
}
Loading
Loading