-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEED_Multi_Event_Registration.module.php
2418 lines (2253 loc) · 87.9 KB
/
EED_Multi_Event_Registration.module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use EventEspresso\core\domain\entities\RegCode;
use EventEspresso\core\domain\entities\RegUrlLink;
use EventEspresso\core\domain\services\registration\CreateRegistrationService;
use EventEspresso\core\services\loaders\LoaderFactory;
use EventEspresso\core\services\request\CurrentPage;
use EventEspresso\core\services\request\DataType;
use EventEspresso\core\services\request\Response;
/**
* Multi_Event_Registration class
*
* @package Multi Event Registration
* @subpackage espresso-multi-registration
* @author Brent Christensen
* @method EED_Multi_Event_Registration EED_Module::get_instance($module_name = '')
* @method EE_Multi_Event_Registration_Config config()
*/
class EED_Multi_Event_Registration extends EED_Module
{
/**
* Cart? Event Cart? Ticket Basket?
*
* @var string $event_cart_name
*/
public static string $event_cart_name = '';
/**
* @var bool $is_ajax
*/
private bool $is_ajax = false;
/**
* @var array holds the array of instantiated Admin route object indexed by route.
*/
public static array $admin_route_objects = [];
/**
* @return EED_Multi_Event_Registration
* @throws EE_Error
* @throws ReflectionException
*/
public static function instance(): EED_Multi_Event_Registration
{
return parent::get_instance(__CLASS__);
}
/**
* @return string
*/
public static function event_cart_name(): string
{
return self::$event_cart_name;
}
/**
* set_hooks - for hooking into EE Core, other modules, etc
*
* @return void
*/
public static function set_hooks()
{
EED_Multi_Event_Registration::set_definitions();
EED_Module::registerRoute('view', 'Multi_Event_Registration', 'view_event_cart', 'event_cart');
EED_Module::registerRoute('update', 'Multi_Event_Registration', 'update_event_cart', 'event_cart');
EED_Module::registerRoute('add_ticket', 'Multi_Event_Registration', 'add_ticket', 'event_cart');
EED_Module::registerRoute('remove_ticket', 'Multi_Event_Registration', 'remove_ticket', 'event_cart');
EED_Module::registerRoute('delete_ticket', 'Multi_Event_Registration', 'delete_ticket', 'event_cart');
EED_Module::registerRoute('empty', 'Multi_Event_Registration', 'empty_event_cart', 'event_cart');
add_action('wp_enqueue_scripts', ['EED_Multi_Event_Registration', 'enqueue_styles_and_scripts']);
// don't empty cart
add_filter('FHEE__EE_Ticket_Selector__process_ticket_selections__clear_session', '__return_false');
// show a View Cart button even if there are no tickets
add_filter(
'FHEE__EE_Ticket_Selector__display_ticket_selector_submit__no_tickets_but_display_register_now_button',
['EED_Multi_Event_Registration', 'no_tickets_but_display_register_now_button'],
10,
2
);
// process registration links
add_filter(
'FHEE__EE_Ticket_Selector__ticket_selector_form_open__html',
['EED_Multi_Event_Registration', 'filter_ticket_selector_form_html'],
10,
2
);
add_filter(
'FHEE__EE_Ticket_Selector__display_ticket_selector_submit__btn_text',
['EED_Multi_Event_Registration', 'filter_ticket_selector_submit_button'],
10,
2
);
add_filter(
'FHEE__EED_Ticket_Selector__proceed_to_registration_btn_txt',
['EED_Multi_Event_Registration', 'filter_ticket_selector_button_txt'],
10,
2
);
add_filter(
'FHEE__EE_Ticket_Selector__display_view_details_btn__btn_text',
['EED_Multi_Event_Registration', 'filter_ticket_selector_button_txt'],
10,
2
);
add_filter(
'FHEE__EED_Ticket_Selector__ticket_selector_iframe__css',
['EED_Multi_Event_Registration', 'style_sheet_URLs']
);
add_filter(
'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__js',
['EED_Multi_Event_Registration', 'javascript_URLs']
);
add_filter(
'FHEE__EE_Ticket_Selector__process_ticket_selections__success_redirect_url',
['EED_Multi_Event_Registration', 'filter_ticket_selector_redirect_url'],
10,
2
);
add_filter(
'FHEE__EED_Ticket_Selector__proceed_to_registration_btn_url',
['EED_Multi_Event_Registration', 'filter_ticket_selector_button_url'],
10,
2
);
add_filter(
'FHEE__EE_Ticket_Selector__display_view_details_btn__btn_url',
['EED_Multi_Event_Registration', 'filter_ticket_selector_button_url'],
10,
2
);
// verify that SPCO registrations correspond to tickets in cart
add_filter(
'FHEE__EED_Single_Page_Checkout___initialize__checkout',
['EED_Multi_Event_Registration', 'verify_tickets_in_cart']
);
// redirect to event_cart
add_action(
'EED_Ticket_Selector__process_ticket_selections__before',
['EED_Multi_Event_Registration', 'redirect_to_event_cart']
);
add_filter(
'FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html',
['EED_Multi_Event_Registration', 'return_to_event_cart_button'],
10,
2
);
// toggling reg status
add_filter(
'FHEE__EE_Registration_Processor__toggle_registration_status_if_no_monies_owing',
['EED_Multi_Event_Registration', 'toggle_registration_status_if_no_monies_owing'],
10,
2
);
// display errors
add_action('wp_footer', ['EED_Multi_Event_Registration', 'cart_results_modal_div'], 1);
// update cart in session
add_action('shutdown', ['EED_Multi_Event_Registration', 'save_cart']);
}
/**
* set_hooks_admin - for hooking into EE Admin Core, other modules, etc
*
* @return void
*/
public static function set_hooks_admin()
{
EED_Multi_Event_Registration::set_definitions();
// loads additional classes for modifying admin pages
add_action('admin_init', ['EED_Multi_Event_Registration', 'route_admin_page_requests']);
add_action(
'EED_Ticket_Selector__process_ticket_selections__before',
['EED_Multi_Event_Registration', 'redirect_to_event_cart']
);
// process ticket selections
add_action(
'wp_ajax_espresso_process_ticket_selections',
['EED_Multi_Event_Registration', 'process_ticket_selections']
);
add_action(
'wp_ajax_nopriv_espresso_process_ticket_selections',
['EED_Multi_Event_Registration', 'process_ticket_selections']
);
// don't empty cart
add_filter('FHEE__EE_Ticket_Selector__process_ticket_selections__clear_session', '__return_false');
// ajax add attendees
add_action('wp_ajax_espresso_add_ticket_to_event_cart', ['EED_Multi_Event_Registration', 'ajax_add_ticket']);
add_action(
'wp_ajax_nopriv_espresso_add_ticket_to_event_cart',
['EED_Multi_Event_Registration', 'ajax_add_ticket']
);
// ajax remove attendees
add_action(
'wp_ajax_espresso_remove_ticket_from_event_cart',
['EED_Multi_Event_Registration', 'ajax_remove_ticket']
);
add_action(
'wp_ajax_nopriv_espresso_remove_ticket_from_event_cart',
['EED_Multi_Event_Registration', 'ajax_remove_ticket']
);
// ajax remove event
add_action(
'wp_ajax_espresso_delete_ticket_from_event_cart',
['EED_Multi_Event_Registration', 'ajax_delete_ticket']
);
add_action(
'wp_ajax_nopriv_espresso_delete_ticket_from_event_cart',
['EED_Multi_Event_Registration', 'ajax_delete_ticket']
);
// ajax remove event
add_action('wp_ajax_espresso_empty_event_cart', ['EED_Multi_Event_Registration', 'ajax_empty_event_cart']);
add_action(
'wp_ajax_nopriv_espresso_empty_event_cart',
['EED_Multi_Event_Registration', 'ajax_empty_event_cart']
);
// ajax update event
add_action('wp_ajax_espresso_view_event_cart', ['EED_Multi_Event_Registration', 'ajax_view_event_cart']);
add_action('wp_ajax_nopriv_espresso_view_event_cart', ['EED_Multi_Event_Registration', 'ajax_view_event_cart']);
// ajax update event
add_action('wp_ajax_espresso_update_event_cart', ['EED_Multi_Event_Registration', 'ajax_update_event_cart']);
add_action(
'wp_ajax_nopriv_espresso_update_event_cart',
['EED_Multi_Event_Registration', 'ajax_update_event_cart']
);
// ajax available_spaces
add_action(
'wp_ajax_espresso_get_available_spaces',
['EED_Multi_Event_Registration', 'ajax_get_available_spaces']
);
add_action(
'wp_ajax_nopriv_espresso_get_available_spaces',
['EED_Multi_Event_Registration', 'ajax_get_available_spaces']
);
// verify that SPCO registrations correspond to tickets in cart
add_filter(
'FHEE__EED_Single_Page_Checkout___initialize__checkout',
['EED_Multi_Event_Registration', 'verify_tickets_in_cart']
);
// toggling reg status
add_filter(
'FHEE__EE_Registration_Processor__toggle_registration_status_if_no_monies_owing',
['EED_Multi_Event_Registration', 'toggle_registration_status_if_no_monies_owing'],
10,
2
);
// prevent overloading cart
add_filter(
'FHEE__EE_Ticket_Selector___add_ticket_to_cart__allow_add_to_cart',
['EED_Multi_Event_Registration', 'allow_ticket_selector_add_to_cart'],
10,
3
);
add_filter(
'FHEE__EE_Ticket_Selector___add_ticket_to_cart__allow_display_availability_error',
['EED_Multi_Event_Registration', 'display_availability_error']
);
add_filter(
'FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html',
['EED_Multi_Event_Registration', 'return_to_event_cart_button'],
10,
2
);
// update cart in session
add_action('shutdown', ['EED_Multi_Event_Registration', 'save_cart']);
}
/**
* @return void
*/
public static function set_definitions()
{
// base url for the site's registration page - additional url params will be added to this
define('EE_EVENT_QUEUE_BASE_URL', EE_Registry::instance()->CFG->core->reg_page_url());
define(
'EE_EVENTS_LIST_URL',
apply_filters(
'FHEE__EED_Multi_Event_Registration__set_definitions__events_list_url',
get_post_type_archive_link('espresso_events')
)
);
EED_Multi_Event_Registration::$event_cart_name = apply_filters(
'FHEE__EED_Multi_Event_Registration__set_definitions__event_cart_name',
esc_html__('Event Cart', 'event_espresso')
);
EE_Registry::$i18n_js_strings['iframe_tickets_added'] = sprintf(
esc_html__('Success! Please click "View %s" to proceed with your registration.', 'event_espresso'),
EED_Multi_Event_Registration::$event_cart_name
);
}
/**
* loads additional classes for modifying admin pages
*
* @return void
*/
public static function route_admin_page_requests()
{
$route = self::getRequest()->getRequestParam('page');
if (! empty($route)) {
$sanitized_route = sanitize_title($route);
// convert page=espresso_transactions into "Transactions"
$page = ucwords(str_replace('espresso_', '', $sanitized_route));
// then into "EE_MER_Transactions_Admin"
$class_name = 'EE_MER_' . $page . '_Admin';
// and then load that class if it exists
if (class_exists($class_name)) {
EED_Multi_Event_Registration::$admin_route_objects[ $sanitized_route ] = new $class_name();
}
}
}
/**
* this configures this module to use the same config as the EE_Promotions class
*
* @return EE_Multi_Event_Registration_Config
*/
public function set_config(): EE_Multi_Event_Registration_Config
{
$this->set_config_section('addons');
$this->set_config_class('EE_Multi_Event_Registration_Config');
$this->set_config_name('multi_event_registration');
return $this->config();
}
/**
* @return void
*/
protected function init()
{
// stop SPCO from executing
add_filter('FHEE__EED_Single_Page_Checkout__run', '__return_false');
if (! defined('MER_ACTIVE')) {
define('MER_ACTIVE', true);
}
// set MER active to TRUE
add_filter('filter_hook_espresso_MER_active', '__return_true');
$this->is_ajax = self::getRequest()->isFrontAjax();
$this->translate_js_strings();
}
/**
* load resources required to run MER
*
* @return void
* @throws EE_Error
* @throws ReflectionException
*/
public static function load_classes()
{
static $loaded = false;
if (! $loaded) {
EE_Registry::instance()->load_core('Cart');
$loaded = true;
}
}
/**
* @return void
*/
public function translate_js_strings()
{
EE_Registry::$i18n_js_strings['server_error'] =
esc_html__(
'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again or contact support.',
'event_espresso'
);
EE_Registry::$i18n_js_strings['confirm_delete_state'] =
esc_html__(
"This item is required. Removing it will also remove any related items from the event cart!\nClick OK to continue or Cancel to keep this item.",
'event_espresso'
);
}
/**
* mer_style_sheets
*
* @param array $style_sheet_URLs
* @return array
*/
public static function style_sheet_URLs(array $style_sheet_URLs = []): array
{
$style_sheet_URLs[] = EE_MER_URL . 'css' . DS . 'multi_event_registration.css';
return $style_sheet_URLs;
}
/**
* @param array $javascript_URLs
* @return array
*/
public static function javascript_URLs(array $javascript_URLs = []): array
{
$javascript_URLs[] = EE_MER_URL . 'scripts' . DS . 'multi_event_registration.js';
return $javascript_URLs;
}
/**
* Load the scripts and css
*
* @return void
*/
public static function enqueue_styles_and_scripts()
{
/** @var CurrentPage $current_page */
$current_page = LoaderFactory::getLoader()->getShared(CurrentPage::class);
// only load on our pages plz
if ($current_page->isEspressoPage()) {
// styles
wp_register_style(
'espresso_multi_event_registration',
apply_filters(
'FHEE__EED_Multi_Event_Registration__enqueue_scripts__event_cart_css',
EE_MER_URL . 'css' . DS . 'multi_event_registration.css'
),
['espresso_default'],
EE_MER_VERSION
);
wp_enqueue_style('espresso_multi_event_registration');
// scripts
wp_register_script(
'espresso_multi_event_registration',
EE_MER_URL . 'scripts' . DS . 'multi_event_registration.js',
['espresso_core'],
EE_MER_VERSION,
true
);
wp_enqueue_script('espresso_multi_event_registration');
}
}
/**
* run - initial module setup
*
* @param WP $WP
* @return void
* @throws EE_Error
* @throws ReflectionException
*/
public function run($WP)
{
EED_Multi_Event_Registration::instance()->set_config();
}
// *******************************************************************************************************
// ******************************************* EVENT LISTING *******************************************
// *******************************************************************************************************
/**
* @throws EE_Error
* @throws ReflectionException
*/
public static function no_tickets_but_display_register_now_button($display = false, $event = null): bool
{
// verify event
if (! $event instanceof EE_Event) {
if (WP_DEBUG) {
EE_Error::add_error(
esc_html__('An invalid event object was received.', 'event_espresso'),
__FILE__,
__FUNCTION__,
__LINE__
);
}
return false;
}
return EED_Multi_Event_Registration::has_tickets_in_cart($event);
}
/**
* changes the default "Register Now" text based on event's inclusion in the cart
*
* @param string $btn_text
* @param EE_Event|null $event
* @param bool $tickets_in_cart
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
public static function filter_ticket_selector_submit_button(
string $btn_text = '',
EE_Event $event = null,
bool $tickets_in_cart = false
): string {
// verify event
if (! $event instanceof EE_Event && ! $tickets_in_cart) {
if (WP_DEBUG) {
EE_Error::add_error(
esc_html__('An invalid event object was received.', 'event_espresso'),
__FILE__,
__FUNCTION__,
__LINE__
);
}
return $btn_text;
}
if (
$event instanceof EE_Event
&& $event->external_url()
&& $event->external_url() !== get_permalink()
) {
return $btn_text;
} elseif ($tickets_in_cart || EED_Multi_Event_Registration::has_tickets_in_cart($event)) {
$btn_text = sprintf(
esc_html__('View %s', 'event_espresso'),
EED_Multi_Event_Registration::$event_cart_name
);
} else {
$btn_text = sprintf(
esc_html__('Add to %s', 'event_espresso'),
EED_Multi_Event_Registration::$event_cart_name
);
}
return $btn_text;
}
/**
* @param EE_Event $event
* @return array
* @throws EE_Error
* @throws ReflectionException
*/
protected static function get_all_event_tickets(EE_Event $event): array
{
$tickets = [];
// get active events
foreach ($event->datetimes_ordered(false) as $datetime) {
$datetime_tickets = $datetime->ticket_types_available_for_purchase();
foreach ($datetime_tickets as $datetime_ticket) {
if ($datetime_ticket instanceof EE_Ticket) {
$tickets[ $datetime_ticket->ID() ] = $datetime_ticket;
}
}
}
return $tickets;
}
/**
* @param EE_Event|null $event
* @return bool
* @throws EE_Error
* @throws ReflectionException
*/
protected static function has_tickets_in_cart(EE_Event $event = null): bool
{
if (! $event instanceof EE_Event) {
return false;
}
EED_Multi_Event_Registration::load_classes();
$event_tickets = EED_Multi_Event_Registration::get_all_event_tickets($event);
$tickets_in_cart = EE_Registry::instance()->CART->get_tickets();
foreach ($tickets_in_cart as $ticket_in_cart) {
if (
$ticket_in_cart instanceof EE_Line_Item
&& $ticket_in_cart->OBJ_type() === 'Ticket'
&& isset($event_tickets[ $ticket_in_cart->OBJ_ID() ])
) {
return true;
}
}
return false;
}
/**
* adds a hidden input to the Ticket Selector form
*
* @param string $html
* @param EE_Event|null $event
* @param bool $tickets_in_cart
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
public static function filter_ticket_selector_form_html(
string $html = '',
EE_Event $event = null,
bool $tickets_in_cart = false
): string {
if ($tickets_in_cart || EED_Multi_Event_Registration::has_tickets_in_cart($event)) {
$html .= '<input type="hidden" value="view" name="event_cart">';
}
return $html;
}
/**
* @param string $button_url
* @param EE_Event $event
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
public static function filter_ticket_selector_button_url(string $button_url, EE_Event $event): string
{
return EED_Multi_Event_Registration::_filter_ticket_selector_button($event)
? add_query_arg(['event_cart' => 'view'], EE_EVENT_QUEUE_BASE_URL)
: $button_url;
}
/**
* If the Ticket Selector should be displayed
* and the event is NOT sold out
* (unless it's sold out because we just added the last tickets to the cart)
* then change the submit button text to "View Event Cart"
*
* @param EE_Event $event
* @return boolean
* @throws EE_Error
* @throws ReflectionException
*/
protected static function _filter_ticket_selector_button(EE_Event $event): bool
{
return $event->display_ticket_selector()
&& EE_Config::instance()->template_settings->EED_Events_Archive instanceof EE_Events_Archive_Config
&& EE_Config::instance()->template_settings->EED_Events_Archive->display_ticket_selector
&& ! (
$event->is_sold_out()
&& ! EED_Multi_Event_Registration::has_tickets_in_cart($event)
);
}
/**
* @param string $button_txt
* @param EE_Event $event
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
public static function filter_ticket_selector_button_txt(string $button_txt, EE_Event $event): string
{
return EED_Multi_Event_Registration::_filter_ticket_selector_button($event)
? sprintf(esc_html__('View %s', 'event_espresso'), EED_Multi_Event_Registration::$event_cart_name)
: $button_txt;
}
/**
* changes event list button URL based on tickets in cart
*
* @return string
*/
public static function filter_ticket_selector_redirect_url(): string
{
if (
apply_filters(
'FHEE__EED_Multi_Event_Registration__filter_ticket_selector_redirect_url__redirect_to_cart',
false
)
) {
return add_query_arg(['event_cart' => 'view'], EE_EVENT_QUEUE_BASE_URL);
}
$request = self::getRequest();
$referer_uri = $request->getServerParam('HTTP_REFERER', '');
if ($referer_uri === EE_EVENTS_LIST_URL) {
return EE_EVENTS_LIST_URL;
}
$request_uri = $request->getServerParam('REQUEST_URI', '');
if (basename($request_uri) != basename(EE_EVENTS_LIST_URL)) {
return EE_EVENTS_LIST_URL . basename($request_uri);
}
return EE_EVENTS_LIST_URL;
}
/**
* creates button for going back to the Event Cart
*
* @param string $html
* @param EE_SPCO_Reg_Step $reg_step
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
public static function return_to_event_cart_button(string $html, EE_SPCO_Reg_Step $reg_step): string
{
// returning to SPCO ?
if ($reg_step->checkout->revisit) {
// no return to cart button for you!
return $html;
}
// and if a payment has already been made and this isn't a revisit
if (
$reg_step->checkout->transaction instanceof EE_Transaction
&& self::getRequest()->getRequestParam('e_reg_url_link', '') === ''
) {
$last_payment = $reg_step->checkout->transaction->last_payment();
if (
$last_payment instanceof EE_Payment
&& $last_payment->status() !== EEM_Payment::status_id_failed
&& $reg_step->checkout->transaction->paid() > 0
) {
return $html;
}
}
return '<a class="return-to-event-cart-mini-cart-lnk mini-cart-view-cart-lnk view-cart-lnk mini-cart-button hide-me-after-successful-payment-js button" href = "'
. add_query_arg(['event_cart' => 'view'], EE_EVENT_QUEUE_BASE_URL)
. '" ><span class="dashicons dashicons-cart" ></span >'
. apply_filters(
'FHEE__EED_Multi_Event_Registration__view_event_cart_btn_txt',
sprintf(
esc_html__(
'return to %s',
'event_espresso'
),
EED_Multi_Event_Registration::$event_cart_name
)
)
. '</a >'
. $html;
}
/**
* @return void
* @throws EE_Error
* @throws ReflectionException
*/
public static function redirect_to_event_cart()
{
// grab event ID from Ticket Selector
$EVT_ID = self::getRequest()->getRequestParam('tkt-slctr-event-id', 0, DataType::INT);
if ($EVT_ID) {
// grab ticket quantity array
$ticket_quantities = self::getRequest()->getRequestParam(
'tkt-slctr-qty-' . $EVT_ID,
[],
DataType::INT,
true
);
$ticket_quantities = is_array($ticket_quantities) ? $ticket_quantities : [$ticket_quantities];
foreach ($ticket_quantities as $ticket_quantity) {
// if ANY qty was set, then don't redirect
$ticket_quantity = absint($ticket_quantity);
if ($ticket_quantity == 1) {
$event = EEM_Event::instance()->get_one_by_ID($EVT_ID);
if ($event instanceof EE_Event) {
if (
$event->additional_limit() == 1
&& EED_Multi_Event_Registration::instance()
->has_tickets_in_cart($event)
) {
continue;
} else {
return;
}
}
} elseif ($ticket_quantity > 1) {
return;
}
}
}
$redirect_url = add_query_arg(['event_cart' => 'view'], EE_EVENT_QUEUE_BASE_URL);
if (self::getRequest()->getRequestParam('event_cart') === 'view') {
if (self::getRequest()->isFrontAjax()) {
// just send the ajax
echo json_encode(
array_merge(
EE_Error::get_notices(false),
['redirect_url' => $redirect_url]
)
);
} else {
wp_safe_redirect($redirect_url);
}
exit();
}
}
/**
* @return void
* @throws EE_Error
* @throws ReflectionException
*/
public static function process_ticket_selections()
{
// echo "\n\n " . __LINE__ . ") " . __METHOD__ . "() <br />";
$response = ['tickets_added' => false];
if (EED_Ticket_Selector::instance()->process_ticket_selections()) {
$EVT_ID = self::getRequest()->getRequestParam('tkt-slctr-event-id', 0, DataType::INT);
// radio buttons send ticket info as a string like: "TKT_ID-QTY"
$ticket_string = self::getRequest()->getRequestParam('tkt-slctr-qty-' . $EVT_ID);
if ($ticket_string) {
$tickets = explode('-', $ticket_string);
array_shift($tickets);
} else {
// ticket qty info is an array
$tickets = self::getRequest()->getRequestParam(
'tkt-slctr-qty-' . $EVT_ID,
[],
DataType::INT,
true
);
}
$ticket_count = 0;
foreach ($tickets as $quantity) {
$ticket_count += $quantity;
}
$response = [
'tickets_added' => true,
'ticket_count' => $ticket_count,
'btn_id' => "#ticket-selector-submit-$EVT_ID-btn",
'btn_txt' => EED_Multi_Event_Registration::filter_ticket_selector_submit_button('', null, true),
'form_html' => EED_Multi_Event_Registration::filter_ticket_selector_form_html('', null, true),
'mini_cart' => EED_Multi_Event_Registration::get_mini_cart(),
'cart_results' => EED_Multi_Event_Registration::get_cart_results($ticket_count),
];
}
// $notices = EE_Error::get_notices( false );
// echo "\n notices: ";
// var_dump( $notices );
// just send the ajax
echo json_encode(
array_merge(
EE_Error::get_notices(false),
$response
)
);
// to be... or...
die();
}
/**
* @param int $ticket_count
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
public static function get_cart_results(int $ticket_count = 0): string
{
// total tickets in cart
$total_tickets = EE_Registry::instance()->CART->all_ticket_quantity_count();
// what page the user is currently on
$referer_uri = isset($_SERVER['HTTP_REFERER']) ? basename($_SERVER['HTTP_REFERER']) : '';
$term_exists = term_exists($referer_uri, 'espresso_event_categories');
if (isset($term_exists['term_id'])) {
$term_id = intval($term_exists['term_id']);
$return_url = get_term_link($term_id, 'espresso_event_categories');
$close_modal = ' close-modal-js';
if ($return_url != wp_get_referer() || is_wp_error($return_url)) {
$return_url = EE_EVENTS_LIST_URL;
$close_modal = '';
}
} elseif ($referer_uri == basename(EE_EVENTS_LIST_URL)) {
$return_url = EE_EVENTS_LIST_URL;
$close_modal = ' close-modal-js';
} else {
$return_url = EE_EVENTS_LIST_URL;
$close_modal = '';
}
$template_args = [
'results' => apply_filters(
'FHEE__EED_Multi_Event_Registration__get_cart_results_results_message',
sprintf(
esc_html(
_n(
'1 item was successfully added for this event.',
'%1$s items were successfully added for this event.',
$ticket_count,
'event_espresso'
)
),
$ticket_count
),
$ticket_count
),
'current_cart' => apply_filters(
'FHEE__EED_Multi_Event_Registration__get_cart_results_current_cart_message',
sprintf(
esc_html(
_n(
'There is currently 1 item in the %2$s.',
'There are currently %1$d items in the %2$s.',
$total_tickets,
'event_espresso'
)
),
$total_tickets,
EED_Multi_Event_Registration::event_cart_name()
),
$total_tickets
),
'event_cart_name' => EED_Multi_Event_Registration::event_cart_name(),
'return_url' => $return_url,
'register_url' => EE_EVENT_QUEUE_BASE_URL,
'view_event_cart_url' => add_query_arg(['event_cart' => 'view'], EE_EVENT_QUEUE_BASE_URL),
'close_modal' => $close_modal,
'btn_class' => apply_filters(
'FHEE__EED_Multi_Event_Registration__event_cart_template__btn_class',
''
),
'additional_info' => apply_filters(
'FHEE__EED_Multi_Event_Registration__event_cart_template__additional_info',
''
),
];
return EEH_Template::display_template(
EE_MER_PATH . 'templates' . DS . 'cart_results_modal_dialog.template.php',
$template_args,
true
);
}
/**
* @return void
*/
public static function cart_results_modal_div()
{
echo '<div id="cart-results-modal-wrap-dv" style="display: none;"></div>';
}
/**
* @return string
* @throws EE_Error
*/
public static function get_mini_cart(): string
{
global $wp_widget_factory;
$mini_cart = $wp_widget_factory->widgets['EEW_Mini_Cart'];
if ($mini_cart instanceof EEW_Mini_Cart) {
$options = get_option($mini_cart->option_name);
if (isset($options[ $mini_cart->number ], $options[ $mini_cart->number ]['template'])) {
$template = $options[ $mini_cart->number ]['template'];
} else {
$template = '';
}
return $mini_cart->get_mini_cart($template);
}
return '';
}
// *******************************************************************************************************
// ******************************************** EVENT QUEUE ********************************************
// *******************************************************************************************************
/**
* basically retrieves the URL for the event cart, and instructs browser to redirect
*
* @return void
*/
public static function ajax_view_event_cart()
{
// just send the ajax
echo json_encode(
array_merge(
EE_Error::get_notices(false),
['redirect_url' => add_query_arg(['event_cart' => 'view'], EE_EVENT_QUEUE_BASE_URL)]
)
);
exit();
}
/**
* load and display Event Cart contents prior to completing registration
*
* @return void
* @throws EE_Error
* @throws ReflectionException
*/
public function view_event_cart()
{
$this->init();
// load classes
EED_Multi_Event_Registration::load_classes();
EE_Registry::instance()->load_helper('Template');
// EE_Registry::instance()->CART->recalculate_all_cart_totals();
$grand_total = EE_Registry::instance()->CART->get_grand_total();
$grand_total->recalculate_total_including_taxes();