| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Capabilities |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* VIP implementation of the Capability Manager. |
| 10 |
*/ |
| 11 |
final class WPSEO_Capability_Manager_VIP extends WPSEO_Abstract_Capability_Manager { |
| 12 |
|
| 13 |
/** |
| 14 |
* Adds the registered capabilities to the system. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function add() { |
| 19 |
$role_capabilities = []; |
| 20 |
foreach ( $this->capabilities as $capability => $roles ) { |
| 21 |
$role_capabilities = $this->get_role_capabilities( $role_capabilities, $capability, $roles ); |
| 22 |
} |
| 23 |
|
| 24 |
foreach ( $role_capabilities as $role => $capabilities ) { |
| 25 |
wpcom_vip_add_role_caps( $role, $capabilities ); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Removes the registered capabilities from the system |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function remove() { |
| 35 |
// Remove from any role it has been added to. |
| 36 |
$roles = wp_roles()->get_names(); |
| 37 |
$roles = array_keys( $roles ); |
| 38 |
|
| 39 |
$role_capabilities = []; |
| 40 |
foreach ( array_keys( $this->capabilities ) as $capability ) { |
| 41 |
// Allow filtering of roles. |
| 42 |
$role_capabilities = $this->get_role_capabilities( $role_capabilities, $capability, $roles ); |
| 43 |
} |
| 44 |
|
| 45 |
foreach ( $role_capabilities as $role => $capabilities ) { |
| 46 |
wpcom_vip_remove_role_caps( $role, $capabilities ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the roles which the capability is registered on. |
| 52 |
* |
| 53 |
* @param array $role_capabilities List of all roles with their capabilities. |
| 54 |
* @param string $capability Capability to filter roles for. |
| 55 |
* @param array $roles List of default roles. |
| 56 |
* |
| 57 |
* @return array List of capabilities. |
| 58 |
*/ |
| 59 |
protected function get_role_capabilities( $role_capabilities, $capability, $roles ) { |
| 60 |
// Allow filtering of roles. |
| 61 |
$filtered_roles = $this->filter_roles( $capability, $roles ); |
| 62 |
|
| 63 |
foreach ( $filtered_roles as $role ) { |
| 64 |
if ( ! isset( $add_role_caps[ $role ] ) ) { |
| 65 |
$role_capabilities[ $role ] = []; |
| 66 |
} |
| 67 |
|
| 68 |
$role_capabilities[ $role ][] = $capability; |
| 69 |
} |
| 70 |
|
| 71 |
return $role_capabilities; |
| 72 |
} |
| 73 |
} |
| 74 |
|