173 lines
3.6 KiB
PHP
173 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* Role manager class
|
|
*
|
|
* @package TwoFactorExtended
|
|
* @since 0.1.0
|
|
*/
|
|
|
|
// Prevent direct access.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class Two_Factor_Extended_Role_Manager
|
|
*
|
|
* Manages WordPress roles and role detection.
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
class Two_Factor_Extended_Role_Manager {
|
|
|
|
/**
|
|
* Get all WordPress roles.
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @return array Array of role slugs and names.
|
|
*/
|
|
public static function get_all_roles(): array {
|
|
if ( ! function_exists( 'get_editable_roles' ) ) {
|
|
require_once ABSPATH . 'wp-admin/includes/user.php';
|
|
}
|
|
|
|
$roles = array();
|
|
$wp_roles = get_editable_roles();
|
|
|
|
foreach ( $wp_roles as $role_slug => $role_data ) {
|
|
$roles[ $role_slug ] = translate_user_role( $role_data['name'] );
|
|
}
|
|
|
|
return $roles;
|
|
}
|
|
|
|
/**
|
|
* Get role display name.
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @param string $role_slug Role slug.
|
|
*
|
|
* @return string Role display name or slug if not found.
|
|
*/
|
|
public static function get_role_display_name( string $role_slug ): string {
|
|
$roles = self::get_all_roles();
|
|
|
|
return $roles[ $role_slug ] ?? $role_slug;
|
|
}
|
|
|
|
/**
|
|
* Get user's roles.
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @param int $user_id User ID.
|
|
*
|
|
* @return array Array of role slugs.
|
|
*/
|
|
public static function get_user_roles( int $user_id ): array {
|
|
$user = get_userdata( $user_id );
|
|
|
|
if ( ! $user || ! isset( $user->roles ) ) {
|
|
return array();
|
|
}
|
|
|
|
return $user->roles;
|
|
}
|
|
|
|
/**
|
|
* Check if user has specific role.
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @param int $user_id User ID.
|
|
* @param string $role_slug Role slug to check.
|
|
*
|
|
* @return bool True if user has the role, false otherwise.
|
|
*/
|
|
public static function user_has_role( int $user_id, string $role_slug ): bool {
|
|
$user_roles = self::get_user_roles( $user_id );
|
|
|
|
return in_array( $role_slug, $user_roles, true );
|
|
}
|
|
|
|
/**
|
|
* Get user's primary role (first role).
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @param int $user_id User ID.
|
|
*
|
|
* @return string|null Primary role slug or null if user has no roles.
|
|
*/
|
|
public static function get_user_primary_role( int $user_id ): ?string {
|
|
$roles = self::get_user_roles( $user_id );
|
|
|
|
return ! empty( $roles ) ? $roles[0] : null;
|
|
}
|
|
|
|
/**
|
|
* Check if role exists.
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @param string $role_slug Role slug.
|
|
*
|
|
* @return bool True if role exists, false otherwise.
|
|
*/
|
|
public static function role_exists( string $role_slug ): bool {
|
|
$roles = self::get_all_roles();
|
|
|
|
return isset( $roles[ $role_slug ] );
|
|
}
|
|
|
|
/**
|
|
* Get roles for Multisite (network vs site roles).
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @param int $user_id User ID.
|
|
* @param int $blog_id Blog ID (optional, defaults to current blog).
|
|
*
|
|
* @return array Array of role slugs for the specified blog.
|
|
*/
|
|
public static function get_user_roles_for_blog( int $user_id, int $blog_id = 0 ): array {
|
|
if ( ! is_multisite() ) {
|
|
return self::get_user_roles( $user_id );
|
|
}
|
|
|
|
if ( 0 === $blog_id ) {
|
|
$blog_id = get_current_blog_id();
|
|
}
|
|
|
|
$user = get_userdata( $user_id );
|
|
|
|
if ( ! $user ) {
|
|
return array();
|
|
}
|
|
|
|
// Get roles for specific blog.
|
|
$roles_key = $GLOBALS['wpdb']->get_blog_prefix( $blog_id ) . 'capabilities';
|
|
$roles = isset( $user->{$roles_key} ) ? array_keys( $user->{$roles_key} ) : array();
|
|
|
|
return $roles;
|
|
}
|
|
|
|
/**
|
|
* Check if user is super admin (Multisite).
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @param int $user_id User ID.
|
|
*
|
|
* @return bool True if user is super admin, false otherwise.
|
|
*/
|
|
public static function is_super_admin( int $user_id ): bool {
|
|
if ( ! is_multisite() ) {
|
|
return false;
|
|
}
|
|
|
|
return is_super_admin( $user_id );
|
|
}
|
|
}
|