Skip to content

Commit 005d3cd

Browse files
committed
Fixes for field role permissions
1 parent c0172e1 commit 005d3cd

File tree

6 files changed

+81
-61
lines changed

6 files changed

+81
-61
lines changed

includes/abstracts/abstract-wpum-field-type.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -146,75 +146,75 @@ public function get_data_keys() {
146146
protected function get_default_editor_settings() {
147147

148148
$settings = [
149-
'general' => [
150-
'field_title' => array(
149+
'general' => [
150+
'field_title' => array(
151151
'type' => 'input',
152152
'inputType' => 'text',
153153
'label' => esc_html__( 'Field title', 'wp-user-manager' ),
154154
'model' => 'field_title',
155155
'required' => true,
156156
'placeholder' => esc_html__( 'Enter a title for this field', 'wp-user-manager' ),
157157
'validator' => [ 'string' ],
158-
'min' => 1
158+
'min' => 1,
159159
),
160160
'field_description' => array(
161161
'type' => 'textArea',
162162
'inputType' => 'text',
163163
'label' => esc_html__( 'Field description (optional)', 'wp-user-manager' ),
164164
'model' => 'field_description',
165165
'rows' => 3,
166-
'hint' => esc_html__( 'This is the text that appears as a description within the forms. Leave blank if not needed.', 'wp-user-manager' )
166+
'hint' => esc_html__( 'This is the text that appears as a description within the forms. Leave blank if not needed.', 'wp-user-manager' ),
167167
),
168-
'user_meta_key' => array(
168+
'user_meta_key' => array(
169169
'type' => 'input',
170170
'inputType' => 'text',
171171
'label' => esc_html__( 'Unique meta key', 'wp-user-manager' ),
172172
'model' => 'user_meta_key',
173173
'required' => true,
174174
'hint' => esc_html__( 'The key must be unique for each field and written in lowercase with an underscore ( _ ) separating words e.g country_list or job_title. This will be used to store information about your users into the database of your website.', 'wp-user-manager' ),
175175
'validator' => [ 'string' ],
176-
'min' => 1
176+
'min' => 1,
177177
),
178-
'placeholder' => array(
178+
'placeholder' => array(
179179
'type' => 'input',
180180
'inputType' => 'text',
181181
'label' => esc_html__( 'Placeholder', 'wp-user-manager' ),
182182
'model' => 'placeholder',
183183
'hint' => esc_html__( 'This text will appear within the field when empty. Leave blank if not needed.', 'wp-user-manager' ),
184184
),
185185
],
186-
'validation' => [
186+
'validation' => [
187187
'required' => array(
188188
'type' => 'checkbox',
189189
'label' => esc_html__( 'Set as required', 'wp-user-manager' ),
190190
'model' => 'required',
191191
'default' => false,
192192
'hint' => esc_html__( 'Enable this option so the field must be filled before the form can be processed.', 'wp-user-manager' ),
193-
)
193+
),
194194
],
195-
'privacy' => [
195+
'privacy' => [
196196
'visibility' => array(
197-
'type' => 'radios',
198-
'label' => esc_html__( 'Profile visibility', 'wp-user-manager' ),
199-
'model' => 'visibility',
200-
'hint' => esc_html__( 'Set the visibility of this field on users profiles.', 'wp-user-manager' ),
201-
'values' => [
197+
'type' => 'radios',
198+
'label' => esc_html__( 'Profile visibility', 'wp-user-manager' ),
199+
'model' => 'visibility',
200+
'hint' => esc_html__( 'Set the visibility of this field on users profiles.', 'wp-user-manager' ),
201+
'values' => [
202202
[ 'value' => 'public', 'name' => esc_html__( 'Publicly visible', 'wp-user-manager' ) ],
203-
[ 'value' => 'hidden', 'name' => esc_html__( 'Hidden', 'wp-user-manager' ) ]
203+
[ 'value' => 'hidden', 'name' => esc_html__( 'Hidden', 'wp-user-manager' ) ],
204204
],
205205
'noneSelectedText' => '',
206-
'hideNoneSelectedText' => true,
207-
)
206+
'hideNoneSelectedText' => true,
207+
),
208208
],
209209
'permissions' => [
210-
'editing' => array(
211-
'type' => 'radios',
212-
'label' => esc_html__( 'Profile editing', 'wp-user-manager' ),
213-
'model' => 'editing',
214-
'hint' => esc_html__( 'Set who can edit this field. Hidden fields will not be editable within the front-end account page.', 'wp-user-manager' ),
210+
'editing' => array(
211+
'type' => 'radios',
212+
'label' => esc_html__( 'Profile editing', 'wp-user-manager' ),
213+
'model' => 'editing',
214+
'hint' => esc_html__( 'Set who can edit this field. Hidden fields will not be editable within the front-end account page.', 'wp-user-manager' ),
215215
'values' => [
216216
[ 'value' => 'public', 'name' => esc_html__( 'Publicly editable', 'wp-user-manager' ) ],
217-
[ 'value' => 'hidden', 'name' => esc_html__( 'Hidden (admins only)', 'wp-user-manager' ) ]
217+
[ 'value' => 'hidden', 'name' => esc_html__( 'Hidden (admins only)', 'wp-user-manager' ) ],
218218
],
219219
),
220220
'read_only' => array(
@@ -224,16 +224,16 @@ protected function get_default_editor_settings() {
224224
'default' => false,
225225
'hint' => esc_html__( 'Enable to prevent users from editing this field. Note: if the profile editing option is set to publicly editable, the field will still be visible within the account page but will not be customizable.', 'wp-user-manager' ),
226226
),
227-
'roles' => array(
228-
'type' => 'multiselect',
229-
'label' => esc_html__( 'Roles', 'wp-user-manager' ),
230-
'model' => 'roles',
231-
'default' => array(),
232-
'options' => wpum_get_roles( true, true ),
227+
'roles' => array(
228+
'type' => 'multiselect',
229+
'label' => esc_html__( 'Allowed Roles', 'wp-user-manager' ),
230+
'model' => 'roles',
231+
'default' => array(),
232+
'options' => wpum_get_roles( true, true ),
233233
'multiple' => true,
234-
'hint' => esc_html__( 'Show the fields only for selected users', 'wp-user-manager' )
235-
)
236-
]
234+
'hint' => esc_html__( 'Show the field on the profile and account pages to users with the selected roles.', 'wp-user-manager' ),
235+
),
236+
],
237237
];
238238

239239
if ( $this->allow_default ) {

includes/filters.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,38 @@ function wpum_set_displayname_on_registration( $user_data ) {
294294
}
295295

296296
add_filter( 'wpum_registration_user_data', 'wpum_set_displayname_on_registration', 10 );
297+
298+
add_filter( 'wpum_account_display_field', 'wpum_maybe_display_field', 10, 2 );
299+
add_filter( 'wpum_profile_display_field', 'wpum_maybe_display_field' );
300+
301+
/**
302+
* Verify if the field has correct user role permission.
303+
*
304+
* @param bool $display
305+
* @param WPUM_Field $field
306+
*
307+
* @return bool
308+
*/
309+
function wpum_maybe_display_field( $display, $field = null ) {
310+
if ( ! $display ) {
311+
return $display;
312+
}
313+
314+
if ( empty( $field ) ) {
315+
global $wpum_field;
316+
317+
$field = $wpum_field;
318+
}
319+
320+
$field_roles = $field->get_meta( 'roles' );
321+
322+
if ( empty( $field_roles ) ) {
323+
return true;
324+
}
325+
326+
if ( ! is_user_logged_in() ) {
327+
return false;
328+
}
329+
330+
return count( array_intersect( wp_get_current_user()->roles, $field_roles ) ) > 0;
331+
}

includes/wpum-fields/wpum-fields-functions.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -489,19 +489,7 @@ function wpum_is_field_required() {
489489
return apply_filters( 'wpum_is_field_required', $wpum_field->is_required(), $wpum_field->get_ID() );
490490
}
491491

492-
/**
493-
* Verify if the field has correct user role permission.
494-
*
495-
* @return bool
496-
*/
497-
function wpum_field_has_user_role_permission( $field_id = null ) {
498-
global $wpum_field;
499-
500-
$field = $field_id ? new WPUM_Field( $field_id ) : $wpum_field;
501-
$field_roles = $field->get_meta( 'roles' );
502492

503-
return ! ( is_array( $field_roles ) && count( $field_roles ) && !count( array_intersect( wp_get_current_user()->roles, $field_roles ) ) );
504-
}
505493

506494
/**
507495
* Retrieve the current field type within a loop.

includes/wpum-forms/class-wpum-form-profile.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ private function get_account_fields() {
167167

168168
$field = new WPUM_Field( $field );
169169

170-
if ( $field->exists() && $field->get_meta( 'editing' ) == 'public' && $field->get_primary_id() !== 'user_password' ) {
170+
if ( $field->exists() && $field->get_primary_id() !== 'user_password' ) {
171+
172+
if ( ! apply_filters( 'wpum_account_display_field', $field->get_meta( 'editing' ) === 'public', $field ) ) {
173+
continue;
174+
}
171175

172176
// Skip the avatar field if disabled.
173177
if ( $field->get_primary_id() == 'user_avatar' && ! wpum_get_option( 'custom_avatars' ) ) {

templates/forms/form-account.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@
4444

4545
<?php foreach ( $data->fields as $key => $field ) : ?>
4646

47-
<?php
48-
//Checks user has role perimission
49-
if( ! apply_filters( 'wpum_account_form_field_can_render', wpum_field_has_user_role_permission( $field['id'] ), $field, $data ) ){
50-
continue;
51-
}
52-
?>
53-
5447
<?php
5548
// Parent field should handle the child field rendering
5649
if( in_array( $field['type'], wpum_get_registered_parent_field_types() ) ){

templates/profiles/about.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636

3737
<table class="profile-fields-table">
3838
<tbody>
39-
<?php while ( wpum_profile_fields() ) : wpum_the_profile_field(); ?>
40-
<?php if ( wpum_field_has_data() && apply_filters( 'wpum_about_field_can_render', wpum_field_has_user_role_permission() ) ) : ?>
41-
<tr class="<?php wpum_the_field_css_class(); ?>">
42-
<td class="label"><?php wpum_the_field_name(); ?></td>
43-
<td class="data"><?php wpum_the_field_value(); ?></td>
44-
</tr>
45-
<?php endif; ?>
46-
<?php endwhile; ?>
39+
<?php while ( wpum_profile_fields() ) : wpum_the_profile_field(); ?>
40+
<?php if ( wpum_field_has_data() && apply_filters( 'wpum_profile_display_field', true ) ) : ?>
41+
<tr class="<?php wpum_the_field_css_class(); ?>">
42+
<td class="label"><?php wpum_the_field_name(); ?></td>
43+
<td class="data"><?php wpum_the_field_value(); ?></td>
44+
</tr>
45+
<?php endif; ?>
46+
<?php endwhile; ?>
4747
</tbody>
4848
</table>
4949
</div>

0 commit comments

Comments
 (0)