@@ -17,760 +17,760 @@ |
||
17 | 17 | class EE_Datetime_Field extends EE_Model_Field_Base |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
22 | - * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
23 | - * |
|
24 | - * @type string unix_timestamp_regex |
|
25 | - */ |
|
26 | - const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
27 | - |
|
28 | - /** |
|
29 | - * @type string mysql_timestamp_format |
|
30 | - */ |
|
31 | - const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
32 | - |
|
33 | - /** |
|
34 | - * @type string mysql_date_format |
|
35 | - */ |
|
36 | - const mysql_date_format = 'Y-m-d'; |
|
37 | - |
|
38 | - /** |
|
39 | - * @type string mysql_time_format |
|
40 | - */ |
|
41 | - const mysql_time_format = 'H:i:s'; |
|
42 | - |
|
43 | - /** |
|
44 | - * Const for using in the default value. If the field's default is set to this, |
|
45 | - * then we will return the time of calling `get_default_value()`, not |
|
46 | - * just the current time at construction |
|
47 | - */ |
|
48 | - const now = 'now'; |
|
49 | - |
|
50 | - /** |
|
51 | - * The following properties hold the default formats for date and time. |
|
52 | - * Defaults are set via the constructor and can be overridden on class instantiation. |
|
53 | - * However they can also be overridden later by the set_format() method |
|
54 | - * (and corresponding set_date_format, set_time_format methods); |
|
55 | - */ |
|
56 | - /** |
|
57 | - * @type string $_date_format |
|
58 | - */ |
|
59 | - protected $_date_format = ''; |
|
60 | - |
|
61 | - /** |
|
62 | - * @type string $_time_format |
|
63 | - */ |
|
64 | - protected $_time_format = ''; |
|
65 | - |
|
66 | - /** |
|
67 | - * @type string $_pretty_date_format |
|
68 | - */ |
|
69 | - protected $_pretty_date_format = ''; |
|
70 | - |
|
71 | - /** |
|
72 | - * @type string $_pretty_time_format |
|
73 | - */ |
|
74 | - protected $_pretty_time_format = ''; |
|
75 | - |
|
76 | - /** |
|
77 | - * @type DateTimeZone $_DateTimeZone |
|
78 | - */ |
|
79 | - protected $_DateTimeZone; |
|
80 | - |
|
81 | - /** |
|
82 | - * @type DateTimeZone $_UTC_DateTimeZone |
|
83 | - */ |
|
84 | - protected $_UTC_DateTimeZone; |
|
85 | - |
|
86 | - /** |
|
87 | - * @type DateTimeZone $_blog_DateTimeZone |
|
88 | - */ |
|
89 | - protected $_blog_DateTimeZone; |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
94 | - * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
95 | - * and time returned via getters. |
|
96 | - * |
|
97 | - * @var mixed (null|string) |
|
98 | - */ |
|
99 | - protected $_date_time_output; |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * timezone string |
|
104 | - * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
105 | - * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
106 | - * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
107 | - * |
|
108 | - * @var string |
|
109 | - */ |
|
110 | - protected $_timezone_string; |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
115 | - * offsets for comparison purposes). |
|
116 | - * |
|
117 | - * @var int |
|
118 | - */ |
|
119 | - protected $_blog_offset; |
|
120 | - |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * @param string $table_column |
|
125 | - * @param string $nice_name |
|
126 | - * @param bool $nullable |
|
127 | - * @param string $default_value |
|
128 | - * @param string $timezone_string |
|
129 | - * @param string $date_format |
|
130 | - * @param string $time_format |
|
131 | - * @param string $pretty_date_format |
|
132 | - * @param string $pretty_time_format |
|
133 | - * @throws EE_Error |
|
134 | - * @throws InvalidArgumentException |
|
135 | - */ |
|
136 | - public function __construct( |
|
137 | - $table_column, |
|
138 | - $nice_name, |
|
139 | - $nullable, |
|
140 | - $default_value, |
|
141 | - $timezone_string = '', |
|
142 | - $date_format = '', |
|
143 | - $time_format = '', |
|
144 | - $pretty_date_format = '', |
|
145 | - $pretty_time_format = '' |
|
146 | - ) { |
|
147 | - |
|
148 | - $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
149 | - $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
150 | - $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
|
151 | - $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
|
152 | - |
|
153 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
154 | - $this->set_timezone($timezone_string); |
|
155 | - $this->setSchemaFormat('date-time'); |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * @return DateTimeZone |
|
161 | - * @throws \EE_Error |
|
162 | - */ |
|
163 | - public function get_UTC_DateTimeZone() |
|
164 | - { |
|
165 | - return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
166 | - ? $this->_UTC_DateTimeZone |
|
167 | - : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * @return DateTimeZone |
|
173 | - * @throws \EE_Error |
|
174 | - */ |
|
175 | - public function get_blog_DateTimeZone() |
|
176 | - { |
|
177 | - return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
178 | - ? $this->_blog_DateTimeZone |
|
179 | - : $this->_create_timezone_object_from_timezone_string(''); |
|
180 | - } |
|
181 | - |
|
182 | - |
|
183 | - /** |
|
184 | - * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
185 | - * |
|
186 | - * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
187 | - * timestamp |
|
188 | - * @return DateTime |
|
189 | - */ |
|
190 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
191 | - { |
|
192 | - return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
193 | - } |
|
194 | - |
|
195 | - |
|
196 | - /** |
|
197 | - * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
198 | - * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
199 | - * |
|
200 | - * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
201 | - * @return string The final assembled format string. |
|
202 | - */ |
|
203 | - protected function _get_date_time_output($pretty = false) |
|
204 | - { |
|
205 | - |
|
206 | - switch ($this->_date_time_output) { |
|
207 | - case 'time' : |
|
208 | - return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
209 | - break; |
|
210 | - |
|
211 | - case 'date' : |
|
212 | - return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
213 | - break; |
|
214 | - |
|
215 | - default : |
|
216 | - return $pretty |
|
217 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
218 | - : $this->_date_format . ' ' . $this->_time_format; |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
225 | - * returned (using the format properties) |
|
226 | - * |
|
227 | - * @param string $what acceptable values are 'time' or 'date'. |
|
228 | - * Any other value will be set but will always result |
|
229 | - * in both 'date' and 'time' being returned. |
|
230 | - * @return void |
|
231 | - */ |
|
232 | - public function set_date_time_output($what = null) |
|
233 | - { |
|
234 | - $this->_date_time_output = $what; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
240 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
241 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
242 | - * We also set some other properties in this method. |
|
243 | - * |
|
244 | - * @param string $timezone_string A valid timezone string as described by @link |
|
245 | - * http://www.php.net/manual/en/timezones.php |
|
246 | - * @return void |
|
247 | - * @throws InvalidArgumentException |
|
248 | - * @throws InvalidDataTypeException |
|
249 | - * @throws InvalidInterfaceException |
|
250 | - */ |
|
251 | - public function set_timezone($timezone_string) |
|
252 | - { |
|
253 | - if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
254 | - // leave the timezone AS-IS if we already have one and |
|
255 | - // the function arg didn't provide one |
|
256 | - return; |
|
257 | - } |
|
258 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
259 | - $this->_timezone_string = ! empty($timezone_string) ? $timezone_string : 'UTC'; |
|
260 | - $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * _create_timezone_object_from_timezone_name |
|
266 | - * |
|
267 | - * @access protected |
|
268 | - * @param string $timezone_string |
|
269 | - * @return \DateTimeZone |
|
270 | - * @throws InvalidArgumentException |
|
271 | - * @throws InvalidDataTypeException |
|
272 | - * @throws InvalidInterfaceException |
|
273 | - */ |
|
274 | - protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
|
275 | - { |
|
276 | - return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
277 | - } |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * This just returns whatever is set for the current timezone. |
|
282 | - * |
|
283 | - * @access public |
|
284 | - * @return string timezone string |
|
285 | - */ |
|
286 | - public function get_timezone() |
|
287 | - { |
|
288 | - return $this->_timezone_string; |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * set the $_date_format property |
|
294 | - * |
|
295 | - * @access public |
|
296 | - * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
297 | - * @param bool $pretty Whether to set pretty format or not. |
|
298 | - * @return void |
|
299 | - */ |
|
300 | - public function set_date_format($format, $pretty = false) |
|
301 | - { |
|
302 | - if ($pretty) { |
|
303 | - $this->_pretty_date_format = $format; |
|
304 | - } else { |
|
305 | - $this->_date_format = $format; |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - |
|
310 | - /** |
|
311 | - * return the $_date_format property value. |
|
312 | - * |
|
313 | - * @param bool $pretty Whether to get pretty format or not. |
|
314 | - * @return string |
|
315 | - */ |
|
316 | - public function get_date_format($pretty = false) |
|
317 | - { |
|
318 | - return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * set the $_time_format property |
|
324 | - * |
|
325 | - * @access public |
|
326 | - * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
327 | - * @param bool $pretty Whether to set pretty format or not. |
|
328 | - * @return void |
|
329 | - */ |
|
330 | - public function set_time_format($format, $pretty = false) |
|
331 | - { |
|
332 | - if ($pretty) { |
|
333 | - $this->_pretty_time_format = $format; |
|
334 | - } else { |
|
335 | - $this->_time_format = $format; |
|
336 | - } |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * return the $_time_format property value. |
|
342 | - * |
|
343 | - * @param bool $pretty Whether to get pretty format or not. |
|
344 | - * @return string |
|
345 | - */ |
|
346 | - public function get_time_format($pretty = false) |
|
347 | - { |
|
348 | - return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * set the $_pretty_date_format property |
|
354 | - * |
|
355 | - * @access public |
|
356 | - * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
357 | - * @return void |
|
358 | - */ |
|
359 | - public function set_pretty_date_format($format) |
|
360 | - { |
|
361 | - $this->_pretty_date_format = $format; |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * set the $_pretty_time_format property |
|
367 | - * |
|
368 | - * @access public |
|
369 | - * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
370 | - * @return void |
|
371 | - */ |
|
372 | - public function set_pretty_time_format($format) |
|
373 | - { |
|
374 | - $this->_pretty_time_format = $format; |
|
375 | - } |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * Only sets the time portion of the datetime. |
|
380 | - * |
|
381 | - * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
382 | - * @param DateTime $current current DateTime object for the datetime field |
|
383 | - * @return DateTime |
|
384 | - */ |
|
385 | - public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
386 | - { |
|
387 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
388 | - // Otherwise parse the string. |
|
389 | - if ($time_to_set_string instanceof DateTime) { |
|
390 | - $parsed = array( |
|
391 | - 'hour' => $time_to_set_string->format('H'), |
|
392 | - 'minute' => $time_to_set_string->format('i'), |
|
393 | - 'second' => $time_to_set_string->format('s'), |
|
394 | - ); |
|
395 | - } else { |
|
396 | - //parse incoming string |
|
397 | - $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
398 | - } |
|
399 | - |
|
400 | - //make sure $current is in the correct timezone. |
|
401 | - $current->setTimezone($this->_DateTimeZone); |
|
402 | - |
|
403 | - return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
404 | - } |
|
405 | - |
|
406 | - |
|
407 | - /** |
|
408 | - * Only sets the date portion of the datetime. |
|
409 | - * |
|
410 | - * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
411 | - * @param DateTime $current current DateTime object for the datetime field |
|
412 | - * @return DateTime |
|
413 | - */ |
|
414 | - public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
415 | - { |
|
416 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
417 | - // Otherwise parse the string. |
|
418 | - if ($date_to_set_string instanceof DateTime) { |
|
419 | - $parsed = array( |
|
420 | - 'year' => $date_to_set_string->format('Y'), |
|
421 | - 'month' => $date_to_set_string->format('m'), |
|
422 | - 'day' => $date_to_set_string->format('d'), |
|
423 | - ); |
|
424 | - } else { |
|
425 | - //parse incoming string |
|
426 | - $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
427 | - } |
|
428 | - |
|
429 | - //make sure $current is in the correct timezone |
|
430 | - $current->setTimezone($this->_DateTimeZone); |
|
431 | - |
|
432 | - return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
438 | - * datetime gets to this stage it should ALREADY be in UTC time |
|
439 | - * |
|
440 | - * @param DateTime $DateTime |
|
441 | - * @return string formatted date time for given timezone |
|
442 | - * @throws \EE_Error |
|
443 | - */ |
|
444 | - public function prepare_for_get($DateTime) |
|
445 | - { |
|
446 | - return $this->_prepare_for_display($DateTime); |
|
447 | - } |
|
448 | - |
|
449 | - |
|
450 | - /** |
|
451 | - * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
452 | - * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
453 | - * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
454 | - * abbreviation to the date_string. |
|
455 | - * |
|
456 | - * @param mixed $DateTime |
|
457 | - * @param null $schema |
|
458 | - * @return string |
|
459 | - * @throws \EE_Error |
|
460 | - */ |
|
461 | - public function prepare_for_pretty_echoing($DateTime, $schema = null) |
|
462 | - { |
|
463 | - return $this->_prepare_for_display($DateTime, $schema ? $schema : true); |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
469 | - * timezone). |
|
470 | - * |
|
471 | - * @param DateTime $DateTime |
|
472 | - * @param bool|string $schema |
|
473 | - * @return string |
|
474 | - * @throws \EE_Error |
|
475 | - */ |
|
476 | - protected function _prepare_for_display($DateTime, $schema = false) |
|
477 | - { |
|
478 | - if (! $DateTime instanceof DateTime) { |
|
479 | - if ($this->_nullable) { |
|
480 | - return ''; |
|
481 | - } else { |
|
482 | - if (WP_DEBUG) { |
|
483 | - throw new EE_Error( |
|
484 | - sprintf( |
|
485 | - __( |
|
486 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
487 | - 'event_espresso' |
|
488 | - ), |
|
489 | - $this->_nicename |
|
490 | - ) |
|
491 | - ); |
|
492 | - } else { |
|
493 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now); |
|
494 | - EE_Error::add_error( |
|
495 | - sprintf( |
|
496 | - __( |
|
497 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
498 | - 'event_espresso' |
|
499 | - ), |
|
500 | - $this->_nicename |
|
501 | - ) |
|
502 | - ); |
|
503 | - } |
|
504 | - } |
|
505 | - } |
|
506 | - $format_string = $this->_get_date_time_output($schema); |
|
507 | - //make sure datetime_value is in the correct timezone (in case that's been updated). |
|
508 | - $DateTime->setTimezone($this->_DateTimeZone); |
|
509 | - if ($schema) { |
|
510 | - if ($this->_display_timezone()) { |
|
511 | - //must be explicit because schema could equal true. |
|
512 | - if ($schema === 'no_html') { |
|
513 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
514 | - } else { |
|
515 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
516 | - } |
|
517 | - } else { |
|
518 | - $timezone_string = ''; |
|
519 | - } |
|
520 | - |
|
521 | - return $DateTime->format($format_string) . $timezone_string; |
|
522 | - } else { |
|
523 | - return $DateTime->format($format_string); |
|
524 | - } |
|
525 | - } |
|
526 | - |
|
527 | - |
|
528 | - /** |
|
529 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
530 | - * timezone). |
|
531 | - * |
|
532 | - * @param mixed $datetime_value u |
|
533 | - * @return string mysql timestamp in UTC |
|
534 | - * @throws \EE_Error |
|
535 | - */ |
|
536 | - public function prepare_for_use_in_db($datetime_value) |
|
537 | - { |
|
538 | - //we allow an empty value or DateTime object, but nothing else. |
|
539 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
540 | - throw new EE_Error( |
|
541 | - sprintf( |
|
542 | - __( |
|
543 | - 'The incoming value being prepared for setting in the database must either be empty or a php |
|
20 | + /** |
|
21 | + * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
22 | + * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
23 | + * |
|
24 | + * @type string unix_timestamp_regex |
|
25 | + */ |
|
26 | + const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
27 | + |
|
28 | + /** |
|
29 | + * @type string mysql_timestamp_format |
|
30 | + */ |
|
31 | + const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
32 | + |
|
33 | + /** |
|
34 | + * @type string mysql_date_format |
|
35 | + */ |
|
36 | + const mysql_date_format = 'Y-m-d'; |
|
37 | + |
|
38 | + /** |
|
39 | + * @type string mysql_time_format |
|
40 | + */ |
|
41 | + const mysql_time_format = 'H:i:s'; |
|
42 | + |
|
43 | + /** |
|
44 | + * Const for using in the default value. If the field's default is set to this, |
|
45 | + * then we will return the time of calling `get_default_value()`, not |
|
46 | + * just the current time at construction |
|
47 | + */ |
|
48 | + const now = 'now'; |
|
49 | + |
|
50 | + /** |
|
51 | + * The following properties hold the default formats for date and time. |
|
52 | + * Defaults are set via the constructor and can be overridden on class instantiation. |
|
53 | + * However they can also be overridden later by the set_format() method |
|
54 | + * (and corresponding set_date_format, set_time_format methods); |
|
55 | + */ |
|
56 | + /** |
|
57 | + * @type string $_date_format |
|
58 | + */ |
|
59 | + protected $_date_format = ''; |
|
60 | + |
|
61 | + /** |
|
62 | + * @type string $_time_format |
|
63 | + */ |
|
64 | + protected $_time_format = ''; |
|
65 | + |
|
66 | + /** |
|
67 | + * @type string $_pretty_date_format |
|
68 | + */ |
|
69 | + protected $_pretty_date_format = ''; |
|
70 | + |
|
71 | + /** |
|
72 | + * @type string $_pretty_time_format |
|
73 | + */ |
|
74 | + protected $_pretty_time_format = ''; |
|
75 | + |
|
76 | + /** |
|
77 | + * @type DateTimeZone $_DateTimeZone |
|
78 | + */ |
|
79 | + protected $_DateTimeZone; |
|
80 | + |
|
81 | + /** |
|
82 | + * @type DateTimeZone $_UTC_DateTimeZone |
|
83 | + */ |
|
84 | + protected $_UTC_DateTimeZone; |
|
85 | + |
|
86 | + /** |
|
87 | + * @type DateTimeZone $_blog_DateTimeZone |
|
88 | + */ |
|
89 | + protected $_blog_DateTimeZone; |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
94 | + * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
95 | + * and time returned via getters. |
|
96 | + * |
|
97 | + * @var mixed (null|string) |
|
98 | + */ |
|
99 | + protected $_date_time_output; |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * timezone string |
|
104 | + * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
105 | + * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
106 | + * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
107 | + * |
|
108 | + * @var string |
|
109 | + */ |
|
110 | + protected $_timezone_string; |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
115 | + * offsets for comparison purposes). |
|
116 | + * |
|
117 | + * @var int |
|
118 | + */ |
|
119 | + protected $_blog_offset; |
|
120 | + |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * @param string $table_column |
|
125 | + * @param string $nice_name |
|
126 | + * @param bool $nullable |
|
127 | + * @param string $default_value |
|
128 | + * @param string $timezone_string |
|
129 | + * @param string $date_format |
|
130 | + * @param string $time_format |
|
131 | + * @param string $pretty_date_format |
|
132 | + * @param string $pretty_time_format |
|
133 | + * @throws EE_Error |
|
134 | + * @throws InvalidArgumentException |
|
135 | + */ |
|
136 | + public function __construct( |
|
137 | + $table_column, |
|
138 | + $nice_name, |
|
139 | + $nullable, |
|
140 | + $default_value, |
|
141 | + $timezone_string = '', |
|
142 | + $date_format = '', |
|
143 | + $time_format = '', |
|
144 | + $pretty_date_format = '', |
|
145 | + $pretty_time_format = '' |
|
146 | + ) { |
|
147 | + |
|
148 | + $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
149 | + $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
150 | + $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
|
151 | + $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
|
152 | + |
|
153 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
154 | + $this->set_timezone($timezone_string); |
|
155 | + $this->setSchemaFormat('date-time'); |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * @return DateTimeZone |
|
161 | + * @throws \EE_Error |
|
162 | + */ |
|
163 | + public function get_UTC_DateTimeZone() |
|
164 | + { |
|
165 | + return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
166 | + ? $this->_UTC_DateTimeZone |
|
167 | + : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * @return DateTimeZone |
|
173 | + * @throws \EE_Error |
|
174 | + */ |
|
175 | + public function get_blog_DateTimeZone() |
|
176 | + { |
|
177 | + return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
178 | + ? $this->_blog_DateTimeZone |
|
179 | + : $this->_create_timezone_object_from_timezone_string(''); |
|
180 | + } |
|
181 | + |
|
182 | + |
|
183 | + /** |
|
184 | + * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
185 | + * |
|
186 | + * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
187 | + * timestamp |
|
188 | + * @return DateTime |
|
189 | + */ |
|
190 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
191 | + { |
|
192 | + return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
193 | + } |
|
194 | + |
|
195 | + |
|
196 | + /** |
|
197 | + * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
198 | + * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
199 | + * |
|
200 | + * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
201 | + * @return string The final assembled format string. |
|
202 | + */ |
|
203 | + protected function _get_date_time_output($pretty = false) |
|
204 | + { |
|
205 | + |
|
206 | + switch ($this->_date_time_output) { |
|
207 | + case 'time' : |
|
208 | + return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
209 | + break; |
|
210 | + |
|
211 | + case 'date' : |
|
212 | + return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
213 | + break; |
|
214 | + |
|
215 | + default : |
|
216 | + return $pretty |
|
217 | + ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
218 | + : $this->_date_format . ' ' . $this->_time_format; |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
225 | + * returned (using the format properties) |
|
226 | + * |
|
227 | + * @param string $what acceptable values are 'time' or 'date'. |
|
228 | + * Any other value will be set but will always result |
|
229 | + * in both 'date' and 'time' being returned. |
|
230 | + * @return void |
|
231 | + */ |
|
232 | + public function set_date_time_output($what = null) |
|
233 | + { |
|
234 | + $this->_date_time_output = $what; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
240 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
241 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
242 | + * We also set some other properties in this method. |
|
243 | + * |
|
244 | + * @param string $timezone_string A valid timezone string as described by @link |
|
245 | + * http://www.php.net/manual/en/timezones.php |
|
246 | + * @return void |
|
247 | + * @throws InvalidArgumentException |
|
248 | + * @throws InvalidDataTypeException |
|
249 | + * @throws InvalidInterfaceException |
|
250 | + */ |
|
251 | + public function set_timezone($timezone_string) |
|
252 | + { |
|
253 | + if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
254 | + // leave the timezone AS-IS if we already have one and |
|
255 | + // the function arg didn't provide one |
|
256 | + return; |
|
257 | + } |
|
258 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
259 | + $this->_timezone_string = ! empty($timezone_string) ? $timezone_string : 'UTC'; |
|
260 | + $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * _create_timezone_object_from_timezone_name |
|
266 | + * |
|
267 | + * @access protected |
|
268 | + * @param string $timezone_string |
|
269 | + * @return \DateTimeZone |
|
270 | + * @throws InvalidArgumentException |
|
271 | + * @throws InvalidDataTypeException |
|
272 | + * @throws InvalidInterfaceException |
|
273 | + */ |
|
274 | + protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
|
275 | + { |
|
276 | + return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
277 | + } |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * This just returns whatever is set for the current timezone. |
|
282 | + * |
|
283 | + * @access public |
|
284 | + * @return string timezone string |
|
285 | + */ |
|
286 | + public function get_timezone() |
|
287 | + { |
|
288 | + return $this->_timezone_string; |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * set the $_date_format property |
|
294 | + * |
|
295 | + * @access public |
|
296 | + * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
297 | + * @param bool $pretty Whether to set pretty format or not. |
|
298 | + * @return void |
|
299 | + */ |
|
300 | + public function set_date_format($format, $pretty = false) |
|
301 | + { |
|
302 | + if ($pretty) { |
|
303 | + $this->_pretty_date_format = $format; |
|
304 | + } else { |
|
305 | + $this->_date_format = $format; |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + |
|
310 | + /** |
|
311 | + * return the $_date_format property value. |
|
312 | + * |
|
313 | + * @param bool $pretty Whether to get pretty format or not. |
|
314 | + * @return string |
|
315 | + */ |
|
316 | + public function get_date_format($pretty = false) |
|
317 | + { |
|
318 | + return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * set the $_time_format property |
|
324 | + * |
|
325 | + * @access public |
|
326 | + * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
327 | + * @param bool $pretty Whether to set pretty format or not. |
|
328 | + * @return void |
|
329 | + */ |
|
330 | + public function set_time_format($format, $pretty = false) |
|
331 | + { |
|
332 | + if ($pretty) { |
|
333 | + $this->_pretty_time_format = $format; |
|
334 | + } else { |
|
335 | + $this->_time_format = $format; |
|
336 | + } |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * return the $_time_format property value. |
|
342 | + * |
|
343 | + * @param bool $pretty Whether to get pretty format or not. |
|
344 | + * @return string |
|
345 | + */ |
|
346 | + public function get_time_format($pretty = false) |
|
347 | + { |
|
348 | + return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * set the $_pretty_date_format property |
|
354 | + * |
|
355 | + * @access public |
|
356 | + * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
357 | + * @return void |
|
358 | + */ |
|
359 | + public function set_pretty_date_format($format) |
|
360 | + { |
|
361 | + $this->_pretty_date_format = $format; |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * set the $_pretty_time_format property |
|
367 | + * |
|
368 | + * @access public |
|
369 | + * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
370 | + * @return void |
|
371 | + */ |
|
372 | + public function set_pretty_time_format($format) |
|
373 | + { |
|
374 | + $this->_pretty_time_format = $format; |
|
375 | + } |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * Only sets the time portion of the datetime. |
|
380 | + * |
|
381 | + * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
382 | + * @param DateTime $current current DateTime object for the datetime field |
|
383 | + * @return DateTime |
|
384 | + */ |
|
385 | + public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
386 | + { |
|
387 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
388 | + // Otherwise parse the string. |
|
389 | + if ($time_to_set_string instanceof DateTime) { |
|
390 | + $parsed = array( |
|
391 | + 'hour' => $time_to_set_string->format('H'), |
|
392 | + 'minute' => $time_to_set_string->format('i'), |
|
393 | + 'second' => $time_to_set_string->format('s'), |
|
394 | + ); |
|
395 | + } else { |
|
396 | + //parse incoming string |
|
397 | + $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
398 | + } |
|
399 | + |
|
400 | + //make sure $current is in the correct timezone. |
|
401 | + $current->setTimezone($this->_DateTimeZone); |
|
402 | + |
|
403 | + return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
404 | + } |
|
405 | + |
|
406 | + |
|
407 | + /** |
|
408 | + * Only sets the date portion of the datetime. |
|
409 | + * |
|
410 | + * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
411 | + * @param DateTime $current current DateTime object for the datetime field |
|
412 | + * @return DateTime |
|
413 | + */ |
|
414 | + public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
415 | + { |
|
416 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
417 | + // Otherwise parse the string. |
|
418 | + if ($date_to_set_string instanceof DateTime) { |
|
419 | + $parsed = array( |
|
420 | + 'year' => $date_to_set_string->format('Y'), |
|
421 | + 'month' => $date_to_set_string->format('m'), |
|
422 | + 'day' => $date_to_set_string->format('d'), |
|
423 | + ); |
|
424 | + } else { |
|
425 | + //parse incoming string |
|
426 | + $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
427 | + } |
|
428 | + |
|
429 | + //make sure $current is in the correct timezone |
|
430 | + $current->setTimezone($this->_DateTimeZone); |
|
431 | + |
|
432 | + return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
438 | + * datetime gets to this stage it should ALREADY be in UTC time |
|
439 | + * |
|
440 | + * @param DateTime $DateTime |
|
441 | + * @return string formatted date time for given timezone |
|
442 | + * @throws \EE_Error |
|
443 | + */ |
|
444 | + public function prepare_for_get($DateTime) |
|
445 | + { |
|
446 | + return $this->_prepare_for_display($DateTime); |
|
447 | + } |
|
448 | + |
|
449 | + |
|
450 | + /** |
|
451 | + * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
452 | + * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
453 | + * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
454 | + * abbreviation to the date_string. |
|
455 | + * |
|
456 | + * @param mixed $DateTime |
|
457 | + * @param null $schema |
|
458 | + * @return string |
|
459 | + * @throws \EE_Error |
|
460 | + */ |
|
461 | + public function prepare_for_pretty_echoing($DateTime, $schema = null) |
|
462 | + { |
|
463 | + return $this->_prepare_for_display($DateTime, $schema ? $schema : true); |
|
464 | + } |
|
465 | + |
|
466 | + |
|
467 | + /** |
|
468 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
469 | + * timezone). |
|
470 | + * |
|
471 | + * @param DateTime $DateTime |
|
472 | + * @param bool|string $schema |
|
473 | + * @return string |
|
474 | + * @throws \EE_Error |
|
475 | + */ |
|
476 | + protected function _prepare_for_display($DateTime, $schema = false) |
|
477 | + { |
|
478 | + if (! $DateTime instanceof DateTime) { |
|
479 | + if ($this->_nullable) { |
|
480 | + return ''; |
|
481 | + } else { |
|
482 | + if (WP_DEBUG) { |
|
483 | + throw new EE_Error( |
|
484 | + sprintf( |
|
485 | + __( |
|
486 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
487 | + 'event_espresso' |
|
488 | + ), |
|
489 | + $this->_nicename |
|
490 | + ) |
|
491 | + ); |
|
492 | + } else { |
|
493 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now); |
|
494 | + EE_Error::add_error( |
|
495 | + sprintf( |
|
496 | + __( |
|
497 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
498 | + 'event_espresso' |
|
499 | + ), |
|
500 | + $this->_nicename |
|
501 | + ) |
|
502 | + ); |
|
503 | + } |
|
504 | + } |
|
505 | + } |
|
506 | + $format_string = $this->_get_date_time_output($schema); |
|
507 | + //make sure datetime_value is in the correct timezone (in case that's been updated). |
|
508 | + $DateTime->setTimezone($this->_DateTimeZone); |
|
509 | + if ($schema) { |
|
510 | + if ($this->_display_timezone()) { |
|
511 | + //must be explicit because schema could equal true. |
|
512 | + if ($schema === 'no_html') { |
|
513 | + $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
514 | + } else { |
|
515 | + $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
516 | + } |
|
517 | + } else { |
|
518 | + $timezone_string = ''; |
|
519 | + } |
|
520 | + |
|
521 | + return $DateTime->format($format_string) . $timezone_string; |
|
522 | + } else { |
|
523 | + return $DateTime->format($format_string); |
|
524 | + } |
|
525 | + } |
|
526 | + |
|
527 | + |
|
528 | + /** |
|
529 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
530 | + * timezone). |
|
531 | + * |
|
532 | + * @param mixed $datetime_value u |
|
533 | + * @return string mysql timestamp in UTC |
|
534 | + * @throws \EE_Error |
|
535 | + */ |
|
536 | + public function prepare_for_use_in_db($datetime_value) |
|
537 | + { |
|
538 | + //we allow an empty value or DateTime object, but nothing else. |
|
539 | + if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
540 | + throw new EE_Error( |
|
541 | + sprintf( |
|
542 | + __( |
|
543 | + 'The incoming value being prepared for setting in the database must either be empty or a php |
|
544 | 544 | DateTime object, instead of: %1$s %2$s', |
545 | - 'event_espresso' |
|
546 | - ), |
|
547 | - '<br />', |
|
548 | - print_r($datetime_value, true) |
|
549 | - ) |
|
550 | - ); |
|
551 | - } |
|
552 | - |
|
553 | - if ($datetime_value instanceof DateTime) { |
|
554 | - if ( ! $datetime_value instanceof DbSafeDateTime) { |
|
555 | - $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
556 | - } |
|
557 | - |
|
558 | - return $datetime_value->setTimezone($this->get_UTC_DateTimeZone())->format( |
|
559 | - EE_Datetime_Field::mysql_timestamp_format |
|
560 | - ); |
|
561 | - } |
|
562 | - |
|
563 | - // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
564 | - return ! $this->_nullable && empty($datetime_value) ? current_time('mysql', true) : null; |
|
565 | - } |
|
566 | - |
|
567 | - |
|
568 | - /** |
|
569 | - * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
570 | - * allowed) |
|
571 | - * |
|
572 | - * @param string $datetime_string mysql timestamp in UTC |
|
573 | - * @return mixed null | DateTime |
|
574 | - * @throws \EE_Error |
|
575 | - */ |
|
576 | - public function prepare_for_set_from_db($datetime_string) |
|
577 | - { |
|
578 | - //if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
579 | - if (empty($datetime_string) && $this->_nullable) { |
|
580 | - return null; |
|
581 | - } |
|
582 | - // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
583 | - if (empty($datetime_string)) { |
|
584 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
585 | - } else { |
|
586 | - $DateTime = DateTime::createFromFormat( |
|
587 | - EE_Datetime_Field::mysql_timestamp_format, |
|
588 | - $datetime_string, |
|
589 | - $this->get_UTC_DateTimeZone() |
|
590 | - ); |
|
591 | - if ($DateTime instanceof \DateTime) { |
|
592 | - $DateTime = new DbSafeDateTime( |
|
593 | - $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
594 | - $this->get_UTC_DateTimeZone() |
|
595 | - ); |
|
596 | - } |
|
597 | - } |
|
598 | - |
|
599 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
600 | - // if still no datetime object, then let's just use now |
|
601 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
602 | - } |
|
603 | - // THEN apply the field's set DateTimeZone |
|
604 | - $DateTime->setTimezone($this->_DateTimeZone); |
|
605 | - //apparently in PHP5.6 this is needed to ensure the correct timestamp is internal on this object. This fixes |
|
606 | - //failing tests against PHP5.6 for the `Read_Test::test_handle_request_get_one__event` test. |
|
607 | - //@see https://events.codebasehq.com/projects/event-espresso/tickets/11233 |
|
608 | - $DateTime->getTimestamp(); |
|
609 | - return $DateTime; |
|
610 | - } |
|
611 | - |
|
612 | - |
|
613 | - /** |
|
614 | - * All this method does is determine if we're going to display the timezone string or not on any output. |
|
615 | - * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
616 | - * If so, then true. |
|
617 | - * |
|
618 | - * @return bool true for yes false for no |
|
619 | - * @throws \EE_Error |
|
620 | - */ |
|
621 | - protected function _display_timezone() |
|
622 | - { |
|
623 | - |
|
624 | - // first let's do a comparison of timezone strings. |
|
625 | - // If they match then we can get out without any further calculations |
|
626 | - $blog_string = get_option('timezone_string'); |
|
627 | - if ($blog_string === $this->_timezone_string) { |
|
628 | - return false; |
|
629 | - } |
|
630 | - // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
631 | - $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
632 | - $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
633 | - // now compare |
|
634 | - return $blog_offset !== $this_offset; |
|
635 | - } |
|
636 | - |
|
637 | - |
|
638 | - /** |
|
639 | - * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
640 | - * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
641 | - * with. |
|
642 | - * |
|
643 | - * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
644 | - * in the format that is set on the date_field (or DateTime |
|
645 | - * object)! |
|
646 | - * @return DateTime |
|
647 | - */ |
|
648 | - protected function _get_date_object($date_string) |
|
649 | - { |
|
650 | - //first if this is an empty date_string and nullable is allowed, just return null. |
|
651 | - if ($this->_nullable && empty($date_string)) { |
|
652 | - return null; |
|
653 | - } |
|
654 | - |
|
655 | - // if incoming date |
|
656 | - if ($date_string instanceof DateTime) { |
|
657 | - $date_string->setTimezone($this->_DateTimeZone); |
|
658 | - |
|
659 | - return $date_string; |
|
660 | - } |
|
661 | - // if empty date_string and made it here. |
|
662 | - // Return a datetime object for now in the given timezone. |
|
663 | - if (empty($date_string)) { |
|
664 | - return new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
665 | - } |
|
666 | - // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
667 | - if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
668 | - try { |
|
669 | - // This is operating under the assumption that the incoming Unix timestamp |
|
670 | - // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
671 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
672 | - $DateTime->setTimestamp($date_string); |
|
673 | - |
|
674 | - return $DateTime; |
|
675 | - } catch (Exception $e) { |
|
676 | - // should be rare, but if things got fooled then let's just continue |
|
677 | - } |
|
678 | - } |
|
679 | - //not a unix timestamp. So we will use the set format on this object and set timezone to |
|
680 | - //create the DateTime object. |
|
681 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
682 | - try { |
|
683 | - $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
684 | - if ($DateTime instanceof DateTime) { |
|
685 | - $DateTime = new DbSafeDateTime( |
|
686 | - $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
687 | - $this->_DateTimeZone |
|
688 | - ); |
|
689 | - } |
|
690 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
691 | - throw new EE_Error( |
|
692 | - sprintf( |
|
693 | - __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
|
694 | - $date_string, |
|
695 | - $format |
|
696 | - ) |
|
697 | - ); |
|
698 | - } |
|
699 | - } catch (Exception $e) { |
|
700 | - // if we made it here then likely then something went really wrong. |
|
701 | - // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
702 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
703 | - } |
|
704 | - |
|
705 | - return $DateTime; |
|
706 | - } |
|
707 | - |
|
708 | - |
|
709 | - |
|
710 | - /** |
|
711 | - * get_timezone_transitions |
|
712 | - * |
|
713 | - * @param \DateTimeZone $DateTimeZone |
|
714 | - * @param int $time |
|
715 | - * @param bool $first_only |
|
716 | - * @return mixed |
|
717 | - */ |
|
718 | - public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
719 | - { |
|
720 | - return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
721 | - } |
|
722 | - |
|
723 | - |
|
724 | - |
|
725 | - /** |
|
726 | - * get_timezone_offset |
|
727 | - * |
|
728 | - * @param \DateTimeZone $DateTimeZone |
|
729 | - * @param int $time |
|
730 | - * @return mixed |
|
731 | - * @throws \DomainException |
|
732 | - */ |
|
733 | - public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
734 | - { |
|
735 | - return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
736 | - } |
|
737 | - |
|
738 | - |
|
739 | - /** |
|
740 | - * This will take an incoming timezone string and return the abbreviation for that timezone |
|
741 | - * |
|
742 | - * @param string $timezone_string |
|
743 | - * @return string abbreviation |
|
744 | - * @throws \EE_Error |
|
745 | - */ |
|
746 | - public function get_timezone_abbrev($timezone_string) |
|
747 | - { |
|
748 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
749 | - $dateTime = new DateTime(\EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
750 | - |
|
751 | - return $dateTime->format('T'); |
|
752 | - } |
|
753 | - |
|
754 | - /** |
|
755 | - * Overrides the parent to allow for having a dynamic "now" value |
|
756 | - * |
|
757 | - * @return mixed |
|
758 | - */ |
|
759 | - public function get_default_value() |
|
760 | - { |
|
761 | - if ($this->_default_value === EE_Datetime_Field::now) { |
|
762 | - return time(); |
|
763 | - } else { |
|
764 | - return parent::get_default_value(); |
|
765 | - } |
|
766 | - } |
|
767 | - |
|
768 | - |
|
769 | - public function getSchemaDescription() |
|
770 | - { |
|
771 | - return sprintf( |
|
772 | - esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
773 | - $this->get_nicename() |
|
774 | - ); |
|
775 | - } |
|
545 | + 'event_espresso' |
|
546 | + ), |
|
547 | + '<br />', |
|
548 | + print_r($datetime_value, true) |
|
549 | + ) |
|
550 | + ); |
|
551 | + } |
|
552 | + |
|
553 | + if ($datetime_value instanceof DateTime) { |
|
554 | + if ( ! $datetime_value instanceof DbSafeDateTime) { |
|
555 | + $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
556 | + } |
|
557 | + |
|
558 | + return $datetime_value->setTimezone($this->get_UTC_DateTimeZone())->format( |
|
559 | + EE_Datetime_Field::mysql_timestamp_format |
|
560 | + ); |
|
561 | + } |
|
562 | + |
|
563 | + // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
564 | + return ! $this->_nullable && empty($datetime_value) ? current_time('mysql', true) : null; |
|
565 | + } |
|
566 | + |
|
567 | + |
|
568 | + /** |
|
569 | + * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
570 | + * allowed) |
|
571 | + * |
|
572 | + * @param string $datetime_string mysql timestamp in UTC |
|
573 | + * @return mixed null | DateTime |
|
574 | + * @throws \EE_Error |
|
575 | + */ |
|
576 | + public function prepare_for_set_from_db($datetime_string) |
|
577 | + { |
|
578 | + //if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
579 | + if (empty($datetime_string) && $this->_nullable) { |
|
580 | + return null; |
|
581 | + } |
|
582 | + // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
583 | + if (empty($datetime_string)) { |
|
584 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
585 | + } else { |
|
586 | + $DateTime = DateTime::createFromFormat( |
|
587 | + EE_Datetime_Field::mysql_timestamp_format, |
|
588 | + $datetime_string, |
|
589 | + $this->get_UTC_DateTimeZone() |
|
590 | + ); |
|
591 | + if ($DateTime instanceof \DateTime) { |
|
592 | + $DateTime = new DbSafeDateTime( |
|
593 | + $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
594 | + $this->get_UTC_DateTimeZone() |
|
595 | + ); |
|
596 | + } |
|
597 | + } |
|
598 | + |
|
599 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
600 | + // if still no datetime object, then let's just use now |
|
601 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
602 | + } |
|
603 | + // THEN apply the field's set DateTimeZone |
|
604 | + $DateTime->setTimezone($this->_DateTimeZone); |
|
605 | + //apparently in PHP5.6 this is needed to ensure the correct timestamp is internal on this object. This fixes |
|
606 | + //failing tests against PHP5.6 for the `Read_Test::test_handle_request_get_one__event` test. |
|
607 | + //@see https://events.codebasehq.com/projects/event-espresso/tickets/11233 |
|
608 | + $DateTime->getTimestamp(); |
|
609 | + return $DateTime; |
|
610 | + } |
|
611 | + |
|
612 | + |
|
613 | + /** |
|
614 | + * All this method does is determine if we're going to display the timezone string or not on any output. |
|
615 | + * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
616 | + * If so, then true. |
|
617 | + * |
|
618 | + * @return bool true for yes false for no |
|
619 | + * @throws \EE_Error |
|
620 | + */ |
|
621 | + protected function _display_timezone() |
|
622 | + { |
|
623 | + |
|
624 | + // first let's do a comparison of timezone strings. |
|
625 | + // If they match then we can get out without any further calculations |
|
626 | + $blog_string = get_option('timezone_string'); |
|
627 | + if ($blog_string === $this->_timezone_string) { |
|
628 | + return false; |
|
629 | + } |
|
630 | + // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
631 | + $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
632 | + $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
633 | + // now compare |
|
634 | + return $blog_offset !== $this_offset; |
|
635 | + } |
|
636 | + |
|
637 | + |
|
638 | + /** |
|
639 | + * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
640 | + * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
641 | + * with. |
|
642 | + * |
|
643 | + * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
644 | + * in the format that is set on the date_field (or DateTime |
|
645 | + * object)! |
|
646 | + * @return DateTime |
|
647 | + */ |
|
648 | + protected function _get_date_object($date_string) |
|
649 | + { |
|
650 | + //first if this is an empty date_string and nullable is allowed, just return null. |
|
651 | + if ($this->_nullable && empty($date_string)) { |
|
652 | + return null; |
|
653 | + } |
|
654 | + |
|
655 | + // if incoming date |
|
656 | + if ($date_string instanceof DateTime) { |
|
657 | + $date_string->setTimezone($this->_DateTimeZone); |
|
658 | + |
|
659 | + return $date_string; |
|
660 | + } |
|
661 | + // if empty date_string and made it here. |
|
662 | + // Return a datetime object for now in the given timezone. |
|
663 | + if (empty($date_string)) { |
|
664 | + return new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
665 | + } |
|
666 | + // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
667 | + if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
668 | + try { |
|
669 | + // This is operating under the assumption that the incoming Unix timestamp |
|
670 | + // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
671 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
672 | + $DateTime->setTimestamp($date_string); |
|
673 | + |
|
674 | + return $DateTime; |
|
675 | + } catch (Exception $e) { |
|
676 | + // should be rare, but if things got fooled then let's just continue |
|
677 | + } |
|
678 | + } |
|
679 | + //not a unix timestamp. So we will use the set format on this object and set timezone to |
|
680 | + //create the DateTime object. |
|
681 | + $format = $this->_date_format . ' ' . $this->_time_format; |
|
682 | + try { |
|
683 | + $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
684 | + if ($DateTime instanceof DateTime) { |
|
685 | + $DateTime = new DbSafeDateTime( |
|
686 | + $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
687 | + $this->_DateTimeZone |
|
688 | + ); |
|
689 | + } |
|
690 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
691 | + throw new EE_Error( |
|
692 | + sprintf( |
|
693 | + __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
|
694 | + $date_string, |
|
695 | + $format |
|
696 | + ) |
|
697 | + ); |
|
698 | + } |
|
699 | + } catch (Exception $e) { |
|
700 | + // if we made it here then likely then something went really wrong. |
|
701 | + // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
702 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
703 | + } |
|
704 | + |
|
705 | + return $DateTime; |
|
706 | + } |
|
707 | + |
|
708 | + |
|
709 | + |
|
710 | + /** |
|
711 | + * get_timezone_transitions |
|
712 | + * |
|
713 | + * @param \DateTimeZone $DateTimeZone |
|
714 | + * @param int $time |
|
715 | + * @param bool $first_only |
|
716 | + * @return mixed |
|
717 | + */ |
|
718 | + public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
719 | + { |
|
720 | + return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
721 | + } |
|
722 | + |
|
723 | + |
|
724 | + |
|
725 | + /** |
|
726 | + * get_timezone_offset |
|
727 | + * |
|
728 | + * @param \DateTimeZone $DateTimeZone |
|
729 | + * @param int $time |
|
730 | + * @return mixed |
|
731 | + * @throws \DomainException |
|
732 | + */ |
|
733 | + public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
734 | + { |
|
735 | + return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
736 | + } |
|
737 | + |
|
738 | + |
|
739 | + /** |
|
740 | + * This will take an incoming timezone string and return the abbreviation for that timezone |
|
741 | + * |
|
742 | + * @param string $timezone_string |
|
743 | + * @return string abbreviation |
|
744 | + * @throws \EE_Error |
|
745 | + */ |
|
746 | + public function get_timezone_abbrev($timezone_string) |
|
747 | + { |
|
748 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
749 | + $dateTime = new DateTime(\EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
750 | + |
|
751 | + return $dateTime->format('T'); |
|
752 | + } |
|
753 | + |
|
754 | + /** |
|
755 | + * Overrides the parent to allow for having a dynamic "now" value |
|
756 | + * |
|
757 | + * @return mixed |
|
758 | + */ |
|
759 | + public function get_default_value() |
|
760 | + { |
|
761 | + if ($this->_default_value === EE_Datetime_Field::now) { |
|
762 | + return time(); |
|
763 | + } else { |
|
764 | + return parent::get_default_value(); |
|
765 | + } |
|
766 | + } |
|
767 | + |
|
768 | + |
|
769 | + public function getSchemaDescription() |
|
770 | + { |
|
771 | + return sprintf( |
|
772 | + esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
773 | + $this->get_nicename() |
|
774 | + ); |
|
775 | + } |
|
776 | 776 | } |
@@ -16,94 +16,94 @@ |
||
16 | 16 | interface HelperInterface |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * Ensures that a valid timezone string is returned. |
|
21 | - * |
|
22 | - * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
|
23 | - * settings (or derive from set UTC offset). |
|
24 | - * @return string |
|
25 | - */ |
|
26 | - public function getValidTimezoneString($timezone_string = ''); |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
31 | - * |
|
32 | - * @param string $timezone_string |
|
33 | - * @param bool $throw_error |
|
34 | - * @return bool |
|
35 | - */ |
|
36 | - public function validateTimezone($timezone_string, $throw_error = true); |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * Returns a timezone string for the provided gmt_offset. |
|
41 | - * @param float|string $gmt_offset |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - public function getTimezoneStringFromGmtOffset($gmt_offset = ''); |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * Gets the site's GMT offset based on either the timezone string |
|
49 | - * (in which case the gmt offset will vary depending on the location's |
|
50 | - * observance of daylight savings time) or the gmt_offset wp option |
|
51 | - * |
|
52 | - * @return int seconds offset |
|
53 | - */ |
|
54 | - public function getSiteTimezoneGmtOffset(); |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * Get timezone transitions |
|
59 | - * @param DateTimeZone $date_time_zone |
|
60 | - * @param int|null $time |
|
61 | - * @param bool $first_only |
|
62 | - * @return array |
|
63 | - */ |
|
64 | - public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true); |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Get Timezone offset for given timezone object |
|
69 | - * @param DateTimeZone $date_time_zone |
|
70 | - * @param null|int $time |
|
71 | - * @return int |
|
72 | - */ |
|
73 | - public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null); |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * Provide a timezone select input |
|
78 | - * @param string $timezone_string |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function timezoneSelectInput($timezone_string = ''); |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
86 | - * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
87 | - * the site is used. |
|
88 | - * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
89 | - * computed timestamp (i.e. date_i18n() ) |
|
90 | - * |
|
91 | - * @param int $unix_timestamp if 0, then time() will be used. |
|
92 | - * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
93 | - * site will be used. |
|
94 | - * @return int unix_timestamp value with the offset applied for the given timezone. |
|
95 | - */ |
|
96 | - public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = ''); |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * Depending on PHP version, |
|
101 | - * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
|
102 | - * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
103 | - * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
104 | - * |
|
105 | - * @param int $gmt_offset |
|
106 | - * @return int |
|
107 | - */ |
|
108 | - public function adjustInvalidGmtOffsets($gmt_offset); |
|
19 | + /** |
|
20 | + * Ensures that a valid timezone string is returned. |
|
21 | + * |
|
22 | + * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
|
23 | + * settings (or derive from set UTC offset). |
|
24 | + * @return string |
|
25 | + */ |
|
26 | + public function getValidTimezoneString($timezone_string = ''); |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
31 | + * |
|
32 | + * @param string $timezone_string |
|
33 | + * @param bool $throw_error |
|
34 | + * @return bool |
|
35 | + */ |
|
36 | + public function validateTimezone($timezone_string, $throw_error = true); |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * Returns a timezone string for the provided gmt_offset. |
|
41 | + * @param float|string $gmt_offset |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + public function getTimezoneStringFromGmtOffset($gmt_offset = ''); |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * Gets the site's GMT offset based on either the timezone string |
|
49 | + * (in which case the gmt offset will vary depending on the location's |
|
50 | + * observance of daylight savings time) or the gmt_offset wp option |
|
51 | + * |
|
52 | + * @return int seconds offset |
|
53 | + */ |
|
54 | + public function getSiteTimezoneGmtOffset(); |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * Get timezone transitions |
|
59 | + * @param DateTimeZone $date_time_zone |
|
60 | + * @param int|null $time |
|
61 | + * @param bool $first_only |
|
62 | + * @return array |
|
63 | + */ |
|
64 | + public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true); |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Get Timezone offset for given timezone object |
|
69 | + * @param DateTimeZone $date_time_zone |
|
70 | + * @param null|int $time |
|
71 | + * @return int |
|
72 | + */ |
|
73 | + public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null); |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * Provide a timezone select input |
|
78 | + * @param string $timezone_string |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function timezoneSelectInput($timezone_string = ''); |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
86 | + * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
87 | + * the site is used. |
|
88 | + * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
89 | + * computed timestamp (i.e. date_i18n() ) |
|
90 | + * |
|
91 | + * @param int $unix_timestamp if 0, then time() will be used. |
|
92 | + * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
93 | + * site will be used. |
|
94 | + * @return int unix_timestamp value with the offset applied for the given timezone. |
|
95 | + */ |
|
96 | + public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = ''); |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * Depending on PHP version, |
|
101 | + * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
|
102 | + * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
103 | + * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
104 | + * |
|
105 | + * @param int $gmt_offset |
|
106 | + * @return int |
|
107 | + */ |
|
108 | + public function adjustInvalidGmtOffsets($gmt_offset); |
|
109 | 109 | } |
@@ -12,136 +12,136 @@ discard block |
||
12 | 12 | { |
13 | 13 | |
14 | 14 | |
15 | - /** |
|
16 | - * Ensures that a valid timezone string is returned. |
|
17 | - * |
|
18 | - * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
|
19 | - * settings (or derive from set UTC offset). |
|
20 | - * @return string |
|
21 | - * @throws EE_Error |
|
22 | - */ |
|
23 | - public function getValidTimezoneString($timezone_string = '') |
|
24 | - { |
|
25 | - $timezone_string = ! empty($timezone_string) ? $timezone_string : (string) get_option('timezone_string'); |
|
26 | - $timezone_string = ! empty($timezone_string) |
|
27 | - ? $timezone_string |
|
28 | - : $this->getTimezoneStringFromGmtOffset(); |
|
29 | - $this->validateTimezone($timezone_string); |
|
30 | - return $timezone_string; |
|
31 | - } |
|
15 | + /** |
|
16 | + * Ensures that a valid timezone string is returned. |
|
17 | + * |
|
18 | + * @param string $timezone_string When not provided then attempt to use the timezone_string set in the WP Time |
|
19 | + * settings (or derive from set UTC offset). |
|
20 | + * @return string |
|
21 | + * @throws EE_Error |
|
22 | + */ |
|
23 | + public function getValidTimezoneString($timezone_string = '') |
|
24 | + { |
|
25 | + $timezone_string = ! empty($timezone_string) ? $timezone_string : (string) get_option('timezone_string'); |
|
26 | + $timezone_string = ! empty($timezone_string) |
|
27 | + ? $timezone_string |
|
28 | + : $this->getTimezoneStringFromGmtOffset(); |
|
29 | + $this->validateTimezone($timezone_string); |
|
30 | + return $timezone_string; |
|
31 | + } |
|
32 | 32 | |
33 | 33 | |
34 | 34 | |
35 | - /** |
|
36 | - * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
37 | - * |
|
38 | - * @param string $timezone_string |
|
39 | - * @param bool $throw_error |
|
40 | - * @return bool |
|
41 | - * @throws EE_Error |
|
42 | - */ |
|
43 | - public function validateTimezone($timezone_string, $throw_error = true) |
|
44 | - { |
|
45 | - // easiest way to test a timezone string is just see if it throws an error when you try to create a |
|
46 | - // DateTimeZone object with it |
|
47 | - try { |
|
48 | - new DateTimeZone($timezone_string); |
|
49 | - } catch (Exception $e) { |
|
50 | - // sometimes we take exception to exceptions |
|
51 | - if (! $throw_error) { |
|
52 | - return false; |
|
53 | - } |
|
54 | - throw new EE_Error( |
|
55 | - sprintf( |
|
56 | - esc_html__( |
|
57 | - 'The timezone given (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
58 | - 'event_espresso' |
|
59 | - ), |
|
60 | - $timezone_string, |
|
61 | - '<a href="http://www.php.net/manual/en/timezones.php">', |
|
62 | - '</a>' |
|
63 | - ) |
|
64 | - ); |
|
65 | - } |
|
66 | - return true; |
|
67 | - } |
|
35 | + /** |
|
36 | + * The only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
37 | + * |
|
38 | + * @param string $timezone_string |
|
39 | + * @param bool $throw_error |
|
40 | + * @return bool |
|
41 | + * @throws EE_Error |
|
42 | + */ |
|
43 | + public function validateTimezone($timezone_string, $throw_error = true) |
|
44 | + { |
|
45 | + // easiest way to test a timezone string is just see if it throws an error when you try to create a |
|
46 | + // DateTimeZone object with it |
|
47 | + try { |
|
48 | + new DateTimeZone($timezone_string); |
|
49 | + } catch (Exception $e) { |
|
50 | + // sometimes we take exception to exceptions |
|
51 | + if (! $throw_error) { |
|
52 | + return false; |
|
53 | + } |
|
54 | + throw new EE_Error( |
|
55 | + sprintf( |
|
56 | + esc_html__( |
|
57 | + 'The timezone given (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
58 | + 'event_espresso' |
|
59 | + ), |
|
60 | + $timezone_string, |
|
61 | + '<a href="http://www.php.net/manual/en/timezones.php">', |
|
62 | + '</a>' |
|
63 | + ) |
|
64 | + ); |
|
65 | + } |
|
66 | + return true; |
|
67 | + } |
|
68 | 68 | |
69 | 69 | |
70 | - /** |
|
71 | - * Gets the site's GMT offset based on either the timezone string |
|
72 | - * (in which case the gmt offset will vary depending on the location's |
|
73 | - * observance of daylight savings time) or the gmt_offset wp option |
|
74 | - * |
|
75 | - * @return int seconds offset |
|
76 | - */ |
|
77 | - public function getSiteTimezoneGmtOffset() |
|
78 | - { |
|
79 | - $timezone_string = (string)get_option('timezone_string'); |
|
80 | - if ($timezone_string) { |
|
81 | - try { |
|
82 | - $timezone = new DateTimeZone($timezone_string); |
|
83 | - return $timezone->getOffset(new DateTime()); //in WordPress DateTime defaults to UTC |
|
84 | - } catch (Exception $e) { |
|
85 | - } |
|
86 | - } |
|
87 | - $offset = get_option('gmt_offset'); |
|
88 | - return (int) ($offset * HOUR_IN_SECONDS); |
|
89 | - } |
|
70 | + /** |
|
71 | + * Gets the site's GMT offset based on either the timezone string |
|
72 | + * (in which case the gmt offset will vary depending on the location's |
|
73 | + * observance of daylight savings time) or the gmt_offset wp option |
|
74 | + * |
|
75 | + * @return int seconds offset |
|
76 | + */ |
|
77 | + public function getSiteTimezoneGmtOffset() |
|
78 | + { |
|
79 | + $timezone_string = (string)get_option('timezone_string'); |
|
80 | + if ($timezone_string) { |
|
81 | + try { |
|
82 | + $timezone = new DateTimeZone($timezone_string); |
|
83 | + return $timezone->getOffset(new DateTime()); //in WordPress DateTime defaults to UTC |
|
84 | + } catch (Exception $e) { |
|
85 | + } |
|
86 | + } |
|
87 | + $offset = get_option('gmt_offset'); |
|
88 | + return (int) ($offset * HOUR_IN_SECONDS); |
|
89 | + } |
|
90 | 90 | |
91 | 91 | |
92 | - /** |
|
93 | - * Get Timezone offset for given timezone object |
|
94 | - * |
|
95 | - * @param DateTimeZone $date_time_zone |
|
96 | - * @param null|int $time |
|
97 | - * @return int |
|
98 | - * @throws DomainException |
|
99 | - */ |
|
100 | - public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null) |
|
101 | - { |
|
102 | - $transition = $this->getTimezoneTransitions($date_time_zone, $time); |
|
103 | - if (! isset($transition['offset'])) { |
|
104 | - throw new DomainException( |
|
105 | - sprintf( |
|
106 | - esc_html__('An invalid timezone transition was received %1$s', 'event_espresso'), |
|
107 | - print_r($transition, true) |
|
108 | - ) |
|
109 | - ); |
|
110 | - } |
|
111 | - return $transition['offset']; |
|
112 | - } |
|
92 | + /** |
|
93 | + * Get Timezone offset for given timezone object |
|
94 | + * |
|
95 | + * @param DateTimeZone $date_time_zone |
|
96 | + * @param null|int $time |
|
97 | + * @return int |
|
98 | + * @throws DomainException |
|
99 | + */ |
|
100 | + public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null) |
|
101 | + { |
|
102 | + $transition = $this->getTimezoneTransitions($date_time_zone, $time); |
|
103 | + if (! isset($transition['offset'])) { |
|
104 | + throw new DomainException( |
|
105 | + sprintf( |
|
106 | + esc_html__('An invalid timezone transition was received %1$s', 'event_espresso'), |
|
107 | + print_r($transition, true) |
|
108 | + ) |
|
109 | + ); |
|
110 | + } |
|
111 | + return $transition['offset']; |
|
112 | + } |
|
113 | 113 | |
114 | 114 | |
115 | - /** |
|
116 | - * Provide a timezone select input |
|
117 | - * |
|
118 | - * @param string $timezone_string |
|
119 | - * @return string |
|
120 | - * @throws EE_Error |
|
121 | - */ |
|
122 | - public function timezoneSelectInput($timezone_string = '') |
|
123 | - { |
|
124 | - // get WP date time format |
|
125 | - $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
126 | - // if passed a value, then use that, else get WP option |
|
127 | - $timezone_string = ! empty($timezone_string) ? $timezone_string : (string)get_option('timezone_string'); |
|
128 | - // check if the timezone is valid but don't throw any errors if it isn't |
|
129 | - $timezone_string = $this->validateTimezone($timezone_string, false) |
|
130 | - ? $timezone_string |
|
131 | - : ''; |
|
132 | - $gmt_offset = get_option('gmt_offset'); |
|
133 | - $check_zone_info = true; |
|
134 | - if (empty($timezone_string)) { |
|
135 | - // Create a UTC+- zone if no timezone string exists |
|
136 | - $timezone_string = 'UTC'; |
|
137 | - $check_zone_info = false; |
|
138 | - if ($gmt_offset > 0) { |
|
139 | - $timezone_string = 'UTC+' . $gmt_offset; |
|
140 | - } elseif ($gmt_offset < 0) { |
|
141 | - $timezone_string = 'UTC' . $gmt_offset; |
|
142 | - } |
|
143 | - } |
|
144 | - ?> |
|
115 | + /** |
|
116 | + * Provide a timezone select input |
|
117 | + * |
|
118 | + * @param string $timezone_string |
|
119 | + * @return string |
|
120 | + * @throws EE_Error |
|
121 | + */ |
|
122 | + public function timezoneSelectInput($timezone_string = '') |
|
123 | + { |
|
124 | + // get WP date time format |
|
125 | + $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
126 | + // if passed a value, then use that, else get WP option |
|
127 | + $timezone_string = ! empty($timezone_string) ? $timezone_string : (string)get_option('timezone_string'); |
|
128 | + // check if the timezone is valid but don't throw any errors if it isn't |
|
129 | + $timezone_string = $this->validateTimezone($timezone_string, false) |
|
130 | + ? $timezone_string |
|
131 | + : ''; |
|
132 | + $gmt_offset = get_option('gmt_offset'); |
|
133 | + $check_zone_info = true; |
|
134 | + if (empty($timezone_string)) { |
|
135 | + // Create a UTC+- zone if no timezone string exists |
|
136 | + $timezone_string = 'UTC'; |
|
137 | + $check_zone_info = false; |
|
138 | + if ($gmt_offset > 0) { |
|
139 | + $timezone_string = 'UTC+' . $gmt_offset; |
|
140 | + } elseif ($gmt_offset < 0) { |
|
141 | + $timezone_string = 'UTC' . $gmt_offset; |
|
142 | + } |
|
143 | + } |
|
144 | + ?> |
|
145 | 145 | <p> |
146 | 146 | <label for="timezone_string"><?php _e('timezone'); ?></label> |
147 | 147 | <select id="timezone_string" name="timezone_string"> |
@@ -153,13 +153,13 @@ discard block |
||
153 | 153 | |
154 | 154 | <p> |
155 | 155 | <span><?php |
156 | - printf( |
|
157 | - __('%1$sUTC%2$s time is %3$s'), |
|
158 | - '<abbr title="Coordinated Universal Time">', |
|
159 | - '</abbr>', |
|
160 | - '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
161 | - ); |
|
162 | - ?></span> |
|
156 | + printf( |
|
157 | + __('%1$sUTC%2$s time is %3$s'), |
|
158 | + '<abbr title="Coordinated Universal Time">', |
|
159 | + '</abbr>', |
|
160 | + '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
161 | + ); |
|
162 | + ?></span> |
|
163 | 163 | <?php if (! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
164 | 164 | <br/><span><?php printf(__('Local time is %1$s'), '<code>' . date_i18n($datetime_format) . '</code>'); ?></span> |
165 | 165 | <?php endif; ?> |
@@ -168,132 +168,132 @@ discard block |
||
168 | 168 | <br/> |
169 | 169 | <span> |
170 | 170 | <?php |
171 | - // Set TZ so localtime works. |
|
172 | - date_default_timezone_set($timezone_string); |
|
173 | - $now = localtime(time(), true); |
|
174 | - if ($now['tm_isdst']) { |
|
175 | - _e('This timezone is currently in daylight saving time.'); |
|
176 | - } else { |
|
177 | - _e('This timezone is currently in standard time.'); |
|
178 | - } |
|
179 | - ?> |
|
171 | + // Set TZ so localtime works. |
|
172 | + date_default_timezone_set($timezone_string); |
|
173 | + $now = localtime(time(), true); |
|
174 | + if ($now['tm_isdst']) { |
|
175 | + _e('This timezone is currently in daylight saving time.'); |
|
176 | + } else { |
|
177 | + _e('This timezone is currently in standard time.'); |
|
178 | + } |
|
179 | + ?> |
|
180 | 180 | <br/> |
181 | 181 | <?php |
182 | - if (function_exists('timezone_transitions_get')) { |
|
183 | - $found = false; |
|
184 | - $date_time_zone_selected = new DateTimeZone($timezone_string); |
|
185 | - $tz_offset = timezone_offset_get($date_time_zone_selected, date_create()); |
|
186 | - $right_now = time(); |
|
187 | - $tr['isdst'] = false; |
|
188 | - foreach (timezone_transitions_get($date_time_zone_selected) as $tr) { |
|
189 | - if ($tr['ts'] > $right_now) { |
|
190 | - $found = true; |
|
191 | - break; |
|
192 | - } |
|
193 | - } |
|
194 | - if ($found) { |
|
195 | - $message = $tr['isdst'] |
|
196 | - ? __(' Daylight saving time begins on: %s.') |
|
197 | - : __(' Standard time begins on: %s.'); |
|
198 | - // Add the difference between the current offset and the new offset to ts to get the correct |
|
199 | - // transition time from date_i18n(). |
|
200 | - printf( |
|
201 | - $message, |
|
202 | - '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >' |
|
203 | - ); |
|
204 | - } else { |
|
205 | - _e('This timezone does not observe daylight saving time.'); |
|
206 | - } |
|
207 | - } |
|
208 | - // Set back to UTC. |
|
209 | - date_default_timezone_set('UTC'); |
|
210 | - ?> |
|
182 | + if (function_exists('timezone_transitions_get')) { |
|
183 | + $found = false; |
|
184 | + $date_time_zone_selected = new DateTimeZone($timezone_string); |
|
185 | + $tz_offset = timezone_offset_get($date_time_zone_selected, date_create()); |
|
186 | + $right_now = time(); |
|
187 | + $tr['isdst'] = false; |
|
188 | + foreach (timezone_transitions_get($date_time_zone_selected) as $tr) { |
|
189 | + if ($tr['ts'] > $right_now) { |
|
190 | + $found = true; |
|
191 | + break; |
|
192 | + } |
|
193 | + } |
|
194 | + if ($found) { |
|
195 | + $message = $tr['isdst'] |
|
196 | + ? __(' Daylight saving time begins on: %s.') |
|
197 | + : __(' Standard time begins on: %s.'); |
|
198 | + // Add the difference between the current offset and the new offset to ts to get the correct |
|
199 | + // transition time from date_i18n(). |
|
200 | + printf( |
|
201 | + $message, |
|
202 | + '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >' |
|
203 | + ); |
|
204 | + } else { |
|
205 | + _e('This timezone does not observe daylight saving time.'); |
|
206 | + } |
|
207 | + } |
|
208 | + // Set back to UTC. |
|
209 | + date_default_timezone_set('UTC'); |
|
210 | + ?> |
|
211 | 211 | </span></p> |
212 | 212 | <?php |
213 | - endif; |
|
214 | - } |
|
213 | + endif; |
|
214 | + } |
|
215 | 215 | |
216 | 216 | |
217 | - /** |
|
218 | - * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
219 | - * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
220 | - * the site is used. |
|
221 | - * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
222 | - * computed timestamp (i.e. date_i18n() ) |
|
223 | - * |
|
224 | - * @param int $unix_timestamp if 0, then time() will be used. |
|
225 | - * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
226 | - * site will be used. |
|
227 | - * @return int unix_timestamp value with the offset applied for the given timezone. |
|
228 | - * @throws EE_Error |
|
229 | - */ |
|
230 | - public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = '') |
|
231 | - { |
|
232 | - $unix_timestamp = $unix_timestamp === 0 ? time() : (int) $unix_timestamp; |
|
233 | - $timezone_string = $this->getValidTimezoneString($timezone_string); |
|
234 | - $TimeZone = new DateTimeZone($timezone_string); |
|
235 | - $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
236 | - $offset = timezone_offset_get($TimeZone, $DateTime); |
|
237 | - return (int) $DateTime->format('U') + (int) $offset; |
|
238 | - } |
|
217 | + /** |
|
218 | + * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
219 | + * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
220 | + * the site is used. |
|
221 | + * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
222 | + * computed timestamp (i.e. date_i18n() ) |
|
223 | + * |
|
224 | + * @param int $unix_timestamp if 0, then time() will be used. |
|
225 | + * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
226 | + * site will be used. |
|
227 | + * @return int unix_timestamp value with the offset applied for the given timezone. |
|
228 | + * @throws EE_Error |
|
229 | + */ |
|
230 | + public function getTimestampWithOffset($unix_timestamp = 0, $timezone_string = '') |
|
231 | + { |
|
232 | + $unix_timestamp = $unix_timestamp === 0 ? time() : (int) $unix_timestamp; |
|
233 | + $timezone_string = $this->getValidTimezoneString($timezone_string); |
|
234 | + $TimeZone = new DateTimeZone($timezone_string); |
|
235 | + $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
236 | + $offset = timezone_offset_get($TimeZone, $DateTime); |
|
237 | + return (int) $DateTime->format('U') + (int) $offset; |
|
238 | + } |
|
239 | 239 | |
240 | 240 | |
241 | - /** |
|
242 | - * Get Timezone Transitions |
|
243 | - * |
|
244 | - * @param DateTimeZone $date_time_zone |
|
245 | - * @param int|null $time |
|
246 | - * @param bool $first_only |
|
247 | - * @return array|mixed |
|
248 | - */ |
|
249 | - public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true) |
|
250 | - { |
|
251 | - $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
|
252 | - $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
253 | - $transitions = $date_time_zone->getTransitions($time); |
|
254 | - return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
|
255 | - } |
|
241 | + /** |
|
242 | + * Get Timezone Transitions |
|
243 | + * |
|
244 | + * @param DateTimeZone $date_time_zone |
|
245 | + * @param int|null $time |
|
246 | + * @param bool $first_only |
|
247 | + * @return array|mixed |
|
248 | + */ |
|
249 | + public function getTimezoneTransitions(DateTimeZone $date_time_zone, $time = null, $first_only = true) |
|
250 | + { |
|
251 | + $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
|
252 | + $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
253 | + $transitions = $date_time_zone->getTransitions($time); |
|
254 | + return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
|
255 | + } |
|
256 | 256 | |
257 | 257 | |
258 | 258 | |
259 | - /** |
|
260 | - * Default to just returning the provided $gmt_offset. Children can override if adjustment needed. |
|
261 | - * |
|
262 | - * @param int $gmt_offset |
|
263 | - * @return int |
|
264 | - */ |
|
265 | - public function adjustInvalidGmtOffsets($gmt_offset = 0) |
|
266 | - { |
|
267 | - return $gmt_offset; |
|
268 | - } |
|
259 | + /** |
|
260 | + * Default to just returning the provided $gmt_offset. Children can override if adjustment needed. |
|
261 | + * |
|
262 | + * @param int $gmt_offset |
|
263 | + * @return int |
|
264 | + */ |
|
265 | + public function adjustInvalidGmtOffsets($gmt_offset = 0) |
|
266 | + { |
|
267 | + return $gmt_offset; |
|
268 | + } |
|
269 | 269 | |
270 | 270 | |
271 | 271 | |
272 | - /** |
|
273 | - * This receives an incoming gmt_offset and santizes it. If the provide value is an empty string, then this will |
|
274 | - * attempt to get the offset from the timezone string. If this returns a string, then a timezone string was |
|
275 | - * successfully derived from existing timezone_string in the db. If not, then a float is returned for the provided |
|
276 | - * offset. |
|
277 | - * @param float|string $gmt_offset |
|
278 | - * @return float|string |
|
279 | - */ |
|
280 | - protected function sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset) |
|
281 | - { |
|
282 | - //if there is no incoming gmt_offset, then because WP hooks in on timezone_string, we need to see if that is |
|
283 | - //set because it will override `gmt_offset` via `pre_get_option` filter. If that's set, then let's just use |
|
284 | - //that! Otherwise we'll leave timezone_string at the default of 'UTC' before doing other logic. |
|
285 | - if ($gmt_offset === '') { |
|
286 | - //autoloaded so no need to set to a variable. There will not be multiple hits to the db. |
|
287 | - if (get_option('timezone_string')) { |
|
288 | - return (string) get_option('timezone_string'); |
|
289 | - } |
|
290 | - } |
|
291 | - $gmt_offset = $gmt_offset !== '' ? $gmt_offset : (string) get_option('gmt_offset'); |
|
292 | - $gmt_offset = (float) $gmt_offset; |
|
293 | - //if $gmt_offset is 0 or is still an empty string, then just return UTC |
|
294 | - if ($gmt_offset === (float) 0) { |
|
295 | - return 'UTC'; |
|
296 | - } |
|
297 | - return $gmt_offset; |
|
298 | - } |
|
272 | + /** |
|
273 | + * This receives an incoming gmt_offset and santizes it. If the provide value is an empty string, then this will |
|
274 | + * attempt to get the offset from the timezone string. If this returns a string, then a timezone string was |
|
275 | + * successfully derived from existing timezone_string in the db. If not, then a float is returned for the provided |
|
276 | + * offset. |
|
277 | + * @param float|string $gmt_offset |
|
278 | + * @return float|string |
|
279 | + */ |
|
280 | + protected function sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset) |
|
281 | + { |
|
282 | + //if there is no incoming gmt_offset, then because WP hooks in on timezone_string, we need to see if that is |
|
283 | + //set because it will override `gmt_offset` via `pre_get_option` filter. If that's set, then let's just use |
|
284 | + //that! Otherwise we'll leave timezone_string at the default of 'UTC' before doing other logic. |
|
285 | + if ($gmt_offset === '') { |
|
286 | + //autoloaded so no need to set to a variable. There will not be multiple hits to the db. |
|
287 | + if (get_option('timezone_string')) { |
|
288 | + return (string) get_option('timezone_string'); |
|
289 | + } |
|
290 | + } |
|
291 | + $gmt_offset = $gmt_offset !== '' ? $gmt_offset : (string) get_option('gmt_offset'); |
|
292 | + $gmt_offset = (float) $gmt_offset; |
|
293 | + //if $gmt_offset is 0 or is still an empty string, then just return UTC |
|
294 | + if ($gmt_offset === (float) 0) { |
|
295 | + return 'UTC'; |
|
296 | + } |
|
297 | + return $gmt_offset; |
|
298 | + } |
|
299 | 299 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | new DateTimeZone($timezone_string); |
28 | 28 | } catch (Exception $e) { |
29 | 29 | // sometimes we take exception to exceptions |
30 | - if (! $throw_error) { |
|
30 | + if ( ! $throw_error) { |
|
31 | 31 | return false; |
32 | 32 | } |
33 | 33 | throw new EE_Error( |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | */ |
56 | 56 | public function getSiteTimezoneGmtOffset() |
57 | 57 | { |
58 | - $timezone_string = (string)get_option('timezone_string'); |
|
58 | + $timezone_string = (string) get_option('timezone_string'); |
|
59 | 59 | if ($timezone_string) { |
60 | 60 | try { |
61 | 61 | $timezone = new DateTimeZone($timezone_string); |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | public function getTimezoneOffset(DateTimeZone $date_time_zone, $time = null) |
80 | 80 | { |
81 | 81 | $transition = $this->getTimezoneTransitions($date_time_zone, $time); |
82 | - if (! isset($transition['offset'])) { |
|
82 | + if ( ! isset($transition['offset'])) { |
|
83 | 83 | throw new DomainException( |
84 | 84 | sprintf( |
85 | 85 | esc_html__('An invalid timezone transition was received %1$s', 'event_espresso'), |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | public function timezoneSelectInput($timezone_string = '') |
102 | 102 | { |
103 | 103 | // get WP date time format |
104 | - $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
104 | + $datetime_format = get_option('date_format').' '.get_option('time_format'); |
|
105 | 105 | // if passed a value, then use that, else get WP option |
106 | - $timezone_string = ! empty($timezone_string) ? $timezone_string : (string)get_option('timezone_string'); |
|
106 | + $timezone_string = ! empty($timezone_string) ? $timezone_string : (string) get_option('timezone_string'); |
|
107 | 107 | // check if the timezone is valid but don't throw any errors if it isn't |
108 | 108 | $timezone_string = $this->validateTimezone($timezone_string, false) |
109 | 109 | ? $timezone_string |
@@ -115,9 +115,9 @@ discard block |
||
115 | 115 | $timezone_string = 'UTC'; |
116 | 116 | $check_zone_info = false; |
117 | 117 | if ($gmt_offset > 0) { |
118 | - $timezone_string = 'UTC+' . $gmt_offset; |
|
118 | + $timezone_string = 'UTC+'.$gmt_offset; |
|
119 | 119 | } elseif ($gmt_offset < 0) { |
120 | - $timezone_string = 'UTC' . $gmt_offset; |
|
120 | + $timezone_string = 'UTC'.$gmt_offset; |
|
121 | 121 | } |
122 | 122 | } |
123 | 123 | ?> |
@@ -136,11 +136,11 @@ discard block |
||
136 | 136 | __('%1$sUTC%2$s time is %3$s'), |
137 | 137 | '<abbr title="Coordinated Universal Time">', |
138 | 138 | '</abbr>', |
139 | - '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
139 | + '<code>'.date_i18n($datetime_format, false, true).'</code>' |
|
140 | 140 | ); |
141 | 141 | ?></span> |
142 | - <?php if (! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
|
143 | - <br/><span><?php printf(__('Local time is %1$s'), '<code>' . date_i18n($datetime_format) . '</code>'); ?></span> |
|
142 | + <?php if ( ! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
|
143 | + <br/><span><?php printf(__('Local time is %1$s'), '<code>'.date_i18n($datetime_format).'</code>'); ?></span> |
|
144 | 144 | <?php endif; ?> |
145 | 145 | |
146 | 146 | <?php if ($check_zone_info && $timezone_string) : ?> |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | // transition time from date_i18n(). |
179 | 179 | printf( |
180 | 180 | $message, |
181 | - '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >' |
|
181 | + '<code >'.date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])).'</code >' |
|
182 | 182 | ); |
183 | 183 | } else { |
184 | 184 | _e('This timezone does not observe daylight saving time.'); |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | $unix_timestamp = $unix_timestamp === 0 ? time() : (int) $unix_timestamp; |
211 | 211 | $timezone_string = $this->getValidTimezoneString($timezone_string); |
212 | 212 | $TimeZone = new DateTimeZone($timezone_string); |
213 | - $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
213 | + $DateTime = new DateTime('@'.$unix_timestamp, $TimeZone); |
|
214 | 214 | $offset = timezone_offset_get($TimeZone, $DateTime); |
215 | 215 | return (int) $DateTime->format('U') + (int) $offset; |
216 | 216 | } |
@@ -8,91 +8,91 @@ |
||
8 | 8 | |
9 | 9 | class PhpCompatGreaterFiveSixHelper extends AbstractHelper |
10 | 10 | { |
11 | - /** |
|
12 | - * PhpCompatLessFiveSixHelper constructor. |
|
13 | - * |
|
14 | - * @throws DomainException |
|
15 | - */ |
|
16 | - public function __construct() |
|
17 | - { |
|
18 | - if (PHP_VERSION_ID < 50600) { |
|
19 | - throw new DomainException( |
|
20 | - sprintf( |
|
21 | - esc_html__( |
|
22 | - 'The %1$s is only usable on php versions greater than 5.6. You\'ll want to use %2$s instead.', |
|
23 | - 'event_espresso' |
|
24 | - ), |
|
25 | - __CLASS__, |
|
26 | - 'EventEspresso\core\services\helpers\datetime\PhpCompatLessFiveSixHelper' |
|
27 | - ) |
|
28 | - ); |
|
29 | - } |
|
30 | - } |
|
11 | + /** |
|
12 | + * PhpCompatLessFiveSixHelper constructor. |
|
13 | + * |
|
14 | + * @throws DomainException |
|
15 | + */ |
|
16 | + public function __construct() |
|
17 | + { |
|
18 | + if (PHP_VERSION_ID < 50600) { |
|
19 | + throw new DomainException( |
|
20 | + sprintf( |
|
21 | + esc_html__( |
|
22 | + 'The %1$s is only usable on php versions greater than 5.6. You\'ll want to use %2$s instead.', |
|
23 | + 'event_espresso' |
|
24 | + ), |
|
25 | + __CLASS__, |
|
26 | + 'EventEspresso\core\services\helpers\datetime\PhpCompatLessFiveSixHelper' |
|
27 | + ) |
|
28 | + ); |
|
29 | + } |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Returns a timezone string for the provided gmt_offset. |
|
34 | - * This is a valid timezone string that can be sent into DateTimeZone |
|
35 | - * |
|
36 | - * @param float|string $gmt_offset |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
|
40 | - { |
|
41 | - $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
|
42 | - return is_float($gmt_offset_or_timezone_string) |
|
43 | - ? $this->convertWpGmtOffsetForDateTimeZone($gmt_offset_or_timezone_string) |
|
44 | - : $gmt_offset_or_timezone_string; |
|
45 | - } |
|
32 | + /** |
|
33 | + * Returns a timezone string for the provided gmt_offset. |
|
34 | + * This is a valid timezone string that can be sent into DateTimeZone |
|
35 | + * |
|
36 | + * @param float|string $gmt_offset |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
|
40 | + { |
|
41 | + $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
|
42 | + return is_float($gmt_offset_or_timezone_string) |
|
43 | + ? $this->convertWpGmtOffsetForDateTimeZone($gmt_offset_or_timezone_string) |
|
44 | + : $gmt_offset_or_timezone_string; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * Returns a formatted offset for use as an argument for constructing DateTimeZone |
|
51 | - * @param float $gmt_offset This should be a float representing the gmt_offset. |
|
52 | - * @return string |
|
53 | - */ |
|
54 | - protected function convertWpGmtOffsetForDateTimeZone($gmt_offset) |
|
55 | - { |
|
56 | - $gmt_offset = (float) $gmt_offset; |
|
57 | - $is_negative = $gmt_offset < 0; |
|
58 | - $gmt_offset *= 100; |
|
59 | - $gmt_offset = absint($gmt_offset); |
|
60 | - //negative and need zero padding? |
|
61 | - if (strlen($gmt_offset) < 4) { |
|
62 | - $gmt_offset = str_pad($gmt_offset, 4, '0', STR_PAD_LEFT); |
|
63 | - } |
|
64 | - $gmt_offset = $this->convertToTimeFraction($gmt_offset); |
|
65 | - //return something like -1300, -0200 or +1300, +0200 |
|
66 | - return $is_negative ? '-' . $gmt_offset : '+' . $gmt_offset; |
|
67 | - } |
|
49 | + /** |
|
50 | + * Returns a formatted offset for use as an argument for constructing DateTimeZone |
|
51 | + * @param float $gmt_offset This should be a float representing the gmt_offset. |
|
52 | + * @return string |
|
53 | + */ |
|
54 | + protected function convertWpGmtOffsetForDateTimeZone($gmt_offset) |
|
55 | + { |
|
56 | + $gmt_offset = (float) $gmt_offset; |
|
57 | + $is_negative = $gmt_offset < 0; |
|
58 | + $gmt_offset *= 100; |
|
59 | + $gmt_offset = absint($gmt_offset); |
|
60 | + //negative and need zero padding? |
|
61 | + if (strlen($gmt_offset) < 4) { |
|
62 | + $gmt_offset = str_pad($gmt_offset, 4, '0', STR_PAD_LEFT); |
|
63 | + } |
|
64 | + $gmt_offset = $this->convertToTimeFraction($gmt_offset); |
|
65 | + //return something like -1300, -0200 or +1300, +0200 |
|
66 | + return $is_negative ? '-' . $gmt_offset : '+' . $gmt_offset; |
|
67 | + } |
|
68 | 68 | |
69 | 69 | |
70 | - /** |
|
71 | - * Converts something like `1550` to `1530` or `0275` to `0245` |
|
72 | - * Incoming offset should be a positive value, this will mutate negative values. Be aware! |
|
73 | - * @param int $offset |
|
74 | - * @return mixed |
|
75 | - */ |
|
76 | - protected function convertToTimeFraction($offset) |
|
77 | - { |
|
78 | - $first_part = substr($offset, 0, 2); |
|
79 | - $second_part = substr($offset, 2, 2); |
|
80 | - $second_part = str_replace(array('25', '50', '75'), array('15', '30', '45'), $second_part); |
|
81 | - return $first_part . $second_part; |
|
82 | - } |
|
70 | + /** |
|
71 | + * Converts something like `1550` to `1530` or `0275` to `0245` |
|
72 | + * Incoming offset should be a positive value, this will mutate negative values. Be aware! |
|
73 | + * @param int $offset |
|
74 | + * @return mixed |
|
75 | + */ |
|
76 | + protected function convertToTimeFraction($offset) |
|
77 | + { |
|
78 | + $first_part = substr($offset, 0, 2); |
|
79 | + $second_part = substr($offset, 2, 2); |
|
80 | + $second_part = str_replace(array('25', '50', '75'), array('15', '30', '45'), $second_part); |
|
81 | + return $first_part . $second_part; |
|
82 | + } |
|
83 | 83 | |
84 | 84 | |
85 | - /** |
|
86 | - * Get Timezone offset for given timezone object |
|
87 | - * |
|
88 | - * @param DateTimeZone $date_time_zone |
|
89 | - * @param null|int $time |
|
90 | - * @return int |
|
91 | - */ |
|
92 | - public function getTimezoneOffset(DateTimezone $date_time_zone, $time = NULL) |
|
93 | - { |
|
94 | - $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
|
95 | - $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
96 | - return $date_time_zone->getOffset(new DateTime('@' . $time)); |
|
97 | - } |
|
85 | + /** |
|
86 | + * Get Timezone offset for given timezone object |
|
87 | + * |
|
88 | + * @param DateTimeZone $date_time_zone |
|
89 | + * @param null|int $time |
|
90 | + * @return int |
|
91 | + */ |
|
92 | + public function getTimezoneOffset(DateTimezone $date_time_zone, $time = NULL) |
|
93 | + { |
|
94 | + $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
|
95 | + $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
96 | + return $date_time_zone->getOffset(new DateTime('@' . $time)); |
|
97 | + } |
|
98 | 98 | } |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | } |
64 | 64 | $gmt_offset = $this->convertToTimeFraction($gmt_offset); |
65 | 65 | //return something like -1300, -0200 or +1300, +0200 |
66 | - return $is_negative ? '-' . $gmt_offset : '+' . $gmt_offset; |
|
66 | + return $is_negative ? '-'.$gmt_offset : '+'.$gmt_offset; |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | $first_part = substr($offset, 0, 2); |
79 | 79 | $second_part = substr($offset, 2, 2); |
80 | 80 | $second_part = str_replace(array('25', '50', '75'), array('15', '30', '45'), $second_part); |
81 | - return $first_part . $second_part; |
|
81 | + return $first_part.$second_part; |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | |
@@ -93,6 +93,6 @@ discard block |
||
93 | 93 | { |
94 | 94 | $time = is_int($time) || $time === null ? $time : (int) strtotime($time); |
95 | 95 | $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
96 | - return $date_time_zone->getOffset(new DateTime('@' . $time)); |
|
96 | + return $date_time_zone->getOffset(new DateTime('@'.$time)); |
|
97 | 97 | } |
98 | 98 | } |
@@ -18,221 +18,221 @@ |
||
18 | 18 | class PhpCompatLessFiveSixHelper extends AbstractHelper |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * PhpCompatLessFiveSixHelper constructor. |
|
23 | - * |
|
24 | - * @throws DomainException |
|
25 | - */ |
|
26 | - public function __construct() |
|
27 | - { |
|
28 | - if (PHP_VERSION_ID >= 50600) { |
|
29 | - throw new DomainException( |
|
30 | - sprintf( |
|
31 | - esc_html__( |
|
32 | - 'The %1$s is only usable on php versions less than 5.6. You\'ll want to use %2$s instead.', |
|
33 | - 'event_espresso' |
|
34 | - ), |
|
35 | - __CLASS__, |
|
36 | - 'EventEspresso\core\services\helpers\datetime\PhpCompatGreaterFiveSixHelper' |
|
37 | - ) |
|
38 | - ); |
|
39 | - } |
|
40 | - } |
|
21 | + /** |
|
22 | + * PhpCompatLessFiveSixHelper constructor. |
|
23 | + * |
|
24 | + * @throws DomainException |
|
25 | + */ |
|
26 | + public function __construct() |
|
27 | + { |
|
28 | + if (PHP_VERSION_ID >= 50600) { |
|
29 | + throw new DomainException( |
|
30 | + sprintf( |
|
31 | + esc_html__( |
|
32 | + 'The %1$s is only usable on php versions less than 5.6. You\'ll want to use %2$s instead.', |
|
33 | + 'event_espresso' |
|
34 | + ), |
|
35 | + __CLASS__, |
|
36 | + 'EventEspresso\core\services\helpers\datetime\PhpCompatGreaterFiveSixHelper' |
|
37 | + ) |
|
38 | + ); |
|
39 | + } |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Returns a timezone string for the provided gmt_offset. |
|
44 | - * |
|
45 | - * @param float|string $gmt_offset |
|
46 | - * @return string |
|
47 | - * @throws EE_Error |
|
48 | - */ |
|
49 | - public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
|
50 | - { |
|
51 | - $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
|
52 | - if (is_string($gmt_offset_or_timezone_string)) { |
|
53 | - return $gmt_offset_or_timezone_string; |
|
54 | - } |
|
55 | - //well we know its a float, so let's roll with it. |
|
56 | - $gmt_offset = $gmt_offset_or_timezone_string; |
|
57 | - // convert GMT offset to seconds |
|
58 | - $gmt_offset *= HOUR_IN_SECONDS; |
|
59 | - // although we don't know the TZ abbreviation, we know the UTC offset |
|
60 | - $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
|
61 | - //only use this timezone_string IF it's current offset matches the given offset |
|
62 | - if (! empty($timezone_string)) { |
|
63 | - $offset = null; |
|
64 | - try { |
|
65 | - $offset = $this->getTimezoneOffset(new DateTimeZone($timezone_string)); |
|
66 | - if ($offset !== $gmt_offset) { |
|
67 | - $timezone_string = false; |
|
68 | - } |
|
69 | - } catch (Exception $e) { |
|
70 | - $timezone_string = false; |
|
71 | - } |
|
72 | - } |
|
73 | - // better have a valid timezone string by now, but if not, sigh... loop thru the timezone_abbreviations_list() |
|
74 | - //... |
|
75 | - $timezone_string = $timezone_string !== false |
|
76 | - ? $timezone_string |
|
77 | - : $this->getTimezoneStringFromAbbreviationsList($gmt_offset); |
|
78 | - return $timezone_string; |
|
79 | - } |
|
42 | + /** |
|
43 | + * Returns a timezone string for the provided gmt_offset. |
|
44 | + * |
|
45 | + * @param float|string $gmt_offset |
|
46 | + * @return string |
|
47 | + * @throws EE_Error |
|
48 | + */ |
|
49 | + public function getTimezoneStringFromGmtOffset($gmt_offset = '') |
|
50 | + { |
|
51 | + $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset); |
|
52 | + if (is_string($gmt_offset_or_timezone_string)) { |
|
53 | + return $gmt_offset_or_timezone_string; |
|
54 | + } |
|
55 | + //well we know its a float, so let's roll with it. |
|
56 | + $gmt_offset = $gmt_offset_or_timezone_string; |
|
57 | + // convert GMT offset to seconds |
|
58 | + $gmt_offset *= HOUR_IN_SECONDS; |
|
59 | + // although we don't know the TZ abbreviation, we know the UTC offset |
|
60 | + $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
|
61 | + //only use this timezone_string IF it's current offset matches the given offset |
|
62 | + if (! empty($timezone_string)) { |
|
63 | + $offset = null; |
|
64 | + try { |
|
65 | + $offset = $this->getTimezoneOffset(new DateTimeZone($timezone_string)); |
|
66 | + if ($offset !== $gmt_offset) { |
|
67 | + $timezone_string = false; |
|
68 | + } |
|
69 | + } catch (Exception $e) { |
|
70 | + $timezone_string = false; |
|
71 | + } |
|
72 | + } |
|
73 | + // better have a valid timezone string by now, but if not, sigh... loop thru the timezone_abbreviations_list() |
|
74 | + //... |
|
75 | + $timezone_string = $timezone_string !== false |
|
76 | + ? $timezone_string |
|
77 | + : $this->getTimezoneStringFromAbbreviationsList($gmt_offset); |
|
78 | + return $timezone_string; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * @param int $gmt_offset |
|
84 | - * @param bool $coerce If true, we attempt to coerce with our adjustment table |
|
85 | - * @see self::adjustInvalidGmtOffset |
|
86 | - * @return string |
|
87 | - * @throws EE_Error |
|
88 | - */ |
|
89 | - protected function getTimezoneStringFromAbbreviationsList($gmt_offset = 0, $coerce = true) |
|
90 | - { |
|
91 | - $gmt_offset = (int)$gmt_offset; |
|
92 | - /** @var array[] $abbreviations */ |
|
93 | - $abbreviations = DateTimeZone::listAbbreviations(); |
|
94 | - foreach ($abbreviations as $abbreviation) { |
|
95 | - foreach ($abbreviation as $timezone) { |
|
96 | - if ((int)$timezone['offset'] === $gmt_offset && (bool)$timezone['dst'] === false) { |
|
97 | - try { |
|
98 | - $offset = $this->getTimezoneOffset(new DateTimeZone($timezone['timezone_id'])); |
|
99 | - if ($offset !== $gmt_offset) { |
|
100 | - continue; |
|
101 | - } |
|
102 | - return $timezone['timezone_id']; |
|
103 | - } catch (Exception $e) { |
|
104 | - continue; |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
108 | - } |
|
109 | - //if $coerce is true, let's see if we can get a timezone string after the offset is adjusted |
|
110 | - if ($coerce === true) { |
|
111 | - $timezone_string = $this->getTimezoneStringFromAbbreviationsList( |
|
112 | - $this->adjustInvalidGmtOffsets($gmt_offset), |
|
113 | - false |
|
114 | - ); |
|
115 | - if ($timezone_string) { |
|
116 | - return $timezone_string; |
|
117 | - } |
|
118 | - } |
|
119 | - throw new EE_Error( |
|
120 | - sprintf( |
|
121 | - esc_html__( |
|
122 | - 'The provided GMT offset (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
123 | - 'event_espresso' |
|
124 | - ), |
|
125 | - $gmt_offset / HOUR_IN_SECONDS, |
|
126 | - '<a href="http://www.php.net/manual/en/timezones.php">', |
|
127 | - '</a>' |
|
128 | - ) |
|
129 | - ); |
|
130 | - } |
|
82 | + /** |
|
83 | + * @param int $gmt_offset |
|
84 | + * @param bool $coerce If true, we attempt to coerce with our adjustment table |
|
85 | + * @see self::adjustInvalidGmtOffset |
|
86 | + * @return string |
|
87 | + * @throws EE_Error |
|
88 | + */ |
|
89 | + protected function getTimezoneStringFromAbbreviationsList($gmt_offset = 0, $coerce = true) |
|
90 | + { |
|
91 | + $gmt_offset = (int)$gmt_offset; |
|
92 | + /** @var array[] $abbreviations */ |
|
93 | + $abbreviations = DateTimeZone::listAbbreviations(); |
|
94 | + foreach ($abbreviations as $abbreviation) { |
|
95 | + foreach ($abbreviation as $timezone) { |
|
96 | + if ((int)$timezone['offset'] === $gmt_offset && (bool)$timezone['dst'] === false) { |
|
97 | + try { |
|
98 | + $offset = $this->getTimezoneOffset(new DateTimeZone($timezone['timezone_id'])); |
|
99 | + if ($offset !== $gmt_offset) { |
|
100 | + continue; |
|
101 | + } |
|
102 | + return $timezone['timezone_id']; |
|
103 | + } catch (Exception $e) { |
|
104 | + continue; |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | + } |
|
109 | + //if $coerce is true, let's see if we can get a timezone string after the offset is adjusted |
|
110 | + if ($coerce === true) { |
|
111 | + $timezone_string = $this->getTimezoneStringFromAbbreviationsList( |
|
112 | + $this->adjustInvalidGmtOffsets($gmt_offset), |
|
113 | + false |
|
114 | + ); |
|
115 | + if ($timezone_string) { |
|
116 | + return $timezone_string; |
|
117 | + } |
|
118 | + } |
|
119 | + throw new EE_Error( |
|
120 | + sprintf( |
|
121 | + esc_html__( |
|
122 | + 'The provided GMT offset (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
123 | + 'event_espresso' |
|
124 | + ), |
|
125 | + $gmt_offset / HOUR_IN_SECONDS, |
|
126 | + '<a href="http://www.php.net/manual/en/timezones.php">', |
|
127 | + '</a>' |
|
128 | + ) |
|
129 | + ); |
|
130 | + } |
|
131 | 131 | |
132 | 132 | |
133 | - /** |
|
134 | - * Depending on PHP version, |
|
135 | - * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
|
136 | - * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
137 | - * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
138 | - * |
|
139 | - * @param int $gmt_offset |
|
140 | - * @return int |
|
141 | - */ |
|
142 | - public function adjustInvalidGmtOffsets($gmt_offset = 0) |
|
143 | - { |
|
144 | - //make sure $gmt_offset is int |
|
145 | - $gmt_offset = (int) $gmt_offset; |
|
146 | - switch ($gmt_offset) { |
|
147 | - //-12 |
|
148 | - case -43200: |
|
149 | - $gmt_offset = -39600; |
|
150 | - break; |
|
151 | - //-11.5 |
|
152 | - case -41400: |
|
153 | - $gmt_offset = -39600; |
|
154 | - break; |
|
155 | - //-10.5 |
|
156 | - case -37800: |
|
157 | - $gmt_offset = -39600; |
|
158 | - break; |
|
159 | - //-8.5 |
|
160 | - case -30600: |
|
161 | - $gmt_offset = -28800; |
|
162 | - break; |
|
163 | - //-7.5 |
|
164 | - case -27000: |
|
165 | - $gmt_offset = -25200; |
|
166 | - break; |
|
167 | - //-6.5 |
|
168 | - case -23400: |
|
169 | - $gmt_offset = -21600; |
|
170 | - break; |
|
171 | - //-5.5 |
|
172 | - case -19800: |
|
173 | - $gmt_offset = -18000; |
|
174 | - break; |
|
175 | - //-4.5 |
|
176 | - case -16200: |
|
177 | - $gmt_offset = -14400; |
|
178 | - break; |
|
179 | - //-3.5 |
|
180 | - case -12600: |
|
181 | - $gmt_offset = -10800; |
|
182 | - break; |
|
183 | - //-2.5 |
|
184 | - case -9000: |
|
185 | - $gmt_offset = -7200; |
|
186 | - break; |
|
187 | - //-1.5 |
|
188 | - case -5400: |
|
189 | - $gmt_offset = -3600; |
|
190 | - break; |
|
191 | - //-0.5 |
|
192 | - case -1800: |
|
193 | - $gmt_offset = 0; |
|
194 | - break; |
|
195 | - //.5 |
|
196 | - case 1800: |
|
197 | - $gmt_offset = 3600; |
|
198 | - break; |
|
199 | - //1.5 |
|
200 | - case 5400: |
|
201 | - $gmt_offset = 7200; |
|
202 | - break; |
|
203 | - //2.5 |
|
204 | - case 9000: |
|
205 | - $gmt_offset = 10800; |
|
206 | - break; |
|
207 | - //3.5 |
|
208 | - case 12600: |
|
209 | - $gmt_offset = 14400; |
|
210 | - break; |
|
211 | - //7.5 |
|
212 | - case 27000: |
|
213 | - $gmt_offset = 28800; |
|
214 | - break; |
|
215 | - //8.5 |
|
216 | - case 30600: |
|
217 | - $gmt_offset = 31500; |
|
218 | - break; |
|
219 | - //10.5 |
|
220 | - case 37800: |
|
221 | - $gmt_offset = 39600; |
|
222 | - break; |
|
223 | - //11.5 |
|
224 | - case 41400: |
|
225 | - $gmt_offset = 43200; |
|
226 | - break; |
|
227 | - //12.75 |
|
228 | - case 45900: |
|
229 | - $gmt_offset = 46800; |
|
230 | - break; |
|
231 | - //13.75 |
|
232 | - case 49500: |
|
233 | - $gmt_offset = 50400; |
|
234 | - break; |
|
235 | - } |
|
236 | - return $gmt_offset; |
|
237 | - } |
|
133 | + /** |
|
134 | + * Depending on PHP version, |
|
135 | + * there might not be valid current timezone strings to match these gmt_offsets in its timezone tables. |
|
136 | + * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
137 | + * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
138 | + * |
|
139 | + * @param int $gmt_offset |
|
140 | + * @return int |
|
141 | + */ |
|
142 | + public function adjustInvalidGmtOffsets($gmt_offset = 0) |
|
143 | + { |
|
144 | + //make sure $gmt_offset is int |
|
145 | + $gmt_offset = (int) $gmt_offset; |
|
146 | + switch ($gmt_offset) { |
|
147 | + //-12 |
|
148 | + case -43200: |
|
149 | + $gmt_offset = -39600; |
|
150 | + break; |
|
151 | + //-11.5 |
|
152 | + case -41400: |
|
153 | + $gmt_offset = -39600; |
|
154 | + break; |
|
155 | + //-10.5 |
|
156 | + case -37800: |
|
157 | + $gmt_offset = -39600; |
|
158 | + break; |
|
159 | + //-8.5 |
|
160 | + case -30600: |
|
161 | + $gmt_offset = -28800; |
|
162 | + break; |
|
163 | + //-7.5 |
|
164 | + case -27000: |
|
165 | + $gmt_offset = -25200; |
|
166 | + break; |
|
167 | + //-6.5 |
|
168 | + case -23400: |
|
169 | + $gmt_offset = -21600; |
|
170 | + break; |
|
171 | + //-5.5 |
|
172 | + case -19800: |
|
173 | + $gmt_offset = -18000; |
|
174 | + break; |
|
175 | + //-4.5 |
|
176 | + case -16200: |
|
177 | + $gmt_offset = -14400; |
|
178 | + break; |
|
179 | + //-3.5 |
|
180 | + case -12600: |
|
181 | + $gmt_offset = -10800; |
|
182 | + break; |
|
183 | + //-2.5 |
|
184 | + case -9000: |
|
185 | + $gmt_offset = -7200; |
|
186 | + break; |
|
187 | + //-1.5 |
|
188 | + case -5400: |
|
189 | + $gmt_offset = -3600; |
|
190 | + break; |
|
191 | + //-0.5 |
|
192 | + case -1800: |
|
193 | + $gmt_offset = 0; |
|
194 | + break; |
|
195 | + //.5 |
|
196 | + case 1800: |
|
197 | + $gmt_offset = 3600; |
|
198 | + break; |
|
199 | + //1.5 |
|
200 | + case 5400: |
|
201 | + $gmt_offset = 7200; |
|
202 | + break; |
|
203 | + //2.5 |
|
204 | + case 9000: |
|
205 | + $gmt_offset = 10800; |
|
206 | + break; |
|
207 | + //3.5 |
|
208 | + case 12600: |
|
209 | + $gmt_offset = 14400; |
|
210 | + break; |
|
211 | + //7.5 |
|
212 | + case 27000: |
|
213 | + $gmt_offset = 28800; |
|
214 | + break; |
|
215 | + //8.5 |
|
216 | + case 30600: |
|
217 | + $gmt_offset = 31500; |
|
218 | + break; |
|
219 | + //10.5 |
|
220 | + case 37800: |
|
221 | + $gmt_offset = 39600; |
|
222 | + break; |
|
223 | + //11.5 |
|
224 | + case 41400: |
|
225 | + $gmt_offset = 43200; |
|
226 | + break; |
|
227 | + //12.75 |
|
228 | + case 45900: |
|
229 | + $gmt_offset = 46800; |
|
230 | + break; |
|
231 | + //13.75 |
|
232 | + case 49500: |
|
233 | + $gmt_offset = 50400; |
|
234 | + break; |
|
235 | + } |
|
236 | + return $gmt_offset; |
|
237 | + } |
|
238 | 238 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | // although we don't know the TZ abbreviation, we know the UTC offset |
60 | 60 | $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
61 | 61 | //only use this timezone_string IF it's current offset matches the given offset |
62 | - if (! empty($timezone_string)) { |
|
62 | + if ( ! empty($timezone_string)) { |
|
63 | 63 | $offset = null; |
64 | 64 | try { |
65 | 65 | $offset = $this->getTimezoneOffset(new DateTimeZone($timezone_string)); |
@@ -88,12 +88,12 @@ discard block |
||
88 | 88 | */ |
89 | 89 | protected function getTimezoneStringFromAbbreviationsList($gmt_offset = 0, $coerce = true) |
90 | 90 | { |
91 | - $gmt_offset = (int)$gmt_offset; |
|
91 | + $gmt_offset = (int) $gmt_offset; |
|
92 | 92 | /** @var array[] $abbreviations */ |
93 | 93 | $abbreviations = DateTimeZone::listAbbreviations(); |
94 | 94 | foreach ($abbreviations as $abbreviation) { |
95 | 95 | foreach ($abbreviation as $timezone) { |
96 | - if ((int)$timezone['offset'] === $gmt_offset && (bool)$timezone['dst'] === false) { |
|
96 | + if ((int) $timezone['offset'] === $gmt_offset && (bool) $timezone['dst'] === false) { |
|
97 | 97 | try { |
98 | 98 | $offset = $this->getTimezoneOffset(new DateTimeZone($timezone['timezone_id'])); |
99 | 99 | if ($offset !== $gmt_offset) { |
@@ -21,196 +21,196 @@ |
||
21 | 21 | class Url |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * @var string $scheme |
|
26 | - */ |
|
27 | - private $scheme; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var string $host |
|
31 | - */ |
|
32 | - private $host; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string $path |
|
36 | - */ |
|
37 | - private $path; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var string $query |
|
41 | - */ |
|
42 | - private $query; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var string $fragment |
|
46 | - */ |
|
47 | - private $fragment; |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Url constructor. |
|
52 | - * |
|
53 | - * @param $url |
|
54 | - * @throws InvalidArgumentException |
|
55 | - */ |
|
56 | - public function __construct($url) |
|
57 | - { |
|
58 | - if ( |
|
59 | - ! filter_var( |
|
60 | - $url, |
|
61 | - FILTER_VALIDATE_URL, |
|
62 | - array(FILTER_FLAG_SCHEME_REQUIRED, FILTER_FLAG_HOST_REQUIRED) |
|
63 | - ) |
|
64 | - ) { |
|
65 | - throw new InvalidArgumentException(esc_html__('Invalid URL. Both the "Scheme" and "Host" are required.', |
|
66 | - 'event_espresso')); |
|
67 | - } |
|
68 | - $url = parse_url($url); |
|
69 | - $this->setScheme($url); |
|
70 | - $this->setHost($url); |
|
71 | - $this->setPath($url); |
|
72 | - $this->setQuery($url); |
|
73 | - $this->setFragment($url); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
79 | - * will return a string like: 'abc://' |
|
80 | - * |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public function scheme() |
|
84 | - { |
|
85 | - return $this->scheme; |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * @param array $url |
|
91 | - */ |
|
92 | - private function setScheme($url) |
|
93 | - { |
|
94 | - $this->scheme = $url['scheme'] . '://'; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
100 | - * will return a string like: 'example.com' |
|
101 | - * |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - public function host() |
|
105 | - { |
|
106 | - return $this->host; |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * @param array $url |
|
112 | - */ |
|
113 | - private function setHost($url) |
|
114 | - { |
|
115 | - $this->host = $url['host']; |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
121 | - * will return a string like: '/path/data' |
|
122 | - * |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - public function path() |
|
126 | - { |
|
127 | - return $this->path; |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * @param array $url |
|
133 | - */ |
|
134 | - private function setPath($url) |
|
135 | - { |
|
136 | - $this->path = isset($url['path']) ? $url['path'] : ''; |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
142 | - * will return a string like: '?key=value' |
|
143 | - * |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - public function queryString() |
|
147 | - { |
|
148 | - return $this->query !== '' ? '?' . $this->query : ''; |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
154 | - * will return an array like: array('key' => 'value') |
|
155 | - * |
|
156 | - * @return array |
|
157 | - */ |
|
158 | - public function queryParams() |
|
159 | - { |
|
160 | - return wp_parse_args($this->query); |
|
161 | - } |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * @param array $url |
|
166 | - */ |
|
167 | - private function setQuery($url) |
|
168 | - { |
|
169 | - $this->query = isset($url['query']) ? $url['query'] : ''; |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
175 | - * will return a string like: '#id' |
|
176 | - * |
|
177 | - * @return string |
|
178 | - */ |
|
179 | - public function fragment() |
|
180 | - { |
|
181 | - return $this->fragment !== '' ? '#' . $this->fragment : ''; |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * @param array $url |
|
187 | - */ |
|
188 | - private function setFragment($url) |
|
189 | - { |
|
190 | - $this->fragment = isset($url['fragment']) ? $url['fragment'] : ''; |
|
191 | - } |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
196 | - * will return a string like: 'abc://example.com/path/data?key=value#id' |
|
197 | - * |
|
198 | - * @return string |
|
199 | - */ |
|
200 | - public function getFullUrl() |
|
201 | - { |
|
202 | - return $this->scheme() . $this->host() . $this->path() . $this->queryString() . $this->fragment(); |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
208 | - * will return a string like: 'abc://example.com/path/data?key=value#id' |
|
209 | - * |
|
210 | - * @return string |
|
211 | - */ |
|
212 | - public function __toString() |
|
213 | - { |
|
214 | - return $this->getFullUrl(); |
|
215 | - } |
|
24 | + /** |
|
25 | + * @var string $scheme |
|
26 | + */ |
|
27 | + private $scheme; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var string $host |
|
31 | + */ |
|
32 | + private $host; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string $path |
|
36 | + */ |
|
37 | + private $path; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var string $query |
|
41 | + */ |
|
42 | + private $query; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var string $fragment |
|
46 | + */ |
|
47 | + private $fragment; |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Url constructor. |
|
52 | + * |
|
53 | + * @param $url |
|
54 | + * @throws InvalidArgumentException |
|
55 | + */ |
|
56 | + public function __construct($url) |
|
57 | + { |
|
58 | + if ( |
|
59 | + ! filter_var( |
|
60 | + $url, |
|
61 | + FILTER_VALIDATE_URL, |
|
62 | + array(FILTER_FLAG_SCHEME_REQUIRED, FILTER_FLAG_HOST_REQUIRED) |
|
63 | + ) |
|
64 | + ) { |
|
65 | + throw new InvalidArgumentException(esc_html__('Invalid URL. Both the "Scheme" and "Host" are required.', |
|
66 | + 'event_espresso')); |
|
67 | + } |
|
68 | + $url = parse_url($url); |
|
69 | + $this->setScheme($url); |
|
70 | + $this->setHost($url); |
|
71 | + $this->setPath($url); |
|
72 | + $this->setQuery($url); |
|
73 | + $this->setFragment($url); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
79 | + * will return a string like: 'abc://' |
|
80 | + * |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public function scheme() |
|
84 | + { |
|
85 | + return $this->scheme; |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * @param array $url |
|
91 | + */ |
|
92 | + private function setScheme($url) |
|
93 | + { |
|
94 | + $this->scheme = $url['scheme'] . '://'; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
100 | + * will return a string like: 'example.com' |
|
101 | + * |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + public function host() |
|
105 | + { |
|
106 | + return $this->host; |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * @param array $url |
|
112 | + */ |
|
113 | + private function setHost($url) |
|
114 | + { |
|
115 | + $this->host = $url['host']; |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
121 | + * will return a string like: '/path/data' |
|
122 | + * |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + public function path() |
|
126 | + { |
|
127 | + return $this->path; |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * @param array $url |
|
133 | + */ |
|
134 | + private function setPath($url) |
|
135 | + { |
|
136 | + $this->path = isset($url['path']) ? $url['path'] : ''; |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
142 | + * will return a string like: '?key=value' |
|
143 | + * |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + public function queryString() |
|
147 | + { |
|
148 | + return $this->query !== '' ? '?' . $this->query : ''; |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
154 | + * will return an array like: array('key' => 'value') |
|
155 | + * |
|
156 | + * @return array |
|
157 | + */ |
|
158 | + public function queryParams() |
|
159 | + { |
|
160 | + return wp_parse_args($this->query); |
|
161 | + } |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * @param array $url |
|
166 | + */ |
|
167 | + private function setQuery($url) |
|
168 | + { |
|
169 | + $this->query = isset($url['query']) ? $url['query'] : ''; |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
175 | + * will return a string like: '#id' |
|
176 | + * |
|
177 | + * @return string |
|
178 | + */ |
|
179 | + public function fragment() |
|
180 | + { |
|
181 | + return $this->fragment !== '' ? '#' . $this->fragment : ''; |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * @param array $url |
|
187 | + */ |
|
188 | + private function setFragment($url) |
|
189 | + { |
|
190 | + $this->fragment = isset($url['fragment']) ? $url['fragment'] : ''; |
|
191 | + } |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
196 | + * will return a string like: 'abc://example.com/path/data?key=value#id' |
|
197 | + * |
|
198 | + * @return string |
|
199 | + */ |
|
200 | + public function getFullUrl() |
|
201 | + { |
|
202 | + return $this->scheme() . $this->host() . $this->path() . $this->queryString() . $this->fragment(); |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * For a URL like: abc://username:[email protected]:123/path/data?key=value#id |
|
208 | + * will return a string like: 'abc://example.com/path/data?key=value#id' |
|
209 | + * |
|
210 | + * @return string |
|
211 | + */ |
|
212 | + public function __toString() |
|
213 | + { |
|
214 | + return $this->getFullUrl(); |
|
215 | + } |
|
216 | 216 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | */ |
92 | 92 | private function setScheme($url) |
93 | 93 | { |
94 | - $this->scheme = $url['scheme'] . '://'; |
|
94 | + $this->scheme = $url['scheme'].'://'; |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | */ |
146 | 146 | public function queryString() |
147 | 147 | { |
148 | - return $this->query !== '' ? '?' . $this->query : ''; |
|
148 | + return $this->query !== '' ? '?'.$this->query : ''; |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | */ |
179 | 179 | public function fragment() |
180 | 180 | { |
181 | - return $this->fragment !== '' ? '#' . $this->fragment : ''; |
|
181 | + return $this->fragment !== '' ? '#'.$this->fragment : ''; |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | */ |
200 | 200 | public function getFullUrl() |
201 | 201 | { |
202 | - return $this->scheme() . $this->host() . $this->path() . $this->queryString() . $this->fragment(); |
|
202 | + return $this->scheme().$this->host().$this->path().$this->queryString().$this->fragment(); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 |
@@ -22,148 +22,148 @@ |
||
22 | 22 | class ContextChecker |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * A unique string used to identify where this ContextChecker is being employed |
|
27 | - * Is currently only used within the hook name for the filterable return value of isAllowed(). |
|
28 | - * |
|
29 | - * @var string $identifier |
|
30 | - */ |
|
31 | - private $identifier; |
|
32 | - |
|
33 | - /** |
|
34 | - * A list of values to be compared against the slug of the Context class passed to isAllowed() |
|
35 | - * |
|
36 | - * @var array $acceptable_values |
|
37 | - */ |
|
38 | - private $acceptable_values; |
|
39 | - |
|
40 | - /** |
|
41 | - * Closure that will be called to perform the evaluation within isAllowed(). |
|
42 | - * If none is provided, then a simple type sensitive in_array() check will be used |
|
43 | - * and return true if the incoming Context::slug() is found within the array of $acceptable_values. |
|
44 | - * |
|
45 | - * @var Closure $evaluation_callback |
|
46 | - */ |
|
47 | - private $evaluation_callback; |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * ContextChecker constructor. |
|
52 | - * |
|
53 | - * @param string $identifier |
|
54 | - * @param array $acceptable_values |
|
55 | - * @param Closure|null $evaluation_callback [optional] |
|
56 | - */ |
|
57 | - public function __construct($identifier, array $acceptable_values, Closure $evaluation_callback = null) |
|
58 | - { |
|
59 | - $this->setIdentifier($identifier); |
|
60 | - $this->setAcceptableValues($acceptable_values); |
|
61 | - $this->setEvaluationCallback($evaluation_callback); |
|
62 | - } |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * @param string $identifier |
|
67 | - */ |
|
68 | - private function setIdentifier($identifier) |
|
69 | - { |
|
70 | - $this->identifier = sanitize_key($identifier); |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @param array $acceptable_values |
|
76 | - */ |
|
77 | - private function setAcceptableValues(array $acceptable_values) |
|
78 | - { |
|
79 | - $this->acceptable_values = $acceptable_values; |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * @param Closure $evaluation_callback |
|
85 | - */ |
|
86 | - private function setEvaluationCallback(Closure $evaluation_callback = null) |
|
87 | - { |
|
88 | - $this->evaluation_callback = $evaluation_callback instanceof Closure |
|
89 | - ? $evaluation_callback |
|
90 | - : function (ContextInterface $context, $acceptable_values) { |
|
91 | - return in_array($context->slug(), $acceptable_values, true); |
|
92 | - }; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * @return string |
|
98 | - */ |
|
99 | - protected function identifier() |
|
100 | - { |
|
101 | - return $this->identifier; |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * @return array |
|
107 | - */ |
|
108 | - protected function acceptableValues() |
|
109 | - { |
|
110 | - return apply_filters( |
|
111 | - "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__{$this->identifier}__acceptableValues", |
|
112 | - $this->acceptable_values |
|
113 | - ); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * @return Closure |
|
119 | - */ |
|
120 | - protected function evaluationCallback() |
|
121 | - { |
|
122 | - return $this->evaluation_callback; |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns true if the incoming Context class slug matches one of the preset acceptable values. |
|
129 | - * The result is filterable using the identifier for this ContextChecker. |
|
130 | - * example: |
|
131 | - * If this ContextChecker's $identifier was set to "registration-checkout-type", |
|
132 | - * then the filter here would be named: |
|
133 | - * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed". |
|
134 | - * Other code could hook into the filter in isAllowed() using the above name |
|
135 | - * and test for additional acceptable values. |
|
136 | - * So if the set of $acceptable_values was: [ "initial-visit", "revisit" ] |
|
137 | - * then adding a filter to |
|
138 | - * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed", |
|
139 | - * would allow you to perform your own conditional and allow "wait-list-checkout" as an acceptable value. |
|
140 | - * example: |
|
141 | - * add_filter( |
|
142 | - * 'FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed', |
|
143 | - * function ($is_allowed, ContextInterface $context) { |
|
144 | - * return $context->slug() === 'wait-list-checkout' |
|
145 | - * ? true |
|
146 | - * : $is_allowed; |
|
147 | - * }, |
|
148 | - * 10, |
|
149 | - * 2 |
|
150 | - * ); |
|
151 | - * |
|
152 | - * @param ContextInterface $context |
|
153 | - * @return boolean |
|
154 | - */ |
|
155 | - public function isAllowed(ContextInterface $context) |
|
156 | - { |
|
157 | - $evaluation_callback = $this->evaluationCallback(); |
|
158 | - return filter_var( |
|
159 | - apply_filters( |
|
160 | - "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__{$this->identifier}__isAllowed", |
|
161 | - $evaluation_callback($context, $this->acceptableValues()), |
|
162 | - $context, |
|
163 | - $this |
|
164 | - ), |
|
165 | - FILTER_VALIDATE_BOOLEAN |
|
166 | - ); |
|
167 | - } |
|
25 | + /** |
|
26 | + * A unique string used to identify where this ContextChecker is being employed |
|
27 | + * Is currently only used within the hook name for the filterable return value of isAllowed(). |
|
28 | + * |
|
29 | + * @var string $identifier |
|
30 | + */ |
|
31 | + private $identifier; |
|
32 | + |
|
33 | + /** |
|
34 | + * A list of values to be compared against the slug of the Context class passed to isAllowed() |
|
35 | + * |
|
36 | + * @var array $acceptable_values |
|
37 | + */ |
|
38 | + private $acceptable_values; |
|
39 | + |
|
40 | + /** |
|
41 | + * Closure that will be called to perform the evaluation within isAllowed(). |
|
42 | + * If none is provided, then a simple type sensitive in_array() check will be used |
|
43 | + * and return true if the incoming Context::slug() is found within the array of $acceptable_values. |
|
44 | + * |
|
45 | + * @var Closure $evaluation_callback |
|
46 | + */ |
|
47 | + private $evaluation_callback; |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * ContextChecker constructor. |
|
52 | + * |
|
53 | + * @param string $identifier |
|
54 | + * @param array $acceptable_values |
|
55 | + * @param Closure|null $evaluation_callback [optional] |
|
56 | + */ |
|
57 | + public function __construct($identifier, array $acceptable_values, Closure $evaluation_callback = null) |
|
58 | + { |
|
59 | + $this->setIdentifier($identifier); |
|
60 | + $this->setAcceptableValues($acceptable_values); |
|
61 | + $this->setEvaluationCallback($evaluation_callback); |
|
62 | + } |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * @param string $identifier |
|
67 | + */ |
|
68 | + private function setIdentifier($identifier) |
|
69 | + { |
|
70 | + $this->identifier = sanitize_key($identifier); |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @param array $acceptable_values |
|
76 | + */ |
|
77 | + private function setAcceptableValues(array $acceptable_values) |
|
78 | + { |
|
79 | + $this->acceptable_values = $acceptable_values; |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * @param Closure $evaluation_callback |
|
85 | + */ |
|
86 | + private function setEvaluationCallback(Closure $evaluation_callback = null) |
|
87 | + { |
|
88 | + $this->evaluation_callback = $evaluation_callback instanceof Closure |
|
89 | + ? $evaluation_callback |
|
90 | + : function (ContextInterface $context, $acceptable_values) { |
|
91 | + return in_array($context->slug(), $acceptable_values, true); |
|
92 | + }; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * @return string |
|
98 | + */ |
|
99 | + protected function identifier() |
|
100 | + { |
|
101 | + return $this->identifier; |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * @return array |
|
107 | + */ |
|
108 | + protected function acceptableValues() |
|
109 | + { |
|
110 | + return apply_filters( |
|
111 | + "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__{$this->identifier}__acceptableValues", |
|
112 | + $this->acceptable_values |
|
113 | + ); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * @return Closure |
|
119 | + */ |
|
120 | + protected function evaluationCallback() |
|
121 | + { |
|
122 | + return $this->evaluation_callback; |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns true if the incoming Context class slug matches one of the preset acceptable values. |
|
129 | + * The result is filterable using the identifier for this ContextChecker. |
|
130 | + * example: |
|
131 | + * If this ContextChecker's $identifier was set to "registration-checkout-type", |
|
132 | + * then the filter here would be named: |
|
133 | + * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed". |
|
134 | + * Other code could hook into the filter in isAllowed() using the above name |
|
135 | + * and test for additional acceptable values. |
|
136 | + * So if the set of $acceptable_values was: [ "initial-visit", "revisit" ] |
|
137 | + * then adding a filter to |
|
138 | + * "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed", |
|
139 | + * would allow you to perform your own conditional and allow "wait-list-checkout" as an acceptable value. |
|
140 | + * example: |
|
141 | + * add_filter( |
|
142 | + * 'FHEE__EventEspresso_core_domain_entities_context_ContextChecker__registration-checkout-type__isAllowed', |
|
143 | + * function ($is_allowed, ContextInterface $context) { |
|
144 | + * return $context->slug() === 'wait-list-checkout' |
|
145 | + * ? true |
|
146 | + * : $is_allowed; |
|
147 | + * }, |
|
148 | + * 10, |
|
149 | + * 2 |
|
150 | + * ); |
|
151 | + * |
|
152 | + * @param ContextInterface $context |
|
153 | + * @return boolean |
|
154 | + */ |
|
155 | + public function isAllowed(ContextInterface $context) |
|
156 | + { |
|
157 | + $evaluation_callback = $this->evaluationCallback(); |
|
158 | + return filter_var( |
|
159 | + apply_filters( |
|
160 | + "FHEE__EventEspresso_core_domain_entities_context_ContextChecker__{$this->identifier}__isAllowed", |
|
161 | + $evaluation_callback($context, $this->acceptableValues()), |
|
162 | + $context, |
|
163 | + $this |
|
164 | + ), |
|
165 | + FILTER_VALIDATE_BOOLEAN |
|
166 | + ); |
|
167 | + } |
|
168 | 168 | |
169 | 169 | } |
@@ -87,7 +87,7 @@ |
||
87 | 87 | { |
88 | 88 | $this->evaluation_callback = $evaluation_callback instanceof Closure |
89 | 89 | ? $evaluation_callback |
90 | - : function (ContextInterface $context, $acceptable_values) { |
|
90 | + : function(ContextInterface $context, $acceptable_values) { |
|
91 | 91 | return in_array($context->slug(), $acceptable_values, true); |
92 | 92 | }; |
93 | 93 | } |
@@ -17,64 +17,64 @@ |
||
17 | 17 | class Context implements ContextInterface |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string $slug |
|
22 | - */ |
|
23 | - private $slug; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string $description |
|
27 | - */ |
|
28 | - private $description; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * Context constructor. |
|
33 | - * |
|
34 | - * @param string $slug |
|
35 | - * @param string $description |
|
36 | - */ |
|
37 | - public function __construct($slug, $description) |
|
38 | - { |
|
39 | - $this->setSlug($slug); |
|
40 | - $this->setDescription($description); |
|
41 | - } |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * @return string |
|
46 | - */ |
|
47 | - public function slug() |
|
48 | - { |
|
49 | - return $this->slug; |
|
50 | - } |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * @param string $slug |
|
55 | - */ |
|
56 | - private function setSlug($slug) |
|
57 | - { |
|
58 | - $this->slug = sanitize_key($slug); |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function description() |
|
66 | - { |
|
67 | - return $this->description; |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * @param string $description |
|
73 | - */ |
|
74 | - private function setDescription($description) |
|
75 | - { |
|
76 | - $this->description = sanitize_text_field($description); |
|
77 | - } |
|
20 | + /** |
|
21 | + * @var string $slug |
|
22 | + */ |
|
23 | + private $slug; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string $description |
|
27 | + */ |
|
28 | + private $description; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * Context constructor. |
|
33 | + * |
|
34 | + * @param string $slug |
|
35 | + * @param string $description |
|
36 | + */ |
|
37 | + public function __construct($slug, $description) |
|
38 | + { |
|
39 | + $this->setSlug($slug); |
|
40 | + $this->setDescription($description); |
|
41 | + } |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * @return string |
|
46 | + */ |
|
47 | + public function slug() |
|
48 | + { |
|
49 | + return $this->slug; |
|
50 | + } |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * @param string $slug |
|
55 | + */ |
|
56 | + private function setSlug($slug) |
|
57 | + { |
|
58 | + $this->slug = sanitize_key($slug); |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function description() |
|
66 | + { |
|
67 | + return $this->description; |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * @param string $description |
|
73 | + */ |
|
74 | + private function setDescription($description) |
|
75 | + { |
|
76 | + $this->description = sanitize_text_field($description); |
|
77 | + } |
|
78 | 78 | |
79 | 79 | } |
80 | 80 | // Location: Context.php |
@@ -18,14 +18,14 @@ |
||
18 | 18 | interface ContextInterface |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function slug(); |
|
21 | + /** |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function slug(); |
|
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - public function description(); |
|
27 | + /** |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + public function description(); |
|
31 | 31 | } |