PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / front-end / default-fields / user-role / user-role.php

user-role.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.9, at front-end/default-fields/user-role/user-role.php

227 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 /* handle field output */
5 function wppb_user_role_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){
6 if ( $field['field'] == 'Select (User Role)' ){
7
8 $roles_editor_active = false;
9 $wppb_generalSettings = get_option( 'wppb_general_settings', 'not_found' );
10 if( $wppb_generalSettings != 'not_found' ) {
11 if( ! empty( $wppb_generalSettings['rolesEditor'] ) && ( $wppb_generalSettings['rolesEditor'] == 'yes' ) && $form_location == 'edit_profile' && current_user_can('manage_options') ) {
12 $roles_editor_active = true;
13 }
14 }
15
16 $user_role = '';
17 $user_roles = '';
18 $user_can_manage_options = false;
19
20 if( $form_location == 'edit_profile' && isset($field['user-roles-on-edit-profile']) && $field['user-roles-on-edit-profile'] === 'yes' )
21 $show_user_role_on_edit_profile = true;
22 else
23 $show_user_role_on_edit_profile = false;
24
25 // Get user data, set user's role and check to see if user can manage options
26 if( $user_id != 0 ) {
27 $user_data = get_userdata( $user_id );
28
29 if( ! empty( $user_data->roles ) ) {
30 $user_role = reset( $user_data->roles );
31 $user_roles = $user_data->roles;
32 }
33
34 if( isset( $user_data->allcaps['manage_options'] ) && $user_data->allcaps['manage_options'] == 1 ) {
35 $user_can_manage_options = true;
36 }
37 }
38
39 $input_value = isset( $request_data['custom_field_user_role'] ) ? $request_data['custom_field_user_role'] : $user_role;
40 $input_value_multiple = isset( $request_data['custom_field_user_role'] ) ? $request_data['custom_field_user_role'] : $user_roles;
41
42 $item_title = apply_filters( 'wppb_'.$form_location.'_user_role_custom_field_'.$field['id'].'_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_title_translation', $field['field-title'], true ) );
43 $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
44
45 //get user roles
46 if( !empty( $field['user-roles'] ) ) {
47 global $wp_roles;
48
49 $available_user_roles = explode( ', ', $field['user-roles'] );
50
51 foreach( $available_user_roles as $key => $role_slug ) {
52 if( isset( $wp_roles->roles[$role_slug]['name'] ) ) {
53 $available_user_roles[$key] = array(
54 'slug' => $role_slug,
55 'name' => $wp_roles->roles[$role_slug]['name']
56 );
57 } else {
58 unset( $available_user_roles[$key] );
59 }
60 }
61 }
62
63 $extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location );
64
65 if( $form_location == 'register' || ( $form_location == 'edit_profile' && apply_filters( 'wppb_user_role_select_field_capability', current_user_can('manage_options' )) && $user_can_manage_options == false ) || ( $show_user_role_on_edit_profile && !current_user_can('manage_options') ) ) {
66 $error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' );
67
68 if ( array_key_exists( $field['id'], $field_check_errors ) )
69 $error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>';
70
71 $output = '
72 <label for="custom_field_user_role">'.$item_title.$error_mark.'</label>
73 <select name="custom_field_user_role'. ( $roles_editor_active ? '[]' : '' ) .'" id="'.$field['meta-name'].'" class="custom_field_user_role '. apply_filters( 'wppb_fields_extra_css_class', '', $field ) .'" '. $extra_attr . ( $roles_editor_active ? ' multiple="multiple"' : '' ) .'>';
74
75 $extra_select_option = apply_filters( 'wppb_extra_select_option', '', $field, $item_title );
76 if( ! empty( $extra_select_option ) ) {
77 $output .= $extra_select_option;
78 }
79
80 if( ! empty( $available_user_roles ) ) {
81 foreach( $available_user_roles as $user_role ) {
82 $output .= '<option value="'. $user_role['slug'] .'"';
83
84 if( $roles_editor_active && is_array( $input_value_multiple ) ) {
85 if( in_array( $user_role['slug'], $input_value_multiple ) ) {
86 $output .= ' selected="selected" ';
87 }
88 } else {
89 $output .= selected( $input_value, $user_role['slug'], false );
90 }
91
92 $output .= '>'. translate_user_role( $user_role['name'] ) .'</option>';
93 }
94 }
95
96 $output .= '</select>';
97
98 if( $form_location == 'edit_profile' && !$show_user_role_on_edit_profile && !apply_filters( 'wppb_user_role_select_field_capability', current_user_can('manage_options' )) )
99 $output .= '<span class="wppb-description-delimiter">'. __( 'Only administrators can see this field on edit profile forms.', 'profile-builder' ) .'</span>';
100
101 if( !empty( $item_description ) )
102 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
103
104 } elseif( $form_location == 'edit_profile' && current_user_can('manage_options') && $user_can_manage_options == true ) {
105
106 $output = '
107 <label for="custom_field_user_role">'.$item_title.'</label>
108 <p>' . __( 'As an administrator you cannot change your role.', 'profile-builder' ) . '</p>';
109
110 $output .= '</select>';
111
112 if( !$show_user_role_on_edit_profile )
113 $output .= '<span class="wppb-description-delimiter">'. __( 'Only administrators can see this field on edit profile forms.', 'profile-builder' ) .'</span>';
114
115 if( !empty( $item_description ) )
116 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
117
118 }
119 else{
120 if( !empty( $input_value_multiple ) ){
121 foreach( $input_value_multiple as $input_value_multi ){
122 $output .= '<input type="hidden" disabled="disabled" readonly="readonly" value="'.$input_value_multi.'">';
123 }
124 }
125 else {
126 $output .= '<input type="hidden" disabled="disabled" readonly="readonly" value="' . $input_value . '">';
127 }
128 }
129
130 return apply_filters( 'wppb_'.$form_location.'_user_role_custom_field_'.$field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value, $input_value_multiple );
131 }
132 }
133 add_filter( 'wppb_output_form_field_select-user-role', 'wppb_user_role_handler', 10, 6 );
134
135
136 /* handle field validation */
137 function wppb_check_user_role_value( $message, $field, $request_data, $form_location ) {
138
139 if( $form_location == 'edit_profile' && isset($field['user-roles-on-edit-profile']) && $field['user-roles-on-edit-profile'] === 'yes' )
140 $show_user_role_on_edit_profile = true;
141 else
142 $show_user_role_on_edit_profile = false;
143
144 $field['meta-name'] = 'custom_field_user_role';
145
146 if( $form_location == 'back_end' )
147 return $message;
148
149 if( $form_location == 'edit_profile' && ( ( !current_user_can( 'manage_options' ) && ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ) ) && !$show_user_role_on_edit_profile ) )
150 return __( 'You cannot register this user role', 'profile-builder');
151
152 $roles_editor_active = false;
153 $wppb_generalSettings = get_option( 'wppb_general_settings', 'not_found' );
154 if( $wppb_generalSettings != 'not_found' ) {
155 if( ! empty( $wppb_generalSettings['rolesEditor'] ) && ( $wppb_generalSettings['rolesEditor'] == 'yes' ) && $form_location == 'edit_profile' && current_user_can('manage_options') ) {
156 $roles_editor_active = true;
157 }
158 }
159
160 if( $field['field'] == 'Select (User Role)' ){
161
162 if( ( $form_location == 'register' || $show_user_role_on_edit_profile ) && $field['required'] == 'Yes' && current_user_can( 'manage_options' ) === false ) {
163 if( ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) && ( trim( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) == '' ) ) || !isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ){
164 return wppb_required_field_error($field["field-title"]);
165 }
166 }
167
168 if( isset( $field['user-roles'] ) && isset( $request_data['custom_field_user_role'] ) ) {
169 $available_user_roles = explode(', ', $field['user-roles'] );
170
171 if( $roles_editor_active && is_array( $request_data['custom_field_user_role'] ) ) {
172 foreach( $request_data['custom_field_user_role'] as $key => $value ) {
173 if( ! in_array( $value, $available_user_roles ) ) {
174 return __( 'You cannot register this user role', 'profile-builder');
175 }
176 }
177 } else {
178 if( ! in_array( $request_data['custom_field_user_role'], $available_user_roles ) ) {
179 return __( 'You cannot register this user role', 'profile-builder');
180 }
181 }
182 }
183
184 }
185
186 return $message;
187 }
188 add_filter( 'wppb_check_form_field_select-user-role', 'wppb_check_user_role_value', 10, 4 );
189
190
191 /* handle field save */
192 function wppb_userdata_add_user_role( $userdata, $global_request, $form_args ){
193
194 if( wppb_field_exists_in_form( 'Select (User Role)', $form_args ) ) {
195
196 $roles_editor_active = false;
197 $wppb_generalSettings = get_option('wppb_general_settings', 'not_found');
198 if ($wppb_generalSettings != 'not_found') {
199 if (!empty($wppb_generalSettings['rolesEditor']) && $wppb_generalSettings['rolesEditor'] === 'yes' && current_user_can('manage_options')) {
200 $roles_editor_active = true;
201 }
202 }
203
204 if (isset($global_request['custom_field_user_role'])) {
205 if ($roles_editor_active && is_array($global_request['custom_field_user_role'])) {
206 $user_roles = array_map('trim', $global_request['custom_field_user_role']);
207 $user_roles = array_map('sanitize_text_field', $user_roles);
208
209 //don't allow administrator value. it should never be here but just in case make a hard check
210 if (($key = array_search("administrator", $user_roles)) !== false) {
211 unset($user_roles[$key]);
212 }
213
214 $userdata['role'] = $user_roles;
215 } else {
216 $role = sanitize_text_field(trim($global_request['custom_field_user_role']));
217 if( $role !== 'administrator' ) {//don't allow administrator value. it should never be here but just in case make a hard check
218 $userdata['role'] = $role;
219 }
220 }
221 }
222 }
223
224 return $userdata;
225 }
226 add_filter( 'wppb_build_userdata', 'wppb_userdata_add_user_role', 10, 3 );
227