|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\libraries\rest_api; |
|
3
|
|
|
|
|
4
|
|
|
use EEM_Base; |
|
5
|
|
|
use EventEspresso\core\libraries\rest_api\controllers\Base; |
|
6
|
|
|
use EEH_Inflector; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class CalculatedModelFields |
|
10
|
|
|
* Class for defining which model fields can be calculated, and performing those calculations |
|
11
|
|
|
* as requested |
|
12
|
|
|
* |
|
13
|
|
|
* @package Event Espresso |
|
14
|
|
|
* @subpackage |
|
15
|
|
|
* @author Mike Nelson |
|
16
|
|
|
* @since 4.8.35.rc.001 |
|
17
|
|
|
*/ |
|
18
|
|
|
if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
19
|
|
|
exit('No direct script access allowed'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class CalculatedModelFields |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $mapping; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param bool $refresh |
|
36
|
|
|
* @return array top-level-keys are model names (eg "Event") |
|
37
|
|
|
* next-level are the calculated field names AND method names on classes |
|
38
|
|
|
* which perform calculations, values are the fully qualified classnames which do the calculations |
|
39
|
|
|
* These callbacks should accept as arguments: |
|
40
|
|
|
* the wpdb row results, |
|
41
|
|
|
* the WP_Request object, |
|
42
|
|
|
* the controller object |
|
43
|
|
|
*/ |
|
44
|
|
|
public function mapping($refresh = false) |
|
45
|
|
|
{ |
|
46
|
|
|
if (! $this->mapping || $refresh) { |
|
|
|
|
|
|
47
|
|
|
$this->mapping = $this->generateNewMapping(); |
|
48
|
|
|
} |
|
49
|
|
|
return $this->mapping; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Generates anew mapping between model calculated fields and their callbacks |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function generateNewMapping() |
|
60
|
|
|
{ |
|
61
|
|
|
$rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
|
62
|
|
|
$event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
63
|
|
|
$datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
64
|
|
|
$registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
65
|
|
|
return apply_filters( |
|
66
|
|
|
'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
67
|
|
|
array( |
|
68
|
|
|
'Event' => array( |
|
69
|
|
|
'optimum_sales_at_start' => $event_calculations_class, |
|
70
|
|
|
'optimum_sales_now' => $event_calculations_class, |
|
71
|
|
|
'spots_taken' => $event_calculations_class, |
|
72
|
|
|
'spots_taken_pending_payment' => $event_calculations_class, |
|
73
|
|
|
'spaces_remaining' => $event_calculations_class, |
|
74
|
|
|
'registrations_checked_in_count' => $event_calculations_class, |
|
75
|
|
|
'registrations_checked_out_count' => $event_calculations_class, |
|
76
|
|
|
'image_thumbnail' => $event_calculations_class, |
|
77
|
|
|
'image_medium' => $event_calculations_class, |
|
78
|
|
|
'image_medium_large' => $event_calculations_class, |
|
79
|
|
|
'image_large' => $event_calculations_class, |
|
80
|
|
|
'image_post_thumbnail' => $event_calculations_class, |
|
81
|
|
|
'image_full' => $event_calculations_class, |
|
82
|
|
|
), |
|
83
|
|
|
'Datetime' => array( |
|
84
|
|
|
'spaces_remaining_considering_tickets' => $datetime_calculations_class, |
|
85
|
|
|
'registrations_checked_in_count' => $datetime_calculations_class, |
|
86
|
|
|
'registrations_checked_out_count' => $datetime_calculations_class, |
|
87
|
|
|
'spots_taken_pending_payment' => $datetime_calculations_class, |
|
88
|
|
|
), |
|
89
|
|
|
'Registration' => array( |
|
90
|
|
|
'datetime_checkin_stati' => $registration_class, |
|
91
|
|
|
), |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Gets the known calculated fields for model |
|
100
|
|
|
* |
|
101
|
|
|
* @param EEM_Base $model |
|
102
|
|
|
* @return array allowable values for this field |
|
103
|
|
|
*/ |
|
104
|
|
|
public function retrieveCalculatedFieldsForModel(EEM_Base $model) |
|
105
|
|
|
{ |
|
106
|
|
|
$mapping = $this->mapping(); |
|
107
|
|
|
if (isset($mapping[$model->get_this_model_name()])) { |
|
108
|
|
|
return array_keys($mapping[$model->get_this_model_name()]); |
|
109
|
|
|
} else { |
|
110
|
|
|
return array(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Retrieves the value for this calculation |
|
118
|
|
|
* |
|
119
|
|
|
* @param EEM_Base $model |
|
120
|
|
|
* @param string $field_name |
|
121
|
|
|
* @param array $wpdb_row |
|
122
|
|
|
* @param \WP_REST_Request |
|
123
|
|
|
* @param \EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
124
|
|
|
* @return mixed|null |
|
125
|
|
|
* @throws \EE_Error |
|
126
|
|
|
*/ |
|
127
|
|
|
public function retrieveCalculatedFieldValue( |
|
128
|
|
|
EEM_Base $model, |
|
129
|
|
|
$field_name, |
|
130
|
|
|
$wpdb_row, |
|
131
|
|
|
$rest_request, |
|
132
|
|
|
Base $controller |
|
133
|
|
|
) { |
|
134
|
|
|
$mapping = $this->mapping(); |
|
135
|
|
|
if (isset($mapping[$model->get_this_model_name()]) |
|
136
|
|
|
&& isset($mapping[$model->get_this_model_name()][$field_name]) |
|
137
|
|
|
) { |
|
138
|
|
|
$classname = $mapping[$model->get_this_model_name()][$field_name]; |
|
139
|
|
|
$class_method_name = EEH_Inflector::camelize_all_but_first($field_name); |
|
140
|
|
|
return call_user_func(array($classname, $class_method_name), $wpdb_row, $rest_request, $controller); |
|
141
|
|
|
} |
|
142
|
|
|
throw new RestException( |
|
143
|
|
|
'calculated_field_does_not_exist', |
|
144
|
|
|
sprintf( |
|
145
|
|
|
__('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
146
|
|
|
$field_name, |
|
147
|
|
|
$model->get_this_model_name() |
|
148
|
|
|
) |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.