-
Notifications
You must be signed in to change notification settings - Fork 0
/
better-user-management.php
2341 lines (2002 loc) · 73.6 KB
/
better-user-management.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
/**
* @Author Jonathon byrd
* @link http://www.5twentystudios.com
* @Package Wordpress
* @SubPackage Better User Management
* @Since 1.0.0
* @copyright Copyright (C) 2011 5Twenty Studios
*
*/
defined('ABSPATH') or die("Cannot access pages directly.");
/**
* Create the menu
*/
function bum_menu()
{
add_users_page( 'Manage Roles', 'Manage Roles', 'read', 'manage-roles', 'bum_manage_roles' );
add_users_page( 'Manage Capabilities', 'Manage Capabilities', 'read', 'manage-capabilities', 'bum_manage_caps' );
add_users_page( 'Manage Profiles', 'Manage Profiles', 'read', 'manage-profile', 'bum_manage_fields' );
}
function bum_show_custom_fields_admin()
{
//get extra fields
$profileuser = bum_get_user_to_edit( $_GET['user_id'] );
if(!$profileuser->data->ID) exit;
$fields = get_term_by( 'slug', $profileuser->roles[0], BUM_HIDDEN_FIELDS );
$form = new ValidForm( 'your-profile', '', get_permalink().'?user_id='.$_GET['user_id'] );
/*
* This handles extra fields ( basically reading the field info and putting it into ValidForm )
* Currently handles `radio`, `checkbox`, `select`, `input_text` ( text field ), and `textarea`
*/
if( $fields->description )
{
echo '<h3>Custom fields</h3>';
$fields = json_decode( $fields->description );
foreach( $fields as $field )
{
//get info
$info = bum_get_field_info( $field );
$fid = 'bum_'.sanitize_title( $info['title'] );
//this is handling `radio`, `checkbox`, `select`
if( in_array( $info['cssClass'], array( 'radio', 'checkbox', 'select' ) ) )
{
if( $info['cssClass'] == 'radio' )
$type = VFORM_RADIO_LIST;
elseif( $info['cssClass'] == 'checkbox' )
$type = VFORM_CHECK_LIST;
else
$type = VFORM_SELECT_LIST;
//Multiple values are seperated by | ( pipe )
if( strpos( $info['meta_value'], '|' ) !== false )
$info['meta_value'] = explode( '|', $info['meta_value'] );
$box = $form->addField( 'bum_'.$info['id'], $info['title'], $type,
array( 'required' => ($info['required']=='false'?false:true) ),
array( 'required' => 'The following field is required: '.$info['title'] ),
( $info['tip'] ? array( 'tip' => $info['tip'], 'default' => $info['meta_value'] ) : array('default' => $info['meta_value']) )
);
foreach( $info['values'] as $checkbox )
$box->addField( $checkbox->value, htmlentities( $checkbox->value ) );
}
//this is handling `input_text`, `textarea`
if( in_array( $info['cssClass'], array( 'input_text', 'textarea' ) ) )
{
if( $info['cssClass'] == 'input_text' )
$type = VFORM_STRING;
else
$type = VFORM_TEXT;
$form->addField( 'bum_'.$info['id'], $info['values'], $type,
array( 'required' => ($info['required']=='false'?false:true) ),
array( 'required' => 'The following field is required: '.$info['values'] ),
( $info['tip'] ? array( 'tip' => $info['tip'], 'default' => $info['meta_value'] ) : array('default' => $info['meta_value']) )
);
}
}
}
echo $form->toWpHtml();
}
/**
* Returns a Title, ID and Value
*/
function bum_get_field_info( $field )
{
//init
global $user_id;
$user = get_userdata($user_id);
$return = array();
//get title
if( $field->cssClass == 'radio' || $field->cssClass == 'checkbox' || $field->cssClass == 'select' )
$return['title'] = $field->title;
else
$return['title'] = $field->values;
//create ID
$return['id'] = $field->id == '' ? sanitize_title( $return['title'] ) : $field->id;
//get current value ( if set )
if( $user )
{
$meta = get_user_meta($user->ID, 'bum_'.$return['id'], true);
$return['meta_value'] = $meta ? $meta : 'Not set.';
}
else
{
$return['meta_value'] = false;
}
foreach( $field as $key => $value )
{
if( !isset( $return[$key] ) )
$return[$key] = $value;
}
return $return;
}
/**
*
*/
function bum_preload_data()
{
global $wp_roles;
//$wp_roles isn't set if visitor isn't logged in
$wp_roles = new WP_Roles();
if( isset( $wp_roles->roles ) )
{
foreach( $wp_roles->roles as $key => $role )
{
//loading additional role info
$term = get_term_by( 'slug', $key, BUM_HIDDEN_ROLES );
if( isset( $term ) && $term->description == 'yes' )
{
$wp_roles->roles[$key]['register'] = 'yes';
}
else
{
$wp_roles->roles[$key]['register'] = 'no';
}
}
}
}
/**
* Handle field stuff
*/
function bum_manage_fields()
{
$action = isset( $_POST['action'] ) ? $_POST['action'] : $_GET['action'];
switch( $action )
{
case 'add_edit':
global $wp_roles, $wp_user_fields;
if( isset( $_POST['fbJson'] ) )
{
//the fields are seperated by '&frmb'
$form_fields = array();
$fields = explode( '&frmb', $_POST['fbJson'] );
foreach( $fields as $field )
{
if( $field != '' )
{
//get info
list( $the_key, $the_value ) = explode( '=', $field );
preg_match_all( '/\[(.+?)]/', $the_key, $keys );
$keys = $keys[1];
//checkboxes show up as 'undefined' if unchecked or 'checked', should be saved as bool
$the_value = $the_value == 'undefined' ? 'false' : urldecode( $the_value );
$the_value = $the_value == 'checked' ? 'true' : $the_value;
//this is to deal with different depths of values
if( count( $keys ) == 2 )
$form_fields[$keys[0]][$keys[1]] = $the_value;
elseif( count( $keys ) == 3 )
$form_fields[$keys[0]][$keys[1]][$keys[2]] = $the_value;
elseif( count( $keys ) == 4 )
$form_fields[$keys[0]][$keys[1]][$keys[2]][$keys[3]] = $the_value;
}
}
//generate the json for this form
$form = new Formbuilder( $form_fields );
$vals = $form->store();
$json = $form->generate_json();
$hash = $vals['form_hash'];
//update or insert the json into hidden taxonomy
$term = get_term_by( 'slug', sanitize_title( $_GET['ptab'] ), BUM_HIDDEN_FIELDS );
if( $term )
{
wp_update_term( $term->term_id, BUM_HIDDEN_FIELDS,
array(
'description' => $json
)
);
}
else
{
wp_insert_term( $_GET['ptab'], BUM_HIDDEN_FIELDS,
array(
'description' => $json
)
);
}
}
break;
}
echo bum_get_show_view( 'bum-manage-fields' );
}
/**
* Handle capability stuff
*/
function bum_manage_caps()
{
$action = isset( $_POST['action'] ) ? $_POST['action'] : $_GET['action'];
switch( $action )
{
case 'delete-user-cap':
global $wp_roles;
$cap = $_GET['delete-id'];
foreach( $wp_roles->roles as $key => $role )
$wp_roles->remove_cap( $key, $cap );
break;
case 'edit-user-cap':
case 'add-user-cap':
global $wp_roles;
$cap = $_POST['cap-name'];
$roles = (array)$_POST['cap-roles'];
foreach( $wp_roles->roles as $key => $role )
{
if( in_array( $key, $roles ) )
$wp_roles->add_cap( $key, $cap );
else
$wp_roles->remove_cap( $key, $cap );
}
break;
}
echo bum_get_show_view( 'bum-manage-capabilities' );
}
/**
* Handle role stuff
*/
function bum_manage_roles()
{
$action = isset( $_POST['action'] ) ? $_POST['action'] : $_GET['action'];
switch( $action )
{
case 'delete-user-role':
//get vars
$name = $_GET['delete-id'];
$roles = new WP_Roles();
$roles->remove_role( $name );
break;
case 'edit-user-role':
//get vars
$edit_id = $_POST['edit-id'];
case 'add-user-role':
//get vars
$allow = $_POST['role-allow'] == 'yes' ? 'yes' : 'no';
$name = $_POST['role-name'];
$id = $_POST['role-slug'] == '' ? sanitize_title( $name ) : $_POST['role-slug'];
//add role
$roles = new WP_Roles();
if( isset( $edit_id ) )
$roles->remove_role( $edit_id );
$roles->add_role( $id, $name, array() );
//save registration part in hidden taxonomy
$term = get_term_by( 'slug', sanitize_title( (isset($edit_id)?$edit_id:$id) ), BUM_HIDDEN_ROLES );
if( $term )
{
wp_update_term( $term->term_id, BUM_HIDDEN_ROLES,
array(
'description' => $allow
)
);
}
else
{
wp_insert_term( sanitize_title( (isset($edit_id)?$edit_id:$id) ), BUM_HIDDEN_ROLES,
array(
'description' => $allow
)
);
}
break;
}
echo bum_get_show_view( 'bum-manage-roles' );
}
function bum_get_user_by_role( $role )
{
if( class_exists( 'WP_User_Search' ) )
{
$wp_user_search = new WP_User_Search( '', '', $role );
$ids = $wp_user_search->get_results();
}
else
{
global $wpdb;
$ids = $wpdb->get_col('SELECT ID
FROM '.$wpdb->users.' INNER JOIN '.$wpdb->usermeta.'
ON '.$wpdb->users.'.ID = '.$wpdb->usermeta.'.user_id
WHERE '.$wpdb->usermeta.'.meta_key = \''.$wpdb->prefix.'capabilities\'
AND '.$wpdb->usermeta.'.meta_value LIKE \'%"'.$role.'"%\'');
}
return $ids;
}
/**
* Contains all of the default user fields
*
* @return array
*/
function bum_get_default_profile_fields()
{
return array(
'rich_editing' => array(
'name' => 'Visual Editor',
'desc' => 'Disable the visual editor when writing',
'id' => 'rich_editing',
'type' => 'checkbox',
'std' => ''
),
'comment_shortcuts' => array(
'name' => 'Keyboard Shortcuts',
'desc' => 'Enable keyboard shortcuts for comment moderation. <a href="http://codex.wordpress.org/Keyboard_Shortcuts">More information</a>',
'id' => 'comment_shortcuts',
'type' => 'checkbox',
'std' => ''
),
'user_login' => array(
'name' => 'Username',
'desc' => 'Usernames cannot be changed.',
'id' => 'user_login',
'type' => 'text',
'std' => ''
),
'role' => array(
'name' => 'Role',
'desc' => 'Disable the visual editor when writing',
'id' => 'role',
'type' => 'select',
'options' => create_function('', "return get_roles_array();")
),
'first_name' => array(
'name' => 'First Name',
'desc' => '',
'id' => 'first_name',
'type' => 'text',
'std' => ''
),
'last_name' => array(
'name' => 'Last Name',
'desc' => '',
'id' => 'last_name',
'type' => 'text',
'std' => ''
),
'nickname' => array(
'name' => 'Nickname',
'desc' => '',
'id' => 'nickname',
'type' => 'text',
'std' => ''
),
'display_name' => array(
'name' => 'Display name publicly as',
'desc' => '',
'id' => 'display_name',
'type' => 'select',
'options' => create_function('', "return get_display_names( getVar( 'user_id' ));")
),
'user_email' => array(
'name' => 'E-mail',
'desc' => '',
'id' => 'user_email',
'type' => 'email',
'std' => ''
),
'url' => array(
'name' => 'Website',
'desc' => '',
'id' => 'url',
'type' => 'text',
'std' => ''
),
'aim' => array(
'name' => 'AIM',
'desc' => '',
'id' => 'aim',
'type' => 'text',
'std' => ''
),
'yim' => array(
'name' => 'Yahoo IM',
'desc' => '',
'id' => 'yim',
'type' => 'text',
'std' => ''
),
'jabber' => array(
'name' => 'Jabberz / Google Talk',
'desc' => '',
'id' => 'jabber',
'type' => 'text',
'std' => ''
),
'description' => array(
'name' => 'Biographical Info',
'desc' => 'Share a little biographical information to fill out your profile. This may be shown publicly.',
'id' => 'description',
'type' => 'textarea',
'std' => ''
),
'password' => array(
'name' => 'New Password',
'desc' => '',
'id' => 'password',
'type' => 'password',
'std' => ''
),
);
}
/**
* Do registration form
*
* @return boolean
* @since 1.2
*/
function bum_do_registration_form()
{
//initializing variables
$user_type = bum_is_user_type();
$pages = get_registration_pages();
$status = true;
//reasons to fail
if (!$user_type) $status = false;
if ($status && !isset($pages[$user_type])) $status = false;
bum_display_custom_user_fields( null, get_registration_fields( $user_type ));
return true;
}
/**
* Checks to see if there's a user type
*
*/
function bum_is_user_type()
{
if (isset($_REQUEST['user_type']) && $type = $_REQUEST['user_type'])
return $type;
return false;
}
/**
* Display the user edit fields
*
* @param unknown_type $user
*/
function bum_display_custom_user_fields($user = null, $fields = null)
{
//initializing variables
if (!is_null($user)) $user = get_userdata($user->ID);
$is_administration = false;
if (is_null($fields)) $is_administration = true;
if ($is_administration)
{
echo "<style>",
".field_wrapper label {display:block;position:relative;float:left;width:220px;}",
".typetext input {width: 25em;}",
".typecheckbox input {margin-right:200px;position:relative;float:left;}",
".field_wrapper span {display:block;padding-left:220px;}",
".field_wrapper {padding: 10px;}",
".typetextarea textarea {width: 500px;}",
".field_wrapper .profile_description{font-family: 'Lucida Grande', Verdana, Arial, 'Bitstream Vera Sans', sans-serif;font-size: 12px;font-style: italic;color: #666;}",
"</style>",
"<h3>Additional Details</h3>";
//initializing variables
$currentUser = new WP_User( $user->ID );
$fields = array();
foreach ($currentUser->roles as $role)
{
$fields = wp_parse_args($fields, get_custom_user_fields( $role ));
}
}
//reasons to fail
if (empty($fields)) return false;
// Use nonce for verification
echo '<div class="nonce_wrapper"><input type="hidden" name="user_meta_box_nonce" value="',
wp_create_nonce(basename(__FILE__)), '" /></div>',
'<input type="hidden" name="user_type" value="',bum_is_user_type(),'" />';
foreach ($fields as $field)
{
if (!current_user_can('edit_users') && $field['id'] == 'role')
{
continue;
}
// get current post meta data
$unique = md5(microtime());
if (!is_null($user) && isset($user->{$field['id']}))
{
$meta = $user->{$field['id']};
}
elseif(!is_null($user))
{
$meta = get_user_meta($user->ID, $field['id'], true);
}
else
{
$meta = (isset($_REQUEST[$field['id']]))?$_REQUEST[$field['id']]:'';
}
echo '<p class="field_wrapper div', $field['id'], ' type',$field['type'],'">';
if ($field['type'] != 'password') echo '<label for="', $field['id'], '">', $field['name'], '</label>';
switch ($field['type'])
{
case 'password':
echo
'<label for="', $field['id'], '">', $field['name'], '</label>',
'<input type="password" name="pass1" id="pass1" size="16" value="" autocomplete="off">',
'<span class="description">If you would like to change the password type a new one. Otherwise leave this blank.</span><br>',
'<input type="password" name="pass2" id="pass2" size="16" value="" autocomplete="off">',
'<span class="description">Type your new password again.</span><br>',
'<div id="pass-strength-result">Strength indicator</div>',
'<p class="description indicator-hint">Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ & ).</p>',
'<script type="text/javascript"> /* <![CDATA[ */
var pwsL10n = {
empty: "Strength indicator",
short: "Very weak",
bad: "Weak",
good: "Medium",
strong: "Strong",
mismatch: "Mismatch"
};
try{convertEntities(pwsL10n);}catch(e){};
/* ]]> */
</script>',
'<script type="text/javascript" src="',get_bloginfo('url'),'/wp-admin/load-scripts.php?c=1&load=jquery,hoverIntent,common,jquery-color,user-profile,password-strength-meter"></script>';
break;
case 'address':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', "\n",
'<label><div class="registration-label">',$field['desc'],'</div></label>';
break;
case 'email':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" class="registration-text" />', "\n";
echo '<input type="text" name="', $field['id'], '1" id="', $field['id'], '" value="', ($default = "Please confirm your email"),
'" class="registration-text" onBlur="if (this.value == \'\') this.value = \'',$default,'\';" onFocus="if (this.value == \'',$default,'\') this.value = \'\';" />', "\n",
'<label><div class="registration-label">'.$field['desc'].'</div></label>';
break;
case 'text':
$disabled = '';
if (is_user_logged_in() && $field['id'] == 'user_login') $disabled = 'readonly="true"';
echo '<input ',$disabled,' type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" class="registration-text" />', "\n",
'<label><div class="registration-label">', $field['desc'], '</div></label>';
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="30" rows="5">', $meta ? $meta : $field['std'], '</textarea>', "\n",
'<span class="profile_description">', $field['desc'], '</span>';
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
if (!is_array($field['options']))
{
$field['options'] = $field['options']();
}
foreach ($field['options'] as $key => $option)
{
if (is_int($key)) $key = $option;
echo '<option ', $meta == $option ? ' selected="selected"' : '',
' value="',$key,'">', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option)
{
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
echo '<br/>',$field['desc'];
break;
case 'checkbox':
echo '<input type="hidden" name="', $field['id'], '" value="" /> ';
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', ($meta && $meta != 'false') ? ' checked="checked"' : '', ' />',
'<span class="profile_description">', $field['desc'], '</span>';
break;
case 'editor':
echo
'<div style="border:1px solid #DFDFDF;border-collapse: separate;border-top-left-radius: 6px 6px;border-top-right-radius: 6px 6px;">',
'<textarea rows="10" class="theEditor" cols="40" name="', $field['id'], '" id="'.$unique.'"></textarea>',
'</div>',
'<script type="text/javascript">edCanvas = document.getElementById(\''.$unique.'\');</script>', "\n", $field['desc'];
break;
}
echo '</p>';
}
}
/**
*
* Enter description here ...
* @param int $user_id
* @param object $old_user
*/
function bum_save_user_profile( $user_id, $old_user )
{
bum_save_user_meta_data( $user_id );
}
/**
* Save user meta data
*
* @param $user_id
*/
function bum_save_user_meta_data( $user_id, $role = false )
{
//initializing variables
$user = new WP_User( $user_id );
$fields = array();
$role = $role === false ? $user->roles[0] : $role;
$fields = get_custom_user_fields( $role );
//reasons to fail
/*if (!isset($_REQUEST['user_meta_box_nonce']) || empty($fields)) return false;
// verify nonce
if (!wp_verify_nonce($_REQUEST['user_meta_box_nonce'], basename(__FILE__))) {
return $user_id;
}*/
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $user_id;
}
//update metas
if (is_array($fields))
{
foreach ($fields as $field)
{
$info = bum_get_field_info( $field );
$fid = 'bum_'.$info['id'];
if (!isset($_POST[$fid]) && !isset($_POST[$fid.'[]'])) continue;
$old = $info['meta_value'];
$new = $_REQUEST[$fid];
if ($new && $new != $old)
{
if( is_array( $new ) )
$new = implode( '|', $new );
update_user_meta($user_id, $fid, $new);
}
elseif ('' == $new && $old)
{
delete_user_meta($user_id, $fid, $old);
}
}
}
return true;
}
/**
* Function is responsible for remembering the user roles that
* will be used for registration
*
* @param unknown_type $id
* @param unknown_type $name
* @param unknown_type $capabilities
*/
function bum_register_user($id = null, $name = null, $capabilities =array( 'read' => true, 'level_0' => true ) )
{
//initializing
static $users;
if (!isset($users))
{
$users = array();
}
if (!is_null($id) && !isset($users[$id]))
{
$users[$id] = array(
'name' => $name,
'capabilities' => $capabilities,
);
}
unset($users[0]);
return $users;
}
/**
* Function is responsible for adding in the new roles that
* we'll be using for this job board
*
*/
function bum_user_roles()
{
//initializing
$users = bum_register_user();
foreach ((array)$users as $id => $user)
{
add_role($id, $user['name'], $user['capabilities']);
}
}
/**
* Function is responsible for displaying the login page.
*
* @param unknown_type $config
* @return unknown
*/
function bum_pages_shortcode( $config )
{
//initializing
$defaults = array(
'page' => 'Login'
);
$config = shortcode_atts($defaults, $config);
$page = strtolower($config['page']);
return bum_show_view( apply_filters('bum-page-shortcode', $page, $config) );
}
/**
* Function is responsible for setting the proper profile page
*
* @param unknown_type $page
* @param unknown_type $config
*/
function bum_page_profile( $page, $config )
{
global $bum_public_user;
if ($page == 'profile')
{
$page = 'bum-page-profile';
if (isset($_GET['action']) && $_GET['action'] == 'edit')
{
$page .= '-edit';
}
elseif (!is_null($bum_public_user))
{
$page .= '-public';
}
}
return $page;
}
/**
* Function is responsible for setting the proper login page
*
* @param unknown_type $page
* @param unknown_type $config
*/
function bum_page_login( $page, $config )
{
if ($page == 'login')
{
$page = 'bum-page-login';
}
return $page;
}
/**
* Function is responsible for adjusting the page template
*
* @param unknown_type $page
* @param unknown_type $config
*/
function bum_page_registration( $page, $config )
{
if ($page == 'registration')
{
$page = 'bum-page-registration';
if (isset($_GET['user_type']))
{
if ($view = bum_get_show_view($page.'-'.$_GET['user_type']))
{
echo $view;
}
else
{
$page .= '-form';
}
}
}
return $page;
}
/**
* Function is responsible for redirecting the user from the original
* login page, to our login page.
*/
function bum_login_redirect_original()
{
//initializing
$url = parse_url( bum_get_page_url() );
//reasons to return
if ($url['query'] == 'action=register')
wp_redirect( bum_get_permalink_registration() );
elseif(substr($url['path'],-12) == 'wp-login.php')
wp_redirect( bum_get_permalink_login() );
else
return false;
exit();
}
/**
* Function is responsible for adjusting the login/out links
*
* @param unknown_type $menu
* @param unknown_type $args
*/
function bum_wp_page_menu( $menu, $args )
{
//initializing
$html = str_get_html($menu);
if ($html->find('a', 0))
{
foreach ($html->find('a') as $a)
{
$a->href = apply_filters('bum_menu_href', $ahref = $a->href, $a->innertext);
$a->innertext = apply_filters('bum_menu_text', $a->innertext, $ahref);
//remove the registration page if logged in
if (is_user_logged_in() && (string)$a->href == bum_get_permalink_registration())
{
$parent = $a->parent();
$parent->outertext = '';
}
//remove the profile page if not logged in
elseif (!is_user_logged_in() && (string)$a->href == bum_get_permalink_profile())
{
$parent = $a->parent();
$parent->outertext = '';
}
}
}
return (string)$html;
}
/**
* Function is responsible for changing the hrefs
*
* @param unknown_type $href
* @param unknown_type $text
*/
function bum_check_menu_hrefs( $href, $text )
{
//initializing
$parts = parse_url($href);
if (is_user_logged_in())
{
// this is the login page
if (bum_get_permalink_login() == $href)
{
$href = bum_get_permalink_login('action=logout');
}
}
return $href;
}
/**
* Function is responsible for changing the menun item texts
*
* @param unknown_type $text
* @param unknown_type $href
*/
function bum_check_menu_text( $text, $href )
{
if (is_user_logged_in())
{
//this is the login page
if (bum_get_permalink_login() == $href)
{
$text = 'Logout';
}
}
return $text;
}
/**
* Since we're using this value to register users, we don't want
* the possibility for users to change it to something other then
* our specific user types
*
* @return string|string
*/
function bum_get_registration_type()
{
//initializing
global $wp_roles;
static $type;
//reasons to return
if (!isset($_REQUEST['user_type'])) return false;
if (!isset($type))
{
foreach( $wp_roles->roles as $key => $role )
{
if( sanitize_title( $_REQUEST['user_type'] ) == $key && $role['register'] == 'yes' )
$type = sanitize_title( $_REQUEST['user_type'] );
}
}
return $type;
}
/**
* Function is responsible for returning the necessary separator
*/
function bum_get_query_separator()
{
//initializing
global $wp_rewrite;
$s = ($wp_rewrite->using_permalinks())? "?" :"&" ;
return $s;
}
/**
* Function is responsible for updating the login url
*
* @param unknown_type $login_url
* @return unknown
*/
function bum_update_login_url( $url, $path )
{
if (substr($path,0,12) == 'wp-login.php')
{
$parts = parse_url($url);
if (!isset($parts['query']))
$parts['query'] = array();
return bum_get_permalink_login( $parts['query'] );
}
/*
//@FIX ME this is not equaling