| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Roles |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* WordPress' default implementation of the Role Manager. |
| 10 |
*/ |
| 11 |
final class WPSEO_Role_Manager_WP extends WPSEO_Abstract_Role_Manager { |
| 12 |
|
| 13 |
/** |
| 14 |
* Adds a role to the system. |
| 15 |
* |
| 16 |
* @param string $role Role to add. |
| 17 |
* @param string $display_name Name to display for the role. |
| 18 |
* @param array $capabilities Capabilities to add to the role. |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
protected function add_role( $role, $display_name, array $capabilities = [] ) { |
| 23 |
$wp_role = get_role( $role ); |
| 24 |
if ( $wp_role ) { |
| 25 |
foreach ( $capabilities as $capability => $grant ) { |
| 26 |
$wp_role->add_cap( $capability, $grant ); |
| 27 |
} |
| 28 |
|
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
add_role( $role, $display_name, $capabilities ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Removes a role from the system. |
| 37 |
* |
| 38 |
* @param string $role Role to remove. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
protected function remove_role( $role ) { |
| 43 |
remove_role( $role ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Formats the capabilities to the required format. |
| 48 |
* |
| 49 |
* @param array $capabilities Capabilities to format. |
| 50 |
* @param bool $enabled Whether these capabilities should be enabled or not. |
| 51 |
* |
| 52 |
* @return array Formatted capabilities. |
| 53 |
*/ |
| 54 |
protected function format_capabilities( array $capabilities, $enabled = true ) { |
| 55 |
// Flip keys and values. |
| 56 |
$capabilities = array_flip( $capabilities ); |
| 57 |
|
| 58 |
// Set all values to $enabled. |
| 59 |
return array_fill_keys( array_keys( $capabilities ), $enabled ); |
| 60 |
} |
| 61 |
} |
| 62 |
|