lib = URE_Lib::get_instance(); $this->set_hooks(); } // end of $lib public function set_hooks() { if (is_admin()) { add_filter( 'additional_capabilities_display', array($this, 'additional_capabilities_display'), 10, 1); add_action( 'admin_print_styles-user-edit.php', array($this, 'load_css') ); add_action( 'admin_print_styles-user-new.php', array($this, 'load_css') ); add_action( 'admin_enqueue_scripts', array($this, 'load_js' ) ); add_action( 'edit_user_profile', array($this, 'edit_user_profile_html'), 10, 1 ); add_action( 'user_new_form', array($this, 'user_new_form'), 10, 1 ); add_action( 'profile_update', array($this, 'update'), 10 ); } $multisite = $this->lib->get('multisite'); if ($multisite) { add_action( 'wpmu_activate_user', array($this, 'add_other_roles'), 10, 1 ); add_action( 'added_existing_user', array($this, 'add_other_roles'), 10, 1); } add_action( 'user_register', array($this, 'add_other_roles'), 10, 1 ); } // end of set_hooks() public function additional_capabilities_display($display) { $display = false; return $display; } // end of additional_capabilities_display() /* * Load CSS for the user profile edit page */ public function load_css() { wp_enqueue_style('wp-jquery-ui-dialog'); wp_enqueue_style('ure-jquery-multiple-select', plugins_url('/css/multiple-select.css', URE_PLUGIN_FULL_PATH), array(), false, 'screen'); } // end of load_css() public function load_js($hook_suffix) { if (!in_array($hook_suffix, array('user-edit.php', 'user-new.php'))) { return; } $select_primary_role = apply_filters('ure_users_select_primary_role', true); wp_enqueue_script('jquery-ui-dialog', '', array('jquery-ui-core', 'jquery-ui-button', 'jquery')); wp_register_script('ure-jquery-multiple-select', plugins_url('/js/multiple-select.js', URE_PLUGIN_FULL_PATH)); wp_enqueue_script('ure-jquery-multiple-select'); wp_register_script('ure-user-profile-other-roles', plugins_url('/js/user-profile-other-roles.js', URE_PLUGIN_FULL_PATH)); wp_enqueue_script('ure-user-profile-other-roles'); wp_localize_script('ure-user-profile-other-roles', 'ure_data_user_profile_other_roles', array( 'wp_nonce' => wp_create_nonce('user-role-editor'), 'other_roles' => esc_html__('Other Roles', 'user-role-editor'), 'select_roles' => esc_html__('Select additional roles for this user', 'user-role-editor'), 'select_primary_role' => ($select_primary_role || $this->lib->is_super_admin()) ? 1: 0 )); } // end of load_js() /** * Returns list of user roles, except 1st one, and bbPress assigned as they are shown by WordPress and bbPress themselves. * * @param type $user WP_User from wp-includes/capabilities.php * @return array */ public function get_roles_array( $user ) { if ( !is_array( $user->roles ) || count( $user->roles )<=1 ) { return array(); } // get bbPress assigned user role if ( function_exists( 'bbp_filter_blog_editable_roles' ) ) { $bb_press_role = bbp_get_user_role( $user->ID ); } else { $bb_press_role = ''; } $roles = array(); foreach ( $user->roles as $role) { if (!empty($bb_press_role) && $bb_press_role === $role) { // exclude bbPress assigned role continue; } $roles[] = $role; } array_shift( $roles ); // exclude primary role which is shown by WordPress itself return $roles; } // end of get_roles_array() private function roles_select_html($user, $context) { global $wp_roles; $user_roles = $user->roles; $primary_role = array_shift($user_roles); $roles = apply_filters('editable_roles', $wp_roles->roles); // exclude restricted roles if any $roles = array_reverse( $roles ); if (isset($roles[$primary_role])) { // exclude role assigned to the user as a primary role unset($roles[$primary_role]); } $button_number = (self::$counter>0) ? '_2': ''; echo '
'."\n"; if ($context=='add-new-user' || $context=='add-existing-user') { // Get other default roles $other_roles = $this->lib->get_option('other_default_roles', array()); } else { $other_roles = $this->get_roles_array($user); } if (is_array($other_roles) && count($other_roles) > 0) { $other_roles_str = implode(',', $other_roles); } else { $other_roles_str = ''; } echo ''; $output = $this->lib->roles_text($other_roles); echo ''. $output .''; self::$counter++; } // end of roles_select() /** * Returns comma separated string of capabilities directly (not through the roles) assigned to the user * * @global WP_Roles $wp_roles * @param object $user * @return string */ private function get_user_caps_str( $user ) { global $wp_roles; $output = ''; foreach ($user->caps as $cap => $value) { if (!$wp_roles->is_role($cap)) { if ('' != $output) { $output .= ', '; } $output .= $value ? $cap : sprintf(__('Denied: %s'), $cap); } } return $output; } // end of get_user_caps_str() private function user_profile_capabilities($user) { $current_user_id = get_current_user_id(); $user_caps = $this->get_user_caps_str($user); ?> '; if ($this->lib->user_is_admin($current_user_id)) { echo 'ID}", "ure_user_{$user->ID}") . '">' . esc_html__('Edit', 'user-role-editor') . ''; } ?> user_profile_capabilities($user); } ?>
roles_select_html($user, $context); ?>
is_user_profile_extention_allowed()) { return; } $show = apply_filters('ure_show_additional_capabilities_section', true); if (empty($show)) { return; } ?>

display($user, 'user-edit'); } // end of edit_user_profile_html() public function user_new_form($context) { $show = apply_filters('ure_show_additional_capabilities_section', true); if (empty($show)) { return; } $user = new WP_User(); ?> display($user, $context); ?>
roles ) && !in_array( $role_id, $user->roles ) ) { $ure_other_roles[] = $role_id; } } foreach( $ure_other_roles as $role ) { $user->add_role( $role ); } return 2; } // end of update() private function add_default_other_roles($user_id) { if ( !current_user_can('edit_users') ) { return false; } if ( !current_user_can('edit_user', $user_id) ) { return false; } $user = get_user_by('id', $user_id ); if ( empty( $user->ID ) ) { return true; } // Get default roles if any $other_default_roles = $this->lib->get_option('other_default_roles', array() ); if ( count( $other_default_roles ) == 0 ) { return true; } foreach ( $other_default_roles as $role ) { if ( !isset( $user->caps[$role] ) ) { $user->add_role( $role ); } } } // end of add_default_other_roles() public function add_other_roles( $user_id ) { if ( empty( $user_id ) ) { return false; } $result = $this->update( $user_id ); if ( $result==3 ) { // Other roles were not selected manually $this->add_default_other_roles( $user_id ); } } // end of add_other_roles() } // end of URE_User_Other_Roles class