diff --git a/includes/wc-formatting-functions.php b/includes/wc-formatting-functions.php index 967bb207f343c..cc5d0f6872d6e 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -713,6 +713,11 @@ function wc_string_to_datetime( $time_string ) { * @return string PHP timezone string for the site */ function wc_timezone_string() { + // Added in WordPress 5.3 Ref https://developer.wordpress.org/reference/functions/wp_timezone_string/. + if ( function_exists( 'wp_timezone_string' ) ) { + return wp_timezone_string(); + } + // If site timezone string exists, return it. $timezone = get_option( 'timezone_string' ); if ( $timezone ) { @@ -720,13 +725,13 @@ function wc_timezone_string() { } // Get UTC offset, if it isn't set then return UTC. - $utc_offset = intval( get_option( 'gmt_offset', 0 ) ); - if ( 0 === $utc_offset ) { + $utc_offset = floatval( get_option( 'gmt_offset', 0 ) ); + if ( ! is_numeric( $utc_offset ) || 0.0 === $utc_offset ) { return 'UTC'; } // Adjust UTC offset from hours to seconds. - $utc_offset *= 3600; + $utc_offset = (int) ( $utc_offset * 3600 ); // Attempt to guess the timezone string from the UTC offset. $timezone = timezone_name_from_abbr( '', $utc_offset ); diff --git a/tests/legacy/unit-tests/formatting/functions.php b/tests/legacy/unit-tests/formatting/functions.php index a2e517dfea45a..a88a086ee02f9 100644 --- a/tests/legacy/unit-tests/formatting/functions.php +++ b/tests/legacy/unit-tests/formatting/functions.php @@ -699,15 +699,15 @@ public function test_wc_timezone_string() { // Test with missing UTC offset. delete_option( 'gmt_offset' ); - $this->assertEquals( 'UTC', wc_timezone_string() ); + $this->assertEquals( '+00:00', wc_timezone_string() ); // Test with manually set UTC offset. update_option( 'gmt_offset', -4 ); $this->assertNotEquals( 'UTC', wc_timezone_string() ); // Test with invalid offset. - update_option( 'gmt_offset', 99 ); - $this->assertEquals( 'UTC', wc_timezone_string() ); + update_option( 'gmt_offset', 'invalid' ); + $this->assertEquals( '+00:00', wc_timezone_string() ); // Restore default. update_option( 'gmt_offset', '0' );