Hooks
Sometimes you need to handle events in a plugin that hasn’t been initialized and can't subscribe to events. In such cases, you can use hooks. A hook initializes the plugin and passes it the event received from any part of the system. The Core is responsible for managing hook registration and invocation.
-
Define hooks in the system:
define('HOOK_OFFER', 'onHookOffer'); define('HOOK_CANCEL_PURCHASE', 'onHookCancelPurchase');
The hook name should match the method name in the handling plugins. For example, all plugins handling the
onHookOffer
hook should implement this method. -
Describe the array of listener plugins:
$hooks = array( HOOK_OFFER => 'PluginOne', HOOK_CANCEL_PURCHASE => array('PluginOne', 'PluginTwo') );
-
Pass the hook list to the core:
Core::getInstance()->setHooks($hooks);
-
Trigger hooks where needed in your business logic:
Core::getInstance()->fireHook(HOOK_OFFER, $param1...);
You can pass an unlimited number of parameters, which will be sent to the listening plugin methods.
-
Implement methods in the listening plugins:
class PluginOne extends ObjectPlugin { ... /** * @hook */ public function onHookOffer($param1...) { ... } }