PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
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 4.0.3, at front-end/default-fields/user-role/user-role.php

248 lines 12.2 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 $input_value = apply_filters( 'wppb_form_user_role_field_value', $input_value, $field, $form_location );
42 $input_value_multiple = apply_filters( 'wppb_form_user_role_multiple_field_value', $input_value_multiple, $field, $form_location );
43
44 $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 ) );
45 $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
46
47 //get user roles
48 if( !empty( $field['user-roles'] ) ) {
49 global $wp_roles;
50
51 $available_user_roles = explode( ', ', $field['user-roles'] );
52
53 foreach( $available_user_roles as $key => $role_slug ) {
54 if( isset( $wp_roles->roles[$role_slug]['name'] ) ) {
55 $available_user_roles[$key] = array(
56 'slug' => $role_slug,
57 'name' => $wp_roles->roles[$role_slug]['name']
58 );
59 } else {
60 unset( $available_user_roles[$key] );
61 }
62 }
63 }
64
65 $extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location );
66
67 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') ) ) {
68 $error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' );
69
70 if ( array_key_exists( $field['id'], $field_check_errors ) )
71 $error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>';
72
73 $output = '
74 <label for="custom_field_user_role">'.$item_title.$error_mark.'</label>
75 <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"' : '' ) .'>';
76
77 $extra_select_option = apply_filters( 'wppb_extra_select_option', '', $field, $item_title );
78 if( ! empty( $extra_select_option ) ) {
79 $output .= $extra_select_option;
80 }
81
82 if( ! empty( $available_user_roles ) ) {
83 foreach( $available_user_roles as $user_role ) {
84 $output .= '<option value="'. $user_role['slug'] .'"';
85
86 if( $roles_editor_active && is_array( $input_value_multiple ) ) {
87 if( in_array( $user_role['slug'], $input_value_multiple ) ) {
88 $output .= ' selected="selected" ';
89 }
90 } else {
91 $output .= selected( $input_value, $user_role['slug'], false );
92 }
93
94 $output .= '>'. translate_user_role( $user_role['name'] ) .'</option>';
95 }
96 }
97
98 $output .= '</select>';
99
100 if( $form_location == 'edit_profile' && !$show_user_role_on_edit_profile && !apply_filters( 'wppb_user_role_select_field_capability', current_user_can('manage_options' )) )
101 $output .= '<span class="wppb-description-delimiter">'. __( 'Only administrators can see this field on edit profile forms.', 'profile-builder' ) .'</span>';
102
103 if( !empty( $item_description ) )
104 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
105
106 } elseif( $form_location == 'edit_profile' && current_user_can('manage_options') && $user_can_manage_options == true ) {
107
108 $output = '
109 <label for="custom_field_user_role">'.$item_title.'</label>
110 <p>' . __( 'As an administrator you cannot change your role.', 'profile-builder' ) . '</p>';
111
112 $output .= '</select>';
113
114 if( !$show_user_role_on_edit_profile )
115 $output .= '<span class="wppb-description-delimiter">'. __( 'Only administrators can see this field on edit profile forms.', 'profile-builder' ) .'</span>';
116
117 if( !empty( $item_description ) )
118 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
119
120 }
121 else{
122 if( !empty( $input_value_multiple ) ){
123 foreach( $input_value_multiple as $input_value_multi ){
124 $output .= '<input type="hidden" disabled="disabled" readonly="readonly" value="' . esc_attr( $input_value_multi ) . '">';
125 }
126 }
127 else {
128 $output .= '<input type="hidden" disabled="disabled" readonly="readonly" value="' . esc_attr( $input_value ) . '">';
129 }
130 }
131
132 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 );
133 }
134 }
135 add_filter( 'wppb_output_form_field_select-user-role', 'wppb_user_role_handler', 10, 6 );
136
137
138 /* handle field validation */
139 function wppb_check_user_role_value( $message, $field, $request_data, $form_location ) {
140
141 if( $form_location == 'edit_profile' && isset($field['user-roles-on-edit-profile']) && $field['user-roles-on-edit-profile'] === 'yes' )
142 $show_user_role_on_edit_profile = true;
143 else
144 $show_user_role_on_edit_profile = false;
145
146 $field['meta-name'] = 'custom_field_user_role';
147
148 if( $form_location == 'back_end' )
149 return $message;
150
151 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 ) )
152 return __( 'You cannot register this user role', 'profile-builder');
153
154 $roles_editor_active = false;
155 $wppb_generalSettings = get_option( 'wppb_general_settings', 'not_found' );
156 if( $wppb_generalSettings != 'not_found' ) {
157 if( ! empty( $wppb_generalSettings['rolesEditor'] ) && ( $wppb_generalSettings['rolesEditor'] == 'yes' ) && $form_location == 'edit_profile' && current_user_can('manage_options') ) {
158 $roles_editor_active = true;
159 }
160 }
161
162 if( $field['field'] == 'Select (User Role)' ){
163
164 if( ( $form_location == 'register' || $show_user_role_on_edit_profile ) && $field['required'] == 'Yes' && current_user_can( 'manage_options' ) === false ) {
165 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'] )] ) ){
166 return wppb_required_field_error($field["field-title"]);
167 }
168 }
169
170 if( isset( $field['user-roles'] ) && isset( $request_data['custom_field_user_role'] ) ) {
171 $available_user_roles = explode(', ', $field['user-roles'] );
172
173 if( $roles_editor_active && is_array( $request_data['custom_field_user_role'] ) ) {
174 foreach( $request_data['custom_field_user_role'] as $key => $value ) {
175 if( ! in_array( $value, $available_user_roles ) ) {
176 return __( 'You cannot register this user role', 'profile-builder');
177 }
178 }
179 } else {
180 if( ! in_array( $request_data['custom_field_user_role'], $available_user_roles ) ) {
181 return __( 'You cannot register this user role', 'profile-builder');
182 }
183 }
184 }
185
186 }
187
188 return $message;
189 }
190 add_filter( 'wppb_check_form_field_select-user-role', 'wppb_check_user_role_value', 10, 4 );
191
192
193 /* handle field save */
194 function wppb_userdata_add_user_role( $userdata, $global_request, $form_args ){
195
196 if( wppb_field_exists_in_form( 'Select (User Role)', $form_args ) ) {
197
198 $roles_editor_active = false;
199 $wppb_generalSettings = get_option('wppb_general_settings', 'not_found');
200 if ($wppb_generalSettings != 'not_found') {
201 if (!empty($wppb_generalSettings['rolesEditor']) && $wppb_generalSettings['rolesEditor'] === 'yes' && current_user_can('manage_options')) {
202 $roles_editor_active = true;
203 }
204 }
205
206 if (isset($global_request['custom_field_user_role'])) {
207 if ($roles_editor_active && is_array($global_request['custom_field_user_role'])) {
208 $user_roles = array_map('trim', $global_request['custom_field_user_role']);
209 $user_roles = array_map('sanitize_text_field', $user_roles);
210
211 //don't allow administrator value. it should never be here but just in case make a hard check
212 if (($key = array_search("administrator", $user_roles)) !== false) {
213 unset($user_roles[$key]);
214 }
215
216 $userdata['role'] = $user_roles;
217 } else {
218 $role = sanitize_text_field(trim($global_request['custom_field_user_role']));
219 if( $role !== 'administrator' ) {//don't allow administrator value. it should never be here but just in case make a hard check
220 $userdata['role'] = $role;
221 }
222 }
223 }
224 }
225
226 return $userdata;
227 }
228 add_filter( 'wppb_build_userdata', 'wppb_userdata_add_user_role', 10, 3 );
229
230 //Activating Roles Editor from setup wizard
231 function wppb_handle_enable_roles_editor(){
232
233 if ( !isset( $_GET['wppb_nonce'] ) || !wp_verify_nonce( sanitize_text_field( $_GET['wppb_nonce'] ), 'wppb_enable_roles_editor_nonce' ) || !current_user_can( 'manage_options' ) )
234 return;
235
236 $wppb_generalSettings = get_option( 'wppb_general_settings', 'not_found' );
237
238 if ( $wppb_generalSettings !== 'not_found' && is_array( $wppb_generalSettings ) ) {
239 $wppb_generalSettings['rolesEditor'] = 'yes';
240 update_option( 'wppb_general_settings', $wppb_generalSettings );
241 }
242
243 // Redirect to Roles Editor page
244 wp_safe_redirect( admin_url( 'edit.php?post_type=wppb-roles-editor' ) );
245 exit;
246 }
247 add_action( 'admin_init', 'wppb_handle_enable_roles_editor', 9 );
248