-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEED_WP_Users_Admin.module.php
1191 lines (1066 loc) · 46.5 KB
/
EED_WP_Users_Admin.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\services\assets\EspressoLegacyAdminAssetManager;
/**
*
* EED_WP_Users_Adminmodule. Takes care of WP Users integration with EE admin.
*
* @since 1.0.0
*
* @package EE WP Users
* @subpackage modules, admin
* @author Darren Ethier
*
* ------------------------------------------------------------------------
*/
class EED_WP_Users_Admin extends EED_Module
{
public static function set_hooks()
{
}
public static function set_hooks_admin()
{
add_action('admin_enqueue_scripts', ['EED_WP_Users_Admin', 'admin_enqueue_scripts_styles']);
// hook into EE contact publish metabox.
add_action('post_submitbox_misc_actions', ['EED_WP_Users_Admin', 'add_link_to_wp_user_account']);
// hook into wp users
add_action('edit_user_profile', ['EED_WP_Users_Admin', 'add_link_to_ee_contact_details']);
add_action('show_user_profile', ['EED_WP_Users_Admin', 'add_link_to_ee_contact_details']);
add_action('edit_user_profile', ['EED_WP_Users_Admin', 'view_registrations_for_contact']);
add_action('show_user_profile', ['EED_WP_Users_Admin', 'view_registrations_for_contact']);
add_action('profile_update', ['EED_WP_Users_Admin', 'sync_with_contact'], 10, 2);
add_action('user_register', ['EED_WP_Users_Admin', 'sync_with_contact']);
// hook into attendee saves
add_filter(
'FHEE__Registrations_Admin_Page__insert_update_cpt_item__attendee_update',
['EED_WP_Users_Admin', 'add_sync_with_wp_users_callback'],
10
);
// hook into registration_form_admin_page routes and config.
add_filter(
'FHEE__Extend_Registration_Form_Admin_Page__page_setup__page_routes',
['EED_WP_Users_Admin', 'add_wp_user_default_settings_route'],
10,
2
);
add_filter(
'FHEE__Extend_Registration_Form_Admin_Page__page_setup__page_config',
['EED_WP_Users_Admin', 'add_wp_user_default_settings_config'],
10,
2
);
add_filter(
'FHEE__Extend_Events_Admin_Page__page_setup__page_config',
['EED_WP_Users_Admin', 'add_ticket_capability_help_tab'],
10,
2
);
add_filter(
'FHEE__EE_Admin_Page___publish_post_box__box_label',
['EED_WP_Users_Admin', 'modify_settings_publish_box_label'],
10,
3
);
// hooking into event editor
add_action('add_meta_boxes', ['EED_WP_Users_Admin', 'add_metaboxes']);
add_filter(
'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks',
['EED_WP_Users_Admin', 'set_callback_save_wp_user_event_setting'],
10,
2
);
add_filter(
'FHEE__EED_WP_Users_Admin__event_editor_metabox__wp_user_form_content',
['EED_WP_Users_Admin', 'set_capability_default_user_create_role_event_editor'],
10
);
add_action(
'AHEE__Extend_Events_Admin_Page___duplicate_event__after',
['EED_WP_Users_Admin', 'duplicate_user_settings_for_event'],
10,
2
);
add_action(
'AHEE__Extend_Events_Admin_Page___duplicate_event__duplicate_ticket__after',
['EED_WP_Users_Admin', 'set_capability_on_new_duplicated_ticket'],
10,
2
);
// hook into ticket editor in event editor.
add_action(
'AHEE__event_tickets_datetime_ticket_row_template__advanced_details_end',
['EED_WP_Users_Admin', 'insert_ticket_meta_interface'],
10,
2
);
add_action(
'AHEE__espresso_events_Pricing_Hooks___update_tkts_new_ticket',
['EED_WP_Users_Admin', 'update_capability_on_ticket'],
10,
4
);
add_action(
'AHEE__espresso_events_Pricing_Hooks___update_tkts_update_ticket',
['EED_WP_Users_Admin', 'update_capability_on_ticket'],
10,
4
);
add_action(
'AHEE__espresso_events_Pricing_Hooks___update_tkts_new_default_ticket',
['EED_WP_Users_Admin', 'update_capability_on_ticket'],
10,
4
);
// hook into model deletes that may affect relations set on WP_User.
add_action(
'AHEE__EE_Base_Class__delete_permanently__before',
['EED_WP_Users_Admin', 'remove_relations_on_delete']
);
}
public static function admin_enqueue_scripts_styles()
{
if (get_current_screen()->base == 'profile' || get_current_screen()->base == 'user-edit') {
wp_register_style(
EspressoLegacyAdminAssetManager::CSS_HANDLE_EE_ADMIN,
EE_ADMIN_URL . 'assets/ee-admin-page.css',
['espresso_admin_base'],
EVENT_ESPRESSO_VERSION
);
wp_register_style(
'espresso_att',
REG_ASSETS_URL . 'espresso_attendees_admin.css',
['ee-admin-css'],
EVENT_ESPRESSO_VERSION
);
wp_enqueue_style('espresso_att');
}
}
public function run($WP)
{
}
/**
* Register metaboxes for event editor.
*/
public static function add_metaboxes()
{
$page = EE_Registry::instance()->REQ->get('page');
$route = EE_Registry::instance()->REQ->get('action');
// on event editor page?
if ($page == 'espresso_events' && ($route == 'edit' || $route == 'create_new')) {
add_meta_box(
'eea_wp_user_integration',
esc_html__('User Integration Settings', 'event_espresso'),
['EED_WP_Users_Admin', 'event_editor_metabox'],
null,
'side',
'default'
);
}
}
public static function add_sync_with_wp_users_callback($callbacks)
{
$callbacks[] = ['EED_WP_Users_Admin', 'sync_with_wp_user'];
return $callbacks;
}
/**
* Callback for post_submitbox_misc_actions that adds a link to the wp user
* edit page for the user attached to the EE_Attendee (if present).
*
* @since 1.0.0
*/
public static function add_link_to_wp_user_account()
{
global $post;
if (! $post instanceof WP_Post || $post->post_type != 'espresso_attendees') {
return;
}
// is there an attached wp_user for this attendee record?
$user_id = EE_WPUsers::get_attendee_user($post->ID);
if (empty($user_id)) {
return;
}
// let's get the WP_user and setup the link
$url = get_edit_user_link($user_id);
// if $url is empty, that means logged in user does not have access to view user details so we bail.
if (empty($url)) {
return;
}
// we HAVE url so let's assemble the item to display.
?>
<div class="misc-pub-section">
<span class="dashicons dashicons-wordpress"></span>
<a href="<?php echo $url; ?>"
title="<?php esc_html_e('Click to view WordPress user profile', 'event_espresso'); ?>"
><?php esc_html_e('WordPress User Profile', 'event_espresso'); ?></a>
</div>
<?php
}
/**
* callback for edit_user_profile that is used to display a table of all the registrations this
* user is connected with.
*
* @param WP_User $user
* @return string
* @throws DomainException
* @throws EE_Error
* @throws ReflectionException
*/
public static function view_registrations_for_contact($user)
{
if (! $user instanceof WP_User) {
return '';
}
// is there an attadched EE_Attendee?
$att_id = get_user_option('EE_Attendee_ID', $user->ID);
if (empty($att_id)) {
return ''; // bail, no attached attendee_id.
}
// grab contact
$contact = EEM_Attendee::instance()->get_one_by_ID($att_id);
// if no contact then bail
if (! $contact instanceof EE_Attendee) {
return '';
}
$template_args = [
'attendee' => $contact,
'registrations' => $contact->get_many_related('Registration'),
];
EEH_Template::display_template(
EE_WPUSERS_TEMPLATE_PATH . 'eea-wp-users-registrations-table.template.php',
$template_args
);
}
/**
* callback for edit_user_profile that is used to add link to the EE_Attendee
* details if there is one attached to the user.
*
* @param WP_User $user
*/
public static function add_link_to_ee_contact_details($user)
{
if (! $user instanceof WP_User) {
return;
}
// is there an attached EE_Attendee?
$att_id = get_user_option('EE_Attendee_ID', $user->ID);
if (empty($att_id)) {
return; // bail, no attached attendee_id.
}
// does logged in user have the capability to edit this attendee?
if (! EE_Registry::instance()->CAP->current_user_can('ee_edit_contacts', 'edit_attendee', $att_id)) {
return; // bail no access.
}
// url
$url = admin_url(
add_query_arg(
[
'page' => 'espresso_registrations',
'action' => 'edit_attendee',
'post' => $att_id,
],
'admin.php'
)
);
?>
<table class="ee-admin-two-column-layout form-table">
<tr class="ee-wpuser-integration-row">
<th></th>
<td>
<p>
<?php esc_html_e(
'When you save this user profile, the details will be synced with the attached Event Espresso Contact',
'event_espresso'
); ?>
</p>
<p>
<a class="button button-secondary"
href="<?php echo $url; ?>"
title="<?php esc_html_e('Click to go to Attendee Details', 'event_espresso'); ?>"
><?php esc_html_e('View Linked Contact', 'event_espresso'); ?></a>
</p>
</td>
</tr>
</table>
<?php
}
/**
* Callback for the 'profile_update' and 'user_register' hooks that enable syncing saved user data
* with an EE_Attendee record.
* This callback detects whether we're creating a user record or not.
* If creating:
* - Is there already an EE_Contact that matches the first name/last name/email address of
* the user record?
* - Yes -> attach it.
* - No -> create it.
* If updating:
* - Is there already an attached EE_Contact record on the user account?
* - Yes -> update it.
* - No -> do the same as when we create user.
*
* @param int $user_id The id of the user that was just created/updated.
* @param WP_User|null $old_user_data Object container user's data prior to update. If empty, then
* the user_register hook was fired.
* @return void
* @throws EE_Error
* @throws ReflectionException
* @since 1.0.0
*/
public static function sync_with_contact($user_id, $old_user_data = null)
{
$user = get_userdata($user_id);
// creating?
if (empty($old_user_data)) {
self::_connect_wp_user_with_contact($user);
return;
}
// if we make it here then we're updating an existing user
$att_id = get_user_option('EE_Attendee_ID', $user->ID);
if (empty($att_id)) {
self::_connect_wp_user_with_contact($user);
return;
} else {
// update the existing attendee attached to the wp_user!
$att = EE_Registry::instance()->load_model('Attendee')->get_one_by_ID($att_id);
if ($att instanceof EE_Attendee) {
$att->set_email($user->user_email);
$att->set_fname($user->first_name);
$att->set_lname($user->last_name);
$att->set('ATT_bio', $user->user_description);
$att->save();
}
}
return;
}
/**
* Callback for FHEE__Registrations_Admin_Page__insert_update_cpt_item__attendee_update
* filter. Used to sync the saved Attendee data with any attached wp_user.
* Note: currently this does NOT create a user.
*
* @param EE_Attendee $attendee
* @param array $request_data The request data from the save.
* @return void
* @throws EE_Error
* @throws ReflectionException
*/
public static function sync_with_wp_user(EE_Attendee $attendee, $request_data)
{
// is there a user for this attendee ID?
$user_id = EE_WPUsers::get_attendee_user($attendee->ID());
if (empty($user_id)) {
return;
}
// made it here, so let's sync the main attendee details with the user account
// remove the existing action for updates so that we don't cause recursion.
remove_action('profile_update', ['EED_WP_Users_Admin', 'sync_with_contact']);
wp_update_user(
[
'ID' => $user_id,
'first_name' => $attendee->fname(),
'last_name' => $attendee->lname(),
'user_email' => $attendee->email(),
'description' => $attendee->get('ATT_bio'),
]
);
return;
}
/**
* This takes an incoming wp_user object and either connects it with an existing contact that
* matches its details, or creates a new attendee and attaches.
*
* @param WP_User $user
* @return EE_Attendee
* @throws EE_Error
* @throws ReflectionException
* @since 1.0.0
*/
protected static function _connect_wp_user_with_contact(WP_User $user)
{
// no attached EE_Attendee. Is there an existing attendee that matches this user's details?
$att = self::_find_existing_attendee_from_wpuser($user);
if ($att instanceof EE_Attendee && ! EE_WPUsers::get_attendee_user($att->ID())) {
update_user_option($user->ID, 'EE_Attendee_ID', $att->ID());
}
return $att;
}
/**
* Using the given WP_User object, this method finds an EE_Attendee that matches email
* address, first name, last name and returns if it exists.
*
* @param WP_User $user
*
* @return EE_Attendee|bool false if EE_Attendee does not exist.
* @throws EE_Error
* @throws ReflectionException
*/
protected static function _find_existing_attendee_from_wpuser(WP_User $user)
{
$existing_attendee = EE_Registry::instance()->load_model('Attendee')->find_existing_attendee(
[
'ATT_fname' => $user->first_name,
'ATT_lname' => $user->last_name,
'ATT_email' => $user->user_email,
]
);
return $existing_attendee instanceof EE_Attendee ? $existing_attendee : false;
}
/**
* This creates an EE_Attendee record using data from the given user and attaches that
* EE_Attendee to the user.
*
* @param WP_User $user
* @return EE_Attendee
* @throws EE_Error
* @throws ReflectionException
* @since 1.0.0
*/
protected static function _create_attendee_and_attach_wpuser(WP_User $user)
{
$att = EE_Attendee::new_instance(
[
'ATT_fname' => $user->first_name,
'ATT_lname' => $user->last_name,
'ATT_email' => $user->user_email,
'ATT_bio' => $user->user_description,
]
);
$att->save();
// attach to user
update_user_option($user->ID, 'EE_Attendee_ID', $att->ID());
return $att;
}
/**
* callback for FHEE__Extend_Registration_Form_Admin_Page__page_setup__page_routes.
* Add additional routes for saving WP_User settings to the Registration Form admin page system
*
* @param array $page_routes current array of page routes.
* @param EE_Admin_Page $admin_page
*
* @return array
* @since 1.0.0
*
*/
public static function add_wp_user_default_settings_route($page_routes, EE_Admin_Page $admin_page)
{
$page_routes['wp_user_settings'] = [
'func' => ['EED_WP_Users_Admin', 'wp_user_settings'],
'args' => [$admin_page],
'capability' => 'manage_options',
];
$page_routes['update_wp_user_settings'] = [
'func' => ['EED_WP_Users_Admin', 'update_wp_user_settings'],
'args' => [$admin_page],
'capability' => 'manage_options',
'noheader' => true,
];
return $page_routes;
}
/**
* callback for the wp_user_settings route.
*
* @param EE_Admin_Page $admin_page
*
* @return string html for displaying wp_user_settings.
* @throws EE_Error
* @throws ReflectionException
*/
public static function wp_user_settings(EE_Admin_Page $admin_page)
{
$template_args['admin_page_content'] = self::_wp_user_settings_form()->get_html_and_js();
$admin_page->set_add_edit_form_tags('update_wp_user_settings');
$admin_page->set_publish_post_box_vars('', 0, '', '', false);
$admin_page->set_template_args($template_args);
$admin_page->display_admin_page_with_sidebar();
}
/**
* This outputs the settings form for WP_User_integration.
*
* @return string html form.
* @throws EE_Error
* @throws ReflectionException
* @since 1.0.0
*/
protected static function _wp_user_settings_form()
{
EE_Registry::instance()->load_helper('HTML');
EE_Registry::instance()->load_helper('Template');
return new EE_Form_Section_Proper(
[
'name' => 'wp_user_settings_form',
'html_id' => 'wp_user_settings_form',
'layout_strategy' => new EE_Div_Per_Section_Layout(),
'subsections' => apply_filters(
'FHEE__EED_WP_Users_Admin___wp_user_settings_form__form_subsections',
[
'main_settings_hdr' => new EE_Form_Section_HTML(
EEH_HTML::h3(
esc_html__('WP User Integration Defaults', 'event_espresso')
)
),
'main_settings' => EED_WP_Users_Admin::_main_settings(),
]
),
]
);
}
/**
* Output the main settings section for wp_user_integration settings page.
*
* @return string html form.
* @throws EE_Error
*/
protected static function _main_settings()
{
global $wp_roles;
$registration_turned_off_msg = get_option('users_can_register')
? ''
: '<br><div class="error inline"><p></p>'
. sprintf(
esc_html__(
'Registration is currently turned off for your site, so the registration link will not show. If you want the registration link to show please %sgo here%s to turn it on.',
'event_espresso'
),
'<a href="' . admin_url('options-general.php') . '">',
'</a>'
)
. '</p></div>';
return new EE_Form_Section_Proper(
[
'name' => 'wp_user_settings_tbl',
'html_id' => 'wp_user_settings_tbl',
'html_class' => 'form-table',
'layout_strategy' => new EE_Admin_Two_Column_Layout(),
'subsections' => apply_filters(
'FHEE__EED_WP_Users_Admin___main_settigns__form_subsections',
[
'force_login' => new EE_Yes_No_Input(
[
'html_label_text' => esc_html__(
'Default setting for Login Required on Registration',
'event_espresso'
),
'html_help_text' => esc_html__(
'When this is set to "Yes", that means when you create an event the default for the "Login Required" setting on that event will be set to "Yes". When Login Required is set to "Yes" on an event it means that before users can register they MUST be logged in. You can still override this on each event.',
'event_espresso'
),
'default' => isset(
EE_Registry::instance()->CFG->addons->user_integration->force_login
)
? EE_Registry::instance()->CFG->addons->user_integration->force_login
: false,
'display_html_label_text' => false,
]
),
'registration_page' => new EE_Text_Input(
[
'html_label_text' => esc_html__(
'Registration Page URL (if different from default WordPress Registration)',
'event_espresso'
),
'html_help_text' => esc_html__(
'When login is required on an event, this will be the url used for the registration link on the login form',
'event_espresso'
) . $registration_turned_off_msg,
'default' => isset(
EE_Registry::instance()->CFG->addons->user_integration->registration_page
)
? EE_Registry::instance()->CFG->addons->user_integration->registration_page
: '',
'display_html_label_text' => true,
]
),
'auto_create_user' => new EE_Yes_No_Input(
[
'html_label_text' => esc_html__(
'Default setting for User Creation on Registration.',
'event_espresso'
),
'html_help_text' => esc_html__(
'When this is set to "Yes", that means when you create an event the default for the "Create User On Registration" setting on that event will be set to "Yes". When this setting is set to "Yes" on an event it means that when new non-logged in users register for an event, a new WP_User is created for them. You can still override this on each event.',
'event_espresso'
),
'default' => isset(
EE_Registry::instance()->CFG->addons->user_integration->auto_create_user
)
? EE_Registry::instance()->CFG->addons->user_integration->auto_create_user
: false,
'display_html_label_text' => false,
]
),
'default_wp_user_role' => new EE_Select_Input(
$wp_roles->get_names(),
[
'html_label_text' => esc_html__(
'Default role for User Creation on Registration.',
'event_espresso'
),
'html_help_text' => esc_html__(
'On new events, when User creation is set to yes, this setting indicates what the default role for new users will be on creation. You can still override this on each event.',
'event_espresso'
),
'default' => isset(
EE_Registry::instance()->CFG->addons->user_integration->default_wp_user_role
)
? EE_Registry::instance()->CFG->addons->user_integration->default_wp_user_role
: 'subscriber',
'display_html_label_text' => false,
]
),
'sync_user_with_contact' => new EE_Yes_No_Input(
[
'html_label_text' => esc_html__(
'Always sync contact information with WP user profile?',
'event_espresso'
) . EEH_Template::get_help_tab_link('user_sync_info'),
'html_help_text' => esc_html__(
'This global option is used to indicate behaviour when a logged in user registers for an event, and what happens to that user’s related contact, which in turn is related to the primary registration.',
'event_espresso'
),
'default' => isset(
EE_Registry::instance()->CFG->addons->user_integration->sync_user_with_contact
)
? EE_Registry::instance()->CFG->addons->user_integration->sync_user_with_contact
: true,
'display_html_label_text' => false,
]
),
] // end form subsections
) // end apply_filters for form subsections
]
);
}
/**
* callback for the update_wp_user_settings route.
* This handles the config update when the settings are saved.
*
* @param EE_Admin_Page $admin_page
* @return void
* @throws EE_Error
* @throws ReflectionException
*/
public static function update_wp_user_settings(EE_Admin_Page $admin_page)
{
$config = EE_Registry::instance()->CFG->addons->user_integration;
try {
$form = self::_wp_user_settings_form();
if ($form->was_submitted()) {
// capture form data
$form->receive_form_submission();
// validate_form_data
if ($form->is_valid()) {
$valid_data = $form->valid_data();
$config->force_login = $valid_data['main_settings']['force_login'];
$config->registration_page = $valid_data['main_settings']['registration_page'];
$config->auto_create_user = $valid_data['main_settings']['auto_create_user'];
$config->default_wp_user_role = $valid_data['main_settings']['default_wp_user_role'];
$config->sync_user_with_contact = $valid_data['main_settings']['sync_user_with_contact'];
}
} else {
if ($form->submission_error_message() != '') {
EE_Error::add_error($form->submission_error_message(), __FILE__, __FUNCTION__, __LINE__);
}
}
} catch (EE_Error $e) {
$e->get_error();
}
EE_Error::add_success(__('User Integration Settings updated.', 'event_espresso'));
EE_Registry::instance()->CFG->update_config('addons', 'user_integration', $config);
$admin_page->redirect_after_action(
false,
'',
'',
['action' => 'wp_user_settings'],
true
);
}
/**
* callback for FHEE__Extend_Registration_Form_Admin_Page__page_setup__page_config.
* Add additional config for saving WP_User settings to the Registration Form admin page system.
*
* @param array $page_config current page config.
* @param EE_Admin_Page $admin_page
* @return array
* @throws EE_Error
* @throws ReflectionException
* @since 1.0.0
*/
public static function add_wp_user_default_settings_config($page_config, EE_Admin_Page $admin_page)
{
$page_config['wp_user_settings'] = [
'nav' => [
'label' => esc_html__('User Integration Settings', 'event_espresso'),
'order' => 50,
],
'require_nonce' => false,
'help_tabs' => [
'wp_user_settings_help_tab' => [
'title' => esc_html__('WP User Settings', 'event_espresso'),
'content' => self::_settings_help_tab_content(),
],
],
'metaboxes' => ['_publish_post_box', '_espresso_news_post_box', '_espresso_links_post_box'],
];
return $page_config;
}
/**
* Callback for the WP Users Settings help tab content as set in the page_config array
*
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
protected static function _settings_help_tab_content()
{
EE_Registry::instance()->load_helper('Template');
return EEH_Template::display_template(
EE_WPUSERS_TEMPLATE_PATH . 'settings_help_tab.help_tab.php',
[],
true
);
}
/**
* Callback for FHEE__Extend_Events_Admin_Page__page_setup__page_config.
* Just injecting config for help tab contents added for ticket capability fields.
*
* @param array $page_config current page config
* @param EE_Admin_Page $admin_page
* @return array
* @throws EE_Error
* @throws ReflectionException
* @since 1.0.0
*/
public static function add_ticket_capability_help_tab($page_config, EE_Admin_Page $admin_page)
{
EE_Registry::instance()->load_helper('Template');
$file =
EE_WPUSERS_TEMPLATE_PATH . 'ticket_capability_help_content.template.php';
$page_config['create_new']['help_tabs']['ticket_capability_info'] = [
'title' => esc_html__('Ticket Capability Restrictions', 'event_espresso'),
'content' => EEH_Template::display_template($file, [], true),
];
$page_config['edit']['help_tabs']['ticket_capability_info'] = [
'title' => esc_html__('Ticket Capability Restrictions', 'event_espresso'),
'content' => EEH_Template::display_template($file, [], true),
];
return $page_config;
}
/**
* This is the metabox content for the wp user integration in the event editor.
*
* @param WP_Post $post
* @param array $metabox metabox arguments
* @return string html for metabox content.
* @throws EE_Error
* @throws ReflectionException
*/
public static function event_editor_metabox($post, $metabox)
{
// setup form and print out!
echo self::_get_event_editor_wp_users_form($post)->get_html_and_js();
}
/**
* Generate the event editor wp user settings form.
*
* @param $post
* @return EE_Form_Section_Proper
* @throws EE_Error
* @throws ReflectionException
*/
protected static function _get_event_editor_wp_users_form($post)
{
global $wp_roles;
$evt_id = $post instanceof EE_Event ? $post->ID() : null;
$evt_id = empty($evt_id) && isset($post->ID) ? $post->ID : 0;
EE_Registry::instance()->load_helper('HTML');
return new EE_Form_Section_Proper(
[
'name' => 'wp_user_event_settings_form',
'html_id' => 'wp_user_event_settings_form',
'layout_strategy' => new EE_No_Layout(['use_break_tags' => false]),
'subsections' => apply_filters(
'FHEE__EED_WP_Users_Admin__event_editor_metabox__wp_user_form_content',
[
'force_login' => new EE_Yes_No_Input(
[
'html_label_text' => esc_html__(
'Force Login for registrations?',
'event_espresso'
),
'html_help_text' => esc_html__(
'If yes, then all people registering for this event must login before they can register',
'event_espresso'
),
'default' => EE_WPUsers::is_event_force_login($evt_id),
'display_html_label_text' => true,
// 'html_class' => 'ee-input-width--small',
]
),
'spacing1' => new EE_Form_Section_HTML('<br>'),
'auto_user_create' => new EE_Yes_No_Input(
[
'html_label_text' => esc_html__(
'Auto Create users with registrations?',
'event_espresso'
),
'html_help_text' => esc_html__(
'If yes, then when non-logged in users register for this event, a user will automatically be created.',
'event_espresso'
),
'default' => EE_WPUsers::is_auto_user_create_on($evt_id),
'display_html_label_text' => true,
// 'html_class' => 'ee-input-width--small',
]
),
'spacing2' => new EE_Form_Section_HTML('<br>'),
'default_user_create_role' => new EE_Select_Input(
$wp_roles->get_names(),
[
'html_label_text' => esc_html__(
'Default role for auto-created users:',
'event_espresso'
),
'html_help_text' => esc_html__(
'When users are auto-created, what default role do you want them to have?',
'event_espresso'
),
'default' => EE_WPUsers::default_user_create_role($evt_id),
'display_html_label_text' => true,
]
),
]
),
]
);
}
/**
* Callback for FHEE__EED_WP_Users_Admin__event_editor_metabox__wp_user_form_content.
* Limit the Default role for auto-created users option to roles with manage_options cap.
*
* @param array $array of meta_box subsections.
*
* @return array
*/
public static function set_capability_default_user_create_role_event_editor($array)
{
if (! current_user_can('manage_options')) {
unset($array['default_user_create_role']);
}
return $array;
}
/**
* callback for FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks
* Set the callback for updating wp_user_settings on event update.
*
* @param array $callbacks existing array of callbacks.
* @return array
*/
public static function set_callback_save_wp_user_event_setting($callbacks)
{
$callbacks[] = ['EED_WP_Users_Admin', 'save_wp_user_event_setting'];
return $callbacks;
}
/**
* Callback for FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks.
* Saving WP_User event specific settings when events updated.
*
* @param EE_Event $event
* @param array $req_data request data.
* @return bool true success, false fail.
* @throws EE_Error
* @throws ReflectionException
*/
public static function save_wp_user_event_setting(EE_Event $event, $req_data)
{
try {
$form = self::_get_event_editor_wp_users_form($event);
if ($form->was_submitted()) {
$form->receive_form_submission();
if ($form->is_valid()) {
$valid_data = $form->valid_data();
EE_WPUsers::update_event_force_login($event, $valid_data['force_login']);
EE_WPUsers::update_auto_create_user($event, $valid_data['auto_user_create']);
if (current_user_can('manage_options')) {
EE_WPUsers::update_default_wp_user_role($event, $valid_data['default_user_create_role']);
}
}
} else {
if ($form->submission_error_message() != '') {
EE_Error::add_error($form->submission_error_message(), __FILE__, __FUNCTION__, __LINE__);
return false;
}
}
} catch (EE_Error $e) {
$e->get_error();
}
EE_Error::add_success(__('User Integration Event Settings updated.', 'event_espresso'));