/*
Theme Name: Houzez Child
Theme URI: http://www.favethemes.com/
Author: Favethemes
Author URI: http://www.favethemes.com/
Description: Houzez is a premium WordPress theme for real estate agents where modern aesthetics are combined with tasteful simplicity, and where the ease of use is achieved without compromise in your ability to customise the design. Whether you are a real estate agent looking to build a website for your company or a web developer seeking a perfect WordPress theme for your next project, you are certain to appreciate the numerous features and benefits that our theme provides.
Version: 1.0
Tags: white, right-sidebar, left-sidebar, custom-colors, custom-menu, featured-images, post-formats, theme-options, translation-ready
License: GNU General Public License version 3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Text Domain: houzez
Template: houzez
*/


@import url("../houzez/style.css");

/* Your CSS code goes here
-------------------------------------- */
<?php
/**
 * PR: Custom Dashboard Menu (Allowlist) - v3 (role + meta detection)
 */

if ( ! defined('ABSPATH') ) { exit; }
if ( ! is_user_logged_in() ) { return; }

$user  = wp_get_current_user();
$roles = (array) $user->roles;

// --- Detect Agency/Agent robustly ---
// 1) Role-based (covers most Houzez installs)
$agency_roles = [
    'agency',
    'houzez_agency',
    'houzez_agency_admin',
    'houzez_agency_owner',
    'agency_admin',
];

$agent_roles = [
    'agent',
    'houzez_agent',
    'houzez_agent_user',
];

// 2) Meta-based (most reliable in Houzez)
$agency_id = (int) get_user_meta( $user->ID, 'fave_author_agency_id', true );
$agent_id  = (int) get_user_meta( $user->ID, 'fave_author_agent_id', true );

$is_agency = ( $agency_id > 0 ) || (bool) array_intersect($roles, $agency_roles);
$is_agent  = ( $agent_id  > 0 ) || (bool) array_intersect($roles, $agent_roles);

// Ако нищо не хванем – по подразбиране НЕ показваме ЕКИП.
if ( ! $is_agency && ! $is_agent ) {
    // По-добре безопасно: показваме само Account/Other като за agent.
    $is_agent = true;
}

/**
 * Houzez линкове (ако функцията съществува), иначе fallback slug.
 */
$houzez_link = function( $template_file, $fallback_path ) {
    if ( function_exists('houzez_get_template_link') ) {
        $url = houzez_get_template_link( $template_file );
        if ( ! empty($url) ) return $url;
    }
    return home_url( $fallback_path );
};

// Account
$link_profile  = $houzez_link('template/user_dashboard_profile.php',   '/my-profile/');
$link_invoices = $houzez_link('template/user_dashboard_invoices.php',  '/invoices/');
$link_messages = $houzez_link('template/user_dashboard_messages.php',  '/messages/');
$link_logout   = wp_logout_url( home_url('/') );

// Team (agency only)
$link_team_list = $houzez_link('template/user_dashboard_agency_agents.php', '/agency-agents/');
$link_team_add  = $houzez_link('template/user_dashboard_agency_add_agent.php', '/add-agent/');

?>
<div class="dashboard-menu-wrap">
    <ul class="dashboard-menu">

        <?php if ( $is_agency ) : ?>
            <li class="sidebar-menu-title">ЕКИП</li>

            <li class="menu-item">
                <a href="<?php echo esc_url($link_team_list); ?>">
                    <i class="houzez-icon icon-multiple-users"></i>
                    <span>Агенти/Брокери</span>
                </a>
            </li>

            <li class="menu-item">
                <a href="<?php echo esc_url($link_team_add); ?>">
                    <i class="houzez-icon icon-add-circle"></i>
                    <span>Прибави нов</span>
                </a>
            </li>
        <?php endif; ?>

        <li class="sidebar-menu-title">ДРУГИ</li>

        <li class="menu-item">
            <a href="<?php echo esc_url($link_invoices); ?>">
                <i class="houzez-icon icon-invoice"></i>
                <span>Фактури</span>
            </a>
        </li>

        <li class="menu-item">
            <a href="<?php echo esc_url($link_messages); ?>">
                <i class="houzez-icon icon-messages-bubble"></i>
                <span>Съобщения</span>
            </a>
        </li>

        <li class="sidebar-menu-title">АКАУНТ</li>

        <li class="menu-item">
            <a href="<?php echo esc_url($link_profile); ?>">
                <i class="houzez-icon icon-single-neutral-circle"></i>
                <span>Моят профил</span>
            </a>
        </li>

        <li class="menu-item">
            <a href="<?php echo esc_url($link_logout); ?>">
                <i class="houzez-icon icon-logout"></i>
                <span>Отписване</span>
            </a>
        </li>

    </ul>
</div>
