@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | use EE_Datetime_Field; |
10 | 10 | |
11 | 11 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
12 | - exit('No direct script access allowed'); |
|
12 | + exit('No direct script access allowed'); |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | |
@@ -27,1245 +27,1245 @@ discard block |
||
27 | 27 | |
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * @var Calculated_Model_Fields |
|
32 | - */ |
|
33 | - protected $_fields_calculator; |
|
34 | - |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * Read constructor. |
|
39 | - */ |
|
40 | - public function __construct() |
|
41 | - { |
|
42 | - parent::__construct(); |
|
43 | - $this->_fields_calculator = new Calculated_Model_Fields(); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
50 | - * |
|
51 | - * @param \WP_REST_Request $request |
|
52 | - * @return \WP_REST_Response|\WP_Error |
|
53 | - */ |
|
54 | - public static function handle_request_get_all(\WP_REST_Request $request) |
|
55 | - { |
|
56 | - $controller = new Read(); |
|
57 | - try { |
|
58 | - $matches = $controller->parse_route( |
|
59 | - $request->get_route(), |
|
60 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
61 | - array('version', 'model') |
|
62 | - ); |
|
63 | - $controller->set_requested_version($matches['version']); |
|
64 | - $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
65 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
66 | - return $controller->send_response( |
|
67 | - new \WP_Error( |
|
68 | - 'endpoint_parsing_error', |
|
69 | - sprintf( |
|
70 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
71 | - 'event_espresso'), |
|
72 | - $model_name_singular |
|
73 | - ) |
|
74 | - ) |
|
75 | - ); |
|
76 | - } |
|
77 | - return $controller->send_response( |
|
78 | - $controller->get_entities_from_model( |
|
79 | - $controller->get_model_version_info()->load_model($model_name_singular), |
|
80 | - $request |
|
81 | - ) |
|
82 | - ); |
|
83 | - } catch (\Exception $e) { |
|
84 | - return $controller->send_response($e); |
|
85 | - } |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * Prepares and returns schema for any OPTIONS request. |
|
92 | - * |
|
93 | - * @param string $model_name Something like `Event` or `Registration` |
|
94 | - * @param string $version The API endpoint version being used. |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - public static function handle_schema_request($model_name, $version) |
|
98 | - { |
|
99 | - $controller = new Read(); |
|
100 | - try { |
|
101 | - $controller->set_requested_version($version); |
|
102 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
103 | - return array(); |
|
104 | - } |
|
105 | - //get the model for this version |
|
106 | - $model = $controller->get_model_version_info()->load_model($model_name); |
|
107 | - $model_schema = new JsonModelSchema($model); |
|
108 | - return $model_schema->getModelSchemaForRelations( |
|
109 | - $controller->get_model_version_info()->relation_settings($model), |
|
110 | - $controller->_customize_schema_for_rest_response( |
|
111 | - $model, |
|
112 | - $model_schema->getModelSchemaForFields( |
|
113 | - $controller->get_model_version_info()->fields_on_model_in_this_version($model), |
|
114 | - $model_schema->getInitialSchemaStructure() |
|
115 | - ) |
|
116 | - ) |
|
117 | - ); |
|
118 | - } catch (\Exception $e) { |
|
119 | - return array(); |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - |
|
125 | - /** |
|
126 | - * This loops through each field in the given schema for the model and does the following: |
|
127 | - * - add any extra fields that are REST API specific and related to existing fields. |
|
128 | - * - transform default values into the correct format for a REST API response. |
|
129 | - * |
|
130 | - * @param \EEM_Base $model |
|
131 | - * @param array $schema |
|
132 | - * @return array The final schema. |
|
133 | - */ |
|
134 | - protected function _customize_schema_for_rest_response(\EEM_Base $model, array $schema) |
|
135 | - { |
|
136 | - foreach ($this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => $field) { |
|
137 | - $schema = $this->_translate_defaults_for_rest_response( |
|
138 | - $field_name, |
|
139 | - $field, |
|
140 | - $this->_maybe_add_extra_fields_to_schema($field_name, $field, $schema) |
|
141 | - ); |
|
142 | - } |
|
143 | - return $schema; |
|
144 | - } |
|
145 | - |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
150 | - * response. |
|
151 | - * |
|
152 | - * @param $field_name |
|
153 | - * @param \EE_Model_Field_Base $field |
|
154 | - * @param array $schema |
|
155 | - * @return array |
|
156 | - */ |
|
157 | - protected function _translate_defaults_for_rest_response($field_name, \EE_Model_Field_Base $field, array $schema) |
|
158 | - { |
|
159 | - if (isset($schema['properties'][$field_name]['default'])) { |
|
160 | - if (is_array($schema['properties'][$field_name]['default'])) { |
|
161 | - foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
162 | - if ($default_key === 'raw') { |
|
163 | - $schema['properties'][$field_name]['default'][$default_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
164 | - $field, |
|
165 | - $default_value, |
|
166 | - $this->get_model_version_info()->requested_version() |
|
167 | - ); |
|
168 | - } |
|
169 | - } |
|
170 | - } else { |
|
171 | - $schema['properties'][$field_name]['default'] = Model_Data_Translator::prepare_field_value_for_json( |
|
172 | - $field, |
|
173 | - $schema['properties'][$field_name]['default'], |
|
174 | - $this->get_model_version_info()->requested_version() |
|
175 | - ); |
|
176 | - } |
|
177 | - } |
|
178 | - return $schema; |
|
179 | - } |
|
180 | - |
|
181 | - |
|
182 | - |
|
183 | - /** |
|
184 | - * Adds additional fields to the schema |
|
185 | - * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
186 | - * needs to be added to the schema. |
|
187 | - * |
|
188 | - * @param $field_name |
|
189 | - * @param \EE_Model_Field_Base $field |
|
190 | - * @param array $schema |
|
191 | - * @return array |
|
192 | - */ |
|
193 | - protected function _maybe_add_extra_fields_to_schema($field_name, \EE_Model_Field_Base $field, array $schema) |
|
194 | - { |
|
195 | - if ($field instanceof EE_Datetime_Field) { |
|
196 | - $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
197 | - //modify the description |
|
198 | - $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
199 | - esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
200 | - $field->get_nicename() |
|
201 | - ); |
|
202 | - } |
|
203 | - return $schema; |
|
204 | - } |
|
205 | - |
|
206 | - |
|
207 | - |
|
208 | - /** |
|
209 | - * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
210 | - * |
|
211 | - * @return string |
|
212 | - */ |
|
213 | - protected function get_route_from_request() |
|
214 | - { |
|
215 | - if (isset($GLOBALS['wp']) |
|
216 | - && $GLOBALS['wp'] instanceof \WP |
|
217 | - && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
218 | - ) { |
|
219 | - return $GLOBALS['wp']->query_vars['rest_route']; |
|
220 | - } else { |
|
221 | - return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - |
|
227 | - /** |
|
228 | - * Gets a single entity related to the model indicated in the path and its id |
|
229 | - * |
|
230 | - * @param \WP_REST_Request $request |
|
231 | - * @return \WP_REST_Response|\WP_Error |
|
232 | - */ |
|
233 | - public static function handle_request_get_one(\WP_REST_Request $request) |
|
234 | - { |
|
235 | - $controller = new Read(); |
|
236 | - try { |
|
237 | - $matches = $controller->parse_route( |
|
238 | - $request->get_route(), |
|
239 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
240 | - array('version', 'model', 'id')); |
|
241 | - $controller->set_requested_version($matches['version']); |
|
242 | - $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
243 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
244 | - return $controller->send_response( |
|
245 | - new \WP_Error( |
|
246 | - 'endpoint_parsing_error', |
|
247 | - sprintf( |
|
248 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
249 | - 'event_espresso'), |
|
250 | - $model_name_singular |
|
251 | - ) |
|
252 | - ) |
|
253 | - ); |
|
254 | - } |
|
255 | - return $controller->send_response( |
|
256 | - $controller->get_entity_from_model( |
|
257 | - $controller->get_model_version_info()->load_model($model_name_singular), |
|
258 | - $request |
|
259 | - ) |
|
260 | - ); |
|
261 | - } catch (\Exception $e) { |
|
262 | - return $controller->send_response($e); |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * Gets all the related entities (or if its a belongs-to relation just the one) |
|
270 | - * to the item with the given id |
|
271 | - * |
|
272 | - * @param \WP_REST_Request $request |
|
273 | - * @return \WP_REST_Response|\WP_Error |
|
274 | - */ |
|
275 | - public static function handle_request_get_related(\WP_REST_Request $request) |
|
276 | - { |
|
277 | - $controller = new Read(); |
|
278 | - try { |
|
279 | - $matches = $controller->parse_route( |
|
280 | - $request->get_route(), |
|
281 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
282 | - array('version', 'model', 'id', 'related_model') |
|
283 | - ); |
|
284 | - $controller->set_requested_version($matches['version']); |
|
285 | - $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
286 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
287 | - return $controller->send_response( |
|
288 | - new \WP_Error( |
|
289 | - 'endpoint_parsing_error', |
|
290 | - sprintf( |
|
291 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
292 | - 'event_espresso'), |
|
293 | - $main_model_name_singular |
|
294 | - ) |
|
295 | - ) |
|
296 | - ); |
|
297 | - } |
|
298 | - $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
|
299 | - //assume the related model name is plural and try to find the model's name |
|
300 | - $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
|
301 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
302 | - //so the word didn't singularize well. Maybe that's just because it's a singular word? |
|
303 | - $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
|
304 | - } |
|
305 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
306 | - return $controller->send_response( |
|
307 | - new \WP_Error( |
|
308 | - 'endpoint_parsing_error', |
|
309 | - sprintf( |
|
310 | - __('There is no model for endpoint %s. Please contact event espresso support', |
|
311 | - 'event_espresso'), |
|
312 | - $related_model_name_singular |
|
313 | - ) |
|
314 | - ) |
|
315 | - ); |
|
316 | - } |
|
317 | - return $controller->send_response( |
|
318 | - $controller->get_entities_from_relation( |
|
319 | - $request->get_param('id'), |
|
320 | - $main_model->related_settings_for($related_model_name_singular), |
|
321 | - $request |
|
322 | - ) |
|
323 | - ); |
|
324 | - } catch (\Exception $e) { |
|
325 | - return $controller->send_response($e); |
|
326 | - } |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - |
|
331 | - /** |
|
332 | - * Gets a collection for the given model and filters |
|
333 | - * |
|
334 | - * @param \EEM_Base $model |
|
335 | - * @param \WP_REST_Request $request |
|
336 | - * @return array|\WP_Error |
|
337 | - */ |
|
338 | - public function get_entities_from_model($model, $request) |
|
339 | - { |
|
340 | - $query_params = $this->create_model_query_params($model, $request->get_params()); |
|
341 | - if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
342 | - $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
343 | - return new \WP_Error( |
|
344 | - sprintf('rest_%s_cannot_list', $model_name_plural), |
|
345 | - sprintf( |
|
346 | - __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
347 | - $model_name_plural, |
|
348 | - Capabilities::get_missing_permissions_string($model, $query_params['caps']) |
|
349 | - ), |
|
350 | - array('status' => 403) |
|
351 | - ); |
|
352 | - } |
|
353 | - if (! $request->get_header('no_rest_headers')) { |
|
354 | - $this->_set_headers_from_query_params($model, $query_params); |
|
355 | - } |
|
356 | - /** @type array $results */ |
|
357 | - $results = $model->get_all_wpdb_results($query_params); |
|
358 | - $nice_results = array(); |
|
359 | - foreach ($results as $result) { |
|
360 | - $nice_results[] = $this->create_entity_from_wpdb_result( |
|
361 | - $model, |
|
362 | - $result, |
|
363 | - $request |
|
364 | - ); |
|
365 | - } |
|
366 | - return $nice_results; |
|
367 | - } |
|
368 | - |
|
369 | - |
|
370 | - |
|
371 | - /** |
|
372 | - * @param array $primary_model_query_params query params for finding the item from which |
|
373 | - * relations will be based |
|
374 | - * @param \EE_Model_Relation_Base $relation |
|
375 | - * @param \WP_REST_Request $request |
|
376 | - * @return \WP_Error|array |
|
377 | - */ |
|
378 | - protected function _get_entities_from_relation($primary_model_query_params, $relation, $request) |
|
379 | - { |
|
380 | - $context = $this->validate_context($request->get_param('caps')); |
|
381 | - $model = $relation->get_this_model(); |
|
382 | - $related_model = $relation->get_other_model(); |
|
383 | - if (! isset($primary_model_query_params[0])) { |
|
384 | - $primary_model_query_params[0] = array(); |
|
385 | - } |
|
386 | - //check if they can access the 1st model object |
|
387 | - $primary_model_query_params = array( |
|
388 | - 0 => $primary_model_query_params[0], |
|
389 | - 'limit' => 1, |
|
390 | - ); |
|
391 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
392 | - $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($primary_model_query_params); |
|
393 | - } |
|
394 | - $restricted_query_params = $primary_model_query_params; |
|
395 | - $restricted_query_params['caps'] = $context; |
|
396 | - $this->_set_debug_info('main model query params', $restricted_query_params); |
|
397 | - $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); |
|
398 | - if ( |
|
399 | - ! ( |
|
400 | - Capabilities::current_user_has_partial_access_to($related_model, $context) |
|
401 | - && $model->exists($restricted_query_params) |
|
402 | - ) |
|
403 | - ) { |
|
404 | - if ($relation instanceof \EE_Belongs_To_Relation) { |
|
405 | - $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
406 | - } else { |
|
407 | - $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); |
|
408 | - } |
|
409 | - return new \WP_Error( |
|
410 | - sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
411 | - sprintf( |
|
412 | - __('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
413 | - 'event_espresso'), |
|
414 | - $related_model_name_maybe_plural, |
|
415 | - $relation->get_this_model()->get_this_model_name(), |
|
416 | - implode( |
|
417 | - ',', |
|
418 | - array_keys( |
|
419 | - Capabilities::get_missing_permissions($related_model, $context) |
|
420 | - ) |
|
421 | - ) |
|
422 | - ), |
|
423 | - array('status' => 403) |
|
424 | - ); |
|
425 | - } |
|
426 | - $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); |
|
427 | - foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
428 | - $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
429 | - . '.' |
|
430 | - . $where_condition_key] = $where_condition_value; |
|
431 | - } |
|
432 | - $query_params['default_where_conditions'] = 'none'; |
|
433 | - $query_params['caps'] = $context; |
|
434 | - if (! $request->get_header('no_rest_headers')) { |
|
435 | - $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
|
436 | - } |
|
437 | - /** @type array $results */ |
|
438 | - $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
439 | - $nice_results = array(); |
|
440 | - foreach ($results as $result) { |
|
441 | - $nice_result = $this->create_entity_from_wpdb_result( |
|
442 | - $relation->get_other_model(), |
|
443 | - $result, |
|
444 | - $request |
|
445 | - ); |
|
446 | - if ($relation instanceof \EE_HABTM_Relation) { |
|
447 | - //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
448 | - //if there are conflicts we prefer the properties from the main model |
|
449 | - $join_model_result = $this->create_entity_from_wpdb_result( |
|
450 | - $relation->get_join_model(), |
|
451 | - $result, |
|
452 | - $request |
|
453 | - ); |
|
454 | - $joined_result = array_merge($nice_result, $join_model_result); |
|
455 | - //but keep the meta stuff from the main model |
|
456 | - if (isset($nice_result['meta'])) { |
|
457 | - $joined_result['meta'] = $nice_result['meta']; |
|
458 | - } |
|
459 | - $nice_result = $joined_result; |
|
460 | - } |
|
461 | - $nice_results[] = $nice_result; |
|
462 | - } |
|
463 | - if ($relation instanceof \EE_Belongs_To_Relation) { |
|
464 | - return array_shift($nice_results); |
|
465 | - } else { |
|
466 | - return $nice_results; |
|
467 | - } |
|
468 | - } |
|
469 | - |
|
470 | - |
|
471 | - |
|
472 | - /** |
|
473 | - * Gets the collection for given relation object |
|
474 | - * The same as Read::get_entities_from_model(), except if the relation |
|
475 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
476 | - * the join-model-object into the results |
|
477 | - * |
|
478 | - * @param string $id the ID of the thing we are fetching related stuff from |
|
479 | - * @param \EE_Model_Relation_Base $relation |
|
480 | - * @param \WP_REST_Request $request |
|
481 | - * @return array|\WP_Error |
|
482 | - * @throws \EE_Error |
|
483 | - */ |
|
484 | - public function get_entities_from_relation($id, $relation, $request) |
|
485 | - { |
|
486 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
487 | - throw new \EE_Error( |
|
488 | - sprintf( |
|
489 | - __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
490 | - 'event_espresso'), |
|
491 | - $relation->get_this_model()->get_this_model_name() |
|
492 | - ) |
|
493 | - ); |
|
494 | - } |
|
495 | - return $this->_get_entities_from_relation( |
|
496 | - array( |
|
497 | - array( |
|
498 | - $relation->get_this_model()->primary_key_name() => $id, |
|
499 | - ), |
|
500 | - ), |
|
501 | - $relation, |
|
502 | - $request |
|
503 | - ); |
|
504 | - } |
|
505 | - |
|
506 | - |
|
507 | - |
|
508 | - /** |
|
509 | - * Sets the headers that are based on the model and query params, |
|
510 | - * like the total records. This should only be called on the original request |
|
511 | - * from the client, not on subsequent internal |
|
512 | - * |
|
513 | - * @param \EEM_Base $model |
|
514 | - * @param array $query_params |
|
515 | - * @return void |
|
516 | - */ |
|
517 | - protected function _set_headers_from_query_params($model, $query_params) |
|
518 | - { |
|
519 | - $this->_set_debug_info('model query params', $query_params); |
|
520 | - $this->_set_debug_info('missing caps', |
|
521 | - Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
|
522 | - //normally the limit to a 2-part array, where the 2nd item is the limit |
|
523 | - if (! isset($query_params['limit'])) { |
|
524 | - $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
525 | - } |
|
526 | - if (is_array($query_params['limit'])) { |
|
527 | - $limit_parts = $query_params['limit']; |
|
528 | - } else { |
|
529 | - $limit_parts = explode(',', $query_params['limit']); |
|
530 | - if (count($limit_parts) == 1) { |
|
531 | - $limit_parts = array(0, $limit_parts[0]); |
|
532 | - } |
|
533 | - } |
|
534 | - //remove the group by and having parts of the query, as those will |
|
535 | - //make the sql query return an array of values, instead of just a single value |
|
536 | - unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
537 | - $count = $model->count($query_params, null, true); |
|
538 | - $pages = $count / $limit_parts[1]; |
|
539 | - $this->_set_response_header('Total', $count, false); |
|
540 | - $this->_set_response_header('PageSize', $limit_parts[1], false); |
|
541 | - $this->_set_response_header('TotalPages', ceil($pages), false); |
|
542 | - } |
|
543 | - |
|
544 | - |
|
545 | - |
|
546 | - /** |
|
547 | - * Changes database results into REST API entities |
|
548 | - * |
|
549 | - * @param \EEM_Base $model |
|
550 | - * @param array $db_row like results from $wpdb->get_results() |
|
551 | - * @param \WP_REST_Request $rest_request |
|
552 | - * @param string $deprecated no longer used |
|
553 | - * @return array ready for being converted into json for sending to client |
|
554 | - */ |
|
555 | - public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
|
556 | - { |
|
557 | - if (! $rest_request instanceof \WP_REST_Request) { |
|
558 | - //ok so this was called in the old style, where the 3rd arg was |
|
559 | - //$include, and the 4th arg was $context |
|
560 | - //now setup the request just to avoid fatal errors, although we won't be able |
|
561 | - //to truly make use of it because it's kinda devoid of info |
|
562 | - $rest_request = new \WP_REST_Request(); |
|
563 | - $rest_request->set_param('include', $rest_request); |
|
564 | - $rest_request->set_param('caps', $deprecated); |
|
565 | - } |
|
566 | - if ($rest_request->get_param('caps') == null) { |
|
567 | - $rest_request->set_param('caps', \EEM_Base::caps_read); |
|
568 | - } |
|
569 | - $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); |
|
570 | - $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); |
|
571 | - $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); |
|
572 | - $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); |
|
573 | - $entity_array = apply_filters('FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
574 | - $entity_array, $model, $rest_request->get_param('caps'), $rest_request, $this); |
|
575 | - $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array, $db_row); |
|
576 | - $entity_array = apply_filters( |
|
577 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
578 | - $entity_array, |
|
579 | - $model, |
|
580 | - $rest_request->get_param('caps'), |
|
581 | - $rest_request, |
|
582 | - $this |
|
583 | - ); |
|
584 | - $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( |
|
585 | - $entity_array, |
|
586 | - $model, |
|
587 | - $rest_request->get_param('caps'), |
|
588 | - $this->get_model_version_info(), |
|
589 | - $model->get_index_primary_key_string( |
|
590 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
591 | - ) |
|
592 | - ); |
|
593 | - $this->_set_debug_info( |
|
594 | - 'inaccessible fields', |
|
595 | - array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
596 | - ); |
|
597 | - return apply_filters( |
|
598 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
599 | - $result_without_inaccessible_fields, |
|
600 | - $model, |
|
601 | - $rest_request->get_param('caps') |
|
602 | - ); |
|
603 | - } |
|
604 | - |
|
605 | - |
|
606 | - |
|
607 | - /** |
|
608 | - * Creates a REST entity array (JSON object we're going to return in the response, but |
|
609 | - * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
610 | - * from $wpdb->get_row( $sql, ARRAY_A) |
|
611 | - * |
|
612 | - * @param \EEM_Base $model |
|
613 | - * @param array $db_row |
|
614 | - * @return array entity mostly ready for converting to JSON and sending in the response |
|
615 | - */ |
|
616 | - protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) |
|
617 | - { |
|
618 | - $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
619 | - $result = array_intersect_key($result, |
|
620 | - $this->get_model_version_info()->fields_on_model_in_this_version($model)); |
|
621 | - foreach ($result as $field_name => $raw_field_value) { |
|
622 | - $field_obj = $model->field_settings_for($field_name); |
|
623 | - $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); |
|
624 | - if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { |
|
625 | - unset($result[$field_name]); |
|
626 | - } elseif ( |
|
627 | - $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) |
|
628 | - ) { |
|
629 | - $result[$field_name] = array( |
|
630 | - 'raw' => $field_obj->prepare_for_get($field_value), |
|
631 | - 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
632 | - ); |
|
633 | - } elseif ( |
|
634 | - $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) |
|
635 | - ) { |
|
636 | - $result[$field_name] = array( |
|
637 | - 'raw' => $field_obj->prepare_for_get($field_value), |
|
638 | - 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
639 | - ); |
|
640 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
641 | - if ($field_value instanceof \DateTime) { |
|
642 | - $timezone = $field_value->getTimezone(); |
|
643 | - $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
644 | - $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
645 | - $field_obj, |
|
646 | - $field_value, |
|
647 | - $this->get_model_version_info()->requested_version() |
|
648 | - ); |
|
649 | - $field_value->setTimezone($timezone); |
|
650 | - $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
651 | - $field_obj, |
|
652 | - $field_value, |
|
653 | - $this->get_model_version_info()->requested_version() |
|
654 | - ); |
|
655 | - } |
|
656 | - } else { |
|
657 | - $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
658 | - $field_obj, |
|
659 | - $field_obj->prepare_for_get($field_value), |
|
660 | - $this->get_model_version_info()->requested_version() |
|
661 | - ); |
|
662 | - } |
|
663 | - } |
|
664 | - return $result; |
|
665 | - } |
|
666 | - |
|
667 | - |
|
668 | - |
|
669 | - /** |
|
670 | - * Adds a few extra fields to the entity response |
|
671 | - * |
|
672 | - * @param \EEM_Base $model |
|
673 | - * @param array $db_row |
|
674 | - * @param array $entity_array |
|
675 | - * @return array modified entity |
|
676 | - */ |
|
677 | - protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) |
|
678 | - { |
|
679 | - if ($model instanceof \EEM_CPT_Base) { |
|
680 | - $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
681 | - } |
|
682 | - return $entity_array; |
|
683 | - } |
|
684 | - |
|
685 | - |
|
686 | - |
|
687 | - /** |
|
688 | - * Gets links we want to add to the response |
|
689 | - * |
|
690 | - * @global \WP_REST_Server $wp_rest_server |
|
691 | - * @param \EEM_Base $model |
|
692 | - * @param array $db_row |
|
693 | - * @param array $entity_array |
|
694 | - * @return array the _links item in the entity |
|
695 | - */ |
|
696 | - protected function _get_entity_links($model, $db_row, $entity_array) |
|
697 | - { |
|
698 | - //add basic links |
|
699 | - $links = array(); |
|
700 | - if ($model->has_primary_key_field()) { |
|
701 | - $links['self'] = array( |
|
702 | - array( |
|
703 | - 'href' => $this->get_versioned_link_to( |
|
704 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
705 | - . '/' |
|
706 | - . $entity_array[$model->primary_key_name()] |
|
707 | - ), |
|
708 | - ), |
|
709 | - ); |
|
710 | - } |
|
711 | - $links['collection'] = array( |
|
712 | - array( |
|
713 | - 'href' => $this->get_versioned_link_to( |
|
714 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
715 | - ), |
|
716 | - ), |
|
717 | - ); |
|
718 | - //add links to related models |
|
719 | - if ($model->has_primary_key_field()) { |
|
720 | - foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
|
721 | - $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
|
722 | - $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
723 | - array( |
|
724 | - 'href' => $this->get_versioned_link_to( |
|
725 | - \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
726 | - . '/' |
|
727 | - . $entity_array[$model->primary_key_name()] |
|
728 | - . '/' |
|
729 | - . $related_model_part |
|
730 | - ), |
|
731 | - 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
732 | - ), |
|
733 | - ); |
|
734 | - } |
|
735 | - } |
|
736 | - return $links; |
|
737 | - } |
|
738 | - |
|
739 | - |
|
740 | - |
|
741 | - /** |
|
742 | - * Adds the included models indicated in the request to the entity provided |
|
743 | - * |
|
744 | - * @param \EEM_Base $model |
|
745 | - * @param \WP_REST_Request $rest_request |
|
746 | - * @param array $entity_array |
|
747 | - * @param array $db_row |
|
748 | - * @return array the modified entity |
|
749 | - */ |
|
750 | - protected function _include_requested_models( |
|
751 | - \EEM_Base $model, |
|
752 | - \WP_REST_Request $rest_request, |
|
753 | - $entity_array, |
|
754 | - $db_row = array() |
|
755 | - ) { |
|
756 | - //if $db_row not included, hope the entity array has what we need |
|
757 | - if (! $db_row) { |
|
758 | - $db_row = $entity_array; |
|
759 | - } |
|
760 | - $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
|
761 | - $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
|
762 | - //if they passed in * or didn't specify any includes, return everything |
|
763 | - if (! in_array('*', $includes_for_this_model) |
|
764 | - && ! empty($includes_for_this_model) |
|
765 | - ) { |
|
766 | - if ($model->has_primary_key_field()) { |
|
767 | - //always include the primary key. ya just gotta know that at least |
|
768 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
769 | - } |
|
770 | - if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { |
|
771 | - $includes_for_this_model[] = '_calculated_fields'; |
|
772 | - } |
|
773 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
774 | - } |
|
775 | - $relation_settings = $this->get_model_version_info()->relation_settings($model); |
|
776 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
777 | - $related_fields_to_include = $this->explode_and_get_items_prefixed_with( |
|
778 | - $rest_request->get_param('include'), |
|
779 | - $relation_name |
|
780 | - ); |
|
781 | - $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( |
|
782 | - $rest_request->get_param('calculate'), |
|
783 | - $relation_name |
|
784 | - ); |
|
785 | - //did they specify they wanted to include a related model, or |
|
786 | - //specific fields from a related model? |
|
787 | - //or did they specify to calculate a field from a related model? |
|
788 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
789 | - //if so, we should include at least some part of the related model |
|
790 | - $pretend_related_request = new \WP_REST_Request(); |
|
791 | - $pretend_related_request->set_query_params( |
|
792 | - array( |
|
793 | - 'caps' => $rest_request->get_param('caps'), |
|
794 | - 'include' => $related_fields_to_include, |
|
795 | - 'calculate' => $related_fields_to_calculate, |
|
796 | - ) |
|
797 | - ); |
|
798 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
799 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
800 | - $model->get_index_primary_key_string( |
|
801 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
802 | - ) |
|
803 | - ); |
|
804 | - $related_results = $this->_get_entities_from_relation( |
|
805 | - $primary_model_query_params, |
|
806 | - $relation_obj, |
|
807 | - $pretend_related_request |
|
808 | - ); |
|
809 | - $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results |
|
810 | - instanceof |
|
811 | - \WP_Error |
|
812 | - ? null |
|
813 | - : $related_results; |
|
814 | - } |
|
815 | - } |
|
816 | - return $entity_array; |
|
817 | - } |
|
818 | - |
|
819 | - |
|
820 | - |
|
821 | - /** |
|
822 | - * Returns a new array with all the names of models removed. Eg |
|
823 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
824 | - * |
|
825 | - * @param array $arr |
|
826 | - * @return array |
|
827 | - */ |
|
828 | - private function _remove_model_names_from_array($arr) |
|
829 | - { |
|
830 | - return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); |
|
831 | - } |
|
832 | - |
|
833 | - |
|
834 | - |
|
835 | - /** |
|
836 | - * Gets the calculated fields for the response |
|
837 | - * |
|
838 | - * @param \EEM_Base $model |
|
839 | - * @param array $wpdb_row |
|
840 | - * @param \WP_REST_Request $rest_request |
|
841 | - * @return \stdClass the _calculations item in the entity |
|
842 | - */ |
|
843 | - protected function _get_entity_calculations($model, $wpdb_row, $rest_request) |
|
844 | - { |
|
845 | - $calculated_fields = $this->explode_and_get_items_prefixed_with( |
|
846 | - $rest_request->get_param('calculate'), |
|
847 | - '' |
|
848 | - ); |
|
849 | - //note: setting calculate=* doesn't do anything |
|
850 | - $calculated_fields_to_return = new \stdClass(); |
|
851 | - foreach ($calculated_fields as $field_to_calculate) { |
|
852 | - try { |
|
853 | - $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( |
|
854 | - null, |
|
855 | - $this->_fields_calculator->retrieve_calculated_field_value( |
|
856 | - $model, |
|
857 | - $field_to_calculate, |
|
858 | - $wpdb_row, |
|
859 | - $rest_request, |
|
860 | - $this |
|
861 | - ), |
|
862 | - $this->get_model_version_info()->requested_version() |
|
863 | - ); |
|
864 | - } catch (Rest_Exception $e) { |
|
865 | - //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
866 | - $this->_set_response_header( |
|
867 | - 'Notices-Field-Calculation-Errors[' |
|
868 | - . $e->get_string_code() |
|
869 | - . '][' |
|
870 | - . $model->get_this_model_name() |
|
871 | - . '][' |
|
872 | - . $field_to_calculate |
|
873 | - . ']', |
|
874 | - $e->getMessage(), |
|
875 | - true |
|
876 | - ); |
|
877 | - } |
|
878 | - } |
|
879 | - return $calculated_fields_to_return; |
|
880 | - } |
|
881 | - |
|
882 | - |
|
883 | - |
|
884 | - /** |
|
885 | - * Gets the full URL to the resource, taking the requested version into account |
|
886 | - * |
|
887 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
888 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
889 | - */ |
|
890 | - public function get_versioned_link_to($link_part_after_version_and_slash) |
|
891 | - { |
|
892 | - return rest_url( |
|
893 | - \EED_Core_Rest_Api::ee_api_namespace |
|
894 | - . $this->get_model_version_info()->requested_version() |
|
895 | - . '/' |
|
896 | - . $link_part_after_version_and_slash |
|
897 | - ); |
|
898 | - } |
|
899 | - |
|
900 | - |
|
901 | - |
|
902 | - /** |
|
903 | - * Gets the correct lowercase name for the relation in the API according |
|
904 | - * to the relation's type |
|
905 | - * |
|
906 | - * @param string $relation_name |
|
907 | - * @param \EE_Model_Relation_Base $relation_obj |
|
908 | - * @return string |
|
909 | - */ |
|
910 | - public static function get_related_entity_name($relation_name, $relation_obj) |
|
911 | - { |
|
912 | - if ($relation_obj instanceof \EE_Belongs_To_Relation) { |
|
913 | - return strtolower($relation_name); |
|
914 | - } else { |
|
915 | - return \EEH_Inflector::pluralize_and_lower($relation_name); |
|
916 | - } |
|
917 | - } |
|
918 | - |
|
919 | - |
|
920 | - |
|
921 | - /** |
|
922 | - * Gets the one model object with the specified id for the specified model |
|
923 | - * |
|
924 | - * @param \EEM_Base $model |
|
925 | - * @param \WP_REST_Request $request |
|
926 | - * @return array|\WP_Error |
|
927 | - */ |
|
928 | - public function get_entity_from_model($model, $request) |
|
929 | - { |
|
930 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
931 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
932 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
933 | - } |
|
934 | - $restricted_query_params = $query_params; |
|
935 | - $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
|
936 | - $this->_set_debug_info('model query params', $restricted_query_params); |
|
937 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
938 | - if (! empty ($model_rows)) { |
|
939 | - return $this->create_entity_from_wpdb_result( |
|
940 | - $model, |
|
941 | - array_shift($model_rows), |
|
942 | - $request); |
|
943 | - } else { |
|
944 | - //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
945 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
946 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
947 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
948 | - //you got shafted- it existed but we didn't want to tell you! |
|
949 | - return new \WP_Error( |
|
950 | - 'rest_user_cannot_read', |
|
951 | - sprintf( |
|
952 | - __('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), |
|
953 | - strtolower($model->get_this_model_name()), |
|
954 | - Capabilities::get_missing_permissions_string( |
|
955 | - $model, |
|
956 | - $this->validate_context($request->get_param('caps'))) |
|
957 | - ), |
|
958 | - array('status' => 403) |
|
959 | - ); |
|
960 | - } else { |
|
961 | - //it's not you. It just doesn't exist |
|
962 | - return new \WP_Error( |
|
963 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
964 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
965 | - array('status' => 404) |
|
966 | - ); |
|
967 | - } |
|
968 | - } |
|
969 | - } |
|
970 | - |
|
971 | - |
|
972 | - |
|
973 | - /** |
|
974 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
975 | - * version so just treat it as a default read |
|
976 | - * |
|
977 | - * @param string $context |
|
978 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
979 | - */ |
|
980 | - public function validate_context($context) |
|
981 | - { |
|
982 | - if (! $context) { |
|
983 | - $context = \EEM_Base::caps_read; |
|
984 | - } |
|
985 | - $valid_contexts = \EEM_Base::valid_cap_contexts(); |
|
986 | - if (in_array($context, $valid_contexts)) { |
|
987 | - return $context; |
|
988 | - } else { |
|
989 | - return \EEM_Base::caps_read; |
|
990 | - } |
|
991 | - } |
|
992 | - |
|
993 | - |
|
994 | - |
|
995 | - /** |
|
996 | - * Verifies the passed in value is an allowable default where conditions value. |
|
997 | - * |
|
998 | - * @param $default_query_params |
|
999 | - * @return string |
|
1000 | - */ |
|
1001 | - public function validate_default_query_params($default_query_params) |
|
1002 | - { |
|
1003 | - $valid_default_where_conditions_for_api_calls = array( |
|
1004 | - \EEM_Base::default_where_conditions_all, |
|
1005 | - \EEM_Base::default_where_conditions_minimum_all, |
|
1006 | - \EEM_Base::default_where_conditions_minimum_others, |
|
1007 | - ); |
|
1008 | - if (! $default_query_params) { |
|
1009 | - $default_query_params = \EEM_Base::default_where_conditions_all; |
|
1010 | - } |
|
1011 | - if ( |
|
1012 | - in_array( |
|
1013 | - $default_query_params, |
|
1014 | - $valid_default_where_conditions_for_api_calls, |
|
1015 | - true |
|
1016 | - ) |
|
1017 | - ) { |
|
1018 | - return $default_query_params; |
|
1019 | - } else { |
|
1020 | - return \EEM_Base::default_where_conditions_all; |
|
1021 | - } |
|
1022 | - } |
|
1023 | - |
|
1024 | - |
|
1025 | - |
|
1026 | - /** |
|
1027 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1028 | - * Note: right now the query parameter keys for fields (and related fields) |
|
1029 | - * can be left as-is, but it's quite possible this will change someday. |
|
1030 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1031 | - * |
|
1032 | - * @param \EEM_Base $model |
|
1033 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1034 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1035 | - * that absolutely no results should be returned |
|
1036 | - * @throws \EE_Error |
|
1037 | - */ |
|
1038 | - public function create_model_query_params($model, $query_parameters) |
|
1039 | - { |
|
1040 | - $model_query_params = array(); |
|
1041 | - if (isset($query_parameters['where'])) { |
|
1042 | - $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
1043 | - $query_parameters['where'], |
|
1044 | - $model, |
|
1045 | - $this->get_model_version_info()->requested_version() |
|
1046 | - ); |
|
1047 | - } |
|
1048 | - if (isset($query_parameters['order_by'])) { |
|
1049 | - $order_by = $query_parameters['order_by']; |
|
1050 | - } elseif (isset($query_parameters['orderby'])) { |
|
1051 | - $order_by = $query_parameters['orderby']; |
|
1052 | - } else { |
|
1053 | - $order_by = null; |
|
1054 | - } |
|
1055 | - if ($order_by !== null) { |
|
1056 | - if (is_array($order_by)) { |
|
1057 | - $order_by = Model_Data_Translator::prepare_field_names_in_array_keys_from_json($order_by); |
|
1058 | - } else { |
|
1059 | - //it's a single item |
|
1060 | - $order_by = Model_Data_Translator::prepare_field_name_from_json($order_by); |
|
1061 | - } |
|
1062 | - $model_query_params['order_by'] = $order_by; |
|
1063 | - } |
|
1064 | - if (isset($query_parameters['group_by'])) { |
|
1065 | - $group_by = $query_parameters['group_by']; |
|
1066 | - } elseif (isset($query_parameters['groupby'])) { |
|
1067 | - $group_by = $query_parameters['groupby']; |
|
1068 | - } else { |
|
1069 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1070 | - } |
|
1071 | - //make sure they're all real names |
|
1072 | - if (is_array($group_by)) { |
|
1073 | - $group_by = Model_Data_Translator::prepare_field_names_from_json($group_by); |
|
1074 | - } |
|
1075 | - if ($group_by !== null) { |
|
1076 | - $model_query_params['group_by'] = $group_by; |
|
1077 | - } |
|
1078 | - if (isset($query_parameters['having'])) { |
|
1079 | - $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
1080 | - $query_parameters['having'], |
|
1081 | - $model, |
|
1082 | - $this->get_model_version_info()->requested_version() |
|
1083 | - ); |
|
1084 | - } |
|
1085 | - if (isset($query_parameters['order'])) { |
|
1086 | - $model_query_params['order'] = $query_parameters['order']; |
|
1087 | - } |
|
1088 | - if (isset($query_parameters['mine'])) { |
|
1089 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1090 | - } |
|
1091 | - if (isset($query_parameters['limit'])) { |
|
1092 | - //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1093 | - if (! is_array($query_parameters['limit'])) { |
|
1094 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1095 | - } else { |
|
1096 | - $limit_array = $query_parameters['limit']; |
|
1097 | - } |
|
1098 | - $sanitized_limit = array(); |
|
1099 | - foreach ($limit_array as $key => $limit_part) { |
|
1100 | - if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1101 | - throw new \EE_Error( |
|
1102 | - sprintf( |
|
1103 | - __('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.', |
|
1104 | - 'event_espresso'), |
|
1105 | - wp_json_encode($query_parameters['limit']) |
|
1106 | - ) |
|
1107 | - ); |
|
1108 | - } |
|
1109 | - $sanitized_limit[] = (int)$limit_part; |
|
1110 | - } |
|
1111 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1112 | - } else { |
|
1113 | - $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
1114 | - } |
|
1115 | - if (isset($query_parameters['caps'])) { |
|
1116 | - $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); |
|
1117 | - } else { |
|
1118 | - $model_query_params['caps'] = \EEM_Base::caps_read; |
|
1119 | - } |
|
1120 | - if (isset($query_parameters['default_where_conditions'])) { |
|
1121 | - $model_query_params['default_where_conditions'] = $this->validate_default_query_params($query_parameters['default_where_conditions']); |
|
1122 | - } |
|
1123 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1124 | - } |
|
1125 | - |
|
1126 | - |
|
1127 | - |
|
1128 | - /** |
|
1129 | - * Changes the REST-style query params for use in the models |
|
1130 | - * |
|
1131 | - * @deprecated |
|
1132 | - * @param \EEM_Base $model |
|
1133 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1134 | - * @return array |
|
1135 | - */ |
|
1136 | - public function prepare_rest_query_params_key_for_models($model, $query_params) |
|
1137 | - { |
|
1138 | - $model_ready_query_params = array(); |
|
1139 | - foreach ($query_params as $key => $value) { |
|
1140 | - if (is_array($value)) { |
|
1141 | - $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); |
|
1142 | - } else { |
|
1143 | - $model_ready_query_params[$key] = $value; |
|
1144 | - } |
|
1145 | - } |
|
1146 | - return $model_ready_query_params; |
|
1147 | - } |
|
1148 | - |
|
1149 | - |
|
1150 | - |
|
1151 | - /** |
|
1152 | - * @deprecated |
|
1153 | - * @param $model |
|
1154 | - * @param $query_params |
|
1155 | - * @return array |
|
1156 | - */ |
|
1157 | - public function prepare_rest_query_params_values_for_models($model, $query_params) |
|
1158 | - { |
|
1159 | - $model_ready_query_params = array(); |
|
1160 | - foreach ($query_params as $key => $value) { |
|
1161 | - if (is_array($value)) { |
|
1162 | - $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); |
|
1163 | - } else { |
|
1164 | - $model_ready_query_params[$key] = $value; |
|
1165 | - } |
|
1166 | - } |
|
1167 | - return $model_ready_query_params; |
|
1168 | - } |
|
1169 | - |
|
1170 | - |
|
1171 | - |
|
1172 | - /** |
|
1173 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1174 | - * If no prefix is specified, returns items with no period. |
|
1175 | - * |
|
1176 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blaabla" or array('jibba', 'jabba' ) |
|
1177 | - * @param string $prefix "Event" or "foobar" |
|
1178 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1179 | - * we only return strings starting with that and a period; if no prefix was |
|
1180 | - * specified we return all items containing NO periods |
|
1181 | - */ |
|
1182 | - public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) |
|
1183 | - { |
|
1184 | - if (is_string($string_to_explode)) { |
|
1185 | - $exploded_contents = explode(',', $string_to_explode); |
|
1186 | - } else if (is_array($string_to_explode)) { |
|
1187 | - $exploded_contents = $string_to_explode; |
|
1188 | - } else { |
|
1189 | - $exploded_contents = array(); |
|
1190 | - } |
|
1191 | - //if the string was empty, we want an empty array |
|
1192 | - $exploded_contents = array_filter($exploded_contents); |
|
1193 | - $contents_with_prefix = array(); |
|
1194 | - foreach ($exploded_contents as $item) { |
|
1195 | - $item = trim($item); |
|
1196 | - //if no prefix was provided, so we look for items with no "." in them |
|
1197 | - if (! $prefix) { |
|
1198 | - //does this item have a period? |
|
1199 | - if (strpos($item, '.') === false) { |
|
1200 | - //if not, then its what we're looking for |
|
1201 | - $contents_with_prefix[] = $item; |
|
1202 | - } |
|
1203 | - } else if (strpos($item, $prefix . '.') === 0) { |
|
1204 | - //this item has the prefix and a period, grab it |
|
1205 | - $contents_with_prefix[] = substr( |
|
1206 | - $item, |
|
1207 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1208 | - ); |
|
1209 | - } else if ($item === $prefix) { |
|
1210 | - //this item is JUST the prefix |
|
1211 | - //so let's grab everything after, which is a blank string |
|
1212 | - $contents_with_prefix[] = ''; |
|
1213 | - } |
|
1214 | - } |
|
1215 | - return $contents_with_prefix; |
|
1216 | - } |
|
1217 | - |
|
1218 | - |
|
1219 | - |
|
1220 | - /** |
|
1221 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1222 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1223 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1224 | - * array('*') (when you provided a model and a model of that kind was found). |
|
1225 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1226 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
1227 | - * name and then a period). |
|
1228 | - * @param string $include_string @see Read:handle_request_get_all |
|
1229 | - * @param string $model_name |
|
1230 | - * @return array of fields for this model. If $model_name is provided, then |
|
1231 | - * the fields for that model, with the model's name removed from each. |
|
1232 | - * If $include_string was blank or '*' returns an empty array |
|
1233 | - */ |
|
1234 | - public function extract_includes_for_this_model($include_string, $model_name = null) |
|
1235 | - { |
|
1236 | - if (is_array($include_string)) { |
|
1237 | - $include_string = implode(',', $include_string); |
|
1238 | - } |
|
1239 | - if ($include_string === '*' || $include_string === '') { |
|
1240 | - return array(); |
|
1241 | - } |
|
1242 | - $includes = explode(',', $include_string); |
|
1243 | - $extracted_fields_to_include = array(); |
|
1244 | - if ($model_name) { |
|
1245 | - foreach ($includes as $field_to_include) { |
|
1246 | - $field_to_include = trim($field_to_include); |
|
1247 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1248 | - //found the model name at the exact start |
|
1249 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1250 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
1251 | - } elseif ($field_to_include == $model_name) { |
|
1252 | - $extracted_fields_to_include[] = '*'; |
|
1253 | - } |
|
1254 | - } |
|
1255 | - } else { |
|
1256 | - //look for ones with no period |
|
1257 | - foreach ($includes as $field_to_include) { |
|
1258 | - $field_to_include = trim($field_to_include); |
|
1259 | - if ( |
|
1260 | - strpos($field_to_include, '.') === false |
|
1261 | - && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) |
|
1262 | - ) { |
|
1263 | - $extracted_fields_to_include[] = $field_to_include; |
|
1264 | - } |
|
1265 | - } |
|
1266 | - } |
|
1267 | - return $extracted_fields_to_include; |
|
1268 | - } |
|
30 | + /** |
|
31 | + * @var Calculated_Model_Fields |
|
32 | + */ |
|
33 | + protected $_fields_calculator; |
|
34 | + |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * Read constructor. |
|
39 | + */ |
|
40 | + public function __construct() |
|
41 | + { |
|
42 | + parent::__construct(); |
|
43 | + $this->_fields_calculator = new Calculated_Model_Fields(); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
50 | + * |
|
51 | + * @param \WP_REST_Request $request |
|
52 | + * @return \WP_REST_Response|\WP_Error |
|
53 | + */ |
|
54 | + public static function handle_request_get_all(\WP_REST_Request $request) |
|
55 | + { |
|
56 | + $controller = new Read(); |
|
57 | + try { |
|
58 | + $matches = $controller->parse_route( |
|
59 | + $request->get_route(), |
|
60 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
61 | + array('version', 'model') |
|
62 | + ); |
|
63 | + $controller->set_requested_version($matches['version']); |
|
64 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
65 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
66 | + return $controller->send_response( |
|
67 | + new \WP_Error( |
|
68 | + 'endpoint_parsing_error', |
|
69 | + sprintf( |
|
70 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
71 | + 'event_espresso'), |
|
72 | + $model_name_singular |
|
73 | + ) |
|
74 | + ) |
|
75 | + ); |
|
76 | + } |
|
77 | + return $controller->send_response( |
|
78 | + $controller->get_entities_from_model( |
|
79 | + $controller->get_model_version_info()->load_model($model_name_singular), |
|
80 | + $request |
|
81 | + ) |
|
82 | + ); |
|
83 | + } catch (\Exception $e) { |
|
84 | + return $controller->send_response($e); |
|
85 | + } |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * Prepares and returns schema for any OPTIONS request. |
|
92 | + * |
|
93 | + * @param string $model_name Something like `Event` or `Registration` |
|
94 | + * @param string $version The API endpoint version being used. |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + public static function handle_schema_request($model_name, $version) |
|
98 | + { |
|
99 | + $controller = new Read(); |
|
100 | + try { |
|
101 | + $controller->set_requested_version($version); |
|
102 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
103 | + return array(); |
|
104 | + } |
|
105 | + //get the model for this version |
|
106 | + $model = $controller->get_model_version_info()->load_model($model_name); |
|
107 | + $model_schema = new JsonModelSchema($model); |
|
108 | + return $model_schema->getModelSchemaForRelations( |
|
109 | + $controller->get_model_version_info()->relation_settings($model), |
|
110 | + $controller->_customize_schema_for_rest_response( |
|
111 | + $model, |
|
112 | + $model_schema->getModelSchemaForFields( |
|
113 | + $controller->get_model_version_info()->fields_on_model_in_this_version($model), |
|
114 | + $model_schema->getInitialSchemaStructure() |
|
115 | + ) |
|
116 | + ) |
|
117 | + ); |
|
118 | + } catch (\Exception $e) { |
|
119 | + return array(); |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + |
|
125 | + /** |
|
126 | + * This loops through each field in the given schema for the model and does the following: |
|
127 | + * - add any extra fields that are REST API specific and related to existing fields. |
|
128 | + * - transform default values into the correct format for a REST API response. |
|
129 | + * |
|
130 | + * @param \EEM_Base $model |
|
131 | + * @param array $schema |
|
132 | + * @return array The final schema. |
|
133 | + */ |
|
134 | + protected function _customize_schema_for_rest_response(\EEM_Base $model, array $schema) |
|
135 | + { |
|
136 | + foreach ($this->get_model_version_info()->fields_on_model_in_this_version($model) as $field_name => $field) { |
|
137 | + $schema = $this->_translate_defaults_for_rest_response( |
|
138 | + $field_name, |
|
139 | + $field, |
|
140 | + $this->_maybe_add_extra_fields_to_schema($field_name, $field, $schema) |
|
141 | + ); |
|
142 | + } |
|
143 | + return $schema; |
|
144 | + } |
|
145 | + |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
150 | + * response. |
|
151 | + * |
|
152 | + * @param $field_name |
|
153 | + * @param \EE_Model_Field_Base $field |
|
154 | + * @param array $schema |
|
155 | + * @return array |
|
156 | + */ |
|
157 | + protected function _translate_defaults_for_rest_response($field_name, \EE_Model_Field_Base $field, array $schema) |
|
158 | + { |
|
159 | + if (isset($schema['properties'][$field_name]['default'])) { |
|
160 | + if (is_array($schema['properties'][$field_name]['default'])) { |
|
161 | + foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
162 | + if ($default_key === 'raw') { |
|
163 | + $schema['properties'][$field_name]['default'][$default_key] = Model_Data_Translator::prepare_field_value_for_json( |
|
164 | + $field, |
|
165 | + $default_value, |
|
166 | + $this->get_model_version_info()->requested_version() |
|
167 | + ); |
|
168 | + } |
|
169 | + } |
|
170 | + } else { |
|
171 | + $schema['properties'][$field_name]['default'] = Model_Data_Translator::prepare_field_value_for_json( |
|
172 | + $field, |
|
173 | + $schema['properties'][$field_name]['default'], |
|
174 | + $this->get_model_version_info()->requested_version() |
|
175 | + ); |
|
176 | + } |
|
177 | + } |
|
178 | + return $schema; |
|
179 | + } |
|
180 | + |
|
181 | + |
|
182 | + |
|
183 | + /** |
|
184 | + * Adds additional fields to the schema |
|
185 | + * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
186 | + * needs to be added to the schema. |
|
187 | + * |
|
188 | + * @param $field_name |
|
189 | + * @param \EE_Model_Field_Base $field |
|
190 | + * @param array $schema |
|
191 | + * @return array |
|
192 | + */ |
|
193 | + protected function _maybe_add_extra_fields_to_schema($field_name, \EE_Model_Field_Base $field, array $schema) |
|
194 | + { |
|
195 | + if ($field instanceof EE_Datetime_Field) { |
|
196 | + $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
197 | + //modify the description |
|
198 | + $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
199 | + esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
200 | + $field->get_nicename() |
|
201 | + ); |
|
202 | + } |
|
203 | + return $schema; |
|
204 | + } |
|
205 | + |
|
206 | + |
|
207 | + |
|
208 | + /** |
|
209 | + * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
210 | + * |
|
211 | + * @return string |
|
212 | + */ |
|
213 | + protected function get_route_from_request() |
|
214 | + { |
|
215 | + if (isset($GLOBALS['wp']) |
|
216 | + && $GLOBALS['wp'] instanceof \WP |
|
217 | + && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
218 | + ) { |
|
219 | + return $GLOBALS['wp']->query_vars['rest_route']; |
|
220 | + } else { |
|
221 | + return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + |
|
226 | + |
|
227 | + /** |
|
228 | + * Gets a single entity related to the model indicated in the path and its id |
|
229 | + * |
|
230 | + * @param \WP_REST_Request $request |
|
231 | + * @return \WP_REST_Response|\WP_Error |
|
232 | + */ |
|
233 | + public static function handle_request_get_one(\WP_REST_Request $request) |
|
234 | + { |
|
235 | + $controller = new Read(); |
|
236 | + try { |
|
237 | + $matches = $controller->parse_route( |
|
238 | + $request->get_route(), |
|
239 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
240 | + array('version', 'model', 'id')); |
|
241 | + $controller->set_requested_version($matches['version']); |
|
242 | + $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
243 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
244 | + return $controller->send_response( |
|
245 | + new \WP_Error( |
|
246 | + 'endpoint_parsing_error', |
|
247 | + sprintf( |
|
248 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
249 | + 'event_espresso'), |
|
250 | + $model_name_singular |
|
251 | + ) |
|
252 | + ) |
|
253 | + ); |
|
254 | + } |
|
255 | + return $controller->send_response( |
|
256 | + $controller->get_entity_from_model( |
|
257 | + $controller->get_model_version_info()->load_model($model_name_singular), |
|
258 | + $request |
|
259 | + ) |
|
260 | + ); |
|
261 | + } catch (\Exception $e) { |
|
262 | + return $controller->send_response($e); |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * Gets all the related entities (or if its a belongs-to relation just the one) |
|
270 | + * to the item with the given id |
|
271 | + * |
|
272 | + * @param \WP_REST_Request $request |
|
273 | + * @return \WP_REST_Response|\WP_Error |
|
274 | + */ |
|
275 | + public static function handle_request_get_related(\WP_REST_Request $request) |
|
276 | + { |
|
277 | + $controller = new Read(); |
|
278 | + try { |
|
279 | + $matches = $controller->parse_route( |
|
280 | + $request->get_route(), |
|
281 | + '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
282 | + array('version', 'model', 'id', 'related_model') |
|
283 | + ); |
|
284 | + $controller->set_requested_version($matches['version']); |
|
285 | + $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
|
286 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
287 | + return $controller->send_response( |
|
288 | + new \WP_Error( |
|
289 | + 'endpoint_parsing_error', |
|
290 | + sprintf( |
|
291 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
292 | + 'event_espresso'), |
|
293 | + $main_model_name_singular |
|
294 | + ) |
|
295 | + ) |
|
296 | + ); |
|
297 | + } |
|
298 | + $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
|
299 | + //assume the related model name is plural and try to find the model's name |
|
300 | + $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
|
301 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
302 | + //so the word didn't singularize well. Maybe that's just because it's a singular word? |
|
303 | + $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
|
304 | + } |
|
305 | + if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
306 | + return $controller->send_response( |
|
307 | + new \WP_Error( |
|
308 | + 'endpoint_parsing_error', |
|
309 | + sprintf( |
|
310 | + __('There is no model for endpoint %s. Please contact event espresso support', |
|
311 | + 'event_espresso'), |
|
312 | + $related_model_name_singular |
|
313 | + ) |
|
314 | + ) |
|
315 | + ); |
|
316 | + } |
|
317 | + return $controller->send_response( |
|
318 | + $controller->get_entities_from_relation( |
|
319 | + $request->get_param('id'), |
|
320 | + $main_model->related_settings_for($related_model_name_singular), |
|
321 | + $request |
|
322 | + ) |
|
323 | + ); |
|
324 | + } catch (\Exception $e) { |
|
325 | + return $controller->send_response($e); |
|
326 | + } |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + |
|
331 | + /** |
|
332 | + * Gets a collection for the given model and filters |
|
333 | + * |
|
334 | + * @param \EEM_Base $model |
|
335 | + * @param \WP_REST_Request $request |
|
336 | + * @return array|\WP_Error |
|
337 | + */ |
|
338 | + public function get_entities_from_model($model, $request) |
|
339 | + { |
|
340 | + $query_params = $this->create_model_query_params($model, $request->get_params()); |
|
341 | + if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
342 | + $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
343 | + return new \WP_Error( |
|
344 | + sprintf('rest_%s_cannot_list', $model_name_plural), |
|
345 | + sprintf( |
|
346 | + __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
347 | + $model_name_plural, |
|
348 | + Capabilities::get_missing_permissions_string($model, $query_params['caps']) |
|
349 | + ), |
|
350 | + array('status' => 403) |
|
351 | + ); |
|
352 | + } |
|
353 | + if (! $request->get_header('no_rest_headers')) { |
|
354 | + $this->_set_headers_from_query_params($model, $query_params); |
|
355 | + } |
|
356 | + /** @type array $results */ |
|
357 | + $results = $model->get_all_wpdb_results($query_params); |
|
358 | + $nice_results = array(); |
|
359 | + foreach ($results as $result) { |
|
360 | + $nice_results[] = $this->create_entity_from_wpdb_result( |
|
361 | + $model, |
|
362 | + $result, |
|
363 | + $request |
|
364 | + ); |
|
365 | + } |
|
366 | + return $nice_results; |
|
367 | + } |
|
368 | + |
|
369 | + |
|
370 | + |
|
371 | + /** |
|
372 | + * @param array $primary_model_query_params query params for finding the item from which |
|
373 | + * relations will be based |
|
374 | + * @param \EE_Model_Relation_Base $relation |
|
375 | + * @param \WP_REST_Request $request |
|
376 | + * @return \WP_Error|array |
|
377 | + */ |
|
378 | + protected function _get_entities_from_relation($primary_model_query_params, $relation, $request) |
|
379 | + { |
|
380 | + $context = $this->validate_context($request->get_param('caps')); |
|
381 | + $model = $relation->get_this_model(); |
|
382 | + $related_model = $relation->get_other_model(); |
|
383 | + if (! isset($primary_model_query_params[0])) { |
|
384 | + $primary_model_query_params[0] = array(); |
|
385 | + } |
|
386 | + //check if they can access the 1st model object |
|
387 | + $primary_model_query_params = array( |
|
388 | + 0 => $primary_model_query_params[0], |
|
389 | + 'limit' => 1, |
|
390 | + ); |
|
391 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
392 | + $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($primary_model_query_params); |
|
393 | + } |
|
394 | + $restricted_query_params = $primary_model_query_params; |
|
395 | + $restricted_query_params['caps'] = $context; |
|
396 | + $this->_set_debug_info('main model query params', $restricted_query_params); |
|
397 | + $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); |
|
398 | + if ( |
|
399 | + ! ( |
|
400 | + Capabilities::current_user_has_partial_access_to($related_model, $context) |
|
401 | + && $model->exists($restricted_query_params) |
|
402 | + ) |
|
403 | + ) { |
|
404 | + if ($relation instanceof \EE_Belongs_To_Relation) { |
|
405 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
406 | + } else { |
|
407 | + $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); |
|
408 | + } |
|
409 | + return new \WP_Error( |
|
410 | + sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
411 | + sprintf( |
|
412 | + __('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
413 | + 'event_espresso'), |
|
414 | + $related_model_name_maybe_plural, |
|
415 | + $relation->get_this_model()->get_this_model_name(), |
|
416 | + implode( |
|
417 | + ',', |
|
418 | + array_keys( |
|
419 | + Capabilities::get_missing_permissions($related_model, $context) |
|
420 | + ) |
|
421 | + ) |
|
422 | + ), |
|
423 | + array('status' => 403) |
|
424 | + ); |
|
425 | + } |
|
426 | + $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); |
|
427 | + foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
428 | + $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
429 | + . '.' |
|
430 | + . $where_condition_key] = $where_condition_value; |
|
431 | + } |
|
432 | + $query_params['default_where_conditions'] = 'none'; |
|
433 | + $query_params['caps'] = $context; |
|
434 | + if (! $request->get_header('no_rest_headers')) { |
|
435 | + $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
|
436 | + } |
|
437 | + /** @type array $results */ |
|
438 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
439 | + $nice_results = array(); |
|
440 | + foreach ($results as $result) { |
|
441 | + $nice_result = $this->create_entity_from_wpdb_result( |
|
442 | + $relation->get_other_model(), |
|
443 | + $result, |
|
444 | + $request |
|
445 | + ); |
|
446 | + if ($relation instanceof \EE_HABTM_Relation) { |
|
447 | + //put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
448 | + //if there are conflicts we prefer the properties from the main model |
|
449 | + $join_model_result = $this->create_entity_from_wpdb_result( |
|
450 | + $relation->get_join_model(), |
|
451 | + $result, |
|
452 | + $request |
|
453 | + ); |
|
454 | + $joined_result = array_merge($nice_result, $join_model_result); |
|
455 | + //but keep the meta stuff from the main model |
|
456 | + if (isset($nice_result['meta'])) { |
|
457 | + $joined_result['meta'] = $nice_result['meta']; |
|
458 | + } |
|
459 | + $nice_result = $joined_result; |
|
460 | + } |
|
461 | + $nice_results[] = $nice_result; |
|
462 | + } |
|
463 | + if ($relation instanceof \EE_Belongs_To_Relation) { |
|
464 | + return array_shift($nice_results); |
|
465 | + } else { |
|
466 | + return $nice_results; |
|
467 | + } |
|
468 | + } |
|
469 | + |
|
470 | + |
|
471 | + |
|
472 | + /** |
|
473 | + * Gets the collection for given relation object |
|
474 | + * The same as Read::get_entities_from_model(), except if the relation |
|
475 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
476 | + * the join-model-object into the results |
|
477 | + * |
|
478 | + * @param string $id the ID of the thing we are fetching related stuff from |
|
479 | + * @param \EE_Model_Relation_Base $relation |
|
480 | + * @param \WP_REST_Request $request |
|
481 | + * @return array|\WP_Error |
|
482 | + * @throws \EE_Error |
|
483 | + */ |
|
484 | + public function get_entities_from_relation($id, $relation, $request) |
|
485 | + { |
|
486 | + if (! $relation->get_this_model()->has_primary_key_field()) { |
|
487 | + throw new \EE_Error( |
|
488 | + sprintf( |
|
489 | + __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
490 | + 'event_espresso'), |
|
491 | + $relation->get_this_model()->get_this_model_name() |
|
492 | + ) |
|
493 | + ); |
|
494 | + } |
|
495 | + return $this->_get_entities_from_relation( |
|
496 | + array( |
|
497 | + array( |
|
498 | + $relation->get_this_model()->primary_key_name() => $id, |
|
499 | + ), |
|
500 | + ), |
|
501 | + $relation, |
|
502 | + $request |
|
503 | + ); |
|
504 | + } |
|
505 | + |
|
506 | + |
|
507 | + |
|
508 | + /** |
|
509 | + * Sets the headers that are based on the model and query params, |
|
510 | + * like the total records. This should only be called on the original request |
|
511 | + * from the client, not on subsequent internal |
|
512 | + * |
|
513 | + * @param \EEM_Base $model |
|
514 | + * @param array $query_params |
|
515 | + * @return void |
|
516 | + */ |
|
517 | + protected function _set_headers_from_query_params($model, $query_params) |
|
518 | + { |
|
519 | + $this->_set_debug_info('model query params', $query_params); |
|
520 | + $this->_set_debug_info('missing caps', |
|
521 | + Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
|
522 | + //normally the limit to a 2-part array, where the 2nd item is the limit |
|
523 | + if (! isset($query_params['limit'])) { |
|
524 | + $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
525 | + } |
|
526 | + if (is_array($query_params['limit'])) { |
|
527 | + $limit_parts = $query_params['limit']; |
|
528 | + } else { |
|
529 | + $limit_parts = explode(',', $query_params['limit']); |
|
530 | + if (count($limit_parts) == 1) { |
|
531 | + $limit_parts = array(0, $limit_parts[0]); |
|
532 | + } |
|
533 | + } |
|
534 | + //remove the group by and having parts of the query, as those will |
|
535 | + //make the sql query return an array of values, instead of just a single value |
|
536 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
537 | + $count = $model->count($query_params, null, true); |
|
538 | + $pages = $count / $limit_parts[1]; |
|
539 | + $this->_set_response_header('Total', $count, false); |
|
540 | + $this->_set_response_header('PageSize', $limit_parts[1], false); |
|
541 | + $this->_set_response_header('TotalPages', ceil($pages), false); |
|
542 | + } |
|
543 | + |
|
544 | + |
|
545 | + |
|
546 | + /** |
|
547 | + * Changes database results into REST API entities |
|
548 | + * |
|
549 | + * @param \EEM_Base $model |
|
550 | + * @param array $db_row like results from $wpdb->get_results() |
|
551 | + * @param \WP_REST_Request $rest_request |
|
552 | + * @param string $deprecated no longer used |
|
553 | + * @return array ready for being converted into json for sending to client |
|
554 | + */ |
|
555 | + public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
|
556 | + { |
|
557 | + if (! $rest_request instanceof \WP_REST_Request) { |
|
558 | + //ok so this was called in the old style, where the 3rd arg was |
|
559 | + //$include, and the 4th arg was $context |
|
560 | + //now setup the request just to avoid fatal errors, although we won't be able |
|
561 | + //to truly make use of it because it's kinda devoid of info |
|
562 | + $rest_request = new \WP_REST_Request(); |
|
563 | + $rest_request->set_param('include', $rest_request); |
|
564 | + $rest_request->set_param('caps', $deprecated); |
|
565 | + } |
|
566 | + if ($rest_request->get_param('caps') == null) { |
|
567 | + $rest_request->set_param('caps', \EEM_Base::caps_read); |
|
568 | + } |
|
569 | + $entity_array = $this->_create_bare_entity_from_wpdb_results($model, $db_row); |
|
570 | + $entity_array = $this->_add_extra_fields($model, $db_row, $entity_array); |
|
571 | + $entity_array['_links'] = $this->_get_entity_links($model, $db_row, $entity_array); |
|
572 | + $entity_array['_calculated_fields'] = $this->_get_entity_calculations($model, $db_row, $rest_request); |
|
573 | + $entity_array = apply_filters('FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
574 | + $entity_array, $model, $rest_request->get_param('caps'), $rest_request, $this); |
|
575 | + $entity_array = $this->_include_requested_models($model, $rest_request, $entity_array, $db_row); |
|
576 | + $entity_array = apply_filters( |
|
577 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
578 | + $entity_array, |
|
579 | + $model, |
|
580 | + $rest_request->get_param('caps'), |
|
581 | + $rest_request, |
|
582 | + $this |
|
583 | + ); |
|
584 | + $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields( |
|
585 | + $entity_array, |
|
586 | + $model, |
|
587 | + $rest_request->get_param('caps'), |
|
588 | + $this->get_model_version_info(), |
|
589 | + $model->get_index_primary_key_string( |
|
590 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
591 | + ) |
|
592 | + ); |
|
593 | + $this->_set_debug_info( |
|
594 | + 'inaccessible fields', |
|
595 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
596 | + ); |
|
597 | + return apply_filters( |
|
598 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
599 | + $result_without_inaccessible_fields, |
|
600 | + $model, |
|
601 | + $rest_request->get_param('caps') |
|
602 | + ); |
|
603 | + } |
|
604 | + |
|
605 | + |
|
606 | + |
|
607 | + /** |
|
608 | + * Creates a REST entity array (JSON object we're going to return in the response, but |
|
609 | + * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
610 | + * from $wpdb->get_row( $sql, ARRAY_A) |
|
611 | + * |
|
612 | + * @param \EEM_Base $model |
|
613 | + * @param array $db_row |
|
614 | + * @return array entity mostly ready for converting to JSON and sending in the response |
|
615 | + */ |
|
616 | + protected function _create_bare_entity_from_wpdb_results(\EEM_Base $model, $db_row) |
|
617 | + { |
|
618 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
619 | + $result = array_intersect_key($result, |
|
620 | + $this->get_model_version_info()->fields_on_model_in_this_version($model)); |
|
621 | + foreach ($result as $field_name => $raw_field_value) { |
|
622 | + $field_obj = $model->field_settings_for($field_name); |
|
623 | + $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); |
|
624 | + if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { |
|
625 | + unset($result[$field_name]); |
|
626 | + } elseif ( |
|
627 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format()) |
|
628 | + ) { |
|
629 | + $result[$field_name] = array( |
|
630 | + 'raw' => $field_obj->prepare_for_get($field_value), |
|
631 | + 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
632 | + ); |
|
633 | + } elseif ( |
|
634 | + $this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format()) |
|
635 | + ) { |
|
636 | + $result[$field_name] = array( |
|
637 | + 'raw' => $field_obj->prepare_for_get($field_value), |
|
638 | + 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value), |
|
639 | + ); |
|
640 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
641 | + if ($field_value instanceof \DateTime) { |
|
642 | + $timezone = $field_value->getTimezone(); |
|
643 | + $field_value->setTimezone(new \DateTimeZone('UTC')); |
|
644 | + $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
645 | + $field_obj, |
|
646 | + $field_value, |
|
647 | + $this->get_model_version_info()->requested_version() |
|
648 | + ); |
|
649 | + $field_value->setTimezone($timezone); |
|
650 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
651 | + $field_obj, |
|
652 | + $field_value, |
|
653 | + $this->get_model_version_info()->requested_version() |
|
654 | + ); |
|
655 | + } |
|
656 | + } else { |
|
657 | + $result[$field_name] = Model_Data_Translator::prepare_field_value_for_json( |
|
658 | + $field_obj, |
|
659 | + $field_obj->prepare_for_get($field_value), |
|
660 | + $this->get_model_version_info()->requested_version() |
|
661 | + ); |
|
662 | + } |
|
663 | + } |
|
664 | + return $result; |
|
665 | + } |
|
666 | + |
|
667 | + |
|
668 | + |
|
669 | + /** |
|
670 | + * Adds a few extra fields to the entity response |
|
671 | + * |
|
672 | + * @param \EEM_Base $model |
|
673 | + * @param array $db_row |
|
674 | + * @param array $entity_array |
|
675 | + * @return array modified entity |
|
676 | + */ |
|
677 | + protected function _add_extra_fields(\EEM_Base $model, $db_row, $entity_array) |
|
678 | + { |
|
679 | + if ($model instanceof \EEM_CPT_Base) { |
|
680 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
681 | + } |
|
682 | + return $entity_array; |
|
683 | + } |
|
684 | + |
|
685 | + |
|
686 | + |
|
687 | + /** |
|
688 | + * Gets links we want to add to the response |
|
689 | + * |
|
690 | + * @global \WP_REST_Server $wp_rest_server |
|
691 | + * @param \EEM_Base $model |
|
692 | + * @param array $db_row |
|
693 | + * @param array $entity_array |
|
694 | + * @return array the _links item in the entity |
|
695 | + */ |
|
696 | + protected function _get_entity_links($model, $db_row, $entity_array) |
|
697 | + { |
|
698 | + //add basic links |
|
699 | + $links = array(); |
|
700 | + if ($model->has_primary_key_field()) { |
|
701 | + $links['self'] = array( |
|
702 | + array( |
|
703 | + 'href' => $this->get_versioned_link_to( |
|
704 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
705 | + . '/' |
|
706 | + . $entity_array[$model->primary_key_name()] |
|
707 | + ), |
|
708 | + ), |
|
709 | + ); |
|
710 | + } |
|
711 | + $links['collection'] = array( |
|
712 | + array( |
|
713 | + 'href' => $this->get_versioned_link_to( |
|
714 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
715 | + ), |
|
716 | + ), |
|
717 | + ); |
|
718 | + //add links to related models |
|
719 | + if ($model->has_primary_key_field()) { |
|
720 | + foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
|
721 | + $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
|
722 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
723 | + array( |
|
724 | + 'href' => $this->get_versioned_link_to( |
|
725 | + \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
726 | + . '/' |
|
727 | + . $entity_array[$model->primary_key_name()] |
|
728 | + . '/' |
|
729 | + . $related_model_part |
|
730 | + ), |
|
731 | + 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false, |
|
732 | + ), |
|
733 | + ); |
|
734 | + } |
|
735 | + } |
|
736 | + return $links; |
|
737 | + } |
|
738 | + |
|
739 | + |
|
740 | + |
|
741 | + /** |
|
742 | + * Adds the included models indicated in the request to the entity provided |
|
743 | + * |
|
744 | + * @param \EEM_Base $model |
|
745 | + * @param \WP_REST_Request $rest_request |
|
746 | + * @param array $entity_array |
|
747 | + * @param array $db_row |
|
748 | + * @return array the modified entity |
|
749 | + */ |
|
750 | + protected function _include_requested_models( |
|
751 | + \EEM_Base $model, |
|
752 | + \WP_REST_Request $rest_request, |
|
753 | + $entity_array, |
|
754 | + $db_row = array() |
|
755 | + ) { |
|
756 | + //if $db_row not included, hope the entity array has what we need |
|
757 | + if (! $db_row) { |
|
758 | + $db_row = $entity_array; |
|
759 | + } |
|
760 | + $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
|
761 | + $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
|
762 | + //if they passed in * or didn't specify any includes, return everything |
|
763 | + if (! in_array('*', $includes_for_this_model) |
|
764 | + && ! empty($includes_for_this_model) |
|
765 | + ) { |
|
766 | + if ($model->has_primary_key_field()) { |
|
767 | + //always include the primary key. ya just gotta know that at least |
|
768 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
769 | + } |
|
770 | + if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { |
|
771 | + $includes_for_this_model[] = '_calculated_fields'; |
|
772 | + } |
|
773 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
774 | + } |
|
775 | + $relation_settings = $this->get_model_version_info()->relation_settings($model); |
|
776 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
777 | + $related_fields_to_include = $this->explode_and_get_items_prefixed_with( |
|
778 | + $rest_request->get_param('include'), |
|
779 | + $relation_name |
|
780 | + ); |
|
781 | + $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with( |
|
782 | + $rest_request->get_param('calculate'), |
|
783 | + $relation_name |
|
784 | + ); |
|
785 | + //did they specify they wanted to include a related model, or |
|
786 | + //specific fields from a related model? |
|
787 | + //or did they specify to calculate a field from a related model? |
|
788 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
789 | + //if so, we should include at least some part of the related model |
|
790 | + $pretend_related_request = new \WP_REST_Request(); |
|
791 | + $pretend_related_request->set_query_params( |
|
792 | + array( |
|
793 | + 'caps' => $rest_request->get_param('caps'), |
|
794 | + 'include' => $related_fields_to_include, |
|
795 | + 'calculate' => $related_fields_to_calculate, |
|
796 | + ) |
|
797 | + ); |
|
798 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
799 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
800 | + $model->get_index_primary_key_string( |
|
801 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
802 | + ) |
|
803 | + ); |
|
804 | + $related_results = $this->_get_entities_from_relation( |
|
805 | + $primary_model_query_params, |
|
806 | + $relation_obj, |
|
807 | + $pretend_related_request |
|
808 | + ); |
|
809 | + $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results |
|
810 | + instanceof |
|
811 | + \WP_Error |
|
812 | + ? null |
|
813 | + : $related_results; |
|
814 | + } |
|
815 | + } |
|
816 | + return $entity_array; |
|
817 | + } |
|
818 | + |
|
819 | + |
|
820 | + |
|
821 | + /** |
|
822 | + * Returns a new array with all the names of models removed. Eg |
|
823 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
824 | + * |
|
825 | + * @param array $arr |
|
826 | + * @return array |
|
827 | + */ |
|
828 | + private function _remove_model_names_from_array($arr) |
|
829 | + { |
|
830 | + return array_diff($arr, array_keys(\EE_Registry::instance()->non_abstract_db_models)); |
|
831 | + } |
|
832 | + |
|
833 | + |
|
834 | + |
|
835 | + /** |
|
836 | + * Gets the calculated fields for the response |
|
837 | + * |
|
838 | + * @param \EEM_Base $model |
|
839 | + * @param array $wpdb_row |
|
840 | + * @param \WP_REST_Request $rest_request |
|
841 | + * @return \stdClass the _calculations item in the entity |
|
842 | + */ |
|
843 | + protected function _get_entity_calculations($model, $wpdb_row, $rest_request) |
|
844 | + { |
|
845 | + $calculated_fields = $this->explode_and_get_items_prefixed_with( |
|
846 | + $rest_request->get_param('calculate'), |
|
847 | + '' |
|
848 | + ); |
|
849 | + //note: setting calculate=* doesn't do anything |
|
850 | + $calculated_fields_to_return = new \stdClass(); |
|
851 | + foreach ($calculated_fields as $field_to_calculate) { |
|
852 | + try { |
|
853 | + $calculated_fields_to_return->$field_to_calculate = Model_Data_Translator::prepare_field_value_for_json( |
|
854 | + null, |
|
855 | + $this->_fields_calculator->retrieve_calculated_field_value( |
|
856 | + $model, |
|
857 | + $field_to_calculate, |
|
858 | + $wpdb_row, |
|
859 | + $rest_request, |
|
860 | + $this |
|
861 | + ), |
|
862 | + $this->get_model_version_info()->requested_version() |
|
863 | + ); |
|
864 | + } catch (Rest_Exception $e) { |
|
865 | + //if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
866 | + $this->_set_response_header( |
|
867 | + 'Notices-Field-Calculation-Errors[' |
|
868 | + . $e->get_string_code() |
|
869 | + . '][' |
|
870 | + . $model->get_this_model_name() |
|
871 | + . '][' |
|
872 | + . $field_to_calculate |
|
873 | + . ']', |
|
874 | + $e->getMessage(), |
|
875 | + true |
|
876 | + ); |
|
877 | + } |
|
878 | + } |
|
879 | + return $calculated_fields_to_return; |
|
880 | + } |
|
881 | + |
|
882 | + |
|
883 | + |
|
884 | + /** |
|
885 | + * Gets the full URL to the resource, taking the requested version into account |
|
886 | + * |
|
887 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
888 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
889 | + */ |
|
890 | + public function get_versioned_link_to($link_part_after_version_and_slash) |
|
891 | + { |
|
892 | + return rest_url( |
|
893 | + \EED_Core_Rest_Api::ee_api_namespace |
|
894 | + . $this->get_model_version_info()->requested_version() |
|
895 | + . '/' |
|
896 | + . $link_part_after_version_and_slash |
|
897 | + ); |
|
898 | + } |
|
899 | + |
|
900 | + |
|
901 | + |
|
902 | + /** |
|
903 | + * Gets the correct lowercase name for the relation in the API according |
|
904 | + * to the relation's type |
|
905 | + * |
|
906 | + * @param string $relation_name |
|
907 | + * @param \EE_Model_Relation_Base $relation_obj |
|
908 | + * @return string |
|
909 | + */ |
|
910 | + public static function get_related_entity_name($relation_name, $relation_obj) |
|
911 | + { |
|
912 | + if ($relation_obj instanceof \EE_Belongs_To_Relation) { |
|
913 | + return strtolower($relation_name); |
|
914 | + } else { |
|
915 | + return \EEH_Inflector::pluralize_and_lower($relation_name); |
|
916 | + } |
|
917 | + } |
|
918 | + |
|
919 | + |
|
920 | + |
|
921 | + /** |
|
922 | + * Gets the one model object with the specified id for the specified model |
|
923 | + * |
|
924 | + * @param \EEM_Base $model |
|
925 | + * @param \WP_REST_Request $request |
|
926 | + * @return array|\WP_Error |
|
927 | + */ |
|
928 | + public function get_entity_from_model($model, $request) |
|
929 | + { |
|
930 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
931 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
932 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
933 | + } |
|
934 | + $restricted_query_params = $query_params; |
|
935 | + $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
|
936 | + $this->_set_debug_info('model query params', $restricted_query_params); |
|
937 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
938 | + if (! empty ($model_rows)) { |
|
939 | + return $this->create_entity_from_wpdb_result( |
|
940 | + $model, |
|
941 | + array_shift($model_rows), |
|
942 | + $request); |
|
943 | + } else { |
|
944 | + //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
945 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
946 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
947 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
948 | + //you got shafted- it existed but we didn't want to tell you! |
|
949 | + return new \WP_Error( |
|
950 | + 'rest_user_cannot_read', |
|
951 | + sprintf( |
|
952 | + __('Sorry, you cannot read this %1$s. Missing permissions are: %2$s', 'event_espresso'), |
|
953 | + strtolower($model->get_this_model_name()), |
|
954 | + Capabilities::get_missing_permissions_string( |
|
955 | + $model, |
|
956 | + $this->validate_context($request->get_param('caps'))) |
|
957 | + ), |
|
958 | + array('status' => 403) |
|
959 | + ); |
|
960 | + } else { |
|
961 | + //it's not you. It just doesn't exist |
|
962 | + return new \WP_Error( |
|
963 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
964 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
965 | + array('status' => 404) |
|
966 | + ); |
|
967 | + } |
|
968 | + } |
|
969 | + } |
|
970 | + |
|
971 | + |
|
972 | + |
|
973 | + /** |
|
974 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
975 | + * version so just treat it as a default read |
|
976 | + * |
|
977 | + * @param string $context |
|
978 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
979 | + */ |
|
980 | + public function validate_context($context) |
|
981 | + { |
|
982 | + if (! $context) { |
|
983 | + $context = \EEM_Base::caps_read; |
|
984 | + } |
|
985 | + $valid_contexts = \EEM_Base::valid_cap_contexts(); |
|
986 | + if (in_array($context, $valid_contexts)) { |
|
987 | + return $context; |
|
988 | + } else { |
|
989 | + return \EEM_Base::caps_read; |
|
990 | + } |
|
991 | + } |
|
992 | + |
|
993 | + |
|
994 | + |
|
995 | + /** |
|
996 | + * Verifies the passed in value is an allowable default where conditions value. |
|
997 | + * |
|
998 | + * @param $default_query_params |
|
999 | + * @return string |
|
1000 | + */ |
|
1001 | + public function validate_default_query_params($default_query_params) |
|
1002 | + { |
|
1003 | + $valid_default_where_conditions_for_api_calls = array( |
|
1004 | + \EEM_Base::default_where_conditions_all, |
|
1005 | + \EEM_Base::default_where_conditions_minimum_all, |
|
1006 | + \EEM_Base::default_where_conditions_minimum_others, |
|
1007 | + ); |
|
1008 | + if (! $default_query_params) { |
|
1009 | + $default_query_params = \EEM_Base::default_where_conditions_all; |
|
1010 | + } |
|
1011 | + if ( |
|
1012 | + in_array( |
|
1013 | + $default_query_params, |
|
1014 | + $valid_default_where_conditions_for_api_calls, |
|
1015 | + true |
|
1016 | + ) |
|
1017 | + ) { |
|
1018 | + return $default_query_params; |
|
1019 | + } else { |
|
1020 | + return \EEM_Base::default_where_conditions_all; |
|
1021 | + } |
|
1022 | + } |
|
1023 | + |
|
1024 | + |
|
1025 | + |
|
1026 | + /** |
|
1027 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1028 | + * Note: right now the query parameter keys for fields (and related fields) |
|
1029 | + * can be left as-is, but it's quite possible this will change someday. |
|
1030 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1031 | + * |
|
1032 | + * @param \EEM_Base $model |
|
1033 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1034 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1035 | + * that absolutely no results should be returned |
|
1036 | + * @throws \EE_Error |
|
1037 | + */ |
|
1038 | + public function create_model_query_params($model, $query_parameters) |
|
1039 | + { |
|
1040 | + $model_query_params = array(); |
|
1041 | + if (isset($query_parameters['where'])) { |
|
1042 | + $model_query_params[0] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
1043 | + $query_parameters['where'], |
|
1044 | + $model, |
|
1045 | + $this->get_model_version_info()->requested_version() |
|
1046 | + ); |
|
1047 | + } |
|
1048 | + if (isset($query_parameters['order_by'])) { |
|
1049 | + $order_by = $query_parameters['order_by']; |
|
1050 | + } elseif (isset($query_parameters['orderby'])) { |
|
1051 | + $order_by = $query_parameters['orderby']; |
|
1052 | + } else { |
|
1053 | + $order_by = null; |
|
1054 | + } |
|
1055 | + if ($order_by !== null) { |
|
1056 | + if (is_array($order_by)) { |
|
1057 | + $order_by = Model_Data_Translator::prepare_field_names_in_array_keys_from_json($order_by); |
|
1058 | + } else { |
|
1059 | + //it's a single item |
|
1060 | + $order_by = Model_Data_Translator::prepare_field_name_from_json($order_by); |
|
1061 | + } |
|
1062 | + $model_query_params['order_by'] = $order_by; |
|
1063 | + } |
|
1064 | + if (isset($query_parameters['group_by'])) { |
|
1065 | + $group_by = $query_parameters['group_by']; |
|
1066 | + } elseif (isset($query_parameters['groupby'])) { |
|
1067 | + $group_by = $query_parameters['groupby']; |
|
1068 | + } else { |
|
1069 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1070 | + } |
|
1071 | + //make sure they're all real names |
|
1072 | + if (is_array($group_by)) { |
|
1073 | + $group_by = Model_Data_Translator::prepare_field_names_from_json($group_by); |
|
1074 | + } |
|
1075 | + if ($group_by !== null) { |
|
1076 | + $model_query_params['group_by'] = $group_by; |
|
1077 | + } |
|
1078 | + if (isset($query_parameters['having'])) { |
|
1079 | + $model_query_params['having'] = Model_Data_Translator::prepare_conditions_query_params_for_models( |
|
1080 | + $query_parameters['having'], |
|
1081 | + $model, |
|
1082 | + $this->get_model_version_info()->requested_version() |
|
1083 | + ); |
|
1084 | + } |
|
1085 | + if (isset($query_parameters['order'])) { |
|
1086 | + $model_query_params['order'] = $query_parameters['order']; |
|
1087 | + } |
|
1088 | + if (isset($query_parameters['mine'])) { |
|
1089 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1090 | + } |
|
1091 | + if (isset($query_parameters['limit'])) { |
|
1092 | + //limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1093 | + if (! is_array($query_parameters['limit'])) { |
|
1094 | + $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1095 | + } else { |
|
1096 | + $limit_array = $query_parameters['limit']; |
|
1097 | + } |
|
1098 | + $sanitized_limit = array(); |
|
1099 | + foreach ($limit_array as $key => $limit_part) { |
|
1100 | + if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1101 | + throw new \EE_Error( |
|
1102 | + sprintf( |
|
1103 | + __('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.', |
|
1104 | + 'event_espresso'), |
|
1105 | + wp_json_encode($query_parameters['limit']) |
|
1106 | + ) |
|
1107 | + ); |
|
1108 | + } |
|
1109 | + $sanitized_limit[] = (int)$limit_part; |
|
1110 | + } |
|
1111 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1112 | + } else { |
|
1113 | + $model_query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
|
1114 | + } |
|
1115 | + if (isset($query_parameters['caps'])) { |
|
1116 | + $model_query_params['caps'] = $this->validate_context($query_parameters['caps']); |
|
1117 | + } else { |
|
1118 | + $model_query_params['caps'] = \EEM_Base::caps_read; |
|
1119 | + } |
|
1120 | + if (isset($query_parameters['default_where_conditions'])) { |
|
1121 | + $model_query_params['default_where_conditions'] = $this->validate_default_query_params($query_parameters['default_where_conditions']); |
|
1122 | + } |
|
1123 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1124 | + } |
|
1125 | + |
|
1126 | + |
|
1127 | + |
|
1128 | + /** |
|
1129 | + * Changes the REST-style query params for use in the models |
|
1130 | + * |
|
1131 | + * @deprecated |
|
1132 | + * @param \EEM_Base $model |
|
1133 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1134 | + * @return array |
|
1135 | + */ |
|
1136 | + public function prepare_rest_query_params_key_for_models($model, $query_params) |
|
1137 | + { |
|
1138 | + $model_ready_query_params = array(); |
|
1139 | + foreach ($query_params as $key => $value) { |
|
1140 | + if (is_array($value)) { |
|
1141 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_key_for_models($model, $value); |
|
1142 | + } else { |
|
1143 | + $model_ready_query_params[$key] = $value; |
|
1144 | + } |
|
1145 | + } |
|
1146 | + return $model_ready_query_params; |
|
1147 | + } |
|
1148 | + |
|
1149 | + |
|
1150 | + |
|
1151 | + /** |
|
1152 | + * @deprecated |
|
1153 | + * @param $model |
|
1154 | + * @param $query_params |
|
1155 | + * @return array |
|
1156 | + */ |
|
1157 | + public function prepare_rest_query_params_values_for_models($model, $query_params) |
|
1158 | + { |
|
1159 | + $model_ready_query_params = array(); |
|
1160 | + foreach ($query_params as $key => $value) { |
|
1161 | + if (is_array($value)) { |
|
1162 | + $model_ready_query_params[$key] = $this->prepare_rest_query_params_values_for_models($model, $value); |
|
1163 | + } else { |
|
1164 | + $model_ready_query_params[$key] = $value; |
|
1165 | + } |
|
1166 | + } |
|
1167 | + return $model_ready_query_params; |
|
1168 | + } |
|
1169 | + |
|
1170 | + |
|
1171 | + |
|
1172 | + /** |
|
1173 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1174 | + * If no prefix is specified, returns items with no period. |
|
1175 | + * |
|
1176 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blaabla" or array('jibba', 'jabba' ) |
|
1177 | + * @param string $prefix "Event" or "foobar" |
|
1178 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1179 | + * we only return strings starting with that and a period; if no prefix was |
|
1180 | + * specified we return all items containing NO periods |
|
1181 | + */ |
|
1182 | + public function explode_and_get_items_prefixed_with($string_to_explode, $prefix) |
|
1183 | + { |
|
1184 | + if (is_string($string_to_explode)) { |
|
1185 | + $exploded_contents = explode(',', $string_to_explode); |
|
1186 | + } else if (is_array($string_to_explode)) { |
|
1187 | + $exploded_contents = $string_to_explode; |
|
1188 | + } else { |
|
1189 | + $exploded_contents = array(); |
|
1190 | + } |
|
1191 | + //if the string was empty, we want an empty array |
|
1192 | + $exploded_contents = array_filter($exploded_contents); |
|
1193 | + $contents_with_prefix = array(); |
|
1194 | + foreach ($exploded_contents as $item) { |
|
1195 | + $item = trim($item); |
|
1196 | + //if no prefix was provided, so we look for items with no "." in them |
|
1197 | + if (! $prefix) { |
|
1198 | + //does this item have a period? |
|
1199 | + if (strpos($item, '.') === false) { |
|
1200 | + //if not, then its what we're looking for |
|
1201 | + $contents_with_prefix[] = $item; |
|
1202 | + } |
|
1203 | + } else if (strpos($item, $prefix . '.') === 0) { |
|
1204 | + //this item has the prefix and a period, grab it |
|
1205 | + $contents_with_prefix[] = substr( |
|
1206 | + $item, |
|
1207 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1208 | + ); |
|
1209 | + } else if ($item === $prefix) { |
|
1210 | + //this item is JUST the prefix |
|
1211 | + //so let's grab everything after, which is a blank string |
|
1212 | + $contents_with_prefix[] = ''; |
|
1213 | + } |
|
1214 | + } |
|
1215 | + return $contents_with_prefix; |
|
1216 | + } |
|
1217 | + |
|
1218 | + |
|
1219 | + |
|
1220 | + /** |
|
1221 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1222 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1223 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1224 | + * array('*') (when you provided a model and a model of that kind was found). |
|
1225 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1226 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
1227 | + * name and then a period). |
|
1228 | + * @param string $include_string @see Read:handle_request_get_all |
|
1229 | + * @param string $model_name |
|
1230 | + * @return array of fields for this model. If $model_name is provided, then |
|
1231 | + * the fields for that model, with the model's name removed from each. |
|
1232 | + * If $include_string was blank or '*' returns an empty array |
|
1233 | + */ |
|
1234 | + public function extract_includes_for_this_model($include_string, $model_name = null) |
|
1235 | + { |
|
1236 | + if (is_array($include_string)) { |
|
1237 | + $include_string = implode(',', $include_string); |
|
1238 | + } |
|
1239 | + if ($include_string === '*' || $include_string === '') { |
|
1240 | + return array(); |
|
1241 | + } |
|
1242 | + $includes = explode(',', $include_string); |
|
1243 | + $extracted_fields_to_include = array(); |
|
1244 | + if ($model_name) { |
|
1245 | + foreach ($includes as $field_to_include) { |
|
1246 | + $field_to_include = trim($field_to_include); |
|
1247 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1248 | + //found the model name at the exact start |
|
1249 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1250 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
1251 | + } elseif ($field_to_include == $model_name) { |
|
1252 | + $extracted_fields_to_include[] = '*'; |
|
1253 | + } |
|
1254 | + } |
|
1255 | + } else { |
|
1256 | + //look for ones with no period |
|
1257 | + foreach ($includes as $field_to_include) { |
|
1258 | + $field_to_include = trim($field_to_include); |
|
1259 | + if ( |
|
1260 | + strpos($field_to_include, '.') === false |
|
1261 | + && ! $this->get_model_version_info()->is_model_name_in_this_version($field_to_include) |
|
1262 | + ) { |
|
1263 | + $extracted_fields_to_include[] = $field_to_include; |
|
1264 | + } |
|
1265 | + } |
|
1266 | + } |
|
1267 | + return $extracted_fields_to_include; |
|
1268 | + } |
|
1269 | 1269 | } |
1270 | 1270 | |
1271 | 1271 |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | use EventEspresso\core\entities\models\JsonModelSchema; |
9 | 9 | use EE_Datetime_Field; |
10 | 10 | |
11 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
11 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
12 | 12 | exit('No direct script access allowed'); |
13 | 13 | } |
14 | 14 | |
@@ -57,12 +57,12 @@ discard block |
||
57 | 57 | try { |
58 | 58 | $matches = $controller->parse_route( |
59 | 59 | $request->get_route(), |
60 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)~', |
|
60 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)~', |
|
61 | 61 | array('version', 'model') |
62 | 62 | ); |
63 | 63 | $controller->set_requested_version($matches['version']); |
64 | 64 | $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
65 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
65 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
66 | 66 | return $controller->send_response( |
67 | 67 | new \WP_Error( |
68 | 68 | 'endpoint_parsing_error', |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | $controller = new Read(); |
100 | 100 | try { |
101 | 101 | $controller->set_requested_version($version); |
102 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
102 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name)) { |
|
103 | 103 | return array(); |
104 | 104 | } |
105 | 105 | //get the model for this version |
@@ -193,9 +193,9 @@ discard block |
||
193 | 193 | protected function _maybe_add_extra_fields_to_schema($field_name, \EE_Model_Field_Base $field, array $schema) |
194 | 194 | { |
195 | 195 | if ($field instanceof EE_Datetime_Field) { |
196 | - $schema['properties'][$field_name . '_gmt'] = $field->getSchema(); |
|
196 | + $schema['properties'][$field_name.'_gmt'] = $field->getSchema(); |
|
197 | 197 | //modify the description |
198 | - $schema['properties'][$field_name . '_gmt']['description'] = sprintf( |
|
198 | + $schema['properties'][$field_name.'_gmt']['description'] = sprintf( |
|
199 | 199 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
200 | 200 | $field->get_nicename() |
201 | 201 | ); |
@@ -236,11 +236,11 @@ discard block |
||
236 | 236 | try { |
237 | 237 | $matches = $controller->parse_route( |
238 | 238 | $request->get_route(), |
239 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)~', |
|
239 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)~', |
|
240 | 240 | array('version', 'model', 'id')); |
241 | 241 | $controller->set_requested_version($matches['version']); |
242 | 242 | $model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
243 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
243 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($model_name_singular)) { |
|
244 | 244 | return $controller->send_response( |
245 | 245 | new \WP_Error( |
246 | 246 | 'endpoint_parsing_error', |
@@ -278,12 +278,12 @@ discard block |
||
278 | 278 | try { |
279 | 279 | $matches = $controller->parse_route( |
280 | 280 | $request->get_route(), |
281 | - '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', |
|
281 | + '~'.\EED_Core_Rest_Api::ee_api_namespace_for_regex.'(.*)/(.*)/(.*)~', |
|
282 | 282 | array('version', 'model', 'id', 'related_model') |
283 | 283 | ); |
284 | 284 | $controller->set_requested_version($matches['version']); |
285 | 285 | $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); |
286 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
286 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { |
|
287 | 287 | return $controller->send_response( |
288 | 288 | new \WP_Error( |
289 | 289 | 'endpoint_parsing_error', |
@@ -298,11 +298,11 @@ discard block |
||
298 | 298 | $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); |
299 | 299 | //assume the related model name is plural and try to find the model's name |
300 | 300 | $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); |
301 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
301 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
302 | 302 | //so the word didn't singularize well. Maybe that's just because it's a singular word? |
303 | 303 | $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); |
304 | 304 | } |
305 | - if (! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
305 | + if ( ! $controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { |
|
306 | 306 | return $controller->send_response( |
307 | 307 | new \WP_Error( |
308 | 308 | 'endpoint_parsing_error', |
@@ -338,7 +338,7 @@ discard block |
||
338 | 338 | public function get_entities_from_model($model, $request) |
339 | 339 | { |
340 | 340 | $query_params = $this->create_model_query_params($model, $request->get_params()); |
341 | - if (! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
341 | + if ( ! Capabilities::current_user_has_partial_access_to($model, $query_params['caps'])) { |
|
342 | 342 | $model_name_plural = \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
343 | 343 | return new \WP_Error( |
344 | 344 | sprintf('rest_%s_cannot_list', $model_name_plural), |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | array('status' => 403) |
351 | 351 | ); |
352 | 352 | } |
353 | - if (! $request->get_header('no_rest_headers')) { |
|
353 | + if ( ! $request->get_header('no_rest_headers')) { |
|
354 | 354 | $this->_set_headers_from_query_params($model, $query_params); |
355 | 355 | } |
356 | 356 | /** @type array $results */ |
@@ -380,7 +380,7 @@ discard block |
||
380 | 380 | $context = $this->validate_context($request->get_param('caps')); |
381 | 381 | $model = $relation->get_this_model(); |
382 | 382 | $related_model = $relation->get_other_model(); |
383 | - if (! isset($primary_model_query_params[0])) { |
|
383 | + if ( ! isset($primary_model_query_params[0])) { |
|
384 | 384 | $primary_model_query_params[0] = array(); |
385 | 385 | } |
386 | 386 | //check if they can access the 1st model object |
@@ -431,7 +431,7 @@ discard block |
||
431 | 431 | } |
432 | 432 | $query_params['default_where_conditions'] = 'none'; |
433 | 433 | $query_params['caps'] = $context; |
434 | - if (! $request->get_header('no_rest_headers')) { |
|
434 | + if ( ! $request->get_header('no_rest_headers')) { |
|
435 | 435 | $this->_set_headers_from_query_params($relation->get_other_model(), $query_params); |
436 | 436 | } |
437 | 437 | /** @type array $results */ |
@@ -483,7 +483,7 @@ discard block |
||
483 | 483 | */ |
484 | 484 | public function get_entities_from_relation($id, $relation, $request) |
485 | 485 | { |
486 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
486 | + if ( ! $relation->get_this_model()->has_primary_key_field()) { |
|
487 | 487 | throw new \EE_Error( |
488 | 488 | sprintf( |
489 | 489 | __('Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
@@ -520,7 +520,7 @@ discard block |
||
520 | 520 | $this->_set_debug_info('missing caps', |
521 | 521 | Capabilities::get_missing_permissions_string($model, $query_params['caps'])); |
522 | 522 | //normally the limit to a 2-part array, where the 2nd item is the limit |
523 | - if (! isset($query_params['limit'])) { |
|
523 | + if ( ! isset($query_params['limit'])) { |
|
524 | 524 | $query_params['limit'] = \EED_Core_Rest_Api::get_default_query_limit(); |
525 | 525 | } |
526 | 526 | if (is_array($query_params['limit'])) { |
@@ -554,7 +554,7 @@ discard block |
||
554 | 554 | */ |
555 | 555 | public function create_entity_from_wpdb_result($model, $db_row, $rest_request, $deprecated = null) |
556 | 556 | { |
557 | - if (! $rest_request instanceof \WP_REST_Request) { |
|
557 | + if ( ! $rest_request instanceof \WP_REST_Request) { |
|
558 | 558 | //ok so this was called in the old style, where the 3rd arg was |
559 | 559 | //$include, and the 4th arg was $context |
560 | 560 | //now setup the request just to avoid fatal errors, although we won't be able |
@@ -641,7 +641,7 @@ discard block |
||
641 | 641 | if ($field_value instanceof \DateTime) { |
642 | 642 | $timezone = $field_value->getTimezone(); |
643 | 643 | $field_value->setTimezone(new \DateTimeZone('UTC')); |
644 | - $result[$field_name . '_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
644 | + $result[$field_name.'_gmt'] = Model_Data_Translator::prepare_field_value_for_json( |
|
645 | 645 | $field_obj, |
646 | 646 | $field_value, |
647 | 647 | $this->get_model_version_info()->requested_version() |
@@ -719,7 +719,7 @@ discard block |
||
719 | 719 | if ($model->has_primary_key_field()) { |
720 | 720 | foreach ($this->get_model_version_info()->relation_settings($model) as $relation_name => $relation_obj) { |
721 | 721 | $related_model_part = Read::get_related_entity_name($relation_name, $relation_obj); |
722 | - $links[\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array( |
|
722 | + $links[\EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
723 | 723 | array( |
724 | 724 | 'href' => $this->get_versioned_link_to( |
725 | 725 | \EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
@@ -754,13 +754,13 @@ discard block |
||
754 | 754 | $db_row = array() |
755 | 755 | ) { |
756 | 756 | //if $db_row not included, hope the entity array has what we need |
757 | - if (! $db_row) { |
|
757 | + if ( ! $db_row) { |
|
758 | 758 | $db_row = $entity_array; |
759 | 759 | } |
760 | 760 | $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); |
761 | 761 | $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); |
762 | 762 | //if they passed in * or didn't specify any includes, return everything |
763 | - if (! in_array('*', $includes_for_this_model) |
|
763 | + if ( ! in_array('*', $includes_for_this_model) |
|
764 | 764 | && ! empty($includes_for_this_model) |
765 | 765 | ) { |
766 | 766 | if ($model->has_primary_key_field()) { |
@@ -935,7 +935,7 @@ discard block |
||
935 | 935 | $restricted_query_params['caps'] = $this->validate_context($request->get_param('caps')); |
936 | 936 | $this->_set_debug_info('model query params', $restricted_query_params); |
937 | 937 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
938 | - if (! empty ($model_rows)) { |
|
938 | + if ( ! empty ($model_rows)) { |
|
939 | 939 | return $this->create_entity_from_wpdb_result( |
940 | 940 | $model, |
941 | 941 | array_shift($model_rows), |
@@ -944,7 +944,7 @@ discard block |
||
944 | 944 | //ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
945 | 945 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
946 | 946 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
947 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
947 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
948 | 948 | //you got shafted- it existed but we didn't want to tell you! |
949 | 949 | return new \WP_Error( |
950 | 950 | 'rest_user_cannot_read', |
@@ -979,7 +979,7 @@ discard block |
||
979 | 979 | */ |
980 | 980 | public function validate_context($context) |
981 | 981 | { |
982 | - if (! $context) { |
|
982 | + if ( ! $context) { |
|
983 | 983 | $context = \EEM_Base::caps_read; |
984 | 984 | } |
985 | 985 | $valid_contexts = \EEM_Base::valid_cap_contexts(); |
@@ -1005,7 +1005,7 @@ discard block |
||
1005 | 1005 | \EEM_Base::default_where_conditions_minimum_all, |
1006 | 1006 | \EEM_Base::default_where_conditions_minimum_others, |
1007 | 1007 | ); |
1008 | - if (! $default_query_params) { |
|
1008 | + if ( ! $default_query_params) { |
|
1009 | 1009 | $default_query_params = \EEM_Base::default_where_conditions_all; |
1010 | 1010 | } |
1011 | 1011 | if ( |
@@ -1090,14 +1090,14 @@ discard block |
||
1090 | 1090 | } |
1091 | 1091 | if (isset($query_parameters['limit'])) { |
1092 | 1092 | //limit should be either a string like '23' or '23,43', or an array with two items in it |
1093 | - if (! is_array($query_parameters['limit'])) { |
|
1094 | - $limit_array = explode(',', (string)$query_parameters['limit']); |
|
1093 | + if ( ! is_array($query_parameters['limit'])) { |
|
1094 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1095 | 1095 | } else { |
1096 | 1096 | $limit_array = $query_parameters['limit']; |
1097 | 1097 | } |
1098 | 1098 | $sanitized_limit = array(); |
1099 | 1099 | foreach ($limit_array as $key => $limit_part) { |
1100 | - if ($this->_debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1100 | + if ($this->_debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1101 | 1101 | throw new \EE_Error( |
1102 | 1102 | sprintf( |
1103 | 1103 | __('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.', |
@@ -1106,7 +1106,7 @@ discard block |
||
1106 | 1106 | ) |
1107 | 1107 | ); |
1108 | 1108 | } |
1109 | - $sanitized_limit[] = (int)$limit_part; |
|
1109 | + $sanitized_limit[] = (int) $limit_part; |
|
1110 | 1110 | } |
1111 | 1111 | $model_query_params['limit'] = implode(',', $sanitized_limit); |
1112 | 1112 | } else { |
@@ -1194,17 +1194,17 @@ discard block |
||
1194 | 1194 | foreach ($exploded_contents as $item) { |
1195 | 1195 | $item = trim($item); |
1196 | 1196 | //if no prefix was provided, so we look for items with no "." in them |
1197 | - if (! $prefix) { |
|
1197 | + if ( ! $prefix) { |
|
1198 | 1198 | //does this item have a period? |
1199 | 1199 | if (strpos($item, '.') === false) { |
1200 | 1200 | //if not, then its what we're looking for |
1201 | 1201 | $contents_with_prefix[] = $item; |
1202 | 1202 | } |
1203 | - } else if (strpos($item, $prefix . '.') === 0) { |
|
1203 | + } else if (strpos($item, $prefix.'.') === 0) { |
|
1204 | 1204 | //this item has the prefix and a period, grab it |
1205 | 1205 | $contents_with_prefix[] = substr( |
1206 | 1206 | $item, |
1207 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1207 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
1208 | 1208 | ); |
1209 | 1209 | } else if ($item === $prefix) { |
1210 | 1210 | //this item is JUST the prefix |
@@ -1244,9 +1244,9 @@ discard block |
||
1244 | 1244 | if ($model_name) { |
1245 | 1245 | foreach ($includes as $field_to_include) { |
1246 | 1246 | $field_to_include = trim($field_to_include); |
1247 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1247 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
1248 | 1248 | //found the model name at the exact start |
1249 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1249 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
1250 | 1250 | $extracted_fields_to_include[] = $field_sans_model_name; |
1251 | 1251 | } elseif ($field_to_include == $model_name) { |
1252 | 1252 | $extracted_fields_to_include[] = '*'; |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace EventEspresso\core\libraries\rest_api\controllers\config; |
3 | 3 | |
4 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
5 | 5 | exit('No direct script access allowed'); |
6 | 6 | } |
7 | 7 | |
@@ -80,8 +80,8 @@ discard block |
||
80 | 80 | ), |
81 | 81 | 'default_currency' => \EE_Config::instance()->currency, |
82 | 82 | 'authentication' => array( |
83 | - 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
84 | - 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
83 | + 'received_basic_auth_data' => (bool) $wp_json_basic_auth_received_data, |
|
84 | + 'insecure_usage_of_basic_auth' => (bool) $insecure_usage_of_basic_auth, |
|
85 | 85 | 'warning' => $warning |
86 | 86 | ) |
87 | 87 | ); |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | namespace EventEspresso\core\libraries\rest_api\controllers\config; |
3 | 3 | |
4 | 4 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
5 | - exit('No direct script access allowed'); |
|
5 | + exit('No direct script access allowed'); |
|
6 | 6 | } |
7 | 7 | |
8 | 8 | |
@@ -18,74 +18,74 @@ discard block |
||
18 | 18 | class Read |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @param \WP_REST_Request $request |
|
23 | - * @return \EE_Config|\WP_Error |
|
24 | - */ |
|
25 | - public static function handle_request(\WP_REST_Request $request) |
|
26 | - { |
|
27 | - $cap = \EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
28 | - if (\EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
29 | - return \EE_Config::instance(); |
|
30 | - } else { |
|
31 | - return new \WP_Error( |
|
32 | - 'cannot_read_config', |
|
33 | - sprintf( |
|
34 | - __( |
|
35 | - 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
36 | - 'event_espresso' |
|
37 | - ), |
|
38 | - $cap |
|
39 | - ), |
|
40 | - array('status' => 403) |
|
41 | - ); |
|
42 | - } |
|
43 | - } |
|
21 | + /** |
|
22 | + * @param \WP_REST_Request $request |
|
23 | + * @return \EE_Config|\WP_Error |
|
24 | + */ |
|
25 | + public static function handle_request(\WP_REST_Request $request) |
|
26 | + { |
|
27 | + $cap = \EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
28 | + if (\EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
29 | + return \EE_Config::instance(); |
|
30 | + } else { |
|
31 | + return new \WP_Error( |
|
32 | + 'cannot_read_config', |
|
33 | + sprintf( |
|
34 | + __( |
|
35 | + 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
36 | + 'event_espresso' |
|
37 | + ), |
|
38 | + $cap |
|
39 | + ), |
|
40 | + array('status' => 403) |
|
41 | + ); |
|
42 | + } |
|
43 | + } |
|
44 | 44 | |
45 | 45 | |
46 | 46 | |
47 | - /** |
|
48 | - * Handles the request for public site info |
|
49 | - * |
|
50 | - * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
51 | - * current user could be authenticated using basic auth data |
|
52 | - * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
53 | - * basic auth data was somehow received |
|
54 | - * @param \WP_REST_Request $request |
|
55 | - * @return \EE_Config|\WP_Error |
|
56 | - */ |
|
57 | - public static function handle_request_site_info(\WP_REST_Request $request) |
|
58 | - { |
|
59 | - global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
60 | - $insecure_usage_of_basic_auth = apply_filters( |
|
61 | - 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
62 | - $wp_json_basic_auth_success && ! is_ssl(), |
|
63 | - $request |
|
64 | - ); |
|
65 | - if ($insecure_usage_of_basic_auth) { |
|
66 | - $warning = sprintf( |
|
67 | - esc_html__('Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
68 | - 'event_espresso'), |
|
69 | - '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
70 | - '</a>' |
|
71 | - ); |
|
72 | - } else { |
|
73 | - $warning = ''; |
|
74 | - } |
|
75 | - return array( |
|
76 | - 'default_timezone' => array( |
|
77 | - 'pretty' => \EEH_DTT_Helper::get_timezone_string_for_display(), |
|
78 | - 'string' => get_option('timezone_string'), |
|
79 | - 'offset' => \EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
80 | - ), |
|
81 | - 'default_currency' => \EE_Config::instance()->currency, |
|
82 | - 'authentication' => array( |
|
83 | - 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
84 | - 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
85 | - 'warning' => $warning |
|
86 | - ) |
|
87 | - ); |
|
88 | - } |
|
47 | + /** |
|
48 | + * Handles the request for public site info |
|
49 | + * |
|
50 | + * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
51 | + * current user could be authenticated using basic auth data |
|
52 | + * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
53 | + * basic auth data was somehow received |
|
54 | + * @param \WP_REST_Request $request |
|
55 | + * @return \EE_Config|\WP_Error |
|
56 | + */ |
|
57 | + public static function handle_request_site_info(\WP_REST_Request $request) |
|
58 | + { |
|
59 | + global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
60 | + $insecure_usage_of_basic_auth = apply_filters( |
|
61 | + 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
62 | + $wp_json_basic_auth_success && ! is_ssl(), |
|
63 | + $request |
|
64 | + ); |
|
65 | + if ($insecure_usage_of_basic_auth) { |
|
66 | + $warning = sprintf( |
|
67 | + esc_html__('Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
68 | + 'event_espresso'), |
|
69 | + '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
70 | + '</a>' |
|
71 | + ); |
|
72 | + } else { |
|
73 | + $warning = ''; |
|
74 | + } |
|
75 | + return array( |
|
76 | + 'default_timezone' => array( |
|
77 | + 'pretty' => \EEH_DTT_Helper::get_timezone_string_for_display(), |
|
78 | + 'string' => get_option('timezone_string'), |
|
79 | + 'offset' => \EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
80 | + ), |
|
81 | + 'default_currency' => \EE_Config::instance()->currency, |
|
82 | + 'authentication' => array( |
|
83 | + 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
84 | + 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
85 | + 'warning' => $warning |
|
86 | + ) |
|
87 | + ); |
|
88 | + } |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | // End of file Read.php |
92 | 92 | \ No newline at end of file |
@@ -7,60 +7,60 @@ |
||
7 | 7 | class Changes_In_4_8_33 extends Changes_In_Base |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * Adds hooks so requests to 4.8.29 don't have the checkin endpoints |
|
12 | - */ |
|
13 | - public function set_hooks() |
|
14 | - { |
|
15 | - //set a hook to remove the checkout/checkout endpoints if the request |
|
16 | - //is for lower than 4.8.33 |
|
17 | - add_filter( |
|
18 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
19 | - array($this, 'remove_checkin_routes_earlier_than_4_8_33'), |
|
20 | - 10, |
|
21 | - 2 |
|
22 | - ); |
|
23 | - add_filter( |
|
24 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
25 | - array($this, 'dont_add_headers_from_ee_notices'), |
|
26 | - 10, |
|
27 | - 2 |
|
28 | - ); |
|
29 | - } |
|
10 | + /** |
|
11 | + * Adds hooks so requests to 4.8.29 don't have the checkin endpoints |
|
12 | + */ |
|
13 | + public function set_hooks() |
|
14 | + { |
|
15 | + //set a hook to remove the checkout/checkout endpoints if the request |
|
16 | + //is for lower than 4.8.33 |
|
17 | + add_filter( |
|
18 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
19 | + array($this, 'remove_checkin_routes_earlier_than_4_8_33'), |
|
20 | + 10, |
|
21 | + 2 |
|
22 | + ); |
|
23 | + add_filter( |
|
24 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
25 | + array($this, 'dont_add_headers_from_ee_notices'), |
|
26 | + 10, |
|
27 | + 2 |
|
28 | + ); |
|
29 | + } |
|
30 | 30 | |
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * Removes the checkin and checkout endpoints from the index for requests |
|
35 | - * to api versions lowers than 4.8.33 |
|
36 | - * |
|
37 | - * @param array $routes_on_this_version |
|
38 | - * @param string $version |
|
39 | - * @return array like $routes_on_this_version |
|
40 | - */ |
|
41 | - public function remove_checkin_routes_earlier_than_4_8_33($routes_on_this_version, $version) |
|
42 | - { |
|
43 | - if ($this->applies_to_version($version)) { |
|
44 | - unset($routes_on_this_version['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)']); |
|
45 | - } |
|
46 | - return $routes_on_this_version; |
|
47 | - } |
|
33 | + /** |
|
34 | + * Removes the checkin and checkout endpoints from the index for requests |
|
35 | + * to api versions lowers than 4.8.33 |
|
36 | + * |
|
37 | + * @param array $routes_on_this_version |
|
38 | + * @param string $version |
|
39 | + * @return array like $routes_on_this_version |
|
40 | + */ |
|
41 | + public function remove_checkin_routes_earlier_than_4_8_33($routes_on_this_version, $version) |
|
42 | + { |
|
43 | + if ($this->applies_to_version($version)) { |
|
44 | + unset($routes_on_this_version['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)']); |
|
45 | + } |
|
46 | + return $routes_on_this_version; |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * We just added headers for notices in this version |
|
53 | - * |
|
54 | - * @param array $headers_from_ee_notices |
|
55 | - * @param string $requested_version |
|
56 | - * @return array |
|
57 | - */ |
|
58 | - public function dont_add_headers_from_ee_notices($headers_from_ee_notices, $requested_version) |
|
59 | - { |
|
60 | - if ($this->applies_to_version($requested_version)) { |
|
61 | - return array(); |
|
62 | - } |
|
63 | - return $headers_from_ee_notices; |
|
64 | - } |
|
51 | + /** |
|
52 | + * We just added headers for notices in this version |
|
53 | + * |
|
54 | + * @param array $headers_from_ee_notices |
|
55 | + * @param string $requested_version |
|
56 | + * @return array |
|
57 | + */ |
|
58 | + public function dont_add_headers_from_ee_notices($headers_from_ee_notices, $requested_version) |
|
59 | + { |
|
60 | + if ($this->applies_to_version($requested_version)) { |
|
61 | + return array(); |
|
62 | + } |
|
63 | + return $headers_from_ee_notices; |
|
64 | + } |
|
65 | 65 | } |
66 | 66 |
@@ -22,15 +22,15 @@ discard block |
||
22 | 22 | */ |
23 | 23 | class EED_Ticket_Selector extends EED_Module { |
24 | 24 | |
25 | - /** |
|
26 | - * @var EventEspresso\modules\ticket_selector\DisplayTicketSelector $ticket_selector |
|
27 | - */ |
|
28 | - private static $ticket_selector; |
|
25 | + /** |
|
26 | + * @var EventEspresso\modules\ticket_selector\DisplayTicketSelector $ticket_selector |
|
27 | + */ |
|
28 | + private static $ticket_selector; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @var EventEspresso\modules\ticket_selector\TicketSelectorIframeEmbedButton $iframe_embed_button |
|
32 | - */ |
|
33 | - private static $iframe_embed_button; |
|
30 | + /** |
|
31 | + * @var EventEspresso\modules\ticket_selector\TicketSelectorIframeEmbedButton $iframe_embed_button |
|
32 | + */ |
|
33 | + private static $iframe_embed_button; |
|
34 | 34 | |
35 | 35 | |
36 | 36 | |
@@ -61,13 +61,13 @@ discard block |
||
61 | 61 | // routing |
62 | 62 | EE_Config::register_route( 'iframe', 'EED_Ticket_Selector', 'ticket_selector_iframe', 'ticket_selector' ); |
63 | 63 | EE_Config::register_route( 'process_ticket_selections', 'EED_Ticket_Selector', 'process_ticket_selections' ); |
64 | - EE_Config::register_route('cancel_ticket_selections', 'EED_Ticket_Selector', 'cancel_ticket_selections'); |
|
65 | - add_action( 'wp_loaded', array( 'EED_Ticket_Selector', 'set_definitions' ), 2 ); |
|
64 | + EE_Config::register_route('cancel_ticket_selections', 'EED_Ticket_Selector', 'cancel_ticket_selections'); |
|
65 | + add_action( 'wp_loaded', array( 'EED_Ticket_Selector', 'set_definitions' ), 2 ); |
|
66 | 66 | add_action( 'AHEE_event_details_header_bottom', array( 'EED_Ticket_Selector', 'display_ticket_selector' ), 10, 1 ); |
67 | - add_action( 'wp_enqueue_scripts', array( 'EED_Ticket_Selector', 'translate_js_strings' ), 0 ); |
|
68 | - add_action( 'wp_enqueue_scripts', array( 'EED_Ticket_Selector', 'load_tckt_slctr_assets' ), 10 ); |
|
69 | - EED_Ticket_Selector::loadIframeAssets(); |
|
70 | - } |
|
67 | + add_action( 'wp_enqueue_scripts', array( 'EED_Ticket_Selector', 'translate_js_strings' ), 0 ); |
|
68 | + add_action( 'wp_enqueue_scripts', array( 'EED_Ticket_Selector', 'load_tckt_slctr_assets' ), 10 ); |
|
69 | + EED_Ticket_Selector::loadIframeAssets(); |
|
70 | + } |
|
71 | 71 | |
72 | 72 | |
73 | 73 | |
@@ -86,16 +86,16 @@ discard block |
||
86 | 86 | 10 |
87 | 87 | ); |
88 | 88 | |
89 | - /** |
|
90 | - * Make sure assets for the ticket selector are loaded on the espresso registrations route so admin side |
|
91 | - * registrations work. |
|
92 | - */ |
|
89 | + /** |
|
90 | + * Make sure assets for the ticket selector are loaded on the espresso registrations route so admin side |
|
91 | + * registrations work. |
|
92 | + */ |
|
93 | 93 | add_action( |
94 | - 'FHEE__EE_Admin_Page___load_page_dependencies__after_load__espresso_registrations__new_registration', |
|
95 | - array('EED_Ticket_Selector', 'set_definitions'), |
|
96 | - 10 |
|
97 | - ); |
|
98 | - } |
|
94 | + 'FHEE__EE_Admin_Page___load_page_dependencies__after_load__espresso_registrations__new_registration', |
|
95 | + array('EED_Ticket_Selector', 'set_definitions'), |
|
96 | + 10 |
|
97 | + ); |
|
98 | + } |
|
99 | 99 | |
100 | 100 | |
101 | 101 | |
@@ -111,23 +111,23 @@ discard block |
||
111 | 111 | |
112 | 112 | //if config is not set, initialize |
113 | 113 | if ( ! EE_Registry::instance()->CFG->template_settings->EED_Ticket_Selector instanceof EE_Ticket_Selector_Config ) { |
114 | - \EED_Ticket_Selector::instance()->set_config(); |
|
115 | - \EE_Registry::instance()->CFG->template_settings->EED_Ticket_Selector = \EED_Ticket_Selector::instance()->config(); |
|
114 | + \EED_Ticket_Selector::instance()->set_config(); |
|
115 | + \EE_Registry::instance()->CFG->template_settings->EED_Ticket_Selector = \EED_Ticket_Selector::instance()->config(); |
|
116 | 116 | } |
117 | 117 | } |
118 | 118 | |
119 | 119 | |
120 | 120 | |
121 | 121 | /** |
122 | - * @return \EventEspresso\modules\ticket_selector\DisplayTicketSelector |
|
123 | - */ |
|
124 | - public static function ticketSelector() |
|
125 | - { |
|
126 | - if ( ! EED_Ticket_Selector::$ticket_selector instanceof DisplayTicketSelector) { |
|
127 | - EED_Ticket_Selector::$ticket_selector = new DisplayTicketSelector(); |
|
128 | - } |
|
129 | - return EED_Ticket_Selector::$ticket_selector; |
|
130 | - } |
|
122 | + * @return \EventEspresso\modules\ticket_selector\DisplayTicketSelector |
|
123 | + */ |
|
124 | + public static function ticketSelector() |
|
125 | + { |
|
126 | + if ( ! EED_Ticket_Selector::$ticket_selector instanceof DisplayTicketSelector) { |
|
127 | + EED_Ticket_Selector::$ticket_selector = new DisplayTicketSelector(); |
|
128 | + } |
|
129 | + return EED_Ticket_Selector::$ticket_selector; |
|
130 | + } |
|
131 | 131 | |
132 | 132 | |
133 | 133 | /** |
@@ -180,15 +180,15 @@ discard block |
||
180 | 180 | |
181 | 181 | |
182 | 182 | |
183 | - /** |
|
184 | - * creates buttons for selecting number of attendees for an event |
|
185 | - * |
|
186 | - * @access public |
|
187 | - * @param WP_Post|int $event |
|
188 | - * @param bool $view_details |
|
189 | - * @return string |
|
190 | - * @throws \EE_Error |
|
191 | - */ |
|
183 | + /** |
|
184 | + * creates buttons for selecting number of attendees for an event |
|
185 | + * |
|
186 | + * @access public |
|
187 | + * @param WP_Post|int $event |
|
188 | + * @param bool $view_details |
|
189 | + * @return string |
|
190 | + * @throws \EE_Error |
|
191 | + */ |
|
192 | 192 | public static function display_ticket_selector( $event = NULL, $view_details = FALSE ) { |
193 | 193 | return EED_Ticket_Selector::ticketSelector()->display( $event, $view_details ); |
194 | 194 | } |
@@ -209,244 +209,244 @@ discard block |
||
209 | 209 | |
210 | 210 | |
211 | 211 | |
212 | - /** |
|
213 | - * cancel_ticket_selections |
|
214 | - * |
|
215 | - * @access public |
|
216 | - * @return string |
|
217 | - */ |
|
218 | - public static function cancel_ticket_selections() |
|
219 | - { |
|
220 | - $form = new ProcessTicketSelector(); |
|
221 | - return $form->cancelTicketSelections(); |
|
222 | - } |
|
212 | + /** |
|
213 | + * cancel_ticket_selections |
|
214 | + * |
|
215 | + * @access public |
|
216 | + * @return string |
|
217 | + */ |
|
218 | + public static function cancel_ticket_selections() |
|
219 | + { |
|
220 | + $form = new ProcessTicketSelector(); |
|
221 | + return $form->cancelTicketSelections(); |
|
222 | + } |
|
223 | 223 | |
224 | 224 | |
225 | 225 | |
226 | 226 | /** |
227 | - * @return void |
|
228 | - */ |
|
227 | + * @return void |
|
228 | + */ |
|
229 | 229 | public static function translate_js_strings() { |
230 | - EE_Registry::$i18n_js_strings['please_select_date_filter_notice'] = esc_html__( |
|
231 | - 'please select a datetime', 'event_espresso' |
|
232 | - ); |
|
233 | - } |
|
230 | + EE_Registry::$i18n_js_strings['please_select_date_filter_notice'] = esc_html__( |
|
231 | + 'please select a datetime', 'event_espresso' |
|
232 | + ); |
|
233 | + } |
|
234 | 234 | |
235 | 235 | |
236 | 236 | |
237 | 237 | /** |
238 | - * load js |
|
239 | - * |
|
240 | - * @access public |
|
241 | - * @return void |
|
242 | - */ |
|
238 | + * load js |
|
239 | + * |
|
240 | + * @access public |
|
241 | + * @return void |
|
242 | + */ |
|
243 | 243 | public static function load_tckt_slctr_assets() { |
244 | 244 | if ( apply_filters( 'FHEE__EED_Ticket_Selector__load_tckt_slctr_assets', FALSE ) ) { |
245 | - // add some style |
|
246 | - wp_register_style( |
|
247 | - 'ticket_selector', |
|
248 | - TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.css', |
|
249 | - array(), |
|
250 | - EVENT_ESPRESSO_VERSION |
|
251 | - ); |
|
252 | - wp_enqueue_style('ticket_selector'); |
|
253 | - // make it dance |
|
254 | - wp_register_script( |
|
255 | - 'ticket_selector', |
|
256 | - TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.js', |
|
257 | - array('espresso_core'), |
|
258 | - EVENT_ESPRESSO_VERSION, |
|
259 | - TRUE |
|
260 | - ); |
|
245 | + // add some style |
|
246 | + wp_register_style( |
|
247 | + 'ticket_selector', |
|
248 | + TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.css', |
|
249 | + array(), |
|
250 | + EVENT_ESPRESSO_VERSION |
|
251 | + ); |
|
252 | + wp_enqueue_style('ticket_selector'); |
|
253 | + // make it dance |
|
254 | + wp_register_script( |
|
255 | + 'ticket_selector', |
|
256 | + TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.js', |
|
257 | + array('espresso_core'), |
|
258 | + EVENT_ESPRESSO_VERSION, |
|
259 | + TRUE |
|
260 | + ); |
|
261 | 261 | wp_enqueue_script('ticket_selector'); |
262 | - require_once( EE_LIBRARIES.'form_sections/strategies/display/EE_Checkbox_Dropdown_Selector_Display_Strategy.strategy.php'); |
|
263 | - \EE_Checkbox_Dropdown_Selector_Display_Strategy::enqueue_styles_and_scripts(); |
|
264 | - } |
|
262 | + require_once( EE_LIBRARIES.'form_sections/strategies/display/EE_Checkbox_Dropdown_Selector_Display_Strategy.strategy.php'); |
|
263 | + \EE_Checkbox_Dropdown_Selector_Display_Strategy::enqueue_styles_and_scripts(); |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * @return void |
|
271 | + */ |
|
272 | + public static function loadIframeAssets() |
|
273 | + { |
|
274 | + // for event lists |
|
275 | + add_filter( |
|
276 | + 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__css', |
|
277 | + array('EED_Ticket_Selector', 'iframeCss') |
|
278 | + ); |
|
279 | + add_filter( |
|
280 | + 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__js', |
|
281 | + array('EED_Ticket_Selector', 'iframeJs') |
|
282 | + ); |
|
283 | + // for ticket selectors |
|
284 | + add_filter( |
|
285 | + 'FHEE__EED_Ticket_Selector__ticket_selector_iframe__css', |
|
286 | + array('EED_Ticket_Selector', 'iframeCss') |
|
287 | + ); |
|
288 | + add_filter( |
|
289 | + 'FHEE__EED_Ticket_Selector__ticket_selector_iframe__js', |
|
290 | + array('EED_Ticket_Selector', 'iframeJs') |
|
291 | + ); |
|
292 | + } |
|
293 | + |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Informs the rest of the forms system what CSS and JS is needed to display the input |
|
298 | + * |
|
299 | + * @param array $iframe_css |
|
300 | + * @return array |
|
301 | + */ |
|
302 | + public static function iframeCss(array $iframe_css) |
|
303 | + { |
|
304 | + $iframe_css['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.css'; |
|
305 | + return $iframe_css; |
|
265 | 306 | } |
266 | 307 | |
267 | 308 | |
268 | 309 | |
269 | - /** |
|
270 | - * @return void |
|
271 | - */ |
|
272 | - public static function loadIframeAssets() |
|
273 | - { |
|
274 | - // for event lists |
|
275 | - add_filter( |
|
276 | - 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__css', |
|
277 | - array('EED_Ticket_Selector', 'iframeCss') |
|
278 | - ); |
|
279 | - add_filter( |
|
280 | - 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__js', |
|
281 | - array('EED_Ticket_Selector', 'iframeJs') |
|
282 | - ); |
|
283 | - // for ticket selectors |
|
284 | - add_filter( |
|
285 | - 'FHEE__EED_Ticket_Selector__ticket_selector_iframe__css', |
|
286 | - array('EED_Ticket_Selector', 'iframeCss') |
|
287 | - ); |
|
288 | - add_filter( |
|
289 | - 'FHEE__EED_Ticket_Selector__ticket_selector_iframe__js', |
|
290 | - array('EED_Ticket_Selector', 'iframeJs') |
|
291 | - ); |
|
292 | - } |
|
293 | - |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Informs the rest of the forms system what CSS and JS is needed to display the input |
|
298 | - * |
|
299 | - * @param array $iframe_css |
|
300 | - * @return array |
|
301 | - */ |
|
302 | - public static function iframeCss(array $iframe_css) |
|
303 | - { |
|
304 | - $iframe_css['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.css'; |
|
305 | - return $iframe_css; |
|
306 | - } |
|
307 | - |
|
308 | - |
|
309 | - |
|
310 | - /** |
|
311 | - * Informs the rest of the forms system what CSS and JS is needed to display the input |
|
312 | - * |
|
313 | - * @param array $iframe_js |
|
314 | - * @return array |
|
315 | - */ |
|
316 | - public static function iframeJs(array $iframe_js) |
|
317 | - { |
|
318 | - $iframe_js['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.js'; |
|
319 | - return $iframe_js; |
|
320 | - } |
|
310 | + /** |
|
311 | + * Informs the rest of the forms system what CSS and JS is needed to display the input |
|
312 | + * |
|
313 | + * @param array $iframe_js |
|
314 | + * @return array |
|
315 | + */ |
|
316 | + public static function iframeJs(array $iframe_js) |
|
317 | + { |
|
318 | + $iframe_js['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.js'; |
|
319 | + return $iframe_js; |
|
320 | + } |
|
321 | 321 | |
322 | 322 | |
323 | 323 | /****************************** DEPRECATED ******************************/ |
324 | 324 | |
325 | 325 | |
326 | 326 | |
327 | - /** |
|
328 | - * @deprecated |
|
329 | - * @return string |
|
330 | - * @throws \EE_Error |
|
331 | - */ |
|
332 | - public static function display_view_details_btn() |
|
333 | - { |
|
334 | - // todo add doing_it_wrong() notice during next major version |
|
335 | - return EED_Ticket_Selector::ticketSelector()->displayViewDetailsButton(); |
|
336 | - } |
|
337 | - |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * @deprecated |
|
342 | - * @return string |
|
343 | - * @throws \EE_Error |
|
344 | - */ |
|
345 | - public static function display_ticket_selector_submit() |
|
346 | - { |
|
347 | - // todo add doing_it_wrong() notice during next major version |
|
348 | - return EED_Ticket_Selector::ticketSelector()->displaySubmitButton(); |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * @deprecated |
|
355 | - * @param string $permalink_string |
|
356 | - * @param int $id |
|
357 | - * @param string $new_title |
|
358 | - * @param string $new_slug |
|
359 | - * @return string |
|
360 | - */ |
|
361 | - public static function iframe_code_button($permalink_string, $id, $new_title = '', $new_slug = '') |
|
362 | - { |
|
363 | - // todo add doing_it_wrong() notice during next major version |
|
364 | - if ( |
|
365 | - \EE_Registry::instance()->REQ->get('page') === 'espresso_events' |
|
366 | - && \EE_Registry::instance()->REQ->get('action') === 'edit' |
|
367 | - ) { |
|
368 | - $iframe_embed_button = \EED_Ticket_Selector::getIframeEmbedButton(); |
|
369 | - $iframe_embed_button->addEventEditorIframeEmbedButton(); |
|
370 | - } |
|
371 | - return ''; |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - |
|
376 | - /** |
|
377 | - * @deprecated |
|
378 | - * @param int $ID |
|
379 | - * @param string $external_url |
|
380 | - * @return string |
|
381 | - */ |
|
382 | - public static function ticket_selector_form_open($ID = 0, $external_url = '') |
|
383 | - { |
|
384 | - // todo add doing_it_wrong() notice during next major version |
|
385 | - return EED_Ticket_Selector::ticketSelector()->formOpen($ID, $external_url); |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - |
|
390 | - /** |
|
391 | - * @deprecated |
|
392 | - * @return string |
|
393 | - */ |
|
394 | - public static function ticket_selector_form_close() |
|
395 | - { |
|
396 | - // todo add doing_it_wrong() notice during next major version |
|
397 | - return EED_Ticket_Selector::ticketSelector()->formClose(); |
|
398 | - } |
|
399 | - |
|
400 | - |
|
401 | - |
|
402 | - /** |
|
403 | - * @deprecated |
|
404 | - * @return string |
|
405 | - */ |
|
406 | - public static function no_tkt_slctr_end_dv() |
|
407 | - { |
|
408 | - // todo add doing_it_wrong() notice during next major version |
|
409 | - return EED_Ticket_Selector::ticketSelector()->ticketSelectorEndDiv(); |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - |
|
414 | - /** |
|
415 | - * @deprecated 4.9.13 |
|
416 | - * @return string |
|
417 | - */ |
|
418 | - public static function tkt_slctr_end_dv() |
|
419 | - { |
|
420 | - return EED_Ticket_Selector::ticketSelector()->clearTicketSelector(); |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - |
|
425 | - /** |
|
426 | - * @deprecated |
|
427 | - * @return string |
|
428 | - */ |
|
429 | - public static function clear_tkt_slctr() |
|
430 | - { |
|
431 | - return EED_Ticket_Selector::ticketSelector()->clearTicketSelector(); |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * @deprecated |
|
438 | - */ |
|
439 | - public static function load_tckt_slctr_assets_admin() |
|
440 | - { |
|
441 | - // todo add doing_it_wrong() notice during next major version |
|
442 | - if ( |
|
443 | - \EE_Registry::instance()->REQ->get( 'page' ) === 'espresso_events' |
|
444 | - && \EE_Registry::instance()->REQ->get( 'action' ) === 'edit' |
|
445 | - ) { |
|
446 | - $iframe_embed_button = \EED_Ticket_Selector::getIframeEmbedButton(); |
|
447 | - $iframe_embed_button->embedButtonAssets(); |
|
448 | - } |
|
449 | - } |
|
327 | + /** |
|
328 | + * @deprecated |
|
329 | + * @return string |
|
330 | + * @throws \EE_Error |
|
331 | + */ |
|
332 | + public static function display_view_details_btn() |
|
333 | + { |
|
334 | + // todo add doing_it_wrong() notice during next major version |
|
335 | + return EED_Ticket_Selector::ticketSelector()->displayViewDetailsButton(); |
|
336 | + } |
|
337 | + |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * @deprecated |
|
342 | + * @return string |
|
343 | + * @throws \EE_Error |
|
344 | + */ |
|
345 | + public static function display_ticket_selector_submit() |
|
346 | + { |
|
347 | + // todo add doing_it_wrong() notice during next major version |
|
348 | + return EED_Ticket_Selector::ticketSelector()->displaySubmitButton(); |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * @deprecated |
|
355 | + * @param string $permalink_string |
|
356 | + * @param int $id |
|
357 | + * @param string $new_title |
|
358 | + * @param string $new_slug |
|
359 | + * @return string |
|
360 | + */ |
|
361 | + public static function iframe_code_button($permalink_string, $id, $new_title = '', $new_slug = '') |
|
362 | + { |
|
363 | + // todo add doing_it_wrong() notice during next major version |
|
364 | + if ( |
|
365 | + \EE_Registry::instance()->REQ->get('page') === 'espresso_events' |
|
366 | + && \EE_Registry::instance()->REQ->get('action') === 'edit' |
|
367 | + ) { |
|
368 | + $iframe_embed_button = \EED_Ticket_Selector::getIframeEmbedButton(); |
|
369 | + $iframe_embed_button->addEventEditorIframeEmbedButton(); |
|
370 | + } |
|
371 | + return ''; |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + |
|
376 | + /** |
|
377 | + * @deprecated |
|
378 | + * @param int $ID |
|
379 | + * @param string $external_url |
|
380 | + * @return string |
|
381 | + */ |
|
382 | + public static function ticket_selector_form_open($ID = 0, $external_url = '') |
|
383 | + { |
|
384 | + // todo add doing_it_wrong() notice during next major version |
|
385 | + return EED_Ticket_Selector::ticketSelector()->formOpen($ID, $external_url); |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + |
|
390 | + /** |
|
391 | + * @deprecated |
|
392 | + * @return string |
|
393 | + */ |
|
394 | + public static function ticket_selector_form_close() |
|
395 | + { |
|
396 | + // todo add doing_it_wrong() notice during next major version |
|
397 | + return EED_Ticket_Selector::ticketSelector()->formClose(); |
|
398 | + } |
|
399 | + |
|
400 | + |
|
401 | + |
|
402 | + /** |
|
403 | + * @deprecated |
|
404 | + * @return string |
|
405 | + */ |
|
406 | + public static function no_tkt_slctr_end_dv() |
|
407 | + { |
|
408 | + // todo add doing_it_wrong() notice during next major version |
|
409 | + return EED_Ticket_Selector::ticketSelector()->ticketSelectorEndDiv(); |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + |
|
414 | + /** |
|
415 | + * @deprecated 4.9.13 |
|
416 | + * @return string |
|
417 | + */ |
|
418 | + public static function tkt_slctr_end_dv() |
|
419 | + { |
|
420 | + return EED_Ticket_Selector::ticketSelector()->clearTicketSelector(); |
|
421 | + } |
|
422 | + |
|
423 | + |
|
424 | + |
|
425 | + /** |
|
426 | + * @deprecated |
|
427 | + * @return string |
|
428 | + */ |
|
429 | + public static function clear_tkt_slctr() |
|
430 | + { |
|
431 | + return EED_Ticket_Selector::ticketSelector()->clearTicketSelector(); |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * @deprecated |
|
438 | + */ |
|
439 | + public static function load_tckt_slctr_assets_admin() |
|
440 | + { |
|
441 | + // todo add doing_it_wrong() notice during next major version |
|
442 | + if ( |
|
443 | + \EE_Registry::instance()->REQ->get( 'page' ) === 'espresso_events' |
|
444 | + && \EE_Registry::instance()->REQ->get( 'action' ) === 'edit' |
|
445 | + ) { |
|
446 | + $iframe_embed_button = \EED_Ticket_Selector::getIframeEmbedButton(); |
|
447 | + $iframe_embed_button->embedButtonAssets(); |
|
448 | + } |
|
449 | + } |
|
450 | 450 | |
451 | 451 | |
452 | 452 | } |
@@ -3,8 +3,8 @@ discard block |
||
3 | 3 | use EventEspresso\modules\ticket_selector\TicketSelectorIframe; |
4 | 4 | use EventEspresso\modules\ticket_selector\TicketSelectorIframeEmbedButton; |
5 | 5 | |
6 | -if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
7 | - exit( 'No direct script access allowed' ); |
|
6 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
7 | + exit('No direct script access allowed'); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | |
@@ -38,15 +38,15 @@ discard block |
||
38 | 38 | * @return EED_Ticket_Selector |
39 | 39 | */ |
40 | 40 | public static function instance() { |
41 | - return parent::get_instance( __CLASS__ ); |
|
41 | + return parent::get_instance(__CLASS__); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | |
45 | 45 | |
46 | - protected function set_config(){ |
|
47 | - $this->set_config_section( 'template_settings' ); |
|
48 | - $this->set_config_class( 'EE_Ticket_Selector_Config' ); |
|
49 | - $this->set_config_name( 'EED_Ticket_Selector' ); |
|
46 | + protected function set_config() { |
|
47 | + $this->set_config_section('template_settings'); |
|
48 | + $this->set_config_class('EE_Ticket_Selector_Config'); |
|
49 | + $this->set_config_name('EED_Ticket_Selector'); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | |
@@ -59,13 +59,13 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public static function set_hooks() { |
61 | 61 | // routing |
62 | - EE_Config::register_route( 'iframe', 'EED_Ticket_Selector', 'ticket_selector_iframe', 'ticket_selector' ); |
|
63 | - EE_Config::register_route( 'process_ticket_selections', 'EED_Ticket_Selector', 'process_ticket_selections' ); |
|
62 | + EE_Config::register_route('iframe', 'EED_Ticket_Selector', 'ticket_selector_iframe', 'ticket_selector'); |
|
63 | + EE_Config::register_route('process_ticket_selections', 'EED_Ticket_Selector', 'process_ticket_selections'); |
|
64 | 64 | EE_Config::register_route('cancel_ticket_selections', 'EED_Ticket_Selector', 'cancel_ticket_selections'); |
65 | - add_action( 'wp_loaded', array( 'EED_Ticket_Selector', 'set_definitions' ), 2 ); |
|
66 | - add_action( 'AHEE_event_details_header_bottom', array( 'EED_Ticket_Selector', 'display_ticket_selector' ), 10, 1 ); |
|
67 | - add_action( 'wp_enqueue_scripts', array( 'EED_Ticket_Selector', 'translate_js_strings' ), 0 ); |
|
68 | - add_action( 'wp_enqueue_scripts', array( 'EED_Ticket_Selector', 'load_tckt_slctr_assets' ), 10 ); |
|
65 | + add_action('wp_loaded', array('EED_Ticket_Selector', 'set_definitions'), 2); |
|
66 | + add_action('AHEE_event_details_header_bottom', array('EED_Ticket_Selector', 'display_ticket_selector'), 10, 1); |
|
67 | + add_action('wp_enqueue_scripts', array('EED_Ticket_Selector', 'translate_js_strings'), 0); |
|
68 | + add_action('wp_enqueue_scripts', array('EED_Ticket_Selector', 'load_tckt_slctr_assets'), 10); |
|
69 | 69 | EED_Ticket_Selector::loadIframeAssets(); |
70 | 70 | } |
71 | 71 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | // to load assets for "espresso_events" page on the "edit" route (action) |
83 | 83 | add_action( |
84 | 84 | 'FHEE__EE_Admin_Page___load_page_dependencies__after_load__espresso_events__edit', |
85 | - array( 'EED_Ticket_Selector', 'ticket_selector_iframe_embed_button' ), |
|
85 | + array('EED_Ticket_Selector', 'ticket_selector_iframe_embed_button'), |
|
86 | 86 | 10 |
87 | 87 | ); |
88 | 88 | |
@@ -106,11 +106,11 @@ discard block |
||
106 | 106 | * @return void |
107 | 107 | */ |
108 | 108 | public static function set_definitions() { |
109 | - define( 'TICKET_SELECTOR_ASSETS_URL', plugin_dir_url( __FILE__ ) . 'assets' . DS ); |
|
110 | - define( 'TICKET_SELECTOR_TEMPLATES_PATH', str_replace( '\\', DS, plugin_dir_path( __FILE__ )) . 'templates' . DS ); |
|
109 | + define('TICKET_SELECTOR_ASSETS_URL', plugin_dir_url(__FILE__).'assets'.DS); |
|
110 | + define('TICKET_SELECTOR_TEMPLATES_PATH', str_replace('\\', DS, plugin_dir_path(__FILE__)).'templates'.DS); |
|
111 | 111 | |
112 | 112 | //if config is not set, initialize |
113 | - if ( ! EE_Registry::instance()->CFG->template_settings->EED_Ticket_Selector instanceof EE_Ticket_Selector_Config ) { |
|
113 | + if ( ! EE_Registry::instance()->CFG->template_settings->EED_Ticket_Selector instanceof EE_Ticket_Selector_Config) { |
|
114 | 114 | \EED_Ticket_Selector::instance()->set_config(); |
115 | 115 | \EE_Registry::instance()->CFG->template_settings->EED_Ticket_Selector = \EED_Ticket_Selector::instance()->config(); |
116 | 116 | } |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | * @param WP $WP |
138 | 138 | * @return void |
139 | 139 | */ |
140 | - public function run( $WP ) {} |
|
140 | + public function run($WP) {} |
|
141 | 141 | |
142 | 142 | |
143 | 143 | |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | * @return \EventEspresso\modules\ticket_selector\TicketSelectorIframeEmbedButton |
146 | 146 | */ |
147 | 147 | public static function getIframeEmbedButton() { |
148 | - if ( ! self::$iframe_embed_button instanceof TicketSelectorIframeEmbedButton ) { |
|
148 | + if ( ! self::$iframe_embed_button instanceof TicketSelectorIframeEmbedButton) { |
|
149 | 149 | self::$iframe_embed_button = new TicketSelectorIframeEmbedButton(); |
150 | 150 | } |
151 | 151 | return self::$iframe_embed_button; |
@@ -189,8 +189,8 @@ discard block |
||
189 | 189 | * @return string |
190 | 190 | * @throws \EE_Error |
191 | 191 | */ |
192 | - public static function display_ticket_selector( $event = NULL, $view_details = FALSE ) { |
|
193 | - return EED_Ticket_Selector::ticketSelector()->display( $event, $view_details ); |
|
192 | + public static function display_ticket_selector($event = NULL, $view_details = FALSE) { |
|
193 | + return EED_Ticket_Selector::ticketSelector()->display($event, $view_details); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | |
@@ -241,11 +241,11 @@ discard block |
||
241 | 241 | * @return void |
242 | 242 | */ |
243 | 243 | public static function load_tckt_slctr_assets() { |
244 | - if ( apply_filters( 'FHEE__EED_Ticket_Selector__load_tckt_slctr_assets', FALSE ) ) { |
|
244 | + if (apply_filters('FHEE__EED_Ticket_Selector__load_tckt_slctr_assets', FALSE)) { |
|
245 | 245 | // add some style |
246 | 246 | wp_register_style( |
247 | 247 | 'ticket_selector', |
248 | - TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.css', |
|
248 | + TICKET_SELECTOR_ASSETS_URL.'ticket_selector.css', |
|
249 | 249 | array(), |
250 | 250 | EVENT_ESPRESSO_VERSION |
251 | 251 | ); |
@@ -253,13 +253,13 @@ discard block |
||
253 | 253 | // make it dance |
254 | 254 | wp_register_script( |
255 | 255 | 'ticket_selector', |
256 | - TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.js', |
|
256 | + TICKET_SELECTOR_ASSETS_URL.'ticket_selector.js', |
|
257 | 257 | array('espresso_core'), |
258 | 258 | EVENT_ESPRESSO_VERSION, |
259 | 259 | TRUE |
260 | 260 | ); |
261 | 261 | wp_enqueue_script('ticket_selector'); |
262 | - require_once( EE_LIBRARIES.'form_sections/strategies/display/EE_Checkbox_Dropdown_Selector_Display_Strategy.strategy.php'); |
|
262 | + require_once(EE_LIBRARIES.'form_sections/strategies/display/EE_Checkbox_Dropdown_Selector_Display_Strategy.strategy.php'); |
|
263 | 263 | \EE_Checkbox_Dropdown_Selector_Display_Strategy::enqueue_styles_and_scripts(); |
264 | 264 | } |
265 | 265 | } |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | */ |
302 | 302 | public static function iframeCss(array $iframe_css) |
303 | 303 | { |
304 | - $iframe_css['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.css'; |
|
304 | + $iframe_css['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL.'ticket_selector.css'; |
|
305 | 305 | return $iframe_css; |
306 | 306 | } |
307 | 307 | |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | */ |
316 | 316 | public static function iframeJs(array $iframe_js) |
317 | 317 | { |
318 | - $iframe_js['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.js'; |
|
318 | + $iframe_js['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL.'ticket_selector.js'; |
|
319 | 319 | return $iframe_js; |
320 | 320 | } |
321 | 321 | |
@@ -440,8 +440,8 @@ discard block |
||
440 | 440 | { |
441 | 441 | // todo add doing_it_wrong() notice during next major version |
442 | 442 | if ( |
443 | - \EE_Registry::instance()->REQ->get( 'page' ) === 'espresso_events' |
|
444 | - && \EE_Registry::instance()->REQ->get( 'action' ) === 'edit' |
|
443 | + \EE_Registry::instance()->REQ->get('page') === 'espresso_events' |
|
444 | + && \EE_Registry::instance()->REQ->get('action') === 'edit' |
|
445 | 445 | ) { |
446 | 446 | $iframe_embed_button = \EED_Ticket_Selector::getIframeEmbedButton(); |
447 | 447 | $iframe_embed_button->embedButtonAssets(); |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | use EventEspresso\core\libraries\rest_api\controllers\model\Read; |
15 | 15 | |
16 | 16 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
17 | - exit('No direct script access allowed'); |
|
17 | + exit('No direct script access allowed'); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | |
@@ -31,116 +31,116 @@ discard block |
||
31 | 31 | class Checkin extends Base |
32 | 32 | { |
33 | 33 | |
34 | - /** |
|
35 | - * @param WP_REST_Request $request |
|
36 | - * @return WP_Error|WP_REST_Response |
|
37 | - */ |
|
38 | - public static function handle_request_toggle_checkin(WP_REST_Request $request) |
|
39 | - { |
|
40 | - $controller = new Checkin(); |
|
41 | - return $controller->_create_checkin_checkout_object($request); |
|
42 | - } |
|
34 | + /** |
|
35 | + * @param WP_REST_Request $request |
|
36 | + * @return WP_Error|WP_REST_Response |
|
37 | + */ |
|
38 | + public static function handle_request_toggle_checkin(WP_REST_Request $request) |
|
39 | + { |
|
40 | + $controller = new Checkin(); |
|
41 | + return $controller->_create_checkin_checkout_object($request); |
|
42 | + } |
|
43 | 43 | |
44 | 44 | |
45 | 45 | |
46 | - /** |
|
47 | - * Toggles whether the user is checked in or not. |
|
48 | - * |
|
49 | - * @param WP_REST_Request $request |
|
50 | - * @return WP_Error|WP_REST_Response |
|
51 | - */ |
|
52 | - protected function _create_checkin_checkout_object(WP_REST_Request $request) |
|
53 | - { |
|
54 | - $reg_id = $request->get_param('REG_ID'); |
|
55 | - $dtt_id = $request->get_param('DTT_ID'); |
|
56 | - $force = $request->get_param('force'); |
|
57 | - if ($force == 'true') { |
|
58 | - $force = true; |
|
59 | - } else { |
|
60 | - $force = false; |
|
61 | - } |
|
62 | - $reg = EEM_Registration::instance()->get_one_by_ID($reg_id); |
|
63 | - if (! $reg instanceof EE_Registration) { |
|
64 | - return $this->send_response( |
|
65 | - new WP_Error( |
|
66 | - 'rest_registration_toggle_checkin_invalid_id', |
|
67 | - sprintf( |
|
68 | - __('You cannot checkin registration with ID %1$s because it doesn\'t exist.', |
|
69 | - 'event_espresso'), |
|
70 | - $reg_id |
|
71 | - ), |
|
72 | - array('status' => 422) |
|
73 | - ) |
|
74 | - ); |
|
75 | - } |
|
76 | - if (! EE_Capabilities::instance() |
|
77 | - ->current_user_can('ee_edit_checkin', 'rest_api_checkin_endpoint', $reg_id) |
|
78 | - ) { |
|
79 | - return $this->send_response( |
|
80 | - new WP_Error( |
|
81 | - 'rest_user_cannot_toggle_checkin', |
|
82 | - sprintf( |
|
83 | - __('You are not allowed to checkin registration with ID %1$s.', 'event_espresso'), |
|
84 | - $reg_id |
|
85 | - ), |
|
86 | - array('status' => 403) |
|
87 | - ) |
|
88 | - ); |
|
89 | - } |
|
90 | - $success = $reg->toggle_checkin_status($dtt_id, ! $force); |
|
91 | - if ($success === false) { |
|
92 | - //check if we know they can't check in because they're not approved and we aren't forcing |
|
93 | - if (! $reg->is_approved() && ! $force) { |
|
94 | - //rely on EE_Error::add_error messages to have been added to give more data about why it failed |
|
95 | - return $this->send_response( |
|
96 | - new WP_Error( |
|
97 | - 'rest_toggle_checkin_failed', |
|
98 | - __('Registration check-in failed because the registration is not approved. You may attempt to force checking in though.', |
|
99 | - 'event_espresso') |
|
100 | - ) |
|
101 | - ); |
|
102 | - } |
|
103 | - return $this->send_response( |
|
104 | - new WP_Error( |
|
105 | - 'rest_toggle_checkin_failed_not_forceable', |
|
106 | - __('Registration checkin failed. Please see additional error data.', 'event_espresso') |
|
107 | - ) |
|
108 | - ); |
|
109 | - } |
|
110 | - $checkin = EEM_Checkin::instance()->get_one( |
|
111 | - array( |
|
112 | - array( |
|
113 | - 'REG_ID' => $reg_id, |
|
114 | - 'DTT_ID' => $dtt_id, |
|
115 | - ), |
|
116 | - 'order_by' => array( |
|
117 | - 'CHK_timestamp' => 'DESC', |
|
118 | - ), |
|
119 | - ) |
|
120 | - ); |
|
121 | - if (! $checkin instanceof EE_Checkin) { |
|
122 | - return $this->send_response( |
|
123 | - new WP_Error( |
|
124 | - 'rest_toggle_checkin_error', |
|
125 | - sprintf( |
|
126 | - __('Supposedly we created a new checkin object for registration %1$s at datetime %2$s, but we can\'t find it.', |
|
127 | - 'event_espresso'), |
|
128 | - $reg_id, |
|
129 | - $dtt_id |
|
130 | - ) |
|
131 | - ) |
|
132 | - ); |
|
133 | - } |
|
134 | - $requested_version = $this->get_requested_version($request->get_route()); |
|
135 | - $get_request = new WP_REST_Request( |
|
136 | - 'GET', |
|
137 | - EED_Core_Rest_Api::ee_api_namespace . $requested_version . '/checkins/' . $checkin->ID() |
|
138 | - ); |
|
139 | - $get_request->set_url_params( |
|
140 | - array( |
|
141 | - 'id' => $checkin->ID(), |
|
142 | - ) |
|
143 | - ); |
|
144 | - return Read::handle_request_get_one($get_request); |
|
145 | - } |
|
46 | + /** |
|
47 | + * Toggles whether the user is checked in or not. |
|
48 | + * |
|
49 | + * @param WP_REST_Request $request |
|
50 | + * @return WP_Error|WP_REST_Response |
|
51 | + */ |
|
52 | + protected function _create_checkin_checkout_object(WP_REST_Request $request) |
|
53 | + { |
|
54 | + $reg_id = $request->get_param('REG_ID'); |
|
55 | + $dtt_id = $request->get_param('DTT_ID'); |
|
56 | + $force = $request->get_param('force'); |
|
57 | + if ($force == 'true') { |
|
58 | + $force = true; |
|
59 | + } else { |
|
60 | + $force = false; |
|
61 | + } |
|
62 | + $reg = EEM_Registration::instance()->get_one_by_ID($reg_id); |
|
63 | + if (! $reg instanceof EE_Registration) { |
|
64 | + return $this->send_response( |
|
65 | + new WP_Error( |
|
66 | + 'rest_registration_toggle_checkin_invalid_id', |
|
67 | + sprintf( |
|
68 | + __('You cannot checkin registration with ID %1$s because it doesn\'t exist.', |
|
69 | + 'event_espresso'), |
|
70 | + $reg_id |
|
71 | + ), |
|
72 | + array('status' => 422) |
|
73 | + ) |
|
74 | + ); |
|
75 | + } |
|
76 | + if (! EE_Capabilities::instance() |
|
77 | + ->current_user_can('ee_edit_checkin', 'rest_api_checkin_endpoint', $reg_id) |
|
78 | + ) { |
|
79 | + return $this->send_response( |
|
80 | + new WP_Error( |
|
81 | + 'rest_user_cannot_toggle_checkin', |
|
82 | + sprintf( |
|
83 | + __('You are not allowed to checkin registration with ID %1$s.', 'event_espresso'), |
|
84 | + $reg_id |
|
85 | + ), |
|
86 | + array('status' => 403) |
|
87 | + ) |
|
88 | + ); |
|
89 | + } |
|
90 | + $success = $reg->toggle_checkin_status($dtt_id, ! $force); |
|
91 | + if ($success === false) { |
|
92 | + //check if we know they can't check in because they're not approved and we aren't forcing |
|
93 | + if (! $reg->is_approved() && ! $force) { |
|
94 | + //rely on EE_Error::add_error messages to have been added to give more data about why it failed |
|
95 | + return $this->send_response( |
|
96 | + new WP_Error( |
|
97 | + 'rest_toggle_checkin_failed', |
|
98 | + __('Registration check-in failed because the registration is not approved. You may attempt to force checking in though.', |
|
99 | + 'event_espresso') |
|
100 | + ) |
|
101 | + ); |
|
102 | + } |
|
103 | + return $this->send_response( |
|
104 | + new WP_Error( |
|
105 | + 'rest_toggle_checkin_failed_not_forceable', |
|
106 | + __('Registration checkin failed. Please see additional error data.', 'event_espresso') |
|
107 | + ) |
|
108 | + ); |
|
109 | + } |
|
110 | + $checkin = EEM_Checkin::instance()->get_one( |
|
111 | + array( |
|
112 | + array( |
|
113 | + 'REG_ID' => $reg_id, |
|
114 | + 'DTT_ID' => $dtt_id, |
|
115 | + ), |
|
116 | + 'order_by' => array( |
|
117 | + 'CHK_timestamp' => 'DESC', |
|
118 | + ), |
|
119 | + ) |
|
120 | + ); |
|
121 | + if (! $checkin instanceof EE_Checkin) { |
|
122 | + return $this->send_response( |
|
123 | + new WP_Error( |
|
124 | + 'rest_toggle_checkin_error', |
|
125 | + sprintf( |
|
126 | + __('Supposedly we created a new checkin object for registration %1$s at datetime %2$s, but we can\'t find it.', |
|
127 | + 'event_espresso'), |
|
128 | + $reg_id, |
|
129 | + $dtt_id |
|
130 | + ) |
|
131 | + ) |
|
132 | + ); |
|
133 | + } |
|
134 | + $requested_version = $this->get_requested_version($request->get_route()); |
|
135 | + $get_request = new WP_REST_Request( |
|
136 | + 'GET', |
|
137 | + EED_Core_Rest_Api::ee_api_namespace . $requested_version . '/checkins/' . $checkin->ID() |
|
138 | + ); |
|
139 | + $get_request->set_url_params( |
|
140 | + array( |
|
141 | + 'id' => $checkin->ID(), |
|
142 | + ) |
|
143 | + ); |
|
144 | + return Read::handle_request_get_one($get_request); |
|
145 | + } |
|
146 | 146 | } |
147 | 147 | \ No newline at end of file |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | use EventEspresso\core\libraries\rest_api\controllers\Base as Base; |
14 | 14 | use EventEspresso\core\libraries\rest_api\controllers\model\Read; |
15 | 15 | |
16 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
16 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
17 | 17 | exit('No direct script access allowed'); |
18 | 18 | } |
19 | 19 | |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | $force = false; |
61 | 61 | } |
62 | 62 | $reg = EEM_Registration::instance()->get_one_by_ID($reg_id); |
63 | - if (! $reg instanceof EE_Registration) { |
|
63 | + if ( ! $reg instanceof EE_Registration) { |
|
64 | 64 | return $this->send_response( |
65 | 65 | new WP_Error( |
66 | 66 | 'rest_registration_toggle_checkin_invalid_id', |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | ) |
74 | 74 | ); |
75 | 75 | } |
76 | - if (! EE_Capabilities::instance() |
|
76 | + if ( ! EE_Capabilities::instance() |
|
77 | 77 | ->current_user_can('ee_edit_checkin', 'rest_api_checkin_endpoint', $reg_id) |
78 | 78 | ) { |
79 | 79 | return $this->send_response( |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | $success = $reg->toggle_checkin_status($dtt_id, ! $force); |
91 | 91 | if ($success === false) { |
92 | 92 | //check if we know they can't check in because they're not approved and we aren't forcing |
93 | - if (! $reg->is_approved() && ! $force) { |
|
93 | + if ( ! $reg->is_approved() && ! $force) { |
|
94 | 94 | //rely on EE_Error::add_error messages to have been added to give more data about why it failed |
95 | 95 | return $this->send_response( |
96 | 96 | new WP_Error( |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | ), |
119 | 119 | ) |
120 | 120 | ); |
121 | - if (! $checkin instanceof EE_Checkin) { |
|
121 | + if ( ! $checkin instanceof EE_Checkin) { |
|
122 | 122 | return $this->send_response( |
123 | 123 | new WP_Error( |
124 | 124 | 'rest_toggle_checkin_error', |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | $requested_version = $this->get_requested_version($request->get_route()); |
135 | 135 | $get_request = new WP_REST_Request( |
136 | 136 | 'GET', |
137 | - EED_Core_Rest_Api::ee_api_namespace . $requested_version . '/checkins/' . $checkin->ID() |
|
137 | + EED_Core_Rest_Api::ee_api_namespace.$requested_version.'/checkins/'.$checkin->ID() |
|
138 | 138 | ); |
139 | 139 | $get_request->set_url_params( |
140 | 140 | array( |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
3 | - exit('NO direct script access allowed'); |
|
3 | + exit('NO direct script access allowed'); |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | |
@@ -28,616 +28,616 @@ discard block |
||
28 | 28 | class EE_Transaction_Shortcodes extends EE_Shortcodes |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * @var EE_Payment_Method $_invoice_pm the invoiec payment method for use in invoices etc |
|
33 | - */ |
|
34 | - protected $_invoice_pm; |
|
35 | - |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * EE_Transaction_Shortcodes constructor. |
|
40 | - */ |
|
41 | - public function __construct() |
|
42 | - { |
|
43 | - parent::__construct(); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - |
|
48 | - protected function _init_props() |
|
49 | - { |
|
50 | - $this->label = esc_html__('Transaction Shortcodes', 'event_espresso'); |
|
51 | - $this->description = esc_html__('All shortcodes specific to transaction related data', 'event_espresso'); |
|
52 | - $this->_shortcodes = array( |
|
53 | - '[TXN_ID]' => esc_html__('The transaction id for the purchase.', 'event_espresso'), |
|
54 | - '[PAYMENT_URL]' => esc_html__('This is a link to make a payment for the event', |
|
55 | - 'event_espresso'), |
|
56 | - '[PAYMENT_LINK_IF_NEEDED_*]' => esc_html__('This is a special dynamic shortcode that allows one to insert a payment link conditional on there being amount owing on the transaction. Three params are available on this shortcode:', |
|
57 | - 'event_espresso') |
|
58 | - . '<ul>' |
|
59 | - . '<li>' |
|
60 | - . sprintf(esc_html__('%sclass:%s This can be used to indicate css class is given to the containing css element (default is "callout").', |
|
61 | - 'event_espresso'), '<strong>', '</strong>') |
|
62 | - . '</li>' |
|
63 | - . '<li>' |
|
64 | - . sprintf(esc_html__('%scustom_text:%s This should be a sprintf format text string (with %%s for where the hyperlink tags go) that is used for the generated link text (The default is "You can %%smake a payment here »%%s.)', |
|
65 | - 'event_espresso'), '<strong>', '</strong>') |
|
66 | - . '</li>' |
|
67 | - . '<li>' |
|
68 | - . sprintf(esc_html__('%scontainer_tag:%s Use this to indicate what container tag you want surrounding the payment link (default is "p").', |
|
69 | - 'event_espresso'), '<strong>', '</strong>') |
|
70 | - . '</li>' |
|
71 | - . '</ul>', |
|
72 | - '[PAYMENT_DUE_DATE_*]' => esc_html__('This is a special dynamic shortcode that allows one to output a payment due date. It will only result in a date shown if there is money owing. Three parameters are available on this shortcode:', |
|
73 | - 'event_espresso') |
|
74 | - . '<ul>' |
|
75 | - . '<li>' |
|
76 | - . sprintf(esc_html__('%sformat:%s This is used to indicate what format the date is in. Default is whatever is set as date formats for your website.', |
|
77 | - 'event_espresso'), '<strong>', '</strong>') |
|
78 | - . '</li>' |
|
79 | - . '<li>' |
|
80 | - . sprintf(esc_html__('%sdays_until_due:%s This is the number of days form the transaction creation date that the payment is due. Defaults to 30.', |
|
81 | - 'event_espresso'), '<strong>', '</strong>') |
|
82 | - . '</li>' |
|
83 | - . '<li>' |
|
84 | - . sprintf(esc_html__('%sprefix_text:%s You can use this to indicate what text will prefix the date string. Defaults to "Payment in full due by:"', |
|
85 | - 'event_espresso'), '<strong>', '</strong>') |
|
86 | - . '</li>', |
|
87 | - '[INVOICE_LINK]' => esc_html__('This is a full html link to the invoice', |
|
88 | - 'event_espresso'), |
|
89 | - '[INVOICE_URL]' => esc_html__('This is just the url for the invoice', 'event_espresso'), |
|
90 | - '[INVOICE_LOGO_URL]' => esc_html__('This returns the url for the logo uploaded via the invoice settings page.', |
|
91 | - 'event_espresso'), |
|
92 | - '[INVOICE_LOGO]' => esc_html__('This returns the logo uploaded via the invoice settings page wrapped in img_tags and with a "logo screen" classes. The image size is also set in the img tags automatically to match the uploaded logo.', |
|
93 | - 'event_espresso'), |
|
94 | - '[INVOICE_PAYEE_NAME]' => esc_html__('This will parse to either: the value of the "Company Name" field in the invoice payment method settings; if that is blank, then the value of the Company Name in the "Your Organization Settings", if that is blank then an empty string.', |
|
95 | - 'event_espresso'), |
|
96 | - '[INVOICE_PAYEE_ADDRESS]' => esc_html__('This will parse to either: the value of the "Company Address" field in the invoice payment method settings; if that is blank, then the value of the Company Address in the "Your Organization Settings", if that is blank then an empty string.', |
|
97 | - 'event_espresso'), |
|
98 | - '[INVOICE_PAYMENT_INSTRUCTIONS]' => esc_html__('This will parse to the value of the "Payment Instructions" field found on the Invoice payment methods settings page', |
|
99 | - 'event_espresso'), |
|
100 | - '[INVOICE_PAYEE_EMAIL]' => esc_html__('This will parse to either: the value of the "Company Email" field in the invoice payment method settings; if that is blank, then the value of the Company Email in the "Your Organization Settings", if that is blank then an empty string.', |
|
101 | - 'event_espresso'), |
|
102 | - '[INVOICE_PAYEE_TAX_NUMBER_*]' => sprintf(esc_html__('This will parse to either: the value of the "Company Tax Number" field in the invoice payment method settings; if that is blank, then the value of the Company Tax Number in the "Your Organization Settings", if that is blank then an empty string. Note this is also a special dynamic shortcode. You can use the "prefix" parameter to indicate what text you want to use as a prefix before this tax number. It defaults to "VAT/Tax Number:". To change this prefix you do the following format for this shortcode: %1$s[INVOICE_PAYEE_TAX_NUMBER_* prefix="GST:"]%2$s and that will output: GST: 12345t56. If you have no tax number in your settings, then no prefix will be output either.', |
|
103 | - 'event_espresso'), '<code>', '</code>'), |
|
104 | - '[TOTAL_COST]' => esc_html__('The total cost for the transaction', 'event_espresso'), |
|
105 | - '[TXN_STATUS]' => esc_html__('The transaction status for the transaction.', |
|
106 | - 'event_espresso'), |
|
107 | - '[TXN_STATUS_ID]' => esc_html__('The ID representing the transaction status as saved in the db. This tends to be useful for including with css classes for styling certain statuses differently from others.', |
|
108 | - 'event_espresso'), |
|
109 | - '[PAYMENT_STATUS]' => esc_html__('The transaction status for the transaction. This parses to the same value as the [TXN_STATUS] shortcode and still remains here for legacy support.', |
|
110 | - 'event_espresso'), |
|
111 | - '[PAYMENT_GATEWAY]' => esc_html__('The payment gateway used for the transaction', |
|
112 | - 'event_espresso'), |
|
113 | - '[AMOUNT_PAID]' => esc_html__('The amount paid or refunded. This will only have a value if there was a payment or refund at the time of generating the message.', |
|
114 | - 'event_espresso'), |
|
115 | - '[LAST_AMOUNT_PAID]' => esc_html__('This is the last payment or refund made on the transaction related to the message being generated.', |
|
116 | - 'event_espresso'), |
|
117 | - '[TOTAL_AMOUNT_PAID]' => esc_html__('This parses to the total amount paid over all payments', |
|
118 | - 'event_espresso'), |
|
119 | - '[TOTAL_OWING]' => esc_html__('The total owing on a transaction with no attributes.', |
|
120 | - 'event_espresso'), |
|
121 | - '[TXN_SUBTOTAL]' => esc_html__('The subtotal for all txn line items.', 'event_espresso'), |
|
122 | - '[TXN_TAX_SUBTOTAL]' => esc_html__('The subtotal for all tax line items.', 'event_espresso'), |
|
123 | - '[OWING_STATUS_MESSAGE_*]' => esc_html__('A dynamic shortcode for adjusting how total owing gets shown. The acceptable attributes on the shortcode are:', |
|
124 | - 'event_espresso') |
|
125 | - . '<p></ul>' |
|
126 | - . |
|
127 | - '<li><strong>still_owing</strong>:' |
|
128 | - . esc_html__('If the transaction is not paid in full, then whatever is set for this attribute is shown (otherwise its just the amount owing). The default is:', |
|
129 | - 'event_espresso') |
|
130 | - . sprintf(esc_html__('%sPlease make a payment.%s', 'event_espresso'), |
|
131 | - '<a href="[PAYMENT_URL]" class="noPrint">', '</a>') |
|
132 | - . '</li>' |
|
133 | - . |
|
134 | - '<li><strong>none_owing</strong>:' |
|
135 | - . esc_html__('If the transaction is paid in full, then you can indicate how this gets displayed. Note, that it defaults to just be the total owing.', |
|
136 | - 'event_espresso') |
|
137 | - . '</li></ul></p>', |
|
138 | - '[TXN_TOTAL_TICKETS]' => esc_html__('The total number of all tickets purchased in a transaction', |
|
139 | - 'event_espresso'), |
|
140 | - '[TKT_QTY_PURCHASED]' => sprintf(esc_html__('The total number of all tickets purchased in a transaction. %1$sNOTE: This shortcode is good to use in the "[TICKET_LIST]" field but has been deprecated from all other contexts in favor of the more explicit [TXN_TOTAL_TICKETS] shortcode.%2$s', |
|
141 | - 'event_espresso'), '<strong>', '</strong>'), |
|
142 | - '[TRANSACTION_ADMIN_URL]' => esc_html__('The url to the admin page for this transaction', |
|
143 | - 'event_espresso'), |
|
144 | - '[RECEIPT_URL]' => esc_html__('This parses to the generated url for retrieving the receipt for the transaction', |
|
145 | - 'event_espresso'), |
|
146 | - '[INVOICE_RECEIPT_SWITCHER_URL]' => esc_html__('This parses to the url that will switch to the receipt if an invoice is displayed, and switch to the invoice if receipt is displayed. If a message type OTHER than invoice or receipt is displayed then this will just return the url for the invoice. If the related message type is not active then will parse to an empty string.', |
|
147 | - 'event_espresso'), |
|
148 | - '[INVOICE_RECEIPT_SWITCHER_BUTTON]' => sprintf(esc_html__('The same as %1$s[INVOICE_RECEIPT_SWITCHER_URL]%2$s except this returns the html for a button linked to the invoice or receipt.', |
|
149 | - 'event_espresso'), '<code>', '</code>'), |
|
150 | - ); |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * @access protected |
|
157 | - * @param string $shortcode the shortcode to be parsed. |
|
158 | - * @return string parsed shortcode |
|
159 | - */ |
|
160 | - protected function _parser($shortcode) |
|
161 | - { |
|
162 | - //attempt to get the transaction. Since this is potentially used in more fields, we may have to look in the _extra_data for the transaction. |
|
163 | - $transaction = $this->_data->txn instanceof EE_Transaction ? $this->_data->txn : null; |
|
164 | - $transaction = ! $transaction instanceof EE_Transaction |
|
165 | - && is_array($this->_extra_data) |
|
166 | - && isset($this->_extra_data['data']) |
|
167 | - && $this->_extra_data['data'] instanceof EE_Messages_Addressee ? $this->_extra_data['data']->txn |
|
168 | - : $transaction; |
|
169 | - //payment |
|
170 | - $payment = $this->_data->payment instanceof EE_Payment ? $this->_data->payment : null; |
|
171 | - $payment = ! $payment instanceof EE_Payment |
|
172 | - && is_array($this->_extra_data) |
|
173 | - && isset($this->_extra_data['data']) |
|
174 | - && $this->_extra_data['data'] instanceof EE_Messages_Addressee ? $this->_extra_data['data']->payment |
|
175 | - : $payment; |
|
176 | - if (! $transaction instanceof EE_Transaction) { |
|
177 | - return ''; |
|
178 | - } |
|
179 | - switch ($shortcode) { |
|
180 | - case '[TXN_ID]' : |
|
181 | - return $transaction->ID(); |
|
182 | - break; |
|
183 | - case '[PAYMENT_URL]' : |
|
184 | - $payment_url = $transaction->payment_overview_url(); |
|
185 | - return empty($payment_url) ? esc_html__('http://dummypaymenturlforpreview.com', 'event_espresso') |
|
186 | - : $payment_url; |
|
187 | - break; |
|
188 | - case '[INVOICE_LINK]' : |
|
189 | - $invoice_url = $transaction->invoice_url(); |
|
190 | - $invoice_url = empty($invoice_url) ? 'http://dummyinvoicelinksforpreview.com' : $invoice_url; |
|
191 | - return sprintf(esc_html__('%sClick here for Invoice%s', 'event_espresso'), |
|
192 | - '<a href="' . $invoice_url . '">', '</a>'); |
|
193 | - break; /**/ |
|
194 | - case '[INVOICE_URL]' : |
|
195 | - $invoice_url = $transaction->invoice_url(); |
|
196 | - return empty($invoice_url) ? 'http://dummyinvoicelinksforpreview.com' : $invoice_url; |
|
197 | - break; |
|
198 | - case '[INVOICE_LOGO_URL]' : |
|
199 | - return $this->_get_invoice_logo(); |
|
200 | - break; |
|
201 | - case '[INVOICE_LOGO]' : |
|
202 | - return $this->_get_invoice_logo(true); |
|
203 | - break; |
|
204 | - case '[INVOICE_PAYEE_NAME]' : |
|
205 | - return $this->_get_invoice_payee_name(); |
|
206 | - break; |
|
207 | - case '[INVOICE_PAYEE_ADDRESS]' : |
|
208 | - return $this->_get_invoice_payee_address(); |
|
209 | - break; |
|
210 | - case '[INVOICE_PAYMENT_INSTRUCTIONS]' : |
|
211 | - return $this->_get_invoice_payment_instructions(); |
|
212 | - break; |
|
213 | - case '[INVOICE_PAYEE_EMAIL]' : |
|
214 | - return $this->_get_invoice_payee_email(); |
|
215 | - break; |
|
216 | - case '[TOTAL_COST]' : |
|
217 | - $total = $transaction->total(); |
|
218 | - return ! empty($total) ? EEH_Template::format_currency($total) : ''; |
|
219 | - break; |
|
220 | - case '[PAYMENT_STATUS]' : |
|
221 | - $status = $transaction->pretty_status(); |
|
222 | - return ! empty($status) ? $status : esc_html__('Unknown', 'event_espresso'); |
|
223 | - break; /**/ |
|
224 | - // note the [payment_status] shortcode is kind of misleading because payment status might be different from txn status so I'm adding this here for clarity. |
|
225 | - case '[TXN_STATUS]': |
|
226 | - $status = $transaction->pretty_status(); |
|
227 | - return ! empty($status) ? $status : esc_html__('Unknown', 'event_espresso'); |
|
228 | - break; |
|
229 | - case '[TXN_STATUS_ID]' : |
|
230 | - return $transaction->status_ID(); |
|
231 | - break; |
|
232 | - case '[PAYMENT_GATEWAY]' : |
|
233 | - return $this->_get_payment_gateway($transaction); |
|
234 | - break; |
|
235 | - case '[AMOUNT_PAID]' : |
|
236 | - return $payment instanceof EE_Payment |
|
237 | - ? EEH_Template::format_currency($payment->amount()) |
|
238 | - : EEH_Template::format_currency(0); |
|
239 | - break; |
|
240 | - case '[LAST_AMOUNT_PAID]' : |
|
241 | - $last_payment = $transaction->last_payment(); |
|
242 | - return $last_payment instanceof EE_Payment |
|
243 | - ? EEH_Template::format_currency($last_payment->amount()) |
|
244 | - : EEH_Template::format_currency(0); |
|
245 | - case '[TOTAL_AMOUNT_PAID]' : |
|
246 | - return EEH_Template::format_currency($transaction->paid()); |
|
247 | - break; |
|
248 | - case '[TOTAL_OWING]' : |
|
249 | - $total_owing = $transaction->remaining(); |
|
250 | - return EEH_Template::format_currency($total_owing); |
|
251 | - break; |
|
252 | - case '[TXN_SUBTOTAL]' : |
|
253 | - return EEH_Template::format_currency($this->_get_subtotal()); |
|
254 | - break; |
|
255 | - case '[TXN_TAX_SUBTOTAL]' : |
|
256 | - return EEH_Template::format_currency($this->_get_subtotal(true)); |
|
257 | - break; |
|
258 | - case '[TKT_QTY_PURCHASED]' : |
|
259 | - case '[TXN_TOTAL_TICKETS]' : |
|
260 | - return $this->_data->total_ticket_count; |
|
261 | - break; |
|
262 | - case '[TRANSACTION_ADMIN_URL]' : |
|
263 | - require_once EE_CORE . 'admin/EE_Admin_Page.core.php'; |
|
264 | - $query_args = array( |
|
265 | - 'page' => 'espresso_transactions', |
|
266 | - 'action' => 'view_transaction', |
|
267 | - 'TXN_ID' => $transaction->ID(), |
|
268 | - ); |
|
269 | - $url = EE_Admin_Page::add_query_args_and_nonce($query_args, admin_url('admin.php')); |
|
270 | - return $url; |
|
271 | - break; |
|
272 | - case '[RECEIPT_URL]' : |
|
273 | - //get primary_registration |
|
274 | - $reg = $this->_data->primary_reg_obj; |
|
275 | - if (! $reg instanceof EE_Registration) { |
|
276 | - return ''; |
|
277 | - } |
|
278 | - return $reg->receipt_url(); |
|
279 | - break; |
|
280 | - case '[INVOICE_RECEIPT_SWITCHER_URL]' : |
|
281 | - return $this->_get_invoice_receipt_switcher(false); |
|
282 | - break; |
|
283 | - case '[INVOICE_RECEIPT_SWITCHER_BUTTON]' : |
|
284 | - return $this->_get_invoice_receipt_switcher(); |
|
285 | - break; |
|
286 | - } |
|
287 | - if (strpos($shortcode, '[OWING_STATUS_MESSAGE_*') !== false) { |
|
288 | - return $this->_get_custom_total_owing($shortcode); |
|
289 | - } |
|
290 | - if (strpos($shortcode, '[INVOICE_PAYEE_TAX_NUMBER_*') !== false) { |
|
291 | - return $this->_get_invoice_payee_tax_number($shortcode); |
|
292 | - } |
|
293 | - if (strpos($shortcode, '[PAYMENT_LINK_IF_NEEDED_*') !== false) { |
|
294 | - return $this->_get_payment_link_if_needed($shortcode); |
|
295 | - } |
|
296 | - if (strpos($shortcode, '[PAYMENT_DUE_DATE_*') !== false) { |
|
297 | - return $this->_get_payment_due_date($shortcode, $transaction); |
|
298 | - } |
|
299 | - return ''; |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * parser for the [OWING_STATUS_MESSAGE_*] attribute type shortcode |
|
306 | - * |
|
307 | - * @since 4.5.0 |
|
308 | - * @param string $shortcode the incoming shortcode |
|
309 | - * @return string parsed. |
|
310 | - */ |
|
311 | - private function _get_custom_total_owing($shortcode) |
|
312 | - { |
|
313 | - $valid_shortcodes = array('transaction'); |
|
314 | - $attrs = $this->_get_shortcode_attrs($shortcode); |
|
315 | - //ensure default is set. |
|
316 | - $addressee = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null; |
|
317 | - $total_owing = $addressee instanceof EE_Messages_Addressee && $addressee->txn instanceof EE_Transaction |
|
318 | - ? $addressee->txn->remaining() : 0; |
|
319 | - if ($total_owing > 0) { |
|
320 | - $owing_content = ! empty($attrs['still_owing']) |
|
321 | - ? $attrs['still_owing'] |
|
322 | - : sprintf(esc_html__('%sPlease make a payment.%s', 'event_espresso'), |
|
323 | - '<a href="[PAYMENT_URL]" class="noPrint">', '</a>'); |
|
324 | - $owing_content = $this->_shortcode_helper->parse_message_template($owing_content, $addressee, |
|
325 | - $valid_shortcodes, $this->_message_type, $this->_messenger, $this->_message); |
|
326 | - } else { |
|
327 | - $owing_content = ! empty($attrs['none_owing']) ? $attrs['none_owing'] : ''; |
|
328 | - } |
|
329 | - return $owing_content; |
|
330 | - } |
|
331 | - |
|
332 | - |
|
333 | - |
|
334 | - /** |
|
335 | - * @param EE_Transaction $transaction |
|
336 | - * @return string |
|
337 | - */ |
|
338 | - private function _get_payment_gateway($transaction) |
|
339 | - { |
|
340 | - if($transaction instanceof EE_Transaction){ |
|
341 | - $pm = $transaction->payment_method(); |
|
342 | - } else { |
|
343 | - $pm = null; |
|
344 | - } |
|
345 | - return $pm instanceof EE_Payment_Method ? $pm->name() : ''; |
|
346 | - } |
|
347 | - |
|
348 | - |
|
349 | - |
|
350 | - /** |
|
351 | - * This retrieves a logo to be used for the invoice from whatever is set on the invoice logo settings page. If its |
|
352 | - * not present then the organization logo is used if its found (set on the organization settings page). |
|
353 | - * |
|
354 | - * @since 4.5.0 |
|
355 | - * @param bool $img_tags TRUE means to return with the img tag wrappers. False just returns the url to the image. |
|
356 | - * @return string url or html |
|
357 | - */ |
|
358 | - private function _get_invoice_logo($img_tags = false) |
|
359 | - { |
|
360 | - //try to get the invoice payment method's logo for this transaction image first |
|
361 | - $pm = $this->_get_invoice_payment_method(); |
|
362 | - if ($pm instanceof EE_Payment_Method) { |
|
363 | - $invoice_logo_url = $pm->get_extra_meta('pdf_logo_image', true); |
|
364 | - } else { |
|
365 | - $invoice_logo_url = null; |
|
366 | - } |
|
367 | - if (empty($invoice_logo_url)) { |
|
368 | - $invoice_logo_url = EE_Registry::instance()->CFG->organization->logo_url; |
|
369 | - } |
|
370 | - if (empty($invoice_logo_url)) { |
|
371 | - return ''; |
|
372 | - } |
|
373 | - if (! $img_tags) { |
|
374 | - return $invoice_logo_url; |
|
375 | - } |
|
376 | - //image tags have been requested. |
|
377 | - $image_size = getimagesize($invoice_logo_url); |
|
378 | - //if image is wider than 300px, set the width to 300 |
|
379 | - if ($image_size[0] > 300) { |
|
380 | - $image_width = 300; |
|
381 | - } else { |
|
382 | - $image_width = $image_size[0]; |
|
383 | - } |
|
384 | - return '<img class="logo screen" src="' . $invoice_logo_url . '" width="' . $image_width . '" alt="logo" />'; |
|
385 | - } |
|
386 | - |
|
387 | - |
|
388 | - |
|
389 | - /** |
|
390 | - * Used to retrieve the appropriate content for the invoice payee name shortcode |
|
391 | - * |
|
392 | - * @since 4.5.0 |
|
393 | - * @return string |
|
394 | - */ |
|
395 | - private function _get_invoice_payee_name() |
|
396 | - { |
|
397 | - $payee_name = null; |
|
398 | - $pm = $this->_get_invoice_payment_method(); |
|
399 | - if ($pm instanceof EE_Payment_Method) { |
|
400 | - $payee_name = $pm->get_extra_meta('pdf_payee_name', true); |
|
401 | - } |
|
402 | - $payee_name = empty($payee_name) ? EE_Registry::instance()->CFG->organization->get_pretty('name') : $payee_name; |
|
403 | - return $payee_name; |
|
404 | - } |
|
405 | - |
|
406 | - |
|
407 | - |
|
408 | - /** |
|
409 | - * gets the default invoice payment method, but has a filter so it can be overridden |
|
410 | - * |
|
411 | - * @return \EE_Payment_Method|null |
|
412 | - */ |
|
413 | - private function _get_invoice_payment_method() |
|
414 | - { |
|
415 | - if (! $this->_invoice_pm instanceof EE_Payment_Method) { |
|
416 | - $transaction = $this->_data->txn instanceof EE_Transaction ? $this->_data->txn : null; |
|
417 | - $transaction = ! $transaction instanceof EE_Transaction |
|
418 | - && is_array($this->_extra_data) |
|
419 | - && isset($this->_extra_data['data']) |
|
420 | - && $this->_extra_data['data'] instanceof EE_Messages_Addressee |
|
421 | - ? $this->_extra_data['data']->txn : $transaction; |
|
422 | - //get the invoice payment method, and remember it for the next call too |
|
423 | - $this->_invoice_pm = apply_filters('FHEE__EE_Transaction_Shortcodes__get_payment_method__default', |
|
424 | - EEM_Payment_Method::instance()->get_one_of_type('Invoice'), $transaction); |
|
425 | - } |
|
426 | - return $this->_invoice_pm; |
|
427 | - } |
|
428 | - |
|
429 | - |
|
430 | - |
|
431 | - /** |
|
432 | - * Used to retrieve the appropriate content for the invoice payee email shortcode |
|
433 | - * |
|
434 | - * @since 4.5.0 |
|
435 | - * @return string |
|
436 | - */ |
|
437 | - private function _get_invoice_payee_email() |
|
438 | - { |
|
439 | - $payee_email = null; |
|
440 | - $pm = $this->_get_invoice_payment_method(); |
|
441 | - if ($pm instanceof EE_Payment_Method) { |
|
442 | - $payee_email = $pm->get_extra_meta('pdf_payee_email', true); |
|
443 | - } |
|
444 | - $payee_email = empty($payee_email) ? EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
445 | - : $payee_email; |
|
446 | - return $payee_email; |
|
447 | - } |
|
448 | - |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * Used to retrieve the appropriate content for the invoice payee tax number shortcode |
|
453 | - * |
|
454 | - * @since 4.5.0 |
|
455 | - * @param string $shortcode |
|
456 | - * @return string |
|
457 | - */ |
|
458 | - private function _get_invoice_payee_tax_number($shortcode) |
|
459 | - { |
|
460 | - $payee_tax_number = null; |
|
461 | - $pm = $this->_get_invoice_payment_method(); |
|
462 | - if ($pm instanceof EE_Payment_Method) { |
|
463 | - $payee_tax_number = $pm->get_extra_meta('pdf_payee_tax_number', true); |
|
464 | - } |
|
465 | - $payee_tax_number = empty($payee_tax_number) ? EE_Registry::instance()->CFG->organization->vat |
|
466 | - : $payee_tax_number; |
|
467 | - if (empty($payee_tax_number)) { |
|
468 | - return ''; |
|
469 | - } |
|
470 | - //any attributes? |
|
471 | - $attrs = $this->_get_shortcode_attrs($shortcode); |
|
472 | - //prefix? |
|
473 | - $prefix = isset($attrs['prefix']) ? $attrs['prefix'] : esc_html__('VAT/Tax Number: ', 'event_espresso'); |
|
474 | - return $prefix . $payee_tax_number; |
|
475 | - } |
|
476 | - |
|
477 | - |
|
478 | - |
|
479 | - /** |
|
480 | - * Used to retrieve the appropriate content for the invoice payee address shortcode. |
|
481 | - * |
|
482 | - * @since 4.5.0 |
|
483 | - * @return string |
|
484 | - */ |
|
485 | - private function _get_invoice_payee_address() |
|
486 | - { |
|
487 | - $payee_address = null; |
|
488 | - $pm = $this->_get_invoice_payment_method(); |
|
489 | - if ($pm instanceof EE_Payment_Method) { |
|
490 | - $payee_address = $pm->get_extra_meta('pdf_payee_address', true); |
|
491 | - } |
|
492 | - if (empty($payee_address)) { |
|
493 | - $organization = EE_Registry::instance()->CFG->organization; |
|
494 | - $payee_address = $organization->get_pretty('address_1') . '<br>'; |
|
495 | - $payee_address .= ! empty($organization->address_2) ? $organization->get_pretty('address_2') . '<br>' : ''; |
|
496 | - $payee_address .= $organization->get_pretty('city') . '<br>'; |
|
497 | - //state |
|
498 | - $state = EE_Registry::instance()->load_model('State')->get_one_by_ID($organization->STA_ID); |
|
499 | - $payee_address .= $state instanceof EE_State ? $state->name() : ''; |
|
500 | - //Country |
|
501 | - $payee_address .= ! empty($organization->CNT_ISO) ? ', ' . $organization->CNT_ISO . '<br>' : ''; |
|
502 | - $payee_address .= ! empty($organization->zip) ? $organization->zip : ''; |
|
503 | - } |
|
504 | - return $payee_address; |
|
505 | - } |
|
506 | - |
|
507 | - |
|
508 | - |
|
509 | - /** |
|
510 | - * Used to retrieve the appropriate content for the invoice payment instructions shortcode. |
|
511 | - * |
|
512 | - * @since 4.5.0 |
|
513 | - * @return string |
|
514 | - */ |
|
515 | - private function _get_invoice_payment_instructions() |
|
516 | - { |
|
517 | - $instructions = null; |
|
518 | - $pm = $this->_get_invoice_payment_method(); |
|
519 | - return ($pm instanceof EE_Payment_Method) ? $pm->get_extra_meta('pdf_instructions', true) : ''; |
|
520 | - } |
|
521 | - |
|
522 | - |
|
523 | - |
|
524 | - /** |
|
525 | - * get invoice/receipt switch button or url. |
|
526 | - * |
|
527 | - * @param bool $button true (default) returns the html for a button, false just returns the url. |
|
528 | - * @return string |
|
529 | - */ |
|
530 | - protected function _get_invoice_receipt_switcher($button = true) |
|
531 | - { |
|
532 | - $reg = $this->_data->primary_reg_obj; |
|
533 | - $message_type = isset($this->_extra_data['message_type']) ? $this->_extra_data['message_type'] : ''; |
|
534 | - if (! $reg instanceof EE_Registration || empty($message_type)) { |
|
535 | - return ''; |
|
536 | - } |
|
537 | - $switch_to_invoice = ! $message_type instanceof EE_Invoice_message_type ? true : false; |
|
538 | - $switch_to_label = $switch_to_invoice && ! $message_type instanceof EE_Receipt_message_type |
|
539 | - ? esc_html__('View Invoice', 'event_espresso') : esc_html__('Switch to Invoice', 'event_espresso'); |
|
540 | - $switch_to_label = ! $switch_to_invoice ? esc_html__('Switch to Receipt', 'event_espresso') : $switch_to_label; |
|
541 | - $switch_to_url = $switch_to_invoice ? $reg->invoice_url() : $reg->receipt_url(); |
|
542 | - if (! $button) { |
|
543 | - return $switch_to_url; |
|
544 | - } |
|
545 | - if (! empty($switch_to_url)) { |
|
546 | - return ' |
|
31 | + /** |
|
32 | + * @var EE_Payment_Method $_invoice_pm the invoiec payment method for use in invoices etc |
|
33 | + */ |
|
34 | + protected $_invoice_pm; |
|
35 | + |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * EE_Transaction_Shortcodes constructor. |
|
40 | + */ |
|
41 | + public function __construct() |
|
42 | + { |
|
43 | + parent::__construct(); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + |
|
48 | + protected function _init_props() |
|
49 | + { |
|
50 | + $this->label = esc_html__('Transaction Shortcodes', 'event_espresso'); |
|
51 | + $this->description = esc_html__('All shortcodes specific to transaction related data', 'event_espresso'); |
|
52 | + $this->_shortcodes = array( |
|
53 | + '[TXN_ID]' => esc_html__('The transaction id for the purchase.', 'event_espresso'), |
|
54 | + '[PAYMENT_URL]' => esc_html__('This is a link to make a payment for the event', |
|
55 | + 'event_espresso'), |
|
56 | + '[PAYMENT_LINK_IF_NEEDED_*]' => esc_html__('This is a special dynamic shortcode that allows one to insert a payment link conditional on there being amount owing on the transaction. Three params are available on this shortcode:', |
|
57 | + 'event_espresso') |
|
58 | + . '<ul>' |
|
59 | + . '<li>' |
|
60 | + . sprintf(esc_html__('%sclass:%s This can be used to indicate css class is given to the containing css element (default is "callout").', |
|
61 | + 'event_espresso'), '<strong>', '</strong>') |
|
62 | + . '</li>' |
|
63 | + . '<li>' |
|
64 | + . sprintf(esc_html__('%scustom_text:%s This should be a sprintf format text string (with %%s for where the hyperlink tags go) that is used for the generated link text (The default is "You can %%smake a payment here »%%s.)', |
|
65 | + 'event_espresso'), '<strong>', '</strong>') |
|
66 | + . '</li>' |
|
67 | + . '<li>' |
|
68 | + . sprintf(esc_html__('%scontainer_tag:%s Use this to indicate what container tag you want surrounding the payment link (default is "p").', |
|
69 | + 'event_espresso'), '<strong>', '</strong>') |
|
70 | + . '</li>' |
|
71 | + . '</ul>', |
|
72 | + '[PAYMENT_DUE_DATE_*]' => esc_html__('This is a special dynamic shortcode that allows one to output a payment due date. It will only result in a date shown if there is money owing. Three parameters are available on this shortcode:', |
|
73 | + 'event_espresso') |
|
74 | + . '<ul>' |
|
75 | + . '<li>' |
|
76 | + . sprintf(esc_html__('%sformat:%s This is used to indicate what format the date is in. Default is whatever is set as date formats for your website.', |
|
77 | + 'event_espresso'), '<strong>', '</strong>') |
|
78 | + . '</li>' |
|
79 | + . '<li>' |
|
80 | + . sprintf(esc_html__('%sdays_until_due:%s This is the number of days form the transaction creation date that the payment is due. Defaults to 30.', |
|
81 | + 'event_espresso'), '<strong>', '</strong>') |
|
82 | + . '</li>' |
|
83 | + . '<li>' |
|
84 | + . sprintf(esc_html__('%sprefix_text:%s You can use this to indicate what text will prefix the date string. Defaults to "Payment in full due by:"', |
|
85 | + 'event_espresso'), '<strong>', '</strong>') |
|
86 | + . '</li>', |
|
87 | + '[INVOICE_LINK]' => esc_html__('This is a full html link to the invoice', |
|
88 | + 'event_espresso'), |
|
89 | + '[INVOICE_URL]' => esc_html__('This is just the url for the invoice', 'event_espresso'), |
|
90 | + '[INVOICE_LOGO_URL]' => esc_html__('This returns the url for the logo uploaded via the invoice settings page.', |
|
91 | + 'event_espresso'), |
|
92 | + '[INVOICE_LOGO]' => esc_html__('This returns the logo uploaded via the invoice settings page wrapped in img_tags and with a "logo screen" classes. The image size is also set in the img tags automatically to match the uploaded logo.', |
|
93 | + 'event_espresso'), |
|
94 | + '[INVOICE_PAYEE_NAME]' => esc_html__('This will parse to either: the value of the "Company Name" field in the invoice payment method settings; if that is blank, then the value of the Company Name in the "Your Organization Settings", if that is blank then an empty string.', |
|
95 | + 'event_espresso'), |
|
96 | + '[INVOICE_PAYEE_ADDRESS]' => esc_html__('This will parse to either: the value of the "Company Address" field in the invoice payment method settings; if that is blank, then the value of the Company Address in the "Your Organization Settings", if that is blank then an empty string.', |
|
97 | + 'event_espresso'), |
|
98 | + '[INVOICE_PAYMENT_INSTRUCTIONS]' => esc_html__('This will parse to the value of the "Payment Instructions" field found on the Invoice payment methods settings page', |
|
99 | + 'event_espresso'), |
|
100 | + '[INVOICE_PAYEE_EMAIL]' => esc_html__('This will parse to either: the value of the "Company Email" field in the invoice payment method settings; if that is blank, then the value of the Company Email in the "Your Organization Settings", if that is blank then an empty string.', |
|
101 | + 'event_espresso'), |
|
102 | + '[INVOICE_PAYEE_TAX_NUMBER_*]' => sprintf(esc_html__('This will parse to either: the value of the "Company Tax Number" field in the invoice payment method settings; if that is blank, then the value of the Company Tax Number in the "Your Organization Settings", if that is blank then an empty string. Note this is also a special dynamic shortcode. You can use the "prefix" parameter to indicate what text you want to use as a prefix before this tax number. It defaults to "VAT/Tax Number:". To change this prefix you do the following format for this shortcode: %1$s[INVOICE_PAYEE_TAX_NUMBER_* prefix="GST:"]%2$s and that will output: GST: 12345t56. If you have no tax number in your settings, then no prefix will be output either.', |
|
103 | + 'event_espresso'), '<code>', '</code>'), |
|
104 | + '[TOTAL_COST]' => esc_html__('The total cost for the transaction', 'event_espresso'), |
|
105 | + '[TXN_STATUS]' => esc_html__('The transaction status for the transaction.', |
|
106 | + 'event_espresso'), |
|
107 | + '[TXN_STATUS_ID]' => esc_html__('The ID representing the transaction status as saved in the db. This tends to be useful for including with css classes for styling certain statuses differently from others.', |
|
108 | + 'event_espresso'), |
|
109 | + '[PAYMENT_STATUS]' => esc_html__('The transaction status for the transaction. This parses to the same value as the [TXN_STATUS] shortcode and still remains here for legacy support.', |
|
110 | + 'event_espresso'), |
|
111 | + '[PAYMENT_GATEWAY]' => esc_html__('The payment gateway used for the transaction', |
|
112 | + 'event_espresso'), |
|
113 | + '[AMOUNT_PAID]' => esc_html__('The amount paid or refunded. This will only have a value if there was a payment or refund at the time of generating the message.', |
|
114 | + 'event_espresso'), |
|
115 | + '[LAST_AMOUNT_PAID]' => esc_html__('This is the last payment or refund made on the transaction related to the message being generated.', |
|
116 | + 'event_espresso'), |
|
117 | + '[TOTAL_AMOUNT_PAID]' => esc_html__('This parses to the total amount paid over all payments', |
|
118 | + 'event_espresso'), |
|
119 | + '[TOTAL_OWING]' => esc_html__('The total owing on a transaction with no attributes.', |
|
120 | + 'event_espresso'), |
|
121 | + '[TXN_SUBTOTAL]' => esc_html__('The subtotal for all txn line items.', 'event_espresso'), |
|
122 | + '[TXN_TAX_SUBTOTAL]' => esc_html__('The subtotal for all tax line items.', 'event_espresso'), |
|
123 | + '[OWING_STATUS_MESSAGE_*]' => esc_html__('A dynamic shortcode for adjusting how total owing gets shown. The acceptable attributes on the shortcode are:', |
|
124 | + 'event_espresso') |
|
125 | + . '<p></ul>' |
|
126 | + . |
|
127 | + '<li><strong>still_owing</strong>:' |
|
128 | + . esc_html__('If the transaction is not paid in full, then whatever is set for this attribute is shown (otherwise its just the amount owing). The default is:', |
|
129 | + 'event_espresso') |
|
130 | + . sprintf(esc_html__('%sPlease make a payment.%s', 'event_espresso'), |
|
131 | + '<a href="[PAYMENT_URL]" class="noPrint">', '</a>') |
|
132 | + . '</li>' |
|
133 | + . |
|
134 | + '<li><strong>none_owing</strong>:' |
|
135 | + . esc_html__('If the transaction is paid in full, then you can indicate how this gets displayed. Note, that it defaults to just be the total owing.', |
|
136 | + 'event_espresso') |
|
137 | + . '</li></ul></p>', |
|
138 | + '[TXN_TOTAL_TICKETS]' => esc_html__('The total number of all tickets purchased in a transaction', |
|
139 | + 'event_espresso'), |
|
140 | + '[TKT_QTY_PURCHASED]' => sprintf(esc_html__('The total number of all tickets purchased in a transaction. %1$sNOTE: This shortcode is good to use in the "[TICKET_LIST]" field but has been deprecated from all other contexts in favor of the more explicit [TXN_TOTAL_TICKETS] shortcode.%2$s', |
|
141 | + 'event_espresso'), '<strong>', '</strong>'), |
|
142 | + '[TRANSACTION_ADMIN_URL]' => esc_html__('The url to the admin page for this transaction', |
|
143 | + 'event_espresso'), |
|
144 | + '[RECEIPT_URL]' => esc_html__('This parses to the generated url for retrieving the receipt for the transaction', |
|
145 | + 'event_espresso'), |
|
146 | + '[INVOICE_RECEIPT_SWITCHER_URL]' => esc_html__('This parses to the url that will switch to the receipt if an invoice is displayed, and switch to the invoice if receipt is displayed. If a message type OTHER than invoice or receipt is displayed then this will just return the url for the invoice. If the related message type is not active then will parse to an empty string.', |
|
147 | + 'event_espresso'), |
|
148 | + '[INVOICE_RECEIPT_SWITCHER_BUTTON]' => sprintf(esc_html__('The same as %1$s[INVOICE_RECEIPT_SWITCHER_URL]%2$s except this returns the html for a button linked to the invoice or receipt.', |
|
149 | + 'event_espresso'), '<code>', '</code>'), |
|
150 | + ); |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * @access protected |
|
157 | + * @param string $shortcode the shortcode to be parsed. |
|
158 | + * @return string parsed shortcode |
|
159 | + */ |
|
160 | + protected function _parser($shortcode) |
|
161 | + { |
|
162 | + //attempt to get the transaction. Since this is potentially used in more fields, we may have to look in the _extra_data for the transaction. |
|
163 | + $transaction = $this->_data->txn instanceof EE_Transaction ? $this->_data->txn : null; |
|
164 | + $transaction = ! $transaction instanceof EE_Transaction |
|
165 | + && is_array($this->_extra_data) |
|
166 | + && isset($this->_extra_data['data']) |
|
167 | + && $this->_extra_data['data'] instanceof EE_Messages_Addressee ? $this->_extra_data['data']->txn |
|
168 | + : $transaction; |
|
169 | + //payment |
|
170 | + $payment = $this->_data->payment instanceof EE_Payment ? $this->_data->payment : null; |
|
171 | + $payment = ! $payment instanceof EE_Payment |
|
172 | + && is_array($this->_extra_data) |
|
173 | + && isset($this->_extra_data['data']) |
|
174 | + && $this->_extra_data['data'] instanceof EE_Messages_Addressee ? $this->_extra_data['data']->payment |
|
175 | + : $payment; |
|
176 | + if (! $transaction instanceof EE_Transaction) { |
|
177 | + return ''; |
|
178 | + } |
|
179 | + switch ($shortcode) { |
|
180 | + case '[TXN_ID]' : |
|
181 | + return $transaction->ID(); |
|
182 | + break; |
|
183 | + case '[PAYMENT_URL]' : |
|
184 | + $payment_url = $transaction->payment_overview_url(); |
|
185 | + return empty($payment_url) ? esc_html__('http://dummypaymenturlforpreview.com', 'event_espresso') |
|
186 | + : $payment_url; |
|
187 | + break; |
|
188 | + case '[INVOICE_LINK]' : |
|
189 | + $invoice_url = $transaction->invoice_url(); |
|
190 | + $invoice_url = empty($invoice_url) ? 'http://dummyinvoicelinksforpreview.com' : $invoice_url; |
|
191 | + return sprintf(esc_html__('%sClick here for Invoice%s', 'event_espresso'), |
|
192 | + '<a href="' . $invoice_url . '">', '</a>'); |
|
193 | + break; /**/ |
|
194 | + case '[INVOICE_URL]' : |
|
195 | + $invoice_url = $transaction->invoice_url(); |
|
196 | + return empty($invoice_url) ? 'http://dummyinvoicelinksforpreview.com' : $invoice_url; |
|
197 | + break; |
|
198 | + case '[INVOICE_LOGO_URL]' : |
|
199 | + return $this->_get_invoice_logo(); |
|
200 | + break; |
|
201 | + case '[INVOICE_LOGO]' : |
|
202 | + return $this->_get_invoice_logo(true); |
|
203 | + break; |
|
204 | + case '[INVOICE_PAYEE_NAME]' : |
|
205 | + return $this->_get_invoice_payee_name(); |
|
206 | + break; |
|
207 | + case '[INVOICE_PAYEE_ADDRESS]' : |
|
208 | + return $this->_get_invoice_payee_address(); |
|
209 | + break; |
|
210 | + case '[INVOICE_PAYMENT_INSTRUCTIONS]' : |
|
211 | + return $this->_get_invoice_payment_instructions(); |
|
212 | + break; |
|
213 | + case '[INVOICE_PAYEE_EMAIL]' : |
|
214 | + return $this->_get_invoice_payee_email(); |
|
215 | + break; |
|
216 | + case '[TOTAL_COST]' : |
|
217 | + $total = $transaction->total(); |
|
218 | + return ! empty($total) ? EEH_Template::format_currency($total) : ''; |
|
219 | + break; |
|
220 | + case '[PAYMENT_STATUS]' : |
|
221 | + $status = $transaction->pretty_status(); |
|
222 | + return ! empty($status) ? $status : esc_html__('Unknown', 'event_espresso'); |
|
223 | + break; /**/ |
|
224 | + // note the [payment_status] shortcode is kind of misleading because payment status might be different from txn status so I'm adding this here for clarity. |
|
225 | + case '[TXN_STATUS]': |
|
226 | + $status = $transaction->pretty_status(); |
|
227 | + return ! empty($status) ? $status : esc_html__('Unknown', 'event_espresso'); |
|
228 | + break; |
|
229 | + case '[TXN_STATUS_ID]' : |
|
230 | + return $transaction->status_ID(); |
|
231 | + break; |
|
232 | + case '[PAYMENT_GATEWAY]' : |
|
233 | + return $this->_get_payment_gateway($transaction); |
|
234 | + break; |
|
235 | + case '[AMOUNT_PAID]' : |
|
236 | + return $payment instanceof EE_Payment |
|
237 | + ? EEH_Template::format_currency($payment->amount()) |
|
238 | + : EEH_Template::format_currency(0); |
|
239 | + break; |
|
240 | + case '[LAST_AMOUNT_PAID]' : |
|
241 | + $last_payment = $transaction->last_payment(); |
|
242 | + return $last_payment instanceof EE_Payment |
|
243 | + ? EEH_Template::format_currency($last_payment->amount()) |
|
244 | + : EEH_Template::format_currency(0); |
|
245 | + case '[TOTAL_AMOUNT_PAID]' : |
|
246 | + return EEH_Template::format_currency($transaction->paid()); |
|
247 | + break; |
|
248 | + case '[TOTAL_OWING]' : |
|
249 | + $total_owing = $transaction->remaining(); |
|
250 | + return EEH_Template::format_currency($total_owing); |
|
251 | + break; |
|
252 | + case '[TXN_SUBTOTAL]' : |
|
253 | + return EEH_Template::format_currency($this->_get_subtotal()); |
|
254 | + break; |
|
255 | + case '[TXN_TAX_SUBTOTAL]' : |
|
256 | + return EEH_Template::format_currency($this->_get_subtotal(true)); |
|
257 | + break; |
|
258 | + case '[TKT_QTY_PURCHASED]' : |
|
259 | + case '[TXN_TOTAL_TICKETS]' : |
|
260 | + return $this->_data->total_ticket_count; |
|
261 | + break; |
|
262 | + case '[TRANSACTION_ADMIN_URL]' : |
|
263 | + require_once EE_CORE . 'admin/EE_Admin_Page.core.php'; |
|
264 | + $query_args = array( |
|
265 | + 'page' => 'espresso_transactions', |
|
266 | + 'action' => 'view_transaction', |
|
267 | + 'TXN_ID' => $transaction->ID(), |
|
268 | + ); |
|
269 | + $url = EE_Admin_Page::add_query_args_and_nonce($query_args, admin_url('admin.php')); |
|
270 | + return $url; |
|
271 | + break; |
|
272 | + case '[RECEIPT_URL]' : |
|
273 | + //get primary_registration |
|
274 | + $reg = $this->_data->primary_reg_obj; |
|
275 | + if (! $reg instanceof EE_Registration) { |
|
276 | + return ''; |
|
277 | + } |
|
278 | + return $reg->receipt_url(); |
|
279 | + break; |
|
280 | + case '[INVOICE_RECEIPT_SWITCHER_URL]' : |
|
281 | + return $this->_get_invoice_receipt_switcher(false); |
|
282 | + break; |
|
283 | + case '[INVOICE_RECEIPT_SWITCHER_BUTTON]' : |
|
284 | + return $this->_get_invoice_receipt_switcher(); |
|
285 | + break; |
|
286 | + } |
|
287 | + if (strpos($shortcode, '[OWING_STATUS_MESSAGE_*') !== false) { |
|
288 | + return $this->_get_custom_total_owing($shortcode); |
|
289 | + } |
|
290 | + if (strpos($shortcode, '[INVOICE_PAYEE_TAX_NUMBER_*') !== false) { |
|
291 | + return $this->_get_invoice_payee_tax_number($shortcode); |
|
292 | + } |
|
293 | + if (strpos($shortcode, '[PAYMENT_LINK_IF_NEEDED_*') !== false) { |
|
294 | + return $this->_get_payment_link_if_needed($shortcode); |
|
295 | + } |
|
296 | + if (strpos($shortcode, '[PAYMENT_DUE_DATE_*') !== false) { |
|
297 | + return $this->_get_payment_due_date($shortcode, $transaction); |
|
298 | + } |
|
299 | + return ''; |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * parser for the [OWING_STATUS_MESSAGE_*] attribute type shortcode |
|
306 | + * |
|
307 | + * @since 4.5.0 |
|
308 | + * @param string $shortcode the incoming shortcode |
|
309 | + * @return string parsed. |
|
310 | + */ |
|
311 | + private function _get_custom_total_owing($shortcode) |
|
312 | + { |
|
313 | + $valid_shortcodes = array('transaction'); |
|
314 | + $attrs = $this->_get_shortcode_attrs($shortcode); |
|
315 | + //ensure default is set. |
|
316 | + $addressee = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null; |
|
317 | + $total_owing = $addressee instanceof EE_Messages_Addressee && $addressee->txn instanceof EE_Transaction |
|
318 | + ? $addressee->txn->remaining() : 0; |
|
319 | + if ($total_owing > 0) { |
|
320 | + $owing_content = ! empty($attrs['still_owing']) |
|
321 | + ? $attrs['still_owing'] |
|
322 | + : sprintf(esc_html__('%sPlease make a payment.%s', 'event_espresso'), |
|
323 | + '<a href="[PAYMENT_URL]" class="noPrint">', '</a>'); |
|
324 | + $owing_content = $this->_shortcode_helper->parse_message_template($owing_content, $addressee, |
|
325 | + $valid_shortcodes, $this->_message_type, $this->_messenger, $this->_message); |
|
326 | + } else { |
|
327 | + $owing_content = ! empty($attrs['none_owing']) ? $attrs['none_owing'] : ''; |
|
328 | + } |
|
329 | + return $owing_content; |
|
330 | + } |
|
331 | + |
|
332 | + |
|
333 | + |
|
334 | + /** |
|
335 | + * @param EE_Transaction $transaction |
|
336 | + * @return string |
|
337 | + */ |
|
338 | + private function _get_payment_gateway($transaction) |
|
339 | + { |
|
340 | + if($transaction instanceof EE_Transaction){ |
|
341 | + $pm = $transaction->payment_method(); |
|
342 | + } else { |
|
343 | + $pm = null; |
|
344 | + } |
|
345 | + return $pm instanceof EE_Payment_Method ? $pm->name() : ''; |
|
346 | + } |
|
347 | + |
|
348 | + |
|
349 | + |
|
350 | + /** |
|
351 | + * This retrieves a logo to be used for the invoice from whatever is set on the invoice logo settings page. If its |
|
352 | + * not present then the organization logo is used if its found (set on the organization settings page). |
|
353 | + * |
|
354 | + * @since 4.5.0 |
|
355 | + * @param bool $img_tags TRUE means to return with the img tag wrappers. False just returns the url to the image. |
|
356 | + * @return string url or html |
|
357 | + */ |
|
358 | + private function _get_invoice_logo($img_tags = false) |
|
359 | + { |
|
360 | + //try to get the invoice payment method's logo for this transaction image first |
|
361 | + $pm = $this->_get_invoice_payment_method(); |
|
362 | + if ($pm instanceof EE_Payment_Method) { |
|
363 | + $invoice_logo_url = $pm->get_extra_meta('pdf_logo_image', true); |
|
364 | + } else { |
|
365 | + $invoice_logo_url = null; |
|
366 | + } |
|
367 | + if (empty($invoice_logo_url)) { |
|
368 | + $invoice_logo_url = EE_Registry::instance()->CFG->organization->logo_url; |
|
369 | + } |
|
370 | + if (empty($invoice_logo_url)) { |
|
371 | + return ''; |
|
372 | + } |
|
373 | + if (! $img_tags) { |
|
374 | + return $invoice_logo_url; |
|
375 | + } |
|
376 | + //image tags have been requested. |
|
377 | + $image_size = getimagesize($invoice_logo_url); |
|
378 | + //if image is wider than 300px, set the width to 300 |
|
379 | + if ($image_size[0] > 300) { |
|
380 | + $image_width = 300; |
|
381 | + } else { |
|
382 | + $image_width = $image_size[0]; |
|
383 | + } |
|
384 | + return '<img class="logo screen" src="' . $invoice_logo_url . '" width="' . $image_width . '" alt="logo" />'; |
|
385 | + } |
|
386 | + |
|
387 | + |
|
388 | + |
|
389 | + /** |
|
390 | + * Used to retrieve the appropriate content for the invoice payee name shortcode |
|
391 | + * |
|
392 | + * @since 4.5.0 |
|
393 | + * @return string |
|
394 | + */ |
|
395 | + private function _get_invoice_payee_name() |
|
396 | + { |
|
397 | + $payee_name = null; |
|
398 | + $pm = $this->_get_invoice_payment_method(); |
|
399 | + if ($pm instanceof EE_Payment_Method) { |
|
400 | + $payee_name = $pm->get_extra_meta('pdf_payee_name', true); |
|
401 | + } |
|
402 | + $payee_name = empty($payee_name) ? EE_Registry::instance()->CFG->organization->get_pretty('name') : $payee_name; |
|
403 | + return $payee_name; |
|
404 | + } |
|
405 | + |
|
406 | + |
|
407 | + |
|
408 | + /** |
|
409 | + * gets the default invoice payment method, but has a filter so it can be overridden |
|
410 | + * |
|
411 | + * @return \EE_Payment_Method|null |
|
412 | + */ |
|
413 | + private function _get_invoice_payment_method() |
|
414 | + { |
|
415 | + if (! $this->_invoice_pm instanceof EE_Payment_Method) { |
|
416 | + $transaction = $this->_data->txn instanceof EE_Transaction ? $this->_data->txn : null; |
|
417 | + $transaction = ! $transaction instanceof EE_Transaction |
|
418 | + && is_array($this->_extra_data) |
|
419 | + && isset($this->_extra_data['data']) |
|
420 | + && $this->_extra_data['data'] instanceof EE_Messages_Addressee |
|
421 | + ? $this->_extra_data['data']->txn : $transaction; |
|
422 | + //get the invoice payment method, and remember it for the next call too |
|
423 | + $this->_invoice_pm = apply_filters('FHEE__EE_Transaction_Shortcodes__get_payment_method__default', |
|
424 | + EEM_Payment_Method::instance()->get_one_of_type('Invoice'), $transaction); |
|
425 | + } |
|
426 | + return $this->_invoice_pm; |
|
427 | + } |
|
428 | + |
|
429 | + |
|
430 | + |
|
431 | + /** |
|
432 | + * Used to retrieve the appropriate content for the invoice payee email shortcode |
|
433 | + * |
|
434 | + * @since 4.5.0 |
|
435 | + * @return string |
|
436 | + */ |
|
437 | + private function _get_invoice_payee_email() |
|
438 | + { |
|
439 | + $payee_email = null; |
|
440 | + $pm = $this->_get_invoice_payment_method(); |
|
441 | + if ($pm instanceof EE_Payment_Method) { |
|
442 | + $payee_email = $pm->get_extra_meta('pdf_payee_email', true); |
|
443 | + } |
|
444 | + $payee_email = empty($payee_email) ? EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
445 | + : $payee_email; |
|
446 | + return $payee_email; |
|
447 | + } |
|
448 | + |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * Used to retrieve the appropriate content for the invoice payee tax number shortcode |
|
453 | + * |
|
454 | + * @since 4.5.0 |
|
455 | + * @param string $shortcode |
|
456 | + * @return string |
|
457 | + */ |
|
458 | + private function _get_invoice_payee_tax_number($shortcode) |
|
459 | + { |
|
460 | + $payee_tax_number = null; |
|
461 | + $pm = $this->_get_invoice_payment_method(); |
|
462 | + if ($pm instanceof EE_Payment_Method) { |
|
463 | + $payee_tax_number = $pm->get_extra_meta('pdf_payee_tax_number', true); |
|
464 | + } |
|
465 | + $payee_tax_number = empty($payee_tax_number) ? EE_Registry::instance()->CFG->organization->vat |
|
466 | + : $payee_tax_number; |
|
467 | + if (empty($payee_tax_number)) { |
|
468 | + return ''; |
|
469 | + } |
|
470 | + //any attributes? |
|
471 | + $attrs = $this->_get_shortcode_attrs($shortcode); |
|
472 | + //prefix? |
|
473 | + $prefix = isset($attrs['prefix']) ? $attrs['prefix'] : esc_html__('VAT/Tax Number: ', 'event_espresso'); |
|
474 | + return $prefix . $payee_tax_number; |
|
475 | + } |
|
476 | + |
|
477 | + |
|
478 | + |
|
479 | + /** |
|
480 | + * Used to retrieve the appropriate content for the invoice payee address shortcode. |
|
481 | + * |
|
482 | + * @since 4.5.0 |
|
483 | + * @return string |
|
484 | + */ |
|
485 | + private function _get_invoice_payee_address() |
|
486 | + { |
|
487 | + $payee_address = null; |
|
488 | + $pm = $this->_get_invoice_payment_method(); |
|
489 | + if ($pm instanceof EE_Payment_Method) { |
|
490 | + $payee_address = $pm->get_extra_meta('pdf_payee_address', true); |
|
491 | + } |
|
492 | + if (empty($payee_address)) { |
|
493 | + $organization = EE_Registry::instance()->CFG->organization; |
|
494 | + $payee_address = $organization->get_pretty('address_1') . '<br>'; |
|
495 | + $payee_address .= ! empty($organization->address_2) ? $organization->get_pretty('address_2') . '<br>' : ''; |
|
496 | + $payee_address .= $organization->get_pretty('city') . '<br>'; |
|
497 | + //state |
|
498 | + $state = EE_Registry::instance()->load_model('State')->get_one_by_ID($organization->STA_ID); |
|
499 | + $payee_address .= $state instanceof EE_State ? $state->name() : ''; |
|
500 | + //Country |
|
501 | + $payee_address .= ! empty($organization->CNT_ISO) ? ', ' . $organization->CNT_ISO . '<br>' : ''; |
|
502 | + $payee_address .= ! empty($organization->zip) ? $organization->zip : ''; |
|
503 | + } |
|
504 | + return $payee_address; |
|
505 | + } |
|
506 | + |
|
507 | + |
|
508 | + |
|
509 | + /** |
|
510 | + * Used to retrieve the appropriate content for the invoice payment instructions shortcode. |
|
511 | + * |
|
512 | + * @since 4.5.0 |
|
513 | + * @return string |
|
514 | + */ |
|
515 | + private function _get_invoice_payment_instructions() |
|
516 | + { |
|
517 | + $instructions = null; |
|
518 | + $pm = $this->_get_invoice_payment_method(); |
|
519 | + return ($pm instanceof EE_Payment_Method) ? $pm->get_extra_meta('pdf_instructions', true) : ''; |
|
520 | + } |
|
521 | + |
|
522 | + |
|
523 | + |
|
524 | + /** |
|
525 | + * get invoice/receipt switch button or url. |
|
526 | + * |
|
527 | + * @param bool $button true (default) returns the html for a button, false just returns the url. |
|
528 | + * @return string |
|
529 | + */ |
|
530 | + protected function _get_invoice_receipt_switcher($button = true) |
|
531 | + { |
|
532 | + $reg = $this->_data->primary_reg_obj; |
|
533 | + $message_type = isset($this->_extra_data['message_type']) ? $this->_extra_data['message_type'] : ''; |
|
534 | + if (! $reg instanceof EE_Registration || empty($message_type)) { |
|
535 | + return ''; |
|
536 | + } |
|
537 | + $switch_to_invoice = ! $message_type instanceof EE_Invoice_message_type ? true : false; |
|
538 | + $switch_to_label = $switch_to_invoice && ! $message_type instanceof EE_Receipt_message_type |
|
539 | + ? esc_html__('View Invoice', 'event_espresso') : esc_html__('Switch to Invoice', 'event_espresso'); |
|
540 | + $switch_to_label = ! $switch_to_invoice ? esc_html__('Switch to Receipt', 'event_espresso') : $switch_to_label; |
|
541 | + $switch_to_url = $switch_to_invoice ? $reg->invoice_url() : $reg->receipt_url(); |
|
542 | + if (! $button) { |
|
543 | + return $switch_to_url; |
|
544 | + } |
|
545 | + if (! empty($switch_to_url)) { |
|
546 | + return ' |
|
547 | 547 | <form method="post" action="' . $switch_to_url . '" > |
548 | 548 | <input class="print_button" type="submit" value="' . $switch_to_label . '" /> |
549 | 549 | </form> |
550 | 550 | '; |
551 | - } |
|
552 | - return ''; |
|
553 | - } |
|
554 | - |
|
555 | - |
|
556 | - |
|
557 | - /** |
|
558 | - * This returns a subtotal. |
|
559 | - * |
|
560 | - * @param bool $tax if true then return the subtotal for tax otherwise return subtotal. |
|
561 | - * @return int |
|
562 | - */ |
|
563 | - private function _get_subtotal($tax = false) |
|
564 | - { |
|
565 | - $grand_total = isset($this->_data->grand_total_line_item) ? $this->_data->grand_total_line_item : null; |
|
566 | - if (! $grand_total instanceof EE_Line_Item) { |
|
567 | - return 0; |
|
568 | - } |
|
569 | - return $tax ? $grand_total->get_total_tax() : $grand_total->get_items_total(); |
|
570 | - } |
|
571 | - |
|
572 | - |
|
573 | - |
|
574 | - /** |
|
575 | - * parser for the [PAYMENT_LINK_IF_NEEDED_*] attribute type shortcode |
|
576 | - * |
|
577 | - * @since 4.7.0 |
|
578 | - * @param string $shortcode the incoming shortcode |
|
579 | - * @return string parsed. |
|
580 | - */ |
|
581 | - private function _get_payment_link_if_needed($shortcode) |
|
582 | - { |
|
583 | - $valid_shortcodes = array('transaction'); |
|
584 | - $attrs = $this->_get_shortcode_attrs($shortcode); |
|
585 | - //ensure default is set. |
|
586 | - $addressee = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null; |
|
587 | - $total_owing = $addressee instanceof EE_Messages_Addressee && $addressee->txn instanceof EE_Transaction |
|
588 | - ? $addressee->txn->remaining() : 0; |
|
589 | - if ($total_owing > 0) { |
|
590 | - $class = isset($attrs['class']) ? $attrs['class'] : 'callout'; |
|
591 | - $custom_text = isset($attrs['custom_text']) ? $attrs['custom_text'] : 'You can %smake a payment here »%s.'; |
|
592 | - $container_tag = isset($attrs['container_tag']) ? $attrs['container_tag'] : 'p'; |
|
593 | - $opening_tag = ! empty($container_tag) ? '<' . $container_tag : ''; |
|
594 | - $opening_tag .= ! empty($opening_tag) && ! empty($class) ? ' class="' . $class . '"' : $opening_tag; |
|
595 | - $opening_tag .= ! empty($opening_tag) ? '>' : $opening_tag; |
|
596 | - $closing_tag = ! empty($container_tag) ? '</' . $container_tag . '>' : ''; |
|
597 | - $content = $opening_tag . sprintf($custom_text, '<a href="[PAYMENT_URL]">', '</a>') . $closing_tag; |
|
598 | - //we need to re run this string through the parser to catch any shortcodes that are in it. |
|
599 | - $owing_content = $this->_shortcode_helper->parse_message_template($content, $addressee, $valid_shortcodes, |
|
600 | - $this->_message_type, $this->_messenger, $this->_message); |
|
601 | - } else { |
|
602 | - return ''; |
|
603 | - } |
|
604 | - return $owing_content; |
|
605 | - } |
|
606 | - |
|
607 | - |
|
608 | - |
|
609 | - /** |
|
610 | - * Parser for the [PAYMENT_DUE_DATE_*] attribute type shortcode |
|
611 | - * |
|
612 | - * @since 4.8.28.rc.011 |
|
613 | - * @param string $shortcode The shortcode being parsed. |
|
614 | - * @param EE_Transaction $transaction |
|
615 | - * @return string |
|
616 | - */ |
|
617 | - protected function _get_payment_due_date($shortcode, EE_Transaction $transaction) |
|
618 | - { |
|
619 | - //if transaction is paid in full then we can just return an empty string |
|
620 | - if ($transaction->remaining() === 0) { |
|
621 | - return ''; |
|
622 | - } |
|
623 | - $attrs = $this->_get_shortcode_attrs($shortcode); |
|
624 | - $format = isset($attrs['format']) ? $attrs['format'] : get_option('date_format'); |
|
625 | - $days_until_due = isset($attrs['days_until_due']) ? (int)$attrs['days_until_due'] : 30; |
|
626 | - $prefix_text = isset($attrs['prefix_text']) ? $attrs['prefix_text'] |
|
627 | - : esc_html__('Payment in full due by: ', 'event_espresso'); |
|
628 | - $transaction_created = $transaction->get_DateTime_object('TXN_timestamp'); |
|
629 | - //setup date due: |
|
630 | - try { |
|
631 | - if ($transaction_created instanceof DateTime) { |
|
632 | - $date_due = $transaction_created->add(new DateInterval('P' . $days_until_due . 'D'))->format($format); |
|
633 | - } else { |
|
634 | - throw new Exception(); |
|
635 | - } |
|
636 | - } catch (Exception $e) { |
|
637 | - //format was likely invalid. |
|
638 | - $date_due = 'Unable to calculate date due, likely the format string is invalid.'; |
|
639 | - } |
|
640 | - return $prefix_text . $date_due; |
|
641 | - } |
|
551 | + } |
|
552 | + return ''; |
|
553 | + } |
|
554 | + |
|
555 | + |
|
556 | + |
|
557 | + /** |
|
558 | + * This returns a subtotal. |
|
559 | + * |
|
560 | + * @param bool $tax if true then return the subtotal for tax otherwise return subtotal. |
|
561 | + * @return int |
|
562 | + */ |
|
563 | + private function _get_subtotal($tax = false) |
|
564 | + { |
|
565 | + $grand_total = isset($this->_data->grand_total_line_item) ? $this->_data->grand_total_line_item : null; |
|
566 | + if (! $grand_total instanceof EE_Line_Item) { |
|
567 | + return 0; |
|
568 | + } |
|
569 | + return $tax ? $grand_total->get_total_tax() : $grand_total->get_items_total(); |
|
570 | + } |
|
571 | + |
|
572 | + |
|
573 | + |
|
574 | + /** |
|
575 | + * parser for the [PAYMENT_LINK_IF_NEEDED_*] attribute type shortcode |
|
576 | + * |
|
577 | + * @since 4.7.0 |
|
578 | + * @param string $shortcode the incoming shortcode |
|
579 | + * @return string parsed. |
|
580 | + */ |
|
581 | + private function _get_payment_link_if_needed($shortcode) |
|
582 | + { |
|
583 | + $valid_shortcodes = array('transaction'); |
|
584 | + $attrs = $this->_get_shortcode_attrs($shortcode); |
|
585 | + //ensure default is set. |
|
586 | + $addressee = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null; |
|
587 | + $total_owing = $addressee instanceof EE_Messages_Addressee && $addressee->txn instanceof EE_Transaction |
|
588 | + ? $addressee->txn->remaining() : 0; |
|
589 | + if ($total_owing > 0) { |
|
590 | + $class = isset($attrs['class']) ? $attrs['class'] : 'callout'; |
|
591 | + $custom_text = isset($attrs['custom_text']) ? $attrs['custom_text'] : 'You can %smake a payment here »%s.'; |
|
592 | + $container_tag = isset($attrs['container_tag']) ? $attrs['container_tag'] : 'p'; |
|
593 | + $opening_tag = ! empty($container_tag) ? '<' . $container_tag : ''; |
|
594 | + $opening_tag .= ! empty($opening_tag) && ! empty($class) ? ' class="' . $class . '"' : $opening_tag; |
|
595 | + $opening_tag .= ! empty($opening_tag) ? '>' : $opening_tag; |
|
596 | + $closing_tag = ! empty($container_tag) ? '</' . $container_tag . '>' : ''; |
|
597 | + $content = $opening_tag . sprintf($custom_text, '<a href="[PAYMENT_URL]">', '</a>') . $closing_tag; |
|
598 | + //we need to re run this string through the parser to catch any shortcodes that are in it. |
|
599 | + $owing_content = $this->_shortcode_helper->parse_message_template($content, $addressee, $valid_shortcodes, |
|
600 | + $this->_message_type, $this->_messenger, $this->_message); |
|
601 | + } else { |
|
602 | + return ''; |
|
603 | + } |
|
604 | + return $owing_content; |
|
605 | + } |
|
606 | + |
|
607 | + |
|
608 | + |
|
609 | + /** |
|
610 | + * Parser for the [PAYMENT_DUE_DATE_*] attribute type shortcode |
|
611 | + * |
|
612 | + * @since 4.8.28.rc.011 |
|
613 | + * @param string $shortcode The shortcode being parsed. |
|
614 | + * @param EE_Transaction $transaction |
|
615 | + * @return string |
|
616 | + */ |
|
617 | + protected function _get_payment_due_date($shortcode, EE_Transaction $transaction) |
|
618 | + { |
|
619 | + //if transaction is paid in full then we can just return an empty string |
|
620 | + if ($transaction->remaining() === 0) { |
|
621 | + return ''; |
|
622 | + } |
|
623 | + $attrs = $this->_get_shortcode_attrs($shortcode); |
|
624 | + $format = isset($attrs['format']) ? $attrs['format'] : get_option('date_format'); |
|
625 | + $days_until_due = isset($attrs['days_until_due']) ? (int)$attrs['days_until_due'] : 30; |
|
626 | + $prefix_text = isset($attrs['prefix_text']) ? $attrs['prefix_text'] |
|
627 | + : esc_html__('Payment in full due by: ', 'event_espresso'); |
|
628 | + $transaction_created = $transaction->get_DateTime_object('TXN_timestamp'); |
|
629 | + //setup date due: |
|
630 | + try { |
|
631 | + if ($transaction_created instanceof DateTime) { |
|
632 | + $date_due = $transaction_created->add(new DateInterval('P' . $days_until_due . 'D'))->format($format); |
|
633 | + } else { |
|
634 | + throw new Exception(); |
|
635 | + } |
|
636 | + } catch (Exception $e) { |
|
637 | + //format was likely invalid. |
|
638 | + $date_due = 'Unable to calculate date due, likely the format string is invalid.'; |
|
639 | + } |
|
640 | + return $prefix_text . $date_due; |
|
641 | + } |
|
642 | 642 | |
643 | 643 | } //end EE_Transaction Shortcodes library |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
2 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
3 | 3 | exit('NO direct script access allowed'); |
4 | 4 | } |
5 | 5 | |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | && isset($this->_extra_data['data']) |
174 | 174 | && $this->_extra_data['data'] instanceof EE_Messages_Addressee ? $this->_extra_data['data']->payment |
175 | 175 | : $payment; |
176 | - if (! $transaction instanceof EE_Transaction) { |
|
176 | + if ( ! $transaction instanceof EE_Transaction) { |
|
177 | 177 | return ''; |
178 | 178 | } |
179 | 179 | switch ($shortcode) { |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | $invoice_url = $transaction->invoice_url(); |
190 | 190 | $invoice_url = empty($invoice_url) ? 'http://dummyinvoicelinksforpreview.com' : $invoice_url; |
191 | 191 | return sprintf(esc_html__('%sClick here for Invoice%s', 'event_espresso'), |
192 | - '<a href="' . $invoice_url . '">', '</a>'); |
|
192 | + '<a href="'.$invoice_url.'">', '</a>'); |
|
193 | 193 | break; /**/ |
194 | 194 | case '[INVOICE_URL]' : |
195 | 195 | $invoice_url = $transaction->invoice_url(); |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | return $this->_data->total_ticket_count; |
261 | 261 | break; |
262 | 262 | case '[TRANSACTION_ADMIN_URL]' : |
263 | - require_once EE_CORE . 'admin/EE_Admin_Page.core.php'; |
|
263 | + require_once EE_CORE.'admin/EE_Admin_Page.core.php'; |
|
264 | 264 | $query_args = array( |
265 | 265 | 'page' => 'espresso_transactions', |
266 | 266 | 'action' => 'view_transaction', |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | case '[RECEIPT_URL]' : |
273 | 273 | //get primary_registration |
274 | 274 | $reg = $this->_data->primary_reg_obj; |
275 | - if (! $reg instanceof EE_Registration) { |
|
275 | + if ( ! $reg instanceof EE_Registration) { |
|
276 | 276 | return ''; |
277 | 277 | } |
278 | 278 | return $reg->receipt_url(); |
@@ -337,7 +337,7 @@ discard block |
||
337 | 337 | */ |
338 | 338 | private function _get_payment_gateway($transaction) |
339 | 339 | { |
340 | - if($transaction instanceof EE_Transaction){ |
|
340 | + if ($transaction instanceof EE_Transaction) { |
|
341 | 341 | $pm = $transaction->payment_method(); |
342 | 342 | } else { |
343 | 343 | $pm = null; |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | if (empty($invoice_logo_url)) { |
371 | 371 | return ''; |
372 | 372 | } |
373 | - if (! $img_tags) { |
|
373 | + if ( ! $img_tags) { |
|
374 | 374 | return $invoice_logo_url; |
375 | 375 | } |
376 | 376 | //image tags have been requested. |
@@ -381,7 +381,7 @@ discard block |
||
381 | 381 | } else { |
382 | 382 | $image_width = $image_size[0]; |
383 | 383 | } |
384 | - return '<img class="logo screen" src="' . $invoice_logo_url . '" width="' . $image_width . '" alt="logo" />'; |
|
384 | + return '<img class="logo screen" src="'.$invoice_logo_url.'" width="'.$image_width.'" alt="logo" />'; |
|
385 | 385 | } |
386 | 386 | |
387 | 387 | |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | */ |
413 | 413 | private function _get_invoice_payment_method() |
414 | 414 | { |
415 | - if (! $this->_invoice_pm instanceof EE_Payment_Method) { |
|
415 | + if ( ! $this->_invoice_pm instanceof EE_Payment_Method) { |
|
416 | 416 | $transaction = $this->_data->txn instanceof EE_Transaction ? $this->_data->txn : null; |
417 | 417 | $transaction = ! $transaction instanceof EE_Transaction |
418 | 418 | && is_array($this->_extra_data) |
@@ -471,7 +471,7 @@ discard block |
||
471 | 471 | $attrs = $this->_get_shortcode_attrs($shortcode); |
472 | 472 | //prefix? |
473 | 473 | $prefix = isset($attrs['prefix']) ? $attrs['prefix'] : esc_html__('VAT/Tax Number: ', 'event_espresso'); |
474 | - return $prefix . $payee_tax_number; |
|
474 | + return $prefix.$payee_tax_number; |
|
475 | 475 | } |
476 | 476 | |
477 | 477 | |
@@ -491,14 +491,14 @@ discard block |
||
491 | 491 | } |
492 | 492 | if (empty($payee_address)) { |
493 | 493 | $organization = EE_Registry::instance()->CFG->organization; |
494 | - $payee_address = $organization->get_pretty('address_1') . '<br>'; |
|
495 | - $payee_address .= ! empty($organization->address_2) ? $organization->get_pretty('address_2') . '<br>' : ''; |
|
496 | - $payee_address .= $organization->get_pretty('city') . '<br>'; |
|
494 | + $payee_address = $organization->get_pretty('address_1').'<br>'; |
|
495 | + $payee_address .= ! empty($organization->address_2) ? $organization->get_pretty('address_2').'<br>' : ''; |
|
496 | + $payee_address .= $organization->get_pretty('city').'<br>'; |
|
497 | 497 | //state |
498 | 498 | $state = EE_Registry::instance()->load_model('State')->get_one_by_ID($organization->STA_ID); |
499 | 499 | $payee_address .= $state instanceof EE_State ? $state->name() : ''; |
500 | 500 | //Country |
501 | - $payee_address .= ! empty($organization->CNT_ISO) ? ', ' . $organization->CNT_ISO . '<br>' : ''; |
|
501 | + $payee_address .= ! empty($organization->CNT_ISO) ? ', '.$organization->CNT_ISO.'<br>' : ''; |
|
502 | 502 | $payee_address .= ! empty($organization->zip) ? $organization->zip : ''; |
503 | 503 | } |
504 | 504 | return $payee_address; |
@@ -531,7 +531,7 @@ discard block |
||
531 | 531 | { |
532 | 532 | $reg = $this->_data->primary_reg_obj; |
533 | 533 | $message_type = isset($this->_extra_data['message_type']) ? $this->_extra_data['message_type'] : ''; |
534 | - if (! $reg instanceof EE_Registration || empty($message_type)) { |
|
534 | + if ( ! $reg instanceof EE_Registration || empty($message_type)) { |
|
535 | 535 | return ''; |
536 | 536 | } |
537 | 537 | $switch_to_invoice = ! $message_type instanceof EE_Invoice_message_type ? true : false; |
@@ -539,13 +539,13 @@ discard block |
||
539 | 539 | ? esc_html__('View Invoice', 'event_espresso') : esc_html__('Switch to Invoice', 'event_espresso'); |
540 | 540 | $switch_to_label = ! $switch_to_invoice ? esc_html__('Switch to Receipt', 'event_espresso') : $switch_to_label; |
541 | 541 | $switch_to_url = $switch_to_invoice ? $reg->invoice_url() : $reg->receipt_url(); |
542 | - if (! $button) { |
|
542 | + if ( ! $button) { |
|
543 | 543 | return $switch_to_url; |
544 | 544 | } |
545 | - if (! empty($switch_to_url)) { |
|
545 | + if ( ! empty($switch_to_url)) { |
|
546 | 546 | return ' |
547 | - <form method="post" action="' . $switch_to_url . '" > |
|
548 | - <input class="print_button" type="submit" value="' . $switch_to_label . '" /> |
|
547 | + <form method="post" action="' . $switch_to_url.'" > |
|
548 | + <input class="print_button" type="submit" value="' . $switch_to_label.'" /> |
|
549 | 549 | </form> |
550 | 550 | '; |
551 | 551 | } |
@@ -563,7 +563,7 @@ discard block |
||
563 | 563 | private function _get_subtotal($tax = false) |
564 | 564 | { |
565 | 565 | $grand_total = isset($this->_data->grand_total_line_item) ? $this->_data->grand_total_line_item : null; |
566 | - if (! $grand_total instanceof EE_Line_Item) { |
|
566 | + if ( ! $grand_total instanceof EE_Line_Item) { |
|
567 | 567 | return 0; |
568 | 568 | } |
569 | 569 | return $tax ? $grand_total->get_total_tax() : $grand_total->get_items_total(); |
@@ -590,11 +590,11 @@ discard block |
||
590 | 590 | $class = isset($attrs['class']) ? $attrs['class'] : 'callout'; |
591 | 591 | $custom_text = isset($attrs['custom_text']) ? $attrs['custom_text'] : 'You can %smake a payment here »%s.'; |
592 | 592 | $container_tag = isset($attrs['container_tag']) ? $attrs['container_tag'] : 'p'; |
593 | - $opening_tag = ! empty($container_tag) ? '<' . $container_tag : ''; |
|
594 | - $opening_tag .= ! empty($opening_tag) && ! empty($class) ? ' class="' . $class . '"' : $opening_tag; |
|
593 | + $opening_tag = ! empty($container_tag) ? '<'.$container_tag : ''; |
|
594 | + $opening_tag .= ! empty($opening_tag) && ! empty($class) ? ' class="'.$class.'"' : $opening_tag; |
|
595 | 595 | $opening_tag .= ! empty($opening_tag) ? '>' : $opening_tag; |
596 | - $closing_tag = ! empty($container_tag) ? '</' . $container_tag . '>' : ''; |
|
597 | - $content = $opening_tag . sprintf($custom_text, '<a href="[PAYMENT_URL]">', '</a>') . $closing_tag; |
|
596 | + $closing_tag = ! empty($container_tag) ? '</'.$container_tag.'>' : ''; |
|
597 | + $content = $opening_tag.sprintf($custom_text, '<a href="[PAYMENT_URL]">', '</a>').$closing_tag; |
|
598 | 598 | //we need to re run this string through the parser to catch any shortcodes that are in it. |
599 | 599 | $owing_content = $this->_shortcode_helper->parse_message_template($content, $addressee, $valid_shortcodes, |
600 | 600 | $this->_message_type, $this->_messenger, $this->_message); |
@@ -622,14 +622,14 @@ discard block |
||
622 | 622 | } |
623 | 623 | $attrs = $this->_get_shortcode_attrs($shortcode); |
624 | 624 | $format = isset($attrs['format']) ? $attrs['format'] : get_option('date_format'); |
625 | - $days_until_due = isset($attrs['days_until_due']) ? (int)$attrs['days_until_due'] : 30; |
|
625 | + $days_until_due = isset($attrs['days_until_due']) ? (int) $attrs['days_until_due'] : 30; |
|
626 | 626 | $prefix_text = isset($attrs['prefix_text']) ? $attrs['prefix_text'] |
627 | 627 | : esc_html__('Payment in full due by: ', 'event_espresso'); |
628 | 628 | $transaction_created = $transaction->get_DateTime_object('TXN_timestamp'); |
629 | 629 | //setup date due: |
630 | 630 | try { |
631 | 631 | if ($transaction_created instanceof DateTime) { |
632 | - $date_due = $transaction_created->add(new DateInterval('P' . $days_until_due . 'D'))->format($format); |
|
632 | + $date_due = $transaction_created->add(new DateInterval('P'.$days_until_due.'D'))->format($format); |
|
633 | 633 | } else { |
634 | 634 | throw new Exception(); |
635 | 635 | } |
@@ -637,7 +637,7 @@ discard block |
||
637 | 637 | //format was likely invalid. |
638 | 638 | $date_due = 'Unable to calculate date due, likely the format string is invalid.'; |
639 | 639 | } |
640 | - return $prefix_text . $date_due; |
|
640 | + return $prefix_text.$date_due; |
|
641 | 641 | } |
642 | 642 | |
643 | 643 | } //end EE_Transaction Shortcodes library |
@@ -1,5 +1,5 @@ discard block |
||
1 | -<?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
2 | - exit( 'No direct script access allowed' ); |
|
1 | +<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
2 | + exit('No direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | /** |
5 | 5 | * |
@@ -37,33 +37,33 @@ discard block |
||
37 | 37 | * @access protected |
38 | 38 | * @return EEM_Payment_Method |
39 | 39 | */ |
40 | - protected function __construct( $timezone = NULL ) { |
|
41 | - $this->singlular_item = __( 'Payment Method', 'event_espresso' ); |
|
42 | - $this->plural_item = __( 'Payment Methods', 'event_espresso' ); |
|
43 | - $this->_tables = array( 'Payment_Method' => new EE_Primary_Table( 'esp_payment_method', 'PMD_ID' ) ); |
|
40 | + protected function __construct($timezone = NULL) { |
|
41 | + $this->singlular_item = __('Payment Method', 'event_espresso'); |
|
42 | + $this->plural_item = __('Payment Methods', 'event_espresso'); |
|
43 | + $this->_tables = array('Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID')); |
|
44 | 44 | $this->_fields = array( |
45 | 45 | 'Payment_Method' => array( |
46 | - 'PMD_ID' => new EE_Primary_Key_Int_Field( 'PMD_ID', __( "ID", 'event_espresso' ) ), |
|
47 | - 'PMD_type' => new EE_Plain_Text_Field( 'PMD_type', __( "Payment Method Type", 'event_espresso' ), FALSE, 'Admin_Only' ), |
|
48 | - 'PMD_name' => new EE_Plain_Text_Field( 'PMD_name', __( "Name", 'event_espresso' ), FALSE ), |
|
49 | - 'PMD_desc' => new EE_Post_Content_Field( 'PMD_desc', __( "Description", 'event_espresso' ), FALSE, '' ), |
|
50 | - 'PMD_admin_name' => new EE_Plain_Text_Field( 'PMD_admin_name', __( "Admin-Only Name", 'event_espresso' ), TRUE ), |
|
51 | - 'PMD_admin_desc' => new EE_Post_Content_Field( 'PMD_admin_desc', __( "Admin-Only Description", 'event_espresso' ), TRUE ), |
|
52 | - 'PMD_slug' => new EE_Slug_Field( 'PMD_slug', __( "Slug", 'event_espresso' ), FALSE ), |
|
53 | - 'PMD_order' => new EE_Integer_Field( 'PMD_order', __( "Order", 'event_espresso' ), FALSE, 0 ), |
|
54 | - 'PMD_debug_mode' => new EE_Boolean_Field( 'PMD_debug_mode', __( "Debug Mode On?", 'event_espresso' ), FALSE, FALSE ), |
|
55 | - 'PMD_wp_user' => new EE_WP_User_Field( 'PMD_wp_user', __( "Payment Method Creator ID", 'event_espresso' ), FALSE ), |
|
56 | - 'PMD_open_by_default' => new EE_Boolean_Field( 'PMD_open_by_default', __( "Open by Default?", 'event_espresso' ), FALSE, FALSE ), 'PMD_button_url' => new EE_Plain_Text_Field( 'PMD_button_url', __( "Button URL", 'event_espresso' ), TRUE, '' ), |
|
57 | - 'PMD_scope' => new EE_Serialized_Text_Field( 'PMD_scope', __( "Usable From?", 'event_espresso' ), FALSE, array() ), //possible values currently are 'CART','ADMIN','API' |
|
46 | + 'PMD_ID' => new EE_Primary_Key_Int_Field('PMD_ID', __("ID", 'event_espresso')), |
|
47 | + 'PMD_type' => new EE_Plain_Text_Field('PMD_type', __("Payment Method Type", 'event_espresso'), FALSE, 'Admin_Only'), |
|
48 | + 'PMD_name' => new EE_Plain_Text_Field('PMD_name', __("Name", 'event_espresso'), FALSE), |
|
49 | + 'PMD_desc' => new EE_Post_Content_Field('PMD_desc', __("Description", 'event_espresso'), FALSE, ''), |
|
50 | + 'PMD_admin_name' => new EE_Plain_Text_Field('PMD_admin_name', __("Admin-Only Name", 'event_espresso'), TRUE), |
|
51 | + 'PMD_admin_desc' => new EE_Post_Content_Field('PMD_admin_desc', __("Admin-Only Description", 'event_espresso'), TRUE), |
|
52 | + 'PMD_slug' => new EE_Slug_Field('PMD_slug', __("Slug", 'event_espresso'), FALSE), |
|
53 | + 'PMD_order' => new EE_Integer_Field('PMD_order', __("Order", 'event_espresso'), FALSE, 0), |
|
54 | + 'PMD_debug_mode' => new EE_Boolean_Field('PMD_debug_mode', __("Debug Mode On?", 'event_espresso'), FALSE, FALSE), |
|
55 | + 'PMD_wp_user' => new EE_WP_User_Field('PMD_wp_user', __("Payment Method Creator ID", 'event_espresso'), FALSE), |
|
56 | + 'PMD_open_by_default' => new EE_Boolean_Field('PMD_open_by_default', __("Open by Default?", 'event_espresso'), FALSE, FALSE), 'PMD_button_url' => new EE_Plain_Text_Field('PMD_button_url', __("Button URL", 'event_espresso'), TRUE, ''), |
|
57 | + 'PMD_scope' => new EE_Serialized_Text_Field('PMD_scope', __("Usable From?", 'event_espresso'), FALSE, array()), //possible values currently are 'CART','ADMIN','API' |
|
58 | 58 | ) ); |
59 | 59 | $this->_model_relations = array( |
60 | 60 | // 'Event'=>new EE_HABTM_Relation('Event_Payment_Method'), |
61 | 61 | 'Payment' => new EE_Has_Many_Relation(), |
62 | - 'Currency' => new EE_HABTM_Relation( 'Currency_Payment_Method' ), |
|
62 | + 'Currency' => new EE_HABTM_Relation('Currency_Payment_Method'), |
|
63 | 63 | 'Transaction' => new EE_Has_Many_Relation(), |
64 | 64 | 'WP_User' => new EE_Belongs_To_Relation(), |
65 | 65 | ); |
66 | - parent::__construct( $timezone ); |
|
66 | + parent::__construct($timezone); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | |
@@ -73,8 +73,8 @@ discard block |
||
73 | 73 | * @param string $slug |
74 | 74 | * @return EE_Payment_Method |
75 | 75 | */ |
76 | - public function get_one_by_slug( $slug ) { |
|
77 | - return $this->get_one( array( array( 'PMD_slug' => $slug ) ) ); |
|
76 | + public function get_one_by_slug($slug) { |
|
77 | + return $this->get_one(array(array('PMD_slug' => $slug))); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | |
@@ -88,8 +88,8 @@ discard block |
||
88 | 88 | return apply_filters( |
89 | 89 | 'FHEE__EEM_Payment_Method__scopes', |
90 | 90 | array( |
91 | - self::scope_cart => __( "Front-end Registration Page", 'event_espresso' ), |
|
92 | - self::scope_admin => __( "Admin Registration Page (no online processing)", 'event_espresso' ) |
|
91 | + self::scope_cart => __("Front-end Registration Page", 'event_espresso'), |
|
92 | + self::scope_admin => __("Admin Registration Page (no online processing)", 'event_espresso') |
|
93 | 93 | ) |
94 | 94 | ); |
95 | 95 | } |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | * @param string $scope like one of EEM_Payment_Method::instance()->scopes() |
102 | 102 | * @return boolean |
103 | 103 | */ |
104 | - public function is_valid_scope( $scope ) { |
|
104 | + public function is_valid_scope($scope) { |
|
105 | 105 | $scopes = $this->scopes(); |
106 | - if ( isset( $scopes[ $scope ] ) ) { |
|
106 | + if (isset($scopes[$scope])) { |
|
107 | 107 | return TRUE; |
108 | 108 | } else { |
109 | 109 | return FALSE; |
@@ -119,11 +119,11 @@ discard block |
||
119 | 119 | * @throws EE_Error |
120 | 120 | * @return EE_Payment_Method[] |
121 | 121 | */ |
122 | - public function get_all_active( $scope = NULL, $query_params = array() ) { |
|
123 | - if( ! isset( $query_params[ 'order_by' ] ) && ! isset( $query_params[ 'order' ] ) ) { |
|
124 | - $query_params['order_by'] = array( 'PMD_order' => 'ASC', 'PMD_ID' => 'ASC' ); |
|
122 | + public function get_all_active($scope = NULL, $query_params = array()) { |
|
123 | + if ( ! isset($query_params['order_by']) && ! isset($query_params['order'])) { |
|
124 | + $query_params['order_by'] = array('PMD_order' => 'ASC', 'PMD_ID' => 'ASC'); |
|
125 | 125 | } |
126 | - return $this->get_all( $this->_get_query_params_for_all_active( $scope, $query_params ) ); |
|
126 | + return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params)); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
@@ -132,8 +132,8 @@ discard block |
||
132 | 132 | * @param array $query_params |
133 | 133 | * @return int |
134 | 134 | */ |
135 | - public function count_active( $scope = NULL, $query_params = array() ){ |
|
136 | - return $this->count( $this->_get_query_params_for_all_active( $scope, $query_params ) ); |
|
135 | + public function count_active($scope = NULL, $query_params = array()) { |
|
136 | + return $this->count($this->_get_query_params_for_all_active($scope, $query_params)); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | /** |
@@ -144,21 +144,21 @@ discard block |
||
144 | 144 | * @return array like param of EEM_Base::get_all() |
145 | 145 | * @throws EE_Error |
146 | 146 | */ |
147 | - protected function _get_query_params_for_all_active( $scope = NULL, $query_params = array() ){ |
|
148 | - if ( $scope ) { |
|
149 | - if ( $this->is_valid_scope( $scope ) ) { |
|
150 | - return array_replace_recursive( array( array( 'PMD_scope' => array( 'LIKE', "%$scope%" ) ) ), $query_params ); |
|
147 | + protected function _get_query_params_for_all_active($scope = NULL, $query_params = array()) { |
|
148 | + if ($scope) { |
|
149 | + if ($this->is_valid_scope($scope)) { |
|
150 | + return array_replace_recursive(array(array('PMD_scope' => array('LIKE', "%$scope%"))), $query_params); |
|
151 | 151 | } else { |
152 | - throw new EE_Error( sprintf( __( "'%s' is not a valid scope for a payment method", "event_espresso" ), $scope ) ); |
|
152 | + throw new EE_Error(sprintf(__("'%s' is not a valid scope for a payment method", "event_espresso"), $scope)); |
|
153 | 153 | } |
154 | 154 | } else { |
155 | 155 | $acceptable_scopes = array(); |
156 | 156 | $count = 0; |
157 | - foreach ( $this->scopes() as $scope_name => $desc ) { |
|
157 | + foreach ($this->scopes() as $scope_name => $desc) { |
|
158 | 158 | $count++; |
159 | - $acceptable_scopes[ 'PMD_scope*' . $count ] = array( 'LIKE', '%' . $scope_name . '%' ); |
|
159 | + $acceptable_scopes['PMD_scope*'.$count] = array('LIKE', '%'.$scope_name.'%'); |
|
160 | 160 | } |
161 | - return array_replace_recursive( array( array( 'OR*active_scope' => $acceptable_scopes ) ), $query_params ); |
|
161 | + return array_replace_recursive(array(array('OR*active_scope' => $acceptable_scopes)), $query_params); |
|
162 | 162 | } |
163 | 163 | } |
164 | 164 | |
@@ -170,8 +170,8 @@ discard block |
||
170 | 170 | * @return array like param of EEM_Base::get_all() |
171 | 171 | * @throws EE_Error |
172 | 172 | */ |
173 | - public function get_query_params_for_all_active( $scope = NULL, $query_params = array() ) { |
|
174 | - return $this->_get_query_params_for_all_active( $scope, $query_params ); |
|
173 | + public function get_query_params_for_all_active($scope = NULL, $query_params = array()) { |
|
174 | + return $this->_get_query_params_for_all_active($scope, $query_params); |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | |
@@ -181,8 +181,8 @@ discard block |
||
181 | 181 | * @param array $query_params |
182 | 182 | * @return EE_Payment_Method |
183 | 183 | */ |
184 | - public function get_one_active( $scope = NULL, $query_params = array() ) { |
|
185 | - return $this->get_one( $this->_get_query_params_for_all_active( $scope, $query_params ) ); |
|
184 | + public function get_one_active($scope = NULL, $query_params = array()) { |
|
185 | + return $this->get_one($this->_get_query_params_for_all_active($scope, $query_params)); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | |
@@ -192,8 +192,8 @@ discard block |
||
192 | 192 | * @param string $type |
193 | 193 | * @return EE_Payment_Method |
194 | 194 | */ |
195 | - public function get_one_of_type( $type ) { |
|
196 | - return $this->get_one( array( array( 'PMD_type' => $type ) ) ); |
|
195 | + public function get_one_of_type($type) { |
|
196 | + return $this->get_one(array(array('PMD_type' => $type))); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | |
@@ -206,22 +206,22 @@ discard block |
||
206 | 206 | * @return EE_Payment_Method |
207 | 207 | * @throws EE_Error |
208 | 208 | */ |
209 | - public function ensure_is_obj( $base_class_obj_or_id, $ensure_is_in_db = FALSE ) { |
|
209 | + public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = FALSE) { |
|
210 | 210 | //first: check if it's a slug |
211 | - if( is_string( $base_class_obj_or_id ) ) { |
|
212 | - $obj = $this->get_one_by_slug( $base_class_obj_or_id ); |
|
213 | - if( $obj ) { |
|
211 | + if (is_string($base_class_obj_or_id)) { |
|
212 | + $obj = $this->get_one_by_slug($base_class_obj_or_id); |
|
213 | + if ($obj) { |
|
214 | 214 | return $obj; |
215 | 215 | } |
216 | 216 | } |
217 | 217 | //ok so it wasn't a slug we were passed. try the usual then (ie, it's an object or an ID) |
218 | 218 | try { |
219 | - return parent::ensure_is_obj( $base_class_obj_or_id, $ensure_is_in_db ); |
|
219 | + return parent::ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db); |
|
220 | 220 | } |
221 | - catch ( EE_Error $e ) { |
|
221 | + catch (EE_Error $e) { |
|
222 | 222 | //handle it outside the catch |
223 | 223 | } |
224 | - throw new EE_Error( sprintf( __( "'%s' is neither a Payment Method ID, slug, nor object.", "event_espresso" ), $base_class_obj_or_id ) ); |
|
224 | + throw new EE_Error(sprintf(__("'%s' is neither a Payment Method ID, slug, nor object.", "event_espresso"), $base_class_obj_or_id)); |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | |
@@ -232,12 +232,12 @@ discard block |
||
232 | 232 | * @param mixed $base_obj_or_id_or_slug |
233 | 233 | * @return int |
234 | 234 | */ |
235 | - function ensure_is_ID( $base_obj_or_id_or_slug ) { |
|
236 | - if ( is_string( $base_obj_or_id_or_slug ) ) { |
|
235 | + function ensure_is_ID($base_obj_or_id_or_slug) { |
|
236 | + if (is_string($base_obj_or_id_or_slug)) { |
|
237 | 237 | //assume it's a slug |
238 | - $base_obj_or_id_or_slug = $this->get_one_by_slug( $base_obj_or_id_or_slug ); |
|
238 | + $base_obj_or_id_or_slug = $this->get_one_by_slug($base_obj_or_id_or_slug); |
|
239 | 239 | } |
240 | - return parent::ensure_is_ID( $base_obj_or_id_or_slug ); |
|
240 | + return parent::ensure_is_ID($base_obj_or_id_or_slug); |
|
241 | 241 | } |
242 | 242 | |
243 | 243 | |
@@ -246,36 +246,36 @@ discard block |
||
246 | 246 | * Verifies the button urls on all the passed payment methods have a valid button url. If not, resets them to their default. |
247 | 247 | * @param EE_Payment_Method[] $payment_methods. If NULL is provided defaults to all payment methods active in the cart |
248 | 248 | */ |
249 | - function verify_button_urls( $payment_methods = NULL ) { |
|
250 | - $payment_methods = is_array( $payment_methods ) ? $payment_methods : $this->get_all_active(EEM_Payment_Method::scope_cart); |
|
251 | - foreach ( $payment_methods as $payment_method ) { |
|
249 | + function verify_button_urls($payment_methods = NULL) { |
|
250 | + $payment_methods = is_array($payment_methods) ? $payment_methods : $this->get_all_active(EEM_Payment_Method::scope_cart); |
|
251 | + foreach ($payment_methods as $payment_method) { |
|
252 | 252 | try { |
253 | 253 | $current_button_url = $payment_method->button_url(); |
254 | - $buttons_urls_to_try = apply_filters( 'FHEE__EEM_Payment_Method__verify_button_urls__button_urls_to_try', array( |
|
255 | - 'current_ssl' => str_replace( "http://", "https://", $current_button_url ), |
|
256 | - 'current' => str_replace( "https://", "http://", $current_button_url ), |
|
257 | - 'default_ssl' => str_replace( "http://", "https://", $payment_method->type_obj()->default_button_url() ), |
|
258 | - 'default' => str_replace( "https://", "http://", $payment_method->type_obj()->default_button_url() ), |
|
259 | - ) ); |
|
260 | - foreach( $buttons_urls_to_try as $button_url_to_try ) { |
|
261 | - if( |
|
254 | + $buttons_urls_to_try = apply_filters('FHEE__EEM_Payment_Method__verify_button_urls__button_urls_to_try', array( |
|
255 | + 'current_ssl' => str_replace("http://", "https://", $current_button_url), |
|
256 | + 'current' => str_replace("https://", "http://", $current_button_url), |
|
257 | + 'default_ssl' => str_replace("http://", "https://", $payment_method->type_obj()->default_button_url()), |
|
258 | + 'default' => str_replace("https://", "http://", $payment_method->type_obj()->default_button_url()), |
|
259 | + )); |
|
260 | + foreach ($buttons_urls_to_try as $button_url_to_try) { |
|
261 | + if ( |
|
262 | 262 | (//this is the current url and it exists, regardless of SSL issues |
263 | 263 | $button_url_to_try == $current_button_url && |
264 | 264 | EEH_URL::remote_file_exists( |
265 | 265 | $button_url_to_try, |
266 | 266 | array( |
267 | 267 | 'sslverify' => false, |
268 | - 'limit_response_size' => 4095,//we don't really care for a full response, but we do want headers at least. Lets just ask for a one block |
|
268 | + 'limit_response_size' => 4095, //we don't really care for a full response, but we do want headers at least. Lets just ask for a one block |
|
269 | 269 | ) ) |
270 | 270 | ) |
271 | 271 | || |
272 | 272 | (//this is NOT the current url and it exists with a working SSL cert |
273 | 273 | $button_url_to_try != $current_button_url && |
274 | - EEH_URL::remote_file_exists( $button_url_to_try ) |
|
274 | + EEH_URL::remote_file_exists($button_url_to_try) |
|
275 | 275 | ) ) { |
276 | - if( $current_button_url != $button_url_to_try ){ |
|
277 | - $payment_method->save( array( 'PMD_button_url' => $button_url_to_try ) ); |
|
278 | - EE_Error::add_attention( sprintf( __( "Payment Method %s's button url was set to %s, because the old image either didnt exist or SSL was recently enabled.", "event_espresso" ), $payment_method->name(), $button_url_to_try ) ); |
|
276 | + if ($current_button_url != $button_url_to_try) { |
|
277 | + $payment_method->save(array('PMD_button_url' => $button_url_to_try)); |
|
278 | + EE_Error::add_attention(sprintf(__("Payment Method %s's button url was set to %s, because the old image either didnt exist or SSL was recently enabled.", "event_espresso"), $payment_method->name(), $button_url_to_try)); |
|
279 | 279 | } |
280 | 280 | //this image exists. So if wasn't set before, now it is; |
281 | 281 | //or if it was already set, we have nothing to do |
@@ -283,8 +283,8 @@ discard block |
||
283 | 283 | } |
284 | 284 | } |
285 | 285 | } |
286 | - catch ( EE_Error $e ) { |
|
287 | - $payment_method->set_active( FALSE ); |
|
286 | + catch (EE_Error $e) { |
|
287 | + $payment_method->set_active(FALSE); |
|
288 | 288 | } |
289 | 289 | } |
290 | 290 | } |
@@ -298,29 +298,29 @@ discard block |
||
298 | 298 | * @param array $rows |
299 | 299 | * @return EE_Payment_Method[] |
300 | 300 | */ |
301 | - protected function _create_objects( $rows = array() ) { |
|
302 | - EE_Registry::instance()->load_lib( 'Payment_Method_Manager' ); |
|
303 | - $payment_methods = parent::_create_objects( $rows ); |
|
301 | + protected function _create_objects($rows = array()) { |
|
302 | + EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
|
303 | + $payment_methods = parent::_create_objects($rows); |
|
304 | 304 | /* @var $payment_methods EE_Payment_Method[] */ |
305 | 305 | $usable_payment_methods = array(); |
306 | - foreach ( $payment_methods as $key => $payment_method ) { |
|
307 | - if ( EE_Payment_Method_Manager::instance()->payment_method_type_exists( $payment_method->type() ) ) { |
|
308 | - $usable_payment_methods[ $key ] = $payment_method; |
|
306 | + foreach ($payment_methods as $key => $payment_method) { |
|
307 | + if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) { |
|
308 | + $usable_payment_methods[$key] = $payment_method; |
|
309 | 309 | //some payment methods enqueue their scripts in EE_PMT_*::__construct |
310 | 310 | //which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue |
311 | 311 | //its scripts). but for backwards-compat we should continue to do that |
312 | 312 | $payment_method->type_obj(); |
313 | - } elseif( $payment_method->active() ) { |
|
313 | + } elseif ($payment_method->active()) { |
|
314 | 314 | //only deactivate and notify the admin if the payment is active somewhere |
315 | 315 | $payment_method->deactivate(); |
316 | 316 | $payment_method->save(); |
317 | 317 | EE_Error::add_persistent_admin_notice( |
318 | - 'auto-deactivated-' . $payment_method->type(), |
|
318 | + 'auto-deactivated-'.$payment_method->type(), |
|
319 | 319 | sprintf( |
320 | - __( 'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.', 'event_espresso' ), |
|
320 | + __('The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.', 'event_espresso'), |
|
321 | 321 | $payment_method->admin_name(), |
322 | 322 | '<br />', |
323 | - '<a href="' . admin_url('plugins.php') . '">', |
|
323 | + '<a href="'.admin_url('plugins.php').'">', |
|
324 | 324 | '</a>' |
325 | 325 | ), |
326 | 326 | true |
@@ -340,11 +340,11 @@ discard block |
||
340 | 340 | * @param string $scope @see EEM_Payment_Method::get_all_for_events |
341 | 341 | * @return EE_Payment_Method[] |
342 | 342 | */ |
343 | - public function get_all_for_transaction( $transaction, $scope ) { |
|
343 | + public function get_all_for_transaction($transaction, $scope) { |
|
344 | 344 | //give addons a chance to override what payment methods are chosen based on the transaction |
345 | 345 | return apply_filters( |
346 | 346 | 'FHEE__EEM_Payment_Method__get_all_for_transaction__payment_methods', |
347 | - $this->get_all_active( $scope, array( 'group_by' => 'PMD_type' ) ), |
|
347 | + $this->get_all_active($scope, array('group_by' => 'PMD_type')), |
|
348 | 348 | $transaction, |
349 | 349 | $scope |
350 | 350 | ); |
@@ -360,16 +360,16 @@ discard block |
||
360 | 360 | * @param EE_Registration|int $registration_or_reg_id Either the EE_Registration object or the id for the registration. |
361 | 361 | * @return EE_Payment|null |
362 | 362 | */ |
363 | - public function get_last_used_for_registration( $registration_or_reg_id ) { |
|
364 | - $registration_id = EEM_Registration::instance()->ensure_is_ID( $registration_or_reg_id ); |
|
363 | + public function get_last_used_for_registration($registration_or_reg_id) { |
|
364 | + $registration_id = EEM_Registration::instance()->ensure_is_ID($registration_or_reg_id); |
|
365 | 365 | |
366 | 366 | $query_params = array( |
367 | 367 | 0 => array( |
368 | 368 | 'Payment.Registration.REG_ID' => $registration_id, |
369 | 369 | ), |
370 | - 'order_by' => array( 'Payment.PAY_ID' => 'DESC' ) |
|
370 | + 'order_by' => array('Payment.PAY_ID' => 'DESC') |
|
371 | 371 | ); |
372 | - return $this->get_one( $query_params ); |
|
372 | + return $this->get_one($query_params); |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | } |
@@ -14,56 +14,56 @@ |
||
14 | 14 | class EE_Number_Input_Display_Strategy extends EE_Display_Strategy_Base |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * minimum value for number field |
|
19 | - * |
|
20 | - * @var int|null $min |
|
21 | - */ |
|
22 | - protected $min; |
|
17 | + /** |
|
18 | + * minimum value for number field |
|
19 | + * |
|
20 | + * @var int|null $min |
|
21 | + */ |
|
22 | + protected $min; |
|
23 | 23 | |
24 | - /** |
|
25 | - * maximum value for number field |
|
26 | - * |
|
27 | - * @var int|null $max |
|
28 | - */ |
|
29 | - protected $max; |
|
24 | + /** |
|
25 | + * maximum value for number field |
|
26 | + * |
|
27 | + * @var int|null $max |
|
28 | + */ |
|
29 | + protected $max; |
|
30 | 30 | |
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * EE_Number_Input_Display_Strategy constructor. |
|
35 | - * |
|
36 | - * @param int $min |
|
37 | - * @param int $max |
|
38 | - */ |
|
39 | - public function __construct($min = null, $max = null) |
|
40 | - { |
|
41 | - $this->min = $min; |
|
42 | - $this->max = $max; |
|
43 | - } |
|
33 | + /** |
|
34 | + * EE_Number_Input_Display_Strategy constructor. |
|
35 | + * |
|
36 | + * @param int $min |
|
37 | + * @param int $max |
|
38 | + */ |
|
39 | + public function __construct($min = null, $max = null) |
|
40 | + { |
|
41 | + $this->min = $min; |
|
42 | + $this->max = $max; |
|
43 | + } |
|
44 | 44 | |
45 | 45 | |
46 | 46 | |
47 | - /** |
|
48 | - * @return string of html to display the field |
|
49 | - */ |
|
50 | - public function display() |
|
51 | - { |
|
52 | - $input = $this->_opening_tag('input'); |
|
53 | - $input .= $this->_attributes_string( |
|
54 | - array_merge( |
|
55 | - $this->_standard_attributes_array(), |
|
56 | - array( |
|
57 | - 'type' => 'number', |
|
58 | - 'min' => $this->min, |
|
59 | - 'max' => $this->max, |
|
60 | - 'value' => $this->_input->raw_value_in_form() |
|
61 | - ) |
|
62 | - ) |
|
63 | - ); |
|
64 | - $input .= $this->_close_tag(); |
|
65 | - return $input; |
|
66 | - } |
|
47 | + /** |
|
48 | + * @return string of html to display the field |
|
49 | + */ |
|
50 | + public function display() |
|
51 | + { |
|
52 | + $input = $this->_opening_tag('input'); |
|
53 | + $input .= $this->_attributes_string( |
|
54 | + array_merge( |
|
55 | + $this->_standard_attributes_array(), |
|
56 | + array( |
|
57 | + 'type' => 'number', |
|
58 | + 'min' => $this->min, |
|
59 | + 'max' => $this->max, |
|
60 | + 'value' => $this->_input->raw_value_in_form() |
|
61 | + ) |
|
62 | + ) |
|
63 | + ); |
|
64 | + $input .= $this->_close_tag(); |
|
65 | + return $input; |
|
66 | + } |
|
67 | 67 | |
68 | 68 | } |
69 | 69 | // End of file EE_Number_Input_Display_Strategy.php |
@@ -233,8 +233,8 @@ |
||
233 | 233 | if ( $registration instanceof EE_Registration ) { |
234 | 234 | $registration->set_paid( $registration->paid() - $amount_paid ); |
235 | 235 | if ($registration->save() !== false) { |
236 | - $registration_payment->delete_permanently(); |
|
237 | - $save_payment = true; |
|
236 | + $registration_payment->delete_permanently(); |
|
237 | + $save_payment = true; |
|
238 | 238 | } |
239 | 239 | } else { |
240 | 240 | EE_Error::add_error( |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
2 | -EE_Registry::instance()->load_class( 'Processor_Base' ); |
|
2 | +EE_Registry::instance()->load_class('Processor_Base'); |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Class EE_Transaction_Payments |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public static function instance() { |
44 | 44 | // check if class object is instantiated |
45 | - if ( ! self::$_instance instanceof EE_Transaction_Payments ) { |
|
45 | + if ( ! self::$_instance instanceof EE_Transaction_Payments) { |
|
46 | 46 | self::$_instance = new self(); |
47 | 47 | } |
48 | 48 | return self::$_instance; |
@@ -59,18 +59,18 @@ discard block |
||
59 | 59 | * @return bool true if TXN total was updated, false if not |
60 | 60 | * @throws \EE_Error |
61 | 61 | */ |
62 | - public function recalculate_transaction_total( EE_Transaction $transaction, $update_txn = true ) { |
|
62 | + public function recalculate_transaction_total(EE_Transaction $transaction, $update_txn = true) { |
|
63 | 63 | $total_line_item = $transaction->total_line_item(); |
64 | - if ( ! $total_line_item instanceof EE_Line_Item ) { |
|
64 | + if ( ! $total_line_item instanceof EE_Line_Item) { |
|
65 | 65 | EE_Error::add_error( |
66 | - sprintf( __( 'The Total Line Item for Transaction %1$d\'s was not found or is invalid.', 'event_espresso' ), $transaction->ID() ), |
|
66 | + sprintf(__('The Total Line Item for Transaction %1$d\'s was not found or is invalid.', 'event_espresso'), $transaction->ID()), |
|
67 | 67 | __FILE__, __FUNCTION__, __LINE__ |
68 | 68 | ); |
69 | 69 | return false; |
70 | 70 | } |
71 | 71 | $new_total = $total_line_item->recalculate_total_including_taxes(); |
72 | - $transaction->set_total( $new_total ); |
|
73 | - if ( $update_txn ) { |
|
72 | + $transaction->set_total($new_total); |
|
73 | + if ($update_txn) { |
|
74 | 74 | return $transaction->save() ? true : false; |
75 | 75 | } |
76 | 76 | return false; |
@@ -93,20 +93,20 @@ discard block |
||
93 | 93 | * @return boolean whether the TXN was saved |
94 | 94 | * @throws \EE_Error |
95 | 95 | */ |
96 | - public function calculate_total_payments_and_update_status( EE_Transaction $transaction, $update_txn = true ){ |
|
96 | + public function calculate_total_payments_and_update_status(EE_Transaction $transaction, $update_txn = true) { |
|
97 | 97 | // verify transaction |
98 | - if ( ! $transaction instanceof EE_Transaction ) { |
|
99 | - EE_Error::add_error( __( 'Please provide a valid EE_Transaction object.', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
|
98 | + if ( ! $transaction instanceof EE_Transaction) { |
|
99 | + EE_Error::add_error(__('Please provide a valid EE_Transaction object.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
100 | 100 | return false; |
101 | 101 | } |
102 | 102 | // calculate total paid |
103 | - $total_paid = $this->recalculate_total_payments_for_transaction( $transaction ); |
|
103 | + $total_paid = $this->recalculate_total_payments_for_transaction($transaction); |
|
104 | 104 | // if total paid has changed |
105 | - if ( $total_paid !== false && (float)$total_paid !== $transaction->paid() ) { |
|
106 | - $transaction->set_paid( $total_paid ); |
|
105 | + if ($total_paid !== false && (float) $total_paid !== $transaction->paid()) { |
|
106 | + $transaction->set_paid($total_paid); |
|
107 | 107 | // maybe update status, and make sure to save transaction if not done already |
108 | - if ( ! $transaction->update_status_based_on_total_paid( $update_txn ) ) { |
|
109 | - if ( $update_txn ) { |
|
108 | + if ( ! $transaction->update_status_based_on_total_paid($update_txn)) { |
|
109 | + if ($update_txn) { |
|
110 | 110 | return $transaction->save() ? true : false; |
111 | 111 | } |
112 | 112 | } else { |
@@ -130,18 +130,18 @@ discard block |
||
130 | 130 | * @return float|false float on success, false on fail |
131 | 131 | * @throws \EE_Error |
132 | 132 | */ |
133 | - public function recalculate_total_payments_for_transaction( EE_Transaction $transaction, $payment_status = EEM_Payment::status_id_approved ) { |
|
133 | + public function recalculate_total_payments_for_transaction(EE_Transaction $transaction, $payment_status = EEM_Payment::status_id_approved) { |
|
134 | 134 | // verify transaction |
135 | - if ( ! $transaction instanceof EE_Transaction ) { |
|
136 | - EE_Error::add_error( __( 'Please provide a valid EE_Transaction object.', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
|
135 | + if ( ! $transaction instanceof EE_Transaction) { |
|
136 | + EE_Error::add_error(__('Please provide a valid EE_Transaction object.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
137 | 137 | return false; |
138 | 138 | } |
139 | 139 | // ensure Payment model is loaded |
140 | - EE_Registry::instance()->load_model( 'Payment' ); |
|
140 | + EE_Registry::instance()->load_model('Payment'); |
|
141 | 141 | // calls EEM_Base::sum() |
142 | 142 | return EEM_Payment::instance()->sum( |
143 | 143 | // query params |
144 | - array( array( 'TXN_ID' => $transaction->ID(), 'STS_ID' => $payment_status )), |
|
144 | + array(array('TXN_ID' => $transaction->ID(), 'STS_ID' => $payment_status)), |
|
145 | 145 | // field to sum |
146 | 146 | 'PAY_amount' |
147 | 147 | ); |
@@ -159,17 +159,17 @@ discard block |
||
159 | 159 | * @return boolean |
160 | 160 | * @throws \EE_Error |
161 | 161 | */ |
162 | - public function delete_payment_and_update_transaction( EE_Payment $payment ) { |
|
162 | + public function delete_payment_and_update_transaction(EE_Payment $payment) { |
|
163 | 163 | // verify payment |
164 | - if ( ! $payment instanceof EE_Payment ) { |
|
165 | - EE_Error::add_error( __( 'A valid Payment object was not received.', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
|
164 | + if ( ! $payment instanceof EE_Payment) { |
|
165 | + EE_Error::add_error(__('A valid Payment object was not received.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
166 | 166 | return false; |
167 | 167 | } |
168 | - if ( ! $this->delete_registration_payments_and_update_registrations( $payment ) ) { |
|
168 | + if ( ! $this->delete_registration_payments_and_update_registrations($payment)) { |
|
169 | 169 | return false; |
170 | 170 | } |
171 | - if ( ! $payment->delete() ) { |
|
172 | - EE_Error::add_error( __( 'The payment could not be deleted.', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
|
171 | + if ( ! $payment->delete()) { |
|
172 | + EE_Error::add_error(__('The payment could not be deleted.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
173 | 173 | return false; |
174 | 174 | } |
175 | 175 | |
@@ -180,14 +180,14 @@ discard block |
||
180 | 180 | || $TXN_status === EEM_Transaction::failed_status_code |
181 | 181 | || $payment->amount() === 0 |
182 | 182 | ) { |
183 | - EE_Error::add_success( __( 'The Payment was successfully deleted.', 'event_espresso' ) ); |
|
183 | + EE_Error::add_success(__('The Payment was successfully deleted.', 'event_espresso')); |
|
184 | 184 | return true; |
185 | 185 | } |
186 | 186 | |
187 | 187 | |
188 | 188 | //if this fails, that just means that the transaction didn't get its status changed and/or updated. |
189 | 189 | //however the payment was still deleted. |
190 | - if ( ! $this->calculate_total_payments_and_update_status( $transaction ) ) { |
|
190 | + if ( ! $this->calculate_total_payments_and_update_status($transaction)) { |
|
191 | 191 | |
192 | 192 | EE_Error::add_attention( |
193 | 193 | __( |
@@ -221,17 +221,17 @@ discard block |
||
221 | 221 | * @return bool |
222 | 222 | * @throws \EE_Error |
223 | 223 | */ |
224 | - public function delete_registration_payments_and_update_registrations( EE_Payment $payment, $reg_payment_query_params = array() ) { |
|
224 | + public function delete_registration_payments_and_update_registrations(EE_Payment $payment, $reg_payment_query_params = array()) { |
|
225 | 225 | $save_payment = false; |
226 | - $reg_payment_query_params = ! empty( $reg_payment_query_params ) ? $reg_payment_query_params : array( array( 'PAY_ID' => $payment->ID() ) ); |
|
227 | - $registration_payments = EEM_Registration_Payment::instance()->get_all( $reg_payment_query_params ); |
|
228 | - if ( ! empty( $registration_payments )) { |
|
229 | - foreach ( $registration_payments as $registration_payment ) { |
|
230 | - if ( $registration_payment instanceof EE_Registration_Payment ) { |
|
226 | + $reg_payment_query_params = ! empty($reg_payment_query_params) ? $reg_payment_query_params : array(array('PAY_ID' => $payment->ID())); |
|
227 | + $registration_payments = EEM_Registration_Payment::instance()->get_all($reg_payment_query_params); |
|
228 | + if ( ! empty($registration_payments)) { |
|
229 | + foreach ($registration_payments as $registration_payment) { |
|
230 | + if ($registration_payment instanceof EE_Registration_Payment) { |
|
231 | 231 | $amount_paid = $registration_payment->amount(); |
232 | 232 | $registration = $registration_payment->registration(); |
233 | - if ( $registration instanceof EE_Registration ) { |
|
234 | - $registration->set_paid( $registration->paid() - $amount_paid ); |
|
233 | + if ($registration instanceof EE_Registration) { |
|
234 | + $registration->set_paid($registration->paid() - $amount_paid); |
|
235 | 235 | if ($registration->save() !== false) { |
236 | 236 | $registration_payment->delete_permanently(); |
237 | 237 | $save_payment = true; |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | } else { |
240 | 240 | EE_Error::add_error( |
241 | 241 | sprintf( |
242 | - __( 'An invalid Registration object was associated with Registration Payment ID# %1$d.', 'event_espresso' ), |
|
242 | + __('An invalid Registration object was associated with Registration Payment ID# %1$d.', 'event_espresso'), |
|
243 | 243 | $registration_payment->ID() |
244 | 244 | ), |
245 | 245 | __FILE__, __FUNCTION__, __LINE__ |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | } else { |
250 | 250 | EE_Error::add_error( |
251 | 251 | sprintf( |
252 | - __( 'An invalid Registration Payment object was associated with payment ID# %1$d.', 'event_espresso' ), |
|
252 | + __('An invalid Registration Payment object was associated with payment ID# %1$d.', 'event_espresso'), |
|
253 | 253 | $payment->ID() |
254 | 254 | ), |
255 | 255 | __FILE__, __FUNCTION__, __LINE__ |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | } |
259 | 259 | } |
260 | 260 | } |
261 | - if ( $save_payment ) { |
|
261 | + if ($save_payment) { |
|
262 | 262 | $payment->save(); |
263 | 263 | } |
264 | 264 | return true; |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | public function update_transaction_status_based_on_total_paid(EE_Transaction $transaction, $update_txn = true) |
283 | 283 | { |
284 | 284 | EE_Error::doing_it_wrong( |
285 | - __CLASS__ . '::' . __FUNCTION__, |
|
285 | + __CLASS__.'::'.__FUNCTION__, |
|
286 | 286 | sprintf(__('This method is deprecated. Please use "%s" instead', 'event_espresso'), |
287 | 287 | 'EE_Transaction::update_status_based_on_total_paid()'), |
288 | 288 | '4.9.1', |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | * @deprecated 4.9.12 |
325 | 325 | * @param string $old_txn_status |
326 | 326 | */ |
327 | - public function set_old_txn_status( $old_txn_status ) { |
|
327 | + public function set_old_txn_status($old_txn_status) { |
|
328 | 328 | EE_Error::doing_it_wrong( |
329 | 329 | __METHOD__, |
330 | 330 | esc_html__( |
@@ -334,7 +334,7 @@ discard block |
||
334 | 334 | '4.9.12' |
335 | 335 | ); |
336 | 336 | // only set the first time |
337 | - if ( $this->_old_txn_status === null ) { |
|
337 | + if ($this->_old_txn_status === null) { |
|
338 | 338 | $this->_old_txn_status = $old_txn_status; |
339 | 339 | } |
340 | 340 | } |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | * @deprecated 4.9.12 |
364 | 364 | * @param string $new_txn_status |
365 | 365 | */ |
366 | - public function set_new_txn_status( $new_txn_status ) { |
|
366 | + public function set_new_txn_status($new_txn_status) { |
|
367 | 367 | EE_Error::doing_it_wrong( |
368 | 368 | __METHOD__, |
369 | 369 | esc_html__( |