本文整理汇总了PHP中WP_Roles::get_role方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_Roles::get_role方法的具体用法?PHP WP_Roles::get_role怎么用?PHP WP_Roles::get_role使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_Roles
的用法示例。
在下文中一共展示了WP_Roles::get_role方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: check
/**
* given a permission string, check for access requirements
*
* @param string $str the permission to check
*
* @return boolean true if yes, else false
* @access public
*/
function check($str)
{
// Generic cms 'administer users' role tranlates to 'administrator' WordPress role
$str = $this->translatePermission($str, 'WordPress', array('administer users' => 'administrator'));
if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) {
return FALSE;
}
if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
return TRUE;
}
// for administrators give them all permissions
if (!function_exists('current_user_can')) {
return TRUE;
}
if (current_user_can('super admin') || current_user_can('administrator')) {
return TRUE;
}
// Make string lowercase and convert spaces into underscore
$str = CRM_Utils_String::munge(strtolower($str));
if (is_user_logged_in()) {
// Check whether the logged in user has the capabilitity
if (current_user_can($str)) {
return TRUE;
}
} else {
//check the capabilities of Anonymous user)
$roleObj = new WP_Roles();
if ($roleObj->get_role('anonymous_user') != NULL && array_key_exists($str, $roleObj->get_role('anonymous_user')->capabilities)) {
return TRUE;
}
}
return FALSE;
}
示例2: retrieveList
/**
*
* @return type
*/
public function retrieveList()
{
$response = array('aaData' => array(), 'aaDefault' => 1);
$subject = $this->getSubject();
$roles = new WP_Roles();
if ($subject->getUID() === aam_Control_Subject_Role::UID) {
//prepare list of all capabilities
$caps = array();
foreach ($roles->role_objects as $role) {
$caps = array_merge($caps, $role->capabilities);
}
//init all caps
foreach ($caps as $capability => $grant) {
$response['aaData'][] = array($capability, $subject->hasCapability($capability) ? 1 : 0, $this->getGroup($capability), $this->getHumanText($capability), '');
}
} else {
$role_list = $subject->roles;
$role = $roles->get_role(array_shift($role_list));
foreach ($role->capabilities as $capability => $grant) {
$response['aaData'][] = array($capability, $subject->hasCapability($capability) ? 1 : 0, $this->getGroup($capability), $this->getHumanText($capability), '');
$response['aaDefault'] = $subject->isDefaultCapSet() ? 1 : 0;
}
}
return json_encode($response);
}
示例3: add
/**
*
* @return type
*/
public function add()
{
$name = trim(aam_Core_Request::post('name'));
$roles = new WP_Roles();
$role_id = 'aamrole_' . uniqid();
//if inherited role is set get capabilities from it
$parent = trim(aam_Core_Request::post('inherit'));
if ($parent && $roles->get_role($parent)) {
$caps = $roles->get_role($parent)->capabilities;
} else {
$caps = array();
}
if ($roles->add_role($role_id, $name, $caps)) {
$response = array('status' => 'success', 'role' => $role_id);
} else {
$response = array('status' => 'failure');
}
return json_encode($response);
}
示例4: check
/**
* Given a permission string, check for access requirements
*
* @param string $str
* The permission to check.
*
* @return bool
* true if yes, else false
*/
public function check($str)
{
// Generic cms 'administer users' role tranlates to 'administrator' WordPress role
$str = $this->translatePermission($str, 'WordPress', array('administer users' => 'edit_users'));
if ($str == CRM_Core_Permission::ALWAYS_DENY_PERMISSION) {
return FALSE;
}
if ($str == CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION) {
return TRUE;
}
// CRM-15629
// During some extern/* calls we don't bootstrap CMS hence
// below constants are not set. In such cases, we don't need to
// check permission, hence directly return TRUE
if (!defined('ABSPATH') || !defined('WPINC')) {
require_once 'CRM/Utils/System.php';
CRM_Utils_System::loadBootStrap();
}
require_once ABSPATH . WPINC . '/pluggable.php';
// for administrators give them all permissions
if (!function_exists('current_user_can')) {
return TRUE;
}
if (current_user_can('super admin') || current_user_can('administrator')) {
return TRUE;
}
// Make string lowercase and convert spaces into underscore
$str = CRM_Utils_String::munge(strtolower($str));
if (is_user_logged_in()) {
// Check whether the logged in user has the capabilitity
if (current_user_can($str)) {
return TRUE;
}
} else {
//check the capabilities of Anonymous user)
$roleObj = new WP_Roles();
if ($roleObj->get_role('anonymous_user') != NULL && array_key_exists($str, $roleObj->get_role('anonymous_user')->capabilities)) {
return TRUE;
}
}
return FALSE;
}
示例5: retrieveSubject
/**
* Retrieve Role based on ID
*
* @return WP_Role|null
*
* @access protected
*/
protected function retrieveSubject()
{
$roles = new WP_Roles();
$role = $roles->get_role($this->getId());
if (!is_null($role) && isset($role->capabilities)) {
//add role capability as role id, weird WordPress behavior
//example is administrator capability
$role->capabilities[$this->getId()] = true;
}
return $role;
}
示例6: addCustomRole
/**
* Add custom user role.
*
* @param $roleKey
* @param $roleName
*
* @since 1.0.0
*/
public function addCustomRole($roleKey, $roleName)
{
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
$customerRole = $wp_roles->get_role('customer');
// Copy customer role capabilities
do_action('wwp_action_before_add_custom_role', $roleKey, $roleName, $customerRole->capabilities);
add_role($roleKey, $roleName, $customerRole->capabilities);
do_action('wwp_action_after_add_custom_role', $roleKey, $roleName, $customerRole->capabilities);
}
示例7: retrieveSubject
/**
* Retrieve Role based on ID
*
* @return WP_Role|null
*
* @access protected
*/
protected function retrieveSubject()
{
$roles = new WP_Roles();
$role = $roles->get_role($this->getId());
if (is_null($role)) {
aam_Core_Console::write('Role ' . $this->getId() . ' does not exist');
} elseif (isset($role->capabilities)) {
//add role capability as role id, weird WordPress behavior
//example is administrator capability
$role->capabilities[$this->getId()] = true;
}
return $role;
}
示例8: add_gf_import_capability
protected function add_gf_import_capability()
{
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
$admin_role = $wp_roles->get_role('administrator');
if (!empty($admin_role) && !$admin_role->has_cap('gravityforms_import')) {
$wp_roles->use_db = true;
// save changes to the database
$admin_role->add_cap('gravityforms_import');
}
}
示例9: openid_provider_xrds_simple
/**
* Add XRDS entries for OpenID Server. Entries added will be highly
* dependant on the requested URL and plugin configuration.
*/
function openid_provider_xrds_simple($xrds)
{
global $wp_roles;
if (!$wp_roles) {
$wp_roles = new WP_Roles();
}
$provider_enabled = false;
foreach ($wp_roles->role_names as $key => $name) {
$role = $wp_roles->get_role($key);
if ($role->has_cap('use_openid_provider')) {
$provider_enabled = true;
break;
}
}
if (!$provider_enabled) {
return $xrds;
}
$user = openid_server_requested_user();
if (!$user && get_option('openid_blog_owner')) {
$url_parts = parse_url(get_option('home'));
$script = preg_replace('/index.php$/', '', $_SERVER['SCRIPT_NAME']);
if ('/' . $url_parts['path'] != $script && !is_admin()) {
return $xrds;
}
if (!defined('OPENID_DISALLOW_OWNER') || !OPENID_DISALLOW_OWNER) {
$user = get_userdatabylogin(get_option('openid_blog_owner'));
}
}
if ($user) {
// if user doesn't have capability, bail
$user_object = new WP_User($user->ID);
if (!$user_object->has_cap('use_openid_provider')) {
return $xrds;
}
if (get_usermeta($user->ID, 'openid_delegate')) {
$services = get_usermeta($user->ID, 'openid_delegate_services');
} else {
$services = array(0 => array('Type' => array(array('content' => 'http://specs.openid.net/auth/2.0/signon')), 'URI' => trailingslashit(get_option('siteurl')) . '?openid_server=1', 'LocalID' => get_author_posts_url($user->ID)), 1 => array('Type' => array(array('content' => 'http://openid.net/signon/1.1')), 'URI' => trailingslashit(get_option('siteurl')) . '?openid_server=1', 'openid:Delegate' => get_author_posts_url($user->ID)));
}
} else {
$services = array(array('Type' => array(array('content' => 'http://specs.openid.net/auth/2.0/server')), 'URI' => trailingslashit(get_option('siteurl')) . '?openid_server=1', 'LocalID' => 'http://specs.openid.net/auth/2.0/identifier_select'));
}
if (!empty($services)) {
foreach ($services as $index => $service) {
$name = 'OpenID Provider Service (' . $index . ')';
$xrds = xrds_add_service($xrds, 'main', $name, $service, $index);
}
}
return $xrds;
}
示例10: add_dentix_caps_to_admin
public function add_dentix_caps_to_admin()
{
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
//create a new role, based on the subscriber role
$subscriber = $wp_roles->get_role('subscriber');
$wp_roles->add_role('dentist', __('Dentist', 'dentix'), $subscriber->capabilities);
$caps = array('read', 'read_patient', 'read_private_patients', 'edit_patients', 'edit_private_patients', 'edit_published_patients', 'edit_others_patients', 'publish_patients', 'delete_patients', 'delete_private_patients', 'delete_published_patients', 'delete_others_patients', 'upload_files');
$roles = array(get_role('administrator'), get_role('dentist'));
foreach ($roles as $role) {
foreach ($caps as $cap) {
$role->add_cap($cap);
}
}
}
示例11: add_new_capability
/**
* Add new capability
*
* @global WP_Roles $wp_roles
* @return string
*/
protected function add_new_capability()
{
global $wp_roles;
if (!current_user_can('ure_create_capabilities')) {
return esc_html__('Insufficient permissions to work with User Role Editor', 'user-role-editor');
}
$mess = '';
if (isset($_POST['capability_id']) && $_POST['capability_id']) {
$user_capability = $_POST['capability_id'];
// sanitize user input for security
$valid_name = preg_match('/[A-Za-z0-9_\\-]*/', $user_capability, $match);
if (!$valid_name || $valid_name && $match[0] != $user_capability) {
// some non-alphanumeric charactes found!
return 'Error! ' . esc_html__('Error: Capability name must contain latin characters and digits only!', 'user-role-editor');
}
if ($user_capability) {
$user_capability = strtolower($user_capability);
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
$wp_roles->use_db = true;
$administrator = $wp_roles->get_role('administrator');
if (!$administrator->has_cap($user_capability)) {
$wp_roles->add_cap('administrator', $user_capability);
$mess = sprintf(esc_html__('Capability %s is added successfully', 'user-role-editor'), $user_capability);
} else {
$mess = sprintf('Error! ' . esc_html__('Capability %s exists already', 'user-role-editor'), $user_capability);
}
}
}
return $mess;
}
示例12: add_new_role
/**
* process new role create request
*
* @global WP_Roles $wp_roles
*
* @return string - message about operation result
*
*/
protected function add_new_role()
{
global $wp_roles;
if (!current_user_can('ure_create_roles')) {
return esc_html__('Insufficient permissions to work with User Role Editor', 'user-role-editor');
}
$mess = '';
$this->current_role = '';
if (isset($_POST['user_role_id']) && $_POST['user_role_id']) {
$user_role_id = utf8_decode($_POST['user_role_id']);
// sanitize user input for security
$valid_name = preg_match('/[A-Za-z0-9_\\-]*/', $user_role_id, $match);
if (!$valid_name || $valid_name && $match[0] != $user_role_id) {
// some non-alphanumeric charactes found!
return esc_html__('Error: Role ID must contain latin characters, digits, hyphens or underscore only!', 'user-role-editor');
}
$numeric_name = preg_match('/[0-9]*/', $user_role_id, $match);
if ($numeric_name && $match[0] == $user_role_id) {
// numeric name discovered
return esc_html__('Error: WordPress does not support numeric Role name (ID). Add latin characters to it.', 'user-role-editor');
}
if ($user_role_id) {
$user_role_name = isset($_POST['user_role_name']) ? $_POST['user_role_name'] : false;
if (!empty($user_role_name)) {
$user_role_name = sanitize_text_field($user_role_name);
} else {
$user_role_name = $user_role_id;
// as user role name is empty, use user role ID instead
}
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
if (isset($wp_roles->roles[$user_role_id])) {
return sprintf('Error! ' . esc_html__('Role %s exists already', 'user-role-editor'), $user_role_id);
}
$user_role_id = strtolower($user_role_id);
$this->current_role = $user_role_id;
$user_role_copy_from = isset($_POST['user_role_copy_from']) ? $_POST['user_role_copy_from'] : false;
if (!empty($user_role_copy_from) && $user_role_copy_from != 'none' && $wp_roles->is_role($user_role_copy_from)) {
$role = $wp_roles->get_role($user_role_copy_from);
$capabilities = $this->remove_caps_not_allowed_for_single_admin($role->capabilities);
} else {
$capabilities = array('read' => true, 'level_0' => true);
}
// add new role to the roles array
$result = add_role($user_role_id, $user_role_name, $capabilities);
if (!isset($result) || empty($result)) {
$mess = 'Error! ' . esc_html__('Error is encountered during new role create operation', 'user-role-editor');
} else {
$mess = sprintf(esc_html__('Role %s is created successfully', 'user-role-editor'), $user_role_name);
}
}
}
return $mess;
}
示例13: postProcess
/**
* Function to process the form
*
* @access public
* @return void
*/
public function postProcess()
{
$params = $this->controller->exportValues($this->_name);
$permissionsArray = self::getPermissionArray();
// Function to get Wordpress roles
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
foreach ($wp_roles->role_names as $role => $name) {
$roleObj = $wp_roles->get_role($role);
//Remove all civicrm capabilities for the role, as there may be some capabilities checkbox unticked
foreach ($permissionsArray as $key => $capability) {
$roleObj->remove_cap($key);
}
//Add the selected wordpress capabilities for the role
$rolePermissions = $params[$role];
if (!empty($rolePermissions)) {
foreach ($rolePermissions as $key => $capability) {
$roleObj->add_cap($key);
}
}
}
// FIXME
// Changed the 'access_civicrm_nav_link' capability in civicrm.php file
// But for some reason, if i remove 'Access CiviCRM' administrator and save, it is showing
// 'You do not have sufficient permissions to access this page'
// which should not happen for Super Admin and Administrators, as checking permissions for Super
// Admin and Administrators always gives TRUE
wp_civicrm_capability();
CRM_Core_Session::setStatus("", ts('Wordpress Access Control Updated'), "success");
// rebuild the menus to comply with the new permisssions/capabilites
CRM_Core_Invoke::rebuildMenuAndCaches();
CRM_Utils_System::redirect('admin.php?page=CiviCRM&q=civicrm/admin/access&reset=1');
CRM_Utils_System::civiExit();
}
示例14: removeSuperAdminRole
protected function removeSuperAdminRole()
{
//update the role capabilities and remove super admin role
$roles = new WP_Roles();
//get all capabilities first and merge them in one array
$capabilities = array();
foreach ($roles->role_objects as $role) {
$capabilities = array_merge($capabilities, $role->capabilities);
}
if (count($capabilities)) {
//update administrator capability role
if ($admin = $roles->get_role('administrator')) {
foreach ($capabilities as $capability => $grand) {
$admin->add_cap($capability);
}
} else {
$roles->add_role('administrator', 'Administrator', $capabilities);
}
//remove Super Admin Role
$roles->remove_role('super_admin');
}
}
示例15: array
/**
* 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
* @uses $wp_roles
* @access public
*/
function get_role_caps()
{
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new 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);
}