/** * Check if user has a role or not * @author Howard <*****@*****.**> * @static * @param string $role * @param int $user_id * @return boolean */ public static function has_permission($role = 'guest', $user_id = '') { /** get user id **/ if (trim($user_id) == '') { $user_id = wpl_users::get_cur_user_id(); } /** get all roles **/ $roles = wpl_users::get_wpl_roles(); /** role validation **/ if (!in_array($role, $roles)) { $role = 'guest'; } $user_role = wpl_users::get_role($user_id); $user_role_point = wpl_users::get_role_point($user_role); $role_point = wpl_users::get_role_point($role); /** return true if user has access **/ if ($user_role_point >= $role_point) { return true; } return false; }
/** * For creating admin bar menu * @author Howard <*****@*****.**> * @global object $wp_admin_bar */ public function wpl_admin_bar_menu() { $cur_user_id = wpl_users::get_cur_user_id(); $cur_user_data = wpl_users::get_user($cur_user_id); $cur_role = wpl_users::get_role($cur_user_id, false); $wpl_roles = wpl_users::get_wpl_roles(); $menus = wpl_global::get_menus('menu', 'backend'); $submenus = wpl_global::get_menus('submenu', 'backend'); global $wp_admin_bar; /** generate pages object **/ $controller = new wpl_controller(); if (wpl_users::is_administrator($cur_user_id) or $cur_user_data->data->wpl_data->id) { /** add menus **/ foreach ($menus as $menu) { $menu_slug = (!wpl_users::is_administrator($cur_user_id) and $menu->capability != 'current') ? 'wpl_admin_profile' : $menu->menu_slug; $wp_admin_bar->add_menu(array('id' => $menu->menu_slug, 'title' => __($menu->menu_title, WPL_TEXTDOMAIN), 'href' => wpl_global::get_wp_admin_url() . 'admin.php?page=' . $menu_slug)); } /** add sub menus **/ foreach ($submenus as $submenu) { if (!wpl_users::has_menu_access($submenu->menu_slug)) { continue; } if (!wpl_users::is_administrator($cur_user_id) and $submenu->capability != 'current') { continue; } $menu_title = $submenu->separator ? $controller->wpl_add_separator() . __($submenu->menu_title, WPL_TEXTDOMAIN) : __($submenu->menu_title, WPL_TEXTDOMAIN); $wp_admin_bar->add_menu(array('id' => $submenu->menu_slug, 'parent' => $submenu->parent, 'title' => $menu_title, 'href' => wpl_global::get_wp_admin_url() . 'admin.php?page=' . $submenu->menu_slug)); } } }
/** * For creating admin menus * @author Howard <*****@*****.**> */ public function wpl_admin_menus() { $cur_user_id = wpl_users::get_cur_user_id(); $cur_user_data = wpl_users::get_user($cur_user_id); $cur_role = wpl_users::get_role($cur_user_id, false); $wpl_roles = wpl_users::get_wpl_roles(); $menus = wpl_global::get_menus('menu', 'backend'); $submenus = wpl_global::get_menus('submenu', 'backend'); /** generate pages object **/ $controller = new wpl_controller(); if (wpl_users::is_administrator($cur_user_id) or isset($cur_user_data->data->wpl_data->id)) { /** add menus **/ foreach ($menus as $menu) { $role = $menu->capability == 'current' ? $cur_role : $wpl_roles[$menu->capability]; $position = $menu->position ? $menu->position : NULL; add_menu_page(__($menu->page_title, WPL_TEXTDOMAIN), __($menu->menu_title, WPL_TEXTDOMAIN), $role, $menu->menu_slug, array($controller, $menu->function), '', $position); } /** add sub menus **/ foreach ($submenus as $submenu) { if (!wpl_users::has_menu_access($submenu->menu_slug)) { continue; } $role = $submenu->capability == 'current' ? $cur_role : $wpl_roles[$submenu->capability]; $menu_title = $submenu->separator ? $controller->wpl_add_separator() . __($submenu->menu_title, WPL_TEXTDOMAIN) : __($submenu->menu_title, WPL_TEXTDOMAIN); add_submenu_page($submenu->parent, __($submenu->page_title, WPL_TEXTDOMAIN), $menu_title, $role, $submenu->menu_slug, array($controller, $submenu->function)); } } }