本文整理汇总了PHP中wp_roles函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_roles函数的具体用法?PHP wp_roles怎么用?PHP wp_roles使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_roles函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getUserRoles
/**
* Get list of user roles
*
* @param array $roles
*
* @return array
*
* @access protected
*/
protected function getUserRoles($roles)
{
$response = array();
$names = wp_roles()->get_names();
foreach ($roles as $role) {
if (isset($names[$role])) {
$response[] = translate_user_role($names[$role]);
}
}
return $response;
}
示例2: count_users
/**
* Count number of users who have each of the user roles.
*
* Assumes there are neither duplicated nor orphaned capabilities meta_values.
* Assumes role names are unique phrases. Same assumption made by WP_User_Query::prepare_query()
* Using $strategy = 'time' this is CPU-intensive and should handle around 10^7 users.
* Using $strategy = 'memory' this is memory-intensive and should handle around 10^5 users, but see WP Bug #12257.
*
* @since 3.0.0
*
* @global wpdb $wpdb
*
* @param string $strategy 'time' or 'memory'
* @return array Includes a grand total and an array of counts indexed by role strings.
*/
function count_users($strategy = 'time')
{
global $wpdb;
// Initialize
$id = get_current_blog_id();
$blog_prefix = $wpdb->get_blog_prefix($id);
$result = array();
if ('time' == $strategy) {
$avail_roles = wp_roles()->get_names();
// Build a CPU-intensive query that will return concise information.
$select_count = array();
foreach ($avail_roles as $this_role => $name) {
$select_count[] = $wpdb->prepare("COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like('"' . $this_role . '"') . '%');
}
$select_count = implode(', ', $select_count);
// Add the meta_value index to the selection list, then run the query.
$row = $wpdb->get_row("SELECT {$select_count}, COUNT(*) FROM {$wpdb->usermeta} WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N);
// Run the previous loop again to associate results with role names.
$col = 0;
$role_counts = array();
foreach ($avail_roles as $this_role => $name) {
$count = (int) $row[$col++];
if ($count > 0) {
$role_counts[$this_role] = $count;
}
}
// Get the meta_value index from the end of the result set.
$total_users = (int) $row[$col];
$result['total_users'] = $total_users;
$result['avail_roles'] =& $role_counts;
} else {
$avail_roles = array();
$users_of_blog = $wpdb->get_col("SELECT meta_value FROM {$wpdb->usermeta} WHERE meta_key = '{$blog_prefix}capabilities'");
foreach ($users_of_blog as $caps_meta) {
$b_roles = maybe_unserialize($caps_meta);
if (!is_array($b_roles)) {
continue;
}
foreach ($b_roles as $b_role => $val) {
if (isset($avail_roles[$b_role])) {
$avail_roles[$b_role]++;
} else {
$avail_roles[$b_role] = 1;
}
}
}
$result['total_users'] = count($users_of_blog);
$result['avail_roles'] =& $avail_roles;
}
return $result;
}
示例3: get_roles
public function get_roles()
{
$roles = array();
foreach (wp_roles()->roles as $k => $role) {
$roles[$k] = translate_user_role($role['name']);
}
return $roles;
}
示例4: add_roles_on_plugin_activation
function add_roles_on_plugin_activation()
{
add_role('guard_role', 'Opiekun', array('read' => false));
add_role('teacher_role', 'Nauczyciel', array('read' => true));
$roles = array('editor', 'author', 'contributor', 'subscriber');
foreach ($roles as $role) {
wp_roles()->remove_role($role);
}
}
示例5: test_get_users_with_no_role_matches_on_role_name
/**
* Role comparison must be done on role name, not role display name.
*
* @ticket 38234
*/
public function test_get_users_with_no_role_matches_on_role_name()
{
// Create a role with a display name which would not match the role name
// in a case-insentive SQL query.
wp_roles()->add_role('somerole', 'Some role display name');
$someuser = self::factory()->user->create(array('role' => 'somerole'));
$users = wp_get_users_with_no_role();
wp_roles()->remove_role('somerole');
$this->assertEmpty($users);
}
示例6: single_row
/**
* Generate HTML for a single row on the users.php admin panel.
*
* @since 3.1.0
* @since 4.2.0 The `$style` argument was deprecated.
* @access public
*
* @param object $user_object The current user object.
* @param string $style Deprecated. Not used.
* @param string $role Optional. Key for the $wp_roles array. Default empty.
* @param int $numposts Optional. Post count to display for this user. Defaults
* to zero, as in, a new user has made zero posts.
* @return string Output for a single row.
*/
public function single_row($user_object, $style = '', $role = '', $numposts = 0)
{
$wp_roles = wp_roles();
if (!$user_object instanceof WP_User) {
$user_object = get_userdata((int) $user_object);
}
$user_object->filter = 'display';
$email = $user_object->user_email;
if ($this->is_site_users) {
$url = "site-users.php?id={$this->site_id}&";
} else {
$url = 'users.php?';
}
$checkbox = '';
// Check if the user for this row is editable
if (current_user_can('list_users')) {
// Set up the user editing link
$edit_link = esc_url(add_query_arg('wp_http_referer', urlencode(wp_unslash($_SERVER['REQUEST_URI'])), get_edit_user_link($user_object->ID)));
// Set up the hover actions for this user
$actions = array();
if (current_user_can('edit_user', $user_object->ID)) {
$edit = "<strong><a href=\"{$edit_link}\">{$user_object->user_login}</a></strong><br />";
$actions['edit'] = '<a href="' . $edit_link . '">' . __('Edit') . '</a>';
} else {
$edit = "<strong>{$user_object->user_login}</strong><br />";
}
if (!is_multisite() && get_current_user_id() != $user_object->ID && current_user_can('delete_user', $user_object->ID)) {
$actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url("users.php?action=delete&user={$user_object->ID}", 'bulk-users') . "'>" . __('Delete') . "</a>";
}
if (is_multisite() && get_current_user_id() != $user_object->ID && current_user_can('remove_user', $user_object->ID)) {
$actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url($url . "action=remove&user={$user_object->ID}", 'bulk-users') . "'>" . __('Remove') . "</a>";
}
/**
* Filter the action links displayed under each user in the Users list table.
*
* @since 2.8.0
*
* @param array $actions An array of action links to be displayed.
* Default 'Edit', 'Delete' for single site, and
* 'Edit', 'Remove' for Multisite.
* @param WP_User $user_object WP_User object for the currently-listed user.
*/
$actions = apply_filters('user_row_actions', $actions, $user_object);
// Set up the checkbox ( because the user is editable, otherwise it's empty )
$checkbox = '<label class="screen-reader-text" for="user_' . $user_object->ID . '">' . sprintf(__('Select %s'), $user_object->user_login) . '</label>' . "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='{$role}' value='{$user_object->ID}' />";
} else {
$edit = '<strong>' . $user_object->user_login . '</strong>';
}
$role_name = isset($wp_roles->role_names[$role]) ? translate_user_role($wp_roles->role_names[$role]) : __('None');
$avatar = get_avatar($user_object->ID, 32);
$r = "<tr id='user-{$user_object->ID}'>";
list($columns, $hidden, $sortable, $primary) = $this->get_column_info();
foreach ($columns as $column_name => $column_display_name) {
$classes = "{$column_name} column-{$column_name}";
if ($primary === $column_name) {
$classes .= ' has-row-actions column-primary';
}
if ('posts' === $column_name) {
$classes .= ' num';
// Special case for that column
}
if (in_array($column_name, $hidden)) {
$classes .= ' hidden';
}
$data = 'data-colname="' . wp_strip_all_tags($column_display_name) . '"';
$attributes = "class='{$classes}' {$data}";
if ('cb' === $column_name) {
$r .= "<th scope='row' class='check-column'>{$checkbox}</th>";
} else {
$r .= "<td {$attributes}>";
switch ($column_name) {
case 'username':
$r .= "{$avatar} {$edit}";
break;
case 'name':
$r .= "{$user_object->first_name} {$user_object->last_name}";
break;
case 'email':
$r .= "<a href='mailto:{$email}'>{$email}</a>";
break;
case 'role':
$r .= $role_name;
break;
case 'posts':
if ($numposts > 0) {
$r .= "<a href='edit.php?author={$user_object->ID}' class='edit'>";
//.........这里部分代码省略.........
示例7: get_role_caps
/**
* Retrieve all of the role capabilities and merge with individual capabilities.
*
* All of the capabilities of the roles the user belongs to are merged with
* the users individual roles. This also means that the user can be denied
* specific roles that their role might have, but the specific user isn't
* granted permission to.
*
* @since 2.0.0
* @access public
*
* @return array List of all capabilities for the user.
*/
public function get_role_caps()
{
$wp_roles = wp_roles();
//Filter out caps that are not role names and assign to $this->roles
if (is_array($this->caps)) {
$this->roles = array_filter(array_keys($this->caps), array($wp_roles, 'is_role'));
}
//Build $allcaps from role caps, overlay user's $caps
$this->allcaps = array();
foreach ((array) $this->roles as $role) {
$the_role = $wp_roles->get_role($role);
$this->allcaps = array_merge((array) $this->allcaps, (array) $the_role->capabilities);
}
$this->allcaps = array_merge((array) $this->allcaps, (array) $this->caps);
return $this->allcaps;
}
示例8: restore_current_blog
/**
* Restore the current blog, after calling switch_to_blog()
*
* @see switch_to_blog()
* @since MU
*
* @global wpdb $wpdb
* @global array $_wp_switched_stack
* @global int $blog_id
* @global bool $switched
* @global string $table_prefix
* @global WP_Object_Cache $wp_object_cache
*
* @return bool True on success, false if we're already on the current blog
*/
function restore_current_blog()
{
global $wpdb;
if (empty($GLOBALS['_wp_switched_stack'])) {
return false;
}
$blog = array_pop($GLOBALS['_wp_switched_stack']);
if ($GLOBALS['blog_id'] == $blog) {
/** This filter is documented in libs/ms-blogs.php */
do_action('switch_blog', $blog, $blog);
// If we still have items in the switched stack, consider ourselves still 'switched'
$GLOBALS['switched'] = !empty($GLOBALS['_wp_switched_stack']);
return true;
}
$wpdb->set_blog_id($blog);
$prev_blog_id = $GLOBALS['blog_id'];
$GLOBALS['blog_id'] = $blog;
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
if (function_exists('wp_cache_switch_to_blog')) {
wp_cache_switch_to_blog($blog);
} else {
global $wp_object_cache;
if (is_object($wp_object_cache) && isset($wp_object_cache->global_groups)) {
$global_groups = $wp_object_cache->global_groups;
} else {
$global_groups = false;
}
wp_cache_init();
if (function_exists('wp_cache_add_global_groups')) {
if (is_array($global_groups)) {
wp_cache_add_global_groups($global_groups);
} else {
wp_cache_add_global_groups(array('users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks'));
}
wp_cache_add_non_persistent_groups(array('comment', 'counts', 'plugins'));
}
}
if (did_action('init')) {
wp_roles()->reinit();
$current_user = wp_get_current_user();
$current_user->for_blog($blog);
}
/** This filter is documented in libs/ms-blogs.php */
do_action('switch_blog', $blog, $prev_blog_id);
// If we still have items in the switched stack, consider ourselves still 'switched'
$GLOBALS['switched'] = !empty($GLOBALS['_wp_switched_stack']);
return true;
}
示例9: upgrade_440
/**
* Executes changes made in WordPress 4.4.0.
*
* @ignore
* @since 4.4.0
*
* @global int $wp_current_db_version Current version.
* @global wpdb $wpdb WordPress database abstraction object.
*/
function upgrade_440()
{
global $wp_current_db_version, $wpdb;
if ($wp_current_db_version < 34030) {
$wpdb->query("ALTER TABLE {$wpdb->options} MODIFY option_name VARCHAR(191)");
}
// Remove the unused 'add_users' role.
$roles = wp_roles();
foreach ($roles->role_objects as $role) {
if ($role->has_cap('add_users')) {
$role->remove_cap('add_users');
}
}
}
示例10: get_views
/**
* Return an associative array listing all the views that can be used
* with this table.
*
* Provides a list of roles and user count for that role for easy
* filtering of the user table.
*
* @since 3.1.0
* @access protected
*
* @global string $role
*
* @return array An array of HTML links, one for each view.
*/
protected function get_views()
{
global $role;
$wp_roles = wp_roles();
if ($this->is_site_users) {
$url = 'site-users.php?id=' . $this->site_id;
switch_to_blog($this->site_id);
$users_of_blog = count_users();
restore_current_blog();
} else {
$url = 'users.php';
$users_of_blog = count_users();
}
$total_users = $users_of_blog['total_users'];
$avail_roles =& $users_of_blog['avail_roles'];
unset($users_of_blog);
$class = empty($role) ? ' class="current"' : '';
$role_links = array();
$role_links['all'] = "<a href='{$url}'{$class}>" . sprintf(_nx('All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users'), number_format_i18n($total_users)) . '</a>';
foreach ($wp_roles->get_names() as $this_role => $name) {
if (!isset($avail_roles[$this_role])) {
continue;
}
$class = '';
if ($this_role === $role) {
$class = ' class="current"';
}
$name = translate_user_role($name);
/* translators: User role name with count */
$name = sprintf(__('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n($avail_roles[$this_role]));
$role_links[$this_role] = "<a href='" . esc_url(add_query_arg('role', $this_role, $url)) . "'{$class}>{$name}</a>";
}
if (!empty($avail_roles['none'])) {
$class = '';
if ('none' === $role) {
$class = ' class="current"';
}
$name = __('No role');
/* translators: User role name with count */
$name = sprintf(__('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n($avail_roles['none']));
$role_links['none'] = "<a href='" . esc_url(add_query_arg('role', 'none', $url)) . "'{$class}>{$name}</a>";
}
return $role_links;
}
示例11: get_role_display_name
/**
* Gets the friendly display name of a user role
*
* @param WP_Role $role
*
* @return array|WP_Roles
*/
public static function get_role_display_name(WP_Role $role)
{
$displayName = wp_roles();
$displayName = $displayName->get_names();
$displayName = $displayName[$role->name];
return $displayName;
}
示例12: column_post_author
function column_post_author($item)
{
$a_name = get_the_author_meta('display_name', $item['post_author']);
//$a_role = get_the_author_meta('roles',$item['post_author'])[0];
$a_role = wp_roles()->role_names[get_the_author_meta('roles', $item['post_author'])[0]];
$display = $a_name . '<br/>(' . $a_role . ')';
$actions = array('view' => sprintf("<a href='%s'>作者資訊</a>", get_edit_profile_url($item['post_author'])));
return sprintf('%1$s %2$s', $display, $this->row_actions($actions));
}
示例13: upgrade_440
/**
* Executes changes made in WordPress 4.4.0.
*
* @since 4.4.0
*
* @global int $wp_current_db_version Current version.
* @global wpdb $wpdb WordPress database abstraction object.
*/
function upgrade_440()
{
global $wp_current_db_version, $wpdb;
if ($wp_current_db_version < 34030) {
$wpdb->query("DROP INDEX {$wpdb->options}" . "_UK1 ON {$wpdb->options}");
$wpdb->query("ALTER TABLE {$wpdb->options} ALTER COLUMN option_name NVARCHAR(191) NOT NULL");
$wpdb->query("CREATE UNIQUE INDEX {$wpdb->options}" . "_UK1 on {$wpdb->options} (option_name)");
$wpdb->query("CREATE TABLE {$wpdb->termmeta} (meta_id int NOT NULL identity(1,1), term_id int NOT NULL default 0, meta_key nvarchar(255) default NULL, meta_value nvarchar(max), CONSTRAINT {$wpdb->termmeta}" . "_PK PRIMARY KEY NONCLUSTERED (meta_id))");
$wpdb->query("CREATE CLUSTERED INDEX {$wpdb->termmeta}" . "_CLU1 on {$wpdb->termmeta} (term_id)");
$wpdb->query("CREATE INDEX {$wpdb->termmeta}" . "_IDX2 on {$wpdb->termmeta} (meta_key)");
}
// Remove the unused 'add_users' role.
$roles = wp_roles();
foreach ($roles->role_objects as $role) {
if ($role->has_cap('add_users')) {
$role->remove_cap('add_users');
}
}
}
示例14: collectAll
/**
* Collect caps.
*
* @since 160524 Cap utils.
*
* @param bool $no_cache Bypass cache?
*
* @return array All collected caps.
*/
public function collectAll(bool $no_cache = false) : array
{
if (($collection =& $this->cacheKey(__FUNCTION__)) !== null && !$no_cache) {
return $collection;
// Already cached this.
}
$collection = [];
// Initialize.
foreach (wp_roles()->roles as $_role_id => $_role) {
foreach (array_keys($_role['capabilities'] ?? []) as $_role_cap) {
$collection[$_role_cap] = $_role_cap;
}
}
// unset($_role_id, $_role, $_role_cap); // Housekeeping.
$collection = array_merge($collection, $this->forRole('super_admin', $no_cache));
foreach (get_post_types([], 'objects') as $_post_type => $_post_type_object) {
foreach ($_post_type_object->cap ?? [] as $_core_cap => $_post_type_cap) {
if (!in_array($_core_cap, ['read_post', 'edit_post', 'delete_post'], true)) {
// ↑ Do not include post meta caps; see: <http://jas.xyz/1XN7IKd>
$collection[$_core_cap] = $_core_cap;
$collection[$_post_type_cap] = $_post_type_cap;
}
}
// unset($_core_cap, $_post_type_cap);
}
// unset($_post_type, $_post_type_object); // Housekeeping.
foreach (get_taxonomies([], 'objects') as $_taxonomy => $_taxonomy_object) {
foreach ($_taxonomy_object->cap ?? [] as $_core_cap => $_taxonomy_cap) {
$collection[$_core_cap] = $_core_cap;
$collection[$_taxonomy_cap] = $_taxonomy_cap;
}
// unset($_core_cap, $_taxonomy_cap);
}
// unset($_taxonomy, $_taxonomy_object); // Housekeeping.
asort($collection, SORT_NATURAL);
return $collection;
}
示例15: update_role
/**
* Updates a user's role if their current one doesn't match the attributes provided by the IdP
*
* @return string
*/
private function update_role()
{
$attrs = $this->saml->getAttributes();
if (array_key_exists($this->settings->get_attribute('groups'), $attrs)) {
foreach (wp_roles()->roles as $role_name => $role_meta) {
if (!isset($role) && in_array($this->settings->get_group($role_name), $attrs[$this->settings->get_attribute('groups')])) {
$role = $role_name;
}
}
if (isset($role)) {
} elseif ($this->settings->get_allow_unlisted_users()) {
$role = 'subscriber';
} else {
$role = false;
}
} else {
$role = false;
}
$user = get_user_by('login', $attrs[$this->settings->get_attribute('username')][0]);
if ($user) {
$user->set_role($role);
}
return $role;
}