@@ -12,9 +12,9 @@ |
||
12 | 12 | */ |
13 | 13 | interface CaffeinatedInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * Used to indicate when functionality is caffeinated or not. |
|
17 | - * @return bool |
|
18 | - */ |
|
19 | - public function isCaffeinated(); |
|
15 | + /** |
|
16 | + * Used to indicate when functionality is caffeinated or not. |
|
17 | + * @return bool |
|
18 | + */ |
|
19 | + public function isCaffeinated(); |
|
20 | 20 | } |
@@ -26,702 +26,702 @@ |
||
26 | 26 | class EventSpacesCalculator |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * @var EE_Event $event |
|
31 | - */ |
|
32 | - private $event; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var array $datetime_query_params |
|
36 | - */ |
|
37 | - private $datetime_query_params; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var EE_Ticket[] $active_tickets |
|
41 | - */ |
|
42 | - private $active_tickets = array(); |
|
43 | - |
|
44 | - /** |
|
45 | - * @var EE_Datetime[] $datetimes |
|
46 | - */ |
|
47 | - private $datetimes = array(); |
|
48 | - |
|
49 | - /** |
|
50 | - * Array of Ticket IDs grouped by Datetime |
|
51 | - * |
|
52 | - * @var array $datetimes |
|
53 | - */ |
|
54 | - private $datetime_tickets = array(); |
|
55 | - |
|
56 | - /** |
|
57 | - * Max spaces for each Datetime (reg limit - previous sold) |
|
58 | - * |
|
59 | - * @var array $datetime_spaces |
|
60 | - */ |
|
61 | - private $datetime_spaces = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * Array of Datetime IDs grouped by Ticket |
|
65 | - * |
|
66 | - * @var array[] $ticket_datetimes |
|
67 | - */ |
|
68 | - private $ticket_datetimes = array(); |
|
69 | - |
|
70 | - /** |
|
71 | - * maximum ticket quantities for each ticket (adjusted for reg limit) |
|
72 | - * |
|
73 | - * @var array $ticket_quantities |
|
74 | - */ |
|
75 | - private $ticket_quantities = array(); |
|
76 | - |
|
77 | - /** |
|
78 | - * total quantity of sold and reserved for each ticket |
|
79 | - * |
|
80 | - * @var array $tickets_sold |
|
81 | - */ |
|
82 | - private $tickets_sold = array(); |
|
83 | - |
|
84 | - /** |
|
85 | - * total spaces available across all datetimes |
|
86 | - * |
|
87 | - * @var array $total_spaces |
|
88 | - */ |
|
89 | - private $total_spaces = array(); |
|
90 | - |
|
91 | - /** |
|
92 | - * @var boolean $debug |
|
93 | - */ |
|
94 | - private $debug = false; // true false |
|
95 | - |
|
96 | - /** |
|
97 | - * @var null|int $spaces_remaining |
|
98 | - */ |
|
99 | - private $spaces_remaining; |
|
100 | - |
|
101 | - /** |
|
102 | - * @var null|int $total_spaces_available |
|
103 | - */ |
|
104 | - private $total_spaces_available; |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * EventSpacesCalculator constructor. |
|
109 | - * |
|
110 | - * @param EE_Event $event |
|
111 | - * @param array $datetime_query_params |
|
112 | - * @throws EE_Error |
|
113 | - */ |
|
114 | - public function __construct(EE_Event $event, array $datetime_query_params = array()) |
|
115 | - { |
|
116 | - $this->event = $event; |
|
117 | - $this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC')); |
|
118 | - $this->setHooks(); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - private function setHooks() |
|
126 | - { |
|
127 | - add_action('AHEE__EE_Ticket__increase_sold', array($this, 'clearResults')); |
|
128 | - add_action('AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults')); |
|
129 | - add_action('AHEE__EE_Datetime__increase_sold', array($this, 'clearResults')); |
|
130 | - add_action('AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults')); |
|
131 | - add_action('AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults')); |
|
132 | - add_action('AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults')); |
|
133 | - add_action('AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults')); |
|
134 | - add_action('AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults')); |
|
135 | - } |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * @return void |
|
140 | - */ |
|
141 | - public function clearResults() |
|
142 | - { |
|
143 | - $this->spaces_remaining = null; |
|
144 | - $this->total_spaces_available = null; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * @return EE_Ticket[] |
|
150 | - * @throws EE_Error |
|
151 | - * @throws InvalidDataTypeException |
|
152 | - * @throws InvalidInterfaceException |
|
153 | - * @throws InvalidArgumentException |
|
154 | - */ |
|
155 | - public function getActiveTickets() |
|
156 | - { |
|
157 | - if (empty($this->active_tickets)) { |
|
158 | - $this->active_tickets = $this->event->tickets( |
|
159 | - array( |
|
160 | - array('TKT_deleted' => false), |
|
161 | - 'order_by' => array('TKT_qty' => 'ASC'), |
|
162 | - ) |
|
163 | - ); |
|
164 | - } |
|
165 | - return $this->active_tickets; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * @param EE_Ticket[] $active_tickets |
|
171 | - * @throws EE_Error |
|
172 | - * @throws DomainException |
|
173 | - * @throws UnexpectedEntityException |
|
174 | - */ |
|
175 | - public function setActiveTickets(array $active_tickets = array()) |
|
176 | - { |
|
177 | - if (! empty($active_tickets)) { |
|
178 | - foreach ($active_tickets as $active_ticket) { |
|
179 | - $this->validateTicket($active_ticket); |
|
180 | - } |
|
181 | - // sort incoming array by ticket quantity (asc) |
|
182 | - usort( |
|
183 | - $active_tickets, |
|
184 | - function (EE_Ticket $a, EE_Ticket $b) { |
|
185 | - if ($a->qty() === $b->qty()) { |
|
186 | - return 0; |
|
187 | - } |
|
188 | - return ($a->qty() < $b->qty()) |
|
189 | - ? -1 |
|
190 | - : 1; |
|
191 | - } |
|
192 | - ); |
|
193 | - } |
|
194 | - $this->active_tickets = $active_tickets; |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * @param $ticket |
|
200 | - * @throws DomainException |
|
201 | - * @throws EE_Error |
|
202 | - * @throws UnexpectedEntityException |
|
203 | - */ |
|
204 | - private function validateTicket($ticket) |
|
205 | - { |
|
206 | - if (! $ticket instanceof EE_Ticket) { |
|
207 | - throw new DomainException( |
|
208 | - esc_html__( |
|
209 | - 'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.', |
|
210 | - 'event_espresso' |
|
211 | - ) |
|
212 | - ); |
|
213 | - } |
|
214 | - if ($ticket->get_event_ID() !== $this->event->ID()) { |
|
215 | - throw new DomainException( |
|
216 | - sprintf( |
|
217 | - esc_html__( |
|
218 | - 'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.', |
|
219 | - 'event_espresso' |
|
220 | - ), |
|
221 | - $ticket->get_event_ID(), |
|
222 | - $this->event->ID() |
|
223 | - ) |
|
224 | - ); |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - |
|
229 | - /** |
|
230 | - * @return EE_Datetime[] |
|
231 | - */ |
|
232 | - public function getDatetimes() |
|
233 | - { |
|
234 | - return $this->datetimes; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * @param EE_Datetime $datetime |
|
240 | - * @throws EE_Error |
|
241 | - * @throws DomainException |
|
242 | - */ |
|
243 | - public function setDatetime(EE_Datetime $datetime) |
|
244 | - { |
|
245 | - if ($datetime->event()->ID() !== $this->event->ID()) { |
|
246 | - throw new DomainException( |
|
247 | - sprintf( |
|
248 | - esc_html__( |
|
249 | - 'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.', |
|
250 | - 'event_espresso' |
|
251 | - ), |
|
252 | - $datetime->event()->ID(), |
|
253 | - $this->event->ID() |
|
254 | - ) |
|
255 | - ); |
|
256 | - } |
|
257 | - $this->datetimes[$datetime->ID()] = $datetime; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * calculate spaces remaining based on "saleable" tickets |
|
263 | - * |
|
264 | - * @return float|int |
|
265 | - * @throws EE_Error |
|
266 | - * @throws DomainException |
|
267 | - * @throws UnexpectedEntityException |
|
268 | - * @throws InvalidDataTypeException |
|
269 | - * @throws InvalidInterfaceException |
|
270 | - * @throws InvalidArgumentException |
|
271 | - */ |
|
272 | - public function spacesRemaining() |
|
273 | - { |
|
274 | - if ($this->spaces_remaining === null) { |
|
275 | - $this->initialize(); |
|
276 | - $this->spaces_remaining = $this->calculate(); |
|
277 | - } |
|
278 | - return $this->spaces_remaining; |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * calculates total available spaces for an event with no regard for sold tickets |
|
284 | - * |
|
285 | - * @return int|float |
|
286 | - * @throws EE_Error |
|
287 | - * @throws DomainException |
|
288 | - * @throws UnexpectedEntityException |
|
289 | - * @throws InvalidDataTypeException |
|
290 | - * @throws InvalidInterfaceException |
|
291 | - * @throws InvalidArgumentException |
|
292 | - */ |
|
293 | - public function totalSpacesAvailable() |
|
294 | - { |
|
295 | - if ($this->total_spaces_available === null) { |
|
296 | - $this->initialize(); |
|
297 | - $this->total_spaces_available = $this->calculate(false); |
|
298 | - } |
|
299 | - return $this->total_spaces_available; |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * Loops through the active tickets for the event |
|
305 | - * and builds a series of data arrays that will be used for calculating |
|
306 | - * the total maximum available spaces, as well as the spaces remaining. |
|
307 | - * Because ticket quantities affect datetime spaces and vice versa, |
|
308 | - * we need to be constantly updating these data arrays as things change, |
|
309 | - * which is the entire reason for their existence. |
|
310 | - * |
|
311 | - * @throws EE_Error |
|
312 | - * @throws DomainException |
|
313 | - * @throws UnexpectedEntityException |
|
314 | - * @throws InvalidDataTypeException |
|
315 | - * @throws InvalidInterfaceException |
|
316 | - * @throws InvalidArgumentException |
|
317 | - */ |
|
318 | - private function initialize() |
|
319 | - { |
|
320 | - if ($this->debug) { |
|
321 | - \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
322 | - } |
|
323 | - $this->datetime_tickets = array(); |
|
324 | - $this->datetime_spaces = array(); |
|
325 | - $this->ticket_datetimes = array(); |
|
326 | - $this->ticket_quantities = array(); |
|
327 | - $this->tickets_sold = array(); |
|
328 | - $this->total_spaces = array(); |
|
329 | - $active_tickets = $this->getActiveTickets(); |
|
330 | - if (! empty($active_tickets)) { |
|
331 | - foreach ($active_tickets as $ticket) { |
|
332 | - $this->validateTicket($ticket); |
|
333 | - // we need to index our data arrays using strings for the purpose of sorting, |
|
334 | - // but we also need them to be unique, so we'll just prepend a letter T to the ID |
|
335 | - $ticket_identifier = "T{$ticket->ID()}"; |
|
336 | - // to start, we'll just consider the raw qty to be the maximum availability for this ticket, |
|
337 | - // unless the ticket is past its "sell until" date, in which case the qty will be 0 |
|
338 | - $max_tickets = $ticket->is_expired() ? 0 : $ticket->qty(); |
|
339 | - // but we'll adjust that after looping over each datetime for the ticket and checking reg limits |
|
340 | - $ticket_datetimes = $ticket->datetimes($this->datetime_query_params); |
|
341 | - foreach ($ticket_datetimes as $datetime) { |
|
342 | - // save all datetimes |
|
343 | - $this->setDatetime($datetime); |
|
344 | - $datetime_identifier = "D{$datetime->ID()}"; |
|
345 | - $reg_limit = $datetime->reg_limit(); |
|
346 | - // ticket quantity can not exceed datetime reg limit |
|
347 | - $max_tickets = min($max_tickets, $reg_limit); |
|
348 | - // as described earlier, because we need to be able to constantly adjust numbers for things, |
|
349 | - // we are going to move all of our data into the following arrays: |
|
350 | - // datetime spaces initially represents the reg limit for each datetime, |
|
351 | - // but this will get adjusted as tickets are accounted for |
|
352 | - $this->datetime_spaces[$datetime_identifier] = $reg_limit; |
|
353 | - // just an array of ticket IDs grouped by datetime |
|
354 | - $this->datetime_tickets[$datetime_identifier][] = $ticket_identifier; |
|
355 | - // and an array of datetime IDs grouped by ticket |
|
356 | - $this->ticket_datetimes[$ticket_identifier][] = $datetime_identifier; |
|
357 | - } |
|
358 | - // total quantity of sold and reserved for each ticket |
|
359 | - $this->tickets_sold[$ticket_identifier] = $ticket->sold() + $ticket->reserved(); |
|
360 | - // and the maximum ticket quantities for each ticket (adjusted for reg limit) |
|
361 | - $this->ticket_quantities[$ticket_identifier] = $max_tickets; |
|
362 | - } |
|
363 | - } |
|
364 | - // sort datetime spaces by reg limit, but maintain our string indexes |
|
365 | - asort($this->datetime_spaces, SORT_NUMERIC); |
|
366 | - // datetime tickets need to be sorted in the SAME order as the above array... |
|
367 | - // so we'll just use array_merge() to take the structure of datetime_spaces |
|
368 | - // but overwrite all of the data with that from datetime_tickets |
|
369 | - $this->datetime_tickets = array_merge( |
|
370 | - $this->datetime_spaces, |
|
371 | - $this->datetime_tickets |
|
372 | - ); |
|
373 | - if ($this->debug) { |
|
374 | - \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__); |
|
375 | - \EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__); |
|
376 | - \EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__); |
|
377 | - } |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * performs calculations on initialized data |
|
383 | - * |
|
384 | - * @param bool $consider_sold |
|
385 | - * @return int|float |
|
386 | - */ |
|
387 | - private function calculate($consider_sold = true) |
|
388 | - { |
|
389 | - if ($this->debug) { |
|
390 | - \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
391 | - \EEH_Debug_Tools::printr($consider_sold, '$consider_sold', __FILE__, __LINE__); |
|
392 | - } |
|
393 | - if ($consider_sold) { |
|
394 | - // subtract amounts sold from all ticket quantities and datetime spaces |
|
395 | - $this->adjustTicketQuantitiesDueToSales(); |
|
396 | - } |
|
397 | - foreach ($this->datetime_tickets as $datetime_identifier => $tickets) { |
|
398 | - $this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets); |
|
399 | - } |
|
400 | - // total spaces available is just the sum of the spaces available for each datetime |
|
401 | - $spaces_remaining = array_sum($this->total_spaces); |
|
402 | - if ($this->debug) { |
|
403 | - \EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__); |
|
404 | - \EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__); |
|
405 | - \EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__); |
|
406 | - } |
|
407 | - return $spaces_remaining; |
|
408 | - } |
|
409 | - |
|
410 | - |
|
411 | - /** |
|
412 | - * subtracts amount of tickets sold from ticket quantities and datetime spaces |
|
413 | - */ |
|
414 | - private function adjustTicketQuantitiesDueToSales() |
|
415 | - { |
|
416 | - if ($this->debug) { |
|
417 | - \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
418 | - } |
|
419 | - foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) { |
|
420 | - if (isset($this->ticket_quantities[$ticket_identifier])) { |
|
421 | - $this->ticket_quantities[$ticket_identifier] -= $tickets_sold; |
|
422 | - // don't let values go below zero |
|
423 | - $this->ticket_quantities[$ticket_identifier] = max( |
|
424 | - $this->ticket_quantities[$ticket_identifier], |
|
425 | - 0 |
|
426 | - ); |
|
427 | - if ($this->debug) { |
|
428 | - \EEH_Debug_Tools::printr( |
|
429 | - "{$tickets_sold} sales for ticket {$ticket_identifier} ", |
|
430 | - 'subtracting', |
|
431 | - __FILE__, |
|
432 | - __LINE__ |
|
433 | - ); |
|
434 | - } |
|
435 | - } |
|
436 | - if (isset($this->ticket_datetimes[$ticket_identifier]) |
|
437 | - && is_array($this->ticket_datetimes[$ticket_identifier]) |
|
438 | - ) { |
|
439 | - foreach ($this->ticket_datetimes[$ticket_identifier] as $ticket_datetime) { |
|
440 | - if (isset($this->ticket_quantities[$ticket_identifier])) { |
|
441 | - $this->datetime_spaces[$ticket_datetime] -= $tickets_sold; |
|
442 | - // don't let values go below zero |
|
443 | - $this->datetime_spaces[$ticket_datetime] = max( |
|
444 | - $this->datetime_spaces[$ticket_datetime], |
|
445 | - 0 |
|
446 | - ); |
|
447 | - if ($this->debug) { |
|
448 | - \EEH_Debug_Tools::printr( |
|
449 | - "{$tickets_sold} sales for datetime {$ticket_datetime} ", |
|
450 | - 'subtracting', |
|
451 | - __FILE__, |
|
452 | - __LINE__ |
|
453 | - ); |
|
454 | - } |
|
455 | - } |
|
456 | - } |
|
457 | - } |
|
458 | - } |
|
459 | - } |
|
460 | - |
|
461 | - |
|
462 | - /** |
|
463 | - * @param string $datetime_identifier |
|
464 | - * @param array $tickets |
|
465 | - */ |
|
466 | - private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets) |
|
467 | - { |
|
468 | - // make sure a reg limit is set for the datetime |
|
469 | - $reg_limit = isset($this->datetime_spaces[$datetime_identifier]) |
|
470 | - ? $this->datetime_spaces[$datetime_identifier] |
|
471 | - : 0; |
|
472 | - // and bail if it is not |
|
473 | - if (! $reg_limit) { |
|
474 | - if ($this->debug) { |
|
475 | - \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__); |
|
476 | - } |
|
477 | - return; |
|
478 | - } |
|
479 | - if ($this->debug) { |
|
480 | - \EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1); |
|
481 | - \EEH_Debug_Tools::printr( |
|
482 | - "{$reg_limit}", |
|
483 | - 'REG LIMIT', |
|
484 | - __FILE__, |
|
485 | - __LINE__ |
|
486 | - ); |
|
487 | - } |
|
488 | - // number of allocated spaces always starts at zero |
|
489 | - $spaces_allocated = 0; |
|
490 | - $this->total_spaces[$datetime_identifier] = 0; |
|
491 | - foreach ($tickets as $ticket_identifier) { |
|
492 | - $spaces_allocated = $this->calculateAvailableSpacesForTicket( |
|
493 | - $datetime_identifier, |
|
494 | - $reg_limit, |
|
495 | - $ticket_identifier, |
|
496 | - $spaces_allocated |
|
497 | - ); |
|
498 | - } |
|
499 | - // spaces can't be negative |
|
500 | - $spaces_allocated = max($spaces_allocated, 0); |
|
501 | - if ($spaces_allocated) { |
|
502 | - // track any non-zero values |
|
503 | - $this->total_spaces[$datetime_identifier] += $spaces_allocated; |
|
504 | - if ($this->debug) { |
|
505 | - \EEH_Debug_Tools::printr((string)$spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__); |
|
506 | - } |
|
507 | - } else { |
|
508 | - if ($this->debug) { |
|
509 | - \EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__); |
|
510 | - } |
|
511 | - } |
|
512 | - if ($this->debug) { |
|
513 | - \EEH_Debug_Tools::printr( |
|
514 | - $this->total_spaces[$datetime_identifier], |
|
515 | - '$total_spaces', |
|
516 | - __FILE__, |
|
517 | - __LINE__ |
|
518 | - ); |
|
519 | - \EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__); |
|
520 | - \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__); |
|
521 | - } |
|
522 | - } |
|
523 | - |
|
524 | - |
|
525 | - /** |
|
526 | - * @param string $datetime_identifier |
|
527 | - * @param int $reg_limit |
|
528 | - * @param string $ticket_identifier |
|
529 | - * @param int $spaces_allocated |
|
530 | - * @return int |
|
531 | - */ |
|
532 | - private function calculateAvailableSpacesForTicket( |
|
533 | - $datetime_identifier, |
|
534 | - $reg_limit, |
|
535 | - $ticket_identifier, |
|
536 | - $spaces_allocated |
|
537 | - ) { |
|
538 | - // make sure ticket quantity is set |
|
539 | - $ticket_quantity = isset($this->ticket_quantities[$ticket_identifier]) |
|
540 | - ? $this->ticket_quantities[$ticket_identifier] |
|
541 | - : 0; |
|
542 | - if ($this->debug) { |
|
543 | - \EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__); |
|
544 | - \EEH_Debug_Tools::printr( |
|
545 | - "{$ticket_quantity}", |
|
546 | - "ticket $ticket_identifier quantity: ", |
|
547 | - __FILE__, |
|
548 | - __LINE__, |
|
549 | - 2 |
|
550 | - ); |
|
551 | - } |
|
552 | - if ($ticket_quantity) { |
|
553 | - if ($this->debug) { |
|
554 | - \EEH_Debug_Tools::printr( |
|
555 | - ($spaces_allocated <= $reg_limit) |
|
556 | - ? 'true' |
|
557 | - : 'false', |
|
558 | - ' . spaces_allocated <= reg_limit = ', |
|
559 | - __FILE__, |
|
560 | - __LINE__ |
|
561 | - ); |
|
562 | - } |
|
563 | - // if the datetime is NOT at full capacity yet |
|
564 | - if ($spaces_allocated <= $reg_limit) { |
|
565 | - // then the maximum ticket quantity we can allocate is the lowest value of either: |
|
566 | - // the number of remaining spaces for the datetime, which is the limit - spaces already taken |
|
567 | - // or the maximum ticket quantity |
|
568 | - $ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity); |
|
569 | - // adjust the available quantity in our tracking array |
|
570 | - $this->ticket_quantities[$ticket_identifier] -= $ticket_quantity; |
|
571 | - // and increment spaces allocated for this datetime |
|
572 | - $spaces_allocated += $ticket_quantity; |
|
573 | - $at_capacity = $spaces_allocated >= $reg_limit; |
|
574 | - if ($this->debug) { |
|
575 | - \EEH_Debug_Tools::printr( |
|
576 | - "{$ticket_quantity} {$ticket_identifier} tickets", |
|
577 | - ' > > allocate ', |
|
578 | - __FILE__, |
|
579 | - __LINE__, |
|
580 | - 3 |
|
581 | - ); |
|
582 | - if ($at_capacity) { |
|
583 | - \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3); |
|
584 | - } |
|
585 | - } |
|
586 | - // now adjust all other datetimes that allow access to this ticket |
|
587 | - $this->adjustDatetimes( |
|
588 | - $datetime_identifier, |
|
589 | - $ticket_identifier, |
|
590 | - $ticket_quantity, |
|
591 | - $at_capacity |
|
592 | - ); |
|
593 | - } |
|
594 | - } |
|
595 | - return $spaces_allocated; |
|
596 | - } |
|
597 | - |
|
598 | - |
|
599 | - /** |
|
600 | - * subtracts ticket amounts from all datetime reg limits |
|
601 | - * that allow access to the ticket specified, |
|
602 | - * because that ticket could be used |
|
603 | - * to attend any of the datetimes it has access to |
|
604 | - * |
|
605 | - * @param string $datetime_identifier |
|
606 | - * @param string $ticket_identifier |
|
607 | - * @param bool $at_capacity |
|
608 | - * @param int $ticket_quantity |
|
609 | - */ |
|
610 | - private function adjustDatetimes( |
|
611 | - $datetime_identifier, |
|
612 | - $ticket_identifier, |
|
613 | - $ticket_quantity, |
|
614 | - $at_capacity |
|
615 | - ) { |
|
616 | - /** @var array $datetime_tickets */ |
|
617 | - foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) { |
|
618 | - if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) { |
|
619 | - continue; |
|
620 | - } |
|
621 | - $adjusted = $this->adjustDatetimeSpaces( |
|
622 | - $datetime_ID, |
|
623 | - $ticket_identifier, |
|
624 | - $ticket_quantity |
|
625 | - ); |
|
626 | - // skip to next ticket if nothing changed |
|
627 | - if (! ($adjusted || $at_capacity)) { |
|
628 | - continue; |
|
629 | - } |
|
630 | - // then all of it's tickets are now unavailable |
|
631 | - foreach ($datetime_tickets as $datetime_ticket) { |
|
632 | - if (($ticket_identifier === $datetime_ticket || $at_capacity) |
|
633 | - && isset($this->ticket_quantities[$datetime_ticket]) |
|
634 | - && $this->ticket_quantities[$datetime_ticket] > 0 |
|
635 | - ) { |
|
636 | - if ($this->debug) { |
|
637 | - \EEH_Debug_Tools::printr( |
|
638 | - $datetime_ticket, |
|
639 | - ' . . . adjust ticket quantities for', |
|
640 | - __FILE__, |
|
641 | - __LINE__ |
|
642 | - ); |
|
643 | - } |
|
644 | - // if this datetime is at full capacity, set any tracked available quantities to zero |
|
645 | - // otherwise just subtract the ticket quantity |
|
646 | - $new_quantity = $at_capacity |
|
647 | - ? 0 |
|
648 | - : $this->ticket_quantities[$datetime_ticket] - $ticket_quantity; |
|
649 | - // don't let ticket quantity go below zero |
|
650 | - $this->ticket_quantities[$datetime_ticket] = max($new_quantity, 0); |
|
651 | - if ($this->debug) { |
|
652 | - \EEH_Debug_Tools::printr( |
|
653 | - $at_capacity |
|
654 | - ? "0 because Datetime {$datetime_identifier} is at capacity" |
|
655 | - : "{$this->ticket_quantities[ $datetime_ticket ]}", |
|
656 | - " . . . . {$datetime_ticket} quantity set to ", |
|
657 | - __FILE__, |
|
658 | - __LINE__ |
|
659 | - ); |
|
660 | - } |
|
661 | - } |
|
662 | - // but we also need to adjust spaces for any other datetimes this ticket has access to |
|
663 | - if ($datetime_ticket === $ticket_identifier) { |
|
664 | - if (isset($this->ticket_datetimes[$datetime_ticket]) |
|
665 | - && is_array($this->ticket_datetimes[$datetime_ticket]) |
|
666 | - ) { |
|
667 | - if ($this->debug) { |
|
668 | - \EEH_Debug_Tools::printr( |
|
669 | - $datetime_ticket, |
|
670 | - ' . . adjust other Datetimes for', |
|
671 | - __FILE__, |
|
672 | - __LINE__ |
|
673 | - ); |
|
674 | - } |
|
675 | - foreach ($this->ticket_datetimes[$datetime_ticket] as $datetime) { |
|
676 | - // don't adjust the current datetime twice |
|
677 | - if ($datetime !== $datetime_identifier) { |
|
678 | - $this->adjustDatetimeSpaces( |
|
679 | - $datetime, |
|
680 | - $datetime_ticket, |
|
681 | - $ticket_quantity |
|
682 | - ); |
|
683 | - } |
|
684 | - } |
|
685 | - } |
|
686 | - } |
|
687 | - } |
|
688 | - } |
|
689 | - } |
|
690 | - |
|
691 | - private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0) |
|
692 | - { |
|
693 | - // does datetime have spaces available? |
|
694 | - // and does the supplied ticket have access to this datetime ? |
|
695 | - if ($this->datetime_spaces[$datetime_identifier] > 0 |
|
696 | - && isset($this->datetime_spaces[$datetime_identifier], $this->datetime_tickets[$datetime_identifier]) |
|
697 | - && in_array($ticket_identifier, $this->datetime_tickets[$datetime_identifier], true) |
|
698 | - ) { |
|
699 | - if ($this->debug) { |
|
700 | - \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__); |
|
701 | - \EEH_Debug_Tools::printr( |
|
702 | - "{$this->datetime_spaces[ $datetime_identifier ]}", |
|
703 | - " . . current {$datetime_identifier} spaces available", |
|
704 | - __FILE__, |
|
705 | - __LINE__ |
|
706 | - ); |
|
707 | - } |
|
708 | - // then decrement the available spaces for the datetime |
|
709 | - $this->datetime_spaces[$datetime_identifier] -= $ticket_quantity; |
|
710 | - // but don't let quantities go below zero |
|
711 | - $this->datetime_spaces[$datetime_identifier] = max( |
|
712 | - $this->datetime_spaces[$datetime_identifier], |
|
713 | - 0 |
|
714 | - ); |
|
715 | - if ($this->debug) { |
|
716 | - \EEH_Debug_Tools::printr( |
|
717 | - "{$ticket_quantity}", |
|
718 | - " . . . {$datetime_identifier} capacity reduced by", |
|
719 | - __FILE__, |
|
720 | - __LINE__ |
|
721 | - ); |
|
722 | - } |
|
723 | - return true; |
|
724 | - } |
|
725 | - return false; |
|
726 | - } |
|
29 | + /** |
|
30 | + * @var EE_Event $event |
|
31 | + */ |
|
32 | + private $event; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var array $datetime_query_params |
|
36 | + */ |
|
37 | + private $datetime_query_params; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var EE_Ticket[] $active_tickets |
|
41 | + */ |
|
42 | + private $active_tickets = array(); |
|
43 | + |
|
44 | + /** |
|
45 | + * @var EE_Datetime[] $datetimes |
|
46 | + */ |
|
47 | + private $datetimes = array(); |
|
48 | + |
|
49 | + /** |
|
50 | + * Array of Ticket IDs grouped by Datetime |
|
51 | + * |
|
52 | + * @var array $datetimes |
|
53 | + */ |
|
54 | + private $datetime_tickets = array(); |
|
55 | + |
|
56 | + /** |
|
57 | + * Max spaces for each Datetime (reg limit - previous sold) |
|
58 | + * |
|
59 | + * @var array $datetime_spaces |
|
60 | + */ |
|
61 | + private $datetime_spaces = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * Array of Datetime IDs grouped by Ticket |
|
65 | + * |
|
66 | + * @var array[] $ticket_datetimes |
|
67 | + */ |
|
68 | + private $ticket_datetimes = array(); |
|
69 | + |
|
70 | + /** |
|
71 | + * maximum ticket quantities for each ticket (adjusted for reg limit) |
|
72 | + * |
|
73 | + * @var array $ticket_quantities |
|
74 | + */ |
|
75 | + private $ticket_quantities = array(); |
|
76 | + |
|
77 | + /** |
|
78 | + * total quantity of sold and reserved for each ticket |
|
79 | + * |
|
80 | + * @var array $tickets_sold |
|
81 | + */ |
|
82 | + private $tickets_sold = array(); |
|
83 | + |
|
84 | + /** |
|
85 | + * total spaces available across all datetimes |
|
86 | + * |
|
87 | + * @var array $total_spaces |
|
88 | + */ |
|
89 | + private $total_spaces = array(); |
|
90 | + |
|
91 | + /** |
|
92 | + * @var boolean $debug |
|
93 | + */ |
|
94 | + private $debug = false; // true false |
|
95 | + |
|
96 | + /** |
|
97 | + * @var null|int $spaces_remaining |
|
98 | + */ |
|
99 | + private $spaces_remaining; |
|
100 | + |
|
101 | + /** |
|
102 | + * @var null|int $total_spaces_available |
|
103 | + */ |
|
104 | + private $total_spaces_available; |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * EventSpacesCalculator constructor. |
|
109 | + * |
|
110 | + * @param EE_Event $event |
|
111 | + * @param array $datetime_query_params |
|
112 | + * @throws EE_Error |
|
113 | + */ |
|
114 | + public function __construct(EE_Event $event, array $datetime_query_params = array()) |
|
115 | + { |
|
116 | + $this->event = $event; |
|
117 | + $this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC')); |
|
118 | + $this->setHooks(); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + private function setHooks() |
|
126 | + { |
|
127 | + add_action('AHEE__EE_Ticket__increase_sold', array($this, 'clearResults')); |
|
128 | + add_action('AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults')); |
|
129 | + add_action('AHEE__EE_Datetime__increase_sold', array($this, 'clearResults')); |
|
130 | + add_action('AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults')); |
|
131 | + add_action('AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults')); |
|
132 | + add_action('AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults')); |
|
133 | + add_action('AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults')); |
|
134 | + add_action('AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults')); |
|
135 | + } |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * @return void |
|
140 | + */ |
|
141 | + public function clearResults() |
|
142 | + { |
|
143 | + $this->spaces_remaining = null; |
|
144 | + $this->total_spaces_available = null; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * @return EE_Ticket[] |
|
150 | + * @throws EE_Error |
|
151 | + * @throws InvalidDataTypeException |
|
152 | + * @throws InvalidInterfaceException |
|
153 | + * @throws InvalidArgumentException |
|
154 | + */ |
|
155 | + public function getActiveTickets() |
|
156 | + { |
|
157 | + if (empty($this->active_tickets)) { |
|
158 | + $this->active_tickets = $this->event->tickets( |
|
159 | + array( |
|
160 | + array('TKT_deleted' => false), |
|
161 | + 'order_by' => array('TKT_qty' => 'ASC'), |
|
162 | + ) |
|
163 | + ); |
|
164 | + } |
|
165 | + return $this->active_tickets; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * @param EE_Ticket[] $active_tickets |
|
171 | + * @throws EE_Error |
|
172 | + * @throws DomainException |
|
173 | + * @throws UnexpectedEntityException |
|
174 | + */ |
|
175 | + public function setActiveTickets(array $active_tickets = array()) |
|
176 | + { |
|
177 | + if (! empty($active_tickets)) { |
|
178 | + foreach ($active_tickets as $active_ticket) { |
|
179 | + $this->validateTicket($active_ticket); |
|
180 | + } |
|
181 | + // sort incoming array by ticket quantity (asc) |
|
182 | + usort( |
|
183 | + $active_tickets, |
|
184 | + function (EE_Ticket $a, EE_Ticket $b) { |
|
185 | + if ($a->qty() === $b->qty()) { |
|
186 | + return 0; |
|
187 | + } |
|
188 | + return ($a->qty() < $b->qty()) |
|
189 | + ? -1 |
|
190 | + : 1; |
|
191 | + } |
|
192 | + ); |
|
193 | + } |
|
194 | + $this->active_tickets = $active_tickets; |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * @param $ticket |
|
200 | + * @throws DomainException |
|
201 | + * @throws EE_Error |
|
202 | + * @throws UnexpectedEntityException |
|
203 | + */ |
|
204 | + private function validateTicket($ticket) |
|
205 | + { |
|
206 | + if (! $ticket instanceof EE_Ticket) { |
|
207 | + throw new DomainException( |
|
208 | + esc_html__( |
|
209 | + 'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.', |
|
210 | + 'event_espresso' |
|
211 | + ) |
|
212 | + ); |
|
213 | + } |
|
214 | + if ($ticket->get_event_ID() !== $this->event->ID()) { |
|
215 | + throw new DomainException( |
|
216 | + sprintf( |
|
217 | + esc_html__( |
|
218 | + 'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.', |
|
219 | + 'event_espresso' |
|
220 | + ), |
|
221 | + $ticket->get_event_ID(), |
|
222 | + $this->event->ID() |
|
223 | + ) |
|
224 | + ); |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + |
|
229 | + /** |
|
230 | + * @return EE_Datetime[] |
|
231 | + */ |
|
232 | + public function getDatetimes() |
|
233 | + { |
|
234 | + return $this->datetimes; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * @param EE_Datetime $datetime |
|
240 | + * @throws EE_Error |
|
241 | + * @throws DomainException |
|
242 | + */ |
|
243 | + public function setDatetime(EE_Datetime $datetime) |
|
244 | + { |
|
245 | + if ($datetime->event()->ID() !== $this->event->ID()) { |
|
246 | + throw new DomainException( |
|
247 | + sprintf( |
|
248 | + esc_html__( |
|
249 | + 'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.', |
|
250 | + 'event_espresso' |
|
251 | + ), |
|
252 | + $datetime->event()->ID(), |
|
253 | + $this->event->ID() |
|
254 | + ) |
|
255 | + ); |
|
256 | + } |
|
257 | + $this->datetimes[$datetime->ID()] = $datetime; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * calculate spaces remaining based on "saleable" tickets |
|
263 | + * |
|
264 | + * @return float|int |
|
265 | + * @throws EE_Error |
|
266 | + * @throws DomainException |
|
267 | + * @throws UnexpectedEntityException |
|
268 | + * @throws InvalidDataTypeException |
|
269 | + * @throws InvalidInterfaceException |
|
270 | + * @throws InvalidArgumentException |
|
271 | + */ |
|
272 | + public function spacesRemaining() |
|
273 | + { |
|
274 | + if ($this->spaces_remaining === null) { |
|
275 | + $this->initialize(); |
|
276 | + $this->spaces_remaining = $this->calculate(); |
|
277 | + } |
|
278 | + return $this->spaces_remaining; |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * calculates total available spaces for an event with no regard for sold tickets |
|
284 | + * |
|
285 | + * @return int|float |
|
286 | + * @throws EE_Error |
|
287 | + * @throws DomainException |
|
288 | + * @throws UnexpectedEntityException |
|
289 | + * @throws InvalidDataTypeException |
|
290 | + * @throws InvalidInterfaceException |
|
291 | + * @throws InvalidArgumentException |
|
292 | + */ |
|
293 | + public function totalSpacesAvailable() |
|
294 | + { |
|
295 | + if ($this->total_spaces_available === null) { |
|
296 | + $this->initialize(); |
|
297 | + $this->total_spaces_available = $this->calculate(false); |
|
298 | + } |
|
299 | + return $this->total_spaces_available; |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * Loops through the active tickets for the event |
|
305 | + * and builds a series of data arrays that will be used for calculating |
|
306 | + * the total maximum available spaces, as well as the spaces remaining. |
|
307 | + * Because ticket quantities affect datetime spaces and vice versa, |
|
308 | + * we need to be constantly updating these data arrays as things change, |
|
309 | + * which is the entire reason for their existence. |
|
310 | + * |
|
311 | + * @throws EE_Error |
|
312 | + * @throws DomainException |
|
313 | + * @throws UnexpectedEntityException |
|
314 | + * @throws InvalidDataTypeException |
|
315 | + * @throws InvalidInterfaceException |
|
316 | + * @throws InvalidArgumentException |
|
317 | + */ |
|
318 | + private function initialize() |
|
319 | + { |
|
320 | + if ($this->debug) { |
|
321 | + \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
322 | + } |
|
323 | + $this->datetime_tickets = array(); |
|
324 | + $this->datetime_spaces = array(); |
|
325 | + $this->ticket_datetimes = array(); |
|
326 | + $this->ticket_quantities = array(); |
|
327 | + $this->tickets_sold = array(); |
|
328 | + $this->total_spaces = array(); |
|
329 | + $active_tickets = $this->getActiveTickets(); |
|
330 | + if (! empty($active_tickets)) { |
|
331 | + foreach ($active_tickets as $ticket) { |
|
332 | + $this->validateTicket($ticket); |
|
333 | + // we need to index our data arrays using strings for the purpose of sorting, |
|
334 | + // but we also need them to be unique, so we'll just prepend a letter T to the ID |
|
335 | + $ticket_identifier = "T{$ticket->ID()}"; |
|
336 | + // to start, we'll just consider the raw qty to be the maximum availability for this ticket, |
|
337 | + // unless the ticket is past its "sell until" date, in which case the qty will be 0 |
|
338 | + $max_tickets = $ticket->is_expired() ? 0 : $ticket->qty(); |
|
339 | + // but we'll adjust that after looping over each datetime for the ticket and checking reg limits |
|
340 | + $ticket_datetimes = $ticket->datetimes($this->datetime_query_params); |
|
341 | + foreach ($ticket_datetimes as $datetime) { |
|
342 | + // save all datetimes |
|
343 | + $this->setDatetime($datetime); |
|
344 | + $datetime_identifier = "D{$datetime->ID()}"; |
|
345 | + $reg_limit = $datetime->reg_limit(); |
|
346 | + // ticket quantity can not exceed datetime reg limit |
|
347 | + $max_tickets = min($max_tickets, $reg_limit); |
|
348 | + // as described earlier, because we need to be able to constantly adjust numbers for things, |
|
349 | + // we are going to move all of our data into the following arrays: |
|
350 | + // datetime spaces initially represents the reg limit for each datetime, |
|
351 | + // but this will get adjusted as tickets are accounted for |
|
352 | + $this->datetime_spaces[$datetime_identifier] = $reg_limit; |
|
353 | + // just an array of ticket IDs grouped by datetime |
|
354 | + $this->datetime_tickets[$datetime_identifier][] = $ticket_identifier; |
|
355 | + // and an array of datetime IDs grouped by ticket |
|
356 | + $this->ticket_datetimes[$ticket_identifier][] = $datetime_identifier; |
|
357 | + } |
|
358 | + // total quantity of sold and reserved for each ticket |
|
359 | + $this->tickets_sold[$ticket_identifier] = $ticket->sold() + $ticket->reserved(); |
|
360 | + // and the maximum ticket quantities for each ticket (adjusted for reg limit) |
|
361 | + $this->ticket_quantities[$ticket_identifier] = $max_tickets; |
|
362 | + } |
|
363 | + } |
|
364 | + // sort datetime spaces by reg limit, but maintain our string indexes |
|
365 | + asort($this->datetime_spaces, SORT_NUMERIC); |
|
366 | + // datetime tickets need to be sorted in the SAME order as the above array... |
|
367 | + // so we'll just use array_merge() to take the structure of datetime_spaces |
|
368 | + // but overwrite all of the data with that from datetime_tickets |
|
369 | + $this->datetime_tickets = array_merge( |
|
370 | + $this->datetime_spaces, |
|
371 | + $this->datetime_tickets |
|
372 | + ); |
|
373 | + if ($this->debug) { |
|
374 | + \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__); |
|
375 | + \EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__); |
|
376 | + \EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__); |
|
377 | + } |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * performs calculations on initialized data |
|
383 | + * |
|
384 | + * @param bool $consider_sold |
|
385 | + * @return int|float |
|
386 | + */ |
|
387 | + private function calculate($consider_sold = true) |
|
388 | + { |
|
389 | + if ($this->debug) { |
|
390 | + \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
391 | + \EEH_Debug_Tools::printr($consider_sold, '$consider_sold', __FILE__, __LINE__); |
|
392 | + } |
|
393 | + if ($consider_sold) { |
|
394 | + // subtract amounts sold from all ticket quantities and datetime spaces |
|
395 | + $this->adjustTicketQuantitiesDueToSales(); |
|
396 | + } |
|
397 | + foreach ($this->datetime_tickets as $datetime_identifier => $tickets) { |
|
398 | + $this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets); |
|
399 | + } |
|
400 | + // total spaces available is just the sum of the spaces available for each datetime |
|
401 | + $spaces_remaining = array_sum($this->total_spaces); |
|
402 | + if ($this->debug) { |
|
403 | + \EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__); |
|
404 | + \EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__); |
|
405 | + \EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__); |
|
406 | + } |
|
407 | + return $spaces_remaining; |
|
408 | + } |
|
409 | + |
|
410 | + |
|
411 | + /** |
|
412 | + * subtracts amount of tickets sold from ticket quantities and datetime spaces |
|
413 | + */ |
|
414 | + private function adjustTicketQuantitiesDueToSales() |
|
415 | + { |
|
416 | + if ($this->debug) { |
|
417 | + \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2); |
|
418 | + } |
|
419 | + foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) { |
|
420 | + if (isset($this->ticket_quantities[$ticket_identifier])) { |
|
421 | + $this->ticket_quantities[$ticket_identifier] -= $tickets_sold; |
|
422 | + // don't let values go below zero |
|
423 | + $this->ticket_quantities[$ticket_identifier] = max( |
|
424 | + $this->ticket_quantities[$ticket_identifier], |
|
425 | + 0 |
|
426 | + ); |
|
427 | + if ($this->debug) { |
|
428 | + \EEH_Debug_Tools::printr( |
|
429 | + "{$tickets_sold} sales for ticket {$ticket_identifier} ", |
|
430 | + 'subtracting', |
|
431 | + __FILE__, |
|
432 | + __LINE__ |
|
433 | + ); |
|
434 | + } |
|
435 | + } |
|
436 | + if (isset($this->ticket_datetimes[$ticket_identifier]) |
|
437 | + && is_array($this->ticket_datetimes[$ticket_identifier]) |
|
438 | + ) { |
|
439 | + foreach ($this->ticket_datetimes[$ticket_identifier] as $ticket_datetime) { |
|
440 | + if (isset($this->ticket_quantities[$ticket_identifier])) { |
|
441 | + $this->datetime_spaces[$ticket_datetime] -= $tickets_sold; |
|
442 | + // don't let values go below zero |
|
443 | + $this->datetime_spaces[$ticket_datetime] = max( |
|
444 | + $this->datetime_spaces[$ticket_datetime], |
|
445 | + 0 |
|
446 | + ); |
|
447 | + if ($this->debug) { |
|
448 | + \EEH_Debug_Tools::printr( |
|
449 | + "{$tickets_sold} sales for datetime {$ticket_datetime} ", |
|
450 | + 'subtracting', |
|
451 | + __FILE__, |
|
452 | + __LINE__ |
|
453 | + ); |
|
454 | + } |
|
455 | + } |
|
456 | + } |
|
457 | + } |
|
458 | + } |
|
459 | + } |
|
460 | + |
|
461 | + |
|
462 | + /** |
|
463 | + * @param string $datetime_identifier |
|
464 | + * @param array $tickets |
|
465 | + */ |
|
466 | + private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets) |
|
467 | + { |
|
468 | + // make sure a reg limit is set for the datetime |
|
469 | + $reg_limit = isset($this->datetime_spaces[$datetime_identifier]) |
|
470 | + ? $this->datetime_spaces[$datetime_identifier] |
|
471 | + : 0; |
|
472 | + // and bail if it is not |
|
473 | + if (! $reg_limit) { |
|
474 | + if ($this->debug) { |
|
475 | + \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__); |
|
476 | + } |
|
477 | + return; |
|
478 | + } |
|
479 | + if ($this->debug) { |
|
480 | + \EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1); |
|
481 | + \EEH_Debug_Tools::printr( |
|
482 | + "{$reg_limit}", |
|
483 | + 'REG LIMIT', |
|
484 | + __FILE__, |
|
485 | + __LINE__ |
|
486 | + ); |
|
487 | + } |
|
488 | + // number of allocated spaces always starts at zero |
|
489 | + $spaces_allocated = 0; |
|
490 | + $this->total_spaces[$datetime_identifier] = 0; |
|
491 | + foreach ($tickets as $ticket_identifier) { |
|
492 | + $spaces_allocated = $this->calculateAvailableSpacesForTicket( |
|
493 | + $datetime_identifier, |
|
494 | + $reg_limit, |
|
495 | + $ticket_identifier, |
|
496 | + $spaces_allocated |
|
497 | + ); |
|
498 | + } |
|
499 | + // spaces can't be negative |
|
500 | + $spaces_allocated = max($spaces_allocated, 0); |
|
501 | + if ($spaces_allocated) { |
|
502 | + // track any non-zero values |
|
503 | + $this->total_spaces[$datetime_identifier] += $spaces_allocated; |
|
504 | + if ($this->debug) { |
|
505 | + \EEH_Debug_Tools::printr((string)$spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__); |
|
506 | + } |
|
507 | + } else { |
|
508 | + if ($this->debug) { |
|
509 | + \EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__); |
|
510 | + } |
|
511 | + } |
|
512 | + if ($this->debug) { |
|
513 | + \EEH_Debug_Tools::printr( |
|
514 | + $this->total_spaces[$datetime_identifier], |
|
515 | + '$total_spaces', |
|
516 | + __FILE__, |
|
517 | + __LINE__ |
|
518 | + ); |
|
519 | + \EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__); |
|
520 | + \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__); |
|
521 | + } |
|
522 | + } |
|
523 | + |
|
524 | + |
|
525 | + /** |
|
526 | + * @param string $datetime_identifier |
|
527 | + * @param int $reg_limit |
|
528 | + * @param string $ticket_identifier |
|
529 | + * @param int $spaces_allocated |
|
530 | + * @return int |
|
531 | + */ |
|
532 | + private function calculateAvailableSpacesForTicket( |
|
533 | + $datetime_identifier, |
|
534 | + $reg_limit, |
|
535 | + $ticket_identifier, |
|
536 | + $spaces_allocated |
|
537 | + ) { |
|
538 | + // make sure ticket quantity is set |
|
539 | + $ticket_quantity = isset($this->ticket_quantities[$ticket_identifier]) |
|
540 | + ? $this->ticket_quantities[$ticket_identifier] |
|
541 | + : 0; |
|
542 | + if ($this->debug) { |
|
543 | + \EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__); |
|
544 | + \EEH_Debug_Tools::printr( |
|
545 | + "{$ticket_quantity}", |
|
546 | + "ticket $ticket_identifier quantity: ", |
|
547 | + __FILE__, |
|
548 | + __LINE__, |
|
549 | + 2 |
|
550 | + ); |
|
551 | + } |
|
552 | + if ($ticket_quantity) { |
|
553 | + if ($this->debug) { |
|
554 | + \EEH_Debug_Tools::printr( |
|
555 | + ($spaces_allocated <= $reg_limit) |
|
556 | + ? 'true' |
|
557 | + : 'false', |
|
558 | + ' . spaces_allocated <= reg_limit = ', |
|
559 | + __FILE__, |
|
560 | + __LINE__ |
|
561 | + ); |
|
562 | + } |
|
563 | + // if the datetime is NOT at full capacity yet |
|
564 | + if ($spaces_allocated <= $reg_limit) { |
|
565 | + // then the maximum ticket quantity we can allocate is the lowest value of either: |
|
566 | + // the number of remaining spaces for the datetime, which is the limit - spaces already taken |
|
567 | + // or the maximum ticket quantity |
|
568 | + $ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity); |
|
569 | + // adjust the available quantity in our tracking array |
|
570 | + $this->ticket_quantities[$ticket_identifier] -= $ticket_quantity; |
|
571 | + // and increment spaces allocated for this datetime |
|
572 | + $spaces_allocated += $ticket_quantity; |
|
573 | + $at_capacity = $spaces_allocated >= $reg_limit; |
|
574 | + if ($this->debug) { |
|
575 | + \EEH_Debug_Tools::printr( |
|
576 | + "{$ticket_quantity} {$ticket_identifier} tickets", |
|
577 | + ' > > allocate ', |
|
578 | + __FILE__, |
|
579 | + __LINE__, |
|
580 | + 3 |
|
581 | + ); |
|
582 | + if ($at_capacity) { |
|
583 | + \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3); |
|
584 | + } |
|
585 | + } |
|
586 | + // now adjust all other datetimes that allow access to this ticket |
|
587 | + $this->adjustDatetimes( |
|
588 | + $datetime_identifier, |
|
589 | + $ticket_identifier, |
|
590 | + $ticket_quantity, |
|
591 | + $at_capacity |
|
592 | + ); |
|
593 | + } |
|
594 | + } |
|
595 | + return $spaces_allocated; |
|
596 | + } |
|
597 | + |
|
598 | + |
|
599 | + /** |
|
600 | + * subtracts ticket amounts from all datetime reg limits |
|
601 | + * that allow access to the ticket specified, |
|
602 | + * because that ticket could be used |
|
603 | + * to attend any of the datetimes it has access to |
|
604 | + * |
|
605 | + * @param string $datetime_identifier |
|
606 | + * @param string $ticket_identifier |
|
607 | + * @param bool $at_capacity |
|
608 | + * @param int $ticket_quantity |
|
609 | + */ |
|
610 | + private function adjustDatetimes( |
|
611 | + $datetime_identifier, |
|
612 | + $ticket_identifier, |
|
613 | + $ticket_quantity, |
|
614 | + $at_capacity |
|
615 | + ) { |
|
616 | + /** @var array $datetime_tickets */ |
|
617 | + foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) { |
|
618 | + if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) { |
|
619 | + continue; |
|
620 | + } |
|
621 | + $adjusted = $this->adjustDatetimeSpaces( |
|
622 | + $datetime_ID, |
|
623 | + $ticket_identifier, |
|
624 | + $ticket_quantity |
|
625 | + ); |
|
626 | + // skip to next ticket if nothing changed |
|
627 | + if (! ($adjusted || $at_capacity)) { |
|
628 | + continue; |
|
629 | + } |
|
630 | + // then all of it's tickets are now unavailable |
|
631 | + foreach ($datetime_tickets as $datetime_ticket) { |
|
632 | + if (($ticket_identifier === $datetime_ticket || $at_capacity) |
|
633 | + && isset($this->ticket_quantities[$datetime_ticket]) |
|
634 | + && $this->ticket_quantities[$datetime_ticket] > 0 |
|
635 | + ) { |
|
636 | + if ($this->debug) { |
|
637 | + \EEH_Debug_Tools::printr( |
|
638 | + $datetime_ticket, |
|
639 | + ' . . . adjust ticket quantities for', |
|
640 | + __FILE__, |
|
641 | + __LINE__ |
|
642 | + ); |
|
643 | + } |
|
644 | + // if this datetime is at full capacity, set any tracked available quantities to zero |
|
645 | + // otherwise just subtract the ticket quantity |
|
646 | + $new_quantity = $at_capacity |
|
647 | + ? 0 |
|
648 | + : $this->ticket_quantities[$datetime_ticket] - $ticket_quantity; |
|
649 | + // don't let ticket quantity go below zero |
|
650 | + $this->ticket_quantities[$datetime_ticket] = max($new_quantity, 0); |
|
651 | + if ($this->debug) { |
|
652 | + \EEH_Debug_Tools::printr( |
|
653 | + $at_capacity |
|
654 | + ? "0 because Datetime {$datetime_identifier} is at capacity" |
|
655 | + : "{$this->ticket_quantities[ $datetime_ticket ]}", |
|
656 | + " . . . . {$datetime_ticket} quantity set to ", |
|
657 | + __FILE__, |
|
658 | + __LINE__ |
|
659 | + ); |
|
660 | + } |
|
661 | + } |
|
662 | + // but we also need to adjust spaces for any other datetimes this ticket has access to |
|
663 | + if ($datetime_ticket === $ticket_identifier) { |
|
664 | + if (isset($this->ticket_datetimes[$datetime_ticket]) |
|
665 | + && is_array($this->ticket_datetimes[$datetime_ticket]) |
|
666 | + ) { |
|
667 | + if ($this->debug) { |
|
668 | + \EEH_Debug_Tools::printr( |
|
669 | + $datetime_ticket, |
|
670 | + ' . . adjust other Datetimes for', |
|
671 | + __FILE__, |
|
672 | + __LINE__ |
|
673 | + ); |
|
674 | + } |
|
675 | + foreach ($this->ticket_datetimes[$datetime_ticket] as $datetime) { |
|
676 | + // don't adjust the current datetime twice |
|
677 | + if ($datetime !== $datetime_identifier) { |
|
678 | + $this->adjustDatetimeSpaces( |
|
679 | + $datetime, |
|
680 | + $datetime_ticket, |
|
681 | + $ticket_quantity |
|
682 | + ); |
|
683 | + } |
|
684 | + } |
|
685 | + } |
|
686 | + } |
|
687 | + } |
|
688 | + } |
|
689 | + } |
|
690 | + |
|
691 | + private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0) |
|
692 | + { |
|
693 | + // does datetime have spaces available? |
|
694 | + // and does the supplied ticket have access to this datetime ? |
|
695 | + if ($this->datetime_spaces[$datetime_identifier] > 0 |
|
696 | + && isset($this->datetime_spaces[$datetime_identifier], $this->datetime_tickets[$datetime_identifier]) |
|
697 | + && in_array($ticket_identifier, $this->datetime_tickets[$datetime_identifier], true) |
|
698 | + ) { |
|
699 | + if ($this->debug) { |
|
700 | + \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__); |
|
701 | + \EEH_Debug_Tools::printr( |
|
702 | + "{$this->datetime_spaces[ $datetime_identifier ]}", |
|
703 | + " . . current {$datetime_identifier} spaces available", |
|
704 | + __FILE__, |
|
705 | + __LINE__ |
|
706 | + ); |
|
707 | + } |
|
708 | + // then decrement the available spaces for the datetime |
|
709 | + $this->datetime_spaces[$datetime_identifier] -= $ticket_quantity; |
|
710 | + // but don't let quantities go below zero |
|
711 | + $this->datetime_spaces[$datetime_identifier] = max( |
|
712 | + $this->datetime_spaces[$datetime_identifier], |
|
713 | + 0 |
|
714 | + ); |
|
715 | + if ($this->debug) { |
|
716 | + \EEH_Debug_Tools::printr( |
|
717 | + "{$ticket_quantity}", |
|
718 | + " . . . {$datetime_identifier} capacity reduced by", |
|
719 | + __FILE__, |
|
720 | + __LINE__ |
|
721 | + ); |
|
722 | + } |
|
723 | + return true; |
|
724 | + } |
|
725 | + return false; |
|
726 | + } |
|
727 | 727 | } |
@@ -174,14 +174,14 @@ discard block |
||
174 | 174 | */ |
175 | 175 | public function setActiveTickets(array $active_tickets = array()) |
176 | 176 | { |
177 | - if (! empty($active_tickets)) { |
|
177 | + if ( ! empty($active_tickets)) { |
|
178 | 178 | foreach ($active_tickets as $active_ticket) { |
179 | 179 | $this->validateTicket($active_ticket); |
180 | 180 | } |
181 | 181 | // sort incoming array by ticket quantity (asc) |
182 | 182 | usort( |
183 | 183 | $active_tickets, |
184 | - function (EE_Ticket $a, EE_Ticket $b) { |
|
184 | + function(EE_Ticket $a, EE_Ticket $b) { |
|
185 | 185 | if ($a->qty() === $b->qty()) { |
186 | 186 | return 0; |
187 | 187 | } |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | */ |
204 | 204 | private function validateTicket($ticket) |
205 | 205 | { |
206 | - if (! $ticket instanceof EE_Ticket) { |
|
206 | + if ( ! $ticket instanceof EE_Ticket) { |
|
207 | 207 | throw new DomainException( |
208 | 208 | esc_html__( |
209 | 209 | 'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.', |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | $this->tickets_sold = array(); |
328 | 328 | $this->total_spaces = array(); |
329 | 329 | $active_tickets = $this->getActiveTickets(); |
330 | - if (! empty($active_tickets)) { |
|
330 | + if ( ! empty($active_tickets)) { |
|
331 | 331 | foreach ($active_tickets as $ticket) { |
332 | 332 | $this->validateTicket($ticket); |
333 | 333 | // we need to index our data arrays using strings for the purpose of sorting, |
@@ -470,7 +470,7 @@ discard block |
||
470 | 470 | ? $this->datetime_spaces[$datetime_identifier] |
471 | 471 | : 0; |
472 | 472 | // and bail if it is not |
473 | - if (! $reg_limit) { |
|
473 | + if ( ! $reg_limit) { |
|
474 | 474 | if ($this->debug) { |
475 | 475 | \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__); |
476 | 476 | } |
@@ -502,7 +502,7 @@ discard block |
||
502 | 502 | // track any non-zero values |
503 | 503 | $this->total_spaces[$datetime_identifier] += $spaces_allocated; |
504 | 504 | if ($this->debug) { |
505 | - \EEH_Debug_Tools::printr((string)$spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__); |
|
505 | + \EEH_Debug_Tools::printr((string) $spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__); |
|
506 | 506 | } |
507 | 507 | } else { |
508 | 508 | if ($this->debug) { |
@@ -624,7 +624,7 @@ discard block |
||
624 | 624 | $ticket_quantity |
625 | 625 | ); |
626 | 626 | // skip to next ticket if nothing changed |
627 | - if (! ($adjusted || $at_capacity)) { |
|
627 | + if ( ! ($adjusted || $at_capacity)) { |
|
628 | 628 | continue; |
629 | 629 | } |
630 | 630 | // then all of it's tickets are now unavailable |
@@ -652,7 +652,7 @@ discard block |
||
652 | 652 | \EEH_Debug_Tools::printr( |
653 | 653 | $at_capacity |
654 | 654 | ? "0 because Datetime {$datetime_identifier} is at capacity" |
655 | - : "{$this->ticket_quantities[ $datetime_ticket ]}", |
|
655 | + : "{$this->ticket_quantities[$datetime_ticket]}", |
|
656 | 656 | " . . . . {$datetime_ticket} quantity set to ", |
657 | 657 | __FILE__, |
658 | 658 | __LINE__ |
@@ -699,7 +699,7 @@ discard block |
||
699 | 699 | if ($this->debug) { |
700 | 700 | \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__); |
701 | 701 | \EEH_Debug_Tools::printr( |
702 | - "{$this->datetime_spaces[ $datetime_identifier ]}", |
|
702 | + "{$this->datetime_spaces[$datetime_identifier]}", |
|
703 | 703 | " . . current {$datetime_identifier} spaces available", |
704 | 704 | __FILE__, |
705 | 705 | __LINE__ |
@@ -16,131 +16,131 @@ |
||
16 | 16 | */ |
17 | 17 | class Config |
18 | 18 | { |
19 | - /** |
|
20 | - * @var EE_Network_Config |
|
21 | - */ |
|
22 | - private $network_config; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * @var EE_Config |
|
27 | - */ |
|
28 | - private $ee_config; |
|
29 | - |
|
30 | - |
|
31 | - public function __construct(EE_Network_Config $network_config, EE_Config $ee_config) |
|
32 | - { |
|
33 | - $this->network_config = $network_config; |
|
34 | - $this->ee_config = $ee_config; |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * Get the site license key for the site. |
|
40 | - */ |
|
41 | - public function siteLicenseKey() |
|
42 | - { |
|
43 | - return $this->network_config->core->site_license_key; |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - public function i18nDomain() |
|
48 | - { |
|
49 | - return 'event_espresso'; |
|
50 | - } |
|
51 | - |
|
52 | - |
|
53 | - public function checkPeriod() |
|
54 | - { |
|
55 | - return 24; |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - public function optionKey() |
|
60 | - { |
|
61 | - return 'site_license_key'; |
|
62 | - } |
|
63 | - |
|
64 | - |
|
65 | - public function optionsPageSlug() |
|
66 | - { |
|
67 | - return 'espresso_general_settings'; |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - public function hostServerUrl() |
|
72 | - { |
|
73 | - return defined('PUE_UPDATES_ENDPOINT') |
|
74 | - ? PUE_UPDATES_ENDPOINT |
|
75 | - : 'https://eventespresso.com'; |
|
76 | - } |
|
77 | - |
|
78 | - |
|
79 | - public function pluginSlug() |
|
80 | - { |
|
81 | - // Note: PUE uses a simple preg_match to determine what type is currently installed based on version number. |
|
82 | - // So it's important that you use a key for the version type that is unique and not found in another key. |
|
83 | - // For example: |
|
84 | - // $plugin_slug['premium']['p'] = 'some-premium-slug'; |
|
85 | - // $plugin_slug['prerelease']['pr'] = 'some-pre-release-slug'; |
|
86 | - // The above would not work because "p" is found in both keys for the version type. ( i.e 1.0.p vs 1.0.pr ) |
|
87 | - // so doing something like: |
|
88 | - // $plugin_slug['premium']['p'] = 'some-premium-slug'; |
|
89 | - // $plugin_slug['prerelease']['b'] = 'some-pre-release-slug'; |
|
90 | - // ..WOULD work! |
|
91 | - return array( |
|
92 | - 'free' => array('decaf' => 'event-espresso-core-decaf'), |
|
93 | - 'premium' => array('p' => 'event-espresso-core-reg'), |
|
94 | - 'prerelease' => array('beta' => 'event-espresso-core-pr'), |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * Return whether the site is opted in for UXIP or not. |
|
101 | - * |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public function isOptedInForUxip() |
|
105 | - { |
|
106 | - return filter_var($this->ee_config->core->ee_ueip_optin, FILTER_VALIDATE_BOOLEAN); |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * Return whether the site has been notified about UXIP or not. |
|
112 | - * |
|
113 | - * @return bool |
|
114 | - */ |
|
115 | - public function hasNotifiedForUxip() |
|
116 | - { |
|
117 | - return filter_var($this->ee_config->core->ee_ueip_has_notified, FILTER_VALIDATE_BOOLEAN); |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Set the site opted in for UXIP. |
|
123 | - */ |
|
124 | - public function setHasOptedInForUxip() |
|
125 | - { |
|
126 | - $this->ee_config->core->ee_ueip_optin = true; |
|
127 | - $this->ee_config->update_espresso_config(false, false); |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * Set the site opted out for UXIP |
|
133 | - */ |
|
134 | - public function setHasOptedOutForUxip() |
|
135 | - { |
|
136 | - $this->ee_config->core->ee_ueip_optin = false; |
|
137 | - $this->ee_config->update_espresso_config(false, false); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - public function setHasNotifiedAboutUxip() |
|
142 | - { |
|
143 | - $this->ee_config->core->ee_ueip_has_notified = true; |
|
144 | - $this->ee_config->update_espresso_config(false, false); |
|
145 | - } |
|
19 | + /** |
|
20 | + * @var EE_Network_Config |
|
21 | + */ |
|
22 | + private $network_config; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * @var EE_Config |
|
27 | + */ |
|
28 | + private $ee_config; |
|
29 | + |
|
30 | + |
|
31 | + public function __construct(EE_Network_Config $network_config, EE_Config $ee_config) |
|
32 | + { |
|
33 | + $this->network_config = $network_config; |
|
34 | + $this->ee_config = $ee_config; |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * Get the site license key for the site. |
|
40 | + */ |
|
41 | + public function siteLicenseKey() |
|
42 | + { |
|
43 | + return $this->network_config->core->site_license_key; |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + public function i18nDomain() |
|
48 | + { |
|
49 | + return 'event_espresso'; |
|
50 | + } |
|
51 | + |
|
52 | + |
|
53 | + public function checkPeriod() |
|
54 | + { |
|
55 | + return 24; |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + public function optionKey() |
|
60 | + { |
|
61 | + return 'site_license_key'; |
|
62 | + } |
|
63 | + |
|
64 | + |
|
65 | + public function optionsPageSlug() |
|
66 | + { |
|
67 | + return 'espresso_general_settings'; |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + public function hostServerUrl() |
|
72 | + { |
|
73 | + return defined('PUE_UPDATES_ENDPOINT') |
|
74 | + ? PUE_UPDATES_ENDPOINT |
|
75 | + : 'https://eventespresso.com'; |
|
76 | + } |
|
77 | + |
|
78 | + |
|
79 | + public function pluginSlug() |
|
80 | + { |
|
81 | + // Note: PUE uses a simple preg_match to determine what type is currently installed based on version number. |
|
82 | + // So it's important that you use a key for the version type that is unique and not found in another key. |
|
83 | + // For example: |
|
84 | + // $plugin_slug['premium']['p'] = 'some-premium-slug'; |
|
85 | + // $plugin_slug['prerelease']['pr'] = 'some-pre-release-slug'; |
|
86 | + // The above would not work because "p" is found in both keys for the version type. ( i.e 1.0.p vs 1.0.pr ) |
|
87 | + // so doing something like: |
|
88 | + // $plugin_slug['premium']['p'] = 'some-premium-slug'; |
|
89 | + // $plugin_slug['prerelease']['b'] = 'some-pre-release-slug'; |
|
90 | + // ..WOULD work! |
|
91 | + return array( |
|
92 | + 'free' => array('decaf' => 'event-espresso-core-decaf'), |
|
93 | + 'premium' => array('p' => 'event-espresso-core-reg'), |
|
94 | + 'prerelease' => array('beta' => 'event-espresso-core-pr'), |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * Return whether the site is opted in for UXIP or not. |
|
101 | + * |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public function isOptedInForUxip() |
|
105 | + { |
|
106 | + return filter_var($this->ee_config->core->ee_ueip_optin, FILTER_VALIDATE_BOOLEAN); |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * Return whether the site has been notified about UXIP or not. |
|
112 | + * |
|
113 | + * @return bool |
|
114 | + */ |
|
115 | + public function hasNotifiedForUxip() |
|
116 | + { |
|
117 | + return filter_var($this->ee_config->core->ee_ueip_has_notified, FILTER_VALIDATE_BOOLEAN); |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Set the site opted in for UXIP. |
|
123 | + */ |
|
124 | + public function setHasOptedInForUxip() |
|
125 | + { |
|
126 | + $this->ee_config->core->ee_ueip_optin = true; |
|
127 | + $this->ee_config->update_espresso_config(false, false); |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * Set the site opted out for UXIP |
|
133 | + */ |
|
134 | + public function setHasOptedOutForUxip() |
|
135 | + { |
|
136 | + $this->ee_config->core->ee_ueip_optin = false; |
|
137 | + $this->ee_config->update_espresso_config(false, false); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + public function setHasNotifiedAboutUxip() |
|
142 | + { |
|
143 | + $this->ee_config->core->ee_ueip_has_notified = true; |
|
144 | + $this->ee_config->update_espresso_config(false, false); |
|
145 | + } |
|
146 | 146 | } |
@@ -22,88 +22,88 @@ discard block |
||
22 | 22 | class Stats |
23 | 23 | { |
24 | 24 | |
25 | - const OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS = 'ee_uxip_stats_expiry'; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var Config |
|
29 | - */ |
|
30 | - private $config; |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @var StatsGatherer |
|
35 | - */ |
|
36 | - private $stats_gatherer; |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * @var EE_Maintenance_Mode |
|
41 | - */ |
|
42 | - private $maintenance_mode; |
|
43 | - |
|
44 | - public function __construct( |
|
45 | - Config $config, |
|
46 | - EE_Maintenance_Mode $maintenance_mode, |
|
47 | - StatsGatherer $stats_gatherer |
|
48 | - ) { |
|
49 | - $this->config = $config; |
|
50 | - $this->maintenance_mode = $maintenance_mode; |
|
51 | - $this->stats_gatherer = $stats_gatherer; |
|
52 | - $this->setUxipNotices(); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * Displays uxip opt-in notice if necessary. |
|
58 | - */ |
|
59 | - private function setUxipNotices() |
|
60 | - { |
|
61 | - if ($this->canDisplayNotices()) { |
|
62 | - add_action('admin_notices', array($this, 'optinNotice')); |
|
63 | - add_action('admin_enqueue_scripts', array($this, 'enqueueScripts')); |
|
64 | - add_action('wp_ajax_espresso_data_optin', array($this, 'ajaxHandler')); |
|
65 | - // makes sure optin defaults to yes even if its currently empty. |
|
66 | - $this->config->setHasOptedInForUxip(); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * This returns the callback that PluginUpdateEngineChecker will use for getting any extra stats to send. |
|
73 | - * |
|
74 | - * @return Closure |
|
75 | - */ |
|
76 | - public function statsCallback() |
|
77 | - { |
|
78 | - // returns a callback that can is used to retrieve the stats to send along to the pue server. |
|
79 | - return function () { |
|
80 | - // we only send stats one a week, so let's see if our stat timestamp has expired. |
|
81 | - if (! $this->sendStats()) { |
|
82 | - return array(); |
|
83 | - } |
|
84 | - return $this->stats_gatherer->stats(); |
|
85 | - }; |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Return whether notices can be displayed or not |
|
91 | - * |
|
92 | - * @return bool |
|
93 | - */ |
|
94 | - private function canDisplayNotices() |
|
95 | - { |
|
96 | - return ! $this->config->hasNotifiedForUxip() |
|
97 | - && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance; |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Callback for the admin_notices hook that outputs the UXIP optin-in notice. |
|
103 | - */ |
|
104 | - public function optinNotice() |
|
105 | - { |
|
106 | - ?> |
|
25 | + const OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS = 'ee_uxip_stats_expiry'; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var Config |
|
29 | + */ |
|
30 | + private $config; |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @var StatsGatherer |
|
35 | + */ |
|
36 | + private $stats_gatherer; |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * @var EE_Maintenance_Mode |
|
41 | + */ |
|
42 | + private $maintenance_mode; |
|
43 | + |
|
44 | + public function __construct( |
|
45 | + Config $config, |
|
46 | + EE_Maintenance_Mode $maintenance_mode, |
|
47 | + StatsGatherer $stats_gatherer |
|
48 | + ) { |
|
49 | + $this->config = $config; |
|
50 | + $this->maintenance_mode = $maintenance_mode; |
|
51 | + $this->stats_gatherer = $stats_gatherer; |
|
52 | + $this->setUxipNotices(); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * Displays uxip opt-in notice if necessary. |
|
58 | + */ |
|
59 | + private function setUxipNotices() |
|
60 | + { |
|
61 | + if ($this->canDisplayNotices()) { |
|
62 | + add_action('admin_notices', array($this, 'optinNotice')); |
|
63 | + add_action('admin_enqueue_scripts', array($this, 'enqueueScripts')); |
|
64 | + add_action('wp_ajax_espresso_data_optin', array($this, 'ajaxHandler')); |
|
65 | + // makes sure optin defaults to yes even if its currently empty. |
|
66 | + $this->config->setHasOptedInForUxip(); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * This returns the callback that PluginUpdateEngineChecker will use for getting any extra stats to send. |
|
73 | + * |
|
74 | + * @return Closure |
|
75 | + */ |
|
76 | + public function statsCallback() |
|
77 | + { |
|
78 | + // returns a callback that can is used to retrieve the stats to send along to the pue server. |
|
79 | + return function () { |
|
80 | + // we only send stats one a week, so let's see if our stat timestamp has expired. |
|
81 | + if (! $this->sendStats()) { |
|
82 | + return array(); |
|
83 | + } |
|
84 | + return $this->stats_gatherer->stats(); |
|
85 | + }; |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Return whether notices can be displayed or not |
|
91 | + * |
|
92 | + * @return bool |
|
93 | + */ |
|
94 | + private function canDisplayNotices() |
|
95 | + { |
|
96 | + return ! $this->config->hasNotifiedForUxip() |
|
97 | + && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance; |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Callback for the admin_notices hook that outputs the UXIP optin-in notice. |
|
103 | + */ |
|
104 | + public function optinNotice() |
|
105 | + { |
|
106 | + ?> |
|
107 | 107 | <div class="updated data-collect-optin" id="espresso-data-collect-optin-container"> |
108 | 108 | <div id="data-collect-optin-options-container"> |
109 | 109 | <span class="dashicons dashicons-admin-site"></span> |
@@ -116,125 +116,125 @@ discard block |
||
116 | 116 | </div> |
117 | 117 | </div> |
118 | 118 | <?php |
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * Retrieves the optin text (static so it can be used in multiple places as necessary). |
|
124 | - * |
|
125 | - * @param bool $extra |
|
126 | - */ |
|
127 | - public static function optinText($extra = true) |
|
128 | - { |
|
129 | - if (! $extra) { |
|
130 | - echo '<h2 class="ee-admin-settings-hdr" ' |
|
131 | - . (! $extra ? 'id="UXIP_settings"' : '') |
|
132 | - . '>' |
|
133 | - . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
|
134 | - . EEH_Template::get_help_tab_link('organization_logo_info') |
|
135 | - . '</h2>'; |
|
136 | - printf( |
|
137 | - esc_html__( |
|
138 | - '%1$sPlease help us make Event Espresso better and vote for your favorite features.%2$s The %3$sUser eXperience Improvement Program (UXIP)%4$s, has been created so when you use Event Espresso you are voting for the features and settings that are important to you. The UXIP helps us understand how you use our products and services, track problems and in what context. If you opt-out of the UXIP you essentially elect for us to disregard how you use Event Espresso as we build new features and make changes. Participation in the program is completely voluntary but it is enabled by default. The end results of the UXIP are software improvements to better meet your needs. The data we collect will never be sold, traded, or misused in any way. %5$sPlease see our %6$sPrivacy Policy%7$s for more information.', |
|
139 | - 'event_espresso' |
|
140 | - ), |
|
141 | - '<p><em>', |
|
142 | - '</em></p>', |
|
143 | - '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
144 | - '</a>', |
|
145 | - '<br><br>', |
|
146 | - '<a href="https://eventespresso.com/about/privacy-policy/" target="_blank">', |
|
147 | - '</a>' |
|
148 | - ); |
|
149 | - } else { |
|
150 | - $settings_url = EE_Admin_Page::add_query_args_and_nonce( |
|
151 | - array('action' => 'default'), |
|
152 | - admin_url('admin.php?page=espresso_general_settings') |
|
153 | - ); |
|
154 | - $settings_url .= '#UXIP_settings'; |
|
155 | - printf( |
|
156 | - esc_html__( |
|
157 | - 'The Event Espresso UXIP feature is active on your site. For %1$smore info%2$s and to opt-out %3$sclick here%4$s.', |
|
158 | - 'event_espresso' |
|
159 | - ), |
|
160 | - '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
161 | - '</a>', |
|
162 | - '<a href="' . $settings_url . '" target="_blank">', |
|
163 | - '</a>' |
|
164 | - ); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * Callback for admin_enqueue_scripts that sets up the scripts and styles for the uxip notice |
|
171 | - */ |
|
172 | - public function enqueueScripts() |
|
173 | - { |
|
174 | - wp_register_script( |
|
175 | - 'ee-data-optin-js', |
|
176 | - EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
177 | - array('jquery'), |
|
178 | - EVENT_ESPRESSO_VERSION, |
|
179 | - true |
|
180 | - ); |
|
181 | - wp_register_style( |
|
182 | - 'ee-data-optin-css', |
|
183 | - EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
184 | - array(), |
|
185 | - EVENT_ESPRESSO_VERSION |
|
186 | - ); |
|
187 | - |
|
188 | - wp_enqueue_script('ee-data-optin-js'); |
|
189 | - wp_enqueue_style('ee-data-optin-css'); |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * Callback for wp_ajax_espresso_data_optin that handles the ajax request |
|
195 | - */ |
|
196 | - public function ajaxHandler() |
|
197 | - { |
|
198 | - // verify nonce |
|
199 | - if (isset($_POST['nonce']) && ! wp_verify_nonce($_POST['nonce'], 'ee-data-optin')) { |
|
200 | - exit(); |
|
201 | - } |
|
202 | - |
|
203 | - // update has notified option |
|
204 | - $this->config->setHasNotifiedAboutUxip(); |
|
205 | - exit(); |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * Used to determine whether additional stats are sent. |
|
211 | - */ |
|
212 | - private function sendStats() |
|
213 | - { |
|
214 | - return $this->config->isOptedInForUxip() |
|
215 | - && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance |
|
216 | - && $this->statSendTimestampExpired(); |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * Returns true when the timestamp used to track whether stats get sent (currently a weekly interval) is expired. |
|
222 | - * Returns false otherwise. |
|
223 | - * |
|
224 | - * @return bool |
|
225 | - */ |
|
226 | - private function statSendTimestampExpired() |
|
227 | - { |
|
228 | - $current_expiry = get_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, null); |
|
229 | - if ($current_expiry === null) { |
|
230 | - add_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS, '', 'no'); |
|
231 | - return true; |
|
232 | - } |
|
233 | - |
|
234 | - if (time() > (int)$current_expiry) { |
|
235 | - update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS); |
|
236 | - return true; |
|
237 | - } |
|
238 | - return false; |
|
239 | - } |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * Retrieves the optin text (static so it can be used in multiple places as necessary). |
|
124 | + * |
|
125 | + * @param bool $extra |
|
126 | + */ |
|
127 | + public static function optinText($extra = true) |
|
128 | + { |
|
129 | + if (! $extra) { |
|
130 | + echo '<h2 class="ee-admin-settings-hdr" ' |
|
131 | + . (! $extra ? 'id="UXIP_settings"' : '') |
|
132 | + . '>' |
|
133 | + . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
|
134 | + . EEH_Template::get_help_tab_link('organization_logo_info') |
|
135 | + . '</h2>'; |
|
136 | + printf( |
|
137 | + esc_html__( |
|
138 | + '%1$sPlease help us make Event Espresso better and vote for your favorite features.%2$s The %3$sUser eXperience Improvement Program (UXIP)%4$s, has been created so when you use Event Espresso you are voting for the features and settings that are important to you. The UXIP helps us understand how you use our products and services, track problems and in what context. If you opt-out of the UXIP you essentially elect for us to disregard how you use Event Espresso as we build new features and make changes. Participation in the program is completely voluntary but it is enabled by default. The end results of the UXIP are software improvements to better meet your needs. The data we collect will never be sold, traded, or misused in any way. %5$sPlease see our %6$sPrivacy Policy%7$s for more information.', |
|
139 | + 'event_espresso' |
|
140 | + ), |
|
141 | + '<p><em>', |
|
142 | + '</em></p>', |
|
143 | + '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
144 | + '</a>', |
|
145 | + '<br><br>', |
|
146 | + '<a href="https://eventespresso.com/about/privacy-policy/" target="_blank">', |
|
147 | + '</a>' |
|
148 | + ); |
|
149 | + } else { |
|
150 | + $settings_url = EE_Admin_Page::add_query_args_and_nonce( |
|
151 | + array('action' => 'default'), |
|
152 | + admin_url('admin.php?page=espresso_general_settings') |
|
153 | + ); |
|
154 | + $settings_url .= '#UXIP_settings'; |
|
155 | + printf( |
|
156 | + esc_html__( |
|
157 | + 'The Event Espresso UXIP feature is active on your site. For %1$smore info%2$s and to opt-out %3$sclick here%4$s.', |
|
158 | + 'event_espresso' |
|
159 | + ), |
|
160 | + '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
161 | + '</a>', |
|
162 | + '<a href="' . $settings_url . '" target="_blank">', |
|
163 | + '</a>' |
|
164 | + ); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * Callback for admin_enqueue_scripts that sets up the scripts and styles for the uxip notice |
|
171 | + */ |
|
172 | + public function enqueueScripts() |
|
173 | + { |
|
174 | + wp_register_script( |
|
175 | + 'ee-data-optin-js', |
|
176 | + EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
177 | + array('jquery'), |
|
178 | + EVENT_ESPRESSO_VERSION, |
|
179 | + true |
|
180 | + ); |
|
181 | + wp_register_style( |
|
182 | + 'ee-data-optin-css', |
|
183 | + EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
184 | + array(), |
|
185 | + EVENT_ESPRESSO_VERSION |
|
186 | + ); |
|
187 | + |
|
188 | + wp_enqueue_script('ee-data-optin-js'); |
|
189 | + wp_enqueue_style('ee-data-optin-css'); |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * Callback for wp_ajax_espresso_data_optin that handles the ajax request |
|
195 | + */ |
|
196 | + public function ajaxHandler() |
|
197 | + { |
|
198 | + // verify nonce |
|
199 | + if (isset($_POST['nonce']) && ! wp_verify_nonce($_POST['nonce'], 'ee-data-optin')) { |
|
200 | + exit(); |
|
201 | + } |
|
202 | + |
|
203 | + // update has notified option |
|
204 | + $this->config->setHasNotifiedAboutUxip(); |
|
205 | + exit(); |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * Used to determine whether additional stats are sent. |
|
211 | + */ |
|
212 | + private function sendStats() |
|
213 | + { |
|
214 | + return $this->config->isOptedInForUxip() |
|
215 | + && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance |
|
216 | + && $this->statSendTimestampExpired(); |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * Returns true when the timestamp used to track whether stats get sent (currently a weekly interval) is expired. |
|
222 | + * Returns false otherwise. |
|
223 | + * |
|
224 | + * @return bool |
|
225 | + */ |
|
226 | + private function statSendTimestampExpired() |
|
227 | + { |
|
228 | + $current_expiry = get_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, null); |
|
229 | + if ($current_expiry === null) { |
|
230 | + add_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS, '', 'no'); |
|
231 | + return true; |
|
232 | + } |
|
233 | + |
|
234 | + if (time() > (int)$current_expiry) { |
|
235 | + update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS); |
|
236 | + return true; |
|
237 | + } |
|
238 | + return false; |
|
239 | + } |
|
240 | 240 | } |
@@ -76,9 +76,9 @@ discard block |
||
76 | 76 | public function statsCallback() |
77 | 77 | { |
78 | 78 | // returns a callback that can is used to retrieve the stats to send along to the pue server. |
79 | - return function () { |
|
79 | + return function() { |
|
80 | 80 | // we only send stats one a week, so let's see if our stat timestamp has expired. |
81 | - if (! $this->sendStats()) { |
|
81 | + if ( ! $this->sendStats()) { |
|
82 | 82 | return array(); |
83 | 83 | } |
84 | 84 | return $this->stats_gatherer->stats(); |
@@ -126,9 +126,9 @@ discard block |
||
126 | 126 | */ |
127 | 127 | public static function optinText($extra = true) |
128 | 128 | { |
129 | - if (! $extra) { |
|
129 | + if ( ! $extra) { |
|
130 | 130 | echo '<h2 class="ee-admin-settings-hdr" ' |
131 | - . (! $extra ? 'id="UXIP_settings"' : '') |
|
131 | + . ( ! $extra ? 'id="UXIP_settings"' : '') |
|
132 | 132 | . '>' |
133 | 133 | . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
134 | 134 | . EEH_Template::get_help_tab_link('organization_logo_info') |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | ), |
160 | 160 | '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
161 | 161 | '</a>', |
162 | - '<a href="' . $settings_url . '" target="_blank">', |
|
162 | + '<a href="'.$settings_url.'" target="_blank">', |
|
163 | 163 | '</a>' |
164 | 164 | ); |
165 | 165 | } |
@@ -173,14 +173,14 @@ discard block |
||
173 | 173 | { |
174 | 174 | wp_register_script( |
175 | 175 | 'ee-data-optin-js', |
176 | - EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
176 | + EE_GLOBAL_ASSETS_URL.'scripts/ee-data-optin.js', |
|
177 | 177 | array('jquery'), |
178 | 178 | EVENT_ESPRESSO_VERSION, |
179 | 179 | true |
180 | 180 | ); |
181 | 181 | wp_register_style( |
182 | 182 | 'ee-data-optin-css', |
183 | - EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
183 | + EE_GLOBAL_ASSETS_URL.'css/ee-data-optin.css', |
|
184 | 184 | array(), |
185 | 185 | EVENT_ESPRESSO_VERSION |
186 | 186 | ); |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | return true; |
232 | 232 | } |
233 | 233 | |
234 | - if (time() > (int)$current_expiry) { |
|
234 | + if (time() > (int) $current_expiry) { |
|
235 | 235 | update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS); |
236 | 236 | return true; |
237 | 237 | } |
@@ -15,293 +15,293 @@ |
||
15 | 15 | class StatsGatherer |
16 | 16 | { |
17 | 17 | |
18 | - const COUNT_ALL_EVENTS = 'event'; |
|
19 | - const COUNT_ACTIVE_EVENTS = 'active_event'; |
|
20 | - const COUNT_DATETIMES = 'datetime'; |
|
21 | - const COUNT_TICKETS = 'ticket'; |
|
22 | - const COUNT_DATETIMES_SOLD = 'datetime_sold'; |
|
23 | - const COUNT_TICKETS_FREE = 'free_ticket'; |
|
24 | - const COUNT_TICKETS_PAID = 'paid_ticket'; |
|
25 | - const COUNT_TICKETS_SOLD = 'ticket_sold'; |
|
26 | - const COUNT_REGISTRATIONS_APPROVED = 'registrations_approved'; |
|
27 | - const COUNT_REGISTRATIONS_NOT_APPROVED = 'registrations_not_approved'; |
|
28 | - const COUNT_REGISTRATIONS_PENDING = 'registrations_pending'; |
|
29 | - const COUNT_REGISTRATIONS_INCOMPLETE = 'registrations_incomplete'; |
|
30 | - const COUNT_REGISTRATIONS_ALL = 'registrations_all'; |
|
31 | - const COUNT_REGISTRATIONS_CANCELLED = 'registrations_cancelled'; |
|
32 | - const COUNT_REGISTRATIONS_DECLINED = 'registrations_declined'; |
|
33 | - const SUM_TRANSACTIONS_COMPLETE_TOTAL = 'transactions_complete_total_sum'; |
|
34 | - const SUM_TRANSACTIONS_ALL_PAID = 'transactions_all_paid'; |
|
35 | - const INFO_SITE_CURRENCY = 'site_currency'; |
|
18 | + const COUNT_ALL_EVENTS = 'event'; |
|
19 | + const COUNT_ACTIVE_EVENTS = 'active_event'; |
|
20 | + const COUNT_DATETIMES = 'datetime'; |
|
21 | + const COUNT_TICKETS = 'ticket'; |
|
22 | + const COUNT_DATETIMES_SOLD = 'datetime_sold'; |
|
23 | + const COUNT_TICKETS_FREE = 'free_ticket'; |
|
24 | + const COUNT_TICKETS_PAID = 'paid_ticket'; |
|
25 | + const COUNT_TICKETS_SOLD = 'ticket_sold'; |
|
26 | + const COUNT_REGISTRATIONS_APPROVED = 'registrations_approved'; |
|
27 | + const COUNT_REGISTRATIONS_NOT_APPROVED = 'registrations_not_approved'; |
|
28 | + const COUNT_REGISTRATIONS_PENDING = 'registrations_pending'; |
|
29 | + const COUNT_REGISTRATIONS_INCOMPLETE = 'registrations_incomplete'; |
|
30 | + const COUNT_REGISTRATIONS_ALL = 'registrations_all'; |
|
31 | + const COUNT_REGISTRATIONS_CANCELLED = 'registrations_cancelled'; |
|
32 | + const COUNT_REGISTRATIONS_DECLINED = 'registrations_declined'; |
|
33 | + const SUM_TRANSACTIONS_COMPLETE_TOTAL = 'transactions_complete_total_sum'; |
|
34 | + const SUM_TRANSACTIONS_ALL_PAID = 'transactions_all_paid'; |
|
35 | + const INFO_SITE_CURRENCY = 'site_currency'; |
|
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * @var EEM_Payment_Method |
|
40 | - */ |
|
41 | - private $payment_method_model; |
|
38 | + /** |
|
39 | + * @var EEM_Payment_Method |
|
40 | + */ |
|
41 | + private $payment_method_model; |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * @var EEM_Event |
|
46 | - */ |
|
47 | - private $event_model; |
|
44 | + /** |
|
45 | + * @var EEM_Event |
|
46 | + */ |
|
47 | + private $event_model; |
|
48 | 48 | |
49 | - /** |
|
50 | - * @var EEM_Datetime |
|
51 | - */ |
|
52 | - private $datetime_model; |
|
49 | + /** |
|
50 | + * @var EEM_Datetime |
|
51 | + */ |
|
52 | + private $datetime_model; |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * @var EEM_Ticket |
|
57 | - */ |
|
58 | - private $ticket_model; |
|
55 | + /** |
|
56 | + * @var EEM_Ticket |
|
57 | + */ |
|
58 | + private $ticket_model; |
|
59 | 59 | |
60 | 60 | |
61 | - /** |
|
62 | - * @var EEM_Registration |
|
63 | - */ |
|
64 | - private $registration_model; |
|
61 | + /** |
|
62 | + * @var EEM_Registration |
|
63 | + */ |
|
64 | + private $registration_model; |
|
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * @var EEM_Transaction |
|
69 | - */ |
|
70 | - private $transaction_model; |
|
67 | + /** |
|
68 | + * @var EEM_Transaction |
|
69 | + */ |
|
70 | + private $transaction_model; |
|
71 | 71 | |
72 | 72 | |
73 | - /** |
|
74 | - * @var EE_Config |
|
75 | - */ |
|
76 | - private $config; |
|
73 | + /** |
|
74 | + * @var EE_Config |
|
75 | + */ |
|
76 | + private $config; |
|
77 | 77 | |
78 | 78 | |
79 | - /** |
|
80 | - * StatsGatherer constructor. |
|
81 | - * |
|
82 | - * @param EEM_Payment_Method $payment_method_model |
|
83 | - * @param EEM_Event $event_model |
|
84 | - * @param EEM_Datetime $datetime_model |
|
85 | - * @param EEM_Ticket $ticket_model |
|
86 | - * @param EEM_Registration $registration_model |
|
87 | - * @param EEM_Transaction $transaction_model |
|
88 | - * @param EE_Config $config |
|
89 | - */ |
|
90 | - public function __construct( |
|
91 | - EEM_Payment_Method $payment_method_model, |
|
92 | - EEM_Event $event_model, |
|
93 | - EEM_Datetime $datetime_model, |
|
94 | - EEM_Ticket $ticket_model, |
|
95 | - EEM_Registration $registration_model, |
|
96 | - EEM_Transaction $transaction_model, |
|
97 | - EE_Config $config |
|
98 | - ) { |
|
99 | - $this->payment_method_model = $payment_method_model; |
|
100 | - $this->event_model = $event_model; |
|
101 | - $this->datetime_model = $datetime_model; |
|
102 | - $this->ticket_model = $ticket_model; |
|
103 | - $this->registration_model = $registration_model; |
|
104 | - $this->transaction_model = $transaction_model; |
|
105 | - $this->config = $config; |
|
106 | - } |
|
79 | + /** |
|
80 | + * StatsGatherer constructor. |
|
81 | + * |
|
82 | + * @param EEM_Payment_Method $payment_method_model |
|
83 | + * @param EEM_Event $event_model |
|
84 | + * @param EEM_Datetime $datetime_model |
|
85 | + * @param EEM_Ticket $ticket_model |
|
86 | + * @param EEM_Registration $registration_model |
|
87 | + * @param EEM_Transaction $transaction_model |
|
88 | + * @param EE_Config $config |
|
89 | + */ |
|
90 | + public function __construct( |
|
91 | + EEM_Payment_Method $payment_method_model, |
|
92 | + EEM_Event $event_model, |
|
93 | + EEM_Datetime $datetime_model, |
|
94 | + EEM_Ticket $ticket_model, |
|
95 | + EEM_Registration $registration_model, |
|
96 | + EEM_Transaction $transaction_model, |
|
97 | + EE_Config $config |
|
98 | + ) { |
|
99 | + $this->payment_method_model = $payment_method_model; |
|
100 | + $this->event_model = $event_model; |
|
101 | + $this->datetime_model = $datetime_model; |
|
102 | + $this->ticket_model = $ticket_model; |
|
103 | + $this->registration_model = $registration_model; |
|
104 | + $this->transaction_model = $transaction_model; |
|
105 | + $this->config = $config; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | |
109 | - /** |
|
110 | - * Return the stats array for PUE UXIP stats. |
|
111 | - * |
|
112 | - * @return array |
|
113 | - */ |
|
114 | - public function stats() |
|
115 | - { |
|
116 | - $stats = $this->paymentMethodStats(); |
|
117 | - // a-ok so let's setup our stats. |
|
118 | - $stats = array_merge($stats, array( |
|
119 | - 'is_multisite' => is_multisite() && is_main_site(), |
|
120 | - 'active_theme' => $this->getActiveThemeStat(), |
|
121 | - 'ee4_all_events_count' => $this->getCountFor(self::COUNT_ALL_EVENTS), |
|
122 | - 'ee4_active_events_count' => $this->getCountFor(self::COUNT_ACTIVE_EVENTS), |
|
123 | - 'all_dtts_count' => $this->getCountFor(self::COUNT_DATETIMES), |
|
124 | - 'dtt_sold' => $this->getCountFor(self::COUNT_DATETIMES_SOLD), |
|
125 | - 'all_tkt_count' => $this->getCountFor(self::COUNT_TICKETS), |
|
126 | - 'free_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_FREE), |
|
127 | - 'paid_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_PAID), |
|
128 | - 'tkt_sold' => $this->getCountFor(self::COUNT_TICKETS_SOLD), |
|
129 | - 'approve_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_APPROVED), |
|
130 | - 'pending_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_PENDING), |
|
131 | - 'not_approved_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_NOT_APPROVED), |
|
132 | - 'incomplete_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_INCOMPLETE), |
|
133 | - 'cancelled_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_CANCELLED), |
|
134 | - 'declined_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_DECLINED), |
|
135 | - 'all_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_ALL), |
|
136 | - 'completed_transaction_total_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_COMPLETE_TOTAL), |
|
137 | - 'all_transaction_paid_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_ALL_PAID), |
|
138 | - self::INFO_SITE_CURRENCY => $this->config->currency instanceof EE_Currency_Config |
|
139 | - ? $this->config->currency->code |
|
140 | - : 'unknown', |
|
141 | - 'phpversion' => implode( |
|
142 | - '.', |
|
143 | - array(PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION) |
|
144 | - ), |
|
145 | - )); |
|
146 | - // remove any values that equal null. This ensures any stats that weren't retrieved successfully are excluded. |
|
147 | - return array_filter($stats, function ($value) { |
|
148 | - return $value !== null; |
|
149 | - }); |
|
150 | - } |
|
109 | + /** |
|
110 | + * Return the stats array for PUE UXIP stats. |
|
111 | + * |
|
112 | + * @return array |
|
113 | + */ |
|
114 | + public function stats() |
|
115 | + { |
|
116 | + $stats = $this->paymentMethodStats(); |
|
117 | + // a-ok so let's setup our stats. |
|
118 | + $stats = array_merge($stats, array( |
|
119 | + 'is_multisite' => is_multisite() && is_main_site(), |
|
120 | + 'active_theme' => $this->getActiveThemeStat(), |
|
121 | + 'ee4_all_events_count' => $this->getCountFor(self::COUNT_ALL_EVENTS), |
|
122 | + 'ee4_active_events_count' => $this->getCountFor(self::COUNT_ACTIVE_EVENTS), |
|
123 | + 'all_dtts_count' => $this->getCountFor(self::COUNT_DATETIMES), |
|
124 | + 'dtt_sold' => $this->getCountFor(self::COUNT_DATETIMES_SOLD), |
|
125 | + 'all_tkt_count' => $this->getCountFor(self::COUNT_TICKETS), |
|
126 | + 'free_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_FREE), |
|
127 | + 'paid_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_PAID), |
|
128 | + 'tkt_sold' => $this->getCountFor(self::COUNT_TICKETS_SOLD), |
|
129 | + 'approve_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_APPROVED), |
|
130 | + 'pending_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_PENDING), |
|
131 | + 'not_approved_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_NOT_APPROVED), |
|
132 | + 'incomplete_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_INCOMPLETE), |
|
133 | + 'cancelled_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_CANCELLED), |
|
134 | + 'declined_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_DECLINED), |
|
135 | + 'all_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_ALL), |
|
136 | + 'completed_transaction_total_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_COMPLETE_TOTAL), |
|
137 | + 'all_transaction_paid_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_ALL_PAID), |
|
138 | + self::INFO_SITE_CURRENCY => $this->config->currency instanceof EE_Currency_Config |
|
139 | + ? $this->config->currency->code |
|
140 | + : 'unknown', |
|
141 | + 'phpversion' => implode( |
|
142 | + '.', |
|
143 | + array(PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION) |
|
144 | + ), |
|
145 | + )); |
|
146 | + // remove any values that equal null. This ensures any stats that weren't retrieved successfully are excluded. |
|
147 | + return array_filter($stats, function ($value) { |
|
148 | + return $value !== null; |
|
149 | + }); |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @param string $which enum (@see constants prefixed with COUNT) |
|
154 | - * @return int|null |
|
155 | - */ |
|
156 | - private function getCountFor($which) |
|
157 | - { |
|
158 | - try { |
|
159 | - switch ($which) { |
|
160 | - case self::COUNT_ALL_EVENTS: |
|
161 | - $count = $this->event_model->count(); |
|
162 | - break; |
|
163 | - case self::COUNT_TICKETS: |
|
164 | - $count = $this->ticket_model->count(); |
|
165 | - break; |
|
166 | - case self::COUNT_DATETIMES: |
|
167 | - $count = $this->datetime_model->count(); |
|
168 | - break; |
|
169 | - case self::COUNT_ACTIVE_EVENTS: |
|
170 | - $count = $this->event_model->get_active_events(array(), true); |
|
171 | - break; |
|
172 | - case self::COUNT_DATETIMES_SOLD: |
|
173 | - $count = $this->datetime_model->sum(array(), 'DTT_sold'); |
|
174 | - break; |
|
175 | - case self::COUNT_TICKETS_FREE: |
|
176 | - $count = $this->ticket_model->count(array( |
|
177 | - array( |
|
178 | - 'TKT_price' => 0, |
|
179 | - ), |
|
180 | - )); |
|
181 | - break; |
|
182 | - case self::COUNT_TICKETS_PAID: |
|
183 | - $count = $this->ticket_model->count(array( |
|
184 | - array( |
|
185 | - 'TKT_price' => array('>', 0), |
|
186 | - ), |
|
187 | - )); |
|
188 | - break; |
|
189 | - case self::COUNT_TICKETS_SOLD: |
|
190 | - $count = $this->ticket_model->sum(array(), 'TKT_sold'); |
|
191 | - break; |
|
192 | - case self::COUNT_REGISTRATIONS_ALL: |
|
193 | - $count = $this->registration_model->count(); |
|
194 | - break; |
|
195 | - case self::COUNT_REGISTRATIONS_CANCELLED: |
|
196 | - $count = $this->registration_model->count( |
|
197 | - array( |
|
198 | - array( |
|
199 | - 'STS_ID' => EEM_Registration::status_id_cancelled, |
|
200 | - ), |
|
201 | - ) |
|
202 | - ); |
|
203 | - break; |
|
204 | - case self::COUNT_REGISTRATIONS_INCOMPLETE: |
|
205 | - $count = $this->registration_model->count( |
|
206 | - array( |
|
207 | - array( |
|
208 | - 'STS_ID' => EEM_Registration::status_id_incomplete, |
|
209 | - ), |
|
210 | - ) |
|
211 | - ); |
|
212 | - break; |
|
213 | - case self::COUNT_REGISTRATIONS_NOT_APPROVED: |
|
214 | - $count = $this->registration_model->count( |
|
215 | - array( |
|
216 | - array( |
|
217 | - 'STS_ID' => EEM_Registration::status_id_not_approved, |
|
218 | - ), |
|
219 | - ) |
|
220 | - ); |
|
221 | - break; |
|
222 | - case self::COUNT_REGISTRATIONS_DECLINED: |
|
223 | - $count = $this->registration_model->count( |
|
224 | - array( |
|
225 | - array( |
|
226 | - 'STS_ID' => EEM_Registration::status_id_declined, |
|
227 | - ), |
|
228 | - ) |
|
229 | - ); |
|
230 | - break; |
|
231 | - case self::COUNT_REGISTRATIONS_PENDING: |
|
232 | - $count = $this->registration_model->count( |
|
233 | - array( |
|
234 | - array( |
|
235 | - 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
236 | - ), |
|
237 | - ) |
|
238 | - ); |
|
239 | - break; |
|
240 | - case self::COUNT_REGISTRATIONS_APPROVED: |
|
241 | - $count = $this->registration_model->count( |
|
242 | - array( |
|
243 | - array( |
|
244 | - 'STS_ID' => EEM_Registration::status_id_approved, |
|
245 | - ), |
|
246 | - ) |
|
247 | - ); |
|
248 | - break; |
|
249 | - case self::SUM_TRANSACTIONS_COMPLETE_TOTAL: |
|
250 | - $count = $this->transaction_model->sum( |
|
251 | - array( |
|
252 | - array( |
|
253 | - 'STS_ID' => EEM_Transaction::complete_status_code, |
|
254 | - ), |
|
255 | - ), |
|
256 | - 'TXN_total' |
|
257 | - ); |
|
258 | - break; |
|
259 | - case self::SUM_TRANSACTIONS_ALL_PAID: |
|
260 | - $count = $this->transaction_model->sum( |
|
261 | - array(), |
|
262 | - 'TXN_paid' |
|
263 | - ); |
|
264 | - break; |
|
265 | - default: |
|
266 | - $count = null; |
|
267 | - break; |
|
268 | - } |
|
269 | - } catch (Exception $e) { |
|
270 | - $count = null; |
|
271 | - } |
|
272 | - return $count; |
|
273 | - } |
|
152 | + /** |
|
153 | + * @param string $which enum (@see constants prefixed with COUNT) |
|
154 | + * @return int|null |
|
155 | + */ |
|
156 | + private function getCountFor($which) |
|
157 | + { |
|
158 | + try { |
|
159 | + switch ($which) { |
|
160 | + case self::COUNT_ALL_EVENTS: |
|
161 | + $count = $this->event_model->count(); |
|
162 | + break; |
|
163 | + case self::COUNT_TICKETS: |
|
164 | + $count = $this->ticket_model->count(); |
|
165 | + break; |
|
166 | + case self::COUNT_DATETIMES: |
|
167 | + $count = $this->datetime_model->count(); |
|
168 | + break; |
|
169 | + case self::COUNT_ACTIVE_EVENTS: |
|
170 | + $count = $this->event_model->get_active_events(array(), true); |
|
171 | + break; |
|
172 | + case self::COUNT_DATETIMES_SOLD: |
|
173 | + $count = $this->datetime_model->sum(array(), 'DTT_sold'); |
|
174 | + break; |
|
175 | + case self::COUNT_TICKETS_FREE: |
|
176 | + $count = $this->ticket_model->count(array( |
|
177 | + array( |
|
178 | + 'TKT_price' => 0, |
|
179 | + ), |
|
180 | + )); |
|
181 | + break; |
|
182 | + case self::COUNT_TICKETS_PAID: |
|
183 | + $count = $this->ticket_model->count(array( |
|
184 | + array( |
|
185 | + 'TKT_price' => array('>', 0), |
|
186 | + ), |
|
187 | + )); |
|
188 | + break; |
|
189 | + case self::COUNT_TICKETS_SOLD: |
|
190 | + $count = $this->ticket_model->sum(array(), 'TKT_sold'); |
|
191 | + break; |
|
192 | + case self::COUNT_REGISTRATIONS_ALL: |
|
193 | + $count = $this->registration_model->count(); |
|
194 | + break; |
|
195 | + case self::COUNT_REGISTRATIONS_CANCELLED: |
|
196 | + $count = $this->registration_model->count( |
|
197 | + array( |
|
198 | + array( |
|
199 | + 'STS_ID' => EEM_Registration::status_id_cancelled, |
|
200 | + ), |
|
201 | + ) |
|
202 | + ); |
|
203 | + break; |
|
204 | + case self::COUNT_REGISTRATIONS_INCOMPLETE: |
|
205 | + $count = $this->registration_model->count( |
|
206 | + array( |
|
207 | + array( |
|
208 | + 'STS_ID' => EEM_Registration::status_id_incomplete, |
|
209 | + ), |
|
210 | + ) |
|
211 | + ); |
|
212 | + break; |
|
213 | + case self::COUNT_REGISTRATIONS_NOT_APPROVED: |
|
214 | + $count = $this->registration_model->count( |
|
215 | + array( |
|
216 | + array( |
|
217 | + 'STS_ID' => EEM_Registration::status_id_not_approved, |
|
218 | + ), |
|
219 | + ) |
|
220 | + ); |
|
221 | + break; |
|
222 | + case self::COUNT_REGISTRATIONS_DECLINED: |
|
223 | + $count = $this->registration_model->count( |
|
224 | + array( |
|
225 | + array( |
|
226 | + 'STS_ID' => EEM_Registration::status_id_declined, |
|
227 | + ), |
|
228 | + ) |
|
229 | + ); |
|
230 | + break; |
|
231 | + case self::COUNT_REGISTRATIONS_PENDING: |
|
232 | + $count = $this->registration_model->count( |
|
233 | + array( |
|
234 | + array( |
|
235 | + 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
236 | + ), |
|
237 | + ) |
|
238 | + ); |
|
239 | + break; |
|
240 | + case self::COUNT_REGISTRATIONS_APPROVED: |
|
241 | + $count = $this->registration_model->count( |
|
242 | + array( |
|
243 | + array( |
|
244 | + 'STS_ID' => EEM_Registration::status_id_approved, |
|
245 | + ), |
|
246 | + ) |
|
247 | + ); |
|
248 | + break; |
|
249 | + case self::SUM_TRANSACTIONS_COMPLETE_TOTAL: |
|
250 | + $count = $this->transaction_model->sum( |
|
251 | + array( |
|
252 | + array( |
|
253 | + 'STS_ID' => EEM_Transaction::complete_status_code, |
|
254 | + ), |
|
255 | + ), |
|
256 | + 'TXN_total' |
|
257 | + ); |
|
258 | + break; |
|
259 | + case self::SUM_TRANSACTIONS_ALL_PAID: |
|
260 | + $count = $this->transaction_model->sum( |
|
261 | + array(), |
|
262 | + 'TXN_paid' |
|
263 | + ); |
|
264 | + break; |
|
265 | + default: |
|
266 | + $count = null; |
|
267 | + break; |
|
268 | + } |
|
269 | + } catch (Exception $e) { |
|
270 | + $count = null; |
|
271 | + } |
|
272 | + return $count; |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * Return the active theme. |
|
277 | - * |
|
278 | - * @return false|string |
|
279 | - */ |
|
280 | - private function getActiveThemeStat() |
|
281 | - { |
|
282 | - $theme = wp_get_theme(); |
|
283 | - return $theme->get('Name'); |
|
284 | - } |
|
275 | + /** |
|
276 | + * Return the active theme. |
|
277 | + * |
|
278 | + * @return false|string |
|
279 | + */ |
|
280 | + private function getActiveThemeStat() |
|
281 | + { |
|
282 | + $theme = wp_get_theme(); |
|
283 | + return $theme->get('Name'); |
|
284 | + } |
|
285 | 285 | |
286 | - /** |
|
287 | - * @return array |
|
288 | - */ |
|
289 | - private function paymentMethodStats() |
|
290 | - { |
|
291 | - $payment_method_stats = array(); |
|
292 | - try { |
|
293 | - $active_payment_methods = $this->payment_method_model->get_all_active( |
|
294 | - null, |
|
295 | - array('group_by' => 'PMD_type') |
|
296 | - ); |
|
297 | - if ($active_payment_methods) { |
|
298 | - foreach ($active_payment_methods as $payment_method) { |
|
299 | - $payment_method_stats[$payment_method->name() . '_active_payment_method'] = 1; |
|
300 | - } |
|
301 | - } |
|
302 | - } catch (Exception $e) { |
|
303 | - // do nothing just prevents fatals. |
|
304 | - } |
|
305 | - return $payment_method_stats; |
|
306 | - } |
|
286 | + /** |
|
287 | + * @return array |
|
288 | + */ |
|
289 | + private function paymentMethodStats() |
|
290 | + { |
|
291 | + $payment_method_stats = array(); |
|
292 | + try { |
|
293 | + $active_payment_methods = $this->payment_method_model->get_all_active( |
|
294 | + null, |
|
295 | + array('group_by' => 'PMD_type') |
|
296 | + ); |
|
297 | + if ($active_payment_methods) { |
|
298 | + foreach ($active_payment_methods as $payment_method) { |
|
299 | + $payment_method_stats[$payment_method->name() . '_active_payment_method'] = 1; |
|
300 | + } |
|
301 | + } |
|
302 | + } catch (Exception $e) { |
|
303 | + // do nothing just prevents fatals. |
|
304 | + } |
|
305 | + return $payment_method_stats; |
|
306 | + } |
|
307 | 307 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | ), |
145 | 145 | )); |
146 | 146 | // remove any values that equal null. This ensures any stats that weren't retrieved successfully are excluded. |
147 | - return array_filter($stats, function ($value) { |
|
147 | + return array_filter($stats, function($value) { |
|
148 | 148 | return $value !== null; |
149 | 149 | }); |
150 | 150 | } |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | ); |
297 | 297 | if ($active_payment_methods) { |
298 | 298 | foreach ($active_payment_methods as $payment_method) { |
299 | - $payment_method_stats[$payment_method->name() . '_active_payment_method'] = 1; |
|
299 | + $payment_method_stats[$payment_method->name().'_active_payment_method'] = 1; |
|
300 | 300 | } |
301 | 301 | } |
302 | 302 | } catch (Exception $e) { |
@@ -5,8 +5,8 @@ |
||
5 | 5 | interface SessionIdentifierInterface |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * @return string |
|
10 | - */ |
|
11 | - public function id(); |
|
8 | + /** |
|
9 | + * @return string |
|
10 | + */ |
|
11 | + public function id(); |
|
12 | 12 | } |
@@ -16,45 +16,45 @@ |
||
16 | 16 | class CancelRegistrationService |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @var CancelTicketLineItemService $cancel_ticket_line_item_service |
|
21 | - */ |
|
22 | - private $cancel_ticket_line_item_service; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * Command constructor |
|
27 | - * |
|
28 | - * @param CancelTicketLineItemService $cancel_ticket_line_item_service |
|
29 | - */ |
|
30 | - public function __construct(CancelTicketLineItemService $cancel_ticket_line_item_service) |
|
31 | - { |
|
32 | - $this->cancel_ticket_line_item_service = $cancel_ticket_line_item_service; |
|
33 | - } |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * @param \EE_Registration $registration |
|
38 | - * @param bool $cancel_ticket_line_item |
|
39 | - */ |
|
40 | - public function cancelRegistrationAndTicketLineItem(\EE_Registration $registration, $cancel_ticket_line_item = true) |
|
41 | - { |
|
42 | - // first cancel the original line item for the registration's ticket |
|
43 | - if ($cancel_ticket_line_item) { |
|
44 | - $this->cancel_ticket_line_item_service->forRegistration($registration); |
|
45 | - } |
|
46 | - $this->cancelRegistrationOnly($registration); |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * @param \EE_Registration $registration |
|
52 | - * @throws \EE_Error |
|
53 | - */ |
|
54 | - public function cancelRegistrationOnly(\EE_Registration $registration) |
|
55 | - { |
|
56 | - // now cancel the registration itself |
|
57 | - $registration->set_status(\EEM_Registration::status_id_cancelled); |
|
58 | - $registration->save(); |
|
59 | - } |
|
19 | + /** |
|
20 | + * @var CancelTicketLineItemService $cancel_ticket_line_item_service |
|
21 | + */ |
|
22 | + private $cancel_ticket_line_item_service; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * Command constructor |
|
27 | + * |
|
28 | + * @param CancelTicketLineItemService $cancel_ticket_line_item_service |
|
29 | + */ |
|
30 | + public function __construct(CancelTicketLineItemService $cancel_ticket_line_item_service) |
|
31 | + { |
|
32 | + $this->cancel_ticket_line_item_service = $cancel_ticket_line_item_service; |
|
33 | + } |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * @param \EE_Registration $registration |
|
38 | + * @param bool $cancel_ticket_line_item |
|
39 | + */ |
|
40 | + public function cancelRegistrationAndTicketLineItem(\EE_Registration $registration, $cancel_ticket_line_item = true) |
|
41 | + { |
|
42 | + // first cancel the original line item for the registration's ticket |
|
43 | + if ($cancel_ticket_line_item) { |
|
44 | + $this->cancel_ticket_line_item_service->forRegistration($registration); |
|
45 | + } |
|
46 | + $this->cancelRegistrationOnly($registration); |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * @param \EE_Registration $registration |
|
52 | + * @throws \EE_Error |
|
53 | + */ |
|
54 | + public function cancelRegistrationOnly(\EE_Registration $registration) |
|
55 | + { |
|
56 | + // now cancel the registration itself |
|
57 | + $registration->set_status(\EEM_Registration::status_id_cancelled); |
|
58 | + $registration->save(); |
|
59 | + } |
|
60 | 60 | } |
@@ -15,23 +15,23 @@ |
||
15 | 15 | class UpdateRegistrationService extends DomainService |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @param \EE_Registration $registration |
|
20 | - * @return bool |
|
21 | - */ |
|
22 | - public function updateRegistrationAndTransaction(\EE_Registration $registration) |
|
23 | - { |
|
24 | - $transaction = $registration->transaction(); |
|
25 | - // reset transaction status back to incomplete |
|
26 | - $transaction->set_status(\EEM_Transaction::incomplete_status_code); |
|
27 | - // update transaction and all line item totals and subtotals |
|
28 | - $transaction->total_line_item()->recalculate_total_including_taxes(); |
|
29 | - // maybe update status, but don't save transaction just yet |
|
30 | - $transaction->update_status_based_on_total_paid(); |
|
31 | - /** @type \EE_Registration_Processor $registration_processor */ |
|
32 | - $registration_processor = \EE_Registry::instance()->load_class('Registration_Processor'); |
|
33 | - $registration_processor->update_registration_final_prices($transaction); |
|
34 | - $registration_processor->update_registration_status_and_trigger_notifications($registration); |
|
35 | - return true; |
|
36 | - } |
|
18 | + /** |
|
19 | + * @param \EE_Registration $registration |
|
20 | + * @return bool |
|
21 | + */ |
|
22 | + public function updateRegistrationAndTransaction(\EE_Registration $registration) |
|
23 | + { |
|
24 | + $transaction = $registration->transaction(); |
|
25 | + // reset transaction status back to incomplete |
|
26 | + $transaction->set_status(\EEM_Transaction::incomplete_status_code); |
|
27 | + // update transaction and all line item totals and subtotals |
|
28 | + $transaction->total_line_item()->recalculate_total_including_taxes(); |
|
29 | + // maybe update status, but don't save transaction just yet |
|
30 | + $transaction->update_status_based_on_total_paid(); |
|
31 | + /** @type \EE_Registration_Processor $registration_processor */ |
|
32 | + $registration_processor = \EE_Registry::instance()->load_class('Registration_Processor'); |
|
33 | + $registration_processor->update_registration_final_prices($transaction); |
|
34 | + $registration_processor->update_registration_status_and_trigger_notifications($registration); |
|
35 | + return true; |
|
36 | + } |
|
37 | 37 | } |
@@ -26,149 +26,149 @@ |
||
26 | 26 | { |
27 | 27 | |
28 | 28 | |
29 | - /** |
|
30 | - * @param EE_Registration $target_registration |
|
31 | - * @param EE_Registration $registration_to_copy |
|
32 | - * @return bool |
|
33 | - * @throws UnexpectedEntityException |
|
34 | - * @throws EntityNotFoundException |
|
35 | - * @throws RuntimeException |
|
36 | - * @throws EE_Error |
|
37 | - */ |
|
38 | - public function copyRegistrationDetails( |
|
39 | - EE_Registration $target_registration, |
|
40 | - EE_Registration $registration_to_copy |
|
41 | - ) { |
|
42 | - // copy attendee |
|
43 | - $target_registration->set_attendee_id($registration_to_copy->attendee_ID()); |
|
44 | - $target_registration->updateStatusBasedOnTotalPaid(false); |
|
45 | - $target_registration->save(); |
|
46 | - // get answers to previous reg questions |
|
47 | - $answers = $this->reindexAnswersByQuestionId($registration_to_copy->answers()); |
|
48 | - // get questions to new event reg form |
|
49 | - $new_event = $target_registration->event(); |
|
50 | - $question_groups = $new_event->question_groups( |
|
51 | - array( |
|
52 | - array( |
|
53 | - 'Event.EVT_ID' => $new_event->ID(), |
|
54 | - 'Event_Question_Group.EQG_primary' => $registration_to_copy->is_primary_registrant(), |
|
55 | - ), |
|
56 | - 'order_by' => array('QSG_order' => 'ASC'), |
|
57 | - ) |
|
58 | - ); |
|
59 | - foreach ($question_groups as $question_group) { |
|
60 | - if ($question_group instanceof \EE_Question_Group) { |
|
61 | - foreach ($question_group->questions() as $question) { |
|
62 | - if ($question instanceof EE_Question) { |
|
63 | - $this->generateNewAnswer( |
|
64 | - $question, |
|
65 | - $target_registration, |
|
66 | - $answers |
|
67 | - ); |
|
68 | - } |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
72 | - return true; |
|
73 | - } |
|
29 | + /** |
|
30 | + * @param EE_Registration $target_registration |
|
31 | + * @param EE_Registration $registration_to_copy |
|
32 | + * @return bool |
|
33 | + * @throws UnexpectedEntityException |
|
34 | + * @throws EntityNotFoundException |
|
35 | + * @throws RuntimeException |
|
36 | + * @throws EE_Error |
|
37 | + */ |
|
38 | + public function copyRegistrationDetails( |
|
39 | + EE_Registration $target_registration, |
|
40 | + EE_Registration $registration_to_copy |
|
41 | + ) { |
|
42 | + // copy attendee |
|
43 | + $target_registration->set_attendee_id($registration_to_copy->attendee_ID()); |
|
44 | + $target_registration->updateStatusBasedOnTotalPaid(false); |
|
45 | + $target_registration->save(); |
|
46 | + // get answers to previous reg questions |
|
47 | + $answers = $this->reindexAnswersByQuestionId($registration_to_copy->answers()); |
|
48 | + // get questions to new event reg form |
|
49 | + $new_event = $target_registration->event(); |
|
50 | + $question_groups = $new_event->question_groups( |
|
51 | + array( |
|
52 | + array( |
|
53 | + 'Event.EVT_ID' => $new_event->ID(), |
|
54 | + 'Event_Question_Group.EQG_primary' => $registration_to_copy->is_primary_registrant(), |
|
55 | + ), |
|
56 | + 'order_by' => array('QSG_order' => 'ASC'), |
|
57 | + ) |
|
58 | + ); |
|
59 | + foreach ($question_groups as $question_group) { |
|
60 | + if ($question_group instanceof \EE_Question_Group) { |
|
61 | + foreach ($question_group->questions() as $question) { |
|
62 | + if ($question instanceof EE_Question) { |
|
63 | + $this->generateNewAnswer( |
|
64 | + $question, |
|
65 | + $target_registration, |
|
66 | + $answers |
|
67 | + ); |
|
68 | + } |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | + return true; |
|
73 | + } |
|
74 | 74 | |
75 | 75 | |
76 | - /** |
|
77 | - * @param EE_Answer[] $answers |
|
78 | - * @return array |
|
79 | - * @throws EE_Error |
|
80 | - */ |
|
81 | - protected function reindexAnswersByQuestionId(array $answers) |
|
82 | - { |
|
83 | - $reindexed_answers = array(); |
|
84 | - foreach ($answers as $answer) { |
|
85 | - if ($answer instanceof EE_Answer) { |
|
86 | - $reindexed_answers[$answer->question_ID()] = $answer->value(); |
|
87 | - } |
|
88 | - } |
|
89 | - return $reindexed_answers; |
|
90 | - } |
|
76 | + /** |
|
77 | + * @param EE_Answer[] $answers |
|
78 | + * @return array |
|
79 | + * @throws EE_Error |
|
80 | + */ |
|
81 | + protected function reindexAnswersByQuestionId(array $answers) |
|
82 | + { |
|
83 | + $reindexed_answers = array(); |
|
84 | + foreach ($answers as $answer) { |
|
85 | + if ($answer instanceof EE_Answer) { |
|
86 | + $reindexed_answers[$answer->question_ID()] = $answer->value(); |
|
87 | + } |
|
88 | + } |
|
89 | + return $reindexed_answers; |
|
90 | + } |
|
91 | 91 | |
92 | 92 | |
93 | - /** |
|
94 | - * @param EE_Question $question |
|
95 | - * @param EE_Registration $registration |
|
96 | - * @param $previous_answers |
|
97 | - * @return EE_Answer |
|
98 | - * @throws UnexpectedEntityException |
|
99 | - * @throws EE_Error |
|
100 | - */ |
|
101 | - protected function generateNewAnswer( |
|
102 | - EE_Question $question, |
|
103 | - EE_Registration $registration, |
|
104 | - $previous_answers |
|
105 | - ) { |
|
106 | - $old_answer_value = isset($previous_answers[$question->ID()]) |
|
107 | - ? $previous_answers[$question->ID()] |
|
108 | - : ''; |
|
109 | - $new_answer = EE_Answer::new_instance( |
|
110 | - array( |
|
111 | - 'QST_ID' => $question->ID(), |
|
112 | - 'REG_ID' => $registration->ID(), |
|
113 | - 'ANS_value' => $old_answer_value, |
|
114 | - ) |
|
115 | - ); |
|
116 | - if (! $new_answer instanceof EE_Answer) { |
|
117 | - throw new UnexpectedEntityException($new_answer, 'EE_Answer'); |
|
118 | - } |
|
119 | - $new_answer->save(); |
|
120 | - return $new_answer; |
|
121 | - } |
|
93 | + /** |
|
94 | + * @param EE_Question $question |
|
95 | + * @param EE_Registration $registration |
|
96 | + * @param $previous_answers |
|
97 | + * @return EE_Answer |
|
98 | + * @throws UnexpectedEntityException |
|
99 | + * @throws EE_Error |
|
100 | + */ |
|
101 | + protected function generateNewAnswer( |
|
102 | + EE_Question $question, |
|
103 | + EE_Registration $registration, |
|
104 | + $previous_answers |
|
105 | + ) { |
|
106 | + $old_answer_value = isset($previous_answers[$question->ID()]) |
|
107 | + ? $previous_answers[$question->ID()] |
|
108 | + : ''; |
|
109 | + $new_answer = EE_Answer::new_instance( |
|
110 | + array( |
|
111 | + 'QST_ID' => $question->ID(), |
|
112 | + 'REG_ID' => $registration->ID(), |
|
113 | + 'ANS_value' => $old_answer_value, |
|
114 | + ) |
|
115 | + ); |
|
116 | + if (! $new_answer instanceof EE_Answer) { |
|
117 | + throw new UnexpectedEntityException($new_answer, 'EE_Answer'); |
|
118 | + } |
|
119 | + $new_answer->save(); |
|
120 | + return $new_answer; |
|
121 | + } |
|
122 | 122 | |
123 | 123 | |
124 | - /** |
|
125 | - * @param EE_Registration $target_registration |
|
126 | - * @param EE_Registration $registration_to_copy |
|
127 | - * @return bool |
|
128 | - * @throws RuntimeException |
|
129 | - * @throws UnexpectedEntityException |
|
130 | - * @throws EE_Error |
|
131 | - */ |
|
132 | - public function copyPaymentDetails( |
|
133 | - EE_Registration $target_registration, |
|
134 | - EE_Registration $registration_to_copy |
|
135 | - ) { |
|
136 | - $save = false; |
|
137 | - $previous_registration_payments = $registration_to_copy->registration_payments(); |
|
138 | - $new_registration_payment_total = 0; |
|
139 | - $registration_to_copy_total = $registration_to_copy->paid(); |
|
140 | - foreach ($previous_registration_payments as $previous_registration_payment) { |
|
141 | - if ($previous_registration_payment instanceof EE_Registration_Payment |
|
142 | - && $previous_registration_payment->payment() instanceof EE_Payment |
|
143 | - && $previous_registration_payment->payment()->is_approved() |
|
144 | - ) { |
|
145 | - $payment_amount = $previous_registration_payment->amount(); |
|
146 | - $new_registration_payment = EE_Registration_Payment::new_instance( |
|
147 | - array( |
|
148 | - 'REG_ID' => $target_registration->ID(), |
|
149 | - 'PAY_ID' => $previous_registration_payment->payment()->ID(), |
|
150 | - 'RPY_amount' => $payment_amount, |
|
151 | - ) |
|
152 | - ); |
|
153 | - if (! $new_registration_payment instanceof EE_Registration_Payment) { |
|
154 | - throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment'); |
|
155 | - } |
|
156 | - $new_registration_payment->save(); |
|
157 | - // if new reg payment is good, then set old reg payment amount to zero |
|
158 | - $previous_registration_payment->set_amount(0); |
|
159 | - $previous_registration_payment->save(); |
|
160 | - // now increment/decrement payment amounts |
|
161 | - $new_registration_payment_total += $payment_amount; |
|
162 | - $registration_to_copy_total -= $payment_amount; |
|
163 | - $save = true; |
|
164 | - } |
|
165 | - } |
|
166 | - if ($save) { |
|
167 | - $target_registration->set_paid($new_registration_payment_total); |
|
168 | - $target_registration->save(); |
|
169 | - $registration_to_copy->set_paid($registration_to_copy_total); |
|
170 | - $registration_to_copy->save(); |
|
171 | - } |
|
172 | - return true; |
|
173 | - } |
|
124 | + /** |
|
125 | + * @param EE_Registration $target_registration |
|
126 | + * @param EE_Registration $registration_to_copy |
|
127 | + * @return bool |
|
128 | + * @throws RuntimeException |
|
129 | + * @throws UnexpectedEntityException |
|
130 | + * @throws EE_Error |
|
131 | + */ |
|
132 | + public function copyPaymentDetails( |
|
133 | + EE_Registration $target_registration, |
|
134 | + EE_Registration $registration_to_copy |
|
135 | + ) { |
|
136 | + $save = false; |
|
137 | + $previous_registration_payments = $registration_to_copy->registration_payments(); |
|
138 | + $new_registration_payment_total = 0; |
|
139 | + $registration_to_copy_total = $registration_to_copy->paid(); |
|
140 | + foreach ($previous_registration_payments as $previous_registration_payment) { |
|
141 | + if ($previous_registration_payment instanceof EE_Registration_Payment |
|
142 | + && $previous_registration_payment->payment() instanceof EE_Payment |
|
143 | + && $previous_registration_payment->payment()->is_approved() |
|
144 | + ) { |
|
145 | + $payment_amount = $previous_registration_payment->amount(); |
|
146 | + $new_registration_payment = EE_Registration_Payment::new_instance( |
|
147 | + array( |
|
148 | + 'REG_ID' => $target_registration->ID(), |
|
149 | + 'PAY_ID' => $previous_registration_payment->payment()->ID(), |
|
150 | + 'RPY_amount' => $payment_amount, |
|
151 | + ) |
|
152 | + ); |
|
153 | + if (! $new_registration_payment instanceof EE_Registration_Payment) { |
|
154 | + throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment'); |
|
155 | + } |
|
156 | + $new_registration_payment->save(); |
|
157 | + // if new reg payment is good, then set old reg payment amount to zero |
|
158 | + $previous_registration_payment->set_amount(0); |
|
159 | + $previous_registration_payment->save(); |
|
160 | + // now increment/decrement payment amounts |
|
161 | + $new_registration_payment_total += $payment_amount; |
|
162 | + $registration_to_copy_total -= $payment_amount; |
|
163 | + $save = true; |
|
164 | + } |
|
165 | + } |
|
166 | + if ($save) { |
|
167 | + $target_registration->set_paid($new_registration_payment_total); |
|
168 | + $target_registration->save(); |
|
169 | + $registration_to_copy->set_paid($registration_to_copy_total); |
|
170 | + $registration_to_copy->save(); |
|
171 | + } |
|
172 | + return true; |
|
173 | + } |
|
174 | 174 | } |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | 'ANS_value' => $old_answer_value, |
114 | 114 | ) |
115 | 115 | ); |
116 | - if (! $new_answer instanceof EE_Answer) { |
|
116 | + if ( ! $new_answer instanceof EE_Answer) { |
|
117 | 117 | throw new UnexpectedEntityException($new_answer, 'EE_Answer'); |
118 | 118 | } |
119 | 119 | $new_answer->save(); |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | 'RPY_amount' => $payment_amount, |
151 | 151 | ) |
152 | 152 | ); |
153 | - if (! $new_registration_payment instanceof EE_Registration_Payment) { |
|
153 | + if ( ! $new_registration_payment instanceof EE_Registration_Payment) { |
|
154 | 154 | throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment'); |
155 | 155 | } |
156 | 156 | $new_registration_payment->save(); |