Completed
Branch FET/conditional-update-queries (14ce8f)
by
unknown
62:24 queued 53:29
created
core/domain/services/event/EventSpacesCalculator.php 1 patch
Indentation   +711 added lines, -711 removed lines patch added patch discarded remove patch
@@ -26,715 +26,715 @@
 block discarded – undo
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
-        if ($this->debug) {
117
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 1);
118
-            \EEH_Debug_Tools::printr((string) $event->ID(), 'For event', __FILE__, __LINE__);
119
-        }
120
-        $this->event = $event;
121
-        $this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC'));
122
-        $this->setHooks();
123
-    }
124
-
125
-
126
-    /**
127
-     * @return void
128
-     */
129
-    private function setHooks()
130
-    {
131
-        add_action('AHEE__EE_Ticket__increase_sold', array($this, 'clearResults'));
132
-        add_action('AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults'));
133
-        add_action('AHEE__EE_Datetime__increase_sold', array($this, 'clearResults'));
134
-        add_action('AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults'));
135
-        add_action('AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults'));
136
-        add_action('AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults'));
137
-        add_action('AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults'));
138
-        add_action('AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults'));
139
-    }
140
-
141
-
142
-    /**
143
-     * @return void
144
-     */
145
-    public function clearResults()
146
-    {
147
-        if ($this->debug) {
148
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 1);
149
-        }
150
-        $this->spaces_remaining = null;
151
-        $this->total_spaces_available = null;
152
-    }
153
-
154
-
155
-    /**
156
-     * @return EE_Ticket[]
157
-     * @throws EE_Error
158
-     * @throws InvalidDataTypeException
159
-     * @throws InvalidInterfaceException
160
-     * @throws InvalidArgumentException
161
-     */
162
-    public function getActiveTickets()
163
-    {
164
-        if (empty($this->active_tickets)) {
165
-            $this->active_tickets = $this->event->tickets(
166
-                array(
167
-                    array('TKT_deleted' => false),
168
-                    'order_by' => array('TKT_qty' => 'ASC'),
169
-                )
170
-            );
171
-        }
172
-        return $this->active_tickets;
173
-    }
174
-
175
-
176
-    /**
177
-     * @param EE_Ticket[] $active_tickets
178
-     * @throws EE_Error
179
-     * @throws DomainException
180
-     * @throws UnexpectedEntityException
181
-     */
182
-    public function setActiveTickets(array $active_tickets = array())
183
-    {
184
-        if (! empty($active_tickets)) {
185
-            foreach ($active_tickets as $active_ticket) {
186
-                $this->validateTicket($active_ticket);
187
-            }
188
-            // sort incoming array by ticket quantity (asc)
189
-            usort(
190
-                $active_tickets,
191
-                function (EE_Ticket $a, EE_Ticket $b) {
192
-                    if ($a->qty() === $b->qty()) {
193
-                        return 0;
194
-                    }
195
-                    return ($a->qty() < $b->qty())
196
-                        ? -1
197
-                        : 1;
198
-                }
199
-            );
200
-        }
201
-        $this->active_tickets = $active_tickets;
202
-    }
203
-
204
-
205
-    /**
206
-     * @param $ticket
207
-     * @throws DomainException
208
-     * @throws EE_Error
209
-     * @throws UnexpectedEntityException
210
-     */
211
-    private function validateTicket($ticket)
212
-    {
213
-        if (! $ticket instanceof EE_Ticket) {
214
-            throw new DomainException(
215
-                esc_html__(
216
-                    'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.',
217
-                    'event_espresso'
218
-                )
219
-            );
220
-        }
221
-        if ($ticket->get_event_ID() !== $this->event->ID()) {
222
-            throw new DomainException(
223
-                sprintf(
224
-                    esc_html__(
225
-                        'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.',
226
-                        'event_espresso'
227
-                    ),
228
-                    $ticket->get_event_ID(),
229
-                    $this->event->ID()
230
-                )
231
-            );
232
-        }
233
-    }
234
-
235
-
236
-    /**
237
-     * @return EE_Datetime[]
238
-     */
239
-    public function getDatetimes()
240
-    {
241
-        return $this->datetimes;
242
-    }
243
-
244
-
245
-    /**
246
-     * @param EE_Datetime $datetime
247
-     * @throws EE_Error
248
-     * @throws DomainException
249
-     */
250
-    public function setDatetime(EE_Datetime $datetime)
251
-    {
252
-        if ($datetime->event()->ID() !== $this->event->ID()) {
253
-            throw new DomainException(
254
-                sprintf(
255
-                    esc_html__(
256
-                        'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.',
257
-                        'event_espresso'
258
-                    ),
259
-                    $datetime->event()->ID(),
260
-                    $this->event->ID()
261
-                )
262
-            );
263
-        }
264
-        $this->datetimes[ $datetime->ID() ] = $datetime;
265
-    }
266
-
267
-
268
-    /**
269
-     * calculate spaces remaining based on "saleable" tickets
270
-     *
271
-     * @return float|int
272
-     * @throws EE_Error
273
-     * @throws DomainException
274
-     * @throws UnexpectedEntityException
275
-     * @throws InvalidDataTypeException
276
-     * @throws InvalidInterfaceException
277
-     * @throws InvalidArgumentException
278
-     */
279
-    public function spacesRemaining()
280
-    {
281
-        if ($this->debug) {
282
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
283
-        }
284
-        if ($this->spaces_remaining === null) {
285
-            $this->initialize();
286
-            $this->spaces_remaining = $this->calculate();
287
-        }
288
-        return $this->spaces_remaining;
289
-    }
290
-
291
-
292
-    /**
293
-     * calculates total available spaces for an event with no regard for sold tickets
294
-     *
295
-     * @return int|float
296
-     * @throws EE_Error
297
-     * @throws DomainException
298
-     * @throws UnexpectedEntityException
299
-     * @throws InvalidDataTypeException
300
-     * @throws InvalidInterfaceException
301
-     * @throws InvalidArgumentException
302
-     */
303
-    public function totalSpacesAvailable()
304
-    {
305
-        if ($this->debug) {
306
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
307
-        }
308
-        if ($this->total_spaces_available === null) {
309
-            $this->initialize();
310
-            $this->total_spaces_available = $this->calculate(false);
311
-        }
312
-        return $this->total_spaces_available;
313
-    }
314
-
315
-
316
-    /**
317
-     * Loops through the active tickets for the event
318
-     * and builds a series of data arrays that will be used for calculating
319
-     * the total maximum available spaces, as well as the spaces remaining.
320
-     * Because ticket quantities affect datetime spaces and vice versa,
321
-     * we need to be constantly updating these data arrays as things change,
322
-     * which is the entire reason for their existence.
323
-     *
324
-     * @throws EE_Error
325
-     * @throws DomainException
326
-     * @throws UnexpectedEntityException
327
-     * @throws InvalidDataTypeException
328
-     * @throws InvalidInterfaceException
329
-     * @throws InvalidArgumentException
330
-     */
331
-    private function initialize()
332
-    {
333
-        if ($this->debug) {
334
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
335
-        }
336
-        $this->datetime_tickets = array();
337
-        $this->datetime_spaces = array();
338
-        $this->ticket_datetimes = array();
339
-        $this->ticket_quantities = array();
340
-        $this->tickets_sold = array();
341
-        $this->total_spaces = array();
342
-        $active_tickets = $this->getActiveTickets();
343
-        if (! empty($active_tickets)) {
344
-            foreach ($active_tickets as $ticket) {
345
-                $this->validateTicket($ticket);
346
-                // we need to index our data arrays using strings for the purpose of sorting,
347
-                // but we also need them to be unique, so  we'll just prepend a letter T to the ID
348
-                $ticket_identifier = "T{$ticket->ID()}";
349
-                // to start, we'll just consider the raw qty to be the maximum availability for this ticket,
350
-                // unless the ticket is past its "sell until" date, in which case the qty will be 0
351
-                $max_tickets = $ticket->is_expired() ? 0 : $ticket->qty();
352
-                // but we'll adjust that after looping over each datetime for the ticket and checking reg limits
353
-                $ticket_datetimes = $ticket->datetimes($this->datetime_query_params);
354
-                foreach ($ticket_datetimes as $datetime) {
355
-                    // save all datetimes
356
-                    $this->setDatetime($datetime);
357
-                    $datetime_identifier = "D{$datetime->ID()}";
358
-                    $reg_limit = $datetime->reg_limit();
359
-                    // ticket quantity can not exceed datetime reg limit
360
-                    $max_tickets = min($max_tickets, $reg_limit);
361
-                    // as described earlier, because we need to be able to constantly adjust numbers for things,
362
-                    // we are going to move all of our data into the following arrays:
363
-                    // datetime spaces initially represents the reg limit for each datetime,
364
-                    // but this will get adjusted as tickets are accounted for
365
-                    $this->datetime_spaces[ $datetime_identifier ] = $reg_limit;
366
-                    // just an array of ticket IDs grouped by datetime
367
-                    $this->datetime_tickets[ $datetime_identifier ][] = $ticket_identifier;
368
-                    // and an array of datetime IDs grouped by ticket
369
-                    $this->ticket_datetimes[ $ticket_identifier ][] = $datetime_identifier;
370
-                }
371
-                // total quantity of sold and reserved for each ticket
372
-                $this->tickets_sold[ $ticket_identifier ] = $ticket->sold() + $ticket->reserved();
373
-                // and the maximum ticket quantities for each ticket (adjusted for reg limit)
374
-                $this->ticket_quantities[ $ticket_identifier ] = $max_tickets;
375
-            }
376
-        }
377
-        // sort datetime spaces by reg limit, but maintain our string indexes
378
-        asort($this->datetime_spaces, SORT_NUMERIC);
379
-        // datetime tickets need to be sorted in the SAME order as the above array...
380
-        // so we'll just use array_merge() to take the structure of datetime_spaces
381
-        // but overwrite all of the data with that from datetime_tickets
382
-        $this->datetime_tickets = array_merge(
383
-            $this->datetime_spaces,
384
-            $this->datetime_tickets
385
-        );
386
-        if ($this->debug) {
387
-            \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
388
-            \EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__);
389
-            \EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__);
390
-        }
391
-    }
392
-
393
-
394
-    /**
395
-     * performs calculations on initialized data
396
-     *
397
-     * @param bool $consider_sold
398
-     * @return int|float
399
-     */
400
-    private function calculate($consider_sold = true)
401
-    {
402
-        if ($this->debug) {
403
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
404
-            \EEH_Debug_Tools::printr($consider_sold, '$consider_sold', __FILE__, __LINE__);
405
-        }
406
-        if ($consider_sold) {
407
-            // subtract amounts sold from all ticket quantities and datetime spaces
408
-            $this->adjustTicketQuantitiesDueToSales();
409
-        }
410
-        foreach ($this->datetime_tickets as $datetime_identifier => $tickets) {
411
-            $this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets);
412
-        }
413
-        // total spaces available is just the sum of the spaces available for each datetime
414
-        $spaces_remaining = array_sum($this->total_spaces);
415
-        if ($this->debug) {
416
-            \EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__);
417
-            \EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__);
418
-            \EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__);
419
-        }
420
-        return $spaces_remaining;
421
-    }
422
-
423
-
424
-    /**
425
-     * subtracts amount of  tickets sold from ticket quantities and datetime spaces
426
-     */
427
-    private function adjustTicketQuantitiesDueToSales()
428
-    {
429
-        if ($this->debug) {
430
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
431
-        }
432
-        foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) {
433
-            if (isset($this->ticket_quantities[ $ticket_identifier ])) {
434
-                $this->ticket_quantities[ $ticket_identifier ] -= $tickets_sold;
435
-                // don't let values go below zero
436
-                $this->ticket_quantities[ $ticket_identifier ] = max(
437
-                    $this->ticket_quantities[ $ticket_identifier ],
438
-                    0
439
-                );
440
-                if ($this->debug) {
441
-                    \EEH_Debug_Tools::printr(
442
-                        "{$tickets_sold} sales for ticket {$ticket_identifier} ",
443
-                        'subtracting',
444
-                        __FILE__,
445
-                        __LINE__
446
-                    );
447
-                }
448
-            }
449
-            if (isset($this->ticket_datetimes[ $ticket_identifier ])
450
-                && is_array($this->ticket_datetimes[ $ticket_identifier ])
451
-            ) {
452
-                foreach ($this->ticket_datetimes[ $ticket_identifier ] as $ticket_datetime) {
453
-                    if (isset($this->ticket_quantities[ $ticket_identifier ])) {
454
-                        $this->datetime_spaces[ $ticket_datetime ] -= $tickets_sold;
455
-                        // don't let values go below zero
456
-                        $this->datetime_spaces[ $ticket_datetime ] = max(
457
-                            $this->datetime_spaces[ $ticket_datetime ],
458
-                            0
459
-                        );
460
-                        if ($this->debug) {
461
-                            \EEH_Debug_Tools::printr(
462
-                                "{$tickets_sold} sales for datetime {$ticket_datetime} ",
463
-                                'subtracting',
464
-                                __FILE__,
465
-                                __LINE__
466
-                            );
467
-                        }
468
-                    }
469
-                }
470
-            }
471
-        }
472
-    }
473
-
474
-
475
-    /**
476
-     * @param string $datetime_identifier
477
-     * @param array  $tickets
478
-     */
479
-    private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets)
480
-    {
481
-        // make sure a reg limit is set for the datetime
482
-        $reg_limit = isset($this->datetime_spaces[ $datetime_identifier ])
483
-            ? $this->datetime_spaces[ $datetime_identifier ]
484
-            : 0;
485
-        // and bail if it is not
486
-        if (! $reg_limit) {
487
-            if ($this->debug) {
488
-                \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__);
489
-            }
490
-            return;
491
-        }
492
-        if ($this->debug) {
493
-            \EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1);
494
-            \EEH_Debug_Tools::printr(
495
-                "{$reg_limit}",
496
-                'REG LIMIT',
497
-                __FILE__,
498
-                __LINE__
499
-            );
500
-        }
501
-        // number of allocated spaces always starts at zero
502
-        $spaces_allocated = 0;
503
-        $this->total_spaces[ $datetime_identifier ] = 0;
504
-        foreach ($tickets as $ticket_identifier) {
505
-            $spaces_allocated = $this->calculateAvailableSpacesForTicket(
506
-                $datetime_identifier,
507
-                $reg_limit,
508
-                $ticket_identifier,
509
-                $spaces_allocated
510
-            );
511
-        }
512
-        // spaces can't be negative
513
-        $spaces_allocated = max($spaces_allocated, 0);
514
-        if ($spaces_allocated) {
515
-            // track any non-zero values
516
-            $this->total_spaces[ $datetime_identifier ] += $spaces_allocated;
517
-            if ($this->debug) {
518
-                \EEH_Debug_Tools::printr((string) $spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__);
519
-            }
520
-        } else {
521
-            if ($this->debug) {
522
-                \EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__);
523
-            }
524
-        }
525
-        if ($this->debug) {
526
-            \EEH_Debug_Tools::printr(
527
-                $this->total_spaces[ $datetime_identifier ],
528
-                '$total_spaces',
529
-                __FILE__,
530
-                __LINE__
531
-            );
532
-            \EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__);
533
-            \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
534
-        }
535
-    }
536
-
537
-
538
-    /**
539
-     * @param string $datetime_identifier
540
-     * @param int    $reg_limit
541
-     * @param string $ticket_identifier
542
-     * @param int    $spaces_allocated
543
-     * @return int
544
-     */
545
-    private function calculateAvailableSpacesForTicket(
546
-        $datetime_identifier,
547
-        $reg_limit,
548
-        $ticket_identifier,
549
-        $spaces_allocated
550
-    ) {
551
-        // make sure ticket quantity is set
552
-        $ticket_quantity = isset($this->ticket_quantities[ $ticket_identifier ])
553
-            ? $this->ticket_quantities[ $ticket_identifier ]
554
-            : 0;
555
-        if ($this->debug) {
556
-            \EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__);
557
-            \EEH_Debug_Tools::printr(
558
-                "{$ticket_quantity}",
559
-                "ticket $ticket_identifier quantity: ",
560
-                __FILE__,
561
-                __LINE__,
562
-                2
563
-            );
564
-        }
565
-        if ($ticket_quantity) {
566
-            if ($this->debug) {
567
-                \EEH_Debug_Tools::printr(
568
-                    ($spaces_allocated <= $reg_limit)
569
-                        ? 'true'
570
-                        : 'false',
571
-                    ' . spaces_allocated <= reg_limit = ',
572
-                    __FILE__,
573
-                    __LINE__
574
-                );
575
-            }
576
-            // if the datetime is NOT at full capacity yet
577
-            if ($spaces_allocated <= $reg_limit) {
578
-                // then the maximum ticket quantity we can allocate is the lowest value of either:
579
-                //  the number of remaining spaces for the datetime, which is the limit - spaces already taken
580
-                //  or the maximum ticket quantity
581
-                $ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity);
582
-                // adjust the available quantity in our tracking array
583
-                $this->ticket_quantities[ $ticket_identifier ] -= $ticket_quantity;
584
-                // and increment spaces allocated for this datetime
585
-                $spaces_allocated += $ticket_quantity;
586
-                $at_capacity = $spaces_allocated >= $reg_limit;
587
-                if ($this->debug) {
588
-                    \EEH_Debug_Tools::printr(
589
-                        "{$ticket_quantity} {$ticket_identifier} tickets",
590
-                        ' > > allocate ',
591
-                        __FILE__,
592
-                        __LINE__,
593
-                        3
594
-                    );
595
-                    if ($at_capacity) {
596
-                        \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3);
597
-                    }
598
-                }
599
-                // now adjust all other datetimes that allow access to this ticket
600
-                $this->adjustDatetimes(
601
-                    $datetime_identifier,
602
-                    $ticket_identifier,
603
-                    $ticket_quantity,
604
-                    $at_capacity
605
-                );
606
-            }
607
-        }
608
-        return $spaces_allocated;
609
-    }
610
-
611
-
612
-    /**
613
-     * subtracts ticket amounts from all datetime reg limits
614
-     * that allow access to the ticket specified,
615
-     * because that ticket could be used
616
-     * to attend any of the datetimes it has access to
617
-     *
618
-     * @param string $datetime_identifier
619
-     * @param string $ticket_identifier
620
-     * @param bool   $at_capacity
621
-     * @param int    $ticket_quantity
622
-     */
623
-    private function adjustDatetimes(
624
-        $datetime_identifier,
625
-        $ticket_identifier,
626
-        $ticket_quantity,
627
-        $at_capacity
628
-    ) {
629
-        /** @var array $datetime_tickets */
630
-        foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) {
631
-            if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) {
632
-                continue;
633
-            }
634
-            $adjusted = $this->adjustDatetimeSpaces(
635
-                $datetime_ID,
636
-                $ticket_identifier,
637
-                $ticket_quantity
638
-            );
639
-            // skip to next ticket if nothing changed
640
-            if (! ($adjusted || $at_capacity)) {
641
-                continue;
642
-            }
643
-            // then all of it's tickets are now unavailable
644
-            foreach ($datetime_tickets as $datetime_ticket) {
645
-                if (($ticket_identifier === $datetime_ticket || $at_capacity)
646
-                    && isset($this->ticket_quantities[ $datetime_ticket ])
647
-                    && $this->ticket_quantities[ $datetime_ticket ] > 0
648
-                ) {
649
-                    if ($this->debug) {
650
-                        \EEH_Debug_Tools::printr(
651
-                            $datetime_ticket,
652
-                            ' . . . adjust ticket quantities for',
653
-                            __FILE__,
654
-                            __LINE__
655
-                        );
656
-                    }
657
-                    // if this datetime is at full capacity, set any tracked available quantities to zero
658
-                    // otherwise just subtract the ticket quantity
659
-                    $new_quantity = $at_capacity
660
-                        ? 0
661
-                        : $this->ticket_quantities[ $datetime_ticket ] - $ticket_quantity;
662
-                    // don't let ticket quantity go below zero
663
-                    $this->ticket_quantities[ $datetime_ticket ] = max($new_quantity, 0);
664
-                    if ($this->debug) {
665
-                        \EEH_Debug_Tools::printr(
666
-                            $at_capacity
667
-                                ? "0 because Datetime {$datetime_identifier} is at capacity"
668
-                                : "{$this->ticket_quantities[ $datetime_ticket ]}",
669
-                            " . . . . {$datetime_ticket} quantity set to ",
670
-                            __FILE__,
671
-                            __LINE__
672
-                        );
673
-                    }
674
-                }
675
-                // but we also need to adjust spaces for any other datetimes this ticket has access to
676
-                if ($datetime_ticket === $ticket_identifier) {
677
-                    if (isset($this->ticket_datetimes[ $datetime_ticket ])
678
-                        && is_array($this->ticket_datetimes[ $datetime_ticket ])
679
-                    ) {
680
-                        if ($this->debug) {
681
-                            \EEH_Debug_Tools::printr(
682
-                                $datetime_ticket,
683
-                                ' . . adjust other Datetimes for',
684
-                                __FILE__,
685
-                                __LINE__
686
-                            );
687
-                        }
688
-                        foreach ($this->ticket_datetimes[ $datetime_ticket ] as $datetime) {
689
-                            // don't adjust the current datetime twice
690
-                            if ($datetime !== $datetime_identifier) {
691
-                                $this->adjustDatetimeSpaces(
692
-                                    $datetime,
693
-                                    $datetime_ticket,
694
-                                    $ticket_quantity
695
-                                );
696
-                            }
697
-                        }
698
-                    }
699
-                }
700
-            }
701
-        }
702
-    }
703
-
704
-    private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0)
705
-    {
706
-        // does datetime have spaces available?
707
-        // and does the supplied ticket have access to this datetime ?
708
-        if ($this->datetime_spaces[ $datetime_identifier ] > 0
709
-            && isset($this->datetime_spaces[ $datetime_identifier ], $this->datetime_tickets[ $datetime_identifier ])
710
-            && in_array($ticket_identifier, $this->datetime_tickets[ $datetime_identifier ], true)
711
-        ) {
712
-            if ($this->debug) {
713
-                \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__);
714
-                \EEH_Debug_Tools::printr(
715
-                    "{$this->datetime_spaces[ $datetime_identifier ]}",
716
-                    " . . current  {$datetime_identifier} spaces available",
717
-                    __FILE__,
718
-                    __LINE__
719
-                );
720
-            }
721
-            // then decrement the available spaces for the datetime
722
-            $this->datetime_spaces[ $datetime_identifier ] -= $ticket_quantity;
723
-            // but don't let quantities go below zero
724
-            $this->datetime_spaces[ $datetime_identifier ] = max(
725
-                $this->datetime_spaces[ $datetime_identifier ],
726
-                0
727
-            );
728
-            if ($this->debug) {
729
-                \EEH_Debug_Tools::printr(
730
-                    "{$ticket_quantity}",
731
-                    " . . . {$datetime_identifier} capacity reduced by",
732
-                    __FILE__,
733
-                    __LINE__
734
-                );
735
-            }
736
-            return true;
737
-        }
738
-        return false;
739
-    }
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
+		if ($this->debug) {
117
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 1);
118
+			\EEH_Debug_Tools::printr((string) $event->ID(), 'For event', __FILE__, __LINE__);
119
+		}
120
+		$this->event = $event;
121
+		$this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC'));
122
+		$this->setHooks();
123
+	}
124
+
125
+
126
+	/**
127
+	 * @return void
128
+	 */
129
+	private function setHooks()
130
+	{
131
+		add_action('AHEE__EE_Ticket__increase_sold', array($this, 'clearResults'));
132
+		add_action('AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults'));
133
+		add_action('AHEE__EE_Datetime__increase_sold', array($this, 'clearResults'));
134
+		add_action('AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults'));
135
+		add_action('AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults'));
136
+		add_action('AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults'));
137
+		add_action('AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults'));
138
+		add_action('AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults'));
139
+	}
140
+
141
+
142
+	/**
143
+	 * @return void
144
+	 */
145
+	public function clearResults()
146
+	{
147
+		if ($this->debug) {
148
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 1);
149
+		}
150
+		$this->spaces_remaining = null;
151
+		$this->total_spaces_available = null;
152
+	}
153
+
154
+
155
+	/**
156
+	 * @return EE_Ticket[]
157
+	 * @throws EE_Error
158
+	 * @throws InvalidDataTypeException
159
+	 * @throws InvalidInterfaceException
160
+	 * @throws InvalidArgumentException
161
+	 */
162
+	public function getActiveTickets()
163
+	{
164
+		if (empty($this->active_tickets)) {
165
+			$this->active_tickets = $this->event->tickets(
166
+				array(
167
+					array('TKT_deleted' => false),
168
+					'order_by' => array('TKT_qty' => 'ASC'),
169
+				)
170
+			);
171
+		}
172
+		return $this->active_tickets;
173
+	}
174
+
175
+
176
+	/**
177
+	 * @param EE_Ticket[] $active_tickets
178
+	 * @throws EE_Error
179
+	 * @throws DomainException
180
+	 * @throws UnexpectedEntityException
181
+	 */
182
+	public function setActiveTickets(array $active_tickets = array())
183
+	{
184
+		if (! empty($active_tickets)) {
185
+			foreach ($active_tickets as $active_ticket) {
186
+				$this->validateTicket($active_ticket);
187
+			}
188
+			// sort incoming array by ticket quantity (asc)
189
+			usort(
190
+				$active_tickets,
191
+				function (EE_Ticket $a, EE_Ticket $b) {
192
+					if ($a->qty() === $b->qty()) {
193
+						return 0;
194
+					}
195
+					return ($a->qty() < $b->qty())
196
+						? -1
197
+						: 1;
198
+				}
199
+			);
200
+		}
201
+		$this->active_tickets = $active_tickets;
202
+	}
203
+
204
+
205
+	/**
206
+	 * @param $ticket
207
+	 * @throws DomainException
208
+	 * @throws EE_Error
209
+	 * @throws UnexpectedEntityException
210
+	 */
211
+	private function validateTicket($ticket)
212
+	{
213
+		if (! $ticket instanceof EE_Ticket) {
214
+			throw new DomainException(
215
+				esc_html__(
216
+					'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.',
217
+					'event_espresso'
218
+				)
219
+			);
220
+		}
221
+		if ($ticket->get_event_ID() !== $this->event->ID()) {
222
+			throw new DomainException(
223
+				sprintf(
224
+					esc_html__(
225
+						'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.',
226
+						'event_espresso'
227
+					),
228
+					$ticket->get_event_ID(),
229
+					$this->event->ID()
230
+				)
231
+			);
232
+		}
233
+	}
234
+
235
+
236
+	/**
237
+	 * @return EE_Datetime[]
238
+	 */
239
+	public function getDatetimes()
240
+	{
241
+		return $this->datetimes;
242
+	}
243
+
244
+
245
+	/**
246
+	 * @param EE_Datetime $datetime
247
+	 * @throws EE_Error
248
+	 * @throws DomainException
249
+	 */
250
+	public function setDatetime(EE_Datetime $datetime)
251
+	{
252
+		if ($datetime->event()->ID() !== $this->event->ID()) {
253
+			throw new DomainException(
254
+				sprintf(
255
+					esc_html__(
256
+						'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.',
257
+						'event_espresso'
258
+					),
259
+					$datetime->event()->ID(),
260
+					$this->event->ID()
261
+				)
262
+			);
263
+		}
264
+		$this->datetimes[ $datetime->ID() ] = $datetime;
265
+	}
266
+
267
+
268
+	/**
269
+	 * calculate spaces remaining based on "saleable" tickets
270
+	 *
271
+	 * @return float|int
272
+	 * @throws EE_Error
273
+	 * @throws DomainException
274
+	 * @throws UnexpectedEntityException
275
+	 * @throws InvalidDataTypeException
276
+	 * @throws InvalidInterfaceException
277
+	 * @throws InvalidArgumentException
278
+	 */
279
+	public function spacesRemaining()
280
+	{
281
+		if ($this->debug) {
282
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
283
+		}
284
+		if ($this->spaces_remaining === null) {
285
+			$this->initialize();
286
+			$this->spaces_remaining = $this->calculate();
287
+		}
288
+		return $this->spaces_remaining;
289
+	}
290
+
291
+
292
+	/**
293
+	 * calculates total available spaces for an event with no regard for sold tickets
294
+	 *
295
+	 * @return int|float
296
+	 * @throws EE_Error
297
+	 * @throws DomainException
298
+	 * @throws UnexpectedEntityException
299
+	 * @throws InvalidDataTypeException
300
+	 * @throws InvalidInterfaceException
301
+	 * @throws InvalidArgumentException
302
+	 */
303
+	public function totalSpacesAvailable()
304
+	{
305
+		if ($this->debug) {
306
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
307
+		}
308
+		if ($this->total_spaces_available === null) {
309
+			$this->initialize();
310
+			$this->total_spaces_available = $this->calculate(false);
311
+		}
312
+		return $this->total_spaces_available;
313
+	}
314
+
315
+
316
+	/**
317
+	 * Loops through the active tickets for the event
318
+	 * and builds a series of data arrays that will be used for calculating
319
+	 * the total maximum available spaces, as well as the spaces remaining.
320
+	 * Because ticket quantities affect datetime spaces and vice versa,
321
+	 * we need to be constantly updating these data arrays as things change,
322
+	 * which is the entire reason for their existence.
323
+	 *
324
+	 * @throws EE_Error
325
+	 * @throws DomainException
326
+	 * @throws UnexpectedEntityException
327
+	 * @throws InvalidDataTypeException
328
+	 * @throws InvalidInterfaceException
329
+	 * @throws InvalidArgumentException
330
+	 */
331
+	private function initialize()
332
+	{
333
+		if ($this->debug) {
334
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
335
+		}
336
+		$this->datetime_tickets = array();
337
+		$this->datetime_spaces = array();
338
+		$this->ticket_datetimes = array();
339
+		$this->ticket_quantities = array();
340
+		$this->tickets_sold = array();
341
+		$this->total_spaces = array();
342
+		$active_tickets = $this->getActiveTickets();
343
+		if (! empty($active_tickets)) {
344
+			foreach ($active_tickets as $ticket) {
345
+				$this->validateTicket($ticket);
346
+				// we need to index our data arrays using strings for the purpose of sorting,
347
+				// but we also need them to be unique, so  we'll just prepend a letter T to the ID
348
+				$ticket_identifier = "T{$ticket->ID()}";
349
+				// to start, we'll just consider the raw qty to be the maximum availability for this ticket,
350
+				// unless the ticket is past its "sell until" date, in which case the qty will be 0
351
+				$max_tickets = $ticket->is_expired() ? 0 : $ticket->qty();
352
+				// but we'll adjust that after looping over each datetime for the ticket and checking reg limits
353
+				$ticket_datetimes = $ticket->datetimes($this->datetime_query_params);
354
+				foreach ($ticket_datetimes as $datetime) {
355
+					// save all datetimes
356
+					$this->setDatetime($datetime);
357
+					$datetime_identifier = "D{$datetime->ID()}";
358
+					$reg_limit = $datetime->reg_limit();
359
+					// ticket quantity can not exceed datetime reg limit
360
+					$max_tickets = min($max_tickets, $reg_limit);
361
+					// as described earlier, because we need to be able to constantly adjust numbers for things,
362
+					// we are going to move all of our data into the following arrays:
363
+					// datetime spaces initially represents the reg limit for each datetime,
364
+					// but this will get adjusted as tickets are accounted for
365
+					$this->datetime_spaces[ $datetime_identifier ] = $reg_limit;
366
+					// just an array of ticket IDs grouped by datetime
367
+					$this->datetime_tickets[ $datetime_identifier ][] = $ticket_identifier;
368
+					// and an array of datetime IDs grouped by ticket
369
+					$this->ticket_datetimes[ $ticket_identifier ][] = $datetime_identifier;
370
+				}
371
+				// total quantity of sold and reserved for each ticket
372
+				$this->tickets_sold[ $ticket_identifier ] = $ticket->sold() + $ticket->reserved();
373
+				// and the maximum ticket quantities for each ticket (adjusted for reg limit)
374
+				$this->ticket_quantities[ $ticket_identifier ] = $max_tickets;
375
+			}
376
+		}
377
+		// sort datetime spaces by reg limit, but maintain our string indexes
378
+		asort($this->datetime_spaces, SORT_NUMERIC);
379
+		// datetime tickets need to be sorted in the SAME order as the above array...
380
+		// so we'll just use array_merge() to take the structure of datetime_spaces
381
+		// but overwrite all of the data with that from datetime_tickets
382
+		$this->datetime_tickets = array_merge(
383
+			$this->datetime_spaces,
384
+			$this->datetime_tickets
385
+		);
386
+		if ($this->debug) {
387
+			\EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
388
+			\EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__);
389
+			\EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__);
390
+		}
391
+	}
392
+
393
+
394
+	/**
395
+	 * performs calculations on initialized data
396
+	 *
397
+	 * @param bool $consider_sold
398
+	 * @return int|float
399
+	 */
400
+	private function calculate($consider_sold = true)
401
+	{
402
+		if ($this->debug) {
403
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
404
+			\EEH_Debug_Tools::printr($consider_sold, '$consider_sold', __FILE__, __LINE__);
405
+		}
406
+		if ($consider_sold) {
407
+			// subtract amounts sold from all ticket quantities and datetime spaces
408
+			$this->adjustTicketQuantitiesDueToSales();
409
+		}
410
+		foreach ($this->datetime_tickets as $datetime_identifier => $tickets) {
411
+			$this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets);
412
+		}
413
+		// total spaces available is just the sum of the spaces available for each datetime
414
+		$spaces_remaining = array_sum($this->total_spaces);
415
+		if ($this->debug) {
416
+			\EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__);
417
+			\EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__);
418
+			\EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__);
419
+		}
420
+		return $spaces_remaining;
421
+	}
422
+
423
+
424
+	/**
425
+	 * subtracts amount of  tickets sold from ticket quantities and datetime spaces
426
+	 */
427
+	private function adjustTicketQuantitiesDueToSales()
428
+	{
429
+		if ($this->debug) {
430
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
431
+		}
432
+		foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) {
433
+			if (isset($this->ticket_quantities[ $ticket_identifier ])) {
434
+				$this->ticket_quantities[ $ticket_identifier ] -= $tickets_sold;
435
+				// don't let values go below zero
436
+				$this->ticket_quantities[ $ticket_identifier ] = max(
437
+					$this->ticket_quantities[ $ticket_identifier ],
438
+					0
439
+				);
440
+				if ($this->debug) {
441
+					\EEH_Debug_Tools::printr(
442
+						"{$tickets_sold} sales for ticket {$ticket_identifier} ",
443
+						'subtracting',
444
+						__FILE__,
445
+						__LINE__
446
+					);
447
+				}
448
+			}
449
+			if (isset($this->ticket_datetimes[ $ticket_identifier ])
450
+				&& is_array($this->ticket_datetimes[ $ticket_identifier ])
451
+			) {
452
+				foreach ($this->ticket_datetimes[ $ticket_identifier ] as $ticket_datetime) {
453
+					if (isset($this->ticket_quantities[ $ticket_identifier ])) {
454
+						$this->datetime_spaces[ $ticket_datetime ] -= $tickets_sold;
455
+						// don't let values go below zero
456
+						$this->datetime_spaces[ $ticket_datetime ] = max(
457
+							$this->datetime_spaces[ $ticket_datetime ],
458
+							0
459
+						);
460
+						if ($this->debug) {
461
+							\EEH_Debug_Tools::printr(
462
+								"{$tickets_sold} sales for datetime {$ticket_datetime} ",
463
+								'subtracting',
464
+								__FILE__,
465
+								__LINE__
466
+							);
467
+						}
468
+					}
469
+				}
470
+			}
471
+		}
472
+	}
473
+
474
+
475
+	/**
476
+	 * @param string $datetime_identifier
477
+	 * @param array  $tickets
478
+	 */
479
+	private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets)
480
+	{
481
+		// make sure a reg limit is set for the datetime
482
+		$reg_limit = isset($this->datetime_spaces[ $datetime_identifier ])
483
+			? $this->datetime_spaces[ $datetime_identifier ]
484
+			: 0;
485
+		// and bail if it is not
486
+		if (! $reg_limit) {
487
+			if ($this->debug) {
488
+				\EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__);
489
+			}
490
+			return;
491
+		}
492
+		if ($this->debug) {
493
+			\EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1);
494
+			\EEH_Debug_Tools::printr(
495
+				"{$reg_limit}",
496
+				'REG LIMIT',
497
+				__FILE__,
498
+				__LINE__
499
+			);
500
+		}
501
+		// number of allocated spaces always starts at zero
502
+		$spaces_allocated = 0;
503
+		$this->total_spaces[ $datetime_identifier ] = 0;
504
+		foreach ($tickets as $ticket_identifier) {
505
+			$spaces_allocated = $this->calculateAvailableSpacesForTicket(
506
+				$datetime_identifier,
507
+				$reg_limit,
508
+				$ticket_identifier,
509
+				$spaces_allocated
510
+			);
511
+		}
512
+		// spaces can't be negative
513
+		$spaces_allocated = max($spaces_allocated, 0);
514
+		if ($spaces_allocated) {
515
+			// track any non-zero values
516
+			$this->total_spaces[ $datetime_identifier ] += $spaces_allocated;
517
+			if ($this->debug) {
518
+				\EEH_Debug_Tools::printr((string) $spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__);
519
+			}
520
+		} else {
521
+			if ($this->debug) {
522
+				\EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__);
523
+			}
524
+		}
525
+		if ($this->debug) {
526
+			\EEH_Debug_Tools::printr(
527
+				$this->total_spaces[ $datetime_identifier ],
528
+				'$total_spaces',
529
+				__FILE__,
530
+				__LINE__
531
+			);
532
+			\EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__);
533
+			\EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
534
+		}
535
+	}
536
+
537
+
538
+	/**
539
+	 * @param string $datetime_identifier
540
+	 * @param int    $reg_limit
541
+	 * @param string $ticket_identifier
542
+	 * @param int    $spaces_allocated
543
+	 * @return int
544
+	 */
545
+	private function calculateAvailableSpacesForTicket(
546
+		$datetime_identifier,
547
+		$reg_limit,
548
+		$ticket_identifier,
549
+		$spaces_allocated
550
+	) {
551
+		// make sure ticket quantity is set
552
+		$ticket_quantity = isset($this->ticket_quantities[ $ticket_identifier ])
553
+			? $this->ticket_quantities[ $ticket_identifier ]
554
+			: 0;
555
+		if ($this->debug) {
556
+			\EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__);
557
+			\EEH_Debug_Tools::printr(
558
+				"{$ticket_quantity}",
559
+				"ticket $ticket_identifier quantity: ",
560
+				__FILE__,
561
+				__LINE__,
562
+				2
563
+			);
564
+		}
565
+		if ($ticket_quantity) {
566
+			if ($this->debug) {
567
+				\EEH_Debug_Tools::printr(
568
+					($spaces_allocated <= $reg_limit)
569
+						? 'true'
570
+						: 'false',
571
+					' . spaces_allocated <= reg_limit = ',
572
+					__FILE__,
573
+					__LINE__
574
+				);
575
+			}
576
+			// if the datetime is NOT at full capacity yet
577
+			if ($spaces_allocated <= $reg_limit) {
578
+				// then the maximum ticket quantity we can allocate is the lowest value of either:
579
+				//  the number of remaining spaces for the datetime, which is the limit - spaces already taken
580
+				//  or the maximum ticket quantity
581
+				$ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity);
582
+				// adjust the available quantity in our tracking array
583
+				$this->ticket_quantities[ $ticket_identifier ] -= $ticket_quantity;
584
+				// and increment spaces allocated for this datetime
585
+				$spaces_allocated += $ticket_quantity;
586
+				$at_capacity = $spaces_allocated >= $reg_limit;
587
+				if ($this->debug) {
588
+					\EEH_Debug_Tools::printr(
589
+						"{$ticket_quantity} {$ticket_identifier} tickets",
590
+						' > > allocate ',
591
+						__FILE__,
592
+						__LINE__,
593
+						3
594
+					);
595
+					if ($at_capacity) {
596
+						\EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3);
597
+					}
598
+				}
599
+				// now adjust all other datetimes that allow access to this ticket
600
+				$this->adjustDatetimes(
601
+					$datetime_identifier,
602
+					$ticket_identifier,
603
+					$ticket_quantity,
604
+					$at_capacity
605
+				);
606
+			}
607
+		}
608
+		return $spaces_allocated;
609
+	}
610
+
611
+
612
+	/**
613
+	 * subtracts ticket amounts from all datetime reg limits
614
+	 * that allow access to the ticket specified,
615
+	 * because that ticket could be used
616
+	 * to attend any of the datetimes it has access to
617
+	 *
618
+	 * @param string $datetime_identifier
619
+	 * @param string $ticket_identifier
620
+	 * @param bool   $at_capacity
621
+	 * @param int    $ticket_quantity
622
+	 */
623
+	private function adjustDatetimes(
624
+		$datetime_identifier,
625
+		$ticket_identifier,
626
+		$ticket_quantity,
627
+		$at_capacity
628
+	) {
629
+		/** @var array $datetime_tickets */
630
+		foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) {
631
+			if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) {
632
+				continue;
633
+			}
634
+			$adjusted = $this->adjustDatetimeSpaces(
635
+				$datetime_ID,
636
+				$ticket_identifier,
637
+				$ticket_quantity
638
+			);
639
+			// skip to next ticket if nothing changed
640
+			if (! ($adjusted || $at_capacity)) {
641
+				continue;
642
+			}
643
+			// then all of it's tickets are now unavailable
644
+			foreach ($datetime_tickets as $datetime_ticket) {
645
+				if (($ticket_identifier === $datetime_ticket || $at_capacity)
646
+					&& isset($this->ticket_quantities[ $datetime_ticket ])
647
+					&& $this->ticket_quantities[ $datetime_ticket ] > 0
648
+				) {
649
+					if ($this->debug) {
650
+						\EEH_Debug_Tools::printr(
651
+							$datetime_ticket,
652
+							' . . . adjust ticket quantities for',
653
+							__FILE__,
654
+							__LINE__
655
+						);
656
+					}
657
+					// if this datetime is at full capacity, set any tracked available quantities to zero
658
+					// otherwise just subtract the ticket quantity
659
+					$new_quantity = $at_capacity
660
+						? 0
661
+						: $this->ticket_quantities[ $datetime_ticket ] - $ticket_quantity;
662
+					// don't let ticket quantity go below zero
663
+					$this->ticket_quantities[ $datetime_ticket ] = max($new_quantity, 0);
664
+					if ($this->debug) {
665
+						\EEH_Debug_Tools::printr(
666
+							$at_capacity
667
+								? "0 because Datetime {$datetime_identifier} is at capacity"
668
+								: "{$this->ticket_quantities[ $datetime_ticket ]}",
669
+							" . . . . {$datetime_ticket} quantity set to ",
670
+							__FILE__,
671
+							__LINE__
672
+						);
673
+					}
674
+				}
675
+				// but we also need to adjust spaces for any other datetimes this ticket has access to
676
+				if ($datetime_ticket === $ticket_identifier) {
677
+					if (isset($this->ticket_datetimes[ $datetime_ticket ])
678
+						&& is_array($this->ticket_datetimes[ $datetime_ticket ])
679
+					) {
680
+						if ($this->debug) {
681
+							\EEH_Debug_Tools::printr(
682
+								$datetime_ticket,
683
+								' . . adjust other Datetimes for',
684
+								__FILE__,
685
+								__LINE__
686
+							);
687
+						}
688
+						foreach ($this->ticket_datetimes[ $datetime_ticket ] as $datetime) {
689
+							// don't adjust the current datetime twice
690
+							if ($datetime !== $datetime_identifier) {
691
+								$this->adjustDatetimeSpaces(
692
+									$datetime,
693
+									$datetime_ticket,
694
+									$ticket_quantity
695
+								);
696
+							}
697
+						}
698
+					}
699
+				}
700
+			}
701
+		}
702
+	}
703
+
704
+	private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0)
705
+	{
706
+		// does datetime have spaces available?
707
+		// and does the supplied ticket have access to this datetime ?
708
+		if ($this->datetime_spaces[ $datetime_identifier ] > 0
709
+			&& isset($this->datetime_spaces[ $datetime_identifier ], $this->datetime_tickets[ $datetime_identifier ])
710
+			&& in_array($ticket_identifier, $this->datetime_tickets[ $datetime_identifier ], true)
711
+		) {
712
+			if ($this->debug) {
713
+				\EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__);
714
+				\EEH_Debug_Tools::printr(
715
+					"{$this->datetime_spaces[ $datetime_identifier ]}",
716
+					" . . current  {$datetime_identifier} spaces available",
717
+					__FILE__,
718
+					__LINE__
719
+				);
720
+			}
721
+			// then decrement the available spaces for the datetime
722
+			$this->datetime_spaces[ $datetime_identifier ] -= $ticket_quantity;
723
+			// but don't let quantities go below zero
724
+			$this->datetime_spaces[ $datetime_identifier ] = max(
725
+				$this->datetime_spaces[ $datetime_identifier ],
726
+				0
727
+			);
728
+			if ($this->debug) {
729
+				\EEH_Debug_Tools::printr(
730
+					"{$ticket_quantity}",
731
+					" . . . {$datetime_identifier} capacity reduced by",
732
+					__FILE__,
733
+					__LINE__
734
+				);
735
+			}
736
+			return true;
737
+		}
738
+		return false;
739
+	}
740 740
 }
