-
Notifications
You must be signed in to change notification settings - Fork 1
Custom Role
Loïc Antignac edited this page May 24, 2022
·
9 revisions
(adding, removing, updating custom roles)
Once the the plugin is created, the declaration(s) can be added:
<?php
/**
* Plugin Name: Example Custom Role
* Author: My Name
* Text Domain: wax-custom-content
* Domain Path: /languages
*/
defined( 'ABSPATH' ) || exit;
// If you already have an autoload (for example if you are on Bedrock) you can remove these 3 lines, otherwise leave them.
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php';
}
use Webaxones\Core\Entities\Entities;
Webaxones\Core\Library::init( 'webaxones-content' );
/**
* Add Custom role: Owner
*/
$declarations[] = [
'entity' => 'Webaxones\Core\Role\Role',
'labels' => [
'role_name' => _x( 'Owner', 'Custom role name', 'webaxones-content' ),
],
'settings' => [
'slug' => 'owner',
'action' => 'add', /*'remove', 'update'*/
'role_to_clone_slug' => 'administrator',
'capabilities_to_remove' => [
'manage_options',
'switch_themes',
'remove_users',
'activate_plugins',
'delete_others_pages',
'delete_site',
'delete_pages',
'delete_private_pages',
'delete_published_pages',
'delete_others_posts',
'delete_posts',
'delete_private_posts',
'edit_theme_options',
'export',
'import',
'edit_private_pages',
'edit_private_posts',
'promote_users',
'customize',
],
],
];
/**
* Remove Custom role: Test
*/
$declarations[] = [
'entity' => 'Webaxones\Core\Role\Role',
'labels' => [],
'settings' => [
'slug' => 'test',
'action' => 'remove',
'role_to_clone_slug' => '',
'capabilities_to_remove' => [],
],
];
/**
* Update Custom role: Owner
*/
$declarations[] = [
'entity' => 'Webaxones\Core\Role\Role',
'labels' => [
'role_name' => _x( 'Owner', 'Custom role name', 'webaxones-content' ),
],
'settings' => [
'slug' => 'owner',
'action' => 'update',
'role_to_clone_slug' => 'administrator',
'capabilities_to_remove' => [
'manage_options',
'switch_themes',
'remove_users',
'promote_users',
'customize',
],
],
];
Entities::process( $declarations );As for all entities, settings starts with the slug key which identifies the entity and must be unique.
Roles can be added, updated or removed.
There is therefore an action key in settings which can have the following values: add, remove, update.
The addition (and update) is done by cloning an existing role and specifying which capabilities to remove from the new role.
Roles being saved in the database, a check of their existence is made when adding, updating and deleting.