Completed
Branch BUG/11405/ticket-reservations (ec55ed)
by
unknown
25:21 queued 12:44
created
strategies/validation/EE_Many_Valued_Validation_Strategy.strategy.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -45,8 +45,8 @@
 block discarded – undo
45 45
 				}
46 46
 			}
47 47
 		}
48
-        return true;
49
-    }
48
+		return true;
49
+	}
50 50
 
51 51
 
52 52
 
Please login to merge, or discard this patch.
core/libraries/form_sections/form_handlers/FormHandler.php 1 patch
Indentation   +643 added lines, -643 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
 use EventEspresso\core\exceptions\InvalidFormSubmissionException;
16 16
 
17 17
 if (! defined('EVENT_ESPRESSO_VERSION')) {
18
-    exit('No direct script access allowed');
18
+	exit('No direct script access allowed');
19 19
 }
20 20
 
21 21
 
@@ -34,648 +34,648 @@  discard block
 block discarded – undo
34 34
 abstract class FormHandler implements FormHandlerInterface
35 35
 {
36 36
 
37
-    /**
38
-     * will add opening and closing HTML form tags as well as a submit button
39
-     */
40
-    const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit';
41
-
42
-    /**
43
-     * will add opening and closing HTML form tags but NOT a submit button
44
-     */
45
-    const ADD_FORM_TAGS_ONLY = 'add_form_tags_only';
46
-
47
-    /**
48
-     * will NOT add opening and closing HTML form tags but will add a submit button
49
-     */
50
-    const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only';
51
-
52
-    /**
53
-     * will NOT add opening and closing HTML form tags NOR a submit button
54
-     */
55
-    const DO_NOT_SETUP_FORM = 'do_not_setup_form';
56
-
57
-    /**
58
-     * if set to false, then this form has no displayable content,
59
-     * and will only be used for processing data sent passed via GET or POST
60
-     * defaults to true ( ie: form has displayable content )
61
-     *
62
-     * @var boolean $displayable
63
-     */
64
-    private $displayable = true;
65
-
66
-    /**
67
-     * @var string $form_name
68
-     */
69
-    private $form_name;
70
-
71
-    /**
72
-     * @var string $admin_name
73
-     */
74
-    private $admin_name;
75
-
76
-    /**
77
-     * @var string $slug
78
-     */
79
-    private $slug;
80
-
81
-    /**
82
-     * @var string $submit_btn_text
83
-     */
84
-    private $submit_btn_text;
85
-
86
-    /**
87
-     * @var string $form_action
88
-     */
89
-    private $form_action;
90
-
91
-    /**
92
-     * form params in key value pairs
93
-     * can be added to form action URL or as hidden inputs
94
-     *
95
-     * @var array $form_args
96
-     */
97
-    private $form_args = array();
98
-
99
-    /**
100
-     * value of one of the string constant above
101
-     *
102
-     * @var string $form_config
103
-     */
104
-    private $form_config;
105
-
106
-    /**
107
-     * whether or not the form was determined to be invalid
108
-     *
109
-     * @var boolean $form_has_errors
110
-     */
111
-    private $form_has_errors;
112
-
113
-    /**
114
-     * the absolute top level form section being used on the page
115
-     *
116
-     * @var EE_Form_Section_Proper $form
117
-     */
118
-    private $form;
119
-
120
-    /**
121
-     * @var EE_Registry $registry
122
-     */
123
-    protected $registry;
124
-
125
-
126
-
127
-    /**
128
-     * Form constructor.
129
-     *
130
-     * @param string      $form_name
131
-     * @param string      $admin_name
132
-     * @param string      $slug
133
-     * @param string      $form_action
134
-     * @param string      $form_config
135
-     * @param EE_Registry $registry
136
-     * @throws InvalidDataTypeException
137
-     * @throws DomainException
138
-     * @throws InvalidArgumentException
139
-     */
140
-    public function __construct(
141
-        $form_name,
142
-        $admin_name,
143
-        $slug,
144
-        $form_action = '',
145
-        $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
146
-        EE_Registry $registry
147
-    ) {
148
-        $this->setFormName($form_name);
149
-        $this->setAdminName($admin_name);
150
-        $this->setSlug($slug);
151
-        $this->setFormAction($form_action);
152
-        $this->setFormConfig($form_config);
153
-        $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso'));
154
-        $this->registry = $registry;
155
-    }
156
-
157
-
158
-
159
-    /**
160
-     * @return array
161
-     */
162
-    public static function getFormConfigConstants()
163
-    {
164
-        return array(
165
-            FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
166
-            FormHandler::ADD_FORM_TAGS_ONLY,
167
-            FormHandler::ADD_FORM_SUBMIT_ONLY,
168
-            FormHandler::DO_NOT_SETUP_FORM,
169
-        );
170
-    }
171
-
172
-
173
-
174
-    /**
175
-     * @param bool $for_display
176
-     * @return EE_Form_Section_Proper
177
-     * @throws EE_Error
178
-     * @throws LogicException
179
-     */
180
-    public function form($for_display = false)
181
-    {
182
-        if (! $this->formIsValid()) {
183
-            return null;
184
-        }
185
-        if ($for_display) {
186
-            $form_config = $this->formConfig();
187
-            if (
188
-                $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
189
-                || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY
190
-            ) {
191
-                $this->appendSubmitButton();
192
-                $this->clearFormButtonFloats();
193
-            }
194
-        }
195
-        return $this->form;
196
-    }
197
-
198
-
199
-
200
-    /**
201
-     * @return boolean
202
-     * @throws LogicException
203
-     */
204
-    public function formIsValid()
205
-    {
206
-        if ($this->form instanceof EE_Form_Section_Proper) {
207
-            return true;
208
-        }
209
-        $form = apply_filters(
210
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object',
211
-            $this->generate(),
212
-            $this
213
-        );
214
-        if ($this->verifyForm($form)) {
215
-            $this->setForm($form);
216
-        }
217
-        return true;
218
-    }
219
-
220
-
221
-
222
-    /**
223
-     * @param EE_Form_Section_Proper|null $form
224
-     * @return bool
225
-     * @throws LogicException
226
-     */
227
-    public function verifyForm(EE_Form_Section_Proper $form = null)
228
-    {
229
-        $form = $form !== null ? $form : $this->form;
230
-        if ($form instanceof EE_Form_Section_Proper) {
231
-            return true;
232
-        }
233
-        throw new LogicException(
234
-            sprintf(
235
-                esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'),
236
-                $this->form_name,
237
-                var_export($form, true)
238
-            )
239
-        );
240
-    }
241
-
242
-
243
-
244
-    /**
245
-     * @param EE_Form_Section_Proper $form
246
-     */
247
-    public function setForm(EE_Form_Section_Proper $form)
248
-    {
249
-        $this->form = $form;
250
-    }
251
-
252
-
253
-
254
-    /**
255
-     * @return boolean
256
-     */
257
-    public function displayable()
258
-    {
259
-        return $this->displayable;
260
-    }
261
-
262
-
263
-
264
-    /**
265
-     * @param boolean $displayable
266
-     */
267
-    public function setDisplayable($displayable = false)
268
-    {
269
-        $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN);
270
-    }
271
-
272
-
273
-
274
-    /**
275
-     * a public name for the form that can be displayed on the frontend of a site
276
-     *
277
-     * @return string
278
-     */
279
-    public function formName()
280
-    {
281
-        return $this->form_name;
282
-    }
283
-
284
-
285
-
286
-    /**
287
-     * @param string $form_name
288
-     * @throws InvalidDataTypeException
289
-     */
290
-    public function setFormName($form_name)
291
-    {
292
-        if (! is_string($form_name)) {
293
-            throw new InvalidDataTypeException('$form_name', $form_name, 'string');
294
-        }
295
-        $this->form_name = $form_name;
296
-    }
297
-
298
-
299
-
300
-    /**
301
-     * a public name for the form that can be displayed, but only in the admin
302
-     *
303
-     * @return string
304
-     */
305
-    public function adminName()
306
-    {
307
-        return $this->admin_name;
308
-    }
309
-
310
-
311
-
312
-    /**
313
-     * @param string $admin_name
314
-     * @throws InvalidDataTypeException
315
-     */
316
-    public function setAdminName($admin_name)
317
-    {
318
-        if (! is_string($admin_name)) {
319
-            throw new InvalidDataTypeException('$admin_name', $admin_name, 'string');
320
-        }
321
-        $this->admin_name = $admin_name;
322
-    }
323
-
324
-
325
-
326
-    /**
327
-     * a URL friendly string that can be used for identifying the form
328
-     *
329
-     * @return string
330
-     */
331
-    public function slug()
332
-    {
333
-        return $this->slug;
334
-    }
335
-
336
-
337
-
338
-    /**
339
-     * @param string $slug
340
-     * @throws InvalidDataTypeException
341
-     */
342
-    public function setSlug($slug)
343
-    {
344
-        if (! is_string($slug)) {
345
-            throw new InvalidDataTypeException('$slug', $slug, 'string');
346
-        }
347
-        $this->slug = $slug;
348
-    }
349
-
350
-
351
-
352
-    /**
353
-     * @return string
354
-     */
355
-    public function submitBtnText()
356
-    {
357
-        return $this->submit_btn_text;
358
-    }
359
-
360
-
361
-
362
-    /**
363
-     * @param string $submit_btn_text
364
-     * @throws InvalidDataTypeException
365
-     * @throws InvalidArgumentException
366
-     */
367
-    public function setSubmitBtnText($submit_btn_text)
368
-    {
369
-        if (! is_string($submit_btn_text)) {
370
-            throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string');
371
-        }
372
-        if (empty($submit_btn_text)) {
373
-            throw new InvalidArgumentException(
374
-                esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso')
375
-            );
376
-        }
377
-        $this->submit_btn_text = $submit_btn_text;
378
-    }
379
-
380
-
381
-
382
-    /**
383
-     * @return string
384
-     */
385
-    public function formAction()
386
-    {
387
-        return ! empty($this->form_args)
388
-            ? add_query_arg($this->form_args, $this->form_action)
389
-            : $this->form_action;
390
-    }
391
-
392
-
393
-
394
-    /**
395
-     * @param string $form_action
396
-     * @throws InvalidDataTypeException
397
-     */
398
-    public function setFormAction($form_action)
399
-    {
400
-        if (! is_string($form_action)) {
401
-            throw new InvalidDataTypeException('$form_action', $form_action, 'string');
402
-        }
403
-        $this->form_action = $form_action;
404
-    }
405
-
406
-
407
-
408
-    /**
409
-     * @param array $form_args
410
-     * @throws InvalidDataTypeException
411
-     * @throws InvalidArgumentException
412
-     */
413
-    public function addFormActionArgs($form_args = array())
414
-    {
415
-        if (is_object($form_args)) {
416
-            throw new InvalidDataTypeException(
417
-                '$form_args',
418
-                $form_args,
419
-                'anything other than an object was expected.'
420
-            );
421
-        }
422
-        if (empty($form_args)) {
423
-            throw new InvalidArgumentException(
424
-                esc_html__('The redirect arguments can not be an empty array.', 'event_espresso')
425
-            );
426
-        }
427
-        $this->form_args = array_merge($this->form_args, $form_args);
428
-    }
429
-
430
-
431
-
432
-    /**
433
-     * @return string
434
-     */
435
-    public function formConfig()
436
-    {
437
-        return $this->form_config;
438
-    }
439
-
440
-
441
-
442
-    /**
443
-     * @param string $form_config
444
-     * @throws DomainException
445
-     */
446
-    public function setFormConfig($form_config)
447
-    {
448
-        if (
449
-        ! in_array(
450
-            $form_config,
451
-            array(
452
-                FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
453
-                FormHandler::ADD_FORM_TAGS_ONLY,
454
-                FormHandler::ADD_FORM_SUBMIT_ONLY,
455
-                FormHandler::DO_NOT_SETUP_FORM,
456
-            ),
457
-            true
458
-        )
459
-        ) {
460
-            throw new DomainException(
461
-                sprintf(
462
-                    esc_html__('"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form',
463
-                        'event_espresso'),
464
-                    $form_config
465
-                )
466
-            );
467
-        }
468
-        $this->form_config = $form_config;
469
-    }
470
-
471
-
472
-
473
-    /**
474
-     * called after the form is instantiated
475
-     * and used for performing any logic that needs to occur early
476
-     * before any of the other methods are called.
477
-     * returns true if everything is ok to proceed,
478
-     * and false if no further form logic should be implemented
479
-     *
480
-     * @return boolean
481
-     */
482
-    public function initialize()
483
-    {
484
-        $this->form_has_errors = EE_Error::has_error(true);
485
-        return true;
486
-    }
487
-
488
-
489
-
490
-    /**
491
-     * used for setting up css and js
492
-     *
493
-     * @return void
494
-     * @throws LogicException
495
-     * @throws EE_Error
496
-     */
497
-    public function enqueueStylesAndScripts()
498
-    {
499
-        $this->form()->enqueue_js();
500
-    }
501
-
502
-
503
-
504
-    /**
505
-     * creates and returns the actual form
506
-     *
507
-     * @return EE_Form_Section_Proper
508
-     */
509
-    abstract public function generate();
510
-
511
-
512
-
513
-    /**
514
-     * creates and returns an EE_Submit_Input labeled "Submit"
515
-     *
516
-     * @param string $text
517
-     * @return EE_Submit_Input
518
-     */
519
-    public function generateSubmitButton($text = '')
520
-    {
521
-        $text = ! empty($text) ? $text : $this->submitBtnText();
522
-        return new EE_Submit_Input(
523
-            array(
524
-                'html_name'             => 'ee-form-submit-' . $this->slug(),
525
-                'html_id'               => 'ee-form-submit-' . $this->slug(),
526
-                'html_class'            => 'ee-form-submit',
527
-                'html_label'            => ' ',
528
-                'other_html_attributes' => ' rel="' . $this->slug() . '"',
529
-                'default'               => $text,
530
-            )
531
-        );
532
-    }
533
-
534
-
535
-
536
-    /**
537
-     * calls generateSubmitButton() and appends it onto the form along with a float clearing div
538
-     *
539
-     * @param string $text
540
-     * @return void
541
-     * @throws EE_Error
542
-     */
543
-    public function appendSubmitButton($text = '')
544
-    {
545
-        if ($this->form->subsection_exists($this->slug() . '-submit-btn')) {
546
-            return;
547
-        }
548
-        $this->form->add_subsections(
549
-            array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)),
550
-            null,
551
-            false
552
-        );
553
-    }
554
-
555
-
556
-
557
-    /**
558
-     * creates and returns an EE_Submit_Input labeled "Cancel"
559
-     *
560
-     * @param string $text
561
-     * @return EE_Submit_Input
562
-     */
563
-    public function generateCancelButton($text = '')
564
-    {
565
-        $cancel_button = new EE_Submit_Input(
566
-            array(
567
-                'html_name'             => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!!
568
-                'html_id'               => 'ee-cancel-form-' . $this->slug(),
569
-                'html_class'            => 'ee-cancel-form',
570
-                'html_label'            => ' ',
571
-                'other_html_attributes' => ' rel="' . $this->slug() . '"',
572
-                'default'               => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'),
573
-            )
574
-        );
575
-        $cancel_button->set_button_css_attributes(false);
576
-        return $cancel_button;
577
-    }
578
-
579
-
580
-
581
-    /**
582
-     * appends a float clearing div onto end of form
583
-     *
584
-     * @return void
585
-     * @throws EE_Error
586
-     */
587
-    public function clearFormButtonFloats()
588
-    {
589
-        $this->form->add_subsections(
590
-            array(
591
-                'clear-submit-btn-float' => new EE_Form_Section_HTML(
592
-                    EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx()
593
-                ),
594
-            ),
595
-            null,
596
-            false
597
-        );
598
-    }
599
-
600
-
601
-    /**
602
-     * takes the generated form and displays it along with ony other non-form HTML that may be required
603
-     * returns a string of HTML that can be directly echoed in a template
604
-     *
605
-     * @return string
606
-     * @throws \InvalidArgumentException
607
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
608
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
609
-     * @throws LogicException
610
-     * @throws EE_Error
611
-     */
612
-    public function display()
613
-    {
614
-        $form_html = apply_filters(
615
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form',
616
-            ''
617
-        );
618
-        $form_config = $this->formConfig();
619
-        if (
620
-            $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
621
-            || $form_config === FormHandler::ADD_FORM_TAGS_ONLY
622
-        ) {
623
-            $form_html .= $this->form()->form_open($this->formAction());
624
-        }
625
-        $form_html .= $this->form(true)->get_html($this->form_has_errors);
626
-        if (
627
-            $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
628
-            || $form_config === FormHandler::ADD_FORM_TAGS_ONLY
629
-        ) {
630
-            $form_html .= $this->form()->form_close();
631
-        }
632
-        $form_html .= apply_filters(
633
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form',
634
-            ''
635
-        );
636
-        return $form_html;
637
-    }
638
-
639
-
640
-    /**
641
-     * handles processing the form submission
642
-     * returns true or false depending on whether the form was processed successfully or not
643
-     *
644
-     * @param array $submitted_form_data
645
-     * @return array
646
-     * @throws \InvalidArgumentException
647
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
648
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
649
-     * @throws EE_Error
650
-     * @throws LogicException
651
-     * @throws InvalidFormSubmissionException
652
-     */
653
-    public function process($submitted_form_data = array())
654
-    {
655
-        if (! $this->form()->was_submitted($submitted_form_data)) {
656
-            throw new InvalidFormSubmissionException($this->form_name);
657
-        }
658
-        $this->form(true)->receive_form_submission($submitted_form_data);
659
-        if (! $this->form()->is_valid()) {
660
-            throw new InvalidFormSubmissionException(
661
-                $this->form_name,
662
-                sprintf(
663
-                    esc_html__(
664
-                        'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s',
665
-                        'event_espresso'
666
-                    ),
667
-                    $this->form_name,
668
-                    '<br />',
669
-                    implode('<br />', $this->form()->get_validation_errors_accumulated())
670
-                )
671
-            );
672
-        }
673
-        return apply_filters(
674
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data',
675
-            $this->form()->valid_data(),
676
-            $this
677
-        );
678
-    }
37
+	/**
38
+	 * will add opening and closing HTML form tags as well as a submit button
39
+	 */
40
+	const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit';
41
+
42
+	/**
43
+	 * will add opening and closing HTML form tags but NOT a submit button
44
+	 */
45
+	const ADD_FORM_TAGS_ONLY = 'add_form_tags_only';
46
+
47
+	/**
48
+	 * will NOT add opening and closing HTML form tags but will add a submit button
49
+	 */
50
+	const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only';
51
+
52
+	/**
53
+	 * will NOT add opening and closing HTML form tags NOR a submit button
54
+	 */
55
+	const DO_NOT_SETUP_FORM = 'do_not_setup_form';
56
+
57
+	/**
58
+	 * if set to false, then this form has no displayable content,
59
+	 * and will only be used for processing data sent passed via GET or POST
60
+	 * defaults to true ( ie: form has displayable content )
61
+	 *
62
+	 * @var boolean $displayable
63
+	 */
64
+	private $displayable = true;
65
+
66
+	/**
67
+	 * @var string $form_name
68
+	 */
69
+	private $form_name;
70
+
71
+	/**
72
+	 * @var string $admin_name
73
+	 */
74
+	private $admin_name;
75
+
76
+	/**
77
+	 * @var string $slug
78
+	 */
79
+	private $slug;
80
+
81
+	/**
82
+	 * @var string $submit_btn_text
83
+	 */
84
+	private $submit_btn_text;
85
+
86
+	/**
87
+	 * @var string $form_action
88
+	 */
89
+	private $form_action;
90
+
91
+	/**
92
+	 * form params in key value pairs
93
+	 * can be added to form action URL or as hidden inputs
94
+	 *
95
+	 * @var array $form_args
96
+	 */
97
+	private $form_args = array();
98
+
99
+	/**
100
+	 * value of one of the string constant above
101
+	 *
102
+	 * @var string $form_config
103
+	 */
104
+	private $form_config;
105
+
106
+	/**
107
+	 * whether or not the form was determined to be invalid
108
+	 *
109
+	 * @var boolean $form_has_errors
110
+	 */
111
+	private $form_has_errors;
112
+
113
+	/**
114
+	 * the absolute top level form section being used on the page
115
+	 *
116
+	 * @var EE_Form_Section_Proper $form
117
+	 */
118
+	private $form;
119
+
120
+	/**
121
+	 * @var EE_Registry $registry
122
+	 */
123
+	protected $registry;
124
+
125
+
126
+
127
+	/**
128
+	 * Form constructor.
129
+	 *
130
+	 * @param string      $form_name
131
+	 * @param string      $admin_name
132
+	 * @param string      $slug
133
+	 * @param string      $form_action
134
+	 * @param string      $form_config
135
+	 * @param EE_Registry $registry
136
+	 * @throws InvalidDataTypeException
137
+	 * @throws DomainException
138
+	 * @throws InvalidArgumentException
139
+	 */
140
+	public function __construct(
141
+		$form_name,
142
+		$admin_name,
143
+		$slug,
144
+		$form_action = '',
145
+		$form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
146
+		EE_Registry $registry
147
+	) {
148
+		$this->setFormName($form_name);
149
+		$this->setAdminName($admin_name);
150
+		$this->setSlug($slug);
151
+		$this->setFormAction($form_action);
152
+		$this->setFormConfig($form_config);
153
+		$this->setSubmitBtnText(esc_html__('Submit', 'event_espresso'));
154
+		$this->registry = $registry;
155
+	}
156
+
157
+
158
+
159
+	/**
160
+	 * @return array
161
+	 */
162
+	public static function getFormConfigConstants()
163
+	{
164
+		return array(
165
+			FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
166
+			FormHandler::ADD_FORM_TAGS_ONLY,
167
+			FormHandler::ADD_FORM_SUBMIT_ONLY,
168
+			FormHandler::DO_NOT_SETUP_FORM,
169
+		);
170
+	}
171
+
172
+
173
+
174
+	/**
175
+	 * @param bool $for_display
176
+	 * @return EE_Form_Section_Proper
177
+	 * @throws EE_Error
178
+	 * @throws LogicException
179
+	 */
180
+	public function form($for_display = false)
181
+	{
182
+		if (! $this->formIsValid()) {
183
+			return null;
184
+		}
185
+		if ($for_display) {
186
+			$form_config = $this->formConfig();
187
+			if (
188
+				$form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
189
+				|| $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY
190
+			) {
191
+				$this->appendSubmitButton();
192
+				$this->clearFormButtonFloats();
193
+			}
194
+		}
195
+		return $this->form;
196
+	}
197
+
198
+
199
+
200
+	/**
201
+	 * @return boolean
202
+	 * @throws LogicException
203
+	 */
204
+	public function formIsValid()
205
+	{
206
+		if ($this->form instanceof EE_Form_Section_Proper) {
207
+			return true;
208
+		}
209
+		$form = apply_filters(
210
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object',
211
+			$this->generate(),
212
+			$this
213
+		);
214
+		if ($this->verifyForm($form)) {
215
+			$this->setForm($form);
216
+		}
217
+		return true;
218
+	}
219
+
220
+
221
+
222
+	/**
223
+	 * @param EE_Form_Section_Proper|null $form
224
+	 * @return bool
225
+	 * @throws LogicException
226
+	 */
227
+	public function verifyForm(EE_Form_Section_Proper $form = null)
228
+	{
229
+		$form = $form !== null ? $form : $this->form;
230
+		if ($form instanceof EE_Form_Section_Proper) {
231
+			return true;
232
+		}
233
+		throw new LogicException(
234
+			sprintf(
235
+				esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'),
236
+				$this->form_name,
237
+				var_export($form, true)
238
+			)
239
+		);
240
+	}
241
+
242
+
243
+
244
+	/**
245
+	 * @param EE_Form_Section_Proper $form
246
+	 */
247
+	public function setForm(EE_Form_Section_Proper $form)
248
+	{
249
+		$this->form = $form;
250
+	}
251
+
252
+
253
+
254
+	/**
255
+	 * @return boolean
256
+	 */
257
+	public function displayable()
258
+	{
259
+		return $this->displayable;
260
+	}
261
+
262
+
263
+
264
+	/**
265
+	 * @param boolean $displayable
266
+	 */
267
+	public function setDisplayable($displayable = false)
268
+	{
269
+		$this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN);
270
+	}
271
+
272
+
273
+
274
+	/**
275
+	 * a public name for the form that can be displayed on the frontend of a site
276
+	 *
277
+	 * @return string
278
+	 */
279
+	public function formName()
280
+	{
281
+		return $this->form_name;
282
+	}
283
+
284
+
285
+
286
+	/**
287
+	 * @param string $form_name
288
+	 * @throws InvalidDataTypeException
289
+	 */
290
+	public function setFormName($form_name)
291
+	{
292
+		if (! is_string($form_name)) {
293
+			throw new InvalidDataTypeException('$form_name', $form_name, 'string');
294
+		}
295
+		$this->form_name = $form_name;
296
+	}
297
+
298
+
299
+
300
+	/**
301
+	 * a public name for the form that can be displayed, but only in the admin
302
+	 *
303
+	 * @return string
304
+	 */
305
+	public function adminName()
306
+	{
307
+		return $this->admin_name;
308
+	}
309
+
310
+
311
+
312
+	/**
313
+	 * @param string $admin_name
314
+	 * @throws InvalidDataTypeException
315
+	 */
316
+	public function setAdminName($admin_name)
317
+	{
318
+		if (! is_string($admin_name)) {
319
+			throw new InvalidDataTypeException('$admin_name', $admin_name, 'string');
320
+		}
321
+		$this->admin_name = $admin_name;
322
+	}
323
+
324
+
325
+
326
+	/**
327
+	 * a URL friendly string that can be used for identifying the form
328
+	 *
329
+	 * @return string
330
+	 */
331
+	public function slug()
332
+	{
333
+		return $this->slug;
334
+	}
335
+
336
+
337
+
338
+	/**
339
+	 * @param string $slug
340
+	 * @throws InvalidDataTypeException
341
+	 */
342
+	public function setSlug($slug)
343
+	{
344
+		if (! is_string($slug)) {
345
+			throw new InvalidDataTypeException('$slug', $slug, 'string');
346
+		}
347
+		$this->slug = $slug;
348
+	}
349
+
350
+
351
+
352
+	/**
353
+	 * @return string
354
+	 */
355
+	public function submitBtnText()
356
+	{
357
+		return $this->submit_btn_text;
358
+	}
359
+
360
+
361
+
362
+	/**
363
+	 * @param string $submit_btn_text
364
+	 * @throws InvalidDataTypeException
365
+	 * @throws InvalidArgumentException
366
+	 */
367
+	public function setSubmitBtnText($submit_btn_text)
368
+	{
369
+		if (! is_string($submit_btn_text)) {
370
+			throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string');
371
+		}
372
+		if (empty($submit_btn_text)) {
373
+			throw new InvalidArgumentException(
374
+				esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso')
375
+			);
376
+		}
377
+		$this->submit_btn_text = $submit_btn_text;
378
+	}
379
+
380
+
381
+
382
+	/**
383
+	 * @return string
384
+	 */
385
+	public function formAction()
386
+	{
387
+		return ! empty($this->form_args)
388
+			? add_query_arg($this->form_args, $this->form_action)
389
+			: $this->form_action;
390
+	}
391
+
392
+
393
+
394
+	/**
395
+	 * @param string $form_action
396
+	 * @throws InvalidDataTypeException
397
+	 */
398
+	public function setFormAction($form_action)
399
+	{
400
+		if (! is_string($form_action)) {
401
+			throw new InvalidDataTypeException('$form_action', $form_action, 'string');
402
+		}
403
+		$this->form_action = $form_action;
404
+	}
405
+
406
+
407
+
408
+	/**
409
+	 * @param array $form_args
410
+	 * @throws InvalidDataTypeException
411
+	 * @throws InvalidArgumentException
412
+	 */
413
+	public function addFormActionArgs($form_args = array())
414
+	{
415
+		if (is_object($form_args)) {
416
+			throw new InvalidDataTypeException(
417
+				'$form_args',
418
+				$form_args,
419
+				'anything other than an object was expected.'
420
+			);
421
+		}
422
+		if (empty($form_args)) {
423
+			throw new InvalidArgumentException(
424
+				esc_html__('The redirect arguments can not be an empty array.', 'event_espresso')
425
+			);
426
+		}
427
+		$this->form_args = array_merge($this->form_args, $form_args);
428
+	}
429
+
430
+
431
+
432
+	/**
433
+	 * @return string
434
+	 */
435
+	public function formConfig()
436
+	{
437
+		return $this->form_config;
438
+	}
439
+
440
+
441
+
442
+	/**
443
+	 * @param string $form_config
444
+	 * @throws DomainException
445
+	 */
446
+	public function setFormConfig($form_config)
447
+	{
448
+		if (
449
+		! in_array(
450
+			$form_config,
451
+			array(
452
+				FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
453
+				FormHandler::ADD_FORM_TAGS_ONLY,
454
+				FormHandler::ADD_FORM_SUBMIT_ONLY,
455
+				FormHandler::DO_NOT_SETUP_FORM,
456
+			),
457
+			true
458
+		)
459
+		) {
460
+			throw new DomainException(
461
+				sprintf(
462
+					esc_html__('"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form',
463
+						'event_espresso'),
464
+					$form_config
465
+				)
466
+			);
467
+		}
468
+		$this->form_config = $form_config;
469
+	}
470
+
471
+
472
+
473
+	/**
474
+	 * called after the form is instantiated
475
+	 * and used for performing any logic that needs to occur early
476
+	 * before any of the other methods are called.
477
+	 * returns true if everything is ok to proceed,
478
+	 * and false if no further form logic should be implemented
479
+	 *
480
+	 * @return boolean
481
+	 */
482
+	public function initialize()
483
+	{
484
+		$this->form_has_errors = EE_Error::has_error(true);
485
+		return true;
486
+	}
487
+
488
+
489
+
490
+	/**
491
+	 * used for setting up css and js
492
+	 *
493
+	 * @return void
494
+	 * @throws LogicException
495
+	 * @throws EE_Error
496
+	 */
497
+	public function enqueueStylesAndScripts()
498
+	{
499
+		$this->form()->enqueue_js();
500
+	}
501
+
502
+
503
+
504
+	/**
505
+	 * creates and returns the actual form
506
+	 *
507
+	 * @return EE_Form_Section_Proper
508
+	 */
509
+	abstract public function generate();
510
+
511
+
512
+
513
+	/**
514
+	 * creates and returns an EE_Submit_Input labeled "Submit"
515
+	 *
516
+	 * @param string $text
517
+	 * @return EE_Submit_Input
518
+	 */
519
+	public function generateSubmitButton($text = '')
520
+	{
521
+		$text = ! empty($text) ? $text : $this->submitBtnText();
522
+		return new EE_Submit_Input(
523
+			array(
524
+				'html_name'             => 'ee-form-submit-' . $this->slug(),
525
+				'html_id'               => 'ee-form-submit-' . $this->slug(),
526
+				'html_class'            => 'ee-form-submit',
527
+				'html_label'            => '&nbsp;',
528
+				'other_html_attributes' => ' rel="' . $this->slug() . '"',
529
+				'default'               => $text,
530
+			)
531
+		);
532
+	}
533
+
534
+
535
+
536
+	/**
537
+	 * calls generateSubmitButton() and appends it onto the form along with a float clearing div
538
+	 *
539
+	 * @param string $text
540
+	 * @return void
541
+	 * @throws EE_Error
542
+	 */
543
+	public function appendSubmitButton($text = '')
544
+	{
545
+		if ($this->form->subsection_exists($this->slug() . '-submit-btn')) {
546
+			return;
547
+		}
548
+		$this->form->add_subsections(
549
+			array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)),
550
+			null,
551
+			false
552
+		);
553
+	}
554
+
555
+
556
+
557
+	/**
558
+	 * creates and returns an EE_Submit_Input labeled "Cancel"
559
+	 *
560
+	 * @param string $text
561
+	 * @return EE_Submit_Input
562
+	 */
563
+	public function generateCancelButton($text = '')
564
+	{
565
+		$cancel_button = new EE_Submit_Input(
566
+			array(
567
+				'html_name'             => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!!
568
+				'html_id'               => 'ee-cancel-form-' . $this->slug(),
569
+				'html_class'            => 'ee-cancel-form',
570
+				'html_label'            => '&nbsp;',
571
+				'other_html_attributes' => ' rel="' . $this->slug() . '"',
572
+				'default'               => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'),
573
+			)
574
+		);
575
+		$cancel_button->set_button_css_attributes(false);
576
+		return $cancel_button;
577
+	}
578
+
579
+
580
+
581
+	/**
582
+	 * appends a float clearing div onto end of form
583
+	 *
584
+	 * @return void
585
+	 * @throws EE_Error
586
+	 */
587
+	public function clearFormButtonFloats()
588
+	{
589
+		$this->form->add_subsections(
590
+			array(
591
+				'clear-submit-btn-float' => new EE_Form_Section_HTML(
592
+					EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx()
593
+				),
594
+			),
595
+			null,
596
+			false
597
+		);
598
+	}
599
+
600
+
601
+	/**
602
+	 * takes the generated form and displays it along with ony other non-form HTML that may be required
603
+	 * returns a string of HTML that can be directly echoed in a template
604
+	 *
605
+	 * @return string
606
+	 * @throws \InvalidArgumentException
607
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
608
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
609
+	 * @throws LogicException
610
+	 * @throws EE_Error
611
+	 */
612
+	public function display()
613
+	{
614
+		$form_html = apply_filters(
615
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form',
616
+			''
617
+		);
618
+		$form_config = $this->formConfig();
619
+		if (
620
+			$form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
621
+			|| $form_config === FormHandler::ADD_FORM_TAGS_ONLY
622
+		) {
623
+			$form_html .= $this->form()->form_open($this->formAction());
624
+		}
625
+		$form_html .= $this->form(true)->get_html($this->form_has_errors);
626
+		if (
627
+			$form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
628
+			|| $form_config === FormHandler::ADD_FORM_TAGS_ONLY
629
+		) {
630
+			$form_html .= $this->form()->form_close();
631
+		}
632
+		$form_html .= apply_filters(
633
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form',
634
+			''
635
+		);
636
+		return $form_html;
637
+	}
638
+
639
+
640
+	/**
641
+	 * handles processing the form submission
642
+	 * returns true or false depending on whether the form was processed successfully or not
643
+	 *
644
+	 * @param array $submitted_form_data
645
+	 * @return array
646
+	 * @throws \InvalidArgumentException
647
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
648
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
649
+	 * @throws EE_Error
650
+	 * @throws LogicException
651
+	 * @throws InvalidFormSubmissionException
652
+	 */
653
+	public function process($submitted_form_data = array())
654
+	{
655
+		if (! $this->form()->was_submitted($submitted_form_data)) {
656
+			throw new InvalidFormSubmissionException($this->form_name);
657
+		}
658
+		$this->form(true)->receive_form_submission($submitted_form_data);
659
+		if (! $this->form()->is_valid()) {
660
+			throw new InvalidFormSubmissionException(
661
+				$this->form_name,
662
+				sprintf(
663
+					esc_html__(
664
+						'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s',
665
+						'event_espresso'
666
+					),
667
+					$this->form_name,
668
+					'<br />',
669
+					implode('<br />', $this->form()->get_validation_errors_accumulated())
670
+				)
671
+			);
672
+		}
673
+		return apply_filters(
674
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data',
675
+			$this->form()->valid_data(),
676
+			$this
677
+		);
678
+	}
679 679
 
680 680
 
681 681
 
Please login to merge, or discard this patch.
form_sections/strategies/normalization/EE_Null_Normalization.strategy.php 2 patches
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php if (! defined('EVENT_ESPRESSO_VERSION')) {
2
-    exit('No direct script access allowed');
2
+	exit('No direct script access allowed');
3 3
 }
4 4
 
5 5
 
@@ -15,26 +15,26 @@  discard block
 block discarded – undo
15 15
 class EE_Null_Normalization extends EE_Normalization_Strategy_Base
16 16
 {
17 17
 
18
-    /**
19
-     * @param string $value_to_normalize
20
-     * @return null
21
-     */
22
-    public function normalize($value_to_normalize)
23
-    {
24
-        return null;
25
-    }
26
-
27
-
28
-
29
-    /**
30
-     * In the form input we need some string, so use a blank one.
31
-     *
32
-     * @param string $normalized_value
33
-     * @return string
34
-     */
35
-    public function unnormalize($normalized_value)
36
-    {
37
-        return '';
38
-    }
18
+	/**
19
+	 * @param string $value_to_normalize
20
+	 * @return null
21
+	 */
22
+	public function normalize($value_to_normalize)
23
+	{
24
+		return null;
25
+	}
26
+
27
+
28
+
29
+	/**
30
+	 * In the form input we need some string, so use a blank one.
31
+	 *
32
+	 * @param string $normalized_value
33
+	 * @return string
34
+	 */
35
+	public function unnormalize($normalized_value)
36
+	{
37
+		return '';
38
+	}
39 39
 
40 40
 }
41 41
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1
-<?php if (! defined('EVENT_ESPRESSO_VERSION')) {
1
+<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) {
2 2
     exit('No direct script access allowed');
3 3
 }
4 4
 
Please login to merge, or discard this patch.
core/libraries/form_sections/inputs/EE_Form_Input_Base.input.php 2 patches
Indentation   +1171 added lines, -1171 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php if (! defined('EVENT_ESPRESSO_VERSION')) {
2
-    exit('No direct script access allowed');
2
+	exit('No direct script access allowed');
3 3
 }
4 4
 
5 5
 
@@ -16,1174 +16,1174 @@  discard block
 block discarded – undo
16 16
 abstract class EE_Form_Input_Base extends EE_Form_Section_Validatable
17 17
 {
18 18
 
19
-    /**
20
-     * the input's name attribute
21
-     *
22
-     * @var string
23
-     */
24
-    protected $_html_name;
25
-
26
-    /**
27
-     * id for the html label tag
28
-     *
29
-     * @var string
30
-     */
31
-    protected $_html_label_id;
32
-
33
-    /**
34
-     * class for teh html label tag
35
-     *
36
-     * @var string
37
-     */
38
-    protected $_html_label_class;
39
-
40
-    /**
41
-     * any additional html attributes that you may want to add
42
-     *
43
-     * @var string
44
-     */
45
-    protected $_html_other_attributes;
46
-
47
-    /**
48
-     * style for teh html label tag
49
-     *
50
-     * @var string
51
-     */
52
-    protected $_html_label_style;
53
-
54
-    /**
55
-     * text to be placed in the html label
56
-     *
57
-     * @var string
58
-     */
59
-    protected $_html_label_text;
60
-
61
-    /**
62
-     * the full html label. If used, all other html_label_* properties are invalid
63
-     *
64
-     * @var string
65
-     */
66
-    protected $_html_label;
67
-
68
-    /**
69
-     * HTML to use for help text (normally placed below form input), in a span which normally
70
-     * has a class of 'description'
71
-     *
72
-     * @var string
73
-     */
74
-    protected $_html_help_text;
75
-
76
-    /**
77
-     * CSS classes for displaying the help span
78
-     *
79
-     * @var string
80
-     */
81
-    protected $_html_help_class = 'description';
82
-
83
-    /**
84
-     * CSS to put in the style attribute on the help span
85
-     *
86
-     * @var string
87
-     */
88
-    protected $_html_help_style;
89
-
90
-    /**
91
-     * Stores whether or not this input's response is required.
92
-     * Because certain styling elements may also want to know that this
93
-     * input is required etc.
94
-     *
95
-     * @var boolean
96
-     */
97
-    protected $_required;
98
-
99
-    /**
100
-     * css class added to required inputs
101
-     *
102
-     * @var string
103
-     */
104
-    protected $_required_css_class = 'ee-required';
105
-
106
-    /**
107
-     * css styles applied to button type inputs
108
-     *
109
-     * @var string
110
-     */
111
-    protected $_button_css_attributes;
112
-
113
-    /**
114
-     * The raw data submitted for this, like in the $_POST super global.
115
-     * Generally unsafe for usage in client code
116
-     *
117
-     * @var mixed string or array
118
-     */
119
-    protected $_raw_value;
120
-
121
-    /**
122
-     * Value normalized according to the input's normalization strategy.
123
-     * The normalization strategy dictates whether this is a string, int, float,
124
-     * boolean, or array of any of those.
125
-     *
126
-     * @var mixed
127
-     */
128
-    protected $_normalized_value;
129
-
130
-
131
-    /**
132
-     * Normalized default value either initially set on the input, or provided by calling
133
-     * set_default().
134
-     * @var mixed
135
-     */
136
-    protected $_default;
137
-
138
-    /**
139
-     * Strategy used for displaying this field.
140
-     * Child classes must use _get_display_strategy to access it.
141
-     *
142
-     * @var EE_Display_Strategy_Base
143
-     */
144
-    private $_display_strategy;
145
-
146
-    /**
147
-     * Gets all the validation strategies used on this field
148
-     *
149
-     * @var EE_Validation_Strategy_Base[]
150
-     */
151
-    private $_validation_strategies = array();
152
-
153
-    /**
154
-     * The normalization strategy for this field
155
-     *
156
-     * @var EE_Normalization_Strategy_Base
157
-     */
158
-    private $_normalization_strategy;
159
-
160
-    /**
161
-     * Strategy for removing sensitive data after we're done with the form input
162
-     *
163
-     * @var EE_Sensitive_Data_Removal_Base
164
-     */
165
-    protected $_sensitive_data_removal_strategy;
166
-
167
-
168
-
169
-    /**
170
-     * @param array                         $input_args       {
171
-     * @type string                         $html_name        the html name for the input
172
-     * @type string                         $html_label_id    the id attribute to give to the html label tag
173
-     * @type string                         $html_label_class the class attribute to give to the html label tag
174
-     * @type string                         $html_label_style the style attribute to give ot teh label tag
175
-     * @type string                         $html_label_text  the text to put in the label tag
176
-     * @type string                         $html_label       the full html label. If used,
177
-     *                                                        all other html_label_* args are invalid
178
-     * @type string                         $html_help_text   text to put in help element
179
-     * @type string                         $html_help_style  style attribute to give to teh help element
180
-     * @type string                         $html_help_class  class attribute to give to the help element
181
-     * @type string                         $default          default value NORMALIZED (eg, if providing the default
182
-     *       for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0')
183
-     * @type EE_Display_Strategy_Base       $display          strategy
184
-     * @type EE_Normalization_Strategy_Base $normalization_strategy
185
-     * @type EE_Validation_Strategy_Base[]  $validation_strategies
186
-     * @type boolean                        $ignore_input special argument which can be used to avoid adding any validation strategies,
187
-     *                                                    and sets the normalization strategy to the Null normalization. This is good
188
-     *                                                    when you want the input to be totally ignored server-side (like when using
189
-     *                                                    React.js form inputs)
190
-     *                                                        }
191
-     */
192
-    public function __construct($input_args = array())
193
-    {
194
-        $input_args = (array)apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this);
195
-        // the following properties must be cast as arrays
196
-        if (isset($input_args['validation_strategies'])) {
197
-            foreach ((array)$input_args['validation_strategies'] as $validation_strategy) {
198
-                if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) {
199
-                    $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy;
200
-                }
201
-            }
202
-            unset($input_args['validation_strategies']);
203
-        }
204
-        if(isset($input_args['ignore_input'])) {
205
-            $this->_validation_strategies = array();
206
-        }
207
-        // loop thru incoming options
208
-        foreach ($input_args as $key => $value) {
209
-            // add underscore to $key to match property names
210
-            $_key = '_' . $key;
211
-            if (property_exists($this, $_key)) {
212
-                $this->{$_key} = $value;
213
-            }
214
-        }
215
-        // ensure that "required" is set correctly
216
-        $this->set_required(
217
-            $this->_required, isset($input_args['required_validation_error_message'])
218
-            ? $input_args['required_validation_error_message']
219
-            : null
220
-        );
221
-        //$this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE;
222
-        $this->_display_strategy->_construct_finalize($this);
223
-        foreach ($this->_validation_strategies as $validation_strategy) {
224
-            $validation_strategy->_construct_finalize($this);
225
-        }
226
-        if (isset($input_args['ignore_input'])) {
227
-            $this->_normalization_strategy = new EE_Null_Normalization();
228
-        }
229
-        if (! $this->_normalization_strategy) {
230
-                $this->_normalization_strategy = new EE_Text_Normalization();
231
-        }
232
-        $this->_normalization_strategy->_construct_finalize($this);
233
-        //at least we can use the normalization strategy to populate the default
234
-        if (isset($input_args['default'])) {
235
-            $this->set_default($input_args['default']);
236
-            unset($input_args['default']);
237
-        }
238
-        if (! $this->_sensitive_data_removal_strategy) {
239
-            $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal();
240
-        }
241
-        $this->_sensitive_data_removal_strategy->_construct_finalize($this);
242
-        parent::__construct($input_args);
243
-    }
244
-
245
-
246
-
247
-    /**
248
-     * Sets the html_name to its default value, if none was specified in teh constructor.
249
-     * Calculation involves using the name and the parent's html_name
250
-     *
251
-     * @throws \EE_Error
252
-     */
253
-    protected function _set_default_html_name_if_empty()
254
-    {
255
-        if (! $this->_html_name) {
256
-            $this->_html_name = $this->name();
257
-            if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) {
258
-                $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]";
259
-            }
260
-        }
261
-    }
262
-
263
-
264
-
265
-    /**
266
-     * @param $parent_form_section
267
-     * @param $name
268
-     * @throws \EE_Error
269
-     */
270
-    public function _construct_finalize($parent_form_section, $name)
271
-    {
272
-        parent::_construct_finalize($parent_form_section, $name);
273
-        if ($this->_html_label === null && $this->_html_label_text === null) {
274
-            $this->_html_label_text = ucwords(str_replace("_", " ", $name));
275
-        }
276
-        do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name);
277
-    }
278
-
279
-
280
-
281
-    /**
282
-     * Returns the strategy for displaying this form input. If none is set, throws an exception.
283
-     *
284
-     * @return EE_Display_Strategy_Base
285
-     * @throws EE_Error
286
-     */
287
-    protected function _get_display_strategy()
288
-    {
289
-        $this->ensure_construct_finalized_called();
290
-        if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) {
291
-            throw new EE_Error(
292
-                sprintf(
293
-                    __(
294
-                        "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor",
295
-                        "event_espresso"
296
-                    ),
297
-                    $this->html_name(),
298
-                    $this->html_id()
299
-                )
300
-            );
301
-        } else {
302
-            return $this->_display_strategy;
303
-        }
304
-    }
305
-
306
-
307
-
308
-    /**
309
-     * Sets the display strategy.
310
-     *
311
-     * @param EE_Display_Strategy_Base $strategy
312
-     */
313
-    protected function _set_display_strategy(EE_Display_Strategy_Base $strategy)
314
-    {
315
-        $this->_display_strategy = $strategy;
316
-    }
317
-
318
-
319
-
320
-    /**
321
-     * Sets the sanitization strategy
322
-     *
323
-     * @param EE_Normalization_Strategy_Base $strategy
324
-     */
325
-    protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy)
326
-    {
327
-        $this->_normalization_strategy = $strategy;
328
-    }
329
-
330
-
331
-
332
-    /**
333
-     * Gets sensitive_data_removal_strategy
334
-     *
335
-     * @return EE_Sensitive_Data_Removal_Base
336
-     */
337
-    public function get_sensitive_data_removal_strategy()
338
-    {
339
-        return $this->_sensitive_data_removal_strategy;
340
-    }
341
-
342
-
343
-
344
-    /**
345
-     * Sets sensitive_data_removal_strategy
346
-     *
347
-     * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy
348
-     * @return boolean
349
-     */
350
-    public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy)
351
-    {
352
-        $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy;
353
-    }
354
-
355
-
356
-
357
-    /**
358
-     * Gets the display strategy for this input
359
-     *
360
-     * @return EE_Display_Strategy_Base
361
-     */
362
-    public function get_display_strategy()
363
-    {
364
-        return $this->_display_strategy;
365
-    }
366
-
367
-
368
-
369
-    /**
370
-     * Overwrites the display strategy
371
-     *
372
-     * @param EE_Display_Strategy_Base $display_strategy
373
-     */
374
-    public function set_display_strategy($display_strategy)
375
-    {
376
-        $this->_display_strategy = $display_strategy;
377
-        $this->_display_strategy->_construct_finalize($this);
378
-    }
379
-
380
-
381
-
382
-    /**
383
-     * Gets the normalization strategy set on this input
384
-     *
385
-     * @return EE_Normalization_Strategy_Base
386
-     */
387
-    public function get_normalization_strategy()
388
-    {
389
-        return $this->_normalization_strategy;
390
-    }
391
-
392
-
393
-
394
-    /**
395
-     * Overwrites the normalization strategy
396
-     *
397
-     * @param EE_Normalization_Strategy_Base $normalization_strategy
398
-     */
399
-    public function set_normalization_strategy($normalization_strategy)
400
-    {
401
-        $this->_normalization_strategy = $normalization_strategy;
402
-        $this->_normalization_strategy->_construct_finalize($this);
403
-    }
404
-
405
-
406
-
407
-    /**
408
-     * Returns all teh validation strategies which apply to this field, numerically indexed
409
-     *
410
-     * @return EE_Validation_Strategy_Base[]
411
-     */
412
-    public function get_validation_strategies()
413
-    {
414
-        return $this->_validation_strategies;
415
-    }
416
-
417
-
418
-
419
-    /**
420
-     * Adds this strategy to the field so it will be used in both JS validation and server-side validation
421
-     *
422
-     * @param EE_Validation_Strategy_Base $validation_strategy
423
-     * @return void
424
-     */
425
-    protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy)
426
-    {
427
-        $validation_strategy->_construct_finalize($this);
428
-        $this->_validation_strategies[] = $validation_strategy;
429
-    }
430
-
431
-
432
-
433
-    /**
434
-     * Adds a new validation strategy onto the form input
435
-     *
436
-     * @param EE_Validation_Strategy_Base $validation_strategy
437
-     * @return void
438
-     */
439
-    public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy)
440
-    {
441
-        $this->_add_validation_strategy($validation_strategy);
442
-    }
443
-
444
-
445
-
446
-    /**
447
-     * The classname of the validation strategy to remove
448
-     *
449
-     * @param string $validation_strategy_classname
450
-     */
451
-    public function remove_validation_strategy($validation_strategy_classname)
452
-    {
453
-        foreach ($this->_validation_strategies as $key => $validation_strategy) {
454
-            if (
455
-                $validation_strategy instanceof $validation_strategy_classname
456
-                || is_subclass_of($validation_strategy, $validation_strategy_classname)
457
-            ) {
458
-                unset($this->_validation_strategies[$key]);
459
-            }
460
-        }
461
-    }
462
-
463
-
464
-
465
-    /**
466
-     * returns true if input employs any of the validation strategy defined by the supplied array of classnames
467
-     *
468
-     * @param array $validation_strategy_classnames
469
-     * @return bool
470
-     */
471
-    public function has_validation_strategy($validation_strategy_classnames)
472
-    {
473
-        $validation_strategy_classnames = is_array($validation_strategy_classnames)
474
-            ? $validation_strategy_classnames
475
-            : array($validation_strategy_classnames);
476
-        foreach ($this->_validation_strategies as $key => $validation_strategy) {
477
-            if (in_array($key, $validation_strategy_classnames)) {
478
-                return true;
479
-            }
480
-        }
481
-        return false;
482
-    }
483
-
484
-
485
-
486
-    /**
487
-     * Gets the HTML
488
-     *
489
-     * @return string
490
-     */
491
-    public function get_html()
492
-    {
493
-        return $this->_parent_section->get_html_for_input($this);
494
-    }
495
-
496
-
497
-
498
-    /**
499
-     * Gets the HTML for the input itself (no label or errors) according to the
500
-     * input's display strategy
501
-     * Makes sure the JS and CSS are enqueued for it
502
-     *
503
-     * @return string
504
-     * @throws \EE_Error
505
-     */
506
-    public function get_html_for_input()
507
-    {
508
-        return $this->_form_html_filter
509
-            ? $this->_form_html_filter->filterHtml(
510
-                $this->_get_display_strategy()->display(),
511
-                $this
512
-            )
513
-            : $this->_get_display_strategy()->display();
514
-    }
515
-
516
-
517
-
518
-    /**
519
-     * @return string
520
-     */
521
-    public function html_other_attributes()
522
-    {
523
-        return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : '';
524
-    }
525
-
526
-
527
-
528
-    /**
529
-     * @param string $html_other_attributes
530
-     */
531
-    public function set_html_other_attributes($html_other_attributes)
532
-    {
533
-        $this->_html_other_attributes = $html_other_attributes;
534
-    }
535
-
536
-
537
-
538
-    /**
539
-     * Gets the HTML for displaying the label for this form input
540
-     * according to the form section's layout strategy
541
-     *
542
-     * @return string
543
-     */
544
-    public function get_html_for_label()
545
-    {
546
-        return $this->_parent_section->get_layout_strategy()->display_label($this);
547
-    }
548
-
549
-
550
-
551
-    /**
552
-     * Gets the HTML for displaying the errors section for this form input
553
-     * according to the form section's layout strategy
554
-     *
555
-     * @return string
556
-     */
557
-    public function get_html_for_errors()
558
-    {
559
-        return $this->_parent_section->get_layout_strategy()->display_errors($this);
560
-    }
561
-
562
-
563
-
564
-    /**
565
-     * Gets the HTML for displaying the help text for this form input
566
-     * according to the form section's layout strategy
567
-     *
568
-     * @return string
569
-     */
570
-    public function get_html_for_help()
571
-    {
572
-        return $this->_parent_section->get_layout_strategy()->display_help_text($this);
573
-    }
574
-
575
-
576
-
577
-    /**
578
-     * Validates the input's sanitized value (assumes _sanitize() has already been called)
579
-     * and returns whether or not the form input's submitted value is value
580
-     *
581
-     * @return boolean
582
-     */
583
-    protected function _validate()
584
-    {
585
-        foreach ($this->_validation_strategies as $validation_strategy) {
586
-            if ($validation_strategy instanceof EE_Validation_Strategy_Base) {
587
-                try {
588
-                    $validation_strategy->validate($this->normalized_value());
589
-                } catch (EE_Validation_Error $e) {
590
-                    $this->add_validation_error($e);
591
-                }
592
-            }
593
-        }
594
-        if ($this->get_validation_errors()) {
595
-            return false;
596
-        } else {
597
-            return true;
598
-        }
599
-    }
600
-
601
-
602
-
603
-    /**
604
-     * Performs basic sanitization on this value. But what sanitization can be performed anyways?
605
-     * This value MIGHT be allowed to have tags, so we can't really remove them.
606
-     *
607
-     * @param string $value
608
-     * @return null|string
609
-     */
610
-    private function _sanitize($value)
611
-    {
612
-        return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null;
613
-    }
614
-
615
-
616
-
617
-    /**
618
-     * Picks out the form value that relates to this form input,
619
-     * and stores it as the sanitized value on the form input, and sets the normalized value.
620
-     * Returns whether or not any validation errors occurred
621
-     *
622
-     * @param array $req_data like $_POST
623
-     * @return boolean whether or not there was an error
624
-     * @throws \EE_Error
625
-     */
626
-    protected function _normalize($req_data)
627
-    {
628
-        //any existing validation errors don't apply so clear them
629
-        $this->_validation_errors = array();
630
-        try {
631
-            $raw_input = $this->find_form_data_for_this_section($req_data);
632
-            //super simple sanitization for now
633
-            if (is_array($raw_input)) {
634
-                $raw_value = array();
635
-                foreach ($raw_input as $key => $value) {
636
-                    $raw_value[$key] = $this->_sanitize($value);
637
-                }
638
-                $this->_set_raw_value($raw_value);
639
-            } else {
640
-                $this->_set_raw_value($this->_sanitize($raw_input));
641
-            }
642
-            //we want to mostly leave the input alone in case we need to re-display it to the user
643
-            $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value()));
644
-        } catch (EE_Validation_Error $e) {
645
-            $this->add_validation_error($e);
646
-        }
647
-    }
648
-
649
-
650
-
651
-    /**
652
-     * @return string
653
-     */
654
-    public function html_name()
655
-    {
656
-        $this->_set_default_html_name_if_empty();
657
-        return $this->_html_name;
658
-    }
659
-
660
-
661
-
662
-    /**
663
-     * @return string
664
-     */
665
-    public function html_label_id()
666
-    {
667
-        return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl';
668
-    }
669
-
670
-
671
-
672
-    /**
673
-     * @return string
674
-     */
675
-    public function html_label_class()
676
-    {
677
-        return $this->_html_label_class;
678
-    }
679
-
680
-
681
-
682
-    /**
683
-     * @return string
684
-     */
685
-    public function html_label_style()
686
-    {
687
-        return $this->_html_label_style;
688
-    }
689
-
690
-
691
-
692
-    /**
693
-     * @return string
694
-     */
695
-    public function html_label_text()
696
-    {
697
-        return $this->_html_label_text;
698
-    }
699
-
700
-
701
-
702
-    /**
703
-     * @return string
704
-     */
705
-    public function html_help_text()
706
-    {
707
-        return $this->_html_help_text;
708
-    }
709
-
710
-
711
-
712
-    /**
713
-     * @return string
714
-     */
715
-    public function html_help_class()
716
-    {
717
-        return $this->_html_help_class;
718
-    }
719
-
720
-
721
-
722
-    /**
723
-     * @return string
724
-     */
725
-    public function html_help_style()
726
-    {
727
-        return $this->_html_style;
728
-    }
729
-
730
-
731
-
732
-    /**
733
-     * returns the raw, UNSAFE, input, almost exactly as the user submitted it.
734
-     * Please note that almost all client code should instead use the normalized_value;
735
-     * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag,
736
-     * mostly by escaping quotes)
737
-     * Note, we do not store the exact original value sent in the user's request because
738
-     * it may have malicious content, and we MIGHT want to store the form input in a transient or something...
739
-     * in which case, we would have stored the malicious content to our database.
740
-     *
741
-     * @return string
742
-     */
743
-    public function raw_value()
744
-    {
745
-        return $this->_raw_value;
746
-    }
747
-
748
-
749
-
750
-    /**
751
-     * Returns a string safe to usage in form inputs when displaying, because
752
-     * it escapes all html entities
753
-     *
754
-     * @return string
755
-     */
756
-    public function raw_value_in_form()
757
-    {
758
-        return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8');
759
-    }
760
-
761
-
762
-
763
-    /**
764
-     * returns the value after it's been sanitized, and then converted into it's proper type
765
-     * in PHP. Eg, a string, an int, an array,
766
-     *
767
-     * @return mixed
768
-     */
769
-    public function normalized_value()
770
-    {
771
-        return $this->_normalized_value;
772
-    }
773
-
774
-
775
-
776
-    /**
777
-     * Returns the normalized value is a presentable way. By default this is just
778
-     * the normalized value by itself, but it can be overridden for when that's not
779
-     * the best thing to display
780
-     *
781
-     * @return string
782
-     */
783
-    public function pretty_value()
784
-    {
785
-        return $this->_normalized_value;
786
-    }
787
-
788
-
789
-
790
-    /**
791
-     * When generating the JS for the jquery validation rules like<br>
792
-     * <code>$( "#myform" ).validate({
793
-     * rules: {
794
-     * password: "required",
795
-     * password_again: {
796
-     * equalTo: "#password"
797
-     * }
798
-     * }
799
-     * });</code>
800
-     * if this field had the name 'password_again', it should return
801
-     * <br><code>password_again: {
802
-     * equalTo: "#password"
803
-     * }</code>
804
-     *
805
-     * @return array
806
-     */
807
-    public function get_jquery_validation_rules()
808
-    {
809
-        $jquery_validation_js = array();
810
-        $jquery_validation_rules = array();
811
-        foreach ($this->get_validation_strategies() as $validation_strategy) {
812
-            $jquery_validation_rules = array_replace_recursive(
813
-                $jquery_validation_rules,
814
-                $validation_strategy->get_jquery_validation_rule_array()
815
-            );
816
-        }
817
-        if (! empty($jquery_validation_rules)) {
818
-            foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) {
819
-                $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules;
820
-            }
821
-        }
822
-        return $jquery_validation_js;
823
-    }
824
-
825
-
826
-
827
-    /**
828
-     * Sets the input's default value for use in displaying in the form. Note: value should be
829
-     * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0')
830
-     *
831
-     * @param mixed $value
832
-     * @return void
833
-     */
834
-    public function set_default($value)
835
-    {
836
-        $this->_default = $value;
837
-        $this->_set_normalized_value($value);
838
-        $this->_set_raw_value($value);
839
-    }
840
-
841
-
842
-
843
-    /**
844
-     * Sets the normalized value on this input
845
-     *
846
-     * @param mixed $value
847
-     */
848
-    protected function _set_normalized_value($value)
849
-    {
850
-        $this->_normalized_value = $value;
851
-    }
852
-
853
-
854
-
855
-    /**
856
-     * Sets the raw value on this input (ie, exactly as the user submitted it)
857
-     *
858
-     * @param mixed $value
859
-     */
860
-    protected function _set_raw_value($value)
861
-    {
862
-        $this->_raw_value = $this->_normalization_strategy->unnormalize($value);
863
-    }
864
-
865
-
866
-
867
-    /**
868
-     * Sets the HTML label text after it has already been defined
869
-     *
870
-     * @param string $label
871
-     * @return void
872
-     */
873
-    public function set_html_label_text($label)
874
-    {
875
-        $this->_html_label_text = $label;
876
-    }
877
-
878
-
879
-
880
-    /**
881
-     * Sets whether or not this field is required, and adjusts the validation strategy.
882
-     * If you want to use the EE_Conditionally_Required_Validation_Strategy,
883
-     * please add it as a validation strategy using add_validation_strategy as normal
884
-     *
885
-     * @param boolean $required boolean
886
-     * @param null    $required_text
887
-     */
888
-    public function set_required($required = true, $required_text = null)
889
-    {
890
-        $required = filter_var($required, FILTER_VALIDATE_BOOLEAN);
891
-        //whether $required is a string or a boolean, we want to add a required validation strategy
892
-        if ($required) {
893
-            $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text));
894
-        } else {
895
-            $this->remove_validation_strategy('EE_Required_Validation_Strategy');
896
-        }
897
-        $this->_required = $required;
898
-    }
899
-
900
-
901
-
902
-    /**
903
-     * Returns whether or not this field is required
904
-     *
905
-     * @return boolean
906
-     */
907
-    public function required()
908
-    {
909
-        return $this->_required;
910
-    }
911
-
912
-
913
-
914
-    /**
915
-     * @param string $required_css_class
916
-     */
917
-    public function set_required_css_class($required_css_class)
918
-    {
919
-        $this->_required_css_class = $required_css_class;
920
-    }
921
-
922
-
923
-
924
-    /**
925
-     * @return string
926
-     */
927
-    public function required_css_class()
928
-    {
929
-        return $this->_required_css_class;
930
-    }
931
-
932
-
933
-
934
-    /**
935
-     * @param bool $add_required
936
-     * @return string
937
-     */
938
-    public function html_class($add_required = false)
939
-    {
940
-        return $add_required && $this->required()
941
-            ? $this->required_css_class() . ' ' . $this->_html_class
942
-            : $this->_html_class;
943
-    }
944
-
945
-
946
-    /**
947
-     * Sets the help text, in case
948
-     *
949
-     * @param string $text
950
-     */
951
-    public function set_html_help_text($text)
952
-    {
953
-        $this->_html_help_text = $text;
954
-    }
955
-
956
-
957
-
958
-    /**
959
-     * Uses the sensitive data removal strategy to remove the sensitive data from this
960
-     * input. If there is any kind of sensitive data removal on this input, we clear
961
-     * out the raw value completely
962
-     *
963
-     * @return void
964
-     */
965
-    public function clean_sensitive_data()
966
-    {
967
-        //if we do ANY kind of sensitive data removal on this, then just clear out the raw value
968
-        //if we need more logic than this we'll make a strategy for it
969
-        if ($this->_sensitive_data_removal_strategy
970
-            && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal
971
-        ) {
972
-            $this->_set_raw_value(null);
973
-        }
974
-        //and clean the normalized value according to the appropriate strategy
975
-        $this->_set_normalized_value(
976
-            $this->get_sensitive_data_removal_strategy()->remove_sensitive_data(
977
-                $this->_normalized_value
978
-            )
979
-        );
980
-    }
981
-
982
-
983
-
984
-    /**
985
-     * @param bool   $primary
986
-     * @param string $button_size
987
-     * @param string $other_attributes
988
-     */
989
-    public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '')
990
-    {
991
-        $button_css_attributes = 'button';
992
-        $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary';
993
-        switch ($button_size) {
994
-            case 'xs' :
995
-            case 'extra-small' :
996
-                $button_css_attributes .= ' button-xs';
997
-                break;
998
-            case 'sm' :
999
-            case 'small' :
1000
-                $button_css_attributes .= ' button-sm';
1001
-                break;
1002
-            case 'lg' :
1003
-            case 'large' :
1004
-                $button_css_attributes .= ' button-lg';
1005
-                break;
1006
-            case 'block' :
1007
-                $button_css_attributes .= ' button-block';
1008
-                break;
1009
-            case 'md' :
1010
-            case 'medium' :
1011
-            default :
1012
-                $button_css_attributes .= '';
1013
-        }
1014
-        $this->_button_css_attributes .= ! empty($other_attributes)
1015
-            ? $button_css_attributes . ' ' . $other_attributes
1016
-            : $button_css_attributes;
1017
-    }
1018
-
1019
-
1020
-
1021
-    /**
1022
-     * @return string
1023
-     */
1024
-    public function button_css_attributes()
1025
-    {
1026
-        if (empty($this->_button_css_attributes)) {
1027
-            $this->set_button_css_attributes();
1028
-        }
1029
-        return $this->_button_css_attributes;
1030
-    }
1031
-
1032
-
1033
-
1034
-    /**
1035
-     * find_form_data_for_this_section
1036
-     * using this section's name and its parents, finds the value of the form data that corresponds to it.
1037
-     * For example, if this form section's HTML name is my_form[subform][form_input_1],
1038
-     * then it's value should be in $_REQUEST at $_REQUEST['my_form']['subform']['form_input_1'].
1039
-     * (If that doesn't exist, we also check for this subsection's name
1040
-     * at the TOP LEVEL of the request data. Eg $_REQUEST['form_input_1'].)
1041
-     * This function finds its value in the form.
1042
-     *
1043
-     * @param array $req_data
1044
-     * @return mixed whatever the raw value of this form section is in the request data
1045
-     * @throws \EE_Error
1046
-     */
1047
-    public function find_form_data_for_this_section($req_data)
1048
-    {
1049
-        // break up the html name by "[]"
1050
-        if (strpos($this->html_name(), '[') !== false) {
1051
-            $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '['));
1052
-        } else {
1053
-            $before_any_brackets = $this->html_name();
1054
-        }
1055
-        // grab all of the segments
1056
-        preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches);
1057
-        if (isset($matches[1]) && is_array($matches[1])) {
1058
-            $name_parts = $matches[1];
1059
-            array_unshift($name_parts, $before_any_brackets);
1060
-        } else {
1061
-            $name_parts = array($before_any_brackets);
1062
-        }
1063
-        // now get the value for the input
1064
-        $value = $this->_find_form_data_for_this_section_using_name_parts($name_parts, $req_data);
1065
-        // check if this thing's name is at the TOP level of the request data
1066
-        if ($value === null && isset($req_data[$this->name()])) {
1067
-            $value = $req_data[$this->name()];
1068
-        }
1069
-        return $value;
1070
-    }
1071
-
1072
-
1073
-
1074
-    /**
1075
-     * @param array $html_name_parts
1076
-     * @param array $req_data
1077
-     * @return array | NULL
1078
-     */
1079
-    public function _find_form_data_for_this_section_using_name_parts($html_name_parts, $req_data)
1080
-    {
1081
-        $first_part_to_consider = array_shift($html_name_parts);
1082
-        if (isset($req_data[$first_part_to_consider])) {
1083
-            if (empty($html_name_parts)) {
1084
-                return $req_data[$first_part_to_consider];
1085
-            } else {
1086
-                return $this->_find_form_data_for_this_section_using_name_parts(
1087
-                    $html_name_parts,
1088
-                    $req_data[$first_part_to_consider]
1089
-                );
1090
-            }
1091
-        } else {
1092
-            return null;
1093
-        }
1094
-    }
1095
-
1096
-
1097
-
1098
-    /**
1099
-     * Checks if this form input's data is in the request data
1100
-     *
1101
-     * @param array $req_data like $_POST
1102
-     * @return boolean
1103
-     * @throws \EE_Error
1104
-     */
1105
-    public function form_data_present_in($req_data = null)
1106
-    {
1107
-        if ($req_data === null) {
1108
-            $req_data = $_POST;
1109
-        }
1110
-        $checked_value = $this->find_form_data_for_this_section($req_data);
1111
-        if ($checked_value !== null) {
1112
-            return true;
1113
-        } else {
1114
-            return false;
1115
-        }
1116
-    }
1117
-
1118
-
1119
-
1120
-    /**
1121
-     * Overrides parent to add js data from validation and display strategies
1122
-     *
1123
-     * @param array $form_other_js_data
1124
-     * @return array
1125
-     */
1126
-    public function get_other_js_data($form_other_js_data = array())
1127
-    {
1128
-        $form_other_js_data = $this->get_other_js_data_from_strategies($form_other_js_data);
1129
-        return $form_other_js_data;
1130
-    }
1131
-
1132
-
1133
-
1134
-    /**
1135
-     * Gets other JS data for localization from this input's strategies, like
1136
-     * the validation strategies and the display strategy
1137
-     *
1138
-     * @param array $form_other_js_data
1139
-     * @return array
1140
-     */
1141
-    public function get_other_js_data_from_strategies($form_other_js_data = array())
1142
-    {
1143
-        $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data);
1144
-        foreach ($this->get_validation_strategies() as $validation_strategy) {
1145
-            $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data);
1146
-        }
1147
-        return $form_other_js_data;
1148
-    }
1149
-
1150
-
1151
-
1152
-    /**
1153
-     * Override parent because we want to give our strategies an opportunity to enqueue some js and css
1154
-     *
1155
-     * @return void
1156
-     */
1157
-    public function enqueue_js()
1158
-    {
1159
-        //ask our display strategy and validation strategies if they have js to enqueue
1160
-        $this->enqueue_js_from_strategies();
1161
-    }
1162
-
1163
-
1164
-
1165
-    /**
1166
-     * Tells strategies when its ok to enqueue their js and css
1167
-     *
1168
-     * @return void
1169
-     */
1170
-    public function enqueue_js_from_strategies()
1171
-    {
1172
-        $this->get_display_strategy()->enqueue_js();
1173
-        foreach ($this->get_validation_strategies() as $validation_strategy) {
1174
-            $validation_strategy->enqueue_js();
1175
-        }
1176
-    }
1177
-
1178
-
1179
-
1180
-    /**
1181
-     * Gets the default value set on the input (not the current value, which may have been
1182
-     * changed because of a form submission). If no default was set, this us null.
1183
-     * @return mixed
1184
-     */
1185
-    public function get_default()
1186
-    {
1187
-        return $this->_default;
1188
-    }
19
+	/**
20
+	 * the input's name attribute
21
+	 *
22
+	 * @var string
23
+	 */
24
+	protected $_html_name;
25
+
26
+	/**
27
+	 * id for the html label tag
28
+	 *
29
+	 * @var string
30
+	 */
31
+	protected $_html_label_id;
32
+
33
+	/**
34
+	 * class for teh html label tag
35
+	 *
36
+	 * @var string
37
+	 */
38
+	protected $_html_label_class;
39
+
40
+	/**
41
+	 * any additional html attributes that you may want to add
42
+	 *
43
+	 * @var string
44
+	 */
45
+	protected $_html_other_attributes;
46
+
47
+	/**
48
+	 * style for teh html label tag
49
+	 *
50
+	 * @var string
51
+	 */
52
+	protected $_html_label_style;
53
+
54
+	/**
55
+	 * text to be placed in the html label
56
+	 *
57
+	 * @var string
58
+	 */
59
+	protected $_html_label_text;
60
+
61
+	/**
62
+	 * the full html label. If used, all other html_label_* properties are invalid
63
+	 *
64
+	 * @var string
65
+	 */
66
+	protected $_html_label;
67
+
68
+	/**
69
+	 * HTML to use for help text (normally placed below form input), in a span which normally
70
+	 * has a class of 'description'
71
+	 *
72
+	 * @var string
73
+	 */
74
+	protected $_html_help_text;
75
+
76
+	/**
77
+	 * CSS classes for displaying the help span
78
+	 *
79
+	 * @var string
80
+	 */
81
+	protected $_html_help_class = 'description';
82
+
83
+	/**
84
+	 * CSS to put in the style attribute on the help span
85
+	 *
86
+	 * @var string
87
+	 */
88
+	protected $_html_help_style;
89
+
90
+	/**
91
+	 * Stores whether or not this input's response is required.
92
+	 * Because certain styling elements may also want to know that this
93
+	 * input is required etc.
94
+	 *
95
+	 * @var boolean
96
+	 */
97
+	protected $_required;
98
+
99
+	/**
100
+	 * css class added to required inputs
101
+	 *
102
+	 * @var string
103
+	 */
104
+	protected $_required_css_class = 'ee-required';
105
+
106
+	/**
107
+	 * css styles applied to button type inputs
108
+	 *
109
+	 * @var string
110
+	 */
111
+	protected $_button_css_attributes;
112
+
113
+	/**
114
+	 * The raw data submitted for this, like in the $_POST super global.
115
+	 * Generally unsafe for usage in client code
116
+	 *
117
+	 * @var mixed string or array
118
+	 */
119
+	protected $_raw_value;
120
+
121
+	/**
122
+	 * Value normalized according to the input's normalization strategy.
123
+	 * The normalization strategy dictates whether this is a string, int, float,
124
+	 * boolean, or array of any of those.
125
+	 *
126
+	 * @var mixed
127
+	 */
128
+	protected $_normalized_value;
129
+
130
+
131
+	/**
132
+	 * Normalized default value either initially set on the input, or provided by calling
133
+	 * set_default().
134
+	 * @var mixed
135
+	 */
136
+	protected $_default;
137
+
138
+	/**
139
+	 * Strategy used for displaying this field.
140
+	 * Child classes must use _get_display_strategy to access it.
141
+	 *
142
+	 * @var EE_Display_Strategy_Base
143
+	 */
144
+	private $_display_strategy;
145
+
146
+	/**
147
+	 * Gets all the validation strategies used on this field
148
+	 *
149
+	 * @var EE_Validation_Strategy_Base[]
150
+	 */
151
+	private $_validation_strategies = array();
152
+
153
+	/**
154
+	 * The normalization strategy for this field
155
+	 *
156
+	 * @var EE_Normalization_Strategy_Base
157
+	 */
158
+	private $_normalization_strategy;
159
+
160
+	/**
161
+	 * Strategy for removing sensitive data after we're done with the form input
162
+	 *
163
+	 * @var EE_Sensitive_Data_Removal_Base
164
+	 */
165
+	protected $_sensitive_data_removal_strategy;
166
+
167
+
168
+
169
+	/**
170
+	 * @param array                         $input_args       {
171
+	 * @type string                         $html_name        the html name for the input
172
+	 * @type string                         $html_label_id    the id attribute to give to the html label tag
173
+	 * @type string                         $html_label_class the class attribute to give to the html label tag
174
+	 * @type string                         $html_label_style the style attribute to give ot teh label tag
175
+	 * @type string                         $html_label_text  the text to put in the label tag
176
+	 * @type string                         $html_label       the full html label. If used,
177
+	 *                                                        all other html_label_* args are invalid
178
+	 * @type string                         $html_help_text   text to put in help element
179
+	 * @type string                         $html_help_style  style attribute to give to teh help element
180
+	 * @type string                         $html_help_class  class attribute to give to the help element
181
+	 * @type string                         $default          default value NORMALIZED (eg, if providing the default
182
+	 *       for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0')
183
+	 * @type EE_Display_Strategy_Base       $display          strategy
184
+	 * @type EE_Normalization_Strategy_Base $normalization_strategy
185
+	 * @type EE_Validation_Strategy_Base[]  $validation_strategies
186
+	 * @type boolean                        $ignore_input special argument which can be used to avoid adding any validation strategies,
187
+	 *                                                    and sets the normalization strategy to the Null normalization. This is good
188
+	 *                                                    when you want the input to be totally ignored server-side (like when using
189
+	 *                                                    React.js form inputs)
190
+	 *                                                        }
191
+	 */
192
+	public function __construct($input_args = array())
193
+	{
194
+		$input_args = (array)apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this);
195
+		// the following properties must be cast as arrays
196
+		if (isset($input_args['validation_strategies'])) {
197
+			foreach ((array)$input_args['validation_strategies'] as $validation_strategy) {
198
+				if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) {
199
+					$this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy;
200
+				}
201
+			}
202
+			unset($input_args['validation_strategies']);
203
+		}
204
+		if(isset($input_args['ignore_input'])) {
205
+			$this->_validation_strategies = array();
206
+		}
207
+		// loop thru incoming options
208
+		foreach ($input_args as $key => $value) {
209
+			// add underscore to $key to match property names
210
+			$_key = '_' . $key;
211
+			if (property_exists($this, $_key)) {
212
+				$this->{$_key} = $value;
213
+			}
214
+		}
215
+		// ensure that "required" is set correctly
216
+		$this->set_required(
217
+			$this->_required, isset($input_args['required_validation_error_message'])
218
+			? $input_args['required_validation_error_message']
219
+			: null
220
+		);
221
+		//$this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE;
222
+		$this->_display_strategy->_construct_finalize($this);
223
+		foreach ($this->_validation_strategies as $validation_strategy) {
224
+			$validation_strategy->_construct_finalize($this);
225
+		}
226
+		if (isset($input_args['ignore_input'])) {
227
+			$this->_normalization_strategy = new EE_Null_Normalization();
228
+		}
229
+		if (! $this->_normalization_strategy) {
230
+				$this->_normalization_strategy = new EE_Text_Normalization();
231
+		}
232
+		$this->_normalization_strategy->_construct_finalize($this);
233
+		//at least we can use the normalization strategy to populate the default
234
+		if (isset($input_args['default'])) {
235
+			$this->set_default($input_args['default']);
236
+			unset($input_args['default']);
237
+		}
238
+		if (! $this->_sensitive_data_removal_strategy) {
239
+			$this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal();
240
+		}
241
+		$this->_sensitive_data_removal_strategy->_construct_finalize($this);
242
+		parent::__construct($input_args);
243
+	}
244
+
245
+
246
+
247
+	/**
248
+	 * Sets the html_name to its default value, if none was specified in teh constructor.
249
+	 * Calculation involves using the name and the parent's html_name
250
+	 *
251
+	 * @throws \EE_Error
252
+	 */
253
+	protected function _set_default_html_name_if_empty()
254
+	{
255
+		if (! $this->_html_name) {
256
+			$this->_html_name = $this->name();
257
+			if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) {
258
+				$this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]";
259
+			}
260
+		}
261
+	}
262
+
263
+
264
+
265
+	/**
266
+	 * @param $parent_form_section
267
+	 * @param $name
268
+	 * @throws \EE_Error
269
+	 */
270
+	public function _construct_finalize($parent_form_section, $name)
271
+	{
272
+		parent::_construct_finalize($parent_form_section, $name);
273
+		if ($this->_html_label === null && $this->_html_label_text === null) {
274
+			$this->_html_label_text = ucwords(str_replace("_", " ", $name));
275
+		}
276
+		do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name);
277
+	}
278
+
279
+
280
+
281
+	/**
282
+	 * Returns the strategy for displaying this form input. If none is set, throws an exception.
283
+	 *
284
+	 * @return EE_Display_Strategy_Base
285
+	 * @throws EE_Error
286
+	 */
287
+	protected function _get_display_strategy()
288
+	{
289
+		$this->ensure_construct_finalized_called();
290
+		if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) {
291
+			throw new EE_Error(
292
+				sprintf(
293
+					__(
294
+						"Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor",
295
+						"event_espresso"
296
+					),
297
+					$this->html_name(),
298
+					$this->html_id()
299
+				)
300
+			);
301
+		} else {
302
+			return $this->_display_strategy;
303
+		}
304
+	}
305
+
306
+
307
+
308
+	/**
309
+	 * Sets the display strategy.
310
+	 *
311
+	 * @param EE_Display_Strategy_Base $strategy
312
+	 */
313
+	protected function _set_display_strategy(EE_Display_Strategy_Base $strategy)
314
+	{
315
+		$this->_display_strategy = $strategy;
316
+	}
317
+
318
+
319
+
320
+	/**
321
+	 * Sets the sanitization strategy
322
+	 *
323
+	 * @param EE_Normalization_Strategy_Base $strategy
324
+	 */
325
+	protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy)
326
+	{
327
+		$this->_normalization_strategy = $strategy;
328
+	}
329
+
330
+
331
+
332
+	/**
333
+	 * Gets sensitive_data_removal_strategy
334
+	 *
335
+	 * @return EE_Sensitive_Data_Removal_Base
336
+	 */
337
+	public function get_sensitive_data_removal_strategy()
338
+	{
339
+		return $this->_sensitive_data_removal_strategy;
340
+	}
341
+
342
+
343
+
344
+	/**
345
+	 * Sets sensitive_data_removal_strategy
346
+	 *
347
+	 * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy
348
+	 * @return boolean
349
+	 */
350
+	public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy)
351
+	{
352
+		$this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy;
353
+	}
354
+
355
+
356
+
357
+	/**
358
+	 * Gets the display strategy for this input
359
+	 *
360
+	 * @return EE_Display_Strategy_Base
361
+	 */
362
+	public function get_display_strategy()
363
+	{
364
+		return $this->_display_strategy;
365
+	}
366
+
367
+
368
+
369
+	/**
370
+	 * Overwrites the display strategy
371
+	 *
372
+	 * @param EE_Display_Strategy_Base $display_strategy
373
+	 */
374
+	public function set_display_strategy($display_strategy)
375
+	{
376
+		$this->_display_strategy = $display_strategy;
377
+		$this->_display_strategy->_construct_finalize($this);
378
+	}
379
+
380
+
381
+
382
+	/**
383
+	 * Gets the normalization strategy set on this input
384
+	 *
385
+	 * @return EE_Normalization_Strategy_Base
386
+	 */
387
+	public function get_normalization_strategy()
388
+	{
389
+		return $this->_normalization_strategy;
390
+	}
391
+
392
+
393
+
394
+	/**
395
+	 * Overwrites the normalization strategy
396
+	 *
397
+	 * @param EE_Normalization_Strategy_Base $normalization_strategy
398
+	 */
399
+	public function set_normalization_strategy($normalization_strategy)
400
+	{
401
+		$this->_normalization_strategy = $normalization_strategy;
402
+		$this->_normalization_strategy->_construct_finalize($this);
403
+	}
404
+
405
+
406
+
407
+	/**
408
+	 * Returns all teh validation strategies which apply to this field, numerically indexed
409
+	 *
410
+	 * @return EE_Validation_Strategy_Base[]
411
+	 */
412
+	public function get_validation_strategies()
413
+	{
414
+		return $this->_validation_strategies;
415
+	}
416
+
417
+
418
+
419
+	/**
420
+	 * Adds this strategy to the field so it will be used in both JS validation and server-side validation
421
+	 *
422
+	 * @param EE_Validation_Strategy_Base $validation_strategy
423
+	 * @return void
424
+	 */
425
+	protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy)
426
+	{
427
+		$validation_strategy->_construct_finalize($this);
428
+		$this->_validation_strategies[] = $validation_strategy;
429
+	}
430
+
431
+
432
+
433
+	/**
434
+	 * Adds a new validation strategy onto the form input
435
+	 *
436
+	 * @param EE_Validation_Strategy_Base $validation_strategy
437
+	 * @return void
438
+	 */
439
+	public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy)
440
+	{
441
+		$this->_add_validation_strategy($validation_strategy);
442
+	}
443
+
444
+
445
+
446
+	/**
447
+	 * The classname of the validation strategy to remove
448
+	 *
449
+	 * @param string $validation_strategy_classname
450
+	 */
451
+	public function remove_validation_strategy($validation_strategy_classname)
452
+	{
453
+		foreach ($this->_validation_strategies as $key => $validation_strategy) {
454
+			if (
455
+				$validation_strategy instanceof $validation_strategy_classname
456
+				|| is_subclass_of($validation_strategy, $validation_strategy_classname)
457
+			) {
458
+				unset($this->_validation_strategies[$key]);
459
+			}
460
+		}
461
+	}
462
+
463
+
464
+
465
+	/**
466
+	 * returns true if input employs any of the validation strategy defined by the supplied array of classnames
467
+	 *
468
+	 * @param array $validation_strategy_classnames
469
+	 * @return bool
470
+	 */
471
+	public function has_validation_strategy($validation_strategy_classnames)
472
+	{
473
+		$validation_strategy_classnames = is_array($validation_strategy_classnames)
474
+			? $validation_strategy_classnames
475
+			: array($validation_strategy_classnames);
476
+		foreach ($this->_validation_strategies as $key => $validation_strategy) {
477
+			if (in_array($key, $validation_strategy_classnames)) {
478
+				return true;
479
+			}
480
+		}
481
+		return false;
482
+	}
483
+
484
+
485
+
486
+	/**
487
+	 * Gets the HTML
488
+	 *
489
+	 * @return string
490
+	 */
491
+	public function get_html()
492
+	{
493
+		return $this->_parent_section->get_html_for_input($this);
494
+	}
495
+
496
+
497
+
498
+	/**
499
+	 * Gets the HTML for the input itself (no label or errors) according to the
500
+	 * input's display strategy
501
+	 * Makes sure the JS and CSS are enqueued for it
502
+	 *
503
+	 * @return string
504
+	 * @throws \EE_Error
505
+	 */
506
+	public function get_html_for_input()
507
+	{
508
+		return $this->_form_html_filter
509
+			? $this->_form_html_filter->filterHtml(
510
+				$this->_get_display_strategy()->display(),
511
+				$this
512
+			)
513
+			: $this->_get_display_strategy()->display();
514
+	}
515
+
516
+
517
+
518
+	/**
519
+	 * @return string
520
+	 */
521
+	public function html_other_attributes()
522
+	{
523
+		return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : '';
524
+	}
525
+
526
+
527
+
528
+	/**
529
+	 * @param string $html_other_attributes
530
+	 */
531
+	public function set_html_other_attributes($html_other_attributes)
532
+	{
533
+		$this->_html_other_attributes = $html_other_attributes;
534
+	}
535
+
536
+
537
+
538
+	/**
539
+	 * Gets the HTML for displaying the label for this form input
540
+	 * according to the form section's layout strategy
541
+	 *
542
+	 * @return string
543
+	 */
544
+	public function get_html_for_label()
545
+	{
546
+		return $this->_parent_section->get_layout_strategy()->display_label($this);
547
+	}
548
+
549
+
550
+
551
+	/**
552
+	 * Gets the HTML for displaying the errors section for this form input
553
+	 * according to the form section's layout strategy
554
+	 *
555
+	 * @return string
556
+	 */
557
+	public function get_html_for_errors()
558
+	{
559
+		return $this->_parent_section->get_layout_strategy()->display_errors($this);
560
+	}
561
+
562
+
563
+
564
+	/**
565
+	 * Gets the HTML for displaying the help text for this form input
566
+	 * according to the form section's layout strategy
567
+	 *
568
+	 * @return string
569
+	 */
570
+	public function get_html_for_help()
571
+	{
572
+		return $this->_parent_section->get_layout_strategy()->display_help_text($this);
573
+	}
574
+
575
+
576
+
577
+	/**
578
+	 * Validates the input's sanitized value (assumes _sanitize() has already been called)
579
+	 * and returns whether or not the form input's submitted value is value
580
+	 *
581
+	 * @return boolean
582
+	 */
583
+	protected function _validate()
584
+	{
585
+		foreach ($this->_validation_strategies as $validation_strategy) {
586
+			if ($validation_strategy instanceof EE_Validation_Strategy_Base) {
587
+				try {
588
+					$validation_strategy->validate($this->normalized_value());
589
+				} catch (EE_Validation_Error $e) {
590
+					$this->add_validation_error($e);
591
+				}
592
+			}
593
+		}
594
+		if ($this->get_validation_errors()) {
595
+			return false;
596
+		} else {
597
+			return true;
598
+		}
599
+	}
600
+
601
+
602
+
603
+	/**
604
+	 * Performs basic sanitization on this value. But what sanitization can be performed anyways?
605
+	 * This value MIGHT be allowed to have tags, so we can't really remove them.
606
+	 *
607
+	 * @param string $value
608
+	 * @return null|string
609
+	 */
610
+	private function _sanitize($value)
611
+	{
612
+		return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null;
613
+	}
614
+
615
+
616
+
617
+	/**
618
+	 * Picks out the form value that relates to this form input,
619
+	 * and stores it as the sanitized value on the form input, and sets the normalized value.
620
+	 * Returns whether or not any validation errors occurred
621
+	 *
622
+	 * @param array $req_data like $_POST
623
+	 * @return boolean whether or not there was an error
624
+	 * @throws \EE_Error
625
+	 */
626
+	protected function _normalize($req_data)
627
+	{
628
+		//any existing validation errors don't apply so clear them
629
+		$this->_validation_errors = array();
630
+		try {
631
+			$raw_input = $this->find_form_data_for_this_section($req_data);
632
+			//super simple sanitization for now
633
+			if (is_array($raw_input)) {
634
+				$raw_value = array();
635
+				foreach ($raw_input as $key => $value) {
636
+					$raw_value[$key] = $this->_sanitize($value);
637
+				}
638
+				$this->_set_raw_value($raw_value);
639
+			} else {
640
+				$this->_set_raw_value($this->_sanitize($raw_input));
641
+			}
642
+			//we want to mostly leave the input alone in case we need to re-display it to the user
643
+			$this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value()));
644
+		} catch (EE_Validation_Error $e) {
645
+			$this->add_validation_error($e);
646
+		}
647
+	}
648
+
649
+
650
+
651
+	/**
652
+	 * @return string
653
+	 */
654
+	public function html_name()
655
+	{
656
+		$this->_set_default_html_name_if_empty();
657
+		return $this->_html_name;
658
+	}
659
+
660
+
661
+
662
+	/**
663
+	 * @return string
664
+	 */
665
+	public function html_label_id()
666
+	{
667
+		return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl';
668
+	}
669
+
670
+
671
+
672
+	/**
673
+	 * @return string
674
+	 */
675
+	public function html_label_class()
676
+	{
677
+		return $this->_html_label_class;
678
+	}
679
+
680
+
681
+
682
+	/**
683
+	 * @return string
684
+	 */
685
+	public function html_label_style()
686
+	{
687
+		return $this->_html_label_style;
688
+	}
689
+
690
+
691
+
692
+	/**
693
+	 * @return string
694
+	 */
695
+	public function html_label_text()
696
+	{
697
+		return $this->_html_label_text;
698
+	}
699
+
700
+
701
+
702
+	/**
703
+	 * @return string
704
+	 */
705
+	public function html_help_text()
706
+	{
707
+		return $this->_html_help_text;
708
+	}
709
+
710
+
711
+
712
+	/**
713
+	 * @return string
714
+	 */
715
+	public function html_help_class()
716
+	{
717
+		return $this->_html_help_class;
718
+	}
719
+
720
+
721
+
722
+	/**
723
+	 * @return string
724
+	 */
725
+	public function html_help_style()
726
+	{
727
+		return $this->_html_style;
728
+	}
729
+
730
+
731
+
732
+	/**
733
+	 * returns the raw, UNSAFE, input, almost exactly as the user submitted it.
734
+	 * Please note that almost all client code should instead use the normalized_value;
735
+	 * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag,
736
+	 * mostly by escaping quotes)
737
+	 * Note, we do not store the exact original value sent in the user's request because
738
+	 * it may have malicious content, and we MIGHT want to store the form input in a transient or something...
739
+	 * in which case, we would have stored the malicious content to our database.
740
+	 *
741
+	 * @return string
742
+	 */
743
+	public function raw_value()
744
+	{
745
+		return $this->_raw_value;
746
+	}
747
+
748
+
749
+
750
+	/**
751
+	 * Returns a string safe to usage in form inputs when displaying, because
752
+	 * it escapes all html entities
753
+	 *
754
+	 * @return string
755
+	 */
756
+	public function raw_value_in_form()
757
+	{
758
+		return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8');
759
+	}
760
+
761
+
762
+
763
+	/**
764
+	 * returns the value after it's been sanitized, and then converted into it's proper type
765
+	 * in PHP. Eg, a string, an int, an array,
766
+	 *
767
+	 * @return mixed
768
+	 */
769
+	public function normalized_value()
770
+	{
771
+		return $this->_normalized_value;
772
+	}
773
+
774
+
775
+
776
+	/**
777
+	 * Returns the normalized value is a presentable way. By default this is just
778
+	 * the normalized value by itself, but it can be overridden for when that's not
779
+	 * the best thing to display
780
+	 *
781
+	 * @return string
782
+	 */
783
+	public function pretty_value()
784
+	{
785
+		return $this->_normalized_value;
786
+	}
787
+
788
+
789
+
790
+	/**
791
+	 * When generating the JS for the jquery validation rules like<br>
792
+	 * <code>$( "#myform" ).validate({
793
+	 * rules: {
794
+	 * password: "required",
795
+	 * password_again: {
796
+	 * equalTo: "#password"
797
+	 * }
798
+	 * }
799
+	 * });</code>
800
+	 * if this field had the name 'password_again', it should return
801
+	 * <br><code>password_again: {
802
+	 * equalTo: "#password"
803
+	 * }</code>
804
+	 *
805
+	 * @return array
806
+	 */
807
+	public function get_jquery_validation_rules()
808
+	{
809
+		$jquery_validation_js = array();
810
+		$jquery_validation_rules = array();
811
+		foreach ($this->get_validation_strategies() as $validation_strategy) {
812
+			$jquery_validation_rules = array_replace_recursive(
813
+				$jquery_validation_rules,
814
+				$validation_strategy->get_jquery_validation_rule_array()
815
+			);
816
+		}
817
+		if (! empty($jquery_validation_rules)) {
818
+			foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) {
819
+				$jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules;
820
+			}
821
+		}
822
+		return $jquery_validation_js;
823
+	}
824
+
825
+
826
+
827
+	/**
828
+	 * Sets the input's default value for use in displaying in the form. Note: value should be
829
+	 * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0')
830
+	 *
831
+	 * @param mixed $value
832
+	 * @return void
833
+	 */
834
+	public function set_default($value)
835
+	{
836
+		$this->_default = $value;
837
+		$this->_set_normalized_value($value);
838
+		$this->_set_raw_value($value);
839
+	}
840
+
841
+
842
+
843
+	/**
844
+	 * Sets the normalized value on this input
845
+	 *
846
+	 * @param mixed $value
847
+	 */
848
+	protected function _set_normalized_value($value)
849
+	{
850
+		$this->_normalized_value = $value;
851
+	}
852
+
853
+
854
+
855
+	/**
856
+	 * Sets the raw value on this input (ie, exactly as the user submitted it)
857
+	 *
858
+	 * @param mixed $value
859
+	 */
860
+	protected function _set_raw_value($value)
861
+	{
862
+		$this->_raw_value = $this->_normalization_strategy->unnormalize($value);
863
+	}
864
+
865
+
866
+
867
+	/**
868
+	 * Sets the HTML label text after it has already been defined
869
+	 *
870
+	 * @param string $label
871
+	 * @return void
872
+	 */
873
+	public function set_html_label_text($label)
874
+	{
875
+		$this->_html_label_text = $label;
876
+	}
877
+
878
+
879
+
880
+	/**
881
+	 * Sets whether or not this field is required, and adjusts the validation strategy.
882
+	 * If you want to use the EE_Conditionally_Required_Validation_Strategy,
883
+	 * please add it as a validation strategy using add_validation_strategy as normal
884
+	 *
885
+	 * @param boolean $required boolean
886
+	 * @param null    $required_text
887
+	 */
888
+	public function set_required($required = true, $required_text = null)
889
+	{
890
+		$required = filter_var($required, FILTER_VALIDATE_BOOLEAN);
891
+		//whether $required is a string or a boolean, we want to add a required validation strategy
892
+		if ($required) {
893
+			$this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text));
894
+		} else {
895
+			$this->remove_validation_strategy('EE_Required_Validation_Strategy');
896
+		}
897
+		$this->_required = $required;
898
+	}
899
+
900
+
901
+
902
+	/**
903
+	 * Returns whether or not this field is required
904
+	 *
905
+	 * @return boolean
906
+	 */
907
+	public function required()
908
+	{
909
+		return $this->_required;
910
+	}
911
+
912
+
913
+
914
+	/**
915
+	 * @param string $required_css_class
916
+	 */
917
+	public function set_required_css_class($required_css_class)
918
+	{
919
+		$this->_required_css_class = $required_css_class;
920
+	}
921
+
922
+
923
+
924
+	/**
925
+	 * @return string
926
+	 */
927
+	public function required_css_class()
928
+	{
929
+		return $this->_required_css_class;
930
+	}
931
+
932
+
933
+
934
+	/**
935
+	 * @param bool $add_required
936
+	 * @return string
937
+	 */
938
+	public function html_class($add_required = false)
939
+	{
940
+		return $add_required && $this->required()
941
+			? $this->required_css_class() . ' ' . $this->_html_class
942
+			: $this->_html_class;
943
+	}
944
+
945
+
946
+	/**
947
+	 * Sets the help text, in case
948
+	 *
949
+	 * @param string $text
950
+	 */
951
+	public function set_html_help_text($text)
952
+	{
953
+		$this->_html_help_text = $text;
954
+	}
955
+
956
+
957
+
958
+	/**
959
+	 * Uses the sensitive data removal strategy to remove the sensitive data from this
960
+	 * input. If there is any kind of sensitive data removal on this input, we clear
961
+	 * out the raw value completely
962
+	 *
963
+	 * @return void
964
+	 */
965
+	public function clean_sensitive_data()
966
+	{
967
+		//if we do ANY kind of sensitive data removal on this, then just clear out the raw value
968
+		//if we need more logic than this we'll make a strategy for it
969
+		if ($this->_sensitive_data_removal_strategy
970
+			&& ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal
971
+		) {
972
+			$this->_set_raw_value(null);
973
+		}
974
+		//and clean the normalized value according to the appropriate strategy
975
+		$this->_set_normalized_value(
976
+			$this->get_sensitive_data_removal_strategy()->remove_sensitive_data(
977
+				$this->_normalized_value
978
+			)
979
+		);
980
+	}
981
+
982
+
983
+
984
+	/**
985
+	 * @param bool   $primary
986
+	 * @param string $button_size
987
+	 * @param string $other_attributes
988
+	 */
989
+	public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '')
990
+	{
991
+		$button_css_attributes = 'button';
992
+		$button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary';
993
+		switch ($button_size) {
994
+			case 'xs' :
995
+			case 'extra-small' :
996
+				$button_css_attributes .= ' button-xs';
997
+				break;
998
+			case 'sm' :
999
+			case 'small' :
1000
+				$button_css_attributes .= ' button-sm';
1001
+				break;
1002
+			case 'lg' :
1003
+			case 'large' :
1004
+				$button_css_attributes .= ' button-lg';
1005
+				break;
1006
+			case 'block' :
1007
+				$button_css_attributes .= ' button-block';
1008
+				break;
1009
+			case 'md' :
1010
+			case 'medium' :
1011
+			default :
1012
+				$button_css_attributes .= '';
1013
+		}
1014
+		$this->_button_css_attributes .= ! empty($other_attributes)
1015
+			? $button_css_attributes . ' ' . $other_attributes
1016
+			: $button_css_attributes;
1017
+	}
1018
+
1019
+
1020
+
1021
+	/**
1022
+	 * @return string
1023
+	 */
1024
+	public function button_css_attributes()
1025
+	{
1026
+		if (empty($this->_button_css_attributes)) {
1027
+			$this->set_button_css_attributes();
1028
+		}
1029
+		return $this->_button_css_attributes;
1030
+	}
1031
+
1032
+
1033
+
1034
+	/**
1035
+	 * find_form_data_for_this_section
1036
+	 * using this section's name and its parents, finds the value of the form data that corresponds to it.
1037
+	 * For example, if this form section's HTML name is my_form[subform][form_input_1],
1038
+	 * then it's value should be in $_REQUEST at $_REQUEST['my_form']['subform']['form_input_1'].
1039
+	 * (If that doesn't exist, we also check for this subsection's name
1040
+	 * at the TOP LEVEL of the request data. Eg $_REQUEST['form_input_1'].)
1041
+	 * This function finds its value in the form.
1042
+	 *
1043
+	 * @param array $req_data
1044
+	 * @return mixed whatever the raw value of this form section is in the request data
1045
+	 * @throws \EE_Error
1046
+	 */
1047
+	public function find_form_data_for_this_section($req_data)
1048
+	{
1049
+		// break up the html name by "[]"
1050
+		if (strpos($this->html_name(), '[') !== false) {
1051
+			$before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '['));
1052
+		} else {
1053
+			$before_any_brackets = $this->html_name();
1054
+		}
1055
+		// grab all of the segments
1056
+		preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches);
1057
+		if (isset($matches[1]) && is_array($matches[1])) {
1058
+			$name_parts = $matches[1];
1059
+			array_unshift($name_parts, $before_any_brackets);
1060
+		} else {
1061
+			$name_parts = array($before_any_brackets);
1062
+		}
1063
+		// now get the value for the input
1064
+		$value = $this->_find_form_data_for_this_section_using_name_parts($name_parts, $req_data);
1065
+		// check if this thing's name is at the TOP level of the request data
1066
+		if ($value === null && isset($req_data[$this->name()])) {
1067
+			$value = $req_data[$this->name()];
1068
+		}
1069
+		return $value;
1070
+	}
1071
+
1072
+
1073
+
1074
+	/**
1075
+	 * @param array $html_name_parts
1076
+	 * @param array $req_data
1077
+	 * @return array | NULL
1078
+	 */
1079
+	public function _find_form_data_for_this_section_using_name_parts($html_name_parts, $req_data)
1080
+	{
1081
+		$first_part_to_consider = array_shift($html_name_parts);
1082
+		if (isset($req_data[$first_part_to_consider])) {
1083
+			if (empty($html_name_parts)) {
1084
+				return $req_data[$first_part_to_consider];
1085
+			} else {
1086
+				return $this->_find_form_data_for_this_section_using_name_parts(
1087
+					$html_name_parts,
1088
+					$req_data[$first_part_to_consider]
1089
+				);
1090
+			}
1091
+		} else {
1092
+			return null;
1093
+		}
1094
+	}
1095
+
1096
+
1097
+
1098
+	/**
1099
+	 * Checks if this form input's data is in the request data
1100
+	 *
1101
+	 * @param array $req_data like $_POST
1102
+	 * @return boolean
1103
+	 * @throws \EE_Error
1104
+	 */
1105
+	public function form_data_present_in($req_data = null)
1106
+	{
1107
+		if ($req_data === null) {
1108
+			$req_data = $_POST;
1109
+		}
1110
+		$checked_value = $this->find_form_data_for_this_section($req_data);
1111
+		if ($checked_value !== null) {
1112
+			return true;
1113
+		} else {
1114
+			return false;
1115
+		}
1116
+	}
1117
+
1118
+
1119
+
1120
+	/**
1121
+	 * Overrides parent to add js data from validation and display strategies
1122
+	 *
1123
+	 * @param array $form_other_js_data
1124
+	 * @return array
1125
+	 */
1126
+	public function get_other_js_data($form_other_js_data = array())
1127
+	{
1128
+		$form_other_js_data = $this->get_other_js_data_from_strategies($form_other_js_data);
1129
+		return $form_other_js_data;
1130
+	}
1131
+
1132
+
1133
+
1134
+	/**
1135
+	 * Gets other JS data for localization from this input's strategies, like
1136
+	 * the validation strategies and the display strategy
1137
+	 *
1138
+	 * @param array $form_other_js_data
1139
+	 * @return array
1140
+	 */
1141
+	public function get_other_js_data_from_strategies($form_other_js_data = array())
1142
+	{
1143
+		$form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data);
1144
+		foreach ($this->get_validation_strategies() as $validation_strategy) {
1145
+			$form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data);
1146
+		}
1147
+		return $form_other_js_data;
1148
+	}
1149
+
1150
+
1151
+
1152
+	/**
1153
+	 * Override parent because we want to give our strategies an opportunity to enqueue some js and css
1154
+	 *
1155
+	 * @return void
1156
+	 */
1157
+	public function enqueue_js()
1158
+	{
1159
+		//ask our display strategy and validation strategies if they have js to enqueue
1160
+		$this->enqueue_js_from_strategies();
1161
+	}
1162
+
1163
+
1164
+
1165
+	/**
1166
+	 * Tells strategies when its ok to enqueue their js and css
1167
+	 *
1168
+	 * @return void
1169
+	 */
1170
+	public function enqueue_js_from_strategies()
1171
+	{
1172
+		$this->get_display_strategy()->enqueue_js();
1173
+		foreach ($this->get_validation_strategies() as $validation_strategy) {
1174
+			$validation_strategy->enqueue_js();
1175
+		}
1176
+	}
1177
+
1178
+
1179
+
1180
+	/**
1181
+	 * Gets the default value set on the input (not the current value, which may have been
1182
+	 * changed because of a form submission). If no default was set, this us null.
1183
+	 * @return mixed
1184
+	 */
1185
+	public function get_default()
1186
+	{
1187
+		return $this->_default;
1188
+	}
1189 1189
 }
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@  discard block
 block discarded – undo
1
-<?php if (! defined('EVENT_ESPRESSO_VERSION')) {
1
+<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) {
2 2
     exit('No direct script access allowed');
3 3
 }
4 4
 
@@ -191,23 +191,23 @@  discard block
 block discarded – undo
191 191
      */
192 192
     public function __construct($input_args = array())
193 193
     {
194
-        $input_args = (array)apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this);
194
+        $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this);
195 195
         // the following properties must be cast as arrays
196 196
         if (isset($input_args['validation_strategies'])) {
197
-            foreach ((array)$input_args['validation_strategies'] as $validation_strategy) {
197
+            foreach ((array) $input_args['validation_strategies'] as $validation_strategy) {
198 198
                 if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) {
199 199
                     $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy;
200 200
                 }
201 201
             }
202 202
             unset($input_args['validation_strategies']);
203 203
         }
204
-        if(isset($input_args['ignore_input'])) {
204
+        if (isset($input_args['ignore_input'])) {
205 205
             $this->_validation_strategies = array();
206 206
         }
207 207
         // loop thru incoming options
208 208
         foreach ($input_args as $key => $value) {
209 209
             // add underscore to $key to match property names
210
-            $_key = '_' . $key;
210
+            $_key = '_'.$key;
211 211
             if (property_exists($this, $_key)) {
212 212
                 $this->{$_key} = $value;
213 213
             }
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
         if (isset($input_args['ignore_input'])) {
227 227
             $this->_normalization_strategy = new EE_Null_Normalization();
228 228
         }
229
-        if (! $this->_normalization_strategy) {
229
+        if ( ! $this->_normalization_strategy) {
230 230
                 $this->_normalization_strategy = new EE_Text_Normalization();
231 231
         }
232 232
         $this->_normalization_strategy->_construct_finalize($this);
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
             $this->set_default($input_args['default']);
236 236
             unset($input_args['default']);
237 237
         }
238
-        if (! $this->_sensitive_data_removal_strategy) {
238
+        if ( ! $this->_sensitive_data_removal_strategy) {
239 239
             $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal();
240 240
         }
241 241
         $this->_sensitive_data_removal_strategy->_construct_finalize($this);
@@ -252,10 +252,10 @@  discard block
 block discarded – undo
252 252
      */
253 253
     protected function _set_default_html_name_if_empty()
254 254
     {
255
-        if (! $this->_html_name) {
255
+        if ( ! $this->_html_name) {
256 256
             $this->_html_name = $this->name();
257 257
             if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) {
258
-                $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]";
258
+                $this->_html_name = $this->_parent_section->html_name_prefix()."[{$this->name()}]";
259 259
             }
260 260
         }
261 261
     }
@@ -287,7 +287,7 @@  discard block
 block discarded – undo
287 287
     protected function _get_display_strategy()
288 288
     {
289 289
         $this->ensure_construct_finalized_called();
290
-        if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) {
290
+        if ( ! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) {
291 291
             throw new EE_Error(
292 292
                 sprintf(
293 293
                     __(
@@ -520,7 +520,7 @@  discard block
 block discarded – undo
520 520
      */
521 521
     public function html_other_attributes()
522 522
     {
523
-        return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : '';
523
+        return ! empty($this->_html_other_attributes) ? ' '.$this->_html_other_attributes : '';
524 524
     }
525 525
 
526 526
 
@@ -664,7 +664,7 @@  discard block
 block discarded – undo
664 664
      */
665 665
     public function html_label_id()
666 666
     {
667
-        return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl';
667
+        return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id().'-lbl';
668 668
     }
669 669
 
670 670
 
@@ -814,7 +814,7 @@  discard block
 block discarded – undo
814 814
                 $validation_strategy->get_jquery_validation_rule_array()
815 815
             );
816 816
         }
817
-        if (! empty($jquery_validation_rules)) {
817
+        if ( ! empty($jquery_validation_rules)) {
818 818
             foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) {
819 819
                 $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules;
820 820
             }
@@ -938,7 +938,7 @@  discard block
 block discarded – undo
938 938
     public function html_class($add_required = false)
939 939
     {
940 940
         return $add_required && $this->required()
941
-            ? $this->required_css_class() . ' ' . $this->_html_class
941
+            ? $this->required_css_class().' '.$this->_html_class
942 942
             : $this->_html_class;
943 943
     }
944 944
 
@@ -1012,7 +1012,7 @@  discard block
 block discarded – undo
1012 1012
                 $button_css_attributes .= '';
1013 1013
         }
1014 1014
         $this->_button_css_attributes .= ! empty($other_attributes)
1015
-            ? $button_css_attributes . ' ' . $other_attributes
1015
+            ? $button_css_attributes.' '.$other_attributes
1016 1016
             : $button_css_attributes;
1017 1017
     }
1018 1018
 
Please login to merge, or discard this patch.
core/libraries/form_sections/base/EE_Form_Section_Proper.form.php 3 patches
Doc Comments   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -459,7 +459,7 @@  discard block
 block discarded – undo
459 459
      *                                                      with construction finalize being called later
460 460
      *                                                      (realizing that the subsections' html names
461 461
      *                                                      might not be set yet, etc.)
462
-     * @return EE_Form_Section_Base
462
+     * @return EE_Form_Section_Validatable|null
463 463
      * @throws EE_Error
464 464
      */
465 465
     public function get_subsection($name, $require_construction_to_be_finalized = true)
@@ -1289,7 +1289,6 @@  discard block
 block discarded – undo
1289 1289
     /**
1290 1290
      * Sets the submission error message (aka validation error message for this form section and all sub-sections)
1291 1291
      * @param string                           $form_submission_error_message
1292
-     * @param EE_Form_Section_Validatable $form_section unused
1293 1292
      * @throws EE_Error
1294 1293
      */
1295 1294
     public function set_submission_error_message(
Please login to merge, or discard this patch.
Spacing   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -111,8 +111,8 @@  discard block
 block discarded – undo
111 111
             //AND we are going to make sure they're in that specified order
112 112
             $reordered_subsections = array();
113 113
             foreach ($options_array['include'] as $input_name) {
114
-                if (isset($this->_subsections[ $input_name ])) {
115
-                    $reordered_subsections[ $input_name ] = $this->_subsections[ $input_name ];
114
+                if (isset($this->_subsections[$input_name])) {
115
+                    $reordered_subsections[$input_name] = $this->_subsections[$input_name];
116 116
                 }
117 117
             }
118 118
             $this->_subsections = $reordered_subsections;
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
         if (isset($options_array['layout_strategy'])) {
125 125
             $this->_layout_strategy = $options_array['layout_strategy'];
126 126
         }
127
-        if (! $this->_layout_strategy) {
127
+        if ( ! $this->_layout_strategy) {
128 128
             $this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout();
129 129
         }
130 130
         $this->_layout_strategy->_construct_finalize($this);
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
                 $req_data,
278 278
                 $this
279 279
             );
280
-            $this->cached_request_data = (array)$req_data;
280
+            $this->cached_request_data = (array) $req_data;
281 281
         }
282 282
         return $this->cached_request_data;
283 283
     }
@@ -313,7 +313,7 @@  discard block
 block discarded – undo
313 313
         if ($validate) {
314 314
             $this->_validate();
315 315
             //if it's invalid, we're going to want to re-display so remember what they submitted
316
-            if (! $this->is_valid()) {
316
+            if ( ! $this->is_valid()) {
317 317
                 $this->store_submitted_form_data_in_session();
318 318
             }
319 319
         }
@@ -426,11 +426,11 @@  discard block
 block discarded – undo
426 426
     public function populate_defaults($default_data)
427 427
     {
428 428
         foreach ($this->subsections(false) as $subsection_name => $subsection) {
429
-            if (isset($default_data[ $subsection_name ])) {
429
+            if (isset($default_data[$subsection_name])) {
430 430
                 if ($subsection instanceof EE_Form_Input_Base) {
431
-                    $subsection->set_default($default_data[ $subsection_name ]);
431
+                    $subsection->set_default($default_data[$subsection_name]);
432 432
                 } elseif ($subsection instanceof EE_Form_Section_Proper) {
433
-                    $subsection->populate_defaults($default_data[ $subsection_name ]);
433
+                    $subsection->populate_defaults($default_data[$subsection_name]);
434 434
                 }
435 435
             }
436 436
         }
@@ -445,7 +445,7 @@  discard block
 block discarded – undo
445 445
      */
446 446
     public function subsection_exists($name)
447 447
     {
448
-        return isset($this->_subsections[ $name ]) ? true : false;
448
+        return isset($this->_subsections[$name]) ? true : false;
449 449
     }
450 450
 
451 451
 
@@ -467,7 +467,7 @@  discard block
 block discarded – undo
467 467
         if ($require_construction_to_be_finalized) {
468 468
             $this->ensure_construct_finalized_called();
469 469
         }
470
-        return $this->subsection_exists($name) ? $this->_subsections[ $name ] : null;
470
+        return $this->subsection_exists($name) ? $this->_subsections[$name] : null;
471 471
     }
472 472
 
473 473
 
@@ -482,7 +482,7 @@  discard block
 block discarded – undo
482 482
         $validatable_subsections = array();
483 483
         foreach ($this->subsections() as $name => $obj) {
484 484
             if ($obj instanceof EE_Form_Section_Validatable) {
485
-                $validatable_subsections[ $name ] = $obj;
485
+                $validatable_subsections[$name] = $obj;
486 486
             }
487 487
         }
488 488
         return $validatable_subsections;
@@ -509,7 +509,7 @@  discard block
 block discarded – undo
509 509
             $name,
510 510
             $require_construction_to_be_finalized
511 511
         );
512
-        if (! $subsection instanceof EE_Form_Input_Base) {
512
+        if ( ! $subsection instanceof EE_Form_Input_Base) {
513 513
             throw new EE_Error(
514 514
                 sprintf(
515 515
                     esc_html__(
@@ -546,7 +546,7 @@  discard block
 block discarded – undo
546 546
             $name,
547 547
             $require_construction_to_be_finalized
548 548
         );
549
-        if (! $subsection instanceof EE_Form_Section_Proper) {
549
+        if ( ! $subsection instanceof EE_Form_Section_Proper) {
550 550
             throw new EE_Error(
551 551
                 sprintf(
552 552
                     esc_html__(
@@ -585,8 +585,8 @@  discard block
 block discarded – undo
585 585
      */
586 586
     public function is_valid()
587 587
     {
588
-        if($this->is_valid === null) {
589
-            if (! $this->has_received_submission()) {
588
+        if ($this->is_valid === null) {
589
+            if ( ! $this->has_received_submission()) {
590 590
                 throw new EE_Error(
591 591
                     sprintf(
592 592
                         esc_html__(
@@ -596,14 +596,14 @@  discard block
 block discarded – undo
596 596
                     )
597 597
                 );
598 598
             }
599
-            if (! parent::is_valid()) {
599
+            if ( ! parent::is_valid()) {
600 600
                 $this->is_valid = false;
601 601
             } else {
602 602
                 // ok so no general errors to this entire form section.
603 603
                 // so let's check the subsections, but only set errors if that hasn't been done yet
604 604
                 $this->is_valid = true;
605 605
                 foreach ($this->get_validatable_subsections() as $subsection) {
606
-                    if (! $subsection->is_valid()) {
606
+                    if ( ! $subsection->is_valid()) {
607 607
                         $this->is_valid = false;
608 608
                     }
609 609
                 }
@@ -620,7 +620,7 @@  discard block
 block discarded – undo
620 620
      */
621 621
     protected function _set_default_name_if_empty()
622 622
     {
623
-        if (! $this->_name) {
623
+        if ( ! $this->_name) {
624 624
             $classname    = get_class($this);
625 625
             $default_name = str_replace('EE_', '', $classname);
626 626
             $this->_name  = $default_name;
@@ -710,7 +710,7 @@  discard block
 block discarded – undo
710 710
     {
711 711
         wp_register_script(
712 712
             'ee_form_section_validation',
713
-            EE_GLOBAL_ASSETS_URL . 'scripts' . DS . 'form_section_validation.js',
713
+            EE_GLOBAL_ASSETS_URL.'scripts'.DS.'form_section_validation.js',
714 714
             array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'),
715 715
             EVENT_ESPRESSO_VERSION,
716 716
             true
@@ -754,13 +754,13 @@  discard block
 block discarded – undo
754 754
         // we only want to localize vars ONCE for the entire form,
755 755
         // so if the form section doesn't have a parent, then it must be the top dog
756 756
         if ($return_for_subsection || ! $this->parent_section()) {
757
-            EE_Form_Section_Proper::$_js_localization['form_data'][ $this->html_id() ] = array(
757
+            EE_Form_Section_Proper::$_js_localization['form_data'][$this->html_id()] = array(
758 758
                 'form_section_id'  => $this->html_id(true),
759 759
                 'validation_rules' => $this->get_jquery_validation_rules(),
760 760
                 'other_data'       => $this->get_other_js_data(),
761 761
                 'errors'           => $this->subsection_validation_errors_by_html_name(),
762 762
             );
763
-            EE_Form_Section_Proper::$_scripts_localized                                = true;
763
+            EE_Form_Section_Proper::$_scripts_localized = true;
764 764
         }
765 765
     }
766 766
 
@@ -795,7 +795,7 @@  discard block
 block discarded – undo
795 795
         $inputs = array();
796 796
         foreach ($this->subsections() as $subsection) {
797 797
             if ($subsection instanceof EE_Form_Input_Base) {
798
-                $inputs[ $subsection->html_name() ] = $subsection;
798
+                $inputs[$subsection->html_name()] = $subsection;
799 799
             } elseif ($subsection instanceof EE_Form_Section_Proper) {
800 800
                 $inputs += $subsection->inputs_in_subsections();
801 801
             }
@@ -818,7 +818,7 @@  discard block
 block discarded – undo
818 818
         $errors = array();
819 819
         foreach ($inputs as $form_input) {
820 820
             if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) {
821
-                $errors[ $form_input->html_name() ] = $form_input->get_validation_error_string();
821
+                $errors[$form_input->html_name()] = $form_input->get_validation_error_string();
822 822
             }
823 823
         }
824 824
         return $errors;
@@ -841,7 +841,7 @@  discard block
 block discarded – undo
841 841
         $email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level)
842 842
             ? EE_Registry::instance()->CFG->registration->email_validation_level
843 843
             : 'wp_default';
844
-        EE_Form_Section_Proper::$_js_localization['email_validation_level']   = $email_validation_level;
844
+        EE_Form_Section_Proper::$_js_localization['email_validation_level'] = $email_validation_level;
845 845
         wp_enqueue_script('ee_form_section_validation');
846 846
         wp_localize_script(
847 847
             'ee_form_section_validation',
@@ -858,7 +858,7 @@  discard block
 block discarded – undo
858 858
      */
859 859
     public function ensure_scripts_localized()
860 860
     {
861
-        if (! EE_Form_Section_Proper::$_scripts_localized) {
861
+        if ( ! EE_Form_Section_Proper::$_scripts_localized) {
862 862
             $this->_enqueue_and_localize_form_js();
863 863
         }
864 864
     }
@@ -954,8 +954,8 @@  discard block
 block discarded – undo
954 954
         //reset the cache of whether this form is valid or not- we're re-validating it now
955 955
         $this->is_valid = null;
956 956
         foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) {
957
-            if (method_exists($this, '_validate_' . $subsection_name)) {
958
-                call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection));
957
+            if (method_exists($this, '_validate_'.$subsection_name)) {
958
+                call_user_func_array(array($this, '_validate_'.$subsection_name), array($subsection));
959 959
             }
960 960
             $subsection->_validate();
961 961
         }
@@ -973,9 +973,9 @@  discard block
 block discarded – undo
973 973
         $inputs = array();
974 974
         foreach ($this->subsections() as $subsection_name => $subsection) {
975 975
             if ($subsection instanceof EE_Form_Section_Proper) {
976
-                $inputs[ $subsection_name ] = $subsection->valid_data();
976
+                $inputs[$subsection_name] = $subsection->valid_data();
977 977
             } elseif ($subsection instanceof EE_Form_Input_Base) {
978
-                $inputs[ $subsection_name ] = $subsection->normalized_value();
978
+                $inputs[$subsection_name] = $subsection->normalized_value();
979 979
             }
980 980
         }
981 981
         return $inputs;
@@ -993,7 +993,7 @@  discard block
 block discarded – undo
993 993
         $inputs = array();
994 994
         foreach ($this->subsections() as $subsection_name => $subsection) {
995 995
             if ($subsection instanceof EE_Form_Input_Base) {
996
-                $inputs[ $subsection_name ] = $subsection;
996
+                $inputs[$subsection_name] = $subsection;
997 997
             }
998 998
         }
999 999
         return $inputs;
@@ -1011,7 +1011,7 @@  discard block
 block discarded – undo
1011 1011
         $form_sections = array();
1012 1012
         foreach ($this->subsections() as $name => $obj) {
1013 1013
             if ($obj instanceof EE_Form_Section_Proper) {
1014
-                $form_sections[ $name ] = $obj;
1014
+                $form_sections[$name] = $obj;
1015 1015
             }
1016 1016
         }
1017 1017
         return $form_sections;
@@ -1118,7 +1118,7 @@  discard block
 block discarded – undo
1118 1118
         $input_values = array();
1119 1119
         foreach ($this->subsections() as $subsection_name => $subsection) {
1120 1120
             if ($subsection instanceof EE_Form_Input_Base) {
1121
-                $input_values[ $subsection_name ] = $pretty
1121
+                $input_values[$subsection_name] = $pretty
1122 1122
                     ? $subsection->pretty_value()
1123 1123
                     : $subsection->normalized_value();
1124 1124
             } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) {
@@ -1130,7 +1130,7 @@  discard block
 block discarded – undo
1130 1130
                 if ($flatten) {
1131 1131
                     $input_values = array_merge($input_values, $subform_input_values);
1132 1132
                 } else {
1133
-                    $input_values[ $subsection_name ] = $subform_input_values;
1133
+                    $input_values[$subsection_name] = $subform_input_values;
1134 1134
                 }
1135 1135
             }
1136 1136
         }
@@ -1158,7 +1158,7 @@  discard block
 block discarded – undo
1158 1158
             if ($subsection instanceof EE_Form_Input_Base) {
1159 1159
                 // is this input part of an array of inputs?
1160 1160
                 if (strpos($subsection->html_name(), '[') !== false) {
1161
-                    $full_input_name  = EEH_Array::convert_array_values_to_keys(
1161
+                    $full_input_name = EEH_Array::convert_array_values_to_keys(
1162 1162
                         explode(
1163 1163
                             '[',
1164 1164
                             str_replace(']', '', $subsection->html_name())
@@ -1167,7 +1167,7 @@  discard block
 block discarded – undo
1167 1167
                     );
1168 1168
                     $submitted_values = array_replace_recursive($submitted_values, $full_input_name);
1169 1169
                 } else {
1170
-                    $submitted_values[ $subsection->html_name() ] = $subsection->raw_value();
1170
+                    $submitted_values[$subsection->html_name()] = $subsection->raw_value();
1171 1171
                 }
1172 1172
             } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subforms) {
1173 1173
                 $subform_input_values = $subsection->submitted_values($include_subforms);
@@ -1202,7 +1202,7 @@  discard block
 block discarded – undo
1202 1202
     public function exclude(array $inputs_to_exclude = array())
1203 1203
     {
1204 1204
         foreach ($inputs_to_exclude as $input_to_exclude_name) {
1205
-            unset($this->_subsections[ $input_to_exclude_name ]);
1205
+            unset($this->_subsections[$input_to_exclude_name]);
1206 1206
         }
1207 1207
     }
1208 1208
 
@@ -1244,7 +1244,7 @@  discard block
 block discarded – undo
1244 1244
     public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true)
1245 1245
     {
1246 1246
         foreach ($new_subsections as $subsection_name => $subsection) {
1247
-            if (! $subsection instanceof EE_Form_Section_Base) {
1247
+            if ( ! $subsection instanceof EE_Form_Section_Base) {
1248 1248
                 EE_Error::add_error(
1249 1249
                     sprintf(
1250 1250
                         esc_html__(
@@ -1256,7 +1256,7 @@  discard block
 block discarded – undo
1256 1256
                         $this->name()
1257 1257
                     )
1258 1258
                 );
1259
-                unset($new_subsections[ $subsection_name ]);
1259
+                unset($new_subsections[$subsection_name]);
1260 1260
             }
1261 1261
         }
1262 1262
         $this->_subsections = EEH_Array::insert_into_array(
@@ -1348,7 +1348,7 @@  discard block
 block discarded – undo
1348 1348
     public function html_name_prefix()
1349 1349
     {
1350 1350
         if ($this->parent_section() instanceof EE_Form_Section_Proper) {
1351
-            return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']';
1351
+            return $this->parent_section()->html_name_prefix().'['.$this->name().']';
1352 1352
         }
1353 1353
         return $this->name();
1354 1354
     }
@@ -1388,7 +1388,7 @@  discard block
 block discarded – undo
1388 1388
      */
1389 1389
     public function ensure_construct_finalized_called()
1390 1390
     {
1391
-        if (! $this->_construction_finalized) {
1391
+        if ( ! $this->_construction_finalized) {
1392 1392
             $this->_construct_finalize($this->_parent_section, $this->_name);
1393 1393
         }
1394 1394
     }
@@ -1460,7 +1460,7 @@  discard block
 block discarded – undo
1460 1460
                 $form_section = $validation_error->get_form_section();
1461 1461
                 if ($form_section instanceof EE_Form_Input_Base) {
1462 1462
                    $label = $validation_error->get_form_section()->html_label_text();
1463
-                } elseif($form_section instanceof EE_Form_Section_Validatable) {
1463
+                } elseif ($form_section instanceof EE_Form_Section_Validatable) {
1464 1464
                     $label = $validation_error->get_form_section()->name();
1465 1465
                 } else {
1466 1466
                     $label = esc_html__('Unknown', 'event_espresso');
Please login to merge, or discard this patch.
Indentation   +1500 added lines, -1500 removed lines patch added patch discarded remove patch
@@ -14,1505 +14,1505 @@
 block discarded – undo
14 14
 class EE_Form_Section_Proper extends EE_Form_Section_Validatable
15 15
 {
16 16
 
17
-    const SUBMITTED_FORM_DATA_SSN_KEY = 'submitted_form_data';
18
-
19
-    /**
20
-     * Subsections
21
-     *
22
-     * @var EE_Form_Section_Validatable[]
23
-     */
24
-    protected $_subsections = array();
25
-
26
-    /**
27
-     * Strategy for laying out the form
28
-     *
29
-     * @var EE_Form_Section_Layout_Base
30
-     */
31
-    protected $_layout_strategy;
32
-
33
-    /**
34
-     * Whether or not this form has received and validated a form submission yet
35
-     *
36
-     * @var boolean
37
-     */
38
-    protected $_received_submission = false;
39
-
40
-    /**
41
-     * message displayed to users upon successful form submission
42
-     *
43
-     * @var string
44
-     */
45
-    protected $_form_submission_success_message = '';
46
-
47
-    /**
48
-     * message displayed to users upon unsuccessful form submission
49
-     *
50
-     * @var string
51
-     */
52
-    protected $_form_submission_error_message = '';
53
-
54
-    /**
55
-     * @var array like $_REQUEST
56
-     */
57
-    protected $cached_request_data;
58
-
59
-    /**
60
-     * Stores whether this form (and its sub-sections) were found to be valid or not.
61
-     * Starts off as null, but once the form is validated, it set to either true or false
62
-     * @var boolean|null
63
-     */
64
-    protected $is_valid;
65
-
66
-    /**
67
-     * Stores all the data that will localized for form validation
68
-     *
69
-     * @var array
70
-     */
71
-    static protected $_js_localization = array();
72
-
73
-    /**
74
-     * whether or not the form's localized validation JS vars have been set
75
-     *
76
-     * @type boolean
77
-     */
78
-    static protected $_scripts_localized = false;
79
-
80
-
81
-    /**
82
-     * when constructing a proper form section, calls _construct_finalize on children
83
-     * so that they know who their parent is, and what name they've been given.
84
-     *
85
-     * @param array[] $options_array   {
86
-     * @type          $subsections     EE_Form_Section_Validatable[] where keys are the section's name
87
-     * @type          $include         string[] numerically-indexed where values are section names to be included,
88
-     *                                 and in that order. This is handy if you want
89
-     *                                 the subsections to be ordered differently than the default, and if you override
90
-     *                                 which fields are shown
91
-     * @type          $exclude         string[] values are subsections to be excluded. This is handy if you want
92
-     *                                 to remove certain default subsections (note: if you specify BOTH 'include' AND
93
-     *                                 'exclude', the inclusions will be applied first, and the exclusions will exclude
94
-     *                                 items from that list of inclusions)
95
-     * @type          $layout_strategy EE_Form_Section_Layout_Base strategy for laying out the form
96
-     *                                 } @see EE_Form_Section_Validatable::__construct()
97
-     * @throws EE_Error
98
-     */
99
-    public function __construct($options_array = array())
100
-    {
101
-        $options_array = (array) apply_filters(
102
-            'FHEE__EE_Form_Section_Proper___construct__options_array',
103
-            $options_array,
104
-            $this
105
-        );
106
-        //call parent first, as it may be setting the name
107
-        parent::__construct($options_array);
108
-        //if they've included subsections in the constructor, add them now
109
-        if (isset($options_array['include'])) {
110
-            //we are going to make sure we ONLY have those subsections to include
111
-            //AND we are going to make sure they're in that specified order
112
-            $reordered_subsections = array();
113
-            foreach ($options_array['include'] as $input_name) {
114
-                if (isset($this->_subsections[ $input_name ])) {
115
-                    $reordered_subsections[ $input_name ] = $this->_subsections[ $input_name ];
116
-                }
117
-            }
118
-            $this->_subsections = $reordered_subsections;
119
-        }
120
-        if (isset($options_array['exclude'])) {
121
-            $exclude            = $options_array['exclude'];
122
-            $this->_subsections = array_diff_key($this->_subsections, array_flip($exclude));
123
-        }
124
-        if (isset($options_array['layout_strategy'])) {
125
-            $this->_layout_strategy = $options_array['layout_strategy'];
126
-        }
127
-        if (! $this->_layout_strategy) {
128
-            $this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout();
129
-        }
130
-        $this->_layout_strategy->_construct_finalize($this);
131
-        //ok so we are definitely going to want the forms JS,
132
-        //so enqueue it or remember to enqueue it during wp_enqueue_scripts
133
-        if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) {
134
-            //ok so they've constructed this object after when they should have.
135
-            //just enqueue the generic form scripts and initialize the form immediately in the JS
136
-            EE_Form_Section_Proper::wp_enqueue_scripts(true);
137
-        } else {
138
-            add_action('wp_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts'));
139
-            add_action('admin_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts'));
140
-        }
141
-        add_action('wp_footer', array($this, 'ensure_scripts_localized'), 1);
142
-        /**
143
-         * Gives other plugins a chance to hook in before construct finalize is called.
144
-         * The form probably doesn't yet have a parent form section.
145
-         * Since 4.9.32, when this action was introduced, this is the best place to add a subsection onto a form,
146
-         * assuming you don't care what the form section's name, HTML ID, or HTML name etc are.
147
-         * Also see AHEE__EE_Form_Section_Proper___construct_finalize__end
148
-         *
149
-         * @since 4.9.32
150
-         * @param EE_Form_Section_Proper $this          before __construct is done, but all of its logic,
151
-         *                                              except maybe calling _construct_finalize has been done
152
-         * @param array                  $options_array options passed into the constructor
153
-         */
154
-        do_action(
155
-            'AHEE__EE_Form_Input_Base___construct__before_construct_finalize_called',
156
-            $this,
157
-            $options_array
158
-        );
159
-        if (isset($options_array['name'])) {
160
-            $this->_construct_finalize(null, $options_array['name']);
161
-        }
162
-    }
163
-
164
-
165
-    /**
166
-     * Finishes construction given the parent form section and this form section's name
167
-     *
168
-     * @param EE_Form_Section_Proper $parent_form_section
169
-     * @param string                 $name
170
-     * @throws EE_Error
171
-     */
172
-    public function _construct_finalize($parent_form_section, $name)
173
-    {
174
-        parent::_construct_finalize($parent_form_section, $name);
175
-        $this->_set_default_name_if_empty();
176
-        $this->_set_default_html_id_if_empty();
177
-        foreach ($this->_subsections as $subsection_name => $subsection) {
178
-            if ($subsection instanceof EE_Form_Section_Base) {
179
-                $subsection->_construct_finalize($this, $subsection_name);
180
-            } else {
181
-                throw new EE_Error(
182
-                    sprintf(
183
-                        esc_html__(
184
-                            'Subsection "%s" is not an instanceof EE_Form_Section_Base on form "%s". It is a "%s"',
185
-                            'event_espresso'
186
-                        ),
187
-                        $subsection_name,
188
-                        get_class($this),
189
-                        $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso')
190
-                    )
191
-                );
192
-            }
193
-        }
194
-        /**
195
-         * Action performed just after form has been given a name (and HTML ID etc) and is fully constructed.
196
-         * If you have code that should modify the form and needs it and its subsections to have a name, HTML ID
197
-         * (or other attributes derived from the name like the HTML label id, etc), this is where it should be done.
198
-         * This might only happen just before displaying the form, or just before it receives form submission data.
199
-         * If you need to modify the form or its subsections before _construct_finalize is called on it (and we've
200
-         * ensured it has a name, HTML IDs, etc
201
-         *
202
-         * @param EE_Form_Section_Proper      $this
203
-         * @param EE_Form_Section_Proper|null $parent_form_section
204
-         * @param string                      $name
205
-         */
206
-        do_action(
207
-            'AHEE__EE_Form_Section_Proper___construct_finalize__end',
208
-            $this,
209
-            $parent_form_section,
210
-            $name
211
-        );
212
-    }
213
-
214
-
215
-    /**
216
-     * Gets the layout strategy for this form section
217
-     *
218
-     * @return EE_Form_Section_Layout_Base
219
-     */
220
-    public function get_layout_strategy()
221
-    {
222
-        return $this->_layout_strategy;
223
-    }
224
-
225
-
226
-    /**
227
-     * Gets the HTML for a single input for this form section according
228
-     * to the layout strategy
229
-     *
230
-     * @param EE_Form_Input_Base $input
231
-     * @return string
232
-     */
233
-    public function get_html_for_input($input)
234
-    {
235
-        return $this->_layout_strategy->layout_input($input);
236
-    }
237
-
238
-
239
-    /**
240
-     * was_submitted - checks if form inputs are present in request data
241
-     * Basically an alias for form_data_present_in() (which is used by both
242
-     * proper form sections and form inputs)
243
-     *
244
-     * @param null $form_data
245
-     * @return boolean
246
-     * @throws EE_Error
247
-     */
248
-    public function was_submitted($form_data = null)
249
-    {
250
-        return $this->form_data_present_in($form_data);
251
-    }
252
-
253
-    /**
254
-     * Gets the cached request data; but if there is none, or $req_data was set with
255
-     * something different, refresh the cache, and then return it
256
-     * @param null $req_data
257
-     * @return array
258
-     */
259
-    protected function getCachedRequest($req_data = null)
260
-    {
261
-        if ($this->cached_request_data === null
262
-            || (
263
-                $req_data !== null &&
264
-                $req_data !== $this->cached_request_data
265
-            )
266
-        ) {
267
-            $req_data = apply_filters(
268
-                'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data',
269
-                $req_data,
270
-                $this
271
-            );
272
-            if ($req_data === null) {
273
-                $req_data = array_merge($_GET, $_POST);
274
-            }
275
-            $req_data = apply_filters(
276
-                'FHEE__EE_Form_Section_Proper__receive_form_submission__request_data',
277
-                $req_data,
278
-                $this
279
-            );
280
-            $this->cached_request_data = (array)$req_data;
281
-        }
282
-        return $this->cached_request_data;
283
-    }
284
-
285
-
286
-    /**
287
-     * After the form section is initially created, call this to sanitize the data in the submission
288
-     * which relates to this form section, validate it, and set it as properties on the form.
289
-     *
290
-     * @param array|null $req_data should usually be $_POST (the default).
291
-     *                             However, you CAN supply a different array.
292
-     *                             Consider using set_defaults() instead however.
293
-     *                             (If you rendered the form in the page using echo $form_x->get_html()
294
-     *                             the inputs will have the correct name in the request data for this function
295
-     *                             to find them and populate the form with them.
296
-     *                             If you have a flat form (with only input subsections),
297
-     *                             you can supply a flat array where keys
298
-     *                             are the form input names and values are their values)
299
-     * @param boolean    $validate whether or not to perform validation on this data. Default is,
300
-     *                             of course, to validate that data, and set errors on the invalid values.
301
-     *                             But if the data has already been validated
302
-     *                             (eg you validated the data then stored it in the DB)
303
-     *                             you may want to skip this step.
304
-     * @throws InvalidArgumentException
305
-     * @throws InvalidInterfaceException
306
-     * @throws InvalidDataTypeException
307
-     * @throws EE_Error
308
-     */
309
-    public function receive_form_submission($req_data = null, $validate = true)
310
-    {
311
-        $req_data = $this->getCachedRequest($req_data);
312
-        $this->_normalize($req_data);
313
-        if ($validate) {
314
-            $this->_validate();
315
-            //if it's invalid, we're going to want to re-display so remember what they submitted
316
-            if (! $this->is_valid()) {
317
-                $this->store_submitted_form_data_in_session();
318
-            }
319
-        }
320
-        if ($this->submission_error_message() === '' && ! $this->is_valid()) {
321
-            $this->set_submission_error_message();
322
-        }
323
-        do_action(
324
-            'AHEE__EE_Form_Section_Proper__receive_form_submission__end',
325
-            $req_data,
326
-            $this,
327
-            $validate
328
-        );
329
-    }
330
-
331
-
332
-    /**
333
-     * caches the originally submitted input values in the session
334
-     * so that they can be used to repopulate the form if it failed validation
335
-     *
336
-     * @return boolean whether or not the data was successfully stored in the session
337
-     * @throws InvalidArgumentException
338
-     * @throws InvalidInterfaceException
339
-     * @throws InvalidDataTypeException
340
-     * @throws EE_Error
341
-     */
342
-    protected function store_submitted_form_data_in_session()
343
-    {
344
-        return EE_Registry::instance()->SSN->set_session_data(
345
-            array(
346
-                EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY => $this->submitted_values(true),
347
-            )
348
-        );
349
-    }
350
-
351
-
352
-    /**
353
-     * retrieves the originally submitted input values in the session
354
-     * so that they can be used to repopulate the form if it failed validation
355
-     *
356
-     * @return array
357
-     * @throws InvalidArgumentException
358
-     * @throws InvalidInterfaceException
359
-     * @throws InvalidDataTypeException
360
-     */
361
-    protected function get_submitted_form_data_from_session()
362
-    {
363
-        $session = EE_Registry::instance()->SSN;
364
-        if ($session instanceof EE_Session) {
365
-            return $session->get_session_data(
366
-                EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY
367
-            );
368
-        }
369
-        return array();
370
-    }
371
-
372
-
373
-    /**
374
-     * flushed the originally submitted input values from the session
375
-     *
376
-     * @return boolean whether or not the data was successfully removed from the session
377
-     * @throws InvalidArgumentException
378
-     * @throws InvalidInterfaceException
379
-     * @throws InvalidDataTypeException
380
-     */
381
-    protected function flush_submitted_form_data_from_session()
382
-    {
383
-        return EE_Registry::instance()->SSN->reset_data(
384
-            array(EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY)
385
-        );
386
-    }
387
-
388
-
389
-    /**
390
-     * Populates this form and its subsections with data from the session.
391
-     * (Wrapper for EE_Form_Section_Proper::receive_form_submission, so it shows
392
-     * validation errors when displaying too)
393
-     * Returns true if the form was populated from the session, false otherwise
394
-     *
395
-     * @return boolean
396
-     * @throws InvalidArgumentException
397
-     * @throws InvalidInterfaceException
398
-     * @throws InvalidDataTypeException
399
-     * @throws EE_Error
400
-     */
401
-    public function populate_from_session()
402
-    {
403
-        $form_data_in_session = $this->get_submitted_form_data_from_session();
404
-        if (empty($form_data_in_session)) {
405
-            return false;
406
-        }
407
-        $this->receive_form_submission($form_data_in_session);
408
-        $this->flush_submitted_form_data_from_session();
409
-        if ($this->form_data_present_in($form_data_in_session)) {
410
-            return true;
411
-        }
412
-        return false;
413
-    }
414
-
415
-
416
-    /**
417
-     * Populates the default data for the form, given an array where keys are
418
-     * the input names, and values are their values (preferably normalized to be their
419
-     * proper PHP types, not all strings... although that should be ok too).
420
-     * Proper subsections are sub-arrays, the key being the subsection's name, and
421
-     * the value being an array formatted in teh same way
422
-     *
423
-     * @param array $default_data
424
-     * @throws EE_Error
425
-     */
426
-    public function populate_defaults($default_data)
427
-    {
428
-        foreach ($this->subsections(false) as $subsection_name => $subsection) {
429
-            if (isset($default_data[ $subsection_name ])) {
430
-                if ($subsection instanceof EE_Form_Input_Base) {
431
-                    $subsection->set_default($default_data[ $subsection_name ]);
432
-                } elseif ($subsection instanceof EE_Form_Section_Proper) {
433
-                    $subsection->populate_defaults($default_data[ $subsection_name ]);
434
-                }
435
-            }
436
-        }
437
-    }
438
-
439
-
440
-    /**
441
-     * returns true if subsection exists
442
-     *
443
-     * @param string $name
444
-     * @return boolean
445
-     */
446
-    public function subsection_exists($name)
447
-    {
448
-        return isset($this->_subsections[ $name ]) ? true : false;
449
-    }
450
-
451
-
452
-    /**
453
-     * Gets the subsection specified by its name
454
-     *
455
-     * @param string  $name
456
-     * @param boolean $require_construction_to_be_finalized most client code should leave this as TRUE
457
-     *                                                      so that the inputs will be properly configured.
458
-     *                                                      However, some client code may be ok
459
-     *                                                      with construction finalize being called later
460
-     *                                                      (realizing that the subsections' html names
461
-     *                                                      might not be set yet, etc.)
462
-     * @return EE_Form_Section_Base
463
-     * @throws EE_Error
464
-     */
465
-    public function get_subsection($name, $require_construction_to_be_finalized = true)
466
-    {
467
-        if ($require_construction_to_be_finalized) {
468
-            $this->ensure_construct_finalized_called();
469
-        }
470
-        return $this->subsection_exists($name) ? $this->_subsections[ $name ] : null;
471
-    }
472
-
473
-
474
-    /**
475
-     * Gets all the validatable subsections of this form section
476
-     *
477
-     * @return EE_Form_Section_Validatable[]
478
-     * @throws EE_Error
479
-     */
480
-    public function get_validatable_subsections()
481
-    {
482
-        $validatable_subsections = array();
483
-        foreach ($this->subsections() as $name => $obj) {
484
-            if ($obj instanceof EE_Form_Section_Validatable) {
485
-                $validatable_subsections[ $name ] = $obj;
486
-            }
487
-        }
488
-        return $validatable_subsections;
489
-    }
490
-
491
-
492
-    /**
493
-     * Gets an input by the given name. If not found, or if its not an EE_FOrm_Input_Base child,
494
-     * throw an EE_Error.
495
-     *
496
-     * @param string  $name
497
-     * @param boolean $require_construction_to_be_finalized most client code should
498
-     *                                                      leave this as TRUE so that the inputs will be properly
499
-     *                                                      configured. However, some client code may be ok with
500
-     *                                                      construction finalize being called later
501
-     *                                                      (realizing that the subsections' html names might not be
502
-     *                                                      set yet, etc.)
503
-     * @return EE_Form_Input_Base
504
-     * @throws EE_Error
505
-     */
506
-    public function get_input($name, $require_construction_to_be_finalized = true)
507
-    {
508
-        $subsection = $this->get_subsection(
509
-            $name,
510
-            $require_construction_to_be_finalized
511
-        );
512
-        if (! $subsection instanceof EE_Form_Input_Base) {
513
-            throw new EE_Error(
514
-                sprintf(
515
-                    esc_html__(
516
-                        "Subsection '%s' is not an instanceof EE_Form_Input_Base on form '%s'. It is a '%s'",
517
-                        'event_espresso'
518
-                    ),
519
-                    $name,
520
-                    get_class($this),
521
-                    $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso')
522
-                )
523
-            );
524
-        }
525
-        return $subsection;
526
-    }
527
-
528
-
529
-    /**
530
-     * Like get_input(), gets the proper subsection of the form given the name,
531
-     * otherwise throws an EE_Error
532
-     *
533
-     * @param string  $name
534
-     * @param boolean $require_construction_to_be_finalized most client code should
535
-     *                                                      leave this as TRUE so that the inputs will be properly
536
-     *                                                      configured. However, some client code may be ok with
537
-     *                                                      construction finalize being called later
538
-     *                                                      (realizing that the subsections' html names might not be
539
-     *                                                      set yet, etc.)
540
-     * @return EE_Form_Section_Proper
541
-     * @throws EE_Error
542
-     */
543
-    public function get_proper_subsection($name, $require_construction_to_be_finalized = true)
544
-    {
545
-        $subsection = $this->get_subsection(
546
-            $name,
547
-            $require_construction_to_be_finalized
548
-        );
549
-        if (! $subsection instanceof EE_Form_Section_Proper) {
550
-            throw new EE_Error(
551
-                sprintf(
552
-                    esc_html__(
553
-                        "Subsection '%'s is not an instanceof EE_Form_Section_Proper on form '%s'",
554
-                        'event_espresso'
555
-                    ),
556
-                    $name,
557
-                    get_class($this)
558
-                )
559
-            );
560
-        }
561
-        return $subsection;
562
-    }
563
-
564
-
565
-    /**
566
-     * Gets the value of the specified input. Should be called after receive_form_submission()
567
-     * or populate_defaults() on the form, where the normalized value on the input is set.
568
-     *
569
-     * @param string $name
570
-     * @return mixed depending on the input's type and its normalization strategy
571
-     * @throws EE_Error
572
-     */
573
-    public function get_input_value($name)
574
-    {
575
-        $input = $this->get_input($name);
576
-        return $input->normalized_value();
577
-    }
578
-
579
-
580
-    /**
581
-     * Checks if this form section itself is valid, and then checks its subsections
582
-     *
583
-     * @throws EE_Error
584
-     * @return boolean
585
-     */
586
-    public function is_valid()
587
-    {
588
-        if($this->is_valid === null) {
589
-            if (! $this->has_received_submission()) {
590
-                throw new EE_Error(
591
-                    sprintf(
592
-                        esc_html__(
593
-                            'You cannot check if a form is valid before receiving the form submission using receive_form_submission',
594
-                            'event_espresso'
595
-                        )
596
-                    )
597
-                );
598
-            }
599
-            if (! parent::is_valid()) {
600
-                $this->is_valid = false;
601
-            } else {
602
-                // ok so no general errors to this entire form section.
603
-                // so let's check the subsections, but only set errors if that hasn't been done yet
604
-                $this->is_valid = true;
605
-                foreach ($this->get_validatable_subsections() as $subsection) {
606
-                    if (! $subsection->is_valid()) {
607
-                        $this->is_valid = false;
608
-                    }
609
-                }
610
-            }
611
-        }
612
-        return $this->is_valid;
613
-    }
614
-
615
-
616
-    /**
617
-     * gets the default name of this form section if none is specified
618
-     *
619
-     * @return void
620
-     */
621
-    protected function _set_default_name_if_empty()
622
-    {
623
-        if (! $this->_name) {
624
-            $classname    = get_class($this);
625
-            $default_name = str_replace('EE_', '', $classname);
626
-            $this->_name  = $default_name;
627
-        }
628
-    }
629
-
630
-
631
-    /**
632
-     * Returns the HTML for the form, except for the form opening and closing tags
633
-     * (as the form section doesn't know where you necessarily want to send the information to),
634
-     * and except for a submit button. Enqueues JS and CSS; if called early enough we will
635
-     * try to enqueue them in the header, otherwise they'll be enqueued in the footer.
636
-     * Not doing_it_wrong because theoretically this CAN be used properly,
637
-     * provided its used during "wp_enqueue_scripts", or it doesn't need to enqueue
638
-     * any CSS.
639
-     *
640
-     * @throws InvalidArgumentException
641
-     * @throws InvalidInterfaceException
642
-     * @throws InvalidDataTypeException
643
-     * @throws EE_Error
644
-     */
645
-    public function get_html_and_js()
646
-    {
647
-        $this->enqueue_js();
648
-        return $this->get_html();
649
-    }
650
-
651
-
652
-    /**
653
-     * returns HTML for displaying this form section. recursively calls display_section() on all subsections
654
-     *
655
-     * @param bool $display_previously_submitted_data
656
-     * @return string
657
-     * @throws InvalidArgumentException
658
-     * @throws InvalidInterfaceException
659
-     * @throws InvalidDataTypeException
660
-     * @throws EE_Error
661
-     * @throws EE_Error
662
-     * @throws EE_Error
663
-     */
664
-    public function get_html($display_previously_submitted_data = true)
665
-    {
666
-        $this->ensure_construct_finalized_called();
667
-        if ($display_previously_submitted_data) {
668
-            $this->populate_from_session();
669
-        }
670
-        return $this->_form_html_filter
671
-            ? $this->_form_html_filter->filterHtml($this->_layout_strategy->layout_form(), $this)
672
-            : $this->_layout_strategy->layout_form();
673
-    }
674
-
675
-
676
-    /**
677
-     * enqueues JS and CSS for the form.
678
-     * It is preferred to call this before wp_enqueue_scripts so the
679
-     * scripts and styles can be put in the header, but if called later
680
-     * they will be put in the footer (which is OK for JS, but in HTML4 CSS should
681
-     * only be in the header; but in HTML5 its ok in the body.
682
-     * See http://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tag.
683
-     * So if your form enqueues CSS, it's preferred to call this before wp_enqueue_scripts.)
684
-     *
685
-     * @return void
686
-     * @throws EE_Error
687
-     */
688
-    public function enqueue_js()
689
-    {
690
-        $this->_enqueue_and_localize_form_js();
691
-        foreach ($this->subsections() as $subsection) {
692
-            $subsection->enqueue_js();
693
-        }
694
-    }
695
-
696
-
697
-    /**
698
-     * adds a filter so that jquery validate gets enqueued in EE_System::wp_enqueue_scripts().
699
-     * This must be done BEFORE wp_enqueue_scripts() gets called, which is on
700
-     * the wp_enqueue_scripts hook.
701
-     * However, registering the form js and localizing it can happen when we
702
-     * actually output the form (which is preferred, seeing how teh form's fields
703
-     * could change until it's actually outputted)
704
-     *
705
-     * @param boolean $init_form_validation_automatically whether or not we want the form validation
706
-     *                                                    to be triggered automatically or not
707
-     * @return void
708
-     */
709
-    public static function wp_enqueue_scripts($init_form_validation_automatically = true)
710
-    {
711
-        wp_register_script(
712
-            'ee_form_section_validation',
713
-            EE_GLOBAL_ASSETS_URL . 'scripts' . DS . 'form_section_validation.js',
714
-            array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'),
715
-            EVENT_ESPRESSO_VERSION,
716
-            true
717
-        );
718
-        wp_localize_script(
719
-            'ee_form_section_validation',
720
-            'ee_form_section_validation_init',
721
-            array('init' => $init_form_validation_automatically ? '1' : '0')
722
-        );
723
-    }
724
-
725
-
726
-    /**
727
-     * gets the variables used by form_section_validation.js.
728
-     * This needs to be called AFTER we've called $this->_enqueue_jquery_validate_script,
729
-     * but before the wordpress hook wp_loaded
730
-     *
731
-     * @throws EE_Error
732
-     */
733
-    public function _enqueue_and_localize_form_js()
734
-    {
735
-        $this->ensure_construct_finalized_called();
736
-        //actually, we don't want to localize just yet. There may be other forms on the page.
737
-        //so we need to add our form section data to a static variable accessible by all form sections
738
-        //and localize it just before the footer
739
-        $this->localize_validation_rules();
740
-        add_action('wp_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'), 2);
741
-        add_action('admin_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'));
742
-    }
743
-
744
-
745
-    /**
746
-     * add our form section data to a static variable accessible by all form sections
747
-     *
748
-     * @param bool $return_for_subsection
749
-     * @return void
750
-     * @throws EE_Error
751
-     */
752
-    public function localize_validation_rules($return_for_subsection = false)
753
-    {
754
-        // we only want to localize vars ONCE for the entire form,
755
-        // so if the form section doesn't have a parent, then it must be the top dog
756
-        if ($return_for_subsection || ! $this->parent_section()) {
757
-            EE_Form_Section_Proper::$_js_localization['form_data'][ $this->html_id() ] = array(
758
-                'form_section_id'  => $this->html_id(true),
759
-                'validation_rules' => $this->get_jquery_validation_rules(),
760
-                'other_data'       => $this->get_other_js_data(),
761
-                'errors'           => $this->subsection_validation_errors_by_html_name(),
762
-            );
763
-            EE_Form_Section_Proper::$_scripts_localized                                = true;
764
-        }
765
-    }
766
-
767
-
768
-    /**
769
-     * Gets an array of extra data that will be useful for client-side javascript.
770
-     * This is primarily data added by inputs and forms in addition to any
771
-     * scripts they might enqueue
772
-     *
773
-     * @param array $form_other_js_data
774
-     * @return array
775
-     * @throws EE_Error
776
-     */
777
-    public function get_other_js_data($form_other_js_data = array())
778
-    {
779
-        foreach ($this->subsections() as $subsection) {
780
-            $form_other_js_data = $subsection->get_other_js_data($form_other_js_data);
781
-        }
782
-        return $form_other_js_data;
783
-    }
784
-
785
-
786
-    /**
787
-     * Gets a flat array of inputs for this form section and its subsections.
788
-     * Keys are their form names, and values are the inputs themselves
789
-     *
790
-     * @return EE_Form_Input_Base
791
-     * @throws EE_Error
792
-     */
793
-    public function inputs_in_subsections()
794
-    {
795
-        $inputs = array();
796
-        foreach ($this->subsections() as $subsection) {
797
-            if ($subsection instanceof EE_Form_Input_Base) {
798
-                $inputs[ $subsection->html_name() ] = $subsection;
799
-            } elseif ($subsection instanceof EE_Form_Section_Proper) {
800
-                $inputs += $subsection->inputs_in_subsections();
801
-            }
802
-        }
803
-        return $inputs;
804
-    }
805
-
806
-
807
-    /**
808
-     * Gets a flat array of all the validation errors.
809
-     * Keys are html names (because those should be unique)
810
-     * and values are a string of all their validation errors
811
-     *
812
-     * @return string[]
813
-     * @throws EE_Error
814
-     */
815
-    public function subsection_validation_errors_by_html_name()
816
-    {
817
-        $inputs = $this->inputs();
818
-        $errors = array();
819
-        foreach ($inputs as $form_input) {
820
-            if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) {
821
-                $errors[ $form_input->html_name() ] = $form_input->get_validation_error_string();
822
-            }
823
-        }
824
-        return $errors;
825
-    }
826
-
827
-
828
-    /**
829
-     * passes all the form data required by the JS to the JS, and enqueues the few required JS files.
830
-     * Should be setup by each form during the _enqueues_and_localize_form_js
831
-     *
832
-     * @throws InvalidArgumentException
833
-     * @throws InvalidInterfaceException
834
-     * @throws InvalidDataTypeException
835
-     */
836
-    public static function localize_script_for_all_forms()
837
-    {
838
-        //allow inputs and stuff to hook in their JS and stuff here
839
-        do_action('AHEE__EE_Form_Section_Proper__localize_script_for_all_forms__begin');
840
-        EE_Form_Section_Proper::$_js_localization['localized_error_messages'] = EE_Form_Section_Proper::_get_localized_error_messages();
841
-        $email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level)
842
-            ? EE_Registry::instance()->CFG->registration->email_validation_level
843
-            : 'wp_default';
844
-        EE_Form_Section_Proper::$_js_localization['email_validation_level']   = $email_validation_level;
845
-        wp_enqueue_script('ee_form_section_validation');
846
-        wp_localize_script(
847
-            'ee_form_section_validation',
848
-            'ee_form_section_vars',
849
-            EE_Form_Section_Proper::$_js_localization
850
-        );
851
-    }
852
-
853
-
854
-    /**
855
-     * ensure_scripts_localized
856
-     *
857
-     * @throws EE_Error
858
-     */
859
-    public function ensure_scripts_localized()
860
-    {
861
-        if (! EE_Form_Section_Proper::$_scripts_localized) {
862
-            $this->_enqueue_and_localize_form_js();
863
-        }
864
-    }
865
-
866
-
867
-    /**
868
-     * Gets the hard-coded validation error messages to be used in the JS. The convention
869
-     * is that the key here should be the same as the custom validation rule put in the JS file
870
-     *
871
-     * @return array keys are custom validation rules, and values are internationalized strings
872
-     */
873
-    private static function _get_localized_error_messages()
874
-    {
875
-        return array(
876
-            'validUrl' => esc_html__('This is not a valid absolute URL. Eg, http://domain.com/monkey.jpg', 'event_espresso'),
877
-            'regex'    => esc_html__('Please check your input', 'event_espresso'),
878
-        );
879
-    }
880
-
881
-
882
-    /**
883
-     * @return array
884
-     */
885
-    public static function js_localization()
886
-    {
887
-        return self::$_js_localization;
888
-    }
889
-
890
-
891
-    /**
892
-     * @return void
893
-     */
894
-    public static function reset_js_localization()
895
-    {
896
-        self::$_js_localization = array();
897
-    }
898
-
899
-
900
-    /**
901
-     * Gets the JS to put inside the jquery validation rules for subsection of this form section.
902
-     * See parent function for more...
903
-     *
904
-     * @return array
905
-     * @throws EE_Error
906
-     */
907
-    public function get_jquery_validation_rules()
908
-    {
909
-        $jquery_validation_rules = array();
910
-        foreach ($this->get_validatable_subsections() as $subsection) {
911
-            $jquery_validation_rules = array_merge(
912
-                $jquery_validation_rules,
913
-                $subsection->get_jquery_validation_rules()
914
-            );
915
-        }
916
-        return $jquery_validation_rules;
917
-    }
918
-
919
-
920
-    /**
921
-     * Sanitizes all the data and sets the sanitized value of each field
922
-     *
923
-     * @param array $req_data like $_POST
924
-     * @return void
925
-     * @throws EE_Error
926
-     */
927
-    protected function _normalize($req_data)
928
-    {
929
-        $this->_received_submission = true;
930
-        $this->_validation_errors   = array();
931
-        foreach ($this->get_validatable_subsections() as $subsection) {
932
-            try {
933
-                $subsection->_normalize($req_data);
934
-            } catch (EE_Validation_Error $e) {
935
-                $subsection->add_validation_error($e);
936
-            }
937
-        }
938
-    }
939
-
940
-
941
-    /**
942
-     * Performs validation on this form section and its subsections.
943
-     * For each subsection,
944
-     * calls _validate_{subsection_name} on THIS form (if the function exists)
945
-     * and passes it the subsection, then calls _validate on that subsection.
946
-     * If you need to perform validation on the form as a whole (considering multiple)
947
-     * you would be best to override this _validate method,
948
-     * calling parent::_validate() first.
949
-     *
950
-     * @throws EE_Error
951
-     */
952
-    protected function _validate()
953
-    {
954
-        //reset the cache of whether this form is valid or not- we're re-validating it now
955
-        $this->is_valid = null;
956
-        foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) {
957
-            if (method_exists($this, '_validate_' . $subsection_name)) {
958
-                call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection));
959
-            }
960
-            $subsection->_validate();
961
-        }
962
-    }
963
-
964
-
965
-    /**
966
-     * Gets all the validated inputs for the form section
967
-     *
968
-     * @return array
969
-     * @throws EE_Error
970
-     */
971
-    public function valid_data()
972
-    {
973
-        $inputs = array();
974
-        foreach ($this->subsections() as $subsection_name => $subsection) {
975
-            if ($subsection instanceof EE_Form_Section_Proper) {
976
-                $inputs[ $subsection_name ] = $subsection->valid_data();
977
-            } elseif ($subsection instanceof EE_Form_Input_Base) {
978
-                $inputs[ $subsection_name ] = $subsection->normalized_value();
979
-            }
980
-        }
981
-        return $inputs;
982
-    }
983
-
984
-
985
-    /**
986
-     * Gets all the inputs on this form section
987
-     *
988
-     * @return EE_Form_Input_Base[]
989
-     * @throws EE_Error
990
-     */
991
-    public function inputs()
992
-    {
993
-        $inputs = array();
994
-        foreach ($this->subsections() as $subsection_name => $subsection) {
995
-            if ($subsection instanceof EE_Form_Input_Base) {
996
-                $inputs[ $subsection_name ] = $subsection;
997
-            }
998
-        }
999
-        return $inputs;
1000
-    }
1001
-
1002
-
1003
-    /**
1004
-     * Gets all the subsections which are a proper form
1005
-     *
1006
-     * @return EE_Form_Section_Proper[]
1007
-     * @throws EE_Error
1008
-     */
1009
-    public function subforms()
1010
-    {
1011
-        $form_sections = array();
1012
-        foreach ($this->subsections() as $name => $obj) {
1013
-            if ($obj instanceof EE_Form_Section_Proper) {
1014
-                $form_sections[ $name ] = $obj;
1015
-            }
1016
-        }
1017
-        return $form_sections;
1018
-    }
1019
-
1020
-
1021
-    /**
1022
-     * Gets all the subsections (inputs, proper subsections, or html-only sections).
1023
-     * Consider using inputs() or subforms()
1024
-     * if you only want form inputs or proper form sections.
1025
-     *
1026
-     * @param boolean $require_construction_to_be_finalized most client code should
1027
-     *                                                      leave this as TRUE so that the inputs will be properly
1028
-     *                                                      configured. However, some client code may be ok with
1029
-     *                                                      construction finalize being called later
1030
-     *                                                      (realizing that the subsections' html names might not be
1031
-     *                                                      set yet, etc.)
1032
-     * @return EE_Form_Section_Proper[]
1033
-     * @throws EE_Error
1034
-     */
1035
-    public function subsections($require_construction_to_be_finalized = true)
1036
-    {
1037
-        if ($require_construction_to_be_finalized) {
1038
-            $this->ensure_construct_finalized_called();
1039
-        }
1040
-        return $this->_subsections;
1041
-    }
1042
-
1043
-
1044
-    /**
1045
-     * Returns whether this form has any subforms or inputs
1046
-     * @return bool
1047
-     */
1048
-    public function hasSubsections()
1049
-    {
1050
-        return ! empty($this->_subsections);
1051
-    }
1052
-
1053
-
1054
-    /**
1055
-     * Returns a simple array where keys are input names, and values are their normalized
1056
-     * values. (Similar to calling get_input_value on inputs)
1057
-     *
1058
-     * @param boolean $include_subform_inputs Whether to include inputs from subforms,
1059
-     *                                        or just this forms' direct children inputs
1060
-     * @param boolean $flatten                Whether to force the results into 1-dimensional array,
1061
-     *                                        or allow multidimensional array
1062
-     * @return array if $flatten is TRUE it will always be a 1-dimensional array
1063
-     *                                        with array keys being input names
1064
-     *                                        (regardless of whether they are from a subsection or not),
1065
-     *                                        and if $flatten is FALSE it can be a multidimensional array
1066
-     *                                        where keys are always subsection names and values are either
1067
-     *                                        the input's normalized value, or an array like the top-level array
1068
-     * @throws EE_Error
1069
-     */
1070
-    public function input_values($include_subform_inputs = false, $flatten = false)
1071
-    {
1072
-        return $this->_input_values(false, $include_subform_inputs, $flatten);
1073
-    }
1074
-
1075
-
1076
-    /**
1077
-     * Similar to EE_Form_Section_Proper::input_values(), except this returns the 'display_value'
1078
-     * of each input. On some inputs (especially radio boxes or checkboxes), the value stored
1079
-     * is not necessarily the value we want to display to users. This creates an array
1080
-     * where keys are the input names, and values are their display values
1081
-     *
1082
-     * @param boolean $include_subform_inputs Whether to include inputs from subforms,
1083
-     *                                        or just this forms' direct children inputs
1084
-     * @param boolean $flatten                Whether to force the results into 1-dimensional array,
1085
-     *                                        or allow multidimensional array
1086
-     * @return array if $flatten is TRUE it will always be a 1-dimensional array
1087
-     *                                        with array keys being input names
1088
-     *                                        (regardless of whether they are from a subsection or not),
1089
-     *                                        and if $flatten is FALSE it can be a multidimensional array
1090
-     *                                        where keys are always subsection names and values are either
1091
-     *                                        the input's normalized value, or an array like the top-level array
1092
-     * @throws EE_Error
1093
-     */
1094
-    public function input_pretty_values($include_subform_inputs = false, $flatten = false)
1095
-    {
1096
-        return $this->_input_values(true, $include_subform_inputs, $flatten);
1097
-    }
1098
-
1099
-
1100
-    /**
1101
-     * Gets the input values from the form
1102
-     *
1103
-     * @param boolean $pretty                 Whether to retrieve the pretty value,
1104
-     *                                        or just the normalized value
1105
-     * @param boolean $include_subform_inputs Whether to include inputs from subforms,
1106
-     *                                        or just this forms' direct children inputs
1107
-     * @param boolean $flatten                Whether to force the results into 1-dimensional array,
1108
-     *                                        or allow multidimensional array
1109
-     * @return array if $flatten is TRUE it will always be a 1-dimensional array with array keys being
1110
-     *                                        input names (regardless of whether they are from a subsection or not),
1111
-     *                                        and if $flatten is FALSE it can be a multidimensional array
1112
-     *                                        where keys are always subsection names and values are either
1113
-     *                                        the input's normalized value, or an array like the top-level array
1114
-     * @throws EE_Error
1115
-     */
1116
-    public function _input_values($pretty = false, $include_subform_inputs = false, $flatten = false)
1117
-    {
1118
-        $input_values = array();
1119
-        foreach ($this->subsections() as $subsection_name => $subsection) {
1120
-            if ($subsection instanceof EE_Form_Input_Base) {
1121
-                $input_values[ $subsection_name ] = $pretty
1122
-                    ? $subsection->pretty_value()
1123
-                    : $subsection->normalized_value();
1124
-            } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) {
1125
-                $subform_input_values = $subsection->_input_values(
1126
-                    $pretty,
1127
-                    $include_subform_inputs,
1128
-                    $flatten
1129
-                );
1130
-                if ($flatten) {
1131
-                    $input_values = array_merge($input_values, $subform_input_values);
1132
-                } else {
1133
-                    $input_values[ $subsection_name ] = $subform_input_values;
1134
-                }
1135
-            }
1136
-        }
1137
-        return $input_values;
1138
-    }
1139
-
1140
-
1141
-    /**
1142
-     * Gets the originally submitted input values from the form
1143
-     *
1144
-     * @param boolean $include_subforms  Whether to include inputs from subforms,
1145
-     *                                   or just this forms' direct children inputs
1146
-     * @return array                     if $flatten is TRUE it will always be a 1-dimensional array
1147
-     *                                   with array keys being input names
1148
-     *                                   (regardless of whether they are from a subsection or not),
1149
-     *                                   and if $flatten is FALSE it can be a multidimensional array
1150
-     *                                   where keys are always subsection names and values are either
1151
-     *                                   the input's normalized value, or an array like the top-level array
1152
-     * @throws EE_Error
1153
-     */
1154
-    public function submitted_values($include_subforms = false)
1155
-    {
1156
-        $submitted_values = array();
1157
-        foreach ($this->subsections() as $subsection) {
1158
-            if ($subsection instanceof EE_Form_Input_Base) {
1159
-                // is this input part of an array of inputs?
1160
-                if (strpos($subsection->html_name(), '[') !== false) {
1161
-                    $full_input_name  = EEH_Array::convert_array_values_to_keys(
1162
-                        explode(
1163
-                            '[',
1164
-                            str_replace(']', '', $subsection->html_name())
1165
-                        ),
1166
-                        $subsection->raw_value()
1167
-                    );
1168
-                    $submitted_values = array_replace_recursive($submitted_values, $full_input_name);
1169
-                } else {
1170
-                    $submitted_values[ $subsection->html_name() ] = $subsection->raw_value();
1171
-                }
1172
-            } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subforms) {
1173
-                $subform_input_values = $subsection->submitted_values($include_subforms);
1174
-                $submitted_values     = array_replace_recursive($submitted_values, $subform_input_values);
1175
-            }
1176
-        }
1177
-        return $submitted_values;
1178
-    }
1179
-
1180
-
1181
-    /**
1182
-     * Indicates whether or not this form has received a submission yet
1183
-     * (ie, had receive_form_submission called on it yet)
1184
-     *
1185
-     * @return boolean
1186
-     * @throws EE_Error
1187
-     */
1188
-    public function has_received_submission()
1189
-    {
1190
-        $this->ensure_construct_finalized_called();
1191
-        return $this->_received_submission;
1192
-    }
1193
-
1194
-
1195
-    /**
1196
-     * Equivalent to passing 'exclude' in the constructor's options array.
1197
-     * Removes the listed inputs from the form
1198
-     *
1199
-     * @param array $inputs_to_exclude values are the input names
1200
-     * @return void
1201
-     */
1202
-    public function exclude(array $inputs_to_exclude = array())
1203
-    {
1204
-        foreach ($inputs_to_exclude as $input_to_exclude_name) {
1205
-            unset($this->_subsections[ $input_to_exclude_name ]);
1206
-        }
1207
-    }
1208
-
1209
-
1210
-    /**
1211
-     * @param array $inputs_to_hide
1212
-     * @throws EE_Error
1213
-     */
1214
-    public function hide(array $inputs_to_hide = array())
1215
-    {
1216
-        foreach ($inputs_to_hide as $input_to_hide) {
1217
-            $input = $this->get_input($input_to_hide);
1218
-            $input->set_display_strategy(new EE_Hidden_Display_Strategy());
1219
-        }
1220
-    }
1221
-
1222
-
1223
-    /**
1224
-     * add_subsections
1225
-     * Adds the listed subsections to the form section.
1226
-     * If $subsection_name_to_target is provided,
1227
-     * then new subsections are added before or after that subsection,
1228
-     * otherwise to the start or end of the entire subsections array.
1229
-     *
1230
-     * @param EE_Form_Section_Base[] $new_subsections           array of new form subsections
1231
-     *                                                          where keys are their names
1232
-     * @param string                 $subsection_name_to_target an existing for section that $new_subsections
1233
-     *                                                          should be added before or after
1234
-     *                                                          IF $subsection_name_to_target is null,
1235
-     *                                                          then $new_subsections will be added to
1236
-     *                                                          the beginning or end of the entire subsections array
1237
-     * @param boolean                $add_before                whether to add $new_subsections, before or after
1238
-     *                                                          $subsection_name_to_target,
1239
-     *                                                          or if $subsection_name_to_target is null,
1240
-     *                                                          before or after entire subsections array
1241
-     * @return void
1242
-     * @throws EE_Error
1243
-     */
1244
-    public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true)
1245
-    {
1246
-        foreach ($new_subsections as $subsection_name => $subsection) {
1247
-            if (! $subsection instanceof EE_Form_Section_Base) {
1248
-                EE_Error::add_error(
1249
-                    sprintf(
1250
-                        esc_html__(
1251
-                            "Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.",
1252
-                            'event_espresso'
1253
-                        ),
1254
-                        get_class($subsection),
1255
-                        $subsection_name,
1256
-                        $this->name()
1257
-                    )
1258
-                );
1259
-                unset($new_subsections[ $subsection_name ]);
1260
-            }
1261
-        }
1262
-        $this->_subsections = EEH_Array::insert_into_array(
1263
-            $this->_subsections,
1264
-            $new_subsections,
1265
-            $subsection_name_to_target,
1266
-            $add_before
1267
-        );
1268
-        if ($this->_construction_finalized) {
1269
-            foreach ($this->_subsections as $name => $subsection) {
1270
-                $subsection->_construct_finalize($this, $name);
1271
-            }
1272
-        }
1273
-    }
1274
-
1275
-
1276
-    /**
1277
-     * Just gets all validatable subsections to clean their sensitive data
1278
-     *
1279
-     * @throws EE_Error
1280
-     */
1281
-    public function clean_sensitive_data()
1282
-    {
1283
-        foreach ($this->get_validatable_subsections() as $subsection) {
1284
-            $subsection->clean_sensitive_data();
1285
-        }
1286
-    }
1287
-
1288
-
1289
-    /**
1290
-     * Sets the submission error message (aka validation error message for this form section and all sub-sections)
1291
-     * @param string                           $form_submission_error_message
1292
-     * @param EE_Form_Section_Validatable $form_section unused
1293
-     * @throws EE_Error
1294
-     */
1295
-    public function set_submission_error_message(
1296
-        $form_submission_error_message = ''
1297
-    ) {
1298
-        $this->_form_submission_error_message = ! empty($form_submission_error_message)
1299
-            ? $form_submission_error_message
1300
-            : $this->getAllValidationErrorsString();
1301
-    }
1302
-
1303
-
1304
-    /**
1305
-     * Returns the cached error message. A default value is set for this during _validate(),
1306
-     * (called during receive_form_submission) but it can be explicitly set using
1307
-     * set_submission_error_message
1308
-     *
1309
-     * @return string
1310
-     */
1311
-    public function submission_error_message()
1312
-    {
1313
-        return $this->_form_submission_error_message;
1314
-    }
1315
-
1316
-
1317
-    /**
1318
-     * Sets a message to display if the data submitted to the form was valid.
1319
-     * @param string $form_submission_success_message
1320
-     */
1321
-    public function set_submission_success_message($form_submission_success_message = '')
1322
-    {
1323
-        $this->_form_submission_success_message = ! empty($form_submission_success_message)
1324
-            ? $form_submission_success_message
1325
-            : esc_html__('Form submitted successfully', 'event_espresso');
1326
-    }
1327
-
1328
-
1329
-    /**
1330
-     * Gets a message appropriate for display when the form is correctly submitted
1331
-     * @return string
1332
-     */
1333
-    public function submission_success_message()
1334
-    {
1335
-        return $this->_form_submission_success_message;
1336
-    }
1337
-
1338
-
1339
-    /**
1340
-     * Returns the prefix that should be used on child of this form section for
1341
-     * their html names. If this form section itself has a parent, prepends ITS
1342
-     * prefix onto this form section's prefix. Used primarily by
1343
-     * EE_Form_Input_Base::_set_default_html_name_if_empty
1344
-     *
1345
-     * @return string
1346
-     * @throws EE_Error
1347
-     */
1348
-    public function html_name_prefix()
1349
-    {
1350
-        if ($this->parent_section() instanceof EE_Form_Section_Proper) {
1351
-            return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']';
1352
-        }
1353
-        return $this->name();
1354
-    }
1355
-
1356
-
1357
-    /**
1358
-     * Gets the name, but first checks _construct_finalize has been called. If not,
1359
-     * calls it (assumes there is no parent and that we want the name to be whatever
1360
-     * was set, which is probably nothing, or the classname)
1361
-     *
1362
-     * @return string
1363
-     * @throws EE_Error
1364
-     */
1365
-    public function name()
1366
-    {
1367
-        $this->ensure_construct_finalized_called();
1368
-        return parent::name();
1369
-    }
1370
-
1371
-
1372
-    /**
1373
-     * @return EE_Form_Section_Proper
1374
-     * @throws EE_Error
1375
-     */
1376
-    public function parent_section()
1377
-    {
1378
-        $this->ensure_construct_finalized_called();
1379
-        return parent::parent_section();
1380
-    }
1381
-
1382
-
1383
-    /**
1384
-     * make sure construction finalized was called, otherwise children might not be ready
1385
-     *
1386
-     * @return void
1387
-     * @throws EE_Error
1388
-     */
1389
-    public function ensure_construct_finalized_called()
1390
-    {
1391
-        if (! $this->_construction_finalized) {
1392
-            $this->_construct_finalize($this->_parent_section, $this->_name);
1393
-        }
1394
-    }
1395
-
1396
-
1397
-    /**
1398
-     * Checks if any of this form section's inputs, or any of its children's inputs,
1399
-     * are in teh form data. If any are found, returns true. Else false
1400
-     *
1401
-     * @param array $req_data
1402
-     * @return boolean
1403
-     * @throws EE_Error
1404
-     */
1405
-    public function form_data_present_in($req_data = null)
1406
-    {
1407
-        $req_data = $this->getCachedRequest($req_data);
1408
-        foreach ($this->subsections() as $subsection) {
1409
-            if ($subsection instanceof EE_Form_Input_Base) {
1410
-                if ($subsection->form_data_present_in($req_data)) {
1411
-                    return true;
1412
-                }
1413
-            } elseif ($subsection instanceof EE_Form_Section_Proper) {
1414
-                if ($subsection->form_data_present_in($req_data)) {
1415
-                    return true;
1416
-                }
1417
-            }
1418
-        }
1419
-        return false;
1420
-    }
1421
-
1422
-
1423
-    /**
1424
-     * Gets validation errors for this form section and subsections
1425
-     * Similar to EE_Form_Section_Validatable::get_validation_errors() except this
1426
-     * gets the validation errors for ALL subsection
1427
-     *
1428
-     * @return EE_Validation_Error[]
1429
-     * @throws EE_Error
1430
-     */
1431
-    public function get_validation_errors_accumulated()
1432
-    {
1433
-        $validation_errors = $this->get_validation_errors();
1434
-        foreach ($this->get_validatable_subsections() as $subsection) {
1435
-            if ($subsection instanceof EE_Form_Section_Proper) {
1436
-                $validation_errors_on_this_subsection = $subsection->get_validation_errors_accumulated();
1437
-            } else {
1438
-                $validation_errors_on_this_subsection = $subsection->get_validation_errors();
1439
-            }
1440
-            if ($validation_errors_on_this_subsection) {
1441
-                $validation_errors = array_merge($validation_errors, $validation_errors_on_this_subsection);
1442
-            }
1443
-        }
1444
-        return $validation_errors;
1445
-    }
1446
-
1447
-    /**
1448
-     * Fetch validation errors from children and grandchildren and puts them in a single string.
1449
-     * This traverses the form section tree to generate this, but you probably want to instead use
1450
-     * get_form_submission_error_message() which is usually this message cached (or a custom validation error message)
1451
-     *
1452
-     * @return string
1453
-     * @since $VID:$
1454
-     */
1455
-    protected function getAllValidationErrorsString()
1456
-    {
1457
-        $submission_error_messages = array();
1458
-        // bad, bad, bad registrant
1459
-        foreach ($this->get_validation_errors_accumulated() as $validation_error) {
1460
-            if ($validation_error instanceof EE_Validation_Error) {
1461
-                $form_section = $validation_error->get_form_section();
1462
-                if ($form_section instanceof EE_Form_Input_Base) {
1463
-                   $label = $validation_error->get_form_section()->html_label_text();
1464
-                } elseif($form_section instanceof EE_Form_Section_Validatable) {
1465
-                    $label = $validation_error->get_form_section()->name();
1466
-                } else {
1467
-                    $label = esc_html__('Unknown', 'event_espresso');
1468
-                }
1469
-                $submission_error_messages[] = sprintf(
1470
-                    __('%s : %s', 'event_espresso'),
1471
-                    $label,
1472
-                    $validation_error->getMessage()
1473
-                );
1474
-            }
1475
-        }
1476
-        return implode('<br', $submission_error_messages);
1477
-    }
1478
-
1479
-
1480
-    /**
1481
-     * This isn't just the name of an input, it's a path pointing to an input. The
1482
-     * path is similar to a folder path: slash (/) means to descend into a subsection,
1483
-     * dot-dot-slash (../) means to ascend into the parent section.
1484
-     * After a series of slashes and dot-dot-slashes, there should be the name of an input,
1485
-     * which will be returned.
1486
-     * Eg, if you want the related input to be conditional on a sibling input name 'foobar'
1487
-     * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name
1488
-     * 'baz', use '../baz'. If you want it to be conditional on a cousin input,
1489
-     * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'.
1490
-     * Etc
1491
-     *
1492
-     * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false
1493
-     * @return EE_Form_Section_Base
1494
-     * @throws EE_Error
1495
-     */
1496
-    public function find_section_from_path($form_section_path)
1497
-    {
1498
-        //check if we can find the input from purely going straight up the tree
1499
-        $input = parent::find_section_from_path($form_section_path);
1500
-        if ($input instanceof EE_Form_Section_Base) {
1501
-            return $input;
1502
-        }
1503
-        $next_slash_pos = strpos($form_section_path, '/');
1504
-        if ($next_slash_pos !== false) {
1505
-            $child_section_name = substr($form_section_path, 0, $next_slash_pos);
1506
-            $subpath            = substr($form_section_path, $next_slash_pos + 1);
1507
-        } else {
1508
-            $child_section_name = $form_section_path;
1509
-            $subpath            = '';
1510
-        }
1511
-        $child_section = $this->get_subsection($child_section_name);
1512
-        if ($child_section instanceof EE_Form_Section_Base) {
1513
-            return $child_section->find_section_from_path($subpath);
1514
-        }
1515
-        return null;
1516
-    }
17
+	const SUBMITTED_FORM_DATA_SSN_KEY = 'submitted_form_data';
18
+
19
+	/**
20
+	 * Subsections
21
+	 *
22
+	 * @var EE_Form_Section_Validatable[]
23
+	 */
24
+	protected $_subsections = array();
25
+
26
+	/**
27
+	 * Strategy for laying out the form
28
+	 *
29
+	 * @var EE_Form_Section_Layout_Base
30
+	 */
31
+	protected $_layout_strategy;
32
+
33
+	/**
34
+	 * Whether or not this form has received and validated a form submission yet
35
+	 *
36
+	 * @var boolean
37
+	 */
38
+	protected $_received_submission = false;
39
+
40
+	/**
41
+	 * message displayed to users upon successful form submission
42
+	 *
43
+	 * @var string
44
+	 */
45
+	protected $_form_submission_success_message = '';
46
+
47
+	/**
48
+	 * message displayed to users upon unsuccessful form submission
49
+	 *
50
+	 * @var string
51
+	 */
52
+	protected $_form_submission_error_message = '';
53
+
54
+	/**
55
+	 * @var array like $_REQUEST
56
+	 */
57
+	protected $cached_request_data;
58
+
59
+	/**
60
+	 * Stores whether this form (and its sub-sections) were found to be valid or not.
61
+	 * Starts off as null, but once the form is validated, it set to either true or false
62
+	 * @var boolean|null
63
+	 */
64
+	protected $is_valid;
65
+
66
+	/**
67
+	 * Stores all the data that will localized for form validation
68
+	 *
69
+	 * @var array
70
+	 */
71
+	static protected $_js_localization = array();
72
+
73
+	/**
74
+	 * whether or not the form's localized validation JS vars have been set
75
+	 *
76
+	 * @type boolean
77
+	 */
78
+	static protected $_scripts_localized = false;
79
+
80
+
81
+	/**
82
+	 * when constructing a proper form section, calls _construct_finalize on children
83
+	 * so that they know who their parent is, and what name they've been given.
84
+	 *
85
+	 * @param array[] $options_array   {
86
+	 * @type          $subsections     EE_Form_Section_Validatable[] where keys are the section's name
87
+	 * @type          $include         string[] numerically-indexed where values are section names to be included,
88
+	 *                                 and in that order. This is handy if you want
89
+	 *                                 the subsections to be ordered differently than the default, and if you override
90
+	 *                                 which fields are shown
91
+	 * @type          $exclude         string[] values are subsections to be excluded. This is handy if you want
92
+	 *                                 to remove certain default subsections (note: if you specify BOTH 'include' AND
93
+	 *                                 'exclude', the inclusions will be applied first, and the exclusions will exclude
94
+	 *                                 items from that list of inclusions)
95
+	 * @type          $layout_strategy EE_Form_Section_Layout_Base strategy for laying out the form
96
+	 *                                 } @see EE_Form_Section_Validatable::__construct()
97
+	 * @throws EE_Error
98
+	 */
99
+	public function __construct($options_array = array())
100
+	{
101
+		$options_array = (array) apply_filters(
102
+			'FHEE__EE_Form_Section_Proper___construct__options_array',
103
+			$options_array,
104
+			$this
105
+		);
106
+		//call parent first, as it may be setting the name
107
+		parent::__construct($options_array);
108
+		//if they've included subsections in the constructor, add them now
109
+		if (isset($options_array['include'])) {
110
+			//we are going to make sure we ONLY have those subsections to include
111
+			//AND we are going to make sure they're in that specified order
112
+			$reordered_subsections = array();
113
+			foreach ($options_array['include'] as $input_name) {
114
+				if (isset($this->_subsections[ $input_name ])) {
115
+					$reordered_subsections[ $input_name ] = $this->_subsections[ $input_name ];
116
+				}
117
+			}
118
+			$this->_subsections = $reordered_subsections;
119
+		}
120
+		if (isset($options_array['exclude'])) {
121
+			$exclude            = $options_array['exclude'];
122
+			$this->_subsections = array_diff_key($this->_subsections, array_flip($exclude));
123
+		}
124
+		if (isset($options_array['layout_strategy'])) {
125
+			$this->_layout_strategy = $options_array['layout_strategy'];
126
+		}
127
+		if (! $this->_layout_strategy) {
128
+			$this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout();
129
+		}
130
+		$this->_layout_strategy->_construct_finalize($this);
131
+		//ok so we are definitely going to want the forms JS,
132
+		//so enqueue it or remember to enqueue it during wp_enqueue_scripts
133
+		if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) {
134
+			//ok so they've constructed this object after when they should have.
135
+			//just enqueue the generic form scripts and initialize the form immediately in the JS
136
+			EE_Form_Section_Proper::wp_enqueue_scripts(true);
137
+		} else {
138
+			add_action('wp_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts'));
139
+			add_action('admin_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts'));
140
+		}
141
+		add_action('wp_footer', array($this, 'ensure_scripts_localized'), 1);
142
+		/**
143
+		 * Gives other plugins a chance to hook in before construct finalize is called.
144
+		 * The form probably doesn't yet have a parent form section.
145
+		 * Since 4.9.32, when this action was introduced, this is the best place to add a subsection onto a form,
146
+		 * assuming you don't care what the form section's name, HTML ID, or HTML name etc are.
147
+		 * Also see AHEE__EE_Form_Section_Proper___construct_finalize__end
148
+		 *
149
+		 * @since 4.9.32
150
+		 * @param EE_Form_Section_Proper $this          before __construct is done, but all of its logic,
151
+		 *                                              except maybe calling _construct_finalize has been done
152
+		 * @param array                  $options_array options passed into the constructor
153
+		 */
154
+		do_action(
155
+			'AHEE__EE_Form_Input_Base___construct__before_construct_finalize_called',
156
+			$this,
157
+			$options_array
158
+		);
159
+		if (isset($options_array['name'])) {
160
+			$this->_construct_finalize(null, $options_array['name']);
161
+		}
162
+	}
163
+
164
+
165
+	/**
166
+	 * Finishes construction given the parent form section and this form section's name
167
+	 *
168
+	 * @param EE_Form_Section_Proper $parent_form_section
169
+	 * @param string                 $name
170
+	 * @throws EE_Error
171
+	 */
172
+	public function _construct_finalize($parent_form_section, $name)
173
+	{
174
+		parent::_construct_finalize($parent_form_section, $name);
175
+		$this->_set_default_name_if_empty();
176
+		$this->_set_default_html_id_if_empty();
177
+		foreach ($this->_subsections as $subsection_name => $subsection) {
178
+			if ($subsection instanceof EE_Form_Section_Base) {
179
+				$subsection->_construct_finalize($this, $subsection_name);
180
+			} else {
181
+				throw new EE_Error(
182
+					sprintf(
183
+						esc_html__(
184
+							'Subsection "%s" is not an instanceof EE_Form_Section_Base on form "%s". It is a "%s"',
185
+							'event_espresso'
186
+						),
187
+						$subsection_name,
188
+						get_class($this),
189
+						$subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso')
190
+					)
191
+				);
192
+			}
193
+		}
194
+		/**
195
+		 * Action performed just after form has been given a name (and HTML ID etc) and is fully constructed.
196
+		 * If you have code that should modify the form and needs it and its subsections to have a name, HTML ID
197
+		 * (or other attributes derived from the name like the HTML label id, etc), this is where it should be done.
198
+		 * This might only happen just before displaying the form, or just before it receives form submission data.
199
+		 * If you need to modify the form or its subsections before _construct_finalize is called on it (and we've
200
+		 * ensured it has a name, HTML IDs, etc
201
+		 *
202
+		 * @param EE_Form_Section_Proper      $this
203
+		 * @param EE_Form_Section_Proper|null $parent_form_section
204
+		 * @param string                      $name
205
+		 */
206
+		do_action(
207
+			'AHEE__EE_Form_Section_Proper___construct_finalize__end',
208
+			$this,
209
+			$parent_form_section,
210
+			$name
211
+		);
212
+	}
213
+
214
+
215
+	/**
216
+	 * Gets the layout strategy for this form section
217
+	 *
218
+	 * @return EE_Form_Section_Layout_Base
219
+	 */
220
+	public function get_layout_strategy()
221
+	{
222
+		return $this->_layout_strategy;
223
+	}
224
+
225
+
226
+	/**
227
+	 * Gets the HTML for a single input for this form section according
228
+	 * to the layout strategy
229
+	 *
230
+	 * @param EE_Form_Input_Base $input
231
+	 * @return string
232
+	 */
233
+	public function get_html_for_input($input)
234
+	{
235
+		return $this->_layout_strategy->layout_input($input);
236
+	}
237
+
238
+
239
+	/**
240
+	 * was_submitted - checks if form inputs are present in request data
241
+	 * Basically an alias for form_data_present_in() (which is used by both
242
+	 * proper form sections and form inputs)
243
+	 *
244
+	 * @param null $form_data
245
+	 * @return boolean
246
+	 * @throws EE_Error
247
+	 */
248
+	public function was_submitted($form_data = null)
249
+	{
250
+		return $this->form_data_present_in($form_data);
251
+	}
252
+
253
+	/**
254
+	 * Gets the cached request data; but if there is none, or $req_data was set with
255
+	 * something different, refresh the cache, and then return it
256
+	 * @param null $req_data
257
+	 * @return array
258
+	 */
259
+	protected function getCachedRequest($req_data = null)
260
+	{
261
+		if ($this->cached_request_data === null
262
+			|| (
263
+				$req_data !== null &&
264
+				$req_data !== $this->cached_request_data
265
+			)
266
+		) {
267
+			$req_data = apply_filters(
268
+				'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data',
269
+				$req_data,
270
+				$this
271
+			);
272
+			if ($req_data === null) {
273
+				$req_data = array_merge($_GET, $_POST);
274
+			}
275
+			$req_data = apply_filters(
276
+				'FHEE__EE_Form_Section_Proper__receive_form_submission__request_data',
277
+				$req_data,
278
+				$this
279
+			);
280
+			$this->cached_request_data = (array)$req_data;
281
+		}
282
+		return $this->cached_request_data;
283
+	}
284
+
285
+
286
+	/**
287
+	 * After the form section is initially created, call this to sanitize the data in the submission
288
+	 * which relates to this form section, validate it, and set it as properties on the form.
289
+	 *
290
+	 * @param array|null $req_data should usually be $_POST (the default).
291
+	 *                             However, you CAN supply a different array.
292
+	 *                             Consider using set_defaults() instead however.
293
+	 *                             (If you rendered the form in the page using echo $form_x->get_html()
294
+	 *                             the inputs will have the correct name in the request data for this function
295
+	 *                             to find them and populate the form with them.
296
+	 *                             If you have a flat form (with only input subsections),
297
+	 *                             you can supply a flat array where keys
298
+	 *                             are the form input names and values are their values)
299
+	 * @param boolean    $validate whether or not to perform validation on this data. Default is,
300
+	 *                             of course, to validate that data, and set errors on the invalid values.
301
+	 *                             But if the data has already been validated
302
+	 *                             (eg you validated the data then stored it in the DB)
303
+	 *                             you may want to skip this step.
304
+	 * @throws InvalidArgumentException
305
+	 * @throws InvalidInterfaceException
306
+	 * @throws InvalidDataTypeException
307
+	 * @throws EE_Error
308
+	 */
309
+	public function receive_form_submission($req_data = null, $validate = true)
310
+	{
311
+		$req_data = $this->getCachedRequest($req_data);
312
+		$this->_normalize($req_data);
313
+		if ($validate) {
314
+			$this->_validate();
315
+			//if it's invalid, we're going to want to re-display so remember what they submitted
316
+			if (! $this->is_valid()) {
317
+				$this->store_submitted_form_data_in_session();
318
+			}
319
+		}
320
+		if ($this->submission_error_message() === '' && ! $this->is_valid()) {
321
+			$this->set_submission_error_message();
322
+		}
323
+		do_action(
324
+			'AHEE__EE_Form_Section_Proper__receive_form_submission__end',
325
+			$req_data,
326
+			$this,
327
+			$validate
328
+		);
329
+	}
330
+
331
+
332
+	/**
333
+	 * caches the originally submitted input values in the session
334
+	 * so that they can be used to repopulate the form if it failed validation
335
+	 *
336
+	 * @return boolean whether or not the data was successfully stored in the session
337
+	 * @throws InvalidArgumentException
338
+	 * @throws InvalidInterfaceException
339
+	 * @throws InvalidDataTypeException
340
+	 * @throws EE_Error
341
+	 */
342
+	protected function store_submitted_form_data_in_session()
343
+	{
344
+		return EE_Registry::instance()->SSN->set_session_data(
345
+			array(
346
+				EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY => $this->submitted_values(true),
347
+			)
348
+		);
349
+	}
350
+
351
+
352
+	/**
353
+	 * retrieves the originally submitted input values in the session
354
+	 * so that they can be used to repopulate the form if it failed validation
355
+	 *
356
+	 * @return array
357
+	 * @throws InvalidArgumentException
358
+	 * @throws InvalidInterfaceException
359
+	 * @throws InvalidDataTypeException
360
+	 */
361
+	protected function get_submitted_form_data_from_session()
362
+	{
363
+		$session = EE_Registry::instance()->SSN;
364
+		if ($session instanceof EE_Session) {
365
+			return $session->get_session_data(
366
+				EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY
367
+			);
368
+		}
369
+		return array();
370
+	}
371
+
372
+
373
+	/**
374
+	 * flushed the originally submitted input values from the session
375
+	 *
376
+	 * @return boolean whether or not the data was successfully removed from the session
377
+	 * @throws InvalidArgumentException
378
+	 * @throws InvalidInterfaceException
379
+	 * @throws InvalidDataTypeException
380
+	 */
381
+	protected function flush_submitted_form_data_from_session()
382
+	{
383
+		return EE_Registry::instance()->SSN->reset_data(
384
+			array(EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY)
385
+		);
386
+	}
387
+
388
+
389
+	/**
390
+	 * Populates this form and its subsections with data from the session.
391
+	 * (Wrapper for EE_Form_Section_Proper::receive_form_submission, so it shows
392
+	 * validation errors when displaying too)
393
+	 * Returns true if the form was populated from the session, false otherwise
394
+	 *
395
+	 * @return boolean
396
+	 * @throws InvalidArgumentException
397
+	 * @throws InvalidInterfaceException
398
+	 * @throws InvalidDataTypeException
399
+	 * @throws EE_Error
400
+	 */
401
+	public function populate_from_session()
402
+	{
403
+		$form_data_in_session = $this->get_submitted_form_data_from_session();
404
+		if (empty($form_data_in_session)) {
405
+			return false;
406
+		}
407
+		$this->receive_form_submission($form_data_in_session);
408
+		$this->flush_submitted_form_data_from_session();
409
+		if ($this->form_data_present_in($form_data_in_session)) {
410
+			return true;
411
+		}
412
+		return false;
413
+	}
414
+
415
+
416
+	/**
417
+	 * Populates the default data for the form, given an array where keys are
418
+	 * the input names, and values are their values (preferably normalized to be their
419
+	 * proper PHP types, not all strings... although that should be ok too).
420
+	 * Proper subsections are sub-arrays, the key being the subsection's name, and
421
+	 * the value being an array formatted in teh same way
422
+	 *
423
+	 * @param array $default_data
424
+	 * @throws EE_Error
425
+	 */
426
+	public function populate_defaults($default_data)
427
+	{
428
+		foreach ($this->subsections(false) as $subsection_name => $subsection) {
429
+			if (isset($default_data[ $subsection_name ])) {
430
+				if ($subsection instanceof EE_Form_Input_Base) {
431
+					$subsection->set_default($default_data[ $subsection_name ]);
432
+				} elseif ($subsection instanceof EE_Form_Section_Proper) {
433
+					$subsection->populate_defaults($default_data[ $subsection_name ]);
434
+				}
435
+			}
436
+		}
437
+	}
438
+
439
+
440
+	/**
441
+	 * returns true if subsection exists
442
+	 *
443
+	 * @param string $name
444
+	 * @return boolean
445
+	 */
446
+	public function subsection_exists($name)
447
+	{
448
+		return isset($this->_subsections[ $name ]) ? true : false;
449
+	}
450
+
451
+
452
+	/**
453
+	 * Gets the subsection specified by its name
454
+	 *
455
+	 * @param string  $name
456
+	 * @param boolean $require_construction_to_be_finalized most client code should leave this as TRUE
457
+	 *                                                      so that the inputs will be properly configured.
458
+	 *                                                      However, some client code may be ok
459
+	 *                                                      with construction finalize being called later
460
+	 *                                                      (realizing that the subsections' html names
461
+	 *                                                      might not be set yet, etc.)
462
+	 * @return EE_Form_Section_Base
463
+	 * @throws EE_Error
464
+	 */
465
+	public function get_subsection($name, $require_construction_to_be_finalized = true)
466
+	{
467
+		if ($require_construction_to_be_finalized) {
468
+			$this->ensure_construct_finalized_called();
469
+		}
470
+		return $this->subsection_exists($name) ? $this->_subsections[ $name ] : null;
471
+	}
472
+
473
+
474
+	/**
475
+	 * Gets all the validatable subsections of this form section
476
+	 *
477
+	 * @return EE_Form_Section_Validatable[]
478
+	 * @throws EE_Error
479
+	 */
480
+	public function get_validatable_subsections()
481
+	{
482
+		$validatable_subsections = array();
483
+		foreach ($this->subsections() as $name => $obj) {
484
+			if ($obj instanceof EE_Form_Section_Validatable) {
485
+				$validatable_subsections[ $name ] = $obj;
486
+			}
487
+		}
488
+		return $validatable_subsections;
489
+	}
490
+
491
+
492
+	/**
493
+	 * Gets an input by the given name. If not found, or if its not an EE_FOrm_Input_Base child,
494
+	 * throw an EE_Error.
495
+	 *
496
+	 * @param string  $name
497
+	 * @param boolean $require_construction_to_be_finalized most client code should
498
+	 *                                                      leave this as TRUE so that the inputs will be properly
499
+	 *                                                      configured. However, some client code may be ok with
500
+	 *                                                      construction finalize being called later
501
+	 *                                                      (realizing that the subsections' html names might not be
502
+	 *                                                      set yet, etc.)
503
+	 * @return EE_Form_Input_Base
504
+	 * @throws EE_Error
505
+	 */
506
+	public function get_input($name, $require_construction_to_be_finalized = true)
507
+	{
508
+		$subsection = $this->get_subsection(
509
+			$name,
510
+			$require_construction_to_be_finalized
511
+		);
512
+		if (! $subsection instanceof EE_Form_Input_Base) {
513
+			throw new EE_Error(
514
+				sprintf(
515
+					esc_html__(
516
+						"Subsection '%s' is not an instanceof EE_Form_Input_Base on form '%s'. It is a '%s'",
517
+						'event_espresso'
518
+					),
519
+					$name,
520
+					get_class($this),
521
+					$subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso')
522
+				)
523
+			);
524
+		}
525
+		return $subsection;
526
+	}
527
+
528
+
529
+	/**
530
+	 * Like get_input(), gets the proper subsection of the form given the name,
531
+	 * otherwise throws an EE_Error
532
+	 *
533
+	 * @param string  $name
534
+	 * @param boolean $require_construction_to_be_finalized most client code should
535
+	 *                                                      leave this as TRUE so that the inputs will be properly
536
+	 *                                                      configured. However, some client code may be ok with
537
+	 *                                                      construction finalize being called later
538
+	 *                                                      (realizing that the subsections' html names might not be
539
+	 *                                                      set yet, etc.)
540
+	 * @return EE_Form_Section_Proper
541
+	 * @throws EE_Error
542
+	 */
543
+	public function get_proper_subsection($name, $require_construction_to_be_finalized = true)
544
+	{
545
+		$subsection = $this->get_subsection(
546
+			$name,
547
+			$require_construction_to_be_finalized
548
+		);
549
+		if (! $subsection instanceof EE_Form_Section_Proper) {
550
+			throw new EE_Error(
551
+				sprintf(
552
+					esc_html__(
553
+						"Subsection '%'s is not an instanceof EE_Form_Section_Proper on form '%s'",
554
+						'event_espresso'
555
+					),
556
+					$name,
557
+					get_class($this)
558
+				)
559
+			);
560
+		}
561
+		return $subsection;
562
+	}
563
+
564
+
565
+	/**
566
+	 * Gets the value of the specified input. Should be called after receive_form_submission()
567
+	 * or populate_defaults() on the form, where the normalized value on the input is set.
568
+	 *
569
+	 * @param string $name
570
+	 * @return mixed depending on the input's type and its normalization strategy
571
+	 * @throws EE_Error
572
+	 */
573
+	public function get_input_value($name)
574
+	{
575
+		$input = $this->get_input($name);
576
+		return $input->normalized_value();
577
+	}
578
+
579
+
580
+	/**
581
+	 * Checks if this form section itself is valid, and then checks its subsections
582
+	 *
583
+	 * @throws EE_Error
584
+	 * @return boolean
585
+	 */
586
+	public function is_valid()
587
+	{
588
+		if($this->is_valid === null) {
589
+			if (! $this->has_received_submission()) {
590
+				throw new EE_Error(
591
+					sprintf(
592
+						esc_html__(
593
+							'You cannot check if a form is valid before receiving the form submission using receive_form_submission',
594
+							'event_espresso'
595
+						)
596
+					)
597
+				);
598
+			}
599
+			if (! parent::is_valid()) {
600
+				$this->is_valid = false;
601
+			} else {
602
+				// ok so no general errors to this entire form section.
603
+				// so let's check the subsections, but only set errors if that hasn't been done yet
604
+				$this->is_valid = true;
605
+				foreach ($this->get_validatable_subsections() as $subsection) {
606
+					if (! $subsection->is_valid()) {
607
+						$this->is_valid = false;
608
+					}
609
+				}
610
+			}
611
+		}
612
+		return $this->is_valid;
613
+	}
614
+
615
+
616
+	/**
617
+	 * gets the default name of this form section if none is specified
618
+	 *
619
+	 * @return void
620
+	 */
621
+	protected function _set_default_name_if_empty()
622
+	{
623
+		if (! $this->_name) {
624
+			$classname    = get_class($this);
625
+			$default_name = str_replace('EE_', '', $classname);
626
+			$this->_name  = $default_name;
627
+		}
628
+	}
629
+
630
+
631
+	/**
632
+	 * Returns the HTML for the form, except for the form opening and closing tags
633
+	 * (as the form section doesn't know where you necessarily want to send the information to),
634
+	 * and except for a submit button. Enqueues JS and CSS; if called early enough we will
635
+	 * try to enqueue them in the header, otherwise they'll be enqueued in the footer.
636
+	 * Not doing_it_wrong because theoretically this CAN be used properly,
637
+	 * provided its used during "wp_enqueue_scripts", or it doesn't need to enqueue
638
+	 * any CSS.
639
+	 *
640
+	 * @throws InvalidArgumentException
641
+	 * @throws InvalidInterfaceException
642
+	 * @throws InvalidDataTypeException
643
+	 * @throws EE_Error
644
+	 */
645
+	public function get_html_and_js()
646
+	{
647
+		$this->enqueue_js();
648
+		return $this->get_html();
649
+	}
650
+
651
+
652
+	/**
653
+	 * returns HTML for displaying this form section. recursively calls display_section() on all subsections
654
+	 *
655
+	 * @param bool $display_previously_submitted_data
656
+	 * @return string
657
+	 * @throws InvalidArgumentException
658
+	 * @throws InvalidInterfaceException
659
+	 * @throws InvalidDataTypeException
660
+	 * @throws EE_Error
661
+	 * @throws EE_Error
662
+	 * @throws EE_Error
663
+	 */
664
+	public function get_html($display_previously_submitted_data = true)
665
+	{
666
+		$this->ensure_construct_finalized_called();
667
+		if ($display_previously_submitted_data) {
668
+			$this->populate_from_session();
669
+		}
670
+		return $this->_form_html_filter
671
+			? $this->_form_html_filter->filterHtml($this->_layout_strategy->layout_form(), $this)
672
+			: $this->_layout_strategy->layout_form();
673
+	}
674
+
675
+
676
+	/**
677
+	 * enqueues JS and CSS for the form.
678
+	 * It is preferred to call this before wp_enqueue_scripts so the
679
+	 * scripts and styles can be put in the header, but if called later
680
+	 * they will be put in the footer (which is OK for JS, but in HTML4 CSS should
681
+	 * only be in the header; but in HTML5 its ok in the body.
682
+	 * See http://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tag.
683
+	 * So if your form enqueues CSS, it's preferred to call this before wp_enqueue_scripts.)
684
+	 *
685
+	 * @return void
686
+	 * @throws EE_Error
687
+	 */
688
+	public function enqueue_js()
689
+	{
690
+		$this->_enqueue_and_localize_form_js();
691
+		foreach ($this->subsections() as $subsection) {
692
+			$subsection->enqueue_js();
693
+		}
694
+	}
695
+
696
+
697
+	/**
698
+	 * adds a filter so that jquery validate gets enqueued in EE_System::wp_enqueue_scripts().
699
+	 * This must be done BEFORE wp_enqueue_scripts() gets called, which is on
700
+	 * the wp_enqueue_scripts hook.
701
+	 * However, registering the form js and localizing it can happen when we
702
+	 * actually output the form (which is preferred, seeing how teh form's fields
703
+	 * could change until it's actually outputted)
704
+	 *
705
+	 * @param boolean $init_form_validation_automatically whether or not we want the form validation
706
+	 *                                                    to be triggered automatically or not
707
+	 * @return void
708
+	 */
709
+	public static function wp_enqueue_scripts($init_form_validation_automatically = true)
710
+	{
711
+		wp_register_script(
712
+			'ee_form_section_validation',
713
+			EE_GLOBAL_ASSETS_URL . 'scripts' . DS . 'form_section_validation.js',
714
+			array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'),
715
+			EVENT_ESPRESSO_VERSION,
716
+			true
717
+		);
718
+		wp_localize_script(
719
+			'ee_form_section_validation',
720
+			'ee_form_section_validation_init',
721
+			array('init' => $init_form_validation_automatically ? '1' : '0')
722
+		);
723
+	}
724
+
725
+
726
+	/**
727
+	 * gets the variables used by form_section_validation.js.
728
+	 * This needs to be called AFTER we've called $this->_enqueue_jquery_validate_script,
729
+	 * but before the wordpress hook wp_loaded
730
+	 *
731
+	 * @throws EE_Error
732
+	 */
733
+	public function _enqueue_and_localize_form_js()
734
+	{
735
+		$this->ensure_construct_finalized_called();
736
+		//actually, we don't want to localize just yet. There may be other forms on the page.
737
+		//so we need to add our form section data to a static variable accessible by all form sections
738
+		//and localize it just before the footer
739
+		$this->localize_validation_rules();
740
+		add_action('wp_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'), 2);
741
+		add_action('admin_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'));
742
+	}
743
+
744
+
745
+	/**
746
+	 * add our form section data to a static variable accessible by all form sections
747
+	 *
748
+	 * @param bool $return_for_subsection
749
+	 * @return void
750
+	 * @throws EE_Error
751
+	 */
752
+	public function localize_validation_rules($return_for_subsection = false)
753
+	{
754
+		// we only want to localize vars ONCE for the entire form,
755
+		// so if the form section doesn't have a parent, then it must be the top dog
756
+		if ($return_for_subsection || ! $this->parent_section()) {
757
+			EE_Form_Section_Proper::$_js_localization['form_data'][ $this->html_id() ] = array(
758
+				'form_section_id'  => $this->html_id(true),
759
+				'validation_rules' => $this->get_jquery_validation_rules(),
760
+				'other_data'       => $this->get_other_js_data(),
761
+				'errors'           => $this->subsection_validation_errors_by_html_name(),
762
+			);
763
+			EE_Form_Section_Proper::$_scripts_localized                                = true;
764
+		}
765
+	}
766
+
767
+
768
+	/**
769
+	 * Gets an array of extra data that will be useful for client-side javascript.
770
+	 * This is primarily data added by inputs and forms in addition to any
771
+	 * scripts they might enqueue
772
+	 *
773
+	 * @param array $form_other_js_data
774
+	 * @return array
775
+	 * @throws EE_Error
776
+	 */
777
+	public function get_other_js_data($form_other_js_data = array())
778
+	{
779
+		foreach ($this->subsections() as $subsection) {
780
+			$form_other_js_data = $subsection->get_other_js_data($form_other_js_data);
781
+		}
782
+		return $form_other_js_data;
783
+	}
784
+
785
+
786
+	/**
787
+	 * Gets a flat array of inputs for this form section and its subsections.
788
+	 * Keys are their form names, and values are the inputs themselves
789
+	 *
790
+	 * @return EE_Form_Input_Base
791
+	 * @throws EE_Error
792
+	 */
793
+	public function inputs_in_subsections()
794
+	{
795
+		$inputs = array();
796
+		foreach ($this->subsections() as $subsection) {
797
+			if ($subsection instanceof EE_Form_Input_Base) {
798
+				$inputs[ $subsection->html_name() ] = $subsection;
799
+			} elseif ($subsection instanceof EE_Form_Section_Proper) {
800
+				$inputs += $subsection->inputs_in_subsections();
801
+			}
802
+		}
803
+		return $inputs;
804
+	}
805
+
806
+
807
+	/**
808
+	 * Gets a flat array of all the validation errors.
809
+	 * Keys are html names (because those should be unique)
810
+	 * and values are a string of all their validation errors
811
+	 *
812
+	 * @return string[]
813
+	 * @throws EE_Error
814
+	 */
815
+	public function subsection_validation_errors_by_html_name()
816
+	{
817
+		$inputs = $this->inputs();
818
+		$errors = array();
819
+		foreach ($inputs as $form_input) {
820
+			if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) {
821
+				$errors[ $form_input->html_name() ] = $form_input->get_validation_error_string();
822
+			}
823
+		}
824
+		return $errors;
825
+	}
826
+
827
+
828
+	/**
829
+	 * passes all the form data required by the JS to the JS, and enqueues the few required JS files.
830
+	 * Should be setup by each form during the _enqueues_and_localize_form_js
831
+	 *
832
+	 * @throws InvalidArgumentException
833
+	 * @throws InvalidInterfaceException
834
+	 * @throws InvalidDataTypeException
835
+	 */
836
+	public static function localize_script_for_all_forms()
837
+	{
838
+		//allow inputs and stuff to hook in their JS and stuff here
839
+		do_action('AHEE__EE_Form_Section_Proper__localize_script_for_all_forms__begin');
840
+		EE_Form_Section_Proper::$_js_localization['localized_error_messages'] = EE_Form_Section_Proper::_get_localized_error_messages();
841
+		$email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level)
842
+			? EE_Registry::instance()->CFG->registration->email_validation_level
843
+			: 'wp_default';
844
+		EE_Form_Section_Proper::$_js_localization['email_validation_level']   = $email_validation_level;
845
+		wp_enqueue_script('ee_form_section_validation');
846
+		wp_localize_script(
847
+			'ee_form_section_validation',
848
+			'ee_form_section_vars',
849
+			EE_Form_Section_Proper::$_js_localization
850
+		);
851
+	}
852
+
853
+
854
+	/**
855
+	 * ensure_scripts_localized
856
+	 *
857
+	 * @throws EE_Error
858
+	 */
859
+	public function ensure_scripts_localized()
860
+	{
861
+		if (! EE_Form_Section_Proper::$_scripts_localized) {
862
+			$this->_enqueue_and_localize_form_js();
863
+		}
864
+	}
865
+
866
+
867
+	/**
868
+	 * Gets the hard-coded validation error messages to be used in the JS. The convention
869
+	 * is that the key here should be the same as the custom validation rule put in the JS file
870
+	 *
871
+	 * @return array keys are custom validation rules, and values are internationalized strings
872
+	 */
873
+	private static function _get_localized_error_messages()
874
+	{
875
+		return array(
876
+			'validUrl' => esc_html__('This is not a valid absolute URL. Eg, http://domain.com/monkey.jpg', 'event_espresso'),
877
+			'regex'    => esc_html__('Please check your input', 'event_espresso'),
878
+		);
879
+	}
880
+
881
+
882
+	/**
883
+	 * @return array
884
+	 */
885
+	public static function js_localization()
886
+	{
887
+		return self::$_js_localization;
888
+	}
889
+
890
+
891
+	/**
892
+	 * @return void
893
+	 */
894
+	public static function reset_js_localization()
895
+	{
896
+		self::$_js_localization = array();
897
+	}
898
+
899
+
900
+	/**
901
+	 * Gets the JS to put inside the jquery validation rules for subsection of this form section.
902
+	 * See parent function for more...
903
+	 *
904
+	 * @return array
905
+	 * @throws EE_Error
906
+	 */
907
+	public function get_jquery_validation_rules()
908
+	{
909
+		$jquery_validation_rules = array();
910
+		foreach ($this->get_validatable_subsections() as $subsection) {
911
+			$jquery_validation_rules = array_merge(
912
+				$jquery_validation_rules,
913
+				$subsection->get_jquery_validation_rules()
914
+			);
915
+		}
916
+		return $jquery_validation_rules;
917
+	}
918
+
919
+
920
+	/**
921
+	 * Sanitizes all the data and sets the sanitized value of each field
922
+	 *
923
+	 * @param array $req_data like $_POST
924
+	 * @return void
925
+	 * @throws EE_Error
926
+	 */
927
+	protected function _normalize($req_data)
928
+	{
929
+		$this->_received_submission = true;
930
+		$this->_validation_errors   = array();
931
+		foreach ($this->get_validatable_subsections() as $subsection) {
932
+			try {
933
+				$subsection->_normalize($req_data);
934
+			} catch (EE_Validation_Error $e) {
935
+				$subsection->add_validation_error($e);
936
+			}
937
+		}
938
+	}
939
+
940
+
941
+	/**
942
+	 * Performs validation on this form section and its subsections.
943
+	 * For each subsection,
944
+	 * calls _validate_{subsection_name} on THIS form (if the function exists)
945
+	 * and passes it the subsection, then calls _validate on that subsection.
946
+	 * If you need to perform validation on the form as a whole (considering multiple)
947
+	 * you would be best to override this _validate method,
948
+	 * calling parent::_validate() first.
949
+	 *
950
+	 * @throws EE_Error
951
+	 */
952
+	protected function _validate()
953
+	{
954
+		//reset the cache of whether this form is valid or not- we're re-validating it now
955
+		$this->is_valid = null;
956
+		foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) {
957
+			if (method_exists($this, '_validate_' . $subsection_name)) {
958
+				call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection));
959
+			}
960
+			$subsection->_validate();
961
+		}
962
+	}
963
+
964
+
965
+	/**
966
+	 * Gets all the validated inputs for the form section
967
+	 *
968
+	 * @return array
969
+	 * @throws EE_Error
970
+	 */
971
+	public function valid_data()
972
+	{
973
+		$inputs = array();
974
+		foreach ($this->subsections() as $subsection_name => $subsection) {
975
+			if ($subsection instanceof EE_Form_Section_Proper) {
976
+				$inputs[ $subsection_name ] = $subsection->valid_data();
977
+			} elseif ($subsection instanceof EE_Form_Input_Base) {
978
+				$inputs[ $subsection_name ] = $subsection->normalized_value();
979
+			}
980
+		}
981
+		return $inputs;
982
+	}
983
+
984
+
985
+	/**
986
+	 * Gets all the inputs on this form section
987
+	 *
988
+	 * @return EE_Form_Input_Base[]
989
+	 * @throws EE_Error
990
+	 */
991
+	public function inputs()
992
+	{
993
+		$inputs = array();
994
+		foreach ($this->subsections() as $subsection_name => $subsection) {
995
+			if ($subsection instanceof EE_Form_Input_Base) {
996
+				$inputs[ $subsection_name ] = $subsection;
997
+			}
998
+		}
999
+		return $inputs;
1000
+	}
1001
+
1002
+
1003
+	/**
1004
+	 * Gets all the subsections which are a proper form
1005
+	 *
1006
+	 * @return EE_Form_Section_Proper[]
1007
+	 * @throws EE_Error
1008
+	 */
1009
+	public function subforms()
1010
+	{
1011
+		$form_sections = array();
1012
+		foreach ($this->subsections() as $name => $obj) {
1013
+			if ($obj instanceof EE_Form_Section_Proper) {
1014
+				$form_sections[ $name ] = $obj;
1015
+			}
1016
+		}
1017
+		return $form_sections;
1018
+	}
1019
+
1020
+
1021
+	/**
1022
+	 * Gets all the subsections (inputs, proper subsections, or html-only sections).
1023
+	 * Consider using inputs() or subforms()
1024
+	 * if you only want form inputs or proper form sections.
1025
+	 *
1026
+	 * @param boolean $require_construction_to_be_finalized most client code should
1027
+	 *                                                      leave this as TRUE so that the inputs will be properly
1028
+	 *                                                      configured. However, some client code may be ok with
1029
+	 *                                                      construction finalize being called later
1030
+	 *                                                      (realizing that the subsections' html names might not be
1031
+	 *                                                      set yet, etc.)
1032
+	 * @return EE_Form_Section_Proper[]
1033
+	 * @throws EE_Error
1034
+	 */
1035
+	public function subsections($require_construction_to_be_finalized = true)
1036
+	{
1037
+		if ($require_construction_to_be_finalized) {
1038
+			$this->ensure_construct_finalized_called();
1039
+		}
1040
+		return $this->_subsections;
1041
+	}
1042
+
1043
+
1044
+	/**
1045
+	 * Returns whether this form has any subforms or inputs
1046
+	 * @return bool
1047
+	 */
1048
+	public function hasSubsections()
1049
+	{
1050
+		return ! empty($this->_subsections);
1051
+	}
1052
+
1053
+
1054
+	/**
1055
+	 * Returns a simple array where keys are input names, and values are their normalized
1056
+	 * values. (Similar to calling get_input_value on inputs)
1057
+	 *
1058
+	 * @param boolean $include_subform_inputs Whether to include inputs from subforms,
1059
+	 *                                        or just this forms' direct children inputs
1060
+	 * @param boolean $flatten                Whether to force the results into 1-dimensional array,
1061
+	 *                                        or allow multidimensional array
1062
+	 * @return array if $flatten is TRUE it will always be a 1-dimensional array
1063
+	 *                                        with array keys being input names
1064
+	 *                                        (regardless of whether they are from a subsection or not),
1065
+	 *                                        and if $flatten is FALSE it can be a multidimensional array
1066
+	 *                                        where keys are always subsection names and values are either
1067
+	 *                                        the input's normalized value, or an array like the top-level array
1068
+	 * @throws EE_Error
1069
+	 */
1070
+	public function input_values($include_subform_inputs = false, $flatten = false)
1071
+	{
1072
+		return $this->_input_values(false, $include_subform_inputs, $flatten);
1073
+	}
1074
+
1075
+
1076
+	/**
1077
+	 * Similar to EE_Form_Section_Proper::input_values(), except this returns the 'display_value'
1078
+	 * of each input. On some inputs (especially radio boxes or checkboxes), the value stored
1079
+	 * is not necessarily the value we want to display to users. This creates an array
1080
+	 * where keys are the input names, and values are their display values
1081
+	 *
1082
+	 * @param boolean $include_subform_inputs Whether to include inputs from subforms,
1083
+	 *                                        or just this forms' direct children inputs
1084
+	 * @param boolean $flatten                Whether to force the results into 1-dimensional array,
1085
+	 *                                        or allow multidimensional array
1086
+	 * @return array if $flatten is TRUE it will always be a 1-dimensional array
1087
+	 *                                        with array keys being input names
1088
+	 *                                        (regardless of whether they are from a subsection or not),
1089
+	 *                                        and if $flatten is FALSE it can be a multidimensional array
1090
+	 *                                        where keys are always subsection names and values are either
1091
+	 *                                        the input's normalized value, or an array like the top-level array
1092
+	 * @throws EE_Error
1093
+	 */
1094
+	public function input_pretty_values($include_subform_inputs = false, $flatten = false)
1095
+	{
1096
+		return $this->_input_values(true, $include_subform_inputs, $flatten);
1097
+	}
1098
+
1099
+
1100
+	/**
1101
+	 * Gets the input values from the form
1102
+	 *
1103
+	 * @param boolean $pretty                 Whether to retrieve the pretty value,
1104
+	 *                                        or just the normalized value
1105
+	 * @param boolean $include_subform_inputs Whether to include inputs from subforms,
1106
+	 *                                        or just this forms' direct children inputs
1107
+	 * @param boolean $flatten                Whether to force the results into 1-dimensional array,
1108
+	 *                                        or allow multidimensional array
1109
+	 * @return array if $flatten is TRUE it will always be a 1-dimensional array with array keys being
1110
+	 *                                        input names (regardless of whether they are from a subsection or not),
1111
+	 *                                        and if $flatten is FALSE it can be a multidimensional array
1112
+	 *                                        where keys are always subsection names and values are either
1113
+	 *                                        the input's normalized value, or an array like the top-level array
1114
+	 * @throws EE_Error
1115
+	 */
1116
+	public function _input_values($pretty = false, $include_subform_inputs = false, $flatten = false)
1117
+	{
1118
+		$input_values = array();
1119
+		foreach ($this->subsections() as $subsection_name => $subsection) {
1120
+			if ($subsection instanceof EE_Form_Input_Base) {
1121
+				$input_values[ $subsection_name ] = $pretty
1122
+					? $subsection->pretty_value()
1123
+					: $subsection->normalized_value();
1124
+			} elseif ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) {
1125
+				$subform_input_values = $subsection->_input_values(
1126
+					$pretty,
1127
+					$include_subform_inputs,
1128
+					$flatten
1129
+				);
1130
+				if ($flatten) {
1131
+					$input_values = array_merge($input_values, $subform_input_values);
1132
+				} else {
1133
+					$input_values[ $subsection_name ] = $subform_input_values;
1134
+				}
1135
+			}
1136
+		}
1137
+		return $input_values;
1138
+	}
1139
+
1140
+
1141
+	/**
1142
+	 * Gets the originally submitted input values from the form
1143
+	 *
1144
+	 * @param boolean $include_subforms  Whether to include inputs from subforms,
1145
+	 *                                   or just this forms' direct children inputs
1146
+	 * @return array                     if $flatten is TRUE it will always be a 1-dimensional array
1147
+	 *                                   with array keys being input names
1148
+	 *                                   (regardless of whether they are from a subsection or not),
1149
+	 *                                   and if $flatten is FALSE it can be a multidimensional array
1150
+	 *                                   where keys are always subsection names and values are either
1151
+	 *                                   the input's normalized value, or an array like the top-level array
1152
+	 * @throws EE_Error
1153
+	 */
1154
+	public function submitted_values($include_subforms = false)
1155
+	{
1156
+		$submitted_values = array();
1157
+		foreach ($this->subsections() as $subsection) {
1158
+			if ($subsection instanceof EE_Form_Input_Base) {
1159
+				// is this input part of an array of inputs?
1160
+				if (strpos($subsection->html_name(), '[') !== false) {
1161
+					$full_input_name  = EEH_Array::convert_array_values_to_keys(
1162
+						explode(
1163
+							'[',
1164
+							str_replace(']', '', $subsection->html_name())
1165
+						),
1166
+						$subsection->raw_value()
1167
+					);
1168
+					$submitted_values = array_replace_recursive($submitted_values, $full_input_name);
1169
+				} else {
1170
+					$submitted_values[ $subsection->html_name() ] = $subsection->raw_value();
1171
+				}
1172
+			} elseif ($subsection instanceof EE_Form_Section_Proper && $include_subforms) {
1173
+				$subform_input_values = $subsection->submitted_values($include_subforms);
1174
+				$submitted_values     = array_replace_recursive($submitted_values, $subform_input_values);
1175
+			}
1176
+		}
1177
+		return $submitted_values;
1178
+	}
1179
+
1180
+
1181
+	/**
1182
+	 * Indicates whether or not this form has received a submission yet
1183
+	 * (ie, had receive_form_submission called on it yet)
1184
+	 *
1185
+	 * @return boolean
1186
+	 * @throws EE_Error
1187
+	 */
1188
+	public function has_received_submission()
1189
+	{
1190
+		$this->ensure_construct_finalized_called();
1191
+		return $this->_received_submission;
1192
+	}
1193
+
1194
+
1195
+	/**
1196
+	 * Equivalent to passing 'exclude' in the constructor's options array.
1197
+	 * Removes the listed inputs from the form
1198
+	 *
1199
+	 * @param array $inputs_to_exclude values are the input names
1200
+	 * @return void
1201
+	 */
1202
+	public function exclude(array $inputs_to_exclude = array())
1203
+	{
1204
+		foreach ($inputs_to_exclude as $input_to_exclude_name) {
1205
+			unset($this->_subsections[ $input_to_exclude_name ]);
1206
+		}
1207
+	}
1208
+
1209
+
1210
+	/**
1211
+	 * @param array $inputs_to_hide
1212
+	 * @throws EE_Error
1213
+	 */
1214
+	public function hide(array $inputs_to_hide = array())
1215
+	{
1216
+		foreach ($inputs_to_hide as $input_to_hide) {
1217
+			$input = $this->get_input($input_to_hide);
1218
+			$input->set_display_strategy(new EE_Hidden_Display_Strategy());
1219
+		}
1220
+	}
1221
+
1222
+
1223
+	/**
1224
+	 * add_subsections
1225
+	 * Adds the listed subsections to the form section.
1226
+	 * If $subsection_name_to_target is provided,
1227
+	 * then new subsections are added before or after that subsection,
1228
+	 * otherwise to the start or end of the entire subsections array.
1229
+	 *
1230
+	 * @param EE_Form_Section_Base[] $new_subsections           array of new form subsections
1231
+	 *                                                          where keys are their names
1232
+	 * @param string                 $subsection_name_to_target an existing for section that $new_subsections
1233
+	 *                                                          should be added before or after
1234
+	 *                                                          IF $subsection_name_to_target is null,
1235
+	 *                                                          then $new_subsections will be added to
1236
+	 *                                                          the beginning or end of the entire subsections array
1237
+	 * @param boolean                $add_before                whether to add $new_subsections, before or after
1238
+	 *                                                          $subsection_name_to_target,
1239
+	 *                                                          or if $subsection_name_to_target is null,
1240
+	 *                                                          before or after entire subsections array
1241
+	 * @return void
1242
+	 * @throws EE_Error
1243
+	 */
1244
+	public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true)
1245
+	{
1246
+		foreach ($new_subsections as $subsection_name => $subsection) {
1247
+			if (! $subsection instanceof EE_Form_Section_Base) {
1248
+				EE_Error::add_error(
1249
+					sprintf(
1250
+						esc_html__(
1251
+							"Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.",
1252
+							'event_espresso'
1253
+						),
1254
+						get_class($subsection),
1255
+						$subsection_name,
1256
+						$this->name()
1257
+					)
1258
+				);
1259
+				unset($new_subsections[ $subsection_name ]);
1260
+			}
1261
+		}
1262
+		$this->_subsections = EEH_Array::insert_into_array(
1263
+			$this->_subsections,
1264
+			$new_subsections,
1265
+			$subsection_name_to_target,
1266
+			$add_before
1267
+		);
1268
+		if ($this->_construction_finalized) {
1269
+			foreach ($this->_subsections as $name => $subsection) {
1270
+				$subsection->_construct_finalize($this, $name);
1271
+			}
1272
+		}
1273
+	}
1274
+
1275
+
1276
+	/**
1277
+	 * Just gets all validatable subsections to clean their sensitive data
1278
+	 *
1279
+	 * @throws EE_Error
1280
+	 */
1281
+	public function clean_sensitive_data()
1282
+	{
1283
+		foreach ($this->get_validatable_subsections() as $subsection) {
1284
+			$subsection->clean_sensitive_data();
1285
+		}
1286
+	}
1287
+
1288
+
1289
+	/**
1290
+	 * Sets the submission error message (aka validation error message for this form section and all sub-sections)
1291
+	 * @param string                           $form_submission_error_message
1292
+	 * @param EE_Form_Section_Validatable $form_section unused
1293
+	 * @throws EE_Error
1294
+	 */
1295
+	public function set_submission_error_message(
1296
+		$form_submission_error_message = ''
1297
+	) {
1298
+		$this->_form_submission_error_message = ! empty($form_submission_error_message)
1299
+			? $form_submission_error_message
1300
+			: $this->getAllValidationErrorsString();
1301
+	}
1302
+
1303
+
1304
+	/**
1305
+	 * Returns the cached error message. A default value is set for this during _validate(),
1306
+	 * (called during receive_form_submission) but it can be explicitly set using
1307
+	 * set_submission_error_message
1308
+	 *
1309
+	 * @return string
1310
+	 */
1311
+	public function submission_error_message()
1312
+	{
1313
+		return $this->_form_submission_error_message;
1314
+	}
1315
+
1316
+
1317
+	/**
1318
+	 * Sets a message to display if the data submitted to the form was valid.
1319
+	 * @param string $form_submission_success_message
1320
+	 */
1321
+	public function set_submission_success_message($form_submission_success_message = '')
1322
+	{
1323
+		$this->_form_submission_success_message = ! empty($form_submission_success_message)
1324
+			? $form_submission_success_message
1325
+			: esc_html__('Form submitted successfully', 'event_espresso');
1326
+	}
1327
+
1328
+
1329
+	/**
1330
+	 * Gets a message appropriate for display when the form is correctly submitted
1331
+	 * @return string
1332
+	 */
1333
+	public function submission_success_message()
1334
+	{
1335
+		return $this->_form_submission_success_message;
1336
+	}
1337
+
1338
+
1339
+	/**
1340
+	 * Returns the prefix that should be used on child of this form section for
1341
+	 * their html names. If this form section itself has a parent, prepends ITS
1342
+	 * prefix onto this form section's prefix. Used primarily by
1343
+	 * EE_Form_Input_Base::_set_default_html_name_if_empty
1344
+	 *
1345
+	 * @return string
1346
+	 * @throws EE_Error
1347
+	 */
1348
+	public function html_name_prefix()
1349
+	{
1350
+		if ($this->parent_section() instanceof EE_Form_Section_Proper) {
1351
+			return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']';
1352
+		}
1353
+		return $this->name();
1354
+	}
1355
+
1356
+
1357
+	/**
1358
+	 * Gets the name, but first checks _construct_finalize has been called. If not,
1359
+	 * calls it (assumes there is no parent and that we want the name to be whatever
1360
+	 * was set, which is probably nothing, or the classname)
1361
+	 *
1362
+	 * @return string
1363
+	 * @throws EE_Error
1364
+	 */
1365
+	public function name()
1366
+	{
1367
+		$this->ensure_construct_finalized_called();
1368
+		return parent::name();
1369
+	}
1370
+
1371
+
1372
+	/**
1373
+	 * @return EE_Form_Section_Proper
1374
+	 * @throws EE_Error
1375
+	 */
1376
+	public function parent_section()
1377
+	{
1378
+		$this->ensure_construct_finalized_called();
1379
+		return parent::parent_section();
1380
+	}
1381
+
1382
+
1383
+	/**
1384
+	 * make sure construction finalized was called, otherwise children might not be ready
1385
+	 *
1386
+	 * @return void
1387
+	 * @throws EE_Error
1388
+	 */
1389
+	public function ensure_construct_finalized_called()
1390
+	{
1391
+		if (! $this->_construction_finalized) {
1392
+			$this->_construct_finalize($this->_parent_section, $this->_name);
1393
+		}
1394
+	}
1395
+
1396
+
1397
+	/**
1398
+	 * Checks if any of this form section's inputs, or any of its children's inputs,
1399
+	 * are in teh form data. If any are found, returns true. Else false
1400
+	 *
1401
+	 * @param array $req_data
1402
+	 * @return boolean
1403
+	 * @throws EE_Error
1404
+	 */
1405
+	public function form_data_present_in($req_data = null)
1406
+	{
1407
+		$req_data = $this->getCachedRequest($req_data);
1408
+		foreach ($this->subsections() as $subsection) {
1409
+			if ($subsection instanceof EE_Form_Input_Base) {
1410
+				if ($subsection->form_data_present_in($req_data)) {
1411
+					return true;
1412
+				}
1413
+			} elseif ($subsection instanceof EE_Form_Section_Proper) {
1414
+				if ($subsection->form_data_present_in($req_data)) {
1415
+					return true;
1416
+				}
1417
+			}
1418
+		}
1419
+		return false;
1420
+	}
1421
+
1422
+
1423
+	/**
1424
+	 * Gets validation errors for this form section and subsections
1425
+	 * Similar to EE_Form_Section_Validatable::get_validation_errors() except this
1426
+	 * gets the validation errors for ALL subsection
1427
+	 *
1428
+	 * @return EE_Validation_Error[]
1429
+	 * @throws EE_Error
1430
+	 */
1431
+	public function get_validation_errors_accumulated()
1432
+	{
1433
+		$validation_errors = $this->get_validation_errors();
1434
+		foreach ($this->get_validatable_subsections() as $subsection) {
1435
+			if ($subsection instanceof EE_Form_Section_Proper) {
1436
+				$validation_errors_on_this_subsection = $subsection->get_validation_errors_accumulated();
1437
+			} else {
1438
+				$validation_errors_on_this_subsection = $subsection->get_validation_errors();
1439
+			}
1440
+			if ($validation_errors_on_this_subsection) {
1441
+				$validation_errors = array_merge($validation_errors, $validation_errors_on_this_subsection);
1442
+			}
1443
+		}
1444
+		return $validation_errors;
1445
+	}
1446
+
1447
+	/**
1448
+	 * Fetch validation errors from children and grandchildren and puts them in a single string.
1449
+	 * This traverses the form section tree to generate this, but you probably want to instead use
1450
+	 * get_form_submission_error_message() which is usually this message cached (or a custom validation error message)
1451
+	 *
1452
+	 * @return string
1453
+	 * @since $VID:$
1454
+	 */
1455
+	protected function getAllValidationErrorsString()
1456
+	{
1457
+		$submission_error_messages = array();
1458
+		// bad, bad, bad registrant
1459
+		foreach ($this->get_validation_errors_accumulated() as $validation_error) {
1460
+			if ($validation_error instanceof EE_Validation_Error) {
1461
+				$form_section = $validation_error->get_form_section();
1462
+				if ($form_section instanceof EE_Form_Input_Base) {
1463
+				   $label = $validation_error->get_form_section()->html_label_text();
1464
+				} elseif($form_section instanceof EE_Form_Section_Validatable) {
1465
+					$label = $validation_error->get_form_section()->name();
1466
+				} else {
1467
+					$label = esc_html__('Unknown', 'event_espresso');
1468
+				}
1469
+				$submission_error_messages[] = sprintf(
1470
+					__('%s : %s', 'event_espresso'),
1471
+					$label,
1472
+					$validation_error->getMessage()
1473
+				);
1474
+			}
1475
+		}
1476
+		return implode('<br', $submission_error_messages);
1477
+	}
1478
+
1479
+
1480
+	/**
1481
+	 * This isn't just the name of an input, it's a path pointing to an input. The
1482
+	 * path is similar to a folder path: slash (/) means to descend into a subsection,
1483
+	 * dot-dot-slash (../) means to ascend into the parent section.
1484
+	 * After a series of slashes and dot-dot-slashes, there should be the name of an input,
1485
+	 * which will be returned.
1486
+	 * Eg, if you want the related input to be conditional on a sibling input name 'foobar'
1487
+	 * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name
1488
+	 * 'baz', use '../baz'. If you want it to be conditional on a cousin input,
1489
+	 * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'.
1490
+	 * Etc
1491
+	 *
1492
+	 * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false
1493
+	 * @return EE_Form_Section_Base
1494
+	 * @throws EE_Error
1495
+	 */
1496
+	public function find_section_from_path($form_section_path)
1497
+	{
1498
+		//check if we can find the input from purely going straight up the tree
1499
+		$input = parent::find_section_from_path($form_section_path);
1500
+		if ($input instanceof EE_Form_Section_Base) {
1501
+			return $input;
1502
+		}
1503
+		$next_slash_pos = strpos($form_section_path, '/');
1504
+		if ($next_slash_pos !== false) {
1505
+			$child_section_name = substr($form_section_path, 0, $next_slash_pos);
1506
+			$subpath            = substr($form_section_path, $next_slash_pos + 1);
1507
+		} else {
1508
+			$child_section_name = $form_section_path;
1509
+			$subpath            = '';
1510
+		}
1511
+		$child_section = $this->get_subsection($child_section_name);
1512
+		if ($child_section instanceof EE_Form_Section_Base) {
1513
+			return $child_section->find_section_from_path($subpath);
1514
+		}
1515
+		return null;
1516
+	}
1517 1517
 }
1518 1518
 
Please login to merge, or discard this patch.
modules/single_page_checkout/EED_Single_Page_Checkout.module.php 1 patch
Indentation   +1845 added lines, -1845 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
 use EventEspresso\core\exceptions\InvalidEntityException;
6 6
 
7 7
 if ( ! defined('EVENT_ESPRESSO_VERSION')) {
8
-    exit('No direct script access allowed');
8
+	exit('No direct script access allowed');
9 9
 }
10 10
 
11 11
 
@@ -20,1850 +20,1850 @@  discard block
 block discarded – undo
20 20
 class EED_Single_Page_Checkout extends EED_Module
21 21
 {
22 22
 
23
-    /**
24
-     * $_initialized - has the SPCO controller already been initialized ?
25
-     *
26
-     * @access private
27
-     * @var bool $_initialized
28
-     */
29
-    private static $_initialized = false;
30
-
31
-
32
-    /**
33
-     * $_checkout_verified - is the EE_Checkout verified as correct for this request ?
34
-     *
35
-     * @access private
36
-     * @var bool $_valid_checkout
37
-     */
38
-    private static $_checkout_verified = true;
39
-
40
-    /**
41
-     *    $_reg_steps_array - holds initial array of reg steps
42
-     *
43
-     * @access private
44
-     * @var array $_reg_steps_array
45
-     */
46
-    private static $_reg_steps_array = array();
47
-
48
-    /**
49
-     *    $checkout - EE_Checkout object for handling the properties of the current checkout process
50
-     *
51
-     * @access public
52
-     * @var EE_Checkout $checkout
53
-     */
54
-    public $checkout;
55
-
56
-
57
-
58
-    /**
59
-     * @return EED_Module|EED_Single_Page_Checkout
60
-     */
61
-    public static function instance()
62
-    {
63
-        add_filter('EED_Single_Page_Checkout__SPCO_active', '__return_true');
64
-        return parent::get_instance(__CLASS__);
65
-    }
66
-
67
-
68
-
69
-    /**
70
-     * @return EE_CART
71
-     */
72
-    public function cart()
73
-    {
74
-        return $this->checkout->cart;
75
-    }
76
-
77
-
78
-
79
-    /**
80
-     * @return EE_Transaction
81
-     */
82
-    public function transaction()
83
-    {
84
-        return $this->checkout->transaction;
85
-    }
86
-
87
-
88
-
89
-    /**
90
-     *    set_hooks - for hooking into EE Core, other modules, etc
91
-     *
92
-     * @access    public
93
-     * @return    void
94
-     * @throws EE_Error
95
-     */
96
-    public static function set_hooks()
97
-    {
98
-        EED_Single_Page_Checkout::set_definitions();
99
-    }
100
-
101
-
102
-
103
-    /**
104
-     *    set_hooks_admin - for hooking into EE Admin Core, other modules, etc
105
-     *
106
-     * @access    public
107
-     * @return    void
108
-     * @throws EE_Error
109
-     */
110
-    public static function set_hooks_admin()
111
-    {
112
-        EED_Single_Page_Checkout::set_definitions();
113
-        if ( ! (defined('DOING_AJAX') && DOING_AJAX)) {
114
-            return;
115
-        }
116
-        // going to start an output buffer in case anything gets accidentally output
117
-        // that might disrupt our JSON response
118
-        ob_start();
119
-        EED_Single_Page_Checkout::load_request_handler();
120
-        EED_Single_Page_Checkout::load_reg_steps();
121
-        // set ajax hooks
122
-        add_action('wp_ajax_process_reg_step', array('EED_Single_Page_Checkout', 'process_reg_step'));
123
-        add_action('wp_ajax_nopriv_process_reg_step', array('EED_Single_Page_Checkout', 'process_reg_step'));
124
-        add_action('wp_ajax_display_spco_reg_step', array('EED_Single_Page_Checkout', 'display_reg_step'));
125
-        add_action('wp_ajax_nopriv_display_spco_reg_step', array('EED_Single_Page_Checkout', 'display_reg_step'));
126
-        add_action('wp_ajax_update_reg_step', array('EED_Single_Page_Checkout', 'update_reg_step'));
127
-        add_action('wp_ajax_nopriv_update_reg_step', array('EED_Single_Page_Checkout', 'update_reg_step'));
128
-    }
129
-
130
-
131
-
132
-    /**
133
-     *    process ajax request
134
-     *
135
-     * @param string $ajax_action
136
-     * @throws EE_Error
137
-     */
138
-    public static function process_ajax_request($ajax_action)
139
-    {
140
-        EE_Registry::instance()->REQ->set('action', $ajax_action);
141
-        EED_Single_Page_Checkout::instance()->_initialize();
142
-    }
143
-
144
-
145
-
146
-    /**
147
-     *    ajax display registration step
148
-     *
149
-     * @throws EE_Error
150
-     */
151
-    public static function display_reg_step()
152
-    {
153
-        EED_Single_Page_Checkout::process_ajax_request('display_spco_reg_step');
154
-    }
155
-
156
-
157
-
158
-    /**
159
-     *    ajax process registration step
160
-     *
161
-     * @throws EE_Error
162
-     */
163
-    public static function process_reg_step()
164
-    {
165
-        EED_Single_Page_Checkout::process_ajax_request('process_reg_step');
166
-    }
167
-
168
-
169
-
170
-    /**
171
-     *    ajax process registration step
172
-     *
173
-     * @throws EE_Error
174
-     */
175
-    public static function update_reg_step()
176
-    {
177
-        EED_Single_Page_Checkout::process_ajax_request('update_reg_step');
178
-    }
179
-
180
-
181
-
182
-    /**
183
-     *   update_checkout
184
-     *
185
-     * @access public
186
-     * @return void
187
-     * @throws EE_Error
188
-     */
189
-    public static function update_checkout()
190
-    {
191
-        EED_Single_Page_Checkout::process_ajax_request('update_checkout');
192
-    }
193
-
194
-
195
-
196
-    /**
197
-     *    load_request_handler
198
-     *
199
-     * @access    public
200
-     * @return    void
201
-     */
202
-    public static function load_request_handler()
203
-    {
204
-        // load core Request_Handler class
205
-        if (EE_Registry::instance()->REQ !== null) {
206
-            EE_Registry::instance()->load_core('Request_Handler');
207
-        }
208
-    }
209
-
210
-
211
-
212
-    /**
213
-     *    set_definitions
214
-     *
215
-     * @access    public
216
-     * @return    void
217
-     * @throws EE_Error
218
-     */
219
-    public static function set_definitions()
220
-    {
221
-        if(defined('SPCO_BASE_PATH')) {
222
-            return;
223
-        }
224
-        define(
225
-            'SPCO_BASE_PATH',
226
-            rtrim(str_replace(array('\\', '/'), DS, plugin_dir_path(__FILE__)), DS) . DS
227
-        );
228
-        define('SPCO_CSS_URL', plugin_dir_url(__FILE__) . 'css' . DS);
229
-        define('SPCO_IMG_URL', plugin_dir_url(__FILE__) . 'img' . DS);
230
-        define('SPCO_JS_URL', plugin_dir_url(__FILE__) . 'js' . DS);
231
-        define('SPCO_INC_PATH', SPCO_BASE_PATH . 'inc' . DS);
232
-        define('SPCO_REG_STEPS_PATH', SPCO_BASE_PATH . 'reg_steps' . DS);
233
-        define('SPCO_TEMPLATES_PATH', SPCO_BASE_PATH . 'templates' . DS);
234
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(SPCO_BASE_PATH, true);
235
-        EE_Registry::$i18n_js_strings['registration_expiration_notice'] = sprintf(
236
-            __('%1$sWe\'re sorry, but you\'re registration time has expired.%2$s%4$sIf you still wish to complete your registration, please return to the %5$sEvent List%6$sEvent List%7$s and reselect your tickets if available. Please except our apologies for any inconvenience this may have caused.%8$s',
237
-                'event_espresso'),
238
-            '<h4 class="important-notice">',
239
-            '</h4>',
240
-            '<br />',
241
-            '<p>',
242
-            '<a href="' . get_post_type_archive_link('espresso_events') . '" title="',
243
-            '">',
244
-            '</a>',
245
-            '</p>'
246
-        );
247
-    }
248
-
249
-
250
-
251
-    /**
252
-     * load_reg_steps
253
-     * loads and instantiates each reg step based on the EE_Registry::instance()->CFG->registration->reg_steps array
254
-     *
255
-     * @access    private
256
-     * @throws EE_Error
257
-     */
258
-    public static function load_reg_steps()
259
-    {
260
-        static $reg_steps_loaded = false;
261
-        if ($reg_steps_loaded) {
262
-            return;
263
-        }
264
-        // filter list of reg_steps
265
-        $reg_steps_to_load = (array)apply_filters(
266
-            'AHEE__SPCO__load_reg_steps__reg_steps_to_load',
267
-            EED_Single_Page_Checkout::get_reg_steps()
268
-        );
269
-        // sort by key (order)
270
-        ksort($reg_steps_to_load);
271
-        // loop through folders
272
-        foreach ($reg_steps_to_load as $order => $reg_step) {
273
-            // we need a
274
-            if (isset($reg_step['file_path'], $reg_step['class_name'], $reg_step['slug'])) {
275
-                // copy over to the reg_steps_array
276
-                EED_Single_Page_Checkout::$_reg_steps_array[$order] = $reg_step;
277
-                // register custom key route for each reg step
278
-                // ie: step=>"slug" - this is the entire reason we load the reg steps array now
279
-                EE_Config::register_route(
280
-                    $reg_step['slug'],
281
-                    'EED_Single_Page_Checkout',
282
-                    'run',
283
-                    'step'
284
-                );
285
-                // add AJAX or other hooks
286
-                if (isset($reg_step['has_hooks']) && $reg_step['has_hooks']) {
287
-                    // setup autoloaders if necessary
288
-                    if ( ! class_exists($reg_step['class_name'])) {
289
-                        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(
290
-                            $reg_step['file_path'],
291
-                            true
292
-                        );
293
-                    }
294
-                    if (is_callable($reg_step['class_name'], 'set_hooks')) {
295
-                        call_user_func(array($reg_step['class_name'], 'set_hooks'));
296
-                    }
297
-                }
298
-            }
299
-        }
300
-        $reg_steps_loaded = true;
301
-    }
302
-
303
-
304
-
305
-    /**
306
-     *    get_reg_steps
307
-     *
308
-     * @access    public
309
-     * @return    array
310
-     */
311
-    public static function get_reg_steps()
312
-    {
313
-        $reg_steps = EE_Registry::instance()->CFG->registration->reg_steps;
314
-        if (empty($reg_steps)) {
315
-            $reg_steps = array(
316
-                10  => array(
317
-                    'file_path'  => SPCO_REG_STEPS_PATH . 'attendee_information',
318
-                    'class_name' => 'EE_SPCO_Reg_Step_Attendee_Information',
319
-                    'slug'       => 'attendee_information',
320
-                    'has_hooks'  => false,
321
-                ),
322
-                20  => array(
323
-                    'file_path'  => SPCO_REG_STEPS_PATH . 'registration_confirmation',
324
-                    'class_name' => 'EE_SPCO_Reg_Step_Registration_Confirmation',
325
-                    'slug'       => 'registration_confirmation',
326
-                    'has_hooks'  => false,
327
-                ),
328
-                30  => array(
329
-                    'file_path'  => SPCO_REG_STEPS_PATH . 'payment_options',
330
-                    'class_name' => 'EE_SPCO_Reg_Step_Payment_Options',
331
-                    'slug'       => 'payment_options',
332
-                    'has_hooks'  => true,
333
-                ),
334
-                999 => array(
335
-                    'file_path'  => SPCO_REG_STEPS_PATH . 'finalize_registration',
336
-                    'class_name' => 'EE_SPCO_Reg_Step_Finalize_Registration',
337
-                    'slug'       => 'finalize_registration',
338
-                    'has_hooks'  => false,
339
-                ),
340
-            );
341
-        }
342
-        return $reg_steps;
343
-    }
344
-
345
-
346
-
347
-    /**
348
-     *    registration_checkout_for_admin
349
-     *
350
-     * @access    public
351
-     * @return    string
352
-     * @throws EE_Error
353
-     */
354
-    public static function registration_checkout_for_admin()
355
-    {
356
-        EED_Single_Page_Checkout::load_request_handler();
357
-        EE_Registry::instance()->REQ->set('step', 'attendee_information');
358
-        EE_Registry::instance()->REQ->set('action', 'display_spco_reg_step');
359
-        EE_Registry::instance()->REQ->set('process_form_submission', false);
360
-        EED_Single_Page_Checkout::instance()->_initialize();
361
-        EED_Single_Page_Checkout::instance()->_display_spco_reg_form();
362
-        return EE_Registry::instance()->REQ->get_output();
363
-    }
364
-
365
-
366
-
367
-    /**
368
-     * process_registration_from_admin
369
-     *
370
-     * @access public
371
-     * @return \EE_Transaction
372
-     * @throws EE_Error
373
-     */
374
-    public static function process_registration_from_admin()
375
-    {
376
-        EED_Single_Page_Checkout::load_request_handler();
377
-        EE_Registry::instance()->REQ->set('step', 'attendee_information');
378
-        EE_Registry::instance()->REQ->set('action', 'process_reg_step');
379
-        EE_Registry::instance()->REQ->set('process_form_submission', true);
380
-        EED_Single_Page_Checkout::instance()->_initialize();
381
-        if (EED_Single_Page_Checkout::instance()->checkout->current_step->completed()) {
382
-            $final_reg_step = end(EED_Single_Page_Checkout::instance()->checkout->reg_steps);
383
-            if ($final_reg_step instanceof EE_SPCO_Reg_Step_Finalize_Registration) {
384
-                EED_Single_Page_Checkout::instance()->checkout->set_reg_step_initiated($final_reg_step);
385
-                if ($final_reg_step->process_reg_step()) {
386
-                    $final_reg_step->set_completed();
387
-                    EED_Single_Page_Checkout::instance()->checkout->update_txn_reg_steps_array();
388
-                    return EED_Single_Page_Checkout::instance()->checkout->transaction;
389
-                }
390
-            }
391
-        }
392
-        return null;
393
-    }
394
-
395
-
396
-
397
-    /**
398
-     *    run
399
-     *
400
-     * @access    public
401
-     * @param WP_Query $WP_Query
402
-     * @return    void
403
-     * @throws EE_Error
404
-     */
405
-    public function run($WP_Query)
406
-    {
407
-        if (
408
-            $WP_Query instanceof WP_Query
409
-            && $WP_Query->is_main_query()
410
-            && apply_filters('FHEE__EED_Single_Page_Checkout__run', true)
411
-            && $this->_is_reg_checkout()
412
-        ) {
413
-            $this->_initialize();
414
-        }
415
-    }
416
-
417
-
418
-
419
-    /**
420
-     * determines whether current url matches reg page url
421
-     *
422
-     * @return bool
423
-     */
424
-    protected function _is_reg_checkout()
425
-    {
426
-        // get current permalink for reg page without any extra query args
427
-        $reg_page_url = \get_permalink(EE_Config::instance()->core->reg_page_id);
428
-        // get request URI for current request, but without the scheme or host
429
-        $current_request_uri = \EEH_URL::filter_input_server_url('REQUEST_URI');
430
-        $current_request_uri = html_entity_decode($current_request_uri);
431
-        // get array of query args from the current request URI
432
-        $query_args = \EEH_URL::get_query_string($current_request_uri);
433
-        // grab page id if it is set
434
-        $page_id = isset($query_args['page_id']) ? absint($query_args['page_id']) : 0;
435
-        // and remove the page id from the query args (we will re-add it later)
436
-        unset($query_args['page_id']);
437
-        // now strip all query args from current request URI
438
-        $current_request_uri = remove_query_arg(array_keys($query_args), $current_request_uri);
439
-        // and re-add the page id if it was set
440
-        if ($page_id) {
441
-            $current_request_uri = add_query_arg('page_id', $page_id, $current_request_uri);
442
-        }
443
-        // remove slashes and ?
444
-        $current_request_uri = trim($current_request_uri, '?/');
445
-        // is current request URI part of the known full reg page URL ?
446
-        return ! empty($current_request_uri) && strpos($reg_page_url, $current_request_uri) !== false;
447
-    }
448
-
449
-
450
-
451
-    /**
452
-     * @param WP_Query $wp_query
453
-     * @return    void
454
-     * @throws EE_Error
455
-     */
456
-    public static function init($wp_query)
457
-    {
458
-        EED_Single_Page_Checkout::instance()->run($wp_query);
459
-    }
460
-
461
-
462
-
463
-    /**
464
-     *    _initialize - initial module setup
465
-     *
466
-     * @access    private
467
-     * @throws EE_Error
468
-     * @return    void
469
-     */
470
-    private function _initialize()
471
-    {
472
-        // ensure SPCO doesn't run twice
473
-        if (EED_Single_Page_Checkout::$_initialized) {
474
-            return;
475
-        }
476
-        try {
477
-            EED_Single_Page_Checkout::load_reg_steps();
478
-            $this->_verify_session();
479
-            // setup the EE_Checkout object
480
-            $this->checkout = $this->_initialize_checkout();
481
-            // filter checkout
482
-            $this->checkout = apply_filters('FHEE__EED_Single_Page_Checkout___initialize__checkout', $this->checkout);
483
-            // get the $_GET
484
-            $this->_get_request_vars();
485
-            if ($this->_block_bots()) {
486
-                return;
487
-            }
488
-            // filter continue_reg
489
-            $this->checkout->continue_reg = apply_filters(
490
-                'FHEE__EED_Single_Page_Checkout__init___continue_reg',
491
-                true,
492
-                $this->checkout
493
-            );
494
-            // load the reg steps array
495
-            if ( ! $this->_load_and_instantiate_reg_steps()) {
496
-                EED_Single_Page_Checkout::$_initialized = true;
497
-                return;
498
-            }
499
-            // set the current step
500
-            $this->checkout->set_current_step($this->checkout->step);
501
-            // and the next step
502
-            $this->checkout->set_next_step();
503
-            // verify that everything has been setup correctly
504
-            if ( ! ($this->_verify_transaction_and_get_registrations() && $this->_final_verifications())) {
505
-                EED_Single_Page_Checkout::$_initialized = true;
506
-                return;
507
-            }
508
-            // lock the transaction
509
-            $this->checkout->transaction->lock();
510
-            // make sure all of our cached objects are added to their respective model entity mappers
511
-            $this->checkout->refresh_all_entities();
512
-            // set amount owing
513
-            $this->checkout->amount_owing = $this->checkout->transaction->remaining();
514
-            // initialize each reg step, which gives them the chance to potentially alter the process
515
-            $this->_initialize_reg_steps();
516
-            // DEBUG LOG
517
-            //$this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ );
518
-            // get reg form
519
-            if( ! $this->_check_form_submission()) {
520
-                EED_Single_Page_Checkout::$_initialized = true;
521
-                return;
522
-            }
523
-            // checkout the action!!!
524
-            $this->_process_form_action();
525
-            // add some style and make it dance
526
-            $this->add_styles_and_scripts();
527
-            // kk... SPCO has successfully run
528
-            EED_Single_Page_Checkout::$_initialized = true;
529
-            // set no cache headers and constants
530
-            EE_System::do_not_cache();
531
-            // add anchor
532
-            add_action('loop_start', array($this, 'set_checkout_anchor'), 1);
533
-            // remove transaction lock
534
-            add_action('shutdown', array($this, 'unlock_transaction'), 1);
535
-        } catch (Exception $e) {
536
-            EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
537
-        }
538
-    }
539
-
540
-
541
-
542
-    /**
543
-     *    _verify_session
544
-     * checks that the session is valid and not expired
545
-     *
546
-     * @access    private
547
-     * @throws EE_Error
548
-     */
549
-    private function _verify_session()
550
-    {
551
-        if ( ! EE_Registry::instance()->SSN instanceof EE_Session) {
552
-            throw new EE_Error(__('The EE_Session class could not be loaded.', 'event_espresso'));
553
-        }
554
-        $clear_session_requested = filter_var(
555
-            EE_Registry::instance()->REQ->get('clear_session', false),
556
-            FILTER_VALIDATE_BOOLEAN
557
-        );
558
-        // is session still valid ?
559
-        if ($clear_session_requested
560
-            || ( EE_Registry::instance()->SSN->expired()
561
-              && EE_Registry::instance()->REQ->get('e_reg_url_link', '') === ''
562
-            )
563
-        ) {
564
-            $this->checkout = new EE_Checkout();
565
-            EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__);
566
-            // EE_Registry::instance()->SSN->reset_cart();
567
-            // EE_Registry::instance()->SSN->reset_checkout();
568
-            // EE_Registry::instance()->SSN->reset_transaction();
569
-            if (! $clear_session_requested) {
570
-                EE_Error::add_attention(
571
-                    EE_Registry::$i18n_js_strings['registration_expiration_notice'],
572
-                    __FILE__, __FUNCTION__, __LINE__
573
-                );
574
-            }
575
-            // EE_Registry::instance()->SSN->reset_expired();
576
-        }
577
-    }
578
-
579
-
580
-
581
-    /**
582
-     *    _initialize_checkout
583
-     * loads and instantiates EE_Checkout
584
-     *
585
-     * @access    private
586
-     * @throws EE_Error
587
-     * @return EE_Checkout
588
-     */
589
-    private function _initialize_checkout()
590
-    {
591
-        // look in session for existing checkout
592
-        /** @type EE_Checkout $checkout */
593
-        $checkout = EE_Registry::instance()->SSN->checkout();
594
-        // verify
595
-        if ( ! $checkout instanceof EE_Checkout) {
596
-            // instantiate EE_Checkout object for handling the properties of the current checkout process
597
-            $checkout = EE_Registry::instance()->load_file(
598
-                SPCO_INC_PATH,
599
-                'EE_Checkout',
600
-                'class', array(),
601
-                false
602
-            );
603
-        } else {
604
-            if ($checkout->current_step->is_final_step() && $checkout->exit_spco() === true) {
605
-                $this->unlock_transaction();
606
-                wp_safe_redirect($checkout->redirect_url);
607
-                exit();
608
-            }
609
-        }
610
-        $checkout = apply_filters('FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', $checkout);
611
-        // verify again
612
-        if ( ! $checkout instanceof EE_Checkout) {
613
-            throw new EE_Error(__('The EE_Checkout class could not be loaded.', 'event_espresso'));
614
-        }
615
-        // reset anything that needs a clean slate for each request
616
-        $checkout->reset_for_current_request();
617
-        return $checkout;
618
-    }
619
-
620
-
621
-
622
-    /**
623
-     *    _get_request_vars
624
-     *
625
-     * @access    private
626
-     * @return    void
627
-     * @throws EE_Error
628
-     */
629
-    private function _get_request_vars()
630
-    {
631
-        // load classes
632
-        EED_Single_Page_Checkout::load_request_handler();
633
-        //make sure this request is marked as belonging to EE
634
-        EE_Registry::instance()->REQ->set_espresso_page(true);
635
-        // which step is being requested ?
636
-        $this->checkout->step = EE_Registry::instance()->REQ->get('step', $this->_get_first_step());
637
-        // which step is being edited ?
638
-        $this->checkout->edit_step = EE_Registry::instance()->REQ->get('edit_step', '');
639
-        // and what we're doing on the current step
640
-        $this->checkout->action = EE_Registry::instance()->REQ->get('action', 'display_spco_reg_step');
641
-        // timestamp
642
-        $this->checkout->uts = EE_Registry::instance()->REQ->get('uts', 0);
643
-        // returning to edit ?
644
-        $this->checkout->reg_url_link = EE_Registry::instance()->REQ->get('e_reg_url_link', '');
645
-        // add reg url link to registration query params
646
-        if ($this->checkout->reg_url_link && strpos($this->checkout->reg_url_link, '1-') !== 0) {
647
-            $this->checkout->reg_cache_where_params[0]['REG_url_link'] = $this->checkout->reg_url_link;
648
-        }
649
-        // or some other kind of revisit ?
650
-        $this->checkout->revisit = filter_var(
651
-            EE_Registry::instance()->REQ->get('revisit', false),
652
-            FILTER_VALIDATE_BOOLEAN
653
-        );
654
-        // and whether or not to generate a reg form for this request
655
-        $this->checkout->generate_reg_form = filter_var(
656
-            EE_Registry::instance()->REQ->get('generate_reg_form', true),
657
-            FILTER_VALIDATE_BOOLEAN
658
-        );
659
-        // and whether or not to process a reg form submission for this request
660
-        $this->checkout->process_form_submission = filter_var(
661
-            EE_Registry::instance()->REQ->get(
662
-                'process_form_submission',
663
-                $this->checkout->action === 'process_reg_step'
664
-            ),
665
-            FILTER_VALIDATE_BOOLEAN
666
-        );
667
-        $this->checkout->process_form_submission = filter_var(
668
-            $this->checkout->action !== 'display_spco_reg_step'
669
-                ? $this->checkout->process_form_submission
670
-                : false,
671
-            FILTER_VALIDATE_BOOLEAN
672
-        );
673
-        // $this->_display_request_vars();
674
-    }
675
-
676
-
677
-
678
-    /**
679
-     *  _display_request_vars
680
-     *
681
-     * @access    protected
682
-     * @return    void
683
-     */
684
-    protected function _display_request_vars()
685
-    {
686
-        if ( ! WP_DEBUG) {
687
-            return;
688
-        }
689
-        EEH_Debug_Tools::printr($_REQUEST, '$_REQUEST', __FILE__, __LINE__);
690
-        EEH_Debug_Tools::printr($this->checkout->step, '$this->checkout->step', __FILE__, __LINE__);
691
-        EEH_Debug_Tools::printr($this->checkout->edit_step, '$this->checkout->edit_step', __FILE__, __LINE__);
692
-        EEH_Debug_Tools::printr($this->checkout->action, '$this->checkout->action', __FILE__, __LINE__);
693
-        EEH_Debug_Tools::printr($this->checkout->reg_url_link, '$this->checkout->reg_url_link', __FILE__, __LINE__);
694
-        EEH_Debug_Tools::printr($this->checkout->revisit, '$this->checkout->revisit', __FILE__, __LINE__);
695
-        EEH_Debug_Tools::printr($this->checkout->generate_reg_form, '$this->checkout->generate_reg_form', __FILE__, __LINE__);
696
-        EEH_Debug_Tools::printr($this->checkout->process_form_submission, '$this->checkout->process_form_submission', __FILE__, __LINE__);
697
-    }
698
-
699
-
700
-
701
-    /**
702
-     * _block_bots
703
-     * checks that the incoming request has either of the following set:
704
-     *  a uts (unix timestamp) which indicates that the request was redirected from the Ticket Selector
705
-     *  a REG URL Link, which indicates that the request is a return visit to SPCO for a valid TXN
706
-     * so if you're not coming from the Ticket Selector nor returning for a valid IP...
707
-     * then where you coming from man?
708
-     *
709
-     * @return boolean
710
-     */
711
-    private function _block_bots()
712
-    {
713
-        $invalid_checkout_access = EED_Invalid_Checkout_Access::getInvalidCheckoutAccess();
714
-        if ($invalid_checkout_access->checkoutAccessIsInvalid($this->checkout)) {
715
-            return true;
716
-        }
717
-        return false;
718
-    }
719
-
720
-
721
-
722
-    /**
723
-     *    _get_first_step
724
-     *  gets slug for first step in $_reg_steps_array
725
-     *
726
-     * @access    private
727
-     * @throws EE_Error
728
-     * @return    string
729
-     */
730
-    private function _get_first_step()
731
-    {
732
-        $first_step = reset(EED_Single_Page_Checkout::$_reg_steps_array);
733
-        return isset($first_step['slug']) ? $first_step['slug'] : 'attendee_information';
734
-    }
735
-
736
-
737
-
738
-    /**
739
-     *    _load_and_instantiate_reg_steps
740
-     *  instantiates each reg step based on the loaded reg_steps array
741
-     *
742
-     * @access    private
743
-     * @throws EE_Error
744
-     * @return    bool
745
-     */
746
-    private function _load_and_instantiate_reg_steps()
747
-    {
748
-        do_action('AHEE__Single_Page_Checkout___load_and_instantiate_reg_steps__start', $this->checkout);
749
-        // have reg_steps already been instantiated ?
750
-        if (
751
-            empty($this->checkout->reg_steps)
752
-            || apply_filters('FHEE__Single_Page_Checkout__load_reg_steps__reload_reg_steps', false, $this->checkout)
753
-        ) {
754
-            // if not, then loop through raw reg steps array
755
-            foreach (EED_Single_Page_Checkout::$_reg_steps_array as $order => $reg_step) {
756
-                if ( ! $this->_load_and_instantiate_reg_step($reg_step, $order)) {
757
-                    return false;
758
-                }
759
-            }
760
-            EE_Registry::instance()->CFG->registration->skip_reg_confirmation = true;
761
-            EE_Registry::instance()->CFG->registration->reg_confirmation_last = true;
762
-            // skip the registration_confirmation page ?
763
-            if (EE_Registry::instance()->CFG->registration->skip_reg_confirmation) {
764
-                // just remove it from the reg steps array
765
-                $this->checkout->remove_reg_step('registration_confirmation', false);
766
-            } else if (
767
-                isset($this->checkout->reg_steps['registration_confirmation'])
768
-                && EE_Registry::instance()->CFG->registration->reg_confirmation_last
769
-            ) {
770
-                // set the order to something big like 100
771
-                $this->checkout->set_reg_step_order('registration_confirmation', 100);
772
-            }
773
-            // filter the array for good luck
774
-            $this->checkout->reg_steps = apply_filters(
775
-                'FHEE__Single_Page_Checkout__load_reg_steps__reg_steps',
776
-                $this->checkout->reg_steps
777
-            );
778
-            // finally re-sort based on the reg step class order properties
779
-            $this->checkout->sort_reg_steps();
780
-        } else {
781
-            foreach ($this->checkout->reg_steps as $reg_step) {
782
-                // set all current step stati to FALSE
783
-                $reg_step->set_is_current_step(false);
784
-            }
785
-        }
786
-        if (empty($this->checkout->reg_steps)) {
787
-            EE_Error::add_error(
788
-                __('No Reg Steps were loaded..', 'event_espresso'),
789
-                __FILE__, __FUNCTION__, __LINE__
790
-            );
791
-            return false;
792
-        }
793
-        // make reg step details available to JS
794
-        $this->checkout->set_reg_step_JSON_info();
795
-        return true;
796
-    }
797
-
798
-
799
-
800
-    /**
801
-     *     _load_and_instantiate_reg_step
802
-     *
803
-     * @access    private
804
-     * @param array $reg_step
805
-     * @param int   $order
806
-     * @return bool
807
-     */
808
-    private function _load_and_instantiate_reg_step($reg_step = array(), $order = 0)
809
-    {
810
-        // we need a file_path, class_name, and slug to add a reg step
811
-        if (isset($reg_step['file_path'], $reg_step['class_name'], $reg_step['slug'])) {
812
-            // if editing a specific step, but this is NOT that step... (and it's not the 'finalize_registration' step)
813
-            if (
814
-                $this->checkout->reg_url_link
815
-                && $this->checkout->step !== $reg_step['slug']
816
-                && $reg_step['slug'] !== 'finalize_registration'
817
-                // normally at this point we would NOT load the reg step, but this filter can change that
818
-                && apply_filters(
819
-                    'FHEE__Single_Page_Checkout___load_and_instantiate_reg_step__bypass_reg_step',
820
-                    true,
821
-                    $reg_step,
822
-                    $this->checkout
823
-                )
824
-            ) {
825
-                return true;
826
-            }
827
-            // instantiate step class using file path and class name
828
-            $reg_step_obj = EE_Registry::instance()->load_file(
829
-                $reg_step['file_path'],
830
-                $reg_step['class_name'],
831
-                'class',
832
-                $this->checkout,
833
-                false
834
-            );
835
-            // did we gets the goods ?
836
-            if ($reg_step_obj instanceof EE_SPCO_Reg_Step) {
837
-                // set reg step order based on config
838
-                $reg_step_obj->set_order($order);
839
-                // add instantiated reg step object to the master reg steps array
840
-                $this->checkout->add_reg_step($reg_step_obj);
841
-            } else {
842
-                EE_Error::add_error(
843
-                    __('The current step could not be set.', 'event_espresso'),
844
-                    __FILE__, __FUNCTION__, __LINE__
845
-                );
846
-                return false;
847
-            }
848
-        } else {
849
-            if (WP_DEBUG) {
850
-                EE_Error::add_error(
851
-                    sprintf(
852
-                        __(
853
-                            'A registration step could not be loaded. One or more of the following data points is invalid:%4$s%5$sFile Path: %1$s%6$s%5$sClass Name: %2$s%6$s%5$sSlug: %3$s%6$s%7$s',
854
-                            'event_espresso'
855
-                        ),
856
-                        isset($reg_step['file_path']) ? $reg_step['file_path'] : '',
857
-                        isset($reg_step['class_name']) ? $reg_step['class_name'] : '',
858
-                        isset($reg_step['slug']) ? $reg_step['slug'] : '',
859
-                        '<ul>',
860
-                        '<li>',
861
-                        '</li>',
862
-                        '</ul>'
863
-                    ),
864
-                    __FILE__, __FUNCTION__, __LINE__
865
-                );
866
-            }
867
-            return false;
868
-        }
869
-        return true;
870
-    }
871
-
872
-
873
-    /**
874
-     * _verify_transaction_and_get_registrations
875
-     *
876
-     * @access private
877
-     * @return bool
878
-     * @throws InvalidDataTypeException
879
-     * @throws InvalidEntityException
880
-     * @throws EE_Error
881
-     */
882
-    private function _verify_transaction_and_get_registrations()
883
-    {
884
-        // was there already a valid transaction in the checkout from the session ?
885
-        if ( ! $this->checkout->transaction instanceof EE_Transaction) {
886
-            // get transaction from db or session
887
-            $this->checkout->transaction = $this->checkout->reg_url_link && ! is_admin()
888
-                ? $this->_get_transaction_and_cart_for_previous_visit()
889
-                : $this->_get_cart_for_current_session_and_setup_new_transaction();
890
-            if ( ! $this->checkout->transaction instanceof EE_Transaction) {
891
-                EE_Error::add_error(
892
-                    __('Your Registration and Transaction information could not be retrieved from the db.',
893
-                        'event_espresso'),
894
-                    __FILE__, __FUNCTION__, __LINE__
895
-                );
896
-                $this->checkout->transaction = EE_Transaction::new_instance();
897
-                // add some style and make it dance
898
-                $this->add_styles_and_scripts();
899
-                EED_Single_Page_Checkout::$_initialized = true;
900
-                return false;
901
-            }
902
-            // and the registrations for the transaction
903
-            $this->_get_registrations($this->checkout->transaction);
904
-        }
905
-        return true;
906
-    }
907
-
908
-
909
-
910
-    /**
911
-     * _get_transaction_and_cart_for_previous_visit
912
-     *
913
-     * @access private
914
-     * @return mixed EE_Transaction|NULL
915
-     */
916
-    private function _get_transaction_and_cart_for_previous_visit()
917
-    {
918
-        /** @var $TXN_model EEM_Transaction */
919
-        $TXN_model = EE_Registry::instance()->load_model('Transaction');
920
-        // because the reg_url_link is present in the request,
921
-        // this is a return visit to SPCO, so we'll get the transaction data from the db
922
-        $transaction = $TXN_model->get_transaction_from_reg_url_link($this->checkout->reg_url_link);
923
-        // verify transaction
924
-        if ($transaction instanceof EE_Transaction) {
925
-            // and get the cart that was used for that transaction
926
-            $this->checkout->cart = $this->_get_cart_for_transaction($transaction);
927
-            return $transaction;
928
-        }
929
-        EE_Error::add_error(
930
-            __('Your Registration and Transaction information could not be retrieved from the db.', 'event_espresso'),
931
-            __FILE__, __FUNCTION__, __LINE__
932
-        );
933
-        return null;
934
-
935
-    }
936
-
937
-
938
-
939
-    /**
940
-     * _get_cart_for_transaction
941
-     *
942
-     * @access private
943
-     * @param EE_Transaction $transaction
944
-     * @return EE_Cart
945
-     */
946
-    private function _get_cart_for_transaction($transaction)
947
-    {
948
-        return $this->checkout->get_cart_for_transaction($transaction);
949
-    }
950
-
951
-
952
-
953
-    /**
954
-     * get_cart_for_transaction
955
-     *
956
-     * @access public
957
-     * @param EE_Transaction $transaction
958
-     * @return EE_Cart
959
-     */
960
-    public function get_cart_for_transaction(EE_Transaction $transaction)
961
-    {
962
-        return $this->checkout->get_cart_for_transaction($transaction);
963
-    }
964
-
965
-
966
-
967
-    /**
968
-     * _get_transaction_and_cart_for_current_session
969
-     *    generates a new EE_Transaction object and adds it to the $_transaction property.
970
-     *
971
-     * @access private
972
-     * @return EE_Transaction
973
-     * @throws EE_Error
974
-     */
975
-    private function _get_cart_for_current_session_and_setup_new_transaction()
976
-    {
977
-        //  if there's no transaction, then this is the FIRST visit to SPCO
978
-        // so load up the cart ( passing nothing for the TXN because it doesn't exist yet )
979
-        $this->checkout->cart = $this->_get_cart_for_transaction(null);
980
-        // and then create a new transaction
981
-        $transaction = $this->_initialize_transaction();
982
-        // verify transaction
983
-        if ($transaction instanceof EE_Transaction) {
984
-            // save it so that we have an ID for other objects to use
985
-            $transaction->save();
986
-            // and save TXN data to the cart
987
-            $this->checkout->cart->get_grand_total()->save_this_and_descendants_to_txn($transaction->ID());
988
-        } else {
989
-            EE_Error::add_error(
990
-                __('A Valid Transaction could not be initialized.', 'event_espresso'),
991
-                __FILE__, __FUNCTION__, __LINE__
992
-            );
993
-        }
994
-        return $transaction;
995
-    }
996
-
997
-
998
-
999
-    /**
1000
-     *    generates a new EE_Transaction object and adds it to the $_transaction property.
1001
-     *
1002
-     * @access private
1003
-     * @return mixed EE_Transaction|NULL
1004
-     */
1005
-    private function _initialize_transaction()
1006
-    {
1007
-        try {
1008
-            // ensure cart totals have been calculated
1009
-            $this->checkout->cart->get_grand_total()->recalculate_total_including_taxes();
1010
-            // grab the cart grand total
1011
-            $cart_total = $this->checkout->cart->get_cart_grand_total();
1012
-            // create new TXN
1013
-            $transaction = EE_Transaction::new_instance(
1014
-                array(
1015
-                    'TXN_reg_steps' => $this->checkout->initialize_txn_reg_steps_array(),
1016
-                    'TXN_total'     => $cart_total > 0 ? $cart_total : 0,
1017
-                    'TXN_paid'      => 0,
1018
-                    'STS_ID'        => EEM_Transaction::failed_status_code,
1019
-                )
1020
-            );
1021
-            // save it so that we have an ID for other objects to use
1022
-            $transaction->save();
1023
-            // set cron job for following up on TXNs after their session has expired
1024
-            EE_Cron_Tasks::schedule_expired_transaction_check(
1025
-                EE_Registry::instance()->SSN->expiration() + 1,
1026
-                $transaction->ID()
1027
-            );
1028
-            return $transaction;
1029
-        } catch (Exception $e) {
1030
-            EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
1031
-        }
1032
-        return null;
1033
-    }
1034
-
1035
-
1036
-    /**
1037
-     * _get_registrations
1038
-     *
1039
-     * @access private
1040
-     * @param EE_Transaction $transaction
1041
-     * @return void
1042
-     * @throws InvalidDataTypeException
1043
-     * @throws InvalidEntityException
1044
-     * @throws EE_Error
1045
-     */
1046
-    private function _get_registrations(EE_Transaction $transaction)
1047
-    {
1048
-        // first step: grab the registrants  { : o
1049
-        $registrations = $transaction->registrations($this->checkout->reg_cache_where_params, false);
1050
-        $this->checkout->total_ticket_count = count($registrations);
1051
-        // verify registrations have been set
1052
-        if (empty($registrations)) {
1053
-            // if no cached registrations, then check the db
1054
-            $registrations = $transaction->registrations($this->checkout->reg_cache_where_params, false);
1055
-            // still nothing ? well as long as this isn't a revisit
1056
-            if (empty($registrations) && ! $this->checkout->revisit) {
1057
-                // generate new registrations from scratch
1058
-                $registrations = $this->_initialize_registrations($transaction);
1059
-            }
1060
-        }
1061
-        // sort by their original registration order
1062
-        usort($registrations, array('EED_Single_Page_Checkout', 'sort_registrations_by_REG_count'));
1063
-        // then loop thru the array
1064
-        foreach ($registrations as $registration) {
1065
-            // verify each registration
1066
-            if ($registration instanceof EE_Registration) {
1067
-                // we display all attendee info for the primary registrant
1068
-                if ($this->checkout->reg_url_link === $registration->reg_url_link()
1069
-                    && $registration->is_primary_registrant()
1070
-                ) {
1071
-                    $this->checkout->primary_revisit = true;
1072
-                    break;
1073
-                }
1074
-                if ($this->checkout->revisit && $this->checkout->reg_url_link !== $registration->reg_url_link()) {
1075
-                    // but hide info if it doesn't belong to you
1076
-                    $transaction->clear_cache('Registration', $registration->ID());
1077
-                    $this->checkout->total_ticket_count--;
1078
-                }
1079
-                $this->checkout->set_reg_status_updated($registration->ID(), false);
1080
-            }
1081
-        }
1082
-    }
1083
-
1084
-
1085
-    /**
1086
-     *    adds related EE_Registration objects for each ticket in the cart to the current EE_Transaction object
1087
-     *
1088
-     * @access private
1089
-     * @param EE_Transaction $transaction
1090
-     * @return    array
1091
-     * @throws InvalidDataTypeException
1092
-     * @throws InvalidEntityException
1093
-     * @throws EE_Error
1094
-     */
1095
-    private function _initialize_registrations(EE_Transaction $transaction)
1096
-    {
1097
-        $att_nmbr = 0;
1098
-        $registrations = array();
1099
-        if ($transaction instanceof EE_Transaction) {
1100
-            /** @type EE_Registration_Processor $registration_processor */
1101
-            $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
1102
-            $this->checkout->total_ticket_count = $this->checkout->cart->all_ticket_quantity_count();
1103
-            // now let's add the cart items to the $transaction
1104
-            foreach ($this->checkout->cart->get_tickets() as $line_item) {
1105
-                //do the following for each ticket of this type they selected
1106
-                for ($x = 1; $x <= $line_item->quantity(); $x++) {
1107
-                    $att_nmbr++;
1108
-                    /** @var EventEspresso\core\services\commands\registration\CreateRegistrationCommand $CreateRegistrationCommand */
1109
-                    $CreateRegistrationCommand = EE_Registry::instance()->create(
1110
-                        'EventEspresso\core\services\commands\registration\CreateRegistrationCommand',
1111
-                        array(
1112
-                            $transaction,
1113
-                            $line_item,
1114
-                            $att_nmbr,
1115
-                            $this->checkout->total_ticket_count,
1116
-                        )
1117
-                    );
1118
-                    // override capabilities for frontend registrations
1119
-                    if ( ! is_admin()) {
1120
-                        $CreateRegistrationCommand->setCapCheck(
1121
-                            new PublicCapabilities('', 'create_new_registration')
1122
-                        );
1123
-                    }
1124
-                    $registration = EE_Registry::instance()->BUS->execute($CreateRegistrationCommand);
1125
-                    if ( ! $registration instanceof EE_Registration) {
1126
-                        throw new InvalidEntityException($registration, 'EE_Registration');
1127
-                    }
1128
-                    $registrations[ $registration->ID() ] = $registration;
1129
-                }
1130
-            }
1131
-            $registration_processor->fix_reg_final_price_rounding_issue($transaction);
1132
-        }
1133
-        return $registrations;
1134
-    }
1135
-
1136
-
1137
-
1138
-    /**
1139
-     * sorts registrations by REG_count
1140
-     *
1141
-     * @access public
1142
-     * @param EE_Registration $reg_A
1143
-     * @param EE_Registration $reg_B
1144
-     * @return int
1145
-     */
1146
-    public static function sort_registrations_by_REG_count(EE_Registration $reg_A, EE_Registration $reg_B)
1147
-    {
1148
-        // this shouldn't ever happen within the same TXN, but oh well
1149
-        if ($reg_A->count() === $reg_B->count()) {
1150
-            return 0;
1151
-        }
1152
-        return ($reg_A->count() > $reg_B->count()) ? 1 : -1;
1153
-    }
1154
-
1155
-
1156
-
1157
-    /**
1158
-     *    _final_verifications
1159
-     * just makes sure that everything is set up correctly before proceeding
1160
-     *
1161
-     * @access    private
1162
-     * @return    bool
1163
-     * @throws EE_Error
1164
-     */
1165
-    private function _final_verifications()
1166
-    {
1167
-        // filter checkout
1168
-        $this->checkout = apply_filters(
1169
-            'FHEE__EED_Single_Page_Checkout___final_verifications__checkout',
1170
-            $this->checkout
1171
-        );
1172
-        //verify that current step is still set correctly
1173
-        if ( ! $this->checkout->current_step instanceof EE_SPCO_Reg_Step) {
1174
-            EE_Error::add_error(
1175
-                __('We\'re sorry but the registration process can not proceed because one or more registration steps were not setup correctly. Please refresh the page and try again or contact support.', 'event_espresso'),
1176
-                __FILE__,
1177
-                __FUNCTION__,
1178
-                __LINE__
1179
-            );
1180
-            return false;
1181
-        }
1182
-        // if returning to SPCO, then verify that primary registrant is set
1183
-        if ( ! empty($this->checkout->reg_url_link)) {
1184
-            $valid_registrant = $this->checkout->transaction->primary_registration();
1185
-            if ( ! $valid_registrant instanceof EE_Registration) {
1186
-                EE_Error::add_error(
1187
-                    __('We\'re sorry but there appears to be an error with the "reg_url_link" or the primary registrant for this transaction. Please refresh the page and try again or contact support.', 'event_espresso'),
1188
-                    __FILE__,
1189
-                    __FUNCTION__,
1190
-                    __LINE__
1191
-                );
1192
-                return false;
1193
-            }
1194
-            $valid_registrant = null;
1195
-            foreach (
1196
-                $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params) as $registration
1197
-            ) {
1198
-                if (
1199
-                    $registration instanceof EE_Registration
1200
-                    && $registration->reg_url_link() === $this->checkout->reg_url_link
1201
-                ) {
1202
-                    $valid_registrant = $registration;
1203
-                }
1204
-            }
1205
-            if ( ! $valid_registrant instanceof EE_Registration) {
1206
-                // hmmm... maybe we have the wrong session because the user is opening multiple tabs ?
1207
-                if (EED_Single_Page_Checkout::$_checkout_verified) {
1208
-                    // clear the session, mark the checkout as unverified, and try again
1209
-                    EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__);
1210
-                    EED_Single_Page_Checkout::$_initialized = false;
1211
-                    EED_Single_Page_Checkout::$_checkout_verified = false;
1212
-                    $this->_initialize();
1213
-                    EE_Error::reset_notices();
1214
-                    return false;
1215
-                }
1216
-                EE_Error::add_error(
1217
-                    __(
1218
-                        'We\'re sorry but there appears to be an error with the "reg_url_link" or the transaction itself. Please refresh the page and try again or contact support.',
1219
-                        'event_espresso'
1220
-                    ),
1221
-                    __FILE__,
1222
-                    __FUNCTION__,
1223
-                    __LINE__
1224
-                );
1225
-                return false;
1226
-            }
1227
-        }
1228
-        // now that things have been kinda sufficiently verified,
1229
-        // let's add the checkout to the session so that it's available to other systems
1230
-        EE_Registry::instance()->SSN->set_checkout($this->checkout);
1231
-        return true;
1232
-    }
1233
-
1234
-
1235
-
1236
-    /**
1237
-     *    _initialize_reg_steps
1238
-     * first makes sure that EE_Transaction_Processor::set_reg_step_initiated() is called as required
1239
-     * then loops thru all of the active reg steps and calls the initialize_reg_step() method
1240
-     *
1241
-     * @access    private
1242
-     * @param bool $reinitializing
1243
-     * @throws EE_Error
1244
-     */
1245
-    private function _initialize_reg_steps($reinitializing = false)
1246
-    {
1247
-        $this->checkout->set_reg_step_initiated($this->checkout->current_step);
1248
-        // loop thru all steps to call their individual "initialize" methods and set i18n strings for JS
1249
-        foreach ($this->checkout->reg_steps as $reg_step) {
1250
-            if ( ! $reg_step->initialize_reg_step()) {
1251
-                // if not initialized then maybe this step is being removed...
1252
-                if ( ! $reinitializing && $reg_step->is_current_step()) {
1253
-                    // if it was the current step, then we need to start over here
1254
-                    $this->_initialize_reg_steps(true);
1255
-                    return;
1256
-                }
1257
-                continue;
1258
-            }
1259
-            // add css and JS for current step
1260
-            $reg_step->enqueue_styles_and_scripts();
1261
-            // i18n
1262
-            $reg_step->translate_js_strings();
1263
-            if ($reg_step->is_current_step()) {
1264
-                // the text that appears on the reg step form submit button
1265
-                $reg_step->set_submit_button_text();
1266
-            }
1267
-        }
1268
-        // dynamically creates hook point like: AHEE__Single_Page_Checkout___initialize_reg_step__attendee_information
1269
-        do_action(
1270
-            "AHEE__Single_Page_Checkout___initialize_reg_step__{$this->checkout->current_step->slug()}",
1271
-            $this->checkout->current_step
1272
-        );
1273
-    }
1274
-
1275
-
1276
-
1277
-    /**
1278
-     * _check_form_submission
1279
-     *
1280
-     * @access private
1281
-     * @return boolean
1282
-     */
1283
-    private function _check_form_submission()
1284
-    {
1285
-        //does this request require the reg form to be generated ?
1286
-        if ($this->checkout->generate_reg_form) {
1287
-            // ever heard that song by Blue Rodeo ?
1288
-            try {
1289
-                $this->checkout->current_step->reg_form = $this->checkout->current_step->generate_reg_form();
1290
-                // if not displaying a form, then check for form submission
1291
-                if (
1292
-                    $this->checkout->process_form_submission
1293
-                    && $this->checkout->current_step->reg_form->was_submitted()
1294
-                ) {
1295
-                    // clear out any old data in case this step is being run again
1296
-                    $this->checkout->current_step->set_valid_data(array());
1297
-                    // capture submitted form data
1298
-                    $this->checkout->current_step->reg_form->receive_form_submission(
1299
-                        apply_filters(
1300
-                            'FHEE__Single_Page_Checkout___check_form_submission__request_params',
1301
-                            EE_Registry::instance()->REQ->params(),
1302
-                            $this->checkout
1303
-                        )
1304
-                    );
1305
-                    // validate submitted form data
1306
-                    if ( ! $this->checkout->continue_reg || ! $this->checkout->current_step->reg_form->is_valid()) {
1307
-                        // thou shall not pass !!!
1308
-                        $this->checkout->continue_reg = false;
1309
-                        // any form validation errors?
1310
-                        if ($this->checkout->current_step->reg_form->submission_error_message() !== '') {
1311
-                            EE_Error::add_error(
1312
-                                $this->checkout->current_step->reg_form->submission_error_message(),
1313
-                                __FILE__, __FUNCTION__, __LINE__
1314
-                            );
1315
-                        }
1316
-                        // well not really... what will happen is
1317
-                        // we'll just get redirected back to redo the current step
1318
-                        $this->go_to_next_step();
1319
-                        return false;
1320
-                    }
1321
-                }
1322
-            } catch (EE_Error $e) {
1323
-                $e->get_error();
1324
-            }
1325
-        }
1326
-        return true;
1327
-    }
1328
-
1329
-
1330
-
1331
-    /**
1332
-     * _process_action
1333
-     *
1334
-     * @access private
1335
-     * @return void
1336
-     * @throws EE_Error
1337
-     */
1338
-    private function _process_form_action()
1339
-    {
1340
-        // what cha wanna do?
1341
-        switch ($this->checkout->action) {
1342
-            // AJAX next step reg form
1343
-            case 'display_spco_reg_step' :
1344
-                $this->checkout->redirect = false;
1345
-                if (EE_Registry::instance()->REQ->ajax) {
1346
-                    $this->checkout->json_response->set_reg_step_html(
1347
-                        $this->checkout->current_step->display_reg_form()
1348
-                    );
1349
-                }
1350
-                break;
1351
-            default :
1352
-                // meh... do one of those other steps first
1353
-                if (
1354
-                    ! empty($this->checkout->action)
1355
-                    && is_callable(array($this->checkout->current_step, $this->checkout->action))
1356
-                ) {
1357
-                    // dynamically creates hook point like:
1358
-                    //   AHEE__Single_Page_Checkout__before_attendee_information__process_reg_step
1359
-                    do_action(
1360
-                        "AHEE__Single_Page_Checkout__before_{$this->checkout->current_step->slug()}__{$this->checkout->action}",
1361
-                        $this->checkout->current_step
1362
-                    );
1363
-                    // call action on current step
1364
-                    if (call_user_func(array($this->checkout->current_step, $this->checkout->action))) {
1365
-                        // good registrant, you get to proceed
1366
-                        if (
1367
-                            $this->checkout->current_step->success_message() !== ''
1368
-                            && apply_filters(
1369
-                                'FHEE__Single_Page_Checkout___process_form_action__display_success',
1370
-                                false
1371
-                            )
1372
-                        ) {
1373
-                            EE_Error::add_success(
1374
-                                $this->checkout->current_step->success_message()
1375
-                                . '<br />' . $this->checkout->next_step->_instructions()
1376
-                            );
1377
-                        }
1378
-                        // pack it up, pack it in...
1379
-                        $this->_setup_redirect();
1380
-                    }
1381
-                    // dynamically creates hook point like:
1382
-                    //  AHEE__Single_Page_Checkout__after_payment_options__process_reg_step
1383
-                    do_action(
1384
-                        "AHEE__Single_Page_Checkout__after_{$this->checkout->current_step->slug()}__{$this->checkout->action}",
1385
-                        $this->checkout->current_step
1386
-                    );
1387
-                } else {
1388
-                    EE_Error::add_error(
1389
-                        sprintf(
1390
-                            __(
1391
-                                'The requested form action "%s" does not exist for the current "%s" registration step.',
1392
-                                'event_espresso'
1393
-                            ),
1394
-                            $this->checkout->action,
1395
-                            $this->checkout->current_step->name()
1396
-                        ),
1397
-                        __FILE__,
1398
-                        __FUNCTION__,
1399
-                        __LINE__
1400
-                    );
1401
-                }
1402
-            // end default
1403
-        }
1404
-        // store our progress so far
1405
-        $this->checkout->stash_transaction_and_checkout();
1406
-        // advance to the next step! If you pass GO, collect $200
1407
-        $this->go_to_next_step();
1408
-    }
1409
-
1410
-
1411
-
1412
-    /**
1413
-     *        add_styles_and_scripts
1414
-     *
1415
-     * @access        public
1416
-     * @return        void
1417
-     */
1418
-    public function add_styles_and_scripts()
1419
-    {
1420
-        // i18n
1421
-        $this->translate_js_strings();
1422
-        if ($this->checkout->admin_request) {
1423
-            add_action('admin_enqueue_scripts', array($this, 'enqueue_styles_and_scripts'), 10);
1424
-        } else {
1425
-            add_action('wp_enqueue_scripts', array($this, 'enqueue_styles_and_scripts'), 10);
1426
-        }
1427
-    }
1428
-
1429
-
1430
-
1431
-    /**
1432
-     *        translate_js_strings
1433
-     *
1434
-     * @access        public
1435
-     * @return        void
1436
-     */
1437
-    public function translate_js_strings()
1438
-    {
1439
-        EE_Registry::$i18n_js_strings['revisit'] = $this->checkout->revisit;
1440
-        EE_Registry::$i18n_js_strings['e_reg_url_link'] = $this->checkout->reg_url_link;
1441
-        EE_Registry::$i18n_js_strings['server_error'] = __(
1442
-            'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again or contact support.',
1443
-            'event_espresso'
1444
-        );
1445
-        EE_Registry::$i18n_js_strings['invalid_json_response'] = __(
1446
-            'An invalid response was returned from the server while attempting to process your request. Please refresh the page and try again or contact support.',
1447
-            'event_espresso'
1448
-        );
1449
-        EE_Registry::$i18n_js_strings['validation_error'] = __(
1450
-            'There appears to be a problem with the form validation configuration! Please check the admin settings or contact support.',
1451
-            'event_espresso'
1452
-        );
1453
-        EE_Registry::$i18n_js_strings['invalid_payment_method'] = __(
1454
-            'There appears to be a problem with the payment method configuration! Please refresh the page and try again or contact support.',
1455
-            'event_espresso'
1456
-        );
1457
-        EE_Registry::$i18n_js_strings['reg_step_error'] = __(
1458
-            'This registration step could not be completed. Please refresh the page and try again.',
1459
-            'event_espresso'
1460
-        );
1461
-        EE_Registry::$i18n_js_strings['invalid_coupon'] = __(
1462
-            'We\'re sorry but that coupon code does not appear to be valid. If this is incorrect, please contact the site administrator.',
1463
-            'event_espresso'
1464
-        );
1465
-        EE_Registry::$i18n_js_strings['process_registration'] = sprintf(
1466
-            __(
1467
-                'Please wait while we process your registration.%sDo not refresh the page or navigate away while this is happening.%sThank you for your patience.',
1468
-                'event_espresso'
1469
-            ),
1470
-            '<br/>',
1471
-            '<br/>'
1472
-        );
1473
-        EE_Registry::$i18n_js_strings['language'] = get_bloginfo('language');
1474
-        EE_Registry::$i18n_js_strings['EESID'] = EE_Registry::instance()->SSN->id();
1475
-        EE_Registry::$i18n_js_strings['currency'] = EE_Registry::instance()->CFG->currency;
1476
-        EE_Registry::$i18n_js_strings['datepicker_yearRange'] = '-150:+20';
1477
-        EE_Registry::$i18n_js_strings['timer_years'] = __('years', 'event_espresso');
1478
-        EE_Registry::$i18n_js_strings['timer_months'] = __('months', 'event_espresso');
1479
-        EE_Registry::$i18n_js_strings['timer_weeks'] = __('weeks', 'event_espresso');
1480
-        EE_Registry::$i18n_js_strings['timer_days'] = __('days', 'event_espresso');
1481
-        EE_Registry::$i18n_js_strings['timer_hours'] = __('hours', 'event_espresso');
1482
-        EE_Registry::$i18n_js_strings['timer_minutes'] = __('minutes', 'event_espresso');
1483
-        EE_Registry::$i18n_js_strings['timer_seconds'] = __('seconds', 'event_espresso');
1484
-        EE_Registry::$i18n_js_strings['timer_year'] = __('year', 'event_espresso');
1485
-        EE_Registry::$i18n_js_strings['timer_month'] = __('month', 'event_espresso');
1486
-        EE_Registry::$i18n_js_strings['timer_week'] = __('week', 'event_espresso');
1487
-        EE_Registry::$i18n_js_strings['timer_day'] = __('day', 'event_espresso');
1488
-        EE_Registry::$i18n_js_strings['timer_hour'] = __('hour', 'event_espresso');
1489
-        EE_Registry::$i18n_js_strings['timer_minute'] = __('minute', 'event_espresso');
1490
-        EE_Registry::$i18n_js_strings['timer_second'] = __('second', 'event_espresso');
1491
-        EE_Registry::$i18n_js_strings['registration_expiration_notice'] = sprintf(
1492
-            __(
1493
-                '%1$sWe\'re sorry, but your registration time has expired.%2$s%3$s%4$sIf you still wish to complete your registration, please return to the %5$sEvent List%6$sEvent List%7$s and reselect your tickets if available. Please except our apologies for any inconvenience this may have caused.%8$s',
1494
-                'event_espresso'
1495
-            ),
1496
-            '<h4 class="important-notice">',
1497
-            '</h4>',
1498
-            '<br />',
1499
-            '<p>',
1500
-            '<a href="' . get_post_type_archive_link('espresso_events') . '" title="',
1501
-            '">',
1502
-            '</a>',
1503
-            '</p>'
1504
-        );
1505
-        EE_Registry::$i18n_js_strings['ajax_submit'] = apply_filters(
1506
-            'FHEE__Single_Page_Checkout__translate_js_strings__ajax_submit',
1507
-            true
1508
-        );
1509
-        EE_Registry::$i18n_js_strings['session_extension'] = absint(
1510
-            apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS)
1511
-        );
1512
-        EE_Registry::$i18n_js_strings['session_expiration'] = gmdate(
1513
-            'M d, Y H:i:s',
1514
-            EE_Registry::instance()->SSN->expiration() + (get_option('gmt_offset') * HOUR_IN_SECONDS)
1515
-        );
1516
-    }
1517
-
1518
-
1519
-
1520
-    /**
1521
-     *    enqueue_styles_and_scripts
1522
-     *
1523
-     * @access        public
1524
-     * @return        void
1525
-     * @throws EE_Error
1526
-     */
1527
-    public function enqueue_styles_and_scripts()
1528
-    {
1529
-        // load css
1530
-        wp_register_style(
1531
-            'single_page_checkout',
1532
-            SPCO_CSS_URL . 'single_page_checkout.css',
1533
-            array('espresso_default'),
1534
-            EVENT_ESPRESSO_VERSION
1535
-        );
1536
-        wp_enqueue_style('single_page_checkout');
1537
-        // load JS
1538
-        wp_register_script(
1539
-            'jquery_plugin',
1540
-            EE_THIRD_PARTY_URL . 'jquery	.plugin.min.js',
1541
-            array('jquery'),
1542
-            '1.0.1',
1543
-            true
1544
-        );
1545
-        wp_register_script(
1546
-            'jquery_countdown',
1547
-            EE_THIRD_PARTY_URL . 'jquery	.countdown.min.js',
1548
-            array('jquery_plugin'),
1549
-            '2.0.2',
1550
-            true
1551
-        );
1552
-        wp_register_script(
1553
-            'single_page_checkout',
1554
-            SPCO_JS_URL . 'single_page_checkout.js',
1555
-            array('espresso_core', 'underscore', 'ee_form_section_validation', 'jquery_countdown'),
1556
-            EVENT_ESPRESSO_VERSION,
1557
-            true
1558
-        );
1559
-        if ($this->checkout->registration_form instanceof EE_Form_Section_Proper) {
1560
-            $this->checkout->registration_form->enqueue_js();
1561
-        }
1562
-        if ($this->checkout->current_step->reg_form instanceof EE_Form_Section_Proper) {
1563
-            $this->checkout->current_step->reg_form->enqueue_js();
1564
-        }
1565
-        wp_enqueue_script('single_page_checkout');
1566
-        /**
1567
-         * global action hook for enqueueing styles and scripts with
1568
-         * spco calls.
1569
-         */
1570
-        do_action('AHEE__EED_Single_Page_Checkout__enqueue_styles_and_scripts', $this);
1571
-        /**
1572
-         * dynamic action hook for enqueueing styles and scripts with spco calls.
1573
-         * The hook will end up being something like:
1574
-         *      AHEE__EED_Single_Page_Checkout__enqueue_styles_and_scripts__attendee_information
1575
-         */
1576
-        do_action(
1577
-            'AHEE__EED_Single_Page_Checkout__enqueue_styles_and_scripts__' . $this->checkout->current_step->slug(),
1578
-            $this
1579
-        );
1580
-    }
1581
-
1582
-
1583
-
1584
-    /**
1585
-     *    display the Registration Single Page Checkout Form
1586
-     *
1587
-     * @access    private
1588
-     * @return    void
1589
-     * @throws EE_Error
1590
-     */
1591
-    private function _display_spco_reg_form()
1592
-    {
1593
-        // if registering via the admin, just display the reg form for the current step
1594
-        if ($this->checkout->admin_request) {
1595
-            EE_Registry::instance()->REQ->add_output($this->checkout->current_step->display_reg_form());
1596
-        } else {
1597
-            // add powered by EE msg
1598
-            add_action('AHEE__SPCO__reg_form_footer', array('EED_Single_Page_Checkout', 'display_registration_footer'));
1599
-            $empty_cart = count(
1600
-                $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params)
1601
-            ) < 1;
1602
-            EE_Registry::$i18n_js_strings['empty_cart'] = $empty_cart;
1603
-            $cookies_not_set_msg = '';
1604
-            if ($empty_cart) {
1605
-                $cookies_not_set_msg = apply_filters(
1606
-                    'FHEE__Single_Page_Checkout__display_spco_reg_form__cookies_not_set_msg',
1607
-                    sprintf(
1608
-                        __(
1609
-                            '%1$s%3$sIt appears your browser is not currently set to accept Cookies%4$s%5$sIn order to register for events, you need to enable cookies.%7$sIf you require assistance, then click the following link to learn how to %8$senable cookies%9$s%6$s%2$s',
1610
-                            'event_espresso'
1611
-                        ),
1612
-                        '<div class="ee-attention hidden" id="ee-cookies-not-set-msg">',
1613
-                        '</div>',
1614
-                        '<h6 class="important-notice">',
1615
-                        '</h6>',
1616
-                        '<p>',
1617
-                        '</p>',
1618
-                        '<br />',
1619
-                        '<a href="http://www.whatarecookies.com/enable.asp" target="_blank">',
1620
-                        '</a>'
1621
-                    )
1622
-                );
1623
-            }
1624
-            $this->checkout->registration_form = new EE_Form_Section_Proper(
1625
-                array(
1626
-                    'name'            => 'single-page-checkout',
1627
-                    'html_id'         => 'ee-single-page-checkout-dv',
1628
-                    'layout_strategy' =>
1629
-                        new EE_Template_Layout(
1630
-                            array(
1631
-                                'layout_template_file' => SPCO_TEMPLATES_PATH . 'registration_page_wrapper.template.php',
1632
-                                'template_args'        => array(
1633
-                                    'empty_cart'              => $empty_cart,
1634
-                                    'revisit'                 => $this->checkout->revisit,
1635
-                                    'reg_steps'               => $this->checkout->reg_steps,
1636
-                                    'next_step'               => $this->checkout->next_step instanceof EE_SPCO_Reg_Step
1637
-                                        ? $this->checkout->next_step->slug()
1638
-                                        : '',
1639
-                                    'empty_msg'               => apply_filters(
1640
-                                        'FHEE__Single_Page_Checkout__display_spco_reg_form__empty_msg',
1641
-                                        sprintf(
1642
-                                            __(
1643
-                                                'You need to %1$sReturn to Events list%2$sselect at least one event%3$s before you can proceed with the registration process.',
1644
-                                                'event_espresso'
1645
-                                            ),
1646
-                                            '<a href="'
1647
-                                            . get_post_type_archive_link('espresso_events')
1648
-                                            . '" title="',
1649
-                                            '">',
1650
-                                            '</a>'
1651
-                                        )
1652
-                                    ),
1653
-                                    'cookies_not_set_msg'     => $cookies_not_set_msg,
1654
-                                    'registration_time_limit' => $this->checkout->get_registration_time_limit(),
1655
-                                    'session_expiration'      => gmdate(
1656
-                                        'M d, Y H:i:s',
1657
-                                        EE_Registry::instance()->SSN->expiration()
1658
-                                        + (get_option('gmt_offset') * HOUR_IN_SECONDS)
1659
-                                    ),
1660
-                                ),
1661
-                            )
1662
-                        ),
1663
-                )
1664
-            );
1665
-            // load template and add to output sent that gets filtered into the_content()
1666
-            EE_Registry::instance()->REQ->add_output($this->checkout->registration_form->get_html());
1667
-        }
1668
-    }
1669
-
1670
-
1671
-
1672
-    /**
1673
-     *    add_extra_finalize_registration_inputs
1674
-     *
1675
-     * @access    public
1676
-     * @param $next_step
1677
-     * @internal  param string $label
1678
-     * @return void
1679
-     */
1680
-    public function add_extra_finalize_registration_inputs($next_step)
1681
-    {
1682
-        if ($next_step === 'finalize_registration') {
1683
-            echo '<div id="spco-extra-finalize_registration-inputs-dv"></div>';
1684
-        }
1685
-    }
1686
-
1687
-
1688
-
1689
-    /**
1690
-     *    display_registration_footer
1691
-     *
1692
-     * @access    public
1693
-     * @return    string
1694
-     */
1695
-    public static function display_registration_footer()
1696
-    {
1697
-        if (
1698
-        apply_filters(
1699
-            'FHEE__EE_Front__Controller__show_reg_footer',
1700
-            EE_Registry::instance()->CFG->admin->show_reg_footer
1701
-        )
1702
-        ) {
1703
-            add_filter(
1704
-                'FHEE__EEH_Template__powered_by_event_espresso__url',
1705
-                function ($url) {
1706
-                    return apply_filters('FHEE__EE_Front_Controller__registration_footer__url', $url);
1707
-                }
1708
-            );
1709
-            echo apply_filters(
1710
-                'FHEE__EE_Front_Controller__display_registration_footer',
1711
-                \EEH_Template::powered_by_event_espresso(
1712
-                    '',
1713
-                    'espresso-registration-footer-dv',
1714
-                    array('utm_content' => 'registration_checkout')
1715
-                )
1716
-            );
1717
-        }
1718
-        return '';
1719
-    }
1720
-
1721
-
1722
-
1723
-    /**
1724
-     *    unlock_transaction
1725
-     *
1726
-     * @access    public
1727
-     * @return    void
1728
-     * @throws EE_Error
1729
-     */
1730
-    public function unlock_transaction()
1731
-    {
1732
-        if ($this->checkout->transaction instanceof EE_Transaction) {
1733
-            $this->checkout->transaction->unlock();
1734
-        }
1735
-    }
1736
-
1737
-
1738
-
1739
-    /**
1740
-     *        _setup_redirect
1741
-     *
1742
-     * @access    private
1743
-     * @return void
1744
-     */
1745
-    private function _setup_redirect()
1746
-    {
1747
-        if ($this->checkout->continue_reg && $this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
1748
-            $this->checkout->redirect = true;
1749
-            if (empty($this->checkout->redirect_url)) {
1750
-                $this->checkout->redirect_url = $this->checkout->next_step->reg_step_url();
1751
-            }
1752
-            $this->checkout->redirect_url = apply_filters(
1753
-                'FHEE__EED_Single_Page_Checkout___setup_redirect__checkout_redirect_url',
1754
-                $this->checkout->redirect_url,
1755
-                $this->checkout
1756
-            );
1757
-        }
1758
-    }
1759
-
1760
-
1761
-
1762
-    /**
1763
-     *   handle ajax message responses and redirects
1764
-     *
1765
-     * @access public
1766
-     * @return void
1767
-     * @throws EE_Error
1768
-     */
1769
-    public function go_to_next_step()
1770
-    {
1771
-        if (EE_Registry::instance()->REQ->ajax) {
1772
-            // capture contents of output buffer we started earlier in the request, and insert into JSON response
1773
-            $this->checkout->json_response->set_unexpected_errors(ob_get_clean());
1774
-        }
1775
-        $this->unlock_transaction();
1776
-        // just return for these conditions
1777
-        if (
1778
-            $this->checkout->admin_request
1779
-            || $this->checkout->action === 'redirect_form'
1780
-            || $this->checkout->action === 'update_checkout'
1781
-        ) {
1782
-            return;
1783
-        }
1784
-        // AJAX response
1785
-        $this->_handle_json_response();
1786
-        // redirect to next step or the Thank You page
1787
-        $this->_handle_html_redirects();
1788
-        // hmmm... must be something wrong, so let's just display the form again !
1789
-        $this->_display_spco_reg_form();
1790
-    }
1791
-
1792
-
1793
-
1794
-    /**
1795
-     *   _handle_json_response
1796
-     *
1797
-     * @access protected
1798
-     * @return void
1799
-     */
1800
-    protected function _handle_json_response()
1801
-    {
1802
-        // if this is an ajax request
1803
-        if (EE_Registry::instance()->REQ->ajax) {
1804
-            // DEBUG LOG
1805
-            //$this->checkout->log(
1806
-            //	__CLASS__, __FUNCTION__, __LINE__,
1807
-            //	array(
1808
-            //		'json_response_redirect_url' => $this->checkout->json_response->redirect_url(),
1809
-            //		'redirect'                   => $this->checkout->redirect,
1810
-            //		'continue_reg'               => $this->checkout->continue_reg,
1811
-            //	)
1812
-            //);
1813
-            $this->checkout->json_response->set_registration_time_limit(
1814
-                $this->checkout->get_registration_time_limit()
1815
-            );
1816
-            $this->checkout->json_response->set_payment_amount($this->checkout->amount_owing);
1817
-            // just send the ajax (
1818
-            $json_response = apply_filters(
1819
-                'FHEE__EE_Single_Page_Checkout__JSON_response',
1820
-                $this->checkout->json_response
1821
-            );
1822
-            echo $json_response;
1823
-            exit();
1824
-        }
1825
-    }
1826
-
1827
-
1828
-
1829
-    /**
1830
-     *   _handle_redirects
1831
-     *
1832
-     * @access protected
1833
-     * @return void
1834
-     */
1835
-    protected function _handle_html_redirects()
1836
-    {
1837
-        // going somewhere ?
1838
-        if ($this->checkout->redirect && ! empty($this->checkout->redirect_url)) {
1839
-            // store notices in a transient
1840
-            EE_Error::get_notices(false, true, true);
1841
-            // DEBUG LOG
1842
-            //$this->checkout->log(
1843
-            //	__CLASS__, __FUNCTION__, __LINE__,
1844
-            //	array(
1845
-            //		'headers_sent' => headers_sent(),
1846
-            //		'redirect_url'     => $this->checkout->redirect_url,
1847
-            //		'headers_list'    => headers_list(),
1848
-            //	)
1849
-            //);
1850
-            wp_safe_redirect($this->checkout->redirect_url);
1851
-            exit();
1852
-        }
1853
-    }
1854
-
1855
-
1856
-
1857
-    /**
1858
-     *   set_checkout_anchor
1859
-     *
1860
-     * @access public
1861
-     * @return void
1862
-     */
1863
-    public function set_checkout_anchor()
1864
-    {
1865
-        echo '<a id="checkout" style="float: left; margin-left: -999em;"></a>';
1866
-    }
23
+	/**
24
+	 * $_initialized - has the SPCO controller already been initialized ?
25
+	 *
26
+	 * @access private
27
+	 * @var bool $_initialized
28
+	 */
29
+	private static $_initialized = false;
30
+
31
+
32
+	/**
33
+	 * $_checkout_verified - is the EE_Checkout verified as correct for this request ?
34
+	 *
35
+	 * @access private
36
+	 * @var bool $_valid_checkout
37
+	 */
38
+	private static $_checkout_verified = true;
39
+
40
+	/**
41
+	 *    $_reg_steps_array - holds initial array of reg steps
42
+	 *
43
+	 * @access private
44
+	 * @var array $_reg_steps_array
45
+	 */
46
+	private static $_reg_steps_array = array();
47
+
48
+	/**
49
+	 *    $checkout - EE_Checkout object for handling the properties of the current checkout process
50
+	 *
51
+	 * @access public
52
+	 * @var EE_Checkout $checkout
53
+	 */
54
+	public $checkout;
55
+
56
+
57
+
58
+	/**
59
+	 * @return EED_Module|EED_Single_Page_Checkout
60
+	 */
61
+	public static function instance()
62
+	{
63
+		add_filter('EED_Single_Page_Checkout__SPCO_active', '__return_true');
64
+		return parent::get_instance(__CLASS__);
65
+	}
66
+
67
+
68
+
69
+	/**
70
+	 * @return EE_CART
71
+	 */
72
+	public function cart()
73
+	{
74
+		return $this->checkout->cart;
75
+	}
76
+
77
+
78
+
79
+	/**
80
+	 * @return EE_Transaction
81
+	 */
82
+	public function transaction()
83
+	{
84
+		return $this->checkout->transaction;
85
+	}
86
+
87
+
88
+
89
+	/**
90
+	 *    set_hooks - for hooking into EE Core, other modules, etc
91
+	 *
92
+	 * @access    public
93
+	 * @return    void
94
+	 * @throws EE_Error
95
+	 */
96
+	public static function set_hooks()
97
+	{
98
+		EED_Single_Page_Checkout::set_definitions();
99
+	}
100
+
101
+
102
+
103
+	/**
104
+	 *    set_hooks_admin - for hooking into EE Admin Core, other modules, etc
105
+	 *
106
+	 * @access    public
107
+	 * @return    void
108
+	 * @throws EE_Error
109
+	 */
110
+	public static function set_hooks_admin()
111
+	{
112
+		EED_Single_Page_Checkout::set_definitions();
113
+		if ( ! (defined('DOING_AJAX') && DOING_AJAX)) {
114
+			return;
115
+		}
116
+		// going to start an output buffer in case anything gets accidentally output
117
+		// that might disrupt our JSON response
118
+		ob_start();
119
+		EED_Single_Page_Checkout::load_request_handler();
120
+		EED_Single_Page_Checkout::load_reg_steps();
121
+		// set ajax hooks
122
+		add_action('wp_ajax_process_reg_step', array('EED_Single_Page_Checkout', 'process_reg_step'));
123
+		add_action('wp_ajax_nopriv_process_reg_step', array('EED_Single_Page_Checkout', 'process_reg_step'));
124
+		add_action('wp_ajax_display_spco_reg_step', array('EED_Single_Page_Checkout', 'display_reg_step'));
125
+		add_action('wp_ajax_nopriv_display_spco_reg_step', array('EED_Single_Page_Checkout', 'display_reg_step'));
126
+		add_action('wp_ajax_update_reg_step', array('EED_Single_Page_Checkout', 'update_reg_step'));
127
+		add_action('wp_ajax_nopriv_update_reg_step', array('EED_Single_Page_Checkout', 'update_reg_step'));
128
+	}
129
+
130
+
131
+
132
+	/**
133
+	 *    process ajax request
134
+	 *
135
+	 * @param string $ajax_action
136
+	 * @throws EE_Error
137
+	 */
138
+	public static function process_ajax_request($ajax_action)
139
+	{
140
+		EE_Registry::instance()->REQ->set('action', $ajax_action);
141
+		EED_Single_Page_Checkout::instance()->_initialize();
142
+	}
143
+
144
+
145
+
146
+	/**
147
+	 *    ajax display registration step
148
+	 *
149
+	 * @throws EE_Error
150
+	 */
151
+	public static function display_reg_step()
152
+	{
153
+		EED_Single_Page_Checkout::process_ajax_request('display_spco_reg_step');
154
+	}
155
+
156
+
157
+
158
+	/**
159
+	 *    ajax process registration step
160
+	 *
161
+	 * @throws EE_Error
162
+	 */
163
+	public static function process_reg_step()
164
+	{
165
+		EED_Single_Page_Checkout::process_ajax_request('process_reg_step');
166
+	}
167
+
168
+
169
+
170
+	/**
171
+	 *    ajax process registration step
172
+	 *
173
+	 * @throws EE_Error
174
+	 */
175
+	public static function update_reg_step()
176
+	{
177
+		EED_Single_Page_Checkout::process_ajax_request('update_reg_step');
178
+	}
179
+
180
+
181
+
182
+	/**
183
+	 *   update_checkout
184
+	 *
185
+	 * @access public
186
+	 * @return void
187
+	 * @throws EE_Error
188
+	 */
189
+	public static function update_checkout()
190
+	{
191
+		EED_Single_Page_Checkout::process_ajax_request('update_checkout');
192
+	}
193
+
194
+
195
+
196
+	/**
197
+	 *    load_request_handler
198
+	 *
199
+	 * @access    public
200
+	 * @return    void
201
+	 */
202
+	public static function load_request_handler()
203
+	{
204
+		// load core Request_Handler class
205
+		if (EE_Registry::instance()->REQ !== null) {
206
+			EE_Registry::instance()->load_core('Request_Handler');
207
+		}
208
+	}
209
+
210
+
211
+
212
+	/**
213
+	 *    set_definitions
214
+	 *
215
+	 * @access    public
216
+	 * @return    void
217
+	 * @throws EE_Error
218
+	 */
219
+	public static function set_definitions()
220
+	{
221
+		if(defined('SPCO_BASE_PATH')) {
222
+			return;
223
+		}
224
+		define(
225
+			'SPCO_BASE_PATH',
226
+			rtrim(str_replace(array('\\', '/'), DS, plugin_dir_path(__FILE__)), DS) . DS
227
+		);
228
+		define('SPCO_CSS_URL', plugin_dir_url(__FILE__) . 'css' . DS);
229
+		define('SPCO_IMG_URL', plugin_dir_url(__FILE__) . 'img' . DS);
230
+		define('SPCO_JS_URL', plugin_dir_url(__FILE__) . 'js' . DS);
231
+		define('SPCO_INC_PATH', SPCO_BASE_PATH . 'inc' . DS);
232
+		define('SPCO_REG_STEPS_PATH', SPCO_BASE_PATH . 'reg_steps' . DS);
233
+		define('SPCO_TEMPLATES_PATH', SPCO_BASE_PATH . 'templates' . DS);
234
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(SPCO_BASE_PATH, true);
235
+		EE_Registry::$i18n_js_strings['registration_expiration_notice'] = sprintf(
236
+			__('%1$sWe\'re sorry, but you\'re registration time has expired.%2$s%4$sIf you still wish to complete your registration, please return to the %5$sEvent List%6$sEvent List%7$s and reselect your tickets if available. Please except our apologies for any inconvenience this may have caused.%8$s',
237
+				'event_espresso'),
238
+			'<h4 class="important-notice">',
239
+			'</h4>',
240
+			'<br />',
241
+			'<p>',
242
+			'<a href="' . get_post_type_archive_link('espresso_events') . '" title="',
243
+			'">',
244
+			'</a>',
245
+			'</p>'
246
+		);
247
+	}
248
+
249
+
250
+
251
+	/**
252
+	 * load_reg_steps
253
+	 * loads and instantiates each reg step based on the EE_Registry::instance()->CFG->registration->reg_steps array
254
+	 *
255
+	 * @access    private
256
+	 * @throws EE_Error
257
+	 */
258
+	public static function load_reg_steps()
259
+	{
260
+		static $reg_steps_loaded = false;
261
+		if ($reg_steps_loaded) {
262
+			return;
263
+		}
264
+		// filter list of reg_steps
265
+		$reg_steps_to_load = (array)apply_filters(
266
+			'AHEE__SPCO__load_reg_steps__reg_steps_to_load',
267
+			EED_Single_Page_Checkout::get_reg_steps()
268
+		);
269
+		// sort by key (order)
270
+		ksort($reg_steps_to_load);
271
+		// loop through folders
272
+		foreach ($reg_steps_to_load as $order => $reg_step) {
273
+			// we need a
274
+			if (isset($reg_step['file_path'], $reg_step['class_name'], $reg_step['slug'])) {
275
+				// copy over to the reg_steps_array
276
+				EED_Single_Page_Checkout::$_reg_steps_array[$order] = $reg_step;
277
+				// register custom key route for each reg step
278
+				// ie: step=>"slug" - this is the entire reason we load the reg steps array now
279
+				EE_Config::register_route(
280
+					$reg_step['slug'],
281
+					'EED_Single_Page_Checkout',
282
+					'run',
283
+					'step'
284
+				);
285
+				// add AJAX or other hooks
286
+				if (isset($reg_step['has_hooks']) && $reg_step['has_hooks']) {
287
+					// setup autoloaders if necessary
288
+					if ( ! class_exists($reg_step['class_name'])) {
289
+						EEH_Autoloader::register_autoloaders_for_each_file_in_folder(
290
+							$reg_step['file_path'],
291
+							true
292
+						);
293
+					}
294
+					if (is_callable($reg_step['class_name'], 'set_hooks')) {
295
+						call_user_func(array($reg_step['class_name'], 'set_hooks'));
296
+					}
297
+				}
298
+			}
299
+		}
300
+		$reg_steps_loaded = true;
301
+	}
302
+
303
+
304
+
305
+	/**
306
+	 *    get_reg_steps
307
+	 *
308
+	 * @access    public
309
+	 * @return    array
310
+	 */
311
+	public static function get_reg_steps()
312
+	{
313
+		$reg_steps = EE_Registry::instance()->CFG->registration->reg_steps;
314
+		if (empty($reg_steps)) {
315
+			$reg_steps = array(
316
+				10  => array(
317
+					'file_path'  => SPCO_REG_STEPS_PATH . 'attendee_information',
318
+					'class_name' => 'EE_SPCO_Reg_Step_Attendee_Information',
319
+					'slug'       => 'attendee_information',
320
+					'has_hooks'  => false,
321
+				),
322
+				20  => array(
323
+					'file_path'  => SPCO_REG_STEPS_PATH . 'registration_confirmation',
324
+					'class_name' => 'EE_SPCO_Reg_Step_Registration_Confirmation',
325
+					'slug'       => 'registration_confirmation',
326
+					'has_hooks'  => false,
327
+				),
328
+				30  => array(
329
+					'file_path'  => SPCO_REG_STEPS_PATH . 'payment_options',
330
+					'class_name' => 'EE_SPCO_Reg_Step_Payment_Options',
331
+					'slug'       => 'payment_options',
332
+					'has_hooks'  => true,
333
+				),
334
+				999 => array(
335
+					'file_path'  => SPCO_REG_STEPS_PATH . 'finalize_registration',
336
+					'class_name' => 'EE_SPCO_Reg_Step_Finalize_Registration',
337
+					'slug'       => 'finalize_registration',
338
+					'has_hooks'  => false,
339
+				),
340
+			);
341
+		}
342
+		return $reg_steps;
343
+	}
344
+
345
+
346
+
347
+	/**
348
+	 *    registration_checkout_for_admin
349
+	 *
350
+	 * @access    public
351
+	 * @return    string
352
+	 * @throws EE_Error
353
+	 */
354
+	public static function registration_checkout_for_admin()
355
+	{
356
+		EED_Single_Page_Checkout::load_request_handler();
357
+		EE_Registry::instance()->REQ->set('step', 'attendee_information');
358
+		EE_Registry::instance()->REQ->set('action', 'display_spco_reg_step');
359
+		EE_Registry::instance()->REQ->set('process_form_submission', false);
360
+		EED_Single_Page_Checkout::instance()->_initialize();
361
+		EED_Single_Page_Checkout::instance()->_display_spco_reg_form();
362
+		return EE_Registry::instance()->REQ->get_output();
363
+	}
364
+
365
+
366
+
367
+	/**
368
+	 * process_registration_from_admin
369
+	 *
370
+	 * @access public
371
+	 * @return \EE_Transaction
372
+	 * @throws EE_Error
373
+	 */
374
+	public static function process_registration_from_admin()
375
+	{
376
+		EED_Single_Page_Checkout::load_request_handler();
377
+		EE_Registry::instance()->REQ->set('step', 'attendee_information');
378
+		EE_Registry::instance()->REQ->set('action', 'process_reg_step');
379
+		EE_Registry::instance()->REQ->set('process_form_submission', true);
380
+		EED_Single_Page_Checkout::instance()->_initialize();
381
+		if (EED_Single_Page_Checkout::instance()->checkout->current_step->completed()) {
382
+			$final_reg_step = end(EED_Single_Page_Checkout::instance()->checkout->reg_steps);
383
+			if ($final_reg_step instanceof EE_SPCO_Reg_Step_Finalize_Registration) {
384
+				EED_Single_Page_Checkout::instance()->checkout->set_reg_step_initiated($final_reg_step);
385
+				if ($final_reg_step->process_reg_step()) {
386
+					$final_reg_step->set_completed();
387
+					EED_Single_Page_Checkout::instance()->checkout->update_txn_reg_steps_array();
388
+					return EED_Single_Page_Checkout::instance()->checkout->transaction;
389
+				}
390
+			}
391
+		}
392
+		return null;
393
+	}
394
+
395
+
396
+
397
+	/**
398
+	 *    run
399
+	 *
400
+	 * @access    public
401
+	 * @param WP_Query $WP_Query
402
+	 * @return    void
403
+	 * @throws EE_Error
404
+	 */
405
+	public function run($WP_Query)
406
+	{
407
+		if (
408
+			$WP_Query instanceof WP_Query
409
+			&& $WP_Query->is_main_query()
410
+			&& apply_filters('FHEE__EED_Single_Page_Checkout__run', true)
411
+			&& $this->_is_reg_checkout()
412
+		) {
413
+			$this->_initialize();
414
+		}
415
+	}
416
+
417
+
418
+
419
+	/**
420
+	 * determines whether current url matches reg page url
421
+	 *
422
+	 * @return bool
423
+	 */
424
+	protected function _is_reg_checkout()
425
+	{
426
+		// get current permalink for reg page without any extra query args
427
+		$reg_page_url = \get_permalink(EE_Config::instance()->core->reg_page_id);
428
+		// get request URI for current request, but without the scheme or host
429
+		$current_request_uri = \EEH_URL::filter_input_server_url('REQUEST_URI');
430
+		$current_request_uri = html_entity_decode($current_request_uri);
431
+		// get array of query args from the current request URI
432
+		$query_args = \EEH_URL::get_query_string($current_request_uri);
433
+		// grab page id if it is set
434
+		$page_id = isset($query_args['page_id']) ? absint($query_args['page_id']) : 0;
435
+		// and remove the page id from the query args (we will re-add it later)
436
+		unset($query_args['page_id']);
437
+		// now strip all query args from current request URI
438
+		$current_request_uri = remove_query_arg(array_keys($query_args), $current_request_uri);
439
+		// and re-add the page id if it was set
440
+		if ($page_id) {
441
+			$current_request_uri = add_query_arg('page_id', $page_id, $current_request_uri);
442
+		}
443
+		// remove slashes and ?
444
+		$current_request_uri = trim($current_request_uri, '?/');
445
+		// is current request URI part of the known full reg page URL ?
446
+		return ! empty($current_request_uri) && strpos($reg_page_url, $current_request_uri) !== false;
447
+	}
448
+
449
+
450
+
451
+	/**
452
+	 * @param WP_Query $wp_query
453
+	 * @return    void
454
+	 * @throws EE_Error
455
+	 */
456
+	public static function init($wp_query)
457
+	{
458
+		EED_Single_Page_Checkout::instance()->run($wp_query);
459
+	}
460
+
461
+
462
+
463
+	/**
464
+	 *    _initialize - initial module setup
465
+	 *
466
+	 * @access    private
467
+	 * @throws EE_Error
468
+	 * @return    void
469
+	 */
470
+	private function _initialize()
471
+	{
472
+		// ensure SPCO doesn't run twice
473
+		if (EED_Single_Page_Checkout::$_initialized) {
474
+			return;
475
+		}
476
+		try {
477
+			EED_Single_Page_Checkout::load_reg_steps();
478
+			$this->_verify_session();
479
+			// setup the EE_Checkout object
480
+			$this->checkout = $this->_initialize_checkout();
481
+			// filter checkout
482
+			$this->checkout = apply_filters('FHEE__EED_Single_Page_Checkout___initialize__checkout', $this->checkout);
483
+			// get the $_GET
484
+			$this->_get_request_vars();
485
+			if ($this->_block_bots()) {
486
+				return;
487
+			}
488
+			// filter continue_reg
489
+			$this->checkout->continue_reg = apply_filters(
490
+				'FHEE__EED_Single_Page_Checkout__init___continue_reg',
491
+				true,
492
+				$this->checkout
493
+			);
494
+			// load the reg steps array
495
+			if ( ! $this->_load_and_instantiate_reg_steps()) {
496
+				EED_Single_Page_Checkout::$_initialized = true;
497
+				return;
498
+			}
499
+			// set the current step
500
+			$this->checkout->set_current_step($this->checkout->step);
501
+			// and the next step
502
+			$this->checkout->set_next_step();
503
+			// verify that everything has been setup correctly
504
+			if ( ! ($this->_verify_transaction_and_get_registrations() && $this->_final_verifications())) {
505
+				EED_Single_Page_Checkout::$_initialized = true;
506
+				return;
507
+			}
508
+			// lock the transaction
509
+			$this->checkout->transaction->lock();
510
+			// make sure all of our cached objects are added to their respective model entity mappers
511
+			$this->checkout->refresh_all_entities();
512
+			// set amount owing
513
+			$this->checkout->amount_owing = $this->checkout->transaction->remaining();
514
+			// initialize each reg step, which gives them the chance to potentially alter the process
515
+			$this->_initialize_reg_steps();
516
+			// DEBUG LOG
517
+			//$this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ );
518
+			// get reg form
519
+			if( ! $this->_check_form_submission()) {
520
+				EED_Single_Page_Checkout::$_initialized = true;
521
+				return;
522
+			}
523
+			// checkout the action!!!
524
+			$this->_process_form_action();
525
+			// add some style and make it dance
526
+			$this->add_styles_and_scripts();
527
+			// kk... SPCO has successfully run
528
+			EED_Single_Page_Checkout::$_initialized = true;
529
+			// set no cache headers and constants
530
+			EE_System::do_not_cache();
531
+			// add anchor
532
+			add_action('loop_start', array($this, 'set_checkout_anchor'), 1);
533
+			// remove transaction lock
534
+			add_action('shutdown', array($this, 'unlock_transaction'), 1);
535
+		} catch (Exception $e) {
536
+			EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
537
+		}
538
+	}
539
+
540
+
541
+
542
+	/**
543
+	 *    _verify_session
544
+	 * checks that the session is valid and not expired
545
+	 *
546
+	 * @access    private
547
+	 * @throws EE_Error
548
+	 */
549
+	private function _verify_session()
550
+	{
551
+		if ( ! EE_Registry::instance()->SSN instanceof EE_Session) {
552
+			throw new EE_Error(__('The EE_Session class could not be loaded.', 'event_espresso'));
553
+		}
554
+		$clear_session_requested = filter_var(
555
+			EE_Registry::instance()->REQ->get('clear_session', false),
556
+			FILTER_VALIDATE_BOOLEAN
557
+		);
558
+		// is session still valid ?
559
+		if ($clear_session_requested
560
+			|| ( EE_Registry::instance()->SSN->expired()
561
+			  && EE_Registry::instance()->REQ->get('e_reg_url_link', '') === ''
562
+			)
563
+		) {
564
+			$this->checkout = new EE_Checkout();
565
+			EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__);
566
+			// EE_Registry::instance()->SSN->reset_cart();
567
+			// EE_Registry::instance()->SSN->reset_checkout();
568
+			// EE_Registry::instance()->SSN->reset_transaction();
569
+			if (! $clear_session_requested) {
570
+				EE_Error::add_attention(
571
+					EE_Registry::$i18n_js_strings['registration_expiration_notice'],
572
+					__FILE__, __FUNCTION__, __LINE__
573
+				);
574
+			}
575
+			// EE_Registry::instance()->SSN->reset_expired();
576
+		}
577
+	}
578
+
579
+
580
+
581
+	/**
582
+	 *    _initialize_checkout
583
+	 * loads and instantiates EE_Checkout
584
+	 *
585
+	 * @access    private
586
+	 * @throws EE_Error
587
+	 * @return EE_Checkout
588
+	 */
589
+	private function _initialize_checkout()
590
+	{
591
+		// look in session for existing checkout
592
+		/** @type EE_Checkout $checkout */
593
+		$checkout = EE_Registry::instance()->SSN->checkout();
594
+		// verify
595
+		if ( ! $checkout instanceof EE_Checkout) {
596
+			// instantiate EE_Checkout object for handling the properties of the current checkout process
597
+			$checkout = EE_Registry::instance()->load_file(
598
+				SPCO_INC_PATH,
599
+				'EE_Checkout',
600
+				'class', array(),
601
+				false
602
+			);
603
+		} else {
604
+			if ($checkout->current_step->is_final_step() && $checkout->exit_spco() === true) {
605
+				$this->unlock_transaction();
606
+				wp_safe_redirect($checkout->redirect_url);
607
+				exit();
608
+			}
609
+		}
610
+		$checkout = apply_filters('FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', $checkout);
611
+		// verify again
612
+		if ( ! $checkout instanceof EE_Checkout) {
613
+			throw new EE_Error(__('The EE_Checkout class could not be loaded.', 'event_espresso'));
614
+		}
615
+		// reset anything that needs a clean slate for each request
616
+		$checkout->reset_for_current_request();
617
+		return $checkout;
618
+	}
619
+
620
+
621
+
622
+	/**
623
+	 *    _get_request_vars
624
+	 *
625
+	 * @access    private
626
+	 * @return    void
627
+	 * @throws EE_Error
628
+	 */
629
+	private function _get_request_vars()
630
+	{
631
+		// load classes
632
+		EED_Single_Page_Checkout::load_request_handler();
633
+		//make sure this request is marked as belonging to EE
634
+		EE_Registry::instance()->REQ->set_espresso_page(true);
635
+		// which step is being requested ?
636
+		$this->checkout->step = EE_Registry::instance()->REQ->get('step', $this->_get_first_step());
637
+		// which step is being edited ?
638
+		$this->checkout->edit_step = EE_Registry::instance()->REQ->get('edit_step', '');
639
+		// and what we're doing on the current step
640
+		$this->checkout->action = EE_Registry::instance()->REQ->get('action', 'display_spco_reg_step');
641
+		// timestamp
642
+		$this->checkout->uts = EE_Registry::instance()->REQ->get('uts', 0);
643
+		// returning to edit ?
644
+		$this->checkout->reg_url_link = EE_Registry::instance()->REQ->get('e_reg_url_link', '');
645
+		// add reg url link to registration query params
646
+		if ($this->checkout->reg_url_link && strpos($this->checkout->reg_url_link, '1-') !== 0) {
647
+			$this->checkout->reg_cache_where_params[0]['REG_url_link'] = $this->checkout->reg_url_link;
648
+		}
649
+		// or some other kind of revisit ?
650
+		$this->checkout->revisit = filter_var(
651
+			EE_Registry::instance()->REQ->get('revisit', false),
652
+			FILTER_VALIDATE_BOOLEAN
653
+		);
654
+		// and whether or not to generate a reg form for this request
655
+		$this->checkout->generate_reg_form = filter_var(
656
+			EE_Registry::instance()->REQ->get('generate_reg_form', true),
657
+			FILTER_VALIDATE_BOOLEAN
658
+		);
659
+		// and whether or not to process a reg form submission for this request
660
+		$this->checkout->process_form_submission = filter_var(
661
+			EE_Registry::instance()->REQ->get(
662
+				'process_form_submission',
663
+				$this->checkout->action === 'process_reg_step'
664
+			),
665
+			FILTER_VALIDATE_BOOLEAN
666
+		);
667
+		$this->checkout->process_form_submission = filter_var(
668
+			$this->checkout->action !== 'display_spco_reg_step'
669
+				? $this->checkout->process_form_submission
670
+				: false,
671
+			FILTER_VALIDATE_BOOLEAN
672
+		);
673
+		// $this->_display_request_vars();
674
+	}
675
+
676
+
677
+
678
+	/**
679
+	 *  _display_request_vars
680
+	 *
681
+	 * @access    protected
682
+	 * @return    void
683
+	 */
684
+	protected function _display_request_vars()
685
+	{
686
+		if ( ! WP_DEBUG) {
687
+			return;
688
+		}
689
+		EEH_Debug_Tools::printr($_REQUEST, '$_REQUEST', __FILE__, __LINE__);
690
+		EEH_Debug_Tools::printr($this->checkout->step, '$this->checkout->step', __FILE__, __LINE__);
691
+		EEH_Debug_Tools::printr($this->checkout->edit_step, '$this->checkout->edit_step', __FILE__, __LINE__);
692
+		EEH_Debug_Tools::printr($this->checkout->action, '$this->checkout->action', __FILE__, __LINE__);
693
+		EEH_Debug_Tools::printr($this->checkout->reg_url_link, '$this->checkout->reg_url_link', __FILE__, __LINE__);
694
+		EEH_Debug_Tools::printr($this->checkout->revisit, '$this->checkout->revisit', __FILE__, __LINE__);
695
+		EEH_Debug_Tools::printr($this->checkout->generate_reg_form, '$this->checkout->generate_reg_form', __FILE__, __LINE__);
696
+		EEH_Debug_Tools::printr($this->checkout->process_form_submission, '$this->checkout->process_form_submission', __FILE__, __LINE__);
697
+	}
698
+
699
+
700
+
701
+	/**
702
+	 * _block_bots
703
+	 * checks that the incoming request has either of the following set:
704
+	 *  a uts (unix timestamp) which indicates that the request was redirected from the Ticket Selector
705
+	 *  a REG URL Link, which indicates that the request is a return visit to SPCO for a valid TXN
706
+	 * so if you're not coming from the Ticket Selector nor returning for a valid IP...
707
+	 * then where you coming from man?
708
+	 *
709
+	 * @return boolean
710
+	 */
711
+	private function _block_bots()
712
+	{
713
+		$invalid_checkout_access = EED_Invalid_Checkout_Access::getInvalidCheckoutAccess();
714
+		if ($invalid_checkout_access->checkoutAccessIsInvalid($this->checkout)) {
715
+			return true;
716
+		}
717
+		return false;
718
+	}
719
+
720
+
721
+
722
+	/**
723
+	 *    _get_first_step
724
+	 *  gets slug for first step in $_reg_steps_array
725
+	 *
726
+	 * @access    private
727
+	 * @throws EE_Error
728
+	 * @return    string
729
+	 */
730
+	private function _get_first_step()
731
+	{
732
+		$first_step = reset(EED_Single_Page_Checkout::$_reg_steps_array);
733
+		return isset($first_step['slug']) ? $first_step['slug'] : 'attendee_information';
734
+	}
735
+
736
+
737
+
738
+	/**
739
+	 *    _load_and_instantiate_reg_steps
740
+	 *  instantiates each reg step based on the loaded reg_steps array
741
+	 *
742
+	 * @access    private
743
+	 * @throws EE_Error
744
+	 * @return    bool
745
+	 */
746
+	private function _load_and_instantiate_reg_steps()
747
+	{
748
+		do_action('AHEE__Single_Page_Checkout___load_and_instantiate_reg_steps__start', $this->checkout);
749
+		// have reg_steps already been instantiated ?
750
+		if (
751
+			empty($this->checkout->reg_steps)
752
+			|| apply_filters('FHEE__Single_Page_Checkout__load_reg_steps__reload_reg_steps', false, $this->checkout)
753
+		) {
754
+			// if not, then loop through raw reg steps array
755
+			foreach (EED_Single_Page_Checkout::$_reg_steps_array as $order => $reg_step) {
756
+				if ( ! $this->_load_and_instantiate_reg_step($reg_step, $order)) {
757
+					return false;
758
+				}
759
+			}
760
+			EE_Registry::instance()->CFG->registration->skip_reg_confirmation = true;
761
+			EE_Registry::instance()->CFG->registration->reg_confirmation_last = true;
762
+			// skip the registration_confirmation page ?
763
+			if (EE_Registry::instance()->CFG->registration->skip_reg_confirmation) {
764
+				// just remove it from the reg steps array
765
+				$this->checkout->remove_reg_step('registration_confirmation', false);
766
+			} else if (
767
+				isset($this->checkout->reg_steps['registration_confirmation'])
768
+				&& EE_Registry::instance()->CFG->registration->reg_confirmation_last
769
+			) {
770
+				// set the order to something big like 100
771
+				$this->checkout->set_reg_step_order('registration_confirmation', 100);
772
+			}
773
+			// filter the array for good luck
774
+			$this->checkout->reg_steps = apply_filters(
775
+				'FHEE__Single_Page_Checkout__load_reg_steps__reg_steps',
776
+				$this->checkout->reg_steps
777
+			);
778
+			// finally re-sort based on the reg step class order properties
779
+			$this->checkout->sort_reg_steps();
780
+		} else {
781
+			foreach ($this->checkout->reg_steps as $reg_step) {
782
+				// set all current step stati to FALSE
783
+				$reg_step->set_is_current_step(false);
784
+			}
785
+		}
786
+		if (empty($this->checkout->reg_steps)) {
787
+			EE_Error::add_error(
788
+				__('No Reg Steps were loaded..', 'event_espresso'),
789
+				__FILE__, __FUNCTION__, __LINE__
790
+			);
791
+			return false;
792
+		}
793
+		// make reg step details available to JS
794
+		$this->checkout->set_reg_step_JSON_info();
795
+		return true;
796
+	}
797
+
798
+
799
+
800
+	/**
801
+	 *     _load_and_instantiate_reg_step
802
+	 *
803
+	 * @access    private
804
+	 * @param array $reg_step
805
+	 * @param int   $order
806
+	 * @return bool
807
+	 */
808
+	private function _load_and_instantiate_reg_step($reg_step = array(), $order = 0)
809
+	{
810
+		// we need a file_path, class_name, and slug to add a reg step
811
+		if (isset($reg_step['file_path'], $reg_step['class_name'], $reg_step['slug'])) {
812
+			// if editing a specific step, but this is NOT that step... (and it's not the 'finalize_registration' step)
813
+			if (
814
+				$this->checkout->reg_url_link
815
+				&& $this->checkout->step !== $reg_step['slug']
816
+				&& $reg_step['slug'] !== 'finalize_registration'
817
+				// normally at this point we would NOT load the reg step, but this filter can change that
818
+				&& apply_filters(
819
+					'FHEE__Single_Page_Checkout___load_and_instantiate_reg_step__bypass_reg_step',
820
+					true,
821
+					$reg_step,
822
+					$this->checkout
823
+				)
824
+			) {
825
+				return true;
826
+			}
827
+			// instantiate step class using file path and class name
828
+			$reg_step_obj = EE_Registry::instance()->load_file(
829
+				$reg_step['file_path'],
830
+				$reg_step['class_name'],
831
+				'class',
832
+				$this->checkout,
833
+				false
834
+			);
835
+			// did we gets the goods ?
836
+			if ($reg_step_obj instanceof EE_SPCO_Reg_Step) {
837
+				// set reg step order based on config
838
+				$reg_step_obj->set_order($order);
839
+				// add instantiated reg step object to the master reg steps array
840
+				$this->checkout->add_reg_step($reg_step_obj);
841
+			} else {
842
+				EE_Error::add_error(
843
+					__('The current step could not be set.', 'event_espresso'),
844
+					__FILE__, __FUNCTION__, __LINE__
845
+				);
846
+				return false;
847
+			}
848
+		} else {
849
+			if (WP_DEBUG) {
850
+				EE_Error::add_error(
851
+					sprintf(
852
+						__(
853
+							'A registration step could not be loaded. One or more of the following data points is invalid:%4$s%5$sFile Path: %1$s%6$s%5$sClass Name: %2$s%6$s%5$sSlug: %3$s%6$s%7$s',
854
+							'event_espresso'
855
+						),
856
+						isset($reg_step['file_path']) ? $reg_step['file_path'] : '',
857
+						isset($reg_step['class_name']) ? $reg_step['class_name'] : '',
858
+						isset($reg_step['slug']) ? $reg_step['slug'] : '',
859
+						'<ul>',
860
+						'<li>',
861
+						'</li>',
862
+						'</ul>'
863
+					),
864
+					__FILE__, __FUNCTION__, __LINE__
865
+				);
866
+			}
867
+			return false;
868
+		}
869
+		return true;
870
+	}
871
+
872
+
873
+	/**
874
+	 * _verify_transaction_and_get_registrations
875
+	 *
876
+	 * @access private
877
+	 * @return bool
878
+	 * @throws InvalidDataTypeException
879
+	 * @throws InvalidEntityException
880
+	 * @throws EE_Error
881
+	 */
882
+	private function _verify_transaction_and_get_registrations()
883
+	{
884
+		// was there already a valid transaction in the checkout from the session ?
885
+		if ( ! $this->checkout->transaction instanceof EE_Transaction) {
886
+			// get transaction from db or session
887
+			$this->checkout->transaction = $this->checkout->reg_url_link && ! is_admin()
888
+				? $this->_get_transaction_and_cart_for_previous_visit()
889
+				: $this->_get_cart_for_current_session_and_setup_new_transaction();
890
+			if ( ! $this->checkout->transaction instanceof EE_Transaction) {
891
+				EE_Error::add_error(
892
+					__('Your Registration and Transaction information could not be retrieved from the db.',
893
+						'event_espresso'),
894
+					__FILE__, __FUNCTION__, __LINE__
895
+				);
896
+				$this->checkout->transaction = EE_Transaction::new_instance();
897
+				// add some style and make it dance
898
+				$this->add_styles_and_scripts();
899
+				EED_Single_Page_Checkout::$_initialized = true;
900
+				return false;
901
+			}
902
+			// and the registrations for the transaction
903
+			$this->_get_registrations($this->checkout->transaction);
904
+		}
905
+		return true;
906
+	}
907
+
908
+
909
+
910
+	/**
911
+	 * _get_transaction_and_cart_for_previous_visit
912
+	 *
913
+	 * @access private
914
+	 * @return mixed EE_Transaction|NULL
915
+	 */
916
+	private function _get_transaction_and_cart_for_previous_visit()
917
+	{
918
+		/** @var $TXN_model EEM_Transaction */
919
+		$TXN_model = EE_Registry::instance()->load_model('Transaction');
920
+		// because the reg_url_link is present in the request,
921
+		// this is a return visit to SPCO, so we'll get the transaction data from the db
922
+		$transaction = $TXN_model->get_transaction_from_reg_url_link($this->checkout->reg_url_link);
923
+		// verify transaction
924
+		if ($transaction instanceof EE_Transaction) {
925
+			// and get the cart that was used for that transaction
926
+			$this->checkout->cart = $this->_get_cart_for_transaction($transaction);
927
+			return $transaction;
928
+		}
929
+		EE_Error::add_error(
930
+			__('Your Registration and Transaction information could not be retrieved from the db.', 'event_espresso'),
931
+			__FILE__, __FUNCTION__, __LINE__
932
+		);
933
+		return null;
934
+
935
+	}
936
+
937
+
938
+
939
+	/**
940
+	 * _get_cart_for_transaction
941
+	 *
942
+	 * @access private
943
+	 * @param EE_Transaction $transaction
944
+	 * @return EE_Cart
945
+	 */
946
+	private function _get_cart_for_transaction($transaction)
947
+	{
948
+		return $this->checkout->get_cart_for_transaction($transaction);
949
+	}
950
+
951
+
952
+
953
+	/**
954
+	 * get_cart_for_transaction
955
+	 *
956
+	 * @access public
957
+	 * @param EE_Transaction $transaction
958
+	 * @return EE_Cart
959
+	 */
960
+	public function get_cart_for_transaction(EE_Transaction $transaction)
961
+	{
962
+		return $this->checkout->get_cart_for_transaction($transaction);
963
+	}
964
+
965
+
966
+
967
+	/**
968
+	 * _get_transaction_and_cart_for_current_session
969
+	 *    generates a new EE_Transaction object and adds it to the $_transaction property.
970
+	 *
971
+	 * @access private
972
+	 * @return EE_Transaction
973
+	 * @throws EE_Error
974
+	 */
975
+	private function _get_cart_for_current_session_and_setup_new_transaction()
976
+	{
977
+		//  if there's no transaction, then this is the FIRST visit to SPCO
978
+		// so load up the cart ( passing nothing for the TXN because it doesn't exist yet )
979
+		$this->checkout->cart = $this->_get_cart_for_transaction(null);
980
+		// and then create a new transaction
981
+		$transaction = $this->_initialize_transaction();
982
+		// verify transaction
983
+		if ($transaction instanceof EE_Transaction) {
984
+			// save it so that we have an ID for other objects to use
985
+			$transaction->save();
986
+			// and save TXN data to the cart
987
+			$this->checkout->cart->get_grand_total()->save_this_and_descendants_to_txn($transaction->ID());
988
+		} else {
989
+			EE_Error::add_error(
990
+				__('A Valid Transaction could not be initialized.', 'event_espresso'),
991
+				__FILE__, __FUNCTION__, __LINE__
992
+			);
993
+		}
994
+		return $transaction;
995
+	}
996
+
997
+
998
+
999
+	/**
1000
+	 *    generates a new EE_Transaction object and adds it to the $_transaction property.
1001
+	 *
1002
+	 * @access private
1003
+	 * @return mixed EE_Transaction|NULL
1004
+	 */
1005
+	private function _initialize_transaction()
1006
+	{
1007
+		try {
1008
+			// ensure cart totals have been calculated
1009
+			$this->checkout->cart->get_grand_total()->recalculate_total_including_taxes();
1010
+			// grab the cart grand total
1011
+			$cart_total = $this->checkout->cart->get_cart_grand_total();
1012
+			// create new TXN
1013
+			$transaction = EE_Transaction::new_instance(
1014
+				array(
1015
+					'TXN_reg_steps' => $this->checkout->initialize_txn_reg_steps_array(),
1016
+					'TXN_total'     => $cart_total > 0 ? $cart_total : 0,
1017
+					'TXN_paid'      => 0,
1018
+					'STS_ID'        => EEM_Transaction::failed_status_code,
1019
+				)
1020
+			);
1021
+			// save it so that we have an ID for other objects to use
1022
+			$transaction->save();
1023
+			// set cron job for following up on TXNs after their session has expired
1024
+			EE_Cron_Tasks::schedule_expired_transaction_check(
1025
+				EE_Registry::instance()->SSN->expiration() + 1,
1026
+				$transaction->ID()
1027
+			);
1028
+			return $transaction;
1029
+		} catch (Exception $e) {
1030
+			EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
1031
+		}
1032
+		return null;
1033
+	}
1034
+
1035
+
1036
+	/**
1037
+	 * _get_registrations
1038
+	 *
1039
+	 * @access private
1040
+	 * @param EE_Transaction $transaction
1041
+	 * @return void
1042
+	 * @throws InvalidDataTypeException
1043
+	 * @throws InvalidEntityException
1044
+	 * @throws EE_Error
1045
+	 */
1046
+	private function _get_registrations(EE_Transaction $transaction)
1047
+	{
1048
+		// first step: grab the registrants  { : o
1049
+		$registrations = $transaction->registrations($this->checkout->reg_cache_where_params, false);
1050
+		$this->checkout->total_ticket_count = count($registrations);
1051
+		// verify registrations have been set
1052
+		if (empty($registrations)) {
1053
+			// if no cached registrations, then check the db
1054
+			$registrations = $transaction->registrations($this->checkout->reg_cache_where_params, false);
1055
+			// still nothing ? well as long as this isn't a revisit
1056
+			if (empty($registrations) && ! $this->checkout->revisit) {
1057
+				// generate new registrations from scratch
1058
+				$registrations = $this->_initialize_registrations($transaction);
1059
+			}
1060
+		}
1061
+		// sort by their original registration order
1062
+		usort($registrations, array('EED_Single_Page_Checkout', 'sort_registrations_by_REG_count'));
1063
+		// then loop thru the array
1064
+		foreach ($registrations as $registration) {
1065
+			// verify each registration
1066
+			if ($registration instanceof EE_Registration) {
1067
+				// we display all attendee info for the primary registrant
1068
+				if ($this->checkout->reg_url_link === $registration->reg_url_link()
1069
+					&& $registration->is_primary_registrant()
1070
+				) {
1071
+					$this->checkout->primary_revisit = true;
1072
+					break;
1073
+				}
1074
+				if ($this->checkout->revisit && $this->checkout->reg_url_link !== $registration->reg_url_link()) {
1075
+					// but hide info if it doesn't belong to you
1076
+					$transaction->clear_cache('Registration', $registration->ID());
1077
+					$this->checkout->total_ticket_count--;
1078
+				}
1079
+				$this->checkout->set_reg_status_updated($registration->ID(), false);
1080
+			}
1081
+		}
1082
+	}
1083
+
1084
+
1085
+	/**
1086
+	 *    adds related EE_Registration objects for each ticket in the cart to the current EE_Transaction object
1087
+	 *
1088
+	 * @access private
1089
+	 * @param EE_Transaction $transaction
1090
+	 * @return    array
1091
+	 * @throws InvalidDataTypeException
1092
+	 * @throws InvalidEntityException
1093
+	 * @throws EE_Error
1094
+	 */
1095
+	private function _initialize_registrations(EE_Transaction $transaction)
1096
+	{
1097
+		$att_nmbr = 0;
1098
+		$registrations = array();
1099
+		if ($transaction instanceof EE_Transaction) {
1100
+			/** @type EE_Registration_Processor $registration_processor */
1101
+			$registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
1102
+			$this->checkout->total_ticket_count = $this->checkout->cart->all_ticket_quantity_count();
1103
+			// now let's add the cart items to the $transaction
1104
+			foreach ($this->checkout->cart->get_tickets() as $line_item) {
1105
+				//do the following for each ticket of this type they selected
1106
+				for ($x = 1; $x <= $line_item->quantity(); $x++) {
1107
+					$att_nmbr++;
1108
+					/** @var EventEspresso\core\services\commands\registration\CreateRegistrationCommand $CreateRegistrationCommand */
1109
+					$CreateRegistrationCommand = EE_Registry::instance()->create(
1110
+						'EventEspresso\core\services\commands\registration\CreateRegistrationCommand',
1111
+						array(
1112
+							$transaction,
1113
+							$line_item,
1114
+							$att_nmbr,
1115
+							$this->checkout->total_ticket_count,
1116
+						)
1117
+					);
1118
+					// override capabilities for frontend registrations
1119
+					if ( ! is_admin()) {
1120
+						$CreateRegistrationCommand->setCapCheck(
1121
+							new PublicCapabilities('', 'create_new_registration')
1122
+						);
1123
+					}
1124
+					$registration = EE_Registry::instance()->BUS->execute($CreateRegistrationCommand);
1125
+					if ( ! $registration instanceof EE_Registration) {
1126
+						throw new InvalidEntityException($registration, 'EE_Registration');
1127
+					}
1128
+					$registrations[ $registration->ID() ] = $registration;
1129
+				}
1130
+			}
1131
+			$registration_processor->fix_reg_final_price_rounding_issue($transaction);
1132
+		}
1133
+		return $registrations;
1134
+	}
1135
+
1136
+
1137
+
1138
+	/**
1139
+	 * sorts registrations by REG_count
1140
+	 *
1141
+	 * @access public
1142
+	 * @param EE_Registration $reg_A
1143
+	 * @param EE_Registration $reg_B
1144
+	 * @return int
1145
+	 */
1146
+	public static function sort_registrations_by_REG_count(EE_Registration $reg_A, EE_Registration $reg_B)
1147
+	{
1148
+		// this shouldn't ever happen within the same TXN, but oh well
1149
+		if ($reg_A->count() === $reg_B->count()) {
1150
+			return 0;
1151
+		}
1152
+		return ($reg_A->count() > $reg_B->count()) ? 1 : -1;
1153
+	}
1154
+
1155
+
1156
+
1157
+	/**
1158
+	 *    _final_verifications
1159
+	 * just makes sure that everything is set up correctly before proceeding
1160
+	 *
1161
+	 * @access    private
1162
+	 * @return    bool
1163
+	 * @throws EE_Error
1164
+	 */
1165
+	private function _final_verifications()
1166
+	{
1167
+		// filter checkout
1168
+		$this->checkout = apply_filters(
1169
+			'FHEE__EED_Single_Page_Checkout___final_verifications__checkout',
1170
+			$this->checkout
1171
+		);
1172
+		//verify that current step is still set correctly
1173
+		if ( ! $this->checkout->current_step instanceof EE_SPCO_Reg_Step) {
1174
+			EE_Error::add_error(
1175
+				__('We\'re sorry but the registration process can not proceed because one or more registration steps were not setup correctly. Please refresh the page and try again or contact support.', 'event_espresso'),
1176
+				__FILE__,
1177
+				__FUNCTION__,
1178
+				__LINE__
1179
+			);
1180
+			return false;
1181
+		}
1182
+		// if returning to SPCO, then verify that primary registrant is set
1183
+		if ( ! empty($this->checkout->reg_url_link)) {
1184
+			$valid_registrant = $this->checkout->transaction->primary_registration();
1185
+			if ( ! $valid_registrant instanceof EE_Registration) {
1186
+				EE_Error::add_error(
1187
+					__('We\'re sorry but there appears to be an error with the "reg_url_link" or the primary registrant for this transaction. Please refresh the page and try again or contact support.', 'event_espresso'),
1188
+					__FILE__,
1189
+					__FUNCTION__,
1190
+					__LINE__
1191
+				);
1192
+				return false;
1193
+			}
1194
+			$valid_registrant = null;
1195
+			foreach (
1196
+				$this->checkout->transaction->registrations($this->checkout->reg_cache_where_params) as $registration
1197
+			) {
1198
+				if (
1199
+					$registration instanceof EE_Registration
1200
+					&& $registration->reg_url_link() === $this->checkout->reg_url_link
1201
+				) {
1202
+					$valid_registrant = $registration;
1203
+				}
1204
+			}
1205
+			if ( ! $valid_registrant instanceof EE_Registration) {
1206
+				// hmmm... maybe we have the wrong session because the user is opening multiple tabs ?
1207
+				if (EED_Single_Page_Checkout::$_checkout_verified) {
1208
+					// clear the session, mark the checkout as unverified, and try again
1209
+					EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__);
1210
+					EED_Single_Page_Checkout::$_initialized = false;
1211
+					EED_Single_Page_Checkout::$_checkout_verified = false;
1212
+					$this->_initialize();
1213
+					EE_Error::reset_notices();
1214
+					return false;
1215
+				}
1216
+				EE_Error::add_error(
1217
+					__(
1218
+						'We\'re sorry but there appears to be an error with the "reg_url_link" or the transaction itself. Please refresh the page and try again or contact support.',
1219
+						'event_espresso'
1220
+					),
1221
+					__FILE__,
1222
+					__FUNCTION__,
1223
+					__LINE__
1224
+				);
1225
+				return false;
1226
+			}
1227
+		}
1228
+		// now that things have been kinda sufficiently verified,
1229
+		// let's add the checkout to the session so that it's available to other systems
1230
+		EE_Registry::instance()->SSN->set_checkout($this->checkout);
1231
+		return true;
1232
+	}
1233
+
1234
+
1235
+
1236
+	/**
1237
+	 *    _initialize_reg_steps
1238
+	 * first makes sure that EE_Transaction_Processor::set_reg_step_initiated() is called as required
1239
+	 * then loops thru all of the active reg steps and calls the initialize_reg_step() method
1240
+	 *
1241
+	 * @access    private
1242
+	 * @param bool $reinitializing
1243
+	 * @throws EE_Error
1244
+	 */
1245
+	private function _initialize_reg_steps($reinitializing = false)
1246
+	{
1247
+		$this->checkout->set_reg_step_initiated($this->checkout->current_step);
1248
+		// loop thru all steps to call their individual "initialize" methods and set i18n strings for JS
1249
+		foreach ($this->checkout->reg_steps as $reg_step) {
1250
+			if ( ! $reg_step->initialize_reg_step()) {
1251
+				// if not initialized then maybe this step is being removed...
1252
+				if ( ! $reinitializing && $reg_step->is_current_step()) {
1253
+					// if it was the current step, then we need to start over here
1254
+					$this->_initialize_reg_steps(true);
1255
+					return;
1256
+				}
1257
+				continue;
1258
+			}
1259
+			// add css and JS for current step
1260
+			$reg_step->enqueue_styles_and_scripts();
1261
+			// i18n
1262
+			$reg_step->translate_js_strings();
1263
+			if ($reg_step->is_current_step()) {
1264
+				// the text that appears on the reg step form submit button
1265
+				$reg_step->set_submit_button_text();
1266
+			}
1267
+		}
1268
+		// dynamically creates hook point like: AHEE__Single_Page_Checkout___initialize_reg_step__attendee_information
1269
+		do_action(
1270
+			"AHEE__Single_Page_Checkout___initialize_reg_step__{$this->checkout->current_step->slug()}",
1271
+			$this->checkout->current_step
1272
+		);
1273
+	}
1274
+
1275
+
1276
+
1277
+	/**
1278
+	 * _check_form_submission
1279
+	 *
1280
+	 * @access private
1281
+	 * @return boolean
1282
+	 */
1283
+	private function _check_form_submission()
1284
+	{
1285
+		//does this request require the reg form to be generated ?
1286
+		if ($this->checkout->generate_reg_form) {
1287
+			// ever heard that song by Blue Rodeo ?
1288
+			try {
1289
+				$this->checkout->current_step->reg_form = $this->checkout->current_step->generate_reg_form();
1290
+				// if not displaying a form, then check for form submission
1291
+				if (
1292
+					$this->checkout->process_form_submission
1293
+					&& $this->checkout->current_step->reg_form->was_submitted()
1294
+				) {
1295
+					// clear out any old data in case this step is being run again
1296
+					$this->checkout->current_step->set_valid_data(array());
1297
+					// capture submitted form data
1298
+					$this->checkout->current_step->reg_form->receive_form_submission(
1299
+						apply_filters(
1300
+							'FHEE__Single_Page_Checkout___check_form_submission__request_params',
1301
+							EE_Registry::instance()->REQ->params(),
1302
+							$this->checkout
1303
+						)
1304
+					);
1305
+					// validate submitted form data
1306
+					if ( ! $this->checkout->continue_reg || ! $this->checkout->current_step->reg_form->is_valid()) {
1307
+						// thou shall not pass !!!
1308
+						$this->checkout->continue_reg = false;
1309
+						// any form validation errors?
1310
+						if ($this->checkout->current_step->reg_form->submission_error_message() !== '') {
1311
+							EE_Error::add_error(
1312
+								$this->checkout->current_step->reg_form->submission_error_message(),
1313
+								__FILE__, __FUNCTION__, __LINE__
1314
+							);
1315
+						}
1316
+						// well not really... what will happen is
1317
+						// we'll just get redirected back to redo the current step
1318
+						$this->go_to_next_step();
1319
+						return false;
1320
+					}
1321
+				}
1322
+			} catch (EE_Error $e) {
1323
+				$e->get_error();
1324
+			}
1325
+		}
1326
+		return true;
1327
+	}
1328
+
1329
+
1330
+
1331
+	/**
1332
+	 * _process_action
1333
+	 *
1334
+	 * @access private
1335
+	 * @return void
1336
+	 * @throws EE_Error
1337
+	 */
1338
+	private function _process_form_action()
1339
+	{
1340
+		// what cha wanna do?
1341
+		switch ($this->checkout->action) {
1342
+			// AJAX next step reg form
1343
+			case 'display_spco_reg_step' :
1344
+				$this->checkout->redirect = false;
1345
+				if (EE_Registry::instance()->REQ->ajax) {
1346
+					$this->checkout->json_response->set_reg_step_html(
1347
+						$this->checkout->current_step->display_reg_form()
1348
+					);
1349
+				}
1350
+				break;
1351
+			default :
1352
+				// meh... do one of those other steps first
1353
+				if (
1354
+					! empty($this->checkout->action)
1355
+					&& is_callable(array($this->checkout->current_step, $this->checkout->action))
1356
+				) {
1357
+					// dynamically creates hook point like:
1358
+					//   AHEE__Single_Page_Checkout__before_attendee_information__process_reg_step
1359
+					do_action(
1360
+						"AHEE__Single_Page_Checkout__before_{$this->checkout->current_step->slug()}__{$this->checkout->action}",
1361
+						$this->checkout->current_step
1362
+					);
1363
+					// call action on current step
1364
+					if (call_user_func(array($this->checkout->current_step, $this->checkout->action))) {
1365
+						// good registrant, you get to proceed
1366
+						if (
1367
+							$this->checkout->current_step->success_message() !== ''
1368
+							&& apply_filters(
1369
+								'FHEE__Single_Page_Checkout___process_form_action__display_success',
1370
+								false
1371
+							)
1372
+						) {
1373
+							EE_Error::add_success(
1374
+								$this->checkout->current_step->success_message()
1375
+								. '<br />' . $this->checkout->next_step->_instructions()
1376
+							);
1377
+						}
1378
+						// pack it up, pack it in...
1379
+						$this->_setup_redirect();
1380
+					}
1381
+					// dynamically creates hook point like:
1382
+					//  AHEE__Single_Page_Checkout__after_payment_options__process_reg_step
1383
+					do_action(
1384
+						"AHEE__Single_Page_Checkout__after_{$this->checkout->current_step->slug()}__{$this->checkout->action}",
1385
+						$this->checkout->current_step
1386
+					);
1387
+				} else {
1388
+					EE_Error::add_error(
1389
+						sprintf(
1390
+							__(
1391
+								'The requested form action "%s" does not exist for the current "%s" registration step.',
1392
+								'event_espresso'
1393
+							),
1394
+							$this->checkout->action,
1395
+							$this->checkout->current_step->name()
1396
+						),
1397
+						__FILE__,
1398
+						__FUNCTION__,
1399
+						__LINE__
1400
+					);
1401
+				}
1402
+			// end default
1403
+		}
1404
+		// store our progress so far
1405
+		$this->checkout->stash_transaction_and_checkout();
1406
+		// advance to the next step! If you pass GO, collect $200
1407
+		$this->go_to_next_step();
1408
+	}
1409
+
1410
+
1411
+
1412
+	/**
1413
+	 *        add_styles_and_scripts
1414
+	 *
1415
+	 * @access        public
1416
+	 * @return        void
1417
+	 */
1418
+	public function add_styles_and_scripts()
1419
+	{
1420
+		// i18n
1421
+		$this->translate_js_strings();
1422
+		if ($this->checkout->admin_request) {
1423
+			add_action('admin_enqueue_scripts', array($this, 'enqueue_styles_and_scripts'), 10);
1424
+		} else {
1425
+			add_action('wp_enqueue_scripts', array($this, 'enqueue_styles_and_scripts'), 10);
1426
+		}
1427
+	}
1428
+
1429
+
1430
+
1431
+	/**
1432
+	 *        translate_js_strings
1433
+	 *
1434
+	 * @access        public
1435
+	 * @return        void
1436
+	 */
1437
+	public function translate_js_strings()
1438
+	{
1439
+		EE_Registry::$i18n_js_strings['revisit'] = $this->checkout->revisit;
1440
+		EE_Registry::$i18n_js_strings['e_reg_url_link'] = $this->checkout->reg_url_link;
1441
+		EE_Registry::$i18n_js_strings['server_error'] = __(
1442
+			'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again or contact support.',
1443
+			'event_espresso'
1444
+		);
1445
+		EE_Registry::$i18n_js_strings['invalid_json_response'] = __(
1446
+			'An invalid response was returned from the server while attempting to process your request. Please refresh the page and try again or contact support.',
1447
+			'event_espresso'
1448
+		);
1449
+		EE_Registry::$i18n_js_strings['validation_error'] = __(
1450
+			'There appears to be a problem with the form validation configuration! Please check the admin settings or contact support.',
1451
+			'event_espresso'
1452
+		);
1453
+		EE_Registry::$i18n_js_strings['invalid_payment_method'] = __(
1454
+			'There appears to be a problem with the payment method configuration! Please refresh the page and try again or contact support.',
1455
+			'event_espresso'
1456
+		);
1457
+		EE_Registry::$i18n_js_strings['reg_step_error'] = __(
1458
+			'This registration step could not be completed. Please refresh the page and try again.',
1459
+			'event_espresso'
1460
+		);
1461
+		EE_Registry::$i18n_js_strings['invalid_coupon'] = __(
1462
+			'We\'re sorry but that coupon code does not appear to be valid. If this is incorrect, please contact the site administrator.',
1463
+			'event_espresso'
1464
+		);
1465
+		EE_Registry::$i18n_js_strings['process_registration'] = sprintf(
1466
+			__(
1467
+				'Please wait while we process your registration.%sDo not refresh the page or navigate away while this is happening.%sThank you for your patience.',
1468
+				'event_espresso'
1469
+			),
1470
+			'<br/>',
1471
+			'<br/>'
1472
+		);
1473
+		EE_Registry::$i18n_js_strings['language'] = get_bloginfo('language');
1474
+		EE_Registry::$i18n_js_strings['EESID'] = EE_Registry::instance()->SSN->id();
1475
+		EE_Registry::$i18n_js_strings['currency'] = EE_Registry::instance()->CFG->currency;
1476
+		EE_Registry::$i18n_js_strings['datepicker_yearRange'] = '-150:+20';
1477
+		EE_Registry::$i18n_js_strings['timer_years'] = __('years', 'event_espresso');
1478
+		EE_Registry::$i18n_js_strings['timer_months'] = __('months', 'event_espresso');
1479
+		EE_Registry::$i18n_js_strings['timer_weeks'] = __('weeks', 'event_espresso');
1480
+		EE_Registry::$i18n_js_strings['timer_days'] = __('days', 'event_espresso');
1481
+		EE_Registry::$i18n_js_strings['timer_hours'] = __('hours', 'event_espresso');
1482
+		EE_Registry::$i18n_js_strings['timer_minutes'] = __('minutes', 'event_espresso');
1483
+		EE_Registry::$i18n_js_strings['timer_seconds'] = __('seconds', 'event_espresso');
1484
+		EE_Registry::$i18n_js_strings['timer_year'] = __('year', 'event_espresso');
1485
+		EE_Registry::$i18n_js_strings['timer_month'] = __('month', 'event_espresso');
1486
+		EE_Registry::$i18n_js_strings['timer_week'] = __('week', 'event_espresso');
1487
+		EE_Registry::$i18n_js_strings['timer_day'] = __('day', 'event_espresso');
1488
+		EE_Registry::$i18n_js_strings['timer_hour'] = __('hour', 'event_espresso');
1489
+		EE_Registry::$i18n_js_strings['timer_minute'] = __('minute', 'event_espresso');
1490
+		EE_Registry::$i18n_js_strings['timer_second'] = __('second', 'event_espresso');
1491
+		EE_Registry::$i18n_js_strings['registration_expiration_notice'] = sprintf(
1492
+			__(
1493
+				'%1$sWe\'re sorry, but your registration time has expired.%2$s%3$s%4$sIf you still wish to complete your registration, please return to the %5$sEvent List%6$sEvent List%7$s and reselect your tickets if available. Please except our apologies for any inconvenience this may have caused.%8$s',
1494
+				'event_espresso'
1495
+			),
1496
+			'<h4 class="important-notice">',
1497
+			'</h4>',
1498
+			'<br />',
1499
+			'<p>',
1500
+			'<a href="' . get_post_type_archive_link('espresso_events') . '" title="',
1501
+			'">',
1502
+			'</a>',
1503
+			'</p>'
1504
+		);
1505
+		EE_Registry::$i18n_js_strings['ajax_submit'] = apply_filters(
1506
+			'FHEE__Single_Page_Checkout__translate_js_strings__ajax_submit',
1507
+			true
1508
+		);
1509
+		EE_Registry::$i18n_js_strings['session_extension'] = absint(
1510
+			apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS)
1511
+		);
1512
+		EE_Registry::$i18n_js_strings['session_expiration'] = gmdate(
1513
+			'M d, Y H:i:s',
1514
+			EE_Registry::instance()->SSN->expiration() + (get_option('gmt_offset') * HOUR_IN_SECONDS)
1515
+		);
1516
+	}
1517
+
1518
+
1519
+
1520
+	/**
1521
+	 *    enqueue_styles_and_scripts
1522
+	 *
1523
+	 * @access        public
1524
+	 * @return        void
1525
+	 * @throws EE_Error
1526
+	 */
1527
+	public function enqueue_styles_and_scripts()
1528
+	{
1529
+		// load css
1530
+		wp_register_style(
1531
+			'single_page_checkout',
1532
+			SPCO_CSS_URL . 'single_page_checkout.css',
1533
+			array('espresso_default'),
1534
+			EVENT_ESPRESSO_VERSION
1535
+		);
1536
+		wp_enqueue_style('single_page_checkout');
1537
+		// load JS
1538
+		wp_register_script(
1539
+			'jquery_plugin',
1540
+			EE_THIRD_PARTY_URL . 'jquery	.plugin.min.js',
1541
+			array('jquery'),
1542
+			'1.0.1',
1543
+			true
1544
+		);
1545
+		wp_register_script(
1546
+			'jquery_countdown',
1547
+			EE_THIRD_PARTY_URL . 'jquery	.countdown.min.js',
1548
+			array('jquery_plugin'),
1549
+			'2.0.2',
1550
+			true
1551
+		);
1552
+		wp_register_script(
1553
+			'single_page_checkout',
1554
+			SPCO_JS_URL . 'single_page_checkout.js',
1555
+			array('espresso_core', 'underscore', 'ee_form_section_validation', 'jquery_countdown'),
1556
+			EVENT_ESPRESSO_VERSION,
1557
+			true
1558
+		);
1559
+		if ($this->checkout->registration_form instanceof EE_Form_Section_Proper) {
1560
+			$this->checkout->registration_form->enqueue_js();
1561
+		}
1562
+		if ($this->checkout->current_step->reg_form instanceof EE_Form_Section_Proper) {
1563
+			$this->checkout->current_step->reg_form->enqueue_js();
1564
+		}
1565
+		wp_enqueue_script('single_page_checkout');
1566
+		/**
1567
+		 * global action hook for enqueueing styles and scripts with
1568
+		 * spco calls.
1569
+		 */
1570
+		do_action('AHEE__EED_Single_Page_Checkout__enqueue_styles_and_scripts', $this);
1571
+		/**
1572
+		 * dynamic action hook for enqueueing styles and scripts with spco calls.
1573
+		 * The hook will end up being something like:
1574
+		 *      AHEE__EED_Single_Page_Checkout__enqueue_styles_and_scripts__attendee_information
1575
+		 */
1576
+		do_action(
1577
+			'AHEE__EED_Single_Page_Checkout__enqueue_styles_and_scripts__' . $this->checkout->current_step->slug(),
1578
+			$this
1579
+		);
1580
+	}
1581
+
1582
+
1583
+
1584
+	/**
1585
+	 *    display the Registration Single Page Checkout Form
1586
+	 *
1587
+	 * @access    private
1588
+	 * @return    void
1589
+	 * @throws EE_Error
1590
+	 */
1591
+	private function _display_spco_reg_form()
1592
+	{
1593
+		// if registering via the admin, just display the reg form for the current step
1594
+		if ($this->checkout->admin_request) {
1595
+			EE_Registry::instance()->REQ->add_output($this->checkout->current_step->display_reg_form());
1596
+		} else {
1597
+			// add powered by EE msg
1598
+			add_action('AHEE__SPCO__reg_form_footer', array('EED_Single_Page_Checkout', 'display_registration_footer'));
1599
+			$empty_cart = count(
1600
+				$this->checkout->transaction->registrations($this->checkout->reg_cache_where_params)
1601
+			) < 1;
1602
+			EE_Registry::$i18n_js_strings['empty_cart'] = $empty_cart;
1603
+			$cookies_not_set_msg = '';
1604
+			if ($empty_cart) {
1605
+				$cookies_not_set_msg = apply_filters(
1606
+					'FHEE__Single_Page_Checkout__display_spco_reg_form__cookies_not_set_msg',
1607
+					sprintf(
1608
+						__(
1609
+							'%1$s%3$sIt appears your browser is not currently set to accept Cookies%4$s%5$sIn order to register for events, you need to enable cookies.%7$sIf you require assistance, then click the following link to learn how to %8$senable cookies%9$s%6$s%2$s',
1610
+							'event_espresso'
1611
+						),
1612
+						'<div class="ee-attention hidden" id="ee-cookies-not-set-msg">',
1613
+						'</div>',
1614
+						'<h6 class="important-notice">',
1615
+						'</h6>',
1616
+						'<p>',
1617
+						'</p>',
1618
+						'<br />',
1619
+						'<a href="http://www.whatarecookies.com/enable.asp" target="_blank">',
1620
+						'</a>'
1621
+					)
1622
+				);
1623
+			}
1624
+			$this->checkout->registration_form = new EE_Form_Section_Proper(
1625
+				array(
1626
+					'name'            => 'single-page-checkout',
1627
+					'html_id'         => 'ee-single-page-checkout-dv',
1628
+					'layout_strategy' =>
1629
+						new EE_Template_Layout(
1630
+							array(
1631
+								'layout_template_file' => SPCO_TEMPLATES_PATH . 'registration_page_wrapper.template.php',
1632
+								'template_args'        => array(
1633
+									'empty_cart'              => $empty_cart,
1634
+									'revisit'                 => $this->checkout->revisit,
1635
+									'reg_steps'               => $this->checkout->reg_steps,
1636
+									'next_step'               => $this->checkout->next_step instanceof EE_SPCO_Reg_Step
1637
+										? $this->checkout->next_step->slug()
1638
+										: '',
1639
+									'empty_msg'               => apply_filters(
1640
+										'FHEE__Single_Page_Checkout__display_spco_reg_form__empty_msg',
1641
+										sprintf(
1642
+											__(
1643
+												'You need to %1$sReturn to Events list%2$sselect at least one event%3$s before you can proceed with the registration process.',
1644
+												'event_espresso'
1645
+											),
1646
+											'<a href="'
1647
+											. get_post_type_archive_link('espresso_events')
1648
+											. '" title="',
1649
+											'">',
1650
+											'</a>'
1651
+										)
1652
+									),
1653
+									'cookies_not_set_msg'     => $cookies_not_set_msg,
1654
+									'registration_time_limit' => $this->checkout->get_registration_time_limit(),
1655
+									'session_expiration'      => gmdate(
1656
+										'M d, Y H:i:s',
1657
+										EE_Registry::instance()->SSN->expiration()
1658
+										+ (get_option('gmt_offset') * HOUR_IN_SECONDS)
1659
+									),
1660
+								),
1661
+							)
1662
+						),
1663
+				)
1664
+			);
1665
+			// load template and add to output sent that gets filtered into the_content()
1666
+			EE_Registry::instance()->REQ->add_output($this->checkout->registration_form->get_html());
1667
+		}
1668
+	}
1669
+
1670
+
1671
+
1672
+	/**
1673
+	 *    add_extra_finalize_registration_inputs
1674
+	 *
1675
+	 * @access    public
1676
+	 * @param $next_step
1677
+	 * @internal  param string $label
1678
+	 * @return void
1679
+	 */
1680
+	public function add_extra_finalize_registration_inputs($next_step)
1681
+	{
1682
+		if ($next_step === 'finalize_registration') {
1683
+			echo '<div id="spco-extra-finalize_registration-inputs-dv"></div>';
1684
+		}
1685
+	}
1686
+
1687
+
1688
+
1689
+	/**
1690
+	 *    display_registration_footer
1691
+	 *
1692
+	 * @access    public
1693
+	 * @return    string
1694
+	 */
1695
+	public static function display_registration_footer()
1696
+	{
1697
+		if (
1698
+		apply_filters(
1699
+			'FHEE__EE_Front__Controller__show_reg_footer',
1700
+			EE_Registry::instance()->CFG->admin->show_reg_footer
1701
+		)
1702
+		) {
1703
+			add_filter(
1704
+				'FHEE__EEH_Template__powered_by_event_espresso__url',
1705
+				function ($url) {
1706
+					return apply_filters('FHEE__EE_Front_Controller__registration_footer__url', $url);
1707
+				}
1708
+			);
1709
+			echo apply_filters(
1710
+				'FHEE__EE_Front_Controller__display_registration_footer',
1711
+				\EEH_Template::powered_by_event_espresso(
1712
+					'',
1713
+					'espresso-registration-footer-dv',
1714
+					array('utm_content' => 'registration_checkout')
1715
+				)
1716
+			);
1717
+		}
1718
+		return '';
1719
+	}
1720
+
1721
+
1722
+
1723
+	/**
1724
+	 *    unlock_transaction
1725
+	 *
1726
+	 * @access    public
1727
+	 * @return    void
1728
+	 * @throws EE_Error
1729
+	 */
1730
+	public function unlock_transaction()
1731
+	{
1732
+		if ($this->checkout->transaction instanceof EE_Transaction) {
1733
+			$this->checkout->transaction->unlock();
1734
+		}
1735
+	}
1736
+
1737
+
1738
+
1739
+	/**
1740
+	 *        _setup_redirect
1741
+	 *
1742
+	 * @access    private
1743
+	 * @return void
1744
+	 */
1745
+	private function _setup_redirect()
1746
+	{
1747
+		if ($this->checkout->continue_reg && $this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
1748
+			$this->checkout->redirect = true;
1749
+			if (empty($this->checkout->redirect_url)) {
1750
+				$this->checkout->redirect_url = $this->checkout->next_step->reg_step_url();
1751
+			}
1752
+			$this->checkout->redirect_url = apply_filters(
1753
+				'FHEE__EED_Single_Page_Checkout___setup_redirect__checkout_redirect_url',
1754
+				$this->checkout->redirect_url,
1755
+				$this->checkout
1756
+			);
1757
+		}
1758
+	}
1759
+
1760
+
1761
+
1762
+	/**
1763
+	 *   handle ajax message responses and redirects
1764
+	 *
1765
+	 * @access public
1766
+	 * @return void
1767
+	 * @throws EE_Error
1768
+	 */
1769
+	public function go_to_next_step()
1770
+	{
1771
+		if (EE_Registry::instance()->REQ->ajax) {
1772
+			// capture contents of output buffer we started earlier in the request, and insert into JSON response
1773
+			$this->checkout->json_response->set_unexpected_errors(ob_get_clean());
1774
+		}
1775
+		$this->unlock_transaction();
1776
+		// just return for these conditions
1777
+		if (
1778
+			$this->checkout->admin_request
1779
+			|| $this->checkout->action === 'redirect_form'
1780
+			|| $this->checkout->action === 'update_checkout'
1781
+		) {
1782
+			return;
1783
+		}
1784
+		// AJAX response
1785
+		$this->_handle_json_response();
1786
+		// redirect to next step or the Thank You page
1787
+		$this->_handle_html_redirects();
1788
+		// hmmm... must be something wrong, so let's just display the form again !
1789
+		$this->_display_spco_reg_form();
1790
+	}
1791
+
1792
+
1793
+
1794
+	/**
1795
+	 *   _handle_json_response
1796
+	 *
1797
+	 * @access protected
1798
+	 * @return void
1799
+	 */
1800
+	protected function _handle_json_response()
1801
+	{
1802
+		// if this is an ajax request
1803
+		if (EE_Registry::instance()->REQ->ajax) {
1804
+			// DEBUG LOG
1805
+			//$this->checkout->log(
1806
+			//	__CLASS__, __FUNCTION__, __LINE__,
1807
+			//	array(
1808
+			//		'json_response_redirect_url' => $this->checkout->json_response->redirect_url(),
1809
+			//		'redirect'                   => $this->checkout->redirect,
1810
+			//		'continue_reg'               => $this->checkout->continue_reg,
1811
+			//	)
1812
+			//);
1813
+			$this->checkout->json_response->set_registration_time_limit(
1814
+				$this->checkout->get_registration_time_limit()
1815
+			);
1816
+			$this->checkout->json_response->set_payment_amount($this->checkout->amount_owing);
1817
+			// just send the ajax (
1818
+			$json_response = apply_filters(
1819
+				'FHEE__EE_Single_Page_Checkout__JSON_response',
1820
+				$this->checkout->json_response
1821
+			);
1822
+			echo $json_response;
1823
+			exit();
1824
+		}
1825
+	}
1826
+
1827
+
1828
+
1829
+	/**
1830
+	 *   _handle_redirects
1831
+	 *
1832
+	 * @access protected
1833
+	 * @return void
1834
+	 */
1835
+	protected function _handle_html_redirects()
1836
+	{
1837
+		// going somewhere ?
1838
+		if ($this->checkout->redirect && ! empty($this->checkout->redirect_url)) {
1839
+			// store notices in a transient
1840
+			EE_Error::get_notices(false, true, true);
1841
+			// DEBUG LOG
1842
+			//$this->checkout->log(
1843
+			//	__CLASS__, __FUNCTION__, __LINE__,
1844
+			//	array(
1845
+			//		'headers_sent' => headers_sent(),
1846
+			//		'redirect_url'     => $this->checkout->redirect_url,
1847
+			//		'headers_list'    => headers_list(),
1848
+			//	)
1849
+			//);
1850
+			wp_safe_redirect($this->checkout->redirect_url);
1851
+			exit();
1852
+		}
1853
+	}
1854
+
1855
+
1856
+
1857
+	/**
1858
+	 *   set_checkout_anchor
1859
+	 *
1860
+	 * @access public
1861
+	 * @return void
1862
+	 */
1863
+	public function set_checkout_anchor()
1864
+	{
1865
+		echo '<a id="checkout" style="float: left; margin-left: -999em;"></a>';
1866
+	}
1867 1867
 
1868 1868
 
1869 1869
 
Please login to merge, or discard this patch.
core/libraries/form_sections/base/EE_Form_Section_Validatable.form.php 1 patch
Indentation   +147 added lines, -147 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 if (! defined('EVENT_ESPRESSO_VERSION')) {
3
-    exit('No direct script access allowed');
3
+	exit('No direct script access allowed');
4 4
 }
5 5
 
6 6
 
@@ -35,150 +35,150 @@  discard block
 block discarded – undo
35 35
 abstract class EE_Form_Section_Validatable extends EE_Form_Section_Base
36 36
 {
37 37
 
38
-    /**
39
-     * Array of validation errors in this section. Does not contain validation errors in subsections, however.
40
-     * Those are stored individually on each subsection.
41
-     *
42
-     * @var EE_Validation_Error[]
43
-     */
44
-    protected $_validation_errors = array();
45
-
46
-
47
-
48
-    /**
49
-     * Errors on this form section. Note: EE_Form_Section_Proper
50
-     * has another function for getting all errors in this form section and subsections
51
-     * called get_validation_errors_accumulated
52
-     *
53
-     * @return EE_Validation_Error[]
54
-     */
55
-    public function get_validation_errors()
56
-    {
57
-        return $this->_validation_errors;
58
-    }
59
-
60
-
61
-
62
-    /**
63
-     * returns a comma-separated list of all the validation errors in it.
64
-     * If we want this to be customizable, we may decide to create a strategy for displaying it
65
-     *
66
-     * @return string
67
-     */
68
-    public function get_validation_error_string()
69
-    {
70
-        $validation_error_messages = array();
71
-        if ($this->get_validation_errors()) {
72
-            foreach ($this->get_validation_errors() as $validation_error) {
73
-                if ($validation_error instanceof EE_Validation_Error) {
74
-                    $validation_error_messages[] = $validation_error->getMessage();
75
-                }
76
-            }
77
-        }
78
-        return implode(", ", $validation_error_messages);
79
-    }
80
-
81
-
82
-
83
-    /**
84
-     * Performs validation on this form section (and subsections). Should be called after _normalize()
85
-     *
86
-     * @return boolean of whether or not the form section is valid
87
-     */
88
-    abstract protected function _validate();
89
-
90
-
91
-
92
-    /**
93
-     * Checks if this field has any validation errors
94
-     *
95
-     * @return boolean
96
-     */
97
-    public function is_valid()
98
-    {
99
-        if (count($this->_validation_errors)) {
100
-            return false;
101
-        } else {
102
-            return true;
103
-        }
104
-    }
105
-
106
-
107
-
108
-    /**
109
-     * Sanitizes input for this form section
110
-     *
111
-     * @param array $req_data is the full request data like $_POST
112
-     * @return boolean of whether a normalization error occurred
113
-     */
114
-    abstract protected function _normalize($req_data);
115
-
116
-
117
-
118
-    /**
119
-     * Creates a validation error from the arguments provided, and adds it to the form section's list.
120
-     * If such an EE_Validation_Error object is passed in as the first arg, simply sets this as its form section, and
121
-     * adds it to the list of validation errors of errors
122
-     *
123
-     * @param mixed     $message_or_object  internationalized string describing the validation error; or it could be a
124
-     *                                      proper EE_Validation_Error object
125
-     * @param string    $error_code         a short key which can be used to uniquely identify the error
126
-     * @param Exception $previous_exception if there was an exception that caused the error, that exception
127
-     * @return void
128
-     */
129
-    public function add_validation_error($message_or_object, $error_code = null, $previous_exception = null)
130
-    {
131
-        if ($message_or_object instanceof EE_Validation_Error) {
132
-            $validation_error = $message_or_object;
133
-            $validation_error->set_form_section($this);
134
-        } else {
135
-            $validation_error = new EE_Validation_Error($message_or_object, $error_code, $this, $previous_exception);
136
-        }
137
-        $this->_validation_errors[] = $validation_error;
138
-    }
139
-
140
-
141
-
142
-    /**
143
-     * When generating the JS for the jquery validation rules like<br>
144
-     * <code>$( "#myform" ).validate({
145
-     * rules: {
146
-     * password: "required",
147
-     * password_again: {
148
-     * equalTo: "#password"
149
-     * }
150
-     * }
151
-     * });</code>
152
-     * gets the sections like
153
-     * <br><code>password: "required",
154
-     * password_again: {
155
-     * equalTo: "#password"
156
-     * }</code>
157
-     * except we leave it as a PHP object, and leave wp_localize_script to
158
-     * turn it into a JSON object which can be used by the js
159
-     *
160
-     * @return array
161
-     */
162
-    abstract public function get_jquery_validation_rules();
163
-
164
-
165
-
166
-    /**
167
-     * Checks if this form section's data is present in the req data specified
168
-     *
169
-     * @param array $req_data usually $_POST, if null that's what's used
170
-     * @return boolean
171
-     */
172
-    abstract public function form_data_present_in($req_data = null);
173
-
174
-
175
-
176
-    /**
177
-     * Removes teh sensitive data from this form section (usually done after
178
-     * utilizing the data business function, but before saving it somewhere. Eg,
179
-     * may remove a password from the form after verifying it was correct)
180
-     *
181
-     * @return void
182
-     */
183
-    abstract public function clean_sensitive_data();
38
+	/**
39
+	 * Array of validation errors in this section. Does not contain validation errors in subsections, however.
40
+	 * Those are stored individually on each subsection.
41
+	 *
42
+	 * @var EE_Validation_Error[]
43
+	 */
44
+	protected $_validation_errors = array();
45
+
46
+
47
+
48
+	/**
49
+	 * Errors on this form section. Note: EE_Form_Section_Proper
50
+	 * has another function for getting all errors in this form section and subsections
51
+	 * called get_validation_errors_accumulated
52
+	 *
53
+	 * @return EE_Validation_Error[]
54
+	 */
55
+	public function get_validation_errors()
56
+	{
57
+		return $this->_validation_errors;
58
+	}
59
+
60
+
61
+
62
+	/**
63
+	 * returns a comma-separated list of all the validation errors in it.
64
+	 * If we want this to be customizable, we may decide to create a strategy for displaying it
65
+	 *
66
+	 * @return string
67
+	 */
68
+	public function get_validation_error_string()
69
+	{
70
+		$validation_error_messages = array();
71
+		if ($this->get_validation_errors()) {
72
+			foreach ($this->get_validation_errors() as $validation_error) {
73
+				if ($validation_error instanceof EE_Validation_Error) {
74
+					$validation_error_messages[] = $validation_error->getMessage();
75
+				}
76
+			}
77
+		}
78
+		return implode(", ", $validation_error_messages);
79
+	}
80
+
81
+
82
+
83
+	/**
84
+	 * Performs validation on this form section (and subsections). Should be called after _normalize()
85
+	 *
86
+	 * @return boolean of whether or not the form section is valid
87
+	 */
88
+	abstract protected function _validate();
89
+
90
+
91
+
92
+	/**
93
+	 * Checks if this field has any validation errors
94
+	 *
95
+	 * @return boolean
96
+	 */
97
+	public function is_valid()
98
+	{
99
+		if (count($this->_validation_errors)) {
100
+			return false;
101
+		} else {
102
+			return true;
103
+		}
104
+	}
105
+
106
+
107
+
108
+	/**
109
+	 * Sanitizes input for this form section
110
+	 *
111
+	 * @param array $req_data is the full request data like $_POST
112
+	 * @return boolean of whether a normalization error occurred
113
+	 */
114
+	abstract protected function _normalize($req_data);
115
+
116
+
117
+
118
+	/**
119
+	 * Creates a validation error from the arguments provided, and adds it to the form section's list.
120
+	 * If such an EE_Validation_Error object is passed in as the first arg, simply sets this as its form section, and
121
+	 * adds it to the list of validation errors of errors
122
+	 *
123
+	 * @param mixed     $message_or_object  internationalized string describing the validation error; or it could be a
124
+	 *                                      proper EE_Validation_Error object
125
+	 * @param string    $error_code         a short key which can be used to uniquely identify the error
126
+	 * @param Exception $previous_exception if there was an exception that caused the error, that exception
127
+	 * @return void
128
+	 */
129
+	public function add_validation_error($message_or_object, $error_code = null, $previous_exception = null)
130
+	{
131
+		if ($message_or_object instanceof EE_Validation_Error) {
132
+			$validation_error = $message_or_object;
133
+			$validation_error->set_form_section($this);
134
+		} else {
135
+			$validation_error = new EE_Validation_Error($message_or_object, $error_code, $this, $previous_exception);
136
+		}
137
+		$this->_validation_errors[] = $validation_error;
138
+	}
139
+
140
+
141
+
142
+	/**
143
+	 * When generating the JS for the jquery validation rules like<br>
144
+	 * <code>$( "#myform" ).validate({
145
+	 * rules: {
146
+	 * password: "required",
147
+	 * password_again: {
148
+	 * equalTo: "#password"
149
+	 * }
150
+	 * }
151
+	 * });</code>
152
+	 * gets the sections like
153
+	 * <br><code>password: "required",
154
+	 * password_again: {
155
+	 * equalTo: "#password"
156
+	 * }</code>
157
+	 * except we leave it as a PHP object, and leave wp_localize_script to
158
+	 * turn it into a JSON object which can be used by the js
159
+	 *
160
+	 * @return array
161
+	 */
162
+	abstract public function get_jquery_validation_rules();
163
+
164
+
165
+
166
+	/**
167
+	 * Checks if this form section's data is present in the req data specified
168
+	 *
169
+	 * @param array $req_data usually $_POST, if null that's what's used
170
+	 * @return boolean
171
+	 */
172
+	abstract public function form_data_present_in($req_data = null);
173
+
174
+
175
+
176
+	/**
177
+	 * Removes teh sensitive data from this form section (usually done after
178
+	 * utilizing the data business function, but before saving it somewhere. Eg,
179
+	 * may remove a password from the form after verifying it was correct)
180
+	 *
181
+	 * @return void
182
+	 */
183
+	abstract public function clean_sensitive_data();
184 184
 }
185 185
\ No newline at end of file
Please login to merge, or discard this patch.
espresso.php 1 patch
Indentation   +191 added lines, -191 removed lines patch added patch discarded remove patch
@@ -38,216 +38,216 @@
 block discarded – undo
38 38
  * @since       4.0
39 39
  */
40 40
 if (function_exists('espresso_version')) {
41
-    if (! function_exists('espresso_duplicate_plugin_error')) {
42
-        /**
43
-         *    espresso_duplicate_plugin_error
44
-         *    displays if more than one version of EE is activated at the same time
45
-         */
46
-        function espresso_duplicate_plugin_error()
47
-        {
48
-            ?>
41
+	if (! function_exists('espresso_duplicate_plugin_error')) {
42
+		/**
43
+		 *    espresso_duplicate_plugin_error
44
+		 *    displays if more than one version of EE is activated at the same time
45
+		 */
46
+		function espresso_duplicate_plugin_error()
47
+		{
48
+			?>
49 49
             <div class="error">
50 50
                 <p>
51 51
                     <?php
52
-                    echo esc_html__(
53
-                        'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
-                        'event_espresso'
55
-                    ); ?>
52
+					echo esc_html__(
53
+						'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
+						'event_espresso'
55
+					); ?>
56 56
                 </p>
57 57
             </div>
58 58
             <?php
59
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
60
-        }
61
-    }
62
-    add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
59
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
60
+		}
61
+	}
62
+	add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
63 63
 
64 64
 } else {
65
-    define('EE_MIN_PHP_VER_REQUIRED', '5.3.9');
66
-    if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
67
-        /**
68
-         * espresso_minimum_php_version_error
69
-         * @return void
70
-         */
71
-        function espresso_minimum_php_version_error()
72
-        {
73
-            ?>
65
+	define('EE_MIN_PHP_VER_REQUIRED', '5.3.9');
66
+	if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
67
+		/**
68
+		 * espresso_minimum_php_version_error
69
+		 * @return void
70
+		 */
71
+		function espresso_minimum_php_version_error()
72
+		{
73
+			?>
74 74
             <div class="error">
75 75
                 <p>
76 76
                     <?php
77
-                    printf(
78
-                        esc_html__(
79
-                            'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
-                            'event_espresso'
81
-                        ),
82
-                        EE_MIN_PHP_VER_REQUIRED,
83
-                        PHP_VERSION,
84
-                        '<br/>',
85
-                        '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
-                    );
87
-                    ?>
77
+					printf(
78
+						esc_html__(
79
+							'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
+							'event_espresso'
81
+						),
82
+						EE_MIN_PHP_VER_REQUIRED,
83
+						PHP_VERSION,
84
+						'<br/>',
85
+						'<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
+					);
87
+					?>
88 88
                 </p>
89 89
             </div>
90 90
             <?php
91
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
92
-        }
91
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
92
+		}
93 93
 
94
-        add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
-    } else {
96
-        define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
-        /**
98
-         * espresso_version
99
-         * Returns the plugin version
100
-         *
101
-         * @return string
102
-         */
103
-        function espresso_version()
104
-        {
105
-            return apply_filters('FHEE__espresso__espresso_version', '4.9.59.rc.006');
106
-        }
94
+		add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
+	} else {
96
+		define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
+		/**
98
+		 * espresso_version
99
+		 * Returns the plugin version
100
+		 *
101
+		 * @return string
102
+		 */
103
+		function espresso_version()
104
+		{
105
+			return apply_filters('FHEE__espresso__espresso_version', '4.9.59.rc.006');
106
+		}
107 107
 
108
-        /**
109
-         * espresso_plugin_activation
110
-         * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
-         */
112
-        function espresso_plugin_activation()
113
-        {
114
-            update_option('ee_espresso_activation', true);
115
-        }
108
+		/**
109
+		 * espresso_plugin_activation
110
+		 * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
+		 */
112
+		function espresso_plugin_activation()
113
+		{
114
+			update_option('ee_espresso_activation', true);
115
+		}
116 116
 
117
-        register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
118
-        /**
119
-         *    espresso_load_error_handling
120
-         *    this function loads EE's class for handling exceptions and errors
121
-         */
122
-        function espresso_load_error_handling()
123
-        {
124
-            static $error_handling_loaded = false;
125
-            if ($error_handling_loaded) {
126
-                return;
127
-            }
128
-            // load debugging tools
129
-            if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) {
130
-                require_once   EE_HELPERS . 'EEH_Debug_Tools.helper.php';
131
-                \EEH_Debug_Tools::instance();
132
-            }
133
-            // load error handling
134
-            if (is_readable(EE_CORE . 'EE_Error.core.php')) {
135
-                require_once EE_CORE . 'EE_Error.core.php';
136
-            } else {
137
-                wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso'));
138
-            }
139
-            $error_handling_loaded = true;
140
-        }
117
+		register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
118
+		/**
119
+		 *    espresso_load_error_handling
120
+		 *    this function loads EE's class for handling exceptions and errors
121
+		 */
122
+		function espresso_load_error_handling()
123
+		{
124
+			static $error_handling_loaded = false;
125
+			if ($error_handling_loaded) {
126
+				return;
127
+			}
128
+			// load debugging tools
129
+			if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) {
130
+				require_once   EE_HELPERS . 'EEH_Debug_Tools.helper.php';
131
+				\EEH_Debug_Tools::instance();
132
+			}
133
+			// load error handling
134
+			if (is_readable(EE_CORE . 'EE_Error.core.php')) {
135
+				require_once EE_CORE . 'EE_Error.core.php';
136
+			} else {
137
+				wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso'));
138
+			}
139
+			$error_handling_loaded = true;
140
+		}
141 141
 
142
-        /**
143
-         *    espresso_load_required
144
-         *    given a class name and path, this function will load that file or throw an exception
145
-         *
146
-         * @param    string $classname
147
-         * @param    string $full_path_to_file
148
-         * @throws    EE_Error
149
-         */
150
-        function espresso_load_required($classname, $full_path_to_file)
151
-        {
152
-            if (is_readable($full_path_to_file)) {
153
-                require_once $full_path_to_file;
154
-            } else {
155
-                throw new \EE_Error (
156
-                    sprintf(
157
-                        esc_html__(
158
-                            'The %s class file could not be located or is not readable due to file permissions.',
159
-                            'event_espresso'
160
-                        ),
161
-                        $classname
162
-                    )
163
-                );
164
-            }
165
-        }
142
+		/**
143
+		 *    espresso_load_required
144
+		 *    given a class name and path, this function will load that file or throw an exception
145
+		 *
146
+		 * @param    string $classname
147
+		 * @param    string $full_path_to_file
148
+		 * @throws    EE_Error
149
+		 */
150
+		function espresso_load_required($classname, $full_path_to_file)
151
+		{
152
+			if (is_readable($full_path_to_file)) {
153
+				require_once $full_path_to_file;
154
+			} else {
155
+				throw new \EE_Error (
156
+					sprintf(
157
+						esc_html__(
158
+							'The %s class file could not be located or is not readable due to file permissions.',
159
+							'event_espresso'
160
+						),
161
+						$classname
162
+					)
163
+				);
164
+			}
165
+		}
166 166
 
167
-        /**
168
-         * @since 4.9.27
169
-         * @throws \EE_Error
170
-         * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
171
-         * @throws \EventEspresso\core\exceptions\InvalidEntityException
172
-         * @throws \EventEspresso\core\exceptions\InvalidIdentifierException
173
-         * @throws \EventEspresso\core\exceptions\InvalidClassException
174
-         * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
175
-         * @throws \EventEspresso\core\services\container\exceptions\ServiceExistsException
176
-         * @throws \EventEspresso\core\services\container\exceptions\ServiceNotFoundException
177
-         * @throws \OutOfBoundsException
178
-         */
179
-        function bootstrap_espresso()
180
-        {
181
-            require_once __DIR__ . '/core/espresso_definitions.php';
182
-            try {
183
-                espresso_load_error_handling();
184
-                espresso_load_required(
185
-                    'EEH_Base',
186
-                    EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php'
187
-                );
188
-                espresso_load_required(
189
-                    'EEH_File',
190
-                    EE_CORE . 'interfaces' . DS . 'EEHI_File.interface.php'
191
-                );
192
-                espresso_load_required(
193
-                    'EEH_File',
194
-                    EE_CORE . 'helpers' . DS . 'EEH_File.helper.php'
195
-                );
196
-                espresso_load_required(
197
-                    'EEH_Array',
198
-                    EE_CORE . 'helpers' . DS . 'EEH_Array.helper.php'
199
-                );
200
-                // instantiate and configure PSR4 autoloader
201
-                espresso_load_required(
202
-                    'Psr4Autoloader',
203
-                    EE_CORE . 'Psr4Autoloader.php'
204
-                );
205
-                espresso_load_required(
206
-                    'EE_Psr4AutoloaderInit',
207
-                    EE_CORE . 'EE_Psr4AutoloaderInit.core.php'
208
-                );
209
-                $AutoloaderInit = new EE_Psr4AutoloaderInit();
210
-                $AutoloaderInit->initializeAutoloader();
211
-                espresso_load_required(
212
-                    'EE_Request',
213
-                    EE_CORE . 'request_stack' . DS . 'EE_Request.core.php'
214
-                );
215
-                espresso_load_required(
216
-                    'EE_Response',
217
-                    EE_CORE . 'request_stack' . DS . 'EE_Response.core.php'
218
-                );
219
-                espresso_load_required(
220
-                    'EE_Bootstrap',
221
-                    EE_CORE . 'EE_Bootstrap.core.php'
222
-                );
223
-                // bootstrap EE and the request stack
224
-                new EE_Bootstrap(
225
-                    new EE_Request($_GET, $_POST, $_COOKIE),
226
-                    new EE_Response()
227
-                );
228
-            } catch (Exception $e) {
229
-                require_once EE_CORE . 'exceptions' . DS . 'ExceptionStackTraceDisplay.php';
230
-                new EventEspresso\core\exceptions\ExceptionStackTraceDisplay($e);
231
-            }
232
-        }
233
-        bootstrap_espresso();
234
-    }
167
+		/**
168
+		 * @since 4.9.27
169
+		 * @throws \EE_Error
170
+		 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
171
+		 * @throws \EventEspresso\core\exceptions\InvalidEntityException
172
+		 * @throws \EventEspresso\core\exceptions\InvalidIdentifierException
173
+		 * @throws \EventEspresso\core\exceptions\InvalidClassException
174
+		 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
175
+		 * @throws \EventEspresso\core\services\container\exceptions\ServiceExistsException
176
+		 * @throws \EventEspresso\core\services\container\exceptions\ServiceNotFoundException
177
+		 * @throws \OutOfBoundsException
178
+		 */
179
+		function bootstrap_espresso()
180
+		{
181
+			require_once __DIR__ . '/core/espresso_definitions.php';
182
+			try {
183
+				espresso_load_error_handling();
184
+				espresso_load_required(
185
+					'EEH_Base',
186
+					EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php'
187
+				);
188
+				espresso_load_required(
189
+					'EEH_File',
190
+					EE_CORE . 'interfaces' . DS . 'EEHI_File.interface.php'
191
+				);
192
+				espresso_load_required(
193
+					'EEH_File',
194
+					EE_CORE . 'helpers' . DS . 'EEH_File.helper.php'
195
+				);
196
+				espresso_load_required(
197
+					'EEH_Array',
198
+					EE_CORE . 'helpers' . DS . 'EEH_Array.helper.php'
199
+				);
200
+				// instantiate and configure PSR4 autoloader
201
+				espresso_load_required(
202
+					'Psr4Autoloader',
203
+					EE_CORE . 'Psr4Autoloader.php'
204
+				);
205
+				espresso_load_required(
206
+					'EE_Psr4AutoloaderInit',
207
+					EE_CORE . 'EE_Psr4AutoloaderInit.core.php'
208
+				);
209
+				$AutoloaderInit = new EE_Psr4AutoloaderInit();
210
+				$AutoloaderInit->initializeAutoloader();
211
+				espresso_load_required(
212
+					'EE_Request',
213
+					EE_CORE . 'request_stack' . DS . 'EE_Request.core.php'
214
+				);
215
+				espresso_load_required(
216
+					'EE_Response',
217
+					EE_CORE . 'request_stack' . DS . 'EE_Response.core.php'
218
+				);
219
+				espresso_load_required(
220
+					'EE_Bootstrap',
221
+					EE_CORE . 'EE_Bootstrap.core.php'
222
+				);
223
+				// bootstrap EE and the request stack
224
+				new EE_Bootstrap(
225
+					new EE_Request($_GET, $_POST, $_COOKIE),
226
+					new EE_Response()
227
+				);
228
+			} catch (Exception $e) {
229
+				require_once EE_CORE . 'exceptions' . DS . 'ExceptionStackTraceDisplay.php';
230
+				new EventEspresso\core\exceptions\ExceptionStackTraceDisplay($e);
231
+			}
232
+		}
233
+		bootstrap_espresso();
234
+	}
235 235
 }
236 236
 if (! function_exists('espresso_deactivate_plugin')) {
237
-    /**
238
-     *    deactivate_plugin
239
-     * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
240
-     *
241
-     * @access public
242
-     * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
243
-     * @return    void
244
-     */
245
-    function espresso_deactivate_plugin($plugin_basename = '')
246
-    {
247
-        if (! function_exists('deactivate_plugins')) {
248
-            require_once ABSPATH . 'wp-admin/includes/plugin.php';
249
-        }
250
-        unset($_GET['activate'], $_REQUEST['activate']);
251
-        deactivate_plugins($plugin_basename);
252
-    }
237
+	/**
238
+	 *    deactivate_plugin
239
+	 * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
240
+	 *
241
+	 * @access public
242
+	 * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
243
+	 * @return    void
244
+	 */
245
+	function espresso_deactivate_plugin($plugin_basename = '')
246
+	{
247
+		if (! function_exists('deactivate_plugins')) {
248
+			require_once ABSPATH . 'wp-admin/includes/plugin.php';
249
+		}
250
+		unset($_GET['activate'], $_REQUEST['activate']);
251
+		deactivate_plugins($plugin_basename);
252
+	}
253 253
 }
Please login to merge, or discard this patch.
modules/ticket_sales_monitor/EED_Ticket_Sales_Monitor.module.php 3 patches
Doc Comments   +3 added lines, -2 removed lines patch added patch discarded remove patch
@@ -326,7 +326,7 @@  discard block
 block discarded – undo
326 326
      *
327 327
      * @param    EE_Ticket $ticket
328 328
      * @param int          $quantity
329
-     * @return bool
329
+     * @return integer
330 330
      * @throws EE_Error
331 331
      */
332 332
     protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
@@ -343,7 +343,7 @@  discard block
 block discarded – undo
343 343
     /**
344 344
      * @param  EE_Ticket $ticket
345 345
      * @param  int       $quantity
346
-     * @return bool
346
+     * @return integer
347 347
      * @throws EE_Error
348 348
      */
349 349
     protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
@@ -969,6 +969,7 @@  discard block
 block discarded – undo
969 969
     /**
970 970
      * @param EE_Ticket[]    $tickets_with_reservations
971 971
      * @param EE_Line_Item[] $valid_reserved_ticket_line_items
972
+     * @param string $source
972 973
      * @return int
973 974
      * @throws UnexpectedEntityException
974 975
      * @throws DomainException
Please login to merge, or discard this patch.
Indentation   +1021 added lines, -1021 removed lines patch added patch discarded remove patch
@@ -23,1028 +23,1028 @@
 block discarded – undo
23 23
 class EED_Ticket_Sales_Monitor extends EED_Module
24 24
 {
25 25
 
26
-    const debug = false;    //	true false
27
-
28
-    /**
29
-     * an array of raw ticket data from EED_Ticket_Selector
30
-     *
31
-     * @var array $ticket_selections
32
-     */
33
-    protected $ticket_selections = array();
34
-
35
-    /**
36
-     * the raw ticket data from EED_Ticket_Selector is organized in rows
37
-     * according to how they are displayed in the actual Ticket_Selector
38
-     * this tracks the current row being processed
39
-     *
40
-     * @var int $current_row
41
-     */
42
-    protected $current_row = 0;
43
-
44
-    /**
45
-     * an array for tracking names of tickets that have sold out
46
-     *
47
-     * @var array $sold_out_tickets
48
-     */
49
-    protected $sold_out_tickets = array();
50
-
51
-    /**
52
-     * an array for tracking names of tickets that have had their quantities reduced
53
-     *
54
-     * @var array $decremented_tickets
55
-     */
56
-    protected $decremented_tickets = array();
57
-
58
-
59
-
60
-    /**
61
-     * set_hooks - for hooking into EE Core, other modules, etc
62
-     *
63
-     * @return    void
64
-     */
65
-    public static function set_hooks()
66
-    {
67
-        // release tickets for expired carts
68
-        add_action(
69
-            'EED_Ticket_Selector__process_ticket_selections__before',
70
-            array('EED_Ticket_Sales_Monitor', 'release_tickets_for_expired_carts'),
71
-            1
72
-        );
73
-        // check ticket reserves AFTER MER does it's check (hence priority 20)
74
-        add_filter(
75
-            'FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty',
76
-            array('EED_Ticket_Sales_Monitor', 'validate_ticket_sale'),
77
-            20,
78
-            3
79
-        );
80
-        // add notices for sold out tickets
81
-        add_action(
82
-            'AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart',
83
-            array('EED_Ticket_Sales_Monitor', 'post_notices'),
84
-            10
85
-        );
86
-        // handle ticket quantities adjusted in cart
87
-        //add_action(
88
-        //	'FHEE__EED_Multi_Event_Registration__adjust_line_item_quantity__line_item_quantity_updated',
89
-        //	array( 'EED_Ticket_Sales_Monitor', 'ticket_quantity_updated' ),
90
-        //	10, 2
91
-        //);
92
-        // handle tickets deleted from cart
93
-        add_action(
94
-            'FHEE__EED_Multi_Event_Registration__delete_ticket__ticket_removed_from_cart',
95
-            array('EED_Ticket_Sales_Monitor', 'ticket_removed_from_cart'),
96
-            10,
97
-            2
98
-        );
99
-        // handle emptied carts
100
-        add_action(
101
-            'AHEE__EE_Session__reset_cart__before_reset',
102
-            array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
103
-            10,
104
-            1
105
-        );
106
-        add_action(
107
-            'AHEE__EED_Multi_Event_Registration__empty_event_cart__before_delete_cart',
108
-            array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
109
-            10,
110
-            1
111
-        );
112
-        // handle cancelled registrations
113
-        add_action(
114
-            'AHEE__EE_Session__reset_checkout__before_reset',
115
-            array('EED_Ticket_Sales_Monitor', 'session_checkout_reset'),
116
-            10,
117
-            1
118
-        );
119
-        // cron tasks
120
-        add_action(
121
-            'AHEE__EE_Cron_Tasks__process_expired_transactions__abandoned_transaction',
122
-            array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
123
-            10,
124
-            1
125
-        );
126
-        add_action(
127
-            'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction',
128
-            array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
129
-            10,
130
-            1
131
-        );
132
-        add_action(
133
-            'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction',
134
-            array('EED_Ticket_Sales_Monitor', 'process_failed_transactions'),
135
-            10,
136
-            1
137
-        );
138
-    }
139
-
140
-
141
-
142
-    /**
143
-     * set_hooks_admin - for hooking into EE Admin Core, other modules, etc
144
-     *
145
-     * @return void
146
-     */
147
-    public static function set_hooks_admin()
148
-    {
149
-        EED_Ticket_Sales_Monitor::set_hooks();
150
-    }
151
-
152
-
153
-
154
-    /**
155
-     * @return EED_Ticket_Sales_Monitor|EED_Module
156
-     */
157
-    public static function instance()
158
-    {
159
-        return parent::get_instance(__CLASS__);
160
-    }
161
-
162
-
163
-
164
-    /**
165
-     * @param WP_Query $WP_Query
166
-     * @return    void
167
-     */
168
-    public function run($WP_Query)
169
-    {
170
-    }
171
-
172
-
173
-
174
-    /********************************** PRE_TICKET_SALES  **********************************/
175
-
176
-
177
-
178
-    /**
179
-     * Retrieves grand totals from the line items that have no TXN ID
180
-     * and timestamps less than the current time minus the session lifespan.
181
-     * These are carts that have been abandoned before the "registrant" even attempted to checkout.
182
-     * We're going to release the tickets for these line items before attempting to add more to the cart.
183
-     *
184
-     * @return void
185
-     * @throws DomainException
186
-     * @throws EE_Error
187
-     * @throws InvalidArgumentException
188
-     * @throws InvalidDataTypeException
189
-     * @throws InvalidInterfaceException
190
-     * @throws UnexpectedEntityException
191
-     */
192
-    public static function release_tickets_for_expired_carts()
193
-    {
194
-        do_action('AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__begin');
195
-        $expired_ticket_IDs      = array();
196
-        $valid_ticket_line_items = array();
197
-        $total_line_items        = EEM_Line_Item::instance()->get_total_line_items_with_no_transaction();
198
-        if (empty($total_line_items)) {
199
-            do_action(
200
-                'AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__end',
201
-                $total_line_items,
202
-                $valid_ticket_line_items,
203
-                $expired_ticket_IDs
204
-            );
205
-            return;
206
-        }
207
-        $expired = current_time('timestamp') - EE_Registry::instance()->SSN->lifespan();
208
-        foreach ($total_line_items as $total_line_item) {
209
-            /** @var EE_Line_Item $total_line_item */
210
-            $ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item);
211
-            foreach ($ticket_line_items as $ticket_line_item) {
212
-                if (! $ticket_line_item instanceof EE_Line_Item) {
213
-                    continue;
214
-                }
215
-                if ($total_line_item->timestamp(true) <= $expired) {
216
-                    $expired_ticket_IDs[ $ticket_line_item->OBJ_ID() ] = $ticket_line_item->OBJ_ID();
217
-                } else {
218
-                    $valid_ticket_line_items[ $ticket_line_item->OBJ_ID() ] = $ticket_line_item;
219
-                }
220
-            }
221
-        }
222
-        if (! empty($expired_ticket_IDs)) {
223
-            EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
224
-                \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
225
-                $valid_ticket_line_items,
226
-                __FUNCTION__
227
-            );
228
-            // let's get rid of expired line items so that they can't interfere with tracking
229
-            add_action(
230
-                'shutdown',
231
-                array('EED_Ticket_Sales_Monitor', 'clear_expired_line_items_with_no_transaction'),
232
-                999
233
-            );
234
-        }
235
-        do_action(
236
-            'AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__end',
237
-            $total_line_items,
238
-            $valid_ticket_line_items,
239
-            $expired_ticket_IDs
240
-        );
241
-    }
242
-
243
-
244
-
245
-    /********************************** VALIDATE_TICKET_SALE  **********************************/
246
-
247
-
248
-
249
-    /**
250
-     * callback for 'FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data'
251
-     *
252
-     * @param int       $qty
253
-     * @param EE_Ticket $ticket
254
-     * @return bool
255
-     * @throws UnexpectedEntityException
256
-     * @throws EE_Error
257
-     */
258
-    public static function validate_ticket_sale($qty = 1, EE_Ticket $ticket)
259
-    {
260
-        $qty = absint($qty);
261
-        if ($qty > 0) {
262
-            $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
263
-        }
264
-        if (self::debug) {
265
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()';
266
-            echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>';
267
-        }
268
-        return $qty;
269
-    }
270
-
271
-
272
-
273
-    /**
274
-     * checks whether an individual ticket is available for purchase based on datetime, and ticket details
275
-     *
276
-     * @param   EE_Ticket $ticket
277
-     * @param int         $qty
278
-     * @return int
279
-     * @throws UnexpectedEntityException
280
-     * @throws EE_Error
281
-     */
282
-    protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
283
-    {
284
-        if (self::debug) {
285
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
286
-        }
287
-        if (! $ticket instanceof EE_Ticket) {
288
-            return 0;
289
-        }
290
-        if (self::debug) {
291
-            echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>';
292
-            echo '<br /> . original ticket->reserved: ' . $ticket->reserved();
293
-        }
294
-        $ticket->refresh_from_db();
295
-        // first let's determine the ticket availability based on sales
296
-        $available = $ticket->qty('saleable');
297
-        if (self::debug) {
298
-            echo '<br /> . . . ticket->qty: ' . $ticket->qty();
299
-            echo '<br /> . . . ticket->sold: ' . $ticket->sold();
300
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
301
-            echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
302
-            echo '<br /> . . . available: ' . $available;
303
-        }
304
-        if ($available < 1) {
305
-            $this->_ticket_sold_out($ticket);
306
-            return 0;
307
-        }
308
-        if (self::debug) {
309
-            echo '<br /> . . . qty: ' . $qty;
310
-        }
311
-        if ($available < $qty) {
312
-            $qty = $available;
313
-            if (self::debug) {
314
-                echo '<br /> . . . QTY ADJUSTED: ' . $qty;
315
-            }
316
-            $this->_ticket_quantity_decremented($ticket);
317
-        }
318
-        $this->_reserve_ticket($ticket, $qty);
319
-        return $qty;
320
-    }
321
-
322
-
323
-
324
-    /**
325
-     * increments ticket reserved based on quantity passed
326
-     *
327
-     * @param    EE_Ticket $ticket
328
-     * @param int          $quantity
329
-     * @return bool
330
-     * @throws EE_Error
331
-     */
332
-    protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
333
-    {
334
-        if (self::debug) {
335
-            echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity;
336
-        }
337
-        $ticket->increase_reserved($quantity, __LINE__ . ') TSM');
338
-        return $ticket->save();
339
-    }
340
-
341
-
342
-
343
-    /**
344
-     * @param  EE_Ticket $ticket
345
-     * @param  int       $quantity
346
-     * @return bool
347
-     * @throws EE_Error
348
-     */
349
-    protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
350
-    {
351
-        if (self::debug) {
352
-            echo '<br /> . . . ticket->ID: ' . $ticket->ID();
353
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
354
-        }
355
-        $ticket->decrease_reserved($quantity, true, __LINE__ . ') TSM');
356
-        if (self::debug) {
357
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
358
-        }
359
-        return $ticket->save() ? 1 : 0;
360
-    }
361
-
362
-
363
-
364
-    /**
365
-     * removes quantities within the ticket selector based on zero ticket availability
366
-     *
367
-     * @param    EE_Ticket $ticket
368
-     * @return    void
369
-     * @throws UnexpectedEntityException
370
-     * @throws EE_Error
371
-     */
372
-    protected function _ticket_sold_out(EE_Ticket $ticket)
373
-    {
374
-        if (self::debug) {
375
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
376
-            echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
377
-        }
378
-        $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
379
-    }
380
-
381
-
382
-
383
-    /**
384
-     * adjusts quantities within the ticket selector based on decreased ticket availability
385
-     *
386
-     * @param    EE_Ticket $ticket
387
-     * @return void
388
-     * @throws UnexpectedEntityException
389
-     * @throws EE_Error
390
-     */
391
-    protected function _ticket_quantity_decremented(EE_Ticket $ticket)
392
-    {
393
-        if (self::debug) {
394
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
395
-            echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
396
-        }
397
-        $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
398
-    }
399
-
400
-
401
-
402
-    /**
403
-     * builds string out of ticket and event name
404
-     *
405
-     * @param    EE_Ticket $ticket
406
-     * @return string
407
-     * @throws UnexpectedEntityException
408
-     * @throws EE_Error
409
-     */
410
-    protected function _get_ticket_and_event_name(EE_Ticket $ticket)
411
-    {
412
-        $event = $ticket->get_related_event();
413
-        if ($event instanceof EE_Event) {
414
-            $ticket_name = sprintf(
415
-                _x('%1$s for %2$s', 'ticket name for event name', 'event_espresso'),
416
-                $ticket->name(),
417
-                $event->name()
418
-            );
419
-        } else {
420
-            $ticket_name = $ticket->name();
421
-        }
422
-        return $ticket_name;
423
-    }
424
-
425
-
426
-
427
-    /********************************** EVENT CART  **********************************/
428
-
429
-
430
-
431
-    /**
432
-     * releases or reserves ticket(s) based on quantity passed
433
-     *
434
-     * @param  EE_Line_Item $line_item
435
-     * @param  int          $quantity
436
-     * @return void
437
-     * @throws EE_Error
438
-     * @throws InvalidArgumentException
439
-     * @throws InvalidDataTypeException
440
-     * @throws InvalidInterfaceException
441
-     */
442
-    public static function ticket_quantity_updated(EE_Line_Item $line_item, $quantity = 1)
443
-    {
444
-        $ticket = EEM_Ticket::instance()->get_one_by_ID(absint($line_item->OBJ_ID()));
445
-        if ($ticket instanceof EE_Ticket) {
446
-            $ticket->add_extra_meta(
447
-                EE_Ticket::META_KEY_TICKET_RESERVATIONS,
448
-                __LINE__ . ') ' . __METHOD__ . '()'
449
-            );
450
-            if ($quantity > 0) {
451
-                EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity);
452
-            } else {
453
-                EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
454
-            }
455
-        }
456
-    }
457
-
458
-
459
-
460
-    /**
461
-     * releases reserved ticket(s) based on quantity passed
462
-     *
463
-     * @param  EE_Ticket $ticket
464
-     * @param  int       $quantity
465
-     * @return void
466
-     * @throws EE_Error
467
-     */
468
-    public static function ticket_removed_from_cart(EE_Ticket $ticket, $quantity = 1)
469
-    {
470
-        $ticket->add_extra_meta(
471
-            EE_Ticket::META_KEY_TICKET_RESERVATIONS,
472
-            __LINE__ . ') ' . __METHOD__ . '()'
473
-        );
474
-        EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
475
-    }
476
-
477
-
478
-
479
-    /********************************** POST_NOTICES  **********************************/
480
-
481
-
482
-
483
-    /**
484
-     * @return void
485
-     * @throws EE_Error
486
-     * @throws InvalidArgumentException
487
-     * @throws ReflectionException
488
-     * @throws InvalidDataTypeException
489
-     * @throws InvalidInterfaceException
490
-     */
491
-    public static function post_notices()
492
-    {
493
-        EED_Ticket_Sales_Monitor::instance()->_post_notices();
494
-    }
495
-
496
-
497
-
498
-    /**
499
-     * @return void
500
-     * @throws EE_Error
501
-     * @throws InvalidArgumentException
502
-     * @throws ReflectionException
503
-     * @throws InvalidDataTypeException
504
-     * @throws InvalidInterfaceException
505
-     */
506
-    protected function _post_notices()
507
-    {
508
-        if (self::debug) {
509
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
510
-        }
511
-        $refresh_msg    = '';
512
-        $none_added_msg = '';
513
-        if (defined('DOING_AJAX') && DOING_AJAX) {
514
-            $refresh_msg    = __(
515
-                'Please refresh the page to view updated ticket quantities.',
516
-                'event_espresso'
517
-            );
518
-            $none_added_msg = __('No tickets were added for the event.', 'event_espresso');
519
-        }
520
-        if (! empty($this->sold_out_tickets)) {
521
-            EE_Error::add_attention(
522
-                sprintf(
523
-                    apply_filters(
524
-                        'FHEE__EED_Ticket_Sales_Monitor___post_notices__sold_out_tickets_notice',
525
-                        __(
526
-                            'We\'re sorry...%1$sThe following items have sold out since you first viewed this page, and can no longer be registered for:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s',
527
-                            'event_espresso'
528
-                        )
529
-                    ),
530
-                    '<br />',
531
-                    implode('<br />', $this->sold_out_tickets),
532
-                    $none_added_msg,
533
-                    $refresh_msg
534
-                )
535
-            );
536
-            // alter code flow in the Ticket Selector for better UX
537
-            add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', '__return_true');
538
-            add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__success', '__return_false');
539
-            $this->sold_out_tickets = array();
540
-            // and reset the cart
541
-            EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN);
542
-        }
543
-        if (! empty($this->decremented_tickets)) {
544
-            EE_Error::add_attention(
545
-                sprintf(
546
-                    apply_filters(
547
-                        'FHEE__EED_Ticket_Sales_Monitor___ticket_quantity_decremented__notice',
548
-                        __(
549
-                            'We\'re sorry...%1$sDue to sales that have occurred since you first viewed the last page, the following items have had their quantities adjusted to match the current available amount:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s',
550
-                            'event_espresso'
551
-                        )
552
-                    ),
553
-                    '<br />',
554
-                    implode('<br />', $this->decremented_tickets),
555
-                    $none_added_msg,
556
-                    $refresh_msg
557
-                )
558
-            );
559
-            $this->decremented_tickets = array();
560
-        }
561
-    }
562
-
563
-
564
-
565
-    /********************************** RELEASE_ALL_RESERVED_TICKETS_FOR_TRANSACTION  **********************************/
566
-
567
-
568
-
569
-    /**
570
-     * releases reserved tickets for all registrations of an EE_Transaction
571
-     * by default, will NOT release tickets for finalized transactions
572
-     *
573
-     * @param    EE_Transaction $transaction
574
-     * @return int
575
-     * @throws EE_Error
576
-     * @throws InvalidSessionDataException
577
-     */
578
-    protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
579
-    {
580
-        if (self::debug) {
581
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
582
-            echo '<br /> . transaction->ID: ' . $transaction->ID();
583
-        }
584
-        // check if 'finalize_registration' step has been completed...
585
-        $finalized = $transaction->reg_step_completed('finalize_registration');
586
-        if (self::debug) {
587
-            // DEBUG LOG
588
-            EEH_Debug_Tools::log(
589
-                __CLASS__,
590
-                __FUNCTION__,
591
-                __LINE__,
592
-                array('finalized' => $finalized),
593
-                false,
594
-                'EE_Transaction: ' . $transaction->ID()
595
-            );
596
-        }
597
-        // how many tickets were released
598
-        $count = 0;
599
-        if (self::debug) {
600
-            echo '<br /> . . . finalized: ' . $finalized;
601
-        }
602
-        $release_tickets_with_TXN_status = array(
603
-            EEM_Transaction::failed_status_code,
604
-            EEM_Transaction::abandoned_status_code,
605
-            EEM_Transaction::incomplete_status_code,
606
-        );
607
-        // if the session is getting cleared BEFORE the TXN has been finalized
608
-        if (! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
609
-            // let's cancel any reserved tickets
610
-            $registrations = $transaction->registrations();
611
-            if (! empty($registrations)) {
612
-                foreach ($registrations as $registration) {
613
-                    if ($registration instanceof EE_Registration) {
614
-                        $count += $this->_release_reserved_ticket_for_registration($registration, $transaction);
615
-                    }
616
-                }
617
-            }
618
-        }
619
-        return $count;
620
-    }
621
-
622
-
623
-
624
-    /**
625
-     * releases reserved tickets for an EE_Registration
626
-     * by default, will NOT release tickets for APPROVED registrations
627
-     *
628
-     * @param EE_Registration $registration
629
-     * @param EE_Transaction  $transaction
630
-     * @return int
631
-     * @throws EE_Error
632
-     */
633
-    protected function _release_reserved_ticket_for_registration(
634
-        EE_Registration $registration,
635
-        EE_Transaction $transaction
636
-    ) {
637
-        $STS_ID = $transaction->status_ID();
638
-        if (self::debug) {
639
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
640
-            echo '<br /> . . registration->ID: ' . $registration->ID();
641
-            echo '<br /> . . registration->status_ID: ' . $registration->status_ID();
642
-            echo '<br /> . . transaction->status_ID(): ' . $STS_ID;
643
-        }
644
-        if (
645
-            // release Tickets for Failed Transactions and Abandoned Transactions
646
-            $STS_ID === EEM_Transaction::failed_status_code
647
-            || $STS_ID === EEM_Transaction::abandoned_status_code
648
-            || (
649
-                // also release Tickets for Incomplete Transactions, but ONLY if the Registrations are NOT Approved
650
-                $STS_ID === EEM_Transaction::incomplete_status_code
651
-                && $registration->status_ID() !== EEM_Registration::status_id_approved
652
-            )
653
-        ) {
654
-            $ticket = $registration->ticket();
655
-            if ($ticket instanceof EE_Ticket) {
656
-                $ticket->add_extra_meta(
657
-                    EE_Ticket::META_KEY_TICKET_RESERVATIONS,
658
-                    __LINE__ . ') ' . __METHOD__ . '()'
659
-                );
660
-            }
661
-            $registration->release_reserved_ticket();
662
-            return 1;
663
-        }
664
-        return 0;
665
-    }
666
-
667
-
668
-
669
-    /********************************** SESSION_CART_RESET  **********************************/
670
-
671
-
672
-
673
-    /**
674
-     * callback hooked into 'AHEE__EE_Session__reset_cart__before_reset'
675
-     *
676
-     * @param EE_Session $session
677
-     * @return void
678
-     * @throws EE_Error
679
-     * @throws InvalidArgumentException
680
-     * @throws ReflectionException
681
-     * @throws InvalidDataTypeException
682
-     * @throws InvalidInterfaceException
683
-     */
684
-    public static function session_cart_reset(EE_Session $session)
685
-    {
686
-        if (self::debug) {
687
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
688
-        }
689
-        // first check of the session has a valid Checkout object
690
-        $checkout = $session->checkout();
691
-        if ($checkout instanceof EE_Checkout) {
692
-            // and use that to clear ticket reservations because it will update the associated registration meta data
693
-            EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
694
-            return;
695
-        }
696
-        $cart = $session->cart();
697
-        if ($cart instanceof EE_Cart) {
698
-            if (self::debug) {
699
-                echo '<br /><br /> cart instance of EE_Cart: ';
700
-            }
701
-            EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart, $session);
702
-        } else {
703
-            if (self::debug) {
704
-                echo '<br /><br /> invalid EE_Cart: ';
705
-                var_export($cart, true);
706
-            }
707
-        }
708
-    }
709
-
710
-
711
-
712
-    /**
713
-     * releases reserved tickets in the EE_Cart
714
-     *
715
-     * @param EE_Cart $cart
716
-     * @return void
717
-     * @throws EE_Error
718
-     * @throws InvalidArgumentException
719
-     * @throws ReflectionException
720
-     * @throws InvalidDataTypeException
721
-     * @throws InvalidInterfaceException
722
-     */
723
-    protected function _session_cart_reset(EE_Cart $cart, EE_Session $session)
724
-    {
725
-        if (self::debug) {
726
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
727
-        }
728
-        EE_Registry::instance()->load_helper('Line_Item');
729
-        $ticket_line_items = $cart->get_tickets();
730
-        if (empty($ticket_line_items)) {
731
-            return;
732
-        }
733
-        foreach ($ticket_line_items as $ticket_line_item) {
734
-            if (self::debug) {
735
-                echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID();
736
-            }
737
-            if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
738
-                if (self::debug) {
739
-                    echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
740
-                }
741
-                $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
742
-                if ($ticket instanceof EE_Ticket) {
743
-                    if (self::debug) {
744
-                        echo '<br /> . . ticket->ID(): ' . $ticket->ID();
745
-                        echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
746
-                    }
747
-                    $ticket->add_extra_meta(
748
-                        EE_Ticket::META_KEY_TICKET_RESERVATIONS,
749
-                        __LINE__ . ') ' . __METHOD__ . '() SID = ' . $session->id()
750
-                    );
751
-                    $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
752
-                }
753
-            }
754
-        }
755
-        if (self::debug) {
756
-            echo '<br /><br /> RESET COMPLETED ';
757
-        }
758
-    }
759
-
760
-
761
-
762
-    /********************************** SESSION_CHECKOUT_RESET  **********************************/
763
-
764
-
765
-
766
-    /**
767
-     * callback hooked into 'AHEE__EE_Session__reset_checkout__before_reset'
768
-     *
769
-     * @param EE_Session $session
770
-     * @return void
771
-     * @throws EE_Error
772
-     * @throws InvalidSessionDataException
773
-     */
774
-    public static function session_checkout_reset(EE_Session $session)
775
-    {
776
-        $checkout = $session->checkout();
777
-        if ($checkout instanceof EE_Checkout) {
778
-            EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
779
-        }
780
-    }
781
-
782
-
783
-
784
-    /**
785
-     * releases reserved tickets for the EE_Checkout->transaction
786
-     *
787
-     * @param EE_Checkout $checkout
788
-     * @return void
789
-     * @throws EE_Error
790
-     * @throws InvalidSessionDataException
791
-     */
792
-    protected function _session_checkout_reset(EE_Checkout $checkout)
793
-    {
794
-        if (self::debug) {
795
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
796
-        }
797
-        // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
798
-        if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
799
-            return;
800
-        }
801
-        $this->_release_all_reserved_tickets_for_transaction($checkout->transaction);
802
-    }
803
-
804
-
805
-
806
-    /********************************** SESSION_EXPIRED_RESET  **********************************/
807
-
808
-
809
-
810
-    /**
811
-     * @param    EE_Session $session
812
-     * @return    void
813
-     */
814
-    public static function session_expired_reset(EE_Session $session)
815
-    {
816
-    }
817
-
818
-
819
-
820
-    /********************************** PROCESS_ABANDONED_TRANSACTIONS  **********************************/
821
-
822
-
823
-
824
-    /**
825
-     * releases reserved tickets for all registrations of an ABANDONED EE_Transaction
826
-     * by default, will NOT release tickets for free transactions, or any that have received a payment
827
-     *
828
-     * @param EE_Transaction $transaction
829
-     * @return void
830
-     * @throws EE_Error
831
-     * @throws InvalidSessionDataException
832
-     */
833
-    public static function process_abandoned_transactions(EE_Transaction $transaction)
834
-    {
835
-        // is this TXN free or has any money been paid towards this TXN? If so, then leave it alone
836
-        if ($transaction->is_free() || $transaction->paid() > 0) {
837
-            if (self::debug) {
838
-                // DEBUG LOG
839
-                EEH_Debug_Tools::log(
840
-                    __CLASS__,
841
-                    __FUNCTION__,
842
-                    __LINE__,
843
-                    array($transaction),
844
-                    false,
845
-                    'EE_Transaction: ' . $transaction->ID()
846
-                );
847
-            }
848
-            return;
849
-        }
850
-        // have their been any successful payments made ?
851
-        $payments = $transaction->payments();
852
-        foreach ($payments as $payment) {
853
-            if ($payment instanceof EE_Payment && $payment->status() === EEM_Payment::status_id_approved) {
854
-                if (self::debug) {
855
-                    // DEBUG LOG
856
-                    EEH_Debug_Tools::log(
857
-                        __CLASS__,
858
-                        __FUNCTION__,
859
-                        __LINE__,
860
-                        array($payment),
861
-                        false,
862
-                        'EE_Transaction: ' . $transaction->ID()
863
-                    );
864
-                }
865
-                return;
866
-            }
867
-        }
868
-        // since you haven't even attempted to pay for your ticket...
869
-        EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
870
-    }
871
-
872
-
873
-
874
-    /********************************** PROCESS_FAILED_TRANSACTIONS  **********************************/
875
-
876
-
877
-
878
-    /**
879
-     * releases reserved tickets for absolutely ALL registrations of a FAILED EE_Transaction
880
-     *
881
-     * @param EE_Transaction $transaction
882
-     * @return void
883
-     * @throws EE_Error
884
-     * @throws InvalidSessionDataException
885
-     */
886
-    public static function process_failed_transactions(EE_Transaction $transaction)
887
-    {
888
-        // since you haven't even attempted to pay for your ticket...
889
-        EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
890
-    }
891
-
892
-
893
-
894
-    /********************************** RESET RESERVATION COUNTS  *********************************/
895
-
896
-
897
-
898
-    /**
899
-     * Resets all ticket and datetime reserved counts to zero
900
-     * Tickets that are currently associated with a Transaction that is in progress
901
-     *
902
-     * @throws EE_Error
903
-     * @throws DomainException
904
-     * @throws InvalidDataTypeException
905
-     * @throws InvalidInterfaceException
906
-     * @throws InvalidArgumentException
907
-     * @throws UnexpectedEntityException
908
-     */
909
-    public static function reset_reservation_counts()
910
-    {
911
-        /** @var EE_Line_Item[] $valid_reserved_tickets */
912
-        $valid_reserved_tickets = array();
913
-        /** @var EE_Transaction[] $transactions_not_in_progress */
914
-        $transactions_not_in_progress = EEM_Transaction::instance()->get_transactions_not_in_progress();
915
-        foreach ($transactions_not_in_progress as $transaction) {
916
-            // if this TXN has been fully completed, then skip it
917
-            if ($transaction->reg_step_completed('finalize_registration')) {
918
-                continue;
919
-            }
920
-            $total_line_item = $transaction->total_line_item();
921
-            // $transaction_in_progress->line
922
-            if (! $total_line_item instanceof EE_Line_Item) {
923
-                throw new DomainException(
924
-                    esc_html__(
925
-                        'Transaction does not have a valid Total Line Item associated with it.',
926
-                        'event_espresso'
927
-                    )
928
-                );
929
-            }
930
-            $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
931
-                $total_line_item
932
-            );
933
-        }
934
-        $total_line_items = EEM_Line_Item::instance()->get_total_line_items_for_active_carts();
935
-        foreach ($total_line_items as $total_line_item) {
936
-            $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
937
-                $total_line_item
938
-            );
939
-        }
940
-        $tickets_with_reservations = EEM_Ticket::instance()->get_tickets_with_reservations();
941
-        return EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
942
-            $tickets_with_reservations,
943
-            $valid_reserved_tickets,
944
-            __FUNCTION__
945
-        );
946
-    }
947
-
948
-
949
-
950
-    /**
951
-     * @param EE_Line_Item $total_line_item
952
-     * @return EE_Line_Item[]
953
-     */
954
-    private static function get_ticket_line_items_for_grand_total(EE_Line_Item $total_line_item)
955
-    {
956
-        /** @var EE_Line_Item[] $valid_reserved_tickets */
957
-        $valid_reserved_tickets = array();
958
-        $ticket_line_items      = EEH_Line_Item::get_ticket_line_items($total_line_item);
959
-        foreach ($ticket_line_items as $ticket_line_item) {
960
-            if ($ticket_line_item instanceof EE_Line_Item) {
961
-                $valid_reserved_tickets[] = $ticket_line_item;
962
-            }
963
-        }
964
-        return $valid_reserved_tickets;
965
-    }
966
-
967
-
968
-
969
-    /**
970
-     * @param EE_Ticket[]    $tickets_with_reservations
971
-     * @param EE_Line_Item[] $valid_reserved_ticket_line_items
972
-     * @return int
973
-     * @throws UnexpectedEntityException
974
-     * @throws DomainException
975
-     * @throws EE_Error
976
-     */
977
-    private static function release_reservations_for_tickets(
978
-        array $tickets_with_reservations,
979
-        array $valid_reserved_ticket_line_items = array(),
980
-        $source
981
-    ) {
982
-        $total_tickets_released = 0;
983
-        $sold_out_events = array();
984
-        foreach ($tickets_with_reservations as $ticket_with_reservations) {
985
-            if (! $ticket_with_reservations instanceof EE_Ticket) {
986
-                continue;
987
-            }
988
-            $reserved_qty = $ticket_with_reservations->reserved();
989
-            foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) {
990
-                if (
991
-                    $valid_reserved_ticket_line_item instanceof EE_Line_Item
992
-                    && $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID()
993
-                ) {
994
-                    $reserved_qty -= $valid_reserved_ticket_line_item->quantity();
995
-                }
996
-            }
997
-            if ($reserved_qty > 0) {
998
-                $ticket_with_reservations->add_extra_meta(
999
-                    EE_Ticket::META_KEY_TICKET_RESERVATIONS,
1000
-                    __LINE__ . ') ' . $source . '()'
1001
-                );
1002
-                $ticket_with_reservations->decrease_reserved($reserved_qty, true, __LINE__ . ') TSM');
1003
-                $ticket_with_reservations->save();
1004
-                $total_tickets_released += $reserved_qty;
1005
-                $event = $ticket_with_reservations->get_related_event();
1006
-                // track sold out events
1007
-                if ($event instanceof EE_Event && $event->is_sold_out()) {
1008
-                    $sold_out_events[] = $event;
1009
-                }
1010
-            }
1011
-        }
1012
-        // double check whether sold out events should remain sold out after releasing tickets
1013
-        if($sold_out_events !== array()){
1014
-            foreach ($sold_out_events as $sold_out_event) {
1015
-                /** @var EE_Event $sold_out_event */
1016
-                $sold_out_event->perform_sold_out_status_check();
1017
-            }
1018
-        }
1019
-        return $total_tickets_released;
1020
-    }
1021
-
1022
-
1023
-
1024
-    /********************************** SHUTDOWN  **********************************/
1025
-
1026
-
1027
-
1028
-    /**
1029
-     * @return false|int
1030
-     * @throws EE_Error
1031
-     * @throws InvalidArgumentException
1032
-     * @throws InvalidDataTypeException
1033
-     * @throws InvalidInterfaceException
1034
-     */
1035
-    public static function clear_expired_line_items_with_no_transaction()
1036
-    {
1037
-        /** @type WPDB $wpdb */
1038
-        global $wpdb;
1039
-        return $wpdb->query(
1040
-            $wpdb->prepare(
1041
-                'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
26
+	const debug = false;    //	true false
27
+
28
+	/**
29
+	 * an array of raw ticket data from EED_Ticket_Selector
30
+	 *
31
+	 * @var array $ticket_selections
32
+	 */
33
+	protected $ticket_selections = array();
34
+
35
+	/**
36
+	 * the raw ticket data from EED_Ticket_Selector is organized in rows
37
+	 * according to how they are displayed in the actual Ticket_Selector
38
+	 * this tracks the current row being processed
39
+	 *
40
+	 * @var int $current_row
41
+	 */
42
+	protected $current_row = 0;
43
+
44
+	/**
45
+	 * an array for tracking names of tickets that have sold out
46
+	 *
47
+	 * @var array $sold_out_tickets
48
+	 */
49
+	protected $sold_out_tickets = array();
50
+
51
+	/**
52
+	 * an array for tracking names of tickets that have had their quantities reduced
53
+	 *
54
+	 * @var array $decremented_tickets
55
+	 */
56
+	protected $decremented_tickets = array();
57
+
58
+
59
+
60
+	/**
61
+	 * set_hooks - for hooking into EE Core, other modules, etc
62
+	 *
63
+	 * @return    void
64
+	 */
65
+	public static function set_hooks()
66
+	{
67
+		// release tickets for expired carts
68
+		add_action(
69
+			'EED_Ticket_Selector__process_ticket_selections__before',
70
+			array('EED_Ticket_Sales_Monitor', 'release_tickets_for_expired_carts'),
71
+			1
72
+		);
73
+		// check ticket reserves AFTER MER does it's check (hence priority 20)
74
+		add_filter(
75
+			'FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty',
76
+			array('EED_Ticket_Sales_Monitor', 'validate_ticket_sale'),
77
+			20,
78
+			3
79
+		);
80
+		// add notices for sold out tickets
81
+		add_action(
82
+			'AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart',
83
+			array('EED_Ticket_Sales_Monitor', 'post_notices'),
84
+			10
85
+		);
86
+		// handle ticket quantities adjusted in cart
87
+		//add_action(
88
+		//	'FHEE__EED_Multi_Event_Registration__adjust_line_item_quantity__line_item_quantity_updated',
89
+		//	array( 'EED_Ticket_Sales_Monitor', 'ticket_quantity_updated' ),
90
+		//	10, 2
91
+		//);
92
+		// handle tickets deleted from cart
93
+		add_action(
94
+			'FHEE__EED_Multi_Event_Registration__delete_ticket__ticket_removed_from_cart',
95
+			array('EED_Ticket_Sales_Monitor', 'ticket_removed_from_cart'),
96
+			10,
97
+			2
98
+		);
99
+		// handle emptied carts
100
+		add_action(
101
+			'AHEE__EE_Session__reset_cart__before_reset',
102
+			array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
103
+			10,
104
+			1
105
+		);
106
+		add_action(
107
+			'AHEE__EED_Multi_Event_Registration__empty_event_cart__before_delete_cart',
108
+			array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
109
+			10,
110
+			1
111
+		);
112
+		// handle cancelled registrations
113
+		add_action(
114
+			'AHEE__EE_Session__reset_checkout__before_reset',
115
+			array('EED_Ticket_Sales_Monitor', 'session_checkout_reset'),
116
+			10,
117
+			1
118
+		);
119
+		// cron tasks
120
+		add_action(
121
+			'AHEE__EE_Cron_Tasks__process_expired_transactions__abandoned_transaction',
122
+			array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
123
+			10,
124
+			1
125
+		);
126
+		add_action(
127
+			'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction',
128
+			array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
129
+			10,
130
+			1
131
+		);
132
+		add_action(
133
+			'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction',
134
+			array('EED_Ticket_Sales_Monitor', 'process_failed_transactions'),
135
+			10,
136
+			1
137
+		);
138
+	}
139
+
140
+
141
+
142
+	/**
143
+	 * set_hooks_admin - for hooking into EE Admin Core, other modules, etc
144
+	 *
145
+	 * @return void
146
+	 */
147
+	public static function set_hooks_admin()
148
+	{
149
+		EED_Ticket_Sales_Monitor::set_hooks();
150
+	}
151
+
152
+
153
+
154
+	/**
155
+	 * @return EED_Ticket_Sales_Monitor|EED_Module
156
+	 */
157
+	public static function instance()
158
+	{
159
+		return parent::get_instance(__CLASS__);
160
+	}
161
+
162
+
163
+
164
+	/**
165
+	 * @param WP_Query $WP_Query
166
+	 * @return    void
167
+	 */
168
+	public function run($WP_Query)
169
+	{
170
+	}
171
+
172
+
173
+
174
+	/********************************** PRE_TICKET_SALES  **********************************/
175
+
176
+
177
+
178
+	/**
179
+	 * Retrieves grand totals from the line items that have no TXN ID
180
+	 * and timestamps less than the current time minus the session lifespan.
181
+	 * These are carts that have been abandoned before the "registrant" even attempted to checkout.
182
+	 * We're going to release the tickets for these line items before attempting to add more to the cart.
183
+	 *
184
+	 * @return void
185
+	 * @throws DomainException
186
+	 * @throws EE_Error
187
+	 * @throws InvalidArgumentException
188
+	 * @throws InvalidDataTypeException
189
+	 * @throws InvalidInterfaceException
190
+	 * @throws UnexpectedEntityException
191
+	 */
192
+	public static function release_tickets_for_expired_carts()
193
+	{
194
+		do_action('AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__begin');
195
+		$expired_ticket_IDs      = array();
196
+		$valid_ticket_line_items = array();
197
+		$total_line_items        = EEM_Line_Item::instance()->get_total_line_items_with_no_transaction();
198
+		if (empty($total_line_items)) {
199
+			do_action(
200
+				'AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__end',
201
+				$total_line_items,
202
+				$valid_ticket_line_items,
203
+				$expired_ticket_IDs
204
+			);
205
+			return;
206
+		}
207
+		$expired = current_time('timestamp') - EE_Registry::instance()->SSN->lifespan();
208
+		foreach ($total_line_items as $total_line_item) {
209
+			/** @var EE_Line_Item $total_line_item */
210
+			$ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item);
211
+			foreach ($ticket_line_items as $ticket_line_item) {
212
+				if (! $ticket_line_item instanceof EE_Line_Item) {
213
+					continue;
214
+				}
215
+				if ($total_line_item->timestamp(true) <= $expired) {
216
+					$expired_ticket_IDs[ $ticket_line_item->OBJ_ID() ] = $ticket_line_item->OBJ_ID();
217
+				} else {
218
+					$valid_ticket_line_items[ $ticket_line_item->OBJ_ID() ] = $ticket_line_item;
219
+				}
220
+			}
221
+		}
222
+		if (! empty($expired_ticket_IDs)) {
223
+			EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
224
+				\EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
225
+				$valid_ticket_line_items,
226
+				__FUNCTION__
227
+			);
228
+			// let's get rid of expired line items so that they can't interfere with tracking
229
+			add_action(
230
+				'shutdown',
231
+				array('EED_Ticket_Sales_Monitor', 'clear_expired_line_items_with_no_transaction'),
232
+				999
233
+			);
234
+		}
235
+		do_action(
236
+			'AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__end',
237
+			$total_line_items,
238
+			$valid_ticket_line_items,
239
+			$expired_ticket_IDs
240
+		);
241
+	}
242
+
243
+
244
+
245
+	/********************************** VALIDATE_TICKET_SALE  **********************************/
246
+
247
+
248
+
249
+	/**
250
+	 * callback for 'FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data'
251
+	 *
252
+	 * @param int       $qty
253
+	 * @param EE_Ticket $ticket
254
+	 * @return bool
255
+	 * @throws UnexpectedEntityException
256
+	 * @throws EE_Error
257
+	 */
258
+	public static function validate_ticket_sale($qty = 1, EE_Ticket $ticket)
259
+	{
260
+		$qty = absint($qty);
261
+		if ($qty > 0) {
262
+			$qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
263
+		}
264
+		if (self::debug) {
265
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()';
266
+			echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>';
267
+		}
268
+		return $qty;
269
+	}
270
+
271
+
272
+
273
+	/**
274
+	 * checks whether an individual ticket is available for purchase based on datetime, and ticket details
275
+	 *
276
+	 * @param   EE_Ticket $ticket
277
+	 * @param int         $qty
278
+	 * @return int
279
+	 * @throws UnexpectedEntityException
280
+	 * @throws EE_Error
281
+	 */
282
+	protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
283
+	{
284
+		if (self::debug) {
285
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
286
+		}
287
+		if (! $ticket instanceof EE_Ticket) {
288
+			return 0;
289
+		}
290
+		if (self::debug) {
291
+			echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>';
292
+			echo '<br /> . original ticket->reserved: ' . $ticket->reserved();
293
+		}
294
+		$ticket->refresh_from_db();
295
+		// first let's determine the ticket availability based on sales
296
+		$available = $ticket->qty('saleable');
297
+		if (self::debug) {
298
+			echo '<br /> . . . ticket->qty: ' . $ticket->qty();
299
+			echo '<br /> . . . ticket->sold: ' . $ticket->sold();
300
+			echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
301
+			echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
302
+			echo '<br /> . . . available: ' . $available;
303
+		}
304
+		if ($available < 1) {
305
+			$this->_ticket_sold_out($ticket);
306
+			return 0;
307
+		}
308
+		if (self::debug) {
309
+			echo '<br /> . . . qty: ' . $qty;
310
+		}
311
+		if ($available < $qty) {
312
+			$qty = $available;
313
+			if (self::debug) {
314
+				echo '<br /> . . . QTY ADJUSTED: ' . $qty;
315
+			}
316
+			$this->_ticket_quantity_decremented($ticket);
317
+		}
318
+		$this->_reserve_ticket($ticket, $qty);
319
+		return $qty;
320
+	}
321
+
322
+
323
+
324
+	/**
325
+	 * increments ticket reserved based on quantity passed
326
+	 *
327
+	 * @param    EE_Ticket $ticket
328
+	 * @param int          $quantity
329
+	 * @return bool
330
+	 * @throws EE_Error
331
+	 */
332
+	protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
333
+	{
334
+		if (self::debug) {
335
+			echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity;
336
+		}
337
+		$ticket->increase_reserved($quantity, __LINE__ . ') TSM');
338
+		return $ticket->save();
339
+	}
340
+
341
+
342
+
343
+	/**
344
+	 * @param  EE_Ticket $ticket
345
+	 * @param  int       $quantity
346
+	 * @return bool
347
+	 * @throws EE_Error
348
+	 */
349
+	protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
350
+	{
351
+		if (self::debug) {
352
+			echo '<br /> . . . ticket->ID: ' . $ticket->ID();
353
+			echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
354
+		}
355
+		$ticket->decrease_reserved($quantity, true, __LINE__ . ') TSM');
356
+		if (self::debug) {
357
+			echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
358
+		}
359
+		return $ticket->save() ? 1 : 0;
360
+	}
361
+
362
+
363
+
364
+	/**
365
+	 * removes quantities within the ticket selector based on zero ticket availability
366
+	 *
367
+	 * @param    EE_Ticket $ticket
368
+	 * @return    void
369
+	 * @throws UnexpectedEntityException
370
+	 * @throws EE_Error
371
+	 */
372
+	protected function _ticket_sold_out(EE_Ticket $ticket)
373
+	{
374
+		if (self::debug) {
375
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
376
+			echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
377
+		}
378
+		$this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
379
+	}
380
+
381
+
382
+
383
+	/**
384
+	 * adjusts quantities within the ticket selector based on decreased ticket availability
385
+	 *
386
+	 * @param    EE_Ticket $ticket
387
+	 * @return void
388
+	 * @throws UnexpectedEntityException
389
+	 * @throws EE_Error
390
+	 */
391
+	protected function _ticket_quantity_decremented(EE_Ticket $ticket)
392
+	{
393
+		if (self::debug) {
394
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
395
+			echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
396
+		}
397
+		$this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
398
+	}
399
+
400
+
401
+
402
+	/**
403
+	 * builds string out of ticket and event name
404
+	 *
405
+	 * @param    EE_Ticket $ticket
406
+	 * @return string
407
+	 * @throws UnexpectedEntityException
408
+	 * @throws EE_Error
409
+	 */
410
+	protected function _get_ticket_and_event_name(EE_Ticket $ticket)
411
+	{
412
+		$event = $ticket->get_related_event();
413
+		if ($event instanceof EE_Event) {
414
+			$ticket_name = sprintf(
415
+				_x('%1$s for %2$s', 'ticket name for event name', 'event_espresso'),
416
+				$ticket->name(),
417
+				$event->name()
418
+			);
419
+		} else {
420
+			$ticket_name = $ticket->name();
421
+		}
422
+		return $ticket_name;
423
+	}
424
+
425
+
426
+
427
+	/********************************** EVENT CART  **********************************/
428
+
429
+
430
+
431
+	/**
432
+	 * releases or reserves ticket(s) based on quantity passed
433
+	 *
434
+	 * @param  EE_Line_Item $line_item
435
+	 * @param  int          $quantity
436
+	 * @return void
437
+	 * @throws EE_Error
438
+	 * @throws InvalidArgumentException
439
+	 * @throws InvalidDataTypeException
440
+	 * @throws InvalidInterfaceException
441
+	 */
442
+	public static function ticket_quantity_updated(EE_Line_Item $line_item, $quantity = 1)
443
+	{
444
+		$ticket = EEM_Ticket::instance()->get_one_by_ID(absint($line_item->OBJ_ID()));
445
+		if ($ticket instanceof EE_Ticket) {
446
+			$ticket->add_extra_meta(
447
+				EE_Ticket::META_KEY_TICKET_RESERVATIONS,
448
+				__LINE__ . ') ' . __METHOD__ . '()'
449
+			);
450
+			if ($quantity > 0) {
451
+				EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity);
452
+			} else {
453
+				EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
454
+			}
455
+		}
456
+	}
457
+
458
+
459
+
460
+	/**
461
+	 * releases reserved ticket(s) based on quantity passed
462
+	 *
463
+	 * @param  EE_Ticket $ticket
464
+	 * @param  int       $quantity
465
+	 * @return void
466
+	 * @throws EE_Error
467
+	 */
468
+	public static function ticket_removed_from_cart(EE_Ticket $ticket, $quantity = 1)
469
+	{
470
+		$ticket->add_extra_meta(
471
+			EE_Ticket::META_KEY_TICKET_RESERVATIONS,
472
+			__LINE__ . ') ' . __METHOD__ . '()'
473
+		);
474
+		EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
475
+	}
476
+
477
+
478
+
479
+	/********************************** POST_NOTICES  **********************************/
480
+
481
+
482
+
483
+	/**
484
+	 * @return void
485
+	 * @throws EE_Error
486
+	 * @throws InvalidArgumentException
487
+	 * @throws ReflectionException
488
+	 * @throws InvalidDataTypeException
489
+	 * @throws InvalidInterfaceException
490
+	 */
491
+	public static function post_notices()
492
+	{
493
+		EED_Ticket_Sales_Monitor::instance()->_post_notices();
494
+	}
495
+
496
+
497
+
498
+	/**
499
+	 * @return void
500
+	 * @throws EE_Error
501
+	 * @throws InvalidArgumentException
502
+	 * @throws ReflectionException
503
+	 * @throws InvalidDataTypeException
504
+	 * @throws InvalidInterfaceException
505
+	 */
506
+	protected function _post_notices()
507
+	{
508
+		if (self::debug) {
509
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
510
+		}
511
+		$refresh_msg    = '';
512
+		$none_added_msg = '';
513
+		if (defined('DOING_AJAX') && DOING_AJAX) {
514
+			$refresh_msg    = __(
515
+				'Please refresh the page to view updated ticket quantities.',
516
+				'event_espresso'
517
+			);
518
+			$none_added_msg = __('No tickets were added for the event.', 'event_espresso');
519
+		}
520
+		if (! empty($this->sold_out_tickets)) {
521
+			EE_Error::add_attention(
522
+				sprintf(
523
+					apply_filters(
524
+						'FHEE__EED_Ticket_Sales_Monitor___post_notices__sold_out_tickets_notice',
525
+						__(
526
+							'We\'re sorry...%1$sThe following items have sold out since you first viewed this page, and can no longer be registered for:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s',
527
+							'event_espresso'
528
+						)
529
+					),
530
+					'<br />',
531
+					implode('<br />', $this->sold_out_tickets),
532
+					$none_added_msg,
533
+					$refresh_msg
534
+				)
535
+			);
536
+			// alter code flow in the Ticket Selector for better UX
537
+			add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', '__return_true');
538
+			add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__success', '__return_false');
539
+			$this->sold_out_tickets = array();
540
+			// and reset the cart
541
+			EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN);
542
+		}
543
+		if (! empty($this->decremented_tickets)) {
544
+			EE_Error::add_attention(
545
+				sprintf(
546
+					apply_filters(
547
+						'FHEE__EED_Ticket_Sales_Monitor___ticket_quantity_decremented__notice',
548
+						__(
549
+							'We\'re sorry...%1$sDue to sales that have occurred since you first viewed the last page, the following items have had their quantities adjusted to match the current available amount:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s',
550
+							'event_espresso'
551
+						)
552
+					),
553
+					'<br />',
554
+					implode('<br />', $this->decremented_tickets),
555
+					$none_added_msg,
556
+					$refresh_msg
557
+				)
558
+			);
559
+			$this->decremented_tickets = array();
560
+		}
561
+	}
562
+
563
+
564
+
565
+	/********************************** RELEASE_ALL_RESERVED_TICKETS_FOR_TRANSACTION  **********************************/
566
+
567
+
568
+
569
+	/**
570
+	 * releases reserved tickets for all registrations of an EE_Transaction
571
+	 * by default, will NOT release tickets for finalized transactions
572
+	 *
573
+	 * @param    EE_Transaction $transaction
574
+	 * @return int
575
+	 * @throws EE_Error
576
+	 * @throws InvalidSessionDataException
577
+	 */
578
+	protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
579
+	{
580
+		if (self::debug) {
581
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
582
+			echo '<br /> . transaction->ID: ' . $transaction->ID();
583
+		}
584
+		// check if 'finalize_registration' step has been completed...
585
+		$finalized = $transaction->reg_step_completed('finalize_registration');
586
+		if (self::debug) {
587
+			// DEBUG LOG
588
+			EEH_Debug_Tools::log(
589
+				__CLASS__,
590
+				__FUNCTION__,
591
+				__LINE__,
592
+				array('finalized' => $finalized),
593
+				false,
594
+				'EE_Transaction: ' . $transaction->ID()
595
+			);
596
+		}
597
+		// how many tickets were released
598
+		$count = 0;
599
+		if (self::debug) {
600
+			echo '<br /> . . . finalized: ' . $finalized;
601
+		}
602
+		$release_tickets_with_TXN_status = array(
603
+			EEM_Transaction::failed_status_code,
604
+			EEM_Transaction::abandoned_status_code,
605
+			EEM_Transaction::incomplete_status_code,
606
+		);
607
+		// if the session is getting cleared BEFORE the TXN has been finalized
608
+		if (! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
609
+			// let's cancel any reserved tickets
610
+			$registrations = $transaction->registrations();
611
+			if (! empty($registrations)) {
612
+				foreach ($registrations as $registration) {
613
+					if ($registration instanceof EE_Registration) {
614
+						$count += $this->_release_reserved_ticket_for_registration($registration, $transaction);
615
+					}
616
+				}
617
+			}
618
+		}
619
+		return $count;
620
+	}
621
+
622
+
623
+
624
+	/**
625
+	 * releases reserved tickets for an EE_Registration
626
+	 * by default, will NOT release tickets for APPROVED registrations
627
+	 *
628
+	 * @param EE_Registration $registration
629
+	 * @param EE_Transaction  $transaction
630
+	 * @return int
631
+	 * @throws EE_Error
632
+	 */
633
+	protected function _release_reserved_ticket_for_registration(
634
+		EE_Registration $registration,
635
+		EE_Transaction $transaction
636
+	) {
637
+		$STS_ID = $transaction->status_ID();
638
+		if (self::debug) {
639
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
640
+			echo '<br /> . . registration->ID: ' . $registration->ID();
641
+			echo '<br /> . . registration->status_ID: ' . $registration->status_ID();
642
+			echo '<br /> . . transaction->status_ID(): ' . $STS_ID;
643
+		}
644
+		if (
645
+			// release Tickets for Failed Transactions and Abandoned Transactions
646
+			$STS_ID === EEM_Transaction::failed_status_code
647
+			|| $STS_ID === EEM_Transaction::abandoned_status_code
648
+			|| (
649
+				// also release Tickets for Incomplete Transactions, but ONLY if the Registrations are NOT Approved
650
+				$STS_ID === EEM_Transaction::incomplete_status_code
651
+				&& $registration->status_ID() !== EEM_Registration::status_id_approved
652
+			)
653
+		) {
654
+			$ticket = $registration->ticket();
655
+			if ($ticket instanceof EE_Ticket) {
656
+				$ticket->add_extra_meta(
657
+					EE_Ticket::META_KEY_TICKET_RESERVATIONS,
658
+					__LINE__ . ') ' . __METHOD__ . '()'
659
+				);
660
+			}
661
+			$registration->release_reserved_ticket();
662
+			return 1;
663
+		}
664
+		return 0;
665
+	}
666
+
667
+
668
+
669
+	/********************************** SESSION_CART_RESET  **********************************/
670
+
671
+
672
+
673
+	/**
674
+	 * callback hooked into 'AHEE__EE_Session__reset_cart__before_reset'
675
+	 *
676
+	 * @param EE_Session $session
677
+	 * @return void
678
+	 * @throws EE_Error
679
+	 * @throws InvalidArgumentException
680
+	 * @throws ReflectionException
681
+	 * @throws InvalidDataTypeException
682
+	 * @throws InvalidInterfaceException
683
+	 */
684
+	public static function session_cart_reset(EE_Session $session)
685
+	{
686
+		if (self::debug) {
687
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
688
+		}
689
+		// first check of the session has a valid Checkout object
690
+		$checkout = $session->checkout();
691
+		if ($checkout instanceof EE_Checkout) {
692
+			// and use that to clear ticket reservations because it will update the associated registration meta data
693
+			EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
694
+			return;
695
+		}
696
+		$cart = $session->cart();
697
+		if ($cart instanceof EE_Cart) {
698
+			if (self::debug) {
699
+				echo '<br /><br /> cart instance of EE_Cart: ';
700
+			}
701
+			EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart, $session);
702
+		} else {
703
+			if (self::debug) {
704
+				echo '<br /><br /> invalid EE_Cart: ';
705
+				var_export($cart, true);
706
+			}
707
+		}
708
+	}
709
+
710
+
711
+
712
+	/**
713
+	 * releases reserved tickets in the EE_Cart
714
+	 *
715
+	 * @param EE_Cart $cart
716
+	 * @return void
717
+	 * @throws EE_Error
718
+	 * @throws InvalidArgumentException
719
+	 * @throws ReflectionException
720
+	 * @throws InvalidDataTypeException
721
+	 * @throws InvalidInterfaceException
722
+	 */
723
+	protected function _session_cart_reset(EE_Cart $cart, EE_Session $session)
724
+	{
725
+		if (self::debug) {
726
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
727
+		}
728
+		EE_Registry::instance()->load_helper('Line_Item');
729
+		$ticket_line_items = $cart->get_tickets();
730
+		if (empty($ticket_line_items)) {
731
+			return;
732
+		}
733
+		foreach ($ticket_line_items as $ticket_line_item) {
734
+			if (self::debug) {
735
+				echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID();
736
+			}
737
+			if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
738
+				if (self::debug) {
739
+					echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
740
+				}
741
+				$ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
742
+				if ($ticket instanceof EE_Ticket) {
743
+					if (self::debug) {
744
+						echo '<br /> . . ticket->ID(): ' . $ticket->ID();
745
+						echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
746
+					}
747
+					$ticket->add_extra_meta(
748
+						EE_Ticket::META_KEY_TICKET_RESERVATIONS,
749
+						__LINE__ . ') ' . __METHOD__ . '() SID = ' . $session->id()
750
+					);
751
+					$this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
752
+				}
753
+			}
754
+		}
755
+		if (self::debug) {
756
+			echo '<br /><br /> RESET COMPLETED ';
757
+		}
758
+	}
759
+
760
+
761
+
762
+	/********************************** SESSION_CHECKOUT_RESET  **********************************/
763
+
764
+
765
+
766
+	/**
767
+	 * callback hooked into 'AHEE__EE_Session__reset_checkout__before_reset'
768
+	 *
769
+	 * @param EE_Session $session
770
+	 * @return void
771
+	 * @throws EE_Error
772
+	 * @throws InvalidSessionDataException
773
+	 */
774
+	public static function session_checkout_reset(EE_Session $session)
775
+	{
776
+		$checkout = $session->checkout();
777
+		if ($checkout instanceof EE_Checkout) {
778
+			EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
779
+		}
780
+	}
781
+
782
+
783
+
784
+	/**
785
+	 * releases reserved tickets for the EE_Checkout->transaction
786
+	 *
787
+	 * @param EE_Checkout $checkout
788
+	 * @return void
789
+	 * @throws EE_Error
790
+	 * @throws InvalidSessionDataException
791
+	 */
792
+	protected function _session_checkout_reset(EE_Checkout $checkout)
793
+	{
794
+		if (self::debug) {
795
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
796
+		}
797
+		// we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
798
+		if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
799
+			return;
800
+		}
801
+		$this->_release_all_reserved_tickets_for_transaction($checkout->transaction);
802
+	}
803
+
804
+
805
+
806
+	/********************************** SESSION_EXPIRED_RESET  **********************************/
807
+
808
+
809
+
810
+	/**
811
+	 * @param    EE_Session $session
812
+	 * @return    void
813
+	 */
814
+	public static function session_expired_reset(EE_Session $session)
815
+	{
816
+	}
817
+
818
+
819
+
820
+	/********************************** PROCESS_ABANDONED_TRANSACTIONS  **********************************/
821
+
822
+
823
+
824
+	/**
825
+	 * releases reserved tickets for all registrations of an ABANDONED EE_Transaction
826
+	 * by default, will NOT release tickets for free transactions, or any that have received a payment
827
+	 *
828
+	 * @param EE_Transaction $transaction
829
+	 * @return void
830
+	 * @throws EE_Error
831
+	 * @throws InvalidSessionDataException
832
+	 */
833
+	public static function process_abandoned_transactions(EE_Transaction $transaction)
834
+	{
835
+		// is this TXN free or has any money been paid towards this TXN? If so, then leave it alone
836
+		if ($transaction->is_free() || $transaction->paid() > 0) {
837
+			if (self::debug) {
838
+				// DEBUG LOG
839
+				EEH_Debug_Tools::log(
840
+					__CLASS__,
841
+					__FUNCTION__,
842
+					__LINE__,
843
+					array($transaction),
844
+					false,
845
+					'EE_Transaction: ' . $transaction->ID()
846
+				);
847
+			}
848
+			return;
849
+		}
850
+		// have their been any successful payments made ?
851
+		$payments = $transaction->payments();
852
+		foreach ($payments as $payment) {
853
+			if ($payment instanceof EE_Payment && $payment->status() === EEM_Payment::status_id_approved) {
854
+				if (self::debug) {
855
+					// DEBUG LOG
856
+					EEH_Debug_Tools::log(
857
+						__CLASS__,
858
+						__FUNCTION__,
859
+						__LINE__,
860
+						array($payment),
861
+						false,
862
+						'EE_Transaction: ' . $transaction->ID()
863
+					);
864
+				}
865
+				return;
866
+			}
867
+		}
868
+		// since you haven't even attempted to pay for your ticket...
869
+		EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
870
+	}
871
+
872
+
873
+
874
+	/********************************** PROCESS_FAILED_TRANSACTIONS  **********************************/
875
+
876
+
877
+
878
+	/**
879
+	 * releases reserved tickets for absolutely ALL registrations of a FAILED EE_Transaction
880
+	 *
881
+	 * @param EE_Transaction $transaction
882
+	 * @return void
883
+	 * @throws EE_Error
884
+	 * @throws InvalidSessionDataException
885
+	 */
886
+	public static function process_failed_transactions(EE_Transaction $transaction)
887
+	{
888
+		// since you haven't even attempted to pay for your ticket...
889
+		EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
890
+	}
891
+
892
+
893
+
894
+	/********************************** RESET RESERVATION COUNTS  *********************************/
895
+
896
+
897
+
898
+	/**
899
+	 * Resets all ticket and datetime reserved counts to zero
900
+	 * Tickets that are currently associated with a Transaction that is in progress
901
+	 *
902
+	 * @throws EE_Error
903
+	 * @throws DomainException
904
+	 * @throws InvalidDataTypeException
905
+	 * @throws InvalidInterfaceException
906
+	 * @throws InvalidArgumentException
907
+	 * @throws UnexpectedEntityException
908
+	 */
909
+	public static function reset_reservation_counts()
910
+	{
911
+		/** @var EE_Line_Item[] $valid_reserved_tickets */
912
+		$valid_reserved_tickets = array();
913
+		/** @var EE_Transaction[] $transactions_not_in_progress */
914
+		$transactions_not_in_progress = EEM_Transaction::instance()->get_transactions_not_in_progress();
915
+		foreach ($transactions_not_in_progress as $transaction) {
916
+			// if this TXN has been fully completed, then skip it
917
+			if ($transaction->reg_step_completed('finalize_registration')) {
918
+				continue;
919
+			}
920
+			$total_line_item = $transaction->total_line_item();
921
+			// $transaction_in_progress->line
922
+			if (! $total_line_item instanceof EE_Line_Item) {
923
+				throw new DomainException(
924
+					esc_html__(
925
+						'Transaction does not have a valid Total Line Item associated with it.',
926
+						'event_espresso'
927
+					)
928
+				);
929
+			}
930
+			$valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
931
+				$total_line_item
932
+			);
933
+		}
934
+		$total_line_items = EEM_Line_Item::instance()->get_total_line_items_for_active_carts();
935
+		foreach ($total_line_items as $total_line_item) {
936
+			$valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
937
+				$total_line_item
938
+			);
939
+		}
940
+		$tickets_with_reservations = EEM_Ticket::instance()->get_tickets_with_reservations();
941
+		return EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
942
+			$tickets_with_reservations,
943
+			$valid_reserved_tickets,
944
+			__FUNCTION__
945
+		);
946
+	}
947
+
948
+
949
+
950
+	/**
951
+	 * @param EE_Line_Item $total_line_item
952
+	 * @return EE_Line_Item[]
953
+	 */
954
+	private static function get_ticket_line_items_for_grand_total(EE_Line_Item $total_line_item)
955
+	{
956
+		/** @var EE_Line_Item[] $valid_reserved_tickets */
957
+		$valid_reserved_tickets = array();
958
+		$ticket_line_items      = EEH_Line_Item::get_ticket_line_items($total_line_item);
959
+		foreach ($ticket_line_items as $ticket_line_item) {
960
+			if ($ticket_line_item instanceof EE_Line_Item) {
961
+				$valid_reserved_tickets[] = $ticket_line_item;
962
+			}
963
+		}
964
+		return $valid_reserved_tickets;
965
+	}
966
+
967
+
968
+
969
+	/**
970
+	 * @param EE_Ticket[]    $tickets_with_reservations
971
+	 * @param EE_Line_Item[] $valid_reserved_ticket_line_items
972
+	 * @return int
973
+	 * @throws UnexpectedEntityException
974
+	 * @throws DomainException
975
+	 * @throws EE_Error
976
+	 */
977
+	private static function release_reservations_for_tickets(
978
+		array $tickets_with_reservations,
979
+		array $valid_reserved_ticket_line_items = array(),
980
+		$source
981
+	) {
982
+		$total_tickets_released = 0;
983
+		$sold_out_events = array();
984
+		foreach ($tickets_with_reservations as $ticket_with_reservations) {
985
+			if (! $ticket_with_reservations instanceof EE_Ticket) {
986
+				continue;
987
+			}
988
+			$reserved_qty = $ticket_with_reservations->reserved();
989
+			foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) {
990
+				if (
991
+					$valid_reserved_ticket_line_item instanceof EE_Line_Item
992
+					&& $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID()
993
+				) {
994
+					$reserved_qty -= $valid_reserved_ticket_line_item->quantity();
995
+				}
996
+			}
997
+			if ($reserved_qty > 0) {
998
+				$ticket_with_reservations->add_extra_meta(
999
+					EE_Ticket::META_KEY_TICKET_RESERVATIONS,
1000
+					__LINE__ . ') ' . $source . '()'
1001
+				);
1002
+				$ticket_with_reservations->decrease_reserved($reserved_qty, true, __LINE__ . ') TSM');
1003
+				$ticket_with_reservations->save();
1004
+				$total_tickets_released += $reserved_qty;
1005
+				$event = $ticket_with_reservations->get_related_event();
1006
+				// track sold out events
1007
+				if ($event instanceof EE_Event && $event->is_sold_out()) {
1008
+					$sold_out_events[] = $event;
1009
+				}
1010
+			}
1011
+		}
1012
+		// double check whether sold out events should remain sold out after releasing tickets
1013
+		if($sold_out_events !== array()){
1014
+			foreach ($sold_out_events as $sold_out_event) {
1015
+				/** @var EE_Event $sold_out_event */
1016
+				$sold_out_event->perform_sold_out_status_check();
1017
+			}
1018
+		}
1019
+		return $total_tickets_released;
1020
+	}
1021
+
1022
+
1023
+
1024
+	/********************************** SHUTDOWN  **********************************/
1025
+
1026
+
1027
+
1028
+	/**
1029
+	 * @return false|int
1030
+	 * @throws EE_Error
1031
+	 * @throws InvalidArgumentException
1032
+	 * @throws InvalidDataTypeException
1033
+	 * @throws InvalidInterfaceException
1034
+	 */
1035
+	public static function clear_expired_line_items_with_no_transaction()
1036
+	{
1037
+		/** @type WPDB $wpdb */
1038
+		global $wpdb;
1039
+		return $wpdb->query(
1040
+			$wpdb->prepare(
1041
+				'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
1042 1042
                 WHERE TXN_ID = 0 AND LIN_timestamp <= %s',
1043
-                // use GMT time because that's what LIN_timestamps are in
1044
-                date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan())
1045
-            )
1046
-        );
1047
-    }
1043
+				// use GMT time because that's what LIN_timestamps are in
1044
+				date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan())
1045
+			)
1046
+		);
1047
+	}
1048 1048
 
1049 1049
 }
1050 1050
 // End of file EED_Ticket_Sales_Monitor.module.php
Please login to merge, or discard this patch.
Spacing   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -23,7 +23,7 @@  discard block
 block discarded – undo
23 23
 class EED_Ticket_Sales_Monitor extends EED_Module
24 24
 {
25 25
 
26
-    const debug = false;    //	true false
26
+    const debug = false; //	true false
27 27
 
28 28
     /**
29 29
      * an array of raw ticket data from EED_Ticket_Selector
@@ -209,17 +209,17 @@  discard block
 block discarded – undo
209 209
             /** @var EE_Line_Item $total_line_item */
210 210
             $ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item);
211 211
             foreach ($ticket_line_items as $ticket_line_item) {
212
-                if (! $ticket_line_item instanceof EE_Line_Item) {
212
+                if ( ! $ticket_line_item instanceof EE_Line_Item) {
213 213
                     continue;
214 214
                 }
215 215
                 if ($total_line_item->timestamp(true) <= $expired) {
216
-                    $expired_ticket_IDs[ $ticket_line_item->OBJ_ID() ] = $ticket_line_item->OBJ_ID();
216
+                    $expired_ticket_IDs[$ticket_line_item->OBJ_ID()] = $ticket_line_item->OBJ_ID();
217 217
                 } else {
218
-                    $valid_ticket_line_items[ $ticket_line_item->OBJ_ID() ] = $ticket_line_item;
218
+                    $valid_ticket_line_items[$ticket_line_item->OBJ_ID()] = $ticket_line_item;
219 219
                 }
220 220
             }
221 221
         }
222
-        if (! empty($expired_ticket_IDs)) {
222
+        if ( ! empty($expired_ticket_IDs)) {
223 223
             EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
224 224
                 \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
225 225
                 $valid_ticket_line_items,
@@ -262,8 +262,8 @@  discard block
 block discarded – undo
262 262
             $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
263 263
         }
264 264
         if (self::debug) {
265
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()';
266
-            echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>';
265
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'()';
266
+            echo '<br /><br /><b> RETURNED QTY: '.$qty.'</b>';
267 267
         }
268 268
         return $qty;
269 269
     }
@@ -282,36 +282,36 @@  discard block
 block discarded – undo
282 282
     protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
283 283
     {
284 284
         if (self::debug) {
285
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
285
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
286 286
         }
287
-        if (! $ticket instanceof EE_Ticket) {
287
+        if ( ! $ticket instanceof EE_Ticket) {
288 288
             return 0;
289 289
         }
290 290
         if (self::debug) {
291
-            echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>';
292
-            echo '<br /> . original ticket->reserved: ' . $ticket->reserved();
291
+            echo '<br /><b> . ticket->ID: '.$ticket->ID().'</b>';
292
+            echo '<br /> . original ticket->reserved: '.$ticket->reserved();
293 293
         }
294 294
         $ticket->refresh_from_db();
295 295
         // first let's determine the ticket availability based on sales
296 296
         $available = $ticket->qty('saleable');
297 297
         if (self::debug) {
298
-            echo '<br /> . . . ticket->qty: ' . $ticket->qty();
299
-            echo '<br /> . . . ticket->sold: ' . $ticket->sold();
300
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
301
-            echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
302
-            echo '<br /> . . . available: ' . $available;
298
+            echo '<br /> . . . ticket->qty: '.$ticket->qty();
299
+            echo '<br /> . . . ticket->sold: '.$ticket->sold();
300
+            echo '<br /> . . . ticket->reserved: '.$ticket->reserved();
301
+            echo '<br /> . . . ticket->qty(saleable): '.$ticket->qty('saleable');
302
+            echo '<br /> . . . available: '.$available;
303 303
         }
304 304
         if ($available < 1) {
305 305
             $this->_ticket_sold_out($ticket);
306 306
             return 0;
307 307
         }
308 308
         if (self::debug) {
309
-            echo '<br /> . . . qty: ' . $qty;
309
+            echo '<br /> . . . qty: '.$qty;
310 310
         }
311 311
         if ($available < $qty) {
312 312
             $qty = $available;
313 313
             if (self::debug) {
314
-                echo '<br /> . . . QTY ADJUSTED: ' . $qty;
314
+                echo '<br /> . . . QTY ADJUSTED: '.$qty;
315 315
             }
316 316
             $this->_ticket_quantity_decremented($ticket);
317 317
         }
@@ -332,9 +332,9 @@  discard block
 block discarded – undo
332 332
     protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
333 333
     {
334 334
         if (self::debug) {
335
-            echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity;
335
+            echo '<br /><br /> . . . INCREASE RESERVED: '.$quantity;
336 336
         }
337
-        $ticket->increase_reserved($quantity, __LINE__ . ') TSM');
337
+        $ticket->increase_reserved($quantity, __LINE__.') TSM');
338 338
         return $ticket->save();
339 339
     }
340 340
 
@@ -349,12 +349,12 @@  discard block
 block discarded – undo
349 349
     protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
350 350
     {
351 351
         if (self::debug) {
352
-            echo '<br /> . . . ticket->ID: ' . $ticket->ID();
353
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
352
+            echo '<br /> . . . ticket->ID: '.$ticket->ID();
353
+            echo '<br /> . . . ticket->reserved: '.$ticket->reserved();
354 354
         }
355
-        $ticket->decrease_reserved($quantity, true, __LINE__ . ') TSM');
355
+        $ticket->decrease_reserved($quantity, true, __LINE__.') TSM');
356 356
         if (self::debug) {
357
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
357
+            echo '<br /> . . . ticket->reserved: '.$ticket->reserved();
358 358
         }
359 359
         return $ticket->save() ? 1 : 0;
360 360
     }
@@ -372,8 +372,8 @@  discard block
 block discarded – undo
372 372
     protected function _ticket_sold_out(EE_Ticket $ticket)
373 373
     {
374 374
         if (self::debug) {
375
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
376
-            echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
375
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
376
+            echo '<br /> . . ticket->name: '.$this->_get_ticket_and_event_name($ticket);
377 377
         }
378 378
         $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
379 379
     }
@@ -391,8 +391,8 @@  discard block
 block discarded – undo
391 391
     protected function _ticket_quantity_decremented(EE_Ticket $ticket)
392 392
     {
393 393
         if (self::debug) {
394
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
395
-            echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
394
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
395
+            echo '<br /> . . ticket->name: '.$this->_get_ticket_and_event_name($ticket);
396 396
         }
397 397
         $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
398 398
     }
@@ -445,7 +445,7 @@  discard block
 block discarded – undo
445 445
         if ($ticket instanceof EE_Ticket) {
446 446
             $ticket->add_extra_meta(
447 447
                 EE_Ticket::META_KEY_TICKET_RESERVATIONS,
448
-                __LINE__ . ') ' . __METHOD__ . '()'
448
+                __LINE__.') '.__METHOD__.'()'
449 449
             );
450 450
             if ($quantity > 0) {
451 451
                 EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity);
@@ -469,7 +469,7 @@  discard block
 block discarded – undo
469 469
     {
470 470
         $ticket->add_extra_meta(
471 471
             EE_Ticket::META_KEY_TICKET_RESERVATIONS,
472
-            __LINE__ . ') ' . __METHOD__ . '()'
472
+            __LINE__.') '.__METHOD__.'()'
473 473
         );
474 474
         EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
475 475
     }
@@ -506,18 +506,18 @@  discard block
 block discarded – undo
506 506
     protected function _post_notices()
507 507
     {
508 508
         if (self::debug) {
509
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
509
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
510 510
         }
511 511
         $refresh_msg    = '';
512 512
         $none_added_msg = '';
513 513
         if (defined('DOING_AJAX') && DOING_AJAX) {
514
-            $refresh_msg    = __(
514
+            $refresh_msg = __(
515 515
                 'Please refresh the page to view updated ticket quantities.',
516 516
                 'event_espresso'
517 517
             );
518 518
             $none_added_msg = __('No tickets were added for the event.', 'event_espresso');
519 519
         }
520
-        if (! empty($this->sold_out_tickets)) {
520
+        if ( ! empty($this->sold_out_tickets)) {
521 521
             EE_Error::add_attention(
522 522
                 sprintf(
523 523
                     apply_filters(
@@ -540,7 +540,7 @@  discard block
 block discarded – undo
540 540
             // and reset the cart
541 541
             EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN);
542 542
         }
543
-        if (! empty($this->decremented_tickets)) {
543
+        if ( ! empty($this->decremented_tickets)) {
544 544
             EE_Error::add_attention(
545 545
                 sprintf(
546 546
                     apply_filters(
@@ -578,8 +578,8 @@  discard block
 block discarded – undo
578 578
     protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
579 579
     {
580 580
         if (self::debug) {
581
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
582
-            echo '<br /> . transaction->ID: ' . $transaction->ID();
581
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
582
+            echo '<br /> . transaction->ID: '.$transaction->ID();
583 583
         }
584 584
         // check if 'finalize_registration' step has been completed...
585 585
         $finalized = $transaction->reg_step_completed('finalize_registration');
@@ -591,13 +591,13 @@  discard block
 block discarded – undo
591 591
                 __LINE__,
592 592
                 array('finalized' => $finalized),
593 593
                 false,
594
-                'EE_Transaction: ' . $transaction->ID()
594
+                'EE_Transaction: '.$transaction->ID()
595 595
             );
596 596
         }
597 597
         // how many tickets were released
598 598
         $count = 0;
599 599
         if (self::debug) {
600
-            echo '<br /> . . . finalized: ' . $finalized;
600
+            echo '<br /> . . . finalized: '.$finalized;
601 601
         }
602 602
         $release_tickets_with_TXN_status = array(
603 603
             EEM_Transaction::failed_status_code,
@@ -605,10 +605,10 @@  discard block
 block discarded – undo
605 605
             EEM_Transaction::incomplete_status_code,
606 606
         );
607 607
         // if the session is getting cleared BEFORE the TXN has been finalized
608
-        if (! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
608
+        if ( ! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
609 609
             // let's cancel any reserved tickets
610 610
             $registrations = $transaction->registrations();
611
-            if (! empty($registrations)) {
611
+            if ( ! empty($registrations)) {
612 612
                 foreach ($registrations as $registration) {
613 613
                     if ($registration instanceof EE_Registration) {
614 614
                         $count += $this->_release_reserved_ticket_for_registration($registration, $transaction);
@@ -636,10 +636,10 @@  discard block
 block discarded – undo
636 636
     ) {
637 637
         $STS_ID = $transaction->status_ID();
638 638
         if (self::debug) {
639
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
640
-            echo '<br /> . . registration->ID: ' . $registration->ID();
641
-            echo '<br /> . . registration->status_ID: ' . $registration->status_ID();
642
-            echo '<br /> . . transaction->status_ID(): ' . $STS_ID;
639
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
640
+            echo '<br /> . . registration->ID: '.$registration->ID();
641
+            echo '<br /> . . registration->status_ID: '.$registration->status_ID();
642
+            echo '<br /> . . transaction->status_ID(): '.$STS_ID;
643 643
         }
644 644
         if (
645 645
             // release Tickets for Failed Transactions and Abandoned Transactions
@@ -655,7 +655,7 @@  discard block
 block discarded – undo
655 655
             if ($ticket instanceof EE_Ticket) {
656 656
                 $ticket->add_extra_meta(
657 657
                     EE_Ticket::META_KEY_TICKET_RESERVATIONS,
658
-                    __LINE__ . ') ' . __METHOD__ . '()'
658
+                    __LINE__.') '.__METHOD__.'()'
659 659
                 );
660 660
             }
661 661
             $registration->release_reserved_ticket();
@@ -684,7 +684,7 @@  discard block
 block discarded – undo
684 684
     public static function session_cart_reset(EE_Session $session)
685 685
     {
686 686
         if (self::debug) {
687
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
687
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
688 688
         }
689 689
         // first check of the session has a valid Checkout object
690 690
         $checkout = $session->checkout();
@@ -723,7 +723,7 @@  discard block
 block discarded – undo
723 723
     protected function _session_cart_reset(EE_Cart $cart, EE_Session $session)
724 724
     {
725 725
         if (self::debug) {
726
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
726
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
727 727
         }
728 728
         EE_Registry::instance()->load_helper('Line_Item');
729 729
         $ticket_line_items = $cart->get_tickets();
@@ -732,21 +732,21 @@  discard block
 block discarded – undo
732 732
         }
733 733
         foreach ($ticket_line_items as $ticket_line_item) {
734 734
             if (self::debug) {
735
-                echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID();
735
+                echo '<br /> . ticket_line_item->ID(): '.$ticket_line_item->ID();
736 736
             }
737 737
             if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
738 738
                 if (self::debug) {
739
-                    echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
739
+                    echo '<br /> . . ticket_line_item->OBJ_ID(): '.$ticket_line_item->OBJ_ID();
740 740
                 }
741 741
                 $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
742 742
                 if ($ticket instanceof EE_Ticket) {
743 743
                     if (self::debug) {
744
-                        echo '<br /> . . ticket->ID(): ' . $ticket->ID();
745
-                        echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
744
+                        echo '<br /> . . ticket->ID(): '.$ticket->ID();
745
+                        echo '<br /> . . ticket_line_item->quantity(): '.$ticket_line_item->quantity();
746 746
                     }
747 747
                     $ticket->add_extra_meta(
748 748
                         EE_Ticket::META_KEY_TICKET_RESERVATIONS,
749
-                        __LINE__ . ') ' . __METHOD__ . '() SID = ' . $session->id()
749
+                        __LINE__.') '.__METHOD__.'() SID = '.$session->id()
750 750
                     );
751 751
                     $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
752 752
                 }
@@ -792,7 +792,7 @@  discard block
 block discarded – undo
792 792
     protected function _session_checkout_reset(EE_Checkout $checkout)
793 793
     {
794 794
         if (self::debug) {
795
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
795
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
796 796
         }
797 797
         // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
798 798
         if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
@@ -842,7 +842,7 @@  discard block
 block discarded – undo
842 842
                     __LINE__,
843 843
                     array($transaction),
844 844
                     false,
845
-                    'EE_Transaction: ' . $transaction->ID()
845
+                    'EE_Transaction: '.$transaction->ID()
846 846
                 );
847 847
             }
848 848
             return;
@@ -859,7 +859,7 @@  discard block
 block discarded – undo
859 859
                         __LINE__,
860 860
                         array($payment),
861 861
                         false,
862
-                        'EE_Transaction: ' . $transaction->ID()
862
+                        'EE_Transaction: '.$transaction->ID()
863 863
                     );
864 864
                 }
865 865
                 return;
@@ -919,7 +919,7 @@  discard block
 block discarded – undo
919 919
             }
920 920
             $total_line_item = $transaction->total_line_item();
921 921
             // $transaction_in_progress->line
922
-            if (! $total_line_item instanceof EE_Line_Item) {
922
+            if ( ! $total_line_item instanceof EE_Line_Item) {
923 923
                 throw new DomainException(
924 924
                     esc_html__(
925 925
                         'Transaction does not have a valid Total Line Item associated with it.',
@@ -982,7 +982,7 @@  discard block
 block discarded – undo
982 982
         $total_tickets_released = 0;
983 983
         $sold_out_events = array();
984 984
         foreach ($tickets_with_reservations as $ticket_with_reservations) {
985
-            if (! $ticket_with_reservations instanceof EE_Ticket) {
985
+            if ( ! $ticket_with_reservations instanceof EE_Ticket) {
986 986
                 continue;
987 987
             }
988 988
             $reserved_qty = $ticket_with_reservations->reserved();
@@ -997,9 +997,9 @@  discard block
 block discarded – undo
997 997
             if ($reserved_qty > 0) {
998 998
                 $ticket_with_reservations->add_extra_meta(
999 999
                     EE_Ticket::META_KEY_TICKET_RESERVATIONS,
1000
-                    __LINE__ . ') ' . $source . '()'
1000
+                    __LINE__.') '.$source.'()'
1001 1001
                 );
1002
-                $ticket_with_reservations->decrease_reserved($reserved_qty, true, __LINE__ . ') TSM');
1002
+                $ticket_with_reservations->decrease_reserved($reserved_qty, true, __LINE__.') TSM');
1003 1003
                 $ticket_with_reservations->save();
1004 1004
                 $total_tickets_released += $reserved_qty;
1005 1005
                 $event = $ticket_with_reservations->get_related_event();
@@ -1010,7 +1010,7 @@  discard block
 block discarded – undo
1010 1010
             }
1011 1011
         }
1012 1012
         // double check whether sold out events should remain sold out after releasing tickets
1013
-        if($sold_out_events !== array()){
1013
+        if ($sold_out_events !== array()) {
1014 1014
             foreach ($sold_out_events as $sold_out_event) {
1015 1015
                 /** @var EE_Event $sold_out_event */
1016 1016
                 $sold_out_event->perform_sold_out_status_check();
@@ -1038,7 +1038,7 @@  discard block
 block discarded – undo
1038 1038
         global $wpdb;
1039 1039
         return $wpdb->query(
1040 1040
             $wpdb->prepare(
1041
-                'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
1041
+                'DELETE FROM '.EEM_Line_Item::instance()->table().'
1042 1042
                 WHERE TXN_ID = 0 AND LIN_timestamp <= %s',
1043 1043
                 // use GMT time because that's what LIN_timestamps are in
1044 1044
                 date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan())
Please login to merge, or discard this patch.