Improvements to Hooks

Nao

  • Dadman with a boy
  • Posts: 16,079
Improvements to Hooks
« on May 7th, 2011, 12:55 PM »Last edited on May 11th, 2011, 08:22 AM by Nao/Gilles
Feature: Improvements to Hooks
Developer: Nao (main), Arantor
Target: modders, admins
Status: 60% (source hooks completed; template hooks in progress; considering whether to add file param; once everything's done, need to look through the code and add hooks where useful.)
Comment:

It took SMF a long time to add "real" support for hooks -- a way to allow plug-ins to safely add code to the main codebase without actually editing the original files. Still, it's far from being a usable standard.

We simplified the calls to begin with. "add_hook('hook_point', 'my_function', 'my_source_file')" will execute 'function' once Wedge reaches the specified hook point (you may specify a source file to load before calling it.)

We provided context data to a few more hooks, and deleted a few useless hooks and outdated mechanisms (such as pre_include, catch_settings or the SMF_INTEGRATION_SETTINGS array).

Mostly, though, we added hooks to various places that SMF doesn't handle, like auto-suggest, behavior check, friend additions, post creation/modification (before and after), browser detection, query string analysis, dynamic rewrite (pre-pretty URL output buffer time), admin settings, credits...

The code internals are also better laid out. Hooks are unregistered if they're no longer available (e.g. uninstalled add-on.) Everything is put into a $modSettings['hooks'] array, instead of scattered through multiple annoying $modSettings['integrate_*'] variables.
We also try and make sure existing hooks can actually do something instead of just stare at the sky. They can now for instance intercept and modify outstanding personal messages, new topics and error messages.

This feature is under development and hasn't yet a state where it can be considered to be feature-frozen. The goal is to enable modders to add their code anywhere, without having to use SMF's code edit hacks... Thus making it possible to upgrade Wedge versions without uninstalling and reinstalling plugins in the process.

Re: Improvements to Hooks
« Reply #16, on May 22nd, 2011, 06:32 PM »
Well that's quite clever then :) So the other bits:- integrate_bbc_codes and integrate_bbc_buttons... they're hooks are they? And they call / load (again sorry for my mis-usage of coding terms) the functions in her Subs-SimpleSpoiler file?

Re: Improvements to Hooks
« Reply #17, on May 22nd, 2011, 06:36 PM »
Yes, basically.

Really, all a hook physically is, is just a setting in $modSettings that contains a 'to do' list. For the include hooks, it's a list of files to load, for the rest, it's a list of functions to call.

You'll see call_integration_hook() mentions throughout the main SMF source (we renamed the function and the hooks, more on this later), which say, "Get this list of functions, and run them" - and often passing information to the hooked functions, so you can manipulate them right there.

Re: Improvements to Hooks
« Reply #18, on May 22nd, 2011, 06:51 PM »
Cor look at this old dog learning new coding tricks :P  I'm having afternoon tea to celebrate :eheh:

Thanks again Arantor for your time, patience and explanations.

Re: Improvements to Hooks
« Reply #19, on May 22nd, 2011, 07:17 PM »
Here's what Miss All Sunday's code would look like in Wedge:

Code: [Select]
add_hook('bbc_codes', 'spoiler_bbc_add_code', 'Subs-SimpleSpoiler');
add_hook('bbc_buttons', 'spoiler_bbc_add_button', 'Subs-SimpleSpoiler');

A bit simpler, eh ;)

Re: Improvements to Hooks
« Reply #20, on May 22nd, 2011, 08:15 PM »
Yes, even I can see that's much neater. The original post now makes more sense and I can see why it's got an entire thread dedicated to it.


What about the additional menu items (buttons) that I have on my SMF installation - this is just a code hack. Would I be able to make my own hook function? This would be cool as at present I must re-edit the code with each upgrade. Perhaps:
Code: [Select]
add_hook('menu_items', 'farjo_add_menu_items', 'farjo_code_changes');

where menu_items would be substituted for the correct name of the hook that controls the buttons, farjo_code_changes.php contains the code that I currently paste into Subs.php but wrapped within the function farjo_add_menu_items.
And if so will there be a idiot-readable list of hooks so that someone like me would be able to make  this type of minor changes to their set-up? I know that this change is aimed at proper modders, but it would also encourage others with my limited coding skill to make changes (changing the actual source code is something I wouldn't have the confidence to do, but making a separate file.... well what's the harm?).

Re: Improvements to Hooks
« Reply #21, on May 22nd, 2011, 08:21 PM »
Well, not *that* code - that's Wedge code.

In your case, you'll create your file, farjo_code_changes.php, and put it in Sources/

