185 lines
4.7 KiB
PHP
185 lines
4.7 KiB
PHP
<?php
|
|
|
|
class SQ_Models_RoleManager {
|
|
|
|
public $roles;
|
|
|
|
public function __construct() {
|
|
add_action('admin_init', array($this, 'addSQRoles'));
|
|
}
|
|
|
|
/**
|
|
* Get all the Squirrly Caps
|
|
* @param $role
|
|
* @return array
|
|
*/
|
|
public function getSQCaps($role = '') {
|
|
$caps = array();
|
|
|
|
$caps['sq_seo_author'] = array(
|
|
'sq_manage_snippet' => true,
|
|
'sq_manage_snippets' => false,
|
|
'sq_manage_settings' => false,
|
|
'sq_manage_focuspages' => false,
|
|
);
|
|
|
|
$caps['sq_seo_editor'] = array(
|
|
'sq_manage_snippet' => true,
|
|
'sq_manage_snippets' => true,
|
|
'sq_manage_settings' => false,
|
|
'sq_manage_focuspages' => true,
|
|
);
|
|
|
|
$caps['sq_seo_admin'] = array(
|
|
'sq_manage_snippet' => true,
|
|
'sq_manage_snippets' => true,
|
|
'sq_manage_settings' => true,
|
|
'sq_manage_focuspages' => true,
|
|
);
|
|
|
|
$caps = array_filter($caps);
|
|
|
|
if (isset($caps[$role])) {
|
|
return $caps[$role];
|
|
}
|
|
|
|
return $caps;
|
|
}
|
|
|
|
/**
|
|
* Register Squirrly Roles and Caps
|
|
* in case they don't exists
|
|
*/
|
|
public function addSQRoles() {
|
|
global $wp_roles;
|
|
|
|
if (!$wp_roles->is_role('sq_seo_editor') || !$wp_roles->is_role('sq_seo_admin')) {
|
|
$this->updateSQCap('sq_seo_author', 'author');
|
|
$this->updateSQCap('sq_seo_admin', 'editor');
|
|
$this->updateSQCap('sq_seo_admin', 'administrator');
|
|
|
|
//get all Squirrly roles and caps
|
|
$this->addSQRole('sq_seo_editor', __('Squirrly SEO Editor', _SQ_PLUGIN_NAME_), 'editor');
|
|
$this->addSQRole('sq_seo_admin', __('Squirrly SEO Admin', _SQ_PLUGIN_NAME_), 'editor');
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove Squirrly Roles and Caps
|
|
*/
|
|
public function removeSQRoles() {
|
|
global $wp_roles;
|
|
|
|
//get all Squirrly roles and caps
|
|
$sqcaps = $this->getSQCaps();
|
|
|
|
if (!empty($sqcaps)) {
|
|
foreach (array_keys($sqcaps) as $role) {
|
|
if ($wp_roles->is_role($role)) {
|
|
$this->removeRole($role);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public function removeSQCaps() {
|
|
global $wp_roles;
|
|
|
|
$this->removeCap('author', $this->getSQCaps('sq_seo_author'));
|
|
$this->removeCap('editor', $this->getSQCaps('sq_seo_editor'));
|
|
$this->removeCap('administrator', $this->getSQCaps('sq_seo_admin'));
|
|
|
|
//get all Squirrly roles and caps
|
|
$sqcaps = $this->getSQCaps();
|
|
if (!empty($sqcaps)) {
|
|
foreach (array_keys($sqcaps) as $role) {
|
|
if ($wp_roles->is_role($role)) {
|
|
$this->removeCap($role, $this->getSQCaps($role));
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add Squirrly Role and Caps
|
|
* @param $sqrole
|
|
* @param $title
|
|
* @param $wprole
|
|
*/
|
|
public function addSQRole($sqrole, $title, $wprole) {
|
|
$wpcaps = $this->getWpCaps($wprole);
|
|
$sqcaps = $this->getSQCaps($sqrole);
|
|
|
|
$this->addRole($sqrole, $title, array_merge($wpcaps, $sqcaps));
|
|
}
|
|
|
|
/**
|
|
* Update the Squirlly Caps into WP Roles
|
|
* @param $sqrole
|
|
* @param $wprole
|
|
*/
|
|
public function updateSQCap($sqrole, $wprole) {
|
|
$sqcaps = $this->getSQCaps($sqrole);
|
|
|
|
$this->addCap($wprole, $sqcaps);
|
|
}
|
|
|
|
/**
|
|
* Add a role into WP
|
|
* @param $name
|
|
* @param $title
|
|
* @param $capabilities
|
|
*/
|
|
public function addRole($name, $title, $capabilities) {
|
|
add_role($name, $title, $capabilities);
|
|
}
|
|
|
|
/**
|
|
* Add a cap into WP for a role
|
|
* @param $name
|
|
* @param $capabilities
|
|
*/
|
|
public function addCap($name, $capabilities) {
|
|
$role = get_role($name);
|
|
|
|
foreach ($capabilities as $capability => $grant) {
|
|
$role->add_cap($capability, $grant);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the caps for a role
|
|
* @param $name
|
|
* @param $capabilities
|
|
*/
|
|
public function removeCap($name, $capabilities) {
|
|
$role = get_role($name);
|
|
if ($role) {
|
|
foreach ($capabilities as $capability => $grant) {
|
|
$role->remove_cap($capability);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the role
|
|
* @param $name
|
|
*/
|
|
public function removeRole($name) {
|
|
remove_role($name);
|
|
}
|
|
|
|
public function getWpCaps($role) {
|
|
|
|
if ($wprole = get_role($role)) {
|
|
return $wprole->capabilities;
|
|
}
|
|
|
|
return array();
|
|
}
|
|
|
|
} |