PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / system / class-timezone.php

class-timezone.php in DecaLog 4.4.0, 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 Decalog\System;
13
14 use Decalog\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