将类添加到活动的自定义菜单项WHMCS


Adding classes to active custom menu item WHMCS

我正在为WHMCS使用PayPal计费网关,并在挂钩文件中使用以下内容将菜单项添加到ClientAreaPrimaryNavbar和ClientAreaSecondarySidebar:

<?php 
use WHMCS'View'Menu'Item as MenuItem; 
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar) { 
    if (!is_null($primaryNavbar->getChild('Billing'))) { 
        $primaryNavbar->getChild('Billing')->addChild('Manage PayPal Billing', array( 
            'label' => 'Manage PayPal Billing', 
            'uri' => 'paypalbilling.php', 
            'order' => '30' 
        )); 
    } 
}); 
add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) { 
    if (!is_null($secondarySidebar->getChild('Billing'))) { 
        $secondarySidebar->getChild('Billing')->addChild('Manage PayPal Billing', array( 
            'label' => 'Manage PayPal Billing', 
            'uri' => 'paypalbilling.php', 
            'order' => '30' 
        )); 
    } 
});

当有人在paypalbilling.php页面上时,我需要在ClientAreaPrimaryNavbar中获得"Billing"菜单项以获得类别open,并使用类别active突出显示"Billing(账单("下的"Manage PayPal Billing(管理PayPal账单("子项。然后我需要让ClientAreaSecondarySidebar中的"管理PayPal账单"菜单项也用类active突出显示。显然,我只需要在这是活动页面时添加类。

我有一种感觉,这可能与核心paypalbilling.php页面没有定义面包屑有关。。但并不确定。自定义页面代码不是开源的,所以我看不到或编辑它:(

我已经尝试过以各种不同的方式在这个钩子中使用setClass,但无法找出if( is current page = paypalbilling.php )的条件。

如有任何帮助,我们将不胜感激!

我已经找到了如何使用当前页面将类添加到子菜单中,但仍然无法将类添加为主ClientAreaPrimaryNavbar计费菜单。

我试过$primaryNavbar->getChild('Billing')->setClass('active');,但似乎不起作用。

以下是检查当前页面并设置具有"活动"类别的新菜单项的代码:

<?php
use WHMCS'View'Menu'Item as MenuItem;
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar) {
    $filename = basename($_SERVER['REQUEST_URI'], ".php");
    $parseFile = explode('.', $filename);
    if (!is_null($primaryNavbar->getChild('Billing'))) {
        $primaryNavbar->getChild('Billing')->addChild('Manage PayPal Billing', array(
            'label' => 'Manage PayPal Billing',
            'uri' => 'paypalbilling.php',
            'order' => '30'
        ));
        if ($parseFile['0']=='paypalbilling'){
            $primaryNavbar->getChild('Billing')->setClass('active'); // THIS LINE DOES NOT WORK
            $primaryNavbar->getChild('Billing')->getChild('Manage PayPal Billing')->setClass('active');
        }
    }
});
add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {
    $filename = basename($_SERVER['REQUEST_URI'], ".php");
    $parseFile = explode('.', $filename);
    if (!is_null($secondarySidebar->getChild('Billing'))) {
        $secondarySidebar->getChild('Billing')->addChild('Manage PayPal Billing', array(
            'label' => 'Manage PayPal Billing',
            'uri' => 'paypalbilling.php',
            'order' => '30'
        ));
        if ($parseFile['0']=='paypalbilling'){
            $secondarySidebar->getChild('Billing')->getChild('Manage PayPal Billing')->setClass('active');
        }
    }
});

据我所知(从文档中(,WHMCS没有任何方法来获取菜单的活动状态。我使用JavaScript解决了这个问题。希望它能帮助到任何人。

const liElements = document.querySelectorAll('li');
// Loop through each li element
liElements.forEach(liElement => {
    const anchorElement = liElement.querySelector('a');
    if (anchorElement && anchorElement.href === window.location.href) {
        liElement.classList.add('active');
        const dropdownParent = liElement.closest('.dropdown');
        if (dropdownParent) {
            dropdownParent.classList.add('active');
        }
        return;
    }
    const childLiElements = liElement.querySelectorAll('li');
    childLiElements.forEach(childLiElement => {
        const childAnchorElement = childLiElement.querySelector('a');
        if (childAnchorElement && childAnchorElement.href === window.location.href) {
            liElement.classList.add('active');
            const dropdownParent = liElement.closest('.dropdown');
            if (dropdownParent) {
                dropdownParent.classList.add('active');
            }
            return;
        }
    });
});

另一种方法是使用PHP$_SERVER来实现相同的基于URL的逻辑。