-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEED_WP_Users_Ticket_Selector.module.php
640 lines (602 loc) · 24.6 KB
/
EED_WP_Users_Ticket_Selector.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
<?php
use EventEspresso\core\domain\entities\contexts\Context;
use EventEspresso\core\domain\services\factories\EmailAddressFactory;
use EventEspresso\core\domain\services\validation\email\EmailValidationException;
use EventEspresso\core\domain\values\EmailAddress;
use EventEspresso\core\domain\values\Url;
use EventEspresso\core\exceptions\InvalidDataTypeException;
use EventEspresso\core\exceptions\InvalidInterfaceException;
use EventEspresso\core\exceptions\UnexpectedEntityException;
use EventEspresso\core\libraries\form_sections\form_handlers\FormHandler;
use EventEspresso\core\services\loaders\LoaderFactory;
use EventEspresso\WaitList\domain\services\forms\WaitListForm;
use EventEspresso\WaitList\domain\services\forms\WaitListFormHandler;
use EventEspresso\WpUser\domain\entities\exceptions\WpUserLogInRequiredException;
use EventEspresso\WpUser\domain\services\users\WpUserEmailVerification;
/**
* EED_WP_Users_Ticket_Selector module. Takes care of WP Users integration with ticket selector.
*
* @since 1.0.0
* @package EE WP Users
* @subpackage modules
* @author Darren Ethier
*/
class EED_WP_Users_Ticket_Selector extends EED_Module
{
public const META_KEY_LOGIN_REQUIRED_NOTICE = 'login_required_notice';
public static function set_hooks()
{
add_filter(
'FHEE__ticket_selector_chart_template__do_ticket_inside_row',
['EED_WP_Users_Ticket_Selector', 'maybe_restrict_ticket_option_by_cap'],
10,
9
);
// don't display Wait List form if login is required and current user isn't
add_filter(
'FHEE__EventEspresso_WaitList_domain_services_forms_WaitListForm__waitListFormOptions__form_options',
['EED_WP_Users_Ticket_Selector', 'waitListFormNoticeSubsections'],
10,
5
);
// maybe display WP User related notices on the wait list form
add_filter(
'FHEE__EventEspresso_WaitList_domain_services_event_WaitListMonitor__getWaitListFormForEvent__redirect_params',
['EED_WP_Users_Ticket_Selector', 'displayWaitListFormUserNotices'],
10,
2
);
// hook into wait list form submission to check for users
add_filter(
'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data',
['EED_WP_Users_Ticket_Selector', 'verifyWaitListUserAccess'],
10,
2
);
// convert login required exceptions into user displayable notices
add_filter(
'FHEE__EventEspresso_WaitList_domain_services_event_WaitListMonitor__processWaitListFormForEvent__redirect_params',
['EED_WP_Users_Ticket_Selector', 'catchWaitListWpUserLogInRequiredException'],
10,
3
);
add_filter(
'FHEE__EventEspresso_WaitList_domain_services_forms__WaitListFormHandler__generate__tickets',
['EED_WP_Users_Ticket_Selector', 'checkWaitListTicketCaps'],
10,
3
);
add_filter(
'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data',
['EED_WP_Users_Ticket_Selector', 'checkSubmittedWaitListTicketCaps'],
11,
2
);
}
public static function set_hooks_admin()
{
}
public static function enqueue_scripts_styles()
{
}
public function run($WP)
{
}
/**
* @return WpUserEmailVerification
* @throws InvalidArgumentException
* @throws InvalidInterfaceException
* @throws InvalidDataTypeException
*/
protected static function getWpUserEmailVerification(): WpUserEmailVerification
{
return LoaderFactory::getLoader()->getShared(
'EventEspresso\WpUser\domain\services\users\WpUserEmailVerification'
);
}
/**
* Callback for FHEE__ticket_selector_chart_template__do_ticket_inside_row filter.
* We use this to possibly replace the generated row for the current ticket being displayed in the
* ticket selector if the ticket has a required cap and the viewer does not have access to that ticket
* option.
*
* @param bool|string $return_value Either false, which means we're not doing anything
* and let the ticket selector continue on its merry way,
* or a string if we're replacing what gets generated.
* @param EE_Ticket $tkt
* @param int $max Max tickets purchasable
* @param int $min Min tickets purchasable
* @param bool|string $required_ticket_sold_out Either false for tickets not sold out, or date.
* @param float|string $ticket_price Ticket price
* @param bool $ticket_bundle Is this a ticket bundle?
* @param string $tkt_status The status for the ticket.
* @param string $status_class The status class for the ticket.
* @return bool|string @see $return value.
* @throws EE_Error
* @throws InvalidArgumentException
* @throws InvalidDataTypeException
* @throws InvalidInterfaceException
* @throws ReflectionException
*/
public static function maybe_restrict_ticket_option_by_cap(
$return_value,
EE_Ticket $tkt,
int $max,
int $min,
$required_ticket_sold_out,
$ticket_price,
bool $ticket_bundle,
string $tkt_status,
string $status_class
) {
if (EED_WP_Users_Ticket_Selector::ticketAvailableToUser($tkt)) {
return false;
}
// made it here? That means user does not have access to this ticket,
// so let's return a filterable message for them.
$ticket_price = empty($ticket_price)
? ''
: ' (' . EEH_Template::format_currency($ticket_price) . ')';
$full_html_content = '<td class="tckt-slctr-tbl-td-name" colspan="3">';
$inner_message = EED_WP_Users_Ticket_Selector::getMembersOnlyTicketMessage(
$tkt,
$ticket_price,
$tkt_status
);
$full_html_content .= $inner_message . '</td>';
return apply_filters(
'FHEE__EED_WP_Users_Ticket_Selector__maybe_restrict_ticket_option_by_cap__no_access_msg_html',
$full_html_content,
$inner_message,
$tkt,
$ticket_price,
$tkt_status
);
}
/**
* @param EE_Ticket $ticket
* @return bool
* @throws EE_Error
* @throws InvalidArgumentException
* @throws InvalidDataTypeException
* @throws InvalidInterfaceException
* @throws ReflectionException
*/
public static function ticketAvailableToUser(EE_Ticket $ticket): bool
{
// don't check caps if adding registrants via the admin
if (! EE_FRONT_AJAX && is_admin()) {
return true;
}
// check if any caps are required to access this ticket
$cap_required = $ticket->get_extra_meta('ee_ticket_cap_required', true);
if (empty($cap_required)) {
// no cap required so user has access by default
return true;
}
// return true if user is logged in has the correct caps to access this ticket,
// otherwise return false because they are not logged in or don't have the required cap
return is_user_logged_in()
&& EE_Registry::instance()->CAP->current_user_can(
$cap_required,
'wp_user_ticket_selector_check'
);
}
/**
* @param EE_Ticket $ticket
* @param float|string $ticket_price
* @param string $ticket_status
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
private static function getMembersOnlyTicketMessage(EE_Ticket $ticket, $ticket_price, string $ticket_status): string
{
return (string) apply_filters(
'FHEE__EED_WP_Users_Ticket_Selector__maybe_restrict_ticket_option_by_cap__no_access_msg',
sprintf(
esc_html__(
'The %1$s%2$s%3$s%4$s is available to members only. %5$s',
'event_espresso'
),
'<strong>',
$ticket->name(),
$ticket_price,
'</strong>',
$ticket_status
),
$ticket,
$ticket_price,
$ticket_status
);
}
/**
* @param array $subsections
* @param EE_Event $event
* @param array $tickets
* @param int $wait_list_spaces_left
* @param WaitListForm|null $form
* @return array
* @throws EE_Error
* @throws ReflectionException
*/
public static function waitListFormNoticeSubsections(
array $subsections,
EE_Event $event,
array $tickets,
int $wait_list_spaces_left = 10,
?WaitListForm $form = null
): array {
// login not required if adding registrants via the admin
if (! EE_FRONT_AJAX && is_admin()) {
return $subsections;
}
// auto-fill form if known user is already logged in, or display login notice if they are not
return is_user_logged_in()
? EED_WP_Users_Ticket_Selector::autoFillFormWithUserInfo($subsections)
: EED_WP_Users_Ticket_Selector::loginRequiredWaitListFormNotice($subsections, $event);
}
/**
* Retrieves details for the currently logged in user
* and uses them to fill out the Wait List Sign Up form
*
* @param array $subsections
* @return array
* @throws EE_Error
* @throws InvalidArgumentException
* @throws InvalidDataTypeException
* @throws InvalidInterfaceException
*/
public static function autoFillFormWithUserInfo(array $subsections): array
{
$user = get_userdata(get_current_user_id());
if (! $user instanceof WP_User) {
return $subsections;
}
if (
isset($subsections['subsections']['hidden_inputs'])
&& $subsections['subsections']['hidden_inputs'] instanceof EE_Form_Section_Proper
) {
$sign_up_form = $subsections['subsections']['hidden_inputs'];
$inputs = $sign_up_form->subsections(false);
if (isset($inputs['registrant_name']) && $inputs['registrant_name'] instanceof EE_Text_Input) {
$inputs['registrant_name']->set_default($user->display_name);
}
if (isset($inputs['registrant_email']) && $inputs['registrant_email'] instanceof EE_Email_Input) {
$inputs['registrant_email']->set_default($user->user_email);
}
}
return $subsections;
}
/**
* Replaces visible elements of the Wait List Sign Up form
* (as well as some hidden elements) with a "Login Required" notice
*
* @param array $subsections
* @param EE_Event $event
* @return array
* @throws EE_Error
* @throws InvalidArgumentException
* @throws InvalidDataTypeException
* @throws InvalidInterfaceException
* @throws ReflectionException
*/
public static function loginRequiredWaitListFormNotice(array $subsections, EE_Event $event): array
{
// or event does not require login
$user_integration_settings = $event->get_post_meta('ee_wpuser_integration_settings', true);
if (! isset($user_integration_settings['force_login']) || ! $user_integration_settings['force_login']) {
return $subsections;
}
if (
isset($subsections['subsections']['hidden_inputs'])
&& $subsections['subsections']['hidden_inputs'] instanceof EE_Form_Section_Proper
) {
/** @var EE_Form_Section_Proper $sign_up_form */
$sign_up_form = $subsections['subsections']['hidden_inputs'];
$sign_up_form->exclude(
[
'wait_list_form_notice',
'registrant_name',
'registrant_email',
'ticket',
'quantity',
'lb1',
'submit',
]
);
$sign_up_form->add_subsections(
[
'login_required' => new EE_Form_Section_HTML(
EED_WP_Users_Ticket_Selector::userLoginNoticeHeading(
esc_html__('Login Required', 'event_espresso'),
new Context(
'wait-list-sign-up-form-login-required',
'Wait List Sign Up form for Event with "Force Login" activated - login required'
),
$event
)
. sprintf(
esc_html__(
'%1$sLogin is required in order to sign up for this wait list.%3$s',
'event_espresso'
),
'<p>',
'<br />',
'</p>'
)
. EED_WP_Users_SPCO::loginAndRegisterButtonsHtml(
new Url(get_permalink($event->ID()))
)
),
],
'clear_submit'
);
}
return $subsections;
}
/**
* maybe display WP User related notices on the wait list form
*
* @param string $wait_list_form
* @param EE_Event $event
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
public static function displayWaitListFormUserNotices(string $wait_list_form, EE_Event $event): string
{
if (isset($_REQUEST[ EED_WP_Users_Ticket_Selector::META_KEY_LOGIN_REQUIRED_NOTICE ])) {
$login_notice_id = sanitize_text_field(
$_REQUEST[ EED_WP_Users_Ticket_Selector::META_KEY_LOGIN_REQUIRED_NOTICE ]
);
$login_notice = $event->get_extra_meta($login_notice_id, true);
if ($login_notice) {
$login_notice = EEH_HTML::div(
$login_notice,
'ee-login-notice-id-' . $event->ID(),
'ee-login-notice ee-attention'
);
$wait_list_form = $login_notice . $wait_list_form;
$event->delete_extra_meta($login_notice_id);
}
}
return $wait_list_form;
}
/**
* hook into wait list form submission to check for registered users
*
* @param array $form_data
* @param FormHandler $form_handler
* @return array
* @throws WpUserLogInRequiredException
* @throws EmailValidationException
* @throws RuntimeException
* @throws EE_Error
* @throws InvalidArgumentException
* @throws InvalidDataTypeException
* @throws InvalidInterfaceException
* @throws UnexpectedEntityException
* @throws DomainException
* @throws ReflectionException
*/
public static function verifyWaitListUserAccess(array $form_data, FormHandler $form_handler): array
{
// only process the wait list form
if (! $form_handler instanceof WaitListFormHandler) {
return $form_data;
}
$login_notice = EED_WP_Users_Ticket_Selector::userLoginNotice(
EmailAddressFactory::create($form_data['hidden_inputs']['registrant_email']),
$form_handler->event()
);
if ($login_notice !== '') {
throw new WpUserLogInRequiredException($login_notice);
}
return $form_data;
}
/**
* @param string $heading
* @param Context $context
* @param EE_Event $event
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
private static function userLoginNoticeHeading(string $heading, Context $context, EE_Event $event): string
{
return EEH_HTML::h2(
apply_filters(
'FHEE__EED_WP_Users_Ticket_Selector__userLoginNoticeHeading__heading',
$heading,
$context,
$event
),
'ee-login-notice-h4-' . $event->ID(),
'ee-login-notice-h4 important-notice'
);
}
/**
* @param EmailAddress $wp_user_email_address
* @param EE_Event $event
* @return string
* @throws EmailValidationException
* @throws DomainException
* @throws EE_Error
* @throws InvalidArgumentException
* @throws InvalidDataTypeException
* @throws InvalidInterfaceException
* @throws ReflectionException
*/
private static function userLoginNotice(EmailAddress $wp_user_email_address, EE_Event $event): string
{
// LOGIN REQUIRED
$login_required_message = EED_WP_Users_Ticket_Selector::userLoginNoticeHeading(
esc_html__('Login Required', 'event_espresso'),
new Context(
WpUserEmailVerification::EMAIL_ADDRESS_REGISTERED_LOGIN_REQUIRED,
'Wait List Sign Up form submitted using existing User email - login required'
),
$event
);
$login_required_message .= sprintf(
esc_html__(
'%1$sYou have entered an email address that matches an existing user account in our system.%2$sIf this is your email address, please log in before continuing. Otherwise, register with a different email address.%3$s',
'event_espresso'
),
'<p>',
'<br />',
'</p>'
);
$login_required_message .= EED_WP_Users_SPCO::loginAndRegisterButtonsHtml(
new Url(get_permalink($event->ID()))
);
// USER MISMATCH
$user_mismatch_message = EED_WP_Users_Ticket_Selector::userLoginNoticeHeading(
esc_html__('Email Address Mismatch', 'event_espresso'),
new Context(
WpUserEmailVerification::EMAIL_ADDRESS_REGISTERED_USER_MISMATCH,
'Wait List Sign Up form was submitted using existing User email from other User'
),
$event
);
$user_mismatch_message .= sprintf(
esc_html__(
'%1$sYou have entered an email address that matches an existing user account in our system.%2$sYou can only join the Wait List using your own account or one that does not already exist.%2$sPlease use a different email address.%3$s',
'event_espresso'
),
'<p>',
'<br />',
'</p>'
);
$wp_user_email_verification = EED_WP_Users_Ticket_Selector::getWpUserEmailVerification();
return $wp_user_email_verification->getWpUserEmailVerificationNotice(
$wp_user_email_verification->verifyWpUserEmailAddress($wp_user_email_address),
$login_required_message,
$user_mismatch_message
);
}
/**
* convert login required exceptions into user displayable notices
*
* @param array $redirect_params
* @param Exception $exception
* @param EE_Event $event
* @return array
* @throws EE_Error
* @throws ReflectionException
*/
public static function catchWaitListWpUserLogInRequiredException(
array $redirect_params,
Exception $exception,
EE_Event $event
): array {
if ($exception instanceof WpUserLogInRequiredException) {
$login_notice_id = md5($event->ID() . $event->name() . time());
$event->add_extra_meta($login_notice_id, $exception->getMessage(), true);
$redirect_params[ EED_WP_Users_Ticket_Selector::META_KEY_LOGIN_REQUIRED_NOTICE ] = $login_notice_id;
}
return $redirect_params;
}
/**
* @param array $tickets
* @param EE_Ticket[] $active_tickets
* @param EE_Event $event
* @return array
* @throws EE_Error
* @throws InvalidArgumentException
* @throws InvalidDataTypeException
* @throws InvalidInterfaceException
* @throws ReflectionException
*/
public static function checkWaitListTicketCaps(array $tickets, array $active_tickets, EE_Event $event): array
{
foreach ($active_tickets as $active_ticket) {
if (! EED_WP_Users_Ticket_Selector::ticketAvailableToUser($active_ticket)) {
unset($tickets[ $active_ticket->ID() ]);
if (is_user_logged_in()) {
$option_id = 'members_only';
$login_notice = '';
} else {
$option_id = 'login_required';
$login_notice = esc_html__('Please login.', 'event_espresso');
}
$tickets[ $option_id ] = esc_html__('Members Only Options Available. ', 'event_espresso');
$tickets[ $option_id ] .= $login_notice;
}
}
return $tickets;
}
/**
* Checks valid data when the Wait List Sign Up form has been submitted,
* and throws an error if a member only ticket option has been selected
*
* @param array $valid_form_data
* @param FormHandler $form_handler
* @return array
* @throws InvalidInterfaceException
* @throws InvalidDataTypeException
* @throws WpUserLogInRequiredException
* @throws EE_Error
* @throws InvalidArgumentException
* @throws ReflectionException
*/
public static function checkSubmittedWaitListTicketCaps(array $valid_form_data, FormHandler $form_handler): array
{
if (! $form_handler instanceof WaitListFormHandler) {
return $valid_form_data;
}
if (isset($valid_form_data['hidden_inputs']['ticket'])) {
if ($valid_form_data['hidden_inputs']['ticket'] === 'members_only') {
throw new WpUserLogInRequiredException(
EED_WP_Users_Ticket_Selector::userLoginNoticeHeading(
esc_html__('Members Only', 'event_espresso'),
new Context(
'wait-list-ticket-caps-members-only',
'Wait List Sign Up form was submitted using members only ticket selection'
),
$form_handler->event()
)
. apply_filters(
'FHEE__EED_WP_Users_Ticket_Selector__checkSubmittedWaitListTicketCaps__members_only_notice',
sprintf(
esc_html__(
'%1$sWe\'re sorry, but your selected option is for specific member\'s only, and is not available for you at this time.%2$s',
'event_espresso'
),
'<p>',
'</p>'
)
)
);
}
if ($valid_form_data['hidden_inputs']['ticket'] === 'login_required') {
throw new WpUserLogInRequiredException(
EED_WP_Users_Ticket_Selector::userLoginNoticeHeading(
esc_html__('Login Required', 'event_espresso'),
new Context(
'wait-list-ticket-caps-login-required',
'Wait List Sign Up form was submitted using members only ticket selection - login required'
),
$form_handler->event()
)
. apply_filters(
'FHEE__EED_WP_Users_Ticket_Selector__checkSubmittedWaitListTicketCaps__login_required_notice',
sprintf(
esc_html__(
'%1$sWe\'re sorry, but your selected option is for member\'s only. Please login first to see if it is available to you.%2$s',
'event_espresso'
),
'<p>',
'</p>'
)
)
. EED_WP_Users_SPCO::loginAndRegisterButtonsHtml(
new Url(get_permalink($form_handler->event()->ID()))
)
);
}
}
return $valid_form_data;
}
}