ID); if (! empty($organization_id)) { $organization_roles = wiaas_get_organization_roles($organization_id); foreach ($organization_roles as $organization_role) { $role_list[$organization_role] = translate_user_role( wp_roles()->role_names[ $organization_role ] ); } if (! empty($organization_role)) { unset($role_list['none']); } } return $role_list; } /** * Customize columns for users table list view * * @param array $defaults * * @return array */ public static function manage_users_table_columns( $defaults = array() ) { $defaults['role'] = __('Roles', 'wiaas'); unset($defaults['posts']); return $defaults; } /** * Adds organization roles switcher menu to admin bar * * @param $admin_bar */ public static function add_role_switcher_menu($admin_bar) { if (is_super_admin()) { $roles = array( 'administrator' ); } else { $organization_id = wiaas_get_current_user_organization_id(); $roles = wiaas_get_organization_roles($organization_id); } $user = wp_get_current_user(); $current_role = $user->roles[0]; // Add menu item. $admin_bar->add_menu( array( 'id' => 'wiaas-view-as', 'parent' => 'top-secondary', 'title' => '' . __( 'View as:', 'wiaas' ) . '' . ''. __(translate_user_role( wp_roles()->role_names[ $current_role ] ), 'wiaas') . '' . '', 'href' => false, 'meta' => array( 'title' => __( 'View As', 'wiaas' ), 'tabindex' => '0', ), ) ); foreach ($roles as $role) { $role_name = translate_user_role( wp_roles()->role_names[ $role ] ); if ($role === $current_role) { // highlight current role $title = '' . __( $role_name, 'wiaas' ) . ''; $url = false; } else if ($role === 'customer') { // set external link for customer role $title = '' . __( $role_name, 'wiaas' ) . ''; $title .= ''; $url = WIAAS_CUSTOMER_INTERFACE; $target = '_blank'; } else { $title = '' . __( $role_name, 'wiaas' ) . ''; $url = wp_nonce_url(admin_url(), 'wiaas-set-role', 'wiaas-set-role-nonce'); $url = add_query_arg( 'wiaas-role', $role, $url ); $target = '_parent'; } $admin_bar->add_menu( array( 'id' => 'wiaas_'.$role, 'parent' => 'wiaas-view-as', 'title' => $title, 'href' => $url, 'meta' => array( 'title' => __( $role_name, 'wiaas' ), 'target' => $target ), ) ); } } /** * * Process organization role switch request if needed * */ public static function maybe_change_user_role() { if (! empty($_GET['wiaas-set-role-nonce']) && ! empty($_GET['wiaas-role'])) { if (wp_verify_nonce($_GET['wiaas-set-role-nonce'], 'wiaas-set-role')) { $role_name = sanitize_key($_GET['wiaas-role']); $role = wp_roles()->get_role($role_name); if (! empty($role) && $role_name !== 'customer') { // get current user $current_user = wp_get_current_user(); update_user_meta($current_user->ID, '_wiaas_admin_role', $role->name); // switch user role $current_user->set_role($role->name); } } wp_safe_redirect( admin_url('index.php') ); exit; } } } Wiaas_Admin_Organization::init();