Jump to content

Custom hooks issue


Filozof

Recommended Posts

Hi, community! 🙂

I have a questions about hooks. So, I've been using some hooks to "hide" some menu/submenu items and sidebars in client area. I added all the hooks in one file (example: custom_hooks.php) and placed in /includes/hooks directory. And that works fine for some time and then it's simply stop working and all menus/submenus and sidebars reappear again.

I also tried to create separate .php file for each hook and upload it to the same location. Issue is the same: it stops working and all the elements reappear in client area.

Here's an example of the code used:

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {
   $secondarySidebar->removeChild('Choose Currency');
});
add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {
   $secondarySidebar->removeChild('Actions');
});
add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {
   $secondarySidebar->removeChild('Client Shortcuts');
});
add_hook('ClientAreaPrimarySidebar', 1, function (MenuItem $primarySidebar) {
   $primarySidebar->removeChild('My Invoices Summary');
});
add_hook('ClientAreaPrimarySidebar', 1, function (MenuItem $primarySidebar) {
   $primarySidebar->removeChild('My Invoices Status Filter');
});
add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar)
{
    $secondarySidebar->removeChild('Billing');
});
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
    if (!is_null($primaryNavbar->getChild('Support'))) {
        $primaryNavbar->getChild('Support')->removeChild('Downloads');
    }
});
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
    if (!is_null($primaryNavbar->getChild('Support'))) {
        $primaryNavbar->getChild('Support')->removeChild('Network Status');
    }
});
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
    if (!is_null($primaryNavbar->getChild('Billing'))) {
        $primaryNavbar->getChild('Billing')->removeChild('My Quotes');
    }
});

Is this wrong or issue is something else?

Thanks for your help!

Link to comment
Share on other sites

On 28/11/2018 at 17:53, Filozof said:

Is this wrong or issue is something else?

it's wrong - specifically there is one golden rule that you need to remember with navbar/sidebar hooks - you always need to check that the child you are removing/modifying actually exists before you try to do anything to it... otherwise, they will only work on the pages where those children exist.. anywhere else and you might start seeing errors.

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar)
{
	if (!is_null($primarySidebar->getChild('My Invoices Status Filter'))) {
		$primarySidebar->removeChild('My Invoices Status Filter');
	}
	if (!is_null($primarySidebar->getChild('My Invoices Summary'))) {
		$primarySidebar->removeChild('My Invoices Summary');
	}
});

add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar)
{
	if (!is_null($secondarySidebar->getChild('Actions'))) {
		$secondarySidebar->removeChild('Actions');
	}
	if (!is_null($secondarySidebar->getChild('Billing'))) {
		$secondarySidebar->removeChild('Billing');
	}
	if (!is_null($secondarySidebar->getChild('Choose Currency'))) {
		$secondarySidebar->removeChild('Choose Currency');
	}
	if (!is_null($secondarySidebar->getChild('Client Shortcuts'))) {
		$secondarySidebar->removeChild('Client Shortcuts');
	}
});

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	if (!is_null($primaryNavbar->getChild('Support'))) {
		$primaryNavbar->getChild('Support')
			->removeChild('Downloads')
			->removeChild('Network Status');
	}
	if (!is_null($primaryNavbar->getChild('Billing'))) {
		$primaryNavbar->getChild('Billing')
			->removeChild('My Quotes');
	}
});

technically, we should be checking those navbar children exist before removing them, but as long as you don't have other hooks trying to remove them before this hook runs, it shouldn't matter.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated