@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | use EEM_CPT_Base; |
22 | 22 | |
23 | 23 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
24 | - exit('No direct script access allowed'); |
|
24 | + exit('No direct script access allowed'); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | |
@@ -39,1364 +39,1358 @@ discard block |
||
39 | 39 | |
40 | 40 | |
41 | 41 | |
42 | - /** |
|
43 | - * @var CalculatedModelFields |
|
44 | - */ |
|
45 | - protected $fields_calculator; |
|
42 | + /** |
|
43 | + * @var CalculatedModelFields |
|
44 | + */ |
|
45 | + protected $fields_calculator; |
|
46 | 46 | |
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * Read constructor. |
|
51 | - */ |
|
52 | - public function __construct() |
|
53 | - { |
|
54 | - parent::__construct(); |
|
55 | - $this->fields_calculator = new CalculatedModelFields(); |
|
56 | - } |
|
49 | + /** |
|
50 | + * Read constructor. |
|
51 | + */ |
|
52 | + public function __construct() |
|
53 | + { |
|
54 | + parent::__construct(); |
|
55 | + $this->fields_calculator = new CalculatedModelFields(); |
|
56 | + } |
|
57 | 57 | |
58 | 58 | |
59 | 59 | |
60 | - /** |
|
61 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
62 | - |
|
63 | - * |
|
60 | + /** |
|
61 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
62 | + * |
|
64 | 63 | *@param WP_REST_Request $request |
65 | - * @param string $version |
|
66 | - * @param string $model_name |
|
67 | - * @return \WP_REST_Response|WP_Error |
|
68 | - */ |
|
69 | - public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
70 | - { |
|
71 | - $controller = new Read(); |
|
72 | - try { |
|
73 | - $controller->setRequestedVersion($version); |
|
74 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
75 | - return $controller->sendResponse( |
|
76 | - new WP_Error( |
|
77 | - 'endpoint_parsing_error', |
|
78 | - sprintf( |
|
79 | - __( |
|
80 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
81 | - 'event_espresso' |
|
82 | - ), |
|
83 | - $model_name |
|
84 | - ) |
|
85 | - ) |
|
86 | - ); |
|
87 | - } |
|
88 | - return $controller->sendResponse( |
|
89 | - $controller->getEntitiesFromModel( |
|
90 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
91 | - $request |
|
92 | - ) |
|
93 | - ); |
|
94 | - } catch (Exception $e) { |
|
95 | - return $controller->sendResponse($e); |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Prepares and returns schema for any OPTIONS request. |
|
103 | - * |
|
104 | - * @param string $version The API endpoint version being used. |
|
105 | - * @param string $model_name Something like `Event` or `Registration` |
|
106 | - * @return array |
|
107 | - */ |
|
108 | - public static function handleSchemaRequest($version, $model_name) |
|
109 | - { |
|
110 | - $controller = new Read(); |
|
111 | - try { |
|
112 | - $controller->setRequestedVersion($version); |
|
113 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
114 | - return array(); |
|
115 | - } |
|
116 | - //get the model for this version |
|
117 | - $model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
118 | - $model_schema = new JsonModelSchema($model); |
|
119 | - return $model_schema->getModelSchemaForRelations( |
|
120 | - $controller->getModelVersionInfo()->relationSettings($model), |
|
121 | - $controller->customizeSchemaForRestResponse( |
|
122 | - $model, |
|
123 | - $model_schema->getModelSchemaForFields( |
|
124 | - $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
|
125 | - $model_schema->getInitialSchemaStructure() |
|
126 | - ) |
|
127 | - ) |
|
128 | - ); |
|
129 | - } catch (Exception $e) { |
|
130 | - return array(); |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * This loops through each field in the given schema for the model and does the following: |
|
138 | - * - add any extra fields that are REST API specific and related to existing fields. |
|
139 | - * - transform default values into the correct format for a REST API response. |
|
140 | - * |
|
141 | - * @param EEM_Base $model |
|
142 | - * @param array $schema |
|
143 | - * @return array The final schema. |
|
144 | - */ |
|
145 | - protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
|
146 | - { |
|
147 | - foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
|
148 | - $schema = $this->translateDefaultsForRestResponse( |
|
149 | - $field_name, |
|
150 | - $field, |
|
151 | - $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
|
152 | - ); |
|
153 | - } |
|
154 | - return $schema; |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
161 | - * response. |
|
162 | - * |
|
163 | - * @param $field_name |
|
164 | - * @param EE_Model_Field_Base $field |
|
165 | - * @param array $schema |
|
166 | - * @return array |
|
167 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
168 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
169 | - */ |
|
170 | - protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
|
171 | - { |
|
172 | - if (isset($schema['properties'][$field_name]['default'])) { |
|
173 | - if (is_array($schema['properties'][$field_name]['default'])) { |
|
174 | - foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
175 | - if ($default_key === 'raw') { |
|
176 | - $schema['properties'][$field_name]['default'][$default_key] = |
|
177 | - ModelDataTranslator::prepareFieldValueForJson( |
|
178 | - $field, |
|
179 | - $default_value, |
|
180 | - $this->getModelVersionInfo()->requestedVersion() |
|
181 | - ); |
|
182 | - } |
|
183 | - } |
|
184 | - } else { |
|
185 | - $schema['properties'][$field_name]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
186 | - $field, |
|
187 | - $schema['properties'][$field_name]['default'], |
|
188 | - $this->getModelVersionInfo()->requestedVersion() |
|
189 | - ); |
|
190 | - } |
|
191 | - } |
|
192 | - return $schema; |
|
193 | - } |
|
194 | - |
|
195 | - |
|
196 | - |
|
197 | - /** |
|
198 | - * Adds additional fields to the schema |
|
199 | - * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
200 | - * needs to be added to the schema. |
|
201 | - * |
|
202 | - * @param $field_name |
|
203 | - * @param EE_Model_Field_Base $field |
|
204 | - * @param array $schema |
|
205 | - * @return array |
|
206 | - */ |
|
207 | - protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
|
208 | - { |
|
209 | - if ($field instanceof EE_Datetime_Field) { |
|
210 | - $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
211 | - //modify the description |
|
212 | - $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
213 | - esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
214 | - $field->get_nicename() |
|
215 | - ); |
|
216 | - } |
|
217 | - return $schema; |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - |
|
222 | - /** |
|
223 | - * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
224 | - * |
|
225 | - * @return string |
|
226 | - */ |
|
227 | - protected function getRouteFromRequest() |
|
228 | - { |
|
229 | - if (isset($GLOBALS['wp']) |
|
230 | - && $GLOBALS['wp'] instanceof \WP |
|
231 | - && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
232 | - ) { |
|
233 | - return $GLOBALS['wp']->query_vars['rest_route']; |
|
234 | - } else { |
|
235 | - return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
236 | - } |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * Gets a single entity related to the model indicated in the path and its id |
|
243 | - |
|
244 | - * |
|
64 | + * @param string $version |
|
65 | + * @param string $model_name |
|
66 | + * @return \WP_REST_Response|WP_Error |
|
67 | + */ |
|
68 | + public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
69 | + { |
|
70 | + $controller = new Read(); |
|
71 | + try { |
|
72 | + $controller->setRequestedVersion($version); |
|
73 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
74 | + return $controller->sendResponse( |
|
75 | + new WP_Error( |
|
76 | + 'endpoint_parsing_error', |
|
77 | + sprintf( |
|
78 | + __( |
|
79 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
80 | + 'event_espresso' |
|
81 | + ), |
|
82 | + $model_name |
|
83 | + ) |
|
84 | + ) |
|
85 | + ); |
|
86 | + } |
|
87 | + return $controller->sendResponse( |
|
88 | + $controller->getEntitiesFromModel( |
|
89 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
90 | + $request |
|
91 | + ) |
|
92 | + ); |
|
93 | + } catch (Exception $e) { |
|
94 | + return $controller->sendResponse($e); |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Prepares and returns schema for any OPTIONS request. |
|
102 | + * |
|
103 | + * @param string $version The API endpoint version being used. |
|
104 | + * @param string $model_name Something like `Event` or `Registration` |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + public static function handleSchemaRequest($version, $model_name) |
|
108 | + { |
|
109 | + $controller = new Read(); |
|
110 | + try { |
|
111 | + $controller->setRequestedVersion($version); |
|
112 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
113 | + return array(); |
|
114 | + } |
|
115 | + //get the model for this version |
|
116 | + $model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
117 | + $model_schema = new JsonModelSchema($model); |
|
118 | + return $model_schema->getModelSchemaForRelations( |
|
119 | + $controller->getModelVersionInfo()->relationSettings($model), |
|
120 | + $controller->customizeSchemaForRestResponse( |
|
121 | + $model, |
|
122 | + $model_schema->getModelSchemaForFields( |
|
123 | + $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
|
124 | + $model_schema->getInitialSchemaStructure() |
|
125 | + ) |
|
126 | + ) |
|
127 | + ); |
|
128 | + } catch (Exception $e) { |
|
129 | + return array(); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * This loops through each field in the given schema for the model and does the following: |
|
137 | + * - add any extra fields that are REST API specific and related to existing fields. |
|
138 | + * - transform default values into the correct format for a REST API response. |
|
139 | + * |
|
140 | + * @param EEM_Base $model |
|
141 | + * @param array $schema |
|
142 | + * @return array The final schema. |
|
143 | + */ |
|
144 | + protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
|
145 | + { |
|
146 | + foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
|
147 | + $schema = $this->translateDefaultsForRestResponse( |
|
148 | + $field_name, |
|
149 | + $field, |
|
150 | + $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
|
151 | + ); |
|
152 | + } |
|
153 | + return $schema; |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
160 | + * response. |
|
161 | + * |
|
162 | + * @param $field_name |
|
163 | + * @param EE_Model_Field_Base $field |
|
164 | + * @param array $schema |
|
165 | + * @return array |
|
166 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
167 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
168 | + */ |
|
169 | + protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
|
170 | + { |
|
171 | + if (isset($schema['properties'][$field_name]['default'])) { |
|
172 | + if (is_array($schema['properties'][$field_name]['default'])) { |
|
173 | + foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
174 | + if ($default_key === 'raw') { |
|
175 | + $schema['properties'][$field_name]['default'][$default_key] = |
|
176 | + ModelDataTranslator::prepareFieldValueForJson( |
|
177 | + $field, |
|
178 | + $default_value, |
|
179 | + $this->getModelVersionInfo()->requestedVersion() |
|
180 | + ); |
|
181 | + } |
|
182 | + } |
|
183 | + } else { |
|
184 | + $schema['properties'][$field_name]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
185 | + $field, |
|
186 | + $schema['properties'][$field_name]['default'], |
|
187 | + $this->getModelVersionInfo()->requestedVersion() |
|
188 | + ); |
|
189 | + } |
|
190 | + } |
|
191 | + return $schema; |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + |
|
196 | + /** |
|
197 | + * Adds additional fields to the schema |
|
198 | + * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
199 | + * needs to be added to the schema. |
|
200 | + * |
|
201 | + * @param $field_name |
|
202 | + * @param EE_Model_Field_Base $field |
|
203 | + * @param array $schema |
|
204 | + * @return array |
|
205 | + */ |
|
206 | + protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
|
207 | + { |
|
208 | + if ($field instanceof EE_Datetime_Field) { |
|
209 | + $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
210 | + //modify the description |
|
211 | + $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
212 | + esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
213 | + $field->get_nicename() |
|
214 | + ); |
|
215 | + } |
|
216 | + return $schema; |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
223 | + * |
|
224 | + * @return string |
|
225 | + */ |
|
226 | + protected function getRouteFromRequest() |
|
227 | + { |
|
228 | + if (isset($GLOBALS['wp']) |
|
229 | + && $GLOBALS['wp'] instanceof \WP |
|
230 | + && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
231 | + ) { |
|
232 | + return $GLOBALS['wp']->query_vars['rest_route']; |
|
233 | + } else { |
|
234 | + return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * Gets a single entity related to the model indicated in the path and its id |
|
242 | + * |
|
245 | 243 | *@param WP_REST_Request $request |
246 | - * @param string $version |
|
247 | - * @param string $model_name |
|
248 | - * @return \WP_REST_Response|WP_Error |
|
249 | - */ |
|
250 | - public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
251 | - { |
|
252 | - $controller = new Read(); |
|
253 | - try { |
|
254 | - $controller->setRequestedVersion($version); |
|
255 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
256 | - return $controller->sendResponse( |
|
257 | - new WP_Error( |
|
258 | - 'endpoint_parsing_error', |
|
259 | - sprintf( |
|
260 | - __( |
|
261 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
262 | - 'event_espresso' |
|
263 | - ), |
|
264 | - $model_name |
|
265 | - ) |
|
266 | - ) |
|
267 | - ); |
|
268 | - } |
|
269 | - return $controller->sendResponse( |
|
270 | - $controller->getEntityFromModel( |
|
271 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
272 | - $request |
|
273 | - ) |
|
274 | - ); |
|
275 | - } catch (Exception $e) { |
|
276 | - return $controller->sendResponse($e); |
|
277 | - } |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * Gets all the related entities (or if its a belongs-to relation just the one) |
|
284 | - * to the item with the given id |
|
285 | - |
|
286 | - * |
|
244 | + * @param string $version |
|
245 | + * @param string $model_name |
|
246 | + * @return \WP_REST_Response|WP_Error |
|
247 | + */ |
|
248 | + public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
249 | + { |
|
250 | + $controller = new Read(); |
|
251 | + try { |
|
252 | + $controller->setRequestedVersion($version); |
|
253 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
254 | + return $controller->sendResponse( |
|
255 | + new WP_Error( |
|
256 | + 'endpoint_parsing_error', |
|
257 | + sprintf( |
|
258 | + __( |
|
259 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
260 | + 'event_espresso' |
|
261 | + ), |
|
262 | + $model_name |
|
263 | + ) |
|
264 | + ) |
|
265 | + ); |
|
266 | + } |
|
267 | + return $controller->sendResponse( |
|
268 | + $controller->getEntityFromModel( |
|
269 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
270 | + $request |
|
271 | + ) |
|
272 | + ); |
|
273 | + } catch (Exception $e) { |
|
274 | + return $controller->sendResponse($e); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * Gets all the related entities (or if its a belongs-to relation just the one) |
|
282 | + * to the item with the given id |
|
283 | + * |
|
287 | 284 | *@param WP_REST_Request $request |
288 | - * @param string $version |
|
289 | - * @param string $model_name |
|
290 | - * @param string $related_model_name |
|
291 | - * @return \WP_REST_Response|WP_Error |
|
292 | - */ |
|
293 | - public static function handleRequestGetRelated( |
|
294 | - WP_REST_Request $request, |
|
295 | - $version, |
|
296 | - $model_name, |
|
297 | - $related_model_name |
|
298 | - ) { |
|
299 | - $controller = new Read(); |
|
300 | - try { |
|
301 | - $controller->setRequestedVersion($version); |
|
302 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
303 | - return $controller->sendResponse( |
|
304 | - new WP_Error( |
|
305 | - 'endpoint_parsing_error', |
|
306 | - sprintf( |
|
307 | - __( |
|
308 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
309 | - 'event_espresso' |
|
310 | - ), |
|
311 | - $model_name |
|
312 | - ) |
|
313 | - ) |
|
314 | - ); |
|
315 | - } |
|
316 | - $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
317 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
318 | - return $controller->sendResponse( |
|
319 | - new WP_Error( |
|
320 | - 'endpoint_parsing_error', |
|
321 | - sprintf( |
|
322 | - __( |
|
323 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
324 | - 'event_espresso' |
|
325 | - ), |
|
326 | - $related_model_name |
|
327 | - ) |
|
328 | - ) |
|
329 | - ); |
|
330 | - } |
|
331 | - return $controller->sendResponse( |
|
332 | - $controller->getEntitiesFromRelation( |
|
333 | - $request->get_param('id'), |
|
334 | - $main_model->related_settings_for($related_model_name), |
|
335 | - $request |
|
336 | - ) |
|
337 | - ); |
|
338 | - } catch (Exception $e) { |
|
339 | - return $controller->sendResponse($e); |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - |
|
345 | - /** |
|
346 | - * Gets a collection for the given model and filters |
|
347 | - |
|
348 | - * |
|
285 | + * @param string $version |
|
286 | + * @param string $model_name |
|
287 | + * @param string $related_model_name |
|
288 | + * @return \WP_REST_Response|WP_Error |
|
289 | + */ |
|
290 | + public static function handleRequestGetRelated( |
|
291 | + WP_REST_Request $request, |
|
292 | + $version, |
|
293 | + $model_name, |
|
294 | + $related_model_name |
|
295 | + ) { |
|
296 | + $controller = new Read(); |
|
297 | + try { |
|
298 | + $controller->setRequestedVersion($version); |
|
299 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
300 | + return $controller->sendResponse( |
|
301 | + new WP_Error( |
|
302 | + 'endpoint_parsing_error', |
|
303 | + sprintf( |
|
304 | + __( |
|
305 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
306 | + 'event_espresso' |
|
307 | + ), |
|
308 | + $model_name |
|
309 | + ) |
|
310 | + ) |
|
311 | + ); |
|
312 | + } |
|
313 | + $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
314 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
315 | + return $controller->sendResponse( |
|
316 | + new WP_Error( |
|
317 | + 'endpoint_parsing_error', |
|
318 | + sprintf( |
|
319 | + __( |
|
320 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
321 | + 'event_espresso' |
|
322 | + ), |
|
323 | + $related_model_name |
|
324 | + ) |
|
325 | + ) |
|
326 | + ); |
|
327 | + } |
|
328 | + return $controller->sendResponse( |
|
329 | + $controller->getEntitiesFromRelation( |
|
330 | + $request->get_param('id'), |
|
331 | + $main_model->related_settings_for($related_model_name), |
|
332 | + $request |
|
333 | + ) |
|
334 | + ); |
|
335 | + } catch (Exception $e) { |
|
336 | + return $controller->sendResponse($e); |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + |
|
341 | + |
|
342 | + /** |
|
343 | + * Gets a collection for the given model and filters |
|
344 | + * |
|
349 | 345 | *@param EEM_Base $model |
350 | - * @param WP_REST_Request $request |
|
351 | - * @return array|WP_Error |
|
352 | - */ |
|
353 | - public function getEntitiesFromModel($model, $request) |
|
354 | - { |
|
355 | - $query_params = $this->createModelQueryParams($model, $request->get_params()); |
|
356 | - if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
357 | - $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
358 | - return new WP_Error( |
|
359 | - sprintf('rest_%s_cannot_list', $model_name_plural), |
|
360 | - sprintf( |
|
361 | - __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
362 | - $model_name_plural, |
|
363 | - Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
364 | - ), |
|
365 | - array('status' => 403) |
|
366 | - ); |
|
367 | - } |
|
368 | - if (! $request->get_header('no_rest_headers')) { |
|
369 | - $this->setHeadersFromQueryParams($model, $query_params); |
|
370 | - } |
|
371 | - /** @type array $results */ |
|
372 | - $results = $model->get_all_wpdb_results($query_params); |
|
373 | - $nice_results = array(); |
|
374 | - foreach ($results as $result) { |
|
375 | - $nice_results[] = $this->createEntityFromWpdbResult( |
|
376 | - $model, |
|
377 | - $result, |
|
378 | - $request |
|
379 | - ); |
|
380 | - } |
|
381 | - return $nice_results; |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - |
|
386 | - /** |
|
387 | - * Gets the collection for given relation object |
|
388 | - * The same as Read::get_entities_from_model(), except if the relation |
|
389 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
390 | - * the join-model-object into the results |
|
391 | - * |
|
392 | - * @param array $primary_model_query_params query params for finding the item from which |
|
393 | - * relations will be based |
|
394 | - * @param \EE_Model_Relation_Base $relation |
|
395 | - * @param WP_REST_Request $request |
|
396 | - * @return WP_Error|array |
|
397 | - * @throws RestException |
|
398 | - */ |
|
399 | - protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
|
400 | - { |
|
401 | - $context = $this->validateContext($request->get_param('caps')); |
|
402 | - $model = $relation->get_this_model(); |
|
403 | - $related_model = $relation->get_other_model(); |
|
404 | - if (! isset($primary_model_query_params[0])) { |
|
405 | - $primary_model_query_params[0] = array(); |
|
406 | - } |
|
407 | - //check if they can access the 1st model object |
|
408 | - $primary_model_query_params = array( |
|
409 | - 0 => $primary_model_query_params[0], |
|
410 | - 'limit' => 1, |
|
411 | - ); |
|
412 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
413 | - $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
|
414 | - $primary_model_query_params |
|
415 | - ); |
|
416 | - } |
|
417 | - $restricted_query_params = $primary_model_query_params; |
|
418 | - $restricted_query_params['caps'] = $context; |
|
419 | - $this->setDebugInfo('main model query params', $restricted_query_params); |
|
420 | - $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
|
421 | - if (! ( |
|
422 | - Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
|
423 | - && $model->exists($restricted_query_params) |
|
424 | - ) |
|
425 | - ) { |
|
426 | - if ($relation instanceof EE_Belongs_To_Relation) { |
|
427 | - $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
428 | - } else { |
|
429 | - $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
|
430 | - $related_model->get_this_model_name() |
|
431 | - ); |
|
432 | - } |
|
433 | - return new WP_Error( |
|
434 | - sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
435 | - sprintf( |
|
436 | - __( |
|
437 | - 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
438 | - 'event_espresso' |
|
439 | - ), |
|
440 | - $related_model_name_maybe_plural, |
|
441 | - $relation->get_this_model()->get_this_model_name(), |
|
442 | - implode( |
|
443 | - ',', |
|
444 | - array_keys( |
|
445 | - Capabilities::getMissingPermissions($related_model, $context) |
|
446 | - ) |
|
447 | - ) |
|
448 | - ), |
|
449 | - array('status' => 403) |
|
450 | - ); |
|
451 | - } |
|
452 | - $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
|
453 | - foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
454 | - $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
455 | - . '.' |
|
456 | - . $where_condition_key] = $where_condition_value; |
|
457 | - } |
|
458 | - $query_params['default_where_conditions'] = 'none'; |
|
459 | - $query_params['caps'] = $context; |
|
460 | - if (! $request->get_header('no_rest_headers')) { |
|
461 | - $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
|
462 | - } |
|
463 | - /** @type array $results */ |
|
464 | - $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
465 | - $nice_results = array(); |
|
466 | - foreach ($results as $result) { |
|
467 | - $nice_result = $this->createEntityFromWpdbResult( |
|
468 | - $relation->get_other_model(), |
|
469 | - $result, |
|
470 | - $request |
|
471 | - ); |
|
472 | - if ($relation instanceof \EE_HABTM_Relation) { |
|
473 | - //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
474 | - //if there are conflicts we prefer the properties from the main model |
|
475 | - $join_model_result = $this->createEntityFromWpdbResult( |
|
476 | - $relation->get_join_model(), |
|
477 | - $result, |
|
478 | - $request |
|
479 | - ); |
|
480 | - $joined_result = array_merge($nice_result, $join_model_result); |
|
481 | - //but keep the meta stuff from the main model |
|
482 | - if (isset($nice_result['meta'])) { |
|
483 | - $joined_result['meta'] = $nice_result['meta']; |
|
484 | - } |
|
485 | - $nice_result = $joined_result; |
|
486 | - } |
|
487 | - $nice_results[] = $nice_result; |
|
488 | - } |
|
489 | - if ($relation instanceof EE_Belongs_To_Relation) { |
|
490 | - return array_shift($nice_results); |
|
491 | - } else { |
|
492 | - return $nice_results; |
|
493 | - } |
|
494 | - } |
|
495 | - |
|
496 | - |
|
497 | - |
|
498 | - /** |
|
499 | - * Gets the collection for given relation object |
|
500 | - * The same as Read::get_entities_from_model(), except if the relation |
|
501 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
502 | - * the join-model-object into the results |
|
503 | - |
|
504 | - * |
|
346 | + * @param WP_REST_Request $request |
|
347 | + * @return array|WP_Error |
|
348 | + */ |
|
349 | + public function getEntitiesFromModel($model, $request) |
|
350 | + { |
|
351 | + $query_params = $this->createModelQueryParams($model, $request->get_params()); |
|
352 | + if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
353 | + $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
354 | + return new WP_Error( |
|
355 | + sprintf('rest_%s_cannot_list', $model_name_plural), |
|
356 | + sprintf( |
|
357 | + __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
358 | + $model_name_plural, |
|
359 | + Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
360 | + ), |
|
361 | + array('status' => 403) |
|
362 | + ); |
|
363 | + } |
|
364 | + if (! $request->get_header('no_rest_headers')) { |
|
365 | + $this->setHeadersFromQueryParams($model, $query_params); |
|
366 | + } |
|
367 | + /** @type array $results */ |
|
368 | + $results = $model->get_all_wpdb_results($query_params); |
|
369 | + $nice_results = array(); |
|
370 | + foreach ($results as $result) { |
|
371 | + $nice_results[] = $this->createEntityFromWpdbResult( |
|
372 | + $model, |
|
373 | + $result, |
|
374 | + $request |
|
375 | + ); |
|
376 | + } |
|
377 | + return $nice_results; |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * Gets the collection for given relation object |
|
384 | + * The same as Read::get_entities_from_model(), except if the relation |
|
385 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
386 | + * the join-model-object into the results |
|
387 | + * |
|
388 | + * @param array $primary_model_query_params query params for finding the item from which |
|
389 | + * relations will be based |
|
390 | + * @param \EE_Model_Relation_Base $relation |
|
391 | + * @param WP_REST_Request $request |
|
392 | + * @return WP_Error|array |
|
393 | + * @throws RestException |
|
394 | + */ |
|
395 | + protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
|
396 | + { |
|
397 | + $context = $this->validateContext($request->get_param('caps')); |
|
398 | + $model = $relation->get_this_model(); |
|
399 | + $related_model = $relation->get_other_model(); |
|
400 | + if (! isset($primary_model_query_params[0])) { |
|
401 | + $primary_model_query_params[0] = array(); |
|
402 | + } |
|
403 | + //check if they can access the 1st model object |
|
404 | + $primary_model_query_params = array( |
|
405 | + 0 => $primary_model_query_params[0], |
|
406 | + 'limit' => 1, |
|
407 | + ); |
|
408 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
409 | + $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
|
410 | + $primary_model_query_params |
|
411 | + ); |
|
412 | + } |
|
413 | + $restricted_query_params = $primary_model_query_params; |
|
414 | + $restricted_query_params['caps'] = $context; |
|
415 | + $this->setDebugInfo('main model query params', $restricted_query_params); |
|
416 | + $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
|
417 | + if (! ( |
|
418 | + Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
|
419 | + && $model->exists($restricted_query_params) |
|
420 | + ) |
|
421 | + ) { |
|
422 | + if ($relation instanceof EE_Belongs_To_Relation) { |
|
423 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
424 | + } else { |
|
425 | + $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
|
426 | + $related_model->get_this_model_name() |
|
427 | + ); |
|
428 | + } |
|
429 | + return new WP_Error( |
|
430 | + sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
431 | + sprintf( |
|
432 | + __( |
|
433 | + 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
434 | + 'event_espresso' |
|
435 | + ), |
|
436 | + $related_model_name_maybe_plural, |
|
437 | + $relation->get_this_model()->get_this_model_name(), |
|
438 | + implode( |
|
439 | + ',', |
|
440 | + array_keys( |
|
441 | + Capabilities::getMissingPermissions($related_model, $context) |
|
442 | + ) |
|
443 | + ) |
|
444 | + ), |
|
445 | + array('status' => 403) |
|
446 | + ); |
|
447 | + } |
|
448 | + $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
|
449 | + foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
450 | + $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
451 | + . '.' |
|
452 | + . $where_condition_key] = $where_condition_value; |
|
453 | + } |
|
454 | + $query_params['default_where_conditions'] = 'none'; |
|
455 | + $query_params['caps'] = $context; |
|
456 | + if (! $request->get_header('no_rest_headers')) { |
|
457 | + $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
|
458 | + } |
|
459 | + /** @type array $results */ |
|
460 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
461 | + $nice_results = array(); |
|
462 | + foreach ($results as $result) { |
|
463 | + $nice_result = $this->createEntityFromWpdbResult( |
|
464 | + $relation->get_other_model(), |
|
465 | + $result, |
|
466 | + $request |
|
467 | + ); |
|
468 | + if ($relation instanceof \EE_HABTM_Relation) { |
|
469 | + //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
470 | + //if there are conflicts we prefer the properties from the main model |
|
471 | + $join_model_result = $this->createEntityFromWpdbResult( |
|
472 | + $relation->get_join_model(), |
|
473 | + $result, |
|
474 | + $request |
|
475 | + ); |
|
476 | + $joined_result = array_merge($nice_result, $join_model_result); |
|
477 | + //but keep the meta stuff from the main model |
|
478 | + if (isset($nice_result['meta'])) { |
|
479 | + $joined_result['meta'] = $nice_result['meta']; |
|
480 | + } |
|
481 | + $nice_result = $joined_result; |
|
482 | + } |
|
483 | + $nice_results[] = $nice_result; |
|
484 | + } |
|
485 | + if ($relation instanceof EE_Belongs_To_Relation) { |
|
486 | + return array_shift($nice_results); |
|
487 | + } else { |
|
488 | + return $nice_results; |
|
489 | + } |
|
490 | + } |
|
491 | + |
|
492 | + |
|
493 | + |
|
494 | + /** |
|
495 | + * Gets the collection for given relation object |
|
496 | + * The same as Read::get_entities_from_model(), except if the relation |
|
497 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
498 | + * the join-model-object into the results |
|
499 | + * |
|
505 | 500 | *@param string $id the ID of the thing we are fetching related stuff from |
506 | - * @param \EE_Model_Relation_Base $relation |
|
507 | - * @param WP_REST_Request $request |
|
508 | - * @return array|WP_Error |
|
509 | - * @throws EE_Error |
|
510 | - */ |
|
511 | - public function getEntitiesFromRelation($id, $relation, $request) |
|
512 | - { |
|
513 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
514 | - throw new EE_Error( |
|
515 | - sprintf( |
|
516 | - __( |
|
517 | - // @codingStandardsIgnoreStart |
|
518 | - 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
519 | - // @codingStandardsIgnoreEnd |
|
520 | - 'event_espresso' |
|
521 | - ), |
|
522 | - $relation->get_this_model()->get_this_model_name() |
|
523 | - ) |
|
524 | - ); |
|
525 | - } |
|
526 | - return $this->getEntitiesFromRelationUsingModelQueryParams( |
|
527 | - array( |
|
528 | - array( |
|
529 | - $relation->get_this_model()->primary_key_name() => $id, |
|
530 | - ), |
|
531 | - ), |
|
532 | - $relation, |
|
533 | - $request |
|
534 | - ); |
|
535 | - } |
|
536 | - |
|
537 | - |
|
538 | - |
|
539 | - /** |
|
540 | - * Sets the headers that are based on the model and query params, |
|
541 | - * like the total records. This should only be called on the original request |
|
542 | - * from the client, not on subsequent internal |
|
543 | - * |
|
544 | - * @param EEM_Base $model |
|
545 | - * @param array $query_params |
|
546 | - * @return void |
|
547 | - */ |
|
548 | - protected function setHeadersFromQueryParams($model, $query_params) |
|
549 | - { |
|
550 | - $this->setDebugInfo('model query params', $query_params); |
|
551 | - $this->setDebugInfo( |
|
552 | - 'missing caps', |
|
553 | - Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
554 | - ); |
|
555 | - //normally the limit to a 2-part array, where the 2nd item is the limit |
|
556 | - if (! isset($query_params['limit'])) { |
|
557 | - $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
558 | - } |
|
559 | - if (is_array($query_params['limit'])) { |
|
560 | - $limit_parts = $query_params['limit']; |
|
561 | - } else { |
|
562 | - $limit_parts = explode(',', $query_params['limit']); |
|
563 | - if (count($limit_parts) == 1) { |
|
564 | - $limit_parts = array(0, $limit_parts[0]); |
|
565 | - } |
|
566 | - } |
|
567 | - //remove the group by and having parts of the query, as those will |
|
568 | - //make the sql query return an array of values, instead of just a single value |
|
569 | - unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
570 | - $count = $model->count($query_params, null, true); |
|
571 | - $pages = $count / $limit_parts[1]; |
|
572 | - $this->setResponseHeader('Total', $count, false); |
|
573 | - $this->setResponseHeader('PageSize', $limit_parts[1], false); |
|
574 | - $this->setResponseHeader('TotalPages', ceil($pages), false); |
|
575 | - } |
|
576 | - |
|
577 | - |
|
578 | - |
|
579 | - /** |
|
580 | - * Changes database results into REST API entities |
|
581 | - * |
|
582 | - * @param EEM_Base $model |
|
583 | - * @param array $db_row like results from $wpdb->get_results() |
|
584 | - * @param WP_REST_Request $rest_request |
|
585 | - * @param string $deprecated no longer used |
|
586 | - * @return array ready for being converted into json for sending to client |
|
587 | - */ |
|
588 | - public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
|
589 | - { |
|
590 | - if (! $rest_request instanceof WP_REST_Request) { |
|
591 | - //ok so this was called in the old style, where the 3rd arg was |
|
592 | - //$include, and the 4th arg was $context |
|
593 | - //now setup the request just to avoid fatal errors, although we won't be able |
|
594 | - //to truly make use of it because it's kinda devoid of info |
|
595 | - $rest_request = new WP_REST_Request(); |
|
596 | - $rest_request->set_param('include', $rest_request); |
|
597 | - $rest_request->set_param('caps', $deprecated); |
|
598 | - } |
|
599 | - if ($rest_request->get_param('caps') == null) { |
|
600 | - $rest_request->set_param('caps', EEM_Base::caps_read); |
|
601 | - } |
|
602 | - $entity_array = $this->createBareEntityFromWpdbResults($model, $db_row); |
|
603 | - $entity_array = $this->addExtraFields($model, $db_row, $entity_array); |
|
604 | - $entity_array['_links'] = $this->getEntityLinks($model, $db_row, $entity_array); |
|
605 | - $entity_array['_calculated_fields'] = $this->getEntityCalculations($model, $db_row, $rest_request); |
|
606 | - $entity_array = apply_filters( |
|
607 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
608 | - $entity_array, |
|
609 | - $model, |
|
610 | - $rest_request->get_param('caps'), |
|
611 | - $rest_request, |
|
612 | - $this |
|
613 | - ); |
|
614 | - $entity_array = $this->includeRequestedModels($model, $rest_request, $entity_array, $db_row); |
|
615 | - $entity_array = apply_filters( |
|
616 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
617 | - $entity_array, |
|
618 | - $model, |
|
619 | - $rest_request->get_param('caps'), |
|
620 | - $rest_request, |
|
621 | - $this |
|
622 | - ); |
|
623 | - $result_without_inaccessible_fields = Capabilities::filterOutInaccessibleEntityFields( |
|
624 | - $entity_array, |
|
625 | - $model, |
|
626 | - $rest_request->get_param('caps'), |
|
627 | - $this->getModelVersionInfo(), |
|
628 | - $model->get_index_primary_key_string( |
|
629 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
630 | - ) |
|
631 | - ); |
|
632 | - $this->setDebugInfo( |
|
633 | - 'inaccessible fields', |
|
634 | - array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
635 | - ); |
|
636 | - return apply_filters( |
|
637 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
638 | - $result_without_inaccessible_fields, |
|
639 | - $model, |
|
640 | - $rest_request->get_param('caps') |
|
641 | - ); |
|
642 | - } |
|
643 | - |
|
644 | - |
|
645 | - |
|
646 | - /** |
|
647 | - * Creates a REST entity array (JSON object we're going to return in the response, but |
|
648 | - * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
649 | - * from $wpdb->get_row( $sql, ARRAY_A) |
|
650 | - * |
|
651 | - * @param EEM_Base $model |
|
652 | - * @param array $db_row |
|
653 | - * @return array entity mostly ready for converting to JSON and sending in the response |
|
654 | - * |
|
655 | - */ |
|
656 | - protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
|
657 | - { |
|
658 | - $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
659 | - $result = array_intersect_key( |
|
660 | - $result, |
|
661 | - $this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) |
|
662 | - ); |
|
663 | - //if this is a CPT, we need to set the global $post to it, |
|
664 | - //otherwise shortcodes etc won't work properly while rendering it |
|
665 | - if ($model instanceof \EEM_CPT_Base) { |
|
666 | - $do_chevy_shuffle = true; |
|
667 | - } else { |
|
668 | - $do_chevy_shuffle = false; |
|
669 | - } |
|
670 | - if ($do_chevy_shuffle) { |
|
671 | - global $post; |
|
672 | - $old_post = $post; |
|
673 | - $post = get_post($result[$model->primary_key_name()]); |
|
674 | - if (! $post instanceof \WP_Post) { |
|
675 | - //well that's weird, because $result is what we JUST fetched from the database |
|
676 | - throw new RestException( |
|
677 | - 'error_fetching_post_from_database_results', |
|
678 | - esc_html__( |
|
679 | - 'An item was retrieved from the database but it\'s not a WP_Post like it should be.', |
|
680 | - 'event_espresso' |
|
681 | - ) |
|
682 | - ); |
|
683 | - } |
|
684 | - $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
685 | - $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
|
686 | - $model_object_classname, |
|
687 | - $result, |
|
688 | - false, |
|
689 | - false |
|
690 | - ); |
|
691 | - } |
|
692 | - foreach ($result as $field_name => $field_value) { |
|
693 | - $field_obj = $model->field_settings_for($field_name); |
|
694 | - if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
|
695 | - unset($result[$field_name]); |
|
696 | - } elseif ($this->isSubclassOfOne( |
|
697 | - $field_obj, |
|
698 | - $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
|
699 | - ) |
|
700 | - ) { |
|
701 | - $result[$field_name] = array( |
|
702 | - 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
703 | - 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
704 | - ); |
|
705 | - } elseif ($this->isSubclassOfOne( |
|
706 | - $field_obj, |
|
707 | - $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
|
708 | - ) |
|
709 | - ) { |
|
710 | - $result[$field_name] = array( |
|
711 | - 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
712 | - 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
713 | - ); |
|
714 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
715 | - $field_value = $field_obj->prepare_for_set_from_db($field_value); |
|
716 | - $timezone = $field_value->getTimezone(); |
|
717 | - $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
718 | - $result[$field_name . '_gmt'] = ModelDataTranslator::prepareFieldValuesForJson( |
|
719 | - $field_obj, |
|
720 | - $field_value, |
|
721 | - $this->getModelVersionInfo()->requestedVersion() |
|
722 | - ); |
|
723 | - $field_value->setTimezone($timezone); |
|
724 | - $result[$field_name] = ModelDataTranslator::prepareFieldValuesForJson( |
|
725 | - $field_obj, |
|
726 | - $field_value, |
|
727 | - $this->getModelVersionInfo()->requestedVersion() |
|
728 | - ); |
|
729 | - } else { |
|
730 | - $result[$field_name] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
731 | - } |
|
732 | - } |
|
733 | - if ($do_chevy_shuffle) { |
|
734 | - $post = $old_post; |
|
735 | - } |
|
736 | - return $result; |
|
737 | - } |
|
738 | - |
|
739 | - |
|
740 | - |
|
741 | - /** |
|
742 | - * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
743 | - * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
744 | - * representation using $field_obj->prepare_for_set_from_db()) |
|
745 | - * |
|
746 | - * @param EE_Model_Field_Base $field_obj |
|
747 | - * @param mixed $value as it's stored on a model object |
|
748 | - * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
749 | - * @return mixed |
|
750 | - * @throws ObjectDetectedException if $value contains a PHP object |
|
751 | - */ |
|
752 | - protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
753 | - { |
|
754 | - $value = $field_obj->prepare_for_set_from_db($value); |
|
755 | - switch ($format) { |
|
756 | - case 'pretty': |
|
757 | - $value = $field_obj->prepare_for_pretty_echoing($value); |
|
758 | - break; |
|
759 | - case 'normal': |
|
760 | - default: |
|
761 | - $value = $field_obj->prepare_for_get($value); |
|
762 | - break; |
|
763 | - } |
|
764 | - return ModelDataTranslator::prepareFieldValuesForJson( |
|
765 | - $field_obj, |
|
766 | - $value, |
|
767 | - $this->getModelVersionInfo()->requestedVersion() |
|
768 | - ); |
|
769 | - } |
|
770 | - |
|
771 | - |
|
772 | - |
|
773 | - /** |
|
774 | - * Adds a few extra fields to the entity response |
|
775 | - * |
|
776 | - * @param EEM_Base $model |
|
777 | - * @param array $db_row |
|
778 | - * @param array $entity_array |
|
779 | - * @return array modified entity |
|
780 | - */ |
|
781 | - protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
782 | - { |
|
783 | - if ($model instanceof EEM_CPT_Base) { |
|
784 | - $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
785 | - } |
|
786 | - return $entity_array; |
|
787 | - } |
|
788 | - |
|
789 | - |
|
790 | - |
|
791 | - /** |
|
792 | - * Gets links we want to add to the response |
|
793 | - * |
|
794 | - * @global \WP_REST_Server $wp_rest_server |
|
795 | - * @param EEM_Base $model |
|
796 | - * @param array $db_row |
|
797 | - * @param array $entity_array |
|
798 | - * @return array the _links item in the entity |
|
799 | - */ |
|
800 | - protected function getEntityLinks($model, $db_row, $entity_array) |
|
801 | - { |
|
802 | - //add basic links |
|
803 | - $links = array(); |
|
804 | - if ($model->has_primary_key_field()) { |
|
805 | - $links['self'] = array( |
|
806 | - array( |
|
807 | - 'href' => $this->getVersionedLinkTo( |
|
808 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
809 | - . '/' |
|
810 | - . $entity_array[$model->primary_key_name()] |
|
811 | - ), |
|
812 | - ), |
|
813 | - ); |
|
814 | - } |
|
815 | - $links['collection'] = array( |
|
816 | - array( |
|
817 | - 'href' => $this->getVersionedLinkTo( |
|
818 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
819 | - ), |
|
820 | - ), |
|
821 | - ); |
|
822 | - //add links to related models |
|
823 | - if ($model->has_primary_key_field()) { |
|
824 | - foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
825 | - $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
826 | - $links[EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
827 | - array( |
|
828 | - 'href' => $this->getVersionedLinkTo( |
|
829 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
830 | - . '/' |
|
831 | - . $entity_array[$model->primary_key_name()] |
|
832 | - . '/' |
|
833 | - . $related_model_part |
|
834 | - ), |
|
835 | - 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
836 | - ), |
|
837 | - ); |
|
838 | - } |
|
839 | - } |
|
840 | - return $links; |
|
841 | - } |
|
842 | - |
|
843 | - |
|
844 | - |
|
845 | - /** |
|
846 | - * Adds the included models indicated in the request to the entity provided |
|
847 | - * |
|
848 | - * @param EEM_Base $model |
|
849 | - * @param WP_REST_Request $rest_request |
|
850 | - * @param array $entity_array |
|
851 | - * @param array $db_row |
|
852 | - * @return array the modified entity |
|
853 | - */ |
|
854 | - protected function includeRequestedModels( |
|
855 | - EEM_Base $model, |
|
856 | - WP_REST_Request $rest_request, |
|
857 | - $entity_array, |
|
858 | - $db_row = array() |
|
859 | - ) { |
|
860 | - //if $db_row not included, hope the entity array has what we need |
|
861 | - if (! $db_row) { |
|
862 | - $db_row = $entity_array; |
|
863 | - } |
|
864 | - $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
865 | - $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
866 | - //if they passed in * or didn't specify any includes, return everything |
|
867 | - if (! in_array('*', $includes_for_this_model) |
|
868 | - && ! empty($includes_for_this_model) |
|
869 | - ) { |
|
870 | - if ($model->has_primary_key_field()) { |
|
871 | - //always include the primary key. ya just gotta know that at least |
|
872 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
873 | - } |
|
874 | - if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
875 | - $includes_for_this_model[] = '_calculated_fields'; |
|
876 | - } |
|
877 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
878 | - } |
|
879 | - $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
880 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
881 | - $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
882 | - $rest_request->get_param('include'), |
|
883 | - $relation_name |
|
884 | - ); |
|
885 | - $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
886 | - $rest_request->get_param('calculate'), |
|
887 | - $relation_name |
|
888 | - ); |
|
889 | - //did they specify they wanted to include a related model, or |
|
890 | - //specific fields from a related model? |
|
891 | - //or did they specify to calculate a field from a related model? |
|
892 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
893 | - //if so, we should include at least some part of the related model |
|
894 | - $pretend_related_request = new WP_REST_Request(); |
|
895 | - $pretend_related_request->set_query_params( |
|
896 | - array( |
|
897 | - 'caps' => $rest_request->get_param('caps'), |
|
898 | - 'include' => $related_fields_to_include, |
|
899 | - 'calculate' => $related_fields_to_calculate, |
|
900 | - ) |
|
901 | - ); |
|
902 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
903 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
904 | - $model->get_index_primary_key_string( |
|
905 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
906 | - ) |
|
907 | - ); |
|
908 | - $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
909 | - $primary_model_query_params, |
|
910 | - $relation_obj, |
|
911 | - $pretend_related_request |
|
912 | - ); |
|
913 | - $entity_array[Read::getRelatedEntityName($relation_name, $relation_obj)] = $related_results |
|
914 | - instanceof |
|
915 | - WP_Error |
|
916 | - ? null |
|
917 | - : $related_results; |
|
918 | - } |
|
919 | - } |
|
920 | - return $entity_array; |
|
921 | - } |
|
922 | - |
|
923 | - |
|
924 | - |
|
925 | - /** |
|
926 | - * Returns a new array with all the names of models removed. Eg |
|
927 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
928 | - * |
|
929 | - * @param array $arr |
|
930 | - * @return array |
|
931 | - */ |
|
932 | - private function removeModelNamesFromArray($arr) |
|
933 | - { |
|
934 | - return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
935 | - } |
|
936 | - |
|
937 | - |
|
938 | - |
|
939 | - /** |
|
940 | - * Gets the calculated fields for the response |
|
941 | - * |
|
942 | - * @param EEM_Base $model |
|
943 | - * @param array $wpdb_row |
|
944 | - * @param WP_REST_Request $rest_request |
|
945 | - * @return \stdClass the _calculations item in the entity |
|
946 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
947 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
948 | - */ |
|
949 | - protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
950 | - { |
|
951 | - $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
952 | - $rest_request->get_param('calculate'), |
|
953 | - '' |
|
954 | - ); |
|
955 | - //note: setting calculate=* doesn't do anything |
|
956 | - $calculated_fields_to_return = new \stdClass(); |
|
957 | - foreach ($calculated_fields as $field_to_calculate) { |
|
958 | - try { |
|
959 | - $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
960 | - null, |
|
961 | - $this->fields_calculator->retrieveCalculatedFieldValue( |
|
962 | - $model, |
|
963 | - $field_to_calculate, |
|
964 | - $wpdb_row, |
|
965 | - $rest_request, |
|
966 | - $this |
|
967 | - ), |
|
968 | - $this->getModelVersionInfo()->requestedVersion() |
|
969 | - ); |
|
970 | - } catch (RestException $e) { |
|
971 | - //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
972 | - $this->setResponseHeader( |
|
973 | - 'Notices-Field-Calculation-Errors[' |
|
974 | - . $e->getStringCode() |
|
975 | - . '][' |
|
976 | - . $model->get_this_model_name() |
|
977 | - . '][' |
|
978 | - . $field_to_calculate |
|
979 | - . ']', |
|
980 | - $e->getMessage(), |
|
981 | - true |
|
982 | - ); |
|
983 | - } |
|
984 | - } |
|
985 | - return $calculated_fields_to_return; |
|
986 | - } |
|
987 | - |
|
988 | - |
|
989 | - |
|
990 | - /** |
|
991 | - * Gets the full URL to the resource, taking the requested version into account |
|
992 | - * |
|
993 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
994 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
995 | - */ |
|
996 | - public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
997 | - { |
|
998 | - return rest_url( |
|
999 | - EED_Core_Rest_Api::get_versioned_route_to( |
|
1000 | - $link_part_after_version_and_slash, |
|
1001 | - $this->getModelVersionInfo()->requestedVersion() |
|
1002 | - ) |
|
1003 | - ); |
|
1004 | - } |
|
1005 | - |
|
1006 | - |
|
1007 | - |
|
1008 | - /** |
|
1009 | - * Gets the correct lowercase name for the relation in the API according |
|
1010 | - * to the relation's type |
|
1011 | - * |
|
1012 | - * @param string $relation_name |
|
1013 | - * @param \EE_Model_Relation_Base $relation_obj |
|
1014 | - * @return string |
|
1015 | - */ |
|
1016 | - public static function getRelatedEntityName($relation_name, $relation_obj) |
|
1017 | - { |
|
1018 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
1019 | - return strtolower($relation_name); |
|
1020 | - } else { |
|
1021 | - return EEH_Inflector::pluralize_and_lower($relation_name); |
|
1022 | - } |
|
1023 | - } |
|
1024 | - |
|
1025 | - |
|
1026 | - |
|
1027 | - /** |
|
1028 | - * Gets the one model object with the specified id for the specified model |
|
1029 | - * |
|
1030 | - * @param EEM_Base $model |
|
1031 | - * @param WP_REST_Request $request |
|
1032 | - * @return array|WP_Error |
|
1033 | - */ |
|
1034 | - public function getEntityFromModel($model, $request) |
|
1035 | - { |
|
1036 | - $context = $this->validateContext($request->get_param('caps')); |
|
1037 | - return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1038 | - } |
|
1039 | - |
|
1040 | - |
|
1041 | - |
|
1042 | - /** |
|
1043 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
1044 | - * version so just treat it as a default read |
|
1045 | - * |
|
1046 | - * @param string $context |
|
1047 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1048 | - */ |
|
1049 | - public function validateContext($context) |
|
1050 | - { |
|
1051 | - if (! $context) { |
|
1052 | - $context = EEM_Base::caps_read; |
|
1053 | - } |
|
1054 | - $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1055 | - if (in_array($context, $valid_contexts)) { |
|
1056 | - return $context; |
|
1057 | - } else { |
|
1058 | - return EEM_Base::caps_read; |
|
1059 | - } |
|
1060 | - } |
|
1061 | - |
|
1062 | - |
|
1063 | - |
|
1064 | - /** |
|
1065 | - * Verifies the passed in value is an allowable default where conditions value. |
|
1066 | - * |
|
1067 | - * @param $default_query_params |
|
1068 | - * @return string |
|
1069 | - */ |
|
1070 | - public function validateDefaultQueryParams($default_query_params) |
|
1071 | - { |
|
1072 | - $valid_default_where_conditions_for_api_calls = array( |
|
1073 | - EEM_Base::default_where_conditions_all, |
|
1074 | - EEM_Base::default_where_conditions_minimum_all, |
|
1075 | - EEM_Base::default_where_conditions_minimum_others, |
|
1076 | - ); |
|
1077 | - if (! $default_query_params) { |
|
1078 | - $default_query_params = EEM_Base::default_where_conditions_all; |
|
1079 | - } |
|
1080 | - if (in_array( |
|
1081 | - $default_query_params, |
|
1082 | - $valid_default_where_conditions_for_api_calls, |
|
1083 | - true |
|
1084 | - )) { |
|
1085 | - return $default_query_params; |
|
1086 | - } else { |
|
1087 | - return EEM_Base::default_where_conditions_all; |
|
1088 | - } |
|
1089 | - } |
|
1090 | - |
|
1091 | - |
|
1092 | - |
|
1093 | - /** |
|
1094 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1095 | - * Note: right now the query parameter keys for fields (and related fields) |
|
1096 | - * can be left as-is, but it's quite possible this will change someday. |
|
1097 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1098 | - * |
|
1099 | - * @param EEM_Base $model |
|
1100 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1101 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1102 | - * that absolutely no results should be returned |
|
1103 | - * @throws EE_Error |
|
1104 | - * @throws RestException |
|
1105 | - */ |
|
1106 | - public function createModelQueryParams($model, $query_parameters) |
|
1107 | - { |
|
1108 | - $model_query_params = array(); |
|
1109 | - if (isset($query_parameters['where'])) { |
|
1110 | - $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1111 | - $query_parameters['where'], |
|
1112 | - $model, |
|
1113 | - $this->getModelVersionInfo()->requestedVersion() |
|
1114 | - ); |
|
1115 | - } |
|
1116 | - if (isset($query_parameters['order_by'])) { |
|
1117 | - $order_by = $query_parameters['order_by']; |
|
1118 | - } elseif (isset($query_parameters['orderby'])) { |
|
1119 | - $order_by = $query_parameters['orderby']; |
|
1120 | - } else { |
|
1121 | - $order_by = null; |
|
1122 | - } |
|
1123 | - if ($order_by !== null) { |
|
1124 | - if (is_array($order_by)) { |
|
1125 | - $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1126 | - } else { |
|
1127 | - //it's a single item |
|
1128 | - $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1129 | - } |
|
1130 | - $model_query_params['order_by'] = $order_by; |
|
1131 | - } |
|
1132 | - if (isset($query_parameters['group_by'])) { |
|
1133 | - $group_by = $query_parameters['group_by']; |
|
1134 | - } elseif (isset($query_parameters['groupby'])) { |
|
1135 | - $group_by = $query_parameters['groupby']; |
|
1136 | - } else { |
|
1137 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1138 | - } |
|
1139 | - //make sure they're all real names |
|
1140 | - if (is_array($group_by)) { |
|
1141 | - $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1142 | - } |
|
1143 | - if ($group_by !== null) { |
|
1144 | - $model_query_params['group_by'] = $group_by; |
|
1145 | - } |
|
1146 | - if (isset($query_parameters['having'])) { |
|
1147 | - $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1148 | - $query_parameters['having'], |
|
1149 | - $model, |
|
1150 | - $this->getModelVersionInfo()->requestedVersion() |
|
1151 | - ); |
|
1152 | - } |
|
1153 | - if (isset($query_parameters['order'])) { |
|
1154 | - $model_query_params['order'] = $query_parameters['order']; |
|
1155 | - } |
|
1156 | - if (isset($query_parameters['mine'])) { |
|
1157 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1158 | - } |
|
1159 | - if (isset($query_parameters['limit'])) { |
|
1160 | - //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1161 | - if (! is_array($query_parameters['limit'])) { |
|
1162 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1163 | - } else { |
|
1164 | - $limit_array = $query_parameters['limit']; |
|
1165 | - } |
|
1166 | - $sanitized_limit = array(); |
|
1167 | - foreach ($limit_array as $key => $limit_part) { |
|
1168 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1169 | - throw new EE_Error( |
|
1170 | - sprintf( |
|
1171 | - __( |
|
1172 | - // @codingStandardsIgnoreStart |
|
1173 | - 'An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
1174 | - // @codingStandardsIgnoreEnd |
|
1175 | - 'event_espresso' |
|
1176 | - ), |
|
1177 | - wp_json_encode($query_parameters['limit']) |
|
1178 | - ) |
|
1179 | - ); |
|
1180 | - } |
|
1181 | - $sanitized_limit[] = (int)$limit_part; |
|
1182 | - } |
|
1183 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1184 | - } else { |
|
1185 | - $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1186 | - } |
|
1187 | - if (isset($query_parameters['caps'])) { |
|
1188 | - $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1189 | - } else { |
|
1190 | - $model_query_params['caps'] = EEM_Base::caps_read; |
|
1191 | - } |
|
1192 | - if (isset($query_parameters['default_where_conditions'])) { |
|
1193 | - $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1194 | - $query_parameters['default_where_conditions'] |
|
1195 | - ); |
|
1196 | - } |
|
1197 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1198 | - } |
|
1199 | - |
|
1200 | - |
|
1201 | - |
|
1202 | - /** |
|
1203 | - * Changes the REST-style query params for use in the models |
|
1204 | - * |
|
1205 | - * @deprecated |
|
1206 | - * @param EEM_Base $model |
|
1207 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1208 | - * @return array |
|
1209 | - */ |
|
1210 | - public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1211 | - { |
|
1212 | - $model_ready_query_params = array(); |
|
1213 | - foreach ($query_params as $key => $value) { |
|
1214 | - if (is_array($value)) { |
|
1215 | - $model_ready_query_params[$key] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1216 | - } else { |
|
1217 | - $model_ready_query_params[$key] = $value; |
|
1218 | - } |
|
1219 | - } |
|
1220 | - return $model_ready_query_params; |
|
1221 | - } |
|
1222 | - |
|
1223 | - |
|
1224 | - |
|
1225 | - /** |
|
1226 | - * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1227 | - * @param $model |
|
1228 | - * @param $query_params |
|
1229 | - * @return array |
|
1230 | - */ |
|
1231 | - public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1232 | - { |
|
1233 | - $model_ready_query_params = array(); |
|
1234 | - foreach ($query_params as $key => $value) { |
|
1235 | - if (is_array($value)) { |
|
1236 | - $model_ready_query_params[$key] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1237 | - } else { |
|
1238 | - $model_ready_query_params[$key] = $value; |
|
1239 | - } |
|
1240 | - } |
|
1241 | - return $model_ready_query_params; |
|
1242 | - } |
|
1243 | - |
|
1244 | - |
|
1245 | - |
|
1246 | - /** |
|
1247 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1248 | - * If no prefix is specified, returns items with no period. |
|
1249 | - * |
|
1250 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1251 | - * @param string $prefix "Event" or "foobar" |
|
1252 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1253 | - * we only return strings starting with that and a period; if no prefix was |
|
1254 | - * specified we return all items containing NO periods |
|
1255 | - */ |
|
1256 | - public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1257 | - { |
|
1258 | - if (is_string($string_to_explode)) { |
|
1259 | - $exploded_contents = explode(',', $string_to_explode); |
|
1260 | - } elseif (is_array($string_to_explode)) { |
|
1261 | - $exploded_contents = $string_to_explode; |
|
1262 | - } else { |
|
1263 | - $exploded_contents = array(); |
|
1264 | - } |
|
1265 | - //if the string was empty, we want an empty array |
|
1266 | - $exploded_contents = array_filter($exploded_contents); |
|
1267 | - $contents_with_prefix = array(); |
|
1268 | - foreach ($exploded_contents as $item) { |
|
1269 | - $item = trim($item); |
|
1270 | - //if no prefix was provided, so we look for items with no "." in them |
|
1271 | - if (! $prefix) { |
|
1272 | - //does this item have a period? |
|
1273 | - if (strpos($item, '.') === false) { |
|
1274 | - //if not, then its what we're looking for |
|
1275 | - $contents_with_prefix[] = $item; |
|
1276 | - } |
|
1277 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1278 | - //this item has the prefix and a period, grab it |
|
1279 | - $contents_with_prefix[] = substr( |
|
1280 | - $item, |
|
1281 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1282 | - ); |
|
1283 | - } elseif ($item === $prefix) { |
|
1284 | - //this item is JUST the prefix |
|
1285 | - //so let's grab everything after, which is a blank string |
|
1286 | - $contents_with_prefix[] = ''; |
|
1287 | - } |
|
1288 | - } |
|
1289 | - return $contents_with_prefix; |
|
1290 | - } |
|
1291 | - |
|
1292 | - |
|
1293 | - |
|
1294 | - /** |
|
1295 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1296 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1297 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1298 | - * array('*') (when you provided a model and a model of that kind was found). |
|
1299 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1300 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
1301 | - * name and then a period). |
|
1302 | - * @param string $include_string @see Read:handle_request_get_all |
|
1303 | - * @param string $model_name |
|
1304 | - * @return array of fields for this model. If $model_name is provided, then |
|
1305 | - * the fields for that model, with the model's name removed from each. |
|
1306 | - * If $include_string was blank or '*' returns an empty array |
|
1307 | - */ |
|
1308 | - public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1309 | - { |
|
1310 | - if (is_array($include_string)) { |
|
1311 | - $include_string = implode(',', $include_string); |
|
1312 | - } |
|
1313 | - if ($include_string === '*' || $include_string === '') { |
|
1314 | - return array(); |
|
1315 | - } |
|
1316 | - $includes = explode(',', $include_string); |
|
1317 | - $extracted_fields_to_include = array(); |
|
1318 | - if ($model_name) { |
|
1319 | - foreach ($includes as $field_to_include) { |
|
1320 | - $field_to_include = trim($field_to_include); |
|
1321 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1322 | - //found the model name at the exact start |
|
1323 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1324 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
1325 | - } elseif ($field_to_include == $model_name) { |
|
1326 | - $extracted_fields_to_include[] = '*'; |
|
1327 | - } |
|
1328 | - } |
|
1329 | - } else { |
|
1330 | - //look for ones with no period |
|
1331 | - foreach ($includes as $field_to_include) { |
|
1332 | - $field_to_include = trim($field_to_include); |
|
1333 | - if (strpos($field_to_include, '.') === false |
|
1334 | - && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1335 | - ) { |
|
1336 | - $extracted_fields_to_include[] = $field_to_include; |
|
1337 | - } |
|
1338 | - } |
|
1339 | - } |
|
1340 | - return $extracted_fields_to_include; |
|
1341 | - } |
|
1342 | - |
|
1343 | - |
|
1344 | - |
|
1345 | - /** |
|
1346 | - * Gets the single item using the model according to the request in the context given, otherwise |
|
1347 | - * returns that it's inaccessible to the current user |
|
1348 | - |
|
1349 | - * |
|
501 | + * @param \EE_Model_Relation_Base $relation |
|
502 | + * @param WP_REST_Request $request |
|
503 | + * @return array|WP_Error |
|
504 | + * @throws EE_Error |
|
505 | + */ |
|
506 | + public function getEntitiesFromRelation($id, $relation, $request) |
|
507 | + { |
|
508 | + if (! $relation->get_this_model()->has_primary_key_field()) { |
|
509 | + throw new EE_Error( |
|
510 | + sprintf( |
|
511 | + __( |
|
512 | + // @codingStandardsIgnoreStart |
|
513 | + 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
514 | + // @codingStandardsIgnoreEnd |
|
515 | + 'event_espresso' |
|
516 | + ), |
|
517 | + $relation->get_this_model()->get_this_model_name() |
|
518 | + ) |
|
519 | + ); |
|
520 | + } |
|
521 | + return $this->getEntitiesFromRelationUsingModelQueryParams( |
|
522 | + array( |
|
523 | + array( |
|
524 | + $relation->get_this_model()->primary_key_name() => $id, |
|
525 | + ), |
|
526 | + ), |
|
527 | + $relation, |
|
528 | + $request |
|
529 | + ); |
|
530 | + } |
|
531 | + |
|
532 | + |
|
533 | + |
|
534 | + /** |
|
535 | + * Sets the headers that are based on the model and query params, |
|
536 | + * like the total records. This should only be called on the original request |
|
537 | + * from the client, not on subsequent internal |
|
538 | + * |
|
539 | + * @param EEM_Base $model |
|
540 | + * @param array $query_params |
|
541 | + * @return void |
|
542 | + */ |
|
543 | + protected function setHeadersFromQueryParams($model, $query_params) |
|
544 | + { |
|
545 | + $this->setDebugInfo('model query params', $query_params); |
|
546 | + $this->setDebugInfo( |
|
547 | + 'missing caps', |
|
548 | + Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
549 | + ); |
|
550 | + //normally the limit to a 2-part array, where the 2nd item is the limit |
|
551 | + if (! isset($query_params['limit'])) { |
|
552 | + $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
553 | + } |
|
554 | + if (is_array($query_params['limit'])) { |
|
555 | + $limit_parts = $query_params['limit']; |
|
556 | + } else { |
|
557 | + $limit_parts = explode(',', $query_params['limit']); |
|
558 | + if (count($limit_parts) == 1) { |
|
559 | + $limit_parts = array(0, $limit_parts[0]); |
|
560 | + } |
|
561 | + } |
|
562 | + //remove the group by and having parts of the query, as those will |
|
563 | + //make the sql query return an array of values, instead of just a single value |
|
564 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
565 | + $count = $model->count($query_params, null, true); |
|
566 | + $pages = $count / $limit_parts[1]; |
|
567 | + $this->setResponseHeader('Total', $count, false); |
|
568 | + $this->setResponseHeader('PageSize', $limit_parts[1], false); |
|
569 | + $this->setResponseHeader('TotalPages', ceil($pages), false); |
|
570 | + } |
|
571 | + |
|
572 | + |
|
573 | + |
|
574 | + /** |
|
575 | + * Changes database results into REST API entities |
|
576 | + * |
|
577 | + * @param EEM_Base $model |
|
578 | + * @param array $db_row like results from $wpdb->get_results() |
|
579 | + * @param WP_REST_Request $rest_request |
|
580 | + * @param string $deprecated no longer used |
|
581 | + * @return array ready for being converted into json for sending to client |
|
582 | + */ |
|
583 | + public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
|
584 | + { |
|
585 | + if (! $rest_request instanceof WP_REST_Request) { |
|
586 | + //ok so this was called in the old style, where the 3rd arg was |
|
587 | + //$include, and the 4th arg was $context |
|
588 | + //now setup the request just to avoid fatal errors, although we won't be able |
|
589 | + //to truly make use of it because it's kinda devoid of info |
|
590 | + $rest_request = new WP_REST_Request(); |
|
591 | + $rest_request->set_param('include', $rest_request); |
|
592 | + $rest_request->set_param('caps', $deprecated); |
|
593 | + } |
|
594 | + if ($rest_request->get_param('caps') == null) { |
|
595 | + $rest_request->set_param('caps', EEM_Base::caps_read); |
|
596 | + } |
|
597 | + $entity_array = $this->createBareEntityFromWpdbResults($model, $db_row); |
|
598 | + $entity_array = $this->addExtraFields($model, $db_row, $entity_array); |
|
599 | + $entity_array['_links'] = $this->getEntityLinks($model, $db_row, $entity_array); |
|
600 | + $entity_array['_calculated_fields'] = $this->getEntityCalculations($model, $db_row, $rest_request); |
|
601 | + $entity_array = apply_filters( |
|
602 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
603 | + $entity_array, |
|
604 | + $model, |
|
605 | + $rest_request->get_param('caps'), |
|
606 | + $rest_request, |
|
607 | + $this |
|
608 | + ); |
|
609 | + $entity_array = $this->includeRequestedModels($model, $rest_request, $entity_array, $db_row); |
|
610 | + $entity_array = apply_filters( |
|
611 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
612 | + $entity_array, |
|
613 | + $model, |
|
614 | + $rest_request->get_param('caps'), |
|
615 | + $rest_request, |
|
616 | + $this |
|
617 | + ); |
|
618 | + $result_without_inaccessible_fields = Capabilities::filterOutInaccessibleEntityFields( |
|
619 | + $entity_array, |
|
620 | + $model, |
|
621 | + $rest_request->get_param('caps'), |
|
622 | + $this->getModelVersionInfo(), |
|
623 | + $model->get_index_primary_key_string( |
|
624 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
625 | + ) |
|
626 | + ); |
|
627 | + $this->setDebugInfo( |
|
628 | + 'inaccessible fields', |
|
629 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
630 | + ); |
|
631 | + return apply_filters( |
|
632 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
633 | + $result_without_inaccessible_fields, |
|
634 | + $model, |
|
635 | + $rest_request->get_param('caps') |
|
636 | + ); |
|
637 | + } |
|
638 | + |
|
639 | + |
|
640 | + |
|
641 | + /** |
|
642 | + * Creates a REST entity array (JSON object we're going to return in the response, but |
|
643 | + * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
644 | + * from $wpdb->get_row( $sql, ARRAY_A) |
|
645 | + * |
|
646 | + * @param EEM_Base $model |
|
647 | + * @param array $db_row |
|
648 | + * @return array entity mostly ready for converting to JSON and sending in the response |
|
649 | + * |
|
650 | + */ |
|
651 | + protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
|
652 | + { |
|
653 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
654 | + $result = array_intersect_key( |
|
655 | + $result, |
|
656 | + $this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) |
|
657 | + ); |
|
658 | + //if this is a CPT, we need to set the global $post to it, |
|
659 | + //otherwise shortcodes etc won't work properly while rendering it |
|
660 | + if ($model instanceof \EEM_CPT_Base) { |
|
661 | + $do_chevy_shuffle = true; |
|
662 | + } else { |
|
663 | + $do_chevy_shuffle = false; |
|
664 | + } |
|
665 | + if ($do_chevy_shuffle) { |
|
666 | + global $post; |
|
667 | + $old_post = $post; |
|
668 | + $post = get_post($result[$model->primary_key_name()]); |
|
669 | + if (! $post instanceof \WP_Post) { |
|
670 | + //well that's weird, because $result is what we JUST fetched from the database |
|
671 | + throw new RestException( |
|
672 | + 'error_fetching_post_from_database_results', |
|
673 | + esc_html__( |
|
674 | + 'An item was retrieved from the database but it\'s not a WP_Post like it should be.', |
|
675 | + 'event_espresso' |
|
676 | + ) |
|
677 | + ); |
|
678 | + } |
|
679 | + $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
680 | + $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
|
681 | + $model_object_classname, |
|
682 | + $result, |
|
683 | + false, |
|
684 | + false |
|
685 | + ); |
|
686 | + } |
|
687 | + foreach ($result as $field_name => $field_value) { |
|
688 | + $field_obj = $model->field_settings_for($field_name); |
|
689 | + if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
|
690 | + unset($result[$field_name]); |
|
691 | + } elseif ($this->isSubclassOfOne( |
|
692 | + $field_obj, |
|
693 | + $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
|
694 | + ) |
|
695 | + ) { |
|
696 | + $result[$field_name] = array( |
|
697 | + 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
698 | + 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
699 | + ); |
|
700 | + } elseif ($this->isSubclassOfOne( |
|
701 | + $field_obj, |
|
702 | + $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
|
703 | + ) |
|
704 | + ) { |
|
705 | + $result[$field_name] = array( |
|
706 | + 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
707 | + 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
708 | + ); |
|
709 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
710 | + $field_value = $field_obj->prepare_for_set_from_db($field_value); |
|
711 | + $timezone = $field_value->getTimezone(); |
|
712 | + $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
713 | + $result[$field_name . '_gmt'] = ModelDataTranslator::prepareFieldValuesForJson( |
|
714 | + $field_obj, |
|
715 | + $field_value, |
|
716 | + $this->getModelVersionInfo()->requestedVersion() |
|
717 | + ); |
|
718 | + $field_value->setTimezone($timezone); |
|
719 | + $result[$field_name] = ModelDataTranslator::prepareFieldValuesForJson( |
|
720 | + $field_obj, |
|
721 | + $field_value, |
|
722 | + $this->getModelVersionInfo()->requestedVersion() |
|
723 | + ); |
|
724 | + } else { |
|
725 | + $result[$field_name] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
726 | + } |
|
727 | + } |
|
728 | + if ($do_chevy_shuffle) { |
|
729 | + $post = $old_post; |
|
730 | + } |
|
731 | + return $result; |
|
732 | + } |
|
733 | + |
|
734 | + |
|
735 | + |
|
736 | + /** |
|
737 | + * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
738 | + * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
739 | + * representation using $field_obj->prepare_for_set_from_db()) |
|
740 | + * |
|
741 | + * @param EE_Model_Field_Base $field_obj |
|
742 | + * @param mixed $value as it's stored on a model object |
|
743 | + * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
744 | + * @return mixed |
|
745 | + * @throws ObjectDetectedException if $value contains a PHP object |
|
746 | + */ |
|
747 | + protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
748 | + { |
|
749 | + $value = $field_obj->prepare_for_set_from_db($value); |
|
750 | + switch ($format) { |
|
751 | + case 'pretty': |
|
752 | + $value = $field_obj->prepare_for_pretty_echoing($value); |
|
753 | + break; |
|
754 | + case 'normal': |
|
755 | + default: |
|
756 | + $value = $field_obj->prepare_for_get($value); |
|
757 | + break; |
|
758 | + } |
|
759 | + return ModelDataTranslator::prepareFieldValuesForJson( |
|
760 | + $field_obj, |
|
761 | + $value, |
|
762 | + $this->getModelVersionInfo()->requestedVersion() |
|
763 | + ); |
|
764 | + } |
|
765 | + |
|
766 | + |
|
767 | + |
|
768 | + /** |
|
769 | + * Adds a few extra fields to the entity response |
|
770 | + * |
|
771 | + * @param EEM_Base $model |
|
772 | + * @param array $db_row |
|
773 | + * @param array $entity_array |
|
774 | + * @return array modified entity |
|
775 | + */ |
|
776 | + protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
777 | + { |
|
778 | + if ($model instanceof EEM_CPT_Base) { |
|
779 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
780 | + } |
|
781 | + return $entity_array; |
|
782 | + } |
|
783 | + |
|
784 | + |
|
785 | + |
|
786 | + /** |
|
787 | + * Gets links we want to add to the response |
|
788 | + * |
|
789 | + * @global \WP_REST_Server $wp_rest_server |
|
790 | + * @param EEM_Base $model |
|
791 | + * @param array $db_row |
|
792 | + * @param array $entity_array |
|
793 | + * @return array the _links item in the entity |
|
794 | + */ |
|
795 | + protected function getEntityLinks($model, $db_row, $entity_array) |
|
796 | + { |
|
797 | + //add basic links |
|
798 | + $links = array(); |
|
799 | + if ($model->has_primary_key_field()) { |
|
800 | + $links['self'] = array( |
|
801 | + array( |
|
802 | + 'href' => $this->getVersionedLinkTo( |
|
803 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
804 | + . '/' |
|
805 | + . $entity_array[$model->primary_key_name()] |
|
806 | + ), |
|
807 | + ), |
|
808 | + ); |
|
809 | + } |
|
810 | + $links['collection'] = array( |
|
811 | + array( |
|
812 | + 'href' => $this->getVersionedLinkTo( |
|
813 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
814 | + ), |
|
815 | + ), |
|
816 | + ); |
|
817 | + //add links to related models |
|
818 | + if ($model->has_primary_key_field()) { |
|
819 | + foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
820 | + $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
821 | + $links[EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
822 | + array( |
|
823 | + 'href' => $this->getVersionedLinkTo( |
|
824 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
825 | + . '/' |
|
826 | + . $entity_array[$model->primary_key_name()] |
|
827 | + . '/' |
|
828 | + . $related_model_part |
|
829 | + ), |
|
830 | + 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
831 | + ), |
|
832 | + ); |
|
833 | + } |
|
834 | + } |
|
835 | + return $links; |
|
836 | + } |
|
837 | + |
|
838 | + |
|
839 | + |
|
840 | + /** |
|
841 | + * Adds the included models indicated in the request to the entity provided |
|
842 | + * |
|
843 | + * @param EEM_Base $model |
|
844 | + * @param WP_REST_Request $rest_request |
|
845 | + * @param array $entity_array |
|
846 | + * @param array $db_row |
|
847 | + * @return array the modified entity |
|
848 | + */ |
|
849 | + protected function includeRequestedModels( |
|
850 | + EEM_Base $model, |
|
851 | + WP_REST_Request $rest_request, |
|
852 | + $entity_array, |
|
853 | + $db_row = array() |
|
854 | + ) { |
|
855 | + //if $db_row not included, hope the entity array has what we need |
|
856 | + if (! $db_row) { |
|
857 | + $db_row = $entity_array; |
|
858 | + } |
|
859 | + $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
860 | + $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
861 | + //if they passed in * or didn't specify any includes, return everything |
|
862 | + if (! in_array('*', $includes_for_this_model) |
|
863 | + && ! empty($includes_for_this_model) |
|
864 | + ) { |
|
865 | + if ($model->has_primary_key_field()) { |
|
866 | + //always include the primary key. ya just gotta know that at least |
|
867 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
868 | + } |
|
869 | + if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
870 | + $includes_for_this_model[] = '_calculated_fields'; |
|
871 | + } |
|
872 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
873 | + } |
|
874 | + $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
875 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
876 | + $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
877 | + $rest_request->get_param('include'), |
|
878 | + $relation_name |
|
879 | + ); |
|
880 | + $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
881 | + $rest_request->get_param('calculate'), |
|
882 | + $relation_name |
|
883 | + ); |
|
884 | + //did they specify they wanted to include a related model, or |
|
885 | + //specific fields from a related model? |
|
886 | + //or did they specify to calculate a field from a related model? |
|
887 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
888 | + //if so, we should include at least some part of the related model |
|
889 | + $pretend_related_request = new WP_REST_Request(); |
|
890 | + $pretend_related_request->set_query_params( |
|
891 | + array( |
|
892 | + 'caps' => $rest_request->get_param('caps'), |
|
893 | + 'include' => $related_fields_to_include, |
|
894 | + 'calculate' => $related_fields_to_calculate, |
|
895 | + ) |
|
896 | + ); |
|
897 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
898 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
899 | + $model->get_index_primary_key_string( |
|
900 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
901 | + ) |
|
902 | + ); |
|
903 | + $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
904 | + $primary_model_query_params, |
|
905 | + $relation_obj, |
|
906 | + $pretend_related_request |
|
907 | + ); |
|
908 | + $entity_array[Read::getRelatedEntityName($relation_name, $relation_obj)] = $related_results |
|
909 | + instanceof |
|
910 | + WP_Error |
|
911 | + ? null |
|
912 | + : $related_results; |
|
913 | + } |
|
914 | + } |
|
915 | + return $entity_array; |
|
916 | + } |
|
917 | + |
|
918 | + |
|
919 | + |
|
920 | + /** |
|
921 | + * Returns a new array with all the names of models removed. Eg |
|
922 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
923 | + * |
|
924 | + * @param array $arr |
|
925 | + * @return array |
|
926 | + */ |
|
927 | + private function removeModelNamesFromArray($arr) |
|
928 | + { |
|
929 | + return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
930 | + } |
|
931 | + |
|
932 | + |
|
933 | + |
|
934 | + /** |
|
935 | + * Gets the calculated fields for the response |
|
936 | + * |
|
937 | + * @param EEM_Base $model |
|
938 | + * @param array $wpdb_row |
|
939 | + * @param WP_REST_Request $rest_request |
|
940 | + * @return \stdClass the _calculations item in the entity |
|
941 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
942 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
943 | + */ |
|
944 | + protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
945 | + { |
|
946 | + $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
947 | + $rest_request->get_param('calculate'), |
|
948 | + '' |
|
949 | + ); |
|
950 | + //note: setting calculate=* doesn't do anything |
|
951 | + $calculated_fields_to_return = new \stdClass(); |
|
952 | + foreach ($calculated_fields as $field_to_calculate) { |
|
953 | + try { |
|
954 | + $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
955 | + null, |
|
956 | + $this->fields_calculator->retrieveCalculatedFieldValue( |
|
957 | + $model, |
|
958 | + $field_to_calculate, |
|
959 | + $wpdb_row, |
|
960 | + $rest_request, |
|
961 | + $this |
|
962 | + ), |
|
963 | + $this->getModelVersionInfo()->requestedVersion() |
|
964 | + ); |
|
965 | + } catch (RestException $e) { |
|
966 | + //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
967 | + $this->setResponseHeader( |
|
968 | + 'Notices-Field-Calculation-Errors[' |
|
969 | + . $e->getStringCode() |
|
970 | + . '][' |
|
971 | + . $model->get_this_model_name() |
|
972 | + . '][' |
|
973 | + . $field_to_calculate |
|
974 | + . ']', |
|
975 | + $e->getMessage(), |
|
976 | + true |
|
977 | + ); |
|
978 | + } |
|
979 | + } |
|
980 | + return $calculated_fields_to_return; |
|
981 | + } |
|
982 | + |
|
983 | + |
|
984 | + |
|
985 | + /** |
|
986 | + * Gets the full URL to the resource, taking the requested version into account |
|
987 | + * |
|
988 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
989 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
990 | + */ |
|
991 | + public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
992 | + { |
|
993 | + return rest_url( |
|
994 | + EED_Core_Rest_Api::get_versioned_route_to( |
|
995 | + $link_part_after_version_and_slash, |
|
996 | + $this->getModelVersionInfo()->requestedVersion() |
|
997 | + ) |
|
998 | + ); |
|
999 | + } |
|
1000 | + |
|
1001 | + |
|
1002 | + |
|
1003 | + /** |
|
1004 | + * Gets the correct lowercase name for the relation in the API according |
|
1005 | + * to the relation's type |
|
1006 | + * |
|
1007 | + * @param string $relation_name |
|
1008 | + * @param \EE_Model_Relation_Base $relation_obj |
|
1009 | + * @return string |
|
1010 | + */ |
|
1011 | + public static function getRelatedEntityName($relation_name, $relation_obj) |
|
1012 | + { |
|
1013 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
1014 | + return strtolower($relation_name); |
|
1015 | + } else { |
|
1016 | + return EEH_Inflector::pluralize_and_lower($relation_name); |
|
1017 | + } |
|
1018 | + } |
|
1019 | + |
|
1020 | + |
|
1021 | + |
|
1022 | + /** |
|
1023 | + * Gets the one model object with the specified id for the specified model |
|
1024 | + * |
|
1025 | + * @param EEM_Base $model |
|
1026 | + * @param WP_REST_Request $request |
|
1027 | + * @return array|WP_Error |
|
1028 | + */ |
|
1029 | + public function getEntityFromModel($model, $request) |
|
1030 | + { |
|
1031 | + $context = $this->validateContext($request->get_param('caps')); |
|
1032 | + return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1033 | + } |
|
1034 | + |
|
1035 | + |
|
1036 | + |
|
1037 | + /** |
|
1038 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
1039 | + * version so just treat it as a default read |
|
1040 | + * |
|
1041 | + * @param string $context |
|
1042 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1043 | + */ |
|
1044 | + public function validateContext($context) |
|
1045 | + { |
|
1046 | + if (! $context) { |
|
1047 | + $context = EEM_Base::caps_read; |
|
1048 | + } |
|
1049 | + $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1050 | + if (in_array($context, $valid_contexts)) { |
|
1051 | + return $context; |
|
1052 | + } else { |
|
1053 | + return EEM_Base::caps_read; |
|
1054 | + } |
|
1055 | + } |
|
1056 | + |
|
1057 | + |
|
1058 | + |
|
1059 | + /** |
|
1060 | + * Verifies the passed in value is an allowable default where conditions value. |
|
1061 | + * |
|
1062 | + * @param $default_query_params |
|
1063 | + * @return string |
|
1064 | + */ |
|
1065 | + public function validateDefaultQueryParams($default_query_params) |
|
1066 | + { |
|
1067 | + $valid_default_where_conditions_for_api_calls = array( |
|
1068 | + EEM_Base::default_where_conditions_all, |
|
1069 | + EEM_Base::default_where_conditions_minimum_all, |
|
1070 | + EEM_Base::default_where_conditions_minimum_others, |
|
1071 | + ); |
|
1072 | + if (! $default_query_params) { |
|
1073 | + $default_query_params = EEM_Base::default_where_conditions_all; |
|
1074 | + } |
|
1075 | + if (in_array( |
|
1076 | + $default_query_params, |
|
1077 | + $valid_default_where_conditions_for_api_calls, |
|
1078 | + true |
|
1079 | + )) { |
|
1080 | + return $default_query_params; |
|
1081 | + } else { |
|
1082 | + return EEM_Base::default_where_conditions_all; |
|
1083 | + } |
|
1084 | + } |
|
1085 | + |
|
1086 | + |
|
1087 | + |
|
1088 | + /** |
|
1089 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1090 | + * Note: right now the query parameter keys for fields (and related fields) |
|
1091 | + * can be left as-is, but it's quite possible this will change someday. |
|
1092 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1093 | + * |
|
1094 | + * @param EEM_Base $model |
|
1095 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1096 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1097 | + * that absolutely no results should be returned |
|
1098 | + * @throws EE_Error |
|
1099 | + * @throws RestException |
|
1100 | + */ |
|
1101 | + public function createModelQueryParams($model, $query_parameters) |
|
1102 | + { |
|
1103 | + $model_query_params = array(); |
|
1104 | + if (isset($query_parameters['where'])) { |
|
1105 | + $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1106 | + $query_parameters['where'], |
|
1107 | + $model, |
|
1108 | + $this->getModelVersionInfo()->requestedVersion() |
|
1109 | + ); |
|
1110 | + } |
|
1111 | + if (isset($query_parameters['order_by'])) { |
|
1112 | + $order_by = $query_parameters['order_by']; |
|
1113 | + } elseif (isset($query_parameters['orderby'])) { |
|
1114 | + $order_by = $query_parameters['orderby']; |
|
1115 | + } else { |
|
1116 | + $order_by = null; |
|
1117 | + } |
|
1118 | + if ($order_by !== null) { |
|
1119 | + if (is_array($order_by)) { |
|
1120 | + $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1121 | + } else { |
|
1122 | + //it's a single item |
|
1123 | + $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1124 | + } |
|
1125 | + $model_query_params['order_by'] = $order_by; |
|
1126 | + } |
|
1127 | + if (isset($query_parameters['group_by'])) { |
|
1128 | + $group_by = $query_parameters['group_by']; |
|
1129 | + } elseif (isset($query_parameters['groupby'])) { |
|
1130 | + $group_by = $query_parameters['groupby']; |
|
1131 | + } else { |
|
1132 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1133 | + } |
|
1134 | + //make sure they're all real names |
|
1135 | + if (is_array($group_by)) { |
|
1136 | + $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1137 | + } |
|
1138 | + if ($group_by !== null) { |
|
1139 | + $model_query_params['group_by'] = $group_by; |
|
1140 | + } |
|
1141 | + if (isset($query_parameters['having'])) { |
|
1142 | + $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1143 | + $query_parameters['having'], |
|
1144 | + $model, |
|
1145 | + $this->getModelVersionInfo()->requestedVersion() |
|
1146 | + ); |
|
1147 | + } |
|
1148 | + if (isset($query_parameters['order'])) { |
|
1149 | + $model_query_params['order'] = $query_parameters['order']; |
|
1150 | + } |
|
1151 | + if (isset($query_parameters['mine'])) { |
|
1152 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1153 | + } |
|
1154 | + if (isset($query_parameters['limit'])) { |
|
1155 | + //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1156 | + if (! is_array($query_parameters['limit'])) { |
|
1157 | + $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1158 | + } else { |
|
1159 | + $limit_array = $query_parameters['limit']; |
|
1160 | + } |
|
1161 | + $sanitized_limit = array(); |
|
1162 | + foreach ($limit_array as $key => $limit_part) { |
|
1163 | + if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1164 | + throw new EE_Error( |
|
1165 | + sprintf( |
|
1166 | + __( |
|
1167 | + // @codingStandardsIgnoreStart |
|
1168 | + 'An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
1169 | + // @codingStandardsIgnoreEnd |
|
1170 | + 'event_espresso' |
|
1171 | + ), |
|
1172 | + wp_json_encode($query_parameters['limit']) |
|
1173 | + ) |
|
1174 | + ); |
|
1175 | + } |
|
1176 | + $sanitized_limit[] = (int)$limit_part; |
|
1177 | + } |
|
1178 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1179 | + } else { |
|
1180 | + $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1181 | + } |
|
1182 | + if (isset($query_parameters['caps'])) { |
|
1183 | + $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1184 | + } else { |
|
1185 | + $model_query_params['caps'] = EEM_Base::caps_read; |
|
1186 | + } |
|
1187 | + if (isset($query_parameters['default_where_conditions'])) { |
|
1188 | + $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1189 | + $query_parameters['default_where_conditions'] |
|
1190 | + ); |
|
1191 | + } |
|
1192 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1193 | + } |
|
1194 | + |
|
1195 | + |
|
1196 | + |
|
1197 | + /** |
|
1198 | + * Changes the REST-style query params for use in the models |
|
1199 | + * |
|
1200 | + * @deprecated |
|
1201 | + * @param EEM_Base $model |
|
1202 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1203 | + * @return array |
|
1204 | + */ |
|
1205 | + public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1206 | + { |
|
1207 | + $model_ready_query_params = array(); |
|
1208 | + foreach ($query_params as $key => $value) { |
|
1209 | + if (is_array($value)) { |
|
1210 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1211 | + } else { |
|
1212 | + $model_ready_query_params[$key] = $value; |
|
1213 | + } |
|
1214 | + } |
|
1215 | + return $model_ready_query_params; |
|
1216 | + } |
|
1217 | + |
|
1218 | + |
|
1219 | + |
|
1220 | + /** |
|
1221 | + * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1222 | + * @param $model |
|
1223 | + * @param $query_params |
|
1224 | + * @return array |
|
1225 | + */ |
|
1226 | + public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1227 | + { |
|
1228 | + $model_ready_query_params = array(); |
|
1229 | + foreach ($query_params as $key => $value) { |
|
1230 | + if (is_array($value)) { |
|
1231 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1232 | + } else { |
|
1233 | + $model_ready_query_params[$key] = $value; |
|
1234 | + } |
|
1235 | + } |
|
1236 | + return $model_ready_query_params; |
|
1237 | + } |
|
1238 | + |
|
1239 | + |
|
1240 | + |
|
1241 | + /** |
|
1242 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1243 | + * If no prefix is specified, returns items with no period. |
|
1244 | + * |
|
1245 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1246 | + * @param string $prefix "Event" or "foobar" |
|
1247 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1248 | + * we only return strings starting with that and a period; if no prefix was |
|
1249 | + * specified we return all items containing NO periods |
|
1250 | + */ |
|
1251 | + public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1252 | + { |
|
1253 | + if (is_string($string_to_explode)) { |
|
1254 | + $exploded_contents = explode(',', $string_to_explode); |
|
1255 | + } elseif (is_array($string_to_explode)) { |
|
1256 | + $exploded_contents = $string_to_explode; |
|
1257 | + } else { |
|
1258 | + $exploded_contents = array(); |
|
1259 | + } |
|
1260 | + //if the string was empty, we want an empty array |
|
1261 | + $exploded_contents = array_filter($exploded_contents); |
|
1262 | + $contents_with_prefix = array(); |
|
1263 | + foreach ($exploded_contents as $item) { |
|
1264 | + $item = trim($item); |
|
1265 | + //if no prefix was provided, so we look for items with no "." in them |
|
1266 | + if (! $prefix) { |
|
1267 | + //does this item have a period? |
|
1268 | + if (strpos($item, '.') === false) { |
|
1269 | + //if not, then its what we're looking for |
|
1270 | + $contents_with_prefix[] = $item; |
|
1271 | + } |
|
1272 | + } elseif (strpos($item, $prefix . '.') === 0) { |
|
1273 | + //this item has the prefix and a period, grab it |
|
1274 | + $contents_with_prefix[] = substr( |
|
1275 | + $item, |
|
1276 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1277 | + ); |
|
1278 | + } elseif ($item === $prefix) { |
|
1279 | + //this item is JUST the prefix |
|
1280 | + //so let's grab everything after, which is a blank string |
|
1281 | + $contents_with_prefix[] = ''; |
|
1282 | + } |
|
1283 | + } |
|
1284 | + return $contents_with_prefix; |
|
1285 | + } |
|
1286 | + |
|
1287 | + |
|
1288 | + |
|
1289 | + /** |
|
1290 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1291 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1292 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1293 | + * array('*') (when you provided a model and a model of that kind was found). |
|
1294 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1295 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
1296 | + * name and then a period). |
|
1297 | + * @param string $include_string @see Read:handle_request_get_all |
|
1298 | + * @param string $model_name |
|
1299 | + * @return array of fields for this model. If $model_name is provided, then |
|
1300 | + * the fields for that model, with the model's name removed from each. |
|
1301 | + * If $include_string was blank or '*' returns an empty array |
|
1302 | + */ |
|
1303 | + public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1304 | + { |
|
1305 | + if (is_array($include_string)) { |
|
1306 | + $include_string = implode(',', $include_string); |
|
1307 | + } |
|
1308 | + if ($include_string === '*' || $include_string === '') { |
|
1309 | + return array(); |
|
1310 | + } |
|
1311 | + $includes = explode(',', $include_string); |
|
1312 | + $extracted_fields_to_include = array(); |
|
1313 | + if ($model_name) { |
|
1314 | + foreach ($includes as $field_to_include) { |
|
1315 | + $field_to_include = trim($field_to_include); |
|
1316 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1317 | + //found the model name at the exact start |
|
1318 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1319 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
1320 | + } elseif ($field_to_include == $model_name) { |
|
1321 | + $extracted_fields_to_include[] = '*'; |
|
1322 | + } |
|
1323 | + } |
|
1324 | + } else { |
|
1325 | + //look for ones with no period |
|
1326 | + foreach ($includes as $field_to_include) { |
|
1327 | + $field_to_include = trim($field_to_include); |
|
1328 | + if (strpos($field_to_include, '.') === false |
|
1329 | + && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1330 | + ) { |
|
1331 | + $extracted_fields_to_include[] = $field_to_include; |
|
1332 | + } |
|
1333 | + } |
|
1334 | + } |
|
1335 | + return $extracted_fields_to_include; |
|
1336 | + } |
|
1337 | + |
|
1338 | + |
|
1339 | + |
|
1340 | + /** |
|
1341 | + * Gets the single item using the model according to the request in the context given, otherwise |
|
1342 | + * returns that it's inaccessible to the current user |
|
1343 | + * |
|
1350 | 1344 | *@param EEM_Base $model |
1351 | - * @param WP_REST_Request $request |
|
1352 | - * @param null $context |
|
1353 | - * @return array|WP_Error |
|
1354 | - */ |
|
1355 | - public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1356 | - { |
|
1357 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1358 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1359 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1360 | - } |
|
1361 | - $restricted_query_params = $query_params; |
|
1362 | - $restricted_query_params['caps'] = $context; |
|
1363 | - $this->setDebugInfo('model query params', $restricted_query_params); |
|
1364 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1365 | - if (! empty($model_rows)) { |
|
1366 | - return $this->createEntityFromWpdbResult( |
|
1367 | - $model, |
|
1368 | - array_shift($model_rows), |
|
1369 | - $request |
|
1370 | - ); |
|
1371 | - } else { |
|
1372 | - //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1373 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1374 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1375 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1376 | - //you got shafted- it existed but we didn't want to tell you! |
|
1377 | - return new WP_Error( |
|
1378 | - 'rest_user_cannot_' . $context, |
|
1379 | - sprintf( |
|
1380 | - __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1381 | - $context, |
|
1382 | - strtolower($model->get_this_model_name()), |
|
1383 | - Capabilities::getMissingPermissionsString( |
|
1384 | - $model, |
|
1385 | - $context |
|
1386 | - ) |
|
1387 | - ), |
|
1388 | - array('status' => 403) |
|
1389 | - ); |
|
1390 | - } else { |
|
1391 | - //it's not you. It just doesn't exist |
|
1392 | - return new WP_Error( |
|
1393 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1394 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1395 | - array('status' => 404) |
|
1396 | - ); |
|
1397 | - } |
|
1398 | - } |
|
1399 | - } |
|
1345 | + * @param WP_REST_Request $request |
|
1346 | + * @param null $context |
|
1347 | + * @return array|WP_Error |
|
1348 | + */ |
|
1349 | + public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1350 | + { |
|
1351 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1352 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1353 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1354 | + } |
|
1355 | + $restricted_query_params = $query_params; |
|
1356 | + $restricted_query_params['caps'] = $context; |
|
1357 | + $this->setDebugInfo('model query params', $restricted_query_params); |
|
1358 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1359 | + if (! empty($model_rows)) { |
|
1360 | + return $this->createEntityFromWpdbResult( |
|
1361 | + $model, |
|
1362 | + array_shift($model_rows), |
|
1363 | + $request |
|
1364 | + ); |
|
1365 | + } else { |
|
1366 | + //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1367 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1368 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1369 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
1370 | + //you got shafted- it existed but we didn't want to tell you! |
|
1371 | + return new WP_Error( |
|
1372 | + 'rest_user_cannot_' . $context, |
|
1373 | + sprintf( |
|
1374 | + __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1375 | + $context, |
|
1376 | + strtolower($model->get_this_model_name()), |
|
1377 | + Capabilities::getMissingPermissionsString( |
|
1378 | + $model, |
|
1379 | + $context |
|
1380 | + ) |
|
1381 | + ), |
|
1382 | + array('status' => 403) |
|
1383 | + ); |
|
1384 | + } else { |
|
1385 | + //it's not you. It just doesn't exist |
|
1386 | + return new WP_Error( |
|
1387 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1388 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1389 | + array('status' => 404) |
|
1390 | + ); |
|
1391 | + } |
|
1392 | + } |
|
1393 | + } |
|
1400 | 1394 | } |
1401 | 1395 | |
1402 | 1396 |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | use EEM_Base; |
21 | 21 | use EEM_CPT_Base; |
22 | 22 | |
23 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
23 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
24 | 24 | exit('No direct script access allowed'); |
25 | 25 | } |
26 | 26 | |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | $controller = new Read(); |
72 | 72 | try { |
73 | 73 | $controller->setRequestedVersion($version); |
74 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
74 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
75 | 75 | return $controller->sendResponse( |
76 | 76 | new WP_Error( |
77 | 77 | 'endpoint_parsing_error', |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | $controller = new Read(); |
111 | 111 | try { |
112 | 112 | $controller->setRequestedVersion($version); |
113 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
113 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
114 | 114 | return array(); |
115 | 115 | } |
116 | 116 | //get the model for this version |
@@ -207,9 +207,9 @@ discard block |
||
207 | 207 | protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
208 | 208 | { |
209 | 209 | if ($field instanceof EE_Datetime_Field) { |
210 | - $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
210 | + $schema['properties'][$field_name.'_gmt'] = $field->getSchema(); |
|
211 | 211 | //modify the description |
212 | - $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
212 | + $schema['properties'][$field_name.'_gmt']['description'] = sprintf( |
|
213 | 213 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
214 | 214 | $field->get_nicename() |
215 | 215 | ); |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | $controller = new Read(); |
253 | 253 | try { |
254 | 254 | $controller->setRequestedVersion($version); |
255 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
255 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
256 | 256 | return $controller->sendResponse( |
257 | 257 | new WP_Error( |
258 | 258 | 'endpoint_parsing_error', |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | $controller = new Read(); |
300 | 300 | try { |
301 | 301 | $controller->setRequestedVersion($version); |
302 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
302 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
303 | 303 | return $controller->sendResponse( |
304 | 304 | new WP_Error( |
305 | 305 | 'endpoint_parsing_error', |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | ); |
315 | 315 | } |
316 | 316 | $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
317 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
317 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
318 | 318 | return $controller->sendResponse( |
319 | 319 | new WP_Error( |
320 | 320 | 'endpoint_parsing_error', |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | public function getEntitiesFromModel($model, $request) |
354 | 354 | { |
355 | 355 | $query_params = $this->createModelQueryParams($model, $request->get_params()); |
356 | - if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
356 | + if ( ! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
357 | 357 | $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
358 | 358 | return new WP_Error( |
359 | 359 | sprintf('rest_%s_cannot_list', $model_name_plural), |
@@ -365,7 +365,7 @@ discard block |
||
365 | 365 | array('status' => 403) |
366 | 366 | ); |
367 | 367 | } |
368 | - if (! $request->get_header('no_rest_headers')) { |
|
368 | + if ( ! $request->get_header('no_rest_headers')) { |
|
369 | 369 | $this->setHeadersFromQueryParams($model, $query_params); |
370 | 370 | } |
371 | 371 | /** @type array $results */ |
@@ -401,7 +401,7 @@ discard block |
||
401 | 401 | $context = $this->validateContext($request->get_param('caps')); |
402 | 402 | $model = $relation->get_this_model(); |
403 | 403 | $related_model = $relation->get_other_model(); |
404 | - if (! isset($primary_model_query_params[0])) { |
|
404 | + if ( ! isset($primary_model_query_params[0])) { |
|
405 | 405 | $primary_model_query_params[0] = array(); |
406 | 406 | } |
407 | 407 | //check if they can access the 1st model object |
@@ -418,7 +418,7 @@ discard block |
||
418 | 418 | $restricted_query_params['caps'] = $context; |
419 | 419 | $this->setDebugInfo('main model query params', $restricted_query_params); |
420 | 420 | $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
421 | - if (! ( |
|
421 | + if ( ! ( |
|
422 | 422 | Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
423 | 423 | && $model->exists($restricted_query_params) |
424 | 424 | ) |
@@ -457,7 +457,7 @@ discard block |
||
457 | 457 | } |
458 | 458 | $query_params['default_where_conditions'] = 'none'; |
459 | 459 | $query_params['caps'] = $context; |
460 | - if (! $request->get_header('no_rest_headers')) { |
|
460 | + if ( ! $request->get_header('no_rest_headers')) { |
|
461 | 461 | $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
462 | 462 | } |
463 | 463 | /** @type array $results */ |
@@ -510,7 +510,7 @@ discard block |
||
510 | 510 | */ |
511 | 511 | public function getEntitiesFromRelation($id, $relation, $request) |
512 | 512 | { |
513 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
513 | + if ( ! $relation->get_this_model()->has_primary_key_field()) { |
|
514 | 514 | throw new EE_Error( |
515 | 515 | sprintf( |
516 | 516 | __( |
@@ -553,7 +553,7 @@ discard block |
||
553 | 553 | Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
554 | 554 | ); |
555 | 555 | //normally the limit to a 2-part array, where the 2nd item is the limit |
556 | - if (! isset($query_params['limit'])) { |
|
556 | + if ( ! isset($query_params['limit'])) { |
|
557 | 557 | $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
558 | 558 | } |
559 | 559 | if (is_array($query_params['limit'])) { |
@@ -587,7 +587,7 @@ discard block |
||
587 | 587 | */ |
588 | 588 | public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
589 | 589 | { |
590 | - if (! $rest_request instanceof WP_REST_Request) { |
|
590 | + if ( ! $rest_request instanceof WP_REST_Request) { |
|
591 | 591 | //ok so this was called in the old style, where the 3rd arg was |
592 | 592 | //$include, and the 4th arg was $context |
593 | 593 | //now setup the request just to avoid fatal errors, although we won't be able |
@@ -671,7 +671,7 @@ discard block |
||
671 | 671 | global $post; |
672 | 672 | $old_post = $post; |
673 | 673 | $post = get_post($result[$model->primary_key_name()]); |
674 | - if (! $post instanceof \WP_Post) { |
|
674 | + if ( ! $post instanceof \WP_Post) { |
|
675 | 675 | //well that's weird, because $result is what we JUST fetched from the database |
676 | 676 | throw new RestException( |
677 | 677 | 'error_fetching_post_from_database_results', |
@@ -681,7 +681,7 @@ discard block |
||
681 | 681 | ) |
682 | 682 | ); |
683 | 683 | } |
684 | - $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
684 | + $model_object_classname = 'EE_'.$model->get_this_model_name(); |
|
685 | 685 | $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
686 | 686 | $model_object_classname, |
687 | 687 | $result, |
@@ -715,7 +715,7 @@ discard block |
||
715 | 715 | $field_value = $field_obj->prepare_for_set_from_db($field_value); |
716 | 716 | $timezone = $field_value->getTimezone(); |
717 | 717 | $field_value->setTimezone(new \DateTimeZone('UTC')); |
718 | - $result[$field_name . '_gmt'] = ModelDataTranslator::prepareFieldValuesForJson( |
|
718 | + $result[$field_name.'_gmt'] = ModelDataTranslator::prepareFieldValuesForJson( |
|
719 | 719 | $field_obj, |
720 | 720 | $field_value, |
721 | 721 | $this->getModelVersionInfo()->requestedVersion() |
@@ -823,7 +823,7 @@ discard block |
||
823 | 823 | if ($model->has_primary_key_field()) { |
824 | 824 | foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
825 | 825 | $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
826 | - $links[EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
826 | + $links[EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
827 | 827 | array( |
828 | 828 | 'href' => $this->getVersionedLinkTo( |
829 | 829 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
@@ -858,13 +858,13 @@ discard block |
||
858 | 858 | $db_row = array() |
859 | 859 | ) { |
860 | 860 | //if $db_row not included, hope the entity array has what we need |
861 | - if (! $db_row) { |
|
861 | + if ( ! $db_row) { |
|
862 | 862 | $db_row = $entity_array; |
863 | 863 | } |
864 | 864 | $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
865 | 865 | $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
866 | 866 | //if they passed in * or didn't specify any includes, return everything |
867 | - if (! in_array('*', $includes_for_this_model) |
|
867 | + if ( ! in_array('*', $includes_for_this_model) |
|
868 | 868 | && ! empty($includes_for_this_model) |
869 | 869 | ) { |
870 | 870 | if ($model->has_primary_key_field()) { |
@@ -1048,7 +1048,7 @@ discard block |
||
1048 | 1048 | */ |
1049 | 1049 | public function validateContext($context) |
1050 | 1050 | { |
1051 | - if (! $context) { |
|
1051 | + if ( ! $context) { |
|
1052 | 1052 | $context = EEM_Base::caps_read; |
1053 | 1053 | } |
1054 | 1054 | $valid_contexts = EEM_Base::valid_cap_contexts(); |
@@ -1074,7 +1074,7 @@ discard block |
||
1074 | 1074 | EEM_Base::default_where_conditions_minimum_all, |
1075 | 1075 | EEM_Base::default_where_conditions_minimum_others, |
1076 | 1076 | ); |
1077 | - if (! $default_query_params) { |
|
1077 | + if ( ! $default_query_params) { |
|
1078 | 1078 | $default_query_params = EEM_Base::default_where_conditions_all; |
1079 | 1079 | } |
1080 | 1080 | if (in_array( |
@@ -1158,14 +1158,14 @@ discard block |
||
1158 | 1158 | } |
1159 | 1159 | if (isset($query_parameters['limit'])) { |
1160 | 1160 | //limit should be either a string like '23' or '23,43', or an array with two items in it |
1161 | - if (! is_array($query_parameters['limit'])) { |
|
1162 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1161 | + if ( ! is_array($query_parameters['limit'])) { |
|
1162 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1163 | 1163 | } else { |
1164 | 1164 | $limit_array = $query_parameters['limit']; |
1165 | 1165 | } |
1166 | 1166 | $sanitized_limit = array(); |
1167 | 1167 | foreach ($limit_array as $key => $limit_part) { |
1168 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1168 | + if ($this->debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1169 | 1169 | throw new EE_Error( |
1170 | 1170 | sprintf( |
1171 | 1171 | __( |
@@ -1178,7 +1178,7 @@ discard block |
||
1178 | 1178 | ) |
1179 | 1179 | ); |
1180 | 1180 | } |
1181 | - $sanitized_limit[] = (int)$limit_part; |
|
1181 | + $sanitized_limit[] = (int) $limit_part; |
|
1182 | 1182 | } |
1183 | 1183 | $model_query_params['limit'] = implode(',', $sanitized_limit); |
1184 | 1184 | } else { |
@@ -1268,17 +1268,17 @@ discard block |
||
1268 | 1268 | foreach ($exploded_contents as $item) { |
1269 | 1269 | $item = trim($item); |
1270 | 1270 | //if no prefix was provided, so we look for items with no "." in them |
1271 | - if (! $prefix) { |
|
1271 | + if ( ! $prefix) { |
|
1272 | 1272 | //does this item have a period? |
1273 | 1273 | if (strpos($item, '.') === false) { |
1274 | 1274 | //if not, then its what we're looking for |
1275 | 1275 | $contents_with_prefix[] = $item; |
1276 | 1276 | } |
1277 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1277 | + } elseif (strpos($item, $prefix.'.') === 0) { |
|
1278 | 1278 | //this item has the prefix and a period, grab it |
1279 | 1279 | $contents_with_prefix[] = substr( |
1280 | 1280 | $item, |
1281 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1281 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
1282 | 1282 | ); |
1283 | 1283 | } elseif ($item === $prefix) { |
1284 | 1284 | //this item is JUST the prefix |
@@ -1318,9 +1318,9 @@ discard block |
||
1318 | 1318 | if ($model_name) { |
1319 | 1319 | foreach ($includes as $field_to_include) { |
1320 | 1320 | $field_to_include = trim($field_to_include); |
1321 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1321 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
1322 | 1322 | //found the model name at the exact start |
1323 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1323 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
1324 | 1324 | $extracted_fields_to_include[] = $field_sans_model_name; |
1325 | 1325 | } elseif ($field_to_include == $model_name) { |
1326 | 1326 | $extracted_fields_to_include[] = '*'; |
@@ -1362,7 +1362,7 @@ discard block |
||
1362 | 1362 | $restricted_query_params['caps'] = $context; |
1363 | 1363 | $this->setDebugInfo('model query params', $restricted_query_params); |
1364 | 1364 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
1365 | - if (! empty($model_rows)) { |
|
1365 | + if ( ! empty($model_rows)) { |
|
1366 | 1366 | return $this->createEntityFromWpdbResult( |
1367 | 1367 | $model, |
1368 | 1368 | array_shift($model_rows), |
@@ -1372,10 +1372,10 @@ discard block |
||
1372 | 1372 | //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
1373 | 1373 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
1374 | 1374 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
1375 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1375 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
1376 | 1376 | //you got shafted- it existed but we didn't want to tell you! |
1377 | 1377 | return new WP_Error( |
1378 | - 'rest_user_cannot_' . $context, |
|
1378 | + 'rest_user_cannot_'.$context, |
|
1379 | 1379 | sprintf( |
1380 | 1380 | __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
1381 | 1381 | $context, |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | use EEM_Base; |
12 | 12 | |
13 | 13 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
14 | - exit('No direct script access allowed'); |
|
14 | + exit('No direct script access allowed'); |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | |
@@ -36,792 +36,792 @@ discard block |
||
36 | 36 | class ModelDataTranslator |
37 | 37 | { |
38 | 38 | |
39 | - /** |
|
40 | - * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
41 | - * fields that COULD contain -1; so we use null |
|
42 | - */ |
|
43 | - const EE_INF_IN_REST = null; |
|
44 | - |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * Prepares a possible array of input values from JSON for use by the models |
|
49 | - * |
|
50 | - * @param EE_Model_Field_Base $field_obj |
|
51 | - * @param mixed $original_value_maybe_array |
|
52 | - * @param string $requested_version |
|
53 | - * @param string $timezone_string treat values as being in this timezone |
|
54 | - * @return mixed |
|
55 | - * @throws RestException |
|
56 | - */ |
|
57 | - public static function prepareFieldValuesFromJson( |
|
58 | - $field_obj, |
|
59 | - $original_value_maybe_array, |
|
60 | - $requested_version, |
|
61 | - $timezone_string = 'UTC' |
|
62 | - ) { |
|
63 | - if (is_array($original_value_maybe_array) |
|
64 | - && ! $field_obj instanceof EE_Serialized_Text_Field |
|
65 | - ) { |
|
66 | - $new_value_maybe_array = array(); |
|
67 | - foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
68 | - $new_value_maybe_array[$array_key] = ModelDataTranslator::prepareFieldValueFromJson( |
|
69 | - $field_obj, |
|
70 | - $array_item, |
|
71 | - $requested_version, |
|
72 | - $timezone_string |
|
73 | - ); |
|
74 | - } |
|
75 | - } else { |
|
76 | - $new_value_maybe_array = ModelDataTranslator::prepareFieldValueFromJson( |
|
77 | - $field_obj, |
|
78 | - $original_value_maybe_array, |
|
79 | - $requested_version, |
|
80 | - $timezone_string |
|
81 | - ); |
|
82 | - } |
|
83 | - return $new_value_maybe_array; |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * Prepares an array of field values FOR use in JSON/REST API |
|
90 | - * |
|
91 | - * @param EE_Model_Field_Base $field_obj |
|
92 | - * @param mixed $original_value_maybe_array |
|
93 | - * @param string $request_version (eg 4.8.36) |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public static function prepareFieldValuesForJson($field_obj, $original_value_maybe_array, $request_version) |
|
97 | - { |
|
98 | - if (is_array($original_value_maybe_array)) { |
|
99 | - $new_value = array(); |
|
100 | - foreach ($original_value_maybe_array as $key => $value) { |
|
101 | - $new_value[$key] = ModelDataTranslator::prepareFieldValuesForJson($field_obj, $value, $request_version); |
|
102 | - } |
|
103 | - } else { |
|
104 | - $new_value = ModelDataTranslator::prepareFieldValueForJson( |
|
105 | - $field_obj, |
|
106 | - $original_value_maybe_array, |
|
107 | - $request_version |
|
108 | - ); |
|
109 | - } |
|
110 | - return $new_value; |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
117 | - * "$query_params". |
|
118 | - * |
|
119 | - * @param EE_Model_Field_Base $field_obj |
|
120 | - * @param mixed $original_value |
|
121 | - * @param string $requested_version |
|
122 | - * @param string $timezone_string treat values as being in this timezone |
|
123 | - * @return mixed |
|
124 | - * @throws RestException |
|
125 | - */ |
|
126 | - public static function prepareFieldValueFromJson( |
|
127 | - $field_obj, |
|
128 | - $original_value, |
|
129 | - $requested_version, |
|
130 | - $timezone_string = 'UTC' // UTC |
|
131 | - ) { |
|
132 | - //check if they accidentally submitted an error value. If so throw an exception |
|
133 | - if (is_array($original_value) |
|
134 | - && isset($original_value['error_code'], $original_value['error_message'])) { |
|
135 | - throw new RestException( |
|
136 | - 'rest_submitted_error_value', |
|
137 | - sprintf( |
|
138 | - esc_html__( |
|
139 | - 'You tried to submit a JSON error object as a value for %1$s. That\'s not allowed.', |
|
140 | - 'event_espresso' |
|
141 | - ), |
|
142 | - $field_obj->get_name() |
|
143 | - ), |
|
144 | - array( |
|
145 | - 'status' => 400, |
|
146 | - ) |
|
147 | - ); |
|
148 | - } |
|
149 | - //double-check for serialized PHP. We never accept serialized PHP. No way Jose. |
|
150 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($original_value); |
|
151 | - $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
152 | - $new_value = null; |
|
153 | - //walk through the submitted data and double-check for serialized PHP. We never accept serialized PHP. No |
|
154 | - // way Jose. |
|
155 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($original_value); |
|
156 | - if ($field_obj instanceof EE_Infinite_Integer_Field |
|
157 | - && in_array($original_value, array(null, ''), true) |
|
158 | - ) { |
|
159 | - $new_value = EE_INF; |
|
160 | - } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
161 | - list($offset_sign, $offset_secs) = ModelDataTranslator::parseTimezoneOffset( |
|
162 | - $field_obj->get_timezone_offset( |
|
163 | - new \DateTimeZone($timezone_string), |
|
164 | - $original_value |
|
165 | - ) |
|
166 | - ); |
|
167 | - $offset_string = |
|
168 | - str_pad( |
|
169 | - floor($offset_secs / HOUR_IN_SECONDS), |
|
170 | - 2, |
|
171 | - '0', |
|
172 | - STR_PAD_LEFT |
|
173 | - ) |
|
174 | - . ':' |
|
175 | - . str_pad( |
|
176 | - ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
177 | - 2, |
|
178 | - '0', |
|
179 | - STR_PAD_LEFT |
|
180 | - ); |
|
181 | - $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
182 | - } else { |
|
183 | - $new_value = $original_value; |
|
184 | - } |
|
185 | - return $new_value; |
|
186 | - } |
|
187 | - |
|
188 | - |
|
189 | - |
|
190 | - /** |
|
191 | - * Throws an exception if $data is a serialized PHP string (or somehow an actually PHP object, although I don't |
|
192 | - * think that can happen). If $data is an array, recurses into its keys and values |
|
193 | - * @param mixed $data |
|
194 | - * @throws RestException |
|
195 | - * @return void |
|
196 | - */ |
|
197 | - public static function throwExceptionIfContainsSerializedData($data) |
|
198 | - { |
|
199 | - if (is_array($data)) { |
|
200 | - foreach ($data as $key => $value) { |
|
201 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($key); |
|
202 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($value); |
|
203 | - } |
|
204 | - } else { |
|
205 | - if (is_serialized($data) || is_object($data)) { |
|
206 | - throw new RestException( |
|
207 | - 'serialized_data_submission_prohibited', |
|
208 | - esc_html__( |
|
209 | - // @codingStandardsIgnoreStart |
|
210 | - 'You tried to submit a string of serialized text. Serialized PHP is prohibited over the EE4 REST API.', |
|
211 | - // @codingStandardsIgnoreEnd |
|
212 | - 'event_espresso' |
|
213 | - ) |
|
214 | - ); |
|
215 | - } |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * determines what's going on with them timezone strings |
|
223 | - * |
|
224 | - * @param int $timezone_offset |
|
225 | - * @return array |
|
226 | - */ |
|
227 | - private static function parseTimezoneOffset($timezone_offset) |
|
228 | - { |
|
229 | - $first_char = substr((string)$timezone_offset, 0, 1); |
|
230 | - if ($first_char === '+' || $first_char === '-') { |
|
231 | - $offset_sign = $first_char; |
|
232 | - $offset_secs = substr((string)$timezone_offset, 1); |
|
233 | - } else { |
|
234 | - $offset_sign = '+'; |
|
235 | - $offset_secs = $timezone_offset; |
|
236 | - } |
|
237 | - return array($offset_sign, $offset_secs); |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * Prepares a field's value for display in the API |
|
244 | - * |
|
245 | - * @param EE_Model_Field_Base $field_obj |
|
246 | - * @param mixed $original_value |
|
247 | - * @param string $requested_version |
|
248 | - * @return mixed |
|
249 | - */ |
|
250 | - public static function prepareFieldValueForJson($field_obj, $original_value, $requested_version) |
|
251 | - { |
|
252 | - if ($original_value === EE_INF) { |
|
253 | - $new_value = ModelDataTranslator::EE_INF_IN_REST; |
|
254 | - } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
255 | - if (is_string($original_value)) { |
|
256 | - //did they submit a string of a unix timestamp? |
|
257 | - if (is_numeric($original_value)) { |
|
258 | - $datetime_obj = new \DateTime(); |
|
259 | - $datetime_obj->setTimestamp((int)$original_value); |
|
260 | - } else { |
|
261 | - //first, check if its a MySQL timestamp in GMT |
|
262 | - $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
|
263 | - } |
|
264 | - if (! $datetime_obj instanceof \DateTime) { |
|
265 | - //so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
|
266 | - $datetime_obj = $field_obj->prepare_for_set($original_value); |
|
267 | - } |
|
268 | - $original_value = $datetime_obj; |
|
269 | - } |
|
270 | - if ($original_value instanceof \DateTime) { |
|
271 | - $new_value = $original_value->format('Y-m-d H:i:s'); |
|
272 | - } elseif (is_int($original_value)) { |
|
273 | - $new_value = date('Y-m-d H:i:s', $original_value); |
|
274 | - } elseif($original_value === null || $original_value === '') { |
|
275 | - $new_value = null; |
|
276 | - } else { |
|
277 | - //so it's not a datetime object, unix timestamp (as string or int), |
|
278 | - //MySQL timestamp, or even a string in the field object's format. So no idea what it is |
|
279 | - throw new \EE_Error( |
|
280 | - sprintf( |
|
281 | - esc_html__( |
|
282 | - // @codingStandardsIgnoreStart |
|
283 | - 'The value "%1$s" for the field "%2$s" on model "%3$s" could not be understood. It should be a PHP DateTime, unix timestamp, MySQL date, or string in the format "%4$s".', |
|
284 | - // @codingStandardsIgnoreEnd |
|
285 | - 'event_espressso' |
|
286 | - ), |
|
287 | - $original_value, |
|
288 | - $field_obj->get_name(), |
|
289 | - $field_obj->get_model_name(), |
|
290 | - $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
291 | - ) |
|
292 | - ); |
|
293 | - } |
|
294 | - $new_value = mysql_to_rfc3339($new_value); |
|
295 | - } else { |
|
296 | - $new_value = $original_value; |
|
297 | - } |
|
298 | - //are we about to send an object? just don't. We have no good way to represent it in JSON. |
|
299 | - // can't just check using is_object() because that missed PHP incomplete objects |
|
300 | - if (! ModelDataTranslator::isRepresentableInJson($new_value)) { |
|
301 | - $new_value = array( |
|
302 | - 'error_code' => 'php_object_not_return', |
|
303 | - 'error_message' => esc_html__('The value of this field in the database is a PHP object, which can\'t be represented in JSON.', 'event_espresso') |
|
304 | - ); |
|
305 | - } |
|
306 | - return apply_filters( |
|
307 | - 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
308 | - $new_value, |
|
309 | - $field_obj, |
|
310 | - $original_value, |
|
311 | - $requested_version |
|
312 | - ); |
|
313 | - } |
|
314 | - |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * Prepares condition-query-parameters (like what's in where and having) from |
|
319 | - * the format expected in the API to use in the models |
|
320 | - * |
|
321 | - * @param array $inputted_query_params_of_this_type |
|
322 | - * @param EEM_Base $model |
|
323 | - * @param string $requested_version |
|
324 | - * @param boolean $writing whether this data will be written to the DB, or if we're just building a query. |
|
325 | - * If we're writing to the DB, we don't expect any operators, or any logic query parameters, |
|
326 | - * and we also won't accept serialized data unless the current user has unfiltered_html. |
|
327 | - * @return array |
|
328 | - * @throws \DomainException |
|
329 | - * @throws RestException |
|
330 | - * @throws EE_Error |
|
331 | - */ |
|
332 | - public static function prepareConditionsQueryParamsForModels( |
|
333 | - $inputted_query_params_of_this_type, |
|
334 | - EEM_Base $model, |
|
335 | - $requested_version, |
|
336 | - $writing = false |
|
337 | - ) { |
|
338 | - $query_param_for_models = array(); |
|
339 | - $valid_operators = $model->valid_operators(); |
|
340 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
341 | - $is_gmt_datetime_field = false; |
|
342 | - $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
|
343 | - $query_param_key |
|
344 | - ); |
|
345 | - $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
346 | - $query_param_sans_stars, |
|
347 | - $model |
|
348 | - ); |
|
349 | - //double-check is it a *_gmt field? |
|
350 | - if (! $field instanceof EE_Model_Field_Base |
|
351 | - && ModelDataTranslator::isGmtDateFieldName($query_param_sans_stars) |
|
352 | - ) { |
|
353 | - //yep, take off '_gmt', and find the field |
|
354 | - $query_param_key = ModelDataTranslator::removeGmtFromFieldName($query_param_sans_stars); |
|
355 | - $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
356 | - $query_param_key, |
|
357 | - $model |
|
358 | - ); |
|
359 | - $timezone = 'UTC'; |
|
360 | - $is_gmt_datetime_field = true; |
|
361 | - } elseif ($field instanceof EE_Datetime_Field) { |
|
362 | - //so it's not a GMT field. Set the timezone on the model to the default |
|
363 | - $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
364 | - } else { |
|
365 | - //just keep using what's already set for the timezone |
|
366 | - $timezone = $model->get_timezone(); |
|
367 | - } |
|
368 | - if ($field instanceof EE_Model_Field_Base) { |
|
369 | - if (! $writing && is_array($query_param_value)) { |
|
370 | - if (! \EEH_Array::is_array_numerically_and_sequentially_indexed($query_param_value)) { |
|
371 | - if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
372 | - throw new RestException( |
|
373 | - 'numerically_indexed_array_of_values_only', |
|
374 | - sprintf( |
|
375 | - esc_html__( |
|
376 | - 'The array provided for the parameter "%1$s" should be numerically indexed.', |
|
377 | - 'event_espresso' |
|
378 | - ), |
|
379 | - $query_param_key |
|
380 | - ), |
|
381 | - array( |
|
382 | - 'status' => 400, |
|
383 | - ) |
|
384 | - ); |
|
385 | - } |
|
386 | - } |
|
387 | - //did they specify an operator? |
|
388 | - if (isset($query_param_value[0]) |
|
389 | - && isset($valid_operators[$query_param_value[0]]) |
|
390 | - ) { |
|
391 | - $op = $query_param_value[0]; |
|
392 | - $translated_value = array($op); |
|
393 | - if (array_key_exists($op, $model->valid_in_style_operators()) |
|
394 | - && isset($query_param_value[1]) |
|
395 | - && ! isset($query_param_value[2]) |
|
396 | - ) { |
|
397 | - $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
398 | - $field, |
|
399 | - $query_param_value[1], |
|
400 | - $requested_version, |
|
401 | - $timezone |
|
402 | - ); |
|
403 | - } elseif (array_key_exists($op, $model->valid_between_style_operators()) |
|
404 | - && isset($query_param_value[1], $query_param_value[2]) |
|
405 | - && !isset($query_param_value[3]) |
|
406 | - ) { |
|
407 | - $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
408 | - $field, |
|
409 | - $query_param_value[1], |
|
410 | - $requested_version, |
|
411 | - $timezone |
|
412 | - ); |
|
413 | - $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
414 | - $field, |
|
415 | - $query_param_value[2], |
|
416 | - $requested_version, |
|
417 | - $timezone |
|
418 | - ); |
|
419 | - } elseif (array_key_exists($op, $model->valid_like_style_operators()) |
|
420 | - && isset($query_param_value[1]) |
|
421 | - && ! isset($query_param_value[2]) |
|
422 | - ) { |
|
423 | - //we want to leave this value mostly-as-is (eg don't force it to be a float |
|
424 | - //or a boolean or an enum value. Leave it as-is with wildcards etc) |
|
425 | - //but do verify it at least doesn't have any serialized data |
|
426 | - ModelDataTranslator::throwExceptionIfContainsSerializedData($query_param_value[1]); |
|
427 | - $translated_value[] = $query_param_value[1]; |
|
428 | - } elseif (array_key_exists($op, $model->valid_null_style_operators()) |
|
429 | - && !isset($query_param_value[1])) { |
|
430 | - //no arguments should have been provided, so don't look for any |
|
431 | - } elseif (isset($query_param_value[1]) |
|
432 | - && !isset($query_param_value[2]) |
|
433 | - && ! array_key_exists( |
|
434 | - $op, |
|
435 | - array_merge( |
|
436 | - $model->valid_in_style_operators(), |
|
437 | - $model->valid_null_style_operators(), |
|
438 | - $model->valid_like_style_operators(), |
|
439 | - $model->valid_between_style_operators() |
|
440 | - ) |
|
441 | - ) |
|
442 | - ) { |
|
443 | - //it's a valid operator, but none of the exceptions. Treat it normally. |
|
444 | - $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
445 | - $field, |
|
446 | - $query_param_value[1], |
|
447 | - $requested_version, |
|
448 | - $timezone |
|
449 | - ); |
|
450 | - } else { |
|
451 | - //so they provided a valid operator, but wrong number of arguments |
|
452 | - if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
453 | - throw new RestException( |
|
454 | - 'wrong_number_of_arguments', |
|
455 | - sprintf( |
|
456 | - esc_html__( |
|
457 | - 'The operator you provided, "%1$s" had the wrong number of arguments', |
|
458 | - 'event_espresso' |
|
459 | - ), |
|
460 | - $op |
|
461 | - ), |
|
462 | - array( |
|
463 | - 'status' => 400, |
|
464 | - ) |
|
465 | - ); |
|
466 | - } |
|
467 | - $translated_value = null; |
|
468 | - } |
|
469 | - } else { |
|
470 | - //so they didn't provide a valid operator |
|
471 | - if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
472 | - throw new RestException( |
|
473 | - 'invalid_operator', |
|
474 | - sprintf( |
|
475 | - esc_html__( |
|
476 | - 'You provided an invalid parameter, with key "%1$s" and value "%2$s"', |
|
477 | - 'event_espresso' |
|
478 | - ), |
|
479 | - $query_param_key, |
|
480 | - $query_param_value |
|
481 | - ), |
|
482 | - array( |
|
483 | - 'status' => 400, |
|
484 | - ) |
|
485 | - ); |
|
486 | - } |
|
487 | - //if we aren't in debug mode, then just try our best to fulfill the user's request |
|
488 | - $translated_value = null; |
|
489 | - } |
|
490 | - } else { |
|
491 | - $translated_value = ModelDataTranslator::prepareFieldValueFromJson( |
|
492 | - $field, |
|
493 | - $query_param_value, |
|
494 | - $requested_version, |
|
495 | - $timezone |
|
496 | - ); |
|
497 | - } |
|
498 | - if ( |
|
499 | - (isset($query_param_for_models[$query_param_key]) && $is_gmt_datetime_field) |
|
500 | - || |
|
501 | - $translated_value === null |
|
502 | - ) { |
|
503 | - //they have already provided a non-gmt field, ignore the gmt one. That's what WP core |
|
504 | - //currently does (they might change it though). See https://core.trac.wordpress.org/ticket/39954 |
|
505 | - //OR we couldn't create a translated value from their input |
|
506 | - continue; |
|
507 | - } |
|
508 | - $query_param_for_models[$query_param_key] = $translated_value; |
|
509 | - } else { |
|
510 | - //so this param doesn't correspond to a field eh? |
|
511 | - if ($writing) { |
|
512 | - //always tell API clients about invalid parameters when they're creating data. Otherwise, |
|
513 | - //they are probably going to create invalid data |
|
514 | - throw new RestException( |
|
515 | - 'invalid_field', |
|
516 | - sprintf( |
|
517 | - esc_html__('You have provided an invalid parameter: "%1$s"', 'event_espresso'), |
|
518 | - $query_param_key |
|
519 | - ) |
|
520 | - ); |
|
521 | - } else { |
|
522 | - //so it's not for a field, is it a logic query param key? |
|
523 | - if (in_array( |
|
524 | - $query_param_sans_stars, |
|
525 | - $model->logic_query_param_keys() |
|
526 | - )) { |
|
527 | - $query_param_for_models[$query_param_key] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
528 | - $query_param_value, |
|
529 | - $model, |
|
530 | - $requested_version |
|
531 | - ); |
|
532 | - } elseif (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
533 | - //only tell API clients they got it wrong if we're in debug mode |
|
534 | - //otherwise try our best ot fulfill their request by ignoring this invalid data |
|
535 | - throw new RestException( |
|
536 | - 'invalid_parameter', |
|
537 | - sprintf( |
|
538 | - esc_html__( |
|
539 | - 'You provided an invalid parameter, with key "%1$s"', |
|
540 | - 'event_espresso' |
|
541 | - ), |
|
542 | - $query_param_sans_stars |
|
543 | - ), |
|
544 | - array( |
|
545 | - 'status' => 400, |
|
546 | - ) |
|
547 | - ); |
|
548 | - } |
|
549 | - } |
|
550 | - } |
|
551 | - } |
|
552 | - return $query_param_for_models; |
|
553 | - } |
|
554 | - |
|
555 | - |
|
556 | - |
|
557 | - /** |
|
558 | - * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
559 | - * gmt date field name |
|
560 | - * |
|
561 | - * @param string $field_name |
|
562 | - * @return boolean |
|
563 | - */ |
|
564 | - public static function isGmtDateFieldName($field_name) |
|
565 | - { |
|
566 | - return substr( |
|
567 | - ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey($field_name), |
|
568 | - -4, |
|
569 | - 4 |
|
570 | - ) === '_gmt'; |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - |
|
575 | - /** |
|
576 | - * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
577 | - * |
|
578 | - * @param string $field_name |
|
579 | - * @return string |
|
580 | - */ |
|
581 | - public static function removeGmtFromFieldName($field_name) |
|
582 | - { |
|
583 | - if (! ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
584 | - return $field_name; |
|
585 | - } |
|
586 | - $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
|
587 | - $field_name |
|
588 | - ); |
|
589 | - $query_param_sans_gmt_and_sans_stars = substr( |
|
590 | - $query_param_sans_stars, |
|
591 | - 0, |
|
592 | - strrpos( |
|
593 | - $field_name, |
|
594 | - '_gmt' |
|
595 | - ) |
|
596 | - ); |
|
597 | - return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
598 | - } |
|
599 | - |
|
600 | - |
|
601 | - |
|
602 | - /** |
|
603 | - * Takes a field name from the REST API and prepares it for the model querying |
|
604 | - * |
|
605 | - * @param string $field_name |
|
606 | - * @return string |
|
607 | - */ |
|
608 | - public static function prepareFieldNameFromJson($field_name) |
|
609 | - { |
|
610 | - if (ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
611 | - return ModelDataTranslator::removeGmtFromFieldName($field_name); |
|
612 | - } |
|
613 | - return $field_name; |
|
614 | - } |
|
615 | - |
|
616 | - |
|
617 | - |
|
618 | - /** |
|
619 | - * Takes array of field names from REST API and prepares for models |
|
620 | - * |
|
621 | - * @param array $field_names |
|
622 | - * @return array of field names (possibly include model prefixes) |
|
623 | - */ |
|
624 | - public static function prepareFieldNamesFromJson(array $field_names) |
|
625 | - { |
|
626 | - $new_array = array(); |
|
627 | - foreach ($field_names as $key => $field_name) { |
|
628 | - $new_array[$key] = ModelDataTranslator::prepareFieldNameFromJson($field_name); |
|
629 | - } |
|
630 | - return $new_array; |
|
631 | - } |
|
632 | - |
|
633 | - |
|
634 | - |
|
635 | - /** |
|
636 | - * Takes array where array keys are field names (possibly with model path prefixes) |
|
637 | - * from the REST API and prepares them for model querying |
|
638 | - * |
|
639 | - * @param array $field_names_as_keys |
|
640 | - * @return array |
|
641 | - */ |
|
642 | - public static function prepareFieldNamesInArrayKeysFromJson(array $field_names_as_keys) |
|
643 | - { |
|
644 | - $new_array = array(); |
|
645 | - foreach ($field_names_as_keys as $field_name => $value) { |
|
646 | - $new_array[ModelDataTranslator::prepareFieldNameFromJson($field_name)] = $value; |
|
647 | - } |
|
648 | - return $new_array; |
|
649 | - } |
|
650 | - |
|
651 | - |
|
652 | - |
|
653 | - /** |
|
654 | - * Prepares an array of model query params for use in the REST API |
|
655 | - * |
|
656 | - * @param array $model_query_params |
|
657 | - * @param EEM_Base $model |
|
658 | - * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
659 | - * REST API |
|
660 | - * @return array which can be passed into the EE4 REST API when querying a model resource |
|
661 | - * @throws EE_Error |
|
662 | - */ |
|
663 | - public static function prepareQueryParamsForRestApi( |
|
664 | - array $model_query_params, |
|
665 | - EEM_Base $model, |
|
666 | - $requested_version = null |
|
667 | - ) { |
|
668 | - if ($requested_version === null) { |
|
669 | - $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
670 | - } |
|
671 | - $rest_query_params = $model_query_params; |
|
672 | - if (isset($model_query_params[0])) { |
|
673 | - $rest_query_params['where'] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
674 | - $model_query_params[0], |
|
675 | - $model, |
|
676 | - $requested_version |
|
677 | - ); |
|
678 | - unset($rest_query_params[0]); |
|
679 | - } |
|
680 | - if (isset($model_query_params['having'])) { |
|
681 | - $rest_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
682 | - $model_query_params['having'], |
|
683 | - $model, |
|
684 | - $requested_version |
|
685 | - ); |
|
686 | - } |
|
687 | - return apply_filters( |
|
688 | - 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
689 | - $rest_query_params, |
|
690 | - $model_query_params, |
|
691 | - $model, |
|
692 | - $requested_version |
|
693 | - ); |
|
694 | - } |
|
695 | - |
|
696 | - |
|
697 | - |
|
698 | - /** |
|
699 | - * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
700 | - * |
|
701 | - * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
702 | - * passed into EEM_Base::get_all() |
|
703 | - * @param EEM_Base $model |
|
704 | - * @param string $requested_version eg "4.8.36" |
|
705 | - * @return array ready for use in the rest api query params |
|
706 | - * @throws EE_Error |
|
707 | - * @throws ObjectDetectedException if somehow a PHP object were in the query params' values, |
|
708 | - * (which would be really unusual) |
|
709 | - */ |
|
710 | - public static function prepareConditionsQueryParamsForRestApi( |
|
711 | - $inputted_query_params_of_this_type, |
|
712 | - EEM_Base $model, |
|
713 | - $requested_version |
|
714 | - ) { |
|
715 | - $query_param_for_models = array(); |
|
716 | - foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
717 | - $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
718 | - ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey($query_param_key), |
|
719 | - $model |
|
720 | - ); |
|
721 | - if ($field instanceof EE_Model_Field_Base) { |
|
722 | - //did they specify an operator? |
|
723 | - if (is_array($query_param_value)) { |
|
724 | - $op = $query_param_value[0]; |
|
725 | - $translated_value = array($op); |
|
726 | - if (isset($query_param_value[1])) { |
|
727 | - $value = $query_param_value[1]; |
|
728 | - $translated_value[1] = ModelDataTranslator::prepareFieldValuesForJson( |
|
729 | - $field, |
|
730 | - $value, |
|
731 | - $requested_version |
|
732 | - ); |
|
733 | - } |
|
734 | - } else { |
|
735 | - $translated_value = ModelDataTranslator::prepareFieldValueForJson( |
|
736 | - $field, |
|
737 | - $query_param_value, |
|
738 | - $requested_version |
|
739 | - ); |
|
740 | - } |
|
741 | - $query_param_for_models[$query_param_key] = $translated_value; |
|
742 | - } else { |
|
743 | - //so it's not for a field, assume it's a logic query param key |
|
744 | - $query_param_for_models[$query_param_key] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
745 | - $query_param_value, |
|
746 | - $model, |
|
747 | - $requested_version |
|
748 | - ); |
|
749 | - } |
|
750 | - } |
|
751 | - return $query_param_for_models; |
|
752 | - } |
|
753 | - |
|
754 | - |
|
755 | - |
|
756 | - /** |
|
757 | - * @param $condition_query_param_key |
|
758 | - * @return string |
|
759 | - */ |
|
760 | - public static function removeStarsAndAnythingAfterFromConditionQueryParamKey($condition_query_param_key) |
|
761 | - { |
|
762 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
763 | - if ($pos_of_star === false) { |
|
764 | - return $condition_query_param_key; |
|
765 | - } else { |
|
766 | - $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
767 | - return $condition_query_param_sans_star; |
|
768 | - } |
|
769 | - } |
|
770 | - |
|
771 | - |
|
772 | - |
|
773 | - /** |
|
774 | - * Takes the input parameter and finds the model field that it indicates. |
|
775 | - * |
|
776 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
777 | - * @param EEM_Base $model |
|
778 | - * @return EE_Model_Field_Base |
|
779 | - * @throws EE_Error |
|
780 | - */ |
|
781 | - public static function deduceFieldFromQueryParam($query_param_name, EEM_Base $model) |
|
782 | - { |
|
783 | - //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
784 | - //which will help us find the database table and column |
|
785 | - $query_param_parts = explode('.', $query_param_name); |
|
786 | - if (empty($query_param_parts)) { |
|
787 | - throw new EE_Error( |
|
788 | - sprintf( |
|
789 | - __( |
|
790 | - '_extract_column_name is empty when trying to extract column and table name from %s', |
|
791 | - 'event_espresso' |
|
792 | - ), |
|
793 | - $query_param_name |
|
794 | - ) |
|
795 | - ); |
|
796 | - } |
|
797 | - $number_of_parts = count($query_param_parts); |
|
798 | - $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
799 | - if ($number_of_parts === 1) { |
|
800 | - $field_name = $last_query_param_part; |
|
801 | - } else {// $number_of_parts >= 2 |
|
802 | - //the last part is the column name, and there are only 2parts. therefore... |
|
803 | - $field_name = $last_query_param_part; |
|
804 | - $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
805 | - } |
|
806 | - try { |
|
807 | - return $model->field_settings_for($field_name, false); |
|
808 | - } catch (EE_Error $e) { |
|
809 | - return null; |
|
810 | - } |
|
811 | - } |
|
812 | - |
|
813 | - |
|
814 | - |
|
815 | - /** |
|
816 | - * Returns true if $data can be easily represented in JSON. |
|
817 | - * Basically, objects and resources can't be represented in JSON easily. |
|
818 | - * @param mixed $data |
|
819 | - * @return bool |
|
820 | - */ |
|
821 | - protected static function isRepresentableInJson($data) |
|
822 | - { |
|
823 | - return is_scalar($data) |
|
824 | - || is_array($data) |
|
825 | - || is_null($data); |
|
826 | - } |
|
39 | + /** |
|
40 | + * We used to use -1 for infinity in the rest api, but that's ambiguous for |
|
41 | + * fields that COULD contain -1; so we use null |
|
42 | + */ |
|
43 | + const EE_INF_IN_REST = null; |
|
44 | + |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * Prepares a possible array of input values from JSON for use by the models |
|
49 | + * |
|
50 | + * @param EE_Model_Field_Base $field_obj |
|
51 | + * @param mixed $original_value_maybe_array |
|
52 | + * @param string $requested_version |
|
53 | + * @param string $timezone_string treat values as being in this timezone |
|
54 | + * @return mixed |
|
55 | + * @throws RestException |
|
56 | + */ |
|
57 | + public static function prepareFieldValuesFromJson( |
|
58 | + $field_obj, |
|
59 | + $original_value_maybe_array, |
|
60 | + $requested_version, |
|
61 | + $timezone_string = 'UTC' |
|
62 | + ) { |
|
63 | + if (is_array($original_value_maybe_array) |
|
64 | + && ! $field_obj instanceof EE_Serialized_Text_Field |
|
65 | + ) { |
|
66 | + $new_value_maybe_array = array(); |
|
67 | + foreach ($original_value_maybe_array as $array_key => $array_item) { |
|
68 | + $new_value_maybe_array[$array_key] = ModelDataTranslator::prepareFieldValueFromJson( |
|
69 | + $field_obj, |
|
70 | + $array_item, |
|
71 | + $requested_version, |
|
72 | + $timezone_string |
|
73 | + ); |
|
74 | + } |
|
75 | + } else { |
|
76 | + $new_value_maybe_array = ModelDataTranslator::prepareFieldValueFromJson( |
|
77 | + $field_obj, |
|
78 | + $original_value_maybe_array, |
|
79 | + $requested_version, |
|
80 | + $timezone_string |
|
81 | + ); |
|
82 | + } |
|
83 | + return $new_value_maybe_array; |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * Prepares an array of field values FOR use in JSON/REST API |
|
90 | + * |
|
91 | + * @param EE_Model_Field_Base $field_obj |
|
92 | + * @param mixed $original_value_maybe_array |
|
93 | + * @param string $request_version (eg 4.8.36) |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public static function prepareFieldValuesForJson($field_obj, $original_value_maybe_array, $request_version) |
|
97 | + { |
|
98 | + if (is_array($original_value_maybe_array)) { |
|
99 | + $new_value = array(); |
|
100 | + foreach ($original_value_maybe_array as $key => $value) { |
|
101 | + $new_value[$key] = ModelDataTranslator::prepareFieldValuesForJson($field_obj, $value, $request_version); |
|
102 | + } |
|
103 | + } else { |
|
104 | + $new_value = ModelDataTranslator::prepareFieldValueForJson( |
|
105 | + $field_obj, |
|
106 | + $original_value_maybe_array, |
|
107 | + $request_version |
|
108 | + ); |
|
109 | + } |
|
110 | + return $new_value; |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * Prepares incoming data from the json or $_REQUEST parameters for the models' |
|
117 | + * "$query_params". |
|
118 | + * |
|
119 | + * @param EE_Model_Field_Base $field_obj |
|
120 | + * @param mixed $original_value |
|
121 | + * @param string $requested_version |
|
122 | + * @param string $timezone_string treat values as being in this timezone |
|
123 | + * @return mixed |
|
124 | + * @throws RestException |
|
125 | + */ |
|
126 | + public static function prepareFieldValueFromJson( |
|
127 | + $field_obj, |
|
128 | + $original_value, |
|
129 | + $requested_version, |
|
130 | + $timezone_string = 'UTC' // UTC |
|
131 | + ) { |
|
132 | + //check if they accidentally submitted an error value. If so throw an exception |
|
133 | + if (is_array($original_value) |
|
134 | + && isset($original_value['error_code'], $original_value['error_message'])) { |
|
135 | + throw new RestException( |
|
136 | + 'rest_submitted_error_value', |
|
137 | + sprintf( |
|
138 | + esc_html__( |
|
139 | + 'You tried to submit a JSON error object as a value for %1$s. That\'s not allowed.', |
|
140 | + 'event_espresso' |
|
141 | + ), |
|
142 | + $field_obj->get_name() |
|
143 | + ), |
|
144 | + array( |
|
145 | + 'status' => 400, |
|
146 | + ) |
|
147 | + ); |
|
148 | + } |
|
149 | + //double-check for serialized PHP. We never accept serialized PHP. No way Jose. |
|
150 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($original_value); |
|
151 | + $timezone_string = $timezone_string !== '' ? $timezone_string : get_option('timezone_string', ''); |
|
152 | + $new_value = null; |
|
153 | + //walk through the submitted data and double-check for serialized PHP. We never accept serialized PHP. No |
|
154 | + // way Jose. |
|
155 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($original_value); |
|
156 | + if ($field_obj instanceof EE_Infinite_Integer_Field |
|
157 | + && in_array($original_value, array(null, ''), true) |
|
158 | + ) { |
|
159 | + $new_value = EE_INF; |
|
160 | + } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
161 | + list($offset_sign, $offset_secs) = ModelDataTranslator::parseTimezoneOffset( |
|
162 | + $field_obj->get_timezone_offset( |
|
163 | + new \DateTimeZone($timezone_string), |
|
164 | + $original_value |
|
165 | + ) |
|
166 | + ); |
|
167 | + $offset_string = |
|
168 | + str_pad( |
|
169 | + floor($offset_secs / HOUR_IN_SECONDS), |
|
170 | + 2, |
|
171 | + '0', |
|
172 | + STR_PAD_LEFT |
|
173 | + ) |
|
174 | + . ':' |
|
175 | + . str_pad( |
|
176 | + ($offset_secs % HOUR_IN_SECONDS) / MINUTE_IN_SECONDS, |
|
177 | + 2, |
|
178 | + '0', |
|
179 | + STR_PAD_LEFT |
|
180 | + ); |
|
181 | + $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
182 | + } else { |
|
183 | + $new_value = $original_value; |
|
184 | + } |
|
185 | + return $new_value; |
|
186 | + } |
|
187 | + |
|
188 | + |
|
189 | + |
|
190 | + /** |
|
191 | + * Throws an exception if $data is a serialized PHP string (or somehow an actually PHP object, although I don't |
|
192 | + * think that can happen). If $data is an array, recurses into its keys and values |
|
193 | + * @param mixed $data |
|
194 | + * @throws RestException |
|
195 | + * @return void |
|
196 | + */ |
|
197 | + public static function throwExceptionIfContainsSerializedData($data) |
|
198 | + { |
|
199 | + if (is_array($data)) { |
|
200 | + foreach ($data as $key => $value) { |
|
201 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($key); |
|
202 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($value); |
|
203 | + } |
|
204 | + } else { |
|
205 | + if (is_serialized($data) || is_object($data)) { |
|
206 | + throw new RestException( |
|
207 | + 'serialized_data_submission_prohibited', |
|
208 | + esc_html__( |
|
209 | + // @codingStandardsIgnoreStart |
|
210 | + 'You tried to submit a string of serialized text. Serialized PHP is prohibited over the EE4 REST API.', |
|
211 | + // @codingStandardsIgnoreEnd |
|
212 | + 'event_espresso' |
|
213 | + ) |
|
214 | + ); |
|
215 | + } |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * determines what's going on with them timezone strings |
|
223 | + * |
|
224 | + * @param int $timezone_offset |
|
225 | + * @return array |
|
226 | + */ |
|
227 | + private static function parseTimezoneOffset($timezone_offset) |
|
228 | + { |
|
229 | + $first_char = substr((string)$timezone_offset, 0, 1); |
|
230 | + if ($first_char === '+' || $first_char === '-') { |
|
231 | + $offset_sign = $first_char; |
|
232 | + $offset_secs = substr((string)$timezone_offset, 1); |
|
233 | + } else { |
|
234 | + $offset_sign = '+'; |
|
235 | + $offset_secs = $timezone_offset; |
|
236 | + } |
|
237 | + return array($offset_sign, $offset_secs); |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * Prepares a field's value for display in the API |
|
244 | + * |
|
245 | + * @param EE_Model_Field_Base $field_obj |
|
246 | + * @param mixed $original_value |
|
247 | + * @param string $requested_version |
|
248 | + * @return mixed |
|
249 | + */ |
|
250 | + public static function prepareFieldValueForJson($field_obj, $original_value, $requested_version) |
|
251 | + { |
|
252 | + if ($original_value === EE_INF) { |
|
253 | + $new_value = ModelDataTranslator::EE_INF_IN_REST; |
|
254 | + } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
255 | + if (is_string($original_value)) { |
|
256 | + //did they submit a string of a unix timestamp? |
|
257 | + if (is_numeric($original_value)) { |
|
258 | + $datetime_obj = new \DateTime(); |
|
259 | + $datetime_obj->setTimestamp((int)$original_value); |
|
260 | + } else { |
|
261 | + //first, check if its a MySQL timestamp in GMT |
|
262 | + $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
|
263 | + } |
|
264 | + if (! $datetime_obj instanceof \DateTime) { |
|
265 | + //so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
|
266 | + $datetime_obj = $field_obj->prepare_for_set($original_value); |
|
267 | + } |
|
268 | + $original_value = $datetime_obj; |
|
269 | + } |
|
270 | + if ($original_value instanceof \DateTime) { |
|
271 | + $new_value = $original_value->format('Y-m-d H:i:s'); |
|
272 | + } elseif (is_int($original_value)) { |
|
273 | + $new_value = date('Y-m-d H:i:s', $original_value); |
|
274 | + } elseif($original_value === null || $original_value === '') { |
|
275 | + $new_value = null; |
|
276 | + } else { |
|
277 | + //so it's not a datetime object, unix timestamp (as string or int), |
|
278 | + //MySQL timestamp, or even a string in the field object's format. So no idea what it is |
|
279 | + throw new \EE_Error( |
|
280 | + sprintf( |
|
281 | + esc_html__( |
|
282 | + // @codingStandardsIgnoreStart |
|
283 | + 'The value "%1$s" for the field "%2$s" on model "%3$s" could not be understood. It should be a PHP DateTime, unix timestamp, MySQL date, or string in the format "%4$s".', |
|
284 | + // @codingStandardsIgnoreEnd |
|
285 | + 'event_espressso' |
|
286 | + ), |
|
287 | + $original_value, |
|
288 | + $field_obj->get_name(), |
|
289 | + $field_obj->get_model_name(), |
|
290 | + $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
291 | + ) |
|
292 | + ); |
|
293 | + } |
|
294 | + $new_value = mysql_to_rfc3339($new_value); |
|
295 | + } else { |
|
296 | + $new_value = $original_value; |
|
297 | + } |
|
298 | + //are we about to send an object? just don't. We have no good way to represent it in JSON. |
|
299 | + // can't just check using is_object() because that missed PHP incomplete objects |
|
300 | + if (! ModelDataTranslator::isRepresentableInJson($new_value)) { |
|
301 | + $new_value = array( |
|
302 | + 'error_code' => 'php_object_not_return', |
|
303 | + 'error_message' => esc_html__('The value of this field in the database is a PHP object, which can\'t be represented in JSON.', 'event_espresso') |
|
304 | + ); |
|
305 | + } |
|
306 | + return apply_filters( |
|
307 | + 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_field_for_rest_api', |
|
308 | + $new_value, |
|
309 | + $field_obj, |
|
310 | + $original_value, |
|
311 | + $requested_version |
|
312 | + ); |
|
313 | + } |
|
314 | + |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * Prepares condition-query-parameters (like what's in where and having) from |
|
319 | + * the format expected in the API to use in the models |
|
320 | + * |
|
321 | + * @param array $inputted_query_params_of_this_type |
|
322 | + * @param EEM_Base $model |
|
323 | + * @param string $requested_version |
|
324 | + * @param boolean $writing whether this data will be written to the DB, or if we're just building a query. |
|
325 | + * If we're writing to the DB, we don't expect any operators, or any logic query parameters, |
|
326 | + * and we also won't accept serialized data unless the current user has unfiltered_html. |
|
327 | + * @return array |
|
328 | + * @throws \DomainException |
|
329 | + * @throws RestException |
|
330 | + * @throws EE_Error |
|
331 | + */ |
|
332 | + public static function prepareConditionsQueryParamsForModels( |
|
333 | + $inputted_query_params_of_this_type, |
|
334 | + EEM_Base $model, |
|
335 | + $requested_version, |
|
336 | + $writing = false |
|
337 | + ) { |
|
338 | + $query_param_for_models = array(); |
|
339 | + $valid_operators = $model->valid_operators(); |
|
340 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
341 | + $is_gmt_datetime_field = false; |
|
342 | + $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
|
343 | + $query_param_key |
|
344 | + ); |
|
345 | + $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
346 | + $query_param_sans_stars, |
|
347 | + $model |
|
348 | + ); |
|
349 | + //double-check is it a *_gmt field? |
|
350 | + if (! $field instanceof EE_Model_Field_Base |
|
351 | + && ModelDataTranslator::isGmtDateFieldName($query_param_sans_stars) |
|
352 | + ) { |
|
353 | + //yep, take off '_gmt', and find the field |
|
354 | + $query_param_key = ModelDataTranslator::removeGmtFromFieldName($query_param_sans_stars); |
|
355 | + $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
356 | + $query_param_key, |
|
357 | + $model |
|
358 | + ); |
|
359 | + $timezone = 'UTC'; |
|
360 | + $is_gmt_datetime_field = true; |
|
361 | + } elseif ($field instanceof EE_Datetime_Field) { |
|
362 | + //so it's not a GMT field. Set the timezone on the model to the default |
|
363 | + $timezone = \EEH_DTT_Helper::get_valid_timezone_string(); |
|
364 | + } else { |
|
365 | + //just keep using what's already set for the timezone |
|
366 | + $timezone = $model->get_timezone(); |
|
367 | + } |
|
368 | + if ($field instanceof EE_Model_Field_Base) { |
|
369 | + if (! $writing && is_array($query_param_value)) { |
|
370 | + if (! \EEH_Array::is_array_numerically_and_sequentially_indexed($query_param_value)) { |
|
371 | + if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
372 | + throw new RestException( |
|
373 | + 'numerically_indexed_array_of_values_only', |
|
374 | + sprintf( |
|
375 | + esc_html__( |
|
376 | + 'The array provided for the parameter "%1$s" should be numerically indexed.', |
|
377 | + 'event_espresso' |
|
378 | + ), |
|
379 | + $query_param_key |
|
380 | + ), |
|
381 | + array( |
|
382 | + 'status' => 400, |
|
383 | + ) |
|
384 | + ); |
|
385 | + } |
|
386 | + } |
|
387 | + //did they specify an operator? |
|
388 | + if (isset($query_param_value[0]) |
|
389 | + && isset($valid_operators[$query_param_value[0]]) |
|
390 | + ) { |
|
391 | + $op = $query_param_value[0]; |
|
392 | + $translated_value = array($op); |
|
393 | + if (array_key_exists($op, $model->valid_in_style_operators()) |
|
394 | + && isset($query_param_value[1]) |
|
395 | + && ! isset($query_param_value[2]) |
|
396 | + ) { |
|
397 | + $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
398 | + $field, |
|
399 | + $query_param_value[1], |
|
400 | + $requested_version, |
|
401 | + $timezone |
|
402 | + ); |
|
403 | + } elseif (array_key_exists($op, $model->valid_between_style_operators()) |
|
404 | + && isset($query_param_value[1], $query_param_value[2]) |
|
405 | + && !isset($query_param_value[3]) |
|
406 | + ) { |
|
407 | + $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
408 | + $field, |
|
409 | + $query_param_value[1], |
|
410 | + $requested_version, |
|
411 | + $timezone |
|
412 | + ); |
|
413 | + $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
414 | + $field, |
|
415 | + $query_param_value[2], |
|
416 | + $requested_version, |
|
417 | + $timezone |
|
418 | + ); |
|
419 | + } elseif (array_key_exists($op, $model->valid_like_style_operators()) |
|
420 | + && isset($query_param_value[1]) |
|
421 | + && ! isset($query_param_value[2]) |
|
422 | + ) { |
|
423 | + //we want to leave this value mostly-as-is (eg don't force it to be a float |
|
424 | + //or a boolean or an enum value. Leave it as-is with wildcards etc) |
|
425 | + //but do verify it at least doesn't have any serialized data |
|
426 | + ModelDataTranslator::throwExceptionIfContainsSerializedData($query_param_value[1]); |
|
427 | + $translated_value[] = $query_param_value[1]; |
|
428 | + } elseif (array_key_exists($op, $model->valid_null_style_operators()) |
|
429 | + && !isset($query_param_value[1])) { |
|
430 | + //no arguments should have been provided, so don't look for any |
|
431 | + } elseif (isset($query_param_value[1]) |
|
432 | + && !isset($query_param_value[2]) |
|
433 | + && ! array_key_exists( |
|
434 | + $op, |
|
435 | + array_merge( |
|
436 | + $model->valid_in_style_operators(), |
|
437 | + $model->valid_null_style_operators(), |
|
438 | + $model->valid_like_style_operators(), |
|
439 | + $model->valid_between_style_operators() |
|
440 | + ) |
|
441 | + ) |
|
442 | + ) { |
|
443 | + //it's a valid operator, but none of the exceptions. Treat it normally. |
|
444 | + $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
|
445 | + $field, |
|
446 | + $query_param_value[1], |
|
447 | + $requested_version, |
|
448 | + $timezone |
|
449 | + ); |
|
450 | + } else { |
|
451 | + //so they provided a valid operator, but wrong number of arguments |
|
452 | + if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
453 | + throw new RestException( |
|
454 | + 'wrong_number_of_arguments', |
|
455 | + sprintf( |
|
456 | + esc_html__( |
|
457 | + 'The operator you provided, "%1$s" had the wrong number of arguments', |
|
458 | + 'event_espresso' |
|
459 | + ), |
|
460 | + $op |
|
461 | + ), |
|
462 | + array( |
|
463 | + 'status' => 400, |
|
464 | + ) |
|
465 | + ); |
|
466 | + } |
|
467 | + $translated_value = null; |
|
468 | + } |
|
469 | + } else { |
|
470 | + //so they didn't provide a valid operator |
|
471 | + if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
472 | + throw new RestException( |
|
473 | + 'invalid_operator', |
|
474 | + sprintf( |
|
475 | + esc_html__( |
|
476 | + 'You provided an invalid parameter, with key "%1$s" and value "%2$s"', |
|
477 | + 'event_espresso' |
|
478 | + ), |
|
479 | + $query_param_key, |
|
480 | + $query_param_value |
|
481 | + ), |
|
482 | + array( |
|
483 | + 'status' => 400, |
|
484 | + ) |
|
485 | + ); |
|
486 | + } |
|
487 | + //if we aren't in debug mode, then just try our best to fulfill the user's request |
|
488 | + $translated_value = null; |
|
489 | + } |
|
490 | + } else { |
|
491 | + $translated_value = ModelDataTranslator::prepareFieldValueFromJson( |
|
492 | + $field, |
|
493 | + $query_param_value, |
|
494 | + $requested_version, |
|
495 | + $timezone |
|
496 | + ); |
|
497 | + } |
|
498 | + if ( |
|
499 | + (isset($query_param_for_models[$query_param_key]) && $is_gmt_datetime_field) |
|
500 | + || |
|
501 | + $translated_value === null |
|
502 | + ) { |
|
503 | + //they have already provided a non-gmt field, ignore the gmt one. That's what WP core |
|
504 | + //currently does (they might change it though). See https://core.trac.wordpress.org/ticket/39954 |
|
505 | + //OR we couldn't create a translated value from their input |
|
506 | + continue; |
|
507 | + } |
|
508 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
509 | + } else { |
|
510 | + //so this param doesn't correspond to a field eh? |
|
511 | + if ($writing) { |
|
512 | + //always tell API clients about invalid parameters when they're creating data. Otherwise, |
|
513 | + //they are probably going to create invalid data |
|
514 | + throw new RestException( |
|
515 | + 'invalid_field', |
|
516 | + sprintf( |
|
517 | + esc_html__('You have provided an invalid parameter: "%1$s"', 'event_espresso'), |
|
518 | + $query_param_key |
|
519 | + ) |
|
520 | + ); |
|
521 | + } else { |
|
522 | + //so it's not for a field, is it a logic query param key? |
|
523 | + if (in_array( |
|
524 | + $query_param_sans_stars, |
|
525 | + $model->logic_query_param_keys() |
|
526 | + )) { |
|
527 | + $query_param_for_models[$query_param_key] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
528 | + $query_param_value, |
|
529 | + $model, |
|
530 | + $requested_version |
|
531 | + ); |
|
532 | + } elseif (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
|
533 | + //only tell API clients they got it wrong if we're in debug mode |
|
534 | + //otherwise try our best ot fulfill their request by ignoring this invalid data |
|
535 | + throw new RestException( |
|
536 | + 'invalid_parameter', |
|
537 | + sprintf( |
|
538 | + esc_html__( |
|
539 | + 'You provided an invalid parameter, with key "%1$s"', |
|
540 | + 'event_espresso' |
|
541 | + ), |
|
542 | + $query_param_sans_stars |
|
543 | + ), |
|
544 | + array( |
|
545 | + 'status' => 400, |
|
546 | + ) |
|
547 | + ); |
|
548 | + } |
|
549 | + } |
|
550 | + } |
|
551 | + } |
|
552 | + return $query_param_for_models; |
|
553 | + } |
|
554 | + |
|
555 | + |
|
556 | + |
|
557 | + /** |
|
558 | + * Mostly checks if the last 4 characters are "_gmt", indicating its a |
|
559 | + * gmt date field name |
|
560 | + * |
|
561 | + * @param string $field_name |
|
562 | + * @return boolean |
|
563 | + */ |
|
564 | + public static function isGmtDateFieldName($field_name) |
|
565 | + { |
|
566 | + return substr( |
|
567 | + ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey($field_name), |
|
568 | + -4, |
|
569 | + 4 |
|
570 | + ) === '_gmt'; |
|
571 | + } |
|
572 | + |
|
573 | + |
|
574 | + |
|
575 | + /** |
|
576 | + * Removes the last "_gmt" part of a field name (and if there is no "_gmt" at the end, leave it alone) |
|
577 | + * |
|
578 | + * @param string $field_name |
|
579 | + * @return string |
|
580 | + */ |
|
581 | + public static function removeGmtFromFieldName($field_name) |
|
582 | + { |
|
583 | + if (! ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
584 | + return $field_name; |
|
585 | + } |
|
586 | + $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
|
587 | + $field_name |
|
588 | + ); |
|
589 | + $query_param_sans_gmt_and_sans_stars = substr( |
|
590 | + $query_param_sans_stars, |
|
591 | + 0, |
|
592 | + strrpos( |
|
593 | + $field_name, |
|
594 | + '_gmt' |
|
595 | + ) |
|
596 | + ); |
|
597 | + return str_replace($query_param_sans_stars, $query_param_sans_gmt_and_sans_stars, $field_name); |
|
598 | + } |
|
599 | + |
|
600 | + |
|
601 | + |
|
602 | + /** |
|
603 | + * Takes a field name from the REST API and prepares it for the model querying |
|
604 | + * |
|
605 | + * @param string $field_name |
|
606 | + * @return string |
|
607 | + */ |
|
608 | + public static function prepareFieldNameFromJson($field_name) |
|
609 | + { |
|
610 | + if (ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
611 | + return ModelDataTranslator::removeGmtFromFieldName($field_name); |
|
612 | + } |
|
613 | + return $field_name; |
|
614 | + } |
|
615 | + |
|
616 | + |
|
617 | + |
|
618 | + /** |
|
619 | + * Takes array of field names from REST API and prepares for models |
|
620 | + * |
|
621 | + * @param array $field_names |
|
622 | + * @return array of field names (possibly include model prefixes) |
|
623 | + */ |
|
624 | + public static function prepareFieldNamesFromJson(array $field_names) |
|
625 | + { |
|
626 | + $new_array = array(); |
|
627 | + foreach ($field_names as $key => $field_name) { |
|
628 | + $new_array[$key] = ModelDataTranslator::prepareFieldNameFromJson($field_name); |
|
629 | + } |
|
630 | + return $new_array; |
|
631 | + } |
|
632 | + |
|
633 | + |
|
634 | + |
|
635 | + /** |
|
636 | + * Takes array where array keys are field names (possibly with model path prefixes) |
|
637 | + * from the REST API and prepares them for model querying |
|
638 | + * |
|
639 | + * @param array $field_names_as_keys |
|
640 | + * @return array |
|
641 | + */ |
|
642 | + public static function prepareFieldNamesInArrayKeysFromJson(array $field_names_as_keys) |
|
643 | + { |
|
644 | + $new_array = array(); |
|
645 | + foreach ($field_names_as_keys as $field_name => $value) { |
|
646 | + $new_array[ModelDataTranslator::prepareFieldNameFromJson($field_name)] = $value; |
|
647 | + } |
|
648 | + return $new_array; |
|
649 | + } |
|
650 | + |
|
651 | + |
|
652 | + |
|
653 | + /** |
|
654 | + * Prepares an array of model query params for use in the REST API |
|
655 | + * |
|
656 | + * @param array $model_query_params |
|
657 | + * @param EEM_Base $model |
|
658 | + * @param string $requested_version eg "4.8.36". If null is provided, defaults to the latest release of the EE4 |
|
659 | + * REST API |
|
660 | + * @return array which can be passed into the EE4 REST API when querying a model resource |
|
661 | + * @throws EE_Error |
|
662 | + */ |
|
663 | + public static function prepareQueryParamsForRestApi( |
|
664 | + array $model_query_params, |
|
665 | + EEM_Base $model, |
|
666 | + $requested_version = null |
|
667 | + ) { |
|
668 | + if ($requested_version === null) { |
|
669 | + $requested_version = \EED_Core_Rest_Api::latest_rest_api_version(); |
|
670 | + } |
|
671 | + $rest_query_params = $model_query_params; |
|
672 | + if (isset($model_query_params[0])) { |
|
673 | + $rest_query_params['where'] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
674 | + $model_query_params[0], |
|
675 | + $model, |
|
676 | + $requested_version |
|
677 | + ); |
|
678 | + unset($rest_query_params[0]); |
|
679 | + } |
|
680 | + if (isset($model_query_params['having'])) { |
|
681 | + $rest_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
682 | + $model_query_params['having'], |
|
683 | + $model, |
|
684 | + $requested_version |
|
685 | + ); |
|
686 | + } |
|
687 | + return apply_filters( |
|
688 | + 'FHEE__EventEspresso\core\libraries\rest_api\Model_Data_Translator__prepare_query_params_for_rest_api', |
|
689 | + $rest_query_params, |
|
690 | + $model_query_params, |
|
691 | + $model, |
|
692 | + $requested_version |
|
693 | + ); |
|
694 | + } |
|
695 | + |
|
696 | + |
|
697 | + |
|
698 | + /** |
|
699 | + * Prepares all the sub-conditions query parameters (eg having or where conditions) for use in the rest api |
|
700 | + * |
|
701 | + * @param array $inputted_query_params_of_this_type eg like the "where" or "having" conditions query params |
|
702 | + * passed into EEM_Base::get_all() |
|
703 | + * @param EEM_Base $model |
|
704 | + * @param string $requested_version eg "4.8.36" |
|
705 | + * @return array ready for use in the rest api query params |
|
706 | + * @throws EE_Error |
|
707 | + * @throws ObjectDetectedException if somehow a PHP object were in the query params' values, |
|
708 | + * (which would be really unusual) |
|
709 | + */ |
|
710 | + public static function prepareConditionsQueryParamsForRestApi( |
|
711 | + $inputted_query_params_of_this_type, |
|
712 | + EEM_Base $model, |
|
713 | + $requested_version |
|
714 | + ) { |
|
715 | + $query_param_for_models = array(); |
|
716 | + foreach ($inputted_query_params_of_this_type as $query_param_key => $query_param_value) { |
|
717 | + $field = ModelDataTranslator::deduceFieldFromQueryParam( |
|
718 | + ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey($query_param_key), |
|
719 | + $model |
|
720 | + ); |
|
721 | + if ($field instanceof EE_Model_Field_Base) { |
|
722 | + //did they specify an operator? |
|
723 | + if (is_array($query_param_value)) { |
|
724 | + $op = $query_param_value[0]; |
|
725 | + $translated_value = array($op); |
|
726 | + if (isset($query_param_value[1])) { |
|
727 | + $value = $query_param_value[1]; |
|
728 | + $translated_value[1] = ModelDataTranslator::prepareFieldValuesForJson( |
|
729 | + $field, |
|
730 | + $value, |
|
731 | + $requested_version |
|
732 | + ); |
|
733 | + } |
|
734 | + } else { |
|
735 | + $translated_value = ModelDataTranslator::prepareFieldValueForJson( |
|
736 | + $field, |
|
737 | + $query_param_value, |
|
738 | + $requested_version |
|
739 | + ); |
|
740 | + } |
|
741 | + $query_param_for_models[$query_param_key] = $translated_value; |
|
742 | + } else { |
|
743 | + //so it's not for a field, assume it's a logic query param key |
|
744 | + $query_param_for_models[$query_param_key] = ModelDataTranslator::prepareConditionsQueryParamsForRestApi( |
|
745 | + $query_param_value, |
|
746 | + $model, |
|
747 | + $requested_version |
|
748 | + ); |
|
749 | + } |
|
750 | + } |
|
751 | + return $query_param_for_models; |
|
752 | + } |
|
753 | + |
|
754 | + |
|
755 | + |
|
756 | + /** |
|
757 | + * @param $condition_query_param_key |
|
758 | + * @return string |
|
759 | + */ |
|
760 | + public static function removeStarsAndAnythingAfterFromConditionQueryParamKey($condition_query_param_key) |
|
761 | + { |
|
762 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
763 | + if ($pos_of_star === false) { |
|
764 | + return $condition_query_param_key; |
|
765 | + } else { |
|
766 | + $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
767 | + return $condition_query_param_sans_star; |
|
768 | + } |
|
769 | + } |
|
770 | + |
|
771 | + |
|
772 | + |
|
773 | + /** |
|
774 | + * Takes the input parameter and finds the model field that it indicates. |
|
775 | + * |
|
776 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
777 | + * @param EEM_Base $model |
|
778 | + * @return EE_Model_Field_Base |
|
779 | + * @throws EE_Error |
|
780 | + */ |
|
781 | + public static function deduceFieldFromQueryParam($query_param_name, EEM_Base $model) |
|
782 | + { |
|
783 | + //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
784 | + //which will help us find the database table and column |
|
785 | + $query_param_parts = explode('.', $query_param_name); |
|
786 | + if (empty($query_param_parts)) { |
|
787 | + throw new EE_Error( |
|
788 | + sprintf( |
|
789 | + __( |
|
790 | + '_extract_column_name is empty when trying to extract column and table name from %s', |
|
791 | + 'event_espresso' |
|
792 | + ), |
|
793 | + $query_param_name |
|
794 | + ) |
|
795 | + ); |
|
796 | + } |
|
797 | + $number_of_parts = count($query_param_parts); |
|
798 | + $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
799 | + if ($number_of_parts === 1) { |
|
800 | + $field_name = $last_query_param_part; |
|
801 | + } else {// $number_of_parts >= 2 |
|
802 | + //the last part is the column name, and there are only 2parts. therefore... |
|
803 | + $field_name = $last_query_param_part; |
|
804 | + $model = \EE_Registry::instance()->load_model($query_param_parts[$number_of_parts - 2]); |
|
805 | + } |
|
806 | + try { |
|
807 | + return $model->field_settings_for($field_name, false); |
|
808 | + } catch (EE_Error $e) { |
|
809 | + return null; |
|
810 | + } |
|
811 | + } |
|
812 | + |
|
813 | + |
|
814 | + |
|
815 | + /** |
|
816 | + * Returns true if $data can be easily represented in JSON. |
|
817 | + * Basically, objects and resources can't be represented in JSON easily. |
|
818 | + * @param mixed $data |
|
819 | + * @return bool |
|
820 | + */ |
|
821 | + protected static function isRepresentableInJson($data) |
|
822 | + { |
|
823 | + return is_scalar($data) |
|
824 | + || is_array($data) |
|
825 | + || is_null($data); |
|
826 | + } |
|
827 | 827 | } |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | use EE_Serialized_Text_Field; |
11 | 11 | use EEM_Base; |
12 | 12 | |
13 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
13 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
14 | 14 | exit('No direct script access allowed'); |
15 | 15 | } |
16 | 16 | |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | '0', |
179 | 179 | STR_PAD_LEFT |
180 | 180 | ); |
181 | - $new_value = rest_parse_date($original_value . $offset_sign . $offset_string); |
|
181 | + $new_value = rest_parse_date($original_value.$offset_sign.$offset_string); |
|
182 | 182 | } else { |
183 | 183 | $new_value = $original_value; |
184 | 184 | } |
@@ -226,10 +226,10 @@ discard block |
||
226 | 226 | */ |
227 | 227 | private static function parseTimezoneOffset($timezone_offset) |
228 | 228 | { |
229 | - $first_char = substr((string)$timezone_offset, 0, 1); |
|
229 | + $first_char = substr((string) $timezone_offset, 0, 1); |
|
230 | 230 | if ($first_char === '+' || $first_char === '-') { |
231 | 231 | $offset_sign = $first_char; |
232 | - $offset_secs = substr((string)$timezone_offset, 1); |
|
232 | + $offset_secs = substr((string) $timezone_offset, 1); |
|
233 | 233 | } else { |
234 | 234 | $offset_sign = '+'; |
235 | 235 | $offset_secs = $timezone_offset; |
@@ -256,12 +256,12 @@ discard block |
||
256 | 256 | //did they submit a string of a unix timestamp? |
257 | 257 | if (is_numeric($original_value)) { |
258 | 258 | $datetime_obj = new \DateTime(); |
259 | - $datetime_obj->setTimestamp((int)$original_value); |
|
259 | + $datetime_obj->setTimestamp((int) $original_value); |
|
260 | 260 | } else { |
261 | 261 | //first, check if its a MySQL timestamp in GMT |
262 | 262 | $datetime_obj = \DateTime::createFromFormat('Y-m-d H:i:s', $original_value); |
263 | 263 | } |
264 | - if (! $datetime_obj instanceof \DateTime) { |
|
264 | + if ( ! $datetime_obj instanceof \DateTime) { |
|
265 | 265 | //so it's not a unix timestamp or a MySQL timestamp. Maybe its in the field's date/time format? |
266 | 266 | $datetime_obj = $field_obj->prepare_for_set($original_value); |
267 | 267 | } |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | $new_value = $original_value->format('Y-m-d H:i:s'); |
272 | 272 | } elseif (is_int($original_value)) { |
273 | 273 | $new_value = date('Y-m-d H:i:s', $original_value); |
274 | - } elseif($original_value === null || $original_value === '') { |
|
274 | + } elseif ($original_value === null || $original_value === '') { |
|
275 | 275 | $new_value = null; |
276 | 276 | } else { |
277 | 277 | //so it's not a datetime object, unix timestamp (as string or int), |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | $original_value, |
288 | 288 | $field_obj->get_name(), |
289 | 289 | $field_obj->get_model_name(), |
290 | - $field_obj->get_time_format() . ' ' . $field_obj->get_time_format() |
|
290 | + $field_obj->get_time_format().' '.$field_obj->get_time_format() |
|
291 | 291 | ) |
292 | 292 | ); |
293 | 293 | } |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | } |
298 | 298 | //are we about to send an object? just don't. We have no good way to represent it in JSON. |
299 | 299 | // can't just check using is_object() because that missed PHP incomplete objects |
300 | - if (! ModelDataTranslator::isRepresentableInJson($new_value)) { |
|
300 | + if ( ! ModelDataTranslator::isRepresentableInJson($new_value)) { |
|
301 | 301 | $new_value = array( |
302 | 302 | 'error_code' => 'php_object_not_return', |
303 | 303 | 'error_message' => esc_html__('The value of this field in the database is a PHP object, which can\'t be represented in JSON.', 'event_espresso') |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | $model |
348 | 348 | ); |
349 | 349 | //double-check is it a *_gmt field? |
350 | - if (! $field instanceof EE_Model_Field_Base |
|
350 | + if ( ! $field instanceof EE_Model_Field_Base |
|
351 | 351 | && ModelDataTranslator::isGmtDateFieldName($query_param_sans_stars) |
352 | 352 | ) { |
353 | 353 | //yep, take off '_gmt', and find the field |
@@ -366,8 +366,8 @@ discard block |
||
366 | 366 | $timezone = $model->get_timezone(); |
367 | 367 | } |
368 | 368 | if ($field instanceof EE_Model_Field_Base) { |
369 | - if (! $writing && is_array($query_param_value)) { |
|
370 | - if (! \EEH_Array::is_array_numerically_and_sequentially_indexed($query_param_value)) { |
|
369 | + if ( ! $writing && is_array($query_param_value)) { |
|
370 | + if ( ! \EEH_Array::is_array_numerically_and_sequentially_indexed($query_param_value)) { |
|
371 | 371 | if (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE) { |
372 | 372 | throw new RestException( |
373 | 373 | 'numerically_indexed_array_of_values_only', |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | ); |
403 | 403 | } elseif (array_key_exists($op, $model->valid_between_style_operators()) |
404 | 404 | && isset($query_param_value[1], $query_param_value[2]) |
405 | - && !isset($query_param_value[3]) |
|
405 | + && ! isset($query_param_value[3]) |
|
406 | 406 | ) { |
407 | 407 | $translated_value[] = ModelDataTranslator::prepareFieldValuesFromJson( |
408 | 408 | $field, |
@@ -426,10 +426,10 @@ discard block |
||
426 | 426 | ModelDataTranslator::throwExceptionIfContainsSerializedData($query_param_value[1]); |
427 | 427 | $translated_value[] = $query_param_value[1]; |
428 | 428 | } elseif (array_key_exists($op, $model->valid_null_style_operators()) |
429 | - && !isset($query_param_value[1])) { |
|
429 | + && ! isset($query_param_value[1])) { |
|
430 | 430 | //no arguments should have been provided, so don't look for any |
431 | 431 | } elseif (isset($query_param_value[1]) |
432 | - && !isset($query_param_value[2]) |
|
432 | + && ! isset($query_param_value[2]) |
|
433 | 433 | && ! array_key_exists( |
434 | 434 | $op, |
435 | 435 | array_merge( |
@@ -580,7 +580,7 @@ discard block |
||
580 | 580 | */ |
581 | 581 | public static function removeGmtFromFieldName($field_name) |
582 | 582 | { |
583 | - if (! ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
583 | + if ( ! ModelDataTranslator::isGmtDateFieldName($field_name)) { |
|
584 | 584 | return $field_name; |
585 | 585 | } |
586 | 586 | $query_param_sans_stars = ModelDataTranslator::removeStarsAndAnythingAfterFromConditionQueryParamKey( |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | use EventEspresso\core\services\loaders\LoaderInterface; |
6 | 6 | |
7 | 7 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
8 | - exit('No direct script access allowed'); |
|
8 | + exit('No direct script access allowed'); |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | |
@@ -22,782 +22,782 @@ discard block |
||
22 | 22 | class EE_Dependency_Map |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * This means that the requested class dependency is not present in the dependency map |
|
27 | - */ |
|
28 | - const not_registered = 0; |
|
29 | - |
|
30 | - /** |
|
31 | - * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
32 | - */ |
|
33 | - const load_new_object = 1; |
|
34 | - |
|
35 | - /** |
|
36 | - * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
37 | - * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
38 | - */ |
|
39 | - const load_from_cache = 2; |
|
40 | - |
|
41 | - /** |
|
42 | - * When registering a dependency, |
|
43 | - * this indicates to keep any existing dependencies that already exist, |
|
44 | - * and simply discard any new dependencies declared in the incoming data |
|
45 | - */ |
|
46 | - const KEEP_EXISTING_DEPENDENCIES = 0; |
|
47 | - |
|
48 | - /** |
|
49 | - * When registering a dependency, |
|
50 | - * this indicates to overwrite any existing dependencies that already exist using the incoming data |
|
51 | - */ |
|
52 | - const OVERWRITE_DEPENDENCIES = 1; |
|
53 | - |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * @type EE_Dependency_Map $_instance |
|
58 | - */ |
|
59 | - protected static $_instance; |
|
60 | - |
|
61 | - /** |
|
62 | - * @type EE_Request $request |
|
63 | - */ |
|
64 | - protected $_request; |
|
65 | - |
|
66 | - /** |
|
67 | - * @type EE_Response $response |
|
68 | - */ |
|
69 | - protected $_response; |
|
70 | - |
|
71 | - /** |
|
72 | - * @type LoaderInterface $loader |
|
73 | - */ |
|
74 | - protected $loader; |
|
75 | - |
|
76 | - /** |
|
77 | - * @type array $_dependency_map |
|
78 | - */ |
|
79 | - protected $_dependency_map = array(); |
|
80 | - |
|
81 | - /** |
|
82 | - * @type array $_class_loaders |
|
83 | - */ |
|
84 | - protected $_class_loaders = array(); |
|
85 | - |
|
86 | - /** |
|
87 | - * @type array $_aliases |
|
88 | - */ |
|
89 | - protected $_aliases = array(); |
|
90 | - |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * EE_Dependency_Map constructor. |
|
95 | - * |
|
96 | - * @param EE_Request $request |
|
97 | - * @param EE_Response $response |
|
98 | - */ |
|
99 | - protected function __construct(EE_Request $request, EE_Response $response) |
|
100 | - { |
|
101 | - $this->_request = $request; |
|
102 | - $this->_response = $response; |
|
103 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
104 | - do_action('EE_Dependency_Map____construct'); |
|
105 | - } |
|
106 | - |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @throws InvalidDataTypeException |
|
111 | - * @throws InvalidInterfaceException |
|
112 | - * @throws InvalidArgumentException |
|
113 | - */ |
|
114 | - public function initialize() |
|
115 | - { |
|
116 | - $this->_register_core_dependencies(); |
|
117 | - $this->_register_core_class_loaders(); |
|
118 | - $this->_register_core_aliases(); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * @singleton method used to instantiate class object |
|
125 | - * @access public |
|
126 | - * @param EE_Request $request |
|
127 | - * @param EE_Response $response |
|
128 | - * @return EE_Dependency_Map |
|
129 | - */ |
|
130 | - public static function instance(EE_Request $request = null, EE_Response $response = null) |
|
131 | - { |
|
132 | - // check if class object is instantiated, and instantiated properly |
|
133 | - if (! self::$_instance instanceof EE_Dependency_Map) { |
|
134 | - self::$_instance = new EE_Dependency_Map($request, $response); |
|
135 | - } |
|
136 | - return self::$_instance; |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * @param LoaderInterface $loader |
|
143 | - */ |
|
144 | - public function setLoader(LoaderInterface $loader) |
|
145 | - { |
|
146 | - $this->loader = $loader; |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * @param string $class |
|
153 | - * @param array $dependencies |
|
154 | - * @param int $overwrite |
|
155 | - * @return bool |
|
156 | - */ |
|
157 | - public static function register_dependencies( |
|
158 | - $class, |
|
159 | - array $dependencies, |
|
160 | - $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
161 | - ) { |
|
162 | - return self::$_instance->registerDependencies($class, $dependencies, $overwrite); |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * Assigns an array of class names and corresponding load sources (new or cached) |
|
169 | - * to the class specified by the first parameter. |
|
170 | - * IMPORTANT !!! |
|
171 | - * The order of elements in the incoming $dependencies array MUST match |
|
172 | - * the order of the constructor parameters for the class in question. |
|
173 | - * This is especially important when overriding any existing dependencies that are registered. |
|
174 | - * the third parameter controls whether any duplicate dependencies are overwritten or not. |
|
175 | - * |
|
176 | - * @param string $class |
|
177 | - * @param array $dependencies |
|
178 | - * @param int $overwrite |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - public function registerDependencies( |
|
182 | - $class, |
|
183 | - array $dependencies, |
|
184 | - $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
185 | - ) { |
|
186 | - $class = trim($class, '\\'); |
|
187 | - $registered = false; |
|
188 | - if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
189 | - self::$_instance->_dependency_map[ $class ] = array(); |
|
190 | - } |
|
191 | - // we need to make sure that any aliases used when registering a dependency |
|
192 | - // get resolved to the correct class name |
|
193 | - foreach ((array)$dependencies as $dependency => $load_source) { |
|
194 | - $alias = self::$_instance->get_alias($dependency); |
|
195 | - if ( |
|
196 | - $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
|
197 | - || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
198 | - ) { |
|
199 | - unset($dependencies[$dependency]); |
|
200 | - $dependencies[$alias] = $load_source; |
|
201 | - $registered = true; |
|
202 | - } |
|
203 | - } |
|
204 | - // now add our two lists of dependencies together. |
|
205 | - // using Union (+=) favours the arrays in precedence from left to right, |
|
206 | - // so $dependencies is NOT overwritten because it is listed first |
|
207 | - // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
|
208 | - // Union is way faster than array_merge() but should be used with caution... |
|
209 | - // especially with numerically indexed arrays |
|
210 | - $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
211 | - // now we need to ensure that the resulting dependencies |
|
212 | - // array only has the entries that are required for the class |
|
213 | - // so first count how many dependencies were originally registered for the class |
|
214 | - $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
215 | - // if that count is non-zero (meaning dependencies were already registered) |
|
216 | - self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
217 | - // then truncate the final array to match that count |
|
218 | - ? array_slice($dependencies, 0, $dependency_count) |
|
219 | - // otherwise just take the incoming array because nothing previously existed |
|
220 | - : $dependencies; |
|
221 | - return $registered; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $class_name |
|
228 | - * @param string $loader |
|
229 | - * @return bool |
|
230 | - * @throws DomainException |
|
231 | - */ |
|
232 | - public static function register_class_loader($class_name, $loader = 'load_core') |
|
233 | - { |
|
234 | - if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
235 | - throw new DomainException( |
|
236 | - esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
|
237 | - ); |
|
238 | - } |
|
239 | - // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
240 | - if ( |
|
241 | - ! is_callable($loader) |
|
242 | - && ( |
|
243 | - strpos($loader, 'load_') !== 0 |
|
244 | - || ! method_exists('EE_Registry', $loader) |
|
245 | - ) |
|
246 | - ) { |
|
247 | - throw new DomainException( |
|
248 | - sprintf( |
|
249 | - esc_html__( |
|
250 | - '"%1$s" is not a valid loader method on EE_Registry.', |
|
251 | - 'event_espresso' |
|
252 | - ), |
|
253 | - $loader |
|
254 | - ) |
|
255 | - ); |
|
256 | - } |
|
257 | - $class_name = self::$_instance->get_alias($class_name); |
|
258 | - if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
259 | - self::$_instance->_class_loaders[$class_name] = $loader; |
|
260 | - return true; |
|
261 | - } |
|
262 | - return false; |
|
263 | - } |
|
264 | - |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * @return array |
|
269 | - */ |
|
270 | - public function dependency_map() |
|
271 | - { |
|
272 | - return $this->_dependency_map; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * returns TRUE if dependency map contains a listing for the provided class name |
|
279 | - * |
|
280 | - * @param string $class_name |
|
281 | - * @return boolean |
|
282 | - */ |
|
283 | - public function has($class_name = '') |
|
284 | - { |
|
285 | - // all legacy models have the same dependencies |
|
286 | - if (strpos($class_name, 'EEM_') === 0) { |
|
287 | - $class_name = 'LEGACY_MODELS'; |
|
288 | - } |
|
289 | - return isset($this->_dependency_map[$class_name]) ? true : false; |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - |
|
294 | - /** |
|
295 | - * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
296 | - * |
|
297 | - * @param string $class_name |
|
298 | - * @param string $dependency |
|
299 | - * @return bool |
|
300 | - */ |
|
301 | - public function has_dependency_for_class($class_name = '', $dependency = '') |
|
302 | - { |
|
303 | - // all legacy models have the same dependencies |
|
304 | - if (strpos($class_name, 'EEM_') === 0) { |
|
305 | - $class_name = 'LEGACY_MODELS'; |
|
306 | - } |
|
307 | - $dependency = $this->get_alias($dependency); |
|
308 | - return isset($this->_dependency_map[$class_name], $this->_dependency_map[$class_name][$dependency]) |
|
309 | - ? true |
|
310 | - : false; |
|
311 | - } |
|
312 | - |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
317 | - * |
|
318 | - * @param string $class_name |
|
319 | - * @param string $dependency |
|
320 | - * @return int |
|
321 | - */ |
|
322 | - public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
323 | - { |
|
324 | - // all legacy models have the same dependencies |
|
325 | - if (strpos($class_name, 'EEM_') === 0) { |
|
326 | - $class_name = 'LEGACY_MODELS'; |
|
327 | - } |
|
328 | - $dependency = $this->get_alias($dependency); |
|
329 | - return $this->has_dependency_for_class($class_name, $dependency) |
|
330 | - ? $this->_dependency_map[$class_name][$dependency] |
|
331 | - : EE_Dependency_Map::not_registered; |
|
332 | - } |
|
333 | - |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * @param string $class_name |
|
338 | - * @return string | Closure |
|
339 | - */ |
|
340 | - public function class_loader($class_name) |
|
341 | - { |
|
342 | - // all legacy models use load_model() |
|
343 | - if(strpos($class_name, 'EEM_') === 0){ |
|
344 | - return 'load_model'; |
|
345 | - } |
|
346 | - $class_name = $this->get_alias($class_name); |
|
347 | - return isset($this->_class_loaders[$class_name]) ? $this->_class_loaders[$class_name] : ''; |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * @return array |
|
354 | - */ |
|
355 | - public function class_loaders() |
|
356 | - { |
|
357 | - return $this->_class_loaders; |
|
358 | - } |
|
359 | - |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * adds an alias for a classname |
|
364 | - * |
|
365 | - * @param string $class_name the class name that should be used (concrete class to replace interface) |
|
366 | - * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
367 | - * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
368 | - */ |
|
369 | - public function add_alias($class_name, $alias, $for_class = '') |
|
370 | - { |
|
371 | - if ($for_class !== '') { |
|
372 | - if (! isset($this->_aliases[$for_class])) { |
|
373 | - $this->_aliases[$for_class] = array(); |
|
374 | - } |
|
375 | - $this->_aliases[$for_class][$class_name] = $alias; |
|
376 | - } |
|
377 | - $this->_aliases[$class_name] = $alias; |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - |
|
382 | - /** |
|
383 | - * returns TRUE if the provided class name has an alias |
|
384 | - * |
|
385 | - * @param string $class_name |
|
386 | - * @param string $for_class |
|
387 | - * @return bool |
|
388 | - */ |
|
389 | - public function has_alias($class_name = '', $for_class = '') |
|
390 | - { |
|
391 | - return isset($this->_aliases[$for_class], $this->_aliases[$for_class][$class_name]) |
|
392 | - || ( |
|
393 | - isset($this->_aliases[$class_name]) |
|
394 | - && ! is_array($this->_aliases[$class_name]) |
|
395 | - ); |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - |
|
400 | - /** |
|
401 | - * returns alias for class name if one exists, otherwise returns the original classname |
|
402 | - * functions recursively, so that multiple aliases can be used to drill down to a classname |
|
403 | - * for example: |
|
404 | - * if the following two entries were added to the _aliases array: |
|
405 | - * array( |
|
406 | - * 'interface_alias' => 'some\namespace\interface' |
|
407 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
408 | - * ) |
|
409 | - * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
410 | - * to load an instance of 'some\namespace\classname' |
|
411 | - * |
|
412 | - * @param string $class_name |
|
413 | - * @param string $for_class |
|
414 | - * @return string |
|
415 | - */ |
|
416 | - public function get_alias($class_name = '', $for_class = '') |
|
417 | - { |
|
418 | - if (! $this->has_alias($class_name, $for_class)) { |
|
419 | - return $class_name; |
|
420 | - } |
|
421 | - if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
422 | - return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
|
423 | - } |
|
424 | - return $this->get_alias($this->_aliases[$class_name]); |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - |
|
429 | - /** |
|
430 | - * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
431 | - * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
432 | - * This is done by using the following class constants: |
|
433 | - * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
434 | - * EE_Dependency_Map::load_new_object - generates a new object every time |
|
435 | - */ |
|
436 | - protected function _register_core_dependencies() |
|
437 | - { |
|
438 | - $this->_dependency_map = array( |
|
439 | - 'EE_Request_Handler' => array( |
|
440 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
441 | - ), |
|
442 | - 'EE_System' => array( |
|
443 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
444 | - ), |
|
445 | - 'EE_Session' => array( |
|
446 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
447 | - 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
448 | - ), |
|
449 | - 'EE_Cart' => array( |
|
450 | - 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
451 | - ), |
|
452 | - 'EE_Front_Controller' => array( |
|
453 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
454 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
455 | - 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
456 | - ), |
|
457 | - 'EE_Messenger_Collection_Loader' => array( |
|
458 | - 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
459 | - ), |
|
460 | - 'EE_Message_Type_Collection_Loader' => array( |
|
461 | - 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
462 | - ), |
|
463 | - 'EE_Message_Resource_Manager' => array( |
|
464 | - 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
465 | - 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
466 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
467 | - ), |
|
468 | - 'EE_Message_Factory' => array( |
|
469 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
470 | - ), |
|
471 | - 'EE_messages' => array( |
|
472 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
473 | - ), |
|
474 | - 'EE_Messages_Generator' => array( |
|
475 | - 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
476 | - 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
477 | - 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
478 | - 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
479 | - ), |
|
480 | - 'EE_Messages_Processor' => array( |
|
481 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
482 | - ), |
|
483 | - 'EE_Messages_Queue' => array( |
|
484 | - 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
485 | - ), |
|
486 | - 'EE_Messages_Template_Defaults' => array( |
|
487 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
488 | - 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
489 | - ), |
|
490 | - 'EE_Message_To_Generate_From_Request' => array( |
|
491 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
492 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
493 | - ), |
|
494 | - 'EventEspresso\core\services\commands\CommandBus' => array( |
|
495 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
496 | - ), |
|
497 | - 'EventEspresso\services\commands\CommandHandler' => array( |
|
498 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
499 | - 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
500 | - ), |
|
501 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
502 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
503 | - ), |
|
504 | - 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
505 | - 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
506 | - 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
507 | - ), |
|
508 | - 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
509 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
510 | - ), |
|
511 | - 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
512 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
513 | - ), |
|
514 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
515 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
516 | - ), |
|
517 | - 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
518 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
519 | - ), |
|
520 | - 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
521 | - 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
522 | - ), |
|
523 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
524 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
525 | - ), |
|
526 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
527 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
528 | - ), |
|
529 | - 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
530 | - 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
531 | - ), |
|
532 | - 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
533 | - 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
534 | - ), |
|
535 | - 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
536 | - 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
537 | - ), |
|
538 | - 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
539 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
540 | - ), |
|
541 | - 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
542 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
543 | - ), |
|
544 | - 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler' => array( |
|
545 | - 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
546 | - ), |
|
547 | - 'EventEspresso\core\services\database\TableManager' => array( |
|
548 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
549 | - ), |
|
550 | - 'EE_Data_Migration_Class_Base' => array( |
|
551 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
552 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
553 | - ), |
|
554 | - 'EE_DMS_Core_4_1_0' => array( |
|
555 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
556 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
557 | - ), |
|
558 | - 'EE_DMS_Core_4_2_0' => array( |
|
559 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
560 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
561 | - ), |
|
562 | - 'EE_DMS_Core_4_3_0' => array( |
|
563 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
564 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
565 | - ), |
|
566 | - 'EE_DMS_Core_4_4_0' => array( |
|
567 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
568 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
569 | - ), |
|
570 | - 'EE_DMS_Core_4_5_0' => array( |
|
571 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
572 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
573 | - ), |
|
574 | - 'EE_DMS_Core_4_6_0' => array( |
|
575 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
576 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
577 | - ), |
|
578 | - 'EE_DMS_Core_4_7_0' => array( |
|
579 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
580 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
581 | - ), |
|
582 | - 'EE_DMS_Core_4_8_0' => array( |
|
583 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
584 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
585 | - ), |
|
586 | - 'EE_DMS_Core_4_9_0' => array( |
|
587 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
588 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
589 | - ), |
|
590 | - 'EventEspresso\core\services\assets\Registry' => array( |
|
591 | - 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
592 | - 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
593 | - ), |
|
594 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
595 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
596 | - ), |
|
597 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
598 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
599 | - ), |
|
600 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
601 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
602 | - ), |
|
603 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
604 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
605 | - ), |
|
606 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
607 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
608 | - ), |
|
609 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
610 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
611 | - ), |
|
612 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
613 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
614 | - ), |
|
615 | - 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
616 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
617 | - ), |
|
618 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
619 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
620 | - ), |
|
621 | - 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => array( |
|
622 | - 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
623 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
624 | - ), |
|
625 | - 'EventEspresso\core\domain\values\EmailAddress' => array( |
|
626 | - null, |
|
627 | - 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache, |
|
628 | - ), |
|
629 | - 'EventEspresso\core\services\orm\ModelFieldFactory' => array( |
|
630 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
631 | - ), |
|
632 | - 'LEGACY_MODELS' => array( |
|
633 | - null, |
|
634 | - 'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache, |
|
635 | - ), |
|
636 | - ); |
|
637 | - } |
|
638 | - |
|
639 | - |
|
640 | - |
|
641 | - /** |
|
642 | - * Registers how core classes are loaded. |
|
643 | - * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
644 | - * 'EE_Request_Handler' => 'load_core' |
|
645 | - * 'EE_Messages_Queue' => 'load_lib' |
|
646 | - * 'EEH_Debug_Tools' => 'load_helper' |
|
647 | - * or, if greater control is required, by providing a custom closure. For example: |
|
648 | - * 'Some_Class' => function () { |
|
649 | - * return new Some_Class(); |
|
650 | - * }, |
|
651 | - * This is required for instantiating dependencies |
|
652 | - * where an interface has been type hinted in a class constructor. For example: |
|
653 | - * 'Required_Interface' => function () { |
|
654 | - * return new A_Class_That_Implements_Required_Interface(); |
|
655 | - * }, |
|
656 | - * |
|
657 | - * @throws InvalidInterfaceException |
|
658 | - * @throws InvalidDataTypeException |
|
659 | - * @throws InvalidArgumentException |
|
660 | - */ |
|
661 | - protected function _register_core_class_loaders() |
|
662 | - { |
|
663 | - //for PHP5.3 compat, we need to register any properties called here in a variable because `$this` cannot |
|
664 | - //be used in a closure. |
|
665 | - $request = &$this->_request; |
|
666 | - $response = &$this->_response; |
|
667 | - // $loader = &$this->loader; |
|
668 | - $this->_class_loaders = array( |
|
669 | - //load_core |
|
670 | - 'EE_Capabilities' => 'load_core', |
|
671 | - 'EE_Encryption' => 'load_core', |
|
672 | - 'EE_Front_Controller' => 'load_core', |
|
673 | - 'EE_Module_Request_Router' => 'load_core', |
|
674 | - 'EE_Registry' => 'load_core', |
|
675 | - 'EE_Request' => function () use (&$request) { |
|
676 | - return $request; |
|
677 | - }, |
|
678 | - 'EE_Response' => function () use (&$response) { |
|
679 | - return $response; |
|
680 | - }, |
|
681 | - 'EE_Request_Handler' => 'load_core', |
|
682 | - 'EE_Session' => 'load_core', |
|
683 | - 'EE_Cron_Tasks' => 'load_core', |
|
684 | - //load_lib |
|
685 | - 'EE_Message_Resource_Manager' => 'load_lib', |
|
686 | - 'EE_Message_Type_Collection' => 'load_lib', |
|
687 | - 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
688 | - 'EE_Messenger_Collection' => 'load_lib', |
|
689 | - 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
690 | - 'EE_Messages_Processor' => 'load_lib', |
|
691 | - 'EE_Message_Repository' => 'load_lib', |
|
692 | - 'EE_Messages_Queue' => 'load_lib', |
|
693 | - 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
694 | - 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
695 | - 'EE_Messages_Generator' => function () { |
|
696 | - return EE_Registry::instance()->load_lib( |
|
697 | - 'Messages_Generator', |
|
698 | - array(), |
|
699 | - false, |
|
700 | - false |
|
701 | - ); |
|
702 | - }, |
|
703 | - 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
704 | - return EE_Registry::instance()->load_lib( |
|
705 | - 'Messages_Template_Defaults', |
|
706 | - $arguments, |
|
707 | - false, |
|
708 | - false |
|
709 | - ); |
|
710 | - }, |
|
711 | - //load_model |
|
712 | - // 'EEM_Attendee' => 'load_model', |
|
713 | - // 'EEM_Message_Template_Group' => 'load_model', |
|
714 | - // 'EEM_Message_Template' => 'load_model', |
|
715 | - //load_helper |
|
716 | - 'EEH_Parse_Shortcodes' => function () { |
|
717 | - if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
718 | - return new EEH_Parse_Shortcodes(); |
|
719 | - } |
|
720 | - return null; |
|
721 | - }, |
|
722 | - 'EE_Template_Config' => function () { |
|
723 | - return EE_Config::instance()->template_settings; |
|
724 | - }, |
|
725 | - 'EE_Currency_Config' => function () { |
|
726 | - return EE_Config::instance()->currency; |
|
727 | - }, |
|
728 | - 'EE_Registration_Config' => function () { |
|
729 | - return EE_Config::instance()->registration; |
|
730 | - }, |
|
731 | - 'EventEspresso\core\services\loaders\Loader' => function () { |
|
732 | - return LoaderFactory::getLoader(); |
|
733 | - }, |
|
734 | - ); |
|
735 | - } |
|
736 | - |
|
737 | - |
|
738 | - |
|
739 | - /** |
|
740 | - * can be used for supplying alternate names for classes, |
|
741 | - * or for connecting interface names to instantiable classes |
|
742 | - */ |
|
743 | - protected function _register_core_aliases() |
|
744 | - { |
|
745 | - $this->_aliases = array( |
|
746 | - 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
747 | - 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
748 | - 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
749 | - 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
750 | - 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
751 | - 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
752 | - 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
753 | - 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
754 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
755 | - 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
756 | - 'CreateRegCodeCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegCodeCommand', |
|
757 | - 'CreateRegUrlLinkCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegUrlLinkCommand', |
|
758 | - 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
759 | - 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
760 | - 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
761 | - 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
762 | - 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
763 | - 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
764 | - 'CreateTransactionCommandHandler' => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler', |
|
765 | - 'CreateAttendeeCommandHandler' => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler', |
|
766 | - 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
767 | - 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
768 | - 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
769 | - 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
770 | - 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
771 | - 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
772 | - 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
773 | - 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
774 | - 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
775 | - 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
776 | - 'EventEspresso\core\domain\services\session\SessionIdentifierInterface' => 'EE_Session', |
|
777 | - 'EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface', |
|
778 | - 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidationService', |
|
779 | - 'NoticeConverterInterface' => 'EventEspresso\core\services\notices\NoticeConverterInterface', |
|
780 | - 'EventEspresso\core\services\notices\NoticeConverterInterface' => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors', |
|
781 | - 'NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainerInterface', |
|
782 | - 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
|
783 | - ); |
|
784 | - } |
|
785 | - |
|
786 | - |
|
787 | - |
|
788 | - /** |
|
789 | - * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
790 | - * request Primarily used by unit tests. |
|
791 | - * |
|
792 | - * @throws InvalidDataTypeException |
|
793 | - * @throws InvalidInterfaceException |
|
794 | - * @throws InvalidArgumentException |
|
795 | - */ |
|
796 | - public function reset() |
|
797 | - { |
|
798 | - $this->_register_core_class_loaders(); |
|
799 | - $this->_register_core_dependencies(); |
|
800 | - } |
|
25 | + /** |
|
26 | + * This means that the requested class dependency is not present in the dependency map |
|
27 | + */ |
|
28 | + const not_registered = 0; |
|
29 | + |
|
30 | + /** |
|
31 | + * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
32 | + */ |
|
33 | + const load_new_object = 1; |
|
34 | + |
|
35 | + /** |
|
36 | + * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
37 | + * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
38 | + */ |
|
39 | + const load_from_cache = 2; |
|
40 | + |
|
41 | + /** |
|
42 | + * When registering a dependency, |
|
43 | + * this indicates to keep any existing dependencies that already exist, |
|
44 | + * and simply discard any new dependencies declared in the incoming data |
|
45 | + */ |
|
46 | + const KEEP_EXISTING_DEPENDENCIES = 0; |
|
47 | + |
|
48 | + /** |
|
49 | + * When registering a dependency, |
|
50 | + * this indicates to overwrite any existing dependencies that already exist using the incoming data |
|
51 | + */ |
|
52 | + const OVERWRITE_DEPENDENCIES = 1; |
|
53 | + |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * @type EE_Dependency_Map $_instance |
|
58 | + */ |
|
59 | + protected static $_instance; |
|
60 | + |
|
61 | + /** |
|
62 | + * @type EE_Request $request |
|
63 | + */ |
|
64 | + protected $_request; |
|
65 | + |
|
66 | + /** |
|
67 | + * @type EE_Response $response |
|
68 | + */ |
|
69 | + protected $_response; |
|
70 | + |
|
71 | + /** |
|
72 | + * @type LoaderInterface $loader |
|
73 | + */ |
|
74 | + protected $loader; |
|
75 | + |
|
76 | + /** |
|
77 | + * @type array $_dependency_map |
|
78 | + */ |
|
79 | + protected $_dependency_map = array(); |
|
80 | + |
|
81 | + /** |
|
82 | + * @type array $_class_loaders |
|
83 | + */ |
|
84 | + protected $_class_loaders = array(); |
|
85 | + |
|
86 | + /** |
|
87 | + * @type array $_aliases |
|
88 | + */ |
|
89 | + protected $_aliases = array(); |
|
90 | + |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * EE_Dependency_Map constructor. |
|
95 | + * |
|
96 | + * @param EE_Request $request |
|
97 | + * @param EE_Response $response |
|
98 | + */ |
|
99 | + protected function __construct(EE_Request $request, EE_Response $response) |
|
100 | + { |
|
101 | + $this->_request = $request; |
|
102 | + $this->_response = $response; |
|
103 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
104 | + do_action('EE_Dependency_Map____construct'); |
|
105 | + } |
|
106 | + |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @throws InvalidDataTypeException |
|
111 | + * @throws InvalidInterfaceException |
|
112 | + * @throws InvalidArgumentException |
|
113 | + */ |
|
114 | + public function initialize() |
|
115 | + { |
|
116 | + $this->_register_core_dependencies(); |
|
117 | + $this->_register_core_class_loaders(); |
|
118 | + $this->_register_core_aliases(); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * @singleton method used to instantiate class object |
|
125 | + * @access public |
|
126 | + * @param EE_Request $request |
|
127 | + * @param EE_Response $response |
|
128 | + * @return EE_Dependency_Map |
|
129 | + */ |
|
130 | + public static function instance(EE_Request $request = null, EE_Response $response = null) |
|
131 | + { |
|
132 | + // check if class object is instantiated, and instantiated properly |
|
133 | + if (! self::$_instance instanceof EE_Dependency_Map) { |
|
134 | + self::$_instance = new EE_Dependency_Map($request, $response); |
|
135 | + } |
|
136 | + return self::$_instance; |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * @param LoaderInterface $loader |
|
143 | + */ |
|
144 | + public function setLoader(LoaderInterface $loader) |
|
145 | + { |
|
146 | + $this->loader = $loader; |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * @param string $class |
|
153 | + * @param array $dependencies |
|
154 | + * @param int $overwrite |
|
155 | + * @return bool |
|
156 | + */ |
|
157 | + public static function register_dependencies( |
|
158 | + $class, |
|
159 | + array $dependencies, |
|
160 | + $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
161 | + ) { |
|
162 | + return self::$_instance->registerDependencies($class, $dependencies, $overwrite); |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * Assigns an array of class names and corresponding load sources (new or cached) |
|
169 | + * to the class specified by the first parameter. |
|
170 | + * IMPORTANT !!! |
|
171 | + * The order of elements in the incoming $dependencies array MUST match |
|
172 | + * the order of the constructor parameters for the class in question. |
|
173 | + * This is especially important when overriding any existing dependencies that are registered. |
|
174 | + * the third parameter controls whether any duplicate dependencies are overwritten or not. |
|
175 | + * |
|
176 | + * @param string $class |
|
177 | + * @param array $dependencies |
|
178 | + * @param int $overwrite |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + public function registerDependencies( |
|
182 | + $class, |
|
183 | + array $dependencies, |
|
184 | + $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES |
|
185 | + ) { |
|
186 | + $class = trim($class, '\\'); |
|
187 | + $registered = false; |
|
188 | + if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
189 | + self::$_instance->_dependency_map[ $class ] = array(); |
|
190 | + } |
|
191 | + // we need to make sure that any aliases used when registering a dependency |
|
192 | + // get resolved to the correct class name |
|
193 | + foreach ((array)$dependencies as $dependency => $load_source) { |
|
194 | + $alias = self::$_instance->get_alias($dependency); |
|
195 | + if ( |
|
196 | + $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
|
197 | + || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
198 | + ) { |
|
199 | + unset($dependencies[$dependency]); |
|
200 | + $dependencies[$alias] = $load_source; |
|
201 | + $registered = true; |
|
202 | + } |
|
203 | + } |
|
204 | + // now add our two lists of dependencies together. |
|
205 | + // using Union (+=) favours the arrays in precedence from left to right, |
|
206 | + // so $dependencies is NOT overwritten because it is listed first |
|
207 | + // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
|
208 | + // Union is way faster than array_merge() but should be used with caution... |
|
209 | + // especially with numerically indexed arrays |
|
210 | + $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
211 | + // now we need to ensure that the resulting dependencies |
|
212 | + // array only has the entries that are required for the class |
|
213 | + // so first count how many dependencies were originally registered for the class |
|
214 | + $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
215 | + // if that count is non-zero (meaning dependencies were already registered) |
|
216 | + self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
217 | + // then truncate the final array to match that count |
|
218 | + ? array_slice($dependencies, 0, $dependency_count) |
|
219 | + // otherwise just take the incoming array because nothing previously existed |
|
220 | + : $dependencies; |
|
221 | + return $registered; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $class_name |
|
228 | + * @param string $loader |
|
229 | + * @return bool |
|
230 | + * @throws DomainException |
|
231 | + */ |
|
232 | + public static function register_class_loader($class_name, $loader = 'load_core') |
|
233 | + { |
|
234 | + if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
235 | + throw new DomainException( |
|
236 | + esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
|
237 | + ); |
|
238 | + } |
|
239 | + // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
240 | + if ( |
|
241 | + ! is_callable($loader) |
|
242 | + && ( |
|
243 | + strpos($loader, 'load_') !== 0 |
|
244 | + || ! method_exists('EE_Registry', $loader) |
|
245 | + ) |
|
246 | + ) { |
|
247 | + throw new DomainException( |
|
248 | + sprintf( |
|
249 | + esc_html__( |
|
250 | + '"%1$s" is not a valid loader method on EE_Registry.', |
|
251 | + 'event_espresso' |
|
252 | + ), |
|
253 | + $loader |
|
254 | + ) |
|
255 | + ); |
|
256 | + } |
|
257 | + $class_name = self::$_instance->get_alias($class_name); |
|
258 | + if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
259 | + self::$_instance->_class_loaders[$class_name] = $loader; |
|
260 | + return true; |
|
261 | + } |
|
262 | + return false; |
|
263 | + } |
|
264 | + |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * @return array |
|
269 | + */ |
|
270 | + public function dependency_map() |
|
271 | + { |
|
272 | + return $this->_dependency_map; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * returns TRUE if dependency map contains a listing for the provided class name |
|
279 | + * |
|
280 | + * @param string $class_name |
|
281 | + * @return boolean |
|
282 | + */ |
|
283 | + public function has($class_name = '') |
|
284 | + { |
|
285 | + // all legacy models have the same dependencies |
|
286 | + if (strpos($class_name, 'EEM_') === 0) { |
|
287 | + $class_name = 'LEGACY_MODELS'; |
|
288 | + } |
|
289 | + return isset($this->_dependency_map[$class_name]) ? true : false; |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + |
|
294 | + /** |
|
295 | + * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
296 | + * |
|
297 | + * @param string $class_name |
|
298 | + * @param string $dependency |
|
299 | + * @return bool |
|
300 | + */ |
|
301 | + public function has_dependency_for_class($class_name = '', $dependency = '') |
|
302 | + { |
|
303 | + // all legacy models have the same dependencies |
|
304 | + if (strpos($class_name, 'EEM_') === 0) { |
|
305 | + $class_name = 'LEGACY_MODELS'; |
|
306 | + } |
|
307 | + $dependency = $this->get_alias($dependency); |
|
308 | + return isset($this->_dependency_map[$class_name], $this->_dependency_map[$class_name][$dependency]) |
|
309 | + ? true |
|
310 | + : false; |
|
311 | + } |
|
312 | + |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
317 | + * |
|
318 | + * @param string $class_name |
|
319 | + * @param string $dependency |
|
320 | + * @return int |
|
321 | + */ |
|
322 | + public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
323 | + { |
|
324 | + // all legacy models have the same dependencies |
|
325 | + if (strpos($class_name, 'EEM_') === 0) { |
|
326 | + $class_name = 'LEGACY_MODELS'; |
|
327 | + } |
|
328 | + $dependency = $this->get_alias($dependency); |
|
329 | + return $this->has_dependency_for_class($class_name, $dependency) |
|
330 | + ? $this->_dependency_map[$class_name][$dependency] |
|
331 | + : EE_Dependency_Map::not_registered; |
|
332 | + } |
|
333 | + |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * @param string $class_name |
|
338 | + * @return string | Closure |
|
339 | + */ |
|
340 | + public function class_loader($class_name) |
|
341 | + { |
|
342 | + // all legacy models use load_model() |
|
343 | + if(strpos($class_name, 'EEM_') === 0){ |
|
344 | + return 'load_model'; |
|
345 | + } |
|
346 | + $class_name = $this->get_alias($class_name); |
|
347 | + return isset($this->_class_loaders[$class_name]) ? $this->_class_loaders[$class_name] : ''; |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * @return array |
|
354 | + */ |
|
355 | + public function class_loaders() |
|
356 | + { |
|
357 | + return $this->_class_loaders; |
|
358 | + } |
|
359 | + |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * adds an alias for a classname |
|
364 | + * |
|
365 | + * @param string $class_name the class name that should be used (concrete class to replace interface) |
|
366 | + * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
367 | + * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
368 | + */ |
|
369 | + public function add_alias($class_name, $alias, $for_class = '') |
|
370 | + { |
|
371 | + if ($for_class !== '') { |
|
372 | + if (! isset($this->_aliases[$for_class])) { |
|
373 | + $this->_aliases[$for_class] = array(); |
|
374 | + } |
|
375 | + $this->_aliases[$for_class][$class_name] = $alias; |
|
376 | + } |
|
377 | + $this->_aliases[$class_name] = $alias; |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * returns TRUE if the provided class name has an alias |
|
384 | + * |
|
385 | + * @param string $class_name |
|
386 | + * @param string $for_class |
|
387 | + * @return bool |
|
388 | + */ |
|
389 | + public function has_alias($class_name = '', $for_class = '') |
|
390 | + { |
|
391 | + return isset($this->_aliases[$for_class], $this->_aliases[$for_class][$class_name]) |
|
392 | + || ( |
|
393 | + isset($this->_aliases[$class_name]) |
|
394 | + && ! is_array($this->_aliases[$class_name]) |
|
395 | + ); |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + |
|
400 | + /** |
|
401 | + * returns alias for class name if one exists, otherwise returns the original classname |
|
402 | + * functions recursively, so that multiple aliases can be used to drill down to a classname |
|
403 | + * for example: |
|
404 | + * if the following two entries were added to the _aliases array: |
|
405 | + * array( |
|
406 | + * 'interface_alias' => 'some\namespace\interface' |
|
407 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
408 | + * ) |
|
409 | + * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
410 | + * to load an instance of 'some\namespace\classname' |
|
411 | + * |
|
412 | + * @param string $class_name |
|
413 | + * @param string $for_class |
|
414 | + * @return string |
|
415 | + */ |
|
416 | + public function get_alias($class_name = '', $for_class = '') |
|
417 | + { |
|
418 | + if (! $this->has_alias($class_name, $for_class)) { |
|
419 | + return $class_name; |
|
420 | + } |
|
421 | + if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
422 | + return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
|
423 | + } |
|
424 | + return $this->get_alias($this->_aliases[$class_name]); |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + |
|
429 | + /** |
|
430 | + * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
431 | + * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
432 | + * This is done by using the following class constants: |
|
433 | + * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
434 | + * EE_Dependency_Map::load_new_object - generates a new object every time |
|
435 | + */ |
|
436 | + protected function _register_core_dependencies() |
|
437 | + { |
|
438 | + $this->_dependency_map = array( |
|
439 | + 'EE_Request_Handler' => array( |
|
440 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
441 | + ), |
|
442 | + 'EE_System' => array( |
|
443 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
444 | + ), |
|
445 | + 'EE_Session' => array( |
|
446 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
447 | + 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
448 | + ), |
|
449 | + 'EE_Cart' => array( |
|
450 | + 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
451 | + ), |
|
452 | + 'EE_Front_Controller' => array( |
|
453 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
454 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
455 | + 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
456 | + ), |
|
457 | + 'EE_Messenger_Collection_Loader' => array( |
|
458 | + 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
459 | + ), |
|
460 | + 'EE_Message_Type_Collection_Loader' => array( |
|
461 | + 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
462 | + ), |
|
463 | + 'EE_Message_Resource_Manager' => array( |
|
464 | + 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
465 | + 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
466 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
467 | + ), |
|
468 | + 'EE_Message_Factory' => array( |
|
469 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
470 | + ), |
|
471 | + 'EE_messages' => array( |
|
472 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
473 | + ), |
|
474 | + 'EE_Messages_Generator' => array( |
|
475 | + 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
476 | + 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
477 | + 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
478 | + 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
479 | + ), |
|
480 | + 'EE_Messages_Processor' => array( |
|
481 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
482 | + ), |
|
483 | + 'EE_Messages_Queue' => array( |
|
484 | + 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
485 | + ), |
|
486 | + 'EE_Messages_Template_Defaults' => array( |
|
487 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
488 | + 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
489 | + ), |
|
490 | + 'EE_Message_To_Generate_From_Request' => array( |
|
491 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
492 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
493 | + ), |
|
494 | + 'EventEspresso\core\services\commands\CommandBus' => array( |
|
495 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
496 | + ), |
|
497 | + 'EventEspresso\services\commands\CommandHandler' => array( |
|
498 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
499 | + 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
500 | + ), |
|
501 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
502 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
503 | + ), |
|
504 | + 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
505 | + 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
506 | + 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
507 | + ), |
|
508 | + 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
509 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
510 | + ), |
|
511 | + 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
512 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
513 | + ), |
|
514 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
515 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
516 | + ), |
|
517 | + 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
518 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
519 | + ), |
|
520 | + 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
521 | + 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
522 | + ), |
|
523 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
524 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
525 | + ), |
|
526 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
527 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
528 | + ), |
|
529 | + 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
530 | + 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
531 | + ), |
|
532 | + 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
533 | + 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
534 | + ), |
|
535 | + 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
536 | + 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
537 | + ), |
|
538 | + 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
539 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
540 | + ), |
|
541 | + 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
542 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
543 | + ), |
|
544 | + 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler' => array( |
|
545 | + 'EEM_Attendee' => EE_Dependency_Map::load_from_cache, |
|
546 | + ), |
|
547 | + 'EventEspresso\core\services\database\TableManager' => array( |
|
548 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
549 | + ), |
|
550 | + 'EE_Data_Migration_Class_Base' => array( |
|
551 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
552 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
553 | + ), |
|
554 | + 'EE_DMS_Core_4_1_0' => array( |
|
555 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
556 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
557 | + ), |
|
558 | + 'EE_DMS_Core_4_2_0' => array( |
|
559 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
560 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
561 | + ), |
|
562 | + 'EE_DMS_Core_4_3_0' => array( |
|
563 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
564 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
565 | + ), |
|
566 | + 'EE_DMS_Core_4_4_0' => array( |
|
567 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
568 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
569 | + ), |
|
570 | + 'EE_DMS_Core_4_5_0' => array( |
|
571 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
572 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
573 | + ), |
|
574 | + 'EE_DMS_Core_4_6_0' => array( |
|
575 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
576 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
577 | + ), |
|
578 | + 'EE_DMS_Core_4_7_0' => array( |
|
579 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
580 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
581 | + ), |
|
582 | + 'EE_DMS_Core_4_8_0' => array( |
|
583 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
584 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
585 | + ), |
|
586 | + 'EE_DMS_Core_4_9_0' => array( |
|
587 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
588 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
589 | + ), |
|
590 | + 'EventEspresso\core\services\assets\Registry' => array( |
|
591 | + 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
592 | + 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
593 | + ), |
|
594 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
595 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
596 | + ), |
|
597 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
598 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
599 | + ), |
|
600 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
601 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
602 | + ), |
|
603 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
604 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
605 | + ), |
|
606 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
607 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
608 | + ), |
|
609 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
610 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
611 | + ), |
|
612 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
613 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
614 | + ), |
|
615 | + 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
616 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
617 | + ), |
|
618 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
619 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
620 | + ), |
|
621 | + 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => array( |
|
622 | + 'EE_Registration_Config' => EE_Dependency_Map::load_from_cache, |
|
623 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
624 | + ), |
|
625 | + 'EventEspresso\core\domain\values\EmailAddress' => array( |
|
626 | + null, |
|
627 | + 'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache, |
|
628 | + ), |
|
629 | + 'EventEspresso\core\services\orm\ModelFieldFactory' => array( |
|
630 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
631 | + ), |
|
632 | + 'LEGACY_MODELS' => array( |
|
633 | + null, |
|
634 | + 'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache, |
|
635 | + ), |
|
636 | + ); |
|
637 | + } |
|
638 | + |
|
639 | + |
|
640 | + |
|
641 | + /** |
|
642 | + * Registers how core classes are loaded. |
|
643 | + * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
644 | + * 'EE_Request_Handler' => 'load_core' |
|
645 | + * 'EE_Messages_Queue' => 'load_lib' |
|
646 | + * 'EEH_Debug_Tools' => 'load_helper' |
|
647 | + * or, if greater control is required, by providing a custom closure. For example: |
|
648 | + * 'Some_Class' => function () { |
|
649 | + * return new Some_Class(); |
|
650 | + * }, |
|
651 | + * This is required for instantiating dependencies |
|
652 | + * where an interface has been type hinted in a class constructor. For example: |
|
653 | + * 'Required_Interface' => function () { |
|
654 | + * return new A_Class_That_Implements_Required_Interface(); |
|
655 | + * }, |
|
656 | + * |
|
657 | + * @throws InvalidInterfaceException |
|
658 | + * @throws InvalidDataTypeException |
|
659 | + * @throws InvalidArgumentException |
|
660 | + */ |
|
661 | + protected function _register_core_class_loaders() |
|
662 | + { |
|
663 | + //for PHP5.3 compat, we need to register any properties called here in a variable because `$this` cannot |
|
664 | + //be used in a closure. |
|
665 | + $request = &$this->_request; |
|
666 | + $response = &$this->_response; |
|
667 | + // $loader = &$this->loader; |
|
668 | + $this->_class_loaders = array( |
|
669 | + //load_core |
|
670 | + 'EE_Capabilities' => 'load_core', |
|
671 | + 'EE_Encryption' => 'load_core', |
|
672 | + 'EE_Front_Controller' => 'load_core', |
|
673 | + 'EE_Module_Request_Router' => 'load_core', |
|
674 | + 'EE_Registry' => 'load_core', |
|
675 | + 'EE_Request' => function () use (&$request) { |
|
676 | + return $request; |
|
677 | + }, |
|
678 | + 'EE_Response' => function () use (&$response) { |
|
679 | + return $response; |
|
680 | + }, |
|
681 | + 'EE_Request_Handler' => 'load_core', |
|
682 | + 'EE_Session' => 'load_core', |
|
683 | + 'EE_Cron_Tasks' => 'load_core', |
|
684 | + //load_lib |
|
685 | + 'EE_Message_Resource_Manager' => 'load_lib', |
|
686 | + 'EE_Message_Type_Collection' => 'load_lib', |
|
687 | + 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
688 | + 'EE_Messenger_Collection' => 'load_lib', |
|
689 | + 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
690 | + 'EE_Messages_Processor' => 'load_lib', |
|
691 | + 'EE_Message_Repository' => 'load_lib', |
|
692 | + 'EE_Messages_Queue' => 'load_lib', |
|
693 | + 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
694 | + 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
695 | + 'EE_Messages_Generator' => function () { |
|
696 | + return EE_Registry::instance()->load_lib( |
|
697 | + 'Messages_Generator', |
|
698 | + array(), |
|
699 | + false, |
|
700 | + false |
|
701 | + ); |
|
702 | + }, |
|
703 | + 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
704 | + return EE_Registry::instance()->load_lib( |
|
705 | + 'Messages_Template_Defaults', |
|
706 | + $arguments, |
|
707 | + false, |
|
708 | + false |
|
709 | + ); |
|
710 | + }, |
|
711 | + //load_model |
|
712 | + // 'EEM_Attendee' => 'load_model', |
|
713 | + // 'EEM_Message_Template_Group' => 'load_model', |
|
714 | + // 'EEM_Message_Template' => 'load_model', |
|
715 | + //load_helper |
|
716 | + 'EEH_Parse_Shortcodes' => function () { |
|
717 | + if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
718 | + return new EEH_Parse_Shortcodes(); |
|
719 | + } |
|
720 | + return null; |
|
721 | + }, |
|
722 | + 'EE_Template_Config' => function () { |
|
723 | + return EE_Config::instance()->template_settings; |
|
724 | + }, |
|
725 | + 'EE_Currency_Config' => function () { |
|
726 | + return EE_Config::instance()->currency; |
|
727 | + }, |
|
728 | + 'EE_Registration_Config' => function () { |
|
729 | + return EE_Config::instance()->registration; |
|
730 | + }, |
|
731 | + 'EventEspresso\core\services\loaders\Loader' => function () { |
|
732 | + return LoaderFactory::getLoader(); |
|
733 | + }, |
|
734 | + ); |
|
735 | + } |
|
736 | + |
|
737 | + |
|
738 | + |
|
739 | + /** |
|
740 | + * can be used for supplying alternate names for classes, |
|
741 | + * or for connecting interface names to instantiable classes |
|
742 | + */ |
|
743 | + protected function _register_core_aliases() |
|
744 | + { |
|
745 | + $this->_aliases = array( |
|
746 | + 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
747 | + 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
748 | + 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
749 | + 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
750 | + 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
751 | + 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
752 | + 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
753 | + 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
754 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
755 | + 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
756 | + 'CreateRegCodeCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegCodeCommand', |
|
757 | + 'CreateRegUrlLinkCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegUrlLinkCommand', |
|
758 | + 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
759 | + 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
760 | + 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
761 | + 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
762 | + 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
763 | + 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
764 | + 'CreateTransactionCommandHandler' => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler', |
|
765 | + 'CreateAttendeeCommandHandler' => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler', |
|
766 | + 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
767 | + 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
768 | + 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
769 | + 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
770 | + 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
771 | + 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
772 | + 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
773 | + 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
774 | + 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
775 | + 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
776 | + 'EventEspresso\core\domain\services\session\SessionIdentifierInterface' => 'EE_Session', |
|
777 | + 'EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface', |
|
778 | + 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface' => 'EventEspresso\core\domain\services\validation\email\EmailValidationService', |
|
779 | + 'NoticeConverterInterface' => 'EventEspresso\core\services\notices\NoticeConverterInterface', |
|
780 | + 'EventEspresso\core\services\notices\NoticeConverterInterface' => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors', |
|
781 | + 'NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainerInterface', |
|
782 | + 'EventEspresso\core\services\notices\NoticesContainerInterface' => 'EventEspresso\core\services\notices\NoticesContainer', |
|
783 | + ); |
|
784 | + } |
|
785 | + |
|
786 | + |
|
787 | + |
|
788 | + /** |
|
789 | + * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
790 | + * request Primarily used by unit tests. |
|
791 | + * |
|
792 | + * @throws InvalidDataTypeException |
|
793 | + * @throws InvalidInterfaceException |
|
794 | + * @throws InvalidArgumentException |
|
795 | + */ |
|
796 | + public function reset() |
|
797 | + { |
|
798 | + $this->_register_core_class_loaders(); |
|
799 | + $this->_register_core_dependencies(); |
|
800 | + } |
|
801 | 801 | |
802 | 802 | |
803 | 803 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use EventEspresso\core\services\loaders\LoaderFactory; |
5 | 5 | use EventEspresso\core\services\loaders\LoaderInterface; |
6 | 6 | |
7 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
7 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
8 | 8 | exit('No direct script access allowed'); |
9 | 9 | } |
10 | 10 | |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | public static function instance(EE_Request $request = null, EE_Response $response = null) |
131 | 131 | { |
132 | 132 | // check if class object is instantiated, and instantiated properly |
133 | - if (! self::$_instance instanceof EE_Dependency_Map) { |
|
133 | + if ( ! self::$_instance instanceof EE_Dependency_Map) { |
|
134 | 134 | self::$_instance = new EE_Dependency_Map($request, $response); |
135 | 135 | } |
136 | 136 | return self::$_instance; |
@@ -185,16 +185,16 @@ discard block |
||
185 | 185 | ) { |
186 | 186 | $class = trim($class, '\\'); |
187 | 187 | $registered = false; |
188 | - if (empty(self::$_instance->_dependency_map[ $class ])) { |
|
189 | - self::$_instance->_dependency_map[ $class ] = array(); |
|
188 | + if (empty(self::$_instance->_dependency_map[$class])) { |
|
189 | + self::$_instance->_dependency_map[$class] = array(); |
|
190 | 190 | } |
191 | 191 | // we need to make sure that any aliases used when registering a dependency |
192 | 192 | // get resolved to the correct class name |
193 | - foreach ((array)$dependencies as $dependency => $load_source) { |
|
193 | + foreach ((array) $dependencies as $dependency => $load_source) { |
|
194 | 194 | $alias = self::$_instance->get_alias($dependency); |
195 | 195 | if ( |
196 | 196 | $overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES |
197 | - || ! isset(self::$_instance->_dependency_map[ $class ][ $alias ]) |
|
197 | + || ! isset(self::$_instance->_dependency_map[$class][$alias]) |
|
198 | 198 | ) { |
199 | 199 | unset($dependencies[$dependency]); |
200 | 200 | $dependencies[$alias] = $load_source; |
@@ -207,13 +207,13 @@ discard block |
||
207 | 207 | // ie: with A = B + C, entries in B take precedence over duplicate entries in C |
208 | 208 | // Union is way faster than array_merge() but should be used with caution... |
209 | 209 | // especially with numerically indexed arrays |
210 | - $dependencies += self::$_instance->_dependency_map[ $class ]; |
|
210 | + $dependencies += self::$_instance->_dependency_map[$class]; |
|
211 | 211 | // now we need to ensure that the resulting dependencies |
212 | 212 | // array only has the entries that are required for the class |
213 | 213 | // so first count how many dependencies were originally registered for the class |
214 | - $dependency_count = count(self::$_instance->_dependency_map[ $class ]); |
|
214 | + $dependency_count = count(self::$_instance->_dependency_map[$class]); |
|
215 | 215 | // if that count is non-zero (meaning dependencies were already registered) |
216 | - self::$_instance->_dependency_map[ $class ] = $dependency_count |
|
216 | + self::$_instance->_dependency_map[$class] = $dependency_count |
|
217 | 217 | // then truncate the final array to match that count |
218 | 218 | ? array_slice($dependencies, 0, $dependency_count) |
219 | 219 | // otherwise just take the incoming array because nothing previously existed |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | */ |
232 | 232 | public static function register_class_loader($class_name, $loader = 'load_core') |
233 | 233 | { |
234 | - if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
234 | + if ( ! $loader instanceof Closure && strpos($class_name, '\\') !== false) { |
|
235 | 235 | throw new DomainException( |
236 | 236 | esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso') |
237 | 237 | ); |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | ); |
256 | 256 | } |
257 | 257 | $class_name = self::$_instance->get_alias($class_name); |
258 | - if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
258 | + if ( ! isset(self::$_instance->_class_loaders[$class_name])) { |
|
259 | 259 | self::$_instance->_class_loaders[$class_name] = $loader; |
260 | 260 | return true; |
261 | 261 | } |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | public function class_loader($class_name) |
341 | 341 | { |
342 | 342 | // all legacy models use load_model() |
343 | - if(strpos($class_name, 'EEM_') === 0){ |
|
343 | + if (strpos($class_name, 'EEM_') === 0) { |
|
344 | 344 | return 'load_model'; |
345 | 345 | } |
346 | 346 | $class_name = $this->get_alias($class_name); |
@@ -369,7 +369,7 @@ discard block |
||
369 | 369 | public function add_alias($class_name, $alias, $for_class = '') |
370 | 370 | { |
371 | 371 | if ($for_class !== '') { |
372 | - if (! isset($this->_aliases[$for_class])) { |
|
372 | + if ( ! isset($this->_aliases[$for_class])) { |
|
373 | 373 | $this->_aliases[$for_class] = array(); |
374 | 374 | } |
375 | 375 | $this->_aliases[$for_class][$class_name] = $alias; |
@@ -415,10 +415,10 @@ discard block |
||
415 | 415 | */ |
416 | 416 | public function get_alias($class_name = '', $for_class = '') |
417 | 417 | { |
418 | - if (! $this->has_alias($class_name, $for_class)) { |
|
418 | + if ( ! $this->has_alias($class_name, $for_class)) { |
|
419 | 419 | return $class_name; |
420 | 420 | } |
421 | - if ($for_class !== '' && isset($this->_aliases[ $for_class ][ $class_name ])) { |
|
421 | + if ($for_class !== '' && isset($this->_aliases[$for_class][$class_name])) { |
|
422 | 422 | return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
423 | 423 | } |
424 | 424 | return $this->get_alias($this->_aliases[$class_name]); |
@@ -672,10 +672,10 @@ discard block |
||
672 | 672 | 'EE_Front_Controller' => 'load_core', |
673 | 673 | 'EE_Module_Request_Router' => 'load_core', |
674 | 674 | 'EE_Registry' => 'load_core', |
675 | - 'EE_Request' => function () use (&$request) { |
|
675 | + 'EE_Request' => function() use (&$request) { |
|
676 | 676 | return $request; |
677 | 677 | }, |
678 | - 'EE_Response' => function () use (&$response) { |
|
678 | + 'EE_Response' => function() use (&$response) { |
|
679 | 679 | return $response; |
680 | 680 | }, |
681 | 681 | 'EE_Request_Handler' => 'load_core', |
@@ -692,7 +692,7 @@ discard block |
||
692 | 692 | 'EE_Messages_Queue' => 'load_lib', |
693 | 693 | 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
694 | 694 | 'EE_Message_Template_Group_Collection' => 'load_lib', |
695 | - 'EE_Messages_Generator' => function () { |
|
695 | + 'EE_Messages_Generator' => function() { |
|
696 | 696 | return EE_Registry::instance()->load_lib( |
697 | 697 | 'Messages_Generator', |
698 | 698 | array(), |
@@ -700,7 +700,7 @@ discard block |
||
700 | 700 | false |
701 | 701 | ); |
702 | 702 | }, |
703 | - 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
703 | + 'EE_Messages_Template_Defaults' => function($arguments = array()) { |
|
704 | 704 | return EE_Registry::instance()->load_lib( |
705 | 705 | 'Messages_Template_Defaults', |
706 | 706 | $arguments, |
@@ -713,22 +713,22 @@ discard block |
||
713 | 713 | // 'EEM_Message_Template_Group' => 'load_model', |
714 | 714 | // 'EEM_Message_Template' => 'load_model', |
715 | 715 | //load_helper |
716 | - 'EEH_Parse_Shortcodes' => function () { |
|
716 | + 'EEH_Parse_Shortcodes' => function() { |
|
717 | 717 | if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
718 | 718 | return new EEH_Parse_Shortcodes(); |
719 | 719 | } |
720 | 720 | return null; |
721 | 721 | }, |
722 | - 'EE_Template_Config' => function () { |
|
722 | + 'EE_Template_Config' => function() { |
|
723 | 723 | return EE_Config::instance()->template_settings; |
724 | 724 | }, |
725 | - 'EE_Currency_Config' => function () { |
|
725 | + 'EE_Currency_Config' => function() { |
|
726 | 726 | return EE_Config::instance()->currency; |
727 | 727 | }, |
728 | - 'EE_Registration_Config' => function () { |
|
728 | + 'EE_Registration_Config' => function() { |
|
729 | 729 | return EE_Config::instance()->registration; |
730 | 730 | }, |
731 | - 'EventEspresso\core\services\loaders\Loader' => function () { |
|
731 | + 'EventEspresso\core\services\loaders\Loader' => function() { |
|
732 | 732 | return LoaderFactory::getLoader(); |
733 | 733 | }, |
734 | 734 | ); |