diff --git a/src/wp-admin/options-general.php b/src/wp-admin/options-general.php index 9515f71d01551..dbc5b5f358911 100644 --- a/src/wp-admin/options-general.php +++ b/src/wp-admin/options-general.php @@ -354,12 +354,14 @@ class="" if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists. $check_zone_info = false; - if ( 0 === (int) $current_offset ) { + if ( 0 === $current_offset || '0' === $current_offset ) { $tzstring = 'UTC+0'; - } elseif ( $current_offset < 0 ) { + } elseif ( ( is_numeric( $current_offset ) && (float) $current_offset < 0 ) ) { $tzstring = 'UTC' . $current_offset; - } else { + } elseif ( is_numeric( $current_offset ) ) { $tzstring = 'UTC+' . $current_offset; + } else { + $tzstring = 'UTC+0'; } } diff --git a/tests/phpunit/tests/option/gmtOffset.php b/tests/phpunit/tests/option/gmtOffset.php new file mode 100644 index 0000000000000..4fb7eb5baba12 --- /dev/null +++ b/tests/phpunit/tests/option/gmtOffset.php @@ -0,0 +1,91 @@ +assertSame( 0, $current_offset ); + + // Simulate the code logic + if ( 0 === $current_offset ) { + $tzstring = 'UTC+0'; + } + + $this->assertEquals( 'UTC+0', $tzstring ); + } + + /** + * Test gmt_offset comparison with string '0'. + * + * @ticket 57030 + */ + public function test_gmt_offset_with_string_zero() { + update_option( 'gmt_offset', '0' ); + $current_offset = get_option( 'gmt_offset' ); + $this->assertSame( '0', $current_offset ); + + // Simulate the code logic + if ( '0' === $current_offset ) { + $tzstring = 'UTC+0'; + } + + $this->assertEquals( 'UTC+0', $tzstring ); + } + + /** + * Test gmt_offset comparison with negative float. + * + * @ticket 57030 + */ + public function test_gmt_offset_with_negative_float() { + update_option( 'gmt_offset', -5.5 ); + $current_offset = get_option( 'gmt_offset' ); + $this->assertSame( -5.5, (float) $current_offset ); + + // Simulate the code logic + if ( $current_offset < 0 ) { + $tzstring = 'UTC' . $current_offset; + } + + $this->assertEquals( 'UTC-5.5', $tzstring ); + } + + /** + * Test gmt_offset comparison with invalid value. + * + * @ticket 57030 + */ + public function test_gmt_offset_with_invalid_value() { + update_option( 'gmt_offset', 'invalid' ); + $current_offset = get_option( 'gmt_offset' ); + + // Simulate the code logic + if ( 0 === (int) $current_offset ) { + $tzstring = 'UTC+0'; + } elseif ( is_numeric( $current_offset ) && (float) $current_offset < 0 ) { + $tzstring = 'UTC' . $current_offset; + } elseif ( is_numeric( $current_offset ) ) { + $tzstring = 'UTC+' . $current_offset; + } else { + // Handle unexpected types gracefully. + $tzstring = 'UTC+0'; + } + + $this->assertEquals( 'UTC+0', $tzstring ); + } +}