Please login to merge, or discard this patch.
core/services/database/TableManager.php 1 patch
Indentation   +231 added lines, -231 removed lines patch added patch discarded remove patch
@@ -13,254 +13,254 @@
 block discarded – undo
13 13
 class TableManager extends \EE_Base
14 14
 {
15 15
 
16
-    /**
17
-     * @var TableAnalysis $table_analysis
18
-     */
19
-    private $table_analysis;
16
+	/**
17
+	 * @var TableAnalysis $table_analysis
18
+	 */
19
+	private $table_analysis;
20 20
 
21 21
 
22
-    /**
23
-     * TableManager constructor.
24
-     *
25
-     * @param TableAnalysis $TableAnalysis
26
-     */
27
-    public function __construct(TableAnalysis $TableAnalysis)
28
-    {
29
-        $this->table_analysis = $TableAnalysis;
30
-    }
22
+	/**
23
+	 * TableManager constructor.
24
+	 *
25
+	 * @param TableAnalysis $TableAnalysis
26
+	 */
27
+	public function __construct(TableAnalysis $TableAnalysis)
28
+	{
29
+		$this->table_analysis = $TableAnalysis;
30
+	}
31 31
 
32 32
 
33
-    /**
34
-     * Gets the injected table analyzer, or throws an exception
35
-     *
36
-     * @return TableAnalysis
37
-     * @throws \EE_Error
38
-     */
39
-    protected function getTableAnalysis()
40
-    {
41
-        if ($this->table_analysis instanceof TableAnalysis) {
42
-            return $this->table_analysis;
43
-        } else {
44
-            throw new \EE_Error(
45
-                sprintf(
46
-                    __('Table analysis class on class %1$s is not set properly.', 'event_espresso'),
47
-                    get_class($this)
48
-                )
49
-            );
50
-        }
51
-    }
33
+	/**
34
+	 * Gets the injected table analyzer, or throws an exception
35
+	 *
36
+	 * @return TableAnalysis
37
+	 * @throws \EE_Error
38
+	 */
39
+	protected function getTableAnalysis()
40
+	{
41
+		if ($this->table_analysis instanceof TableAnalysis) {
42
+			return $this->table_analysis;
43
+		} else {
44
+			throw new \EE_Error(
45
+				sprintf(
46
+					__('Table analysis class on class %1$s is not set properly.', 'event_espresso'),
47
+					get_class($this)
48
+				)
49
+			);
50
+		}
51
+	}
52 52
 
53 53
 
54
-    /**
55
-     * @param string $table_name which can optionally start with $wpdb->prefix or not
56
-     * @param string $column_name
57
-     * @param string $column_info
58
-     * @return bool|false|int
59
-     */
60
-    public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL')
61
-    {
62
-        if (apply_filters('FHEE__EEH_Activation__add_column_if_it_doesnt_exist__short_circuit', false)) {
63
-            return false;
64
-        }
65
-        global $wpdb;
66
-        $full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
67
-        $columns = $this->getTableColumns($table_name);
68
-        if (! in_array($column_name, $columns)) {
69
-            $alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}";
70
-            return $wpdb->query($alter_query);
71
-        }
72
-        return true;
73
-    }
54
+	/**
55
+	 * @param string $table_name which can optionally start with $wpdb->prefix or not
56
+	 * @param string $column_name
57
+	 * @param string $column_info
58
+	 * @return bool|false|int
59
+	 */
60
+	public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL')
61
+	{
62
+		if (apply_filters('FHEE__EEH_Activation__add_column_if_it_doesnt_exist__short_circuit', false)) {
63
+			return false;
64
+		}
65
+		global $wpdb;
66
+		$full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
67
+		$columns = $this->getTableColumns($table_name);
68
+		if (! in_array($column_name, $columns)) {
69
+			$alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}";
70
+			return $wpdb->query($alter_query);
71
+		}
72
+		return true;
73
+	}
74 74
 
75 75
 
76
-    /**
77
-     * Gets the name of all columns on the  table. $table_name can
78
-     * optionally start with $wpdb->prefix or not
79
-     *
80
-     * @global \wpdb $wpdb
81
-     * @param string $table_name
82
-     * @return array
83
-     */
84
-    public function getTableColumns($table_name)
85
-    {
86
-        global $wpdb;
87
-        $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
88
-        $field_array = array();
89
-        if (! empty($table_name)) {
90
-            $columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} ");
91
-            if ($columns !== false) {
92
-                foreach ($columns as $column) {
93
-                    $field_array[] = $column->Field;
94
-                }
95
-            }
96
-        }
97
-        return $field_array;
98
-    }
76
+	/**
77
+	 * Gets the name of all columns on the  table. $table_name can
78
+	 * optionally start with $wpdb->prefix or not
79
+	 *
80
+	 * @global \wpdb $wpdb
81
+	 * @param string $table_name
82
+	 * @return array
83
+	 */
84
+	public function getTableColumns($table_name)
85
+	{
86
+		global $wpdb;
87
+		$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
88
+		$field_array = array();
89
+		if (! empty($table_name)) {
90
+			$columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} ");
91
+			if ($columns !== false) {
92
+				foreach ($columns as $column) {
93
+					$field_array[] = $column->Field;
94
+				}
95
+			}
96
+		}
97
+		return $field_array;
98
+	}
99 99
 
100 100
 
101
-    /**
102
-     * Drops the specified table from the database. $table_name can
103
-     * optionally start with $wpdb->prefix or not
104
-     *
105
-     * @global \wpdb $wpdb
106
-     * @param string $table_name
107
-     * @return int
108
-     */
109
-    public function dropTable($table_name)
110
-    {
111
-        global $wpdb;
112
-        if ($this->getTableAnalysis()->tableExists($table_name)) {
113
-            $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
114
-            return $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
115
-        }
116
-        return 0;
117
-    }
101
+	/**
102
+	 * Drops the specified table from the database. $table_name can
103
+	 * optionally start with $wpdb->prefix or not
104
+	 *
105
+	 * @global \wpdb $wpdb
106
+	 * @param string $table_name
107
+	 * @return int
108
+	 */
109
+	public function dropTable($table_name)
110
+	{
111
+		global $wpdb;
112
+		if ($this->getTableAnalysis()->tableExists($table_name)) {
113
+			$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
114
+			return $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
115
+		}
116
+		return 0;
117
+	}
118 118
 
119 119
 
120
-    /**
121
-     * Drops all the tables mentioned in a single MYSQL query. Double-checks
122
-     * each table name provided has a wpdb prefix attached, and that it exists.
123
-     * Returns the list actually deleted
124
-     *
125
-     * @global WPDB $wpdb
126
-     * @param array $table_names
127
-     * @return array of table names which we deleted
128
-     */
129
-    public function dropTables($table_names)
130
-    {
131
-        $tables_to_delete = array();
132
-        foreach ($table_names as $table_name) {
133
-            $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
134
-            if ($this->getTableAnalysis()->tableExists($table_name)) {
135
-                $tables_to_delete[ $table_name ] = $table_name;
136
-            }
137
-        }
138
-        if (! empty($tables_to_delete)) {
139
-            global $wpdb;
140
-            // make sure we only have a unique strings in the array.
141
-            $tables_to_delete = array_unique($tables_to_delete);
142
-            $wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete));
143
-        }
144
-        return $tables_to_delete;
145
-    }
120
+	/**
121
+	 * Drops all the tables mentioned in a single MYSQL query. Double-checks
122
+	 * each table name provided has a wpdb prefix attached, and that it exists.
123
+	 * Returns the list actually deleted
124
+	 *
125
+	 * @global WPDB $wpdb
126
+	 * @param array $table_names
127
+	 * @return array of table names which we deleted
128
+	 */
129
+	public function dropTables($table_names)
130
+	{
131
+		$tables_to_delete = array();
132
+		foreach ($table_names as $table_name) {
133
+			$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
134
+			if ($this->getTableAnalysis()->tableExists($table_name)) {
135
+				$tables_to_delete[ $table_name ] = $table_name;
136
+			}
137
+		}
138
+		if (! empty($tables_to_delete)) {
139
+			global $wpdb;
140
+			// make sure we only have a unique strings in the array.
141
+			$tables_to_delete = array_unique($tables_to_delete);
142
+			$wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete));
143
+		}
144
+		return $tables_to_delete;
145
+	}
146 146
 
147 147
 
148
-    /**
149
-     * Drops the specified index from the specified table. $table_name can
150
-     * optionally start with $wpdb->prefix or not
151
-     *
152
-     * @global \wpdb $wpdb
153
-     * @param string $table_name
154
-     * @param string $index_name
155
-     * @return int the number of indexes dropped. False if there was a datbase error
156
-     */
157
-    public function dropIndex($table_name, $index_name)
158
-    {
159
-        if (apply_filters('FHEE__EEH_Activation__drop_index__short_circuit', false)) {
160
-            return 0;
161
-        }
162
-        global $wpdb;
163
-        $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
164
-        $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE key_name = '{$index_name}'";
165
-        if ($this->getTableAnalysis()->tableExists($table_name)
166
-            && $wpdb->get_var($index_exists_query)
167
-               === $table_name // using get_var with the $index_exists_query returns the table's name
168
-        ) {
169
-            return $wpdb->query("ALTER TABLE {$table_name} DROP INDEX {$index_name}");
170
-        }
171
-        return 0;
172
-    }
148
+	/**
149
+	 * Drops the specified index from the specified table. $table_name can
150
+	 * optionally start with $wpdb->prefix or not
151
+	 *
152
+	 * @global \wpdb $wpdb
153
+	 * @param string $table_name
154
+	 * @param string $index_name
155
+	 * @return int the number of indexes dropped. False if there was a datbase error
156
+	 */
157
+	public function dropIndex($table_name, $index_name)
158
+	{
159
+		if (apply_filters('FHEE__EEH_Activation__drop_index__short_circuit', false)) {
160
+			return 0;
161
+		}
162
+		global $wpdb;
163
+		$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
164
+		$index_exists_query = "SHOW INDEX FROM {$table_name} WHERE key_name = '{$index_name}'";
165
+		if ($this->getTableAnalysis()->tableExists($table_name)
166
+			&& $wpdb->get_var($index_exists_query)
167
+			   === $table_name // using get_var with the $index_exists_query returns the table's name
168
+		) {
169
+			return $wpdb->query("ALTER TABLE {$table_name} DROP INDEX {$index_name}");
170
+		}
171
+		return 0;
172
+	}
173 173
 
174 174
 
175
-    /**
176
-     * Just creates the requested table. $table_name can
177
-     * optionally start with $wpdb->prefix or not
178
-     *
179
-     * @param string $table_name
180
-     * @param string $create_sql defining the table's columns and indexes
181
-     * @param string $engine     (no need to specify "ENGINE=", that's implied)
182
-     * @return void
183
-     * @throws \EE_Error
184
-     */
185
-    public function createTable($table_name, $create_sql, $engine = 'MyISAM')
186
-    {
187
-        $engine = apply_filters(
188
-            'FHEE__EventEspresso_core_services_database_TableManager__createTable__engine',
189
-            $engine,
190
-            $table_name,
191
-            $create_sql
192
-        );
193
-        // does $sql contain valid column information? ( LPT: https://regex101.com/ is great for working out regex patterns )
194
-        if (preg_match('((((.*?))(,\s))+)', $create_sql, $valid_column_data)) {
195
-            $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
196
-            /** @var \wpdb $wpdb */
197
-            global $wpdb;
198
-            $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate();
175
+	/**
176
+	 * Just creates the requested table. $table_name can
177
+	 * optionally start with $wpdb->prefix or not
178
+	 *
179
+	 * @param string $table_name
180
+	 * @param string $create_sql defining the table's columns and indexes
181
+	 * @param string $engine     (no need to specify "ENGINE=", that's implied)
182
+	 * @return void
183
+	 * @throws \EE_Error
184
+	 */
185
+	public function createTable($table_name, $create_sql, $engine = 'MyISAM')
186
+	{
187
+		$engine = apply_filters(
188
+			'FHEE__EventEspresso_core_services_database_TableManager__createTable__engine',
189
+			$engine,
190
+			$table_name,
191
+			$create_sql
192
+		);
193
+		// does $sql contain valid column information? ( LPT: https://regex101.com/ is great for working out regex patterns )
194
+		if (preg_match('((((.*?))(,\s))+)', $create_sql, $valid_column_data)) {
195
+			$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
196
+			/** @var \wpdb $wpdb */
197
+			global $wpdb;
198
+			$SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate();
199 199
 
200
-            // get $wpdb to echo errors, but buffer them. This way at least WE know an error
201
-            // happened. And then we can choose to tell the end user
202
-            $old_show_errors_policy = $wpdb->show_errors(true);
203
-            $old_error_suppression_policy = $wpdb->suppress_errors(false);
204
-            ob_start();
205
-            dbDelta($SQL);
206
-            $output = ob_get_contents();
207
-            ob_end_clean();
208
-            $wpdb->show_errors($old_show_errors_policy);
209
-            $wpdb->suppress_errors($old_error_suppression_policy);
210
-            if (! empty($output)) {
211
-                throw new \EE_Error($output);
212
-            }
213
-        } else {
214
-            throw new \EE_Error(
215
-                sprintf(
216
-                    __(
217
-                        'The following table creation SQL does not contain valid information about the table columns: %1$s %2$s',
218
-                        'event_espresso'
219
-                    ),
220
-                    '<br />',
221
-                    $create_sql
222
-                )
223
-            );
224
-        }
225
-    }
200
+			// get $wpdb to echo errors, but buffer them. This way at least WE know an error
201
+			// happened. And then we can choose to tell the end user
202
+			$old_show_errors_policy = $wpdb->show_errors(true);
203
+			$old_error_suppression_policy = $wpdb->suppress_errors(false);
204
+			ob_start();
205
+			dbDelta($SQL);
206
+			$output = ob_get_contents();
207
+			ob_end_clean();
208
+			$wpdb->show_errors($old_show_errors_policy);
209
+			$wpdb->suppress_errors($old_error_suppression_policy);
210
+			if (! empty($output)) {
211
+				throw new \EE_Error($output);
212
+			}
213
+		} else {
214
+			throw new \EE_Error(
215
+				sprintf(
216
+					__(
217
+						'The following table creation SQL does not contain valid information about the table columns: %1$s %2$s',
218
+						'event_espresso'
219
+					),
220
+					'<br />',
221
+					$create_sql
222
+				)
223
+			);
224
+		}
225
+	}
226 226
 
227 227
 
228
-    /**
229
-     * Drops the specified index if it's size differs from $desired_index_size.
230
-     * WordPress' dbdelta method doesn't automatically change index sizes, so this
231
-     * method can be used to only drop the index if needed, and afterwards dbdelta can be used as normal.
232
-     * If the table doesn't exist, or it exists but the index does not, or returns false
233
-     *
234
-     * @param string     $table_name
235
-     * @param string     $index_name
236
-     * @param string     $column_name        if none is provided, we assume the column name matches the index (often
237
-     *                                       true in EE)
238
-     * @param string|int $desired_index_size defaults to TableAnalysis::index_col_size, the max for utf8mb4.
239
-     * @return bool whether an index was dropped or not
240
-     * @throws /EE_Error if table analysis object isn't defined
241
-     */
242
-    public function dropIndexIfSizeNot(
243
-        $table_name,
244
-        $index_name,
245
-        $column_name = null,
246
-        $desired_index_size = TableAnalysis::INDEX_COLUMN_SIZE
247
-    ) {
248
-        if ($column_name === null) {
249
-            $column_name = $index_name;
250
-        }
251
-        if (! $this->getTableAnalysis()->tableExists($table_name)) {
252
-            return false;
253
-        }
254
-        $index_entries = $this->getTableAnalysis()->showIndexes($table_name, $index_name);
255
-        if (empty($index_entries)) {
256
-            return false;
257
-        }
258
-        foreach ($index_entries as $index_entry) {
259
-            if ($column_name === $index_entry->Column_name
260
-                && (string) $desired_index_size !== $index_entry->Sub_part) {
261
-                return $this->dropIndex($table_name, $index_name);
262
-            }
263
-        }
264
-        return false;
265
-    }
228
+	/**
229
+	 * Drops the specified index if it's size differs from $desired_index_size.
230
+	 * WordPress' dbdelta method doesn't automatically change index sizes, so this
231
+	 * method can be used to only drop the index if needed, and afterwards dbdelta can be used as normal.
232
+	 * If the table doesn't exist, or it exists but the index does not, or returns false
233
+	 *
234
+	 * @param string     $table_name
235
+	 * @param string     $index_name
236
+	 * @param string     $column_name        if none is provided, we assume the column name matches the index (often
237
+	 *                                       true in EE)
238
+	 * @param string|int $desired_index_size defaults to TableAnalysis::index_col_size, the max for utf8mb4.
239
+	 * @return bool whether an index was dropped or not
240
+	 * @throws /EE_Error if table analysis object isn't defined
241
+	 */
242
+	public function dropIndexIfSizeNot(
243
+		$table_name,
244
+		$index_name,
245
+		$column_name = null,
246
+		$desired_index_size = TableAnalysis::INDEX_COLUMN_SIZE
247
+	) {
248
+		if ($column_name === null) {
249
+			$column_name = $index_name;
250
+		}
251
+		if (! $this->getTableAnalysis()->tableExists($table_name)) {
252
+			return false;
253
+		}
254
+		$index_entries = $this->getTableAnalysis()->showIndexes($table_name, $index_name);
255
+		if (empty($index_entries)) {
256
+			return false;
257
+		}
258
+		foreach ($index_entries as $index_entry) {
259
+			if ($column_name === $index_entry->Column_name
260
+				&& (string) $desired_index_size !== $index_entry->Sub_part) {
261
+				return $this->dropIndex($table_name, $index_name);
262
+			}
263
+		}
264
+		return false;
265
+	}
266 266
 }
Please login to merge, or discard this patch.
caffeinated/payment_methods/Paypal_Pro/EEG_Paypal_Pro.gateway.php 1 patch
Indentation   +602 added lines, -602 removed lines patch added patch discarded remove patch
@@ -11,606 +11,606 @@
 block discarded – undo
11 11
 class EEG_Paypal_Pro extends EE_Onsite_Gateway