Then your package installer would contain:
Code: [Select]
$hooks = array(
'integrate_menu_buttons' => 'farjo_add_menu_items',
'integrate_pre_include' => '$boarddir/Sources/farjo_code_changes.php',
);

foreach ($hooks as $hook => $function)
add_integration_function($hook, $function);

But the principle's the same. Nao just streamlined what the add function did to avoid the complexity and hassle.

Re: Improvements to Hooks
« Reply #22, on May 22nd, 2011, 09:19 PM »
Sorry, I meant if my club switches to wedge how it could be done. From experience, the SMF way that you show would take me a while as there's plenty for the inexperienced fool to get wrong - a missing single-quote, a misspelt word... But the wedge way would be simple - even I could only make a maximum 1 mistake ;)

Anyway thanks for your time. Another reason to look forward to its release - best you get on with it instead of answering my stupid questions :)

Re: Improvements to Hooks
« Reply #23, on May 22nd, 2011, 10:43 PM »
Quote from Farjo on May 22nd, 2011, 08:15 PM
Yes, even I can see that's much neater. The original post now makes more sense and I can see why it's got an entire thread dedicated to it.
There are more features that deserve their own topic, but I was too lazy in the end so they're just in the Minor Features topic ;)
Quote
What about the additional menu items (buttons) that I have on my SMF installation - this is just a code hack. Would I be able to make my own hook function?
Yes.
Quote
This would be cool as at present I must re-edit the code with each upgrade. Perhaps:
Code: [Select]
add_hook('menu_items', 'farjo_add_menu_items', 'farjo_code_changes');
No, the first param is the original hook name, the second parameter is the function to add to the hook (whatever you wrote), and the third parameter (which is optional) is the name of a source file to load where the function can be found.
i.e. if you load a hook that's in a file that also contains a hook function called right before that hook in the code path, then you don't need to bother loading the file.
Quote
where menu_items would be substituted for the correct name of the hook that controls the buttons,
That's 'menu_buttons', AFAIK.
Quote
And if so will there be a idiot-readable list of hooks so that someone like me would be able to make  this type of minor changes to their set-up?
I'll try to document the hooks in the future. Although a list can easily be built from simply searching for "add_hook(" in the Wedge codebase. Right now it's pretty similar to the SMF hook list -- a few were removed, and I probably added a dozen or so. Thing is, I'm not really excited with the idea of adding hooks that never get used, so I'd rather wait for modders to actually ask me for a hook at this or that position. Then I'll figure out the ideal parameter list and the ideal position for the hook, and add it.
Considering that if your mod only uses hooks, it doesn't need any reinstalling when you upgrade your forum, it'll definitely be worth upgrading said forum just to add support for these hooks.
Quote
I know that this change is aimed at proper modders, but it would also encourage others with my limited coding skill to make changes (changing the actual source code is something I wouldn't have the confidence to do, but making a separate file.... well what's the harm?).
Sure!

Re: Improvements to Hooks
« Reply #24, on May 23rd, 2011, 05:22 PM »
Thanks for the reply.
The hook is as you say integrate_menu_buttons and the list of hooks is here: http://wiki.simplemachines.org/smf/index.php?title=Integration_hooks&oldid=5972 So there's another mini-project for me - to write a mod  :wow:
Quote
There are more features that deserve their own topic, but I was too lazy in the end so they're just in the Minor Features topic ;)
Yes that's a massive topic which I'm still only halfway through reading :)

Re: Improvements to Hooks
« Reply #25, on May 23rd, 2011, 05:34 PM »
They really need to update that, all the hooks are described with the original limitations they had when they were in RC3... (back when I originally wrote it)

Re: Improvements to Hooks
« Reply #26, on May 29th, 2011, 11:54 PM »
Now that mods would be "edit-free" wouldn't be better to be saved in a folder apart from Sources to have things more nice and clean?

Re: Improvements to Hooks
« Reply #27, on May 30th, 2011, 12:04 AM »
Two things.

Firstly, both SMF and Wedge have limitations as far as the hooks go. Wedge does it better, much better, but there's still a limitation as to where files can be.

Secondly, some people do crazy shit like moving their Sources dir out of the web tree, so you kind of need to put it in there - but I guess we could do it as having a folder for add-ons where each add-on gets its own folder therein?

Re: Improvements to Hooks
« Reply #28, on May 30th, 2011, 03:23 AM »
I think that could make the understanding of WHAT IS A MOD and WHAT IS CORE STUFF IMO :P

Re: Improvements to Hooks
« Reply #29, on May 30th, 2011, 09:50 AM »
I'm still pushing for Wedge to completely disable the ability to hack into source files... And I'm ready to add as many hook points as requested to ensure as many mods as possible can be done for Wedge.
It really doesn't kill performance at all, in my tests at least. It's just an extra function call and an empty() test on each new hook...