@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | ) { |
61 | 61 | $new_value_maybe_array = array(); |
62 | 62 | foreach ($original_value_maybe_array as $array_key => $array_item) { |
63 | - $new_value_maybe_array[ $array_key ] = ModelDataTranslator::prepareFieldValueFromJson( |
|
63 | + $new_value_maybe_array[$array_key] = ModelDataTranslator::prepareFieldValueFromJson( |
|
64 | 64 | $field_obj, |
65 | 65 | $array_item, |
66 | 66 | $requested_version, |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | if (is_array($original_value_maybe_array)) { |
93 | 93 | $new_value = array(); |
94 | 94 | foreach ($original_value_maybe_array as $key => $value) { |
95 | - $new_value[ $key ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
95 | + $new_value[$key] = ModelDataTranslator::prepareFieldValuesForJson( |
|
96 | 96 | $field_obj, |
97 | 97 | $value, |
98 | 98 | $request_version |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | '0', |
226 | 226 | STR_PAD_LEFT |
227 | 227 | ); |
228 | - return $original_timestamp . $offset_sign . $offset_string; |
|
228 | + return $original_timestamp.$offset_sign.$offset_string; |
|
229 | 229 | } |
230 | 230 | |
231 | 231 | |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | // first, check if its a MySQL timestamp in GMT |
303 | 303 | $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
304 | 304 | } |
305 | - if (! $datetime_obj instanceof \DateTime) { |
|
305 | + if ( ! $datetime_obj instanceof \DateTime) { |
|
306 | 306 | // so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
307 | 307 | $datetime_obj = $field_obj->prepare_for_set($original_value); |
308 | 308 | } |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | $original_value, |
329 | 329 | $field_obj->get_name(), |
330 | 330 | $field_obj->get_model_name(), |
331 | - $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
331 | + $field_obj->get_time_format().' '.$field_obj->get_time_format() |
|
332 | 332 | ) |
333 | 333 | ); |
334 | 334 | } |
@@ -338,7 +338,7 @@ discard block |
||
338 | 338 | } |
339 | 339 | // are we about to send an object? just don't. We have no good way to represent it in JSON. |
340 | 340 | // can't just check using is_object() because that missed PHP incomplete objects |
341 | - if (! ModelDataTranslator::isRepresentableInJson($new_value)) { |
|
341 | + if ( ! ModelDataTranslator::isRepresentableInJson($new_value)) { |
|
342 | 342 | $new_value = array( |
343 | 343 | 'error_code' => 'php_object_not_return', |
344 | 344 | 'error_message' => esc_html__( |
@@ -391,7 +391,7 @@ discard block |
||
391 | 391 | $model |
392 | 392 | ); |
393 | 393 | // double-check is it a *_gmt field? |
394 | - if (! $field instanceof EE_Model_Field_Base |
|
394 | + if ( ! $field instanceof EE_Model_Field_Base |
|
395 | 395 | && ModelDataTranslator::isGmtDateFieldName($query_param_sans_stars) |
396 | 396 | ) { |
397 | 397 | // yep, take off '_gmt', and find the field |
@@ -410,8 +410,8 @@ discard block |
||
410 | 410 | $timezone = $model->get_timezone(); |
411 | 411 | } |
412 | 412 | if ($field instanceof EE_Model_Field_Base) { |
413 | - if (! $writing && is_array($query_param_value)) { |
|
414 | - if (! \EEH_Array::is_array_numerically_and_sequentially_indexed($query_param_value)) { |
|
413 | + if ( ! $writing && is_array($query_param_value)) { |
|
414 | + if ( ! \EEH_Array::is_array_numerically_and_sequentially_indexed($query_param_value)) { |
|
415 | 415 | if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
416 | 416 | throw new RestException( |
417 | 417 | 'numerically_indexed_array_of_values_only', |
@@ -430,7 +430,7 @@ discard block |
||
430 | 430 | } |
431 | 431 | // did they specify an operator? |
432 | 432 | if (isset($query_param_value[0]) |
433 | - && isset($valid_operators[ $query_param_value[0] ]) |
|
433 | + && isset($valid_operators[$query_param_value[0]]) |
|
434 | 434 | ) { |
435 | 435 | $op = $query_param_value[0]; |
436 | 436 | $translated_value = array($op); |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | $timezone |
540 | 540 | ); |
541 | 541 | } |
542 | - if ((isset($query_param_for_models[ $query_param_key ]) && $is_gmt_datetime_field) |
|
542 | + if ((isset($query_param_for_models[$query_param_key]) && $is_gmt_datetime_field) |
|
543 | 543 | || $translated_value === null |
544 | 544 | ) { |
545 | 545 | // they have already provided a non-gmt field, ignore the gmt one. That's what WP core |
@@ -547,7 +547,7 @@ discard block |
||
547 | 547 | // OR we couldn't create a translated value from their input |
548 | 548 | continue; |
549 | 549 | } |
550 | - $query_param_for_models[ $query_param_key ] = $translated_value; |
|
550 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
551 | 551 | } else { |
552 | 552 | // so this param doesn't correspond to a field eh? |
553 | 553 | if ($writing) { |
@@ -566,7 +566,7 @@ discard block |
||
566 | 566 | $query_param_sans_stars, |
567 | 567 | $model->logic_query_param_keys() |
568 | 568 | )) { |
569 | - $query_param_for_models[ $query_param_key ] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
569 | + $query_param_for_models[$query_param_key] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
570 | 570 | $query_param_value, |
571 | 571 | $model, |
572 | 572 | $requested_version |
@@ -620,7 +620,7 @@ discard block |
||
620 | 620 | */ |
621 | 621 | public static function removeGmtFromFieldName($field_name) |
622 | 622 | { |
623 | - if (! ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
623 | + if ( ! ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
624 | 624 | return $field_name; |
625 | 625 | } |
626 | 626 | $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
@@ -663,7 +663,7 @@ discard block |
||
663 | 663 | { |
664 | 664 | $new_array = array(); |
665 | 665 | foreach ($field_names as $key => $field_name) { |
666 | - $new_array[ $key ] = ModelDataTranslator::prepareFieldNameFromJson($field_name); |
|
666 | + $new_array[$key] = ModelDataTranslator::prepareFieldNameFromJson($field_name); |
|
667 | 667 | } |
668 | 668 | return $new_array; |
669 | 669 | } |
@@ -680,7 +680,7 @@ discard block |
||
680 | 680 | { |
681 | 681 | $new_array = array(); |
682 | 682 | foreach ($field_names_as_keys as $field_name => $value) { |
683 | - $new_array[ ModelDataTranslator::prepareFieldNameFromJson($field_name) ] = $value; |
|
683 | + $new_array[ModelDataTranslator::prepareFieldNameFromJson($field_name)] = $value; |
|
684 | 684 | } |
685 | 685 | return $new_array; |
686 | 686 | } |
@@ -773,10 +773,10 @@ discard block |
||
773 | 773 | $requested_version |
774 | 774 | ); |
775 | 775 | } |
776 | - $query_param_for_models[ $query_param_key ] = $translated_value; |
|
776 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
777 | 777 | } else { |
778 | 778 | // so it's not for a field, assume it's a logic query param key |
779 | - $query_param_for_models[ $query_param_key ] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
779 | + $query_param_for_models[$query_param_key] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
780 | 780 | $query_param_value, |
781 | 781 | $model, |
782 | 782 | $requested_version |
@@ -828,13 +828,13 @@ discard block |
||
828 | 828 | ); |
829 | 829 | } |
830 | 830 | $number_of_parts = count($query_param_parts); |
831 | - $last_query_param_part = $query_param_parts[ count($query_param_parts) - 1 ]; |
|
831 | + $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
832 | 832 | if ($number_of_parts === 1) { |
833 | 833 | $field_name = $last_query_param_part; |
834 | 834 | } else {// $number_of_parts >= 2 |
835 | 835 | // the last part is the column name, and there are only 2parts. therefore... |
836 | 836 | $field_name = $last_query_param_part; |
837 | - $model = \EE_Registry::instance()->load_model($query_param_parts[ $number_of_parts - 2 ]); |
|
837 | + $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
838 | 838 | } |
839 | 839 | try { |
840 | 840 | return $model->field_settings_for($field_name, false); |
@@ -32,834 +32,834 @@ |
||
32 | 32 | class ModelDataTranslator |
33 | 33 | { |
34 | 34 | |
35 | - /** |
|
36 | - * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
37 | - * fields that COULD contain -1; so we use null |
|
38 | - */ |
|
39 | - const EE_INF_IN_REST = null; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * Prepares a possible array of input values from JSON for use by the models |
|
44 | - * |
|
45 | - * @param EE_Model_Field_Base $field_obj |
|
46 | - * @param mixed $original_value_maybe_array |
|
47 | - * @param string $requested_version |
|
48 | - * @param string $timezone_string treat values as being in this timezone |
|
49 | - * @return mixed |
|
50 | - * @throws RestException |
|
51 | - */ |
|
52 | - public static function prepareFieldValuesFromJson( |
|
53 | - $field_obj, |
|
54 | - $original_value_maybe_array, |
|
55 | - $requested_version, |
|
56 | - $timezone_string = 'UTC' |
|
57 | - ) { |
|
58 | - if (is_array($original_value_maybe_array) |
|
59 | - && ! $field_obj instanceof EE_Serialized_Text_Field |
|
60 | - ) { |
|
61 | - $new_value_maybe_array = array(); |
|
62 | - foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
63 | - $new_value_maybe_array[ $array_key ] = ModelDataTranslator::prepareFieldValueFromJson( |
|
64 | - $field_obj, |
|
65 | - $array_item, |
|
66 | - $requested_version, |
|
67 | - $timezone_string |
|
68 | - ); |
|
69 | - } |
|
70 | - } else { |
|
71 | - $new_value_maybe_array = ModelDataTranslator::prepareFieldValueFromJson( |
|
72 | - $field_obj, |
|
73 | - $original_value_maybe_array, |
|
74 | - $requested_version, |
|
75 | - $timezone_string |
|
76 | - ); |
|
77 | - } |
|
78 | - return $new_value_maybe_array; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * Prepares an array of field values FOR use in JSON/REST API |
|
84 | - * |
|
85 | - * @param EE_Model_Field_Base $field_obj |
|
86 | - * @param mixed $original_value_maybe_array |
|
87 | - * @param string $request_version (eg 4.8.36) |
|
88 | - * @return array |
|
89 | - */ |
|
90 | - public static function prepareFieldValuesForJson($field_obj, $original_value_maybe_array, $request_version) |
|
91 | - { |
|
92 | - if (is_array($original_value_maybe_array)) { |
|
93 | - $new_value = array(); |
|
94 | - foreach ($original_value_maybe_array as $key => $value) { |
|
95 | - $new_value[ $key ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
96 | - $field_obj, |
|
97 | - $value, |
|
98 | - $request_version |
|
99 | - ); |
|
100 | - } |
|
101 | - } else { |
|
102 | - $new_value = ModelDataTranslator::prepareFieldValueForJson( |
|
103 | - $field_obj, |
|
104 | - $original_value_maybe_array, |
|
105 | - $request_version |
|
106 | - ); |
|
107 | - } |
|
108 | - return $new_value; |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
114 | - * "$query_params". |
|
115 | - * |
|
116 | - * @param EE_Model_Field_Base $field_obj |
|
117 | - * @param mixed $original_value |
|
118 | - * @param string $requested_version |
|
119 | - * @param string $timezone_string treat values as being in this timezone |
|
120 | - * @return mixed |
|
121 | - * @throws RestException |
|
122 | - * @throws DomainException |
|
123 | - * @throws EE_Error |
|
124 | - */ |
|
125 | - public static function prepareFieldValueFromJson( |
|
126 | - $field_obj, |
|
127 | - $original_value, |
|
128 | - $requested_version, |
|
129 | - $timezone_string = 'UTC' // UTC |
|
130 | - ) { |
|
131 | - // check if they accidentally submitted an error value. If so throw an exception |
|
132 | - if (is_array($original_value) |
|
133 | - && isset($original_value['error_code'], $original_value['error_message'])) { |
|
134 | - throw new RestException( |
|
135 | - 'rest_submitted_error_value', |
|
136 | - sprintf( |
|
137 | - esc_html__( |
|
138 | - 'You tried to submit a JSON error object as a value for %1$s. That\'s not allowed.', |
|
139 | - 'event_espresso' |
|
140 | - ), |
|
141 | - $field_obj->get_name() |
|
142 | - ), |
|
143 | - array( |
|
144 | - 'status' => 400, |
|
145 | - ) |
|
146 | - ); |
|
147 | - } |
|
148 | - // double-check for serialized PHP. We never accept serialized PHP. No way Jose. |
|
149 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($original_value); |
|
150 | - $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
151 | - $new_value = null; |
|
152 | - // walk through the submitted data and double-check for serialized PHP. We never accept serialized PHP. No |
|
153 | - // way Jose. |
|
154 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($original_value); |
|
155 | - if ($field_obj instanceof EE_Infinite_Integer_Field |
|
156 | - && in_array($original_value, array(null, ''), true) |
|
157 | - ) { |
|
158 | - $new_value = EE_INF; |
|
159 | - } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
160 | - $new_value = rest_parse_date( |
|
161 | - self::getTimestampWithTimezoneOffset($original_value, $field_obj, $timezone_string) |
|
162 | - ); |
|
163 | - if ($new_value === false) { |
|
164 | - throw new RestException( |
|
165 | - 'invalid_format_for_timestamp', |
|
166 | - sprintf( |
|
167 | - esc_html__( |
|
168 | - 'Timestamps received on a request as the value for Date and Time fields must be in %1$s/%2$s format. The timestamp provided (%3$s) is not that format.', |
|
169 | - 'event_espresso' |
|
170 | - ), |
|
171 | - 'RFC3339', |
|
172 | - 'ISO8601', |
|
173 | - $original_value |
|
174 | - ), |
|
175 | - array( |
|
176 | - 'status' => 400, |
|
177 | - ) |
|
178 | - ); |
|
179 | - } |
|
180 | - } else { |
|
181 | - $new_value = $original_value; |
|
182 | - } |
|
183 | - return $new_value; |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * This checks if the incoming timestamp has timezone information already on it and if it doesn't then adds timezone |
|
189 | - * information via details obtained from the host site. |
|
190 | - * |
|
191 | - * @param string $original_timestamp |
|
192 | - * @param EE_Datetime_Field $datetime_field |
|
193 | - * @param $timezone_string |
|
194 | - * @return string |
|
195 | - * @throws DomainException |
|
196 | - */ |
|
197 | - private static function getTimestampWithTimezoneOffset( |
|
198 | - $original_timestamp, |
|
199 | - EE_Datetime_Field $datetime_field, |
|
200 | - $timezone_string |
|
201 | - ) { |
|
202 | - // already have timezone information? |
|
203 | - if (preg_match('/Z|(\+|\-)(\d{2}:\d{2})/', $original_timestamp)) { |
|
204 | - // yes, we're ignoring the timezone. |
|
205 | - return $original_timestamp; |
|
206 | - } |
|
207 | - // need to append timezone |
|
208 | - list($offset_sign, $offset_secs) = self::parseTimezoneOffset( |
|
209 | - $datetime_field->get_timezone_offset( |
|
210 | - new \DateTimeZone($timezone_string), |
|
211 | - $original_timestamp |
|
212 | - ) |
|
213 | - ); |
|
214 | - $offset_string = |
|
215 | - str_pad( |
|
216 | - floor($offset_secs / HOUR_IN_SECONDS), |
|
217 | - 2, |
|
218 | - '0', |
|
219 | - STR_PAD_LEFT |
|
220 | - ) |
|
221 | - . ':' |
|
222 | - . str_pad( |
|
223 | - ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
224 | - 2, |
|
225 | - '0', |
|
226 | - STR_PAD_LEFT |
|
227 | - ); |
|
228 | - return $original_timestamp . $offset_sign . $offset_string; |
|
229 | - } |
|
230 | - |
|
231 | - |
|
232 | - /** |
|
233 | - * Throws an exception if $data is a serialized PHP string (or somehow an actually PHP object, although I don't |
|
234 | - * think that can happen). If $data is an array, recurses into its keys and values |
|
235 | - * |
|
236 | - * @param mixed $data |
|
237 | - * @throws RestException |
|
238 | - * @return void |
|
239 | - */ |
|
240 | - public static function throwExceptionIfContainsSerializedData($data) |
|
241 | - { |
|
242 | - if (is_array($data)) { |
|
243 | - foreach ($data as $key => $value) { |
|
244 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($key); |
|
245 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($value); |
|
246 | - } |
|
247 | - } else { |
|
248 | - if (is_serialized($data) || is_object($data)) { |
|
249 | - throw new RestException( |
|
250 | - 'serialized_data_submission_prohibited', |
|
251 | - esc_html__( |
|
252 | - // @codingStandardsIgnoreStart |
|
253 | - 'You tried to submit a string of serialized text. Serialized PHP is prohibited over the EE4 REST API.', |
|
254 | - // @codingStandardsIgnoreEnd |
|
255 | - 'event_espresso' |
|
256 | - ) |
|
257 | - ); |
|
258 | - } |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - |
|
263 | - /** |
|
264 | - * determines what's going on with them timezone strings |
|
265 | - * |
|
266 | - * @param int $timezone_offset |
|
267 | - * @return array |
|
268 | - */ |
|
269 | - private static function parseTimezoneOffset($timezone_offset) |
|
270 | - { |
|
271 | - $first_char = substr((string) $timezone_offset, 0, 1); |
|
272 | - if ($first_char === '+' || $first_char === '-') { |
|
273 | - $offset_sign = $first_char; |
|
274 | - $offset_secs = substr((string) $timezone_offset, 1); |
|
275 | - } else { |
|
276 | - $offset_sign = '+'; |
|
277 | - $offset_secs = $timezone_offset; |
|
278 | - } |
|
279 | - return array($offset_sign, $offset_secs); |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - /** |
|
284 | - * Prepares a field's value for display in the API |
|
285 | - * |
|
286 | - * @param EE_Model_Field_Base $field_obj |
|
287 | - * @param mixed $original_value |
|
288 | - * @param string $requested_version |
|
289 | - * @return mixed |
|
290 | - */ |
|
291 | - public static function prepareFieldValueForJson($field_obj, $original_value, $requested_version) |
|
292 | - { |
|
293 | - if ($original_value === EE_INF) { |
|
294 | - $new_value = ModelDataTranslator::EE_INF_IN_REST; |
|
295 | - } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
296 | - if (is_string($original_value)) { |
|
297 | - // did they submit a string of a unix timestamp? |
|
298 | - if (is_numeric($original_value)) { |
|
299 | - $datetime_obj = new \DateTime(); |
|
300 | - $datetime_obj->setTimestamp((int) $original_value); |
|
301 | - } else { |
|
302 | - // first, check if its a MySQL timestamp in GMT |
|
303 | - $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
|
304 | - } |
|
305 | - if (! $datetime_obj instanceof \DateTime) { |
|
306 | - // so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
|
307 | - $datetime_obj = $field_obj->prepare_for_set($original_value); |
|
308 | - } |
|
309 | - $original_value = $datetime_obj; |
|
310 | - } |
|
311 | - if ($original_value instanceof \DateTime) { |
|
312 | - $new_value = $original_value->format('Y-m-d H:i:s'); |
|
313 | - } elseif (is_int($original_value) || is_float($original_value)) { |
|
314 | - $new_value = date('Y-m-d H:i:s', $original_value); |
|
315 | - } elseif ($original_value === null || $original_value === '') { |
|
316 | - $new_value = null; |
|
317 | - } else { |
|
318 | - // so it's not a datetime object, unix timestamp (as string or int), |
|
319 | - // MySQL timestamp, or even a string in the field object's format. So no idea what it is |
|
320 | - throw new \EE_Error( |
|
321 | - sprintf( |
|
322 | - esc_html__( |
|
323 | - // @codingStandardsIgnoreStart |
|
324 | - '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".', |
|
325 | - // @codingStandardsIgnoreEnd |
|
326 | - 'event_espresso' |
|
327 | - ), |
|
328 | - $original_value, |
|
329 | - $field_obj->get_name(), |
|
330 | - $field_obj->get_model_name(), |
|
331 | - $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
332 | - ) |
|
333 | - ); |
|
334 | - } |
|
335 | - $new_value = mysql_to_rfc3339($new_value); |
|
336 | - } else { |
|
337 | - $new_value = $original_value; |
|
338 | - } |
|
339 | - // are we about to send an object? just don't. We have no good way to represent it in JSON. |
|
340 | - // can't just check using is_object() because that missed PHP incomplete objects |
|
341 | - if (! ModelDataTranslator::isRepresentableInJson($new_value)) { |
|
342 | - $new_value = array( |
|
343 | - 'error_code' => 'php_object_not_return', |
|
344 | - 'error_message' => esc_html__( |
|
345 | - 'The value of this field in the database is a PHP object, which can\'t be represented in JSON.', |
|
346 | - 'event_espresso' |
|
347 | - ), |
|
348 | - ); |
|
349 | - } |
|
350 | - return apply_filters( |
|
351 | - 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
352 | - $new_value, |
|
353 | - $field_obj, |
|
354 | - $original_value, |
|
355 | - $requested_version |
|
356 | - ); |
|
357 | - } |
|
358 | - |
|
359 | - |
|
360 | - /** |
|
361 | - * Prepares condition-query-parameters (like what's in where and having) from |
|
362 | - * the format expected in the API to use in the models |
|
363 | - * |
|
364 | - * @param array $inputted_query_params_of_this_type |
|
365 | - * @param EEM_Base $model |
|
366 | - * @param string $requested_version |
|
367 | - * @param boolean $writing whether this data will be written to the DB, or if we're just building a query. |
|
368 | - * If we're writing to the DB, we don't expect any operators, or any logic query |
|
369 | - * parameters, and we also won't accept serialized data unless the current user has |
|
370 | - * unfiltered_html. |
|
371 | - * @return array |
|
372 | - * @throws DomainException |
|
373 | - * @throws RestException |
|
374 | - * @throws EE_Error |
|
375 | - */ |
|
376 | - public static function prepareConditionsQueryParamsForModels( |
|
377 | - $inputted_query_params_of_this_type, |
|
378 | - EEM_Base $model, |
|
379 | - $requested_version, |
|
380 | - $writing = false |
|
381 | - ) { |
|
382 | - $query_param_for_models = array(); |
|
383 | - $valid_operators = $model->valid_operators(); |
|
384 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
385 | - $is_gmt_datetime_field = false; |
|
386 | - $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
|
387 | - $query_param_key |
|
388 | - ); |
|
389 | - $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
390 | - $query_param_sans_stars, |
|
391 | - $model |
|
392 | - ); |
|
393 | - // double-check is it a *_gmt field? |
|
394 | - if (! $field instanceof EE_Model_Field_Base |
|
395 | - && ModelDataTranslator::isGmtDateFieldName($query_param_sans_stars) |
|
396 | - ) { |
|
397 | - // yep, take off '_gmt', and find the field |
|
398 | - $query_param_key = ModelDataTranslator::removeGmtFromFieldName($query_param_sans_stars); |
|
399 | - $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
400 | - $query_param_key, |
|
401 | - $model |
|
402 | - ); |
|
403 | - $timezone = 'UTC'; |
|
404 | - $is_gmt_datetime_field = true; |
|
405 | - } elseif ($field instanceof EE_Datetime_Field) { |
|
406 | - // so it's not a GMT field. Set the timezone on the model to the default |
|
407 | - $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
408 | - } else { |
|
409 | - // just keep using what's already set for the timezone |
|
410 | - $timezone = $model->get_timezone(); |
|
411 | - } |
|
412 | - if ($field instanceof EE_Model_Field_Base) { |
|
413 | - if (! $writing && is_array($query_param_value)) { |
|
414 | - if (! \EEH_Array::is_array_numerically_and_sequentially_indexed($query_param_value)) { |
|
415 | - if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
416 | - throw new RestException( |
|
417 | - 'numerically_indexed_array_of_values_only', |
|
418 | - sprintf( |
|
419 | - esc_html__( |
|
420 | - 'The array provided for the parameter "%1$s" should be numerically indexed.', |
|
421 | - 'event_espresso' |
|
422 | - ), |
|
423 | - $query_param_key |
|
424 | - ), |
|
425 | - array( |
|
426 | - 'status' => 400, |
|
427 | - ) |
|
428 | - ); |
|
429 | - } |
|
430 | - } |
|
431 | - // did they specify an operator? |
|
432 | - if (isset($query_param_value[0]) |
|
433 | - && isset($valid_operators[ $query_param_value[0] ]) |
|
434 | - ) { |
|
435 | - $op = $query_param_value[0]; |
|
436 | - $translated_value = array($op); |
|
437 | - if (array_key_exists($op, $model->valid_in_style_operators()) |
|
438 | - && isset($query_param_value[1]) |
|
439 | - && ! isset($query_param_value[2]) |
|
440 | - ) { |
|
441 | - $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
442 | - $field, |
|
443 | - $query_param_value[1], |
|
444 | - $requested_version, |
|
445 | - $timezone |
|
446 | - ); |
|
447 | - } elseif (array_key_exists($op, $model->valid_between_style_operators()) |
|
448 | - && isset($query_param_value[1]) |
|
449 | - && is_array($query_param_value[1]) |
|
450 | - && isset($query_param_key[1][0], $query_param_value[1][1]) |
|
451 | - && ! isset($query_param_value[1][2]) |
|
452 | - && ! isset($query_param_value[2]) |
|
453 | - ) { |
|
454 | - $translated_value[] = array( |
|
455 | - ModelDataTranslator::prepareFieldValuesFromJson( |
|
456 | - $field, |
|
457 | - $query_param_value[1][0], |
|
458 | - $requested_version, |
|
459 | - $timezone |
|
460 | - ), |
|
461 | - ModelDataTranslator::prepareFieldValuesFromJson( |
|
462 | - $field, |
|
463 | - $query_param_value[1][1], |
|
464 | - $requested_version, |
|
465 | - $timezone |
|
466 | - ) |
|
467 | - ); |
|
468 | - } elseif (array_key_exists($op, $model->valid_like_style_operators()) |
|
469 | - && isset($query_param_value[1]) |
|
470 | - && ! isset($query_param_value[2]) |
|
471 | - ) { |
|
472 | - // we want to leave this value mostly-as-is (eg don't force it to be a float |
|
473 | - // or a boolean or an enum value. Leave it as-is with wildcards etc) |
|
474 | - // but do verify it at least doesn't have any serialized data |
|
475 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($query_param_value[1]); |
|
476 | - $translated_value[] = $query_param_value[1]; |
|
477 | - } elseif (array_key_exists($op, $model->valid_null_style_operators()) |
|
478 | - && ! isset($query_param_value[1])) { |
|
479 | - // no arguments should have been provided, so don't look for any |
|
480 | - } elseif (isset($query_param_value[1]) |
|
481 | - && ! isset($query_param_value[2]) |
|
482 | - && ! array_key_exists( |
|
483 | - $op, |
|
484 | - array_merge( |
|
485 | - $model->valid_in_style_operators(), |
|
486 | - $model->valid_null_style_operators(), |
|
487 | - $model->valid_like_style_operators(), |
|
488 | - $model->valid_between_style_operators() |
|
489 | - ) |
|
490 | - ) |
|
491 | - ) { |
|
492 | - // it's a valid operator, but none of the exceptions. Treat it normally. |
|
493 | - $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
494 | - $field, |
|
495 | - $query_param_value[1], |
|
496 | - $requested_version, |
|
497 | - $timezone |
|
498 | - ); |
|
499 | - } else { |
|
500 | - // so they provided a valid operator, but wrong number of arguments |
|
501 | - if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
502 | - throw new RestException( |
|
503 | - 'wrong_number_of_arguments', |
|
504 | - sprintf( |
|
505 | - esc_html__( |
|
506 | - 'The operator you provided, "%1$s" had the wrong number of arguments', |
|
507 | - 'event_espresso' |
|
508 | - ), |
|
509 | - $op |
|
510 | - ), |
|
511 | - array( |
|
512 | - 'status' => 400, |
|
513 | - ) |
|
514 | - ); |
|
515 | - } |
|
516 | - $translated_value = null; |
|
517 | - } |
|
518 | - } else { |
|
519 | - // so they didn't provide a valid operator |
|
520 | - if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
521 | - throw new RestException( |
|
522 | - 'invalid_operator', |
|
523 | - sprintf( |
|
524 | - esc_html__( |
|
525 | - 'You provided an invalid parameter, with key "%1$s" and value "%2$s"', |
|
526 | - 'event_espresso' |
|
527 | - ), |
|
528 | - $query_param_key, |
|
529 | - $query_param_value |
|
530 | - ), |
|
531 | - array( |
|
532 | - 'status' => 400, |
|
533 | - ) |
|
534 | - ); |
|
535 | - } |
|
536 | - // if we aren't in debug mode, then just try our best to fulfill the user's request |
|
537 | - $translated_value = null; |
|
538 | - } |
|
539 | - } else { |
|
540 | - $translated_value = ModelDataTranslator::prepareFieldValueFromJson( |
|
541 | - $field, |
|
542 | - $query_param_value, |
|
543 | - $requested_version, |
|
544 | - $timezone |
|
545 | - ); |
|
546 | - } |
|
547 | - if ((isset($query_param_for_models[ $query_param_key ]) && $is_gmt_datetime_field) |
|
548 | - || $translated_value === null |
|
549 | - ) { |
|
550 | - // they have already provided a non-gmt field, ignore the gmt one. That's what WP core |
|
551 | - // currently does (they might change it though). See https://core.trac.wordpress.org/ticket/39954 |
|
552 | - // OR we couldn't create a translated value from their input |
|
553 | - continue; |
|
554 | - } |
|
555 | - $query_param_for_models[ $query_param_key ] = $translated_value; |
|
556 | - } else { |
|
557 | - // so this param doesn't correspond to a field eh? |
|
558 | - if ($writing) { |
|
559 | - // always tell API clients about invalid parameters when they're creating data. Otherwise, |
|
560 | - // they are probably going to create invalid data |
|
561 | - throw new RestException( |
|
562 | - 'invalid_field', |
|
563 | - sprintf( |
|
564 | - esc_html__('You have provided an invalid parameter: "%1$s"', 'event_espresso'), |
|
565 | - $query_param_key |
|
566 | - ) |
|
567 | - ); |
|
568 | - } else { |
|
569 | - // so it's not for a field, is it a logic query param key? |
|
570 | - if (in_array( |
|
571 | - $query_param_sans_stars, |
|
572 | - $model->logic_query_param_keys() |
|
573 | - )) { |
|
574 | - $query_param_for_models[ $query_param_key ] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
575 | - $query_param_value, |
|
576 | - $model, |
|
577 | - $requested_version |
|
578 | - ); |
|
579 | - } elseif (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
580 | - // only tell API clients they got it wrong if we're in debug mode |
|
581 | - // otherwise try our best ot fulfill their request by ignoring this invalid data |
|
582 | - throw new RestException( |
|
583 | - 'invalid_parameter', |
|
584 | - sprintf( |
|
585 | - esc_html__( |
|
586 | - 'You provided an invalid parameter, with key "%1$s"', |
|
587 | - 'event_espresso' |
|
588 | - ), |
|
589 | - $query_param_sans_stars |
|
590 | - ), |
|
591 | - array( |
|
592 | - 'status' => 400, |
|
593 | - ) |
|
594 | - ); |
|
595 | - } |
|
596 | - } |
|
597 | - } |
|
598 | - } |
|
599 | - return $query_param_for_models; |
|
600 | - } |
|
601 | - |
|
602 | - |
|
603 | - /** |
|
604 | - * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
605 | - * gmt date field name |
|
606 | - * |
|
607 | - * @param string $field_name |
|
608 | - * @return boolean |
|
609 | - */ |
|
610 | - public static function isGmtDateFieldName($field_name) |
|
611 | - { |
|
612 | - return substr( |
|
613 | - ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey($field_name), |
|
614 | - -4, |
|
615 | - 4 |
|
616 | - ) === '_gmt'; |
|
617 | - } |
|
618 | - |
|
619 | - |
|
620 | - /** |
|
621 | - * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
622 | - * |
|
623 | - * @param string $field_name |
|
624 | - * @return string |
|
625 | - */ |
|
626 | - public static function removeGmtFromFieldName($field_name) |
|
627 | - { |
|
628 | - if (! ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
629 | - return $field_name; |
|
630 | - } |
|
631 | - $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
|
632 | - $field_name |
|
633 | - ); |
|
634 | - $query_param_sans_gmt_and_sans_stars = substr( |
|
635 | - $query_param_sans_stars, |
|
636 | - 0, |
|
637 | - strrpos( |
|
638 | - $field_name, |
|
639 | - '_gmt' |
|
640 | - ) |
|
641 | - ); |
|
642 | - return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
643 | - } |
|
644 | - |
|
645 | - |
|
646 | - /** |
|
647 | - * Takes a field name from the REST API and prepares it for the model querying |
|
648 | - * |
|
649 | - * @param string $field_name |
|
650 | - * @return string |
|
651 | - */ |
|
652 | - public static function prepareFieldNameFromJson($field_name) |
|
653 | - { |
|
654 | - if (ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
655 | - return ModelDataTranslator::removeGmtFromFieldName($field_name); |
|
656 | - } |
|
657 | - return $field_name; |
|
658 | - } |
|
659 | - |
|
660 | - |
|
661 | - /** |
|
662 | - * Takes array of field names from REST API and prepares for models |
|
663 | - * |
|
664 | - * @param array $field_names |
|
665 | - * @return array of field names (possibly include model prefixes) |
|
666 | - */ |
|
667 | - public static function prepareFieldNamesFromJson(array $field_names) |
|
668 | - { |
|
669 | - $new_array = array(); |
|
670 | - foreach ($field_names as $key => $field_name) { |
|
671 | - $new_array[ $key ] = ModelDataTranslator::prepareFieldNameFromJson($field_name); |
|
672 | - } |
|
673 | - return $new_array; |
|
674 | - } |
|
675 | - |
|
676 | - |
|
677 | - /** |
|
678 | - * Takes array where array keys are field names (possibly with model path prefixes) |
|
679 | - * from the REST API and prepares them for model querying |
|
680 | - * |
|
681 | - * @param array $field_names_as_keys |
|
682 | - * @return array |
|
683 | - */ |
|
684 | - public static function prepareFieldNamesInArrayKeysFromJson(array $field_names_as_keys) |
|
685 | - { |
|
686 | - $new_array = array(); |
|
687 | - foreach ($field_names_as_keys as $field_name => $value) { |
|
688 | - $new_array[ ModelDataTranslator::prepareFieldNameFromJson($field_name) ] = $value; |
|
689 | - } |
|
690 | - return $new_array; |
|
691 | - } |
|
692 | - |
|
693 | - |
|
694 | - /** |
|
695 | - * Prepares an array of model query params for use in the REST API |
|
696 | - * |
|
697 | - * @param array $model_query_params |
|
698 | - * @param EEM_Base $model |
|
699 | - * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
700 | - * REST API |
|
701 | - * @return array which can be passed into the EE4 REST API when querying a model resource |
|
702 | - * @throws EE_Error |
|
703 | - */ |
|
704 | - public static function prepareQueryParamsForRestApi( |
|
705 | - array $model_query_params, |
|
706 | - EEM_Base $model, |
|
707 | - $requested_version = null |
|
708 | - ) { |
|
709 | - if ($requested_version === null) { |
|
710 | - $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
711 | - } |
|
712 | - $rest_query_params = $model_query_params; |
|
713 | - if (isset($model_query_params[0])) { |
|
714 | - $rest_query_params['where'] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
715 | - $model_query_params[0], |
|
716 | - $model, |
|
717 | - $requested_version |
|
718 | - ); |
|
719 | - unset($rest_query_params[0]); |
|
720 | - } |
|
721 | - if (isset($model_query_params['having'])) { |
|
722 | - $rest_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
723 | - $model_query_params['having'], |
|
724 | - $model, |
|
725 | - $requested_version |
|
726 | - ); |
|
727 | - } |
|
728 | - return apply_filters( |
|
729 | - 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
730 | - $rest_query_params, |
|
731 | - $model_query_params, |
|
732 | - $model, |
|
733 | - $requested_version |
|
734 | - ); |
|
735 | - } |
|
736 | - |
|
737 | - |
|
738 | - /** |
|
739 | - * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
740 | - * |
|
741 | - * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
742 | - * passed into EEM_Base::get_all() |
|
743 | - * @param EEM_Base $model |
|
744 | - * @param string $requested_version eg "4.8.36" |
|
745 | - * @return array ready for use in the rest api query params |
|
746 | - * @throws EE_Error |
|
747 | - * @throws ObjectDetectedException if somehow a PHP object were in the query params' values, |
|
748 | - * (which would be really unusual) |
|
749 | - */ |
|
750 | - public static function prepareConditionsQueryParamsForRestApi( |
|
751 | - $inputted_query_params_of_this_type, |
|
752 | - EEM_Base $model, |
|
753 | - $requested_version |
|
754 | - ) { |
|
755 | - $query_param_for_models = array(); |
|
756 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
757 | - $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
758 | - ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey($query_param_key), |
|
759 | - $model |
|
760 | - ); |
|
761 | - if ($field instanceof EE_Model_Field_Base) { |
|
762 | - // did they specify an operator? |
|
763 | - if (is_array($query_param_value)) { |
|
764 | - $op = $query_param_value[0]; |
|
765 | - $translated_value = array($op); |
|
766 | - if (isset($query_param_value[1])) { |
|
767 | - $value = $query_param_value[1]; |
|
768 | - $translated_value[1] = ModelDataTranslator::prepareFieldValuesForJson( |
|
769 | - $field, |
|
770 | - $value, |
|
771 | - $requested_version |
|
772 | - ); |
|
773 | - } |
|
774 | - } else { |
|
775 | - $translated_value = ModelDataTranslator::prepareFieldValueForJson( |
|
776 | - $field, |
|
777 | - $query_param_value, |
|
778 | - $requested_version |
|
779 | - ); |
|
780 | - } |
|
781 | - $query_param_for_models[ $query_param_key ] = $translated_value; |
|
782 | - } else { |
|
783 | - // so it's not for a field, assume it's a logic query param key |
|
784 | - $query_param_for_models[ $query_param_key ] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
785 | - $query_param_value, |
|
786 | - $model, |
|
787 | - $requested_version |
|
788 | - ); |
|
789 | - } |
|
790 | - } |
|
791 | - return $query_param_for_models; |
|
792 | - } |
|
793 | - |
|
794 | - |
|
795 | - /** |
|
796 | - * @param $condition_query_param_key |
|
797 | - * @return string |
|
798 | - */ |
|
799 | - public static function removeStarsAndAnythingAfterFromConditionQueryParamKey($condition_query_param_key) |
|
800 | - { |
|
801 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
802 | - if ($pos_of_star === false) { |
|
803 | - return $condition_query_param_key; |
|
804 | - } else { |
|
805 | - $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
806 | - return $condition_query_param_sans_star; |
|
807 | - } |
|
808 | - } |
|
809 | - |
|
810 | - |
|
811 | - /** |
|
812 | - * Takes the input parameter and finds the model field that it indicates. |
|
813 | - * |
|
814 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
815 | - * @param EEM_Base $model |
|
816 | - * @return EE_Model_Field_Base |
|
817 | - * @throws EE_Error |
|
818 | - */ |
|
819 | - public static function deduceFieldFromQueryParam($query_param_name, EEM_Base $model) |
|
820 | - { |
|
821 | - // ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
822 | - // which will help us find the database table and column |
|
823 | - $query_param_parts = explode('.', $query_param_name); |
|
824 | - if (empty($query_param_parts)) { |
|
825 | - throw new EE_Error( |
|
826 | - sprintf( |
|
827 | - __( |
|
828 | - '_extract_column_name is empty when trying to extract column and table name from %s', |
|
829 | - 'event_espresso' |
|
830 | - ), |
|
831 | - $query_param_name |
|
832 | - ) |
|
833 | - ); |
|
834 | - } |
|
835 | - $number_of_parts = count($query_param_parts); |
|
836 | - $last_query_param_part = $query_param_parts[ count($query_param_parts) - 1 ]; |
|
837 | - if ($number_of_parts === 1) { |
|
838 | - $field_name = $last_query_param_part; |
|
839 | - } else {// $number_of_parts >= 2 |
|
840 | - // the last part is the column name, and there are only 2parts. therefore... |
|
841 | - $field_name = $last_query_param_part; |
|
842 | - $model = \EE_Registry::instance()->load_model($query_param_parts[ $number_of_parts - 2 ]); |
|
843 | - } |
|
844 | - try { |
|
845 | - return $model->field_settings_for($field_name, false); |
|
846 | - } catch (EE_Error $e) { |
|
847 | - return null; |
|
848 | - } |
|
849 | - } |
|
850 | - |
|
851 | - |
|
852 | - /** |
|
853 | - * Returns true if $data can be easily represented in JSON. |
|
854 | - * Basically, objects and resources can't be represented in JSON easily. |
|
855 | - * |
|
856 | - * @param mixed $data |
|
857 | - * @return bool |
|
858 | - */ |
|
859 | - protected static function isRepresentableInJson($data) |
|
860 | - { |
|
861 | - return is_scalar($data) |
|
862 | - || is_array($data) |
|
863 | - || is_null($data); |
|
864 | - } |
|
35 | + /** |
|
36 | + * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
37 | + * fields that COULD contain -1; so we use null |
|
38 | + */ |
|
39 | + const EE_INF_IN_REST = null; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * Prepares a possible array of input values from JSON for use by the models |
|
44 | + * |
|
45 | + * @param EE_Model_Field_Base $field_obj |
|
46 | + * @param mixed $original_value_maybe_array |
|
47 | + * @param string $requested_version |
|
48 | + * @param string $timezone_string treat values as being in this timezone |
|
49 | + * @return mixed |
|
50 | + * @throws RestException |
|
51 | + */ |
|
52 | + public static function prepareFieldValuesFromJson( |
|
53 | + $field_obj, |
|
54 | + $original_value_maybe_array, |
|
55 | + $requested_version, |
|
56 | + $timezone_string = 'UTC' |
|
57 | + ) { |
|
58 | + if (is_array($original_value_maybe_array) |
|
59 | + && ! $field_obj instanceof EE_Serialized_Text_Field |
|
60 | + ) { |
|
61 | + $new_value_maybe_array = array(); |
|
62 | + foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
63 | + $new_value_maybe_array[ $array_key ] = ModelDataTranslator::prepareFieldValueFromJson( |
|
64 | + $field_obj, |
|
65 | + $array_item, |
|
66 | + $requested_version, |
|
67 | + $timezone_string |
|
68 | + ); |
|
69 | + } |
|
70 | + } else { |
|
71 | + $new_value_maybe_array = ModelDataTranslator::prepareFieldValueFromJson( |
|
72 | + $field_obj, |
|
73 | + $original_value_maybe_array, |
|
74 | + $requested_version, |
|
75 | + $timezone_string |
|
76 | + ); |
|
77 | + } |
|
78 | + return $new_value_maybe_array; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * Prepares an array of field values FOR use in JSON/REST API |
|
84 | + * |
|
85 | + * @param EE_Model_Field_Base $field_obj |
|
86 | + * @param mixed $original_value_maybe_array |
|
87 | + * @param string $request_version (eg 4.8.36) |
|
88 | + * @return array |
|
89 | + */ |
|
90 | + public static function prepareFieldValuesForJson($field_obj, $original_value_maybe_array, $request_version) |
|
91 | + { |
|
92 | + if (is_array($original_value_maybe_array)) { |
|
93 | + $new_value = array(); |
|
94 | + foreach ($original_value_maybe_array as $key => $value) { |
|
95 | + $new_value[ $key ] = ModelDataTranslator::prepareFieldValuesForJson( |
|
96 | + $field_obj, |
|
97 | + $value, |
|
98 | + $request_version |
|
99 | + ); |
|
100 | + } |
|
101 | + } else { |
|
102 | + $new_value = ModelDataTranslator::prepareFieldValueForJson( |
|
103 | + $field_obj, |
|
104 | + $original_value_maybe_array, |
|
105 | + $request_version |
|
106 | + ); |
|
107 | + } |
|
108 | + return $new_value; |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
114 | + * "$query_params". |
|
115 | + * |
|
116 | + * @param EE_Model_Field_Base $field_obj |
|
117 | + * @param mixed $original_value |
|
118 | + * @param string $requested_version |
|
119 | + * @param string $timezone_string treat values as being in this timezone |
|
120 | + * @return mixed |
|
121 | + * @throws RestException |
|
122 | + * @throws DomainException |
|
123 | + * @throws EE_Error |
|
124 | + */ |
|
125 | + public static function prepareFieldValueFromJson( |
|
126 | + $field_obj, |
|
127 | + $original_value, |
|
128 | + $requested_version, |
|
129 | + $timezone_string = 'UTC' // UTC |
|
130 | + ) { |
|
131 | + // check if they accidentally submitted an error value. If so throw an exception |
|
132 | + if (is_array($original_value) |
|
133 | + && isset($original_value['error_code'], $original_value['error_message'])) { |
|
134 | + throw new RestException( |
|
135 | + 'rest_submitted_error_value', |
|
136 | + sprintf( |
|
137 | + esc_html__( |
|
138 | + 'You tried to submit a JSON error object as a value for %1$s. That\'s not allowed.', |
|
139 | + 'event_espresso' |
|
140 | + ), |
|
141 | + $field_obj->get_name() |
|
142 | + ), |
|
143 | + array( |
|
144 | + 'status' => 400, |
|
145 | + ) |
|
146 | + ); |
|
147 | + } |
|
148 | + // double-check for serialized PHP. We never accept serialized PHP. No way Jose. |
|
149 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($original_value); |
|
150 | + $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
151 | + $new_value = null; |
|
152 | + // walk through the submitted data and double-check for serialized PHP. We never accept serialized PHP. No |
|
153 | + // way Jose. |
|
154 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($original_value); |
|
155 | + if ($field_obj instanceof EE_Infinite_Integer_Field |
|
156 | + && in_array($original_value, array(null, ''), true) |
|
157 | + ) { |
|
158 | + $new_value = EE_INF; |
|
159 | + } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
160 | + $new_value = rest_parse_date( |
|
161 | + self::getTimestampWithTimezoneOffset($original_value, $field_obj, $timezone_string) |
|
162 | + ); |
|
163 | + if ($new_value === false) { |
|
164 | + throw new RestException( |
|
165 | + 'invalid_format_for_timestamp', |
|
166 | + sprintf( |
|
167 | + esc_html__( |
|
168 | + 'Timestamps received on a request as the value for Date and Time fields must be in %1$s/%2$s format. The timestamp provided (%3$s) is not that format.', |
|
169 | + 'event_espresso' |
|
170 | + ), |
|
171 | + 'RFC3339', |
|
172 | + 'ISO8601', |
|
173 | + $original_value |
|
174 | + ), |
|
175 | + array( |
|
176 | + 'status' => 400, |
|
177 | + ) |
|
178 | + ); |
|
179 | + } |
|
180 | + } else { |
|
181 | + $new_value = $original_value; |
|
182 | + } |
|
183 | + return $new_value; |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * This checks if the incoming timestamp has timezone information already on it and if it doesn't then adds timezone |
|
189 | + * information via details obtained from the host site. |
|
190 | + * |
|
191 | + * @param string $original_timestamp |
|
192 | + * @param EE_Datetime_Field $datetime_field |
|
193 | + * @param $timezone_string |
|
194 | + * @return string |
|
195 | + * @throws DomainException |
|
196 | + */ |
|
197 | + private static function getTimestampWithTimezoneOffset( |
|
198 | + $original_timestamp, |
|
199 | + EE_Datetime_Field $datetime_field, |
|
200 | + $timezone_string |
|
201 | + ) { |
|
202 | + // already have timezone information? |
|
203 | + if (preg_match('/Z|(\+|\-)(\d{2}:\d{2})/', $original_timestamp)) { |
|
204 | + // yes, we're ignoring the timezone. |
|
205 | + return $original_timestamp; |
|
206 | + } |
|
207 | + // need to append timezone |
|
208 | + list($offset_sign, $offset_secs) = self::parseTimezoneOffset( |
|
209 | + $datetime_field->get_timezone_offset( |
|
210 | + new \DateTimeZone($timezone_string), |
|
211 | + $original_timestamp |
|
212 | + ) |
|
213 | + ); |
|
214 | + $offset_string = |
|
215 | + str_pad( |
|
216 | + floor($offset_secs / HOUR_IN_SECONDS), |
|
217 | + 2, |
|
218 | + '0', |
|
219 | + STR_PAD_LEFT |
|
220 | + ) |
|
221 | + . ':' |
|
222 | + . str_pad( |
|
223 | + ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
224 | + 2, |
|
225 | + '0', |
|
226 | + STR_PAD_LEFT |
|
227 | + ); |
|
228 | + return $original_timestamp . $offset_sign . $offset_string; |
|
229 | + } |
|
230 | + |
|
231 | + |
|
232 | + /** |
|
233 | + * Throws an exception if $data is a serialized PHP string (or somehow an actually PHP object, although I don't |
|
234 | + * think that can happen). If $data is an array, recurses into its keys and values |
|
235 | + * |
|
236 | + * @param mixed $data |
|
237 | + * @throws RestException |
|
238 | + * @return void |
|
239 | + */ |
|
240 | + public static function throwExceptionIfContainsSerializedData($data) |
|
241 | + { |
|
242 | + if (is_array($data)) { |
|
243 | + foreach ($data as $key => $value) { |
|
244 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($key); |
|
245 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($value); |
|
246 | + } |
|
247 | + } else { |
|
248 | + if (is_serialized($data) || is_object($data)) { |
|
249 | + throw new RestException( |
|
250 | + 'serialized_data_submission_prohibited', |
|
251 | + esc_html__( |
|
252 | + // @codingStandardsIgnoreStart |
|
253 | + 'You tried to submit a string of serialized text. Serialized PHP is prohibited over the EE4 REST API.', |
|
254 | + // @codingStandardsIgnoreEnd |
|
255 | + 'event_espresso' |
|
256 | + ) |
|
257 | + ); |
|
258 | + } |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + |
|
263 | + /** |
|
264 | + * determines what's going on with them timezone strings |
|
265 | + * |
|
266 | + * @param int $timezone_offset |
|
267 | + * @return array |
|
268 | + */ |
|
269 | + private static function parseTimezoneOffset($timezone_offset) |
|
270 | + { |
|
271 | + $first_char = substr((string) $timezone_offset, 0, 1); |
|
272 | + if ($first_char === '+' || $first_char === '-') { |
|
273 | + $offset_sign = $first_char; |
|
274 | + $offset_secs = substr((string) $timezone_offset, 1); |
|
275 | + } else { |
|
276 | + $offset_sign = '+'; |
|
277 | + $offset_secs = $timezone_offset; |
|
278 | + } |
|
279 | + return array($offset_sign, $offset_secs); |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + /** |
|
284 | + * Prepares a field's value for display in the API |
|
285 | + * |
|
286 | + * @param EE_Model_Field_Base $field_obj |
|
287 | + * @param mixed $original_value |
|
288 | + * @param string $requested_version |
|
289 | + * @return mixed |
|
290 | + */ |
|
291 | + public static function prepareFieldValueForJson($field_obj, $original_value, $requested_version) |
|
292 | + { |
|
293 | + if ($original_value === EE_INF) { |
|
294 | + $new_value = ModelDataTranslator::EE_INF_IN_REST; |
|
295 | + } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
296 | + if (is_string($original_value)) { |
|
297 | + // did they submit a string of a unix timestamp? |
|
298 | + if (is_numeric($original_value)) { |
|
299 | + $datetime_obj = new \DateTime(); |
|
300 | + $datetime_obj->setTimestamp((int) $original_value); |
|
301 | + } else { |
|
302 | + // first, check if its a MySQL timestamp in GMT |
|
303 | + $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
|
304 | + } |
|
305 | + if (! $datetime_obj instanceof \DateTime) { |
|
306 | + // so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
|
307 | + $datetime_obj = $field_obj->prepare_for_set($original_value); |
|
308 | + } |
|
309 | + $original_value = $datetime_obj; |
|
310 | + } |
|
311 | + if ($original_value instanceof \DateTime) { |
|
312 | + $new_value = $original_value->format('Y-m-d H:i:s'); |
|
313 | + } elseif (is_int($original_value) || is_float($original_value)) { |
|
314 | + $new_value = date('Y-m-d H:i:s', $original_value); |
|
315 | + } elseif ($original_value === null || $original_value === '') { |
|
316 | + $new_value = null; |
|
317 | + } else { |
|
318 | + // so it's not a datetime object, unix timestamp (as string or int), |
|
319 | + // MySQL timestamp, or even a string in the field object's format. So no idea what it is |
|
320 | + throw new \EE_Error( |
|
321 | + sprintf( |
|
322 | + esc_html__( |
|
323 | + // @codingStandardsIgnoreStart |
|
324 | + '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".', |
|
325 | + // @codingStandardsIgnoreEnd |
|
326 | + 'event_espresso' |
|
327 | + ), |
|
328 | + $original_value, |
|
329 | + $field_obj->get_name(), |
|
330 | + $field_obj->get_model_name(), |
|
331 | + $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
332 | + ) |
|
333 | + ); |
|
334 | + } |
|
335 | + $new_value = mysql_to_rfc3339($new_value); |
|
336 | + } else { |
|
337 | + $new_value = $original_value; |
|
338 | + } |
|
339 | + // are we about to send an object? just don't. We have no good way to represent it in JSON. |
|
340 | + // can't just check using is_object() because that missed PHP incomplete objects |
|
341 | + if (! ModelDataTranslator::isRepresentableInJson($new_value)) { |
|
342 | + $new_value = array( |
|
343 | + 'error_code' => 'php_object_not_return', |
|
344 | + 'error_message' => esc_html__( |
|
345 | + 'The value of this field in the database is a PHP object, which can\'t be represented in JSON.', |
|
346 | + 'event_espresso' |
|
347 | + ), |
|
348 | + ); |
|
349 | + } |
|
350 | + return apply_filters( |
|
351 | + 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
352 | + $new_value, |
|
353 | + $field_obj, |
|
354 | + $original_value, |
|
355 | + $requested_version |
|
356 | + ); |
|
357 | + } |
|
358 | + |
|
359 | + |
|
360 | + /** |
|
361 | + * Prepares condition-query-parameters (like what's in where and having) from |
|
362 | + * the format expected in the API to use in the models |
|
363 | + * |
|
364 | + * @param array $inputted_query_params_of_this_type |
|
365 | + * @param EEM_Base $model |
|
366 | + * @param string $requested_version |
|
367 | + * @param boolean $writing whether this data will be written to the DB, or if we're just building a query. |
|
368 | + * If we're writing to the DB, we don't expect any operators, or any logic query |
|
369 | + * parameters, and we also won't accept serialized data unless the current user has |
|
370 | + * unfiltered_html. |
|
371 | + * @return array |
|
372 | + * @throws DomainException |
|
373 | + * @throws RestException |
|
374 | + * @throws EE_Error |
|
375 | + */ |
|
376 | + public static function prepareConditionsQueryParamsForModels( |
|
377 | + $inputted_query_params_of_this_type, |
|
378 | + EEM_Base $model, |
|
379 | + $requested_version, |
|
380 | + $writing = false |
|
381 | + ) { |
|
382 | + $query_param_for_models = array(); |
|
383 | + $valid_operators = $model->valid_operators(); |
|
384 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
385 | + $is_gmt_datetime_field = false; |
|
386 | + $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
|
387 | + $query_param_key |
|
388 | + ); |
|
389 | + $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
390 | + $query_param_sans_stars, |
|
391 | + $model |
|
392 | + ); |
|
393 | + // double-check is it a *_gmt field? |
|
394 | + if (! $field instanceof EE_Model_Field_Base |
|
395 | + && ModelDataTranslator::isGmtDateFieldName($query_param_sans_stars) |
|
396 | + ) { |
|
397 | + // yep, take off '_gmt', and find the field |
|
398 | + $query_param_key = ModelDataTranslator::removeGmtFromFieldName($query_param_sans_stars); |
|
399 | + $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
400 | + $query_param_key, |
|
401 | + $model |
|
402 | + ); |
|
403 | + $timezone = 'UTC'; |
|
404 | + $is_gmt_datetime_field = true; |
|
405 | + } elseif ($field instanceof EE_Datetime_Field) { |
|
406 | + // so it's not a GMT field. Set the timezone on the model to the default |
|
407 | + $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
408 | + } else { |
|
409 | + // just keep using what's already set for the timezone |
|
410 | + $timezone = $model->get_timezone(); |
|
411 | + } |
|
412 | + if ($field instanceof EE_Model_Field_Base) { |
|
413 | + if (! $writing && is_array($query_param_value)) { |
|
414 | + if (! \EEH_Array::is_array_numerically_and_sequentially_indexed($query_param_value)) { |
|
415 | + if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
416 | + throw new RestException( |
|
417 | + 'numerically_indexed_array_of_values_only', |
|
418 | + sprintf( |
|
419 | + esc_html__( |
|
420 | + 'The array provided for the parameter "%1$s" should be numerically indexed.', |
|
421 | + 'event_espresso' |
|
422 | + ), |
|
423 | + $query_param_key |
|
424 | + ), |
|
425 | + array( |
|
426 | + 'status' => 400, |
|
427 | + ) |
|
428 | + ); |
|
429 | + } |
|
430 | + } |
|
431 | + // did they specify an operator? |
|
432 | + if (isset($query_param_value[0]) |
|
433 | + && isset($valid_operators[ $query_param_value[0] ]) |
|
434 | + ) { |
|
435 | + $op = $query_param_value[0]; |
|
436 | + $translated_value = array($op); |
|
437 | + if (array_key_exists($op, $model->valid_in_style_operators()) |
|
438 | + && isset($query_param_value[1]) |
|
439 | + && ! isset($query_param_value[2]) |
|
440 | + ) { |
|
441 | + $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
442 | + $field, |
|
443 | + $query_param_value[1], |
|
444 | + $requested_version, |
|
445 | + $timezone |
|
446 | + ); |
|
447 | + } elseif (array_key_exists($op, $model->valid_between_style_operators()) |
|
448 | + && isset($query_param_value[1]) |
|
449 | + && is_array($query_param_value[1]) |
|
450 | + && isset($query_param_key[1][0], $query_param_value[1][1]) |
|
451 | + && ! isset($query_param_value[1][2]) |
|
452 | + && ! isset($query_param_value[2]) |
|
453 | + ) { |
|
454 | + $translated_value[] = array( |
|
455 | + ModelDataTranslator::prepareFieldValuesFromJson( |
|
456 | + $field, |
|
457 | + $query_param_value[1][0], |
|
458 | + $requested_version, |
|
459 | + $timezone |
|
460 | + ), |
|
461 | + ModelDataTranslator::prepareFieldValuesFromJson( |
|
462 | + $field, |
|
463 | + $query_param_value[1][1], |
|
464 | + $requested_version, |
|
465 | + $timezone |
|
466 | + ) |
|
467 | + ); |
|
468 | + } elseif (array_key_exists($op, $model->valid_like_style_operators()) |
|
469 | + && isset($query_param_value[1]) |
|
470 | + && ! isset($query_param_value[2]) |
|
471 | + ) { |
|
472 | + // we want to leave this value mostly-as-is (eg don't force it to be a float |
|
473 | + // or a boolean or an enum value. Leave it as-is with wildcards etc) |
|
474 | + // but do verify it at least doesn't have any serialized data |
|
475 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($query_param_value[1]); |
|
476 | + $translated_value[] = $query_param_value[1]; |
|
477 | + } elseif (array_key_exists($op, $model->valid_null_style_operators()) |
|
478 | + && ! isset($query_param_value[1])) { |
|
479 | + // no arguments should have been provided, so don't look for any |
|
480 | + } elseif (isset($query_param_value[1]) |
|
481 | + && ! isset($query_param_value[2]) |
|
482 | + && ! array_key_exists( |
|
483 | + $op, |
|
484 | + array_merge( |
|
485 | + $model->valid_in_style_operators(), |
|
486 | + $model->valid_null_style_operators(), |
|
487 | + $model->valid_like_style_operators(), |
|
488 | + $model->valid_between_style_operators() |
|
489 | + ) |
|
490 | + ) |
|
491 | + ) { |
|
492 | + // it's a valid operator, but none of the exceptions. Treat it normally. |
|
493 | + $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
494 | + $field, |
|
495 | + $query_param_value[1], |
|
496 | + $requested_version, |
|
497 | + $timezone |
|
498 | + ); |
|
499 | + } else { |
|
500 | + // so they provided a valid operator, but wrong number of arguments |
|
501 | + if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
502 | + throw new RestException( |
|
503 | + 'wrong_number_of_arguments', |
|
504 | + sprintf( |
|
505 | + esc_html__( |
|
506 | + 'The operator you provided, "%1$s" had the wrong number of arguments', |
|
507 | + 'event_espresso' |
|
508 | + ), |
|
509 | + $op |
|
510 | + ), |
|
511 | + array( |
|
512 | + 'status' => 400, |
|
513 | + ) |
|
514 | + ); |
|
515 | + } |
|
516 | + $translated_value = null; |
|
517 | + } |
|
518 | + } else { |
|
519 | + // so they didn't provide a valid operator |
|
520 | + if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
521 | + throw new RestException( |
|
522 | + 'invalid_operator', |
|
523 | + sprintf( |
|
524 | + esc_html__( |
|
525 | + 'You provided an invalid parameter, with key "%1$s" and value "%2$s"', |
|
526 | + 'event_espresso' |
|
527 | + ), |
|
528 | + $query_param_key, |
|
529 | + $query_param_value |
|
530 | + ), |
|
531 | + array( |
|
532 | + 'status' => 400, |
|
533 | + ) |
|
534 | + ); |
|
535 | + } |
|
536 | + // if we aren't in debug mode, then just try our best to fulfill the user's request |
|
537 | + $translated_value = null; |
|
538 | + } |
|
539 | + } else { |
|
540 | + $translated_value = ModelDataTranslator::prepareFieldValueFromJson( |
|
541 | + $field, |
|
542 | + $query_param_value, |
|
543 | + $requested_version, |
|
544 | + $timezone |
|
545 | + ); |
|
546 | + } |
|
547 | + if ((isset($query_param_for_models[ $query_param_key ]) && $is_gmt_datetime_field) |
|
548 | + || $translated_value === null |
|
549 | + ) { |
|
550 | + // they have already provided a non-gmt field, ignore the gmt one. That's what WP core |
|
551 | + // currently does (they might change it though). See https://core.trac.wordpress.org/ticket/39954 |
|
552 | + // OR we couldn't create a translated value from their input |
|
553 | + continue; |
|
554 | + } |
|
555 | + $query_param_for_models[ $query_param_key ] = $translated_value; |
|
556 | + } else { |
|
557 | + // so this param doesn't correspond to a field eh? |
|
558 | + if ($writing) { |
|
559 | + // always tell API clients about invalid parameters when they're creating data. Otherwise, |
|
560 | + // they are probably going to create invalid data |
|
561 | + throw new RestException( |
|
562 | + 'invalid_field', |
|
563 | + sprintf( |
|
564 | + esc_html__('You have provided an invalid parameter: "%1$s"', 'event_espresso'), |
|
565 | + $query_param_key |
|
566 | + ) |
|
567 | + ); |
|
568 | + } else { |
|
569 | + // so it's not for a field, is it a logic query param key? |
|
570 | + if (in_array( |
|
571 | + $query_param_sans_stars, |
|
572 | + $model->logic_query_param_keys() |
|
573 | + )) { |
|
574 | + $query_param_for_models[ $query_param_key ] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
575 | + $query_param_value, |
|
576 | + $model, |
|
577 | + $requested_version |
|
578 | + ); |
|
579 | + } elseif (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
580 | + // only tell API clients they got it wrong if we're in debug mode |
|
581 | + // otherwise try our best ot fulfill their request by ignoring this invalid data |
|
582 | + throw new RestException( |
|
583 | + 'invalid_parameter', |
|
584 | + sprintf( |
|
585 | + esc_html__( |
|
586 | + 'You provided an invalid parameter, with key "%1$s"', |
|
587 | + 'event_espresso' |
|
588 | + ), |
|
589 | + $query_param_sans_stars |
|
590 | + ), |
|
591 | + array( |
|
592 | + 'status' => 400, |
|
593 | + ) |
|
594 | + ); |
|
595 | + } |
|
596 | + } |
|
597 | + } |
|
598 | + } |
|
599 | + return $query_param_for_models; |
|
600 | + } |
|
601 | + |
|
602 | + |
|
603 | + /** |
|
604 | + * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
605 | + * gmt date field name |
|
606 | + * |
|
607 | + * @param string $field_name |
|
608 | + * @return boolean |
|
609 | + */ |
|
610 | + public static function isGmtDateFieldName($field_name) |
|
611 | + { |
|
612 | + return substr( |
|
613 | + ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey($field_name), |
|
614 | + -4, |
|
615 | + 4 |
|
616 | + ) === '_gmt'; |
|
617 | + } |
|
618 | + |
|
619 | + |
|
620 | + /** |
|
621 | + * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
622 | + * |
|
623 | + * @param string $field_name |
|
624 | + * @return string |
|
625 | + */ |
|
626 | + public static function removeGmtFromFieldName($field_name) |
|
627 | + { |
|
628 | + if (! ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
629 | + return $field_name; |
|
630 | + } |
|
631 | + $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
|
632 | + $field_name |
|
633 | + ); |
|
634 | + $query_param_sans_gmt_and_sans_stars = substr( |
|
635 | + $query_param_sans_stars, |
|
636 | + 0, |
|
637 | + strrpos( |
|
638 | + $field_name, |
|
639 | + '_gmt' |
|
640 | + ) |
|
641 | + ); |
|
642 | + return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
643 | + } |
|
644 | + |
|
645 | + |
|
646 | + /** |
|
647 | + * Takes a field name from the REST API and prepares it for the model querying |
|
648 | + * |
|
649 | + * @param string $field_name |
|
650 | + * @return string |
|
651 | + */ |
|
652 | + public static function prepareFieldNameFromJson($field_name) |
|
653 | + { |
|
654 | + if (ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
655 | + return ModelDataTranslator::removeGmtFromFieldName($field_name); |
|
656 | + } |
|
657 | + return $field_name; |
|
658 | + } |
|
659 | + |
|
660 | + |
|
661 | + /** |
|
662 | + * Takes array of field names from REST API and prepares for models |
|
663 | + * |
|
664 | + * @param array $field_names |
|
665 | + * @return array of field names (possibly include model prefixes) |
|
666 | + */ |
|
667 | + public static function prepareFieldNamesFromJson(array $field_names) |
|
668 | + { |
|
669 | + $new_array = array(); |
|
670 | + foreach ($field_names as $key => $field_name) { |
|
671 | + $new_array[ $key ] = ModelDataTranslator::prepareFieldNameFromJson($field_name); |
|
672 | + } |
|
673 | + return $new_array; |
|
674 | + } |
|
675 | + |
|
676 | + |
|
677 | + /** |
|
678 | + * Takes array where array keys are field names (possibly with model path prefixes) |
|
679 | + * from the REST API and prepares them for model querying |
|
680 | + * |
|
681 | + * @param array $field_names_as_keys |
|
682 | + * @return array |
|
683 | + */ |
|
684 | + public static function prepareFieldNamesInArrayKeysFromJson(array $field_names_as_keys) |
|
685 | + { |
|
686 | + $new_array = array(); |
|
687 | + foreach ($field_names_as_keys as $field_name => $value) { |
|
688 | + $new_array[ ModelDataTranslator::prepareFieldNameFromJson($field_name) ] = $value; |
|
689 | + } |
|
690 | + return $new_array; |
|
691 | + } |
|
692 | + |
|
693 | + |
|
694 | + /** |
|
695 | + * Prepares an array of model query params for use in the REST API |
|
696 | + * |
|
697 | + * @param array $model_query_params |
|
698 | + * @param EEM_Base $model |
|
699 | + * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
700 | + * REST API |
|
701 | + * @return array which can be passed into the EE4 REST API when querying a model resource |
|
702 | + * @throws EE_Error |
|
703 | + */ |
|
704 | + public static function prepareQueryParamsForRestApi( |
|
705 | + array $model_query_params, |
|
706 | + EEM_Base $model, |
|
707 | + $requested_version = null |
|
708 | + ) { |
|
709 | + if ($requested_version === null) { |
|
710 | + $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
711 | + } |
|
712 | + $rest_query_params = $model_query_params; |
|
713 | + if (isset($model_query_params[0])) { |
|
714 | + $rest_query_params['where'] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
715 | + $model_query_params[0], |
|
716 | + $model, |
|
717 | + $requested_version |
|
718 | + ); |
|
719 | + unset($rest_query_params[0]); |
|
720 | + } |
|
721 | + if (isset($model_query_params['having'])) { |
|
722 | + $rest_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
723 | + $model_query_params['having'], |
|
724 | + $model, |
|
725 | + $requested_version |
|
726 | + ); |
|
727 | + } |
|
728 | + return apply_filters( |
|
729 | + 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
730 | + $rest_query_params, |
|
731 | + $model_query_params, |
|
732 | + $model, |
|
733 | + $requested_version |
|
734 | + ); |
|
735 | + } |
|
736 | + |
|
737 | + |
|
738 | + /** |
|
739 | + * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
740 | + * |
|
741 | + * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
742 | + * passed into EEM_Base::get_all() |
|
743 | + * @param EEM_Base $model |
|
744 | + * @param string $requested_version eg "4.8.36" |
|
745 | + * @return array ready for use in the rest api query params |
|
746 | + * @throws EE_Error |
|
747 | + * @throws ObjectDetectedException if somehow a PHP object were in the query params' values, |
|
748 | + * (which would be really unusual) |
|
749 | + */ |
|
750 | + public static function prepareConditionsQueryParamsForRestApi( |
|
751 | + $inputted_query_params_of_this_type, |
|
752 | + EEM_Base $model, |
|
753 | + $requested_version |
|
754 | + ) { |
|
755 | + $query_param_for_models = array(); |
|
756 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
757 | + $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
758 | + ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey($query_param_key), |
|
759 | + $model |
|
760 | + ); |
|
761 | + if ($field instanceof EE_Model_Field_Base) { |
|
762 | + // did they specify an operator? |
|
763 | + if (is_array($query_param_value)) { |
|
764 | + $op = $query_param_value[0]; |
|
765 | + $translated_value = array($op); |
|
766 | + if (isset($query_param_value[1])) { |
|
767 | + $value = $query_param_value[1]; |
|
768 | + $translated_value[1] = ModelDataTranslator::prepareFieldValuesForJson( |
|
769 | + $field, |
|
770 | + $value, |
|
771 | + $requested_version |
|
772 | + ); |
|
773 | + } |
|
774 | + } else { |
|
775 | + $translated_value = ModelDataTranslator::prepareFieldValueForJson( |
|
776 | + $field, |
|
777 | + $query_param_value, |
|
778 | + $requested_version |
|
779 | + ); |
|
780 | + } |
|
781 | + $query_param_for_models[ $query_param_key ] = $translated_value; |
|
782 | + } else { |
|
783 | + // so it's not for a field, assume it's a logic query param key |
|
784 | + $query_param_for_models[ $query_param_key ] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
785 | + $query_param_value, |
|
786 | + $model, |
|
787 | + $requested_version |
|
788 | + ); |
|
789 | + } |
|
790 | + } |
|
791 | + return $query_param_for_models; |
|
792 | + } |
|
793 | + |
|
794 | + |
|
795 | + /** |
|
796 | + * @param $condition_query_param_key |
|
797 | + * @return string |
|
798 | + */ |
|
799 | + public static function removeStarsAndAnythingAfterFromConditionQueryParamKey($condition_query_param_key) |
|
800 | + { |
|
801 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
802 | + if ($pos_of_star === false) { |
|
803 | + return $condition_query_param_key; |
|
804 | + } else { |
|
805 | + $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
806 | + return $condition_query_param_sans_star; |
|
807 | + } |
|
808 | + } |
|
809 | + |
|
810 | + |
|
811 | + /** |
|
812 | + * Takes the input parameter and finds the model field that it indicates. |
|
813 | + * |
|
814 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
815 | + * @param EEM_Base $model |
|
816 | + * @return EE_Model_Field_Base |
|
817 | + * @throws EE_Error |
|
818 | + */ |
|
819 | + public static function deduceFieldFromQueryParam($query_param_name, EEM_Base $model) |
|
820 | + { |
|
821 | + // ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
822 | + // which will help us find the database table and column |
|
823 | + $query_param_parts = explode('.', $query_param_name); |
|
824 | + if (empty($query_param_parts)) { |
|
825 | + throw new EE_Error( |
|
826 | + sprintf( |
|
827 | + __( |
|
828 | + '_extract_column_name is empty when trying to extract column and table name from %s', |
|
829 | + 'event_espresso' |
|
830 | + ), |
|
831 | + $query_param_name |
|
832 | + ) |
|
833 | + ); |
|
834 | + } |
|
835 | + $number_of_parts = count($query_param_parts); |
|
836 | + $last_query_param_part = $query_param_parts[ count($query_param_parts) - 1 ]; |
|
837 | + if ($number_of_parts === 1) { |
|
838 | + $field_name = $last_query_param_part; |
|
839 | + } else {// $number_of_parts >= 2 |
|
840 | + // the last part is the column name, and there are only 2parts. therefore... |
|
841 | + $field_name = $last_query_param_part; |
|
842 | + $model = \EE_Registry::instance()->load_model($query_param_parts[ $number_of_parts - 2 ]); |
|
843 | + } |
|
844 | + try { |
|
845 | + return $model->field_settings_for($field_name, false); |
|
846 | + } catch (EE_Error $e) { |
|
847 | + return null; |
|
848 | + } |
|
849 | + } |
|
850 | + |
|
851 | + |
|
852 | + /** |
|
853 | + * Returns true if $data can be easily represented in JSON. |
|
854 | + * Basically, objects and resources can't be represented in JSON easily. |
|
855 | + * |
|
856 | + * @param mixed $data |
|
857 | + * @return bool |
|
858 | + */ |
|
859 | + protected static function isRepresentableInJson($data) |
|
860 | + { |
|
861 | + return is_scalar($data) |
|
862 | + || is_array($data) |
|
863 | + || is_null($data); |
|
864 | + } |
|
865 | 865 | } |
@@ -3,33 +3,33 @@ |
||
3 | 3 | global $post; |
4 | 4 | ?> |
5 | 5 | <div class="event-content"> |
6 | -<?php if ( apply_filters( 'FHEE__content_espresso_events_details_template__display_entry_meta', TRUE )): ?> |
|
6 | +<?php if (apply_filters('FHEE__content_espresso_events_details_template__display_entry_meta', TRUE)): ?> |
|
7 | 7 | <div class="entry-meta"> |
8 | - <span class="tags-links"><?php espresso_event_categories( $post->ID, TRUE, TRUE ); ?></span> |
|
8 | + <span class="tags-links"><?php espresso_event_categories($post->ID, TRUE, TRUE); ?></span> |
|
9 | 9 | <?php |
10 | - if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : |
|
10 | + if ( ! post_password_required() && (comments_open() || get_comments_number())) : |
|
11 | 11 | ?> |
12 | - <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'event_espresso' ), __( '1 Comment', 'event_espresso' ), __( '% Comments', 'event_espresso' ) ); ?></span> |
|
12 | + <span class="comments-link"><?php comments_popup_link(__('Leave a comment', 'event_espresso'), __('1 Comment', 'event_espresso'), __('% Comments', 'event_espresso')); ?></span> |
|
13 | 13 | <?php |
14 | 14 | endif; |
15 | - edit_post_link( __( 'Edit', 'event_espresso' ), '<span class="edit-link">', '</span>' ); |
|
15 | + edit_post_link(__('Edit', 'event_espresso'), '<span class="edit-link">', '</span>'); |
|
16 | 16 | ?> |
17 | 17 | </div> |
18 | 18 | <?php endif; |
19 | - $event_phone = espresso_event_phone( $post->ID, FALSE ); |
|
20 | - if ( $event_phone != '' ) : ?> |
|
19 | + $event_phone = espresso_event_phone($post->ID, FALSE); |
|
20 | + if ($event_phone != '') : ?> |
|
21 | 21 | <p class="event-phone"> |
22 | - <span class="small-text"><strong><?php esc_html_e( 'Event Phone:', 'event_espresso' ); ?> </strong></span> <?php echo $event_phone; ?> |
|
22 | + <span class="small-text"><strong><?php esc_html_e('Event Phone:', 'event_espresso'); ?> </strong></span> <?php echo $event_phone; ?> |
|
23 | 23 | </p> |
24 | -<?php endif; ?> |
|
24 | +<?php endif; ?> |
|
25 | 25 | <?php |
26 | - if ( apply_filters( 'FHEE__content_espresso_events_details_template__display_the_content', true ) ) { |
|
27 | - do_action( 'AHEE_event_details_before_the_content', $post ); |
|
26 | + if (apply_filters('FHEE__content_espresso_events_details_template__display_the_content', true)) { |
|
27 | + do_action('AHEE_event_details_before_the_content', $post); |
|
28 | 28 | echo apply_filters( |
29 | 29 | 'FHEE__content_espresso_events_details_template__the_content', |
30 | - espresso_event_content_or_excerpt( 55, null, false ) |
|
30 | + espresso_event_content_or_excerpt(55, null, false) |
|
31 | 31 | ); |
32 | - do_action( 'AHEE_event_details_after_the_content', $post ); |
|
32 | + do_action('AHEE_event_details_after_the_content', $post); |
|
33 | 33 | } |
34 | 34 | ?> |
35 | 35 | </div> |
@@ -38,103 +38,103 @@ |
||
38 | 38 | * @since 4.0 |
39 | 39 | */ |
40 | 40 | if (function_exists('espresso_version')) { |
41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
42 | - /** |
|
43 | - * espresso_duplicate_plugin_error |
|
44 | - * displays if more than one version of EE is activated at the same time |
|
45 | - */ |
|
46 | - function espresso_duplicate_plugin_error() |
|
47 | - { |
|
48 | - ?> |
|
41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
42 | + /** |
|
43 | + * espresso_duplicate_plugin_error |
|
44 | + * displays if more than one version of EE is activated at the same time |
|
45 | + */ |
|
46 | + function espresso_duplicate_plugin_error() |
|
47 | + { |
|
48 | + ?> |
|
49 | 49 | <div class="error"> |
50 | 50 | <p> |
51 | 51 | <?php |
52 | - 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 | - ); ?> |
|
52 | + 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 | + ); ?> |
|
56 | 56 | </p> |
57 | 57 | </div> |
58 | 58 | <?php |
59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | - } |
|
61 | - } |
|
62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | + } |
|
61 | + } |
|
62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
63 | 63 | } else { |
64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
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.4.0'); |
|
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 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
97 | - /** |
|
98 | - * espresso_version |
|
99 | - * Returns the plugin version |
|
100 | - * |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - function espresso_version() |
|
104 | - { |
|
105 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.65.rc.012'); |
|
106 | - } |
|
94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | + } else { |
|
96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
97 | + /** |
|
98 | + * espresso_version |
|
99 | + * Returns the plugin version |
|
100 | + * |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + function espresso_version() |
|
104 | + { |
|
105 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.65.rc.012'); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * espresso_plugin_activation |
|
110 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
111 | - */ |
|
112 | - function espresso_plugin_activation() |
|
113 | - { |
|
114 | - update_option('ee_espresso_activation', true); |
|
115 | - } |
|
108 | + /** |
|
109 | + * espresso_plugin_activation |
|
110 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
111 | + */ |
|
112 | + function espresso_plugin_activation() |
|
113 | + { |
|
114 | + update_option('ee_espresso_activation', true); |
|
115 | + } |
|
116 | 116 | |
117 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
117 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
118 | 118 | |
119 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
120 | - bootstrap_espresso(); |
|
121 | - } |
|
119 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
120 | + bootstrap_espresso(); |
|
121 | + } |
|
122 | 122 | } |
123 | 123 | if (! function_exists('espresso_deactivate_plugin')) { |
124 | - /** |
|
125 | - * deactivate_plugin |
|
126 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
127 | - * |
|
128 | - * @access public |
|
129 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
130 | - * @return void |
|
131 | - */ |
|
132 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
133 | - { |
|
134 | - if (! function_exists('deactivate_plugins')) { |
|
135 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
136 | - } |
|
137 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
138 | - deactivate_plugins($plugin_basename); |
|
139 | - } |
|
124 | + /** |
|
125 | + * deactivate_plugin |
|
126 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
127 | + * |
|
128 | + * @access public |
|
129 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
130 | + * @return void |
|
131 | + */ |
|
132 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
133 | + { |
|
134 | + if (! function_exists('deactivate_plugins')) { |
|
135 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
136 | + } |
|
137 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
138 | + deactivate_plugins($plugin_basename); |
|
139 | + } |
|
140 | 140 | } |