| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Capabilities |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Capability Utils collection. |
| 10 |
*/ |
| 11 |
class WPSEO_Capability_Utils { |
| 12 |
|
| 13 |
/** |
| 14 |
* Checks if the user has the proper capabilities. |
| 15 |
* |
| 16 |
* @param string $capability Capability to check. |
| 17 |
* |
| 18 |
* @return bool True if the user has the proper rights. |
| 19 |
*/ |
| 20 |
public static function current_user_can( $capability ) { |
| 21 |
if ( $capability === 'wpseo_manage_options' ) { |
| 22 |
return self::has( $capability ); |
| 23 |
} |
| 24 |
|
| 25 |
return self::has_any( [ 'wpseo_manage_options', $capability ] ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Retrieves the users that have the specified capability. |
| 30 |
* |
| 31 |
* @param string $capability The name of the capability. |
| 32 |
* |
| 33 |
* @return array The users that have the capability. |
| 34 |
*/ |
| 35 |
public static function get_applicable_users( $capability ) { |
| 36 |
$applicable_roles = self::get_applicable_roles( $capability ); |
| 37 |
|
| 38 |
if ( $applicable_roles === [] ) { |
| 39 |
return []; |
| 40 |
} |
| 41 |
|
| 42 |
return get_users( [ 'role__in' => $applicable_roles ] ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Retrieves the roles that have the specified capability. |
| 47 |
* |
| 48 |
* @param string $capability The name of the capability. |
| 49 |
* |
| 50 |
* @return array The names of the roles that have the capability. |
| 51 |
*/ |
| 52 |
public static function get_applicable_roles( $capability ) { |
| 53 |
$roles = wp_roles(); |
| 54 |
$role_names = $roles->get_names(); |
| 55 |
|
| 56 |
$applicable_roles = []; |
| 57 |
foreach ( array_keys( $role_names ) as $role_name ) { |
| 58 |
$role = $roles->get_role( $role_name ); |
| 59 |
|
| 60 |
if ( ! $role ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
// Add role if it has the capability. |
| 65 |
if ( array_key_exists( $capability, $role->capabilities ) && $role->capabilities[ $capability ] === true ) { |
| 66 |
$applicable_roles[] = $role_name; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $applicable_roles; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Checks if the current user has at least one of the supplied capabilities. |
| 75 |
* |
| 76 |
* @param array $capabilities Capabilities to check against. |
| 77 |
* |
| 78 |
* @return bool True if the user has at least one capability. |
| 79 |
*/ |
| 80 |
protected static function has_any( array $capabilities ) { |
| 81 |
foreach ( $capabilities as $capability ) { |
| 82 |
if ( self::has( $capability ) ) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Checks if the user has a certain capability. |
| 92 |
* |
| 93 |
* @param string $capability Capability to check against. |
| 94 |
* |
| 95 |
* @return bool True if the user has the capability. |
| 96 |
*/ |
| 97 |
protected static function has( $capability ) { |
| 98 |
return current_user_can( $capability ); |
| 99 |
} |
| 100 |
} |
| 101 |
|