hesabixWpTheme/inc/header-user-menu.php

215 lines
7.5 KiB
PHP

<?php
/**
* منوی حساب کاربر در هدر (جایگزین «پورتال مشتریان» برای کاربر واردشده)
*
* @package Hesabix2
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* آیتم‌های منوی حساب کاربر.
*
* @return array<int, array<string, string>>
*/
function hesabix2_header_user_menu_items() {
$items = array();
if (function_exists('hesabix2_is_woocommerce_active') && hesabix2_is_woocommerce_active()) {
$items[] = array(
'label' => __('حساب کاربری', 'hesabix2'),
'url' => wc_get_account_endpoint_url('dashboard'),
);
$items[] = array(
'label' => __('سفارشات', 'hesabix2'),
'url' => wc_get_account_endpoint_url('orders'),
);
$items[] = array(
'label' => __('تغییر کلمه عبور', 'hesabix2'),
'url' => wc_get_account_endpoint_url('edit-account'),
);
$items[] = array(
'label' => __('خروج', 'hesabix2'),
'url' => wc_logout_url(home_url('/')),
'class' => 'hesabix-header-user__link--logout',
);
} else {
$items[] = array(
'label' => __('حساب کاربری', 'hesabix2'),
'url' => home_url('/'),
);
$items[] = array(
'label' => __('تغییر کلمه عبور', 'hesabix2'),
'url' => function_exists('hesabix2_portal_auth_url')
? hesabix2_portal_auth_url('forgot')
: wp_lostpassword_url(),
);
$items[] = array(
'label' => __('خروج', 'hesabix2'),
'url' => wp_logout_url(home_url('/')),
'class' => 'hesabix-header-user__link--logout',
);
}
return apply_filters('hesabix2_header_user_menu_items', $items);
}
/**
* آیکن SVG کاربر
*
* @return string
*/
function hesabix2_header_user_menu_icon_svg() {
return '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/></svg>';
}
/**
* کلاس لینک فعال منوی کاربر
*
* @param string $url
* @return string
*/
function hesabix2_header_user_link_current_class($url) {
if (!is_user_logged_in() || '' === $url) {
return '';
}
$url = untrailingslashit((string) $url);
if (!function_exists('is_account_page') || !is_account_page()) {
return '';
}
if (!function_exists('hesabix2_is_woocommerce_active') || !hesabix2_is_woocommerce_active()) {
return '';
}
if (false !== strpos($url, 'customer-logout') || false !== strpos($url, 'logout')) {
return '';
}
if ($url === untrailingslashit(wc_get_account_endpoint_url('dashboard')) && !is_wc_endpoint_url()) {
return ' is-current';
}
if (is_wc_endpoint_url('orders') && $url === untrailingslashit(wc_get_account_endpoint_url('orders'))) {
return ' is-current';
}
if (is_wc_endpoint_url('edit-account') && $url === untrailingslashit(wc_get_account_endpoint_url('edit-account'))) {
return ' is-current';
}
return '';
}
/**
* لینک «پورتال مشتریان» (مهمان)
*/
function hesabix2_render_header_portal_link_desktop() {
$url = function_exists('hesabix2_portal_auth_url')
? hesabix2_portal_auth_url('login')
: wp_login_url();
?>
<a
href="<?php echo esc_url($url); ?>"
class="hesabix-header-login"
><?php esc_html_e('پورتال مشتریان', 'hesabix2'); ?></a>
<?php
}
/**
* منوی کشویی حساب — دسکتاپ
*/
function hesabix2_render_header_user_menu_desktop() {
if (!is_user_logged_in()) {
return;
}
$user = wp_get_current_user();
$label = $user->display_name ? $user->display_name : $user->user_login;
?>
<div class="hesabix-header-user hesabix-header-dropdown" data-header-dropdown>
<button
type="button"
class="hesabix-header-icon-btn hesabix-header-dropdown__trigger hesabix-header-user__trigger"
aria-expanded="false"
aria-haspopup="true"
aria-controls="hesabix-header-user-panel"
id="hesabix-header-user-btn"
aria-label="<?php echo esc_attr(sprintf(__('منوی حساب — %s', 'hesabix2'), $label)); ?>"
>
<?php echo hesabix2_header_user_menu_icon_svg(); ?>
</button>
<div
id="hesabix-header-user-panel"
class="hesabix-header-drop__panel hesabix-header-user__panel"
role="menu"
aria-labelledby="hesabix-header-user-btn"
hidden
>
<?php foreach (hesabix2_header_user_menu_items() as $item) :
$url = isset($item['url']) ? (string) $item['url'] : '#';
$cls = 'hesabix-header-user__link' . hesabix2_header_user_link_current_class($url);
if (!empty($item['class'])) {
$cls .= ' ' . esc_attr((string) $item['class']);
}
?>
<a
href="<?php echo esc_url($url); ?>"
class="<?php echo esc_attr(trim($cls)); ?>"
role="menuitem"
<?php echo hesabix2_header_user_link_current_class($url) ? ' aria-current="page"' : ''; ?>
><?php echo esc_html($item['label']); ?></a>
<?php endforeach; ?>
</div>
</div>
<?php
}
/**
* پورتال یا منوی کاربر — دسکتاپ
*/
function hesabix2_render_header_auth_desktop() {
if (is_user_logged_in()) {
hesabix2_render_header_user_menu_desktop();
return;
}
hesabix2_render_header_portal_link_desktop();
}
/**
* پورتال یا لینک‌های حساب — موبایل (دراور)
*/
function hesabix2_render_header_auth_mobile() {
if (is_user_logged_in()) {
$user = wp_get_current_user();
?>
<div class="hesabix-mobile-user-menu" role="navigation" aria-label="<?php esc_attr_e('حساب کاربری', 'hesabix2'); ?>">
<p class="hesabix-mobile-user-menu__title">
<?php echo hesabix2_header_user_menu_icon_svg(); ?>
<span><?php echo esc_html($user->display_name ? $user->display_name : $user->user_login); ?></span>
</p>
<?php foreach (hesabix2_header_user_menu_items() as $item) :
$url = isset($item['url']) ? (string) $item['url'] : '#';
$cls = 'hesabix-mobile-link hesabix-mobile-user-menu__link' . hesabix2_header_user_link_current_class($url);
if (!empty($item['class'])) {
$cls .= ' ' . esc_attr((string) $item['class']);
}
?>
<a
href="<?php echo esc_url($url); ?>"
class="<?php echo esc_attr(trim($cls)); ?>"
<?php echo hesabix2_header_user_link_current_class($url) ? ' aria-current="page"' : ''; ?>
><?php echo esc_html($item['label']); ?></a>
<?php endforeach; ?>
</div>
<?php
return;
}
$url = function_exists('hesabix2_portal_auth_url')
? hesabix2_portal_auth_url('login')
: wp_login_url();
?>
<a href="<?php echo esc_url($url); ?>" class="hesabix-header-login hesabix-header-login--block"><?php esc_html_e('پورتال مشتریان', 'hesabix2'); ?></a>
<?php
}