本文整理汇总了PHP中WP_User::get_role_caps方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_User::get_role_caps方法的具体用法?PHP WP_User::get_role_caps怎么用?PHP WP_User::get_role_caps使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_User
的用法示例。
在下文中一共展示了WP_User::get_role_caps方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _convertToEntity
/**
* @param \WP_User $user
* @return UserEntity|null
*/
protected function _convertToEntity(\WP_User $user)
{
$id = (int) $user->ID;
if ($entity = $this->_getEntity($id)) {
return $entity;
}
$caps = $user->get_role_caps();
$isAdmin = (bool) Vars::get($caps['administrator'], false);
$data = new Data(['id' => $id, 'login' => $user->get('user_login'), 'name' => $user->get('display_name'), 'email' => $user->get('user_email'), 'is_admin' => $isAdmin]);
$this->_users[$id] = new UserEntity($data);
return $this->_users[$id];
}
示例2: init_cache
/**
* Builds the cache entries for user groups and capabilities if needed.
* The cache entries are built only if they do not already exist.
* If you want them rebuilt, delete them before calling.
*
* @param array $capability_ids carries the capability ids for the user on return, but only if cache entries have been built; will provide an empty array by default
* @param array $capabilities carries the capabilities for the user on return, but only if cache entries have been built; will provide an empty array by default
* @param array $group_ids carries the group ids for the user on return, but only if cache entries have been built; will provide an empty array by default
*/
private function init_cache(&$capability_ids = null, &$capabilities = null, &$group_ids = null)
{
global $wpdb;
$capabilities = array();
$capability_ids = array();
$group_ids = array();
if ($this->user !== null && Groups_Cache::get(self::GROUP_IDS . $this->user->ID, self::CACHE_GROUP) === null) {
$group_table = _groups_get_tablename("group");
$capability_table = _groups_get_tablename("capability");
$group_capability_table = _groups_get_tablename("group_capability");
$user_group_table = _groups_get_tablename("user_group");
$user_capability_table = _groups_get_tablename("user_capability");
$limit = $wpdb->get_var("SELECT COUNT(*) FROM {$group_table}");
if ($limit === null) {
$limit = 1;
}
// note that limits by blog_id for multisite are
// enforced when a user is added to a blog
$user_groups = $wpdb->get_results($wpdb->prepare("SELECT group_id FROM {$user_group_table} WHERE user_id = %d", Groups_Utility::id($this->user->ID)));
// get all capabilities directly assigned (those granted through
// groups are added below
$user_capabilities = $wpdb->get_results($wpdb->prepare("SELECT c.capability_id, c.capability FROM {$user_capability_table} uc LEFT JOIN {$capability_table} c ON c.capability_id = uc.capability_id WHERE user_id = %d", Groups_Utility::id($this->user->ID)));
if ($user_capabilities) {
foreach ($user_capabilities as $user_capability) {
$capabilities[] = $user_capability->capability;
$capability_ids[] = $user_capability->capability_id;
}
}
if (apply_filters('groups_user_add_role_capabilities', true)) {
// Get all capabilities from the WP_User object.
$role_caps = $this->user->get_role_caps();
if (!empty($role_caps) && is_array($role_caps)) {
$caps = array();
foreach ($role_caps as $role_cap => $has) {
if ($has && !in_array($role_cap, $capabilities)) {
$caps[] = "'" . $role_cap . "'";
}
}
if (!empty($caps)) {
// Retrieve the capabilities and only add those that are
// recognized. Note that this also effectively filters out
// all roles and that this is desired.
if ($role_capabilities = $wpdb->get_results("SELECT capability_id, capability FROM {$capability_table} c WHERE capability IN (" . implode(',', $caps) . ")")) {
foreach ($role_capabilities as $role_capability) {
$capabilities[] = $role_capability->capability;
$capability_ids[] = $role_capability->capability_id;
}
}
}
}
}
// Get all groups the user belongs to directly or through
// inheritance along with their capabilities.
if ($user_groups) {
foreach ($user_groups as $user_group) {
$group_ids[] = Groups_Utility::id($user_group->group_id);
}
if (count($group_ids) > 0) {
$iterations = 0;
$old_group_ids_count = 0;
while ($iterations < $limit && count($group_ids) !== $old_group_ids_count) {
$iterations++;
$old_group_ids_count = count($group_ids);
$id_list = implode(",", $group_ids);
$parent_group_ids = $wpdb->get_results("SELECT parent_id FROM {$group_table} WHERE parent_id IS NOT NULL AND group_id IN ({$id_list})");
if ($parent_group_ids) {
foreach ($parent_group_ids as $parent_group_id) {
$parent_group_id = Groups_Utility::id($parent_group_id->parent_id);
if (!in_array($parent_group_id, $group_ids)) {
$group_ids[] = $parent_group_id;
}
}
}
}
$id_list = implode(",", $group_ids);
$rows = $wpdb->get_results("SELECT {$group_capability_table}.capability_id, {$capability_table}.capability FROM {$group_capability_table} LEFT JOIN {$capability_table} ON {$group_capability_table}.capability_id = {$capability_table}.capability_id WHERE group_id IN ({$id_list})");
if (count($rows) > 0) {
foreach ($rows as $row) {
if (!in_array($row->capability_id, $capability_ids)) {
$capabilities[] = $row->capability;
$capability_ids[] = $row->capability_id;
}
}
}
}
}
Groups_Cache::set(self::CAPABILITIES . $this->user->ID, $capabilities, self::CACHE_GROUP);
Groups_Cache::set(self::CAPABILITY_IDS . $this->user->ID, $capability_ids, self::CACHE_GROUP);
Groups_Cache::set(self::GROUP_IDS . $this->user->ID, $group_ids, self::CACHE_GROUP);
}
}
示例3: foreach
function delete_role($role)
{
global $wp_roles;
if ($_POST['confirm']) {
$defaultrole = get_settings('default_role');
if ($role == $defaultrole) {
//LAZY CODE ALERT! we should give the option of changing the default role
$this->error(__('You cannot delete the default role.', 'role-manager'));
}
//remove the role from $wp_roles
$oldrole = $wp_roles->get_role($role);
$wp_roles->remove_role($role);
//remove the role from all the users
foreach ($this->get_all_user_ids() as $id) {
//we need a global get_all_user_ids() func
$user = new WP_User($id);
//if this role removal would end them up with no roles, assign the default role instead of removing
if (count($user->get_role_caps()) <= 1) {
$user->set_role($defaultrole);
} else {
$user->remove_role($role);
}
}
header('Location: ' . $this->manage_roles_uri() . '&role-deleted=true');
}
}