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.


What's a hook?
« Reply #2, on May 22nd, 2011, 02:16 PM »
In many of the new features threads there is reference to programming hooks. As a non-coder I have only a vague idea what these are and why they are so important to demand so many threads.


My best guess is:
Code: [Select]
hook(a)
codeblock_1{code...

..
..
..
}
hook(b)
codeblock_2{code...
etc.
So someone can write a mod so that, say, hook(b) executes[1] their code after the codeblock_1. Thus the actual wedge code does not need to be changed as it is with SMF where a chunk of code would be inserted.


Is this anywhere near reality  :blush:
 1. sorry for my poor programming grammar if I use incorrect terms

Re: What's a hook?
« Reply #3, on May 22nd, 2011, 03:08 PM »
That's basically it, yes.

A practical example of the hook usage would be the Display template as of 2.0 RC4 (or RC5, not sure off hand which)

Prior to that, if you wanted to add a button to the menu under a topic - like the Reply, Notify or whatever (here we have a Move To Tracker button!), you have to modify the code.

But with the later version, the array that holds that menu is provided to a function you can specify, which means you can modify it without editing any files.

Another example is the bbcode system; previously, if you wanted to create a new bbcode, you had to edit the file in some way.


Hooks just being able to run functions isn't necessarily that beneficial - but if you can take data out of the main program and manipulate it inside a hook, you're able to do a whole lot more without any code changes, theme or otherwise.

Re: What's a hook?
« Reply #4, on May 22nd, 2011, 04:11 PM »
I don't get them in SMF, and I don't think I've get them here... :P

I need good examples of it.

Re: What's a hook?
« Reply #5, on May 22nd, 2011, 04:26 PM »
Hmm, makes me think we should add hooks at post preparation time...
If only to enable mods to add their own entries into the user menu, things like that.

Re: Improvements to Hooks
« Reply #6, on May 22nd, 2011, 04:53 PM »
Quote from Nao/Gilles on May 22nd, 2011, 04:26 PM
Hmm, makes me think we should add hooks at post preparation time...
If only to enable mods to add their own entries into the user menu, things like that.
That's mostly what SMF uses them for. It's functional if not particularly imaginative.
Quote from DoctorMalboro on May 22nd, 2011, 04:11 PM
I don't get them in SMF, and I don't think I've get them here... :P

I need good examples of it.
OK, let me grab you some from SimpleDesk 2.0.

SimpleDesk is big and scary and does stuff like load its own permissions on every page, amongst plenty of other things it does.

One of them is to load Subs-SimpleDesk.php. Now, I could make a code edit somewhere to load that file, or I could avoid editing any SMF file and add it to the integrate_pre_load hook. The hook is just a list of files to load, so it loads that file. I set it up in the installer, and I'm done. (Look, ma, no edits.)

I then need to do some work with actions. At a minimum I need to define action=helpdesk, and if it's in the weird 'standalone' mode, I also need to remove other actions. Plus I need to juggle action=unread and action=unreadreplies for an SD specific wrapper around them for staff.

Now, I *could* modify index.php for all this, like SimpleDesk 1.0 does, but why should I make file edits when I can achieve the same thing with no hassle for forum owners? And there's a hook for this sort of thing, integrate_actions I think it's called.

Inside that monster Subs-SimpleDesk.php is a function called shd_init_actions. It receives the $actionArray as a variable, in a form it can modify, then I can do my own stuff. I simply tell SMF to call my function at the right time and my function does the rest. So now I have my own file loaded and my own actions added... and I haven't made a single file edit at this point.

I can do exactly the same for the main menu, the admin menu, and plenty more besides.

SlammedDime's SimpleSEF 2.0 (for 2.0 RC5) can actually do everything it does without a single file edit, because it just instructs SMF to load it with the designated hooks and call the relevant functions at the relevant times.

I'd say that the ability to make changes, functionally, to a forum without file edits is pretty damn important. That's our goal with Wedge: to drastically expand the functionality there so it is possible to write most add-ons in a way that doesn't require file edits all over the show.

Even as powerful as the hooks are now (they were stupid before), it's still not enough; SimpleDesk still makes over a dozen file edits, but it's now far fewer than the dozens of edits it used to have to make.

Re: Improvements to Hooks
« Reply #7, on May 22nd, 2011, 05:30 PM »
I suppose what he's looking for is some sample code as in a "real life" situation of a mod attempting to take over an area of the program ;)

Re: Improvements to Hooks
« Reply #8, on May 22nd, 2011, 05:33 PM »
Thanks for the reply Arantor. I don't understand all of it but it sounds very clever  :P


I presume that you have to make an edit to the file that lists the files (/functions?) to be loaded? And if we don't fully understand now I guess it will become apparent when mods start to become available and do much more than on SMF?

Re: Improvements to Hooks
« Reply #9, on May 22nd, 2011, 05:35 PM »
Quote
I presume that you have to make an edit to the file that lists the files (/functions?) to be loaded?
Nope.

You have an install script, it calls existing SMF functions to add to the list of files to load. No edits required.

(I think there's a mod by Miss All Sunday, a spoiler mod IIRC, that uses hooks to add a new bbcode without any edits, might be worth looking at that.)

Re: Improvements to Hooks
« Reply #10, on May 22nd, 2011, 05:39 PM »
Once it's published, I'll take a look at SD 2.0 and it's hooks ;)

Re: Improvements to Hooks
« Reply #11, on May 22nd, 2011, 05:43 PM »
Oh, that might not be the best idea... I did some rather creative interpretation of the hooks at times.

Re: Improvements to Hooks
« Reply #12, on May 22nd, 2011, 05:44 PM »
Then I have to stay with the old school file edits? I need to watch some code to get it, something simple at least :P

Re: Improvements to Hooks
« Reply #13, on May 22nd, 2011, 05:49 PM »
Quote from Arantor on May 22nd, 2011, 05:35 PM
(I think there's a mod by Miss All Sunday, a spoiler mod IIRC, that uses hooks to add a new bbcode without any edits, might be worth looking at that.)

Re: Improvements to Hooks
« Reply #14, on May 22nd, 2011, 06:23 PM »
I hope this isn't copyright and that Miss All Sunday takes this copying as flattery  :)
Code: [Select]
$hooks = array(
'integrate_bbc_codes' => 'spoiler_bbc_add_code',
'integrate_bbc_buttons' => 'spoiler_bbc_add_button',
'integrate_pre_include' => '$boarddir/Sources/Subs-SimpleSpoiler.php',
);


foreach ($hooks as $hook => $function)
add_integration_function($hook, $function);
"You have an install script, it calls existing SMF functions to add to the list of files to load. No edits required."
Is that the "integrate_pre_include" bit?

Re: Improvements to Hooks
« Reply #15, on May 22nd, 2011, 06:25 PM »
Yup. integrate_pre_include just contains a list of files to load on SMF startup, you set it in the installer and no file edit is required to have that file loaded later.