From 2ed3baf58091efb857f9442b954264eb1a336540 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Wed, 2 Dec 2026 12:26:31 +0000 Subject: [PATCH 1/3] Date/Time: Add functions for the manual UTC offsets of the timezone list. Adds `wp_get_timezone_offset_choices()`, `wp_get_timezone_offset_choice()` and `wp_parse_timezone_offset_choice()`, which hold the manual UTC offsets that `wp_timezone_choice()` offers, such as `UTC+2`, and the conversions between those values and the `gmt_offset` option that Settings > General did inline. `wp_timezone_choice()`, `wp-admin/options.php` and `wp-admin/options-general.php` now use them. The timezone list is unchanged. Settings > General now selects `UTC+0.5` and `UTC-0.5` for a site on those offsets, instead of `UTC+0`. See #64616. --- src/wp-admin/options-general.php | 8 +- src/wp-admin/options.php | 6 +- src/wp-includes/functions.php | 93 +++++++++++++++---- .../functions/wpGetTimezoneOffsetChoice.php | 50 ++++++++++ .../functions/wpGetTimezoneOffsetChoices.php | 51 ++++++++++ .../functions/wpParseTimezoneOffsetChoice.php | 72 ++++++++++++++ 6 files changed, 251 insertions(+), 29 deletions(-) create mode 100644 tests/phpunit/tests/functions/wpGetTimezoneOffsetChoice.php create mode 100644 tests/phpunit/tests/functions/wpGetTimezoneOffsetChoices.php create mode 100644 tests/phpunit/tests/functions/wpParseTimezoneOffsetChoice.php diff --git a/src/wp-admin/options-general.php b/src/wp-admin/options-general.php index 1e6e00ea703d8..fa976324e3cd3 100644 --- a/src/wp-admin/options-general.php +++ b/src/wp-admin/options-general.php @@ -417,13 +417,7 @@ class="" 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 ); } ?> diff --git a/src/wp-admin/options.php b/src/wp-admin/options.php index 8f065f64473e6..3dccd9b8c0253 100644 --- a/src/wp-admin/options.php +++ b/src/wp-admin/options.php @@ -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. diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 0059df5b7d548..7994a2be7b9c3 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -7026,8 +7026,33 @@ function wp_timezone_choice( $selected_zone, $locale = null ) { $structure[] = ''; // Do manual UTC offsets. - $structure[] = ''; - $offset_range = array( + $structure[] = ''; + 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[] = ''; + } + $structure[] = ''; + + 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, @@ -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[] = ''; + 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[] = ''; - 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 ); } /** diff --git a/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoice.php b/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoice.php new file mode 100644 index 0000000000000..1818712dc2be0 --- /dev/null +++ b/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoice.php @@ -0,0 +1,50 @@ +assertSame( $expected, wp_get_timezone_offset_choice( $offset ) ); + } + + /** + * Data provider. + * + * @return array UTC offsets and their values. + */ + public static function data_offsets(): 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' ), + 'a negative zero' => array( '-0', 'UTC+0' ), + 'a trailing zero' => array( '5.50', 'UTC+5.5' ), + 'an integer' => array( -3, 'UTC-3' ), + 'a float' => array( 2.0, 'UTC+2' ), + 'an empty string' => array( '', 'UTC+0' ), + 'false' => array( false, 'UTC+0' ), + 'not a number' => array( 'two', 'UTC+0' ), + 'an array' => array( array( 2 ), 'UTC+0' ), + ); + } +} diff --git a/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoices.php b/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoices.php new file mode 100644 index 0000000000000..c7f1c6057cc4e --- /dev/null +++ b/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoices.php @@ -0,0 +1,51 @@ +assertCount( 55, $choices, 'There should be 55 manual UTC offsets.' ); + $this->assertSame( 'UTC-12', reset( $choices ), 'The first offset should be UTC-12.' ); + $this->assertSame( 'UTC+14', end( $choices ), 'The last offset should be UTC+14.' ); + $this->assertContains( 'UTC+0', $choices, 'No offset should be UTC+0.' ); + $this->assertContains( 'UTC+5.75', $choices, 'An offset of 45 minutes should use a decimal.' ); + } + + /** + * Each manual UTC offset is offered by the timezone list. + * + * @since 7.3.0 + */ + public function test_each_offset_is_offered_by_the_timezone_list(): void { + $list = wp_timezone_choice( '' ); + + foreach ( wp_get_timezone_offset_choices() as $choice ) { + $this->assertStringContainsString( 'value="' . $choice . '"', $list, "The timezone list should offer {$choice}." ); + } + } + + /** + * Each manual UTC offset is parsed into the offset that builds it again. + * + * @since 7.3.0 + */ + public function test_each_offset_is_parsed_into_the_offset_that_builds_it(): void { + foreach ( wp_get_timezone_offset_choices() as $choice ) { + $this->assertSame( $choice, wp_get_timezone_offset_choice( wp_parse_timezone_offset_choice( $choice ) ), "{$choice} should be built again from its offset." ); + } + } +} diff --git a/tests/phpunit/tests/functions/wpParseTimezoneOffsetChoice.php b/tests/phpunit/tests/functions/wpParseTimezoneOffsetChoice.php new file mode 100644 index 0000000000000..9a2ee48fd8b37 --- /dev/null +++ b/tests/phpunit/tests/functions/wpParseTimezoneOffsetChoice.php @@ -0,0 +1,72 @@ + General stores it. + * + * @since 7.3.0 + * + * @dataProvider data_manual_offsets + * + * @param string $choice The timezone value. + * @param string $expected The expected offset. + */ + public function test_returns_the_offset_of_a_manual_offset( string $choice, string $expected ): void { + $this->assertSame( $expected, wp_parse_timezone_offset_choice( $choice ) ); + } + + /** + * Data provider. + * + * @return array Manual offsets and their UTC offsets. + */ + public static function data_manual_offsets(): array { + return array( + 'a positive offset' => array( 'UTC+2', '2' ), + 'a negative offset' => array( 'UTC-5.5', '-5.5' ), + 'an offset of 45 minutes' => array( 'UTC+5.75', '5.75' ), + 'no offset' => array( 'UTC+0', '0' ), + 'an offset the list lacks' => array( 'UTC+99', '99' ), + 'not a number' => array( 'UTC+two', 'two' ), + ); + } + + /** + * Returns null for a value that is not a manual offset. + * + * @since 7.3.0 + * + * @dataProvider data_other_values + * + * @param mixed $choice The timezone value. + */ + public function test_returns_null_for_another_value( $choice ): void { + $this->assertNull( wp_parse_timezone_offset_choice( $choice ) ); + } + + /** + * Data provider. + * + * @return array Values that are not manual offsets. + */ + public static function data_other_values(): array { + return array( + 'UTC' => array( 'UTC' ), + 'a timezone identifier' => array( 'Europe/Lisbon' ), + 'an Etc identifier' => array( 'Etc/GMT+2' ), + 'lowercase' => array( 'utc+2' ), + 'an empty string' => array( '' ), + 'null' => array( null ), + 'an array' => array( array( 'UTC+2' ) ), + ); + } +} From adb237140859a0e341e7db08ebc2c2ae84189676 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Wed, 2 Dec 2026 12:26:31 +0000 Subject: [PATCH 2/3] Abilities API: Read and write manual UTC offsets as the `timezone` setting. A site on a manual UTC offset keeps its `timezone_string` option empty and stores the offset in `gmt_offset`, so `core/settings-get` returned an empty timezone and `core/settings-update` could not set an offset. The `timezone` setting of both abilities now accepts and returns the manual offsets that the timezone list offers, such as `UTC+2`, next to timezone identifiers. A site without a timezone string reads as its offset. Writing an offset stores it in `gmt_offset` and empties `timezone_string`, as Settings > General does; an offset the timezone list does not offer is refused, and a refused call writes neither option. See #64616. --- .../abilities/class-wp-abilities-settings.php | 32 ++- src/wp-includes/option.php | 7 +- .../wpRegisterCoreSettingsGetAbility.php | 52 ++++ .../wpRegisterCoreSettingsUpdateAbility.php | 264 +++++++++++++++++- 4 files changed, 344 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-settings.php b/src/wp-includes/abilities/class-wp-abilities-settings.php index c0f3df7ce5500..61326f356efc8 100644 --- a/src/wp-includes/abilities/class-wp-abilities-settings.php +++ b/src/wp-includes/abilities/class-wp-abilities-settings.php @@ -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 ); /* @@ -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 ); @@ -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; @@ -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; } diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index 71c97b5342105..2bcd41f7919eb 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -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.' ), ) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php index f66fe6584a5ef..c5ef1d52df925 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php @@ -586,4 +586,56 @@ 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. + * + * @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 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' ), + ); + } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsUpdateAbility.php index d40b748864418..bd8ffaa00b8ae 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsUpdateAbility.php @@ -239,18 +239,20 @@ private function update( array $data ) { } /** - * Returns the stored rows of every registered setting, read from the database. + * Returns the stored rows of every registered setting and of the UTC offset, read from the database. * - * An option without a row is absent from the result. + * An option without a row is absent from the result. The UTC offset is not a registered setting, + * but writing the timezone can change it. * * @return array Raw option values keyed by option name. */ private function snapshot(): array { global $wpdb; - $options = array_map( 'strval', array_keys( get_registered_settings() ) ); - $in = implode( ', ', array_fill( 0, count( $options ), '%s' ) ); - $rows = $wpdb->get_results( + $options = array_map( 'strval', array_keys( get_registered_settings() ) ); + $options[] = 'gmt_offset'; + $in = implode( ', ', array_fill( 0, count( $options ), '%s' ) ); + $rows = $wpdb->get_results( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- The placeholders are built above. $wpdb->prepare( "SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name IN ( {$in} )", $options ), ARRAY_A @@ -1120,7 +1122,8 @@ public function test_updates_each_setting( string $name, string $option, $value, /** * A null value stores the default of each setting that has one, and deletes the stored value - * of the others, which are then left out. + * of the others, which are then left out. Without a timezone string, the timezone reads as the + * UTC offset of the site. * * @ticket 64616 * @@ -1175,7 +1178,7 @@ public static function data_settings(): array { return array( 'title' => array( 'title', 'blogname', 'Acme Robotics', '"Acme Robotics"', 'Acme Robotics', null ), 'description' => array( 'description', 'blogdescription', 'Robots for everyone', '"Robots for everyone"', 'Robots for everyone', null ), - 'timezone' => array( 'timezone', 'timezone_string', 'Europe/Lisbon', '"Europe\/Lisbon"', 'Europe/Lisbon', null ), + 'timezone' => array( 'timezone', 'timezone_string', 'Europe/Lisbon', '"Europe\/Lisbon"', 'Europe/Lisbon', '"UTC+0"' ), 'date_format' => array( 'date_format', 'date_format', 'Y-m-d', '"Y-m-d"', 'Y-m-d', null ), 'time_format' => array( 'time_format', 'time_format', 'H:i', '"H:i"', 'H:i', null ), 'start_of_week' => array( 'start_of_week', 'start_of_week', 3, '3', '3', null ), @@ -1395,7 +1398,6 @@ public function test_refuses_a_value_the_setting_cannot_use( string $name, $valu public static function data_unusable_values(): array { return array( 'an unknown timezone' => array( 'timezone', 'Mars/Olympus' ), - 'a UTC offset as the timezone' => array( 'timezone', 'UTC+2' ), 'a language that is not installed' => array( 'language', 'xx_XX' ), 'an unknown post format' => array( 'default_post_format', 'banana' ), 'a front page that does not exist' => array( 'page_on_front', 999999 ), @@ -1640,7 +1642,6 @@ public function test_accepts_a_value_the_setting_can_use( string $name, string $ */ public static function data_usable_values(): array { return array( - 'no timezone' => array( 'timezone', 'timezone_string', 'Europe/Lisbon', '' ), 'UTC as the timezone' => array( 'timezone', 'timezone_string', 'Europe/Lisbon', 'UTC' ), 'a timezone under an old name' => array( 'timezone', 'timezone_string', 'Europe/Lisbon', 'US/Pacific' ), 'no default post format' => array( 'default_post_format', 'default_post_format', 'aside', '0' ), @@ -1652,6 +1653,251 @@ public static function data_usable_values(): array { ); } + /** + * A manual UTC offset is stored as the offset of the site, and the timezone string is emptied. + * + * @since 7.3.0 + * + * @dataProvider data_utc_offsets + * + * @param string $value The UTC offset to write. + * @param string $gmt_offset The expected raw stored offset. + * @param string $timezone The expected timezone of the site, as wp_timezone_string() returns it. + */ + public function test_utc_offset_is_stored_as_the_offset_of_the_site( string $value, string $gmt_offset, string $timezone ): void { + $this->store_raw( 'timezone_string', 'Europe/Lisbon' ); + $this->store_raw( 'gmt_offset', '7' ); + $this->become_admin(); + $expected = $this->snapshot(); + + $result = $this->update( array( 'timezone' => $value ) ); + + $this->assertSame( array( 'timezone' => $value ), $result, 'The output should show the offset.' ); + + $expected['timezone_string'] = ''; + $expected['gmt_offset'] = $gmt_offset; + $this->assertSameSetsWithIndex( $expected, $this->snapshot(), 'Only the timezone string and the offset should change.' ); + $this->assertSame( $timezone, wp_timezone_string(), 'The site should run on the offset.' ); + + wp_cache_delete( 'alloptions', 'options' ); + $read = wp_get_ability( 'core/settings-get' )->execute( array( 'fields' => array( 'timezone' ) ) ); + $this->assertSame( array( 'timezone' => $value ), $read, 'The offset should be read back the same way from the database.' ); + } + + /** + * Data provider. + * + * @return array UTC offsets, the raw stored offsets and the timezones of the site. + */ + public static function data_utc_offsets(): array { + return array( + 'a positive offset' => array( 'UTC+2', '2', '+02:00' ), + 'a negative offset' => array( 'UTC-5.5', '-5.5', '-05:30' ), + 'an offset of 45 minutes' => array( 'UTC+5.75', '5.75', '+05:45' ), + 'a negative half-hour offset' => array( 'UTC-0.5', '-0.5', '-00:30' ), + 'no offset' => array( 'UTC+0', '0', '+00:00' ), + 'the lowest offset' => array( 'UTC-12', '-12', '-12:00' ), + 'the highest offset' => array( 'UTC+14', '14', '+14:00' ), + ); + } + + /** + * A timezone identifier, `UTC` included, is stored as the timezone string, which then sets the + * offset of the site. The stored offset is left as it is. + * + * @since 7.3.0 + * + * @dataProvider data_timezone_identifiers + * + * @param string $value The timezone identifier to write. + */ + public function test_timezone_identifier_replaces_a_utc_offset( string $value ): void { + $this->store_raw( 'timezone_string', '' ); + $this->store_raw( 'gmt_offset', '2' ); + $this->become_admin(); + $expected = $this->snapshot(); + + $result = $this->update( array( 'timezone' => $value ) ); + + $this->assertSame( array( 'timezone' => $value ), $result, 'The output should show the timezone identifier.' ); + + $expected['timezone_string'] = $value; + $this->assertSameSetsWithIndex( $expected, $this->snapshot(), 'Only the timezone string should change.' ); + $this->assertSame( $value, wp_timezone_string(), 'The site should run on the timezone identifier.' ); + } + + /** + * Data provider. + * + * @return array Timezone identifiers. + */ + public static function data_timezone_identifiers(): array { + return array( + 'a city' => array( 'Europe/Lisbon' ), + 'UTC' => array( 'UTC' ), + ); + } + + /** + * An empty timezone empties the timezone string, and the output shows the stored UTC offset the + * site then runs on. + * + * @since 7.3.0 + */ + public function test_empty_timezone_reads_as_the_stored_utc_offset(): void { + $this->store_raw( 'timezone_string', 'Europe/Lisbon' ); + $this->store_raw( 'gmt_offset', '3' ); + $this->become_admin(); + $expected = $this->snapshot(); + + $result = $this->update( array( 'timezone' => '' ) ); + + $this->assertSame( array( 'timezone' => 'UTC+3' ), $result, 'The output should show the stored offset.' ); + + $expected['timezone_string'] = ''; + $this->assertSameSetsWithIndex( $expected, $this->snapshot(), 'Only the timezone string should change.' ); + } + + /** + * A UTC offset is stored when the site has no stored offset. + * + * @since 7.3.0 + */ + public function test_utc_offset_is_stored_without_a_stored_offset(): void { + $this->store_raw( 'timezone_string', 'Europe/Lisbon' ); + $this->store_raw( 'gmt_offset', null ); + $this->become_admin(); + $expected = $this->snapshot(); + + $result = $this->update( array( 'timezone' => 'UTC+3' ) ); + + $this->assertSame( array( 'timezone' => 'UTC+3' ), $result, 'The output should show the offset.' ); + + $expected['timezone_string'] = ''; + $expected['gmt_offset'] = '3'; + $this->assertSameSetsWithIndex( $expected, $this->snapshot(), 'The offset should be added.' ); + } + + /** + * A UTC offset that the timezone list does not offer is refused, and nothing is written. + * + * @since 7.3.0 + * + * @dataProvider data_utc_offsets_not_offered + * + * @param string $value The UTC offset to write. + */ + public function test_refuses_a_utc_offset_the_timezone_list_does_not_offer( string $value ): void { + $this->store_raw( 'timezone_string', 'Europe/Lisbon' ); + $this->become_admin(); + $before = $this->snapshot(); + + $result = $this->update( array( 'timezone' => $value ) ); + + $this->assert_invalid_value_error( $result, 'timezone' ); + $this->assertSameSetsWithIndex( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Data provider. + * + * @return array UTC offsets that the timezone list does not offer. + */ + public static function data_utc_offsets_not_offered(): array { + return array( + 'a trailing zero' => array( 'UTC+2.0' ), + 'a leading zero' => array( 'UTC+02' ), + 'above the highest' => array( 'UTC+15' ), + 'below the lowest' => array( 'UTC-12.5' ), + 'a negative zero' => array( 'UTC-0' ), + 'a quarter of an hour' => array( 'UTC+3.25' ), + 'hours and minutes' => array( 'UTC+5:30' ), + 'no number' => array( 'UTC+' ), + 'not a number' => array( 'UTC+two' ), + 'lowercase' => array( 'utc+2' ), + 'a trailing space' => array( 'UTC+2 ' ), + 'GMT instead of UTC' => array( 'GMT+2' ), + ); + } + + /** + * Neither the timezone string nor the offset is written when the call is refused. + * + * @since 7.3.0 + * + * @dataProvider data_refused_calls_with_a_utc_offset + * + * @param array $data The settings to update. + * @param string $code The expected error code. + */ + public function test_utc_offset_is_not_written_when_the_call_is_refused( array $data, string $code ): void { + $this->store_raw( 'timezone_string', 'Europe/Lisbon' ); + $this->store_raw( 'gmt_offset', '0' ); + $this->store_raw( 'start_of_week', 'Monday' ); + $this->become_admin(); + $before = $this->snapshot(); + + $result = $this->update( $data ); + + $this->assert_error_code( $result, $code ); + $this->assertSameSetsWithIndex( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Data provider. + * + * @return array, 1: string}> Refused calls that set a UTC offset, and their error codes. + */ + public static function data_refused_calls_with_a_utc_offset(): array { + return array( + 'an invalid input' => array( + array( + 'timezone' => 'UTC+2', + 'posts_per_page' => 'many', + ), + 'ability_invalid_input', + ), + 'a value the setting cannot use' => array( + array( + 'timezone' => 'UTC+2', + 'language' => 'xx_XX', + ), + 'settings_invalid_value', + ), + 'an invalid stored value' => array( + array( + 'timezone' => 'UTC+2', + 'start_of_week' => null, + ), + 'settings_invalid_stored_value', + ), + ); + } + + /** + * The offset is written before the timezone string is emptied, so a call stopped between the + * two writes leaves the site on its timezone string. + * + * @since 7.3.0 + */ + public function test_call_stopped_after_the_utc_offset_leaves_the_timezone_of_the_site(): void { + $this->store_raw( 'timezone_string', 'Europe/Lisbon' ); + $this->store_raw( 'gmt_offset', '0' ); + $this->become_admin(); + add_action( + 'update_option_gmt_offset', + static function (): void { + throw new RuntimeException( 'Stopped after the offset.' ); + } + ); + + $result = $this->update( array( 'timezone' => 'UTC+2' ) ); + + $this->assert_error_code( $result, 'ability_callback_exception' ); + $this->assertSame( 'Europe/Lisbon', $this->snapshot()['timezone_string'], 'The timezone string should be kept.' ); + $this->assertSame( 'Europe/Lisbon', wp_timezone_string(), 'The site should still run on its timezone string.' ); + } + /** * The language that core/settings-get reads when none is stored, `en_US`, can be written back, * and is saved as English. From 7ad2c233b21b251194e5f220dbf32e12db177bb8 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Wed, 2 Dec 2026 12:43:10 +0000 Subject: [PATCH 3/3] Tests: Add the ticket of the manual UTC offsets to their tests. Follow-up to [64616]. See #66902. --- .../wpRegisterCoreSettingsGetAbility.php | 2 ++ .../wpRegisterCoreSettingsUpdateAbility.php | 15 +++++++++++++++ .../tests/functions/wpGetTimezoneOffsetChoice.php | 2 ++ .../functions/wpGetTimezoneOffsetChoices.php | 6 ++++++ .../functions/wpParseTimezoneOffsetChoice.php | 4 ++++ 5 files changed, 29 insertions(+) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php index c5ef1d52df925..be5d80043699c 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsGetAbility.php @@ -591,6 +591,8 @@ public function test_core_settings_get_skips_a_setting_with_an_unsupported_type( * 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 diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsUpdateAbility.php index bd8ffaa00b8ae..cd25a4959f3d3 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsUpdateAbility.php @@ -1126,6 +1126,7 @@ public function test_updates_each_setting( string $name, string $option, $value, * UTC offset of the site. * * @ticket 64616 + * @ticket 66902 * * @since 7.3.0 * @@ -1656,6 +1657,8 @@ public static function data_usable_values(): array { /** * A manual UTC offset is stored as the offset of the site, and the timezone string is emptied. * + * @ticket 66902 + * * @since 7.3.0 * * @dataProvider data_utc_offsets @@ -1705,6 +1708,8 @@ public static function data_utc_offsets(): array { * A timezone identifier, `UTC` included, is stored as the timezone string, which then sets the * offset of the site. The stored offset is left as it is. * + * @ticket 66902 + * * @since 7.3.0 * * @dataProvider data_timezone_identifiers @@ -1742,6 +1747,8 @@ public static function data_timezone_identifiers(): array { * An empty timezone empties the timezone string, and the output shows the stored UTC offset the * site then runs on. * + * @ticket 66902 + * * @since 7.3.0 */ public function test_empty_timezone_reads_as_the_stored_utc_offset(): void { @@ -1761,6 +1768,8 @@ public function test_empty_timezone_reads_as_the_stored_utc_offset(): void { /** * A UTC offset is stored when the site has no stored offset. * + * @ticket 66902 + * * @since 7.3.0 */ public function test_utc_offset_is_stored_without_a_stored_offset(): void { @@ -1781,6 +1790,8 @@ public function test_utc_offset_is_stored_without_a_stored_offset(): void { /** * A UTC offset that the timezone list does not offer is refused, and nothing is written. * + * @ticket 66902 + * * @since 7.3.0 * * @dataProvider data_utc_offsets_not_offered @@ -1823,6 +1834,8 @@ public static function data_utc_offsets_not_offered(): array { /** * Neither the timezone string nor the offset is written when the call is refused. * + * @ticket 66902 + * * @since 7.3.0 * * @dataProvider data_refused_calls_with_a_utc_offset @@ -1878,6 +1891,8 @@ public static function data_refused_calls_with_a_utc_offset(): array { * The offset is written before the timezone string is emptied, so a call stopped between the * two writes leaves the site on its timezone string. * + * @ticket 66902 + * * @since 7.3.0 */ public function test_call_stopped_after_the_utc_offset_leaves_the_timezone_of_the_site(): void { diff --git a/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoice.php b/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoice.php index 1818712dc2be0..632b356a64f6d 100644 --- a/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoice.php +++ b/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoice.php @@ -13,6 +13,8 @@ class Tests_Functions_WpGetTimezoneOffsetChoice extends WP_UnitTestCase { /** * Returns the value of the timezone list for a UTC offset. * + * @ticket 66902 + * * @since 7.3.0 * * @dataProvider data_offsets diff --git a/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoices.php b/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoices.php index c7f1c6057cc4e..ee4a75d228e57 100644 --- a/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoices.php +++ b/tests/phpunit/tests/functions/wpGetTimezoneOffsetChoices.php @@ -13,6 +13,8 @@ class Tests_Functions_WpGetTimezoneOffsetChoices extends WP_UnitTestCase { /** * The manual UTC offsets run from UTC-12 to UTC+14. * + * @ticket 66902 + * * @since 7.3.0 */ public function test_returns_the_manual_utc_offsets(): void { @@ -28,6 +30,8 @@ public function test_returns_the_manual_utc_offsets(): void { /** * Each manual UTC offset is offered by the timezone list. * + * @ticket 66902 + * * @since 7.3.0 */ public function test_each_offset_is_offered_by_the_timezone_list(): void { @@ -41,6 +45,8 @@ public function test_each_offset_is_offered_by_the_timezone_list(): void { /** * Each manual UTC offset is parsed into the offset that builds it again. * + * @ticket 66902 + * * @since 7.3.0 */ public function test_each_offset_is_parsed_into_the_offset_that_builds_it(): void { diff --git a/tests/phpunit/tests/functions/wpParseTimezoneOffsetChoice.php b/tests/phpunit/tests/functions/wpParseTimezoneOffsetChoice.php index 9a2ee48fd8b37..5809f221ba18b 100644 --- a/tests/phpunit/tests/functions/wpParseTimezoneOffsetChoice.php +++ b/tests/phpunit/tests/functions/wpParseTimezoneOffsetChoice.php @@ -13,6 +13,8 @@ class Tests_Functions_WpParseTimezoneOffsetChoice extends WP_UnitTestCase { /** * Returns the UTC offset of a manual offset, as Settings > General stores it. * + * @ticket 66902 + * * @since 7.3.0 * * @dataProvider data_manual_offsets @@ -43,6 +45,8 @@ public static function data_manual_offsets(): array { /** * Returns null for a value that is not a manual offset. * + * @ticket 66902 + * * @since 7.3.0 * * @dataProvider data_other_values