mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-07-18 06:03:38 +09:00
- added filter for making 3rd-party roles editable through Ultimate Member interfaces. Use 'um_extend_editable_roles' and pass there an array of role keys( e.g. 'editor', 'administrator', etc. );
- reviewed and closed #1151;
This commit is contained in:
@@ -675,16 +675,7 @@ if ( ! class_exists( 'um\core\Builtin' ) ) {
|
||||
|
||||
$um_roles = array();
|
||||
if ( ! empty( $wp_roles->roles ) ) {
|
||||
$role_keys = get_option( 'um_roles', array() );
|
||||
if ( ! empty( $role_keys ) && is_array( $role_keys ) ) {
|
||||
$role_keys = array_map( function( $item ) {
|
||||
return 'um_' . $item;
|
||||
}, $role_keys );
|
||||
} else {
|
||||
$role_keys = array();
|
||||
}
|
||||
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
|
||||
$um_roles = UM()->roles()->get_roles( false, $exclude_roles );
|
||||
}
|
||||
|
||||
|
||||
@@ -3959,12 +3959,9 @@ if ( ! class_exists( 'um\core\Fields' ) ) {
|
||||
|
||||
// role field
|
||||
global $wp_roles;
|
||||
$role_keys = array_map( function( $item ) {
|
||||
return 'um_' . $item;
|
||||
}, get_option( 'um_roles', array() ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
|
||||
$roles = UM()->roles()->get_roles( false, $exclude_roles );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
|
||||
$roles = UM()->roles()->get_roles( false, $exclude_roles );
|
||||
|
||||
if ( ! empty( $options ) ) {
|
||||
|
||||
|
||||
@@ -493,13 +493,7 @@ if ( ! class_exists( 'um\core\Form' ) ) {
|
||||
}
|
||||
|
||||
global $wp_roles;
|
||||
$role_keys = array_map(
|
||||
function( $item ) {
|
||||
return 'um_' . $item;
|
||||
},
|
||||
get_option( 'um_roles', array() )
|
||||
);
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
|
||||
|
||||
if ( ! empty( $role ) &&
|
||||
( ! in_array( $role, $custom_field_roles, true ) || in_array( $role, $exclude_roles, true ) ) ) {
|
||||
@@ -840,13 +834,7 @@ if ( ! class_exists( 'um\core\Form' ) ) {
|
||||
|
||||
// role field
|
||||
global $wp_roles;
|
||||
$role_keys = array_map(
|
||||
function( $item ) {
|
||||
return 'um_' . $item;
|
||||
},
|
||||
get_option( 'um_roles', array() )
|
||||
);
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
|
||||
|
||||
$roles = UM()->roles()->get_roles( false, $exclude_roles );
|
||||
$roles = array_map(
|
||||
|
||||
@@ -446,20 +446,40 @@ if ( ! class_exists( 'um\core\Roles_Capabilities' ) ) {
|
||||
* @return array
|
||||
*/
|
||||
function get_editable_user_roles() {
|
||||
$default_roles = array( 'subscriber' );
|
||||
$editable_roles = array( 'subscriber' );
|
||||
|
||||
// User has roles so look for a UM Role one
|
||||
$um_roles_keys = get_option( 'um_roles', array() );
|
||||
|
||||
if ( ! empty( $um_roles_keys ) && is_array( $um_roles_keys ) ) {
|
||||
$um_roles_keys = array_map( function( $item ) {
|
||||
return 'um_' . $item;
|
||||
}, $um_roles_keys );
|
||||
|
||||
return array_merge( $um_roles_keys, $default_roles );
|
||||
$editable_roles = array_merge( $editable_roles, $um_roles_keys );
|
||||
}
|
||||
|
||||
return $default_roles;
|
||||
/**
|
||||
* UM hook
|
||||
*
|
||||
* @type filter
|
||||
* @title um_extend_editable_roles
|
||||
* @description Extend Editable User Roles
|
||||
* @input_vars
|
||||
* [{"var":"$editable_roles","type":"array","desc":"Editable Roles Keys"}]
|
||||
* @change_log
|
||||
* ["Since: 2.6.0"]
|
||||
* @usage add_filter( 'um_extend_editable_roles', 'function_name', 10, 1 );
|
||||
* @example
|
||||
* <?php
|
||||
* add_filter( 'um_extend_editable_roles', 'my_um_extend_editable_roles', 10, 1 );
|
||||
* function my_um_extend_editable_roles( $editable_roles ) {
|
||||
* // your code here
|
||||
* return $editable_roles;
|
||||
* }
|
||||
* ?>
|
||||
*/
|
||||
$editable_roles = apply_filters( 'um_extend_editable_roles', $editable_roles );
|
||||
return $editable_roles;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2112,18 +2112,8 @@ if ( ! class_exists( 'um\core\User' ) ) {
|
||||
//if isset roles argument validate role to properly for security reasons
|
||||
if ( isset( $args['role'] ) ) {
|
||||
global $wp_roles;
|
||||
$um_roles = get_option( 'um_roles', array() );
|
||||
|
||||
if ( ! empty( $um_roles ) ) {
|
||||
$role_keys = array_map( function( $item ) {
|
||||
return 'um_' . $item;
|
||||
}, $um_roles );
|
||||
} else {
|
||||
$role_keys = array();
|
||||
}
|
||||
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
|
||||
if ( in_array( $args['role'], $exclude_roles ) ) {
|
||||
unset( $args['role'] );
|
||||
}
|
||||
|
||||
@@ -263,7 +263,7 @@ function um_user_edit_profile( $args ) {
|
||||
}
|
||||
|
||||
// skip saving role here
|
||||
if ( in_array( $key, [ 'role', 'role_select', 'role_radio' ] ) ) {
|
||||
if ( in_array( $key, array( 'role', 'role_select', 'role_radio' ) ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -383,10 +383,7 @@ function um_user_edit_profile( $args ) {
|
||||
|
||||
if ( ! empty( $args['submitted']['role'] ) && current_user_can( 'promote_users' ) ) {
|
||||
global $wp_roles;
|
||||
$role_keys = array_map( function( $item ) {
|
||||
return 'um_' . $item;
|
||||
}, get_option( 'um_roles', array() ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
|
||||
|
||||
if ( ! in_array( $args['submitted']['role'], $exclude_roles ) ) {
|
||||
$to_update['role'] = $args['submitted']['role'];
|
||||
@@ -403,10 +400,7 @@ function um_user_edit_profile( $args ) {
|
||||
|
||||
if ( ! empty( $args['submitted']['role'] ) ) {
|
||||
global $wp_roles;
|
||||
$role_keys = array_map( function( $item ) {
|
||||
return 'um_' . $item;
|
||||
}, get_option( 'um_roles', array() ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
|
||||
|
||||
if ( ! in_array( $args['submitted']['role'], $exclude_roles ) ) {
|
||||
$to_update['role'] = $args['submitted']['role'];
|
||||
@@ -620,11 +614,7 @@ function um_restore_default_roles( $user_id, $args, $to_update ) {
|
||||
if ( ! empty( $args['submitted']['role'] ) && ! empty( $to_update['role'] ) ) {
|
||||
$wp_user = new WP_User( $user_id );
|
||||
|
||||
$role_keys = array_map( function( $item ) {
|
||||
return 'um_' . $item;
|
||||
}, get_option( 'um_roles', array() ) );
|
||||
|
||||
$leave_roles = array_diff( $args['roles_before_upgrade'], array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
$leave_roles = array_diff( $args['roles_before_upgrade'], UM()->roles()->get_editable_user_roles() );
|
||||
|
||||
if ( UM()->roles()->is_role_custom( $to_update['role'] ) ) {
|
||||
$wp_user->remove_role( $to_update['role'] );
|
||||
|
||||
@@ -430,17 +430,7 @@ function um_submit_form_register( $args ) {
|
||||
//get user role from field Role dropdown or radio
|
||||
if ( isset( $args['role'] ) ) {
|
||||
global $wp_roles;
|
||||
$um_roles = get_option( 'um_roles', array() );
|
||||
|
||||
if ( ! empty( $um_roles ) ) {
|
||||
$role_keys = array_map( function( $item ) {
|
||||
return 'um_' . $item;
|
||||
}, $um_roles );
|
||||
} else {
|
||||
$role_keys = array();
|
||||
}
|
||||
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );
|
||||
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
|
||||
|
||||
//if role is properly set it
|
||||
if ( ! in_array( $args['role'], $exclude_roles ) ) {
|
||||
|
||||
Reference in New Issue
Block a user