<?php
namespace Tiamr\Rules;

if (!defined('ABSPATH')) {
    exit;
}

final class User_Context {
    public function is_logged_in(): bool {
        return is_user_logged_in();
    }

    public function get_user_roles(): array {
        if (!$this->is_logged_in()) {
            return [];
        }

        $user = wp_get_current_user();

        if (!$user || empty($user->roles) || !is_array($user->roles)) {
            return [];
        }

        return array_values(array_unique(array_map('sanitize_key', $user->roles)));
    }

    public function has_any_role(array $roles): bool {
        if (empty($roles)) {
            return false;
        }

        return (bool) array_intersect($this->get_user_roles(), $roles);
    }
}
