| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Roles; |
| 4 |
|
| 5 |
use FluentForm\Framework\Support\Arr; |
| 6 |
|
| 7 |
class RolesService |
| 8 |
{ |
| 9 |
public function getRoles($attributes = []) |
| 10 |
{ |
| 11 |
if (!current_user_can('manage_options')) { |
| 12 |
return ([ |
| 13 |
'capability' => [], |
| 14 |
'roles' => [], |
| 15 |
]); |
| 16 |
} |
| 17 |
|
| 18 |
$formatted = $this->getFormattedRoles(); |
| 19 |
|
| 20 |
$capability = get_option('_fluentform_form_permission'); |
| 21 |
|
| 22 |
if (is_string($capability)) { |
| 23 |
$capability = []; |
| 24 |
} |
| 25 |
|
| 26 |
return ([ |
| 27 |
'capability' => $capability, |
| 28 |
'roles' => $formatted, |
| 29 |
]); |
| 30 |
} |
| 31 |
|
| 32 |
public function setCapability($attributes = []) |
| 33 |
{ |
| 34 |
if (current_user_can('manage_options')) { |
| 35 |
$capability = wp_unslash(Arr::get($attributes, 'capability', [])); |
| 36 |
|
| 37 |
foreach ($capability as $item) { |
| 38 |
if ('subscriber' == strtolower($item)) { |
| 39 |
return ([ |
| 40 |
'message' => __('Sorry, you can not give access to the Subscriber role.', 'fluentform'), |
| 41 |
]); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
update_option('_fluentform_form_permission', $capability, 'no'); |
| 46 |
|
| 47 |
return ([ |
| 48 |
'message' => __('Successfully saved the role(s).', 'fluentform'), |
| 49 |
]); |
| 50 |
} else { |
| 51 |
return ([ |
| 52 |
'message' => __('Sorry, You can not update permissions. Only administrators can update permissions', |
| 53 |
'fluentform') |
| 54 |
]); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
private function getFormattedRoles() |
| 59 |
{ |
| 60 |
if (!function_exists('get_editable_roles')) { |
| 61 |
require_once ABSPATH . 'wp-admin/includes/user.php'; |
| 62 |
} |
| 63 |
|
| 64 |
$formatted = []; |
| 65 |
$roles = \get_editable_roles(); |
| 66 |
|
| 67 |
foreach ($roles as $key => $role) { |
| 68 |
if ('administrator' == $key) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
if ('subscriber' != $key) { |
| 72 |
$formatted[] = [ |
| 73 |
'name' => $role['name'], |
| 74 |
'key' => $key, |
| 75 |
]; |
| 76 |
} |
| 77 |
} |
| 78 |
return $formatted; |
| 79 |
} |
| 80 |
} |
| 81 |
|