Passed
Pull Request — master (#280)
by Brian
05:07
created

get_local_timezone()   C

Complexity

Conditions 13
Paths 30

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 27
c 1
b 0
f 0
nc 30
nop 1
dl 0
loc 49
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Class ActionScheduler_TimezoneHelper
5
 */
6
abstract class ActionScheduler_TimezoneHelper {
7
	private static $local_timezone = NULL;
8
9
	/**
10
	 * Set a DateTime's timezone to the WordPress site's timezone, or a UTC offset
11
	 * if no timezone string is available.
12
	 *
13
	 * @since  2.1.0
14
	 *
15
	 * @param DateTime $date
16
	 * @return ActionScheduler_DateTime
17
	 */
18
	public static function set_local_timezone( DateTime $date ) {
19
20
		// Accept a DateTime for easier backward compatibility, even though we require methods on ActionScheduler_DateTime
21
		if ( ! is_a( $date, 'ActionScheduler_DateTime' ) ) {
22
			$date = as_get_datetime_object( $date->format( 'U' ) );
23
		}
24
25
		if ( get_option( 'timezone_string' ) ) {
26
			$date->setTimezone( new DateTimeZone( self::get_local_timezone_string() ) );
27
		} else {
28
			$date->setUtcOffset( self::get_local_timezone_offset() );
29
		}
30
31
		return $date;
32
	}
33
34
	/**
35
	 * Helper to retrieve the timezone string for a site until a WP core method exists
36
	 * (see https://core.trac.wordpress.org/ticket/24730).
37
	 *
38
	 * Adapted from wc_timezone_string() and https://secure.php.net/manual/en/function.timezone-name-from-abbr.php#89155.
39
	 *
40
	 * If no timezone string is set, and its not possible to match the UTC offset set for the site to a timezone
41
	 * string, then an empty string will be returned, and the UTC offset should be used to set a DateTime's
42
	 * timezone.
43
	 *
44
	 * @since 2.1.0
45
	 * @return string PHP timezone string for the site or empty if no timezone string is available.
46
	 */
47
	protected static function get_local_timezone_string( $reset = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $reset is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
	protected static function get_local_timezone_string( /** @scrutinizer ignore-unused */ $reset = false ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
		// If site timezone string exists, return it.
49
		$timezone = get_option( 'timezone_string' );
50
		if ( $timezone ) {
51
			return $timezone;
52
		}
53
54
		// Get UTC offset, if it isn't set then return UTC.
55
		$utc_offset = intval( get_option( 'gmt_offset', 0 ) );
56
		if ( 0 === $utc_offset ) {
57
			return 'UTC';
58
		}
59
60
		// Adjust UTC offset from hours to seconds.
61
		$utc_offset *= 3600;
62
63
		// Attempt to guess the timezone string from the UTC offset.
64
		$timezone = timezone_name_from_abbr( '', $utc_offset );
65
		if ( $timezone ) {
66
			return $timezone;
67
		}
68
69
		// Last try, guess timezone string manually.
70
		foreach ( timezone_abbreviations_list() as $abbr ) {
71
			foreach ( $abbr as $city ) {
72
				if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) {
73
					return $city['timezone_id'];
74
				}
75
			}
76
		}
77
78
		// No timezone string
79
		return '';
80
	}
81
82
	/**
83
	 * Get timezone offset in seconds.
84
	 *
85
	 * @since  2.1.0
86
	 * @return float
87
	 */
88
	protected static function get_local_timezone_offset() {
89
		$timezone = get_option( 'timezone_string' );
90
91
		if ( $timezone ) {
92
			$timezone_object = new DateTimeZone( $timezone );
93
			return $timezone_object->getOffset( new DateTime( 'now' ) );
94
		} else {
95
			return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS;
96
		}
97
	}
98
99
	/**
100
	 * @deprecated 2.1.0
101
	 */
102
	public static function get_local_timezone( $reset = FALSE ) {
103
		_deprecated_function( __FUNCTION__, '2.1.0', 'ActionScheduler_TimezoneHelper::set_local_timezone()' );
104
		if ( $reset ) {
105
			self::$local_timezone = NULL;
106
		}
107
		if ( !isset(self::$local_timezone) ) {
108
			$tzstring = get_option('timezone_string');
109
110
			if ( empty($tzstring) ) {
111
				$gmt_offset = get_option('gmt_offset');
112
				if ( $gmt_offset == 0 ) {
113
					$tzstring = 'UTC';
114
				} else {
115
					$gmt_offset *= HOUR_IN_SECONDS;
116
					$tzstring   = timezone_name_from_abbr( '', $gmt_offset, 1 );
0 ignored issues
show
Bug introduced by
It seems like $gmt_offset can also be of type false; however, parameter $gmtOffset of timezone_name_from_abbr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
					$tzstring   = timezone_name_from_abbr( '', /** @scrutinizer ignore-type */ $gmt_offset, 1 );
Loading history...
117
118
					// If there's no timezone string, try again with no DST.
119
					if ( false === $tzstring ) {
120
						$tzstring = timezone_name_from_abbr( '', $gmt_offset, 0 );
121
					}
122
123
					// Try mapping to the first abbreviation we can find.
124
					if ( false === $tzstring ) {
125
						$is_dst = date( 'I' );
126
						foreach ( timezone_abbreviations_list() as $abbr ) {
127
							foreach ( $abbr as $city ) {
128
								if ( $city['dst'] == $is_dst && $city['offset'] == $gmt_offset ) {
129
									// If there's no valid timezone ID, keep looking.
130
									if ( null === $city['timezone_id'] ) {
131
										continue;
132
									}
133
134
									$tzstring = $city['timezone_id'];
135
									break 2;
136
								}
137
							}
138
						}
139
					}
140
141
					// If we still have no valid string, then fall back to UTC.
142
					if ( false === $tzstring ) {
143
						$tzstring = 'UTC';
144
					}
145
				}
146
			}
147
148
			self::$local_timezone = new DateTimeZone($tzstring);
149
		}
150
		return self::$local_timezone;
151
	}
152
}
153