Jump to content

Run Hook First


MACscr

Recommended Posts

So I have a custom hook to run with a order of 0 for ClientAreaPage:

add_hook('ClientAreaPage', 0, function($vars) { $_SESSION['foo'] = 'bar'; });

but the problem is that the hook doesnt run before my custom pages own init file and runs right after instead. Any ideas why it doesnt run before the init file is included? The hook creates some $_SESSION values that i need to access from my init.php. Here is a snippet from my custom page:

<?php

use WHMCS\Database\Capsule;

define("CLIENTAREA", true);
define("FORCESSL", true); // Uncomment to force the page to use https://

require("init.php");

if ($_GET['client_id'] && $_SESSION['adminid'] > 0 && $_SESSION['uid'] != $_GET['client_id']) {
    $_SESSION['uid'] = (int)$_GET['client_id'];
}

$capsule = new Capsule;
$ca = new WHMCS_ClientArea();

// add directory where plugins are stored
$smarty->addPluginsDir('./custom/smarty_plugins/');

$ca->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
$ca->initPage();
$ca->requireLogin();

$client_id = (int)$ca->getUserID();
if ($client_id == 0) {
    die('invalid user id');
}
$ca->assign('client_id', $client_id);

require("custom/init.php");
require("custom/functions.php");

 I doubled checked the order by simply doing: var_dump('hook ran'); from the hook and then var_dump('init ran'); from each file/function to see the order it came up on the page. Its definitely not running the hook first. Any suggestions to solve the problem woudl be sincerely appreciated.

Link to comment
Share on other sites

When you refer to your "init.php" file, do you mean the first one mentioned in your code or the one in the custom folder?  I ask as it looks like you are calling WHMCS's init.php file and thus all the code behind it and so your hook would fire.  If your init.php is the custom/init.php file, then you need to have the require for that above WHMC's init.php file in the code.

Link to comment
Share on other sites

The first one in the code is the whmcs one. Mine comes after. My init is still somehow running before the hook though. Why would you think my init should come before the whmcs one when the problem is the whmcs hook is running AFTER my init.php code? Now my init can access a bunch of whmcs stuff just fine, its just the problem with the hook timing.

Link to comment
Share on other sites

Just misread.  I tested this and got the expected result but the client area did not display and then troubleshooting that lead to not being able to reproduce the the result.  Currently heading off to a biz meeting so can't test this further but will try to later .

Link to comment
Share on other sites

Any luck? I would think this hook would run during the core whmcs init.php. Whatever it is, I need to figure a way to get the hook to consistently run on all whmcs client pages and on my custom pages, before my init runs. 

Link to comment
Share on other sites

I am seeing the same behavior.  In a included file and a hook I set a session value and used logmodulecall in both with the session variable  being different for each one.  The hook's logmodulecall provides the session value that is set in the included file.  However, the included files logmodulecall does not provide the hook's session value until after reloading the page.   I also tried moving the include after the ca->output() and oddly the logmodulecall isn't even called then for the included file. 

Could you use an addon module instead of a custom page?

Link to comment
Share on other sites

In the addon module's _clientarea function, you would check for which page you are calling via a variable.  So for example, a page of "blah" could either be /m=pages&page=blah  where pages is the module name and blah is the actual page.  To get a nicer URL, you could use rewrites in .htaccess that rewrite /pages/blah as /m=pages&page=blah internally.  That is how the addon modules that make WHMCS a whole site work .   So then in the _clientarea function, you could do a if page equals blah, then show this page generated by a template file and info from the database if needed.   The advantage of an addon is you could have all the page's html stored in the database or use template files and smarty for example and use the _output function to manage them on the admin side.  Also, there is no need for all the init bits for WHMCS as that is taken care of.    Addon modules also have their own hooks file, so you can still hook in to events. 

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