| 1 |
<?php |
| 2 |
/** |
| 3 |
* Timezone handling |
| 4 |
* |
| 5 |
* Handles all timezone operations and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace POSessions\System; |
| 13 |
|
| 14 |
use POSessions\System\Environment; |
| 15 |
|
| 16 |
/** |
| 17 |
* Define the timezone functionality. |
| 18 |
* |
| 19 |
* Handles all timezone operations and detection. |
| 20 |
* |
| 21 |
* @package System |
| 22 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Timezone extends \DateTimeZone { |
| 26 |
|
| 27 |
/** |
| 28 |
* Determine time zone from WordPress options and return as object. |
| 29 |
* Inspired by https://github.com/Rarst/wpdatetime repository. |
| 30 |
* |
| 31 |
* @param string $timezone_string The timezone identifier. |
| 32 |
* @param string $offset Optional. The offset of the timezone. |
| 33 |
* @return static |
| 34 |
*/ |
| 35 |
private static function get( $timezone_string, $offset = '0' ) { |
| 36 |
if ( ! empty( $timezone_string ) ) { |
| 37 |
return new static( $timezone_string ); |
| 38 |
} |
| 39 |
$sign = $offset < 0 ? '-' : '+'; |
| 40 |
$hours = (int) $offset; |
| 41 |
$minutes = abs( ( $offset - (int) $offset ) * 60 ); |
| 42 |
$offset = sprintf( '%s%02d:%02d', $sign, abs( $hours ), $minutes ); |
| 43 |
return new static( $offset ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get the timezone for the current site |
| 48 |
* |
| 49 |
* @return static |
| 50 |
*/ |
| 51 |
public static function site_get() { |
| 52 |
return self::get( get_option( 'timezone_string' ), get_option( 'gmt_offset' ) ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the timezone for a specific site |
| 57 |
* |
| 58 |
* @param int $id Optional. The blog id. |
| 59 |
* @return static |
| 60 |
*/ |
| 61 |
public static function site_get_for( $id = 1 ) { |
| 62 |
if ( Environment::is_wordpress_multisite() ) { |
| 63 |
return self::get( get_blog_option( $id, 'timezone_string' ), get_blog_option( $id, 'gmt_offset' ) ); |
| 64 |
} |
| 65 |
return self::site_get(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get the timezone for the network |
| 70 |
* |
| 71 |
* @return static |
| 72 |
*/ |
| 73 |
public static function network_get() { |
| 74 |
if ( Environment::is_wordpress_multisite() ) { |
| 75 |
return self::site_get_for( 1 ); |
| 76 |
} |
| 77 |
return self::site_get(); |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|