<?php
if (!defined('ABSPATH')) exit;

/**
 * Get roles allowed to act as vendors.
 */
function stakmv_get_allowed_vendor_roles() {

    $roles = ['stakmv_vendor'];

    $roles = apply_filters('stakmv_allowed_vendor_roles', $roles);

    if (!is_array($roles)) {
        $roles = ['stakmv_vendor'];
    }

    return array_values(array_unique($roles));
}

/**
 * Final vendor check.
 */
function stakmv_is_vendor($user_id = null) {

    if (!is_user_logged_in()) {
        return false;
    }

    $user = get_userdata($user_id ?: get_current_user_id());

    if (!$user || empty($user->roles)) {
        return false;
    }

    $user_roles = (array) $user->roles;

    // Administrators are always allowed.
    if (in_array('administrator', $user_roles, true)) {
        return true;
    }

    $allowed_roles = stakmv_get_allowed_vendor_roles();

    return !empty(array_intersect($user_roles, $allowed_roles));
}

/**
 * Convenience wrapper.
 */
function stakmv_current_user_can_access_vendor_area() {
    return stakmv_is_vendor();
}
