@@ -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 | |
@@ -24,462 +24,462 @@ discard block |
||
24 | 24 | class Model_Version_Info |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * Constant used in the $_model_changes array to indicate that a model |
|
29 | - * was completely new in this version |
|
30 | - */ |
|
31 | - const model_added = 'model_added_in_this_version'; |
|
32 | - |
|
33 | - /** |
|
34 | - * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
35 | - * next-level keys are model names (eg "Event") that underwent some change in that version |
|
36 | - * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version), |
|
37 | - * or it's an array where the values are model field names, |
|
38 | - * or API resource properties (ie, non-model fields that appear in REST API results) |
|
39 | - * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
40 | - * |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - protected $_model_changes = array(); |
|
44 | - |
|
45 | - /** |
|
46 | - * top-level keys are version numbers, |
|
47 | - * next-level keys are model CLASSNAMES (even parent classnames), |
|
48 | - * and next-level keys are extra resource properties to attach to those models' resources, |
|
49 | - * and next-level key-value pairs, where the keys are: |
|
50 | - * 'raw', 'type', 'nullable', 'table_alias', 'table_column', 'always_available' |
|
51 | - * |
|
52 | - * @var array |
|
53 | - */ |
|
54 | - protected $_resource_changes = array(); |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string indicating what version of the API was requested |
|
58 | - * (eg although core might be at version 4.8.11, they may have sent a request |
|
59 | - * for 4.6) |
|
60 | - */ |
|
61 | - protected $_requested_version = null; |
|
62 | - |
|
63 | - /** |
|
64 | - * Keys are model names, values are their classnames. |
|
65 | - * We cache this so we only need to calculate this once per request |
|
66 | - * |
|
67 | - * @var array |
|
68 | - */ |
|
69 | - protected $_cached_models_for_requested_version = null; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - protected $_cached_model_changes_between_requested_version_and_current = null; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var array |
|
78 | - */ |
|
79 | - protected $_cached_resource_changes_between_requested_version_and_current = null; |
|
80 | - |
|
81 | - /** |
|
82 | - * 2d array where top-level keys are model names, 2nd-level keys are field names |
|
83 | - * and values are the actual field objects |
|
84 | - * |
|
85 | - * @var array |
|
86 | - */ |
|
87 | - protected $_cached_fields_on_models = array(); |
|
88 | - |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Model_Version_Info constructor. |
|
93 | - * |
|
94 | - * @param string $requested_version |
|
95 | - */ |
|
96 | - public function __construct($requested_version) |
|
97 | - { |
|
98 | - $this->_requested_version = $requested_version; |
|
99 | - $this->_model_changes = array( |
|
100 | - '4.8.29' => array( |
|
101 | - //first version where the REST API is in EE core, so no need |
|
102 | - //to specify how its different from the previous |
|
103 | - ), |
|
104 | - ); |
|
105 | - //setup data for "extra" fields added onto resources which don't actually exist on models |
|
106 | - $this->_resource_changes = apply_filters( |
|
107 | - 'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models', |
|
108 | - array() |
|
109 | - ); |
|
110 | - $defaults = array( |
|
111 | - 'raw' => false, |
|
112 | - 'type' => 'N/A', |
|
113 | - 'nullable' => true, |
|
114 | - 'table_alias' => 'N/A', |
|
115 | - 'table_column' => 'N/A', |
|
116 | - 'always_available' => true, |
|
117 | - ); |
|
118 | - foreach ($this->_resource_changes as $version => $model_classnames) { |
|
119 | - foreach ($model_classnames as $model_classname => $extra_fields) { |
|
120 | - foreach ($extra_fields as $fieldname => $field_data) { |
|
121 | - $this->_resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
122 | - foreach ($defaults as $attribute => $default_value) { |
|
123 | - if (! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
124 | - $this->_resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - } |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * Returns a slice of Model_Version_Info::model_changes()'s array |
|
136 | - * indicating exactly what changes happened between the current core version, |
|
137 | - * and the version requested |
|
138 | - * |
|
139 | - * @return array |
|
140 | - */ |
|
141 | - public function model_changes_between_requested_version_and_current() |
|
142 | - { |
|
143 | - if ($this->_cached_model_changes_between_requested_version_and_current === null) { |
|
144 | - $model_changes = array(); |
|
145 | - foreach ($this->model_changes() as $version => $models_changed_in_version) { |
|
146 | - if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
147 | - $model_changes[$version] = $models_changed_in_version; |
|
148 | - } |
|
149 | - } |
|
150 | - $this->_cached_model_changes_between_requested_version_and_current = $model_changes; |
|
151 | - } |
|
152 | - return $this->_cached_model_changes_between_requested_version_and_current; |
|
153 | - } |
|
154 | - |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * Returns a slice of Model_Version_Info::model_changes()'s array |
|
159 | - * indicating exactly what changes happened between the current core version, |
|
160 | - * and the version requested |
|
161 | - * |
|
162 | - * @return array |
|
163 | - */ |
|
164 | - public function resource_changes_between_requested_version_and_current() |
|
165 | - { |
|
166 | - if ($this->_cached_resource_changes_between_requested_version_and_current === null) { |
|
167 | - $resource_changes = array(); |
|
168 | - foreach ($this->resource_changes() as $version => $model_classnames) { |
|
169 | - if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
170 | - $resource_changes[$version] = $model_classnames; |
|
171 | - } |
|
172 | - } |
|
173 | - $this->_cached_resource_changes_between_requested_version_and_current = $resource_changes; |
|
174 | - } |
|
175 | - return $this->_cached_resource_changes_between_requested_version_and_current; |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
|
182 | - * |
|
183 | - * @return string like '4.6' |
|
184 | - */ |
|
185 | - public function requested_version() |
|
186 | - { |
|
187 | - return $this->_requested_version; |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - |
|
192 | - /** |
|
193 | - * Returns an array describing how the models have changed in each version of core |
|
194 | - * that supports the API (starting at 4.6) |
|
195 | - * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
196 | - * next-level keys are model names (eg "Event") that underwent some change in that version |
|
197 | - * and the value is either NULL (indicating the model is completely NEW in this version), |
|
198 | - * or it's an array where fields are value names. |
|
199 | - * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
200 | - * |
|
201 | - * @return array |
|
202 | - */ |
|
203 | - public function model_changes() |
|
204 | - { |
|
205 | - return $this->_model_changes; |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * Takes into account the requested version, and the current version, and |
|
212 | - * what changed between the two, and tries to return. |
|
213 | - * Analogous to EE_Registry::instance()->non_abstract_db_models |
|
214 | - * |
|
215 | - * @return array keys are model names, values are their classname |
|
216 | - */ |
|
217 | - public function models_for_requested_version() |
|
218 | - { |
|
219 | - if ($this->_cached_models_for_requested_version === null) { |
|
220 | - $all_models_in_current_version = \EE_Registry::instance()->non_abstract_db_models; |
|
221 | - foreach ($this->model_changes_between_requested_version_and_current() as $version => $models_changed) { |
|
222 | - foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
223 | - if ($new_indicator_or_fields_added === Model_Version_Info::model_added) { |
|
224 | - unset($all_models_in_current_version[$model_name]); |
|
225 | - } |
|
226 | - } |
|
227 | - } |
|
228 | - $this->_cached_models_for_requested_version = apply_filters( |
|
229 | - 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
230 | - $all_models_in_current_version, |
|
231 | - $this |
|
232 | - ); |
|
233 | - } |
|
234 | - return $this->_cached_models_for_requested_version; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * Determines if this is a valid model name in the requested version. |
|
241 | - * Similar to EE_Registry::instance()->is_model_name(), but takes the requested |
|
242 | - * version's models into account |
|
243 | - * |
|
244 | - * @param string $model_name eg 'Event' |
|
245 | - * @return boolean |
|
246 | - */ |
|
247 | - public function is_model_name_in_this_version($model_name) |
|
248 | - { |
|
249 | - $model_names = $this->models_for_requested_version(); |
|
250 | - if (isset($model_names[$model_name])) { |
|
251 | - return true; |
|
252 | - } else { |
|
253 | - return false; |
|
254 | - } |
|
255 | - } |
|
256 | - |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Wrapper for EE_Registry::instance()->load_model(), but takes the requested |
|
261 | - * version's models into account |
|
262 | - * |
|
263 | - * @param string $model_name |
|
264 | - * @return \EEM_Base |
|
265 | - * @throws \EE_Error |
|
266 | - */ |
|
267 | - public function load_model($model_name) |
|
268 | - { |
|
269 | - if ($this->is_model_name_in_this_version($model_name)) { |
|
270 | - return \EE_Registry::instance()->load_model($model_name); |
|
271 | - } else { |
|
272 | - throw new \EE_Error( |
|
273 | - sprintf( |
|
274 | - __( |
|
275 | - 'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso', |
|
276 | - 'event_espresso' |
|
277 | - ), |
|
278 | - $model_name, |
|
279 | - $this->requested_version() |
|
280 | - ) |
|
281 | - ); |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - |
|
287 | - /** |
|
288 | - * Gets all the fields that should exist on this model right now |
|
289 | - * |
|
290 | - * @param \EEM_Base $model |
|
291 | - * @return array|\EE_Model_Field_Base[] |
|
292 | - */ |
|
293 | - public function fields_on_model_in_this_version($model) |
|
294 | - { |
|
295 | - if (! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
296 | - //get all model changes between the requested version and current core version |
|
297 | - $changes = $this->model_changes_between_requested_version_and_current(); |
|
298 | - //fetch all fields currently on this model |
|
299 | - $current_fields = $model->field_settings(); |
|
300 | - //remove all fields that have been added since |
|
301 | - foreach ($changes as $version => $changes_in_version) { |
|
302 | - if ( |
|
303 | - isset($changes_in_version[$model->get_this_model_name()]) |
|
304 | - && $changes_in_version[$model->get_this_model_name()] !== Model_Version_Info::model_added |
|
305 | - ) { |
|
306 | - $current_fields = array_diff_key( |
|
307 | - $current_fields, |
|
308 | - array_flip($changes_in_version[$model->get_this_model_name()]) |
|
309 | - ); |
|
310 | - } |
|
311 | - } |
|
312 | - $this->_cached_fields_on_models = $current_fields; |
|
313 | - } |
|
314 | - return $this->_cached_fields_on_models; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Determines if $object is of one of the classes of $classes. Similar to |
|
321 | - * in_array(), except this checks if $object is a subclass of the classnames provided |
|
322 | - * in $classnames |
|
323 | - * |
|
324 | - * @param object $object |
|
325 | - * @param array $classnames |
|
326 | - * @return boolean |
|
327 | - */ |
|
328 | - public function is_subclass_of_one($object, $classnames) |
|
329 | - { |
|
330 | - foreach ($classnames as $classname) { |
|
331 | - if (is_a($object, $classname)) { |
|
332 | - return true; |
|
333 | - } |
|
334 | - } |
|
335 | - return false; |
|
336 | - } |
|
337 | - |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * Returns the list of model field classes that that the API basically ignores |
|
342 | - * |
|
343 | - * @return array |
|
344 | - */ |
|
345 | - public function fields_ignored() |
|
346 | - { |
|
347 | - return apply_filters( |
|
348 | - 'FHEE__Controller_Model_Read_fields_ignored', |
|
349 | - array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
350 | - ); |
|
351 | - } |
|
352 | - |
|
353 | - |
|
354 | - |
|
355 | - /** |
|
356 | - * If this field one that should be ignored by the API? |
|
357 | - * |
|
358 | - * @param EE_Model_Field_Base |
|
359 | - * @return boolean |
|
360 | - */ |
|
361 | - public function field_is_ignored($field_obj) |
|
362 | - { |
|
363 | - return $this->is_subclass_of_one($field_obj, $this->fields_ignored()); |
|
364 | - } |
|
365 | - |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * Returns the list of model field classes that have a "raw" and non-raw formats. |
|
370 | - * Normally the "raw" versions are only accessible to those who can edit them. |
|
371 | - * |
|
372 | - * @return array an array of EE_Model_Field_Base child classnames |
|
373 | - */ |
|
374 | - public function fields_that_have_rendered_format() |
|
375 | - { |
|
376 | - return apply_filters( |
|
377 | - 'FHEE__Controller_Model_Read__fields_raw', |
|
378 | - array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
379 | - ); |
|
380 | - } |
|
381 | - |
|
382 | - |
|
383 | - |
|
384 | - /** |
|
385 | - * If this field one that has a raw format |
|
386 | - * |
|
387 | - * @param EE_Model_Field_Base |
|
388 | - * @return boolean |
|
389 | - */ |
|
390 | - public function field_has_rendered_format($field_obj) |
|
391 | - { |
|
392 | - return $this->is_subclass_of_one($field_obj, $this->fields_that_have_rendered_format()); |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - |
|
397 | - /** |
|
398 | - * Returns the list of model field classes that have a "_pretty" and non-pretty versions. |
|
399 | - * The pretty version of the field is NOT queryable or editable, but requires no extra permissions |
|
400 | - * to view |
|
401 | - * |
|
402 | - * @return array an array of EE_Model_Field_Base child classnames |
|
403 | - */ |
|
404 | - public function fields_that_have_pretty_format() |
|
405 | - { |
|
406 | - return apply_filters( |
|
407 | - 'FHEE__Controller_Model_Read__fields_pretty', |
|
408 | - array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
409 | - ); |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - |
|
414 | - /** |
|
415 | - * If this field one that has a pretty equivalent |
|
416 | - * |
|
417 | - * @param EE_Model_Field_Base |
|
418 | - * @return boolean |
|
419 | - */ |
|
420 | - public function field_has_pretty_format($field_obj) |
|
421 | - { |
|
422 | - return $this->is_subclass_of_one($field_obj, $this->fields_that_have_pretty_format()); |
|
423 | - } |
|
424 | - |
|
425 | - |
|
426 | - |
|
427 | - /** |
|
428 | - * Returns an array describing what extra API resource properties have been added through the versions |
|
429 | - * |
|
430 | - * @return array @see $this->_extra_resource_properties_for_models |
|
431 | - */ |
|
432 | - public function resource_changes() |
|
433 | - { |
|
434 | - return $this->_resource_changes; |
|
435 | - } |
|
436 | - |
|
437 | - |
|
438 | - |
|
439 | - /** |
|
440 | - * Returns an array where keys are extra resource properties in this version of the API, |
|
441 | - * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes |
|
442 | - * |
|
443 | - * @param \EEM_Base $model |
|
444 | - * @return array |
|
445 | - */ |
|
446 | - public function extra_resource_properties_for_model($model) |
|
447 | - { |
|
448 | - $extra_properties = array(); |
|
449 | - foreach ($this->resource_changes_between_requested_version_and_current() as $version => $model_classnames) { |
|
450 | - foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
451 | - if (is_subclass_of($model, $model_classname)) { |
|
452 | - $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
453 | - } |
|
454 | - } |
|
455 | - } |
|
456 | - return $extra_properties; |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * Gets all the related models for the specified model. It's good to use this |
|
463 | - * in case this model didn't exist for this version or something |
|
464 | - * |
|
465 | - * @param \EEM_Base $model |
|
466 | - * @return \EE_Model_Relation_Base[] |
|
467 | - */ |
|
468 | - public function relation_settings(\EEM_Base $model) |
|
469 | - { |
|
470 | - $relations = array(); |
|
471 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
472 | - if ($this->is_model_name_in_this_version($relation_name)) { |
|
473 | - $relations[$relation_name] = $relation_obj; |
|
474 | - } |
|
475 | - } |
|
476 | - //filter the results, but use the old filter name |
|
477 | - return apply_filters( |
|
478 | - 'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', |
|
479 | - $relations, |
|
480 | - $model |
|
481 | - ); |
|
482 | - } |
|
27 | + /** |
|
28 | + * Constant used in the $_model_changes array to indicate that a model |
|
29 | + * was completely new in this version |
|
30 | + */ |
|
31 | + const model_added = 'model_added_in_this_version'; |
|
32 | + |
|
33 | + /** |
|
34 | + * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
35 | + * next-level keys are model names (eg "Event") that underwent some change in that version |
|
36 | + * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version), |
|
37 | + * or it's an array where the values are model field names, |
|
38 | + * or API resource properties (ie, non-model fields that appear in REST API results) |
|
39 | + * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
40 | + * |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + protected $_model_changes = array(); |
|
44 | + |
|
45 | + /** |
|
46 | + * top-level keys are version numbers, |
|
47 | + * next-level keys are model CLASSNAMES (even parent classnames), |
|
48 | + * and next-level keys are extra resource properties to attach to those models' resources, |
|
49 | + * and next-level key-value pairs, where the keys are: |
|
50 | + * 'raw', 'type', 'nullable', 'table_alias', 'table_column', 'always_available' |
|
51 | + * |
|
52 | + * @var array |
|
53 | + */ |
|
54 | + protected $_resource_changes = array(); |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string indicating what version of the API was requested |
|
58 | + * (eg although core might be at version 4.8.11, they may have sent a request |
|
59 | + * for 4.6) |
|
60 | + */ |
|
61 | + protected $_requested_version = null; |
|
62 | + |
|
63 | + /** |
|
64 | + * Keys are model names, values are their classnames. |
|
65 | + * We cache this so we only need to calculate this once per request |
|
66 | + * |
|
67 | + * @var array |
|
68 | + */ |
|
69 | + protected $_cached_models_for_requested_version = null; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + protected $_cached_model_changes_between_requested_version_and_current = null; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var array |
|
78 | + */ |
|
79 | + protected $_cached_resource_changes_between_requested_version_and_current = null; |
|
80 | + |
|
81 | + /** |
|
82 | + * 2d array where top-level keys are model names, 2nd-level keys are field names |
|
83 | + * and values are the actual field objects |
|
84 | + * |
|
85 | + * @var array |
|
86 | + */ |
|
87 | + protected $_cached_fields_on_models = array(); |
|
88 | + |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Model_Version_Info constructor. |
|
93 | + * |
|
94 | + * @param string $requested_version |
|
95 | + */ |
|
96 | + public function __construct($requested_version) |
|
97 | + { |
|
98 | + $this->_requested_version = $requested_version; |
|
99 | + $this->_model_changes = array( |
|
100 | + '4.8.29' => array( |
|
101 | + //first version where the REST API is in EE core, so no need |
|
102 | + //to specify how its different from the previous |
|
103 | + ), |
|
104 | + ); |
|
105 | + //setup data for "extra" fields added onto resources which don't actually exist on models |
|
106 | + $this->_resource_changes = apply_filters( |
|
107 | + 'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models', |
|
108 | + array() |
|
109 | + ); |
|
110 | + $defaults = array( |
|
111 | + 'raw' => false, |
|
112 | + 'type' => 'N/A', |
|
113 | + 'nullable' => true, |
|
114 | + 'table_alias' => 'N/A', |
|
115 | + 'table_column' => 'N/A', |
|
116 | + 'always_available' => true, |
|
117 | + ); |
|
118 | + foreach ($this->_resource_changes as $version => $model_classnames) { |
|
119 | + foreach ($model_classnames as $model_classname => $extra_fields) { |
|
120 | + foreach ($extra_fields as $fieldname => $field_data) { |
|
121 | + $this->_resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
122 | + foreach ($defaults as $attribute => $default_value) { |
|
123 | + if (! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
124 | + $this->_resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + } |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * Returns a slice of Model_Version_Info::model_changes()'s array |
|
136 | + * indicating exactly what changes happened between the current core version, |
|
137 | + * and the version requested |
|
138 | + * |
|
139 | + * @return array |
|
140 | + */ |
|
141 | + public function model_changes_between_requested_version_and_current() |
|
142 | + { |
|
143 | + if ($this->_cached_model_changes_between_requested_version_and_current === null) { |
|
144 | + $model_changes = array(); |
|
145 | + foreach ($this->model_changes() as $version => $models_changed_in_version) { |
|
146 | + if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
147 | + $model_changes[$version] = $models_changed_in_version; |
|
148 | + } |
|
149 | + } |
|
150 | + $this->_cached_model_changes_between_requested_version_and_current = $model_changes; |
|
151 | + } |
|
152 | + return $this->_cached_model_changes_between_requested_version_and_current; |
|
153 | + } |
|
154 | + |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * Returns a slice of Model_Version_Info::model_changes()'s array |
|
159 | + * indicating exactly what changes happened between the current core version, |
|
160 | + * and the version requested |
|
161 | + * |
|
162 | + * @return array |
|
163 | + */ |
|
164 | + public function resource_changes_between_requested_version_and_current() |
|
165 | + { |
|
166 | + if ($this->_cached_resource_changes_between_requested_version_and_current === null) { |
|
167 | + $resource_changes = array(); |
|
168 | + foreach ($this->resource_changes() as $version => $model_classnames) { |
|
169 | + if ($version <= \EED_Core_Rest_Api::core_version() && $version > $this->requested_version()) { |
|
170 | + $resource_changes[$version] = $model_classnames; |
|
171 | + } |
|
172 | + } |
|
173 | + $this->_cached_resource_changes_between_requested_version_and_current = $resource_changes; |
|
174 | + } |
|
175 | + return $this->_cached_resource_changes_between_requested_version_and_current; |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
|
182 | + * |
|
183 | + * @return string like '4.6' |
|
184 | + */ |
|
185 | + public function requested_version() |
|
186 | + { |
|
187 | + return $this->_requested_version; |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + |
|
192 | + /** |
|
193 | + * Returns an array describing how the models have changed in each version of core |
|
194 | + * that supports the API (starting at 4.6) |
|
195 | + * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
196 | + * next-level keys are model names (eg "Event") that underwent some change in that version |
|
197 | + * and the value is either NULL (indicating the model is completely NEW in this version), |
|
198 | + * or it's an array where fields are value names. |
|
199 | + * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
200 | + * |
|
201 | + * @return array |
|
202 | + */ |
|
203 | + public function model_changes() |
|
204 | + { |
|
205 | + return $this->_model_changes; |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * Takes into account the requested version, and the current version, and |
|
212 | + * what changed between the two, and tries to return. |
|
213 | + * Analogous to EE_Registry::instance()->non_abstract_db_models |
|
214 | + * |
|
215 | + * @return array keys are model names, values are their classname |
|
216 | + */ |
|
217 | + public function models_for_requested_version() |
|
218 | + { |
|
219 | + if ($this->_cached_models_for_requested_version === null) { |
|
220 | + $all_models_in_current_version = \EE_Registry::instance()->non_abstract_db_models; |
|
221 | + foreach ($this->model_changes_between_requested_version_and_current() as $version => $models_changed) { |
|
222 | + foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
223 | + if ($new_indicator_or_fields_added === Model_Version_Info::model_added) { |
|
224 | + unset($all_models_in_current_version[$model_name]); |
|
225 | + } |
|
226 | + } |
|
227 | + } |
|
228 | + $this->_cached_models_for_requested_version = apply_filters( |
|
229 | + 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
230 | + $all_models_in_current_version, |
|
231 | + $this |
|
232 | + ); |
|
233 | + } |
|
234 | + return $this->_cached_models_for_requested_version; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * Determines if this is a valid model name in the requested version. |
|
241 | + * Similar to EE_Registry::instance()->is_model_name(), but takes the requested |
|
242 | + * version's models into account |
|
243 | + * |
|
244 | + * @param string $model_name eg 'Event' |
|
245 | + * @return boolean |
|
246 | + */ |
|
247 | + public function is_model_name_in_this_version($model_name) |
|
248 | + { |
|
249 | + $model_names = $this->models_for_requested_version(); |
|
250 | + if (isset($model_names[$model_name])) { |
|
251 | + return true; |
|
252 | + } else { |
|
253 | + return false; |
|
254 | + } |
|
255 | + } |
|
256 | + |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Wrapper for EE_Registry::instance()->load_model(), but takes the requested |
|
261 | + * version's models into account |
|
262 | + * |
|
263 | + * @param string $model_name |
|
264 | + * @return \EEM_Base |
|
265 | + * @throws \EE_Error |
|
266 | + */ |
|
267 | + public function load_model($model_name) |
|
268 | + { |
|
269 | + if ($this->is_model_name_in_this_version($model_name)) { |
|
270 | + return \EE_Registry::instance()->load_model($model_name); |
|
271 | + } else { |
|
272 | + throw new \EE_Error( |
|
273 | + sprintf( |
|
274 | + __( |
|
275 | + 'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso', |
|
276 | + 'event_espresso' |
|
277 | + ), |
|
278 | + $model_name, |
|
279 | + $this->requested_version() |
|
280 | + ) |
|
281 | + ); |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + |
|
287 | + /** |
|
288 | + * Gets all the fields that should exist on this model right now |
|
289 | + * |
|
290 | + * @param \EEM_Base $model |
|
291 | + * @return array|\EE_Model_Field_Base[] |
|
292 | + */ |
|
293 | + public function fields_on_model_in_this_version($model) |
|
294 | + { |
|
295 | + if (! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
296 | + //get all model changes between the requested version and current core version |
|
297 | + $changes = $this->model_changes_between_requested_version_and_current(); |
|
298 | + //fetch all fields currently on this model |
|
299 | + $current_fields = $model->field_settings(); |
|
300 | + //remove all fields that have been added since |
|
301 | + foreach ($changes as $version => $changes_in_version) { |
|
302 | + if ( |
|
303 | + isset($changes_in_version[$model->get_this_model_name()]) |
|
304 | + && $changes_in_version[$model->get_this_model_name()] !== Model_Version_Info::model_added |
|
305 | + ) { |
|
306 | + $current_fields = array_diff_key( |
|
307 | + $current_fields, |
|
308 | + array_flip($changes_in_version[$model->get_this_model_name()]) |
|
309 | + ); |
|
310 | + } |
|
311 | + } |
|
312 | + $this->_cached_fields_on_models = $current_fields; |
|
313 | + } |
|
314 | + return $this->_cached_fields_on_models; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Determines if $object is of one of the classes of $classes. Similar to |
|
321 | + * in_array(), except this checks if $object is a subclass of the classnames provided |
|
322 | + * in $classnames |
|
323 | + * |
|
324 | + * @param object $object |
|
325 | + * @param array $classnames |
|
326 | + * @return boolean |
|
327 | + */ |
|
328 | + public function is_subclass_of_one($object, $classnames) |
|
329 | + { |
|
330 | + foreach ($classnames as $classname) { |
|
331 | + if (is_a($object, $classname)) { |
|
332 | + return true; |
|
333 | + } |
|
334 | + } |
|
335 | + return false; |
|
336 | + } |
|
337 | + |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * Returns the list of model field classes that that the API basically ignores |
|
342 | + * |
|
343 | + * @return array |
|
344 | + */ |
|
345 | + public function fields_ignored() |
|
346 | + { |
|
347 | + return apply_filters( |
|
348 | + 'FHEE__Controller_Model_Read_fields_ignored', |
|
349 | + array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
350 | + ); |
|
351 | + } |
|
352 | + |
|
353 | + |
|
354 | + |
|
355 | + /** |
|
356 | + * If this field one that should be ignored by the API? |
|
357 | + * |
|
358 | + * @param EE_Model_Field_Base |
|
359 | + * @return boolean |
|
360 | + */ |
|
361 | + public function field_is_ignored($field_obj) |
|
362 | + { |
|
363 | + return $this->is_subclass_of_one($field_obj, $this->fields_ignored()); |
|
364 | + } |
|
365 | + |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * Returns the list of model field classes that have a "raw" and non-raw formats. |
|
370 | + * Normally the "raw" versions are only accessible to those who can edit them. |
|
371 | + * |
|
372 | + * @return array an array of EE_Model_Field_Base child classnames |
|
373 | + */ |
|
374 | + public function fields_that_have_rendered_format() |
|
375 | + { |
|
376 | + return apply_filters( |
|
377 | + 'FHEE__Controller_Model_Read__fields_raw', |
|
378 | + array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
379 | + ); |
|
380 | + } |
|
381 | + |
|
382 | + |
|
383 | + |
|
384 | + /** |
|
385 | + * If this field one that has a raw format |
|
386 | + * |
|
387 | + * @param EE_Model_Field_Base |
|
388 | + * @return boolean |
|
389 | + */ |
|
390 | + public function field_has_rendered_format($field_obj) |
|
391 | + { |
|
392 | + return $this->is_subclass_of_one($field_obj, $this->fields_that_have_rendered_format()); |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + |
|
397 | + /** |
|
398 | + * Returns the list of model field classes that have a "_pretty" and non-pretty versions. |
|
399 | + * The pretty version of the field is NOT queryable or editable, but requires no extra permissions |
|
400 | + * to view |
|
401 | + * |
|
402 | + * @return array an array of EE_Model_Field_Base child classnames |
|
403 | + */ |
|
404 | + public function fields_that_have_pretty_format() |
|
405 | + { |
|
406 | + return apply_filters( |
|
407 | + 'FHEE__Controller_Model_Read__fields_pretty', |
|
408 | + array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
409 | + ); |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + |
|
414 | + /** |
|
415 | + * If this field one that has a pretty equivalent |
|
416 | + * |
|
417 | + * @param EE_Model_Field_Base |
|
418 | + * @return boolean |
|
419 | + */ |
|
420 | + public function field_has_pretty_format($field_obj) |
|
421 | + { |
|
422 | + return $this->is_subclass_of_one($field_obj, $this->fields_that_have_pretty_format()); |
|
423 | + } |
|
424 | + |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * Returns an array describing what extra API resource properties have been added through the versions |
|
429 | + * |
|
430 | + * @return array @see $this->_extra_resource_properties_for_models |
|
431 | + */ |
|
432 | + public function resource_changes() |
|
433 | + { |
|
434 | + return $this->_resource_changes; |
|
435 | + } |
|
436 | + |
|
437 | + |
|
438 | + |
|
439 | + /** |
|
440 | + * Returns an array where keys are extra resource properties in this version of the API, |
|
441 | + * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes |
|
442 | + * |
|
443 | + * @param \EEM_Base $model |
|
444 | + * @return array |
|
445 | + */ |
|
446 | + public function extra_resource_properties_for_model($model) |
|
447 | + { |
|
448 | + $extra_properties = array(); |
|
449 | + foreach ($this->resource_changes_between_requested_version_and_current() as $version => $model_classnames) { |
|
450 | + foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
451 | + if (is_subclass_of($model, $model_classname)) { |
|
452 | + $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
453 | + } |
|
454 | + } |
|
455 | + } |
|
456 | + return $extra_properties; |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + |
|
461 | + /** |
|
462 | + * Gets all the related models for the specified model. It's good to use this |
|
463 | + * in case this model didn't exist for this version or something |
|
464 | + * |
|
465 | + * @param \EEM_Base $model |
|
466 | + * @return \EE_Model_Relation_Base[] |
|
467 | + */ |
|
468 | + public function relation_settings(\EEM_Base $model) |
|
469 | + { |
|
470 | + $relations = array(); |
|
471 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
472 | + if ($this->is_model_name_in_this_version($relation_name)) { |
|
473 | + $relations[$relation_name] = $relation_obj; |
|
474 | + } |
|
475 | + } |
|
476 | + //filter the results, but use the old filter name |
|
477 | + return apply_filters( |
|
478 | + 'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', |
|
479 | + $relations, |
|
480 | + $model |
|
481 | + ); |
|
482 | + } |
|
483 | 483 | |
484 | 484 | } |
485 | 485 |
@@ -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 | |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | foreach ($extra_fields as $fieldname => $field_data) { |
121 | 121 | $this->_resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
122 | 122 | foreach ($defaults as $attribute => $default_value) { |
123 | - if (! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
123 | + if ( ! isset($this->_resource_changes[$model_classname][$fieldname][$attribute])) { |
|
124 | 124 | $this->_resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
125 | 125 | } |
126 | 126 | } |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | */ |
293 | 293 | public function fields_on_model_in_this_version($model) |
294 | 294 | { |
295 | - if (! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
295 | + if ( ! isset($this->_cached_fields_on_models[$model->get_this_model_name()])) { |
|
296 | 296 | //get all model changes between the requested version and current core version |
297 | 297 | $changes = $this->model_changes_between_requested_version_and_current(); |
298 | 298 | //fetch all fields currently on this model |
@@ -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 | |
@@ -27,485 +27,485 @@ discard block |
||
27 | 27 | class Model_Data_Translator |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
32 | - * fields that COULD contain -1; so we use null |
|
33 | - */ |
|
34 | - const ee_inf_in_rest = null; |
|
35 | - |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * Prepares a possible array of input values from JSON for use by the models |
|
40 | - * |
|
41 | - * @param \EE_Model_Field_Base $field_obj |
|
42 | - * @param mixed $original_value_maybe_array |
|
43 | - * @param string $requested_version |
|
44 | - * @param string $timezone_string treat values as being in this timezone |
|
45 | - * @return mixed |
|
46 | - * @throws \DomainException |
|
47 | - */ |
|
48 | - public static function prepare_field_values_from_json( |
|
49 | - $field_obj, |
|
50 | - $original_value_maybe_array, |
|
51 | - $requested_version, |
|
52 | - $timezone_string = 'UTC' |
|
53 | - ) { |
|
54 | - if (is_array($original_value_maybe_array)) { |
|
55 | - $new_value_maybe_array = array(); |
|
56 | - foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
57 | - $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_from_json( |
|
58 | - $field_obj, |
|
59 | - $array_item, |
|
60 | - $requested_version, |
|
61 | - $timezone_string |
|
62 | - ); |
|
63 | - } |
|
64 | - } else { |
|
65 | - $new_value_maybe_array = Model_Data_Translator::prepare_field_value_from_json( |
|
66 | - $field_obj, |
|
67 | - $original_value_maybe_array, |
|
68 | - $requested_version, |
|
69 | - $timezone_string |
|
70 | - ); |
|
71 | - } |
|
72 | - return $new_value_maybe_array; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Prepares an array of field values FOR use in JSON/REST API |
|
79 | - * |
|
80 | - * @param \EE_Model_Field_Base $field_obj |
|
81 | - * @param mixed $original_value_maybe_array |
|
82 | - * @param string $request_version (eg 4.8.36) |
|
83 | - * @return array |
|
84 | - */ |
|
85 | - public static function prepare_field_values_for_json($field_obj, $original_value_maybe_array, $request_version) |
|
86 | - { |
|
87 | - if (is_array($original_value_maybe_array)) { |
|
88 | - $new_value_maybe_array = array(); |
|
89 | - foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
90 | - $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
91 | - $field_obj, |
|
92 | - $array_item, |
|
93 | - $request_version |
|
94 | - ); |
|
95 | - } |
|
96 | - } else { |
|
97 | - $new_value_maybe_array = Model_Data_Translator::prepare_field_value_for_json( |
|
98 | - $field_obj, |
|
99 | - $original_value_maybe_array, |
|
100 | - $request_version |
|
101 | - ); |
|
102 | - } |
|
103 | - return $new_value_maybe_array; |
|
104 | - } |
|
105 | - |
|
106 | - |
|
107 | - |
|
108 | - /** |
|
109 | - * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
110 | - * "$query_params". |
|
111 | - * |
|
112 | - * @param \EE_Model_Field_Base $field_obj |
|
113 | - * @param mixed $original_value |
|
114 | - * @param string $requested_version |
|
115 | - * @param string $timezone_string treat values as being in this timezone |
|
116 | - * @return mixed |
|
117 | - * @throws \DomainException |
|
118 | - */ |
|
119 | - public static function prepare_field_value_from_json( |
|
120 | - $field_obj, |
|
121 | - $original_value, |
|
122 | - $requested_version, |
|
123 | - $timezone_string = 'UTC' // UTC |
|
124 | - ) |
|
125 | - { |
|
126 | - $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
127 | - $new_value = null; |
|
128 | - if ($field_obj instanceof \EE_Infinite_Integer_Field |
|
129 | - && in_array($original_value, array(null, ''), true) |
|
130 | - ) { |
|
131 | - $new_value = EE_INF; |
|
132 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
133 | - list($offset_sign, $offset_secs) = Model_Data_Translator::parse_timezone_offset( |
|
134 | - $field_obj->get_timezone_offset( |
|
135 | - new \DateTimeZone($timezone_string) |
|
136 | - ) |
|
137 | - ); |
|
138 | - $offset_string = |
|
139 | - str_pad( |
|
140 | - floor($offset_secs / HOUR_IN_SECONDS), |
|
141 | - 2, |
|
142 | - '0', |
|
143 | - STR_PAD_LEFT |
|
144 | - ) |
|
145 | - . ':' |
|
146 | - . str_pad( |
|
147 | - ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
148 | - 2, |
|
149 | - '0', |
|
150 | - STR_PAD_LEFT |
|
151 | - ); |
|
152 | - $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
153 | - } else { |
|
154 | - $new_value = $original_value; |
|
155 | - } |
|
156 | - return $new_value; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * determines what's going on with them timezone strings |
|
163 | - * |
|
164 | - * @param int $timezone_offset |
|
165 | - * @return array |
|
166 | - */ |
|
167 | - private static function parse_timezone_offset($timezone_offset) |
|
168 | - { |
|
169 | - $first_char = substr((string)$timezone_offset, 0, 1); |
|
170 | - if ($first_char === '+' || $first_char === '-') { |
|
171 | - $offset_sign = $first_char; |
|
172 | - $offset_secs = substr((string)$timezone_offset, 1); |
|
173 | - } else { |
|
174 | - $offset_sign = '+'; |
|
175 | - $offset_secs = $timezone_offset; |
|
176 | - } |
|
177 | - return array($offset_sign, $offset_secs); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - |
|
182 | - /** |
|
183 | - * Prepares a field's value for display in the API |
|
184 | - * |
|
185 | - * @param \EE_Model_Field_Base $field_obj |
|
186 | - * @param mixed $original_value |
|
187 | - * @param string $requested_version |
|
188 | - * @return mixed |
|
189 | - */ |
|
190 | - public static function prepare_field_value_for_json($field_obj, $original_value, $requested_version) |
|
191 | - { |
|
192 | - if ($original_value === EE_INF) { |
|
193 | - $new_value = Model_Data_Translator::ee_inf_in_rest; |
|
194 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
195 | - if ($original_value instanceof \DateTime) { |
|
196 | - $new_value = $original_value->format('Y-m-d H:i:s'); |
|
197 | - } elseif (is_int($original_value)) { |
|
198 | - $new_value = date('Y-m-d H:i:s', $original_value); |
|
199 | - } else { |
|
200 | - $new_value = $original_value; |
|
201 | - } |
|
202 | - $new_value = mysql_to_rfc3339($new_value); |
|
203 | - } else { |
|
204 | - $new_value = $original_value; |
|
205 | - } |
|
206 | - return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
207 | - $new_value, |
|
208 | - $field_obj, |
|
209 | - $original_value, |
|
210 | - $requested_version |
|
211 | - ); |
|
212 | - } |
|
213 | - |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * Prepares condition-query-parameters (like what's in where and having) from |
|
218 | - * the format expected in the API to use in the models |
|
219 | - * |
|
220 | - * @param array $inputted_query_params_of_this_type |
|
221 | - * @param \EEM_Base $model |
|
222 | - * @param string $requested_version |
|
223 | - * @return array |
|
224 | - * @throws \DomainException |
|
225 | - * @throws \EE_Error |
|
226 | - */ |
|
227 | - public static function prepare_conditions_query_params_for_models( |
|
228 | - $inputted_query_params_of_this_type, |
|
229 | - \EEM_Base $model, |
|
230 | - $requested_version |
|
231 | - ) { |
|
232 | - $query_param_for_models = array(); |
|
233 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
234 | - $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key); |
|
235 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
236 | - $query_param_sans_stars, |
|
237 | - $model |
|
238 | - ); |
|
239 | - //double-check is it a *_gmt field? |
|
240 | - if (! $field instanceof \EE_Model_Field_Base |
|
241 | - && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
|
242 | - ) { |
|
243 | - //yep, take off '_gmt', and find the field |
|
244 | - $query_param_key = Model_Data_Translator::remove_gmt_from_field_name($query_param_sans_stars); |
|
245 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
246 | - $query_param_key, |
|
247 | - $model |
|
248 | - ); |
|
249 | - $timezone = 'UTC'; |
|
250 | - } else { |
|
251 | - //so it's not a GMT field. Set the timezone on the model to the default |
|
252 | - $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
253 | - } |
|
254 | - if ($field instanceof \EE_Model_Field_Base) { |
|
255 | - //did they specify an operator? |
|
256 | - if (is_array($query_param_value)) { |
|
257 | - $op = $query_param_value[0]; |
|
258 | - $translated_value = array($op); |
|
259 | - if (isset($query_param_value[1])) { |
|
260 | - $value = $query_param_value[1]; |
|
261 | - $translated_value[1] = Model_Data_Translator::prepare_field_values_from_json($field, $value, |
|
262 | - $requested_version, $timezone); |
|
263 | - } |
|
264 | - } else { |
|
265 | - $translated_value = Model_Data_Translator::prepare_field_value_from_json($field, $query_param_value, |
|
266 | - $requested_version, $timezone); |
|
267 | - } |
|
268 | - $query_param_for_models[$query_param_key] = $translated_value; |
|
269 | - } else { |
|
270 | - //so it's not for a field, assume it's a logic query param key |
|
271 | - $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_models($query_param_value, |
|
272 | - $model, $requested_version); |
|
273 | - } |
|
274 | - } |
|
275 | - return $query_param_for_models; |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
282 | - * gmt date field name |
|
283 | - * |
|
284 | - * @param string $field_name |
|
285 | - * @return boolean |
|
286 | - */ |
|
287 | - public static function is_gmt_date_field_name($field_name) |
|
288 | - { |
|
289 | - return substr( |
|
290 | - Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name), |
|
291 | - -4, |
|
292 | - 4 |
|
293 | - ) === '_gmt'; |
|
294 | - } |
|
295 | - |
|
296 | - |
|
297 | - |
|
298 | - /** |
|
299 | - * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
300 | - * |
|
301 | - * @param string $field_name |
|
302 | - * @return string |
|
303 | - */ |
|
304 | - public static function remove_gmt_from_field_name($field_name) |
|
305 | - { |
|
306 | - if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
307 | - return $field_name; |
|
308 | - } |
|
309 | - $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |
|
310 | - $query_param_sans_gmt_and_sans_stars = substr( |
|
311 | - $query_param_sans_stars, |
|
312 | - 0, |
|
313 | - strrpos( |
|
314 | - $field_name, |
|
315 | - '_gmt' |
|
316 | - ) |
|
317 | - ); |
|
318 | - return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - |
|
323 | - /** |
|
324 | - * Takes a field name from the REST API and prepares it for the model querying |
|
325 | - * |
|
326 | - * @param string $field_name |
|
327 | - * @return string |
|
328 | - */ |
|
329 | - public static function prepare_field_name_from_json($field_name) |
|
330 | - { |
|
331 | - if (Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
332 | - return Model_Data_Translator::remove_gmt_from_field_name($field_name); |
|
333 | - } |
|
334 | - return $field_name; |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - |
|
339 | - /** |
|
340 | - * Takes array of field names from REST API and prepares for models |
|
341 | - * |
|
342 | - * @param array $field_names |
|
343 | - * @return array of field names (possibly include model prefixes) |
|
344 | - */ |
|
345 | - public static function prepare_field_names_from_json(array $field_names) |
|
346 | - { |
|
347 | - $new_array = array(); |
|
348 | - foreach ($field_names as $key => $field_name) { |
|
349 | - $new_array[$key] = Model_Data_Translator::prepare_field_name_from_json($field_name); |
|
350 | - } |
|
351 | - return $new_array; |
|
352 | - } |
|
353 | - |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Takes array where array keys are field names (possibly with model path prefixes) |
|
358 | - * from the REST API and prepares them for model querying |
|
359 | - * |
|
360 | - * @param array $field_names_as_keys |
|
361 | - * @return array |
|
362 | - */ |
|
363 | - public static function prepare_field_names_in_array_keys_from_json(array $field_names_as_keys) |
|
364 | - { |
|
365 | - $new_array = array(); |
|
366 | - foreach ($field_names_as_keys as $field_name => $value) { |
|
367 | - $new_array[Model_Data_Translator::prepare_field_name_from_json($field_name)] = $value; |
|
368 | - } |
|
369 | - return $new_array; |
|
370 | - } |
|
371 | - |
|
372 | - |
|
373 | - |
|
374 | - /** |
|
375 | - * Prepares an array of model query params for use in the REST API |
|
376 | - * |
|
377 | - * @param array $model_query_params |
|
378 | - * @param \EEM_Base $model |
|
379 | - * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
380 | - * REST API |
|
381 | - * @return array which can be passed into the EE4 REST API when querying a model resource |
|
382 | - * @throws \EE_Error |
|
383 | - */ |
|
384 | - public static function prepare_query_params_for_rest_api( |
|
385 | - array $model_query_params, |
|
386 | - \EEM_Base $model, |
|
387 | - $requested_version = null |
|
388 | - ) { |
|
389 | - if ($requested_version === null) { |
|
390 | - $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
391 | - } |
|
392 | - $rest_query_params = $model_query_params; |
|
393 | - if (isset($model_query_params[0])) { |
|
394 | - $rest_query_params['where'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
395 | - $model_query_params[0], |
|
396 | - $model, |
|
397 | - $requested_version |
|
398 | - ); |
|
399 | - unset($rest_query_params[0]); |
|
400 | - } |
|
401 | - if (isset($model_query_params['having'])) { |
|
402 | - $rest_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
403 | - $model_query_params['having'], |
|
404 | - $model, |
|
405 | - $requested_version |
|
406 | - ); |
|
407 | - } |
|
408 | - return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
409 | - $rest_query_params, $model_query_params, $model, $requested_version); |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - |
|
414 | - /** |
|
415 | - * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
416 | - * |
|
417 | - * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
418 | - * passed into EEM_Base::get_all() |
|
419 | - * @param \EEM_Base $model |
|
420 | - * @param string $requested_version eg "4.8.36" |
|
421 | - * @return array ready for use in the rest api query params |
|
422 | - * @throws \EE_Error |
|
423 | - */ |
|
424 | - public static function prepare_conditions_query_params_for_rest_api( |
|
425 | - $inputted_query_params_of_this_type, |
|
426 | - \EEM_Base $model, |
|
427 | - $requested_version |
|
428 | - ) { |
|
429 | - $query_param_for_models = array(); |
|
430 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
431 | - $field = Model_Data_Translator::deduce_field_from_query_param( |
|
432 | - Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key), |
|
433 | - $model |
|
434 | - ); |
|
435 | - if ($field instanceof \EE_Model_Field_Base) { |
|
436 | - //did they specify an operator? |
|
437 | - if (is_array($query_param_value)) { |
|
438 | - $op = $query_param_value[0]; |
|
439 | - $translated_value = array($op); |
|
440 | - if (isset($query_param_value[1])) { |
|
441 | - $value = $query_param_value[1]; |
|
442 | - $translated_value[1] = Model_Data_Translator::prepare_field_values_for_json($field, $value, |
|
443 | - $requested_version); |
|
444 | - } |
|
445 | - } else { |
|
446 | - $translated_value = Model_Data_Translator::prepare_field_value_for_json($field, $query_param_value, |
|
447 | - $requested_version); |
|
448 | - } |
|
449 | - $query_param_for_models[$query_param_key] = $translated_value; |
|
450 | - } else { |
|
451 | - //so it's not for a field, assume it's a logic query param key |
|
452 | - $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api($query_param_value, |
|
453 | - $model, $requested_version); |
|
454 | - } |
|
455 | - } |
|
456 | - return $query_param_for_models; |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * @param $condition_query_param_key |
|
463 | - * @return string |
|
464 | - */ |
|
465 | - public static function remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
466 | - { |
|
467 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
468 | - if ($pos_of_star === false) { |
|
469 | - return $condition_query_param_key; |
|
470 | - } else { |
|
471 | - $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
472 | - return $condition_query_param_sans_star; |
|
473 | - } |
|
474 | - } |
|
475 | - |
|
476 | - |
|
477 | - |
|
478 | - /** |
|
479 | - * Takes the input parameter and finds the model field that it indicates. |
|
480 | - * |
|
481 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
482 | - * @param \EEM_Base $model |
|
483 | - * @return \EE_Model_Field_Base |
|
484 | - * @throws \EE_Error |
|
485 | - */ |
|
486 | - public static function deduce_field_from_query_param($query_param_name, \EEM_Base $model) |
|
487 | - { |
|
488 | - //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
489 | - //which will help us find the database table and column |
|
490 | - $query_param_parts = explode('.', $query_param_name); |
|
491 | - if (empty($query_param_parts)) { |
|
492 | - throw new \EE_Error(sprintf(__('_extract_column_name is empty when trying to extract column and table name from %s', |
|
493 | - 'event_espresso'), $query_param_name)); |
|
494 | - } |
|
495 | - $number_of_parts = count($query_param_parts); |
|
496 | - $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
497 | - if ($number_of_parts === 1) { |
|
498 | - $field_name = $last_query_param_part; |
|
499 | - } else {// $number_of_parts >= 2 |
|
500 | - //the last part is the column name, and there are only 2parts. therefore... |
|
501 | - $field_name = $last_query_param_part; |
|
502 | - $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
503 | - } |
|
504 | - try { |
|
505 | - return $model->field_settings_for($field_name); |
|
506 | - } catch (\EE_Error $e) { |
|
507 | - return null; |
|
508 | - } |
|
509 | - } |
|
30 | + /** |
|
31 | + * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
32 | + * fields that COULD contain -1; so we use null |
|
33 | + */ |
|
34 | + const ee_inf_in_rest = null; |
|
35 | + |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * Prepares a possible array of input values from JSON for use by the models |
|
40 | + * |
|
41 | + * @param \EE_Model_Field_Base $field_obj |
|
42 | + * @param mixed $original_value_maybe_array |
|
43 | + * @param string $requested_version |
|
44 | + * @param string $timezone_string treat values as being in this timezone |
|
45 | + * @return mixed |
|
46 | + * @throws \DomainException |
|
47 | + */ |
|
48 | + public static function prepare_field_values_from_json( |
|
49 | + $field_obj, |
|
50 | + $original_value_maybe_array, |
|
51 | + $requested_version, |
|
52 | + $timezone_string = 'UTC' |
|
53 | + ) { |
|
54 | + if (is_array($original_value_maybe_array)) { |
|
55 | + $new_value_maybe_array = array(); |
|
56 | + foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
57 | + $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_from_json( |
|
58 | + $field_obj, |
|
59 | + $array_item, |
|
60 | + $requested_version, |
|
61 | + $timezone_string |
|
62 | + ); |
|
63 | + } |
|
64 | + } else { |
|
65 | + $new_value_maybe_array = Model_Data_Translator::prepare_field_value_from_json( |
|
66 | + $field_obj, |
|
67 | + $original_value_maybe_array, |
|
68 | + $requested_version, |
|
69 | + $timezone_string |
|
70 | + ); |
|
71 | + } |
|
72 | + return $new_value_maybe_array; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Prepares an array of field values FOR use in JSON/REST API |
|
79 | + * |
|
80 | + * @param \EE_Model_Field_Base $field_obj |
|
81 | + * @param mixed $original_value_maybe_array |
|
82 | + * @param string $request_version (eg 4.8.36) |
|
83 | + * @return array |
|
84 | + */ |
|
85 | + public static function prepare_field_values_for_json($field_obj, $original_value_maybe_array, $request_version) |
|
86 | + { |
|
87 | + if (is_array($original_value_maybe_array)) { |
|
88 | + $new_value_maybe_array = array(); |
|
89 | + foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
90 | + $new_value_maybe_array[$array_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
91 | + $field_obj, |
|
92 | + $array_item, |
|
93 | + $request_version |
|
94 | + ); |
|
95 | + } |
|
96 | + } else { |
|
97 | + $new_value_maybe_array = Model_Data_Translator::prepare_field_value_for_json( |
|
98 | + $field_obj, |
|
99 | + $original_value_maybe_array, |
|
100 | + $request_version |
|
101 | + ); |
|
102 | + } |
|
103 | + return $new_value_maybe_array; |
|
104 | + } |
|
105 | + |
|
106 | + |
|
107 | + |
|
108 | + /** |
|
109 | + * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
110 | + * "$query_params". |
|
111 | + * |
|
112 | + * @param \EE_Model_Field_Base $field_obj |
|
113 | + * @param mixed $original_value |
|
114 | + * @param string $requested_version |
|
115 | + * @param string $timezone_string treat values as being in this timezone |
|
116 | + * @return mixed |
|
117 | + * @throws \DomainException |
|
118 | + */ |
|
119 | + public static function prepare_field_value_from_json( |
|
120 | + $field_obj, |
|
121 | + $original_value, |
|
122 | + $requested_version, |
|
123 | + $timezone_string = 'UTC' // UTC |
|
124 | + ) |
|
125 | + { |
|
126 | + $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
127 | + $new_value = null; |
|
128 | + if ($field_obj instanceof \EE_Infinite_Integer_Field |
|
129 | + && in_array($original_value, array(null, ''), true) |
|
130 | + ) { |
|
131 | + $new_value = EE_INF; |
|
132 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
133 | + list($offset_sign, $offset_secs) = Model_Data_Translator::parse_timezone_offset( |
|
134 | + $field_obj->get_timezone_offset( |
|
135 | + new \DateTimeZone($timezone_string) |
|
136 | + ) |
|
137 | + ); |
|
138 | + $offset_string = |
|
139 | + str_pad( |
|
140 | + floor($offset_secs / HOUR_IN_SECONDS), |
|
141 | + 2, |
|
142 | + '0', |
|
143 | + STR_PAD_LEFT |
|
144 | + ) |
|
145 | + . ':' |
|
146 | + . str_pad( |
|
147 | + ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
148 | + 2, |
|
149 | + '0', |
|
150 | + STR_PAD_LEFT |
|
151 | + ); |
|
152 | + $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
153 | + } else { |
|
154 | + $new_value = $original_value; |
|
155 | + } |
|
156 | + return $new_value; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * determines what's going on with them timezone strings |
|
163 | + * |
|
164 | + * @param int $timezone_offset |
|
165 | + * @return array |
|
166 | + */ |
|
167 | + private static function parse_timezone_offset($timezone_offset) |
|
168 | + { |
|
169 | + $first_char = substr((string)$timezone_offset, 0, 1); |
|
170 | + if ($first_char === '+' || $first_char === '-') { |
|
171 | + $offset_sign = $first_char; |
|
172 | + $offset_secs = substr((string)$timezone_offset, 1); |
|
173 | + } else { |
|
174 | + $offset_sign = '+'; |
|
175 | + $offset_secs = $timezone_offset; |
|
176 | + } |
|
177 | + return array($offset_sign, $offset_secs); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + |
|
182 | + /** |
|
183 | + * Prepares a field's value for display in the API |
|
184 | + * |
|
185 | + * @param \EE_Model_Field_Base $field_obj |
|
186 | + * @param mixed $original_value |
|
187 | + * @param string $requested_version |
|
188 | + * @return mixed |
|
189 | + */ |
|
190 | + public static function prepare_field_value_for_json($field_obj, $original_value, $requested_version) |
|
191 | + { |
|
192 | + if ($original_value === EE_INF) { |
|
193 | + $new_value = Model_Data_Translator::ee_inf_in_rest; |
|
194 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
195 | + if ($original_value instanceof \DateTime) { |
|
196 | + $new_value = $original_value->format('Y-m-d H:i:s'); |
|
197 | + } elseif (is_int($original_value)) { |
|
198 | + $new_value = date('Y-m-d H:i:s', $original_value); |
|
199 | + } else { |
|
200 | + $new_value = $original_value; |
|
201 | + } |
|
202 | + $new_value = mysql_to_rfc3339($new_value); |
|
203 | + } else { |
|
204 | + $new_value = $original_value; |
|
205 | + } |
|
206 | + return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
207 | + $new_value, |
|
208 | + $field_obj, |
|
209 | + $original_value, |
|
210 | + $requested_version |
|
211 | + ); |
|
212 | + } |
|
213 | + |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * Prepares condition-query-parameters (like what's in where and having) from |
|
218 | + * the format expected in the API to use in the models |
|
219 | + * |
|
220 | + * @param array $inputted_query_params_of_this_type |
|
221 | + * @param \EEM_Base $model |
|
222 | + * @param string $requested_version |
|
223 | + * @return array |
|
224 | + * @throws \DomainException |
|
225 | + * @throws \EE_Error |
|
226 | + */ |
|
227 | + public static function prepare_conditions_query_params_for_models( |
|
228 | + $inputted_query_params_of_this_type, |
|
229 | + \EEM_Base $model, |
|
230 | + $requested_version |
|
231 | + ) { |
|
232 | + $query_param_for_models = array(); |
|
233 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
234 | + $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key); |
|
235 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
236 | + $query_param_sans_stars, |
|
237 | + $model |
|
238 | + ); |
|
239 | + //double-check is it a *_gmt field? |
|
240 | + if (! $field instanceof \EE_Model_Field_Base |
|
241 | + && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
|
242 | + ) { |
|
243 | + //yep, take off '_gmt', and find the field |
|
244 | + $query_param_key = Model_Data_Translator::remove_gmt_from_field_name($query_param_sans_stars); |
|
245 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
246 | + $query_param_key, |
|
247 | + $model |
|
248 | + ); |
|
249 | + $timezone = 'UTC'; |
|
250 | + } else { |
|
251 | + //so it's not a GMT field. Set the timezone on the model to the default |
|
252 | + $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
253 | + } |
|
254 | + if ($field instanceof \EE_Model_Field_Base) { |
|
255 | + //did they specify an operator? |
|
256 | + if (is_array($query_param_value)) { |
|
257 | + $op = $query_param_value[0]; |
|
258 | + $translated_value = array($op); |
|
259 | + if (isset($query_param_value[1])) { |
|
260 | + $value = $query_param_value[1]; |
|
261 | + $translated_value[1] = Model_Data_Translator::prepare_field_values_from_json($field, $value, |
|
262 | + $requested_version, $timezone); |
|
263 | + } |
|
264 | + } else { |
|
265 | + $translated_value = Model_Data_Translator::prepare_field_value_from_json($field, $query_param_value, |
|
266 | + $requested_version, $timezone); |
|
267 | + } |
|
268 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
269 | + } else { |
|
270 | + //so it's not for a field, assume it's a logic query param key |
|
271 | + $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_models($query_param_value, |
|
272 | + $model, $requested_version); |
|
273 | + } |
|
274 | + } |
|
275 | + return $query_param_for_models; |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
282 | + * gmt date field name |
|
283 | + * |
|
284 | + * @param string $field_name |
|
285 | + * @return boolean |
|
286 | + */ |
|
287 | + public static function is_gmt_date_field_name($field_name) |
|
288 | + { |
|
289 | + return substr( |
|
290 | + Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name), |
|
291 | + -4, |
|
292 | + 4 |
|
293 | + ) === '_gmt'; |
|
294 | + } |
|
295 | + |
|
296 | + |
|
297 | + |
|
298 | + /** |
|
299 | + * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
300 | + * |
|
301 | + * @param string $field_name |
|
302 | + * @return string |
|
303 | + */ |
|
304 | + public static function remove_gmt_from_field_name($field_name) |
|
305 | + { |
|
306 | + if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
307 | + return $field_name; |
|
308 | + } |
|
309 | + $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |
|
310 | + $query_param_sans_gmt_and_sans_stars = substr( |
|
311 | + $query_param_sans_stars, |
|
312 | + 0, |
|
313 | + strrpos( |
|
314 | + $field_name, |
|
315 | + '_gmt' |
|
316 | + ) |
|
317 | + ); |
|
318 | + return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + |
|
323 | + /** |
|
324 | + * Takes a field name from the REST API and prepares it for the model querying |
|
325 | + * |
|
326 | + * @param string $field_name |
|
327 | + * @return string |
|
328 | + */ |
|
329 | + public static function prepare_field_name_from_json($field_name) |
|
330 | + { |
|
331 | + if (Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
332 | + return Model_Data_Translator::remove_gmt_from_field_name($field_name); |
|
333 | + } |
|
334 | + return $field_name; |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + |
|
339 | + /** |
|
340 | + * Takes array of field names from REST API and prepares for models |
|
341 | + * |
|
342 | + * @param array $field_names |
|
343 | + * @return array of field names (possibly include model prefixes) |
|
344 | + */ |
|
345 | + public static function prepare_field_names_from_json(array $field_names) |
|
346 | + { |
|
347 | + $new_array = array(); |
|
348 | + foreach ($field_names as $key => $field_name) { |
|
349 | + $new_array[$key] = Model_Data_Translator::prepare_field_name_from_json($field_name); |
|
350 | + } |
|
351 | + return $new_array; |
|
352 | + } |
|
353 | + |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Takes array where array keys are field names (possibly with model path prefixes) |
|
358 | + * from the REST API and prepares them for model querying |
|
359 | + * |
|
360 | + * @param array $field_names_as_keys |
|
361 | + * @return array |
|
362 | + */ |
|
363 | + public static function prepare_field_names_in_array_keys_from_json(array $field_names_as_keys) |
|
364 | + { |
|
365 | + $new_array = array(); |
|
366 | + foreach ($field_names_as_keys as $field_name => $value) { |
|
367 | + $new_array[Model_Data_Translator::prepare_field_name_from_json($field_name)] = $value; |
|
368 | + } |
|
369 | + return $new_array; |
|
370 | + } |
|
371 | + |
|
372 | + |
|
373 | + |
|
374 | + /** |
|
375 | + * Prepares an array of model query params for use in the REST API |
|
376 | + * |
|
377 | + * @param array $model_query_params |
|
378 | + * @param \EEM_Base $model |
|
379 | + * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
380 | + * REST API |
|
381 | + * @return array which can be passed into the EE4 REST API when querying a model resource |
|
382 | + * @throws \EE_Error |
|
383 | + */ |
|
384 | + public static function prepare_query_params_for_rest_api( |
|
385 | + array $model_query_params, |
|
386 | + \EEM_Base $model, |
|
387 | + $requested_version = null |
|
388 | + ) { |
|
389 | + if ($requested_version === null) { |
|
390 | + $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
391 | + } |
|
392 | + $rest_query_params = $model_query_params; |
|
393 | + if (isset($model_query_params[0])) { |
|
394 | + $rest_query_params['where'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
395 | + $model_query_params[0], |
|
396 | + $model, |
|
397 | + $requested_version |
|
398 | + ); |
|
399 | + unset($rest_query_params[0]); |
|
400 | + } |
|
401 | + if (isset($model_query_params['having'])) { |
|
402 | + $rest_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api( |
|
403 | + $model_query_params['having'], |
|
404 | + $model, |
|
405 | + $requested_version |
|
406 | + ); |
|
407 | + } |
|
408 | + return apply_filters('FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
409 | + $rest_query_params, $model_query_params, $model, $requested_version); |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + |
|
414 | + /** |
|
415 | + * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
416 | + * |
|
417 | + * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
418 | + * passed into EEM_Base::get_all() |
|
419 | + * @param \EEM_Base $model |
|
420 | + * @param string $requested_version eg "4.8.36" |
|
421 | + * @return array ready for use in the rest api query params |
|
422 | + * @throws \EE_Error |
|
423 | + */ |
|
424 | + public static function prepare_conditions_query_params_for_rest_api( |
|
425 | + $inputted_query_params_of_this_type, |
|
426 | + \EEM_Base $model, |
|
427 | + $requested_version |
|
428 | + ) { |
|
429 | + $query_param_for_models = array(); |
|
430 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
431 | + $field = Model_Data_Translator::deduce_field_from_query_param( |
|
432 | + Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($query_param_key), |
|
433 | + $model |
|
434 | + ); |
|
435 | + if ($field instanceof \EE_Model_Field_Base) { |
|
436 | + //did they specify an operator? |
|
437 | + if (is_array($query_param_value)) { |
|
438 | + $op = $query_param_value[0]; |
|
439 | + $translated_value = array($op); |
|
440 | + if (isset($query_param_value[1])) { |
|
441 | + $value = $query_param_value[1]; |
|
442 | + $translated_value[1] = Model_Data_Translator::prepare_field_values_for_json($field, $value, |
|
443 | + $requested_version); |
|
444 | + } |
|
445 | + } else { |
|
446 | + $translated_value = Model_Data_Translator::prepare_field_value_for_json($field, $query_param_value, |
|
447 | + $requested_version); |
|
448 | + } |
|
449 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
450 | + } else { |
|
451 | + //so it's not for a field, assume it's a logic query param key |
|
452 | + $query_param_for_models[$query_param_key] = Model_Data_Translator::prepare_conditions_query_params_for_rest_api($query_param_value, |
|
453 | + $model, $requested_version); |
|
454 | + } |
|
455 | + } |
|
456 | + return $query_param_for_models; |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + |
|
461 | + /** |
|
462 | + * @param $condition_query_param_key |
|
463 | + * @return string |
|
464 | + */ |
|
465 | + public static function remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
466 | + { |
|
467 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
468 | + if ($pos_of_star === false) { |
|
469 | + return $condition_query_param_key; |
|
470 | + } else { |
|
471 | + $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
472 | + return $condition_query_param_sans_star; |
|
473 | + } |
|
474 | + } |
|
475 | + |
|
476 | + |
|
477 | + |
|
478 | + /** |
|
479 | + * Takes the input parameter and finds the model field that it indicates. |
|
480 | + * |
|
481 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
482 | + * @param \EEM_Base $model |
|
483 | + * @return \EE_Model_Field_Base |
|
484 | + * @throws \EE_Error |
|
485 | + */ |
|
486 | + public static function deduce_field_from_query_param($query_param_name, \EEM_Base $model) |
|
487 | + { |
|
488 | + //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
489 | + //which will help us find the database table and column |
|
490 | + $query_param_parts = explode('.', $query_param_name); |
|
491 | + if (empty($query_param_parts)) { |
|
492 | + throw new \EE_Error(sprintf(__('_extract_column_name is empty when trying to extract column and table name from %s', |
|
493 | + 'event_espresso'), $query_param_name)); |
|
494 | + } |
|
495 | + $number_of_parts = count($query_param_parts); |
|
496 | + $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
497 | + if ($number_of_parts === 1) { |
|
498 | + $field_name = $last_query_param_part; |
|
499 | + } else {// $number_of_parts >= 2 |
|
500 | + //the last part is the column name, and there are only 2parts. therefore... |
|
501 | + $field_name = $last_query_param_part; |
|
502 | + $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
503 | + } |
|
504 | + try { |
|
505 | + return $model->field_settings_for($field_name); |
|
506 | + } catch (\EE_Error $e) { |
|
507 | + return null; |
|
508 | + } |
|
509 | + } |
|
510 | 510 | |
511 | 511 | } |
@@ -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 | |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | '0', |
150 | 150 | STR_PAD_LEFT |
151 | 151 | ); |
152 | - $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
152 | + $new_value = rest_parse_date($original_value.$offset_sign.$offset_string); |
|
153 | 153 | } else { |
154 | 154 | $new_value = $original_value; |
155 | 155 | } |
@@ -166,10 +166,10 @@ discard block |
||
166 | 166 | */ |
167 | 167 | private static function parse_timezone_offset($timezone_offset) |
168 | 168 | { |
169 | - $first_char = substr((string)$timezone_offset, 0, 1); |
|
169 | + $first_char = substr((string) $timezone_offset, 0, 1); |
|
170 | 170 | if ($first_char === '+' || $first_char === '-') { |
171 | 171 | $offset_sign = $first_char; |
172 | - $offset_secs = substr((string)$timezone_offset, 1); |
|
172 | + $offset_secs = substr((string) $timezone_offset, 1); |
|
173 | 173 | } else { |
174 | 174 | $offset_sign = '+'; |
175 | 175 | $offset_secs = $timezone_offset; |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | $model |
238 | 238 | ); |
239 | 239 | //double-check is it a *_gmt field? |
240 | - if (! $field instanceof \EE_Model_Field_Base |
|
240 | + if ( ! $field instanceof \EE_Model_Field_Base |
|
241 | 241 | && Model_Data_Translator::is_gmt_date_field_name($query_param_sans_stars) |
242 | 242 | ) { |
243 | 243 | //yep, take off '_gmt', and find the field |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | */ |
304 | 304 | public static function remove_gmt_from_field_name($field_name) |
305 | 305 | { |
306 | - if (! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
306 | + if ( ! Model_Data_Translator::is_gmt_date_field_name($field_name)) { |
|
307 | 307 | return $field_name; |
308 | 308 | } |
309 | 309 | $query_param_sans_stars = Model_Data_Translator::remove_stars_and_anything_after_from_condition_query_param_key($field_name); |
@@ -6,78 +6,78 @@ |
||
6 | 6 | abstract class Changes_In_Base |
7 | 7 | { |
8 | 8 | |
9 | - /** |
|
10 | - * The version that these changes happened |
|
11 | - * |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - protected $_version = null; |
|
9 | + /** |
|
10 | + * The version that these changes happened |
|
11 | + * |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + protected $_version = null; |
|
15 | 15 | |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Called when an EE4 REST API request is made to an earlier version than |
|
20 | - * what is indicated in this class' name. |
|
21 | - * Uses WordPress' add_filter and add_action to modify the EE4 REST API's response |
|
22 | - * so that regardless of what version of EE4 core is running, API clients |
|
23 | - * will have a consistent response |
|
24 | - * |
|
25 | - * @return void |
|
26 | - */ |
|
27 | - abstract public function set_hooks(); |
|
18 | + /** |
|
19 | + * Called when an EE4 REST API request is made to an earlier version than |
|
20 | + * what is indicated in this class' name. |
|
21 | + * Uses WordPress' add_filter and add_action to modify the EE4 REST API's response |
|
22 | + * so that regardless of what version of EE4 core is running, API clients |
|
23 | + * will have a consistent response |
|
24 | + * |
|
25 | + * @return void |
|
26 | + */ |
|
27 | + abstract public function set_hooks(); |
|
28 | 28 | |
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * Returns whether or not this class' name indicates its hooks should |
|
33 | - * apply when a request comes in for $requested_version. A class can use |
|
34 | - * other conditions when determining whether to perform their callbacks or not, |
|
35 | - * but this will typically be enough |
|
36 | - * |
|
37 | - * @param string $requested_version eg "4.8.33" |
|
38 | - * @return boolean true: this class' name indicates its filters and actions |
|
39 | - * should take effect. False: this class' name indicates it shouldn't do anything |
|
40 | - */ |
|
41 | - public function applies_to_version($requested_version) |
|
42 | - { |
|
43 | - if ($this->version() > $requested_version) { |
|
44 | - return true; |
|
45 | - } |
|
46 | - return false; |
|
47 | - } |
|
31 | + /** |
|
32 | + * Returns whether or not this class' name indicates its hooks should |
|
33 | + * apply when a request comes in for $requested_version. A class can use |
|
34 | + * other conditions when determining whether to perform their callbacks or not, |
|
35 | + * but this will typically be enough |
|
36 | + * |
|
37 | + * @param string $requested_version eg "4.8.33" |
|
38 | + * @return boolean true: this class' name indicates its filters and actions |
|
39 | + * should take effect. False: this class' name indicates it shouldn't do anything |
|
40 | + */ |
|
41 | + public function applies_to_version($requested_version) |
|
42 | + { |
|
43 | + if ($this->version() > $requested_version) { |
|
44 | + return true; |
|
45 | + } |
|
46 | + return false; |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * Gets the EE core version when this changes were made to the rest api. |
|
53 | - * Any requests to earlier versions should have modifications made to them |
|
54 | - * by the callbacks of this class. |
|
55 | - * |
|
56 | - * @return string eg "4.8.33" |
|
57 | - * @throws \EE_Error |
|
58 | - */ |
|
59 | - public function version() |
|
60 | - { |
|
61 | - if ($this->_version === null) { |
|
62 | - $matches = array(); |
|
63 | - $regex = '~Changes_In_(.*)_(.*)_(.*)$~'; |
|
64 | - $success = preg_match( |
|
65 | - $regex, |
|
66 | - get_class($this), |
|
67 | - $matches |
|
68 | - ); |
|
69 | - if (! $success) { |
|
70 | - throw new \EE_Error( |
|
71 | - sprintf( |
|
72 | - __('The class %1$s was misnamed. It name should match the regex "%2$s"', 'event_espresso'), |
|
73 | - get_class($this), |
|
74 | - $regex |
|
75 | - ) |
|
76 | - ); |
|
77 | - } |
|
78 | - $this->_version = $matches[1] . '.' . $matches[2] . '.' . $matches[3]; |
|
79 | - } |
|
80 | - return $this->_version; |
|
81 | - } |
|
51 | + /** |
|
52 | + * Gets the EE core version when this changes were made to the rest api. |
|
53 | + * Any requests to earlier versions should have modifications made to them |
|
54 | + * by the callbacks of this class. |
|
55 | + * |
|
56 | + * @return string eg "4.8.33" |
|
57 | + * @throws \EE_Error |
|
58 | + */ |
|
59 | + public function version() |
|
60 | + { |
|
61 | + if ($this->_version === null) { |
|
62 | + $matches = array(); |
|
63 | + $regex = '~Changes_In_(.*)_(.*)_(.*)$~'; |
|
64 | + $success = preg_match( |
|
65 | + $regex, |
|
66 | + get_class($this), |
|
67 | + $matches |
|
68 | + ); |
|
69 | + if (! $success) { |
|
70 | + throw new \EE_Error( |
|
71 | + sprintf( |
|
72 | + __('The class %1$s was misnamed. It name should match the regex "%2$s"', 'event_espresso'), |
|
73 | + get_class($this), |
|
74 | + $regex |
|
75 | + ) |
|
76 | + ); |
|
77 | + } |
|
78 | + $this->_version = $matches[1] . '.' . $matches[2] . '.' . $matches[3]; |
|
79 | + } |
|
80 | + return $this->_version; |
|
81 | + } |
|
82 | 82 | } |
83 | 83 |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | get_class($this), |
67 | 67 | $matches |
68 | 68 | ); |
69 | - if (! $success) { |
|
69 | + if ( ! $success) { |
|
70 | 70 | throw new \EE_Error( |
71 | 71 | sprintf( |
72 | 72 | __('The class %1$s was misnamed. It name should match the regex "%2$s"', 'event_espresso'), |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | ) |
76 | 76 | ); |
77 | 77 | } |
78 | - $this->_version = $matches[1] . '.' . $matches[2] . '.' . $matches[3]; |
|
78 | + $this->_version = $matches[1].'.'.$matches[2].'.'.$matches[3]; |
|
79 | 79 | } |
80 | 80 | return $this->_version; |
81 | 81 | } |
@@ -122,9 +122,9 @@ |
||
122 | 122 | $headers, |
123 | 123 | array_flip( |
124 | 124 | array( |
125 | - Base::header_prefix_for_wp . 'Total', |
|
126 | - Base::header_prefix_for_wp . 'TotalPages', |
|
127 | - Base::header_prefix_for_wp . 'PageSize', |
|
125 | + Base::header_prefix_for_wp.'Total', |
|
126 | + Base::header_prefix_for_wp.'TotalPages', |
|
127 | + Base::header_prefix_for_wp.'PageSize', |
|
128 | 128 | ))); |
129 | 129 | } |
130 | 130 | return $headers; |
@@ -14,179 +14,179 @@ |
||
14 | 14 | class Changes_In_4_8_36 extends Changes_In_Base |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * Adds hooks so requests to 4.8.29 don't have the checkin endpoints |
|
19 | - */ |
|
20 | - public function set_hooks() |
|
21 | - { |
|
22 | - //set a hook to remove the "calculate" query param |
|
23 | - add_filter( |
|
24 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
25 | - array($this, 'remove_calculate_query_param'), |
|
26 | - 10, |
|
27 | - 3 |
|
28 | - ); |
|
29 | - //don't add the _calculated_fields either |
|
30 | - add_filter( |
|
31 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
32 | - array($this, 'remove_calculated_fields_from_response'), |
|
33 | - 10, |
|
34 | - 5 |
|
35 | - ); |
|
36 | - //and also don't add the count headers |
|
37 | - add_filter( |
|
38 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
39 | - array($this, 'remove_headers_new_in_this_version'), |
|
40 | - 10, |
|
41 | - 3 |
|
42 | - ); |
|
43 | - //remove the old featured_image part of the response... |
|
44 | - add_filter( |
|
45 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
46 | - array($this, 'add_old_featured_image_part_of_cpt_entities'), |
|
47 | - 10, |
|
48 | - 5 |
|
49 | - ); |
|
50 | - //assuming ticket 9425's change gets pushed with 9406, we don't need to |
|
51 | - //remove it from the calculated fields on older requests (because this will |
|
52 | - //be the first version with calculated fields) |
|
53 | - //before this, infinity was -1, now it's null |
|
54 | - add_filter( |
|
55 | - 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
56 | - array($this, 'use_negative_one_for_infinity_before_this_version'), |
|
57 | - 10, |
|
58 | - 4 |
|
59 | - ); |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Dont show "calculate" as an query param option in the index |
|
66 | - * |
|
67 | - * @param array $query_params |
|
68 | - * @param \EEM_base $model |
|
69 | - * @param string $version |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function remove_calculate_query_param($query_params, \EEM_Base $model, $version) |
|
73 | - { |
|
74 | - if ($this->applies_to_version($version)) { |
|
75 | - unset($query_params['calculate']); |
|
76 | - } |
|
77 | - return $query_params; |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * Removes the "_calculate_fields" part of entity responses before 4.8.36 |
|
84 | - * |
|
85 | - * @param array $entity_response_array |
|
86 | - * @param \EEM_Base $model |
|
87 | - * @param string $request_context |
|
88 | - * @param \WP_REST_Request $request |
|
89 | - * @param Read $controller |
|
90 | - * @return array |
|
91 | - */ |
|
92 | - public function remove_calculated_fields_from_response( |
|
93 | - $entity_response_array, |
|
94 | - \EEM_Base $model, |
|
95 | - $request_context, |
|
96 | - \WP_REST_Request $request, |
|
97 | - Read $controller |
|
98 | - ) { |
|
99 | - if ($this->applies_to_version($controller->get_model_version_info()->requested_version())) { |
|
100 | - unset($entity_response_array['_calculated_fields']); |
|
101 | - } |
|
102 | - return $entity_response_array; |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * Removes the new headers for requests before 4.8.36 |
|
109 | - * |
|
110 | - * @param array $headers |
|
111 | - * @param Base $controller |
|
112 | - * @param string $version |
|
113 | - * @return array |
|
114 | - */ |
|
115 | - public function remove_headers_new_in_this_version( |
|
116 | - $headers, |
|
117 | - Controller_Base $controller, |
|
118 | - $version |
|
119 | - ) { |
|
120 | - if ($this->applies_to_version($version)) { |
|
121 | - $headers = array_diff_key( |
|
122 | - $headers, |
|
123 | - array_flip( |
|
124 | - array( |
|
125 | - Base::header_prefix_for_wp . 'Total', |
|
126 | - Base::header_prefix_for_wp . 'TotalPages', |
|
127 | - Base::header_prefix_for_wp . 'PageSize', |
|
128 | - ))); |
|
129 | - } |
|
130 | - return $headers; |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * Puts the 'featured_image_url' back in for responses before 4.8.36. |
|
137 | - * |
|
138 | - * @param array $entity_response_array |
|
139 | - * @param \EEM_Base $model |
|
140 | - * @param string $request_context |
|
141 | - * @param \WP_REST_Request $request |
|
142 | - * @param Read $controller |
|
143 | - * @return array |
|
144 | - */ |
|
145 | - public function add_old_featured_image_part_of_cpt_entities( |
|
146 | - $entity_response_array, |
|
147 | - \EEM_Base $model, |
|
148 | - $request_context, |
|
149 | - \WP_REST_Request $request, |
|
150 | - Read $controller |
|
151 | - ) { |
|
152 | - if ($this->applies_to_version($controller->get_model_version_info()->requested_version()) |
|
153 | - && $model instanceof \EEM_CPT_Base |
|
154 | - ) { |
|
155 | - $attachment = wp_get_attachment_image_src( |
|
156 | - get_post_thumbnail_id($entity_response_array[$model->primary_key_name()]), |
|
157 | - 'full' |
|
158 | - ); |
|
159 | - $entity_response_array['featured_image_url'] = ! empty($attachment) ? $attachment[0] : null; |
|
160 | - } |
|
161 | - return $entity_response_array; |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * If the value was infinity, we now use null in our JSON responses, |
|
168 | - * but before this version we used -1. |
|
169 | - * |
|
170 | - * @param mixed $new_value |
|
171 | - * @param \EE_Model_Field_Base $field_obj |
|
172 | - * @param mixed $original_value |
|
173 | - * @param string $requested_value |
|
174 | - * @return mixed |
|
175 | - */ |
|
176 | - public function use_negative_one_for_infinity_before_this_version( |
|
177 | - $new_value, |
|
178 | - $field_obj, |
|
179 | - $original_value, |
|
180 | - $requested_value |
|
181 | - ) { |
|
182 | - if ($this->applies_to_version($requested_value) |
|
183 | - && $original_value === EE_INF |
|
184 | - ) { |
|
185 | - //return the old representation of infinity in the JSON |
|
186 | - return -1; |
|
187 | - } |
|
188 | - return $new_value; |
|
189 | - } |
|
17 | + /** |
|
18 | + * Adds hooks so requests to 4.8.29 don't have the checkin endpoints |
|
19 | + */ |
|
20 | + public function set_hooks() |
|
21 | + { |
|
22 | + //set a hook to remove the "calculate" query param |
|
23 | + add_filter( |
|
24 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
25 | + array($this, 'remove_calculate_query_param'), |
|
26 | + 10, |
|
27 | + 3 |
|
28 | + ); |
|
29 | + //don't add the _calculated_fields either |
|
30 | + add_filter( |
|
31 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
32 | + array($this, 'remove_calculated_fields_from_response'), |
|
33 | + 10, |
|
34 | + 5 |
|
35 | + ); |
|
36 | + //and also don't add the count headers |
|
37 | + add_filter( |
|
38 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
39 | + array($this, 'remove_headers_new_in_this_version'), |
|
40 | + 10, |
|
41 | + 3 |
|
42 | + ); |
|
43 | + //remove the old featured_image part of the response... |
|
44 | + add_filter( |
|
45 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
46 | + array($this, 'add_old_featured_image_part_of_cpt_entities'), |
|
47 | + 10, |
|
48 | + 5 |
|
49 | + ); |
|
50 | + //assuming ticket 9425's change gets pushed with 9406, we don't need to |
|
51 | + //remove it from the calculated fields on older requests (because this will |
|
52 | + //be the first version with calculated fields) |
|
53 | + //before this, infinity was -1, now it's null |
|
54 | + add_filter( |
|
55 | + 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
56 | + array($this, 'use_negative_one_for_infinity_before_this_version'), |
|
57 | + 10, |
|
58 | + 4 |
|
59 | + ); |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Dont show "calculate" as an query param option in the index |
|
66 | + * |
|
67 | + * @param array $query_params |
|
68 | + * @param \EEM_base $model |
|
69 | + * @param string $version |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function remove_calculate_query_param($query_params, \EEM_Base $model, $version) |
|
73 | + { |
|
74 | + if ($this->applies_to_version($version)) { |
|
75 | + unset($query_params['calculate']); |
|
76 | + } |
|
77 | + return $query_params; |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * Removes the "_calculate_fields" part of entity responses before 4.8.36 |
|
84 | + * |
|
85 | + * @param array $entity_response_array |
|
86 | + * @param \EEM_Base $model |
|
87 | + * @param string $request_context |
|
88 | + * @param \WP_REST_Request $request |
|
89 | + * @param Read $controller |
|
90 | + * @return array |
|
91 | + */ |
|
92 | + public function remove_calculated_fields_from_response( |
|
93 | + $entity_response_array, |
|
94 | + \EEM_Base $model, |
|
95 | + $request_context, |
|
96 | + \WP_REST_Request $request, |
|
97 | + Read $controller |
|
98 | + ) { |
|
99 | + if ($this->applies_to_version($controller->get_model_version_info()->requested_version())) { |
|
100 | + unset($entity_response_array['_calculated_fields']); |
|
101 | + } |
|
102 | + return $entity_response_array; |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * Removes the new headers for requests before 4.8.36 |
|
109 | + * |
|
110 | + * @param array $headers |
|
111 | + * @param Base $controller |
|
112 | + * @param string $version |
|
113 | + * @return array |
|
114 | + */ |
|
115 | + public function remove_headers_new_in_this_version( |
|
116 | + $headers, |
|
117 | + Controller_Base $controller, |
|
118 | + $version |
|
119 | + ) { |
|
120 | + if ($this->applies_to_version($version)) { |
|
121 | + $headers = array_diff_key( |
|
122 | + $headers, |
|
123 | + array_flip( |
|
124 | + array( |
|
125 | + Base::header_prefix_for_wp . 'Total', |
|
126 | + Base::header_prefix_for_wp . 'TotalPages', |
|
127 | + Base::header_prefix_for_wp . 'PageSize', |
|
128 | + ))); |
|
129 | + } |
|
130 | + return $headers; |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * Puts the 'featured_image_url' back in for responses before 4.8.36. |
|
137 | + * |
|
138 | + * @param array $entity_response_array |
|
139 | + * @param \EEM_Base $model |
|
140 | + * @param string $request_context |
|
141 | + * @param \WP_REST_Request $request |
|
142 | + * @param Read $controller |
|
143 | + * @return array |
|
144 | + */ |
|
145 | + public function add_old_featured_image_part_of_cpt_entities( |
|
146 | + $entity_response_array, |
|
147 | + \EEM_Base $model, |
|
148 | + $request_context, |
|
149 | + \WP_REST_Request $request, |
|
150 | + Read $controller |
|
151 | + ) { |
|
152 | + if ($this->applies_to_version($controller->get_model_version_info()->requested_version()) |
|
153 | + && $model instanceof \EEM_CPT_Base |
|
154 | + ) { |
|
155 | + $attachment = wp_get_attachment_image_src( |
|
156 | + get_post_thumbnail_id($entity_response_array[$model->primary_key_name()]), |
|
157 | + 'full' |
|
158 | + ); |
|
159 | + $entity_response_array['featured_image_url'] = ! empty($attachment) ? $attachment[0] : null; |
|
160 | + } |
|
161 | + return $entity_response_array; |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * If the value was infinity, we now use null in our JSON responses, |
|
168 | + * but before this version we used -1. |
|
169 | + * |
|
170 | + * @param mixed $new_value |
|
171 | + * @param \EE_Model_Field_Base $field_obj |
|
172 | + * @param mixed $original_value |
|
173 | + * @param string $requested_value |
|
174 | + * @return mixed |
|
175 | + */ |
|
176 | + public function use_negative_one_for_infinity_before_this_version( |
|
177 | + $new_value, |
|
178 | + $field_obj, |
|
179 | + $original_value, |
|
180 | + $requested_value |
|
181 | + ) { |
|
182 | + if ($this->applies_to_version($requested_value) |
|
183 | + && $original_value === EE_INF |
|
184 | + ) { |
|
185 | + //return the old representation of infinity in the JSON |
|
186 | + return -1; |
|
187 | + } |
|
188 | + return $new_value; |
|
189 | + } |
|
190 | 190 | |
191 | 191 | } |
192 | 192 |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * @since $VID:$ |
13 | 13 | */ |
14 | 14 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
15 | - exit('No direct script access allowed'); |
|
15 | + exit('No direct script access allowed'); |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | |
@@ -20,46 +20,46 @@ discard block |
||
20 | 20 | class Rest_Exception extends \EE_Error |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $_wp_error_data = array(); |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $_wp_error_data = array(); |
|
27 | 27 | |
28 | - protected $_wp_error_code = ''; |
|
28 | + protected $_wp_error_code = ''; |
|
29 | 29 | |
30 | 30 | |
31 | 31 | |
32 | - public function __construct($string_code, $message, $wp_error_data = array(), $previous = null) |
|
33 | - { |
|
34 | - parent::__construct( |
|
35 | - $message, |
|
36 | - 500, |
|
37 | - $previous); |
|
38 | - $this->_wp_error_data = $wp_error_data; |
|
39 | - $this->_wp_error_code = $string_code; |
|
40 | - } |
|
32 | + public function __construct($string_code, $message, $wp_error_data = array(), $previous = null) |
|
33 | + { |
|
34 | + parent::__construct( |
|
35 | + $message, |
|
36 | + 500, |
|
37 | + $previous); |
|
38 | + $this->_wp_error_data = $wp_error_data; |
|
39 | + $this->_wp_error_code = $string_code; |
|
40 | + } |
|
41 | 41 | |
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * Array of data that may have been set during the constructor, intended for WP_Error's data |
|
46 | - * |
|
47 | - * @return array |
|
48 | - */ |
|
49 | - public function get_data() |
|
50 | - { |
|
51 | - return $this->_wp_error_data; |
|
52 | - } |
|
44 | + /** |
|
45 | + * Array of data that may have been set during the constructor, intended for WP_Error's data |
|
46 | + * |
|
47 | + * @return array |
|
48 | + */ |
|
49 | + public function get_data() |
|
50 | + { |
|
51 | + return $this->_wp_error_data; |
|
52 | + } |
|
53 | 53 | |
54 | 54 | |
55 | 55 | |
56 | - /** |
|
57 | - * Gets the error string |
|
58 | - * |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - public function get_string_code() |
|
62 | - { |
|
63 | - return $this->_wp_error_code; |
|
64 | - } |
|
56 | + /** |
|
57 | + * Gets the error string |
|
58 | + * |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + public function get_string_code() |
|
62 | + { |
|
63 | + return $this->_wp_error_code; |
|
64 | + } |
|
65 | 65 | } |
@@ -11,7 +11,7 @@ |
||
11 | 11 | * @author Mike Nelson |
12 | 12 | * @since $VID:$ |
13 | 13 | */ |
14 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
14 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
15 | 15 | exit('No direct script access allowed'); |
16 | 16 | } |
17 | 17 |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | * @since $VID:$ |
15 | 15 | */ |
16 | 16 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
17 | - exit('No direct script access allowed'); |
|
17 | + exit('No direct script access allowed'); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | |
@@ -22,128 +22,128 @@ discard block |
||
22 | 22 | class Datetime extends Calculations_Base |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Calculates the total spaces available on the datetime, taking into account |
|
27 | - * ticket limits too. |
|
28 | - * |
|
29 | - * @see EE_Datetime::spaces_remaining( true ) |
|
30 | - * @param array $wpdb_row |
|
31 | - * @param \WP_REST_Request $request |
|
32 | - * @param Controller_Base $controller |
|
33 | - * @return int |
|
34 | - * @throws \EE_Error |
|
35 | - */ |
|
36 | - public static function spaces_remaining_considering_tickets($wpdb_row, $request, $controller) |
|
37 | - { |
|
38 | - if (is_array($wpdb_row) && isset($wpdb_row['Datetime.DTT_ID'])) { |
|
39 | - $dtt_obj = \EEM_Datetime::instance()->get_one_by_ID($wpdb_row['Datetime.DTT_ID']); |
|
40 | - } else { |
|
41 | - $dtt_obj = null; |
|
42 | - } |
|
43 | - if ($dtt_obj instanceof \EE_Datetime) { |
|
44 | - return $dtt_obj->spaces_remaining(true); |
|
45 | - } else { |
|
46 | - throw new \EE_Error( |
|
47 | - sprintf( |
|
48 | - __('Cannot calculate spaces_remaining_considering_tickets because the datetime with ID %1$s (from database row %2$s) was not found', |
|
49 | - 'event_espresso'), |
|
50 | - $wpdb_row['Datetime.DTT_ID'], |
|
51 | - print_r($wpdb_row, true) |
|
52 | - ) |
|
53 | - ); |
|
54 | - } |
|
55 | - } |
|
25 | + /** |
|
26 | + * Calculates the total spaces available on the datetime, taking into account |
|
27 | + * ticket limits too. |
|
28 | + * |
|
29 | + * @see EE_Datetime::spaces_remaining( true ) |
|
30 | + * @param array $wpdb_row |
|
31 | + * @param \WP_REST_Request $request |
|
32 | + * @param Controller_Base $controller |
|
33 | + * @return int |
|
34 | + * @throws \EE_Error |
|
35 | + */ |
|
36 | + public static function spaces_remaining_considering_tickets($wpdb_row, $request, $controller) |
|
37 | + { |
|
38 | + if (is_array($wpdb_row) && isset($wpdb_row['Datetime.DTT_ID'])) { |
|
39 | + $dtt_obj = \EEM_Datetime::instance()->get_one_by_ID($wpdb_row['Datetime.DTT_ID']); |
|
40 | + } else { |
|
41 | + $dtt_obj = null; |
|
42 | + } |
|
43 | + if ($dtt_obj instanceof \EE_Datetime) { |
|
44 | + return $dtt_obj->spaces_remaining(true); |
|
45 | + } else { |
|
46 | + throw new \EE_Error( |
|
47 | + sprintf( |
|
48 | + __('Cannot calculate spaces_remaining_considering_tickets because the datetime with ID %1$s (from database row %2$s) was not found', |
|
49 | + 'event_espresso'), |
|
50 | + $wpdb_row['Datetime.DTT_ID'], |
|
51 | + print_r($wpdb_row, true) |
|
52 | + ) |
|
53 | + ); |
|
54 | + } |
|
55 | + } |
|
56 | 56 | |
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * Counts registrations who have checked into this datetime |
|
61 | - * |
|
62 | - * @param array $wpdb_row |
|
63 | - * @param \WP_REST_Request $request |
|
64 | - * @param Controller_Base $controller |
|
65 | - * @return int |
|
66 | - * @throws \EE_Error |
|
67 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
68 | - */ |
|
69 | - public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
|
70 | - { |
|
71 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
72 | - throw new \EE_Error( |
|
73 | - sprintf( |
|
74 | - __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
75 | - 'event_espresso'), |
|
76 | - print_r($wpdb_row, true) |
|
77 | - ) |
|
78 | - ); |
|
79 | - } |
|
80 | - self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_in_count'); |
|
81 | - return \EEM_Registration::instance() |
|
82 | - ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], true); |
|
83 | - } |
|
59 | + /** |
|
60 | + * Counts registrations who have checked into this datetime |
|
61 | + * |
|
62 | + * @param array $wpdb_row |
|
63 | + * @param \WP_REST_Request $request |
|
64 | + * @param Controller_Base $controller |
|
65 | + * @return int |
|
66 | + * @throws \EE_Error |
|
67 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
68 | + */ |
|
69 | + public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
|
70 | + { |
|
71 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
72 | + throw new \EE_Error( |
|
73 | + sprintf( |
|
74 | + __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
75 | + 'event_espresso'), |
|
76 | + print_r($wpdb_row, true) |
|
77 | + ) |
|
78 | + ); |
|
79 | + } |
|
80 | + self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_in_count'); |
|
81 | + return \EEM_Registration::instance() |
|
82 | + ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], true); |
|
83 | + } |
|
84 | 84 | |
85 | 85 | |
86 | 86 | |
87 | - /** |
|
88 | - * Counts registrations who have checked out of this datetime |
|
89 | - * |
|
90 | - * @param array $wpdb_row |
|
91 | - * @param \WP_REST_Request $request |
|
92 | - * @param Controller_Base $controller |
|
93 | - * @return int |
|
94 | - * @throws \EE_Error |
|
95 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
96 | - */ |
|
97 | - public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
|
98 | - { |
|
99 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
100 | - throw new \EE_Error( |
|
101 | - sprintf( |
|
102 | - __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
103 | - 'event_espresso'), |
|
104 | - print_r($wpdb_row, true) |
|
105 | - ) |
|
106 | - ); |
|
107 | - } |
|
108 | - self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_out_count'); |
|
109 | - return \EEM_Registration::instance() |
|
110 | - ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], false); |
|
111 | - } |
|
87 | + /** |
|
88 | + * Counts registrations who have checked out of this datetime |
|
89 | + * |
|
90 | + * @param array $wpdb_row |
|
91 | + * @param \WP_REST_Request $request |
|
92 | + * @param Controller_Base $controller |
|
93 | + * @return int |
|
94 | + * @throws \EE_Error |
|
95 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
96 | + */ |
|
97 | + public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
|
98 | + { |
|
99 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
100 | + throw new \EE_Error( |
|
101 | + sprintf( |
|
102 | + __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
103 | + 'event_espresso'), |
|
104 | + print_r($wpdb_row, true) |
|
105 | + ) |
|
106 | + ); |
|
107 | + } |
|
108 | + self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_out_count'); |
|
109 | + return \EEM_Registration::instance() |
|
110 | + ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], false); |
|
111 | + } |
|
112 | 112 | |
113 | 113 | |
114 | 114 | |
115 | - /** |
|
116 | - * Counts the number of pending-payment registrations for this event (regardless |
|
117 | - * of how many datetimes each registrations' ticket purchase is for) |
|
118 | - * |
|
119 | - * @param array $wpdb_row |
|
120 | - * @param \WP_REST_Request $request |
|
121 | - * @param Controller_Base $controller |
|
122 | - * @return int |
|
123 | - * @throws \EE_Error |
|
124 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
125 | - */ |
|
126 | - public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
|
127 | - { |
|
128 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
129 | - throw new \EE_Error( |
|
130 | - sprintf( |
|
131 | - __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
132 | - 'event_espresso'), |
|
133 | - print_r($wpdb_row, true) |
|
134 | - ) |
|
135 | - ); |
|
136 | - } |
|
137 | - self::_verify_current_user_can('ee_read_registrations', 'spots_taken_pending_payment'); |
|
138 | - return \EEM_Registration::instance()->count( |
|
139 | - array( |
|
140 | - array( |
|
141 | - 'Ticket.Datetime.DTT_ID' => $wpdb_row['Datetime.DTT_ID'], |
|
142 | - 'STS_ID' => \EEM_Registration::status_id_pending_payment, |
|
143 | - ), |
|
144 | - ), |
|
145 | - 'REG_ID', |
|
146 | - true |
|
147 | - ); |
|
148 | - } |
|
115 | + /** |
|
116 | + * Counts the number of pending-payment registrations for this event (regardless |
|
117 | + * of how many datetimes each registrations' ticket purchase is for) |
|
118 | + * |
|
119 | + * @param array $wpdb_row |
|
120 | + * @param \WP_REST_Request $request |
|
121 | + * @param Controller_Base $controller |
|
122 | + * @return int |
|
123 | + * @throws \EE_Error |
|
124 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
125 | + */ |
|
126 | + public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
|
127 | + { |
|
128 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
129 | + throw new \EE_Error( |
|
130 | + sprintf( |
|
131 | + __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
132 | + 'event_espresso'), |
|
133 | + print_r($wpdb_row, true) |
|
134 | + ) |
|
135 | + ); |
|
136 | + } |
|
137 | + self::_verify_current_user_can('ee_read_registrations', 'spots_taken_pending_payment'); |
|
138 | + return \EEM_Registration::instance()->count( |
|
139 | + array( |
|
140 | + array( |
|
141 | + 'Ticket.Datetime.DTT_ID' => $wpdb_row['Datetime.DTT_ID'], |
|
142 | + 'STS_ID' => \EEM_Registration::status_id_pending_payment, |
|
143 | + ), |
|
144 | + ), |
|
145 | + 'REG_ID', |
|
146 | + true |
|
147 | + ); |
|
148 | + } |
|
149 | 149 | } |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * @author Mike Nelson |
14 | 14 | * @since $VID:$ |
15 | 15 | */ |
16 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
16 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
17 | 17 | exit('No direct script access allowed'); |
18 | 18 | } |
19 | 19 | |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
70 | 70 | { |
71 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
71 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
72 | 72 | throw new \EE_Error( |
73 | 73 | sprintf( |
74 | 74 | __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | */ |
97 | 97 | public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
98 | 98 | { |
99 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
99 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
100 | 100 | throw new \EE_Error( |
101 | 101 | sprintf( |
102 | 102 | __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | */ |
126 | 126 | public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
127 | 127 | { |
128 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
128 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
129 | 129 | throw new \EE_Error( |
130 | 130 | sprintf( |
131 | 131 | __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * @since $VID:$ |
14 | 14 | */ |
15 | 15 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
16 | - exit('No direct script access allowed'); |
|
16 | + exit('No direct script access allowed'); |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | |
@@ -21,37 +21,37 @@ discard block |
||
21 | 21 | class Base |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * @param $required_permission |
|
26 | - * @param $attempted_calculation |
|
27 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
28 | - */ |
|
29 | - protected static function _verify_current_user_can($required_permission, $attempted_calculation) |
|
30 | - { |
|
31 | - if (! current_user_can($required_permission)) { |
|
32 | - throw new Rest_Exception( |
|
33 | - 'permission_denied', |
|
34 | - sprintf( |
|
35 | - __('Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
36 | - 'event_espresso'), |
|
37 | - $attempted_calculation, |
|
38 | - \EEH_Inflector::pluralize_and_lower(self::get_resource_name()), |
|
39 | - $required_permission |
|
40 | - ) |
|
41 | - ); |
|
42 | - } |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * Gets the name of the resource of the called class |
|
49 | - * |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public static function get_resource_name() |
|
53 | - { |
|
54 | - $classname = get_called_class(); |
|
55 | - return substr($classname, strrpos($classname, '\\') + 1); |
|
56 | - } |
|
24 | + /** |
|
25 | + * @param $required_permission |
|
26 | + * @param $attempted_calculation |
|
27 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
28 | + */ |
|
29 | + protected static function _verify_current_user_can($required_permission, $attempted_calculation) |
|
30 | + { |
|
31 | + if (! current_user_can($required_permission)) { |
|
32 | + throw new Rest_Exception( |
|
33 | + 'permission_denied', |
|
34 | + sprintf( |
|
35 | + __('Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
36 | + 'event_espresso'), |
|
37 | + $attempted_calculation, |
|
38 | + \EEH_Inflector::pluralize_and_lower(self::get_resource_name()), |
|
39 | + $required_permission |
|
40 | + ) |
|
41 | + ); |
|
42 | + } |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * Gets the name of the resource of the called class |
|
49 | + * |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public static function get_resource_name() |
|
53 | + { |
|
54 | + $classname = get_called_class(); |
|
55 | + return substr($classname, strrpos($classname, '\\') + 1); |
|
56 | + } |
|
57 | 57 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * @author Mike Nelson |
13 | 13 | * @since $VID:$ |
14 | 14 | */ |
15 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
15 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
16 | 16 | exit('No direct script access allowed'); |
17 | 17 | } |
18 | 18 | |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | */ |
29 | 29 | protected static function _verify_current_user_can($required_permission, $attempted_calculation) |
30 | 30 | { |
31 | - if (! current_user_can($required_permission)) { |
|
31 | + if ( ! current_user_can($required_permission)) { |
|
32 | 32 | throw new Rest_Exception( |
33 | 33 | 'permission_denied', |
34 | 34 | sprintf( |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * @author Mike Nelson |
13 | 13 | * @since $VID:$ |
14 | 14 | */ |
15 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
15 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
16 | 16 | exit('No direct script access allowed'); |
17 | 17 | } |
18 | 18 | |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | } else { |
38 | 38 | $reg = null; |
39 | 39 | } |
40 | - if (! $reg instanceof \EE_Registration |
|
40 | + if ( ! $reg instanceof \EE_Registration |
|
41 | 41 | ) { |
42 | 42 | throw new \EE_Error( |
43 | 43 | sprintf( |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * @since $VID:$ |
14 | 14 | */ |
15 | 15 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
16 | - exit('No direct script access allowed'); |
|
16 | + exit('No direct script access allowed'); |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | |
@@ -21,58 +21,58 @@ discard block |
||
21 | 21 | class Registration extends Calculations_Base |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * Calculates the checkin status for each datetime this registration has access to |
|
26 | - * |
|
27 | - * @param array $wpdb_row |
|
28 | - * @param \WP_REST_Request $request |
|
29 | - * @param Base $controller |
|
30 | - * @return int |
|
31 | - * @throws \EE_Error |
|
32 | - */ |
|
33 | - public static function datetime_checkin_stati($wpdb_row, $request, $controller) |
|
34 | - { |
|
35 | - if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
36 | - $reg = \EEM_Registration::instance()->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
37 | - } else { |
|
38 | - $reg = null; |
|
39 | - } |
|
40 | - if (! $reg instanceof \EE_Registration |
|
41 | - ) { |
|
42 | - throw new \EE_Error( |
|
43 | - sprintf( |
|
44 | - __('Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
45 | - 'event_espresso'), |
|
46 | - $wpdb_row['Registration.REG_ID'], |
|
47 | - print_r($wpdb_row, true) |
|
48 | - ) |
|
49 | - ); |
|
50 | - } |
|
51 | - $datetime_ids = \EEM_Datetime::instance()->get_col( |
|
52 | - array( |
|
53 | - array( |
|
54 | - 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
55 | - ), |
|
56 | - 'default_where_conditions' => \EEM_Base::default_where_conditions_minimum_all |
|
57 | - ) |
|
58 | - ); |
|
59 | - $checkin_stati = array(); |
|
60 | - foreach ($datetime_ids as $datetime_id) { |
|
61 | - $status = $reg->check_in_status_for_datetime($datetime_id); |
|
62 | - switch ($status) { |
|
63 | - case \EE_Registration::checkin_status_out: |
|
64 | - $status_pretty = 'OUT'; |
|
65 | - break; |
|
66 | - case \EE_Registration::checkin_status_in: |
|
67 | - $status_pretty = 'IN'; |
|
68 | - break; |
|
69 | - case \EE_Registration::checkin_status_never: |
|
70 | - default: |
|
71 | - $status_pretty = 'NEVER'; |
|
72 | - break; |
|
73 | - } |
|
74 | - $checkin_stati[$datetime_id] = $status_pretty; |
|
75 | - } |
|
76 | - return $checkin_stati; |
|
77 | - } |
|
24 | + /** |
|
25 | + * Calculates the checkin status for each datetime this registration has access to |
|
26 | + * |
|
27 | + * @param array $wpdb_row |
|
28 | + * @param \WP_REST_Request $request |
|
29 | + * @param Base $controller |
|
30 | + * @return int |
|
31 | + * @throws \EE_Error |
|
32 | + */ |
|
33 | + public static function datetime_checkin_stati($wpdb_row, $request, $controller) |
|
34 | + { |
|
35 | + if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
36 | + $reg = \EEM_Registration::instance()->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
37 | + } else { |
|
38 | + $reg = null; |
|
39 | + } |
|
40 | + if (! $reg instanceof \EE_Registration |
|
41 | + ) { |
|
42 | + throw new \EE_Error( |
|
43 | + sprintf( |
|
44 | + __('Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
45 | + 'event_espresso'), |
|
46 | + $wpdb_row['Registration.REG_ID'], |
|
47 | + print_r($wpdb_row, true) |
|
48 | + ) |
|
49 | + ); |
|
50 | + } |
|
51 | + $datetime_ids = \EEM_Datetime::instance()->get_col( |
|
52 | + array( |
|
53 | + array( |
|
54 | + 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
55 | + ), |
|
56 | + 'default_where_conditions' => \EEM_Base::default_where_conditions_minimum_all |
|
57 | + ) |
|
58 | + ); |
|
59 | + $checkin_stati = array(); |
|
60 | + foreach ($datetime_ids as $datetime_id) { |
|
61 | + $status = $reg->check_in_status_for_datetime($datetime_id); |
|
62 | + switch ($status) { |
|
63 | + case \EE_Registration::checkin_status_out: |
|
64 | + $status_pretty = 'OUT'; |
|
65 | + break; |
|
66 | + case \EE_Registration::checkin_status_in: |
|
67 | + $status_pretty = 'IN'; |
|
68 | + break; |
|
69 | + case \EE_Registration::checkin_status_never: |
|
70 | + default: |
|
71 | + $status_pretty = 'NEVER'; |
|
72 | + break; |
|
73 | + } |
|
74 | + $checkin_stati[$datetime_id] = $status_pretty; |
|
75 | + } |
|
76 | + return $checkin_stati; |
|
77 | + } |
|
78 | 78 | } |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | * @since $VID:$ |
15 | 15 | */ |
16 | 16 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
17 | - exit('No direct script access allowed'); |
|
17 | + exit('No direct script access allowed'); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | |
@@ -22,350 +22,350 @@ discard block |
||
22 | 22 | class Event extends Calculations_Base |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Calculates the total spaces on the event (not subtracting sales, but taking |
|
27 | - * sales into account; so this is the optimum sales that CAN still be achieved) |
|
28 | - * See EE_Event::total_available_spaces( true ); |
|
29 | - * |
|
30 | - * @param array $wpdb_row |
|
31 | - * @param \WP_REST_Request $request |
|
32 | - * @param Base $controller |
|
33 | - * @return int |
|
34 | - * @throws \EE_Error |
|
35 | - */ |
|
36 | - public static function optimum_sales_at_start($wpdb_row, $request, $controller) |
|
37 | - { |
|
38 | - if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
39 | - $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
40 | - } else { |
|
41 | - $event_obj = null; |
|
42 | - } |
|
43 | - if ($event_obj instanceof \EE_Event) { |
|
44 | - return $event_obj->total_available_spaces(true); |
|
45 | - } else { |
|
46 | - throw new \EE_Error( |
|
47 | - sprintf( |
|
48 | - __('Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
|
49 | - 'event_espresso'), |
|
50 | - $wpdb_row['Event_CPT.ID'], |
|
51 | - print_r($wpdb_row, true) |
|
52 | - ) |
|
53 | - ); |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
|
61 | - * sales that COULD have been achieved) |
|
62 | - * See EE_Event::total_available_spaces( true ); |
|
63 | - * |
|
64 | - * @param array $wpdb_row |
|
65 | - * @param \WP_REST_Request $request |
|
66 | - * @param Base $controller |
|
67 | - * @return int |
|
68 | - * @throws \EE_Error |
|
69 | - */ |
|
70 | - public static function optimum_sales_now($wpdb_row, $request, $controller) |
|
71 | - { |
|
72 | - if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
73 | - $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
74 | - } else { |
|
75 | - $event_obj = null; |
|
76 | - } |
|
77 | - if ($event_obj instanceof \EE_Event) { |
|
78 | - return $event_obj->total_available_spaces(false); |
|
79 | - } else { |
|
80 | - throw new \EE_Error( |
|
81 | - sprintf( |
|
82 | - __('Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
|
83 | - 'event_espresso'), |
|
84 | - $wpdb_row['Event_CPT.ID'], |
|
85 | - print_r($wpdb_row, true) |
|
86 | - ) |
|
87 | - ); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Like optimum_sales_now, but minus total sales so far. |
|
95 | - * See EE_Event::spaces_remaining_for_sale( true ); |
|
96 | - * |
|
97 | - * @param array $wpdb_row |
|
98 | - * @param \WP_REST_Request $request |
|
99 | - * @param Base $controller |
|
100 | - * @return int |
|
101 | - * @throws \EE_Error |
|
102 | - */ |
|
103 | - public static function spaces_remaining($wpdb_row, $request, $controller) |
|
104 | - { |
|
105 | - if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
106 | - $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
107 | - } else { |
|
108 | - $event_obj = null; |
|
109 | - } |
|
110 | - if ($event_obj instanceof \EE_Event) { |
|
111 | - return $event_obj->spaces_remaining_for_sale(); |
|
112 | - } else { |
|
113 | - throw new \EE_Error( |
|
114 | - sprintf( |
|
115 | - __('Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
|
116 | - 'event_espresso'), |
|
117 | - $wpdb_row['Event_CPT.ID'], |
|
118 | - print_r($wpdb_row, true) |
|
119 | - ) |
|
120 | - ); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Counts the number of approved registrations for this event (regardless |
|
128 | - * of how many datetimes each registrations' ticket purchase is for) |
|
129 | - * |
|
130 | - * @param array $wpdb_row |
|
131 | - * @param \WP_REST_Request $request |
|
132 | - * @param Base $controller |
|
133 | - * @return int |
|
134 | - * @throws \EE_Error |
|
135 | - */ |
|
136 | - public static function spots_taken($wpdb_row, $request, $controller) |
|
137 | - { |
|
138 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
139 | - throw new \EE_Error( |
|
140 | - sprintf( |
|
141 | - __('Cannot calculate spots_taken because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
142 | - 'event_espresso'), |
|
143 | - print_r($wpdb_row, true) |
|
144 | - ) |
|
145 | - ); |
|
146 | - } |
|
147 | - return \EEM_Registration::instance()->count( |
|
148 | - array( |
|
149 | - array( |
|
150 | - 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
151 | - 'STS_ID' => \EEM_Registration::status_id_approved, |
|
152 | - ), |
|
153 | - ), |
|
154 | - 'REG_ID', |
|
155 | - true |
|
156 | - ); |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * Counts the number of pending-payment registrations for this event (regardless |
|
163 | - * of how many datetimes each registrations' ticket purchase is for) |
|
164 | - * |
|
165 | - * @param array $wpdb_row |
|
166 | - * @param \WP_REST_Request $request |
|
167 | - * @param Base $controller |
|
168 | - * @return int |
|
169 | - * @throws \EE_Error |
|
170 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
171 | - */ |
|
172 | - public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
|
173 | - { |
|
174 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
175 | - throw new \EE_Error( |
|
176 | - sprintf( |
|
177 | - __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
178 | - 'event_espresso'), |
|
179 | - print_r($wpdb_row, true) |
|
180 | - ) |
|
181 | - ); |
|
182 | - } |
|
183 | - self::_verify_current_user_can('ee_read_registrations', 'spots_taken_pending_payment'); |
|
184 | - return \EEM_Registration::instance()->count( |
|
185 | - array( |
|
186 | - array( |
|
187 | - 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
188 | - 'STS_ID' => \EEM_Registration::status_id_pending_payment, |
|
189 | - ), |
|
190 | - ), |
|
191 | - 'REG_ID', |
|
192 | - true |
|
193 | - ); |
|
194 | - } |
|
195 | - |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * Counts all the registrations who have checked into one of this events' datetimes |
|
200 | - * See EE_Event::total_available_spaces( false ); |
|
201 | - * |
|
202 | - * @param array $wpdb_row |
|
203 | - * @param \WP_REST_Request $request |
|
204 | - * @param Base $controller |
|
205 | - * @return int|null if permission denied |
|
206 | - * @throws \EE_Error |
|
207 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
208 | - */ |
|
209 | - public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
|
210 | - { |
|
211 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
212 | - throw new \EE_Error( |
|
213 | - sprintf( |
|
214 | - __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
215 | - 'event_espresso'), |
|
216 | - print_r($wpdb_row, true) |
|
217 | - ) |
|
218 | - ); |
|
219 | - } |
|
220 | - self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_in_count'); |
|
221 | - return \EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - |
|
226 | - /** |
|
227 | - * Counts all the registrations who have checked out of one of this events' datetimes |
|
228 | - * See EE_Event::total_available_spaces( false ); |
|
229 | - * |
|
230 | - * @param array $wpdb_row |
|
231 | - * @param \WP_REST_Request $request |
|
232 | - * @param Base $controller |
|
233 | - * @return int |
|
234 | - * @throws \EE_Error |
|
235 | - * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
236 | - */ |
|
237 | - public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
|
238 | - { |
|
239 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
240 | - throw new \EE_Error( |
|
241 | - sprintf( |
|
242 | - __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
243 | - 'event_espresso'), |
|
244 | - print_r($wpdb_row, true) |
|
245 | - ) |
|
246 | - ); |
|
247 | - } |
|
248 | - self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_out_count'); |
|
249 | - return \EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Gets the thumbnail image |
|
256 | - * |
|
257 | - * @param array $wpdb_row |
|
258 | - * @param \WP_REST_Request $request |
|
259 | - * @param Base $controller |
|
260 | - * @return array |
|
261 | - */ |
|
262 | - public static function image_thumbnail($wpdb_row, $request, $controller) |
|
263 | - { |
|
264 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'thumbnail'); |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - |
|
269 | - /** |
|
270 | - * Gets the medium image |
|
271 | - * |
|
272 | - * @param array $wpdb_row |
|
273 | - * @param \WP_REST_Request $request |
|
274 | - * @param Base $controller |
|
275 | - * @return array |
|
276 | - */ |
|
277 | - public static function image_medium($wpdb_row, $request, $controller) |
|
278 | - { |
|
279 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'medium'); |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - |
|
284 | - /** |
|
285 | - * Gets the medium-large image |
|
286 | - * |
|
287 | - * @param array $wpdb_row |
|
288 | - * @param \WP_REST_Request $request |
|
289 | - * @param Base $controller |
|
290 | - * @return array |
|
291 | - */ |
|
292 | - public static function image_medium_large($wpdb_row, $request, $controller) |
|
293 | - { |
|
294 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'medium_large'); |
|
295 | - } |
|
296 | - |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * Gets the large image |
|
301 | - * |
|
302 | - * @param array $wpdb_row |
|
303 | - * @param \WP_REST_Request $request |
|
304 | - * @param Base $controller |
|
305 | - * @return array |
|
306 | - */ |
|
307 | - public static function image_large($wpdb_row, $request, $controller) |
|
308 | - { |
|
309 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'large'); |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - |
|
314 | - /** |
|
315 | - * Gets the post-thumbnail image |
|
316 | - * |
|
317 | - * @param array $wpdb_row |
|
318 | - * @param \WP_REST_Request $request |
|
319 | - * @param Base $controller |
|
320 | - * @return array |
|
321 | - */ |
|
322 | - public static function image_post_thumbnail($wpdb_row, $request, $controller) |
|
323 | - { |
|
324 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'post-thumbnail'); |
|
325 | - } |
|
326 | - |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * Gets the full size image |
|
331 | - * |
|
332 | - * @param array $wpdb_row |
|
333 | - * @param \WP_REST_Request $request |
|
334 | - * @param Base $controller |
|
335 | - * @return array |
|
336 | - */ |
|
337 | - public static function image_full($wpdb_row, $request, $controller) |
|
338 | - { |
|
339 | - return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'full'); |
|
340 | - } |
|
341 | - |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * Gets image specs and formats them for the display in the API, |
|
346 | - * according to the image size requested |
|
347 | - * |
|
348 | - * @param int $EVT_ID |
|
349 | - * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
|
350 | - * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
|
351 | - */ |
|
352 | - protected static function _calculate_image_data($EVT_ID, $image_size) |
|
353 | - { |
|
354 | - $attachment_id = get_post_thumbnail_id($EVT_ID); |
|
355 | - $data = wp_get_attachment_image_src($attachment_id, $image_size); |
|
356 | - if (! $data) { |
|
357 | - return null; |
|
358 | - } |
|
359 | - if (isset($data[3])) { |
|
360 | - $generated = $data[3]; |
|
361 | - } else { |
|
362 | - $generated = true; |
|
363 | - } |
|
364 | - return array( |
|
365 | - 'url' => $data[0], |
|
366 | - 'width' => $data[1], |
|
367 | - 'height' => $data[2], |
|
368 | - 'generated' => $generated, |
|
369 | - ); |
|
370 | - } |
|
25 | + /** |
|
26 | + * Calculates the total spaces on the event (not subtracting sales, but taking |
|
27 | + * sales into account; so this is the optimum sales that CAN still be achieved) |
|
28 | + * See EE_Event::total_available_spaces( true ); |
|
29 | + * |
|
30 | + * @param array $wpdb_row |
|
31 | + * @param \WP_REST_Request $request |
|
32 | + * @param Base $controller |
|
33 | + * @return int |
|
34 | + * @throws \EE_Error |
|
35 | + */ |
|
36 | + public static function optimum_sales_at_start($wpdb_row, $request, $controller) |
|
37 | + { |
|
38 | + if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
39 | + $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
40 | + } else { |
|
41 | + $event_obj = null; |
|
42 | + } |
|
43 | + if ($event_obj instanceof \EE_Event) { |
|
44 | + return $event_obj->total_available_spaces(true); |
|
45 | + } else { |
|
46 | + throw new \EE_Error( |
|
47 | + sprintf( |
|
48 | + __('Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
|
49 | + 'event_espresso'), |
|
50 | + $wpdb_row['Event_CPT.ID'], |
|
51 | + print_r($wpdb_row, true) |
|
52 | + ) |
|
53 | + ); |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
|
61 | + * sales that COULD have been achieved) |
|
62 | + * See EE_Event::total_available_spaces( true ); |
|
63 | + * |
|
64 | + * @param array $wpdb_row |
|
65 | + * @param \WP_REST_Request $request |
|
66 | + * @param Base $controller |
|
67 | + * @return int |
|
68 | + * @throws \EE_Error |
|
69 | + */ |
|
70 | + public static function optimum_sales_now($wpdb_row, $request, $controller) |
|
71 | + { |
|
72 | + if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
73 | + $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
74 | + } else { |
|
75 | + $event_obj = null; |
|
76 | + } |
|
77 | + if ($event_obj instanceof \EE_Event) { |
|
78 | + return $event_obj->total_available_spaces(false); |
|
79 | + } else { |
|
80 | + throw new \EE_Error( |
|
81 | + sprintf( |
|
82 | + __('Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
|
83 | + 'event_espresso'), |
|
84 | + $wpdb_row['Event_CPT.ID'], |
|
85 | + print_r($wpdb_row, true) |
|
86 | + ) |
|
87 | + ); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Like optimum_sales_now, but minus total sales so far. |
|
95 | + * See EE_Event::spaces_remaining_for_sale( true ); |
|
96 | + * |
|
97 | + * @param array $wpdb_row |
|
98 | + * @param \WP_REST_Request $request |
|
99 | + * @param Base $controller |
|
100 | + * @return int |
|
101 | + * @throws \EE_Error |
|
102 | + */ |
|
103 | + public static function spaces_remaining($wpdb_row, $request, $controller) |
|
104 | + { |
|
105 | + if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID'])) { |
|
106 | + $event_obj = \EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
107 | + } else { |
|
108 | + $event_obj = null; |
|
109 | + } |
|
110 | + if ($event_obj instanceof \EE_Event) { |
|
111 | + return $event_obj->spaces_remaining_for_sale(); |
|
112 | + } else { |
|
113 | + throw new \EE_Error( |
|
114 | + sprintf( |
|
115 | + __('Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
|
116 | + 'event_espresso'), |
|
117 | + $wpdb_row['Event_CPT.ID'], |
|
118 | + print_r($wpdb_row, true) |
|
119 | + ) |
|
120 | + ); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Counts the number of approved registrations for this event (regardless |
|
128 | + * of how many datetimes each registrations' ticket purchase is for) |
|
129 | + * |
|
130 | + * @param array $wpdb_row |
|
131 | + * @param \WP_REST_Request $request |
|
132 | + * @param Base $controller |
|
133 | + * @return int |
|
134 | + * @throws \EE_Error |
|
135 | + */ |
|
136 | + public static function spots_taken($wpdb_row, $request, $controller) |
|
137 | + { |
|
138 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
139 | + throw new \EE_Error( |
|
140 | + sprintf( |
|
141 | + __('Cannot calculate spots_taken because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
142 | + 'event_espresso'), |
|
143 | + print_r($wpdb_row, true) |
|
144 | + ) |
|
145 | + ); |
|
146 | + } |
|
147 | + return \EEM_Registration::instance()->count( |
|
148 | + array( |
|
149 | + array( |
|
150 | + 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
151 | + 'STS_ID' => \EEM_Registration::status_id_approved, |
|
152 | + ), |
|
153 | + ), |
|
154 | + 'REG_ID', |
|
155 | + true |
|
156 | + ); |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * Counts the number of pending-payment registrations for this event (regardless |
|
163 | + * of how many datetimes each registrations' ticket purchase is for) |
|
164 | + * |
|
165 | + * @param array $wpdb_row |
|
166 | + * @param \WP_REST_Request $request |
|
167 | + * @param Base $controller |
|
168 | + * @return int |
|
169 | + * @throws \EE_Error |
|
170 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
171 | + */ |
|
172 | + public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
|
173 | + { |
|
174 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
175 | + throw new \EE_Error( |
|
176 | + sprintf( |
|
177 | + __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
178 | + 'event_espresso'), |
|
179 | + print_r($wpdb_row, true) |
|
180 | + ) |
|
181 | + ); |
|
182 | + } |
|
183 | + self::_verify_current_user_can('ee_read_registrations', 'spots_taken_pending_payment'); |
|
184 | + return \EEM_Registration::instance()->count( |
|
185 | + array( |
|
186 | + array( |
|
187 | + 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
188 | + 'STS_ID' => \EEM_Registration::status_id_pending_payment, |
|
189 | + ), |
|
190 | + ), |
|
191 | + 'REG_ID', |
|
192 | + true |
|
193 | + ); |
|
194 | + } |
|
195 | + |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * Counts all the registrations who have checked into one of this events' datetimes |
|
200 | + * See EE_Event::total_available_spaces( false ); |
|
201 | + * |
|
202 | + * @param array $wpdb_row |
|
203 | + * @param \WP_REST_Request $request |
|
204 | + * @param Base $controller |
|
205 | + * @return int|null if permission denied |
|
206 | + * @throws \EE_Error |
|
207 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
208 | + */ |
|
209 | + public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
|
210 | + { |
|
211 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
212 | + throw new \EE_Error( |
|
213 | + sprintf( |
|
214 | + __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
215 | + 'event_espresso'), |
|
216 | + print_r($wpdb_row, true) |
|
217 | + ) |
|
218 | + ); |
|
219 | + } |
|
220 | + self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_in_count'); |
|
221 | + return \EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + |
|
226 | + /** |
|
227 | + * Counts all the registrations who have checked out of one of this events' datetimes |
|
228 | + * See EE_Event::total_available_spaces( false ); |
|
229 | + * |
|
230 | + * @param array $wpdb_row |
|
231 | + * @param \WP_REST_Request $request |
|
232 | + * @param Base $controller |
|
233 | + * @return int |
|
234 | + * @throws \EE_Error |
|
235 | + * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
|
236 | + */ |
|
237 | + public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
|
238 | + { |
|
239 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
240 | + throw new \EE_Error( |
|
241 | + sprintf( |
|
242 | + __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
243 | + 'event_espresso'), |
|
244 | + print_r($wpdb_row, true) |
|
245 | + ) |
|
246 | + ); |
|
247 | + } |
|
248 | + self::_verify_current_user_can('ee_read_checkins', 'registrations_checked_out_count'); |
|
249 | + return \EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Gets the thumbnail image |
|
256 | + * |
|
257 | + * @param array $wpdb_row |
|
258 | + * @param \WP_REST_Request $request |
|
259 | + * @param Base $controller |
|
260 | + * @return array |
|
261 | + */ |
|
262 | + public static function image_thumbnail($wpdb_row, $request, $controller) |
|
263 | + { |
|
264 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'thumbnail'); |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * Gets the medium image |
|
271 | + * |
|
272 | + * @param array $wpdb_row |
|
273 | + * @param \WP_REST_Request $request |
|
274 | + * @param Base $controller |
|
275 | + * @return array |
|
276 | + */ |
|
277 | + public static function image_medium($wpdb_row, $request, $controller) |
|
278 | + { |
|
279 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'medium'); |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + |
|
284 | + /** |
|
285 | + * Gets the medium-large image |
|
286 | + * |
|
287 | + * @param array $wpdb_row |
|
288 | + * @param \WP_REST_Request $request |
|
289 | + * @param Base $controller |
|
290 | + * @return array |
|
291 | + */ |
|
292 | + public static function image_medium_large($wpdb_row, $request, $controller) |
|
293 | + { |
|
294 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'medium_large'); |
|
295 | + } |
|
296 | + |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * Gets the large image |
|
301 | + * |
|
302 | + * @param array $wpdb_row |
|
303 | + * @param \WP_REST_Request $request |
|
304 | + * @param Base $controller |
|
305 | + * @return array |
|
306 | + */ |
|
307 | + public static function image_large($wpdb_row, $request, $controller) |
|
308 | + { |
|
309 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'large'); |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + |
|
314 | + /** |
|
315 | + * Gets the post-thumbnail image |
|
316 | + * |
|
317 | + * @param array $wpdb_row |
|
318 | + * @param \WP_REST_Request $request |
|
319 | + * @param Base $controller |
|
320 | + * @return array |
|
321 | + */ |
|
322 | + public static function image_post_thumbnail($wpdb_row, $request, $controller) |
|
323 | + { |
|
324 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'post-thumbnail'); |
|
325 | + } |
|
326 | + |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * Gets the full size image |
|
331 | + * |
|
332 | + * @param array $wpdb_row |
|
333 | + * @param \WP_REST_Request $request |
|
334 | + * @param Base $controller |
|
335 | + * @return array |
|
336 | + */ |
|
337 | + public static function image_full($wpdb_row, $request, $controller) |
|
338 | + { |
|
339 | + return self::_calculate_image_data($wpdb_row['Event_CPT.ID'], 'full'); |
|
340 | + } |
|
341 | + |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * Gets image specs and formats them for the display in the API, |
|
346 | + * according to the image size requested |
|
347 | + * |
|
348 | + * @param int $EVT_ID |
|
349 | + * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
|
350 | + * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
|
351 | + */ |
|
352 | + protected static function _calculate_image_data($EVT_ID, $image_size) |
|
353 | + { |
|
354 | + $attachment_id = get_post_thumbnail_id($EVT_ID); |
|
355 | + $data = wp_get_attachment_image_src($attachment_id, $image_size); |
|
356 | + if (! $data) { |
|
357 | + return null; |
|
358 | + } |
|
359 | + if (isset($data[3])) { |
|
360 | + $generated = $data[3]; |
|
361 | + } else { |
|
362 | + $generated = true; |
|
363 | + } |
|
364 | + return array( |
|
365 | + 'url' => $data[0], |
|
366 | + 'width' => $data[1], |
|
367 | + 'height' => $data[2], |
|
368 | + 'generated' => $generated, |
|
369 | + ); |
|
370 | + } |
|
371 | 371 | } |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * @author Mike Nelson |
14 | 14 | * @since $VID:$ |
15 | 15 | */ |
16 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
16 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
17 | 17 | exit('No direct script access allowed'); |
18 | 18 | } |
19 | 19 | |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public static function spots_taken($wpdb_row, $request, $controller) |
137 | 137 | { |
138 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
138 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
139 | 139 | throw new \EE_Error( |
140 | 140 | sprintf( |
141 | 141 | __('Cannot calculate spots_taken because the database row %1$s does not have an entry for "Event_CPT.ID"', |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | */ |
172 | 172 | public static function spots_taken_pending_payment($wpdb_row, $request, $controller) |
173 | 173 | { |
174 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
174 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
175 | 175 | throw new \EE_Error( |
176 | 176 | sprintf( |
177 | 177 | __('Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | */ |
209 | 209 | public static function registrations_checked_in_count($wpdb_row, $request, $controller) |
210 | 210 | { |
211 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
211 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
212 | 212 | throw new \EE_Error( |
213 | 213 | sprintf( |
214 | 214 | __('Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | */ |
237 | 237 | public static function registrations_checked_out_count($wpdb_row, $request, $controller) |
238 | 238 | { |
239 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
239 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) { |
|
240 | 240 | throw new \EE_Error( |
241 | 241 | sprintf( |
242 | 242 | __('Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | { |
354 | 354 | $attachment_id = get_post_thumbnail_id($EVT_ID); |
355 | 355 | $data = wp_get_attachment_image_src($attachment_id, $image_size); |
356 | - if (! $data) { |
|
356 | + if ( ! $data) { |
|
357 | 357 | return null; |
358 | 358 | } |
359 | 359 | if (isset($data[3])) { |