12 12
 {
13 13
 
14
-    /**
15
-     * @var $_paypal_api_username string
16
-     */
17
-    protected $_username = null;
18
-
19
-    /**
20
-     * @var $_password string
21
-     */
22
-    protected $_password = null;
23
-
24
-    /**
25
-     * @var $_signature string
26
-     */
27
-    protected $_signature = null;
28
-
29
-    /**
30
-     * @var $_credit_card_types array with the keys for credit card types accepted on this account
31
-     */
32
-    protected $_credit_card_types    = null;
33
-
34
-    protected $_currencies_supported = array(
35
-        'USD',
36
-        'GBP',
37
-        'CAD',
38
-        'AUD',
39
-        'BRL',
40
-        'CHF',
41
-        'CZK',
42
-        'DKK',
43
-        'EUR',
44
-        'HKD',
45
-        'HUF',
46
-        'ILS',
47
-        'JPY',
48
-        'MXN',
49
-        'MYR',
50
-        'NOK',
51
-        'NZD',
52
-        'PHP',
53
-        'PLN',
54
-        'SEK',
55
-        'SGD',
56
-        'THB',
57
-        'TRY',
58
-        'TWD',
59
-        'RUB',
60
-        'INR',
61
-    );
62
-
63
-
64
-
65
-    /**
66
-     * @param EEI_Payment $payment
67
-     * @param array       $billing_info {
68
-     * @type string $credit_card
69
-     * @type string $credit_card_type
70
-     * @type string $exp_month always 2 characters
71
-     * @type string $exp_year always 4 characters
72
-     * @type string $cvv
73
-     * }
74
-     * @see      parent::do_direct_payment for more info
75
-     * @return EE_Payment|EEI_Payment
76
-     * @throws EE_Error
77
-     */
78
-    public function do_direct_payment($payment, $billing_info = null)
79
-    {
80
-        $transaction = $payment->transaction();
81
-        if (! $transaction instanceof EEI_Transaction) {
82
-            throw new EE_Error(
83
-                esc_html__('No transaction for payment while paying with PayPal Pro.', 'event_espresso')
84
-            );
85
-        }
86
-        $primary_registrant = $transaction->primary_registration();
87
-        if (! $primary_registrant instanceof EEI_Registration) {
88
-            throw new EE_Error(
89
-                esc_html__(
90
-                    'No primary registration on transaction while paying with PayPal Pro.',
91
-                    'event_espresso'
92
-                )
93
-            );
94
-        }
95
-        $attendee = $primary_registrant->attendee();
96
-        if (! $attendee instanceof EEI_Attendee) {
97
-            throw new EE_Error(
98
-                esc_html__(
99
-                    'No attendee on primary registration while paying with PayPal Pro.',
100
-                    'event_espresso'
101
-                )
102
-            );
103
-        }
104
-        $gateway_formatter = $this->_get_gateway_formatter();
105
-        $order_description = substr($gateway_formatter->formatOrderDescription($payment), 0, 127);
106
-        // charge for the full amount. Show itemized list
107
-        if ($this->_money->compare_floats($payment->amount(), $transaction->total(), '==')) {
108
-            $item_num = 1;
109
-            $total_line_item = $transaction->total_line_item();
110
-            $order_items = array();
111
-            foreach ($total_line_item->get_items() as $line_item) {
112
-                // ignore line items with a quantity of 0
113
-                if ($line_item->quantity() == 0) {
114
-                    continue;
115
-                }
116
-                // For percent items, whose unit_price is 0, use the total instead.
117
-                if ($line_item->is_percent()) {
118
-                    $unit_price = $line_item->total();
119
-                    $line_item_quantity = 1;
120
-                } else {
121
-                    $unit_price = $line_item->unit_price();
122
-                    $line_item_quantity = $line_item->quantity();
123
-                }
124
-                $item = array(
125
-                    // Item Name.  127 char max.
126
-                    'l_name'                 => substr(
127
-                        $gateway_formatter->formatLineItemName($line_item, $payment),
128
-                        0,
129
-                        127
130
-                    ),
131
-                    // Item description.  127 char max.
132
-                    'l_desc'                 => substr(
133
-                        $gateway_formatter->formatLineItemDesc($line_item, $payment),
134
-                        0,
135
-                        127
136
-                    ),
137
-                    // Cost of individual item.
138
-                    'l_amt'                  => $unit_price,
139
-                    // Item Number.  127 char max.
140
-                    'l_number'               => $item_num++,
141
-                    // Item quantity.  Must be any positive integer.
142
-                    'l_qty'                  => $line_item_quantity,
143
-                    // Item's sales tax amount.
144
-                    'l_taxamt'               => '',
145
-                    // eBay auction number of item.
146
-                    'l_ebayitemnumber'       => '',
147
-                    // eBay transaction ID of purchased item.
148
-                    'l_ebayitemauctiontxnid' => '',
149
-                    // eBay order ID for the item.
150
-                    'l_ebayitemorderid'      => '',
151
-                );
152
-                // add to array of all items
153
-                array_push($order_items, $item);
154
-            }
155
-            $item_amount = $total_line_item->get_items_total();
156
-            $tax_amount = $total_line_item->get_total_tax();
157
-        } else {
158
-            $order_items = array();
159
-            $item_amount = $payment->amount();
160
-            $tax_amount = 0;
161
-            array_push($order_items, array(
162
-                // Item Name.  127 char max.
163
-                'l_name'   => substr(
164
-                    $gateway_formatter->formatPartialPaymentLineItemName($payment),
165
-                    0,
166
-                    127
167
-                ),
168
-                // Item description.  127 char max.
169
-                'l_desc'   => substr(
170
-                    $gateway_formatter->formatPartialPaymentLineItemDesc($payment),
171
-                    0,
172
-                    127
173
-                ),
174
-                // Cost of individual item.
175
-                'l_amt'    => $payment->amount(),
176
-                // Item Number.  127 char max.
177
-                'l_number' => 1,
178
-                // Item quantity.  Must be any positive integer.
179
-                'l_qty'    => 1,
180
-            ));
181
-        }
182
-        // Populate data arrays with order data.
183
-        $DPFields = array(
184
-            // How you want to obtain payment ?
185
-            // Authorization indicates the payment is a basic auth subject to settlement with Auth & Capture.
186
-            // Sale indicates that this is a final sale for which you are requesting payment.  Default is Sale.
187
-            'paymentaction'    => 'Sale',
188
-            // Required.  IP address of the payer's browser.
189
-            'ipaddress'        => $_SERVER['REMOTE_ADDR'],
190
-            // Flag to determine whether you want the results returned by FMF.  1 or 0.  Default is 0.
191
-            'returnfmfdetails' => '1',
192
-        );
193
-        $CCDetails = array(
194
-            // Required. Type of credit card.  Visa, MasterCard, Discover, Amex, Maestro, Solo.
195
-            // If Maestro or Solo, the currency code must be GBP.
196
-            //  In addition, either start date or issue number must be specified.
197
-            'creditcardtype' => $billing_info['credit_card_type'],
198
-            // Required.  Credit card number.  No spaces or punctuation.
199
-            'acct'           => $billing_info['credit_card'],
200
-            // Required.  Credit card expiration date.  Format is MMYYYY
201
-            'expdate'        => $billing_info['exp_month'] . $billing_info['exp_year'],
202
-            // Requirements determined by your PayPal account settings.  Security digits for credit card.
203
-            'cvv2'           => $billing_info['cvv'],
204
-        );
205
-        $PayerInfo = array(
206
-            // Email address of payer.
207
-            'email'       => $billing_info['email'],
208
-            // Unique PayPal customer ID for payer.
209
-            'payerid'     => '',
210
-            // Status of payer.  Values are verified or unverified
211
-            'payerstatus' => '',
212
-            // Payer's business name.
213
-            'business'    => '',
214
-        );
215
-        $PayerName = array(
216
-            // Payer's salutation.  20 char max.
217
-            'salutation' => '',
218
-            // Payer's first name.  25 char max.
219
-            'firstname'  => substr($billing_info['first_name'], 0, 25),
220
-            // Payer's middle name.  25 char max.
221
-            'middlename' => '',
222
-            // Payer's last name.  25 char max.
223
-            'lastname'   => substr($billing_info['last_name'], 0, 25),
224
-            // Payer's suffix.  12 char max.
225
-            'suffix'     => '',
226
-        );
227
-        $BillingAddress = array(
228
-            // Required.  First street address.
229
-            'street'      => $billing_info['address'],
230
-            // Second street address.
231
-            'street2'     => $billing_info['address2'],
232
-            // Required.  Name of City.
233
-            'city'        => $billing_info['city'],
234
-            // Required. Name of State or Province.
235
-            'state'       => substr($billing_info['state'], 0, 40),
236
-            // Required.  Country code.
237
-            'countrycode' => $billing_info['country'],
238
-            // Required.  Postal code of payer.
239
-            'zip'         => $billing_info['zip'],
240
-        );
241
-        // check if the registration info contains the needed fields for paypal pro
242
-        // (see https://developer.paypal.com/docs/classic/api/merchant/DoDirectPayment_API_Operation_NVP/)
243
-        if ($attendee->address() && $attendee->city() && $attendee->country_ID()) {
244
-            $use_registration_address_info = true;
245
-        } else {
246
-            $use_registration_address_info = false;
247
-        }
248
-        // so if the attendee has enough data to fill out PayPal Pro's shipping info, use it.
249
-        // If not, use the billing info again
250
-        $ShippingAddress = array(
251
-            'shiptoname'     => substr($use_registration_address_info
252
-                ? $attendee->full_name()
253
-                : $billing_info['first_name'] . ' ' . $billing_info['last_name'], 0, 32),
254
-            'shiptostreet'   => substr($use_registration_address_info
255
-                ? $attendee->address()
256
-                : $billing_info['address'], 0, 100),
257
-            'shiptostreet2'  => substr($use_registration_address_info
258
-                ? $attendee->address2() : $billing_info['address2'], 0, 100),
259
-            'shiptocity'     => substr($use_registration_address_info
260
-                ? $attendee->city()
261
-                : $billing_info['city'], 0, 40),
262
-            'state'          => substr($use_registration_address_info
263
-                ? $attendee->state_name()
264
-                : $billing_info['state'], 0, 40),
265
-            'shiptocountry'  => $use_registration_address_info
266
-                ? $attendee->country_ID()
267
-                : $billing_info['country'],
268
-            'shiptozip'      => substr($use_registration_address_info
269
-                ? $attendee->zip()
270
-                : $billing_info['zip'], 0, 20),
271
-            'shiptophonenum' => substr($use_registration_address_info
272
-                ? $attendee->phone()
273
-                : $billing_info['phone'], 0, 20),
274
-        );
275
-        $PaymentDetails = array(
276
-            // Required.  Total amount of order, including shipping, handling, and tax.
277
-            'amt'          => $gateway_formatter->formatCurrency($payment->amount()),
278
-            // Required.  Three-letter currency code.  Default is USD.
279
-            'currencycode' => $payment->currency_code(),
280
-            // Required if you include itemized cart details. (L_AMTn, etc.)
281
-            // Subtotal of items not including S&H, or tax.
282
-            'itemamt'      => $gateway_formatter->formatCurrency($item_amount),//
283
-            // Total shipping costs for the order.  If you specify shippingamt, you must also specify itemamt.
284
-            'shippingamt'  => '',
285
-            // Total handling costs for the order.  If you specify handlingamt, you must also specify itemamt.
286
-            'handlingamt'  => '',
287
-            // Required if you specify itemized cart tax details.
288
-            // Sum of tax for all items on the order.  Total sales tax.
289
-            'taxamt'       => $gateway_formatter->formatCurrency($tax_amount),
290
-            // Description of the order the customer is purchasing.  127 char max.
291
-            'desc'         => $order_description,
292
-            // Free-form field for your own use.  256 char max.
293
-            'custom'       => $primary_registrant ? $primary_registrant->ID() : '',
294
-            // Your own invoice or tracking number
295
-            'invnum'       => wp_generate_password(12, false),// $transaction->ID(),
296
-            // URL for receiving Instant Payment Notifications.  This overrides what your profile is set to use.
297
-            'notifyurl'    => '',
298
-            'buttonsource' => 'EventEspresso_SP',// EE will blow up if you change this
299
-        );
300
-        // Wrap all data arrays into a single, "master" array which will be passed into the class function.
301
-        $PayPalRequestData = array(
302
-            'DPFields'        => $DPFields,
303
-            'CCDetails'       => $CCDetails,
304
-            'PayerInfo'       => $PayerInfo,
305
-            'PayerName'       => $PayerName,
306
-            'BillingAddress'  => $BillingAddress,
307
-            'ShippingAddress' => $ShippingAddress,
308
-            'PaymentDetails'  => $PaymentDetails,
309
-            'OrderItems'      => $order_items,
310
-        );
311
-        $this->_log_clean_request($PayPalRequestData, $payment);
312
-        try {
313
-            $PayPalResult = $this->prep_and_curl_request($PayPalRequestData);
314
-            // remove PCI-sensitive data so it doesn't get stored
315
-            $PayPalResult = $this->_log_clean_response($PayPalResult, $payment);
316
-            if (isset($PayPalResult['L_ERRORCODE0']) && $PayPalResult['L_ERRORCODE0'] === '10002') {
317
-                $message = esc_html__('PayPal did not accept your API username, password, or signature. Please double-check these credentials and if debug mode is on.', 'event_espresso');
318
-            } elseif (isset($PayPalResult['L_LONGMESSAGE0'])) {
319
-                $message = $PayPalResult['L_LONGMESSAGE0'];
320
-            } else {
321
-                $message = $PayPalResult['ACK'];
322
-            }
323
-            if (empty($PayPalResult['RAWRESPONSE'])) {
324
-                $payment->set_status($this->_pay_model->failed_status());
325
-                $payment->set_gateway_response(__('No response received from Paypal Pro', 'event_espresso'));
326
-                $payment->set_details($PayPalResult);
327
-            } else {
328
-                if ($this->_APICallSuccessful($PayPalResult)) {
329
-                    $payment->set_status($this->_pay_model->approved_status());
330
-                } else {
331
-                    $payment->set_status($this->_pay_model->declined_status());
332
-                }
333
-                // make sure we interpret the AMT as a float, not an international string
334
-                // (where periods are thousand separators)
335
-                $payment->set_amount(isset($PayPalResult['AMT']) ? floatval($PayPalResult['AMT']) : 0);
336
-                $payment->set_gateway_response($message);
337
-                $payment->set_txn_id_chq_nmbr(isset($PayPalResult['TRANSACTIONID'])
338
-                    ? $PayPalResult['TRANSACTIONID']
339
-                    : null);
340
-                $primary_registration_code = $primary_registrant instanceof EE_Registration
341
-                    ? $primary_registrant->reg_code()
342
-                    : '';
343
-                $payment->set_extra_accntng($primary_registration_code);
344
-                $payment->set_details($PayPalResult);
345
-            }
346
-        } catch (Exception $e) {
347
-            $payment->set_status($this->_pay_model->failed_status());
348
-            $payment->set_gateway_response($e->getMessage());
349
-        }
350
-        // $payment->set_status( $this->_pay_model->declined_status() );
351
-        // $payment->set_gateway_response( '' );
352
-        return $payment;
353
-    }
354
-
355
-
356
-
357
-    /**
358
-     * CLeans out sensitive CC data and then logs it, and returns the cleaned request
359
-     *
360
-     * @param array       $request
361
-     * @param EEI_Payment $payment
362
-     * @return void
363
-     */
364
-    private function _log_clean_request($request, $payment)
365
-    {
366
-        $cleaned_request_data = $request;
367
-        unset($cleaned_request_data['CCDetails']['acct']);
368
-        unset($cleaned_request_data['CCDetails']['cvv2']);
369
-        unset($cleaned_request_data['CCDetails']['expdate']);
370
-        $this->log(array('Paypal Request' => $cleaned_request_data), $payment);
371
-    }
372
-
373
-
374
-
375
-    /**
376
-     * Cleans the response, logs it, and returns it
377
-     *
378
-     * @param array       $response
379
-     * @param EEI_Payment $payment
380
-     * @return array cleaned
381
-     */
382
-    private function _log_clean_response($response, $payment)
383
-    {
384
-        unset($response['REQUESTDATA']['CREDITCARDTYPE']);
385
-        unset($response['REQUESTDATA']['ACCT']);
386
-        unset($response['REQUESTDATA']['EXPDATE']);
387
-        unset($response['REQUESTDATA']['CVV2']);
388
-        unset($response['RAWREQUEST']);
389
-        $this->log(array('Paypal Response' => $response), $payment);
390
-        return $response;
391
-    }
392
-
393
-
394
-
395
-    /**
396
-     * @param $DataArray
397
-     * @return array
398
-     */
399
-    private function prep_and_curl_request($DataArray)
400
-    {
401
-        // Create empty holders for each portion of the NVP string
402
-        $DPFieldsNVP = '&METHOD=DoDirectPayment&BUTTONSOURCE=AngellEYE_PHP_Class_DDP';
403
-        $CCDetailsNVP = '';
404
-        $PayerInfoNVP = '';
405
-        $PayerNameNVP = '';
406
-        $BillingAddressNVP = '';
407
-        $ShippingAddressNVP = '';
408
-        $PaymentDetailsNVP = '';
409
-        $OrderItemsNVP = '';
410
-        $Secure3DNVP = '';
411
-        // DP Fields
412
-        $DPFields = isset($DataArray['DPFields']) ? $DataArray['DPFields'] : array();
413
-        foreach ($DPFields as $DPFieldsVar => $DPFieldsVal) {
414
-            $DPFieldsNVP .= '&' . strtoupper($DPFieldsVar) . '=' . urlencode($DPFieldsVal);
415
-        }
416
-        // CC Details Fields
417
-        $CCDetails = isset($DataArray['CCDetails']) ? $DataArray['CCDetails'] : array();
418
-        foreach ($CCDetails as $CCDetailsVar => $CCDetailsVal) {
419
-            $CCDetailsNVP .= '&' . strtoupper($CCDetailsVar) . '=' . urlencode($CCDetailsVal);
420
-        }
421
-        // PayerInfo Type Fields
422
-        $PayerInfo = isset($DataArray['PayerInfo']) ? $DataArray['PayerInfo'] : array();
423
-        foreach ($PayerInfo as $PayerInfoVar => $PayerInfoVal) {
424
-            $PayerInfoNVP .= '&' . strtoupper($PayerInfoVar) . '=' . urlencode($PayerInfoVal);
425
-        }
426
-        // Payer Name Fields
427
-        $PayerName = isset($DataArray['PayerName']) ? $DataArray['PayerName'] : array();
428
-        foreach ($PayerName as $PayerNameVar => $PayerNameVal) {
429
-            $PayerNameNVP .= '&' . strtoupper($PayerNameVar) . '=' . urlencode($PayerNameVal);
430
-        }
431
-        // Address Fields (Billing)
432
-        $BillingAddress = isset($DataArray['BillingAddress']) ? $DataArray['BillingAddress'] : array();
433
-        foreach ($BillingAddress as $BillingAddressVar => $BillingAddressVal) {
434
-            $BillingAddressNVP .= '&' . strtoupper($BillingAddressVar) . '=' . urlencode($BillingAddressVal);
435
-        }
436
-        // Payment Details Type Fields
437
-        $PaymentDetails = isset($DataArray['PaymentDetails']) ? $DataArray['PaymentDetails'] : array();
438
-        foreach ($PaymentDetails as $PaymentDetailsVar => $PaymentDetailsVal) {
439
-            $PaymentDetailsNVP .= '&' . strtoupper($PaymentDetailsVar) . '=' . urlencode($PaymentDetailsVal);
440
-        }
441
-        // Payment Details Item Type Fields
442
-        $OrderItems = isset($DataArray['OrderItems']) ? $DataArray['OrderItems'] : array();
443
-        $n = 0;
444
-        foreach ($OrderItems as $OrderItemsVar => $OrderItemsVal) {
445
-            $CurrentItem = $OrderItems[ $OrderItemsVar ];
446
-            foreach ($CurrentItem as $CurrentItemVar => $CurrentItemVal) {
447
-                $OrderItemsNVP .= '&' . strtoupper($CurrentItemVar) . $n . '=' . urlencode($CurrentItemVal);
448
-            }
449
-            $n++;
450
-        }
451
-        // Ship To Address Fields
452
-        $ShippingAddress = isset($DataArray['ShippingAddress']) ? $DataArray['ShippingAddress'] : array();
453
-        foreach ($ShippingAddress as $ShippingAddressVar => $ShippingAddressVal) {
454
-            $ShippingAddressNVP .= '&' . strtoupper($ShippingAddressVar) . '=' . urlencode($ShippingAddressVal);
455
-        }
456
-        // 3D Secure Fields
457
-        $Secure3D = isset($DataArray['Secure3D']) ? $DataArray['Secure3D'] : array();
458
-        foreach ($Secure3D as $Secure3DVar => $Secure3DVal) {
459
-            $Secure3DNVP .= '&' . strtoupper($Secure3DVar) . '=' . urlencode($Secure3DVal);
460
-        }
461
-        // Now that we have each chunk we need to go ahead and append them all together for our entire NVP string
462
-        $NVPRequest = 'USER='
463
-                      . $this->_username
464
-                      . '&PWD='
465
-                      . $this->_password
466
-                      . '&VERSION=64.0'
467
-                      . '&SIGNATURE='
468
-                      . $this->_signature
469
-                      . $DPFieldsNVP
470
-                      . $CCDetailsNVP
471
-                      . $PayerInfoNVP
472
-                      . $PayerNameNVP
473
-                      . $BillingAddressNVP
474
-                      . $PaymentDetailsNVP
475
-                      . $OrderItemsNVP
476
-                      . $ShippingAddressNVP
477
-                      . $Secure3DNVP;
478
-        $NVPResponse = $this->_CURLRequest($NVPRequest);
479
-        $NVPRequestArray = $this->_NVPToArray($NVPRequest);
480
-        $NVPResponseArray = $this->_NVPToArray($NVPResponse);
481
-        $Errors = $this->_GetErrors($NVPResponseArray);
482
-        $NVPResponseArray['ERRORS'] = $Errors;
483
-        $NVPResponseArray['REQUESTDATA'] = $NVPRequestArray;
484
-        $NVPResponseArray['RAWREQUEST'] = $NVPRequest;
485
-        $NVPResponseArray['RAWRESPONSE'] = $NVPResponse;
486
-        return $NVPResponseArray;
487
-    }
488
-
489
-
490
-
491
-    /**
492
-     * @param $Request
493
-     * @return mixed
494
-     */
495
-    private function _CURLRequest($Request)
496
-    {
497
-        $EndPointURL = $this->_debug_mode ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp';
498
-        $curl = curl_init();
499
-        curl_setopt($curl, CURLOPT_VERBOSE, apply_filters('FHEE__EEG_Paypal_Pro__CurlRequest__CURLOPT_VERBOSE', true));
500
-        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
501
-        curl_setopt($curl, CURLOPT_TIMEOUT, 60);
502
-        curl_setopt($curl, CURLOPT_URL, $EndPointURL);
503
-        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
504
-        curl_setopt($curl, CURLOPT_POSTFIELDS, $Request);
505
-        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
506
-        // execute the curl POST
507
-        $Response = curl_exec($curl);
508
-        curl_close($curl);
509
-        return $Response;
510
-    }
511
-
512
-
513
-
514
-    /**
515
-     * @param $NVPString
516
-     * @return array
517
-     */
518
-    private function _NVPToArray($NVPString)
519
-    {
520
-        // prepare responses into array
521
-        $proArray = array();
522
-        while (strlen($NVPString)) {
523
-            // name
524
-            $keypos = strpos($NVPString, '=');
525
-            $keyval = substr($NVPString, 0, $keypos);
526
-            // value
527
-            $valuepos = strpos($NVPString, '&') ? strpos($NVPString, '&') : strlen($NVPString);
528
-            $valval = substr($NVPString, $keypos + 1, $valuepos - $keypos - 1);
529
-            // decoding the response
530
-            $proArray[ $keyval ] = urldecode($valval);
531
-            $NVPString = substr($NVPString, $valuepos + 1, strlen($NVPString));
532
-        }
533
-        return $proArray;
534
-    }
535
-
536
-
537
-
538
-    /**
539
-     * @param array $PayPalResult
540
-     * @return bool
541
-     */
542
-    private function _APICallSuccessful($PayPalResult)
543
-    {
544
-        $approved = false;
545
-        // check main response message from PayPal
546
-        if (isset($PayPalResult['ACK']) && ! empty($PayPalResult['ACK'])) {
547
-            $ack = strtoupper($PayPalResult['ACK']);
548
-            $approved = ($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING' || $ack == 'PARTIALSUCCESS') ? true : false;
549
-        }
550
-        return $approved;
551
-    }
552
-
553
-
554
-
555
-    /**
556
-     * @param $DataArray
557
-     * @return array
558
-     */
559
-    private function _GetErrors($DataArray)
560
-    {
561
-        $Errors = array();
562
-        $n = 0;
563
-        while (isset($DataArray[ 'L_ERRORCODE' . $n . '' ])) {
564
-            $LErrorCode = isset($DataArray[ 'L_ERRORCODE' . $n . '' ]) ? $DataArray[ 'L_ERRORCODE' . $n . '' ] : '';
565
-            $LShortMessage = isset($DataArray[ 'L_SHORTMESSAGE' . $n . '' ])
566
-                ? $DataArray[ 'L_SHORTMESSAGE' . $n . '' ]
567
-                : '';
568
-            $LLongMessage = isset($DataArray[ 'L_LONGMESSAGE' . $n . '' ])
569
-                ? $DataArray[ 'L_LONGMESSAGE' . $n . '' ]
570
-                : '';
571
-            $LSeverityCode = isset($DataArray[ 'L_SEVERITYCODE' . $n . '' ])
572
-                ? $DataArray[ 'L_SEVERITYCODE' . $n . '' ]
573
-                : '';
574
-            $CurrentItem = array(
575
-                'L_ERRORCODE'    => $LErrorCode,
576
-                'L_SHORTMESSAGE' => $LShortMessage,
577
-                'L_LONGMESSAGE'  => $LLongMessage,
578
-                'L_SEVERITYCODE' => $LSeverityCode,
579
-            );
580
-            array_push($Errors, $CurrentItem);
581
-            $n++;
582
-        }
583
-        return $Errors;
584
-    }
585
-
586
-
587
-
588
-    /**
589
-     *        nothing to see here...  move along....
590
-     *
591
-     * @access protected
592
-     * @param $Errors
593
-     * @return string
594
-     */
595
-    private function _DisplayErrors($Errors)
596
-    {
597
-        $error = '';
598
-        foreach ($Errors as $ErrorVar => $ErrorVal) {
599
-            $CurrentError = $Errors[ $ErrorVar ];
600
-            foreach ($CurrentError as $CurrentErrorVar => $CurrentErrorVal) {
601
-                $CurrentVarName = '';
602
-                if ($CurrentErrorVar == 'L_ERRORCODE') {
603
-                    $CurrentVarName = 'Error Code';
604
-                } elseif ($CurrentErrorVar == 'L_SHORTMESSAGE') {
605
-                    $CurrentVarName = 'Short Message';
606
-                } elseif ($CurrentErrorVar == 'L_LONGMESSAGE') {
607
-                    $CurrentVarName = 'Long Message';
608
-                } elseif ($CurrentErrorVar == 'L_SEVERITYCODE') {
609
-                    $CurrentVarName = 'Severity Code';
610
-                }
611
-                $error .= '<br />' . $CurrentVarName . ': ' . $CurrentErrorVal;
612
-            }
613
-        }
614
-        return $error;
615
-    }
14
+	/**
15
+	 * @var $_paypal_api_username string
16
+	 */
17
+	protected $_username = null;
18
+
19
+	/**
20
+	 * @var $_password string
21
+	 */
22
+	protected $_password = null;
23
+
24
+	/**
25
+	 * @var $_signature string
26
+	 */
27
+	protected $_signature = null;
28
+
29
+	/**
30
+	 * @var $_credit_card_types array with the keys for credit card types accepted on this account
31
+	 */
32
+	protected $_credit_card_types    = null;
33
+
34
+	protected $_currencies_supported = array(
35
+		'USD',
36
+		'GBP',
37
+		'CAD',
38
+		'AUD',
39
+		'BRL',
40
+		'CHF',
41
+		'CZK',
42
+		'DKK',
43
+		'EUR',
44
+		'HKD',
45
+		'HUF',
46
+		'ILS',
47
+		'JPY',
48
+		'MXN',
49
+		'MYR',
50
+		'NOK',
51
+		'NZD',
52
+		'PHP',
53
+		'PLN',
54
+		'SEK',
55
+		'SGD',
56
+		'THB',
57
+		'TRY',
58
+		'TWD',
59
+		'RUB',
60
+		'INR',
61
+	);
62
+
63
+
64
+
65
+	/**
66
+	 * @param EEI_Payment $payment
67
+	 * @param array       $billing_info {
68
+	 * @type string $credit_card
69
+	 * @type string $credit_card_type
70
+	 * @type string $exp_month always 2 characters
71
+	 * @type string $exp_year always 4 characters
72
+	 * @type string $cvv
73
+	 * }
74
+	 * @see      parent::do_direct_payment for more info
75
+	 * @return EE_Payment|EEI_Payment
76
+	 * @throws EE_Error
77
+	 */
78
+	public function do_direct_payment($payment, $billing_info = null)
79
+	{
80
+		$transaction = $payment->transaction();
81
+		if (! $transaction instanceof EEI_Transaction) {
82
+			throw new EE_Error(
83
+				esc_html__('No transaction for payment while paying with PayPal Pro.', 'event_espresso')
84
+			);
85
+		}
86
+		$primary_registrant = $transaction->primary_registration();
87
+		if (! $primary_registrant instanceof EEI_Registration) {
88
+			throw new EE_Error(
89
+				esc_html__(
90
+					'No primary registration on transaction while paying with PayPal Pro.',
91
+					'event_espresso'
92
+				)
93
+			);
94
+		}
95
+		$attendee = $primary_registrant->attendee();
96
+		if (! $attendee instanceof EEI_Attendee) {
97
+			throw new EE_Error(
98
+				esc_html__(
99
+					'No attendee on primary registration while paying with PayPal Pro.',
100
+					'event_espresso'
101
+				)
102
+			);
103
+		}
104
+		$gateway_formatter = $this->_get_gateway_formatter();
105
+		$order_description = substr($gateway_formatter->formatOrderDescription($payment), 0, 127);
106
+		// charge for the full amount. Show itemized list
107
+		if ($this->_money->compare_floats($payment->amount(), $transaction->total(), '==')) {
108
+			$item_num = 1;
109
+			$total_line_item = $transaction->total_line_item();
110
+			$order_items = array();
111
+			foreach ($total_line_item->get_items() as $line_item) {
112
+				// ignore line items with a quantity of 0
113
+				if ($line_item->quantity() == 0) {
114
+					continue;
115
+				}
116
+				// For percent items, whose unit_price is 0, use the total instead.
117
+				if ($line_item->is_percent()) {
118
+					$unit_price = $line_item->total();
119
+					$line_item_quantity = 1;
120
+				} else {
121
+					$unit_price = $line_item->unit_price();
122
+					$line_item_quantity = $line_item->quantity();
123
+				}
124
+				$item = array(
125
+					// Item Name.  127 char max.
126
+					'l_name'                 => substr(
127
+						$gateway_formatter->formatLineItemName($line_item, $payment),
128
+						0,
129
+						127
130
+					),
131
+					// Item description.  127 char max.
132
+					'l_desc'                 => substr(
133
+						$gateway_formatter->formatLineItemDesc($line_item, $payment),
134
+						0,
135
+						127
136
+					),
137
+					// Cost of individual item.
138
+					'l_amt'                  => $unit_price,
139
+					// Item Number.  127 char max.
140
+					'l_number'               => $item_num++,
141
+					// Item quantity.  Must be any positive integer.
142
+					'l_qty'                  => $line_item_quantity,
143
+					// Item's sales tax amount.
144
+					'l_taxamt'               => '',
145
+					// eBay auction number of item.
146
+					'l_ebayitemnumber'       => '',
147
+					// eBay transaction ID of purchased item.
148
+					'l_ebayitemauctiontxnid' => '',
149
+					// eBay order ID for the item.
150
+					'l_ebayitemorderid'      => '',
151
+				);
152
+				// add to array of all items
153
+				array_push($order_items, $item);
154
+			}
155
+			$item_amount = $total_line_item->get_items_total();
156
+			$tax_amount = $total_line_item->get_total_tax();
157
+		} else {
158
+			$order_items = array();
159
+			$item_amount = $payment->amount();
160
+			$tax_amount = 0;
161
+			array_push($order_items, array(
162
+				// Item Name.  127 char max.
163
+				'l_name'   => substr(
164
+					$gateway_formatter->formatPartialPaymentLineItemName($payment),
165
+					0,
166
+					127
167
+				),
168
+				// Item description.  127 char max.
169
+				'l_desc'   => substr(
170
+					$gateway_formatter->formatPartialPaymentLineItemDesc($payment),
171
+					0,
172
+					127
173
+				),
174
+				// Cost of individual item.
175
+				'l_amt'    => $payment->amount(),
176
+				// Item Number.  127 char max.
177
+				'l_number' => 1,
178
+				// Item quantity.  Must be any positive integer.
179
+				'l_qty'    => 1,
180
+			));
181
+		}
182
+		// Populate data arrays with order data.
183
+		$DPFields = array(
184
+			// How you want to obtain payment ?
185
+			// Authorization indicates the payment is a basic auth subject to settlement with Auth & Capture.
186
+			// Sale indicates that this is a final sale for which you are requesting payment.  Default is Sale.
187
+			'paymentaction'    => 'Sale',
188
+			// Required.  IP address of the payer's browser.
189
+			'ipaddress'        => $_SERVER['REMOTE_ADDR'],
190
+			// Flag to determine whether you want the results returned by FMF.  1 or 0.  Default is 0.
191
+			'returnfmfdetails' => '1',
192
+		);
193
+		$CCDetails = array(
194
+			// Required. Type of credit card.  Visa, MasterCard, Discover, Amex, Maestro, Solo.
195
+			// If Maestro or Solo, the currency code must be GBP.
196
+			//  In addition, either start date or issue number must be specified.
197
+			'creditcardtype' => $billing_info['credit_card_type'],
198
+			// Required.  Credit card number.  No spaces or punctuation.
199
+			'acct'           => $billing_info['credit_card'],
200
+			// Required.  Credit card expiration date.  Format is MMYYYY
201
+			'expdate'        => $billing_info['exp_month'] . $billing_info['exp_year'],
202
+			// Requirements determined by your PayPal account settings.  Security digits for credit card.
203
+			'cvv2'           => $billing_info['cvv'],
204
+		);
205
+		$PayerInfo = array(
206
+			// Email address of payer.
207
+			'email'       => $billing_info['email'],
208
+			// Unique PayPal customer ID for payer.
209
+			'payerid'     => '',
210
+			// Status of payer.  Values are verified or unverified
211
+			'payerstatus' => '',
212
+			// Payer's business name.
213
+			'business'    => '',
214
+		);
215
+		$PayerName = array(
216
+			// Payer's salutation.  20 char max.
217
+			'salutation' => '',
218
+			// Payer's first name.  25 char max.
219
+			'firstname'  => substr($billing_info['first_name'], 0, 25),
220
+			// Payer's middle name.  25 char max.
221
+			'middlename' => '',
222
+			// Payer's last name.  25 char max.
223
+			'lastname'   => substr($billing_info['last_name'], 0, 25),
224
+			// Payer's suffix.  12 char max.
225
+			'suffix'     => '',
226
+		);
227
+		$BillingAddress = array(
228
+			// Required.  First street address.
229
+			'street'      => $billing_info['address'],
230
+			// Second street address.
231
+			'street2'     => $billing_info['address2'],
232
+			// Required.  Name of City.
233
+			'city'        => $billing_info['city'],
234
+			// Required. Name of State or Province.
235
+			'state'       => substr($billing_info['state'], 0, 40),
236
+			// Required.  Country code.
237
+			'countrycode' => $billing_info['country'],
238
+			// Required.  Postal code of payer.
239
+			'zip'         => $billing_info['zip'],
240
+		);
241
+		// check if the registration info contains the needed fields for paypal pro
242
+		// (see https://developer.paypal.com/docs/classic/api/merchant/DoDirectPayment_API_Operation_NVP/)
243
+		if ($attendee->address() && $attendee->city() && $attendee->country_ID()) {
244
+			$use_registration_address_info = true;
245
+		} else {
246
+			$use_registration_address_info = false;
247
+		}
248
+		// so if the attendee has enough data to fill out PayPal Pro's shipping info, use it.
249
+		// If not, use the billing info again
250
+		$ShippingAddress = array(
251
+			'shiptoname'     => substr($use_registration_address_info
252
+				? $attendee->full_name()
253
+				: $billing_info['first_name'] . ' ' . $billing_info['last_name'], 0, 32),
254
+			'shiptostreet'   => substr($use_registration_address_info
255
+				? $attendee->address()
256
+				: $billing_info['address'], 0, 100),
257
+			'shiptostreet2'  => substr($use_registration_address_info
258
+				? $attendee->address2() : $billing_info['address2'], 0, 100),
259
+			'shiptocity'     => substr($use_registration_address_info
260
+				? $attendee->city()
261
+				: $billing_info['city'], 0, 40),
262
+			'state'          => substr($use_registration_address_info
263
+				? $attendee->state_name()
264
+				: $billing_info['state'], 0, 40),
265
+			'shiptocountry'  => $use_registration_address_info
266
+				? $attendee->country_ID()
267
+				: $billing_info['country'],
268
+			'shiptozip'      => substr($use_registration_address_info
269
+				? $attendee->zip()
270
+				: $billing_info['zip'], 0, 20),
271
+			'shiptophonenum' => substr($use_registration_address_info
272
+				? $attendee->phone()
273
+				: $billing_info['phone'], 0, 20),
274
+		);
275
+		$PaymentDetails = array(
276
+			// Required.  Total amount of order, including shipping, handling, and tax.
277
+			'amt'          => $gateway_formatter->formatCurrency($payment->amount()),
278
+			// Required.  Three-letter currency code.  Default is USD.
279
+			'currencycode' => $payment->currency_code(),
280
+			// Required if you include itemized cart details. (L_AMTn, etc.)
281
+			// Subtotal of items not including S&H, or tax.
282
+			'itemamt'      => $gateway_formatter->formatCurrency($item_amount),//
283
+			// Total shipping costs for the order.  If you specify shippingamt, you must also specify itemamt.
284
+			'shippingamt'  => '',
285
+			// Total handling costs for the order.  If you specify handlingamt, you must also specify itemamt.
286
+			'handlingamt'  => '',
287
+			// Required if you specify itemized cart tax details.
288
+			// Sum of tax for all items on the order.  Total sales tax.
289
+			'taxamt'       => $gateway_formatter->formatCurrency($tax_amount),
290
+			// Description of the order the customer is purchasing.  127 char max.
291
+			'desc'         => $order_description,
292
+			// Free-form field for your own use.  256 char max.
293
+			'custom'       => $primary_registrant ? $primary_registrant->ID() : '',
294
+			// Your own invoice or tracking number
295
+			'invnum'       => wp_generate_password(12, false),// $transaction->ID(),
296
+			// URL for receiving Instant Payment Notifications.  This overrides what your profile is set to use.
297
+			'notifyurl'    => '',
298
+			'buttonsource' => 'EventEspresso_SP',// EE will blow up if you change this
299
+		);
300
+		// Wrap all data arrays into a single, "master" array which will be passed into the class function.
301
+		$PayPalRequestData = array(
302
+			'DPFields'        => $DPFields,
303
+			'CCDetails'       => $CCDetails,
304
+			'PayerInfo'       => $PayerInfo,
305
+			'PayerName'       => $PayerName,
306
+			'BillingAddress'  => $BillingAddress,
307
+			'ShippingAddress' => $ShippingAddress,
308
+			'PaymentDetails'  => $PaymentDetails,
309
+			'OrderItems'      => $order_items,
310
+		);
311
+		$this->_log_clean_request($PayPalRequestData, $payment);
312
+		try {
313
+			$PayPalResult = $this->prep_and_curl_request($PayPalRequestData);
314
+			// remove PCI-sensitive data so it doesn't get stored
315
+			$PayPalResult = $this->_log_clean_response($PayPalResult, $payment);
316
+			if (isset($PayPalResult['L_ERRORCODE0']) && $PayPalResult['L_ERRORCODE0'] === '10002') {
317
+				$message = esc_html__('PayPal did not accept your API username, password, or signature. Please double-check these credentials and if debug mode is on.', 'event_espresso');
318
+			} elseif (isset($PayPalResult['L_LONGMESSAGE0'])) {
319
+				$message = $PayPalResult['L_LONGMESSAGE0'];
320
+			} else {
321
+				$message = $PayPalResult['ACK'];
322
+			}
323
+			if (empty($PayPalResult['RAWRESPONSE'])) {
324
+				$payment->set_status($this->_pay_model->failed_status());
325
+				$payment->set_gateway_response(__('No response received from Paypal Pro', 'event_espresso'));
326
+				$payment->set_details($PayPalResult);
327
+			} else {
328
+				if ($this->_APICallSuccessful($PayPalResult)) {
329
+					$payment->set_status($this->_pay_model->approved_status());
330
+				} else {
331
+					$payment->set_status($this->_pay_model->declined_status());
332
+				}
333
+				// make sure we interpret the AMT as a float, not an international string
334
+				// (where periods are thousand separators)
335
+				$payment->set_amount(isset($PayPalResult['AMT']) ? floatval($PayPalResult['AMT']) : 0);
336
+				$payment->set_gateway_response($message);
337
+				$payment->set_txn_id_chq_nmbr(isset($PayPalResult['TRANSACTIONID'])
338
+					? $PayPalResult['TRANSACTIONID']
339
+					: null);
340
+				$primary_registration_code = $primary_registrant instanceof EE_Registration
341
+					? $primary_registrant->reg_code()
342
+					: '';
343
+				$payment->set_extra_accntng($primary_registration_code);
344
+				$payment->set_details($PayPalResult);
345
+			}
346
+		} catch (Exception $e) {
347
+			$payment->set_status($this->_pay_model->failed_status());
348
+			$payment->set_gateway_response($e->getMessage());
349
+		}
350
+		// $payment->set_status( $this->_pay_model->declined_status() );
351
+		// $payment->set_gateway_response( '' );
352
+		return $payment;
353
+	}
354
+
355
+
356
+
357
+	/**
358
+	 * CLeans out sensitive CC data and then logs it, and returns the cleaned request
359
+	 *
360
+	 * @param array       $request
361
+	 * @param EEI_Payment $payment
362
+	 * @return void
363
+	 */
364
+	private function _log_clean_request($request, $payment)
365
+	{
366
+		$cleaned_request_data = $request;
367
+		unset($cleaned_request_data['CCDetails']['acct']);
368
+		unset($cleaned_request_data['CCDetails']['cvv2']);
369
+		unset($cleaned_request_data['CCDetails']['expdate']);
370
+		$this->log(array('Paypal Request' => $cleaned_request_data), $payment);
371
+	}
372
+
373
+
374
+
375
+	/**
376
+	 * Cleans the response, logs it, and returns it
377
+	 *
378
+	 * @param array       $response
379
+	 * @param EEI_Payment $payment
380
+	 * @return array cleaned
381
+	 */
382
+	private function _log_clean_response($response, $payment)
383
+	{
384
+		unset($response['REQUESTDATA']['CREDITCARDTYPE']);
385
+		unset($response['REQUESTDATA']['ACCT']);
386
+		unset($response['REQUESTDATA']['EXPDATE']);
387
+		unset($response['REQUESTDATA']['CVV2']);
388
+		unset($response['RAWREQUEST']);
389
+		$this->log(array('Paypal Response' => $response), $payment);
390
+		return $response;
391
+	}
392
+
393
+
394
+
395
+	/**
396
+	 * @param $DataArray
397
+	 * @return array
398
+	 */
399
+	private function prep_and_curl_request($DataArray)
400
+	{
401
+		// Create empty holders for each portion of the NVP string
402
+		$DPFieldsNVP = '&METHOD=DoDirectPayment&BUTTONSOURCE=AngellEYE_PHP_Class_DDP';
403
+		$CCDetailsNVP = '';
404
+		$PayerInfoNVP = '';
405
+		$PayerNameNVP = '';
406
+		$BillingAddressNVP = '';
407
+		$ShippingAddressNVP = '';
408
+		$PaymentDetailsNVP = '';
409
+		$OrderItemsNVP = '';
410
+		$Secure3DNVP = '';
411
+		// DP Fields
412
+		$DPFields = isset($DataArray['DPFields']) ? $DataArray['DPFields'] : array();
413
+		foreach ($DPFields as $DPFieldsVar => $DPFieldsVal) {
414
+			$DPFieldsNVP .= '&' . strtoupper($DPFieldsVar) . '=' . urlencode($DPFieldsVal);
415
+		}
416
+		// CC Details Fields
417
+		$CCDetails = isset($DataArray['CCDetails']) ? $DataArray['CCDetails'] : array();
418
+		foreach ($CCDetails as $CCDetailsVar => $CCDetailsVal) {
419
+			$CCDetailsNVP .= '&' . strtoupper($CCDetailsVar) . '=' . urlencode($CCDetailsVal);
420
+		}
421
+		// PayerInfo Type Fields
422
+		$PayerInfo = isset($DataArray['PayerInfo']) ? $DataArray['PayerInfo'] : array();
423
+		foreach ($PayerInfo as $PayerInfoVar => $PayerInfoVal) {
424
+			$PayerInfoNVP .= '&' . strtoupper($PayerInfoVar) . '=' . urlencode($PayerInfoVal);
425
+		}
426
+		// Payer Name Fields
427
+		$PayerName = isset($DataArray['PayerName']) ? $DataArray['PayerName'] : array();
428
+		foreach ($PayerName as $PayerNameVar => $PayerNameVal) {
429
+			$PayerNameNVP .= '&' . strtoupper($PayerNameVar) . '=' . urlencode($PayerNameVal);
430
+		}
431
+		// Address Fields (Billing)
432
+		$BillingAddress = isset($DataArray['BillingAddress']) ? $DataArray['BillingAddress'] : array();
433
+		foreach ($BillingAddress as $BillingAddressVar => $BillingAddressVal) {
434
+			$BillingAddressNVP .= '&' . strtoupper($BillingAddressVar) . '=' . urlencode($BillingAddressVal);
435
+		}
436
+		// Payment Details Type Fields
437
+		$PaymentDetails = isset($DataArray['PaymentDetails']) ? $DataArray['PaymentDetails'] : array();
438
+		foreach ($PaymentDetails as $PaymentDetailsVar => $PaymentDetailsVal) {
439
+			$PaymentDetailsNVP .= '&' . strtoupper($PaymentDetailsVar) . '=' . urlencode($PaymentDetailsVal);
440
+		}
441
+		// Payment Details Item Type Fields
442
+		$OrderItems = isset($DataArray['OrderItems']) ? $DataArray['OrderItems'] : array();
443
+		$n = 0;
444
+		foreach ($OrderItems as $OrderItemsVar => $OrderItemsVal) {
445
+			$CurrentItem = $OrderItems[ $OrderItemsVar ];
446
+			foreach ($CurrentItem as $CurrentItemVar => $CurrentItemVal) {
447
+				$OrderItemsNVP .= '&' . strtoupper($CurrentItemVar) . $n . '=' . urlencode($CurrentItemVal);
448
+			}
449
+			$n++;
450
+		}
451
+		// Ship To Address Fields
452
+		$ShippingAddress = isset($DataArray['ShippingAddress']) ? $DataArray['ShippingAddress'] : array();
453
+		foreach ($ShippingAddress as $ShippingAddressVar => $ShippingAddressVal) {
454
+			$ShippingAddressNVP .= '&' . strtoupper($ShippingAddressVar) . '=' . urlencode($ShippingAddressVal);
455
+		}
456
+		// 3D Secure Fields
457
+		$Secure3D = isset($DataArray['Secure3D']) ? $DataArray['Secure3D'] : array();
458
+		foreach ($Secure3D as $Secure3DVar => $Secure3DVal) {
459
+			$Secure3DNVP .= '&' . strtoupper($Secure3DVar) . '=' . urlencode($Secure3DVal);
460
+		}
461
+		// Now that we have each chunk we need to go ahead and append them all together for our entire NVP string
462
+		$NVPRequest = 'USER='
463
+					  . $this->_username
464
+					  . '&PWD='
465
+					  . $this->_password
466
+					  . '&VERSION=64.0'
467
+					  . '&SIGNATURE='
468
+					  . $this->_signature
469
+					  . $DPFieldsNVP
470
+					  . $CCDetailsNVP
471
+					  . $PayerInfoNVP
472
+					  . $PayerNameNVP
473
+					  . $BillingAddressNVP
474
+					  . $PaymentDetailsNVP
475
+					  . $OrderItemsNVP
476
+					  . $ShippingAddressNVP
477
+					  . $Secure3DNVP;
478
+		$NVPResponse = $this->_CURLRequest($NVPRequest);
479
+		$NVPRequestArray = $this->_NVPToArray($NVPRequest);
480
+		$NVPResponseArray = $this->_NVPToArray($NVPResponse);
481
+		$Errors = $this->_GetErrors($NVPResponseArray);
482
+		$NVPResponseArray['ERRORS'] = $Errors;
483
+		$NVPResponseArray['REQUESTDATA'] = $NVPRequestArray;
484
+		$NVPResponseArray['RAWREQUEST'] = $NVPRequest;
485
+		$NVPResponseArray['RAWRESPONSE'] = $NVPResponse;
486
+		return $NVPResponseArray;
487
+	}
488
+
489
+
490
+
491
+	/**
492
+	 * @param $Request
493
+	 * @return mixed
494
+	 */
495
+	private function _CURLRequest($Request)
496
+	{
497
+		$EndPointURL = $this->_debug_mode ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp';
498
+		$curl = curl_init();
499
+		curl_setopt($curl, CURLOPT_VERBOSE, apply_filters('FHEE__EEG_Paypal_Pro__CurlRequest__CURLOPT_VERBOSE', true));
500
+		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
501
+		curl_setopt($curl, CURLOPT_TIMEOUT, 60);
502
+		curl_setopt($curl, CURLOPT_URL, $EndPointURL);
503
+		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
504
+		curl_setopt($curl, CURLOPT_POSTFIELDS, $Request);
505
+		curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
506
+		// execute the curl POST
507
+		$Response = curl_exec($curl);
508
+		curl_close($curl);
509
+		return $Response;
510
+	}
511
+
512
+
513
+
514
+	/**
515
+	 * @param $NVPString
516
+	 * @return array
517
+	 */
518
+	private function _NVPToArray($NVPString)
519
+	{
520
+		// prepare responses into array
521
+		$proArray = array();
522
+		while (strlen($NVPString)) {
523
+			// name
524
+			$keypos = strpos($NVPString, '=');
525
+			$keyval = substr($NVPString, 0, $keypos);
526
+			// value
527
+			$valuepos = strpos($NVPString, '&') ? strpos($NVPString, '&') : strlen($NVPString);
528
+			$valval = substr($NVPString, $keypos + 1, $valuepos - $keypos - 1);
529
+			// decoding the response
530
+			$proArray[ $keyval ] = urldecode($valval);
531
+			$NVPString = substr($NVPString, $valuepos + 1, strlen($NVPString));
532
+		}
533
+		return $proArray;
534
+	}
535
+
536
+
537
+
538
+	/**
539
+	 * @param array $PayPalResult
540
+	 * @return bool
541
+	 */
542
+	private function _APICallSuccessful($PayPalResult)
543
+	{
544
+		$approved = false;
545
+		// check main response message from PayPal
546
+		if (isset($PayPalResult['ACK']) && ! empty($PayPalResult['ACK'])) {
547
+			$ack = strtoupper($PayPalResult['ACK']);
548
+			$approved = ($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING' || $ack == 'PARTIALSUCCESS') ? true : false;
549
+		}
550
+		return $approved;
551
+	}
552
+
553
+
554
+
555
+	/**
556
+	 * @param $DataArray
557
+	 * @return array
558
+	 */
559
+	private function _GetErrors($DataArray)
560
+	{
561
+		$Errors = array();
562
+		$n = 0;
563
+		while (isset($DataArray[ 'L_ERRORCODE' . $n . '' ])) {
564
+			$LErrorCode = isset($DataArray[ 'L_ERRORCODE' . $n . '' ]) ? $DataArray[ 'L_ERRORCODE' . $n . '' ] : '';
565
+			$LShortMessage = isset($DataArray[ 'L_SHORTMESSAGE' . $n . '' ])
566
+				? $DataArray[ 'L_SHORTMESSAGE' . $n . '' ]
567
+				: '';
568
+			$LLongMessage = isset($DataArray[ 'L_LONGMESSAGE' . $n . '' ])
569
+				? $DataArray[ 'L_LONGMESSAGE' . $n . '' ]
570
+				: '';
571
+			$LSeverityCode = isset($DataArray[ 'L_SEVERITYCODE' . $n . '' ])
572
+				? $DataArray[ 'L_SEVERITYCODE' . $n . '' ]
573
+				: '';
574
+			$CurrentItem = array(
575
+				'L_ERRORCODE'    => $LErrorCode,
576
+				'L_SHORTMESSAGE' => $LShortMessage,
577
+				'L_LONGMESSAGE'  => $LLongMessage,
578
+				'L_SEVERITYCODE' => $LSeverityCode,
579
+			);
580
+			array_push($Errors, $CurrentItem);
581
+			$n++;
582
+		}
583
+		return $Errors;
584
+	}
585
+
586
+
587
+
588
+	/**
589
+	 *        nothing to see here...  move along....
590
+	 *
591
+	 * @access protected
592
+	 * @param $Errors
593
+	 * @return string
594
+	 */
595
+	private function _DisplayErrors($Errors)
596
+	{
597
+		$error = '';
598
+		foreach ($Errors as $ErrorVar => $ErrorVal) {
599
+			$CurrentError = $Errors[ $ErrorVar ];
600
+			foreach ($CurrentError as $CurrentErrorVar => $CurrentErrorVal) {
601
+				$CurrentVarName = '';
602
+				if ($CurrentErrorVar == 'L_ERRORCODE') {
603
+					$CurrentVarName = 'Error Code';
604
+				} elseif ($CurrentErrorVar == 'L_SHORTMESSAGE') {
605
+					$CurrentVarName = 'Short Message';
606
+				} elseif ($CurrentErrorVar == 'L_LONGMESSAGE') {
607
+					$CurrentVarName = 'Long Message';
608
+				} elseif ($CurrentErrorVar == 'L_SEVERITYCODE') {
609
+					$CurrentVarName = 'Severity Code';
610
+				}
611
+				$error .= '<br />' . $CurrentVarName . ': ' . $CurrentErrorVal;
612
+			}
613
+		}
614
+		return $error;
615
+	}
616 616
 }
Please login to merge, or discard this patch.
admin_pages/messages/Messages_Template_List_Table.class.php 2 patches
Indentation   +350 added lines, -350 removed lines patch added patch discarded remove patch
@@ -13,354 +13,354 @@
 block discarded – undo
13 13
 {
14 14
 
15 15
 
16
-    /**
17
-     * @return Messages_Admin_Page
18
-     */
19
-    public function get_admin_page()
20
-    {
21
-        return $this->_admin_page;
22
-    }
23
-
24
-
25
-    /**
26
-     * Setup data object
27
-     */
28
-    protected function _setup_data()
29
-    {
30
-        $this->_data = $this->get_admin_page()->get_message_templates(
31
-            $this->_per_page,
32
-            $this->_view,
33
-            false
34
-        );
35
-        $this->_all_data_count = $this->get_admin_page()->get_message_templates(
36
-            $this->_per_page,
37
-            $this->_view,
38
-            true,
39
-            true
40
-        );
41
-    }
42
-
43
-
44
-    /**
45
-     * Set internal properties
46
-     */
47
-    protected function _set_properties()
48
-    {
49
-        $this->_wp_list_args = array(
50
-            'singular' => esc_html__('Message Template Group', 'event_espresso'),
51
-            'plural'   => esc_html__('Message Template', 'event_espresso'),
52
-            'ajax'     => true, // for now,
53
-            'screen'   => $this->get_admin_page()->get_current_screen()->id,
54
-        );
55
-        $this->_columns = array(
56
-            // 'cb' => '<input type="checkbox" />', //no deleting default (global) templates!
57
-            'message_type' => esc_html__('Message Type', 'event_espresso'),
58
-            'messenger'    => esc_html__('Messenger', 'event_espresso'),
59
-            'description'  => esc_html__('Description', 'event_espresso'),
60
-        );
61
-
62
-        $this->_sortable_columns = array(
63
-            'messenger' => array('MTP_messenger' => true),
64
-        );
65
-
66
-        $this->_hidden_columns = array();
67
-    }
68
-
69
-
70
-    /**
71
-     * Overriding the single_row method from parent to verify whether the $item has an accessible
72
-     * message_type or messenger object before generating the row.
73
-     *
74
-     * @param EE_Message_Template_Group $item
75
-     * @return string
76
-     * @throws EE_Error
77
-     */
78
-    public function single_row($item)
79
-    {
80
-        $message_type = $item->message_type_obj();
81
-        $messenger = $item->messenger_obj();
82
-
83
-        if (! $message_type instanceof EE_message_type || ! $messenger instanceof EE_messenger) {
84
-            echo '';
85
-            return;
86
-        }
87
-
88
-        parent::single_row($item);
89
-    }
90
-
91
-
92
-    /**
93
-     * @return array
94
-     * @throws EE_Error
95
-     */
96
-    protected function _get_table_filters()
97
-    {
98
-        $filters = array();
99
-
100
-        // get select inputs
101
-        $select_inputs = array(
102
-            $this->_get_messengers_dropdown_filter(),
103
-            $this->_get_message_types_dropdown_filter(),
104
-        );
105
-
106
-        // set filters to select inputs if they aren't empty
107
-        foreach ($select_inputs as $select_input) {
108
-            if ($select_input) {
109
-                $filters[] = $select_input;
110
-            }
111
-        }
112
-        return $filters;
113
-    }
114
-
115
-    /**
116
-     * We're just removing the search box for message templates, not needed.
117
-     *
118
-     * @param string $text
119
-     * @param string $input_id
120
-     * @return string ;
121
-     */
122
-    public function search_box($text, $input_id)
123
-    {
124
-        return '';
125
-    }
126
-
127
-
128
-    /**
129
-     * Add counts to the _views property
130
-     */
131
-    protected function _add_view_counts()
132
-    {
133
-        foreach ($this->_views as $view => $args) {
134
-            $this->_views[ $view ]['count'] = $this->get_admin_page()->get_message_templates(
135
-                $this->_per_page,
136
-                $view,
137
-                true,
138
-                true
139
-            );
140
-        }
141
-    }
142
-
143
-
144
-    /**
145
-     * @param EE_Message_Template_Group $item
146
-     * @return string
147
-     */
148
-    public function column_cb($item)
149
-    {
150
-        return '';
151
-    }
152
-
153
-
154
-    /**
155
-     * @param EE_Message_Template_Group $item
156
-     * @return string
157
-     * @throws EE_Error
158
-     */
159
-    public function column_description($item)
160
-    {
161
-        return '<p>' . $item->message_type_obj()->description . '</p>';
162
-    }
163
-
164
-
165
-    /**
166
-     * @param EE_Message_Template_Group $item
167
-     * @return string
168
-     * @throws EE_Error
169
-     */
170
-    public function column_messenger($item)
171
-    {
172
-        // Return the name contents
173
-        return sprintf(
174
-            '%1$s <span style="color:silver">(id:%2$s)</span><br />%3$s%4$s',
175
-            /* $1%s */
176
-            ucwords($item->messenger_obj()->label['singular']),
177
-            /* $2%s */
178
-            $item->GRP_ID(),
179
-            /* %4$s */
180
-            $this->_get_context_links($item),
181
-            $this->row_actions($this->_get_actions_for_messenger_column($item))
182
-        );
183
-    }
184
-
185
-    /**
186
-     * column_message_type
187
-     *
188
-     * @param  EE_Message_Template_Group $item message info for the row
189
-     * @return string message_type name
190
-     * @throws EE_Error
191
-     */
192
-    public function column_message_type($item)
193
-    {
194
-        return ucwords($item->message_type_obj()->label['singular']);
195
-    }
196
-
197
-
198
-    /**
199
-     * Generate dropdown filter select input for messengers
200
-     *
201
-     * @param bool $global
202
-     * @return string
203
-     * @throws EE_Error
204
-     */
205
-    protected function _get_messengers_dropdown_filter($global = true)
206
-    {
207
-        $messenger_options = array();
208
-        $active_message_template_groups_grouped_by_messenger = EEM_Message_Template_Group::instance()->get_all(
209
-            array(
210
-                array(
211
-                    'MTP_is_active' => true,
212
-                    'MTP_is_global' => $global,
213
-                ),
214
-                'group_by' => 'MTP_messenger',
215
-            )
216
-        );
217
-
218
-        foreach ($active_message_template_groups_grouped_by_messenger as $active_message_template_group) {
219
-            if ($active_message_template_group instanceof EE_Message_Template_Group) {
220
-                $messenger = $active_message_template_group->messenger_obj();
221
-                $messenger_label = $messenger instanceof EE_messenger
222
-                    ? $messenger->label['singular']
223
-                    : $active_message_template_group->messenger();
224
-                $messenger_options[ $active_message_template_group->messenger() ] = ucwords($messenger_label);
225
-            }
226
-        }
227
-        return $this->get_admin_page()->get_messengers_select_input($messenger_options);
228
-    }
229
-
230
-
231
-    /**
232
-     * Generate dropdown filter select input for message types
233
-     *
234
-     * @param bool $global
235
-     * @return string
236
-     * @throws EE_Error
237
-     */
238
-    protected function _get_message_types_dropdown_filter($global = true)
239
-    {
240
-        $message_type_options = array();
241
-        $active_message_template_groups_grouped_by_message_type = EEM_Message_Template_Group::instance()->get_all(
242
-            array(
243
-                array(
244
-                    'MTP_is_active' => true,
245
-                    'MTP_is_global' => true,
246
-                ),
247
-                'group_by' => 'MTP_message_type',
248
-            )
249
-        );
250
-
251
-        foreach ($active_message_template_groups_grouped_by_message_type as $active_message_template_group) {
252
-            if ($active_message_template_group instanceof EE_Message_Template_Group) {
253
-                $message_type = $active_message_template_group->message_type_obj();
254
-                $message_type_label = $message_type instanceof EE_message_type
255
-                    ? $message_type->label['singular']
256
-                    : $active_message_template_group->message_type();
257
-                $message_type_options[ $active_message_template_group->message_type() ] = ucwords($message_type_label);
258
-            }
259
-        }
260
-        return $this->get_admin_page()->get_message_types_select_input($message_type_options);
261
-    }
262
-
263
-
264
-    /**
265
-     * Return the edit url for the message template group.
266
-     *
267
-     * @param EE_Message_Template_Group $item
268
-     * @return string
269
-     * @throws EE_Error
270
-     */
271
-    protected function _get_edit_url(EE_Message_Template_Group $item)
272
-    {
273
-        $edit_url = '';
274
-        // edit link but only if item isn't trashed.
275
-        if (! $item->get('MTP_deleted')
276
-            && EE_Registry::instance()->CAP->current_user_can(
277
-                'ee_edit_message',
278
-                'espresso_messages_edit_message_template',
279
-                $item->ID()
280
-            )) {
281
-            $edit_url = EE_Admin_Page::add_query_args_and_nonce(
282
-                array(
283
-                    'action' => 'edit_message_template',
284
-                    'id'     => $item->GRP_ID(),
285
-                ),
286
-                EE_MSG_ADMIN_URL
287
-            );
288
-        }
289
-        return $edit_url;
290
-    }
291
-
292
-
293
-    /**
294
-     * Get the context link string for the messenger column.
295
-     *
296
-     * @param EE_Message_Template_Group $item
297
-     * @return string
298
-     * @throws EE_Error
299
-     */
300
-    protected function _get_context_links(EE_Message_Template_Group $item)
301
-    {
302
-        // first check if we even show the context links or not.
303
-        if (! EE_Registry::instance()->CAP->current_user_can(
304
-            'ee_edit_message',
305
-            'espresso_messages_edit_message_template',
306
-            $item->ID()
307
-        )
308
-            || $item->get('MTP_deleted')
309
-        ) {
310
-            return '';
311
-        }
312
-        // we want to display the contexts in here so we need to set them up
313
-        $c_label = $item->context_label();
314
-        $c_configs = $item->contexts_config();
315
-        $ctxt = array();
316
-        $context_templates = $item->context_templates();
317
-        foreach ($context_templates as $context => $template_fields) {
318
-            $mtp_to = ! empty($context_templates[ $context ]['to'])
319
-                      && $context_templates[ $context ]['to'] instanceof EE_Message_Template
320
-                ? $context_templates[ $context ]['to']->get('MTP_content')
321
-                : null;
322
-            $inactive_class = (
323
-                                  empty($mtp_to)
324
-                                  && ! empty($context_templates[ $context ]['to'])
325
-                              )
326
-                              || ! $item->is_context_active($context)
327
-                ? ' mtp-inactive'
328
-                : '';
329
-            $context_title = sprintf(
330
-                /* translators: Placeholder represents the context label. Example "Edit Event Admin" */
331
-                esc_html__('Edit %1$s', 'event_espresso'),
332
-                ucwords($c_configs[ $context ]['label'])
333
-            );
334
-            $edit_link = EE_Admin_Page::add_query_args_and_nonce(
335
-                array(
336
-                    'action'  => 'edit_message_template',
337
-                    'id'      => $item->GRP_ID(),
338
-                    'context' => $context,
339
-                ),
340
-                EE_MSG_ADMIN_URL
341
-            );
342
-            $ctxt[] = '<a'
343
-                      . ' href="' . $edit_link . '"'
344
-                      . ' class="' . $item->message_type() . '-' . $context . '-edit-link' . $inactive_class . '"'
345
-                      . ' title="' . esc_attr__('Edit Context', 'event_espresso') . '">'
346
-                      . $context_title
347
-                      . '</a>';
348
-        }
349
-
350
-        return sprintf('<strong>%s:</strong> ', ucwords($c_label['plural'])) . implode(' | ', $ctxt);
351
-    }
352
-
353
-
354
-    /**
355
-     * Returns the actions for the messenger column.
356
-     *
357
-     * Note: Children classes may override this so do not remove it.
358
-     *
359
-     * @param EE_Message_Template_Group $item
360
-     * @return array
361
-     */
362
-    protected function _get_actions_for_messenger_column(EE_Message_Template_Group $item)
363
-    {
364
-        return [];
365
-    }
16
+	/**
17
+	 * @return Messages_Admin_Page
18
+	 */
19
+	public function get_admin_page()
20
+	{
21
+		return $this->_admin_page;
22
+	}
23
+
24
+
25
+	/**
26
+	 * Setup data object
27
+	 */
28
+	protected function _setup_data()
29
+	{
30
+		$this->_data = $this->get_admin_page()->get_message_templates(
31
+			$this->_per_page,
32
+			$this->_view,
33
+			false
34
+		);
35
+		$this->_all_data_count = $this->get_admin_page()->get_message_templates(
36
+			$this->_per_page,
37
+			$this->_view,
38
+			true,
39
+			true
40
+		);
41
+	}
42
+
43
+
44
+	/**
45
+	 * Set internal properties
46
+	 */
47
+	protected function _set_properties()
48
+	{
49
+		$this->_wp_list_args = array(
50
+			'singular' => esc_html__('Message Template Group', 'event_espresso'),
51
+			'plural'   => esc_html__('Message Template', 'event_espresso'),
52
+			'ajax'     => true, // for now,
53
+			'screen'   => $this->get_admin_page()->get_current_screen()->id,
54
+		);
55
+		$this->_columns = array(
56
+			// 'cb' => '<input type="checkbox" />', //no deleting default (global) templates!
57
+			'message_type' => esc_html__('Message Type', 'event_espresso'),
58
+			'messenger'    => esc_html__('Messenger', 'event_espresso'),
59
+			'description'  => esc_html__('Description', 'event_espresso'),
60
+		);
61
+
62
+		$this->_sortable_columns = array(
63
+			'messenger' => array('MTP_messenger' => true),
64
+		);
65
+
66
+		$this->_hidden_columns = array();
67
+	}
68
+
69
+
70
+	/**
71
+	 * Overriding the single_row method from parent to verify whether the $item has an accessible
72
+	 * message_type or messenger object before generating the row.
73
+	 *
74
+	 * @param EE_Message_Template_Group $item
75
+	 * @return string
76
+	 * @throws EE_Error
77
+	 */
78
+	public function single_row($item)
79
+	{
80
+		$message_type = $item->message_type_obj();
81
+		$messenger = $item->messenger_obj();
82
+
83
+		if (! $message_type instanceof EE_message_type || ! $messenger instanceof EE_messenger) {
84
+			echo '';
85
+			return;
86
+		}
87
+
88
+		parent::single_row($item);
89
+	}
90
+
91
+
92
+	/**
93
+	 * @return array
94
+	 * @throws EE_Error
95
+	 */
96
+	protected function _get_table_filters()
97
+	{
98
+		$filters = array();
99
+
100
+		// get select inputs
101
+		$select_inputs = array(
102
+			$this->_get_messengers_dropdown_filter(),
103
+			$this->_get_message_types_dropdown_filter(),
104
+		);
105
+
106
+		// set filters to select inputs if they aren't empty
107
+		foreach ($select_inputs as $select_input) {
108
+			if ($select_input) {
109
+				$filters[] = $select_input;
110
+			}
111
+		}
112
+		return $filters;
113
+	}
114
+
115
+	/**
116
+	 * We're just removing the search box for message templates, not needed.
117
+	 *
118
+	 * @param string $text
119
+	 * @param string $input_id
120
+	 * @return string ;
121
+	 */
122
+	public function search_box($text, $input_id)
123
+	{
124
+		return '';
125
+	}
126
+
127
+
128
+	/**
129
+	 * Add counts to the _views property
130
+	 */
131
+	protected function _add_view_counts()
132
+	{
133
+		foreach ($this->_views as $view => $args) {
134
+			$this->_views[ $view ]['count'] = $this->get_admin_page()->get_message_templates(
135
+				$this->_per_page,
136
+				$view,
137
+				true,
138
+				true
139
+			);
140
+		}
141
+	}
142
+
143
+
144
+	/**
145
+	 * @param EE_Message_Template_Group $item
146
+	 * @return string
147
+	 */
148
+	public function column_cb($item)
149
+	{
150
+		return '';
151
+	}
152
+
153
+
154
+	/**
155
+	 * @param EE_Message_Template_Group $item
156
+	 * @return string
157
+	 * @throws EE_Error
158
+	 */
159
+	public function column_description($item)
160
+	{
161
+		return '<p>' . $item->message_type_obj()->description . '</p>';
162
+	}
163
+
164
+
165
+	/**
166
+	 * @param EE_Message_Template_Group $item
167
+	 * @return string
168
+	 * @throws EE_Error
169
+	 */
170
+	public function column_messenger($item)
171
+	{
172
+		// Return the name contents
173
+		return sprintf(
174
+			'%1$s <span style="color:silver">(id:%2$s)</span><br />%3$s%4$s',
175
+			/* $1%s */
176
+			ucwords($item->messenger_obj()->label['singular']),
177
+			/* $2%s */
178
+			$item->GRP_ID(),
179
+			/* %4$s */
180
+			$this->_get_context_links($item),
181
+			$this->row_actions($this->_get_actions_for_messenger_column($item))
182
+		);
183
+	}
184
+
185
+	/**
186
+	 * column_message_type
187
+	 *
188
+	 * @param  EE_Message_Template_Group $item message info for the row
189
+	 * @return string message_type name
190
+	 * @throws EE_Error
191
+	 */
192
+	public function column_message_type($item)
193
+	{
194
+		return ucwords($item->message_type_obj()->label['singular']);
195
+	}
196
+
197
+
198
+	/**
199
+	 * Generate dropdown filter select input for messengers
200
+	 *
201
+	 * @param bool $global
202
+	 * @return string
203
+	 * @throws EE_Error
204
+	 */
205
+	protected function _get_messengers_dropdown_filter($global = true)
206
+	{
207
+		$messenger_options = array();
208
+		$active_message_template_groups_grouped_by_messenger = EEM_Message_Template_Group::instance()->get_all(
209
+			array(
210
+				array(
211
+					'MTP_is_active' => true,
212
+					'MTP_is_global' => $global,
213
+				),
214
+				'group_by' => 'MTP_messenger',
215
+			)
216
+		);
217
+
218
+		foreach ($active_message_template_groups_grouped_by_messenger as $active_message_template_group) {
219
+			if ($active_message_template_group instanceof EE_Message_Template_Group) {
220
+				$messenger = $active_message_template_group->messenger_obj();
221
+				$messenger_label = $messenger instanceof EE_messenger
222
+					? $messenger->label['singular']
223
+					: $active_message_template_group->messenger();
224
+				$messenger_options[ $active_message_template_group->messenger() ] = ucwords($messenger_label);
225
+			}
226
+		}
227
+		return $this->get_admin_page()->get_messengers_select_input($messenger_options);
228
+	}
229
+
230
+
231
+	/**
232
+	 * Generate dropdown filter select input for message types
233
+	 *
234
+	 * @param bool $global
235
+	 * @return string
236
+	 * @throws EE_Error
237
+	 */
238
+	protected function _get_message_types_dropdown_filter($global = true)
239
+	{
240
+		$message_type_options = array();
241
+		$active_message_template_groups_grouped_by_message_type = EEM_Message_Template_Group::instance()->get_all(
242
+			array(
243
+				array(
244
+					'MTP_is_active' => true,
245
+					'MTP_is_global' => true,
246
+				),
247
+				'group_by' => 'MTP_message_type',
248
+			)
249
+		);
250
+
251
+		foreach ($active_message_template_groups_grouped_by_message_type as $active_message_template_group) {
252
+			if ($active_message_template_group instanceof EE_Message_Template_Group) {
253
+				$message_type = $active_message_template_group->message_type_obj();
254
+				$message_type_label = $message_type instanceof EE_message_type
255
+					? $message_type->label['singular']
256
+					: $active_message_template_group->message_type();
257
+				$message_type_options[ $active_message_template_group->message_type() ] = ucwords($message_type_label);
258
+			}
259
+		}
260
+		return $this->get_admin_page()->get_message_types_select_input($message_type_options);
261
+	}
262
+
263
+
264
+	/**
265
+	 * Return the edit url for the message template group.
266
+	 *
267
+	 * @param EE_Message_Template_Group $item
268
+	 * @return string
269
+	 * @throws EE_Error
270
+	 */
271
+	protected function _get_edit_url(EE_Message_Template_Group $item)
272
+	{
273
+		$edit_url = '';
274
+		// edit link but only if item isn't trashed.
275
+		if (! $item->get('MTP_deleted')
276
+			&& EE_Registry::instance()->CAP->current_user_can(
277
+				'ee_edit_message',
278
+				'espresso_messages_edit_message_template',
279
+				$item->ID()
280
+			)) {
281
+			$edit_url = EE_Admin_Page::add_query_args_and_nonce(
282
+				array(
283
+					'action' => 'edit_message_template',
284
+					'id'     => $item->GRP_ID(),
285
+				),
286
+				EE_MSG_ADMIN_URL
287
+			);
288
+		}
289
+		return $edit_url;
290
+	}
291
+
292
+
293
+	/**
294
+	 * Get the context link string for the messenger column.
295
+	 *
296
+	 * @param EE_Message_Template_Group $item
297
+	 * @return string
298
+	 * @throws EE_Error
299
+	 */
300
+	protected function _get_context_links(EE_Message_Template_Group $item)
301
+	{
302
+		// first check if we even show the context links or not.
303
+		if (! EE_Registry::instance()->CAP->current_user_can(
304
+			'ee_edit_message',
305
+			'espresso_messages_edit_message_template',
306
+			$item->ID()
307
+		)
308
+			|| $item->get('MTP_deleted')
309
+		) {
310
+			return '';
311
+		}
312
+		// we want to display the contexts in here so we need to set them up
313
+		$c_label = $item->context_label();
314
+		$c_configs = $item->contexts_config();
315
+		$ctxt = array();
316
+		$context_templates = $item->context_templates();
317
+		foreach ($context_templates as $context => $template_fields) {
318
+			$mtp_to = ! empty($context_templates[ $context ]['to'])
319
+					  && $context_templates[ $context ]['to'] instanceof EE_Message_Template
320
+				? $context_templates[ $context ]['to']->get('MTP_content')
321
+				: null;
322
+			$inactive_class = (
323
+								  empty($mtp_to)
324
+								  && ! empty($context_templates[ $context ]['to'])
325
+							  )
326
+							  || ! $item->is_context_active($context)
327
+				? ' mtp-inactive'
328
+				: '';
329
+			$context_title = sprintf(
330
+				/* translators: Placeholder represents the context label. Example "Edit Event Admin" */
331
+				esc_html__('Edit %1$s', 'event_espresso'),
332
+				ucwords($c_configs[ $context ]['label'])
333
+			);
334
+			$edit_link = EE_Admin_Page::add_query_args_and_nonce(
335
+				array(
336
+					'action'  => 'edit_message_template',
337
+					'id'      => $item->GRP_ID(),
338
+					'context' => $context,
339
+				),
340
+				EE_MSG_ADMIN_URL
341
+			);
342
+			$ctxt[] = '<a'
343
+					  . ' href="' . $edit_link . '"'
344
+					  . ' class="' . $item->message_type() . '-' . $context . '-edit-link' . $inactive_class . '"'
345
+					  . ' title="' . esc_attr__('Edit Context', 'event_espresso') . '">'
346
+					  . $context_title
347
+					  . '</a>';
348
+		}
349
+
350
+		return sprintf('<strong>%s:</strong> ', ucwords($c_label['plural'])) . implode(' | ', $ctxt);
351
+	}
352
+
353
+
354
+	/**
355
+	 * Returns the actions for the messenger column.
356
+	 *
357
+	 * Note: Children classes may override this so do not remove it.
358
+	 *
359
+	 * @param EE_Message_Template_Group $item
360
+	 * @return array
361
+	 */
362
+	protected function _get_actions_for_messenger_column(EE_Message_Template_Group $item)
363
+	{
364
+		return [];
365
+	}
366 366
 }
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
         $message_type = $item->message_type_obj();
81 81
         $messenger = $item->messenger_obj();
82 82
 
83
-        if (! $message_type instanceof EE_message_type || ! $messenger instanceof EE_messenger) {
83
+        if ( ! $message_type instanceof EE_message_type || ! $messenger instanceof EE_messenger) {
84 84
             echo '';
85 85
             return;
86 86
         }
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
     protected function _add_view_counts()
132 132
     {
133 133
         foreach ($this->_views as $view => $args) {
134
-            $this->_views[ $view ]['count'] = $this->get_admin_page()->get_message_templates(
134
+            $this->_views[$view]['count'] = $this->get_admin_page()->get_message_templates(
135 135
                 $this->_per_page,
136 136
                 $view,
137 137
                 true,
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
      */
159 159
     public function column_description($item)
160 160
     {
161
-        return '<p>' . $item->message_type_obj()->description . '</p>';
161
+        return '<p>'.$item->message_type_obj()->description.'</p>';
162 162
     }
163 163
 
164 164
 
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
                 $messenger_label = $messenger instanceof EE_messenger
222 222
                     ? $messenger->label['singular']
223 223
                     : $active_message_template_group->messenger();
224
-                $messenger_options[ $active_message_template_group->messenger() ] = ucwords($messenger_label);
224
+                $messenger_options[$active_message_template_group->messenger()] = ucwords($messenger_label);
225 225
             }
226 226
         }
227 227
         return $this->get_admin_page()->get_messengers_select_input($messenger_options);
@@ -254,7 +254,7 @@  discard block
 block discarded – undo
254 254
                 $message_type_label = $message_type instanceof EE_message_type
255 255
                     ? $message_type->label['singular']
256 256
                     : $active_message_template_group->message_type();
257
-                $message_type_options[ $active_message_template_group->message_type() ] = ucwords($message_type_label);
257
+                $message_type_options[$active_message_template_group->message_type()] = ucwords($message_type_label);
258 258
             }
259 259
         }
260 260
         return $this->get_admin_page()->get_message_types_select_input($message_type_options);
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
     {
273 273
         $edit_url = '';
274 274
         // edit link but only if item isn't trashed.
275
-        if (! $item->get('MTP_deleted')
275
+        if ( ! $item->get('MTP_deleted')
276 276
             && EE_Registry::instance()->CAP->current_user_can(
277 277
                 'ee_edit_message',
278 278
                 'espresso_messages_edit_message_template',
@@ -300,7 +300,7 @@  discard block
 block discarded – undo
300 300
     protected function _get_context_links(EE_Message_Template_Group $item)
301 301
     {
302 302
         // first check if we even show the context links or not.
303
-        if (! EE_Registry::instance()->CAP->current_user_can(
303
+        if ( ! EE_Registry::instance()->CAP->current_user_can(
304 304
             'ee_edit_message',
305 305
             'espresso_messages_edit_message_template',
306 306
             $item->ID()
@@ -315,13 +315,13 @@  discard block
 block discarded – undo
315 315
         $ctxt = array();
316 316
         $context_templates = $item->context_templates();
317 317
         foreach ($context_templates as $context => $template_fields) {
318
-            $mtp_to = ! empty($context_templates[ $context ]['to'])
319
-                      && $context_templates[ $context ]['to'] instanceof EE_Message_Template
320
-                ? $context_templates[ $context ]['to']->get('MTP_content')
318
+            $mtp_to = ! empty($context_templates[$context]['to'])
319
+                      && $context_templates[$context]['to'] instanceof EE_Message_Template
320
+                ? $context_templates[$context]['to']->get('MTP_content')
321 321
                 : null;
322 322
             $inactive_class = (
323 323
                                   empty($mtp_to)
324
-                                  && ! empty($context_templates[ $context ]['to'])
324
+                                  && ! empty($context_templates[$context]['to'])
325 325
                               )
326 326
                               || ! $item->is_context_active($context)
327 327
                 ? ' mtp-inactive'
@@ -329,7 +329,7 @@  discard block
 block discarded – undo
329 329
             $context_title = sprintf(
330 330
                 /* translators: Placeholder represents the context label. Example "Edit Event Admin" */
331 331
                 esc_html__('Edit %1$s', 'event_espresso'),
332
-                ucwords($c_configs[ $context ]['label'])
332
+                ucwords($c_configs[$context]['label'])
333 333
             );
334 334
             $edit_link = EE_Admin_Page::add_query_args_and_nonce(
335 335
                 array(
@@ -340,14 +340,14 @@  discard block
 block discarded – undo
340 340
                 EE_MSG_ADMIN_URL
341 341
             );
342 342
             $ctxt[] = '<a'
343
-                      . ' href="' . $edit_link . '"'
344
-                      . ' class="' . $item->message_type() . '-' . $context . '-edit-link' . $inactive_class . '"'
345
-                      . ' title="' . esc_attr__('Edit Context', 'event_espresso') . '">'
343
+                      . ' href="'.$edit_link.'"'
344
+                      . ' class="'.$item->message_type().'-'.$context.'-edit-link'.$inactive_class.'"'
345
+                      . ' title="'.esc_attr__('Edit Context', 'event_espresso').'">'
346 346
                       . $context_title
347 347
                       . '</a>';
348 348
         }
349 349
 
350
-        return sprintf('<strong>%s:</strong> ', ucwords($c_label['plural'])) . implode(' | ', $ctxt);
350
+        return sprintf('<strong>%s:</strong> ', ucwords($c_label['plural'])).implode(' | ', $ctxt);
351 351
     }
352 352
 
353 353
 
Please login to merge, or discard this patch.
core/domain/entities/editor/blocks/EventAttendees.php 1 patch
Indentation   +157 added lines, -157 removed lines patch added patch discarded remove patch
@@ -21,172 +21,172 @@
 block discarded – undo
21 21
 class EventAttendees extends Block
22 22
 {
23 23
 
24
-    const BLOCK_TYPE = 'event-attendees';
24
+	const BLOCK_TYPE = 'event-attendees';
25 25
 
26
-    /**
27
-     * @var EventAttendeesBlockRenderer $renderer
28
-     */
29
-    protected $renderer;
26
+	/**
27
+	 * @var EventAttendeesBlockRenderer $renderer
28
+	 */
29
+	protected $renderer;
30 30
 
31 31
 
32
-    /**
33
-     * EventAttendees constructor.
34
-     *
35
-     * @param CoreBlocksAssetManager      $block_asset_manager
36
-     * @param RequestInterface            $request
37
-     * @param EventAttendeesBlockRenderer $renderer
38
-     */
39
-    public function __construct(
40
-        CoreBlocksAssetManager $block_asset_manager,
41
-        RequestInterface $request,
42
-        EventAttendeesBlockRenderer $renderer
43
-    ) {
44
-        parent::__construct($block_asset_manager, $request);
45
-        $this->renderer= $renderer;
46
-    }
32
+	/**
33
+	 * EventAttendees constructor.
34
+	 *
35
+	 * @param CoreBlocksAssetManager      $block_asset_manager
36
+	 * @param RequestInterface            $request
37
+	 * @param EventAttendeesBlockRenderer $renderer
38
+	 */
39
+	public function __construct(
40
+		CoreBlocksAssetManager $block_asset_manager,
41
+		RequestInterface $request,
42
+		EventAttendeesBlockRenderer $renderer
43
+	) {
44
+		parent::__construct($block_asset_manager, $request);
45
+		$this->renderer= $renderer;
46
+	}
47 47
 
48 48
 
49
-    /**
50
-     * Perform any early setup required by the block
51
-     * including setting the block type and supported post types
52
-     *
53
-     * @return void
54
-     */
55
-    public function initialize()
56
-    {
57
-        $this->setBlockType(self::BLOCK_TYPE);
58
-        $this->setSupportedRoutes(
59
-            array(
60
-                'EventEspresso\core\domain\entities\route_match\specifications\admin\EspressoStandardPostTypeEditor',
61
-                'EventEspresso\core\domain\entities\route_match\specifications\admin\WordPressPostTypeEditor',
62
-                'EventEspresso\core\domain\entities\route_match\specifications\frontend\EspressoBlockRenderer',
63
-                'EventEspresso\core\domain\entities\route_match\specifications\frontend\AnyFrontendRequest'
64
-            )
65
-        );
66
-        $EVT_ID = $this->request->getRequestParam('page') === 'espresso_events'
67
-            ? $this->request->getRequestParam('post', 0)
68
-            : 0;
69
-        $this->setAttributes(
70
-            array(
71
-                'eventId'           => array(
72
-                    'type'    => 'number',
73
-                    'default' => $EVT_ID,
74
-                ),
75
-                'datetimeId'        => array(
76
-                    'type'    => 'number',
77
-                    'default' => 0,
78
-                ),
79
-                'ticketId'          => array(
80
-                    'type'    => 'number',
81
-                    'default' => 0,
82
-                ),
83
-                'status'            => array(
84
-                    'type'    => 'string',
85
-                    'default' => EEM_Registration::status_id_approved,
86
-                ),
87
-                'limit'             => array(
88
-                    'type'    => 'number',
89
-                    'default' => 100,
90
-                ),
91
-                'order' => array(
92
-                    'type' => 'string',
93
-                    'default' => 'ASC'
94
-                ),
95
-                'orderBy' => array(
96
-                    'type' => 'string',
97
-                    'default' => 'lastThenFirstName',
98
-                ),
99
-                'showGravatar'      => array(
100
-                    'type'    => 'boolean',
101
-                    'default' => false,
102
-                ),
103
-                'avatarClass' => array(
104
-                    'type' => 'string',
105
-                    'default' => 'contact',
106
-                ),
107
-                'avatarSize' => array(
108
-                    'type' => 'number',
109
-                    'default' => 24,
110
-                ),
111
-                'displayOnArchives' => array(
112
-                    'type'    => 'boolean',
113
-                    'default' => false,
114
-                ),
115
-            )
116
-        );
117
-        $this->setDynamic();
118
-    }
49
+	/**
50
+	 * Perform any early setup required by the block
51
+	 * including setting the block type and supported post types
52
+	 *
53
+	 * @return void
54
+	 */
55
+	public function initialize()
56
+	{
57
+		$this->setBlockType(self::BLOCK_TYPE);
58
+		$this->setSupportedRoutes(
59
+			array(
60
+				'EventEspresso\core\domain\entities\route_match\specifications\admin\EspressoStandardPostTypeEditor',
61
+				'EventEspresso\core\domain\entities\route_match\specifications\admin\WordPressPostTypeEditor',
62
+				'EventEspresso\core\domain\entities\route_match\specifications\frontend\EspressoBlockRenderer',
63
+				'EventEspresso\core\domain\entities\route_match\specifications\frontend\AnyFrontendRequest'
64
+			)
65
+		);
66
+		$EVT_ID = $this->request->getRequestParam('page') === 'espresso_events'
67
+			? $this->request->getRequestParam('post', 0)
68
+			: 0;
69
+		$this->setAttributes(
70
+			array(
71
+				'eventId'           => array(
72
+					'type'    => 'number',
73
+					'default' => $EVT_ID,
74
+				),
75
+				'datetimeId'        => array(
76
+					'type'    => 'number',
77
+					'default' => 0,
78
+				),
79
+				'ticketId'          => array(
80
+					'type'    => 'number',
81
+					'default' => 0,
82
+				),
83
+				'status'            => array(
84
+					'type'    => 'string',
85
+					'default' => EEM_Registration::status_id_approved,
86
+				),
87
+				'limit'             => array(
88
+					'type'    => 'number',
89
+					'default' => 100,
90
+				),
91
+				'order' => array(
92
+					'type' => 'string',
93
+					'default' => 'ASC'
94
+				),
95
+				'orderBy' => array(
96
+					'type' => 'string',
97
+					'default' => 'lastThenFirstName',
98
+				),
99
+				'showGravatar'      => array(
100
+					'type'    => 'boolean',
101
+					'default' => false,
102
+				),
103
+				'avatarClass' => array(
104
+					'type' => 'string',
105
+					'default' => 'contact',
106
+				),
107
+				'avatarSize' => array(
108
+					'type' => 'number',
109
+					'default' => 24,
110
+				),
111
+				'displayOnArchives' => array(
112
+					'type'    => 'boolean',
113
+					'default' => false,
114
+				),
115
+			)
116
+		);
117
+		$this->setDynamic();
118
+	}
119 119
 
120 120
 
121
-    /**
122
-     * Returns an array where the key corresponds to the incoming attribute name from the WP block
123
-     * and the value corresponds to the attribute name for the existing EspressoEventAttendees shortcode
124
-     *
125
-     * @since 4.9.71.p
126
-     * @return array
127
-     */
128
-    private function getAttributesMap()
129
-    {
130
-        return array(
131
-            'eventId'           => 'absint',
132
-            'datetimeId'        => 'absint',
133
-            'ticketId'          => 'absint',
134
-            'status'            => 'sanitize_text_field',
135
-            'limit'             => 'intval',
136
-            'showGravatar'      => 'bool',
137
-            'avatarClass'       => 'sanitize_text_field',
138
-            'avatarSize'        => 'absint',
139
-            'displayOnArchives' => 'bool',
140
-            'order' => 'sanitize_text_field',
141
-            'orderBy' => 'sanitize_text_field',
142
-        );
143
-    }
121
+	/**
122
+	 * Returns an array where the key corresponds to the incoming attribute name from the WP block
123
+	 * and the value corresponds to the attribute name for the existing EspressoEventAttendees shortcode
124
+	 *
125
+	 * @since 4.9.71.p
126
+	 * @return array
127
+	 */
128
+	private function getAttributesMap()
129
+	{
130
+		return array(
131
+			'eventId'           => 'absint',
132
+			'datetimeId'        => 'absint',
133
+			'ticketId'          => 'absint',
134
+			'status'            => 'sanitize_text_field',
135
+			'limit'             => 'intval',
136
+			'showGravatar'      => 'bool',
137
+			'avatarClass'       => 'sanitize_text_field',
138
+			'avatarSize'        => 'absint',
139
+			'displayOnArchives' => 'bool',
140
+			'order' => 'sanitize_text_field',
141
+			'orderBy' => 'sanitize_text_field',
142
+		);
143
+	}
144 144
 
145 145
 
146
-    /**
147
-     * Sanitizes attributes.
148
-     *
149
-     * @param array $attributes
150
-     * @return array
151
-     */
152
-    private function sanitizeAttributes(array $attributes)
153
-    {
154
-        $sanitized_attributes = array();
155
-        foreach ($attributes as $attribute => $value) {
156
-            $convert = $this->getAttributesMap();
157
-            if (isset($convert[ $attribute ])) {
158
-                $sanitize = $convert[ $attribute ];
159
-                if ($sanitize === 'bool') {
160
-                    $sanitized_attributes[ $attribute ] = filter_var(
161
-                        $value,
162
-                        FILTER_VALIDATE_BOOLEAN
163
-                    );
164
-                } else {
165
-                    $sanitized_attributes[ $attribute ] = $sanitize($value);
166
-                }
167
-                // don't pass along attributes with a 0 value
168
-                if ($sanitized_attributes[ $attribute ] === 0) {
169
-                    unset($sanitized_attributes[ $attribute ]);
170
-                }
171
-            }
172
-        }
173
-        return $attributes;
174
-    }
146
+	/**
147
+	 * Sanitizes attributes.
148
+	 *
149
+	 * @param array $attributes
150
+	 * @return array
151
+	 */
152
+	private function sanitizeAttributes(array $attributes)
153
+	{
154
+		$sanitized_attributes = array();
155
+		foreach ($attributes as $attribute => $value) {
156
+			$convert = $this->getAttributesMap();
157
+			if (isset($convert[ $attribute ])) {
158
+				$sanitize = $convert[ $attribute ];
159
+				if ($sanitize === 'bool') {
160
+					$sanitized_attributes[ $attribute ] = filter_var(
161
+						$value,
162
+						FILTER_VALIDATE_BOOLEAN
163
+					);
164
+				} else {
165
+					$sanitized_attributes[ $attribute ] = $sanitize($value);
166
+				}
167
+				// don't pass along attributes with a 0 value
168
+				if ($sanitized_attributes[ $attribute ] === 0) {
169
+					unset($sanitized_attributes[ $attribute ]);
170
+				}
171
+			}
172
+		}
173
+		return $attributes;
174
+	}
175 175
 
176 176
 
177
-    /**
178
-     * Returns the rendered HTML for the block
179
-     *
180
-     * @param array $attributes
181
-     * @return string
182
-     * @throws DomainException
183
-     * @throws EE_Error
184
-     */
185
-    public function renderBlock(array $attributes = array())
186
-    {
187
-        $attributes = $this->sanitizeAttributes($attributes);
188
-        return (is_archive() || is_front_page() || is_home()) && ! $attributes['displayOnArchives']
189
-            ? ''
190
-            : $this->renderer->render($attributes);
191
-    }
177
+	/**
178
+	 * Returns the rendered HTML for the block
179
+	 *
180
+	 * @param array $attributes
181
+	 * @return string
182
+	 * @throws DomainException
183
+	 * @throws EE_Error
184
+	 */
185
+	public function renderBlock(array $attributes = array())
186
+	{
187
+		$attributes = $this->sanitizeAttributes($attributes);
188
+		return (is_archive() || is_front_page() || is_home()) && ! $attributes['displayOnArchives']
189
+			? ''
190
+			: $this->renderer->render($attributes);
191
+	}
192 192
 }
Please login to merge, or discard this patch.
espresso.php 1 patch
Indentation   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -38,103 +38,103 @@
 block discarded – undo
38 38
  * @since           4.0
39 39
  */
40 40
 if (function_exists('espresso_version')) {
41
-    if (! function_exists('espresso_duplicate_plugin_error')) {
42
-        /**
43
-         *    espresso_duplicate_plugin_error
44
-         *    displays if more than one version of EE is activated at the same time
45
-         */
46
-        function espresso_duplicate_plugin_error()
47
-        {
48
-            ?>
41
+	if (! function_exists('espresso_duplicate_plugin_error')) {
42
+		/**
43
+		 *    espresso_duplicate_plugin_error
44
+		 *    displays if more than one version of EE is activated at the same time
45
+		 */
46
+		function espresso_duplicate_plugin_error()
47
+		{
48
+			?>
49 49
             <div class="error">
50 50
                 <p>
51 51
                     <?php
52
-                    echo esc_html__(
53
-                        'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
-                        'event_espresso'
55
-                    ); ?>
52
+					echo esc_html__(
53
+						'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
+						'event_espresso'
55
+					); ?>
56 56
                 </p>
57 57
             </div>
58 58
             <?php
59
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
60
-        }
61
-    }
62
-    add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
59
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
60
+		}
61
+	}
62
+	add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
63 63
 } else {
64
-    define('EE_MIN_PHP_VER_REQUIRED', '5.4.0');
65
-    if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
-        /**
67
-         * espresso_minimum_php_version_error
68
-         *
69
-         * @return void
70
-         */
71
-        function espresso_minimum_php_version_error()
72
-        {
73
-            ?>
64
+	define('EE_MIN_PHP_VER_REQUIRED', '5.4.0');
65
+	if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
+		/**
67
+		 * espresso_minimum_php_version_error
68
+		 *
69
+		 * @return void
70
+		 */
71
+		function espresso_minimum_php_version_error()
72
+		{
73
+			?>
74 74
             <div class="error">
75 75
                 <p>
76 76
                     <?php
77
-                    printf(
78
-                        esc_html__(
79
-                            'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
-                            'event_espresso'
81
-                        ),
82
-                        EE_MIN_PHP_VER_REQUIRED,
83
-                        PHP_VERSION,
84
-                        '<br/>',
85
-                        '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
-                    );
87
-                    ?>
77
+					printf(
78
+						esc_html__(
79
+							'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
+							'event_espresso'
81
+						),
82
+						EE_MIN_PHP_VER_REQUIRED,
83
+						PHP_VERSION,
84
+						'<br/>',
85
+						'<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
+					);
87
+					?>
88 88
                 </p>
89 89
             </div>
90 90
             <?php
91
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
92
-        }
91
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
92
+		}
93 93
 
94
-        add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
-    } else {
96
-        define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
-        /**
98
-         * espresso_version
99
-         * Returns the plugin version
100
-         *
101
-         * @return string
102
-         */
103
-        function espresso_version()
104
-        {
105
-            return apply_filters('FHEE__espresso__espresso_version', '4.9.78.rc.013');
106
-        }
94
+		add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
+	} else {
96
+		define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
+		/**
98
+		 * espresso_version
99
+		 * Returns the plugin version
100
+		 *
101
+		 * @return string
102
+		 */
103
+		function espresso_version()
104
+		{
105
+			return apply_filters('FHEE__espresso__espresso_version', '4.9.78.rc.013');
106
+		}
107 107
 
108
-        /**
109
-         * espresso_plugin_activation
110
-         * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
-         */
112
-        function espresso_plugin_activation()
113
-        {
114
-            update_option('ee_espresso_activation', true);
115
-        }
108
+		/**
109
+		 * espresso_plugin_activation
110
+		 * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
+		 */
112
+		function espresso_plugin_activation()
113
+		{
114
+			update_option('ee_espresso_activation', true);
115
+		}
116 116
 
117
-        register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
117
+		register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
118 118
 
119
-        require_once __DIR__ . '/core/bootstrap_espresso.php';
120
-        bootstrap_espresso();
121
-    }
119
+		require_once __DIR__ . '/core/bootstrap_espresso.php';
120
+		bootstrap_espresso();
121
+	}
122 122
 }
123 123
 if (! function_exists('espresso_deactivate_plugin')) {
124
-    /**
125
-     *    deactivate_plugin
126
-     * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
-     *
128
-     * @access public
129
-     * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
-     * @return    void
131
-     */
132
-    function espresso_deactivate_plugin($plugin_basename = '')
133
-    {
134
-        if (! function_exists('deactivate_plugins')) {
135
-            require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
-        }
137
-        unset($_GET['activate'], $_REQUEST['activate']);
138
-        deactivate_plugins($plugin_basename);
139
-    }
124
+	/**
125
+	 *    deactivate_plugin
126
+	 * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
+	 *
128
+	 * @access public
129
+	 * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
+	 * @return    void
131
+	 */
132
+	function espresso_deactivate_plugin($plugin_basename = '')
133
+	{
134
+		if (! function_exists('deactivate_plugins')) {
135
+			require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
+		}
137
+		unset($_GET['activate'], $_REQUEST['activate']);
138
+		deactivate_plugins($plugin_basename);
139
+	}
140 140
 }
Please login to merge, or discard this patch.
core/db_classes/EE_Registration.class.php 1 patch
Indentation   +2071 added lines, -2071 removed lines patch added patch discarded remove patch
@@ -17,2075 +17,2075 @@
 block discarded – undo
17 17
 {
18 18
 
19 19
 
20
-    /**
21
-     * Used to reference when a registration has never been checked in.
22
-     *
23
-     * @deprecated use \EE_Checkin::status_checked_never instead
24
-     * @type int
25
-     */
26
-    const checkin_status_never = 2;
27
-
28
-    /**
29
-     * Used to reference when a registration has been checked in.
30
-     *
31
-     * @deprecated use \EE_Checkin::status_checked_in instead
32
-     * @type int
33
-     */
34
-    const checkin_status_in = 1;
35
-
36
-
37
-    /**
38
-     * Used to reference when a registration has been checked out.
39
-     *
40
-     * @deprecated use \EE_Checkin::status_checked_out instead
41
-     * @type int
42
-     */
43
-    const checkin_status_out = 0;
44
-
45
-
46
-    /**
47
-     * extra meta key for tracking reg status os trashed registrations
48
-     *
49
-     * @type string
50
-     */
51
-    const PRE_TRASH_REG_STATUS_KEY = 'pre_trash_registration_status';
52
-
53
-
54
-    /**
55
-     * extra meta key for tracking if registration has reserved ticket
56
-     *
57
-     * @type string
58
-     */
59
-    const HAS_RESERVED_TICKET_KEY = 'has_reserved_ticket';
60
-
61
-
62
-    /**
63
-     * @param array  $props_n_values          incoming values
64
-     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
65
-     *                                        used.)
66
-     * @param array  $date_formats            incoming date_formats in an array where the first value is the
67
-     *                                        date_format and the second value is the time format
68
-     * @return EE_Registration
69
-     * @throws EE_Error
70
-     */
71
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
72
-    {
73
-        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
74
-        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
75
-    }
76
-
77
-
78
-    /**
79
-     * @param array  $props_n_values  incoming values from the database
80
-     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
81
-     *                                the website will be used.
82
-     * @return EE_Registration
83
-     */
84
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
85
-    {
86
-        return new self($props_n_values, true, $timezone);
87
-    }
88
-
89
-
90
-    /**
91
-     *        Set Event ID
92
-     *
93
-     * @param        int $EVT_ID Event ID
94
-     * @throws EE_Error
95
-     * @throws RuntimeException
96
-     */
97
-    public function set_event($EVT_ID = 0)
98
-    {
99
-        $this->set('EVT_ID', $EVT_ID);
100
-    }
101
-
102
-
103
-    /**
104
-     * Overrides parent set() method so that all calls to set( 'REG_code', $REG_code ) OR set( 'STS_ID', $STS_ID ) can
105
-     * be routed to internal methods
106
-     *
107
-     * @param string $field_name
108
-     * @param mixed  $field_value
109
-     * @param bool   $use_default
110
-     * @throws EE_Error
111
-     * @throws EntityNotFoundException
112
-     * @throws InvalidArgumentException
113
-     * @throws InvalidDataTypeException
114
-     * @throws InvalidInterfaceException
115
-     * @throws ReflectionException
116
-     * @throws RuntimeException
117
-     */
118
-    public function set($field_name, $field_value, $use_default = false)
119
-    {
120
-        switch ($field_name) {
121
-            case 'REG_code':
122
-                if (! empty($field_value) && $this->reg_code() === null) {
123
-                    $this->set_reg_code($field_value, $use_default);
124
-                }
125
-                break;
126
-            case 'STS_ID':
127
-                $this->set_status($field_value, $use_default);
128
-                break;
129
-            default:
130
-                parent::set($field_name, $field_value, $use_default);
131
-        }
132
-    }
133
-
134
-
135
-    /**
136
-     * Set Status ID
137
-     * updates the registration status and ALSO...
138
-     * calls reserve_registration_space() if the reg status changes TO approved from any other reg status
139
-     * calls release_registration_space() if the reg status changes FROM approved to any other reg status
140
-     *
141
-     * @param string                $new_STS_ID
142
-     * @param boolean               $use_default
143
-     * @param ContextInterface|null $context
144
-     * @return bool
145
-     * @throws DomainException
146
-     * @throws EE_Error
147
-     * @throws EntityNotFoundException
148
-     * @throws InvalidArgumentException
149
-     * @throws InvalidDataTypeException
150
-     * @throws InvalidInterfaceException
151
-     * @throws ReflectionException
152
-     * @throws RuntimeException
153
-     * @throws UnexpectedEntityException
154
-     */
155
-    public function set_status($new_STS_ID = null, $use_default = false, ContextInterface $context = null)
156
-    {
157
-        // get current REG_Status
158
-        $old_STS_ID = $this->status_ID();
159
-        // if status has changed
160
-        if ($old_STS_ID !== $new_STS_ID // and that status has actually changed
161
-            && ! empty($old_STS_ID) // and that old status is actually set
162
-            && ! empty($new_STS_ID) // as well as the new status
163
-            && $this->ID() // ensure registration is in the db
164
-        ) {
165
-            // update internal status first
166
-            parent::set('STS_ID', $new_STS_ID, $use_default);
167
-            // THEN handle other changes that occur when reg status changes
168
-            // TO approved
169
-            if ($new_STS_ID === EEM_Registration::status_id_approved) {
170
-                // reserve a space by incrementing ticket and datetime sold values
171
-                $this->reserveRegistrationSpace();
172
-                do_action('AHEE__EE_Registration__set_status__to_approved', $this, $old_STS_ID, $new_STS_ID, $context);
173
-                // OR FROM  approved
174
-            } elseif ($old_STS_ID === EEM_Registration::status_id_approved) {
175
-                // release a space by decrementing ticket and datetime sold values
176
-                $this->releaseRegistrationSpace();
177
-                do_action(
178
-                    'AHEE__EE_Registration__set_status__from_approved',
179
-                    $this,
180
-                    $old_STS_ID,
181
-                    $new_STS_ID,
182
-                    $context
183
-                );
184
-            }
185
-            // update status
186
-            parent::set('STS_ID', $new_STS_ID, $use_default);
187
-            $this->updateIfCanceledOrReinstated($new_STS_ID, $old_STS_ID, $context);
188
-            if ($this->statusChangeUpdatesTransaction($context)) {
189
-                $this->updateTransactionAfterStatusChange();
190
-            }
191
-            do_action('AHEE__EE_Registration__set_status__after_update', $this, $old_STS_ID, $new_STS_ID, $context);
192
-            return true;
193
-        }
194
-        // even though the old value matches the new value, it's still good to
195
-        // allow the parent set method to have a say
196
-        parent::set('STS_ID', $new_STS_ID, $use_default);
197
-        return true;
198
-    }
199
-
200
-
201
-    /**
202
-     * update REGs and TXN when cancelled or declined registrations involved
203
-     *
204
-     * @param string                $new_STS_ID
205
-     * @param string                $old_STS_ID
206
-     * @param ContextInterface|null $context
207
-     * @throws EE_Error
208
-     * @throws InvalidArgumentException
209
-     * @throws InvalidDataTypeException
210
-     * @throws InvalidInterfaceException
211
-     * @throws ReflectionException
212
-     * @throws RuntimeException
213
-     */
214
-    private function updateIfCanceledOrReinstated($new_STS_ID, $old_STS_ID, ContextInterface $context = null)
215
-    {
216
-        // these reg statuses should not be considered in any calculations involving monies owing
217
-        $closed_reg_statuses = EEM_Registration::closed_reg_statuses();
218
-        // true if registration has been cancelled or declined
219
-        $this->updateIfCanceled(
220
-            $closed_reg_statuses,
221
-            $new_STS_ID,
222
-            $old_STS_ID,
223
-            $context
224
-        );
225
-        $this->updateIfReinstated(
226
-            $closed_reg_statuses,
227
-            $new_STS_ID,
228
-            $old_STS_ID,
229
-            $context
230
-        );
231
-    }
232
-
233
-
234
-    /**
235
-     * update REGs and TXN when cancelled or declined registrations involved
236
-     *
237
-     * @param array                 $closed_reg_statuses
238
-     * @param string                $new_STS_ID
239
-     * @param string                $old_STS_ID
240
-     * @param ContextInterface|null $context
241
-     * @throws EE_Error
242
-     * @throws InvalidArgumentException
243
-     * @throws InvalidDataTypeException
244
-     * @throws InvalidInterfaceException
245
-     * @throws ReflectionException
246
-     * @throws RuntimeException
247
-     */
248
-    private function updateIfCanceled(
249
-        array $closed_reg_statuses,
250
-        $new_STS_ID,
251
-        $old_STS_ID,
252
-        ContextInterface $context = null
253
-    ) {
254
-        // true if registration has been cancelled or declined
255
-        if (in_array($new_STS_ID, $closed_reg_statuses, true)
256
-            && ! in_array($old_STS_ID, $closed_reg_statuses, true)
257
-        ) {
258
-            /** @type EE_Registration_Processor $registration_processor */
259
-            $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
260
-            /** @type EE_Transaction_Processor $transaction_processor */
261
-            $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
262
-            // cancelled or declined registration
263
-            $registration_processor->update_registration_after_being_canceled_or_declined(
264
-                $this,
265
-                $closed_reg_statuses
266
-            );
267
-            $transaction_processor->update_transaction_after_canceled_or_declined_registration(
268
-                $this,
269
-                $closed_reg_statuses,
270
-                false
271
-            );
272
-            do_action(
273
-                'AHEE__EE_Registration__set_status__canceled_or_declined',
274
-                $this,
275
-                $old_STS_ID,
276
-                $new_STS_ID,
277
-                $context
278
-            );
279
-            return;
280
-        }
281
-    }
282
-
283
-
284
-    /**
285
-     * update REGs and TXN when cancelled or declined registrations involved
286
-     *
287
-     * @param array                 $closed_reg_statuses
288
-     * @param string                $new_STS_ID
289
-     * @param string                $old_STS_ID
290
-     * @param ContextInterface|null $context
291
-     * @throws EE_Error
292
-     * @throws InvalidArgumentException
293
-     * @throws InvalidDataTypeException
294
-     * @throws InvalidInterfaceException
295
-     * @throws ReflectionException
296
-     */
297
-    private function updateIfReinstated(
298
-        array $closed_reg_statuses,
299
-        $new_STS_ID,
300
-        $old_STS_ID,
301
-        ContextInterface $context = null
302
-    ) {
303
-        // true if reinstating cancelled or declined registration
304
-        if (in_array($old_STS_ID, $closed_reg_statuses, true)
305
-            && ! in_array($new_STS_ID, $closed_reg_statuses, true)
306
-        ) {
307
-            /** @type EE_Registration_Processor $registration_processor */
308
-            $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
309
-            /** @type EE_Transaction_Processor $transaction_processor */
310
-            $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
311
-            // reinstating cancelled or declined registration
312
-            $registration_processor->update_canceled_or_declined_registration_after_being_reinstated(
313
-                $this,
314
-                $closed_reg_statuses
315
-            );
316
-            $transaction_processor->update_transaction_after_reinstating_canceled_registration(
317
-                $this,
318
-                $closed_reg_statuses,
319
-                false
320
-            );
321
-            do_action(
322
-                'AHEE__EE_Registration__set_status__after_reinstated',
323
-                $this,
324
-                $old_STS_ID,
325
-                $new_STS_ID,
326
-                $context
327
-            );
328
-        }
329
-    }
330
-
331
-
332
-    /**
333
-     * @param ContextInterface|null $context
334
-     * @return bool
335
-     */
336
-    private function statusChangeUpdatesTransaction(ContextInterface $context = null)
337
-    {
338
-        $contexts_that_do_not_update_transaction = (array) apply_filters(
339
-            'AHEE__EE_Registration__statusChangeUpdatesTransaction__contexts_that_do_not_update_transaction',
340
-            array('spco_reg_step_attendee_information_process_registrations'),
341
-            $context,
342
-            $this
343
-        );
344
-        return ! (
345
-            $context instanceof ContextInterface
346
-            && in_array($context->slug(), $contexts_that_do_not_update_transaction, true)
347
-        );
348
-    }
349
-
350
-
351
-    /**
352
-     * @throws EE_Error
353
-     * @throws EntityNotFoundException
354
-     * @throws InvalidArgumentException
355
-     * @throws InvalidDataTypeException
356
-     * @throws InvalidInterfaceException
357
-     * @throws ReflectionException
358
-     * @throws RuntimeException
359
-     */
360
-    private function updateTransactionAfterStatusChange()
361
-    {
362
-        /** @type EE_Transaction_Payments $transaction_payments */
363
-        $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
364
-        $transaction_payments->recalculate_transaction_total($this->transaction(), false);
365
-        $this->transaction()->update_status_based_on_total_paid(true);
366
-    }
367
-
368
-
369
-    /**
370
-     *        get Status ID
371
-     */
372
-    public function status_ID()
373
-    {
374
-        return $this->get('STS_ID');
375
-    }
376
-
377
-
378
-    /**
379
-     * Gets the ticket this registration is for
380
-     *
381
-     * @param boolean $include_archived whether to include archived tickets or not.
382
-     *
383
-     * @return EE_Ticket|EE_Base_Class
384
-     * @throws EE_Error
385
-     */
386
-    public function ticket($include_archived = true)
387
-    {
388
-        $query_params = array();
389
-        if ($include_archived) {
390
-            $query_params['default_where_conditions'] = 'none';
391
-        }
392
-        return $this->get_first_related('Ticket', $query_params);
393
-    }
394
-
395
-
396
-    /**
397
-     * Gets the event this registration is for
398
-     *
399
-     * @return EE_Event
400
-     * @throws EE_Error
401
-     * @throws EntityNotFoundException
402
-     */
403
-    public function event()
404
-    {
405
-        $event = $this->get_first_related('Event');
406
-        if (! $event instanceof \EE_Event) {
407
-            throw new EntityNotFoundException('Event ID', $this->event_ID());
408
-        }
409
-        return $event;
410
-    }
411
-
412
-
413
-    /**
414
-     * Gets the "author" of the registration.  Note that for the purposes of registrations, the author will correspond
415
-     * with the author of the event this registration is for.
416
-     *
417
-     * @since 4.5.0
418
-     * @return int
419
-     * @throws EE_Error
420
-     * @throws EntityNotFoundException
421
-     */
422
-    public function wp_user()
423
-    {
424
-        $event = $this->event();
425
-        if ($event instanceof EE_Event) {
426
-            return $event->wp_user();
427
-        }
428
-        return 0;
429
-    }
430
-
431
-
432
-    /**
433
-     * increments this registration's related ticket sold and corresponding datetime sold values
434
-     *
435
-     * @return void
436
-     * @throws DomainException
437
-     * @throws EE_Error
438
-     * @throws EntityNotFoundException
439
-     * @throws InvalidArgumentException
440
-     * @throws InvalidDataTypeException
441
-     * @throws InvalidInterfaceException
442
-     * @throws ReflectionException
443
-     * @throws UnexpectedEntityException
444
-     */
445
-    private function reserveRegistrationSpace()
446
-    {
447
-        // reserved ticket and datetime counts will be decremented as sold counts are incremented
448
-        // so stop tracking that this reg has a ticket reserved
449
-        $this->release_reserved_ticket(false, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
450
-        $ticket = $this->ticket();
451
-        $ticket->increaseSold();
452
-        // possibly set event status to sold out
453
-        $this->event()->perform_sold_out_status_check();
454
-    }
455
-
456
-
457
-    /**
458
-     * decrements (subtracts) this registration's related ticket sold and corresponding datetime sold values
459
-     *
460
-     * @return void
461
-     * @throws DomainException
462
-     * @throws EE_Error
463
-     * @throws EntityNotFoundException
464
-     * @throws InvalidArgumentException
465
-     * @throws InvalidDataTypeException
466
-     * @throws InvalidInterfaceException
467
-     * @throws ReflectionException
468
-     * @throws UnexpectedEntityException
469
-     */
470
-    private function releaseRegistrationSpace()
471
-    {
472
-        $ticket = $this->ticket();
473
-        $ticket->decreaseSold();
474
-        // possibly change event status from sold out back to previous status
475
-        $this->event()->perform_sold_out_status_check();
476
-    }
477
-
478
-
479
-    /**
480
-     * tracks this registration's ticket reservation in extra meta
481
-     * and can increment related ticket reserved and corresponding datetime reserved values
482
-     *
483
-     * @param bool $update_ticket if true, will increment ticket and datetime reserved count
484
-     * @return void
485
-     * @throws EE_Error
486
-     * @throws InvalidArgumentException
487
-     * @throws InvalidDataTypeException
488
-     * @throws InvalidInterfaceException
489
-     * @throws ReflectionException
490
-     */
491
-    public function reserve_ticket($update_ticket = false, $source = 'unknown')
492
-    {
493
-        // only reserve ticket if space is not currently reserved
494
-        if ((bool) $this->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true) !== true) {
495
-            $this->update_extra_meta('reserve_ticket', "{$this->ticket_ID()} from {$source}");
496
-            // IMPORTANT !!!
497
-            // although checking $update_ticket first would be more efficient,
498
-            // we NEED to ALWAYS call update_extra_meta(), which is why that is done first
499
-            if ($this->update_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true)
500
-                && $update_ticket
501
-            ) {
502
-                $ticket = $this->ticket();
503
-                $ticket->increaseReserved(1, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
504
-                $ticket->save();
505
-            }
506
-        }
507
-    }
508
-
509
-
510
-    /**
511
-     * stops tracking this registration's ticket reservation in extra meta
512
-     * decrements (subtracts) related ticket reserved and corresponding datetime reserved values
513
-     *
514
-     * @param bool $update_ticket if true, will decrement ticket and datetime reserved count
515
-     * @return void
516
-     * @throws EE_Error
517
-     * @throws InvalidArgumentException
518
-     * @throws InvalidDataTypeException
519
-     * @throws InvalidInterfaceException
520
-     * @throws ReflectionException
521
-     */
522
-    public function release_reserved_ticket($update_ticket = false, $source = 'unknown')
523
-    {
524
-        // only release ticket if space is currently reserved
525
-        if ((bool) $this->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true) === true) {
526
-            $this->update_extra_meta('release_reserved_ticket', "{$this->ticket_ID()} from {$source}");
527
-            // IMPORTANT !!!
528
-            // although checking $update_ticket first would be more efficient,
529
-            // we NEED to ALWAYS call update_extra_meta(), which is why that is done first
530
-            if ($this->update_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, false)
531
-                && $update_ticket
532
-            ) {
533
-                $ticket = $this->ticket();
534
-                $ticket->decreaseReserved(1, true, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
535
-            }
536
-        }
537
-    }
538
-
539
-
540
-    /**
541
-     * Set Attendee ID
542
-     *
543
-     * @param        int $ATT_ID Attendee ID
544
-     * @throws EE_Error
545
-     * @throws RuntimeException
546
-     */
547
-    public function set_attendee_id($ATT_ID = 0)
548
-    {
549
-        $this->set('ATT_ID', $ATT_ID);
550
-    }
551
-
552
-
553
-    /**
554
-     *        Set Transaction ID
555
-     *
556
-     * @param        int $TXN_ID Transaction ID
557
-     * @throws EE_Error
558
-     * @throws RuntimeException
559
-     */
560
-    public function set_transaction_id($TXN_ID = 0)
561
-    {
562
-        $this->set('TXN_ID', $TXN_ID);
563
-    }
564
-
565
-
566
-    /**
567
-     *        Set Session
568
-     *
569
-     * @param    string $REG_session PHP Session ID
570
-     * @throws EE_Error
571
-     * @throws RuntimeException
572
-     */
573
-    public function set_session($REG_session = '')
574
-    {
575
-        $this->set('REG_session', $REG_session);
576
-    }
577
-
578
-
579
-    /**
580
-     *        Set Registration URL Link
581
-     *
582
-     * @param    string $REG_url_link Registration URL Link
583
-     * @throws EE_Error
584
-     * @throws RuntimeException
585
-     */
586
-    public function set_reg_url_link($REG_url_link = '')
587
-    {
588
-        $this->set('REG_url_link', $REG_url_link);
589
-    }
590
-
591
-
592
-    /**
593
-     *        Set Attendee Counter
594
-     *
595
-     * @param        int $REG_count Primary Attendee
596
-     * @throws EE_Error
597
-     * @throws RuntimeException
598
-     */
599
-    public function set_count($REG_count = 1)
600
-    {
601
-        $this->set('REG_count', $REG_count);
602
-    }
603
-
604
-
605
-    /**
606
-     *        Set Group Size
607
-     *
608
-     * @param        boolean $REG_group_size Group Registration
609
-     * @throws EE_Error
610
-     * @throws RuntimeException
611
-     */
612
-    public function set_group_size($REG_group_size = false)
613
-    {
614
-        $this->set('REG_group_size', $REG_group_size);
615
-    }
616
-
617
-
618
-    /**
619
-     *    is_not_approved -  convenience method that returns TRUE if REG status ID ==
620
-     *    EEM_Registration::status_id_not_approved
621
-     *
622
-     * @return        boolean
623
-     */
624
-    public function is_not_approved()
625
-    {
626
-        return $this->status_ID() == EEM_Registration::status_id_not_approved ? true : false;
627
-    }
628
-
629
-
630
-    /**
631
-     *    is_pending_payment -  convenience method that returns TRUE if REG status ID ==
632
-     *    EEM_Registration::status_id_pending_payment
633
-     *
634
-     * @return        boolean
635
-     */
636
-    public function is_pending_payment()
637
-    {
638
-        return $this->status_ID() == EEM_Registration::status_id_pending_payment ? true : false;
639
-    }
640
-
641
-
642
-    /**
643
-     *    is_approved -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_approved
644
-     *
645
-     * @return        boolean
646
-     */
647
-    public function is_approved()
648
-    {
649
-        return $this->status_ID() == EEM_Registration::status_id_approved ? true : false;
650
-    }
651
-
652
-
653
-    /**
654
-     *    is_cancelled -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_cancelled
655
-     *
656
-     * @return        boolean
657
-     */
658
-    public function is_cancelled()
659
-    {
660
-        return $this->status_ID() == EEM_Registration::status_id_cancelled ? true : false;
661
-    }
662
-
663
-
664
-    /**
665
-     *    is_declined -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_declined
666
-     *
667
-     * @return        boolean
668
-     */
669
-    public function is_declined()
670
-    {
671
-        return $this->status_ID() == EEM_Registration::status_id_declined ? true : false;
672
-    }
673
-
674
-
675
-    /**
676
-     *    is_incomplete -  convenience method that returns TRUE if REG status ID ==
677
-     *    EEM_Registration::status_id_incomplete
678
-     *
679
-     * @return        boolean
680
-     */
681
-    public function is_incomplete()
682
-    {
683
-        return $this->status_ID() == EEM_Registration::status_id_incomplete ? true : false;
684
-    }
685
-
686
-
687
-    /**
688
-     *        Set Registration Date
689
-     *
690
-     * @param        mixed ( int or string ) $REG_date Registration Date - Unix timestamp or string representation of
691
-     *                                                 Date
692
-     * @throws EE_Error
693
-     * @throws RuntimeException
694
-     */
695
-    public function set_reg_date($REG_date = false)
696
-    {
697
-        $this->set('REG_date', $REG_date);
698
-    }
699
-
700
-
701
-    /**
702
-     *    Set final price owing for this registration after all ticket/price modifications
703
-     *
704
-     * @access    public
705
-     * @param    float $REG_final_price
706
-     * @throws EE_Error
707
-     * @throws RuntimeException
708
-     */
709
-    public function set_final_price($REG_final_price = 0.00)
710
-    {
711
-        $this->set('REG_final_price', $REG_final_price);
712
-    }
713
-
714
-
715
-    /**
716
-     *    Set amount paid towards this registration's final price
717
-     *
718
-     * @access    public
719
-     * @param    float $REG_paid
720
-     * @throws EE_Error
721
-     * @throws RuntimeException
722
-     */
723
-    public function set_paid($REG_paid = 0.00)
724
-    {
725
-        $this->set('REG_paid', $REG_paid);
726
-    }
727
-
728
-
729
-    /**
730
-     *        Attendee Is Going
731
-     *
732
-     * @param        boolean $REG_att_is_going Attendee Is Going
733
-     * @throws EE_Error
734
-     * @throws RuntimeException
735
-     */
736
-    public function set_att_is_going($REG_att_is_going = false)
737
-    {
738
-        $this->set('REG_att_is_going', $REG_att_is_going);
739
-    }
740
-
741
-
742
-    /**
743
-     * Gets the related attendee
744
-     *
745
-     * @return EE_Attendee
746
-     * @throws EE_Error
747
-     */
748
-    public function attendee()
749
-    {
750
-        return $this->get_first_related('Attendee');
751
-    }
752
-
753
-
754
-    /**
755
-     *        get Event ID
756
-     */
757
-    public function event_ID()
758
-    {
759
-        return $this->get('EVT_ID');
760
-    }
761
-
762
-
763
-    /**
764
-     *        get Event ID
765
-     */
766
-    public function event_name()
767
-    {
768
-        $event = $this->event_obj();
769
-        if ($event) {
770
-            return $event->name();
771
-        } else {
772
-            return null;
773
-        }
774
-    }
775
-
776
-
777
-    /**
778
-     * Fetches the event this registration is for
779
-     *
780
-     * @return EE_Event
781
-     * @throws EE_Error
782
-     */
783
-    public function event_obj()
784
-    {
785
-        return $this->get_first_related('Event');
786
-    }
787
-
788
-
789
-    /**
790
-     *        get Attendee ID
791
-     */
792
-    public function attendee_ID()
793
-    {
794
-        return $this->get('ATT_ID');
795
-    }
796
-
797
-
798
-    /**
799
-     *        get PHP Session ID
800
-     */
801
-    public function session_ID()
802
-    {
803
-        return $this->get('REG_session');
804
-    }
805
-
806
-
807
-    /**
808
-     * Gets the string which represents the URL trigger for the receipt template in the message template system.
809
-     *
810
-     * @param string $messenger 'pdf' or 'html'.  Default 'html'.
811
-     * @return string
812
-     */
813
-    public function receipt_url($messenger = 'html')
814
-    {
815
-
816
-        /**
817
-         * The below will be deprecated one version after this.  We check first if there is a custom receipt template
818
-         * already in use on old system.  If there is then we just return the standard url for it.
819
-         *
820
-         * @since 4.5.0
821
-         */
822
-        $template_relative_path = 'modules/gateways/Invoice/lib/templates/receipt_body.template.php';
823
-        $has_custom = EEH_Template::locate_template(
824
-            $template_relative_path,
825
-            array(),
826
-            true,
827
-            true,
828
-            true
829
-        );
830
-
831
-        if ($has_custom) {
832
-            return add_query_arg(array('receipt' => 'true'), $this->invoice_url('launch'));
833
-        }
834
-        return apply_filters('FHEE__EE_Registration__receipt_url__receipt_url', '', $this, $messenger, 'receipt');
835
-    }
836
-
837
-
838
-    /**
839
-     * Gets the string which represents the URL trigger for the invoice template in the message template system.
840
-     *
841
-     * @param string $messenger 'pdf' or 'html'.  Default 'html'.
842
-     * @return string
843
-     * @throws EE_Error
844
-     */
845
-    public function invoice_url($messenger = 'html')
846
-    {
847
-        /**
848
-         * The below will be deprecated one version after this.  We check first if there is a custom invoice template
849
-         * already in use on old system.  If there is then we just return the standard url for it.
850
-         *
851
-         * @since 4.5.0
852
-         */
853
-        $template_relative_path = 'modules/gateways/Invoice/lib/templates/invoice_body.template.php';
854
-        $has_custom = EEH_Template::locate_template(
855
-            $template_relative_path,
856
-            array(),
857
-            true,
858
-            true,
859
-            true
860
-        );
861
-
862
-        if ($has_custom) {
863
-            if ($messenger == 'html') {
864
-                return $this->invoice_url('launch');
865
-            }
866
-            $route = $messenger == 'download' || $messenger == 'pdf' ? 'download_invoice' : 'launch_invoice';
867
-
868
-            $query_args = array('ee' => $route, 'id' => $this->reg_url_link());
869
-            if ($messenger == 'html') {
870
-                $query_args['html'] = true;
871
-            }
872
-            return add_query_arg($query_args, get_permalink(EE_Registry::instance()->CFG->core->thank_you_page_id));
873
-        }
874
-        return apply_filters('FHEE__EE_Registration__invoice_url__invoice_url', '', $this, $messenger, 'invoice');
875
-    }
876
-
877
-
878
-    /**
879
-     * get Registration URL Link
880
-     *
881
-     * @access public
882
-     * @return string
883
-     * @throws EE_Error
884
-     */
885
-    public function reg_url_link()
886
-    {
887
-        return (string) $this->get('REG_url_link');
888
-    }
889
-
890
-
891
-    /**
892
-     * Echoes out invoice_url()
893
-     *
894
-     * @param string $type 'download','launch', or 'html' (default is 'launch')
895
-     * @return void
896
-     * @throws EE_Error
897
-     */
898
-    public function e_invoice_url($type = 'launch')
899
-    {
900
-        echo $this->invoice_url($type);
901
-    }
902
-
903
-
904
-    /**
905
-     * Echoes out payment_overview_url
906
-     */
907
-    public function e_payment_overview_url()
908
-    {
909
-        echo $this->payment_overview_url();
910
-    }
911
-
912
-
913
-    /**
914
-     * Gets the URL for the checkout payment options reg step
915
-     * with this registration's REG_url_link added as a query parameter
916
-     *
917
-     * @param bool $clear_session Set to true when you want to clear the session on revisiting the
918
-     *                            payment overview url.
919
-     * @return string
920
-     * @throws InvalidInterfaceException
921
-     * @throws InvalidDataTypeException
922
-     * @throws EE_Error
923
-     * @throws InvalidArgumentException
924
-     */
925
-    public function payment_overview_url($clear_session = false)
926
-    {
927
-        return add_query_arg(
928
-            (array) apply_filters(
929
-                'FHEE__EE_Registration__payment_overview_url__query_args',
930
-                array(
931
-                    'e_reg_url_link' => $this->reg_url_link(),
932
-                    'step'           => 'payment_options',
933
-                    'revisit'        => true,
934
-                    'clear_session'  => (bool) $clear_session,
935
-                ),
936
-                $this
937
-            ),
938
-            EE_Registry::instance()->CFG->core->reg_page_url()
939
-        );
940
-    }
941
-
942
-
943
-    /**
944
-     * Gets the URL for the checkout attendee information reg step
945
-     * with this registration's REG_url_link added as a query parameter
946
-     *
947
-     * @return string
948
-     * @throws InvalidInterfaceException
949
-     * @throws InvalidDataTypeException
950
-     * @throws EE_Error
951
-     * @throws InvalidArgumentException
952
-     */
953
-    public function edit_attendee_information_url()
954
-    {
955
-        return add_query_arg(
956
-            (array) apply_filters(
957
-                'FHEE__EE_Registration__edit_attendee_information_url__query_args',
958
-                array(
959
-                    'e_reg_url_link' => $this->reg_url_link(),
960
-                    'step'           => 'attendee_information',
961
-                    'revisit'        => true,
962
-                ),
963
-                $this
964
-            ),
965
-            EE_Registry::instance()->CFG->core->reg_page_url()
966
-        );
967
-    }
968
-
969
-
970
-    /**
971
-     * Simply generates and returns the appropriate admin_url link to edit this registration
972
-     *
973
-     * @return string
974
-     * @throws EE_Error
975
-     */
976
-    public function get_admin_edit_url()
977
-    {
978
-        return EEH_URL::add_query_args_and_nonce(
979
-            array(
980
-                'page'    => 'espresso_registrations',
981
-                'action'  => 'view_registration',
982
-                '_REG_ID' => $this->ID(),
983
-            ),
984
-            admin_url('admin.php')
985
-        );
986
-    }
987
-
988
-
989
-    /**
990
-     *    is_primary_registrant?
991
-     */
992
-    public function is_primary_registrant()
993
-    {
994
-        return $this->get('REG_count') == 1 ? true : false;
995
-    }
996
-
997
-
998
-    /**
999
-     * This returns the primary registration object for this registration group (which may be this object).
1000
-     *
1001
-     * @return EE_Registration
1002
-     * @throws EE_Error
1003
-     */
1004
-    public function get_primary_registration()
1005
-    {
1006
-        if ($this->is_primary_registrant()) {
1007
-            return $this;
1008
-        }
1009
-
1010
-        // k reg_count !== 1 so let's get the EE_Registration object matching this txn_id and reg_count == 1
1011
-        /** @var EE_Registration $primary_registrant */
1012
-        $primary_registrant = EEM_Registration::instance()->get_one(
1013
-            array(
1014
-                array(
1015
-                    'TXN_ID'    => $this->transaction_ID(),
1016
-                    'REG_count' => 1,
1017
-                ),
1018
-            )
1019
-        );
1020
-        return $primary_registrant;
1021
-    }
1022
-
1023
-
1024
-    /**
1025
-     *        get  Attendee Number
1026
-     *
1027
-     * @access        public
1028
-     */
1029
-    public function count()
1030
-    {
1031
-        return $this->get('REG_count');
1032
-    }
1033
-
1034
-
1035
-    /**
1036
-     *        get Group Size
1037
-     */
1038
-    public function group_size()
1039
-    {
1040
-        return $this->get('REG_group_size');
1041
-    }
1042
-
1043
-
1044
-    /**
1045
-     *        get Registration Date
1046
-     */
1047
-    public function date()
1048
-    {
1049
-        return $this->get('REG_date');
1050
-    }
1051
-
1052
-
1053
-    /**
1054
-     * gets a pretty date
1055
-     *
1056
-     * @param string $date_format
1057
-     * @param string $time_format
1058
-     * @return string
1059
-     * @throws EE_Error
1060
-     */
1061
-    public function pretty_date($date_format = null, $time_format = null)
1062
-    {
1063
-        return $this->get_datetime('REG_date', $date_format, $time_format);
1064
-    }
1065
-
1066
-
1067
-    /**
1068
-     * final_price
1069
-     * the registration's share of the transaction total, so that the
1070
-     * sum of all the transaction's REG_final_prices equal the transaction's total
1071
-     *
1072
-     * @return float
1073
-     * @throws EE_Error
1074
-     */
1075
-    public function final_price()
1076
-    {
1077
-        return $this->get('REG_final_price');
1078
-    }
1079
-
1080
-
1081
-    /**
1082
-     * pretty_final_price
1083
-     *  final price as formatted string, with correct decimal places and currency symbol
1084
-     *
1085
-     * @return string
1086
-     * @throws EE_Error
1087
-     */
1088
-    public function pretty_final_price()
1089
-    {
1090
-        return $this->get_pretty('REG_final_price');
1091
-    }
1092
-
1093
-
1094
-    /**
1095
-     * get paid (yeah)
1096
-     *
1097
-     * @return float
1098
-     * @throws EE_Error
1099
-     */
1100
-    public function paid()
1101
-    {
1102
-        return $this->get('REG_paid');
1103
-    }
1104
-
1105
-
1106
-    /**
1107
-     * pretty_paid
1108
-     *
1109
-     * @return float
1110
-     * @throws EE_Error
1111
-     */
1112
-    public function pretty_paid()
1113
-    {
1114
-        return $this->get_pretty('REG_paid');
1115
-    }
1116
-
1117
-
1118
-    /**
1119
-     * owes_monies_and_can_pay
1120
-     * whether or not this registration has monies owing and it's' status allows payment
1121
-     *
1122
-     * @param array $requires_payment
1123
-     * @return bool
1124
-     * @throws EE_Error
1125
-     */
1126
-    public function owes_monies_and_can_pay($requires_payment = array())
1127
-    {
1128
-        // these reg statuses require payment (if event is not free)
1129
-        $requires_payment = ! empty($requires_payment)
1130
-            ? $requires_payment
1131
-            : EEM_Registration::reg_statuses_that_allow_payment();
1132
-        if (in_array($this->status_ID(), $requires_payment) &&
1133
-            $this->final_price() != 0 &&
1134
-            $this->final_price() != $this->paid()
1135
-        ) {
1136
-            return true;
1137
-        } else {
1138
-            return false;
1139
-        }
1140
-    }
1141
-
1142
-
1143
-    /**
1144
-     * Prints out the return value of $this->pretty_status()
1145
-     *
1146
-     * @param bool $show_icons
1147
-     * @return void
1148
-     * @throws EE_Error
1149
-     */
1150
-    public function e_pretty_status($show_icons = false)
1151
-    {
1152
-        echo $this->pretty_status($show_icons);
1153
-    }
1154
-
1155
-
1156
-    /**
1157
-     * Returns a nice version of the status for displaying to customers
1158
-     *
1159
-     * @param bool $show_icons
1160
-     * @return string
1161
-     * @throws EE_Error
1162
-     */
1163
-    public function pretty_status($show_icons = false)
1164
-    {
1165
-        $status = EEM_Status::instance()->localized_status(
1166
-            array($this->status_ID() => esc_html__('unknown', 'event_espresso')),
1167
-            false,
1168
-            'sentence'
1169
-        );
1170
-        $icon = '';
1171
-        switch ($this->status_ID()) {
1172
-            case EEM_Registration::status_id_approved:
1173
-                $icon = $show_icons
1174
-                    ? '<span class="dashicons dashicons-star-filled ee-icon-size-16 green-text"></span>'
1175
-                    : '';
1176
-                break;
1177
-            case EEM_Registration::status_id_pending_payment:
1178
-                $icon = $show_icons
1179
-                    ? '<span class="dashicons dashicons-star-half ee-icon-size-16 orange-text"></span>'
1180
-                    : '';
1181
-                break;
1182
-            case EEM_Registration::status_id_not_approved:
1183
-                $icon = $show_icons
1184
-                    ? '<span class="dashicons dashicons-marker ee-icon-size-16 orange-text"></span>'
1185
-                    : '';
1186
-                break;
1187
-            case EEM_Registration::status_id_cancelled:
1188
-                $icon = $show_icons
1189
-                    ? '<span class="dashicons dashicons-no ee-icon-size-16 lt-grey-text"></span>'
1190
-                    : '';
1191
-                break;
1192
-            case EEM_Registration::status_id_incomplete:
1193
-                $icon = $show_icons
1194
-                    ? '<span class="dashicons dashicons-no ee-icon-size-16 lt-orange-text"></span>'
1195
-                    : '';
1196
-                break;
1197
-            case EEM_Registration::status_id_declined:
1198
-                $icon = $show_icons
1199
-                    ? '<span class="dashicons dashicons-no ee-icon-size-16 red-text"></span>'
1200
-                    : '';
1201
-                break;
1202
-            case EEM_Registration::status_id_wait_list:
1203
-                $icon = $show_icons
1204
-                    ? '<span class="dashicons dashicons-clipboard ee-icon-size-16 purple-text"></span>'
1205
-                    : '';
1206
-                break;
1207
-        }
1208
-        return $icon . $status[ $this->status_ID() ];
1209
-    }
1210
-
1211
-
1212
-    /**
1213
-     *        get Attendee Is Going
1214
-     */
1215
-    public function att_is_going()
1216
-    {
1217
-        return $this->get('REG_att_is_going');
1218
-    }
1219
-
1220
-
1221
-    /**
1222
-     * Gets related answers
1223
-     *
1224
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1225
-     * @return EE_Answer[]
1226
-     * @throws EE_Error
1227
-     */
1228
-    public function answers($query_params = null)
1229
-    {
1230
-        return $this->get_many_related('Answer', $query_params);
1231
-    }
1232
-
1233
-
1234
-    /**
1235
-     * Gets the registration's answer value to the specified question
1236
-     * (either the question's ID or a question object)
1237
-     *
1238
-     * @param EE_Question|int $question
1239
-     * @param bool            $pretty_value
1240
-     * @return array|string if pretty_value= true, the result will always be a string
1241
-     * (because the answer might be an array of answer values, so passing pretty_value=true
1242
-     * will convert it into some kind of string)
1243
-     * @throws EE_Error
1244
-     */
1245
-    public function answer_value_to_question($question, $pretty_value = true)
1246
-    {
1247
-        $question_id = EEM_Question::instance()->ensure_is_ID($question);
1248
-        return EEM_Answer::instance()->get_answer_value_to_question($this, $question_id, $pretty_value);
1249
-    }
1250
-
1251
-
1252
-    /**
1253
-     * question_groups
1254
-     * returns an array of EE_Question_Group objects for this registration
1255
-     *
1256
-     * @return EE_Question_Group[]
1257
-     * @throws EE_Error
1258
-     * @throws EntityNotFoundException
1259
-     */
1260
-    public function question_groups()
1261
-    {
1262
-        $question_groups = array();
1263
-        if ($this->event() instanceof EE_Event) {
1264
-            $question_groups = $this->event()->question_groups(
1265
-                array(
1266
-                    array(
1267
-                        'Event_Question_Group.EQG_primary' => $this->count() == 1 ? true : false,
1268
-                    ),
1269
-                    'order_by' => array('QSG_order' => 'ASC'),
1270
-                )
1271
-            );
1272
-        }
1273
-        return $question_groups;
1274
-    }
1275
-
1276
-
1277
-    /**
1278
-     * count_question_groups
1279
-     * returns a count of the number of EE_Question_Group objects for this registration
1280
-     *
1281
-     * @return int
1282
-     * @throws EE_Error
1283
-     * @throws EntityNotFoundException
1284
-     */
1285
-    public function count_question_groups()
1286
-    {
1287
-        $qg_count = 0;
1288
-        if ($this->event() instanceof EE_Event) {
1289
-            $qg_count = $this->event()->count_related(
1290
-                'Question_Group',
1291
-                array(
1292
-                    array(
1293
-                        'Event_Question_Group.EQG_primary' => $this->count() == 1 ? true : false,
1294
-                    ),
1295
-                )
1296
-            );
1297
-        }
1298
-        return $qg_count;
1299
-    }
1300
-
1301
-
1302
-    /**
1303
-     * Returns the registration date in the 'standard' string format
1304
-     * (function may be improved in the future to allow for different formats and timezones)
1305
-     *
1306
-     * @return string
1307
-     * @throws EE_Error
1308
-     */
1309
-    public function reg_date()
1310
-    {
1311
-        return $this->get_datetime('REG_date');
1312
-    }
1313
-
1314
-
1315
-    /**
1316
-     * Gets the datetime-ticket for this registration (ie, it can be used to isolate
1317
-     * the ticket this registration purchased, or the datetime they have registered
1318
-     * to attend)
1319
-     *
1320
-     * @return EE_Datetime_Ticket
1321
-     * @throws EE_Error
1322
-     */
1323
-    public function datetime_ticket()
1324
-    {
1325
-        return $this->get_first_related('Datetime_Ticket');
1326
-    }
1327
-
1328
-
1329
-    /**
1330
-     * Sets the registration's datetime_ticket.
1331
-     *
1332
-     * @param EE_Datetime_Ticket $datetime_ticket
1333
-     * @return EE_Datetime_Ticket
1334
-     * @throws EE_Error
1335
-     */
1336
-    public function set_datetime_ticket($datetime_ticket)
1337
-    {
1338
-        return $this->_add_relation_to($datetime_ticket, 'Datetime_Ticket');
1339
-    }
1340
-
1341
-    /**
1342
-     * Gets deleted
1343
-     *
1344
-     * @return bool
1345
-     * @throws EE_Error
1346
-     */
1347
-    public function deleted()
1348
-    {
1349
-        return $this->get('REG_deleted');
1350
-    }
1351
-
1352
-    /**
1353
-     * Sets deleted
1354
-     *
1355
-     * @param boolean $deleted
1356
-     * @return bool
1357
-     * @throws EE_Error
1358
-     * @throws RuntimeException
1359
-     */
1360
-    public function set_deleted($deleted)
1361
-    {
1362
-        if ($deleted) {
1363
-            $this->delete();
1364
-        } else {
1365
-            $this->restore();
1366
-        }
1367
-    }
1368
-
1369
-
1370
-    /**
1371
-     * Get the status object of this object
1372
-     *
1373
-     * @return EE_Status
1374
-     * @throws EE_Error
1375
-     */
1376
-    public function status_obj()
1377
-    {
1378
-        return $this->get_first_related('Status');
1379
-    }
1380
-
1381
-
1382
-    /**
1383
-     * Returns the number of times this registration has checked into any of the datetimes
1384
-     * its available for
1385
-     *
1386
-     * @return int
1387
-     * @throws EE_Error
1388
-     */
1389
-    public function count_checkins()
1390
-    {
1391
-        return $this->get_model()->count_related($this, 'Checkin');
1392
-    }
1393
-
1394
-
1395
-    /**
1396
-     * Returns the number of current Check-ins this registration is checked into for any of the datetimes the
1397
-     * registration is for.  Note, this is ONLY checked in (does not include checkedout)
1398
-     *
1399
-     * @return int
1400
-     * @throws EE_Error
1401
-     */
1402
-    public function count_checkins_not_checkedout()
1403
-    {
1404
-        return $this->get_model()->count_related($this, 'Checkin', array(array('CHK_in' => 1)));
1405
-    }
1406
-
1407
-
1408
-    /**
1409
-     * The purpose of this method is simply to check whether this registration can checkin to the given datetime.
1410
-     *
1411
-     * @param int | EE_Datetime $DTT_OR_ID      The datetime the registration is being checked against
1412
-     * @param bool              $check_approved This is used to indicate whether the caller wants can_checkin to also
1413
-     *                                          consider registration status as well as datetime access.
1414
-     * @return bool
1415
-     * @throws EE_Error
1416
-     */
1417
-    public function can_checkin($DTT_OR_ID, $check_approved = true)
1418
-    {
1419
-        $DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1420
-
1421
-        // first check registration status
1422
-        if (($check_approved && ! $this->is_approved()) || ! $DTT_ID) {
1423
-            return false;
1424
-        }
1425
-        // is there a datetime ticket that matches this dtt_ID?
1426
-        if (! (EEM_Datetime_Ticket::instance()->exists(
1427
-            array(
1428
-                array(
1429
-                    'TKT_ID' => $this->get('TKT_ID'),
1430
-                    'DTT_ID' => $DTT_ID,
1431
-                ),
1432
-            )
1433
-        ))
1434
-        ) {
1435
-            return false;
1436
-        }
1437
-
1438
-        // final check is against TKT_uses
1439
-        return $this->verify_can_checkin_against_TKT_uses($DTT_ID);
1440
-    }
1441
-
1442
-
1443
-    /**
1444
-     * This method verifies whether the user can checkin for the given datetime considering the max uses value set on
1445
-     * the ticket. To do this,  a query is done to get the count of the datetime records already checked into.  If the
1446
-     * datetime given does not have a check-in record and checking in for that datetime will exceed the allowed uses,
1447
-     * then return false.  Otherwise return true.
1448
-     *
1449
-     * @param int | EE_Datetime $DTT_OR_ID The datetime the registration is being checked against
1450
-     * @return bool true means can checkin.  false means cannot checkin.
1451
-     * @throws EE_Error
1452
-     */
1453
-    public function verify_can_checkin_against_TKT_uses($DTT_OR_ID)
1454
-    {
1455
-        $DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1456
-
1457
-        if (! $DTT_ID) {
1458
-            return false;
1459
-        }
1460
-
1461
-        $max_uses = $this->ticket() instanceof EE_Ticket ? $this->ticket()->uses() : EE_INF;
1462
-
1463
-        // if max uses is not set or equals infinity then return true cause its not a factor for whether user can
1464
-        // check-in or not.
1465
-        if (! $max_uses || $max_uses === EE_INF) {
1466
-            return true;
1467
-        }
1468
-
1469
-        // does this datetime have a checkin record?  If so, then the dtt count has already been verified so we can just
1470
-        // go ahead and toggle.
1471
-        if (EEM_Checkin::instance()->exists(array(array('REG_ID' => $this->ID(), 'DTT_ID' => $DTT_ID)))) {
1472
-            return true;
1473
-        }
1474
-
1475
-        // made it here so the last check is whether the number of checkins per unique datetime on this registration
1476
-        // disallows further check-ins.
1477
-        $count_unique_dtt_checkins = EEM_Checkin::instance()->count(
1478
-            array(
1479
-                array(
1480
-                    'REG_ID' => $this->ID(),
1481
-                    'CHK_in' => true,
1482
-                ),
1483
-            ),
1484
-            'DTT_ID',
1485
-            true
1486
-        );
1487
-        // checkins have already reached their max number of uses
1488
-        // so registrant can NOT checkin
1489
-        if ($count_unique_dtt_checkins >= $max_uses) {
1490
-            EE_Error::add_error(
1491
-                esc_html__(
1492
-                    'Check-in denied because number of datetime uses for the ticket has been reached or exceeded.',
1493
-                    'event_espresso'
1494
-                ),
1495
-                __FILE__,
1496
-                __FUNCTION__,
1497
-                __LINE__
1498
-            );
1499
-            return false;
1500
-        }
1501
-        return true;
1502
-    }
1503
-
1504
-
1505
-    /**
1506
-     * toggle Check-in status for this registration
1507
-     * Check-ins are toggled in the following order:
1508
-     * never checked in -> checked in
1509
-     * checked in -> checked out
1510
-     * checked out -> checked in
1511
-     *
1512
-     * @param  int $DTT_ID  include specific datetime to toggle Check-in for.
1513
-     *                      If not included or null, then it is assumed latest datetime is being toggled.
1514
-     * @param bool $verify  If true then can_checkin() is used to verify whether the person
1515
-     *                      can be checked in or not.  Otherwise this forces change in checkin status.
1516
-     * @return bool|int     the chk_in status toggled to OR false if nothing got changed.
1517
-     * @throws EE_Error
1518
-     */
1519
-    public function toggle_checkin_status($DTT_ID = null, $verify = false)
1520
-    {
1521
-        if (empty($DTT_ID)) {
1522
-            $datetime = $this->get_latest_related_datetime();
1523
-            $DTT_ID = $datetime instanceof EE_Datetime ? $datetime->ID() : 0;
1524
-            // verify the registration can checkin for the given DTT_ID
1525
-        } elseif (! $this->can_checkin($DTT_ID, $verify)) {
1526
-            EE_Error::add_error(
1527
-                sprintf(
1528
-                    esc_html__(
1529
-                        'The given registration (ID:%1$d) can not be checked in to the given DTT_ID (%2$d), because the registration does not have access',
1530
-                        'event_espresso'
1531
-                    ),
1532
-                    $this->ID(),
1533
-                    $DTT_ID
1534
-                ),
1535
-                __FILE__,
1536
-                __FUNCTION__,
1537
-                __LINE__
1538
-            );
1539
-            return false;
1540
-        }
1541
-        $status_paths = array(
1542
-            EE_Checkin::status_checked_never => EE_Checkin::status_checked_in,
1543
-            EE_Checkin::status_checked_in    => EE_Checkin::status_checked_out,
1544
-            EE_Checkin::status_checked_out   => EE_Checkin::status_checked_in,
1545
-        );
1546
-        // start by getting the current status so we know what status we'll be changing to.
1547
-        $cur_status = $this->check_in_status_for_datetime($DTT_ID, null);
1548
-        $status_to = $status_paths[ $cur_status ];
1549
-        // database only records true for checked IN or false for checked OUT
1550
-        // no record ( null ) means checked in NEVER, but we obviously don't save that
1551
-        $new_status = $status_to === EE_Checkin::status_checked_in ? true : false;
1552
-        // add relation - note Check-ins are always creating new rows
1553
-        // because we are keeping track of Check-ins over time.
1554
-        // Eventually we'll probably want to show a list table
1555
-        // for the individual Check-ins so that they can be managed.
1556
-        $checkin = EE_Checkin::new_instance(
1557
-            array(
1558
-                'REG_ID' => $this->ID(),
1559
-                'DTT_ID' => $DTT_ID,
1560
-                'CHK_in' => $new_status,
1561
-            )
1562
-        );
1563
-        // if the record could not be saved then return false
1564
-        if ($checkin->save() === 0) {
1565
-            if (WP_DEBUG) {
1566
-                global $wpdb;
1567
-                $error = sprintf(
1568
-                    esc_html__(
1569
-                        'Registration check in update failed because of the following database error: %1$s%2$s',
1570
-                        'event_espresso'
1571
-                    ),
1572
-                    '<br />',
1573
-                    $wpdb->last_error
1574
-                );
1575
-            } else {
1576
-                $error = esc_html__(
1577
-                    'Registration check in update failed because of an unknown database error',
1578
-                    'event_espresso'
1579
-                );
1580
-            }
1581
-            EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__);
1582
-            return false;
1583
-        }
1584
-        return $status_to;
1585
-    }
1586
-
1587
-
1588
-    /**
1589
-     * Returns the latest datetime related to this registration (via the ticket attached to the registration).
1590
-     * "Latest" is defined by the `DTT_EVT_start` column.
1591
-     *
1592
-     * @return EE_Datetime|null
1593
-     * @throws EE_Error
1594
-     */
1595
-    public function get_latest_related_datetime()
1596
-    {
1597
-        return EEM_Datetime::instance()->get_one(
1598
-            array(
1599
-                array(
1600
-                    'Ticket.Registration.REG_ID' => $this->ID(),
1601
-                ),
1602
-                'order_by' => array('DTT_EVT_start' => 'DESC'),
1603
-            )
1604
-        );
1605
-    }
1606
-
1607
-
1608
-    /**
1609
-     * Returns the earliest datetime related to this registration (via the ticket attached to the registration).
1610
-     * "Earliest" is defined by the `DTT_EVT_start` column.
1611
-     *
1612
-     * @throws EE_Error
1613
-     */
1614
-    public function get_earliest_related_datetime()
1615
-    {
1616
-        return EEM_Datetime::instance()->get_one(
1617
-            array(
1618
-                array(
1619
-                    'Ticket.Registration.REG_ID' => $this->ID(),
1620
-                ),
1621
-                'order_by' => array('DTT_EVT_start' => 'ASC'),
1622
-            )
1623
-        );
1624
-    }
1625
-
1626
-
1627
-    /**
1628
-     * This method simply returns the check-in status for this registration and the given datetime.
1629
-     * If neither the datetime nor the checkin values are provided as arguments,
1630
-     * then this will return the LATEST check-in status for the registration across all datetimes it belongs to.
1631
-     *
1632
-     * @param  int       $DTT_ID  The ID of the datetime we're checking against
1633
-     *                            (if empty we'll get the primary datetime for
1634
-     *                            this registration (via event) and use it's ID);
1635
-     * @param EE_Checkin $checkin If present, we use the given checkin object rather than the dtt_id.
1636
-     *
1637
-     * @return int                Integer representing Check-in status.
1638
-     * @throws EE_Error
1639
-     */
1640
-    public function check_in_status_for_datetime($DTT_ID = 0, $checkin = null)
1641
-    {
1642
-        $checkin_query_params = array(
1643
-            'order_by' => array('CHK_timestamp' => 'DESC'),
1644
-        );
1645
-
1646
-        if ($DTT_ID > 0) {
1647
-            $checkin_query_params[0] = array('DTT_ID' => $DTT_ID);
1648
-        }
1649
-
1650
-        // get checkin object (if exists)
1651
-        $checkin = $checkin instanceof EE_Checkin
1652
-            ? $checkin
1653
-            : $this->get_first_related('Checkin', $checkin_query_params);
1654
-        if ($checkin instanceof EE_Checkin) {
1655
-            if ($checkin->get('CHK_in')) {
1656
-                return EE_Checkin::status_checked_in; // checked in
1657
-            }
1658
-            return EE_Checkin::status_checked_out; // had checked in but is now checked out.
1659
-        }
1660
-        return EE_Checkin::status_checked_never; // never been checked in
1661
-    }
1662
-
1663
-
1664
-    /**
1665
-     * This method returns a localized message for the toggled Check-in message.
1666
-     *
1667
-     * @param  int $DTT_ID include specific datetime to get the correct Check-in message.  If not included or null,
1668
-     *                     then it is assumed Check-in for primary datetime was toggled.
1669
-     * @param bool $error  This just flags that you want an error message returned. This is put in so that the error
1670
-     *                     message can be customized with the attendee name.
1671
-     * @return string internationalized message
1672
-     * @throws EE_Error
1673
-     */
1674
-    public function get_checkin_msg($DTT_ID, $error = false)
1675
-    {
1676
-        // let's get the attendee first so we can include the name of the attendee
1677
-        $attendee = $this->get_first_related('Attendee');
1678
-        if ($attendee instanceof EE_Attendee) {
1679
-            if ($error) {
1680
-                return sprintf(__("%s's check-in status was not changed.", "event_espresso"), $attendee->full_name());
1681
-            }
1682
-            $cur_status = $this->check_in_status_for_datetime($DTT_ID);
1683
-            // what is the status message going to be?
1684
-            switch ($cur_status) {
1685
-                case EE_Checkin::status_checked_never:
1686
-                    return sprintf(
1687
-                        __("%s has been removed from Check-in records", "event_espresso"),
1688
-                        $attendee->full_name()
1689
-                    );
1690
-                    break;
1691
-                case EE_Checkin::status_checked_in:
1692
-                    return sprintf(__('%s has been checked in', 'event_espresso'), $attendee->full_name());
1693
-                    break;
1694
-                case EE_Checkin::status_checked_out:
1695
-                    return sprintf(__('%s has been checked out', 'event_espresso'), $attendee->full_name());
1696
-                    break;
1697
-            }
1698
-        }
1699
-        return esc_html__("The check-in status could not be determined.", "event_espresso");
1700
-    }
1701
-
1702
-
1703
-    /**
1704
-     * Returns the related EE_Transaction to this registration
1705
-     *
1706
-     * @return EE_Transaction
1707
-     * @throws EE_Error
1708
-     * @throws EntityNotFoundException
1709
-     */
1710
-    public function transaction()
1711
-    {
1712
-        $transaction = $this->get_first_related('Transaction');
1713
-        if (! $transaction instanceof \EE_Transaction) {
1714
-            throw new EntityNotFoundException('Transaction ID', $this->transaction_ID());
1715
-        }
1716
-        return $transaction;
1717
-    }
1718
-
1719
-
1720
-    /**
1721
-     *        get Registration Code
1722
-     */
1723
-    public function reg_code()
1724
-    {
1725
-        return $this->get('REG_code');
1726
-    }
1727
-
1728
-
1729
-    /**
1730
-     *        get Transaction ID
1731
-     */
1732
-    public function transaction_ID()
1733
-    {
1734
-        return $this->get('TXN_ID');
1735
-    }
1736
-
1737
-
1738
-    /**
1739
-     * @return int
1740
-     * @throws EE_Error
1741
-     */
1742
-    public function ticket_ID()
1743
-    {
1744
-        return $this->get('TKT_ID');
1745
-    }
1746
-
1747
-
1748
-    /**
1749
-     *        Set Registration Code
1750
-     *
1751
-     * @access    public
1752
-     * @param    string  $REG_code Registration Code
1753
-     * @param    boolean $use_default
1754
-     * @throws EE_Error
1755
-     */
1756
-    public function set_reg_code($REG_code, $use_default = false)
1757
-    {
1758
-        if (empty($REG_code)) {
1759
-            EE_Error::add_error(
1760
-                esc_html__('REG_code can not be empty.', 'event_espresso'),
1761
-                __FILE__,
1762
-                __FUNCTION__,
1763
-                __LINE__
1764
-            );
1765
-            return;
1766
-        }
1767
-        if (! $this->reg_code()) {
1768
-            parent::set('REG_code', $REG_code, $use_default);
1769
-        } else {
1770
-            EE_Error::doing_it_wrong(
1771
-                __CLASS__ . '::' . __FUNCTION__,
1772
-                esc_html__('Can not change a registration REG_code once it has been set.', 'event_espresso'),
1773
-                '4.6.0'
1774
-            );
1775
-        }
1776
-    }
1777
-
1778
-
1779
-    /**
1780
-     * Returns all other registrations in the same group as this registrant who have the same ticket option.
1781
-     * Note, if you want to just get all registrations in the same transaction (group), use:
1782
-     *    $registration->transaction()->registrations();
1783
-     *
1784
-     * @since 4.5.0
1785
-     * @return EE_Registration[] or empty array if this isn't a group registration.
1786
-     * @throws EE_Error
1787
-     */
1788
-    public function get_all_other_registrations_in_group()
1789
-    {
1790
-        if ($this->group_size() < 2) {
1791
-            return array();
1792
-        }
1793
-
1794
-        $query[0] = array(
1795
-            'TXN_ID' => $this->transaction_ID(),
1796
-            'REG_ID' => array('!=', $this->ID()),
1797
-            'TKT_ID' => $this->ticket_ID(),
1798
-        );
1799
-        /** @var EE_Registration[] $registrations */
1800
-        $registrations = $this->get_model()->get_all($query);
1801
-        return $registrations;
1802
-    }
1803
-
1804
-    /**
1805
-     * Return the link to the admin details for the object.
1806
-     *
1807
-     * @return string
1808
-     * @throws EE_Error
1809
-     */
1810
-    public function get_admin_details_link()
1811
-    {
1812
-        EE_Registry::instance()->load_helper('URL');
1813
-        return EEH_URL::add_query_args_and_nonce(
1814
-            array(
1815
-                'page'    => 'espresso_registrations',
1816
-                'action'  => 'view_registration',
1817
-                '_REG_ID' => $this->ID(),
1818
-            ),
1819
-            admin_url('admin.php')
1820
-        );
1821
-    }
1822
-
1823
-    /**
1824
-     * Returns the link to the editor for the object.  Sometimes this is the same as the details.
1825
-     *
1826
-     * @return string
1827
-     * @throws EE_Error
1828
-     */
1829
-    public function get_admin_edit_link()
1830
-    {
1831
-        return $this->get_admin_details_link();
1832
-    }
1833
-
1834
-    /**
1835
-     * Returns the link to a settings page for the object.
1836
-     *
1837
-     * @return string
1838
-     * @throws EE_Error
1839
-     */
1840
-    public function get_admin_settings_link()
1841
-    {
1842
-        return $this->get_admin_details_link();
1843
-    }
1844
-
1845
-    /**
1846
-     * Returns the link to the "overview" for the object (typically the "list table" view).
1847
-     *
1848
-     * @return string
1849
-     */
1850
-    public function get_admin_overview_link()
1851
-    {
1852
-        EE_Registry::instance()->load_helper('URL');
1853
-        return EEH_URL::add_query_args_and_nonce(
1854
-            array(
1855
-                'page' => 'espresso_registrations',
1856
-            ),
1857
-            admin_url('admin.php')
1858
-        );
1859
-    }
1860
-
1861
-
1862
-    /**
1863
-     * @param array $query_params
1864
-     *
1865
-     * @return \EE_Registration[]
1866
-     * @throws EE_Error
1867
-     */
1868
-    public function payments($query_params = array())
1869
-    {
1870
-        return $this->get_many_related('Payment', $query_params);
1871
-    }
1872
-
1873
-
1874
-    /**
1875
-     * @param array $query_params
1876
-     *
1877
-     * @return \EE_Registration_Payment[]
1878
-     * @throws EE_Error
1879
-     */
1880
-    public function registration_payments($query_params = array())
1881
-    {
1882
-        return $this->get_many_related('Registration_Payment', $query_params);
1883
-    }
1884
-
1885
-
1886
-    /**
1887
-     * This grabs the payment method corresponding to the last payment made for the amount owing on the registration.
1888
-     * Note: if there are no payments on the registration there will be no payment method returned.
1889
-     *
1890
-     * @return EE_Payment_Method|null
1891
-     */
1892
-    public function payment_method()
1893
-    {
1894
-        return EEM_Payment_Method::instance()->get_last_used_for_registration($this);
1895
-    }
1896
-
1897
-
1898
-    /**
1899
-     * @return \EE_Line_Item
1900
-     * @throws EntityNotFoundException
1901
-     * @throws EE_Error
1902
-     */
1903
-    public function ticket_line_item()
1904
-    {
1905
-        $ticket = $this->ticket();
1906
-        $transaction = $this->transaction();
1907
-        $line_item = null;
1908
-        $ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs(
1909
-            $transaction->total_line_item(),
1910
-            'Ticket',
1911
-            array($ticket->ID())
1912
-        );
1913
-        foreach ($ticket_line_items as $ticket_line_item) {
1914
-            if ($ticket_line_item instanceof \EE_Line_Item
1915
-                && $ticket_line_item->OBJ_type() === 'Ticket'
1916
-                && $ticket_line_item->OBJ_ID() === $ticket->ID()
1917
-            ) {
1918
-                $line_item = $ticket_line_item;
1919
-                break;
1920
-            }
1921
-        }
1922
-        if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) {
1923
-            throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID());
1924
-        }
1925
-        return $line_item;
1926
-    }
1927
-
1928
-
1929
-    /**
1930
-     * Soft Deletes this model object.
1931
-     *
1932
-     * @return boolean | int
1933
-     * @throws RuntimeException
1934
-     * @throws EE_Error
1935
-     */
1936
-    public function delete()
1937
-    {
1938
-        if ($this->update_extra_meta(EE_Registration::PRE_TRASH_REG_STATUS_KEY, $this->status_ID()) === true) {
1939
-            $this->set_status(EEM_Registration::status_id_cancelled);
1940
-        }
1941
-        return parent::delete();
1942
-    }
1943
-
1944
-
1945
-    /**
1946
-     * Restores whatever the previous status was on a registration before it was trashed (if possible)
1947
-     *
1948
-     * @throws EE_Error
1949
-     * @throws RuntimeException
1950
-     */
1951
-    public function restore()
1952
-    {
1953
-        $previous_status = $this->get_extra_meta(
1954
-            EE_Registration::PRE_TRASH_REG_STATUS_KEY,
1955
-            true,
1956
-            EEM_Registration::status_id_cancelled
1957
-        );
1958
-        if ($previous_status) {
1959
-            $this->delete_extra_meta(EE_Registration::PRE_TRASH_REG_STATUS_KEY);
1960
-            $this->set_status($previous_status);
1961
-        }
1962
-        return parent::restore();
1963
-    }
1964
-
1965
-
1966
-    /**
1967
-     * possibly toggle Registration status based on comparison of REG_paid vs REG_final_price
1968
-     *
1969
-     * @param  boolean $trigger_set_status_logic EE_Registration::set_status() can trigger additional logic
1970
-     *                                           depending on whether the reg status changes to or from "Approved"
1971
-     * @return boolean whether the Registration status was updated
1972
-     * @throws EE_Error
1973
-     * @throws RuntimeException
1974
-     */
1975
-    public function updateStatusBasedOnTotalPaid($trigger_set_status_logic = true)
1976
-    {
1977
-        $paid = $this->paid();
1978
-        $price = $this->final_price();
1979
-        switch (true) {
1980
-            // overpaid or paid
1981
-            case EEH_Money::compare_floats($paid, $price, '>'):
1982
-            case EEH_Money::compare_floats($paid, $price):
1983
-                $new_status = EEM_Registration::status_id_approved;
1984
-                break;
1985
-            //  underpaid
1986
-            case EEH_Money::compare_floats($paid, $price, '<'):
1987
-                $new_status = EEM_Registration::status_id_pending_payment;
1988
-                break;
1989
-            // uhhh Houston...
1990
-            default:
1991
-                throw new RuntimeException(
1992
-                    esc_html__('The total paid calculation for this registration is inaccurate.', 'event_espresso')
1993
-                );
1994
-        }
1995
-        if ($new_status !== $this->status_ID()) {
1996
-            if ($trigger_set_status_logic) {
1997
-                return $this->set_status($new_status);
1998
-            }
1999
-            parent::set('STS_ID', $new_status);
2000
-            return true;
2001
-        }
2002
-        return false;
2003
-    }
2004
-
2005
-
2006
-    /*************************** DEPRECATED ***************************/
2007
-
2008
-
2009
-    /**
2010
-     * @deprecated
2011
-     * @since     4.7.0
2012
-     * @access    public
2013
-     */
2014
-    public function price_paid()
2015
-    {
2016
-        EE_Error::doing_it_wrong(
2017
-            'EE_Registration::price_paid()',
2018
-            esc_html__(
2019
-                'This method is deprecated, please use EE_Registration::final_price() instead.',
2020
-                'event_espresso'
2021
-            ),
2022
-            '4.7.0'
2023
-        );
2024
-        return $this->final_price();
2025
-    }
2026
-
2027
-
2028
-    /**
2029
-     * @deprecated
2030
-     * @since     4.7.0
2031
-     * @access    public
2032
-     * @param    float $REG_final_price
2033
-     * @throws EE_Error
2034
-     * @throws RuntimeException
2035
-     */
2036
-    public function set_price_paid($REG_final_price = 0.00)
2037
-    {
2038
-        EE_Error::doing_it_wrong(
2039
-            'EE_Registration::set_price_paid()',
2040
-            esc_html__(
2041
-                'This method is deprecated, please use EE_Registration::set_final_price() instead.',
2042
-                'event_espresso'
2043
-            ),
2044
-            '4.7.0'
2045
-        );
2046
-        $this->set_final_price($REG_final_price);
2047
-    }
2048
-
2049
-
2050
-    /**
2051
-     * @deprecated
2052
-     * @since 4.7.0
2053
-     * @return string
2054
-     * @throws EE_Error
2055
-     */
2056
-    public function pretty_price_paid()
2057
-    {
2058
-        EE_Error::doing_it_wrong(
2059
-            'EE_Registration::pretty_price_paid()',
2060
-            esc_html__(
2061
-                'This method is deprecated, please use EE_Registration::pretty_final_price() instead.',
2062
-                'event_espresso'
2063
-            ),
2064
-            '4.7.0'
2065
-        );
2066
-        return $this->pretty_final_price();
2067
-    }
2068
-
2069
-
2070
-    /**
2071
-     * Gets the primary datetime related to this registration via the related Event to this registration
2072
-     *
2073
-     * @deprecated 4.9.17
2074
-     * @return EE_Datetime
2075
-     * @throws EE_Error
2076
-     * @throws EntityNotFoundException
2077
-     */
2078
-    public function get_related_primary_datetime()
2079
-    {
2080
-        EE_Error::doing_it_wrong(
2081
-            __METHOD__,
2082
-            esc_html__(
2083
-                'Use EE_Registration::get_latest_related_datetime() or EE_Registration::get_earliest_related_datetime()',
2084
-                'event_espresso'
2085
-            ),
2086
-            '4.9.17',
2087
-            '5.0.0'
2088
-        );
2089
-        return $this->event()->primary_datetime();
2090
-    }
20
+	/**
21
+	 * Used to reference when a registration has never been checked in.
22
+	 *
23
+	 * @deprecated use \EE_Checkin::status_checked_never instead
24
+	 * @type int
25
+	 */
26
+	const checkin_status_never = 2;
27
+
28
+	/**
29
+	 * Used to reference when a registration has been checked in.
30
+	 *
31
+	 * @deprecated use \EE_Checkin::status_checked_in instead
32
+	 * @type int
33
+	 */
34
+	const checkin_status_in = 1;
35
+
36
+
37
+	/**
38
+	 * Used to reference when a registration has been checked out.
39
+	 *
40
+	 * @deprecated use \EE_Checkin::status_checked_out instead
41
+	 * @type int
42
+	 */
43
+	const checkin_status_out = 0;
44
+
45
+
46
+	/**
47
+	 * extra meta key for tracking reg status os trashed registrations
48
+	 *
49
+	 * @type string
50
+	 */
51
+	const PRE_TRASH_REG_STATUS_KEY = 'pre_trash_registration_status';
52
+
53
+
54
+	/**
55
+	 * extra meta key for tracking if registration has reserved ticket
56
+	 *
57
+	 * @type string
58
+	 */
59
+	const HAS_RESERVED_TICKET_KEY = 'has_reserved_ticket';
60
+
61
+
62
+	/**
63
+	 * @param array  $props_n_values          incoming values
64
+	 * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
65
+	 *                                        used.)
66
+	 * @param array  $date_formats            incoming date_formats in an array where the first value is the
67
+	 *                                        date_format and the second value is the time format
68
+	 * @return EE_Registration
69
+	 * @throws EE_Error
70
+	 */
71
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
72
+	{
73
+		$has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
74
+		return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
75
+	}
76
+
77
+
78
+	/**
79
+	 * @param array  $props_n_values  incoming values from the database
80
+	 * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
81
+	 *                                the website will be used.
82
+	 * @return EE_Registration
83
+	 */
84
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
85
+	{
86
+		return new self($props_n_values, true, $timezone);
87
+	}
88
+
89
+
90
+	/**
91
+	 *        Set Event ID
92
+	 *
93
+	 * @param        int $EVT_ID Event ID
94
+	 * @throws EE_Error
95
+	 * @throws RuntimeException
96
+	 */
97
+	public function set_event($EVT_ID = 0)
98
+	{
99
+		$this->set('EVT_ID', $EVT_ID);
100
+	}
101
+
102
+
103
+	/**
104
+	 * Overrides parent set() method so that all calls to set( 'REG_code', $REG_code ) OR set( 'STS_ID', $STS_ID ) can
105
+	 * be routed to internal methods
106
+	 *
107
+	 * @param string $field_name
108
+	 * @param mixed  $field_value
109
+	 * @param bool   $use_default
110
+	 * @throws EE_Error
111
+	 * @throws EntityNotFoundException
112
+	 * @throws InvalidArgumentException
113
+	 * @throws InvalidDataTypeException
114
+	 * @throws InvalidInterfaceException
115
+	 * @throws ReflectionException
116
+	 * @throws RuntimeException
117
+	 */
118
+	public function set($field_name, $field_value, $use_default = false)
119
+	{
120
+		switch ($field_name) {
121
+			case 'REG_code':
122
+				if (! empty($field_value) && $this->reg_code() === null) {
123
+					$this->set_reg_code($field_value, $use_default);
124
+				}
125
+				break;
126
+			case 'STS_ID':
127
+				$this->set_status($field_value, $use_default);
128
+				break;
129
+			default:
130
+				parent::set($field_name, $field_value, $use_default);
131
+		}
132
+	}
133
+
134
+
135
+	/**
136
+	 * Set Status ID
137
+	 * updates the registration status and ALSO...
138
+	 * calls reserve_registration_space() if the reg status changes TO approved from any other reg status
139
+	 * calls release_registration_space() if the reg status changes FROM approved to any other reg status
140
+	 *
141
+	 * @param string                $new_STS_ID
142
+	 * @param boolean               $use_default
143
+	 * @param ContextInterface|null $context
144
+	 * @return bool
145
+	 * @throws DomainException
146
+	 * @throws EE_Error
147
+	 * @throws EntityNotFoundException
148
+	 * @throws InvalidArgumentException
149
+	 * @throws InvalidDataTypeException
150
+	 * @throws InvalidInterfaceException
151
+	 * @throws ReflectionException
152
+	 * @throws RuntimeException
153
+	 * @throws UnexpectedEntityException
154
+	 */
155
+	public function set_status($new_STS_ID = null, $use_default = false, ContextInterface $context = null)
156
+	{
157
+		// get current REG_Status
158
+		$old_STS_ID = $this->status_ID();
159
+		// if status has changed
160
+		if ($old_STS_ID !== $new_STS_ID // and that status has actually changed
161
+			&& ! empty($old_STS_ID) // and that old status is actually set
162
+			&& ! empty($new_STS_ID) // as well as the new status
163
+			&& $this->ID() // ensure registration is in the db
164
+		) {
165
+			// update internal status first
166
+			parent::set('STS_ID', $new_STS_ID, $use_default);
167
+			// THEN handle other changes that occur when reg status changes
168
+			// TO approved
169
+			if ($new_STS_ID === EEM_Registration::status_id_approved) {
170
+				// reserve a space by incrementing ticket and datetime sold values
171
+				$this->reserveRegistrationSpace();
172
+				do_action('AHEE__EE_Registration__set_status__to_approved', $this, $old_STS_ID, $new_STS_ID, $context);
173
+				// OR FROM  approved
174
+			} elseif ($old_STS_ID === EEM_Registration::status_id_approved) {
175
+				// release a space by decrementing ticket and datetime sold values
176
+				$this->releaseRegistrationSpace();
177
+				do_action(
178
+					'AHEE__EE_Registration__set_status__from_approved',
179
+					$this,
180
+					$old_STS_ID,
181
+					$new_STS_ID,
182
+					$context
183
+				);
184
+			}
185
+			// update status
186
+			parent::set('STS_ID', $new_STS_ID, $use_default);
187
+			$this->updateIfCanceledOrReinstated($new_STS_ID, $old_STS_ID, $context);
188
+			if ($this->statusChangeUpdatesTransaction($context)) {
189
+				$this->updateTransactionAfterStatusChange();
190
+			}
191
+			do_action('AHEE__EE_Registration__set_status__after_update', $this, $old_STS_ID, $new_STS_ID, $context);
192
+			return true;
193
+		}
194
+		// even though the old value matches the new value, it's still good to
195
+		// allow the parent set method to have a say
196
+		parent::set('STS_ID', $new_STS_ID, $use_default);
197
+		return true;
198
+	}
199
+
200
+
201
+	/**
202
+	 * update REGs and TXN when cancelled or declined registrations involved
203
+	 *
204
+	 * @param string                $new_STS_ID
205
+	 * @param string                $old_STS_ID
206
+	 * @param ContextInterface|null $context
207
+	 * @throws EE_Error
208
+	 * @throws InvalidArgumentException
209
+	 * @throws InvalidDataTypeException
210
+	 * @throws InvalidInterfaceException
211
+	 * @throws ReflectionException
212
+	 * @throws RuntimeException
213
+	 */
214
+	private function updateIfCanceledOrReinstated($new_STS_ID, $old_STS_ID, ContextInterface $context = null)
215
+	{
216
+		// these reg statuses should not be considered in any calculations involving monies owing
217
+		$closed_reg_statuses = EEM_Registration::closed_reg_statuses();
218
+		// true if registration has been cancelled or declined
219
+		$this->updateIfCanceled(
220
+			$closed_reg_statuses,
221
+			$new_STS_ID,
222
+			$old_STS_ID,
223
+			$context
224
+		);
225
+		$this->updateIfReinstated(
226
+			$closed_reg_statuses,
227
+			$new_STS_ID,
228
+			$old_STS_ID,
229
+			$context
230
+		);
231
+	}
232
+
233
+
234
+	/**
235
+	 * update REGs and TXN when cancelled or declined registrations involved
236
+	 *
237
+	 * @param array                 $closed_reg_statuses
238
+	 * @param string                $new_STS_ID
239
+	 * @param string                $old_STS_ID
240
+	 * @param ContextInterface|null $context
241
+	 * @throws EE_Error
242
+	 * @throws InvalidArgumentException
243
+	 * @throws InvalidDataTypeException
244
+	 * @throws InvalidInterfaceException
245
+	 * @throws ReflectionException
246
+	 * @throws RuntimeException
247
+	 */
248
+	private function updateIfCanceled(
249
+		array $closed_reg_statuses,
250
+		$new_STS_ID,
251
+		$old_STS_ID,
252
+		ContextInterface $context = null
253
+	) {
254
+		// true if registration has been cancelled or declined
255
+		if (in_array($new_STS_ID, $closed_reg_statuses, true)
256
+			&& ! in_array($old_STS_ID, $closed_reg_statuses, true)
257
+		) {
258
+			/** @type EE_Registration_Processor $registration_processor */
259
+			$registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
260
+			/** @type EE_Transaction_Processor $transaction_processor */
261
+			$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
262
+			// cancelled or declined registration
263
+			$registration_processor->update_registration_after_being_canceled_or_declined(
264
+				$this,
265
+				$closed_reg_statuses
266
+			);
267
+			$transaction_processor->update_transaction_after_canceled_or_declined_registration(
268
+				$this,
269
+				$closed_reg_statuses,
270
+				false
271
+			);
272
+			do_action(
273
+				'AHEE__EE_Registration__set_status__canceled_or_declined',
274
+				$this,
275
+				$old_STS_ID,
276
+				$new_STS_ID,
277
+				$context
278
+			);
279
+			return;
280
+		}
281
+	}
282
+
283
+
284
+	/**
285
+	 * update REGs and TXN when cancelled or declined registrations involved
286
+	 *
287
+	 * @param array                 $closed_reg_statuses
288
+	 * @param string                $new_STS_ID
289
+	 * @param string                $old_STS_ID
290
+	 * @param ContextInterface|null $context
291
+	 * @throws EE_Error
292
+	 * @throws InvalidArgumentException
293
+	 * @throws InvalidDataTypeException
294
+	 * @throws InvalidInterfaceException
295
+	 * @throws ReflectionException
296
+	 */
297
+	private function updateIfReinstated(
298
+		array $closed_reg_statuses,
299
+		$new_STS_ID,
300
+		$old_STS_ID,
301
+		ContextInterface $context = null
302
+	) {
303
+		// true if reinstating cancelled or declined registration
304
+		if (in_array($old_STS_ID, $closed_reg_statuses, true)
305
+			&& ! in_array($new_STS_ID, $closed_reg_statuses, true)
306
+		) {
307
+			/** @type EE_Registration_Processor $registration_processor */
308
+			$registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
309
+			/** @type EE_Transaction_Processor $transaction_processor */
310
+			$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
311
+			// reinstating cancelled or declined registration
312
+			$registration_processor->update_canceled_or_declined_registration_after_being_reinstated(
313
+				$this,
314
+				$closed_reg_statuses
315
+			);
316
+			$transaction_processor->update_transaction_after_reinstating_canceled_registration(
317
+				$this,
318
+				$closed_reg_statuses,
319
+				false
320
+			);
321
+			do_action(
322
+				'AHEE__EE_Registration__set_status__after_reinstated',
323
+				$this,
324
+				$old_STS_ID,
325
+				$new_STS_ID,
326
+				$context
327
+			);
328
+		}
329
+	}
330
+
331
+
332
+	/**
333
+	 * @param ContextInterface|null $context
334
+	 * @return bool
335
+	 */
336
+	private function statusChangeUpdatesTransaction(ContextInterface $context = null)
337
+	{
338
+		$contexts_that_do_not_update_transaction = (array) apply_filters(
339
+			'AHEE__EE_Registration__statusChangeUpdatesTransaction__contexts_that_do_not_update_transaction',
340
+			array('spco_reg_step_attendee_information_process_registrations'),
341
+			$context,
342
+			$this
343
+		);
344
+		return ! (
345
+			$context instanceof ContextInterface
346
+			&& in_array($context->slug(), $contexts_that_do_not_update_transaction, true)
347
+		);
348
+	}
349
+
350
+
351
+	/**
352
+	 * @throws EE_Error
353
+	 * @throws EntityNotFoundException
354
+	 * @throws InvalidArgumentException
355
+	 * @throws InvalidDataTypeException
356
+	 * @throws InvalidInterfaceException
357
+	 * @throws ReflectionException
358
+	 * @throws RuntimeException
359
+	 */
360
+	private function updateTransactionAfterStatusChange()
361
+	{
362
+		/** @type EE_Transaction_Payments $transaction_payments */
363
+		$transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
364
+		$transaction_payments->recalculate_transaction_total($this->transaction(), false);
365
+		$this->transaction()->update_status_based_on_total_paid(true);
366
+	}
367
+
368
+
369
+	/**
370
+	 *        get Status ID
371
+	 */
372
+	public function status_ID()
373
+	{
374
+		return $this->get('STS_ID');
375
+	}
376
+
377
+
378
+	/**
379
+	 * Gets the ticket this registration is for
380
+	 *
381
+	 * @param boolean $include_archived whether to include archived tickets or not.
382
+	 *
383
+	 * @return EE_Ticket|EE_Base_Class
384
+	 * @throws EE_Error
385
+	 */
386
+	public function ticket($include_archived = true)
387
+	{
388
+		$query_params = array();
389
+		if ($include_archived) {
390
+			$query_params['default_where_conditions'] = 'none';
391
+		}
392
+		return $this->get_first_related('Ticket', $query_params);
393
+	}
394
+
395
+
396
+	/**
397
+	 * Gets the event this registration is for
398
+	 *
399
+	 * @return EE_Event
400
+	 * @throws EE_Error
401
+	 * @throws EntityNotFoundException
402
+	 */
403
+	public function event()
404
+	{
405
+		$event = $this->get_first_related('Event');
406
+		if (! $event instanceof \EE_Event) {
407
+			throw new EntityNotFoundException('Event ID', $this->event_ID());
408
+		}
409
+		return $event;
410
+	}
411
+
412
+
413
+	/**
414
+	 * Gets the "author" of the registration.  Note that for the purposes of registrations, the author will correspond
415
+	 * with the author of the event this registration is for.
416
+	 *
417
+	 * @since 4.5.0
418
+	 * @return int
419
+	 * @throws EE_Error
420
+	 * @throws EntityNotFoundException
421
+	 */
422
+	public function wp_user()
423
+	{
424
+		$event = $this->event();
425
+		if ($event instanceof EE_Event) {
426
+			return $event->wp_user();
427
+		}
428
+		return 0;
429
+	}
430
+
431
+
432
+	/**
433
+	 * increments this registration's related ticket sold and corresponding datetime sold values
434
+	 *
435
+	 * @return void
436
+	 * @throws DomainException
437
+	 * @throws EE_Error
438
+	 * @throws EntityNotFoundException
439
+	 * @throws InvalidArgumentException
440
+	 * @throws InvalidDataTypeException
441
+	 * @throws InvalidInterfaceException
442
+	 * @throws ReflectionException
443
+	 * @throws UnexpectedEntityException
444
+	 */
445
+	private function reserveRegistrationSpace()
446
+	{
447
+		// reserved ticket and datetime counts will be decremented as sold counts are incremented
448
+		// so stop tracking that this reg has a ticket reserved
449
+		$this->release_reserved_ticket(false, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
450
+		$ticket = $this->ticket();
451
+		$ticket->increaseSold();
452
+		// possibly set event status to sold out
453
+		$this->event()->perform_sold_out_status_check();
454
+	}
455
+
456
+
457
+	/**
458
+	 * decrements (subtracts) this registration's related ticket sold and corresponding datetime sold values
459
+	 *
460
+	 * @return void
461
+	 * @throws DomainException
462
+	 * @throws EE_Error
463
+	 * @throws EntityNotFoundException
464
+	 * @throws InvalidArgumentException
465
+	 * @throws InvalidDataTypeException
466
+	 * @throws InvalidInterfaceException
467
+	 * @throws ReflectionException
468
+	 * @throws UnexpectedEntityException
469
+	 */
470
+	private function releaseRegistrationSpace()
471
+	{
472
+		$ticket = $this->ticket();
473
+		$ticket->decreaseSold();
474
+		// possibly change event status from sold out back to previous status
475
+		$this->event()->perform_sold_out_status_check();
476
+	}
477
+
478
+
479
+	/**
480
+	 * tracks this registration's ticket reservation in extra meta
481
+	 * and can increment related ticket reserved and corresponding datetime reserved values
482
+	 *
483
+	 * @param bool $update_ticket if true, will increment ticket and datetime reserved count
484
+	 * @return void
485
+	 * @throws EE_Error
486
+	 * @throws InvalidArgumentException
487
+	 * @throws InvalidDataTypeException
488
+	 * @throws InvalidInterfaceException
489
+	 * @throws ReflectionException
490
+	 */
491
+	public function reserve_ticket($update_ticket = false, $source = 'unknown')
492
+	{
493
+		// only reserve ticket if space is not currently reserved
494
+		if ((bool) $this->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true) !== true) {
495
+			$this->update_extra_meta('reserve_ticket', "{$this->ticket_ID()} from {$source}");
496
+			// IMPORTANT !!!
497
+			// although checking $update_ticket first would be more efficient,
498
+			// we NEED to ALWAYS call update_extra_meta(), which is why that is done first
499
+			if ($this->update_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true)
500
+				&& $update_ticket
501
+			) {
502
+				$ticket = $this->ticket();
503
+				$ticket->increaseReserved(1, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
504
+				$ticket->save();
505
+			}
506
+		}
507
+	}
508
+
509
+
510
+	/**
511
+	 * stops tracking this registration's ticket reservation in extra meta
512
+	 * decrements (subtracts) related ticket reserved and corresponding datetime reserved values
513
+	 *
514
+	 * @param bool $update_ticket if true, will decrement ticket and datetime reserved count
515
+	 * @return void
516
+	 * @throws EE_Error
517
+	 * @throws InvalidArgumentException
518
+	 * @throws InvalidDataTypeException
519
+	 * @throws InvalidInterfaceException
520
+	 * @throws ReflectionException
521
+	 */
522
+	public function release_reserved_ticket($update_ticket = false, $source = 'unknown')
523
+	{
524
+		// only release ticket if space is currently reserved
525
+		if ((bool) $this->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true) === true) {
526
+			$this->update_extra_meta('release_reserved_ticket', "{$this->ticket_ID()} from {$source}");
527
+			// IMPORTANT !!!
528
+			// although checking $update_ticket first would be more efficient,
529
+			// we NEED to ALWAYS call update_extra_meta(), which is why that is done first
530
+			if ($this->update_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, false)
531
+				&& $update_ticket
532
+			) {
533
+				$ticket = $this->ticket();
534
+				$ticket->decreaseReserved(1, true, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
535
+			}
536
+		}
537
+	}
538
+
539
+
540
+	/**
541
+	 * Set Attendee ID
542
+	 *
543
+	 * @param        int $ATT_ID Attendee ID
544
+	 * @throws EE_Error
545
+	 * @throws RuntimeException
546
+	 */
547
+	public function set_attendee_id($ATT_ID = 0)
548
+	{
549
+		$this->set('ATT_ID', $ATT_ID);
550
+	}
551
+
552
+
553
+	/**
554
+	 *        Set Transaction ID
555
+	 *
556
+	 * @param        int $TXN_ID Transaction ID
557
+	 * @throws EE_Error
558
+	 * @throws RuntimeException
559
+	 */
560
+	public function set_transaction_id($TXN_ID = 0)
561
+	{
562
+		$this->set('TXN_ID', $TXN_ID);
563
+	}
564
+
565
+
566
+	/**
567
+	 *        Set Session
568
+	 *
569
+	 * @param    string $REG_session PHP Session ID
570
+	 * @throws EE_Error
571
+	 * @throws RuntimeException
572
+	 */
573
+	public function set_session($REG_session = '')
574
+	{
575
+		$this->set('REG_session', $REG_session);
576
+	}
577
+
578
+
579
+	/**
580
+	 *        Set Registration URL Link
581
+	 *
582
+	 * @param    string $REG_url_link Registration URL Link
583
+	 * @throws EE_Error
584
+	 * @throws RuntimeException
585
+	 */
586
+	public function set_reg_url_link($REG_url_link = '')
587
+	{
588
+		$this->set('REG_url_link', $REG_url_link);
589
+	}
590
+
591
+
592
+	/**
593
+	 *        Set Attendee Counter
594
+	 *
595
+	 * @param        int $REG_count Primary Attendee
596
+	 * @throws EE_Error
597
+	 * @throws RuntimeException
598
+	 */
599
+	public function set_count($REG_count = 1)
600
+	{
601
+		$this->set('REG_count', $REG_count);
602
+	}
603
+
604
+
605
+	/**
606
+	 *        Set Group Size
607
+	 *
608
+	 * @param        boolean $REG_group_size Group Registration
609
+	 * @throws EE_Error
610
+	 * @throws RuntimeException
611
+	 */
612
+	public function set_group_size($REG_group_size = false)
613
+	{
614
+		$this->set('REG_group_size', $REG_group_size);
615
+	}
616
+
617
+
618
+	/**
619
+	 *    is_not_approved -  convenience method that returns TRUE if REG status ID ==
620
+	 *    EEM_Registration::status_id_not_approved
621
+	 *
622
+	 * @return        boolean
623
+	 */
624
+	public function is_not_approved()
625
+	{
626
+		return $this->status_ID() == EEM_Registration::status_id_not_approved ? true : false;
627
+	}
628
+
629
+
630
+	/**
631
+	 *    is_pending_payment -  convenience method that returns TRUE if REG status ID ==
632
+	 *    EEM_Registration::status_id_pending_payment
633
+	 *
634
+	 * @return        boolean
635
+	 */
636
+	public function is_pending_payment()
637
+	{
638
+		return $this->status_ID() == EEM_Registration::status_id_pending_payment ? true : false;
639
+	}
640
+
641
+
642
+	/**
643
+	 *    is_approved -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_approved
644
+	 *
645
+	 * @return        boolean
646
+	 */
647
+	public function is_approved()
648
+	{
649
+		return $this->status_ID() == EEM_Registration::status_id_approved ? true : false;
650
+	}
651
+
652
+
653
+	/**
654
+	 *    is_cancelled -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_cancelled
655
+	 *
656
+	 * @return        boolean
657
+	 */
658
+	public function is_cancelled()
659
+	{
660
+		return $this->status_ID() == EEM_Registration::status_id_cancelled ? true : false;
661
+	}
662
+
663
+
664
+	/**
665
+	 *    is_declined -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_declined
666
+	 *
667
+	 * @return        boolean
668
+	 */
669
+	public function is_declined()
670
+	{
671
+		return $this->status_ID() == EEM_Registration::status_id_declined ? true : false;
672
+	}
673
+
674
+
675
+	/**
676
+	 *    is_incomplete -  convenience method that returns TRUE if REG status ID ==
677
+	 *    EEM_Registration::status_id_incomplete
678
+	 *
679
+	 * @return        boolean
680
+	 */
681
+	public function is_incomplete()
682
+	{
683
+		return $this->status_ID() == EEM_Registration::status_id_incomplete ? true : false;
684
+	}
685
+
686
+
687
+	/**
688
+	 *        Set Registration Date
689
+	 *
690
+	 * @param        mixed ( int or string ) $REG_date Registration Date - Unix timestamp or string representation of
691
+	 *                                                 Date
692
+	 * @throws EE_Error
693
+	 * @throws RuntimeException
694
+	 */
695
+	public function set_reg_date($REG_date = false)
696
+	{
697
+		$this->set('REG_date', $REG_date);
698
+	}
699
+
700
+
701
+	/**
702
+	 *    Set final price owing for this registration after all ticket/price modifications
703
+	 *
704
+	 * @access    public
705
+	 * @param    float $REG_final_price
706
+	 * @throws EE_Error
707
+	 * @throws RuntimeException
708
+	 */
709
+	public function set_final_price($REG_final_price = 0.00)
710
+	{
711
+		$this->set('REG_final_price', $REG_final_price);
712
+	}
713
+
714
+
715
+	/**
716
+	 *    Set amount paid towards this registration's final price
717
+	 *
718
+	 * @access    public
719
+	 * @param    float $REG_paid
720
+	 * @throws EE_Error
721
+	 * @throws RuntimeException
722
+	 */
723
+	public function set_paid($REG_paid = 0.00)
724
+	{
725
+		$this->set('REG_paid', $REG_paid);
726
+	}
727
+
728
+
729
+	/**
730
+	 *        Attendee Is Going
731
+	 *
732
+	 * @param        boolean $REG_att_is_going Attendee Is Going
733
+	 * @throws EE_Error
734
+	 * @throws RuntimeException
735
+	 */
736
+	public function set_att_is_going($REG_att_is_going = false)
737
+	{
738
+		$this->set('REG_att_is_going', $REG_att_is_going);
739
+	}
740
+
741
+
742
+	/**
743
+	 * Gets the related attendee
744
+	 *
745
+	 * @return EE_Attendee
746
+	 * @throws EE_Error
747
+	 */
748
+	public function attendee()
749
+	{
750
+		return $this->get_first_related('Attendee');
751
+	}
752
+
753
+
754
+	/**
755
+	 *        get Event ID
756
+	 */
757
+	public function event_ID()
758
+	{
759
+		return $this->get('EVT_ID');
760
+	}
761
+
762
+
763
+	/**
764
+	 *        get Event ID
765
+	 */
766
+	public function event_name()
767
+	{
768
+		$event = $this->event_obj();
769
+		if ($event) {
770
+			return $event->name();
771
+		} else {
772
+			return null;
773
+		}
774
+	}
775
+
776
+
777
+	/**
778
+	 * Fetches the event this registration is for
779
+	 *
780
+	 * @return EE_Event
781
+	 * @throws EE_Error
782
+	 */
783
+	public function event_obj()
784
+	{
785
+		return $this->get_first_related('Event');
786
+	}
787
+
788
+
789
+	/**
790
+	 *        get Attendee ID
791
+	 */
792
+	public function attendee_ID()
793
+	{
794
+		return $this->get('ATT_ID');
795
+	}
796
+
797
+
798
+	/**
799
+	 *        get PHP Session ID
800
+	 */
801
+	public function session_ID()
802
+	{
803
+		return $this->get('REG_session');
804
+	}
805
+
806
+
807
+	/**
808
+	 * Gets the string which represents the URL trigger for the receipt template in the message template system.
809
+	 *
810
+	 * @param string $messenger 'pdf' or 'html'.  Default 'html'.
811
+	 * @return string
812
+	 */
813
+	public function receipt_url($messenger = 'html')
814
+	{
815
+
816
+		/**
817
+		 * The below will be deprecated one version after this.  We check first if there is a custom receipt template
818
+		 * already in use on old system.  If there is then we just return the standard url for it.
819
+		 *
820
+		 * @since 4.5.0
821
+		 */
822
+		$template_relative_path = 'modules/gateways/Invoice/lib/templates/receipt_body.template.php';
823
+		$has_custom = EEH_Template::locate_template(
824
+			$template_relative_path,
825
+			array(),
826
+			true,
827
+			true,
828
+			true
829
+		);
830
+
831
+		if ($has_custom) {
832
+			return add_query_arg(array('receipt' => 'true'), $this->invoice_url('launch'));
833
+		}
834
+		return apply_filters('FHEE__EE_Registration__receipt_url__receipt_url', '', $this, $messenger, 'receipt');
835
+	}
836
+
837
+
838
+	/**
839
+	 * Gets the string which represents the URL trigger for the invoice template in the message template system.
840
+	 *
841
+	 * @param string $messenger 'pdf' or 'html'.  Default 'html'.
842
+	 * @return string
843
+	 * @throws EE_Error
844
+	 */
845
+	public function invoice_url($messenger = 'html')
846
+	{
847
+		/**
848
+		 * The below will be deprecated one version after this.  We check first if there is a custom invoice template
849
+		 * already in use on old system.  If there is then we just return the standard url for it.
850
+		 *
851
+		 * @since 4.5.0
852
+		 */
853
+		$template_relative_path = 'modules/gateways/Invoice/lib/templates/invoice_body.template.php';
854
+		$has_custom = EEH_Template::locate_template(
855
+			$template_relative_path,
856
+			array(),
857
+			true,
858
+			true,
859
+			true
860
+		);
861
+
862
+		if ($has_custom) {
863
+			if ($messenger == 'html') {
864
+				return $this->invoice_url('launch');
865
+			}
866
+			$route = $messenger == 'download' || $messenger == 'pdf' ? 'download_invoice' : 'launch_invoice';
867
+
868
+			$query_args = array('ee' => $route, 'id' => $this->reg_url_link());
869
+			if ($messenger == 'html') {
870
+				$query_args['html'] = true;
871
+			}
872
+			return add_query_arg($query_args, get_permalink(EE_Registry::instance()->CFG->core->thank_you_page_id));
873
+		}
874
+		return apply_filters('FHEE__EE_Registration__invoice_url__invoice_url', '', $this, $messenger, 'invoice');
875
+	}
876
+
877
+
878
+	/**
879
+	 * get Registration URL Link
880
+	 *
881
+	 * @access public
882
+	 * @return string
883
+	 * @throws EE_Error
884
+	 */
885
+	public function reg_url_link()
886
+	{
887
+		return (string) $this->get('REG_url_link');
888
+	}
889
+
890
+
891
+	/**
892
+	 * Echoes out invoice_url()
893
+	 *
894
+	 * @param string $type 'download','launch', or 'html' (default is 'launch')
895
+	 * @return void
896
+	 * @throws EE_Error
897
+	 */
898
+	public function e_invoice_url($type = 'launch')
899
+	{
900
+		echo $this->invoice_url($type);
901
+	}
902
+
903
+
904
+	/**
905
+	 * Echoes out payment_overview_url
906
+	 */
907
+	public function e_payment_overview_url()
908
+	{
909
+		echo $this->payment_overview_url();
910
+	}
911
+
912
+
913
+	/**
914
+	 * Gets the URL for the checkout payment options reg step
915
+	 * with this registration's REG_url_link added as a query parameter
916
+	 *
917
+	 * @param bool $clear_session Set to true when you want to clear the session on revisiting the
918
+	 *                            payment overview url.
919
+	 * @return string
920
+	 * @throws InvalidInterfaceException
921
+	 * @throws InvalidDataTypeException
922
+	 * @throws EE_Error
923
+	 * @throws InvalidArgumentException
924
+	 */
925
+	public function payment_overview_url($clear_session = false)
926
+	{
927
+		return add_query_arg(
928
+			(array) apply_filters(
929
+				'FHEE__EE_Registration__payment_overview_url__query_args',
930
+				array(
931
+					'e_reg_url_link' => $this->reg_url_link(),
932
+					'step'           => 'payment_options',
933
+					'revisit'        => true,
934
+					'clear_session'  => (bool) $clear_session,
935
+				),
936
+				$this
937
+			),
938
+			EE_Registry::instance()->CFG->core->reg_page_url()
939
+		);
940
+	}
941
+
942
+
943
+	/**
944
+	 * Gets the URL for the checkout attendee information reg step
945
+	 * with this registration's REG_url_link added as a query parameter
946
+	 *
947
+	 * @return string
948
+	 * @throws InvalidInterfaceException
949
+	 * @throws InvalidDataTypeException
950
+	 * @throws EE_Error
951
+	 * @throws InvalidArgumentException
952
+	 */
953
+	public function edit_attendee_information_url()
954
+	{
955
+		return add_query_arg(
956
+			(array) apply_filters(
957
+				'FHEE__EE_Registration__edit_attendee_information_url__query_args',
958
+				array(
959
+					'e_reg_url_link' => $this->reg_url_link(),
960
+					'step'           => 'attendee_information',
961
+					'revisit'        => true,
962
+				),
963
+				$this
964
+			),
965
+			EE_Registry::instance()->CFG->core->reg_page_url()
966
+		);
967
+	}
968
+
969
+
970
+	/**
971
+	 * Simply generates and returns the appropriate admin_url link to edit this registration
972
+	 *
973
+	 * @return string
974
+	 * @throws EE_Error
975
+	 */
976
+	public function get_admin_edit_url()
977
+	{
978
+		return EEH_URL::add_query_args_and_nonce(
979
+			array(
980
+				'page'    => 'espresso_registrations',
981
+				'action'  => 'view_registration',
982
+				'_REG_ID' => $this->ID(),
983
+			),
984
+			admin_url('admin.php')
985
+		);
986
+	}
987
+
988
+
989
+	/**
990
+	 *    is_primary_registrant?
991
+	 */
992
+	public function is_primary_registrant()
993
+	{
994
+		return $this->get('REG_count') == 1 ? true : false;
995
+	}
996
+
997
+
998
+	/**
999
+	 * This returns the primary registration object for this registration group (which may be this object).
1000
+	 *
1001
+	 * @return EE_Registration
1002
+	 * @throws EE_Error
1003
+	 */
1004
+	public function get_primary_registration()
1005
+	{
1006
+		if ($this->is_primary_registrant()) {
1007
+			return $this;
1008
+		}
1009
+
1010
+		// k reg_count !== 1 so let's get the EE_Registration object matching this txn_id and reg_count == 1
1011
+		/** @var EE_Registration $primary_registrant */
1012
+		$primary_registrant = EEM_Registration::instance()->get_one(
1013
+			array(
1014
+				array(
1015
+					'TXN_ID'    => $this->transaction_ID(),
1016
+					'REG_count' => 1,
1017
+				),
1018
+			)
1019
+		);
1020
+		return $primary_registrant;
1021
+	}
1022
+
1023
+
1024
+	/**
1025
+	 *        get  Attendee Number
1026
+	 *
1027
+	 * @access        public
1028
+	 */
1029
+	public function count()
1030
+	{
1031
+		return $this->get('REG_count');
1032
+	}
1033
+
1034
+
1035
+	/**
1036
+	 *        get Group Size
1037
+	 */
1038
+	public function group_size()
1039
+	{
1040
+		return $this->get('REG_group_size');
1041
+	}
1042
+
1043
+
1044
+	/**
1045
+	 *        get Registration Date
1046
+	 */
1047
+	public function date()
1048
+	{
1049
+		return $this->get('REG_date');
1050
+	}
1051
+
1052
+
1053
+	/**
1054
+	 * gets a pretty date
1055
+	 *
1056
+	 * @param string $date_format
1057
+	 * @param string $time_format
1058
+	 * @return string
1059
+	 * @throws EE_Error
1060
+	 */
1061
+	public function pretty_date($date_format = null, $time_format = null)
1062
+	{
1063
+		return $this->get_datetime('REG_date', $date_format, $time_format);
1064
+	}
1065
+
1066
+
1067
+	/**
1068
+	 * final_price
1069
+	 * the registration's share of the transaction total, so that the
1070
+	 * sum of all the transaction's REG_final_prices equal the transaction's total
1071
+	 *
1072
+	 * @return float
1073
+	 * @throws EE_Error
1074
+	 */
1075
+	public function final_price()
1076
+	{
1077
+		return $this->get('REG_final_price');
1078
+	}
1079
+
1080
+
1081
+	/**
1082
+	 * pretty_final_price
1083
+	 *  final price as formatted string, with correct decimal places and currency symbol
1084
+	 *
1085
+	 * @return string
1086
+	 * @throws EE_Error
1087
+	 */
1088
+	public function pretty_final_price()
1089
+	{
1090
+		return $this->get_pretty('REG_final_price');
1091
+	}
1092
+
1093
+
1094
+	/**
1095
+	 * get paid (yeah)
1096
+	 *
1097
+	 * @return float
1098
+	 * @throws EE_Error
1099
+	 */
1100
+	public function paid()
1101
+	{
1102
+		return $this->get('REG_paid');
1103
+	}
1104
+
1105
+
1106
+	/**
1107
+	 * pretty_paid
1108
+	 *
1109
+	 * @return float
1110
+	 * @throws EE_Error
1111
+	 */
1112
+	public function pretty_paid()
1113
+	{
1114
+		return $this->get_pretty('REG_paid');
1115
+	}
1116
+
1117
+
1118
+	/**
1119
+	 * owes_monies_and_can_pay
1120
+	 * whether or not this registration has monies owing and it's' status allows payment
1121
+	 *
1122
+	 * @param array $requires_payment
1123
+	 * @return bool
1124
+	 * @throws EE_Error
1125
+	 */
1126
+	public function owes_monies_and_can_pay($requires_payment = array())
1127
+	{
1128
+		// these reg statuses require payment (if event is not free)
1129
+		$requires_payment = ! empty($requires_payment)
1130
+			? $requires_payment
1131
+			: EEM_Registration::reg_statuses_that_allow_payment();
1132
+		if (in_array($this->status_ID(), $requires_payment) &&
1133
+			$this->final_price() != 0 &&
1134
+			$this->final_price() != $this->paid()
1135
+		) {
1136
+			return true;
1137
+		} else {
1138
+			return false;
1139
+		}
1140
+	}
1141
+
1142
+
1143
+	/**
1144
+	 * Prints out the return value of $this->pretty_status()
1145
+	 *
1146
+	 * @param bool $show_icons
1147
+	 * @return void
1148
+	 * @throws EE_Error
1149
+	 */
1150
+	public function e_pretty_status($show_icons = false)
1151
+	{
1152
+		echo $this->pretty_status($show_icons);
1153
+	}
1154
+
1155
+
1156
+	/**
1157
+	 * Returns a nice version of the status for displaying to customers
1158
+	 *
1159
+	 * @param bool $show_icons
1160
+	 * @return string
1161
+	 * @throws EE_Error
1162
+	 */
1163
+	public function pretty_status($show_icons = false)
1164
+	{
1165
+		$status = EEM_Status::instance()->localized_status(
1166
+			array($this->status_ID() => esc_html__('unknown', 'event_espresso')),
1167
+			false,
1168
+			'sentence'
1169
+		);
1170
+		$icon = '';
1171
+		switch ($this->status_ID()) {
1172
+			case EEM_Registration::status_id_approved:
1173
+				$icon = $show_icons
1174
+					? '<span class="dashicons dashicons-star-filled ee-icon-size-16 green-text"></span>'
1175
+					: '';
1176
+				break;
1177
+			case EEM_Registration::status_id_pending_payment:
1178
+				$icon = $show_icons
1179
+					? '<span class="dashicons dashicons-star-half ee-icon-size-16 orange-text"></span>'
1180
+					: '';
1181
+				break;
1182
+			case EEM_Registration::status_id_not_approved:
1183
+				$icon = $show_icons
1184
+					? '<span class="dashicons dashicons-marker ee-icon-size-16 orange-text"></span>'
1185
+					: '';
1186
+				break;
1187
+			case EEM_Registration::status_id_cancelled:
1188
+				$icon = $show_icons
1189
+					? '<span class="dashicons dashicons-no ee-icon-size-16 lt-grey-text"></span>'
1190
+					: '';
1191
+				break;
1192
+			case EEM_Registration::status_id_incomplete:
1193
+				$icon = $show_icons
1194
+					? '<span class="dashicons dashicons-no ee-icon-size-16 lt-orange-text"></span>'
1195
+					: '';
1196
+				break;
1197
+			case EEM_Registration::status_id_declined:
1198
+				$icon = $show_icons
1199
+					? '<span class="dashicons dashicons-no ee-icon-size-16 red-text"></span>'
1200
+					: '';
1201
+				break;
1202
+			case EEM_Registration::status_id_wait_list:
1203
+				$icon = $show_icons
1204
+					? '<span class="dashicons dashicons-clipboard ee-icon-size-16 purple-text"></span>'
1205
+					: '';
1206
+				break;
1207
+		}
1208
+		return $icon . $status[ $this->status_ID() ];
1209
+	}
1210
+
1211
+
1212
+	/**
1213
+	 *        get Attendee Is Going
1214
+	 */
1215
+	public function att_is_going()
1216
+	{
1217
+		return $this->get('REG_att_is_going');
1218
+	}
1219
+
1220
+
1221
+	/**
1222
+	 * Gets related answers
1223
+	 *
1224
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1225
+	 * @return EE_Answer[]
1226
+	 * @throws EE_Error
1227
+	 */
1228
+	public function answers($query_params = null)
1229
+	{
1230
+		return $this->get_many_related('Answer', $query_params);
1231
+	}
1232
+
1233
+
1234
+	/**
1235
+	 * Gets the registration's answer value to the specified question
1236
+	 * (either the question's ID or a question object)
1237
+	 *
1238
+	 * @param EE_Question|int $question
1239
+	 * @param bool            $pretty_value
1240
+	 * @return array|string if pretty_value= true, the result will always be a string
1241
+	 * (because the answer might be an array of answer values, so passing pretty_value=true
1242
+	 * will convert it into some kind of string)
1243
+	 * @throws EE_Error
1244
+	 */
1245
+	public function answer_value_to_question($question, $pretty_value = true)
1246
+	{
1247
+		$question_id = EEM_Question::instance()->ensure_is_ID($question);
1248
+		return EEM_Answer::instance()->get_answer_value_to_question($this, $question_id, $pretty_value);
1249
+	}
1250
+
1251
+
1252
+	/**
1253
+	 * question_groups
1254
+	 * returns an array of EE_Question_Group objects for this registration
1255
+	 *
1256
+	 * @return EE_Question_Group[]
1257
+	 * @throws EE_Error
1258
+	 * @throws EntityNotFoundException
1259
+	 */
1260
+	public function question_groups()
1261
+	{
1262
+		$question_groups = array();
1263
+		if ($this->event() instanceof EE_Event) {
1264
+			$question_groups = $this->event()->question_groups(
1265
+				array(
1266
+					array(
1267
+						'Event_Question_Group.EQG_primary' => $this->count() == 1 ? true : false,
1268
+					),
1269
+					'order_by' => array('QSG_order' => 'ASC'),
1270
+				)
1271
+			);
1272
+		}
1273
+		return $question_groups;
1274
+	}
1275
+
1276
+
1277
+	/**
1278
+	 * count_question_groups
1279
+	 * returns a count of the number of EE_Question_Group objects for this registration
1280
+	 *
1281
+	 * @return int
1282
+	 * @throws EE_Error
1283
+	 * @throws EntityNotFoundException
1284
+	 */
1285
+	public function count_question_groups()
1286
+	{
1287
+		$qg_count = 0;
1288
+		if ($this->event() instanceof EE_Event) {
1289
+			$qg_count = $this->event()->count_related(
1290
+				'Question_Group',
1291
+				array(
1292
+					array(
1293
+						'Event_Question_Group.EQG_primary' => $this->count() == 1 ? true : false,
1294
+					),
1295
+				)
1296
+			);
1297
+		}
1298
+		return $qg_count;
1299
+	}
1300
+
1301
+
1302
+	/**
1303
+	 * Returns the registration date in the 'standard' string format
1304
+	 * (function may be improved in the future to allow for different formats and timezones)
1305
+	 *
1306
+	 * @return string
1307
+	 * @throws EE_Error
1308
+	 */
1309
+	public function reg_date()
1310
+	{
1311
+		return $this->get_datetime('REG_date');
1312
+	}
1313
+
1314
+
1315
+	/**
1316
+	 * Gets the datetime-ticket for this registration (ie, it can be used to isolate
1317
+	 * the ticket this registration purchased, or the datetime they have registered
1318
+	 * to attend)
1319
+	 *
1320
+	 * @return EE_Datetime_Ticket
1321
+	 * @throws EE_Error
1322
+	 */
1323
+	public function datetime_ticket()
1324
+	{
1325
+		return $this->get_first_related('Datetime_Ticket');
1326
+	}
1327
+
1328
+
1329
+	/**
1330
+	 * Sets the registration's datetime_ticket.
1331
+	 *
1332
+	 * @param EE_Datetime_Ticket $datetime_ticket
1333
+	 * @return EE_Datetime_Ticket
1334
+	 * @throws EE_Error
1335
+	 */
1336
+	public function set_datetime_ticket($datetime_ticket)
1337
+	{
1338
+		return $this->_add_relation_to($datetime_ticket, 'Datetime_Ticket');
1339
+	}
1340
+
1341
+	/**
1342
+	 * Gets deleted
1343
+	 *
1344
+	 * @return bool
1345
+	 * @throws EE_Error
1346
+	 */
1347
+	public function deleted()
1348
+	{
1349
+		return $this->get('REG_deleted');
1350
+	}
1351
+
1352
+	/**
1353
+	 * Sets deleted
1354
+	 *
1355
+	 * @param boolean $deleted
1356
+	 * @return bool
1357
+	 * @throws EE_Error
1358
+	 * @throws RuntimeException
1359
+	 */
1360
+	public function set_deleted($deleted)
1361
+	{
1362
+		if ($deleted) {
1363
+			$this->delete();
1364
+		} else {
1365
+			$this->restore();
1366
+		}
1367
+	}
1368
+
1369
+
1370
+	/**
1371
+	 * Get the status object of this object
1372
+	 *
1373
+	 * @return EE_Status
1374
+	 * @throws EE_Error
1375
+	 */
1376
+	public function status_obj()
1377
+	{
1378
+		return $this->get_first_related('Status');
1379
+	}
1380
+
1381
+
1382
+	/**
1383
+	 * Returns the number of times this registration has checked into any of the datetimes
1384
+	 * its available for
1385
+	 *
1386
+	 * @return int
1387
+	 * @throws EE_Error
1388
+	 */
1389
+	public function count_checkins()
1390
+	{
1391
+		return $this->get_model()->count_related($this, 'Checkin');
1392
+	}
1393
+
1394
+
1395
+	/**
1396
+	 * Returns the number of current Check-ins this registration is checked into for any of the datetimes the
1397
+	 * registration is for.  Note, this is ONLY checked in (does not include checkedout)
1398
+	 *
1399
+	 * @return int
1400
+	 * @throws EE_Error
1401
+	 */
1402
+	public function count_checkins_not_checkedout()
1403
+	{
1404
+		return $this->get_model()->count_related($this, 'Checkin', array(array('CHK_in' => 1)));
1405
+	}
1406
+
1407
+
1408
+	/**
1409
+	 * The purpose of this method is simply to check whether this registration can checkin to the given datetime.
1410
+	 *
1411
+	 * @param int | EE_Datetime $DTT_OR_ID      The datetime the registration is being checked against
1412
+	 * @param bool              $check_approved This is used to indicate whether the caller wants can_checkin to also
1413
+	 *                                          consider registration status as well as datetime access.
1414
+	 * @return bool
1415
+	 * @throws EE_Error
1416
+	 */
1417
+	public function can_checkin($DTT_OR_ID, $check_approved = true)
1418
+	{
1419
+		$DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1420
+
1421
+		// first check registration status
1422
+		if (($check_approved && ! $this->is_approved()) || ! $DTT_ID) {
1423
+			return false;
1424
+		}
1425
+		// is there a datetime ticket that matches this dtt_ID?
1426
+		if (! (EEM_Datetime_Ticket::instance()->exists(
1427
+			array(
1428
+				array(
1429
+					'TKT_ID' => $this->get('TKT_ID'),
1430
+					'DTT_ID' => $DTT_ID,
1431
+				),
1432
+			)
1433
+		))
1434
+		) {
1435
+			return false;
1436
+		}
1437
+
1438
+		// final check is against TKT_uses
1439
+		return $this->verify_can_checkin_against_TKT_uses($DTT_ID);
1440
+	}
1441
+
1442
+
1443
+	/**
1444
+	 * This method verifies whether the user can checkin for the given datetime considering the max uses value set on
1445
+	 * the ticket. To do this,  a query is done to get the count of the datetime records already checked into.  If the
1446
+	 * datetime given does not have a check-in record and checking in for that datetime will exceed the allowed uses,
1447
+	 * then return false.  Otherwise return true.
1448
+	 *
1449
+	 * @param int | EE_Datetime $DTT_OR_ID The datetime the registration is being checked against
1450
+	 * @return bool true means can checkin.  false means cannot checkin.
1451
+	 * @throws EE_Error
1452
+	 */
1453
+	public function verify_can_checkin_against_TKT_uses($DTT_OR_ID)
1454
+	{
1455
+		$DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1456
+
1457
+		if (! $DTT_ID) {
1458
+			return false;
1459
+		}
1460
+
1461
+		$max_uses = $this->ticket() instanceof EE_Ticket ? $this->ticket()->uses() : EE_INF;
1462
+
1463
+		// if max uses is not set or equals infinity then return true cause its not a factor for whether user can
1464
+		// check-in or not.
1465
+		if (! $max_uses || $max_uses === EE_INF) {
1466
+			return true;
1467
+		}
1468
+
1469
+		// does this datetime have a checkin record?  If so, then the dtt count has already been verified so we can just
1470
+		// go ahead and toggle.
1471
+		if (EEM_Checkin::instance()->exists(array(array('REG_ID' => $this->ID(), 'DTT_ID' => $DTT_ID)))) {
1472
+			return true;
1473
+		}
1474
+
1475
+		// made it here so the last check is whether the number of checkins per unique datetime on this registration
1476
+		// disallows further check-ins.
1477
+		$count_unique_dtt_checkins = EEM_Checkin::instance()->count(
1478
+			array(
1479
+				array(
1480
+					'REG_ID' => $this->ID(),
1481
+					'CHK_in' => true,
1482
+				),
1483
+			),
1484
+			'DTT_ID',
1485
+			true
1486
+		);
1487
+		// checkins have already reached their max number of uses
1488
+		// so registrant can NOT checkin
1489
+		if ($count_unique_dtt_checkins >= $max_uses) {
1490
+			EE_Error::add_error(
1491
+				esc_html__(
1492
+					'Check-in denied because number of datetime uses for the ticket has been reached or exceeded.',
1493
+					'event_espresso'
1494
+				),
1495
+				__FILE__,
1496
+				__FUNCTION__,
1497
+				__LINE__
1498
+			);
1499
+			return false;
1500
+		}
1501
+		return true;
1502
+	}
1503
+
1504
+
1505
+	/**
1506
+	 * toggle Check-in status for this registration
1507
+	 * Check-ins are toggled in the following order:
1508
+	 * never checked in -> checked in
1509
+	 * checked in -> checked out
1510
+	 * checked out -> checked in
1511
+	 *
1512
+	 * @param  int $DTT_ID  include specific datetime to toggle Check-in for.
1513
+	 *                      If not included or null, then it is assumed latest datetime is being toggled.
1514
+	 * @param bool $verify  If true then can_checkin() is used to verify whether the person
1515
+	 *                      can be checked in or not.  Otherwise this forces change in checkin status.
1516
+	 * @return bool|int     the chk_in status toggled to OR false if nothing got changed.
1517
+	 * @throws EE_Error
1518
+	 */
1519
+	public function toggle_checkin_status($DTT_ID = null, $verify = false)
1520
+	{
1521
+		if (empty($DTT_ID)) {
1522
+			$datetime = $this->get_latest_related_datetime();
1523
+			$DTT_ID = $datetime instanceof EE_Datetime ? $datetime->ID() : 0;
1524
+			// verify the registration can checkin for the given DTT_ID
1525
+		} elseif (! $this->can_checkin($DTT_ID, $verify)) {
1526
+			EE_Error::add_error(
1527
+				sprintf(
1528
+					esc_html__(
1529
+						'The given registration (ID:%1$d) can not be checked in to the given DTT_ID (%2$d), because the registration does not have access',
1530
+						'event_espresso'
1531
+					),
1532
+					$this->ID(),
1533
+					$DTT_ID
1534
+				),
1535
+				__FILE__,
1536
+				__FUNCTION__,
1537
+				__LINE__
1538
+			);
1539
+			return false;
1540
+		}
1541
+		$status_paths = array(
1542
+			EE_Checkin::status_checked_never => EE_Checkin::status_checked_in,
1543
+			EE_Checkin::status_checked_in    => EE_Checkin::status_checked_out,
1544
+			EE_Checkin::status_checked_out   => EE_Checkin::status_checked_in,
1545
+		);
1546
+		// start by getting the current status so we know what status we'll be changing to.
1547
+		$cur_status = $this->check_in_status_for_datetime($DTT_ID, null);
1548
+		$status_to = $status_paths[ $cur_status ];
1549
+		// database only records true for checked IN or false for checked OUT
1550
+		// no record ( null ) means checked in NEVER, but we obviously don't save that
1551
+		$new_status = $status_to === EE_Checkin::status_checked_in ? true : false;
1552
+		// add relation - note Check-ins are always creating new rows
1553
+		// because we are keeping track of Check-ins over time.
1554
+		// Eventually we'll probably want to show a list table
1555
+		// for the individual Check-ins so that they can be managed.
1556
+		$checkin = EE_Checkin::new_instance(
1557
+			array(
1558
+				'REG_ID' => $this->ID(),
1559
+				'DTT_ID' => $DTT_ID,
1560
+				'CHK_in' => $new_status,
1561
+			)
1562
+		);
1563
+		// if the record could not be saved then return false
1564
+		if ($checkin->save() === 0) {
1565
+			if (WP_DEBUG) {
1566
+				global $wpdb;
1567
+				$error = sprintf(
1568
+					esc_html__(
1569
+						'Registration check in update failed because of the following database error: %1$s%2$s',
1570
+						'event_espresso'
1571
+					),
1572
+					'<br />',
1573
+					$wpdb->last_error
1574
+				);
1575
+			} else {
1576
+				$error = esc_html__(
1577
+					'Registration check in update failed because of an unknown database error',
1578
+					'event_espresso'
1579
+				);
1580
+			}
1581
+			EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__);
1582
+			return false;
1583
+		}
1584
+		return $status_to;
1585
+	}
1586
+
1587
+
1588
+	/**
1589
+	 * Returns the latest datetime related to this registration (via the ticket attached to the registration).
1590
+	 * "Latest" is defined by the `DTT_EVT_start` column.
1591
+	 *
1592
+	 * @return EE_Datetime|null
1593
+	 * @throws EE_Error
1594
+	 */
1595
+	public function get_latest_related_datetime()
1596
+	{
1597
+		return EEM_Datetime::instance()->get_one(
1598
+			array(
1599
+				array(
1600
+					'Ticket.Registration.REG_ID' => $this->ID(),
1601
+				),
1602
+				'order_by' => array('DTT_EVT_start' => 'DESC'),
1603
+			)
1604
+		);
1605
+	}
1606
+
1607
+
1608
+	/**
1609
+	 * Returns the earliest datetime related to this registration (via the ticket attached to the registration).
1610
+	 * "Earliest" is defined by the `DTT_EVT_start` column.
1611
+	 *
1612
+	 * @throws EE_Error
1613
+	 */
1614
+	public function get_earliest_related_datetime()
1615
+	{
1616
+		return EEM_Datetime::instance()->get_one(
1617
+			array(
1618
+				array(
1619
+					'Ticket.Registration.REG_ID' => $this->ID(),
1620
+				),
1621
+				'order_by' => array('DTT_EVT_start' => 'ASC'),
1622
+			)
1623
+		);
1624
+	}
1625
+
1626
+
1627
+	/**
1628
+	 * This method simply returns the check-in status for this registration and the given datetime.
1629
+	 * If neither the datetime nor the checkin values are provided as arguments,
1630
+	 * then this will return the LATEST check-in status for the registration across all datetimes it belongs to.
1631
+	 *
1632
+	 * @param  int       $DTT_ID  The ID of the datetime we're checking against
1633
+	 *                            (if empty we'll get the primary datetime for
1634
+	 *                            this registration (via event) and use it's ID);
1635
+	 * @param EE_Checkin $checkin If present, we use the given checkin object rather than the dtt_id.
1636
+	 *
1637
+	 * @return int                Integer representing Check-in status.
1638
+	 * @throws EE_Error
1639
+	 */
1640
+	public function check_in_status_for_datetime($DTT_ID = 0, $checkin = null)
1641
+	{
1642
+		$checkin_query_params = array(
1643
+			'order_by' => array('CHK_timestamp' => 'DESC'),
1644
+		);
1645
+
1646
+		if ($DTT_ID > 0) {
1647
+			$checkin_query_params[0] = array('DTT_ID' => $DTT_ID);
1648
+		}
1649
+
1650
+		// get checkin object (if exists)
1651
+		$checkin = $checkin instanceof EE_Checkin
1652
+			? $checkin
1653
+			: $this->get_first_related('Checkin', $checkin_query_params);
1654
+		if ($checkin instanceof EE_Checkin) {
1655
+			if ($checkin->get('CHK_in')) {
1656
+				return EE_Checkin::status_checked_in; // checked in
1657
+			}
1658
+			return EE_Checkin::status_checked_out; // had checked in but is now checked out.
1659
+		}
1660
+		return EE_Checkin::status_checked_never; // never been checked in
1661
+	}
1662
+
1663
+
1664
+	/**
1665
+	 * This method returns a localized message for the toggled Check-in message.
1666
+	 *
1667
+	 * @param  int $DTT_ID include specific datetime to get the correct Check-in message.  If not included or null,
1668
+	 *                     then it is assumed Check-in for primary datetime was toggled.
1669
+	 * @param bool $error  This just flags that you want an error message returned. This is put in so that the error
1670
+	 *                     message can be customized with the attendee name.
1671
+	 * @return string internationalized message
1672
+	 * @throws EE_Error
1673
+	 */
1674
+	public function get_checkin_msg($DTT_ID, $error = false)
1675
+	{
1676
+		// let's get the attendee first so we can include the name of the attendee
1677
+		$attendee = $this->get_first_related('Attendee');
1678
+		if ($attendee instanceof EE_Attendee) {
1679
+			if ($error) {
1680
+				return sprintf(__("%s's check-in status was not changed.", "event_espresso"), $attendee->full_name());
1681
+			}
1682
+			$cur_status = $this->check_in_status_for_datetime($DTT_ID);
1683
+			// what is the status message going to be?
1684
+			switch ($cur_status) {
1685
+				case EE_Checkin::status_checked_never:
1686
+					return sprintf(
1687
+						__("%s has been removed from Check-in records", "event_espresso"),
1688
+						$attendee->full_name()
1689
+					);
1690
+					break;
1691
+				case EE_Checkin::status_checked_in:
1692
+					return sprintf(__('%s has been checked in', 'event_espresso'), $attendee->full_name());
1693
+					break;
1694
+				case EE_Checkin::status_checked_out:
1695
+					return sprintf(__('%s has been checked out', 'event_espresso'), $attendee->full_name());
1696
+					break;
1697
+			}
1698
+		}
1699
+		return esc_html__("The check-in status could not be determined.", "event_espresso");
1700
+	}
1701
+
1702
+
1703
+	/**
1704
+	 * Returns the related EE_Transaction to this registration
1705
+	 *
1706
+	 * @return EE_Transaction
1707
+	 * @throws EE_Error
1708
+	 * @throws EntityNotFoundException
1709
+	 */
1710
+	public function transaction()
1711
+	{
1712
+		$transaction = $this->get_first_related('Transaction');
1713
+		if (! $transaction instanceof \EE_Transaction) {
1714
+			throw new EntityNotFoundException('Transaction ID', $this->transaction_ID());
1715
+		}
1716
+		return $transaction;
1717
+	}
1718
+
1719
+
1720
+	/**
1721
+	 *        get Registration Code
1722
+	 */
1723
+	public function reg_code()
1724
+	{
1725
+		return $this->get('REG_code');
1726
+	}
1727
+
1728
+
1729
+	/**
1730
+	 *        get Transaction ID
1731
+	 */
1732
+	public function transaction_ID()
1733
+	{
1734
+		return $this->get('TXN_ID');
1735
+	}
1736
+
1737
+
1738
+	/**
1739
+	 * @return int
1740
+	 * @throws EE_Error
1741
+	 */
1742
+	public function ticket_ID()
1743
+	{
1744
+		return $this->get('TKT_ID');
1745
+	}
1746
+
1747
+
1748
+	/**
1749
+	 *        Set Registration Code
1750
+	 *
1751
+	 * @access    public
1752
+	 * @param    string  $REG_code Registration Code
1753
+	 * @param    boolean $use_default
1754
+	 * @throws EE_Error
1755
+	 */
1756
+	public function set_reg_code($REG_code, $use_default = false)
1757
+	{
1758
+		if (empty($REG_code)) {
1759
+			EE_Error::add_error(
1760
+				esc_html__('REG_code can not be empty.', 'event_espresso'),
1761
+				__FILE__,
1762
+				__FUNCTION__,
1763
+				__LINE__
1764
+			);
1765
+			return;
1766
+		}
1767
+		if (! $this->reg_code()) {
1768
+			parent::set('REG_code', $REG_code, $use_default);
1769
+		} else {
1770
+			EE_Error::doing_it_wrong(
1771
+				__CLASS__ . '::' . __FUNCTION__,
1772
+				esc_html__('Can not change a registration REG_code once it has been set.', 'event_espresso'),
1773
+				'4.6.0'
1774
+			);
1775
+		}
1776
+	}
1777
+
1778
+
1779
+	/**
1780
+	 * Returns all other registrations in the same group as this registrant who have the same ticket option.
1781
+	 * Note, if you want to just get all registrations in the same transaction (group), use:
1782
+	 *    $registration->transaction()->registrations();
1783
+	 *
1784
+	 * @since 4.5.0
1785
+	 * @return EE_Registration[] or empty array if this isn't a group registration.
1786
+	 * @throws EE_Error
1787
+	 */
1788
+	public function get_all_other_registrations_in_group()
1789
+	{
1790
+		if ($this->group_size() < 2) {
1791
+			return array();
1792
+		}
1793
+
1794
+		$query[0] = array(
1795
+			'TXN_ID' => $this->transaction_ID(),
1796
+			'REG_ID' => array('!=', $this->ID()),
1797
+			'TKT_ID' => $this->ticket_ID(),
1798
+		);
1799
+		/** @var EE_Registration[] $registrations */
1800
+		$registrations = $this->get_model()->get_all($query);
1801
+		return $registrations;
1802
+	}
1803
+
1804
+	/**
1805
+	 * Return the link to the admin details for the object.
1806
+	 *
1807
+	 * @return string
1808
+	 * @throws EE_Error
1809
+	 */
1810
+	public function get_admin_details_link()
1811
+	{
1812
+		EE_Registry::instance()->load_helper('URL');
1813
+		return EEH_URL::add_query_args_and_nonce(
1814
+			array(
1815
+				'page'    => 'espresso_registrations',
1816
+				'action'  => 'view_registration',
1817
+				'_REG_ID' => $this->ID(),
1818
+			),
1819
+			admin_url('admin.php')
1820
+		);
1821
+	}
1822
+
1823
+	/**
1824
+	 * Returns the link to the editor for the object.  Sometimes this is the same as the details.
1825
+	 *
1826
+	 * @return string
1827
+	 * @throws EE_Error
1828
+	 */
1829
+	public function get_admin_edit_link()
1830
+	{
1831
+		return $this->get_admin_details_link();
1832
+	}
1833
+
1834
+	/**
1835
+	 * Returns the link to a settings page for the object.
1836
+	 *
1837
+	 * @return string
1838
+	 * @throws EE_Error
1839
+	 */
1840
+	public function get_admin_settings_link()
1841
+	{
1842
+		return $this->get_admin_details_link();
1843
+	}
1844
+
1845
+	/**
1846
+	 * Returns the link to the "overview" for the object (typically the "list table" view).
1847
+	 *
1848
+	 * @return string
1849
+	 */
1850
+	public function get_admin_overview_link()
1851
+	{
1852
+		EE_Registry::instance()->load_helper('URL');
1853
+		return EEH_URL::add_query_args_and_nonce(
1854
+			array(
1855
+				'page' => 'espresso_registrations',
1856
+			),
1857
+			admin_url('admin.php')
1858
+		);
1859
+	}
1860
+
1861
+
1862
+	/**
1863
+	 * @param array $query_params
1864
+	 *
1865
+	 * @return \EE_Registration[]
1866
+	 * @throws EE_Error
1867
+	 */
1868
+	public function payments($query_params = array())
1869
+	{
1870
+		return $this->get_many_related('Payment', $query_params);
1871
+	}
1872
+
1873
+
1874
+	/**
1875
+	 * @param array $query_params
1876
+	 *
1877
+	 * @return \EE_Registration_Payment[]
1878
+	 * @throws EE_Error
1879
+	 */
1880
+	public function registration_payments($query_params = array())
1881
+	{
1882
+		return $this->get_many_related('Registration_Payment', $query_params);
1883
+	}
1884
+
1885
+
1886
+	/**
1887
+	 * This grabs the payment method corresponding to the last payment made for the amount owing on the registration.
1888
+	 * Note: if there are no payments on the registration there will be no payment method returned.
1889
+	 *
1890
+	 * @return EE_Payment_Method|null
1891
+	 */
1892
+	public function payment_method()
1893
+	{
1894
+		return EEM_Payment_Method::instance()->get_last_used_for_registration($this);
1895
+	}
1896
+
1897
+
1898
+	/**
1899
+	 * @return \EE_Line_Item
1900
+	 * @throws EntityNotFoundException
1901
+	 * @throws EE_Error
1902
+	 */
1903
+	public function ticket_line_item()
1904
+	{
1905
+		$ticket = $this->ticket();
1906
+		$transaction = $this->transaction();
1907
+		$line_item = null;
1908
+		$ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs(
1909
+			$transaction->total_line_item(),
1910
+			'Ticket',
1911
+			array($ticket->ID())
1912
+		);
1913
+		foreach ($ticket_line_items as $ticket_line_item) {
1914
+			if ($ticket_line_item instanceof \EE_Line_Item
1915
+				&& $ticket_line_item->OBJ_type() === 'Ticket'
1916
+				&& $ticket_line_item->OBJ_ID() === $ticket->ID()
1917
+			) {
1918
+				$line_item = $ticket_line_item;
1919
+				break;
1920
+			}
1921
+		}
1922
+		if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) {
1923
+			throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID());
1924
+		}
1925
+		return $line_item;
1926
+	}
1927
+
1928
+
1929
+	/**
1930
+	 * Soft Deletes this model object.
1931
+	 *
1932
+	 * @return boolean | int
1933
+	 * @throws RuntimeException
1934
+	 * @throws EE_Error
1935
+	 */
1936
+	public function delete()
1937
+	{
1938
+		if ($this->update_extra_meta(EE_Registration::PRE_TRASH_REG_STATUS_KEY, $this->status_ID()) === true) {
1939
+			$this->set_status(EEM_Registration::status_id_cancelled);
1940
+		}
1941
+		return parent::delete();
1942
+	}
1943
+
1944
+
1945
+	/**
1946
+	 * Restores whatever the previous status was on a registration before it was trashed (if possible)
1947
+	 *
1948
+	 * @throws EE_Error
1949
+	 * @throws RuntimeException
1950
+	 */
1951
+	public function restore()
1952
+	{
1953
+		$previous_status = $this->get_extra_meta(
1954
+			EE_Registration::PRE_TRASH_REG_STATUS_KEY,
1955
+			true,
1956
+			EEM_Registration::status_id_cancelled
1957
+		);
1958
+		if ($previous_status) {
1959
+			$this->delete_extra_meta(EE_Registration::PRE_TRASH_REG_STATUS_KEY);
1960
+			$this->set_status($previous_status);
1961
+		}
1962
+		return parent::restore();
1963
+	}
1964
+
1965
+
1966
+	/**
1967
+	 * possibly toggle Registration status based on comparison of REG_paid vs REG_final_price
1968
+	 *
1969
+	 * @param  boolean $trigger_set_status_logic EE_Registration::set_status() can trigger additional logic
1970
+	 *                                           depending on whether the reg status changes to or from "Approved"
1971
+	 * @return boolean whether the Registration status was updated
1972
+	 * @throws EE_Error
1973
+	 * @throws RuntimeException
1974
+	 */
1975
+	public function updateStatusBasedOnTotalPaid($trigger_set_status_logic = true)
1976
+	{
1977
+		$paid = $this->paid();
1978
+		$price = $this->final_price();
1979
+		switch (true) {
1980
+			// overpaid or paid
1981
+			case EEH_Money::compare_floats($paid, $price, '>'):
1982
+			case EEH_Money::compare_floats($paid, $price):
1983
+				$new_status = EEM_Registration::status_id_approved;
1984
+				break;
1985
+			//  underpaid
1986
+			case EEH_Money::compare_floats($paid, $price, '<'):
1987
+				$new_status = EEM_Registration::status_id_pending_payment;
1988
+				break;
1989
+			// uhhh Houston...
1990
+			default:
1991
+				throw new RuntimeException(
1992
+					esc_html__('The total paid calculation for this registration is inaccurate.', 'event_espresso')
1993
+				);
1994
+		}
1995
+		if ($new_status !== $this->status_ID()) {
1996
+			if ($trigger_set_status_logic) {
1997
+				return $this->set_status($new_status);
1998
+			}
1999
+			parent::set('STS_ID', $new_status);
2000
+			return true;
2001
+		}
2002
+		return false;
2003
+	}
2004
+
2005
+
2006
+	/*************************** DEPRECATED ***************************/
2007
+
2008
+
2009
+	/**
2010
+	 * @deprecated
2011
+	 * @since     4.7.0
2012
+	 * @access    public
2013
+	 */
2014
+	public function price_paid()
2015
+	{
2016
+		EE_Error::doing_it_wrong(
2017
+			'EE_Registration::price_paid()',
2018
+			esc_html__(
2019
+				'This method is deprecated, please use EE_Registration::final_price() instead.',
2020
+				'event_espresso'
2021
+			),
2022
+			'4.7.0'
2023
+		);
2024
+		return $this->final_price();
2025
+	}
2026
+
2027
+
2028
+	/**
2029
+	 * @deprecated
2030
+	 * @since     4.7.0
2031
+	 * @access    public
2032
+	 * @param    float $REG_final_price
2033
+	 * @throws EE_Error
2034
+	 * @throws RuntimeException
2035
+	 */
2036
+	public function set_price_paid($REG_final_price = 0.00)
2037
+	{
2038
+		EE_Error::doing_it_wrong(
2039
+			'EE_Registration::set_price_paid()',
2040
+			esc_html__(
2041
+				'This method is deprecated, please use EE_Registration::set_final_price() instead.',
2042
+				'event_espresso'
2043
+			),
2044
+			'4.7.0'
2045
+		);
2046
+		$this->set_final_price($REG_final_price);
2047
+	}
2048
+
2049
+
2050
+	/**
2051
+	 * @deprecated
2052
+	 * @since 4.7.0
2053
+	 * @return string
2054
+	 * @throws EE_Error
2055
+	 */
2056
+	public function pretty_price_paid()
2057
+	{
2058
+		EE_Error::doing_it_wrong(
2059
+			'EE_Registration::pretty_price_paid()',
2060
+			esc_html__(
2061
+				'This method is deprecated, please use EE_Registration::pretty_final_price() instead.',
2062
+				'event_espresso'
2063
+			),
2064
+			'4.7.0'
2065
+		);
2066
+		return $this->pretty_final_price();
2067
+	}
2068
+
2069
+
2070
+	/**
2071
+	 * Gets the primary datetime related to this registration via the related Event to this registration
2072
+	 *
2073
+	 * @deprecated 4.9.17
2074
+	 * @return EE_Datetime
2075
+	 * @throws EE_Error
2076
+	 * @throws EntityNotFoundException
2077
+	 */
2078
+	public function get_related_primary_datetime()
2079
+	{
2080
+		EE_Error::doing_it_wrong(
2081
+			__METHOD__,
2082
+			esc_html__(
2083
+				'Use EE_Registration::get_latest_related_datetime() or EE_Registration::get_earliest_related_datetime()',
2084
+				'event_espresso'
2085
+			),
2086
+			'4.9.17',
2087
+			'5.0.0'
2088
+		);
2089
+		return $this->event()->primary_datetime();
2090
+	}
2091 2091
 }
Please login to merge, or discard this patch.