PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-timezone.php

class-timezone.php in Sessions 2.3.1, at includes/system/class-timezone.php

81 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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