-
Notifications
You must be signed in to change notification settings - Fork 41
/
pmpro-register-helper.php
executable file
·1621 lines (1426 loc) · 51 KB
/
pmpro-register-helper.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
/*
Plugin Name: Paid Memberships Pro - Register Helper Add On
Plugin URI: https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/
Description: Add custom form fields to membership checkout and user profiles with Paid Memberships Pro.
Version: 1.8.3
Author: Paid Memberships Pro
Author URI: https://www.paidmembershipspro.com
Text Domain: pmpro-register-helper
*/
define('PMPRORH_DIR', dirname(__FILE__) );
define('PMPRORH_URL', WP_PLUGIN_URL . "/pmpro-register-helper");
define('PMPRORH_VERSION', '1.8.3');
/*
Load plugin textdomain.
*/
function pmprorh_load_textdomain() {
load_plugin_textdomain( 'pmpro-register-helper', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'pmprorh_load_textdomain' );
/*
options - just defaults for now, will be in settings eventually
Copy these lines into a custom plugin or your theme's functions.php
and edit them to fit your needs.
*/
global $pmprorh_options;
//$pmprorh_options["register_redirect_url"] = home_url("/tools/rq/");
//$pmprorh_options["use_email_for_login"] = true;
//$pmprorh_options["directory_page"] = "/directory/";
//$pmprorh_options["profile_page"] = "/profile/";
/*
Includes
*/
require_once(PMPRORH_DIR . "/shortcodes/pmpro_signup.php"); //[pmpro_signup ...] shortcode
/*
Loading Modules
You can create customizations/modifications by copying the
register-form.php, change-password.php, directory.php, and/or profile.php
files and placing them in wp-content/themes/{YOUR THEME}/paid-memberships-pro/register-helper/
The code will check if there is a custom version before using the default.
*/
$pmprorh_options['modules'] = apply_filters('pmprorh_modules', array('register-form', 'change-password', 'directory', 'profile'));
$custom_dir = get_stylesheet_directory()."/paid-memberships-pro/register-helper/";
if(!empty($pmprorh_options) && !empty($pmprorh_options['modules']))
{
foreach($pmprorh_options['modules'] as $value)
{
//check folder for files,
$custom_file = $custom_dir.$value.".php";
if(file_exists($custom_file))
{
require_once($custom_file);
}
else
require_once(PMPRORH_DIR . "/modules/".$value.".php");
}
}
//PMProRH_Field class
/*
Some examples of creating fields with the class.
//company field is required and editable by admins and users in the profile page
$text = new PMProRH_Field("company", "text", array("size"=>40, "class"=>"company", "profile"=>true, "required"=>true));
//referral id is not required and only editable by admins. Includes an updated label.
$referral = new PMProRH_Field("referral", "text", array("label"=>"Referral Code", "profile"=>"admins"));
//dropdown, includes a blank option
$dropdown = new PMProRH_Field("gender", "select", array("options"=>array("" => "", "male"=>"Male", "female"=>"Female")));
//textarea
$history = new PMProRH_Field("history", "textarea", array("rows"=>10, "label"=>"Tell us a little about your history."));
//hidden
$secret = new PMProRH_Field("secret", "hidden", array("value"=>"this is the secret"));
*/
require_once(dirname(__FILE__) . "/classes/class.field.php");
//global to store extra registration fields
global $pmprorh_registration_fields, $pmprorh_checkout_boxes;
$pmprorh_registration_fields = array();
$cb = new stdClass();
$cb->name = "checkout_boxes";
$cb->label = apply_filters("pmprorh_section_header", __('More Information','pmpro-register-helper') );
$cb->order = 0;
$pmprorh_checkout_boxes = array("checkout_boxes" => $cb);
/*
$text = new PMProRH_Field("company", "text", array("size"=>40, "class"=>"company", "profile"=>true, "required"=>true));
pmprorh_add_registration_field("after_billing_fields", $text);
*/
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
/*
Add a field to the PMProRH regisration fields global
$where refers to various hooks in the PMPro checkout page and can be:
- after_username
- after_password
- after_email
- after_captcha
- checkout_boxes
- after_billing_fields
- before_submit_button
- just_profile (make sure you set the profile attr of the field to true or admins)
*/
function pmprorh_add_registration_field($where, $field) {
global $pmprorh_registration_fields;
if(empty($pmprorh_registration_fields[$where])) {
$pmprorh_registration_fields[$where] = array();
}
if ( !empty($field) && is_a( $field, 'PMProRH_Field') ) {
$pmprorh_registration_fields[$where][] = $field;
return true;
}
return false;
}
}
if ( ! function_exists( 'pmprorh_add_checkout_box' ) ) {
/*
Add a new checkout box to the checkout_boxes section. You can then use this as the $where parameter to pmprorh_add_registration_field.
Name must contain no spaces or special characters.
*/
function pmprorh_add_checkout_box($name, $label = NULL, $description = "", $order = NULL) {
global $pmprorh_checkout_boxes;
$temp = new stdClass();
$temp->name = $name;
$temp->label = $label;
$temp->description = $description;
$temp->order = $order;
//defaults
if(empty($temp->label))
$temp->label = ucwords($temp->name);
if(!isset($order))
{
$lastbox = pmprorh_end($pmprorh_checkout_boxes);
$temp->order = $lastbox->order + 1;
}
$pmprorh_checkout_boxes[$name] = $temp;
usort($pmprorh_checkout_boxes, "pmprorh_sortByOrder");
return true;
}
}
if ( ! function_exists( 'pmprorh_getCheckoutBoxByName' ) ) {
function pmprorh_getCheckoutBoxByName($name) {
global $pmprorh_checkout_boxes;
if(!empty($pmprorh_checkout_boxes))
{
foreach($pmprorh_checkout_boxes as $box)
{
if($box->name == $name)
return $box;
}
}
return false;
}
}
if ( ! function_exists( 'pmprorh_end' ) ) {
//from: http://www.php.net/manual/en/function.end.php#107733
function pmprorh_end($array) { return end($array); }
}
if ( ! function_exists( 'pmprorh_add_user_taxonomy' ) ) {
/**
* Add a new User Taxonomy. You can then use this as the user_taxonomny parameter to pmprorh_add_registration_field.
*
* @param string $name The singular name for the taxonomy object.
* @param string $name_plural The plural name for the taxonomy object.
*
*/
function pmprorh_add_user_taxonomy( $name, $name_plural ) {
global $pmprorh_user_taxonomies;
// Sanitize the taxonomy $name and make sure it is less than 32 characters.
$safe_name = sanitize_key( $name );
if ( strlen( $safe_name ) > 32 ) {
$safe_name = substr( $safe_name, 0, 32 );
}
// Make sure name and plural name are less than 32 characters.
if ( strlen( $name ) > 32 ) {
$name = substr( $name, 0, 32 );
}
if ( strlen( $name_plural ) > 32 ) {
$name_plural = substr( $name_plural, 0, 32 );
}
$pmprorh_user_taxonomy_labels = array(
'name' => ucwords( $name ),
'singular_name' => ucwords( $name ),
'menu_name' => ucwords( $name_plural ),
'search_items' => sprintf( __( 'Search %s', 'pmpro-register-helper' ), ucwords( $name_plural ) ),
'popular_items' => sprintf( __( 'Popular %s', 'pmpro-register-helper' ), ucwords( $name_plural ) ),
'all_items' => sprintf( __( 'All %s', 'pmpro-register-helper' ), ucwords( $name_plural ) ),
'edit_item' => sprintf( __( 'Edit %s', 'pmpro-register-helper' ), ucwords( $name ) ),
'update_item' => sprintf( __( 'Update %s', 'pmpro-register-helper' ), ucwords( $name ) ),
'add_new_item' => sprintf( __( 'Add New %s', 'pmpro-register-helper' ), ucwords( $name ) ),
'new_item_name' => sprintf( __( 'New %s Name', 'pmpro-register-helper' ), ucwords( $name ) ),
'separate_items_with_commas' => sprintf( __( 'Separate %s with commas', 'pmpro-register-helper' ), $name_plural ),
'add_or_remove_items' => sprintf( __( 'Add or remove %s', 'pmpro-register-helper' ), $name_plural ),
'choose_from_most_used' => sprintf( __( 'Choose from the most popular %s', 'pmpro-register-helper' ), $name_plural ),
);
//TO DO: Should this filter name just be formatted as core PMPro since this will be merged?
/**
* Filter the args passed to the user taxonomy created.
*
* @param array $pmprorh_user_taxonomy_args The arguments passed to the register_taxonomy function.
*
*/
$pmprorh_user_taxonomy_args = apply_filters( 'pmprorh_user_taxonomy_args', array(
'public' => false,
'labels' => $pmprorh_user_taxonomy_labels,
'rewrite' => false,
'show_ui' => true,
'capabilities' => array(
'manage_terms' => 'edit_users',
'edit_terms' => 'edit_users',
'delete_terms' => 'edit_users',
'assign_terms' => 'read',
),
)
);
register_taxonomy( $safe_name, 'user', $pmprorh_user_taxonomy_args );
/**
* Add admin page for the registered user taxonomies.
*/
add_action( 'admin_menu', function() use ( $pmprorh_user_taxonomy_labels, $safe_name ) {
add_users_page(
esc_attr( $pmprorh_user_taxonomy_labels['menu_name'] ),
esc_attr( $pmprorh_user_taxonomy_labels['menu_name'] ),
'edit_users',
'edit-tags.php?taxonomy=' . $safe_name
);
} );
/**
* Update parent file name to fix the selected menu issue for a user taaxonomy.
*/
add_filter( 'parent_file', function( $parent_file ) use ( $safe_name ) {
global $submenu_file;
if (
isset($_GET['taxonomy']) &&
$_GET['taxonomy'] == $safe_name &&
$submenu_file == 'edit-tags.php?taxonomy=' . $safe_name
) {
$parent_file = 'users.php';
}
return $parent_file;
} );
}
}
function pmprorh_sortByOrder($a, $b)
{
if ($a->order == $b->order) {
return 0;
}
return ($a->order < $b->order) ? -1 : 1;
}
/*
Load CSS, JS files
*/
function pmprorh_scripts()
{
global $pmpro_level, $pmpro_pages;
if( !is_admin() && ( !empty( $_REQUEST['level'] ) || !empty( $pmpro_level ) || ( isset( $pmpro_pages['member_profile_edit'] ) && is_page( $pmpro_pages['member_profile_edit'] ) ) ) ) {
if(!defined("PMPRO_VERSION"))
{
//load some styles that we need from PMPro (check child theme, then parent theme, then plugin folder)
if(file_exists(get_stylesheet_directory()."/paid-memberships-pro/register-helper/css/pmpro.css"))
wp_enqueue_style(get_stylesheet_directory_uri()."/paid-memberships-pro/register-helper/css/pmpro.css");
elseif(file_exists(get_template_directory()."/paid-memberships-pro/register-helper/css/pmpro.css"))
wp_enqueue_style(get_template_directory_uri()."/paid-memberships-pro/register-helper/css/pmpro.css");
else
wp_enqueue_style("pmprorh_pmpro", PMPRORH_URL . "/css/pmpro.css", NULL, PMPRORH_VERSION);
}
//load some styles that we need from PMPro (check child theme, then parent theme, then plugin folder)
if(file_exists(get_stylesheet_directory()."/paid-memberships-pro/register-helper/css/pmprorh_frontend.css"))
wp_enqueue_style(get_stylesheet_directory_uri()."/paid-memberships-pro/register-helper/css/pmprorh_frontend.css");
elseif(file_exists(get_template_directory()."/paid-memberships-pro/register-helper/css/pmprorh_frontend.css"))
wp_enqueue_style(get_template_directory_uri()."/paid-memberships-pro/register-helper/css/pmprorh_frontend.css");
elseif(function_exists("pmpro_https_filter"))
wp_enqueue_style("pmprorh_frontend", pmpro_https_filter(PMPRORH_URL) . "/css/pmprorh_frontend.css", NULL, "");
else
wp_enqueue_style("pmprorh_frontend", PMPRORH_URL . "/css/pmprorh_frontend.css", NULL, "");
}
}
add_action( 'wp_enqueue_scripts', 'pmprorh_scripts' );
/**
* Enqueue admin CSS
*/
function pmprorh_admin_enqueue_scripts() {
wp_enqueue_style(
'pmprorh_admin',
function_exists( 'pmpro_https_filter' )
? pmpro_https_filter( PMPRORH_URL ) . '/css/pmprorh_admin.css'
: PMPRORH_URL . '/css/pmprorh_admin.css',
array(),
PMPRORH_VERSION,
'screen'
);
}
add_action( 'admin_enqueue_scripts', 'pmprorh_admin_enqueue_scripts' );
/*
Cycle through extra fields. Show them at checkout.
*/
//default register_form (if PMPro is not installed)
function pmprorh_default_register_form()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["register_form"]))
{
foreach($pmprorh_registration_fields["register_form"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("register_form", "pmprorh_default_register_form");
//pmprorh register_form after_email
function pmprorh_register_form_after_email()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["pmprorh_after_email"]))
{
foreach($pmprorh_registration_fields["pmprorh_after_email"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin") ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmprorh_after_email", "pmprorh_register_form_after_email");
//pmprorh register_form after_password
function pmprorh_register_form_after_password()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["pmprorh_after_password"]))
{
foreach($pmprorh_registration_fields["pmprorh_after_password"] as $field)
{
if(is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmprorh_after_password", "pmprorh_register_form_after_password");
//pmprorh register_form after_email
function pmprorh_register_form()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["pmprorh_register_form"]))
{
foreach($pmprorh_registration_fields["pmprorh_register_form"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmprorh_register_form", "pmprorh_register_form");
/*
Cycle through extra fields. Show them at checkout.
*/
//after_username
function pmprorh_pmpro_checkout_after_username()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["after_username"]))
{
foreach($pmprorh_registration_fields["after_username"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmpro_checkout_after_username", "pmprorh_pmpro_checkout_after_username");
//after_password
function pmprorh_pmpro_checkout_after_password()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["after_password"]))
{
foreach($pmprorh_registration_fields["after_password"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmpro_checkout_after_password", "pmprorh_pmpro_checkout_after_password");
//after_email
function pmprorh_pmpro_checkout_after_email()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["after_email"]))
{
foreach($pmprorh_registration_fields["after_email"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmpro_checkout_after_email", "pmprorh_pmpro_checkout_after_email");
//after captcha
function pmprorh_pmpro_checkout_after_captcha()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["after_captcha"]))
{
foreach($pmprorh_registration_fields["after_captcha"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmpro_checkout_after_captcha", "pmprorh_pmpro_checkout_after_captcha");
//checkout boxes
function pmprorh_pmpro_checkout_boxes()
{
global $pmprorh_registration_fields, $pmprorh_checkout_boxes;
foreach($pmprorh_checkout_boxes as $cb)
{
//how many fields to show at checkout?
$n = 0;
if(!empty($pmprorh_registration_fields[$cb->name]))
foreach($pmprorh_registration_fields[$cb->name] as $field)
if(is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && (!isset($field->profile) || (isset($field->profile) && $field->profile !== "only" && $field->profile !== "only_admin"))) $n++;
if($n > 0) {
?>
<div id="pmpro_checkout_box-<?php echo $cb->name; ?>" class="pmpro_checkout">
<hr />
<h3>
<span class="pmpro_checkout-h3-name"><?php echo $cb->label;?></span>
</h3>
<div class="pmpro_checkout-fields">
<?php if(!empty($cb->description)) { ?>
<div class="pmpro_checkout_decription"><?php echo $cb->description; ?></div>
<?php } ?>
<?php
foreach($pmprorh_registration_fields[$cb->name] as $field) {
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && (!isset($field->profile) || (isset($field->profile) && $field->profile !== "only" && $field->profile !== "only_admin"))) {
$field->displayAtCheckout();
}
}
?>
</div> <!-- end pmpro_checkout-fields -->
</div> <!-- end pmpro_checkout_box-name -->
<?php
}
}
}
add_action("pmpro_checkout_boxes", "pmprorh_pmpro_checkout_boxes");
//after_pricing_fields
function pmprorh_pmpro_checkout_after_pricing_fields()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["after_pricing_fields"]))
{
foreach($pmprorh_registration_fields["after_pricing_fields"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmpro_checkout_after_pricing_fields", "pmprorh_pmpro_checkout_after_pricing_fields");
//after_billing_fields
function pmprorh_pmpro_checkout_after_billing_fields()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["after_billing_fields"]))
{
foreach($pmprorh_registration_fields["after_billing_fields"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmpro_checkout_after_billing_fields", "pmprorh_pmpro_checkout_after_billing_fields");
//before submit button
function pmprorh_pmpro_checkout_before_submit_button()
{
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields["before_submit_button"]))
{
foreach($pmprorh_registration_fields["before_submit_button"] as $field)
{
if( is_a($field, 'PMProRH_Field') && pmprorh_checkFieldForLevel($field) && ( !isset( $field->profile ) || $field->profile !== "only" && $field->profile !== "only_admin" ) ) {
$field->displayAtCheckout();
}
}
}
}
add_action("pmpro_checkout_before_submit_button", "pmprorh_pmpro_checkout_before_submit_button");
/*
Update the fields at checkout.
*/
function pmprorh_pmpro_after_checkout($user_id)
{
global $pmprorh_registration_fields;
//any fields?
if(!empty($pmprorh_registration_fields))
{
//cycle through groups
foreach($pmprorh_registration_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
if ( !is_a($field, 'PMProRH_Field') )
continue;
if(!pmprorh_checkFieldForLevel($field))
continue;
if(!empty($field->profile) && ($field->profile === "only" || $field->profile === "only_admin"))
continue; //wasn't shown at checkout
//assume no value
$value = NULL;
//where are we getting the value from?
if(isset($_REQUEST[$field->name]))
{
//request
$value = $_REQUEST[$field->name];
}
elseif(isset($_REQUEST[$field->name . '_checkbox']) && $field->type == 'checkbox')
{
//unchecked checkbox
$value = 0;
}
elseif(!empty($_POST[$field->name . "_checkbox"]) && in_array( $field->type, array( 'checkbox', 'checkbox_grouped', 'select2' ) ) ) //handle unchecked checkboxes
{
//unchecked checkbox
$value = array();
}
elseif(isset($_SESSION[$field->name]))
{
//file or value?
if(is_array($_SESSION[$field->name]) && isset($_SESSION[$field->name]['name']))
{
//add to files global
$_FILES[$field->name] = $_SESSION[$field->name];
//set value to name
$value = $_SESSION[$field->name]['name'];
}
else
{
//session
$value = $_SESSION[$field->name];
}
//unset
unset($_SESSION[$field->name]);
}
elseif(isset($_FILES[$field->name]))
{
//file
$value = $_FILES[$field->name]['name'];
}
//update user meta
if(isset($value))
{
if ( isset( $field->sanitize ) && true === $field->sanitize ) {
$value = pmprorh_sanitize( $value, $field );
}
//callback?
if(!empty($field->save_function))
call_user_func($field->save_function, $user_id, $field->name, $value);
else
update_user_meta($user_id, $field->meta_key, $value);
}
}
}
}
}
add_action('pmpro_after_checkout', 'pmprorh_pmpro_after_checkout');
add_action('pmpro_before_send_to_paypal_standard', 'pmprorh_pmpro_after_checkout'); //for paypal standard we need to do this just before sending the user to paypal
add_action('pmpro_before_send_to_twocheckout', 'pmprorh_pmpro_after_checkout', 20); //for 2checkout we need to do this just before sending the user to 2checkout
add_action('pmpro_before_send_to_gourl', 'pmprorh_pmpro_after_checkout', 20); //for the GoURL Bitcoin Gateway Add On
add_action('pmpro_before_send_to_payfast', 'pmprorh_pmpro_after_checkout', 20); //for the Payfast Gateway Add On
/**
* Sanitizes the passed value.
*
* @param array|int|null|string|stdClass $value The value to sanitize
*
* @return array|int|string|object Sanitized value
*/
function pmprorh_sanitize( $value, $field = null ) {
if ( is_array( $value ) ) {
foreach ( $value as $key => $val ) {
$value[ $key ] = pmprorh_sanitize( $val );
}
}
if ( is_object( $value ) ) {
foreach ( $value as $key => $val ) {
$value->{$key} = pmprorh_sanitize( $val );
}
}
if ( ! empty( $field ) && ! empty( $field->type ) && $field->type === 'textarea' ) {
$value = sanitize_textarea_field( $value );
} elseif ( ( ! is_array( $value ) ) && ctype_alpha( $value ) ||
( ( ! is_array( $value ) ) && strtotime( $value ) ) ||
( ( ! is_array( $value ) ) && is_string( $value ) ) ||
( ( ! is_array( $value ) ) && is_numeric( $value) )
) {
$value = sanitize_text_field( $value );
}
return $value;
}
/*
Require required fields.
*/
//require the fields
function pmprorh_rf_pmpro_registration_checks($okay)
{
global $current_user;
//arrays to store fields that were required and missed
$required = array();
$required_labels = array();
//any fields?
global $pmprorh_registration_fields;
if(!empty($pmprorh_registration_fields))
{
//cycle through groups
foreach($pmprorh_registration_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
//handle arrays
$field->name = preg_replace('/\[\]$/', '', $field->name);
//if the field is not for this level, skip it
if(!is_a($field, 'PMProRH_Field') || !pmprorh_checkFieldForLevel($field))
continue;
if(!empty($field->profile) && ($field->profile === "only" || $field->profile === "only_admin"))
continue; //wasn't shown at checkout
if(isset($_REQUEST[$field->name]))
$value = $_REQUEST[$field->name];
elseif(isset($_FILES[$field->name]))
{
$value = $_FILES[$field->name]['name'];
//handle empty file but the user already has a file
if(empty($value) && !empty($_REQUEST[$field->name . "_old"]))
$value = $_REQUEST[$field->name . "_old"];
elseif(!empty($value))
{
//check extension against allowed extensions
$filetype = wp_check_filetype_and_ext($_FILES[$field->name]['tmp_name'], $_FILES[$field->name]['name']);
if((!$filetype['type'] || !$filetype['ext'] ) && !current_user_can( 'unfiltered_upload' ))
{
if($okay) //only want to update message if there is no previous error
pmpro_setMessage(sprintf(__("Sorry, the file type for %s is not permitted for security reasons.", "pmprorh"), $_FILES[$field->name]['name']), "pmpro_error");
return false;
}
else
{
//check for specific extensions anyway
if(!empty($field->ext) && !in_array($filetype['ext'], $field->ext))
{
if($okay) //only want to update message if there is no previous error
pmpro_setMessage(sprintf(__("Sorry, the file type for %s is not permitted for security reasons.", "pmprorh"), $_FILES[$field->name]['name']), "pmpro_error");
return false;
}
}
}
}
else
$value = false;
if( ! $field->was_filled_if_needed() ) {
$required[] = $field->name;
$required_labels[] = $field->label;
}
}
}
}
if(!empty($required))
{
$required = array_unique($required);
//add them to error fields
global $pmpro_error_fields;
$pmpro_error_fields = array_merge((array)$pmpro_error_fields, $required);
if( count( $required ) == 1 ) {
$pmpro_msg = sprintf( __( 'The %s field is required.', 'pmpro-register-helper' ), implode(", ", $required_labels) );
$pmpro_msgt = 'pmpro_error';
} else {
$pmpro_msg = sprintf( __( 'The %s fields are required.', 'pmpro-register-helper' ), implode(", ", $required_labels) );
$pmpro_msgt = 'pmpro_error';
}
if($okay)
pmpro_setMessage($pmpro_msg, $pmpro_msgt);
return false;
}
//return whatever status was before
return $okay;
}
add_filter("pmpro_registration_checks", "pmprorh_rf_pmpro_registration_checks");
/*
Sessions vars for PayPal Express
*/
function pmprorh_rf_pmpro_paypalexpress_session_vars()
{
global $pmprorh_registration_fields;
//save our added fields in session while the user goes off to PayPal
if(!empty($pmprorh_registration_fields))
{
//cycle through groups
foreach($pmprorh_registration_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
if( !is_a($field, 'PMProRH_Field') || !pmprorh_checkFieldForLevel($field))
continue;
if(isset($_REQUEST[$field->name]))
$_SESSION[$field->name] = $_REQUEST[$field->name];
elseif(isset($_FILES[$field->name]))
{
/*
We need to save the file somewhere and save values in $_SESSION
*/
//check for a register helper directory in wp-content
$upload_dir = wp_upload_dir();
$pmprorh_dir = $upload_dir['basedir'] . "/pmpro-register-helper/tmp/";
//create the dir and subdir if needed
if(!is_dir($pmprorh_dir))
{
wp_mkdir_p($pmprorh_dir);
}
//move file
$new_filename = $pmprorh_dir . basename($_FILES[$field->name]['tmp_name']);
move_uploaded_file($_FILES[$field->name]['tmp_name'], $new_filename);
//update location of file
$_FILES[$field->name]['tmp_name'] = $new_filename;
//save file info in session
$_SESSION[$field->name] = $_FILES[$field->name];
}
}
}
}
}
add_action("pmpro_paypalexpress_session_vars", "pmprorh_rf_pmpro_paypalexpress_session_vars");
add_action("pmpro_before_send_to_twocheckout", "pmprorh_rf_pmpro_paypalexpress_session_vars", 10, 0);
/*
Show profile fields.
*/
function pmprorh_rf_show_extra_profile_fields($user, $withlocations = false)
{
global $pmprorh_registration_fields;
//which fields are marked for the profile
$profile_fields = pmprorh_getProfileFields($user->ID, $withlocations);
//show the fields
if(!empty($profile_fields) && $withlocations)
{
foreach($profile_fields as $where => $fields)
{
$box = pmprorh_getCheckoutBoxByName($where);
if ( !empty($box->label) )
{ ?>
<h3><?php echo $box->label; ?></h3><?php
} ?>
<table class="form-table">
<?php
//cycle through groups
foreach($fields as $field)
{
if ( is_a($field, 'PMProRH_Field') )
$field->displayInProfile($user->ID);
}
?>
</table>
<?php
}
}
elseif(!empty($profile_fields))
{
?>
<table class="form-table">
<?php
//cycle through groups
foreach($profile_fields as $field)
{
if ( is_a($field, 'PMProRH_Field') )
$field->displayInProfile($user->ID);
}
?>
</table>
<?php
}
}
function pmprorh_rf_show_extra_profile_fields_withlocations($user)
{
pmprorh_rf_show_extra_profile_fields($user, true);
}
add_action( 'show_user_profile', 'pmprorh_rf_show_extra_profile_fields_withlocations' );
add_action( 'edit_user_profile', 'pmprorh_rf_show_extra_profile_fields_withlocations' );
/**
* Show Profile fields on the frontend "Member Profile Edit" page.
*
* @since 2.3
*/
function pmprorh_rf_show_extra_frontend_profile_fields( $user, $withlocations = false ) {
global $pmprorh_registration_fields;
//which fields are marked for the profile
$profile_fields = pmprorh_getProfileFields($user->ID, $withlocations);
//show the fields
if ( ! empty( $profile_fields ) && $withlocations ) {
foreach( $profile_fields as $where => $fields ) {
$box = pmprorh_getCheckoutBoxByName( $where );
// Only show on front-end if there are fields to be shown.
$show_fields = false;
foreach( $fields as $key => $field ) {
if ( $field->profile !== 'only_admin' ) {
$show_fields = true;
}
}
// Bail if there are no fields to show on the front-end profile.
if ( ! $show_fields ) {
return;
}
?>
<div class="pmpro_checkout_box-<?php echo $where; ?>">
<?php if ( ! empty( $box->label ) ) { ?>
<h3><?php echo $box->label; ?></h3>
<?php } ?>
<div class="pmpro_member_profile_edit-fields">
<?php if ( ! empty( $box->description ) ) { ?>
<div class="pmpro_checkout_description"><?php echo $box->description; ?></div>
<?php } ?>
<?php
// Cycle through groups.
foreach( $fields as $field ) {
if ( is_a( $field, 'PMProRH_Field' ) && $field->profile !== 'only_admin' ) {
$field->displayAtCheckout( $user->ID );
}
}
?>
</div> <!-- end pmpro_member_profile_edit-fields -->
</div> <!-- end pmpro_checkout_box_$where -->
<?php
}
} elseif ( ! empty( $profile_fields ) ) { ?>
<div class="pmpro_member_profile_edit-fields">
<?php
// Cycle through groups.
foreach( $fields as $field ) {
if ( is_a( $field, 'PMProRH_Field' ) && $field->profile !== 'only_admin' ) {
$field->displayAtCheckout( $user->ID );
}
}
?>
</div> <!-- end pmpro_member_profile_edit-fields -->
<?php
}
}
function pmprorh_rf_show_extra_frontend_profile_fields_withlocations( $user ) {
pmprorh_rf_show_extra_frontend_profile_fields($user, true);
}
add_action( 'pmpro_show_user_profile', 'pmprorh_rf_show_extra_frontend_profile_fields_withlocations' );
/*
Integrate with PMPro Add Member Admin addon
*/
function pmprorh_pmpro_add_member_fields( $user = null, $user_id = null)
{
global $pmprorh_registration_fields;
$addmember_fields = array();
if(!empty($pmprorh_registration_fields))
{
//cycle through groups
foreach($pmprorh_registration_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
if(is_a($field, 'PMProRH_Field') && isset($field->addmember) && !empty($field->addmember) && ( in_array( strtolower( $field->addmember ), array( 'true', 'yes' ) ) || true == $field->addmember ) )
{
$addmember_fields[] = $field;
}
}
}
}