| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Capabilities |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Abstract Capability Manager shared code. |
| 10 |
*/ |
| 11 |
abstract class WPSEO_Abstract_Capability_Manager implements WPSEO_Capability_Manager { |
| 12 |
|
| 13 |
/** |
| 14 |
* Registered capabilities. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $capabilities = []; |
| 19 |
|
| 20 |
/** |
| 21 |
* Registers a capability. |
| 22 |
* |
| 23 |
* @param string $capability Capability to register. |
| 24 |
* @param array $roles Roles to add the capability to. |
| 25 |
* @param bool $overwrite Optional. Use add or overwrite as registration method. |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function register( $capability, array $roles, $overwrite = false ) { |
| 30 |
if ( $overwrite || ! isset( $this->capabilities[ $capability ] ) ) { |
| 31 |
$this->capabilities[ $capability ] = $roles; |
| 32 |
|
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Combine configurations. |
| 37 |
$this->capabilities[ $capability ] = array_merge( $roles, $this->capabilities[ $capability ] ); |
| 38 |
|
| 39 |
// Remove doubles. |
| 40 |
$this->capabilities[ $capability ] = array_unique( $this->capabilities[ $capability ] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns the list of registered capabilitities. |
| 45 |
* |
| 46 |
* @return string[] Registered capabilities. |
| 47 |
*/ |
| 48 |
public function get_capabilities() { |
| 49 |
return array_keys( $this->capabilities ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns a list of WP_Role roles. |
| 54 |
* |
| 55 |
* The string array of role names are converted to actual WP_Role objects. |
| 56 |
* These are needed to be able to use the API on them. |
| 57 |
* |
| 58 |
* @param array $roles Roles to retrieve the objects for. |
| 59 |
* |
| 60 |
* @return WP_Role[] List of WP_Role objects. |
| 61 |
*/ |
| 62 |
protected function get_wp_roles( array $roles ) { |
| 63 |
$wp_roles = array_map( 'get_role', $roles ); |
| 64 |
|
| 65 |
return array_filter( $wp_roles ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Filter capability roles. |
| 70 |
* |
| 71 |
* @param string $capability Capability to filter roles for. |
| 72 |
* @param array $roles List of roles which can be filtered. |
| 73 |
* |
| 74 |
* @return array Filtered list of roles for the capability. |
| 75 |
*/ |
| 76 |
protected function filter_roles( $capability, array $roles ) { |
| 77 |
/** |
| 78 |
* Filter: Allow changing roles that a capability is added to. |
| 79 |
* |
| 80 |
* @param array $roles The default roles to be filtered. |
| 81 |
*/ |
| 82 |
$filtered = apply_filters( $capability . '_roles', $roles ); |
| 83 |
|
| 84 |
// Make sure we have the expected type. |
| 85 |
if ( ! is_array( $filtered ) ) { |
| 86 |
return []; |
| 87 |
} |
| 88 |
|
| 89 |
return $filtered; |
| 90 |
} |
| 91 |
} |
| 92 |
|