本文整理匯總了PHP中WP_User::update_user_level_from_caps方法的典型用法代碼示例。如果您正苦於以下問題:PHP WP_User::update_user_level_from_caps方法的具體用法?PHP WP_User::update_user_level_from_caps怎麽用?PHP WP_User::update_user_level_from_caps使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類WP_User
的用法示例。
在下文中一共展示了WP_User::update_user_level_from_caps方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: solvease_roles_capabilities_update_user_role_cap
/**
* update user roles and capabilities
* @param type $user
* @return boolean
*/
public function solvease_roles_capabilities_update_user_role_cap($user_id)
{
// check if its a valid POST
if (!isset($_POST['solvease_user_role_cap_nonce']) || !wp_verify_nonce($_POST['solvease_user_role_cap_nonce'], 'solvease_user_role_cap') || $_POST['user_id'] != $user_id) {
return;
}
$user = new WP_User($user_id);
// capabilities
$capabilities = isset($_POST['cap']) ? array_keys($_POST['cap']) : array();
// all user roles
$all_roles = $this->solvease_roles_capabilities_get_roles();
// primary roles
$primary_role = $_POST['primary_role'];
// secondary roles
$secondary_roles = isset($_POST['secondary_roles']) ? array_keys($_POST['secondary_roles']) : array();
// blank user roles
$user->roles = array();
// remove all user roles
$user->remove_all_caps();
// add primary roles
if ($primary_role != '' && isset($all_roles[$primary_role])) {
$user->add_role($primary_role);
} else {
return FALSE;
}
// add secondary roles
if (!empty($secondary_roles)) {
foreach ($secondary_roles as $secondary_role) {
if (isset($all_roles[$secondary_role]) && $primary_role != $secondary_role) {
$user->add_role($secondary_role);
}
}
}
// add capabilities of user roles
$user->update_user_level_from_caps();
// add capabilities
if (!empty($capabilities)) {
foreach ($capabilities as $capability) {
$user->add_cap($capability);
}
}
return $user;
}
示例2: update_user
/**
* Update user roles and capabilities
*
* @global WP_Roles $wp_roles
* @param WP_User $user
* @return boolean
*/
private function update_user($user)
{
global $wp_roles;
$values = array_values($user->roles);
$primary_role = array_shift($values);
// get 1st element from roles array as user primary role
if (empty($primary_role) || !isset($this->roles[$primary_role])) {
$primary_role = '';
}
if (function_exists('bbp_filter_blog_editable_roles')) {
// bbPress plugin is active
$bbp_user_role = bbp_get_user_role($user->ID);
} else {
$bbp_user_role = '';
}
// revoke all roles and capabilities from this user
$user->roles = array();
$user->remove_all_caps();
// restore primary role
if (!empty($primary_role)) {
$user->add_role($primary_role);
}
// restore bbPress user role if she had one
if (!empty($bbp_user_role)) {
$user->add_role($bbp_user_role);
}
// add other roles to user
foreach ($_POST as $key => $value) {
$result = preg_match('/^wp_role_(.+)/', $key, $match);
if ($result === 1) {
$role = $match[1];
if (isset($wp_roles->roles[$role])) {
$user->add_role($role);
}
}
}
// add individual capabilities to user
if (count($this->capabilities_to_save) > 0) {
foreach ($this->capabilities_to_save as $key => $value) {
$user->add_cap($key);
}
}
$user->update_user_level_from_caps();
return true;
}