Skip to content

Commit b6e5873

Browse files
committed
Role field
1 parent 73662c3 commit b6e5873

File tree

5 files changed

+141
-19
lines changed

5 files changed

+141
-19
lines changed

includes/actions.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -456,25 +456,7 @@ function wpum_action_profile_update( $userId, $oldUserData = [] ) {
456456
$user = get_user_by('ID', $userId);
457457

458458
$wpum_roles = explode( '|', $_POST['_wpum_user_roles'] );
459-
$currentRoles = $user->roles;
460-
461-
if ( empty( $wpum_roles ) || ! is_array( $wpum_roles )) {
462-
return;
463-
}
464-
465-
// Remove unselected roles
466-
foreach ( $currentRoles as $role ) {
467-
if ( ! in_array( $role, $wpum_roles ) ) {
468-
$user->remove_role( $role );
469-
}
470-
}
471-
472-
// Add new roles
473-
foreach ( $wpum_roles as $role ) {
474-
if ( ! in_array( $role, $currentRoles ) ) {
475-
$user->add_role( $role );
476-
}
477-
}
459+
wpum_update_roles( $wpum_roles, $user );
478460
}
479461
}
480462

includes/functions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,32 @@ function wpum_send_registration_confirmation_email( $user_id, $psw = false, $pas
439439
$emails->__set( 'plain_text_password', null );
440440
}
441441

442+
/**
443+
* @param array $roles
444+
* @param WP_User $user
445+
*/
446+
function wpum_update_roles( $roles, $user, $remove_whitelist = array() ) {
447+
$currentRoles = $user->roles;
448+
449+
if ( empty( $roles ) || ! is_array( $roles )) {
450+
return;
451+
}
452+
453+
// Remove unselected roles
454+
foreach ( $currentRoles as $role ) {
455+
if ( ( empty( $remove_whitelist ) || in_array( $role, $remove_whitelist ) ) && ! in_array( $role, $roles ) ) {
456+
$user->remove_role( $role );
457+
}
458+
}
459+
460+
// Add new roles
461+
foreach ( $roles as $role ) {
462+
if ( ! in_array( $role, $currentRoles ) ) {
463+
$user->add_role( $role );
464+
}
465+
}
466+
}
467+
442468
/**
443469
* @param WP_User $user
444470
*/

includes/wpum-fields/class-wpum-fields.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function load() {
6363
'hidden',
6464
'taxonomy',
6565
'user',
66+
'userrole',
6667
] );
6768

6869
foreach ( $fields as $field ) {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Registers a User field for the forms.
4+
*
5+
* @package wp-user-manager
6+
* @copyright Copyright (c) 2021, WP User Manager
7+
* @license https://opensource.org/licenses/GPL-3.0 GNU Public License
8+
*/
9+
10+
// Exit if accessed directly
11+
if ( ! defined( 'ABSPATH' ) ) exit;
12+
13+
/**
14+
* Register a dropdown field type.
15+
*/
16+
class WPUM_Field_Userrole extends WPUM_Field_Type {
17+
18+
public function __construct() {
19+
$this->name = esc_html__( 'User Role', 'wp-user-manager' );
20+
$this->type = 'userrole';
21+
$this->icon = 'dashicons-admin-generic';
22+
$this->group = 'advanced';
23+
$this->allow_default = true;
24+
$this->min_addon_version = '2.5';
25+
}
26+
27+
public function get_data_keys() {
28+
$keys = parent::get_data_keys();
29+
30+
return array_merge( $keys, array_keys( $this->get_editor_settings()['general'] ) );
31+
}
32+
33+
/**
34+
* @return array
35+
*/
36+
public function get_editor_settings() {
37+
$settings = [
38+
'general' => [
39+
'options' => array(
40+
'type' => 'multiselect',
41+
'label' => __( 'User Roles', 'wp-user-manager' ),
42+
'hint' => esc_html__( 'List of roles the users can select from', 'wp-user-manager' ),
43+
'model' => 'options',
44+
'labels' => [],
45+
'required' => true,
46+
'options' => wpum_get_roles(),
47+
'multiple' => true,
48+
),
49+
'type_label' => array(
50+
'type' => 'input',
51+
'inputType' => 'text',
52+
'label' => esc_html__( 'Type label', 'wp-user-manager' ),
53+
'model' => 'type_label',
54+
'default' => 'Role',
55+
),
56+
],
57+
];
58+
59+
if ( wpum_get_option( 'allow_multiple_user_roles' ) ) {
60+
$settings['allow_multiple'] = array(
61+
'type' => 'checkbox',
62+
'label' => esc_html__( 'Allow multiple selection', 'wp-user-manager' ),
63+
'hint' => esc_html__( 'Allow users to select multiple roles for themselves', 'wp-user-manager' ),
64+
'model' => 'allow_multiple',
65+
'default' => false,
66+
);
67+
}
68+
69+
return $settings;
70+
}
71+
72+
/**
73+
* Format the output onto the profiles for the taxonomy field.
74+
*
75+
* @param object $field
76+
* @param mixed $value
77+
* @return string
78+
*/
79+
function get_formatted_output( $field, $value ) {
80+
if ( ! is_array( $value ) ) {
81+
$value = array( $value );
82+
}
83+
84+
return implode( ', ', $value );
85+
}
86+
87+
public function template() {
88+
return $this->type;
89+
}
90+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* The template for displaying the user role field.
4+
*
5+
* This template can be overridden by copying it to yourtheme/wpum/form-fields/userrole-field.php
6+
*
7+
* HOWEVER, on occasion WPUM will need to update template files and you
8+
* (the theme developer) will need to copy the new files to your theme to
9+
* maintain compatibility. We try to do this as little as possible, but it does
10+
* happen. When this occurs the version of the template file will be bumped and
11+
* the readme will list any important changes.
12+
*
13+
* @version 1.0.0
14+
*/
15+
16+
// Exit if accessed directly
17+
if ( ! defined( 'ABSPATH' ) ) exit;
18+
19+
$field_type = empty( $data->allow_multiple ) ? 'select' : 'multiselect';
20+
21+
$data->options = apply_filters( 'wpum_user_role_field_options', $data->options, $data );
22+
23+
WPUM()->templates->set_template_data( $data )->get_template_part( 'form-fields/' . $field_type, 'field' );

0 commit comments

Comments
 (0)