Completed
Branch FET/conditional-update-queries (7f5e44)
by
unknown
20:59 queued 12:06
created
core/db_classes/EE_Line_Item.class.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -781,7 +781,7 @@
 block discarded – undo
781 781
     /**
782 782
      * Gets the event that's related to the ticket, if this line item represents a ticket.
783 783
      *
784
-     * @return EE_Event|null
784
+     * @return EE_Base_Class|null
785 785
      * @throws EE_Error
786 786
      * @throws InvalidArgumentException
787 787
      * @throws InvalidDataTypeException
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
     protected function __construct($fieldValues = array(), $bydb = false, $timezone = '')
89 89
     {
90 90
         parent::__construct($fieldValues, $bydb, $timezone);
91
-        if (! $this->get('LIN_code')) {
91
+        if ( ! $this->get('LIN_code')) {
92 92
             $this->set_code($this->generate_code());
93 93
         }
94 94
     }
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
     public function name()
156 156
     {
157 157
         $name = $this->get('LIN_name');
158
-        if (! $name) {
158
+        if ( ! $name) {
159 159
             $name = ucwords(str_replace('-', ' ', $this->type()));
160 160
         }
161 161
         return $name;
@@ -615,7 +615,7 @@  discard block
 block discarded – undo
615 615
                 )
616 616
             );
617 617
         }
618
-        if (! is_array($this->_children)) {
618
+        if ( ! is_array($this->_children)) {
619 619
             $this->_children = array();
620 620
         }
621 621
         return $this->_children;
@@ -856,7 +856,7 @@  discard block
 block discarded – undo
856 856
             }
857 857
             return $line_item->save();
858 858
         }
859
-        $this->_children[ $line_item->code() ] = $line_item;
859
+        $this->_children[$line_item->code()] = $line_item;
860 860
         if ($line_item->parent() !== $this) {
861 861
             $line_item->set_parent($this);
862 862
         }
@@ -880,7 +880,7 @@  discard block
 block discarded – undo
880 880
     public function set_parent($line_item)
881 881
     {
882 882
         if ($this->ID()) {
883
-            if (! $line_item->ID()) {
883
+            if ( ! $line_item->ID()) {
884 884
                 $line_item->save();
885 885
             }
886 886
             $this->set_parent_ID($line_item->ID());
@@ -912,8 +912,8 @@  discard block
 block discarded – undo
912 912
                 array(array('LIN_parent' => $this->ID(), 'LIN_code' => $code))
913 913
             );
914 914
         }
915
-        return isset($this->_children[ $code ])
916
-            ? $this->_children[ $code ]
915
+        return isset($this->_children[$code])
916
+            ? $this->_children[$code]
917 917
             : null;
918 918
     }
919 919
 
@@ -973,8 +973,8 @@  discard block
 block discarded – undo
973 973
             }
974 974
             return $items_deleted;
975 975
         }
976
-        if (isset($this->_children[ $code ])) {
977
-            unset($this->_children[ $code ]);
976
+        if (isset($this->_children[$code])) {
977
+            unset($this->_children[$code]);
978 978
             return 1;
979 979
         }
980 980
         return 0;
@@ -1015,7 +1015,7 @@  discard block
 block discarded – undo
1015 1015
     public function generate_code()
1016 1016
     {
1017 1017
         // each line item in the cart requires a unique identifier
1018
-        return md5($this->get('OBJ_type') . $this->get('OBJ_ID') . microtime());
1018
+        return md5($this->get('OBJ_type').$this->get('OBJ_ID').microtime());
1019 1019
     }
1020 1020
 
1021 1021
 
@@ -1220,7 +1220,7 @@  discard block
 block discarded – undo
1220 1220
         $has_children = ! empty($my_children);
1221 1221
         if ($has_children && $this->is_line_item()) {
1222 1222
             $total = $this->_recalculate_pretax_total_for_line_item($total, $my_children);
1223
-        } elseif (! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) {
1223
+        } elseif ( ! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) {
1224 1224
             $total = $this->unit_price() * $this->quantity();
1225 1225
         } elseif ($this->is_sub_total() || $this->is_total()) {
1226 1226
             $total = $this->_recalculate_pretax_total_for_subtotal($total, $my_children);
@@ -1229,18 +1229,18 @@  discard block
 block discarded – undo
1229 1229
             return 0;
1230 1230
         }
1231 1231
         // ensure all non-line items and non-sub-line-items have a quantity of 1 (except for Events)
1232
-        if (! $this->is_line_item() && ! $this->is_sub_line_item() && ! $this->is_cancellation()
1232
+        if ( ! $this->is_line_item() && ! $this->is_sub_line_item() && ! $this->is_cancellation()
1233 1233
         ) {
1234 1234
             if ($this->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_EVENT) {
1235 1235
                 $this->set_quantity(1);
1236 1236
             }
1237
-            if (! $this->is_percent()) {
1237
+            if ( ! $this->is_percent()) {
1238 1238
                 $this->set_unit_price($total);
1239 1239
             }
1240 1240
         }
1241 1241
         // we don't want to bother saving grand totals, because that needs to factor in taxes anyways
1242 1242
         // so it ought to be
1243
-        if (! $this->is_total()) {
1243
+        if ( ! $this->is_total()) {
1244 1244
             $this->set_total($total);
1245 1245
             // if not a percent line item, make sure we keep the unit price in sync
1246 1246
             if ($has_children
@@ -1587,7 +1587,7 @@  discard block
 block discarded – undo
1587 1587
     public function save_this_and_descendants_to_txn($txn_id = null)
1588 1588
     {
1589 1589
         $count = 0;
1590
-        if (! $txn_id) {
1590
+        if ( ! $txn_id) {
1591 1591
             $txn_id = $this->TXN_ID();
1592 1592
         }
1593 1593
         $this->set_TXN_ID($txn_id);
Please login to merge, or discard this patch.
Indentation   +1735 added lines, -1735 removed lines patch added patch discarded remove patch
@@ -14,1739 +14,1739 @@
 block discarded – undo
14 14
 class EE_Line_Item extends EE_Base_Class implements EEI_Line_Item
15 15
 {
16 16
 
17
-    /**
18
-     * for children line items (currently not a normal relation)
19
-     *
20
-     * @type EE_Line_Item[]
21
-     */
22
-    protected $_children = array();
23
-
24
-    /**
25
-     * for the parent line item
26
-     *
27
-     * @var EE_Line_Item
28
-     */
29
-    protected $_parent;
30
-
31
-
32
-    /**
33
-     * @param array  $props_n_values          incoming values
34
-     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
35
-     *                                        used.)
36
-     * @param array  $date_formats            incoming date_formats in an array where the first value is the
37
-     *                                        date_format and the second value is the time format
38
-     * @return EE_Line_Item
39
-     * @throws EE_Error
40
-     * @throws InvalidArgumentException
41
-     * @throws InvalidDataTypeException
42
-     * @throws InvalidInterfaceException
43
-     * @throws ReflectionException
44
-     */
45
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
46
-    {
47
-        $has_object = parent::_check_for_object(
48
-            $props_n_values,
49
-            __CLASS__,
50
-            $timezone,
51
-            $date_formats
52
-        );
53
-        return $has_object
54
-            ? $has_object
55
-            : new self($props_n_values, false, $timezone);
56
-    }
57
-
58
-
59
-    /**
60
-     * @param array  $props_n_values  incoming values from the database
61
-     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
62
-     *                                the website will be used.
63
-     * @return EE_Line_Item
64
-     * @throws EE_Error
65
-     * @throws InvalidArgumentException
66
-     * @throws InvalidDataTypeException
67
-     * @throws InvalidInterfaceException
68
-     * @throws ReflectionException
69
-     */
70
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
71
-    {
72
-        return new self($props_n_values, true, $timezone);
73
-    }
74
-
75
-
76
-    /**
77
-     * Adds some defaults if they're not specified
78
-     *
79
-     * @param array  $fieldValues
80
-     * @param bool   $bydb
81
-     * @param string $timezone
82
-     * @throws EE_Error
83
-     * @throws InvalidArgumentException
84
-     * @throws InvalidDataTypeException
85
-     * @throws InvalidInterfaceException
86
-     * @throws ReflectionException
87
-     */
88
-    protected function __construct($fieldValues = array(), $bydb = false, $timezone = '')
89
-    {
90
-        parent::__construct($fieldValues, $bydb, $timezone);
91
-        if (! $this->get('LIN_code')) {
92
-            $this->set_code($this->generate_code());
93
-        }
94
-    }
95
-
96
-
97
-    /**
98
-     * Gets ID
99
-     *
100
-     * @return int
101
-     * @throws EE_Error
102
-     * @throws InvalidArgumentException
103
-     * @throws InvalidDataTypeException
104
-     * @throws InvalidInterfaceException
105
-     * @throws ReflectionException
106
-     */
107
-    public function ID()
108
-    {
109
-        return $this->get('LIN_ID');
110
-    }
111
-
112
-
113
-    /**
114
-     * Gets TXN_ID
115
-     *
116
-     * @return int
117
-     * @throws EE_Error
118
-     * @throws InvalidArgumentException
119
-     * @throws InvalidDataTypeException
120
-     * @throws InvalidInterfaceException
121
-     * @throws ReflectionException
122
-     */
123
-    public function TXN_ID()
124
-    {
125
-        return $this->get('TXN_ID');
126
-    }
127
-
128
-
129
-    /**
130
-     * Sets TXN_ID
131
-     *
132
-     * @param int $TXN_ID
133
-     * @throws EE_Error
134
-     * @throws InvalidArgumentException
135
-     * @throws InvalidDataTypeException
136
-     * @throws InvalidInterfaceException
137
-     * @throws ReflectionException
138
-     */
139
-    public function set_TXN_ID($TXN_ID)
140
-    {
141
-        $this->set('TXN_ID', $TXN_ID);
142
-    }
143
-
144
-
145
-    /**
146
-     * Gets name
147
-     *
148
-     * @return string
149
-     * @throws EE_Error
150
-     * @throws InvalidArgumentException
151
-     * @throws InvalidDataTypeException
152
-     * @throws InvalidInterfaceException
153
-     * @throws ReflectionException
154
-     */
155
-    public function name()
156
-    {
157
-        $name = $this->get('LIN_name');
158
-        if (! $name) {
159
-            $name = ucwords(str_replace('-', ' ', $this->type()));
160
-        }
161
-        return $name;
162
-    }
163
-
164
-
165
-    /**
166
-     * Sets name
167
-     *
168
-     * @param string $name
169
-     * @throws EE_Error
170
-     * @throws InvalidArgumentException
171
-     * @throws InvalidDataTypeException
172
-     * @throws InvalidInterfaceException
173
-     * @throws ReflectionException
174
-     */
175
-    public function set_name($name)
176
-    {
177
-        $this->set('LIN_name', $name);
178
-    }
179
-
180
-
181
-    /**
182
-     * Gets desc
183
-     *
184
-     * @return string
185
-     * @throws EE_Error
186
-     * @throws InvalidArgumentException
187
-     * @throws InvalidDataTypeException
188
-     * @throws InvalidInterfaceException
189
-     * @throws ReflectionException
190
-     */
191
-    public function desc()
192
-    {
193
-        return $this->get('LIN_desc');
194
-    }
195
-
196
-
197
-    /**
198
-     * Sets desc
199
-     *
200
-     * @param string $desc
201
-     * @throws EE_Error
202
-     * @throws InvalidArgumentException
203
-     * @throws InvalidDataTypeException
204
-     * @throws InvalidInterfaceException
205
-     * @throws ReflectionException
206
-     */
207
-    public function set_desc($desc)
208
-    {
209
-        $this->set('LIN_desc', $desc);
210
-    }
211
-
212
-
213
-    /**
214
-     * Gets quantity
215
-     *
216
-     * @return int
217
-     * @throws EE_Error
218
-     * @throws InvalidArgumentException
219
-     * @throws InvalidDataTypeException
220
-     * @throws InvalidInterfaceException
221
-     * @throws ReflectionException
222
-     */
223
-    public function quantity()
224
-    {
225
-        return $this->get('LIN_quantity');
226
-    }
227
-
228
-
229
-    /**
230
-     * Sets quantity
231
-     *
232
-     * @param int $quantity
233
-     * @throws EE_Error
234
-     * @throws InvalidArgumentException
235
-     * @throws InvalidDataTypeException
236
-     * @throws InvalidInterfaceException
237
-     * @throws ReflectionException
238
-     */
239
-    public function set_quantity($quantity)
240
-    {
241
-        $this->set('LIN_quantity', max($quantity, 0));
242
-    }
243
-
244
-
245
-    /**
246
-     * Gets item_id
247
-     *
248
-     * @return string
249
-     * @throws EE_Error
250
-     * @throws InvalidArgumentException
251
-     * @throws InvalidDataTypeException
252
-     * @throws InvalidInterfaceException
253
-     * @throws ReflectionException
254
-     */
255
-    public function OBJ_ID()
256
-    {
257
-        return $this->get('OBJ_ID');
258
-    }
259
-
260
-
261
-    /**
262
-     * Sets item_id
263
-     *
264
-     * @param string $item_id
265
-     * @throws EE_Error
266
-     * @throws InvalidArgumentException
267
-     * @throws InvalidDataTypeException
268
-     * @throws InvalidInterfaceException
269
-     * @throws ReflectionException
270
-     */
271
-    public function set_OBJ_ID($item_id)
272
-    {
273
-        $this->set('OBJ_ID', $item_id);
274
-    }
275
-
276
-
277
-    /**
278
-     * Gets item_type
279
-     *
280
-     * @return string
281
-     * @throws EE_Error
282
-     * @throws InvalidArgumentException
283
-     * @throws InvalidDataTypeException
284
-     * @throws InvalidInterfaceException
285
-     * @throws ReflectionException
286
-     */
287
-    public function OBJ_type()
288
-    {
289
-        return $this->get('OBJ_type');
290
-    }
291
-
292
-
293
-    /**
294
-     * Gets item_type
295
-     *
296
-     * @return string
297
-     * @throws EE_Error
298
-     * @throws InvalidArgumentException
299
-     * @throws InvalidDataTypeException
300
-     * @throws InvalidInterfaceException
301
-     * @throws ReflectionException
302
-     */
303
-    public function OBJ_type_i18n()
304
-    {
305
-        $obj_type = $this->OBJ_type();
306
-        switch ($obj_type) {
307
-            case EEM_Line_Item::OBJ_TYPE_EVENT:
308
-                $obj_type = esc_html__('Event', 'event_espresso');
309
-                break;
310
-            case EEM_Line_Item::OBJ_TYPE_PRICE:
311
-                $obj_type = esc_html__('Price', 'event_espresso');
312
-                break;
313
-            case EEM_Line_Item::OBJ_TYPE_PROMOTION:
314
-                $obj_type = esc_html__('Promotion', 'event_espresso');
315
-                break;
316
-            case EEM_Line_Item::OBJ_TYPE_TICKET:
317
-                $obj_type = esc_html__('Ticket', 'event_espresso');
318
-                break;
319
-            case EEM_Line_Item::OBJ_TYPE_TRANSACTION:
320
-                $obj_type = esc_html__('Transaction', 'event_espresso');
321
-                break;
322
-        }
323
-        return apply_filters('FHEE__EE_Line_Item__OBJ_type_i18n', $obj_type, $this);
324
-    }
325
-
326
-
327
-    /**
328
-     * Sets item_type
329
-     *
330
-     * @param string $OBJ_type
331
-     * @throws EE_Error
332
-     * @throws InvalidArgumentException
333
-     * @throws InvalidDataTypeException
334
-     * @throws InvalidInterfaceException
335
-     * @throws ReflectionException
336
-     */
337
-    public function set_OBJ_type($OBJ_type)
338
-    {
339
-        $this->set('OBJ_type', $OBJ_type);
340
-    }
341
-
342
-
343
-    /**
344
-     * Gets unit_price
345
-     *
346
-     * @return float
347
-     * @throws EE_Error
348
-     * @throws InvalidArgumentException
349
-     * @throws InvalidDataTypeException
350
-     * @throws InvalidInterfaceException
351
-     * @throws ReflectionException
352
-     */
353
-    public function unit_price()
354
-    {
355
-        return $this->get('LIN_unit_price');
356
-    }
357
-
358
-
359
-    /**
360
-     * Sets unit_price
361
-     *
362
-     * @param float $unit_price
363
-     * @throws EE_Error
364
-     * @throws InvalidArgumentException
365
-     * @throws InvalidDataTypeException
366
-     * @throws InvalidInterfaceException
367
-     * @throws ReflectionException
368
-     */
369
-    public function set_unit_price($unit_price)
370
-    {
371
-        $this->set('LIN_unit_price', $unit_price);
372
-    }
373
-
374
-
375
-    /**
376
-     * Checks if this item is a percentage modifier or not
377
-     *
378
-     * @return boolean
379
-     * @throws EE_Error
380
-     * @throws InvalidArgumentException
381
-     * @throws InvalidDataTypeException
382
-     * @throws InvalidInterfaceException
383
-     * @throws ReflectionException
384
-     */
385
-    public function is_percent()
386
-    {
387
-        if ($this->is_tax_sub_total()) {
388
-            // tax subtotals HAVE a percent on them, that percentage only applies
389
-            // to taxable items, so its' an exception. Treat it like a flat line item
390
-            return false;
391
-        }
392
-        $unit_price = abs($this->get('LIN_unit_price'));
393
-        $percent = abs($this->get('LIN_percent'));
394
-        if ($unit_price < .001 && $percent) {
395
-            return true;
396
-        }
397
-        if ($unit_price >= .001 && ! $percent) {
398
-            return false;
399
-        }
400
-        if ($unit_price >= .001 && $percent) {
401
-            throw new EE_Error(
402
-                sprintf(
403
-                    esc_html__(
404
-                        'A Line Item can not have a unit price of (%s) AND a percent (%s)!',
405
-                        'event_espresso'
406
-                    ),
407
-                    $unit_price,
408
-                    $percent
409
-                )
410
-            );
411
-        }
412
-        // if they're both 0, assume its not a percent item
413
-        return false;
414
-    }
415
-
416
-
417
-    /**
418
-     * Gets percent (between 100-.001)
419
-     *
420
-     * @return float
421
-     * @throws EE_Error
422
-     * @throws InvalidArgumentException
423
-     * @throws InvalidDataTypeException
424
-     * @throws InvalidInterfaceException
425
-     * @throws ReflectionException
426
-     */
427
-    public function percent()
428
-    {
429
-        return $this->get('LIN_percent');
430
-    }
431
-
432
-
433
-    /**
434
-     * Sets percent (between 100-0.01)
435
-     *
436
-     * @param float $percent
437
-     * @throws EE_Error
438
-     * @throws InvalidArgumentException
439
-     * @throws InvalidDataTypeException
440
-     * @throws InvalidInterfaceException
441
-     * @throws ReflectionException
442
-     */
443
-    public function set_percent($percent)
444
-    {
445
-        $this->set('LIN_percent', $percent);
446
-    }
447
-
448
-
449
-    /**
450
-     * Gets total
451
-     *
452
-     * @return float
453
-     * @throws EE_Error
454
-     * @throws InvalidArgumentException
455
-     * @throws InvalidDataTypeException
456
-     * @throws InvalidInterfaceException
457
-     * @throws ReflectionException
458
-     */
459
-    public function total()
460
-    {
461
-        return $this->get('LIN_total');
462
-    }
463
-
464
-
465
-    /**
466
-     * Sets total
467
-     *
468
-     * @param float $total
469
-     * @throws EE_Error
470
-     * @throws InvalidArgumentException
471
-     * @throws InvalidDataTypeException
472
-     * @throws InvalidInterfaceException
473
-     * @throws ReflectionException
474
-     */
475
-    public function set_total($total)
476
-    {
477
-        $this->set('LIN_total', $total);
478
-    }
479
-
480
-
481
-    /**
482
-     * Gets order
483
-     *
484
-     * @return int
485
-     * @throws EE_Error
486
-     * @throws InvalidArgumentException
487
-     * @throws InvalidDataTypeException
488
-     * @throws InvalidInterfaceException
489
-     * @throws ReflectionException
490
-     */
491
-    public function order()
492
-    {
493
-        return $this->get('LIN_order');
494
-    }
495
-
496
-
497
-    /**
498
-     * Sets order
499
-     *
500
-     * @param int $order
501
-     * @throws EE_Error
502
-     * @throws InvalidArgumentException
503
-     * @throws InvalidDataTypeException
504
-     * @throws InvalidInterfaceException
505
-     * @throws ReflectionException
506
-     */
507
-    public function set_order($order)
508
-    {
509
-        $this->set('LIN_order', $order);
510
-    }
511
-
512
-
513
-    /**
514
-     * Gets parent
515
-     *
516
-     * @return int
517
-     * @throws EE_Error
518
-     * @throws InvalidArgumentException
519
-     * @throws InvalidDataTypeException
520
-     * @throws InvalidInterfaceException
521
-     * @throws ReflectionException
522
-     */
523
-    public function parent_ID()
524
-    {
525
-        return $this->get('LIN_parent');
526
-    }
527
-
528
-
529
-    /**
530
-     * Sets parent
531
-     *
532
-     * @param int $parent
533
-     * @throws EE_Error
534
-     * @throws InvalidArgumentException
535
-     * @throws InvalidDataTypeException
536
-     * @throws InvalidInterfaceException
537
-     * @throws ReflectionException
538
-     */
539
-    public function set_parent_ID($parent)
540
-    {
541
-        $this->set('LIN_parent', $parent);
542
-    }
543
-
544
-
545
-    /**
546
-     * Gets type
547
-     *
548
-     * @return string
549
-     * @throws EE_Error
550
-     * @throws InvalidArgumentException
551
-     * @throws InvalidDataTypeException
552
-     * @throws InvalidInterfaceException
553
-     * @throws ReflectionException
554
-     */
555
-    public function type()
556
-    {
557
-        return $this->get('LIN_type');
558
-    }
559
-
560
-
561
-    /**
562
-     * Sets type
563
-     *
564
-     * @param string $type
565
-     * @throws EE_Error
566
-     * @throws InvalidArgumentException
567
-     * @throws InvalidDataTypeException
568
-     * @throws InvalidInterfaceException
569
-     * @throws ReflectionException
570
-     */
571
-    public function set_type($type)
572
-    {
573
-        $this->set('LIN_type', $type);
574
-    }
575
-
576
-
577
-    /**
578
-     * Gets the line item of which this item is a composite. Eg, if this is a subtotal, the parent might be a total\
579
-     * If this line item is saved to the DB, fetches the parent from the DB. However, if this line item isn't in the DB
580
-     * it uses its cached reference to its parent line item (which would have been set by `EE_Line_Item::set_parent()`
581
-     * or indirectly by `EE_Line_item::add_child_line_item()`)
582
-     *
583
-     * @return EE_Base_Class|EE_Line_Item
584
-     * @throws EE_Error
585
-     * @throws InvalidArgumentException
586
-     * @throws InvalidDataTypeException
587
-     * @throws InvalidInterfaceException
588
-     * @throws ReflectionException
589
-     */
590
-    public function parent()
591
-    {
592
-        return $this->ID()
593
-            ? $this->get_model()->get_one_by_ID($this->parent_ID())
594
-            : $this->_parent;
595
-    }
596
-
597
-
598
-    /**
599
-     * Gets ALL the children of this line item (ie, all the parts that contribute towards this total).
600
-     *
601
-     * @return EE_Base_Class[]|EE_Line_Item[]
602
-     * @throws EE_Error
603
-     * @throws InvalidArgumentException
604
-     * @throws InvalidDataTypeException
605
-     * @throws InvalidInterfaceException
606
-     * @throws ReflectionException
607
-     */
608
-    public function children()
609
-    {
610
-        if ($this->ID()) {
611
-            return $this->get_model()->get_all(
612
-                array(
613
-                    array('LIN_parent' => $this->ID()),
614
-                    'order_by' => array('LIN_order' => 'ASC'),
615
-                )
616
-            );
617
-        }
618
-        if (! is_array($this->_children)) {
619
-            $this->_children = array();
620
-        }
621
-        return $this->_children;
622
-    }
623
-
624
-
625
-    /**
626
-     * Gets code
627
-     *
628
-     * @return string
629
-     * @throws EE_Error
630
-     * @throws InvalidArgumentException
631
-     * @throws InvalidDataTypeException
632
-     * @throws InvalidInterfaceException
633
-     * @throws ReflectionException
634
-     */
635
-    public function code()
636
-    {
637
-        return $this->get('LIN_code');
638
-    }
639
-
640
-
641
-    /**
642
-     * Sets code
643
-     *
644
-     * @param string $code
645
-     * @throws EE_Error
646
-     * @throws InvalidArgumentException
647
-     * @throws InvalidDataTypeException
648
-     * @throws InvalidInterfaceException
649
-     * @throws ReflectionException
650
-     */
651
-    public function set_code($code)
652
-    {
653
-        $this->set('LIN_code', $code);
654
-    }
655
-
656
-
657
-    /**
658
-     * Gets is_taxable
659
-     *
660
-     * @return boolean
661
-     * @throws EE_Error
662
-     * @throws InvalidArgumentException
663
-     * @throws InvalidDataTypeException
664
-     * @throws InvalidInterfaceException
665
-     * @throws ReflectionException
666
-     */
667
-    public function is_taxable()
668
-    {
669
-        return $this->get('LIN_is_taxable');
670
-    }
671
-
672
-
673
-    /**
674
-     * Sets is_taxable
675
-     *
676
-     * @param boolean $is_taxable
677
-     * @throws EE_Error
678
-     * @throws InvalidArgumentException
679
-     * @throws InvalidDataTypeException
680
-     * @throws InvalidInterfaceException
681
-     * @throws ReflectionException
682
-     */
683
-    public function set_is_taxable($is_taxable)
684
-    {
685
-        $this->set('LIN_is_taxable', $is_taxable);
686
-    }
687
-
688
-
689
-    /**
690
-     * Gets the object that this model-joins-to.
691
-     * returns one of the model objects that the field OBJ_ID can point to... see the 'OBJ_ID' field on
692
-     * EEM_Promotion_Object
693
-     *        Eg, if this line item join model object is for a ticket, this will return the EE_Ticket object
694
-     *
695
-     * @return EE_Base_Class | NULL
696
-     * @throws EE_Error
697
-     * @throws InvalidArgumentException
698
-     * @throws InvalidDataTypeException
699
-     * @throws InvalidInterfaceException
700
-     * @throws ReflectionException
701
-     */
702
-    public function get_object()
703
-    {
704
-        $model_name_of_related_obj = $this->OBJ_type();
705
-        return $this->get_model()->has_relation($model_name_of_related_obj)
706
-            ? $this->get_first_related($model_name_of_related_obj)
707
-            : null;
708
-    }
709
-
710
-
711
-    /**
712
-     * Like EE_Line_Item::get_object(), but can only ever actually return an EE_Ticket.
713
-     * (IE, if this line item is for a price or something else, will return NULL)
714
-     *
715
-     * @param array $query_params
716
-     * @return EE_Base_Class|EE_Ticket
717
-     * @throws EE_Error
718
-     * @throws InvalidArgumentException
719
-     * @throws InvalidDataTypeException
720
-     * @throws InvalidInterfaceException
721
-     * @throws ReflectionException
722
-     */
723
-    public function ticket($query_params = array())
724
-    {
725
-        // we're going to assume that when this method is called
726
-        // we always want to receive the attached ticket EVEN if that ticket is archived.
727
-        // This can be overridden via the incoming $query_params argument
728
-        $remove_defaults = array('default_where_conditions' => 'none');
729
-        $query_params = array_merge($remove_defaults, $query_params);
730
-        return $this->get_first_related(EEM_Line_Item::OBJ_TYPE_TICKET, $query_params);
731
-    }
732
-
733
-
734
-    /**
735
-     * Gets the EE_Datetime that's related to the ticket, IF this is for a ticket
736
-     *
737
-     * @return EE_Datetime | NULL
738
-     * @throws EE_Error
739
-     * @throws InvalidArgumentException
740
-     * @throws InvalidDataTypeException
741
-     * @throws InvalidInterfaceException
742
-     * @throws ReflectionException
743
-     */
744
-    public function get_ticket_datetime()
745
-    {
746
-        if ($this->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
747
-            $ticket = $this->ticket();
748
-            if ($ticket instanceof EE_Ticket) {
749
-                $datetime = $ticket->first_datetime();
750
-                if ($datetime instanceof EE_Datetime) {
751
-                    return $datetime;
752
-                }
753
-            }
754
-        }
755
-        return null;
756
-    }
757
-
758
-
759
-    /**
760
-     * Gets the event's name that's related to the ticket, if this is for
761
-     * a ticket
762
-     *
763
-     * @return string
764
-     * @throws EE_Error
765
-     * @throws InvalidArgumentException
766
-     * @throws InvalidDataTypeException
767
-     * @throws InvalidInterfaceException
768
-     * @throws ReflectionException
769
-     */
770
-    public function ticket_event_name()
771
-    {
772
-        $event_name = esc_html__('Unknown', 'event_espresso');
773
-        $event = $this->ticket_event();
774
-        if ($event instanceof EE_Event) {
775
-            $event_name = $event->name();
776
-        }
777
-        return $event_name;
778
-    }
779
-
780
-
781
-    /**
782
-     * Gets the event that's related to the ticket, if this line item represents a ticket.
783
-     *
784
-     * @return EE_Event|null
785
-     * @throws EE_Error
786
-     * @throws InvalidArgumentException
787
-     * @throws InvalidDataTypeException
788
-     * @throws InvalidInterfaceException
789
-     * @throws ReflectionException
790
-     */
791
-    public function ticket_event()
792
-    {
793
-        $event = null;
794
-        $ticket = $this->ticket();
795
-        if ($ticket instanceof EE_Ticket) {
796
-            $datetime = $ticket->first_datetime();
797
-            if ($datetime instanceof EE_Datetime) {
798
-                $event = $datetime->event();
799
-            }
800
-        }
801
-        return $event;
802
-    }
803
-
804
-
805
-    /**
806
-     * Gets the first datetime for this lien item, assuming it's for a ticket
807
-     *
808
-     * @param string $date_format
809
-     * @param string $time_format
810
-     * @return string
811
-     * @throws EE_Error
812
-     * @throws InvalidArgumentException
813
-     * @throws InvalidDataTypeException
814
-     * @throws InvalidInterfaceException
815
-     * @throws ReflectionException
816
-     */
817
-    public function ticket_datetime_start($date_format = '', $time_format = '')
818
-    {
819
-        $first_datetime_string = esc_html__('Unknown', 'event_espresso');
820
-        $datetime = $this->get_ticket_datetime();
821
-        if ($datetime) {
822
-            $first_datetime_string = $datetime->start_date_and_time($date_format, $time_format);
823
-        }
824
-        return $first_datetime_string;
825
-    }
826
-
827
-
828
-    /**
829
-     * Adds the line item as a child to this line item. If there is another child line
830
-     * item with the same LIN_code, it is overwritten by this new one
831
-     *
832
-     * @param EEI_Line_Item $line_item
833
-     * @param bool          $set_order
834
-     * @return bool success
835
-     * @throws EE_Error
836
-     * @throws InvalidArgumentException
837
-     * @throws InvalidDataTypeException
838
-     * @throws InvalidInterfaceException
839
-     * @throws ReflectionException
840
-     */
841
-    public function add_child_line_item(EEI_Line_Item $line_item, $set_order = true)
842
-    {
843
-        // should we calculate the LIN_order for this line item ?
844
-        if ($set_order || $line_item->order() === null) {
845
-            $line_item->set_order(count($this->children()));
846
-        }
847
-        if ($this->ID()) {
848
-            // check for any duplicate line items (with the same code), if so, this replaces it
849
-            $line_item_with_same_code = $this->get_child_line_item($line_item->code());
850
-            if ($line_item_with_same_code instanceof EE_Line_Item && $line_item_with_same_code !== $line_item) {
851
-                $this->delete_child_line_item($line_item_with_same_code->code());
852
-            }
853
-            $line_item->set_parent_ID($this->ID());
854
-            if ($this->TXN_ID()) {
855
-                $line_item->set_TXN_ID($this->TXN_ID());
856
-            }
857
-            return $line_item->save();
858
-        }
859
-        $this->_children[ $line_item->code() ] = $line_item;
860
-        if ($line_item->parent() !== $this) {
861
-            $line_item->set_parent($this);
862
-        }
863
-        return true;
864
-    }
865
-
866
-
867
-    /**
868
-     * Similar to EE_Base_Class::_add_relation_to, except this isn't a normal relation.
869
-     * If this line item is saved to the DB, this is just a wrapper for set_parent_ID() and save()
870
-     * However, if this line item is NOT saved to the DB, this just caches the parent on
871
-     * the EE_Line_Item::_parent property.
872
-     *
873
-     * @param EE_Line_Item $line_item
874
-     * @throws EE_Error
875
-     * @throws InvalidArgumentException
876
-     * @throws InvalidDataTypeException
877
-     * @throws InvalidInterfaceException
878
-     * @throws ReflectionException
879
-     */
880
-    public function set_parent($line_item)
881
-    {
882
-        if ($this->ID()) {
883
-            if (! $line_item->ID()) {
884
-                $line_item->save();
885
-            }
886
-            $this->set_parent_ID($line_item->ID());
887
-            $this->save();
888
-        } else {
889
-            $this->_parent = $line_item;
890
-            $this->set_parent_ID($line_item->ID());
891
-        }
892
-    }
893
-
894
-
895
-    /**
896
-     * Gets the child line item as specified by its code. Because this returns an object (by reference)
897
-     * you can modify this child line item and the parent (this object) can know about them
898
-     * because it also has a reference to that line item
899
-     *
900
-     * @param string $code
901
-     * @return EE_Base_Class|EE_Line_Item|EE_Soft_Delete_Base_Class|NULL
902
-     * @throws EE_Error
903
-     * @throws InvalidArgumentException
904
-     * @throws InvalidDataTypeException
905
-     * @throws InvalidInterfaceException
906
-     * @throws ReflectionException
907
-     */
908
-    public function get_child_line_item($code)
909
-    {
910
-        if ($this->ID()) {
911
-            return $this->get_model()->get_one(
912
-                array(array('LIN_parent' => $this->ID(), 'LIN_code' => $code))
913
-            );
914
-        }
915
-        return isset($this->_children[ $code ])
916
-            ? $this->_children[ $code ]
917
-            : null;
918
-    }
919
-
920
-
921
-    /**
922
-     * Returns how many items are deleted (or, if this item has not been saved ot the DB yet, just how many it HAD
923
-     * cached on it)
924
-     *
925
-     * @return int
926
-     * @throws EE_Error
927
-     * @throws InvalidArgumentException
928
-     * @throws InvalidDataTypeException
929
-     * @throws InvalidInterfaceException
930
-     * @throws ReflectionException
931
-     */
932
-    public function delete_children_line_items()
933
-    {
934
-        if ($this->ID()) {
935
-            return $this->get_model()->delete(array(array('LIN_parent' => $this->ID())));
936
-        }
937
-        $count = count($this->_children);
938
-        $this->_children = array();
939
-        return $count;
940
-    }
941
-
942
-
943
-    /**
944
-     * If this line item has been saved to the DB, deletes its child with LIN_code == $code. If this line
945
-     * HAS NOT been saved to the DB, removes the child line item with index $code.
946
-     * Also searches through the child's children for a matching line item. However, once a line item has been found
947
-     * and deleted, stops searching (so if there are line items with duplicate codes, only the first one found will be
948
-     * deleted)
949
-     *
950
-     * @param string $code
951
-     * @param bool   $stop_search_once_found
952
-     * @return int count of items deleted (or simply removed from the line item's cache, if not has not been saved to
953
-     *             the DB yet)
954
-     * @throws EE_Error
955
-     * @throws InvalidArgumentException
956
-     * @throws InvalidDataTypeException
957
-     * @throws InvalidInterfaceException
958
-     * @throws ReflectionException
959
-     */
960
-    public function delete_child_line_item($code, $stop_search_once_found = true)
961
-    {
962
-        if ($this->ID()) {
963
-            $items_deleted = 0;
964
-            if ($this->code() === $code) {
965
-                $items_deleted += EEH_Line_Item::delete_all_child_items($this);
966
-                $items_deleted += (int) $this->delete();
967
-                if ($stop_search_once_found) {
968
-                    return $items_deleted;
969
-                }
970
-            }
971
-            foreach ($this->children() as $child_line_item) {
972
-                $items_deleted += $child_line_item->delete_child_line_item($code, $stop_search_once_found);
973
-            }
974
-            return $items_deleted;
975
-        }
976
-        if (isset($this->_children[ $code ])) {
977
-            unset($this->_children[ $code ]);
978
-            return 1;
979
-        }
980
-        return 0;
981
-    }
982
-
983
-
984
-    /**
985
-     * If this line item is in the database, is of the type subtotal, and
986
-     * has no children, why do we have it? It should be deleted so this function
987
-     * does that
988
-     *
989
-     * @return boolean
990
-     * @throws EE_Error
991
-     * @throws InvalidArgumentException
992
-     * @throws InvalidDataTypeException
993
-     * @throws InvalidInterfaceException
994
-     * @throws ReflectionException
995
-     */
996
-    public function delete_if_childless_subtotal()
997
-    {
998
-        if ($this->ID() && $this->type() === EEM_Line_Item::type_sub_total && ! $this->children()) {
999
-            return $this->delete();
1000
-        }
1001
-        return false;
1002
-    }
1003
-
1004
-
1005
-    /**
1006
-     * Creates a code and returns a string. doesn't assign the code to this model object
1007
-     *
1008
-     * @return string
1009
-     * @throws EE_Error
1010
-     * @throws InvalidArgumentException
1011
-     * @throws InvalidDataTypeException
1012
-     * @throws InvalidInterfaceException
1013
-     * @throws ReflectionException
1014
-     */
1015
-    public function generate_code()
1016
-    {
1017
-        // each line item in the cart requires a unique identifier
1018
-        return md5($this->get('OBJ_type') . $this->get('OBJ_ID') . microtime());
1019
-    }
1020
-
1021
-
1022
-    /**
1023
-     * @return bool
1024
-     * @throws EE_Error
1025
-     * @throws InvalidArgumentException
1026
-     * @throws InvalidDataTypeException
1027
-     * @throws InvalidInterfaceException
1028
-     * @throws ReflectionException
1029
-     */
1030
-    public function is_tax()
1031
-    {
1032
-        return $this->type() === EEM_Line_Item::type_tax;
1033
-    }
1034
-
1035
-
1036
-    /**
1037
-     * @return bool
1038
-     * @throws EE_Error
1039
-     * @throws InvalidArgumentException
1040
-     * @throws InvalidDataTypeException
1041
-     * @throws InvalidInterfaceException
1042
-     * @throws ReflectionException
1043
-     */
1044
-    public function is_tax_sub_total()
1045
-    {
1046
-        return $this->type() === EEM_Line_Item::type_tax_sub_total;
1047
-    }
1048
-
1049
-
1050
-    /**
1051
-     * @return bool
1052
-     * @throws EE_Error
1053
-     * @throws InvalidArgumentException
1054
-     * @throws InvalidDataTypeException
1055
-     * @throws InvalidInterfaceException
1056
-     * @throws ReflectionException
1057
-     */
1058
-    public function is_line_item()
1059
-    {
1060
-        return $this->type() === EEM_Line_Item::type_line_item;
1061
-    }
1062
-
1063
-
1064
-    /**
1065
-     * @return bool
1066
-     * @throws EE_Error
1067
-     * @throws InvalidArgumentException
1068
-     * @throws InvalidDataTypeException
1069
-     * @throws InvalidInterfaceException
1070
-     * @throws ReflectionException
1071
-     */
1072
-    public function is_sub_line_item()
1073
-    {
1074
-        return $this->type() === EEM_Line_Item::type_sub_line_item;
1075
-    }
1076
-
1077
-
1078
-    /**
1079
-     * @return bool
1080
-     * @throws EE_Error
1081
-     * @throws InvalidArgumentException
1082
-     * @throws InvalidDataTypeException
1083
-     * @throws InvalidInterfaceException
1084
-     * @throws ReflectionException
1085
-     */
1086
-    public function is_sub_total()
1087
-    {
1088
-        return $this->type() === EEM_Line_Item::type_sub_total;
1089
-    }
1090
-
1091
-
1092
-    /**
1093
-     * Whether or not this line item is a cancellation line item
1094
-     *
1095
-     * @return boolean
1096
-     * @throws EE_Error
1097
-     * @throws InvalidArgumentException
1098
-     * @throws InvalidDataTypeException
1099
-     * @throws InvalidInterfaceException
1100
-     * @throws ReflectionException
1101
-     */
1102
-    public function is_cancellation()
1103
-    {
1104
-        return EEM_Line_Item::type_cancellation === $this->type();
1105
-    }
1106
-
1107
-
1108
-    /**
1109
-     * @return bool
1110
-     * @throws EE_Error
1111
-     * @throws InvalidArgumentException
1112
-     * @throws InvalidDataTypeException
1113
-     * @throws InvalidInterfaceException
1114
-     * @throws ReflectionException
1115
-     */
1116
-    public function is_total()
1117
-    {
1118
-        return $this->type() === EEM_Line_Item::type_total;
1119
-    }
1120
-
1121
-
1122
-    /**
1123
-     * @return bool
1124
-     * @throws EE_Error
1125
-     * @throws InvalidArgumentException
1126
-     * @throws InvalidDataTypeException
1127
-     * @throws InvalidInterfaceException
1128
-     * @throws ReflectionException
1129
-     */
1130
-    public function is_cancelled()
1131
-    {
1132
-        return $this->type() === EEM_Line_Item::type_cancellation;
1133
-    }
1134
-
1135
-
1136
-    /**
1137
-     * @return string like '2, 004.00', formatted according to the localized currency
1138
-     * @throws EE_Error
1139
-     * @throws InvalidArgumentException
1140
-     * @throws InvalidDataTypeException
1141
-     * @throws InvalidInterfaceException
1142
-     * @throws ReflectionException
1143
-     */
1144
-    public function unit_price_no_code()
1145
-    {
1146
-        return $this->get_pretty('LIN_unit_price', 'no_currency_code');
1147
-    }
1148
-
1149
-
1150
-    /**
1151
-     * @return string like '2, 004.00', formatted according to the localized currency
1152
-     * @throws EE_Error
1153
-     * @throws InvalidArgumentException
1154
-     * @throws InvalidDataTypeException
1155
-     * @throws InvalidInterfaceException
1156
-     * @throws ReflectionException
1157
-     */
1158
-    public function total_no_code()
1159
-    {
1160
-        return $this->get_pretty('LIN_total', 'no_currency_code');
1161
-    }
1162
-
1163
-
1164
-    /**
1165
-     * Gets the final total on this item, taking taxes into account.
1166
-     * Has the side-effect of setting the sub-total as it was just calculated.
1167
-     * If this is used on a grand-total line item, also updates the transaction's
1168
-     * TXN_total (provided this line item is allowed to persist, otherwise we don't
1169
-     * want to change a persistable transaction with info from a non-persistent line item)
1170
-     *
1171
-     * @param bool $update_txn_status
1172
-     * @return float
1173
-     * @throws EE_Error
1174
-     * @throws InvalidArgumentException
1175
-     * @throws InvalidDataTypeException
1176
-     * @throws InvalidInterfaceException
1177
-     * @throws ReflectionException
1178
-     * @throws RuntimeException
1179
-     */
1180
-    public function recalculate_total_including_taxes($update_txn_status = false)
1181
-    {
1182
-        $pre_tax_total = $this->recalculate_pre_tax_total();
1183
-        $tax_total = $this->recalculate_taxes_and_tax_total();
1184
-        $total = $pre_tax_total + $tax_total;
1185
-        // no negative totals plz
1186
-        $total = max($total, 0);
1187
-        $this->set_total($total);
1188
-        // only update the related transaction's total
1189
-        // if we intend to save this line item and its a grand total
1190
-        if ($this->allow_persist() && $this->type() === EEM_Line_Item::type_total
1191
-            && $this->transaction()
1192
-               instanceof
1193
-               EE_Transaction
1194
-        ) {
1195
-            $this->transaction()->set_total($total);
1196
-            if ($update_txn_status) {
1197
-                // don't save the TXN because that will be done below
1198
-                // and the following method only saves if the status changes
1199
-                $this->transaction()->update_status_based_on_total_paid(false);
1200
-            }
1201
-            if ($this->transaction()->ID()) {
1202
-                $this->transaction()->save();
1203
-            }
1204
-        }
1205
-        $this->maybe_save();
1206
-        return $total;
1207
-    }
1208
-
1209
-
1210
-    /**
1211
-     * Recursively goes through all the children and recalculates sub-totals EXCEPT for
1212
-     * tax-sub-totals (they're a an odd beast). Updates the 'total' on each line item according to either its
1213
-     * unit price * quantity or the total of all its children EXCEPT when we're only calculating the taxable total and
1214
-     * when this is called on the grand total
1215
-     *
1216
-     * @return float
1217
-     * @throws EE_Error
1218
-     * @throws InvalidArgumentException
1219
-     * @throws InvalidDataTypeException
1220
-     * @throws InvalidInterfaceException
1221
-     * @throws ReflectionException
1222
-     */
1223
-    public function recalculate_pre_tax_total()
1224
-    {
1225
-        $total = 0;
1226
-        $my_children = $this->children();
1227
-        $has_children = ! empty($my_children);
1228
-        if ($has_children && $this->is_line_item()) {
1229
-            $total = $this->_recalculate_pretax_total_for_line_item($total, $my_children);
1230
-        } elseif (! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) {
1231
-            $total = $this->unit_price() * $this->quantity();
1232
-        } elseif ($this->is_sub_total() || $this->is_total()) {
1233
-            $total = $this->_recalculate_pretax_total_for_subtotal($total, $my_children);
1234
-        } elseif ($this->is_tax_sub_total() || $this->is_tax() || $this->is_cancelled()) {
1235
-            // completely ignore tax totals, tax sub-totals, and cancelled line items, when calculating the pre-tax-total
1236
-            return 0;
1237
-        }
1238
-        // ensure all non-line items and non-sub-line-items have a quantity of 1 (except for Events)
1239
-        if (! $this->is_line_item() && ! $this->is_sub_line_item() && ! $this->is_cancellation()
1240
-        ) {
1241
-            if ($this->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_EVENT) {
1242
-                $this->set_quantity(1);
1243
-            }
1244
-            if (! $this->is_percent()) {
1245
-                $this->set_unit_price($total);
1246
-            }
1247
-        }
1248
-        // we don't want to bother saving grand totals, because that needs to factor in taxes anyways
1249
-        // so it ought to be
1250
-        if (! $this->is_total()) {
1251
-            $this->set_total($total);
1252
-            // if not a percent line item, make sure we keep the unit price in sync
1253
-            if ($has_children
1254
-                && $this->is_line_item()
1255
-                && ! $this->is_percent()
1256
-            ) {
1257
-                if ($this->quantity() === 0) {
1258
-                    $new_unit_price = 0;
1259
-                } else {
1260
-                    $new_unit_price = $this->total() / $this->quantity();
1261
-                }
1262
-                $this->set_unit_price($new_unit_price);
1263
-            }
1264
-            $this->maybe_save();
1265
-        }
1266
-        return $total;
1267
-    }
1268
-
1269
-
1270
-    /**
1271
-     * Calculates the pretax total when this line item is a subtotal or total line item.
1272
-     * Basically does a sum-then-round approach (ie, any percent line item that are children
1273
-     * will calculate their total based on the un-rounded total we're working with so far, and
1274
-     * THEN round the result; instead of rounding as we go like with sub-line-items)
1275
-     *
1276
-     * @param float          $calculated_total_so_far
1277
-     * @param EE_Line_Item[] $my_children
1278
-     * @return float
1279
-     * @throws EE_Error
1280
-     * @throws InvalidArgumentException
1281
-     * @throws InvalidDataTypeException
1282
-     * @throws InvalidInterfaceException
1283
-     * @throws ReflectionException
1284
-     */
1285
-    protected function _recalculate_pretax_total_for_subtotal($calculated_total_so_far, $my_children = null)
1286
-    {
1287
-        if ($my_children === null) {
1288
-            $my_children = $this->children();
1289
-        }
1290
-        $subtotal_quantity = 0;
1291
-        // get the total of all its children
1292
-        foreach ($my_children as $child_line_item) {
1293
-            if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) {
1294
-                // percentage line items are based on total so far
1295
-                if ($child_line_item->is_percent()) {
1296
-                    // round as we go so that the line items add up ok
1297
-                    $percent_total = round(
1298
-                        $calculated_total_so_far * $child_line_item->percent() / 100,
1299
-                        EE_Registry::instance()->CFG->currency->dec_plc
1300
-                    );
1301
-                    $child_line_item->set_total($percent_total);
1302
-                    // so far all percent line items should have a quantity of 1
1303
-                    // (ie, no double percent discounts. Although that might be requested someday)
1304
-                    $child_line_item->set_quantity(1);
1305
-                    $child_line_item->maybe_save();
1306
-                    $calculated_total_so_far += $percent_total;
1307
-                } else {
1308
-                    // verify flat sub-line-item quantities match their parent
1309
-                    if ($child_line_item->is_sub_line_item()) {
1310
-                        $child_line_item->set_quantity($this->quantity());
1311
-                    }
1312
-                    $calculated_total_so_far += $child_line_item->recalculate_pre_tax_total();
1313
-                    $subtotal_quantity += $child_line_item->quantity();
1314
-                }
1315
-            }
1316
-        }
1317
-        if ($this->is_sub_total()) {
1318
-            // no negative totals plz
1319
-            $calculated_total_so_far = max($calculated_total_so_far, 0);
1320
-            $subtotal_quantity = $subtotal_quantity > 0 ? 1 : 0;
1321
-            $this->set_quantity($subtotal_quantity);
1322
-            $this->maybe_save();
1323
-        }
1324
-        return $calculated_total_so_far;
1325
-    }
1326
-
1327
-
1328
-    /**
1329
-     * Calculates the pretax total for a normal line item, in a round-then-sum approach
1330
-     * (where each sub-line-item is applied to the base price for the line item
1331
-     * and the result is immediately rounded, rather than summing all the sub-line-items
1332
-     * then rounding, like we do when recalculating pretax totals on totals and subtotals).
1333
-     *
1334
-     * @param float          $calculated_total_so_far
1335
-     * @param EE_Line_Item[] $my_children
1336
-     * @return float
1337
-     * @throws EE_Error
1338
-     * @throws InvalidArgumentException
1339
-     * @throws InvalidDataTypeException
1340
-     * @throws InvalidInterfaceException
1341
-     * @throws ReflectionException
1342
-     */
1343
-    protected function _recalculate_pretax_total_for_line_item($calculated_total_so_far, $my_children = null)
1344
-    {
1345
-        if ($my_children === null) {
1346
-            $my_children = $this->children();
1347
-        }
1348
-        // we need to keep track of the running total for a single item,
1349
-        // because we need to round as we go
1350
-        $unit_price_for_total = 0;
1351
-        $quantity_for_total = 1;
1352
-        // get the total of all its children
1353
-        foreach ($my_children as $child_line_item) {
1354
-            if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) {
1355
-                if ($child_line_item->is_percent()) {
1356
-                    // it should be the unit-price-so-far multiplied by teh percent multiplied by the quantity
1357
-                    // not total multiplied by percent, because that ignores rounding along-the-way
1358
-                    $percent_unit_price = round(
1359
-                        $unit_price_for_total * $child_line_item->percent() / 100,
1360
-                        EE_Registry::instance()->CFG->currency->dec_plc
1361
-                    );
1362
-                    $percent_total = $percent_unit_price * $quantity_for_total;
1363
-                    $child_line_item->set_total($percent_total);
1364
-                    // so far all percent line items should have a quantity of 1
1365
-                    // (ie, no double percent discounts. Although that might be requested someday)
1366
-                    $child_line_item->set_quantity(1);
1367
-                    $child_line_item->maybe_save();
1368
-                    $calculated_total_so_far += $percent_total;
1369
-                    $unit_price_for_total += $percent_unit_price;
1370
-                } else {
1371
-                    // verify flat sub-line-item quantities match their parent
1372
-                    if ($child_line_item->is_sub_line_item()) {
1373
-                        $child_line_item->set_quantity($this->quantity());
1374
-                    }
1375
-                    $quantity_for_total = $child_line_item->quantity();
1376
-                    $calculated_total_so_far += $child_line_item->recalculate_pre_tax_total();
1377
-                    $unit_price_for_total += $child_line_item->unit_price();
1378
-                }
1379
-            }
1380
-        }
1381
-        return $calculated_total_so_far;
1382
-    }
1383
-
1384
-
1385
-    /**
1386
-     * Recalculates the total on each individual tax (based on a recalculation of the pre-tax total), sets
1387
-     * the totals on each tax calculated, and returns the final tax total. Re-saves tax line items
1388
-     * and tax sub-total if already in the DB
1389
-     *
1390
-     * @return float
1391
-     * @throws EE_Error
1392
-     * @throws InvalidArgumentException
1393
-     * @throws InvalidDataTypeException
1394
-     * @throws InvalidInterfaceException
1395
-     * @throws ReflectionException
1396
-     */
1397
-    public function recalculate_taxes_and_tax_total()
1398
-    {
1399
-        // get all taxes
1400
-        $taxes = $this->tax_descendants();
1401
-        // calculate the pretax total
1402
-        $taxable_total = $this->taxable_total();
1403
-        $tax_total = 0;
1404
-        foreach ($taxes as $tax) {
1405
-            $total_on_this_tax = $taxable_total * $tax->percent() / 100;
1406
-            // remember the total on this line item
1407
-            $tax->set_total($total_on_this_tax);
1408
-            $tax->maybe_save();
1409
-            $tax_total += $tax->total();
1410
-        }
1411
-        $this->_recalculate_tax_sub_total();
1412
-        return $tax_total;
1413
-    }
1414
-
1415
-
1416
-    /**
1417
-     * Simply forces all the tax-sub-totals to recalculate. Assumes the taxes have been calculated
1418
-     *
1419
-     * @return void
1420
-     * @throws EE_Error
1421
-     * @throws InvalidArgumentException
1422
-     * @throws InvalidDataTypeException
1423
-     * @throws InvalidInterfaceException
1424
-     * @throws ReflectionException
1425
-     */
1426
-    private function _recalculate_tax_sub_total()
1427
-    {
1428
-        if ($this->is_tax_sub_total()) {
1429
-            $total = 0;
1430
-            $total_percent = 0;
1431
-            // simply loop through all its children (which should be taxes) and sum their total
1432
-            foreach ($this->children() as $child_tax) {
1433
-                if ($child_tax instanceof EE_Line_Item) {
1434
-                    $total += $child_tax->total();
1435
-                    $total_percent += $child_tax->percent();
1436
-                }
1437
-            }
1438
-            $this->set_total($total);
1439
-            $this->set_percent($total_percent);
1440
-            $this->maybe_save();
1441
-        } elseif ($this->is_total()) {
1442
-            foreach ($this->children() as $maybe_tax_subtotal) {
1443
-                if ($maybe_tax_subtotal instanceof EE_Line_Item) {
1444
-                    $maybe_tax_subtotal->_recalculate_tax_sub_total();
1445
-                }
1446
-            }
1447
-        }
1448
-    }
1449
-
1450
-
1451
-    /**
1452
-     * Gets the total tax on this line item. Assumes taxes have already been calculated using
1453
-     * recalculate_taxes_and_total
1454
-     *
1455
-     * @return float
1456
-     * @throws EE_Error
1457
-     * @throws InvalidArgumentException
1458
-     * @throws InvalidDataTypeException
1459
-     * @throws InvalidInterfaceException
1460
-     * @throws ReflectionException
1461
-     */
1462
-    public function get_total_tax()
1463
-    {
1464
-        $this->_recalculate_tax_sub_total();
1465
-        $total = 0;
1466
-        foreach ($this->tax_descendants() as $tax_line_item) {
1467
-            if ($tax_line_item instanceof EE_Line_Item) {
1468
-                $total += $tax_line_item->total();
1469
-            }
1470
-        }
1471
-        return $total;
1472
-    }
1473
-
1474
-
1475
-    /**
1476
-     * Gets the total for all the items purchased only
1477
-     *
1478
-     * @return float
1479
-     * @throws EE_Error
1480
-     * @throws InvalidArgumentException
1481
-     * @throws InvalidDataTypeException
1482
-     * @throws InvalidInterfaceException
1483
-     * @throws ReflectionException
1484
-     */
1485
-    public function get_items_total()
1486
-    {
1487
-        // by default, let's make sure we're consistent with the existing line item
1488
-        if ($this->is_total()) {
1489
-            $pretax_subtotal_li = EEH_Line_Item::get_pre_tax_subtotal($this);
1490
-            if ($pretax_subtotal_li instanceof EE_Line_Item) {
1491
-                return $pretax_subtotal_li->total();
1492
-            }
1493
-        }
1494
-        $total = 0;
1495
-        foreach ($this->get_items() as $item) {
1496
-            if ($item instanceof EE_Line_Item) {
1497
-                $total += $item->total();
1498
-            }
1499
-        }
1500
-        return $total;
1501
-    }
1502
-
1503
-
1504
-    /**
1505
-     * Gets all the descendants (ie, children or children of children etc) that
1506
-     * are of the type 'tax'
1507
-     *
1508
-     * @return EE_Line_Item[]
1509
-     * @throws EE_Error
1510
-     */
1511
-    public function tax_descendants()
1512
-    {
1513
-        return EEH_Line_Item::get_tax_descendants($this);
1514
-    }
1515
-
1516
-
1517
-    /**
1518
-     * Gets all the real items purchased which are children of this item
1519
-     *
1520
-     * @return EE_Line_Item[]
1521
-     * @throws EE_Error
1522
-     */
1523
-    public function get_items()
1524
-    {
1525
-        return EEH_Line_Item::get_line_item_descendants($this);
1526
-    }
1527
-
1528
-
1529
-    /**
1530
-     * Returns the amount taxable among this line item's children (or if it has no children,
1531
-     * how much of it is taxable). Does not recalculate totals or subtotals.
1532
-     * If the taxable total is negative, (eg, if none of the tickets were taxable,
1533
-     * but there is a "Taxable" discount), returns 0.
1534
-     *
1535
-     * @return float
1536
-     * @throws EE_Error
1537
-     * @throws InvalidArgumentException
1538
-     * @throws InvalidDataTypeException
1539
-     * @throws InvalidInterfaceException
1540
-     * @throws ReflectionException
1541
-     */
1542
-    public function taxable_total()
1543
-    {
1544
-        $total = 0;
1545
-        if ($this->children()) {
1546
-            foreach ($this->children() as $child_line_item) {
1547
-                if ($child_line_item->type() === EEM_Line_Item::type_line_item && $child_line_item->is_taxable()) {
1548
-                    // if it's a percent item, only take into account the percent
1549
-                    // that's taxable too (the taxable total so far)
1550
-                    if ($child_line_item->is_percent()) {
1551
-                        $total += ($total * $child_line_item->percent() / 100);
1552
-                    } else {
1553
-                        $total += $child_line_item->total();
1554
-                    }
1555
-                } elseif ($child_line_item->type() === EEM_Line_Item::type_sub_total) {
1556
-                    $total += $child_line_item->taxable_total();
1557
-                }
1558
-            }
1559
-        }
1560
-        return max($total, 0);
1561
-    }
1562
-
1563
-
1564
-    /**
1565
-     * Gets the transaction for this line item
1566
-     *
1567
-     * @return EE_Base_Class|EE_Transaction
1568
-     * @throws EE_Error
1569
-     * @throws InvalidArgumentException
1570
-     * @throws InvalidDataTypeException
1571
-     * @throws InvalidInterfaceException
1572
-     * @throws ReflectionException
1573
-     */
1574
-    public function transaction()
1575
-    {
1576
-        return $this->get_first_related(EEM_Line_Item::OBJ_TYPE_TRANSACTION);
1577
-    }
1578
-
1579
-
1580
-    /**
1581
-     * Saves this line item to the DB, and recursively saves its descendants.
1582
-     * Because there currently is no proper parent-child relation on the model,
1583
-     * save_this_and_cached() will NOT save the descendants.
1584
-     * Also sets the transaction on this line item and all its descendants before saving
1585
-     *
1586
-     * @param int $txn_id if none is provided, assumes $this->TXN_ID()
1587
-     * @return int count of items saved
1588
-     * @throws EE_Error
1589
-     * @throws InvalidArgumentException
1590
-     * @throws InvalidDataTypeException
1591
-     * @throws InvalidInterfaceException
1592
-     * @throws ReflectionException
1593
-     */
1594
-    public function save_this_and_descendants_to_txn($txn_id = null)
1595
-    {
1596
-        $count = 0;
1597
-        if (! $txn_id) {
1598
-            $txn_id = $this->TXN_ID();
1599
-        }
1600
-        $this->set_TXN_ID($txn_id);
1601
-        $children = $this->children();
1602
-        $count += $this->save()
1603
-            ? 1
1604
-            : 0;
1605
-        foreach ($children as $child_line_item) {
1606
-            if ($child_line_item instanceof EE_Line_Item) {
1607
-                $child_line_item->set_parent_ID($this->ID());
1608
-                $count += $child_line_item->save_this_and_descendants_to_txn($txn_id);
1609
-            }
1610
-        }
1611
-        return $count;
1612
-    }
1613
-
1614
-
1615
-    /**
1616
-     * Saves this line item to the DB, and recursively saves its descendants.
1617
-     *
1618
-     * @return int count of items saved
1619
-     * @throws EE_Error
1620
-     * @throws InvalidArgumentException
1621
-     * @throws InvalidDataTypeException
1622
-     * @throws InvalidInterfaceException
1623
-     * @throws ReflectionException
1624
-     */
1625
-    public function save_this_and_descendants()
1626
-    {
1627
-        $count = 0;
1628
-        $children = $this->children();
1629
-        $count += $this->save()
1630
-            ? 1
1631
-            : 0;
1632
-        foreach ($children as $child_line_item) {
1633
-            if ($child_line_item instanceof EE_Line_Item) {
1634
-                $child_line_item->set_parent_ID($this->ID());
1635
-                $count += $child_line_item->save_this_and_descendants();
1636
-            }
1637
-        }
1638
-        return $count;
1639
-    }
1640
-
1641
-
1642
-    /**
1643
-     * returns the cancellation line item if this item was cancelled
1644
-     *
1645
-     * @return EE_Line_Item[]
1646
-     * @throws InvalidArgumentException
1647
-     * @throws InvalidInterfaceException
1648
-     * @throws InvalidDataTypeException
1649
-     * @throws ReflectionException
1650
-     * @throws EE_Error
1651
-     */
1652
-    public function get_cancellations()
1653
-    {
1654
-        EE_Registry::instance()->load_helper('Line_Item');
1655
-        return EEH_Line_Item::get_descendants_of_type($this, EEM_Line_Item::type_cancellation);
1656
-    }
1657
-
1658
-
1659
-    /**
1660
-     * If this item has an ID, then this saves it again to update the db
1661
-     *
1662
-     * @return int count of items saved
1663
-     * @throws EE_Error
1664
-     * @throws InvalidArgumentException
1665
-     * @throws InvalidDataTypeException
1666
-     * @throws InvalidInterfaceException
1667
-     * @throws ReflectionException
1668
-     */
1669
-    public function maybe_save()
1670
-    {
1671
-        if ($this->ID()) {
1672
-            return $this->save();
1673
-        }
1674
-        return false;
1675
-    }
1676
-
1677
-
1678
-    /**
1679
-     * clears the cached children and parent from the line item
1680
-     *
1681
-     * @return void
1682
-     */
1683
-    public function clear_related_line_item_cache()
1684
-    {
1685
-        $this->_children = array();
1686
-        $this->_parent = null;
1687
-    }
1688
-
1689
-
1690
-    /**
1691
-     * @param bool $raw
1692
-     * @return int
1693
-     * @throws EE_Error
1694
-     * @throws InvalidArgumentException
1695
-     * @throws InvalidDataTypeException
1696
-     * @throws InvalidInterfaceException
1697
-     * @throws ReflectionException
1698
-     */
1699
-    public function timestamp($raw = false)
1700
-    {
1701
-        return $raw
1702
-            ? $this->get_raw('LIN_timestamp')
1703
-            : $this->get('LIN_timestamp');
1704
-    }
1705
-
1706
-
1707
-
1708
-
1709
-    /************************* DEPRECATED *************************/
1710
-    /**
1711
-     * @deprecated 4.6.0
1712
-     * @param string $type one of the constants on EEM_Line_Item
1713
-     * @return EE_Line_Item[]
1714
-     * @throws EE_Error
1715
-     */
1716
-    protected function _get_descendants_of_type($type)
1717
-    {
1718
-        EE_Error::doing_it_wrong(
1719
-            'EE_Line_Item::_get_descendants_of_type()',
1720
-            sprintf(
1721
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1722
-                'EEH_Line_Item::get_descendants_of_type()'
1723
-            ),
1724
-            '4.6.0'
1725
-        );
1726
-        return EEH_Line_Item::get_descendants_of_type($this, $type);
1727
-    }
1728
-
1729
-
1730
-    /**
1731
-     * @deprecated 4.6.0
1732
-     * @param string $type like one of the EEM_Line_Item::type_*
1733
-     * @return EE_Line_Item
1734
-     * @throws EE_Error
1735
-     * @throws InvalidArgumentException
1736
-     * @throws InvalidDataTypeException
1737
-     * @throws InvalidInterfaceException
1738
-     * @throws ReflectionException
1739
-     */
1740
-    public function get_nearest_descendant_of_type($type)
1741
-    {
1742
-        EE_Error::doing_it_wrong(
1743
-            'EE_Line_Item::get_nearest_descendant_of_type()',
1744
-            sprintf(
1745
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1746
-                'EEH_Line_Item::get_nearest_descendant_of_type()'
1747
-            ),
1748
-            '4.6.0'
1749
-        );
1750
-        return EEH_Line_Item::get_nearest_descendant_of_type($this, $type);
1751
-    }
17
+	/**
18
+	 * for children line items (currently not a normal relation)
19
+	 *
20
+	 * @type EE_Line_Item[]
21
+	 */
22
+	protected $_children = array();
23
+
24
+	/**
25
+	 * for the parent line item
26
+	 *
27
+	 * @var EE_Line_Item
28
+	 */
29
+	protected $_parent;
30
+
31
+
32
+	/**
33
+	 * @param array  $props_n_values          incoming values
34
+	 * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
35
+	 *                                        used.)
36
+	 * @param array  $date_formats            incoming date_formats in an array where the first value is the
37
+	 *                                        date_format and the second value is the time format
38
+	 * @return EE_Line_Item
39
+	 * @throws EE_Error
40
+	 * @throws InvalidArgumentException
41
+	 * @throws InvalidDataTypeException
42
+	 * @throws InvalidInterfaceException
43
+	 * @throws ReflectionException
44
+	 */
45
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
46
+	{
47
+		$has_object = parent::_check_for_object(
48
+			$props_n_values,
49
+			__CLASS__,
50
+			$timezone,
51
+			$date_formats
52
+		);
53
+		return $has_object
54
+			? $has_object
55
+			: new self($props_n_values, false, $timezone);
56
+	}
57
+
58
+
59
+	/**
60
+	 * @param array  $props_n_values  incoming values from the database
61
+	 * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
62
+	 *                                the website will be used.
63
+	 * @return EE_Line_Item
64
+	 * @throws EE_Error
65
+	 * @throws InvalidArgumentException
66
+	 * @throws InvalidDataTypeException
67
+	 * @throws InvalidInterfaceException
68
+	 * @throws ReflectionException
69
+	 */
70
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
71
+	{
72
+		return new self($props_n_values, true, $timezone);
73
+	}
74
+
75
+
76
+	/**
77
+	 * Adds some defaults if they're not specified
78
+	 *
79
+	 * @param array  $fieldValues
80
+	 * @param bool   $bydb
81
+	 * @param string $timezone
82
+	 * @throws EE_Error
83
+	 * @throws InvalidArgumentException
84
+	 * @throws InvalidDataTypeException
85
+	 * @throws InvalidInterfaceException
86
+	 * @throws ReflectionException
87
+	 */
88
+	protected function __construct($fieldValues = array(), $bydb = false, $timezone = '')
89
+	{
90
+		parent::__construct($fieldValues, $bydb, $timezone);
91
+		if (! $this->get('LIN_code')) {
92
+			$this->set_code($this->generate_code());
93
+		}
94
+	}
95
+
96
+
97
+	/**
98
+	 * Gets ID
99
+	 *
100
+	 * @return int
101
+	 * @throws EE_Error
102
+	 * @throws InvalidArgumentException
103
+	 * @throws InvalidDataTypeException
104
+	 * @throws InvalidInterfaceException
105
+	 * @throws ReflectionException
106
+	 */
107
+	public function ID()
108
+	{
109
+		return $this->get('LIN_ID');
110
+	}
111
+
112
+
113
+	/**
114
+	 * Gets TXN_ID
115
+	 *
116
+	 * @return int
117
+	 * @throws EE_Error
118
+	 * @throws InvalidArgumentException
119
+	 * @throws InvalidDataTypeException
120
+	 * @throws InvalidInterfaceException
121
+	 * @throws ReflectionException
122
+	 */
123
+	public function TXN_ID()
124
+	{
125
+		return $this->get('TXN_ID');
126
+	}
127
+
128
+
129
+	/**
130
+	 * Sets TXN_ID
131
+	 *
132
+	 * @param int $TXN_ID
133
+	 * @throws EE_Error
134
+	 * @throws InvalidArgumentException
135
+	 * @throws InvalidDataTypeException
136
+	 * @throws InvalidInterfaceException
137
+	 * @throws ReflectionException
138
+	 */
139
+	public function set_TXN_ID($TXN_ID)
140
+	{
141
+		$this->set('TXN_ID', $TXN_ID);
142
+	}
143
+
144
+
145
+	/**
146
+	 * Gets name
147
+	 *
148
+	 * @return string
149
+	 * @throws EE_Error
150
+	 * @throws InvalidArgumentException
151
+	 * @throws InvalidDataTypeException
152
+	 * @throws InvalidInterfaceException
153
+	 * @throws ReflectionException
154
+	 */
155
+	public function name()
156
+	{
157
+		$name = $this->get('LIN_name');
158
+		if (! $name) {
159
+			$name = ucwords(str_replace('-', ' ', $this->type()));
160
+		}
161
+		return $name;
162
+	}
163
+
164
+
165
+	/**
166
+	 * Sets name
167
+	 *
168
+	 * @param string $name
169
+	 * @throws EE_Error
170
+	 * @throws InvalidArgumentException
171
+	 * @throws InvalidDataTypeException
172
+	 * @throws InvalidInterfaceException
173
+	 * @throws ReflectionException
174
+	 */
175
+	public function set_name($name)
176
+	{
177
+		$this->set('LIN_name', $name);
178
+	}
179
+
180
+
181
+	/**
182
+	 * Gets desc
183
+	 *
184
+	 * @return string
185
+	 * @throws EE_Error
186
+	 * @throws InvalidArgumentException
187
+	 * @throws InvalidDataTypeException
188
+	 * @throws InvalidInterfaceException
189
+	 * @throws ReflectionException
190
+	 */
191
+	public function desc()
192
+	{
193
+		return $this->get('LIN_desc');
194
+	}
195
+
196
+
197
+	/**
198
+	 * Sets desc
199
+	 *
200
+	 * @param string $desc
201
+	 * @throws EE_Error
202
+	 * @throws InvalidArgumentException
203
+	 * @throws InvalidDataTypeException
204
+	 * @throws InvalidInterfaceException
205
+	 * @throws ReflectionException
206
+	 */
207
+	public function set_desc($desc)
208
+	{
209
+		$this->set('LIN_desc', $desc);
210
+	}
211
+
212
+
213
+	/**
214
+	 * Gets quantity
215
+	 *
216
+	 * @return int
217
+	 * @throws EE_Error
218
+	 * @throws InvalidArgumentException
219
+	 * @throws InvalidDataTypeException
220
+	 * @throws InvalidInterfaceException
221
+	 * @throws ReflectionException
222
+	 */
223
+	public function quantity()
224
+	{
225
+		return $this->get('LIN_quantity');
226
+	}
227
+
228
+
229
+	/**
230
+	 * Sets quantity
231
+	 *
232
+	 * @param int $quantity
233
+	 * @throws EE_Error
234
+	 * @throws InvalidArgumentException
235
+	 * @throws InvalidDataTypeException
236
+	 * @throws InvalidInterfaceException
237
+	 * @throws ReflectionException
238
+	 */
239
+	public function set_quantity($quantity)
240
+	{
241
+		$this->set('LIN_quantity', max($quantity, 0));
242
+	}
243
+
244
+
245
+	/**
246
+	 * Gets item_id
247
+	 *
248
+	 * @return string
249
+	 * @throws EE_Error
250
+	 * @throws InvalidArgumentException
251
+	 * @throws InvalidDataTypeException
252
+	 * @throws InvalidInterfaceException
253
+	 * @throws ReflectionException
254
+	 */
255
+	public function OBJ_ID()
256
+	{
257
+		return $this->get('OBJ_ID');
258
+	}
259
+
260
+
261
+	/**
262
+	 * Sets item_id
263
+	 *
264
+	 * @param string $item_id
265
+	 * @throws EE_Error
266
+	 * @throws InvalidArgumentException
267
+	 * @throws InvalidDataTypeException
268
+	 * @throws InvalidInterfaceException
269
+	 * @throws ReflectionException
270
+	 */
271
+	public function set_OBJ_ID($item_id)
272
+	{
273
+		$this->set('OBJ_ID', $item_id);
274
+	}
275
+
276
+
277
+	/**
278
+	 * Gets item_type
279
+	 *
280
+	 * @return string
281
+	 * @throws EE_Error
282
+	 * @throws InvalidArgumentException
283
+	 * @throws InvalidDataTypeException
284
+	 * @throws InvalidInterfaceException
285
+	 * @throws ReflectionException
286
+	 */
287
+	public function OBJ_type()
288
+	{
289
+		return $this->get('OBJ_type');
290
+	}
291
+
292
+
293
+	/**
294
+	 * Gets item_type
295
+	 *
296
+	 * @return string
297
+	 * @throws EE_Error
298
+	 * @throws InvalidArgumentException
299
+	 * @throws InvalidDataTypeException
300
+	 * @throws InvalidInterfaceException
301
+	 * @throws ReflectionException
302
+	 */
303
+	public function OBJ_type_i18n()
304
+	{
305
+		$obj_type = $this->OBJ_type();
306
+		switch ($obj_type) {
307
+			case EEM_Line_Item::OBJ_TYPE_EVENT:
308
+				$obj_type = esc_html__('Event', 'event_espresso');
309
+				break;
310
+			case EEM_Line_Item::OBJ_TYPE_PRICE:
311
+				$obj_type = esc_html__('Price', 'event_espresso');
312
+				break;
313
+			case EEM_Line_Item::OBJ_TYPE_PROMOTION:
314
+				$obj_type = esc_html__('Promotion', 'event_espresso');
315
+				break;
316
+			case EEM_Line_Item::OBJ_TYPE_TICKET:
317
+				$obj_type = esc_html__('Ticket', 'event_espresso');
318
+				break;
319
+			case EEM_Line_Item::OBJ_TYPE_TRANSACTION:
320
+				$obj_type = esc_html__('Transaction', 'event_espresso');
321
+				break;
322
+		}
323
+		return apply_filters('FHEE__EE_Line_Item__OBJ_type_i18n', $obj_type, $this);
324
+	}
325
+
326
+
327
+	/**
328
+	 * Sets item_type
329
+	 *
330
+	 * @param string $OBJ_type
331
+	 * @throws EE_Error
332
+	 * @throws InvalidArgumentException
333
+	 * @throws InvalidDataTypeException
334
+	 * @throws InvalidInterfaceException
335
+	 * @throws ReflectionException
336
+	 */
337
+	public function set_OBJ_type($OBJ_type)
338
+	{
339
+		$this->set('OBJ_type', $OBJ_type);
340
+	}
341
+
342
+
343
+	/**
344
+	 * Gets unit_price
345
+	 *
346
+	 * @return float
347
+	 * @throws EE_Error
348
+	 * @throws InvalidArgumentException
349
+	 * @throws InvalidDataTypeException
350
+	 * @throws InvalidInterfaceException
351
+	 * @throws ReflectionException
352
+	 */
353
+	public function unit_price()
354
+	{
355
+		return $this->get('LIN_unit_price');
356
+	}
357
+
358
+
359
+	/**
360
+	 * Sets unit_price
361
+	 *
362
+	 * @param float $unit_price
363
+	 * @throws EE_Error
364
+	 * @throws InvalidArgumentException
365
+	 * @throws InvalidDataTypeException
366
+	 * @throws InvalidInterfaceException
367
+	 * @throws ReflectionException
368
+	 */
369
+	public function set_unit_price($unit_price)
370
+	{
371
+		$this->set('LIN_unit_price', $unit_price);
372
+	}
373
+
374
+
375
+	/**
376
+	 * Checks if this item is a percentage modifier or not
377
+	 *
378
+	 * @return boolean
379
+	 * @throws EE_Error
380
+	 * @throws InvalidArgumentException
381
+	 * @throws InvalidDataTypeException
382
+	 * @throws InvalidInterfaceException
383
+	 * @throws ReflectionException
384
+	 */
385
+	public function is_percent()
386
+	{
387
+		if ($this->is_tax_sub_total()) {
388
+			// tax subtotals HAVE a percent on them, that percentage only applies
389
+			// to taxable items, so its' an exception. Treat it like a flat line item
390
+			return false;
391
+		}
392
+		$unit_price = abs($this->get('LIN_unit_price'));
393
+		$percent = abs($this->get('LIN_percent'));
394
+		if ($unit_price < .001 && $percent) {
395
+			return true;
396
+		}
397
+		if ($unit_price >= .001 && ! $percent) {
398
+			return false;
399
+		}
400
+		if ($unit_price >= .001 && $percent) {
401
+			throw new EE_Error(
402
+				sprintf(
403
+					esc_html__(
404
+						'A Line Item can not have a unit price of (%s) AND a percent (%s)!',
405
+						'event_espresso'
406
+					),
407
+					$unit_price,
408
+					$percent
409
+				)
410
+			);
411
+		}
412
+		// if they're both 0, assume its not a percent item
413
+		return false;
414
+	}
415
+
416
+
417
+	/**
418
+	 * Gets percent (between 100-.001)
419
+	 *
420
+	 * @return float
421
+	 * @throws EE_Error
422
+	 * @throws InvalidArgumentException
423
+	 * @throws InvalidDataTypeException
424
+	 * @throws InvalidInterfaceException
425
+	 * @throws ReflectionException
426
+	 */
427
+	public function percent()
428
+	{
429
+		return $this->get('LIN_percent');
430
+	}
431
+
432
+
433
+	/**
434
+	 * Sets percent (between 100-0.01)
435
+	 *
436
+	 * @param float $percent
437
+	 * @throws EE_Error
438
+	 * @throws InvalidArgumentException
439
+	 * @throws InvalidDataTypeException
440
+	 * @throws InvalidInterfaceException
441
+	 * @throws ReflectionException
442
+	 */
443
+	public function set_percent($percent)
444
+	{
445
+		$this->set('LIN_percent', $percent);
446
+	}
447
+
448
+
449
+	/**
450
+	 * Gets total
451
+	 *
452
+	 * @return float
453
+	 * @throws EE_Error
454
+	 * @throws InvalidArgumentException
455
+	 * @throws InvalidDataTypeException
456
+	 * @throws InvalidInterfaceException
457
+	 * @throws ReflectionException
458
+	 */
459
+	public function total()
460
+	{
461
+		return $this->get('LIN_total');
462
+	}
463
+
464
+
465
+	/**
466
+	 * Sets total
467
+	 *
468
+	 * @param float $total
469
+	 * @throws EE_Error
470
+	 * @throws InvalidArgumentException
471
+	 * @throws InvalidDataTypeException
472
+	 * @throws InvalidInterfaceException
473
+	 * @throws ReflectionException
474
+	 */
475
+	public function set_total($total)
476
+	{
477
+		$this->set('LIN_total', $total);
478
+	}
479
+
480
+
481
+	/**
482
+	 * Gets order
483
+	 *
484
+	 * @return int
485
+	 * @throws EE_Error
486
+	 * @throws InvalidArgumentException
487
+	 * @throws InvalidDataTypeException
488
+	 * @throws InvalidInterfaceException
489
+	 * @throws ReflectionException
490
+	 */
491
+	public function order()
492
+	{
493
+		return $this->get('LIN_order');
494
+	}
495
+
496
+
497
+	/**
498
+	 * Sets order
499
+	 *
500
+	 * @param int $order
501
+	 * @throws EE_Error
502
+	 * @throws InvalidArgumentException
503
+	 * @throws InvalidDataTypeException
504
+	 * @throws InvalidInterfaceException
505
+	 * @throws ReflectionException
506
+	 */
507
+	public function set_order($order)
508
+	{
509
+		$this->set('LIN_order', $order);
510
+	}
511
+
512
+
513
+	/**
514
+	 * Gets parent
515
+	 *
516
+	 * @return int
517
+	 * @throws EE_Error
518
+	 * @throws InvalidArgumentException
519
+	 * @throws InvalidDataTypeException
520
+	 * @throws InvalidInterfaceException
521
+	 * @throws ReflectionException
522
+	 */
523
+	public function parent_ID()
524
+	{
525
+		return $this->get('LIN_parent');
526
+	}
527
+
528
+
529
+	/**
530
+	 * Sets parent
531
+	 *
532
+	 * @param int $parent
533
+	 * @throws EE_Error
534
+	 * @throws InvalidArgumentException
535
+	 * @throws InvalidDataTypeException
536
+	 * @throws InvalidInterfaceException
537
+	 * @throws ReflectionException
538
+	 */
539
+	public function set_parent_ID($parent)
540
+	{
541
+		$this->set('LIN_parent', $parent);
542
+	}
543
+
544
+
545
+	/**
546
+	 * Gets type
547
+	 *
548
+	 * @return string
549
+	 * @throws EE_Error
550
+	 * @throws InvalidArgumentException
551
+	 * @throws InvalidDataTypeException
552
+	 * @throws InvalidInterfaceException
553
+	 * @throws ReflectionException
554
+	 */
555
+	public function type()
556
+	{
557
+		return $this->get('LIN_type');
558
+	}
559
+
560
+
561
+	/**
562
+	 * Sets type
563
+	 *
564
+	 * @param string $type
565
+	 * @throws EE_Error
566
+	 * @throws InvalidArgumentException
567
+	 * @throws InvalidDataTypeException
568
+	 * @throws InvalidInterfaceException
569
+	 * @throws ReflectionException
570
+	 */
571
+	public function set_type($type)
572
+	{
573
+		$this->set('LIN_type', $type);
574
+	}
575
+
576
+
577
+	/**
578
+	 * Gets the line item of which this item is a composite. Eg, if this is a subtotal, the parent might be a total\
579
+	 * If this line item is saved to the DB, fetches the parent from the DB. However, if this line item isn't in the DB
580
+	 * it uses its cached reference to its parent line item (which would have been set by `EE_Line_Item::set_parent()`
581
+	 * or indirectly by `EE_Line_item::add_child_line_item()`)
582
+	 *
583
+	 * @return EE_Base_Class|EE_Line_Item
584
+	 * @throws EE_Error
585
+	 * @throws InvalidArgumentException
586
+	 * @throws InvalidDataTypeException
587
+	 * @throws InvalidInterfaceException
588
+	 * @throws ReflectionException
589
+	 */
590
+	public function parent()
591
+	{
592
+		return $this->ID()
593
+			? $this->get_model()->get_one_by_ID($this->parent_ID())
594
+			: $this->_parent;
595
+	}
596
+
597
+
598
+	/**
599
+	 * Gets ALL the children of this line item (ie, all the parts that contribute towards this total).
600
+	 *
601
+	 * @return EE_Base_Class[]|EE_Line_Item[]
602
+	 * @throws EE_Error
603
+	 * @throws InvalidArgumentException
604
+	 * @throws InvalidDataTypeException
605
+	 * @throws InvalidInterfaceException
606
+	 * @throws ReflectionException
607
+	 */
608
+	public function children()
609
+	{
610
+		if ($this->ID()) {
611
+			return $this->get_model()->get_all(
612
+				array(
613
+					array('LIN_parent' => $this->ID()),
614
+					'order_by' => array('LIN_order' => 'ASC'),
615
+				)
616
+			);
617
+		}
618
+		if (! is_array($this->_children)) {
619
+			$this->_children = array();
620
+		}
621
+		return $this->_children;
622
+	}
623
+
624
+
625
+	/**
626
+	 * Gets code
627
+	 *
628
+	 * @return string
629
+	 * @throws EE_Error
630
+	 * @throws InvalidArgumentException
631
+	 * @throws InvalidDataTypeException
632
+	 * @throws InvalidInterfaceException
633
+	 * @throws ReflectionException
634
+	 */
635
+	public function code()
636
+	{
637
+		return $this->get('LIN_code');
638
+	}
639
+
640
+
641
+	/**
642
+	 * Sets code
643
+	 *
644
+	 * @param string $code
645
+	 * @throws EE_Error
646
+	 * @throws InvalidArgumentException
647
+	 * @throws InvalidDataTypeException
648
+	 * @throws InvalidInterfaceException
649
+	 * @throws ReflectionException
650
+	 */
651
+	public function set_code($code)
652
+	{
653
+		$this->set('LIN_code', $code);
654
+	}
655
+
656
+
657
+	/**
658
+	 * Gets is_taxable
659
+	 *
660
+	 * @return boolean
661
+	 * @throws EE_Error
662
+	 * @throws InvalidArgumentException
663
+	 * @throws InvalidDataTypeException
664
+	 * @throws InvalidInterfaceException
665
+	 * @throws ReflectionException
666
+	 */
667
+	public function is_taxable()
668
+	{
669
+		return $this->get('LIN_is_taxable');
670
+	}
671
+
672
+
673
+	/**
674
+	 * Sets is_taxable
675
+	 *
676
+	 * @param boolean $is_taxable
677
+	 * @throws EE_Error
678
+	 * @throws InvalidArgumentException
679
+	 * @throws InvalidDataTypeException
680
+	 * @throws InvalidInterfaceException
681
+	 * @throws ReflectionException
682
+	 */
683
+	public function set_is_taxable($is_taxable)
684
+	{
685
+		$this->set('LIN_is_taxable', $is_taxable);
686
+	}
687
+
688
+
689
+	/**
690
+	 * Gets the object that this model-joins-to.
691
+	 * returns one of the model objects that the field OBJ_ID can point to... see the 'OBJ_ID' field on
692
+	 * EEM_Promotion_Object
693
+	 *        Eg, if this line item join model object is for a ticket, this will return the EE_Ticket object
694
+	 *
695
+	 * @return EE_Base_Class | NULL
696
+	 * @throws EE_Error
697
+	 * @throws InvalidArgumentException
698
+	 * @throws InvalidDataTypeException
699
+	 * @throws InvalidInterfaceException
700
+	 * @throws ReflectionException
701
+	 */
702
+	public function get_object()
703
+	{
704
+		$model_name_of_related_obj = $this->OBJ_type();
705
+		return $this->get_model()->has_relation($model_name_of_related_obj)
706
+			? $this->get_first_related($model_name_of_related_obj)
707
+			: null;
708
+	}
709
+
710
+
711
+	/**
712
+	 * Like EE_Line_Item::get_object(), but can only ever actually return an EE_Ticket.
713
+	 * (IE, if this line item is for a price or something else, will return NULL)
714
+	 *
715
+	 * @param array $query_params
716
+	 * @return EE_Base_Class|EE_Ticket
717
+	 * @throws EE_Error
718
+	 * @throws InvalidArgumentException
719
+	 * @throws InvalidDataTypeException
720
+	 * @throws InvalidInterfaceException
721
+	 * @throws ReflectionException
722
+	 */
723
+	public function ticket($query_params = array())
724
+	{
725
+		// we're going to assume that when this method is called
726
+		// we always want to receive the attached ticket EVEN if that ticket is archived.
727
+		// This can be overridden via the incoming $query_params argument
728
+		$remove_defaults = array('default_where_conditions' => 'none');
729
+		$query_params = array_merge($remove_defaults, $query_params);
730
+		return $this->get_first_related(EEM_Line_Item::OBJ_TYPE_TICKET, $query_params);
731
+	}
732
+
733
+
734
+	/**
735
+	 * Gets the EE_Datetime that's related to the ticket, IF this is for a ticket
736
+	 *
737
+	 * @return EE_Datetime | NULL
738
+	 * @throws EE_Error
739
+	 * @throws InvalidArgumentException
740
+	 * @throws InvalidDataTypeException
741
+	 * @throws InvalidInterfaceException
742
+	 * @throws ReflectionException
743
+	 */
744
+	public function get_ticket_datetime()
745
+	{
746
+		if ($this->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
747
+			$ticket = $this->ticket();
748
+			if ($ticket instanceof EE_Ticket) {
749
+				$datetime = $ticket->first_datetime();
750
+				if ($datetime instanceof EE_Datetime) {
751
+					return $datetime;
752
+				}
753
+			}
754
+		}
755
+		return null;
756
+	}
757
+
758
+
759
+	/**
760
+	 * Gets the event's name that's related to the ticket, if this is for
761
+	 * a ticket
762
+	 *
763
+	 * @return string
764
+	 * @throws EE_Error
765
+	 * @throws InvalidArgumentException
766
+	 * @throws InvalidDataTypeException
767
+	 * @throws InvalidInterfaceException
768
+	 * @throws ReflectionException
769
+	 */
770
+	public function ticket_event_name()
771
+	{
772
+		$event_name = esc_html__('Unknown', 'event_espresso');
773
+		$event = $this->ticket_event();
774
+		if ($event instanceof EE_Event) {
775
+			$event_name = $event->name();
776
+		}
777
+		return $event_name;
778
+	}
779
+
780
+
781
+	/**
782
+	 * Gets the event that's related to the ticket, if this line item represents a ticket.
783
+	 *
784
+	 * @return EE_Event|null
785
+	 * @throws EE_Error
786
+	 * @throws InvalidArgumentException
787
+	 * @throws InvalidDataTypeException
788
+	 * @throws InvalidInterfaceException
789
+	 * @throws ReflectionException
790
+	 */
791
+	public function ticket_event()
792
+	{
793
+		$event = null;
794
+		$ticket = $this->ticket();
795
+		if ($ticket instanceof EE_Ticket) {
796
+			$datetime = $ticket->first_datetime();
797
+			if ($datetime instanceof EE_Datetime) {
798
+				$event = $datetime->event();
799
+			}
800
+		}
801
+		return $event;
802
+	}
803
+
804
+
805
+	/**
806
+	 * Gets the first datetime for this lien item, assuming it's for a ticket
807
+	 *
808
+	 * @param string $date_format
809
+	 * @param string $time_format
810
+	 * @return string
811
+	 * @throws EE_Error
812
+	 * @throws InvalidArgumentException
813
+	 * @throws InvalidDataTypeException
814
+	 * @throws InvalidInterfaceException
815
+	 * @throws ReflectionException
816
+	 */
817
+	public function ticket_datetime_start($date_format = '', $time_format = '')
818
+	{
819
+		$first_datetime_string = esc_html__('Unknown', 'event_espresso');
820
+		$datetime = $this->get_ticket_datetime();
821
+		if ($datetime) {
822
+			$first_datetime_string = $datetime->start_date_and_time($date_format, $time_format);
823
+		}
824
+		return $first_datetime_string;
825
+	}
826
+
827
+
828
+	/**
829
+	 * Adds the line item as a child to this line item. If there is another child line
830
+	 * item with the same LIN_code, it is overwritten by this new one
831
+	 *
832
+	 * @param EEI_Line_Item $line_item
833
+	 * @param bool          $set_order
834
+	 * @return bool success
835
+	 * @throws EE_Error
836
+	 * @throws InvalidArgumentException
837
+	 * @throws InvalidDataTypeException
838
+	 * @throws InvalidInterfaceException
839
+	 * @throws ReflectionException
840
+	 */
841
+	public function add_child_line_item(EEI_Line_Item $line_item, $set_order = true)
842
+	{
843
+		// should we calculate the LIN_order for this line item ?
844
+		if ($set_order || $line_item->order() === null) {
845
+			$line_item->set_order(count($this->children()));
846
+		}
847
+		if ($this->ID()) {
848
+			// check for any duplicate line items (with the same code), if so, this replaces it
849
+			$line_item_with_same_code = $this->get_child_line_item($line_item->code());
850
+			if ($line_item_with_same_code instanceof EE_Line_Item && $line_item_with_same_code !== $line_item) {
851
+				$this->delete_child_line_item($line_item_with_same_code->code());
852
+			}
853
+			$line_item->set_parent_ID($this->ID());
854
+			if ($this->TXN_ID()) {
855
+				$line_item->set_TXN_ID($this->TXN_ID());
856
+			}
857
+			return $line_item->save();
858
+		}
859
+		$this->_children[ $line_item->code() ] = $line_item;
860
+		if ($line_item->parent() !== $this) {
861
+			$line_item->set_parent($this);
862
+		}
863
+		return true;
864
+	}
865
+
866
+
867
+	/**
868
+	 * Similar to EE_Base_Class::_add_relation_to, except this isn't a normal relation.
869
+	 * If this line item is saved to the DB, this is just a wrapper for set_parent_ID() and save()
870
+	 * However, if this line item is NOT saved to the DB, this just caches the parent on
871
+	 * the EE_Line_Item::_parent property.
872
+	 *
873
+	 * @param EE_Line_Item $line_item
874
+	 * @throws EE_Error
875
+	 * @throws InvalidArgumentException
876
+	 * @throws InvalidDataTypeException
877
+	 * @throws InvalidInterfaceException
878
+	 * @throws ReflectionException
879
+	 */
880
+	public function set_parent($line_item)
881
+	{
882
+		if ($this->ID()) {
883
+			if (! $line_item->ID()) {
884
+				$line_item->save();
885
+			}
886
+			$this->set_parent_ID($line_item->ID());
887
+			$this->save();
888
+		} else {
889
+			$this->_parent = $line_item;
890
+			$this->set_parent_ID($line_item->ID());
891
+		}
892
+	}
893
+
894
+
895
+	/**
896
+	 * Gets the child line item as specified by its code. Because this returns an object (by reference)
897
+	 * you can modify this child line item and the parent (this object) can know about them
898
+	 * because it also has a reference to that line item
899
+	 *
900
+	 * @param string $code
901
+	 * @return EE_Base_Class|EE_Line_Item|EE_Soft_Delete_Base_Class|NULL
902
+	 * @throws EE_Error
903
+	 * @throws InvalidArgumentException
904
+	 * @throws InvalidDataTypeException
905
+	 * @throws InvalidInterfaceException
906
+	 * @throws ReflectionException
907
+	 */
908
+	public function get_child_line_item($code)
909
+	{
910
+		if ($this->ID()) {
911
+			return $this->get_model()->get_one(
912
+				array(array('LIN_parent' => $this->ID(), 'LIN_code' => $code))
913
+			);
914
+		}
915
+		return isset($this->_children[ $code ])
916
+			? $this->_children[ $code ]
917
+			: null;
918
+	}
919
+
920
+
921
+	/**
922
+	 * Returns how many items are deleted (or, if this item has not been saved ot the DB yet, just how many it HAD
923
+	 * cached on it)
924
+	 *
925
+	 * @return int
926
+	 * @throws EE_Error
927
+	 * @throws InvalidArgumentException
928
+	 * @throws InvalidDataTypeException
929
+	 * @throws InvalidInterfaceException
930
+	 * @throws ReflectionException
931
+	 */
932
+	public function delete_children_line_items()
933
+	{
934
+		if ($this->ID()) {
935
+			return $this->get_model()->delete(array(array('LIN_parent' => $this->ID())));
936
+		}
937
+		$count = count($this->_children);
938
+		$this->_children = array();
939
+		return $count;
940
+	}
941
+
942
+
943
+	/**
944
+	 * If this line item has been saved to the DB, deletes its child with LIN_code == $code. If this line
945
+	 * HAS NOT been saved to the DB, removes the child line item with index $code.
946
+	 * Also searches through the child's children for a matching line item. However, once a line item has been found
947
+	 * and deleted, stops searching (so if there are line items with duplicate codes, only the first one found will be
948
+	 * deleted)
949
+	 *
950
+	 * @param string $code
951
+	 * @param bool   $stop_search_once_found
952
+	 * @return int count of items deleted (or simply removed from the line item's cache, if not has not been saved to
953
+	 *             the DB yet)
954
+	 * @throws EE_Error
955
+	 * @throws InvalidArgumentException
956
+	 * @throws InvalidDataTypeException
957
+	 * @throws InvalidInterfaceException
958
+	 * @throws ReflectionException
959
+	 */
960
+	public function delete_child_line_item($code, $stop_search_once_found = true)
961
+	{
962
+		if ($this->ID()) {
963
+			$items_deleted = 0;
964
+			if ($this->code() === $code) {
965
+				$items_deleted += EEH_Line_Item::delete_all_child_items($this);
966
+				$items_deleted += (int) $this->delete();
967
+				if ($stop_search_once_found) {
968
+					return $items_deleted;
969
+				}
970
+			}
971
+			foreach ($this->children() as $child_line_item) {
972
+				$items_deleted += $child_line_item->delete_child_line_item($code, $stop_search_once_found);
973
+			}
974
+			return $items_deleted;
975
+		}
976
+		if (isset($this->_children[ $code ])) {
977
+			unset($this->_children[ $code ]);
978
+			return 1;
979
+		}
980
+		return 0;
981
+	}
982
+
983
+
984
+	/**
985
+	 * If this line item is in the database, is of the type subtotal, and
986
+	 * has no children, why do we have it? It should be deleted so this function
987
+	 * does that
988
+	 *
989
+	 * @return boolean
990
+	 * @throws EE_Error
991
+	 * @throws InvalidArgumentException
992
+	 * @throws InvalidDataTypeException
993
+	 * @throws InvalidInterfaceException
994
+	 * @throws ReflectionException
995
+	 */
996
+	public function delete_if_childless_subtotal()
997
+	{
998
+		if ($this->ID() && $this->type() === EEM_Line_Item::type_sub_total && ! $this->children()) {
999
+			return $this->delete();
1000
+		}
1001
+		return false;
1002
+	}
1003
+
1004
+
1005
+	/**
1006
+	 * Creates a code and returns a string. doesn't assign the code to this model object
1007
+	 *
1008
+	 * @return string
1009
+	 * @throws EE_Error
1010
+	 * @throws InvalidArgumentException
1011
+	 * @throws InvalidDataTypeException
1012
+	 * @throws InvalidInterfaceException
1013
+	 * @throws ReflectionException
1014
+	 */
1015
+	public function generate_code()
1016
+	{
1017
+		// each line item in the cart requires a unique identifier
1018
+		return md5($this->get('OBJ_type') . $this->get('OBJ_ID') . microtime());
1019
+	}
1020
+
1021
+
1022
+	/**
1023
+	 * @return bool
1024
+	 * @throws EE_Error
1025
+	 * @throws InvalidArgumentException
1026
+	 * @throws InvalidDataTypeException
1027
+	 * @throws InvalidInterfaceException
1028
+	 * @throws ReflectionException
1029
+	 */
1030
+	public function is_tax()
1031
+	{
1032
+		return $this->type() === EEM_Line_Item::type_tax;
1033
+	}
1034
+
1035
+
1036
+	/**
1037
+	 * @return bool
1038
+	 * @throws EE_Error
1039
+	 * @throws InvalidArgumentException
1040
+	 * @throws InvalidDataTypeException
1041
+	 * @throws InvalidInterfaceException
1042
+	 * @throws ReflectionException
1043
+	 */
1044
+	public function is_tax_sub_total()
1045
+	{
1046
+		return $this->type() === EEM_Line_Item::type_tax_sub_total;
1047
+	}
1048
+
1049
+
1050
+	/**
1051
+	 * @return bool
1052
+	 * @throws EE_Error
1053
+	 * @throws InvalidArgumentException
1054
+	 * @throws InvalidDataTypeException
1055
+	 * @throws InvalidInterfaceException
1056
+	 * @throws ReflectionException
1057
+	 */
1058
+	public function is_line_item()
1059
+	{
1060
+		return $this->type() === EEM_Line_Item::type_line_item;
1061
+	}
1062
+
1063
+
1064
+	/**
1065
+	 * @return bool
1066
+	 * @throws EE_Error
1067
+	 * @throws InvalidArgumentException
1068
+	 * @throws InvalidDataTypeException
1069
+	 * @throws InvalidInterfaceException
1070
+	 * @throws ReflectionException
1071
+	 */
1072
+	public function is_sub_line_item()
1073
+	{
1074
+		return $this->type() === EEM_Line_Item::type_sub_line_item;
1075
+	}
1076
+
1077
+
1078
+	/**
1079
+	 * @return bool
1080
+	 * @throws EE_Error
1081
+	 * @throws InvalidArgumentException
1082
+	 * @throws InvalidDataTypeException
1083
+	 * @throws InvalidInterfaceException
1084
+	 * @throws ReflectionException
1085
+	 */
1086
+	public function is_sub_total()
1087
+	{
1088
+		return $this->type() === EEM_Line_Item::type_sub_total;
1089
+	}
1090
+
1091
+
1092
+	/**
1093
+	 * Whether or not this line item is a cancellation line item
1094
+	 *
1095
+	 * @return boolean
1096
+	 * @throws EE_Error
1097
+	 * @throws InvalidArgumentException
1098
+	 * @throws InvalidDataTypeException
1099
+	 * @throws InvalidInterfaceException
1100
+	 * @throws ReflectionException
1101
+	 */
1102
+	public function is_cancellation()
1103
+	{
1104
+		return EEM_Line_Item::type_cancellation === $this->type();
1105
+	}
1106
+
1107
+
1108
+	/**
1109
+	 * @return bool
1110
+	 * @throws EE_Error
1111
+	 * @throws InvalidArgumentException
1112
+	 * @throws InvalidDataTypeException
1113
+	 * @throws InvalidInterfaceException
1114
+	 * @throws ReflectionException
1115
+	 */
1116
+	public function is_total()
1117
+	{
1118
+		return $this->type() === EEM_Line_Item::type_total;
1119
+	}
1120
+
1121
+
1122
+	/**
1123
+	 * @return bool
1124
+	 * @throws EE_Error
1125
+	 * @throws InvalidArgumentException
1126
+	 * @throws InvalidDataTypeException
1127
+	 * @throws InvalidInterfaceException
1128
+	 * @throws ReflectionException
1129
+	 */
1130
+	public function is_cancelled()
1131
+	{
1132
+		return $this->type() === EEM_Line_Item::type_cancellation;
1133
+	}
1134
+
1135
+
1136
+	/**
1137
+	 * @return string like '2, 004.00', formatted according to the localized currency
1138
+	 * @throws EE_Error
1139
+	 * @throws InvalidArgumentException
1140
+	 * @throws InvalidDataTypeException
1141
+	 * @throws InvalidInterfaceException
1142
+	 * @throws ReflectionException
1143
+	 */
1144
+	public function unit_price_no_code()
1145
+	{
1146
+		return $this->get_pretty('LIN_unit_price', 'no_currency_code');
1147
+	}
1148
+
1149
+
1150
+	/**
1151
+	 * @return string like '2, 004.00', formatted according to the localized currency
1152
+	 * @throws EE_Error
1153
+	 * @throws InvalidArgumentException
1154
+	 * @throws InvalidDataTypeException
1155
+	 * @throws InvalidInterfaceException
1156
+	 * @throws ReflectionException
1157
+	 */
1158
+	public function total_no_code()
1159
+	{
1160
+		return $this->get_pretty('LIN_total', 'no_currency_code');
1161
+	}
1162
+
1163
+
1164
+	/**
1165
+	 * Gets the final total on this item, taking taxes into account.
1166
+	 * Has the side-effect of setting the sub-total as it was just calculated.
1167
+	 * If this is used on a grand-total line item, also updates the transaction's
1168
+	 * TXN_total (provided this line item is allowed to persist, otherwise we don't
1169
+	 * want to change a persistable transaction with info from a non-persistent line item)
1170
+	 *
1171
+	 * @param bool $update_txn_status
1172
+	 * @return float
1173
+	 * @throws EE_Error
1174
+	 * @throws InvalidArgumentException
1175
+	 * @throws InvalidDataTypeException
1176
+	 * @throws InvalidInterfaceException
1177
+	 * @throws ReflectionException
1178
+	 * @throws RuntimeException
1179
+	 */
1180
+	public function recalculate_total_including_taxes($update_txn_status = false)
1181
+	{
1182
+		$pre_tax_total = $this->recalculate_pre_tax_total();
1183
+		$tax_total = $this->recalculate_taxes_and_tax_total();
1184
+		$total = $pre_tax_total + $tax_total;
1185
+		// no negative totals plz
1186
+		$total = max($total, 0);
1187
+		$this->set_total($total);
1188
+		// only update the related transaction's total
1189
+		// if we intend to save this line item and its a grand total
1190
+		if ($this->allow_persist() && $this->type() === EEM_Line_Item::type_total
1191
+			&& $this->transaction()
1192
+			   instanceof
1193
+			   EE_Transaction
1194
+		) {
1195
+			$this->transaction()->set_total($total);
1196
+			if ($update_txn_status) {
1197
+				// don't save the TXN because that will be done below
1198
+				// and the following method only saves if the status changes
1199
+				$this->transaction()->update_status_based_on_total_paid(false);
1200
+			}
1201
+			if ($this->transaction()->ID()) {
1202
+				$this->transaction()->save();
1203
+			}
1204
+		}
1205
+		$this->maybe_save();
1206
+		return $total;
1207
+	}
1208
+
1209
+
1210
+	/**
1211
+	 * Recursively goes through all the children and recalculates sub-totals EXCEPT for
1212
+	 * tax-sub-totals (they're a an odd beast). Updates the 'total' on each line item according to either its
1213
+	 * unit price * quantity or the total of all its children EXCEPT when we're only calculating the taxable total and
1214
+	 * when this is called on the grand total
1215
+	 *
1216
+	 * @return float
1217
+	 * @throws EE_Error
1218
+	 * @throws InvalidArgumentException
1219
+	 * @throws InvalidDataTypeException
1220
+	 * @throws InvalidInterfaceException
1221
+	 * @throws ReflectionException
1222
+	 */
1223
+	public function recalculate_pre_tax_total()
1224
+	{
1225
+		$total = 0;
1226
+		$my_children = $this->children();
1227
+		$has_children = ! empty($my_children);
1228
+		if ($has_children && $this->is_line_item()) {
1229
+			$total = $this->_recalculate_pretax_total_for_line_item($total, $my_children);
1230
+		} elseif (! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) {
1231
+			$total = $this->unit_price() * $this->quantity();
1232
+		} elseif ($this->is_sub_total() || $this->is_total()) {
1233
+			$total = $this->_recalculate_pretax_total_for_subtotal($total, $my_children);
1234
+		} elseif ($this->is_tax_sub_total() || $this->is_tax() || $this->is_cancelled()) {
1235
+			// completely ignore tax totals, tax sub-totals, and cancelled line items, when calculating the pre-tax-total
1236
+			return 0;
1237
+		}
1238
+		// ensure all non-line items and non-sub-line-items have a quantity of 1 (except for Events)
1239
+		if (! $this->is_line_item() && ! $this->is_sub_line_item() && ! $this->is_cancellation()
1240
+		) {
1241
+			if ($this->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_EVENT) {
1242
+				$this->set_quantity(1);
1243
+			}
1244
+			if (! $this->is_percent()) {
1245
+				$this->set_unit_price($total);
1246
+			}
1247
+		}
1248
+		// we don't want to bother saving grand totals, because that needs to factor in taxes anyways
1249
+		// so it ought to be
1250
+		if (! $this->is_total()) {
1251
+			$this->set_total($total);
1252
+			// if not a percent line item, make sure we keep the unit price in sync
1253
+			if ($has_children
1254
+				&& $this->is_line_item()
1255
+				&& ! $this->is_percent()
1256
+			) {
1257
+				if ($this->quantity() === 0) {
1258
+					$new_unit_price = 0;
1259
+				} else {
1260
+					$new_unit_price = $this->total() / $this->quantity();
1261
+				}
1262
+				$this->set_unit_price($new_unit_price);
1263
+			}
1264
+			$this->maybe_save();
1265
+		}
1266
+		return $total;
1267
+	}
1268
+
1269
+
1270
+	/**
1271
+	 * Calculates the pretax total when this line item is a subtotal or total line item.
1272
+	 * Basically does a sum-then-round approach (ie, any percent line item that are children
1273
+	 * will calculate their total based on the un-rounded total we're working with so far, and
1274
+	 * THEN round the result; instead of rounding as we go like with sub-line-items)
1275
+	 *
1276
+	 * @param float          $calculated_total_so_far
1277
+	 * @param EE_Line_Item[] $my_children
1278
+	 * @return float
1279
+	 * @throws EE_Error
1280
+	 * @throws InvalidArgumentException
1281
+	 * @throws InvalidDataTypeException
1282
+	 * @throws InvalidInterfaceException
1283
+	 * @throws ReflectionException
1284
+	 */
1285
+	protected function _recalculate_pretax_total_for_subtotal($calculated_total_so_far, $my_children = null)
1286
+	{
1287
+		if ($my_children === null) {
1288
+			$my_children = $this->children();
1289
+		}
1290
+		$subtotal_quantity = 0;
1291
+		// get the total of all its children
1292
+		foreach ($my_children as $child_line_item) {
1293
+			if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) {
1294
+				// percentage line items are based on total so far
1295
+				if ($child_line_item->is_percent()) {
1296
+					// round as we go so that the line items add up ok
1297
+					$percent_total = round(
1298
+						$calculated_total_so_far * $child_line_item->percent() / 100,
1299
+						EE_Registry::instance()->CFG->currency->dec_plc
1300
+					);
1301
+					$child_line_item->set_total($percent_total);
1302
+					// so far all percent line items should have a quantity of 1
1303
+					// (ie, no double percent discounts. Although that might be requested someday)
1304
+					$child_line_item->set_quantity(1);
1305
+					$child_line_item->maybe_save();
1306
+					$calculated_total_so_far += $percent_total;
1307
+				} else {
1308
+					// verify flat sub-line-item quantities match their parent
1309
+					if ($child_line_item->is_sub_line_item()) {
1310
+						$child_line_item->set_quantity($this->quantity());
1311
+					}
1312
+					$calculated_total_so_far += $child_line_item->recalculate_pre_tax_total();
1313
+					$subtotal_quantity += $child_line_item->quantity();
1314
+				}
1315
+			}
1316
+		}
1317
+		if ($this->is_sub_total()) {
1318
+			// no negative totals plz
1319
+			$calculated_total_so_far = max($calculated_total_so_far, 0);
1320
+			$subtotal_quantity = $subtotal_quantity > 0 ? 1 : 0;
1321
+			$this->set_quantity($subtotal_quantity);
1322
+			$this->maybe_save();
1323
+		}
1324
+		return $calculated_total_so_far;
1325
+	}
1326
+
1327
+
1328
+	/**
1329
+	 * Calculates the pretax total for a normal line item, in a round-then-sum approach
1330
+	 * (where each sub-line-item is applied to the base price for the line item
1331
+	 * and the result is immediately rounded, rather than summing all the sub-line-items
1332
+	 * then rounding, like we do when recalculating pretax totals on totals and subtotals).
1333
+	 *
1334
+	 * @param float          $calculated_total_so_far
1335
+	 * @param EE_Line_Item[] $my_children
1336
+	 * @return float
1337
+	 * @throws EE_Error
1338
+	 * @throws InvalidArgumentException
1339
+	 * @throws InvalidDataTypeException
1340
+	 * @throws InvalidInterfaceException
1341
+	 * @throws ReflectionException
1342
+	 */
1343
+	protected function _recalculate_pretax_total_for_line_item($calculated_total_so_far, $my_children = null)
1344
+	{
1345
+		if ($my_children === null) {
1346
+			$my_children = $this->children();
1347
+		}
1348
+		// we need to keep track of the running total for a single item,
1349
+		// because we need to round as we go
1350
+		$unit_price_for_total = 0;
1351
+		$quantity_for_total = 1;
1352
+		// get the total of all its children
1353
+		foreach ($my_children as $child_line_item) {
1354
+			if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) {
1355
+				if ($child_line_item->is_percent()) {
1356
+					// it should be the unit-price-so-far multiplied by teh percent multiplied by the quantity
1357
+					// not total multiplied by percent, because that ignores rounding along-the-way
1358
+					$percent_unit_price = round(
1359
+						$unit_price_for_total * $child_line_item->percent() / 100,
1360
+						EE_Registry::instance()->CFG->currency->dec_plc
1361
+					);
1362
+					$percent_total = $percent_unit_price * $quantity_for_total;
1363
+					$child_line_item->set_total($percent_total);
1364
+					// so far all percent line items should have a quantity of 1
1365
+					// (ie, no double percent discounts. Although that might be requested someday)
1366
+					$child_line_item->set_quantity(1);
1367
+					$child_line_item->maybe_save();
1368
+					$calculated_total_so_far += $percent_total;
1369
+					$unit_price_for_total += $percent_unit_price;
1370
+				} else {
1371
+					// verify flat sub-line-item quantities match their parent
1372
+					if ($child_line_item->is_sub_line_item()) {
1373
+						$child_line_item->set_quantity($this->quantity());
1374
+					}
1375
+					$quantity_for_total = $child_line_item->quantity();
1376
+					$calculated_total_so_far += $child_line_item->recalculate_pre_tax_total();
1377
+					$unit_price_for_total += $child_line_item->unit_price();
1378
+				}
1379
+			}
1380
+		}
1381
+		return $calculated_total_so_far;
1382
+	}
1383
+
1384
+
1385
+	/**
1386
+	 * Recalculates the total on each individual tax (based on a recalculation of the pre-tax total), sets
1387
+	 * the totals on each tax calculated, and returns the final tax total. Re-saves tax line items
1388
+	 * and tax sub-total if already in the DB
1389
+	 *
1390
+	 * @return float
1391
+	 * @throws EE_Error
1392
+	 * @throws InvalidArgumentException
1393
+	 * @throws InvalidDataTypeException
1394
+	 * @throws InvalidInterfaceException
1395
+	 * @throws ReflectionException
1396
+	 */
1397
+	public function recalculate_taxes_and_tax_total()
1398
+	{
1399
+		// get all taxes
1400
+		$taxes = $this->tax_descendants();
1401
+		// calculate the pretax total
1402
+		$taxable_total = $this->taxable_total();
1403
+		$tax_total = 0;
1404
+		foreach ($taxes as $tax) {
1405
+			$total_on_this_tax = $taxable_total * $tax->percent() / 100;
1406
+			// remember the total on this line item
1407
+			$tax->set_total($total_on_this_tax);
1408
+			$tax->maybe_save();
1409
+			$tax_total += $tax->total();
1410
+		}
1411
+		$this->_recalculate_tax_sub_total();
1412
+		return $tax_total;
1413
+	}
1414
+
1415
+
1416
+	/**
1417
+	 * Simply forces all the tax-sub-totals to recalculate. Assumes the taxes have been calculated
1418
+	 *
1419
+	 * @return void
1420
+	 * @throws EE_Error
1421
+	 * @throws InvalidArgumentException
1422
+	 * @throws InvalidDataTypeException
1423
+	 * @throws InvalidInterfaceException
1424
+	 * @throws ReflectionException
1425
+	 */
1426
+	private function _recalculate_tax_sub_total()
1427
+	{
1428
+		if ($this->is_tax_sub_total()) {
1429
+			$total = 0;
1430
+			$total_percent = 0;
1431
+			// simply loop through all its children (which should be taxes) and sum their total
1432
+			foreach ($this->children() as $child_tax) {
1433
+				if ($child_tax instanceof EE_Line_Item) {
1434
+					$total += $child_tax->total();
1435
+					$total_percent += $child_tax->percent();
1436
+				}
1437
+			}
1438
+			$this->set_total($total);
1439
+			$this->set_percent($total_percent);
1440
+			$this->maybe_save();
1441
+		} elseif ($this->is_total()) {
1442
+			foreach ($this->children() as $maybe_tax_subtotal) {
1443
+				if ($maybe_tax_subtotal instanceof EE_Line_Item) {
1444
+					$maybe_tax_subtotal->_recalculate_tax_sub_total();
1445
+				}
1446
+			}
1447
+		}
1448
+	}
1449
+
1450
+
1451
+	/**
1452
+	 * Gets the total tax on this line item. Assumes taxes have already been calculated using
1453
+	 * recalculate_taxes_and_total
1454
+	 *
1455
+	 * @return float
1456
+	 * @throws EE_Error
1457
+	 * @throws InvalidArgumentException
1458
+	 * @throws InvalidDataTypeException
1459
+	 * @throws InvalidInterfaceException
1460
+	 * @throws ReflectionException
1461
+	 */
1462
+	public function get_total_tax()
1463
+	{
1464
+		$this->_recalculate_tax_sub_total();
1465
+		$total = 0;
1466
+		foreach ($this->tax_descendants() as $tax_line_item) {
1467
+			if ($tax_line_item instanceof EE_Line_Item) {
1468
+				$total += $tax_line_item->total();
1469
+			}
1470
+		}
1471
+		return $total;
1472
+	}
1473
+
1474
+
1475
+	/**
1476
+	 * Gets the total for all the items purchased only
1477
+	 *
1478
+	 * @return float
1479
+	 * @throws EE_Error
1480
+	 * @throws InvalidArgumentException
1481
+	 * @throws InvalidDataTypeException
1482
+	 * @throws InvalidInterfaceException
1483
+	 * @throws ReflectionException
1484
+	 */
1485
+	public function get_items_total()
1486
+	{
1487
+		// by default, let's make sure we're consistent with the existing line item
1488
+		if ($this->is_total()) {
1489
+			$pretax_subtotal_li = EEH_Line_Item::get_pre_tax_subtotal($this);
1490
+			if ($pretax_subtotal_li instanceof EE_Line_Item) {
1491
+				return $pretax_subtotal_li->total();
1492
+			}
1493
+		}
1494
+		$total = 0;
1495
+		foreach ($this->get_items() as $item) {
1496
+			if ($item instanceof EE_Line_Item) {
1497
+				$total += $item->total();
1498
+			}
1499
+		}
1500
+		return $total;
1501
+	}
1502
+
1503
+
1504
+	/**
1505
+	 * Gets all the descendants (ie, children or children of children etc) that
1506
+	 * are of the type 'tax'
1507
+	 *
1508
+	 * @return EE_Line_Item[]
1509
+	 * @throws EE_Error
1510
+	 */
1511
+	public function tax_descendants()
1512
+	{
1513
+		return EEH_Line_Item::get_tax_descendants($this);
1514
+	}
1515
+
1516
+
1517
+	/**
1518
+	 * Gets all the real items purchased which are children of this item
1519
+	 *
1520
+	 * @return EE_Line_Item[]
1521
+	 * @throws EE_Error
1522
+	 */
1523
+	public function get_items()
1524
+	{
1525
+		return EEH_Line_Item::get_line_item_descendants($this);
1526
+	}
1527
+
1528
+
1529
+	/**
1530
+	 * Returns the amount taxable among this line item's children (or if it has no children,
1531
+	 * how much of it is taxable). Does not recalculate totals or subtotals.
1532
+	 * If the taxable total is negative, (eg, if none of the tickets were taxable,
1533
+	 * but there is a "Taxable" discount), returns 0.
1534
+	 *
1535
+	 * @return float
1536
+	 * @throws EE_Error
1537
+	 * @throws InvalidArgumentException
1538
+	 * @throws InvalidDataTypeException
1539
+	 * @throws InvalidInterfaceException
1540
+	 * @throws ReflectionException
1541
+	 */
1542
+	public function taxable_total()
1543
+	{
1544
+		$total = 0;
1545
+		if ($this->children()) {
1546
+			foreach ($this->children() as $child_line_item) {
1547
+				if ($child_line_item->type() === EEM_Line_Item::type_line_item && $child_line_item->is_taxable()) {
1548
+					// if it's a percent item, only take into account the percent
1549
+					// that's taxable too (the taxable total so far)
1550
+					if ($child_line_item->is_percent()) {
1551
+						$total += ($total * $child_line_item->percent() / 100);
1552
+					} else {
1553
+						$total += $child_line_item->total();
1554
+					}
1555
+				} elseif ($child_line_item->type() === EEM_Line_Item::type_sub_total) {
1556
+					$total += $child_line_item->taxable_total();
1557
+				}
1558
+			}
1559
+		}
1560
+		return max($total, 0);
1561
+	}
1562
+
1563
+
1564
+	/**
1565
+	 * Gets the transaction for this line item
1566
+	 *
1567
+	 * @return EE_Base_Class|EE_Transaction
1568
+	 * @throws EE_Error
1569
+	 * @throws InvalidArgumentException
1570
+	 * @throws InvalidDataTypeException
1571
+	 * @throws InvalidInterfaceException
1572
+	 * @throws ReflectionException
1573
+	 */
1574
+	public function transaction()
1575
+	{
1576
+		return $this->get_first_related(EEM_Line_Item::OBJ_TYPE_TRANSACTION);
1577
+	}
1578
+
1579
+
1580
+	/**
1581
+	 * Saves this line item to the DB, and recursively saves its descendants.
1582
+	 * Because there currently is no proper parent-child relation on the model,
1583
+	 * save_this_and_cached() will NOT save the descendants.
1584
+	 * Also sets the transaction on this line item and all its descendants before saving
1585
+	 *
1586
+	 * @param int $txn_id if none is provided, assumes $this->TXN_ID()
1587
+	 * @return int count of items saved
1588
+	 * @throws EE_Error
1589
+	 * @throws InvalidArgumentException
1590
+	 * @throws InvalidDataTypeException
1591
+	 * @throws InvalidInterfaceException
1592
+	 * @throws ReflectionException
1593
+	 */
1594
+	public function save_this_and_descendants_to_txn($txn_id = null)
1595
+	{
1596
+		$count = 0;
1597
+		if (! $txn_id) {
1598
+			$txn_id = $this->TXN_ID();
1599
+		}
1600
+		$this->set_TXN_ID($txn_id);
1601
+		$children = $this->children();
1602
+		$count += $this->save()
1603
+			? 1
1604
+			: 0;
1605
+		foreach ($children as $child_line_item) {
1606
+			if ($child_line_item instanceof EE_Line_Item) {
1607
+				$child_line_item->set_parent_ID($this->ID());
1608
+				$count += $child_line_item->save_this_and_descendants_to_txn($txn_id);
1609
+			}
1610
+		}
1611
+		return $count;
1612
+	}
1613
+
1614
+
1615
+	/**
1616
+	 * Saves this line item to the DB, and recursively saves its descendants.
1617
+	 *
1618
+	 * @return int count of items saved
1619
+	 * @throws EE_Error
1620
+	 * @throws InvalidArgumentException
1621
+	 * @throws InvalidDataTypeException
1622
+	 * @throws InvalidInterfaceException
1623
+	 * @throws ReflectionException
1624
+	 */
1625
+	public function save_this_and_descendants()
1626
+	{
1627
+		$count = 0;
1628
+		$children = $this->children();
1629
+		$count += $this->save()
1630
+			? 1
1631
+			: 0;
1632
+		foreach ($children as $child_line_item) {
1633
+			if ($child_line_item instanceof EE_Line_Item) {
1634
+				$child_line_item->set_parent_ID($this->ID());
1635
+				$count += $child_line_item->save_this_and_descendants();
1636
+			}
1637
+		}
1638
+		return $count;
1639
+	}
1640
+
1641
+
1642
+	/**
1643
+	 * returns the cancellation line item if this item was cancelled
1644
+	 *
1645
+	 * @return EE_Line_Item[]
1646
+	 * @throws InvalidArgumentException
1647
+	 * @throws InvalidInterfaceException
1648
+	 * @throws InvalidDataTypeException
1649
+	 * @throws ReflectionException
1650
+	 * @throws EE_Error
1651
+	 */
1652
+	public function get_cancellations()
1653
+	{
1654
+		EE_Registry::instance()->load_helper('Line_Item');
1655
+		return EEH_Line_Item::get_descendants_of_type($this, EEM_Line_Item::type_cancellation);
1656
+	}
1657
+
1658
+
1659
+	/**
1660
+	 * If this item has an ID, then this saves it again to update the db
1661
+	 *
1662
+	 * @return int count of items saved
1663
+	 * @throws EE_Error
1664
+	 * @throws InvalidArgumentException
1665
+	 * @throws InvalidDataTypeException
1666
+	 * @throws InvalidInterfaceException
1667
+	 * @throws ReflectionException
1668
+	 */
1669
+	public function maybe_save()
1670
+	{
1671
+		if ($this->ID()) {
1672
+			return $this->save();
1673
+		}
1674
+		return false;
1675
+	}
1676
+
1677
+
1678
+	/**
1679
+	 * clears the cached children and parent from the line item
1680
+	 *
1681
+	 * @return void
1682
+	 */
1683
+	public function clear_related_line_item_cache()
1684
+	{
1685
+		$this->_children = array();
1686
+		$this->_parent = null;
1687
+	}
1688
+
1689
+
1690
+	/**
1691
+	 * @param bool $raw
1692
+	 * @return int
1693
+	 * @throws EE_Error
1694
+	 * @throws InvalidArgumentException
1695
+	 * @throws InvalidDataTypeException
1696
+	 * @throws InvalidInterfaceException
1697
+	 * @throws ReflectionException
1698
+	 */
1699
+	public function timestamp($raw = false)
1700
+	{
1701
+		return $raw
1702
+			? $this->get_raw('LIN_timestamp')
1703
+			: $this->get('LIN_timestamp');
1704
+	}
1705
+
1706
+
1707
+
1708
+
1709
+	/************************* DEPRECATED *************************/
1710
+	/**
1711
+	 * @deprecated 4.6.0
1712
+	 * @param string $type one of the constants on EEM_Line_Item
1713
+	 * @return EE_Line_Item[]
1714
+	 * @throws EE_Error
1715
+	 */
1716
+	protected function _get_descendants_of_type($type)
1717
+	{
1718
+		EE_Error::doing_it_wrong(
1719
+			'EE_Line_Item::_get_descendants_of_type()',
1720
+			sprintf(
1721
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1722
+				'EEH_Line_Item::get_descendants_of_type()'
1723
+			),
1724
+			'4.6.0'
1725
+		);
1726
+		return EEH_Line_Item::get_descendants_of_type($this, $type);
1727
+	}
1728
+
1729
+
1730
+	/**
1731
+	 * @deprecated 4.6.0
1732
+	 * @param string $type like one of the EEM_Line_Item::type_*
1733
+	 * @return EE_Line_Item
1734
+	 * @throws EE_Error
1735
+	 * @throws InvalidArgumentException
1736
+	 * @throws InvalidDataTypeException
1737
+	 * @throws InvalidInterfaceException
1738
+	 * @throws ReflectionException
1739
+	 */
1740
+	public function get_nearest_descendant_of_type($type)
1741
+	{
1742
+		EE_Error::doing_it_wrong(
1743
+			'EE_Line_Item::get_nearest_descendant_of_type()',
1744
+			sprintf(
1745
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1746
+				'EEH_Line_Item::get_nearest_descendant_of_type()'
1747
+			),
1748
+			'4.6.0'
1749
+		);
1750
+		return EEH_Line_Item::get_nearest_descendant_of_type($this, $type);
1751
+	}
1752 1752
 }
Please login to merge, or discard this patch.
core/helpers/EEH_Line_Item.helper.php 3 patches
Doc Comments   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1027,7 +1027,7 @@  discard block
 block discarded – undo
1027 1027
      * Deletes ALL children of the passed line item
1028 1028
      *
1029 1029
      * @param EE_Line_Item $parent_line_item
1030
-     * @return bool
1030
+     * @return integer
1031 1031
      * @throws EE_Error
1032 1032
      * @throws InvalidArgumentException
1033 1033
      * @throws InvalidDataTypeException
@@ -1333,6 +1333,7 @@  discard block
 block discarded – undo
1333 1333
      * @param string        $line_item_type   one of the EEM_Line_Item constants
1334 1334
      * @param string | NULL $obj_type         object model class name (minus prefix) or NULL to ignore object type when
1335 1335
      *                                        searching
1336
+     * @param string $obj_type
1336 1337
      * @return EE_Line_Item[]
1337 1338
      * @throws EE_Error
1338 1339
      */
Please login to merge, or discard this patch.
Spacing   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
      */
139 139
     public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140 140
     {
141
-        if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
141
+        if ( ! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142 142
             throw new EE_Error(
143 143
                 sprintf(
144 144
                     esc_html__(
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
         // either increment the qty for an existing ticket
154 154
         $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155 155
         // or add a new one
156
-        if (! $line_item instanceof EE_Line_Item) {
156
+        if ( ! $line_item instanceof EE_Line_Item) {
157 157
             $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158 158
         }
159 159
         $total_line_item->recalculate_total_including_taxes();
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
      */
215 215
     public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216 216
     {
217
-        if (! $line_item->is_percent()) {
217
+        if ( ! $line_item->is_percent()) {
218 218
             $qty += $line_item->quantity();
219 219
             $line_item->set_quantity($qty);
220 220
             $line_item->set_total($line_item->unit_price() * $qty);
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
      */
244 244
     public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245 245
     {
246
-        if (! $line_item->is_percent()) {
246
+        if ( ! $line_item->is_percent()) {
247 247
             $qty = $line_item->quantity() - $qty;
248 248
             $qty = max($qty, 0);
249 249
             $line_item->set_quantity($qty);
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
      */
273 273
     public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274 274
     {
275
-        if (! $line_item->is_percent()) {
275
+        if ( ! $line_item->is_percent()) {
276 276
             $line_item->set_quantity($new_quantity);
277 277
             $line_item->set_total($line_item->unit_price() * $new_quantity);
278 278
             $line_item->save();
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
         // add $ticket to cart
313 313
         $line_item = EE_Line_Item::new_instance(array(
314 314
             'LIN_name'       => $ticket->name(),
315
-            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
315
+            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description().' '.$event : $event,
316 316
             'LIN_unit_price' => $ticket->price(),
317 317
             'LIN_quantity'   => $qty,
318 318
             'LIN_is_taxable' => $ticket->taxable(),
@@ -462,7 +462,7 @@  discard block
 block discarded – undo
462 462
                         'event_espresso'
463 463
                     ),
464 464
                     $ticket_line_item->name(),
465
-                    current_time(get_option('date_format') . ' ' . get_option('time_format'))
465
+                    current_time(get_option('date_format').' '.get_option('time_format'))
466 466
                 ),
467 467
                 'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468 468
                 'LIN_quantity'   => $qty,
@@ -524,7 +524,7 @@  discard block
 block discarded – undo
524 524
         );
525 525
         $cancellation_line_item = reset($cancellation_line_item);
526 526
         // verify that this ticket was indeed previously cancelled
527
-        if (! $cancellation_line_item instanceof EE_Line_Item) {
527
+        if ( ! $cancellation_line_item instanceof EE_Line_Item) {
528 528
             return false;
529 529
         }
530 530
         if ($cancellation_line_item->quantity() > $qty) {
@@ -729,7 +729,7 @@  discard block
 block discarded – undo
729 729
             'LIN_code'  => 'taxes',
730 730
             'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731 731
             'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
-            'LIN_order' => 1000,// this should always come last
732
+            'LIN_order' => 1000, // this should always come last
733 733
         ));
734 734
         $tax_line_item = apply_filters(
735 735
             'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
@@ -785,7 +785,7 @@  discard block
 block discarded – undo
785 785
      */
786 786
     public static function get_event_code($event)
787 787
     {
788
-        return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
788
+        return 'event-'.($event instanceof EE_Event ? $event->ID() : '0');
789 789
     }
790 790
 
791 791
 
@@ -834,7 +834,7 @@  discard block
 block discarded – undo
834 834
     public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835 835
     {
836 836
         $first_datetime = $ticket->first_datetime();
837
-        if (! $first_datetime instanceof EE_Datetime) {
837
+        if ( ! $first_datetime instanceof EE_Datetime) {
838 838
             throw new EE_Error(
839 839
                 sprintf(
840 840
                     __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
@@ -843,7 +843,7 @@  discard block
 block discarded – undo
843 843
             );
844 844
         }
845 845
         $event = $first_datetime->event();
846
-        if (! $event instanceof EE_Event) {
846
+        if ( ! $event instanceof EE_Event) {
847 847
             throw new EE_Error(
848 848
                 sprintf(
849 849
                     esc_html__(
@@ -855,7 +855,7 @@  discard block
 block discarded – undo
855 855
             );
856 856
         }
857 857
         $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
-        if (! $events_sub_total instanceof EE_Line_Item) {
858
+        if ( ! $events_sub_total instanceof EE_Line_Item) {
859 859
             throw new EE_Error(
860 860
                 sprintf(
861 861
                     esc_html__(
@@ -891,7 +891,7 @@  discard block
 block discarded – undo
891 891
         $found = false;
892 892
         foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893 893
             // default event subtotal, we should only ever find this the first time this method is called
894
-            if (! $event_line_item->OBJ_ID()) {
894
+            if ( ! $event_line_item->OBJ_ID()) {
895 895
                 // let's use this! but first... set the event details
896 896
                 EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897 897
                 $found = true;
@@ -903,7 +903,7 @@  discard block
 block discarded – undo
903 903
                 break;
904 904
             }
905 905
         }
906
-        if (! $found) {
906
+        if ( ! $found) {
907 907
             // there is no event sub-total yet, so add it
908 908
             $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909 909
             // create a new "event" subtotal below that
@@ -1018,7 +1018,7 @@  discard block
 block discarded – undo
1018 1018
     public static function ensure_taxes_applied($total_line_item)
1019 1019
     {
1020 1020
         $taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1021
-        if (! $taxes_subtotal->children()) {
1021
+        if ( ! $taxes_subtotal->children()) {
1022 1022
             self::apply_taxes($total_line_item);
1023 1023
         }
1024 1024
         return $taxes_subtotal->total();
@@ -1085,7 +1085,7 @@  discard block
 block discarded – undo
1085 1085
         do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1086 1086
 
1087 1087
         // check if only a single line_item_id was passed
1088
-        if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1088
+        if ( ! empty($line_item_codes) && ! is_array($line_item_codes)) {
1089 1089
             // place single line_item_id in an array to appear as multiple line_item_ids
1090 1090
             $line_item_codes = array($line_item_codes);
1091 1091
         }
@@ -1192,7 +1192,7 @@  discard block
 block discarded – undo
1192 1192
         if ($code_substring_for_whitelist !== null) {
1193 1193
             $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1194 1194
         }
1195
-        if (! $whitelisted && $line_item->is_line_item()) {
1195
+        if ( ! $whitelisted && $line_item->is_line_item()) {
1196 1196
             $line_item->set_is_taxable($taxable);
1197 1197
         }
1198 1198
         foreach ($line_item->children() as $child_line_item) {
@@ -1554,7 +1554,7 @@  discard block
 block discarded – undo
1554 1554
     public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1555 1555
     {
1556 1556
         echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1557
-        if (! $indentation) {
1557
+        if ( ! $indentation) {
1558 1558
             echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559 1559
         }
1560 1560
         for ($i = 0; $i < $indentation; $i++) {
@@ -1565,12 +1565,12 @@  discard block
 block discarded – undo
1565 1565
             if ($line_item->is_percent()) {
1566 1566
                 $breakdown = "{$line_item->percent()}%";
1567 1567
             } else {
1568
-                $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1568
+                $breakdown = '$'."{$line_item->unit_price()} x {$line_item->quantity()}";
1569 1569
             }
1570 1570
         }
1571 1571
         echo $line_item->name();
1572 1572
         echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1573
-        echo '$' . (string) $line_item->total();
1573
+        echo '$'.(string) $line_item->total();
1574 1574
         if ($breakdown) {
1575 1575
             echo " ( {$breakdown} )";
1576 1576
         }
@@ -1658,8 +1658,8 @@  discard block
 block discarded – undo
1658 1658
                         if ($line_item_id === 'taxable') {
1659 1659
                             continue;
1660 1660
                         }
1661
-                        $taxable_total = $running_totals['taxable'][ $line_item_id ];
1662
-                        $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1661
+                        $taxable_total = $running_totals['taxable'][$line_item_id];
1662
+                        $running_totals[$line_item_id] += ($taxable_total * $tax_percent_decimal);
1663 1663
                     }
1664 1664
                     break;
1665 1665
 
@@ -1667,7 +1667,7 @@  discard block
 block discarded – undo
1667 1667
                     // ticket line items or ????
1668 1668
                     if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1669 1669
                         // kk it's a ticket
1670
-                        if (isset($running_totals[ $child_line_item->ID() ])) {
1670
+                        if (isset($running_totals[$child_line_item->ID()])) {
1671 1671
                             // huh? that shouldn't happen.
1672 1672
                             $running_totals['total'] += $child_line_item->total();
1673 1673
                         } else {
@@ -1678,18 +1678,18 @@  discard block
 block discarded – undo
1678 1678
                                 $taxable_amount = 0;
1679 1679
                             }
1680 1680
                             // are we only calculating totals for some tickets?
1681
-                            if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1682
-                                $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1683
-                                $running_totals[ $child_line_item->ID() ] = $quantity
1681
+                            if (isset($billable_ticket_quantities[$child_line_item->OBJ_ID()])) {
1682
+                                $quantity = $billable_ticket_quantities[$child_line_item->OBJ_ID()];
1683
+                                $running_totals[$child_line_item->ID()] = $quantity
1684 1684
                                     ? $child_line_item->unit_price()
1685 1685
                                     : 0;
1686
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1686
+                                $running_totals['taxable'][$child_line_item->ID()] = $quantity
1687 1687
                                     ? $taxable_amount
1688 1688
                                     : 0;
1689 1689
                             } else {
1690 1690
                                 $quantity = $child_line_item->quantity();
1691
-                                $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1692
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1691
+                                $running_totals[$child_line_item->ID()] = $child_line_item->unit_price();
1692
+                                $running_totals['taxable'][$child_line_item->ID()] = $taxable_amount;
1693 1693
                             }
1694 1694
                             $running_totals['taxable']['total'] += $taxable_amount * $quantity;
1695 1695
                             $running_totals['total'] += $child_line_item->unit_price() * $quantity;
@@ -1709,12 +1709,12 @@  discard block
 block discarded – undo
1709 1709
                             }
1710 1710
                             // update the running totals
1711 1711
                             // yes this actually even works for the running grand total!
1712
-                            $running_totals[ $line_item_id ] =
1712
+                            $running_totals[$line_item_id] =
1713 1713
                                 $line_items_percent_of_running_total * $this_running_total;
1714 1714
 
1715 1715
                             if ($child_line_item->is_taxable()) {
1716
-                                $running_totals['taxable'][ $line_item_id ] =
1717
-                                    $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1716
+                                $running_totals['taxable'][$line_item_id] =
1717
+                                    $line_items_percent_of_running_total * $running_totals['taxable'][$line_item_id];
1718 1718
                             }
1719 1719
                         }
1720 1720
                     }
@@ -1747,8 +1747,8 @@  discard block
 block discarded – undo
1747 1747
             );
1748 1748
         }
1749 1749
         // ok now find this new registration's final price
1750
-        if (isset($final_prices_per_ticket_line_item[ $ticket_line_item->ID() ])) {
1751
-            return $final_prices_per_ticket_line_item[ $ticket_line_item->ID() ];
1750
+        if (isset($final_prices_per_ticket_line_item[$ticket_line_item->ID()])) {
1751
+            return $final_prices_per_ticket_line_item[$ticket_line_item->ID()];
1752 1752
         }
1753 1753
         $message = sprintf(
1754 1754
             esc_html__(
@@ -1758,7 +1758,7 @@  discard block
 block discarded – undo
1758 1758
             $ticket_line_item->ID()
1759 1759
         );
1760 1760
         if (WP_DEBUG) {
1761
-            $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1761
+            $message .= '<br>'.print_r($final_prices_per_ticket_line_item, true);
1762 1762
             throw new OutOfRangeException($message);
1763 1763
         }
1764 1764
         EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
Please login to merge, or discard this patch.
Indentation   +2041 added lines, -2041 removed lines patch added patch discarded remove patch
@@ -21,2045 +21,2045 @@
 block discarded – undo
21 21
 class EEH_Line_Item
22 22
 {
23 23
 
24
-    /**
25
-     * Adds a simple item (unrelated to any other model object) to the provided PARENT line item.
26
-     * Does NOT automatically re-calculate the line item totals or update the related transaction.
27
-     * You should call recalculate_total_including_taxes() on the grant total line item after this
28
-     * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
29
-     * to keep the registration final prices in-sync with the transaction's total.
30
-     *
31
-     * @param EE_Line_Item $parent_line_item
32
-     * @param string       $name
33
-     * @param float        $unit_price
34
-     * @param string       $description
35
-     * @param int          $quantity
36
-     * @param boolean      $taxable
37
-     * @param boolean      $code if set to a value, ensures there is only one line item with that code
38
-     * @return boolean success
39
-     * @throws EE_Error
40
-     * @throws InvalidArgumentException
41
-     * @throws InvalidDataTypeException
42
-     * @throws InvalidInterfaceException
43
-     * @throws ReflectionException
44
-     */
45
-    public static function add_unrelated_item(
46
-        EE_Line_Item $parent_line_item,
47
-        $name,
48
-        $unit_price,
49
-        $description = '',
50
-        $quantity = 1,
51
-        $taxable = false,
52
-        $code = null
53
-    ) {
54
-        $items_subtotal = self::get_pre_tax_subtotal($parent_line_item);
55
-        $line_item = EE_Line_Item::new_instance(array(
56
-            'LIN_name'       => $name,
57
-            'LIN_desc'       => $description,
58
-            'LIN_unit_price' => $unit_price,
59
-            'LIN_quantity'   => $quantity,
60
-            'LIN_percent'    => null,
61
-            'LIN_is_taxable' => $taxable,
62
-            'LIN_order'      => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0,
63
-            'LIN_total'      => (float) $unit_price * (int) $quantity,
64
-            'LIN_type'       => EEM_Line_Item::type_line_item,
65
-            'LIN_code'       => $code,
66
-        ));
67
-        $line_item = apply_filters(
68
-            'FHEE__EEH_Line_Item__add_unrelated_item__line_item',
69
-            $line_item,
70
-            $parent_line_item
71
-        );
72
-        return self::add_item($parent_line_item, $line_item);
73
-    }
74
-
75
-
76
-    /**
77
-     * Adds a simple item ( unrelated to any other model object) to the total line item,
78
-     * in the correct spot in the line item tree. Automatically
79
-     * re-calculates the line item totals and updates the related transaction. But
80
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
81
-     * should probably change because of this).
82
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
83
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
84
-     *
85
-     * @param EE_Line_Item $parent_line_item
86
-     * @param string       $name
87
-     * @param float        $percentage_amount
88
-     * @param string       $description
89
-     * @param boolean      $taxable
90
-     * @return boolean success
91
-     * @throws EE_Error
92
-     */
93
-    public static function add_percentage_based_item(
94
-        EE_Line_Item $parent_line_item,
95
-        $name,
96
-        $percentage_amount,
97
-        $description = '',
98
-        $taxable = false
99
-    ) {
100
-        $line_item = EE_Line_Item::new_instance(array(
101
-            'LIN_name'       => $name,
102
-            'LIN_desc'       => $description,
103
-            'LIN_unit_price' => 0,
104
-            'LIN_percent'    => $percentage_amount,
105
-            'LIN_quantity'   => 1,
106
-            'LIN_is_taxable' => $taxable,
107
-            'LIN_total'      => (float) ($percentage_amount * ($parent_line_item->total() / 100)),
108
-            'LIN_type'       => EEM_Line_Item::type_line_item,
109
-            'LIN_parent'     => $parent_line_item->ID(),
110
-        ));
111
-        $line_item = apply_filters(
112
-            'FHEE__EEH_Line_Item__add_percentage_based_item__line_item',
113
-            $line_item
114
-        );
115
-        return $parent_line_item->add_child_line_item($line_item, false);
116
-    }
117
-
118
-
119
-    /**
120
-     * Returns the new line item created by adding a purchase of the ticket
121
-     * ensures that ticket line item is saved, and that cart total has been recalculated.
122
-     * If this ticket has already been purchased, just increments its count.
123
-     * Automatically re-calculates the line item totals and updates the related transaction. But
124
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
125
-     * should probably change because of this).
126
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
127
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
128
-     *
129
-     * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total
130
-     * @param EE_Ticket    $ticket
131
-     * @param int          $qty
132
-     * @return EE_Line_Item
133
-     * @throws EE_Error
134
-     * @throws InvalidArgumentException
135
-     * @throws InvalidDataTypeException
136
-     * @throws InvalidInterfaceException
137
-     * @throws ReflectionException
138
-     */
139
-    public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140
-    {
141
-        if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142
-            throw new EE_Error(
143
-                sprintf(
144
-                    esc_html__(
145
-                        'A valid line item total is required in order to add tickets. A line item of type "%s" was passed.',
146
-                        'event_espresso'
147
-                    ),
148
-                    $ticket->ID(),
149
-                    $total_line_item->ID()
150
-                )
151
-            );
152
-        }
153
-        // either increment the qty for an existing ticket
154
-        $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155
-        // or add a new one
156
-        if (! $line_item instanceof EE_Line_Item) {
157
-            $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158
-        }
159
-        $total_line_item->recalculate_total_including_taxes();
160
-        return $line_item;
161
-    }
162
-
163
-
164
-    /**
165
-     * Returns the new line item created by adding a purchase of the ticket
166
-     *
167
-     * @param EE_Line_Item $total_line_item
168
-     * @param EE_Ticket    $ticket
169
-     * @param int          $qty
170
-     * @return EE_Line_Item
171
-     * @throws EE_Error
172
-     * @throws InvalidArgumentException
173
-     * @throws InvalidDataTypeException
174
-     * @throws InvalidInterfaceException
175
-     * @throws ReflectionException
176
-     */
177
-    public static function increment_ticket_qty_if_already_in_cart(
178
-        EE_Line_Item $total_line_item,
179
-        EE_Ticket $ticket,
180
-        $qty = 1
181
-    ) {
182
-        $line_item = null;
183
-        if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) {
184
-            $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
185
-            foreach ((array) $ticket_line_items as $ticket_line_item) {
186
-                if ($ticket_line_item instanceof EE_Line_Item
187
-                    && (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID()
188
-                ) {
189
-                    $line_item = $ticket_line_item;
190
-                    break;
191
-                }
192
-            }
193
-        }
194
-        if ($line_item instanceof EE_Line_Item) {
195
-            EEH_Line_Item::increment_quantity($line_item, $qty);
196
-            return $line_item;
197
-        }
198
-        return null;
199
-    }
200
-
201
-
202
-    /**
203
-     * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected).
204
-     * Does NOT save or recalculate other line items totals
205
-     *
206
-     * @param EE_Line_Item $line_item
207
-     * @param int          $qty
208
-     * @return void
209
-     * @throws EE_Error
210
-     * @throws InvalidArgumentException
211
-     * @throws InvalidDataTypeException
212
-     * @throws InvalidInterfaceException
213
-     * @throws ReflectionException
214
-     */
215
-    public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216
-    {
217
-        if (! $line_item->is_percent()) {
218
-            $qty += $line_item->quantity();
219
-            $line_item->set_quantity($qty);
220
-            $line_item->set_total($line_item->unit_price() * $qty);
221
-            $line_item->save();
222
-        }
223
-        foreach ($line_item->children() as $child) {
224
-            if ($child->is_sub_line_item()) {
225
-                EEH_Line_Item::update_quantity($child, $qty);
226
-            }
227
-        }
228
-    }
229
-
230
-
231
-    /**
232
-     * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected).
233
-     * Does NOT save or recalculate other line items totals
234
-     *
235
-     * @param EE_Line_Item $line_item
236
-     * @param int          $qty
237
-     * @return void
238
-     * @throws EE_Error
239
-     * @throws InvalidArgumentException
240
-     * @throws InvalidDataTypeException
241
-     * @throws InvalidInterfaceException
242
-     * @throws ReflectionException
243
-     */
244
-    public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245
-    {
246
-        if (! $line_item->is_percent()) {
247
-            $qty = $line_item->quantity() - $qty;
248
-            $qty = max($qty, 0);
249
-            $line_item->set_quantity($qty);
250
-            $line_item->set_total($line_item->unit_price() * $qty);
251
-            $line_item->save();
252
-        }
253
-        foreach ($line_item->children() as $child) {
254
-            if ($child->is_sub_line_item()) {
255
-                EEH_Line_Item::update_quantity($child, $qty);
256
-            }
257
-        }
258
-    }
259
-
260
-
261
-    /**
262
-     * Updates the line item and its children's quantities to the specified number.
263
-     * Does NOT save them or recalculate totals.
264
-     *
265
-     * @param EE_Line_Item $line_item
266
-     * @param int          $new_quantity
267
-     * @throws EE_Error
268
-     * @throws InvalidArgumentException
269
-     * @throws InvalidDataTypeException
270
-     * @throws InvalidInterfaceException
271
-     * @throws ReflectionException
272
-     */
273
-    public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274
-    {
275
-        if (! $line_item->is_percent()) {
276
-            $line_item->set_quantity($new_quantity);
277
-            $line_item->set_total($line_item->unit_price() * $new_quantity);
278
-            $line_item->save();
279
-        }
280
-        foreach ($line_item->children() as $child) {
281
-            if ($child->is_sub_line_item()) {
282
-                EEH_Line_Item::update_quantity($child, $new_quantity);
283
-            }
284
-        }
285
-    }
286
-
287
-
288
-    /**
289
-     * Returns the new line item created by adding a purchase of the ticket
290
-     *
291
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
292
-     * @param EE_Ticket    $ticket
293
-     * @param int          $qty
294
-     * @return EE_Line_Item
295
-     * @throws EE_Error
296
-     * @throws InvalidArgumentException
297
-     * @throws InvalidDataTypeException
298
-     * @throws InvalidInterfaceException
299
-     * @throws ReflectionException
300
-     */
301
-    public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
302
-    {
303
-        $datetimes = $ticket->datetimes();
304
-        $first_datetime = reset($datetimes);
305
-        $first_datetime_name = esc_html__('Event', 'event_espresso');
306
-        if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) {
307
-            $first_datetime_name = $first_datetime->event()->name();
308
-        }
309
-        $event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name);
310
-        // get event subtotal line
311
-        $events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket);
312
-        // add $ticket to cart
313
-        $line_item = EE_Line_Item::new_instance(array(
314
-            'LIN_name'       => $ticket->name(),
315
-            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
316
-            'LIN_unit_price' => $ticket->price(),
317
-            'LIN_quantity'   => $qty,
318
-            'LIN_is_taxable' => $ticket->taxable(),
319
-            'LIN_order'      => count($events_sub_total->children()),
320
-            'LIN_total'      => $ticket->price() * $qty,
321
-            'LIN_type'       => EEM_Line_Item::type_line_item,
322
-            'OBJ_ID'         => $ticket->ID(),
323
-            'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_TICKET,
324
-        ));
325
-        $line_item = apply_filters(
326
-            'FHEE__EEH_Line_Item__create_ticket_line_item__line_item',
327
-            $line_item
328
-        );
329
-        $events_sub_total->add_child_line_item($line_item);
330
-        // now add the sub-line items
331
-        $running_total_for_ticket = 0;
332
-        foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) {
333
-            $sign = $price->is_discount() ? -1 : 1;
334
-            $price_total = $price->is_percent()
335
-                ? $running_total_for_ticket * $price->amount() / 100
336
-                : $price->amount() * $qty;
337
-            $sub_line_item = EE_Line_Item::new_instance(array(
338
-                'LIN_name'       => $price->name(),
339
-                'LIN_desc'       => $price->desc(),
340
-                'LIN_quantity'   => $price->is_percent() ? null : $qty,
341
-                'LIN_is_taxable' => false,
342
-                'LIN_order'      => $price->order(),
343
-                'LIN_total'      => $sign * $price_total,
344
-                'LIN_type'       => EEM_Line_Item::type_sub_line_item,
345
-                'OBJ_ID'         => $price->ID(),
346
-                'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
347
-            ));
348
-            $sub_line_item = apply_filters(
349
-                'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item',
350
-                $sub_line_item
351
-            );
352
-            if ($price->is_percent()) {
353
-                $sub_line_item->set_percent($sign * $price->amount());
354
-            } else {
355
-                $sub_line_item->set_unit_price($sign * $price->amount());
356
-            }
357
-            $running_total_for_ticket += $price_total;
358
-            $line_item->add_child_line_item($sub_line_item);
359
-        }
360
-        return $line_item;
361
-    }
362
-
363
-
364
-    /**
365
-     * Adds the specified item under the pre-tax-sub-total line item. Automatically
366
-     * re-calculates the line item totals and updates the related transaction. But
367
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
368
-     * should probably change because of this).
369
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
370
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
371
-     *
372
-     * @param EE_Line_Item $total_line_item
373
-     * @param EE_Line_Item $item to be added
374
-     * @return boolean
375
-     * @throws EE_Error
376
-     * @throws InvalidArgumentException
377
-     * @throws InvalidDataTypeException
378
-     * @throws InvalidInterfaceException
379
-     * @throws ReflectionException
380
-     */
381
-    public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item)
382
-    {
383
-        $pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item);
384
-        if ($pre_tax_subtotal instanceof EE_Line_Item) {
385
-            $success = $pre_tax_subtotal->add_child_line_item($item);
386
-        } else {
387
-            return false;
388
-        }
389
-        $total_line_item->recalculate_total_including_taxes();
390
-        return $success;
391
-    }
392
-
393
-
394
-    /**
395
-     * cancels an existing ticket line item,
396
-     * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item.
397
-     * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
398
-     *
399
-     * @param EE_Line_Item $ticket_line_item
400
-     * @param int          $qty
401
-     * @return bool success
402
-     * @throws EE_Error
403
-     * @throws InvalidArgumentException
404
-     * @throws InvalidDataTypeException
405
-     * @throws InvalidInterfaceException
406
-     * @throws ReflectionException
407
-     */
408
-    public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
409
-    {
410
-        // validate incoming line_item
411
-        if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
412
-            throw new EE_Error(
413
-                sprintf(
414
-                    esc_html__(
415
-                        'The supplied line item must have an Object Type of "Ticket", not %1$s.',
416
-                        'event_espresso'
417
-                    ),
418
-                    $ticket_line_item->type()
419
-                )
420
-            );
421
-        }
422
-        if ($ticket_line_item->quantity() < $qty) {
423
-            throw new EE_Error(
424
-                sprintf(
425
-                    esc_html__(
426
-                        'Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.',
427
-                        'event_espresso'
428
-                    ),
429
-                    $qty,
430
-                    $ticket_line_item->quantity()
431
-                )
432
-            );
433
-        }
434
-        // decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this
435
-        $ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty);
436
-        foreach ($ticket_line_item->children() as $child_line_item) {
437
-            if ($child_line_item->is_sub_line_item()
438
-                && ! $child_line_item->is_percent()
439
-                && ! $child_line_item->is_cancellation()
440
-            ) {
441
-                $child_line_item->set_quantity($child_line_item->quantity() - $qty);
442
-            }
443
-        }
444
-        // get cancellation sub line item
445
-        $cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
446
-            $ticket_line_item,
447
-            EEM_Line_Item::type_cancellation
448
-        );
449
-        $cancellation_line_item = reset($cancellation_line_item);
450
-        // verify that this ticket was indeed previously cancelled
451
-        if ($cancellation_line_item instanceof EE_Line_Item) {
452
-            // increment cancelled quantity
453
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty);
454
-        } else {
455
-            // create cancellation sub line item
456
-            $cancellation_line_item = EE_Line_Item::new_instance(array(
457
-                'LIN_name'       => esc_html__('Cancellation', 'event_espresso'),
458
-                'LIN_desc'       => sprintf(
459
-                    esc_html_x(
460
-                        'Cancelled %1$s : %2$s',
461
-                        'Cancelled Ticket Name : 2015-01-01 11:11',
462
-                        'event_espresso'
463
-                    ),
464
-                    $ticket_line_item->name(),
465
-                    current_time(get_option('date_format') . ' ' . get_option('time_format'))
466
-                ),
467
-                'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468
-                'LIN_quantity'   => $qty,
469
-                'LIN_is_taxable' => $ticket_line_item->is_taxable(),
470
-                'LIN_order'      => count($ticket_line_item->children()),
471
-                'LIN_total'      => 0, // $ticket_line_item->unit_price()
472
-                'LIN_type'       => EEM_Line_Item::type_cancellation,
473
-            ));
474
-            $ticket_line_item->add_child_line_item($cancellation_line_item);
475
-        }
476
-        if ($ticket_line_item->save_this_and_descendants() > 0) {
477
-            // decrement parent line item quantity
478
-            $event_line_item = $ticket_line_item->parent();
479
-            if ($event_line_item instanceof EE_Line_Item
480
-                && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
481
-            ) {
482
-                $event_line_item->set_quantity($event_line_item->quantity() - $qty);
483
-                $event_line_item->save();
484
-            }
485
-            EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
486
-            return true;
487
-        }
488
-        return false;
489
-    }
490
-
491
-
492
-    /**
493
-     * reinstates (un-cancels?) a previously canceled ticket line item,
494
-     * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item.
495
-     * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
496
-     *
497
-     * @param EE_Line_Item $ticket_line_item
498
-     * @param int          $qty
499
-     * @return bool success
500
-     * @throws EE_Error
501
-     * @throws InvalidArgumentException
502
-     * @throws InvalidDataTypeException
503
-     * @throws InvalidInterfaceException
504
-     * @throws ReflectionException
505
-     */
506
-    public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
507
-    {
508
-        // validate incoming line_item
509
-        if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
510
-            throw new EE_Error(
511
-                sprintf(
512
-                    esc_html__(
513
-                        'The supplied line item must have an Object Type of "Ticket", not %1$s.',
514
-                        'event_espresso'
515
-                    ),
516
-                    $ticket_line_item->type()
517
-                )
518
-            );
519
-        }
520
-        // get cancellation sub line item
521
-        $cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
522
-            $ticket_line_item,
523
-            EEM_Line_Item::type_cancellation
524
-        );
525
-        $cancellation_line_item = reset($cancellation_line_item);
526
-        // verify that this ticket was indeed previously cancelled
527
-        if (! $cancellation_line_item instanceof EE_Line_Item) {
528
-            return false;
529
-        }
530
-        if ($cancellation_line_item->quantity() > $qty) {
531
-            // decrement cancelled quantity
532
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
533
-        } elseif ($cancellation_line_item->quantity() === $qty) {
534
-            // decrement cancelled quantity in case anyone still has the object kicking around
535
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
536
-            // delete because quantity will end up as 0
537
-            $cancellation_line_item->delete();
538
-            // and attempt to destroy the object,
539
-            // even though PHP won't actually destroy it until it needs the memory
540
-            unset($cancellation_line_item);
541
-        } else {
542
-            // what ?!?! negative quantity ?!?!
543
-            throw new EE_Error(
544
-                sprintf(
545
-                    esc_html__(
546
-                        'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.',
547
-                        'event_espresso'
548
-                    ),
549
-                    $qty,
550
-                    $cancellation_line_item->quantity()
551
-                )
552
-            );
553
-        }
554
-        // increment ticket quantity
555
-        $ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty);
556
-        if ($ticket_line_item->save_this_and_descendants() > 0) {
557
-            // increment parent line item quantity
558
-            $event_line_item = $ticket_line_item->parent();
559
-            if ($event_line_item instanceof EE_Line_Item
560
-                && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
561
-            ) {
562
-                $event_line_item->set_quantity($event_line_item->quantity() + $qty);
563
-            }
564
-            EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
565
-            return true;
566
-        }
567
-        return false;
568
-    }
569
-
570
-
571
-    /**
572
-     * calls EEH_Line_Item::find_transaction_grand_total_for_line_item()
573
-     * then EE_Line_Item::recalculate_total_including_taxes() on the result
574
-     *
575
-     * @param EE_Line_Item $line_item
576
-     * @return float
577
-     * @throws EE_Error
578
-     * @throws InvalidArgumentException
579
-     * @throws InvalidDataTypeException
580
-     * @throws InvalidInterfaceException
581
-     * @throws ReflectionException
582
-     */
583
-    public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item)
584
-    {
585
-        $grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item);
586
-        return $grand_total_line_item->recalculate_total_including_taxes();
587
-    }
588
-
589
-
590
-    /**
591
-     * Gets the line item which contains the subtotal of all the items
592
-     *
593
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
594
-     * @return EE_Line_Item
595
-     * @throws EE_Error
596
-     * @throws InvalidArgumentException
597
-     * @throws InvalidDataTypeException
598
-     * @throws InvalidInterfaceException
599
-     * @throws ReflectionException
600
-     */
601
-    public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item)
602
-    {
603
-        $pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal');
604
-        return $pre_tax_subtotal instanceof EE_Line_Item
605
-            ? $pre_tax_subtotal
606
-            : self::create_pre_tax_subtotal($total_line_item);
607
-    }
608
-
609
-
610
-    /**
611
-     * Gets the line item for the taxes subtotal
612
-     *
613
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
614
-     * @return EE_Line_Item
615
-     * @throws EE_Error
616
-     * @throws InvalidArgumentException
617
-     * @throws InvalidDataTypeException
618
-     * @throws InvalidInterfaceException
619
-     * @throws ReflectionException
620
-     */
621
-    public static function get_taxes_subtotal(EE_Line_Item $total_line_item)
622
-    {
623
-        $taxes = $total_line_item->get_child_line_item('taxes');
624
-        return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item);
625
-    }
626
-
627
-
628
-    /**
629
-     * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object
630
-     *
631
-     * @param EE_Line_Item   $line_item
632
-     * @param EE_Transaction $transaction
633
-     * @return void
634
-     * @throws EE_Error
635
-     * @throws InvalidArgumentException
636
-     * @throws InvalidDataTypeException
637
-     * @throws InvalidInterfaceException
638
-     * @throws ReflectionException
639
-     */
640
-    public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null)
641
-    {
642
-        if ($transaction) {
643
-            /** @type EEM_Transaction $EEM_Transaction */
644
-            $EEM_Transaction = EE_Registry::instance()->load_model('Transaction');
645
-            $TXN_ID = $EEM_Transaction->ensure_is_ID($transaction);
646
-            $line_item->set_TXN_ID($TXN_ID);
647
-        }
648
-    }
649
-
650
-
651
-    /**
652
-     * Creates a new default total line item for the transaction,
653
-     * and its tickets subtotal and taxes subtotal line items (and adds the
654
-     * existing taxes as children of the taxes subtotal line item)
655
-     *
656
-     * @param EE_Transaction $transaction
657
-     * @return EE_Line_Item of type total
658
-     * @throws EE_Error
659
-     * @throws InvalidArgumentException
660
-     * @throws InvalidDataTypeException
661
-     * @throws InvalidInterfaceException
662
-     * @throws ReflectionException
663
-     */
664
-    public static function create_total_line_item($transaction = null)
665
-    {
666
-        $total_line_item = EE_Line_Item::new_instance(array(
667
-            'LIN_code' => 'total',
668
-            'LIN_name' => esc_html__('Grand Total', 'event_espresso'),
669
-            'LIN_type' => EEM_Line_Item::type_total,
670
-            'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TRANSACTION,
671
-        ));
672
-        $total_line_item = apply_filters(
673
-            'FHEE__EEH_Line_Item__create_total_line_item__total_line_item',
674
-            $total_line_item
675
-        );
676
-        self::set_TXN_ID($total_line_item, $transaction);
677
-        self::create_pre_tax_subtotal($total_line_item, $transaction);
678
-        self::create_taxes_subtotal($total_line_item, $transaction);
679
-        return $total_line_item;
680
-    }
681
-
682
-
683
-    /**
684
-     * Creates a default items subtotal line item
685
-     *
686
-     * @param EE_Line_Item   $total_line_item
687
-     * @param EE_Transaction $transaction
688
-     * @return EE_Line_Item
689
-     * @throws EE_Error
690
-     * @throws InvalidArgumentException
691
-     * @throws InvalidDataTypeException
692
-     * @throws InvalidInterfaceException
693
-     * @throws ReflectionException
694
-     */
695
-    protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null)
696
-    {
697
-        $pre_tax_line_item = EE_Line_Item::new_instance(array(
698
-            'LIN_code' => 'pre-tax-subtotal',
699
-            'LIN_name' => esc_html__('Pre-Tax Subtotal', 'event_espresso'),
700
-            'LIN_type' => EEM_Line_Item::type_sub_total,
701
-        ));
702
-        $pre_tax_line_item = apply_filters(
703
-            'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item',
704
-            $pre_tax_line_item
705
-        );
706
-        self::set_TXN_ID($pre_tax_line_item, $transaction);
707
-        $total_line_item->add_child_line_item($pre_tax_line_item);
708
-        self::create_event_subtotal($pre_tax_line_item, $transaction);
709
-        return $pre_tax_line_item;
710
-    }
711
-
712
-
713
-    /**
714
-     * Creates a line item for the taxes subtotal and finds all the tax prices
715
-     * and applies taxes to it
716
-     *
717
-     * @param EE_Line_Item   $total_line_item of type EEM_Line_Item::type_total
718
-     * @param EE_Transaction $transaction
719
-     * @return EE_Line_Item
720
-     * @throws EE_Error
721
-     * @throws InvalidArgumentException
722
-     * @throws InvalidDataTypeException
723
-     * @throws InvalidInterfaceException
724
-     * @throws ReflectionException
725
-     */
726
-    protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
727
-    {
728
-        $tax_line_item = EE_Line_Item::new_instance(array(
729
-            'LIN_code'  => 'taxes',
730
-            'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731
-            'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
-            'LIN_order' => 1000,// this should always come last
733
-        ));
734
-        $tax_line_item = apply_filters(
735
-            'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
736
-            $tax_line_item
737
-        );
738
-        self::set_TXN_ID($tax_line_item, $transaction);
739
-        $total_line_item->add_child_line_item($tax_line_item);
740
-        // and lastly, add the actual taxes
741
-        self::apply_taxes($total_line_item);
742
-        return $tax_line_item;
743
-    }
744
-
745
-
746
-    /**
747
-     * Creates a default items subtotal line item
748
-     *
749
-     * @param EE_Line_Item   $pre_tax_line_item
750
-     * @param EE_Transaction $transaction
751
-     * @param EE_Event       $event
752
-     * @return EE_Line_Item
753
-     * @throws EE_Error
754
-     * @throws InvalidArgumentException
755
-     * @throws InvalidDataTypeException
756
-     * @throws InvalidInterfaceException
757
-     * @throws ReflectionException
758
-     */
759
-    public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null)
760
-    {
761
-        $event_line_item = EE_Line_Item::new_instance(array(
762
-            'LIN_code' => self::get_event_code($event),
763
-            'LIN_name' => self::get_event_name($event),
764
-            'LIN_desc' => self::get_event_desc($event),
765
-            'LIN_type' => EEM_Line_Item::type_sub_total,
766
-            'OBJ_type' => EEM_Line_Item::OBJ_TYPE_EVENT,
767
-            'OBJ_ID'   => $event instanceof EE_Event ? $event->ID() : 0,
768
-        ));
769
-        $event_line_item = apply_filters(
770
-            'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item',
771
-            $event_line_item
772
-        );
773
-        self::set_TXN_ID($event_line_item, $transaction);
774
-        $pre_tax_line_item->add_child_line_item($event_line_item);
775
-        return $event_line_item;
776
-    }
777
-
778
-
779
-    /**
780
-     * Gets what the event ticket's code SHOULD be
781
-     *
782
-     * @param EE_Event $event
783
-     * @return string
784
-     * @throws EE_Error
785
-     */
786
-    public static function get_event_code($event)
787
-    {
788
-        return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
789
-    }
790
-
791
-
792
-    /**
793
-     * Gets the event name
794
-     *
795
-     * @param EE_Event $event
796
-     * @return string
797
-     * @throws EE_Error
798
-     */
799
-    public static function get_event_name($event)
800
-    {
801
-        return $event instanceof EE_Event
802
-            ? mb_substr($event->name(), 0, 245)
803
-            : esc_html__('Event', 'event_espresso');
804
-    }
805
-
806
-
807
-    /**
808
-     * Gets the event excerpt
809
-     *
810
-     * @param EE_Event $event
811
-     * @return string
812
-     * @throws EE_Error
813
-     */
814
-    public static function get_event_desc($event)
815
-    {
816
-        return $event instanceof EE_Event ? $event->short_description() : '';
817
-    }
818
-
819
-
820
-    /**
821
-     * Given the grand total line item and a ticket, finds the event sub-total
822
-     * line item the ticket's purchase should be added onto
823
-     *
824
-     * @access public
825
-     * @param EE_Line_Item $grand_total the grand total line item
826
-     * @param EE_Ticket    $ticket
827
-     * @return EE_Line_Item
828
-     * @throws EE_Error
829
-     * @throws InvalidArgumentException
830
-     * @throws InvalidDataTypeException
831
-     * @throws InvalidInterfaceException
832
-     * @throws ReflectionException
833
-     */
834
-    public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835
-    {
836
-        $first_datetime = $ticket->first_datetime();
837
-        if (! $first_datetime instanceof EE_Datetime) {
838
-            throw new EE_Error(
839
-                sprintf(
840
-                    __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
841
-                    $ticket->ID()
842
-                )
843
-            );
844
-        }
845
-        $event = $first_datetime->event();
846
-        if (! $event instanceof EE_Event) {
847
-            throw new EE_Error(
848
-                sprintf(
849
-                    esc_html__(
850
-                        'The supplied ticket (ID %d) has no event data associated with it.',
851
-                        'event_espresso'
852
-                    ),
853
-                    $ticket->ID()
854
-                )
855
-            );
856
-        }
857
-        $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
-        if (! $events_sub_total instanceof EE_Line_Item) {
859
-            throw new EE_Error(
860
-                sprintf(
861
-                    esc_html__(
862
-                        'There is no events sub-total for ticket %s on total line item %d',
863
-                        'event_espresso'
864
-                    ),
865
-                    $ticket->ID(),
866
-                    $grand_total->ID()
867
-                )
868
-            );
869
-        }
870
-        return $events_sub_total;
871
-    }
872
-
873
-
874
-    /**
875
-     * Gets the event line item
876
-     *
877
-     * @param EE_Line_Item $grand_total
878
-     * @param EE_Event     $event
879
-     * @return EE_Line_Item for the event subtotal which is a child of $grand_total
880
-     * @throws EE_Error
881
-     * @throws InvalidArgumentException
882
-     * @throws InvalidDataTypeException
883
-     * @throws InvalidInterfaceException
884
-     * @throws ReflectionException
885
-     */
886
-    public static function get_event_line_item(EE_Line_Item $grand_total, $event)
887
-    {
888
-        /** @type EE_Event $event */
889
-        $event = EEM_Event::instance()->ensure_is_obj($event, true);
890
-        $event_line_item = null;
891
-        $found = false;
892
-        foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893
-            // default event subtotal, we should only ever find this the first time this method is called
894
-            if (! $event_line_item->OBJ_ID()) {
895
-                // let's use this! but first... set the event details
896
-                EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897
-                $found = true;
898
-                break;
899
-            }
900
-            if ($event_line_item->OBJ_ID() === $event->ID()) {
901
-                // found existing line item for this event in the cart, so break out of loop and use this one
902
-                $found = true;
903
-                break;
904
-            }
905
-        }
906
-        if (! $found) {
907
-            // there is no event sub-total yet, so add it
908
-            $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909
-            // create a new "event" subtotal below that
910
-            $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event);
911
-            // and set the event details
912
-            EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
913
-        }
914
-        return $event_line_item;
915
-    }
916
-
917
-
918
-    /**
919
-     * Creates a default items subtotal line item
920
-     *
921
-     * @param EE_Line_Item   $event_line_item
922
-     * @param EE_Event       $event
923
-     * @param EE_Transaction $transaction
924
-     * @return void
925
-     * @throws EE_Error
926
-     * @throws InvalidArgumentException
927
-     * @throws InvalidDataTypeException
928
-     * @throws InvalidInterfaceException
929
-     * @throws ReflectionException
930
-     */
931
-    public static function set_event_subtotal_details(
932
-        EE_Line_Item $event_line_item,
933
-        EE_Event $event,
934
-        $transaction = null
935
-    ) {
936
-        if ($event instanceof EE_Event) {
937
-            $event_line_item->set_code(self::get_event_code($event));
938
-            $event_line_item->set_name(self::get_event_name($event));
939
-            $event_line_item->set_desc(self::get_event_desc($event));
940
-            $event_line_item->set_OBJ_ID($event->ID());
941
-        }
942
-        self::set_TXN_ID($event_line_item, $transaction);
943
-    }
944
-
945
-
946
-    /**
947
-     * Finds what taxes should apply, adds them as tax line items under the taxes sub-total,
948
-     * and recalculates the taxes sub-total and the grand total. Resets the taxes, so
949
-     * any old taxes are removed
950
-     *
951
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
952
-     * @param bool         $update_txn_status
953
-     * @return bool
954
-     * @throws EE_Error
955
-     * @throws InvalidArgumentException
956
-     * @throws InvalidDataTypeException
957
-     * @throws InvalidInterfaceException
958
-     * @throws ReflectionException
959
-     * @throws RuntimeException
960
-     */
961
-    public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false)
962
-    {
963
-        /** @type EEM_Price $EEM_Price */
964
-        $EEM_Price = EE_Registry::instance()->load_model('Price');
965
-        // get array of taxes via Price Model
966
-        $ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes();
967
-        ksort($ordered_taxes);
968
-        $taxes_line_item = self::get_taxes_subtotal($total_line_item);
969
-        // just to be safe, remove its old tax line items
970
-        $deleted = $taxes_line_item->delete_children_line_items();
971
-        $updates = false;
972
-        // loop thru taxes
973
-        foreach ($ordered_taxes as $order => $taxes) {
974
-            foreach ($taxes as $tax) {
975
-                if ($tax instanceof EE_Price) {
976
-                    $tax_line_item = EE_Line_Item::new_instance(
977
-                        array(
978
-                            'LIN_name'       => $tax->name(),
979
-                            'LIN_desc'       => $tax->desc(),
980
-                            'LIN_percent'    => $tax->amount(),
981
-                            'LIN_is_taxable' => false,
982
-                            'LIN_order'      => $order,
983
-                            'LIN_total'      => 0,
984
-                            'LIN_type'       => EEM_Line_Item::type_tax,
985
-                            'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
986
-                            'OBJ_ID'         => $tax->ID(),
987
-                        )
988
-                    );
989
-                    $tax_line_item = apply_filters(
990
-                        'FHEE__EEH_Line_Item__apply_taxes__tax_line_item',
991
-                        $tax_line_item
992
-                    );
993
-                    $updates = $taxes_line_item->add_child_line_item($tax_line_item) ?
994
-                        true :
995
-                        $updates;
996
-                }
997
-            }
998
-        }
999
-        // only recalculate totals if something changed
1000
-        if ($deleted || $updates) {
1001
-            $total_line_item->recalculate_total_including_taxes($update_txn_status);
1002
-            return true;
1003
-        }
1004
-        return false;
1005
-    }
1006
-
1007
-
1008
-    /**
1009
-     * Ensures that taxes have been applied to the order, if not applies them.
1010
-     * Returns the total amount of tax
1011
-     *
1012
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
1013
-     * @return float
1014
-     * @throws EE_Error
1015
-     * @throws InvalidArgumentException
1016
-     * @throws InvalidDataTypeException
1017
-     * @throws InvalidInterfaceException
1018
-     * @throws ReflectionException
1019
-     */
1020
-    public static function ensure_taxes_applied($total_line_item)
1021
-    {
1022
-        $taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1023
-        if (! $taxes_subtotal->children()) {
1024
-            self::apply_taxes($total_line_item);
1025
-        }
1026
-        return $taxes_subtotal->total();
1027
-    }
1028
-
1029
-
1030
-    /**
1031
-     * Deletes ALL children of the passed line item
1032
-     *
1033
-     * @param EE_Line_Item $parent_line_item
1034
-     * @return bool
1035
-     * @throws EE_Error
1036
-     * @throws InvalidArgumentException
1037
-     * @throws InvalidDataTypeException
1038
-     * @throws InvalidInterfaceException
1039
-     * @throws ReflectionException
1040
-     */
1041
-    public static function delete_all_child_items(EE_Line_Item $parent_line_item)
1042
-    {
1043
-        $deleted = 0;
1044
-        foreach ($parent_line_item->children() as $child_line_item) {
1045
-            if ($child_line_item instanceof EE_Line_Item) {
1046
-                $deleted += EEH_Line_Item::delete_all_child_items($child_line_item);
1047
-                if ($child_line_item->ID()) {
1048
-                    $child_line_item->delete();
1049
-                    unset($child_line_item);
1050
-                } else {
1051
-                    $parent_line_item->delete_child_line_item($child_line_item->code());
1052
-                }
1053
-                $deleted++;
1054
-            }
1055
-        }
1056
-        return $deleted;
1057
-    }
1058
-
1059
-
1060
-    /**
1061
-     * Deletes the line items as indicated by the line item code(s) provided,
1062
-     * regardless of where they're found in the line item tree. Automatically
1063
-     * re-calculates the line item totals and updates the related transaction. But
1064
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
1065
-     * should probably change because of this).
1066
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
1067
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
1068
-     *
1069
-     * @param EE_Line_Item      $total_line_item of type EEM_Line_Item::type_total
1070
-     * @param array|bool|string $line_item_codes
1071
-     * @return int number of items successfully removed
1072
-     * @throws EE_Error
1073
-     */
1074
-    public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false)
1075
-    {
1076
-
1077
-        if ($total_line_item->type() !== EEM_Line_Item::type_total) {
1078
-            EE_Error::doing_it_wrong(
1079
-                'EEH_Line_Item::delete_items',
1080
-                esc_html__(
1081
-                    'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly',
1082
-                    'event_espresso'
1083
-                ),
1084
-                '4.6.18'
1085
-            );
1086
-        }
1087
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1088
-
1089
-        // check if only a single line_item_id was passed
1090
-        if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1091
-            // place single line_item_id in an array to appear as multiple line_item_ids
1092
-            $line_item_codes = array($line_item_codes);
1093
-        }
1094
-        $removals = 0;
1095
-        // cycle thru line_item_ids
1096
-        foreach ($line_item_codes as $line_item_id) {
1097
-            $removals += $total_line_item->delete_child_line_item($line_item_id);
1098
-        }
1099
-
1100
-        if ($removals > 0) {
1101
-            $total_line_item->recalculate_taxes_and_tax_total();
1102
-            return $removals;
1103
-        } else {
1104
-            return false;
1105
-        }
1106
-    }
1107
-
1108
-
1109
-    /**
1110
-     * Overwrites the previous tax by clearing out the old taxes, and creates a new
1111
-     * tax and updates the total line item accordingly
1112
-     *
1113
-     * @param EE_Line_Item $total_line_item
1114
-     * @param float        $amount
1115
-     * @param string       $name
1116
-     * @param string       $description
1117
-     * @param string       $code
1118
-     * @param boolean      $add_to_existing_line_item
1119
-     *                          if true, and a duplicate line item with the same code is found,
1120
-     *                          $amount will be added onto it; otherwise will simply set the taxes to match $amount
1121
-     * @return EE_Line_Item the new tax line item created
1122
-     * @throws EE_Error
1123
-     * @throws InvalidArgumentException
1124
-     * @throws InvalidDataTypeException
1125
-     * @throws InvalidInterfaceException
1126
-     * @throws ReflectionException
1127
-     */
1128
-    public static function set_total_tax_to(
1129
-        EE_Line_Item $total_line_item,
1130
-        $amount,
1131
-        $name = null,
1132
-        $description = null,
1133
-        $code = null,
1134
-        $add_to_existing_line_item = false
1135
-    ) {
1136
-        $tax_subtotal = self::get_taxes_subtotal($total_line_item);
1137
-        $taxable_total = $total_line_item->taxable_total();
1138
-
1139
-        if ($add_to_existing_line_item) {
1140
-            $new_tax = $tax_subtotal->get_child_line_item($code);
1141
-            EEM_Line_Item::instance()->delete(
1142
-                array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID()))
1143
-            );
1144
-        } else {
1145
-            $new_tax = null;
1146
-            $tax_subtotal->delete_children_line_items();
1147
-        }
1148
-        if ($new_tax) {
1149
-            $new_tax->set_total($new_tax->total() + $amount);
1150
-            $new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0);
1151
-        } else {
1152
-            // no existing tax item. Create it
1153
-            $new_tax = EE_Line_Item::new_instance(array(
1154
-                'TXN_ID'      => $total_line_item->TXN_ID(),
1155
-                'LIN_name'    => $name ? $name : esc_html__('Tax', 'event_espresso'),
1156
-                'LIN_desc'    => $description ? $description : '',
1157
-                'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0,
1158
-                'LIN_total'   => $amount,
1159
-                'LIN_parent'  => $tax_subtotal->ID(),
1160
-                'LIN_type'    => EEM_Line_Item::type_tax,
1161
-                'LIN_code'    => $code,
1162
-            ));
1163
-        }
1164
-
1165
-        $new_tax = apply_filters(
1166
-            'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal',
1167
-            $new_tax,
1168
-            $total_line_item
1169
-        );
1170
-        $new_tax->save();
1171
-        $tax_subtotal->set_total($new_tax->total());
1172
-        $tax_subtotal->save();
1173
-        $total_line_item->recalculate_total_including_taxes();
1174
-        return $new_tax;
1175
-    }
1176
-
1177
-
1178
-    /**
1179
-     * Makes all the line items which are children of $line_item taxable (or not).
1180
-     * Does NOT save the line items
1181
-     *
1182
-     * @param EE_Line_Item $line_item
1183
-     * @param boolean      $taxable
1184
-     * @param string       $code_substring_for_whitelist if this string is part of the line item's code
1185
-     *                                                   it will be whitelisted (ie, except from becoming taxable)
1186
-     * @throws EE_Error
1187
-     */
1188
-    public static function set_line_items_taxable(
1189
-        EE_Line_Item $line_item,
1190
-        $taxable = true,
1191
-        $code_substring_for_whitelist = null
1192
-    ) {
1193
-        $whitelisted = false;
1194
-        if ($code_substring_for_whitelist !== null) {
1195
-            $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1196
-        }
1197
-        if (! $whitelisted && $line_item->is_line_item()) {
1198
-            $line_item->set_is_taxable($taxable);
1199
-        }
1200
-        foreach ($line_item->children() as $child_line_item) {
1201
-            EEH_Line_Item::set_line_items_taxable(
1202
-                $child_line_item,
1203
-                $taxable,
1204
-                $code_substring_for_whitelist
1205
-            );
1206
-        }
1207
-    }
1208
-
1209
-
1210
-    /**
1211
-     * Gets all descendants that are event subtotals
1212
-     *
1213
-     * @uses  EEH_Line_Item::get_subtotals_of_object_type()
1214
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1215
-     * @return EE_Line_Item[]
1216
-     * @throws EE_Error
1217
-     */
1218
-    public static function get_event_subtotals(EE_Line_Item $parent_line_item)
1219
-    {
1220
-        return self::get_subtotals_of_object_type($parent_line_item, EEM_Line_Item::OBJ_TYPE_EVENT);
1221
-    }
1222
-
1223
-
1224
-    /**
1225
-     * Gets all descendants subtotals that match the supplied object type
1226
-     *
1227
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1228
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1229
-     * @param string       $obj_type
1230
-     * @return EE_Line_Item[]
1231
-     * @throws EE_Error
1232
-     */
1233
-    public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1234
-    {
1235
-        return self::_get_descendants_by_type_and_object_type(
1236
-            $parent_line_item,
1237
-            EEM_Line_Item::type_sub_total,
1238
-            $obj_type
1239
-        );
1240
-    }
1241
-
1242
-
1243
-    /**
1244
-     * Gets all descendants that are tickets
1245
-     *
1246
-     * @uses  EEH_Line_Item::get_line_items_of_object_type()
1247
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1248
-     * @return EE_Line_Item[]
1249
-     * @throws EE_Error
1250
-     */
1251
-    public static function get_ticket_line_items(EE_Line_Item $parent_line_item)
1252
-    {
1253
-        return self::get_line_items_of_object_type(
1254
-            $parent_line_item,
1255
-            EEM_Line_Item::OBJ_TYPE_TICKET
1256
-        );
1257
-    }
1258
-
1259
-
1260
-    /**
1261
-     * Gets all descendants subtotals that match the supplied object type
1262
-     *
1263
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1264
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1265
-     * @param string       $obj_type
1266
-     * @return EE_Line_Item[]
1267
-     * @throws EE_Error
1268
-     */
1269
-    public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1270
-    {
1271
-        return self::_get_descendants_by_type_and_object_type(
1272
-            $parent_line_item,
1273
-            EEM_Line_Item::type_line_item,
1274
-            $obj_type
1275
-        );
1276
-    }
1277
-
1278
-
1279
-    /**
1280
-     * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax'
1281
-     *
1282
-     * @uses  EEH_Line_Item::get_descendants_of_type()
1283
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1284
-     * @return EE_Line_Item[]
1285
-     * @throws EE_Error
1286
-     */
1287
-    public static function get_tax_descendants(EE_Line_Item $parent_line_item)
1288
-    {
1289
-        return EEH_Line_Item::get_descendants_of_type(
1290
-            $parent_line_item,
1291
-            EEM_Line_Item::type_tax
1292
-        );
1293
-    }
1294
-
1295
-
1296
-    /**
1297
-     * Gets all the real items purchased which are children of this item
1298
-     *
1299
-     * @uses  EEH_Line_Item::get_descendants_of_type()
1300
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1301
-     * @return EE_Line_Item[]
1302
-     * @throws EE_Error
1303
-     */
1304
-    public static function get_line_item_descendants(EE_Line_Item $parent_line_item)
1305
-    {
1306
-        return EEH_Line_Item::get_descendants_of_type(
1307
-            $parent_line_item,
1308
-            EEM_Line_Item::type_line_item
1309
-        );
1310
-    }
1311
-
1312
-
1313
-    /**
1314
-     * Gets all descendants of supplied line item that match the supplied line item type
1315
-     *
1316
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1317
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1318
-     * @param string       $line_item_type   one of the EEM_Line_Item constants
1319
-     * @return EE_Line_Item[]
1320
-     * @throws EE_Error
1321
-     */
1322
-    public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type)
1323
-    {
1324
-        return self::_get_descendants_by_type_and_object_type(
1325
-            $parent_line_item,
1326
-            $line_item_type,
1327
-            null
1328
-        );
1329
-    }
1330
-
1331
-
1332
-    /**
1333
-     * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1334
-     * as well
1335
-     *
1336
-     * @param EE_Line_Item  $parent_line_item - the line item to find descendants of
1337
-     * @param string        $line_item_type   one of the EEM_Line_Item constants
1338
-     * @param string | NULL $obj_type         object model class name (minus prefix) or NULL to ignore object type when
1339
-     *                                        searching
1340
-     * @return EE_Line_Item[]
1341
-     * @throws EE_Error
1342
-     */
1343
-    protected static function _get_descendants_by_type_and_object_type(
1344
-        EE_Line_Item $parent_line_item,
1345
-        $line_item_type,
1346
-        $obj_type = null
1347
-    ) {
1348
-        $objects = array();
1349
-        foreach ($parent_line_item->children() as $child_line_item) {
1350
-            if ($child_line_item instanceof EE_Line_Item) {
1351
-                if ($child_line_item->type() === $line_item_type
1352
-                    && (
1353
-                        $child_line_item->OBJ_type() === $obj_type || $obj_type === null
1354
-                    )
1355
-                ) {
1356
-                    $objects[] = $child_line_item;
1357
-                } else {
1358
-                    // go-through-all-its children looking for more matches
1359
-                    $objects = array_merge(
1360
-                        $objects,
1361
-                        self::_get_descendants_by_type_and_object_type(
1362
-                            $child_line_item,
1363
-                            $line_item_type,
1364
-                            $obj_type
1365
-                        )
1366
-                    );
1367
-                }
1368
-            }
1369
-        }
1370
-        return $objects;
1371
-    }
1372
-
1373
-
1374
-    /**
1375
-     * Gets all descendants subtotals that match the supplied object type
1376
-     *
1377
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1378
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1379
-     * @param string       $OBJ_type         object type (like Event)
1380
-     * @param array        $OBJ_IDs          array of OBJ_IDs
1381
-     * @return EE_Line_Item[]
1382
-     * @throws EE_Error
1383
-     */
1384
-    public static function get_line_items_by_object_type_and_IDs(
1385
-        EE_Line_Item $parent_line_item,
1386
-        $OBJ_type = '',
1387
-        $OBJ_IDs = array()
1388
-    ) {
1389
-        return self::_get_descendants_by_object_type_and_object_ID(
1390
-            $parent_line_item,
1391
-            $OBJ_type,
1392
-            $OBJ_IDs
1393
-        );
1394
-    }
1395
-
1396
-
1397
-    /**
1398
-     * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1399
-     * as well
1400
-     *
1401
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1402
-     * @param string       $OBJ_type         object type (like Event)
1403
-     * @param array        $OBJ_IDs          array of OBJ_IDs
1404
-     * @return EE_Line_Item[]
1405
-     * @throws EE_Error
1406
-     */
1407
-    protected static function _get_descendants_by_object_type_and_object_ID(
1408
-        EE_Line_Item $parent_line_item,
1409
-        $OBJ_type,
1410
-        $OBJ_IDs
1411
-    ) {
1412
-        $objects = array();
1413
-        foreach ($parent_line_item->children() as $child_line_item) {
1414
-            if ($child_line_item instanceof EE_Line_Item) {
1415
-                if ($child_line_item->OBJ_type() === $OBJ_type
1416
-                    && is_array($OBJ_IDs)
1417
-                    && in_array($child_line_item->OBJ_ID(), $OBJ_IDs)
1418
-                ) {
1419
-                    $objects[] = $child_line_item;
1420
-                } else {
1421
-                    // go-through-all-its children looking for more matches
1422
-                    $objects = array_merge(
1423
-                        $objects,
1424
-                        self::_get_descendants_by_object_type_and_object_ID(
1425
-                            $child_line_item,
1426
-                            $OBJ_type,
1427
-                            $OBJ_IDs
1428
-                        )
1429
-                    );
1430
-                }
1431
-            }
1432
-        }
1433
-        return $objects;
1434
-    }
1435
-
1436
-
1437
-    /**
1438
-     * Uses a breadth-first-search in order to find the nearest descendant of
1439
-     * the specified type and returns it, else NULL
1440
-     *
1441
-     * @uses  EEH_Line_Item::_get_nearest_descendant()
1442
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1443
-     * @param string       $type             like one of the EEM_Line_Item::type_*
1444
-     * @return EE_Line_Item
1445
-     * @throws EE_Error
1446
-     * @throws InvalidArgumentException
1447
-     * @throws InvalidDataTypeException
1448
-     * @throws InvalidInterfaceException
1449
-     * @throws ReflectionException
1450
-     */
1451
-    public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type)
1452
-    {
1453
-        return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type);
1454
-    }
1455
-
1456
-
1457
-    /**
1458
-     * Uses a breadth-first-search in order to find the nearest descendant
1459
-     * having the specified LIN_code and returns it, else NULL
1460
-     *
1461
-     * @uses  EEH_Line_Item::_get_nearest_descendant()
1462
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1463
-     * @param string       $code             any value used for LIN_code
1464
-     * @return EE_Line_Item
1465
-     * @throws EE_Error
1466
-     * @throws InvalidArgumentException
1467
-     * @throws InvalidDataTypeException
1468
-     * @throws InvalidInterfaceException
1469
-     * @throws ReflectionException
1470
-     */
1471
-    public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code)
1472
-    {
1473
-        return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code);
1474
-    }
1475
-
1476
-
1477
-    /**
1478
-     * Uses a breadth-first-search in order to find the nearest descendant
1479
-     * having the specified LIN_code and returns it, else NULL
1480
-     *
1481
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1482
-     * @param string       $search_field     name of EE_Line_Item property
1483
-     * @param string       $value            any value stored in $search_field
1484
-     * @return EE_Line_Item
1485
-     * @throws EE_Error
1486
-     * @throws InvalidArgumentException
1487
-     * @throws InvalidDataTypeException
1488
-     * @throws InvalidInterfaceException
1489
-     * @throws ReflectionException
1490
-     */
1491
-    protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value)
1492
-    {
1493
-        foreach ($parent_line_item->children() as $child) {
1494
-            if ($child->get($search_field) == $value) {
1495
-                return $child;
1496
-            }
1497
-        }
1498
-        foreach ($parent_line_item->children() as $child) {
1499
-            $descendant_found = self::_get_nearest_descendant(
1500
-                $child,
1501
-                $search_field,
1502
-                $value
1503
-            );
1504
-            if ($descendant_found) {
1505
-                return $descendant_found;
1506
-            }
1507
-        }
1508
-        return null;
1509
-    }
1510
-
1511
-
1512
-    /**
1513
-     * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction,
1514
-     * else recursively walks up the line item tree until a parent of type total is found,
1515
-     *
1516
-     * @param EE_Line_Item $line_item
1517
-     * @return EE_Line_Item
1518
-     * @throws EE_Error
1519
-     */
1520
-    public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item)
1521
-    {
1522
-        if ($line_item->TXN_ID()) {
1523
-            $total_line_item = $line_item->transaction()->total_line_item(false);
1524
-            if ($total_line_item instanceof EE_Line_Item) {
1525
-                return $total_line_item;
1526
-            }
1527
-        } else {
1528
-            $line_item_parent = $line_item->parent();
1529
-            if ($line_item_parent instanceof EE_Line_Item) {
1530
-                if ($line_item_parent->is_total()) {
1531
-                    return $line_item_parent;
1532
-                }
1533
-                return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent);
1534
-            }
1535
-        }
1536
-        throw new EE_Error(
1537
-            sprintf(
1538
-                esc_html__(
1539
-                    'A valid grand total for line item %1$d was not found.',
1540
-                    'event_espresso'
1541
-                ),
1542
-                $line_item->ID()
1543
-            )
1544
-        );
1545
-    }
1546
-
1547
-
1548
-    /**
1549
-     * Prints out a representation of the line item tree
1550
-     *
1551
-     * @param EE_Line_Item $line_item
1552
-     * @param int          $indentation
1553
-     * @return void
1554
-     * @throws EE_Error
1555
-     */
1556
-    public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1557
-    {
1558
-        echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559
-        if (! $indentation) {
1560
-            echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1561
-        }
1562
-        for ($i = 0; $i < $indentation; $i++) {
1563
-            echo '. ';
1564
-        }
1565
-        $breakdown = '';
1566
-        if ($line_item->is_line_item()) {
1567
-            if ($line_item->is_percent()) {
1568
-                $breakdown = "{$line_item->percent()}%";
1569
-            } else {
1570
-                $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1571
-            }
1572
-        }
1573
-        echo $line_item->name();
1574
-        echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1575
-        echo '$' . (string) $line_item->total();
1576
-        if ($breakdown) {
1577
-            echo " ( {$breakdown} )";
1578
-        }
1579
-        if ($line_item->is_taxable()) {
1580
-            echo '  * taxable';
1581
-        }
1582
-        if ($line_item->children()) {
1583
-            foreach ($line_item->children() as $child) {
1584
-                self::visualize($child, $indentation + 1);
1585
-            }
1586
-        }
1587
-    }
1588
-
1589
-
1590
-    /**
1591
-     * Calculates the registration's final price, taking into account that they
1592
-     * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes,
1593
-     * and receive a portion of any transaction-wide discounts.
1594
-     * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount
1595
-     * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get
1596
-     * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50
1597
-     * and brent's final price should be $5.50.
1598
-     * In order to do this, we basically need to traverse the line item tree calculating
1599
-     * the running totals (just as if we were recalculating the total), but when we identify
1600
-     * regular line items, we need to keep track of their share of the grand total.
1601
-     * Also, we need to keep track of the TAXABLE total for each ticket purchase, so
1602
-     * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total"
1603
-     * when there are non-taxable items; otherwise they would be the same)
1604
-     *
1605
-     * @param EE_Line_Item $line_item
1606
-     * @param array        $billable_ticket_quantities  array of EE_Ticket IDs and their corresponding quantity that
1607
-     *                                                  can be included in price calculations at this moment
1608
-     * @return array        keys are line items for tickets IDs and values are their share of the running total,
1609
-     *                                                  plus the key 'total', and 'taxable' which also has keys of all
1610
-     *                                                  the ticket IDs.
1611
-     *                                                  Eg array(
1612
-     *                                                      12 => 4.3
1613
-     *                                                      23 => 8.0
1614
-     *                                                      'total' => 16.6,
1615
-     *                                                      'taxable' => array(
1616
-     *                                                          12 => 10,
1617
-     *                                                          23 => 4
1618
-     *                                                      ).
1619
-     *                                                  So to find which registrations have which final price, we need
1620
-     *                                                  to find which line item is theirs, which can be done with
1621
-     *                                                  `EEM_Line_Item::instance()->get_line_item_for_registration(
1622
-     *                                                  $registration );`
1623
-     * @throws EE_Error
1624
-     * @throws InvalidArgumentException
1625
-     * @throws InvalidDataTypeException
1626
-     * @throws InvalidInterfaceException
1627
-     * @throws ReflectionException
1628
-     */
1629
-    public static function calculate_reg_final_prices_per_line_item(
1630
-        EE_Line_Item $line_item,
1631
-        $billable_ticket_quantities = array()
1632
-    ) {
1633
-        $running_totals = [
1634
-            'total'   => 0,
1635
-            'taxable' => ['total' => 0]
1636
-        ];
1637
-        foreach ($line_item->children() as $child_line_item) {
1638
-            switch ($child_line_item->type()) {
1639
-                case EEM_Line_Item::type_sub_total:
1640
-                    $running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1641
-                        $child_line_item,
1642
-                        $billable_ticket_quantities
1643
-                    );
1644
-                    // combine arrays but preserve numeric keys
1645
-                    $running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals);
1646
-                    $running_totals['total'] += $running_totals_from_subtotal['total'];
1647
-                    $running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total'];
1648
-                    break;
1649
-
1650
-                case EEM_Line_Item::type_tax_sub_total:
1651
-                    // find how much the taxes percentage is
1652
-                    if ($child_line_item->percent() !== 0) {
1653
-                        $tax_percent_decimal = $child_line_item->percent() / 100;
1654
-                    } else {
1655
-                        $tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100;
1656
-                    }
1657
-                    // and apply to all the taxable totals, and add to the pretax totals
1658
-                    foreach ($running_totals as $line_item_id => $this_running_total) {
1659
-                        // "total" and "taxable" array key is an exception
1660
-                        if ($line_item_id === 'taxable') {
1661
-                            continue;
1662
-                        }
1663
-                        $taxable_total = $running_totals['taxable'][ $line_item_id ];
1664
-                        $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1665
-                    }
1666
-                    break;
1667
-
1668
-                case EEM_Line_Item::type_line_item:
1669
-                    // ticket line items or ????
1670
-                    if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1671
-                        // kk it's a ticket
1672
-                        if (isset($running_totals[ $child_line_item->ID() ])) {
1673
-                            // huh? that shouldn't happen.
1674
-                            $running_totals['total'] += $child_line_item->total();
1675
-                        } else {
1676
-                            // its not in our running totals yet. great.
1677
-                            if ($child_line_item->is_taxable()) {
1678
-                                $taxable_amount = $child_line_item->unit_price();
1679
-                            } else {
1680
-                                $taxable_amount = 0;
1681
-                            }
1682
-                            // are we only calculating totals for some tickets?
1683
-                            if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1684
-                                $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1685
-                                $running_totals[ $child_line_item->ID() ] = $quantity
1686
-                                    ? $child_line_item->unit_price()
1687
-                                    : 0;
1688
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1689
-                                    ? $taxable_amount
1690
-                                    : 0;
1691
-                            } else {
1692
-                                $quantity = $child_line_item->quantity();
1693
-                                $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1694
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1695
-                            }
1696
-                            $running_totals['taxable']['total'] += $taxable_amount * $quantity;
1697
-                            $running_totals['total'] += $child_line_item->unit_price() * $quantity;
1698
-                        }
1699
-                    } else {
1700
-                        // it's some other type of item added to the cart
1701
-                        // it should affect the running totals
1702
-                        // basically we want to convert it into a PERCENT modifier. Because
1703
-                        // more clearly affect all registration's final price equally
1704
-                        $line_items_percent_of_running_total = $running_totals['total'] > 0
1705
-                            ? ($child_line_item->total() / $running_totals['total']) + 1
1706
-                            : 1;
1707
-                        foreach ($running_totals as $line_item_id => $this_running_total) {
1708
-                            // the "taxable" array key is an exception
1709
-                            if ($line_item_id === 'taxable') {
1710
-                                continue;
1711
-                            }
1712
-                            // update the running totals
1713
-                            // yes this actually even works for the running grand total!
1714
-                            $running_totals[ $line_item_id ] =
1715
-                                $line_items_percent_of_running_total * $this_running_total;
1716
-
1717
-                            if ($child_line_item->is_taxable()) {
1718
-                                $running_totals['taxable'][ $line_item_id ] =
1719
-                                    $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1720
-                            }
1721
-                        }
1722
-                    }
1723
-                    break;
1724
-            }
1725
-        }
1726
-        return $running_totals;
1727
-    }
1728
-
1729
-
1730
-    /**
1731
-     * @param EE_Line_Item $total_line_item
1732
-     * @param EE_Line_Item $ticket_line_item
1733
-     * @return float | null
1734
-     * @throws EE_Error
1735
-     * @throws InvalidArgumentException
1736
-     * @throws InvalidDataTypeException
1737
-     * @throws InvalidInterfaceException
1738
-     * @throws OutOfRangeException
1739
-     * @throws ReflectionException
1740
-     */
1741
-    public static function calculate_final_price_for_ticket_line_item(
1742
-        EE_Line_Item $total_line_item,
1743
-        EE_Line_Item $ticket_line_item
1744
-    ) {
1745
-        static $final_prices_per_ticket_line_item = array();
1746
-        if (empty($final_prices_per_ticket_line_item)) {
1747
-            $final_prices_per_ticket_line_item = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1748
-                $total_line_item
1749
-            );
1750
-        }
1751
-        // ok now find this new registration's final price
1752
-        if (isset($final_prices_per_ticket_line_item[ $ticket_line_item->ID() ])) {
1753
-            return $final_prices_per_ticket_line_item[ $ticket_line_item->ID() ];
1754
-        }
1755
-        $message = sprintf(
1756
-            esc_html__(
1757
-                'The final price for the ticket line item (ID:%1$d) could not be calculated.',
1758
-                'event_espresso'
1759
-            ),
1760
-            $ticket_line_item->ID()
1761
-        );
1762
-        if (WP_DEBUG) {
1763
-            $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1764
-            throw new OutOfRangeException($message);
1765
-        }
1766
-        EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
1767
-        return null;
1768
-    }
1769
-
1770
-
1771
-    /**
1772
-     * Creates a duplicate of the line item tree, except only includes billable items
1773
-     * and the portion of line items attributed to billable things
1774
-     *
1775
-     * @param EE_Line_Item      $line_item
1776
-     * @param EE_Registration[] $registrations
1777
-     * @return EE_Line_Item
1778
-     * @throws EE_Error
1779
-     * @throws InvalidArgumentException
1780
-     * @throws InvalidDataTypeException
1781
-     * @throws InvalidInterfaceException
1782
-     * @throws ReflectionException
1783
-     */
1784
-    public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations)
1785
-    {
1786
-        $copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations);
1787
-        foreach ($line_item->children() as $child_li) {
1788
-            $copy_li->add_child_line_item(
1789
-                EEH_Line_Item::billable_line_item_tree($child_li, $registrations)
1790
-            );
1791
-        }
1792
-        // if this is the grand total line item, make sure the totals all add up
1793
-        // (we could have duplicated this logic AS we copied the line items, but
1794
-        // it seems DRYer this way)
1795
-        if ($copy_li->type() === EEM_Line_Item::type_total) {
1796
-            $copy_li->recalculate_total_including_taxes();
1797
-        }
1798
-        return $copy_li;
1799
-    }
1800
-
1801
-
1802
-    /**
1803
-     * Creates a new, unsaved line item from $line_item that factors in the
1804
-     * number of billable registrations on $registrations.
1805
-     *
1806
-     * @param EE_Line_Item      $line_item
1807
-     * @param EE_Registration[] $registrations
1808
-     * @return EE_Line_Item
1809
-     * @throws EE_Error
1810
-     * @throws InvalidArgumentException
1811
-     * @throws InvalidDataTypeException
1812
-     * @throws InvalidInterfaceException
1813
-     * @throws ReflectionException
1814
-     */
1815
-    public static function billable_line_item(EE_Line_Item $line_item, $registrations)
1816
-    {
1817
-        $new_li_fields = $line_item->model_field_array();
1818
-        if ($line_item->type() === EEM_Line_Item::type_line_item &&
1819
-            $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1820
-        ) {
1821
-            $count = 0;
1822
-            foreach ($registrations as $registration) {
1823
-                if ($line_item->OBJ_ID() === $registration->ticket_ID() &&
1824
-                    in_array(
1825
-                        $registration->status_ID(),
1826
-                        EEM_Registration::reg_statuses_that_allow_payment(),
1827
-                        true
1828
-                    )
1829
-                ) {
1830
-                    $count++;
1831
-                }
1832
-            }
1833
-            $new_li_fields['LIN_quantity'] = $count;
1834
-        }
1835
-        // don't set the total. We'll leave that up to the code that calculates it
1836
-        unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']);
1837
-        return EE_Line_Item::new_instance($new_li_fields);
1838
-    }
1839
-
1840
-
1841
-    /**
1842
-     * Returns a modified line item tree where all the subtotals which have a total of 0
1843
-     * are removed, and line items with a quantity of 0
1844
-     *
1845
-     * @param EE_Line_Item $line_item |null
1846
-     * @return EE_Line_Item|null
1847
-     * @throws EE_Error
1848
-     * @throws InvalidArgumentException
1849
-     * @throws InvalidDataTypeException
1850
-     * @throws InvalidInterfaceException
1851
-     * @throws ReflectionException
1852
-     */
1853
-    public static function non_empty_line_items(EE_Line_Item $line_item)
1854
-    {
1855
-        $copied_li = EEH_Line_Item::non_empty_line_item($line_item);
1856
-        if ($copied_li === null) {
1857
-            return null;
1858
-        }
1859
-        // if this is an event subtotal, we want to only include it if it
1860
-        // has a non-zero total and at least one ticket line item child
1861
-        $ticket_children = 0;
1862
-        foreach ($line_item->children() as $child_li) {
1863
-            $child_li_copy = EEH_Line_Item::non_empty_line_items($child_li);
1864
-            if ($child_li_copy !== null) {
1865
-                $copied_li->add_child_line_item($child_li_copy);
1866
-                if ($child_li_copy->type() === EEM_Line_Item::type_line_item &&
1867
-                    $child_li_copy->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1868
-                ) {
1869
-                    $ticket_children++;
1870
-                }
1871
-            }
1872
-        }
1873
-        // if this is an event subtotal with NO ticket children
1874
-        // we basically want to ignore it
1875
-        if ($ticket_children === 0
1876
-            && $line_item->type() === EEM_Line_Item::type_sub_total
1877
-            && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
1878
-            && $line_item->total() === 0
1879
-        ) {
1880
-            return null;
1881
-        }
1882
-        return $copied_li;
1883
-    }
1884
-
1885
-
1886
-    /**
1887
-     * Creates a new, unsaved line item, but if it's a ticket line item
1888
-     * with a total of 0, or a subtotal of 0, returns null instead
1889
-     *
1890
-     * @param EE_Line_Item $line_item
1891
-     * @return EE_Line_Item
1892
-     * @throws EE_Error
1893
-     * @throws InvalidArgumentException
1894
-     * @throws InvalidDataTypeException
1895
-     * @throws InvalidInterfaceException
1896
-     * @throws ReflectionException
1897
-     */
1898
-    public static function non_empty_line_item(EE_Line_Item $line_item)
1899
-    {
1900
-        if ($line_item->type() === EEM_Line_Item::type_line_item
1901
-            && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1902
-            && $line_item->quantity() === 0
1903
-        ) {
1904
-            return null;
1905
-        }
1906
-        $new_li_fields = $line_item->model_field_array();
1907
-        // don't set the total. We'll leave that up to the code that calculates it
1908
-        unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']);
1909
-        return EE_Line_Item::new_instance($new_li_fields);
1910
-    }
1911
-
1912
-
1913
-    /**
1914
-     * Cycles through all of the ticket line items for the supplied total line item
1915
-     * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket
1916
-     *
1917
-     * @param EE_Line_Item $total_line_item
1918
-     * @since 4.9.79.p
1919
-     * @throws EE_Error
1920
-     * @throws InvalidArgumentException
1921
-     * @throws InvalidDataTypeException
1922
-     * @throws InvalidInterfaceException
1923
-     * @throws ReflectionException
1924
-     */
1925
-    public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item)
1926
-    {
1927
-        $ticket_line_items = self::get_ticket_line_items($total_line_item);
1928
-        foreach ($ticket_line_items as $ticket_line_item) {
1929
-            if ($ticket_line_item instanceof EE_Line_Item
1930
-                && $ticket_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1931
-            ) {
1932
-                $ticket = $ticket_line_item->ticket();
1933
-                if ($ticket instanceof EE_Ticket && $ticket->taxable() !== $ticket_line_item->is_taxable()) {
1934
-                    $ticket_line_item->set_is_taxable($ticket->taxable());
1935
-                    $ticket_line_item->save();
1936
-                }
1937
-            }
1938
-        }
1939
-    }
1940
-
1941
-
1942
-
1943
-    /**************************************** @DEPRECATED METHODS *************************************** */
1944
-    /**
1945
-     * @deprecated
1946
-     * @param EE_Line_Item $total_line_item
1947
-     * @return EE_Line_Item
1948
-     * @throws EE_Error
1949
-     * @throws InvalidArgumentException
1950
-     * @throws InvalidDataTypeException
1951
-     * @throws InvalidInterfaceException
1952
-     * @throws ReflectionException
1953
-     */
1954
-    public static function get_items_subtotal(EE_Line_Item $total_line_item)
1955
-    {
1956
-        EE_Error::doing_it_wrong(
1957
-            'EEH_Line_Item::get_items_subtotal()',
1958
-            sprintf(
1959
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1960
-                'EEH_Line_Item::get_pre_tax_subtotal()'
1961
-            ),
1962
-            '4.6.0'
1963
-        );
1964
-        return self::get_pre_tax_subtotal($total_line_item);
1965
-    }
1966
-
1967
-
1968
-    /**
1969
-     * @deprecated
1970
-     * @param EE_Transaction $transaction
1971
-     * @return EE_Line_Item
1972
-     * @throws EE_Error
1973
-     * @throws InvalidArgumentException
1974
-     * @throws InvalidDataTypeException
1975
-     * @throws InvalidInterfaceException
1976
-     * @throws ReflectionException
1977
-     */
1978
-    public static function create_default_total_line_item($transaction = null)
1979
-    {
1980
-        EE_Error::doing_it_wrong(
1981
-            'EEH_Line_Item::create_default_total_line_item()',
1982
-            sprintf(
1983
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1984
-                'EEH_Line_Item::create_total_line_item()'
1985
-            ),
1986
-            '4.6.0'
1987
-        );
1988
-        return self::create_total_line_item($transaction);
1989
-    }
1990
-
1991
-
1992
-    /**
1993
-     * @deprecated
1994
-     * @param EE_Line_Item   $total_line_item
1995
-     * @param EE_Transaction $transaction
1996
-     * @return EE_Line_Item
1997
-     * @throws EE_Error
1998
-     * @throws InvalidArgumentException
1999
-     * @throws InvalidDataTypeException
2000
-     * @throws InvalidInterfaceException
2001
-     * @throws ReflectionException
2002
-     */
2003
-    public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2004
-    {
2005
-        EE_Error::doing_it_wrong(
2006
-            'EEH_Line_Item::create_default_tickets_subtotal()',
2007
-            sprintf(
2008
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2009
-                'EEH_Line_Item::create_pre_tax_subtotal()'
2010
-            ),
2011
-            '4.6.0'
2012
-        );
2013
-        return self::create_pre_tax_subtotal($total_line_item, $transaction);
2014
-    }
2015
-
2016
-
2017
-    /**
2018
-     * @deprecated
2019
-     * @param EE_Line_Item   $total_line_item
2020
-     * @param EE_Transaction $transaction
2021
-     * @return EE_Line_Item
2022
-     * @throws EE_Error
2023
-     * @throws InvalidArgumentException
2024
-     * @throws InvalidDataTypeException
2025
-     * @throws InvalidInterfaceException
2026
-     * @throws ReflectionException
2027
-     */
2028
-    public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2029
-    {
2030
-        EE_Error::doing_it_wrong(
2031
-            'EEH_Line_Item::create_default_taxes_subtotal()',
2032
-            sprintf(
2033
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2034
-                'EEH_Line_Item::create_taxes_subtotal()'
2035
-            ),
2036
-            '4.6.0'
2037
-        );
2038
-        return self::create_taxes_subtotal($total_line_item, $transaction);
2039
-    }
2040
-
2041
-
2042
-    /**
2043
-     * @deprecated
2044
-     * @param EE_Line_Item   $total_line_item
2045
-     * @param EE_Transaction $transaction
2046
-     * @return EE_Line_Item
2047
-     * @throws EE_Error
2048
-     * @throws InvalidArgumentException
2049
-     * @throws InvalidDataTypeException
2050
-     * @throws InvalidInterfaceException
2051
-     * @throws ReflectionException
2052
-     */
2053
-    public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2054
-    {
2055
-        EE_Error::doing_it_wrong(
2056
-            'EEH_Line_Item::create_default_event_subtotal()',
2057
-            sprintf(
2058
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2059
-                'EEH_Line_Item::create_event_subtotal()'
2060
-            ),
2061
-            '4.6.0'
2062
-        );
2063
-        return self::create_event_subtotal($total_line_item, $transaction);
2064
-    }
24
+	/**
25
+	 * Adds a simple item (unrelated to any other model object) to the provided PARENT line item.
26
+	 * Does NOT automatically re-calculate the line item totals or update the related transaction.
27
+	 * You should call recalculate_total_including_taxes() on the grant total line item after this
28
+	 * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
29
+	 * to keep the registration final prices in-sync with the transaction's total.
30
+	 *
31
+	 * @param EE_Line_Item $parent_line_item
32
+	 * @param string       $name
33
+	 * @param float        $unit_price
34
+	 * @param string       $description
35
+	 * @param int          $quantity
36
+	 * @param boolean      $taxable
37
+	 * @param boolean      $code if set to a value, ensures there is only one line item with that code
38
+	 * @return boolean success
39
+	 * @throws EE_Error
40
+	 * @throws InvalidArgumentException
41
+	 * @throws InvalidDataTypeException
42
+	 * @throws InvalidInterfaceException
43
+	 * @throws ReflectionException
44
+	 */
45
+	public static function add_unrelated_item(
46
+		EE_Line_Item $parent_line_item,
47
+		$name,
48
+		$unit_price,
49
+		$description = '',
50
+		$quantity = 1,
51
+		$taxable = false,
52
+		$code = null
53
+	) {
54
+		$items_subtotal = self::get_pre_tax_subtotal($parent_line_item);
55
+		$line_item = EE_Line_Item::new_instance(array(
56
+			'LIN_name'       => $name,
57
+			'LIN_desc'       => $description,
58
+			'LIN_unit_price' => $unit_price,
59
+			'LIN_quantity'   => $quantity,
60
+			'LIN_percent'    => null,
61
+			'LIN_is_taxable' => $taxable,
62
+			'LIN_order'      => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0,
63
+			'LIN_total'      => (float) $unit_price * (int) $quantity,
64
+			'LIN_type'       => EEM_Line_Item::type_line_item,
65
+			'LIN_code'       => $code,
66
+		));
67
+		$line_item = apply_filters(
68
+			'FHEE__EEH_Line_Item__add_unrelated_item__line_item',
69
+			$line_item,
70
+			$parent_line_item
71
+		);
72
+		return self::add_item($parent_line_item, $line_item);
73
+	}
74
+
75
+
76
+	/**
77
+	 * Adds a simple item ( unrelated to any other model object) to the total line item,
78
+	 * in the correct spot in the line item tree. Automatically
79
+	 * re-calculates the line item totals and updates the related transaction. But
80
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
81
+	 * should probably change because of this).
82
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
83
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
84
+	 *
85
+	 * @param EE_Line_Item $parent_line_item
86
+	 * @param string       $name
87
+	 * @param float        $percentage_amount
88
+	 * @param string       $description
89
+	 * @param boolean      $taxable
90
+	 * @return boolean success
91
+	 * @throws EE_Error
92
+	 */
93
+	public static function add_percentage_based_item(
94
+		EE_Line_Item $parent_line_item,
95
+		$name,
96
+		$percentage_amount,
97
+		$description = '',
98
+		$taxable = false
99
+	) {
100
+		$line_item = EE_Line_Item::new_instance(array(
101
+			'LIN_name'       => $name,
102
+			'LIN_desc'       => $description,
103
+			'LIN_unit_price' => 0,
104
+			'LIN_percent'    => $percentage_amount,
105
+			'LIN_quantity'   => 1,
106
+			'LIN_is_taxable' => $taxable,
107
+			'LIN_total'      => (float) ($percentage_amount * ($parent_line_item->total() / 100)),
108
+			'LIN_type'       => EEM_Line_Item::type_line_item,
109
+			'LIN_parent'     => $parent_line_item->ID(),
110
+		));
111
+		$line_item = apply_filters(
112
+			'FHEE__EEH_Line_Item__add_percentage_based_item__line_item',
113
+			$line_item
114
+		);
115
+		return $parent_line_item->add_child_line_item($line_item, false);
116
+	}
117
+
118
+
119
+	/**
120
+	 * Returns the new line item created by adding a purchase of the ticket
121
+	 * ensures that ticket line item is saved, and that cart total has been recalculated.
122
+	 * If this ticket has already been purchased, just increments its count.
123
+	 * Automatically re-calculates the line item totals and updates the related transaction. But
124
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
125
+	 * should probably change because of this).
126
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
127
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
128
+	 *
129
+	 * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total
130
+	 * @param EE_Ticket    $ticket
131
+	 * @param int          $qty
132
+	 * @return EE_Line_Item
133
+	 * @throws EE_Error
134
+	 * @throws InvalidArgumentException
135
+	 * @throws InvalidDataTypeException
136
+	 * @throws InvalidInterfaceException
137
+	 * @throws ReflectionException
138
+	 */
139
+	public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140
+	{
141
+		if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142
+			throw new EE_Error(
143
+				sprintf(
144
+					esc_html__(
145
+						'A valid line item total is required in order to add tickets. A line item of type "%s" was passed.',
146
+						'event_espresso'
147
+					),
148
+					$ticket->ID(),
149
+					$total_line_item->ID()
150
+				)
151
+			);
152
+		}
153
+		// either increment the qty for an existing ticket
154
+		$line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155
+		// or add a new one
156
+		if (! $line_item instanceof EE_Line_Item) {
157
+			$line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158
+		}
159
+		$total_line_item->recalculate_total_including_taxes();
160
+		return $line_item;
161
+	}
162
+
163
+
164
+	/**
165
+	 * Returns the new line item created by adding a purchase of the ticket
166
+	 *
167
+	 * @param EE_Line_Item $total_line_item
168
+	 * @param EE_Ticket    $ticket
169
+	 * @param int          $qty
170
+	 * @return EE_Line_Item
171
+	 * @throws EE_Error
172
+	 * @throws InvalidArgumentException
173
+	 * @throws InvalidDataTypeException
174
+	 * @throws InvalidInterfaceException
175
+	 * @throws ReflectionException
176
+	 */
177
+	public static function increment_ticket_qty_if_already_in_cart(
178
+		EE_Line_Item $total_line_item,
179
+		EE_Ticket $ticket,
180
+		$qty = 1
181
+	) {
182
+		$line_item = null;
183
+		if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) {
184
+			$ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
185
+			foreach ((array) $ticket_line_items as $ticket_line_item) {
186
+				if ($ticket_line_item instanceof EE_Line_Item
187
+					&& (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID()
188
+				) {
189
+					$line_item = $ticket_line_item;
190
+					break;
191
+				}
192
+			}
193
+		}
194
+		if ($line_item instanceof EE_Line_Item) {
195
+			EEH_Line_Item::increment_quantity($line_item, $qty);
196
+			return $line_item;
197
+		}
198
+		return null;
199
+	}
200
+
201
+
202
+	/**
203
+	 * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected).
204
+	 * Does NOT save or recalculate other line items totals
205
+	 *
206
+	 * @param EE_Line_Item $line_item
207
+	 * @param int          $qty
208
+	 * @return void
209
+	 * @throws EE_Error
210
+	 * @throws InvalidArgumentException
211
+	 * @throws InvalidDataTypeException
212
+	 * @throws InvalidInterfaceException
213
+	 * @throws ReflectionException
214
+	 */
215
+	public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216
+	{
217
+		if (! $line_item->is_percent()) {
218
+			$qty += $line_item->quantity();
219
+			$line_item->set_quantity($qty);
220
+			$line_item->set_total($line_item->unit_price() * $qty);
221
+			$line_item->save();
222
+		}
223
+		foreach ($line_item->children() as $child) {
224
+			if ($child->is_sub_line_item()) {
225
+				EEH_Line_Item::update_quantity($child, $qty);
226
+			}
227
+		}
228
+	}
229
+
230
+
231
+	/**
232
+	 * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected).
233
+	 * Does NOT save or recalculate other line items totals
234
+	 *
235
+	 * @param EE_Line_Item $line_item
236
+	 * @param int          $qty
237
+	 * @return void
238
+	 * @throws EE_Error
239
+	 * @throws InvalidArgumentException
240
+	 * @throws InvalidDataTypeException
241
+	 * @throws InvalidInterfaceException
242
+	 * @throws ReflectionException
243
+	 */
244
+	public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245
+	{
246
+		if (! $line_item->is_percent()) {
247
+			$qty = $line_item->quantity() - $qty;
248
+			$qty = max($qty, 0);
249
+			$line_item->set_quantity($qty);
250
+			$line_item->set_total($line_item->unit_price() * $qty);
251
+			$line_item->save();
252
+		}
253
+		foreach ($line_item->children() as $child) {
254
+			if ($child->is_sub_line_item()) {
255
+				EEH_Line_Item::update_quantity($child, $qty);
256
+			}
257
+		}
258
+	}
259
+
260
+
261
+	/**
262
+	 * Updates the line item and its children's quantities to the specified number.
263
+	 * Does NOT save them or recalculate totals.
264
+	 *
265
+	 * @param EE_Line_Item $line_item
266
+	 * @param int          $new_quantity
267
+	 * @throws EE_Error
268
+	 * @throws InvalidArgumentException
269
+	 * @throws InvalidDataTypeException
270
+	 * @throws InvalidInterfaceException
271
+	 * @throws ReflectionException
272
+	 */
273
+	public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274
+	{
275
+		if (! $line_item->is_percent()) {
276
+			$line_item->set_quantity($new_quantity);
277
+			$line_item->set_total($line_item->unit_price() * $new_quantity);
278
+			$line_item->save();
279
+		}
280
+		foreach ($line_item->children() as $child) {
281
+			if ($child->is_sub_line_item()) {
282
+				EEH_Line_Item::update_quantity($child, $new_quantity);
283
+			}
284
+		}
285
+	}
286
+
287
+
288
+	/**
289
+	 * Returns the new line item created by adding a purchase of the ticket
290
+	 *
291
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
292
+	 * @param EE_Ticket    $ticket
293
+	 * @param int          $qty
294
+	 * @return EE_Line_Item
295
+	 * @throws EE_Error
296
+	 * @throws InvalidArgumentException
297
+	 * @throws InvalidDataTypeException
298
+	 * @throws InvalidInterfaceException
299
+	 * @throws ReflectionException
300
+	 */
301
+	public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
302
+	{
303
+		$datetimes = $ticket->datetimes();
304
+		$first_datetime = reset($datetimes);
305
+		$first_datetime_name = esc_html__('Event', 'event_espresso');
306
+		if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) {
307
+			$first_datetime_name = $first_datetime->event()->name();
308
+		}
309
+		$event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name);
310
+		// get event subtotal line
311
+		$events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket);
312
+		// add $ticket to cart
313
+		$line_item = EE_Line_Item::new_instance(array(
314
+			'LIN_name'       => $ticket->name(),
315
+			'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
316
+			'LIN_unit_price' => $ticket->price(),
317
+			'LIN_quantity'   => $qty,
318
+			'LIN_is_taxable' => $ticket->taxable(),
319
+			'LIN_order'      => count($events_sub_total->children()),
320
+			'LIN_total'      => $ticket->price() * $qty,
321
+			'LIN_type'       => EEM_Line_Item::type_line_item,
322
+			'OBJ_ID'         => $ticket->ID(),
323
+			'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_TICKET,
324
+		));
325
+		$line_item = apply_filters(
326
+			'FHEE__EEH_Line_Item__create_ticket_line_item__line_item',
327
+			$line_item
328
+		);
329
+		$events_sub_total->add_child_line_item($line_item);
330
+		// now add the sub-line items
331
+		$running_total_for_ticket = 0;
332
+		foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) {
333
+			$sign = $price->is_discount() ? -1 : 1;
334
+			$price_total = $price->is_percent()
335
+				? $running_total_for_ticket * $price->amount() / 100
336
+				: $price->amount() * $qty;
337
+			$sub_line_item = EE_Line_Item::new_instance(array(
338
+				'LIN_name'       => $price->name(),
339
+				'LIN_desc'       => $price->desc(),
340
+				'LIN_quantity'   => $price->is_percent() ? null : $qty,
341
+				'LIN_is_taxable' => false,
342
+				'LIN_order'      => $price->order(),
343
+				'LIN_total'      => $sign * $price_total,
344
+				'LIN_type'       => EEM_Line_Item::type_sub_line_item,
345
+				'OBJ_ID'         => $price->ID(),
346
+				'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
347
+			));
348
+			$sub_line_item = apply_filters(
349
+				'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item',
350
+				$sub_line_item
351
+			);
352
+			if ($price->is_percent()) {
353
+				$sub_line_item->set_percent($sign * $price->amount());
354
+			} else {
355
+				$sub_line_item->set_unit_price($sign * $price->amount());
356
+			}
357
+			$running_total_for_ticket += $price_total;
358
+			$line_item->add_child_line_item($sub_line_item);
359
+		}
360
+		return $line_item;
361
+	}
362
+
363
+
364
+	/**
365
+	 * Adds the specified item under the pre-tax-sub-total line item. Automatically
366
+	 * re-calculates the line item totals and updates the related transaction. But
367
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
368
+	 * should probably change because of this).
369
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
370
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
371
+	 *
372
+	 * @param EE_Line_Item $total_line_item
373
+	 * @param EE_Line_Item $item to be added
374
+	 * @return boolean
375
+	 * @throws EE_Error
376
+	 * @throws InvalidArgumentException
377
+	 * @throws InvalidDataTypeException
378
+	 * @throws InvalidInterfaceException
379
+	 * @throws ReflectionException
380
+	 */
381
+	public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item)
382
+	{
383
+		$pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item);
384
+		if ($pre_tax_subtotal instanceof EE_Line_Item) {
385
+			$success = $pre_tax_subtotal->add_child_line_item($item);
386
+		} else {
387
+			return false;
388
+		}
389
+		$total_line_item->recalculate_total_including_taxes();
390
+		return $success;
391
+	}
392
+
393
+
394
+	/**
395
+	 * cancels an existing ticket line item,
396
+	 * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item.
397
+	 * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
398
+	 *
399
+	 * @param EE_Line_Item $ticket_line_item
400
+	 * @param int          $qty
401
+	 * @return bool success
402
+	 * @throws EE_Error
403
+	 * @throws InvalidArgumentException
404
+	 * @throws InvalidDataTypeException
405
+	 * @throws InvalidInterfaceException
406
+	 * @throws ReflectionException
407
+	 */
408
+	public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
409
+	{
410
+		// validate incoming line_item
411
+		if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
412
+			throw new EE_Error(
413
+				sprintf(
414
+					esc_html__(
415
+						'The supplied line item must have an Object Type of "Ticket", not %1$s.',
416
+						'event_espresso'
417
+					),
418
+					$ticket_line_item->type()
419
+				)
420
+			);
421
+		}
422
+		if ($ticket_line_item->quantity() < $qty) {
423
+			throw new EE_Error(
424
+				sprintf(
425
+					esc_html__(
426
+						'Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.',
427
+						'event_espresso'
428
+					),
429
+					$qty,
430
+					$ticket_line_item->quantity()
431
+				)
432
+			);
433
+		}
434
+		// decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this
435
+		$ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty);
436
+		foreach ($ticket_line_item->children() as $child_line_item) {
437
+			if ($child_line_item->is_sub_line_item()
438
+				&& ! $child_line_item->is_percent()
439
+				&& ! $child_line_item->is_cancellation()
440
+			) {
441
+				$child_line_item->set_quantity($child_line_item->quantity() - $qty);
442
+			}
443
+		}
444
+		// get cancellation sub line item
445
+		$cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
446
+			$ticket_line_item,
447
+			EEM_Line_Item::type_cancellation
448
+		);
449
+		$cancellation_line_item = reset($cancellation_line_item);
450
+		// verify that this ticket was indeed previously cancelled
451
+		if ($cancellation_line_item instanceof EE_Line_Item) {
452
+			// increment cancelled quantity
453
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty);
454
+		} else {
455
+			// create cancellation sub line item
456
+			$cancellation_line_item = EE_Line_Item::new_instance(array(
457
+				'LIN_name'       => esc_html__('Cancellation', 'event_espresso'),
458
+				'LIN_desc'       => sprintf(
459
+					esc_html_x(
460
+						'Cancelled %1$s : %2$s',
461
+						'Cancelled Ticket Name : 2015-01-01 11:11',
462
+						'event_espresso'
463
+					),
464
+					$ticket_line_item->name(),
465
+					current_time(get_option('date_format') . ' ' . get_option('time_format'))
466
+				),
467
+				'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468
+				'LIN_quantity'   => $qty,
469
+				'LIN_is_taxable' => $ticket_line_item->is_taxable(),
470
+				'LIN_order'      => count($ticket_line_item->children()),
471
+				'LIN_total'      => 0, // $ticket_line_item->unit_price()
472
+				'LIN_type'       => EEM_Line_Item::type_cancellation,
473
+			));
474
+			$ticket_line_item->add_child_line_item($cancellation_line_item);
475
+		}
476
+		if ($ticket_line_item->save_this_and_descendants() > 0) {
477
+			// decrement parent line item quantity
478
+			$event_line_item = $ticket_line_item->parent();
479
+			if ($event_line_item instanceof EE_Line_Item
480
+				&& $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
481
+			) {
482
+				$event_line_item->set_quantity($event_line_item->quantity() - $qty);
483
+				$event_line_item->save();
484
+			}
485
+			EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
486
+			return true;
487
+		}
488
+		return false;
489
+	}
490
+
491
+
492
+	/**
493
+	 * reinstates (un-cancels?) a previously canceled ticket line item,
494
+	 * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item.
495
+	 * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
496
+	 *
497
+	 * @param EE_Line_Item $ticket_line_item
498
+	 * @param int          $qty
499
+	 * @return bool success
500
+	 * @throws EE_Error
501
+	 * @throws InvalidArgumentException
502
+	 * @throws InvalidDataTypeException
503
+	 * @throws InvalidInterfaceException
504
+	 * @throws ReflectionException
505
+	 */
506
+	public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
507
+	{
508
+		// validate incoming line_item
509
+		if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
510
+			throw new EE_Error(
511
+				sprintf(
512
+					esc_html__(
513
+						'The supplied line item must have an Object Type of "Ticket", not %1$s.',
514
+						'event_espresso'
515
+					),
516
+					$ticket_line_item->type()
517
+				)
518
+			);
519
+		}
520
+		// get cancellation sub line item
521
+		$cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
522
+			$ticket_line_item,
523
+			EEM_Line_Item::type_cancellation
524
+		);
525
+		$cancellation_line_item = reset($cancellation_line_item);
526
+		// verify that this ticket was indeed previously cancelled
527
+		if (! $cancellation_line_item instanceof EE_Line_Item) {
528
+			return false;
529
+		}
530
+		if ($cancellation_line_item->quantity() > $qty) {
531
+			// decrement cancelled quantity
532
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
533
+		} elseif ($cancellation_line_item->quantity() === $qty) {
534
+			// decrement cancelled quantity in case anyone still has the object kicking around
535
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
536
+			// delete because quantity will end up as 0
537
+			$cancellation_line_item->delete();
538
+			// and attempt to destroy the object,
539
+			// even though PHP won't actually destroy it until it needs the memory
540
+			unset($cancellation_line_item);
541
+		} else {
542
+			// what ?!?! negative quantity ?!?!
543
+			throw new EE_Error(
544
+				sprintf(
545
+					esc_html__(
546
+						'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.',
547
+						'event_espresso'
548
+					),
549
+					$qty,
550
+					$cancellation_line_item->quantity()
551
+				)
552
+			);
553
+		}
554
+		// increment ticket quantity
555
+		$ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty);
556
+		if ($ticket_line_item->save_this_and_descendants() > 0) {
557
+			// increment parent line item quantity
558
+			$event_line_item = $ticket_line_item->parent();
559
+			if ($event_line_item instanceof EE_Line_Item
560
+				&& $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
561
+			) {
562
+				$event_line_item->set_quantity($event_line_item->quantity() + $qty);
563
+			}
564
+			EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
565
+			return true;
566
+		}
567
+		return false;
568
+	}
569
+
570
+
571
+	/**
572
+	 * calls EEH_Line_Item::find_transaction_grand_total_for_line_item()
573
+	 * then EE_Line_Item::recalculate_total_including_taxes() on the result
574
+	 *
575
+	 * @param EE_Line_Item $line_item
576
+	 * @return float
577
+	 * @throws EE_Error
578
+	 * @throws InvalidArgumentException
579
+	 * @throws InvalidDataTypeException
580
+	 * @throws InvalidInterfaceException
581
+	 * @throws ReflectionException
582
+	 */
583
+	public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item)
584
+	{
585
+		$grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item);
586
+		return $grand_total_line_item->recalculate_total_including_taxes();
587
+	}
588
+
589
+
590
+	/**
591
+	 * Gets the line item which contains the subtotal of all the items
592
+	 *
593
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
594
+	 * @return EE_Line_Item
595
+	 * @throws EE_Error
596
+	 * @throws InvalidArgumentException
597
+	 * @throws InvalidDataTypeException
598
+	 * @throws InvalidInterfaceException
599
+	 * @throws ReflectionException
600
+	 */
601
+	public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item)
602
+	{
603
+		$pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal');
604
+		return $pre_tax_subtotal instanceof EE_Line_Item
605
+			? $pre_tax_subtotal
606
+			: self::create_pre_tax_subtotal($total_line_item);
607
+	}
608
+
609
+
610
+	/**
611
+	 * Gets the line item for the taxes subtotal
612
+	 *
613
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
614
+	 * @return EE_Line_Item
615
+	 * @throws EE_Error
616
+	 * @throws InvalidArgumentException
617
+	 * @throws InvalidDataTypeException
618
+	 * @throws InvalidInterfaceException
619
+	 * @throws ReflectionException
620
+	 */
621
+	public static function get_taxes_subtotal(EE_Line_Item $total_line_item)
622
+	{
623
+		$taxes = $total_line_item->get_child_line_item('taxes');
624
+		return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item);
625
+	}
626
+
627
+
628
+	/**
629
+	 * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object
630
+	 *
631
+	 * @param EE_Line_Item   $line_item
632
+	 * @param EE_Transaction $transaction
633
+	 * @return void
634
+	 * @throws EE_Error
635
+	 * @throws InvalidArgumentException
636
+	 * @throws InvalidDataTypeException
637
+	 * @throws InvalidInterfaceException
638
+	 * @throws ReflectionException
639
+	 */
640
+	public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null)
641
+	{
642
+		if ($transaction) {
643
+			/** @type EEM_Transaction $EEM_Transaction */
644
+			$EEM_Transaction = EE_Registry::instance()->load_model('Transaction');
645
+			$TXN_ID = $EEM_Transaction->ensure_is_ID($transaction);
646
+			$line_item->set_TXN_ID($TXN_ID);
647
+		}
648
+	}
649
+
650
+
651
+	/**
652
+	 * Creates a new default total line item for the transaction,
653
+	 * and its tickets subtotal and taxes subtotal line items (and adds the
654
+	 * existing taxes as children of the taxes subtotal line item)
655
+	 *
656
+	 * @param EE_Transaction $transaction
657
+	 * @return EE_Line_Item of type total
658
+	 * @throws EE_Error
659
+	 * @throws InvalidArgumentException
660
+	 * @throws InvalidDataTypeException
661
+	 * @throws InvalidInterfaceException
662
+	 * @throws ReflectionException
663
+	 */
664
+	public static function create_total_line_item($transaction = null)
665
+	{
666
+		$total_line_item = EE_Line_Item::new_instance(array(
667
+			'LIN_code' => 'total',
668
+			'LIN_name' => esc_html__('Grand Total', 'event_espresso'),
669
+			'LIN_type' => EEM_Line_Item::type_total,
670
+			'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TRANSACTION,
671
+		));
672
+		$total_line_item = apply_filters(
673
+			'FHEE__EEH_Line_Item__create_total_line_item__total_line_item',
674
+			$total_line_item
675
+		);
676
+		self::set_TXN_ID($total_line_item, $transaction);
677
+		self::create_pre_tax_subtotal($total_line_item, $transaction);
678
+		self::create_taxes_subtotal($total_line_item, $transaction);
679
+		return $total_line_item;
680
+	}
681
+
682
+
683
+	/**
684
+	 * Creates a default items subtotal line item
685
+	 *
686
+	 * @param EE_Line_Item   $total_line_item
687
+	 * @param EE_Transaction $transaction
688
+	 * @return EE_Line_Item
689
+	 * @throws EE_Error
690
+	 * @throws InvalidArgumentException
691
+	 * @throws InvalidDataTypeException
692
+	 * @throws InvalidInterfaceException
693
+	 * @throws ReflectionException
694
+	 */
695
+	protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null)
696
+	{
697
+		$pre_tax_line_item = EE_Line_Item::new_instance(array(
698
+			'LIN_code' => 'pre-tax-subtotal',
699
+			'LIN_name' => esc_html__('Pre-Tax Subtotal', 'event_espresso'),
700
+			'LIN_type' => EEM_Line_Item::type_sub_total,
701
+		));
702
+		$pre_tax_line_item = apply_filters(
703
+			'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item',
704
+			$pre_tax_line_item
705
+		);
706
+		self::set_TXN_ID($pre_tax_line_item, $transaction);
707
+		$total_line_item->add_child_line_item($pre_tax_line_item);
708
+		self::create_event_subtotal($pre_tax_line_item, $transaction);
709
+		return $pre_tax_line_item;
710
+	}
711
+
712
+
713
+	/**
714
+	 * Creates a line item for the taxes subtotal and finds all the tax prices
715
+	 * and applies taxes to it
716
+	 *
717
+	 * @param EE_Line_Item   $total_line_item of type EEM_Line_Item::type_total
718
+	 * @param EE_Transaction $transaction
719
+	 * @return EE_Line_Item
720
+	 * @throws EE_Error
721
+	 * @throws InvalidArgumentException
722
+	 * @throws InvalidDataTypeException
723
+	 * @throws InvalidInterfaceException
724
+	 * @throws ReflectionException
725
+	 */
726
+	protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
727
+	{
728
+		$tax_line_item = EE_Line_Item::new_instance(array(
729
+			'LIN_code'  => 'taxes',
730
+			'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731
+			'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
+			'LIN_order' => 1000,// this should always come last
733
+		));
734
+		$tax_line_item = apply_filters(
735
+			'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
736
+			$tax_line_item
737
+		);
738
+		self::set_TXN_ID($tax_line_item, $transaction);
739
+		$total_line_item->add_child_line_item($tax_line_item);
740
+		// and lastly, add the actual taxes
741
+		self::apply_taxes($total_line_item);
742
+		return $tax_line_item;
743
+	}
744
+
745
+
746
+	/**
747
+	 * Creates a default items subtotal line item
748
+	 *
749
+	 * @param EE_Line_Item   $pre_tax_line_item
750
+	 * @param EE_Transaction $transaction
751
+	 * @param EE_Event       $event
752
+	 * @return EE_Line_Item
753
+	 * @throws EE_Error
754
+	 * @throws InvalidArgumentException
755
+	 * @throws InvalidDataTypeException
756
+	 * @throws InvalidInterfaceException
757
+	 * @throws ReflectionException
758
+	 */
759
+	public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null)
760
+	{
761
+		$event_line_item = EE_Line_Item::new_instance(array(
762
+			'LIN_code' => self::get_event_code($event),
763
+			'LIN_name' => self::get_event_name($event),
764
+			'LIN_desc' => self::get_event_desc($event),
765
+			'LIN_type' => EEM_Line_Item::type_sub_total,
766
+			'OBJ_type' => EEM_Line_Item::OBJ_TYPE_EVENT,
767
+			'OBJ_ID'   => $event instanceof EE_Event ? $event->ID() : 0,
768
+		));
769
+		$event_line_item = apply_filters(
770
+			'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item',
771
+			$event_line_item
772
+		);
773
+		self::set_TXN_ID($event_line_item, $transaction);
774
+		$pre_tax_line_item->add_child_line_item($event_line_item);
775
+		return $event_line_item;
776
+	}
777
+
778
+
779
+	/**
780
+	 * Gets what the event ticket's code SHOULD be
781
+	 *
782
+	 * @param EE_Event $event
783
+	 * @return string
784
+	 * @throws EE_Error
785
+	 */
786
+	public static function get_event_code($event)
787
+	{
788
+		return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
789
+	}
790
+
791
+
792
+	/**
793
+	 * Gets the event name
794
+	 *
795
+	 * @param EE_Event $event
796
+	 * @return string
797
+	 * @throws EE_Error
798
+	 */
799
+	public static function get_event_name($event)
800
+	{
801
+		return $event instanceof EE_Event
802
+			? mb_substr($event->name(), 0, 245)
803
+			: esc_html__('Event', 'event_espresso');
804
+	}
805
+
806
+
807
+	/**
808
+	 * Gets the event excerpt
809
+	 *
810
+	 * @param EE_Event $event
811
+	 * @return string
812
+	 * @throws EE_Error
813
+	 */
814
+	public static function get_event_desc($event)
815
+	{
816
+		return $event instanceof EE_Event ? $event->short_description() : '';
817
+	}
818
+
819
+
820
+	/**
821
+	 * Given the grand total line item and a ticket, finds the event sub-total
822
+	 * line item the ticket's purchase should be added onto
823
+	 *
824
+	 * @access public
825
+	 * @param EE_Line_Item $grand_total the grand total line item
826
+	 * @param EE_Ticket    $ticket
827
+	 * @return EE_Line_Item
828
+	 * @throws EE_Error
829
+	 * @throws InvalidArgumentException
830
+	 * @throws InvalidDataTypeException
831
+	 * @throws InvalidInterfaceException
832
+	 * @throws ReflectionException
833
+	 */
834
+	public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835
+	{
836
+		$first_datetime = $ticket->first_datetime();
837
+		if (! $first_datetime instanceof EE_Datetime) {
838
+			throw new EE_Error(
839
+				sprintf(
840
+					__('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
841
+					$ticket->ID()
842
+				)
843
+			);
844
+		}
845
+		$event = $first_datetime->event();
846
+		if (! $event instanceof EE_Event) {
847
+			throw new EE_Error(
848
+				sprintf(
849
+					esc_html__(
850
+						'The supplied ticket (ID %d) has no event data associated with it.',
851
+						'event_espresso'
852
+					),
853
+					$ticket->ID()
854
+				)
855
+			);
856
+		}
857
+		$events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
+		if (! $events_sub_total instanceof EE_Line_Item) {
859
+			throw new EE_Error(
860
+				sprintf(
861
+					esc_html__(
862
+						'There is no events sub-total for ticket %s on total line item %d',
863
+						'event_espresso'
864
+					),
865
+					$ticket->ID(),
866
+					$grand_total->ID()
867
+				)
868
+			);
869
+		}
870
+		return $events_sub_total;
871
+	}
872
+
873
+
874
+	/**
875
+	 * Gets the event line item
876
+	 *
877
+	 * @param EE_Line_Item $grand_total
878
+	 * @param EE_Event     $event
879
+	 * @return EE_Line_Item for the event subtotal which is a child of $grand_total
880
+	 * @throws EE_Error
881
+	 * @throws InvalidArgumentException
882
+	 * @throws InvalidDataTypeException
883
+	 * @throws InvalidInterfaceException
884
+	 * @throws ReflectionException
885
+	 */
886
+	public static function get_event_line_item(EE_Line_Item $grand_total, $event)
887
+	{
888
+		/** @type EE_Event $event */
889
+		$event = EEM_Event::instance()->ensure_is_obj($event, true);
890
+		$event_line_item = null;
891
+		$found = false;
892
+		foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893
+			// default event subtotal, we should only ever find this the first time this method is called
894
+			if (! $event_line_item->OBJ_ID()) {
895
+				// let's use this! but first... set the event details
896
+				EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897
+				$found = true;
898
+				break;
899
+			}
900
+			if ($event_line_item->OBJ_ID() === $event->ID()) {
901
+				// found existing line item for this event in the cart, so break out of loop and use this one
902
+				$found = true;
903
+				break;
904
+			}
905
+		}
906
+		if (! $found) {
907
+			// there is no event sub-total yet, so add it
908
+			$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909
+			// create a new "event" subtotal below that
910
+			$event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event);
911
+			// and set the event details
912
+			EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
913
+		}
914
+		return $event_line_item;
915
+	}
916
+
917
+
918
+	/**
919
+	 * Creates a default items subtotal line item
920
+	 *
921
+	 * @param EE_Line_Item   $event_line_item
922
+	 * @param EE_Event       $event
923
+	 * @param EE_Transaction $transaction
924
+	 * @return void
925
+	 * @throws EE_Error
926
+	 * @throws InvalidArgumentException
927
+	 * @throws InvalidDataTypeException
928
+	 * @throws InvalidInterfaceException
929
+	 * @throws ReflectionException
930
+	 */
931
+	public static function set_event_subtotal_details(
932
+		EE_Line_Item $event_line_item,
933
+		EE_Event $event,
934
+		$transaction = null
935
+	) {
936
+		if ($event instanceof EE_Event) {
937
+			$event_line_item->set_code(self::get_event_code($event));
938
+			$event_line_item->set_name(self::get_event_name($event));
939
+			$event_line_item->set_desc(self::get_event_desc($event));
940
+			$event_line_item->set_OBJ_ID($event->ID());
941
+		}
942
+		self::set_TXN_ID($event_line_item, $transaction);
943
+	}
944
+
945
+
946
+	/**
947
+	 * Finds what taxes should apply, adds them as tax line items under the taxes sub-total,
948
+	 * and recalculates the taxes sub-total and the grand total. Resets the taxes, so
949
+	 * any old taxes are removed
950
+	 *
951
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
952
+	 * @param bool         $update_txn_status
953
+	 * @return bool
954
+	 * @throws EE_Error
955
+	 * @throws InvalidArgumentException
956
+	 * @throws InvalidDataTypeException
957
+	 * @throws InvalidInterfaceException
958
+	 * @throws ReflectionException
959
+	 * @throws RuntimeException
960
+	 */
961
+	public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false)
962
+	{
963
+		/** @type EEM_Price $EEM_Price */
964
+		$EEM_Price = EE_Registry::instance()->load_model('Price');
965
+		// get array of taxes via Price Model
966
+		$ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes();
967
+		ksort($ordered_taxes);
968
+		$taxes_line_item = self::get_taxes_subtotal($total_line_item);
969
+		// just to be safe, remove its old tax line items
970
+		$deleted = $taxes_line_item->delete_children_line_items();
971
+		$updates = false;
972
+		// loop thru taxes
973
+		foreach ($ordered_taxes as $order => $taxes) {
974
+			foreach ($taxes as $tax) {
975
+				if ($tax instanceof EE_Price) {
976
+					$tax_line_item = EE_Line_Item::new_instance(
977
+						array(
978
+							'LIN_name'       => $tax->name(),
979
+							'LIN_desc'       => $tax->desc(),
980
+							'LIN_percent'    => $tax->amount(),
981
+							'LIN_is_taxable' => false,
982
+							'LIN_order'      => $order,
983
+							'LIN_total'      => 0,
984
+							'LIN_type'       => EEM_Line_Item::type_tax,
985
+							'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
986
+							'OBJ_ID'         => $tax->ID(),
987
+						)
988
+					);
989
+					$tax_line_item = apply_filters(
990
+						'FHEE__EEH_Line_Item__apply_taxes__tax_line_item',
991
+						$tax_line_item
992
+					);
993
+					$updates = $taxes_line_item->add_child_line_item($tax_line_item) ?
994
+						true :
995
+						$updates;
996
+				}
997
+			}
998
+		}
999
+		// only recalculate totals if something changed
1000
+		if ($deleted || $updates) {
1001
+			$total_line_item->recalculate_total_including_taxes($update_txn_status);
1002
+			return true;
1003
+		}
1004
+		return false;
1005
+	}
1006
+
1007
+
1008
+	/**
1009
+	 * Ensures that taxes have been applied to the order, if not applies them.
1010
+	 * Returns the total amount of tax
1011
+	 *
1012
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
1013
+	 * @return float
1014
+	 * @throws EE_Error
1015
+	 * @throws InvalidArgumentException
1016
+	 * @throws InvalidDataTypeException
1017
+	 * @throws InvalidInterfaceException
1018
+	 * @throws ReflectionException
1019
+	 */
1020
+	public static function ensure_taxes_applied($total_line_item)
1021
+	{
1022
+		$taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1023
+		if (! $taxes_subtotal->children()) {
1024
+			self::apply_taxes($total_line_item);
1025
+		}
1026
+		return $taxes_subtotal->total();
1027
+	}
1028
+
1029
+
1030
+	/**
1031
+	 * Deletes ALL children of the passed line item
1032
+	 *
1033
+	 * @param EE_Line_Item $parent_line_item
1034
+	 * @return bool
1035
+	 * @throws EE_Error
1036
+	 * @throws InvalidArgumentException
1037
+	 * @throws InvalidDataTypeException
1038
+	 * @throws InvalidInterfaceException
1039
+	 * @throws ReflectionException
1040
+	 */
1041
+	public static function delete_all_child_items(EE_Line_Item $parent_line_item)
1042
+	{
1043
+		$deleted = 0;
1044
+		foreach ($parent_line_item->children() as $child_line_item) {
1045
+			if ($child_line_item instanceof EE_Line_Item) {
1046
+				$deleted += EEH_Line_Item::delete_all_child_items($child_line_item);
1047
+				if ($child_line_item->ID()) {
1048
+					$child_line_item->delete();
1049
+					unset($child_line_item);
1050
+				} else {
1051
+					$parent_line_item->delete_child_line_item($child_line_item->code());
1052
+				}
1053
+				$deleted++;
1054
+			}
1055
+		}
1056
+		return $deleted;
1057
+	}
1058
+
1059
+
1060
+	/**
1061
+	 * Deletes the line items as indicated by the line item code(s) provided,
1062
+	 * regardless of where they're found in the line item tree. Automatically
1063
+	 * re-calculates the line item totals and updates the related transaction. But
1064
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
1065
+	 * should probably change because of this).
1066
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
1067
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
1068
+	 *
1069
+	 * @param EE_Line_Item      $total_line_item of type EEM_Line_Item::type_total
1070
+	 * @param array|bool|string $line_item_codes
1071
+	 * @return int number of items successfully removed
1072
+	 * @throws EE_Error
1073
+	 */
1074
+	public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false)
1075
+	{
1076
+
1077
+		if ($total_line_item->type() !== EEM_Line_Item::type_total) {
1078
+			EE_Error::doing_it_wrong(
1079
+				'EEH_Line_Item::delete_items',
1080
+				esc_html__(
1081
+					'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly',
1082
+					'event_espresso'
1083
+				),
1084
+				'4.6.18'
1085
+			);
1086
+		}
1087
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1088
+
1089
+		// check if only a single line_item_id was passed
1090
+		if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1091
+			// place single line_item_id in an array to appear as multiple line_item_ids
1092
+			$line_item_codes = array($line_item_codes);
1093
+		}
1094
+		$removals = 0;
1095
+		// cycle thru line_item_ids
1096
+		foreach ($line_item_codes as $line_item_id) {
1097
+			$removals += $total_line_item->delete_child_line_item($line_item_id);
1098
+		}
1099
+
1100
+		if ($removals > 0) {
1101
+			$total_line_item->recalculate_taxes_and_tax_total();
1102
+			return $removals;
1103
+		} else {
1104
+			return false;
1105
+		}
1106
+	}
1107
+
1108
+
1109
+	/**
1110
+	 * Overwrites the previous tax by clearing out the old taxes, and creates a new
1111
+	 * tax and updates the total line item accordingly
1112
+	 *
1113
+	 * @param EE_Line_Item $total_line_item
1114
+	 * @param float        $amount
1115
+	 * @param string       $name
1116
+	 * @param string       $description
1117
+	 * @param string       $code
1118
+	 * @param boolean      $add_to_existing_line_item
1119
+	 *                          if true, and a duplicate line item with the same code is found,
1120
+	 *                          $amount will be added onto it; otherwise will simply set the taxes to match $amount
1121
+	 * @return EE_Line_Item the new tax line item created
1122
+	 * @throws EE_Error
1123
+	 * @throws InvalidArgumentException
1124
+	 * @throws InvalidDataTypeException
1125
+	 * @throws InvalidInterfaceException
1126
+	 * @throws ReflectionException
1127
+	 */
1128
+	public static function set_total_tax_to(
1129
+		EE_Line_Item $total_line_item,
1130
+		$amount,
1131
+		$name = null,
1132
+		$description = null,
1133
+		$code = null,
1134
+		$add_to_existing_line_item = false
1135
+	) {
1136
+		$tax_subtotal = self::get_taxes_subtotal($total_line_item);
1137
+		$taxable_total = $total_line_item->taxable_total();
1138
+
1139
+		if ($add_to_existing_line_item) {
1140
+			$new_tax = $tax_subtotal->get_child_line_item($code);
1141
+			EEM_Line_Item::instance()->delete(
1142
+				array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID()))
1143
+			);
1144
+		} else {
1145
+			$new_tax = null;
1146
+			$tax_subtotal->delete_children_line_items();
1147
+		}
1148
+		if ($new_tax) {
1149
+			$new_tax->set_total($new_tax->total() + $amount);
1150
+			$new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0);
1151
+		} else {
1152
+			// no existing tax item. Create it
1153
+			$new_tax = EE_Line_Item::new_instance(array(
1154
+				'TXN_ID'      => $total_line_item->TXN_ID(),
1155
+				'LIN_name'    => $name ? $name : esc_html__('Tax', 'event_espresso'),
1156
+				'LIN_desc'    => $description ? $description : '',
1157
+				'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0,
1158
+				'LIN_total'   => $amount,
1159
+				'LIN_parent'  => $tax_subtotal->ID(),
1160
+				'LIN_type'    => EEM_Line_Item::type_tax,
1161
+				'LIN_code'    => $code,
1162
+			));
1163
+		}
1164
+
1165
+		$new_tax = apply_filters(
1166
+			'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal',
1167
+			$new_tax,
1168
+			$total_line_item
1169
+		);
1170
+		$new_tax->save();
1171
+		$tax_subtotal->set_total($new_tax->total());
1172
+		$tax_subtotal->save();
1173
+		$total_line_item->recalculate_total_including_taxes();
1174
+		return $new_tax;
1175
+	}
1176
+
1177
+
1178
+	/**
1179
+	 * Makes all the line items which are children of $line_item taxable (or not).
1180
+	 * Does NOT save the line items
1181
+	 *
1182
+	 * @param EE_Line_Item $line_item
1183
+	 * @param boolean      $taxable
1184
+	 * @param string       $code_substring_for_whitelist if this string is part of the line item's code
1185
+	 *                                                   it will be whitelisted (ie, except from becoming taxable)
1186
+	 * @throws EE_Error
1187
+	 */
1188
+	public static function set_line_items_taxable(
1189
+		EE_Line_Item $line_item,
1190
+		$taxable = true,
1191
+		$code_substring_for_whitelist = null
1192
+	) {
1193
+		$whitelisted = false;
1194
+		if ($code_substring_for_whitelist !== null) {
1195
+			$whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1196
+		}
1197
+		if (! $whitelisted && $line_item->is_line_item()) {
1198
+			$line_item->set_is_taxable($taxable);
1199
+		}
1200
+		foreach ($line_item->children() as $child_line_item) {
1201
+			EEH_Line_Item::set_line_items_taxable(
1202
+				$child_line_item,
1203
+				$taxable,
1204
+				$code_substring_for_whitelist
1205
+			);
1206
+		}
1207
+	}
1208
+
1209
+
1210
+	/**
1211
+	 * Gets all descendants that are event subtotals
1212
+	 *
1213
+	 * @uses  EEH_Line_Item::get_subtotals_of_object_type()
1214
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1215
+	 * @return EE_Line_Item[]
1216
+	 * @throws EE_Error
1217
+	 */
1218
+	public static function get_event_subtotals(EE_Line_Item $parent_line_item)
1219
+	{
1220
+		return self::get_subtotals_of_object_type($parent_line_item, EEM_Line_Item::OBJ_TYPE_EVENT);
1221
+	}
1222
+
1223
+
1224
+	/**
1225
+	 * Gets all descendants subtotals that match the supplied object type
1226
+	 *
1227
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1228
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1229
+	 * @param string       $obj_type
1230
+	 * @return EE_Line_Item[]
1231
+	 * @throws EE_Error
1232
+	 */
1233
+	public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1234
+	{
1235
+		return self::_get_descendants_by_type_and_object_type(
1236
+			$parent_line_item,
1237
+			EEM_Line_Item::type_sub_total,
1238
+			$obj_type
1239
+		);
1240
+	}
1241
+
1242
+
1243
+	/**
1244
+	 * Gets all descendants that are tickets
1245
+	 *
1246
+	 * @uses  EEH_Line_Item::get_line_items_of_object_type()
1247
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1248
+	 * @return EE_Line_Item[]
1249
+	 * @throws EE_Error
1250
+	 */
1251
+	public static function get_ticket_line_items(EE_Line_Item $parent_line_item)
1252
+	{
1253
+		return self::get_line_items_of_object_type(
1254
+			$parent_line_item,
1255
+			EEM_Line_Item::OBJ_TYPE_TICKET
1256
+		);
1257
+	}
1258
+
1259
+
1260
+	/**
1261
+	 * Gets all descendants subtotals that match the supplied object type
1262
+	 *
1263
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1264
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1265
+	 * @param string       $obj_type
1266
+	 * @return EE_Line_Item[]
1267
+	 * @throws EE_Error
1268
+	 */
1269
+	public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1270
+	{
1271
+		return self::_get_descendants_by_type_and_object_type(
1272
+			$parent_line_item,
1273
+			EEM_Line_Item::type_line_item,
1274
+			$obj_type
1275
+		);
1276
+	}
1277
+
1278
+
1279
+	/**
1280
+	 * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax'
1281
+	 *
1282
+	 * @uses  EEH_Line_Item::get_descendants_of_type()
1283
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1284
+	 * @return EE_Line_Item[]
1285
+	 * @throws EE_Error
1286
+	 */
1287
+	public static function get_tax_descendants(EE_Line_Item $parent_line_item)
1288
+	{
1289
+		return EEH_Line_Item::get_descendants_of_type(
1290
+			$parent_line_item,
1291
+			EEM_Line_Item::type_tax
1292
+		);
1293
+	}
1294
+
1295
+
1296
+	/**
1297
+	 * Gets all the real items purchased which are children of this item
1298
+	 *
1299
+	 * @uses  EEH_Line_Item::get_descendants_of_type()
1300
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1301
+	 * @return EE_Line_Item[]
1302
+	 * @throws EE_Error
1303
+	 */
1304
+	public static function get_line_item_descendants(EE_Line_Item $parent_line_item)
1305
+	{
1306
+		return EEH_Line_Item::get_descendants_of_type(
1307
+			$parent_line_item,
1308
+			EEM_Line_Item::type_line_item
1309
+		);
1310
+	}
1311
+
1312
+
1313
+	/**
1314
+	 * Gets all descendants of supplied line item that match the supplied line item type
1315
+	 *
1316
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1317
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1318
+	 * @param string       $line_item_type   one of the EEM_Line_Item constants
1319
+	 * @return EE_Line_Item[]
1320
+	 * @throws EE_Error
1321
+	 */
1322
+	public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type)
1323
+	{
1324
+		return self::_get_descendants_by_type_and_object_type(
1325
+			$parent_line_item,
1326
+			$line_item_type,
1327
+			null
1328
+		);
1329
+	}
1330
+
1331
+
1332
+	/**
1333
+	 * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1334
+	 * as well
1335
+	 *
1336
+	 * @param EE_Line_Item  $parent_line_item - the line item to find descendants of
1337
+	 * @param string        $line_item_type   one of the EEM_Line_Item constants
1338
+	 * @param string | NULL $obj_type         object model class name (minus prefix) or NULL to ignore object type when
1339
+	 *                                        searching
1340
+	 * @return EE_Line_Item[]
1341
+	 * @throws EE_Error
1342
+	 */
1343
+	protected static function _get_descendants_by_type_and_object_type(
1344
+		EE_Line_Item $parent_line_item,
1345
+		$line_item_type,
1346
+		$obj_type = null
1347
+	) {
1348
+		$objects = array();
1349
+		foreach ($parent_line_item->children() as $child_line_item) {
1350
+			if ($child_line_item instanceof EE_Line_Item) {
1351
+				if ($child_line_item->type() === $line_item_type
1352
+					&& (
1353
+						$child_line_item->OBJ_type() === $obj_type || $obj_type === null
1354
+					)
1355
+				) {
1356
+					$objects[] = $child_line_item;
1357
+				} else {
1358
+					// go-through-all-its children looking for more matches
1359
+					$objects = array_merge(
1360
+						$objects,
1361
+						self::_get_descendants_by_type_and_object_type(
1362
+							$child_line_item,
1363
+							$line_item_type,
1364
+							$obj_type
1365
+						)
1366
+					);
1367
+				}
1368
+			}
1369
+		}
1370
+		return $objects;
1371
+	}
1372
+
1373
+
1374
+	/**
1375
+	 * Gets all descendants subtotals that match the supplied object type
1376
+	 *
1377
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1378
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1379
+	 * @param string       $OBJ_type         object type (like Event)
1380
+	 * @param array        $OBJ_IDs          array of OBJ_IDs
1381
+	 * @return EE_Line_Item[]
1382
+	 * @throws EE_Error
1383
+	 */
1384
+	public static function get_line_items_by_object_type_and_IDs(
1385
+		EE_Line_Item $parent_line_item,
1386
+		$OBJ_type = '',
1387
+		$OBJ_IDs = array()
1388
+	) {
1389
+		return self::_get_descendants_by_object_type_and_object_ID(
1390
+			$parent_line_item,
1391
+			$OBJ_type,
1392
+			$OBJ_IDs
1393
+		);
1394
+	}
1395
+
1396
+
1397
+	/**
1398
+	 * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1399
+	 * as well
1400
+	 *
1401
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1402
+	 * @param string       $OBJ_type         object type (like Event)
1403
+	 * @param array        $OBJ_IDs          array of OBJ_IDs
1404
+	 * @return EE_Line_Item[]
1405
+	 * @throws EE_Error
1406
+	 */
1407
+	protected static function _get_descendants_by_object_type_and_object_ID(
1408
+		EE_Line_Item $parent_line_item,
1409
+		$OBJ_type,
1410
+		$OBJ_IDs
1411
+	) {
1412
+		$objects = array();
1413
+		foreach ($parent_line_item->children() as $child_line_item) {
1414
+			if ($child_line_item instanceof EE_Line_Item) {
1415
+				if ($child_line_item->OBJ_type() === $OBJ_type
1416
+					&& is_array($OBJ_IDs)
1417
+					&& in_array($child_line_item->OBJ_ID(), $OBJ_IDs)
1418
+				) {
1419
+					$objects[] = $child_line_item;
1420
+				} else {
1421
+					// go-through-all-its children looking for more matches
1422
+					$objects = array_merge(
1423
+						$objects,
1424
+						self::_get_descendants_by_object_type_and_object_ID(
1425
+							$child_line_item,
1426
+							$OBJ_type,
1427
+							$OBJ_IDs
1428
+						)
1429
+					);
1430
+				}
1431
+			}
1432
+		}
1433
+		return $objects;
1434
+	}
1435
+
1436
+
1437
+	/**
1438
+	 * Uses a breadth-first-search in order to find the nearest descendant of
1439
+	 * the specified type and returns it, else NULL
1440
+	 *
1441
+	 * @uses  EEH_Line_Item::_get_nearest_descendant()
1442
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1443
+	 * @param string       $type             like one of the EEM_Line_Item::type_*
1444
+	 * @return EE_Line_Item
1445
+	 * @throws EE_Error
1446
+	 * @throws InvalidArgumentException
1447
+	 * @throws InvalidDataTypeException
1448
+	 * @throws InvalidInterfaceException
1449
+	 * @throws ReflectionException
1450
+	 */
1451
+	public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type)
1452
+	{
1453
+		return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type);
1454
+	}
1455
+
1456
+
1457
+	/**
1458
+	 * Uses a breadth-first-search in order to find the nearest descendant
1459
+	 * having the specified LIN_code and returns it, else NULL
1460
+	 *
1461
+	 * @uses  EEH_Line_Item::_get_nearest_descendant()
1462
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1463
+	 * @param string       $code             any value used for LIN_code
1464
+	 * @return EE_Line_Item
1465
+	 * @throws EE_Error
1466
+	 * @throws InvalidArgumentException
1467
+	 * @throws InvalidDataTypeException
1468
+	 * @throws InvalidInterfaceException
1469
+	 * @throws ReflectionException
1470
+	 */
1471
+	public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code)
1472
+	{
1473
+		return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code);
1474
+	}
1475
+
1476
+
1477
+	/**
1478
+	 * Uses a breadth-first-search in order to find the nearest descendant
1479
+	 * having the specified LIN_code and returns it, else NULL
1480
+	 *
1481
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1482
+	 * @param string       $search_field     name of EE_Line_Item property
1483
+	 * @param string       $value            any value stored in $search_field
1484
+	 * @return EE_Line_Item
1485
+	 * @throws EE_Error
1486
+	 * @throws InvalidArgumentException
1487
+	 * @throws InvalidDataTypeException
1488
+	 * @throws InvalidInterfaceException
1489
+	 * @throws ReflectionException
1490
+	 */
1491
+	protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value)
1492
+	{
1493
+		foreach ($parent_line_item->children() as $child) {
1494
+			if ($child->get($search_field) == $value) {
1495
+				return $child;
1496
+			}
1497
+		}
1498
+		foreach ($parent_line_item->children() as $child) {
1499
+			$descendant_found = self::_get_nearest_descendant(
1500
+				$child,
1501
+				$search_field,
1502
+				$value
1503
+			);
1504
+			if ($descendant_found) {
1505
+				return $descendant_found;
1506
+			}
1507
+		}
1508
+		return null;
1509
+	}
1510
+
1511
+
1512
+	/**
1513
+	 * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction,
1514
+	 * else recursively walks up the line item tree until a parent of type total is found,
1515
+	 *
1516
+	 * @param EE_Line_Item $line_item
1517
+	 * @return EE_Line_Item
1518
+	 * @throws EE_Error
1519
+	 */
1520
+	public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item)
1521
+	{
1522
+		if ($line_item->TXN_ID()) {
1523
+			$total_line_item = $line_item->transaction()->total_line_item(false);
1524
+			if ($total_line_item instanceof EE_Line_Item) {
1525
+				return $total_line_item;
1526
+			}
1527
+		} else {
1528
+			$line_item_parent = $line_item->parent();
1529
+			if ($line_item_parent instanceof EE_Line_Item) {
1530
+				if ($line_item_parent->is_total()) {
1531
+					return $line_item_parent;
1532
+				}
1533
+				return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent);
1534
+			}
1535
+		}
1536
+		throw new EE_Error(
1537
+			sprintf(
1538
+				esc_html__(
1539
+					'A valid grand total for line item %1$d was not found.',
1540
+					'event_espresso'
1541
+				),
1542
+				$line_item->ID()
1543
+			)
1544
+		);
1545
+	}
1546
+
1547
+
1548
+	/**
1549
+	 * Prints out a representation of the line item tree
1550
+	 *
1551
+	 * @param EE_Line_Item $line_item
1552
+	 * @param int          $indentation
1553
+	 * @return void
1554
+	 * @throws EE_Error
1555
+	 */
1556
+	public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1557
+	{
1558
+		echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559
+		if (! $indentation) {
1560
+			echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1561
+		}
1562
+		for ($i = 0; $i < $indentation; $i++) {
1563
+			echo '. ';
1564
+		}
1565
+		$breakdown = '';
1566
+		if ($line_item->is_line_item()) {
1567
+			if ($line_item->is_percent()) {
1568
+				$breakdown = "{$line_item->percent()}%";
1569
+			} else {
1570
+				$breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1571
+			}
1572
+		}
1573
+		echo $line_item->name();
1574
+		echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1575
+		echo '$' . (string) $line_item->total();
1576
+		if ($breakdown) {
1577
+			echo " ( {$breakdown} )";
1578
+		}
1579
+		if ($line_item->is_taxable()) {
1580
+			echo '  * taxable';
1581
+		}
1582
+		if ($line_item->children()) {
1583
+			foreach ($line_item->children() as $child) {
1584
+				self::visualize($child, $indentation + 1);
1585
+			}
1586
+		}
1587
+	}
1588
+
1589
+
1590
+	/**
1591
+	 * Calculates the registration's final price, taking into account that they
1592
+	 * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes,
1593
+	 * and receive a portion of any transaction-wide discounts.
1594
+	 * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount
1595
+	 * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get
1596
+	 * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50
1597
+	 * and brent's final price should be $5.50.
1598
+	 * In order to do this, we basically need to traverse the line item tree calculating
1599
+	 * the running totals (just as if we were recalculating the total), but when we identify
1600
+	 * regular line items, we need to keep track of their share of the grand total.
1601
+	 * Also, we need to keep track of the TAXABLE total for each ticket purchase, so
1602
+	 * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total"
1603
+	 * when there are non-taxable items; otherwise they would be the same)
1604
+	 *
1605
+	 * @param EE_Line_Item $line_item
1606
+	 * @param array        $billable_ticket_quantities  array of EE_Ticket IDs and their corresponding quantity that
1607
+	 *                                                  can be included in price calculations at this moment
1608
+	 * @return array        keys are line items for tickets IDs and values are their share of the running total,
1609
+	 *                                                  plus the key 'total', and 'taxable' which also has keys of all
1610
+	 *                                                  the ticket IDs.
1611
+	 *                                                  Eg array(
1612
+	 *                                                      12 => 4.3
1613
+	 *                                                      23 => 8.0
1614
+	 *                                                      'total' => 16.6,
1615
+	 *                                                      'taxable' => array(
1616
+	 *                                                          12 => 10,
1617
+	 *                                                          23 => 4
1618
+	 *                                                      ).
1619
+	 *                                                  So to find which registrations have which final price, we need
1620
+	 *                                                  to find which line item is theirs, which can be done with
1621
+	 *                                                  `EEM_Line_Item::instance()->get_line_item_for_registration(
1622
+	 *                                                  $registration );`
1623
+	 * @throws EE_Error
1624
+	 * @throws InvalidArgumentException
1625
+	 * @throws InvalidDataTypeException
1626
+	 * @throws InvalidInterfaceException
1627
+	 * @throws ReflectionException
1628
+	 */
1629
+	public static function calculate_reg_final_prices_per_line_item(
1630
+		EE_Line_Item $line_item,
1631
+		$billable_ticket_quantities = array()
1632
+	) {
1633
+		$running_totals = [
1634
+			'total'   => 0,
1635
+			'taxable' => ['total' => 0]
1636
+		];
1637
+		foreach ($line_item->children() as $child_line_item) {
1638
+			switch ($child_line_item->type()) {
1639
+				case EEM_Line_Item::type_sub_total:
1640
+					$running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1641
+						$child_line_item,
1642
+						$billable_ticket_quantities
1643
+					);
1644
+					// combine arrays but preserve numeric keys
1645
+					$running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals);
1646
+					$running_totals['total'] += $running_totals_from_subtotal['total'];
1647
+					$running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total'];
1648
+					break;
1649
+
1650
+				case EEM_Line_Item::type_tax_sub_total:
1651
+					// find how much the taxes percentage is
1652
+					if ($child_line_item->percent() !== 0) {
1653
+						$tax_percent_decimal = $child_line_item->percent() / 100;
1654
+					} else {
1655
+						$tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100;
1656
+					}
1657
+					// and apply to all the taxable totals, and add to the pretax totals
1658
+					foreach ($running_totals as $line_item_id => $this_running_total) {
1659
+						// "total" and "taxable" array key is an exception
1660
+						if ($line_item_id === 'taxable') {
1661
+							continue;
1662
+						}
1663
+						$taxable_total = $running_totals['taxable'][ $line_item_id ];
1664
+						$running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1665
+					}
1666
+					break;
1667
+
1668
+				case EEM_Line_Item::type_line_item:
1669
+					// ticket line items or ????
1670
+					if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1671
+						// kk it's a ticket
1672
+						if (isset($running_totals[ $child_line_item->ID() ])) {
1673
+							// huh? that shouldn't happen.
1674
+							$running_totals['total'] += $child_line_item->total();
1675
+						} else {
1676
+							// its not in our running totals yet. great.
1677
+							if ($child_line_item->is_taxable()) {
1678
+								$taxable_amount = $child_line_item->unit_price();
1679
+							} else {
1680
+								$taxable_amount = 0;
1681
+							}
1682
+							// are we only calculating totals for some tickets?
1683
+							if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1684
+								$quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1685
+								$running_totals[ $child_line_item->ID() ] = $quantity
1686
+									? $child_line_item->unit_price()
1687
+									: 0;
1688
+								$running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1689
+									? $taxable_amount
1690
+									: 0;
1691
+							} else {
1692
+								$quantity = $child_line_item->quantity();
1693
+								$running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1694
+								$running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1695
+							}
1696
+							$running_totals['taxable']['total'] += $taxable_amount * $quantity;
1697
+							$running_totals['total'] += $child_line_item->unit_price() * $quantity;
1698
+						}
1699
+					} else {
1700
+						// it's some other type of item added to the cart
1701
+						// it should affect the running totals
1702
+						// basically we want to convert it into a PERCENT modifier. Because
1703
+						// more clearly affect all registration's final price equally
1704
+						$line_items_percent_of_running_total = $running_totals['total'] > 0
1705
+							? ($child_line_item->total() / $running_totals['total']) + 1
1706
+							: 1;
1707
+						foreach ($running_totals as $line_item_id => $this_running_total) {
1708
+							// the "taxable" array key is an exception
1709
+							if ($line_item_id === 'taxable') {
1710
+								continue;
1711
+							}
1712
+							// update the running totals
1713
+							// yes this actually even works for the running grand total!
1714
+							$running_totals[ $line_item_id ] =
1715
+								$line_items_percent_of_running_total * $this_running_total;
1716
+
1717
+							if ($child_line_item->is_taxable()) {
1718
+								$running_totals['taxable'][ $line_item_id ] =
1719
+									$line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1720
+							}
1721
+						}
1722
+					}
1723
+					break;
1724
+			}
1725
+		}
1726
+		return $running_totals;
1727
+	}
1728
+
1729
+
1730
+	/**
1731
+	 * @param EE_Line_Item $total_line_item
1732
+	 * @param EE_Line_Item $ticket_line_item
1733
+	 * @return float | null
1734
+	 * @throws EE_Error
1735
+	 * @throws InvalidArgumentException
1736
+	 * @throws InvalidDataTypeException
1737
+	 * @throws InvalidInterfaceException
1738
+	 * @throws OutOfRangeException
1739
+	 * @throws ReflectionException
1740
+	 */
1741
+	public static function calculate_final_price_for_ticket_line_item(
1742
+		EE_Line_Item $total_line_item,
1743
+		EE_Line_Item $ticket_line_item
1744
+	) {
1745
+		static $final_prices_per_ticket_line_item = array();
1746
+		if (empty($final_prices_per_ticket_line_item)) {
1747
+			$final_prices_per_ticket_line_item = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1748
+				$total_line_item
1749
+			);
1750
+		}
1751
+		// ok now find this new registration's final price
1752
+		if (isset($final_prices_per_ticket_line_item[ $ticket_line_item->ID() ])) {
1753
+			return $final_prices_per_ticket_line_item[ $ticket_line_item->ID() ];
1754
+		}
1755
+		$message = sprintf(
1756
+			esc_html__(
1757
+				'The final price for the ticket line item (ID:%1$d) could not be calculated.',
1758
+				'event_espresso'
1759
+			),
1760
+			$ticket_line_item->ID()
1761
+		);
1762
+		if (WP_DEBUG) {
1763
+			$message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1764
+			throw new OutOfRangeException($message);
1765
+		}
1766
+		EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
1767
+		return null;
1768
+	}
1769
+
1770
+
1771
+	/**
1772
+	 * Creates a duplicate of the line item tree, except only includes billable items
1773
+	 * and the portion of line items attributed to billable things
1774
+	 *
1775
+	 * @param EE_Line_Item      $line_item
1776
+	 * @param EE_Registration[] $registrations
1777
+	 * @return EE_Line_Item
1778
+	 * @throws EE_Error
1779
+	 * @throws InvalidArgumentException
1780
+	 * @throws InvalidDataTypeException
1781
+	 * @throws InvalidInterfaceException
1782
+	 * @throws ReflectionException
1783
+	 */
1784
+	public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations)
1785
+	{
1786
+		$copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations);
1787
+		foreach ($line_item->children() as $child_li) {
1788
+			$copy_li->add_child_line_item(
1789
+				EEH_Line_Item::billable_line_item_tree($child_li, $registrations)
1790
+			);
1791
+		}
1792
+		// if this is the grand total line item, make sure the totals all add up
1793
+		// (we could have duplicated this logic AS we copied the line items, but
1794
+		// it seems DRYer this way)
1795
+		if ($copy_li->type() === EEM_Line_Item::type_total) {
1796
+			$copy_li->recalculate_total_including_taxes();
1797
+		}
1798
+		return $copy_li;
1799
+	}
1800
+
1801
+
1802
+	/**
1803
+	 * Creates a new, unsaved line item from $line_item that factors in the
1804
+	 * number of billable registrations on $registrations.
1805
+	 *
1806
+	 * @param EE_Line_Item      $line_item
1807
+	 * @param EE_Registration[] $registrations
1808
+	 * @return EE_Line_Item
1809
+	 * @throws EE_Error
1810
+	 * @throws InvalidArgumentException
1811
+	 * @throws InvalidDataTypeException
1812
+	 * @throws InvalidInterfaceException
1813
+	 * @throws ReflectionException
1814
+	 */
1815
+	public static function billable_line_item(EE_Line_Item $line_item, $registrations)
1816
+	{
1817
+		$new_li_fields = $line_item->model_field_array();
1818
+		if ($line_item->type() === EEM_Line_Item::type_line_item &&
1819
+			$line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1820
+		) {
1821
+			$count = 0;
1822
+			foreach ($registrations as $registration) {
1823
+				if ($line_item->OBJ_ID() === $registration->ticket_ID() &&
1824
+					in_array(
1825
+						$registration->status_ID(),
1826
+						EEM_Registration::reg_statuses_that_allow_payment(),
1827
+						true
1828
+					)
1829
+				) {
1830
+					$count++;
1831
+				}
1832
+			}
1833
+			$new_li_fields['LIN_quantity'] = $count;
1834
+		}
1835
+		// don't set the total. We'll leave that up to the code that calculates it
1836
+		unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']);
1837
+		return EE_Line_Item::new_instance($new_li_fields);
1838
+	}
1839
+
1840
+
1841
+	/**
1842
+	 * Returns a modified line item tree where all the subtotals which have a total of 0
1843
+	 * are removed, and line items with a quantity of 0
1844
+	 *
1845
+	 * @param EE_Line_Item $line_item |null
1846
+	 * @return EE_Line_Item|null
1847
+	 * @throws EE_Error
1848
+	 * @throws InvalidArgumentException
1849
+	 * @throws InvalidDataTypeException
1850
+	 * @throws InvalidInterfaceException
1851
+	 * @throws ReflectionException
1852
+	 */
1853
+	public static function non_empty_line_items(EE_Line_Item $line_item)
1854
+	{
1855
+		$copied_li = EEH_Line_Item::non_empty_line_item($line_item);
1856
+		if ($copied_li === null) {
1857
+			return null;
1858
+		}
1859
+		// if this is an event subtotal, we want to only include it if it
1860
+		// has a non-zero total and at least one ticket line item child
1861
+		$ticket_children = 0;
1862
+		foreach ($line_item->children() as $child_li) {
1863
+			$child_li_copy = EEH_Line_Item::non_empty_line_items($child_li);
1864
+			if ($child_li_copy !== null) {
1865
+				$copied_li->add_child_line_item($child_li_copy);
1866
+				if ($child_li_copy->type() === EEM_Line_Item::type_line_item &&
1867
+					$child_li_copy->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1868
+				) {
1869
+					$ticket_children++;
1870
+				}
1871
+			}
1872
+		}
1873
+		// if this is an event subtotal with NO ticket children
1874
+		// we basically want to ignore it
1875
+		if ($ticket_children === 0
1876
+			&& $line_item->type() === EEM_Line_Item::type_sub_total
1877
+			&& $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
1878
+			&& $line_item->total() === 0
1879
+		) {
1880
+			return null;
1881
+		}
1882
+		return $copied_li;
1883
+	}
1884
+
1885
+
1886
+	/**
1887
+	 * Creates a new, unsaved line item, but if it's a ticket line item
1888
+	 * with a total of 0, or a subtotal of 0, returns null instead
1889
+	 *
1890
+	 * @param EE_Line_Item $line_item
1891
+	 * @return EE_Line_Item
1892
+	 * @throws EE_Error
1893
+	 * @throws InvalidArgumentException
1894
+	 * @throws InvalidDataTypeException
1895
+	 * @throws InvalidInterfaceException
1896
+	 * @throws ReflectionException
1897
+	 */
1898
+	public static function non_empty_line_item(EE_Line_Item $line_item)
1899
+	{
1900
+		if ($line_item->type() === EEM_Line_Item::type_line_item
1901
+			&& $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1902
+			&& $line_item->quantity() === 0
1903
+		) {
1904
+			return null;
1905
+		}
1906
+		$new_li_fields = $line_item->model_field_array();
1907
+		// don't set the total. We'll leave that up to the code that calculates it
1908
+		unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']);
1909
+		return EE_Line_Item::new_instance($new_li_fields);
1910
+	}
1911
+
1912
+
1913
+	/**
1914
+	 * Cycles through all of the ticket line items for the supplied total line item
1915
+	 * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket
1916
+	 *
1917
+	 * @param EE_Line_Item $total_line_item
1918
+	 * @since 4.9.79.p
1919
+	 * @throws EE_Error
1920
+	 * @throws InvalidArgumentException
1921
+	 * @throws InvalidDataTypeException
1922
+	 * @throws InvalidInterfaceException
1923
+	 * @throws ReflectionException
1924
+	 */
1925
+	public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item)
1926
+	{
1927
+		$ticket_line_items = self::get_ticket_line_items($total_line_item);
1928
+		foreach ($ticket_line_items as $ticket_line_item) {
1929
+			if ($ticket_line_item instanceof EE_Line_Item
1930
+				&& $ticket_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1931
+			) {
1932
+				$ticket = $ticket_line_item->ticket();
1933
+				if ($ticket instanceof EE_Ticket && $ticket->taxable() !== $ticket_line_item->is_taxable()) {
1934
+					$ticket_line_item->set_is_taxable($ticket->taxable());
1935
+					$ticket_line_item->save();
1936
+				}
1937
+			}
1938
+		}
1939
+	}
1940
+
1941
+
1942
+
1943
+	/**************************************** @DEPRECATED METHODS *************************************** */
1944
+	/**
1945
+	 * @deprecated
1946
+	 * @param EE_Line_Item $total_line_item
1947
+	 * @return EE_Line_Item
1948
+	 * @throws EE_Error
1949
+	 * @throws InvalidArgumentException
1950
+	 * @throws InvalidDataTypeException
1951
+	 * @throws InvalidInterfaceException
1952
+	 * @throws ReflectionException
1953
+	 */
1954
+	public static function get_items_subtotal(EE_Line_Item $total_line_item)
1955
+	{
1956
+		EE_Error::doing_it_wrong(
1957
+			'EEH_Line_Item::get_items_subtotal()',
1958
+			sprintf(
1959
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1960
+				'EEH_Line_Item::get_pre_tax_subtotal()'
1961
+			),
1962
+			'4.6.0'
1963
+		);
1964
+		return self::get_pre_tax_subtotal($total_line_item);
1965
+	}
1966
+
1967
+
1968
+	/**
1969
+	 * @deprecated
1970
+	 * @param EE_Transaction $transaction
1971
+	 * @return EE_Line_Item
1972
+	 * @throws EE_Error
1973
+	 * @throws InvalidArgumentException
1974
+	 * @throws InvalidDataTypeException
1975
+	 * @throws InvalidInterfaceException
1976
+	 * @throws ReflectionException
1977
+	 */
1978
+	public static function create_default_total_line_item($transaction = null)
1979
+	{
1980
+		EE_Error::doing_it_wrong(
1981
+			'EEH_Line_Item::create_default_total_line_item()',
1982
+			sprintf(
1983
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1984
+				'EEH_Line_Item::create_total_line_item()'
1985
+			),
1986
+			'4.6.0'
1987
+		);
1988
+		return self::create_total_line_item($transaction);
1989
+	}
1990
+
1991
+
1992
+	/**
1993
+	 * @deprecated
1994
+	 * @param EE_Line_Item   $total_line_item
1995
+	 * @param EE_Transaction $transaction
1996
+	 * @return EE_Line_Item
1997
+	 * @throws EE_Error
1998
+	 * @throws InvalidArgumentException
1999
+	 * @throws InvalidDataTypeException
2000
+	 * @throws InvalidInterfaceException
2001
+	 * @throws ReflectionException
2002
+	 */
2003
+	public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2004
+	{
2005
+		EE_Error::doing_it_wrong(
2006
+			'EEH_Line_Item::create_default_tickets_subtotal()',
2007
+			sprintf(
2008
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2009
+				'EEH_Line_Item::create_pre_tax_subtotal()'
2010
+			),
2011
+			'4.6.0'
2012
+		);
2013
+		return self::create_pre_tax_subtotal($total_line_item, $transaction);
2014
+	}
2015
+
2016
+
2017
+	/**
2018
+	 * @deprecated
2019
+	 * @param EE_Line_Item   $total_line_item
2020
+	 * @param EE_Transaction $transaction
2021
+	 * @return EE_Line_Item
2022
+	 * @throws EE_Error
2023
+	 * @throws InvalidArgumentException
2024
+	 * @throws InvalidDataTypeException
2025
+	 * @throws InvalidInterfaceException
2026
+	 * @throws ReflectionException
2027
+	 */
2028
+	public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2029
+	{
2030
+		EE_Error::doing_it_wrong(
2031
+			'EEH_Line_Item::create_default_taxes_subtotal()',
2032
+			sprintf(
2033
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2034
+				'EEH_Line_Item::create_taxes_subtotal()'
2035
+			),
2036
+			'4.6.0'
2037
+		);
2038
+		return self::create_taxes_subtotal($total_line_item, $transaction);
2039
+	}
2040
+
2041
+
2042
+	/**
2043
+	 * @deprecated
2044
+	 * @param EE_Line_Item   $total_line_item
2045
+	 * @param EE_Transaction $transaction
2046
+	 * @return EE_Line_Item
2047
+	 * @throws EE_Error
2048
+	 * @throws InvalidArgumentException
2049
+	 * @throws InvalidDataTypeException
2050
+	 * @throws InvalidInterfaceException
2051
+	 * @throws ReflectionException
2052
+	 */
2053
+	public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2054
+	{
2055
+		EE_Error::doing_it_wrong(
2056
+			'EEH_Line_Item::create_default_event_subtotal()',
2057
+			sprintf(
2058
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2059
+				'EEH_Line_Item::create_event_subtotal()'
2060
+			),
2061
+			'4.6.0'
2062
+		);
2063
+		return self::create_event_subtotal($total_line_item, $transaction);
2064
+	}
2065 2065
 }
Please login to merge, or discard this patch.
core/db_models/EEM_Line_Item.model.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -324,8 +324,8 @@  discard block
 block discarded – undo
324 324
         );
325 325
         $query = $wpdb->prepare(
326 326
             'DELETE li
327
-				FROM ' . $this->table() . ' li
328
-				LEFT JOIN ' . EEM_Transaction::instance()->table() . ' t ON li.TXN_ID = t.TXN_ID
327
+				FROM ' . $this->table().' li
328
+				LEFT JOIN ' . EEM_Transaction::instance()->table().' t ON li.TXN_ID = t.TXN_ID
329 329
 				WHERE t.TXN_ID IS NULL AND li.LIN_timestamp < %s',
330 330
             // use GMT time because that's what TXN_timestamps are in
331 331
             date('Y-m-d H:i:s', time() - $time_to_leave_alone)
@@ -598,7 +598,7 @@  discard block
 block discarded – undo
598 598
      */
599 599
     public function getTicketLineItemsForExpiredCarts($timestamp = 0)
600 600
     {
601
-        if (! absint($timestamp)) {
601
+        if ( ! absint($timestamp)) {
602 602
             /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
603 603
             $session_lifespan = LoaderFactory::getLoader()->getShared(
604 604
                 'EventEspresso\core\domain\values\session\SessionLifespan'
Please login to merge, or discard this patch.
Indentation   +594 added lines, -594 removed lines patch added patch discarded remove patch
@@ -28,601 +28,601 @@
 block discarded – undo
28 28
 class EEM_Line_Item extends EEM_Base
29 29
 {
30 30
 
31
-    /**
32
-     * Tax sub-total is just the total of all the taxes, which should be children
33
-     * of this line item. There should only ever be one tax sub-total, and it should
34
-     * be a direct child of. Its quantity and LIN_unit_price = 1.
35
-     */
36
-    const type_tax_sub_total = 'tax-sub-total';
37
-
38
-    /**
39
-     * Tax line items indicate a tax applied to all the taxable line items.
40
-     * Should not have any children line items. Its LIN_unit_price = 0. Its LIN_percent is a percent, not a decimal
41
-     * (eg 10% tax = 10, not 0.1). Its LIN_total = LIN_unit_price * pre-tax-total. Quantity = 1.
42
-     */
43
-    const type_tax = 'tax';
44
-
45
-    /**
46
-     * Indicating individual items purchased, or discounts or surcharges.
47
-     * The sum of all the regular line items  plus the tax items should equal the grand total.
48
-     * Possible children are sub-line-items and cancellations.
49
-     * For flat items, LIN_unit_price * LIN_quantity = LIN_total. Its LIN_total is the sum of all the children
50
-     * LIN_totals. Its LIN_percent = 0.
51
-     * For percent items, its LIN_unit_price = 0. Its LIN_percent is a percent, not a decimal (eg 10% = 10, not 0.1).
52
-     * Its LIN_total is LIN_percent / 100 * sum of lower-priority sibling line items. Quantity = 1.
53
-     */
54
-    const type_line_item = 'line-item';
55
-
56
-    /**
57
-     * Line item indicating all the factors that make a single line item.
58
-     * Sub-line items should have NO children line items.
59
-     * For flat sub-items, their quantity should match their parent item, their LIN_unit_price should be this sub-item's
60
-     * contribution towards the price of ONE of their parent items, and its LIN_total should be
61
-     *  = LIN_quantity * LIN_unit_price. Its LIN_percent = 0.
62
-     * For percent sub-items, the quantity should be 1, LIN_unit_price should be 0, and its LIN_total should
63
-     * = LIN_percent / 100 * sum of lower-priority sibling line items..
64
-     */
65
-    const type_sub_line_item = 'sub-item';
66
-
67
-    /**
68
-     * Line item indicating a sub-total (eg total for an event, or pre-tax subtotal).
69
-     * Direct children should be event subtotals.
70
-     * Should have quantity of 1, and a LIN_total and LIN_unit_price of the sum of all its sub-items' LIN_totals.
71
-     */
72
-    const type_sub_total = 'sub-total';
73
-
74
-    /**
75
-     * Line item for the grand total of an order.
76
-     * Its direct children should be tax subtotals and (pre-tax) subtotals,
77
-     * and possibly a regular line item indicating a transaction-wide discount/surcharge.
78
-     * Should have a quantity of 1, a LIN_total and LIN_unit_price of the entire order's amount.
79
-     */
80
-    const type_total = 'total';
81
-
82
-    /**
83
-     * When a line item is cancelled, a sub-line-item of type 'cancellation'
84
-     * should be created, indicating the quantity that were cancelled
85
-     * (because a line item could have a quantity of 1, and its cancellation item
86
-     * could be for 3, indicating that originally 4 were purchased, but 3 have been
87
-     * cancelled, and only one remains).
88
-     * When items are refunded, a cancellation line item should be made, which points
89
-     * to teh payment model object which actually refunded the payment.
90
-     * Cancellations should NOT have any children line items; the should NOT affect
91
-     * any calculations, and are only meant as a record that cancellations have occurred.
92
-     * Their LIN_percent should be 0.
93
-     */
94
-    const type_cancellation = 'cancellation';
95
-
96
-    // various line item object types
97
-    const OBJ_TYPE_EVENT = 'Event';
98
-
99
-    const OBJ_TYPE_PRICE = 'Price';
100
-
101
-    const OBJ_TYPE_PROMOTION = 'Promotion';
102
-
103
-    const OBJ_TYPE_TICKET = 'Ticket';
104
-
105
-    const OBJ_TYPE_TRANSACTION = 'Transaction';
106
-
107
-    /**
108
-     * @var EEM_Line_Item $_instance
109
-     */
110
-    protected static $_instance;
111
-
112
-
113
-    /**
114
-     * private constructor to prevent direct creation
115
-     *
116
-     * @Constructor
117
-     * @param string $timezone string representing the timezone we want to set for returned Date Time Strings
118
-     *                         (and any incoming timezone data that gets saved).
119
-     *                         Note this just sends the timezone info to the date time model field objects.
120
-     *                         Default is NULL
121
-     *                         (and will be assumed using the set timezone in the 'timezone_string' wp option)
122
-     * @throws EE_Error
123
-     * @throws InvalidArgumentException
124
-     */
125
-    protected function __construct($timezone)
126
-    {
127
-        $this->singular_item = esc_html__('Line Item', 'event_espresso');
128
-        $this->plural_item = esc_html__('Line Items', 'event_espresso');
129
-
130
-        $this->_tables = array(
131
-            'Line_Item' => new EE_Primary_Table('esp_line_item', 'LIN_ID'),
132
-        );
133
-        $line_items_can_be_for = apply_filters(
134
-            'FHEE__EEM_Line_Item__line_items_can_be_for',
135
-            array('Ticket', 'Price', 'Event')
136
-        );
137
-        $this->_fields = array(
138
-            'Line_Item' => array(
139
-                'LIN_ID'         => new EE_Primary_Key_Int_Field(
140
-                    'LIN_ID',
141
-                    esc_html__('ID', 'event_espresso')
142
-                ),
143
-                'LIN_code'       => new EE_Slug_Field(
144
-                    'LIN_code',
145
-                    esc_html__('Code for index into Cart', 'event_espresso'),
146
-                    true
147
-                ),
148
-                'TXN_ID'         => new EE_Foreign_Key_Int_Field(
149
-                    'TXN_ID',
150
-                    esc_html__('Transaction ID', 'event_espresso'),
151
-                    true,
152
-                    null,
153
-                    'Transaction'
154
-                ),
155
-                'LIN_name'       => new EE_Full_HTML_Field(
156
-                    'LIN_name',
157
-                    esc_html__('Line Item Name', 'event_espresso'),
158
-                    false,
159
-                    ''
160
-                ),
161
-                'LIN_desc'       => new EE_Full_HTML_Field(
162
-                    'LIN_desc',
163
-                    esc_html__('Line Item Description', 'event_espresso'),
164
-                    true
165
-                ),
166
-                'LIN_unit_price' => new EE_Money_Field(
167
-                    'LIN_unit_price',
168
-                    esc_html__('Unit Price', 'event_espresso'),
169
-                    false,
170
-                    0
171
-                ),
172
-                'LIN_percent'    => new EE_Float_Field(
173
-                    'LIN_percent',
174
-                    esc_html__('Percent', 'event_espresso'),
175
-                    false,
176
-                    0
177
-                ),
178
-                'LIN_is_taxable' => new EE_Boolean_Field(
179
-                    'LIN_is_taxable',
180
-                    esc_html__('Taxable', 'event_espresso'),
181
-                    false,
182
-                    false
183
-                ),
184
-                'LIN_order'      => new EE_Integer_Field(
185
-                    'LIN_order',
186
-                    esc_html__('Order of Application towards total of parent', 'event_espresso'),
187
-                    false,
188
-                    1
189
-                ),
190
-                'LIN_total'      => new EE_Money_Field(
191
-                    'LIN_total',
192
-                    esc_html__('Total (unit price x quantity)', 'event_espresso'),
193
-                    false,
194
-                    0
195
-                ),
196
-                'LIN_quantity'   => new EE_Integer_Field(
197
-                    'LIN_quantity',
198
-                    esc_html__('Quantity', 'event_espresso'),
199
-                    true,
200
-                    1
201
-                ),
202
-                'LIN_parent'     => new EE_Integer_Field(
203
-                    'LIN_parent',
204
-                    esc_html__("Parent ID (this item goes towards that Line Item's total)", 'event_espresso'),
205
-                    true,
206
-                    null
207
-                ),
208
-                'LIN_type'       => new EE_Enum_Text_Field(
209
-                    'LIN_type',
210
-                    esc_html__('Type', 'event_espresso'),
211
-                    false,
212
-                    'line-item',
213
-                    array(
214
-                        self::type_line_item     => esc_html__('Line Item', 'event_espresso'),
215
-                        self::type_sub_line_item => esc_html__('Sub-Item', 'event_espresso'),
216
-                        self::type_sub_total     => esc_html__('Subtotal', 'event_espresso'),
217
-                        self::type_tax_sub_total => esc_html__('Tax Subtotal', 'event_espresso'),
218
-                        self::type_tax           => esc_html__('Tax', 'event_espresso'),
219
-                        self::type_total         => esc_html__('Total', 'event_espresso'),
220
-                        self::type_cancellation  => esc_html__('Cancellation', 'event_espresso'),
221
-                    )
222
-                ),
223
-                'OBJ_ID'         => new EE_Foreign_Key_Int_Field(
224
-                    'OBJ_ID',
225
-                    esc_html__('ID of Item purchased.', 'event_espresso'),
226
-                    true,
227
-                    null,
228
-                    $line_items_can_be_for
229
-                ),
230
-                'OBJ_type'       => new EE_Any_Foreign_Model_Name_Field(
231
-                    'OBJ_type',
232
-                    esc_html__('Model Name this Line Item is for', 'event_espresso'),
233
-                    true,
234
-                    null,
235
-                    $line_items_can_be_for
236
-                ),
237
-                'LIN_timestamp'  => new EE_Datetime_Field(
238
-                    'LIN_timestamp',
239
-                    esc_html__('When the line item was created', 'event_espresso'),
240
-                    false,
241
-                    EE_Datetime_Field::now,
242
-                    $timezone
243
-                ),
244
-            ),
245
-        );
246
-        $this->_model_relations = array(
247
-            'Transaction' => new EE_Belongs_To_Relation(),
248
-            'Ticket'      => new EE_Belongs_To_Any_Relation(),
249
-            'Price'       => new EE_Belongs_To_Any_Relation(),
250
-            'Event'       => new EE_Belongs_To_Any_Relation(),
251
-        );
252
-        $this->_model_chain_to_wp_user = 'Transaction.Registration.Event';
253
-        $this->_caps_slug = 'transactions';
254
-        parent::__construct($timezone);
255
-    }
256
-
257
-
258
-    /**
259
-     * Gets all the line items for this transaction of the given type
260
-     *
261
-     * @param string             $line_item_type like one of EEM_Line_Item::type_*
262
-     * @param EE_Transaction|int $transaction
263
-     * @return EE_Base_Class[]|EE_Line_Item[]
264
-     * @throws EE_Error
265
-     * @throws InvalidArgumentException
266
-     * @throws InvalidDataTypeException
267
-     * @throws InvalidInterfaceException
268
-     */
269
-    public function get_all_of_type_for_transaction($line_item_type, $transaction)
270
-    {
271
-        $transaction = EEM_Transaction::instance()->ensure_is_ID($transaction);
272
-        return $this->get_all(array(
273
-            array(
274
-                'LIN_type' => $line_item_type,
275
-                'TXN_ID'   => $transaction,
276
-            ),
277
-        ));
278
-    }
279
-
280
-
281
-    /**
282
-     * Gets all line items unrelated to tickets that are normal line items
283
-     * (eg shipping, promotions, and miscellaneous other stuff should probably fit in this category)
284
-     *
285
-     * @param EE_Transaction|int $transaction
286
-     * @return EE_Base_Class[]|EE_Line_Item[]
287
-     * @throws EE_Error
288
-     * @throws InvalidArgumentException
289
-     * @throws InvalidDataTypeException
290
-     * @throws InvalidInterfaceException
291
-     */
292
-    public function get_all_non_ticket_line_items_for_transaction($transaction)
293
-    {
294
-        $transaction = EEM_Transaction::instance()->ensure_is_ID($transaction);
295
-        return $this->get_all(array(
296
-            array(
297
-                'LIN_type' => self::type_line_item,
298
-                'TXN_ID'   => $transaction,
299
-                'OR'       => array(
300
-                    'OBJ_type*notticket' => array('!=', EEM_Line_Item::OBJ_TYPE_TICKET),
301
-                    'OBJ_type*null'      => array('IS_NULL'),
302
-                ),
303
-            ),
304
-        ));
305
-    }
306
-
307
-
308
-    /**
309
-     * Deletes line items with no transaction who have passed the transaction cutoff time.
310
-     * This needs to be very efficient
311
-     * because if there are spam bots afoot there will be LOTS of line items
312
-     *
313
-     * @return int count of how many deleted
314
-     * @throws EE_Error
315
-     * @throws InvalidArgumentException
316
-     * @throws InvalidDataTypeException
317
-     * @throws InvalidInterfaceException
318
-     */
319
-    public function delete_line_items_with_no_transaction()
320
-    {
321
-        /** @type WPDB $wpdb */
322
-        global $wpdb;
323
-        $time_to_leave_alone = apply_filters(
324
-            'FHEE__EEM_Line_Item__delete_line_items_with_no_transaction__time_to_leave_alone',
325
-            WEEK_IN_SECONDS
326
-        );
327
-        $query = $wpdb->prepare(
328
-            'DELETE li
31
+	/**
32
+	 * Tax sub-total is just the total of all the taxes, which should be children
33
+	 * of this line item. There should only ever be one tax sub-total, and it should
34
+	 * be a direct child of. Its quantity and LIN_unit_price = 1.
35
+	 */
36
+	const type_tax_sub_total = 'tax-sub-total';
37
+
38
+	/**
39
+	 * Tax line items indicate a tax applied to all the taxable line items.
40
+	 * Should not have any children line items. Its LIN_unit_price = 0. Its LIN_percent is a percent, not a decimal
41
+	 * (eg 10% tax = 10, not 0.1). Its LIN_total = LIN_unit_price * pre-tax-total. Quantity = 1.
42
+	 */
43
+	const type_tax = 'tax';
44
+
45
+	/**
46
+	 * Indicating individual items purchased, or discounts or surcharges.
47
+	 * The sum of all the regular line items  plus the tax items should equal the grand total.
48
+	 * Possible children are sub-line-items and cancellations.
49
+	 * For flat items, LIN_unit_price * LIN_quantity = LIN_total. Its LIN_total is the sum of all the children
50
+	 * LIN_totals. Its LIN_percent = 0.
51
+	 * For percent items, its LIN_unit_price = 0. Its LIN_percent is a percent, not a decimal (eg 10% = 10, not 0.1).
52
+	 * Its LIN_total is LIN_percent / 100 * sum of lower-priority sibling line items. Quantity = 1.
53
+	 */
54
+	const type_line_item = 'line-item';
55
+
56
+	/**
57
+	 * Line item indicating all the factors that make a single line item.
58
+	 * Sub-line items should have NO children line items.
59
+	 * For flat sub-items, their quantity should match their parent item, their LIN_unit_price should be this sub-item's
60
+	 * contribution towards the price of ONE of their parent items, and its LIN_total should be
61
+	 *  = LIN_quantity * LIN_unit_price. Its LIN_percent = 0.
62
+	 * For percent sub-items, the quantity should be 1, LIN_unit_price should be 0, and its LIN_total should
63
+	 * = LIN_percent / 100 * sum of lower-priority sibling line items..
64
+	 */
65
+	const type_sub_line_item = 'sub-item';
66
+
67
+	/**
68
+	 * Line item indicating a sub-total (eg total for an event, or pre-tax subtotal).
69
+	 * Direct children should be event subtotals.
70
+	 * Should have quantity of 1, and a LIN_total and LIN_unit_price of the sum of all its sub-items' LIN_totals.
71
+	 */
72
+	const type_sub_total = 'sub-total';
73
+
74
+	/**
75
+	 * Line item for the grand total of an order.
76
+	 * Its direct children should be tax subtotals and (pre-tax) subtotals,
77
+	 * and possibly a regular line item indicating a transaction-wide discount/surcharge.
78
+	 * Should have a quantity of 1, a LIN_total and LIN_unit_price of the entire order's amount.
79
+	 */
80
+	const type_total = 'total';
81
+
82
+	/**
83
+	 * When a line item is cancelled, a sub-line-item of type 'cancellation'
84
+	 * should be created, indicating the quantity that were cancelled
85
+	 * (because a line item could have a quantity of 1, and its cancellation item
86
+	 * could be for 3, indicating that originally 4 were purchased, but 3 have been
87
+	 * cancelled, and only one remains).
88
+	 * When items are refunded, a cancellation line item should be made, which points
89
+	 * to teh payment model object which actually refunded the payment.
90
+	 * Cancellations should NOT have any children line items; the should NOT affect
91
+	 * any calculations, and are only meant as a record that cancellations have occurred.
92
+	 * Their LIN_percent should be 0.
93
+	 */
94
+	const type_cancellation = 'cancellation';
95
+
96
+	// various line item object types
97
+	const OBJ_TYPE_EVENT = 'Event';
98
+
99
+	const OBJ_TYPE_PRICE = 'Price';
100
+
101
+	const OBJ_TYPE_PROMOTION = 'Promotion';
102
+
103
+	const OBJ_TYPE_TICKET = 'Ticket';
104
+
105
+	const OBJ_TYPE_TRANSACTION = 'Transaction';
106
+
107
+	/**
108
+	 * @var EEM_Line_Item $_instance
109
+	 */
110
+	protected static $_instance;
111
+
112
+
113
+	/**
114
+	 * private constructor to prevent direct creation
115
+	 *
116
+	 * @Constructor
117
+	 * @param string $timezone string representing the timezone we want to set for returned Date Time Strings
118
+	 *                         (and any incoming timezone data that gets saved).
119
+	 *                         Note this just sends the timezone info to the date time model field objects.
120
+	 *                         Default is NULL
121
+	 *                         (and will be assumed using the set timezone in the 'timezone_string' wp option)
122
+	 * @throws EE_Error
123
+	 * @throws InvalidArgumentException
124
+	 */
125
+	protected function __construct($timezone)
126
+	{
127
+		$this->singular_item = esc_html__('Line Item', 'event_espresso');
128
+		$this->plural_item = esc_html__('Line Items', 'event_espresso');
129
+
130
+		$this->_tables = array(
131
+			'Line_Item' => new EE_Primary_Table('esp_line_item', 'LIN_ID'),
132
+		);
133
+		$line_items_can_be_for = apply_filters(
134
+			'FHEE__EEM_Line_Item__line_items_can_be_for',
135
+			array('Ticket', 'Price', 'Event')
136
+		);
137
+		$this->_fields = array(
138
+			'Line_Item' => array(
139
+				'LIN_ID'         => new EE_Primary_Key_Int_Field(
140
+					'LIN_ID',
141
+					esc_html__('ID', 'event_espresso')
142
+				),
143
+				'LIN_code'       => new EE_Slug_Field(
144
+					'LIN_code',
145
+					esc_html__('Code for index into Cart', 'event_espresso'),
146
+					true
147
+				),
148
+				'TXN_ID'         => new EE_Foreign_Key_Int_Field(
149
+					'TXN_ID',
150
+					esc_html__('Transaction ID', 'event_espresso'),
151
+					true,
152
+					null,
153
+					'Transaction'
154
+				),
155
+				'LIN_name'       => new EE_Full_HTML_Field(
156
+					'LIN_name',
157
+					esc_html__('Line Item Name', 'event_espresso'),
158
+					false,
159
+					''
160
+				),
161
+				'LIN_desc'       => new EE_Full_HTML_Field(
162
+					'LIN_desc',
163
+					esc_html__('Line Item Description', 'event_espresso'),
164
+					true
165
+				),
166
+				'LIN_unit_price' => new EE_Money_Field(
167
+					'LIN_unit_price',
168
+					esc_html__('Unit Price', 'event_espresso'),
169
+					false,
170
+					0
171
+				),
172
+				'LIN_percent'    => new EE_Float_Field(
173
+					'LIN_percent',
174
+					esc_html__('Percent', 'event_espresso'),
175
+					false,
176
+					0
177
+				),
178
+				'LIN_is_taxable' => new EE_Boolean_Field(
179
+					'LIN_is_taxable',
180
+					esc_html__('Taxable', 'event_espresso'),
181
+					false,
182
+					false
183
+				),
184
+				'LIN_order'      => new EE_Integer_Field(
185
+					'LIN_order',
186
+					esc_html__('Order of Application towards total of parent', 'event_espresso'),
187
+					false,
188
+					1
189
+				),
190
+				'LIN_total'      => new EE_Money_Field(
191
+					'LIN_total',
192
+					esc_html__('Total (unit price x quantity)', 'event_espresso'),
193
+					false,
194
+					0
195
+				),
196
+				'LIN_quantity'   => new EE_Integer_Field(
197
+					'LIN_quantity',
198
+					esc_html__('Quantity', 'event_espresso'),
199
+					true,
200
+					1
201
+				),
202
+				'LIN_parent'     => new EE_Integer_Field(
203
+					'LIN_parent',
204
+					esc_html__("Parent ID (this item goes towards that Line Item's total)", 'event_espresso'),
205
+					true,
206
+					null
207
+				),
208
+				'LIN_type'       => new EE_Enum_Text_Field(
209
+					'LIN_type',
210
+					esc_html__('Type', 'event_espresso'),
211
+					false,
212
+					'line-item',
213
+					array(
214
+						self::type_line_item     => esc_html__('Line Item', 'event_espresso'),
215
+						self::type_sub_line_item => esc_html__('Sub-Item', 'event_espresso'),
216
+						self::type_sub_total     => esc_html__('Subtotal', 'event_espresso'),
217
+						self::type_tax_sub_total => esc_html__('Tax Subtotal', 'event_espresso'),
218
+						self::type_tax           => esc_html__('Tax', 'event_espresso'),
219
+						self::type_total         => esc_html__('Total', 'event_espresso'),
220
+						self::type_cancellation  => esc_html__('Cancellation', 'event_espresso'),
221
+					)
222
+				),
223
+				'OBJ_ID'         => new EE_Foreign_Key_Int_Field(
224
+					'OBJ_ID',
225
+					esc_html__('ID of Item purchased.', 'event_espresso'),
226
+					true,
227
+					null,
228
+					$line_items_can_be_for
229
+				),
230
+				'OBJ_type'       => new EE_Any_Foreign_Model_Name_Field(
231
+					'OBJ_type',
232
+					esc_html__('Model Name this Line Item is for', 'event_espresso'),
233
+					true,
234
+					null,
235
+					$line_items_can_be_for
236
+				),
237
+				'LIN_timestamp'  => new EE_Datetime_Field(
238
+					'LIN_timestamp',
239
+					esc_html__('When the line item was created', 'event_espresso'),
240
+					false,
241
+					EE_Datetime_Field::now,
242
+					$timezone
243
+				),
244
+			),
245
+		);
246
+		$this->_model_relations = array(
247
+			'Transaction' => new EE_Belongs_To_Relation(),
248
+			'Ticket'      => new EE_Belongs_To_Any_Relation(),
249
+			'Price'       => new EE_Belongs_To_Any_Relation(),
250
+			'Event'       => new EE_Belongs_To_Any_Relation(),
251
+		);
252
+		$this->_model_chain_to_wp_user = 'Transaction.Registration.Event';
253
+		$this->_caps_slug = 'transactions';
254
+		parent::__construct($timezone);
255
+	}
256
+
257
+
258
+	/**
259
+	 * Gets all the line items for this transaction of the given type
260
+	 *
261
+	 * @param string             $line_item_type like one of EEM_Line_Item::type_*
262
+	 * @param EE_Transaction|int $transaction
263
+	 * @return EE_Base_Class[]|EE_Line_Item[]
264
+	 * @throws EE_Error
265
+	 * @throws InvalidArgumentException
266
+	 * @throws InvalidDataTypeException
267
+	 * @throws InvalidInterfaceException
268
+	 */
269
+	public function get_all_of_type_for_transaction($line_item_type, $transaction)
270
+	{
271
+		$transaction = EEM_Transaction::instance()->ensure_is_ID($transaction);
272
+		return $this->get_all(array(
273
+			array(
274
+				'LIN_type' => $line_item_type,
275
+				'TXN_ID'   => $transaction,
276
+			),
277
+		));
278
+	}
279
+
280
+
281
+	/**
282
+	 * Gets all line items unrelated to tickets that are normal line items
283
+	 * (eg shipping, promotions, and miscellaneous other stuff should probably fit in this category)
284
+	 *
285
+	 * @param EE_Transaction|int $transaction
286
+	 * @return EE_Base_Class[]|EE_Line_Item[]
287
+	 * @throws EE_Error
288
+	 * @throws InvalidArgumentException
289
+	 * @throws InvalidDataTypeException
290
+	 * @throws InvalidInterfaceException
291
+	 */
292
+	public function get_all_non_ticket_line_items_for_transaction($transaction)
293
+	{
294
+		$transaction = EEM_Transaction::instance()->ensure_is_ID($transaction);
295
+		return $this->get_all(array(
296
+			array(
297
+				'LIN_type' => self::type_line_item,
298
+				'TXN_ID'   => $transaction,
299
+				'OR'       => array(
300
+					'OBJ_type*notticket' => array('!=', EEM_Line_Item::OBJ_TYPE_TICKET),
301
+					'OBJ_type*null'      => array('IS_NULL'),
302
+				),
303
+			),
304
+		));
305
+	}
306
+
307
+
308
+	/**
309
+	 * Deletes line items with no transaction who have passed the transaction cutoff time.
310
+	 * This needs to be very efficient
311
+	 * because if there are spam bots afoot there will be LOTS of line items
312
+	 *
313
+	 * @return int count of how many deleted
314
+	 * @throws EE_Error
315
+	 * @throws InvalidArgumentException
316
+	 * @throws InvalidDataTypeException
317
+	 * @throws InvalidInterfaceException
318
+	 */
319
+	public function delete_line_items_with_no_transaction()
320
+	{
321
+		/** @type WPDB $wpdb */
322
+		global $wpdb;
323
+		$time_to_leave_alone = apply_filters(
324
+			'FHEE__EEM_Line_Item__delete_line_items_with_no_transaction__time_to_leave_alone',
325
+			WEEK_IN_SECONDS
326
+		);
327
+		$query = $wpdb->prepare(
328
+			'DELETE li
329 329
 				FROM ' . $this->table() . ' li
330 330
 				LEFT JOIN ' . EEM_Transaction::instance()->table() . ' t ON li.TXN_ID = t.TXN_ID
331 331
 				WHERE t.TXN_ID IS NULL AND li.LIN_timestamp < %s',
332
-            // use GMT time because that's what TXN_timestamps are in
333
-            date('Y-m-d H:i:s', time() - $time_to_leave_alone)
334
-        );
335
-        return $wpdb->query($query);
336
-    }
337
-
338
-
339
-    /**
340
-     * get_line_item_for_transaction_object
341
-     * Gets a transaction's line item record for a specific object such as a EE_Event or EE_Ticket
342
-     *
343
-     * @param int           $TXN_ID
344
-     * @param EE_Base_Class $object
345
-     * @return EE_Base_Class[]|EE_Line_Item[]
346
-     * @throws EE_Error
347
-     * @throws InvalidArgumentException
348
-     * @throws InvalidDataTypeException
349
-     * @throws InvalidInterfaceException
350
-     * @throws ReflectionException
351
-     */
352
-    public function get_line_item_for_transaction_object($TXN_ID, EE_Base_Class $object)
353
-    {
354
-        return $this->get_all(array(
355
-            array(
356
-                'TXN_ID'   => $TXN_ID,
357
-                'OBJ_type' => str_replace('EE_', '', get_class($object)),
358
-                'OBJ_ID'   => $object->ID(),
359
-            ),
360
-        ));
361
-    }
362
-
363
-
364
-    /**
365
-     * get_object_line_items_for_transaction
366
-     * Gets all of the the object line items for a transaction, based on an object type plus an array of object IDs
367
-     *
368
-     * @param int    $TXN_ID
369
-     * @param string $OBJ_type
370
-     * @param array  $OBJ_IDs
371
-     * @return EE_Base_Class[]|EE_Line_Item[]
372
-     * @throws EE_Error
373
-     */
374
-    public function get_object_line_items_for_transaction(
375
-        $TXN_ID,
376
-        $OBJ_type = EEM_Line_Item::OBJ_TYPE_EVENT,
377
-        $OBJ_IDs = array()
378
-    ) {
379
-        $query_params = array(
380
-            'OBJ_type' => $OBJ_type,
381
-            // if incoming $OBJ_IDs is an array, then make sure it is formatted correctly for the query
382
-            'OBJ_ID'   => is_array($OBJ_IDs) && ! isset($OBJ_IDs['IN']) ? array('IN', $OBJ_IDs) : $OBJ_IDs,
383
-        );
384
-        if ($TXN_ID) {
385
-            $query_params['TXN_ID'] = $TXN_ID;
386
-        }
387
-        return $this->get_all(array($query_params));
388
-    }
389
-
390
-
391
-    /**
392
-     * get_all_ticket_line_items_for_transaction
393
-     *
394
-     * @param EE_Transaction $transaction
395
-     * @return EE_Base_Class[]|EE_Line_Item[]
396
-     * @throws EE_Error
397
-     * @throws InvalidArgumentException
398
-     * @throws InvalidDataTypeException
399
-     * @throws InvalidInterfaceException
400
-     * @throws ReflectionException
401
-     */
402
-    public function get_all_ticket_line_items_for_transaction(EE_Transaction $transaction)
403
-    {
404
-        return $this->get_all(array(
405
-            array(
406
-                'TXN_ID'   => $transaction->ID(),
407
-                'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TICKET,
408
-            ),
409
-        ));
410
-    }
411
-
412
-
413
-    /**
414
-     * get_ticket_line_item_for_transaction
415
-     *
416
-     * @param int $TXN_ID
417
-     * @param int $TKT_ID
418
-     * @return EE_Base_Class|EE_Line_Item|EE_Soft_Delete_Base_Class|NULL
419
-     * @throws EE_Error
420
-     * @throws InvalidArgumentException
421
-     * @throws InvalidDataTypeException
422
-     * @throws InvalidInterfaceException
423
-     */
424
-    public function get_ticket_line_item_for_transaction($TXN_ID, $TKT_ID)
425
-    {
426
-        return $this->get_one(array(
427
-            array(
428
-                'TXN_ID'   => EEM_Transaction::instance()->ensure_is_ID($TXN_ID),
429
-                'OBJ_ID'   => $TKT_ID,
430
-                'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TICKET,
431
-            ),
432
-        ));
433
-    }
434
-
435
-
436
-    /**
437
-     * get_existing_promotion_line_item
438
-     * searches the cart for existing line items for the specified promotion
439
-     *
440
-     * @since 1.0.0
441
-     * @param EE_Line_Item $parent_line_item
442
-     * @param EE_Promotion $promotion
443
-     * @return EE_Base_Class|EE_Line_Item|EE_Soft_Delete_Base_Class|NULL
444
-     * @throws EE_Error
445
-     * @throws InvalidArgumentException
446
-     * @throws InvalidDataTypeException
447
-     * @throws InvalidInterfaceException
448
-     * @throws ReflectionException
449
-     */
450
-    public function get_existing_promotion_line_item(EE_Line_Item $parent_line_item, EE_Promotion $promotion)
451
-    {
452
-        return $this->get_one(array(
453
-            array(
454
-                'TXN_ID'     => $parent_line_item->TXN_ID(),
455
-                'LIN_parent' => $parent_line_item->ID(),
456
-                'OBJ_type'   => EEM_Line_Item::OBJ_TYPE_PROMOTION,
457
-                'OBJ_ID'     => $promotion->ID(),
458
-            ),
459
-        ));
460
-    }
461
-
462
-
463
-    /**
464
-     * get_all_promotion_line_items
465
-     * searches the cart for any and all existing promotion line items
466
-     *
467
-     * @since   1.0.0
468
-     * @param EE_Line_Item $parent_line_item
469
-     * @return EE_Base_Class[]|EE_Line_Item[]
470
-     * @throws EE_Error
471
-     * @throws InvalidArgumentException
472
-     * @throws InvalidDataTypeException
473
-     * @throws InvalidInterfaceException
474
-     * @throws ReflectionException
475
-     */
476
-    public function get_all_promotion_line_items(EE_Line_Item $parent_line_item)
477
-    {
478
-        return $this->get_all(array(
479
-            array(
480
-                'TXN_ID'     => $parent_line_item->TXN_ID(),
481
-                'LIN_parent' => $parent_line_item->ID(),
482
-                'OBJ_type'   => EEM_Line_Item::OBJ_TYPE_PROMOTION,
483
-            ),
484
-        ));
485
-    }
486
-
487
-
488
-    /**
489
-     * Gets the registration's corresponding line item.
490
-     * Note: basically does NOT support having multiple line items for a single ticket,
491
-     * which would happen if some of the registrations had a price modifier while others didn't.
492
-     * In order to support that, we'd probably need a LIN_ID on registrations or something.
493
-     *
494
-     * @param EE_Registration $registration
495
-     * @return EE_Base_Class|EE_Line_ITem|EE_Soft_Delete_Base_Class|NULL
496
-     * @throws EE_Error
497
-     */
498
-    public function get_line_item_for_registration(EE_Registration $registration)
499
-    {
500
-        return $this->get_one($this->line_item_for_registration_query_params($registration));
501
-    }
502
-
503
-
504
-    /**
505
-     * Gets the query params used to retrieve a specific line item for the given registration
506
-     *
507
-     * @param EE_Registration $registration
508
-     * @param array           $original_query_params any extra query params you'd like to be merged with
509
-     * @return array @see
510
-     *      https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
511
-     * @throws EE_Error
512
-     */
513
-    public function line_item_for_registration_query_params(
514
-        EE_Registration $registration,
515
-        $original_query_params = array()
516
-    ) {
517
-        return array_replace_recursive($original_query_params, array(
518
-            array(
519
-                'OBJ_ID'   => $registration->ticket_ID(),
520
-                'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TICKET,
521
-                'TXN_ID'   => $registration->transaction_ID(),
522
-            ),
523
-        ));
524
-    }
525
-
526
-
527
-    /**
528
-     * @return EE_Base_Class[]|EE_Line_Item[]
529
-     * @throws InvalidInterfaceException
530
-     * @throws InvalidDataTypeException
531
-     * @throws EE_Error
532
-     * @throws InvalidArgumentException
533
-     */
534
-    public function get_total_line_items_with_no_transaction()
535
-    {
536
-        return $this->get_total_line_items_for_carts();
537
-    }
538
-
539
-
540
-    /**
541
-     * @return EE_Base_Class[]|EE_Line_Item[]
542
-     * @throws InvalidInterfaceException
543
-     * @throws InvalidDataTypeException
544
-     * @throws EE_Error
545
-     * @throws InvalidArgumentException
546
-     */
547
-    public function get_total_line_items_for_active_carts()
548
-    {
549
-        return $this->get_total_line_items_for_carts(false);
550
-    }
551
-
552
-
553
-    /**
554
-     * @return EE_Base_Class[]|EE_Line_Item[]
555
-     * @throws InvalidInterfaceException
556
-     * @throws InvalidDataTypeException
557
-     * @throws EE_Error
558
-     * @throws InvalidArgumentException
559
-     */
560
-    public function get_total_line_items_for_expired_carts()
561
-    {
562
-        return $this->get_total_line_items_for_carts(true);
563
-    }
564
-
565
-
566
-    /**
567
-     * Returns an array of grand total line items where the TXN_ID is 0.
568
-     * If $expired is set to true, then only line items for expired sessions will be returned.
569
-     * If $expired is set to false, then only line items for active sessions will be returned.
570
-     *
571
-     * @param null $expired
572
-     * @return EE_Base_Class[]|EE_Line_Item[]
573
-     * @throws EE_Error
574
-     * @throws InvalidArgumentException
575
-     * @throws InvalidDataTypeException
576
-     * @throws InvalidInterfaceException
577
-     */
578
-    private function get_total_line_items_for_carts($expired = null)
579
-    {
580
-        $where_params = array(
581
-            'TXN_ID'   => 0,
582
-            'LIN_type' => 'total',
583
-        );
584
-        if ($expired !== null) {
585
-            /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
586
-            $session_lifespan = LoaderFactory::getLoader()->getShared(
587
-                'EventEspresso\core\domain\values\session\SessionLifespan'
588
-            );
589
-            $where_params['LIN_timestamp'] = array(
590
-                $expired ? '<=' : '>',
591
-                $session_lifespan->expiration(),
592
-            );
593
-        }
594
-        return $this->get_all(array($where_params));
595
-    }
596
-
597
-
598
-    /**
599
-     * Returns an array of ticket total line items where the TXN_ID is 0
600
-     * AND the timestamp is older than the session lifespan.
601
-     *
602
-     * @param int $timestamp
603
-     * @return EE_Base_Class[]|EE_Line_Item[]
604
-     * @throws EE_Error
605
-     * @throws InvalidArgumentException
606
-     * @throws InvalidDataTypeException
607
-     * @throws InvalidInterfaceException
608
-     */
609
-    public function getTicketLineItemsForExpiredCarts($timestamp = 0)
610
-    {
611
-        if (! absint($timestamp)) {
612
-            /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
613
-            $session_lifespan = LoaderFactory::getLoader()->getShared(
614
-                'EventEspresso\core\domain\values\session\SessionLifespan'
615
-            );
616
-            $timestamp = $session_lifespan->expiration();
617
-        }
618
-        return $this->get_all(
619
-            array(
620
-                array(
621
-                    'TXN_ID'        => 0,
622
-                    'OBJ_type'      => EEM_Line_Item::OBJ_TYPE_TICKET,
623
-                    'LIN_timestamp' => array('<=', $timestamp),
624
-                ),
625
-            )
626
-        );
627
-    }
332
+			// use GMT time because that's what TXN_timestamps are in
333
+			date('Y-m-d H:i:s', time() - $time_to_leave_alone)
334
+		);
335
+		return $wpdb->query($query);
336
+	}
337
+
338
+
339
+	/**
340
+	 * get_line_item_for_transaction_object
341
+	 * Gets a transaction's line item record for a specific object such as a EE_Event or EE_Ticket
342
+	 *
343
+	 * @param int           $TXN_ID
344
+	 * @param EE_Base_Class $object
345
+	 * @return EE_Base_Class[]|EE_Line_Item[]
346
+	 * @throws EE_Error
347
+	 * @throws InvalidArgumentException
348
+	 * @throws InvalidDataTypeException
349
+	 * @throws InvalidInterfaceException
350
+	 * @throws ReflectionException
351
+	 */
352
+	public function get_line_item_for_transaction_object($TXN_ID, EE_Base_Class $object)
353
+	{
354
+		return $this->get_all(array(
355
+			array(
356
+				'TXN_ID'   => $TXN_ID,
357
+				'OBJ_type' => str_replace('EE_', '', get_class($object)),
358
+				'OBJ_ID'   => $object->ID(),
359
+			),
360
+		));
361
+	}
362
+
363
+
364
+	/**
365
+	 * get_object_line_items_for_transaction
366
+	 * Gets all of the the object line items for a transaction, based on an object type plus an array of object IDs
367
+	 *
368
+	 * @param int    $TXN_ID
369
+	 * @param string $OBJ_type
370
+	 * @param array  $OBJ_IDs
371
+	 * @return EE_Base_Class[]|EE_Line_Item[]
372
+	 * @throws EE_Error
373
+	 */
374
+	public function get_object_line_items_for_transaction(
375
+		$TXN_ID,
376
+		$OBJ_type = EEM_Line_Item::OBJ_TYPE_EVENT,
377
+		$OBJ_IDs = array()
378
+	) {
379
+		$query_params = array(
380
+			'OBJ_type' => $OBJ_type,
381
+			// if incoming $OBJ_IDs is an array, then make sure it is formatted correctly for the query
382
+			'OBJ_ID'   => is_array($OBJ_IDs) && ! isset($OBJ_IDs['IN']) ? array('IN', $OBJ_IDs) : $OBJ_IDs,
383
+		);
384
+		if ($TXN_ID) {
385
+			$query_params['TXN_ID'] = $TXN_ID;
386
+		}
387
+		return $this->get_all(array($query_params));
388
+	}
389
+
390
+
391
+	/**
392
+	 * get_all_ticket_line_items_for_transaction
393
+	 *
394
+	 * @param EE_Transaction $transaction
395
+	 * @return EE_Base_Class[]|EE_Line_Item[]
396
+	 * @throws EE_Error
397
+	 * @throws InvalidArgumentException
398
+	 * @throws InvalidDataTypeException
399
+	 * @throws InvalidInterfaceException
400
+	 * @throws ReflectionException
401
+	 */
402
+	public function get_all_ticket_line_items_for_transaction(EE_Transaction $transaction)
403
+	{
404
+		return $this->get_all(array(
405
+			array(
406
+				'TXN_ID'   => $transaction->ID(),
407
+				'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TICKET,
408
+			),
409
+		));
410
+	}
411
+
412
+
413
+	/**
414
+	 * get_ticket_line_item_for_transaction
415
+	 *
416
+	 * @param int $TXN_ID
417
+	 * @param int $TKT_ID
418
+	 * @return EE_Base_Class|EE_Line_Item|EE_Soft_Delete_Base_Class|NULL
419
+	 * @throws EE_Error
420
+	 * @throws InvalidArgumentException
421
+	 * @throws InvalidDataTypeException
422
+	 * @throws InvalidInterfaceException
423
+	 */
424
+	public function get_ticket_line_item_for_transaction($TXN_ID, $TKT_ID)
425
+	{
426
+		return $this->get_one(array(
427
+			array(
428
+				'TXN_ID'   => EEM_Transaction::instance()->ensure_is_ID($TXN_ID),
429
+				'OBJ_ID'   => $TKT_ID,
430
+				'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TICKET,
431
+			),
432
+		));
433
+	}
434
+
435
+
436
+	/**
437
+	 * get_existing_promotion_line_item
438
+	 * searches the cart for existing line items for the specified promotion
439
+	 *
440
+	 * @since 1.0.0
441
+	 * @param EE_Line_Item $parent_line_item
442
+	 * @param EE_Promotion $promotion
443
+	 * @return EE_Base_Class|EE_Line_Item|EE_Soft_Delete_Base_Class|NULL
444
+	 * @throws EE_Error
445
+	 * @throws InvalidArgumentException
446
+	 * @throws InvalidDataTypeException
447
+	 * @throws InvalidInterfaceException
448
+	 * @throws ReflectionException
449
+	 */
450
+	public function get_existing_promotion_line_item(EE_Line_Item $parent_line_item, EE_Promotion $promotion)
451
+	{
452
+		return $this->get_one(array(
453
+			array(
454
+				'TXN_ID'     => $parent_line_item->TXN_ID(),
455
+				'LIN_parent' => $parent_line_item->ID(),
456
+				'OBJ_type'   => EEM_Line_Item::OBJ_TYPE_PROMOTION,
457
+				'OBJ_ID'     => $promotion->ID(),
458
+			),
459
+		));
460
+	}
461
+
462
+
463
+	/**
464
+	 * get_all_promotion_line_items
465
+	 * searches the cart for any and all existing promotion line items
466
+	 *
467
+	 * @since   1.0.0
468
+	 * @param EE_Line_Item $parent_line_item
469
+	 * @return EE_Base_Class[]|EE_Line_Item[]
470
+	 * @throws EE_Error
471
+	 * @throws InvalidArgumentException
472
+	 * @throws InvalidDataTypeException
473
+	 * @throws InvalidInterfaceException
474
+	 * @throws ReflectionException
475
+	 */
476
+	public function get_all_promotion_line_items(EE_Line_Item $parent_line_item)
477
+	{
478
+		return $this->get_all(array(
479
+			array(
480
+				'TXN_ID'     => $parent_line_item->TXN_ID(),
481
+				'LIN_parent' => $parent_line_item->ID(),
482
+				'OBJ_type'   => EEM_Line_Item::OBJ_TYPE_PROMOTION,
483
+			),
484
+		));
485
+	}
486
+
487
+
488
+	/**
489
+	 * Gets the registration's corresponding line item.
490
+	 * Note: basically does NOT support having multiple line items for a single ticket,
491
+	 * which would happen if some of the registrations had a price modifier while others didn't.
492
+	 * In order to support that, we'd probably need a LIN_ID on registrations or something.
493
+	 *
494
+	 * @param EE_Registration $registration
495
+	 * @return EE_Base_Class|EE_Line_ITem|EE_Soft_Delete_Base_Class|NULL
496
+	 * @throws EE_Error
497
+	 */
498
+	public function get_line_item_for_registration(EE_Registration $registration)
499
+	{
500
+		return $this->get_one($this->line_item_for_registration_query_params($registration));
501
+	}
502
+
503
+
504
+	/**
505
+	 * Gets the query params used to retrieve a specific line item for the given registration
506
+	 *
507
+	 * @param EE_Registration $registration
508
+	 * @param array           $original_query_params any extra query params you'd like to be merged with
509
+	 * @return array @see
510
+	 *      https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
511
+	 * @throws EE_Error
512
+	 */
513
+	public function line_item_for_registration_query_params(
514
+		EE_Registration $registration,
515
+		$original_query_params = array()
516
+	) {
517
+		return array_replace_recursive($original_query_params, array(
518
+			array(
519
+				'OBJ_ID'   => $registration->ticket_ID(),
520
+				'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TICKET,
521
+				'TXN_ID'   => $registration->transaction_ID(),
522
+			),
523
+		));
524
+	}
525
+
526
+
527
+	/**
528
+	 * @return EE_Base_Class[]|EE_Line_Item[]
529
+	 * @throws InvalidInterfaceException
530
+	 * @throws InvalidDataTypeException
531
+	 * @throws EE_Error
532
+	 * @throws InvalidArgumentException
533
+	 */
534
+	public function get_total_line_items_with_no_transaction()
535
+	{
536
+		return $this->get_total_line_items_for_carts();
537
+	}
538
+
539
+
540
+	/**
541
+	 * @return EE_Base_Class[]|EE_Line_Item[]
542
+	 * @throws InvalidInterfaceException
543
+	 * @throws InvalidDataTypeException
544
+	 * @throws EE_Error
545
+	 * @throws InvalidArgumentException
546
+	 */
547
+	public function get_total_line_items_for_active_carts()
548
+	{
549
+		return $this->get_total_line_items_for_carts(false);
550
+	}
551
+
552
+
553
+	/**
554
+	 * @return EE_Base_Class[]|EE_Line_Item[]
555
+	 * @throws InvalidInterfaceException
556
+	 * @throws InvalidDataTypeException
557
+	 * @throws EE_Error
558
+	 * @throws InvalidArgumentException
559
+	 */
560
+	public function get_total_line_items_for_expired_carts()
561
+	{
562
+		return $this->get_total_line_items_for_carts(true);
563
+	}
564
+
565
+
566
+	/**
567
+	 * Returns an array of grand total line items where the TXN_ID is 0.
568
+	 * If $expired is set to true, then only line items for expired sessions will be returned.
569
+	 * If $expired is set to false, then only line items for active sessions will be returned.
570
+	 *
571
+	 * @param null $expired
572
+	 * @return EE_Base_Class[]|EE_Line_Item[]
573
+	 * @throws EE_Error
574
+	 * @throws InvalidArgumentException
575
+	 * @throws InvalidDataTypeException
576
+	 * @throws InvalidInterfaceException
577
+	 */
578
+	private function get_total_line_items_for_carts($expired = null)
579
+	{
580
+		$where_params = array(
581
+			'TXN_ID'   => 0,
582
+			'LIN_type' => 'total',
583
+		);
584
+		if ($expired !== null) {
585
+			/** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
586
+			$session_lifespan = LoaderFactory::getLoader()->getShared(
587
+				'EventEspresso\core\domain\values\session\SessionLifespan'
588
+			);
589
+			$where_params['LIN_timestamp'] = array(
590
+				$expired ? '<=' : '>',
591
+				$session_lifespan->expiration(),
592
+			);
593
+		}
594
+		return $this->get_all(array($where_params));
595
+	}
596
+
597
+
598
+	/**
599
+	 * Returns an array of ticket total line items where the TXN_ID is 0
600
+	 * AND the timestamp is older than the session lifespan.
601
+	 *
602
+	 * @param int $timestamp
603
+	 * @return EE_Base_Class[]|EE_Line_Item[]
604
+	 * @throws EE_Error
605
+	 * @throws InvalidArgumentException
606
+	 * @throws InvalidDataTypeException
607
+	 * @throws InvalidInterfaceException
608
+	 */
609
+	public function getTicketLineItemsForExpiredCarts($timestamp = 0)
610
+	{
611
+		if (! absint($timestamp)) {
612
+			/** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
613
+			$session_lifespan = LoaderFactory::getLoader()->getShared(
614
+				'EventEspresso\core\domain\values\session\SessionLifespan'
615
+			);
616
+			$timestamp = $session_lifespan->expiration();
617
+		}
618
+		return $this->get_all(
619
+			array(
620
+				array(
621
+					'TXN_ID'        => 0,
622
+					'OBJ_type'      => EEM_Line_Item::OBJ_TYPE_TICKET,
623
+					'LIN_timestamp' => array('<=', $timestamp),
624
+				),
625
+			)
626
+		);
627
+	}
628 628
 }
Please login to merge, or discard this patch.
admin_pages/transactions/Transactions_Admin_Page.core.php 2 patches
Spacing   +84 added lines, -84 removed lines patch added patch discarded remove patch
@@ -356,7 +356,7 @@  discard block
 block discarded – undo
356 356
         // enqueue style
357 357
         wp_register_style(
358 358
             'espresso_txn',
359
-            TXN_ASSETS_URL . 'espresso_transactions_admin.css',
359
+            TXN_ASSETS_URL.'espresso_transactions_admin.css',
360 360
             array(),
361 361
             EVENT_ESPRESSO_VERSION
362 362
         );
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
         // scripts
365 365
         wp_register_script(
366 366
             'espresso_txn',
367
-            TXN_ASSETS_URL . 'espresso_transactions_admin.js',
367
+            TXN_ASSETS_URL.'espresso_transactions_admin.js',
368 368
             array(
369 369
                 'ee_admin_js',
370 370
                 'ee-datepicker',
@@ -492,7 +492,7 @@  discard block
 block discarded – undo
492 492
             $this->_transaction->verify_abandoned_transaction_status();
493 493
         }
494 494
 
495
-        if (! $this->_transaction instanceof EE_Transaction) {
495
+        if ( ! $this->_transaction instanceof EE_Transaction) {
496 496
             $error_msg = sprintf(
497 497
                 esc_html__(
498 498
                     'An error occurred and the details for the transaction with the ID # %d could not be retrieved.',
@@ -585,7 +585,7 @@  discard block
 block discarded – undo
585 585
             'FHEE__Transactions_Admin_Page___transaction_legend_items__more_items',
586 586
             array(
587 587
                 'overpaid'   => array(
588
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::overpaid_status_code,
588
+                    'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::overpaid_status_code,
589 589
                     'desc'  => EEH_Template::pretty_status(
590 590
                         EEM_Transaction::overpaid_status_code,
591 591
                         false,
@@ -593,7 +593,7 @@  discard block
 block discarded – undo
593 593
                     ),
594 594
                 ),
595 595
                 'complete'   => array(
596
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::complete_status_code,
596
+                    'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::complete_status_code,
597 597
                     'desc'  => EEH_Template::pretty_status(
598 598
                         EEM_Transaction::complete_status_code,
599 599
                         false,
@@ -601,7 +601,7 @@  discard block
 block discarded – undo
601 601
                     ),
602 602
                 ),
603 603
                 'incomplete' => array(
604
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::incomplete_status_code,
604
+                    'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::incomplete_status_code,
605 605
                     'desc'  => EEH_Template::pretty_status(
606 606
                         EEM_Transaction::incomplete_status_code,
607 607
                         false,
@@ -609,7 +609,7 @@  discard block
 block discarded – undo
609 609
                     ),
610 610
                 ),
611 611
                 'abandoned'  => array(
612
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::abandoned_status_code,
612
+                    'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::abandoned_status_code,
613 613
                     'desc'  => EEH_Template::pretty_status(
614 614
                         EEM_Transaction::abandoned_status_code,
615 615
                         false,
@@ -617,7 +617,7 @@  discard block
 block discarded – undo
617 617
                     ),
618 618
                 ),
619 619
                 'failed'     => array(
620
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::failed_status_code,
620
+                    'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::failed_status_code,
621 621
                     'desc'  => EEH_Template::pretty_status(
622 622
                         EEM_Transaction::failed_status_code,
623 623
                         false,
@@ -666,7 +666,7 @@  discard block
 block discarded – undo
666 666
                     'Click to Edit event',
667 667
                     'event_espresso'
668 668
                 )
669
-                . '">' . $event->name() . '</a>',
669
+                . '">'.$event->name().'</a>',
670 670
                 '</h3>'
671 671
             )
672 672
             : '';
@@ -700,7 +700,7 @@  discard block
 block discarded – undo
700 700
 
701 701
         $this->_set_transaction_object();
702 702
 
703
-        if (! $this->_transaction instanceof EE_Transaction) {
703
+        if ( ! $this->_transaction instanceof EE_Transaction) {
704 704
             return;
705 705
         }
706 706
         $primary_registration = $this->_transaction->primary_registration();
@@ -714,9 +714,9 @@  discard block
 block discarded – undo
714 714
         $this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp');
715 715
         $this->_template_args['txn_datetime']['label'] = esc_html__('Date', 'event_espresso');
716 716
 
717
-        $this->_template_args['txn_status']['value'] = self::$_txn_status[ $this->_transaction->status_ID() ];
717
+        $this->_template_args['txn_status']['value'] = self::$_txn_status[$this->_transaction->status_ID()];
718 718
         $this->_template_args['txn_status']['label'] = esc_html__('Transaction Status', 'event_espresso');
719
-        $this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->status_ID();
719
+        $this->_template_args['txn_status']['class'] = 'status-'.$this->_transaction->status_ID();
720 720
 
721 721
         $this->_template_args['grand_total'] = $this->_transaction->total();
722 722
         $this->_template_args['total_paid'] = $this->_transaction->paid();
@@ -832,7 +832,7 @@  discard block
 block discarded – undo
832 832
         // grab messages at the last second
833 833
         $this->_template_args['notices'] = EE_Error::get_notices();
834 834
         // path to template
835
-        $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php';
835
+        $template_path = TXN_TEMPLATE_PATH.'txn_admin_details_header.template.php';
836 836
         $this->_template_args['admin_page_header'] = EEH_Template::display_template(
837 837
             $template_path,
838 838
             $this->_template_args,
@@ -861,7 +861,7 @@  discard block
 block discarded – undo
861 861
 
862 862
         $this->_set_transaction_object();
863 863
 
864
-        if (! $this->_transaction instanceof EE_Transaction) {
864
+        if ( ! $this->_transaction instanceof EE_Transaction) {
865 865
             return;
866 866
         }
867 867
         add_meta_box(
@@ -917,7 +917,7 @@  discard block
 block discarded – undo
917 917
     {
918 918
         $content = '';
919 919
         $actions = array();
920
-        if (! $transaction instanceof EE_Transaction) {
920
+        if ( ! $transaction instanceof EE_Transaction) {
921 921
             return $content;
922 922
         }
923 923
         /** @var EE_Registration $primary_registration */
@@ -998,7 +998,7 @@  discard block
 block discarded – undo
998 998
         );
999 999
         if ($actions) {
1000 1000
             $content = '<ul>';
1001
-            $content .= '<li>' . implode('</li><li>', $actions) . '</li>';
1001
+            $content .= '<li>'.implode('</li><li>', $actions).'</li>';
1002 1002
             $content .= '</uL>';
1003 1003
         }
1004 1004
         return $content;
@@ -1059,7 +1059,7 @@  discard block
 block discarded – undo
1059 1059
 
1060 1060
         // process payment details
1061 1061
         $payments = $this->_transaction->payments();
1062
-        if (! empty($payments)) {
1062
+        if ( ! empty($payments)) {
1063 1063
             $this->_template_args['payments'] = $payments;
1064 1064
             $this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments);
1065 1065
         } else {
@@ -1120,7 +1120,7 @@  discard block
 block discarded – undo
1120 1120
                                   esc_html__('%1$s : Initiated %2$s', 'event_espresso'),
1121 1121
                                   ucwords(str_replace('_', ' ', $reg_step)),
1122 1122
                                   date(
1123
-                                      get_option('date_format') . ' ' . get_option('time_format'),
1123
+                                      get_option('date_format').' '.get_option('time_format'),
1124 1124
                                       $reg_step_status + (get_option('gmt_offset') * HOUR_IN_SECONDS)
1125 1125
                                   )
1126 1126
                               )
@@ -1173,7 +1173,7 @@  discard block
 block discarded – undo
1173 1173
 
1174 1174
         // 'espresso_delete_payment_nonce'
1175 1175
 
1176
-        $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php';
1176
+        $template_path = TXN_TEMPLATE_PATH.'txn_admin_details_main_meta_box_txn_details.template.php';
1177 1177
         echo EEH_Template::display_template($template_path, $this->_template_args, true);
1178 1178
     }
1179 1179
 
@@ -1205,18 +1205,18 @@  discard block
 block discarded – undo
1205 1205
                 ),
1206 1206
             )
1207 1207
         );
1208
-        if (! empty($reg_payments)) {
1208
+        if ( ! empty($reg_payments)) {
1209 1209
             foreach ($payments as $payment) {
1210
-                if (! $payment instanceof EE_Payment) {
1210
+                if ( ! $payment instanceof EE_Payment) {
1211 1211
                     continue;
1212
-                } elseif (! isset($existing_reg_payments[ $payment->ID() ])) {
1213
-                    $existing_reg_payments[ $payment->ID() ] = array();
1212
+                } elseif ( ! isset($existing_reg_payments[$payment->ID()])) {
1213
+                    $existing_reg_payments[$payment->ID()] = array();
1214 1214
                 }
1215 1215
                 foreach ($reg_payments as $reg_payment) {
1216 1216
                     if ($reg_payment instanceof EE_Registration_Payment
1217 1217
                         && $reg_payment->payment_ID() === $payment->ID()
1218 1218
                     ) {
1219
-                        $existing_reg_payments[ $payment->ID() ][] = $reg_payment->registration_ID();
1219
+                        $existing_reg_payments[$payment->ID()][] = $reg_payment->registration_ID();
1220 1220
                     }
1221 1221
                 }
1222 1222
             }
@@ -1254,22 +1254,22 @@  discard block
 block discarded – undo
1254 1254
                 ),
1255 1255
             ),
1256 1256
         );
1257
-        $registrations_to_apply_payment_to = EEH_HTML::br() . EEH_HTML::div(
1257
+        $registrations_to_apply_payment_to = EEH_HTML::br().EEH_HTML::div(
1258 1258
             '',
1259 1259
             'txn-admin-apply-payment-to-registrations-dv',
1260 1260
             '',
1261 1261
             'clear: both; margin: 1.5em 0 0; display: none;'
1262 1262
         );
1263
-        $registrations_to_apply_payment_to .= EEH_HTML::br() . EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap');
1263
+        $registrations_to_apply_payment_to .= EEH_HTML::br().EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap');
1264 1264
         $registrations_to_apply_payment_to .= EEH_HTML::table('', '', 'admin-primary-mbox-tbl');
1265 1265
         $registrations_to_apply_payment_to .= EEH_HTML::thead(
1266 1266
             EEH_HTML::tr(
1267
-                EEH_HTML::th(esc_html__('ID', 'event_espresso')) .
1268
-                EEH_HTML::th(esc_html__('Registrant', 'event_espresso')) .
1269
-                EEH_HTML::th(esc_html__('Ticket', 'event_espresso')) .
1270
-                EEH_HTML::th(esc_html__('Event', 'event_espresso')) .
1271
-                EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr') .
1272
-                EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr') .
1267
+                EEH_HTML::th(esc_html__('ID', 'event_espresso')).
1268
+                EEH_HTML::th(esc_html__('Registrant', 'event_espresso')).
1269
+                EEH_HTML::th(esc_html__('Ticket', 'event_espresso')).
1270
+                EEH_HTML::th(esc_html__('Event', 'event_espresso')).
1271
+                EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr').
1272
+                EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr').
1273 1273
                 EEH_HTML::th(esc_html__('Apply', 'event_espresso'), '', 'jst-cntr')
1274 1274
             )
1275 1275
         );
@@ -1284,7 +1284,7 @@  discard block
 block discarded – undo
1284 1284
                     : esc_html__('Unknown Attendee', 'event_espresso');
1285 1285
                 $owing = $registration->final_price() - $registration->paid();
1286 1286
                 $taxable = $registration->ticket()->taxable()
1287
-                    ? ' <span class="smaller-text lt-grey-text"> ' . esc_html__('+ tax', 'event_espresso') . '</span>'
1287
+                    ? ' <span class="smaller-text lt-grey-text"> '.esc_html__('+ tax', 'event_espresso').'</span>'
1288 1288
                     : '';
1289 1289
                 $checked = empty($existing_reg_payments)
1290 1290
                            || in_array($registration->ID(), $existing_reg_payments, true)
@@ -1292,26 +1292,26 @@  discard block
 block discarded – undo
1292 1292
                     : '';
1293 1293
                 $disabled = $registration->final_price() > 0 ? '' : ' disabled';
1294 1294
                 $registrations_to_apply_payment_to .= EEH_HTML::tr(
1295
-                    EEH_HTML::td($registration->ID()) .
1296
-                    EEH_HTML::td($attendee_name) .
1295
+                    EEH_HTML::td($registration->ID()).
1296
+                    EEH_HTML::td($attendee_name).
1297 1297
                     EEH_HTML::td(
1298
-                        $registration->ticket()->name() . ' : ' . $registration->ticket()->pretty_price() . $taxable
1299
-                    ) .
1300
-                    EEH_HTML::td($registration->event_name()) .
1301
-                    EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr') .
1298
+                        $registration->ticket()->name().' : '.$registration->ticket()->pretty_price().$taxable
1299
+                    ).
1300
+                    EEH_HTML::td($registration->event_name()).
1301
+                    EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr').
1302 1302
                     EEH_HTML::td(
1303 1303
                         EEH_Template::format_currency($owing),
1304 1304
                         '',
1305 1305
                         'txn-admin-payment-owing-td jst-cntr'
1306
-                    ) .
1306
+                    ).
1307 1307
                     EEH_HTML::td(
1308
-                        '<input type="checkbox" value="' . $registration->ID()
1308
+                        '<input type="checkbox" value="'.$registration->ID()
1309 1309
                         . '" name="txn_admin_payment[registrations]"'
1310
-                        . $checked . $disabled . '>',
1310
+                        . $checked.$disabled.'>',
1311 1311
                         '',
1312 1312
                         'jst-cntr'
1313 1313
                     ),
1314
-                    'apply-payment-registration-row-' . $registration->ID()
1314
+                    'apply-payment-registration-row-'.$registration->ID()
1315 1315
                 );
1316 1316
             }
1317 1317
         }
@@ -1392,12 +1392,12 @@  discard block
 block discarded – undo
1392 1392
                 array(
1393 1393
                     'OR*payment_method_for_payment' => array(
1394 1394
                         'PMD_ID'    => array('IN', $payment_methods_of_payments),
1395
-                        'PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'),
1395
+                        'PMD_scope' => array('LIKE', '%'.EEM_Payment_Method::scope_admin.'%'),
1396 1396
                     ),
1397 1397
                 ),
1398 1398
             );
1399 1399
         } else {
1400
-            $query_args = array(array('PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%')));
1400
+            $query_args = array(array('PMD_scope' => array('LIKE', '%'.EEM_Payment_Method::scope_admin.'%')));
1401 1401
         }
1402 1402
         $this->_template_args['payment_methods'] = EEM_Payment_Method::instance()->get_all($query_args);
1403 1403
     }
@@ -1430,7 +1430,7 @@  discard block
 block discarded – undo
1430 1430
             'Line_Item',
1431 1431
             array(array('LIN_type' => 'line-item'))
1432 1432
         );
1433
-        if (! empty($line_items)) {
1433
+        if ( ! empty($line_items)) {
1434 1434
             foreach ($line_items as $item) {
1435 1435
                 if ($item instanceof EE_Line_Item) {
1436 1436
                     switch ($item->OBJ_type()) {
@@ -1440,7 +1440,7 @@  discard block
 block discarded – undo
1440 1440
                             $ticket = $item->ticket();
1441 1441
                             // right now we're only handling tickets here.
1442 1442
                             // Cause its expected that only tickets will have attendees right?
1443
-                            if (! $ticket instanceof EE_Ticket) {
1443
+                            if ( ! $ticket instanceof EE_Ticket) {
1444 1444
                                 break;
1445 1445
                             }
1446 1446
                             try {
@@ -1449,45 +1449,45 @@  discard block
 block discarded – undo
1449 1449
                                 EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
1450 1450
                                 $event_name = esc_html__('Unknown Event', 'event_espresso');
1451 1451
                             }
1452
-                            $event_name .= ' - ' . $item->name();
1452
+                            $event_name .= ' - '.$item->name();
1453 1453
                             $ticket_price = EEH_Template::format_currency($item->unit_price());
1454 1454
                             // now get all of the registrations for this transaction that use this ticket
1455 1455
                             $registrations = $ticket->registrations(
1456 1456
                                 array(array('TXN_ID' => $this->_transaction->ID()))
1457 1457
                             );
1458 1458
                             foreach ($registrations as $registration) {
1459
-                                if (! $registration instanceof EE_Registration) {
1459
+                                if ( ! $registration instanceof EE_Registration) {
1460 1460
                                     break;
1461 1461
                                 }
1462
-                                $this->_template_args['event_attendees'][ $registration->ID() ]['STS_ID']
1462
+                                $this->_template_args['event_attendees'][$registration->ID()]['STS_ID']
1463 1463
                                     = $registration->status_ID();
1464
-                                $this->_template_args['event_attendees'][ $registration->ID() ]['att_num']
1464
+                                $this->_template_args['event_attendees'][$registration->ID()]['att_num']
1465 1465
                                     = $registration->count();
1466
-                                $this->_template_args['event_attendees'][ $registration->ID() ]['event_ticket_name']
1466
+                                $this->_template_args['event_attendees'][$registration->ID()]['event_ticket_name']
1467 1467
                                     = $event_name;
1468
-                                $this->_template_args['event_attendees'][ $registration->ID() ]['ticket_price']
1468
+                                $this->_template_args['event_attendees'][$registration->ID()]['ticket_price']
1469 1469
                                     = $ticket_price;
1470 1470
                                 // attendee info
1471 1471
                                 $attendee = $registration->get_first_related('Attendee');
1472 1472
                                 if ($attendee instanceof EE_Attendee) {
1473
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['att_id']
1473
+                                    $this->_template_args['event_attendees'][$registration->ID()]['att_id']
1474 1474
                                         = $attendee->ID();
1475
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['attendee']
1475
+                                    $this->_template_args['event_attendees'][$registration->ID()]['attendee']
1476 1476
                                         = $attendee->full_name();
1477
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['email']
1478
-                                        = '<a href="mailto:' . $attendee->email() . '?subject=' . $event_name
1477
+                                    $this->_template_args['event_attendees'][$registration->ID()]['email']
1478
+                                        = '<a href="mailto:'.$attendee->email().'?subject='.$event_name
1479 1479
                                           . esc_html__(
1480 1480
                                               ' Event',
1481 1481
                                               'event_espresso'
1482 1482
                                           )
1483
-                                          . '">' . $attendee->email() . '</a>';
1484
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['address']
1483
+                                          . '">'.$attendee->email().'</a>';
1484
+                                    $this->_template_args['event_attendees'][$registration->ID()]['address']
1485 1485
                                         = EEH_Address::format($attendee, 'inline', false, false);
1486 1486
                                 } else {
1487
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] = '';
1488
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] = '';
1489
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['email'] = '';
1490
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['address'] = '';
1487
+                                    $this->_template_args['event_attendees'][$registration->ID()]['att_id'] = '';
1488
+                                    $this->_template_args['event_attendees'][$registration->ID()]['attendee'] = '';
1489
+                                    $this->_template_args['event_attendees'][$registration->ID()]['email'] = '';
1490
+                                    $this->_template_args['event_attendees'][$registration->ID()]['address'] = '';
1491 1491
                                 }
1492 1492
                             }
1493 1493
                             break;
@@ -1503,7 +1503,7 @@  discard block
 block discarded – undo
1503 1503
                 TXN_ADMIN_URL
1504 1504
             );
1505 1505
             echo EEH_Template::display_template(
1506
-                TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_attendees.template.php',
1506
+                TXN_TEMPLATE_PATH.'txn_admin_details_main_meta_box_attendees.template.php',
1507 1507
                 $this->_template_args,
1508 1508
                 true
1509 1509
             );
@@ -1538,7 +1538,7 @@  discard block
 block discarded – undo
1538 1538
         $primary_att = $this->_transaction->primary_registration() instanceof EE_Registration
1539 1539
             ? $this->_transaction->primary_registration()->get_first_related('Attendee')
1540 1540
             : null;
1541
-        if (! $primary_att instanceof EE_Attendee) {
1541
+        if ( ! $primary_att instanceof EE_Attendee) {
1542 1542
             $this->_template_args['no_attendee_message'] = esc_html__(
1543 1543
                 'There is no attached contact for this transaction.  The transaction either failed due to an error or was abandoned.',
1544 1544
                 'event_espresso'
@@ -1560,7 +1560,7 @@  discard block
 block discarded – undo
1560 1560
         // get formatted address for registrant
1561 1561
         $this->_template_args['formatted_address'] = EEH_Address::format($primary_att);
1562 1562
         echo EEH_Template::display_template(
1563
-            TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php',
1563
+            TXN_TEMPLATE_PATH.'txn_admin_details_side_meta_box_registrant.template.php',
1564 1564
             $this->_template_args,
1565 1565
             true
1566 1566
         );
@@ -1585,7 +1585,7 @@  discard block
 block discarded – undo
1585 1585
             TXN_ADMIN_URL
1586 1586
         );
1587 1587
 
1588
-        $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_billing_info.template.php';
1588
+        $template_path = TXN_TEMPLATE_PATH.'txn_admin_details_side_meta_box_billing_info.template.php';
1589 1589
         echo EEH_Template::display_template($template_path, $this->_template_args, true);
1590 1590
     }
1591 1591
 
@@ -1611,7 +1611,7 @@  discard block
 block discarded – undo
1611 1611
             'ee_edit_payments',
1612 1612
             'apply_payment_or_refund_from_registration_details'
1613 1613
         );
1614
-        if (! empty($valid_data) && $has_access) {
1614
+        if ( ! empty($valid_data) && $has_access) {
1615 1615
             $PAY_ID = $valid_data['PAY_ID'];
1616 1616
             // save  the new payment
1617 1617
             $payment = $this->_create_payment_from_request_data($valid_data);
@@ -1624,7 +1624,7 @@  discard block
 block discarded – undo
1624 1624
                 $REG_IDs = $this->_get_REG_IDs_to_apply_payment_to($payment);
1625 1625
                 $this->_remove_existing_registration_payments($payment, $PAY_ID);
1626 1626
                 // apply payment to registrations (if applicable)
1627
-                if (! empty($REG_IDs)) {
1627
+                if ( ! empty($REG_IDs)) {
1628 1628
                     $this->_update_registration_payments($transaction, $payment, $REG_IDs);
1629 1629
                     $this->_maybe_send_notifications();
1630 1630
                     // now process status changes for the same registrations
@@ -1695,14 +1695,14 @@  discard block
 block discarded – undo
1695 1695
      */
1696 1696
     protected function _validate_payment_request_data()
1697 1697
     {
1698
-        if (! isset($this->_req_data['txn_admin_payment'])) {
1698
+        if ( ! isset($this->_req_data['txn_admin_payment'])) {
1699 1699
             return array();
1700 1700
         }
1701 1701
         $payment_form = $this->_generate_payment_form_section();
1702 1702
         try {
1703 1703
             if ($payment_form->was_submitted()) {
1704 1704
                 $payment_form->receive_form_submission();
1705
-                if (! $payment_form->is_valid()) {
1705
+                if ( ! $payment_form->is_valid()) {
1706 1706
                     $submission_error_messages = array();
1707 1707
                     foreach ($payment_form->get_validation_errors_accumulated() as $validation_error) {
1708 1708
                         if ($validation_error instanceof EE_Validation_Error) {
@@ -1883,7 +1883,7 @@  discard block
 block discarded – undo
1883 1883
             array('Y-m-d', 'g:i a')
1884 1884
         );
1885 1885
 
1886
-        if (! $payment->save()) {
1886
+        if ( ! $payment->save()) {
1887 1887
             EE_Error::add_error(
1888 1888
                 sprintf(
1889 1889
                     esc_html__('Payment %1$d has not been successfully saved to the database.', 'event_espresso'),
@@ -2088,12 +2088,12 @@  discard block
 block discarded – undo
2088 2088
         // but add in some conditions regarding payment,
2089 2089
         // so that we don't apply payments to registrations that are free or have already been paid for
2090 2090
         // but ONLY if the payment is NOT a refund ( ie: the payment amount is not negative )
2091
-        if (! $payment->is_a_refund()) {
2091
+        if ( ! $payment->is_a_refund()) {
2092 2092
             $registration_query_where_params['REG_final_price'] = array('!=', 0);
2093 2093
             $registration_query_where_params['REG_final_price*'] = array('!=', 'REG_paid', true);
2094 2094
         }
2095 2095
         $registrations = $transaction->registrations(array($registration_query_where_params));
2096
-        if (! empty($registrations)) {
2096
+        if ( ! empty($registrations)) {
2097 2097
             /** @type EE_Payment_Processor $payment_processor */
2098 2098
             $payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
2099 2099
             $payment_processor->process_registration_payments($transaction, $payment, $registrations);
@@ -2118,7 +2118,7 @@  discard block
 block discarded – undo
2118 2118
     protected function _process_registration_status_change(EE_Transaction $transaction, $REG_IDs = array())
2119 2119
     {
2120 2120
         // first if there is no change in status then we get out.
2121
-        if (! isset($this->_req_data['txn_reg_status_change']['reg_status'])
2121
+        if ( ! isset($this->_req_data['txn_reg_status_change']['reg_status'])
2122 2122
             || $this->_req_data['txn_reg_status_change']['reg_status'] === 'NAN'
2123 2123
         ) {
2124 2124
             // no error message, no change requested, just nothing to do man.
@@ -2176,7 +2176,7 @@  discard block
 block discarded – undo
2176 2176
                 'pay_status'       => $payment->STS_ID(),
2177 2177
                 'PAY_ID'           => $payment->ID(),
2178 2178
                 'STS_ID'           => $payment->STS_ID(),
2179
-                'status'           => self::$_pay_status[ $payment->STS_ID() ],
2179
+                'status'           => self::$_pay_status[$payment->STS_ID()],
2180 2180
                 'date'             => $payment->timestamp('Y-m-d', 'h:i a'),
2181 2181
                 'method'           => strtoupper($payment->source()),
2182 2182
                 'PM_ID'            => $payment->payment_method() ? $payment->payment_method()->ID() : 1,
@@ -2296,11 +2296,11 @@  discard block
 block discarded – undo
2296 2296
     {
2297 2297
         $registration_payment_data = array();
2298 2298
         // if non empty reg_ids lets get an array of registrations and update the values for the apply_payment/refund rows.
2299
-        if (! empty($REG_IDs)) {
2299
+        if ( ! empty($REG_IDs)) {
2300 2300
             $registrations = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $REG_IDs))));
2301 2301
             foreach ($registrations as $registration) {
2302 2302
                 if ($registration instanceof EE_Registration) {
2303
-                    $registration_payment_data[ $registration->ID() ] = array(
2303
+                    $registration_payment_data[$registration->ID()] = array(
2304 2304
                         'paid'  => $registration->pretty_paid(),
2305 2305
                         'owing' => EEH_Template::format_currency($registration->final_price() - $registration->paid()),
2306 2306
                     );
@@ -2412,8 +2412,8 @@  discard block
 block discarded – undo
2412 2412
             : date('m/d/Y');
2413 2413
 
2414 2414
         // make sure our timestamps start and end right at the boundaries for each day
2415
-        $start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00';
2416
-        $end_date = date('Y-m-d', strtotime($end_date)) . ' 23:59:59';
2415
+        $start_date = date('Y-m-d', strtotime($start_date)).' 00:00:00';
2416
+        $end_date = date('Y-m-d', strtotime($end_date)).' 23:59:59';
2417 2417
 
2418 2418
 
2419 2419
         // convert to timestamps
@@ -2472,7 +2472,7 @@  discard block
 block discarded – undo
2472 2472
         }
2473 2473
 
2474 2474
         if (isset($this->_req_data['s'])) {
2475
-            $search_string = '%' . $this->_req_data['s'] . '%';
2475
+            $search_string = '%'.$this->_req_data['s'].'%';
2476 2476
             $_where['OR'] = array(
2477 2477
                 'Registration.Event.EVT_name'         => array('LIKE', $search_string),
2478 2478
                 'Registration.Event.EVT_desc'         => array('LIKE', $search_string),
@@ -2499,11 +2499,11 @@  discard block
 block discarded – undo
2499 2499
         }
2500 2500
 
2501 2501
         // failed transactions
2502
-        $failed = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'failed' && ! $count)
2502
+        $failed = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] === 'failed' && ! $count)
2503 2503
                   || ($count && $view === 'failed');
2504
-        $abandoned = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'abandoned' && ! $count)
2504
+        $abandoned = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] === 'abandoned' && ! $count)
2505 2505
                      || ($count && $view === 'abandoned');
2506
-        $incomplete = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'incomplete' && ! $count)
2506
+        $incomplete = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] === 'incomplete' && ! $count)
2507 2507
                       || ($count && $view === 'incomplete');
2508 2508
 
2509 2509
         if ($failed) {
Please login to merge, or discard this patch.
Indentation   +2558 added lines, -2558 removed lines patch added patch discarded remove patch
@@ -13,2562 +13,2562 @@
 block discarded – undo
13 13
 class Transactions_Admin_Page extends EE_Admin_Page
14 14
 {
15 15
 
16
-    /**
17
-     * @var EE_Transaction
18
-     */
19
-    private $_transaction;
20
-
21
-    /**
22
-     * @var EE_Session
23
-     */
24
-    private $_session;
25
-
26
-    /**
27
-     * @var array $_txn_status
28
-     */
29
-    private static $_txn_status;
30
-
31
-    /**
32
-     * @var array $_pay_status
33
-     */
34
-    private static $_pay_status;
35
-
36
-    /**
37
-     * @var array $_existing_reg_payment_REG_IDs
38
-     */
39
-    protected $_existing_reg_payment_REG_IDs;
40
-
41
-
42
-    /**
43
-     *    _init_page_props
44
-     *
45
-     * @return void
46
-     */
47
-    protected function _init_page_props()
48
-    {
49
-        $this->page_slug = TXN_PG_SLUG;
50
-        $this->page_label = esc_html__('Transactions', 'event_espresso');
51
-        $this->_admin_base_url = TXN_ADMIN_URL;
52
-        $this->_admin_base_path = TXN_ADMIN;
53
-    }
54
-
55
-
56
-    /**
57
-     *    _ajax_hooks
58
-     *
59
-     * @return void
60
-     */
61
-    protected function _ajax_hooks()
62
-    {
63
-        add_action('wp_ajax_espresso_apply_payment', array($this, 'apply_payments_or_refunds'));
64
-        add_action('wp_ajax_espresso_apply_refund', array($this, 'apply_payments_or_refunds'));
65
-        add_action('wp_ajax_espresso_delete_payment', array($this, 'delete_payment'));
66
-    }
67
-
68
-
69
-    /**
70
-     *    _define_page_props
71
-     *
72
-     * @return void
73
-     */
74
-    protected function _define_page_props()
75
-    {
76
-        $this->_admin_page_title = $this->page_label;
77
-        $this->_labels = array(
78
-            'buttons' => array(
79
-                'add'    => esc_html__('Add New Transaction', 'event_espresso'),
80
-                'edit'   => esc_html__('Edit Transaction', 'event_espresso'),
81
-                'delete' => esc_html__('Delete Transaction', 'event_espresso'),
82
-            ),
83
-        );
84
-    }
85
-
86
-
87
-    /**
88
-     *        grab url requests and route them
89
-     *
90
-     * @access private
91
-     * @return void
92
-     * @throws EE_Error
93
-     * @throws InvalidArgumentException
94
-     * @throws InvalidDataTypeException
95
-     * @throws InvalidInterfaceException
96
-     */
97
-    public function _set_page_routes()
98
-    {
99
-
100
-        $this->_set_transaction_status_array();
101
-
102
-        $txn_id = ! empty($this->_req_data['TXN_ID'])
103
-                  && ! is_array($this->_req_data['TXN_ID'])
104
-            ? $this->_req_data['TXN_ID']
105
-            : 0;
106
-
107
-        $this->_page_routes = array(
108
-
109
-            'default' => array(
110
-                'func'       => '_transactions_overview_list_table',
111
-                'capability' => 'ee_read_transactions',
112
-            ),
113
-
114
-            'view_transaction' => array(
115
-                'func'       => '_transaction_details',
116
-                'capability' => 'ee_read_transaction',
117
-                'obj_id'     => $txn_id,
118
-            ),
119
-
120
-            'send_payment_reminder' => array(
121
-                'func'       => '_send_payment_reminder',
122
-                'noheader'   => true,
123
-                'capability' => 'ee_send_message',
124
-            ),
125
-
126
-            'espresso_apply_payment' => array(
127
-                'func'       => 'apply_payments_or_refunds',
128
-                'noheader'   => true,
129
-                'capability' => 'ee_edit_payments',
130
-            ),
131
-
132
-            'espresso_apply_refund' => array(
133
-                'func'       => 'apply_payments_or_refunds',
134
-                'noheader'   => true,
135
-                'capability' => 'ee_edit_payments',
136
-            ),
137
-
138
-            'espresso_delete_payment' => array(
139
-                'func'       => 'delete_payment',
140
-                'noheader'   => true,
141
-                'capability' => 'ee_delete_payments',
142
-            ),
143
-
144
-            'espresso_recalculate_line_items' => array(
145
-                'func'       => 'recalculateLineItems',
146
-                'noheader'   => true,
147
-                'capability' => 'ee_edit_payments',
148
-            ),
149
-
150
-        );
151
-    }
152
-
153
-
154
-    protected function _set_page_config()
155
-    {
156
-        $this->_page_config = array(
157
-            'default'          => array(
158
-                'nav'           => array(
159
-                    'label' => esc_html__('Overview', 'event_espresso'),
160
-                    'order' => 10,
161
-                ),
162
-                'list_table'    => 'EE_Admin_Transactions_List_Table',
163
-                'help_tabs'     => array(
164
-                    'transactions_overview_help_tab'                       => array(
165
-                        'title'    => esc_html__('Transactions Overview', 'event_espresso'),
166
-                        'filename' => 'transactions_overview',
167
-                    ),
168
-                    'transactions_overview_table_column_headings_help_tab' => array(
169
-                        'title'    => esc_html__('Transactions Table Column Headings', 'event_espresso'),
170
-                        'filename' => 'transactions_overview_table_column_headings',
171
-                    ),
172
-                    'transactions_overview_views_filters_help_tab'         => array(
173
-                        'title'    => esc_html__('Transaction Views & Filters & Search', 'event_espresso'),
174
-                        'filename' => 'transactions_overview_views_filters_search',
175
-                    ),
176
-                ),
177
-                'help_tour'     => array('Transactions_Overview_Help_Tour'),
178
-                /**
179
-                 * commented out because currently we are not displaying tips for transaction list table status but this
180
-                 * may change in a later iteration so want to keep the code for then.
181
-                 */
182
-                // 'qtips' => array( 'Transactions_List_Table_Tips' ),
183
-                'require_nonce' => false,
184
-            ),
185
-            'view_transaction' => array(
186
-                'nav'       => array(
187
-                    'label'      => esc_html__('View Transaction', 'event_espresso'),
188
-                    'order'      => 5,
189
-                    'url'        => isset($this->_req_data['TXN_ID'])
190
-                        ? add_query_arg(array('TXN_ID' => $this->_req_data['TXN_ID']), $this->_current_page_view_url)
191
-                        : $this->_admin_base_url,
192
-                    'persistent' => false,
193
-                ),
194
-                'help_tabs' => array(
195
-                    'transactions_view_transaction_help_tab'                                              => array(
196
-                        'title'    => esc_html__('View Transaction', 'event_espresso'),
197
-                        'filename' => 'transactions_view_transaction',
198
-                    ),
199
-                    'transactions_view_transaction_transaction_details_table_help_tab'                    => array(
200
-                        'title'    => esc_html__('Transaction Details Table', 'event_espresso'),
201
-                        'filename' => 'transactions_view_transaction_transaction_details_table',
202
-                    ),
203
-                    'transactions_view_transaction_attendees_registered_help_tab'                         => array(
204
-                        'title'    => esc_html__('Attendees Registered', 'event_espresso'),
205
-                        'filename' => 'transactions_view_transaction_attendees_registered',
206
-                    ),
207
-                    'transactions_view_transaction_views_primary_registrant_billing_information_help_tab' => array(
208
-                        'title'    => esc_html__('Primary Registrant & Billing Information', 'event_espresso'),
209
-                        'filename' => 'transactions_view_transaction_primary_registrant_billing_information',
210
-                    ),
211
-                ),
212
-                'qtips'     => array('Transaction_Details_Tips'),
213
-                'help_tour' => array('Transaction_Details_Help_Tour'),
214
-                'metaboxes' => array('_transaction_details_metaboxes'),
215
-
216
-                'require_nonce' => false,
217
-            ),
218
-        );
219
-    }
220
-
221
-
222
-    /**
223
-     * The below methods aren't used by this class currently
224
-     */
225
-    protected function _add_screen_options()
226
-    {
227
-        // noop
228
-    }
229
-
230
-
231
-    protected function _add_feature_pointers()
232
-    {
233
-        // noop
234
-    }
235
-
236
-
237
-    public function admin_init()
238
-    {
239
-        // IF a registration was JUST added via the admin...
240
-        if (isset(
241
-            $this->_req_data['redirect_from'],
242
-            $this->_req_data['EVT_ID'],
243
-            $this->_req_data['event_name']
244
-        )) {
245
-            // then set a cookie so that we can block any attempts to use
246
-            // the back button as a way to enter another registration.
247
-            setcookie(
248
-                'ee_registration_added',
249
-                $this->_req_data['EVT_ID'],
250
-                time() + WEEK_IN_SECONDS,
251
-                '/'
252
-            );
253
-            // and update the global
254
-            $_COOKIE['ee_registration_added'] = $this->_req_data['EVT_ID'];
255
-        }
256
-        EE_Registry::$i18n_js_strings['invalid_server_response'] = esc_html__(
257
-            'An error occurred! Your request may have been processed, but a valid response from the server was not received. Please refresh the page and try again.',
258
-            'event_espresso'
259
-        );
260
-        EE_Registry::$i18n_js_strings['error_occurred'] = esc_html__(
261
-            'An error occurred! Please refresh the page and try again.',
262
-            'event_espresso'
263
-        );
264
-        EE_Registry::$i18n_js_strings['txn_status_array'] = self::$_txn_status;
265
-        EE_Registry::$i18n_js_strings['pay_status_array'] = self::$_pay_status;
266
-        EE_Registry::$i18n_js_strings['payments_total'] = esc_html__('Payments Total', 'event_espresso');
267
-        EE_Registry::$i18n_js_strings['transaction_overpaid'] = esc_html__(
268
-            'This transaction has been overpaid ! Payments Total',
269
-            'event_espresso'
270
-        );
271
-    }
272
-
273
-
274
-    public function admin_notices()
275
-    {
276
-        // noop
277
-    }
278
-
279
-
280
-    public function admin_footer_scripts()
281
-    {
282
-        // noop
283
-    }
284
-
285
-
286
-    /**
287
-     * _set_transaction_status_array
288
-     * sets list of transaction statuses
289
-     *
290
-     * @access private
291
-     * @return void
292
-     * @throws EE_Error
293
-     * @throws InvalidArgumentException
294
-     * @throws InvalidDataTypeException
295
-     * @throws InvalidInterfaceException
296
-     */
297
-    private function _set_transaction_status_array()
298
-    {
299
-        self::$_txn_status = EEM_Transaction::instance()->status_array(true);
300
-    }
301
-
302
-
303
-    /**
304
-     * get_transaction_status_array
305
-     * return the transaction status array for wp_list_table
306
-     *
307
-     * @access public
308
-     * @return array
309
-     */
310
-    public function get_transaction_status_array()
311
-    {
312
-        return self::$_txn_status;
313
-    }
314
-
315
-
316
-    /**
317
-     *    get list of payment statuses
318
-     *
319
-     * @access private
320
-     * @return void
321
-     * @throws EE_Error
322
-     * @throws InvalidArgumentException
323
-     * @throws InvalidDataTypeException
324
-     * @throws InvalidInterfaceException
325
-     */
326
-    private function _get_payment_status_array()
327
-    {
328
-        self::$_pay_status = EEM_Payment::instance()->status_array(true);
329
-        $this->_template_args['payment_status'] = self::$_pay_status;
330
-    }
331
-
332
-
333
-    /**
334
-     *    _add_screen_options_default
335
-     *
336
-     * @access protected
337
-     * @return void
338
-     * @throws InvalidArgumentException
339
-     * @throws InvalidDataTypeException
340
-     * @throws InvalidInterfaceException
341
-     */
342
-    protected function _add_screen_options_default()
343
-    {
344
-        $this->_per_page_screen_option();
345
-    }
346
-
347
-
348
-    /**
349
-     * load_scripts_styles
350
-     *
351
-     * @access public
352
-     * @return void
353
-     */
354
-    public function load_scripts_styles()
355
-    {
356
-        // enqueue style
357
-        wp_register_style(
358
-            'espresso_txn',
359
-            TXN_ASSETS_URL . 'espresso_transactions_admin.css',
360
-            array(),
361
-            EVENT_ESPRESSO_VERSION
362
-        );
363
-        wp_enqueue_style('espresso_txn');
364
-        // scripts
365
-        wp_register_script(
366
-            'espresso_txn',
367
-            TXN_ASSETS_URL . 'espresso_transactions_admin.js',
368
-            array(
369
-                'ee_admin_js',
370
-                'ee-datepicker',
371
-                'jquery-ui-datepicker',
372
-                'jquery-ui-draggable',
373
-                'ee-dialog',
374
-                'ee-accounting',
375
-                'ee-serialize-full-array',
376
-            ),
377
-            EVENT_ESPRESSO_VERSION,
378
-            true
379
-        );
380
-        wp_enqueue_script('espresso_txn');
381
-    }
382
-
383
-
384
-    /**
385
-     *    load_scripts_styles_view_transaction
386
-     *
387
-     * @access public
388
-     * @return void
389
-     */
390
-    public function load_scripts_styles_view_transaction()
391
-    {
392
-        // styles
393
-        wp_enqueue_style('espresso-ui-theme');
394
-    }
395
-
396
-
397
-    /**
398
-     *    load_scripts_styles_default
399
-     *
400
-     * @access public
401
-     * @return void
402
-     */
403
-    public function load_scripts_styles_default()
404
-    {
405
-        // styles
406
-        wp_enqueue_style('espresso-ui-theme');
407
-    }
408
-
409
-
410
-    /**
411
-     *    _set_list_table_views_default
412
-     *
413
-     * @access protected
414
-     * @return void
415
-     */
416
-    protected function _set_list_table_views_default()
417
-    {
418
-        $this->_views = array(
419
-            'all'        => array(
420
-                'slug'  => 'all',
421
-                'label' => esc_html__('View All Transactions', 'event_espresso'),
422
-                'count' => 0,
423
-            ),
424
-            'abandoned'  => array(
425
-                'slug'  => 'abandoned',
426
-                'label' => esc_html__('Abandoned Transactions', 'event_espresso'),
427
-                'count' => 0,
428
-            ),
429
-            'incomplete' => array(
430
-                'slug'  => 'incomplete',
431
-                'label' => esc_html__('Incomplete Transactions', 'event_espresso'),
432
-                'count' => 0,
433
-            ),
434
-        );
435
-        if (/**
436
-         * Filters whether a link to the "Failed Transactions" list table
437
-         * appears on the Transactions Admin Page list table.
438
-         * List display can be turned back on via the following:
439
-         * add_filter(
440
-         *     'FHEE__Transactions_Admin_Page___set_list_table_views_default__display_failed_txns_list',
441
-         *     '__return_true'
442
-         * );
443
-         *
444
-         * @since 4.9.70.p
445
-         * @param boolean                 $display_failed_txns_list
446
-         * @param Transactions_Admin_Page $this
447
-         */
448
-        apply_filters(
449
-            'FHEE__Transactions_Admin_Page___set_list_table_views_default__display_failed_txns_list',
450
-            false,
451
-            $this
452
-        )
453
-        ) {
454
-            $this->_views['failed'] = array(
455
-                'slug'  => 'failed',
456
-                'label' => esc_html__('Failed Transactions', 'event_espresso'),
457
-                'count' => 0,
458
-            );
459
-        }
460
-    }
461
-
462
-
463
-    /**
464
-     * _set_transaction_object
465
-     * This sets the _transaction property for the transaction details screen
466
-     *
467
-     * @access private
468
-     * @return void
469
-     * @throws EE_Error
470
-     * @throws InvalidArgumentException
471
-     * @throws RuntimeException
472
-     * @throws InvalidDataTypeException
473
-     * @throws InvalidInterfaceException
474
-     * @throws ReflectionException
475
-     */
476
-    private function _set_transaction_object()
477
-    {
478
-        if ($this->_transaction instanceof EE_Transaction) {
479
-            return;
480
-        } //get out we've already set the object
481
-
482
-        $TXN_ID = ! empty($this->_req_data['TXN_ID'])
483
-            ? absint($this->_req_data['TXN_ID'])
484
-            : false;
485
-
486
-        // get transaction object
487
-        $this->_transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID);
488
-        $this->_session = $this->_transaction instanceof EE_Transaction
489
-            ? $this->_transaction->session_data()
490
-            : null;
491
-        if ($this->_transaction instanceof EE_Transaction) {
492
-            $this->_transaction->verify_abandoned_transaction_status();
493
-        }
494
-
495
-        if (! $this->_transaction instanceof EE_Transaction) {
496
-            $error_msg = sprintf(
497
-                esc_html__(
498
-                    'An error occurred and the details for the transaction with the ID # %d could not be retrieved.',
499
-                    'event_espresso'
500
-                ),
501
-                $TXN_ID
502
-            );
503
-            EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__);
504
-        }
505
-    }
506
-
507
-
508
-    /**
509
-     *    _transaction_legend_items
510
-     *
511
-     * @access protected
512
-     * @return array
513
-     * @throws EE_Error
514
-     * @throws InvalidArgumentException
515
-     * @throws ReflectionException
516
-     * @throws InvalidDataTypeException
517
-     * @throws InvalidInterfaceException
518
-     */
519
-    protected function _transaction_legend_items()
520
-    {
521
-        EE_Registry::instance()->load_helper('MSG_Template');
522
-        $items = array();
523
-
524
-        if (EE_Registry::instance()->CAP->current_user_can(
525
-            'ee_read_global_messages',
526
-            'view_filtered_messages'
527
-        )) {
528
-            $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for');
529
-            if (is_array($related_for_icon)
530
-                && isset($related_for_icon['css_class'], $related_for_icon['label'])
531
-            ) {
532
-                $items['view_related_messages'] = array(
533
-                    'class' => $related_for_icon['css_class'],
534
-                    'desc'  => $related_for_icon['label'],
535
-                );
536
-            }
537
-        }
538
-
539
-        $items = apply_filters(
540
-            'FHEE__Transactions_Admin_Page___transaction_legend_items__items',
541
-            array_merge(
542
-                $items,
543
-                array(
544
-                    'view_details'          => array(
545
-                        'class' => 'dashicons dashicons-cart',
546
-                        'desc'  => esc_html__('View Transaction Details', 'event_espresso'),
547
-                    ),
548
-                    'view_invoice'          => array(
549
-                        'class' => 'dashicons dashicons-media-spreadsheet',
550
-                        'desc'  => esc_html__('View Transaction Invoice', 'event_espresso'),
551
-                    ),
552
-                    'view_receipt'          => array(
553
-                        'class' => 'dashicons dashicons-media-default',
554
-                        'desc'  => esc_html__('View Transaction Receipt', 'event_espresso'),
555
-                    ),
556
-                    'view_registration'     => array(
557
-                        'class' => 'dashicons dashicons-clipboard',
558
-                        'desc'  => esc_html__('View Registration Details', 'event_espresso'),
559
-                    ),
560
-                    'payment_overview_link' => array(
561
-                        'class' => 'dashicons dashicons-money',
562
-                        'desc'  => esc_html__('Make Payment on Frontend', 'event_espresso'),
563
-                    ),
564
-                )
565
-            )
566
-        );
567
-
568
-        if (EEH_MSG_Template::is_mt_active('payment_reminder')
569
-            && EE_Registry::instance()->CAP->current_user_can(
570
-                'ee_send_message',
571
-                'espresso_transactions_send_payment_reminder'
572
-            )
573
-        ) {
574
-            $items['send_payment_reminder'] = array(
575
-                'class' => 'dashicons dashicons-email-alt',
576
-                'desc'  => esc_html__('Send Payment Reminder', 'event_espresso'),
577
-            );
578
-        } else {
579
-            $items['blank*'] = array(
580
-                'class' => '',
581
-                'desc'  => '',
582
-            );
583
-        }
584
-        $more_items = apply_filters(
585
-            'FHEE__Transactions_Admin_Page___transaction_legend_items__more_items',
586
-            array(
587
-                'overpaid'   => array(
588
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::overpaid_status_code,
589
-                    'desc'  => EEH_Template::pretty_status(
590
-                        EEM_Transaction::overpaid_status_code,
591
-                        false,
592
-                        'sentence'
593
-                    ),
594
-                ),
595
-                'complete'   => array(
596
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::complete_status_code,
597
-                    'desc'  => EEH_Template::pretty_status(
598
-                        EEM_Transaction::complete_status_code,
599
-                        false,
600
-                        'sentence'
601
-                    ),
602
-                ),
603
-                'incomplete' => array(
604
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::incomplete_status_code,
605
-                    'desc'  => EEH_Template::pretty_status(
606
-                        EEM_Transaction::incomplete_status_code,
607
-                        false,
608
-                        'sentence'
609
-                    ),
610
-                ),
611
-                'abandoned'  => array(
612
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::abandoned_status_code,
613
-                    'desc'  => EEH_Template::pretty_status(
614
-                        EEM_Transaction::abandoned_status_code,
615
-                        false,
616
-                        'sentence'
617
-                    ),
618
-                ),
619
-                'failed'     => array(
620
-                    'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::failed_status_code,
621
-                    'desc'  => EEH_Template::pretty_status(
622
-                        EEM_Transaction::failed_status_code,
623
-                        false,
624
-                        'sentence'
625
-                    ),
626
-                ),
627
-            )
628
-        );
629
-
630
-        return array_merge($items, $more_items);
631
-    }
632
-
633
-
634
-    /**
635
-     *    _transactions_overview_list_table
636
-     *
637
-     * @access protected
638
-     * @return void
639
-     * @throws DomainException
640
-     * @throws EE_Error
641
-     * @throws InvalidArgumentException
642
-     * @throws InvalidDataTypeException
643
-     * @throws InvalidInterfaceException
644
-     * @throws ReflectionException
645
-     */
646
-    protected function _transactions_overview_list_table()
647
-    {
648
-        $this->_admin_page_title = esc_html__('Transactions', 'event_espresso');
649
-        $event = isset($this->_req_data['EVT_ID'])
650
-            ? EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID'])
651
-            : null;
652
-        $this->_template_args['admin_page_header'] = $event instanceof EE_Event
653
-            ? sprintf(
654
-                esc_html__(
655
-                    '%sViewing Transactions for the Event: %s%s',
656
-                    'event_espresso'
657
-                ),
658
-                '<h3>',
659
-                '<a href="'
660
-                . EE_Admin_Page::add_query_args_and_nonce(
661
-                    array('action' => 'edit', 'post' => $event->ID()),
662
-                    EVENTS_ADMIN_URL
663
-                )
664
-                . '" title="'
665
-                . esc_attr__(
666
-                    'Click to Edit event',
667
-                    'event_espresso'
668
-                )
669
-                . '">' . $event->name() . '</a>',
670
-                '</h3>'
671
-            )
672
-            : '';
673
-        $this->_template_args['after_list_table'] = $this->_display_legend($this->_transaction_legend_items());
674
-        $this->display_admin_list_table_page_with_no_sidebar();
675
-    }
676
-
677
-
678
-    /**
679
-     *    _transaction_details
680
-     * generates HTML for the View Transaction Details Admin page
681
-     *
682
-     * @access protected
683
-     * @return void
684
-     * @throws DomainException
685
-     * @throws EE_Error
686
-     * @throws InvalidArgumentException
687
-     * @throws InvalidDataTypeException
688
-     * @throws InvalidInterfaceException
689
-     * @throws RuntimeException
690
-     * @throws ReflectionException
691
-     */
692
-    protected function _transaction_details()
693
-    {
694
-        do_action('AHEE__Transactions_Admin_Page__transaction_details__start', $this->_transaction);
695
-
696
-        $this->_set_transaction_status_array();
697
-
698
-        $this->_template_args = array();
699
-        $this->_template_args['transactions_page'] = $this->_wp_page_slug;
700
-
701
-        $this->_set_transaction_object();
702
-
703
-        if (! $this->_transaction instanceof EE_Transaction) {
704
-            return;
705
-        }
706
-        $primary_registration = $this->_transaction->primary_registration();
707
-        $attendee = $primary_registration instanceof EE_Registration
708
-            ? $primary_registration->attendee()
709
-            : null;
710
-
711
-        $this->_template_args['txn_nmbr']['value'] = $this->_transaction->ID();
712
-        $this->_template_args['txn_nmbr']['label'] = esc_html__('Transaction Number', 'event_espresso');
713
-
714
-        $this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp');
715
-        $this->_template_args['txn_datetime']['label'] = esc_html__('Date', 'event_espresso');
716
-
717
-        $this->_template_args['txn_status']['value'] = self::$_txn_status[ $this->_transaction->status_ID() ];
718
-        $this->_template_args['txn_status']['label'] = esc_html__('Transaction Status', 'event_espresso');
719
-        $this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->status_ID();
720
-
721
-        $this->_template_args['grand_total'] = $this->_transaction->total();
722
-        $this->_template_args['total_paid'] = $this->_transaction->paid();
723
-
724
-        $amount_due = $this->_transaction->total() - $this->_transaction->paid();
725
-        $this->_template_args['amount_due'] = EEH_Template::format_currency(
726
-            $amount_due,
727
-            true
728
-        );
729
-        if (EE_Registry::instance()->CFG->currency->sign_b4) {
730
-            $this->_template_args['amount_due'] = EE_Registry::instance()->CFG->currency->sign
731
-                                                  . $this->_template_args['amount_due'];
732
-        } else {
733
-            $this->_template_args['amount_due'] .= EE_Registry::instance()->CFG->currency->sign;
734
-        }
735
-        $this->_template_args['amount_due_class'] = '';
736
-
737
-        if ($this->_transaction->paid() === $this->_transaction->total()) {
738
-            // paid in full
739
-            $this->_template_args['amount_due'] = false;
740
-        } elseif ($this->_transaction->paid() > $this->_transaction->total()) {
741
-            // overpaid
742
-            $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn';
743
-        } elseif ($this->_transaction->total() > (float) 0) {
744
-            if ($this->_transaction->paid() > (float) 0) {
745
-                // monies owing
746
-                $this->_template_args['amount_due_class'] = 'txn-overview-part-payment-spn';
747
-            } elseif ($this->_transaction->paid() === (float) 0) {
748
-                // no payments made yet
749
-                $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn';
750
-            }
751
-        } elseif ($this->_transaction->total() === (float) 0) {
752
-            // free event
753
-            $this->_template_args['amount_due'] = false;
754
-        }
755
-
756
-        $payment_method = $this->_transaction->payment_method();
757
-
758
-        $this->_template_args['method_of_payment_name'] = $payment_method instanceof EE_Payment_Method
759
-            ? $payment_method->admin_name()
760
-            : esc_html__('Unknown', 'event_espresso');
761
-
762
-        $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign;
763
-        // link back to overview
764
-        $this->_template_args['txn_overview_url'] = ! empty($_SERVER['HTTP_REFERER'])
765
-            ? $_SERVER['HTTP_REFERER']
766
-            : TXN_ADMIN_URL;
767
-
768
-
769
-        // next link
770
-        $next_txn = $this->_transaction->next(
771
-            null,
772
-            array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))),
773
-            'TXN_ID'
774
-        );
775
-        $this->_template_args['next_transaction'] = $next_txn
776
-            ? $this->_next_link(
777
-                EE_Admin_Page::add_query_args_and_nonce(
778
-                    array('action' => 'view_transaction', 'TXN_ID' => $next_txn['TXN_ID']),
779
-                    TXN_ADMIN_URL
780
-                ),
781
-                'dashicons dashicons-arrow-right ee-icon-size-22'
782
-            )
783
-            : '';
784
-        // previous link
785
-        $previous_txn = $this->_transaction->previous(
786
-            null,
787
-            array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))),
788
-            'TXN_ID'
789
-        );
790
-        $this->_template_args['previous_transaction'] = $previous_txn
791
-            ? $this->_previous_link(
792
-                EE_Admin_Page::add_query_args_and_nonce(
793
-                    array('action' => 'view_transaction', 'TXN_ID' => $previous_txn['TXN_ID']),
794
-                    TXN_ADMIN_URL
795
-                ),
796
-                'dashicons dashicons-arrow-left ee-icon-size-22'
797
-            )
798
-            : '';
799
-
800
-        // were we just redirected here after adding a new registration ???
801
-        if (isset(
802
-            $this->_req_data['redirect_from'],
803
-            $this->_req_data['EVT_ID'],
804
-            $this->_req_data['event_name']
805
-        )) {
806
-            if (EE_Registry::instance()->CAP->current_user_can(
807
-                'ee_edit_registrations',
808
-                'espresso_registrations_new_registration',
809
-                $this->_req_data['EVT_ID']
810
-            )) {
811
-                $this->_admin_page_title .= '<a id="add-new-registration" class="add-new-h2 button-primary" href="';
812
-                $this->_admin_page_title .= EE_Admin_Page::add_query_args_and_nonce(
813
-                    array(
814
-                        'page'     => 'espresso_registrations',
815
-                        'action'   => 'new_registration',
816
-                        'return'   => 'default',
817
-                        'TXN_ID'   => $this->_transaction->ID(),
818
-                        'event_id' => $this->_req_data['EVT_ID'],
819
-                    ),
820
-                    REG_ADMIN_URL
821
-                );
822
-                $this->_admin_page_title .= '">';
823
-
824
-                $this->_admin_page_title .= sprintf(
825
-                    esc_html__('Add Another New Registration to Event: "%1$s" ?', 'event_espresso'),
826
-                    htmlentities(urldecode($this->_req_data['event_name']), ENT_QUOTES, 'UTF-8')
827
-                );
828
-                $this->_admin_page_title .= '</a>';
829
-            }
830
-            EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__);
831
-        }
832
-        // grab messages at the last second
833
-        $this->_template_args['notices'] = EE_Error::get_notices();
834
-        // path to template
835
-        $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php';
836
-        $this->_template_args['admin_page_header'] = EEH_Template::display_template(
837
-            $template_path,
838
-            $this->_template_args,
839
-            true
840
-        );
841
-
842
-        // the details template wrapper
843
-        $this->display_admin_page_with_sidebar();
844
-    }
845
-
846
-
847
-    /**
848
-     *        _transaction_details_metaboxes
849
-     *
850
-     * @access protected
851
-     * @return void
852
-     * @throws EE_Error
853
-     * @throws InvalidArgumentException
854
-     * @throws InvalidDataTypeException
855
-     * @throws InvalidInterfaceException
856
-     * @throws RuntimeException
857
-     * @throws ReflectionException
858
-     */
859
-    protected function _transaction_details_metaboxes()
860
-    {
861
-
862
-        $this->_set_transaction_object();
863
-
864
-        if (! $this->_transaction instanceof EE_Transaction) {
865
-            return;
866
-        }
867
-        add_meta_box(
868
-            'edit-txn-details-mbox',
869
-            esc_html__('Transaction Details', 'event_espresso'),
870
-            array($this, 'txn_details_meta_box'),
871
-            $this->_wp_page_slug,
872
-            'normal',
873
-            'high'
874
-        );
875
-        add_meta_box(
876
-            'edit-txn-attendees-mbox',
877
-            esc_html__('Attendees Registered in this Transaction', 'event_espresso'),
878
-            array($this, 'txn_attendees_meta_box'),
879
-            $this->_wp_page_slug,
880
-            'normal',
881
-            'high',
882
-            array('TXN_ID' => $this->_transaction->ID())
883
-        );
884
-        add_meta_box(
885
-            'edit-txn-registrant-mbox',
886
-            esc_html__('Primary Contact', 'event_espresso'),
887
-            array($this, 'txn_registrant_side_meta_box'),
888
-            $this->_wp_page_slug,
889
-            'side',
890
-            'high'
891
-        );
892
-        add_meta_box(
893
-            'edit-txn-billing-info-mbox',
894
-            esc_html__('Billing Information', 'event_espresso'),
895
-            array($this, 'txn_billing_info_side_meta_box'),
896
-            $this->_wp_page_slug,
897
-            'side',
898
-            'high'
899
-        );
900
-    }
901
-
902
-
903
-    /**
904
-     * Callback for transaction actions metabox.
905
-     *
906
-     * @param EE_Transaction|null $transaction
907
-     * @return string
908
-     * @throws DomainException
909
-     * @throws EE_Error
910
-     * @throws InvalidArgumentException
911
-     * @throws InvalidDataTypeException
912
-     * @throws InvalidInterfaceException
913
-     * @throws ReflectionException
914
-     * @throws RuntimeException
915
-     */
916
-    public function getActionButtons(EE_Transaction $transaction = null)
917
-    {
918
-        $content = '';
919
-        $actions = array();
920
-        if (! $transaction instanceof EE_Transaction) {
921
-            return $content;
922
-        }
923
-        /** @var EE_Registration $primary_registration */
924
-        $primary_registration = $transaction->primary_registration();
925
-        $attendee = $primary_registration instanceof EE_Registration
926
-            ? $primary_registration->attendee()
927
-            : null;
928
-
929
-        if ($attendee instanceof EE_Attendee
930
-            && EE_Registry::instance()->CAP->current_user_can(
931
-                'ee_send_message',
932
-                'espresso_transactions_send_payment_reminder'
933
-            )
934
-        ) {
935
-            $actions['payment_reminder'] =
936
-                EEH_MSG_Template::is_mt_active('payment_reminder')
937
-                && $this->_transaction->status_ID() !== EEM_Transaction::complete_status_code
938
-                && $this->_transaction->status_ID() !== EEM_Transaction::overpaid_status_code
939
-                    ? EEH_Template::get_button_or_link(
940
-                        EE_Admin_Page::add_query_args_and_nonce(
941
-                            array(
942
-                                'action'      => 'send_payment_reminder',
943
-                                'TXN_ID'      => $this->_transaction->ID(),
944
-                                'redirect_to' => 'view_transaction',
945
-                            ),
946
-                            TXN_ADMIN_URL
947
-                        ),
948
-                        esc_html__(' Send Payment Reminder', 'event_espresso'),
949
-                        'button secondary-button',
950
-                        'dashicons dashicons-email-alt'
951
-                    )
952
-                    : '';
953
-        }
954
-
955
-        if (EE_Registry::instance()->CAP->current_user_can(
956
-            'ee_edit_payments',
957
-            'espresso_transactions_recalculate_line_items'
958
-        )
959
-        ) {
960
-            $actions['recalculate_line_items'] = EEH_Template::get_button_or_link(
961
-                EE_Admin_Page::add_query_args_and_nonce(
962
-                    array(
963
-                        'action'      => 'espresso_recalculate_line_items',
964
-                        'TXN_ID'      => $this->_transaction->ID(),
965
-                        'redirect_to' => 'view_transaction',
966
-                    ),
967
-                    TXN_ADMIN_URL
968
-                ),
969
-                esc_html__(' Recalculate Taxes and Total', 'event_espresso'),
970
-                'button secondary-button',
971
-                'dashicons dashicons-update'
972
-            );
973
-        }
974
-
975
-        if ($primary_registration instanceof EE_Registration
976
-            && EEH_MSG_Template::is_mt_active('receipt')
977
-        ) {
978
-            $actions['receipt'] = EEH_Template::get_button_or_link(
979
-                $primary_registration->receipt_url(),
980
-                esc_html__('View Receipt', 'event_espresso'),
981
-                'button secondary-button',
982
-                'dashicons dashicons-media-default'
983
-            );
984
-        }
985
-
986
-        if ($primary_registration instanceof EE_Registration
987
-            && EEH_MSG_Template::is_mt_active('invoice')
988
-        ) {
989
-            $actions['invoice'] = EEH_Template::get_button_or_link(
990
-                $primary_registration->invoice_url(),
991
-                esc_html__('View Invoice', 'event_espresso'),
992
-                'button secondary-button',
993
-                'dashicons dashicons-media-spreadsheet'
994
-            );
995
-        }
996
-        $actions = array_filter(
997
-            apply_filters('FHEE__Transactions_Admin_Page__getActionButtons__actions', $actions, $transaction)
998
-        );
999
-        if ($actions) {
1000
-            $content = '<ul>';
1001
-            $content .= '<li>' . implode('</li><li>', $actions) . '</li>';
1002
-            $content .= '</uL>';
1003
-        }
1004
-        return $content;
1005
-    }
1006
-
1007
-
1008
-    /**
1009
-     * txn_details_meta_box
1010
-     * generates HTML for the Transaction main meta box
1011
-     *
1012
-     * @return void
1013
-     * @throws DomainException
1014
-     * @throws EE_Error
1015
-     * @throws InvalidArgumentException
1016
-     * @throws InvalidDataTypeException
1017
-     * @throws InvalidInterfaceException
1018
-     * @throws RuntimeException
1019
-     * @throws ReflectionException
1020
-     */
1021
-    public function txn_details_meta_box()
1022
-    {
1023
-        $this->_set_transaction_object();
1024
-        $this->_template_args['TXN_ID'] = $this->_transaction->ID();
1025
-        $this->_template_args['attendee'] = $this->_transaction->primary_registration() instanceof EE_Registration
1026
-            ? $this->_transaction->primary_registration()->attendee()
1027
-            : null;
1028
-        $this->_template_args['can_edit_payments'] = EE_Registry::instance()->CAP->current_user_can(
1029
-            'ee_edit_payments',
1030
-            'apply_payment_or_refund_from_registration_details'
1031
-        );
1032
-        $this->_template_args['can_delete_payments'] = EE_Registry::instance()->CAP->current_user_can(
1033
-            'ee_delete_payments',
1034
-            'delete_payment_from_registration_details'
1035
-        );
1036
-
1037
-        // get line table
1038
-        EEH_Autoloader::register_line_item_display_autoloaders();
1039
-        $Line_Item_Display = new EE_Line_Item_Display(
1040
-            'admin_table',
1041
-            'EE_Admin_Table_Line_Item_Display_Strategy'
1042
-        );
1043
-        $this->_template_args['line_item_table'] = $Line_Item_Display->display_line_item(
1044
-            $this->_transaction->total_line_item()
1045
-        );
1046
-        $this->_template_args['REG_code'] = $this->_transaction->primary_registration()->reg_code();
1047
-
1048
-        // process taxes
1049
-        $taxes = $this->_transaction->line_items(array(array('LIN_type' => EEM_Line_Item::type_tax)));
1050
-        $this->_template_args['taxes'] = ! empty($taxes) ? $taxes : false;
1051
-
1052
-        $this->_template_args['grand_total'] = EEH_Template::format_currency(
1053
-            $this->_transaction->total(),
1054
-            false,
1055
-            false
1056
-        );
1057
-        $this->_template_args['grand_raw_total'] = $this->_transaction->total();
1058
-        $this->_template_args['TXN_status'] = $this->_transaction->status_ID();
1059
-
1060
-        // process payment details
1061
-        $payments = $this->_transaction->payments();
1062
-        if (! empty($payments)) {
1063
-            $this->_template_args['payments'] = $payments;
1064
-            $this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments);
1065
-        } else {
1066
-            $this->_template_args['payments'] = false;
1067
-            $this->_template_args['existing_reg_payments'] = array();
1068
-        }
1069
-
1070
-        $this->_template_args['edit_payment_url'] = add_query_arg(array('action' => 'edit_payment'), TXN_ADMIN_URL);
1071
-        $this->_template_args['delete_payment_url'] = add_query_arg(
1072
-            array('action' => 'espresso_delete_payment'),
1073
-            TXN_ADMIN_URL
1074
-        );
1075
-
1076
-        if (isset($txn_details['invoice_number'])) {
1077
-            $this->_template_args['txn_details']['invoice_number']['value'] = $this->_template_args['REG_code'];
1078
-            $this->_template_args['txn_details']['invoice_number']['label'] = esc_html__(
1079
-                'Invoice Number',
1080
-                'event_espresso'
1081
-            );
1082
-        }
1083
-
1084
-        $this->_template_args['txn_details']['registration_session']['value'] = $this->_transaction
1085
-            ->primary_registration()
1086
-            ->session_ID();
1087
-        $this->_template_args['txn_details']['registration_session']['label'] = esc_html__(
1088
-            'Registration Session',
1089
-            'event_espresso'
1090
-        );
1091
-
1092
-        $this->_template_args['txn_details']['ip_address']['value'] = isset($this->_session['ip_address'])
1093
-            ? $this->_session['ip_address']
1094
-            : '';
1095
-        $this->_template_args['txn_details']['ip_address']['label'] = esc_html__(
1096
-            'Transaction placed from IP',
1097
-            'event_espresso'
1098
-        );
1099
-
1100
-        $this->_template_args['txn_details']['user_agent']['value'] = isset($this->_session['user_agent'])
1101
-            ? $this->_session['user_agent']
1102
-            : '';
1103
-        $this->_template_args['txn_details']['user_agent']['label'] = esc_html__(
1104
-            'Registrant User Agent',
1105
-            'event_espresso'
1106
-        );
1107
-
1108
-        $reg_steps = '<ul>';
1109
-        foreach ($this->_transaction->reg_steps() as $reg_step => $reg_step_status) {
1110
-            if ($reg_step_status === true) {
1111
-                $reg_steps .= '<li style="color:#70cc50">'
1112
-                              . sprintf(
1113
-                                  esc_html__('%1$s : Completed', 'event_espresso'),
1114
-                                  ucwords(str_replace('_', ' ', $reg_step))
1115
-                              )
1116
-                              . '</li>';
1117
-            } elseif (is_numeric($reg_step_status) && $reg_step_status !== false) {
1118
-                $reg_steps .= '<li style="color:#2EA2CC">'
1119
-                              . sprintf(
1120
-                                  esc_html__('%1$s : Initiated %2$s', 'event_espresso'),
1121
-                                  ucwords(str_replace('_', ' ', $reg_step)),
1122
-                                  date(
1123
-                                      get_option('date_format') . ' ' . get_option('time_format'),
1124
-                                      $reg_step_status + (get_option('gmt_offset') * HOUR_IN_SECONDS)
1125
-                                  )
1126
-                              )
1127
-                              . '</li>';
1128
-            } else {
1129
-                $reg_steps .= '<li style="color:#E76700">'
1130
-                              . sprintf(
1131
-                                  esc_html__('%1$s : Never Initiated', 'event_espresso'),
1132
-                                  ucwords(str_replace('_', ' ', $reg_step))
1133
-                              )
1134
-                              . '</li>';
1135
-            }
1136
-        }
1137
-        $reg_steps .= '</ul>';
1138
-        $this->_template_args['txn_details']['reg_steps']['value'] = $reg_steps;
1139
-        $this->_template_args['txn_details']['reg_steps']['label'] = esc_html__(
1140
-            'Registration Step Progress',
1141
-            'event_espresso'
1142
-        );
1143
-
1144
-
1145
-        $this->_get_registrations_to_apply_payment_to();
1146
-        $this->_get_payment_methods($payments);
1147
-        $this->_get_payment_status_array();
1148
-        $this->_get_reg_status_selection(); // sets up the template args for the reg status array for the transaction.
1149
-
1150
-        $this->_template_args['transaction_form_url'] = add_query_arg(
1151
-            array(
1152
-                'action'  => 'edit_transaction',
1153
-                'process' => 'transaction',
1154
-            ),
1155
-            TXN_ADMIN_URL
1156
-        );
1157
-        $this->_template_args['apply_payment_form_url'] = add_query_arg(
1158
-            array(
1159
-                'page'   => 'espresso_transactions',
1160
-                'action' => 'espresso_apply_payment',
1161
-            ),
1162
-            WP_AJAX_URL
1163
-        );
1164
-        $this->_template_args['delete_payment_form_url'] = add_query_arg(
1165
-            array(
1166
-                'page'   => 'espresso_transactions',
1167
-                'action' => 'espresso_delete_payment',
1168
-            ),
1169
-            WP_AJAX_URL
1170
-        );
1171
-
1172
-        $this->_template_args['action_buttons'] = $this->getActionButtons($this->_transaction);
1173
-
1174
-        // 'espresso_delete_payment_nonce'
1175
-
1176
-        $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php';
1177
-        echo EEH_Template::display_template($template_path, $this->_template_args, true);
1178
-    }
1179
-
1180
-
1181
-    /**
1182
-     * _get_registration_payment_IDs
1183
-     *    generates an array of Payment IDs and their corresponding Registration IDs
1184
-     *
1185
-     * @access protected
1186
-     * @param EE_Payment[] $payments
1187
-     * @return array
1188
-     * @throws EE_Error
1189
-     * @throws InvalidArgumentException
1190
-     * @throws InvalidDataTypeException
1191
-     * @throws InvalidInterfaceException
1192
-     * @throws ReflectionException
1193
-     */
1194
-    protected function _get_registration_payment_IDs($payments = array())
1195
-    {
1196
-        $existing_reg_payments = array();
1197
-        // get all reg payments for these payments
1198
-        $reg_payments = EEM_Registration_Payment::instance()->get_all(
1199
-            array(
1200
-                array(
1201
-                    'PAY_ID' => array(
1202
-                        'IN',
1203
-                        array_keys($payments),
1204
-                    ),
1205
-                ),
1206
-            )
1207
-        );
1208
-        if (! empty($reg_payments)) {
1209
-            foreach ($payments as $payment) {
1210
-                if (! $payment instanceof EE_Payment) {
1211
-                    continue;
1212
-                } elseif (! isset($existing_reg_payments[ $payment->ID() ])) {
1213
-                    $existing_reg_payments[ $payment->ID() ] = array();
1214
-                }
1215
-                foreach ($reg_payments as $reg_payment) {
1216
-                    if ($reg_payment instanceof EE_Registration_Payment
1217
-                        && $reg_payment->payment_ID() === $payment->ID()
1218
-                    ) {
1219
-                        $existing_reg_payments[ $payment->ID() ][] = $reg_payment->registration_ID();
1220
-                    }
1221
-                }
1222
-            }
1223
-        }
1224
-
1225
-        return $existing_reg_payments;
1226
-    }
1227
-
1228
-
1229
-    /**
1230
-     * _get_registrations_to_apply_payment_to
1231
-     *    generates HTML for displaying a series of checkboxes in the admin payment modal window
1232
-     * which allows the admin to only apply the payment to the specific registrations
1233
-     *
1234
-     * @access protected
1235
-     * @return void
1236
-     * @throws EE_Error
1237
-     * @throws InvalidArgumentException
1238
-     * @throws InvalidDataTypeException
1239
-     * @throws InvalidInterfaceException
1240
-     * @throws ReflectionException
1241
-     */
1242
-    protected function _get_registrations_to_apply_payment_to()
1243
-    {
1244
-        // we want any registration with an active status (ie: not deleted or cancelled)
1245
-        $query_params = array(
1246
-            array(
1247
-                'STS_ID' => array(
1248
-                    'IN',
1249
-                    array(
1250
-                        EEM_Registration::status_id_approved,
1251
-                        EEM_Registration::status_id_pending_payment,
1252
-                        EEM_Registration::status_id_not_approved,
1253
-                    ),
1254
-                ),
1255
-            ),
1256
-        );
1257
-        $registrations_to_apply_payment_to = EEH_HTML::br() . EEH_HTML::div(
1258
-            '',
1259
-            'txn-admin-apply-payment-to-registrations-dv',
1260
-            '',
1261
-            'clear: both; margin: 1.5em 0 0; display: none;'
1262
-        );
1263
-        $registrations_to_apply_payment_to .= EEH_HTML::br() . EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap');
1264
-        $registrations_to_apply_payment_to .= EEH_HTML::table('', '', 'admin-primary-mbox-tbl');
1265
-        $registrations_to_apply_payment_to .= EEH_HTML::thead(
1266
-            EEH_HTML::tr(
1267
-                EEH_HTML::th(esc_html__('ID', 'event_espresso')) .
1268
-                EEH_HTML::th(esc_html__('Registrant', 'event_espresso')) .
1269
-                EEH_HTML::th(esc_html__('Ticket', 'event_espresso')) .
1270
-                EEH_HTML::th(esc_html__('Event', 'event_espresso')) .
1271
-                EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr') .
1272
-                EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr') .
1273
-                EEH_HTML::th(esc_html__('Apply', 'event_espresso'), '', 'jst-cntr')
1274
-            )
1275
-        );
1276
-        $registrations_to_apply_payment_to .= EEH_HTML::tbody();
1277
-        // get registrations for TXN
1278
-        $registrations = $this->_transaction->registrations($query_params);
1279
-        $existing_reg_payments = $this->_template_args['existing_reg_payments'];
1280
-        foreach ($registrations as $registration) {
1281
-            if ($registration instanceof EE_Registration) {
1282
-                $attendee_name = $registration->attendee() instanceof EE_Attendee
1283
-                    ? $registration->attendee()->full_name()
1284
-                    : esc_html__('Unknown Attendee', 'event_espresso');
1285
-                $owing = $registration->final_price() - $registration->paid();
1286
-                $taxable = $registration->ticket()->taxable()
1287
-                    ? ' <span class="smaller-text lt-grey-text"> ' . esc_html__('+ tax', 'event_espresso') . '</span>'
1288
-                    : '';
1289
-                $checked = empty($existing_reg_payments)
1290
-                           || in_array($registration->ID(), $existing_reg_payments, true)
1291
-                    ? ' checked="checked"'
1292
-                    : '';
1293
-                $disabled = $registration->final_price() > 0 ? '' : ' disabled';
1294
-                $registrations_to_apply_payment_to .= EEH_HTML::tr(
1295
-                    EEH_HTML::td($registration->ID()) .
1296
-                    EEH_HTML::td($attendee_name) .
1297
-                    EEH_HTML::td(
1298
-                        $registration->ticket()->name() . ' : ' . $registration->ticket()->pretty_price() . $taxable
1299
-                    ) .
1300
-                    EEH_HTML::td($registration->event_name()) .
1301
-                    EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr') .
1302
-                    EEH_HTML::td(
1303
-                        EEH_Template::format_currency($owing),
1304
-                        '',
1305
-                        'txn-admin-payment-owing-td jst-cntr'
1306
-                    ) .
1307
-                    EEH_HTML::td(
1308
-                        '<input type="checkbox" value="' . $registration->ID()
1309
-                        . '" name="txn_admin_payment[registrations]"'
1310
-                        . $checked . $disabled . '>',
1311
-                        '',
1312
-                        'jst-cntr'
1313
-                    ),
1314
-                    'apply-payment-registration-row-' . $registration->ID()
1315
-                );
1316
-            }
1317
-        }
1318
-        $registrations_to_apply_payment_to .= EEH_HTML::tbodyx();
1319
-        $registrations_to_apply_payment_to .= EEH_HTML::tablex();
1320
-        $registrations_to_apply_payment_to .= EEH_HTML::divx();
1321
-        $registrations_to_apply_payment_to .= EEH_HTML::p(
1322
-            esc_html__(
1323
-                'The payment will only be applied to the registrations that have a check mark in their corresponding check box. Checkboxes for free registrations have been disabled.',
1324
-                'event_espresso'
1325
-            ),
1326
-            '',
1327
-            'clear description'
1328
-        );
1329
-        $registrations_to_apply_payment_to .= EEH_HTML::divx();
1330
-        $this->_template_args['registrations_to_apply_payment_to'] = $registrations_to_apply_payment_to;
1331
-    }
1332
-
1333
-
1334
-    /**
1335
-     * _get_reg_status_selection
1336
-     *
1337
-     * @todo   this will need to be adjusted either once MER comes along OR we move default reg status to tickets
1338
-     *         instead of events.
1339
-     * @access protected
1340
-     * @return void
1341
-     * @throws EE_Error
1342
-     */
1343
-    protected function _get_reg_status_selection()
1344
-    {
1345
-        // first get all possible statuses
1346
-        $statuses = EEM_Registration::reg_status_array(array(), true);
1347
-        // let's add a "don't change" option.
1348
-        $status_array['NAN'] = esc_html__('Leave the Same', 'event_espresso');
1349
-        $status_array = array_merge($status_array, $statuses);
1350
-        $this->_template_args['status_change_select'] = EEH_Form_Fields::select_input(
1351
-            'txn_reg_status_change[reg_status]',
1352
-            $status_array,
1353
-            'NAN',
1354
-            'id="txn-admin-payment-reg-status-inp"',
1355
-            'txn-reg-status-change-reg-status'
1356
-        );
1357
-        $this->_template_args['delete_status_change_select'] = EEH_Form_Fields::select_input(
1358
-            'delete_txn_reg_status_change[reg_status]',
1359
-            $status_array,
1360
-            'NAN',
1361
-            'delete-txn-admin-payment-reg-status-inp',
1362
-            'delete-txn-reg-status-change-reg-status'
1363
-        );
1364
-    }
1365
-
1366
-
1367
-    /**
1368
-     *    _get_payment_methods
1369
-     * Gets all the payment methods available generally, or the ones that are already
1370
-     * selected on these payments (in case their payment methods are no longer active).
1371
-     * Has the side-effect of updating the template args' payment_methods item
1372
-     *
1373
-     * @access private
1374
-     * @param EE_Payment[] to show on this page
1375
-     * @return void
1376
-     * @throws EE_Error
1377
-     * @throws InvalidArgumentException
1378
-     * @throws InvalidDataTypeException
1379
-     * @throws InvalidInterfaceException
1380
-     * @throws ReflectionException
1381
-     */
1382
-    private function _get_payment_methods($payments = array())
1383
-    {
1384
-        $payment_methods_of_payments = array();
1385
-        foreach ($payments as $payment) {
1386
-            if ($payment instanceof EE_Payment) {
1387
-                $payment_methods_of_payments[] = $payment->ID();
1388
-            }
1389
-        }
1390
-        if ($payment_methods_of_payments) {
1391
-            $query_args = array(
1392
-                array(
1393
-                    'OR*payment_method_for_payment' => array(
1394
-                        'PMD_ID'    => array('IN', $payment_methods_of_payments),
1395
-                        'PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'),
1396
-                    ),
1397
-                ),
1398
-            );
1399
-        } else {
1400
-            $query_args = array(array('PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%')));
1401
-        }
1402
-        $this->_template_args['payment_methods'] = EEM_Payment_Method::instance()->get_all($query_args);
1403
-    }
1404
-
1405
-
1406
-    /**
1407
-     * txn_attendees_meta_box
1408
-     *    generates HTML for the Attendees Transaction main meta box
1409
-     *
1410
-     * @access public
1411
-     * @param WP_Post $post
1412
-     * @param array   $metabox
1413
-     * @return void
1414
-     * @throws DomainException
1415
-     * @throws EE_Error
1416
-     * @throws InvalidArgumentException
1417
-     * @throws InvalidDataTypeException
1418
-     * @throws InvalidInterfaceException
1419
-     * @throws ReflectionException
1420
-     */
1421
-    public function txn_attendees_meta_box($post, $metabox = array('args' => array()))
1422
-    {
1423
-
1424
-        /** @noinspection NonSecureExtractUsageInspection */
1425
-        extract($metabox['args']);
1426
-        $this->_template_args['post'] = $post;
1427
-        $this->_template_args['event_attendees'] = array();
1428
-        // process items in cart
1429
-        $line_items = $this->_transaction->get_many_related(
1430
-            'Line_Item',
1431
-            array(array('LIN_type' => 'line-item'))
1432
-        );
1433
-        if (! empty($line_items)) {
1434
-            foreach ($line_items as $item) {
1435
-                if ($item instanceof EE_Line_Item) {
1436
-                    switch ($item->OBJ_type()) {
1437
-                        case 'Event':
1438
-                            break;
1439
-                        case 'Ticket':
1440
-                            $ticket = $item->ticket();
1441
-                            // right now we're only handling tickets here.
1442
-                            // Cause its expected that only tickets will have attendees right?
1443
-                            if (! $ticket instanceof EE_Ticket) {
1444
-                                break;
1445
-                            }
1446
-                            try {
1447
-                                $event_name = $ticket->get_event_name();
1448
-                            } catch (Exception $e) {
1449
-                                EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
1450
-                                $event_name = esc_html__('Unknown Event', 'event_espresso');
1451
-                            }
1452
-                            $event_name .= ' - ' . $item->name();
1453
-                            $ticket_price = EEH_Template::format_currency($item->unit_price());
1454
-                            // now get all of the registrations for this transaction that use this ticket
1455
-                            $registrations = $ticket->registrations(
1456
-                                array(array('TXN_ID' => $this->_transaction->ID()))
1457
-                            );
1458
-                            foreach ($registrations as $registration) {
1459
-                                if (! $registration instanceof EE_Registration) {
1460
-                                    break;
1461
-                                }
1462
-                                $this->_template_args['event_attendees'][ $registration->ID() ]['STS_ID']
1463
-                                    = $registration->status_ID();
1464
-                                $this->_template_args['event_attendees'][ $registration->ID() ]['att_num']
1465
-                                    = $registration->count();
1466
-                                $this->_template_args['event_attendees'][ $registration->ID() ]['event_ticket_name']
1467
-                                    = $event_name;
1468
-                                $this->_template_args['event_attendees'][ $registration->ID() ]['ticket_price']
1469
-                                    = $ticket_price;
1470
-                                // attendee info
1471
-                                $attendee = $registration->get_first_related('Attendee');
1472
-                                if ($attendee instanceof EE_Attendee) {
1473
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['att_id']
1474
-                                        = $attendee->ID();
1475
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['attendee']
1476
-                                        = $attendee->full_name();
1477
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['email']
1478
-                                        = '<a href="mailto:' . $attendee->email() . '?subject=' . $event_name
1479
-                                          . esc_html__(
1480
-                                              ' Event',
1481
-                                              'event_espresso'
1482
-                                          )
1483
-                                          . '">' . $attendee->email() . '</a>';
1484
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['address']
1485
-                                        = EEH_Address::format($attendee, 'inline', false, false);
1486
-                                } else {
1487
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] = '';
1488
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] = '';
1489
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['email'] = '';
1490
-                                    $this->_template_args['event_attendees'][ $registration->ID() ]['address'] = '';
1491
-                                }
1492
-                            }
1493
-                            break;
1494
-                    }
1495
-                }
1496
-            }
1497
-
1498
-            $this->_template_args['transaction_form_url'] = add_query_arg(
1499
-                array(
1500
-                    'action'  => 'edit_transaction',
1501
-                    'process' => 'attendees',
1502
-                ),
1503
-                TXN_ADMIN_URL
1504
-            );
1505
-            echo EEH_Template::display_template(
1506
-                TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_attendees.template.php',
1507
-                $this->_template_args,
1508
-                true
1509
-            );
1510
-        } else {
1511
-            echo sprintf(
1512
-                esc_html__(
1513
-                    '%1$sFor some reason, there are no attendees registered for this transaction. Likely the registration was abandoned in process.%2$s',
1514
-                    'event_espresso'
1515
-                ),
1516
-                '<p class="important-notice">',
1517
-                '</p>'
1518
-            );
1519
-        }
1520
-    }
1521
-
1522
-
1523
-    /**
1524
-     * txn_registrant_side_meta_box
1525
-     * generates HTML for the Edit Transaction side meta box
1526
-     *
1527
-     * @access public
1528
-     * @return void
1529
-     * @throws DomainException
1530
-     * @throws EE_Error
1531
-     * @throws InvalidArgumentException
1532
-     * @throws InvalidDataTypeException
1533
-     * @throws InvalidInterfaceException
1534
-     * @throws ReflectionException
1535
-     */
1536
-    public function txn_registrant_side_meta_box()
1537
-    {
1538
-        $primary_att = $this->_transaction->primary_registration() instanceof EE_Registration
1539
-            ? $this->_transaction->primary_registration()->get_first_related('Attendee')
1540
-            : null;
1541
-        if (! $primary_att instanceof EE_Attendee) {
1542
-            $this->_template_args['no_attendee_message'] = esc_html__(
1543
-                'There is no attached contact for this transaction.  The transaction either failed due to an error or was abandoned.',
1544
-                'event_espresso'
1545
-            );
1546
-            $primary_att = EEM_Attendee::instance()->create_default_object();
1547
-        }
1548
-        $this->_template_args['ATT_ID'] = $primary_att->ID();
1549
-        $this->_template_args['prime_reg_fname'] = $primary_att->fname();
1550
-        $this->_template_args['prime_reg_lname'] = $primary_att->lname();
1551
-        $this->_template_args['prime_reg_email'] = $primary_att->email();
1552
-        $this->_template_args['prime_reg_phone'] = $primary_att->phone();
1553
-        $this->_template_args['edit_attendee_url'] = EE_Admin_Page::add_query_args_and_nonce(
1554
-            array(
1555
-                'action' => 'edit_attendee',
1556
-                'post'   => $primary_att->ID(),
1557
-            ),
1558
-            REG_ADMIN_URL
1559
-        );
1560
-        // get formatted address for registrant
1561
-        $this->_template_args['formatted_address'] = EEH_Address::format($primary_att);
1562
-        echo EEH_Template::display_template(
1563
-            TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php',
1564
-            $this->_template_args,
1565
-            true
1566
-        );
1567
-    }
1568
-
1569
-
1570
-    /**
1571
-     * txn_billing_info_side_meta_box
1572
-     *    generates HTML for the Edit Transaction side meta box
1573
-     *
1574
-     * @access public
1575
-     * @return void
1576
-     * @throws DomainException
1577
-     * @throws EE_Error
1578
-     */
1579
-    public function txn_billing_info_side_meta_box()
1580
-    {
1581
-
1582
-        $this->_template_args['billing_form'] = $this->_transaction->billing_info();
1583
-        $this->_template_args['billing_form_url'] = add_query_arg(
1584
-            array('action' => 'edit_transaction', 'process' => 'billing'),
1585
-            TXN_ADMIN_URL
1586
-        );
1587
-
1588
-        $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_billing_info.template.php';
1589
-        echo EEH_Template::display_template($template_path, $this->_template_args, true);
1590
-    }
1591
-
1592
-
1593
-    /**
1594
-     * apply_payments_or_refunds
1595
-     *    registers a payment or refund made towards a transaction
1596
-     *
1597
-     * @access public
1598
-     * @return void
1599
-     * @throws EE_Error
1600
-     * @throws InvalidArgumentException
1601
-     * @throws ReflectionException
1602
-     * @throws RuntimeException
1603
-     * @throws InvalidDataTypeException
1604
-     * @throws InvalidInterfaceException
1605
-     */
1606
-    public function apply_payments_or_refunds()
1607
-    {
1608
-        $json_response_data = array('return_data' => false);
1609
-        $valid_data = $this->_validate_payment_request_data();
1610
-        $has_access = EE_Registry::instance()->CAP->current_user_can(
1611
-            'ee_edit_payments',
1612
-            'apply_payment_or_refund_from_registration_details'
1613
-        );
1614
-        if (! empty($valid_data) && $has_access) {
1615
-            $PAY_ID = $valid_data['PAY_ID'];
1616
-            // save  the new payment
1617
-            $payment = $this->_create_payment_from_request_data($valid_data);
1618
-            // get the TXN for this payment
1619
-            $transaction = $payment->transaction();
1620
-            // verify transaction
1621
-            if ($transaction instanceof EE_Transaction) {
1622
-                // calculate_total_payments_and_update_status
1623
-                $this->_process_transaction_payments($transaction);
1624
-                $REG_IDs = $this->_get_REG_IDs_to_apply_payment_to($payment);
1625
-                $this->_remove_existing_registration_payments($payment, $PAY_ID);
1626
-                // apply payment to registrations (if applicable)
1627
-                if (! empty($REG_IDs)) {
1628
-                    $this->_update_registration_payments($transaction, $payment, $REG_IDs);
1629
-                    $this->_maybe_send_notifications();
1630
-                    // now process status changes for the same registrations
1631
-                    $this->_process_registration_status_change($transaction, $REG_IDs);
1632
-                }
1633
-                $this->_maybe_send_notifications($payment);
1634
-                // prepare to render page
1635
-                $json_response_data['return_data'] = $this->_build_payment_json_response($payment, $REG_IDs);
1636
-                do_action(
1637
-                    'AHEE__Transactions_Admin_Page__apply_payments_or_refund__after_recording',
1638
-                    $transaction,
1639
-                    $payment
1640
-                );
1641
-            } else {
1642
-                EE_Error::add_error(
1643
-                    esc_html__(
1644
-                        'A valid Transaction for this payment could not be retrieved.',
1645
-                        'event_espresso'
1646
-                    ),
1647
-                    __FILE__,
1648
-                    __FUNCTION__,
1649
-                    __LINE__
1650
-                );
1651
-            }
1652
-        } elseif ($has_access) {
1653
-            EE_Error::add_error(
1654
-                esc_html__(
1655
-                    'The payment form data could not be processed. Please try again.',
1656
-                    'event_espresso'
1657
-                ),
1658
-                __FILE__,
1659
-                __FUNCTION__,
1660
-                __LINE__
1661
-            );
1662
-        } else {
1663
-            EE_Error::add_error(
1664
-                esc_html__(
1665
-                    'You do not have access to apply payments or refunds to a registration.',
1666
-                    'event_espresso'
1667
-                ),
1668
-                __FILE__,
1669
-                __FUNCTION__,
1670
-                __LINE__
1671
-            );
1672
-        }
1673
-        $notices = EE_Error::get_notices(
1674
-            false,
1675
-            false,
1676
-            false
1677
-        );
1678
-        $this->_template_args = array(
1679
-            'data'    => $json_response_data,
1680
-            'error'   => $notices['errors'],
1681
-            'success' => $notices['success'],
1682
-        );
1683
-        $this->_return_json();
1684
-    }
1685
-
1686
-
1687
-    /**
1688
-     * _validate_payment_request_data
1689
-     *
1690
-     * @return array
1691
-     * @throws EE_Error
1692
-     * @throws InvalidArgumentException
1693
-     * @throws InvalidDataTypeException
1694
-     * @throws InvalidInterfaceException
1695
-     */
1696
-    protected function _validate_payment_request_data()
1697
-    {
1698
-        if (! isset($this->_req_data['txn_admin_payment'])) {
1699
-            return array();
1700
-        }
1701
-        $payment_form = $this->_generate_payment_form_section();
1702
-        try {
1703
-            if ($payment_form->was_submitted()) {
1704
-                $payment_form->receive_form_submission();
1705
-                if (! $payment_form->is_valid()) {
1706
-                    $submission_error_messages = array();
1707
-                    foreach ($payment_form->get_validation_errors_accumulated() as $validation_error) {
1708
-                        if ($validation_error instanceof EE_Validation_Error) {
1709
-                            $submission_error_messages[] = sprintf(
1710
-                                _x('%s : %s', 'Form Section Name : Form Validation Error', 'event_espresso'),
1711
-                                $validation_error->get_form_section()->html_label_text(),
1712
-                                $validation_error->getMessage()
1713
-                            );
1714
-                        }
1715
-                    }
1716
-                    EE_Error::add_error(
1717
-                        implode('<br />', $submission_error_messages),
1718
-                        __FILE__,
1719
-                        __FUNCTION__,
1720
-                        __LINE__
1721
-                    );
1722
-                    return array();
1723
-                }
1724
-            }
1725
-        } catch (EE_Error $e) {
1726
-            EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
1727
-            return array();
1728
-        }
1729
-
1730
-        return $payment_form->valid_data();
1731
-    }
1732
-
1733
-
1734
-    /**
1735
-     * _generate_payment_form_section
1736
-     *
1737
-     * @return EE_Form_Section_Proper
1738
-     * @throws EE_Error
1739
-     */
1740
-    protected function _generate_payment_form_section()
1741
-    {
1742
-        return new EE_Form_Section_Proper(
1743
-            array(
1744
-                'name'        => 'txn_admin_payment',
1745
-                'subsections' => array(
1746
-                    'PAY_ID'          => new EE_Text_Input(
1747
-                        array(
1748
-                            'default'               => 0,
1749
-                            'required'              => false,
1750
-                            'html_label_text'       => esc_html__('Payment ID', 'event_espresso'),
1751
-                            'validation_strategies' => array(new EE_Int_Normalization()),
1752
-                        )
1753
-                    ),
1754
-                    'TXN_ID'          => new EE_Text_Input(
1755
-                        array(
1756
-                            'default'               => 0,
1757
-                            'required'              => true,
1758
-                            'html_label_text'       => esc_html__('Transaction ID', 'event_espresso'),
1759
-                            'validation_strategies' => array(new EE_Int_Normalization()),
1760
-                        )
1761
-                    ),
1762
-                    'type'            => new EE_Text_Input(
1763
-                        array(
1764
-                            'default'               => 1,
1765
-                            'required'              => true,
1766
-                            'html_label_text'       => esc_html__('Payment or Refund', 'event_espresso'),
1767
-                            'validation_strategies' => array(new EE_Int_Normalization()),
1768
-                        )
1769
-                    ),
1770
-                    'amount'          => new EE_Text_Input(
1771
-                        array(
1772
-                            'default'               => 0,
1773
-                            'required'              => true,
1774
-                            'html_label_text'       => esc_html__('Payment amount', 'event_espresso'),
1775
-                            'validation_strategies' => array(new EE_Float_Normalization()),
1776
-                        )
1777
-                    ),
1778
-                    'status'          => new EE_Text_Input(
1779
-                        array(
1780
-                            'default'         => EEM_Payment::status_id_approved,
1781
-                            'required'        => true,
1782
-                            'html_label_text' => esc_html__('Payment status', 'event_espresso'),
1783
-                        )
1784
-                    ),
1785
-                    'PMD_ID'          => new EE_Text_Input(
1786
-                        array(
1787
-                            'default'               => 2,
1788
-                            'required'              => true,
1789
-                            'html_label_text'       => esc_html__('Payment Method', 'event_espresso'),
1790
-                            'validation_strategies' => array(new EE_Int_Normalization()),
1791
-                        )
1792
-                    ),
1793
-                    'date'            => new EE_Text_Input(
1794
-                        array(
1795
-                            'default'         => time(),
1796
-                            'required'        => true,
1797
-                            'html_label_text' => esc_html__('Payment date', 'event_espresso'),
1798
-                        )
1799
-                    ),
1800
-                    'txn_id_chq_nmbr' => new EE_Text_Input(
1801
-                        array(
1802
-                            'default'               => '',
1803
-                            'required'              => false,
1804
-                            'html_label_text'       => esc_html__('Transaction or Cheque Number', 'event_espresso'),
1805
-                            'validation_strategies' => array(
1806
-                                new EE_Max_Length_Validation_Strategy(
1807
-                                    esc_html__('Input too long', 'event_espresso'),
1808
-                                    100
1809
-                                ),
1810
-                            ),
1811
-                        )
1812
-                    ),
1813
-                    'po_number'       => new EE_Text_Input(
1814
-                        array(
1815
-                            'default'               => '',
1816
-                            'required'              => false,
1817
-                            'html_label_text'       => esc_html__('Purchase Order Number', 'event_espresso'),
1818
-                            'validation_strategies' => array(
1819
-                                new EE_Max_Length_Validation_Strategy(
1820
-                                    esc_html__('Input too long', 'event_espresso'),
1821
-                                    100
1822
-                                ),
1823
-                            ),
1824
-                        )
1825
-                    ),
1826
-                    'accounting'      => new EE_Text_Input(
1827
-                        array(
1828
-                            'default'               => '',
1829
-                            'required'              => false,
1830
-                            'html_label_text'       => esc_html__('Extra Field for Accounting', 'event_espresso'),
1831
-                            'validation_strategies' => array(
1832
-                                new EE_Max_Length_Validation_Strategy(
1833
-                                    esc_html__('Input too long', 'event_espresso'),
1834
-                                    100
1835
-                                ),
1836
-                            ),
1837
-                        )
1838
-                    ),
1839
-                ),
1840
-            )
1841
-        );
1842
-    }
1843
-
1844
-
1845
-    /**
1846
-     * _create_payment_from_request_data
1847
-     *
1848
-     * @param array $valid_data
1849
-     * @return EE_Payment
1850
-     * @throws EE_Error
1851
-     * @throws InvalidArgumentException
1852
-     * @throws InvalidDataTypeException
1853
-     * @throws InvalidInterfaceException
1854
-     * @throws ReflectionException
1855
-     */
1856
-    protected function _create_payment_from_request_data($valid_data)
1857
-    {
1858
-        $PAY_ID = $valid_data['PAY_ID'];
1859
-        // get payment amount
1860
-        $amount = $valid_data['amount'] ? abs($valid_data['amount']) : 0;
1861
-        // payments have a type value of 1 and refunds have a type value of -1
1862
-        // so multiplying amount by type will give a positive value for payments, and negative values for refunds
1863
-        $amount = $valid_data['type'] < 0 ? $amount * -1 : $amount;
1864
-        // for some reason the date string coming in has extra spaces between the date and time.  This fixes that.
1865
-        $date = $valid_data['date']
1866
-            ? preg_replace('/\s+/', ' ', $valid_data['date'])
1867
-            : date('Y-m-d g:i a', current_time('timestamp'));
1868
-        $payment = EE_Payment::new_instance(
1869
-            array(
1870
-                'TXN_ID'              => $valid_data['TXN_ID'],
1871
-                'STS_ID'              => $valid_data['status'],
1872
-                'PAY_timestamp'       => $date,
1873
-                'PAY_source'          => EEM_Payment_Method::scope_admin,
1874
-                'PMD_ID'              => $valid_data['PMD_ID'],
1875
-                'PAY_amount'          => $amount,
1876
-                'PAY_txn_id_chq_nmbr' => $valid_data['txn_id_chq_nmbr'],
1877
-                'PAY_po_number'       => $valid_data['po_number'],
1878
-                'PAY_extra_accntng'   => $valid_data['accounting'],
1879
-                'PAY_details'         => $valid_data,
1880
-                'PAY_ID'              => $PAY_ID,
1881
-            ),
1882
-            '',
1883
-            array('Y-m-d', 'g:i a')
1884
-        );
1885
-
1886
-        if (! $payment->save()) {
1887
-            EE_Error::add_error(
1888
-                sprintf(
1889
-                    esc_html__('Payment %1$d has not been successfully saved to the database.', 'event_espresso'),
1890
-                    $payment->ID()
1891
-                ),
1892
-                __FILE__,
1893
-                __FUNCTION__,
1894
-                __LINE__
1895
-            );
1896
-        }
1897
-
1898
-        return $payment;
1899
-    }
1900
-
1901
-
1902
-    /**
1903
-     * _process_transaction_payments
1904
-     *
1905
-     * @param \EE_Transaction $transaction
1906
-     * @return void
1907
-     * @throws EE_Error
1908
-     * @throws InvalidArgumentException
1909
-     * @throws ReflectionException
1910
-     * @throws InvalidDataTypeException
1911
-     * @throws InvalidInterfaceException
1912
-     */
1913
-    protected function _process_transaction_payments(EE_Transaction $transaction)
1914
-    {
1915
-        /** @type EE_Transaction_Payments $transaction_payments */
1916
-        $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
1917
-        // update the transaction with this payment
1918
-        if ($transaction_payments->calculate_total_payments_and_update_status($transaction)) {
1919
-            EE_Error::add_success(
1920
-                esc_html__(
1921
-                    'The payment has been processed successfully.',
1922
-                    'event_espresso'
1923
-                ),
1924
-                __FILE__,
1925
-                __FUNCTION__,
1926
-                __LINE__
1927
-            );
1928
-        } else {
1929
-            EE_Error::add_error(
1930
-                esc_html__(
1931
-                    'The payment was processed successfully but the amount paid for the transaction was not updated.',
1932
-                    'event_espresso'
1933
-                ),
1934
-                __FILE__,
1935
-                __FUNCTION__,
1936
-                __LINE__
1937
-            );
1938
-        }
1939
-    }
1940
-
1941
-
1942
-    /**
1943
-     * _get_REG_IDs_to_apply_payment_to
1944
-     * returns a list of registration IDs that the payment will apply to
1945
-     *
1946
-     * @param \EE_Payment $payment
1947
-     * @return array
1948
-     * @throws EE_Error
1949
-     * @throws InvalidArgumentException
1950
-     * @throws InvalidDataTypeException
1951
-     * @throws InvalidInterfaceException
1952
-     * @throws ReflectionException
1953
-     */
1954
-    protected function _get_REG_IDs_to_apply_payment_to(EE_Payment $payment)
1955
-    {
1956
-        $REG_IDs = array();
1957
-        // grab array of IDs for specific registrations to apply changes to
1958
-        if (isset($this->_req_data['txn_admin_payment']['registrations'])) {
1959
-            $REG_IDs = (array) $this->_req_data['txn_admin_payment']['registrations'];
1960
-        }
1961
-        // nothing specified ? then get all reg IDs
1962
-        if (empty($REG_IDs)) {
1963
-            $registrations = $payment->transaction()->registrations();
1964
-            $REG_IDs = ! empty($registrations)
1965
-                ? array_keys($registrations)
1966
-                : $this->_get_existing_reg_payment_REG_IDs($payment);
1967
-        }
1968
-
1969
-        // ensure that REG_IDs are integers and NOT strings
1970
-        return array_map('intval', $REG_IDs);
1971
-    }
1972
-
1973
-
1974
-    /**
1975
-     * @return array
1976
-     */
1977
-    public function existing_reg_payment_REG_IDs()
1978
-    {
1979
-        return $this->_existing_reg_payment_REG_IDs;
1980
-    }
1981
-
1982
-
1983
-    /**
1984
-     * @param array $existing_reg_payment_REG_IDs
1985
-     */
1986
-    public function set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs = null)
1987
-    {
1988
-        $this->_existing_reg_payment_REG_IDs = $existing_reg_payment_REG_IDs;
1989
-    }
1990
-
1991
-
1992
-    /**
1993
-     * _get_existing_reg_payment_REG_IDs
1994
-     * returns a list of registration IDs that the payment is currently related to
1995
-     * as recorded in the database
1996
-     *
1997
-     * @param \EE_Payment $payment
1998
-     * @return array
1999
-     * @throws EE_Error
2000
-     * @throws InvalidArgumentException
2001
-     * @throws InvalidDataTypeException
2002
-     * @throws InvalidInterfaceException
2003
-     * @throws ReflectionException
2004
-     */
2005
-    protected function _get_existing_reg_payment_REG_IDs(EE_Payment $payment)
2006
-    {
2007
-        if ($this->existing_reg_payment_REG_IDs() === null) {
2008
-            // let's get any existing reg payment records for this payment
2009
-            $existing_reg_payment_REG_IDs = $payment->get_many_related('Registration');
2010
-            // but we only want the REG IDs, so grab the array keys
2011
-            $existing_reg_payment_REG_IDs = ! empty($existing_reg_payment_REG_IDs)
2012
-                ? array_keys($existing_reg_payment_REG_IDs)
2013
-                : array();
2014
-            $this->set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs);
2015
-        }
2016
-
2017
-        return $this->existing_reg_payment_REG_IDs();
2018
-    }
2019
-
2020
-
2021
-    /**
2022
-     * _remove_existing_registration_payments
2023
-     * this calculates the difference between existing relations
2024
-     * to the supplied payment and the new list registration IDs,
2025
-     * removes any related registrations that no longer apply,
2026
-     * and then updates the registration paid fields
2027
-     *
2028
-     * @param \EE_Payment $payment
2029
-     * @param int         $PAY_ID
2030
-     * @return bool;
2031
-     * @throws EE_Error
2032
-     * @throws InvalidArgumentException
2033
-     * @throws ReflectionException
2034
-     * @throws InvalidDataTypeException
2035
-     * @throws InvalidInterfaceException
2036
-     */
2037
-    protected function _remove_existing_registration_payments(EE_Payment $payment, $PAY_ID = 0)
2038
-    {
2039
-        // newly created payments will have nothing recorded for $PAY_ID
2040
-        if (absint($PAY_ID) === 0) {
2041
-            return false;
2042
-        }
2043
-        $existing_reg_payment_REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment);
2044
-        if (empty($existing_reg_payment_REG_IDs)) {
2045
-            return false;
2046
-        }
2047
-        /** @type EE_Transaction_Payments $transaction_payments */
2048
-        $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
2049
-
2050
-        return $transaction_payments->delete_registration_payments_and_update_registrations(
2051
-            $payment,
2052
-            array(
2053
-                array(
2054
-                    'PAY_ID' => $payment->ID(),
2055
-                    'REG_ID' => array('IN', $existing_reg_payment_REG_IDs),
2056
-                ),
2057
-            )
2058
-        );
2059
-    }
2060
-
2061
-
2062
-    /**
2063
-     * _update_registration_payments
2064
-     * this applies the payments to the selected registrations
2065
-     * but only if they have not already been paid for
2066
-     *
2067
-     * @param  EE_Transaction $transaction
2068
-     * @param \EE_Payment     $payment
2069
-     * @param array           $REG_IDs
2070
-     * @return void
2071
-     * @throws EE_Error
2072
-     * @throws InvalidArgumentException
2073
-     * @throws ReflectionException
2074
-     * @throws RuntimeException
2075
-     * @throws InvalidDataTypeException
2076
-     * @throws InvalidInterfaceException
2077
-     */
2078
-    protected function _update_registration_payments(
2079
-        EE_Transaction $transaction,
2080
-        EE_Payment $payment,
2081
-        $REG_IDs = array()
2082
-    ) {
2083
-        // we can pass our own custom set of registrations to EE_Payment_Processor::process_registration_payments()
2084
-        // so let's do that using our set of REG_IDs from the form
2085
-        $registration_query_where_params = array(
2086
-            'REG_ID' => array('IN', $REG_IDs),
2087
-        );
2088
-        // but add in some conditions regarding payment,
2089
-        // so that we don't apply payments to registrations that are free or have already been paid for
2090
-        // but ONLY if the payment is NOT a refund ( ie: the payment amount is not negative )
2091
-        if (! $payment->is_a_refund()) {
2092
-            $registration_query_where_params['REG_final_price'] = array('!=', 0);
2093
-            $registration_query_where_params['REG_final_price*'] = array('!=', 'REG_paid', true);
2094
-        }
2095
-        $registrations = $transaction->registrations(array($registration_query_where_params));
2096
-        if (! empty($registrations)) {
2097
-            /** @type EE_Payment_Processor $payment_processor */
2098
-            $payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
2099
-            $payment_processor->process_registration_payments($transaction, $payment, $registrations);
2100
-        }
2101
-    }
2102
-
2103
-
2104
-    /**
2105
-     * _process_registration_status_change
2106
-     * This processes requested registration status changes for all the registrations
2107
-     * on a given transaction and (optionally) sends out notifications for the changes.
2108
-     *
2109
-     * @param  EE_Transaction $transaction
2110
-     * @param array           $REG_IDs
2111
-     * @return bool
2112
-     * @throws EE_Error
2113
-     * @throws InvalidArgumentException
2114
-     * @throws ReflectionException
2115
-     * @throws InvalidDataTypeException
2116
-     * @throws InvalidInterfaceException
2117
-     */
2118
-    protected function _process_registration_status_change(EE_Transaction $transaction, $REG_IDs = array())
2119
-    {
2120
-        // first if there is no change in status then we get out.
2121
-        if (! isset($this->_req_data['txn_reg_status_change']['reg_status'])
2122
-            || $this->_req_data['txn_reg_status_change']['reg_status'] === 'NAN'
2123
-        ) {
2124
-            // no error message, no change requested, just nothing to do man.
2125
-            return false;
2126
-        }
2127
-        /** @type EE_Transaction_Processor $transaction_processor */
2128
-        $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
2129
-
2130
-        // made it here dude?  Oh WOW.  K, let's take care of changing the statuses
2131
-        return $transaction_processor->manually_update_registration_statuses(
2132
-            $transaction,
2133
-            sanitize_text_field($this->_req_data['txn_reg_status_change']['reg_status']),
2134
-            array(array('REG_ID' => array('IN', $REG_IDs)))
2135
-        );
2136
-    }
2137
-
2138
-
2139
-    /**
2140
-     * _build_payment_json_response
2141
-     *
2142
-     * @access public
2143
-     * @param \EE_Payment $payment
2144
-     * @param array       $REG_IDs
2145
-     * @param bool | null $delete_txn_reg_status_change
2146
-     * @return array
2147
-     * @throws EE_Error
2148
-     * @throws InvalidArgumentException
2149
-     * @throws InvalidDataTypeException
2150
-     * @throws InvalidInterfaceException
2151
-     * @throws ReflectionException
2152
-     */
2153
-    protected function _build_payment_json_response(
2154
-        EE_Payment $payment,
2155
-        $REG_IDs = array(),
2156
-        $delete_txn_reg_status_change = null
2157
-    ) {
2158
-        // was the payment deleted ?
2159
-        if (is_bool($delete_txn_reg_status_change)) {
2160
-            return array(
2161
-                'PAY_ID'                       => $payment->ID(),
2162
-                'amount'                       => $payment->amount(),
2163
-                'total_paid'                   => $payment->transaction()->paid(),
2164
-                'txn_status'                   => $payment->transaction()->status_ID(),
2165
-                'pay_status'                   => $payment->STS_ID(),
2166
-                'registrations'                => $this->_registration_payment_data_array($REG_IDs),
2167
-                'delete_txn_reg_status_change' => $delete_txn_reg_status_change,
2168
-            );
2169
-        } else {
2170
-            $this->_get_payment_status_array();
2171
-
2172
-            return array(
2173
-                'amount'           => $payment->amount(),
2174
-                'total_paid'       => $payment->transaction()->paid(),
2175
-                'txn_status'       => $payment->transaction()->status_ID(),
2176
-                'pay_status'       => $payment->STS_ID(),
2177
-                'PAY_ID'           => $payment->ID(),
2178
-                'STS_ID'           => $payment->STS_ID(),
2179
-                'status'           => self::$_pay_status[ $payment->STS_ID() ],
2180
-                'date'             => $payment->timestamp('Y-m-d', 'h:i a'),
2181
-                'method'           => strtoupper($payment->source()),
2182
-                'PM_ID'            => $payment->payment_method() ? $payment->payment_method()->ID() : 1,
2183
-                'gateway'          => $payment->payment_method()
2184
-                    ? $payment->payment_method()->admin_name()
2185
-                    : esc_html__('Unknown', 'event_espresso'),
2186
-                'gateway_response' => $payment->gateway_response(),
2187
-                'txn_id_chq_nmbr'  => $payment->txn_id_chq_nmbr(),
2188
-                'po_number'        => $payment->po_number(),
2189
-                'extra_accntng'    => $payment->extra_accntng(),
2190
-                'registrations'    => $this->_registration_payment_data_array($REG_IDs),
2191
-            );
2192
-        }
2193
-    }
2194
-
2195
-
2196
-    /**
2197
-     * delete_payment
2198
-     *    delete a payment or refund made towards a transaction
2199
-     *
2200
-     * @access public
2201
-     * @return void
2202
-     * @throws EE_Error
2203
-     * @throws InvalidArgumentException
2204
-     * @throws ReflectionException
2205
-     * @throws InvalidDataTypeException
2206
-     * @throws InvalidInterfaceException
2207
-     */
2208
-    public function delete_payment()
2209
-    {
2210
-        $json_response_data = array('return_data' => false);
2211
-        $PAY_ID = isset($this->_req_data['delete_txn_admin_payment']['PAY_ID'])
2212
-            ? absint($this->_req_data['delete_txn_admin_payment']['PAY_ID'])
2213
-            : 0;
2214
-        $can_delete = EE_Registry::instance()->CAP->current_user_can(
2215
-            'ee_delete_payments',
2216
-            'delete_payment_from_registration_details'
2217
-        );
2218
-        if ($PAY_ID && $can_delete) {
2219
-            $delete_txn_reg_status_change = isset($this->_req_data['delete_txn_reg_status_change'])
2220
-                ? $this->_req_data['delete_txn_reg_status_change']
2221
-                : false;
2222
-            $payment = EEM_Payment::instance()->get_one_by_ID($PAY_ID);
2223
-            if ($payment instanceof EE_Payment) {
2224
-                $REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment);
2225
-                /** @type EE_Transaction_Payments $transaction_payments */
2226
-                $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
2227
-                if ($transaction_payments->delete_payment_and_update_transaction($payment)) {
2228
-                    $json_response_data['return_data'] = $this->_build_payment_json_response(
2229
-                        $payment,
2230
-                        $REG_IDs,
2231
-                        $delete_txn_reg_status_change
2232
-                    );
2233
-                    if ($delete_txn_reg_status_change) {
2234
-                        $this->_req_data['txn_reg_status_change'] = $delete_txn_reg_status_change;
2235
-                        // MAKE sure we also add the delete_txn_req_status_change to the
2236
-                        // $_REQUEST global because that's how messages will be looking for it.
2237
-                        $_REQUEST['txn_reg_status_change'] = $delete_txn_reg_status_change;
2238
-                        $this->_maybe_send_notifications();
2239
-                        $this->_process_registration_status_change($payment->transaction(), $REG_IDs);
2240
-                    }
2241
-                }
2242
-            } else {
2243
-                EE_Error::add_error(
2244
-                    esc_html__('Valid Payment data could not be retrieved from the database.', 'event_espresso'),
2245
-                    __FILE__,
2246
-                    __FUNCTION__,
2247
-                    __LINE__
2248
-                );
2249
-            }
2250
-        } elseif ($can_delete) {
2251
-            EE_Error::add_error(
2252
-                esc_html__(
2253
-                    'A valid Payment ID was not received, therefore payment form data could not be loaded.',
2254
-                    'event_espresso'
2255
-                ),
2256
-                __FILE__,
2257
-                __FUNCTION__,
2258
-                __LINE__
2259
-            );
2260
-        } else {
2261
-            EE_Error::add_error(
2262
-                esc_html__(
2263
-                    'You do not have access to delete a payment.',
2264
-                    'event_espresso'
2265
-                ),
2266
-                __FILE__,
2267
-                __FUNCTION__,
2268
-                __LINE__
2269
-            );
2270
-        }
2271
-        $notices = EE_Error::get_notices(false, false, false);
2272
-        $this->_template_args = array(
2273
-            'data'      => $json_response_data,
2274
-            'success'   => $notices['success'],
2275
-            'error'     => $notices['errors'],
2276
-            'attention' => $notices['attention'],
2277
-        );
2278
-        $this->_return_json();
2279
-    }
2280
-
2281
-
2282
-    /**
2283
-     * _registration_payment_data_array
2284
-     * adds info for 'owing' and 'paid' for each registration to the json response
2285
-     *
2286
-     * @access protected
2287
-     * @param array $REG_IDs
2288
-     * @return array
2289
-     * @throws EE_Error
2290
-     * @throws InvalidArgumentException
2291
-     * @throws InvalidDataTypeException
2292
-     * @throws InvalidInterfaceException
2293
-     * @throws ReflectionException
2294
-     */
2295
-    protected function _registration_payment_data_array($REG_IDs)
2296
-    {
2297
-        $registration_payment_data = array();
2298
-        // if non empty reg_ids lets get an array of registrations and update the values for the apply_payment/refund rows.
2299
-        if (! empty($REG_IDs)) {
2300
-            $registrations = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $REG_IDs))));
2301
-            foreach ($registrations as $registration) {
2302
-                if ($registration instanceof EE_Registration) {
2303
-                    $registration_payment_data[ $registration->ID() ] = array(
2304
-                        'paid'  => $registration->pretty_paid(),
2305
-                        'owing' => EEH_Template::format_currency($registration->final_price() - $registration->paid()),
2306
-                    );
2307
-                }
2308
-            }
2309
-        }
2310
-
2311
-        return $registration_payment_data;
2312
-    }
2313
-
2314
-
2315
-    /**
2316
-     * _maybe_send_notifications
2317
-     * determines whether or not the admin has indicated that notifications should be sent.
2318
-     * If so, will toggle a filter switch for delivering registration notices.
2319
-     * If passed an EE_Payment object, then it will trigger payment notifications instead.
2320
-     *
2321
-     * @access protected
2322
-     * @param \EE_Payment | null $payment
2323
-     */
2324
-    protected function _maybe_send_notifications($payment = null)
2325
-    {
2326
-        switch ($payment instanceof EE_Payment) {
2327
-            // payment notifications
2328
-            case true:
2329
-                if (isset($this->_req_data['txn_payments']['send_notifications'])
2330
-                    && filter_var(
2331
-                        $this->_req_data['txn_payments']['send_notifications'],
2332
-                        FILTER_VALIDATE_BOOLEAN
2333
-                    )
2334
-                ) {
2335
-                    $this->_process_payment_notification($payment);
2336
-                }
2337
-                break;
2338
-            // registration notifications
2339
-            case false:
2340
-                if (isset($this->_req_data['txn_reg_status_change']['send_notifications'])
2341
-                    && filter_var(
2342
-                        $this->_req_data['txn_reg_status_change']['send_notifications'],
2343
-                        FILTER_VALIDATE_BOOLEAN
2344
-                    )
2345
-                ) {
2346
-                    add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true');
2347
-                }
2348
-                break;
2349
-        }
2350
-    }
2351
-
2352
-
2353
-    /**
2354
-     * _send_payment_reminder
2355
-     *    generates HTML for the View Transaction Details Admin page
2356
-     *
2357
-     * @access protected
2358
-     * @return void
2359
-     * @throws EE_Error
2360
-     * @throws InvalidArgumentException
2361
-     * @throws InvalidDataTypeException
2362
-     * @throws InvalidInterfaceException
2363
-     */
2364
-    protected function _send_payment_reminder()
2365
-    {
2366
-        $TXN_ID = ! empty($this->_req_data['TXN_ID']) ? absint($this->_req_data['TXN_ID']) : false;
2367
-        $transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID);
2368
-        $query_args = isset($this->_req_data['redirect_to']) ? array(
2369
-            'action' => $this->_req_data['redirect_to'],
2370
-            'TXN_ID' => $this->_req_data['TXN_ID'],
2371
-        ) : array();
2372
-        do_action(
2373
-            'AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder',
2374
-            $transaction
2375
-        );
2376
-        $this->_redirect_after_action(
2377
-            false,
2378
-            esc_html__('payment reminder', 'event_espresso'),
2379
-            esc_html__('sent', 'event_espresso'),
2380
-            $query_args,
2381
-            true
2382
-        );
2383
-    }
2384
-
2385
-
2386
-    /**
2387
-     *  get_transactions
2388
-     *    get transactions for given parameters (used by list table)
2389
-     *
2390
-     * @param  int     $perpage how many transactions displayed per page
2391
-     * @param  boolean $count   return the count or objects
2392
-     * @param string   $view
2393
-     * @return mixed int = count || array of transaction objects
2394
-     * @throws EE_Error
2395
-     * @throws InvalidArgumentException
2396
-     * @throws InvalidDataTypeException
2397
-     * @throws InvalidInterfaceException
2398
-     */
2399
-    public function get_transactions($perpage, $count = false, $view = '')
2400
-    {
2401
-
2402
-        $TXN = EEM_Transaction::instance();
2403
-
2404
-        $start_date = isset($this->_req_data['txn-filter-start-date'])
2405
-            ? wp_strip_all_tags($this->_req_data['txn-filter-start-date'])
2406
-            : date(
2407
-                'm/d/Y',
2408
-                strtotime('-10 year')
2409
-            );
2410
-        $end_date = isset($this->_req_data['txn-filter-end-date'])
2411
-            ? wp_strip_all_tags($this->_req_data['txn-filter-end-date'])
2412
-            : date('m/d/Y');
2413
-
2414
-        // make sure our timestamps start and end right at the boundaries for each day
2415
-        $start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00';
2416
-        $end_date = date('Y-m-d', strtotime($end_date)) . ' 23:59:59';
2417
-
2418
-
2419
-        // convert to timestamps
2420
-        $start_date = strtotime($start_date);
2421
-        $end_date = strtotime($end_date);
2422
-
2423
-        // makes sure start date is the lowest value and vice versa
2424
-        $start_date = min($start_date, $end_date);
2425
-        $end_date = max($start_date, $end_date);
2426
-
2427
-        // convert to correct format for query
2428
-        $start_date = EEM_Transaction::instance()->convert_datetime_for_query(
2429
-            'TXN_timestamp',
2430
-            date('Y-m-d H:i:s', $start_date),
2431
-            'Y-m-d H:i:s'
2432
-        );
2433
-        $end_date = EEM_Transaction::instance()->convert_datetime_for_query(
2434
-            'TXN_timestamp',
2435
-            date('Y-m-d H:i:s', $end_date),
2436
-            'Y-m-d H:i:s'
2437
-        );
2438
-
2439
-
2440
-        // set orderby
2441
-        $this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : '';
2442
-
2443
-        switch ($this->_req_data['orderby']) {
2444
-            case 'TXN_ID':
2445
-                $orderby = 'TXN_ID';
2446
-                break;
2447
-            case 'ATT_fname':
2448
-                $orderby = 'Registration.Attendee.ATT_fname';
2449
-                break;
2450
-            case 'event_name':
2451
-                $orderby = 'Registration.Event.EVT_name';
2452
-                break;
2453
-            default: // 'TXN_timestamp'
2454
-                $orderby = 'TXN_timestamp';
2455
-        }
2456
-
2457
-        $sort = ! empty($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC';
2458
-        $current_page = ! empty($this->_req_data['paged']) ? $this->_req_data['paged'] : 1;
2459
-        $per_page = ! empty($perpage) ? $perpage : 10;
2460
-        $per_page = ! empty($this->_req_data['perpage']) ? $this->_req_data['perpage'] : $per_page;
2461
-
2462
-        $offset = ($current_page - 1) * $per_page;
2463
-        $limit = array($offset, $per_page);
2464
-
2465
-        $_where = array(
2466
-            'TXN_timestamp'          => array('BETWEEN', array($start_date, $end_date)),
2467
-            'Registration.REG_count' => 1,
2468
-        );
2469
-
2470
-        if (isset($this->_req_data['EVT_ID'])) {
2471
-            $_where['Registration.EVT_ID'] = $this->_req_data['EVT_ID'];
2472
-        }
2473
-
2474
-        if (isset($this->_req_data['s'])) {
2475
-            $search_string = '%' . $this->_req_data['s'] . '%';
2476
-            $_where['OR'] = array(
2477
-                'Registration.Event.EVT_name'         => array('LIKE', $search_string),
2478
-                'Registration.Event.EVT_desc'         => array('LIKE', $search_string),
2479
-                'Registration.Event.EVT_short_desc'   => array('LIKE', $search_string),
2480
-                'Registration.Attendee.ATT_full_name' => array('LIKE', $search_string),
2481
-                'Registration.Attendee.ATT_fname'     => array('LIKE', $search_string),
2482
-                'Registration.Attendee.ATT_lname'     => array('LIKE', $search_string),
2483
-                'Registration.Attendee.ATT_short_bio' => array('LIKE', $search_string),
2484
-                'Registration.Attendee.ATT_email'     => array('LIKE', $search_string),
2485
-                'Registration.Attendee.ATT_address'   => array('LIKE', $search_string),
2486
-                'Registration.Attendee.ATT_address2'  => array('LIKE', $search_string),
2487
-                'Registration.Attendee.ATT_city'      => array('LIKE', $search_string),
2488
-                'Registration.REG_final_price'        => array('LIKE', $search_string),
2489
-                'Registration.REG_code'               => array('LIKE', $search_string),
2490
-                'Registration.REG_count'              => array('LIKE', $search_string),
2491
-                'Registration.REG_group_size'         => array('LIKE', $search_string),
2492
-                'Registration.Ticket.TKT_name'        => array('LIKE', $search_string),
2493
-                'Registration.Ticket.TKT_description' => array('LIKE', $search_string),
2494
-                'Payment.PAY_source'                  => array('LIKE', $search_string),
2495
-                'Payment.Payment_Method.PMD_name'     => array('LIKE', $search_string),
2496
-                'TXN_session_data'                    => array('LIKE', $search_string),
2497
-                'Payment.PAY_txn_id_chq_nmbr'         => array('LIKE', $search_string),
2498
-            );
2499
-        }
2500
-
2501
-        // failed transactions
2502
-        $failed = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'failed' && ! $count)
2503
-                  || ($count && $view === 'failed');
2504
-        $abandoned = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'abandoned' && ! $count)
2505
-                     || ($count && $view === 'abandoned');
2506
-        $incomplete = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'incomplete' && ! $count)
2507
-                      || ($count && $view === 'incomplete');
2508
-
2509
-        if ($failed) {
2510
-            $_where['STS_ID'] = EEM_Transaction::failed_status_code;
2511
-        } elseif ($abandoned) {
2512
-            $_where['STS_ID'] = EEM_Transaction::abandoned_status_code;
2513
-        } elseif ($incomplete) {
2514
-            $_where['STS_ID'] = EEM_Transaction::incomplete_status_code;
2515
-        } else {
2516
-            $_where['STS_ID'] = array('!=', EEM_Transaction::failed_status_code);
2517
-            $_where['STS_ID*'] = array('!=', EEM_Transaction::abandoned_status_code);
2518
-        }
2519
-
2520
-        $query_params = apply_filters(
2521
-            'FHEE__Transactions_Admin_Page___get_transactions_query_params',
2522
-            array(
2523
-                $_where,
2524
-                'order_by'                 => array($orderby => $sort),
2525
-                'limit'                    => $limit,
2526
-                'default_where_conditions' => EEM_Base::default_where_conditions_this_only,
2527
-            ),
2528
-            $this->_req_data,
2529
-            $view,
2530
-            $count
2531
-        );
2532
-
2533
-        $transactions = $count
2534
-            ? $TXN->count(array($query_params[0]), 'TXN_ID', true)
2535
-            : $TXN->get_all($query_params);
2536
-
2537
-        return $transactions;
2538
-    }
2539
-
2540
-
2541
-    /**
2542
-     * @since 4.9.79.p
2543
-     * @throws EE_Error
2544
-     * @throws InvalidArgumentException
2545
-     * @throws InvalidDataTypeException
2546
-     * @throws InvalidInterfaceException
2547
-     * @throws ReflectionException
2548
-     * @throws RuntimeException
2549
-     */
2550
-    public function recalculateLineItems()
2551
-    {
2552
-        $TXN_ID = ! empty($this->_req_data['TXN_ID']) ? absint($this->_req_data['TXN_ID']) : false;
2553
-        /** @var EE_Transaction $transaction */
2554
-        $transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID);
2555
-        $total_line_item = $transaction->total_line_item(false);
2556
-        $success = false;
2557
-        if ($total_line_item instanceof EE_Line_Item) {
2558
-            EEH_Line_Item::resetIsTaxableForTickets($total_line_item);
2559
-            $success = EEH_Line_Item::apply_taxes($total_line_item, true);
2560
-        }
2561
-        $this->_redirect_after_action(
2562
-            (bool) $success,
2563
-            esc_html__('Transaction taxes and totals', 'event_espresso'),
2564
-            esc_html__('recalculated', 'event_espresso'),
2565
-            isset($this->_req_data['redirect_to'])
2566
-                ? array(
2567
-                'action' => $this->_req_data['redirect_to'],
2568
-                'TXN_ID' => $this->_req_data['TXN_ID'],
2569
-            )
2570
-                : array(),
2571
-            true
2572
-        );
2573
-    }
16
+	/**
17
+	 * @var EE_Transaction
18
+	 */
19
+	private $_transaction;
20
+
21
+	/**
22
+	 * @var EE_Session
23
+	 */
24
+	private $_session;
25
+
26
+	/**
27
+	 * @var array $_txn_status
28
+	 */
29
+	private static $_txn_status;
30
+
31
+	/**
32
+	 * @var array $_pay_status
33
+	 */
34
+	private static $_pay_status;
35
+
36
+	/**
37
+	 * @var array $_existing_reg_payment_REG_IDs
38
+	 */
39
+	protected $_existing_reg_payment_REG_IDs;
40
+
41
+
42
+	/**
43
+	 *    _init_page_props
44
+	 *
45
+	 * @return void
46
+	 */
47
+	protected function _init_page_props()
48
+	{
49
+		$this->page_slug = TXN_PG_SLUG;
50
+		$this->page_label = esc_html__('Transactions', 'event_espresso');
51
+		$this->_admin_base_url = TXN_ADMIN_URL;
52
+		$this->_admin_base_path = TXN_ADMIN;
53
+	}
54
+
55
+
56
+	/**
57
+	 *    _ajax_hooks
58
+	 *
59
+	 * @return void
60
+	 */
61
+	protected function _ajax_hooks()
62
+	{
63
+		add_action('wp_ajax_espresso_apply_payment', array($this, 'apply_payments_or_refunds'));
64
+		add_action('wp_ajax_espresso_apply_refund', array($this, 'apply_payments_or_refunds'));
65
+		add_action('wp_ajax_espresso_delete_payment', array($this, 'delete_payment'));
66
+	}
67
+
68
+
69
+	/**
70
+	 *    _define_page_props
71
+	 *
72
+	 * @return void
73
+	 */
74
+	protected function _define_page_props()
75
+	{
76
+		$this->_admin_page_title = $this->page_label;
77
+		$this->_labels = array(
78
+			'buttons' => array(
79
+				'add'    => esc_html__('Add New Transaction', 'event_espresso'),
80
+				'edit'   => esc_html__('Edit Transaction', 'event_espresso'),
81
+				'delete' => esc_html__('Delete Transaction', 'event_espresso'),
82
+			),
83
+		);
84
+	}
85
+
86
+
87
+	/**
88
+	 *        grab url requests and route them
89
+	 *
90
+	 * @access private
91
+	 * @return void
92
+	 * @throws EE_Error
93
+	 * @throws InvalidArgumentException
94
+	 * @throws InvalidDataTypeException
95
+	 * @throws InvalidInterfaceException
96
+	 */
97
+	public function _set_page_routes()
98
+	{
99
+
100
+		$this->_set_transaction_status_array();
101
+
102
+		$txn_id = ! empty($this->_req_data['TXN_ID'])
103
+				  && ! is_array($this->_req_data['TXN_ID'])
104
+			? $this->_req_data['TXN_ID']
105
+			: 0;
106
+
107
+		$this->_page_routes = array(
108
+
109
+			'default' => array(
110
+				'func'       => '_transactions_overview_list_table',
111
+				'capability' => 'ee_read_transactions',
112
+			),
113
+
114
+			'view_transaction' => array(
115
+				'func'       => '_transaction_details',
116
+				'capability' => 'ee_read_transaction',
117
+				'obj_id'     => $txn_id,
118
+			),
119
+
120
+			'send_payment_reminder' => array(
121
+				'func'       => '_send_payment_reminder',
122
+				'noheader'   => true,
123
+				'capability' => 'ee_send_message',
124
+			),
125
+
126
+			'espresso_apply_payment' => array(
127
+				'func'       => 'apply_payments_or_refunds',
128
+				'noheader'   => true,
129
+				'capability' => 'ee_edit_payments',
130
+			),
131
+
132
+			'espresso_apply_refund' => array(
133
+				'func'       => 'apply_payments_or_refunds',
134
+				'noheader'   => true,
135
+				'capability' => 'ee_edit_payments',
136
+			),
137
+
138
+			'espresso_delete_payment' => array(
139
+				'func'       => 'delete_payment',
140
+				'noheader'   => true,
141
+				'capability' => 'ee_delete_payments',
142
+			),
143
+
144
+			'espresso_recalculate_line_items' => array(
145
+				'func'       => 'recalculateLineItems',
146
+				'noheader'   => true,
147
+				'capability' => 'ee_edit_payments',
148
+			),
149
+
150
+		);
151
+	}
152
+
153
+
154
+	protected function _set_page_config()
155
+	{
156
+		$this->_page_config = array(
157
+			'default'          => array(
158
+				'nav'           => array(
159
+					'label' => esc_html__('Overview', 'event_espresso'),
160
+					'order' => 10,
161
+				),
162
+				'list_table'    => 'EE_Admin_Transactions_List_Table',
163
+				'help_tabs'     => array(
164
+					'transactions_overview_help_tab'                       => array(
165
+						'title'    => esc_html__('Transactions Overview', 'event_espresso'),
166
+						'filename' => 'transactions_overview',
167
+					),
168
+					'transactions_overview_table_column_headings_help_tab' => array(
169
+						'title'    => esc_html__('Transactions Table Column Headings', 'event_espresso'),
170
+						'filename' => 'transactions_overview_table_column_headings',
171
+					),
172
+					'transactions_overview_views_filters_help_tab'         => array(
173
+						'title'    => esc_html__('Transaction Views & Filters & Search', 'event_espresso'),
174
+						'filename' => 'transactions_overview_views_filters_search',
175
+					),
176
+				),
177
+				'help_tour'     => array('Transactions_Overview_Help_Tour'),
178
+				/**
179
+				 * commented out because currently we are not displaying tips for transaction list table status but this
180
+				 * may change in a later iteration so want to keep the code for then.
181
+				 */
182
+				// 'qtips' => array( 'Transactions_List_Table_Tips' ),
183
+				'require_nonce' => false,
184
+			),
185
+			'view_transaction' => array(
186
+				'nav'       => array(
187
+					'label'      => esc_html__('View Transaction', 'event_espresso'),
188
+					'order'      => 5,
189
+					'url'        => isset($this->_req_data['TXN_ID'])
190
+						? add_query_arg(array('TXN_ID' => $this->_req_data['TXN_ID']), $this->_current_page_view_url)
191
+						: $this->_admin_base_url,
192
+					'persistent' => false,
193
+				),
194
+				'help_tabs' => array(
195
+					'transactions_view_transaction_help_tab'                                              => array(
196
+						'title'    => esc_html__('View Transaction', 'event_espresso'),
197
+						'filename' => 'transactions_view_transaction',
198
+					),
199
+					'transactions_view_transaction_transaction_details_table_help_tab'                    => array(
200
+						'title'    => esc_html__('Transaction Details Table', 'event_espresso'),
201
+						'filename' => 'transactions_view_transaction_transaction_details_table',
202
+					),
203
+					'transactions_view_transaction_attendees_registered_help_tab'                         => array(
204
+						'title'    => esc_html__('Attendees Registered', 'event_espresso'),
205
+						'filename' => 'transactions_view_transaction_attendees_registered',
206
+					),
207
+					'transactions_view_transaction_views_primary_registrant_billing_information_help_tab' => array(
208
+						'title'    => esc_html__('Primary Registrant & Billing Information', 'event_espresso'),
209
+						'filename' => 'transactions_view_transaction_primary_registrant_billing_information',
210
+					),
211
+				),
212
+				'qtips'     => array('Transaction_Details_Tips'),
213
+				'help_tour' => array('Transaction_Details_Help_Tour'),
214
+				'metaboxes' => array('_transaction_details_metaboxes'),
215
+
216
+				'require_nonce' => false,
217
+			),
218
+		);
219
+	}
220
+
221
+
222
+	/**
223
+	 * The below methods aren't used by this class currently
224
+	 */
225
+	protected function _add_screen_options()
226
+	{
227
+		// noop
228
+	}
229
+
230
+
231
+	protected function _add_feature_pointers()
232
+	{
233
+		// noop
234
+	}
235
+
236
+
237
+	public function admin_init()
238
+	{
239
+		// IF a registration was JUST added via the admin...
240
+		if (isset(
241
+			$this->_req_data['redirect_from'],
242
+			$this->_req_data['EVT_ID'],
243
+			$this->_req_data['event_name']
244
+		)) {
245
+			// then set a cookie so that we can block any attempts to use
246
+			// the back button as a way to enter another registration.
247
+			setcookie(
248
+				'ee_registration_added',
249
+				$this->_req_data['EVT_ID'],
250
+				time() + WEEK_IN_SECONDS,
251
+				'/'
252
+			);
253
+			// and update the global
254
+			$_COOKIE['ee_registration_added'] = $this->_req_data['EVT_ID'];
255
+		}
256
+		EE_Registry::$i18n_js_strings['invalid_server_response'] = esc_html__(
257
+			'An error occurred! Your request may have been processed, but a valid response from the server was not received. Please refresh the page and try again.',
258
+			'event_espresso'
259
+		);
260
+		EE_Registry::$i18n_js_strings['error_occurred'] = esc_html__(
261
+			'An error occurred! Please refresh the page and try again.',
262
+			'event_espresso'
263
+		);
264
+		EE_Registry::$i18n_js_strings['txn_status_array'] = self::$_txn_status;
265
+		EE_Registry::$i18n_js_strings['pay_status_array'] = self::$_pay_status;
266
+		EE_Registry::$i18n_js_strings['payments_total'] = esc_html__('Payments Total', 'event_espresso');
267
+		EE_Registry::$i18n_js_strings['transaction_overpaid'] = esc_html__(
268
+			'This transaction has been overpaid ! Payments Total',
269
+			'event_espresso'
270
+		);
271
+	}
272
+
273
+
274
+	public function admin_notices()
275
+	{
276
+		// noop
277
+	}
278
+
279
+
280
+	public function admin_footer_scripts()
281
+	{
282
+		// noop
283
+	}
284
+
285
+
286
+	/**
287
+	 * _set_transaction_status_array
288
+	 * sets list of transaction statuses
289
+	 *
290
+	 * @access private
291
+	 * @return void
292
+	 * @throws EE_Error
293
+	 * @throws InvalidArgumentException
294
+	 * @throws InvalidDataTypeException
295
+	 * @throws InvalidInterfaceException
296
+	 */
297
+	private function _set_transaction_status_array()
298
+	{
299
+		self::$_txn_status = EEM_Transaction::instance()->status_array(true);
300
+	}
301
+
302
+
303
+	/**
304
+	 * get_transaction_status_array
305
+	 * return the transaction status array for wp_list_table
306
+	 *
307
+	 * @access public
308
+	 * @return array
309
+	 */
310
+	public function get_transaction_status_array()
311
+	{
312
+		return self::$_txn_status;
313
+	}
314
+
315
+
316
+	/**
317
+	 *    get list of payment statuses
318
+	 *
319
+	 * @access private
320
+	 * @return void
321
+	 * @throws EE_Error
322
+	 * @throws InvalidArgumentException
323
+	 * @throws InvalidDataTypeException
324
+	 * @throws InvalidInterfaceException
325
+	 */
326
+	private function _get_payment_status_array()
327
+	{
328
+		self::$_pay_status = EEM_Payment::instance()->status_array(true);
329
+		$this->_template_args['payment_status'] = self::$_pay_status;
330
+	}
331
+
332
+
333
+	/**
334
+	 *    _add_screen_options_default
335
+	 *
336
+	 * @access protected
337
+	 * @return void
338
+	 * @throws InvalidArgumentException
339
+	 * @throws InvalidDataTypeException
340
+	 * @throws InvalidInterfaceException
341
+	 */
342
+	protected function _add_screen_options_default()
343
+	{
344
+		$this->_per_page_screen_option();
345
+	}
346
+
347
+
348
+	/**
349
+	 * load_scripts_styles
350
+	 *
351
+	 * @access public
352
+	 * @return void
353
+	 */
354
+	public function load_scripts_styles()
355
+	{
356
+		// enqueue style
357
+		wp_register_style(
358
+			'espresso_txn',
359
+			TXN_ASSETS_URL . 'espresso_transactions_admin.css',
360
+			array(),
361
+			EVENT_ESPRESSO_VERSION
362
+		);
363
+		wp_enqueue_style('espresso_txn');
364
+		// scripts
365
+		wp_register_script(
366
+			'espresso_txn',
367
+			TXN_ASSETS_URL . 'espresso_transactions_admin.js',
368
+			array(
369
+				'ee_admin_js',
370
+				'ee-datepicker',
371
+				'jquery-ui-datepicker',
372
+				'jquery-ui-draggable',
373
+				'ee-dialog',
374
+				'ee-accounting',
375
+				'ee-serialize-full-array',
376
+			),
377
+			EVENT_ESPRESSO_VERSION,
378
+			true
379
+		);
380
+		wp_enqueue_script('espresso_txn');
381
+	}
382
+
383
+
384
+	/**
385
+	 *    load_scripts_styles_view_transaction
386
+	 *
387
+	 * @access public
388
+	 * @return void
389
+	 */
390
+	public function load_scripts_styles_view_transaction()
391
+	{
392
+		// styles
393
+		wp_enqueue_style('espresso-ui-theme');
394
+	}
395
+
396
+
397
+	/**
398
+	 *    load_scripts_styles_default
399
+	 *
400
+	 * @access public
401
+	 * @return void
402
+	 */
403
+	public function load_scripts_styles_default()
404
+	{
405
+		// styles
406
+		wp_enqueue_style('espresso-ui-theme');
407
+	}
408
+
409
+
410
+	/**
411
+	 *    _set_list_table_views_default
412
+	 *
413
+	 * @access protected
414
+	 * @return void
415
+	 */
416
+	protected function _set_list_table_views_default()
417
+	{
418
+		$this->_views = array(
419
+			'all'        => array(
420
+				'slug'  => 'all',
421
+				'label' => esc_html__('View All Transactions', 'event_espresso'),
422
+				'count' => 0,
423
+			),
424
+			'abandoned'  => array(
425
+				'slug'  => 'abandoned',
426
+				'label' => esc_html__('Abandoned Transactions', 'event_espresso'),
427
+				'count' => 0,
428
+			),
429
+			'incomplete' => array(
430
+				'slug'  => 'incomplete',
431
+				'label' => esc_html__('Incomplete Transactions', 'event_espresso'),
432
+				'count' => 0,
433
+			),
434
+		);
435
+		if (/**
436
+		 * Filters whether a link to the "Failed Transactions" list table
437
+		 * appears on the Transactions Admin Page list table.
438
+		 * List display can be turned back on via the following:
439
+		 * add_filter(
440
+		 *     'FHEE__Transactions_Admin_Page___set_list_table_views_default__display_failed_txns_list',
441
+		 *     '__return_true'
442
+		 * );
443
+		 *
444
+		 * @since 4.9.70.p
445
+		 * @param boolean                 $display_failed_txns_list
446
+		 * @param Transactions_Admin_Page $this
447
+		 */
448
+		apply_filters(
449
+			'FHEE__Transactions_Admin_Page___set_list_table_views_default__display_failed_txns_list',
450
+			false,
451
+			$this
452
+		)
453
+		) {
454
+			$this->_views['failed'] = array(
455
+				'slug'  => 'failed',
456
+				'label' => esc_html__('Failed Transactions', 'event_espresso'),
457
+				'count' => 0,
458
+			);
459
+		}
460
+	}
461
+
462
+
463
+	/**
464
+	 * _set_transaction_object
465
+	 * This sets the _transaction property for the transaction details screen
466
+	 *
467
+	 * @access private
468
+	 * @return void
469
+	 * @throws EE_Error
470
+	 * @throws InvalidArgumentException
471
+	 * @throws RuntimeException
472
+	 * @throws InvalidDataTypeException
473
+	 * @throws InvalidInterfaceException
474
+	 * @throws ReflectionException
475
+	 */
476
+	private function _set_transaction_object()
477
+	{
478
+		if ($this->_transaction instanceof EE_Transaction) {
479
+			return;
480
+		} //get out we've already set the object
481
+
482
+		$TXN_ID = ! empty($this->_req_data['TXN_ID'])
483
+			? absint($this->_req_data['TXN_ID'])
484
+			: false;
485
+
486
+		// get transaction object
487
+		$this->_transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID);
488
+		$this->_session = $this->_transaction instanceof EE_Transaction
489
+			? $this->_transaction->session_data()
490
+			: null;
491
+		if ($this->_transaction instanceof EE_Transaction) {
492
+			$this->_transaction->verify_abandoned_transaction_status();
493
+		}
494
+
495
+		if (! $this->_transaction instanceof EE_Transaction) {
496
+			$error_msg = sprintf(
497
+				esc_html__(
498
+					'An error occurred and the details for the transaction with the ID # %d could not be retrieved.',
499
+					'event_espresso'
500
+				),
501
+				$TXN_ID
502
+			);
503
+			EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__);
504
+		}
505
+	}
506
+
507
+
508
+	/**
509
+	 *    _transaction_legend_items
510
+	 *
511
+	 * @access protected
512
+	 * @return array
513
+	 * @throws EE_Error
514
+	 * @throws InvalidArgumentException
515
+	 * @throws ReflectionException
516
+	 * @throws InvalidDataTypeException
517
+	 * @throws InvalidInterfaceException
518
+	 */
519
+	protected function _transaction_legend_items()
520
+	{
521
+		EE_Registry::instance()->load_helper('MSG_Template');
522
+		$items = array();
523
+
524
+		if (EE_Registry::instance()->CAP->current_user_can(
525
+			'ee_read_global_messages',
526
+			'view_filtered_messages'
527
+		)) {
528
+			$related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for');
529
+			if (is_array($related_for_icon)
530
+				&& isset($related_for_icon['css_class'], $related_for_icon['label'])
531
+			) {
532
+				$items['view_related_messages'] = array(
533
+					'class' => $related_for_icon['css_class'],
534
+					'desc'  => $related_for_icon['label'],
535
+				);
536
+			}
537
+		}
538
+
539
+		$items = apply_filters(
540
+			'FHEE__Transactions_Admin_Page___transaction_legend_items__items',
541
+			array_merge(
542
+				$items,
543
+				array(
544
+					'view_details'          => array(
545
+						'class' => 'dashicons dashicons-cart',
546
+						'desc'  => esc_html__('View Transaction Details', 'event_espresso'),
547
+					),
548
+					'view_invoice'          => array(
549
+						'class' => 'dashicons dashicons-media-spreadsheet',
550
+						'desc'  => esc_html__('View Transaction Invoice', 'event_espresso'),
551
+					),
552
+					'view_receipt'          => array(
553
+						'class' => 'dashicons dashicons-media-default',
554
+						'desc'  => esc_html__('View Transaction Receipt', 'event_espresso'),
555
+					),
556
+					'view_registration'     => array(
557
+						'class' => 'dashicons dashicons-clipboard',
558
+						'desc'  => esc_html__('View Registration Details', 'event_espresso'),
559
+					),
560
+					'payment_overview_link' => array(
561
+						'class' => 'dashicons dashicons-money',
562
+						'desc'  => esc_html__('Make Payment on Frontend', 'event_espresso'),
563
+					),
564
+				)
565
+			)
566
+		);
567
+
568
+		if (EEH_MSG_Template::is_mt_active('payment_reminder')
569
+			&& EE_Registry::instance()->CAP->current_user_can(
570
+				'ee_send_message',
571
+				'espresso_transactions_send_payment_reminder'
572
+			)
573
+		) {
574
+			$items['send_payment_reminder'] = array(
575
+				'class' => 'dashicons dashicons-email-alt',
576
+				'desc'  => esc_html__('Send Payment Reminder', 'event_espresso'),
577
+			);
578
+		} else {
579
+			$items['blank*'] = array(
580
+				'class' => '',
581
+				'desc'  => '',
582
+			);
583
+		}
584
+		$more_items = apply_filters(
585
+			'FHEE__Transactions_Admin_Page___transaction_legend_items__more_items',
586
+			array(
587
+				'overpaid'   => array(
588
+					'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::overpaid_status_code,
589
+					'desc'  => EEH_Template::pretty_status(
590
+						EEM_Transaction::overpaid_status_code,
591
+						false,
592
+						'sentence'
593
+					),
594
+				),
595
+				'complete'   => array(
596
+					'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::complete_status_code,
597
+					'desc'  => EEH_Template::pretty_status(
598
+						EEM_Transaction::complete_status_code,
599
+						false,
600
+						'sentence'
601
+					),
602
+				),
603
+				'incomplete' => array(
604
+					'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::incomplete_status_code,
605
+					'desc'  => EEH_Template::pretty_status(
606
+						EEM_Transaction::incomplete_status_code,
607
+						false,
608
+						'sentence'
609
+					),
610
+				),
611
+				'abandoned'  => array(
612
+					'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::abandoned_status_code,
613
+					'desc'  => EEH_Template::pretty_status(
614
+						EEM_Transaction::abandoned_status_code,
615
+						false,
616
+						'sentence'
617
+					),
618
+				),
619
+				'failed'     => array(
620
+					'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::failed_status_code,
621
+					'desc'  => EEH_Template::pretty_status(
622
+						EEM_Transaction::failed_status_code,
623
+						false,
624
+						'sentence'
625
+					),
626
+				),
627
+			)
628
+		);
629
+
630
+		return array_merge($items, $more_items);
631
+	}
632
+
633
+
634
+	/**
635
+	 *    _transactions_overview_list_table
636
+	 *
637
+	 * @access protected
638
+	 * @return void
639
+	 * @throws DomainException
640
+	 * @throws EE_Error
641
+	 * @throws InvalidArgumentException
642
+	 * @throws InvalidDataTypeException
643
+	 * @throws InvalidInterfaceException
644
+	 * @throws ReflectionException
645
+	 */
646
+	protected function _transactions_overview_list_table()
647
+	{
648
+		$this->_admin_page_title = esc_html__('Transactions', 'event_espresso');
649
+		$event = isset($this->_req_data['EVT_ID'])
650
+			? EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID'])
651
+			: null;
652
+		$this->_template_args['admin_page_header'] = $event instanceof EE_Event
653
+			? sprintf(
654
+				esc_html__(
655
+					'%sViewing Transactions for the Event: %s%s',
656
+					'event_espresso'
657
+				),
658
+				'<h3>',
659
+				'<a href="'
660
+				. EE_Admin_Page::add_query_args_and_nonce(
661
+					array('action' => 'edit', 'post' => $event->ID()),
662
+					EVENTS_ADMIN_URL
663
+				)
664
+				. '" title="'
665
+				. esc_attr__(
666
+					'Click to Edit event',
667
+					'event_espresso'
668
+				)
669
+				. '">' . $event->name() . '</a>',
670
+				'</h3>'
671
+			)
672
+			: '';
673
+		$this->_template_args['after_list_table'] = $this->_display_legend($this->_transaction_legend_items());
674
+		$this->display_admin_list_table_page_with_no_sidebar();
675
+	}
676
+
677
+
678
+	/**
679
+	 *    _transaction_details
680
+	 * generates HTML for the View Transaction Details Admin page
681
+	 *
682
+	 * @access protected
683
+	 * @return void
684
+	 * @throws DomainException
685
+	 * @throws EE_Error
686
+	 * @throws InvalidArgumentException
687
+	 * @throws InvalidDataTypeException
688
+	 * @throws InvalidInterfaceException
689
+	 * @throws RuntimeException
690
+	 * @throws ReflectionException
691
+	 */
692
+	protected function _transaction_details()
693
+	{
694
+		do_action('AHEE__Transactions_Admin_Page__transaction_details__start', $this->_transaction);
695
+
696
+		$this->_set_transaction_status_array();
697
+
698
+		$this->_template_args = array();
699
+		$this->_template_args['transactions_page'] = $this->_wp_page_slug;
700
+
701
+		$this->_set_transaction_object();
702
+
703
+		if (! $this->_transaction instanceof EE_Transaction) {
704
+			return;
705
+		}
706
+		$primary_registration = $this->_transaction->primary_registration();
707
+		$attendee = $primary_registration instanceof EE_Registration
708
+			? $primary_registration->attendee()
709
+			: null;
710
+
711
+		$this->_template_args['txn_nmbr']['value'] = $this->_transaction->ID();
712
+		$this->_template_args['txn_nmbr']['label'] = esc_html__('Transaction Number', 'event_espresso');
713
+
714
+		$this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp');
715
+		$this->_template_args['txn_datetime']['label'] = esc_html__('Date', 'event_espresso');
716
+
717
+		$this->_template_args['txn_status']['value'] = self::$_txn_status[ $this->_transaction->status_ID() ];
718
+		$this->_template_args['txn_status']['label'] = esc_html__('Transaction Status', 'event_espresso');
719
+		$this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->status_ID();
720
+
721
+		$this->_template_args['grand_total'] = $this->_transaction->total();
722
+		$this->_template_args['total_paid'] = $this->_transaction->paid();
723
+
724
+		$amount_due = $this->_transaction->total() - $this->_transaction->paid();
725
+		$this->_template_args['amount_due'] = EEH_Template::format_currency(
726
+			$amount_due,
727
+			true
728
+		);
729
+		if (EE_Registry::instance()->CFG->currency->sign_b4) {
730
+			$this->_template_args['amount_due'] = EE_Registry::instance()->CFG->currency->sign
731
+												  . $this->_template_args['amount_due'];
732
+		} else {
733
+			$this->_template_args['amount_due'] .= EE_Registry::instance()->CFG->currency->sign;
734
+		}
735
+		$this->_template_args['amount_due_class'] = '';
736
+
737
+		if ($this->_transaction->paid() === $this->_transaction->total()) {
738
+			// paid in full
739
+			$this->_template_args['amount_due'] = false;
740
+		} elseif ($this->_transaction->paid() > $this->_transaction->total()) {
741
+			// overpaid
742
+			$this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn';
743
+		} elseif ($this->_transaction->total() > (float) 0) {
744
+			if ($this->_transaction->paid() > (float) 0) {
745
+				// monies owing
746
+				$this->_template_args['amount_due_class'] = 'txn-overview-part-payment-spn';
747
+			} elseif ($this->_transaction->paid() === (float) 0) {
748
+				// no payments made yet
749
+				$this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn';
750
+			}
751
+		} elseif ($this->_transaction->total() === (float) 0) {
752
+			// free event
753
+			$this->_template_args['amount_due'] = false;
754
+		}
755
+
756
+		$payment_method = $this->_transaction->payment_method();
757
+
758
+		$this->_template_args['method_of_payment_name'] = $payment_method instanceof EE_Payment_Method
759
+			? $payment_method->admin_name()
760
+			: esc_html__('Unknown', 'event_espresso');
761
+
762
+		$this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign;
763
+		// link back to overview
764
+		$this->_template_args['txn_overview_url'] = ! empty($_SERVER['HTTP_REFERER'])
765
+			? $_SERVER['HTTP_REFERER']
766
+			: TXN_ADMIN_URL;
767
+
768
+
769
+		// next link
770
+		$next_txn = $this->_transaction->next(
771
+			null,
772
+			array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))),
773
+			'TXN_ID'
774
+		);
775
+		$this->_template_args['next_transaction'] = $next_txn
776
+			? $this->_next_link(
777
+				EE_Admin_Page::add_query_args_and_nonce(
778
+					array('action' => 'view_transaction', 'TXN_ID' => $next_txn['TXN_ID']),
779
+					TXN_ADMIN_URL
780
+				),
781
+				'dashicons dashicons-arrow-right ee-icon-size-22'
782
+			)
783
+			: '';
784
+		// previous link
785
+		$previous_txn = $this->_transaction->previous(
786
+			null,
787
+			array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))),
788
+			'TXN_ID'
789
+		);
790
+		$this->_template_args['previous_transaction'] = $previous_txn
791
+			? $this->_previous_link(
792
+				EE_Admin_Page::add_query_args_and_nonce(
793
+					array('action' => 'view_transaction', 'TXN_ID' => $previous_txn['TXN_ID']),
794
+					TXN_ADMIN_URL
795
+				),
796
+				'dashicons dashicons-arrow-left ee-icon-size-22'
797
+			)
798
+			: '';
799
+
800
+		// were we just redirected here after adding a new registration ???
801
+		if (isset(
802
+			$this->_req_data['redirect_from'],
803
+			$this->_req_data['EVT_ID'],
804
+			$this->_req_data['event_name']
805
+		)) {
806
+			if (EE_Registry::instance()->CAP->current_user_can(
807
+				'ee_edit_registrations',
808
+				'espresso_registrations_new_registration',
809
+				$this->_req_data['EVT_ID']
810
+			)) {
811
+				$this->_admin_page_title .= '<a id="add-new-registration" class="add-new-h2 button-primary" href="';
812
+				$this->_admin_page_title .= EE_Admin_Page::add_query_args_and_nonce(
813
+					array(
814
+						'page'     => 'espresso_registrations',
815
+						'action'   => 'new_registration',
816
+						'return'   => 'default',
817
+						'TXN_ID'   => $this->_transaction->ID(),
818
+						'event_id' => $this->_req_data['EVT_ID'],
819
+					),
820
+					REG_ADMIN_URL
821
+				);
822
+				$this->_admin_page_title .= '">';
823
+
824
+				$this->_admin_page_title .= sprintf(
825
+					esc_html__('Add Another New Registration to Event: "%1$s" ?', 'event_espresso'),
826
+					htmlentities(urldecode($this->_req_data['event_name']), ENT_QUOTES, 'UTF-8')
827
+				);
828
+				$this->_admin_page_title .= '</a>';
829
+			}
830
+			EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__);
831
+		}
832
+		// grab messages at the last second
833
+		$this->_template_args['notices'] = EE_Error::get_notices();
834
+		// path to template
835
+		$template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php';
836
+		$this->_template_args['admin_page_header'] = EEH_Template::display_template(
837
+			$template_path,
838
+			$this->_template_args,
839
+			true
840
+		);
841
+
842
+		// the details template wrapper
843
+		$this->display_admin_page_with_sidebar();
844
+	}
845
+
846
+
847
+	/**
848
+	 *        _transaction_details_metaboxes
849
+	 *
850
+	 * @access protected
851
+	 * @return void
852
+	 * @throws EE_Error
853
+	 * @throws InvalidArgumentException
854
+	 * @throws InvalidDataTypeException
855
+	 * @throws InvalidInterfaceException
856
+	 * @throws RuntimeException
857
+	 * @throws ReflectionException
858
+	 */
859
+	protected function _transaction_details_metaboxes()
860
+	{
861
+
862
+		$this->_set_transaction_object();
863
+
864
+		if (! $this->_transaction instanceof EE_Transaction) {
865
+			return;
866
+		}
867
+		add_meta_box(
868
+			'edit-txn-details-mbox',
869
+			esc_html__('Transaction Details', 'event_espresso'),
870
+			array($this, 'txn_details_meta_box'),
871
+			$this->_wp_page_slug,
872
+			'normal',
873
+			'high'
874
+		);
875
+		add_meta_box(
876
+			'edit-txn-attendees-mbox',
877
+			esc_html__('Attendees Registered in this Transaction', 'event_espresso'),
878
+			array($this, 'txn_attendees_meta_box'),
879
+			$this->_wp_page_slug,
880
+			'normal',
881
+			'high',
882
+			array('TXN_ID' => $this->_transaction->ID())
883
+		);
884
+		add_meta_box(
885
+			'edit-txn-registrant-mbox',
886
+			esc_html__('Primary Contact', 'event_espresso'),
887
+			array($this, 'txn_registrant_side_meta_box'),
888
+			$this->_wp_page_slug,
889
+			'side',
890
+			'high'
891
+		);
892
+		add_meta_box(
893
+			'edit-txn-billing-info-mbox',
894
+			esc_html__('Billing Information', 'event_espresso'),
895
+			array($this, 'txn_billing_info_side_meta_box'),
896
+			$this->_wp_page_slug,
897
+			'side',
898
+			'high'
899
+		);
900
+	}
901
+
902
+
903
+	/**
904
+	 * Callback for transaction actions metabox.
905
+	 *
906
+	 * @param EE_Transaction|null $transaction
907
+	 * @return string
908
+	 * @throws DomainException
909
+	 * @throws EE_Error
910
+	 * @throws InvalidArgumentException
911
+	 * @throws InvalidDataTypeException
912
+	 * @throws InvalidInterfaceException
913
+	 * @throws ReflectionException
914
+	 * @throws RuntimeException
915
+	 */
916
+	public function getActionButtons(EE_Transaction $transaction = null)
917
+	{
918
+		$content = '';
919
+		$actions = array();
920
+		if (! $transaction instanceof EE_Transaction) {
921
+			return $content;
922
+		}
923
+		/** @var EE_Registration $primary_registration */
924
+		$primary_registration = $transaction->primary_registration();
925
+		$attendee = $primary_registration instanceof EE_Registration
926
+			? $primary_registration->attendee()
927
+			: null;
928
+
929
+		if ($attendee instanceof EE_Attendee
930
+			&& EE_Registry::instance()->CAP->current_user_can(
931
+				'ee_send_message',
932
+				'espresso_transactions_send_payment_reminder'
933
+			)
934
+		) {
935
+			$actions['payment_reminder'] =
936
+				EEH_MSG_Template::is_mt_active('payment_reminder')
937
+				&& $this->_transaction->status_ID() !== EEM_Transaction::complete_status_code
938
+				&& $this->_transaction->status_ID() !== EEM_Transaction::overpaid_status_code
939
+					? EEH_Template::get_button_or_link(
940
+						EE_Admin_Page::add_query_args_and_nonce(
941
+							array(
942
+								'action'      => 'send_payment_reminder',
943
+								'TXN_ID'      => $this->_transaction->ID(),
944
+								'redirect_to' => 'view_transaction',
945
+							),
946
+							TXN_ADMIN_URL
947
+						),
948
+						esc_html__(' Send Payment Reminder', 'event_espresso'),
949
+						'button secondary-button',
950
+						'dashicons dashicons-email-alt'
951
+					)
952
+					: '';
953
+		}
954
+
955
+		if (EE_Registry::instance()->CAP->current_user_can(
956
+			'ee_edit_payments',
957
+			'espresso_transactions_recalculate_line_items'
958
+		)
959
+		) {
960
+			$actions['recalculate_line_items'] = EEH_Template::get_button_or_link(
961
+				EE_Admin_Page::add_query_args_and_nonce(
962
+					array(
963
+						'action'      => 'espresso_recalculate_line_items',
964
+						'TXN_ID'      => $this->_transaction->ID(),
965
+						'redirect_to' => 'view_transaction',
966
+					),
967
+					TXN_ADMIN_URL
968
+				),
969
+				esc_html__(' Recalculate Taxes and Total', 'event_espresso'),
970
+				'button secondary-button',
971
+				'dashicons dashicons-update'
972
+			);
973
+		}
974
+
975
+		if ($primary_registration instanceof EE_Registration
976
+			&& EEH_MSG_Template::is_mt_active('receipt')
977
+		) {
978
+			$actions['receipt'] = EEH_Template::get_button_or_link(
979
+				$primary_registration->receipt_url(),
980
+				esc_html__('View Receipt', 'event_espresso'),
981
+				'button secondary-button',
982
+				'dashicons dashicons-media-default'
983
+			);
984
+		}
985
+
986
+		if ($primary_registration instanceof EE_Registration
987
+			&& EEH_MSG_Template::is_mt_active('invoice')
988
+		) {
989
+			$actions['invoice'] = EEH_Template::get_button_or_link(
990
+				$primary_registration->invoice_url(),
991
+				esc_html__('View Invoice', 'event_espresso'),
992
+				'button secondary-button',
993
+				'dashicons dashicons-media-spreadsheet'
994
+			);
995
+		}
996
+		$actions = array_filter(
997
+			apply_filters('FHEE__Transactions_Admin_Page__getActionButtons__actions', $actions, $transaction)
998
+		);
999
+		if ($actions) {
1000
+			$content = '<ul>';
1001
+			$content .= '<li>' . implode('</li><li>', $actions) . '</li>';
1002
+			$content .= '</uL>';
1003
+		}
1004
+		return $content;
1005
+	}
1006
+
1007
+
1008
+	/**
1009
+	 * txn_details_meta_box
1010
+	 * generates HTML for the Transaction main meta box
1011
+	 *
1012
+	 * @return void
1013
+	 * @throws DomainException
1014
+	 * @throws EE_Error
1015
+	 * @throws InvalidArgumentException
1016
+	 * @throws InvalidDataTypeException
1017
+	 * @throws InvalidInterfaceException
1018
+	 * @throws RuntimeException
1019
+	 * @throws ReflectionException
1020
+	 */
1021
+	public function txn_details_meta_box()
1022
+	{
1023
+		$this->_set_transaction_object();
1024
+		$this->_template_args['TXN_ID'] = $this->_transaction->ID();
1025
+		$this->_template_args['attendee'] = $this->_transaction->primary_registration() instanceof EE_Registration
1026
+			? $this->_transaction->primary_registration()->attendee()
1027
+			: null;
1028
+		$this->_template_args['can_edit_payments'] = EE_Registry::instance()->CAP->current_user_can(
1029
+			'ee_edit_payments',
1030
+			'apply_payment_or_refund_from_registration_details'
1031
+		);
1032
+		$this->_template_args['can_delete_payments'] = EE_Registry::instance()->CAP->current_user_can(
1033
+			'ee_delete_payments',
1034
+			'delete_payment_from_registration_details'
1035
+		);
1036
+
1037
+		// get line table
1038
+		EEH_Autoloader::register_line_item_display_autoloaders();
1039
+		$Line_Item_Display = new EE_Line_Item_Display(
1040
+			'admin_table',
1041
+			'EE_Admin_Table_Line_Item_Display_Strategy'
1042
+		);
1043
+		$this->_template_args['line_item_table'] = $Line_Item_Display->display_line_item(
1044
+			$this->_transaction->total_line_item()
1045
+		);
1046
+		$this->_template_args['REG_code'] = $this->_transaction->primary_registration()->reg_code();
1047
+
1048
+		// process taxes
1049
+		$taxes = $this->_transaction->line_items(array(array('LIN_type' => EEM_Line_Item::type_tax)));
1050
+		$this->_template_args['taxes'] = ! empty($taxes) ? $taxes : false;
1051
+
1052
+		$this->_template_args['grand_total'] = EEH_Template::format_currency(
1053
+			$this->_transaction->total(),
1054
+			false,
1055
+			false
1056
+		);
1057
+		$this->_template_args['grand_raw_total'] = $this->_transaction->total();
1058
+		$this->_template_args['TXN_status'] = $this->_transaction->status_ID();
1059
+
1060
+		// process payment details
1061
+		$payments = $this->_transaction->payments();
1062
+		if (! empty($payments)) {
1063
+			$this->_template_args['payments'] = $payments;
1064
+			$this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments);
1065
+		} else {
1066
+			$this->_template_args['payments'] = false;
1067
+			$this->_template_args['existing_reg_payments'] = array();
1068
+		}
1069
+
1070
+		$this->_template_args['edit_payment_url'] = add_query_arg(array('action' => 'edit_payment'), TXN_ADMIN_URL);
1071
+		$this->_template_args['delete_payment_url'] = add_query_arg(
1072
+			array('action' => 'espresso_delete_payment'),
1073
+			TXN_ADMIN_URL
1074
+		);
1075
+
1076
+		if (isset($txn_details['invoice_number'])) {
1077
+			$this->_template_args['txn_details']['invoice_number']['value'] = $this->_template_args['REG_code'];
1078
+			$this->_template_args['txn_details']['invoice_number']['label'] = esc_html__(
1079
+				'Invoice Number',
1080
+				'event_espresso'
1081
+			);
1082
+		}
1083
+
1084
+		$this->_template_args['txn_details']['registration_session']['value'] = $this->_transaction
1085
+			->primary_registration()
1086
+			->session_ID();
1087
+		$this->_template_args['txn_details']['registration_session']['label'] = esc_html__(
1088
+			'Registration Session',
1089
+			'event_espresso'
1090
+		);
1091
+
1092
+		$this->_template_args['txn_details']['ip_address']['value'] = isset($this->_session['ip_address'])
1093
+			? $this->_session['ip_address']
1094
+			: '';
1095
+		$this->_template_args['txn_details']['ip_address']['label'] = esc_html__(
1096
+			'Transaction placed from IP',
1097
+			'event_espresso'
1098
+		);
1099
+
1100
+		$this->_template_args['txn_details']['user_agent']['value'] = isset($this->_session['user_agent'])
1101
+			? $this->_session['user_agent']
1102
+			: '';
1103
+		$this->_template_args['txn_details']['user_agent']['label'] = esc_html__(
1104
+			'Registrant User Agent',
1105
+			'event_espresso'
1106
+		);
1107
+
1108
+		$reg_steps = '<ul>';
1109
+		foreach ($this->_transaction->reg_steps() as $reg_step => $reg_step_status) {
1110
+			if ($reg_step_status === true) {
1111
+				$reg_steps .= '<li style="color:#70cc50">'
1112
+							  . sprintf(
1113
+								  esc_html__('%1$s : Completed', 'event_espresso'),
1114
+								  ucwords(str_replace('_', ' ', $reg_step))
1115
+							  )
1116
+							  . '</li>';
1117
+			} elseif (is_numeric($reg_step_status) && $reg_step_status !== false) {
1118
+				$reg_steps .= '<li style="color:#2EA2CC">'
1119
+							  . sprintf(
1120
+								  esc_html__('%1$s : Initiated %2$s', 'event_espresso'),
1121
+								  ucwords(str_replace('_', ' ', $reg_step)),
1122
+								  date(
1123
+									  get_option('date_format') . ' ' . get_option('time_format'),
1124
+									  $reg_step_status + (get_option('gmt_offset') * HOUR_IN_SECONDS)
1125
+								  )
1126
+							  )
1127
+							  . '</li>';
1128
+			} else {
1129
+				$reg_steps .= '<li style="color:#E76700">'
1130
+							  . sprintf(
1131
+								  esc_html__('%1$s : Never Initiated', 'event_espresso'),
1132
+								  ucwords(str_replace('_', ' ', $reg_step))
1133
+							  )
1134
+							  . '</li>';
1135
+			}
1136
+		}
1137
+		$reg_steps .= '</ul>';
1138
+		$this->_template_args['txn_details']['reg_steps']['value'] = $reg_steps;
1139
+		$this->_template_args['txn_details']['reg_steps']['label'] = esc_html__(
1140
+			'Registration Step Progress',
1141
+			'event_espresso'
1142
+		);
1143
+
1144
+
1145
+		$this->_get_registrations_to_apply_payment_to();
1146
+		$this->_get_payment_methods($payments);
1147
+		$this->_get_payment_status_array();
1148
+		$this->_get_reg_status_selection(); // sets up the template args for the reg status array for the transaction.
1149
+
1150
+		$this->_template_args['transaction_form_url'] = add_query_arg(
1151
+			array(
1152
+				'action'  => 'edit_transaction',
1153
+				'process' => 'transaction',
1154
+			),
1155
+			TXN_ADMIN_URL
1156
+		);
1157
+		$this->_template_args['apply_payment_form_url'] = add_query_arg(
1158
+			array(
1159
+				'page'   => 'espresso_transactions',
1160
+				'action' => 'espresso_apply_payment',
1161
+			),
1162
+			WP_AJAX_URL
1163
+		);
1164
+		$this->_template_args['delete_payment_form_url'] = add_query_arg(
1165
+			array(
1166
+				'page'   => 'espresso_transactions',
1167
+				'action' => 'espresso_delete_payment',
1168
+			),
1169
+			WP_AJAX_URL
1170
+		);
1171
+
1172
+		$this->_template_args['action_buttons'] = $this->getActionButtons($this->_transaction);
1173
+
1174
+		// 'espresso_delete_payment_nonce'
1175
+
1176
+		$template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php';
1177
+		echo EEH_Template::display_template($template_path, $this->_template_args, true);
1178
+	}
1179
+
1180
+
1181
+	/**
1182
+	 * _get_registration_payment_IDs
1183
+	 *    generates an array of Payment IDs and their corresponding Registration IDs
1184
+	 *
1185
+	 * @access protected
1186
+	 * @param EE_Payment[] $payments
1187
+	 * @return array
1188
+	 * @throws EE_Error
1189
+	 * @throws InvalidArgumentException
1190
+	 * @throws InvalidDataTypeException
1191
+	 * @throws InvalidInterfaceException
1192
+	 * @throws ReflectionException
1193
+	 */
1194
+	protected function _get_registration_payment_IDs($payments = array())
1195
+	{
1196
+		$existing_reg_payments = array();
1197
+		// get all reg payments for these payments
1198
+		$reg_payments = EEM_Registration_Payment::instance()->get_all(
1199
+			array(
1200
+				array(
1201
+					'PAY_ID' => array(
1202
+						'IN',
1203
+						array_keys($payments),
1204
+					),
1205
+				),
1206
+			)
1207
+		);
1208
+		if (! empty($reg_payments)) {
1209
+			foreach ($payments as $payment) {
1210
+				if (! $payment instanceof EE_Payment) {
1211
+					continue;
1212
+				} elseif (! isset($existing_reg_payments[ $payment->ID() ])) {
1213
+					$existing_reg_payments[ $payment->ID() ] = array();
1214
+				}
1215
+				foreach ($reg_payments as $reg_payment) {
1216
+					if ($reg_payment instanceof EE_Registration_Payment
1217
+						&& $reg_payment->payment_ID() === $payment->ID()
1218
+					) {
1219
+						$existing_reg_payments[ $payment->ID() ][] = $reg_payment->registration_ID();
1220
+					}
1221
+				}
1222
+			}
1223
+		}
1224
+
1225
+		return $existing_reg_payments;
1226
+	}
1227
+
1228
+
1229
+	/**
1230
+	 * _get_registrations_to_apply_payment_to
1231
+	 *    generates HTML for displaying a series of checkboxes in the admin payment modal window
1232
+	 * which allows the admin to only apply the payment to the specific registrations
1233
+	 *
1234
+	 * @access protected
1235
+	 * @return void
1236
+	 * @throws EE_Error
1237
+	 * @throws InvalidArgumentException
1238
+	 * @throws InvalidDataTypeException
1239
+	 * @throws InvalidInterfaceException
1240
+	 * @throws ReflectionException
1241
+	 */
1242
+	protected function _get_registrations_to_apply_payment_to()
1243
+	{
1244
+		// we want any registration with an active status (ie: not deleted or cancelled)
1245
+		$query_params = array(
1246
+			array(
1247
+				'STS_ID' => array(
1248
+					'IN',
1249
+					array(
1250
+						EEM_Registration::status_id_approved,
1251
+						EEM_Registration::status_id_pending_payment,
1252
+						EEM_Registration::status_id_not_approved,
1253
+					),
1254
+				),
1255
+			),
1256
+		);
1257
+		$registrations_to_apply_payment_to = EEH_HTML::br() . EEH_HTML::div(
1258
+			'',
1259
+			'txn-admin-apply-payment-to-registrations-dv',
1260
+			'',
1261
+			'clear: both; margin: 1.5em 0 0; display: none;'
1262
+		);
1263
+		$registrations_to_apply_payment_to .= EEH_HTML::br() . EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap');
1264
+		$registrations_to_apply_payment_to .= EEH_HTML::table('', '', 'admin-primary-mbox-tbl');
1265
+		$registrations_to_apply_payment_to .= EEH_HTML::thead(
1266
+			EEH_HTML::tr(
1267
+				EEH_HTML::th(esc_html__('ID', 'event_espresso')) .
1268
+				EEH_HTML::th(esc_html__('Registrant', 'event_espresso')) .
1269
+				EEH_HTML::th(esc_html__('Ticket', 'event_espresso')) .
1270
+				EEH_HTML::th(esc_html__('Event', 'event_espresso')) .
1271
+				EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr') .
1272
+				EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr') .
1273
+				EEH_HTML::th(esc_html__('Apply', 'event_espresso'), '', 'jst-cntr')
1274
+			)
1275
+		);
1276
+		$registrations_to_apply_payment_to .= EEH_HTML::tbody();
1277
+		// get registrations for TXN
1278
+		$registrations = $this->_transaction->registrations($query_params);
1279
+		$existing_reg_payments = $this->_template_args['existing_reg_payments'];
1280
+		foreach ($registrations as $registration) {
1281
+			if ($registration instanceof EE_Registration) {
1282
+				$attendee_name = $registration->attendee() instanceof EE_Attendee
1283
+					? $registration->attendee()->full_name()
1284
+					: esc_html__('Unknown Attendee', 'event_espresso');
1285
+				$owing = $registration->final_price() - $registration->paid();
1286
+				$taxable = $registration->ticket()->taxable()
1287
+					? ' <span class="smaller-text lt-grey-text"> ' . esc_html__('+ tax', 'event_espresso') . '</span>'
1288
+					: '';
1289
+				$checked = empty($existing_reg_payments)
1290
+						   || in_array($registration->ID(), $existing_reg_payments, true)
1291
+					? ' checked="checked"'
1292
+					: '';
1293
+				$disabled = $registration->final_price() > 0 ? '' : ' disabled';
1294
+				$registrations_to_apply_payment_to .= EEH_HTML::tr(
1295
+					EEH_HTML::td($registration->ID()) .
1296
+					EEH_HTML::td($attendee_name) .
1297
+					EEH_HTML::td(
1298
+						$registration->ticket()->name() . ' : ' . $registration->ticket()->pretty_price() . $taxable
1299
+					) .
1300
+					EEH_HTML::td($registration->event_name()) .
1301
+					EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr') .
1302
+					EEH_HTML::td(
1303
+						EEH_Template::format_currency($owing),
1304
+						'',
1305
+						'txn-admin-payment-owing-td jst-cntr'
1306
+					) .
1307
+					EEH_HTML::td(
1308
+						'<input type="checkbox" value="' . $registration->ID()
1309
+						. '" name="txn_admin_payment[registrations]"'
1310
+						. $checked . $disabled . '>',
1311
+						'',
1312
+						'jst-cntr'
1313
+					),
1314
+					'apply-payment-registration-row-' . $registration->ID()
1315
+				);
1316
+			}
1317
+		}
1318
+		$registrations_to_apply_payment_to .= EEH_HTML::tbodyx();
1319
+		$registrations_to_apply_payment_to .= EEH_HTML::tablex();
1320
+		$registrations_to_apply_payment_to .= EEH_HTML::divx();
1321
+		$registrations_to_apply_payment_to .= EEH_HTML::p(
1322
+			esc_html__(
1323
+				'The payment will only be applied to the registrations that have a check mark in their corresponding check box. Checkboxes for free registrations have been disabled.',
1324
+				'event_espresso'
1325
+			),
1326
+			'',
1327
+			'clear description'
1328
+		);
1329
+		$registrations_to_apply_payment_to .= EEH_HTML::divx();
1330
+		$this->_template_args['registrations_to_apply_payment_to'] = $registrations_to_apply_payment_to;
1331
+	}
1332
+
1333
+
1334
+	/**
1335
+	 * _get_reg_status_selection
1336
+	 *
1337
+	 * @todo   this will need to be adjusted either once MER comes along OR we move default reg status to tickets
1338
+	 *         instead of events.
1339
+	 * @access protected
1340
+	 * @return void
1341
+	 * @throws EE_Error
1342
+	 */
1343
+	protected function _get_reg_status_selection()
1344
+	{
1345
+		// first get all possible statuses
1346
+		$statuses = EEM_Registration::reg_status_array(array(), true);
1347
+		// let's add a "don't change" option.
1348
+		$status_array['NAN'] = esc_html__('Leave the Same', 'event_espresso');
1349
+		$status_array = array_merge($status_array, $statuses);
1350
+		$this->_template_args['status_change_select'] = EEH_Form_Fields::select_input(
1351
+			'txn_reg_status_change[reg_status]',
1352
+			$status_array,
1353
+			'NAN',
1354
+			'id="txn-admin-payment-reg-status-inp"',
1355
+			'txn-reg-status-change-reg-status'
1356
+		);
1357
+		$this->_template_args['delete_status_change_select'] = EEH_Form_Fields::select_input(
1358
+			'delete_txn_reg_status_change[reg_status]',
1359
+			$status_array,
1360
+			'NAN',
1361
+			'delete-txn-admin-payment-reg-status-inp',
1362
+			'delete-txn-reg-status-change-reg-status'
1363
+		);
1364
+	}
1365
+
1366
+
1367
+	/**
1368
+	 *    _get_payment_methods
1369
+	 * Gets all the payment methods available generally, or the ones that are already
1370
+	 * selected on these payments (in case their payment methods are no longer active).
1371
+	 * Has the side-effect of updating the template args' payment_methods item
1372
+	 *
1373
+	 * @access private
1374
+	 * @param EE_Payment[] to show on this page
1375
+	 * @return void
1376
+	 * @throws EE_Error
1377
+	 * @throws InvalidArgumentException
1378
+	 * @throws InvalidDataTypeException
1379
+	 * @throws InvalidInterfaceException
1380
+	 * @throws ReflectionException
1381
+	 */
1382
+	private function _get_payment_methods($payments = array())
1383
+	{
1384
+		$payment_methods_of_payments = array();
1385
+		foreach ($payments as $payment) {
1386
+			if ($payment instanceof EE_Payment) {
1387
+				$payment_methods_of_payments[] = $payment->ID();
1388
+			}
1389
+		}
1390
+		if ($payment_methods_of_payments) {
1391
+			$query_args = array(
1392
+				array(
1393
+					'OR*payment_method_for_payment' => array(
1394
+						'PMD_ID'    => array('IN', $payment_methods_of_payments),
1395
+						'PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'),
1396
+					),
1397
+				),
1398
+			);
1399
+		} else {
1400
+			$query_args = array(array('PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%')));
1401
+		}
1402
+		$this->_template_args['payment_methods'] = EEM_Payment_Method::instance()->get_all($query_args);
1403
+	}
1404
+
1405
+
1406
+	/**
1407
+	 * txn_attendees_meta_box
1408
+	 *    generates HTML for the Attendees Transaction main meta box
1409
+	 *
1410
+	 * @access public
1411
+	 * @param WP_Post $post
1412
+	 * @param array   $metabox
1413
+	 * @return void
1414
+	 * @throws DomainException
1415
+	 * @throws EE_Error
1416
+	 * @throws InvalidArgumentException
1417
+	 * @throws InvalidDataTypeException
1418
+	 * @throws InvalidInterfaceException
1419
+	 * @throws ReflectionException
1420
+	 */
1421
+	public function txn_attendees_meta_box($post, $metabox = array('args' => array()))
1422
+	{
1423
+
1424
+		/** @noinspection NonSecureExtractUsageInspection */
1425
+		extract($metabox['args']);
1426
+		$this->_template_args['post'] = $post;
1427
+		$this->_template_args['event_attendees'] = array();
1428
+		// process items in cart
1429
+		$line_items = $this->_transaction->get_many_related(
1430
+			'Line_Item',
1431
+			array(array('LIN_type' => 'line-item'))
1432
+		);
1433
+		if (! empty($line_items)) {
1434
+			foreach ($line_items as $item) {
1435
+				if ($item instanceof EE_Line_Item) {
1436
+					switch ($item->OBJ_type()) {
1437
+						case 'Event':
1438
+							break;
1439
+						case 'Ticket':
1440
+							$ticket = $item->ticket();
1441
+							// right now we're only handling tickets here.
1442
+							// Cause its expected that only tickets will have attendees right?
1443
+							if (! $ticket instanceof EE_Ticket) {
1444
+								break;
1445
+							}
1446
+							try {
1447
+								$event_name = $ticket->get_event_name();
1448
+							} catch (Exception $e) {
1449
+								EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
1450
+								$event_name = esc_html__('Unknown Event', 'event_espresso');
1451
+							}
1452
+							$event_name .= ' - ' . $item->name();
1453
+							$ticket_price = EEH_Template::format_currency($item->unit_price());
1454
+							// now get all of the registrations for this transaction that use this ticket
1455
+							$registrations = $ticket->registrations(
1456
+								array(array('TXN_ID' => $this->_transaction->ID()))
1457
+							);
1458
+							foreach ($registrations as $registration) {
1459
+								if (! $registration instanceof EE_Registration) {
1460
+									break;
1461
+								}
1462
+								$this->_template_args['event_attendees'][ $registration->ID() ]['STS_ID']
1463
+									= $registration->status_ID();
1464
+								$this->_template_args['event_attendees'][ $registration->ID() ]['att_num']
1465
+									= $registration->count();
1466
+								$this->_template_args['event_attendees'][ $registration->ID() ]['event_ticket_name']
1467
+									= $event_name;
1468
+								$this->_template_args['event_attendees'][ $registration->ID() ]['ticket_price']
1469
+									= $ticket_price;
1470
+								// attendee info
1471
+								$attendee = $registration->get_first_related('Attendee');
1472
+								if ($attendee instanceof EE_Attendee) {
1473
+									$this->_template_args['event_attendees'][ $registration->ID() ]['att_id']
1474
+										= $attendee->ID();
1475
+									$this->_template_args['event_attendees'][ $registration->ID() ]['attendee']
1476
+										= $attendee->full_name();
1477
+									$this->_template_args['event_attendees'][ $registration->ID() ]['email']
1478
+										= '<a href="mailto:' . $attendee->email() . '?subject=' . $event_name
1479
+										  . esc_html__(
1480
+											  ' Event',
1481
+											  'event_espresso'
1482
+										  )
1483
+										  . '">' . $attendee->email() . '</a>';
1484
+									$this->_template_args['event_attendees'][ $registration->ID() ]['address']
1485
+										= EEH_Address::format($attendee, 'inline', false, false);
1486
+								} else {
1487
+									$this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] = '';
1488
+									$this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] = '';
1489
+									$this->_template_args['event_attendees'][ $registration->ID() ]['email'] = '';
1490
+									$this->_template_args['event_attendees'][ $registration->ID() ]['address'] = '';
1491
+								}
1492
+							}
1493
+							break;
1494
+					}
1495
+				}
1496
+			}
1497
+
1498
+			$this->_template_args['transaction_form_url'] = add_query_arg(
1499
+				array(
1500
+					'action'  => 'edit_transaction',
1501
+					'process' => 'attendees',
1502
+				),
1503
+				TXN_ADMIN_URL
1504
+			);
1505
+			echo EEH_Template::display_template(
1506
+				TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_attendees.template.php',
1507
+				$this->_template_args,
1508
+				true
1509
+			);
1510
+		} else {
1511
+			echo sprintf(
1512
+				esc_html__(
1513
+					'%1$sFor some reason, there are no attendees registered for this transaction. Likely the registration was abandoned in process.%2$s',
1514
+					'event_espresso'
1515
+				),
1516
+				'<p class="important-notice">',
1517
+				'</p>'
1518
+			);
1519
+		}
1520
+	}
1521
+
1522
+
1523
+	/**
1524
+	 * txn_registrant_side_meta_box
1525
+	 * generates HTML for the Edit Transaction side meta box
1526
+	 *
1527
+	 * @access public
1528
+	 * @return void
1529
+	 * @throws DomainException
1530
+	 * @throws EE_Error
1531
+	 * @throws InvalidArgumentException
1532
+	 * @throws InvalidDataTypeException
1533
+	 * @throws InvalidInterfaceException
1534
+	 * @throws ReflectionException
1535
+	 */
1536
+	public function txn_registrant_side_meta_box()
1537
+	{
1538
+		$primary_att = $this->_transaction->primary_registration() instanceof EE_Registration
1539
+			? $this->_transaction->primary_registration()->get_first_related('Attendee')
1540
+			: null;
1541
+		if (! $primary_att instanceof EE_Attendee) {
1542
+			$this->_template_args['no_attendee_message'] = esc_html__(
1543
+				'There is no attached contact for this transaction.  The transaction either failed due to an error or was abandoned.',
1544
+				'event_espresso'
1545
+			);
1546
+			$primary_att = EEM_Attendee::instance()->create_default_object();
1547
+		}
1548
+		$this->_template_args['ATT_ID'] = $primary_att->ID();
1549
+		$this->_template_args['prime_reg_fname'] = $primary_att->fname();
1550
+		$this->_template_args['prime_reg_lname'] = $primary_att->lname();
1551
+		$this->_template_args['prime_reg_email'] = $primary_att->email();
1552
+		$this->_template_args['prime_reg_phone'] = $primary_att->phone();
1553
+		$this->_template_args['edit_attendee_url'] = EE_Admin_Page::add_query_args_and_nonce(
1554
+			array(
1555
+				'action' => 'edit_attendee',
1556
+				'post'   => $primary_att->ID(),
1557
+			),
1558
+			REG_ADMIN_URL
1559
+		);
1560
+		// get formatted address for registrant
1561
+		$this->_template_args['formatted_address'] = EEH_Address::format($primary_att);
1562
+		echo EEH_Template::display_template(
1563
+			TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php',
1564
+			$this->_template_args,
1565
+			true
1566
+		);
1567
+	}
1568
+
1569
+
1570
+	/**
1571
+	 * txn_billing_info_side_meta_box
1572
+	 *    generates HTML for the Edit Transaction side meta box
1573
+	 *
1574
+	 * @access public
1575
+	 * @return void
1576
+	 * @throws DomainException
1577
+	 * @throws EE_Error
1578
+	 */
1579
+	public function txn_billing_info_side_meta_box()
1580
+	{
1581
+
1582
+		$this->_template_args['billing_form'] = $this->_transaction->billing_info();
1583
+		$this->_template_args['billing_form_url'] = add_query_arg(
1584
+			array('action' => 'edit_transaction', 'process' => 'billing'),
1585
+			TXN_ADMIN_URL
1586
+		);
1587
+
1588
+		$template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_billing_info.template.php';
1589
+		echo EEH_Template::display_template($template_path, $this->_template_args, true);
1590
+	}
1591
+
1592
+
1593
+	/**
1594
+	 * apply_payments_or_refunds
1595
+	 *    registers a payment or refund made towards a transaction
1596
+	 *
1597
+	 * @access public
1598
+	 * @return void
1599
+	 * @throws EE_Error
1600
+	 * @throws InvalidArgumentException
1601
+	 * @throws ReflectionException
1602
+	 * @throws RuntimeException
1603
+	 * @throws InvalidDataTypeException
1604
+	 * @throws InvalidInterfaceException
1605
+	 */
1606
+	public function apply_payments_or_refunds()
1607
+	{
1608
+		$json_response_data = array('return_data' => false);
1609
+		$valid_data = $this->_validate_payment_request_data();
1610
+		$has_access = EE_Registry::instance()->CAP->current_user_can(
1611
+			'ee_edit_payments',
1612
+			'apply_payment_or_refund_from_registration_details'
1613
+		);
1614
+		if (! empty($valid_data) && $has_access) {
1615
+			$PAY_ID = $valid_data['PAY_ID'];
1616
+			// save  the new payment
1617
+			$payment = $this->_create_payment_from_request_data($valid_data);
1618
+			// get the TXN for this payment
1619
+			$transaction = $payment->transaction();
1620
+			// verify transaction
1621
+			if ($transaction instanceof EE_Transaction) {
1622
+				// calculate_total_payments_and_update_status
1623
+				$this->_process_transaction_payments($transaction);
1624
+				$REG_IDs = $this->_get_REG_IDs_to_apply_payment_to($payment);
1625
+				$this->_remove_existing_registration_payments($payment, $PAY_ID);
1626
+				// apply payment to registrations (if applicable)
1627
+				if (! empty($REG_IDs)) {
1628
+					$this->_update_registration_payments($transaction, $payment, $REG_IDs);
1629
+					$this->_maybe_send_notifications();
1630
+					// now process status changes for the same registrations
1631
+					$this->_process_registration_status_change($transaction, $REG_IDs);
1632
+				}
1633
+				$this->_maybe_send_notifications($payment);
1634
+				// prepare to render page
1635
+				$json_response_data['return_data'] = $this->_build_payment_json_response($payment, $REG_IDs);
1636
+				do_action(
1637
+					'AHEE__Transactions_Admin_Page__apply_payments_or_refund__after_recording',
1638
+					$transaction,
1639
+					$payment
1640
+				);
1641
+			} else {
1642
+				EE_Error::add_error(
1643
+					esc_html__(
1644
+						'A valid Transaction for this payment could not be retrieved.',
1645
+						'event_espresso'
1646
+					),
1647
+					__FILE__,
1648
+					__FUNCTION__,
1649
+					__LINE__
1650
+				);
1651
+			}
1652
+		} elseif ($has_access) {
1653
+			EE_Error::add_error(
1654
+				esc_html__(
1655
+					'The payment form data could not be processed. Please try again.',
1656
+					'event_espresso'
1657
+				),
1658
+				__FILE__,
1659
+				__FUNCTION__,
1660
+				__LINE__
1661
+			);
1662
+		} else {
1663
+			EE_Error::add_error(
1664
+				esc_html__(
1665
+					'You do not have access to apply payments or refunds to a registration.',
1666
+					'event_espresso'
1667
+				),
1668
+				__FILE__,
1669
+				__FUNCTION__,
1670
+				__LINE__
1671
+			);
1672
+		}
1673
+		$notices = EE_Error::get_notices(
1674
+			false,
1675
+			false,
1676
+			false
1677
+		);
1678
+		$this->_template_args = array(
1679
+			'data'    => $json_response_data,
1680
+			'error'   => $notices['errors'],
1681
+			'success' => $notices['success'],
1682
+		);
1683
+		$this->_return_json();
1684
+	}
1685
+
1686
+
1687
+	/**
1688
+	 * _validate_payment_request_data
1689
+	 *
1690
+	 * @return array
1691
+	 * @throws EE_Error
1692
+	 * @throws InvalidArgumentException
1693
+	 * @throws InvalidDataTypeException
1694
+	 * @throws InvalidInterfaceException
1695
+	 */
1696
+	protected function _validate_payment_request_data()
1697
+	{
1698
+		if (! isset($this->_req_data['txn_admin_payment'])) {
1699
+			return array();
1700
+		}
1701
+		$payment_form = $this->_generate_payment_form_section();
1702
+		try {
1703
+			if ($payment_form->was_submitted()) {
1704
+				$payment_form->receive_form_submission();
1705
+				if (! $payment_form->is_valid()) {
1706
+					$submission_error_messages = array();
1707
+					foreach ($payment_form->get_validation_errors_accumulated() as $validation_error) {
1708
+						if ($validation_error instanceof EE_Validation_Error) {
1709
+							$submission_error_messages[] = sprintf(
1710
+								_x('%s : %s', 'Form Section Name : Form Validation Error', 'event_espresso'),
1711
+								$validation_error->get_form_section()->html_label_text(),
1712
+								$validation_error->getMessage()
1713
+							);
1714
+						}
1715
+					}
1716
+					EE_Error::add_error(
1717
+						implode('<br />', $submission_error_messages),
1718
+						__FILE__,
1719
+						__FUNCTION__,
1720
+						__LINE__
1721
+					);
1722
+					return array();
1723
+				}
1724
+			}
1725
+		} catch (EE_Error $e) {
1726
+			EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
1727
+			return array();
1728
+		}
1729
+
1730
+		return $payment_form->valid_data();
1731
+	}
1732
+
1733
+
1734
+	/**
1735
+	 * _generate_payment_form_section
1736
+	 *
1737
+	 * @return EE_Form_Section_Proper
1738
+	 * @throws EE_Error
1739
+	 */
1740
+	protected function _generate_payment_form_section()
1741
+	{
1742
+		return new EE_Form_Section_Proper(
1743
+			array(
1744
+				'name'        => 'txn_admin_payment',
1745
+				'subsections' => array(
1746
+					'PAY_ID'          => new EE_Text_Input(
1747
+						array(
1748
+							'default'               => 0,
1749
+							'required'              => false,
1750
+							'html_label_text'       => esc_html__('Payment ID', 'event_espresso'),
1751
+							'validation_strategies' => array(new EE_Int_Normalization()),
1752
+						)
1753
+					),
1754
+					'TXN_ID'          => new EE_Text_Input(
1755
+						array(
1756
+							'default'               => 0,
1757
+							'required'              => true,
1758
+							'html_label_text'       => esc_html__('Transaction ID', 'event_espresso'),
1759
+							'validation_strategies' => array(new EE_Int_Normalization()),
1760
+						)
1761
+					),
1762
+					'type'            => new EE_Text_Input(
1763
+						array(
1764
+							'default'               => 1,
1765
+							'required'              => true,
1766
+							'html_label_text'       => esc_html__('Payment or Refund', 'event_espresso'),
1767
+							'validation_strategies' => array(new EE_Int_Normalization()),
1768
+						)
1769
+					),
1770
+					'amount'          => new EE_Text_Input(
1771
+						array(
1772
+							'default'               => 0,
1773
+							'required'              => true,
1774
+							'html_label_text'       => esc_html__('Payment amount', 'event_espresso'),
1775
+							'validation_strategies' => array(new EE_Float_Normalization()),
1776
+						)
1777
+					),
1778
+					'status'          => new EE_Text_Input(
1779
+						array(
1780
+							'default'         => EEM_Payment::status_id_approved,
1781
+							'required'        => true,
1782
+							'html_label_text' => esc_html__('Payment status', 'event_espresso'),
1783
+						)
1784
+					),
1785
+					'PMD_ID'          => new EE_Text_Input(
1786
+						array(
1787
+							'default'               => 2,
1788
+							'required'              => true,
1789
+							'html_label_text'       => esc_html__('Payment Method', 'event_espresso'),
1790
+							'validation_strategies' => array(new EE_Int_Normalization()),
1791
+						)
1792
+					),
1793
+					'date'            => new EE_Text_Input(
1794
+						array(
1795
+							'default'         => time(),
1796
+							'required'        => true,
1797
+							'html_label_text' => esc_html__('Payment date', 'event_espresso'),
1798
+						)
1799
+					),
1800
+					'txn_id_chq_nmbr' => new EE_Text_Input(
1801
+						array(
1802
+							'default'               => '',
1803
+							'required'              => false,
1804
+							'html_label_text'       => esc_html__('Transaction or Cheque Number', 'event_espresso'),
1805
+							'validation_strategies' => array(
1806
+								new EE_Max_Length_Validation_Strategy(
1807
+									esc_html__('Input too long', 'event_espresso'),
1808
+									100
1809
+								),
1810
+							),
1811
+						)
1812
+					),
1813
+					'po_number'       => new EE_Text_Input(
1814
+						array(
1815
+							'default'               => '',
1816
+							'required'              => false,
1817
+							'html_label_text'       => esc_html__('Purchase Order Number', 'event_espresso'),
1818
+							'validation_strategies' => array(
1819
+								new EE_Max_Length_Validation_Strategy(
1820
+									esc_html__('Input too long', 'event_espresso'),
1821
+									100
1822
+								),
1823
+							),
1824
+						)
1825
+					),
1826
+					'accounting'      => new EE_Text_Input(
1827
+						array(
1828
+							'default'               => '',
1829
+							'required'              => false,
1830
+							'html_label_text'       => esc_html__('Extra Field for Accounting', 'event_espresso'),
1831
+							'validation_strategies' => array(
1832
+								new EE_Max_Length_Validation_Strategy(
1833
+									esc_html__('Input too long', 'event_espresso'),
1834
+									100
1835
+								),
1836
+							),
1837
+						)
1838
+					),
1839
+				),
1840
+			)
1841
+		);
1842
+	}
1843
+
1844
+
1845
+	/**
1846
+	 * _create_payment_from_request_data
1847
+	 *
1848
+	 * @param array $valid_data
1849
+	 * @return EE_Payment
1850
+	 * @throws EE_Error
1851
+	 * @throws InvalidArgumentException
1852
+	 * @throws InvalidDataTypeException
1853
+	 * @throws InvalidInterfaceException
1854
+	 * @throws ReflectionException
1855
+	 */
1856
+	protected function _create_payment_from_request_data($valid_data)
1857
+	{
1858
+		$PAY_ID = $valid_data['PAY_ID'];
1859
+		// get payment amount
1860
+		$amount = $valid_data['amount'] ? abs($valid_data['amount']) : 0;
1861
+		// payments have a type value of 1 and refunds have a type value of -1
1862
+		// so multiplying amount by type will give a positive value for payments, and negative values for refunds
1863
+		$amount = $valid_data['type'] < 0 ? $amount * -1 : $amount;
1864
+		// for some reason the date string coming in has extra spaces between the date and time.  This fixes that.
1865
+		$date = $valid_data['date']
1866
+			? preg_replace('/\s+/', ' ', $valid_data['date'])
1867
+			: date('Y-m-d g:i a', current_time('timestamp'));
1868
+		$payment = EE_Payment::new_instance(
1869
+			array(
1870
+				'TXN_ID'              => $valid_data['TXN_ID'],
1871
+				'STS_ID'              => $valid_data['status'],
1872
+				'PAY_timestamp'       => $date,
1873
+				'PAY_source'          => EEM_Payment_Method::scope_admin,
1874
+				'PMD_ID'              => $valid_data['PMD_ID'],
1875
+				'PAY_amount'          => $amount,
1876
+				'PAY_txn_id_chq_nmbr' => $valid_data['txn_id_chq_nmbr'],
1877
+				'PAY_po_number'       => $valid_data['po_number'],
1878
+				'PAY_extra_accntng'   => $valid_data['accounting'],
1879
+				'PAY_details'         => $valid_data,
1880
+				'PAY_ID'              => $PAY_ID,
1881
+			),
1882
+			'',
1883
+			array('Y-m-d', 'g:i a')
1884
+		);
1885
+
1886
+		if (! $payment->save()) {
1887
+			EE_Error::add_error(
1888
+				sprintf(
1889
+					esc_html__('Payment %1$d has not been successfully saved to the database.', 'event_espresso'),
1890
+					$payment->ID()
1891
+				),
1892
+				__FILE__,
1893
+				__FUNCTION__,
1894
+				__LINE__
1895
+			);
1896
+		}
1897
+
1898
+		return $payment;
1899
+	}
1900
+
1901
+
1902
+	/**
1903
+	 * _process_transaction_payments
1904
+	 *
1905
+	 * @param \EE_Transaction $transaction
1906
+	 * @return void
1907
+	 * @throws EE_Error
1908
+	 * @throws InvalidArgumentException
1909
+	 * @throws ReflectionException
1910
+	 * @throws InvalidDataTypeException
1911
+	 * @throws InvalidInterfaceException
1912
+	 */
1913
+	protected function _process_transaction_payments(EE_Transaction $transaction)
1914
+	{
1915
+		/** @type EE_Transaction_Payments $transaction_payments */
1916
+		$transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
1917
+		// update the transaction with this payment
1918
+		if ($transaction_payments->calculate_total_payments_and_update_status($transaction)) {
1919
+			EE_Error::add_success(
1920
+				esc_html__(
1921
+					'The payment has been processed successfully.',
1922
+					'event_espresso'
1923
+				),
1924
+				__FILE__,
1925
+				__FUNCTION__,
1926
+				__LINE__
1927
+			);
1928
+		} else {
1929
+			EE_Error::add_error(
1930
+				esc_html__(
1931
+					'The payment was processed successfully but the amount paid for the transaction was not updated.',
1932
+					'event_espresso'
1933
+				),
1934
+				__FILE__,
1935
+				__FUNCTION__,
1936
+				__LINE__
1937
+			);
1938
+		}
1939
+	}
1940
+
1941
+
1942
+	/**
1943
+	 * _get_REG_IDs_to_apply_payment_to
1944
+	 * returns a list of registration IDs that the payment will apply to
1945
+	 *
1946
+	 * @param \EE_Payment $payment
1947
+	 * @return array
1948
+	 * @throws EE_Error
1949
+	 * @throws InvalidArgumentException
1950
+	 * @throws InvalidDataTypeException
1951
+	 * @throws InvalidInterfaceException
1952
+	 * @throws ReflectionException
1953
+	 */
1954
+	protected function _get_REG_IDs_to_apply_payment_to(EE_Payment $payment)
1955
+	{
1956
+		$REG_IDs = array();
1957
+		// grab array of IDs for specific registrations to apply changes to
1958
+		if (isset($this->_req_data['txn_admin_payment']['registrations'])) {
1959
+			$REG_IDs = (array) $this->_req_data['txn_admin_payment']['registrations'];
1960
+		}
1961
+		// nothing specified ? then get all reg IDs
1962
+		if (empty($REG_IDs)) {
1963
+			$registrations = $payment->transaction()->registrations();
1964
+			$REG_IDs = ! empty($registrations)
1965
+				? array_keys($registrations)
1966
+				: $this->_get_existing_reg_payment_REG_IDs($payment);
1967
+		}
1968
+
1969
+		// ensure that REG_IDs are integers and NOT strings
1970
+		return array_map('intval', $REG_IDs);
1971
+	}
1972
+
1973
+
1974
+	/**
1975
+	 * @return array
1976
+	 */
1977
+	public function existing_reg_payment_REG_IDs()
1978
+	{
1979
+		return $this->_existing_reg_payment_REG_IDs;
1980
+	}
1981
+
1982
+
1983
+	/**
1984
+	 * @param array $existing_reg_payment_REG_IDs
1985
+	 */
1986
+	public function set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs = null)
1987
+	{
1988
+		$this->_existing_reg_payment_REG_IDs = $existing_reg_payment_REG_IDs;
1989
+	}
1990
+
1991
+
1992
+	/**
1993
+	 * _get_existing_reg_payment_REG_IDs
1994
+	 * returns a list of registration IDs that the payment is currently related to
1995
+	 * as recorded in the database
1996
+	 *
1997
+	 * @param \EE_Payment $payment
1998
+	 * @return array
1999
+	 * @throws EE_Error
2000
+	 * @throws InvalidArgumentException
2001
+	 * @throws InvalidDataTypeException
2002
+	 * @throws InvalidInterfaceException
2003
+	 * @throws ReflectionException
2004
+	 */
2005
+	protected function _get_existing_reg_payment_REG_IDs(EE_Payment $payment)
2006
+	{
2007
+		if ($this->existing_reg_payment_REG_IDs() === null) {
2008
+			// let's get any existing reg payment records for this payment
2009
+			$existing_reg_payment_REG_IDs = $payment->get_many_related('Registration');
2010
+			// but we only want the REG IDs, so grab the array keys
2011
+			$existing_reg_payment_REG_IDs = ! empty($existing_reg_payment_REG_IDs)
2012
+				? array_keys($existing_reg_payment_REG_IDs)
2013
+				: array();
2014
+			$this->set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs);
2015
+		}
2016
+
2017
+		return $this->existing_reg_payment_REG_IDs();
2018
+	}
2019
+
2020
+
2021
+	/**
2022
+	 * _remove_existing_registration_payments
2023
+	 * this calculates the difference between existing relations
2024
+	 * to the supplied payment and the new list registration IDs,
2025
+	 * removes any related registrations that no longer apply,
2026
+	 * and then updates the registration paid fields
2027
+	 *
2028
+	 * @param \EE_Payment $payment
2029
+	 * @param int         $PAY_ID
2030
+	 * @return bool;
2031
+	 * @throws EE_Error
2032
+	 * @throws InvalidArgumentException
2033
+	 * @throws ReflectionException
2034
+	 * @throws InvalidDataTypeException
2035
+	 * @throws InvalidInterfaceException
2036
+	 */
2037
+	protected function _remove_existing_registration_payments(EE_Payment $payment, $PAY_ID = 0)
2038
+	{
2039
+		// newly created payments will have nothing recorded for $PAY_ID
2040
+		if (absint($PAY_ID) === 0) {
2041
+			return false;
2042
+		}
2043
+		$existing_reg_payment_REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment);
2044
+		if (empty($existing_reg_payment_REG_IDs)) {
2045
+			return false;
2046
+		}
2047
+		/** @type EE_Transaction_Payments $transaction_payments */
2048
+		$transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
2049
+
2050
+		return $transaction_payments->delete_registration_payments_and_update_registrations(
2051
+			$payment,
2052
+			array(
2053
+				array(
2054
+					'PAY_ID' => $payment->ID(),
2055
+					'REG_ID' => array('IN', $existing_reg_payment_REG_IDs),
2056
+				),
2057
+			)
2058
+		);
2059
+	}
2060
+
2061
+
2062
+	/**
2063
+	 * _update_registration_payments
2064
+	 * this applies the payments to the selected registrations
2065
+	 * but only if they have not already been paid for
2066
+	 *
2067
+	 * @param  EE_Transaction $transaction
2068
+	 * @param \EE_Payment     $payment
2069
+	 * @param array           $REG_IDs
2070
+	 * @return void
2071
+	 * @throws EE_Error
2072
+	 * @throws InvalidArgumentException
2073
+	 * @throws ReflectionException
2074
+	 * @throws RuntimeException
2075
+	 * @throws InvalidDataTypeException
2076
+	 * @throws InvalidInterfaceException
2077
+	 */
2078
+	protected function _update_registration_payments(
2079
+		EE_Transaction $transaction,
2080
+		EE_Payment $payment,
2081
+		$REG_IDs = array()
2082
+	) {
2083
+		// we can pass our own custom set of registrations to EE_Payment_Processor::process_registration_payments()
2084
+		// so let's do that using our set of REG_IDs from the form
2085
+		$registration_query_where_params = array(
2086
+			'REG_ID' => array('IN', $REG_IDs),
2087
+		);
2088
+		// but add in some conditions regarding payment,
2089
+		// so that we don't apply payments to registrations that are free or have already been paid for
2090
+		// but ONLY if the payment is NOT a refund ( ie: the payment amount is not negative )
2091
+		if (! $payment->is_a_refund()) {
2092
+			$registration_query_where_params['REG_final_price'] = array('!=', 0);
2093
+			$registration_query_where_params['REG_final_price*'] = array('!=', 'REG_paid', true);
2094
+		}
2095
+		$registrations = $transaction->registrations(array($registration_query_where_params));
2096
+		if (! empty($registrations)) {
2097
+			/** @type EE_Payment_Processor $payment_processor */
2098
+			$payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
2099
+			$payment_processor->process_registration_payments($transaction, $payment, $registrations);
2100
+		}
2101
+	}
2102
+
2103
+
2104
+	/**
2105
+	 * _process_registration_status_change
2106
+	 * This processes requested registration status changes for all the registrations
2107
+	 * on a given transaction and (optionally) sends out notifications for the changes.
2108
+	 *
2109
+	 * @param  EE_Transaction $transaction
2110
+	 * @param array           $REG_IDs
2111
+	 * @return bool
2112
+	 * @throws EE_Error
2113
+	 * @throws InvalidArgumentException
2114
+	 * @throws ReflectionException
2115
+	 * @throws InvalidDataTypeException
2116
+	 * @throws InvalidInterfaceException
2117
+	 */
2118
+	protected function _process_registration_status_change(EE_Transaction $transaction, $REG_IDs = array())
2119
+	{
2120
+		// first if there is no change in status then we get out.
2121
+		if (! isset($this->_req_data['txn_reg_status_change']['reg_status'])
2122
+			|| $this->_req_data['txn_reg_status_change']['reg_status'] === 'NAN'
2123
+		) {
2124
+			// no error message, no change requested, just nothing to do man.
2125
+			return false;
2126
+		}
2127
+		/** @type EE_Transaction_Processor $transaction_processor */
2128
+		$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
2129
+
2130
+		// made it here dude?  Oh WOW.  K, let's take care of changing the statuses
2131
+		return $transaction_processor->manually_update_registration_statuses(
2132
+			$transaction,
2133
+			sanitize_text_field($this->_req_data['txn_reg_status_change']['reg_status']),
2134
+			array(array('REG_ID' => array('IN', $REG_IDs)))
2135
+		);
2136
+	}
2137
+
2138
+
2139
+	/**
2140
+	 * _build_payment_json_response
2141
+	 *
2142
+	 * @access public
2143
+	 * @param \EE_Payment $payment
2144
+	 * @param array       $REG_IDs
2145
+	 * @param bool | null $delete_txn_reg_status_change
2146
+	 * @return array
2147
+	 * @throws EE_Error
2148
+	 * @throws InvalidArgumentException
2149
+	 * @throws InvalidDataTypeException
2150
+	 * @throws InvalidInterfaceException
2151
+	 * @throws ReflectionException
2152
+	 */
2153
+	protected function _build_payment_json_response(
2154
+		EE_Payment $payment,
2155
+		$REG_IDs = array(),
2156
+		$delete_txn_reg_status_change = null
2157
+	) {
2158
+		// was the payment deleted ?
2159
+		if (is_bool($delete_txn_reg_status_change)) {
2160
+			return array(
2161
+				'PAY_ID'                       => $payment->ID(),
2162
+				'amount'                       => $payment->amount(),
2163
+				'total_paid'                   => $payment->transaction()->paid(),
2164
+				'txn_status'                   => $payment->transaction()->status_ID(),
2165
+				'pay_status'                   => $payment->STS_ID(),
2166
+				'registrations'                => $this->_registration_payment_data_array($REG_IDs),
2167
+				'delete_txn_reg_status_change' => $delete_txn_reg_status_change,
2168
+			);
2169
+		} else {
2170
+			$this->_get_payment_status_array();
2171
+
2172
+			return array(
2173
+				'amount'           => $payment->amount(),
2174
+				'total_paid'       => $payment->transaction()->paid(),
2175
+				'txn_status'       => $payment->transaction()->status_ID(),
2176
+				'pay_status'       => $payment->STS_ID(),
2177
+				'PAY_ID'           => $payment->ID(),
2178
+				'STS_ID'           => $payment->STS_ID(),
2179
+				'status'           => self::$_pay_status[ $payment->STS_ID() ],
2180
+				'date'             => $payment->timestamp('Y-m-d', 'h:i a'),
2181
+				'method'           => strtoupper($payment->source()),
2182
+				'PM_ID'            => $payment->payment_method() ? $payment->payment_method()->ID() : 1,
2183
+				'gateway'          => $payment->payment_method()
2184
+					? $payment->payment_method()->admin_name()
2185
+					: esc_html__('Unknown', 'event_espresso'),
2186
+				'gateway_response' => $payment->gateway_response(),
2187
+				'txn_id_chq_nmbr'  => $payment->txn_id_chq_nmbr(),
2188
+				'po_number'        => $payment->po_number(),
2189
+				'extra_accntng'    => $payment->extra_accntng(),
2190
+				'registrations'    => $this->_registration_payment_data_array($REG_IDs),
2191
+			);
2192
+		}
2193
+	}
2194
+
2195
+
2196
+	/**
2197
+	 * delete_payment
2198
+	 *    delete a payment or refund made towards a transaction
2199
+	 *
2200
+	 * @access public
2201
+	 * @return void
2202
+	 * @throws EE_Error
2203
+	 * @throws InvalidArgumentException
2204
+	 * @throws ReflectionException
2205
+	 * @throws InvalidDataTypeException
2206
+	 * @throws InvalidInterfaceException
2207
+	 */
2208
+	public function delete_payment()
2209
+	{
2210
+		$json_response_data = array('return_data' => false);
2211
+		$PAY_ID = isset($this->_req_data['delete_txn_admin_payment']['PAY_ID'])
2212
+			? absint($this->_req_data['delete_txn_admin_payment']['PAY_ID'])
2213
+			: 0;
2214
+		$can_delete = EE_Registry::instance()->CAP->current_user_can(
2215
+			'ee_delete_payments',
2216
+			'delete_payment_from_registration_details'
2217
+		);
2218
+		if ($PAY_ID && $can_delete) {
2219
+			$delete_txn_reg_status_change = isset($this->_req_data['delete_txn_reg_status_change'])
2220
+				? $this->_req_data['delete_txn_reg_status_change']
2221
+				: false;
2222
+			$payment = EEM_Payment::instance()->get_one_by_ID($PAY_ID);
2223
+			if ($payment instanceof EE_Payment) {
2224
+				$REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment);
2225
+				/** @type EE_Transaction_Payments $transaction_payments */
2226
+				$transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
2227
+				if ($transaction_payments->delete_payment_and_update_transaction($payment)) {
2228
+					$json_response_data['return_data'] = $this->_build_payment_json_response(
2229
+						$payment,
2230
+						$REG_IDs,
2231
+						$delete_txn_reg_status_change
2232
+					);
2233
+					if ($delete_txn_reg_status_change) {
2234
+						$this->_req_data['txn_reg_status_change'] = $delete_txn_reg_status_change;
2235
+						// MAKE sure we also add the delete_txn_req_status_change to the
2236
+						// $_REQUEST global because that's how messages will be looking for it.
2237
+						$_REQUEST['txn_reg_status_change'] = $delete_txn_reg_status_change;
2238
+						$this->_maybe_send_notifications();
2239
+						$this->_process_registration_status_change($payment->transaction(), $REG_IDs);
2240
+					}
2241
+				}
2242
+			} else {
2243
+				EE_Error::add_error(
2244
+					esc_html__('Valid Payment data could not be retrieved from the database.', 'event_espresso'),
2245
+					__FILE__,
2246
+					__FUNCTION__,
2247
+					__LINE__
2248
+				);
2249
+			}
2250
+		} elseif ($can_delete) {
2251
+			EE_Error::add_error(
2252
+				esc_html__(
2253
+					'A valid Payment ID was not received, therefore payment form data could not be loaded.',
2254
+					'event_espresso'
2255
+				),
2256
+				__FILE__,
2257
+				__FUNCTION__,
2258
+				__LINE__
2259
+			);
2260
+		} else {
2261
+			EE_Error::add_error(
2262
+				esc_html__(
2263
+					'You do not have access to delete a payment.',
2264
+					'event_espresso'
2265
+				),
2266
+				__FILE__,
2267
+				__FUNCTION__,
2268
+				__LINE__
2269
+			);
2270
+		}
2271
+		$notices = EE_Error::get_notices(false, false, false);
2272
+		$this->_template_args = array(
2273
+			'data'      => $json_response_data,
2274
+			'success'   => $notices['success'],
2275
+			'error'     => $notices['errors'],
2276
+			'attention' => $notices['attention'],
2277
+		);
2278
+		$this->_return_json();
2279
+	}
2280
+
2281
+
2282
+	/**
2283
+	 * _registration_payment_data_array
2284
+	 * adds info for 'owing' and 'paid' for each registration to the json response
2285
+	 *
2286
+	 * @access protected
2287
+	 * @param array $REG_IDs
2288
+	 * @return array
2289
+	 * @throws EE_Error
2290
+	 * @throws InvalidArgumentException
2291
+	 * @throws InvalidDataTypeException
2292
+	 * @throws InvalidInterfaceException
2293
+	 * @throws ReflectionException
2294
+	 */
2295
+	protected function _registration_payment_data_array($REG_IDs)
2296
+	{
2297
+		$registration_payment_data = array();
2298
+		// if non empty reg_ids lets get an array of registrations and update the values for the apply_payment/refund rows.
2299
+		if (! empty($REG_IDs)) {
2300
+			$registrations = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $REG_IDs))));
2301
+			foreach ($registrations as $registration) {
2302
+				if ($registration instanceof EE_Registration) {
2303
+					$registration_payment_data[ $registration->ID() ] = array(
2304
+						'paid'  => $registration->pretty_paid(),
2305
+						'owing' => EEH_Template::format_currency($registration->final_price() - $registration->paid()),
2306
+					);
2307
+				}
2308
+			}
2309
+		}
2310
+
2311
+		return $registration_payment_data;
2312
+	}
2313
+
2314
+
2315
+	/**
2316
+	 * _maybe_send_notifications
2317
+	 * determines whether or not the admin has indicated that notifications should be sent.
2318
+	 * If so, will toggle a filter switch for delivering registration notices.
2319
+	 * If passed an EE_Payment object, then it will trigger payment notifications instead.
2320
+	 *
2321
+	 * @access protected
2322
+	 * @param \EE_Payment | null $payment
2323
+	 */
2324
+	protected function _maybe_send_notifications($payment = null)
2325
+	{
2326
+		switch ($payment instanceof EE_Payment) {
2327
+			// payment notifications
2328
+			case true:
2329
+				if (isset($this->_req_data['txn_payments']['send_notifications'])
2330
+					&& filter_var(
2331
+						$this->_req_data['txn_payments']['send_notifications'],
2332
+						FILTER_VALIDATE_BOOLEAN
2333
+					)
2334
+				) {
2335
+					$this->_process_payment_notification($payment);
2336
+				}
2337
+				break;
2338
+			// registration notifications
2339
+			case false:
2340
+				if (isset($this->_req_data['txn_reg_status_change']['send_notifications'])
2341
+					&& filter_var(
2342
+						$this->_req_data['txn_reg_status_change']['send_notifications'],
2343
+						FILTER_VALIDATE_BOOLEAN
2344
+					)
2345
+				) {
2346
+					add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true');
2347
+				}
2348
+				break;
2349
+		}
2350
+	}
2351
+
2352
+
2353
+	/**
2354
+	 * _send_payment_reminder
2355
+	 *    generates HTML for the View Transaction Details Admin page
2356
+	 *
2357
+	 * @access protected
2358
+	 * @return void
2359
+	 * @throws EE_Error
2360
+	 * @throws InvalidArgumentException
2361
+	 * @throws InvalidDataTypeException
2362
+	 * @throws InvalidInterfaceException
2363
+	 */
2364
+	protected function _send_payment_reminder()
2365
+	{
2366
+		$TXN_ID = ! empty($this->_req_data['TXN_ID']) ? absint($this->_req_data['TXN_ID']) : false;
2367
+		$transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID);
2368
+		$query_args = isset($this->_req_data['redirect_to']) ? array(
2369
+			'action' => $this->_req_data['redirect_to'],
2370
+			'TXN_ID' => $this->_req_data['TXN_ID'],
2371
+		) : array();
2372
+		do_action(
2373
+			'AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder',
2374
+			$transaction
2375
+		);
2376
+		$this->_redirect_after_action(
2377
+			false,
2378
+			esc_html__('payment reminder', 'event_espresso'),
2379
+			esc_html__('sent', 'event_espresso'),
2380
+			$query_args,
2381
+			true
2382
+		);
2383
+	}
2384
+
2385
+
2386
+	/**
2387
+	 *  get_transactions
2388
+	 *    get transactions for given parameters (used by list table)
2389
+	 *
2390
+	 * @param  int     $perpage how many transactions displayed per page
2391
+	 * @param  boolean $count   return the count or objects
2392
+	 * @param string   $view
2393
+	 * @return mixed int = count || array of transaction objects
2394
+	 * @throws EE_Error
2395
+	 * @throws InvalidArgumentException
2396
+	 * @throws InvalidDataTypeException
2397
+	 * @throws InvalidInterfaceException
2398
+	 */
2399
+	public function get_transactions($perpage, $count = false, $view = '')
2400
+	{
2401
+
2402
+		$TXN = EEM_Transaction::instance();
2403
+
2404
+		$start_date = isset($this->_req_data['txn-filter-start-date'])
2405
+			? wp_strip_all_tags($this->_req_data['txn-filter-start-date'])
2406
+			: date(
2407
+				'm/d/Y',
2408
+				strtotime('-10 year')
2409
+			);
2410
+		$end_date = isset($this->_req_data['txn-filter-end-date'])
2411
+			? wp_strip_all_tags($this->_req_data['txn-filter-end-date'])
2412
+			: date('m/d/Y');
2413
+
2414
+		// make sure our timestamps start and end right at the boundaries for each day
2415
+		$start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00';
2416
+		$end_date = date('Y-m-d', strtotime($end_date)) . ' 23:59:59';
2417
+
2418
+
2419
+		// convert to timestamps
2420
+		$start_date = strtotime($start_date);
2421
+		$end_date = strtotime($end_date);
2422
+
2423
+		// makes sure start date is the lowest value and vice versa
2424
+		$start_date = min($start_date, $end_date);
2425
+		$end_date = max($start_date, $end_date);
2426
+
2427
+		// convert to correct format for query
2428
+		$start_date = EEM_Transaction::instance()->convert_datetime_for_query(
2429
+			'TXN_timestamp',
2430
+			date('Y-m-d H:i:s', $start_date),
2431
+			'Y-m-d H:i:s'
2432
+		);
2433
+		$end_date = EEM_Transaction::instance()->convert_datetime_for_query(
2434
+			'TXN_timestamp',
2435
+			date('Y-m-d H:i:s', $end_date),
2436
+			'Y-m-d H:i:s'
2437
+		);
2438
+
2439
+
2440
+		// set orderby
2441
+		$this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : '';
2442
+
2443
+		switch ($this->_req_data['orderby']) {
2444
+			case 'TXN_ID':
2445
+				$orderby = 'TXN_ID';
2446
+				break;
2447
+			case 'ATT_fname':
2448
+				$orderby = 'Registration.Attendee.ATT_fname';
2449
+				break;
2450
+			case 'event_name':
2451
+				$orderby = 'Registration.Event.EVT_name';
2452
+				break;
2453
+			default: // 'TXN_timestamp'
2454
+				$orderby = 'TXN_timestamp';
2455
+		}
2456
+
2457
+		$sort = ! empty($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC';
2458
+		$current_page = ! empty($this->_req_data['paged']) ? $this->_req_data['paged'] : 1;
2459
+		$per_page = ! empty($perpage) ? $perpage : 10;
2460
+		$per_page = ! empty($this->_req_data['perpage']) ? $this->_req_data['perpage'] : $per_page;
2461
+
2462
+		$offset = ($current_page - 1) * $per_page;
2463
+		$limit = array($offset, $per_page);
2464
+
2465
+		$_where = array(
2466
+			'TXN_timestamp'          => array('BETWEEN', array($start_date, $end_date)),
2467
+			'Registration.REG_count' => 1,
2468
+		);
2469
+
2470
+		if (isset($this->_req_data['EVT_ID'])) {
2471
+			$_where['Registration.EVT_ID'] = $this->_req_data['EVT_ID'];
2472
+		}
2473
+
2474
+		if (isset($this->_req_data['s'])) {
2475
+			$search_string = '%' . $this->_req_data['s'] . '%';
2476
+			$_where['OR'] = array(
2477
+				'Registration.Event.EVT_name'         => array('LIKE', $search_string),
2478
+				'Registration.Event.EVT_desc'         => array('LIKE', $search_string),
2479
+				'Registration.Event.EVT_short_desc'   => array('LIKE', $search_string),
2480
+				'Registration.Attendee.ATT_full_name' => array('LIKE', $search_string),
2481
+				'Registration.Attendee.ATT_fname'     => array('LIKE', $search_string),
2482
+				'Registration.Attendee.ATT_lname'     => array('LIKE', $search_string),
2483
+				'Registration.Attendee.ATT_short_bio' => array('LIKE', $search_string),
2484
+				'Registration.Attendee.ATT_email'     => array('LIKE', $search_string),
2485
+				'Registration.Attendee.ATT_address'   => array('LIKE', $search_string),
2486
+				'Registration.Attendee.ATT_address2'  => array('LIKE', $search_string),
2487
+				'Registration.Attendee.ATT_city'      => array('LIKE', $search_string),
2488
+				'Registration.REG_final_price'        => array('LIKE', $search_string),
2489
+				'Registration.REG_code'               => array('LIKE', $search_string),
2490
+				'Registration.REG_count'              => array('LIKE', $search_string),
2491
+				'Registration.REG_group_size'         => array('LIKE', $search_string),
2492
+				'Registration.Ticket.TKT_name'        => array('LIKE', $search_string),
2493
+				'Registration.Ticket.TKT_description' => array('LIKE', $search_string),
2494
+				'Payment.PAY_source'                  => array('LIKE', $search_string),
2495
+				'Payment.Payment_Method.PMD_name'     => array('LIKE', $search_string),
2496
+				'TXN_session_data'                    => array('LIKE', $search_string),
2497
+				'Payment.PAY_txn_id_chq_nmbr'         => array('LIKE', $search_string),
2498
+			);
2499
+		}
2500
+
2501
+		// failed transactions
2502
+		$failed = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'failed' && ! $count)
2503
+				  || ($count && $view === 'failed');
2504
+		$abandoned = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'abandoned' && ! $count)
2505
+					 || ($count && $view === 'abandoned');
2506
+		$incomplete = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'incomplete' && ! $count)
2507
+					  || ($count && $view === 'incomplete');
2508
+
2509
+		if ($failed) {
2510
+			$_where['STS_ID'] = EEM_Transaction::failed_status_code;
2511
+		} elseif ($abandoned) {
2512
+			$_where['STS_ID'] = EEM_Transaction::abandoned_status_code;
2513
+		} elseif ($incomplete) {
2514
+			$_where['STS_ID'] = EEM_Transaction::incomplete_status_code;
2515
+		} else {
2516
+			$_where['STS_ID'] = array('!=', EEM_Transaction::failed_status_code);
2517
+			$_where['STS_ID*'] = array('!=', EEM_Transaction::abandoned_status_code);
2518
+		}
2519
+
2520
+		$query_params = apply_filters(
2521
+			'FHEE__Transactions_Admin_Page___get_transactions_query_params',
2522
+			array(
2523
+				$_where,
2524
+				'order_by'                 => array($orderby => $sort),
2525
+				'limit'                    => $limit,
2526
+				'default_where_conditions' => EEM_Base::default_where_conditions_this_only,
2527
+			),
2528
+			$this->_req_data,
2529
+			$view,
2530
+			$count
2531
+		);
2532
+
2533
+		$transactions = $count
2534
+			? $TXN->count(array($query_params[0]), 'TXN_ID', true)
2535
+			: $TXN->get_all($query_params);
2536
+
2537
+		return $transactions;
2538
+	}
2539
+
2540
+
2541
+	/**
2542
+	 * @since 4.9.79.p
2543
+	 * @throws EE_Error
2544
+	 * @throws InvalidArgumentException
2545
+	 * @throws InvalidDataTypeException
2546
+	 * @throws InvalidInterfaceException
2547
+	 * @throws ReflectionException
2548
+	 * @throws RuntimeException
2549
+	 */
2550
+	public function recalculateLineItems()
2551
+	{
2552
+		$TXN_ID = ! empty($this->_req_data['TXN_ID']) ? absint($this->_req_data['TXN_ID']) : false;
2553
+		/** @var EE_Transaction $transaction */
2554
+		$transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID);
2555
+		$total_line_item = $transaction->total_line_item(false);
2556
+		$success = false;
2557
+		if ($total_line_item instanceof EE_Line_Item) {
2558
+			EEH_Line_Item::resetIsTaxableForTickets($total_line_item);
2559
+			$success = EEH_Line_Item::apply_taxes($total_line_item, true);
2560
+		}
2561
+		$this->_redirect_after_action(
2562
+			(bool) $success,
2563
+			esc_html__('Transaction taxes and totals', 'event_espresso'),
2564
+			esc_html__('recalculated', 'event_espresso'),
2565
+			isset($this->_req_data['redirect_to'])
2566
+				? array(
2567
+				'action' => $this->_req_data['redirect_to'],
2568
+				'TXN_ID' => $this->_req_data['TXN_ID'],
2569
+			)
2570
+				: array(),
2571
+			true
2572
+		);
2573
+	}
2574 2574
 }
Please login to merge, or discard this patch.
core/db_classes/EE_Transaction.class.php 2 patches
Doc Comments   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -366,7 +366,7 @@  discard block
 block discarded – undo
366 366
 
367 367
 
368 368
     /**
369
-     * @return mixed|null
369
+     * @return EE_Cart|null
370 370
      * @throws EE_Error
371 371
      * @throws InvalidArgumentException
372 372
      * @throws InvalidDataTypeException
@@ -508,7 +508,7 @@  discard block
 block discarded – undo
508 508
      * Gets all the attendees for this transaction (handy for use with EE_Attendee's get_registrations_for_event
509 509
      * function for getting attendees and how many registrations they each have for an event)
510 510
      *
511
-     * @return mixed EE_Attendee[] by default, int if $output is set to 'COUNT'
511
+     * @return EE_Base_Class[] EE_Attendee[] by default, int if $output is set to 'COUNT'
512 512
      * @throws EE_Error
513 513
      * @throws InvalidArgumentException
514 514
      * @throws InvalidDataTypeException
@@ -564,7 +564,7 @@  discard block
 block discarded – undo
564 564
     /**
565 565
      * Gets all payments which have not been approved
566 566
      *
567
-     * @return EE_Base_Class[]|EEI_Payment[]
567
+     * @return EE_Base_Class[]
568 568
      * @throws EE_Error if a model is misconfigured somehow
569 569
      * @throws InvalidArgumentException
570 570
      * @throws InvalidDataTypeException
Please login to merge, or discard this patch.
Indentation   +1678 added lines, -1678 removed lines patch added patch discarded remove patch
@@ -13,1682 +13,1682 @@
 block discarded – undo
13 13
 class EE_Transaction extends EE_Base_Class implements EEI_Transaction
14 14
 {
15 15
 
16
-    /**
17
-     * The length of time in seconds that a lock is applied before being considered expired.
18
-     * It is not long because a transaction should only be locked for the duration of the request that locked it
19
-     */
20
-    const LOCK_EXPIRATION = 2;
21
-
22
-    /**
23
-     * txn status upon initial construction.
24
-     *
25
-     * @var string
26
-     */
27
-    protected $_old_txn_status;
28
-
29
-
30
-    /**
31
-     * @param array  $props_n_values          incoming values
32
-     * @param string $timezone                incoming timezone
33
-     *                                        (if not set the timezone set for the website will be used.)
34
-     * @param array  $date_formats            incoming date_formats in an array where the first value is the
35
-     *                                        date_format and the second value is the time format
36
-     * @return EE_Transaction
37
-     * @throws EE_Error
38
-     * @throws InvalidArgumentException
39
-     * @throws InvalidDataTypeException
40
-     * @throws InvalidInterfaceException
41
-     * @throws ReflectionException
42
-     */
43
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
44
-    {
45
-        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
46
-        $txn = $has_object
47
-            ? $has_object
48
-            : new self($props_n_values, false, $timezone, $date_formats);
49
-        if (! $has_object) {
50
-            $txn->set_old_txn_status($txn->status_ID());
51
-        }
52
-        return $txn;
53
-    }
54
-
55
-
56
-    /**
57
-     * @param array  $props_n_values  incoming values from the database
58
-     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
59
-     *                                the website will be used.
60
-     * @return EE_Transaction
61
-     * @throws EE_Error
62
-     * @throws InvalidArgumentException
63
-     * @throws InvalidDataTypeException
64
-     * @throws InvalidInterfaceException
65
-     * @throws ReflectionException
66
-     */
67
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
68
-    {
69
-        $txn = new self($props_n_values, true, $timezone);
70
-        $txn->set_old_txn_status($txn->status_ID());
71
-        return $txn;
72
-    }
73
-
74
-
75
-    /**
76
-     * Sets a meta field indicating that this TXN is locked and should not be updated in the db.
77
-     * If a lock has already been set, then we will attempt to remove it in case it has expired.
78
-     * If that also fails, then an exception is thrown.
79
-     *
80
-     * @throws EE_Error
81
-     * @throws InvalidArgumentException
82
-     * @throws InvalidDataTypeException
83
-     * @throws InvalidInterfaceException
84
-     * @throws ReflectionException
85
-     */
86
-    public function lock()
87
-    {
88
-        // attempt to set lock, but if that fails...
89
-        if (! $this->add_extra_meta('lock', time(), true)) {
90
-            // then attempt to remove the lock in case it is expired
91
-            if ($this->_remove_expired_lock()) {
92
-                // if removal was successful, then try setting lock again
93
-                $this->lock();
94
-            } else {
95
-                // but if the lock can not be removed, then throw an exception
96
-                throw new EE_Error(
97
-                    sprintf(
98
-                        __(
99
-                            'Could not lock Transaction %1$d because it is already locked, meaning another part of the system is currently editing it. It should already be unlocked by the time you read this, so please refresh the page and try again.',
100
-                            'event_espresso'
101
-                        ),
102
-                        $this->ID()
103
-                    )
104
-                );
105
-            }
106
-        }
107
-    }
108
-
109
-
110
-    /**
111
-     * removes transaction lock applied in EE_Transaction::lock()
112
-     *
113
-     * @return int
114
-     * @throws EE_Error
115
-     * @throws InvalidArgumentException
116
-     * @throws InvalidDataTypeException
117
-     * @throws InvalidInterfaceException
118
-     * @throws ReflectionException
119
-     */
120
-    public function unlock()
121
-    {
122
-        return $this->delete_extra_meta('lock');
123
-    }
124
-
125
-
126
-    /**
127
-     * Decides whether or not now is the right time to update the transaction.
128
-     * This is useful because we don't always know if it is safe to update the transaction
129
-     * and its related data. why?
130
-     * because it's possible that the transaction is being used in another
131
-     * request and could overwrite anything we save.
132
-     * So we want to only update the txn once we know that won't happen.
133
-     * We also check that the lock isn't expired, and remove it if it is
134
-     *
135
-     * @return boolean
136
-     * @throws EE_Error
137
-     * @throws InvalidArgumentException
138
-     * @throws InvalidDataTypeException
139
-     * @throws InvalidInterfaceException
140
-     * @throws ReflectionException
141
-     */
142
-    public function is_locked()
143
-    {
144
-        // if TXN is not locked, then return false immediately
145
-        if (! $this->_get_lock()) {
146
-            return false;
147
-        }
148
-        // if not, then let's try and remove the lock in case it's expired...
149
-        // _remove_expired_lock() returns 0 when lock is valid (ie: removed = false)
150
-        // and a positive number if the lock was removed (ie: number of locks deleted),
151
-        // so we need to return the opposite
152
-        return ! $this->_remove_expired_lock() ? true : false;
153
-    }
154
-
155
-
156
-    /**
157
-     * Gets the meta field indicating that this TXN is locked
158
-     *
159
-     * @return int
160
-     * @throws EE_Error
161
-     * @throws InvalidArgumentException
162
-     * @throws InvalidDataTypeException
163
-     * @throws InvalidInterfaceException
164
-     * @throws ReflectionException
165
-     */
166
-    protected function _get_lock()
167
-    {
168
-        return (int) $this->get_extra_meta('lock', true, 0);
169
-    }
170
-
171
-
172
-    /**
173
-     * If the lock on this transaction is expired, then we want to remove it so that the transaction can be updated
174
-     *
175
-     * @return int
176
-     * @throws EE_Error
177
-     * @throws InvalidArgumentException
178
-     * @throws InvalidDataTypeException
179
-     * @throws InvalidInterfaceException
180
-     * @throws ReflectionException
181
-     */
182
-    protected function _remove_expired_lock()
183
-    {
184
-        $locked = $this->_get_lock();
185
-        if ($locked && time() - EE_Transaction::LOCK_EXPIRATION > $locked) {
186
-            return $this->unlock();
187
-        }
188
-        return 0;
189
-    }
190
-
191
-
192
-    /**
193
-     * Set transaction total
194
-     *
195
-     * @param float $total total value of transaction
196
-     * @throws EE_Error
197
-     * @throws InvalidArgumentException
198
-     * @throws InvalidDataTypeException
199
-     * @throws InvalidInterfaceException
200
-     * @throws ReflectionException
201
-     */
202
-    public function set_total($total = 0.00)
203
-    {
204
-        $this->set('TXN_total', (float) $total);
205
-    }
206
-
207
-
208
-    /**
209
-     * Set Total Amount Paid to Date
210
-     *
211
-     * @param float $total_paid total amount paid to date (sum of all payments)
212
-     * @throws EE_Error
213
-     * @throws InvalidArgumentException
214
-     * @throws InvalidDataTypeException
215
-     * @throws InvalidInterfaceException
216
-     * @throws ReflectionException
217
-     */
218
-    public function set_paid($total_paid = 0.00)
219
-    {
220
-        $this->set('TXN_paid', (float) $total_paid);
221
-    }
222
-
223
-
224
-    /**
225
-     * Set transaction status
226
-     *
227
-     * @param string $status        whether the transaction is open, declined, accepted,
228
-     *                              or any number of custom values that can be set
229
-     * @throws EE_Error
230
-     * @throws InvalidArgumentException
231
-     * @throws InvalidDataTypeException
232
-     * @throws InvalidInterfaceException
233
-     * @throws ReflectionException
234
-     */
235
-    public function set_status($status = '')
236
-    {
237
-        $this->set('STS_ID', $status);
238
-    }
239
-
240
-
241
-    /**
242
-     * Set hash salt
243
-     *
244
-     * @param string $hash_salt required for some payment gateways
245
-     * @throws EE_Error
246
-     * @throws InvalidArgumentException
247
-     * @throws InvalidDataTypeException
248
-     * @throws InvalidInterfaceException
249
-     * @throws ReflectionException
250
-     */
251
-    public function set_hash_salt($hash_salt = '')
252
-    {
253
-        $this->set('TXN_hash_salt', $hash_salt);
254
-    }
255
-
256
-
257
-    /**
258
-     * Sets TXN_reg_steps array
259
-     *
260
-     * @param array $txn_reg_steps
261
-     * @throws EE_Error
262
-     * @throws InvalidArgumentException
263
-     * @throws InvalidDataTypeException
264
-     * @throws InvalidInterfaceException
265
-     * @throws ReflectionException
266
-     */
267
-    public function set_reg_steps(array $txn_reg_steps)
268
-    {
269
-        $this->set('TXN_reg_steps', $txn_reg_steps);
270
-    }
271
-
272
-
273
-    /**
274
-     * Gets TXN_reg_steps
275
-     *
276
-     * @return array
277
-     * @throws EE_Error
278
-     * @throws InvalidArgumentException
279
-     * @throws InvalidDataTypeException
280
-     * @throws InvalidInterfaceException
281
-     * @throws ReflectionException
282
-     */
283
-    public function reg_steps()
284
-    {
285
-        $TXN_reg_steps = $this->get('TXN_reg_steps');
286
-        return is_array($TXN_reg_steps) ? (array) $TXN_reg_steps : array();
287
-    }
288
-
289
-
290
-    /**
291
-     * @return string of transaction's total cost, with currency symbol and decimal
292
-     * @throws EE_Error
293
-     * @throws InvalidArgumentException
294
-     * @throws InvalidDataTypeException
295
-     * @throws InvalidInterfaceException
296
-     * @throws ReflectionException
297
-     */
298
-    public function pretty_total()
299
-    {
300
-        return $this->get_pretty('TXN_total');
301
-    }
302
-
303
-
304
-    /**
305
-     * Gets the amount paid in a pretty string (formatted and with currency symbol)
306
-     *
307
-     * @return string
308
-     * @throws EE_Error
309
-     * @throws InvalidArgumentException
310
-     * @throws InvalidDataTypeException
311
-     * @throws InvalidInterfaceException
312
-     * @throws ReflectionException
313
-     */
314
-    public function pretty_paid()
315
-    {
316
-        return $this->get_pretty('TXN_paid');
317
-    }
318
-
319
-
320
-    /**
321
-     * calculate the amount remaining for this transaction and return;
322
-     *
323
-     * @return float amount remaining
324
-     * @throws EE_Error
325
-     * @throws InvalidArgumentException
326
-     * @throws InvalidDataTypeException
327
-     * @throws InvalidInterfaceException
328
-     * @throws ReflectionException
329
-     */
330
-    public function remaining()
331
-    {
332
-        return $this->total() - $this->paid();
333
-    }
334
-
335
-
336
-    /**
337
-     * get Transaction Total
338
-     *
339
-     * @return float
340
-     * @throws EE_Error
341
-     * @throws InvalidArgumentException
342
-     * @throws InvalidDataTypeException
343
-     * @throws InvalidInterfaceException
344
-     * @throws ReflectionException
345
-     */
346
-    public function total()
347
-    {
348
-        return (float) $this->get('TXN_total');
349
-    }
350
-
351
-
352
-    /**
353
-     * get Total Amount Paid to Date
354
-     *
355
-     * @return float
356
-     * @throws EE_Error
357
-     * @throws InvalidArgumentException
358
-     * @throws InvalidDataTypeException
359
-     * @throws InvalidInterfaceException
360
-     * @throws ReflectionException
361
-     */
362
-    public function paid()
363
-    {
364
-        return (float) $this->get('TXN_paid');
365
-    }
366
-
367
-
368
-    /**
369
-     * @return mixed|null
370
-     * @throws EE_Error
371
-     * @throws InvalidArgumentException
372
-     * @throws InvalidDataTypeException
373
-     * @throws InvalidInterfaceException
374
-     * @throws ReflectionException
375
-     */
376
-    public function get_cart_session()
377
-    {
378
-        $session_data = (array) $this->get('TXN_session_data');
379
-        return isset($session_data['cart']) && $session_data['cart'] instanceof EE_Cart
380
-            ? $session_data['cart']
381
-            : null;
382
-    }
383
-
384
-
385
-    /**
386
-     * get Transaction session data
387
-     *
388
-     * @return array|mixed
389
-     * @throws EE_Error
390
-     * @throws InvalidArgumentException
391
-     * @throws InvalidDataTypeException
392
-     * @throws InvalidInterfaceException
393
-     * @throws ReflectionException
394
-     */
395
-    public function session_data()
396
-    {
397
-        $session_data = $this->get('TXN_session_data');
398
-        if (empty($session_data)) {
399
-            $session_data = array(
400
-                'id'            => null,
401
-                'user_id'       => null,
402
-                'ip_address'    => null,
403
-                'user_agent'    => null,
404
-                'init_access'   => null,
405
-                'last_access'   => null,
406
-                'pages_visited' => array(),
407
-            );
408
-        }
409
-        return $session_data;
410
-    }
411
-
412
-
413
-    /**
414
-     * Set session data within the TXN object
415
-     *
416
-     * @param EE_Session|array $session_data
417
-     * @throws EE_Error
418
-     * @throws InvalidArgumentException
419
-     * @throws InvalidDataTypeException
420
-     * @throws InvalidInterfaceException
421
-     * @throws ReflectionException
422
-     */
423
-    public function set_txn_session_data($session_data)
424
-    {
425
-        if ($session_data instanceof EE_Session) {
426
-            $this->set('TXN_session_data', $session_data->get_session_data(null, true));
427
-        } else {
428
-            $this->set('TXN_session_data', $session_data);
429
-        }
430
-    }
431
-
432
-
433
-    /**
434
-     * get Transaction hash salt
435
-     *
436
-     * @return mixed
437
-     * @throws EE_Error
438
-     * @throws InvalidArgumentException
439
-     * @throws InvalidDataTypeException
440
-     * @throws InvalidInterfaceException
441
-     * @throws ReflectionException
442
-     */
443
-    public function hash_salt_()
444
-    {
445
-        return $this->get('TXN_hash_salt');
446
-    }
447
-
448
-
449
-    /**
450
-     * Returns the transaction datetime as either:
451
-     *            - unix timestamp format ($format = false, $gmt = true)
452
-     *            - formatted date string including the UTC (timezone) offset ($format = true ($gmt
453
-     *              has no affect with this option)), this also may include a timezone abbreviation if the
454
-     *              set timezone in this class differs from what the timezone is on the blog.
455
-     *            - formatted date string including the UTC (timezone) offset (default).
456
-     *
457
-     * @param boolean $format   - whether to return a unix timestamp (default) or formatted date string
458
-     * @param boolean $gmt      - whether to return a unix timestamp with UTC offset applied (default)
459
-     *                          or no UTC offset applied
460
-     * @return string | int
461
-     * @throws EE_Error
462
-     * @throws InvalidArgumentException
463
-     * @throws InvalidDataTypeException
464
-     * @throws InvalidInterfaceException
465
-     * @throws ReflectionException
466
-     */
467
-    public function datetime($format = false, $gmt = false)
468
-    {
469
-        if ($format) {
470
-            return $this->get_pretty('TXN_timestamp');
471
-        }
472
-        if ($gmt) {
473
-            return $this->get_raw('TXN_timestamp');
474
-        }
475
-        return $this->get('TXN_timestamp');
476
-    }
477
-
478
-
479
-    /**
480
-     * Gets registrations on this transaction
481
-     *
482
-     * @param array   $query_params array of query parameters
483
-     * @param boolean $get_cached   TRUE to retrieve cached registrations or FALSE to pull from the db
484
-     * @return EE_Base_Class[]|EE_Registration[]
485
-     * @throws EE_Error
486
-     * @throws InvalidArgumentException
487
-     * @throws InvalidDataTypeException
488
-     * @throws InvalidInterfaceException
489
-     * @throws ReflectionException
490
-     */
491
-    public function registrations($query_params = array(), $get_cached = false)
492
-    {
493
-        $query_params = (empty($query_params) || ! is_array($query_params))
494
-            ? array(
495
-                'order_by' => array(
496
-                    'Event.EVT_name'     => 'ASC',
497
-                    'Attendee.ATT_lname' => 'ASC',
498
-                    'Attendee.ATT_fname' => 'ASC',
499
-                ),
500
-            )
501
-            : $query_params;
502
-        $query_params = $get_cached ? array() : $query_params;
503
-        return $this->get_many_related('Registration', $query_params);
504
-    }
505
-
506
-
507
-    /**
508
-     * Gets all the attendees for this transaction (handy for use with EE_Attendee's get_registrations_for_event
509
-     * function for getting attendees and how many registrations they each have for an event)
510
-     *
511
-     * @return mixed EE_Attendee[] by default, int if $output is set to 'COUNT'
512
-     * @throws EE_Error
513
-     * @throws InvalidArgumentException
514
-     * @throws InvalidDataTypeException
515
-     * @throws InvalidInterfaceException
516
-     * @throws ReflectionException
517
-     */
518
-    public function attendees()
519
-    {
520
-        return $this->get_many_related('Attendee', array(array('Registration.Transaction.TXN_ID' => $this->ID())));
521
-    }
522
-
523
-
524
-    /**
525
-     * Gets payments for this transaction. Unlike other such functions, order by 'DESC' by default
526
-     *
527
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
528
-     * @return EE_Base_Class[]|EE_Payment[]
529
-     * @throws EE_Error
530
-     * @throws InvalidArgumentException
531
-     * @throws InvalidDataTypeException
532
-     * @throws InvalidInterfaceException
533
-     * @throws ReflectionException
534
-     */
535
-    public function payments($query_params = array())
536
-    {
537
-        return $this->get_many_related('Payment', $query_params);
538
-    }
539
-
540
-
541
-    /**
542
-     * gets only approved payments for this transaction
543
-     *
544
-     * @return EE_Base_Class[]|EE_Payment[]
545
-     * @throws EE_Error
546
-     * @throws InvalidArgumentException
547
-     * @throws ReflectionException
548
-     * @throws InvalidDataTypeException
549
-     * @throws InvalidInterfaceException
550
-     */
551
-    public function approved_payments()
552
-    {
553
-        EE_Registry::instance()->load_model('Payment');
554
-        return $this->get_many_related(
555
-            'Payment',
556
-            array(
557
-                array('STS_ID' => EEM_Payment::status_id_approved),
558
-                'order_by' => array('PAY_timestamp' => 'DESC'),
559
-            )
560
-        );
561
-    }
562
-
563
-
564
-    /**
565
-     * Gets all payments which have not been approved
566
-     *
567
-     * @return EE_Base_Class[]|EEI_Payment[]
568
-     * @throws EE_Error if a model is misconfigured somehow
569
-     * @throws InvalidArgumentException
570
-     * @throws InvalidDataTypeException
571
-     * @throws InvalidInterfaceException
572
-     * @throws ReflectionException
573
-     */
574
-    public function pending_payments()
575
-    {
576
-        return $this->get_many_related(
577
-            'Payment',
578
-            array(
579
-                array(
580
-                    'STS_ID' => EEM_Payment::status_id_pending,
581
-                ),
582
-                'order_by' => array(
583
-                    'PAY_timestamp' => 'DESC',
584
-                ),
585
-            )
586
-        );
587
-    }
588
-
589
-
590
-    /**
591
-     * echoes $this->pretty_status()
592
-     *
593
-     * @param bool $show_icons
594
-     * @throws EE_Error
595
-     * @throws InvalidArgumentException
596
-     * @throws InvalidDataTypeException
597
-     * @throws InvalidInterfaceException
598
-     * @throws ReflectionException
599
-     */
600
-    public function e_pretty_status($show_icons = false)
601
-    {
602
-        echo $this->pretty_status($show_icons);
603
-    }
604
-
605
-
606
-    /**
607
-     * returns a pretty version of the status, good for displaying to users
608
-     *
609
-     * @param bool $show_icons
610
-     * @return string
611
-     * @throws EE_Error
612
-     * @throws InvalidArgumentException
613
-     * @throws InvalidDataTypeException
614
-     * @throws InvalidInterfaceException
615
-     * @throws ReflectionException
616
-     */
617
-    public function pretty_status($show_icons = false)
618
-    {
619
-        $status = EEM_Status::instance()->localized_status(
620
-            array($this->status_ID() => __('unknown', 'event_espresso')),
621
-            false,
622
-            'sentence'
623
-        );
624
-        $icon = '';
625
-        switch ($this->status_ID()) {
626
-            case EEM_Transaction::complete_status_code:
627
-                $icon = $show_icons ? '<span class="dashicons dashicons-yes ee-icon-size-24 green-text"></span>' : '';
628
-                break;
629
-            case EEM_Transaction::incomplete_status_code:
630
-                $icon = $show_icons ? '<span class="dashicons dashicons-marker ee-icon-size-16 lt-blue-text"></span>'
631
-                    : '';
632
-                break;
633
-            case EEM_Transaction::abandoned_status_code:
634
-                $icon = $show_icons ? '<span class="dashicons dashicons-marker ee-icon-size-16 red-text"></span>' : '';
635
-                break;
636
-            case EEM_Transaction::failed_status_code:
637
-                $icon = $show_icons ? '<span class="dashicons dashicons-no ee-icon-size-16 red-text"></span>' : '';
638
-                break;
639
-            case EEM_Transaction::overpaid_status_code:
640
-                $icon = $show_icons ? '<span class="dashicons dashicons-plus ee-icon-size-16 orange-text"></span>' : '';
641
-                break;
642
-        }
643
-        return $icon . $status[ $this->status_ID() ];
644
-    }
645
-
646
-
647
-    /**
648
-     * get Transaction Status
649
-     *
650
-     * @return mixed
651
-     * @throws EE_Error
652
-     * @throws InvalidArgumentException
653
-     * @throws InvalidDataTypeException
654
-     * @throws InvalidInterfaceException
655
-     * @throws ReflectionException
656
-     */
657
-    public function status_ID()
658
-    {
659
-        return $this->get('STS_ID');
660
-    }
661
-
662
-
663
-    /**
664
-     * Returns TRUE or FALSE for whether or not this transaction cost any money
665
-     *
666
-     * @return boolean
667
-     * @throws EE_Error
668
-     * @throws InvalidArgumentException
669
-     * @throws InvalidDataTypeException
670
-     * @throws InvalidInterfaceException
671
-     * @throws ReflectionException
672
-     */
673
-    public function is_free()
674
-    {
675
-        return EEH_Money::compare_floats($this->get('TXN_total'), 0, '==');
676
-    }
677
-
678
-
679
-    /**
680
-     * Returns whether this transaction is complete
681
-     * Useful in templates and other logic for deciding if we should ask for another payment...
682
-     *
683
-     * @return boolean
684
-     * @throws EE_Error
685
-     * @throws InvalidArgumentException
686
-     * @throws InvalidDataTypeException
687
-     * @throws InvalidInterfaceException
688
-     * @throws ReflectionException
689
-     */
690
-    public function is_completed()
691
-    {
692
-        return $this->status_ID() === EEM_Transaction::complete_status_code;
693
-    }
694
-
695
-
696
-    /**
697
-     * Returns whether this transaction is incomplete
698
-     * Useful in templates and other logic for deciding if we should ask for another payment...
699
-     *
700
-     * @return boolean
701
-     * @throws EE_Error
702
-     * @throws InvalidArgumentException
703
-     * @throws InvalidDataTypeException
704
-     * @throws InvalidInterfaceException
705
-     * @throws ReflectionException
706
-     */
707
-    public function is_incomplete()
708
-    {
709
-        return $this->status_ID() === EEM_Transaction::incomplete_status_code;
710
-    }
711
-
712
-
713
-    /**
714
-     * Returns whether this transaction is overpaid
715
-     * Useful in templates and other logic for deciding if monies need to be refunded
716
-     *
717
-     * @return boolean
718
-     * @throws EE_Error
719
-     * @throws InvalidArgumentException
720
-     * @throws InvalidDataTypeException
721
-     * @throws InvalidInterfaceException
722
-     * @throws ReflectionException
723
-     */
724
-    public function is_overpaid()
725
-    {
726
-        return $this->status_ID() === EEM_Transaction::overpaid_status_code;
727
-    }
728
-
729
-
730
-    /**
731
-     * Returns whether this transaction was abandoned
732
-     * meaning that the transaction/registration process was somehow interrupted and never completed
733
-     * but that contact information exists for at least one registrant
734
-     *
735
-     * @return boolean
736
-     * @throws EE_Error
737
-     * @throws InvalidArgumentException
738
-     * @throws InvalidDataTypeException
739
-     * @throws InvalidInterfaceException
740
-     * @throws ReflectionException
741
-     */
742
-    public function is_abandoned()
743
-    {
744
-        return $this->status_ID() === EEM_Transaction::abandoned_status_code;
745
-    }
746
-
747
-
748
-    /**
749
-     * Returns whether this transaction failed
750
-     * meaning that the transaction/registration process was somehow interrupted and never completed
751
-     * and that NO contact information exists for any registrants
752
-     *
753
-     * @return boolean
754
-     * @throws EE_Error
755
-     * @throws InvalidArgumentException
756
-     * @throws InvalidDataTypeException
757
-     * @throws InvalidInterfaceException
758
-     * @throws ReflectionException
759
-     */
760
-    public function failed()
761
-    {
762
-        return $this->status_ID() === EEM_Transaction::failed_status_code;
763
-    }
764
-
765
-
766
-    /**
767
-     * This returns the url for the invoice of this transaction
768
-     *
769
-     * @param string $type 'html' or 'pdf' (default is pdf)
770
-     * @return string
771
-     * @throws EE_Error
772
-     * @throws InvalidArgumentException
773
-     * @throws InvalidDataTypeException
774
-     * @throws InvalidInterfaceException
775
-     * @throws ReflectionException
776
-     */
777
-    public function invoice_url($type = 'html')
778
-    {
779
-        $REG = $this->primary_registration();
780
-        if (! $REG instanceof EE_Registration) {
781
-            return '';
782
-        }
783
-        return $REG->invoice_url($type);
784
-    }
785
-
786
-
787
-    /**
788
-     * Gets the primary registration only
789
-     *
790
-     * @return EE_Base_Class|EE_Registration
791
-     * @throws EE_Error
792
-     * @throws InvalidArgumentException
793
-     * @throws InvalidDataTypeException
794
-     * @throws InvalidInterfaceException
795
-     * @throws ReflectionException
796
-     */
797
-    public function primary_registration()
798
-    {
799
-        $registrations = (array) $this->get_many_related(
800
-            'Registration',
801
-            array(array('REG_count' => EEM_Registration::PRIMARY_REGISTRANT_COUNT))
802
-        );
803
-        foreach ($registrations as $registration) {
804
-            // valid registration that is NOT cancelled or declined ?
805
-            if ($registration instanceof EE_Registration
806
-                && ! in_array($registration->status_ID(), EEM_Registration::closed_reg_statuses(), true)
807
-            ) {
808
-                return $registration;
809
-            }
810
-        }
811
-        // nothing valid found, so just return first thing from array of results
812
-        return reset($registrations);
813
-    }
814
-
815
-
816
-    /**
817
-     * Gets the URL for viewing the receipt
818
-     *
819
-     * @param string $type 'pdf' or 'html' (default is 'html')
820
-     * @return string
821
-     * @throws EE_Error
822
-     * @throws InvalidArgumentException
823
-     * @throws InvalidDataTypeException
824
-     * @throws InvalidInterfaceException
825
-     * @throws ReflectionException
826
-     */
827
-    public function receipt_url($type = 'html')
828
-    {
829
-        $REG = $this->primary_registration();
830
-        if (! $REG instanceof EE_Registration) {
831
-            return '';
832
-        }
833
-        return $REG->receipt_url($type);
834
-    }
835
-
836
-
837
-    /**
838
-     * Gets the URL of the thank you page with this registration REG_url_link added as
839
-     * a query parameter
840
-     *
841
-     * @return string
842
-     * @throws EE_Error
843
-     * @throws InvalidArgumentException
844
-     * @throws InvalidDataTypeException
845
-     * @throws InvalidInterfaceException
846
-     * @throws ReflectionException
847
-     */
848
-    public function payment_overview_url()
849
-    {
850
-        $primary_registration = $this->primary_registration();
851
-        return $primary_registration instanceof EE_Registration ? $primary_registration->payment_overview_url() : false;
852
-    }
853
-
854
-
855
-    /**
856
-     * @return string
857
-     * @throws EE_Error
858
-     * @throws InvalidArgumentException
859
-     * @throws InvalidDataTypeException
860
-     * @throws InvalidInterfaceException
861
-     * @throws ReflectionException
862
-     */
863
-    public function gateway_response_on_transaction()
864
-    {
865
-        $payment = $this->get_first_related('Payment');
866
-        return $payment instanceof EE_Payment ? $payment->gateway_response() : '';
867
-    }
868
-
869
-
870
-    /**
871
-     * Get the status object of this object
872
-     *
873
-     * @return EE_Base_Class|EE_Status
874
-     * @throws EE_Error
875
-     * @throws InvalidArgumentException
876
-     * @throws InvalidDataTypeException
877
-     * @throws InvalidInterfaceException
878
-     * @throws ReflectionException
879
-     */
880
-    public function status_obj()
881
-    {
882
-        return $this->get_first_related('Status');
883
-    }
884
-
885
-
886
-    /**
887
-     * Gets all the extra meta info on this payment
888
-     *
889
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
890
-     * @return EE_Base_Class[]|EE_Extra_Meta
891
-     * @throws EE_Error
892
-     * @throws InvalidArgumentException
893
-     * @throws InvalidDataTypeException
894
-     * @throws InvalidInterfaceException
895
-     * @throws ReflectionException
896
-     */
897
-    public function extra_meta($query_params = array())
898
-    {
899
-        return $this->get_many_related('Extra_Meta', $query_params);
900
-    }
901
-
902
-
903
-    /**
904
-     * Wrapper for _add_relation_to
905
-     *
906
-     * @param EE_Registration $registration
907
-     * @return EE_Base_Class the relation was added to
908
-     * @throws EE_Error
909
-     * @throws InvalidArgumentException
910
-     * @throws InvalidDataTypeException
911
-     * @throws InvalidInterfaceException
912
-     * @throws ReflectionException
913
-     */
914
-    public function add_registration(EE_Registration $registration)
915
-    {
916
-        return $this->_add_relation_to($registration, 'Registration');
917
-    }
918
-
919
-
920
-    /**
921
-     * Removes the given registration from being related (even before saving this transaction).
922
-     * If an ID/index is provided and this transaction isn't saved yet, removes it from list of cached relations
923
-     *
924
-     * @param int $registration_or_id
925
-     * @return EE_Base_Class that was removed from being related
926
-     * @throws EE_Error
927
-     * @throws InvalidArgumentException
928
-     * @throws InvalidDataTypeException
929
-     * @throws InvalidInterfaceException
930
-     * @throws ReflectionException
931
-     */
932
-    public function remove_registration_with_id($registration_or_id)
933
-    {
934
-        return $this->_remove_relation_to($registration_or_id, 'Registration');
935
-    }
936
-
937
-
938
-    /**
939
-     * Gets all the line items which are for ACTUAL items
940
-     *
941
-     * @return EE_Line_Item[]
942
-     * @throws EE_Error
943
-     * @throws InvalidArgumentException
944
-     * @throws InvalidDataTypeException
945
-     * @throws InvalidInterfaceException
946
-     * @throws ReflectionException
947
-     */
948
-    public function items_purchased()
949
-    {
950
-        return $this->line_items(array(array('LIN_type' => EEM_Line_Item::type_line_item)));
951
-    }
952
-
953
-
954
-    /**
955
-     * Wrapper for _add_relation_to
956
-     *
957
-     * @param EE_Line_Item $line_item
958
-     * @return EE_Base_Class the relation was added to
959
-     * @throws EE_Error
960
-     * @throws InvalidArgumentException
961
-     * @throws InvalidDataTypeException
962
-     * @throws InvalidInterfaceException
963
-     * @throws ReflectionException
964
-     */
965
-    public function add_line_item(EE_Line_Item $line_item)
966
-    {
967
-        return $this->_add_relation_to($line_item, 'Line_Item');
968
-    }
969
-
970
-
971
-    /**
972
-     * Gets ALL the line items related to this transaction (unstructured)
973
-     *
974
-     * @param array $query_params
975
-     * @return EE_Base_Class[]|EE_Line_Item[]
976
-     * @throws EE_Error
977
-     * @throws InvalidArgumentException
978
-     * @throws InvalidDataTypeException
979
-     * @throws InvalidInterfaceException
980
-     * @throws ReflectionException
981
-     */
982
-    public function line_items($query_params = array())
983
-    {
984
-        return $this->get_many_related('Line_Item', $query_params);
985
-    }
986
-
987
-
988
-    /**
989
-     * Gets all the line items which are taxes on the total
990
-     *
991
-     * @return EE_Line_Item[]
992
-     * @throws EE_Error
993
-     * @throws InvalidArgumentException
994
-     * @throws InvalidDataTypeException
995
-     * @throws InvalidInterfaceException
996
-     * @throws ReflectionException
997
-     */
998
-    public function tax_items()
999
-    {
1000
-        return $this->line_items(array(array('LIN_type' => EEM_Line_Item::type_tax)));
1001
-    }
1002
-
1003
-
1004
-    /**
1005
-     * Gets the total line item (which is a parent of all other related line items,
1006
-     * meaning it takes them all into account on its total)
1007
-     *
1008
-     * @param bool $create_if_not_found
1009
-     * @return \EE_Line_Item
1010
-     * @throws EE_Error
1011
-     * @throws InvalidArgumentException
1012
-     * @throws InvalidDataTypeException
1013
-     * @throws InvalidInterfaceException
1014
-     * @throws ReflectionException
1015
-     */
1016
-    public function total_line_item($create_if_not_found = true)
1017
-    {
1018
-        $item = $this->get_first_related('Line_Item', array(array('LIN_type' => EEM_Line_Item::type_total)));
1019
-        if (! $item && $create_if_not_found) {
1020
-            $item = EEH_Line_Item::create_total_line_item($this);
1021
-        }
1022
-        return $item;
1023
-    }
1024
-
1025
-
1026
-    /**
1027
-     * Returns the total amount of tax on this transaction
1028
-     * (assumes there's only one tax subtotal line item)
1029
-     *
1030
-     * @return float
1031
-     * @throws EE_Error
1032
-     * @throws InvalidArgumentException
1033
-     * @throws InvalidDataTypeException
1034
-     * @throws InvalidInterfaceException
1035
-     * @throws ReflectionException
1036
-     */
1037
-    public function tax_total()
1038
-    {
1039
-        $tax_line_item = $this->tax_total_line_item();
1040
-        if ($tax_line_item) {
1041
-            return (float) $tax_line_item->total();
1042
-        }
1043
-        return (float) 0;
1044
-    }
1045
-
1046
-
1047
-    /**
1048
-     * Gets the tax subtotal line item (assumes there's only one)
1049
-     *
1050
-     * @return EE_Line_Item
1051
-     * @throws EE_Error
1052
-     * @throws InvalidArgumentException
1053
-     * @throws InvalidDataTypeException
1054
-     * @throws InvalidInterfaceException
1055
-     * @throws ReflectionException
1056
-     */
1057
-    public function tax_total_line_item()
1058
-    {
1059
-        return EEH_Line_Item::get_taxes_subtotal($this->total_line_item());
1060
-    }
1061
-
1062
-
1063
-    /**
1064
-     * Gets the array of billing info for the gateway and for this transaction's primary registration's attendee.
1065
-     *
1066
-     * @return EE_Form_Section_Proper
1067
-     * @throws EE_Error
1068
-     * @throws InvalidArgumentException
1069
-     * @throws InvalidDataTypeException
1070
-     * @throws InvalidInterfaceException
1071
-     * @throws ReflectionException
1072
-     */
1073
-    public function billing_info()
1074
-    {
1075
-        $payment_method = $this->payment_method();
1076
-        if (! $payment_method) {
1077
-            EE_Error::add_error(
1078
-                __(
1079
-                    'Could not find billing info for transaction because no gateway has been used for it yet',
1080
-                    'event_espresso'
1081
-                ),
1082
-                __FILE__,
1083
-                __FUNCTION__,
1084
-                __LINE__
1085
-            );
1086
-            return null;
1087
-        }
1088
-        $primary_reg = $this->primary_registration();
1089
-        if (! $primary_reg) {
1090
-            EE_Error::add_error(
1091
-                __(
1092
-                    'Cannot get billing info for gateway %s on transaction because no primary registration exists',
1093
-                    'event_espresso'
1094
-                ),
1095
-                __FILE__,
1096
-                __FUNCTION__,
1097
-                __LINE__
1098
-            );
1099
-            return null;
1100
-        }
1101
-        $attendee = $primary_reg->attendee();
1102
-        if (! $attendee) {
1103
-            EE_Error::add_error(
1104
-                __(
1105
-                    'Cannot get billing info for gateway %s on transaction because the primary registration has no attendee exists',
1106
-                    'event_espresso'
1107
-                ),
1108
-                __FILE__,
1109
-                __FUNCTION__,
1110
-                __LINE__
1111
-            );
1112
-            return null;
1113
-        }
1114
-        return $attendee->billing_info_for_payment_method($payment_method);
1115
-    }
1116
-
1117
-
1118
-    /**
1119
-     * Gets PMD_ID
1120
-     *
1121
-     * @return int
1122
-     * @throws EE_Error
1123
-     * @throws InvalidArgumentException
1124
-     * @throws InvalidDataTypeException
1125
-     * @throws InvalidInterfaceException
1126
-     * @throws ReflectionException
1127
-     */
1128
-    public function payment_method_ID()
1129
-    {
1130
-        return $this->get('PMD_ID');
1131
-    }
1132
-
1133
-
1134
-    /**
1135
-     * Sets PMD_ID
1136
-     *
1137
-     * @param int $PMD_ID
1138
-     * @throws EE_Error
1139
-     * @throws InvalidArgumentException
1140
-     * @throws InvalidDataTypeException
1141
-     * @throws InvalidInterfaceException
1142
-     * @throws ReflectionException
1143
-     */
1144
-    public function set_payment_method_ID($PMD_ID)
1145
-    {
1146
-        $this->set('PMD_ID', $PMD_ID);
1147
-    }
1148
-
1149
-
1150
-    /**
1151
-     * Gets the last-used payment method on this transaction
1152
-     * (we COULD just use the last-made payment, but some payment methods, namely
1153
-     * offline ones, dont' create payments)
1154
-     *
1155
-     * @return EE_Payment_Method
1156
-     * @throws EE_Error
1157
-     * @throws InvalidArgumentException
1158
-     * @throws InvalidDataTypeException
1159
-     * @throws InvalidInterfaceException
1160
-     * @throws ReflectionException
1161
-     */
1162
-    public function payment_method()
1163
-    {
1164
-        $pm = $this->get_first_related('Payment_Method');
1165
-        if ($pm instanceof EE_Payment_Method) {
1166
-            return $pm;
1167
-        }
1168
-        $last_payment = $this->last_payment();
1169
-        if ($last_payment instanceof EE_Payment && $last_payment->payment_method()) {
1170
-            return $last_payment->payment_method();
1171
-        }
1172
-        return null;
1173
-    }
1174
-
1175
-
1176
-    /**
1177
-     * Gets the last payment made
1178
-     *
1179
-     * @return EE_Base_Class|EE_Payment
1180
-     * @throws EE_Error
1181
-     * @throws InvalidArgumentException
1182
-     * @throws InvalidDataTypeException
1183
-     * @throws InvalidInterfaceException
1184
-     * @throws ReflectionException
1185
-     */
1186
-    public function last_payment()
1187
-    {
1188
-        return $this->get_first_related('Payment', array('order_by' => array('PAY_ID' => 'desc')));
1189
-    }
1190
-
1191
-
1192
-    /**
1193
-     * Gets all the line items which are unrelated to tickets on this transaction
1194
-     *
1195
-     * @return EE_Line_Item[]
1196
-     * @throws EE_Error
1197
-     * @throws InvalidArgumentException
1198
-     * @throws InvalidDataTypeException
1199
-     * @throws InvalidInterfaceException
1200
-     * @throws ReflectionException
1201
-     */
1202
-    public function non_ticket_line_items()
1203
-    {
1204
-        return EEM_Line_Item::instance()->get_all_non_ticket_line_items_for_transaction($this->ID());
1205
-    }
1206
-
1207
-
1208
-    /**
1209
-     * possibly toggles TXN status
1210
-     *
1211
-     * @param  boolean $update whether to save the TXN
1212
-     * @return bool whether the TXN was saved
1213
-     * @throws EE_Error
1214
-     * @throws InvalidArgumentException
1215
-     * @throws InvalidDataTypeException
1216
-     * @throws InvalidInterfaceException
1217
-     * @throws ReflectionException
1218
-     * @throws RuntimeException
1219
-     */
1220
-    public function update_status_based_on_total_paid($update = true)
1221
-    {
1222
-        // set transaction status based on comparison of TXN_paid vs TXN_total
1223
-        if (EEH_Money::compare_floats($this->paid(), $this->total(), '>')) {
1224
-            $new_txn_status = EEM_Transaction::overpaid_status_code;
1225
-        } elseif (EEH_Money::compare_floats($this->paid(), $this->total())) {
1226
-            $new_txn_status = EEM_Transaction::complete_status_code;
1227
-        } elseif (EEH_Money::compare_floats($this->paid(), $this->total(), '<')) {
1228
-            $new_txn_status = EEM_Transaction::incomplete_status_code;
1229
-        } else {
1230
-            throw new RuntimeException(
1231
-                __('The total paid calculation for this transaction is inaccurate.', 'event_espresso')
1232
-            );
1233
-        }
1234
-        if ($new_txn_status !== $this->status_ID()) {
1235
-            $this->set_status($new_txn_status);
1236
-            if ($update) {
1237
-                return $this->save() ? true : false;
1238
-            }
1239
-        }
1240
-        return false;
1241
-    }
1242
-
1243
-
1244
-    /**
1245
-     * Updates the transaction's status and total_paid based on all the payments
1246
-     * that apply to it
1247
-     *
1248
-     * @deprecated
1249
-     * @return array|bool
1250
-     * @throws EE_Error
1251
-     * @throws InvalidArgumentException
1252
-     * @throws ReflectionException
1253
-     * @throws InvalidDataTypeException
1254
-     * @throws InvalidInterfaceException
1255
-     */
1256
-    public function update_based_on_payments()
1257
-    {
1258
-        EE_Error::doing_it_wrong(
1259
-            __CLASS__ . '::' . __FUNCTION__,
1260
-            sprintf(
1261
-                __('This method is deprecated. Please use "%s" instead', 'event_espresso'),
1262
-                'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()'
1263
-            ),
1264
-            '4.6.0'
1265
-        );
1266
-        /** @type EE_Transaction_Processor $transaction_processor */
1267
-        $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
1268
-        return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment($this);
1269
-    }
1270
-
1271
-
1272
-    /**
1273
-     * @return string
1274
-     */
1275
-    public function old_txn_status()
1276
-    {
1277
-        return $this->_old_txn_status;
1278
-    }
1279
-
1280
-
1281
-    /**
1282
-     * @param string $old_txn_status
1283
-     */
1284
-    public function set_old_txn_status($old_txn_status)
1285
-    {
1286
-        // only set the first time
1287
-        if ($this->_old_txn_status === null) {
1288
-            $this->_old_txn_status = $old_txn_status;
1289
-        }
1290
-    }
1291
-
1292
-
1293
-    /**
1294
-     * reg_status_updated
1295
-     *
1296
-     * @return bool
1297
-     * @throws EE_Error
1298
-     * @throws InvalidArgumentException
1299
-     * @throws InvalidDataTypeException
1300
-     * @throws InvalidInterfaceException
1301
-     * @throws ReflectionException
1302
-     */
1303
-    public function txn_status_updated()
1304
-    {
1305
-        return $this->status_ID() !== $this->_old_txn_status && $this->_old_txn_status !== null;
1306
-    }
1307
-
1308
-
1309
-    /**
1310
-     * _reg_steps_completed
1311
-     * if $check_all is TRUE, then returns TRUE if ALL reg steps have been marked as completed,
1312
-     * if a $reg_step_slug is provided, then this step will be skipped when testing for completion
1313
-     * if $check_all is FALSE and a $reg_step_slug is provided, then ONLY that reg step will be tested for completion
1314
-     *
1315
-     * @param string $reg_step_slug
1316
-     * @param bool   $check_all
1317
-     * @return bool|int
1318
-     * @throws EE_Error
1319
-     * @throws InvalidArgumentException
1320
-     * @throws InvalidDataTypeException
1321
-     * @throws InvalidInterfaceException
1322
-     * @throws ReflectionException
1323
-     */
1324
-    private function _reg_steps_completed($reg_step_slug = '', $check_all = true)
1325
-    {
1326
-        $reg_steps = $this->reg_steps();
1327
-        if (! is_array($reg_steps) || empty($reg_steps)) {
1328
-            return false;
1329
-        }
1330
-        // loop thru reg steps array)
1331
-        foreach ($reg_steps as $slug => $reg_step_completed) {
1332
-            // if NOT checking ALL steps (only checking one step)
1333
-            if (! $check_all) {
1334
-                // and this is the one
1335
-                if ($slug === $reg_step_slug) {
1336
-                    return $reg_step_completed;
1337
-                }
1338
-                // skip to next reg step in loop
1339
-                continue;
1340
-            }
1341
-            // $check_all must be true, else we would never have gotten to this point
1342
-            if ($slug === $reg_step_slug) {
1343
-                // if we reach this point, then we are testing either:
1344
-                // all_reg_steps_completed_except() or
1345
-                // all_reg_steps_completed_except_final_step(),
1346
-                // and since this is the reg step EXCEPTION being tested
1347
-                // we want to return true (yes true) if this reg step is NOT completed
1348
-                // ie: "is everything completed except the final step?"
1349
-                // "that is correct... the final step is not completed, but all others are."
1350
-                return $reg_step_completed !== true;
1351
-            }
1352
-            if ($reg_step_completed !== true) {
1353
-                // if any reg step is NOT completed, then ALL steps are not completed
1354
-                return false;
1355
-            }
1356
-        }
1357
-        return true;
1358
-    }
1359
-
1360
-
1361
-    /**
1362
-     * all_reg_steps_completed
1363
-     * returns:
1364
-     *    true if ALL reg steps have been marked as completed
1365
-     *        or false if any step is not completed
1366
-     *
1367
-     * @return bool
1368
-     * @throws EE_Error
1369
-     * @throws InvalidArgumentException
1370
-     * @throws InvalidDataTypeException
1371
-     * @throws InvalidInterfaceException
1372
-     * @throws ReflectionException
1373
-     */
1374
-    public function all_reg_steps_completed()
1375
-    {
1376
-        return $this->_reg_steps_completed();
1377
-    }
1378
-
1379
-
1380
-    /**
1381
-     * all_reg_steps_completed_except
1382
-     * returns:
1383
-     *        true if ALL reg steps, except a particular step that you wish to skip over, have been marked as completed
1384
-     *        or false if any other step is not completed
1385
-     *        or false if ALL steps are completed including the exception you are testing !!!
1386
-     *
1387
-     * @param string $exception
1388
-     * @return bool
1389
-     * @throws EE_Error
1390
-     * @throws InvalidArgumentException
1391
-     * @throws InvalidDataTypeException
1392
-     * @throws InvalidInterfaceException
1393
-     * @throws ReflectionException
1394
-     */
1395
-    public function all_reg_steps_completed_except($exception = '')
1396
-    {
1397
-        return $this->_reg_steps_completed($exception);
1398
-    }
1399
-
1400
-
1401
-    /**
1402
-     * all_reg_steps_completed_except
1403
-     * returns:
1404
-     *        true if ALL reg steps, except the final step, have been marked as completed
1405
-     *        or false if any step is not completed
1406
-     *    or false if ALL steps are completed including the final step !!!
1407
-     *
1408
-     * @return bool
1409
-     * @throws EE_Error
1410
-     * @throws InvalidArgumentException
1411
-     * @throws InvalidDataTypeException
1412
-     * @throws InvalidInterfaceException
1413
-     * @throws ReflectionException
1414
-     */
1415
-    public function all_reg_steps_completed_except_final_step()
1416
-    {
1417
-        return $this->_reg_steps_completed('finalize_registration');
1418
-    }
1419
-
1420
-
1421
-    /**
1422
-     * reg_step_completed
1423
-     * returns:
1424
-     *    true if a specific reg step has been marked as completed
1425
-     *    a Unix timestamp if it has been initialized but not yet completed,
1426
-     *    or false if it has not yet been initialized
1427
-     *
1428
-     * @param string $reg_step_slug
1429
-     * @return bool|int
1430
-     * @throws EE_Error
1431
-     * @throws InvalidArgumentException
1432
-     * @throws InvalidDataTypeException
1433
-     * @throws InvalidInterfaceException
1434
-     * @throws ReflectionException
1435
-     */
1436
-    public function reg_step_completed($reg_step_slug)
1437
-    {
1438
-        return $this->_reg_steps_completed($reg_step_slug, false);
1439
-    }
1440
-
1441
-
1442
-    /**
1443
-     * completed_final_reg_step
1444
-     * returns:
1445
-     *    true if the finalize_registration reg step has been marked as completed
1446
-     *    a Unix timestamp if it has been initialized but not yet completed,
1447
-     *    or false if it has not yet been initialized
1448
-     *
1449
-     * @return bool|int
1450
-     * @throws EE_Error
1451
-     * @throws InvalidArgumentException
1452
-     * @throws InvalidDataTypeException
1453
-     * @throws InvalidInterfaceException
1454
-     * @throws ReflectionException
1455
-     */
1456
-    public function final_reg_step_completed()
1457
-    {
1458
-        return $this->_reg_steps_completed('finalize_registration', false);
1459
-    }
1460
-
1461
-
1462
-    /**
1463
-     * set_reg_step_initiated
1464
-     * given a valid TXN_reg_step, this sets it's value to a unix timestamp
1465
-     *
1466
-     * @param string $reg_step_slug
1467
-     * @return boolean
1468
-     * @throws EE_Error
1469
-     * @throws InvalidArgumentException
1470
-     * @throws InvalidDataTypeException
1471
-     * @throws InvalidInterfaceException
1472
-     * @throws ReflectionException
1473
-     */
1474
-    public function set_reg_step_initiated($reg_step_slug)
1475
-    {
1476
-        return $this->_set_reg_step_completed_status($reg_step_slug, time());
1477
-    }
1478
-
1479
-
1480
-    /**
1481
-     * set_reg_step_completed
1482
-     * given a valid TXN_reg_step, this sets the step as completed
1483
-     *
1484
-     * @param string $reg_step_slug
1485
-     * @return boolean
1486
-     * @throws EE_Error
1487
-     * @throws InvalidArgumentException
1488
-     * @throws InvalidDataTypeException
1489
-     * @throws InvalidInterfaceException
1490
-     * @throws ReflectionException
1491
-     */
1492
-    public function set_reg_step_completed($reg_step_slug)
1493
-    {
1494
-        return $this->_set_reg_step_completed_status($reg_step_slug, true);
1495
-    }
1496
-
1497
-
1498
-    /**
1499
-     * set_reg_step_completed
1500
-     * given a valid TXN_reg_step slug, this sets the step as NOT completed
1501
-     *
1502
-     * @param string $reg_step_slug
1503
-     * @return boolean
1504
-     * @throws EE_Error
1505
-     * @throws InvalidArgumentException
1506
-     * @throws InvalidDataTypeException
1507
-     * @throws InvalidInterfaceException
1508
-     * @throws ReflectionException
1509
-     */
1510
-    public function set_reg_step_not_completed($reg_step_slug)
1511
-    {
1512
-        return $this->_set_reg_step_completed_status($reg_step_slug, false);
1513
-    }
1514
-
1515
-
1516
-    /**
1517
-     * set_reg_step_completed
1518
-     * given a valid reg step slug, this sets the TXN_reg_step completed status which is either:
1519
-     *
1520
-     * @param  string      $reg_step_slug
1521
-     * @param  boolean|int $status
1522
-     * @return boolean
1523
-     * @throws EE_Error
1524
-     * @throws InvalidArgumentException
1525
-     * @throws InvalidDataTypeException
1526
-     * @throws InvalidInterfaceException
1527
-     * @throws ReflectionException
1528
-     */
1529
-    private function _set_reg_step_completed_status($reg_step_slug, $status)
1530
-    {
1531
-        // validate status
1532
-        $status = is_bool($status) || is_int($status) ? $status : false;
1533
-        // get reg steps array
1534
-        $txn_reg_steps = $this->reg_steps();
1535
-        // if reg step does NOT exist
1536
-        if (! isset($txn_reg_steps[ $reg_step_slug ])) {
1537
-            return false;
1538
-        }
1539
-        // if  we're trying to complete a step that is already completed
1540
-        if ($txn_reg_steps[ $reg_step_slug ] === true) {
1541
-            return true;
1542
-        }
1543
-        // if  we're trying to complete a step that hasn't even started
1544
-        if ($status === true && $txn_reg_steps[ $reg_step_slug ] === false) {
1545
-            return false;
1546
-        }
1547
-        // if current status value matches the incoming value (no change)
1548
-        // type casting as int means values should collapse to either 0, 1, or a timestamp like 1234567890
1549
-        if ((int) $txn_reg_steps[ $reg_step_slug ] === (int) $status) {
1550
-            // this will happen in cases where multiple AJAX requests occur during the same step
1551
-            return true;
1552
-        }
1553
-        // if we're trying to set a start time, but it has already been set...
1554
-        if (is_numeric($status) && is_numeric($txn_reg_steps[ $reg_step_slug ])) {
1555
-            // skip the update below, but don't return FALSE so that errors won't be displayed
1556
-            return true;
1557
-        }
1558
-        // update completed status
1559
-        $txn_reg_steps[ $reg_step_slug ] = $status;
1560
-        $this->set_reg_steps($txn_reg_steps);
1561
-        $this->save();
1562
-        return true;
1563
-    }
1564
-
1565
-
1566
-    /**
1567
-     * remove_reg_step
1568
-     * given a valid TXN_reg_step slug, this will remove (unset)
1569
-     * the reg step from the TXN reg step array
1570
-     *
1571
-     * @param string $reg_step_slug
1572
-     * @return void
1573
-     * @throws EE_Error
1574
-     * @throws InvalidArgumentException
1575
-     * @throws InvalidDataTypeException
1576
-     * @throws InvalidInterfaceException
1577
-     * @throws ReflectionException
1578
-     */
1579
-    public function remove_reg_step($reg_step_slug)
1580
-    {
1581
-        // get reg steps array
1582
-        $txn_reg_steps = $this->reg_steps();
1583
-        unset($txn_reg_steps[ $reg_step_slug ]);
1584
-        $this->set_reg_steps($txn_reg_steps);
1585
-    }
1586
-
1587
-
1588
-    /**
1589
-     * toggle_failed_transaction_status
1590
-     * upgrades a TXNs status from failed to abandoned,
1591
-     * meaning that contact information has been captured for at least one registrant
1592
-     *
1593
-     * @param bool $save
1594
-     * @return bool
1595
-     * @throws EE_Error
1596
-     * @throws InvalidArgumentException
1597
-     * @throws InvalidDataTypeException
1598
-     * @throws InvalidInterfaceException
1599
-     * @throws ReflectionException
1600
-     */
1601
-    public function toggle_failed_transaction_status($save = true)
1602
-    {
1603
-        // if TXN status is still set as "failed"...
1604
-        if ($this->status_ID() === EEM_Transaction::failed_status_code) {
1605
-            $this->set_status(EEM_Transaction::abandoned_status_code);
1606
-            if ($save) {
1607
-                $this->save();
1608
-            }
1609
-            return true;
1610
-        }
1611
-        return false;
1612
-    }
1613
-
1614
-
1615
-    /**
1616
-     * toggle_abandoned_transaction_status
1617
-     * upgrades a TXNs status from failed or abandoned to incomplete
1618
-     *
1619
-     * @return bool
1620
-     * @throws EE_Error
1621
-     * @throws InvalidArgumentException
1622
-     * @throws InvalidDataTypeException
1623
-     * @throws InvalidInterfaceException
1624
-     * @throws ReflectionException
1625
-     */
1626
-    public function toggle_abandoned_transaction_status()
1627
-    {
1628
-        // if TXN status has not been updated already due to a payment, and is still set as "failed" or "abandoned"...
1629
-        $txn_status = $this->status_ID();
1630
-        if ($txn_status === EEM_Transaction::failed_status_code
1631
-            || $txn_status === EEM_Transaction::abandoned_status_code
1632
-        ) {
1633
-            // if a contact record for the primary registrant has been created
1634
-            if ($this->primary_registration() instanceof EE_Registration
1635
-                && $this->primary_registration()->attendee() instanceof EE_Attendee
1636
-            ) {
1637
-                $this->set_status(EEM_Transaction::incomplete_status_code);
1638
-            } else {
1639
-                // no contact record? yer abandoned!
1640
-                $this->set_status(EEM_Transaction::abandoned_status_code);
1641
-            }
1642
-            return true;
1643
-        }
1644
-        return false;
1645
-    }
1646
-
1647
-
1648
-    /**
1649
-     * checks if an Abandoned TXN has any related payments, and if so,
1650
-     * updates the TXN status based on the amount paid
1651
-     *
1652
-     * @throws EE_Error
1653
-     * @throws InvalidDataTypeException
1654
-     * @throws InvalidInterfaceException
1655
-     * @throws InvalidArgumentException
1656
-     * @throws RuntimeException
1657
-     * @throws ReflectionException
1658
-     */
1659
-    public function verify_abandoned_transaction_status()
1660
-    {
1661
-        if ($this->status_ID() !== EEM_Transaction::abandoned_status_code) {
1662
-            return;
1663
-        }
1664
-        $payments = $this->get_many_related('Payment');
1665
-        if (! empty($payments)) {
1666
-            foreach ($payments as $payment) {
1667
-                if ($payment instanceof EE_Payment) {
1668
-                    // kk this TXN should NOT be abandoned
1669
-                    $this->update_status_based_on_total_paid();
1670
-                    if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) {
1671
-                        EE_Error::add_attention(
1672
-                            sprintf(
1673
-                                esc_html__(
1674
-                                    'The status for Transaction #%1$d has been updated from "Abandoned" to "%2$s", because at least one payment has been made towards it. If the payment appears in the "Payment Details" table below, you may need to edit its status and/or other details as well.',
1675
-                                    'event_espresso'
1676
-                                ),
1677
-                                $this->ID(),
1678
-                                $this->pretty_status()
1679
-                            )
1680
-                        );
1681
-                    }
1682
-                    // get final reg step status
1683
-                    $finalized = $this->final_reg_step_completed();
1684
-                    // if the 'finalize_registration' step has been initiated (has a timestamp)
1685
-                    // but has not yet been fully completed (TRUE)
1686
-                    if (is_int($finalized) && $finalized !== false && $finalized !== true) {
1687
-                        $this->set_reg_step_completed('finalize_registration');
1688
-                        $this->save();
1689
-                    }
1690
-                }
1691
-            }
1692
-        }
1693
-    }
16
+	/**
17
+	 * The length of time in seconds that a lock is applied before being considered expired.
18
+	 * It is not long because a transaction should only be locked for the duration of the request that locked it
19
+	 */
20
+	const LOCK_EXPIRATION = 2;
21
+
22
+	/**
23
+	 * txn status upon initial construction.
24
+	 *
25
+	 * @var string
26
+	 */
27
+	protected $_old_txn_status;
28
+
29
+
30
+	/**
31
+	 * @param array  $props_n_values          incoming values
32
+	 * @param string $timezone                incoming timezone
33
+	 *                                        (if not set the timezone set for the website will be used.)
34
+	 * @param array  $date_formats            incoming date_formats in an array where the first value is the
35
+	 *                                        date_format and the second value is the time format
36
+	 * @return EE_Transaction
37
+	 * @throws EE_Error
38
+	 * @throws InvalidArgumentException
39
+	 * @throws InvalidDataTypeException
40
+	 * @throws InvalidInterfaceException
41
+	 * @throws ReflectionException
42
+	 */
43
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
44
+	{
45
+		$has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
46
+		$txn = $has_object
47
+			? $has_object
48
+			: new self($props_n_values, false, $timezone, $date_formats);
49
+		if (! $has_object) {
50
+			$txn->set_old_txn_status($txn->status_ID());
51
+		}
52
+		return $txn;
53
+	}
54
+
55
+
56
+	/**
57
+	 * @param array  $props_n_values  incoming values from the database
58
+	 * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
59
+	 *                                the website will be used.
60
+	 * @return EE_Transaction
61
+	 * @throws EE_Error
62
+	 * @throws InvalidArgumentException
63
+	 * @throws InvalidDataTypeException
64
+	 * @throws InvalidInterfaceException
65
+	 * @throws ReflectionException
66
+	 */
67
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
68
+	{
69
+		$txn = new self($props_n_values, true, $timezone);
70
+		$txn->set_old_txn_status($txn->status_ID());
71
+		return $txn;
72
+	}
73
+
74
+
75
+	/**
76
+	 * Sets a meta field indicating that this TXN is locked and should not be updated in the db.
77
+	 * If a lock has already been set, then we will attempt to remove it in case it has expired.
78
+	 * If that also fails, then an exception is thrown.
79
+	 *
80
+	 * @throws EE_Error
81
+	 * @throws InvalidArgumentException
82
+	 * @throws InvalidDataTypeException
83
+	 * @throws InvalidInterfaceException
84
+	 * @throws ReflectionException
85
+	 */
86
+	public function lock()
87
+	{
88
+		// attempt to set lock, but if that fails...
89
+		if (! $this->add_extra_meta('lock', time(), true)) {
90
+			// then attempt to remove the lock in case it is expired
91
+			if ($this->_remove_expired_lock()) {
92
+				// if removal was successful, then try setting lock again
93
+				$this->lock();
94
+			} else {
95
+				// but if the lock can not be removed, then throw an exception
96
+				throw new EE_Error(
97
+					sprintf(
98
+						__(
99
+							'Could not lock Transaction %1$d because it is already locked, meaning another part of the system is currently editing it. It should already be unlocked by the time you read this, so please refresh the page and try again.',
100
+							'event_espresso'
101
+						),
102
+						$this->ID()
103
+					)
104
+				);
105
+			}
106
+		}
107
+	}
108
+
109
+
110
+	/**
111
+	 * removes transaction lock applied in EE_Transaction::lock()
112
+	 *
113
+	 * @return int
114
+	 * @throws EE_Error
115
+	 * @throws InvalidArgumentException
116
+	 * @throws InvalidDataTypeException
117
+	 * @throws InvalidInterfaceException
118
+	 * @throws ReflectionException
119
+	 */
120
+	public function unlock()
121
+	{
122
+		return $this->delete_extra_meta('lock');
123
+	}
124
+
125
+
126
+	/**
127
+	 * Decides whether or not now is the right time to update the transaction.
128
+	 * This is useful because we don't always know if it is safe to update the transaction
129
+	 * and its related data. why?
130
+	 * because it's possible that the transaction is being used in another
131
+	 * request and could overwrite anything we save.
132
+	 * So we want to only update the txn once we know that won't happen.
133
+	 * We also check that the lock isn't expired, and remove it if it is
134
+	 *
135
+	 * @return boolean
136
+	 * @throws EE_Error
137
+	 * @throws InvalidArgumentException
138
+	 * @throws InvalidDataTypeException
139
+	 * @throws InvalidInterfaceException
140
+	 * @throws ReflectionException
141
+	 */
142
+	public function is_locked()
143
+	{
144
+		// if TXN is not locked, then return false immediately
145
+		if (! $this->_get_lock()) {
146
+			return false;
147
+		}
148
+		// if not, then let's try and remove the lock in case it's expired...
149
+		// _remove_expired_lock() returns 0 when lock is valid (ie: removed = false)
150
+		// and a positive number if the lock was removed (ie: number of locks deleted),
151
+		// so we need to return the opposite
152
+		return ! $this->_remove_expired_lock() ? true : false;
153
+	}
154
+
155
+
156
+	/**
157
+	 * Gets the meta field indicating that this TXN is locked
158
+	 *
159
+	 * @return int
160
+	 * @throws EE_Error
161
+	 * @throws InvalidArgumentException
162
+	 * @throws InvalidDataTypeException
163
+	 * @throws InvalidInterfaceException
164
+	 * @throws ReflectionException
165
+	 */
166
+	protected function _get_lock()
167
+	{
168
+		return (int) $this->get_extra_meta('lock', true, 0);
169
+	}
170
+
171
+
172
+	/**
173
+	 * If the lock on this transaction is expired, then we want to remove it so that the transaction can be updated
174
+	 *
175
+	 * @return int
176
+	 * @throws EE_Error
177
+	 * @throws InvalidArgumentException
178
+	 * @throws InvalidDataTypeException
179
+	 * @throws InvalidInterfaceException
180
+	 * @throws ReflectionException
181
+	 */
182
+	protected function _remove_expired_lock()
183
+	{
184
+		$locked = $this->_get_lock();
185
+		if ($locked && time() - EE_Transaction::LOCK_EXPIRATION > $locked) {
186
+			return $this->unlock();
187
+		}
188
+		return 0;
189
+	}
190
+
191
+
192
+	/**
193
+	 * Set transaction total
194
+	 *
195
+	 * @param float $total total value of transaction
196
+	 * @throws EE_Error
197
+	 * @throws InvalidArgumentException
198
+	 * @throws InvalidDataTypeException
199
+	 * @throws InvalidInterfaceException
200
+	 * @throws ReflectionException
201
+	 */
202
+	public function set_total($total = 0.00)
203
+	{
204
+		$this->set('TXN_total', (float) $total);
205
+	}
206
+
207
+
208
+	/**
209
+	 * Set Total Amount Paid to Date
210
+	 *
211
+	 * @param float $total_paid total amount paid to date (sum of all payments)
212
+	 * @throws EE_Error
213
+	 * @throws InvalidArgumentException
214
+	 * @throws InvalidDataTypeException
215
+	 * @throws InvalidInterfaceException
216
+	 * @throws ReflectionException
217
+	 */
218
+	public function set_paid($total_paid = 0.00)
219
+	{
220
+		$this->set('TXN_paid', (float) $total_paid);
221
+	}
222
+
223
+
224
+	/**
225
+	 * Set transaction status
226
+	 *
227
+	 * @param string $status        whether the transaction is open, declined, accepted,
228
+	 *                              or any number of custom values that can be set
229
+	 * @throws EE_Error
230
+	 * @throws InvalidArgumentException
231
+	 * @throws InvalidDataTypeException
232
+	 * @throws InvalidInterfaceException
233
+	 * @throws ReflectionException
234
+	 */
235
+	public function set_status($status = '')
236
+	{
237
+		$this->set('STS_ID', $status);
238
+	}
239
+
240
+
241
+	/**
242
+	 * Set hash salt
243
+	 *
244
+	 * @param string $hash_salt required for some payment gateways
245
+	 * @throws EE_Error
246
+	 * @throws InvalidArgumentException
247
+	 * @throws InvalidDataTypeException
248
+	 * @throws InvalidInterfaceException
249
+	 * @throws ReflectionException
250
+	 */
251
+	public function set_hash_salt($hash_salt = '')
252
+	{
253
+		$this->set('TXN_hash_salt', $hash_salt);
254
+	}
255
+
256
+
257
+	/**
258
+	 * Sets TXN_reg_steps array
259
+	 *
260
+	 * @param array $txn_reg_steps
261
+	 * @throws EE_Error
262
+	 * @throws InvalidArgumentException
263
+	 * @throws InvalidDataTypeException
264
+	 * @throws InvalidInterfaceException
265
+	 * @throws ReflectionException
266
+	 */
267
+	public function set_reg_steps(array $txn_reg_steps)
268
+	{
269
+		$this->set('TXN_reg_steps', $txn_reg_steps);
270
+	}
271
+
272
+
273
+	/**
274
+	 * Gets TXN_reg_steps
275
+	 *
276
+	 * @return array
277
+	 * @throws EE_Error
278
+	 * @throws InvalidArgumentException
279
+	 * @throws InvalidDataTypeException
280
+	 * @throws InvalidInterfaceException
281
+	 * @throws ReflectionException
282
+	 */
283
+	public function reg_steps()
284
+	{
285
+		$TXN_reg_steps = $this->get('TXN_reg_steps');
286
+		return is_array($TXN_reg_steps) ? (array) $TXN_reg_steps : array();
287
+	}
288
+
289
+
290
+	/**
291
+	 * @return string of transaction's total cost, with currency symbol and decimal
292
+	 * @throws EE_Error
293
+	 * @throws InvalidArgumentException
294
+	 * @throws InvalidDataTypeException
295
+	 * @throws InvalidInterfaceException
296
+	 * @throws ReflectionException
297
+	 */
298
+	public function pretty_total()
299
+	{
300
+		return $this->get_pretty('TXN_total');
301
+	}
302
+
303
+
304
+	/**
305
+	 * Gets the amount paid in a pretty string (formatted and with currency symbol)
306
+	 *
307
+	 * @return string
308
+	 * @throws EE_Error
309
+	 * @throws InvalidArgumentException
310
+	 * @throws InvalidDataTypeException
311
+	 * @throws InvalidInterfaceException
312
+	 * @throws ReflectionException
313
+	 */
314
+	public function pretty_paid()
315
+	{
316
+		return $this->get_pretty('TXN_paid');
317
+	}
318
+
319
+
320
+	/**
321
+	 * calculate the amount remaining for this transaction and return;
322
+	 *
323
+	 * @return float amount remaining
324
+	 * @throws EE_Error
325
+	 * @throws InvalidArgumentException
326
+	 * @throws InvalidDataTypeException
327
+	 * @throws InvalidInterfaceException
328
+	 * @throws ReflectionException
329
+	 */
330
+	public function remaining()
331
+	{
332
+		return $this->total() - $this->paid();
333
+	}
334
+
335
+
336
+	/**
337
+	 * get Transaction Total
338
+	 *
339
+	 * @return float
340
+	 * @throws EE_Error
341
+	 * @throws InvalidArgumentException
342
+	 * @throws InvalidDataTypeException
343
+	 * @throws InvalidInterfaceException
344
+	 * @throws ReflectionException
345
+	 */
346
+	public function total()
347
+	{
348
+		return (float) $this->get('TXN_total');
349
+	}
350
+
351
+
352
+	/**
353
+	 * get Total Amount Paid to Date
354
+	 *
355
+	 * @return float
356
+	 * @throws EE_Error
357
+	 * @throws InvalidArgumentException
358
+	 * @throws InvalidDataTypeException
359
+	 * @throws InvalidInterfaceException
360
+	 * @throws ReflectionException
361
+	 */
362
+	public function paid()
363
+	{
364
+		return (float) $this->get('TXN_paid');
365
+	}
366
+
367
+
368
+	/**
369
+	 * @return mixed|null
370
+	 * @throws EE_Error
371
+	 * @throws InvalidArgumentException
372
+	 * @throws InvalidDataTypeException
373
+	 * @throws InvalidInterfaceException
374
+	 * @throws ReflectionException
375
+	 */
376
+	public function get_cart_session()
377
+	{
378
+		$session_data = (array) $this->get('TXN_session_data');
379
+		return isset($session_data['cart']) && $session_data['cart'] instanceof EE_Cart
380
+			? $session_data['cart']
381
+			: null;
382
+	}
383
+
384
+
385
+	/**
386
+	 * get Transaction session data
387
+	 *
388
+	 * @return array|mixed
389
+	 * @throws EE_Error
390
+	 * @throws InvalidArgumentException
391
+	 * @throws InvalidDataTypeException
392
+	 * @throws InvalidInterfaceException
393
+	 * @throws ReflectionException
394
+	 */
395
+	public function session_data()
396
+	{
397
+		$session_data = $this->get('TXN_session_data');
398
+		if (empty($session_data)) {
399
+			$session_data = array(
400
+				'id'            => null,
401
+				'user_id'       => null,
402
+				'ip_address'    => null,
403
+				'user_agent'    => null,
404
+				'init_access'   => null,
405
+				'last_access'   => null,
406
+				'pages_visited' => array(),
407
+			);
408
+		}
409
+		return $session_data;
410
+	}
411
+
412
+
413
+	/**
414
+	 * Set session data within the TXN object
415
+	 *
416
+	 * @param EE_Session|array $session_data
417
+	 * @throws EE_Error
418
+	 * @throws InvalidArgumentException
419
+	 * @throws InvalidDataTypeException
420
+	 * @throws InvalidInterfaceException
421
+	 * @throws ReflectionException
422
+	 */
423
+	public function set_txn_session_data($session_data)
424
+	{
425
+		if ($session_data instanceof EE_Session) {
426
+			$this->set('TXN_session_data', $session_data->get_session_data(null, true));
427
+		} else {
428
+			$this->set('TXN_session_data', $session_data);
429
+		}
430
+	}
431
+
432
+
433
+	/**
434
+	 * get Transaction hash salt
435
+	 *
436
+	 * @return mixed
437
+	 * @throws EE_Error
438
+	 * @throws InvalidArgumentException
439
+	 * @throws InvalidDataTypeException
440
+	 * @throws InvalidInterfaceException
441
+	 * @throws ReflectionException
442
+	 */
443
+	public function hash_salt_()
444
+	{
445
+		return $this->get('TXN_hash_salt');
446
+	}
447
+
448
+
449
+	/**
450
+	 * Returns the transaction datetime as either:
451
+	 *            - unix timestamp format ($format = false, $gmt = true)
452
+	 *            - formatted date string including the UTC (timezone) offset ($format = true ($gmt
453
+	 *              has no affect with this option)), this also may include a timezone abbreviation if the
454
+	 *              set timezone in this class differs from what the timezone is on the blog.
455
+	 *            - formatted date string including the UTC (timezone) offset (default).
456
+	 *
457
+	 * @param boolean $format   - whether to return a unix timestamp (default) or formatted date string
458
+	 * @param boolean $gmt      - whether to return a unix timestamp with UTC offset applied (default)
459
+	 *                          or no UTC offset applied
460
+	 * @return string | int
461
+	 * @throws EE_Error
462
+	 * @throws InvalidArgumentException
463
+	 * @throws InvalidDataTypeException
464
+	 * @throws InvalidInterfaceException
465
+	 * @throws ReflectionException
466
+	 */
467
+	public function datetime($format = false, $gmt = false)
468
+	{
469
+		if ($format) {
470
+			return $this->get_pretty('TXN_timestamp');
471
+		}
472
+		if ($gmt) {
473
+			return $this->get_raw('TXN_timestamp');
474
+		}
475
+		return $this->get('TXN_timestamp');
476
+	}
477
+
478
+
479
+	/**
480
+	 * Gets registrations on this transaction
481
+	 *
482
+	 * @param array   $query_params array of query parameters
483
+	 * @param boolean $get_cached   TRUE to retrieve cached registrations or FALSE to pull from the db
484
+	 * @return EE_Base_Class[]|EE_Registration[]
485
+	 * @throws EE_Error
486
+	 * @throws InvalidArgumentException
487
+	 * @throws InvalidDataTypeException
488
+	 * @throws InvalidInterfaceException
489
+	 * @throws ReflectionException
490
+	 */
491
+	public function registrations($query_params = array(), $get_cached = false)
492
+	{
493
+		$query_params = (empty($query_params) || ! is_array($query_params))
494
+			? array(
495
+				'order_by' => array(
496
+					'Event.EVT_name'     => 'ASC',
497
+					'Attendee.ATT_lname' => 'ASC',
498
+					'Attendee.ATT_fname' => 'ASC',
499
+				),
500
+			)
501
+			: $query_params;
502
+		$query_params = $get_cached ? array() : $query_params;
503
+		return $this->get_many_related('Registration', $query_params);
504
+	}
505
+
506
+
507
+	/**
508
+	 * Gets all the attendees for this transaction (handy for use with EE_Attendee's get_registrations_for_event
509
+	 * function for getting attendees and how many registrations they each have for an event)
510
+	 *
511
+	 * @return mixed EE_Attendee[] by default, int if $output is set to 'COUNT'
512
+	 * @throws EE_Error
513
+	 * @throws InvalidArgumentException
514
+	 * @throws InvalidDataTypeException
515
+	 * @throws InvalidInterfaceException
516
+	 * @throws ReflectionException
517
+	 */
518
+	public function attendees()
519
+	{
520
+		return $this->get_many_related('Attendee', array(array('Registration.Transaction.TXN_ID' => $this->ID())));
521
+	}
522
+
523
+
524
+	/**
525
+	 * Gets payments for this transaction. Unlike other such functions, order by 'DESC' by default
526
+	 *
527
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
528
+	 * @return EE_Base_Class[]|EE_Payment[]
529
+	 * @throws EE_Error
530
+	 * @throws InvalidArgumentException
531
+	 * @throws InvalidDataTypeException
532
+	 * @throws InvalidInterfaceException
533
+	 * @throws ReflectionException
534
+	 */
535
+	public function payments($query_params = array())
536
+	{
537
+		return $this->get_many_related('Payment', $query_params);
538
+	}
539
+
540
+
541
+	/**
542
+	 * gets only approved payments for this transaction
543
+	 *
544
+	 * @return EE_Base_Class[]|EE_Payment[]
545
+	 * @throws EE_Error
546
+	 * @throws InvalidArgumentException
547
+	 * @throws ReflectionException
548
+	 * @throws InvalidDataTypeException
549
+	 * @throws InvalidInterfaceException
550
+	 */
551
+	public function approved_payments()
552
+	{
553
+		EE_Registry::instance()->load_model('Payment');
554
+		return $this->get_many_related(
555
+			'Payment',
556
+			array(
557
+				array('STS_ID' => EEM_Payment::status_id_approved),
558
+				'order_by' => array('PAY_timestamp' => 'DESC'),
559
+			)
560
+		);
561
+	}
562
+
563
+
564
+	/**
565
+	 * Gets all payments which have not been approved
566
+	 *
567
+	 * @return EE_Base_Class[]|EEI_Payment[]
568
+	 * @throws EE_Error if a model is misconfigured somehow
569
+	 * @throws InvalidArgumentException
570
+	 * @throws InvalidDataTypeException
571
+	 * @throws InvalidInterfaceException
572
+	 * @throws ReflectionException
573
+	 */
574
+	public function pending_payments()
575
+	{
576
+		return $this->get_many_related(
577
+			'Payment',
578
+			array(
579
+				array(
580
+					'STS_ID' => EEM_Payment::status_id_pending,
581
+				),
582
+				'order_by' => array(
583
+					'PAY_timestamp' => 'DESC',
584
+				),
585
+			)
586
+		);
587
+	}
588
+
589
+
590
+	/**
591
+	 * echoes $this->pretty_status()
592
+	 *
593
+	 * @param bool $show_icons
594
+	 * @throws EE_Error
595
+	 * @throws InvalidArgumentException
596
+	 * @throws InvalidDataTypeException
597
+	 * @throws InvalidInterfaceException
598
+	 * @throws ReflectionException
599
+	 */
600
+	public function e_pretty_status($show_icons = false)
601
+	{
602
+		echo $this->pretty_status($show_icons);
603
+	}
604
+
605
+
606
+	/**
607
+	 * returns a pretty version of the status, good for displaying to users
608
+	 *
609
+	 * @param bool $show_icons
610
+	 * @return string
611
+	 * @throws EE_Error
612
+	 * @throws InvalidArgumentException
613
+	 * @throws InvalidDataTypeException
614
+	 * @throws InvalidInterfaceException
615
+	 * @throws ReflectionException
616
+	 */
617
+	public function pretty_status($show_icons = false)
618
+	{
619
+		$status = EEM_Status::instance()->localized_status(
620
+			array($this->status_ID() => __('unknown', 'event_espresso')),
621
+			false,
622
+			'sentence'
623
+		);
624
+		$icon = '';
625
+		switch ($this->status_ID()) {
626
+			case EEM_Transaction::complete_status_code:
627
+				$icon = $show_icons ? '<span class="dashicons dashicons-yes ee-icon-size-24 green-text"></span>' : '';
628
+				break;
629
+			case EEM_Transaction::incomplete_status_code:
630
+				$icon = $show_icons ? '<span class="dashicons dashicons-marker ee-icon-size-16 lt-blue-text"></span>'
631
+					: '';
632
+				break;
633
+			case EEM_Transaction::abandoned_status_code:
634
+				$icon = $show_icons ? '<span class="dashicons dashicons-marker ee-icon-size-16 red-text"></span>' : '';
635
+				break;
636
+			case EEM_Transaction::failed_status_code:
637
+				$icon = $show_icons ? '<span class="dashicons dashicons-no ee-icon-size-16 red-text"></span>' : '';
638
+				break;
639
+			case EEM_Transaction::overpaid_status_code:
640
+				$icon = $show_icons ? '<span class="dashicons dashicons-plus ee-icon-size-16 orange-text"></span>' : '';
641
+				break;
642
+		}
643
+		return $icon . $status[ $this->status_ID() ];
644
+	}
645
+
646
+
647
+	/**
648
+	 * get Transaction Status
649
+	 *
650
+	 * @return mixed
651
+	 * @throws EE_Error
652
+	 * @throws InvalidArgumentException
653
+	 * @throws InvalidDataTypeException
654
+	 * @throws InvalidInterfaceException
655
+	 * @throws ReflectionException
656
+	 */
657
+	public function status_ID()
658
+	{
659
+		return $this->get('STS_ID');
660
+	}
661
+
662
+
663
+	/**
664
+	 * Returns TRUE or FALSE for whether or not this transaction cost any money
665
+	 *
666
+	 * @return boolean
667
+	 * @throws EE_Error
668
+	 * @throws InvalidArgumentException
669
+	 * @throws InvalidDataTypeException
670
+	 * @throws InvalidInterfaceException
671
+	 * @throws ReflectionException
672
+	 */
673
+	public function is_free()
674
+	{
675
+		return EEH_Money::compare_floats($this->get('TXN_total'), 0, '==');
676
+	}
677
+
678
+
679
+	/**
680
+	 * Returns whether this transaction is complete
681
+	 * Useful in templates and other logic for deciding if we should ask for another payment...
682
+	 *
683
+	 * @return boolean
684
+	 * @throws EE_Error
685
+	 * @throws InvalidArgumentException
686
+	 * @throws InvalidDataTypeException
687
+	 * @throws InvalidInterfaceException
688
+	 * @throws ReflectionException
689
+	 */
690
+	public function is_completed()
691
+	{
692
+		return $this->status_ID() === EEM_Transaction::complete_status_code;
693
+	}
694
+
695
+
696
+	/**
697
+	 * Returns whether this transaction is incomplete
698
+	 * Useful in templates and other logic for deciding if we should ask for another payment...
699
+	 *
700
+	 * @return boolean
701
+	 * @throws EE_Error
702
+	 * @throws InvalidArgumentException
703
+	 * @throws InvalidDataTypeException
704
+	 * @throws InvalidInterfaceException
705
+	 * @throws ReflectionException
706
+	 */
707
+	public function is_incomplete()
708
+	{
709
+		return $this->status_ID() === EEM_Transaction::incomplete_status_code;
710
+	}
711
+
712
+
713
+	/**
714
+	 * Returns whether this transaction is overpaid
715
+	 * Useful in templates and other logic for deciding if monies need to be refunded
716
+	 *
717
+	 * @return boolean
718
+	 * @throws EE_Error
719
+	 * @throws InvalidArgumentException
720
+	 * @throws InvalidDataTypeException
721
+	 * @throws InvalidInterfaceException
722
+	 * @throws ReflectionException
723
+	 */
724
+	public function is_overpaid()
725
+	{
726
+		return $this->status_ID() === EEM_Transaction::overpaid_status_code;
727
+	}
728
+
729
+
730
+	/**
731
+	 * Returns whether this transaction was abandoned
732
+	 * meaning that the transaction/registration process was somehow interrupted and never completed
733
+	 * but that contact information exists for at least one registrant
734
+	 *
735
+	 * @return boolean
736
+	 * @throws EE_Error
737
+	 * @throws InvalidArgumentException
738
+	 * @throws InvalidDataTypeException
739
+	 * @throws InvalidInterfaceException
740
+	 * @throws ReflectionException
741
+	 */
742
+	public function is_abandoned()
743
+	{
744
+		return $this->status_ID() === EEM_Transaction::abandoned_status_code;
745
+	}
746
+
747
+
748
+	/**
749
+	 * Returns whether this transaction failed
750
+	 * meaning that the transaction/registration process was somehow interrupted and never completed
751
+	 * and that NO contact information exists for any registrants
752
+	 *
753
+	 * @return boolean
754
+	 * @throws EE_Error
755
+	 * @throws InvalidArgumentException
756
+	 * @throws InvalidDataTypeException
757
+	 * @throws InvalidInterfaceException
758
+	 * @throws ReflectionException
759
+	 */
760
+	public function failed()
761
+	{
762
+		return $this->status_ID() === EEM_Transaction::failed_status_code;
763
+	}
764
+
765
+
766
+	/**
767
+	 * This returns the url for the invoice of this transaction
768
+	 *
769
+	 * @param string $type 'html' or 'pdf' (default is pdf)
770
+	 * @return string
771
+	 * @throws EE_Error
772
+	 * @throws InvalidArgumentException
773
+	 * @throws InvalidDataTypeException
774
+	 * @throws InvalidInterfaceException
775
+	 * @throws ReflectionException
776
+	 */
777
+	public function invoice_url($type = 'html')
778
+	{
779
+		$REG = $this->primary_registration();
780
+		if (! $REG instanceof EE_Registration) {
781
+			return '';
782
+		}
783
+		return $REG->invoice_url($type);
784
+	}
785
+
786
+
787
+	/**
788
+	 * Gets the primary registration only
789
+	 *
790
+	 * @return EE_Base_Class|EE_Registration
791
+	 * @throws EE_Error
792
+	 * @throws InvalidArgumentException
793
+	 * @throws InvalidDataTypeException
794
+	 * @throws InvalidInterfaceException
795
+	 * @throws ReflectionException
796
+	 */
797
+	public function primary_registration()
798
+	{
799
+		$registrations = (array) $this->get_many_related(
800
+			'Registration',
801
+			array(array('REG_count' => EEM_Registration::PRIMARY_REGISTRANT_COUNT))
802
+		);
803
+		foreach ($registrations as $registration) {
804
+			// valid registration that is NOT cancelled or declined ?
805
+			if ($registration instanceof EE_Registration
806
+				&& ! in_array($registration->status_ID(), EEM_Registration::closed_reg_statuses(), true)
807
+			) {
808
+				return $registration;
809
+			}
810
+		}
811
+		// nothing valid found, so just return first thing from array of results
812
+		return reset($registrations);
813
+	}
814
+
815
+
816
+	/**
817
+	 * Gets the URL for viewing the receipt
818
+	 *
819
+	 * @param string $type 'pdf' or 'html' (default is 'html')
820
+	 * @return string
821
+	 * @throws EE_Error
822
+	 * @throws InvalidArgumentException
823
+	 * @throws InvalidDataTypeException
824
+	 * @throws InvalidInterfaceException
825
+	 * @throws ReflectionException
826
+	 */
827
+	public function receipt_url($type = 'html')
828
+	{
829
+		$REG = $this->primary_registration();
830
+		if (! $REG instanceof EE_Registration) {
831
+			return '';
832
+		}
833
+		return $REG->receipt_url($type);
834
+	}
835
+
836
+
837
+	/**
838
+	 * Gets the URL of the thank you page with this registration REG_url_link added as
839
+	 * a query parameter
840
+	 *
841
+	 * @return string
842
+	 * @throws EE_Error
843
+	 * @throws InvalidArgumentException
844
+	 * @throws InvalidDataTypeException
845
+	 * @throws InvalidInterfaceException
846
+	 * @throws ReflectionException
847
+	 */
848
+	public function payment_overview_url()
849
+	{
850
+		$primary_registration = $this->primary_registration();
851
+		return $primary_registration instanceof EE_Registration ? $primary_registration->payment_overview_url() : false;
852
+	}
853
+
854
+
855
+	/**
856
+	 * @return string
857
+	 * @throws EE_Error
858
+	 * @throws InvalidArgumentException
859
+	 * @throws InvalidDataTypeException
860
+	 * @throws InvalidInterfaceException
861
+	 * @throws ReflectionException
862
+	 */
863
+	public function gateway_response_on_transaction()
864
+	{
865
+		$payment = $this->get_first_related('Payment');
866
+		return $payment instanceof EE_Payment ? $payment->gateway_response() : '';
867
+	}
868
+
869
+
870
+	/**
871
+	 * Get the status object of this object
872
+	 *
873
+	 * @return EE_Base_Class|EE_Status
874
+	 * @throws EE_Error
875
+	 * @throws InvalidArgumentException
876
+	 * @throws InvalidDataTypeException
877
+	 * @throws InvalidInterfaceException
878
+	 * @throws ReflectionException
879
+	 */
880
+	public function status_obj()
881
+	{
882
+		return $this->get_first_related('Status');
883
+	}
884
+
885
+
886
+	/**
887
+	 * Gets all the extra meta info on this payment
888
+	 *
889
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
890
+	 * @return EE_Base_Class[]|EE_Extra_Meta
891
+	 * @throws EE_Error
892
+	 * @throws InvalidArgumentException
893
+	 * @throws InvalidDataTypeException
894
+	 * @throws InvalidInterfaceException
895
+	 * @throws ReflectionException
896
+	 */
897
+	public function extra_meta($query_params = array())
898
+	{
899
+		return $this->get_many_related('Extra_Meta', $query_params);
900
+	}
901
+
902
+
903
+	/**
904
+	 * Wrapper for _add_relation_to
905
+	 *
906
+	 * @param EE_Registration $registration
907
+	 * @return EE_Base_Class the relation was added to
908
+	 * @throws EE_Error
909
+	 * @throws InvalidArgumentException
910
+	 * @throws InvalidDataTypeException
911
+	 * @throws InvalidInterfaceException
912
+	 * @throws ReflectionException
913
+	 */
914
+	public function add_registration(EE_Registration $registration)
915
+	{
916
+		return $this->_add_relation_to($registration, 'Registration');
917
+	}
918
+
919
+
920
+	/**
921
+	 * Removes the given registration from being related (even before saving this transaction).
922
+	 * If an ID/index is provided and this transaction isn't saved yet, removes it from list of cached relations
923
+	 *
924
+	 * @param int $registration_or_id
925
+	 * @return EE_Base_Class that was removed from being related
926
+	 * @throws EE_Error
927
+	 * @throws InvalidArgumentException
928
+	 * @throws InvalidDataTypeException
929
+	 * @throws InvalidInterfaceException
930
+	 * @throws ReflectionException
931
+	 */
932
+	public function remove_registration_with_id($registration_or_id)
933
+	{
934
+		return $this->_remove_relation_to($registration_or_id, 'Registration');
935
+	}
936
+
937
+
938
+	/**
939
+	 * Gets all the line items which are for ACTUAL items
940
+	 *
941
+	 * @return EE_Line_Item[]
942
+	 * @throws EE_Error
943
+	 * @throws InvalidArgumentException
944
+	 * @throws InvalidDataTypeException
945
+	 * @throws InvalidInterfaceException
946
+	 * @throws ReflectionException
947
+	 */
948
+	public function items_purchased()
949
+	{
950
+		return $this->line_items(array(array('LIN_type' => EEM_Line_Item::type_line_item)));
951
+	}
952
+
953
+
954
+	/**
955
+	 * Wrapper for _add_relation_to
956
+	 *
957
+	 * @param EE_Line_Item $line_item
958
+	 * @return EE_Base_Class the relation was added to
959
+	 * @throws EE_Error
960
+	 * @throws InvalidArgumentException
961
+	 * @throws InvalidDataTypeException
962
+	 * @throws InvalidInterfaceException
963
+	 * @throws ReflectionException
964
+	 */
965
+	public function add_line_item(EE_Line_Item $line_item)
966
+	{
967
+		return $this->_add_relation_to($line_item, 'Line_Item');
968
+	}
969
+
970
+
971
+	/**
972
+	 * Gets ALL the line items related to this transaction (unstructured)
973
+	 *
974
+	 * @param array $query_params
975
+	 * @return EE_Base_Class[]|EE_Line_Item[]
976
+	 * @throws EE_Error
977
+	 * @throws InvalidArgumentException
978
+	 * @throws InvalidDataTypeException
979
+	 * @throws InvalidInterfaceException
980
+	 * @throws ReflectionException
981
+	 */
982
+	public function line_items($query_params = array())
983
+	{
984
+		return $this->get_many_related('Line_Item', $query_params);
985
+	}
986
+
987
+
988
+	/**
989
+	 * Gets all the line items which are taxes on the total
990
+	 *
991
+	 * @return EE_Line_Item[]
992
+	 * @throws EE_Error
993
+	 * @throws InvalidArgumentException
994
+	 * @throws InvalidDataTypeException
995
+	 * @throws InvalidInterfaceException
996
+	 * @throws ReflectionException
997
+	 */
998
+	public function tax_items()
999
+	{
1000
+		return $this->line_items(array(array('LIN_type' => EEM_Line_Item::type_tax)));
1001
+	}
1002
+
1003
+
1004
+	/**
1005
+	 * Gets the total line item (which is a parent of all other related line items,
1006
+	 * meaning it takes them all into account on its total)
1007
+	 *
1008
+	 * @param bool $create_if_not_found
1009
+	 * @return \EE_Line_Item
1010
+	 * @throws EE_Error
1011
+	 * @throws InvalidArgumentException
1012
+	 * @throws InvalidDataTypeException
1013
+	 * @throws InvalidInterfaceException
1014
+	 * @throws ReflectionException
1015
+	 */
1016
+	public function total_line_item($create_if_not_found = true)
1017
+	{
1018
+		$item = $this->get_first_related('Line_Item', array(array('LIN_type' => EEM_Line_Item::type_total)));
1019
+		if (! $item && $create_if_not_found) {
1020
+			$item = EEH_Line_Item::create_total_line_item($this);
1021
+		}
1022
+		return $item;
1023
+	}
1024
+
1025
+
1026
+	/**
1027
+	 * Returns the total amount of tax on this transaction
1028
+	 * (assumes there's only one tax subtotal line item)
1029
+	 *
1030
+	 * @return float
1031
+	 * @throws EE_Error
1032
+	 * @throws InvalidArgumentException
1033
+	 * @throws InvalidDataTypeException
1034
+	 * @throws InvalidInterfaceException
1035
+	 * @throws ReflectionException
1036
+	 */
1037
+	public function tax_total()
1038
+	{
1039
+		$tax_line_item = $this->tax_total_line_item();
1040
+		if ($tax_line_item) {
1041
+			return (float) $tax_line_item->total();
1042
+		}
1043
+		return (float) 0;
1044
+	}
1045
+
1046
+
1047
+	/**
1048
+	 * Gets the tax subtotal line item (assumes there's only one)
1049
+	 *
1050
+	 * @return EE_Line_Item
1051
+	 * @throws EE_Error
1052
+	 * @throws InvalidArgumentException
1053
+	 * @throws InvalidDataTypeException
1054
+	 * @throws InvalidInterfaceException
1055
+	 * @throws ReflectionException
1056
+	 */
1057
+	public function tax_total_line_item()
1058
+	{
1059
+		return EEH_Line_Item::get_taxes_subtotal($this->total_line_item());
1060
+	}
1061
+
1062
+
1063
+	/**
1064
+	 * Gets the array of billing info for the gateway and for this transaction's primary registration's attendee.
1065
+	 *
1066
+	 * @return EE_Form_Section_Proper
1067
+	 * @throws EE_Error
1068
+	 * @throws InvalidArgumentException
1069
+	 * @throws InvalidDataTypeException
1070
+	 * @throws InvalidInterfaceException
1071
+	 * @throws ReflectionException
1072
+	 */
1073
+	public function billing_info()
1074
+	{
1075
+		$payment_method = $this->payment_method();
1076
+		if (! $payment_method) {
1077
+			EE_Error::add_error(
1078
+				__(
1079
+					'Could not find billing info for transaction because no gateway has been used for it yet',
1080
+					'event_espresso'
1081
+				),
1082
+				__FILE__,
1083
+				__FUNCTION__,
1084
+				__LINE__
1085
+			);
1086
+			return null;
1087
+		}
1088
+		$primary_reg = $this->primary_registration();
1089
+		if (! $primary_reg) {
1090
+			EE_Error::add_error(
1091
+				__(
1092
+					'Cannot get billing info for gateway %s on transaction because no primary registration exists',
1093
+					'event_espresso'
1094
+				),
1095
+				__FILE__,
1096
+				__FUNCTION__,
1097
+				__LINE__
1098
+			);
1099
+			return null;
1100
+		}
1101
+		$attendee = $primary_reg->attendee();
1102
+		if (! $attendee) {
1103
+			EE_Error::add_error(
1104
+				__(
1105
+					'Cannot get billing info for gateway %s on transaction because the primary registration has no attendee exists',
1106
+					'event_espresso'
1107
+				),
1108
+				__FILE__,
1109
+				__FUNCTION__,
1110
+				__LINE__
1111
+			);
1112
+			return null;
1113
+		}
1114
+		return $attendee->billing_info_for_payment_method($payment_method);
1115
+	}
1116
+
1117
+
1118
+	/**
1119
+	 * Gets PMD_ID
1120
+	 *
1121
+	 * @return int
1122
+	 * @throws EE_Error
1123
+	 * @throws InvalidArgumentException
1124
+	 * @throws InvalidDataTypeException
1125
+	 * @throws InvalidInterfaceException
1126
+	 * @throws ReflectionException
1127
+	 */
1128
+	public function payment_method_ID()
1129
+	{
1130
+		return $this->get('PMD_ID');
1131
+	}
1132
+
1133
+
1134
+	/**
1135
+	 * Sets PMD_ID
1136
+	 *
1137
+	 * @param int $PMD_ID
1138
+	 * @throws EE_Error
1139
+	 * @throws InvalidArgumentException
1140
+	 * @throws InvalidDataTypeException
1141
+	 * @throws InvalidInterfaceException
1142
+	 * @throws ReflectionException
1143
+	 */
1144
+	public function set_payment_method_ID($PMD_ID)
1145
+	{
1146
+		$this->set('PMD_ID', $PMD_ID);
1147
+	}
1148
+
1149
+
1150
+	/**
1151
+	 * Gets the last-used payment method on this transaction
1152
+	 * (we COULD just use the last-made payment, but some payment methods, namely
1153
+	 * offline ones, dont' create payments)
1154
+	 *
1155
+	 * @return EE_Payment_Method
1156
+	 * @throws EE_Error
1157
+	 * @throws InvalidArgumentException
1158
+	 * @throws InvalidDataTypeException
1159
+	 * @throws InvalidInterfaceException
1160
+	 * @throws ReflectionException
1161
+	 */
1162
+	public function payment_method()
1163
+	{
1164
+		$pm = $this->get_first_related('Payment_Method');
1165
+		if ($pm instanceof EE_Payment_Method) {
1166
+			return $pm;
1167
+		}
1168
+		$last_payment = $this->last_payment();
1169
+		if ($last_payment instanceof EE_Payment && $last_payment->payment_method()) {
1170
+			return $last_payment->payment_method();
1171
+		}
1172
+		return null;
1173
+	}
1174
+
1175
+
1176
+	/**
1177
+	 * Gets the last payment made
1178
+	 *
1179
+	 * @return EE_Base_Class|EE_Payment
1180
+	 * @throws EE_Error
1181
+	 * @throws InvalidArgumentException
1182
+	 * @throws InvalidDataTypeException
1183
+	 * @throws InvalidInterfaceException
1184
+	 * @throws ReflectionException
1185
+	 */
1186
+	public function last_payment()
1187
+	{
1188
+		return $this->get_first_related('Payment', array('order_by' => array('PAY_ID' => 'desc')));
1189
+	}
1190
+
1191
+
1192
+	/**
1193
+	 * Gets all the line items which are unrelated to tickets on this transaction
1194
+	 *
1195
+	 * @return EE_Line_Item[]
1196
+	 * @throws EE_Error
1197
+	 * @throws InvalidArgumentException
1198
+	 * @throws InvalidDataTypeException
1199
+	 * @throws InvalidInterfaceException
1200
+	 * @throws ReflectionException
1201
+	 */
1202
+	public function non_ticket_line_items()
1203
+	{
1204
+		return EEM_Line_Item::instance()->get_all_non_ticket_line_items_for_transaction($this->ID());
1205
+	}
1206
+
1207
+
1208
+	/**
1209
+	 * possibly toggles TXN status
1210
+	 *
1211
+	 * @param  boolean $update whether to save the TXN
1212
+	 * @return bool whether the TXN was saved
1213
+	 * @throws EE_Error
1214
+	 * @throws InvalidArgumentException
1215
+	 * @throws InvalidDataTypeException
1216
+	 * @throws InvalidInterfaceException
1217
+	 * @throws ReflectionException
1218
+	 * @throws RuntimeException
1219
+	 */
1220
+	public function update_status_based_on_total_paid($update = true)
1221
+	{
1222
+		// set transaction status based on comparison of TXN_paid vs TXN_total
1223
+		if (EEH_Money::compare_floats($this->paid(), $this->total(), '>')) {
1224
+			$new_txn_status = EEM_Transaction::overpaid_status_code;
1225
+		} elseif (EEH_Money::compare_floats($this->paid(), $this->total())) {
1226
+			$new_txn_status = EEM_Transaction::complete_status_code;
1227
+		} elseif (EEH_Money::compare_floats($this->paid(), $this->total(), '<')) {
1228
+			$new_txn_status = EEM_Transaction::incomplete_status_code;
1229
+		} else {
1230
+			throw new RuntimeException(
1231
+				__('The total paid calculation for this transaction is inaccurate.', 'event_espresso')
1232
+			);
1233
+		}
1234
+		if ($new_txn_status !== $this->status_ID()) {
1235
+			$this->set_status($new_txn_status);
1236
+			if ($update) {
1237
+				return $this->save() ? true : false;
1238
+			}
1239
+		}
1240
+		return false;
1241
+	}
1242
+
1243
+
1244
+	/**
1245
+	 * Updates the transaction's status and total_paid based on all the payments
1246
+	 * that apply to it
1247
+	 *
1248
+	 * @deprecated
1249
+	 * @return array|bool
1250
+	 * @throws EE_Error
1251
+	 * @throws InvalidArgumentException
1252
+	 * @throws ReflectionException
1253
+	 * @throws InvalidDataTypeException
1254
+	 * @throws InvalidInterfaceException
1255
+	 */
1256
+	public function update_based_on_payments()
1257
+	{
1258
+		EE_Error::doing_it_wrong(
1259
+			__CLASS__ . '::' . __FUNCTION__,
1260
+			sprintf(
1261
+				__('This method is deprecated. Please use "%s" instead', 'event_espresso'),
1262
+				'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()'
1263
+			),
1264
+			'4.6.0'
1265
+		);
1266
+		/** @type EE_Transaction_Processor $transaction_processor */
1267
+		$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
1268
+		return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment($this);
1269
+	}
1270
+
1271
+
1272
+	/**
1273
+	 * @return string
1274
+	 */
1275
+	public function old_txn_status()
1276
+	{
1277
+		return $this->_old_txn_status;
1278
+	}
1279
+
1280
+
1281
+	/**
1282
+	 * @param string $old_txn_status
1283
+	 */
1284
+	public function set_old_txn_status($old_txn_status)
1285
+	{
1286
+		// only set the first time
1287
+		if ($this->_old_txn_status === null) {
1288
+			$this->_old_txn_status = $old_txn_status;
1289
+		}
1290
+	}
1291
+
1292
+
1293
+	/**
1294
+	 * reg_status_updated
1295
+	 *
1296
+	 * @return bool
1297
+	 * @throws EE_Error
1298
+	 * @throws InvalidArgumentException
1299
+	 * @throws InvalidDataTypeException
1300
+	 * @throws InvalidInterfaceException
1301
+	 * @throws ReflectionException
1302
+	 */
1303
+	public function txn_status_updated()
1304
+	{
1305
+		return $this->status_ID() !== $this->_old_txn_status && $this->_old_txn_status !== null;
1306
+	}
1307
+
1308
+
1309
+	/**
1310
+	 * _reg_steps_completed
1311
+	 * if $check_all is TRUE, then returns TRUE if ALL reg steps have been marked as completed,
1312
+	 * if a $reg_step_slug is provided, then this step will be skipped when testing for completion
1313
+	 * if $check_all is FALSE and a $reg_step_slug is provided, then ONLY that reg step will be tested for completion
1314
+	 *
1315
+	 * @param string $reg_step_slug
1316
+	 * @param bool   $check_all
1317
+	 * @return bool|int
1318
+	 * @throws EE_Error
1319
+	 * @throws InvalidArgumentException
1320
+	 * @throws InvalidDataTypeException
1321
+	 * @throws InvalidInterfaceException
1322
+	 * @throws ReflectionException
1323
+	 */
1324
+	private function _reg_steps_completed($reg_step_slug = '', $check_all = true)
1325
+	{
1326
+		$reg_steps = $this->reg_steps();
1327
+		if (! is_array($reg_steps) || empty($reg_steps)) {
1328
+			return false;
1329
+		}
1330
+		// loop thru reg steps array)
1331
+		foreach ($reg_steps as $slug => $reg_step_completed) {
1332
+			// if NOT checking ALL steps (only checking one step)
1333
+			if (! $check_all) {
1334
+				// and this is the one
1335
+				if ($slug === $reg_step_slug) {
1336
+					return $reg_step_completed;
1337
+				}
1338
+				// skip to next reg step in loop
1339
+				continue;
1340
+			}
1341
+			// $check_all must be true, else we would never have gotten to this point
1342
+			if ($slug === $reg_step_slug) {
1343
+				// if we reach this point, then we are testing either:
1344
+				// all_reg_steps_completed_except() or
1345
+				// all_reg_steps_completed_except_final_step(),
1346
+				// and since this is the reg step EXCEPTION being tested
1347
+				// we want to return true (yes true) if this reg step is NOT completed
1348
+				// ie: "is everything completed except the final step?"
1349
+				// "that is correct... the final step is not completed, but all others are."
1350
+				return $reg_step_completed !== true;
1351
+			}
1352
+			if ($reg_step_completed !== true) {
1353
+				// if any reg step is NOT completed, then ALL steps are not completed
1354
+				return false;
1355
+			}
1356
+		}
1357
+		return true;
1358
+	}
1359
+
1360
+
1361
+	/**
1362
+	 * all_reg_steps_completed
1363
+	 * returns:
1364
+	 *    true if ALL reg steps have been marked as completed
1365
+	 *        or false if any step is not completed
1366
+	 *
1367
+	 * @return bool
1368
+	 * @throws EE_Error
1369
+	 * @throws InvalidArgumentException
1370
+	 * @throws InvalidDataTypeException
1371
+	 * @throws InvalidInterfaceException
1372
+	 * @throws ReflectionException
1373
+	 */
1374
+	public function all_reg_steps_completed()
1375
+	{
1376
+		return $this->_reg_steps_completed();
1377
+	}
1378
+
1379
+
1380
+	/**
1381
+	 * all_reg_steps_completed_except
1382
+	 * returns:
1383
+	 *        true if ALL reg steps, except a particular step that you wish to skip over, have been marked as completed
1384
+	 *        or false if any other step is not completed
1385
+	 *        or false if ALL steps are completed including the exception you are testing !!!
1386
+	 *
1387
+	 * @param string $exception
1388
+	 * @return bool
1389
+	 * @throws EE_Error
1390
+	 * @throws InvalidArgumentException
1391
+	 * @throws InvalidDataTypeException
1392
+	 * @throws InvalidInterfaceException
1393
+	 * @throws ReflectionException
1394
+	 */
1395
+	public function all_reg_steps_completed_except($exception = '')
1396
+	{
1397
+		return $this->_reg_steps_completed($exception);
1398
+	}
1399
+
1400
+
1401
+	/**
1402
+	 * all_reg_steps_completed_except
1403
+	 * returns:
1404
+	 *        true if ALL reg steps, except the final step, have been marked as completed
1405
+	 *        or false if any step is not completed
1406
+	 *    or false if ALL steps are completed including the final step !!!
1407
+	 *
1408
+	 * @return bool
1409
+	 * @throws EE_Error
1410
+	 * @throws InvalidArgumentException
1411
+	 * @throws InvalidDataTypeException
1412
+	 * @throws InvalidInterfaceException
1413
+	 * @throws ReflectionException
1414
+	 */
1415
+	public function all_reg_steps_completed_except_final_step()
1416
+	{
1417
+		return $this->_reg_steps_completed('finalize_registration');
1418
+	}
1419
+
1420
+
1421
+	/**
1422
+	 * reg_step_completed
1423
+	 * returns:
1424
+	 *    true if a specific reg step has been marked as completed
1425
+	 *    a Unix timestamp if it has been initialized but not yet completed,
1426
+	 *    or false if it has not yet been initialized
1427
+	 *
1428
+	 * @param string $reg_step_slug
1429
+	 * @return bool|int
1430
+	 * @throws EE_Error
1431
+	 * @throws InvalidArgumentException
1432
+	 * @throws InvalidDataTypeException
1433
+	 * @throws InvalidInterfaceException
1434
+	 * @throws ReflectionException
1435
+	 */
1436
+	public function reg_step_completed($reg_step_slug)
1437
+	{
1438
+		return $this->_reg_steps_completed($reg_step_slug, false);
1439
+	}
1440
+
1441
+
1442
+	/**
1443
+	 * completed_final_reg_step
1444
+	 * returns:
1445
+	 *    true if the finalize_registration reg step has been marked as completed
1446
+	 *    a Unix timestamp if it has been initialized but not yet completed,
1447
+	 *    or false if it has not yet been initialized
1448
+	 *
1449
+	 * @return bool|int
1450
+	 * @throws EE_Error
1451
+	 * @throws InvalidArgumentException
1452
+	 * @throws InvalidDataTypeException
1453
+	 * @throws InvalidInterfaceException
1454
+	 * @throws ReflectionException
1455
+	 */
1456
+	public function final_reg_step_completed()
1457
+	{
1458
+		return $this->_reg_steps_completed('finalize_registration', false);
1459
+	}
1460
+
1461
+
1462
+	/**
1463
+	 * set_reg_step_initiated
1464
+	 * given a valid TXN_reg_step, this sets it's value to a unix timestamp
1465
+	 *
1466
+	 * @param string $reg_step_slug
1467
+	 * @return boolean
1468
+	 * @throws EE_Error
1469
+	 * @throws InvalidArgumentException
1470
+	 * @throws InvalidDataTypeException
1471
+	 * @throws InvalidInterfaceException
1472
+	 * @throws ReflectionException
1473
+	 */
1474
+	public function set_reg_step_initiated($reg_step_slug)
1475
+	{
1476
+		return $this->_set_reg_step_completed_status($reg_step_slug, time());
1477
+	}
1478
+
1479
+
1480
+	/**
1481
+	 * set_reg_step_completed
1482
+	 * given a valid TXN_reg_step, this sets the step as completed
1483
+	 *
1484
+	 * @param string $reg_step_slug
1485
+	 * @return boolean
1486
+	 * @throws EE_Error
1487
+	 * @throws InvalidArgumentException
1488
+	 * @throws InvalidDataTypeException
1489
+	 * @throws InvalidInterfaceException
1490
+	 * @throws ReflectionException
1491
+	 */
1492
+	public function set_reg_step_completed($reg_step_slug)
1493
+	{
1494
+		return $this->_set_reg_step_completed_status($reg_step_slug, true);
1495
+	}
1496
+
1497
+
1498
+	/**
1499
+	 * set_reg_step_completed
1500
+	 * given a valid TXN_reg_step slug, this sets the step as NOT completed
1501
+	 *
1502
+	 * @param string $reg_step_slug
1503
+	 * @return boolean
1504
+	 * @throws EE_Error
1505
+	 * @throws InvalidArgumentException
1506
+	 * @throws InvalidDataTypeException
1507
+	 * @throws InvalidInterfaceException
1508
+	 * @throws ReflectionException
1509
+	 */
1510
+	public function set_reg_step_not_completed($reg_step_slug)
1511
+	{
1512
+		return $this->_set_reg_step_completed_status($reg_step_slug, false);
1513
+	}
1514
+
1515
+
1516
+	/**
1517
+	 * set_reg_step_completed
1518
+	 * given a valid reg step slug, this sets the TXN_reg_step completed status which is either:
1519
+	 *
1520
+	 * @param  string      $reg_step_slug
1521
+	 * @param  boolean|int $status
1522
+	 * @return boolean
1523
+	 * @throws EE_Error
1524
+	 * @throws InvalidArgumentException
1525
+	 * @throws InvalidDataTypeException
1526
+	 * @throws InvalidInterfaceException
1527
+	 * @throws ReflectionException
1528
+	 */
1529
+	private function _set_reg_step_completed_status($reg_step_slug, $status)
1530
+	{
1531
+		// validate status
1532
+		$status = is_bool($status) || is_int($status) ? $status : false;
1533
+		// get reg steps array
1534
+		$txn_reg_steps = $this->reg_steps();
1535
+		// if reg step does NOT exist
1536
+		if (! isset($txn_reg_steps[ $reg_step_slug ])) {
1537
+			return false;
1538
+		}
1539
+		// if  we're trying to complete a step that is already completed
1540
+		if ($txn_reg_steps[ $reg_step_slug ] === true) {
1541
+			return true;
1542
+		}
1543
+		// if  we're trying to complete a step that hasn't even started
1544
+		if ($status === true && $txn_reg_steps[ $reg_step_slug ] === false) {
1545
+			return false;
1546
+		}
1547
+		// if current status value matches the incoming value (no change)
1548
+		// type casting as int means values should collapse to either 0, 1, or a timestamp like 1234567890
1549
+		if ((int) $txn_reg_steps[ $reg_step_slug ] === (int) $status) {
1550
+			// this will happen in cases where multiple AJAX requests occur during the same step
1551
+			return true;
1552
+		}
1553
+		// if we're trying to set a start time, but it has already been set...
1554
+		if (is_numeric($status) && is_numeric($txn_reg_steps[ $reg_step_slug ])) {
1555
+			// skip the update below, but don't return FALSE so that errors won't be displayed
1556
+			return true;
1557
+		}
1558
+		// update completed status
1559
+		$txn_reg_steps[ $reg_step_slug ] = $status;
1560
+		$this->set_reg_steps($txn_reg_steps);
1561
+		$this->save();
1562
+		return true;
1563
+	}
1564
+
1565
+
1566
+	/**
1567
+	 * remove_reg_step
1568
+	 * given a valid TXN_reg_step slug, this will remove (unset)
1569
+	 * the reg step from the TXN reg step array
1570
+	 *
1571
+	 * @param string $reg_step_slug
1572
+	 * @return void
1573
+	 * @throws EE_Error
1574
+	 * @throws InvalidArgumentException
1575
+	 * @throws InvalidDataTypeException
1576
+	 * @throws InvalidInterfaceException
1577
+	 * @throws ReflectionException
1578
+	 */
1579
+	public function remove_reg_step($reg_step_slug)
1580
+	{
1581
+		// get reg steps array
1582
+		$txn_reg_steps = $this->reg_steps();
1583
+		unset($txn_reg_steps[ $reg_step_slug ]);
1584
+		$this->set_reg_steps($txn_reg_steps);
1585
+	}
1586
+
1587
+
1588
+	/**
1589
+	 * toggle_failed_transaction_status
1590
+	 * upgrades a TXNs status from failed to abandoned,
1591
+	 * meaning that contact information has been captured for at least one registrant
1592
+	 *
1593
+	 * @param bool $save
1594
+	 * @return bool
1595
+	 * @throws EE_Error
1596
+	 * @throws InvalidArgumentException
1597
+	 * @throws InvalidDataTypeException
1598
+	 * @throws InvalidInterfaceException
1599
+	 * @throws ReflectionException
1600
+	 */
1601
+	public function toggle_failed_transaction_status($save = true)
1602
+	{
1603
+		// if TXN status is still set as "failed"...
1604
+		if ($this->status_ID() === EEM_Transaction::failed_status_code) {
1605
+			$this->set_status(EEM_Transaction::abandoned_status_code);
1606
+			if ($save) {
1607
+				$this->save();
1608
+			}
1609
+			return true;
1610
+		}
1611
+		return false;
1612
+	}
1613
+
1614
+
1615
+	/**
1616
+	 * toggle_abandoned_transaction_status
1617
+	 * upgrades a TXNs status from failed or abandoned to incomplete
1618
+	 *
1619
+	 * @return bool
1620
+	 * @throws EE_Error
1621
+	 * @throws InvalidArgumentException
1622
+	 * @throws InvalidDataTypeException
1623
+	 * @throws InvalidInterfaceException
1624
+	 * @throws ReflectionException
1625
+	 */
1626
+	public function toggle_abandoned_transaction_status()
1627
+	{
1628
+		// if TXN status has not been updated already due to a payment, and is still set as "failed" or "abandoned"...
1629
+		$txn_status = $this->status_ID();
1630
+		if ($txn_status === EEM_Transaction::failed_status_code
1631
+			|| $txn_status === EEM_Transaction::abandoned_status_code
1632
+		) {
1633
+			// if a contact record for the primary registrant has been created
1634
+			if ($this->primary_registration() instanceof EE_Registration
1635
+				&& $this->primary_registration()->attendee() instanceof EE_Attendee
1636
+			) {
1637
+				$this->set_status(EEM_Transaction::incomplete_status_code);
1638
+			} else {
1639
+				// no contact record? yer abandoned!
1640
+				$this->set_status(EEM_Transaction::abandoned_status_code);
1641
+			}
1642
+			return true;
1643
+		}
1644
+		return false;
1645
+	}
1646
+
1647
+
1648
+	/**
1649
+	 * checks if an Abandoned TXN has any related payments, and if so,
1650
+	 * updates the TXN status based on the amount paid
1651
+	 *
1652
+	 * @throws EE_Error
1653
+	 * @throws InvalidDataTypeException
1654
+	 * @throws InvalidInterfaceException
1655
+	 * @throws InvalidArgumentException
1656
+	 * @throws RuntimeException
1657
+	 * @throws ReflectionException
1658
+	 */
1659
+	public function verify_abandoned_transaction_status()
1660
+	{
1661
+		if ($this->status_ID() !== EEM_Transaction::abandoned_status_code) {
1662
+			return;
1663
+		}
1664
+		$payments = $this->get_many_related('Payment');
1665
+		if (! empty($payments)) {
1666
+			foreach ($payments as $payment) {
1667
+				if ($payment instanceof EE_Payment) {
1668
+					// kk this TXN should NOT be abandoned
1669
+					$this->update_status_based_on_total_paid();
1670
+					if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) {
1671
+						EE_Error::add_attention(
1672
+							sprintf(
1673
+								esc_html__(
1674
+									'The status for Transaction #%1$d has been updated from "Abandoned" to "%2$s", because at least one payment has been made towards it. If the payment appears in the "Payment Details" table below, you may need to edit its status and/or other details as well.',
1675
+									'event_espresso'
1676
+								),
1677
+								$this->ID(),
1678
+								$this->pretty_status()
1679
+							)
1680
+						);
1681
+					}
1682
+					// get final reg step status
1683
+					$finalized = $this->final_reg_step_completed();
1684
+					// if the 'finalize_registration' step has been initiated (has a timestamp)
1685
+					// but has not yet been fully completed (TRUE)
1686
+					if (is_int($finalized) && $finalized !== false && $finalized !== true) {
1687
+						$this->set_reg_step_completed('finalize_registration');
1688
+						$this->save();
1689
+					}
1690
+				}
1691
+			}
1692
+		}
1693
+	}
1694 1694
 }
Please login to merge, or discard this patch.
admin/extend/registrations/Extend_Registrations_Admin_Page.core.php 2 patches
Indentation   +1202 added lines, -1202 removed lines patch added patch discarded remove patch
@@ -16,1259 +16,1259 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * This is used to hold the reports template data which is setup early in the request.
21
-     *
22
-     * @type array
23
-     */
24
-    protected $_reports_template_data = array();
19
+	/**
20
+	 * This is used to hold the reports template data which is setup early in the request.
21
+	 *
22
+	 * @type array
23
+	 */
24
+	protected $_reports_template_data = array();
25 25
 
26 26
 
27
-    /**
28
-     * Extend_Registrations_Admin_Page constructor.
29
-     *
30
-     * @param bool $routing
31
-     */
32
-    public function __construct($routing = true)
33
-    {
34
-        parent::__construct($routing);
35
-        if (! defined('REG_CAF_TEMPLATE_PATH')) {
36
-            define('REG_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/templates/');
37
-            define('REG_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/assets/');
38
-            define('REG_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'registrations/assets/');
39
-        }
40
-    }
27
+	/**
28
+	 * Extend_Registrations_Admin_Page constructor.
29
+	 *
30
+	 * @param bool $routing
31
+	 */
32
+	public function __construct($routing = true)
33
+	{
34
+		parent::__construct($routing);
35
+		if (! defined('REG_CAF_TEMPLATE_PATH')) {
36
+			define('REG_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/templates/');
37
+			define('REG_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/assets/');
38
+			define('REG_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'registrations/assets/');
39
+		}
40
+	}
41 41
 
42 42
 
43
-    /**
44
-     * Extending page configuration.
45
-     */
46
-    protected function _extend_page_config()
47
-    {
48
-        $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'registrations';
49
-        $reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID'])
50
-            ? $this->_req_data['_REG_ID']
51
-            : 0;
52
-        $new_page_routes = array(
53
-            'reports'                      => array(
54
-                'func'       => '_registration_reports',
55
-                'capability' => 'ee_read_registrations',
56
-            ),
57
-            'registration_checkins'        => array(
58
-                'func'       => '_registration_checkin_list_table',
59
-                'capability' => 'ee_read_checkins',
60
-            ),
61
-            'newsletter_selected_send'     => array(
62
-                'func'       => '_newsletter_selected_send',
63
-                'noheader'   => true,
64
-                'capability' => 'ee_send_message',
65
-            ),
66
-            'delete_checkin_rows'          => array(
67
-                'func'       => '_delete_checkin_rows',
68
-                'noheader'   => true,
69
-                'capability' => 'ee_delete_checkins',
70
-            ),
71
-            'delete_checkin_row'           => array(
72
-                'func'       => '_delete_checkin_row',
73
-                'noheader'   => true,
74
-                'capability' => 'ee_delete_checkin',
75
-                'obj_id'     => $reg_id,
76
-            ),
77
-            'toggle_checkin_status'        => array(
78
-                'func'       => '_toggle_checkin_status',
79
-                'noheader'   => true,
80
-                'capability' => 'ee_edit_checkin',
81
-                'obj_id'     => $reg_id,
82
-            ),
83
-            'toggle_checkin_status_bulk'   => array(
84
-                'func'       => '_toggle_checkin_status',
85
-                'noheader'   => true,
86
-                'capability' => 'ee_edit_checkins',
87
-            ),
88
-            'event_registrations'          => array(
89
-                'func'       => '_event_registrations_list_table',
90
-                'capability' => 'ee_read_checkins',
91
-            ),
92
-            'registrations_checkin_report' => array(
93
-                'func'       => '_registrations_checkin_report',
94
-                'noheader'   => true,
95
-                'capability' => 'ee_read_registrations',
96
-            ),
97
-        );
98
-        $this->_page_routes = array_merge($this->_page_routes, $new_page_routes);
99
-        $new_page_config = array(
100
-            'reports'               => array(
101
-                'nav'           => array(
102
-                    'label' => esc_html__('Reports', 'event_espresso'),
103
-                    'order' => 30,
104
-                ),
105
-                'help_tabs'     => array(
106
-                    'registrations_reports_help_tab' => array(
107
-                        'title'    => esc_html__('Registration Reports', 'event_espresso'),
108
-                        'filename' => 'registrations_reports',
109
-                    ),
110
-                ),
111
-                /*'help_tour' => array( 'Registration_Reports_Help_Tour' ),*/
112
-                'require_nonce' => false,
113
-            ),
114
-            'event_registrations'   => array(
115
-                'nav'           => array(
116
-                    'label'      => esc_html__('Event Check-In', 'event_espresso'),
117
-                    'order'      => 10,
118
-                    'persistent' => true,
119
-                ),
120
-                'help_tabs'     => array(
121
-                    'registrations_event_checkin_help_tab'                       => array(
122
-                        'title'    => esc_html__('Registrations Event Check-In', 'event_espresso'),
123
-                        'filename' => 'registrations_event_checkin',
124
-                    ),
125
-                    'registrations_event_checkin_table_column_headings_help_tab' => array(
126
-                        'title'    => esc_html__('Event Check-In Table Column Headings', 'event_espresso'),
127
-                        'filename' => 'registrations_event_checkin_table_column_headings',
128
-                    ),
129
-                    'registrations_event_checkin_filters_help_tab'               => array(
130
-                        'title'    => esc_html__('Event Check-In Filters', 'event_espresso'),
131
-                        'filename' => 'registrations_event_checkin_filters',
132
-                    ),
133
-                    'registrations_event_checkin_views_help_tab'                 => array(
134
-                        'title'    => esc_html__('Event Check-In Views', 'event_espresso'),
135
-                        'filename' => 'registrations_event_checkin_views',
136
-                    ),
137
-                    'registrations_event_checkin_other_help_tab'                 => array(
138
-                        'title'    => esc_html__('Event Check-In Other', 'event_espresso'),
139
-                        'filename' => 'registrations_event_checkin_other',
140
-                    ),
141
-                ),
142
-                'help_tour'     => array('Event_Checkin_Help_Tour'),
143
-                'qtips'         => array('Registration_List_Table_Tips'),
144
-                'list_table'    => 'EE_Event_Registrations_List_Table',
145
-                'metaboxes'     => array(),
146
-                'require_nonce' => false,
147
-            ),
148
-            'registration_checkins' => array(
149
-                'nav'           => array(
150
-                    'label'      => esc_html__('Check-In Records', 'event_espresso'),
151
-                    'order'      => 15,
152
-                    'persistent' => false,
153
-                    'url'        => '',
154
-                ),
155
-                'list_table'    => 'EE_Registration_CheckIn_List_Table',
156
-                // 'help_tour' => array( 'Checkin_Toggle_View_Help_Tour' ),
157
-                'metaboxes'     => array(),
158
-                'require_nonce' => false,
159
-            ),
160
-        );
161
-        $this->_page_config = array_merge($this->_page_config, $new_page_config);
162
-        $this->_page_config['contact_list']['list_table'] = 'Extend_EE_Attendee_Contact_List_Table';
163
-        $this->_page_config['default']['list_table'] = 'Extend_EE_Registrations_List_Table';
164
-    }
43
+	/**
44
+	 * Extending page configuration.
45
+	 */
46
+	protected function _extend_page_config()
47
+	{
48
+		$this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'registrations';
49
+		$reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID'])
50
+			? $this->_req_data['_REG_ID']
51
+			: 0;
52
+		$new_page_routes = array(
53
+			'reports'                      => array(
54
+				'func'       => '_registration_reports',
55
+				'capability' => 'ee_read_registrations',
56
+			),
57
+			'registration_checkins'        => array(
58
+				'func'       => '_registration_checkin_list_table',
59
+				'capability' => 'ee_read_checkins',
60
+			),
61
+			'newsletter_selected_send'     => array(
62
+				'func'       => '_newsletter_selected_send',
63
+				'noheader'   => true,
64
+				'capability' => 'ee_send_message',
65
+			),
66
+			'delete_checkin_rows'          => array(
67
+				'func'       => '_delete_checkin_rows',
68
+				'noheader'   => true,
69
+				'capability' => 'ee_delete_checkins',
70
+			),
71
+			'delete_checkin_row'           => array(
72
+				'func'       => '_delete_checkin_row',
73
+				'noheader'   => true,
74
+				'capability' => 'ee_delete_checkin',
75
+				'obj_id'     => $reg_id,
76
+			),
77
+			'toggle_checkin_status'        => array(
78
+				'func'       => '_toggle_checkin_status',
79
+				'noheader'   => true,
80
+				'capability' => 'ee_edit_checkin',
81
+				'obj_id'     => $reg_id,
82
+			),
83
+			'toggle_checkin_status_bulk'   => array(
84
+				'func'       => '_toggle_checkin_status',
85
+				'noheader'   => true,
86
+				'capability' => 'ee_edit_checkins',
87
+			),
88
+			'event_registrations'          => array(
89
+				'func'       => '_event_registrations_list_table',
90
+				'capability' => 'ee_read_checkins',
91
+			),
92
+			'registrations_checkin_report' => array(
93
+				'func'       => '_registrations_checkin_report',
94
+				'noheader'   => true,
95
+				'capability' => 'ee_read_registrations',
96
+			),
97
+		);
98
+		$this->_page_routes = array_merge($this->_page_routes, $new_page_routes);
99
+		$new_page_config = array(
100
+			'reports'               => array(
101
+				'nav'           => array(
102
+					'label' => esc_html__('Reports', 'event_espresso'),
103
+					'order' => 30,
104
+				),
105
+				'help_tabs'     => array(
106
+					'registrations_reports_help_tab' => array(
107
+						'title'    => esc_html__('Registration Reports', 'event_espresso'),
108
+						'filename' => 'registrations_reports',
109
+					),
110
+				),
111
+				/*'help_tour' => array( 'Registration_Reports_Help_Tour' ),*/
112
+				'require_nonce' => false,
113
+			),
114
+			'event_registrations'   => array(
115
+				'nav'           => array(
116
+					'label'      => esc_html__('Event Check-In', 'event_espresso'),
117
+					'order'      => 10,
118
+					'persistent' => true,
119
+				),
120
+				'help_tabs'     => array(
121
+					'registrations_event_checkin_help_tab'                       => array(
122
+						'title'    => esc_html__('Registrations Event Check-In', 'event_espresso'),
123
+						'filename' => 'registrations_event_checkin',
124
+					),
125
+					'registrations_event_checkin_table_column_headings_help_tab' => array(
126
+						'title'    => esc_html__('Event Check-In Table Column Headings', 'event_espresso'),
127
+						'filename' => 'registrations_event_checkin_table_column_headings',
128
+					),
129
+					'registrations_event_checkin_filters_help_tab'               => array(
130
+						'title'    => esc_html__('Event Check-In Filters', 'event_espresso'),
131
+						'filename' => 'registrations_event_checkin_filters',
132
+					),
133
+					'registrations_event_checkin_views_help_tab'                 => array(
134
+						'title'    => esc_html__('Event Check-In Views', 'event_espresso'),
135
+						'filename' => 'registrations_event_checkin_views',
136
+					),
137
+					'registrations_event_checkin_other_help_tab'                 => array(
138
+						'title'    => esc_html__('Event Check-In Other', 'event_espresso'),
139
+						'filename' => 'registrations_event_checkin_other',
140
+					),
141
+				),
142
+				'help_tour'     => array('Event_Checkin_Help_Tour'),
143
+				'qtips'         => array('Registration_List_Table_Tips'),
144
+				'list_table'    => 'EE_Event_Registrations_List_Table',
145
+				'metaboxes'     => array(),
146
+				'require_nonce' => false,
147
+			),
148
+			'registration_checkins' => array(
149
+				'nav'           => array(
150
+					'label'      => esc_html__('Check-In Records', 'event_espresso'),
151
+					'order'      => 15,
152
+					'persistent' => false,
153
+					'url'        => '',
154
+				),
155
+				'list_table'    => 'EE_Registration_CheckIn_List_Table',
156
+				// 'help_tour' => array( 'Checkin_Toggle_View_Help_Tour' ),
157
+				'metaboxes'     => array(),
158
+				'require_nonce' => false,
159
+			),
160
+		);
161
+		$this->_page_config = array_merge($this->_page_config, $new_page_config);
162
+		$this->_page_config['contact_list']['list_table'] = 'Extend_EE_Attendee_Contact_List_Table';
163
+		$this->_page_config['default']['list_table'] = 'Extend_EE_Registrations_List_Table';
164
+	}
165 165
 
166 166
 
167
-    /**
168
-     * Ajax hooks for all routes in this page.
169
-     */
170
-    protected function _ajax_hooks()
171
-    {
172
-        parent::_ajax_hooks();
173
-        add_action('wp_ajax_get_newsletter_form_content', array($this, 'get_newsletter_form_content'));
174
-    }
167
+	/**
168
+	 * Ajax hooks for all routes in this page.
169
+	 */
170
+	protected function _ajax_hooks()
171
+	{
172
+		parent::_ajax_hooks();
173
+		add_action('wp_ajax_get_newsletter_form_content', array($this, 'get_newsletter_form_content'));
174
+	}
175 175
 
176 176
 
177
-    /**
178
-     * Global scripts for all routes in this page.
179
-     */
180
-    public function load_scripts_styles()
181
-    {
182
-        parent::load_scripts_styles();
183
-        // if newsletter message type is active then let's add filter and load js for it.
184
-        if (EEH_MSG_Template::is_mt_active('newsletter')) {
185
-            // enqueue newsletter js
186
-            wp_enqueue_script(
187
-                'ee-newsletter-trigger',
188
-                REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.js',
189
-                array('ee-dialog'),
190
-                EVENT_ESPRESSO_VERSION,
191
-                true
192
-            );
193
-            wp_enqueue_style(
194
-                'ee-newsletter-trigger-css',
195
-                REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.css',
196
-                array(),
197
-                EVENT_ESPRESSO_VERSION
198
-            );
199
-            // hook in buttons for newsletter message type trigger.
200
-            add_action(
201
-                'AHEE__EE_Admin_List_Table__extra_tablenav__after_bottom_buttons',
202
-                array($this, 'add_newsletter_action_buttons'),
203
-                10
204
-            );
205
-        }
206
-    }
177
+	/**
178
+	 * Global scripts for all routes in this page.
179
+	 */
180
+	public function load_scripts_styles()
181
+	{
182
+		parent::load_scripts_styles();
183
+		// if newsletter message type is active then let's add filter and load js for it.
184
+		if (EEH_MSG_Template::is_mt_active('newsletter')) {
185
+			// enqueue newsletter js
186
+			wp_enqueue_script(
187
+				'ee-newsletter-trigger',
188
+				REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.js',
189
+				array('ee-dialog'),
190
+				EVENT_ESPRESSO_VERSION,
191
+				true
192
+			);
193
+			wp_enqueue_style(
194
+				'ee-newsletter-trigger-css',
195
+				REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.css',
196
+				array(),
197
+				EVENT_ESPRESSO_VERSION
198
+			);
199
+			// hook in buttons for newsletter message type trigger.
200
+			add_action(
201
+				'AHEE__EE_Admin_List_Table__extra_tablenav__after_bottom_buttons',
202
+				array($this, 'add_newsletter_action_buttons'),
203
+				10
204
+			);
205
+		}
206
+	}
207 207
 
208 208
 
209
-    /**
210
-     * Scripts and styles for just the reports route.
211
-     */
212
-    public function load_scripts_styles_reports()
213
-    {
214
-        wp_register_script(
215
-            'ee-reg-reports-js',
216
-            REG_CAF_ASSETS_URL . 'ee-registration-admin-reports.js',
217
-            array('google-charts'),
218
-            EVENT_ESPRESSO_VERSION,
219
-            true
220
-        );
221
-        wp_enqueue_script('ee-reg-reports-js');
222
-        $this->_registration_reports_js_setup();
223
-    }
209
+	/**
210
+	 * Scripts and styles for just the reports route.
211
+	 */
212
+	public function load_scripts_styles_reports()
213
+	{
214
+		wp_register_script(
215
+			'ee-reg-reports-js',
216
+			REG_CAF_ASSETS_URL . 'ee-registration-admin-reports.js',
217
+			array('google-charts'),
218
+			EVENT_ESPRESSO_VERSION,
219
+			true
220
+		);
221
+		wp_enqueue_script('ee-reg-reports-js');
222
+		$this->_registration_reports_js_setup();
223
+	}
224 224
 
225 225
 
226
-    /**
227
-     * Register screen options for event_registrations route.
228
-     */
229
-    protected function _add_screen_options_event_registrations()
230
-    {
231
-        $this->_per_page_screen_option();
232
-    }
226
+	/**
227
+	 * Register screen options for event_registrations route.
228
+	 */
229
+	protected function _add_screen_options_event_registrations()
230
+	{
231
+		$this->_per_page_screen_option();
232
+	}
233 233
 
234 234
 
235
-    /**
236
-     * Register screen options for registration_checkins route
237
-     */
238
-    protected function _add_screen_options_registration_checkins()
239
-    {
240
-        $page_title = $this->_admin_page_title;
241
-        $this->_admin_page_title = esc_html__('Check-In Records', 'event_espresso');
242
-        $this->_per_page_screen_option();
243
-        $this->_admin_page_title = $page_title;
244
-    }
235
+	/**
236
+	 * Register screen options for registration_checkins route
237
+	 */
238
+	protected function _add_screen_options_registration_checkins()
239
+	{
240
+		$page_title = $this->_admin_page_title;
241
+		$this->_admin_page_title = esc_html__('Check-In Records', 'event_espresso');
242
+		$this->_per_page_screen_option();
243
+		$this->_admin_page_title = $page_title;
244
+	}
245 245
 
246 246
 
247
-    /**
248
-     * Set views property for event_registrations route.
249
-     */
250
-    protected function _set_list_table_views_event_registrations()
251
-    {
252
-        $this->_views = array(
253
-            'all' => array(
254
-                'slug'        => 'all',
255
-                'label'       => esc_html__('All', 'event_espresso'),
256
-                'count'       => 0,
257
-                'bulk_action' => ! isset($this->_req_data['event_id'])
258
-                    ? array()
259
-                    : array(
260
-                        'toggle_checkin_status_bulk' => esc_html__('Toggle Check-In', 'event_espresso'),
261
-                    ),
262
-            ),
263
-        );
264
-    }
247
+	/**
248
+	 * Set views property for event_registrations route.
249
+	 */
250
+	protected function _set_list_table_views_event_registrations()
251
+	{
252
+		$this->_views = array(
253
+			'all' => array(
254
+				'slug'        => 'all',
255
+				'label'       => esc_html__('All', 'event_espresso'),
256
+				'count'       => 0,
257
+				'bulk_action' => ! isset($this->_req_data['event_id'])
258
+					? array()
259
+					: array(
260
+						'toggle_checkin_status_bulk' => esc_html__('Toggle Check-In', 'event_espresso'),
261
+					),
262
+			),
263
+		);
264
+	}
265 265
 
266 266
 
267
-    /**
268
-     * Set views property for registration_checkins route.
269
-     */
270
-    protected function _set_list_table_views_registration_checkins()
271
-    {
272
-        $this->_views = array(
273
-            'all' => array(
274
-                'slug'        => 'all',
275
-                'label'       => esc_html__('All', 'event_espresso'),
276
-                'count'       => 0,
277
-                'bulk_action' => array('delete_checkin_rows' => esc_html__('Delete Check-In Rows', 'event_espresso')),
278
-            ),
279
-        );
280
-    }
267
+	/**
268
+	 * Set views property for registration_checkins route.
269
+	 */
270
+	protected function _set_list_table_views_registration_checkins()
271
+	{
272
+		$this->_views = array(
273
+			'all' => array(
274
+				'slug'        => 'all',
275
+				'label'       => esc_html__('All', 'event_espresso'),
276
+				'count'       => 0,
277
+				'bulk_action' => array('delete_checkin_rows' => esc_html__('Delete Check-In Rows', 'event_espresso')),
278
+			),
279
+		);
280
+	}
281 281
 
282 282
 
283
-    /**
284
-     * callback for ajax action.
285
-     *
286
-     * @since 4.3.0
287
-     * @return void (JSON)
288
-     * @throws EE_Error
289
-     * @throws InvalidArgumentException
290
-     * @throws InvalidDataTypeException
291
-     * @throws InvalidInterfaceException
292
-     */
293
-    public function get_newsletter_form_content()
294
-    {
295
-        // do a nonce check cause we're not coming in from an normal route here.
296
-        $nonce = isset($this->_req_data['get_newsletter_form_content_nonce']) ? sanitize_text_field(
297
-            $this->_req_data['get_newsletter_form_content_nonce']
298
-        ) : '';
299
-        $nonce_ref = 'get_newsletter_form_content_nonce';
300
-        $this->_verify_nonce($nonce, $nonce_ref);
301
-        // let's get the mtp for the incoming MTP_ ID
302
-        if (! isset($this->_req_data['GRP_ID'])) {
303
-            EE_Error::add_error(
304
-                esc_html__(
305
-                    'There must be something broken with the js or html structure because the required data for getting a message template group is not present (need an GRP_ID).',
306
-                    'event_espresso'
307
-                ),
308
-                __FILE__,
309
-                __FUNCTION__,
310
-                __LINE__
311
-            );
312
-            $this->_template_args['success'] = false;
313
-            $this->_template_args['error'] = true;
314
-            $this->_return_json();
315
-        }
316
-        $MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID($this->_req_data['GRP_ID']);
317
-        if (! $MTPG instanceof EE_Message_Template_Group) {
318
-            EE_Error::add_error(
319
-                sprintf(
320
-                    esc_html__(
321
-                        'The GRP_ID given (%d) does not appear to have a corresponding row in the database.',
322
-                        'event_espresso'
323
-                    ),
324
-                    $this->_req_data['GRP_ID']
325
-                ),
326
-                __FILE__,
327
-                __FUNCTION__,
328
-                __LINE__
329
-            );
330
-            $this->_template_args['success'] = false;
331
-            $this->_template_args['error'] = true;
332
-            $this->_return_json();
333
-        }
334
-        $MTPs = $MTPG->context_templates();
335
-        $MTPs = $MTPs['attendee'];
336
-        $template_fields = array();
337
-        /** @var EE_Message_Template $MTP */
338
-        foreach ($MTPs as $MTP) {
339
-            $field = $MTP->get('MTP_template_field');
340
-            if ($field === 'content') {
341
-                $content = $MTP->get('MTP_content');
342
-                if (! empty($content['newsletter_content'])) {
343
-                    $template_fields['newsletter_content'] = $content['newsletter_content'];
344
-                }
345
-                continue;
346
-            }
347
-            $template_fields[ $MTP->get('MTP_template_field') ] = $MTP->get('MTP_content');
348
-        }
349
-        $this->_template_args['success'] = true;
350
-        $this->_template_args['error'] = false;
351
-        $this->_template_args['data'] = array(
352
-            'batch_message_from'    => isset($template_fields['from'])
353
-                ? $template_fields['from']
354
-                : '',
355
-            'batch_message_subject' => isset($template_fields['subject'])
356
-                ? $template_fields['subject']
357
-                : '',
358
-            'batch_message_content' => isset($template_fields['newsletter_content'])
359
-                ? $template_fields['newsletter_content']
360
-                : '',
361
-        );
362
-        $this->_return_json();
363
-    }
283
+	/**
284
+	 * callback for ajax action.
285
+	 *
286
+	 * @since 4.3.0
287
+	 * @return void (JSON)
288
+	 * @throws EE_Error
289
+	 * @throws InvalidArgumentException
290
+	 * @throws InvalidDataTypeException
291
+	 * @throws InvalidInterfaceException
292
+	 */
293
+	public function get_newsletter_form_content()
294
+	{
295
+		// do a nonce check cause we're not coming in from an normal route here.
296
+		$nonce = isset($this->_req_data['get_newsletter_form_content_nonce']) ? sanitize_text_field(
297
+			$this->_req_data['get_newsletter_form_content_nonce']
298
+		) : '';
299
+		$nonce_ref = 'get_newsletter_form_content_nonce';
300
+		$this->_verify_nonce($nonce, $nonce_ref);
301
+		// let's get the mtp for the incoming MTP_ ID
302
+		if (! isset($this->_req_data['GRP_ID'])) {
303
+			EE_Error::add_error(
304
+				esc_html__(
305
+					'There must be something broken with the js or html structure because the required data for getting a message template group is not present (need an GRP_ID).',
306
+					'event_espresso'
307
+				),
308
+				__FILE__,
309
+				__FUNCTION__,
310
+				__LINE__
311
+			);
312
+			$this->_template_args['success'] = false;
313
+			$this->_template_args['error'] = true;
314
+			$this->_return_json();
315
+		}
316
+		$MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID($this->_req_data['GRP_ID']);
317
+		if (! $MTPG instanceof EE_Message_Template_Group) {
318
+			EE_Error::add_error(
319
+				sprintf(
320
+					esc_html__(
321
+						'The GRP_ID given (%d) does not appear to have a corresponding row in the database.',
322
+						'event_espresso'
323
+					),
324
+					$this->_req_data['GRP_ID']
325
+				),
326
+				__FILE__,
327
+				__FUNCTION__,
328
+				__LINE__
329
+			);
330
+			$this->_template_args['success'] = false;
331
+			$this->_template_args['error'] = true;
332
+			$this->_return_json();
333
+		}
334
+		$MTPs = $MTPG->context_templates();
335
+		$MTPs = $MTPs['attendee'];
336
+		$template_fields = array();
337
+		/** @var EE_Message_Template $MTP */
338
+		foreach ($MTPs as $MTP) {
339
+			$field = $MTP->get('MTP_template_field');
340
+			if ($field === 'content') {
341
+				$content = $MTP->get('MTP_content');
342
+				if (! empty($content['newsletter_content'])) {
343
+					$template_fields['newsletter_content'] = $content['newsletter_content'];
344
+				}
345
+				continue;
346
+			}
347
+			$template_fields[ $MTP->get('MTP_template_field') ] = $MTP->get('MTP_content');
348
+		}
349
+		$this->_template_args['success'] = true;
350
+		$this->_template_args['error'] = false;
351
+		$this->_template_args['data'] = array(
352
+			'batch_message_from'    => isset($template_fields['from'])
353
+				? $template_fields['from']
354
+				: '',
355
+			'batch_message_subject' => isset($template_fields['subject'])
356
+				? $template_fields['subject']
357
+				: '',
358
+			'batch_message_content' => isset($template_fields['newsletter_content'])
359
+				? $template_fields['newsletter_content']
360
+				: '',
361
+		);
362
+		$this->_return_json();
363
+	}
364 364
 
365 365
 
366
-    /**
367
-     * callback for AHEE__EE_Admin_List_Table__extra_tablenav__after_bottom_buttons action
368
-     *
369
-     * @since 4.3.0
370
-     * @param EE_Admin_List_Table $list_table
371
-     * @return void
372
-     * @throws InvalidArgumentException
373
-     * @throws InvalidDataTypeException
374
-     * @throws InvalidInterfaceException
375
-     */
376
-    public function add_newsletter_action_buttons(EE_Admin_List_Table $list_table)
377
-    {
378
-        if (! EE_Registry::instance()->CAP->current_user_can(
379
-            'ee_send_message',
380
-            'espresso_registrations_newsletter_selected_send'
381
-        )
382
-        ) {
383
-            return;
384
-        }
385
-        $routes_to_add_to = array(
386
-            'contact_list',
387
-            'event_registrations',
388
-            'default',
389
-        );
390
-        if ($this->_current_page === 'espresso_registrations' && in_array($this->_req_action, $routes_to_add_to)) {
391
-            if (($this->_req_action === 'event_registrations' && empty($this->_req_data['event_id']))
392
-                || (isset($this->_req_data['status']) && $this->_req_data['status'] === 'trash')
393
-            ) {
394
-                echo '';
395
-            } else {
396
-                $button_text = sprintf(
397
-                    esc_html__('Send Batch Message (%s selected)', 'event_espresso'),
398
-                    '<span class="send-selected-newsletter-count">0</span>'
399
-                );
400
-                echo '<button id="selected-batch-send-trigger" class="button secondary-button">'
401
-                     . '<span class="dashicons dashicons-email "></span>'
402
-                     . $button_text
403
-                     . '</button>';
404
-                add_action('admin_footer', array($this, 'newsletter_send_form_skeleton'));
405
-            }
406
-        }
407
-    }
366
+	/**
367
+	 * callback for AHEE__EE_Admin_List_Table__extra_tablenav__after_bottom_buttons action
368
+	 *
369
+	 * @since 4.3.0
370
+	 * @param EE_Admin_List_Table $list_table
371
+	 * @return void
372
+	 * @throws InvalidArgumentException
373
+	 * @throws InvalidDataTypeException
374
+	 * @throws InvalidInterfaceException
375
+	 */
376
+	public function add_newsletter_action_buttons(EE_Admin_List_Table $list_table)
377
+	{
378
+		if (! EE_Registry::instance()->CAP->current_user_can(
379
+			'ee_send_message',
380
+			'espresso_registrations_newsletter_selected_send'
381
+		)
382
+		) {
383
+			return;
384
+		}
385
+		$routes_to_add_to = array(
386
+			'contact_list',
387
+			'event_registrations',
388
+			'default',
389
+		);
390
+		if ($this->_current_page === 'espresso_registrations' && in_array($this->_req_action, $routes_to_add_to)) {
391
+			if (($this->_req_action === 'event_registrations' && empty($this->_req_data['event_id']))
392
+				|| (isset($this->_req_data['status']) && $this->_req_data['status'] === 'trash')
393
+			) {
394
+				echo '';
395
+			} else {
396
+				$button_text = sprintf(
397
+					esc_html__('Send Batch Message (%s selected)', 'event_espresso'),
398
+					'<span class="send-selected-newsletter-count">0</span>'
399
+				);
400
+				echo '<button id="selected-batch-send-trigger" class="button secondary-button">'
401
+					 . '<span class="dashicons dashicons-email "></span>'
402
+					 . $button_text
403
+					 . '</button>';
404
+				add_action('admin_footer', array($this, 'newsletter_send_form_skeleton'));
405
+			}
406
+		}
407
+	}
408 408
 
409 409
 
410
-    /**
411
-     * @throws DomainException
412
-     * @throws EE_Error
413
-     * @throws InvalidArgumentException
414
-     * @throws InvalidDataTypeException
415
-     * @throws InvalidInterfaceException
416
-     */
417
-    public function newsletter_send_form_skeleton()
418
-    {
419
-        $list_table = $this->_list_table_object;
420
-        $codes = array();
421
-        // need to templates for the newsletter message type for the template selector.
422
-        $values[] = array('text' => esc_html__('Select Template to Use', 'event_espresso'), 'id' => 0);
423
-        $mtps = EEM_Message_Template_Group::instance()->get_all(
424
-            array(array('MTP_message_type' => 'newsletter', 'MTP_messenger' => 'email'))
425
-        );
426
-        foreach ($mtps as $mtp) {
427
-            $name = $mtp->name();
428
-            $values[] = array(
429
-                'text' => empty($name) ? esc_html__('Global', 'event_espresso') : $name,
430
-                'id'   => $mtp->ID(),
431
-            );
432
-        }
433
-        // need to get a list of shortcodes that are available for the newsletter message type.
434
-        $shortcodes = EEH_MSG_Template::get_shortcodes(
435
-            'newsletter',
436
-            'email',
437
-            array(),
438
-            'attendee',
439
-            false
440
-        );
441
-        foreach ($shortcodes as $field => $shortcode_array) {
442
-            $available_shortcodes = array();
443
-            foreach ($shortcode_array as $shortcode => $shortcode_details) {
444
-                $field_id = $field === '[NEWSLETTER_CONTENT]'
445
-                    ? 'content'
446
-                    : $field;
447
-                $field_id = 'batch-message-' . strtolower($field_id);
448
-                $available_shortcodes[] = '<span class="js-shortcode-selection" data-value="'
449
-                                          . $shortcode
450
-                                          . '" data-linked-input-id="' . $field_id . '">'
451
-                                          . $shortcode
452
-                                          . '</span>';
453
-            }
454
-            $codes[ $field ] = implode(', ', $available_shortcodes);
455
-        }
456
-        $shortcodes = $codes;
457
-        $form_template = REG_CAF_TEMPLATE_PATH . 'newsletter-send-form.template.php';
458
-        $form_template_args = array(
459
-            'form_action'       => admin_url('admin.php?page=espresso_registrations'),
460
-            'form_route'        => 'newsletter_selected_send',
461
-            'form_nonce_name'   => 'newsletter_selected_send_nonce',
462
-            'form_nonce'        => wp_create_nonce('newsletter_selected_send_nonce'),
463
-            'redirect_back_to'  => $this->_req_action,
464
-            'ajax_nonce'        => wp_create_nonce('get_newsletter_form_content_nonce'),
465
-            'template_selector' => EEH_Form_Fields::select_input('newsletter_mtp_selected', $values),
466
-            'shortcodes'        => $shortcodes,
467
-            'id_type'           => $list_table instanceof EE_Attendee_Contact_List_Table ? 'contact' : 'registration',
468
-        );
469
-        EEH_Template::display_template($form_template, $form_template_args);
470
-    }
410
+	/**
411
+	 * @throws DomainException
412
+	 * @throws EE_Error
413
+	 * @throws InvalidArgumentException
414
+	 * @throws InvalidDataTypeException
415
+	 * @throws InvalidInterfaceException
416
+	 */
417
+	public function newsletter_send_form_skeleton()
418
+	{
419
+		$list_table = $this->_list_table_object;
420
+		$codes = array();
421
+		// need to templates for the newsletter message type for the template selector.
422
+		$values[] = array('text' => esc_html__('Select Template to Use', 'event_espresso'), 'id' => 0);
423
+		$mtps = EEM_Message_Template_Group::instance()->get_all(
424
+			array(array('MTP_message_type' => 'newsletter', 'MTP_messenger' => 'email'))
425
+		);
426
+		foreach ($mtps as $mtp) {
427
+			$name = $mtp->name();
428
+			$values[] = array(
429
+				'text' => empty($name) ? esc_html__('Global', 'event_espresso') : $name,
430
+				'id'   => $mtp->ID(),
431
+			);
432
+		}
433
+		// need to get a list of shortcodes that are available for the newsletter message type.
434
+		$shortcodes = EEH_MSG_Template::get_shortcodes(
435
+			'newsletter',
436
+			'email',
437
+			array(),
438
+			'attendee',
439
+			false
440
+		);
441
+		foreach ($shortcodes as $field => $shortcode_array) {
442
+			$available_shortcodes = array();
443
+			foreach ($shortcode_array as $shortcode => $shortcode_details) {
444
+				$field_id = $field === '[NEWSLETTER_CONTENT]'
445
+					? 'content'
446
+					: $field;
447
+				$field_id = 'batch-message-' . strtolower($field_id);
448
+				$available_shortcodes[] = '<span class="js-shortcode-selection" data-value="'
449
+										  . $shortcode
450
+										  . '" data-linked-input-id="' . $field_id . '">'
451
+										  . $shortcode
452
+										  . '</span>';
453
+			}
454
+			$codes[ $field ] = implode(', ', $available_shortcodes);
455
+		}
456
+		$shortcodes = $codes;
457
+		$form_template = REG_CAF_TEMPLATE_PATH . 'newsletter-send-form.template.php';
458
+		$form_template_args = array(
459
+			'form_action'       => admin_url('admin.php?page=espresso_registrations'),
460
+			'form_route'        => 'newsletter_selected_send',
461
+			'form_nonce_name'   => 'newsletter_selected_send_nonce',
462
+			'form_nonce'        => wp_create_nonce('newsletter_selected_send_nonce'),
463
+			'redirect_back_to'  => $this->_req_action,
464
+			'ajax_nonce'        => wp_create_nonce('get_newsletter_form_content_nonce'),
465
+			'template_selector' => EEH_Form_Fields::select_input('newsletter_mtp_selected', $values),
466
+			'shortcodes'        => $shortcodes,
467
+			'id_type'           => $list_table instanceof EE_Attendee_Contact_List_Table ? 'contact' : 'registration',
468
+		);
469
+		EEH_Template::display_template($form_template, $form_template_args);
470
+	}
471 471
 
472 472
 
473
-    /**
474
-     * Handles sending selected registrations/contacts a newsletter.
475
-     *
476
-     * @since  4.3.0
477
-     * @return void
478
-     * @throws EE_Error
479
-     * @throws InvalidArgumentException
480
-     * @throws InvalidDataTypeException
481
-     * @throws InvalidInterfaceException
482
-     */
483
-    protected function _newsletter_selected_send()
484
-    {
485
-        $success = true;
486
-        // first we need to make sure we have a GRP_ID so we know what template we're sending and updating!
487
-        if (empty($this->_req_data['newsletter_mtp_selected'])) {
488
-            EE_Error::add_error(
489
-                esc_html__(
490
-                    'In order to send a message, a Message Template GRP_ID is needed. It was not provided so messages were not sent.',
491
-                    'event_espresso'
492
-                ),
493
-                __FILE__,
494
-                __FUNCTION__,
495
-                __LINE__
496
-            );
497
-            $success = false;
498
-        }
499
-        if ($success) {
500
-            // update Message template in case there are any changes
501
-            $Message_Template_Group = EEM_Message_Template_Group::instance()->get_one_by_ID(
502
-                $this->_req_data['newsletter_mtp_selected']
503
-            );
504
-            $Message_Templates = $Message_Template_Group instanceof EE_Message_Template_Group
505
-                ? $Message_Template_Group->context_templates()
506
-                : array();
507
-            if (empty($Message_Templates)) {
508
-                EE_Error::add_error(
509
-                    esc_html__(
510
-                        'Unable to retrieve message template fields from the db. Messages not sent.',
511
-                        'event_espresso'
512
-                    ),
513
-                    __FILE__,
514
-                    __FUNCTION__,
515
-                    __LINE__
516
-                );
517
-            }
518
-            // let's just update the specific fields
519
-            foreach ($Message_Templates['attendee'] as $Message_Template) {
520
-                if ($Message_Template instanceof EE_Message_Template) {
521
-                    $field = $Message_Template->get('MTP_template_field');
522
-                    $content = $Message_Template->get('MTP_content');
523
-                    $new_content = $content;
524
-                    switch ($field) {
525
-                        case 'from':
526
-                            $new_content = ! empty($this->_req_data['batch_message']['from'])
527
-                                ? $this->_req_data['batch_message']['from']
528
-                                : $content;
529
-                            break;
530
-                        case 'subject':
531
-                            $new_content = ! empty($this->_req_data['batch_message']['subject'])
532
-                                ? $this->_req_data['batch_message']['subject']
533
-                                : $content;
534
-                            break;
535
-                        case 'content':
536
-                            $new_content = $content;
537
-                            $new_content['newsletter_content'] = ! empty($this->_req_data['batch_message']['content'])
538
-                                ? $this->_req_data['batch_message']['content']
539
-                                : $content['newsletter_content'];
540
-                            break;
541
-                        default:
542
-                            // continue the foreach loop, we don't want to set $new_content nor save.
543
-                            continue 2;
544
-                    }
545
-                    $Message_Template->set('MTP_content', $new_content);
546
-                    $Message_Template->save();
547
-                }
548
-            }
549
-            // great fields are updated!  now let's make sure we just have contact objects (EE_Attendee).
550
-            $id_type = ! empty($this->_req_data['batch_message']['id_type'])
551
-                ? $this->_req_data['batch_message']['id_type']
552
-                : 'registration';
553
-            // id_type will affect how we assemble the ids.
554
-            $ids = ! empty($this->_req_data['batch_message']['ids'])
555
-                ? json_decode(stripslashes($this->_req_data['batch_message']['ids']))
556
-                : array();
557
-            $registrations_used_for_contact_data = array();
558
-            // using switch because eventually we'll have other contexts that will be used for generating messages.
559
-            switch ($id_type) {
560
-                case 'registration':
561
-                    $registrations_used_for_contact_data = EEM_Registration::instance()->get_all(
562
-                        array(
563
-                            array(
564
-                                'REG_ID' => array('IN', $ids),
565
-                            ),
566
-                        )
567
-                    );
568
-                    break;
569
-                case 'contact':
570
-                    $registrations_used_for_contact_data = EEM_Registration::instance()
571
-                                                                           ->get_latest_registration_for_each_of_given_contacts(
572
-                                                                               $ids
573
-                                                                           );
574
-                    break;
575
-            }
576
-            do_action_ref_array(
577
-                'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations',
578
-                array(
579
-                    $registrations_used_for_contact_data,
580
-                    $Message_Template_Group->ID(),
581
-                )
582
-            );
583
-            // kept for backward compat, internally we no longer use this action.
584
-            // @deprecated 4.8.36.rc.002
585
-            $contacts = $id_type === 'registration'
586
-                ? EEM_Attendee::instance()->get_array_of_contacts_from_reg_ids($ids)
587
-                : EEM_Attendee::instance()->get_all(array(array('ATT_ID' => array('in', $ids))));
588
-            do_action_ref_array(
589
-                'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send',
590
-                array(
591
-                    $contacts,
592
-                    $Message_Template_Group->ID(),
593
-                )
594
-            );
595
-        }
596
-        $query_args = array(
597
-            'action' => ! empty($this->_req_data['redirect_back_to'])
598
-                ? $this->_req_data['redirect_back_to']
599
-                : 'default',
600
-        );
601
-        $this->_redirect_after_action(false, '', '', $query_args, true);
602
-    }
473
+	/**
474
+	 * Handles sending selected registrations/contacts a newsletter.
475
+	 *
476
+	 * @since  4.3.0
477
+	 * @return void
478
+	 * @throws EE_Error
479
+	 * @throws InvalidArgumentException
480
+	 * @throws InvalidDataTypeException
481
+	 * @throws InvalidInterfaceException
482
+	 */
483
+	protected function _newsletter_selected_send()
484
+	{
485
+		$success = true;
486
+		// first we need to make sure we have a GRP_ID so we know what template we're sending and updating!
487
+		if (empty($this->_req_data['newsletter_mtp_selected'])) {
488
+			EE_Error::add_error(
489
+				esc_html__(
490
+					'In order to send a message, a Message Template GRP_ID is needed. It was not provided so messages were not sent.',
491
+					'event_espresso'
492
+				),
493
+				__FILE__,
494
+				__FUNCTION__,
495
+				__LINE__
496
+			);
497
+			$success = false;
498
+		}
499
+		if ($success) {
500
+			// update Message template in case there are any changes
501
+			$Message_Template_Group = EEM_Message_Template_Group::instance()->get_one_by_ID(
502
+				$this->_req_data['newsletter_mtp_selected']
503
+			);
504
+			$Message_Templates = $Message_Template_Group instanceof EE_Message_Template_Group
505
+				? $Message_Template_Group->context_templates()
506
+				: array();
507
+			if (empty($Message_Templates)) {
508
+				EE_Error::add_error(
509
+					esc_html__(
510
+						'Unable to retrieve message template fields from the db. Messages not sent.',
511
+						'event_espresso'
512
+					),
513
+					__FILE__,
514
+					__FUNCTION__,
515
+					__LINE__
516
+				);
517
+			}
518
+			// let's just update the specific fields
519
+			foreach ($Message_Templates['attendee'] as $Message_Template) {
520
+				if ($Message_Template instanceof EE_Message_Template) {
521
+					$field = $Message_Template->get('MTP_template_field');
522
+					$content = $Message_Template->get('MTP_content');
523
+					$new_content = $content;
524
+					switch ($field) {
525
+						case 'from':
526
+							$new_content = ! empty($this->_req_data['batch_message']['from'])
527
+								? $this->_req_data['batch_message']['from']
528
+								: $content;
529
+							break;
530
+						case 'subject':
531
+							$new_content = ! empty($this->_req_data['batch_message']['subject'])
532
+								? $this->_req_data['batch_message']['subject']
533
+								: $content;
534
+							break;
535
+						case 'content':
536
+							$new_content = $content;
537
+							$new_content['newsletter_content'] = ! empty($this->_req_data['batch_message']['content'])
538
+								? $this->_req_data['batch_message']['content']
539
+								: $content['newsletter_content'];
540
+							break;
541
+						default:
542
+							// continue the foreach loop, we don't want to set $new_content nor save.
543
+							continue 2;
544
+					}
545
+					$Message_Template->set('MTP_content', $new_content);
546
+					$Message_Template->save();
547
+				}
548
+			}
549
+			// great fields are updated!  now let's make sure we just have contact objects (EE_Attendee).
550
+			$id_type = ! empty($this->_req_data['batch_message']['id_type'])
551
+				? $this->_req_data['batch_message']['id_type']
552
+				: 'registration';
553
+			// id_type will affect how we assemble the ids.
554
+			$ids = ! empty($this->_req_data['batch_message']['ids'])
555
+				? json_decode(stripslashes($this->_req_data['batch_message']['ids']))
556
+				: array();
557
+			$registrations_used_for_contact_data = array();
558
+			// using switch because eventually we'll have other contexts that will be used for generating messages.
559
+			switch ($id_type) {
560
+				case 'registration':
561
+					$registrations_used_for_contact_data = EEM_Registration::instance()->get_all(
562
+						array(
563
+							array(
564
+								'REG_ID' => array('IN', $ids),
565
+							),
566
+						)
567
+					);
568
+					break;
569
+				case 'contact':
570
+					$registrations_used_for_contact_data = EEM_Registration::instance()
571
+																		   ->get_latest_registration_for_each_of_given_contacts(
572
+																			   $ids
573
+																		   );
574
+					break;
575
+			}
576
+			do_action_ref_array(
577
+				'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations',
578
+				array(
579
+					$registrations_used_for_contact_data,
580
+					$Message_Template_Group->ID(),
581
+				)
582
+			);
583
+			// kept for backward compat, internally we no longer use this action.
584
+			// @deprecated 4.8.36.rc.002
585
+			$contacts = $id_type === 'registration'
586
+				? EEM_Attendee::instance()->get_array_of_contacts_from_reg_ids($ids)
587
+				: EEM_Attendee::instance()->get_all(array(array('ATT_ID' => array('in', $ids))));
588
+			do_action_ref_array(
589
+				'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send',
590
+				array(
591
+					$contacts,
592
+					$Message_Template_Group->ID(),
593
+				)
594
+			);
595
+		}
596
+		$query_args = array(
597
+			'action' => ! empty($this->_req_data['redirect_back_to'])
598
+				? $this->_req_data['redirect_back_to']
599
+				: 'default',
600
+		);
601
+		$this->_redirect_after_action(false, '', '', $query_args, true);
602
+	}
603 603
 
604 604
 
605
-    /**
606
-     * This is called when javascript is being enqueued to setup the various data needed for the reports js.
607
-     * Also $this->{$_reports_template_data} property is set for later usage by the _registration_reports method.
608
-     */
609
-    protected function _registration_reports_js_setup()
610
-    {
611
-        $this->_reports_template_data['admin_reports'][] = $this->_registrations_per_day_report();
612
-        $this->_reports_template_data['admin_reports'][] = $this->_registrations_per_event_report();
613
-    }
605
+	/**
606
+	 * This is called when javascript is being enqueued to setup the various data needed for the reports js.
607
+	 * Also $this->{$_reports_template_data} property is set for later usage by the _registration_reports method.
608
+	 */
609
+	protected function _registration_reports_js_setup()
610
+	{
611
+		$this->_reports_template_data['admin_reports'][] = $this->_registrations_per_day_report();
612
+		$this->_reports_template_data['admin_reports'][] = $this->_registrations_per_event_report();
613
+	}
614 614
 
615 615
 
616
-    /**
617
-     *        generates Business Reports regarding Registrations
618
-     *
619
-     * @access protected
620
-     * @return void
621
-     * @throws DomainException
622
-     */
623
-    protected function _registration_reports()
624
-    {
625
-        $template_path = EE_ADMIN_TEMPLATE . 'admin_reports.template.php';
626
-        $this->_template_args['admin_page_content'] = EEH_Template::display_template(
627
-            $template_path,
628
-            $this->_reports_template_data,
629
-            true
630
-        );
631
-        // the final template wrapper
632
-        $this->display_admin_page_with_no_sidebar();
633
-    }
616
+	/**
617
+	 *        generates Business Reports regarding Registrations
618
+	 *
619
+	 * @access protected
620
+	 * @return void
621
+	 * @throws DomainException
622
+	 */
623
+	protected function _registration_reports()
624
+	{
625
+		$template_path = EE_ADMIN_TEMPLATE . 'admin_reports.template.php';
626
+		$this->_template_args['admin_page_content'] = EEH_Template::display_template(
627
+			$template_path,
628
+			$this->_reports_template_data,
629
+			true
630
+		);
631
+		// the final template wrapper
632
+		$this->display_admin_page_with_no_sidebar();
633
+	}
634 634
 
635 635
 
636
-    /**
637
-     * Generates Business Report showing total registrations per day.
638
-     *
639
-     * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated.
640
-     * @return string
641
-     * @throws EE_Error
642
-     * @throws InvalidArgumentException
643
-     * @throws InvalidDataTypeException
644
-     * @throws InvalidInterfaceException
645
-     */
646
-    private function _registrations_per_day_report($period = '-1 month')
647
-    {
648
-        $report_ID = 'reg-admin-registrations-per-day-report-dv';
649
-        $results = EEM_Registration::instance()->get_registrations_per_day_and_per_status_report($period);
650
-        $results = (array) $results;
651
-        $regs = array();
652
-        $subtitle = '';
653
-        if ($results) {
654
-            $column_titles = array();
655
-            $tracker = 0;
656
-            foreach ($results as $result) {
657
-                $report_column_values = array();
658
-                foreach ($result as $property_name => $property_value) {
659
-                    $property_value = $property_name === 'Registration_REG_date' ? $property_value
660
-                        : (int) $property_value;
661
-                    $report_column_values[] = $property_value;
662
-                    if ($tracker === 0) {
663
-                        if ($property_name === 'Registration_REG_date') {
664
-                            $column_titles[] = esc_html__(
665
-                                'Date (only days with registrations are shown)',
666
-                                'event_espresso'
667
-                            );
668
-                        } else {
669
-                            $column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence');
670
-                        }
671
-                    }
672
-                }
673
-                $tracker++;
674
-                $regs[] = $report_column_values;
675
-            }
676
-            // make sure the column_titles is pushed to the beginning of the array
677
-            array_unshift($regs, $column_titles);
678
-            // setup the date range.
679
-            $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone());
680
-            $beginning_date = new DateTime("now " . $period, $DateTimeZone);
681
-            $ending_date = new DateTime("now", $DateTimeZone);
682
-            $subtitle = sprintf(
683
-                _x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'),
684
-                $beginning_date->format('Y-m-d'),
685
-                $ending_date->format('Y-m-d')
686
-            );
687
-        }
688
-        $report_title = esc_html__('Total Registrations per Day', 'event_espresso');
689
-        $report_params = array(
690
-            'title'     => $report_title,
691
-            'subtitle'  => $subtitle,
692
-            'id'        => $report_ID,
693
-            'regs'      => $regs,
694
-            'noResults' => empty($regs),
695
-            'noRegsMsg' => sprintf(
696
-                esc_html__(
697
-                    '%sThere are currently no registration records in the last month for this report.%s',
698
-                    'event_espresso'
699
-                ),
700
-                '<h2>' . $report_title . '</h2><p>',
701
-                '</p>'
702
-            ),
703
-        );
704
-        wp_localize_script('ee-reg-reports-js', 'regPerDay', $report_params);
705
-        return $report_ID;
706
-    }
636
+	/**
637
+	 * Generates Business Report showing total registrations per day.
638
+	 *
639
+	 * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated.
640
+	 * @return string
641
+	 * @throws EE_Error
642
+	 * @throws InvalidArgumentException
643
+	 * @throws InvalidDataTypeException
644
+	 * @throws InvalidInterfaceException
645
+	 */
646
+	private function _registrations_per_day_report($period = '-1 month')
647
+	{
648
+		$report_ID = 'reg-admin-registrations-per-day-report-dv';
649
+		$results = EEM_Registration::instance()->get_registrations_per_day_and_per_status_report($period);
650
+		$results = (array) $results;
651
+		$regs = array();
652
+		$subtitle = '';
653
+		if ($results) {
654
+			$column_titles = array();
655
+			$tracker = 0;
656
+			foreach ($results as $result) {
657
+				$report_column_values = array();
658
+				foreach ($result as $property_name => $property_value) {
659
+					$property_value = $property_name === 'Registration_REG_date' ? $property_value
660
+						: (int) $property_value;
661
+					$report_column_values[] = $property_value;
662
+					if ($tracker === 0) {
663
+						if ($property_name === 'Registration_REG_date') {
664
+							$column_titles[] = esc_html__(
665
+								'Date (only days with registrations are shown)',
666
+								'event_espresso'
667
+							);
668
+						} else {
669
+							$column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence');
670
+						}
671
+					}
672
+				}
673
+				$tracker++;
674
+				$regs[] = $report_column_values;
675
+			}
676
+			// make sure the column_titles is pushed to the beginning of the array
677
+			array_unshift($regs, $column_titles);
678
+			// setup the date range.
679
+			$DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone());
680
+			$beginning_date = new DateTime("now " . $period, $DateTimeZone);
681
+			$ending_date = new DateTime("now", $DateTimeZone);
682
+			$subtitle = sprintf(
683
+				_x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'),
684
+				$beginning_date->format('Y-m-d'),
685
+				$ending_date->format('Y-m-d')
686
+			);
687
+		}
688
+		$report_title = esc_html__('Total Registrations per Day', 'event_espresso');
689
+		$report_params = array(
690
+			'title'     => $report_title,
691
+			'subtitle'  => $subtitle,
692
+			'id'        => $report_ID,
693
+			'regs'      => $regs,
694
+			'noResults' => empty($regs),
695
+			'noRegsMsg' => sprintf(
696
+				esc_html__(
697
+					'%sThere are currently no registration records in the last month for this report.%s',
698
+					'event_espresso'
699
+				),
700
+				'<h2>' . $report_title . '</h2><p>',
701
+				'</p>'
702
+			),
703
+		);
704
+		wp_localize_script('ee-reg-reports-js', 'regPerDay', $report_params);
705
+		return $report_ID;
706
+	}
707 707
 
708 708
 
709
-    /**
710
-     * Generates Business Report showing total registrations per event.
711
-     *
712
-     * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated.
713
-     * @return string
714
-     * @throws EE_Error
715
-     * @throws InvalidArgumentException
716
-     * @throws InvalidDataTypeException
717
-     * @throws InvalidInterfaceException
718
-     */
719
-    private function _registrations_per_event_report($period = '-1 month')
720
-    {
721
-        $report_ID = 'reg-admin-registrations-per-event-report-dv';
722
-        $results = EEM_Registration::instance()->get_registrations_per_event_and_per_status_report($period);
723
-        $results = (array) $results;
724
-        $regs = array();
725
-        $subtitle = '';
726
-        if ($results) {
727
-            $column_titles = array();
728
-            $tracker = 0;
729
-            foreach ($results as $result) {
730
-                $report_column_values = array();
731
-                foreach ($result as $property_name => $property_value) {
732
-                    $property_value = $property_name === 'Registration_Event' ? wp_trim_words(
733
-                        $property_value,
734
-                        4,
735
-                        '...'
736
-                    ) : (int) $property_value;
737
-                    $report_column_values[] = $property_value;
738
-                    if ($tracker === 0) {
739
-                        if ($property_name === 'Registration_Event') {
740
-                            $column_titles[] = esc_html__('Event', 'event_espresso');
741
-                        } else {
742
-                            $column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence');
743
-                        }
744
-                    }
745
-                }
746
-                $tracker++;
747
-                $regs[] = $report_column_values;
748
-            }
749
-            // make sure the column_titles is pushed to the beginning of the array
750
-            array_unshift($regs, $column_titles);
751
-            // setup the date range.
752
-            $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone());
753
-            $beginning_date = new DateTime("now " . $period, $DateTimeZone);
754
-            $ending_date = new DateTime("now", $DateTimeZone);
755
-            $subtitle = sprintf(
756
-                _x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'),
757
-                $beginning_date->format('Y-m-d'),
758
-                $ending_date->format('Y-m-d')
759
-            );
760
-        }
761
-        $report_title = esc_html__('Total Registrations per Event', 'event_espresso');
762
-        $report_params = array(
763
-            'title'     => $report_title,
764
-            'subtitle'  => $subtitle,
765
-            'id'        => $report_ID,
766
-            'regs'      => $regs,
767
-            'noResults' => empty($regs),
768
-            'noRegsMsg' => sprintf(
769
-                esc_html__(
770
-                    '%sThere are currently no registration records in the last month for this report.%s',
771
-                    'event_espresso'
772
-                ),
773
-                '<h2>' . $report_title . '</h2><p>',
774
-                '</p>'
775
-            ),
776
-        );
777
-        wp_localize_script('ee-reg-reports-js', 'regPerEvent', $report_params);
778
-        return $report_ID;
779
-    }
709
+	/**
710
+	 * Generates Business Report showing total registrations per event.
711
+	 *
712
+	 * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated.
713
+	 * @return string
714
+	 * @throws EE_Error
715
+	 * @throws InvalidArgumentException
716
+	 * @throws InvalidDataTypeException
717
+	 * @throws InvalidInterfaceException
718
+	 */
719
+	private function _registrations_per_event_report($period = '-1 month')
720
+	{
721
+		$report_ID = 'reg-admin-registrations-per-event-report-dv';
722
+		$results = EEM_Registration::instance()->get_registrations_per_event_and_per_status_report($period);
723
+		$results = (array) $results;
724
+		$regs = array();
725
+		$subtitle = '';
726
+		if ($results) {
727
+			$column_titles = array();
728
+			$tracker = 0;
729
+			foreach ($results as $result) {
730
+				$report_column_values = array();
731
+				foreach ($result as $property_name => $property_value) {
732
+					$property_value = $property_name === 'Registration_Event' ? wp_trim_words(
733
+						$property_value,
734
+						4,
735
+						'...'
736
+					) : (int) $property_value;
737
+					$report_column_values[] = $property_value;
738
+					if ($tracker === 0) {
739
+						if ($property_name === 'Registration_Event') {
740
+							$column_titles[] = esc_html__('Event', 'event_espresso');
741
+						} else {
742
+							$column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence');
743
+						}
744
+					}
745
+				}
746
+				$tracker++;
747
+				$regs[] = $report_column_values;
748
+			}
749
+			// make sure the column_titles is pushed to the beginning of the array
750
+			array_unshift($regs, $column_titles);
751
+			// setup the date range.
752
+			$DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone());
753
+			$beginning_date = new DateTime("now " . $period, $DateTimeZone);
754
+			$ending_date = new DateTime("now", $DateTimeZone);
755
+			$subtitle = sprintf(
756
+				_x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'),
757
+				$beginning_date->format('Y-m-d'),
758
+				$ending_date->format('Y-m-d')
759
+			);
760
+		}
761
+		$report_title = esc_html__('Total Registrations per Event', 'event_espresso');
762
+		$report_params = array(
763
+			'title'     => $report_title,
764
+			'subtitle'  => $subtitle,
765
+			'id'        => $report_ID,
766
+			'regs'      => $regs,
767
+			'noResults' => empty($regs),
768
+			'noRegsMsg' => sprintf(
769
+				esc_html__(
770
+					'%sThere are currently no registration records in the last month for this report.%s',
771
+					'event_espresso'
772
+				),
773
+				'<h2>' . $report_title . '</h2><p>',
774
+				'</p>'
775
+			),
776
+		);
777
+		wp_localize_script('ee-reg-reports-js', 'regPerEvent', $report_params);
778
+		return $report_ID;
779
+	}
780 780
 
781 781
 
782
-    /**
783
-     * generates HTML for the Registration Check-in list table (showing all Check-ins for a specific registration)
784
-     *
785
-     * @access protected
786
-     * @return void
787
-     * @throws EE_Error
788
-     * @throws InvalidArgumentException
789
-     * @throws InvalidDataTypeException
790
-     * @throws InvalidInterfaceException
791
-     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
792
-     */
793
-    protected function _registration_checkin_list_table()
794
-    {
795
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
796
-        $reg_id = isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : null;
797
-        /** @var EE_Registration $registration */
798
-        $registration = EEM_Registration::instance()->get_one_by_ID($reg_id);
799
-        $attendee = $registration->attendee();
800
-        $this->_admin_page_title .= $this->get_action_link_or_button(
801
-            'new_registration',
802
-            'add-registrant',
803
-            array('event_id' => $registration->event_ID()),
804
-            'add-new-h2'
805
-        );
806
-        $checked_in = new CheckinStatusDashicon(EE_Checkin::status_checked_in);
807
-        $checked_out = new CheckinStatusDashicon(EE_Checkin::status_checked_out);
808
-        $legend_items = array(
809
-            'checkin'  => array(
810
-                'class' => $checked_in->cssClasses(),
811
-                'desc'  => $checked_in->legendLabel(),
812
-            ),
813
-            'checkout' => array(
814
-                'class' => $checked_out->cssClasses(),
815
-                'desc'  => $checked_out->legendLabel(),
816
-            ),
817
-        );
818
-        $this->_template_args['after_list_table'] = $this->_display_legend($legend_items);
819
-        $dtt_id = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null;
820
-        /** @var EE_Datetime $datetime */
821
-        $datetime = EEM_Datetime::instance()->get_one_by_ID($dtt_id);
822
-        $datetime_label = '';
823
-        if ($datetime instanceof EE_Datetime) {
824
-            $datetime_label = $datetime->get_dtt_display_name(true);
825
-            $datetime_label .= ! empty($datetime_label)
826
-                ? ' (' . $datetime->get_dtt_display_name() . ')'
827
-                : $datetime->get_dtt_display_name();
828
-        }
829
-        $datetime_link = ! empty($dtt_id) && $registration instanceof EE_Registration
830
-            ? EE_Admin_Page::add_query_args_and_nonce(
831
-                array(
832
-                    'action'   => 'event_registrations',
833
-                    'event_id' => $registration->event_ID(),
834
-                    'DTT_ID'   => $dtt_id,
835
-                ),
836
-                $this->_admin_base_url
837
-            )
838
-            : '';
839
-        $datetime_link = ! empty($datetime_link)
840
-            ? '<a href="' . $datetime_link . '">'
841
-              . '<span id="checkin-dtt">'
842
-              . $datetime_label
843
-              . '</span></a>'
844
-            : $datetime_label;
845
-        $attendee_name = $attendee instanceof EE_Attendee
846
-            ? $attendee->full_name()
847
-            : '';
848
-        $attendee_link = $attendee instanceof EE_Attendee
849
-            ? $attendee->get_admin_details_link()
850
-            : '';
851
-        $attendee_link = ! empty($attendee_link)
852
-            ? '<a href="' . $attendee->get_admin_details_link() . '"'
853
-              . ' title="' . esc_html__('Click for attendee details', 'event_espresso') . '">'
854
-              . '<span id="checkin-attendee-name">'
855
-              . $attendee_name
856
-              . '</span></a>'
857
-            : '';
858
-        $event_link = $registration->event() instanceof EE_Event
859
-            ? $registration->event()->get_admin_details_link()
860
-            : '';
861
-        $event_link = ! empty($event_link)
862
-            ? '<a href="' . $event_link . '"'
863
-              . ' title="' . esc_html__('Click here to edit event.', 'event_espresso') . '">'
864
-              . '<span id="checkin-event-name">'
865
-              . $registration->event_name()
866
-              . '</span>'
867
-              . '</a>'
868
-            : '';
869
-        $this->_template_args['before_list_table'] = ! empty($reg_id) && ! empty($dtt_id)
870
-            ? '<h2>' . sprintf(
871
-                esc_html__('Displaying check in records for %1$s for %2$s at the event, %3$s', 'event_espresso'),
872
-                $attendee_link,
873
-                $datetime_link,
874
-                $event_link
875
-            ) . '</h2>'
876
-            : '';
877
-        $this->_template_args['list_table_hidden_fields'] = ! empty($reg_id)
878
-            ? '<input type="hidden" name="_REG_ID" value="' . $reg_id . '">' : '';
879
-        $this->_template_args['list_table_hidden_fields'] .= ! empty($dtt_id)
880
-            ? '<input type="hidden" name="DTT_ID" value="' . $dtt_id . '">' : '';
881
-        $this->display_admin_list_table_page_with_no_sidebar();
882
-    }
782
+	/**
783
+	 * generates HTML for the Registration Check-in list table (showing all Check-ins for a specific registration)
784
+	 *
785
+	 * @access protected
786
+	 * @return void
787
+	 * @throws EE_Error
788
+	 * @throws InvalidArgumentException
789
+	 * @throws InvalidDataTypeException
790
+	 * @throws InvalidInterfaceException
791
+	 * @throws \EventEspresso\core\exceptions\EntityNotFoundException
792
+	 */
793
+	protected function _registration_checkin_list_table()
794
+	{
795
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
796
+		$reg_id = isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : null;
797
+		/** @var EE_Registration $registration */
798
+		$registration = EEM_Registration::instance()->get_one_by_ID($reg_id);
799
+		$attendee = $registration->attendee();
800
+		$this->_admin_page_title .= $this->get_action_link_or_button(
801
+			'new_registration',
802
+			'add-registrant',
803
+			array('event_id' => $registration->event_ID()),
804
+			'add-new-h2'
805
+		);
806
+		$checked_in = new CheckinStatusDashicon(EE_Checkin::status_checked_in);
807
+		$checked_out = new CheckinStatusDashicon(EE_Checkin::status_checked_out);
808
+		$legend_items = array(
809
+			'checkin'  => array(
810
+				'class' => $checked_in->cssClasses(),
811
+				'desc'  => $checked_in->legendLabel(),
812
+			),
813
+			'checkout' => array(
814
+				'class' => $checked_out->cssClasses(),
815
+				'desc'  => $checked_out->legendLabel(),
816
+			),
817
+		);
818
+		$this->_template_args['after_list_table'] = $this->_display_legend($legend_items);
819
+		$dtt_id = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null;
820
+		/** @var EE_Datetime $datetime */
821
+		$datetime = EEM_Datetime::instance()->get_one_by_ID($dtt_id);
822
+		$datetime_label = '';
823
+		if ($datetime instanceof EE_Datetime) {
824
+			$datetime_label = $datetime->get_dtt_display_name(true);
825
+			$datetime_label .= ! empty($datetime_label)
826
+				? ' (' . $datetime->get_dtt_display_name() . ')'
827
+				: $datetime->get_dtt_display_name();
828
+		}
829
+		$datetime_link = ! empty($dtt_id) && $registration instanceof EE_Registration
830
+			? EE_Admin_Page::add_query_args_and_nonce(
831
+				array(
832
+					'action'   => 'event_registrations',
833
+					'event_id' => $registration->event_ID(),
834
+					'DTT_ID'   => $dtt_id,
835
+				),
836
+				$this->_admin_base_url
837
+			)
838
+			: '';
839
+		$datetime_link = ! empty($datetime_link)
840
+			? '<a href="' . $datetime_link . '">'
841
+			  . '<span id="checkin-dtt">'
842
+			  . $datetime_label
843
+			  . '</span></a>'
844
+			: $datetime_label;
845
+		$attendee_name = $attendee instanceof EE_Attendee
846
+			? $attendee->full_name()
847
+			: '';
848
+		$attendee_link = $attendee instanceof EE_Attendee
849
+			? $attendee->get_admin_details_link()
850
+			: '';
851
+		$attendee_link = ! empty($attendee_link)
852
+			? '<a href="' . $attendee->get_admin_details_link() . '"'
853
+			  . ' title="' . esc_html__('Click for attendee details', 'event_espresso') . '">'
854
+			  . '<span id="checkin-attendee-name">'
855
+			  . $attendee_name
856
+			  . '</span></a>'
857
+			: '';
858
+		$event_link = $registration->event() instanceof EE_Event
859
+			? $registration->event()->get_admin_details_link()
860
+			: '';
861
+		$event_link = ! empty($event_link)
862
+			? '<a href="' . $event_link . '"'
863
+			  . ' title="' . esc_html__('Click here to edit event.', 'event_espresso') . '">'
864
+			  . '<span id="checkin-event-name">'
865
+			  . $registration->event_name()
866
+			  . '</span>'
867
+			  . '</a>'
868
+			: '';
869
+		$this->_template_args['before_list_table'] = ! empty($reg_id) && ! empty($dtt_id)
870
+			? '<h2>' . sprintf(
871
+				esc_html__('Displaying check in records for %1$s for %2$s at the event, %3$s', 'event_espresso'),
872
+				$attendee_link,
873
+				$datetime_link,
874
+				$event_link
875
+			) . '</h2>'
876
+			: '';
877
+		$this->_template_args['list_table_hidden_fields'] = ! empty($reg_id)
878
+			? '<input type="hidden" name="_REG_ID" value="' . $reg_id . '">' : '';
879
+		$this->_template_args['list_table_hidden_fields'] .= ! empty($dtt_id)
880
+			? '<input type="hidden" name="DTT_ID" value="' . $dtt_id . '">' : '';
881
+		$this->display_admin_list_table_page_with_no_sidebar();
882
+	}
883 883
 
884 884
 
885
-    /**
886
-     * toggle the Check-in status for the given registration (coming from ajax)
887
-     *
888
-     * @return void (JSON)
889
-     * @throws EE_Error
890
-     * @throws InvalidArgumentException
891
-     * @throws InvalidDataTypeException
892
-     * @throws InvalidInterfaceException
893
-     */
894
-    public function toggle_checkin_status()
895
-    {
896
-        // first make sure we have the necessary data
897
-        if (! isset($this->_req_data['_regid'])) {
898
-            EE_Error::add_error(
899
-                esc_html__(
900
-                    'There must be something broken with the html structure because the required data for toggling the Check-in status is not being sent via ajax',
901
-                    'event_espresso'
902
-                ),
903
-                __FILE__,
904
-                __FUNCTION__,
905
-                __LINE__
906
-            );
907
-            $this->_template_args['success'] = false;
908
-            $this->_template_args['error'] = true;
909
-            $this->_return_json();
910
-        };
911
-        // do a nonce check cause we're not coming in from an normal route here.
912
-        $nonce = isset($this->_req_data['checkinnonce']) ? sanitize_text_field($this->_req_data['checkinnonce'])
913
-            : '';
914
-        $nonce_ref = 'checkin_nonce';
915
-        $this->_verify_nonce($nonce, $nonce_ref);
916
-        // beautiful! Made it this far so let's get the status.
917
-        $new_status = new CheckinStatusDashicon($this->_toggle_checkin_status());
918
-        // setup new class to return via ajax
919
-        $this->_template_args['admin_page_content'] = 'clickable trigger-checkin ' . $new_status->cssClasses();
920
-        $this->_template_args['success'] = true;
921
-        $this->_return_json();
922
-    }
885
+	/**
886
+	 * toggle the Check-in status for the given registration (coming from ajax)
887
+	 *
888
+	 * @return void (JSON)
889
+	 * @throws EE_Error
890
+	 * @throws InvalidArgumentException
891
+	 * @throws InvalidDataTypeException
892
+	 * @throws InvalidInterfaceException
893
+	 */
894
+	public function toggle_checkin_status()
895
+	{
896
+		// first make sure we have the necessary data
897
+		if (! isset($this->_req_data['_regid'])) {
898
+			EE_Error::add_error(
899
+				esc_html__(
900
+					'There must be something broken with the html structure because the required data for toggling the Check-in status is not being sent via ajax',
901
+					'event_espresso'
902
+				),
903
+				__FILE__,
904
+				__FUNCTION__,
905
+				__LINE__
906
+			);
907
+			$this->_template_args['success'] = false;
908
+			$this->_template_args['error'] = true;
909
+			$this->_return_json();
910
+		};
911
+		// do a nonce check cause we're not coming in from an normal route here.
912
+		$nonce = isset($this->_req_data['checkinnonce']) ? sanitize_text_field($this->_req_data['checkinnonce'])
913
+			: '';
914
+		$nonce_ref = 'checkin_nonce';
915
+		$this->_verify_nonce($nonce, $nonce_ref);
916
+		// beautiful! Made it this far so let's get the status.
917
+		$new_status = new CheckinStatusDashicon($this->_toggle_checkin_status());
918
+		// setup new class to return via ajax
919
+		$this->_template_args['admin_page_content'] = 'clickable trigger-checkin ' . $new_status->cssClasses();
920
+		$this->_template_args['success'] = true;
921
+		$this->_return_json();
922
+	}
923 923
 
924 924
 
925
-    /**
926
-     * handles toggling the checkin status for the registration,
927
-     *
928
-     * @access protected
929
-     * @return int|void
930
-     * @throws EE_Error
931
-     * @throws InvalidArgumentException
932
-     * @throws InvalidDataTypeException
933
-     * @throws InvalidInterfaceException
934
-     */
935
-    protected function _toggle_checkin_status()
936
-    {
937
-        // first let's get the query args out of the way for the redirect
938
-        $query_args = array(
939
-            'action'   => 'event_registrations',
940
-            'event_id' => isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null,
941
-            'DTT_ID'   => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null,
942
-        );
943
-        $new_status = false;
944
-        // bulk action check in toggle
945
-        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
946
-            // cycle thru checkboxes
947
-            while (list($REG_ID, $value) = each($this->_req_data['checkbox'])) {
948
-                $DTT_ID = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null;
949
-                $new_status = $this->_toggle_checkin($REG_ID, $DTT_ID);
950
-            }
951
-        } elseif (isset($this->_req_data['_regid'])) {
952
-            // coming from ajax request
953
-            $DTT_ID = isset($this->_req_data['dttid']) ? $this->_req_data['dttid'] : null;
954
-            $query_args['DTT_ID'] = $DTT_ID;
955
-            $new_status = $this->_toggle_checkin($this->_req_data['_regid'], $DTT_ID);
956
-        } else {
957
-            EE_Error::add_error(
958
-                esc_html__('Missing some required data to toggle the Check-in', 'event_espresso'),
959
-                __FILE__,
960
-                __FUNCTION__,
961
-                __LINE__
962
-            );
963
-        }
964
-        if (defined('DOING_AJAX')) {
965
-            return $new_status;
966
-        }
967
-        $this->_redirect_after_action(false, '', '', $query_args, true);
968
-    }
925
+	/**
926
+	 * handles toggling the checkin status for the registration,
927
+	 *
928
+	 * @access protected
929
+	 * @return int|void
930
+	 * @throws EE_Error
931
+	 * @throws InvalidArgumentException
932
+	 * @throws InvalidDataTypeException
933
+	 * @throws InvalidInterfaceException
934
+	 */
935
+	protected function _toggle_checkin_status()
936
+	{
937
+		// first let's get the query args out of the way for the redirect
938
+		$query_args = array(
939
+			'action'   => 'event_registrations',
940
+			'event_id' => isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null,
941
+			'DTT_ID'   => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null,
942
+		);
943
+		$new_status = false;
944
+		// bulk action check in toggle
945
+		if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
946
+			// cycle thru checkboxes
947
+			while (list($REG_ID, $value) = each($this->_req_data['checkbox'])) {
948
+				$DTT_ID = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null;
949
+				$new_status = $this->_toggle_checkin($REG_ID, $DTT_ID);
950
+			}
951
+		} elseif (isset($this->_req_data['_regid'])) {
952
+			// coming from ajax request
953
+			$DTT_ID = isset($this->_req_data['dttid']) ? $this->_req_data['dttid'] : null;
954
+			$query_args['DTT_ID'] = $DTT_ID;
955
+			$new_status = $this->_toggle_checkin($this->_req_data['_regid'], $DTT_ID);
956
+		} else {
957
+			EE_Error::add_error(
958
+				esc_html__('Missing some required data to toggle the Check-in', 'event_espresso'),
959
+				__FILE__,
960
+				__FUNCTION__,
961
+				__LINE__
962
+			);
963
+		}
964
+		if (defined('DOING_AJAX')) {
965
+			return $new_status;
966
+		}
967
+		$this->_redirect_after_action(false, '', '', $query_args, true);
968
+	}
969 969
 
970 970
 
971
-    /**
972
-     * This is toggles a single Check-in for the given registration and datetime.
973
-     *
974
-     * @param  int $REG_ID The registration we're toggling
975
-     * @param  int $DTT_ID The datetime we're toggling
976
-     * @return int The new status toggled to.
977
-     * @throws EE_Error
978
-     * @throws InvalidArgumentException
979
-     * @throws InvalidDataTypeException
980
-     * @throws InvalidInterfaceException
981
-     */
982
-    private function _toggle_checkin($REG_ID, $DTT_ID)
983
-    {
984
-        /** @var EE_Registration $REG */
985
-        $REG = EEM_Registration::instance()->get_one_by_ID($REG_ID);
986
-        $new_status = $REG->toggle_checkin_status($DTT_ID);
987
-        if ($new_status !== false) {
988
-            EE_Error::add_success($REG->get_checkin_msg($DTT_ID));
989
-        } else {
990
-            EE_Error::add_error($REG->get_checkin_msg($DTT_ID, true), __FILE__, __FUNCTION__, __LINE__);
991
-            $new_status = false;
992
-        }
993
-        return $new_status;
994
-    }
971
+	/**
972
+	 * This is toggles a single Check-in for the given registration and datetime.
973
+	 *
974
+	 * @param  int $REG_ID The registration we're toggling
975
+	 * @param  int $DTT_ID The datetime we're toggling
976
+	 * @return int The new status toggled to.
977
+	 * @throws EE_Error
978
+	 * @throws InvalidArgumentException
979
+	 * @throws InvalidDataTypeException
980
+	 * @throws InvalidInterfaceException
981
+	 */
982
+	private function _toggle_checkin($REG_ID, $DTT_ID)
983
+	{
984
+		/** @var EE_Registration $REG */
985
+		$REG = EEM_Registration::instance()->get_one_by_ID($REG_ID);
986
+		$new_status = $REG->toggle_checkin_status($DTT_ID);
987
+		if ($new_status !== false) {
988
+			EE_Error::add_success($REG->get_checkin_msg($DTT_ID));
989
+		} else {
990
+			EE_Error::add_error($REG->get_checkin_msg($DTT_ID, true), __FILE__, __FUNCTION__, __LINE__);
991
+			$new_status = false;
992
+		}
993
+		return $new_status;
994
+	}
995 995
 
996 996
 
997
-    /**
998
-     * Takes care of deleting multiple EE_Checkin table rows
999
-     *
1000
-     * @access protected
1001
-     * @return void
1002
-     * @throws EE_Error
1003
-     * @throws InvalidArgumentException
1004
-     * @throws InvalidDataTypeException
1005
-     * @throws InvalidInterfaceException
1006
-     */
1007
-    protected function _delete_checkin_rows()
1008
-    {
1009
-        $query_args = array(
1010
-            'action'  => 'registration_checkins',
1011
-            'DTT_ID'  => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0,
1012
-            '_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0,
1013
-        );
1014
-        $errors = 0;
1015
-        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
1016
-            while (list($CHK_ID, $value) = each($this->_req_data['checkbox'])) {
1017
-                if (! EEM_Checkin::instance()->delete_by_ID($CHK_ID)) {
1018
-                    $errors++;
1019
-                }
1020
-            }
1021
-        } else {
1022
-            EE_Error::add_error(
1023
-                esc_html__(
1024
-                    'So, something went wrong with the bulk delete because there was no data received for instructions on WHAT to delete!',
1025
-                    'event_espresso'
1026
-                ),
1027
-                __FILE__,
1028
-                __FUNCTION__,
1029
-                __LINE__
1030
-            );
1031
-            $this->_redirect_after_action(false, '', '', $query_args, true);
1032
-        }
1033
-        if ($errors > 0) {
1034
-            EE_Error::add_error(
1035
-                sprintf(__('There were %d records that did not delete successfully', 'event_espresso'), $errors),
1036
-                __FILE__,
1037
-                __FUNCTION__,
1038
-                __LINE__
1039
-            );
1040
-        } else {
1041
-            EE_Error::add_success(__('Records were successfully deleted', 'event_espresso'));
1042
-        }
1043
-        $this->_redirect_after_action(false, '', '', $query_args, true);
1044
-    }
997
+	/**
998
+	 * Takes care of deleting multiple EE_Checkin table rows
999
+	 *
1000
+	 * @access protected
1001
+	 * @return void
1002
+	 * @throws EE_Error
1003
+	 * @throws InvalidArgumentException
1004
+	 * @throws InvalidDataTypeException
1005
+	 * @throws InvalidInterfaceException
1006
+	 */
1007
+	protected function _delete_checkin_rows()
1008
+	{
1009
+		$query_args = array(
1010
+			'action'  => 'registration_checkins',
1011
+			'DTT_ID'  => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0,
1012
+			'_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0,
1013
+		);
1014
+		$errors = 0;
1015
+		if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
1016
+			while (list($CHK_ID, $value) = each($this->_req_data['checkbox'])) {
1017
+				if (! EEM_Checkin::instance()->delete_by_ID($CHK_ID)) {
1018
+					$errors++;
1019
+				}
1020
+			}
1021
+		} else {
1022
+			EE_Error::add_error(
1023
+				esc_html__(
1024
+					'So, something went wrong with the bulk delete because there was no data received for instructions on WHAT to delete!',
1025
+					'event_espresso'
1026
+				),
1027
+				__FILE__,
1028
+				__FUNCTION__,
1029
+				__LINE__
1030
+			);
1031
+			$this->_redirect_after_action(false, '', '', $query_args, true);
1032
+		}
1033
+		if ($errors > 0) {
1034
+			EE_Error::add_error(
1035
+				sprintf(__('There were %d records that did not delete successfully', 'event_espresso'), $errors),
1036
+				__FILE__,
1037
+				__FUNCTION__,
1038
+				__LINE__
1039
+			);
1040
+		} else {
1041
+			EE_Error::add_success(__('Records were successfully deleted', 'event_espresso'));
1042
+		}
1043
+		$this->_redirect_after_action(false, '', '', $query_args, true);
1044
+	}
1045 1045
 
1046 1046
 
1047
-    /**
1048
-     * Deletes a single EE_Checkin row
1049
-     *
1050
-     * @return void
1051
-     * @throws EE_Error
1052
-     * @throws InvalidArgumentException
1053
-     * @throws InvalidDataTypeException
1054
-     * @throws InvalidInterfaceException
1055
-     */
1056
-    protected function _delete_checkin_row()
1057
-    {
1058
-        $query_args = array(
1059
-            'action'  => 'registration_checkins',
1060
-            'DTT_ID'  => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0,
1061
-            '_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0,
1062
-        );
1063
-        if (! empty($this->_req_data['CHK_ID'])) {
1064
-            if (! EEM_Checkin::instance()->delete_by_ID($this->_req_data['CHK_ID'])) {
1065
-                EE_Error::add_error(
1066
-                    esc_html__('Something went wrong and this check-in record was not deleted', 'event_espresso'),
1067
-                    __FILE__,
1068
-                    __FUNCTION__,
1069
-                    __LINE__
1070
-                );
1071
-            } else {
1072
-                EE_Error::add_success(__('Check-In record successfully deleted', 'event_espresso'));
1073
-            }
1074
-        } else {
1075
-            EE_Error::add_error(
1076
-                esc_html__(
1077
-                    'In order to delete a Check-in record, there must be a Check-In ID available. There is not. It is not your fault, there is just a gremlin living in the code',
1078
-                    'event_espresso'
1079
-                ),
1080
-                __FILE__,
1081
-                __FUNCTION__,
1082
-                __LINE__
1083
-            );
1084
-        }
1085
-        $this->_redirect_after_action(false, '', '', $query_args, true);
1086
-    }
1047
+	/**
1048
+	 * Deletes a single EE_Checkin row
1049
+	 *
1050
+	 * @return void
1051
+	 * @throws EE_Error
1052
+	 * @throws InvalidArgumentException
1053
+	 * @throws InvalidDataTypeException
1054
+	 * @throws InvalidInterfaceException
1055
+	 */
1056
+	protected function _delete_checkin_row()
1057
+	{
1058
+		$query_args = array(
1059
+			'action'  => 'registration_checkins',
1060
+			'DTT_ID'  => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0,
1061
+			'_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0,
1062
+		);
1063
+		if (! empty($this->_req_data['CHK_ID'])) {
1064
+			if (! EEM_Checkin::instance()->delete_by_ID($this->_req_data['CHK_ID'])) {
1065
+				EE_Error::add_error(
1066
+					esc_html__('Something went wrong and this check-in record was not deleted', 'event_espresso'),
1067
+					__FILE__,
1068
+					__FUNCTION__,
1069
+					__LINE__
1070
+				);
1071
+			} else {
1072
+				EE_Error::add_success(__('Check-In record successfully deleted', 'event_espresso'));
1073
+			}
1074
+		} else {
1075
+			EE_Error::add_error(
1076
+				esc_html__(
1077
+					'In order to delete a Check-in record, there must be a Check-In ID available. There is not. It is not your fault, there is just a gremlin living in the code',
1078
+					'event_espresso'
1079
+				),
1080
+				__FILE__,
1081
+				__FUNCTION__,
1082
+				__LINE__
1083
+			);
1084
+		}
1085
+		$this->_redirect_after_action(false, '', '', $query_args, true);
1086
+	}
1087 1087
 
1088 1088
 
1089
-    /**
1090
-     *        generates HTML for the Event Registrations List Table
1091
-     *
1092
-     * @access protected
1093
-     * @return void
1094
-     * @throws EE_Error
1095
-     * @throws InvalidArgumentException
1096
-     * @throws InvalidDataTypeException
1097
-     * @throws InvalidInterfaceException
1098
-     */
1099
-    protected function _event_registrations_list_table()
1100
-    {
1101
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1102
-        $this->_admin_page_title .= isset($this->_req_data['event_id'])
1103
-            ? $this->get_action_link_or_button(
1104
-                'new_registration',
1105
-                'add-registrant',
1106
-                array('event_id' => $this->_req_data['event_id']),
1107
-                'add-new-h2',
1108
-                '',
1109
-                false
1110
-            )
1111
-            : '';
1112
-        $checked_in = new CheckinStatusDashicon(EE_Checkin::status_checked_in);
1113
-        $checked_out = new CheckinStatusDashicon(EE_Checkin::status_checked_out);
1114
-        $checked_never = new CheckinStatusDashicon(EE_Checkin::status_checked_never);
1115
-        $legend_items = array(
1116
-            'star-icon'        => array(
1117
-                'class' => 'dashicons dashicons-star-filled lt-blue-icon ee-icon-size-8',
1118
-                'desc'  => esc_html__('This Registrant is the Primary Registrant', 'event_espresso'),
1119
-            ),
1120
-            'checkin'          => array(
1121
-                'class' => $checked_in->cssClasses(),
1122
-                'desc'  => $checked_in->legendLabel(),
1123
-            ),
1124
-            'checkout'         => array(
1125
-                'class' => $checked_out->cssClasses(),
1126
-                'desc'  => $checked_out->legendLabel(),
1127
-            ),
1128
-            'nocheckinrecord'  => array(
1129
-                'class' => $checked_never->cssClasses(),
1130
-                'desc'  => $checked_never->legendLabel(),
1131
-            ),
1132
-            'approved_status'  => array(
1133
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_approved,
1134
-                'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_approved, false, 'sentence'),
1135
-            ),
1136
-            'cancelled_status' => array(
1137
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_cancelled,
1138
-                'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_cancelled, false, 'sentence'),
1139
-            ),
1140
-            'declined_status'  => array(
1141
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_declined,
1142
-                'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_declined, false, 'sentence'),
1143
-            ),
1144
-            'not_approved'     => array(
1145
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_not_approved,
1146
-                'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_not_approved, false, 'sentence'),
1147
-            ),
1148
-            'pending_status'   => array(
1149
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_pending_payment,
1150
-                'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_pending_payment, false, 'sentence'),
1151
-            ),
1152
-            'wait_list'        => array(
1153
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_wait_list,
1154
-                'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_wait_list, false, 'sentence'),
1155
-            ),
1156
-        );
1157
-        $this->_template_args['after_list_table'] = $this->_display_legend($legend_items);
1158
-        $event_id = isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null;
1159
-        /** @var EE_Event $event */
1160
-        $event = EEM_Event::instance()->get_one_by_ID($event_id);
1161
-        $this->_template_args['before_list_table'] = $event instanceof EE_Event
1162
-            ? '<h2>' . sprintf(
1163
-                esc_html__('Viewing Registrations for Event: %s', 'event_espresso'),
1164
-                EEM_Event::instance()->get_one_by_ID($event_id)->get('EVT_name')
1165
-            ) . '</h2>'
1166
-            : '';
1167
-        // need to get the number of datetimes on the event and set default datetime_id if there is only one datetime on
1168
-        // the event.
1169
-        $DTT_ID = ! empty($this->_req_data['DTT_ID']) ? absint($this->_req_data['DTT_ID']) : 0;
1170
-        $datetime = null;
1171
-        if ($event instanceof EE_Event) {
1172
-            $datetimes_on_event = $event->datetimes();
1173
-            if (count($datetimes_on_event) === 1) {
1174
-                $datetime = reset($datetimes_on_event);
1175
-            }
1176
-        }
1177
-        $datetime = $datetime instanceof EE_Datetime ? $datetime : EEM_Datetime::instance()->get_one_by_ID($DTT_ID);
1178
-        if ($datetime instanceof EE_Datetime && $this->_template_args['before_list_table'] !== '') {
1179
-            $this->_template_args['before_list_table'] = substr($this->_template_args['before_list_table'], 0, -5);
1180
-            $this->_template_args['before_list_table'] .= ' &nbsp;<span class="drk-grey-text">';
1181
-            $this->_template_args['before_list_table'] .= '<span class="dashicons dashicons-calendar"></span>';
1182
-            $this->_template_args['before_list_table'] .= $datetime->name();
1183
-            $this->_template_args['before_list_table'] .= ' ( ' . $datetime->date_and_time_range() . ' )';
1184
-            $this->_template_args['before_list_table'] .= '</span></h2>';
1185
-        }
1186
-        // if no datetime, then we're on the initial view, so let's give some helpful instructions on what the status
1187
-        // column represents
1188
-        if (! $datetime instanceof EE_Datetime) {
1189
-            $this->_template_args['before_list_table'] .= '<br><p class="description">'
1190
-                                                          . esc_html__(
1191
-                                                              'In this view, the check-in status represents the latest check-in record for the registration in that row.',
1192
-                                                              'event_espresso'
1193
-                                                          )
1194
-                                                          . '</p>';
1195
-        }
1196
-        $this->display_admin_list_table_page_with_no_sidebar();
1197
-    }
1089
+	/**
1090
+	 *        generates HTML for the Event Registrations List Table
1091
+	 *
1092
+	 * @access protected
1093
+	 * @return void
1094
+	 * @throws EE_Error
1095
+	 * @throws InvalidArgumentException
1096
+	 * @throws InvalidDataTypeException
1097
+	 * @throws InvalidInterfaceException
1098
+	 */
1099
+	protected function _event_registrations_list_table()
1100
+	{
1101
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1102
+		$this->_admin_page_title .= isset($this->_req_data['event_id'])
1103
+			? $this->get_action_link_or_button(
1104
+				'new_registration',
1105
+				'add-registrant',
1106
+				array('event_id' => $this->_req_data['event_id']),
1107
+				'add-new-h2',
1108
+				'',
1109
+				false
1110
+			)
1111
+			: '';
1112
+		$checked_in = new CheckinStatusDashicon(EE_Checkin::status_checked_in);
1113
+		$checked_out = new CheckinStatusDashicon(EE_Checkin::status_checked_out);
1114
+		$checked_never = new CheckinStatusDashicon(EE_Checkin::status_checked_never);
1115
+		$legend_items = array(
1116
+			'star-icon'        => array(
1117
+				'class' => 'dashicons dashicons-star-filled lt-blue-icon ee-icon-size-8',
1118
+				'desc'  => esc_html__('This Registrant is the Primary Registrant', 'event_espresso'),
1119
+			),
1120
+			'checkin'          => array(
1121
+				'class' => $checked_in->cssClasses(),
1122
+				'desc'  => $checked_in->legendLabel(),
1123
+			),
1124
+			'checkout'         => array(
1125
+				'class' => $checked_out->cssClasses(),
1126
+				'desc'  => $checked_out->legendLabel(),
1127
+			),
1128
+			'nocheckinrecord'  => array(
1129
+				'class' => $checked_never->cssClasses(),
1130
+				'desc'  => $checked_never->legendLabel(),
1131
+			),
1132
+			'approved_status'  => array(
1133
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_approved,
1134
+				'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_approved, false, 'sentence'),
1135
+			),
1136
+			'cancelled_status' => array(
1137
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_cancelled,
1138
+				'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_cancelled, false, 'sentence'),
1139
+			),
1140
+			'declined_status'  => array(
1141
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_declined,
1142
+				'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_declined, false, 'sentence'),
1143
+			),
1144
+			'not_approved'     => array(
1145
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_not_approved,
1146
+				'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_not_approved, false, 'sentence'),
1147
+			),
1148
+			'pending_status'   => array(
1149
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_pending_payment,
1150
+				'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_pending_payment, false, 'sentence'),
1151
+			),
1152
+			'wait_list'        => array(
1153
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_wait_list,
1154
+				'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_wait_list, false, 'sentence'),
1155
+			),
1156
+		);
1157
+		$this->_template_args['after_list_table'] = $this->_display_legend($legend_items);
1158
+		$event_id = isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null;
1159
+		/** @var EE_Event $event */
1160
+		$event = EEM_Event::instance()->get_one_by_ID($event_id);
1161
+		$this->_template_args['before_list_table'] = $event instanceof EE_Event
1162
+			? '<h2>' . sprintf(
1163
+				esc_html__('Viewing Registrations for Event: %s', 'event_espresso'),
1164
+				EEM_Event::instance()->get_one_by_ID($event_id)->get('EVT_name')
1165
+			) . '</h2>'
1166
+			: '';
1167
+		// need to get the number of datetimes on the event and set default datetime_id if there is only one datetime on
1168
+		// the event.
1169
+		$DTT_ID = ! empty($this->_req_data['DTT_ID']) ? absint($this->_req_data['DTT_ID']) : 0;
1170
+		$datetime = null;
1171
+		if ($event instanceof EE_Event) {
1172
+			$datetimes_on_event = $event->datetimes();
1173
+			if (count($datetimes_on_event) === 1) {
1174
+				$datetime = reset($datetimes_on_event);
1175
+			}
1176
+		}
1177
+		$datetime = $datetime instanceof EE_Datetime ? $datetime : EEM_Datetime::instance()->get_one_by_ID($DTT_ID);
1178
+		if ($datetime instanceof EE_Datetime && $this->_template_args['before_list_table'] !== '') {
1179
+			$this->_template_args['before_list_table'] = substr($this->_template_args['before_list_table'], 0, -5);
1180
+			$this->_template_args['before_list_table'] .= ' &nbsp;<span class="drk-grey-text">';
1181
+			$this->_template_args['before_list_table'] .= '<span class="dashicons dashicons-calendar"></span>';
1182
+			$this->_template_args['before_list_table'] .= $datetime->name();
1183
+			$this->_template_args['before_list_table'] .= ' ( ' . $datetime->date_and_time_range() . ' )';
1184
+			$this->_template_args['before_list_table'] .= '</span></h2>';
1185
+		}
1186
+		// if no datetime, then we're on the initial view, so let's give some helpful instructions on what the status
1187
+		// column represents
1188
+		if (! $datetime instanceof EE_Datetime) {
1189
+			$this->_template_args['before_list_table'] .= '<br><p class="description">'
1190
+														  . esc_html__(
1191
+															  'In this view, the check-in status represents the latest check-in record for the registration in that row.',
1192
+															  'event_espresso'
1193
+														  )
1194
+														  . '</p>';
1195
+		}
1196
+		$this->display_admin_list_table_page_with_no_sidebar();
1197
+	}
1198 1198
 
1199
-    /**
1200
-     * Download the registrations check-in report (same as the normal registration report, but with different where
1201
-     * conditions)
1202
-     *
1203
-     * @return void ends the request by a redirect or download
1204
-     */
1205
-    public function _registrations_checkin_report()
1206
-    {
1207
-        $this->_registrations_report_base('_get_checkin_query_params_from_request');
1208
-    }
1199
+	/**
1200
+	 * Download the registrations check-in report (same as the normal registration report, but with different where
1201
+	 * conditions)
1202
+	 *
1203
+	 * @return void ends the request by a redirect or download
1204
+	 */
1205
+	public function _registrations_checkin_report()
1206
+	{
1207
+		$this->_registrations_report_base('_get_checkin_query_params_from_request');
1208
+	}
1209 1209
 
1210
-    /**
1211
-     * Gets the query params from the request, plus adds a where condition for the registration status,
1212
-     * because on the checkin page we only ever want to see approved and pending-approval registrations
1213
-     *
1214
-     * @param array $request
1215
-     * @param int   $per_page
1216
-     * @param bool  $count
1217
-     * @return array
1218
-     * @throws EE_Error
1219
-     */
1220
-    protected function _get_checkin_query_params_from_request(
1221
-        $request,
1222
-        $per_page = 10,
1223
-        $count = false
1224
-    ) {
1225
-        $query_params = $this->_get_registration_query_parameters($request, $per_page, $count);
1226
-        // unlike the regular registrations list table,
1227
-        $status_ids_array = apply_filters(
1228
-            'FHEE__Extend_Registrations_Admin_Page__get_event_attendees__status_ids_array',
1229
-            array(EEM_Registration::status_id_pending_payment, EEM_Registration::status_id_approved)
1230
-        );
1231
-        $query_params[0]['STS_ID'] = array('IN', $status_ids_array);
1232
-        return $query_params;
1233
-    }
1210
+	/**
1211
+	 * Gets the query params from the request, plus adds a where condition for the registration status,
1212
+	 * because on the checkin page we only ever want to see approved and pending-approval registrations
1213
+	 *
1214
+	 * @param array $request
1215
+	 * @param int   $per_page
1216
+	 * @param bool  $count
1217
+	 * @return array
1218
+	 * @throws EE_Error
1219
+	 */
1220
+	protected function _get_checkin_query_params_from_request(
1221
+		$request,
1222
+		$per_page = 10,
1223
+		$count = false
1224
+	) {
1225
+		$query_params = $this->_get_registration_query_parameters($request, $per_page, $count);
1226
+		// unlike the regular registrations list table,
1227
+		$status_ids_array = apply_filters(
1228
+			'FHEE__Extend_Registrations_Admin_Page__get_event_attendees__status_ids_array',
1229
+			array(EEM_Registration::status_id_pending_payment, EEM_Registration::status_id_approved)
1230
+		);
1231
+		$query_params[0]['STS_ID'] = array('IN', $status_ids_array);
1232
+		return $query_params;
1233
+	}
1234 1234
 
1235 1235
 
1236
-    /**
1237
-     * Gets registrations for an event
1238
-     *
1239
-     * @param int    $per_page
1240
-     * @param bool   $count whether to return count or data.
1241
-     * @param bool   $trash
1242
-     * @param string $orderby
1243
-     * @return EE_Registration[]|int
1244
-     * @throws EE_Error
1245
-     * @throws InvalidArgumentException
1246
-     * @throws InvalidDataTypeException
1247
-     * @throws InvalidInterfaceException
1248
-     */
1249
-    public function get_event_attendees($per_page = 10, $count = false, $trash = false, $orderby = 'ATT_fname')
1250
-    {
1251
-        // normalize some request params that get setup by the parent `get_registrations` method.
1252
-        $request = $this->_req_data;
1253
-        $request['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : $orderby;
1254
-        $request['order'] = ! empty($this->_req_data['order']) ? $this->_req_data['order'] : 'ASC';
1255
-        if ($trash) {
1256
-            $request['status'] = 'trash';
1257
-        }
1258
-        $query_params = $this->_get_checkin_query_params_from_request($request, $per_page, $count);
1259
-        /**
1260
-         * Override the default groupby added by EEM_Base so that sorts with multiple order bys work as expected
1261
-         *
1262
-         * @link https://events.codebasehq.com/projects/event-espresso/tickets/10093
1263
-         * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1264
-         *                             or if you have the development copy of EE you can view this at the path:
1265
-         *                             /docs/G--Model-System/model-query-params.md
1266
-         */
1267
-        $query_params['group_by'] = '';
1236
+	/**
1237
+	 * Gets registrations for an event
1238
+	 *
1239
+	 * @param int    $per_page
1240
+	 * @param bool   $count whether to return count or data.
1241
+	 * @param bool   $trash
1242
+	 * @param string $orderby
1243
+	 * @return EE_Registration[]|int
1244
+	 * @throws EE_Error
1245
+	 * @throws InvalidArgumentException
1246
+	 * @throws InvalidDataTypeException
1247
+	 * @throws InvalidInterfaceException
1248
+	 */
1249
+	public function get_event_attendees($per_page = 10, $count = false, $trash = false, $orderby = 'ATT_fname')
1250
+	{
1251
+		// normalize some request params that get setup by the parent `get_registrations` method.
1252
+		$request = $this->_req_data;
1253
+		$request['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : $orderby;
1254
+		$request['order'] = ! empty($this->_req_data['order']) ? $this->_req_data['order'] : 'ASC';
1255
+		if ($trash) {
1256
+			$request['status'] = 'trash';
1257
+		}
1258
+		$query_params = $this->_get_checkin_query_params_from_request($request, $per_page, $count);
1259
+		/**
1260
+		 * Override the default groupby added by EEM_Base so that sorts with multiple order bys work as expected
1261
+		 *
1262
+		 * @link https://events.codebasehq.com/projects/event-espresso/tickets/10093
1263
+		 * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1264
+		 *                             or if you have the development copy of EE you can view this at the path:
1265
+		 *                             /docs/G--Model-System/model-query-params.md
1266
+		 */
1267
+		$query_params['group_by'] = '';
1268 1268
 
1269
-        return $count
1270
-            ? EEM_Registration::instance()->count($query_params)
1271
-            /** @type EE_Registration[] */
1272
-            : EEM_Registration::instance()->get_all($query_params);
1273
-    }
1269
+		return $count
1270
+			? EEM_Registration::instance()->count($query_params)
1271
+			/** @type EE_Registration[] */
1272
+			: EEM_Registration::instance()->get_all($query_params);
1273
+	}
1274 1274
 }
Please login to merge, or discard this patch.
Spacing   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -32,10 +32,10 @@  discard block
 block discarded – undo
32 32
     public function __construct($routing = true)
33 33
     {
34 34
         parent::__construct($routing);
35
-        if (! defined('REG_CAF_TEMPLATE_PATH')) {
36
-            define('REG_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/templates/');
37
-            define('REG_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/assets/');
38
-            define('REG_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'registrations/assets/');
35
+        if ( ! defined('REG_CAF_TEMPLATE_PATH')) {
36
+            define('REG_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND.'registrations/templates/');
37
+            define('REG_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND.'registrations/assets/');
38
+            define('REG_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL.'registrations/assets/');
39 39
         }
40 40
     }
41 41
 
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
      */
46 46
     protected function _extend_page_config()
47 47
     {
48
-        $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'registrations';
48
+        $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND.'registrations';
49 49
         $reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID'])
50 50
             ? $this->_req_data['_REG_ID']
51 51
             : 0;
@@ -185,14 +185,14 @@  discard block
 block discarded – undo
185 185
             // enqueue newsletter js
186 186
             wp_enqueue_script(
187 187
                 'ee-newsletter-trigger',
188
-                REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.js',
188
+                REG_CAF_ASSETS_URL.'ee-newsletter-trigger.js',
189 189
                 array('ee-dialog'),
190 190
                 EVENT_ESPRESSO_VERSION,
191 191
                 true
192 192
             );
193 193
             wp_enqueue_style(
194 194
                 'ee-newsletter-trigger-css',
195
-                REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.css',
195
+                REG_CAF_ASSETS_URL.'ee-newsletter-trigger.css',
196 196
                 array(),
197 197
                 EVENT_ESPRESSO_VERSION
198 198
             );
@@ -213,7 +213,7 @@  discard block
 block discarded – undo
213 213
     {
214 214
         wp_register_script(
215 215
             'ee-reg-reports-js',
216
-            REG_CAF_ASSETS_URL . 'ee-registration-admin-reports.js',
216
+            REG_CAF_ASSETS_URL.'ee-registration-admin-reports.js',
217 217
             array('google-charts'),
218 218
             EVENT_ESPRESSO_VERSION,
219 219
             true
@@ -299,7 +299,7 @@  discard block
 block discarded – undo
299 299
         $nonce_ref = 'get_newsletter_form_content_nonce';
300 300
         $this->_verify_nonce($nonce, $nonce_ref);
301 301
         // let's get the mtp for the incoming MTP_ ID
302
-        if (! isset($this->_req_data['GRP_ID'])) {
302
+        if ( ! isset($this->_req_data['GRP_ID'])) {
303 303
             EE_Error::add_error(
304 304
                 esc_html__(
305 305
                     'There must be something broken with the js or html structure because the required data for getting a message template group is not present (need an GRP_ID).',
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
             $this->_return_json();
315 315
         }
316 316
         $MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID($this->_req_data['GRP_ID']);
317
-        if (! $MTPG instanceof EE_Message_Template_Group) {
317
+        if ( ! $MTPG instanceof EE_Message_Template_Group) {
318 318
             EE_Error::add_error(
319 319
                 sprintf(
320 320
                     esc_html__(
@@ -339,12 +339,12 @@  discard block
 block discarded – undo
339 339
             $field = $MTP->get('MTP_template_field');
340 340
             if ($field === 'content') {
341 341
                 $content = $MTP->get('MTP_content');
342
-                if (! empty($content['newsletter_content'])) {
342
+                if ( ! empty($content['newsletter_content'])) {
343 343
                     $template_fields['newsletter_content'] = $content['newsletter_content'];
344 344
                 }
345 345
                 continue;
346 346
             }
347
-            $template_fields[ $MTP->get('MTP_template_field') ] = $MTP->get('MTP_content');
347
+            $template_fields[$MTP->get('MTP_template_field')] = $MTP->get('MTP_content');
348 348
         }
349 349
         $this->_template_args['success'] = true;
350 350
         $this->_template_args['error'] = false;
@@ -375,7 +375,7 @@  discard block
 block discarded – undo
375 375
      */
376 376
     public function add_newsletter_action_buttons(EE_Admin_List_Table $list_table)
377 377
     {
378
-        if (! EE_Registry::instance()->CAP->current_user_can(
378
+        if ( ! EE_Registry::instance()->CAP->current_user_can(
379 379
             'ee_send_message',
380 380
             'espresso_registrations_newsletter_selected_send'
381 381
         )
@@ -444,17 +444,17 @@  discard block
 block discarded – undo
444 444
                 $field_id = $field === '[NEWSLETTER_CONTENT]'
445 445
                     ? 'content'
446 446
                     : $field;
447
-                $field_id = 'batch-message-' . strtolower($field_id);
447
+                $field_id = 'batch-message-'.strtolower($field_id);
448 448
                 $available_shortcodes[] = '<span class="js-shortcode-selection" data-value="'
449 449
                                           . $shortcode
450
-                                          . '" data-linked-input-id="' . $field_id . '">'
450
+                                          . '" data-linked-input-id="'.$field_id.'">'
451 451
                                           . $shortcode
452 452
                                           . '</span>';
453 453
             }
454
-            $codes[ $field ] = implode(', ', $available_shortcodes);
454
+            $codes[$field] = implode(', ', $available_shortcodes);
455 455
         }
456 456
         $shortcodes = $codes;
457
-        $form_template = REG_CAF_TEMPLATE_PATH . 'newsletter-send-form.template.php';
457
+        $form_template = REG_CAF_TEMPLATE_PATH.'newsletter-send-form.template.php';
458 458
         $form_template_args = array(
459 459
             'form_action'       => admin_url('admin.php?page=espresso_registrations'),
460 460
             'form_route'        => 'newsletter_selected_send',
@@ -622,7 +622,7 @@  discard block
 block discarded – undo
622 622
      */
623 623
     protected function _registration_reports()
624 624
     {
625
-        $template_path = EE_ADMIN_TEMPLATE . 'admin_reports.template.php';
625
+        $template_path = EE_ADMIN_TEMPLATE.'admin_reports.template.php';
626 626
         $this->_template_args['admin_page_content'] = EEH_Template::display_template(
627 627
             $template_path,
628 628
             $this->_reports_template_data,
@@ -677,7 +677,7 @@  discard block
 block discarded – undo
677 677
             array_unshift($regs, $column_titles);
678 678
             // setup the date range.
679 679
             $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone());
680
-            $beginning_date = new DateTime("now " . $period, $DateTimeZone);
680
+            $beginning_date = new DateTime("now ".$period, $DateTimeZone);
681 681
             $ending_date = new DateTime("now", $DateTimeZone);
682 682
             $subtitle = sprintf(
683 683
                 _x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'),
@@ -697,7 +697,7 @@  discard block
 block discarded – undo
697 697
                     '%sThere are currently no registration records in the last month for this report.%s',
698 698
                     'event_espresso'
699 699
                 ),
700
-                '<h2>' . $report_title . '</h2><p>',
700
+                '<h2>'.$report_title.'</h2><p>',
701 701
                 '</p>'
702 702
             ),
703 703
         );
@@ -750,7 +750,7 @@  discard block
 block discarded – undo
750 750
             array_unshift($regs, $column_titles);
751 751
             // setup the date range.
752 752
             $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone());
753
-            $beginning_date = new DateTime("now " . $period, $DateTimeZone);
753
+            $beginning_date = new DateTime("now ".$period, $DateTimeZone);
754 754
             $ending_date = new DateTime("now", $DateTimeZone);
755 755
             $subtitle = sprintf(
756 756
                 _x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'),
@@ -770,7 +770,7 @@  discard block
 block discarded – undo
770 770
                     '%sThere are currently no registration records in the last month for this report.%s',
771 771
                     'event_espresso'
772 772
                 ),
773
-                '<h2>' . $report_title . '</h2><p>',
773
+                '<h2>'.$report_title.'</h2><p>',
774 774
                 '</p>'
775 775
             ),
776 776
         );
@@ -823,7 +823,7 @@  discard block
 block discarded – undo
823 823
         if ($datetime instanceof EE_Datetime) {
824 824
             $datetime_label = $datetime->get_dtt_display_name(true);
825 825
             $datetime_label .= ! empty($datetime_label)
826
-                ? ' (' . $datetime->get_dtt_display_name() . ')'
826
+                ? ' ('.$datetime->get_dtt_display_name().')'
827 827
                 : $datetime->get_dtt_display_name();
828 828
         }
829 829
         $datetime_link = ! empty($dtt_id) && $registration instanceof EE_Registration
@@ -837,7 +837,7 @@  discard block
 block discarded – undo
837 837
             )
838 838
             : '';
839 839
         $datetime_link = ! empty($datetime_link)
840
-            ? '<a href="' . $datetime_link . '">'
840
+            ? '<a href="'.$datetime_link.'">'
841 841
               . '<span id="checkin-dtt">'
842 842
               . $datetime_label
843 843
               . '</span></a>'
@@ -849,8 +849,8 @@  discard block
 block discarded – undo
849 849
             ? $attendee->get_admin_details_link()
850 850
             : '';
851 851
         $attendee_link = ! empty($attendee_link)
852
-            ? '<a href="' . $attendee->get_admin_details_link() . '"'
853
-              . ' title="' . esc_html__('Click for attendee details', 'event_espresso') . '">'
852
+            ? '<a href="'.$attendee->get_admin_details_link().'"'
853
+              . ' title="'.esc_html__('Click for attendee details', 'event_espresso').'">'
854 854
               . '<span id="checkin-attendee-name">'
855 855
               . $attendee_name
856 856
               . '</span></a>'
@@ -859,25 +859,25 @@  discard block
 block discarded – undo
859 859
             ? $registration->event()->get_admin_details_link()
860 860
             : '';
861 861
         $event_link = ! empty($event_link)
862
-            ? '<a href="' . $event_link . '"'
863
-              . ' title="' . esc_html__('Click here to edit event.', 'event_espresso') . '">'
862
+            ? '<a href="'.$event_link.'"'
863
+              . ' title="'.esc_html__('Click here to edit event.', 'event_espresso').'">'
864 864
               . '<span id="checkin-event-name">'
865 865
               . $registration->event_name()
866 866
               . '</span>'
867 867
               . '</a>'
868 868
             : '';
869 869
         $this->_template_args['before_list_table'] = ! empty($reg_id) && ! empty($dtt_id)
870
-            ? '<h2>' . sprintf(
870
+            ? '<h2>'.sprintf(
871 871
                 esc_html__('Displaying check in records for %1$s for %2$s at the event, %3$s', 'event_espresso'),
872 872
                 $attendee_link,
873 873
                 $datetime_link,
874 874
                 $event_link
875
-            ) . '</h2>'
875
+            ).'</h2>'
876 876
             : '';
877 877
         $this->_template_args['list_table_hidden_fields'] = ! empty($reg_id)
878
-            ? '<input type="hidden" name="_REG_ID" value="' . $reg_id . '">' : '';
878
+            ? '<input type="hidden" name="_REG_ID" value="'.$reg_id.'">' : '';
879 879
         $this->_template_args['list_table_hidden_fields'] .= ! empty($dtt_id)
880
-            ? '<input type="hidden" name="DTT_ID" value="' . $dtt_id . '">' : '';
880
+            ? '<input type="hidden" name="DTT_ID" value="'.$dtt_id.'">' : '';
881 881
         $this->display_admin_list_table_page_with_no_sidebar();
882 882
     }
883 883
 
@@ -894,7 +894,7 @@  discard block
 block discarded – undo
894 894
     public function toggle_checkin_status()
895 895
     {
896 896
         // first make sure we have the necessary data
897
-        if (! isset($this->_req_data['_regid'])) {
897
+        if ( ! isset($this->_req_data['_regid'])) {
898 898
             EE_Error::add_error(
899 899
                 esc_html__(
900 900
                     'There must be something broken with the html structure because the required data for toggling the Check-in status is not being sent via ajax',
@@ -916,7 +916,7 @@  discard block
 block discarded – undo
916 916
         // beautiful! Made it this far so let's get the status.
917 917
         $new_status = new CheckinStatusDashicon($this->_toggle_checkin_status());
918 918
         // setup new class to return via ajax
919
-        $this->_template_args['admin_page_content'] = 'clickable trigger-checkin ' . $new_status->cssClasses();
919
+        $this->_template_args['admin_page_content'] = 'clickable trigger-checkin '.$new_status->cssClasses();
920 920
         $this->_template_args['success'] = true;
921 921
         $this->_return_json();
922 922
     }
@@ -942,7 +942,7 @@  discard block
 block discarded – undo
942 942
         );
943 943
         $new_status = false;
944 944
         // bulk action check in toggle
945
-        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
945
+        if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
946 946
             // cycle thru checkboxes
947 947
             while (list($REG_ID, $value) = each($this->_req_data['checkbox'])) {
948 948
                 $DTT_ID = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null;
@@ -1012,9 +1012,9 @@  discard block
 block discarded – undo
1012 1012
             '_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0,
1013 1013
         );
1014 1014
         $errors = 0;
1015
-        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
1015
+        if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
1016 1016
             while (list($CHK_ID, $value) = each($this->_req_data['checkbox'])) {
1017
-                if (! EEM_Checkin::instance()->delete_by_ID($CHK_ID)) {
1017
+                if ( ! EEM_Checkin::instance()->delete_by_ID($CHK_ID)) {
1018 1018
                     $errors++;
1019 1019
                 }
1020 1020
             }
@@ -1060,8 +1060,8 @@  discard block
 block discarded – undo
1060 1060
             'DTT_ID'  => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0,
1061 1061
             '_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0,
1062 1062
         );
1063
-        if (! empty($this->_req_data['CHK_ID'])) {
1064
-            if (! EEM_Checkin::instance()->delete_by_ID($this->_req_data['CHK_ID'])) {
1063
+        if ( ! empty($this->_req_data['CHK_ID'])) {
1064
+            if ( ! EEM_Checkin::instance()->delete_by_ID($this->_req_data['CHK_ID'])) {
1065 1065
                 EE_Error::add_error(
1066 1066
                     esc_html__('Something went wrong and this check-in record was not deleted', 'event_espresso'),
1067 1067
                     __FILE__,
@@ -1130,27 +1130,27 @@  discard block
 block discarded – undo
1130 1130
                 'desc'  => $checked_never->legendLabel(),
1131 1131
             ),
1132 1132
             'approved_status'  => array(
1133
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_approved,
1133
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_approved,
1134 1134
                 'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_approved, false, 'sentence'),
1135 1135
             ),
1136 1136
             'cancelled_status' => array(
1137
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_cancelled,
1137
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_cancelled,
1138 1138
                 'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_cancelled, false, 'sentence'),
1139 1139
             ),
1140 1140
             'declined_status'  => array(
1141
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_declined,
1141
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_declined,
1142 1142
                 'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_declined, false, 'sentence'),
1143 1143
             ),
1144 1144
             'not_approved'     => array(
1145
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_not_approved,
1145
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_not_approved,
1146 1146
                 'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_not_approved, false, 'sentence'),
1147 1147
             ),
1148 1148
             'pending_status'   => array(
1149
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_pending_payment,
1149
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_pending_payment,
1150 1150
                 'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_pending_payment, false, 'sentence'),
1151 1151
             ),
1152 1152
             'wait_list'        => array(
1153
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_wait_list,
1153
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_wait_list,
1154 1154
                 'desc'  => EEH_Template::pretty_status(EEM_Registration::status_id_wait_list, false, 'sentence'),
1155 1155
             ),
1156 1156
         );
@@ -1159,10 +1159,10 @@  discard block
 block discarded – undo
1159 1159
         /** @var EE_Event $event */
1160 1160
         $event = EEM_Event::instance()->get_one_by_ID($event_id);
1161 1161
         $this->_template_args['before_list_table'] = $event instanceof EE_Event
1162
-            ? '<h2>' . sprintf(
1162
+            ? '<h2>'.sprintf(
1163 1163
                 esc_html__('Viewing Registrations for Event: %s', 'event_espresso'),
1164 1164
                 EEM_Event::instance()->get_one_by_ID($event_id)->get('EVT_name')
1165
-            ) . '</h2>'
1165
+            ).'</h2>'
1166 1166
             : '';
1167 1167
         // need to get the number of datetimes on the event and set default datetime_id if there is only one datetime on
1168 1168
         // the event.
@@ -1180,12 +1180,12 @@  discard block
 block discarded – undo
1180 1180
             $this->_template_args['before_list_table'] .= ' &nbsp;<span class="drk-grey-text">';
1181 1181
             $this->_template_args['before_list_table'] .= '<span class="dashicons dashicons-calendar"></span>';
1182 1182
             $this->_template_args['before_list_table'] .= $datetime->name();
1183
-            $this->_template_args['before_list_table'] .= ' ( ' . $datetime->date_and_time_range() . ' )';
1183
+            $this->_template_args['before_list_table'] .= ' ( '.$datetime->date_and_time_range().' )';
1184 1184
             $this->_template_args['before_list_table'] .= '</span></h2>';
1185 1185
         }
1186 1186
         // if no datetime, then we're on the initial view, so let's give some helpful instructions on what the status
1187 1187
         // column represents
1188
-        if (! $datetime instanceof EE_Datetime) {
1188
+        if ( ! $datetime instanceof EE_Datetime) {
1189 1189
             $this->_template_args['before_list_table'] .= '<br><p class="description">'
1190 1190
                                                           . esc_html__(
1191 1191
                                                               'In this view, the check-in status represents the latest check-in record for the registration in that row.',
Please login to merge, or discard this patch.
core/data_migration_scripts/EE_Data_Migration_Script_Base.core.php 1 patch
Indentation   +876 added lines, -876 removed lines patch added patch discarded remove patch
@@ -15,880 +15,880 @@
 block discarded – undo
15 15
 abstract class EE_Data_Migration_Script_Base extends EE_Data_Migration_Class_Base
16 16
 {
17 17
 
18
-    /**
19
-     * Set by client code to indicate this DMS is being ran as part of a proper migration,
20
-     * instead of being used to merely setup (or verify) the database structure.
21
-     * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration
22
-     * should call EE_Data_Migration_Script_Base::set_migrating( FALSE )
23
-     *
24
-     * @var boolean
25
-     */
26
-    protected $_migrating = true;
27
-
28
-    /**
29
-     * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object
30
-     *
31
-     * @var EE_Data_Migration_Script_Stage[] $migration_functions
32
-     */
33
-    protected $_migration_stages = array();
34
-
35
-    /**
36
-     * Indicates we've already ran the schema changes that needed to happen BEFORE the data migration
37
-     *
38
-     * @var boolean
39
-     */
40
-    protected $_schema_changes_before_migration_ran = null;
41
-
42
-    /**
43
-     * Indicates we've already ran the schema changes that needed to happen AFTER the data migration
44
-     *
45
-     * @var boolean
46
-     */
47
-    protected $_schema_changes_after_migration_ran = null;
48
-
49
-    /**
50
-     * String which describes what's currently happening in this migration
51
-     *
52
-     * @var string
53
-     */
54
-    protected $_feedback_message;
55
-
56
-    /**
57
-     * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers
58
-     * correspond to earlier execution
59
-     *
60
-     * @var int
61
-     */
62
-    protected $_priority = 5;
63
-
64
-    /**
65
-     * Multi-dimensional array that defines the mapping from OLD table Primary Keys
66
-     * to NEW table Primary Keys.
67
-     * Top-level array keys are OLD table names (minus the "wp_" part),
68
-     * 2nd-level array keys are NEW table names (again, minus the "wp_" part),
69
-     * 3rd-level array keys are the OLD table primary keys
70
-     * and 3rd-level array values are the NEW table primary keys
71
-     *
72
-     * @var array
73
-     */
74
-    protected $_mappings = array();
75
-
76
-
77
-    /**
78
-     * Returns whether or not this data migration script can operate on the given version of the database.
79
-     * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and
80
-     * it's passed a string like '3.1.38B', it should return true.
81
-     * If this DMS is to migrate data from an EE3 addon, you will probably want to use
82
-     * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and
83
-     * EE_Data_Migration_Manager::get_migration_ran() to check that core was already
84
-     * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated
85
-     * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want
86
-     * to run this DMS).
87
-     * If this DMS migrates data from a previous version of this EE4 addon, just
88
-     * comparing $current_database_state_of[ $this->slug() ] will probably suffice.
89
-     * If this DMS should never migrate data, because it's only used to define the initial
90
-     * database state, just return FALSE (and core's activation process will take care
91
-     * of calling its schema_changes_before_migration() and
92
-     * schema_changes_after_migration() for you. )
93
-     *
94
-     * @param array $current_database_state_of keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc)
95
-     * @return boolean
96
-     */
97
-    abstract public function can_migrate_from_version($current_database_state_of);
98
-
99
-
100
-    /**
101
-     * Performs database schema changes that need to occur BEFORE the data is migrated.
102
-     * Eg, if we were going to change user passwords from plaintext to encoded versions
103
-     * during this migration, this would probably add a new column called something like
104
-     * "encoded_password".
105
-     *
106
-     * @return boolean of success
107
-     */
108
-    abstract public function schema_changes_before_migration();
109
-
110
-
111
-    /**
112
-     * Performs the database schema changes that need to occur AFTER the data has been migrated.
113
-     * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords
114
-     * from plaintext to encoded versions, and we had added a column called "encoded_password",
115
-     * this function would probably remove the old column "password" (which still holds the plaintext password)
116
-     * and possibly rename "encoded_password" to "password"
117
-     *
118
-     * @return boolean of success
119
-     */
120
-    abstract public function schema_changes_after_migration();
121
-
122
-
123
-    /**
124
-     * All children of this must call parent::__construct()
125
-     * at the end of their constructor or suffer the consequences!
126
-     *
127
-     * @param TableManager  $table_manager
128
-     * @param TableAnalysis $table_analysis
129
-     */
130
-    public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null)
131
-    {
132
-        $this->_migration_stages = (array) apply_filters(
133
-            'FHEE__' . get_class($this) . '__construct__migration_stages',
134
-            $this->_migration_stages
135
-        );
136
-        foreach ($this->_migration_stages as $migration_stage) {
137
-            if ($migration_stage instanceof EE_Data_Migration_Script_Stage) {
138
-                $migration_stage->_construct_finalize($this);
139
-            }
140
-        }
141
-        parent::__construct($table_manager, $table_analysis);
142
-    }
143
-
144
-
145
-    /**
146
-     * Place to add hooks and filters for tweaking the migrations page, in order
147
-     * to customize it
148
-     */
149
-    public function migration_page_hooks()
150
-    {
151
-        // by default none are added because we normally like the default look of the migration page
152
-    }
153
-
154
-
155
-    /**
156
-     * Sets the mapping from old table primary keys to new table primary keys.
157
-     * This mapping is automatically persisted as a property on the migration
158
-     *
159
-     * @param string     $old_table with wpdb prefix (wp_). Eg: wp_events_detail
160
-     * @param int|string $old_pk    old primary key. Eg events_detail.id's value
161
-     * @param string     $new_table with wpdb prefix (wp_). Eg: wp_posts
162
-     * @param int|string $new_pk    eg posts.ID
163
-     * @return void
164
-     */
165
-    public function set_mapping($old_table, $old_pk, $new_table, $new_pk)
166
-    {
167
-        // make sure it has the needed keys
168
-        if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) {
169
-            $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
170
-        }
171
-        $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk;
172
-    }
173
-
174
-
175
-    /**
176
-     * Gets the new primary key, if provided with the OLD table and the primary key
177
-     * of an item in the old table, and the new table
178
-     *
179
-     * @param string     $old_table with wpdb prefix (wp_). Eg: wp_events_detail
180
-     * @param int|string $old_pk    old primary key. Eg events_detail.id's value
181
-     * @param string     $new_table with wpdb prefix (wp_). Eg: wp_posts
182
-     * @return mixed the primary key on the new table
183
-     */
184
-    public function get_mapping_new_pk($old_table, $old_pk, $new_table)
185
-    {
186
-        if (! isset($this->_mappings[ $old_table ]) ||
187
-            ! isset($this->_mappings[ $old_table ][ $new_table ])) {
188
-            // try fetching the option
189
-            $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
190
-        }
191
-        return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ])
192
-            ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null;
193
-    }
194
-
195
-
196
-    /**
197
-     * Gets the old primary key, if provided with the OLD table,
198
-     * and the new table and the primary key of an item in the new table
199
-     *
200
-     * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail
201
-     * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts
202
-     * @param mixed  $new_pk
203
-     * @return mixed
204
-     */
205
-    public function get_mapping_old_pk($old_table, $new_table, $new_pk)
206
-    {
207
-        if (! isset($this->_mappings[ $old_table ]) ||
208
-            ! isset($this->_mappings[ $old_table ][ $new_table ])) {
209
-            // try fetching the option
210
-            $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
211
-        }
212
-        if (isset($this->_mappings[ $old_table ][ $new_table ])) {
213
-            $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]);
214
-            if (isset($new_pk_to_old_pk[ $new_pk ])) {
215
-                return $new_pk_to_old_pk[ $new_pk ];
216
-            }
217
-        }
218
-        return null;
219
-    }
220
-
221
-
222
-    /**
223
-     * Gets the mapping array option specified by the table names
224
-     *
225
-     * @param string $old_table_name
226
-     * @param string $new_table_name
227
-     * @return array
228
-     */
229
-    protected function _get_mapping_option($old_table_name, $new_table_name)
230
-    {
231
-        $option = get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array());
232
-        return $option;
233
-    }
234
-
235
-
236
-    /**
237
-     * Updates the mapping option specified by the table names with the array provided
238
-     *
239
-     * @param string $old_table_name
240
-     * @param string $new_table_name
241
-     * @param array  $mapping_array
242
-     * @return boolean success of updating option
243
-     */
244
-    protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array)
245
-    {
246
-        $success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false);
247
-        return $success;
248
-    }
249
-
250
-
251
-    /**
252
-     * Gets the option name for this script to map from $old_table_name to $new_table_name
253
-     *
254
-     * @param string $old_table_name
255
-     * @param string $new_table_name
256
-     * @return string
257
-     */
258
-    protected function _get_mapping_option_name($old_table_name, $new_table_name)
259
-    {
260
-        global $wpdb;
261
-        $old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name);
262
-        $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name);
263
-        $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this));
264
-        return substr(
265
-            EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp,
266
-            0,
267
-            64
268
-        );
269
-    }
270
-
271
-
272
-    /**
273
-     * Counts all the records that will be migrated during this data migration.
274
-     * For example, if we were changing old user passwords from plaintext to encoded versions,
275
-     * this would be a count of all users who have passwords. If we were going to also split
276
-     * attendee records into transactions, registrations, and attendee records, this would include
277
-     * the count of all attendees currently in existence in the DB (ie, users + attendees).
278
-     * If you can't determine how many records there are to migrate, just provide a guess: this
279
-     * number will only be used in calculating the percent complete. If you estimate there to be
280
-     * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at
281
-     * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete.
282
-     *
283
-     * @return int
284
-     */
285
-    protected function _count_records_to_migrate()
286
-    {
287
-        $count = 0;
288
-        foreach ($this->stages() as $stage) {
289
-            $count += $stage->count_records_to_migrate();
290
-        }
291
-        return $count;
292
-    }
293
-
294
-
295
-    /**
296
-     * Returns the number of records updated so far. Usually this is easiest to do
297
-     * by just setting a transient and updating it after each migration_step
298
-     *
299
-     * @return int
300
-     */
301
-    public function count_records_migrated()
302
-    {
303
-        $count = 0;
304
-        foreach ($this->stages() as $stage) {
305
-            $count += $stage->count_records_migrated();
306
-        }
307
-        $this->_records_migrated = $count;
308
-        return $count;
309
-    }
310
-
311
-
312
-    /**
313
-     * @param int $num_records_to_migrate_limit
314
-     * @return int
315
-     * @throws EE_Error
316
-     * @throws Exception
317
-     */
318
-    public function migration_step($num_records_to_migrate_limit)
319
-    {
320
-        // reset the feedback message
321
-        $this->_feedback_message = '';
322
-        // if we haven't yet done the 1st schema changes, do them now. buffer any output
323
-        $this->_maybe_do_schema_changes(true);
324
-
325
-        $num_records_actually_migrated = 0;
326
-        $records_migrated_per_stage = array();
327
-        // setup the 'stage' variable, which should hold the last run stage of the migration  (or none at all if nothing runs)
328
-        $stage = null;
329
-        // get the next stage that isn't complete
330
-        foreach ($this->stages() as $stage) {
331
-            if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) {
332
-                try {
333
-                    $records_migrated_during_stage = $stage->migration_step(
334
-                        $num_records_to_migrate_limit - $num_records_actually_migrated
335
-                    );
336
-                    $num_records_actually_migrated += $records_migrated_during_stage;
337
-                    $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage;
338
-                } catch (Exception $e) {
339
-                    // yes if we catch an exception here, we consider that migration stage borked.
340
-                    $stage->set_status(EE_Data_Migration_Manager::status_fatal_error);
341
-                    $this->set_status(EE_Data_Migration_Manager::status_fatal_error);
342
-                    $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString());
343
-                    throw $e;
344
-                }
345
-                // check that the migration stage didn't mark itself as having a fatal error
346
-                if ($stage->is_broken()) {
347
-                    $this->set_broken();
348
-                    throw new EE_Error($stage->get_last_error());
349
-                }
350
-            }
351
-            // once we've migrated all the number we intended to (possibly from different stages), stop migrating
352
-            // or if we had a fatal error
353
-            // or if the current script stopped early- its not done, but it's done all it thinks we should do on this step
354
-            if ($num_records_actually_migrated >= $num_records_to_migrate_limit
355
-                || $stage->is_broken()
356
-                || $stage->has_more_to_do()
357
-            ) {
358
-                break;
359
-            }
360
-        }
361
-        // check if we're all done this data migration...
362
-        // which is indicated by being done early AND the last stage claims to be done
363
-        if ($stage == null) {
364
-            // this migration script apparently has NO stages... which is super weird, but whatever
365
-            $this->set_completed();
366
-            $this->_maybe_do_schema_changes(false);
367
-        } elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) {
368
-            // apparently we're done, because we couldn't migrate the number we intended to
369
-            $this->set_completed();
370
-            $this->_update_feedback_message(array_reverse($records_migrated_per_stage));
371
-            // do schema changes for after the migration now
372
-            // first double-check we haven't already done this
373
-            $this->_maybe_do_schema_changes(false);
374
-        } else {
375
-            // update feedback message, keeping in mind that we show them with the most recent at the top
376
-            $this->_update_feedback_message(array_reverse($records_migrated_per_stage));
377
-        }
378
-        return $num_records_actually_migrated;
379
-    }
380
-
381
-
382
-    /**
383
-     * Updates the feedback message according to what was done during this migration stage.
384
-     *
385
-     * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records
386
-     *                                          migrated from that stage
387
-     * @return void
388
-     */
389
-    private function _update_feedback_message($records_migrated_per_stage)
390
-    {
391
-        $feedback_message_array = array();
392
-        foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) {
393
-            $feedback_message_array[] = sprintf(
394
-                __("Migrated %d records successfully during %s", "event_espresso"),
395
-                $num_records_migrated,
396
-                $migration_stage_name
397
-            );
398
-        }
399
-        $this->_feedback_message .= implode("<br>", $feedback_message_array);
400
-    }
401
-
402
-
403
-    /**
404
-     * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration
405
-     * (if $before==false). Buffers their outputs and stores them on the class.
406
-     *
407
-     * @param boolean $before
408
-     * @throws Exception
409
-     * @return void
410
-     */
411
-    private function _maybe_do_schema_changes($before = true)
412
-    {
413
-        // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran
414
-        $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran';
415
-        if (! $this->{$property_name}) {
416
-            try {
417
-                ob_start();
418
-                if ($before) {
419
-                    $this->schema_changes_before_migration();
420
-                } else {
421
-                    $this->schema_changes_after_migration();
422
-                }
423
-                $output = ob_get_contents();
424
-                ob_end_clean();
425
-            } catch (Exception $e) {
426
-                $this->set_status(EE_Data_Migration_Manager::status_fatal_error);
427
-                throw $e;
428
-            }
429
-            // record that we've done these schema changes
430
-            $this->{$property_name} = true;
431
-            // if there were any warnings etc, record them as non-fatal errors
432
-            if ($output) {
433
-                // there were some warnings
434
-                $this->_errors[] = $output;
435
-            }
436
-        }
437
-    }
438
-
439
-
440
-    /**
441
-     * Wrapper for EEH_Activation::create_table. However, takes into account the request type when
442
-     * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead
443
-     * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to
444
-     * or
445
-     * activated currently. If this is a brand new activation or a migration, and we're indicating this table should
446
-     * not
447
-     * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this
448
-     * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table
449
-     * should be nuked.
450
-     *
451
-     * Just for a bit of context, the migration script's db_schema_changes_* methods
452
-     * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the
453
-     * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was
454
-     * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should
455
-     * be dropped), and during a migration when we're moving the DB to the state of the migration script
456
-     *
457
-     * @param string $table_name
458
-     * @param string $table_definition_sql
459
-     * @param string $engine_string
460
-     */
461
-    protected function _table_is_new_in_this_version(
462
-        $table_name,
463
-        $table_definition_sql,
464
-        $engine_string = 'ENGINE=InnoDB '
465
-    ) {
466
-        $this->_create_table_and_catch_errors(
467
-            $table_name,
468
-            $table_definition_sql,
469
-            $engine_string,
470
-            $this->_pre_existing_table_should_be_dropped(true)
471
-        );
472
-    }
473
-
474
-    /**
475
-     * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table
476
-     * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not
477
-     * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is
478
-     * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as
479
-     * _table_should_exist_previously.
480
-     * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the
481
-     * table shouldn't exist).
482
-     *
483
-     * @param string $table_name
484
-     * @param string $table_definition_sql
485
-     * @param string $engine_string
486
-     */
487
-    protected function _table_is_changed_in_this_version(
488
-        $table_name,
489
-        $table_definition_sql,
490
-        $engine_string = 'ENGINE=MyISAM'
491
-    ) {
492
-        $this->_create_table_and_catch_errors(
493
-            $table_name,
494
-            $table_definition_sql,
495
-            $engine_string,
496
-            $this->_pre_existing_table_should_be_dropped(false)
497
-        );
498
-    }
499
-
500
-
501
-    /**
502
-     * _old_table_exists
503
-     * returns TRUE if the requested table exists in the current database
504
-     *
505
-     * @param string $table_name
506
-     * @return boolean
507
-     */
508
-    protected function _old_table_exists($table_name)
509
-    {
510
-        return $this->_get_table_analysis()->tableExists($table_name);
511
-    }
512
-
513
-
514
-    /**
515
-     * _delete_table_if_empty
516
-     * returns TRUE if the requested table was empty and successfully empty
517
-     *
518
-     * @param string $table_name
519
-     * @return boolean
520
-     */
521
-    protected function _delete_table_if_empty($table_name)
522
-    {
523
-        return EEH_Activation::delete_db_table_if_empty($table_name);
524
-    }
525
-
526
-
527
-    /**
528
-     * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version
529
-     * as these are significantly more efficient or explicit.
530
-     * Please see description of _table_is_new_in_this_version. This function will only set
531
-     * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand
532
-     * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin"
533
-     * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually
534
-     * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table
535
-     * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then
536
-     * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations
537
-     * because apparently it hasn't changed since the previous one, right?
538
-     *
539
-     * @param string $table_name
540
-     * @param string $table_definition_sql
541
-     * @param string $engine_string
542
-     */
543
-    protected function _table_should_exist_previously(
544
-        $table_name,
545
-        $table_definition_sql,
546
-        $engine_string = 'ENGINE=MyISAM'
547
-    ) {
548
-        $this->_create_table_and_catch_errors(
549
-            $table_name,
550
-            $table_definition_sql,
551
-            $engine_string,
552
-            $this->_pre_existing_table_should_be_dropped(false)
553
-        );
554
-    }
555
-
556
-    /**
557
-     * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing
558
-     * a migration, we skip checking this table's structure in the database and just assume it's correct.
559
-     * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs,
560
-     * but important for multisite where migrations can take a very long time otherwise).
561
-     * If the table is known to have changed since previous version, use _table_is_changed_in_this_version().
562
-     * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table
563
-     * shouldn't exist).
564
-     *
565
-     * @param string $table_name
566
-     * @param string $table_definition_sql
567
-     * @param string $engine_string
568
-     */
569
-    protected function _table_has_not_changed_since_previous(
570
-        $table_name,
571
-        $table_definition_sql,
572
-        $engine_string = 'ENGINE=MyISAM'
573
-    ) {
574
-        if ($this->_currently_migrating()) {
575
-            // if we're doing a migration, and this table apparently already exists, then we don't need do anything right?
576
-            return;
577
-        }
578
-        $this->_create_table_and_catch_errors(
579
-            $table_name,
580
-            $table_definition_sql,
581
-            $engine_string,
582
-            $this->_pre_existing_table_should_be_dropped(false)
583
-        );
584
-    }
585
-
586
-    /**
587
-     * Returns whether or not this migration script is being used as part of an actual migration
588
-     *
589
-     * @return boolean
590
-     */
591
-    protected function _currently_migrating()
592
-    {
593
-        // we want to know if we are currently performing a migration. We could just believe what was set on the _migrating property, but let's double-check (ie the script should apply and we should be in MM)
594
-        return $this->_migrating &&
595
-               $this->can_migrate_from_version(
596
-                   EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set()
597
-               ) &&
598
-               EE_Maintenance_Mode::instance()->real_level() == EE_Maintenance_Mode::level_2_complete_maintenance;
599
-    }
600
-
601
-    /**
602
-     * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new,
603
-     * and the plugin's request type.
604
-     * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table
605
-     * shouldn't exist no matter what).
606
-     *
607
-     * @param boolean $table_is_new
608
-     * @return boolean
609
-     */
610
-    protected function _pre_existing_table_should_be_dropped($table_is_new)
611
-    {
612
-        if ($table_is_new) {
613
-            if ($this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation
614
-                || $this->_currently_migrating()
615
-            ) {
616
-                return true;
617
-            } else {
618
-                return false;
619
-            }
620
-        } else {
621
-            if (in_array(
622
-                $this->_get_req_type_for_plugin_corresponding_to_this_dms(),
623
-                array(EE_System::req_type_new_activation)
624
-            )) {
625
-                return true;
626
-            } else {
627
-                return false;
628
-            }
629
-        }
630
-    }
631
-
632
-    /**
633
-     * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS
634
-     *
635
-     * @param string  $table_name
636
-     * @param string  $table_definition_sql
637
-     * @param string  $engine_string
638
-     * @param boolean $drop_pre_existing_tables
639
-     */
640
-    private function _create_table_and_catch_errors(
641
-        $table_name,
642
-        $table_definition_sql,
643
-        $engine_string = 'ENGINE=MyISAM',
644
-        $drop_pre_existing_tables = false
645
-    ) {
646
-        try {
647
-            EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables);
648
-        } catch (EE_Error $e) {
649
-            $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString();
650
-            $this->add_error($message);
651
-            $this->_feedback_message .= $message;
652
-        }
653
-    }
654
-
655
-
656
-    /**
657
-     * Gets the request type for the plugin (core or addon) that corresponds to this DMS
658
-     *
659
-     * @return int one of EE_System::_req_type_* constants
660
-     * @throws EE_Error
661
-     */
662
-    private function _get_req_type_for_plugin_corresponding_to_this_dms()
663
-    {
664
-        if ($this->slug() == 'Core') {
665
-            return EE_System::instance()->detect_req_type();
666
-        } else {// it must be for an addon
667
-            $addon_name = $this->slug();
668
-            if (EE_Registry::instance()->get_addon_by_name($addon_name)) {
669
-                return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type();
670
-            } else {
671
-                throw new EE_Error(
672
-                    sprintf(
673
-                        __(
674
-                            "The DMS slug '%s' should correspond to the addon's name, which should also be '%s', but no such addon was registered. These are the registered addons' names: %s",
675
-                            "event_espresso"
676
-                        ),
677
-                        $this->slug(),
678
-                        $addon_name,
679
-                        implode(",", array_keys(EE_Registry::instance()->get_addons_by_name()))
680
-                    )
681
-                );
682
-            }
683
-        }
684
-    }
685
-
686
-
687
-    /**
688
-     * returns an array of strings describing errors by all the script's stages
689
-     *
690
-     * @return array
691
-     */
692
-    public function get_errors()
693
-    {
694
-        $all_errors = $this->_errors;
695
-        if (! is_array($all_errors)) {
696
-            $all_errors = array();
697
-        }
698
-        foreach ($this->stages() as $stage) {
699
-            $all_errors = array_merge($stage->get_errors(), $all_errors);
700
-        }
701
-        return $all_errors;
702
-    }
703
-
704
-
705
-    /**
706
-     * Indicates whether or not this migration script should continue
707
-     *
708
-     * @return boolean
709
-     */
710
-    public function can_continue()
711
-    {
712
-        return in_array(
713
-            $this->get_status(),
714
-            EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script
715
-        );
716
-    }
717
-
718
-
719
-    /**
720
-     * Gets all the data migration stages associated with this script. Note:
721
-     * addons can filter this list to add their own stages, and because the list is
722
-     * numerically-indexed, they can insert their stage wherever they like and it will
723
-     * get ordered by the indexes
724
-     *
725
-     * @return EE_Data_Migration_Script_Stage[]
726
-     */
727
-    protected function stages()
728
-    {
729
-        $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages);
730
-        ksort($stages);
731
-        return $stages;
732
-    }
733
-
734
-
735
-    /**
736
-     * Gets a string which should describe what's going on currently with this migration, which
737
-     * can be displayed to the user
738
-     *
739
-     * @return string
740
-     */
741
-    public function get_feedback_message()
742
-    {
743
-        return $this->_feedback_message;
744
-    }
745
-
746
-
747
-    /**
748
-     * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class'
749
-     * properties to the DB. However, we don't want to use __sleep() because its quite
750
-     * possible that this class is defined when it goes to sleep, but NOT available when it
751
-     * awakes (eg, this class is part of an addon that is deactivated at some point).
752
-     */
753
-    public function properties_as_array()
754
-    {
755
-        $properties = parent::properties_as_array();
756
-        $properties['_migration_stages'] = array();
757
-        foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) {
758
-            $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array(
759
-            );
760
-        }
761
-        unset($properties['_mappings']);
762
-
763
-        foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) {
764
-            foreach ($mapping_to_new_table as $new_table_name => $mapping) {
765
-                $this->_set_mapping_option($old_table_name, $new_table_name, $mapping);
766
-            }
767
-        }
768
-        return $properties;
769
-    }
770
-
771
-
772
-    /**
773
-     * Sets all of the properties of this script stage to match what's in the array, which is assumed
774
-     * to have been made from the properties_as_array() function.
775
-     *
776
-     * @param array $array_of_properties like what's produced from properties_as_array() method
777
-     * @return void
778
-     */
779
-    public function instantiate_from_array_of_properties($array_of_properties)
780
-    {
781
-        $stages_properties_arrays = $array_of_properties['_migration_stages'];
782
-        unset($array_of_properties['_migration_stages']);
783
-        unset($array_of_properties['class']);
784
-        foreach ($array_of_properties as $property_name => $property_value) {
785
-            $this->{$property_name} = $property_value;
786
-        }
787
-        // _migration_stages are already instantiated, but have only default data
788
-        foreach ($this->_migration_stages as $stage) {
789
-            $stage_data = $this->_find_migration_stage_data_with_classname(
790
-                get_class($stage),
791
-                $stages_properties_arrays
792
-            );
793
-            // SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by
794
-            // an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet)
795
-            if ($stage_data) {
796
-                $stage->instantiate_from_array_of_properties($stage_data);
797
-            }
798
-        }
799
-    }
800
-
801
-
802
-    /**
803
-     * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which
804
-     * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes)
805
-     * for the given classname
806
-     *
807
-     * @param string $classname
808
-     * @param array  $migration_stage_data_arrays
809
-     * @return null
810
-     */
811
-    private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays)
812
-    {
813
-        foreach ($migration_stage_data_arrays as $migration_stage_data_array) {
814
-            if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) {
815
-                return $migration_stage_data_array;
816
-            }
817
-        }
818
-        return null;
819
-    }
820
-
821
-
822
-    /**
823
-     * Returns the version that this script migrates to, based on the script's name.
824
-     * Cannot be overwritten because lots of code needs to know which version a script
825
-     * migrates to knowing only its name.
826
-     *
827
-     * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin
828
-     * that will be updated to. Eg array('Core','4.1.0')
829
-     */
830
-    final public function migrates_to_version()
831
-    {
832
-        return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this));
833
-    }
834
-
835
-
836
-    /**
837
-     * Gets this addon's slug as it would appear in the current_db_state wp option,
838
-     * and if this migration script is for an addon, it SHOULD match the addon's slug
839
-     * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon.
840
-     * Or 'Core' for core (non-addon).
841
-     *
842
-     * @return string
843
-     */
844
-    public function slug()
845
-    {
846
-        $migrates_to_version_info = $this->migrates_to_version();
847
-        // the slug is the first part of the array
848
-        return $migrates_to_version_info['slug'];
849
-    }
850
-
851
-
852
-    /**
853
-     * Returns the script's priority relative to DMSs from other addons. However, when
854
-     * two DMSs from the same addon/core apply, this is ignored (and instead the version that
855
-     * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs
856
-     * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons,
857
-     * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set
858
-     * the database up so it can run), then you can set "A" to priority 3 or something.
859
-     *
860
-     * @return int
861
-     */
862
-    public function priority()
863
-    {
864
-        return $this->_priority;
865
-    }
866
-
867
-
868
-    /**
869
-     * Sets whether or not this DMS is being ran as part of a migration, instead of
870
-     * just being used to setup (or verify) the current database structure matches
871
-     * what the latest DMS indicates it should be
872
-     *
873
-     * @param boolean $migrating
874
-     * @return void
875
-     */
876
-    public function set_migrating($migrating = true)
877
-    {
878
-        $this->_migrating = $migrating;
879
-    }
880
-
881
-    /**
882
-     * Marks that we think this migration class can continue to migrate
883
-     */
884
-    public function reattempt()
885
-    {
886
-        parent::reattempt();
887
-        // also, we want to reattempt any stages that were marked as borked
888
-        foreach ($this->stages() as $stage) {
889
-            if ($stage->is_broken()) {
890
-                $stage->reattempt();
891
-            }
892
-        }
893
-    }
18
+	/**
19
+	 * Set by client code to indicate this DMS is being ran as part of a proper migration,
20
+	 * instead of being used to merely setup (or verify) the database structure.
21
+	 * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration
22
+	 * should call EE_Data_Migration_Script_Base::set_migrating( FALSE )
23
+	 *
24
+	 * @var boolean
25
+	 */
26
+	protected $_migrating = true;
27
+
28
+	/**
29
+	 * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object
30
+	 *
31
+	 * @var EE_Data_Migration_Script_Stage[] $migration_functions
32
+	 */
33
+	protected $_migration_stages = array();
34
+
35
+	/**
36
+	 * Indicates we've already ran the schema changes that needed to happen BEFORE the data migration
37
+	 *
38
+	 * @var boolean
39
+	 */
40
+	protected $_schema_changes_before_migration_ran = null;
41
+
42
+	/**
43
+	 * Indicates we've already ran the schema changes that needed to happen AFTER the data migration
44
+	 *
45
+	 * @var boolean
46
+	 */
47
+	protected $_schema_changes_after_migration_ran = null;
48
+
49
+	/**
50
+	 * String which describes what's currently happening in this migration
51
+	 *
52
+	 * @var string
53
+	 */
54
+	protected $_feedback_message;
55
+
56
+	/**
57
+	 * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers
58
+	 * correspond to earlier execution
59
+	 *
60
+	 * @var int
61
+	 */
62
+	protected $_priority = 5;
63
+
64
+	/**
65
+	 * Multi-dimensional array that defines the mapping from OLD table Primary Keys
66
+	 * to NEW table Primary Keys.
67
+	 * Top-level array keys are OLD table names (minus the "wp_" part),
68
+	 * 2nd-level array keys are NEW table names (again, minus the "wp_" part),
69
+	 * 3rd-level array keys are the OLD table primary keys
70
+	 * and 3rd-level array values are the NEW table primary keys
71
+	 *
72
+	 * @var array
73
+	 */
74
+	protected $_mappings = array();
75
+
76
+
77
+	/**
78
+	 * Returns whether or not this data migration script can operate on the given version of the database.
79
+	 * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and
80
+	 * it's passed a string like '3.1.38B', it should return true.
81
+	 * If this DMS is to migrate data from an EE3 addon, you will probably want to use
82
+	 * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and
83
+	 * EE_Data_Migration_Manager::get_migration_ran() to check that core was already
84
+	 * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated
85
+	 * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want
86
+	 * to run this DMS).
87
+	 * If this DMS migrates data from a previous version of this EE4 addon, just
88
+	 * comparing $current_database_state_of[ $this->slug() ] will probably suffice.
89
+	 * If this DMS should never migrate data, because it's only used to define the initial
90
+	 * database state, just return FALSE (and core's activation process will take care
91
+	 * of calling its schema_changes_before_migration() and
92
+	 * schema_changes_after_migration() for you. )
93
+	 *
94
+	 * @param array $current_database_state_of keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc)
95
+	 * @return boolean
96
+	 */
97
+	abstract public function can_migrate_from_version($current_database_state_of);
98
+
99
+
100
+	/**
101
+	 * Performs database schema changes that need to occur BEFORE the data is migrated.
102
+	 * Eg, if we were going to change user passwords from plaintext to encoded versions
103
+	 * during this migration, this would probably add a new column called something like
104
+	 * "encoded_password".
105
+	 *
106
+	 * @return boolean of success
107
+	 */
108
+	abstract public function schema_changes_before_migration();
109
+
110
+
111
+	/**
112
+	 * Performs the database schema changes that need to occur AFTER the data has been migrated.
113
+	 * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords
114
+	 * from plaintext to encoded versions, and we had added a column called "encoded_password",
115
+	 * this function would probably remove the old column "password" (which still holds the plaintext password)
116
+	 * and possibly rename "encoded_password" to "password"
117
+	 *
118
+	 * @return boolean of success
119
+	 */
120
+	abstract public function schema_changes_after_migration();
121
+
122
+
123
+	/**
124
+	 * All children of this must call parent::__construct()
125
+	 * at the end of their constructor or suffer the consequences!
126
+	 *
127
+	 * @param TableManager  $table_manager
128
+	 * @param TableAnalysis $table_analysis
129
+	 */
130
+	public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null)
131
+	{
132
+		$this->_migration_stages = (array) apply_filters(
133
+			'FHEE__' . get_class($this) . '__construct__migration_stages',
134
+			$this->_migration_stages
135
+		);
136
+		foreach ($this->_migration_stages as $migration_stage) {
137
+			if ($migration_stage instanceof EE_Data_Migration_Script_Stage) {
138
+				$migration_stage->_construct_finalize($this);
139
+			}
140
+		}
141
+		parent::__construct($table_manager, $table_analysis);
142
+	}
143
+
144
+
145
+	/**
146
+	 * Place to add hooks and filters for tweaking the migrations page, in order
147
+	 * to customize it
148
+	 */
149
+	public function migration_page_hooks()
150
+	{
151
+		// by default none are added because we normally like the default look of the migration page
152
+	}
153
+
154
+
155
+	/**
156
+	 * Sets the mapping from old table primary keys to new table primary keys.
157
+	 * This mapping is automatically persisted as a property on the migration
158
+	 *
159
+	 * @param string     $old_table with wpdb prefix (wp_). Eg: wp_events_detail
160
+	 * @param int|string $old_pk    old primary key. Eg events_detail.id's value
161
+	 * @param string     $new_table with wpdb prefix (wp_). Eg: wp_posts
162
+	 * @param int|string $new_pk    eg posts.ID
163
+	 * @return void
164
+	 */
165
+	public function set_mapping($old_table, $old_pk, $new_table, $new_pk)
166
+	{
167
+		// make sure it has the needed keys
168
+		if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) {
169
+			$this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
170
+		}
171
+		$this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk;
172
+	}
173
+
174
+
175
+	/**
176
+	 * Gets the new primary key, if provided with the OLD table and the primary key
177
+	 * of an item in the old table, and the new table
178
+	 *
179
+	 * @param string     $old_table with wpdb prefix (wp_). Eg: wp_events_detail
180
+	 * @param int|string $old_pk    old primary key. Eg events_detail.id's value
181
+	 * @param string     $new_table with wpdb prefix (wp_). Eg: wp_posts
182
+	 * @return mixed the primary key on the new table
183
+	 */
184
+	public function get_mapping_new_pk($old_table, $old_pk, $new_table)
185
+	{
186
+		if (! isset($this->_mappings[ $old_table ]) ||
187
+			! isset($this->_mappings[ $old_table ][ $new_table ])) {
188
+			// try fetching the option
189
+			$this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
190
+		}
191
+		return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ])
192
+			? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null;
193
+	}
194
+
195
+
196
+	/**
197
+	 * Gets the old primary key, if provided with the OLD table,
198
+	 * and the new table and the primary key of an item in the new table
199
+	 *
200
+	 * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail
201
+	 * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts
202
+	 * @param mixed  $new_pk
203
+	 * @return mixed
204
+	 */
205
+	public function get_mapping_old_pk($old_table, $new_table, $new_pk)
206
+	{
207
+		if (! isset($this->_mappings[ $old_table ]) ||
208
+			! isset($this->_mappings[ $old_table ][ $new_table ])) {
209
+			// try fetching the option
210
+			$this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
211
+		}
212
+		if (isset($this->_mappings[ $old_table ][ $new_table ])) {
213
+			$new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]);
214
+			if (isset($new_pk_to_old_pk[ $new_pk ])) {
215
+				return $new_pk_to_old_pk[ $new_pk ];
216
+			}
217
+		}
218
+		return null;
219
+	}
220
+
221
+
222
+	/**
223
+	 * Gets the mapping array option specified by the table names
224
+	 *
225
+	 * @param string $old_table_name
226
+	 * @param string $new_table_name
227
+	 * @return array
228
+	 */
229
+	protected function _get_mapping_option($old_table_name, $new_table_name)
230
+	{
231
+		$option = get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array());
232
+		return $option;
233
+	}
234
+
235
+
236
+	/**
237
+	 * Updates the mapping option specified by the table names with the array provided
238
+	 *
239
+	 * @param string $old_table_name
240
+	 * @param string $new_table_name
241
+	 * @param array  $mapping_array
242
+	 * @return boolean success of updating option
243
+	 */
244
+	protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array)
245
+	{
246
+		$success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false);
247
+		return $success;
248
+	}
249
+
250
+
251
+	/**
252
+	 * Gets the option name for this script to map from $old_table_name to $new_table_name
253
+	 *
254
+	 * @param string $old_table_name
255
+	 * @param string $new_table_name
256
+	 * @return string
257
+	 */
258
+	protected function _get_mapping_option_name($old_table_name, $new_table_name)
259
+	{
260
+		global $wpdb;
261
+		$old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name);
262
+		$new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name);
263
+		$migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this));
264
+		return substr(
265
+			EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp,
266
+			0,
267
+			64
268
+		);
269
+	}
270
+
271
+
272
+	/**
273
+	 * Counts all the records that will be migrated during this data migration.
274
+	 * For example, if we were changing old user passwords from plaintext to encoded versions,
275
+	 * this would be a count of all users who have passwords. If we were going to also split
276
+	 * attendee records into transactions, registrations, and attendee records, this would include
277
+	 * the count of all attendees currently in existence in the DB (ie, users + attendees).
278
+	 * If you can't determine how many records there are to migrate, just provide a guess: this
279
+	 * number will only be used in calculating the percent complete. If you estimate there to be
280
+	 * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at
281
+	 * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete.
282
+	 *
283
+	 * @return int
284
+	 */
285
+	protected function _count_records_to_migrate()
286
+	{
287
+		$count = 0;
288
+		foreach ($this->stages() as $stage) {
289
+			$count += $stage->count_records_to_migrate();
290
+		}
291
+		return $count;
292
+	}
293
+
294
+
295
+	/**
296
+	 * Returns the number of records updated so far. Usually this is easiest to do
297
+	 * by just setting a transient and updating it after each migration_step
298
+	 *
299
+	 * @return int
300
+	 */
301
+	public function count_records_migrated()
302
+	{
303
+		$count = 0;
304
+		foreach ($this->stages() as $stage) {
305
+			$count += $stage->count_records_migrated();
306
+		}
307
+		$this->_records_migrated = $count;
308
+		return $count;
309
+	}
310
+
311
+
312
+	/**
313
+	 * @param int $num_records_to_migrate_limit
314
+	 * @return int
315
+	 * @throws EE_Error
316
+	 * @throws Exception
317
+	 */
318
+	public function migration_step($num_records_to_migrate_limit)
319
+	{
320
+		// reset the feedback message
321
+		$this->_feedback_message = '';
322
+		// if we haven't yet done the 1st schema changes, do them now. buffer any output
323
+		$this->_maybe_do_schema_changes(true);
324
+
325
+		$num_records_actually_migrated = 0;
326
+		$records_migrated_per_stage = array();
327
+		// setup the 'stage' variable, which should hold the last run stage of the migration  (or none at all if nothing runs)
328
+		$stage = null;
329
+		// get the next stage that isn't complete
330
+		foreach ($this->stages() as $stage) {
331
+			if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) {
332
+				try {
333
+					$records_migrated_during_stage = $stage->migration_step(
334
+						$num_records_to_migrate_limit - $num_records_actually_migrated
335
+					);
336
+					$num_records_actually_migrated += $records_migrated_during_stage;
337
+					$records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage;
338
+				} catch (Exception $e) {
339
+					// yes if we catch an exception here, we consider that migration stage borked.
340
+					$stage->set_status(EE_Data_Migration_Manager::status_fatal_error);
341
+					$this->set_status(EE_Data_Migration_Manager::status_fatal_error);
342
+					$stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString());
343
+					throw $e;
344
+				}
345
+				// check that the migration stage didn't mark itself as having a fatal error
346
+				if ($stage->is_broken()) {
347
+					$this->set_broken();
348
+					throw new EE_Error($stage->get_last_error());
349
+				}
350
+			}
351
+			// once we've migrated all the number we intended to (possibly from different stages), stop migrating
352
+			// or if we had a fatal error
353
+			// or if the current script stopped early- its not done, but it's done all it thinks we should do on this step
354
+			if ($num_records_actually_migrated >= $num_records_to_migrate_limit
355
+				|| $stage->is_broken()
356
+				|| $stage->has_more_to_do()
357
+			) {
358
+				break;
359
+			}
360
+		}
361
+		// check if we're all done this data migration...
362
+		// which is indicated by being done early AND the last stage claims to be done
363
+		if ($stage == null) {
364
+			// this migration script apparently has NO stages... which is super weird, but whatever
365
+			$this->set_completed();
366
+			$this->_maybe_do_schema_changes(false);
367
+		} elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) {
368
+			// apparently we're done, because we couldn't migrate the number we intended to
369
+			$this->set_completed();
370
+			$this->_update_feedback_message(array_reverse($records_migrated_per_stage));
371
+			// do schema changes for after the migration now
372
+			// first double-check we haven't already done this
373
+			$this->_maybe_do_schema_changes(false);
374
+		} else {
375
+			// update feedback message, keeping in mind that we show them with the most recent at the top
376
+			$this->_update_feedback_message(array_reverse($records_migrated_per_stage));
377
+		}
378
+		return $num_records_actually_migrated;
379
+	}
380
+
381
+
382
+	/**
383
+	 * Updates the feedback message according to what was done during this migration stage.
384
+	 *
385
+	 * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records
386
+	 *                                          migrated from that stage
387
+	 * @return void
388
+	 */
389
+	private function _update_feedback_message($records_migrated_per_stage)
390
+	{
391
+		$feedback_message_array = array();
392
+		foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) {
393
+			$feedback_message_array[] = sprintf(
394
+				__("Migrated %d records successfully during %s", "event_espresso"),
395
+				$num_records_migrated,
396
+				$migration_stage_name
397
+			);
398
+		}
399
+		$this->_feedback_message .= implode("<br>", $feedback_message_array);
400
+	}
401
+
402
+
403
+	/**
404
+	 * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration
405
+	 * (if $before==false). Buffers their outputs and stores them on the class.
406
+	 *
407
+	 * @param boolean $before
408
+	 * @throws Exception
409
+	 * @return void
410
+	 */
411
+	private function _maybe_do_schema_changes($before = true)
412
+	{
413
+		// so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran
414
+		$property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran';
415
+		if (! $this->{$property_name}) {
416
+			try {
417
+				ob_start();
418
+				if ($before) {
419
+					$this->schema_changes_before_migration();
420
+				} else {
421
+					$this->schema_changes_after_migration();
422
+				}
423
+				$output = ob_get_contents();
424
+				ob_end_clean();
425
+			} catch (Exception $e) {
426
+				$this->set_status(EE_Data_Migration_Manager::status_fatal_error);
427
+				throw $e;
428
+			}
429
+			// record that we've done these schema changes
430
+			$this->{$property_name} = true;
431
+			// if there were any warnings etc, record them as non-fatal errors
432
+			if ($output) {
433
+				// there were some warnings
434
+				$this->_errors[] = $output;
435
+			}
436
+		}
437
+	}
438
+
439
+
440
+	/**
441
+	 * Wrapper for EEH_Activation::create_table. However, takes into account the request type when
442
+	 * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead
443
+	 * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to
444
+	 * or
445
+	 * activated currently. If this is a brand new activation or a migration, and we're indicating this table should
446
+	 * not
447
+	 * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this
448
+	 * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table
449
+	 * should be nuked.
450
+	 *
451
+	 * Just for a bit of context, the migration script's db_schema_changes_* methods
452
+	 * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the
453
+	 * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was
454
+	 * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should
455
+	 * be dropped), and during a migration when we're moving the DB to the state of the migration script
456
+	 *
457
+	 * @param string $table_name
458
+	 * @param string $table_definition_sql
459
+	 * @param string $engine_string
460
+	 */
461
+	protected function _table_is_new_in_this_version(
462
+		$table_name,
463
+		$table_definition_sql,
464
+		$engine_string = 'ENGINE=InnoDB '
465
+	) {
466
+		$this->_create_table_and_catch_errors(
467
+			$table_name,
468
+			$table_definition_sql,
469
+			$engine_string,
470
+			$this->_pre_existing_table_should_be_dropped(true)
471
+		);
472
+	}
473
+
474
+	/**
475
+	 * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table
476
+	 * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not
477
+	 * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is
478
+	 * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as
479
+	 * _table_should_exist_previously.
480
+	 * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the
481
+	 * table shouldn't exist).
482
+	 *
483
+	 * @param string $table_name
484
+	 * @param string $table_definition_sql
485
+	 * @param string $engine_string
486
+	 */
487
+	protected function _table_is_changed_in_this_version(
488
+		$table_name,
489
+		$table_definition_sql,
490
+		$engine_string = 'ENGINE=MyISAM'
491
+	) {
492
+		$this->_create_table_and_catch_errors(
493
+			$table_name,
494
+			$table_definition_sql,
495
+			$engine_string,
496
+			$this->_pre_existing_table_should_be_dropped(false)
497
+		);
498
+	}
499
+
500
+
501
+	/**
502
+	 * _old_table_exists
503
+	 * returns TRUE if the requested table exists in the current database
504
+	 *
505
+	 * @param string $table_name
506
+	 * @return boolean
507
+	 */
508
+	protected function _old_table_exists($table_name)
509
+	{
510
+		return $this->_get_table_analysis()->tableExists($table_name);
511
+	}
512
+
513
+
514
+	/**
515
+	 * _delete_table_if_empty
516
+	 * returns TRUE if the requested table was empty and successfully empty
517
+	 *
518
+	 * @param string $table_name
519
+	 * @return boolean
520
+	 */
521
+	protected function _delete_table_if_empty($table_name)
522
+	{
523
+		return EEH_Activation::delete_db_table_if_empty($table_name);
524
+	}
525
+
526
+
527
+	/**
528
+	 * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version
529
+	 * as these are significantly more efficient or explicit.
530
+	 * Please see description of _table_is_new_in_this_version. This function will only set
531
+	 * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand
532
+	 * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin"
533
+	 * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually
534
+	 * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table
535
+	 * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then
536
+	 * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations
537
+	 * because apparently it hasn't changed since the previous one, right?
538
+	 *
539
+	 * @param string $table_name
540
+	 * @param string $table_definition_sql
541
+	 * @param string $engine_string
542
+	 */
543
+	protected function _table_should_exist_previously(
544
+		$table_name,
545
+		$table_definition_sql,
546
+		$engine_string = 'ENGINE=MyISAM'
547
+	) {
548
+		$this->_create_table_and_catch_errors(
549
+			$table_name,
550
+			$table_definition_sql,
551
+			$engine_string,
552
+			$this->_pre_existing_table_should_be_dropped(false)
553
+		);
554
+	}
555
+
556
+	/**
557
+	 * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing
558
+	 * a migration, we skip checking this table's structure in the database and just assume it's correct.
559
+	 * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs,
560
+	 * but important for multisite where migrations can take a very long time otherwise).
561
+	 * If the table is known to have changed since previous version, use _table_is_changed_in_this_version().
562
+	 * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table
563
+	 * shouldn't exist).
564
+	 *
565
+	 * @param string $table_name
566
+	 * @param string $table_definition_sql
567
+	 * @param string $engine_string
568
+	 */
569
+	protected function _table_has_not_changed_since_previous(
570
+		$table_name,
571
+		$table_definition_sql,
572
+		$engine_string = 'ENGINE=MyISAM'
573
+	) {
574
+		if ($this->_currently_migrating()) {
575
+			// if we're doing a migration, and this table apparently already exists, then we don't need do anything right?
576
+			return;
577
+		}
578
+		$this->_create_table_and_catch_errors(
579
+			$table_name,
580
+			$table_definition_sql,
581
+			$engine_string,
582
+			$this->_pre_existing_table_should_be_dropped(false)
583
+		);
584
+	}
585
+
586
+	/**
587
+	 * Returns whether or not this migration script is being used as part of an actual migration
588
+	 *
589
+	 * @return boolean
590
+	 */
591
+	protected function _currently_migrating()
592
+	{
593
+		// we want to know if we are currently performing a migration. We could just believe what was set on the _migrating property, but let's double-check (ie the script should apply and we should be in MM)
594
+		return $this->_migrating &&
595
+			   $this->can_migrate_from_version(
596
+				   EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set()
597
+			   ) &&
598
+			   EE_Maintenance_Mode::instance()->real_level() == EE_Maintenance_Mode::level_2_complete_maintenance;
599
+	}
600
+
601
+	/**
602
+	 * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new,
603
+	 * and the plugin's request type.
604
+	 * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table
605
+	 * shouldn't exist no matter what).
606
+	 *
607
+	 * @param boolean $table_is_new
608
+	 * @return boolean
609
+	 */
610
+	protected function _pre_existing_table_should_be_dropped($table_is_new)
611
+	{
612
+		if ($table_is_new) {
613
+			if ($this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation
614
+				|| $this->_currently_migrating()
615
+			) {
616
+				return true;
617
+			} else {
618
+				return false;
619
+			}
620
+		} else {
621
+			if (in_array(
622
+				$this->_get_req_type_for_plugin_corresponding_to_this_dms(),
623
+				array(EE_System::req_type_new_activation)
624
+			)) {
625
+				return true;
626
+			} else {
627
+				return false;
628
+			}
629
+		}
630
+	}
631
+
632
+	/**
633
+	 * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS
634
+	 *
635
+	 * @param string  $table_name
636
+	 * @param string  $table_definition_sql
637
+	 * @param string  $engine_string
638
+	 * @param boolean $drop_pre_existing_tables
639
+	 */
640
+	private function _create_table_and_catch_errors(
641
+		$table_name,
642
+		$table_definition_sql,
643
+		$engine_string = 'ENGINE=MyISAM',
644
+		$drop_pre_existing_tables = false
645
+	) {
646
+		try {
647
+			EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables);
648
+		} catch (EE_Error $e) {
649
+			$message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString();
650
+			$this->add_error($message);
651
+			$this->_feedback_message .= $message;
652
+		}
653
+	}
654
+
655
+
656
+	/**
657
+	 * Gets the request type for the plugin (core or addon) that corresponds to this DMS
658
+	 *
659
+	 * @return int one of EE_System::_req_type_* constants
660
+	 * @throws EE_Error
661
+	 */
662
+	private function _get_req_type_for_plugin_corresponding_to_this_dms()
663
+	{
664
+		if ($this->slug() == 'Core') {
665
+			return EE_System::instance()->detect_req_type();
666
+		} else {// it must be for an addon
667
+			$addon_name = $this->slug();
668
+			if (EE_Registry::instance()->get_addon_by_name($addon_name)) {
669
+				return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type();
670
+			} else {
671
+				throw new EE_Error(
672
+					sprintf(
673
+						__(
674
+							"The DMS slug '%s' should correspond to the addon's name, which should also be '%s', but no such addon was registered. These are the registered addons' names: %s",
675
+							"event_espresso"
676
+						),
677
+						$this->slug(),
678
+						$addon_name,
679
+						implode(",", array_keys(EE_Registry::instance()->get_addons_by_name()))
680
+					)
681
+				);
682
+			}
683
+		}
684
+	}
685
+
686
+
687
+	/**
688
+	 * returns an array of strings describing errors by all the script's stages
689
+	 *
690
+	 * @return array
691
+	 */
692
+	public function get_errors()
693
+	{
694
+		$all_errors = $this->_errors;
695
+		if (! is_array($all_errors)) {
696
+			$all_errors = array();
697
+		}
698
+		foreach ($this->stages() as $stage) {
699
+			$all_errors = array_merge($stage->get_errors(), $all_errors);
700
+		}
701
+		return $all_errors;
702
+	}
703
+
704
+
705
+	/**
706
+	 * Indicates whether or not this migration script should continue
707
+	 *
708
+	 * @return boolean
709
+	 */
710
+	public function can_continue()
711
+	{
712
+		return in_array(
713
+			$this->get_status(),
714
+			EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script
715
+		);
716
+	}
717
+
718
+
719
+	/**
720
+	 * Gets all the data migration stages associated with this script. Note:
721
+	 * addons can filter this list to add their own stages, and because the list is
722
+	 * numerically-indexed, they can insert their stage wherever they like and it will
723
+	 * get ordered by the indexes
724
+	 *
725
+	 * @return EE_Data_Migration_Script_Stage[]
726
+	 */
727
+	protected function stages()
728
+	{
729
+		$stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages);
730
+		ksort($stages);
731
+		return $stages;
732
+	}
733
+
734
+
735
+	/**
736
+	 * Gets a string which should describe what's going on currently with this migration, which
737
+	 * can be displayed to the user
738
+	 *
739
+	 * @return string
740
+	 */
741
+	public function get_feedback_message()
742
+	{
743
+		return $this->_feedback_message;
744
+	}
745
+
746
+
747
+	/**
748
+	 * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class'
749
+	 * properties to the DB. However, we don't want to use __sleep() because its quite
750
+	 * possible that this class is defined when it goes to sleep, but NOT available when it
751
+	 * awakes (eg, this class is part of an addon that is deactivated at some point).
752
+	 */
753
+	public function properties_as_array()
754
+	{
755
+		$properties = parent::properties_as_array();
756
+		$properties['_migration_stages'] = array();
757
+		foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) {
758
+			$properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array(
759
+			);
760
+		}
761
+		unset($properties['_mappings']);
762
+
763
+		foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) {
764
+			foreach ($mapping_to_new_table as $new_table_name => $mapping) {
765
+				$this->_set_mapping_option($old_table_name, $new_table_name, $mapping);
766
+			}
767
+		}
768
+		return $properties;
769
+	}
770
+
771
+
772
+	/**
773
+	 * Sets all of the properties of this script stage to match what's in the array, which is assumed
774
+	 * to have been made from the properties_as_array() function.
775
+	 *
776
+	 * @param array $array_of_properties like what's produced from properties_as_array() method
777
+	 * @return void
778
+	 */
779
+	public function instantiate_from_array_of_properties($array_of_properties)
780
+	{
781
+		$stages_properties_arrays = $array_of_properties['_migration_stages'];
782
+		unset($array_of_properties['_migration_stages']);
783
+		unset($array_of_properties['class']);
784
+		foreach ($array_of_properties as $property_name => $property_value) {
785
+			$this->{$property_name} = $property_value;
786
+		}
787
+		// _migration_stages are already instantiated, but have only default data
788
+		foreach ($this->_migration_stages as $stage) {
789
+			$stage_data = $this->_find_migration_stage_data_with_classname(
790
+				get_class($stage),
791
+				$stages_properties_arrays
792
+			);
793
+			// SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by
794
+			// an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet)
795
+			if ($stage_data) {
796
+				$stage->instantiate_from_array_of_properties($stage_data);
797
+			}
798
+		}
799
+	}
800
+
801
+
802
+	/**
803
+	 * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which
804
+	 * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes)
805
+	 * for the given classname
806
+	 *
807
+	 * @param string $classname
808
+	 * @param array  $migration_stage_data_arrays
809
+	 * @return null
810
+	 */
811
+	private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays)
812
+	{
813
+		foreach ($migration_stage_data_arrays as $migration_stage_data_array) {
814
+			if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) {
815
+				return $migration_stage_data_array;
816
+			}
817
+		}
818
+		return null;
819
+	}
820
+
821
+
822
+	/**
823
+	 * Returns the version that this script migrates to, based on the script's name.
824
+	 * Cannot be overwritten because lots of code needs to know which version a script
825
+	 * migrates to knowing only its name.
826
+	 *
827
+	 * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin
828
+	 * that will be updated to. Eg array('Core','4.1.0')
829
+	 */
830
+	final public function migrates_to_version()
831
+	{
832
+		return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this));
833
+	}
834
+
835
+
836
+	/**
837
+	 * Gets this addon's slug as it would appear in the current_db_state wp option,
838
+	 * and if this migration script is for an addon, it SHOULD match the addon's slug
839
+	 * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon.
840
+	 * Or 'Core' for core (non-addon).
841
+	 *
842
+	 * @return string
843
+	 */
844
+	public function slug()
845
+	{
846
+		$migrates_to_version_info = $this->migrates_to_version();
847
+		// the slug is the first part of the array
848
+		return $migrates_to_version_info['slug'];
849
+	}
850
+
851
+
852
+	/**
853
+	 * Returns the script's priority relative to DMSs from other addons. However, when
854
+	 * two DMSs from the same addon/core apply, this is ignored (and instead the version that
855
+	 * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs
856
+	 * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons,
857
+	 * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set
858
+	 * the database up so it can run), then you can set "A" to priority 3 or something.
859
+	 *
860
+	 * @return int
861
+	 */
862
+	public function priority()
863
+	{
864
+		return $this->_priority;
865
+	}
866
+
867
+
868
+	/**
869
+	 * Sets whether or not this DMS is being ran as part of a migration, instead of
870
+	 * just being used to setup (or verify) the current database structure matches
871
+	 * what the latest DMS indicates it should be
872
+	 *
873
+	 * @param boolean $migrating
874
+	 * @return void
875
+	 */
876
+	public function set_migrating($migrating = true)
877
+	{
878
+		$this->_migrating = $migrating;
879
+	}
880
+
881
+	/**
882
+	 * Marks that we think this migration class can continue to migrate
883
+	 */
884
+	public function reattempt()
885
+	{
886
+		parent::reattempt();
887
+		// also, we want to reattempt any stages that were marked as borked
888
+		foreach ($this->stages() as $stage) {
889
+			if ($stage->is_broken()) {
890
+				$stage->reattempt();
891
+			}
892
+		}
893
+	}
894 894
 }
Please login to merge, or discard this patch.
core/db_classes/EE_Event.class.php 1 patch
Indentation   +1353 added lines, -1353 removed lines patch added patch discarded remove patch
@@ -13,1357 +13,1357 @@
 block discarded – undo
13 13
 class EE_Event extends EE_CPT_Base implements EEI_Line_Item_Object, EEI_Admin_Links, EEI_Has_Icon, EEI_Event
14 14
 {
15 15
 
16
-    /**
17
-     * cached value for the the logical active status for the event
18
-     *
19
-     * @see get_active_status()
20
-     * @var string
21
-     */
22
-    protected $_active_status = '';
23
-
24
-    /**
25
-     * This is just used for caching the Primary Datetime for the Event on initial retrieval
26
-     *
27
-     * @var EE_Datetime
28
-     */
29
-    protected $_Primary_Datetime;
30
-
31
-    /**
32
-     * @var EventSpacesCalculator $available_spaces_calculator
33
-     */
34
-    protected $available_spaces_calculator;
35
-
36
-
37
-    /**
38
-     * @param array  $props_n_values          incoming values
39
-     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
40
-     *                                        used.)
41
-     * @param array  $date_formats            incoming date_formats in an array where the first value is the
42
-     *                                        date_format and the second value is the time format
43
-     * @return EE_Event
44
-     * @throws EE_Error
45
-     */
46
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
47
-    {
48
-        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
49
-        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
50
-    }
51
-
52
-
53
-    /**
54
-     * @param array  $props_n_values  incoming values from the database
55
-     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
56
-     *                                the website will be used.
57
-     * @return EE_Event
58
-     * @throws EE_Error
59
-     */
60
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
61
-    {
62
-        return new self($props_n_values, true, $timezone);
63
-    }
64
-
65
-
66
-    /**
67
-     * @return EventSpacesCalculator
68
-     * @throws \EE_Error
69
-     */
70
-    public function getAvailableSpacesCalculator()
71
-    {
72
-        if (! $this->available_spaces_calculator instanceof EventSpacesCalculator) {
73
-            $this->available_spaces_calculator = new EventSpacesCalculator($this);
74
-        }
75
-        return $this->available_spaces_calculator;
76
-    }
77
-
78
-
79
-    /**
80
-     * Overrides parent set() method so that all calls to set( 'status', $status ) can be routed to internal methods
81
-     *
82
-     * @param string $field_name
83
-     * @param mixed  $field_value
84
-     * @param bool   $use_default
85
-     * @throws EE_Error
86
-     */
87
-    public function set($field_name, $field_value, $use_default = false)
88
-    {
89
-        switch ($field_name) {
90
-            case 'status':
91
-                $this->set_status($field_value, $use_default);
92
-                break;
93
-            default:
94
-                parent::set($field_name, $field_value, $use_default);
95
-        }
96
-    }
97
-
98
-
99
-    /**
100
-     *    set_status
101
-     * Checks if event status is being changed to SOLD OUT
102
-     * and updates event meta data with previous event status
103
-     * so that we can revert things if/when the event is no longer sold out
104
-     *
105
-     * @access public
106
-     * @param string $new_status
107
-     * @param bool   $use_default
108
-     * @return void
109
-     * @throws EE_Error
110
-     */
111
-    public function set_status($new_status = null, $use_default = false)
112
-    {
113
-        // if nothing is set, and we aren't explicitly wanting to reset the status, then just leave
114
-        if (empty($new_status) && ! $use_default) {
115
-            return;
116
-        }
117
-        // get current Event status
118
-        $old_status = $this->status();
119
-        // if status has changed
120
-        if ($old_status !== $new_status) {
121
-            // TO sold_out
122
-            if ($new_status === EEM_Event::sold_out) {
123
-                // save the previous event status so that we can revert if the event is no longer sold out
124
-                $this->add_post_meta('_previous_event_status', $old_status);
125
-                do_action('AHEE__EE_Event__set_status__to_sold_out', $this, $old_status, $new_status);
126
-                // OR FROM  sold_out
127
-            } elseif ($old_status === EEM_Event::sold_out) {
128
-                $this->delete_post_meta('_previous_event_status');
129
-                do_action('AHEE__EE_Event__set_status__from_sold_out', $this, $old_status, $new_status);
130
-            }
131
-            // clear out the active status so that it gets reset the next time it is requested
132
-            $this->_active_status = null;
133
-            // update status
134
-            parent::set('status', $new_status, $use_default);
135
-            do_action('AHEE__EE_Event__set_status__after_update', $this);
136
-            return;
137
-        }
138
-        // even though the old value matches the new value, it's still good to
139
-        // allow the parent set method to have a say
140
-        parent::set('status', $new_status, $use_default);
141
-    }
142
-
143
-
144
-    /**
145
-     * Gets all the datetimes for this event
146
-     *
147
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
148
-     * @return EE_Base_Class[]|EE_Datetime[]
149
-     * @throws EE_Error
150
-     */
151
-    public function datetimes($query_params = array())
152
-    {
153
-        return $this->get_many_related('Datetime', $query_params);
154
-    }
155
-
156
-
157
-    /**
158
-     * Gets all the datetimes for this event, ordered by DTT_EVT_start in ascending order
159
-     *
160
-     * @return EE_Base_Class[]|EE_Datetime[]
161
-     * @throws EE_Error
162
-     */
163
-    public function datetimes_in_chronological_order()
164
-    {
165
-        return $this->get_many_related('Datetime', array('order_by' => array('DTT_EVT_start' => 'ASC')));
166
-    }
167
-
168
-
169
-    /**
170
-     * Gets all the datetimes for this event, ordered by the DTT_order on the datetime.
171
-     * @darren, we should probably UNSET timezone on the EEM_Datetime model
172
-     * after running our query, so that this timezone isn't set for EVERY query
173
-     * on EEM_Datetime for the rest of the request, no?
174
-     *
175
-     * @param boolean $show_expired whether or not to include expired events
176
-     * @param boolean $show_deleted whether or not to include deleted events
177
-     * @param null    $limit
178
-     * @return EE_Datetime[]
179
-     * @throws EE_Error
180
-     */
181
-    public function datetimes_ordered($show_expired = true, $show_deleted = false, $limit = null)
182
-    {
183
-        return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_event_ordered_by_DTT_order(
184
-            $this->ID(),
185
-            $show_expired,
186
-            $show_deleted,
187
-            $limit
188
-        );
189
-    }
190
-
191
-
192
-    /**
193
-     * Returns one related datetime. Mostly only used by some legacy code.
194
-     *
195
-     * @return EE_Base_Class|EE_Datetime
196
-     * @throws EE_Error
197
-     */
198
-    public function first_datetime()
199
-    {
200
-        return $this->get_first_related('Datetime');
201
-    }
202
-
203
-
204
-    /**
205
-     * Returns the 'primary' datetime for the event
206
-     *
207
-     * @param bool $try_to_exclude_expired
208
-     * @param bool $try_to_exclude_deleted
209
-     * @return EE_Datetime
210
-     * @throws EE_Error
211
-     */
212
-    public function primary_datetime($try_to_exclude_expired = true, $try_to_exclude_deleted = true)
213
-    {
214
-        if (! empty($this->_Primary_Datetime)) {
215
-            return $this->_Primary_Datetime;
216
-        }
217
-        $this->_Primary_Datetime = EEM_Datetime::instance($this->_timezone)->get_primary_datetime_for_event(
218
-            $this->ID(),
219
-            $try_to_exclude_expired,
220
-            $try_to_exclude_deleted
221
-        );
222
-        return $this->_Primary_Datetime;
223
-    }
224
-
225
-
226
-    /**
227
-     * Gets all the tickets available for purchase of this event
228
-     *
229
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
230
-     * @return EE_Base_Class[]|EE_Ticket[]
231
-     * @throws EE_Error
232
-     */
233
-    public function tickets($query_params = array())
234
-    {
235
-        // first get all datetimes
236
-        $datetimes = $this->datetimes_ordered();
237
-        if (! $datetimes) {
238
-            return array();
239
-        }
240
-        $datetime_ids = array();
241
-        foreach ($datetimes as $datetime) {
242
-            $datetime_ids[] = $datetime->ID();
243
-        }
244
-        $where_params = array('Datetime.DTT_ID' => array('IN', $datetime_ids));
245
-        // if incoming $query_params has where conditions let's merge but not override existing.
246
-        if (is_array($query_params) && isset($query_params[0])) {
247
-            $where_params = array_merge($query_params[0], $where_params);
248
-            unset($query_params[0]);
249
-        }
250
-        // now add $where_params to $query_params
251
-        $query_params[0] = $where_params;
252
-        return EEM_Ticket::instance()->get_all($query_params);
253
-    }
254
-
255
-
256
-    /**
257
-     * get all unexpired untrashed tickets
258
-     *
259
-     * @return EE_Ticket[]
260
-     * @throws EE_Error
261
-     */
262
-    public function active_tickets()
263
-    {
264
-        return $this->tickets(
265
-            array(
266
-                array(
267
-                    'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')),
268
-                    'TKT_deleted'  => false,
269
-                ),
270
-            )
271
-        );
272
-    }
273
-
274
-
275
-    /**
276
-     * @return bool
277
-     * @throws EE_Error
278
-     */
279
-    public function additional_limit()
280
-    {
281
-        return $this->get('EVT_additional_limit');
282
-    }
283
-
284
-
285
-    /**
286
-     * @return bool
287
-     * @throws EE_Error
288
-     */
289
-    public function allow_overflow()
290
-    {
291
-        return $this->get('EVT_allow_overflow');
292
-    }
293
-
294
-
295
-    /**
296
-     * @return bool
297
-     * @throws EE_Error
298
-     */
299
-    public function created()
300
-    {
301
-        return $this->get('EVT_created');
302
-    }
303
-
304
-
305
-    /**
306
-     * @return bool
307
-     * @throws EE_Error
308
-     */
309
-    public function description()
310
-    {
311
-        return $this->get('EVT_desc');
312
-    }
313
-
314
-
315
-    /**
316
-     * Runs do_shortcode and wpautop on the description
317
-     *
318
-     * @return string of html
319
-     * @throws EE_Error
320
-     */
321
-    public function description_filtered()
322
-    {
323
-        return $this->get_pretty('EVT_desc');
324
-    }
325
-
326
-
327
-    /**
328
-     * @return bool
329
-     * @throws EE_Error
330
-     */
331
-    public function display_description()
332
-    {
333
-        return $this->get('EVT_display_desc');
334
-    }
335
-
336
-
337
-    /**
338
-     * @return bool
339
-     * @throws EE_Error
340
-     */
341
-    public function display_ticket_selector()
342
-    {
343
-        return (bool) $this->get('EVT_display_ticket_selector');
344
-    }
345
-
346
-
347
-    /**
348
-     * @return bool
349
-     * @throws EE_Error
350
-     */
351
-    public function external_url()
352
-    {
353
-        return $this->get('EVT_external_URL');
354
-    }
355
-
356
-
357
-    /**
358
-     * @return bool
359
-     * @throws EE_Error
360
-     */
361
-    public function member_only()
362
-    {
363
-        return $this->get('EVT_member_only');
364
-    }
365
-
366
-
367
-    /**
368
-     * @return bool
369
-     * @throws EE_Error
370
-     */
371
-    public function phone()
372
-    {
373
-        return $this->get('EVT_phone');
374
-    }
375
-
376
-
377
-    /**
378
-     * @return bool
379
-     * @throws EE_Error
380
-     */
381
-    public function modified()
382
-    {
383
-        return $this->get('EVT_modified');
384
-    }
385
-
386
-
387
-    /**
388
-     * @return bool
389
-     * @throws EE_Error
390
-     */
391
-    public function name()
392
-    {
393
-        return $this->get('EVT_name');
394
-    }
395
-
396
-
397
-    /**
398
-     * @return bool
399
-     * @throws EE_Error
400
-     */
401
-    public function order()
402
-    {
403
-        return $this->get('EVT_order');
404
-    }
405
-
406
-
407
-    /**
408
-     * @return bool|string
409
-     * @throws EE_Error
410
-     */
411
-    public function default_registration_status()
412
-    {
413
-        $event_default_registration_status = $this->get('EVT_default_registration_status');
414
-        return ! empty($event_default_registration_status)
415
-            ? $event_default_registration_status
416
-            : EE_Registry::instance()->CFG->registration->default_STS_ID;
417
-    }
418
-
419
-
420
-    /**
421
-     * @param int  $num_words
422
-     * @param null $more
423
-     * @param bool $not_full_desc
424
-     * @return bool|string
425
-     * @throws EE_Error
426
-     */
427
-    public function short_description($num_words = 55, $more = null, $not_full_desc = false)
428
-    {
429
-        $short_desc = $this->get('EVT_short_desc');
430
-        if (! empty($short_desc) || $not_full_desc) {
431
-            return $short_desc;
432
-        }
433
-        $full_desc = $this->get('EVT_desc');
434
-        return wp_trim_words($full_desc, $num_words, $more);
435
-    }
436
-
437
-
438
-    /**
439
-     * @return bool
440
-     * @throws EE_Error
441
-     */
442
-    public function slug()
443
-    {
444
-        return $this->get('EVT_slug');
445
-    }
446
-
447
-
448
-    /**
449
-     * @return bool
450
-     * @throws EE_Error
451
-     */
452
-    public function timezone_string()
453
-    {
454
-        return $this->get('EVT_timezone_string');
455
-    }
456
-
457
-
458
-    /**
459
-     * @return bool
460
-     * @throws EE_Error
461
-     */
462
-    public function visible_on()
463
-    {
464
-        return $this->get('EVT_visible_on');
465
-    }
466
-
467
-
468
-    /**
469
-     * @return int
470
-     * @throws EE_Error
471
-     */
472
-    public function wp_user()
473
-    {
474
-        return $this->get('EVT_wp_user');
475
-    }
476
-
477
-
478
-    /**
479
-     * @return bool
480
-     * @throws EE_Error
481
-     */
482
-    public function donations()
483
-    {
484
-        return $this->get('EVT_donations');
485
-    }
486
-
487
-
488
-    /**
489
-     * @param $limit
490
-     * @throws EE_Error
491
-     */
492
-    public function set_additional_limit($limit)
493
-    {
494
-        $this->set('EVT_additional_limit', $limit);
495
-    }
496
-
497
-
498
-    /**
499
-     * @param $created
500
-     * @throws EE_Error
501
-     */
502
-    public function set_created($created)
503
-    {
504
-        $this->set('EVT_created', $created);
505
-    }
506
-
507
-
508
-    /**
509
-     * @param $desc
510
-     * @throws EE_Error
511
-     */
512
-    public function set_description($desc)
513
-    {
514
-        $this->set('EVT_desc', $desc);
515
-    }
516
-
517
-
518
-    /**
519
-     * @param $display_desc
520
-     * @throws EE_Error
521
-     */
522
-    public function set_display_description($display_desc)
523
-    {
524
-        $this->set('EVT_display_desc', $display_desc);
525
-    }
526
-
527
-
528
-    /**
529
-     * @param $display_ticket_selector
530
-     * @throws EE_Error
531
-     */
532
-    public function set_display_ticket_selector($display_ticket_selector)
533
-    {
534
-        $this->set('EVT_display_ticket_selector', $display_ticket_selector);
535
-    }
536
-
537
-
538
-    /**
539
-     * @param $external_url
540
-     * @throws EE_Error
541
-     */
542
-    public function set_external_url($external_url)
543
-    {
544
-        $this->set('EVT_external_URL', $external_url);
545
-    }
546
-
547
-
548
-    /**
549
-     * @param $member_only
550
-     * @throws EE_Error
551
-     */
552
-    public function set_member_only($member_only)
553
-    {
554
-        $this->set('EVT_member_only', $member_only);
555
-    }
556
-
557
-
558
-    /**
559
-     * @param $event_phone
560
-     * @throws EE_Error
561
-     */
562
-    public function set_event_phone($event_phone)
563
-    {
564
-        $this->set('EVT_phone', $event_phone);
565
-    }
566
-
567
-
568
-    /**
569
-     * @param $modified
570
-     * @throws EE_Error
571
-     */
572
-    public function set_modified($modified)
573
-    {
574
-        $this->set('EVT_modified', $modified);
575
-    }
576
-
577
-
578
-    /**
579
-     * @param $name
580
-     * @throws EE_Error
581
-     */
582
-    public function set_name($name)
583
-    {
584
-        $this->set('EVT_name', $name);
585
-    }
586
-
587
-
588
-    /**
589
-     * @param $order
590
-     * @throws EE_Error
591
-     */
592
-    public function set_order($order)
593
-    {
594
-        $this->set('EVT_order', $order);
595
-    }
596
-
597
-
598
-    /**
599
-     * @param $short_desc
600
-     * @throws EE_Error
601
-     */
602
-    public function set_short_description($short_desc)
603
-    {
604
-        $this->set('EVT_short_desc', $short_desc);
605
-    }
606
-
607
-
608
-    /**
609
-     * @param $slug
610
-     * @throws EE_Error
611
-     */
612
-    public function set_slug($slug)
613
-    {
614
-        $this->set('EVT_slug', $slug);
615
-    }
616
-
617
-
618
-    /**
619
-     * @param $timezone_string
620
-     * @throws EE_Error
621
-     */
622
-    public function set_timezone_string($timezone_string)
623
-    {
624
-        $this->set('EVT_timezone_string', $timezone_string);
625
-    }
626
-
627
-
628
-    /**
629
-     * @param $visible_on
630
-     * @throws EE_Error
631
-     */
632
-    public function set_visible_on($visible_on)
633
-    {
634
-        $this->set('EVT_visible_on', $visible_on);
635
-    }
636
-
637
-
638
-    /**
639
-     * @param $wp_user
640
-     * @throws EE_Error
641
-     */
642
-    public function set_wp_user($wp_user)
643
-    {
644
-        $this->set('EVT_wp_user', $wp_user);
645
-    }
646
-
647
-
648
-    /**
649
-     * @param $default_registration_status
650
-     * @throws EE_Error
651
-     */
652
-    public function set_default_registration_status($default_registration_status)
653
-    {
654
-        $this->set('EVT_default_registration_status', $default_registration_status);
655
-    }
656
-
657
-
658
-    /**
659
-     * @param $donations
660
-     * @throws EE_Error
661
-     */
662
-    public function set_donations($donations)
663
-    {
664
-        $this->set('EVT_donations', $donations);
665
-    }
666
-
667
-
668
-    /**
669
-     * Adds a venue to this event
670
-     *
671
-     * @param EE_Venue /int $venue_id_or_obj
672
-     * @return EE_Base_Class|EE_Venue
673
-     * @throws EE_Error
674
-     */
675
-    public function add_venue($venue_id_or_obj)
676
-    {
677
-        return $this->_add_relation_to($venue_id_or_obj, 'Venue');
678
-    }
679
-
680
-
681
-    /**
682
-     * Removes a venue from the event
683
-     *
684
-     * @param EE_Venue /int $venue_id_or_obj
685
-     * @return EE_Base_Class|EE_Venue
686
-     * @throws EE_Error
687
-     */
688
-    public function remove_venue($venue_id_or_obj)
689
-    {
690
-        return $this->_remove_relation_to($venue_id_or_obj, 'Venue');
691
-    }
692
-
693
-
694
-    /**
695
-     * Gets all the venues related ot the event. May provide additional $query_params if desired
696
-     *
697
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
698
-     * @return EE_Base_Class[]|EE_Venue[]
699
-     * @throws EE_Error
700
-     */
701
-    public function venues($query_params = array())
702
-    {
703
-        return $this->get_many_related('Venue', $query_params);
704
-    }
705
-
706
-
707
-    /**
708
-     * check if event id is present and if event is published
709
-     *
710
-     * @access public
711
-     * @return boolean true yes, false no
712
-     * @throws EE_Error
713
-     */
714
-    private function _has_ID_and_is_published()
715
-    {
716
-        // first check if event id is present and not NULL,
717
-        // then check if this event is published (or any of the equivalent "published" statuses)
718
-        return
719
-            $this->ID() && $this->ID() !== null
720
-            && (
721
-                $this->status() === 'publish'
722
-                || $this->status() === EEM_Event::sold_out
723
-                || $this->status() === EEM_Event::postponed
724
-                || $this->status() === EEM_Event::cancelled
725
-            );
726
-    }
727
-
728
-
729
-    /**
730
-     * This simply compares the internal dates with NOW and determines if the event is upcoming or not.
731
-     *
732
-     * @access public
733
-     * @return boolean true yes, false no
734
-     * @throws EE_Error
735
-     */
736
-    public function is_upcoming()
737
-    {
738
-        // check if event id is present and if this event is published
739
-        if ($this->is_inactive()) {
740
-            return false;
741
-        }
742
-        // set initial value
743
-        $upcoming = false;
744
-        // next let's get all datetimes and loop through them
745
-        $datetimes = $this->datetimes_in_chronological_order();
746
-        foreach ($datetimes as $datetime) {
747
-            if ($datetime instanceof EE_Datetime) {
748
-                // if this dtt is expired then we continue cause one of the other datetimes might be upcoming.
749
-                if ($datetime->is_expired()) {
750
-                    continue;
751
-                }
752
-                // if this dtt is active then we return false.
753
-                if ($datetime->is_active()) {
754
-                    return false;
755
-                }
756
-                // otherwise let's check upcoming status
757
-                $upcoming = $datetime->is_upcoming();
758
-            }
759
-        }
760
-        return $upcoming;
761
-    }
762
-
763
-
764
-    /**
765
-     * @return bool
766
-     * @throws EE_Error
767
-     */
768
-    public function is_active()
769
-    {
770
-        // check if event id is present and if this event is published
771
-        if ($this->is_inactive()) {
772
-            return false;
773
-        }
774
-        // set initial value
775
-        $active = false;
776
-        // next let's get all datetimes and loop through them
777
-        $datetimes = $this->datetimes_in_chronological_order();
778
-        foreach ($datetimes as $datetime) {
779
-            if ($datetime instanceof EE_Datetime) {
780
-                // if this dtt is expired then we continue cause one of the other datetimes might be active.
781
-                if ($datetime->is_expired()) {
782
-                    continue;
783
-                }
784
-                // if this dtt is upcoming then we return false.
785
-                if ($datetime->is_upcoming()) {
786
-                    return false;
787
-                }
788
-                // otherwise let's check active status
789
-                $active = $datetime->is_active();
790
-            }
791
-        }
792
-        return $active;
793
-    }
794
-
795
-
796
-    /**
797
-     * @return bool
798
-     * @throws EE_Error
799
-     */
800
-    public function is_expired()
801
-    {
802
-        // check if event id is present and if this event is published
803
-        if ($this->is_inactive()) {
804
-            return false;
805
-        }
806
-        // set initial value
807
-        $expired = false;
808
-        // first let's get all datetimes and loop through them
809
-        $datetimes = $this->datetimes_in_chronological_order();
810
-        foreach ($datetimes as $datetime) {
811
-            if ($datetime instanceof EE_Datetime) {
812
-                // if this dtt is upcoming or active then we return false.
813
-                if ($datetime->is_upcoming() || $datetime->is_active()) {
814
-                    return false;
815
-                }
816
-                // otherwise let's check active status
817
-                $expired = $datetime->is_expired();
818
-            }
819
-        }
820
-        return $expired;
821
-    }
822
-
823
-
824
-    /**
825
-     * @return bool
826
-     * @throws EE_Error
827
-     */
828
-    public function is_inactive()
829
-    {
830
-        // check if event id is present and if this event is published
831
-        if ($this->_has_ID_and_is_published()) {
832
-            return false;
833
-        }
834
-        return true;
835
-    }
836
-
837
-
838
-    /**
839
-     * calculate spaces remaining based on "saleable" tickets
840
-     *
841
-     * @param array $tickets
842
-     * @param bool  $filtered
843
-     * @return int|float
844
-     * @throws EE_Error
845
-     * @throws DomainException
846
-     * @throws UnexpectedEntityException
847
-     */
848
-    public function spaces_remaining($tickets = array(), $filtered = true)
849
-    {
850
-        $this->getAvailableSpacesCalculator()->setActiveTickets($tickets);
851
-        $spaces_remaining = $this->getAvailableSpacesCalculator()->spacesRemaining();
852
-        return $filtered
853
-            ? apply_filters(
854
-                'FHEE_EE_Event__spaces_remaining',
855
-                $spaces_remaining,
856
-                $this,
857
-                $tickets
858
-            )
859
-            : $spaces_remaining;
860
-    }
861
-
862
-
863
-    /**
864
-     *    perform_sold_out_status_check
865
-     *    checks all of this events's datetime  reg_limit - sold values to determine if ANY datetimes have spaces
866
-     *    available... if NOT, then the event status will get toggled to 'sold_out'
867
-     *
868
-     * @return bool    return the ACTUAL sold out state.
869
-     * @throws EE_Error
870
-     * @throws DomainException
871
-     * @throws UnexpectedEntityException
872
-     */
873
-    public function perform_sold_out_status_check()
874
-    {
875
-        // get all unexpired untrashed tickets
876
-        $tickets = $this->tickets(
877
-            array(
878
-                array('TKT_deleted' => false),
879
-                'order_by' => array('TKT_qty' => 'ASC'),
880
-            )
881
-        );
882
-        $all_expired = true;
883
-        foreach ($tickets as $ticket) {
884
-            if (! $ticket->is_expired()) {
885
-                $all_expired = false;
886
-                break;
887
-            }
888
-        }
889
-        // if all the tickets are just expired, then don't update the event status to sold out
890
-        if ($all_expired) {
891
-            return true;
892
-        }
893
-        $spaces_remaining = $this->spaces_remaining($tickets);
894
-        if ($spaces_remaining < 1) {
895
-            if ($this->status() !== EEM_Event::post_status_private) {
896
-                $this->set_status(EEM_Event::sold_out);
897
-                $this->save();
898
-            }
899
-            $sold_out = true;
900
-        } else {
901
-            $sold_out = false;
902
-            // was event previously marked as sold out ?
903
-            if ($this->status() === EEM_Event::sold_out) {
904
-                // revert status to previous value, if it was set
905
-                $previous_event_status = $this->get_post_meta('_previous_event_status', true);
906
-                if ($previous_event_status) {
907
-                    $this->set_status($previous_event_status);
908
-                    $this->save();
909
-                }
910
-            }
911
-        }
912
-        do_action('AHEE__EE_Event__perform_sold_out_status_check__end', $this, $sold_out, $spaces_remaining, $tickets);
913
-        return $sold_out;
914
-    }
915
-
916
-
917
-    /**
918
-     * This returns the total remaining spaces for sale on this event.
919
-     *
920
-     * @uses EE_Event::total_available_spaces()
921
-     * @return float|int
922
-     * @throws EE_Error
923
-     * @throws DomainException
924
-     * @throws UnexpectedEntityException
925
-     */
926
-    public function spaces_remaining_for_sale()
927
-    {
928
-        return $this->total_available_spaces(true);
929
-    }
930
-
931
-
932
-    /**
933
-     * This returns the total spaces available for an event
934
-     * while considering all the qtys on the tickets and the reg limits
935
-     * on the datetimes attached to this event.
936
-     *
937
-     * @param   bool $consider_sold Whether to consider any tickets that have already sold in our calculation.
938
-     *                              If this is false, then we return the most tickets that could ever be sold
939
-     *                              for this event with the datetime and tickets setup on the event under optimal
940
-     *                              selling conditions.  Otherwise we return a live calculation of spaces available
941
-     *                              based on tickets sold.  Depending on setup and stage of sales, this
942
-     *                              may appear to equal remaining tickets.  However, the more tickets are
943
-     *                              sold out, the more accurate the "live" total is.
944
-     * @return float|int
945
-     * @throws EE_Error
946
-     * @throws DomainException
947
-     * @throws UnexpectedEntityException
948
-     */
949
-    public function total_available_spaces($consider_sold = false)
950
-    {
951
-        $spaces_available = $consider_sold
952
-            ? $this->getAvailableSpacesCalculator()->spacesRemaining()
953
-            : $this->getAvailableSpacesCalculator()->totalSpacesAvailable();
954
-        return apply_filters(
955
-            'FHEE_EE_Event__total_available_spaces__spaces_available',
956
-            $spaces_available,
957
-            $this,
958
-            $this->getAvailableSpacesCalculator()->getDatetimes(),
959
-            $this->getAvailableSpacesCalculator()->getActiveTickets()
960
-        );
961
-    }
962
-
963
-
964
-    /**
965
-     * Checks if the event is set to sold out
966
-     *
967
-     * @param  bool $actual whether or not to perform calculations to not only figure the
968
-     *                      actual status but also to flip the status if necessary to sold
969
-     *                      out If false, we just check the existing status of the event
970
-     * @return boolean
971
-     * @throws EE_Error
972
-     */
973
-    public function is_sold_out($actual = false)
974
-    {
975
-        if (! $actual) {
976
-            return $this->status() === EEM_Event::sold_out;
977
-        }
978
-        return $this->perform_sold_out_status_check();
979
-    }
980
-
981
-
982
-    /**
983
-     * Checks if the event is marked as postponed
984
-     *
985
-     * @return boolean
986
-     */
987
-    public function is_postponed()
988
-    {
989
-        return $this->status() === EEM_Event::postponed;
990
-    }
991
-
992
-
993
-    /**
994
-     * Checks if the event is marked as cancelled
995
-     *
996
-     * @return boolean
997
-     */
998
-    public function is_cancelled()
999
-    {
1000
-        return $this->status() === EEM_Event::cancelled;
1001
-    }
1002
-
1003
-
1004
-    /**
1005
-     * Get the logical active status in a hierarchical order for all the datetimes.  Note
1006
-     * Basically, we order the datetimes by EVT_start_date.  Then first test on whether the event is published.  If its
1007
-     * NOT published then we test for whether its expired or not.  IF it IS published then we test first on whether an
1008
-     * event has any active dates.  If no active dates then we check for any upcoming dates.  If no upcoming dates then
1009
-     * the event is considered expired.
1010
-     * NOTE: this method does NOT calculate whether the datetimes are sold out when event is published.  Sold Out is a
1011
-     * status set on the EVENT when it is not published and thus is done
1012
-     *
1013
-     * @param bool $reset
1014
-     * @return bool | string - based on EE_Datetime active constants or FALSE if error.
1015
-     * @throws EE_Error
1016
-     */
1017
-    public function get_active_status($reset = false)
1018
-    {
1019
-        // if the active status has already been set, then just use that value (unless we are resetting it)
1020
-        if (! empty($this->_active_status) && ! $reset) {
1021
-            return $this->_active_status;
1022
-        }
1023
-        // first check if event id is present on this object
1024
-        if (! $this->ID()) {
1025
-            return false;
1026
-        }
1027
-        $where_params_for_event = array(array('EVT_ID' => $this->ID()));
1028
-        // if event is published:
1029
-        if ($this->status() === EEM_Event::post_status_publish || $this->status() === EEM_Event::post_status_private) {
1030
-            // active?
1031
-            if (EEM_Datetime::instance()->get_datetime_count_for_status(
1032
-                EE_Datetime::active,
1033
-                $where_params_for_event
1034
-            ) > 0) {
1035
-                $this->_active_status = EE_Datetime::active;
1036
-            } else {
1037
-                // upcoming?
1038
-                if (EEM_Datetime::instance()->get_datetime_count_for_status(
1039
-                    EE_Datetime::upcoming,
1040
-                    $where_params_for_event
1041
-                ) > 0) {
1042
-                    $this->_active_status = EE_Datetime::upcoming;
1043
-                } else {
1044
-                    // expired?
1045
-                    if (EEM_Datetime::instance()->get_datetime_count_for_status(
1046
-                        EE_Datetime::expired,
1047
-                        $where_params_for_event
1048
-                    ) > 0
1049
-                    ) {
1050
-                        $this->_active_status = EE_Datetime::expired;
1051
-                    } else {
1052
-                        // it would be odd if things make it this far because it basically means there are no datetime's
1053
-                        // attached to the event.  So in this case it will just be considered inactive.
1054
-                        $this->_active_status = EE_Datetime::inactive;
1055
-                    }
1056
-                }
1057
-            }
1058
-        } else {
1059
-            // the event is not published, so let's just set it's active status according to its' post status
1060
-            switch ($this->status()) {
1061
-                case EEM_Event::sold_out:
1062
-                    $this->_active_status = EE_Datetime::sold_out;
1063
-                    break;
1064
-                case EEM_Event::cancelled:
1065
-                    $this->_active_status = EE_Datetime::cancelled;
1066
-                    break;
1067
-                case EEM_Event::postponed:
1068
-                    $this->_active_status = EE_Datetime::postponed;
1069
-                    break;
1070
-                default:
1071
-                    $this->_active_status = EE_Datetime::inactive;
1072
-            }
1073
-        }
1074
-        return $this->_active_status;
1075
-    }
1076
-
1077
-
1078
-    /**
1079
-     *    pretty_active_status
1080
-     *
1081
-     * @access public
1082
-     * @param boolean $echo whether to return (FALSE), or echo out the result (TRUE)
1083
-     * @return mixed void|string
1084
-     * @throws EE_Error
1085
-     */
1086
-    public function pretty_active_status($echo = true)
1087
-    {
1088
-        $active_status = $this->get_active_status();
1089
-        $status = '<span class="ee-status event-active-status-'
1090
-                  . $active_status
1091
-                  . '">'
1092
-                  . EEH_Template::pretty_status($active_status, false, 'sentence')
1093
-                  . '</span>';
1094
-        if ($echo) {
1095
-            echo $status;
1096
-            return '';
1097
-        }
1098
-        return $status;
1099
-    }
1100
-
1101
-
1102
-    /**
1103
-     * @return bool|int
1104
-     * @throws EE_Error
1105
-     */
1106
-    public function get_number_of_tickets_sold()
1107
-    {
1108
-        $tkt_sold = 0;
1109
-        if (! $this->ID()) {
1110
-            return 0;
1111
-        }
1112
-        $datetimes = $this->datetimes();
1113
-        foreach ($datetimes as $datetime) {
1114
-            if ($datetime instanceof EE_Datetime) {
1115
-                $tkt_sold += $datetime->sold();
1116
-            }
1117
-        }
1118
-        return $tkt_sold;
1119
-    }
1120
-
1121
-
1122
-    /**
1123
-     * This just returns a count of all the registrations for this event
1124
-     *
1125
-     * @access  public
1126
-     * @return int
1127
-     * @throws EE_Error
1128
-     */
1129
-    public function get_count_of_all_registrations()
1130
-    {
1131
-        return EEM_Event::instance()->count_related($this, 'Registration');
1132
-    }
1133
-
1134
-
1135
-    /**
1136
-     * This returns the ticket with the earliest start time that is
1137
-     * available for this event (across all datetimes attached to the event)
1138
-     *
1139
-     * @return EE_Base_Class|EE_Ticket|null
1140
-     * @throws EE_Error
1141
-     */
1142
-    public function get_ticket_with_earliest_start_time()
1143
-    {
1144
-        $where['Datetime.EVT_ID'] = $this->ID();
1145
-        $query_params = array($where, 'order_by' => array('TKT_start_date' => 'ASC'));
1146
-        return EE_Registry::instance()->load_model('Ticket')->get_one($query_params);
1147
-    }
1148
-
1149
-
1150
-    /**
1151
-     * This returns the ticket with the latest end time that is available
1152
-     * for this event (across all datetimes attached to the event)
1153
-     *
1154
-     * @return EE_Base_Class|EE_Ticket|null
1155
-     * @throws EE_Error
1156
-     */
1157
-    public function get_ticket_with_latest_end_time()
1158
-    {
1159
-        $where['Datetime.EVT_ID'] = $this->ID();
1160
-        $query_params = array($where, 'order_by' => array('TKT_end_date' => 'DESC'));
1161
-        return EE_Registry::instance()->load_model('Ticket')->get_one($query_params);
1162
-    }
1163
-
1164
-
1165
-    /**
1166
-     * This returns the number of different ticket types currently on sale for this event.
1167
-     *
1168
-     * @return int
1169
-     * @throws EE_Error
1170
-     */
1171
-    public function countTicketsOnSale()
1172
-    {
1173
-        $where = array(
1174
-            'Datetime.EVT_ID' => $this->ID(),
1175
-            'TKT_start_date'  => array('<', time()),
1176
-            'TKT_end_date'    => array('>', time()),
1177
-        );
1178
-        return EEM_Ticket::instance()->count(array($where));
1179
-    }
1180
-
1181
-
1182
-    /**
1183
-     * This returns whether there are any tickets on sale for this event.
1184
-     *
1185
-     * @return bool true = YES tickets on sale.
1186
-     * @throws EE_Error
1187
-     */
1188
-    public function tickets_on_sale()
1189
-    {
1190
-        return $this->countTicketsOnSale() > 0;
1191
-    }
1192
-
1193
-
1194
-    /**
1195
-     * Gets the URL for viewing this event on the front-end. Overrides parent
1196
-     * to check for an external URL first
1197
-     *
1198
-     * @return string
1199
-     * @throws EE_Error
1200
-     */
1201
-    public function get_permalink()
1202
-    {
1203
-        if ($this->external_url()) {
1204
-            return $this->external_url();
1205
-        }
1206
-        return parent::get_permalink();
1207
-    }
1208
-
1209
-
1210
-    /**
1211
-     * Gets the first term for 'espresso_event_categories' we can find
1212
-     *
1213
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1214
-     * @return EE_Base_Class|EE_Term|null
1215
-     * @throws EE_Error
1216
-     */
1217
-    public function first_event_category($query_params = array())
1218
-    {
1219
-        $query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
1220
-        $query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $this->ID();
1221
-        return EEM_Term::instance()->get_one($query_params);
1222
-    }
1223
-
1224
-
1225
-    /**
1226
-     * Gets all terms for 'espresso_event_categories' we can find
1227
-     *
1228
-     * @param array $query_params
1229
-     * @return EE_Base_Class[]|EE_Term[]
1230
-     * @throws EE_Error
1231
-     */
1232
-    public function get_all_event_categories($query_params = array())
1233
-    {
1234
-        $query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
1235
-        $query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $this->ID();
1236
-        return EEM_Term::instance()->get_all($query_params);
1237
-    }
1238
-
1239
-
1240
-    /**
1241
-     * Adds a question group to this event
1242
-     *
1243
-     * @param EE_Question_Group|int $question_group_id_or_obj
1244
-     * @param bool                  $for_primary if true, the question group will be added for the primary
1245
-     *                                           registrant, if false will be added for others. default: false
1246
-     * @return EE_Base_Class|EE_Question_Group
1247
-     * @throws EE_Error
1248
-     */
1249
-    public function add_question_group($question_group_id_or_obj, $for_primary = false)
1250
-    {
1251
-        $extra = $for_primary
1252
-            ? array('EQG_primary' => 1)
1253
-            : array();
1254
-        return $this->_add_relation_to($question_group_id_or_obj, 'Question_Group', $extra);
1255
-    }
1256
-
1257
-
1258
-    /**
1259
-     * Removes a question group from the event
1260
-     *
1261
-     * @param EE_Question_Group|int $question_group_id_or_obj
1262
-     * @param bool                  $for_primary if true, the question group will be removed from the primary
1263
-     *                                           registrant, if false will be removed from others. default: false
1264
-     * @return EE_Base_Class|EE_Question_Group
1265
-     * @throws EE_Error
1266
-     */
1267
-    public function remove_question_group($question_group_id_or_obj, $for_primary = false)
1268
-    {
1269
-        $where = $for_primary
1270
-            ? array('EQG_primary' => 1)
1271
-            : array();
1272
-        return $this->_remove_relation_to($question_group_id_or_obj, 'Question_Group', $where);
1273
-    }
1274
-
1275
-
1276
-    /**
1277
-     * Gets all the question groups, ordering them by QSG_order ascending
1278
-     *
1279
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1280
-     * @return EE_Base_Class[]|EE_Question_Group[]
1281
-     * @throws EE_Error
1282
-     */
1283
-    public function question_groups($query_params = array())
1284
-    {
1285
-        $query_params = ! empty($query_params) ? $query_params : array('order_by' => array('QSG_order' => 'ASC'));
1286
-        return $this->get_many_related('Question_Group', $query_params);
1287
-    }
1288
-
1289
-
1290
-    /**
1291
-     * Implementation for EEI_Has_Icon interface method.
1292
-     *
1293
-     * @see EEI_Visual_Representation for comments
1294
-     * @return string
1295
-     */
1296
-    public function get_icon()
1297
-    {
1298
-        return '<span class="dashicons dashicons-flag"></span>';
1299
-    }
1300
-
1301
-
1302
-    /**
1303
-     * Implementation for EEI_Admin_Links interface method.
1304
-     *
1305
-     * @see EEI_Admin_Links for comments
1306
-     * @return string
1307
-     * @throws EE_Error
1308
-     */
1309
-    public function get_admin_details_link()
1310
-    {
1311
-        return $this->get_admin_edit_link();
1312
-    }
1313
-
1314
-
1315
-    /**
1316
-     * Implementation for EEI_Admin_Links interface method.
1317
-     *
1318
-     * @see EEI_Admin_Links for comments
1319
-     * @return string
1320
-     * @throws EE_Error
1321
-     */
1322
-    public function get_admin_edit_link()
1323
-    {
1324
-        return EEH_URL::add_query_args_and_nonce(
1325
-            array(
1326
-                'page'   => 'espresso_events',
1327
-                'action' => 'edit',
1328
-                'post'   => $this->ID(),
1329
-            ),
1330
-            admin_url('admin.php')
1331
-        );
1332
-    }
1333
-
1334
-
1335
-    /**
1336
-     * Implementation for EEI_Admin_Links interface method.
1337
-     *
1338
-     * @see EEI_Admin_Links for comments
1339
-     * @return string
1340
-     */
1341
-    public function get_admin_settings_link()
1342
-    {
1343
-        return EEH_URL::add_query_args_and_nonce(
1344
-            array(
1345
-                'page'   => 'espresso_events',
1346
-                'action' => 'default_event_settings',
1347
-            ),
1348
-            admin_url('admin.php')
1349
-        );
1350
-    }
1351
-
1352
-
1353
-    /**
1354
-     * Implementation for EEI_Admin_Links interface method.
1355
-     *
1356
-     * @see EEI_Admin_Links for comments
1357
-     * @return string
1358
-     */
1359
-    public function get_admin_overview_link()
1360
-    {
1361
-        return EEH_URL::add_query_args_and_nonce(
1362
-            array(
1363
-                'page'   => 'espresso_events',
1364
-                'action' => 'default',
1365
-            ),
1366
-            admin_url('admin.php')
1367
-        );
1368
-    }
16
+	/**
17
+	 * cached value for the the logical active status for the event
18
+	 *
19
+	 * @see get_active_status()
20
+	 * @var string
21
+	 */
22
+	protected $_active_status = '';
23
+
24
+	/**
25
+	 * This is just used for caching the Primary Datetime for the Event on initial retrieval
26
+	 *
27
+	 * @var EE_Datetime
28
+	 */
29
+	protected $_Primary_Datetime;
30
+
31
+	/**
32
+	 * @var EventSpacesCalculator $available_spaces_calculator
33
+	 */
34
+	protected $available_spaces_calculator;
35
+
36
+
37
+	/**
38
+	 * @param array  $props_n_values          incoming values
39
+	 * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
40
+	 *                                        used.)
41
+	 * @param array  $date_formats            incoming date_formats in an array where the first value is the
42
+	 *                                        date_format and the second value is the time format
43
+	 * @return EE_Event
44
+	 * @throws EE_Error
45
+	 */
46
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
47
+	{
48
+		$has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
49
+		return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
50
+	}
51
+
52
+
53
+	/**
54
+	 * @param array  $props_n_values  incoming values from the database
55
+	 * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
56
+	 *                                the website will be used.
57
+	 * @return EE_Event
58
+	 * @throws EE_Error
59
+	 */
60
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
61
+	{
62
+		return new self($props_n_values, true, $timezone);
63
+	}
64
+
65
+
66
+	/**
67
+	 * @return EventSpacesCalculator
68
+	 * @throws \EE_Error
69
+	 */
70
+	public function getAvailableSpacesCalculator()
71
+	{
72
+		if (! $this->available_spaces_calculator instanceof EventSpacesCalculator) {
73
+			$this->available_spaces_calculator = new EventSpacesCalculator($this);
74
+		}
75
+		return $this->available_spaces_calculator;
76
+	}
77
+
78
+
79
+	/**
80
+	 * Overrides parent set() method so that all calls to set( 'status', $status ) can be routed to internal methods
81
+	 *
82
+	 * @param string $field_name
83
+	 * @param mixed  $field_value
84
+	 * @param bool   $use_default
85
+	 * @throws EE_Error
86
+	 */
87
+	public function set($field_name, $field_value, $use_default = false)
88
+	{
89
+		switch ($field_name) {
90
+			case 'status':
91
+				$this->set_status($field_value, $use_default);
92
+				break;
93
+			default:
94
+				parent::set($field_name, $field_value, $use_default);
95
+		}
96
+	}
97
+
98
+
99
+	/**
100
+	 *    set_status
101
+	 * Checks if event status is being changed to SOLD OUT
102
+	 * and updates event meta data with previous event status
103
+	 * so that we can revert things if/when the event is no longer sold out
104
+	 *
105
+	 * @access public
106
+	 * @param string $new_status
107
+	 * @param bool   $use_default
108
+	 * @return void
109
+	 * @throws EE_Error
110
+	 */
111
+	public function set_status($new_status = null, $use_default = false)
112
+	{
113
+		// if nothing is set, and we aren't explicitly wanting to reset the status, then just leave
114
+		if (empty($new_status) && ! $use_default) {
115
+			return;
116
+		}
117
+		// get current Event status
118
+		$old_status = $this->status();
119
+		// if status has changed
120
+		if ($old_status !== $new_status) {
121
+			// TO sold_out
122
+			if ($new_status === EEM_Event::sold_out) {
123
+				// save the previous event status so that we can revert if the event is no longer sold out
124
+				$this->add_post_meta('_previous_event_status', $old_status);
125
+				do_action('AHEE__EE_Event__set_status__to_sold_out', $this, $old_status, $new_status);
126
+				// OR FROM  sold_out
127
+			} elseif ($old_status === EEM_Event::sold_out) {
128
+				$this->delete_post_meta('_previous_event_status');
129
+				do_action('AHEE__EE_Event__set_status__from_sold_out', $this, $old_status, $new_status);
130
+			}
131
+			// clear out the active status so that it gets reset the next time it is requested
132
+			$this->_active_status = null;
133
+			// update status
134
+			parent::set('status', $new_status, $use_default);
135
+			do_action('AHEE__EE_Event__set_status__after_update', $this);
136
+			return;
137
+		}
138
+		// even though the old value matches the new value, it's still good to
139
+		// allow the parent set method to have a say
140
+		parent::set('status', $new_status, $use_default);
141
+	}
142
+
143
+
144
+	/**
145
+	 * Gets all the datetimes for this event
146
+	 *
147
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
148
+	 * @return EE_Base_Class[]|EE_Datetime[]
149
+	 * @throws EE_Error
150
+	 */
151
+	public function datetimes($query_params = array())
152
+	{
153
+		return $this->get_many_related('Datetime', $query_params);
154
+	}
155
+
156
+
157
+	/**
158
+	 * Gets all the datetimes for this event, ordered by DTT_EVT_start in ascending order
159
+	 *
160
+	 * @return EE_Base_Class[]|EE_Datetime[]
161
+	 * @throws EE_Error
162
+	 */
163
+	public function datetimes_in_chronological_order()
164
+	{
165
+		return $this->get_many_related('Datetime', array('order_by' => array('DTT_EVT_start' => 'ASC')));
166
+	}
167
+
168
+
169
+	/**
170
+	 * Gets all the datetimes for this event, ordered by the DTT_order on the datetime.
171
+	 * @darren, we should probably UNSET timezone on the EEM_Datetime model
172
+	 * after running our query, so that this timezone isn't set for EVERY query
173
+	 * on EEM_Datetime for the rest of the request, no?
174
+	 *
175
+	 * @param boolean $show_expired whether or not to include expired events
176
+	 * @param boolean $show_deleted whether or not to include deleted events
177
+	 * @param null    $limit
178
+	 * @return EE_Datetime[]
179
+	 * @throws EE_Error
180
+	 */
181
+	public function datetimes_ordered($show_expired = true, $show_deleted = false, $limit = null)
182
+	{
183
+		return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_event_ordered_by_DTT_order(
184
+			$this->ID(),
185
+			$show_expired,
186
+			$show_deleted,
187
+			$limit
188
+		);
189
+	}
190
+
191
+
192
+	/**
193
+	 * Returns one related datetime. Mostly only used by some legacy code.
194
+	 *
195
+	 * @return EE_Base_Class|EE_Datetime
196
+	 * @throws EE_Error
197
+	 */
198
+	public function first_datetime()
199
+	{
200
+		return $this->get_first_related('Datetime');
201
+	}
202
+
203
+
204
+	/**
205
+	 * Returns the 'primary' datetime for the event
206
+	 *
207
+	 * @param bool $try_to_exclude_expired
208
+	 * @param bool $try_to_exclude_deleted
209
+	 * @return EE_Datetime
210
+	 * @throws EE_Error
211
+	 */
212
+	public function primary_datetime($try_to_exclude_expired = true, $try_to_exclude_deleted = true)
213
+	{
214
+		if (! empty($this->_Primary_Datetime)) {
215
+			return $this->_Primary_Datetime;
216
+		}
217
+		$this->_Primary_Datetime = EEM_Datetime::instance($this->_timezone)->get_primary_datetime_for_event(
218
+			$this->ID(),
219
+			$try_to_exclude_expired,
220
+			$try_to_exclude_deleted
221
+		);
222
+		return $this->_Primary_Datetime;
223
+	}
224
+
225
+
226
+	/**
227
+	 * Gets all the tickets available for purchase of this event
228
+	 *
229
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
230
+	 * @return EE_Base_Class[]|EE_Ticket[]
231
+	 * @throws EE_Error
232
+	 */
233
+	public function tickets($query_params = array())
234
+	{
235
+		// first get all datetimes
236
+		$datetimes = $this->datetimes_ordered();
237
+		if (! $datetimes) {
238
+			return array();
239
+		}
240
+		$datetime_ids = array();
241
+		foreach ($datetimes as $datetime) {
242
+			$datetime_ids[] = $datetime->ID();
243
+		}
244
+		$where_params = array('Datetime.DTT_ID' => array('IN', $datetime_ids));
245
+		// if incoming $query_params has where conditions let's merge but not override existing.
246
+		if (is_array($query_params) && isset($query_params[0])) {
247
+			$where_params = array_merge($query_params[0], $where_params);
248
+			unset($query_params[0]);
249
+		}
250
+		// now add $where_params to $query_params
251
+		$query_params[0] = $where_params;
252
+		return EEM_Ticket::instance()->get_all($query_params);
253
+	}
254
+
255
+
256
+	/**
257
+	 * get all unexpired untrashed tickets
258
+	 *
259
+	 * @return EE_Ticket[]
260
+	 * @throws EE_Error
261
+	 */
262
+	public function active_tickets()
263
+	{
264
+		return $this->tickets(
265
+			array(
266
+				array(
267
+					'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')),
268
+					'TKT_deleted'  => false,
269
+				),
270
+			)
271
+		);
272
+	}
273
+
274
+
275
+	/**
276
+	 * @return bool
277
+	 * @throws EE_Error
278
+	 */
279
+	public function additional_limit()
280
+	{
281
+		return $this->get('EVT_additional_limit');
282
+	}
283
+
284
+
285
+	/**
286
+	 * @return bool
287
+	 * @throws EE_Error
288
+	 */
289
+	public function allow_overflow()
290
+	{
291
+		return $this->get('EVT_allow_overflow');
292
+	}
293
+
294
+
295
+	/**
296
+	 * @return bool
297
+	 * @throws EE_Error
298
+	 */
299
+	public function created()
300
+	{
301
+		return $this->get('EVT_created');
302
+	}
303
+
304
+
305
+	/**
306
+	 * @return bool
307
+	 * @throws EE_Error
308
+	 */
309
+	public function description()
310
+	{
311
+		return $this->get('EVT_desc');
312
+	}
313
+
314
+
315
+	/**
316
+	 * Runs do_shortcode and wpautop on the description
317
+	 *
318
+	 * @return string of html
319
+	 * @throws EE_Error
320
+	 */
321
+	public function description_filtered()
322
+	{
323
+		return $this->get_pretty('EVT_desc');
324
+	}
325
+
326
+
327
+	/**
328
+	 * @return bool
329
+	 * @throws EE_Error
330
+	 */
331
+	public function display_description()
332
+	{
333
+		return $this->get('EVT_display_desc');
334
+	}
335
+
336
+
337
+	/**
338
+	 * @return bool
339
+	 * @throws EE_Error
340
+	 */
341
+	public function display_ticket_selector()
342
+	{
343
+		return (bool) $this->get('EVT_display_ticket_selector');
344
+	}
345
+
346
+
347
+	/**
348
+	 * @return bool
349
+	 * @throws EE_Error
350
+	 */
351
+	public function external_url()
352
+	{
353
+		return $this->get('EVT_external_URL');
354
+	}
355
+
356
+
357
+	/**
358
+	 * @return bool
359
+	 * @throws EE_Error
360
+	 */
361
+	public function member_only()
362
+	{
363
+		return $this->get('EVT_member_only');
364
+	}
365
+
366
+
367
+	/**
368
+	 * @return bool
369
+	 * @throws EE_Error
370
+	 */
371
+	public function phone()
372
+	{
373
+		return $this->get('EVT_phone');
374
+	}
375
+
376
+
377
+	/**
378
+	 * @return bool
379
+	 * @throws EE_Error
380
+	 */
381
+	public function modified()
382
+	{
383
+		return $this->get('EVT_modified');
384
+	}
385
+
386
+
387
+	/**
388
+	 * @return bool
389
+	 * @throws EE_Error
390
+	 */
391
+	public function name()
392
+	{
393
+		return $this->get('EVT_name');
394
+	}
395
+
396
+
397
+	/**
398
+	 * @return bool
399
+	 * @throws EE_Error
400
+	 */
401
+	public function order()
402
+	{
403
+		return $this->get('EVT_order');
404
+	}
405
+
406
+
407
+	/**
408
+	 * @return bool|string
409
+	 * @throws EE_Error
410
+	 */
411
+	public function default_registration_status()
412
+	{
413
+		$event_default_registration_status = $this->get('EVT_default_registration_status');
414
+		return ! empty($event_default_registration_status)
415
+			? $event_default_registration_status
416
+			: EE_Registry::instance()->CFG->registration->default_STS_ID;
417
+	}
418
+
419
+
420
+	/**
421
+	 * @param int  $num_words
422
+	 * @param null $more
423
+	 * @param bool $not_full_desc
424
+	 * @return bool|string
425
+	 * @throws EE_Error
426
+	 */
427
+	public function short_description($num_words = 55, $more = null, $not_full_desc = false)
428
+	{
429
+		$short_desc = $this->get('EVT_short_desc');
430
+		if (! empty($short_desc) || $not_full_desc) {
431
+			return $short_desc;
432
+		}
433
+		$full_desc = $this->get('EVT_desc');
434
+		return wp_trim_words($full_desc, $num_words, $more);
435
+	}
436
+
437
+
438
+	/**
439
+	 * @return bool
440
+	 * @throws EE_Error
441
+	 */
442
+	public function slug()
443
+	{
444
+		return $this->get('EVT_slug');
445
+	}
446
+
447
+
448
+	/**
449
+	 * @return bool
450
+	 * @throws EE_Error
451
+	 */
452
+	public function timezone_string()
453
+	{
454
+		return $this->get('EVT_timezone_string');
455
+	}
456
+
457
+
458
+	/**
459
+	 * @return bool
460
+	 * @throws EE_Error
461
+	 */
462
+	public function visible_on()
463
+	{
464
+		return $this->get('EVT_visible_on');
465
+	}
466
+
467
+
468
+	/**
469
+	 * @return int
470
+	 * @throws EE_Error
471
+	 */
472
+	public function wp_user()
473
+	{
474
+		return $this->get('EVT_wp_user');
475
+	}
476
+
477
+
478
+	/**
479
+	 * @return bool
480
+	 * @throws EE_Error
481
+	 */
482
+	public function donations()
483
+	{
484
+		return $this->get('EVT_donations');
485
+	}
486
+
487
+
488
+	/**
489
+	 * @param $limit
490
+	 * @throws EE_Error
491
+	 */
492
+	public function set_additional_limit($limit)
493
+	{
494
+		$this->set('EVT_additional_limit', $limit);
495
+	}
496
+
497
+
498
+	/**
499
+	 * @param $created
500
+	 * @throws EE_Error
501
+	 */
502
+	public function set_created($created)
503
+	{
504
+		$this->set('EVT_created', $created);
505
+	}
506
+
507
+
508
+	/**
509
+	 * @param $desc
510
+	 * @throws EE_Error
511
+	 */
512
+	public function set_description($desc)
513
+	{
514
+		$this->set('EVT_desc', $desc);
515
+	}
516
+
517
+
518
+	/**
519
+	 * @param $display_desc
520
+	 * @throws EE_Error
521
+	 */
522
+	public function set_display_description($display_desc)
523
+	{
524
+		$this->set('EVT_display_desc', $display_desc);
525
+	}
526
+
527
+
528
+	/**
529
+	 * @param $display_ticket_selector
530
+	 * @throws EE_Error
531
+	 */
532
+	public function set_display_ticket_selector($display_ticket_selector)
533
+	{
534
+		$this->set('EVT_display_ticket_selector', $display_ticket_selector);
535
+	}
536
+
537
+
538
+	/**
539
+	 * @param $external_url
540
+	 * @throws EE_Error
541
+	 */
542
+	public function set_external_url($external_url)
543
+	{
544
+		$this->set('EVT_external_URL', $external_url);
545
+	}
546
+
547
+
548
+	/**
549
+	 * @param $member_only
550
+	 * @throws EE_Error
551
+	 */
552
+	public function set_member_only($member_only)
553
+	{
554
+		$this->set('EVT_member_only', $member_only);
555
+	}
556
+
557
+
558
+	/**
559
+	 * @param $event_phone
560
+	 * @throws EE_Error
561
+	 */
562
+	public function set_event_phone($event_phone)
563
+	{
564
+		$this->set('EVT_phone', $event_phone);
565
+	}
566
+
567
+
568
+	/**
569
+	 * @param $modified
570
+	 * @throws EE_Error
571
+	 */
572
+	public function set_modified($modified)
573
+	{
574
+		$this->set('EVT_modified', $modified);
575
+	}
576
+
577
+
578
+	/**
579
+	 * @param $name
580
+	 * @throws EE_Error
581
+	 */
582
+	public function set_name($name)
583
+	{
584
+		$this->set('EVT_name', $name);
585
+	}
586
+
587
+
588
+	/**
589
+	 * @param $order
590
+	 * @throws EE_Error
591
+	 */
592
+	public function set_order($order)
593
+	{
594
+		$this->set('EVT_order', $order);
595
+	}
596
+
597
+
598
+	/**
599
+	 * @param $short_desc
600
+	 * @throws EE_Error
601
+	 */
602
+	public function set_short_description($short_desc)
603
+	{
604
+		$this->set('EVT_short_desc', $short_desc);
605
+	}
606
+
607
+
608
+	/**
609
+	 * @param $slug
610
+	 * @throws EE_Error
611
+	 */
612
+	public function set_slug($slug)
613
+	{
614
+		$this->set('EVT_slug', $slug);
615
+	}
616
+
617
+
618
+	/**
619
+	 * @param $timezone_string
620
+	 * @throws EE_Error
621
+	 */
622
+	public function set_timezone_string($timezone_string)
623
+	{
624
+		$this->set('EVT_timezone_string', $timezone_string);
625
+	}
626
+
627
+
628
+	/**
629
+	 * @param $visible_on
630
+	 * @throws EE_Error
631
+	 */
632
+	public function set_visible_on($visible_on)
633
+	{
634
+		$this->set('EVT_visible_on', $visible_on);
635
+	}
636
+
637
+
638
+	/**
639
+	 * @param $wp_user
640
+	 * @throws EE_Error
641
+	 */
642
+	public function set_wp_user($wp_user)
643
+	{
644
+		$this->set('EVT_wp_user', $wp_user);
645
+	}
646
+
647
+
648
+	/**
649
+	 * @param $default_registration_status
650
+	 * @throws EE_Error
651
+	 */
652
+	public function set_default_registration_status($default_registration_status)
653
+	{
654
+		$this->set('EVT_default_registration_status', $default_registration_status);
655
+	}
656
+
657
+
658
+	/**
659
+	 * @param $donations
660
+	 * @throws EE_Error
661
+	 */
662
+	public function set_donations($donations)
663
+	{
664
+		$this->set('EVT_donations', $donations);
665
+	}
666
+
667
+
668
+	/**
669
+	 * Adds a venue to this event
670
+	 *
671
+	 * @param EE_Venue /int $venue_id_or_obj
672
+	 * @return EE_Base_Class|EE_Venue
673
+	 * @throws EE_Error
674
+	 */
675
+	public function add_venue($venue_id_or_obj)
676
+	{
677
+		return $this->_add_relation_to($venue_id_or_obj, 'Venue');
678
+	}
679
+
680
+
681
+	/**
682
+	 * Removes a venue from the event
683
+	 *
684
+	 * @param EE_Venue /int $venue_id_or_obj
685
+	 * @return EE_Base_Class|EE_Venue
686
+	 * @throws EE_Error
687
+	 */
688
+	public function remove_venue($venue_id_or_obj)
689
+	{
690
+		return $this->_remove_relation_to($venue_id_or_obj, 'Venue');
691
+	}
692
+
693
+
694
+	/**
695
+	 * Gets all the venues related ot the event. May provide additional $query_params if desired
696
+	 *
697
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
698
+	 * @return EE_Base_Class[]|EE_Venue[]
699
+	 * @throws EE_Error
700
+	 */
701
+	public function venues($query_params = array())
702
+	{
703
+		return $this->get_many_related('Venue', $query_params);
704
+	}
705
+
706
+
707
+	/**
708
+	 * check if event id is present and if event is published
709
+	 *
710
+	 * @access public
711
+	 * @return boolean true yes, false no
712
+	 * @throws EE_Error
713
+	 */
714
+	private function _has_ID_and_is_published()
715
+	{
716
+		// first check if event id is present and not NULL,
717
+		// then check if this event is published (or any of the equivalent "published" statuses)
718
+		return
719
+			$this->ID() && $this->ID() !== null
720
+			&& (
721
+				$this->status() === 'publish'
722
+				|| $this->status() === EEM_Event::sold_out
723
+				|| $this->status() === EEM_Event::postponed
724
+				|| $this->status() === EEM_Event::cancelled
725
+			);
726
+	}
727
+
728
+
729
+	/**
730
+	 * This simply compares the internal dates with NOW and determines if the event is upcoming or not.
731
+	 *
732
+	 * @access public
733
+	 * @return boolean true yes, false no
734
+	 * @throws EE_Error
735
+	 */
736
+	public function is_upcoming()
737
+	{
738
+		// check if event id is present and if this event is published
739
+		if ($this->is_inactive()) {
740
+			return false;
741
+		}
742
+		// set initial value
743
+		$upcoming = false;
744
+		// next let's get all datetimes and loop through them
745
+		$datetimes = $this->datetimes_in_chronological_order();
746
+		foreach ($datetimes as $datetime) {
747
+			if ($datetime instanceof EE_Datetime) {
748
+				// if this dtt is expired then we continue cause one of the other datetimes might be upcoming.
749
+				if ($datetime->is_expired()) {
750
+					continue;
751
+				}
752
+				// if this dtt is active then we return false.
753
+				if ($datetime->is_active()) {
754
+					return false;
755
+				}
756
+				// otherwise let's check upcoming status
757
+				$upcoming = $datetime->is_upcoming();
758
+			}
759
+		}
760
+		return $upcoming;
761
+	}
762
+
763
+
764
+	/**
765
+	 * @return bool
766
+	 * @throws EE_Error
767
+	 */
768
+	public function is_active()
769
+	{
770
+		// check if event id is present and if this event is published
771
+		if ($this->is_inactive()) {
772
+			return false;
773
+		}
774
+		// set initial value
775
+		$active = false;
776
+		// next let's get all datetimes and loop through them
777
+		$datetimes = $this->datetimes_in_chronological_order();
778
+		foreach ($datetimes as $datetime) {
779
+			if ($datetime instanceof EE_Datetime) {
780
+				// if this dtt is expired then we continue cause one of the other datetimes might be active.
781
+				if ($datetime->is_expired()) {
782
+					continue;
783
+				}
784
+				// if this dtt is upcoming then we return false.
785
+				if ($datetime->is_upcoming()) {
786
+					return false;
787
+				}
788
+				// otherwise let's check active status
789
+				$active = $datetime->is_active();
790
+			}
791
+		}
792
+		return $active;
793
+	}
794
+
795
+
796
+	/**
797
+	 * @return bool
798
+	 * @throws EE_Error
799
+	 */
800
+	public function is_expired()
801
+	{
802
+		// check if event id is present and if this event is published
803
+		if ($this->is_inactive()) {
804
+			return false;
805
+		}
806
+		// set initial value
807
+		$expired = false;
808
+		// first let's get all datetimes and loop through them
809
+		$datetimes = $this->datetimes_in_chronological_order();
810
+		foreach ($datetimes as $datetime) {
811
+			if ($datetime instanceof EE_Datetime) {
812
+				// if this dtt is upcoming or active then we return false.
813
+				if ($datetime->is_upcoming() || $datetime->is_active()) {
814
+					return false;
815
+				}
816
+				// otherwise let's check active status
817
+				$expired = $datetime->is_expired();
818
+			}
819
+		}
820
+		return $expired;
821
+	}
822
+
823
+
824
+	/**
825
+	 * @return bool
826
+	 * @throws EE_Error
827
+	 */
828
+	public function is_inactive()
829
+	{
830
+		// check if event id is present and if this event is published
831
+		if ($this->_has_ID_and_is_published()) {
832
+			return false;
833
+		}
834
+		return true;
835
+	}
836
+
837
+
838
+	/**
839
+	 * calculate spaces remaining based on "saleable" tickets
840
+	 *
841
+	 * @param array $tickets
842
+	 * @param bool  $filtered
843
+	 * @return int|float
844
+	 * @throws EE_Error
845
+	 * @throws DomainException
846
+	 * @throws UnexpectedEntityException
847
+	 */
848
+	public function spaces_remaining($tickets = array(), $filtered = true)
849
+	{
850
+		$this->getAvailableSpacesCalculator()->setActiveTickets($tickets);
851
+		$spaces_remaining = $this->getAvailableSpacesCalculator()->spacesRemaining();
852
+		return $filtered
853
+			? apply_filters(
854
+				'FHEE_EE_Event__spaces_remaining',
855
+				$spaces_remaining,
856
+				$this,
857
+				$tickets
858
+			)
859
+			: $spaces_remaining;
860
+	}
861
+
862
+
863
+	/**
864
+	 *    perform_sold_out_status_check
865
+	 *    checks all of this events's datetime  reg_limit - sold values to determine if ANY datetimes have spaces
866
+	 *    available... if NOT, then the event status will get toggled to 'sold_out'
867
+	 *
868
+	 * @return bool    return the ACTUAL sold out state.
869
+	 * @throws EE_Error
870
+	 * @throws DomainException
871
+	 * @throws UnexpectedEntityException
872
+	 */
873
+	public function perform_sold_out_status_check()
874
+	{
875
+		// get all unexpired untrashed tickets
876
+		$tickets = $this->tickets(
877
+			array(
878
+				array('TKT_deleted' => false),
879
+				'order_by' => array('TKT_qty' => 'ASC'),
880
+			)
881
+		);
882
+		$all_expired = true;
883
+		foreach ($tickets as $ticket) {
884
+			if (! $ticket->is_expired()) {
885
+				$all_expired = false;
886
+				break;
887
+			}
888
+		}
889
+		// if all the tickets are just expired, then don't update the event status to sold out
890
+		if ($all_expired) {
891
+			return true;
892
+		}
893
+		$spaces_remaining = $this->spaces_remaining($tickets);
894
+		if ($spaces_remaining < 1) {
895
+			if ($this->status() !== EEM_Event::post_status_private) {
896
+				$this->set_status(EEM_Event::sold_out);
897
+				$this->save();
898
+			}
899
+			$sold_out = true;
900
+		} else {
901
+			$sold_out = false;
902
+			// was event previously marked as sold out ?
903
+			if ($this->status() === EEM_Event::sold_out) {
904
+				// revert status to previous value, if it was set
905
+				$previous_event_status = $this->get_post_meta('_previous_event_status', true);
906
+				if ($previous_event_status) {
907
+					$this->set_status($previous_event_status);
908
+					$this->save();
909
+				}
910
+			}
911
+		}
912
+		do_action('AHEE__EE_Event__perform_sold_out_status_check__end', $this, $sold_out, $spaces_remaining, $tickets);
913
+		return $sold_out;
914
+	}
915
+
916
+
917
+	/**
918
+	 * This returns the total remaining spaces for sale on this event.
919
+	 *
920
+	 * @uses EE_Event::total_available_spaces()
921
+	 * @return float|int
922
+	 * @throws EE_Error
923
+	 * @throws DomainException
924
+	 * @throws UnexpectedEntityException
925
+	 */
926
+	public function spaces_remaining_for_sale()
927
+	{
928
+		return $this->total_available_spaces(true);
929
+	}
930
+
931
+
932
+	/**
933
+	 * This returns the total spaces available for an event
934
+	 * while considering all the qtys on the tickets and the reg limits
935
+	 * on the datetimes attached to this event.
936
+	 *
937
+	 * @param   bool $consider_sold Whether to consider any tickets that have already sold in our calculation.
938
+	 *                              If this is false, then we return the most tickets that could ever be sold
939
+	 *                              for this event with the datetime and tickets setup on the event under optimal
940
+	 *                              selling conditions.  Otherwise we return a live calculation of spaces available
941
+	 *                              based on tickets sold.  Depending on setup and stage of sales, this
942
+	 *                              may appear to equal remaining tickets.  However, the more tickets are
943
+	 *                              sold out, the more accurate the "live" total is.
944
+	 * @return float|int
945
+	 * @throws EE_Error
946
+	 * @throws DomainException
947
+	 * @throws UnexpectedEntityException
948
+	 */
949
+	public function total_available_spaces($consider_sold = false)
950
+	{
951
+		$spaces_available = $consider_sold
952
+			? $this->getAvailableSpacesCalculator()->spacesRemaining()
953
+			: $this->getAvailableSpacesCalculator()->totalSpacesAvailable();
954
+		return apply_filters(
955
+			'FHEE_EE_Event__total_available_spaces__spaces_available',
956
+			$spaces_available,
957
+			$this,
958
+			$this->getAvailableSpacesCalculator()->getDatetimes(),
959
+			$this->getAvailableSpacesCalculator()->getActiveTickets()
960
+		);
961
+	}
962
+
963
+
964
+	/**
965
+	 * Checks if the event is set to sold out
966
+	 *
967
+	 * @param  bool $actual whether or not to perform calculations to not only figure the
968
+	 *                      actual status but also to flip the status if necessary to sold
969
+	 *                      out If false, we just check the existing status of the event
970
+	 * @return boolean
971
+	 * @throws EE_Error
972
+	 */
973
+	public function is_sold_out($actual = false)
974
+	{
975
+		if (! $actual) {
976
+			return $this->status() === EEM_Event::sold_out;
977
+		}
978
+		return $this->perform_sold_out_status_check();
979
+	}
980
+
981
+
982
+	/**
983
+	 * Checks if the event is marked as postponed
984
+	 *
985
+	 * @return boolean
986
+	 */
987
+	public function is_postponed()
988
+	{
989
+		return $this->status() === EEM_Event::postponed;
990
+	}
991
+
992
+
993
+	/**
994
+	 * Checks if the event is marked as cancelled
995
+	 *
996
+	 * @return boolean
997
+	 */
998
+	public function is_cancelled()
999
+	{
1000
+		return $this->status() === EEM_Event::cancelled;
1001
+	}
1002
+
1003
+
1004
+	/**
1005
+	 * Get the logical active status in a hierarchical order for all the datetimes.  Note
1006
+	 * Basically, we order the datetimes by EVT_start_date.  Then first test on whether the event is published.  If its
1007
+	 * NOT published then we test for whether its expired or not.  IF it IS published then we test first on whether an
1008
+	 * event has any active dates.  If no active dates then we check for any upcoming dates.  If no upcoming dates then
1009
+	 * the event is considered expired.
1010
+	 * NOTE: this method does NOT calculate whether the datetimes are sold out when event is published.  Sold Out is a
1011
+	 * status set on the EVENT when it is not published and thus is done
1012
+	 *
1013
+	 * @param bool $reset
1014
+	 * @return bool | string - based on EE_Datetime active constants or FALSE if error.
1015
+	 * @throws EE_Error
1016
+	 */
1017
+	public function get_active_status($reset = false)
1018
+	{
1019
+		// if the active status has already been set, then just use that value (unless we are resetting it)
1020
+		if (! empty($this->_active_status) && ! $reset) {
1021
+			return $this->_active_status;
1022
+		}
1023
+		// first check if event id is present on this object
1024
+		if (! $this->ID()) {
1025
+			return false;
1026
+		}
1027
+		$where_params_for_event = array(array('EVT_ID' => $this->ID()));
1028
+		// if event is published:
1029
+		if ($this->status() === EEM_Event::post_status_publish || $this->status() === EEM_Event::post_status_private) {
1030
+			// active?
1031
+			if (EEM_Datetime::instance()->get_datetime_count_for_status(
1032
+				EE_Datetime::active,
1033
+				$where_params_for_event
1034
+			) > 0) {
1035
+				$this->_active_status = EE_Datetime::active;
1036
+			} else {
1037
+				// upcoming?
1038
+				if (EEM_Datetime::instance()->get_datetime_count_for_status(
1039
+					EE_Datetime::upcoming,
1040
+					$where_params_for_event
1041
+				) > 0) {
1042
+					$this->_active_status = EE_Datetime::upcoming;
1043
+				} else {
1044
+					// expired?
1045
+					if (EEM_Datetime::instance()->get_datetime_count_for_status(
1046
+						EE_Datetime::expired,
1047
+						$where_params_for_event
1048
+					) > 0
1049
+					) {
1050
+						$this->_active_status = EE_Datetime::expired;
1051
+					} else {
1052
+						// it would be odd if things make it this far because it basically means there are no datetime's
1053
+						// attached to the event.  So in this case it will just be considered inactive.
1054
+						$this->_active_status = EE_Datetime::inactive;
1055
+					}
1056
+				}
1057
+			}
1058
+		} else {
1059
+			// the event is not published, so let's just set it's active status according to its' post status
1060
+			switch ($this->status()) {
1061
+				case EEM_Event::sold_out:
1062
+					$this->_active_status = EE_Datetime::sold_out;
1063
+					break;
1064
+				case EEM_Event::cancelled:
1065
+					$this->_active_status = EE_Datetime::cancelled;
1066
+					break;
1067
+				case EEM_Event::postponed:
1068
+					$this->_active_status = EE_Datetime::postponed;
1069
+					break;
1070
+				default:
1071
+					$this->_active_status = EE_Datetime::inactive;
1072
+			}
1073
+		}
1074
+		return $this->_active_status;
1075
+	}
1076
+
1077
+
1078
+	/**
1079
+	 *    pretty_active_status
1080
+	 *
1081
+	 * @access public
1082
+	 * @param boolean $echo whether to return (FALSE), or echo out the result (TRUE)
1083
+	 * @return mixed void|string
1084
+	 * @throws EE_Error
1085
+	 */
1086
+	public function pretty_active_status($echo = true)
1087
+	{
1088
+		$active_status = $this->get_active_status();
1089
+		$status = '<span class="ee-status event-active-status-'
1090
+				  . $active_status
1091
+				  . '">'
1092
+				  . EEH_Template::pretty_status($active_status, false, 'sentence')
1093
+				  . '</span>';
1094
+		if ($echo) {
1095
+			echo $status;
1096
+			return '';
1097
+		}
1098
+		return $status;
1099
+	}
1100
+
1101
+
1102
+	/**
1103
+	 * @return bool|int
1104
+	 * @throws EE_Error
1105
+	 */
1106
+	public function get_number_of_tickets_sold()
1107
+	{
1108
+		$tkt_sold = 0;
1109
+		if (! $this->ID()) {
1110
+			return 0;
1111
+		}
1112
+		$datetimes = $this->datetimes();
1113
+		foreach ($datetimes as $datetime) {
1114
+			if ($datetime instanceof EE_Datetime) {
1115
+				$tkt_sold += $datetime->sold();
1116
+			}
1117
+		}
1118
+		return $tkt_sold;
1119
+	}
1120
+
1121
+
1122
+	/**
1123
+	 * This just returns a count of all the registrations for this event
1124
+	 *
1125
+	 * @access  public
1126
+	 * @return int
1127
+	 * @throws EE_Error
1128
+	 */
1129
+	public function get_count_of_all_registrations()
1130
+	{
1131
+		return EEM_Event::instance()->count_related($this, 'Registration');
1132
+	}
1133
+
1134
+
1135
+	/**
1136
+	 * This returns the ticket with the earliest start time that is
1137
+	 * available for this event (across all datetimes attached to the event)
1138
+	 *
1139
+	 * @return EE_Base_Class|EE_Ticket|null
1140
+	 * @throws EE_Error
1141
+	 */
1142
+	public function get_ticket_with_earliest_start_time()
1143
+	{
1144
+		$where['Datetime.EVT_ID'] = $this->ID();
1145
+		$query_params = array($where, 'order_by' => array('TKT_start_date' => 'ASC'));
1146
+		return EE_Registry::instance()->load_model('Ticket')->get_one($query_params);
1147
+	}
1148
+
1149
+
1150
+	/**
1151
+	 * This returns the ticket with the latest end time that is available
1152
+	 * for this event (across all datetimes attached to the event)
1153
+	 *
1154
+	 * @return EE_Base_Class|EE_Ticket|null
1155
+	 * @throws EE_Error
1156
+	 */
1157
+	public function get_ticket_with_latest_end_time()
1158
+	{
1159
+		$where['Datetime.EVT_ID'] = $this->ID();
1160
+		$query_params = array($where, 'order_by' => array('TKT_end_date' => 'DESC'));
1161
+		return EE_Registry::instance()->load_model('Ticket')->get_one($query_params);
1162
+	}
1163
+
1164
+
1165
+	/**
1166
+	 * This returns the number of different ticket types currently on sale for this event.
1167
+	 *
1168
+	 * @return int
1169
+	 * @throws EE_Error
1170
+	 */
1171
+	public function countTicketsOnSale()
1172
+	{
1173
+		$where = array(
1174
+			'Datetime.EVT_ID' => $this->ID(),
1175
+			'TKT_start_date'  => array('<', time()),
1176
+			'TKT_end_date'    => array('>', time()),
1177
+		);
1178
+		return EEM_Ticket::instance()->count(array($where));
1179
+	}
1180
+
1181
+
1182
+	/**
1183
+	 * This returns whether there are any tickets on sale for this event.
1184
+	 *
1185
+	 * @return bool true = YES tickets on sale.
1186
+	 * @throws EE_Error
1187
+	 */
1188
+	public function tickets_on_sale()
1189
+	{
1190
+		return $this->countTicketsOnSale() > 0;
1191
+	}
1192
+
1193
+
1194
+	/**
1195
+	 * Gets the URL for viewing this event on the front-end. Overrides parent
1196
+	 * to check for an external URL first
1197
+	 *
1198
+	 * @return string
1199
+	 * @throws EE_Error
1200
+	 */
1201
+	public function get_permalink()
1202
+	{
1203
+		if ($this->external_url()) {
1204
+			return $this->external_url();
1205
+		}
1206
+		return parent::get_permalink();
1207
+	}
1208
+
1209
+
1210
+	/**
1211
+	 * Gets the first term for 'espresso_event_categories' we can find
1212
+	 *
1213
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1214
+	 * @return EE_Base_Class|EE_Term|null
1215
+	 * @throws EE_Error
1216
+	 */
1217
+	public function first_event_category($query_params = array())
1218
+	{
1219
+		$query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
1220
+		$query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $this->ID();
1221
+		return EEM_Term::instance()->get_one($query_params);
1222
+	}
1223
+
1224
+
1225
+	/**
1226
+	 * Gets all terms for 'espresso_event_categories' we can find
1227
+	 *
1228
+	 * @param array $query_params
1229
+	 * @return EE_Base_Class[]|EE_Term[]
1230
+	 * @throws EE_Error
1231
+	 */
1232
+	public function get_all_event_categories($query_params = array())
1233
+	{
1234
+		$query_params[0]['Term_Taxonomy.taxonomy'] = 'espresso_event_categories';
1235
+		$query_params[0]['Term_Taxonomy.Event.EVT_ID'] = $this->ID();
1236
+		return EEM_Term::instance()->get_all($query_params);
1237
+	}
1238
+
1239
+
1240
+	/**
1241
+	 * Adds a question group to this event
1242
+	 *
1243
+	 * @param EE_Question_Group|int $question_group_id_or_obj
1244
+	 * @param bool                  $for_primary if true, the question group will be added for the primary
1245
+	 *                                           registrant, if false will be added for others. default: false
1246
+	 * @return EE_Base_Class|EE_Question_Group
1247
+	 * @throws EE_Error
1248
+	 */
1249
+	public function add_question_group($question_group_id_or_obj, $for_primary = false)
1250
+	{
1251
+		$extra = $for_primary
1252
+			? array('EQG_primary' => 1)
1253
+			: array();
1254
+		return $this->_add_relation_to($question_group_id_or_obj, 'Question_Group', $extra);
1255
+	}
1256
+
1257
+
1258
+	/**
1259
+	 * Removes a question group from the event
1260
+	 *
1261
+	 * @param EE_Question_Group|int $question_group_id_or_obj
1262
+	 * @param bool                  $for_primary if true, the question group will be removed from the primary
1263
+	 *                                           registrant, if false will be removed from others. default: false
1264
+	 * @return EE_Base_Class|EE_Question_Group
1265
+	 * @throws EE_Error
1266
+	 */
1267
+	public function remove_question_group($question_group_id_or_obj, $for_primary = false)
1268
+	{
1269
+		$where = $for_primary
1270
+			? array('EQG_primary' => 1)
1271
+			: array();
1272
+		return $this->_remove_relation_to($question_group_id_or_obj, 'Question_Group', $where);
1273
+	}
1274
+
1275
+
1276
+	/**
1277
+	 * Gets all the question groups, ordering them by QSG_order ascending
1278
+	 *
1279
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1280
+	 * @return EE_Base_Class[]|EE_Question_Group[]
1281
+	 * @throws EE_Error
1282
+	 */
1283
+	public function question_groups($query_params = array())
1284
+	{
1285
+		$query_params = ! empty($query_params) ? $query_params : array('order_by' => array('QSG_order' => 'ASC'));
1286
+		return $this->get_many_related('Question_Group', $query_params);
1287
+	}
1288
+
1289
+
1290
+	/**
1291
+	 * Implementation for EEI_Has_Icon interface method.
1292
+	 *
1293
+	 * @see EEI_Visual_Representation for comments
1294
+	 * @return string
1295
+	 */
1296
+	public function get_icon()
1297
+	{
1298
+		return '<span class="dashicons dashicons-flag"></span>';
1299
+	}
1300
+
1301
+
1302
+	/**
1303
+	 * Implementation for EEI_Admin_Links interface method.
1304
+	 *
1305
+	 * @see EEI_Admin_Links for comments
1306
+	 * @return string
1307
+	 * @throws EE_Error
1308
+	 */
1309
+	public function get_admin_details_link()
1310
+	{
1311
+		return $this->get_admin_edit_link();
1312
+	}
1313
+
1314
+
1315
+	/**
1316
+	 * Implementation for EEI_Admin_Links interface method.
1317
+	 *
1318
+	 * @see EEI_Admin_Links for comments
1319
+	 * @return string
1320
+	 * @throws EE_Error
1321
+	 */
1322
+	public function get_admin_edit_link()
1323
+	{
1324
+		return EEH_URL::add_query_args_and_nonce(
1325
+			array(
1326
+				'page'   => 'espresso_events',
1327
+				'action' => 'edit',
1328
+				'post'   => $this->ID(),
1329
+			),
1330
+			admin_url('admin.php')
1331
+		);
1332
+	}
1333
+
1334
+
1335
+	/**
1336
+	 * Implementation for EEI_Admin_Links interface method.
1337
+	 *
1338
+	 * @see EEI_Admin_Links for comments
1339
+	 * @return string
1340
+	 */
1341
+	public function get_admin_settings_link()
1342
+	{
1343
+		return EEH_URL::add_query_args_and_nonce(
1344
+			array(
1345
+				'page'   => 'espresso_events',
1346
+				'action' => 'default_event_settings',
1347
+			),
1348
+			admin_url('admin.php')
1349
+		);
1350
+	}
1351
+
1352
+
1353
+	/**
1354
+	 * Implementation for EEI_Admin_Links interface method.
1355
+	 *
1356
+	 * @see EEI_Admin_Links for comments
1357
+	 * @return string
1358
+	 */
1359
+	public function get_admin_overview_link()
1360
+	{
1361
+		return EEH_URL::add_query_args_and_nonce(
1362
+			array(
1363
+				'page'   => 'espresso_events',
1364
+				'action' => 'default',
1365
+			),
1366
+			admin_url('admin.php')
1367
+		);
1368
+	}
1369 1369
 }
Please login to merge, or discard this patch.
core/EE_Config.core.php 2 patches
Spacing   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -145,7 +145,7 @@  discard block
 block discarded – undo
145 145
     public static function instance()
146 146
     {
147 147
         // check if class object is instantiated, and instantiated properly
148
-        if (! self::$_instance instanceof EE_Config) {
148
+        if ( ! self::$_instance instanceof EE_Config) {
149 149
             self::$_instance = new self();
150 150
         }
151 151
         return self::$_instance;
@@ -283,7 +283,7 @@  discard block
 block discarded – undo
283 283
                 $this
284 284
             );
285 285
             if (is_object($settings) && property_exists($this, $config)) {
286
-                $this->{$config} = apply_filters('FHEE__EE_Config___load_core_config__' . $config, $settings);
286
+                $this->{$config} = apply_filters('FHEE__EE_Config___load_core_config__'.$config, $settings);
287 287
                 // call configs populate method to ensure any defaults are set for empty values.
288 288
                 if (method_exists($settings, 'populate')) {
289 289
                     $this->{$config}->populate();
@@ -556,7 +556,7 @@  discard block
 block discarded – undo
556 556
                         break;
557 557
                     // TEST #2 : check that settings section exists
558 558
                     case 2:
559
-                        if (! isset($this->{$section})) {
559
+                        if ( ! isset($this->{$section})) {
560 560
                             if ($display_errors) {
561 561
                                 throw new EE_Error(
562 562
                                     sprintf(
@@ -570,7 +570,7 @@  discard block
 block discarded – undo
570 570
                         break;
571 571
                     // TEST #3 : check that section is the proper format
572 572
                     case 3:
573
-                        if (! ($this->{$section} instanceof EE_Config_Base || $this->{$section} instanceof stdClass)
573
+                        if ( ! ($this->{$section} instanceof EE_Config_Base || $this->{$section} instanceof stdClass)
574 574
                         ) {
575 575
                             if ($display_errors) {
576 576
                                 throw new EE_Error(
@@ -616,7 +616,7 @@  discard block
 block discarded – undo
616 616
                         break;
617 617
                     // TEST #6 : verify config class is accessible
618 618
                     case 6:
619
-                        if (! class_exists($config_class)) {
619
+                        if ( ! class_exists($config_class)) {
620 620
                             if ($display_errors) {
621 621
                                 throw new EE_Error(
622 622
                                     sprintf(
@@ -633,7 +633,7 @@  discard block
 block discarded – undo
633 633
                         break;
634 634
                     // TEST #7 : check that config has even been set
635 635
                     case 7:
636
-                        if (! isset($this->{$section}->{$name})) {
636
+                        if ( ! isset($this->{$section}->{$name})) {
637 637
                             if ($display_errors) {
638 638
                                 throw new EE_Error(
639 639
                                     sprintf(
@@ -651,7 +651,7 @@  discard block
 block discarded – undo
651 651
                         break;
652 652
                     // TEST #8 : check that config is the requested type
653 653
                     case 8:
654
-                        if (! $this->{$section}->{$name} instanceof $config_class) {
654
+                        if ( ! $this->{$section}->{$name} instanceof $config_class) {
655 655
                             if ($display_errors) {
656 656
                                 throw new EE_Error(
657 657
                                     sprintf(
@@ -670,7 +670,7 @@  discard block
 block discarded – undo
670 670
                         break;
671 671
                     // TEST #9 : verify config object
672 672
                     case 9:
673
-                        if (! $config_obj instanceof EE_Config_Base) {
673
+                        if ( ! $config_obj instanceof EE_Config_Base) {
674 674
                             if ($display_errors) {
675 675
                                 throw new EE_Error(
676 676
                                     sprintf(
@@ -702,7 +702,7 @@  discard block
 block discarded – undo
702 702
      */
703 703
     private function _generate_config_option_name($section = '', $name = '')
704 704
     {
705
-        return 'ee_config-' . strtolower($section . '-' . str_replace(array('EE_', 'EED_'), '', $name));
705
+        return 'ee_config-'.strtolower($section.'-'.str_replace(array('EE_', 'EED_'), '', $name));
706 706
     }
707 707
 
708 708
 
@@ -719,7 +719,7 @@  discard block
 block discarded – undo
719 719
     {
720 720
         return ! empty($config_class)
721 721
             ? $config_class
722
-            : str_replace(' ', '_', ucwords(str_replace('_', ' ', $name))) . '_Config';
722
+            : str_replace(' ', '_', ucwords(str_replace('_', ' ', $name))).'_Config';
723 723
     }
724 724
 
725 725
 
@@ -738,17 +738,17 @@  discard block
 block discarded – undo
738 738
         // ensure config class is set to something
739 739
         $config_class = $this->_set_config_class($config_class, $name);
740 740
         // run tests 1-4, 6, and 7 to verify all config params are set and valid
741
-        if (! $this->_verify_config_params($section, $name, $config_class, null, array(1, 2, 3, 4, 5, 6))) {
741
+        if ( ! $this->_verify_config_params($section, $name, $config_class, null, array(1, 2, 3, 4, 5, 6))) {
742 742
             return null;
743 743
         }
744 744
         $config_option_name = $this->_generate_config_option_name($section, $name);
745 745
         // if the config option name hasn't been added yet to the list of option names we're tracking, then do so now
746
-        if (! isset($this->_addon_option_names[ $config_option_name ])) {
747
-            $this->_addon_option_names[ $config_option_name ] = $config_class;
746
+        if ( ! isset($this->_addon_option_names[$config_option_name])) {
747
+            $this->_addon_option_names[$config_option_name] = $config_class;
748 748
             $this->update_addon_option_names();
749 749
         }
750 750
         // verify the incoming config object but suppress errors
751
-        if (! $this->_verify_config_params($section, $name, $config_class, $config_obj, array(9), false)) {
751
+        if ( ! $this->_verify_config_params($section, $name, $config_class, $config_obj, array(9), false)) {
752 752
             $config_obj = new $config_class();
753 753
         }
754 754
         if (get_option($config_option_name)) {
@@ -795,7 +795,7 @@  discard block
 block discarded – undo
795 795
         // get class name of the incoming object
796 796
         $config_class = get_class($config_obj);
797 797
         // run tests 1-5 and 9 to verify config
798
-        if (! $this->_verify_config_params(
798
+        if ( ! $this->_verify_config_params(
799 799
             $section,
800 800
             $name,
801 801
             $config_class,
@@ -807,7 +807,7 @@  discard block
 block discarded – undo
807 807
         }
808 808
         $config_option_name = $this->_generate_config_option_name($section, $name);
809 809
         // check if config object has been added to db by seeing if config option name is in $this->_addon_option_names array
810
-        if (! isset($this->_addon_option_names[ $config_option_name ])) {
810
+        if ( ! isset($this->_addon_option_names[$config_option_name])) {
811 811
             // save new config to db
812 812
             if ($this->set_config($section, $name, $config_class, $config_obj)) {
813 813
                 return true;
@@ -833,7 +833,7 @@  discard block
 block discarded – undo
833 833
                             'event_espresso'
834 834
                         ),
835 835
                         $config_class,
836
-                        'EE_Config->' . $section . '->' . $name
836
+                        'EE_Config->'.$section.'->'.$name
837 837
                     ),
838 838
                     __FILE__,
839 839
                     __FUNCTION__,
@@ -859,7 +859,7 @@  discard block
 block discarded – undo
859 859
         // ensure config class is set to something
860 860
         $config_class = $this->_set_config_class($config_class, $name);
861 861
         // run tests 1-4, 6 and 7 to verify that all params have been set
862
-        if (! $this->_verify_config_params($section, $name, $config_class, null, array(1, 2, 3, 4, 5, 6))) {
862
+        if ( ! $this->_verify_config_params($section, $name, $config_class, null, array(1, 2, 3, 4, 5, 6))) {
863 863
             return null;
864 864
         }
865 865
         // now test if the requested config object exists, but suppress errors
@@ -904,7 +904,7 @@  discard block
 block discarded – undo
904 904
         // retrieve the wp-option for this config class.
905 905
         $config_option = maybe_unserialize(get_option($config_option_name, array()));
906 906
         if (empty($config_option)) {
907
-            EE_Config::log($config_option_name . '-NOT-FOUND');
907
+            EE_Config::log($config_option_name.'-NOT-FOUND');
908 908
         }
909 909
         return $config_option;
910 910
     }
@@ -922,7 +922,7 @@  discard block
 block discarded – undo
922 922
             // copy incoming $_REQUEST and sanitize it so we can save it
923 923
             $_request = $_REQUEST;
924 924
             array_walk_recursive($_request, 'sanitize_text_field');
925
-            $config_log[ (string) microtime(true) ] = array(
925
+            $config_log[(string) microtime(true)] = array(
926 926
                 'config_name' => $config_option_name,
927 927
                 'request'     => $_request,
928 928
             );
@@ -937,7 +937,7 @@  discard block
 block discarded – undo
937 937
      */
938 938
     public static function trim_log()
939 939
     {
940
-        if (! EE_Config::logging_enabled()) {
940
+        if ( ! EE_Config::logging_enabled()) {
941 941
             return;
942 942
         }
943 943
         $config_log = maybe_unserialize(get_option(EE_Config::LOG_NAME, array()));
@@ -961,7 +961,7 @@  discard block
 block discarded – undo
961 961
     public static function get_page_for_posts()
962 962
     {
963 963
         $page_for_posts = get_option('page_for_posts');
964
-        if (! $page_for_posts) {
964
+        if ( ! $page_for_posts) {
965 965
             return 'posts';
966 966
         }
967 967
         /** @type WPDB $wpdb */
@@ -1011,20 +1011,20 @@  discard block
 block discarded – undo
1011 1011
     {
1012 1012
         // only init widgets on admin pages when not in complete maintenance, and
1013 1013
         // on frontend when not in any maintenance mode
1014
-        if (! EE_Maintenance_Mode::instance()->level()
1014
+        if ( ! EE_Maintenance_Mode::instance()->level()
1015 1015
             || (
1016 1016
                 is_admin()
1017 1017
                 && EE_Maintenance_Mode::instance()->level() !== EE_Maintenance_Mode::level_2_complete_maintenance
1018 1018
             )
1019 1019
         ) {
1020 1020
             // grab list of installed widgets
1021
-            $widgets_to_register = glob(EE_WIDGETS . '*', GLOB_ONLYDIR);
1021
+            $widgets_to_register = glob(EE_WIDGETS.'*', GLOB_ONLYDIR);
1022 1022
             // filter list of modules to register
1023 1023
             $widgets_to_register = apply_filters(
1024 1024
                 'FHEE__EE_Config__register_widgets__widgets_to_register',
1025 1025
                 $widgets_to_register
1026 1026
             );
1027
-            if (! empty($widgets_to_register)) {
1027
+            if ( ! empty($widgets_to_register)) {
1028 1028
                 // cycle thru widget folders
1029 1029
                 foreach ($widgets_to_register as $widget_path) {
1030 1030
                     // add to list of installed widget modules
@@ -1074,31 +1074,31 @@  discard block
 block discarded – undo
1074 1074
         // create classname from widget directory name
1075 1075
         $widget = str_replace(' ', '_', ucwords(str_replace('_', ' ', $widget)));
1076 1076
         // add class prefix
1077
-        $widget_class = 'EEW_' . $widget;
1077
+        $widget_class = 'EEW_'.$widget;
1078 1078
         // does the widget exist ?
1079
-        if (! is_readable($widget_path . DS . $widget_class . $widget_ext)) {
1079
+        if ( ! is_readable($widget_path.DS.$widget_class.$widget_ext)) {
1080 1080
             $msg = sprintf(
1081 1081
                 __(
1082 1082
                     'The requested %s widget file could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s',
1083 1083
                     'event_espresso'
1084 1084
                 ),
1085 1085
                 $widget_class,
1086
-                $widget_path . DS . $widget_class . $widget_ext
1086
+                $widget_path.DS.$widget_class.$widget_ext
1087 1087
             );
1088
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1088
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1089 1089
             return;
1090 1090
         }
1091 1091
         // load the widget class file
1092
-        require_once($widget_path . DS . $widget_class . $widget_ext);
1092
+        require_once($widget_path.DS.$widget_class.$widget_ext);
1093 1093
         // verify that class exists
1094
-        if (! class_exists($widget_class)) {
1094
+        if ( ! class_exists($widget_class)) {
1095 1095
             $msg = sprintf(__('The requested %s widget class does not exist.', 'event_espresso'), $widget_class);
1096
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1096
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1097 1097
             return;
1098 1098
         }
1099 1099
         register_widget($widget_class);
1100 1100
         // add to array of registered widgets
1101
-        EE_Registry::instance()->widgets->{$widget_class} = $widget_path . DS . $widget_class . $widget_ext;
1101
+        EE_Registry::instance()->widgets->{$widget_class} = $widget_path.DS.$widget_class.$widget_ext;
1102 1102
     }
1103 1103
 
1104 1104
 
@@ -1111,18 +1111,18 @@  discard block
 block discarded – undo
1111 1111
     private function _register_modules()
1112 1112
     {
1113 1113
         // grab list of installed modules
1114
-        $modules_to_register = glob(EE_MODULES . '*', GLOB_ONLYDIR);
1114
+        $modules_to_register = glob(EE_MODULES.'*', GLOB_ONLYDIR);
1115 1115
         // filter list of modules to register
1116 1116
         $modules_to_register = apply_filters(
1117 1117
             'FHEE__EE_Config__register_modules__modules_to_register',
1118 1118
             $modules_to_register
1119 1119
         );
1120
-        if (! empty($modules_to_register)) {
1120
+        if ( ! empty($modules_to_register)) {
1121 1121
             // loop through folders
1122 1122
             foreach ($modules_to_register as $module_path) {
1123 1123
                 /**TEMPORARILY EXCLUDE gateways from modules for time being**/
1124
-                if ($module_path !== EE_MODULES . 'zzz-copy-this-module-template'
1125
-                    && $module_path !== EE_MODULES . 'gateways'
1124
+                if ($module_path !== EE_MODULES.'zzz-copy-this-module-template'
1125
+                    && $module_path !== EE_MODULES.'gateways'
1126 1126
                 ) {
1127 1127
                     // add to list of installed modules
1128 1128
                     EE_Config::register_module($module_path);
@@ -1159,25 +1159,25 @@  discard block
 block discarded – undo
1159 1159
             // remove last segment
1160 1160
             array_pop($module_path);
1161 1161
             // glue it back together
1162
-            $module_path = implode(DS, $module_path) . DS;
1162
+            $module_path = implode(DS, $module_path).DS;
1163 1163
             // take first segment from file name pieces and sanitize it
1164 1164
             $module = preg_replace('/[^a-zA-Z0-9_\-]/', '', $module_file[0]);
1165 1165
             // ensure class prefix is added
1166
-            $module_class = strpos($module, 'EED_') !== 0 ? 'EED_' . $module : $module;
1166
+            $module_class = strpos($module, 'EED_') !== 0 ? 'EED_'.$module : $module;
1167 1167
         } else {
1168 1168
             // we need to generate the filename based off of the folder name
1169 1169
             // grab and sanitize module name
1170 1170
             $module = strtolower(basename($module_path));
1171 1171
             $module = preg_replace('/[^a-z0-9_\-]/', '', $module);
1172 1172
             // like trailingslashit()
1173
-            $module_path = rtrim($module_path, DS) . DS;
1173
+            $module_path = rtrim($module_path, DS).DS;
1174 1174
             // create classname from module directory name
1175 1175
             $module = str_replace(' ', '_', ucwords(str_replace('_', ' ', $module)));
1176 1176
             // add class prefix
1177
-            $module_class = 'EED_' . $module;
1177
+            $module_class = 'EED_'.$module;
1178 1178
         }
1179 1179
         // does the module exist ?
1180
-        if (! is_readable($module_path . DS . $module_class . $module_ext)) {
1180
+        if ( ! is_readable($module_path.DS.$module_class.$module_ext)) {
1181 1181
             $msg = sprintf(
1182 1182
                 __(
1183 1183
                     'The requested %s module file could not be found or is not readable due to file permissions.',
@@ -1185,19 +1185,19 @@  discard block
 block discarded – undo
1185 1185
                 ),
1186 1186
                 $module
1187 1187
             );
1188
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1188
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1189 1189
             return false;
1190 1190
         }
1191 1191
         // load the module class file
1192
-        require_once($module_path . $module_class . $module_ext);
1192
+        require_once($module_path.$module_class.$module_ext);
1193 1193
         // verify that class exists
1194
-        if (! class_exists($module_class)) {
1194
+        if ( ! class_exists($module_class)) {
1195 1195
             $msg = sprintf(__('The requested %s module class does not exist.', 'event_espresso'), $module_class);
1196
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1196
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1197 1197
             return false;
1198 1198
         }
1199 1199
         // add to array of registered modules
1200
-        EE_Registry::instance()->modules->{$module_class} = $module_path . $module_class . $module_ext;
1200
+        EE_Registry::instance()->modules->{$module_class} = $module_path.$module_class.$module_ext;
1201 1201
         do_action(
1202 1202
             'AHEE__EE_Config__register_module__complete',
1203 1203
             $module_class,
@@ -1248,26 +1248,26 @@  discard block
 block discarded – undo
1248 1248
     {
1249 1249
         do_action('AHEE__EE_Config__register_route__begin', $route, $module, $method_name);
1250 1250
         $module = str_replace('EED_', '', $module);
1251
-        $module_class = 'EED_' . $module;
1252
-        if (! isset(EE_Registry::instance()->modules->{$module_class})) {
1251
+        $module_class = 'EED_'.$module;
1252
+        if ( ! isset(EE_Registry::instance()->modules->{$module_class})) {
1253 1253
             $msg = sprintf(__('The module %s has not been registered.', 'event_espresso'), $module);
1254
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1254
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1255 1255
             return false;
1256 1256
         }
1257 1257
         if (empty($route)) {
1258 1258
             $msg = sprintf(__('No route has been supplied.', 'event_espresso'), $route);
1259
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1259
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1260 1260
             return false;
1261 1261
         }
1262
-        if (! method_exists('EED_' . $module, $method_name)) {
1262
+        if ( ! method_exists('EED_'.$module, $method_name)) {
1263 1263
             $msg = sprintf(
1264 1264
                 __('A valid class method for the %s route has not been supplied.', 'event_espresso'),
1265 1265
                 $route
1266 1266
             );
1267
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1267
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1268 1268
             return false;
1269 1269
         }
1270
-        EE_Config::$_module_route_map[ (string) $key ][ (string) $route ] = array('EED_' . $module, $method_name);
1270
+        EE_Config::$_module_route_map[(string) $key][(string) $route] = array('EED_'.$module, $method_name);
1271 1271
         return true;
1272 1272
     }
1273 1273
 
@@ -1284,8 +1284,8 @@  discard block
 block discarded – undo
1284 1284
     {
1285 1285
         do_action('AHEE__EE_Config__get_route__begin', $route);
1286 1286
         $route = (string) apply_filters('FHEE__EE_Config__get_route', $route);
1287
-        if (isset(EE_Config::$_module_route_map[ $key ][ $route ])) {
1288
-            return EE_Config::$_module_route_map[ $key ][ $route ];
1287
+        if (isset(EE_Config::$_module_route_map[$key][$route])) {
1288
+            return EE_Config::$_module_route_map[$key][$route];
1289 1289
         }
1290 1290
         return null;
1291 1291
     }
@@ -1317,47 +1317,47 @@  discard block
 block discarded – undo
1317 1317
     public static function register_forward($route = null, $status = 0, $forward = null, $key = 'ee')
1318 1318
     {
1319 1319
         do_action('AHEE__EE_Config__register_forward', $route, $status, $forward);
1320
-        if (! isset(EE_Config::$_module_route_map[ $key ][ $route ]) || empty($route)) {
1320
+        if ( ! isset(EE_Config::$_module_route_map[$key][$route]) || empty($route)) {
1321 1321
             $msg = sprintf(
1322 1322
                 __('The module route %s for this forward has not been registered.', 'event_espresso'),
1323 1323
                 $route
1324 1324
             );
1325
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1325
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1326 1326
             return false;
1327 1327
         }
1328 1328
         if (empty($forward)) {
1329 1329
             $msg = sprintf(__('No forwarding route has been supplied.', 'event_espresso'), $route);
1330
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1330
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1331 1331
             return false;
1332 1332
         }
1333 1333
         if (is_array($forward)) {
1334
-            if (! isset($forward[1])) {
1334
+            if ( ! isset($forward[1])) {
1335 1335
                 $msg = sprintf(
1336 1336
                     __('A class method for the %s forwarding route has not been supplied.', 'event_espresso'),
1337 1337
                     $route
1338 1338
                 );
1339
-                EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1339
+                EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1340 1340
                 return false;
1341 1341
             }
1342
-            if (! method_exists($forward[0], $forward[1])) {
1342
+            if ( ! method_exists($forward[0], $forward[1])) {
1343 1343
                 $msg = sprintf(
1344 1344
                     __('The class method %s for the %s forwarding route is in invalid.', 'event_espresso'),
1345 1345
                     $forward[1],
1346 1346
                     $route
1347 1347
                 );
1348
-                EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1348
+                EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1349 1349
                 return false;
1350 1350
             }
1351
-        } elseif (! function_exists($forward)) {
1351
+        } elseif ( ! function_exists($forward)) {
1352 1352
             $msg = sprintf(
1353 1353
                 __('The function %s for the %s forwarding route is in invalid.', 'event_espresso'),
1354 1354
                 $forward,
1355 1355
                 $route
1356 1356
             );
1357
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1357
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1358 1358
             return false;
1359 1359
         }
1360
-        EE_Config::$_module_forward_map[ $key ][ $route ][ absint($status) ] = $forward;
1360
+        EE_Config::$_module_forward_map[$key][$route][absint($status)] = $forward;
1361 1361
         return true;
1362 1362
     }
1363 1363
 
@@ -1375,10 +1375,10 @@  discard block
 block discarded – undo
1375 1375
     public static function get_forward($route = null, $status = 0, $key = 'ee')
1376 1376
     {
1377 1377
         do_action('AHEE__EE_Config__get_forward__begin', $route, $status);
1378
-        if (isset(EE_Config::$_module_forward_map[ $key ][ $route ][ $status ])) {
1378
+        if (isset(EE_Config::$_module_forward_map[$key][$route][$status])) {
1379 1379
             return apply_filters(
1380 1380
                 'FHEE__EE_Config__get_forward',
1381
-                EE_Config::$_module_forward_map[ $key ][ $route ][ $status ],
1381
+                EE_Config::$_module_forward_map[$key][$route][$status],
1382 1382
                 $route,
1383 1383
                 $status
1384 1384
             );
@@ -1402,15 +1402,15 @@  discard block
 block discarded – undo
1402 1402
     public static function register_view($route = null, $status = 0, $view = null, $key = 'ee')
1403 1403
     {
1404 1404
         do_action('AHEE__EE_Config__register_view__begin', $route, $status, $view);
1405
-        if (! isset(EE_Config::$_module_route_map[ $key ][ $route ]) || empty($route)) {
1405
+        if ( ! isset(EE_Config::$_module_route_map[$key][$route]) || empty($route)) {
1406 1406
             $msg = sprintf(
1407 1407
                 __('The module route %s for this view has not been registered.', 'event_espresso'),
1408 1408
                 $route
1409 1409
             );
1410
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1410
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1411 1411
             return false;
1412 1412
         }
1413
-        if (! is_readable($view)) {
1413
+        if ( ! is_readable($view)) {
1414 1414
             $msg = sprintf(
1415 1415
                 __(
1416 1416
                     'The %s view file could not be found or is not readable due to file permissions.',
@@ -1418,10 +1418,10 @@  discard block
 block discarded – undo
1418 1418
                 ),
1419 1419
                 $view
1420 1420
             );
1421
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1421
+            EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__);
1422 1422
             return false;
1423 1423
         }
1424
-        EE_Config::$_module_view_map[ $key ][ $route ][ absint($status) ] = $view;
1424
+        EE_Config::$_module_view_map[$key][$route][absint($status)] = $view;
1425 1425
         return true;
1426 1426
     }
1427 1427
 
@@ -1439,10 +1439,10 @@  discard block
 block discarded – undo
1439 1439
     public static function get_view($route = null, $status = 0, $key = 'ee')
1440 1440
     {
1441 1441
         do_action('AHEE__EE_Config__get_view__begin', $route, $status);
1442
-        if (isset(EE_Config::$_module_view_map[ $key ][ $route ][ $status ])) {
1442
+        if (isset(EE_Config::$_module_view_map[$key][$route][$status])) {
1443 1443
             return apply_filters(
1444 1444
                 'FHEE__EE_Config__get_view',
1445
-                EE_Config::$_module_view_map[ $key ][ $route ][ $status ],
1445
+                EE_Config::$_module_view_map[$key][$route][$status],
1446 1446
                 $route,
1447 1447
                 $status
1448 1448
             );
@@ -1469,7 +1469,7 @@  discard block
 block discarded – undo
1469 1469
     public static function getLegacyShortcodesManager()
1470 1470
     {
1471 1471
 
1472
-        if (! EE_Config::instance()->legacy_shortcodes_manager instanceof LegacyShortcodesManager) {
1472
+        if ( ! EE_Config::instance()->legacy_shortcodes_manager instanceof LegacyShortcodesManager) {
1473 1473
             EE_Config::instance()->legacy_shortcodes_manager = new LegacyShortcodesManager(
1474 1474
                 EE_Registry::instance()
1475 1475
             );
@@ -1516,7 +1516,7 @@  discard block
 block discarded – undo
1516 1516
      */
1517 1517
     public function get_pretty($property)
1518 1518
     {
1519
-        if (! property_exists($this, $property)) {
1519
+        if ( ! property_exists($this, $property)) {
1520 1520
             throw new EE_Error(
1521 1521
                 sprintf(
1522 1522
                     __(
@@ -1745,11 +1745,11 @@  discard block
 block discarded – undo
1745 1745
      */
1746 1746
     public function reg_page_url()
1747 1747
     {
1748
-        if (! $this->reg_page_url) {
1748
+        if ( ! $this->reg_page_url) {
1749 1749
             $this->reg_page_url = add_query_arg(
1750 1750
                 array('uts' => time()),
1751 1751
                 get_permalink($this->reg_page_id)
1752
-            ) . '#checkout';
1752
+            ).'#checkout';
1753 1753
         }
1754 1754
         return $this->reg_page_url;
1755 1755
     }
@@ -1765,7 +1765,7 @@  discard block
 block discarded – undo
1765 1765
      */
1766 1766
     public function txn_page_url($query_args = array())
1767 1767
     {
1768
-        if (! $this->txn_page_url) {
1768
+        if ( ! $this->txn_page_url) {
1769 1769
             $this->txn_page_url = get_permalink($this->txn_page_id);
1770 1770
         }
1771 1771
         if ($query_args) {
@@ -1786,7 +1786,7 @@  discard block
 block discarded – undo
1786 1786
      */
1787 1787
     public function thank_you_page_url($query_args = array())
1788 1788
     {
1789
-        if (! $this->thank_you_page_url) {
1789
+        if ( ! $this->thank_you_page_url) {
1790 1790
             $this->thank_you_page_url = get_permalink($this->thank_you_page_id);
1791 1791
         }
1792 1792
         if ($query_args) {
@@ -1805,7 +1805,7 @@  discard block
 block discarded – undo
1805 1805
      */
1806 1806
     public function cancel_page_url()
1807 1807
     {
1808
-        if (! $this->cancel_page_url) {
1808
+        if ( ! $this->cancel_page_url) {
1809 1809
             $this->cancel_page_url = get_permalink($this->cancel_page_id);
1810 1810
         }
1811 1811
         return $this->cancel_page_url;
@@ -1848,13 +1848,13 @@  discard block
 block discarded – undo
1848 1848
         $current_main_site_id = ! empty($current_network_main_site) ? $current_network_main_site->blog_id : 1;
1849 1849
         $option = self::OPTION_NAME_UXIP;
1850 1850
         // set correct table for query
1851
-        $table_name = $wpdb->get_blog_prefix($current_main_site_id) . 'options';
1851
+        $table_name = $wpdb->get_blog_prefix($current_main_site_id).'options';
1852 1852
         // rather than getting blog option for the $current_main_site_id, we do a direct $wpdb query because
1853 1853
         // get_blog_option() does a switch_to_blog an that could cause infinite recursion because EE_Core_Config might be
1854 1854
         // re-constructed on the blog switch.  Note, we are still executing any core wp filters on this option retrieval.
1855 1855
         // this bit of code is basically a direct copy of get_option without any caching because we are NOT switched to the blog
1856 1856
         // for the purpose of caching.
1857
-        $pre = apply_filters('pre_option_' . $option, false, $option);
1857
+        $pre = apply_filters('pre_option_'.$option, false, $option);
1858 1858
         if (false !== $pre) {
1859 1859
             EE_Core_Config::$ee_ueip_option = $pre;
1860 1860
             return EE_Core_Config::$ee_ueip_option;
@@ -1868,10 +1868,10 @@  discard block
 block discarded – undo
1868 1868
         if (is_object($row)) {
1869 1869
             $value = $row->option_value;
1870 1870
         } else { // option does not exist so use default.
1871
-            EE_Core_Config::$ee_ueip_option =  apply_filters('default_option_' . $option, false, $option);
1871
+            EE_Core_Config::$ee_ueip_option = apply_filters('default_option_'.$option, false, $option);
1872 1872
             return EE_Core_Config::$ee_ueip_option;
1873 1873
         }
1874
-        EE_Core_Config::$ee_ueip_option = apply_filters('option_' . $option, maybe_unserialize($value), $option);
1874
+        EE_Core_Config::$ee_ueip_option = apply_filters('option_'.$option, maybe_unserialize($value), $option);
1875 1875
         return EE_Core_Config::$ee_ueip_option;
1876 1876
     }
1877 1877
 
@@ -2125,37 +2125,37 @@  discard block
 block discarded – undo
2125 2125
         // but override if requested
2126 2126
         $CNT_ISO = ! empty($CNT_ISO) ? $CNT_ISO : $ORG_CNT;
2127 2127
         // so if that all went well, and we are not in M-Mode (cuz you can't query the db in M-Mode) and double-check the countries table exists
2128
-        if (! empty($CNT_ISO)
2128
+        if ( ! empty($CNT_ISO)
2129 2129
             && EE_Maintenance_Mode::instance()->models_can_query()
2130 2130
             && $table_analysis->tableExists(EE_Registry::instance()->load_model('Country')->table())
2131 2131
         ) {
2132 2132
             // retrieve the country settings from the db, just in case they have been customized
2133 2133
             $country = EE_Registry::instance()->load_model('Country')->get_one_by_ID($CNT_ISO);
2134 2134
             if ($country instanceof EE_Country) {
2135
-                $this->code = $country->currency_code();    // currency code: USD, CAD, EUR
2136
-                $this->name = $country->currency_name_single();    // Dollar
2137
-                $this->plural = $country->currency_name_plural();    // Dollars
2138
-                $this->sign = $country->currency_sign();            // currency sign: $
2135
+                $this->code = $country->currency_code(); // currency code: USD, CAD, EUR
2136
+                $this->name = $country->currency_name_single(); // Dollar
2137
+                $this->plural = $country->currency_name_plural(); // Dollars
2138
+                $this->sign = $country->currency_sign(); // currency sign: $
2139 2139
                 $this->sign_b4 = $country->currency_sign_before(
2140
-                );        // currency sign before or after: $TRUE  or  FALSE$
2141
-                $this->dec_plc = $country->currency_decimal_places();    // decimal places: 2 = 0.00  3 = 0.000
2140
+                ); // currency sign before or after: $TRUE  or  FALSE$
2141
+                $this->dec_plc = $country->currency_decimal_places(); // decimal places: 2 = 0.00  3 = 0.000
2142 2142
                 $this->dec_mrk = $country->currency_decimal_mark(
2143
-                );    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
2143
+                ); // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
2144 2144
                 $this->thsnds = $country->currency_thousands_separator(
2145
-                );    // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
2145
+                ); // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
2146 2146
             }
2147 2147
         }
2148 2148
         // fallback to hardcoded defaults, in case the above failed
2149 2149
         if (empty($this->code)) {
2150 2150
             // set default currency settings
2151
-            $this->code = 'USD';    // currency code: USD, CAD, EUR
2152
-            $this->name = __('Dollar', 'event_espresso');    // Dollar
2153
-            $this->plural = __('Dollars', 'event_espresso');    // Dollars
2154
-            $this->sign = '$';    // currency sign: $
2155
-            $this->sign_b4 = true;    // currency sign before or after: $TRUE  or  FALSE$
2156
-            $this->dec_plc = 2;    // decimal places: 2 = 0.00  3 = 0.000
2157
-            $this->dec_mrk = '.';    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
2158
-            $this->thsnds = ',';    // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
2151
+            $this->code = 'USD'; // currency code: USD, CAD, EUR
2152
+            $this->name = __('Dollar', 'event_espresso'); // Dollar
2153
+            $this->plural = __('Dollars', 'event_espresso'); // Dollars
2154
+            $this->sign = '$'; // currency sign: $
2155
+            $this->sign_b4 = true; // currency sign before or after: $TRUE  or  FALSE$
2156
+            $this->dec_plc = 2; // decimal places: 2 = 0.00  3 = 0.000
2157
+            $this->dec_mrk = '.'; // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
2158
+            $this->thsnds = ','; // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
2159 2159
         }
2160 2160
     }
2161 2161
 }
@@ -2414,8 +2414,8 @@  discard block
 block discarded – undo
2414 2414
             $closing_a_tag = '';
2415 2415
             if (function_exists('get_privacy_policy_url')) {
2416 2416
                 $privacy_page_url = get_privacy_policy_url();
2417
-                if (! empty($privacy_page_url)) {
2418
-                    $opening_a_tag = '<a href="' . $privacy_page_url . '" target="_blank">';
2417
+                if ( ! empty($privacy_page_url)) {
2418
+                    $opening_a_tag = '<a href="'.$privacy_page_url.'" target="_blank">';
2419 2419
                     $closing_a_tag = '</a>';
2420 2420
                 }
2421 2421
             }
@@ -2627,7 +2627,7 @@  discard block
 block discarded – undo
2627 2627
     public function log_file_name($reset = false)
2628 2628
     {
2629 2629
         if (empty($this->log_file_name) || $reset) {
2630
-            $this->log_file_name = sanitize_key('espresso_log_' . md5(uniqid('', true))) . '.txt';
2630
+            $this->log_file_name = sanitize_key('espresso_log_'.md5(uniqid('', true))).'.txt';
2631 2631
             EE_Config::instance()->update_espresso_config(false, false);
2632 2632
         }
2633 2633
         return $this->log_file_name;
@@ -2641,7 +2641,7 @@  discard block
 block discarded – undo
2641 2641
     public function debug_file_name($reset = false)
2642 2642
     {
2643 2643
         if (empty($this->debug_file_name) || $reset) {
2644
-            $this->debug_file_name = sanitize_key('espresso_debug_' . md5(uniqid('', true))) . '.txt';
2644
+            $this->debug_file_name = sanitize_key('espresso_debug_'.md5(uniqid('', true))).'.txt';
2645 2645
             EE_Config::instance()->update_espresso_config(false, false);
2646 2646
         }
2647 2647
         return $this->debug_file_name;
@@ -2845,21 +2845,21 @@  discard block
 block discarded – undo
2845 2845
         $this->use_google_maps = true;
2846 2846
         $this->google_map_api_key = '';
2847 2847
         // for event details pages (reg page)
2848
-        $this->event_details_map_width = 585;            // ee_map_width_single
2849
-        $this->event_details_map_height = 362;            // ee_map_height_single
2850
-        $this->event_details_map_zoom = 14;            // ee_map_zoom_single
2851
-        $this->event_details_display_nav = true;            // ee_map_nav_display_single
2852
-        $this->event_details_nav_size = false;            // ee_map_nav_size_single
2853
-        $this->event_details_control_type = 'default';        // ee_map_type_control_single
2854
-        $this->event_details_map_align = 'center';            // ee_map_align_single
2848
+        $this->event_details_map_width = 585; // ee_map_width_single
2849
+        $this->event_details_map_height = 362; // ee_map_height_single
2850
+        $this->event_details_map_zoom = 14; // ee_map_zoom_single
2851
+        $this->event_details_display_nav = true; // ee_map_nav_display_single
2852
+        $this->event_details_nav_size = false; // ee_map_nav_size_single
2853
+        $this->event_details_control_type = 'default'; // ee_map_type_control_single
2854
+        $this->event_details_map_align = 'center'; // ee_map_align_single
2855 2855
         // for event list pages
2856
-        $this->event_list_map_width = 300;            // ee_map_width
2857
-        $this->event_list_map_height = 185;        // ee_map_height
2858
-        $this->event_list_map_zoom = 12;            // ee_map_zoom
2859
-        $this->event_list_display_nav = false;        // ee_map_nav_display
2860
-        $this->event_list_nav_size = true;            // ee_map_nav_size
2861
-        $this->event_list_control_type = 'dropdown';        // ee_map_type_control
2862
-        $this->event_list_map_align = 'center';            // ee_map_align
2856
+        $this->event_list_map_width = 300; // ee_map_width
2857
+        $this->event_list_map_height = 185; // ee_map_height
2858
+        $this->event_list_map_zoom = 12; // ee_map_zoom
2859
+        $this->event_list_display_nav = false; // ee_map_nav_display
2860
+        $this->event_list_nav_size = true; // ee_map_nav_size
2861
+        $this->event_list_control_type = 'dropdown'; // ee_map_type_control
2862
+        $this->event_list_map_align = 'center'; // ee_map_align
2863 2863
     }
2864 2864
 }
2865 2865
 
@@ -3177,7 +3177,7 @@  discard block
 block discarded – undo
3177 3177
      */
3178 3178
     public function max_input_vars_limit_check($input_count = 0)
3179 3179
     {
3180
-        if (! empty($this->php->max_input_vars)
3180
+        if ( ! empty($this->php->max_input_vars)
3181 3181
             && ($input_count >= $this->php->max_input_vars)
3182 3182
         ) {
3183 3183
             // check the server setting because the config value could be stale
Please login to merge, or discard this patch.
Indentation   +3151 added lines, -3151 removed lines patch added patch discarded remove patch
@@ -14,2509 +14,2509 @@  discard block
 block discarded – undo
14 14
 final class EE_Config implements ResettableInterface
15 15
 {
16 16
 
17
-    const OPTION_NAME = 'ee_config';
18
-
19
-    const LOG_NAME = 'ee_config_log';
20
-
21
-    const LOG_LENGTH = 100;
22
-
23
-    const ADDON_OPTION_NAMES = 'ee_config_option_names';
24
-
25
-    /**
26
-     *    instance of the EE_Config object
27
-     *
28
-     * @var    EE_Config $_instance
29
-     * @access    private
30
-     */
31
-    private static $_instance;
32
-
33
-    /**
34
-     * @var boolean $_logging_enabled
35
-     */
36
-    private static $_logging_enabled = false;
37
-
38
-    /**
39
-     * @var LegacyShortcodesManager $legacy_shortcodes_manager
40
-     */
41
-    private $legacy_shortcodes_manager;
42
-
43
-    /**
44
-     * An StdClass whose property names are addon slugs,
45
-     * and values are their config classes
46
-     *
47
-     * @var StdClass
48
-     */
49
-    public $addons;
50
-
51
-    /**
52
-     * @var EE_Admin_Config
53
-     */
54
-    public $admin;
55
-
56
-    /**
57
-     * @var EE_Core_Config
58
-     */
59
-    public $core;
60
-
61
-    /**
62
-     * @var EE_Currency_Config
63
-     */
64
-    public $currency;
65
-
66
-    /**
67
-     * @var EE_Organization_Config
68
-     */
69
-    public $organization;
70
-
71
-    /**
72
-     * @var EE_Registration_Config
73
-     */
74
-    public $registration;
75
-
76
-    /**
77
-     * @var EE_Template_Config
78
-     */
79
-    public $template_settings;
80
-
81
-    /**
82
-     * Holds EE environment values.
83
-     *
84
-     * @var EE_Environment_Config
85
-     */
86
-    public $environment;
87
-
88
-    /**
89
-     * settings pertaining to Google maps
90
-     *
91
-     * @var EE_Map_Config
92
-     */
93
-    public $map_settings;
94
-
95
-    /**
96
-     * settings pertaining to Taxes
97
-     *
98
-     * @var EE_Tax_Config
99
-     */
100
-    public $tax_settings;
101
-
102
-    /**
103
-     * Settings pertaining to global messages settings.
104
-     *
105
-     * @var EE_Messages_Config
106
-     */
107
-    public $messages;
108
-
109
-    /**
110
-     * @deprecated
111
-     * @var EE_Gateway_Config
112
-     */
113
-    public $gateway;
114
-
115
-    /**
116
-     * @var    array $_addon_option_names
117
-     * @access    private
118
-     */
119
-    private $_addon_option_names = array();
120
-
121
-    /**
122
-     * @var    array $_module_route_map
123
-     * @access    private
124
-     */
125
-    private static $_module_route_map = array();
126
-
127
-    /**
128
-     * @var    array $_module_forward_map
129
-     * @access    private
130
-     */
131
-    private static $_module_forward_map = array();
132
-
133
-    /**
134
-     * @var    array $_module_view_map
135
-     * @access    private
136
-     */
137
-    private static $_module_view_map = array();
138
-
139
-
140
-    /**
141
-     * @singleton method used to instantiate class object
142
-     * @access    public
143
-     * @return EE_Config instance
144
-     */
145
-    public static function instance()
146
-    {
147
-        // check if class object is instantiated, and instantiated properly
148
-        if (! self::$_instance instanceof EE_Config) {
149
-            self::$_instance = new self();
150
-        }
151
-        return self::$_instance;
152
-    }
153
-
154
-
155
-    /**
156
-     * Resets the config
157
-     *
158
-     * @param bool    $hard_reset    if TRUE, sets EE_CONFig back to its original settings in the database. If FALSE
159
-     *                               (default) leaves the database alone, and merely resets the EE_Config object to
160
-     *                               reflect its state in the database
161
-     * @param boolean $reinstantiate if TRUE (default) call instance() and return it. Otherwise, just leave
162
-     *                               $_instance as NULL. Useful in case you want to forget about the old instance on
163
-     *                               EE_Config, but might not be ready to instantiate EE_Config currently (eg if the
164
-     *                               site was put into maintenance mode)
165
-     * @return EE_Config
166
-     */
167
-    public static function reset($hard_reset = false, $reinstantiate = true)
168
-    {
169
-        if (self::$_instance instanceof EE_Config) {
170
-            if ($hard_reset) {
171
-                self::$_instance->legacy_shortcodes_manager = null;
172
-                self::$_instance->_addon_option_names = array();
173
-                self::$_instance->_initialize_config();
174
-                self::$_instance->update_espresso_config();
175
-            }
176
-            self::$_instance->update_addon_option_names();
177
-        }
178
-        self::$_instance = null;
179
-        // we don't need to reset the static properties imo because those should
180
-        // only change when a module is added or removed. Currently we don't
181
-        // support removing a module during a request when it previously existed
182
-        if ($reinstantiate) {
183
-            return self::instance();
184
-        } else {
185
-            return null;
186
-        }
187
-    }
188
-
189
-
190
-    /**
191
-     *    class constructor
192
-     *
193
-     * @access    private
194
-     */
195
-    private function __construct()
196
-    {
197
-        do_action('AHEE__EE_Config__construct__begin', $this);
198
-        EE_Config::$_logging_enabled = apply_filters('FHEE__EE_Config___construct__logging_enabled', false);
199
-        // setup empty config classes
200
-        $this->_initialize_config();
201
-        // load existing EE site settings
202
-        $this->_load_core_config();
203
-        // confirm everything loaded correctly and set filtered defaults if not
204
-        $this->_verify_config();
205
-        //  register shortcodes and modules
206
-        add_action(
207
-            'AHEE__EE_System__register_shortcodes_modules_and_widgets',
208
-            array($this, 'register_shortcodes_and_modules'),
209
-            999
210
-        );
211
-        //  initialize shortcodes and modules
212
-        add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'initialize_shortcodes_and_modules'));
213
-        // register widgets
214
-        add_action('widgets_init', array($this, 'widgets_init'), 10);
215
-        // shutdown
216
-        add_action('shutdown', array($this, 'shutdown'), 10);
217
-        // construct__end hook
218
-        do_action('AHEE__EE_Config__construct__end', $this);
219
-        // hardcoded hack
220
-        $this->template_settings->current_espresso_theme = 'Espresso_Arabica_2014';
221
-    }
222
-
223
-
224
-    /**
225
-     * @return boolean
226
-     */
227
-    public static function logging_enabled()
228
-    {
229
-        return self::$_logging_enabled;
230
-    }
231
-
232
-
233
-    /**
234
-     * use to get the current theme if needed from static context
235
-     *
236
-     * @return string current theme set.
237
-     */
238
-    public static function get_current_theme()
239
-    {
240
-        return isset(self::$_instance->template_settings->current_espresso_theme)
241
-            ? self::$_instance->template_settings->current_espresso_theme : 'Espresso_Arabica_2014';
242
-    }
243
-
244
-
245
-    /**
246
-     *        _initialize_config
247
-     *
248
-     * @access private
249
-     * @return void
250
-     */
251
-    private function _initialize_config()
252
-    {
253
-        EE_Config::trim_log();
254
-        // set defaults
255
-        $this->_addon_option_names = get_option(EE_Config::ADDON_OPTION_NAMES, array());
256
-        $this->addons = new stdClass();
257
-        // set _module_route_map
258
-        EE_Config::$_module_route_map = array();
259
-        // set _module_forward_map
260
-        EE_Config::$_module_forward_map = array();
261
-        // set _module_view_map
262
-        EE_Config::$_module_view_map = array();
263
-    }
264
-
265
-
266
-    /**
267
-     *        load core plugin configuration
268
-     *
269
-     * @access private
270
-     * @return void
271
-     */
272
-    private function _load_core_config()
273
-    {
274
-        // load_core_config__start hook
275
-        do_action('AHEE__EE_Config___load_core_config__start', $this);
276
-        $espresso_config = $this->get_espresso_config();
277
-        foreach ($espresso_config as $config => $settings) {
278
-            // load_core_config__start hook
279
-            $settings = apply_filters(
280
-                'FHEE__EE_Config___load_core_config__config_settings',
281
-                $settings,
282
-                $config,
283
-                $this
284
-            );
285
-            if (is_object($settings) && property_exists($this, $config)) {
286
-                $this->{$config} = apply_filters('FHEE__EE_Config___load_core_config__' . $config, $settings);
287
-                // call configs populate method to ensure any defaults are set for empty values.
288
-                if (method_exists($settings, 'populate')) {
289
-                    $this->{$config}->populate();
290
-                }
291
-                if (method_exists($settings, 'do_hooks')) {
292
-                    $this->{$config}->do_hooks();
293
-                }
294
-            }
295
-        }
296
-        if (apply_filters('FHEE__EE_Config___load_core_config__update_espresso_config', false)) {
297
-            $this->update_espresso_config();
298
-        }
299
-        // load_core_config__end hook
300
-        do_action('AHEE__EE_Config___load_core_config__end', $this);
301
-    }
302
-
303
-
304
-    /**
305
-     *    _verify_config
306
-     *
307
-     * @access    protected
308
-     * @return    void
309
-     */
310
-    protected function _verify_config()
311
-    {
312
-        $this->core = $this->core instanceof EE_Core_Config
313
-            ? $this->core
314
-            : new EE_Core_Config();
315
-        $this->core = apply_filters('FHEE__EE_Config___initialize_config__core', $this->core);
316
-        $this->organization = $this->organization instanceof EE_Organization_Config
317
-            ? $this->organization
318
-            : new EE_Organization_Config();
319
-        $this->organization = apply_filters(
320
-            'FHEE__EE_Config___initialize_config__organization',
321
-            $this->organization
322
-        );
323
-        $this->currency = $this->currency instanceof EE_Currency_Config
324
-            ? $this->currency
325
-            : new EE_Currency_Config();
326
-        $this->currency = apply_filters('FHEE__EE_Config___initialize_config__currency', $this->currency);
327
-        $this->registration = $this->registration instanceof EE_Registration_Config
328
-            ? $this->registration
329
-            : new EE_Registration_Config();
330
-        $this->registration = apply_filters(
331
-            'FHEE__EE_Config___initialize_config__registration',
332
-            $this->registration
333
-        );
334
-        $this->admin = $this->admin instanceof EE_Admin_Config
335
-            ? $this->admin
336
-            : new EE_Admin_Config();
337
-        $this->admin = apply_filters('FHEE__EE_Config___initialize_config__admin', $this->admin);
338
-        $this->template_settings = $this->template_settings instanceof EE_Template_Config
339
-            ? $this->template_settings
340
-            : new EE_Template_Config();
341
-        $this->template_settings = apply_filters(
342
-            'FHEE__EE_Config___initialize_config__template_settings',
343
-            $this->template_settings
344
-        );
345
-        $this->map_settings = $this->map_settings instanceof EE_Map_Config
346
-            ? $this->map_settings
347
-            : new EE_Map_Config();
348
-        $this->map_settings = apply_filters(
349
-            'FHEE__EE_Config___initialize_config__map_settings',
350
-            $this->map_settings
351
-        );
352
-        $this->environment = $this->environment instanceof EE_Environment_Config
353
-            ? $this->environment
354
-            : new EE_Environment_Config();
355
-        $this->environment = apply_filters(
356
-            'FHEE__EE_Config___initialize_config__environment',
357
-            $this->environment
358
-        );
359
-        $this->tax_settings = $this->tax_settings instanceof EE_Tax_Config
360
-            ? $this->tax_settings
361
-            : new EE_Tax_Config();
362
-        $this->tax_settings = apply_filters(
363
-            'FHEE__EE_Config___initialize_config__tax_settings',
364
-            $this->tax_settings
365
-        );
366
-        $this->messages = apply_filters('FHEE__EE_Config__initialize_config__messages', $this->messages);
367
-        $this->messages = $this->messages instanceof EE_Messages_Config
368
-            ? $this->messages
369
-            : new EE_Messages_Config();
370
-        $this->gateway = $this->gateway instanceof EE_Gateway_Config
371
-            ? $this->gateway
372
-            : new EE_Gateway_Config();
373
-        $this->gateway = apply_filters('FHEE__EE_Config___initialize_config__gateway', $this->gateway);
374
-        $this->legacy_shortcodes_manager = null;
375
-    }
376
-
377
-
378
-    /**
379
-     *    get_espresso_config
380
-     *
381
-     * @access    public
382
-     * @return    array of espresso config stuff
383
-     */
384
-    public function get_espresso_config()
385
-    {
386
-        // grab espresso configuration
387
-        return apply_filters(
388
-            'FHEE__EE_Config__get_espresso_config__CFG',
389
-            get_option(EE_Config::OPTION_NAME, array())
390
-        );
391
-    }
392
-
393
-
394
-    /**
395
-     *    double_check_config_comparison
396
-     *
397
-     * @access    public
398
-     * @param string $option
399
-     * @param        $old_value
400
-     * @param        $value
401
-     */
402
-    public function double_check_config_comparison($option = '', $old_value, $value)
403
-    {
404
-        // make sure we're checking the ee config
405
-        if ($option === EE_Config::OPTION_NAME) {
406
-            // run a loose comparison of the old value against the new value for type and properties,
407
-            // but NOT exact instance like WP update_option does (ie: NOT type safe comparison)
408
-            if ($value != $old_value) {
409
-                // if they are NOT the same, then remove the hook,
410
-                // which means the subsequent update results will be based solely on the update query results
411
-                // the reason we do this is because, as stated above,
412
-                // WP update_option performs an exact instance comparison (===) on any update values passed to it
413
-                // this happens PRIOR to serialization and any subsequent update.
414
-                // If values are found to match their previous old value,
415
-                // then WP bails before performing any update.
416
-                // Since we are passing the EE_Config object, it is comparing the EXACT instance of the saved version
417
-                // it just pulled from the db, with the one being passed to it (which will not match).
418
-                // HOWEVER, once the object is serialized and passed off to MySQL to update,
419
-                // MySQL MAY ALSO NOT perform the update because
420
-                // the string it sees in the db looks the same as the new one it has been passed!!!
421
-                // This results in the query returning an "affected rows" value of ZERO,
422
-                // which gets returned immediately by WP update_option and looks like an error.
423
-                remove_action('update_option', array($this, 'check_config_updated'));
424
-            }
425
-        }
426
-    }
427
-
428
-
429
-    /**
430
-     *    update_espresso_config
431
-     *
432
-     * @access   public
433
-     */
434
-    protected function _reset_espresso_addon_config()
435
-    {
436
-        $this->_addon_option_names = array();
437
-        foreach ($this->addons as $addon_name => $addon_config_obj) {
438
-            $addon_config_obj = maybe_unserialize($addon_config_obj);
439
-            if ($addon_config_obj instanceof EE_Config_Base) {
440
-                $this->update_config('addons', $addon_name, $addon_config_obj, false);
441
-            }
442
-            $this->addons->{$addon_name} = null;
443
-        }
444
-    }
445
-
446
-
447
-    /**
448
-     *    update_espresso_config
449
-     *
450
-     * @access   public
451
-     * @param   bool $add_success
452
-     * @param   bool $add_error
453
-     * @return   bool
454
-     */
455
-    public function update_espresso_config($add_success = false, $add_error = true)
456
-    {
457
-        // don't allow config updates during WP heartbeats
458
-        if (\EE_Registry::instance()->REQ->get('action', '') === 'heartbeat') {
459
-            return false;
460
-        }
461
-        // commented out the following re: https://events.codebasehq.com/projects/event-espresso/tickets/8197
462
-        // $clone = clone( self::$_instance );
463
-        // self::$_instance = NULL;
464
-        do_action('AHEE__EE_Config__update_espresso_config__begin', $this);
465
-        $this->_reset_espresso_addon_config();
466
-        // hook into update_option because that happens AFTER the ( $value === $old_value ) conditional
467
-        // but BEFORE the actual update occurs
468
-        add_action('update_option', array($this, 'double_check_config_comparison'), 1, 3);
469
-        // don't want to persist legacy_shortcodes_manager, but don't want to lose it either
470
-        $legacy_shortcodes_manager = $this->legacy_shortcodes_manager;
471
-        $this->legacy_shortcodes_manager = null;
472
-        // now update "ee_config"
473
-        $saved = update_option(EE_Config::OPTION_NAME, $this);
474
-        $this->legacy_shortcodes_manager = $legacy_shortcodes_manager;
475
-        EE_Config::log(EE_Config::OPTION_NAME);
476
-        // if not saved... check if the hook we just added still exists;
477
-        // if it does, it means one of two things:
478
-        // that update_option bailed at the($value === $old_value) conditional,
479
-        // or...
480
-        // the db update query returned 0 rows affected
481
-        // (probably because the data  value was the same from it's perspective)
482
-        // so the existence of the hook means that a negative result from update_option is NOT an error,
483
-        // but just means no update occurred, so don't display an error to the user.
484
-        // BUT... if update_option returns FALSE, AND the hook is missing,
485
-        // then it means that something truly went wrong
486
-        $saved = ! $saved ? has_action('update_option', array($this, 'double_check_config_comparison')) : $saved;
487
-        // remove our action since we don't want it in the system anymore
488
-        remove_action('update_option', array($this, 'double_check_config_comparison'), 1);
489
-        do_action('AHEE__EE_Config__update_espresso_config__end', $this, $saved);
490
-        // self::$_instance = $clone;
491
-        // unset( $clone );
492
-        // if config remains the same or was updated successfully
493
-        if ($saved) {
494
-            if ($add_success) {
495
-                EE_Error::add_success(
496
-                    __('The Event Espresso Configuration Settings have been successfully updated.', 'event_espresso'),
497
-                    __FILE__,
498
-                    __FUNCTION__,
499
-                    __LINE__
500
-                );
501
-            }
502
-            return true;
503
-        } else {
504
-            if ($add_error) {
505
-                EE_Error::add_error(
506
-                    __('The Event Espresso Configuration Settings were not updated.', 'event_espresso'),
507
-                    __FILE__,
508
-                    __FUNCTION__,
509
-                    __LINE__
510
-                );
511
-            }
512
-            return false;
513
-        }
514
-    }
515
-
516
-
517
-    /**
518
-     *    _verify_config_params
519
-     *
520
-     * @access    private
521
-     * @param    string         $section
522
-     * @param    string         $name
523
-     * @param    string         $config_class
524
-     * @param    EE_Config_Base $config_obj
525
-     * @param    array          $tests_to_run
526
-     * @param    bool           $display_errors
527
-     * @return    bool    TRUE on success, FALSE on fail
528
-     */
529
-    private function _verify_config_params(
530
-        $section = '',
531
-        $name = '',
532
-        $config_class = '',
533
-        $config_obj = null,
534
-        $tests_to_run = array(1, 2, 3, 4, 5, 6, 7, 8),
535
-        $display_errors = true
536
-    ) {
537
-        try {
538
-            foreach ($tests_to_run as $test) {
539
-                switch ($test) {
540
-                    // TEST #1 : check that section was set
541
-                    case 1:
542
-                        if (empty($section)) {
543
-                            if ($display_errors) {
544
-                                throw new EE_Error(
545
-                                    sprintf(
546
-                                        __(
547
-                                            'No configuration section has been provided while attempting to save "%s".',
548
-                                            'event_espresso'
549
-                                        ),
550
-                                        $config_class
551
-                                    )
552
-                                );
553
-                            }
554
-                            return false;
555
-                        }
556
-                        break;
557
-                    // TEST #2 : check that settings section exists
558
-                    case 2:
559
-                        if (! isset($this->{$section})) {
560
-                            if ($display_errors) {
561
-                                throw new EE_Error(
562
-                                    sprintf(
563
-                                        __('The "%s" configuration section does not exist.', 'event_espresso'),
564
-                                        $section
565
-                                    )
566
-                                );
567
-                            }
568
-                            return false;
569
-                        }
570
-                        break;
571
-                    // TEST #3 : check that section is the proper format
572
-                    case 3:
573
-                        if (! ($this->{$section} instanceof EE_Config_Base || $this->{$section} instanceof stdClass)
574
-                        ) {
575
-                            if ($display_errors) {
576
-                                throw new EE_Error(
577
-                                    sprintf(
578
-                                        __(
579
-                                            'The "%s" configuration settings have not been formatted correctly.',
580
-                                            'event_espresso'
581
-                                        ),
582
-                                        $section
583
-                                    )
584
-                                );
585
-                            }
586
-                            return false;
587
-                        }
588
-                        break;
589
-                    // TEST #4 : check that config section name has been set
590
-                    case 4:
591
-                        if (empty($name)) {
592
-                            if ($display_errors) {
593
-                                throw new EE_Error(
594
-                                    __(
595
-                                        'No name has been provided for the specific configuration section.',
596
-                                        'event_espresso'
597
-                                    )
598
-                                );
599
-                            }
600
-                            return false;
601
-                        }
602
-                        break;
603
-                    // TEST #5 : check that a config class name has been set
604
-                    case 5:
605
-                        if (empty($config_class)) {
606
-                            if ($display_errors) {
607
-                                throw new EE_Error(
608
-                                    __(
609
-                                        'No class name has been provided for the specific configuration section.',
610
-                                        'event_espresso'
611
-                                    )
612
-                                );
613
-                            }
614
-                            return false;
615
-                        }
616
-                        break;
617
-                    // TEST #6 : verify config class is accessible
618
-                    case 6:
619
-                        if (! class_exists($config_class)) {
620
-                            if ($display_errors) {
621
-                                throw new EE_Error(
622
-                                    sprintf(
623
-                                        __(
624
-                                            'The "%s" class does not exist. Please ensure that an autoloader has been set for it.',
625
-                                            'event_espresso'
626
-                                        ),
627
-                                        $config_class
628
-                                    )
629
-                                );
630
-                            }
631
-                            return false;
632
-                        }
633
-                        break;
634
-                    // TEST #7 : check that config has even been set
635
-                    case 7:
636
-                        if (! isset($this->{$section}->{$name})) {
637
-                            if ($display_errors) {
638
-                                throw new EE_Error(
639
-                                    sprintf(
640
-                                        __('No configuration has been set for "%1$s->%2$s".', 'event_espresso'),
641
-                                        $section,
642
-                                        $name
643
-                                    )
644
-                                );
645
-                            }
646
-                            return false;
647
-                        } else {
648
-                            // and make sure it's not serialized
649
-                            $this->{$section}->{$name} = maybe_unserialize($this->{$section}->{$name});
650
-                        }
651
-                        break;
652
-                    // TEST #8 : check that config is the requested type
653
-                    case 8:
654
-                        if (! $this->{$section}->{$name} instanceof $config_class) {
655
-                            if ($display_errors) {
656
-                                throw new EE_Error(
657
-                                    sprintf(
658
-                                        __(
659
-                                            'The configuration for "%1$s->%2$s" is not of the "%3$s" class.',
660
-                                            'event_espresso'
661
-                                        ),
662
-                                        $section,
663
-                                        $name,
664
-                                        $config_class
665
-                                    )
666
-                                );
667
-                            }
668
-                            return false;
669
-                        }
670
-                        break;
671
-                    // TEST #9 : verify config object
672
-                    case 9:
673
-                        if (! $config_obj instanceof EE_Config_Base) {
674
-                            if ($display_errors) {
675
-                                throw new EE_Error(
676
-                                    sprintf(
677
-                                        __('The "%s" class is not an instance of EE_Config_Base.', 'event_espresso'),
678
-                                        print_r($config_obj, true)
679
-                                    )
680
-                                );
681
-                            }
682
-                            return false;
683
-                        }
684
-                        break;
685
-                }
686
-            }
687
-        } catch (EE_Error $e) {
688
-            $e->get_error();
689
-        }
690
-        // you have successfully run the gauntlet
691
-        return true;
692
-    }
693
-
694
-
695
-    /**
696
-     *    _generate_config_option_name
697
-     *
698
-     * @access        protected
699
-     * @param        string $section
700
-     * @param        string $name
701
-     * @return        string
702
-     */
703
-    private function _generate_config_option_name($section = '', $name = '')
704
-    {
705
-        return 'ee_config-' . strtolower($section . '-' . str_replace(array('EE_', 'EED_'), '', $name));
706
-    }
707
-
708
-
709
-    /**
710
-     *    _set_config_class
711
-     * ensures that a config class is set, either from a passed config class or one generated from the config name
712
-     *
713
-     * @access    private
714
-     * @param    string $config_class
715
-     * @param    string $name
716
-     * @return    string
717
-     */
718
-    private function _set_config_class($config_class = '', $name = '')
719
-    {
720
-        return ! empty($config_class)
721
-            ? $config_class
722
-            : str_replace(' ', '_', ucwords(str_replace('_', ' ', $name))) . '_Config';
723
-    }
724
-
725
-
726
-    /**
727
-     *    set_config
728
-     *
729
-     * @access    protected
730
-     * @param    string         $section
731
-     * @param    string         $name
732
-     * @param    string         $config_class
733
-     * @param    EE_Config_Base $config_obj
734
-     * @return    EE_Config_Base
735
-     */
736
-    public function set_config($section = '', $name = '', $config_class = '', EE_Config_Base $config_obj = null)
737
-    {
738
-        // ensure config class is set to something
739
-        $config_class = $this->_set_config_class($config_class, $name);
740
-        // run tests 1-4, 6, and 7 to verify all config params are set and valid
741
-        if (! $this->_verify_config_params($section, $name, $config_class, null, array(1, 2, 3, 4, 5, 6))) {
742
-            return null;
743
-        }
744
-        $config_option_name = $this->_generate_config_option_name($section, $name);
745
-        // if the config option name hasn't been added yet to the list of option names we're tracking, then do so now
746
-        if (! isset($this->_addon_option_names[ $config_option_name ])) {
747
-            $this->_addon_option_names[ $config_option_name ] = $config_class;
748
-            $this->update_addon_option_names();
749
-        }
750
-        // verify the incoming config object but suppress errors
751
-        if (! $this->_verify_config_params($section, $name, $config_class, $config_obj, array(9), false)) {
752
-            $config_obj = new $config_class();
753
-        }
754
-        if (get_option($config_option_name)) {
755
-            EE_Config::log($config_option_name);
756
-            update_option($config_option_name, $config_obj);
757
-            $this->{$section}->{$name} = $config_obj;
758
-            return $this->{$section}->{$name};
759
-        } else {
760
-            // create a wp-option for this config
761
-            if (add_option($config_option_name, $config_obj, '', 'no')) {
762
-                $this->{$section}->{$name} = maybe_unserialize($config_obj);
763
-                return $this->{$section}->{$name};
764
-            } else {
765
-                EE_Error::add_error(
766
-                    sprintf(__('The "%s" could not be saved to the database.', 'event_espresso'), $config_class),
767
-                    __FILE__,
768
-                    __FUNCTION__,
769
-                    __LINE__
770
-                );
771
-                return null;
772
-            }
773
-        }
774
-    }
775
-
776
-
777
-    /**
778
-     *    update_config
779
-     * Important: the config object must ALREADY be set, otherwise this will produce an error.
780
-     *
781
-     * @access    public
782
-     * @param    string                $section
783
-     * @param    string                $name
784
-     * @param    EE_Config_Base|string $config_obj
785
-     * @param    bool                  $throw_errors
786
-     * @return    bool
787
-     */
788
-    public function update_config($section = '', $name = '', $config_obj = '', $throw_errors = true)
789
-    {
790
-        // don't allow config updates during WP heartbeats
791
-        if (\EE_Registry::instance()->REQ->get('action', '') === 'heartbeat') {
792
-            return false;
793
-        }
794
-        $config_obj = maybe_unserialize($config_obj);
795
-        // get class name of the incoming object
796
-        $config_class = get_class($config_obj);
797
-        // run tests 1-5 and 9 to verify config
798
-        if (! $this->_verify_config_params(
799
-            $section,
800
-            $name,
801
-            $config_class,
802
-            $config_obj,
803
-            array(1, 2, 3, 4, 7, 9)
804
-        )
805
-        ) {
806
-            return false;
807
-        }
808
-        $config_option_name = $this->_generate_config_option_name($section, $name);
809
-        // check if config object has been added to db by seeing if config option name is in $this->_addon_option_names array
810
-        if (! isset($this->_addon_option_names[ $config_option_name ])) {
811
-            // save new config to db
812
-            if ($this->set_config($section, $name, $config_class, $config_obj)) {
813
-                return true;
814
-            }
815
-        } else {
816
-            // first check if the record already exists
817
-            $existing_config = get_option($config_option_name);
818
-            $config_obj = serialize($config_obj);
819
-            // just return if db record is already up to date (NOT type safe comparison)
820
-            if ($existing_config == $config_obj) {
821
-                $this->{$section}->{$name} = $config_obj;
822
-                return true;
823
-            } elseif (update_option($config_option_name, $config_obj)) {
824
-                EE_Config::log($config_option_name);
825
-                // update wp-option for this config class
826
-                $this->{$section}->{$name} = $config_obj;
827
-                return true;
828
-            } elseif ($throw_errors) {
829
-                EE_Error::add_error(
830
-                    sprintf(
831
-                        __(
832
-                            'The "%1$s" object stored at"%2$s" was not successfully updated in the database.',
833
-                            'event_espresso'
834
-                        ),
835
-                        $config_class,
836
-                        'EE_Config->' . $section . '->' . $name
837
-                    ),
838
-                    __FILE__,
839
-                    __FUNCTION__,
840
-                    __LINE__
841
-                );
842
-            }
843
-        }
844
-        return false;
845
-    }
846
-
847
-
848
-    /**
849
-     *    get_config
850
-     *
851
-     * @access    public
852
-     * @param    string $section
853
-     * @param    string $name
854
-     * @param    string $config_class
855
-     * @return    mixed EE_Config_Base | NULL
856
-     */
857
-    public function get_config($section = '', $name = '', $config_class = '')
858
-    {
859
-        // ensure config class is set to something
860
-        $config_class = $this->_set_config_class($config_class, $name);
861
-        // run tests 1-4, 6 and 7 to verify that all params have been set
862
-        if (! $this->_verify_config_params($section, $name, $config_class, null, array(1, 2, 3, 4, 5, 6))) {
863
-            return null;
864
-        }
865
-        // now test if the requested config object exists, but suppress errors
866
-        if ($this->_verify_config_params($section, $name, $config_class, null, array(7, 8), false)) {
867
-            // config already exists, so pass it back
868
-            return $this->{$section}->{$name};
869
-        }
870
-        // load config option from db if it exists
871
-        $config_obj = $this->get_config_option($this->_generate_config_option_name($section, $name));
872
-        // verify the newly retrieved config object, but suppress errors
873
-        if ($this->_verify_config_params($section, $name, $config_class, $config_obj, array(9), false)) {
874
-            // config is good, so set it and pass it back
875
-            $this->{$section}->{$name} = $config_obj;
876
-            return $this->{$section}->{$name};
877
-        }
878
-        // oops! $config_obj is not already set and does not exist in the db, so create a new one
879
-        $config_obj = $this->set_config($section, $name, $config_class);
880
-        // verify the newly created config object
881
-        if ($this->_verify_config_params($section, $name, $config_class, $config_obj, array(9))) {
882
-            return $this->{$section}->{$name};
883
-        } else {
884
-            EE_Error::add_error(
885
-                sprintf(__('The "%s" could not be retrieved from the database.', 'event_espresso'), $config_class),
886
-                __FILE__,
887
-                __FUNCTION__,
888
-                __LINE__
889
-            );
890
-        }
891
-        return null;
892
-    }
893
-
894
-
895
-    /**
896
-     *    get_config_option
897
-     *
898
-     * @access    public
899
-     * @param    string $config_option_name
900
-     * @return    mixed EE_Config_Base | FALSE
901
-     */
902
-    public function get_config_option($config_option_name = '')
903
-    {
904
-        // retrieve the wp-option for this config class.
905
-        $config_option = maybe_unserialize(get_option($config_option_name, array()));
906
-        if (empty($config_option)) {
907
-            EE_Config::log($config_option_name . '-NOT-FOUND');
908
-        }
909
-        return $config_option;
910
-    }
911
-
912
-
913
-    /**
914
-     * log
915
-     *
916
-     * @param string $config_option_name
917
-     */
918
-    public static function log($config_option_name = '')
919
-    {
920
-        if (EE_Config::logging_enabled() && ! empty($config_option_name)) {
921
-            $config_log = get_option(EE_Config::LOG_NAME, array());
922
-            // copy incoming $_REQUEST and sanitize it so we can save it
923
-            $_request = $_REQUEST;
924
-            array_walk_recursive($_request, 'sanitize_text_field');
925
-            $config_log[ (string) microtime(true) ] = array(
926
-                'config_name' => $config_option_name,
927
-                'request'     => $_request,
928
-            );
929
-            update_option(EE_Config::LOG_NAME, $config_log);
930
-        }
931
-    }
932
-
933
-
934
-    /**
935
-     * trim_log
936
-     * reduces the size of the config log to the length specified by EE_Config::LOG_LENGTH
937
-     */
938
-    public static function trim_log()
939
-    {
940
-        if (! EE_Config::logging_enabled()) {
941
-            return;
942
-        }
943
-        $config_log = maybe_unserialize(get_option(EE_Config::LOG_NAME, array()));
944
-        $log_length = count($config_log);
945
-        if ($log_length > EE_Config::LOG_LENGTH) {
946
-            ksort($config_log);
947
-            $config_log = array_slice($config_log, $log_length - EE_Config::LOG_LENGTH, null, true);
948
-            update_option(EE_Config::LOG_NAME, $config_log);
949
-        }
950
-    }
951
-
952
-
953
-    /**
954
-     *    get_page_for_posts
955
-     *    if the wp-option "show_on_front" is set to "page", then this is the post_name for the post set in the
956
-     *    wp-option "page_for_posts", or "posts" if no page is selected
957
-     *
958
-     * @access    public
959
-     * @return    string
960
-     */
961
-    public static function get_page_for_posts()
962
-    {
963
-        $page_for_posts = get_option('page_for_posts');
964
-        if (! $page_for_posts) {
965
-            return 'posts';
966
-        }
967
-        /** @type WPDB $wpdb */
968
-        global $wpdb;
969
-        $SQL = "SELECT post_name from $wpdb->posts WHERE post_type='posts' OR post_type='page' AND post_status='publish' AND ID=%d";
970
-        return $wpdb->get_var($wpdb->prepare($SQL, $page_for_posts));
971
-    }
972
-
973
-
974
-    /**
975
-     *    register_shortcodes_and_modules.
976
-     *    At this point, it's too early to tell if we're maintenance mode or not.
977
-     *    In fact, this is where we give modules a chance to let core know they exist
978
-     *    so they can help trigger maintenance mode if it's needed
979
-     *
980
-     * @access    public
981
-     * @return    void
982
-     */
983
-    public function register_shortcodes_and_modules()
984
-    {
985
-        // allow modules to set hooks for the rest of the system
986
-        EE_Registry::instance()->modules = $this->_register_modules();
987
-    }
988
-
989
-
990
-    /**
991
-     *    initialize_shortcodes_and_modules
992
-     *    meaning they can start adding their hooks to get stuff done
993
-     *
994
-     * @access    public
995
-     * @return    void
996
-     */
997
-    public function initialize_shortcodes_and_modules()
998
-    {
999
-        // allow modules to set hooks for the rest of the system
1000
-        $this->_initialize_modules();
1001
-    }
1002
-
1003
-
1004
-    /**
1005
-     *    widgets_init
1006
-     *
1007
-     * @access private
1008
-     * @return void
1009
-     */
1010
-    public function widgets_init()
1011
-    {
1012
-        // only init widgets on admin pages when not in complete maintenance, and
1013
-        // on frontend when not in any maintenance mode
1014
-        if (! EE_Maintenance_Mode::instance()->level()
1015
-            || (
1016
-                is_admin()
1017
-                && EE_Maintenance_Mode::instance()->level() !== EE_Maintenance_Mode::level_2_complete_maintenance
1018
-            )
1019
-        ) {
1020
-            // grab list of installed widgets
1021
-            $widgets_to_register = glob(EE_WIDGETS . '*', GLOB_ONLYDIR);
1022
-            // filter list of modules to register
1023
-            $widgets_to_register = apply_filters(
1024
-                'FHEE__EE_Config__register_widgets__widgets_to_register',
1025
-                $widgets_to_register
1026
-            );
1027
-            if (! empty($widgets_to_register)) {
1028
-                // cycle thru widget folders
1029
-                foreach ($widgets_to_register as $widget_path) {
1030
-                    // add to list of installed widget modules
1031
-                    EE_Config::register_ee_widget($widget_path);
1032
-                }
1033
-            }
1034
-            // filter list of installed modules
1035
-            EE_Registry::instance()->widgets = apply_filters(
1036
-                'FHEE__EE_Config__register_widgets__installed_widgets',
1037
-                EE_Registry::instance()->widgets
1038
-            );
1039
-        }
1040
-    }
1041
-
1042
-
1043
-    /**
1044
-     *    register_ee_widget - makes core aware of this widget
1045
-     *
1046
-     * @access    public
1047
-     * @param    string $widget_path - full path up to and including widget folder
1048
-     * @return    void
1049
-     */
1050
-    public static function register_ee_widget($widget_path = null)
1051
-    {
1052
-        do_action('AHEE__EE_Config__register_widget__begin', $widget_path);
1053
-        $widget_ext = '.widget.php';
1054
-        // make all separators match
1055
-        $widget_path = rtrim(str_replace('\\', DS, $widget_path), DS);
1056
-        // does the file path INCLUDE the actual file name as part of the path ?
1057
-        if (strpos($widget_path, $widget_ext) !== false) {
1058
-            // grab and shortcode file name from directory name and break apart at dots
1059
-            $file_name = explode('.', basename($widget_path));
1060
-            // take first segment from file name pieces and remove class prefix if it exists
1061
-            $widget = strpos($file_name[0], 'EEW_') === 0 ? substr($file_name[0], 4) : $file_name[0];
1062
-            // sanitize shortcode directory name
1063
-            $widget = sanitize_key($widget);
1064
-            // now we need to rebuild the shortcode path
1065
-            $widget_path = explode(DS, $widget_path);
1066
-            // remove last segment
1067
-            array_pop($widget_path);
1068
-            // glue it back together
1069
-            $widget_path = implode(DS, $widget_path);
1070
-        } else {
1071
-            // grab and sanitize widget directory name
1072
-            $widget = sanitize_key(basename($widget_path));
1073
-        }
1074
-        // create classname from widget directory name
1075
-        $widget = str_replace(' ', '_', ucwords(str_replace('_', ' ', $widget)));
1076
-        // add class prefix
1077
-        $widget_class = 'EEW_' . $widget;
1078
-        // does the widget exist ?
1079
-        if (! is_readable($widget_path . DS . $widget_class . $widget_ext)) {
1080
-            $msg = sprintf(
1081
-                __(
1082
-                    'The requested %s widget file could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s',
1083
-                    'event_espresso'
1084
-                ),
1085
-                $widget_class,
1086
-                $widget_path . DS . $widget_class . $widget_ext
1087
-            );
1088
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1089
-            return;
1090
-        }
1091
-        // load the widget class file
1092
-        require_once($widget_path . DS . $widget_class . $widget_ext);
1093
-        // verify that class exists
1094
-        if (! class_exists($widget_class)) {
1095
-            $msg = sprintf(__('The requested %s widget class does not exist.', 'event_espresso'), $widget_class);
1096
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1097
-            return;
1098
-        }
1099
-        register_widget($widget_class);
1100
-        // add to array of registered widgets
1101
-        EE_Registry::instance()->widgets->{$widget_class} = $widget_path . DS . $widget_class . $widget_ext;
1102
-    }
1103
-
1104
-
1105
-    /**
1106
-     *        _register_modules
1107
-     *
1108
-     * @access private
1109
-     * @return array
1110
-     */
1111
-    private function _register_modules()
1112
-    {
1113
-        // grab list of installed modules
1114
-        $modules_to_register = glob(EE_MODULES . '*', GLOB_ONLYDIR);
1115
-        // filter list of modules to register
1116
-        $modules_to_register = apply_filters(
1117
-            'FHEE__EE_Config__register_modules__modules_to_register',
1118
-            $modules_to_register
1119
-        );
1120
-        if (! empty($modules_to_register)) {
1121
-            // loop through folders
1122
-            foreach ($modules_to_register as $module_path) {
1123
-                /**TEMPORARILY EXCLUDE gateways from modules for time being**/
1124
-                if ($module_path !== EE_MODULES . 'zzz-copy-this-module-template'
1125
-                    && $module_path !== EE_MODULES . 'gateways'
1126
-                ) {
1127
-                    // add to list of installed modules
1128
-                    EE_Config::register_module($module_path);
1129
-                }
1130
-            }
1131
-        }
1132
-        // filter list of installed modules
1133
-        return apply_filters(
1134
-            'FHEE__EE_Config___register_modules__installed_modules',
1135
-            EE_Registry::instance()->modules
1136
-        );
1137
-    }
1138
-
1139
-
1140
-    /**
1141
-     *    register_module - makes core aware of this module
1142
-     *
1143
-     * @access    public
1144
-     * @param    string $module_path - full path up to and including module folder
1145
-     * @return    bool
1146
-     */
1147
-    public static function register_module($module_path = null)
1148
-    {
1149
-        do_action('AHEE__EE_Config__register_module__begin', $module_path);
1150
-        $module_ext = '.module.php';
1151
-        // make all separators match
1152
-        $module_path = str_replace(array('\\', '/'), DS, $module_path);
1153
-        // does the file path INCLUDE the actual file name as part of the path ?
1154
-        if (strpos($module_path, $module_ext) !== false) {
1155
-            // grab and shortcode file name from directory name and break apart at dots
1156
-            $module_file = explode('.', basename($module_path));
1157
-            // now we need to rebuild the shortcode path
1158
-            $module_path = explode(DS, $module_path);
1159
-            // remove last segment
1160
-            array_pop($module_path);
1161
-            // glue it back together
1162
-            $module_path = implode(DS, $module_path) . DS;
1163
-            // take first segment from file name pieces and sanitize it
1164
-            $module = preg_replace('/[^a-zA-Z0-9_\-]/', '', $module_file[0]);
1165
-            // ensure class prefix is added
1166
-            $module_class = strpos($module, 'EED_') !== 0 ? 'EED_' . $module : $module;
1167
-        } else {
1168
-            // we need to generate the filename based off of the folder name
1169
-            // grab and sanitize module name
1170
-            $module = strtolower(basename($module_path));
1171
-            $module = preg_replace('/[^a-z0-9_\-]/', '', $module);
1172
-            // like trailingslashit()
1173
-            $module_path = rtrim($module_path, DS) . DS;
1174
-            // create classname from module directory name
1175
-            $module = str_replace(' ', '_', ucwords(str_replace('_', ' ', $module)));
1176
-            // add class prefix
1177
-            $module_class = 'EED_' . $module;
1178
-        }
1179
-        // does the module exist ?
1180
-        if (! is_readable($module_path . DS . $module_class . $module_ext)) {
1181
-            $msg = sprintf(
1182
-                __(
1183
-                    'The requested %s module file could not be found or is not readable due to file permissions.',
1184
-                    'event_espresso'
1185
-                ),
1186
-                $module
1187
-            );
1188
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1189
-            return false;
1190
-        }
1191
-        // load the module class file
1192
-        require_once($module_path . $module_class . $module_ext);
1193
-        // verify that class exists
1194
-        if (! class_exists($module_class)) {
1195
-            $msg = sprintf(__('The requested %s module class does not exist.', 'event_espresso'), $module_class);
1196
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1197
-            return false;
1198
-        }
1199
-        // add to array of registered modules
1200
-        EE_Registry::instance()->modules->{$module_class} = $module_path . $module_class . $module_ext;
1201
-        do_action(
1202
-            'AHEE__EE_Config__register_module__complete',
1203
-            $module_class,
1204
-            EE_Registry::instance()->modules->{$module_class}
1205
-        );
1206
-        return true;
1207
-    }
1208
-
1209
-
1210
-    /**
1211
-     *    _initialize_modules
1212
-     *    allow modules to set hooks for the rest of the system
1213
-     *
1214
-     * @access private
1215
-     * @return void
1216
-     */
1217
-    private function _initialize_modules()
1218
-    {
1219
-        // cycle thru shortcode folders
1220
-        foreach (EE_Registry::instance()->modules as $module_class => $module_path) {
1221
-            // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system
1222
-            // which set hooks ?
1223
-            if (is_admin()) {
1224
-                // fire immediately
1225
-                call_user_func(array($module_class, 'set_hooks_admin'));
1226
-            } else {
1227
-                // delay until other systems are online
1228
-                add_action(
1229
-                    'AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons',
1230
-                    array($module_class, 'set_hooks')
1231
-                );
1232
-            }
1233
-        }
1234
-    }
1235
-
1236
-
1237
-    /**
1238
-     *    register_route - adds module method routes to route_map
1239
-     *
1240
-     * @access    public
1241
-     * @param    string $route       - "pretty" public alias for module method
1242
-     * @param    string $module      - module name (classname without EED_ prefix)
1243
-     * @param    string $method_name - the actual module method to be routed to
1244
-     * @param    string $key         - url param key indicating a route is being called
1245
-     * @return    bool
1246
-     */
1247
-    public static function register_route($route = null, $module = null, $method_name = null, $key = 'ee')
1248
-    {
1249
-        do_action('AHEE__EE_Config__register_route__begin', $route, $module, $method_name);
1250
-        $module = str_replace('EED_', '', $module);
1251
-        $module_class = 'EED_' . $module;
1252
-        if (! isset(EE_Registry::instance()->modules->{$module_class})) {
1253
-            $msg = sprintf(__('The module %s has not been registered.', 'event_espresso'), $module);
1254
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1255
-            return false;
1256
-        }
1257
-        if (empty($route)) {
1258
-            $msg = sprintf(__('No route has been supplied.', 'event_espresso'), $route);
1259
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1260
-            return false;
1261
-        }
1262
-        if (! method_exists('EED_' . $module, $method_name)) {
1263
-            $msg = sprintf(
1264
-                __('A valid class method for the %s route has not been supplied.', 'event_espresso'),
1265
-                $route
1266
-            );
1267
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1268
-            return false;
1269
-        }
1270
-        EE_Config::$_module_route_map[ (string) $key ][ (string) $route ] = array('EED_' . $module, $method_name);
1271
-        return true;
1272
-    }
1273
-
1274
-
1275
-    /**
1276
-     *    get_route - get module method route
1277
-     *
1278
-     * @access    public
1279
-     * @param    string $route - "pretty" public alias for module method
1280
-     * @param    string $key   - url param key indicating a route is being called
1281
-     * @return    string
1282
-     */
1283
-    public static function get_route($route = null, $key = 'ee')
1284
-    {
1285
-        do_action('AHEE__EE_Config__get_route__begin', $route);
1286
-        $route = (string) apply_filters('FHEE__EE_Config__get_route', $route);
1287
-        if (isset(EE_Config::$_module_route_map[ $key ][ $route ])) {
1288
-            return EE_Config::$_module_route_map[ $key ][ $route ];
1289
-        }
1290
-        return null;
1291
-    }
1292
-
1293
-
1294
-    /**
1295
-     *    get_routes - get ALL module method routes
1296
-     *
1297
-     * @access    public
1298
-     * @return    array
1299
-     */
1300
-    public static function get_routes()
1301
-    {
1302
-        return EE_Config::$_module_route_map;
1303
-    }
1304
-
1305
-
1306
-    /**
1307
-     *    register_forward - allows modules to forward request to another module for further processing
1308
-     *
1309
-     * @access    public
1310
-     * @param    string       $route   - "pretty" public alias for module method
1311
-     * @param    integer      $status  - integer value corresponding  to status constant strings set in module parent
1312
-     *                                 class, allows different forwards to be served based on status
1313
-     * @param    array|string $forward - function name or array( class, method )
1314
-     * @param    string       $key     - url param key indicating a route is being called
1315
-     * @return    bool
1316
-     */
1317
-    public static function register_forward($route = null, $status = 0, $forward = null, $key = 'ee')
1318
-    {
1319
-        do_action('AHEE__EE_Config__register_forward', $route, $status, $forward);
1320
-        if (! isset(EE_Config::$_module_route_map[ $key ][ $route ]) || empty($route)) {
1321
-            $msg = sprintf(
1322
-                __('The module route %s for this forward has not been registered.', 'event_espresso'),
1323
-                $route
1324
-            );
1325
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1326
-            return false;
1327
-        }
1328
-        if (empty($forward)) {
1329
-            $msg = sprintf(__('No forwarding route has been supplied.', 'event_espresso'), $route);
1330
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1331
-            return false;
1332
-        }
1333
-        if (is_array($forward)) {
1334
-            if (! isset($forward[1])) {
1335
-                $msg = sprintf(
1336
-                    __('A class method for the %s forwarding route has not been supplied.', 'event_espresso'),
1337
-                    $route
1338
-                );
1339
-                EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1340
-                return false;
1341
-            }
1342
-            if (! method_exists($forward[0], $forward[1])) {
1343
-                $msg = sprintf(
1344
-                    __('The class method %s for the %s forwarding route is in invalid.', 'event_espresso'),
1345
-                    $forward[1],
1346
-                    $route
1347
-                );
1348
-                EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1349
-                return false;
1350
-            }
1351
-        } elseif (! function_exists($forward)) {
1352
-            $msg = sprintf(
1353
-                __('The function %s for the %s forwarding route is in invalid.', 'event_espresso'),
1354
-                $forward,
1355
-                $route
1356
-            );
1357
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1358
-            return false;
1359
-        }
1360
-        EE_Config::$_module_forward_map[ $key ][ $route ][ absint($status) ] = $forward;
1361
-        return true;
1362
-    }
1363
-
1364
-
1365
-    /**
1366
-     *    get_forward - get forwarding route
1367
-     *
1368
-     * @access    public
1369
-     * @param    string  $route  - "pretty" public alias for module method
1370
-     * @param    integer $status - integer value corresponding  to status constant strings set in module parent class,
1371
-     *                           allows different forwards to be served based on status
1372
-     * @param    string  $key    - url param key indicating a route is being called
1373
-     * @return    string
1374
-     */
1375
-    public static function get_forward($route = null, $status = 0, $key = 'ee')
1376
-    {
1377
-        do_action('AHEE__EE_Config__get_forward__begin', $route, $status);
1378
-        if (isset(EE_Config::$_module_forward_map[ $key ][ $route ][ $status ])) {
1379
-            return apply_filters(
1380
-                'FHEE__EE_Config__get_forward',
1381
-                EE_Config::$_module_forward_map[ $key ][ $route ][ $status ],
1382
-                $route,
1383
-                $status
1384
-            );
1385
-        }
1386
-        return null;
1387
-    }
1388
-
1389
-
1390
-    /**
1391
-     *    register_forward - allows modules to specify different view templates for different method routes and status
1392
-     *    results
1393
-     *
1394
-     * @access    public
1395
-     * @param    string  $route  - "pretty" public alias for module method
1396
-     * @param    integer $status - integer value corresponding  to status constant strings set in module parent class,
1397
-     *                           allows different views to be served based on status
1398
-     * @param    string  $view
1399
-     * @param    string  $key    - url param key indicating a route is being called
1400
-     * @return    bool
1401
-     */
1402
-    public static function register_view($route = null, $status = 0, $view = null, $key = 'ee')
1403
-    {
1404
-        do_action('AHEE__EE_Config__register_view__begin', $route, $status, $view);
1405
-        if (! isset(EE_Config::$_module_route_map[ $key ][ $route ]) || empty($route)) {
1406
-            $msg = sprintf(
1407
-                __('The module route %s for this view has not been registered.', 'event_espresso'),
1408
-                $route
1409
-            );
1410
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1411
-            return false;
1412
-        }
1413
-        if (! is_readable($view)) {
1414
-            $msg = sprintf(
1415
-                __(
1416
-                    'The %s view file could not be found or is not readable due to file permissions.',
1417
-                    'event_espresso'
1418
-                ),
1419
-                $view
1420
-            );
1421
-            EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1422
-            return false;
1423
-        }
1424
-        EE_Config::$_module_view_map[ $key ][ $route ][ absint($status) ] = $view;
1425
-        return true;
1426
-    }
1427
-
1428
-
1429
-    /**
1430
-     *    get_view - get view for route and status
1431
-     *
1432
-     * @access    public
1433
-     * @param    string  $route  - "pretty" public alias for module method
1434
-     * @param    integer $status - integer value corresponding  to status constant strings set in module parent class,
1435
-     *                           allows different views to be served based on status
1436
-     * @param    string  $key    - url param key indicating a route is being called
1437
-     * @return    string
1438
-     */
1439
-    public static function get_view($route = null, $status = 0, $key = 'ee')
1440
-    {
1441
-        do_action('AHEE__EE_Config__get_view__begin', $route, $status);
1442
-        if (isset(EE_Config::$_module_view_map[ $key ][ $route ][ $status ])) {
1443
-            return apply_filters(
1444
-                'FHEE__EE_Config__get_view',
1445
-                EE_Config::$_module_view_map[ $key ][ $route ][ $status ],
1446
-                $route,
1447
-                $status
1448
-            );
1449
-        }
1450
-        return null;
1451
-    }
1452
-
1453
-
1454
-    public function update_addon_option_names()
1455
-    {
1456
-        update_option(EE_Config::ADDON_OPTION_NAMES, $this->_addon_option_names);
1457
-    }
1458
-
1459
-
1460
-    public function shutdown()
1461
-    {
1462
-        $this->update_addon_option_names();
1463
-    }
1464
-
1465
-
1466
-    /**
1467
-     * @return LegacyShortcodesManager
1468
-     */
1469
-    public static function getLegacyShortcodesManager()
1470
-    {
1471
-
1472
-        if (! EE_Config::instance()->legacy_shortcodes_manager instanceof LegacyShortcodesManager) {
1473
-            EE_Config::instance()->legacy_shortcodes_manager = new LegacyShortcodesManager(
1474
-                EE_Registry::instance()
1475
-            );
1476
-        }
1477
-        return EE_Config::instance()->legacy_shortcodes_manager;
1478
-    }
1479
-
1480
-
1481
-    /**
1482
-     * register_shortcode - makes core aware of this shortcode
1483
-     *
1484
-     * @deprecated 4.9.26
1485
-     * @param    string $shortcode_path - full path up to and including shortcode folder
1486
-     * @return    bool
1487
-     */
1488
-    public static function register_shortcode($shortcode_path = null)
1489
-    {
1490
-        EE_Error::doing_it_wrong(
1491
-            __METHOD__,
1492
-            __(
1493
-                'Usage is deprecated. Use \EventEspresso\core\services\shortcodes\LegacyShortcodesManager::registerShortcode() as direct replacement, or better yet, please see the new \EventEspresso\core\services\shortcodes\ShortcodesManager class.',
1494
-                'event_espresso'
1495
-            ),
1496
-            '4.9.26'
1497
-        );
1498
-        return EE_Config::instance()->getLegacyShortcodesManager()->registerShortcode($shortcode_path);
1499
-    }
1500
-}
1501
-
1502
-/**
1503
- * Base class used for config classes. These classes should generally not have
1504
- * magic functions in use, except we'll allow them to magically set and get stuff...
1505
- * basically, they should just be well-defined stdClasses
1506
- */
1507
-class EE_Config_Base
1508
-{
1509
-
1510
-    /**
1511
-     * Utility function for escaping the value of a property and returning.
1512
-     *
1513
-     * @param string $property property name (checks to see if exists).
1514
-     * @return mixed if a detected type found return the escaped value, otherwise just the raw value is returned.
1515
-     * @throws \EE_Error
1516
-     */
1517
-    public function get_pretty($property)
1518
-    {
1519
-        if (! property_exists($this, $property)) {
1520
-            throw new EE_Error(
1521
-                sprintf(
1522
-                    __(
1523
-                        '%1$s::get_pretty() has been called with the property %2$s which does not exist on the %1$s config class.',
1524
-                        'event_espresso'
1525
-                    ),
1526
-                    get_class($this),
1527
-                    $property
1528
-                )
1529
-            );
1530
-        }
1531
-        // just handling escaping of strings for now.
1532
-        if (is_string($this->{$property})) {
1533
-            return stripslashes($this->{$property});
1534
-        }
1535
-        return $this->{$property};
1536
-    }
1537
-
1538
-
1539
-    public function populate()
1540
-    {
1541
-        // grab defaults via a new instance of this class.
1542
-        $class_name = get_class($this);
1543
-        $defaults = new $class_name;
1544
-        // loop through the properties for this class and see if they are set.  If they are NOT, then grab the
1545
-        // default from our $defaults object.
1546
-        foreach (get_object_vars($defaults) as $property => $value) {
1547
-            if ($this->{$property} === null) {
1548
-                $this->{$property} = $value;
1549
-            }
1550
-        }
1551
-        // cleanup
1552
-        unset($defaults);
1553
-    }
1554
-
1555
-
1556
-    /**
1557
-     *        __isset
1558
-     *
1559
-     * @param $a
1560
-     * @return bool
1561
-     */
1562
-    public function __isset($a)
1563
-    {
1564
-        return false;
1565
-    }
1566
-
1567
-
1568
-    /**
1569
-     *        __unset
1570
-     *
1571
-     * @param $a
1572
-     * @return bool
1573
-     */
1574
-    public function __unset($a)
1575
-    {
1576
-        return false;
1577
-    }
1578
-
1579
-
1580
-    /**
1581
-     *        __clone
1582
-     */
1583
-    public function __clone()
1584
-    {
1585
-    }
1586
-
1587
-
1588
-    /**
1589
-     *        __wakeup
1590
-     */
1591
-    public function __wakeup()
1592
-    {
1593
-    }
1594
-
1595
-
1596
-    /**
1597
-     *        __destruct
1598
-     */
1599
-    public function __destruct()
1600
-    {
1601
-    }
1602
-}
1603
-
1604
-/**
1605
- * Class for defining what's in the EE_Config relating to registration settings
1606
- */
1607
-class EE_Core_Config extends EE_Config_Base
1608
-{
1609
-
1610
-    const OPTION_NAME_UXIP = 'ee_ueip_optin';
1611
-
1612
-
1613
-    public $current_blog_id;
1614
-
1615
-    public $ee_ueip_optin;
1616
-
1617
-    public $ee_ueip_has_notified;
1618
-
1619
-    /**
1620
-     * Not to be confused with the 4 critical page variables (See
1621
-     * get_critical_pages_array()), this is just an array of wp posts that have EE
1622
-     * shortcodes in them. Keys are slugs, values are arrays with only 1 element: where the key is the shortcode
1623
-     * in the page, and the value is the page's ID. The key 'posts' is basically a duplicate of this same array.
1624
-     *
1625
-     * @var array
1626
-     */
1627
-    public $post_shortcodes;
1628
-
1629
-    public $module_route_map;
1630
-
1631
-    public $module_forward_map;
1632
-
1633
-    public $module_view_map;
1634
-
1635
-    /**
1636
-     * The next 4 vars are the IDs of critical EE pages.
1637
-     *
1638
-     * @var int
1639
-     */
1640
-    public $reg_page_id;
1641
-
1642
-    public $txn_page_id;
1643
-
1644
-    public $thank_you_page_id;
1645
-
1646
-    public $cancel_page_id;
1647
-
1648
-    /**
1649
-     * The next 4 vars are the URLs of critical EE pages.
1650
-     *
1651
-     * @var int
1652
-     */
1653
-    public $reg_page_url;
1654
-
1655
-    public $txn_page_url;
1656
-
1657
-    public $thank_you_page_url;
1658
-
1659
-    public $cancel_page_url;
1660
-
1661
-    /**
1662
-     * The next vars relate to the custom slugs for EE CPT routes
1663
-     */
1664
-    public $event_cpt_slug;
1665
-
1666
-    /**
1667
-     * This caches the _ee_ueip_option in case this config is reset in the same
1668
-     * request across blog switches in a multisite context.
1669
-     * Avoids extra queries to the db for this option.
1670
-     *
1671
-     * @var bool
1672
-     */
1673
-    public static $ee_ueip_option;
1674
-
1675
-
1676
-    /**
1677
-     *    class constructor
1678
-     *
1679
-     * @access    public
1680
-     */
1681
-    public function __construct()
1682
-    {
1683
-        // set default organization settings
1684
-        $this->current_blog_id = get_current_blog_id();
1685
-        $this->current_blog_id = $this->current_blog_id === null ? 1 : $this->current_blog_id;
1686
-        $this->ee_ueip_optin = $this->_get_main_ee_ueip_optin();
1687
-        $this->ee_ueip_has_notified = is_main_site() ? get_option('ee_ueip_has_notified', false) : true;
1688
-        $this->post_shortcodes = array();
1689
-        $this->module_route_map = array();
1690
-        $this->module_forward_map = array();
1691
-        $this->module_view_map = array();
1692
-        // critical EE page IDs
1693
-        $this->reg_page_id = 0;
1694
-        $this->txn_page_id = 0;
1695
-        $this->thank_you_page_id = 0;
1696
-        $this->cancel_page_id = 0;
1697
-        // critical EE page URLs
1698
-        $this->reg_page_url = '';
1699
-        $this->txn_page_url = '';
1700
-        $this->thank_you_page_url = '';
1701
-        $this->cancel_page_url = '';
1702
-        // cpt slugs
1703
-        $this->event_cpt_slug = __('events', 'event_espresso');
1704
-        // ueip constant check
1705
-        if (defined('EE_DISABLE_UXIP') && EE_DISABLE_UXIP) {
1706
-            $this->ee_ueip_optin = false;
1707
-            $this->ee_ueip_has_notified = true;
1708
-        }
1709
-    }
1710
-
1711
-
1712
-    /**
1713
-     * @return array
1714
-     */
1715
-    public function get_critical_pages_array()
1716
-    {
1717
-        return array(
1718
-            $this->reg_page_id,
1719
-            $this->txn_page_id,
1720
-            $this->thank_you_page_id,
1721
-            $this->cancel_page_id,
1722
-        );
1723
-    }
1724
-
1725
-
1726
-    /**
1727
-     * @return array
1728
-     */
1729
-    public function get_critical_pages_shortcodes_array()
1730
-    {
1731
-        return array(
1732
-            $this->reg_page_id       => 'ESPRESSO_CHECKOUT',
1733
-            $this->txn_page_id       => 'ESPRESSO_TXN_PAGE',
1734
-            $this->thank_you_page_id => 'ESPRESSO_THANK_YOU',
1735
-            $this->cancel_page_id    => 'ESPRESSO_CANCELLED',
1736
-        );
1737
-    }
1738
-
1739
-
1740
-    /**
1741
-     *  gets/returns URL for EE reg_page
1742
-     *
1743
-     * @access    public
1744
-     * @return    string
1745
-     */
1746
-    public function reg_page_url()
1747
-    {
1748
-        if (! $this->reg_page_url) {
1749
-            $this->reg_page_url = add_query_arg(
1750
-                array('uts' => time()),
1751
-                get_permalink($this->reg_page_id)
1752
-            ) . '#checkout';
1753
-        }
1754
-        return $this->reg_page_url;
1755
-    }
1756
-
1757
-
1758
-    /**
1759
-     *  gets/returns URL for EE txn_page
1760
-     *
1761
-     * @param array $query_args like what gets passed to
1762
-     *                          add_query_arg() as the first argument
1763
-     * @access    public
1764
-     * @return    string
1765
-     */
1766
-    public function txn_page_url($query_args = array())
1767
-    {
1768
-        if (! $this->txn_page_url) {
1769
-            $this->txn_page_url = get_permalink($this->txn_page_id);
1770
-        }
1771
-        if ($query_args) {
1772
-            return add_query_arg($query_args, $this->txn_page_url);
1773
-        } else {
1774
-            return $this->txn_page_url;
1775
-        }
1776
-    }
1777
-
1778
-
1779
-    /**
1780
-     *  gets/returns URL for EE thank_you_page
1781
-     *
1782
-     * @param array $query_args like what gets passed to
1783
-     *                          add_query_arg() as the first argument
1784
-     * @access    public
1785
-     * @return    string
1786
-     */
1787
-    public function thank_you_page_url($query_args = array())
1788
-    {
1789
-        if (! $this->thank_you_page_url) {
1790
-            $this->thank_you_page_url = get_permalink($this->thank_you_page_id);
1791
-        }
1792
-        if ($query_args) {
1793
-            return add_query_arg($query_args, $this->thank_you_page_url);
1794
-        } else {
1795
-            return $this->thank_you_page_url;
1796
-        }
1797
-    }
1798
-
1799
-
1800
-    /**
1801
-     *  gets/returns URL for EE cancel_page
1802
-     *
1803
-     * @access    public
1804
-     * @return    string
1805
-     */
1806
-    public function cancel_page_url()
1807
-    {
1808
-        if (! $this->cancel_page_url) {
1809
-            $this->cancel_page_url = get_permalink($this->cancel_page_id);
1810
-        }
1811
-        return $this->cancel_page_url;
1812
-    }
1813
-
1814
-
1815
-    /**
1816
-     * Resets all critical page urls to their original state.  Used primarily by the __sleep() magic method currently.
1817
-     *
1818
-     * @since 4.7.5
1819
-     */
1820
-    protected function _reset_urls()
1821
-    {
1822
-        $this->reg_page_url = '';
1823
-        $this->txn_page_url = '';
1824
-        $this->cancel_page_url = '';
1825
-        $this->thank_you_page_url = '';
1826
-    }
1827
-
1828
-
1829
-    /**
1830
-     * Used to return what the optin value is set for the EE User Experience Program.
1831
-     * This accounts for multisite and this value being requested for a subsite.  In multisite, the value is set
1832
-     * on the main site only.
1833
-     *
1834
-     * @return bool
1835
-     */
1836
-    protected function _get_main_ee_ueip_optin()
1837
-    {
1838
-        // if this is the main site then we can just bypass our direct query.
1839
-        if (is_main_site()) {
1840
-            return get_option(self::OPTION_NAME_UXIP, false);
1841
-        }
1842
-        // is this already cached for this request?  If so use it.
1843
-        if (EE_Core_Config::$ee_ueip_option !== null) {
1844
-            return EE_Core_Config::$ee_ueip_option;
1845
-        }
1846
-        global $wpdb;
1847
-        $current_network_main_site = is_multisite() ? get_current_site() : null;
1848
-        $current_main_site_id = ! empty($current_network_main_site) ? $current_network_main_site->blog_id : 1;
1849
-        $option = self::OPTION_NAME_UXIP;
1850
-        // set correct table for query
1851
-        $table_name = $wpdb->get_blog_prefix($current_main_site_id) . 'options';
1852
-        // rather than getting blog option for the $current_main_site_id, we do a direct $wpdb query because
1853
-        // get_blog_option() does a switch_to_blog an that could cause infinite recursion because EE_Core_Config might be
1854
-        // re-constructed on the blog switch.  Note, we are still executing any core wp filters on this option retrieval.
1855
-        // this bit of code is basically a direct copy of get_option without any caching because we are NOT switched to the blog
1856
-        // for the purpose of caching.
1857
-        $pre = apply_filters('pre_option_' . $option, false, $option);
1858
-        if (false !== $pre) {
1859
-            EE_Core_Config::$ee_ueip_option = $pre;
1860
-            return EE_Core_Config::$ee_ueip_option;
1861
-        }
1862
-        $row = $wpdb->get_row(
1863
-            $wpdb->prepare(
1864
-                "SELECT option_value FROM $table_name WHERE option_name = %s LIMIT 1",
1865
-                $option
1866
-            )
1867
-        );
1868
-        if (is_object($row)) {
1869
-            $value = $row->option_value;
1870
-        } else { // option does not exist so use default.
1871
-            EE_Core_Config::$ee_ueip_option =  apply_filters('default_option_' . $option, false, $option);
1872
-            return EE_Core_Config::$ee_ueip_option;
1873
-        }
1874
-        EE_Core_Config::$ee_ueip_option = apply_filters('option_' . $option, maybe_unserialize($value), $option);
1875
-        return EE_Core_Config::$ee_ueip_option;
1876
-    }
1877
-
1878
-
1879
-    /**
1880
-     * Utility function for escaping the value of a property and returning.
1881
-     *
1882
-     * @param string $property property name (checks to see if exists).
1883
-     * @return mixed if a detected type found return the escaped value, otherwise just the raw value is returned.
1884
-     * @throws \EE_Error
1885
-     */
1886
-    public function get_pretty($property)
1887
-    {
1888
-        if ($property === self::OPTION_NAME_UXIP) {
1889
-            return $this->ee_ueip_optin ? 'yes' : 'no';
1890
-        }
1891
-        return parent::get_pretty($property);
1892
-    }
1893
-
1894
-
1895
-    /**
1896
-     * Currently used to ensure critical page urls have initial values saved to the db instead of any current set values
1897
-     * on the object.
1898
-     *
1899
-     * @return array
1900
-     */
1901
-    public function __sleep()
1902
-    {
1903
-        // reset all url properties
1904
-        $this->_reset_urls();
1905
-        // return what to save to db
1906
-        return array_keys(get_object_vars($this));
1907
-    }
1908
-}
1909
-
1910
-/**
1911
- * Config class for storing info on the Organization
1912
- */
1913
-class EE_Organization_Config extends EE_Config_Base
1914
-{
1915
-
1916
-    /**
1917
-     * @var string $name
1918
-     * eg EE4.1
1919
-     */
1920
-    public $name;
1921
-
1922
-    /**
1923
-     * @var string $address_1
1924
-     * eg 123 Onna Road
1925
-     */
1926
-    public $address_1 = '';
1927
-
1928
-    /**
1929
-     * @var string $address_2
1930
-     * eg PO Box 123
1931
-     */
1932
-    public $address_2 = '';
1933
-
1934
-    /**
1935
-     * @var string $city
1936
-     * eg Inna City
1937
-     */
1938
-    public $city = '';
1939
-
1940
-    /**
1941
-     * @var int $STA_ID
1942
-     * eg 4
1943
-     */
1944
-    public $STA_ID = 0;
1945
-
1946
-    /**
1947
-     * @var string $CNT_ISO
1948
-     * eg US
1949
-     */
1950
-    public $CNT_ISO = '';
1951
-
1952
-    /**
1953
-     * @var string $zip
1954
-     * eg 12345  or V1A 2B3
1955
-     */
1956
-    public $zip = '';
1957
-
1958
-    /**
1959
-     * @var string $email
1960
-     * eg [email protected]
1961
-     */
1962
-    public $email;
1963
-
1964
-    /**
1965
-     * @var string $phone
1966
-     * eg. 111-111-1111
1967
-     */
1968
-    public $phone = '';
1969
-
1970
-    /**
1971
-     * @var string $vat
1972
-     * VAT/Tax Number
1973
-     */
1974
-    public $vat = '';
1975
-
1976
-    /**
1977
-     * @var string $logo_url
1978
-     * eg http://www.somedomain.com/wp-content/uploads/kittehs.jpg
1979
-     */
1980
-    public $logo_url = '';
1981
-
1982
-    /**
1983
-     * The below are all various properties for holding links to organization social network profiles
1984
-     *
1985
-     * @var string
1986
-     */
1987
-    /**
1988
-     * facebook (facebook.com/profile.name)
1989
-     *
1990
-     * @var string
1991
-     */
1992
-    public $facebook = '';
1993
-
1994
-    /**
1995
-     * twitter (twitter.com/twitter_handle)
1996
-     *
1997
-     * @var string
1998
-     */
1999
-    public $twitter = '';
2000
-
2001
-    /**
2002
-     * linkedin (linkedin.com/in/profile_name)
2003
-     *
2004
-     * @var string
2005
-     */
2006
-    public $linkedin = '';
2007
-
2008
-    /**
2009
-     * pinterest (www.pinterest.com/profile_name)
2010
-     *
2011
-     * @var string
2012
-     */
2013
-    public $pinterest = '';
2014
-
2015
-    /**
2016
-     * google+ (google.com/+profileName)
2017
-     *
2018
-     * @var string
2019
-     */
2020
-    public $google = '';
2021
-
2022
-    /**
2023
-     * instagram (instagram.com/handle)
2024
-     *
2025
-     * @var string
2026
-     */
2027
-    public $instagram = '';
2028
-
2029
-
2030
-    /**
2031
-     *    class constructor
2032
-     *
2033
-     * @access    public
2034
-     */
2035
-    public function __construct()
2036
-    {
2037
-        // set default organization settings
2038
-        // decode HTML entities from the WP blogname, because it's stored in the DB with HTML entities encoded
2039
-        $this->name = wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES);
2040
-        $this->email = get_bloginfo('admin_email');
2041
-    }
2042
-}
2043
-
2044
-/**
2045
- * Class for defining what's in the EE_Config relating to currency
2046
- */
2047
-class EE_Currency_Config extends EE_Config_Base
2048
-{
2049
-
2050
-    /**
2051
-     * @var string $code
2052
-     * eg 'US'
2053
-     */
2054
-    public $code;
2055
-
2056
-    /**
2057
-     * @var string $name
2058
-     * eg 'Dollar'
2059
-     */
2060
-    public $name;
2061
-
2062
-    /**
2063
-     * plural name
2064
-     *
2065
-     * @var string $plural
2066
-     * eg 'Dollars'
2067
-     */
2068
-    public $plural;
2069
-
2070
-    /**
2071
-     * currency sign
2072
-     *
2073
-     * @var string $sign
2074
-     * eg '$'
2075
-     */
2076
-    public $sign;
2077
-
2078
-    /**
2079
-     * Whether the currency sign should come before the number or not
2080
-     *
2081
-     * @var boolean $sign_b4
2082
-     */
2083
-    public $sign_b4;
2084
-
2085
-    /**
2086
-     * How many digits should come after the decimal place
2087
-     *
2088
-     * @var int $dec_plc
2089
-     */
2090
-    public $dec_plc;
2091
-
2092
-    /**
2093
-     * Symbol to use for decimal mark
2094
-     *
2095
-     * @var string $dec_mrk
2096
-     * eg '.'
2097
-     */
2098
-    public $dec_mrk;
2099
-
2100
-    /**
2101
-     * Symbol to use for thousands
2102
-     *
2103
-     * @var string $thsnds
2104
-     * eg ','
2105
-     */
2106
-    public $thsnds;
2107
-
2108
-
2109
-    /**
2110
-     *    class constructor
2111
-     *
2112
-     * @access    public
2113
-     * @param string $CNT_ISO
2114
-     * @throws \EE_Error
2115
-     */
2116
-    public function __construct($CNT_ISO = '')
2117
-    {
2118
-        /** @var \EventEspresso\core\services\database\TableAnalysis $table_analysis */
2119
-        $table_analysis = EE_Registry::instance()->create('TableAnalysis', array(), true);
2120
-        // get country code from organization settings or use default
2121
-        $ORG_CNT = isset(EE_Registry::instance()->CFG->organization)
2122
-                   && EE_Registry::instance()->CFG->organization instanceof EE_Organization_Config
2123
-            ? EE_Registry::instance()->CFG->organization->CNT_ISO
2124
-            : '';
2125
-        // but override if requested
2126
-        $CNT_ISO = ! empty($CNT_ISO) ? $CNT_ISO : $ORG_CNT;
2127
-        // so if that all went well, and we are not in M-Mode (cuz you can't query the db in M-Mode) and double-check the countries table exists
2128
-        if (! empty($CNT_ISO)
2129
-            && EE_Maintenance_Mode::instance()->models_can_query()
2130
-            && $table_analysis->tableExists(EE_Registry::instance()->load_model('Country')->table())
2131
-        ) {
2132
-            // retrieve the country settings from the db, just in case they have been customized
2133
-            $country = EE_Registry::instance()->load_model('Country')->get_one_by_ID($CNT_ISO);
2134
-            if ($country instanceof EE_Country) {
2135
-                $this->code = $country->currency_code();    // currency code: USD, CAD, EUR
2136
-                $this->name = $country->currency_name_single();    // Dollar
2137
-                $this->plural = $country->currency_name_plural();    // Dollars
2138
-                $this->sign = $country->currency_sign();            // currency sign: $
2139
-                $this->sign_b4 = $country->currency_sign_before(
2140
-                );        // currency sign before or after: $TRUE  or  FALSE$
2141
-                $this->dec_plc = $country->currency_decimal_places();    // decimal places: 2 = 0.00  3 = 0.000
2142
-                $this->dec_mrk = $country->currency_decimal_mark(
2143
-                );    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
2144
-                $this->thsnds = $country->currency_thousands_separator(
2145
-                );    // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
2146
-            }
2147
-        }
2148
-        // fallback to hardcoded defaults, in case the above failed
2149
-        if (empty($this->code)) {
2150
-            // set default currency settings
2151
-            $this->code = 'USD';    // currency code: USD, CAD, EUR
2152
-            $this->name = __('Dollar', 'event_espresso');    // Dollar
2153
-            $this->plural = __('Dollars', 'event_espresso');    // Dollars
2154
-            $this->sign = '$';    // currency sign: $
2155
-            $this->sign_b4 = true;    // currency sign before or after: $TRUE  or  FALSE$
2156
-            $this->dec_plc = 2;    // decimal places: 2 = 0.00  3 = 0.000
2157
-            $this->dec_mrk = '.';    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
2158
-            $this->thsnds = ',';    // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
2159
-        }
2160
-    }
2161
-}
2162
-
2163
-/**
2164
- * Class for defining what's in the EE_Config relating to registration settings
2165
- */
2166
-class EE_Registration_Config extends EE_Config_Base
2167
-{
2168
-
2169
-    /**
2170
-     * Default registration status
2171
-     *
2172
-     * @var string $default_STS_ID
2173
-     * eg 'RPP'
2174
-     */
2175
-    public $default_STS_ID;
2176
-
2177
-    /**
2178
-     * For new events, this will be the default value for the maximum number of tickets (equivalent to maximum number of
2179
-     * registrations)
2180
-     *
2181
-     * @var int
2182
-     */
2183
-    public $default_maximum_number_of_tickets;
2184
-
2185
-    /**
2186
-     * level of validation to apply to email addresses
2187
-     *
2188
-     * @var string $email_validation_level
2189
-     * options: 'basic', 'wp_default', 'i18n', 'i18n_dns'
2190
-     */
2191
-    public $email_validation_level;
2192
-
2193
-    /**
2194
-     *    whether or not to show alternate payment options during the reg process if payment status is pending
2195
-     *
2196
-     * @var boolean $show_pending_payment_options
2197
-     */
2198
-    public $show_pending_payment_options;
2199
-
2200
-    /**
2201
-     * Whether to skip the registration confirmation page
2202
-     *
2203
-     * @var boolean $skip_reg_confirmation
2204
-     */
2205
-    public $skip_reg_confirmation;
2206
-
2207
-    /**
2208
-     * an array of SPCO reg steps where:
2209
-     *        the keys denotes the reg step order
2210
-     *        each element consists of an array with the following elements:
2211
-     *            "file_path" => the file path to the EE_SPCO_Reg_Step class
2212
-     *            "class_name" => the specific EE_SPCO_Reg_Step child class name
2213
-     *            "slug" => the URL param used to trigger the reg step
2214
-     *
2215
-     * @var array $reg_steps
2216
-     */
2217
-    public $reg_steps;
2218
-
2219
-    /**
2220
-     * Whether registration confirmation should be the last page of SPCO
2221
-     *
2222
-     * @var boolean $reg_confirmation_last
2223
-     */
2224
-    public $reg_confirmation_last;
2225
-
2226
-    /**
2227
-     * Whether or not to enable the EE Bot Trap
2228
-     *
2229
-     * @var boolean $use_bot_trap
2230
-     */
2231
-    public $use_bot_trap;
2232
-
2233
-    /**
2234
-     * Whether or not to encrypt some data sent by the EE Bot Trap
2235
-     *
2236
-     * @var boolean $use_encryption
2237
-     */
2238
-    public $use_encryption;
2239
-
2240
-    /**
2241
-     * Whether or not to use ReCaptcha
2242
-     *
2243
-     * @var boolean $use_captcha
2244
-     */
2245
-    public $use_captcha;
2246
-
2247
-    /**
2248
-     * ReCaptcha Theme
2249
-     *
2250
-     * @var string $recaptcha_theme
2251
-     *    options: 'dark', 'light', 'invisible'
2252
-     */
2253
-    public $recaptcha_theme;
2254
-
2255
-    /**
2256
-     * ReCaptcha Badge - determines the position of the reCAPTCHA badge if using Invisible ReCaptcha.
2257
-     *
2258
-     * @var string $recaptcha_badge
2259
-     *    options: 'bottomright', 'bottomleft', 'inline'
2260
-     */
2261
-    public $recaptcha_badge;
17
+	const OPTION_NAME = 'ee_config';
18
+
19
+	const LOG_NAME = 'ee_config_log';
20
+
21
+	const LOG_LENGTH = 100;
22
+
23
+	const ADDON_OPTION_NAMES = 'ee_config_option_names';
24
+
25
+	/**
26
+	 *    instance of the EE_Config object
27
+	 *
28
+	 * @var    EE_Config $_instance
29
+	 * @access    private
30
+	 */
31
+	private static $_instance;
32
+
33
+	/**
34
+	 * @var boolean $_logging_enabled
35
+	 */
36
+	private static $_logging_enabled = false;
37
+
38
+	/**
39
+	 * @var LegacyShortcodesManager $legacy_shortcodes_manager
40
+	 */
41
+	private $legacy_shortcodes_manager;
42
+
43
+	/**
44
+	 * An StdClass whose property names are addon slugs,
45
+	 * and values are their config classes
46
+	 *
47
+	 * @var StdClass
48
+	 */
49
+	public $addons;
50
+
51
+	/**
52
+	 * @var EE_Admin_Config
53
+	 */
54
+	public $admin;
55
+
56
+	/**
57
+	 * @var EE_Core_Config
58
+	 */
59
+	public $core;
60
+
61
+	/**
62
+	 * @var EE_Currency_Config
63
+	 */
64
+	public $currency;
65
+
66
+	/**
67
+	 * @var EE_Organization_Config
68
+	 */
69
+	public $organization;
70
+
71
+	/**
72
+	 * @var EE_Registration_Config
73
+	 */
74
+	public $registration;
75
+
76
+	/**
77
+	 * @var EE_Template_Config
78
+	 */
79
+	public $template_settings;
80
+
81
+	/**
82
+	 * Holds EE environment values.
83
+	 *
84
+	 * @var EE_Environment_Config
85
+	 */
86
+	public $environment;
87
+
88
+	/**
89
+	 * settings pertaining to Google maps
90
+	 *
91
+	 * @var EE_Map_Config
92
+	 */
93
+	public $map_settings;
94
+
95
+	/**
96
+	 * settings pertaining to Taxes
97
+	 *
98
+	 * @var EE_Tax_Config
99
+	 */
100
+	public $tax_settings;
101
+
102
+	/**
103
+	 * Settings pertaining to global messages settings.
104
+	 *
105
+	 * @var EE_Messages_Config
106
+	 */
107
+	public $messages;
108
+
109
+	/**
110
+	 * @deprecated
111
+	 * @var EE_Gateway_Config
112
+	 */
113
+	public $gateway;
114
+
115
+	/**
116
+	 * @var    array $_addon_option_names
117
+	 * @access    private
118
+	 */
119
+	private $_addon_option_names = array();
120
+
121
+	/**
122
+	 * @var    array $_module_route_map
123
+	 * @access    private
124
+	 */
125
+	private static $_module_route_map = array();
126
+
127
+	/**
128
+	 * @var    array $_module_forward_map
129
+	 * @access    private
130
+	 */
131
+	private static $_module_forward_map = array();
132
+
133
+	/**
134
+	 * @var    array $_module_view_map
135
+	 * @access    private
136
+	 */
137
+	private static $_module_view_map = array();
138
+
139
+
140
+	/**
141
+	 * @singleton method used to instantiate class object
142
+	 * @access    public
143
+	 * @return EE_Config instance
144
+	 */
145
+	public static function instance()
146
+	{
147
+		// check if class object is instantiated, and instantiated properly
148
+		if (! self::$_instance instanceof EE_Config) {
149
+			self::$_instance = new self();
150
+		}
151
+		return self::$_instance;
152
+	}
153
+
154
+
155
+	/**
156
+	 * Resets the config
157
+	 *
158
+	 * @param bool    $hard_reset    if TRUE, sets EE_CONFig back to its original settings in the database. If FALSE
159
+	 *                               (default) leaves the database alone, and merely resets the EE_Config object to
160
+	 *                               reflect its state in the database
161
+	 * @param boolean $reinstantiate if TRUE (default) call instance() and return it. Otherwise, just leave
162
+	 *                               $_instance as NULL. Useful in case you want to forget about the old instance on
163
+	 *                               EE_Config, but might not be ready to instantiate EE_Config currently (eg if the
164
+	 *                               site was put into maintenance mode)
165
+	 * @return EE_Config
166
+	 */
167
+	public static function reset($hard_reset = false, $reinstantiate = true)
168
+	{
169
+		if (self::$_instance instanceof EE_Config) {
170
+			if ($hard_reset) {
171
+				self::$_instance->legacy_shortcodes_manager = null;
172
+				self::$_instance->_addon_option_names = array();
173
+				self::$_instance->_initialize_config();
174
+				self::$_instance->update_espresso_config();
175
+			}
176
+			self::$_instance->update_addon_option_names();
177
+		}
178
+		self::$_instance = null;
179
+		// we don't need to reset the static properties imo because those should
180
+		// only change when a module is added or removed. Currently we don't
181
+		// support removing a module during a request when it previously existed
182
+		if ($reinstantiate) {
183
+			return self::instance();
184
+		} else {
185
+			return null;
186
+		}
187
+	}
188
+
189
+
190
+	/**
191
+	 *    class constructor
192
+	 *
193
+	 * @access    private
194
+	 */
195
+	private function __construct()
196
+	{
197
+		do_action('AHEE__EE_Config__construct__begin', $this);
198
+		EE_Config::$_logging_enabled = apply_filters('FHEE__EE_Config___construct__logging_enabled', false);
199
+		// setup empty config classes
200
+		$this->_initialize_config();
201
+		// load existing EE site settings
202
+		$this->_load_core_config();
203
+		// confirm everything loaded correctly and set filtered defaults if not
204
+		$this->_verify_config();
205
+		//  register shortcodes and modules
206
+		add_action(
207
+			'AHEE__EE_System__register_shortcodes_modules_and_widgets',
208
+			array($this, 'register_shortcodes_and_modules'),
209
+			999
210
+		);
211
+		//  initialize shortcodes and modules
212
+		add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'initialize_shortcodes_and_modules'));
213
+		// register widgets
214
+		add_action('widgets_init', array($this, 'widgets_init'), 10);
215
+		// shutdown
216
+		add_action('shutdown', array($this, 'shutdown'), 10);
217
+		// construct__end hook
218
+		do_action('AHEE__EE_Config__construct__end', $this);
219
+		// hardcoded hack
220
+		$this->template_settings->current_espresso_theme = 'Espresso_Arabica_2014';
221
+	}
222
+
223
+
224
+	/**
225
+	 * @return boolean
226
+	 */
227
+	public static function logging_enabled()
228
+	{
229
+		return self::$_logging_enabled;
230
+	}
231
+
232
+
233
+	/**
234
+	 * use to get the current theme if needed from static context
235
+	 *
236
+	 * @return string current theme set.
237
+	 */
238
+	public static function get_current_theme()
239
+	{
240
+		return isset(self::$_instance->template_settings->current_espresso_theme)
241
+			? self::$_instance->template_settings->current_espresso_theme : 'Espresso_Arabica_2014';
242
+	}
243
+
244
+
245
+	/**
246
+	 *        _initialize_config
247
+	 *
248
+	 * @access private
249
+	 * @return void
250
+	 */
251
+	private function _initialize_config()
252
+	{
253
+		EE_Config::trim_log();
254
+		// set defaults
255
+		$this->_addon_option_names = get_option(EE_Config::ADDON_OPTION_NAMES, array());
256
+		$this->addons = new stdClass();
257
+		// set _module_route_map
258
+		EE_Config::$_module_route_map = array();
259
+		// set _module_forward_map
260
+		EE_Config::$_module_forward_map = array();
261
+		// set _module_view_map
262
+		EE_Config::$_module_view_map = array();
263
+	}
264
+
265
+
266
+	/**
267
+	 *        load core plugin configuration
268
+	 *
269
+	 * @access private
270
+	 * @return void
271
+	 */
272
+	private function _load_core_config()
273
+	{
274
+		// load_core_config__start hook
275
+		do_action('AHEE__EE_Config___load_core_config__start', $this);
276
+		$espresso_config = $this->get_espresso_config();
277
+		foreach ($espresso_config as $config => $settings) {
278
+			// load_core_config__start hook
279
+			$settings = apply_filters(
280
+				'FHEE__EE_Config___load_core_config__config_settings',
281
+				$settings,
282
+				$config,
283
+				$this
284
+			);
285
+			if (is_object($settings) && property_exists($this, $config)) {
286
+				$this->{$config} = apply_filters('FHEE__EE_Config___load_core_config__' . $config, $settings);
287
+				// call configs populate method to ensure any defaults are set for empty values.
288
+				if (method_exists($settings, 'populate')) {
289
+					$this->{$config}->populate();
290
+				}
291
+				if (method_exists($settings, 'do_hooks')) {
292
+					$this->{$config}->do_hooks();
293
+				}
294
+			}
295
+		}
296
+		if (apply_filters('FHEE__EE_Config___load_core_config__update_espresso_config', false)) {
297
+			$this->update_espresso_config();
298
+		}
299
+		// load_core_config__end hook
300
+		do_action('AHEE__EE_Config___load_core_config__end', $this);
301
+	}
302
+
303
+
304
+	/**
305
+	 *    _verify_config
306
+	 *
307
+	 * @access    protected
308
+	 * @return    void
309
+	 */
310
+	protected function _verify_config()
311
+	{
312
+		$this->core = $this->core instanceof EE_Core_Config
313
+			? $this->core
314
+			: new EE_Core_Config();
315
+		$this->core = apply_filters('FHEE__EE_Config___initialize_config__core', $this->core);
316
+		$this->organization = $this->organization instanceof EE_Organization_Config
317
+			? $this->organization
318
+			: new EE_Organization_Config();
319
+		$this->organization = apply_filters(
320
+			'FHEE__EE_Config___initialize_config__organization',
321
+			$this->organization
322
+		);
323
+		$this->currency = $this->currency instanceof EE_Currency_Config
324
+			? $this->currency
325
+			: new EE_Currency_Config();
326
+		$this->currency = apply_filters('FHEE__EE_Config___initialize_config__currency', $this->currency);
327
+		$this->registration = $this->registration instanceof EE_Registration_Config
328
+			? $this->registration
329
+			: new EE_Registration_Config();
330
+		$this->registration = apply_filters(
331
+			'FHEE__EE_Config___initialize_config__registration',
332
+			$this->registration
333
+		);
334
+		$this->admin = $this->admin instanceof EE_Admin_Config
335
+			? $this->admin
336
+			: new EE_Admin_Config();
337
+		$this->admin = apply_filters('FHEE__EE_Config___initialize_config__admin', $this->admin);
338
+		$this->template_settings = $this->template_settings instanceof EE_Template_Config
339
+			? $this->template_settings
340
+			: new EE_Template_Config();
341
+		$this->template_settings = apply_filters(
342
+			'FHEE__EE_Config___initialize_config__template_settings',
343
+			$this->template_settings
344
+		);
345
+		$this->map_settings = $this->map_settings instanceof EE_Map_Config
346
+			? $this->map_settings
347
+			: new EE_Map_Config();
348
+		$this->map_settings = apply_filters(
349
+			'FHEE__EE_Config___initialize_config__map_settings',
350
+			$this->map_settings
351
+		);
352
+		$this->environment = $this->environment instanceof EE_Environment_Config
353
+			? $this->environment
354
+			: new EE_Environment_Config();
355
+		$this->environment = apply_filters(
356
+			'FHEE__EE_Config___initialize_config__environment',
357
+			$this->environment
358
+		);
359
+		$this->tax_settings = $this->tax_settings instanceof EE_Tax_Config
360
+			? $this->tax_settings
361
+			: new EE_Tax_Config();
362
+		$this->tax_settings = apply_filters(
363
+			'FHEE__EE_Config___initialize_config__tax_settings',
364
+			$this->tax_settings
365
+		);
366
+		$this->messages = apply_filters('FHEE__EE_Config__initialize_config__messages', $this->messages);
367
+		$this->messages = $this->messages instanceof EE_Messages_Config
368
+			? $this->messages
369
+			: new EE_Messages_Config();
370
+		$this->gateway = $this->gateway instanceof EE_Gateway_Config
371
+			? $this->gateway
372
+			: new EE_Gateway_Config();
373
+		$this->gateway = apply_filters('FHEE__EE_Config___initialize_config__gateway', $this->gateway);
374
+		$this->legacy_shortcodes_manager = null;
375
+	}
376
+
377
+
378
+	/**
379
+	 *    get_espresso_config
380
+	 *
381
+	 * @access    public
382
+	 * @return    array of espresso config stuff
383
+	 */
384
+	public function get_espresso_config()
385
+	{
386
+		// grab espresso configuration
387
+		return apply_filters(
388
+			'FHEE__EE_Config__get_espresso_config__CFG',
389
+			get_option(EE_Config::OPTION_NAME, array())
390
+		);
391
+	}
392
+
393
+
394
+	/**
395
+	 *    double_check_config_comparison
396
+	 *
397
+	 * @access    public
398
+	 * @param string $option
399
+	 * @param        $old_value
400
+	 * @param        $value
401
+	 */
402
+	public function double_check_config_comparison($option = '', $old_value, $value)
403
+	{
404
+		// make sure we're checking the ee config
405
+		if ($option === EE_Config::OPTION_NAME) {
406
+			// run a loose comparison of the old value against the new value for type and properties,
407
+			// but NOT exact instance like WP update_option does (ie: NOT type safe comparison)
408
+			if ($value != $old_value) {
409
+				// if they are NOT the same, then remove the hook,
410
+				// which means the subsequent update results will be based solely on the update query results
411
+				// the reason we do this is because, as stated above,
412
+				// WP update_option performs an exact instance comparison (===) on any update values passed to it
413
+				// this happens PRIOR to serialization and any subsequent update.
414
+				// If values are found to match their previous old value,
415
+				// then WP bails before performing any update.
416
+				// Since we are passing the EE_Config object, it is comparing the EXACT instance of the saved version
417
+				// it just pulled from the db, with the one being passed to it (which will not match).
418
+				// HOWEVER, once the object is serialized and passed off to MySQL to update,
419
+				// MySQL MAY ALSO NOT perform the update because
420
+				// the string it sees in the db looks the same as the new one it has been passed!!!
421
+				// This results in the query returning an "affected rows" value of ZERO,
422
+				// which gets returned immediately by WP update_option and looks like an error.
423
+				remove_action('update_option', array($this, 'check_config_updated'));
424
+			}
425
+		}
426
+	}
427
+
428
+
429
+	/**
430
+	 *    update_espresso_config
431
+	 *
432
+	 * @access   public
433
+	 */
434
+	protected function _reset_espresso_addon_config()
435
+	{
436
+		$this->_addon_option_names = array();
437
+		foreach ($this->addons as $addon_name => $addon_config_obj) {
438
+			$addon_config_obj = maybe_unserialize($addon_config_obj);
439
+			if ($addon_config_obj instanceof EE_Config_Base) {
440
+				$this->update_config('addons', $addon_name, $addon_config_obj, false);
441
+			}
442
+			$this->addons->{$addon_name} = null;
443
+		}
444
+	}
445
+
446
+
447
+	/**
448
+	 *    update_espresso_config
449
+	 *
450
+	 * @access   public
451
+	 * @param   bool $add_success
452
+	 * @param   bool $add_error
453
+	 * @return   bool
454
+	 */
455
+	public function update_espresso_config($add_success = false, $add_error = true)
456
+	{
457
+		// don't allow config updates during WP heartbeats
458
+		if (\EE_Registry::instance()->REQ->get('action', '') === 'heartbeat') {
459
+			return false;
460
+		}
461
+		// commented out the following re: https://events.codebasehq.com/projects/event-espresso/tickets/8197
462
+		// $clone = clone( self::$_instance );
463
+		// self::$_instance = NULL;
464
+		do_action('AHEE__EE_Config__update_espresso_config__begin', $this);
465
+		$this->_reset_espresso_addon_config();
466
+		// hook into update_option because that happens AFTER the ( $value === $old_value ) conditional
467
+		// but BEFORE the actual update occurs
468
+		add_action('update_option', array($this, 'double_check_config_comparison'), 1, 3);
469
+		// don't want to persist legacy_shortcodes_manager, but don't want to lose it either
470
+		$legacy_shortcodes_manager = $this->legacy_shortcodes_manager;
471
+		$this->legacy_shortcodes_manager = null;
472
+		// now update "ee_config"
473
+		$saved = update_option(EE_Config::OPTION_NAME, $this);
474
+		$this->legacy_shortcodes_manager = $legacy_shortcodes_manager;
475
+		EE_Config::log(EE_Config::OPTION_NAME);
476
+		// if not saved... check if the hook we just added still exists;
477
+		// if it does, it means one of two things:
478
+		// that update_option bailed at the($value === $old_value) conditional,
479
+		// or...
480
+		// the db update query returned 0 rows affected
481
+		// (probably because the data  value was the same from it's perspective)
482
+		// so the existence of the hook means that a negative result from update_option is NOT an error,
483
+		// but just means no update occurred, so don't display an error to the user.
484
+		// BUT... if update_option returns FALSE, AND the hook is missing,
485
+		// then it means that something truly went wrong
486
+		$saved = ! $saved ? has_action('update_option', array($this, 'double_check_config_comparison')) : $saved;
487
+		// remove our action since we don't want it in the system anymore
488
+		remove_action('update_option', array($this, 'double_check_config_comparison'), 1);
489
+		do_action('AHEE__EE_Config__update_espresso_config__end', $this, $saved);
490
+		// self::$_instance = $clone;
491
+		// unset( $clone );
492
+		// if config remains the same or was updated successfully
493
+		if ($saved) {
494
+			if ($add_success) {
495
+				EE_Error::add_success(
496
+					__('The Event Espresso Configuration Settings have been successfully updated.', 'event_espresso'),
497
+					__FILE__,
498
+					__FUNCTION__,
499
+					__LINE__
500
+				);
501
+			}
502
+			return true;
503
+		} else {
504
+			if ($add_error) {
505
+				EE_Error::add_error(
506
+					__('The Event Espresso Configuration Settings were not updated.', 'event_espresso'),
507
+					__FILE__,
508
+					__FUNCTION__,
509
+					__LINE__
510
+				);
511
+			}
512
+			return false;
513
+		}
514
+	}
515
+
516
+
517
+	/**
518
+	 *    _verify_config_params
519
+	 *
520
+	 * @access    private
521
+	 * @param    string         $section
522
+	 * @param    string         $name
523
+	 * @param    string         $config_class
524
+	 * @param    EE_Config_Base $config_obj
525
+	 * @param    array          $tests_to_run
526
+	 * @param    bool           $display_errors
527
+	 * @return    bool    TRUE on success, FALSE on fail
528
+	 */
529
+	private function _verify_config_params(
530
+		$section = '',
531
+		$name = '',
532
+		$config_class = '',
533
+		$config_obj = null,
534
+		$tests_to_run = array(1, 2, 3, 4, 5, 6, 7, 8),
535
+		$display_errors = true
536
+	) {
537
+		try {
538
+			foreach ($tests_to_run as $test) {
539
+				switch ($test) {
540
+					// TEST #1 : check that section was set
541
+					case 1:
542
+						if (empty($section)) {
543
+							if ($display_errors) {
544
+								throw new EE_Error(
545
+									sprintf(
546
+										__(
547
+											'No configuration section has been provided while attempting to save "%s".',
548
+											'event_espresso'
549
+										),
550
+										$config_class
551
+									)
552
+								);
553
+							}
554
+							return false;
555
+						}
556
+						break;
557
+					// TEST #2 : check that settings section exists
558
+					case 2:
559
+						if (! isset($this->{$section})) {
560
+							if ($display_errors) {
561
+								throw new EE_Error(
562
+									sprintf(
563
+										__('The "%s" configuration section does not exist.', 'event_espresso'),
564
+										$section
565
+									)
566
+								);
567
+							}
568
+							return false;
569
+						}
570
+						break;
571
+					// TEST #3 : check that section is the proper format
572
+					case 3:
573
+						if (! ($this->{$section} instanceof EE_Config_Base || $this->{$section} instanceof stdClass)
574
+						) {
575
+							if ($display_errors) {
576
+								throw new EE_Error(
577
+									sprintf(
578
+										__(
579
+											'The "%s" configuration settings have not been formatted correctly.',
580
+											'event_espresso'
581
+										),
582
+										$section
583
+									)
584
+								);
585
+							}
586
+							return false;
587
+						}
588
+						break;
589
+					// TEST #4 : check that config section name has been set
590
+					case 4:
591
+						if (empty($name)) {
592
+							if ($display_errors) {
593
+								throw new EE_Error(
594
+									__(
595
+										'No name has been provided for the specific configuration section.',
596
+										'event_espresso'
597
+									)
598
+								);
599
+							}
600
+							return false;
601
+						}
602
+						break;
603
+					// TEST #5 : check that a config class name has been set
604
+					case 5:
605
+						if (empty($config_class)) {
606
+							if ($display_errors) {
607
+								throw new EE_Error(
608
+									__(
609
+										'No class name has been provided for the specific configuration section.',
610
+										'event_espresso'
611
+									)
612
+								);
613
+							}
614
+							return false;
615
+						}
616
+						break;
617
+					// TEST #6 : verify config class is accessible
618
+					case 6:
619
+						if (! class_exists($config_class)) {
620
+							if ($display_errors) {
621
+								throw new EE_Error(
622
+									sprintf(
623
+										__(
624
+											'The "%s" class does not exist. Please ensure that an autoloader has been set for it.',
625
+											'event_espresso'
626
+										),
627
+										$config_class
628
+									)
629
+								);
630
+							}
631
+							return false;
632
+						}
633
+						break;
634
+					// TEST #7 : check that config has even been set
635
+					case 7:
636
+						if (! isset($this->{$section}->{$name})) {
637
+							if ($display_errors) {
638
+								throw new EE_Error(
639
+									sprintf(
640
+										__('No configuration has been set for "%1$s->%2$s".', 'event_espresso'),
641
+										$section,
642
+										$name
643
+									)
644
+								);
645
+							}
646
+							return false;
647
+						} else {
648
+							// and make sure it's not serialized
649
+							$this->{$section}->{$name} = maybe_unserialize($this->{$section}->{$name});
650
+						}
651
+						break;
652
+					// TEST #8 : check that config is the requested type
653
+					case 8:
654
+						if (! $this->{$section}->{$name} instanceof $config_class) {
655
+							if ($display_errors) {
656
+								throw new EE_Error(
657
+									sprintf(
658
+										__(
659
+											'The configuration for "%1$s->%2$s" is not of the "%3$s" class.',
660
+											'event_espresso'
661
+										),
662
+										$section,
663
+										$name,
664
+										$config_class
665
+									)
666
+								);
667
+							}
668
+							return false;
669
+						}
670
+						break;
671
+					// TEST #9 : verify config object
672
+					case 9:
673
+						if (! $config_obj instanceof EE_Config_Base) {
674
+							if ($display_errors) {
675
+								throw new EE_Error(
676
+									sprintf(
677
+										__('The "%s" class is not an instance of EE_Config_Base.', 'event_espresso'),
678
+										print_r($config_obj, true)
679
+									)
680
+								);
681
+							}
682
+							return false;
683
+						}
684
+						break;
685
+				}
686
+			}
687
+		} catch (EE_Error $e) {
688
+			$e->get_error();
689
+		}
690
+		// you have successfully run the gauntlet
691
+		return true;
692
+	}
693
+
694
+
695
+	/**
696
+	 *    _generate_config_option_name
697
+	 *
698
+	 * @access        protected
699
+	 * @param        string $section
700
+	 * @param        string $name
701
+	 * @return        string
702
+	 */
703
+	private function _generate_config_option_name($section = '', $name = '')
704
+	{
705
+		return 'ee_config-' . strtolower($section . '-' . str_replace(array('EE_', 'EED_'), '', $name));
706
+	}
707
+
708
+
709
+	/**
710
+	 *    _set_config_class
711
+	 * ensures that a config class is set, either from a passed config class or one generated from the config name
712
+	 *
713
+	 * @access    private
714
+	 * @param    string $config_class
715
+	 * @param    string $name
716
+	 * @return    string
717
+	 */
718
+	private function _set_config_class($config_class = '', $name = '')
719
+	{
720
+		return ! empty($config_class)
721
+			? $config_class
722
+			: str_replace(' ', '_', ucwords(str_replace('_', ' ', $name))) . '_Config';
723
+	}
724
+
725
+
726
+	/**
727
+	 *    set_config
728
+	 *
729
+	 * @access    protected
730
+	 * @param    string         $section
731
+	 * @param    string         $name
732
+	 * @param    string         $config_class
733
+	 * @param    EE_Config_Base $config_obj
734
+	 * @return    EE_Config_Base
735
+	 */
736
+	public function set_config($section = '', $name = '', $config_class = '', EE_Config_Base $config_obj = null)
737
+	{
738
+		// ensure config class is set to something
739
+		$config_class = $this->_set_config_class($config_class, $name);
740
+		// run tests 1-4, 6, and 7 to verify all config params are set and valid
741
+		if (! $this->_verify_config_params($section, $name, $config_class, null, array(1, 2, 3, 4, 5, 6))) {
742
+			return null;
743
+		}
744
+		$config_option_name = $this->_generate_config_option_name($section, $name);
745
+		// if the config option name hasn't been added yet to the list of option names we're tracking, then do so now
746
+		if (! isset($this->_addon_option_names[ $config_option_name ])) {
747
+			$this->_addon_option_names[ $config_option_name ] = $config_class;
748
+			$this->update_addon_option_names();
749
+		}
750
+		// verify the incoming config object but suppress errors
751
+		if (! $this->_verify_config_params($section, $name, $config_class, $config_obj, array(9), false)) {
752
+			$config_obj = new $config_class();
753
+		}
754
+		if (get_option($config_option_name)) {
755
+			EE_Config::log($config_option_name);
756
+			update_option($config_option_name, $config_obj);
757
+			$this->{$section}->{$name} = $config_obj;
758
+			return $this->{$section}->{$name};
759
+		} else {
760
+			// create a wp-option for this config
761
+			if (add_option($config_option_name, $config_obj, '', 'no')) {
762
+				$this->{$section}->{$name} = maybe_unserialize($config_obj);
763
+				return $this->{$section}->{$name};
764
+			} else {
765
+				EE_Error::add_error(
766
+					sprintf(__('The "%s" could not be saved to the database.', 'event_espresso'), $config_class),
767
+					__FILE__,
768
+					__FUNCTION__,
769
+					__LINE__
770
+				);
771
+				return null;
772
+			}
773
+		}
774
+	}
775
+
776
+
777
+	/**
778
+	 *    update_config
779
+	 * Important: the config object must ALREADY be set, otherwise this will produce an error.
780
+	 *
781
+	 * @access    public
782
+	 * @param    string                $section
783
+	 * @param    string                $name
784
+	 * @param    EE_Config_Base|string $config_obj
785
+	 * @param    bool                  $throw_errors
786
+	 * @return    bool
787
+	 */
788
+	public function update_config($section = '', $name = '', $config_obj = '', $throw_errors = true)
789
+	{
790
+		// don't allow config updates during WP heartbeats
791
+		if (\EE_Registry::instance()->REQ->get('action', '') === 'heartbeat') {
792
+			return false;
793
+		}
794
+		$config_obj = maybe_unserialize($config_obj);
795
+		// get class name of the incoming object
796
+		$config_class = get_class($config_obj);
797
+		// run tests 1-5 and 9 to verify config
798
+		if (! $this->_verify_config_params(
799
+			$section,
800
+			$name,
801
+			$config_class,
802
+			$config_obj,
803
+			array(1, 2, 3, 4, 7, 9)
804
+		)
805
+		) {
806
+			return false;
807
+		}
808
+		$config_option_name = $this->_generate_config_option_name($section, $name);
809
+		// check if config object has been added to db by seeing if config option name is in $this->_addon_option_names array
810
+		if (! isset($this->_addon_option_names[ $config_option_name ])) {
811
+			// save new config to db
812
+			if ($this->set_config($section, $name, $config_class, $config_obj)) {
813
+				return true;
814
+			}
815
+		} else {
816
+			// first check if the record already exists
817
+			$existing_config = get_option($config_option_name);
818
+			$config_obj = serialize($config_obj);
819
+			// just return if db record is already up to date (NOT type safe comparison)
820
+			if ($existing_config == $config_obj) {
821
+				$this->{$section}->{$name} = $config_obj;
822
+				return true;
823
+			} elseif (update_option($config_option_name, $config_obj)) {
824
+				EE_Config::log($config_option_name);
825
+				// update wp-option for this config class
826
+				$this->{$section}->{$name} = $config_obj;
827
+				return true;
828
+			} elseif ($throw_errors) {
829
+				EE_Error::add_error(
830
+					sprintf(
831
+						__(
832
+							'The "%1$s" object stored at"%2$s" was not successfully updated in the database.',
833
+							'event_espresso'
834
+						),
835
+						$config_class,
836
+						'EE_Config->' . $section . '->' . $name
837
+					),
838
+					__FILE__,
839
+					__FUNCTION__,
840
+					__LINE__
841
+				);
842
+			}
843
+		}
844
+		return false;
845
+	}
846
+
847
+
848
+	/**
849
+	 *    get_config
850
+	 *
851
+	 * @access    public
852
+	 * @param    string $section
853
+	 * @param    string $name
854
+	 * @param    string $config_class
855
+	 * @return    mixed EE_Config_Base | NULL
856
+	 */
857
+	public function get_config($section = '', $name = '', $config_class = '')
858
+	{
859
+		// ensure config class is set to something
860
+		$config_class = $this->_set_config_class($config_class, $name);
861
+		// run tests 1-4, 6 and 7 to verify that all params have been set
862
+		if (! $this->_verify_config_params($section, $name, $config_class, null, array(1, 2, 3, 4, 5, 6))) {
863
+			return null;
864
+		}
865
+		// now test if the requested config object exists, but suppress errors
866
+		if ($this->_verify_config_params($section, $name, $config_class, null, array(7, 8), false)) {
867
+			// config already exists, so pass it back
868
+			return $this->{$section}->{$name};
869
+		}
870
+		// load config option from db if it exists
871
+		$config_obj = $this->get_config_option($this->_generate_config_option_name($section, $name));
872
+		// verify the newly retrieved config object, but suppress errors
873
+		if ($this->_verify_config_params($section, $name, $config_class, $config_obj, array(9), false)) {
874
+			// config is good, so set it and pass it back
875
+			$this->{$section}->{$name} = $config_obj;
876
+			return $this->{$section}->{$name};
877
+		}
878
+		// oops! $config_obj is not already set and does not exist in the db, so create a new one
879
+		$config_obj = $this->set_config($section, $name, $config_class);
880
+		// verify the newly created config object
881
+		if ($this->_verify_config_params($section, $name, $config_class, $config_obj, array(9))) {
882
+			return $this->{$section}->{$name};
883
+		} else {
884
+			EE_Error::add_error(
885
+				sprintf(__('The "%s" could not be retrieved from the database.', 'event_espresso'), $config_class),
886
+				__FILE__,
887
+				__FUNCTION__,
888
+				__LINE__
889
+			);
890
+		}
891
+		return null;
892
+	}
893
+
894
+
895
+	/**
896
+	 *    get_config_option
897
+	 *
898
+	 * @access    public
899
+	 * @param    string $config_option_name
900
+	 * @return    mixed EE_Config_Base | FALSE
901
+	 */
902
+	public function get_config_option($config_option_name = '')
903
+	{
904
+		// retrieve the wp-option for this config class.
905
+		$config_option = maybe_unserialize(get_option($config_option_name, array()));
906
+		if (empty($config_option)) {
907
+			EE_Config::log($config_option_name . '-NOT-FOUND');
908
+		}
909
+		return $config_option;
910
+	}
911
+
912
+
913
+	/**
914
+	 * log
915
+	 *
916
+	 * @param string $config_option_name
917
+	 */
918
+	public static function log($config_option_name = '')
919
+	{
920
+		if (EE_Config::logging_enabled() && ! empty($config_option_name)) {
921
+			$config_log = get_option(EE_Config::LOG_NAME, array());
922
+			// copy incoming $_REQUEST and sanitize it so we can save it
923
+			$_request = $_REQUEST;
924
+			array_walk_recursive($_request, 'sanitize_text_field');
925
+			$config_log[ (string) microtime(true) ] = array(
926
+				'config_name' => $config_option_name,
927
+				'request'     => $_request,
928
+			);
929
+			update_option(EE_Config::LOG_NAME, $config_log);
930
+		}
931
+	}
932
+
933
+
934
+	/**
935
+	 * trim_log
936
+	 * reduces the size of the config log to the length specified by EE_Config::LOG_LENGTH
937
+	 */
938
+	public static function trim_log()
939
+	{
940
+		if (! EE_Config::logging_enabled()) {
941
+			return;
942
+		}
943
+		$config_log = maybe_unserialize(get_option(EE_Config::LOG_NAME, array()));
944
+		$log_length = count($config_log);
945
+		if ($log_length > EE_Config::LOG_LENGTH) {
946
+			ksort($config_log);
947
+			$config_log = array_slice($config_log, $log_length - EE_Config::LOG_LENGTH, null, true);
948
+			update_option(EE_Config::LOG_NAME, $config_log);
949
+		}
950
+	}
951
+
952
+
953
+	/**
954
+	 *    get_page_for_posts
955
+	 *    if the wp-option "show_on_front" is set to "page", then this is the post_name for the post set in the
956
+	 *    wp-option "page_for_posts", or "posts" if no page is selected
957
+	 *
958
+	 * @access    public
959
+	 * @return    string
960
+	 */
961
+	public static function get_page_for_posts()
962
+	{
963
+		$page_for_posts = get_option('page_for_posts');
964
+		if (! $page_for_posts) {
965
+			return 'posts';
966
+		}
967
+		/** @type WPDB $wpdb */
968
+		global $wpdb;
969
+		$SQL = "SELECT post_name from $wpdb->posts WHERE post_type='posts' OR post_type='page' AND post_status='publish' AND ID=%d";
970
+		return $wpdb->get_var($wpdb->prepare($SQL, $page_for_posts));
971
+	}
972
+
973
+
974
+	/**
975
+	 *    register_shortcodes_and_modules.
976
+	 *    At this point, it's too early to tell if we're maintenance mode or not.
977
+	 *    In fact, this is where we give modules a chance to let core know they exist
978
+	 *    so they can help trigger maintenance mode if it's needed
979
+	 *
980
+	 * @access    public
981
+	 * @return    void
982
+	 */
983
+	public function register_shortcodes_and_modules()
984
+	{
985
+		// allow modules to set hooks for the rest of the system
986
+		EE_Registry::instance()->modules = $this->_register_modules();
987
+	}
988
+
989
+
990
+	/**
991
+	 *    initialize_shortcodes_and_modules
992
+	 *    meaning they can start adding their hooks to get stuff done
993
+	 *
994
+	 * @access    public
995
+	 * @return    void
996
+	 */
997
+	public function initialize_shortcodes_and_modules()
998
+	{
999
+		// allow modules to set hooks for the rest of the system
1000
+		$this->_initialize_modules();
1001
+	}
1002
+
1003
+
1004
+	/**
1005
+	 *    widgets_init
1006
+	 *
1007
+	 * @access private
1008
+	 * @return void
1009
+	 */
1010
+	public function widgets_init()
1011
+	{
1012
+		// only init widgets on admin pages when not in complete maintenance, and
1013
+		// on frontend when not in any maintenance mode
1014
+		if (! EE_Maintenance_Mode::instance()->level()
1015
+			|| (
1016
+				is_admin()
1017
+				&& EE_Maintenance_Mode::instance()->level() !== EE_Maintenance_Mode::level_2_complete_maintenance
1018
+			)
1019
+		) {
1020
+			// grab list of installed widgets
1021
+			$widgets_to_register = glob(EE_WIDGETS . '*', GLOB_ONLYDIR);
1022
+			// filter list of modules to register
1023
+			$widgets_to_register = apply_filters(
1024
+				'FHEE__EE_Config__register_widgets__widgets_to_register',
1025
+				$widgets_to_register
1026
+			);
1027
+			if (! empty($widgets_to_register)) {
1028
+				// cycle thru widget folders
1029
+				foreach ($widgets_to_register as $widget_path) {
1030
+					// add to list of installed widget modules
1031
+					EE_Config::register_ee_widget($widget_path);
1032
+				}
1033
+			}
1034
+			// filter list of installed modules
1035
+			EE_Registry::instance()->widgets = apply_filters(
1036
+				'FHEE__EE_Config__register_widgets__installed_widgets',
1037
+				EE_Registry::instance()->widgets
1038
+			);
1039
+		}
1040
+	}
1041
+
1042
+
1043
+	/**
1044
+	 *    register_ee_widget - makes core aware of this widget
1045
+	 *
1046
+	 * @access    public
1047
+	 * @param    string $widget_path - full path up to and including widget folder
1048
+	 * @return    void
1049
+	 */
1050
+	public static function register_ee_widget($widget_path = null)
1051
+	{
1052
+		do_action('AHEE__EE_Config__register_widget__begin', $widget_path);
1053
+		$widget_ext = '.widget.php';
1054
+		// make all separators match
1055
+		$widget_path = rtrim(str_replace('\\', DS, $widget_path), DS);
1056
+		// does the file path INCLUDE the actual file name as part of the path ?
1057
+		if (strpos($widget_path, $widget_ext) !== false) {
1058
+			// grab and shortcode file name from directory name and break apart at dots
1059
+			$file_name = explode('.', basename($widget_path));
1060
+			// take first segment from file name pieces and remove class prefix if it exists
1061
+			$widget = strpos($file_name[0], 'EEW_') === 0 ? substr($file_name[0], 4) : $file_name[0];
1062
+			// sanitize shortcode directory name
1063
+			$widget = sanitize_key($widget);
1064
+			// now we need to rebuild the shortcode path
1065
+			$widget_path = explode(DS, $widget_path);
1066
+			// remove last segment
1067
+			array_pop($widget_path);
1068
+			// glue it back together
1069
+			$widget_path = implode(DS, $widget_path);
1070
+		} else {
1071
+			// grab and sanitize widget directory name
1072
+			$widget = sanitize_key(basename($widget_path));
1073
+		}
1074
+		// create classname from widget directory name
1075
+		$widget = str_replace(' ', '_', ucwords(str_replace('_', ' ', $widget)));
1076
+		// add class prefix
1077
+		$widget_class = 'EEW_' . $widget;
1078
+		// does the widget exist ?
1079
+		if (! is_readable($widget_path . DS . $widget_class . $widget_ext)) {
1080
+			$msg = sprintf(
1081
+				__(
1082
+					'The requested %s widget file could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s',
1083
+					'event_espresso'
1084
+				),
1085
+				$widget_class,
1086
+				$widget_path . DS . $widget_class . $widget_ext
1087
+			);
1088
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1089
+			return;
1090
+		}
1091
+		// load the widget class file
1092
+		require_once($widget_path . DS . $widget_class . $widget_ext);
1093
+		// verify that class exists
1094
+		if (! class_exists($widget_class)) {
1095
+			$msg = sprintf(__('The requested %s widget class does not exist.', 'event_espresso'), $widget_class);
1096
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1097
+			return;
1098
+		}
1099
+		register_widget($widget_class);
1100
+		// add to array of registered widgets
1101
+		EE_Registry::instance()->widgets->{$widget_class} = $widget_path . DS . $widget_class . $widget_ext;
1102
+	}
1103
+
1104
+
1105
+	/**
1106
+	 *        _register_modules
1107
+	 *
1108
+	 * @access private
1109
+	 * @return array
1110
+	 */
1111
+	private function _register_modules()
1112
+	{
1113
+		// grab list of installed modules
1114
+		$modules_to_register = glob(EE_MODULES . '*', GLOB_ONLYDIR);
1115
+		// filter list of modules to register
1116
+		$modules_to_register = apply_filters(
1117
+			'FHEE__EE_Config__register_modules__modules_to_register',
1118
+			$modules_to_register
1119
+		);
1120
+		if (! empty($modules_to_register)) {
1121
+			// loop through folders
1122
+			foreach ($modules_to_register as $module_path) {
1123
+				/**TEMPORARILY EXCLUDE gateways from modules for time being**/
1124
+				if ($module_path !== EE_MODULES . 'zzz-copy-this-module-template'
1125
+					&& $module_path !== EE_MODULES . 'gateways'
1126
+				) {
1127
+					// add to list of installed modules
1128
+					EE_Config::register_module($module_path);
1129
+				}
1130
+			}
1131
+		}
1132
+		// filter list of installed modules
1133
+		return apply_filters(
1134
+			'FHEE__EE_Config___register_modules__installed_modules',
1135
+			EE_Registry::instance()->modules
1136
+		);
1137
+	}
1138
+
1139
+
1140
+	/**
1141
+	 *    register_module - makes core aware of this module
1142
+	 *
1143
+	 * @access    public
1144
+	 * @param    string $module_path - full path up to and including module folder
1145
+	 * @return    bool
1146
+	 */
1147
+	public static function register_module($module_path = null)
1148
+	{
1149
+		do_action('AHEE__EE_Config__register_module__begin', $module_path);
1150
+		$module_ext = '.module.php';
1151
+		// make all separators match
1152
+		$module_path = str_replace(array('\\', '/'), DS, $module_path);
1153
+		// does the file path INCLUDE the actual file name as part of the path ?
1154
+		if (strpos($module_path, $module_ext) !== false) {
1155
+			// grab and shortcode file name from directory name and break apart at dots
1156
+			$module_file = explode('.', basename($module_path));
1157
+			// now we need to rebuild the shortcode path
1158
+			$module_path = explode(DS, $module_path);
1159
+			// remove last segment
1160
+			array_pop($module_path);
1161
+			// glue it back together
1162
+			$module_path = implode(DS, $module_path) . DS;
1163
+			// take first segment from file name pieces and sanitize it
1164
+			$module = preg_replace('/[^a-zA-Z0-9_\-]/', '', $module_file[0]);
1165
+			// ensure class prefix is added
1166
+			$module_class = strpos($module, 'EED_') !== 0 ? 'EED_' . $module : $module;
1167
+		} else {
1168
+			// we need to generate the filename based off of the folder name
1169
+			// grab and sanitize module name
1170
+			$module = strtolower(basename($module_path));
1171
+			$module = preg_replace('/[^a-z0-9_\-]/', '', $module);
1172
+			// like trailingslashit()
1173
+			$module_path = rtrim($module_path, DS) . DS;
1174
+			// create classname from module directory name
1175
+			$module = str_replace(' ', '_', ucwords(str_replace('_', ' ', $module)));
1176
+			// add class prefix
1177
+			$module_class = 'EED_' . $module;
1178
+		}
1179
+		// does the module exist ?
1180
+		if (! is_readable($module_path . DS . $module_class . $module_ext)) {
1181
+			$msg = sprintf(
1182
+				__(
1183
+					'The requested %s module file could not be found or is not readable due to file permissions.',
1184
+					'event_espresso'
1185
+				),
1186
+				$module
1187
+			);
1188
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1189
+			return false;
1190
+		}
1191
+		// load the module class file
1192
+		require_once($module_path . $module_class . $module_ext);
1193
+		// verify that class exists
1194
+		if (! class_exists($module_class)) {
1195
+			$msg = sprintf(__('The requested %s module class does not exist.', 'event_espresso'), $module_class);
1196
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1197
+			return false;
1198
+		}
1199
+		// add to array of registered modules
1200
+		EE_Registry::instance()->modules->{$module_class} = $module_path . $module_class . $module_ext;
1201
+		do_action(
1202
+			'AHEE__EE_Config__register_module__complete',
1203
+			$module_class,
1204
+			EE_Registry::instance()->modules->{$module_class}
1205
+		);
1206
+		return true;
1207
+	}
1208
+
1209
+
1210
+	/**
1211
+	 *    _initialize_modules
1212
+	 *    allow modules to set hooks for the rest of the system
1213
+	 *
1214
+	 * @access private
1215
+	 * @return void
1216
+	 */
1217
+	private function _initialize_modules()
1218
+	{
1219
+		// cycle thru shortcode folders
1220
+		foreach (EE_Registry::instance()->modules as $module_class => $module_path) {
1221
+			// fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system
1222
+			// which set hooks ?
1223
+			if (is_admin()) {
1224
+				// fire immediately
1225
+				call_user_func(array($module_class, 'set_hooks_admin'));
1226
+			} else {
1227
+				// delay until other systems are online
1228
+				add_action(
1229
+					'AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons',
1230
+					array($module_class, 'set_hooks')
1231
+				);
1232
+			}
1233
+		}
1234
+	}
1235
+
1236
+
1237
+	/**
1238
+	 *    register_route - adds module method routes to route_map
1239
+	 *
1240
+	 * @access    public
1241
+	 * @param    string $route       - "pretty" public alias for module method
1242
+	 * @param    string $module      - module name (classname without EED_ prefix)
1243
+	 * @param    string $method_name - the actual module method to be routed to
1244
+	 * @param    string $key         - url param key indicating a route is being called
1245
+	 * @return    bool
1246
+	 */
1247
+	public static function register_route($route = null, $module = null, $method_name = null, $key = 'ee')
1248
+	{
1249
+		do_action('AHEE__EE_Config__register_route__begin', $route, $module, $method_name);
1250
+		$module = str_replace('EED_', '', $module);
1251
+		$module_class = 'EED_' . $module;
1252
+		if (! isset(EE_Registry::instance()->modules->{$module_class})) {
1253
+			$msg = sprintf(__('The module %s has not been registered.', 'event_espresso'), $module);
1254
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1255
+			return false;
1256
+		}
1257
+		if (empty($route)) {
1258
+			$msg = sprintf(__('No route has been supplied.', 'event_espresso'), $route);
1259
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1260
+			return false;
1261
+		}
1262
+		if (! method_exists('EED_' . $module, $method_name)) {
1263
+			$msg = sprintf(
1264
+				__('A valid class method for the %s route has not been supplied.', 'event_espresso'),
1265
+				$route
1266
+			);
1267
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1268
+			return false;
1269
+		}
1270
+		EE_Config::$_module_route_map[ (string) $key ][ (string) $route ] = array('EED_' . $module, $method_name);
1271
+		return true;
1272
+	}
1273
+
1274
+
1275
+	/**
1276
+	 *    get_route - get module method route
1277
+	 *
1278
+	 * @access    public
1279
+	 * @param    string $route - "pretty" public alias for module method
1280
+	 * @param    string $key   - url param key indicating a route is being called
1281
+	 * @return    string
1282
+	 */
1283
+	public static function get_route($route = null, $key = 'ee')
1284
+	{
1285
+		do_action('AHEE__EE_Config__get_route__begin', $route);
1286
+		$route = (string) apply_filters('FHEE__EE_Config__get_route', $route);
1287
+		if (isset(EE_Config::$_module_route_map[ $key ][ $route ])) {
1288
+			return EE_Config::$_module_route_map[ $key ][ $route ];
1289
+		}
1290
+		return null;
1291
+	}
1292
+
1293
+
1294
+	/**
1295
+	 *    get_routes - get ALL module method routes
1296
+	 *
1297
+	 * @access    public
1298
+	 * @return    array
1299
+	 */
1300
+	public static function get_routes()
1301
+	{
1302
+		return EE_Config::$_module_route_map;
1303
+	}
1304
+
1305
+
1306
+	/**
1307
+	 *    register_forward - allows modules to forward request to another module for further processing
1308
+	 *
1309
+	 * @access    public
1310
+	 * @param    string       $route   - "pretty" public alias for module method
1311
+	 * @param    integer      $status  - integer value corresponding  to status constant strings set in module parent
1312
+	 *                                 class, allows different forwards to be served based on status
1313
+	 * @param    array|string $forward - function name or array( class, method )
1314
+	 * @param    string       $key     - url param key indicating a route is being called
1315
+	 * @return    bool
1316
+	 */
1317
+	public static function register_forward($route = null, $status = 0, $forward = null, $key = 'ee')
1318
+	{
1319
+		do_action('AHEE__EE_Config__register_forward', $route, $status, $forward);
1320
+		if (! isset(EE_Config::$_module_route_map[ $key ][ $route ]) || empty($route)) {
1321
+			$msg = sprintf(
1322
+				__('The module route %s for this forward has not been registered.', 'event_espresso'),
1323
+				$route
1324
+			);
1325
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1326
+			return false;
1327
+		}
1328
+		if (empty($forward)) {
1329
+			$msg = sprintf(__('No forwarding route has been supplied.', 'event_espresso'), $route);
1330
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1331
+			return false;
1332
+		}
1333
+		if (is_array($forward)) {
1334
+			if (! isset($forward[1])) {
1335
+				$msg = sprintf(
1336
+					__('A class method for the %s forwarding route has not been supplied.', 'event_espresso'),
1337
+					$route
1338
+				);
1339
+				EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1340
+				return false;
1341
+			}
1342
+			if (! method_exists($forward[0], $forward[1])) {
1343
+				$msg = sprintf(
1344
+					__('The class method %s for the %s forwarding route is in invalid.', 'event_espresso'),
1345
+					$forward[1],
1346
+					$route
1347
+				);
1348
+				EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1349
+				return false;
1350
+			}
1351
+		} elseif (! function_exists($forward)) {
1352
+			$msg = sprintf(
1353
+				__('The function %s for the %s forwarding route is in invalid.', 'event_espresso'),
1354
+				$forward,
1355
+				$route
1356
+			);
1357
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1358
+			return false;
1359
+		}
1360
+		EE_Config::$_module_forward_map[ $key ][ $route ][ absint($status) ] = $forward;
1361
+		return true;
1362
+	}
1363
+
1364
+
1365
+	/**
1366
+	 *    get_forward - get forwarding route
1367
+	 *
1368
+	 * @access    public
1369
+	 * @param    string  $route  - "pretty" public alias for module method
1370
+	 * @param    integer $status - integer value corresponding  to status constant strings set in module parent class,
1371
+	 *                           allows different forwards to be served based on status
1372
+	 * @param    string  $key    - url param key indicating a route is being called
1373
+	 * @return    string
1374
+	 */
1375
+	public static function get_forward($route = null, $status = 0, $key = 'ee')
1376
+	{
1377
+		do_action('AHEE__EE_Config__get_forward__begin', $route, $status);
1378
+		if (isset(EE_Config::$_module_forward_map[ $key ][ $route ][ $status ])) {
1379
+			return apply_filters(
1380
+				'FHEE__EE_Config__get_forward',
1381
+				EE_Config::$_module_forward_map[ $key ][ $route ][ $status ],
1382
+				$route,
1383
+				$status
1384
+			);
1385
+		}
1386
+		return null;
1387
+	}
1388
+
1389
+
1390
+	/**
1391
+	 *    register_forward - allows modules to specify different view templates for different method routes and status
1392
+	 *    results
1393
+	 *
1394
+	 * @access    public
1395
+	 * @param    string  $route  - "pretty" public alias for module method
1396
+	 * @param    integer $status - integer value corresponding  to status constant strings set in module parent class,
1397
+	 *                           allows different views to be served based on status
1398
+	 * @param    string  $view
1399
+	 * @param    string  $key    - url param key indicating a route is being called
1400
+	 * @return    bool
1401
+	 */
1402
+	public static function register_view($route = null, $status = 0, $view = null, $key = 'ee')
1403
+	{
1404
+		do_action('AHEE__EE_Config__register_view__begin', $route, $status, $view);
1405
+		if (! isset(EE_Config::$_module_route_map[ $key ][ $route ]) || empty($route)) {
1406
+			$msg = sprintf(
1407
+				__('The module route %s for this view has not been registered.', 'event_espresso'),
1408
+				$route
1409
+			);
1410
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1411
+			return false;
1412
+		}
1413
+		if (! is_readable($view)) {
1414
+			$msg = sprintf(
1415
+				__(
1416
+					'The %s view file could not be found or is not readable due to file permissions.',
1417
+					'event_espresso'
1418
+				),
1419
+				$view
1420
+			);
1421
+			EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__);
1422
+			return false;
1423
+		}
1424
+		EE_Config::$_module_view_map[ $key ][ $route ][ absint($status) ] = $view;
1425
+		return true;
1426
+	}
1427
+
1428
+
1429
+	/**
1430
+	 *    get_view - get view for route and status
1431
+	 *
1432
+	 * @access    public
1433
+	 * @param    string  $route  - "pretty" public alias for module method
1434
+	 * @param    integer $status - integer value corresponding  to status constant strings set in module parent class,
1435
+	 *                           allows different views to be served based on status
1436
+	 * @param    string  $key    - url param key indicating a route is being called
1437
+	 * @return    string
1438
+	 */
1439
+	public static function get_view($route = null, $status = 0, $key = 'ee')
1440
+	{
1441
+		do_action('AHEE__EE_Config__get_view__begin', $route, $status);
1442
+		if (isset(EE_Config::$_module_view_map[ $key ][ $route ][ $status ])) {
1443
+			return apply_filters(
1444
+				'FHEE__EE_Config__get_view',
1445
+				EE_Config::$_module_view_map[ $key ][ $route ][ $status ],
1446
+				$route,
1447
+				$status
1448
+			);
1449
+		}
1450
+		return null;
1451
+	}
1452
+
1453
+
1454
+	public function update_addon_option_names()
1455
+	{
1456
+		update_option(EE_Config::ADDON_OPTION_NAMES, $this->_addon_option_names);
1457
+	}
1458
+
1459
+
1460
+	public function shutdown()
1461
+	{
1462
+		$this->update_addon_option_names();
1463
+	}
1464
+
1465
+
1466
+	/**
1467
+	 * @return LegacyShortcodesManager
1468
+	 */
1469
+	public static function getLegacyShortcodesManager()
1470
+	{
1471
+
1472
+		if (! EE_Config::instance()->legacy_shortcodes_manager instanceof LegacyShortcodesManager) {
1473
+			EE_Config::instance()->legacy_shortcodes_manager = new LegacyShortcodesManager(
1474
+				EE_Registry::instance()
1475
+			);
1476
+		}
1477
+		return EE_Config::instance()->legacy_shortcodes_manager;
1478
+	}
1479
+
1480
+
1481
+	/**
1482
+	 * register_shortcode - makes core aware of this shortcode
1483
+	 *
1484
+	 * @deprecated 4.9.26
1485
+	 * @param    string $shortcode_path - full path up to and including shortcode folder
1486
+	 * @return    bool
1487
+	 */
1488
+	public static function register_shortcode($shortcode_path = null)
1489
+	{
1490
+		EE_Error::doing_it_wrong(
1491
+			__METHOD__,
1492
+			__(
1493
+				'Usage is deprecated. Use \EventEspresso\core\services\shortcodes\LegacyShortcodesManager::registerShortcode() as direct replacement, or better yet, please see the new \EventEspresso\core\services\shortcodes\ShortcodesManager class.',
1494
+				'event_espresso'
1495
+			),
1496
+			'4.9.26'
1497
+		);
1498
+		return EE_Config::instance()->getLegacyShortcodesManager()->registerShortcode($shortcode_path);
1499
+	}
1500
+}
2262 1501
 
2263
-    /**
2264
-     * ReCaptcha Type
2265
-     *
2266
-     * @var string $recaptcha_type
2267
-     *    options: 'audio', 'image'
2268
-     */
2269
-    public $recaptcha_type;
1502
+/**
1503
+ * Base class used for config classes. These classes should generally not have
1504
+ * magic functions in use, except we'll allow them to magically set and get stuff...
1505
+ * basically, they should just be well-defined stdClasses
1506
+ */
1507
+class EE_Config_Base
1508
+{
2270 1509
 
2271
-    /**
2272
-     * ReCaptcha language
2273
-     *
2274
-     * @var string $recaptcha_language
2275
-     * eg 'en'
2276
-     */
2277
-    public $recaptcha_language;
1510
+	/**
1511
+	 * Utility function for escaping the value of a property and returning.
1512
+	 *
1513
+	 * @param string $property property name (checks to see if exists).
1514
+	 * @return mixed if a detected type found return the escaped value, otherwise just the raw value is returned.
1515
+	 * @throws \EE_Error
1516
+	 */
1517
+	public function get_pretty($property)
1518
+	{
1519
+		if (! property_exists($this, $property)) {
1520
+			throw new EE_Error(
1521
+				sprintf(
1522
+					__(
1523
+						'%1$s::get_pretty() has been called with the property %2$s which does not exist on the %1$s config class.',
1524
+						'event_espresso'
1525
+					),
1526
+					get_class($this),
1527
+					$property
1528
+				)
1529
+			);
1530
+		}
1531
+		// just handling escaping of strings for now.
1532
+		if (is_string($this->{$property})) {
1533
+			return stripslashes($this->{$property});
1534
+		}
1535
+		return $this->{$property};
1536
+	}
1537
+
1538
+
1539
+	public function populate()
1540
+	{
1541
+		// grab defaults via a new instance of this class.
1542
+		$class_name = get_class($this);
1543
+		$defaults = new $class_name;
1544
+		// loop through the properties for this class and see if they are set.  If they are NOT, then grab the
1545
+		// default from our $defaults object.
1546
+		foreach (get_object_vars($defaults) as $property => $value) {
1547
+			if ($this->{$property} === null) {
1548
+				$this->{$property} = $value;
1549
+			}
1550
+		}
1551
+		// cleanup
1552
+		unset($defaults);
1553
+	}
1554
+
1555
+
1556
+	/**
1557
+	 *        __isset
1558
+	 *
1559
+	 * @param $a
1560
+	 * @return bool
1561
+	 */
1562
+	public function __isset($a)
1563
+	{
1564
+		return false;
1565
+	}
1566
+
1567
+
1568
+	/**
1569
+	 *        __unset
1570
+	 *
1571
+	 * @param $a
1572
+	 * @return bool
1573
+	 */
1574
+	public function __unset($a)
1575
+	{
1576
+		return false;
1577
+	}
1578
+
1579
+
1580
+	/**
1581
+	 *        __clone
1582
+	 */
1583
+	public function __clone()
1584
+	{
1585
+	}
1586
+
1587
+
1588
+	/**
1589
+	 *        __wakeup
1590
+	 */
1591
+	public function __wakeup()
1592
+	{
1593
+	}
1594
+
1595
+
1596
+	/**
1597
+	 *        __destruct
1598
+	 */
1599
+	public function __destruct()
1600
+	{
1601
+	}
1602
+}
2278 1603
 
2279
-    /**
2280
-     * ReCaptcha public key
2281
-     *
2282
-     * @var string $recaptcha_publickey
2283
-     */
2284
-    public $recaptcha_publickey;
1604
+/**
1605
+ * Class for defining what's in the EE_Config relating to registration settings
1606
+ */
1607
+class EE_Core_Config extends EE_Config_Base
1608
+{
2285 1609
 
2286
-    /**
2287
-     * ReCaptcha private key
2288
-     *
2289
-     * @var string $recaptcha_privatekey
2290
-     */
2291
-    public $recaptcha_privatekey;
1610
+	const OPTION_NAME_UXIP = 'ee_ueip_optin';
1611
+
1612
+
1613
+	public $current_blog_id;
1614
+
1615
+	public $ee_ueip_optin;
1616
+
1617
+	public $ee_ueip_has_notified;
1618
+
1619
+	/**
1620
+	 * Not to be confused with the 4 critical page variables (See
1621
+	 * get_critical_pages_array()), this is just an array of wp posts that have EE
1622
+	 * shortcodes in them. Keys are slugs, values are arrays with only 1 element: where the key is the shortcode
1623
+	 * in the page, and the value is the page's ID. The key 'posts' is basically a duplicate of this same array.
1624
+	 *
1625
+	 * @var array
1626
+	 */
1627
+	public $post_shortcodes;
1628
+
1629
+	public $module_route_map;
1630
+
1631
+	public $module_forward_map;
1632
+
1633
+	public $module_view_map;
1634
+
1635
+	/**
1636
+	 * The next 4 vars are the IDs of critical EE pages.
1637
+	 *
1638
+	 * @var int
1639
+	 */
1640
+	public $reg_page_id;
1641
+
1642
+	public $txn_page_id;
1643
+
1644
+	public $thank_you_page_id;
1645
+
1646
+	public $cancel_page_id;
1647
+
1648
+	/**
1649
+	 * The next 4 vars are the URLs of critical EE pages.
1650
+	 *
1651
+	 * @var int
1652
+	 */
1653
+	public $reg_page_url;
1654
+
1655
+	public $txn_page_url;
1656
+
1657
+	public $thank_you_page_url;
1658
+
1659
+	public $cancel_page_url;
1660
+
1661
+	/**
1662
+	 * The next vars relate to the custom slugs for EE CPT routes
1663
+	 */
1664
+	public $event_cpt_slug;
1665
+
1666
+	/**
1667
+	 * This caches the _ee_ueip_option in case this config is reset in the same
1668
+	 * request across blog switches in a multisite context.
1669
+	 * Avoids extra queries to the db for this option.
1670
+	 *
1671
+	 * @var bool
1672
+	 */
1673
+	public static $ee_ueip_option;
1674
+
1675
+
1676
+	/**
1677
+	 *    class constructor
1678
+	 *
1679
+	 * @access    public
1680
+	 */
1681
+	public function __construct()
1682
+	{
1683
+		// set default organization settings
1684
+		$this->current_blog_id = get_current_blog_id();
1685
+		$this->current_blog_id = $this->current_blog_id === null ? 1 : $this->current_blog_id;
1686
+		$this->ee_ueip_optin = $this->_get_main_ee_ueip_optin();
1687
+		$this->ee_ueip_has_notified = is_main_site() ? get_option('ee_ueip_has_notified', false) : true;
1688
+		$this->post_shortcodes = array();
1689
+		$this->module_route_map = array();
1690
+		$this->module_forward_map = array();
1691
+		$this->module_view_map = array();
1692
+		// critical EE page IDs
1693
+		$this->reg_page_id = 0;
1694
+		$this->txn_page_id = 0;
1695
+		$this->thank_you_page_id = 0;
1696
+		$this->cancel_page_id = 0;
1697
+		// critical EE page URLs
1698
+		$this->reg_page_url = '';
1699
+		$this->txn_page_url = '';
1700
+		$this->thank_you_page_url = '';
1701
+		$this->cancel_page_url = '';
1702
+		// cpt slugs
1703
+		$this->event_cpt_slug = __('events', 'event_espresso');
1704
+		// ueip constant check
1705
+		if (defined('EE_DISABLE_UXIP') && EE_DISABLE_UXIP) {
1706
+			$this->ee_ueip_optin = false;
1707
+			$this->ee_ueip_has_notified = true;
1708
+		}
1709
+	}
1710
+
1711
+
1712
+	/**
1713
+	 * @return array
1714
+	 */
1715
+	public function get_critical_pages_array()
1716
+	{
1717
+		return array(
1718
+			$this->reg_page_id,
1719
+			$this->txn_page_id,
1720
+			$this->thank_you_page_id,
1721
+			$this->cancel_page_id,
1722
+		);
1723
+	}
1724
+
1725
+
1726
+	/**
1727
+	 * @return array
1728
+	 */
1729
+	public function get_critical_pages_shortcodes_array()
1730
+	{
1731
+		return array(
1732
+			$this->reg_page_id       => 'ESPRESSO_CHECKOUT',
1733
+			$this->txn_page_id       => 'ESPRESSO_TXN_PAGE',
1734
+			$this->thank_you_page_id => 'ESPRESSO_THANK_YOU',
1735
+			$this->cancel_page_id    => 'ESPRESSO_CANCELLED',
1736
+		);
1737
+	}
1738
+
1739
+
1740
+	/**
1741
+	 *  gets/returns URL for EE reg_page
1742
+	 *
1743
+	 * @access    public
1744
+	 * @return    string
1745
+	 */
1746
+	public function reg_page_url()
1747
+	{
1748
+		if (! $this->reg_page_url) {
1749
+			$this->reg_page_url = add_query_arg(
1750
+				array('uts' => time()),
1751
+				get_permalink($this->reg_page_id)
1752
+			) . '#checkout';
1753
+		}
1754
+		return $this->reg_page_url;
1755
+	}
1756
+
1757
+
1758
+	/**
1759
+	 *  gets/returns URL for EE txn_page
1760
+	 *
1761
+	 * @param array $query_args like what gets passed to
1762
+	 *                          add_query_arg() as the first argument
1763
+	 * @access    public
1764
+	 * @return    string
1765
+	 */
1766
+	public function txn_page_url($query_args = array())
1767
+	{
1768
+		if (! $this->txn_page_url) {
1769
+			$this->txn_page_url = get_permalink($this->txn_page_id);
1770
+		}
1771
+		if ($query_args) {
1772
+			return add_query_arg($query_args, $this->txn_page_url);
1773
+		} else {
1774
+			return $this->txn_page_url;
1775
+		}
1776
+	}
1777
+
1778
+
1779
+	/**
1780
+	 *  gets/returns URL for EE thank_you_page
1781
+	 *
1782
+	 * @param array $query_args like what gets passed to
1783
+	 *                          add_query_arg() as the first argument
1784
+	 * @access    public
1785
+	 * @return    string
1786
+	 */
1787
+	public function thank_you_page_url($query_args = array())
1788
+	{
1789
+		if (! $this->thank_you_page_url) {
1790
+			$this->thank_you_page_url = get_permalink($this->thank_you_page_id);
1791
+		}
1792
+		if ($query_args) {
1793
+			return add_query_arg($query_args, $this->thank_you_page_url);
1794
+		} else {
1795
+			return $this->thank_you_page_url;
1796
+		}
1797
+	}
1798
+
1799
+
1800
+	/**
1801
+	 *  gets/returns URL for EE cancel_page
1802
+	 *
1803
+	 * @access    public
1804
+	 * @return    string
1805
+	 */
1806
+	public function cancel_page_url()
1807
+	{
1808
+		if (! $this->cancel_page_url) {
1809
+			$this->cancel_page_url = get_permalink($this->cancel_page_id);
1810
+		}
1811
+		return $this->cancel_page_url;
1812
+	}
1813
+
1814
+
1815
+	/**
1816
+	 * Resets all critical page urls to their original state.  Used primarily by the __sleep() magic method currently.
1817
+	 *
1818
+	 * @since 4.7.5
1819
+	 */
1820
+	protected function _reset_urls()
1821
+	{
1822
+		$this->reg_page_url = '';
1823
+		$this->txn_page_url = '';
1824
+		$this->cancel_page_url = '';
1825
+		$this->thank_you_page_url = '';
1826
+	}
1827
+
1828
+
1829
+	/**
1830
+	 * Used to return what the optin value is set for the EE User Experience Program.
1831
+	 * This accounts for multisite and this value being requested for a subsite.  In multisite, the value is set
1832
+	 * on the main site only.
1833
+	 *
1834
+	 * @return bool
1835
+	 */
1836
+	protected function _get_main_ee_ueip_optin()
1837
+	{
1838
+		// if this is the main site then we can just bypass our direct query.
1839
+		if (is_main_site()) {
1840
+			return get_option(self::OPTION_NAME_UXIP, false);
1841
+		}
1842
+		// is this already cached for this request?  If so use it.
1843
+		if (EE_Core_Config::$ee_ueip_option !== null) {
1844
+			return EE_Core_Config::$ee_ueip_option;
1845
+		}
1846
+		global $wpdb;
1847
+		$current_network_main_site = is_multisite() ? get_current_site() : null;
1848
+		$current_main_site_id = ! empty($current_network_main_site) ? $current_network_main_site->blog_id : 1;
1849
+		$option = self::OPTION_NAME_UXIP;
1850
+		// set correct table for query
1851
+		$table_name = $wpdb->get_blog_prefix($current_main_site_id) . 'options';
1852
+		// rather than getting blog option for the $current_main_site_id, we do a direct $wpdb query because
1853
+		// get_blog_option() does a switch_to_blog an that could cause infinite recursion because EE_Core_Config might be
1854
+		// re-constructed on the blog switch.  Note, we are still executing any core wp filters on this option retrieval.
1855
+		// this bit of code is basically a direct copy of get_option without any caching because we are NOT switched to the blog
1856
+		// for the purpose of caching.
1857
+		$pre = apply_filters('pre_option_' . $option, false, $option);
1858
+		if (false !== $pre) {
1859
+			EE_Core_Config::$ee_ueip_option = $pre;
1860
+			return EE_Core_Config::$ee_ueip_option;
1861
+		}
1862
+		$row = $wpdb->get_row(
1863
+			$wpdb->prepare(
1864
+				"SELECT option_value FROM $table_name WHERE option_name = %s LIMIT 1",
1865
+				$option
1866
+			)
1867
+		);
1868
+		if (is_object($row)) {
1869
+			$value = $row->option_value;
1870
+		} else { // option does not exist so use default.
1871
+			EE_Core_Config::$ee_ueip_option =  apply_filters('default_option_' . $option, false, $option);
1872
+			return EE_Core_Config::$ee_ueip_option;
1873
+		}
1874
+		EE_Core_Config::$ee_ueip_option = apply_filters('option_' . $option, maybe_unserialize($value), $option);
1875
+		return EE_Core_Config::$ee_ueip_option;
1876
+	}
1877
+
1878
+
1879
+	/**
1880
+	 * Utility function for escaping the value of a property and returning.
1881
+	 *
1882
+	 * @param string $property property name (checks to see if exists).
1883
+	 * @return mixed if a detected type found return the escaped value, otherwise just the raw value is returned.
1884
+	 * @throws \EE_Error
1885
+	 */
1886
+	public function get_pretty($property)
1887
+	{
1888
+		if ($property === self::OPTION_NAME_UXIP) {
1889
+			return $this->ee_ueip_optin ? 'yes' : 'no';
1890
+		}
1891
+		return parent::get_pretty($property);
1892
+	}
1893
+
1894
+
1895
+	/**
1896
+	 * Currently used to ensure critical page urls have initial values saved to the db instead of any current set values
1897
+	 * on the object.
1898
+	 *
1899
+	 * @return array
1900
+	 */
1901
+	public function __sleep()
1902
+	{
1903
+		// reset all url properties
1904
+		$this->_reset_urls();
1905
+		// return what to save to db
1906
+		return array_keys(get_object_vars($this));
1907
+	}
1908
+}
2292 1909
 
2293
-    /**
2294
-     * array of form names protected by ReCaptcha
2295
-     *
2296
-     * @var array $recaptcha_protected_forms
2297
-     */
2298
-    public $recaptcha_protected_forms;
1910
+/**
1911
+ * Config class for storing info on the Organization
1912
+ */
1913
+class EE_Organization_Config extends EE_Config_Base
1914
+{
2299 1915
 
2300
-    /**
2301
-     * ReCaptcha width
2302
-     *
2303
-     * @var int $recaptcha_width
2304
-     * @deprecated
2305
-     */
2306
-    public $recaptcha_width;
1916
+	/**
1917
+	 * @var string $name
1918
+	 * eg EE4.1
1919
+	 */
1920
+	public $name;
1921
+
1922
+	/**
1923
+	 * @var string $address_1
1924
+	 * eg 123 Onna Road
1925
+	 */
1926
+	public $address_1 = '';
1927
+
1928
+	/**
1929
+	 * @var string $address_2
1930
+	 * eg PO Box 123
1931
+	 */
1932
+	public $address_2 = '';
1933
+
1934
+	/**
1935
+	 * @var string $city
1936
+	 * eg Inna City
1937
+	 */
1938
+	public $city = '';
1939
+
1940
+	/**
1941
+	 * @var int $STA_ID
1942
+	 * eg 4
1943
+	 */
1944
+	public $STA_ID = 0;
1945
+
1946
+	/**
1947
+	 * @var string $CNT_ISO
1948
+	 * eg US
1949
+	 */
1950
+	public $CNT_ISO = '';
1951
+
1952
+	/**
1953
+	 * @var string $zip
1954
+	 * eg 12345  or V1A 2B3
1955
+	 */
1956
+	public $zip = '';
1957
+
1958
+	/**
1959
+	 * @var string $email
1960
+	 * eg [email protected]
1961
+	 */
1962
+	public $email;
1963
+
1964
+	/**
1965
+	 * @var string $phone
1966
+	 * eg. 111-111-1111
1967
+	 */
1968
+	public $phone = '';
1969
+
1970
+	/**
1971
+	 * @var string $vat
1972
+	 * VAT/Tax Number
1973
+	 */
1974
+	public $vat = '';
1975
+
1976
+	/**
1977
+	 * @var string $logo_url
1978
+	 * eg http://www.somedomain.com/wp-content/uploads/kittehs.jpg
1979
+	 */
1980
+	public $logo_url = '';
1981
+
1982
+	/**
1983
+	 * The below are all various properties for holding links to organization social network profiles
1984
+	 *
1985
+	 * @var string
1986
+	 */
1987
+	/**
1988
+	 * facebook (facebook.com/profile.name)
1989
+	 *
1990
+	 * @var string
1991
+	 */
1992
+	public $facebook = '';
1993
+
1994
+	/**
1995
+	 * twitter (twitter.com/twitter_handle)
1996
+	 *
1997
+	 * @var string
1998
+	 */
1999
+	public $twitter = '';
2000
+
2001
+	/**
2002
+	 * linkedin (linkedin.com/in/profile_name)
2003
+	 *
2004
+	 * @var string
2005
+	 */
2006
+	public $linkedin = '';
2007
+
2008
+	/**
2009
+	 * pinterest (www.pinterest.com/profile_name)
2010
+	 *
2011
+	 * @var string
2012
+	 */
2013
+	public $pinterest = '';
2014
+
2015
+	/**
2016
+	 * google+ (google.com/+profileName)
2017
+	 *
2018
+	 * @var string
2019
+	 */
2020
+	public $google = '';
2021
+
2022
+	/**
2023
+	 * instagram (instagram.com/handle)
2024
+	 *
2025
+	 * @var string
2026
+	 */
2027
+	public $instagram = '';
2028
+
2029
+
2030
+	/**
2031
+	 *    class constructor
2032
+	 *
2033
+	 * @access    public
2034
+	 */
2035
+	public function __construct()
2036
+	{
2037
+		// set default organization settings
2038
+		// decode HTML entities from the WP blogname, because it's stored in the DB with HTML entities encoded
2039
+		$this->name = wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES);
2040
+		$this->email = get_bloginfo('admin_email');
2041
+	}
2042
+}
2307 2043
 
2308
-    /**
2309
-     * Whether or not invalid attempts to directly access the registration checkout page should be tracked.
2310
-     *
2311
-     * @var boolean $track_invalid_checkout_access
2312
-     */
2313
-    protected $track_invalid_checkout_access = true;
2044
+/**
2045
+ * Class for defining what's in the EE_Config relating to currency
2046
+ */
2047
+class EE_Currency_Config extends EE_Config_Base
2048
+{
2314 2049
 
2315
-    /**
2316
-     * Whether or not to show the privacy policy consent checkbox
2317
-     *
2318
-     * @var bool
2319
-     */
2320
-    public $consent_checkbox_enabled;
2050
+	/**
2051
+	 * @var string $code
2052
+	 * eg 'US'
2053
+	 */
2054
+	public $code;
2055
+
2056
+	/**
2057
+	 * @var string $name
2058
+	 * eg 'Dollar'
2059
+	 */
2060
+	public $name;
2061
+
2062
+	/**
2063
+	 * plural name
2064
+	 *
2065
+	 * @var string $plural
2066
+	 * eg 'Dollars'
2067
+	 */
2068
+	public $plural;
2069
+
2070
+	/**
2071
+	 * currency sign
2072
+	 *
2073
+	 * @var string $sign
2074
+	 * eg '$'
2075
+	 */
2076
+	public $sign;
2077
+
2078
+	/**
2079
+	 * Whether the currency sign should come before the number or not
2080
+	 *
2081
+	 * @var boolean $sign_b4
2082
+	 */
2083
+	public $sign_b4;
2084
+
2085
+	/**
2086
+	 * How many digits should come after the decimal place
2087
+	 *
2088
+	 * @var int $dec_plc
2089
+	 */
2090
+	public $dec_plc;
2091
+
2092
+	/**
2093
+	 * Symbol to use for decimal mark
2094
+	 *
2095
+	 * @var string $dec_mrk
2096
+	 * eg '.'
2097
+	 */
2098
+	public $dec_mrk;
2099
+
2100
+	/**
2101
+	 * Symbol to use for thousands
2102
+	 *
2103
+	 * @var string $thsnds
2104
+	 * eg ','
2105
+	 */
2106
+	public $thsnds;
2107
+
2108
+
2109
+	/**
2110
+	 *    class constructor
2111
+	 *
2112
+	 * @access    public
2113
+	 * @param string $CNT_ISO
2114
+	 * @throws \EE_Error
2115
+	 */
2116
+	public function __construct($CNT_ISO = '')
2117
+	{
2118
+		/** @var \EventEspresso\core\services\database\TableAnalysis $table_analysis */
2119
+		$table_analysis = EE_Registry::instance()->create('TableAnalysis', array(), true);
2120
+		// get country code from organization settings or use default
2121
+		$ORG_CNT = isset(EE_Registry::instance()->CFG->organization)
2122
+				   && EE_Registry::instance()->CFG->organization instanceof EE_Organization_Config
2123
+			? EE_Registry::instance()->CFG->organization->CNT_ISO
2124
+			: '';
2125
+		// but override if requested
2126
+		$CNT_ISO = ! empty($CNT_ISO) ? $CNT_ISO : $ORG_CNT;
2127
+		// so if that all went well, and we are not in M-Mode (cuz you can't query the db in M-Mode) and double-check the countries table exists
2128
+		if (! empty($CNT_ISO)
2129
+			&& EE_Maintenance_Mode::instance()->models_can_query()
2130
+			&& $table_analysis->tableExists(EE_Registry::instance()->load_model('Country')->table())
2131
+		) {
2132
+			// retrieve the country settings from the db, just in case they have been customized
2133
+			$country = EE_Registry::instance()->load_model('Country')->get_one_by_ID($CNT_ISO);
2134
+			if ($country instanceof EE_Country) {
2135
+				$this->code = $country->currency_code();    // currency code: USD, CAD, EUR
2136
+				$this->name = $country->currency_name_single();    // Dollar
2137
+				$this->plural = $country->currency_name_plural();    // Dollars
2138
+				$this->sign = $country->currency_sign();            // currency sign: $
2139
+				$this->sign_b4 = $country->currency_sign_before(
2140
+				);        // currency sign before or after: $TRUE  or  FALSE$
2141
+				$this->dec_plc = $country->currency_decimal_places();    // decimal places: 2 = 0.00  3 = 0.000
2142
+				$this->dec_mrk = $country->currency_decimal_mark(
2143
+				);    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
2144
+				$this->thsnds = $country->currency_thousands_separator(
2145
+				);    // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
2146
+			}
2147
+		}
2148
+		// fallback to hardcoded defaults, in case the above failed
2149
+		if (empty($this->code)) {
2150
+			// set default currency settings
2151
+			$this->code = 'USD';    // currency code: USD, CAD, EUR
2152
+			$this->name = __('Dollar', 'event_espresso');    // Dollar
2153
+			$this->plural = __('Dollars', 'event_espresso');    // Dollars
2154
+			$this->sign = '$';    // currency sign: $
2155
+			$this->sign_b4 = true;    // currency sign before or after: $TRUE  or  FALSE$
2156
+			$this->dec_plc = 2;    // decimal places: 2 = 0.00  3 = 0.000
2157
+			$this->dec_mrk = '.';    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
2158
+			$this->thsnds = ',';    // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
2159
+		}
2160
+	}
2161
+}
2321 2162
 
2322
-    /**
2323
-     * Label text to show on the checkbox
2324
-     *
2325
-     * @var string
2326
-     */
2327
-    public $consent_checkbox_label_text;
2163
+/**
2164
+ * Class for defining what's in the EE_Config relating to registration settings
2165
+ */
2166
+class EE_Registration_Config extends EE_Config_Base
2167
+{
2328 2168
 
2329
-    /*
2169
+	/**
2170
+	 * Default registration status
2171
+	 *
2172
+	 * @var string $default_STS_ID
2173
+	 * eg 'RPP'
2174
+	 */
2175
+	public $default_STS_ID;
2176
+
2177
+	/**
2178
+	 * For new events, this will be the default value for the maximum number of tickets (equivalent to maximum number of
2179
+	 * registrations)
2180
+	 *
2181
+	 * @var int
2182
+	 */
2183
+	public $default_maximum_number_of_tickets;
2184
+
2185
+	/**
2186
+	 * level of validation to apply to email addresses
2187
+	 *
2188
+	 * @var string $email_validation_level
2189
+	 * options: 'basic', 'wp_default', 'i18n', 'i18n_dns'
2190
+	 */
2191
+	public $email_validation_level;
2192
+
2193
+	/**
2194
+	 *    whether or not to show alternate payment options during the reg process if payment status is pending
2195
+	 *
2196
+	 * @var boolean $show_pending_payment_options
2197
+	 */
2198
+	public $show_pending_payment_options;
2199
+
2200
+	/**
2201
+	 * Whether to skip the registration confirmation page
2202
+	 *
2203
+	 * @var boolean $skip_reg_confirmation
2204
+	 */
2205
+	public $skip_reg_confirmation;
2206
+
2207
+	/**
2208
+	 * an array of SPCO reg steps where:
2209
+	 *        the keys denotes the reg step order
2210
+	 *        each element consists of an array with the following elements:
2211
+	 *            "file_path" => the file path to the EE_SPCO_Reg_Step class
2212
+	 *            "class_name" => the specific EE_SPCO_Reg_Step child class name
2213
+	 *            "slug" => the URL param used to trigger the reg step
2214
+	 *
2215
+	 * @var array $reg_steps
2216
+	 */
2217
+	public $reg_steps;
2218
+
2219
+	/**
2220
+	 * Whether registration confirmation should be the last page of SPCO
2221
+	 *
2222
+	 * @var boolean $reg_confirmation_last
2223
+	 */
2224
+	public $reg_confirmation_last;
2225
+
2226
+	/**
2227
+	 * Whether or not to enable the EE Bot Trap
2228
+	 *
2229
+	 * @var boolean $use_bot_trap
2230
+	 */
2231
+	public $use_bot_trap;
2232
+
2233
+	/**
2234
+	 * Whether or not to encrypt some data sent by the EE Bot Trap
2235
+	 *
2236
+	 * @var boolean $use_encryption
2237
+	 */
2238
+	public $use_encryption;
2239
+
2240
+	/**
2241
+	 * Whether or not to use ReCaptcha
2242
+	 *
2243
+	 * @var boolean $use_captcha
2244
+	 */
2245
+	public $use_captcha;
2246
+
2247
+	/**
2248
+	 * ReCaptcha Theme
2249
+	 *
2250
+	 * @var string $recaptcha_theme
2251
+	 *    options: 'dark', 'light', 'invisible'
2252
+	 */
2253
+	public $recaptcha_theme;
2254
+
2255
+	/**
2256
+	 * ReCaptcha Badge - determines the position of the reCAPTCHA badge if using Invisible ReCaptcha.
2257
+	 *
2258
+	 * @var string $recaptcha_badge
2259
+	 *    options: 'bottomright', 'bottomleft', 'inline'
2260
+	 */
2261
+	public $recaptcha_badge;
2262
+
2263
+	/**
2264
+	 * ReCaptcha Type
2265
+	 *
2266
+	 * @var string $recaptcha_type
2267
+	 *    options: 'audio', 'image'
2268
+	 */
2269
+	public $recaptcha_type;
2270
+
2271
+	/**
2272
+	 * ReCaptcha language
2273
+	 *
2274
+	 * @var string $recaptcha_language
2275
+	 * eg 'en'
2276
+	 */
2277
+	public $recaptcha_language;
2278
+
2279
+	/**
2280
+	 * ReCaptcha public key
2281
+	 *
2282
+	 * @var string $recaptcha_publickey
2283
+	 */
2284
+	public $recaptcha_publickey;
2285
+
2286
+	/**
2287
+	 * ReCaptcha private key
2288
+	 *
2289
+	 * @var string $recaptcha_privatekey
2290
+	 */
2291
+	public $recaptcha_privatekey;
2292
+
2293
+	/**
2294
+	 * array of form names protected by ReCaptcha
2295
+	 *
2296
+	 * @var array $recaptcha_protected_forms
2297
+	 */
2298
+	public $recaptcha_protected_forms;
2299
+
2300
+	/**
2301
+	 * ReCaptcha width
2302
+	 *
2303
+	 * @var int $recaptcha_width
2304
+	 * @deprecated
2305
+	 */
2306
+	public $recaptcha_width;
2307
+
2308
+	/**
2309
+	 * Whether or not invalid attempts to directly access the registration checkout page should be tracked.
2310
+	 *
2311
+	 * @var boolean $track_invalid_checkout_access
2312
+	 */
2313
+	protected $track_invalid_checkout_access = true;
2314
+
2315
+	/**
2316
+	 * Whether or not to show the privacy policy consent checkbox
2317
+	 *
2318
+	 * @var bool
2319
+	 */
2320
+	public $consent_checkbox_enabled;
2321
+
2322
+	/**
2323
+	 * Label text to show on the checkbox
2324
+	 *
2325
+	 * @var string
2326
+	 */
2327
+	public $consent_checkbox_label_text;
2328
+
2329
+	/*
2330 2330
      * String describing how long to keep payment logs. Passed into DateTime constructor
2331 2331
      * @var string
2332 2332
      */
2333
-    public $gateway_log_lifespan = '1 week';
2334
-
2335
-
2336
-    /**
2337
-     *    class constructor
2338
-     *
2339
-     * @access    public
2340
-     */
2341
-    public function __construct()
2342
-    {
2343
-        // set default registration settings
2344
-        $this->default_STS_ID = EEM_Registration::status_id_pending_payment;
2345
-        $this->email_validation_level = 'wp_default';
2346
-        $this->show_pending_payment_options = true;
2347
-        $this->skip_reg_confirmation = true;
2348
-        $this->reg_steps = array();
2349
-        $this->reg_confirmation_last = false;
2350
-        $this->use_bot_trap = true;
2351
-        $this->use_encryption = true;
2352
-        $this->use_captcha = false;
2353
-        $this->recaptcha_theme = 'light';
2354
-        $this->recaptcha_badge = 'bottomleft';
2355
-        $this->recaptcha_type = 'image';
2356
-        $this->recaptcha_language = 'en';
2357
-        $this->recaptcha_publickey = null;
2358
-        $this->recaptcha_privatekey = null;
2359
-        $this->recaptcha_protected_forms = array();
2360
-        $this->recaptcha_width = 500;
2361
-        $this->default_maximum_number_of_tickets = 10;
2362
-        $this->consent_checkbox_enabled = false;
2363
-        $this->consent_checkbox_label_text = '';
2364
-        $this->gateway_log_lifespan = '7 days';
2365
-    }
2366
-
2367
-
2368
-    /**
2369
-     * This is called by the config loader and hooks are initialized AFTER the config has been populated.
2370
-     *
2371
-     * @since 4.8.8.rc.019
2372
-     */
2373
-    public function do_hooks()
2374
-    {
2375
-        add_action('AHEE__EE_Config___load_core_config__end', array($this, 'set_default_reg_status_on_EEM_Event'));
2376
-        add_action('AHEE__EE_Config___load_core_config__end', array($this, 'set_default_max_ticket_on_EEM_Event'));
2377
-        add_action('setup_theme', array($this, 'setDefaultCheckboxLabelText'));
2378
-    }
2379
-
2380
-
2381
-    /**
2382
-     * Hooked into `AHEE__EE_Config___load_core_config__end` to ensure the default for the
2383
-     * EVT_default_registration_status field matches the config setting for default_STS_ID.
2384
-     */
2385
-    public function set_default_reg_status_on_EEM_Event()
2386
-    {
2387
-        EEM_Event::set_default_reg_status($this->default_STS_ID);
2388
-    }
2389
-
2390
-
2391
-    /**
2392
-     * Hooked into `AHEE__EE_Config___load_core_config__end` to ensure the default for the EVT_additional_limit field
2393
-     * for Events matches the config setting for default_maximum_number_of_tickets
2394
-     */
2395
-    public function set_default_max_ticket_on_EEM_Event()
2396
-    {
2397
-        EEM_Event::set_default_additional_limit($this->default_maximum_number_of_tickets);
2398
-    }
2399
-
2400
-
2401
-    /**
2402
-     * Sets the default consent checkbox text. This needs to be done a bit later than when EE_Registration_Config is
2403
-     * constructed because that happens before we can get the privacy policy page's permalink.
2404
-     *
2405
-     * @throws InvalidArgumentException
2406
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2407
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2408
-     */
2409
-    public function setDefaultCheckboxLabelText()
2410
-    {
2411
-        if ($this->getConsentCheckboxLabelText() === null
2412
-            || $this->getConsentCheckboxLabelText() === '') {
2413
-            $opening_a_tag = '';
2414
-            $closing_a_tag = '';
2415
-            if (function_exists('get_privacy_policy_url')) {
2416
-                $privacy_page_url = get_privacy_policy_url();
2417
-                if (! empty($privacy_page_url)) {
2418
-                    $opening_a_tag = '<a href="' . $privacy_page_url . '" target="_blank">';
2419
-                    $closing_a_tag = '</a>';
2420
-                }
2421
-            }
2422
-            $loader = LoaderFactory::getLoader();
2423
-            $org_config = $loader->getShared('EE_Organization_Config');
2424
-            /**
2425
-             * @var $org_config EE_Organization_Config
2426
-             */
2427
-
2428
-            $this->setConsentCheckboxLabelText(
2429
-                sprintf(
2430
-                    esc_html__(
2431
-                        'I consent to %1$s storing and using my personal information, according to their %2$sprivacy policy%3$s.',
2432
-                        'event_espresso'
2433
-                    ),
2434
-                    $org_config->name,
2435
-                    $opening_a_tag,
2436
-                    $closing_a_tag
2437
-                )
2438
-            );
2439
-        }
2440
-    }
2441
-
2442
-
2443
-    /**
2444
-     * @return boolean
2445
-     */
2446
-    public function track_invalid_checkout_access()
2447
-    {
2448
-        return $this->track_invalid_checkout_access;
2449
-    }
2450
-
2451
-
2452
-    /**
2453
-     * @param boolean $track_invalid_checkout_access
2454
-     */
2455
-    public function set_track_invalid_checkout_access($track_invalid_checkout_access)
2456
-    {
2457
-        $this->track_invalid_checkout_access = filter_var(
2458
-            $track_invalid_checkout_access,
2459
-            FILTER_VALIDATE_BOOLEAN
2460
-        );
2461
-    }
2462
-
2463
-
2464
-    /**
2465
-     * Gets the options to make availalbe for the gateway log lifespan
2466
-     * @return array
2467
-     */
2468
-    public function gatewayLogLifespanOptions()
2469
-    {
2470
-        return (array) apply_filters(
2471
-            'FHEE_EE_Admin_Config__gatewayLogLifespanOptions',
2472
-            array(
2473
-                '1 second' => esc_html__('Don\'t Log At All', 'event_espresso'),
2474
-                '1 day' => esc_html__('1 Day', 'event_espresso'),
2475
-                '7 days' => esc_html__('7 Days', 'event_espresso'),
2476
-                '14 days' => esc_html__('14 Days', 'event_espresso'),
2477
-                '30 days' => esc_html__('30 Days', 'event_espresso')
2478
-            )
2479
-        );
2480
-    }
2481
-
2482
-
2483
-    /**
2484
-     * @return bool
2485
-     */
2486
-    public function isConsentCheckboxEnabled()
2487
-    {
2488
-        return $this->consent_checkbox_enabled;
2489
-    }
2490
-
2491
-
2492
-    /**
2493
-     * @param bool $consent_checkbox_enabled
2494
-     */
2495
-    public function setConsentCheckboxEnabled($consent_checkbox_enabled)
2496
-    {
2497
-        $this->consent_checkbox_enabled = filter_var(
2498
-            $consent_checkbox_enabled,
2499
-            FILTER_VALIDATE_BOOLEAN
2500
-        );
2501
-    }
2502
-
2503
-
2504
-    /**
2505
-     * @return string
2506
-     */
2507
-    public function getConsentCheckboxLabelText()
2508
-    {
2509
-        return $this->consent_checkbox_label_text;
2510
-    }
2511
-
2512
-
2513
-    /**
2514
-     * @param string $consent_checkbox_label_text
2515
-     */
2516
-    public function setConsentCheckboxLabelText($consent_checkbox_label_text)
2517
-    {
2518
-        $this->consent_checkbox_label_text = (string) $consent_checkbox_label_text;
2519
-    }
2333
+	public $gateway_log_lifespan = '1 week';
2334
+
2335
+
2336
+	/**
2337
+	 *    class constructor
2338
+	 *
2339
+	 * @access    public
2340
+	 */
2341
+	public function __construct()
2342
+	{
2343
+		// set default registration settings
2344
+		$this->default_STS_ID = EEM_Registration::status_id_pending_payment;
2345
+		$this->email_validation_level = 'wp_default';
2346
+		$this->show_pending_payment_options = true;
2347
+		$this->skip_reg_confirmation = true;
2348
+		$this->reg_steps = array();
2349
+		$this->reg_confirmation_last = false;
2350
+		$this->use_bot_trap = true;
2351
+		$this->use_encryption = true;
2352
+		$this->use_captcha = false;
2353
+		$this->recaptcha_theme = 'light';
2354
+		$this->recaptcha_badge = 'bottomleft';
2355
+		$this->recaptcha_type = 'image';
2356
+		$this->recaptcha_language = 'en';
2357
+		$this->recaptcha_publickey = null;
2358
+		$this->recaptcha_privatekey = null;
2359
+		$this->recaptcha_protected_forms = array();
2360
+		$this->recaptcha_width = 500;
2361
+		$this->default_maximum_number_of_tickets = 10;
2362
+		$this->consent_checkbox_enabled = false;
2363
+		$this->consent_checkbox_label_text = '';
2364
+		$this->gateway_log_lifespan = '7 days';
2365
+	}
2366
+
2367
+
2368
+	/**
2369
+	 * This is called by the config loader and hooks are initialized AFTER the config has been populated.
2370
+	 *
2371
+	 * @since 4.8.8.rc.019
2372
+	 */
2373
+	public function do_hooks()
2374
+	{
2375
+		add_action('AHEE__EE_Config___load_core_config__end', array($this, 'set_default_reg_status_on_EEM_Event'));
2376
+		add_action('AHEE__EE_Config___load_core_config__end', array($this, 'set_default_max_ticket_on_EEM_Event'));
2377
+		add_action('setup_theme', array($this, 'setDefaultCheckboxLabelText'));
2378
+	}
2379
+
2380
+
2381
+	/**
2382
+	 * Hooked into `AHEE__EE_Config___load_core_config__end` to ensure the default for the
2383
+	 * EVT_default_registration_status field matches the config setting for default_STS_ID.
2384
+	 */
2385
+	public function set_default_reg_status_on_EEM_Event()
2386
+	{
2387
+		EEM_Event::set_default_reg_status($this->default_STS_ID);
2388
+	}
2389
+
2390
+
2391
+	/**
2392
+	 * Hooked into `AHEE__EE_Config___load_core_config__end` to ensure the default for the EVT_additional_limit field
2393
+	 * for Events matches the config setting for default_maximum_number_of_tickets
2394
+	 */
2395
+	public function set_default_max_ticket_on_EEM_Event()
2396
+	{
2397
+		EEM_Event::set_default_additional_limit($this->default_maximum_number_of_tickets);
2398
+	}
2399
+
2400
+
2401
+	/**
2402
+	 * Sets the default consent checkbox text. This needs to be done a bit later than when EE_Registration_Config is
2403
+	 * constructed because that happens before we can get the privacy policy page's permalink.
2404
+	 *
2405
+	 * @throws InvalidArgumentException
2406
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2407
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2408
+	 */
2409
+	public function setDefaultCheckboxLabelText()
2410
+	{
2411
+		if ($this->getConsentCheckboxLabelText() === null
2412
+			|| $this->getConsentCheckboxLabelText() === '') {
2413
+			$opening_a_tag = '';
2414
+			$closing_a_tag = '';
2415
+			if (function_exists('get_privacy_policy_url')) {
2416
+				$privacy_page_url = get_privacy_policy_url();
2417
+				if (! empty($privacy_page_url)) {
2418
+					$opening_a_tag = '<a href="' . $privacy_page_url . '" target="_blank">';
2419
+					$closing_a_tag = '</a>';
2420
+				}
2421
+			}
2422
+			$loader = LoaderFactory::getLoader();
2423
+			$org_config = $loader->getShared('EE_Organization_Config');
2424
+			/**
2425
+			 * @var $org_config EE_Organization_Config
2426
+			 */
2427
+
2428
+			$this->setConsentCheckboxLabelText(
2429
+				sprintf(
2430
+					esc_html__(
2431
+						'I consent to %1$s storing and using my personal information, according to their %2$sprivacy policy%3$s.',
2432
+						'event_espresso'
2433
+					),
2434
+					$org_config->name,
2435
+					$opening_a_tag,
2436
+					$closing_a_tag
2437
+				)
2438
+			);
2439
+		}
2440
+	}
2441
+
2442
+
2443
+	/**
2444
+	 * @return boolean
2445
+	 */
2446
+	public function track_invalid_checkout_access()
2447
+	{
2448
+		return $this->track_invalid_checkout_access;
2449
+	}
2450
+
2451
+
2452
+	/**
2453
+	 * @param boolean $track_invalid_checkout_access
2454
+	 */
2455
+	public function set_track_invalid_checkout_access($track_invalid_checkout_access)
2456
+	{
2457
+		$this->track_invalid_checkout_access = filter_var(
2458
+			$track_invalid_checkout_access,
2459
+			FILTER_VALIDATE_BOOLEAN
2460
+		);
2461
+	}
2462
+
2463
+
2464
+	/**
2465
+	 * Gets the options to make availalbe for the gateway log lifespan
2466
+	 * @return array
2467
+	 */
2468
+	public function gatewayLogLifespanOptions()
2469
+	{
2470
+		return (array) apply_filters(
2471
+			'FHEE_EE_Admin_Config__gatewayLogLifespanOptions',
2472
+			array(
2473
+				'1 second' => esc_html__('Don\'t Log At All', 'event_espresso'),
2474
+				'1 day' => esc_html__('1 Day', 'event_espresso'),
2475
+				'7 days' => esc_html__('7 Days', 'event_espresso'),
2476
+				'14 days' => esc_html__('14 Days', 'event_espresso'),
2477
+				'30 days' => esc_html__('30 Days', 'event_espresso')
2478
+			)
2479
+		);
2480
+	}
2481
+
2482
+
2483
+	/**
2484
+	 * @return bool
2485
+	 */
2486
+	public function isConsentCheckboxEnabled()
2487
+	{
2488
+		return $this->consent_checkbox_enabled;
2489
+	}
2490
+
2491
+
2492
+	/**
2493
+	 * @param bool $consent_checkbox_enabled
2494
+	 */
2495
+	public function setConsentCheckboxEnabled($consent_checkbox_enabled)
2496
+	{
2497
+		$this->consent_checkbox_enabled = filter_var(
2498
+			$consent_checkbox_enabled,
2499
+			FILTER_VALIDATE_BOOLEAN
2500
+		);
2501
+	}
2502
+
2503
+
2504
+	/**
2505
+	 * @return string
2506
+	 */
2507
+	public function getConsentCheckboxLabelText()
2508
+	{
2509
+		return $this->consent_checkbox_label_text;
2510
+	}
2511
+
2512
+
2513
+	/**
2514
+	 * @param string $consent_checkbox_label_text
2515
+	 */
2516
+	public function setConsentCheckboxLabelText($consent_checkbox_label_text)
2517
+	{
2518
+		$this->consent_checkbox_label_text = (string) $consent_checkbox_label_text;
2519
+	}
2520 2520
 }
2521 2521
 
2522 2522
 /**
@@ -2525,154 +2525,154 @@  discard block
 block discarded – undo
2525 2525
 class EE_Admin_Config extends EE_Config_Base
2526 2526
 {
2527 2527
 
2528
-    /**
2529
-     * @var boolean $use_personnel_manager
2530
-     */
2531
-    public $use_personnel_manager;
2532
-
2533
-    /**
2534
-     * @var boolean $use_dashboard_widget
2535
-     */
2536
-    public $use_dashboard_widget;
2537
-
2538
-    /**
2539
-     * @var int $events_in_dashboard
2540
-     */
2541
-    public $events_in_dashboard;
2542
-
2543
-    /**
2544
-     * @var boolean $use_event_timezones
2545
-     */
2546
-    public $use_event_timezones;
2547
-
2548
-    /**
2549
-     * @var boolean $use_full_logging
2550
-     */
2551
-    public $use_full_logging;
2552
-
2553
-    /**
2554
-     * @var string $log_file_name
2555
-     */
2556
-    public $log_file_name;
2557
-
2558
-    /**
2559
-     * @var string $debug_file_name
2560
-     */
2561
-    public $debug_file_name;
2562
-
2563
-    /**
2564
-     * @var boolean $use_remote_logging
2565
-     */
2566
-    public $use_remote_logging;
2567
-
2568
-    /**
2569
-     * @var string $remote_logging_url
2570
-     */
2571
-    public $remote_logging_url;
2572
-
2573
-    /**
2574
-     * @var boolean $show_reg_footer
2575
-     */
2576
-    public $show_reg_footer;
2577
-
2578
-    /**
2579
-     * @var string $affiliate_id
2580
-     */
2581
-    public $affiliate_id;
2582
-
2583
-    /**
2584
-     * help tours on or off (global setting)
2585
-     *
2586
-     * @var boolean
2587
-     */
2588
-    public $help_tour_activation;
2589
-
2590
-    /**
2591
-     * adds extra layer of encoding to session data to prevent serialization errors
2592
-     * but is incompatible with some server configuration errors
2593
-     * if you get "500 internal server errors" during registration, try turning this on
2594
-     * if you get PHP fatal errors regarding base 64 methods not defined, then turn this off
2595
-     *
2596
-     * @var boolean $encode_session_data
2597
-     */
2598
-    private $encode_session_data = false;
2599
-
2600
-
2601
-    /**
2602
-     *    class constructor
2603
-     *
2604
-     * @access    public
2605
-     */
2606
-    public function __construct()
2607
-    {
2608
-        // set default general admin settings
2609
-        $this->use_personnel_manager = true;
2610
-        $this->use_dashboard_widget = true;
2611
-        $this->events_in_dashboard = 30;
2612
-        $this->use_event_timezones = false;
2613
-        $this->use_full_logging = false;
2614
-        $this->use_remote_logging = false;
2615
-        $this->remote_logging_url = null;
2616
-        $this->show_reg_footer = true;
2617
-        $this->affiliate_id = 'default';
2618
-        $this->help_tour_activation = true;
2619
-        $this->encode_session_data = false;
2620
-    }
2621
-
2622
-
2623
-    /**
2624
-     * @param bool $reset
2625
-     * @return string
2626
-     */
2627
-    public function log_file_name($reset = false)
2628
-    {
2629
-        if (empty($this->log_file_name) || $reset) {
2630
-            $this->log_file_name = sanitize_key('espresso_log_' . md5(uniqid('', true))) . '.txt';
2631
-            EE_Config::instance()->update_espresso_config(false, false);
2632
-        }
2633
-        return $this->log_file_name;
2634
-    }
2635
-
2636
-
2637
-    /**
2638
-     * @param bool $reset
2639
-     * @return string
2640
-     */
2641
-    public function debug_file_name($reset = false)
2642
-    {
2643
-        if (empty($this->debug_file_name) || $reset) {
2644
-            $this->debug_file_name = sanitize_key('espresso_debug_' . md5(uniqid('', true))) . '.txt';
2645
-            EE_Config::instance()->update_espresso_config(false, false);
2646
-        }
2647
-        return $this->debug_file_name;
2648
-    }
2649
-
2650
-
2651
-    /**
2652
-     * @return string
2653
-     */
2654
-    public function affiliate_id()
2655
-    {
2656
-        return ! empty($this->affiliate_id) ? $this->affiliate_id : 'default';
2657
-    }
2658
-
2659
-
2660
-    /**
2661
-     * @return boolean
2662
-     */
2663
-    public function encode_session_data()
2664
-    {
2665
-        return filter_var($this->encode_session_data, FILTER_VALIDATE_BOOLEAN);
2666
-    }
2667
-
2668
-
2669
-    /**
2670
-     * @param boolean $encode_session_data
2671
-     */
2672
-    public function set_encode_session_data($encode_session_data)
2673
-    {
2674
-        $this->encode_session_data = filter_var($encode_session_data, FILTER_VALIDATE_BOOLEAN);
2675
-    }
2528
+	/**
2529
+	 * @var boolean $use_personnel_manager
2530
+	 */
2531
+	public $use_personnel_manager;
2532
+
2533
+	/**
2534
+	 * @var boolean $use_dashboard_widget
2535
+	 */
2536
+	public $use_dashboard_widget;
2537
+
2538
+	/**
2539
+	 * @var int $events_in_dashboard
2540
+	 */
2541
+	public $events_in_dashboard;
2542
+
2543
+	/**
2544
+	 * @var boolean $use_event_timezones
2545
+	 */
2546
+	public $use_event_timezones;
2547
+
2548
+	/**
2549
+	 * @var boolean $use_full_logging
2550
+	 */
2551
+	public $use_full_logging;
2552
+
2553
+	/**
2554
+	 * @var string $log_file_name
2555
+	 */
2556
+	public $log_file_name;
2557
+
2558
+	/**
2559
+	 * @var string $debug_file_name
2560
+	 */
2561
+	public $debug_file_name;
2562
+
2563
+	/**
2564
+	 * @var boolean $use_remote_logging
2565
+	 */
2566
+	public $use_remote_logging;
2567
+
2568
+	/**
2569
+	 * @var string $remote_logging_url
2570
+	 */
2571
+	public $remote_logging_url;
2572
+
2573
+	/**
2574
+	 * @var boolean $show_reg_footer
2575
+	 */
2576
+	public $show_reg_footer;
2577
+
2578
+	/**
2579
+	 * @var string $affiliate_id
2580
+	 */
2581
+	public $affiliate_id;
2582
+
2583
+	/**
2584
+	 * help tours on or off (global setting)
2585
+	 *
2586
+	 * @var boolean
2587
+	 */
2588
+	public $help_tour_activation;
2589
+
2590
+	/**
2591
+	 * adds extra layer of encoding to session data to prevent serialization errors
2592
+	 * but is incompatible with some server configuration errors
2593
+	 * if you get "500 internal server errors" during registration, try turning this on
2594
+	 * if you get PHP fatal errors regarding base 64 methods not defined, then turn this off
2595
+	 *
2596
+	 * @var boolean $encode_session_data
2597
+	 */
2598
+	private $encode_session_data = false;
2599
+
2600
+
2601
+	/**
2602
+	 *    class constructor
2603
+	 *
2604
+	 * @access    public
2605
+	 */
2606
+	public function __construct()
2607
+	{
2608
+		// set default general admin settings
2609
+		$this->use_personnel_manager = true;
2610
+		$this->use_dashboard_widget = true;
2611
+		$this->events_in_dashboard = 30;
2612
+		$this->use_event_timezones = false;
2613
+		$this->use_full_logging = false;
2614
+		$this->use_remote_logging = false;
2615
+		$this->remote_logging_url = null;
2616
+		$this->show_reg_footer = true;
2617
+		$this->affiliate_id = 'default';
2618
+		$this->help_tour_activation = true;
2619
+		$this->encode_session_data = false;
2620
+	}
2621
+
2622
+
2623
+	/**
2624
+	 * @param bool $reset
2625
+	 * @return string
2626
+	 */
2627
+	public function log_file_name($reset = false)
2628
+	{
2629
+		if (empty($this->log_file_name) || $reset) {
2630
+			$this->log_file_name = sanitize_key('espresso_log_' . md5(uniqid('', true))) . '.txt';
2631
+			EE_Config::instance()->update_espresso_config(false, false);
2632
+		}
2633
+		return $this->log_file_name;
2634
+	}
2635
+
2636
+
2637
+	/**
2638
+	 * @param bool $reset
2639
+	 * @return string
2640
+	 */
2641
+	public function debug_file_name($reset = false)
2642
+	{
2643
+		if (empty($this->debug_file_name) || $reset) {
2644
+			$this->debug_file_name = sanitize_key('espresso_debug_' . md5(uniqid('', true))) . '.txt';
2645
+			EE_Config::instance()->update_espresso_config(false, false);
2646
+		}
2647
+		return $this->debug_file_name;
2648
+	}
2649
+
2650
+
2651
+	/**
2652
+	 * @return string
2653
+	 */
2654
+	public function affiliate_id()
2655
+	{
2656
+		return ! empty($this->affiliate_id) ? $this->affiliate_id : 'default';
2657
+	}
2658
+
2659
+
2660
+	/**
2661
+	 * @return boolean
2662
+	 */
2663
+	public function encode_session_data()
2664
+	{
2665
+		return filter_var($this->encode_session_data, FILTER_VALIDATE_BOOLEAN);
2666
+	}
2667
+
2668
+
2669
+	/**
2670
+	 * @param boolean $encode_session_data
2671
+	 */
2672
+	public function set_encode_session_data($encode_session_data)
2673
+	{
2674
+		$this->encode_session_data = filter_var($encode_session_data, FILTER_VALIDATE_BOOLEAN);
2675
+	}
2676 2676
 }
2677 2677
 
2678 2678
 /**
@@ -2681,70 +2681,70 @@  discard block
 block discarded – undo
2681 2681
 class EE_Template_Config extends EE_Config_Base
2682 2682
 {
2683 2683
 
2684
-    /**
2685
-     * @var boolean $enable_default_style
2686
-     */
2687
-    public $enable_default_style;
2688
-
2689
-    /**
2690
-     * @var string $custom_style_sheet
2691
-     */
2692
-    public $custom_style_sheet;
2693
-
2694
-    /**
2695
-     * @var boolean $display_address_in_regform
2696
-     */
2697
-    public $display_address_in_regform;
2698
-
2699
-    /**
2700
-     * @var int $display_description_on_multi_reg_page
2701
-     */
2702
-    public $display_description_on_multi_reg_page;
2703
-
2704
-    /**
2705
-     * @var boolean $use_custom_templates
2706
-     */
2707
-    public $use_custom_templates;
2708
-
2709
-    /**
2710
-     * @var string $current_espresso_theme
2711
-     */
2712
-    public $current_espresso_theme;
2713
-
2714
-    /**
2715
-     * @var EE_Ticket_Selector_Config $EED_Ticket_Selector
2716
-     */
2717
-    public $EED_Ticket_Selector;
2718
-
2719
-    /**
2720
-     * @var EE_Event_Single_Config $EED_Event_Single
2721
-     */
2722
-    public $EED_Event_Single;
2723
-
2724
-    /**
2725
-     * @var EE_Events_Archive_Config $EED_Events_Archive
2726
-     */
2727
-    public $EED_Events_Archive;
2728
-
2729
-
2730
-    /**
2731
-     *    class constructor
2732
-     *
2733
-     * @access    public
2734
-     */
2735
-    public function __construct()
2736
-    {
2737
-        // set default template settings
2738
-        $this->enable_default_style = true;
2739
-        $this->custom_style_sheet = null;
2740
-        $this->display_address_in_regform = true;
2741
-        $this->display_description_on_multi_reg_page = false;
2742
-        $this->use_custom_templates = false;
2743
-        $this->current_espresso_theme = 'Espresso_Arabica_2014';
2744
-        $this->EED_Event_Single = null;
2745
-        $this->EED_Events_Archive = null;
2746
-        $this->EED_Ticket_Selector = null;
2747
-    }
2684
+	/**
2685
+	 * @var boolean $enable_default_style
2686
+	 */
2687
+	public $enable_default_style;
2688
+
2689
+	/**
2690
+	 * @var string $custom_style_sheet
2691
+	 */
2692
+	public $custom_style_sheet;
2693
+
2694
+	/**
2695
+	 * @var boolean $display_address_in_regform
2696
+	 */
2697
+	public $display_address_in_regform;
2698
+
2699
+	/**
2700
+	 * @var int $display_description_on_multi_reg_page
2701
+	 */
2702
+	public $display_description_on_multi_reg_page;
2703
+
2704
+	/**
2705
+	 * @var boolean $use_custom_templates
2706
+	 */
2707
+	public $use_custom_templates;
2708
+
2709
+	/**
2710
+	 * @var string $current_espresso_theme
2711
+	 */
2712
+	public $current_espresso_theme;
2713
+
2714
+	/**
2715
+	 * @var EE_Ticket_Selector_Config $EED_Ticket_Selector
2716
+	 */
2717
+	public $EED_Ticket_Selector;
2718
+
2719
+	/**
2720
+	 * @var EE_Event_Single_Config $EED_Event_Single
2721
+	 */
2722
+	public $EED_Event_Single;
2723
+
2724
+	/**
2725
+	 * @var EE_Events_Archive_Config $EED_Events_Archive
2726
+	 */
2727
+	public $EED_Events_Archive;
2728
+
2729
+
2730
+	/**
2731
+	 *    class constructor
2732
+	 *
2733
+	 * @access    public
2734
+	 */
2735
+	public function __construct()
2736
+	{
2737
+		// set default template settings
2738
+		$this->enable_default_style = true;
2739
+		$this->custom_style_sheet = null;
2740
+		$this->display_address_in_regform = true;
2741
+		$this->display_description_on_multi_reg_page = false;
2742
+		$this->use_custom_templates = false;
2743
+		$this->current_espresso_theme = 'Espresso_Arabica_2014';
2744
+		$this->EED_Event_Single = null;
2745
+		$this->EED_Events_Archive = null;
2746
+		$this->EED_Ticket_Selector = null;
2747
+	}
2748 2748
 }
2749 2749
 
2750 2750
 /**
@@ -2753,114 +2753,114 @@  discard block
 block discarded – undo
2753 2753
 class EE_Map_Config extends EE_Config_Base
2754 2754
 {
2755 2755
 
2756
-    /**
2757
-     * @var boolean $use_google_maps
2758
-     */
2759
-    public $use_google_maps;
2760
-
2761
-    /**
2762
-     * @var string $api_key
2763
-     */
2764
-    public $google_map_api_key;
2765
-
2766
-    /**
2767
-     * @var int $event_details_map_width
2768
-     */
2769
-    public $event_details_map_width;
2770
-
2771
-    /**
2772
-     * @var int $event_details_map_height
2773
-     */
2774
-    public $event_details_map_height;
2775
-
2776
-    /**
2777
-     * @var int $event_details_map_zoom
2778
-     */
2779
-    public $event_details_map_zoom;
2780
-
2781
-    /**
2782
-     * @var boolean $event_details_display_nav
2783
-     */
2784
-    public $event_details_display_nav;
2785
-
2786
-    /**
2787
-     * @var boolean $event_details_nav_size
2788
-     */
2789
-    public $event_details_nav_size;
2790
-
2791
-    /**
2792
-     * @var string $event_details_control_type
2793
-     */
2794
-    public $event_details_control_type;
2795
-
2796
-    /**
2797
-     * @var string $event_details_map_align
2798
-     */
2799
-    public $event_details_map_align;
2800
-
2801
-    /**
2802
-     * @var int $event_list_map_width
2803
-     */
2804
-    public $event_list_map_width;
2805
-
2806
-    /**
2807
-     * @var int $event_list_map_height
2808
-     */
2809
-    public $event_list_map_height;
2810
-
2811
-    /**
2812
-     * @var int $event_list_map_zoom
2813
-     */
2814
-    public $event_list_map_zoom;
2815
-
2816
-    /**
2817
-     * @var boolean $event_list_display_nav
2818
-     */
2819
-    public $event_list_display_nav;
2820
-
2821
-    /**
2822
-     * @var boolean $event_list_nav_size
2823
-     */
2824
-    public $event_list_nav_size;
2825
-
2826
-    /**
2827
-     * @var string $event_list_control_type
2828
-     */
2829
-    public $event_list_control_type;
2830
-
2831
-    /**
2832
-     * @var string $event_list_map_align
2833
-     */
2834
-    public $event_list_map_align;
2835
-
2836
-
2837
-    /**
2838
-     *    class constructor
2839
-     *
2840
-     * @access    public
2841
-     */
2842
-    public function __construct()
2843
-    {
2844
-        // set default map settings
2845
-        $this->use_google_maps = true;
2846
-        $this->google_map_api_key = '';
2847
-        // for event details pages (reg page)
2848
-        $this->event_details_map_width = 585;            // ee_map_width_single
2849
-        $this->event_details_map_height = 362;            // ee_map_height_single
2850
-        $this->event_details_map_zoom = 14;            // ee_map_zoom_single
2851
-        $this->event_details_display_nav = true;            // ee_map_nav_display_single
2852
-        $this->event_details_nav_size = false;            // ee_map_nav_size_single
2853
-        $this->event_details_control_type = 'default';        // ee_map_type_control_single
2854
-        $this->event_details_map_align = 'center';            // ee_map_align_single
2855
-        // for event list pages
2856
-        $this->event_list_map_width = 300;            // ee_map_width
2857
-        $this->event_list_map_height = 185;        // ee_map_height
2858
-        $this->event_list_map_zoom = 12;            // ee_map_zoom
2859
-        $this->event_list_display_nav = false;        // ee_map_nav_display
2860
-        $this->event_list_nav_size = true;            // ee_map_nav_size
2861
-        $this->event_list_control_type = 'dropdown';        // ee_map_type_control
2862
-        $this->event_list_map_align = 'center';            // ee_map_align
2863
-    }
2756
+	/**
2757
+	 * @var boolean $use_google_maps
2758
+	 */
2759
+	public $use_google_maps;
2760
+
2761
+	/**
2762
+	 * @var string $api_key
2763
+	 */
2764
+	public $google_map_api_key;
2765
+
2766
+	/**
2767
+	 * @var int $event_details_map_width
2768
+	 */
2769
+	public $event_details_map_width;
2770
+
2771
+	/**
2772
+	 * @var int $event_details_map_height
2773
+	 */
2774
+	public $event_details_map_height;
2775
+
2776
+	/**
2777
+	 * @var int $event_details_map_zoom
2778
+	 */
2779
+	public $event_details_map_zoom;
2780
+
2781
+	/**
2782
+	 * @var boolean $event_details_display_nav
2783
+	 */
2784
+	public $event_details_display_nav;
2785
+
2786
+	/**
2787
+	 * @var boolean $event_details_nav_size
2788
+	 */
2789
+	public $event_details_nav_size;
2790
+
2791
+	/**
2792
+	 * @var string $event_details_control_type
2793
+	 */
2794
+	public $event_details_control_type;
2795
+
2796
+	/**
2797
+	 * @var string $event_details_map_align
2798
+	 */
2799
+	public $event_details_map_align;
2800
+
2801
+	/**
2802
+	 * @var int $event_list_map_width
2803
+	 */
2804
+	public $event_list_map_width;
2805
+
2806
+	/**
2807
+	 * @var int $event_list_map_height
2808
+	 */
2809
+	public $event_list_map_height;
2810
+
2811
+	/**
2812
+	 * @var int $event_list_map_zoom
2813
+	 */
2814
+	public $event_list_map_zoom;
2815
+
2816
+	/**
2817
+	 * @var boolean $event_list_display_nav
2818
+	 */
2819
+	public $event_list_display_nav;
2820
+
2821
+	/**
2822
+	 * @var boolean $event_list_nav_size
2823
+	 */
2824
+	public $event_list_nav_size;
2825
+
2826
+	/**
2827
+	 * @var string $event_list_control_type
2828
+	 */
2829
+	public $event_list_control_type;
2830
+
2831
+	/**
2832
+	 * @var string $event_list_map_align
2833
+	 */
2834
+	public $event_list_map_align;
2835
+
2836
+
2837
+	/**
2838
+	 *    class constructor
2839
+	 *
2840
+	 * @access    public
2841
+	 */
2842
+	public function __construct()
2843
+	{
2844
+		// set default map settings
2845
+		$this->use_google_maps = true;
2846
+		$this->google_map_api_key = '';
2847
+		// for event details pages (reg page)
2848
+		$this->event_details_map_width = 585;            // ee_map_width_single
2849
+		$this->event_details_map_height = 362;            // ee_map_height_single
2850
+		$this->event_details_map_zoom = 14;            // ee_map_zoom_single
2851
+		$this->event_details_display_nav = true;            // ee_map_nav_display_single
2852
+		$this->event_details_nav_size = false;            // ee_map_nav_size_single
2853
+		$this->event_details_control_type = 'default';        // ee_map_type_control_single
2854
+		$this->event_details_map_align = 'center';            // ee_map_align_single
2855
+		// for event list pages
2856
+		$this->event_list_map_width = 300;            // ee_map_width
2857
+		$this->event_list_map_height = 185;        // ee_map_height
2858
+		$this->event_list_map_zoom = 12;            // ee_map_zoom
2859
+		$this->event_list_display_nav = false;        // ee_map_nav_display
2860
+		$this->event_list_nav_size = true;            // ee_map_nav_size
2861
+		$this->event_list_control_type = 'dropdown';        // ee_map_type_control
2862
+		$this->event_list_map_align = 'center';            // ee_map_align
2863
+	}
2864 2864
 }
2865 2865
 
2866 2866
 /**
@@ -2869,46 +2869,46 @@  discard block
 block discarded – undo
2869 2869
 class EE_Events_Archive_Config extends EE_Config_Base
2870 2870
 {
2871 2871
 
2872
-    public $display_status_banner;
2872
+	public $display_status_banner;
2873 2873
 
2874
-    public $display_description;
2874
+	public $display_description;
2875 2875
 
2876
-    public $display_ticket_selector;
2876
+	public $display_ticket_selector;
2877 2877
 
2878
-    public $display_datetimes;
2878
+	public $display_datetimes;
2879 2879
 
2880
-    public $display_venue;
2880
+	public $display_venue;
2881 2881
 
2882
-    public $display_expired_events;
2882
+	public $display_expired_events;
2883 2883
 
2884
-    public $use_sortable_display_order;
2884
+	public $use_sortable_display_order;
2885 2885
 
2886
-    public $display_order_tickets;
2886
+	public $display_order_tickets;
2887 2887
 
2888
-    public $display_order_datetimes;
2888
+	public $display_order_datetimes;
2889 2889
 
2890
-    public $display_order_event;
2890
+	public $display_order_event;
2891 2891
 
2892
-    public $display_order_venue;
2892
+	public $display_order_venue;
2893 2893
 
2894 2894
 
2895
-    /**
2896
-     *    class constructor
2897
-     */
2898
-    public function __construct()
2899
-    {
2900
-        $this->display_status_banner = 0;
2901
-        $this->display_description = 1;
2902
-        $this->display_ticket_selector = 0;
2903
-        $this->display_datetimes = 1;
2904
-        $this->display_venue = 0;
2905
-        $this->display_expired_events = 0;
2906
-        $this->use_sortable_display_order = false;
2907
-        $this->display_order_tickets = 100;
2908
-        $this->display_order_datetimes = 110;
2909
-        $this->display_order_event = 120;
2910
-        $this->display_order_venue = 130;
2911
-    }
2895
+	/**
2896
+	 *    class constructor
2897
+	 */
2898
+	public function __construct()
2899
+	{
2900
+		$this->display_status_banner = 0;
2901
+		$this->display_description = 1;
2902
+		$this->display_ticket_selector = 0;
2903
+		$this->display_datetimes = 1;
2904
+		$this->display_venue = 0;
2905
+		$this->display_expired_events = 0;
2906
+		$this->use_sortable_display_order = false;
2907
+		$this->display_order_tickets = 100;
2908
+		$this->display_order_datetimes = 110;
2909
+		$this->display_order_event = 120;
2910
+		$this->display_order_venue = 130;
2911
+	}
2912 2912
 }
2913 2913
 
2914 2914
 /**
@@ -2917,34 +2917,34 @@  discard block
 block discarded – undo
2917 2917
 class EE_Event_Single_Config extends EE_Config_Base
2918 2918
 {
2919 2919
 
2920
-    public $display_status_banner_single;
2920
+	public $display_status_banner_single;
2921 2921
 
2922
-    public $display_venue;
2922
+	public $display_venue;
2923 2923
 
2924
-    public $use_sortable_display_order;
2924
+	public $use_sortable_display_order;
2925 2925
 
2926
-    public $display_order_tickets;
2926
+	public $display_order_tickets;
2927 2927
 
2928
-    public $display_order_datetimes;
2928
+	public $display_order_datetimes;
2929 2929
 
2930
-    public $display_order_event;
2930
+	public $display_order_event;
2931 2931
 
2932
-    public $display_order_venue;
2932
+	public $display_order_venue;
2933 2933
 
2934 2934
 
2935
-    /**
2936
-     *    class constructor
2937
-     */
2938
-    public function __construct()
2939
-    {
2940
-        $this->display_status_banner_single = 0;
2941
-        $this->display_venue = 1;
2942
-        $this->use_sortable_display_order = false;
2943
-        $this->display_order_tickets = 100;
2944
-        $this->display_order_datetimes = 110;
2945
-        $this->display_order_event = 120;
2946
-        $this->display_order_venue = 130;
2947
-    }
2935
+	/**
2936
+	 *    class constructor
2937
+	 */
2938
+	public function __construct()
2939
+	{
2940
+		$this->display_status_banner_single = 0;
2941
+		$this->display_venue = 1;
2942
+		$this->use_sortable_display_order = false;
2943
+		$this->display_order_tickets = 100;
2944
+		$this->display_order_datetimes = 110;
2945
+		$this->display_order_event = 120;
2946
+		$this->display_order_venue = 130;
2947
+	}
2948 2948
 }
2949 2949
 
2950 2950
 /**
@@ -2953,172 +2953,172 @@  discard block
 block discarded – undo
2953 2953
 class EE_Ticket_Selector_Config extends EE_Config_Base
2954 2954
 {
2955 2955
 
2956
-    /**
2957
-     * constant to indicate that a datetime selector should NEVER be shown for ticket selectors
2958
-     */
2959
-    const DO_NOT_SHOW_DATETIME_SELECTOR = 'no_datetime_selector';
2960
-
2961
-    /**
2962
-     * constant to indicate that a datetime selector should only be shown for ticket selectors
2963
-     * when the number of datetimes for the event matches the value set for $datetime_selector_threshold
2964
-     */
2965
-    const MAYBE_SHOW_DATETIME_SELECTOR = 'maybe_datetime_selector';
2966
-
2967
-    /**
2968
-     * @var boolean $show_ticket_sale_columns
2969
-     */
2970
-    public $show_ticket_sale_columns;
2971
-
2972
-    /**
2973
-     * @var boolean $show_ticket_details
2974
-     */
2975
-    public $show_ticket_details;
2976
-
2977
-    /**
2978
-     * @var boolean $show_expired_tickets
2979
-     */
2980
-    public $show_expired_tickets;
2981
-
2982
-    /**
2983
-     * whether or not to display a dropdown box populated with event datetimes
2984
-     * that toggles which tickets are displayed for a ticket selector.
2985
-     * uses one of the *_DATETIME_SELECTOR constants defined above
2986
-     *
2987
-     * @var string $show_datetime_selector
2988
-     */
2989
-    private $show_datetime_selector = 'no_datetime_selector';
2990
-
2991
-    /**
2992
-     * the number of datetimes an event has to have before conditionally displaying a datetime selector
2993
-     *
2994
-     * @var int $datetime_selector_threshold
2995
-     */
2996
-    private $datetime_selector_threshold = 3;
2997
-
2998
-    /**
2999
-     * determines the maximum number of "checked" dates in the date and time filter
3000
-     *
3001
-     * @var int $datetime_selector_checked
3002
-     */
3003
-    private $datetime_selector_max_checked = 10;
3004
-
3005
-
3006
-    /**
3007
-     *    class constructor
3008
-     */
3009
-    public function __construct()
3010
-    {
3011
-        $this->show_ticket_sale_columns = true;
3012
-        $this->show_ticket_details = true;
3013
-        $this->show_expired_tickets = true;
3014
-        $this->show_datetime_selector = \EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR;
3015
-        $this->datetime_selector_threshold = 3;
3016
-        $this->datetime_selector_max_checked = 10;
3017
-    }
3018
-
3019
-
3020
-    /**
3021
-     * returns true if a datetime selector should be displayed
3022
-     *
3023
-     * @param array $datetimes
3024
-     * @return bool
3025
-     */
3026
-    public function showDatetimeSelector(array $datetimes)
3027
-    {
3028
-        // if the settings are NOT: don't show OR below threshold, THEN active = true
3029
-        return ! (
3030
-            $this->getShowDatetimeSelector() === \EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR
3031
-            || (
3032
-                $this->getShowDatetimeSelector() === \EE_Ticket_Selector_Config::MAYBE_SHOW_DATETIME_SELECTOR
3033
-                && count($datetimes) < $this->getDatetimeSelectorThreshold()
3034
-            )
3035
-        );
3036
-    }
3037
-
3038
-
3039
-    /**
3040
-     * @return string
3041
-     */
3042
-    public function getShowDatetimeSelector()
3043
-    {
3044
-        return $this->show_datetime_selector;
3045
-    }
3046
-
3047
-
3048
-    /**
3049
-     * @param bool $keys_only
3050
-     * @return array
3051
-     */
3052
-    public function getShowDatetimeSelectorOptions($keys_only = true)
3053
-    {
3054
-        return $keys_only
3055
-            ? array(
3056
-                \EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR,
3057
-                \EE_Ticket_Selector_Config::MAYBE_SHOW_DATETIME_SELECTOR,
3058
-            )
3059
-            : array(
3060
-                \EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR => esc_html__(
3061
-                    'Do not show date & time filter',
3062
-                    'event_espresso'
3063
-                ),
3064
-                \EE_Ticket_Selector_Config::MAYBE_SHOW_DATETIME_SELECTOR  => esc_html__(
3065
-                    'Maybe show date & time filter',
3066
-                    'event_espresso'
3067
-                ),
3068
-            );
3069
-    }
3070
-
3071
-
3072
-    /**
3073
-     * @param string $show_datetime_selector
3074
-     */
3075
-    public function setShowDatetimeSelector($show_datetime_selector)
3076
-    {
3077
-        $this->show_datetime_selector = in_array(
3078
-            $show_datetime_selector,
3079
-            $this->getShowDatetimeSelectorOptions(),
3080
-            true
3081
-        )
3082
-            ? $show_datetime_selector
3083
-            : \EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR;
3084
-    }
3085
-
3086
-
3087
-    /**
3088
-     * @return int
3089
-     */
3090
-    public function getDatetimeSelectorThreshold()
3091
-    {
3092
-        return $this->datetime_selector_threshold;
3093
-    }
3094
-
3095
-
3096
-    /**
3097
-     * @param int $datetime_selector_threshold
3098
-     */
3099
-    public function setDatetimeSelectorThreshold($datetime_selector_threshold)
3100
-    {
3101
-        $datetime_selector_threshold = absint($datetime_selector_threshold);
3102
-        $this->datetime_selector_threshold = $datetime_selector_threshold ? $datetime_selector_threshold : 3;
3103
-    }
3104
-
3105
-
3106
-    /**
3107
-     * @return int
3108
-     */
3109
-    public function getDatetimeSelectorMaxChecked()
3110
-    {
3111
-        return $this->datetime_selector_max_checked;
3112
-    }
3113
-
3114
-
3115
-    /**
3116
-     * @param int $datetime_selector_max_checked
3117
-     */
3118
-    public function setDatetimeSelectorMaxChecked($datetime_selector_max_checked)
3119
-    {
3120
-        $this->datetime_selector_max_checked = absint($datetime_selector_max_checked);
3121
-    }
2956
+	/**
2957
+	 * constant to indicate that a datetime selector should NEVER be shown for ticket selectors
2958
+	 */
2959
+	const DO_NOT_SHOW_DATETIME_SELECTOR = 'no_datetime_selector';
2960
+
2961
+	/**
2962
+	 * constant to indicate that a datetime selector should only be shown for ticket selectors
2963
+	 * when the number of datetimes for the event matches the value set for $datetime_selector_threshold
2964
+	 */
2965
+	const MAYBE_SHOW_DATETIME_SELECTOR = 'maybe_datetime_selector';
2966
+
2967
+	/**
2968
+	 * @var boolean $show_ticket_sale_columns
2969
+	 */
2970
+	public $show_ticket_sale_columns;
2971
+
2972
+	/**
2973
+	 * @var boolean $show_ticket_details
2974
+	 */
2975
+	public $show_ticket_details;
2976
+
2977
+	/**
2978
+	 * @var boolean $show_expired_tickets
2979
+	 */
2980
+	public $show_expired_tickets;
2981
+
2982
+	/**
2983
+	 * whether or not to display a dropdown box populated with event datetimes
2984
+	 * that toggles which tickets are displayed for a ticket selector.
2985
+	 * uses one of the *_DATETIME_SELECTOR constants defined above
2986
+	 *
2987
+	 * @var string $show_datetime_selector
2988
+	 */
2989
+	private $show_datetime_selector = 'no_datetime_selector';
2990
+
2991
+	/**
2992
+	 * the number of datetimes an event has to have before conditionally displaying a datetime selector
2993
+	 *
2994
+	 * @var int $datetime_selector_threshold
2995
+	 */
2996
+	private $datetime_selector_threshold = 3;
2997
+
2998
+	/**
2999
+	 * determines the maximum number of "checked" dates in the date and time filter
3000
+	 *
3001
+	 * @var int $datetime_selector_checked
3002
+	 */
3003
+	private $datetime_selector_max_checked = 10;
3004
+
3005
+
3006
+	/**
3007
+	 *    class constructor
3008
+	 */
3009
+	public function __construct()
3010
+	{
3011
+		$this->show_ticket_sale_columns = true;
3012
+		$this->show_ticket_details = true;
3013
+		$this->show_expired_tickets = true;
3014
+		$this->show_datetime_selector = \EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR;
3015
+		$this->datetime_selector_threshold = 3;
3016
+		$this->datetime_selector_max_checked = 10;
3017
+	}
3018
+
3019
+
3020
+	/**
3021
+	 * returns true if a datetime selector should be displayed
3022
+	 *
3023
+	 * @param array $datetimes
3024
+	 * @return bool
3025
+	 */
3026
+	public function showDatetimeSelector(array $datetimes)
3027
+	{
3028
+		// if the settings are NOT: don't show OR below threshold, THEN active = true
3029
+		return ! (
3030
+			$this->getShowDatetimeSelector() === \EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR
3031
+			|| (
3032
+				$this->getShowDatetimeSelector() === \EE_Ticket_Selector_Config::MAYBE_SHOW_DATETIME_SELECTOR
3033
+				&& count($datetimes) < $this->getDatetimeSelectorThreshold()
3034
+			)
3035
+		);
3036
+	}
3037
+
3038
+
3039
+	/**
3040
+	 * @return string
3041
+	 */
3042
+	public function getShowDatetimeSelector()
3043
+	{
3044
+		return $this->show_datetime_selector;
3045
+	}
3046
+
3047
+
3048
+	/**
3049
+	 * @param bool $keys_only
3050
+	 * @return array
3051
+	 */
3052
+	public function getShowDatetimeSelectorOptions($keys_only = true)
3053
+	{
3054
+		return $keys_only
3055
+			? array(
3056
+				\EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR,
3057
+				\EE_Ticket_Selector_Config::MAYBE_SHOW_DATETIME_SELECTOR,
3058
+			)
3059
+			: array(
3060
+				\EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR => esc_html__(
3061
+					'Do not show date & time filter',
3062
+					'event_espresso'
3063
+				),
3064
+				\EE_Ticket_Selector_Config::MAYBE_SHOW_DATETIME_SELECTOR  => esc_html__(
3065
+					'Maybe show date & time filter',
3066
+					'event_espresso'
3067
+				),
3068
+			);
3069
+	}
3070
+
3071
+
3072
+	/**
3073
+	 * @param string $show_datetime_selector
3074
+	 */
3075
+	public function setShowDatetimeSelector($show_datetime_selector)
3076
+	{
3077
+		$this->show_datetime_selector = in_array(
3078
+			$show_datetime_selector,
3079
+			$this->getShowDatetimeSelectorOptions(),
3080
+			true
3081
+		)
3082
+			? $show_datetime_selector
3083
+			: \EE_Ticket_Selector_Config::DO_NOT_SHOW_DATETIME_SELECTOR;
3084
+	}
3085
+
3086
+
3087
+	/**
3088
+	 * @return int
3089
+	 */
3090
+	public function getDatetimeSelectorThreshold()
3091
+	{
3092
+		return $this->datetime_selector_threshold;
3093
+	}
3094
+
3095
+
3096
+	/**
3097
+	 * @param int $datetime_selector_threshold
3098
+	 */
3099
+	public function setDatetimeSelectorThreshold($datetime_selector_threshold)
3100
+	{
3101
+		$datetime_selector_threshold = absint($datetime_selector_threshold);
3102
+		$this->datetime_selector_threshold = $datetime_selector_threshold ? $datetime_selector_threshold : 3;
3103
+	}
3104
+
3105
+
3106
+	/**
3107
+	 * @return int
3108
+	 */
3109
+	public function getDatetimeSelectorMaxChecked()
3110
+	{
3111
+		return $this->datetime_selector_max_checked;
3112
+	}
3113
+
3114
+
3115
+	/**
3116
+	 * @param int $datetime_selector_max_checked
3117
+	 */
3118
+	public function setDatetimeSelectorMaxChecked($datetime_selector_max_checked)
3119
+	{
3120
+		$this->datetime_selector_max_checked = absint($datetime_selector_max_checked);
3121
+	}
3122 3122
 }
3123 3123
 
3124 3124
 /**
@@ -3131,86 +3131,86 @@  discard block
 block discarded – undo
3131 3131
 class EE_Environment_Config extends EE_Config_Base
3132 3132
 {
3133 3133
 
3134
-    /**
3135
-     * Hold any php environment variables that we want to track.
3136
-     *
3137
-     * @var stdClass;
3138
-     */
3139
-    public $php;
3140
-
3141
-
3142
-    /**
3143
-     *    constructor
3144
-     */
3145
-    public function __construct()
3146
-    {
3147
-        $this->php = new stdClass();
3148
-        $this->_set_php_values();
3149
-    }
3150
-
3151
-
3152
-    /**
3153
-     * This sets the php environment variables.
3154
-     *
3155
-     * @since 4.4.0
3156
-     * @return void
3157
-     */
3158
-    protected function _set_php_values()
3159
-    {
3160
-        $this->php->max_input_vars = ini_get('max_input_vars');
3161
-        $this->php->version = phpversion();
3162
-    }
3163
-
3164
-
3165
-    /**
3166
-     * helper method for determining whether input_count is
3167
-     * reaching the potential maximum the server can handle
3168
-     * according to max_input_vars
3169
-     *
3170
-     * @param int   $input_count the count of input vars.
3171
-     * @return array {
3172
-     *                           An array that represents whether available space and if no available space the error
3173
-     *                           message.
3174
-     * @type bool   $has_space   whether more inputs can be added.
3175
-     * @type string $msg         Any message to be displayed.
3176
-     *                           }
3177
-     */
3178
-    public function max_input_vars_limit_check($input_count = 0)
3179
-    {
3180
-        if (! empty($this->php->max_input_vars)
3181
-            && ($input_count >= $this->php->max_input_vars)
3182
-        ) {
3183
-            // check the server setting because the config value could be stale
3184
-            $max_input_vars = ini_get('max_input_vars');
3185
-            if ($input_count >= $max_input_vars) {
3186
-                return sprintf(
3187
-                    esc_html__(
3188
-                        'The maximum number of inputs on this page has been exceeded. You cannot make edits to this page because of your server\'s PHP "max_input_vars" setting.%1$sThere are %2$d inputs and the maximum amount currently allowed by your server is %3$d.%1$sPlease contact your web host and ask them to raise the "max_input_vars" limit.',
3189
-                        'event_espresso'
3190
-                    ),
3191
-                    '<br>',
3192
-                    $input_count,
3193
-                    $max_input_vars
3194
-                );
3195
-            } else {
3196
-                return '';
3197
-            }
3198
-        } else {
3199
-            return '';
3200
-        }
3201
-    }
3202
-
3203
-
3204
-    /**
3205
-     * The purpose of this method is just to force rechecking php values so if they've changed, they get updated.
3206
-     *
3207
-     * @since 4.4.1
3208
-     * @return void
3209
-     */
3210
-    public function recheck_values()
3211
-    {
3212
-        $this->_set_php_values();
3213
-    }
3134
+	/**
3135
+	 * Hold any php environment variables that we want to track.
3136
+	 *
3137
+	 * @var stdClass;
3138
+	 */
3139
+	public $php;
3140
+
3141
+
3142
+	/**
3143
+	 *    constructor
3144
+	 */
3145
+	public function __construct()
3146
+	{
3147
+		$this->php = new stdClass();
3148
+		$this->_set_php_values();
3149
+	}
3150
+
3151
+
3152
+	/**
3153
+	 * This sets the php environment variables.
3154
+	 *
3155
+	 * @since 4.4.0
3156
+	 * @return void
3157
+	 */
3158
+	protected function _set_php_values()
3159
+	{
3160
+		$this->php->max_input_vars = ini_get('max_input_vars');
3161
+		$this->php->version = phpversion();
3162
+	}
3163
+
3164
+
3165
+	/**
3166
+	 * helper method for determining whether input_count is
3167
+	 * reaching the potential maximum the server can handle
3168
+	 * according to max_input_vars
3169
+	 *
3170
+	 * @param int   $input_count the count of input vars.
3171
+	 * @return array {
3172
+	 *                           An array that represents whether available space and if no available space the error
3173
+	 *                           message.
3174
+	 * @type bool   $has_space   whether more inputs can be added.
3175
+	 * @type string $msg         Any message to be displayed.
3176
+	 *                           }
3177
+	 */
3178
+	public function max_input_vars_limit_check($input_count = 0)
3179
+	{
3180
+		if (! empty($this->php->max_input_vars)
3181
+			&& ($input_count >= $this->php->max_input_vars)
3182
+		) {
3183
+			// check the server setting because the config value could be stale
3184
+			$max_input_vars = ini_get('max_input_vars');
3185
+			if ($input_count >= $max_input_vars) {
3186
+				return sprintf(
3187
+					esc_html__(
3188
+						'The maximum number of inputs on this page has been exceeded. You cannot make edits to this page because of your server\'s PHP "max_input_vars" setting.%1$sThere are %2$d inputs and the maximum amount currently allowed by your server is %3$d.%1$sPlease contact your web host and ask them to raise the "max_input_vars" limit.',
3189
+						'event_espresso'
3190
+					),
3191
+					'<br>',
3192
+					$input_count,
3193
+					$max_input_vars
3194
+				);
3195
+			} else {
3196
+				return '';
3197
+			}
3198
+		} else {
3199
+			return '';
3200
+		}
3201
+	}
3202
+
3203
+
3204
+	/**
3205
+	 * The purpose of this method is just to force rechecking php values so if they've changed, they get updated.
3206
+	 *
3207
+	 * @since 4.4.1
3208
+	 * @return void
3209
+	 */
3210
+	public function recheck_values()
3211
+	{
3212
+		$this->_set_php_values();
3213
+	}
3214 3214
 }
3215 3215
 
3216 3216
 /**
@@ -3223,21 +3223,21 @@  discard block
 block discarded – undo
3223 3223
 class EE_Tax_Config extends EE_Config_Base
3224 3224
 {
3225 3225
 
3226
-    /*
3226
+	/*
3227 3227
      * flag to indicate whether or not to display ticket prices with the taxes included
3228 3228
      *
3229 3229
      * @var boolean $prices_displayed_including_taxes
3230 3230
      */
3231
-    public $prices_displayed_including_taxes;
3231
+	public $prices_displayed_including_taxes;
3232 3232
 
3233 3233
 
3234
-    /**
3235
-     *    class constructor
3236
-     */
3237
-    public function __construct()
3238
-    {
3239
-        $this->prices_displayed_including_taxes = true;
3240
-    }
3234
+	/**
3235
+	 *    class constructor
3236
+	 */
3237
+	public function __construct()
3238
+	{
3239
+		$this->prices_displayed_including_taxes = true;
3240
+	}
3241 3241
 }
3242 3242
 
3243 3243
 /**
@@ -3251,19 +3251,19 @@  discard block
 block discarded – undo
3251 3251
 class EE_Messages_Config extends EE_Config_Base
3252 3252
 {
3253 3253
 
3254
-    /**
3255
-     * This is an integer representing the deletion threshold in months for when old messages will get deleted.
3256
-     * A value of 0 represents never deleting.  Default is 0.
3257
-     *
3258
-     * @var integer
3259
-     */
3260
-    public $delete_threshold;
3254
+	/**
3255
+	 * This is an integer representing the deletion threshold in months for when old messages will get deleted.
3256
+	 * A value of 0 represents never deleting.  Default is 0.
3257
+	 *
3258
+	 * @var integer
3259
+	 */
3260
+	public $delete_threshold;
3261 3261
 
3262 3262
 
3263
-    public function __construct()
3264
-    {
3265
-        $this->delete_threshold = 0;
3266
-    }
3263
+	public function __construct()
3264
+	{
3265
+		$this->delete_threshold = 0;
3266
+	}
3267 3267
 }
3268 3268
 
3269 3269
 /**
@@ -3274,31 +3274,31 @@  discard block
 block discarded – undo
3274 3274
 class EE_Gateway_Config extends EE_Config_Base
3275 3275
 {
3276 3276
 
3277
-    /**
3278
-     * Array with keys that are payment gateways slugs, and values are arrays
3279
-     * with any config info the gateway wants to store
3280
-     *
3281
-     * @var array
3282
-     */
3283
-    public $payment_settings;
3284
-
3285
-    /**
3286
-     * Where keys are gateway slugs, and values are booleans indicating whether or not
3287
-     * the gateway is stored in the uploads directory
3288
-     *
3289
-     * @var array
3290
-     */
3291
-    public $active_gateways;
3292
-
3293
-
3294
-    /**
3295
-     *    class constructor
3296
-     *
3297
-     * @deprecated
3298
-     */
3299
-    public function __construct()
3300
-    {
3301
-        $this->payment_settings = array();
3302
-        $this->active_gateways = array('Invoice' => false);
3303
-    }
3277
+	/**
3278
+	 * Array with keys that are payment gateways slugs, and values are arrays
3279
+	 * with any config info the gateway wants to store
3280
+	 *
3281
+	 * @var array
3282
+	 */
3283
+	public $payment_settings;
3284
+
3285
+	/**
3286
+	 * Where keys are gateway slugs, and values are booleans indicating whether or not
3287
+	 * the gateway is stored in the uploads directory
3288
+	 *
3289
+	 * @var array
3290
+	 */
3291
+	public $active_gateways;
3292
+
3293
+
3294
+	/**
3295
+	 *    class constructor
3296
+	 *
3297
+	 * @deprecated
3298
+	 */
3299
+	public function __construct()
3300
+	{
3301
+		$this->payment_settings = array();
3302
+		$this->active_gateways = array('Invoice' => false);
3303
+	}
3304 3304
 }
Please login to merge, or discard this patch.