本文整理汇总了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;
}