NewRoleAction.php
1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php namespace MeowCrew\RoleAndCustomerBasedPricing\RoleManagement\Actions;
use Exception;
class NewRoleAction extends RoleManagementPageAction {
public function handle() {
$roleName = $this->getRoleName();
$inheritRole = $this->getInheritedRole();
$newCapabilities = array();
if ( $inheritRole ) {
$roles = wp_roles()->roles;
$role = array_key_exists( $inheritRole, $roles ) ? $roles[ $inheritRole ] : false;
if ( ! empty( $role ) ) {
$newCapabilities = $role['capabilities'];
}
}
add_role( $roleName, $roleName, $newCapabilities );
$this->getContainer()->getAdminNotifier()->flash( __( 'The role has been added successfully.', 'role-and-customer-based-pricing-for-woocommerce' ), 'success', true );
wp_redirect( wp_get_referer() );
}
public function validate() {
if ( ! $this->getRoleName() ) {
throw new Exception( __( 'Role name is required.', 'role-and-customer-based-pricing-for-woocommerce' ) );
}
$roles = wp_roles()->roles;
if ( $this->getInheritedRole() && ! array_key_exists( $this->getInheritedRole(), $roles ) ) {
throw new Exception( __( 'Invalid inherited role.', 'role-and-customer-based-pricing-for-woocommerce' ) );
}
parent::validate();
}
public function getRoleName() {
return isset( $_REQUEST['role_name'] ) ? sanitize_text_field( $_REQUEST['role_name'] ) : false;
}
public function getInheritedRole() {
return isset( $_REQUEST['inherited_role'] ) ? sanitize_text_field( $_REQUEST['inherited_role'] ) : false;
}
public function getActionSlug() {
return 'rcbp_new_role__action';
}
}