@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
3 | 3 | |
4 | 4 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
5 | - exit('No direct script access allowed'); |
|
5 | + exit('No direct script access allowed'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | |
@@ -26,525 +26,525 @@ discard block |
||
26 | 26 | class Model_Data_Translator |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
31 | - * fields that COULD contain -1; so we use null |
|
32 | - */ |
|
33 | - const ee_inf_in_rest = null; |
|
34 | - |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * Prepares a possible array of input values from JSON for use by the models |
|
39 | - * |
|
40 | - * @param \EE_Model_Field_Base $field_obj |
|
41 | - * @param mixed $original_value_maybe_array |
|
42 | - * @param string $requested_version |
|
43 | - * @param string $timezone_string treat values as being in this timezone |
|
44 | - * @return mixed |
|
45 | - * @throws \DomainException |
|
46 | - */ |
|
47 | - public static function prepare_field_values_from_json( |
|
48 | - $field_obj, |
|
49 | - $original_value_maybe_array, |
|
50 | - $requested_version, |
|
51 | - $timezone_string = 'UTC' |
|
52 | - ) { |
|
53 | - if (is_array($original_value_maybe_array)) { |
|
54 | - $new_value_maybe_array = array(); |
|
55 | - foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
56 | - $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_from_json( |
|
57 | - $field_obj, |
|
58 | - $array_item, |
|
59 | - $requested_version, |
|
60 | - $timezone_string |
|
61 | - ); |
|
62 | - } |
|
63 | - } else { |
|
64 | - $new_value_maybe_array = Model_Data_Translator::prepare_field_value_from_json( |
|
65 | - $field_obj, |
|
66 | - $original_value_maybe_array, |
|
67 | - $requested_version, |
|
68 | - $timezone_string |
|
69 | - ); |
|
70 | - } |
|
71 | - return $new_value_maybe_array; |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * Prepares an array of field values FOR use in JSON/REST API |
|
78 | - * |
|
79 | - * @param \EE_Model_Field_Base $field_obj |
|
80 | - * @param mixed $original_value_maybe_array |
|
81 | - * @param string $request_version (eg 4.8.36) |
|
82 | - * @return array |
|
83 | - */ |
|
84 | - public static function prepare_field_values_for_json($field_obj, $original_value_maybe_array, $request_version) |
|
85 | - { |
|
86 | - if (is_array($original_value_maybe_array)) { |
|
87 | - $new_value_maybe_array = array(); |
|
88 | - foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
89 | - $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
90 | - $field_obj, |
|
91 | - $array_item, |
|
92 | - $request_version |
|
93 | - ); |
|
94 | - } |
|
95 | - } else { |
|
96 | - $new_value_maybe_array = Model_Data_Translator::prepare_field_value_for_json( |
|
97 | - $field_obj, |
|
98 | - $original_value_maybe_array, |
|
99 | - $request_version |
|
100 | - ); |
|
101 | - } |
|
102 | - return $new_value_maybe_array; |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
109 | - * "$query_params". |
|
110 | - * |
|
111 | - * @param \EE_Model_Field_Base $field_obj |
|
112 | - * @param mixed $original_value |
|
113 | - * @param string $requested_version |
|
114 | - * @param string $timezone_string treat values as being in this timezone |
|
115 | - * @return mixed |
|
116 | - * @throws \DomainException |
|
117 | - */ |
|
118 | - public static function prepare_field_value_from_json( |
|
119 | - $field_obj, |
|
120 | - $original_value, |
|
121 | - $requested_version, |
|
122 | - $timezone_string = 'UTC' // UTC |
|
123 | - ) |
|
124 | - { |
|
125 | - $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
126 | - $new_value = null; |
|
127 | - if ($field_obj instanceof \EE_Infinite_Integer_Field |
|
128 | - && in_array($original_value, array(null, ''), true) |
|
129 | - ) { |
|
130 | - $new_value = EE_INF; |
|
131 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
132 | - list($offset_sign, $offset_secs) = Model_Data_Translator::parse_timezone_offset( |
|
133 | - $field_obj->get_timezone_offset( |
|
134 | - new \DateTimeZone($timezone_string) |
|
135 | - ) |
|
136 | - ); |
|
137 | - $offset_string = |
|
138 | - str_pad( |
|
139 | - floor($offset_secs / HOUR_IN_SECONDS), |
|
140 | - 2, |
|
141 | - '0', |
|
142 | - STR_PAD_LEFT |
|
143 | - ) |
|
144 | - . ':' |
|
145 | - . str_pad( |
|
146 | - ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
147 | - 2, |
|
148 | - '0', |
|
149 | - STR_PAD_LEFT |
|
150 | - ); |
|
151 | - $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
152 | - } else { |
|
153 | - $new_value = $original_value; |
|
154 | - } |
|
155 | - return $new_value; |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * determines what's going on with them timezone strings |
|
162 | - * |
|
163 | - * @param int $timezone_offset |
|
164 | - * @return array |
|
165 | - */ |
|
166 | - private static function parse_timezone_offset($timezone_offset) |
|
167 | - { |
|
168 | - $first_char = substr((string)$timezone_offset, 0, 1); |
|
169 | - if ($first_char === '+' || $first_char === '-') { |
|
170 | - $offset_sign = $first_char; |
|
171 | - $offset_secs = substr((string)$timezone_offset, 1); |
|
172 | - } else { |
|
173 | - $offset_sign = '+'; |
|
174 | - $offset_secs = $timezone_offset; |
|
175 | - } |
|
176 | - return array($offset_sign, $offset_secs); |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * Prepares a field's value for display in the API. |
|
183 | - * The $original_value should be in the model object's domain of values, see the explanation at the top of EEM_Base. |
|
184 | - * However, for backward compatibility, we also attempt to handle $original_values from the |
|
185 | - * model client-code domain, and from the database domain. |
|
186 | - * E.g., when working with EE_Datetime_Fields, $original_value should be a DateTime or DbSafeDateTime |
|
187 | - * (model object domain). However, for backward compatibility, we also accept a unix timestamp |
|
188 | - * (old model object domain), MySQL datetime string (database domain) or string formatted according to the |
|
189 | - * WP Datetime format (model client-code domain) |
|
190 | - * |
|
191 | - * @param \EE_Model_Field_Base $field_obj |
|
192 | - * @param mixed $original_value |
|
193 | - * @param string $requested_version |
|
194 | - * @return mixed |
|
195 | - */ |
|
196 | - public static function prepare_field_value_for_json($field_obj, $original_value, $requested_version) |
|
197 | - { |
|
198 | - if ($original_value === EE_INF) { |
|
199 | - $new_value = Model_Data_Translator::ee_inf_in_rest; |
|
200 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
201 | - if (is_string($original_value)) { |
|
202 | - //did they submit a string of a unix timestamp? |
|
203 | - if (is_numeric($original_value)) { |
|
204 | - $datetime_obj = new \DateTime(); |
|
205 | - $datetime_obj->setTimestamp((int)$original_value); |
|
206 | - } else { |
|
207 | - //first, check if its a MySQL timestamp in GMT |
|
208 | - $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
|
209 | - } |
|
210 | - if (! $datetime_obj instanceof \DateTime) { |
|
211 | - //so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
|
212 | - $datetime_obj = $field_obj->prepare_for_set($original_value); |
|
213 | - } |
|
214 | - $original_value = $datetime_obj; |
|
215 | - } |
|
216 | - if ($original_value instanceof \DateTime) { |
|
217 | - $new_value = $original_value->format('Y-m-d H:i:s'); |
|
218 | - } elseif (is_int($original_value)) { |
|
219 | - $new_value = date('Y-m-d H:i:s', $original_value); |
|
220 | - } elseif($original_value === null || $original_value === '') { |
|
221 | - $new_value = null; |
|
222 | - } else { |
|
223 | - //so it's not a datetime object, unix timestamp (as string or int), |
|
224 | - //MySQL timestamp, or even a string in the field object's format. So no idea what it is |
|
225 | - throw new \EE_Error( |
|
226 | - sprintf( |
|
227 | - esc_html__( |
|
228 | - // @codingStandardsIgnoreStart |
|
229 | - 'The value "%1$s" for the field "%2$s" on model "%3$s" could not be understood. It should be a PHP DateTime, unix timestamp, MySQL date, or string in the format "%4$s".', |
|
230 | - // @codingStandardsIgnoreEnd |
|
231 | - 'event_espressso' |
|
232 | - ), |
|
233 | - $original_value, |
|
234 | - $field_obj->get_name(), |
|
235 | - $field_obj->get_model_name(), |
|
236 | - $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
237 | - ) |
|
238 | - ); |
|
239 | - } |
|
240 | - $new_value = mysql_to_rfc3339($new_value); |
|
241 | - } else { |
|
242 | - $new_value = $original_value; |
|
243 | - } |
|
244 | - return apply_filters( |
|
245 | - 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
246 | - $new_value, |
|
247 | - $field_obj, |
|
248 | - $original_value, |
|
249 | - $requested_version |
|
250 | - ); |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - |
|
255 | - /** |
|
256 | - * Prepares condition-query-parameters (like what's in where and having) from |
|
257 | - * the format expected in the API to use in the models |
|
258 | - * |
|
259 | - * @param array $inputted_query_params_of_this_type |
|
260 | - * @param \EEM_Base $model |
|
261 | - * @param string $requested_version |
|
262 | - * @return array |
|
263 | - * @throws \DomainException |
|
264 | - * @throws \EE_Error |
|
265 | - */ |
|
266 | - public static function prepare_conditions_query_params_for_models( |
|
267 | - $inputted_query_params_of_this_type, |
|
268 | - \EEM_Base $model, |
|
269 | - $requested_version |
|
270 | - ) { |
|
271 | - $query_param_for_models = array(); |
|
272 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
273 | - $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key); |
|
274 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
275 | - $query_param_sans_stars, |
|
276 | - $model |
|
277 | - ); |
|
278 | - //double-check is it a *_gmt field? |
|
279 | - if (! $field instanceof \EE_Model_Field_Base |
|
280 | - && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
|
281 | - ) { |
|
282 | - //yep, take off '_gmt', and find the field |
|
283 | - $query_param_key = Model_Data_Translator::remove_gmt_from_field_name($query_param_sans_stars); |
|
284 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
285 | - $query_param_key, |
|
286 | - $model |
|
287 | - ); |
|
288 | - $timezone = 'UTC'; |
|
289 | - } else { |
|
290 | - //so it's not a GMT field. Set the timezone on the model to the default |
|
291 | - $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
292 | - } |
|
293 | - if ($field instanceof \EE_Model_Field_Base) { |
|
294 | - //did they specify an operator? |
|
295 | - if (is_array($query_param_value)) { |
|
296 | - $op = $query_param_value[0]; |
|
297 | - $translated_value = array($op); |
|
298 | - if (isset($query_param_value[1])) { |
|
299 | - $value = $query_param_value[1]; |
|
300 | - $translated_value[1] = Model_Data_Translator::prepare_field_values_from_json($field, $value, |
|
301 | - $requested_version, $timezone); |
|
302 | - } |
|
303 | - } else { |
|
304 | - $translated_value = Model_Data_Translator::prepare_field_value_from_json($field, $query_param_value, |
|
305 | - $requested_version, $timezone); |
|
306 | - } |
|
307 | - $query_param_for_models[$query_param_key] = $translated_value; |
|
308 | - } else { |
|
309 | - //so it's not for a field, assume it's a logic query param key |
|
310 | - $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_models($query_param_value, |
|
311 | - $model, $requested_version); |
|
312 | - } |
|
313 | - } |
|
314 | - return $query_param_for_models; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
321 | - * gmt date field name |
|
322 | - * |
|
323 | - * @param string $field_name |
|
324 | - * @return boolean |
|
325 | - */ |
|
326 | - public static function is_gmt_date_field_name($field_name) |
|
327 | - { |
|
328 | - return substr( |
|
329 | - Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name), |
|
330 | - -4, |
|
331 | - 4 |
|
332 | - ) === '_gmt'; |
|
333 | - } |
|
334 | - |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
339 | - * |
|
340 | - * @param string $field_name |
|
341 | - * @return string |
|
342 | - */ |
|
343 | - public static function remove_gmt_from_field_name($field_name) |
|
344 | - { |
|
345 | - if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
346 | - return $field_name; |
|
347 | - } |
|
348 | - $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |
|
349 | - $query_param_sans_gmt_and_sans_stars = substr( |
|
350 | - $query_param_sans_stars, |
|
351 | - 0, |
|
352 | - strrpos( |
|
353 | - $field_name, |
|
354 | - '_gmt' |
|
355 | - ) |
|
356 | - ); |
|
357 | - return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
358 | - } |
|
359 | - |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * Takes a field name from the REST API and prepares it for the model querying |
|
364 | - * |
|
365 | - * @param string $field_name |
|
366 | - * @return string |
|
367 | - */ |
|
368 | - public static function prepare_field_name_from_json($field_name) |
|
369 | - { |
|
370 | - if (Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
371 | - return Model_Data_Translator::remove_gmt_from_field_name($field_name); |
|
372 | - } |
|
373 | - return $field_name; |
|
374 | - } |
|
375 | - |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * Takes array of field names from REST API and prepares for models |
|
380 | - * |
|
381 | - * @param array $field_names |
|
382 | - * @return array of field names (possibly include model prefixes) |
|
383 | - */ |
|
384 | - public static function prepare_field_names_from_json(array $field_names) |
|
385 | - { |
|
386 | - $new_array = array(); |
|
387 | - foreach ($field_names as $key => $field_name) { |
|
388 | - $new_array[$key] = Model_Data_Translator::prepare_field_name_from_json($field_name); |
|
389 | - } |
|
390 | - return $new_array; |
|
391 | - } |
|
392 | - |
|
393 | - |
|
394 | - |
|
395 | - /** |
|
396 | - * Takes array where array keys are field names (possibly with model path prefixes) |
|
397 | - * from the REST API and prepares them for model querying |
|
398 | - * |
|
399 | - * @param array $field_names_as_keys |
|
400 | - * @return array |
|
401 | - */ |
|
402 | - public static function prepare_field_names_in_array_keys_from_json(array $field_names_as_keys) |
|
403 | - { |
|
404 | - $new_array = array(); |
|
405 | - foreach ($field_names_as_keys as $field_name => $value) { |
|
406 | - $new_array[Model_Data_Translator::prepare_field_name_from_json($field_name)] = $value; |
|
407 | - } |
|
408 | - return $new_array; |
|
409 | - } |
|
410 | - |
|
411 | - |
|
412 | - |
|
413 | - /** |
|
414 | - * Prepares an array of model query params for use in the REST API |
|
415 | - * |
|
416 | - * @param array $model_query_params |
|
417 | - * @param \EEM_Base $model |
|
418 | - * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
419 | - * REST API |
|
420 | - * @return array which can be passed into the EE4 REST API when querying a model resource |
|
421 | - * @throws \EE_Error |
|
422 | - */ |
|
423 | - public static function prepare_query_params_for_rest_api( |
|
424 | - array $model_query_params, |
|
425 | - \EEM_Base $model, |
|
426 | - $requested_version = null |
|
427 | - ) { |
|
428 | - if ($requested_version === null) { |
|
429 | - $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
430 | - } |
|
431 | - $rest_query_params = $model_query_params; |
|
432 | - if (isset($model_query_params[0])) { |
|
433 | - $rest_query_params['where'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
434 | - $model_query_params[0], |
|
435 | - $model, |
|
436 | - $requested_version |
|
437 | - ); |
|
438 | - unset($rest_query_params[0]); |
|
439 | - } |
|
440 | - if (isset($model_query_params['having'])) { |
|
441 | - $rest_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
442 | - $model_query_params['having'], |
|
443 | - $model, |
|
444 | - $requested_version |
|
445 | - ); |
|
446 | - } |
|
447 | - return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
448 | - $rest_query_params, $model_query_params, $model, $requested_version); |
|
449 | - } |
|
450 | - |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
455 | - * |
|
456 | - * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
457 | - * passed into EEM_Base::get_all() |
|
458 | - * @param \EEM_Base $model |
|
459 | - * @param string $requested_version eg "4.8.36" |
|
460 | - * @return array ready for use in the rest api query params |
|
461 | - * @throws \EE_Error |
|
462 | - */ |
|
463 | - public static function prepare_conditions_query_params_for_rest_api( |
|
464 | - $inputted_query_params_of_this_type, |
|
465 | - \EEM_Base $model, |
|
466 | - $requested_version |
|
467 | - ) { |
|
468 | - $query_param_for_models = array(); |
|
469 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
470 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
471 | - Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key), |
|
472 | - $model |
|
473 | - ); |
|
474 | - if ($field instanceof \EE_Model_Field_Base) { |
|
475 | - //did they specify an operator? |
|
476 | - if (is_array($query_param_value)) { |
|
477 | - $op = $query_param_value[0]; |
|
478 | - $translated_value = array($op); |
|
479 | - if (isset($query_param_value[1])) { |
|
480 | - $value = $query_param_value[1]; |
|
481 | - $translated_value[1] = Model_Data_Translator::prepare_field_values_for_json($field, $value, |
|
482 | - $requested_version); |
|
483 | - } |
|
484 | - } else { |
|
485 | - $translated_value = Model_Data_Translator::prepare_field_value_for_json($field, $query_param_value, |
|
486 | - $requested_version); |
|
487 | - } |
|
488 | - $query_param_for_models[$query_param_key] = $translated_value; |
|
489 | - } else { |
|
490 | - //so it's not for a field, assume it's a logic query param key |
|
491 | - $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api($query_param_value, |
|
492 | - $model, $requested_version); |
|
493 | - } |
|
494 | - } |
|
495 | - return $query_param_for_models; |
|
496 | - } |
|
497 | - |
|
498 | - |
|
499 | - |
|
500 | - /** |
|
501 | - * @param $condition_query_param_key |
|
502 | - * @return string |
|
503 | - */ |
|
504 | - public static function remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
505 | - { |
|
506 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
507 | - if ($pos_of_star === false) { |
|
508 | - return $condition_query_param_key; |
|
509 | - } else { |
|
510 | - $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
511 | - return $condition_query_param_sans_star; |
|
512 | - } |
|
513 | - } |
|
514 | - |
|
515 | - |
|
516 | - |
|
517 | - /** |
|
518 | - * Takes the input parameter and finds the model field that it indicates. |
|
519 | - * |
|
520 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
521 | - * @param \EEM_Base $model |
|
522 | - * @return \EE_Model_Field_Base |
|
523 | - * @throws \EE_Error |
|
524 | - */ |
|
525 | - public static function deduce_field_from_query_param($query_param_name, \EEM_Base $model) |
|
526 | - { |
|
527 | - //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
528 | - //which will help us find the database table and column |
|
529 | - $query_param_parts = explode('.', $query_param_name); |
|
530 | - if (empty($query_param_parts)) { |
|
531 | - throw new \EE_Error(sprintf(__('_extract_column_name is empty when trying to extract column and table name from %s', |
|
532 | - 'event_espresso'), $query_param_name)); |
|
533 | - } |
|
534 | - $number_of_parts = count($query_param_parts); |
|
535 | - $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
536 | - if ($number_of_parts === 1) { |
|
537 | - $field_name = $last_query_param_part; |
|
538 | - } else {// $number_of_parts >= 2 |
|
539 | - //the last part is the column name, and there are only 2parts. therefore... |
|
540 | - $field_name = $last_query_param_part; |
|
541 | - $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
542 | - } |
|
543 | - try { |
|
544 | - return $model->field_settings_for($field_name); |
|
545 | - } catch (\EE_Error $e) { |
|
546 | - return null; |
|
547 | - } |
|
548 | - } |
|
29 | + /** |
|
30 | + * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
31 | + * fields that COULD contain -1; so we use null |
|
32 | + */ |
|
33 | + const ee_inf_in_rest = null; |
|
34 | + |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * Prepares a possible array of input values from JSON for use by the models |
|
39 | + * |
|
40 | + * @param \EE_Model_Field_Base $field_obj |
|
41 | + * @param mixed $original_value_maybe_array |
|
42 | + * @param string $requested_version |
|
43 | + * @param string $timezone_string treat values as being in this timezone |
|
44 | + * @return mixed |
|
45 | + * @throws \DomainException |
|
46 | + */ |
|
47 | + public static function prepare_field_values_from_json( |
|
48 | + $field_obj, |
|
49 | + $original_value_maybe_array, |
|
50 | + $requested_version, |
|
51 | + $timezone_string = 'UTC' |
|
52 | + ) { |
|
53 | + if (is_array($original_value_maybe_array)) { |
|
54 | + $new_value_maybe_array = array(); |
|
55 | + foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
56 | + $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_from_json( |
|
57 | + $field_obj, |
|
58 | + $array_item, |
|
59 | + $requested_version, |
|
60 | + $timezone_string |
|
61 | + ); |
|
62 | + } |
|
63 | + } else { |
|
64 | + $new_value_maybe_array = Model_Data_Translator::prepare_field_value_from_json( |
|
65 | + $field_obj, |
|
66 | + $original_value_maybe_array, |
|
67 | + $requested_version, |
|
68 | + $timezone_string |
|
69 | + ); |
|
70 | + } |
|
71 | + return $new_value_maybe_array; |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * Prepares an array of field values FOR use in JSON/REST API |
|
78 | + * |
|
79 | + * @param \EE_Model_Field_Base $field_obj |
|
80 | + * @param mixed $original_value_maybe_array |
|
81 | + * @param string $request_version (eg 4.8.36) |
|
82 | + * @return array |
|
83 | + */ |
|
84 | + public static function prepare_field_values_for_json($field_obj, $original_value_maybe_array, $request_version) |
|
85 | + { |
|
86 | + if (is_array($original_value_maybe_array)) { |
|
87 | + $new_value_maybe_array = array(); |
|
88 | + foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
89 | + $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
90 | + $field_obj, |
|
91 | + $array_item, |
|
92 | + $request_version |
|
93 | + ); |
|
94 | + } |
|
95 | + } else { |
|
96 | + $new_value_maybe_array = Model_Data_Translator::prepare_field_value_for_json( |
|
97 | + $field_obj, |
|
98 | + $original_value_maybe_array, |
|
99 | + $request_version |
|
100 | + ); |
|
101 | + } |
|
102 | + return $new_value_maybe_array; |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
109 | + * "$query_params". |
|
110 | + * |
|
111 | + * @param \EE_Model_Field_Base $field_obj |
|
112 | + * @param mixed $original_value |
|
113 | + * @param string $requested_version |
|
114 | + * @param string $timezone_string treat values as being in this timezone |
|
115 | + * @return mixed |
|
116 | + * @throws \DomainException |
|
117 | + */ |
|
118 | + public static function prepare_field_value_from_json( |
|
119 | + $field_obj, |
|
120 | + $original_value, |
|
121 | + $requested_version, |
|
122 | + $timezone_string = 'UTC' // UTC |
|
123 | + ) |
|
124 | + { |
|
125 | + $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
126 | + $new_value = null; |
|
127 | + if ($field_obj instanceof \EE_Infinite_Integer_Field |
|
128 | + && in_array($original_value, array(null, ''), true) |
|
129 | + ) { |
|
130 | + $new_value = EE_INF; |
|
131 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
132 | + list($offset_sign, $offset_secs) = Model_Data_Translator::parse_timezone_offset( |
|
133 | + $field_obj->get_timezone_offset( |
|
134 | + new \DateTimeZone($timezone_string) |
|
135 | + ) |
|
136 | + ); |
|
137 | + $offset_string = |
|
138 | + str_pad( |
|
139 | + floor($offset_secs / HOUR_IN_SECONDS), |
|
140 | + 2, |
|
141 | + '0', |
|
142 | + STR_PAD_LEFT |
|
143 | + ) |
|
144 | + . ':' |
|
145 | + . str_pad( |
|
146 | + ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
147 | + 2, |
|
148 | + '0', |
|
149 | + STR_PAD_LEFT |
|
150 | + ); |
|
151 | + $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
152 | + } else { |
|
153 | + $new_value = $original_value; |
|
154 | + } |
|
155 | + return $new_value; |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * determines what's going on with them timezone strings |
|
162 | + * |
|
163 | + * @param int $timezone_offset |
|
164 | + * @return array |
|
165 | + */ |
|
166 | + private static function parse_timezone_offset($timezone_offset) |
|
167 | + { |
|
168 | + $first_char = substr((string)$timezone_offset, 0, 1); |
|
169 | + if ($first_char === '+' || $first_char === '-') { |
|
170 | + $offset_sign = $first_char; |
|
171 | + $offset_secs = substr((string)$timezone_offset, 1); |
|
172 | + } else { |
|
173 | + $offset_sign = '+'; |
|
174 | + $offset_secs = $timezone_offset; |
|
175 | + } |
|
176 | + return array($offset_sign, $offset_secs); |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * Prepares a field's value for display in the API. |
|
183 | + * The $original_value should be in the model object's domain of values, see the explanation at the top of EEM_Base. |
|
184 | + * However, for backward compatibility, we also attempt to handle $original_values from the |
|
185 | + * model client-code domain, and from the database domain. |
|
186 | + * E.g., when working with EE_Datetime_Fields, $original_value should be a DateTime or DbSafeDateTime |
|
187 | + * (model object domain). However, for backward compatibility, we also accept a unix timestamp |
|
188 | + * (old model object domain), MySQL datetime string (database domain) or string formatted according to the |
|
189 | + * WP Datetime format (model client-code domain) |
|
190 | + * |
|
191 | + * @param \EE_Model_Field_Base $field_obj |
|
192 | + * @param mixed $original_value |
|
193 | + * @param string $requested_version |
|
194 | + * @return mixed |
|
195 | + */ |
|
196 | + public static function prepare_field_value_for_json($field_obj, $original_value, $requested_version) |
|
197 | + { |
|
198 | + if ($original_value === EE_INF) { |
|
199 | + $new_value = Model_Data_Translator::ee_inf_in_rest; |
|
200 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
201 | + if (is_string($original_value)) { |
|
202 | + //did they submit a string of a unix timestamp? |
|
203 | + if (is_numeric($original_value)) { |
|
204 | + $datetime_obj = new \DateTime(); |
|
205 | + $datetime_obj->setTimestamp((int)$original_value); |
|
206 | + } else { |
|
207 | + //first, check if its a MySQL timestamp in GMT |
|
208 | + $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
|
209 | + } |
|
210 | + if (! $datetime_obj instanceof \DateTime) { |
|
211 | + //so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
|
212 | + $datetime_obj = $field_obj->prepare_for_set($original_value); |
|
213 | + } |
|
214 | + $original_value = $datetime_obj; |
|
215 | + } |
|
216 | + if ($original_value instanceof \DateTime) { |
|
217 | + $new_value = $original_value->format('Y-m-d H:i:s'); |
|
218 | + } elseif (is_int($original_value)) { |
|
219 | + $new_value = date('Y-m-d H:i:s', $original_value); |
|
220 | + } elseif($original_value === null || $original_value === '') { |
|
221 | + $new_value = null; |
|
222 | + } else { |
|
223 | + //so it's not a datetime object, unix timestamp (as string or int), |
|
224 | + //MySQL timestamp, or even a string in the field object's format. So no idea what it is |
|
225 | + throw new \EE_Error( |
|
226 | + sprintf( |
|
227 | + esc_html__( |
|
228 | + // @codingStandardsIgnoreStart |
|
229 | + 'The value "%1$s" for the field "%2$s" on model "%3$s" could not be understood. It should be a PHP DateTime, unix timestamp, MySQL date, or string in the format "%4$s".', |
|
230 | + // @codingStandardsIgnoreEnd |
|
231 | + 'event_espressso' |
|
232 | + ), |
|
233 | + $original_value, |
|
234 | + $field_obj->get_name(), |
|
235 | + $field_obj->get_model_name(), |
|
236 | + $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
237 | + ) |
|
238 | + ); |
|
239 | + } |
|
240 | + $new_value = mysql_to_rfc3339($new_value); |
|
241 | + } else { |
|
242 | + $new_value = $original_value; |
|
243 | + } |
|
244 | + return apply_filters( |
|
245 | + 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
246 | + $new_value, |
|
247 | + $field_obj, |
|
248 | + $original_value, |
|
249 | + $requested_version |
|
250 | + ); |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + |
|
255 | + /** |
|
256 | + * Prepares condition-query-parameters (like what's in where and having) from |
|
257 | + * the format expected in the API to use in the models |
|
258 | + * |
|
259 | + * @param array $inputted_query_params_of_this_type |
|
260 | + * @param \EEM_Base $model |
|
261 | + * @param string $requested_version |
|
262 | + * @return array |
|
263 | + * @throws \DomainException |
|
264 | + * @throws \EE_Error |
|
265 | + */ |
|
266 | + public static function prepare_conditions_query_params_for_models( |
|
267 | + $inputted_query_params_of_this_type, |
|
268 | + \EEM_Base $model, |
|
269 | + $requested_version |
|
270 | + ) { |
|
271 | + $query_param_for_models = array(); |
|
272 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
273 | + $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key); |
|
274 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
275 | + $query_param_sans_stars, |
|
276 | + $model |
|
277 | + ); |
|
278 | + //double-check is it a *_gmt field? |
|
279 | + if (! $field instanceof \EE_Model_Field_Base |
|
280 | + && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
|
281 | + ) { |
|
282 | + //yep, take off '_gmt', and find the field |
|
283 | + $query_param_key = Model_Data_Translator::remove_gmt_from_field_name($query_param_sans_stars); |
|
284 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
285 | + $query_param_key, |
|
286 | + $model |
|
287 | + ); |
|
288 | + $timezone = 'UTC'; |
|
289 | + } else { |
|
290 | + //so it's not a GMT field. Set the timezone on the model to the default |
|
291 | + $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
292 | + } |
|
293 | + if ($field instanceof \EE_Model_Field_Base) { |
|
294 | + //did they specify an operator? |
|
295 | + if (is_array($query_param_value)) { |
|
296 | + $op = $query_param_value[0]; |
|
297 | + $translated_value = array($op); |
|
298 | + if (isset($query_param_value[1])) { |
|
299 | + $value = $query_param_value[1]; |
|
300 | + $translated_value[1] = Model_Data_Translator::prepare_field_values_from_json($field, $value, |
|
301 | + $requested_version, $timezone); |
|
302 | + } |
|
303 | + } else { |
|
304 | + $translated_value = Model_Data_Translator::prepare_field_value_from_json($field, $query_param_value, |
|
305 | + $requested_version, $timezone); |
|
306 | + } |
|
307 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
308 | + } else { |
|
309 | + //so it's not for a field, assume it's a logic query param key |
|
310 | + $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_models($query_param_value, |
|
311 | + $model, $requested_version); |
|
312 | + } |
|
313 | + } |
|
314 | + return $query_param_for_models; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
321 | + * gmt date field name |
|
322 | + * |
|
323 | + * @param string $field_name |
|
324 | + * @return boolean |
|
325 | + */ |
|
326 | + public static function is_gmt_date_field_name($field_name) |
|
327 | + { |
|
328 | + return substr( |
|
329 | + Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name), |
|
330 | + -4, |
|
331 | + 4 |
|
332 | + ) === '_gmt'; |
|
333 | + } |
|
334 | + |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
339 | + * |
|
340 | + * @param string $field_name |
|
341 | + * @return string |
|
342 | + */ |
|
343 | + public static function remove_gmt_from_field_name($field_name) |
|
344 | + { |
|
345 | + if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
346 | + return $field_name; |
|
347 | + } |
|
348 | + $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |
|
349 | + $query_param_sans_gmt_and_sans_stars = substr( |
|
350 | + $query_param_sans_stars, |
|
351 | + 0, |
|
352 | + strrpos( |
|
353 | + $field_name, |
|
354 | + '_gmt' |
|
355 | + ) |
|
356 | + ); |
|
357 | + return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
358 | + } |
|
359 | + |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * Takes a field name from the REST API and prepares it for the model querying |
|
364 | + * |
|
365 | + * @param string $field_name |
|
366 | + * @return string |
|
367 | + */ |
|
368 | + public static function prepare_field_name_from_json($field_name) |
|
369 | + { |
|
370 | + if (Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
371 | + return Model_Data_Translator::remove_gmt_from_field_name($field_name); |
|
372 | + } |
|
373 | + return $field_name; |
|
374 | + } |
|
375 | + |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * Takes array of field names from REST API and prepares for models |
|
380 | + * |
|
381 | + * @param array $field_names |
|
382 | + * @return array of field names (possibly include model prefixes) |
|
383 | + */ |
|
384 | + public static function prepare_field_names_from_json(array $field_names) |
|
385 | + { |
|
386 | + $new_array = array(); |
|
387 | + foreach ($field_names as $key => $field_name) { |
|
388 | + $new_array[$key] = Model_Data_Translator::prepare_field_name_from_json($field_name); |
|
389 | + } |
|
390 | + return $new_array; |
|
391 | + } |
|
392 | + |
|
393 | + |
|
394 | + |
|
395 | + /** |
|
396 | + * Takes array where array keys are field names (possibly with model path prefixes) |
|
397 | + * from the REST API and prepares them for model querying |
|
398 | + * |
|
399 | + * @param array $field_names_as_keys |
|
400 | + * @return array |
|
401 | + */ |
|
402 | + public static function prepare_field_names_in_array_keys_from_json(array $field_names_as_keys) |
|
403 | + { |
|
404 | + $new_array = array(); |
|
405 | + foreach ($field_names_as_keys as $field_name => $value) { |
|
406 | + $new_array[Model_Data_Translator::prepare_field_name_from_json($field_name)] = $value; |
|
407 | + } |
|
408 | + return $new_array; |
|
409 | + } |
|
410 | + |
|
411 | + |
|
412 | + |
|
413 | + /** |
|
414 | + * Prepares an array of model query params for use in the REST API |
|
415 | + * |
|
416 | + * @param array $model_query_params |
|
417 | + * @param \EEM_Base $model |
|
418 | + * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
419 | + * REST API |
|
420 | + * @return array which can be passed into the EE4 REST API when querying a model resource |
|
421 | + * @throws \EE_Error |
|
422 | + */ |
|
423 | + public static function prepare_query_params_for_rest_api( |
|
424 | + array $model_query_params, |
|
425 | + \EEM_Base $model, |
|
426 | + $requested_version = null |
|
427 | + ) { |
|
428 | + if ($requested_version === null) { |
|
429 | + $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
430 | + } |
|
431 | + $rest_query_params = $model_query_params; |
|
432 | + if (isset($model_query_params[0])) { |
|
433 | + $rest_query_params['where'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
434 | + $model_query_params[0], |
|
435 | + $model, |
|
436 | + $requested_version |
|
437 | + ); |
|
438 | + unset($rest_query_params[0]); |
|
439 | + } |
|
440 | + if (isset($model_query_params['having'])) { |
|
441 | + $rest_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
442 | + $model_query_params['having'], |
|
443 | + $model, |
|
444 | + $requested_version |
|
445 | + ); |
|
446 | + } |
|
447 | + return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
448 | + $rest_query_params, $model_query_params, $model, $requested_version); |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
455 | + * |
|
456 | + * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
457 | + * passed into EEM_Base::get_all() |
|
458 | + * @param \EEM_Base $model |
|
459 | + * @param string $requested_version eg "4.8.36" |
|
460 | + * @return array ready for use in the rest api query params |
|
461 | + * @throws \EE_Error |
|
462 | + */ |
|
463 | + public static function prepare_conditions_query_params_for_rest_api( |
|
464 | + $inputted_query_params_of_this_type, |
|
465 | + \EEM_Base $model, |
|
466 | + $requested_version |
|
467 | + ) { |
|
468 | + $query_param_for_models = array(); |
|
469 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
470 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
471 | + Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key), |
|
472 | + $model |
|
473 | + ); |
|
474 | + if ($field instanceof \EE_Model_Field_Base) { |
|
475 | + //did they specify an operator? |
|
476 | + if (is_array($query_param_value)) { |
|
477 | + $op = $query_param_value[0]; |
|
478 | + $translated_value = array($op); |
|
479 | + if (isset($query_param_value[1])) { |
|
480 | + $value = $query_param_value[1]; |
|
481 | + $translated_value[1] = Model_Data_Translator::prepare_field_values_for_json($field, $value, |
|
482 | + $requested_version); |
|
483 | + } |
|
484 | + } else { |
|
485 | + $translated_value = Model_Data_Translator::prepare_field_value_for_json($field, $query_param_value, |
|
486 | + $requested_version); |
|
487 | + } |
|
488 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
489 | + } else { |
|
490 | + //so it's not for a field, assume it's a logic query param key |
|
491 | + $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api($query_param_value, |
|
492 | + $model, $requested_version); |
|
493 | + } |
|
494 | + } |
|
495 | + return $query_param_for_models; |
|
496 | + } |
|
497 | + |
|
498 | + |
|
499 | + |
|
500 | + /** |
|
501 | + * @param $condition_query_param_key |
|
502 | + * @return string |
|
503 | + */ |
|
504 | + public static function remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
505 | + { |
|
506 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
507 | + if ($pos_of_star === false) { |
|
508 | + return $condition_query_param_key; |
|
509 | + } else { |
|
510 | + $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
511 | + return $condition_query_param_sans_star; |
|
512 | + } |
|
513 | + } |
|
514 | + |
|
515 | + |
|
516 | + |
|
517 | + /** |
|
518 | + * Takes the input parameter and finds the model field that it indicates. |
|
519 | + * |
|
520 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
521 | + * @param \EEM_Base $model |
|
522 | + * @return \EE_Model_Field_Base |
|
523 | + * @throws \EE_Error |
|
524 | + */ |
|
525 | + public static function deduce_field_from_query_param($query_param_name, \EEM_Base $model) |
|
526 | + { |
|
527 | + //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
528 | + //which will help us find the database table and column |
|
529 | + $query_param_parts = explode('.', $query_param_name); |
|
530 | + if (empty($query_param_parts)) { |
|
531 | + throw new \EE_Error(sprintf(__('_extract_column_name is empty when trying to extract column and table name from %s', |
|
532 | + 'event_espresso'), $query_param_name)); |
|
533 | + } |
|
534 | + $number_of_parts = count($query_param_parts); |
|
535 | + $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
536 | + if ($number_of_parts === 1) { |
|
537 | + $field_name = $last_query_param_part; |
|
538 | + } else {// $number_of_parts >= 2 |
|
539 | + //the last part is the column name, and there are only 2parts. therefore... |
|
540 | + $field_name = $last_query_param_part; |
|
541 | + $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
542 | + } |
|
543 | + try { |
|
544 | + return $model->field_settings_for($field_name); |
|
545 | + } catch (\EE_Error $e) { |
|
546 | + return null; |
|
547 | + } |
|
548 | + } |
|
549 | 549 | |
550 | 550 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace EventEspresso\core\libraries\rest_api; |
3 | 3 | |
4 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
5 | 5 | exit('No direct script access allowed'); |
6 | 6 | } |
7 | 7 | |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | '0', |
149 | 149 | STR_PAD_LEFT |
150 | 150 | ); |
151 | - $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
151 | + $new_value = rest_parse_date($original_value.$offset_sign.$offset_string); |
|
152 | 152 | } else { |
153 | 153 | $new_value = $original_value; |
154 | 154 | } |
@@ -165,10 +165,10 @@ discard block |
||
165 | 165 | */ |
166 | 166 | private static function parse_timezone_offset($timezone_offset) |
167 | 167 | { |
168 | - $first_char = substr((string)$timezone_offset, 0, 1); |
|
168 | + $first_char = substr((string) $timezone_offset, 0, 1); |
|
169 | 169 | if ($first_char === '+' || $first_char === '-') { |
170 | 170 | $offset_sign = $first_char; |
171 | - $offset_secs = substr((string)$timezone_offset, 1); |
|
171 | + $offset_secs = substr((string) $timezone_offset, 1); |
|
172 | 172 | } else { |
173 | 173 | $offset_sign = '+'; |
174 | 174 | $offset_secs = $timezone_offset; |
@@ -202,12 +202,12 @@ discard block |
||
202 | 202 | //did they submit a string of a unix timestamp? |
203 | 203 | if (is_numeric($original_value)) { |
204 | 204 | $datetime_obj = new \DateTime(); |
205 | - $datetime_obj->setTimestamp((int)$original_value); |
|
205 | + $datetime_obj->setTimestamp((int) $original_value); |
|
206 | 206 | } else { |
207 | 207 | //first, check if its a MySQL timestamp in GMT |
208 | 208 | $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
209 | 209 | } |
210 | - if (! $datetime_obj instanceof \DateTime) { |
|
210 | + if ( ! $datetime_obj instanceof \DateTime) { |
|
211 | 211 | //so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
212 | 212 | $datetime_obj = $field_obj->prepare_for_set($original_value); |
213 | 213 | } |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | $new_value = $original_value->format('Y-m-d H:i:s'); |
218 | 218 | } elseif (is_int($original_value)) { |
219 | 219 | $new_value = date('Y-m-d H:i:s', $original_value); |
220 | - } elseif($original_value === null || $original_value === '') { |
|
220 | + } elseif ($original_value === null || $original_value === '') { |
|
221 | 221 | $new_value = null; |
222 | 222 | } else { |
223 | 223 | //so it's not a datetime object, unix timestamp (as string or int), |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | $original_value, |
234 | 234 | $field_obj->get_name(), |
235 | 235 | $field_obj->get_model_name(), |
236 | - $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
236 | + $field_obj->get_time_format().' '.$field_obj->get_time_format() |
|
237 | 237 | ) |
238 | 238 | ); |
239 | 239 | } |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | $model |
277 | 277 | ); |
278 | 278 | //double-check is it a *_gmt field? |
279 | - if (! $field instanceof \EE_Model_Field_Base |
|
279 | + if ( ! $field instanceof \EE_Model_Field_Base |
|
280 | 280 | && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
281 | 281 | ) { |
282 | 282 | //yep, take off '_gmt', and find the field |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | */ |
343 | 343 | public static function remove_gmt_from_field_name($field_name) |
344 | 344 | { |
345 | - if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
345 | + if ( ! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
346 | 346 | return $field_name; |
347 | 347 | } |
348 | 348 | $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | /** |
328 | 328 | * Get Timezone Transitions |
329 | 329 | * @param \DateTimeZone $date_time_zone |
330 | - * @param null $time |
|
330 | + * @param integer|null $time |
|
331 | 331 | * @param bool $first_only |
332 | 332 | * @return array|mixed |
333 | 333 | */ |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | /** |
344 | 344 | * Get Timezone Offset for given timezone object. |
345 | 345 | * @param \DateTimeZone $date_time_zone |
346 | - * @param null $time |
|
346 | + * @param integer|null $time |
|
347 | 347 | * @return mixed |
348 | 348 | * @throws \DomainException |
349 | 349 | */ |
@@ -1075,7 +1075,7 @@ discard block |
||
1075 | 1075 | * this method will add that "1" into your date regardless of the format. |
1076 | 1076 | * |
1077 | 1077 | * @param string $month |
1078 | - * @return string |
|
1078 | + * @return integer |
|
1079 | 1079 | */ |
1080 | 1080 | public static function first_of_month_timestamp($month = '') |
1081 | 1081 | { |
@@ -1227,7 +1227,7 @@ discard block |
||
1227 | 1227 | /** |
1228 | 1228 | * Shim for the WP function `get_user_locale` that was added in WordPress 4.7.0 |
1229 | 1229 | * |
1230 | - * @param int|WP_User $user_id |
|
1230 | + * @param integer $user_id |
|
1231 | 1231 | * @return string |
1232 | 1232 | */ |
1233 | 1233 | public static function get_user_locale($user_id = 0) |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
3 | - exit('NO direct script access allowed'); |
|
3 | + exit('NO direct script access allowed'); |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -26,364 +26,364 @@ discard block |
||
26 | 26 | { |
27 | 27 | |
28 | 28 | |
29 | - /** |
|
30 | - * return the timezone set for the WP install |
|
31 | - * |
|
32 | - * @return string valid timezone string for PHP DateTimeZone() class |
|
33 | - */ |
|
34 | - public static function get_timezone() |
|
35 | - { |
|
36 | - return EEH_DTT_Helper::get_valid_timezone_string(); |
|
37 | - } |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * get_valid_timezone_string |
|
42 | - * ensures that a valid timezone string is returned |
|
43 | - * |
|
44 | - * @access protected |
|
45 | - * @param string $timezone_string |
|
46 | - * @return string |
|
47 | - * @throws \EE_Error |
|
48 | - */ |
|
49 | - public static function get_valid_timezone_string($timezone_string = '') |
|
50 | - { |
|
51 | - // if passed a value, then use that, else get WP option |
|
52 | - $timezone_string = ! empty($timezone_string) ? $timezone_string : get_option('timezone_string'); |
|
53 | - // value from above exists, use that, else get timezone string from gmt_offset |
|
54 | - $timezone_string = ! empty($timezone_string) ? $timezone_string : EEH_DTT_Helper::get_timezone_string_from_gmt_offset(); |
|
55 | - EEH_DTT_Helper::validate_timezone($timezone_string); |
|
56 | - return $timezone_string; |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * This only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
62 | - * |
|
63 | - * @static |
|
64 | - * @access public |
|
65 | - * @param string $timezone_string Timezone string to check |
|
66 | - * @param bool $throw_error |
|
67 | - * @return bool |
|
68 | - * @throws \EE_Error |
|
69 | - */ |
|
70 | - public static function validate_timezone($timezone_string, $throw_error = true) |
|
71 | - { |
|
72 | - // easiest way to test a timezone string is just see if it throws an error when you try to create a DateTimeZone object with it |
|
73 | - try { |
|
74 | - new DateTimeZone($timezone_string); |
|
75 | - } catch (Exception $e) { |
|
76 | - // sometimes we take exception to exceptions |
|
77 | - if (! $throw_error) { |
|
78 | - return false; |
|
79 | - } |
|
80 | - throw new EE_Error( |
|
81 | - sprintf( |
|
82 | - __('The timezone given (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
83 | - 'event_espresso'), |
|
84 | - $timezone_string, |
|
85 | - '<a href="http://www.php.net/manual/en/timezones.php">', |
|
86 | - '</a>' |
|
87 | - ) |
|
88 | - ); |
|
89 | - } |
|
90 | - return true; |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * _create_timezone_object_from_timezone_name |
|
96 | - * |
|
97 | - * @access protected |
|
98 | - * @param string $gmt_offset |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - public static function get_timezone_string_from_gmt_offset($gmt_offset = '') |
|
102 | - { |
|
103 | - $timezone_string = 'UTC'; |
|
104 | - //if there is no incoming gmt_offset, then because WP hooks in on timezone_string, we need to see if that is |
|
105 | - //set because it will override `gmt_offset` via `pre_get_option` filter. If that's set, then let's just use |
|
106 | - //that! Otherwise we'll leave timezone_string at the default of 'UTC' before doing other logic. |
|
107 | - if ($gmt_offset === '') { |
|
108 | - //autoloaded so no need to set to a variable. There will not be multiple hits to the db. |
|
109 | - if (get_option('timezone_string')) { |
|
110 | - return get_option('timezone_string'); |
|
111 | - } |
|
112 | - } |
|
113 | - $gmt_offset = $gmt_offset !== '' ? $gmt_offset : get_option('gmt_offset'); |
|
114 | - $gmt_offset = (float) $gmt_offset; |
|
115 | - |
|
116 | - //if $gmt_offset is 0, then just return UTC |
|
117 | - if ($gmt_offset === (float) 0) { |
|
118 | - return $timezone_string; |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - if ($gmt_offset !== '') { |
|
123 | - // convert GMT offset to seconds |
|
124 | - $gmt_offset = $gmt_offset * HOUR_IN_SECONDS; |
|
125 | - // although we don't know the TZ abbreviation, we know the UTC offset |
|
126 | - $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
|
127 | - //only use this timezone_string IF it's current offset matches the given offset |
|
128 | - try { |
|
129 | - $offset = self::get_timezone_offset(new DateTimeZone($timezone_string)); |
|
130 | - if ($offset !== $gmt_offset) { |
|
131 | - $timezone_string = false; |
|
132 | - } |
|
133 | - } catch (Exception $e) { |
|
134 | - $timezone_string = false; |
|
135 | - } |
|
136 | - } |
|
137 | - // better have a valid timezone string by now, but if not, sigh... loop thru the timezone_abbreviations_list()... |
|
138 | - $timezone_string = $timezone_string !== false |
|
139 | - ? $timezone_string |
|
140 | - : EEH_DTT_Helper::get_timezone_string_from_abbreviations_list($gmt_offset); |
|
141 | - return $timezone_string; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Gets the site's GMT offset based on either the timezone string |
|
146 | - * (in which case teh gmt offset will vary depending on the location's |
|
147 | - * observance of daylight savings time) or the gmt_offset wp option |
|
148 | - * |
|
149 | - * @return int seconds offset |
|
150 | - */ |
|
151 | - public static function get_site_timezone_gmt_offset() |
|
152 | - { |
|
153 | - $timezone_string = get_option('timezone_string'); |
|
154 | - if ($timezone_string) { |
|
155 | - try { |
|
156 | - $timezone = new DateTimeZone($timezone_string); |
|
157 | - return $timezone->getOffset(new DateTime()); //in WordPress DateTime defaults to UTC |
|
158 | - } catch (Exception $e) { |
|
159 | - } |
|
160 | - } |
|
161 | - $offset = get_option('gmt_offset'); |
|
162 | - return (int)($offset * HOUR_IN_SECONDS); |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * Depending on PHP version, there might not bevalid current timezone strings to match these gmt_offsets in its |
|
168 | - * timezone tables. |
|
169 | - * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
170 | - * |
|
171 | - * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
172 | - * |
|
173 | - * @access public |
|
174 | - * @param int $gmt_offset |
|
175 | - * @return int |
|
176 | - */ |
|
177 | - public static function adjust_invalid_gmt_offsets($gmt_offset = 0) |
|
178 | - { |
|
179 | - //make sure $gmt_offset is int |
|
180 | - $gmt_offset = (int)$gmt_offset; |
|
181 | - switch ($gmt_offset) { |
|
182 | - //-12 |
|
183 | - case -43200: |
|
184 | - $gmt_offset = -39600; |
|
185 | - break; |
|
186 | - //-11.5 |
|
187 | - case -41400: |
|
188 | - $gmt_offset = -39600; |
|
189 | - break; |
|
190 | - //-10.5 |
|
191 | - case -37800: |
|
192 | - $gmt_offset = -39600; |
|
193 | - break; |
|
194 | - //-8.5 |
|
195 | - case -30600: |
|
196 | - $gmt_offset = -28800; |
|
197 | - break; |
|
198 | - //-7.5 |
|
199 | - case -27000: |
|
200 | - $gmt_offset = -25200; |
|
201 | - break; |
|
202 | - //-6.5 |
|
203 | - case -23400: |
|
204 | - $gmt_offset = -21600; |
|
205 | - break; |
|
206 | - //-5.5 |
|
207 | - case -19800: |
|
208 | - $gmt_offset = -18000; |
|
209 | - break; |
|
210 | - //-4.5 |
|
211 | - case -16200: |
|
212 | - $gmt_offset = -14400; |
|
213 | - break; |
|
214 | - //-3.5 |
|
215 | - case -12600: |
|
216 | - $gmt_offset = -10800; |
|
217 | - break; |
|
218 | - //-2.5 |
|
219 | - case -9000: |
|
220 | - $gmt_offset = -7200; |
|
221 | - break; |
|
222 | - //-1.5 |
|
223 | - case -5400: |
|
224 | - $gmt_offset = -3600; |
|
225 | - break; |
|
226 | - //-0.5 |
|
227 | - case -1800: |
|
228 | - $gmt_offset = 0; |
|
229 | - break; |
|
230 | - //.5 |
|
231 | - case 1800: |
|
232 | - $gmt_offset = 3600; |
|
233 | - break; |
|
234 | - //1.5 |
|
235 | - case 5400: |
|
236 | - $gmt_offset = 7200; |
|
237 | - break; |
|
238 | - //2.5 |
|
239 | - case 9000: |
|
240 | - $gmt_offset = 10800; |
|
241 | - break; |
|
242 | - //3.5 |
|
243 | - case 12600: |
|
244 | - $gmt_offset = 14400; |
|
245 | - break; |
|
246 | - |
|
247 | - //7.5 |
|
248 | - case 27000: |
|
249 | - $gmt_offset = 28800; |
|
250 | - break; |
|
251 | - //8.5 |
|
252 | - case 30600: |
|
253 | - $gmt_offset = 31500; |
|
254 | - break; |
|
255 | - //10.5 |
|
256 | - case 37800: |
|
257 | - $gmt_offset = 39600; |
|
258 | - break; |
|
259 | - //11.5 |
|
260 | - case 41400: |
|
261 | - $gmt_offset = 43200; |
|
262 | - break; |
|
263 | - //12.75 |
|
264 | - case 45900: |
|
265 | - $gmt_offset = 46800; |
|
266 | - break; |
|
267 | - //13.75 |
|
268 | - case 49500: |
|
269 | - $gmt_offset = 50400; |
|
270 | - break; |
|
271 | - } |
|
272 | - return $gmt_offset; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * get_timezone_string_from_abbreviations_list |
|
278 | - * |
|
279 | - * @access public |
|
280 | - * @param int $gmt_offset |
|
281 | - * @param bool $coerce If true, we attempt to coerce with our adjustment table @see self::adjust_invalid_gmt_offset. |
|
282 | - * @return string |
|
283 | - * @throws \EE_Error |
|
284 | - */ |
|
285 | - public static function get_timezone_string_from_abbreviations_list($gmt_offset = 0, $coerce = true) |
|
286 | - { |
|
287 | - $abbreviations = timezone_abbreviations_list(); |
|
288 | - foreach ($abbreviations as $abbreviation) { |
|
289 | - foreach ($abbreviation as $city) { |
|
290 | - if ($city['offset'] === $gmt_offset && $city['dst'] === false) { |
|
291 | - try { |
|
292 | - $offset = self::get_timezone_offset(new DateTimeZone($city['timezone_id'])); |
|
293 | - if ($offset !== $gmt_offset) { |
|
294 | - continue; |
|
295 | - } else { |
|
296 | - return $city['timezone_id']; |
|
297 | - } |
|
298 | - } catch (Exception $e) { |
|
299 | - continue; |
|
300 | - } |
|
301 | - } |
|
302 | - } |
|
303 | - } |
|
304 | - //if $coerce is true, let's see if we can get a timezone string after the offset is adjusted |
|
305 | - if ($coerce == true) { |
|
306 | - $timezone_string = self::get_timezone_string_from_abbreviations_list( |
|
307 | - self::adjust_invalid_gmt_offsets($gmt_offset), |
|
308 | - false |
|
309 | - ); |
|
310 | - if ($timezone_string) { |
|
311 | - return $timezone_string; |
|
312 | - } |
|
313 | - } |
|
314 | - throw new EE_Error( |
|
315 | - sprintf( |
|
316 | - __('The provided GMT offset (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
317 | - 'event_espresso'), |
|
318 | - $gmt_offset, |
|
319 | - '<a href="http://www.php.net/manual/en/timezones.php">', |
|
320 | - '</a>' |
|
321 | - ) |
|
322 | - ); |
|
323 | - } |
|
324 | - |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Get Timezone Transitions |
|
329 | - * @param \DateTimeZone $date_time_zone |
|
330 | - * @param null $time |
|
331 | - * @param bool $first_only |
|
332 | - * @return array|mixed |
|
333 | - */ |
|
334 | - public static function get_timezone_transitions(DateTimeZone $date_time_zone, $time = null, $first_only = true) |
|
335 | - { |
|
336 | - $time = is_int($time) || $time === null ? $time : strtotime($time); |
|
337 | - $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
338 | - $transitions = $date_time_zone->getTransitions($time); |
|
339 | - return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
|
340 | - } |
|
341 | - |
|
342 | - |
|
343 | - /** |
|
344 | - * Get Timezone Offset for given timezone object. |
|
345 | - * @param \DateTimeZone $date_time_zone |
|
346 | - * @param null $time |
|
347 | - * @return mixed |
|
348 | - * @throws \DomainException |
|
349 | - */ |
|
350 | - public static function get_timezone_offset(DateTimeZone $date_time_zone, $time = null) |
|
351 | - { |
|
352 | - $transitions = self::get_timezone_transitions($date_time_zone, $time); |
|
353 | - if (! isset($transitions['offset'])) { |
|
354 | - throw new DomainException(); |
|
355 | - } |
|
356 | - return $transitions['offset']; |
|
357 | - } |
|
358 | - |
|
359 | - |
|
360 | - /** |
|
361 | - * @access public |
|
362 | - * @param string $timezone_string |
|
363 | - */ |
|
364 | - public static function timezone_select_input($timezone_string = '') |
|
365 | - { |
|
366 | - // get WP date time format |
|
367 | - $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
368 | - // if passed a value, then use that, else get WP option |
|
369 | - $timezone_string = ! empty($timezone_string) ? $timezone_string : get_option('timezone_string'); |
|
370 | - // check if the timezone is valid but don't throw any errors if it isn't |
|
371 | - $timezone_string = EEH_DTT_Helper::validate_timezone($timezone_string, false); |
|
372 | - $gmt_offset = get_option('gmt_offset'); |
|
373 | - |
|
374 | - $check_zone_info = true; |
|
375 | - if (empty($timezone_string)) { |
|
376 | - // Create a UTC+- zone if no timezone string exists |
|
377 | - $check_zone_info = false; |
|
378 | - if ($gmt_offset > 0) { |
|
379 | - $timezone_string = 'UTC+' . $gmt_offset; |
|
380 | - } elseif ($gmt_offset < 0) { |
|
381 | - $timezone_string = 'UTC' . $gmt_offset; |
|
382 | - } else { |
|
383 | - $timezone_string = 'UTC'; |
|
384 | - } |
|
385 | - } |
|
386 | - ?> |
|
29 | + /** |
|
30 | + * return the timezone set for the WP install |
|
31 | + * |
|
32 | + * @return string valid timezone string for PHP DateTimeZone() class |
|
33 | + */ |
|
34 | + public static function get_timezone() |
|
35 | + { |
|
36 | + return EEH_DTT_Helper::get_valid_timezone_string(); |
|
37 | + } |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * get_valid_timezone_string |
|
42 | + * ensures that a valid timezone string is returned |
|
43 | + * |
|
44 | + * @access protected |
|
45 | + * @param string $timezone_string |
|
46 | + * @return string |
|
47 | + * @throws \EE_Error |
|
48 | + */ |
|
49 | + public static function get_valid_timezone_string($timezone_string = '') |
|
50 | + { |
|
51 | + // if passed a value, then use that, else get WP option |
|
52 | + $timezone_string = ! empty($timezone_string) ? $timezone_string : get_option('timezone_string'); |
|
53 | + // value from above exists, use that, else get timezone string from gmt_offset |
|
54 | + $timezone_string = ! empty($timezone_string) ? $timezone_string : EEH_DTT_Helper::get_timezone_string_from_gmt_offset(); |
|
55 | + EEH_DTT_Helper::validate_timezone($timezone_string); |
|
56 | + return $timezone_string; |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * This only purpose for this static method is to validate that the incoming timezone is a valid php timezone. |
|
62 | + * |
|
63 | + * @static |
|
64 | + * @access public |
|
65 | + * @param string $timezone_string Timezone string to check |
|
66 | + * @param bool $throw_error |
|
67 | + * @return bool |
|
68 | + * @throws \EE_Error |
|
69 | + */ |
|
70 | + public static function validate_timezone($timezone_string, $throw_error = true) |
|
71 | + { |
|
72 | + // easiest way to test a timezone string is just see if it throws an error when you try to create a DateTimeZone object with it |
|
73 | + try { |
|
74 | + new DateTimeZone($timezone_string); |
|
75 | + } catch (Exception $e) { |
|
76 | + // sometimes we take exception to exceptions |
|
77 | + if (! $throw_error) { |
|
78 | + return false; |
|
79 | + } |
|
80 | + throw new EE_Error( |
|
81 | + sprintf( |
|
82 | + __('The timezone given (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
83 | + 'event_espresso'), |
|
84 | + $timezone_string, |
|
85 | + '<a href="http://www.php.net/manual/en/timezones.php">', |
|
86 | + '</a>' |
|
87 | + ) |
|
88 | + ); |
|
89 | + } |
|
90 | + return true; |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * _create_timezone_object_from_timezone_name |
|
96 | + * |
|
97 | + * @access protected |
|
98 | + * @param string $gmt_offset |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + public static function get_timezone_string_from_gmt_offset($gmt_offset = '') |
|
102 | + { |
|
103 | + $timezone_string = 'UTC'; |
|
104 | + //if there is no incoming gmt_offset, then because WP hooks in on timezone_string, we need to see if that is |
|
105 | + //set because it will override `gmt_offset` via `pre_get_option` filter. If that's set, then let's just use |
|
106 | + //that! Otherwise we'll leave timezone_string at the default of 'UTC' before doing other logic. |
|
107 | + if ($gmt_offset === '') { |
|
108 | + //autoloaded so no need to set to a variable. There will not be multiple hits to the db. |
|
109 | + if (get_option('timezone_string')) { |
|
110 | + return get_option('timezone_string'); |
|
111 | + } |
|
112 | + } |
|
113 | + $gmt_offset = $gmt_offset !== '' ? $gmt_offset : get_option('gmt_offset'); |
|
114 | + $gmt_offset = (float) $gmt_offset; |
|
115 | + |
|
116 | + //if $gmt_offset is 0, then just return UTC |
|
117 | + if ($gmt_offset === (float) 0) { |
|
118 | + return $timezone_string; |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + if ($gmt_offset !== '') { |
|
123 | + // convert GMT offset to seconds |
|
124 | + $gmt_offset = $gmt_offset * HOUR_IN_SECONDS; |
|
125 | + // although we don't know the TZ abbreviation, we know the UTC offset |
|
126 | + $timezone_string = timezone_name_from_abbr(null, $gmt_offset); |
|
127 | + //only use this timezone_string IF it's current offset matches the given offset |
|
128 | + try { |
|
129 | + $offset = self::get_timezone_offset(new DateTimeZone($timezone_string)); |
|
130 | + if ($offset !== $gmt_offset) { |
|
131 | + $timezone_string = false; |
|
132 | + } |
|
133 | + } catch (Exception $e) { |
|
134 | + $timezone_string = false; |
|
135 | + } |
|
136 | + } |
|
137 | + // better have a valid timezone string by now, but if not, sigh... loop thru the timezone_abbreviations_list()... |
|
138 | + $timezone_string = $timezone_string !== false |
|
139 | + ? $timezone_string |
|
140 | + : EEH_DTT_Helper::get_timezone_string_from_abbreviations_list($gmt_offset); |
|
141 | + return $timezone_string; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Gets the site's GMT offset based on either the timezone string |
|
146 | + * (in which case teh gmt offset will vary depending on the location's |
|
147 | + * observance of daylight savings time) or the gmt_offset wp option |
|
148 | + * |
|
149 | + * @return int seconds offset |
|
150 | + */ |
|
151 | + public static function get_site_timezone_gmt_offset() |
|
152 | + { |
|
153 | + $timezone_string = get_option('timezone_string'); |
|
154 | + if ($timezone_string) { |
|
155 | + try { |
|
156 | + $timezone = new DateTimeZone($timezone_string); |
|
157 | + return $timezone->getOffset(new DateTime()); //in WordPress DateTime defaults to UTC |
|
158 | + } catch (Exception $e) { |
|
159 | + } |
|
160 | + } |
|
161 | + $offset = get_option('gmt_offset'); |
|
162 | + return (int)($offset * HOUR_IN_SECONDS); |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * Depending on PHP version, there might not bevalid current timezone strings to match these gmt_offsets in its |
|
168 | + * timezone tables. |
|
169 | + * To get around that, for these fringe timezones we bump them to a known valid offset. |
|
170 | + * |
|
171 | + * This method should ONLY be called after first verifying an timezone_string cannot be retrieved for the offset. |
|
172 | + * |
|
173 | + * @access public |
|
174 | + * @param int $gmt_offset |
|
175 | + * @return int |
|
176 | + */ |
|
177 | + public static function adjust_invalid_gmt_offsets($gmt_offset = 0) |
|
178 | + { |
|
179 | + //make sure $gmt_offset is int |
|
180 | + $gmt_offset = (int)$gmt_offset; |
|
181 | + switch ($gmt_offset) { |
|
182 | + //-12 |
|
183 | + case -43200: |
|
184 | + $gmt_offset = -39600; |
|
185 | + break; |
|
186 | + //-11.5 |
|
187 | + case -41400: |
|
188 | + $gmt_offset = -39600; |
|
189 | + break; |
|
190 | + //-10.5 |
|
191 | + case -37800: |
|
192 | + $gmt_offset = -39600; |
|
193 | + break; |
|
194 | + //-8.5 |
|
195 | + case -30600: |
|
196 | + $gmt_offset = -28800; |
|
197 | + break; |
|
198 | + //-7.5 |
|
199 | + case -27000: |
|
200 | + $gmt_offset = -25200; |
|
201 | + break; |
|
202 | + //-6.5 |
|
203 | + case -23400: |
|
204 | + $gmt_offset = -21600; |
|
205 | + break; |
|
206 | + //-5.5 |
|
207 | + case -19800: |
|
208 | + $gmt_offset = -18000; |
|
209 | + break; |
|
210 | + //-4.5 |
|
211 | + case -16200: |
|
212 | + $gmt_offset = -14400; |
|
213 | + break; |
|
214 | + //-3.5 |
|
215 | + case -12600: |
|
216 | + $gmt_offset = -10800; |
|
217 | + break; |
|
218 | + //-2.5 |
|
219 | + case -9000: |
|
220 | + $gmt_offset = -7200; |
|
221 | + break; |
|
222 | + //-1.5 |
|
223 | + case -5400: |
|
224 | + $gmt_offset = -3600; |
|
225 | + break; |
|
226 | + //-0.5 |
|
227 | + case -1800: |
|
228 | + $gmt_offset = 0; |
|
229 | + break; |
|
230 | + //.5 |
|
231 | + case 1800: |
|
232 | + $gmt_offset = 3600; |
|
233 | + break; |
|
234 | + //1.5 |
|
235 | + case 5400: |
|
236 | + $gmt_offset = 7200; |
|
237 | + break; |
|
238 | + //2.5 |
|
239 | + case 9000: |
|
240 | + $gmt_offset = 10800; |
|
241 | + break; |
|
242 | + //3.5 |
|
243 | + case 12600: |
|
244 | + $gmt_offset = 14400; |
|
245 | + break; |
|
246 | + |
|
247 | + //7.5 |
|
248 | + case 27000: |
|
249 | + $gmt_offset = 28800; |
|
250 | + break; |
|
251 | + //8.5 |
|
252 | + case 30600: |
|
253 | + $gmt_offset = 31500; |
|
254 | + break; |
|
255 | + //10.5 |
|
256 | + case 37800: |
|
257 | + $gmt_offset = 39600; |
|
258 | + break; |
|
259 | + //11.5 |
|
260 | + case 41400: |
|
261 | + $gmt_offset = 43200; |
|
262 | + break; |
|
263 | + //12.75 |
|
264 | + case 45900: |
|
265 | + $gmt_offset = 46800; |
|
266 | + break; |
|
267 | + //13.75 |
|
268 | + case 49500: |
|
269 | + $gmt_offset = 50400; |
|
270 | + break; |
|
271 | + } |
|
272 | + return $gmt_offset; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * get_timezone_string_from_abbreviations_list |
|
278 | + * |
|
279 | + * @access public |
|
280 | + * @param int $gmt_offset |
|
281 | + * @param bool $coerce If true, we attempt to coerce with our adjustment table @see self::adjust_invalid_gmt_offset. |
|
282 | + * @return string |
|
283 | + * @throws \EE_Error |
|
284 | + */ |
|
285 | + public static function get_timezone_string_from_abbreviations_list($gmt_offset = 0, $coerce = true) |
|
286 | + { |
|
287 | + $abbreviations = timezone_abbreviations_list(); |
|
288 | + foreach ($abbreviations as $abbreviation) { |
|
289 | + foreach ($abbreviation as $city) { |
|
290 | + if ($city['offset'] === $gmt_offset && $city['dst'] === false) { |
|
291 | + try { |
|
292 | + $offset = self::get_timezone_offset(new DateTimeZone($city['timezone_id'])); |
|
293 | + if ($offset !== $gmt_offset) { |
|
294 | + continue; |
|
295 | + } else { |
|
296 | + return $city['timezone_id']; |
|
297 | + } |
|
298 | + } catch (Exception $e) { |
|
299 | + continue; |
|
300 | + } |
|
301 | + } |
|
302 | + } |
|
303 | + } |
|
304 | + //if $coerce is true, let's see if we can get a timezone string after the offset is adjusted |
|
305 | + if ($coerce == true) { |
|
306 | + $timezone_string = self::get_timezone_string_from_abbreviations_list( |
|
307 | + self::adjust_invalid_gmt_offsets($gmt_offset), |
|
308 | + false |
|
309 | + ); |
|
310 | + if ($timezone_string) { |
|
311 | + return $timezone_string; |
|
312 | + } |
|
313 | + } |
|
314 | + throw new EE_Error( |
|
315 | + sprintf( |
|
316 | + __('The provided GMT offset (%1$s), is invalid, please check with %2$sthis list%3$s for what valid timezones can be used', |
|
317 | + 'event_espresso'), |
|
318 | + $gmt_offset, |
|
319 | + '<a href="http://www.php.net/manual/en/timezones.php">', |
|
320 | + '</a>' |
|
321 | + ) |
|
322 | + ); |
|
323 | + } |
|
324 | + |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Get Timezone Transitions |
|
329 | + * @param \DateTimeZone $date_time_zone |
|
330 | + * @param null $time |
|
331 | + * @param bool $first_only |
|
332 | + * @return array|mixed |
|
333 | + */ |
|
334 | + public static function get_timezone_transitions(DateTimeZone $date_time_zone, $time = null, $first_only = true) |
|
335 | + { |
|
336 | + $time = is_int($time) || $time === null ? $time : strtotime($time); |
|
337 | + $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time(); |
|
338 | + $transitions = $date_time_zone->getTransitions($time); |
|
339 | + return $first_only && ! isset($transitions['ts']) ? reset($transitions) : $transitions; |
|
340 | + } |
|
341 | + |
|
342 | + |
|
343 | + /** |
|
344 | + * Get Timezone Offset for given timezone object. |
|
345 | + * @param \DateTimeZone $date_time_zone |
|
346 | + * @param null $time |
|
347 | + * @return mixed |
|
348 | + * @throws \DomainException |
|
349 | + */ |
|
350 | + public static function get_timezone_offset(DateTimeZone $date_time_zone, $time = null) |
|
351 | + { |
|
352 | + $transitions = self::get_timezone_transitions($date_time_zone, $time); |
|
353 | + if (! isset($transitions['offset'])) { |
|
354 | + throw new DomainException(); |
|
355 | + } |
|
356 | + return $transitions['offset']; |
|
357 | + } |
|
358 | + |
|
359 | + |
|
360 | + /** |
|
361 | + * @access public |
|
362 | + * @param string $timezone_string |
|
363 | + */ |
|
364 | + public static function timezone_select_input($timezone_string = '') |
|
365 | + { |
|
366 | + // get WP date time format |
|
367 | + $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
368 | + // if passed a value, then use that, else get WP option |
|
369 | + $timezone_string = ! empty($timezone_string) ? $timezone_string : get_option('timezone_string'); |
|
370 | + // check if the timezone is valid but don't throw any errors if it isn't |
|
371 | + $timezone_string = EEH_DTT_Helper::validate_timezone($timezone_string, false); |
|
372 | + $gmt_offset = get_option('gmt_offset'); |
|
373 | + |
|
374 | + $check_zone_info = true; |
|
375 | + if (empty($timezone_string)) { |
|
376 | + // Create a UTC+- zone if no timezone string exists |
|
377 | + $check_zone_info = false; |
|
378 | + if ($gmt_offset > 0) { |
|
379 | + $timezone_string = 'UTC+' . $gmt_offset; |
|
380 | + } elseif ($gmt_offset < 0) { |
|
381 | + $timezone_string = 'UTC' . $gmt_offset; |
|
382 | + } else { |
|
383 | + $timezone_string = 'UTC'; |
|
384 | + } |
|
385 | + } |
|
386 | + ?> |
|
387 | 387 | |
388 | 388 | <p> |
389 | 389 | <label for="timezone_string"><?php _e('timezone'); ?></label> |
@@ -396,13 +396,13 @@ discard block |
||
396 | 396 | |
397 | 397 | <p> |
398 | 398 | <span><?php |
399 | - printf( |
|
400 | - __('%1$sUTC%2$s time is %3$s'), |
|
401 | - '<abbr title="Coordinated Universal Time">', |
|
402 | - '</abbr>', |
|
403 | - '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
404 | - ); |
|
405 | - ?></span> |
|
399 | + printf( |
|
400 | + __('%1$sUTC%2$s time is %3$s'), |
|
401 | + '<abbr title="Coordinated Universal Time">', |
|
402 | + '</abbr>', |
|
403 | + '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
404 | + ); |
|
405 | + ?></span> |
|
406 | 406 | <?php if (! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
407 | 407 | <br/><span><?php printf(__('Local time is %1$s'), '<code>' . date_i18n($datetime_format) . '</code>'); ?></span> |
408 | 408 | <?php endif; ?> |
@@ -411,693 +411,693 @@ discard block |
||
411 | 411 | <br/> |
412 | 412 | <span> |
413 | 413 | <?php |
414 | - // Set TZ so localtime works. |
|
415 | - date_default_timezone_set($timezone_string); |
|
416 | - $now = localtime(time(), true); |
|
417 | - if ($now['tm_isdst']) { |
|
418 | - _e('This timezone is currently in daylight saving time.'); |
|
419 | - } else { |
|
420 | - _e('This timezone is currently in standard time.'); |
|
421 | - } |
|
422 | - ?> |
|
414 | + // Set TZ so localtime works. |
|
415 | + date_default_timezone_set($timezone_string); |
|
416 | + $now = localtime(time(), true); |
|
417 | + if ($now['tm_isdst']) { |
|
418 | + _e('This timezone is currently in daylight saving time.'); |
|
419 | + } else { |
|
420 | + _e('This timezone is currently in standard time.'); |
|
421 | + } |
|
422 | + ?> |
|
423 | 423 | <br/> |
424 | 424 | <?php |
425 | - if (function_exists('timezone_transitions_get')) { |
|
426 | - $found = false; |
|
427 | - $date_time_zone_selected = new DateTimeZone($timezone_string); |
|
428 | - $tz_offset = timezone_offset_get($date_time_zone_selected, date_create()); |
|
429 | - $right_now = time(); |
|
430 | - $tr['isdst'] = false; |
|
431 | - foreach (timezone_transitions_get($date_time_zone_selected) as $tr) { |
|
432 | - if ($tr['ts'] > $right_now) { |
|
433 | - $found = true; |
|
434 | - break; |
|
435 | - } |
|
436 | - } |
|
437 | - |
|
438 | - if ($found) { |
|
439 | - $message = $tr['isdst'] ? |
|
440 | - __(' Daylight saving time begins on: %s.') : |
|
441 | - __(' Standard time begins on: %s.'); |
|
442 | - // Add the difference between the current offset and the new offset to ts to get the correct transition time from date_i18n(). |
|
443 | - printf($message, |
|
444 | - '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >'); |
|
445 | - } else { |
|
446 | - _e('This timezone does not observe daylight saving time.'); |
|
447 | - } |
|
448 | - } |
|
449 | - // Set back to UTC. |
|
450 | - date_default_timezone_set('UTC'); |
|
451 | - ?> |
|
425 | + if (function_exists('timezone_transitions_get')) { |
|
426 | + $found = false; |
|
427 | + $date_time_zone_selected = new DateTimeZone($timezone_string); |
|
428 | + $tz_offset = timezone_offset_get($date_time_zone_selected, date_create()); |
|
429 | + $right_now = time(); |
|
430 | + $tr['isdst'] = false; |
|
431 | + foreach (timezone_transitions_get($date_time_zone_selected) as $tr) { |
|
432 | + if ($tr['ts'] > $right_now) { |
|
433 | + $found = true; |
|
434 | + break; |
|
435 | + } |
|
436 | + } |
|
437 | + |
|
438 | + if ($found) { |
|
439 | + $message = $tr['isdst'] ? |
|
440 | + __(' Daylight saving time begins on: %s.') : |
|
441 | + __(' Standard time begins on: %s.'); |
|
442 | + // Add the difference between the current offset and the new offset to ts to get the correct transition time from date_i18n(). |
|
443 | + printf($message, |
|
444 | + '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >'); |
|
445 | + } else { |
|
446 | + _e('This timezone does not observe daylight saving time.'); |
|
447 | + } |
|
448 | + } |
|
449 | + // Set back to UTC. |
|
450 | + date_default_timezone_set('UTC'); |
|
451 | + ?> |
|
452 | 452 | </span></p> |
453 | 453 | <?php |
454 | - endif; |
|
455 | - } |
|
456 | - |
|
457 | - |
|
458 | - /** |
|
459 | - * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
460 | - * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
461 | - * the site is used. |
|
462 | - * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
463 | - * computed timestamp (i.e. date_i18n() ) |
|
464 | - * |
|
465 | - * @param int $unix_timestamp if 0, then time() will be used. |
|
466 | - * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
467 | - * site will be used. |
|
468 | - * @return int $unix_timestamp with the offset applied for the given timezone. |
|
469 | - */ |
|
470 | - public static function get_timestamp_with_offset($unix_timestamp = 0, $timezone_string = '') |
|
471 | - { |
|
472 | - $unix_timestamp = $unix_timestamp === 0 ? time() : (int)$unix_timestamp; |
|
473 | - $timezone_string = self::get_valid_timezone_string($timezone_string); |
|
474 | - $TimeZone = new DateTimeZone($timezone_string); |
|
475 | - |
|
476 | - $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
477 | - $offset = timezone_offset_get($TimeZone, $DateTime); |
|
478 | - return (int)$DateTime->format('U') + (int)$offset; |
|
479 | - } |
|
480 | - |
|
481 | - |
|
482 | - /** |
|
483 | - * _set_date_time_field |
|
484 | - * modifies EE_Base_Class EE_Datetime_Field objects |
|
485 | - * |
|
486 | - * @param EE_Base_Class $obj EE_Base_Class object |
|
487 | - * @param DateTime $DateTime PHP DateTime object |
|
488 | - * @param string $datetime_field_name the datetime fieldname to be manipulated |
|
489 | - * @return EE_Base_Class |
|
490 | - */ |
|
491 | - protected static function _set_date_time_field(EE_Base_Class $obj, DateTime $DateTime, $datetime_field_name) |
|
492 | - { |
|
493 | - // grab current datetime format |
|
494 | - $current_format = $obj->get_format(); |
|
495 | - // set new full timestamp format |
|
496 | - $obj->set_date_format(EE_Datetime_Field::mysql_date_format); |
|
497 | - $obj->set_time_format(EE_Datetime_Field::mysql_time_format); |
|
498 | - // set the new date value using a full timestamp format so that no data is lost |
|
499 | - $obj->set($datetime_field_name, $DateTime->format(EE_Datetime_Field::mysql_timestamp_format)); |
|
500 | - // reset datetime formats |
|
501 | - $obj->set_date_format($current_format[0]); |
|
502 | - $obj->set_time_format($current_format[1]); |
|
503 | - return $obj; |
|
504 | - } |
|
505 | - |
|
506 | - |
|
507 | - /** |
|
508 | - * date_time_add |
|
509 | - * helper for doing simple datetime calculations on a given datetime from EE_Base_Class |
|
510 | - * and modifying it IN the EE_Base_Class so you don't have to do anything else. |
|
511 | - * |
|
512 | - * @param EE_Base_Class $obj EE_Base_Class object |
|
513 | - * @param string $datetime_field_name name of the EE_Datetime_Filed datatype db column to be manipulated |
|
514 | - * @param string $period what you are adding. The options are (years, months, days, hours, |
|
515 | - * minutes, seconds) defaults to years |
|
516 | - * @param integer $value what you want to increment the time by |
|
517 | - * @return EE_Base_Class return the EE_Base_Class object so right away you can do something with it |
|
518 | - * (chaining) |
|
519 | - */ |
|
520 | - public static function date_time_add(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1) |
|
521 | - { |
|
522 | - //get the raw UTC date. |
|
523 | - $DateTime = $obj->get_DateTime_object($datetime_field_name); |
|
524 | - $DateTime = EEH_DTT_Helper::calc_date($DateTime, $period, $value); |
|
525 | - return EEH_DTT_Helper::_set_date_time_field($obj, $DateTime, $datetime_field_name); |
|
526 | - } |
|
527 | - |
|
528 | - |
|
529 | - /** |
|
530 | - * date_time_subtract |
|
531 | - * same as date_time_add except subtracting value instead of adding. |
|
532 | - * |
|
533 | - * @param \EE_Base_Class $obj |
|
534 | - * @param string $datetime_field_name name of the EE_Datetime_Filed datatype db column to be manipulated |
|
535 | - * @param string $period |
|
536 | - * @param int $value |
|
537 | - * @return \EE_Base_Class |
|
538 | - */ |
|
539 | - public static function date_time_subtract(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1) |
|
540 | - { |
|
541 | - //get the raw UTC date |
|
542 | - $DateTime = $obj->get_DateTime_object($datetime_field_name); |
|
543 | - $DateTime = EEH_DTT_Helper::calc_date($DateTime, $period, $value, '-'); |
|
544 | - return EEH_DTT_Helper::_set_date_time_field($obj, $DateTime, $datetime_field_name); |
|
545 | - } |
|
546 | - |
|
547 | - |
|
548 | - /** |
|
549 | - * Simply takes an incoming DateTime object and does calculations on it based on the incoming parameters |
|
550 | - * |
|
551 | - * @param DateTime $DateTime DateTime object |
|
552 | - * @param string $period a value to indicate what interval is being used in the calculation. The options are |
|
553 | - * 'years', 'months', 'days', 'hours', 'minutes', 'seconds'. Defaults to years. |
|
554 | - * @param integer $value What you want to increment the date by |
|
555 | - * @param string $operand What operand you wish to use for the calculation |
|
556 | - * @return \DateTime return whatever type came in. |
|
557 | - * @throws \EE_Error |
|
558 | - */ |
|
559 | - protected static function _modify_datetime_object(DateTime $DateTime, $period = 'years', $value = 1, $operand = '+') |
|
560 | - { |
|
561 | - if (! $DateTime instanceof DateTime) { |
|
562 | - throw new EE_Error( |
|
563 | - sprintf( |
|
564 | - __('Expected a PHP DateTime object, but instead received %1$s', 'event_espresso'), |
|
565 | - print_r($DateTime, true) |
|
566 | - ) |
|
567 | - ); |
|
568 | - } |
|
569 | - switch ($period) { |
|
570 | - case 'years' : |
|
571 | - $value = 'P' . $value . 'Y'; |
|
572 | - break; |
|
573 | - case 'months' : |
|
574 | - $value = 'P' . $value . 'M'; |
|
575 | - break; |
|
576 | - case 'weeks' : |
|
577 | - $value = 'P' . $value . 'W'; |
|
578 | - break; |
|
579 | - case 'days' : |
|
580 | - $value = 'P' . $value . 'D'; |
|
581 | - break; |
|
582 | - case 'hours' : |
|
583 | - $value = 'PT' . $value . 'H'; |
|
584 | - break; |
|
585 | - case 'minutes' : |
|
586 | - $value = 'PT' . $value . 'M'; |
|
587 | - break; |
|
588 | - case 'seconds' : |
|
589 | - $value = 'PT' . $value . 'S'; |
|
590 | - break; |
|
591 | - } |
|
592 | - switch ($operand) { |
|
593 | - case '+': |
|
594 | - $DateTime->add(new DateInterval($value)); |
|
595 | - break; |
|
596 | - case '-': |
|
597 | - $DateTime->sub(new DateInterval($value)); |
|
598 | - break; |
|
599 | - } |
|
600 | - return $DateTime; |
|
601 | - } |
|
602 | - |
|
603 | - |
|
604 | - /** |
|
605 | - * Simply takes an incoming Unix timestamp and does calculations on it based on the incoming parameters |
|
606 | - * |
|
607 | - * @param int $timestamp Unix timestamp |
|
608 | - * @param string $period a value to indicate what interval is being used in the calculation. The options are |
|
609 | - * 'years', 'months', 'days', 'hours', 'minutes', 'seconds'. Defaults to years. |
|
610 | - * @param integer $value What you want to increment the date by |
|
611 | - * @param string $operand What operand you wish to use for the calculation |
|
612 | - * @return \DateTime return whatever type came in. |
|
613 | - * @throws \EE_Error |
|
614 | - */ |
|
615 | - protected static function _modify_timestamp($timestamp, $period = 'years', $value = 1, $operand = '+') |
|
616 | - { |
|
617 | - if (! preg_match(EE_Datetime_Field::unix_timestamp_regex, $timestamp)) { |
|
618 | - throw new EE_Error( |
|
619 | - sprintf( |
|
620 | - __('Expected a Unix timestamp, but instead received %1$s', 'event_espresso'), |
|
621 | - print_r($timestamp, true) |
|
622 | - ) |
|
623 | - ); |
|
624 | - } |
|
625 | - switch ($period) { |
|
626 | - case 'years' : |
|
627 | - $value = YEAR_IN_SECONDS * $value; |
|
628 | - break; |
|
629 | - case 'months' : |
|
630 | - $value = YEAR_IN_SECONDS / 12 * $value; |
|
631 | - break; |
|
632 | - case 'weeks' : |
|
633 | - $value = WEEK_IN_SECONDS * $value; |
|
634 | - break; |
|
635 | - case 'days' : |
|
636 | - $value = DAY_IN_SECONDS * $value; |
|
637 | - break; |
|
638 | - case 'hours' : |
|
639 | - $value = HOUR_IN_SECONDS * $value; |
|
640 | - break; |
|
641 | - case 'minutes' : |
|
642 | - $value = MINUTE_IN_SECONDS * $value; |
|
643 | - break; |
|
644 | - } |
|
645 | - switch ($operand) { |
|
646 | - case '+': |
|
647 | - $timestamp += $value; |
|
648 | - break; |
|
649 | - case '-': |
|
650 | - $timestamp -= $value; |
|
651 | - break; |
|
652 | - } |
|
653 | - return $timestamp; |
|
654 | - } |
|
655 | - |
|
656 | - |
|
657 | - /** |
|
658 | - * Simply takes an incoming UTC timestamp or DateTime object and does calculations on it based on the incoming |
|
659 | - * parameters and returns the new timestamp or DateTime. |
|
660 | - * |
|
661 | - * @param int | DateTime $DateTime_or_timestamp DateTime object or Unix timestamp |
|
662 | - * @param string $period a value to indicate what interval is being used in the |
|
663 | - * calculation. The options are 'years', 'months', 'days', 'hours', |
|
664 | - * 'minutes', 'seconds'. Defaults to years. |
|
665 | - * @param integer $value What you want to increment the date by |
|
666 | - * @param string $operand What operand you wish to use for the calculation |
|
667 | - * @return mixed string|DateTime return whatever type came in. |
|
668 | - */ |
|
669 | - public static function calc_date($DateTime_or_timestamp, $period = 'years', $value = 1, $operand = '+') |
|
670 | - { |
|
671 | - if ($DateTime_or_timestamp instanceof DateTime) { |
|
672 | - return EEH_DTT_Helper::_modify_datetime_object($DateTime_or_timestamp, $period, $value, $operand); |
|
673 | - } else if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $DateTime_or_timestamp)) { |
|
674 | - return EEH_DTT_Helper::_modify_timestamp($DateTime_or_timestamp, $period, $value, $operand); |
|
675 | - } else { |
|
676 | - //error |
|
677 | - return $DateTime_or_timestamp; |
|
678 | - } |
|
679 | - } |
|
680 | - |
|
681 | - |
|
682 | - /** |
|
683 | - * The purpose of this helper method is to receive an incoming format string in php date/time format |
|
684 | - * and spit out the js and moment.js equivalent formats. |
|
685 | - * Note, if no format string is given, then it is assumed the user wants what is set for WP. |
|
686 | - * Note, js date and time formats are those used by the jquery-ui datepicker and the jquery-ui date- |
|
687 | - * time picker. |
|
688 | - * |
|
689 | - * @see http://stackoverflow.com/posts/16725290/ for the code inspiration. |
|
690 | - * @param null $date_format_string |
|
691 | - * @param null $time_format_string |
|
692 | - * @return array |
|
693 | - * array( |
|
694 | - * 'js' => array ( |
|
695 | - * 'date' => //date format |
|
696 | - * 'time' => //time format |
|
697 | - * ), |
|
698 | - * 'moment' => //date and time format. |
|
699 | - * ) |
|
700 | - */ |
|
701 | - public static function convert_php_to_js_and_moment_date_formats( |
|
702 | - $date_format_string = null, |
|
703 | - $time_format_string = null |
|
704 | - ) { |
|
705 | - if ($date_format_string === null) { |
|
706 | - $date_format_string = get_option('date_format'); |
|
707 | - } |
|
708 | - |
|
709 | - if ($time_format_string === null) { |
|
710 | - $time_format_string = get_option('time_format'); |
|
711 | - } |
|
712 | - |
|
713 | - $date_format = self::_php_to_js_moment_converter($date_format_string); |
|
714 | - $time_format = self::_php_to_js_moment_converter($time_format_string); |
|
715 | - |
|
716 | - return array( |
|
717 | - 'js' => array( |
|
718 | - 'date' => $date_format['js'], |
|
719 | - 'time' => $time_format['js'], |
|
720 | - ), |
|
721 | - 'moment' => $date_format['moment'] . ' ' . $time_format['moment'], |
|
722 | - ); |
|
723 | - } |
|
724 | - |
|
725 | - |
|
726 | - /** |
|
727 | - * This converts incoming format string into js and moment variations. |
|
728 | - * |
|
729 | - * @param string $format_string incoming php format string |
|
730 | - * @return array js and moment formats. |
|
731 | - */ |
|
732 | - protected static function _php_to_js_moment_converter($format_string) |
|
733 | - { |
|
734 | - /** |
|
735 | - * This is a map of symbols for formats. |
|
736 | - * The index is the php symbol, the equivalent values are in the array. |
|
737 | - * |
|
738 | - * @var array |
|
739 | - */ |
|
740 | - $symbols_map = array( |
|
741 | - // Day |
|
742 | - //01 |
|
743 | - 'd' => array( |
|
744 | - 'js' => 'dd', |
|
745 | - 'moment' => 'DD', |
|
746 | - ), |
|
747 | - //Mon |
|
748 | - 'D' => array( |
|
749 | - 'js' => 'D', |
|
750 | - 'moment' => 'ddd', |
|
751 | - ), |
|
752 | - //1,2,...31 |
|
753 | - 'j' => array( |
|
754 | - 'js' => 'd', |
|
755 | - 'moment' => 'D', |
|
756 | - ), |
|
757 | - //Monday |
|
758 | - 'l' => array( |
|
759 | - 'js' => 'DD', |
|
760 | - 'moment' => 'dddd', |
|
761 | - ), |
|
762 | - //ISO numeric representation of the day of the week (1-6) |
|
763 | - 'N' => array( |
|
764 | - 'js' => '', |
|
765 | - 'moment' => 'E', |
|
766 | - ), |
|
767 | - //st,nd.rd |
|
768 | - 'S' => array( |
|
769 | - 'js' => '', |
|
770 | - 'moment' => 'o', |
|
771 | - ), |
|
772 | - //numeric representation of day of week (0-6) |
|
773 | - 'w' => array( |
|
774 | - 'js' => '', |
|
775 | - 'moment' => 'd', |
|
776 | - ), |
|
777 | - //day of year starting from 0 (0-365) |
|
778 | - 'z' => array( |
|
779 | - 'js' => 'o', |
|
780 | - 'moment' => 'DDD' //note moment does not start with 0 so will need to modify by subtracting 1 |
|
781 | - ), |
|
782 | - // Week |
|
783 | - //ISO-8601 week number of year (weeks starting on monday) |
|
784 | - 'W' => array( |
|
785 | - 'js' => '', |
|
786 | - 'moment' => 'w', |
|
787 | - ), |
|
788 | - // Month |
|
789 | - // January...December |
|
790 | - 'F' => array( |
|
791 | - 'js' => 'MM', |
|
792 | - 'moment' => 'MMMM', |
|
793 | - ), |
|
794 | - //01...12 |
|
795 | - 'm' => array( |
|
796 | - 'js' => 'mm', |
|
797 | - 'moment' => 'MM', |
|
798 | - ), |
|
799 | - //Jan...Dec |
|
800 | - 'M' => array( |
|
801 | - 'js' => 'M', |
|
802 | - 'moment' => 'MMM', |
|
803 | - ), |
|
804 | - //1-12 |
|
805 | - 'n' => array( |
|
806 | - 'js' => 'm', |
|
807 | - 'moment' => 'M', |
|
808 | - ), |
|
809 | - //number of days in given month |
|
810 | - 't' => array( |
|
811 | - 'js' => '', |
|
812 | - 'moment' => '', |
|
813 | - ), |
|
814 | - // Year |
|
815 | - //whether leap year or not 1/0 |
|
816 | - 'L' => array( |
|
817 | - 'js' => '', |
|
818 | - 'moment' => '', |
|
819 | - ), |
|
820 | - //ISO-8601 year number |
|
821 | - 'o' => array( |
|
822 | - 'js' => '', |
|
823 | - 'moment' => 'GGGG', |
|
824 | - ), |
|
825 | - //1999...2003 |
|
826 | - 'Y' => array( |
|
827 | - 'js' => 'yy', |
|
828 | - 'moment' => 'YYYY', |
|
829 | - ), |
|
830 | - //99...03 |
|
831 | - 'y' => array( |
|
832 | - 'js' => 'y', |
|
833 | - 'moment' => 'YY', |
|
834 | - ), |
|
835 | - // Time |
|
836 | - // am/pm |
|
837 | - 'a' => array( |
|
838 | - 'js' => 'tt', |
|
839 | - 'moment' => 'a', |
|
840 | - ), |
|
841 | - // AM/PM |
|
842 | - 'A' => array( |
|
843 | - 'js' => 'TT', |
|
844 | - 'moment' => 'A', |
|
845 | - ), |
|
846 | - // Swatch Internet Time?!? |
|
847 | - 'B' => array( |
|
848 | - 'js' => '', |
|
849 | - 'moment' => '', |
|
850 | - ), |
|
851 | - //1...12 |
|
852 | - 'g' => array( |
|
853 | - 'js' => 'h', |
|
854 | - 'moment' => 'h', |
|
855 | - ), |
|
856 | - //0...23 |
|
857 | - 'G' => array( |
|
858 | - 'js' => 'H', |
|
859 | - 'moment' => 'H', |
|
860 | - ), |
|
861 | - //01...12 |
|
862 | - 'h' => array( |
|
863 | - 'js' => 'hh', |
|
864 | - 'moment' => 'hh', |
|
865 | - ), |
|
866 | - //00...23 |
|
867 | - 'H' => array( |
|
868 | - 'js' => 'HH', |
|
869 | - 'moment' => 'HH', |
|
870 | - ), |
|
871 | - //00..59 |
|
872 | - 'i' => array( |
|
873 | - 'js' => 'mm', |
|
874 | - 'moment' => 'mm', |
|
875 | - ), |
|
876 | - //seconds... 00...59 |
|
877 | - 's' => array( |
|
878 | - 'js' => 'ss', |
|
879 | - 'moment' => 'ss', |
|
880 | - ), |
|
881 | - //microseconds |
|
882 | - 'u' => array( |
|
883 | - 'js' => '', |
|
884 | - 'moment' => '', |
|
885 | - ), |
|
886 | - ); |
|
887 | - $jquery_ui_format = ""; |
|
888 | - $moment_format = ""; |
|
889 | - $escaping = false; |
|
890 | - for ($i = 0; $i < strlen($format_string); $i++) { |
|
891 | - $char = $format_string[$i]; |
|
892 | - if ($char === '\\') { // PHP date format escaping character |
|
893 | - $i++; |
|
894 | - if ($escaping) { |
|
895 | - $jquery_ui_format .= $format_string[$i]; |
|
896 | - $moment_format .= $format_string[$i]; |
|
897 | - } else { |
|
898 | - $jquery_ui_format .= '\'' . $format_string[$i]; |
|
899 | - $moment_format .= $format_string[$i]; |
|
900 | - } |
|
901 | - $escaping = true; |
|
902 | - } else { |
|
903 | - if ($escaping) { |
|
904 | - $jquery_ui_format .= "'"; |
|
905 | - $moment_format .= "'"; |
|
906 | - $escaping = false; |
|
907 | - } |
|
908 | - if (isset($symbols_map[$char])) { |
|
909 | - $jquery_ui_format .= $symbols_map[$char]['js']; |
|
910 | - $moment_format .= $symbols_map[$char]['moment']; |
|
911 | - } else { |
|
912 | - $jquery_ui_format .= $char; |
|
913 | - $moment_format .= $char; |
|
914 | - } |
|
915 | - } |
|
916 | - } |
|
917 | - return array('js' => $jquery_ui_format, 'moment' => $moment_format); |
|
918 | - } |
|
919 | - |
|
920 | - |
|
921 | - /** |
|
922 | - * This takes an incoming format string and validates it to ensure it will work fine with PHP. |
|
923 | - * |
|
924 | - * @param string $format_string Incoming format string for php date(). |
|
925 | - * @return mixed bool|array If all is okay then TRUE is returned. Otherwise an array of validation |
|
926 | - * errors is returned. So for client code calling, check for is_array() to |
|
927 | - * indicate failed validations. |
|
928 | - */ |
|
929 | - public static function validate_format_string($format_string) |
|
930 | - { |
|
931 | - $error_msg = array(); |
|
932 | - //time format checks |
|
933 | - switch (true) { |
|
934 | - case strpos($format_string, 'h') !== false : |
|
935 | - case strpos($format_string, 'g') !== false : |
|
936 | - /** |
|
937 | - * if the time string has a lowercase 'h' which == 12 hour time format and there |
|
938 | - * is not any ante meridiem format ('a' or 'A'). Then throw an error because its |
|
939 | - * too ambiguous and PHP won't be able to figure out whether 1 = 1pm or 1am. |
|
940 | - */ |
|
941 | - if (strpos(strtoupper($format_string), 'A') === false) { |
|
942 | - $error_msg[] = __('There is a time format for 12 hour time but no "a" or "A" to indicate am/pm. Without this distinction, PHP is unable to determine if a "1" for the hour value equals "1pm" or "1am".', |
|
943 | - 'event_espresso'); |
|
944 | - } |
|
945 | - break; |
|
946 | - |
|
947 | - } |
|
948 | - |
|
949 | - return empty($error_msg) ? true : $error_msg; |
|
950 | - } |
|
951 | - |
|
952 | - |
|
953 | - /** |
|
954 | - * If the the first date starts at midnight on one day, and the next date ends at midnight on the |
|
955 | - * very next day then this method will return true. |
|
956 | - * If $date_1 = 2015-12-15 00:00:00 and $date_2 = 2015-12-16 00:00:00 then this function will return true. |
|
957 | - * If $date_1 = 2015-12-15 03:00:00 and $date_2 = 2015-12_16 03:00:00 then this function will return false. |
|
958 | - * If $date_1 = 2015-12-15 00:00:00 and $date_2 = 2015-12-15 00:00:00 then this function will return true. |
|
959 | - * |
|
960 | - * @param mixed $date_1 |
|
961 | - * @param mixed $date_2 |
|
962 | - * @return bool |
|
963 | - */ |
|
964 | - public static function dates_represent_one_24_hour_date($date_1, $date_2) |
|
965 | - { |
|
966 | - |
|
967 | - if ( |
|
968 | - (! $date_1 instanceof DateTime || ! $date_2 instanceof DateTime) || |
|
969 | - ($date_1->format(EE_Datetime_Field::mysql_time_format) != '00:00:00' || $date_2->format(EE_Datetime_Field::mysql_time_format) != '00:00:00') |
|
970 | - ) { |
|
971 | - return false; |
|
972 | - } |
|
973 | - return $date_2->format('U') - $date_1->format('U') == 86400 ? true : false; |
|
974 | - } |
|
975 | - |
|
976 | - |
|
977 | - /** |
|
978 | - * This returns the appropriate query interval string that can be used in sql queries involving mysql Date |
|
979 | - * Functions. |
|
980 | - * |
|
981 | - * @param string $timezone_string A timezone string in a valid format to instantiate a DateTimeZone object. |
|
982 | - * @param string $field_for_interval The Database field that is the interval is applied to in the query. |
|
983 | - * @return string |
|
984 | - */ |
|
985 | - public static function get_sql_query_interval_for_offset($timezone_string, $field_for_interval) |
|
986 | - { |
|
987 | - try { |
|
988 | - /** need to account for timezone offset on the selects */ |
|
989 | - $DateTimeZone = new DateTimeZone($timezone_string); |
|
990 | - } catch (Exception $e) { |
|
991 | - $DateTimeZone = null; |
|
992 | - } |
|
993 | - |
|
994 | - /** |
|
995 | - * Note get_option( 'gmt_offset') returns a value in hours, whereas DateTimeZone::getOffset returns values in seconds. |
|
996 | - * Hence we do the calc for DateTimeZone::getOffset. |
|
997 | - */ |
|
998 | - $offset = $DateTimeZone instanceof DateTimeZone ? ($DateTimeZone->getOffset(new DateTime('now'))) / HOUR_IN_SECONDS : get_option('gmt_offset'); |
|
999 | - $query_interval = $offset < 0 |
|
1000 | - ? 'DATE_SUB(' . $field_for_interval . ', INTERVAL ' . $offset * -1 . ' HOUR)' |
|
1001 | - : 'DATE_ADD(' . $field_for_interval . ', INTERVAL ' . $offset . ' HOUR)'; |
|
1002 | - return $query_interval; |
|
1003 | - } |
|
1004 | - |
|
1005 | - /** |
|
1006 | - * Retrieves the site's default timezone and returns it formatted so it's ready for display |
|
1007 | - * to users. If you want to customize how its displayed feel free to fetch the 'timezone_string' |
|
1008 | - * and 'gmt_offset' WordPress options directly; or use the filter |
|
1009 | - * FHEE__EEH_DTT_Helper__get_timezone_string_for_display |
|
1010 | - * (although note that we remove any HTML that may be added) |
|
1011 | - * |
|
1012 | - * @return string |
|
1013 | - */ |
|
1014 | - public static function get_timezone_string_for_display() |
|
1015 | - { |
|
1016 | - $pretty_timezone = apply_filters('FHEE__EEH_DTT_Helper__get_timezone_string_for_display', ''); |
|
1017 | - if (! empty($pretty_timezone)) { |
|
1018 | - return esc_html($pretty_timezone); |
|
1019 | - } |
|
1020 | - $timezone_string = get_option('timezone_string'); |
|
1021 | - if ($timezone_string) { |
|
1022 | - static $mo_loaded = false; |
|
1023 | - // Load translations for continents and cities just like wp_timezone_choice does |
|
1024 | - if (! $mo_loaded) { |
|
1025 | - $locale = get_locale(); |
|
1026 | - $mofile = WP_LANG_DIR . '/continents-cities-' . $locale . '.mo'; |
|
1027 | - load_textdomain('continents-cities', $mofile); |
|
1028 | - $mo_loaded = true; |
|
1029 | - } |
|
1030 | - //well that was easy. |
|
1031 | - $parts = explode('/', $timezone_string); |
|
1032 | - //remove the continent |
|
1033 | - unset($parts[0]); |
|
1034 | - $t_parts = array(); |
|
1035 | - foreach ($parts as $part) { |
|
1036 | - $t_parts[] = translate(str_replace('_', ' ', $part), 'continents-cities'); |
|
1037 | - } |
|
1038 | - return implode(' - ', $t_parts); |
|
1039 | - } |
|
1040 | - //they haven't set the timezone string, so let's return a string like "UTC+1" |
|
1041 | - $gmt_offset = get_option('gmt_offset'); |
|
1042 | - if (intval($gmt_offset) >= 0) { |
|
1043 | - $prefix = '+'; |
|
1044 | - } else { |
|
1045 | - $prefix = ''; |
|
1046 | - } |
|
1047 | - $parts = explode('.', (string)$gmt_offset); |
|
1048 | - if (count($parts) === 1) { |
|
1049 | - $parts[1] = '00'; |
|
1050 | - } else { |
|
1051 | - //convert the part after the decimal, eg "5" (from x.5) or "25" (from x.25) |
|
1052 | - //to minutes, eg 30 or 15, respectively |
|
1053 | - $hour_fraction = (float)('0.' . $parts[1]); |
|
1054 | - $parts[1] = (string)$hour_fraction * 60; |
|
1055 | - } |
|
1056 | - return sprintf(__('UTC%1$s', 'event_espresso'), $prefix . implode(':', $parts)); |
|
1057 | - } |
|
1058 | - |
|
1059 | - |
|
1060 | - |
|
1061 | - /** |
|
1062 | - * So PHP does this awesome thing where if you are trying to get a timestamp |
|
1063 | - * for a month using a string like "February" or "February 2017", |
|
1064 | - * and you don't specify a day as part of your string, |
|
1065 | - * then PHP will use whatever the current day of the month is. |
|
1066 | - * IF the current day of the month happens to be the 30th or 31st, |
|
1067 | - * then PHP gets really confused by a date like February 30, |
|
1068 | - * so instead of saying |
|
1069 | - * "Hey February only has 28 days (this year)... |
|
1070 | - * ...you must have meant the last day of the month!" |
|
1071 | - * PHP does the next most logical thing, and bumps the date up to March 2nd, |
|
1072 | - * because someone requesting February 30th obviously meant March 1st! |
|
1073 | - * The way around this is to always set the day to the first, |
|
1074 | - * so that the month will stay on the month you wanted. |
|
1075 | - * this method will add that "1" into your date regardless of the format. |
|
1076 | - * |
|
1077 | - * @param string $month |
|
1078 | - * @return string |
|
1079 | - */ |
|
1080 | - public static function first_of_month_timestamp($month = '') |
|
1081 | - { |
|
1082 | - $month = (string)$month; |
|
1083 | - $year = ''; |
|
1084 | - // check if the incoming string has a year in it or not |
|
1085 | - if (preg_match('/\b\d{4}\b/', $month, $matches)) { |
|
1086 | - $year = $matches[0]; |
|
1087 | - // ten remove that from the month string as well as any spaces |
|
1088 | - $month = trim(str_replace($year, '', $month)); |
|
1089 | - // add a space before the year |
|
1090 | - $year = " {$year}"; |
|
1091 | - } |
|
1092 | - // return timestamp for something like "February 1 2017" |
|
1093 | - return strtotime("{$month} 1{$year}"); |
|
1094 | - } |
|
454 | + endif; |
|
455 | + } |
|
456 | + |
|
457 | + |
|
458 | + /** |
|
459 | + * This method will take an incoming unix timestamp and add the offset to it for the given timezone_string. |
|
460 | + * If no unix timestamp is given then time() is used. If no timezone is given then the set timezone string for |
|
461 | + * the site is used. |
|
462 | + * This is used typically when using a Unix timestamp any core WP functions that expect their specially |
|
463 | + * computed timestamp (i.e. date_i18n() ) |
|
464 | + * |
|
465 | + * @param int $unix_timestamp if 0, then time() will be used. |
|
466 | + * @param string $timezone_string timezone_string. If empty, then the current set timezone for the |
|
467 | + * site will be used. |
|
468 | + * @return int $unix_timestamp with the offset applied for the given timezone. |
|
469 | + */ |
|
470 | + public static function get_timestamp_with_offset($unix_timestamp = 0, $timezone_string = '') |
|
471 | + { |
|
472 | + $unix_timestamp = $unix_timestamp === 0 ? time() : (int)$unix_timestamp; |
|
473 | + $timezone_string = self::get_valid_timezone_string($timezone_string); |
|
474 | + $TimeZone = new DateTimeZone($timezone_string); |
|
475 | + |
|
476 | + $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
477 | + $offset = timezone_offset_get($TimeZone, $DateTime); |
|
478 | + return (int)$DateTime->format('U') + (int)$offset; |
|
479 | + } |
|
480 | + |
|
481 | + |
|
482 | + /** |
|
483 | + * _set_date_time_field |
|
484 | + * modifies EE_Base_Class EE_Datetime_Field objects |
|
485 | + * |
|
486 | + * @param EE_Base_Class $obj EE_Base_Class object |
|
487 | + * @param DateTime $DateTime PHP DateTime object |
|
488 | + * @param string $datetime_field_name the datetime fieldname to be manipulated |
|
489 | + * @return EE_Base_Class |
|
490 | + */ |
|
491 | + protected static function _set_date_time_field(EE_Base_Class $obj, DateTime $DateTime, $datetime_field_name) |
|
492 | + { |
|
493 | + // grab current datetime format |
|
494 | + $current_format = $obj->get_format(); |
|
495 | + // set new full timestamp format |
|
496 | + $obj->set_date_format(EE_Datetime_Field::mysql_date_format); |
|
497 | + $obj->set_time_format(EE_Datetime_Field::mysql_time_format); |
|
498 | + // set the new date value using a full timestamp format so that no data is lost |
|
499 | + $obj->set($datetime_field_name, $DateTime->format(EE_Datetime_Field::mysql_timestamp_format)); |
|
500 | + // reset datetime formats |
|
501 | + $obj->set_date_format($current_format[0]); |
|
502 | + $obj->set_time_format($current_format[1]); |
|
503 | + return $obj; |
|
504 | + } |
|
505 | + |
|
1095 | 506 | |
1096 | 507 | /** |
1097 | - * This simply returns the timestamp for tomorrow (midnight next day) in this sites timezone. So it may be midnight |
|
1098 | - * for this sites timezone, but the timestamp could be some other time GMT. |
|
1099 | - */ |
|
1100 | - public static function tomorrow() |
|
508 | + * date_time_add |
|
509 | + * helper for doing simple datetime calculations on a given datetime from EE_Base_Class |
|
510 | + * and modifying it IN the EE_Base_Class so you don't have to do anything else. |
|
511 | + * |
|
512 | + * @param EE_Base_Class $obj EE_Base_Class object |
|
513 | + * @param string $datetime_field_name name of the EE_Datetime_Filed datatype db column to be manipulated |
|
514 | + * @param string $period what you are adding. The options are (years, months, days, hours, |
|
515 | + * minutes, seconds) defaults to years |
|
516 | + * @param integer $value what you want to increment the time by |
|
517 | + * @return EE_Base_Class return the EE_Base_Class object so right away you can do something with it |
|
518 | + * (chaining) |
|
519 | + */ |
|
520 | + public static function date_time_add(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1) |
|
521 | + { |
|
522 | + //get the raw UTC date. |
|
523 | + $DateTime = $obj->get_DateTime_object($datetime_field_name); |
|
524 | + $DateTime = EEH_DTT_Helper::calc_date($DateTime, $period, $value); |
|
525 | + return EEH_DTT_Helper::_set_date_time_field($obj, $DateTime, $datetime_field_name); |
|
526 | + } |
|
527 | + |
|
528 | + |
|
529 | + /** |
|
530 | + * date_time_subtract |
|
531 | + * same as date_time_add except subtracting value instead of adding. |
|
532 | + * |
|
533 | + * @param \EE_Base_Class $obj |
|
534 | + * @param string $datetime_field_name name of the EE_Datetime_Filed datatype db column to be manipulated |
|
535 | + * @param string $period |
|
536 | + * @param int $value |
|
537 | + * @return \EE_Base_Class |
|
538 | + */ |
|
539 | + public static function date_time_subtract(EE_Base_Class $obj, $datetime_field_name, $period = 'years', $value = 1) |
|
540 | + { |
|
541 | + //get the raw UTC date |
|
542 | + $DateTime = $obj->get_DateTime_object($datetime_field_name); |
|
543 | + $DateTime = EEH_DTT_Helper::calc_date($DateTime, $period, $value, '-'); |
|
544 | + return EEH_DTT_Helper::_set_date_time_field($obj, $DateTime, $datetime_field_name); |
|
545 | + } |
|
546 | + |
|
547 | + |
|
548 | + /** |
|
549 | + * Simply takes an incoming DateTime object and does calculations on it based on the incoming parameters |
|
550 | + * |
|
551 | + * @param DateTime $DateTime DateTime object |
|
552 | + * @param string $period a value to indicate what interval is being used in the calculation. The options are |
|
553 | + * 'years', 'months', 'days', 'hours', 'minutes', 'seconds'. Defaults to years. |
|
554 | + * @param integer $value What you want to increment the date by |
|
555 | + * @param string $operand What operand you wish to use for the calculation |
|
556 | + * @return \DateTime return whatever type came in. |
|
557 | + * @throws \EE_Error |
|
558 | + */ |
|
559 | + protected static function _modify_datetime_object(DateTime $DateTime, $period = 'years', $value = 1, $operand = '+') |
|
560 | + { |
|
561 | + if (! $DateTime instanceof DateTime) { |
|
562 | + throw new EE_Error( |
|
563 | + sprintf( |
|
564 | + __('Expected a PHP DateTime object, but instead received %1$s', 'event_espresso'), |
|
565 | + print_r($DateTime, true) |
|
566 | + ) |
|
567 | + ); |
|
568 | + } |
|
569 | + switch ($period) { |
|
570 | + case 'years' : |
|
571 | + $value = 'P' . $value . 'Y'; |
|
572 | + break; |
|
573 | + case 'months' : |
|
574 | + $value = 'P' . $value . 'M'; |
|
575 | + break; |
|
576 | + case 'weeks' : |
|
577 | + $value = 'P' . $value . 'W'; |
|
578 | + break; |
|
579 | + case 'days' : |
|
580 | + $value = 'P' . $value . 'D'; |
|
581 | + break; |
|
582 | + case 'hours' : |
|
583 | + $value = 'PT' . $value . 'H'; |
|
584 | + break; |
|
585 | + case 'minutes' : |
|
586 | + $value = 'PT' . $value . 'M'; |
|
587 | + break; |
|
588 | + case 'seconds' : |
|
589 | + $value = 'PT' . $value . 'S'; |
|
590 | + break; |
|
591 | + } |
|
592 | + switch ($operand) { |
|
593 | + case '+': |
|
594 | + $DateTime->add(new DateInterval($value)); |
|
595 | + break; |
|
596 | + case '-': |
|
597 | + $DateTime->sub(new DateInterval($value)); |
|
598 | + break; |
|
599 | + } |
|
600 | + return $DateTime; |
|
601 | + } |
|
602 | + |
|
603 | + |
|
604 | + /** |
|
605 | + * Simply takes an incoming Unix timestamp and does calculations on it based on the incoming parameters |
|
606 | + * |
|
607 | + * @param int $timestamp Unix timestamp |
|
608 | + * @param string $period a value to indicate what interval is being used in the calculation. The options are |
|
609 | + * 'years', 'months', 'days', 'hours', 'minutes', 'seconds'. Defaults to years. |
|
610 | + * @param integer $value What you want to increment the date by |
|
611 | + * @param string $operand What operand you wish to use for the calculation |
|
612 | + * @return \DateTime return whatever type came in. |
|
613 | + * @throws \EE_Error |
|
614 | + */ |
|
615 | + protected static function _modify_timestamp($timestamp, $period = 'years', $value = 1, $operand = '+') |
|
616 | + { |
|
617 | + if (! preg_match(EE_Datetime_Field::unix_timestamp_regex, $timestamp)) { |
|
618 | + throw new EE_Error( |
|
619 | + sprintf( |
|
620 | + __('Expected a Unix timestamp, but instead received %1$s', 'event_espresso'), |
|
621 | + print_r($timestamp, true) |
|
622 | + ) |
|
623 | + ); |
|
624 | + } |
|
625 | + switch ($period) { |
|
626 | + case 'years' : |
|
627 | + $value = YEAR_IN_SECONDS * $value; |
|
628 | + break; |
|
629 | + case 'months' : |
|
630 | + $value = YEAR_IN_SECONDS / 12 * $value; |
|
631 | + break; |
|
632 | + case 'weeks' : |
|
633 | + $value = WEEK_IN_SECONDS * $value; |
|
634 | + break; |
|
635 | + case 'days' : |
|
636 | + $value = DAY_IN_SECONDS * $value; |
|
637 | + break; |
|
638 | + case 'hours' : |
|
639 | + $value = HOUR_IN_SECONDS * $value; |
|
640 | + break; |
|
641 | + case 'minutes' : |
|
642 | + $value = MINUTE_IN_SECONDS * $value; |
|
643 | + break; |
|
644 | + } |
|
645 | + switch ($operand) { |
|
646 | + case '+': |
|
647 | + $timestamp += $value; |
|
648 | + break; |
|
649 | + case '-': |
|
650 | + $timestamp -= $value; |
|
651 | + break; |
|
652 | + } |
|
653 | + return $timestamp; |
|
654 | + } |
|
655 | + |
|
656 | + |
|
657 | + /** |
|
658 | + * Simply takes an incoming UTC timestamp or DateTime object and does calculations on it based on the incoming |
|
659 | + * parameters and returns the new timestamp or DateTime. |
|
660 | + * |
|
661 | + * @param int | DateTime $DateTime_or_timestamp DateTime object or Unix timestamp |
|
662 | + * @param string $period a value to indicate what interval is being used in the |
|
663 | + * calculation. The options are 'years', 'months', 'days', 'hours', |
|
664 | + * 'minutes', 'seconds'. Defaults to years. |
|
665 | + * @param integer $value What you want to increment the date by |
|
666 | + * @param string $operand What operand you wish to use for the calculation |
|
667 | + * @return mixed string|DateTime return whatever type came in. |
|
668 | + */ |
|
669 | + public static function calc_date($DateTime_or_timestamp, $period = 'years', $value = 1, $operand = '+') |
|
670 | + { |
|
671 | + if ($DateTime_or_timestamp instanceof DateTime) { |
|
672 | + return EEH_DTT_Helper::_modify_datetime_object($DateTime_or_timestamp, $period, $value, $operand); |
|
673 | + } else if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $DateTime_or_timestamp)) { |
|
674 | + return EEH_DTT_Helper::_modify_timestamp($DateTime_or_timestamp, $period, $value, $operand); |
|
675 | + } else { |
|
676 | + //error |
|
677 | + return $DateTime_or_timestamp; |
|
678 | + } |
|
679 | + } |
|
680 | + |
|
681 | + |
|
682 | + /** |
|
683 | + * The purpose of this helper method is to receive an incoming format string in php date/time format |
|
684 | + * and spit out the js and moment.js equivalent formats. |
|
685 | + * Note, if no format string is given, then it is assumed the user wants what is set for WP. |
|
686 | + * Note, js date and time formats are those used by the jquery-ui datepicker and the jquery-ui date- |
|
687 | + * time picker. |
|
688 | + * |
|
689 | + * @see http://stackoverflow.com/posts/16725290/ for the code inspiration. |
|
690 | + * @param null $date_format_string |
|
691 | + * @param null $time_format_string |
|
692 | + * @return array |
|
693 | + * array( |
|
694 | + * 'js' => array ( |
|
695 | + * 'date' => //date format |
|
696 | + * 'time' => //time format |
|
697 | + * ), |
|
698 | + * 'moment' => //date and time format. |
|
699 | + * ) |
|
700 | + */ |
|
701 | + public static function convert_php_to_js_and_moment_date_formats( |
|
702 | + $date_format_string = null, |
|
703 | + $time_format_string = null |
|
704 | + ) { |
|
705 | + if ($date_format_string === null) { |
|
706 | + $date_format_string = get_option('date_format'); |
|
707 | + } |
|
708 | + |
|
709 | + if ($time_format_string === null) { |
|
710 | + $time_format_string = get_option('time_format'); |
|
711 | + } |
|
712 | + |
|
713 | + $date_format = self::_php_to_js_moment_converter($date_format_string); |
|
714 | + $time_format = self::_php_to_js_moment_converter($time_format_string); |
|
715 | + |
|
716 | + return array( |
|
717 | + 'js' => array( |
|
718 | + 'date' => $date_format['js'], |
|
719 | + 'time' => $time_format['js'], |
|
720 | + ), |
|
721 | + 'moment' => $date_format['moment'] . ' ' . $time_format['moment'], |
|
722 | + ); |
|
723 | + } |
|
724 | + |
|
725 | + |
|
726 | + /** |
|
727 | + * This converts incoming format string into js and moment variations. |
|
728 | + * |
|
729 | + * @param string $format_string incoming php format string |
|
730 | + * @return array js and moment formats. |
|
731 | + */ |
|
732 | + protected static function _php_to_js_moment_converter($format_string) |
|
733 | + { |
|
734 | + /** |
|
735 | + * This is a map of symbols for formats. |
|
736 | + * The index is the php symbol, the equivalent values are in the array. |
|
737 | + * |
|
738 | + * @var array |
|
739 | + */ |
|
740 | + $symbols_map = array( |
|
741 | + // Day |
|
742 | + //01 |
|
743 | + 'd' => array( |
|
744 | + 'js' => 'dd', |
|
745 | + 'moment' => 'DD', |
|
746 | + ), |
|
747 | + //Mon |
|
748 | + 'D' => array( |
|
749 | + 'js' => 'D', |
|
750 | + 'moment' => 'ddd', |
|
751 | + ), |
|
752 | + //1,2,...31 |
|
753 | + 'j' => array( |
|
754 | + 'js' => 'd', |
|
755 | + 'moment' => 'D', |
|
756 | + ), |
|
757 | + //Monday |
|
758 | + 'l' => array( |
|
759 | + 'js' => 'DD', |
|
760 | + 'moment' => 'dddd', |
|
761 | + ), |
|
762 | + //ISO numeric representation of the day of the week (1-6) |
|
763 | + 'N' => array( |
|
764 | + 'js' => '', |
|
765 | + 'moment' => 'E', |
|
766 | + ), |
|
767 | + //st,nd.rd |
|
768 | + 'S' => array( |
|
769 | + 'js' => '', |
|
770 | + 'moment' => 'o', |
|
771 | + ), |
|
772 | + //numeric representation of day of week (0-6) |
|
773 | + 'w' => array( |
|
774 | + 'js' => '', |
|
775 | + 'moment' => 'd', |
|
776 | + ), |
|
777 | + //day of year starting from 0 (0-365) |
|
778 | + 'z' => array( |
|
779 | + 'js' => 'o', |
|
780 | + 'moment' => 'DDD' //note moment does not start with 0 so will need to modify by subtracting 1 |
|
781 | + ), |
|
782 | + // Week |
|
783 | + //ISO-8601 week number of year (weeks starting on monday) |
|
784 | + 'W' => array( |
|
785 | + 'js' => '', |
|
786 | + 'moment' => 'w', |
|
787 | + ), |
|
788 | + // Month |
|
789 | + // January...December |
|
790 | + 'F' => array( |
|
791 | + 'js' => 'MM', |
|
792 | + 'moment' => 'MMMM', |
|
793 | + ), |
|
794 | + //01...12 |
|
795 | + 'm' => array( |
|
796 | + 'js' => 'mm', |
|
797 | + 'moment' => 'MM', |
|
798 | + ), |
|
799 | + //Jan...Dec |
|
800 | + 'M' => array( |
|
801 | + 'js' => 'M', |
|
802 | + 'moment' => 'MMM', |
|
803 | + ), |
|
804 | + //1-12 |
|
805 | + 'n' => array( |
|
806 | + 'js' => 'm', |
|
807 | + 'moment' => 'M', |
|
808 | + ), |
|
809 | + //number of days in given month |
|
810 | + 't' => array( |
|
811 | + 'js' => '', |
|
812 | + 'moment' => '', |
|
813 | + ), |
|
814 | + // Year |
|
815 | + //whether leap year or not 1/0 |
|
816 | + 'L' => array( |
|
817 | + 'js' => '', |
|
818 | + 'moment' => '', |
|
819 | + ), |
|
820 | + //ISO-8601 year number |
|
821 | + 'o' => array( |
|
822 | + 'js' => '', |
|
823 | + 'moment' => 'GGGG', |
|
824 | + ), |
|
825 | + //1999...2003 |
|
826 | + 'Y' => array( |
|
827 | + 'js' => 'yy', |
|
828 | + 'moment' => 'YYYY', |
|
829 | + ), |
|
830 | + //99...03 |
|
831 | + 'y' => array( |
|
832 | + 'js' => 'y', |
|
833 | + 'moment' => 'YY', |
|
834 | + ), |
|
835 | + // Time |
|
836 | + // am/pm |
|
837 | + 'a' => array( |
|
838 | + 'js' => 'tt', |
|
839 | + 'moment' => 'a', |
|
840 | + ), |
|
841 | + // AM/PM |
|
842 | + 'A' => array( |
|
843 | + 'js' => 'TT', |
|
844 | + 'moment' => 'A', |
|
845 | + ), |
|
846 | + // Swatch Internet Time?!? |
|
847 | + 'B' => array( |
|
848 | + 'js' => '', |
|
849 | + 'moment' => '', |
|
850 | + ), |
|
851 | + //1...12 |
|
852 | + 'g' => array( |
|
853 | + 'js' => 'h', |
|
854 | + 'moment' => 'h', |
|
855 | + ), |
|
856 | + //0...23 |
|
857 | + 'G' => array( |
|
858 | + 'js' => 'H', |
|
859 | + 'moment' => 'H', |
|
860 | + ), |
|
861 | + //01...12 |
|
862 | + 'h' => array( |
|
863 | + 'js' => 'hh', |
|
864 | + 'moment' => 'hh', |
|
865 | + ), |
|
866 | + //00...23 |
|
867 | + 'H' => array( |
|
868 | + 'js' => 'HH', |
|
869 | + 'moment' => 'HH', |
|
870 | + ), |
|
871 | + //00..59 |
|
872 | + 'i' => array( |
|
873 | + 'js' => 'mm', |
|
874 | + 'moment' => 'mm', |
|
875 | + ), |
|
876 | + //seconds... 00...59 |
|
877 | + 's' => array( |
|
878 | + 'js' => 'ss', |
|
879 | + 'moment' => 'ss', |
|
880 | + ), |
|
881 | + //microseconds |
|
882 | + 'u' => array( |
|
883 | + 'js' => '', |
|
884 | + 'moment' => '', |
|
885 | + ), |
|
886 | + ); |
|
887 | + $jquery_ui_format = ""; |
|
888 | + $moment_format = ""; |
|
889 | + $escaping = false; |
|
890 | + for ($i = 0; $i < strlen($format_string); $i++) { |
|
891 | + $char = $format_string[$i]; |
|
892 | + if ($char === '\\') { // PHP date format escaping character |
|
893 | + $i++; |
|
894 | + if ($escaping) { |
|
895 | + $jquery_ui_format .= $format_string[$i]; |
|
896 | + $moment_format .= $format_string[$i]; |
|
897 | + } else { |
|
898 | + $jquery_ui_format .= '\'' . $format_string[$i]; |
|
899 | + $moment_format .= $format_string[$i]; |
|
900 | + } |
|
901 | + $escaping = true; |
|
902 | + } else { |
|
903 | + if ($escaping) { |
|
904 | + $jquery_ui_format .= "'"; |
|
905 | + $moment_format .= "'"; |
|
906 | + $escaping = false; |
|
907 | + } |
|
908 | + if (isset($symbols_map[$char])) { |
|
909 | + $jquery_ui_format .= $symbols_map[$char]['js']; |
|
910 | + $moment_format .= $symbols_map[$char]['moment']; |
|
911 | + } else { |
|
912 | + $jquery_ui_format .= $char; |
|
913 | + $moment_format .= $char; |
|
914 | + } |
|
915 | + } |
|
916 | + } |
|
917 | + return array('js' => $jquery_ui_format, 'moment' => $moment_format); |
|
918 | + } |
|
919 | + |
|
920 | + |
|
921 | + /** |
|
922 | + * This takes an incoming format string and validates it to ensure it will work fine with PHP. |
|
923 | + * |
|
924 | + * @param string $format_string Incoming format string for php date(). |
|
925 | + * @return mixed bool|array If all is okay then TRUE is returned. Otherwise an array of validation |
|
926 | + * errors is returned. So for client code calling, check for is_array() to |
|
927 | + * indicate failed validations. |
|
928 | + */ |
|
929 | + public static function validate_format_string($format_string) |
|
930 | + { |
|
931 | + $error_msg = array(); |
|
932 | + //time format checks |
|
933 | + switch (true) { |
|
934 | + case strpos($format_string, 'h') !== false : |
|
935 | + case strpos($format_string, 'g') !== false : |
|
936 | + /** |
|
937 | + * if the time string has a lowercase 'h' which == 12 hour time format and there |
|
938 | + * is not any ante meridiem format ('a' or 'A'). Then throw an error because its |
|
939 | + * too ambiguous and PHP won't be able to figure out whether 1 = 1pm or 1am. |
|
940 | + */ |
|
941 | + if (strpos(strtoupper($format_string), 'A') === false) { |
|
942 | + $error_msg[] = __('There is a time format for 12 hour time but no "a" or "A" to indicate am/pm. Without this distinction, PHP is unable to determine if a "1" for the hour value equals "1pm" or "1am".', |
|
943 | + 'event_espresso'); |
|
944 | + } |
|
945 | + break; |
|
946 | + |
|
947 | + } |
|
948 | + |
|
949 | + return empty($error_msg) ? true : $error_msg; |
|
950 | + } |
|
951 | + |
|
952 | + |
|
953 | + /** |
|
954 | + * If the the first date starts at midnight on one day, and the next date ends at midnight on the |
|
955 | + * very next day then this method will return true. |
|
956 | + * If $date_1 = 2015-12-15 00:00:00 and $date_2 = 2015-12-16 00:00:00 then this function will return true. |
|
957 | + * If $date_1 = 2015-12-15 03:00:00 and $date_2 = 2015-12_16 03:00:00 then this function will return false. |
|
958 | + * If $date_1 = 2015-12-15 00:00:00 and $date_2 = 2015-12-15 00:00:00 then this function will return true. |
|
959 | + * |
|
960 | + * @param mixed $date_1 |
|
961 | + * @param mixed $date_2 |
|
962 | + * @return bool |
|
963 | + */ |
|
964 | + public static function dates_represent_one_24_hour_date($date_1, $date_2) |
|
965 | + { |
|
966 | + |
|
967 | + if ( |
|
968 | + (! $date_1 instanceof DateTime || ! $date_2 instanceof DateTime) || |
|
969 | + ($date_1->format(EE_Datetime_Field::mysql_time_format) != '00:00:00' || $date_2->format(EE_Datetime_Field::mysql_time_format) != '00:00:00') |
|
970 | + ) { |
|
971 | + return false; |
|
972 | + } |
|
973 | + return $date_2->format('U') - $date_1->format('U') == 86400 ? true : false; |
|
974 | + } |
|
975 | + |
|
976 | + |
|
977 | + /** |
|
978 | + * This returns the appropriate query interval string that can be used in sql queries involving mysql Date |
|
979 | + * Functions. |
|
980 | + * |
|
981 | + * @param string $timezone_string A timezone string in a valid format to instantiate a DateTimeZone object. |
|
982 | + * @param string $field_for_interval The Database field that is the interval is applied to in the query. |
|
983 | + * @return string |
|
984 | + */ |
|
985 | + public static function get_sql_query_interval_for_offset($timezone_string, $field_for_interval) |
|
986 | + { |
|
987 | + try { |
|
988 | + /** need to account for timezone offset on the selects */ |
|
989 | + $DateTimeZone = new DateTimeZone($timezone_string); |
|
990 | + } catch (Exception $e) { |
|
991 | + $DateTimeZone = null; |
|
992 | + } |
|
993 | + |
|
994 | + /** |
|
995 | + * Note get_option( 'gmt_offset') returns a value in hours, whereas DateTimeZone::getOffset returns values in seconds. |
|
996 | + * Hence we do the calc for DateTimeZone::getOffset. |
|
997 | + */ |
|
998 | + $offset = $DateTimeZone instanceof DateTimeZone ? ($DateTimeZone->getOffset(new DateTime('now'))) / HOUR_IN_SECONDS : get_option('gmt_offset'); |
|
999 | + $query_interval = $offset < 0 |
|
1000 | + ? 'DATE_SUB(' . $field_for_interval . ', INTERVAL ' . $offset * -1 . ' HOUR)' |
|
1001 | + : 'DATE_ADD(' . $field_for_interval . ', INTERVAL ' . $offset . ' HOUR)'; |
|
1002 | + return $query_interval; |
|
1003 | + } |
|
1004 | + |
|
1005 | + /** |
|
1006 | + * Retrieves the site's default timezone and returns it formatted so it's ready for display |
|
1007 | + * to users. If you want to customize how its displayed feel free to fetch the 'timezone_string' |
|
1008 | + * and 'gmt_offset' WordPress options directly; or use the filter |
|
1009 | + * FHEE__EEH_DTT_Helper__get_timezone_string_for_display |
|
1010 | + * (although note that we remove any HTML that may be added) |
|
1011 | + * |
|
1012 | + * @return string |
|
1013 | + */ |
|
1014 | + public static function get_timezone_string_for_display() |
|
1015 | + { |
|
1016 | + $pretty_timezone = apply_filters('FHEE__EEH_DTT_Helper__get_timezone_string_for_display', ''); |
|
1017 | + if (! empty($pretty_timezone)) { |
|
1018 | + return esc_html($pretty_timezone); |
|
1019 | + } |
|
1020 | + $timezone_string = get_option('timezone_string'); |
|
1021 | + if ($timezone_string) { |
|
1022 | + static $mo_loaded = false; |
|
1023 | + // Load translations for continents and cities just like wp_timezone_choice does |
|
1024 | + if (! $mo_loaded) { |
|
1025 | + $locale = get_locale(); |
|
1026 | + $mofile = WP_LANG_DIR . '/continents-cities-' . $locale . '.mo'; |
|
1027 | + load_textdomain('continents-cities', $mofile); |
|
1028 | + $mo_loaded = true; |
|
1029 | + } |
|
1030 | + //well that was easy. |
|
1031 | + $parts = explode('/', $timezone_string); |
|
1032 | + //remove the continent |
|
1033 | + unset($parts[0]); |
|
1034 | + $t_parts = array(); |
|
1035 | + foreach ($parts as $part) { |
|
1036 | + $t_parts[] = translate(str_replace('_', ' ', $part), 'continents-cities'); |
|
1037 | + } |
|
1038 | + return implode(' - ', $t_parts); |
|
1039 | + } |
|
1040 | + //they haven't set the timezone string, so let's return a string like "UTC+1" |
|
1041 | + $gmt_offset = get_option('gmt_offset'); |
|
1042 | + if (intval($gmt_offset) >= 0) { |
|
1043 | + $prefix = '+'; |
|
1044 | + } else { |
|
1045 | + $prefix = ''; |
|
1046 | + } |
|
1047 | + $parts = explode('.', (string)$gmt_offset); |
|
1048 | + if (count($parts) === 1) { |
|
1049 | + $parts[1] = '00'; |
|
1050 | + } else { |
|
1051 | + //convert the part after the decimal, eg "5" (from x.5) or "25" (from x.25) |
|
1052 | + //to minutes, eg 30 or 15, respectively |
|
1053 | + $hour_fraction = (float)('0.' . $parts[1]); |
|
1054 | + $parts[1] = (string)$hour_fraction * 60; |
|
1055 | + } |
|
1056 | + return sprintf(__('UTC%1$s', 'event_espresso'), $prefix . implode(':', $parts)); |
|
1057 | + } |
|
1058 | + |
|
1059 | + |
|
1060 | + |
|
1061 | + /** |
|
1062 | + * So PHP does this awesome thing where if you are trying to get a timestamp |
|
1063 | + * for a month using a string like "February" or "February 2017", |
|
1064 | + * and you don't specify a day as part of your string, |
|
1065 | + * then PHP will use whatever the current day of the month is. |
|
1066 | + * IF the current day of the month happens to be the 30th or 31st, |
|
1067 | + * then PHP gets really confused by a date like February 30, |
|
1068 | + * so instead of saying |
|
1069 | + * "Hey February only has 28 days (this year)... |
|
1070 | + * ...you must have meant the last day of the month!" |
|
1071 | + * PHP does the next most logical thing, and bumps the date up to March 2nd, |
|
1072 | + * because someone requesting February 30th obviously meant March 1st! |
|
1073 | + * The way around this is to always set the day to the first, |
|
1074 | + * so that the month will stay on the month you wanted. |
|
1075 | + * this method will add that "1" into your date regardless of the format. |
|
1076 | + * |
|
1077 | + * @param string $month |
|
1078 | + * @return string |
|
1079 | + */ |
|
1080 | + public static function first_of_month_timestamp($month = '') |
|
1081 | + { |
|
1082 | + $month = (string)$month; |
|
1083 | + $year = ''; |
|
1084 | + // check if the incoming string has a year in it or not |
|
1085 | + if (preg_match('/\b\d{4}\b/', $month, $matches)) { |
|
1086 | + $year = $matches[0]; |
|
1087 | + // ten remove that from the month string as well as any spaces |
|
1088 | + $month = trim(str_replace($year, '', $month)); |
|
1089 | + // add a space before the year |
|
1090 | + $year = " {$year}"; |
|
1091 | + } |
|
1092 | + // return timestamp for something like "February 1 2017" |
|
1093 | + return strtotime("{$month} 1{$year}"); |
|
1094 | + } |
|
1095 | + |
|
1096 | + /** |
|
1097 | + * This simply returns the timestamp for tomorrow (midnight next day) in this sites timezone. So it may be midnight |
|
1098 | + * for this sites timezone, but the timestamp could be some other time GMT. |
|
1099 | + */ |
|
1100 | + public static function tomorrow() |
|
1101 | 1101 | { |
1102 | 1102 | //The multiplication of -1 ensures that we switch positive offsets to negative and negative offsets to positive |
1103 | 1103 | //before adding to the timestamp. Why? Because we want tomorrow to be for midnight the next day in THIS timezone |
@@ -1107,135 +1107,135 @@ discard block |
||
1107 | 1107 | } |
1108 | 1108 | |
1109 | 1109 | |
1110 | - /** |
|
1111 | - * ** |
|
1112 | - * Gives a nicely-formatted list of timezone strings. |
|
1113 | - * Copied from the core wp function by the same name so we could customize to remove UTC offsets. |
|
1114 | - * |
|
1115 | - * @since 4.9.40.rc.008 |
|
1116 | - * @staticvar bool $mo_loaded |
|
1117 | - * @staticvar string $locale_loaded |
|
1118 | - * @param string $selected_zone Selected timezone. |
|
1119 | - * @param string $locale Optional. Locale to load the timezones in. Default current site locale. |
|
1120 | - * @return string |
|
1121 | - */ |
|
1122 | - public static function wp_timezone_choice($selected_zone, $locale = null) |
|
1123 | - { |
|
1124 | - static $mo_loaded = false, $locale_loaded = null; |
|
1125 | - |
|
1126 | - $continents = array( |
|
1127 | - 'Africa', |
|
1128 | - 'America', |
|
1129 | - 'Antarctica', |
|
1130 | - 'Arctic', |
|
1131 | - 'Asia', |
|
1132 | - 'Atlantic', |
|
1133 | - 'Australia', |
|
1134 | - 'Europe', |
|
1135 | - 'Indian', |
|
1136 | - 'Pacific', |
|
1137 | - ); |
|
1138 | - |
|
1139 | - // Load translations for continents and cities. |
|
1140 | - if (! $mo_loaded || $locale !== $locale_loaded) { |
|
1141 | - $locale_loaded = $locale ? $locale : get_locale(); |
|
1142 | - $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo'; |
|
1143 | - unload_textdomain('continents-cities'); |
|
1144 | - load_textdomain('continents-cities', $mofile); |
|
1145 | - $mo_loaded = true; |
|
1146 | - } |
|
1147 | - |
|
1148 | - $zonen = array(); |
|
1149 | - foreach (timezone_identifiers_list() as $zone) { |
|
1150 | - $zone = explode('/', $zone); |
|
1151 | - if (! in_array($zone[0], $continents)) { |
|
1152 | - continue; |
|
1153 | - } |
|
1154 | - |
|
1155 | - // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later |
|
1156 | - $exists = array( |
|
1157 | - 0 => (isset($zone[0]) && $zone[0]), |
|
1158 | - 1 => (isset($zone[1]) && $zone[1]), |
|
1159 | - 2 => (isset($zone[2]) && $zone[2]), |
|
1160 | - ); |
|
1161 | - $exists[3] = ($exists[0] && 'Etc' !== $zone[0]); |
|
1162 | - $exists[4] = ($exists[1] && $exists[3]); |
|
1163 | - $exists[5] = ($exists[2] && $exists[3]); |
|
1164 | - |
|
1165 | - $zonen[] = array( |
|
1166 | - 'continent' => ($exists[0] ? $zone[0] : ''), |
|
1167 | - 'city' => ($exists[1] ? $zone[1] : ''), |
|
1168 | - 'subcity' => ($exists[2] ? $zone[2] : ''), |
|
1169 | - 't_continent' => ($exists[3] ? translate(str_replace('_', ' ', $zone[0]), 'continents-cities') : ''), |
|
1170 | - 't_city' => ($exists[4] ? translate(str_replace('_', ' ', $zone[1]), 'continents-cities') : ''), |
|
1171 | - 't_subcity' => ($exists[5] ? translate(str_replace('_', ' ', $zone[2]), 'continents-cities') : ''), |
|
1172 | - ); |
|
1173 | - } |
|
1174 | - usort($zonen, '_wp_timezone_choice_usort_callback'); |
|
1175 | - |
|
1176 | - $structure = array(); |
|
1177 | - |
|
1178 | - if (empty($selected_zone)) { |
|
1179 | - $structure[] = '<option selected="selected" value="">' . __('Select a city') . '</option>'; |
|
1180 | - } |
|
1181 | - |
|
1182 | - foreach ($zonen as $key => $zone) { |
|
1183 | - // Build value in an array to join later |
|
1184 | - $value = array($zone['continent']); |
|
1185 | - |
|
1186 | - if (empty($zone['city'])) { |
|
1187 | - // It's at the continent level (generally won't happen) |
|
1188 | - $display = $zone['t_continent']; |
|
1189 | - } else { |
|
1190 | - // It's inside a continent group |
|
1191 | - |
|
1192 | - // Continent optgroup |
|
1193 | - if (! isset($zonen[$key - 1]) || $zonen[$key - 1]['continent'] !== $zone['continent']) { |
|
1194 | - $label = $zone['t_continent']; |
|
1195 | - $structure[] = '<optgroup label="' . esc_attr($label) . '">'; |
|
1196 | - } |
|
1197 | - |
|
1198 | - // Add the city to the value |
|
1199 | - $value[] = $zone['city']; |
|
1200 | - |
|
1201 | - $display = $zone['t_city']; |
|
1202 | - if (! empty($zone['subcity'])) { |
|
1203 | - // Add the subcity to the value |
|
1204 | - $value[] = $zone['subcity']; |
|
1205 | - $display .= ' - ' . $zone['t_subcity']; |
|
1206 | - } |
|
1207 | - } |
|
1208 | - |
|
1209 | - // Build the value |
|
1210 | - $value = join('/', $value); |
|
1211 | - $selected = ''; |
|
1212 | - if ($value === $selected_zone) { |
|
1213 | - $selected = 'selected="selected" '; |
|
1214 | - } |
|
1215 | - $structure[] = '<option ' . $selected . 'value="' . esc_attr($value) . '">' . esc_html($display) . "</option>"; |
|
1216 | - |
|
1217 | - // Close continent optgroup |
|
1218 | - if (! empty($zone['city']) && (! isset($zonen[$key + 1]) || (isset($zonen[$key + 1]) && $zonen[$key + 1]['continent'] !== $zone['continent']))) { |
|
1219 | - $structure[] = '</optgroup>'; |
|
1220 | - } |
|
1221 | - } |
|
1222 | - |
|
1223 | - return join("\n", $structure); |
|
1224 | - } |
|
1225 | - |
|
1226 | - |
|
1227 | - /** |
|
1228 | - * Shim for the WP function `get_user_locale` that was added in WordPress 4.7.0 |
|
1229 | - * |
|
1230 | - * @param int|WP_User $user_id |
|
1231 | - * @return string |
|
1232 | - */ |
|
1233 | - public static function get_user_locale($user_id = 0) |
|
1234 | - { |
|
1235 | - if (function_exists('get_user_locale')) { |
|
1236 | - return get_user_locale($user_id); |
|
1237 | - } |
|
1238 | - return get_locale(); |
|
1239 | - } |
|
1110 | + /** |
|
1111 | + * ** |
|
1112 | + * Gives a nicely-formatted list of timezone strings. |
|
1113 | + * Copied from the core wp function by the same name so we could customize to remove UTC offsets. |
|
1114 | + * |
|
1115 | + * @since 4.9.40.rc.008 |
|
1116 | + * @staticvar bool $mo_loaded |
|
1117 | + * @staticvar string $locale_loaded |
|
1118 | + * @param string $selected_zone Selected timezone. |
|
1119 | + * @param string $locale Optional. Locale to load the timezones in. Default current site locale. |
|
1120 | + * @return string |
|
1121 | + */ |
|
1122 | + public static function wp_timezone_choice($selected_zone, $locale = null) |
|
1123 | + { |
|
1124 | + static $mo_loaded = false, $locale_loaded = null; |
|
1125 | + |
|
1126 | + $continents = array( |
|
1127 | + 'Africa', |
|
1128 | + 'America', |
|
1129 | + 'Antarctica', |
|
1130 | + 'Arctic', |
|
1131 | + 'Asia', |
|
1132 | + 'Atlantic', |
|
1133 | + 'Australia', |
|
1134 | + 'Europe', |
|
1135 | + 'Indian', |
|
1136 | + 'Pacific', |
|
1137 | + ); |
|
1138 | + |
|
1139 | + // Load translations for continents and cities. |
|
1140 | + if (! $mo_loaded || $locale !== $locale_loaded) { |
|
1141 | + $locale_loaded = $locale ? $locale : get_locale(); |
|
1142 | + $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo'; |
|
1143 | + unload_textdomain('continents-cities'); |
|
1144 | + load_textdomain('continents-cities', $mofile); |
|
1145 | + $mo_loaded = true; |
|
1146 | + } |
|
1147 | + |
|
1148 | + $zonen = array(); |
|
1149 | + foreach (timezone_identifiers_list() as $zone) { |
|
1150 | + $zone = explode('/', $zone); |
|
1151 | + if (! in_array($zone[0], $continents)) { |
|
1152 | + continue; |
|
1153 | + } |
|
1154 | + |
|
1155 | + // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later |
|
1156 | + $exists = array( |
|
1157 | + 0 => (isset($zone[0]) && $zone[0]), |
|
1158 | + 1 => (isset($zone[1]) && $zone[1]), |
|
1159 | + 2 => (isset($zone[2]) && $zone[2]), |
|
1160 | + ); |
|
1161 | + $exists[3] = ($exists[0] && 'Etc' !== $zone[0]); |
|
1162 | + $exists[4] = ($exists[1] && $exists[3]); |
|
1163 | + $exists[5] = ($exists[2] && $exists[3]); |
|
1164 | + |
|
1165 | + $zonen[] = array( |
|
1166 | + 'continent' => ($exists[0] ? $zone[0] : ''), |
|
1167 | + 'city' => ($exists[1] ? $zone[1] : ''), |
|
1168 | + 'subcity' => ($exists[2] ? $zone[2] : ''), |
|
1169 | + 't_continent' => ($exists[3] ? translate(str_replace('_', ' ', $zone[0]), 'continents-cities') : ''), |
|
1170 | + 't_city' => ($exists[4] ? translate(str_replace('_', ' ', $zone[1]), 'continents-cities') : ''), |
|
1171 | + 't_subcity' => ($exists[5] ? translate(str_replace('_', ' ', $zone[2]), 'continents-cities') : ''), |
|
1172 | + ); |
|
1173 | + } |
|
1174 | + usort($zonen, '_wp_timezone_choice_usort_callback'); |
|
1175 | + |
|
1176 | + $structure = array(); |
|
1177 | + |
|
1178 | + if (empty($selected_zone)) { |
|
1179 | + $structure[] = '<option selected="selected" value="">' . __('Select a city') . '</option>'; |
|
1180 | + } |
|
1181 | + |
|
1182 | + foreach ($zonen as $key => $zone) { |
|
1183 | + // Build value in an array to join later |
|
1184 | + $value = array($zone['continent']); |
|
1185 | + |
|
1186 | + if (empty($zone['city'])) { |
|
1187 | + // It's at the continent level (generally won't happen) |
|
1188 | + $display = $zone['t_continent']; |
|
1189 | + } else { |
|
1190 | + // It's inside a continent group |
|
1191 | + |
|
1192 | + // Continent optgroup |
|
1193 | + if (! isset($zonen[$key - 1]) || $zonen[$key - 1]['continent'] !== $zone['continent']) { |
|
1194 | + $label = $zone['t_continent']; |
|
1195 | + $structure[] = '<optgroup label="' . esc_attr($label) . '">'; |
|
1196 | + } |
|
1197 | + |
|
1198 | + // Add the city to the value |
|
1199 | + $value[] = $zone['city']; |
|
1200 | + |
|
1201 | + $display = $zone['t_city']; |
|
1202 | + if (! empty($zone['subcity'])) { |
|
1203 | + // Add the subcity to the value |
|
1204 | + $value[] = $zone['subcity']; |
|
1205 | + $display .= ' - ' . $zone['t_subcity']; |
|
1206 | + } |
|
1207 | + } |
|
1208 | + |
|
1209 | + // Build the value |
|
1210 | + $value = join('/', $value); |
|
1211 | + $selected = ''; |
|
1212 | + if ($value === $selected_zone) { |
|
1213 | + $selected = 'selected="selected" '; |
|
1214 | + } |
|
1215 | + $structure[] = '<option ' . $selected . 'value="' . esc_attr($value) . '">' . esc_html($display) . "</option>"; |
|
1216 | + |
|
1217 | + // Close continent optgroup |
|
1218 | + if (! empty($zone['city']) && (! isset($zonen[$key + 1]) || (isset($zonen[$key + 1]) && $zonen[$key + 1]['continent'] !== $zone['continent']))) { |
|
1219 | + $structure[] = '</optgroup>'; |
|
1220 | + } |
|
1221 | + } |
|
1222 | + |
|
1223 | + return join("\n", $structure); |
|
1224 | + } |
|
1225 | + |
|
1226 | + |
|
1227 | + /** |
|
1228 | + * Shim for the WP function `get_user_locale` that was added in WordPress 4.7.0 |
|
1229 | + * |
|
1230 | + * @param int|WP_User $user_id |
|
1231 | + * @return string |
|
1232 | + */ |
|
1233 | + public static function get_user_locale($user_id = 0) |
|
1234 | + { |
|
1235 | + if (function_exists('get_user_locale')) { |
|
1236 | + return get_user_locale($user_id); |
|
1237 | + } |
|
1238 | + return get_locale(); |
|
1239 | + } |
|
1240 | 1240 | |
1241 | 1241 | }// end class EEH_DTT_Helper |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
2 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
3 | 3 | exit('NO direct script access allowed'); |
4 | 4 | } |
5 | 5 | |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | new DateTimeZone($timezone_string); |
75 | 75 | } catch (Exception $e) { |
76 | 76 | // sometimes we take exception to exceptions |
77 | - if (! $throw_error) { |
|
77 | + if ( ! $throw_error) { |
|
78 | 78 | return false; |
79 | 79 | } |
80 | 80 | throw new EE_Error( |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | } |
160 | 160 | } |
161 | 161 | $offset = get_option('gmt_offset'); |
162 | - return (int)($offset * HOUR_IN_SECONDS); |
|
162 | + return (int) ($offset * HOUR_IN_SECONDS); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | public static function adjust_invalid_gmt_offsets($gmt_offset = 0) |
178 | 178 | { |
179 | 179 | //make sure $gmt_offset is int |
180 | - $gmt_offset = (int)$gmt_offset; |
|
180 | + $gmt_offset = (int) $gmt_offset; |
|
181 | 181 | switch ($gmt_offset) { |
182 | 182 | //-12 |
183 | 183 | case -43200: |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | public static function get_timezone_offset(DateTimeZone $date_time_zone, $time = null) |
351 | 351 | { |
352 | 352 | $transitions = self::get_timezone_transitions($date_time_zone, $time); |
353 | - if (! isset($transitions['offset'])) { |
|
353 | + if ( ! isset($transitions['offset'])) { |
|
354 | 354 | throw new DomainException(); |
355 | 355 | } |
356 | 356 | return $transitions['offset']; |
@@ -364,7 +364,7 @@ discard block |
||
364 | 364 | public static function timezone_select_input($timezone_string = '') |
365 | 365 | { |
366 | 366 | // get WP date time format |
367 | - $datetime_format = get_option('date_format') . ' ' . get_option('time_format'); |
|
367 | + $datetime_format = get_option('date_format').' '.get_option('time_format'); |
|
368 | 368 | // if passed a value, then use that, else get WP option |
369 | 369 | $timezone_string = ! empty($timezone_string) ? $timezone_string : get_option('timezone_string'); |
370 | 370 | // check if the timezone is valid but don't throw any errors if it isn't |
@@ -376,9 +376,9 @@ discard block |
||
376 | 376 | // Create a UTC+- zone if no timezone string exists |
377 | 377 | $check_zone_info = false; |
378 | 378 | if ($gmt_offset > 0) { |
379 | - $timezone_string = 'UTC+' . $gmt_offset; |
|
379 | + $timezone_string = 'UTC+'.$gmt_offset; |
|
380 | 380 | } elseif ($gmt_offset < 0) { |
381 | - $timezone_string = 'UTC' . $gmt_offset; |
|
381 | + $timezone_string = 'UTC'.$gmt_offset; |
|
382 | 382 | } else { |
383 | 383 | $timezone_string = 'UTC'; |
384 | 384 | } |
@@ -400,11 +400,11 @@ discard block |
||
400 | 400 | __('%1$sUTC%2$s time is %3$s'), |
401 | 401 | '<abbr title="Coordinated Universal Time">', |
402 | 402 | '</abbr>', |
403 | - '<code>' . date_i18n($datetime_format, false, true) . '</code>' |
|
403 | + '<code>'.date_i18n($datetime_format, false, true).'</code>' |
|
404 | 404 | ); |
405 | 405 | ?></span> |
406 | - <?php if (! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
|
407 | - <br/><span><?php printf(__('Local time is %1$s'), '<code>' . date_i18n($datetime_format) . '</code>'); ?></span> |
|
406 | + <?php if ( ! empty($timezone_string) || ! empty($gmt_offset)) : ?> |
|
407 | + <br/><span><?php printf(__('Local time is %1$s'), '<code>'.date_i18n($datetime_format).'</code>'); ?></span> |
|
408 | 408 | <?php endif; ?> |
409 | 409 | |
410 | 410 | <?php if ($check_zone_info && $timezone_string) : ?> |
@@ -437,11 +437,10 @@ discard block |
||
437 | 437 | |
438 | 438 | if ($found) { |
439 | 439 | $message = $tr['isdst'] ? |
440 | - __(' Daylight saving time begins on: %s.') : |
|
441 | - __(' Standard time begins on: %s.'); |
|
440 | + __(' Daylight saving time begins on: %s.') : __(' Standard time begins on: %s.'); |
|
442 | 441 | // Add the difference between the current offset and the new offset to ts to get the correct transition time from date_i18n(). |
443 | 442 | printf($message, |
444 | - '<code >' . date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])) . '</code >'); |
|
443 | + '<code >'.date_i18n($datetime_format, $tr['ts'] + ($tz_offset - $tr['offset'])).'</code >'); |
|
445 | 444 | } else { |
446 | 445 | _e('This timezone does not observe daylight saving time.'); |
447 | 446 | } |
@@ -469,13 +468,13 @@ discard block |
||
469 | 468 | */ |
470 | 469 | public static function get_timestamp_with_offset($unix_timestamp = 0, $timezone_string = '') |
471 | 470 | { |
472 | - $unix_timestamp = $unix_timestamp === 0 ? time() : (int)$unix_timestamp; |
|
471 | + $unix_timestamp = $unix_timestamp === 0 ? time() : (int) $unix_timestamp; |
|
473 | 472 | $timezone_string = self::get_valid_timezone_string($timezone_string); |
474 | 473 | $TimeZone = new DateTimeZone($timezone_string); |
475 | 474 | |
476 | - $DateTime = new DateTime('@' . $unix_timestamp, $TimeZone); |
|
475 | + $DateTime = new DateTime('@'.$unix_timestamp, $TimeZone); |
|
477 | 476 | $offset = timezone_offset_get($TimeZone, $DateTime); |
478 | - return (int)$DateTime->format('U') + (int)$offset; |
|
477 | + return (int) $DateTime->format('U') + (int) $offset; |
|
479 | 478 | } |
480 | 479 | |
481 | 480 | |
@@ -558,7 +557,7 @@ discard block |
||
558 | 557 | */ |
559 | 558 | protected static function _modify_datetime_object(DateTime $DateTime, $period = 'years', $value = 1, $operand = '+') |
560 | 559 | { |
561 | - if (! $DateTime instanceof DateTime) { |
|
560 | + if ( ! $DateTime instanceof DateTime) { |
|
562 | 561 | throw new EE_Error( |
563 | 562 | sprintf( |
564 | 563 | __('Expected a PHP DateTime object, but instead received %1$s', 'event_espresso'), |
@@ -568,25 +567,25 @@ discard block |
||
568 | 567 | } |
569 | 568 | switch ($period) { |
570 | 569 | case 'years' : |
571 | - $value = 'P' . $value . 'Y'; |
|
570 | + $value = 'P'.$value.'Y'; |
|
572 | 571 | break; |
573 | 572 | case 'months' : |
574 | - $value = 'P' . $value . 'M'; |
|
573 | + $value = 'P'.$value.'M'; |
|
575 | 574 | break; |
576 | 575 | case 'weeks' : |
577 | - $value = 'P' . $value . 'W'; |
|
576 | + $value = 'P'.$value.'W'; |
|
578 | 577 | break; |
579 | 578 | case 'days' : |
580 | - $value = 'P' . $value . 'D'; |
|
579 | + $value = 'P'.$value.'D'; |
|
581 | 580 | break; |
582 | 581 | case 'hours' : |
583 | - $value = 'PT' . $value . 'H'; |
|
582 | + $value = 'PT'.$value.'H'; |
|
584 | 583 | break; |
585 | 584 | case 'minutes' : |
586 | - $value = 'PT' . $value . 'M'; |
|
585 | + $value = 'PT'.$value.'M'; |
|
587 | 586 | break; |
588 | 587 | case 'seconds' : |
589 | - $value = 'PT' . $value . 'S'; |
|
588 | + $value = 'PT'.$value.'S'; |
|
590 | 589 | break; |
591 | 590 | } |
592 | 591 | switch ($operand) { |
@@ -614,7 +613,7 @@ discard block |
||
614 | 613 | */ |
615 | 614 | protected static function _modify_timestamp($timestamp, $period = 'years', $value = 1, $operand = '+') |
616 | 615 | { |
617 | - if (! preg_match(EE_Datetime_Field::unix_timestamp_regex, $timestamp)) { |
|
616 | + if ( ! preg_match(EE_Datetime_Field::unix_timestamp_regex, $timestamp)) { |
|
618 | 617 | throw new EE_Error( |
619 | 618 | sprintf( |
620 | 619 | __('Expected a Unix timestamp, but instead received %1$s', 'event_espresso'), |
@@ -718,7 +717,7 @@ discard block |
||
718 | 717 | 'date' => $date_format['js'], |
719 | 718 | 'time' => $time_format['js'], |
720 | 719 | ), |
721 | - 'moment' => $date_format['moment'] . ' ' . $time_format['moment'], |
|
720 | + 'moment' => $date_format['moment'].' '.$time_format['moment'], |
|
722 | 721 | ); |
723 | 722 | } |
724 | 723 | |
@@ -737,7 +736,7 @@ discard block |
||
737 | 736 | * |
738 | 737 | * @var array |
739 | 738 | */ |
740 | - $symbols_map = array( |
|
739 | + $symbols_map = array( |
|
741 | 740 | // Day |
742 | 741 | //01 |
743 | 742 | 'd' => array( |
@@ -895,7 +894,7 @@ discard block |
||
895 | 894 | $jquery_ui_format .= $format_string[$i]; |
896 | 895 | $moment_format .= $format_string[$i]; |
897 | 896 | } else { |
898 | - $jquery_ui_format .= '\'' . $format_string[$i]; |
|
897 | + $jquery_ui_format .= '\''.$format_string[$i]; |
|
899 | 898 | $moment_format .= $format_string[$i]; |
900 | 899 | } |
901 | 900 | $escaping = true; |
@@ -965,7 +964,7 @@ discard block |
||
965 | 964 | { |
966 | 965 | |
967 | 966 | if ( |
968 | - (! $date_1 instanceof DateTime || ! $date_2 instanceof DateTime) || |
|
967 | + ( ! $date_1 instanceof DateTime || ! $date_2 instanceof DateTime) || |
|
969 | 968 | ($date_1->format(EE_Datetime_Field::mysql_time_format) != '00:00:00' || $date_2->format(EE_Datetime_Field::mysql_time_format) != '00:00:00') |
970 | 969 | ) { |
971 | 970 | return false; |
@@ -997,8 +996,8 @@ discard block |
||
997 | 996 | */ |
998 | 997 | $offset = $DateTimeZone instanceof DateTimeZone ? ($DateTimeZone->getOffset(new DateTime('now'))) / HOUR_IN_SECONDS : get_option('gmt_offset'); |
999 | 998 | $query_interval = $offset < 0 |
1000 | - ? 'DATE_SUB(' . $field_for_interval . ', INTERVAL ' . $offset * -1 . ' HOUR)' |
|
1001 | - : 'DATE_ADD(' . $field_for_interval . ', INTERVAL ' . $offset . ' HOUR)'; |
|
999 | + ? 'DATE_SUB('.$field_for_interval.', INTERVAL '.$offset * -1.' HOUR)' |
|
1000 | + : 'DATE_ADD('.$field_for_interval.', INTERVAL '.$offset.' HOUR)'; |
|
1002 | 1001 | return $query_interval; |
1003 | 1002 | } |
1004 | 1003 | |
@@ -1014,16 +1013,16 @@ discard block |
||
1014 | 1013 | public static function get_timezone_string_for_display() |
1015 | 1014 | { |
1016 | 1015 | $pretty_timezone = apply_filters('FHEE__EEH_DTT_Helper__get_timezone_string_for_display', ''); |
1017 | - if (! empty($pretty_timezone)) { |
|
1016 | + if ( ! empty($pretty_timezone)) { |
|
1018 | 1017 | return esc_html($pretty_timezone); |
1019 | 1018 | } |
1020 | 1019 | $timezone_string = get_option('timezone_string'); |
1021 | 1020 | if ($timezone_string) { |
1022 | 1021 | static $mo_loaded = false; |
1023 | 1022 | // Load translations for continents and cities just like wp_timezone_choice does |
1024 | - if (! $mo_loaded) { |
|
1023 | + if ( ! $mo_loaded) { |
|
1025 | 1024 | $locale = get_locale(); |
1026 | - $mofile = WP_LANG_DIR . '/continents-cities-' . $locale . '.mo'; |
|
1025 | + $mofile = WP_LANG_DIR.'/continents-cities-'.$locale.'.mo'; |
|
1027 | 1026 | load_textdomain('continents-cities', $mofile); |
1028 | 1027 | $mo_loaded = true; |
1029 | 1028 | } |
@@ -1044,16 +1043,16 @@ discard block |
||
1044 | 1043 | } else { |
1045 | 1044 | $prefix = ''; |
1046 | 1045 | } |
1047 | - $parts = explode('.', (string)$gmt_offset); |
|
1046 | + $parts = explode('.', (string) $gmt_offset); |
|
1048 | 1047 | if (count($parts) === 1) { |
1049 | 1048 | $parts[1] = '00'; |
1050 | 1049 | } else { |
1051 | 1050 | //convert the part after the decimal, eg "5" (from x.5) or "25" (from x.25) |
1052 | 1051 | //to minutes, eg 30 or 15, respectively |
1053 | - $hour_fraction = (float)('0.' . $parts[1]); |
|
1054 | - $parts[1] = (string)$hour_fraction * 60; |
|
1052 | + $hour_fraction = (float) ('0.'.$parts[1]); |
|
1053 | + $parts[1] = (string) $hour_fraction * 60; |
|
1055 | 1054 | } |
1056 | - return sprintf(__('UTC%1$s', 'event_espresso'), $prefix . implode(':', $parts)); |
|
1055 | + return sprintf(__('UTC%1$s', 'event_espresso'), $prefix.implode(':', $parts)); |
|
1057 | 1056 | } |
1058 | 1057 | |
1059 | 1058 | |
@@ -1079,7 +1078,7 @@ discard block |
||
1079 | 1078 | */ |
1080 | 1079 | public static function first_of_month_timestamp($month = '') |
1081 | 1080 | { |
1082 | - $month = (string)$month; |
|
1081 | + $month = (string) $month; |
|
1083 | 1082 | $year = ''; |
1084 | 1083 | // check if the incoming string has a year in it or not |
1085 | 1084 | if (preg_match('/\b\d{4}\b/', $month, $matches)) { |
@@ -1103,7 +1102,7 @@ discard block |
||
1103 | 1102 | //before adding to the timestamp. Why? Because we want tomorrow to be for midnight the next day in THIS timezone |
1104 | 1103 | //not an offset from midnight in UTC. So if we're starting with UTC 00:00:00, then we want to make sure the |
1105 | 1104 | //final timestamp is equivalent to midnight in this timezone as represented in GMT. |
1106 | - return strtotime('tomorrow') + (self::get_site_timezone_gmt_offset()*-1); |
|
1105 | + return strtotime('tomorrow') + (self::get_site_timezone_gmt_offset() * -1); |
|
1107 | 1106 | } |
1108 | 1107 | |
1109 | 1108 | |
@@ -1137,9 +1136,9 @@ discard block |
||
1137 | 1136 | ); |
1138 | 1137 | |
1139 | 1138 | // Load translations for continents and cities. |
1140 | - if (! $mo_loaded || $locale !== $locale_loaded) { |
|
1139 | + if ( ! $mo_loaded || $locale !== $locale_loaded) { |
|
1141 | 1140 | $locale_loaded = $locale ? $locale : get_locale(); |
1142 | - $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo'; |
|
1141 | + $mofile = WP_LANG_DIR.'/continents-cities-'.$locale_loaded.'.mo'; |
|
1143 | 1142 | unload_textdomain('continents-cities'); |
1144 | 1143 | load_textdomain('continents-cities', $mofile); |
1145 | 1144 | $mo_loaded = true; |
@@ -1148,12 +1147,12 @@ discard block |
||
1148 | 1147 | $zonen = array(); |
1149 | 1148 | foreach (timezone_identifiers_list() as $zone) { |
1150 | 1149 | $zone = explode('/', $zone); |
1151 | - if (! in_array($zone[0], $continents)) { |
|
1150 | + if ( ! in_array($zone[0], $continents)) { |
|
1152 | 1151 | continue; |
1153 | 1152 | } |
1154 | 1153 | |
1155 | 1154 | // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later |
1156 | - $exists = array( |
|
1155 | + $exists = array( |
|
1157 | 1156 | 0 => (isset($zone[0]) && $zone[0]), |
1158 | 1157 | 1 => (isset($zone[1]) && $zone[1]), |
1159 | 1158 | 2 => (isset($zone[2]) && $zone[2]), |
@@ -1176,7 +1175,7 @@ discard block |
||
1176 | 1175 | $structure = array(); |
1177 | 1176 | |
1178 | 1177 | if (empty($selected_zone)) { |
1179 | - $structure[] = '<option selected="selected" value="">' . __('Select a city') . '</option>'; |
|
1178 | + $structure[] = '<option selected="selected" value="">'.__('Select a city').'</option>'; |
|
1180 | 1179 | } |
1181 | 1180 | |
1182 | 1181 | foreach ($zonen as $key => $zone) { |
@@ -1190,19 +1189,19 @@ discard block |
||
1190 | 1189 | // It's inside a continent group |
1191 | 1190 | |
1192 | 1191 | // Continent optgroup |
1193 | - if (! isset($zonen[$key - 1]) || $zonen[$key - 1]['continent'] !== $zone['continent']) { |
|
1192 | + if ( ! isset($zonen[$key - 1]) || $zonen[$key - 1]['continent'] !== $zone['continent']) { |
|
1194 | 1193 | $label = $zone['t_continent']; |
1195 | - $structure[] = '<optgroup label="' . esc_attr($label) . '">'; |
|
1194 | + $structure[] = '<optgroup label="'.esc_attr($label).'">'; |
|
1196 | 1195 | } |
1197 | 1196 | |
1198 | 1197 | // Add the city to the value |
1199 | 1198 | $value[] = $zone['city']; |
1200 | 1199 | |
1201 | 1200 | $display = $zone['t_city']; |
1202 | - if (! empty($zone['subcity'])) { |
|
1201 | + if ( ! empty($zone['subcity'])) { |
|
1203 | 1202 | // Add the subcity to the value |
1204 | 1203 | $value[] = $zone['subcity']; |
1205 | - $display .= ' - ' . $zone['t_subcity']; |
|
1204 | + $display .= ' - '.$zone['t_subcity']; |
|
1206 | 1205 | } |
1207 | 1206 | } |
1208 | 1207 | |
@@ -1212,10 +1211,10 @@ discard block |
||
1212 | 1211 | if ($value === $selected_zone) { |
1213 | 1212 | $selected = 'selected="selected" '; |
1214 | 1213 | } |
1215 | - $structure[] = '<option ' . $selected . 'value="' . esc_attr($value) . '">' . esc_html($display) . "</option>"; |
|
1214 | + $structure[] = '<option '.$selected.'value="'.esc_attr($value).'">'.esc_html($display)."</option>"; |
|
1216 | 1215 | |
1217 | 1216 | // Close continent optgroup |
1218 | - if (! empty($zone['city']) && (! isset($zonen[$key + 1]) || (isset($zonen[$key + 1]) && $zonen[$key + 1]['continent'] !== $zone['continent']))) { |
|
1217 | + if ( ! empty($zone['city']) && ( ! isset($zonen[$key + 1]) || (isset($zonen[$key + 1]) && $zonen[$key + 1]['continent'] !== $zone['continent']))) { |
|
1219 | 1218 | $structure[] = '</optgroup>'; |
1220 | 1219 | } |
1221 | 1220 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if ( ! defined('ABSPATH')) { |
2 | - exit('No direct script access allowed'); |
|
2 | + exit('No direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | /* |
5 | 5 | Plugin Name: Event Espresso |
@@ -40,243 +40,243 @@ discard block |
||
40 | 40 | * @since 4.0 |
41 | 41 | */ |
42 | 42 | if (function_exists('espresso_version')) { |
43 | - /** |
|
44 | - * espresso_duplicate_plugin_error |
|
45 | - * displays if more than one version of EE is activated at the same time |
|
46 | - */ |
|
47 | - function espresso_duplicate_plugin_error() |
|
48 | - { |
|
49 | - ?> |
|
43 | + /** |
|
44 | + * espresso_duplicate_plugin_error |
|
45 | + * displays if more than one version of EE is activated at the same time |
|
46 | + */ |
|
47 | + function espresso_duplicate_plugin_error() |
|
48 | + { |
|
49 | + ?> |
|
50 | 50 | <div class="error"> |
51 | 51 | <p> |
52 | 52 | <?php echo esc_html__( |
53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | - 'event_espresso' |
|
55 | - ); ?> |
|
53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | + 'event_espresso' |
|
55 | + ); ?> |
|
56 | 56 | </p> |
57 | 57 | </div> |
58 | 58 | <?php |
59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | - } |
|
59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | + } |
|
61 | 61 | |
62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
63 | 63 | } else { |
64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.3.9'); |
|
65 | - if ( ! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | - /** |
|
67 | - * espresso_minimum_php_version_error |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - function espresso_minimum_php_version_error() |
|
72 | - { |
|
73 | - ?> |
|
64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.3.9'); |
|
65 | + if ( ! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | + /** |
|
67 | + * espresso_minimum_php_version_error |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + function espresso_minimum_php_version_error() |
|
72 | + { |
|
73 | + ?> |
|
74 | 74 | <div class="error"> |
75 | 75 | <p> |
76 | 76 | <?php |
77 | - printf( |
|
78 | - esc_html__( |
|
79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | - 'event_espresso' |
|
81 | - ), |
|
82 | - EE_MIN_PHP_VER_REQUIRED, |
|
83 | - PHP_VERSION, |
|
84 | - '<br/>', |
|
85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | - ); |
|
87 | - ?> |
|
77 | + printf( |
|
78 | + esc_html__( |
|
79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | + 'event_espresso' |
|
81 | + ), |
|
82 | + EE_MIN_PHP_VER_REQUIRED, |
|
83 | + PHP_VERSION, |
|
84 | + '<br/>', |
|
85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | + ); |
|
87 | + ?> |
|
88 | 88 | </p> |
89 | 89 | </div> |
90 | 90 | <?php |
91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | - } |
|
91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | + } |
|
93 | 93 | |
94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | - } else { |
|
96 | - /** |
|
97 | - * espresso_version |
|
98 | - * Returns the plugin version |
|
99 | - * |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - function espresso_version() |
|
103 | - { |
|
104 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.45.rc.017'); |
|
105 | - } |
|
94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | + } else { |
|
96 | + /** |
|
97 | + * espresso_version |
|
98 | + * Returns the plugin version |
|
99 | + * |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + function espresso_version() |
|
103 | + { |
|
104 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.45.rc.017'); |
|
105 | + } |
|
106 | 106 | |
107 | - // define versions |
|
108 | - define('EVENT_ESPRESSO_VERSION', espresso_version()); |
|
109 | - define('EE_MIN_WP_VER_REQUIRED', '4.1'); |
|
110 | - define('EE_MIN_WP_VER_RECOMMENDED', '4.4.2'); |
|
111 | - define('EE_MIN_PHP_VER_RECOMMENDED', '5.4.44'); |
|
112 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
113 | - //used to be DIRECTORY_SEPARATOR, but that caused issues on windows |
|
114 | - if ( ! defined('DS')) { |
|
115 | - define('DS', '/'); |
|
116 | - } |
|
117 | - if ( ! defined('PS')) { |
|
118 | - define('PS', PATH_SEPARATOR); |
|
119 | - } |
|
120 | - if ( ! defined('SP')) { |
|
121 | - define('SP', ' '); |
|
122 | - } |
|
123 | - if ( ! defined('EENL')) { |
|
124 | - define('EENL', "\n"); |
|
125 | - } |
|
126 | - define('EE_SUPPORT_EMAIL', '[email protected]'); |
|
127 | - // define the plugin directory and URL |
|
128 | - define('EE_PLUGIN_BASENAME', plugin_basename(EVENT_ESPRESSO_MAIN_FILE)); |
|
129 | - define('EE_PLUGIN_DIR_PATH', plugin_dir_path(EVENT_ESPRESSO_MAIN_FILE)); |
|
130 | - define('EE_PLUGIN_DIR_URL', plugin_dir_url(EVENT_ESPRESSO_MAIN_FILE)); |
|
131 | - // main root folder paths |
|
132 | - define('EE_ADMIN_PAGES', EE_PLUGIN_DIR_PATH . 'admin_pages' . DS); |
|
133 | - define('EE_CORE', EE_PLUGIN_DIR_PATH . 'core' . DS); |
|
134 | - define('EE_MODULES', EE_PLUGIN_DIR_PATH . 'modules' . DS); |
|
135 | - define('EE_PUBLIC', EE_PLUGIN_DIR_PATH . 'public' . DS); |
|
136 | - define('EE_SHORTCODES', EE_PLUGIN_DIR_PATH . 'shortcodes' . DS); |
|
137 | - define('EE_WIDGETS', EE_PLUGIN_DIR_PATH . 'widgets' . DS); |
|
138 | - define('EE_PAYMENT_METHODS', EE_PLUGIN_DIR_PATH . 'payment_methods' . DS); |
|
139 | - define('EE_CAFF_PATH', EE_PLUGIN_DIR_PATH . 'caffeinated' . DS); |
|
140 | - // core system paths |
|
141 | - define('EE_ADMIN', EE_CORE . 'admin' . DS); |
|
142 | - define('EE_CPTS', EE_CORE . 'CPTs' . DS); |
|
143 | - define('EE_CLASSES', EE_CORE . 'db_classes' . DS); |
|
144 | - define('EE_INTERFACES', EE_CORE . 'interfaces' . DS); |
|
145 | - define('EE_BUSINESS', EE_CORE . 'business' . DS); |
|
146 | - define('EE_MODELS', EE_CORE . 'db_models' . DS); |
|
147 | - define('EE_HELPERS', EE_CORE . 'helpers' . DS); |
|
148 | - define('EE_LIBRARIES', EE_CORE . 'libraries' . DS); |
|
149 | - define('EE_TEMPLATES', EE_CORE . 'templates' . DS); |
|
150 | - define('EE_THIRD_PARTY', EE_CORE . 'third_party_libs' . DS); |
|
151 | - define('EE_GLOBAL_ASSETS', EE_TEMPLATES . 'global_assets' . DS); |
|
152 | - define('EE_FORM_SECTIONS', EE_LIBRARIES . 'form_sections' . DS); |
|
153 | - // gateways |
|
154 | - define('EE_GATEWAYS', EE_MODULES . 'gateways' . DS); |
|
155 | - define('EE_GATEWAYS_URL', EE_PLUGIN_DIR_URL . 'modules' . DS . 'gateways' . DS); |
|
156 | - // asset URL paths |
|
157 | - define('EE_TEMPLATES_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'templates' . DS); |
|
158 | - define('EE_GLOBAL_ASSETS_URL', EE_TEMPLATES_URL . 'global_assets' . DS); |
|
159 | - define('EE_IMAGES_URL', EE_GLOBAL_ASSETS_URL . 'images' . DS); |
|
160 | - define('EE_THIRD_PARTY_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'third_party_libs' . DS); |
|
161 | - define('EE_HELPERS_ASSETS', EE_PLUGIN_DIR_URL . 'core/helpers/assets/'); |
|
162 | - define('EE_LIBRARIES_URL', EE_PLUGIN_DIR_URL . 'core/libraries/'); |
|
163 | - // define upload paths |
|
164 | - $uploads = wp_upload_dir(); |
|
165 | - // define the uploads directory and URL |
|
166 | - define('EVENT_ESPRESSO_UPLOAD_DIR', $uploads['basedir'] . DS . 'espresso' . DS); |
|
167 | - define('EVENT_ESPRESSO_UPLOAD_URL', $uploads['baseurl'] . DS . 'espresso' . DS); |
|
168 | - // define the templates directory and URL |
|
169 | - define('EVENT_ESPRESSO_TEMPLATE_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'templates' . DS); |
|
170 | - define('EVENT_ESPRESSO_TEMPLATE_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'templates' . DS); |
|
171 | - // define the gateway directory and URL |
|
172 | - define('EVENT_ESPRESSO_GATEWAY_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
173 | - define('EVENT_ESPRESSO_GATEWAY_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
174 | - // languages folder/path |
|
175 | - define('EE_LANGUAGES_SAFE_LOC', '..' . DS . 'uploads' . DS . 'espresso' . DS . 'languages' . DS); |
|
176 | - define('EE_LANGUAGES_SAFE_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'languages' . DS); |
|
177 | - //check for dompdf fonts in uploads |
|
178 | - if (file_exists(EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS)) { |
|
179 | - define('DOMPDF_FONT_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS); |
|
180 | - } |
|
181 | - //ajax constants |
|
182 | - define( |
|
183 | - 'EE_FRONT_AJAX', |
|
184 | - isset($_REQUEST['ee_front_ajax']) || isset($_REQUEST['data']['ee_front_ajax']) ? true : false |
|
185 | - ); |
|
186 | - define( |
|
187 | - 'EE_ADMIN_AJAX', |
|
188 | - isset($_REQUEST['ee_admin_ajax']) || isset($_REQUEST['data']['ee_admin_ajax']) ? true : false |
|
189 | - ); |
|
190 | - //just a handy constant occasionally needed for finding values representing infinity in the DB |
|
191 | - //you're better to use this than its straight value (currently -1) in case you ever |
|
192 | - //want to change its default value! or find when -1 means infinity |
|
193 | - define('EE_INF_IN_DB', -1); |
|
194 | - define('EE_INF', INF > (float)PHP_INT_MAX ? INF : PHP_INT_MAX); |
|
195 | - define('EE_DEBUG', false); |
|
196 | - // for older WP versions |
|
197 | - if ( ! defined('MONTH_IN_SECONDS')) { |
|
198 | - define('MONTH_IN_SECONDS', DAY_IN_SECONDS * 30); |
|
199 | - } |
|
200 | - /** |
|
201 | - * espresso_plugin_activation |
|
202 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
203 | - */ |
|
204 | - function espresso_plugin_activation() |
|
205 | - { |
|
206 | - update_option('ee_espresso_activation', true); |
|
207 | - } |
|
107 | + // define versions |
|
108 | + define('EVENT_ESPRESSO_VERSION', espresso_version()); |
|
109 | + define('EE_MIN_WP_VER_REQUIRED', '4.1'); |
|
110 | + define('EE_MIN_WP_VER_RECOMMENDED', '4.4.2'); |
|
111 | + define('EE_MIN_PHP_VER_RECOMMENDED', '5.4.44'); |
|
112 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
113 | + //used to be DIRECTORY_SEPARATOR, but that caused issues on windows |
|
114 | + if ( ! defined('DS')) { |
|
115 | + define('DS', '/'); |
|
116 | + } |
|
117 | + if ( ! defined('PS')) { |
|
118 | + define('PS', PATH_SEPARATOR); |
|
119 | + } |
|
120 | + if ( ! defined('SP')) { |
|
121 | + define('SP', ' '); |
|
122 | + } |
|
123 | + if ( ! defined('EENL')) { |
|
124 | + define('EENL', "\n"); |
|
125 | + } |
|
126 | + define('EE_SUPPORT_EMAIL', '[email protected]'); |
|
127 | + // define the plugin directory and URL |
|
128 | + define('EE_PLUGIN_BASENAME', plugin_basename(EVENT_ESPRESSO_MAIN_FILE)); |
|
129 | + define('EE_PLUGIN_DIR_PATH', plugin_dir_path(EVENT_ESPRESSO_MAIN_FILE)); |
|
130 | + define('EE_PLUGIN_DIR_URL', plugin_dir_url(EVENT_ESPRESSO_MAIN_FILE)); |
|
131 | + // main root folder paths |
|
132 | + define('EE_ADMIN_PAGES', EE_PLUGIN_DIR_PATH . 'admin_pages' . DS); |
|
133 | + define('EE_CORE', EE_PLUGIN_DIR_PATH . 'core' . DS); |
|
134 | + define('EE_MODULES', EE_PLUGIN_DIR_PATH . 'modules' . DS); |
|
135 | + define('EE_PUBLIC', EE_PLUGIN_DIR_PATH . 'public' . DS); |
|
136 | + define('EE_SHORTCODES', EE_PLUGIN_DIR_PATH . 'shortcodes' . DS); |
|
137 | + define('EE_WIDGETS', EE_PLUGIN_DIR_PATH . 'widgets' . DS); |
|
138 | + define('EE_PAYMENT_METHODS', EE_PLUGIN_DIR_PATH . 'payment_methods' . DS); |
|
139 | + define('EE_CAFF_PATH', EE_PLUGIN_DIR_PATH . 'caffeinated' . DS); |
|
140 | + // core system paths |
|
141 | + define('EE_ADMIN', EE_CORE . 'admin' . DS); |
|
142 | + define('EE_CPTS', EE_CORE . 'CPTs' . DS); |
|
143 | + define('EE_CLASSES', EE_CORE . 'db_classes' . DS); |
|
144 | + define('EE_INTERFACES', EE_CORE . 'interfaces' . DS); |
|
145 | + define('EE_BUSINESS', EE_CORE . 'business' . DS); |
|
146 | + define('EE_MODELS', EE_CORE . 'db_models' . DS); |
|
147 | + define('EE_HELPERS', EE_CORE . 'helpers' . DS); |
|
148 | + define('EE_LIBRARIES', EE_CORE . 'libraries' . DS); |
|
149 | + define('EE_TEMPLATES', EE_CORE . 'templates' . DS); |
|
150 | + define('EE_THIRD_PARTY', EE_CORE . 'third_party_libs' . DS); |
|
151 | + define('EE_GLOBAL_ASSETS', EE_TEMPLATES . 'global_assets' . DS); |
|
152 | + define('EE_FORM_SECTIONS', EE_LIBRARIES . 'form_sections' . DS); |
|
153 | + // gateways |
|
154 | + define('EE_GATEWAYS', EE_MODULES . 'gateways' . DS); |
|
155 | + define('EE_GATEWAYS_URL', EE_PLUGIN_DIR_URL . 'modules' . DS . 'gateways' . DS); |
|
156 | + // asset URL paths |
|
157 | + define('EE_TEMPLATES_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'templates' . DS); |
|
158 | + define('EE_GLOBAL_ASSETS_URL', EE_TEMPLATES_URL . 'global_assets' . DS); |
|
159 | + define('EE_IMAGES_URL', EE_GLOBAL_ASSETS_URL . 'images' . DS); |
|
160 | + define('EE_THIRD_PARTY_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'third_party_libs' . DS); |
|
161 | + define('EE_HELPERS_ASSETS', EE_PLUGIN_DIR_URL . 'core/helpers/assets/'); |
|
162 | + define('EE_LIBRARIES_URL', EE_PLUGIN_DIR_URL . 'core/libraries/'); |
|
163 | + // define upload paths |
|
164 | + $uploads = wp_upload_dir(); |
|
165 | + // define the uploads directory and URL |
|
166 | + define('EVENT_ESPRESSO_UPLOAD_DIR', $uploads['basedir'] . DS . 'espresso' . DS); |
|
167 | + define('EVENT_ESPRESSO_UPLOAD_URL', $uploads['baseurl'] . DS . 'espresso' . DS); |
|
168 | + // define the templates directory and URL |
|
169 | + define('EVENT_ESPRESSO_TEMPLATE_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'templates' . DS); |
|
170 | + define('EVENT_ESPRESSO_TEMPLATE_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'templates' . DS); |
|
171 | + // define the gateway directory and URL |
|
172 | + define('EVENT_ESPRESSO_GATEWAY_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
173 | + define('EVENT_ESPRESSO_GATEWAY_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
174 | + // languages folder/path |
|
175 | + define('EE_LANGUAGES_SAFE_LOC', '..' . DS . 'uploads' . DS . 'espresso' . DS . 'languages' . DS); |
|
176 | + define('EE_LANGUAGES_SAFE_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'languages' . DS); |
|
177 | + //check for dompdf fonts in uploads |
|
178 | + if (file_exists(EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS)) { |
|
179 | + define('DOMPDF_FONT_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS); |
|
180 | + } |
|
181 | + //ajax constants |
|
182 | + define( |
|
183 | + 'EE_FRONT_AJAX', |
|
184 | + isset($_REQUEST['ee_front_ajax']) || isset($_REQUEST['data']['ee_front_ajax']) ? true : false |
|
185 | + ); |
|
186 | + define( |
|
187 | + 'EE_ADMIN_AJAX', |
|
188 | + isset($_REQUEST['ee_admin_ajax']) || isset($_REQUEST['data']['ee_admin_ajax']) ? true : false |
|
189 | + ); |
|
190 | + //just a handy constant occasionally needed for finding values representing infinity in the DB |
|
191 | + //you're better to use this than its straight value (currently -1) in case you ever |
|
192 | + //want to change its default value! or find when -1 means infinity |
|
193 | + define('EE_INF_IN_DB', -1); |
|
194 | + define('EE_INF', INF > (float)PHP_INT_MAX ? INF : PHP_INT_MAX); |
|
195 | + define('EE_DEBUG', false); |
|
196 | + // for older WP versions |
|
197 | + if ( ! defined('MONTH_IN_SECONDS')) { |
|
198 | + define('MONTH_IN_SECONDS', DAY_IN_SECONDS * 30); |
|
199 | + } |
|
200 | + /** |
|
201 | + * espresso_plugin_activation |
|
202 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
203 | + */ |
|
204 | + function espresso_plugin_activation() |
|
205 | + { |
|
206 | + update_option('ee_espresso_activation', true); |
|
207 | + } |
|
208 | 208 | |
209 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
210 | - /** |
|
211 | - * espresso_load_error_handling |
|
212 | - * this function loads EE's class for handling exceptions and errors |
|
213 | - */ |
|
214 | - function espresso_load_error_handling() |
|
215 | - { |
|
216 | - // load debugging tools |
|
217 | - if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) { |
|
218 | - require_once(EE_HELPERS . 'EEH_Debug_Tools.helper.php'); |
|
219 | - EEH_Debug_Tools::instance(); |
|
220 | - } |
|
221 | - // load error handling |
|
222 | - if (is_readable(EE_CORE . 'EE_Error.core.php')) { |
|
223 | - require_once(EE_CORE . 'EE_Error.core.php'); |
|
224 | - } else { |
|
225 | - wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso')); |
|
226 | - } |
|
227 | - } |
|
209 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
210 | + /** |
|
211 | + * espresso_load_error_handling |
|
212 | + * this function loads EE's class for handling exceptions and errors |
|
213 | + */ |
|
214 | + function espresso_load_error_handling() |
|
215 | + { |
|
216 | + // load debugging tools |
|
217 | + if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) { |
|
218 | + require_once(EE_HELPERS . 'EEH_Debug_Tools.helper.php'); |
|
219 | + EEH_Debug_Tools::instance(); |
|
220 | + } |
|
221 | + // load error handling |
|
222 | + if (is_readable(EE_CORE . 'EE_Error.core.php')) { |
|
223 | + require_once(EE_CORE . 'EE_Error.core.php'); |
|
224 | + } else { |
|
225 | + wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso')); |
|
226 | + } |
|
227 | + } |
|
228 | 228 | |
229 | - /** |
|
230 | - * espresso_load_required |
|
231 | - * given a class name and path, this function will load that file or throw an exception |
|
232 | - * |
|
233 | - * @param string $classname |
|
234 | - * @param string $full_path_to_file |
|
235 | - * @throws EE_Error |
|
236 | - */ |
|
237 | - function espresso_load_required($classname, $full_path_to_file) |
|
238 | - { |
|
239 | - static $error_handling_loaded = false; |
|
240 | - if ( ! $error_handling_loaded) { |
|
241 | - espresso_load_error_handling(); |
|
242 | - $error_handling_loaded = true; |
|
243 | - } |
|
244 | - if (is_readable($full_path_to_file)) { |
|
245 | - require_once($full_path_to_file); |
|
246 | - } else { |
|
247 | - throw new EE_Error ( |
|
248 | - sprintf( |
|
249 | - esc_html__( |
|
250 | - 'The %s class file could not be located or is not readable due to file permissions.', |
|
251 | - 'event_espresso' |
|
252 | - ), |
|
253 | - $classname |
|
254 | - ) |
|
255 | - ); |
|
256 | - } |
|
257 | - } |
|
229 | + /** |
|
230 | + * espresso_load_required |
|
231 | + * given a class name and path, this function will load that file or throw an exception |
|
232 | + * |
|
233 | + * @param string $classname |
|
234 | + * @param string $full_path_to_file |
|
235 | + * @throws EE_Error |
|
236 | + */ |
|
237 | + function espresso_load_required($classname, $full_path_to_file) |
|
238 | + { |
|
239 | + static $error_handling_loaded = false; |
|
240 | + if ( ! $error_handling_loaded) { |
|
241 | + espresso_load_error_handling(); |
|
242 | + $error_handling_loaded = true; |
|
243 | + } |
|
244 | + if (is_readable($full_path_to_file)) { |
|
245 | + require_once($full_path_to_file); |
|
246 | + } else { |
|
247 | + throw new EE_Error ( |
|
248 | + sprintf( |
|
249 | + esc_html__( |
|
250 | + 'The %s class file could not be located or is not readable due to file permissions.', |
|
251 | + 'event_espresso' |
|
252 | + ), |
|
253 | + $classname |
|
254 | + ) |
|
255 | + ); |
|
256 | + } |
|
257 | + } |
|
258 | 258 | |
259 | - espresso_load_required('EEH_Base', EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php'); |
|
260 | - espresso_load_required('EEH_File', EE_CORE . 'helpers' . DS . 'EEH_File.helper.php'); |
|
261 | - espresso_load_required('EE_Bootstrap', EE_CORE . 'EE_Bootstrap.core.php'); |
|
262 | - new EE_Bootstrap(); |
|
263 | - } |
|
259 | + espresso_load_required('EEH_Base', EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php'); |
|
260 | + espresso_load_required('EEH_File', EE_CORE . 'helpers' . DS . 'EEH_File.helper.php'); |
|
261 | + espresso_load_required('EE_Bootstrap', EE_CORE . 'EE_Bootstrap.core.php'); |
|
262 | + new EE_Bootstrap(); |
|
263 | + } |
|
264 | 264 | } |
265 | 265 | if ( ! function_exists('espresso_deactivate_plugin')) { |
266 | - /** |
|
267 | - * deactivate_plugin |
|
268 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
269 | - * |
|
270 | - * @access public |
|
271 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
272 | - * @return void |
|
273 | - */ |
|
274 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
275 | - { |
|
276 | - if ( ! function_exists('deactivate_plugins')) { |
|
277 | - require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
|
278 | - } |
|
279 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
280 | - deactivate_plugins($plugin_basename); |
|
281 | - } |
|
266 | + /** |
|
267 | + * deactivate_plugin |
|
268 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
269 | + * |
|
270 | + * @access public |
|
271 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
272 | + * @return void |
|
273 | + */ |
|
274 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
275 | + { |
|
276 | + if ( ! function_exists('deactivate_plugins')) { |
|
277 | + require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
|
278 | + } |
|
279 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
280 | + deactivate_plugins($plugin_basename); |
|
281 | + } |
|
282 | 282 | } |
283 | 283 | \ No newline at end of file |