Download the PHP package pinkcrab/hook-loader without Composer
On this page you can find all versions of the php package pinkcrab/hook-loader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pinkcrab/hook-loader
More information about pinkcrab/hook-loader
Files in pinkcrab/hook-loader
Package hook-loader
Short Description An object based hook loader for WordPress.
License MIT
Homepage https://pinkcrab.co.uk
Informations about the package hook-loader
Hook_Loader
The PinkCrab Hook Hook_Loader.
For more details please visit our docs. https://perique.info/lib/Hook_Loader.html
Version
Release 1.2.0
Since v1.0.0 we have made some changes to have this all works under the hood, we have changed from Loader to Hook_Loader as the main class, but Loader has been left in as pollyfill for older versions.
Why?
WordPress and especially WooCommerce is built around hooks and if you have to register a lot of them, it can be hard to keep track of them all and what is currently being registered with WP.
The PinkCrab Hook_Loader gives a more manageable way of registering and removing hook call, shortcode and ajax calls.
Setup
Once you have the hook loader installed it just case of putting it to use. As this is all held as a class, you can pass the instance as depenedecnies to any class you wish to give access to the loader.
Registering Hooks (actions & filters)
Hook_Loader::action(string $hook, callable $method, int $priority=10, int $args = 1): void
Hook_Loader::admin_action(string $hook, callable $method, int $priority=10, int $args = 1): void
Hook_Loader::front_action(string $hook, callable $method, int $priority=10, int $args = 1): void
Hook_Loader::filter(string $hook, callable $method, int $priority=10, int $args = 1): void
Hook_Loader::admin_filter(string $hook, callable $method, int $priority=10, int $args = 1): void
Hook_Loader::front_filter(string $hook, callable $method, int $priority=10, int $args = 1): void
Use with a class.
Hook Removal (actions & filters)
Hook_Loader::remove(string $hook, callable $method, int $priority=10): void
Hook_Loader::remove_filter(string $hook, callable $method, int $priority=10): void
Hook_Loader::remove_hook(string $hook, callable $method, int $priority=10): void
While remove_action() and remove_filter() are prefectly suitable 90% of the time, it can be tricky to unset hooks with have been added as isntance to classes, you can not recall the same instance. Out Hook_Removal class will manually remove all hooks based on the class name (instance or static use). Allowing for the removal of hooks created by other plugins.
Even if the hook was added via a class instance, you only need to use the class name to add the method used. This allows the avoidance of having to recreate an instance of the class and potentially rerunning other hooks and setup routines.
Shortcodes
Hook_Loader::shortcode(string $hook, callable $method): void
You can easily add shortcodes using the loader, not only that you ensure they come with fully populated objects behind them
Ajax
Hook_Loader::ajax(string $hook, callable $method, bool $public, bool $private): void
If you want to register ajax calls, it requires 2 hook calls. This soon gets messy if you are setting up multiple calls. The Hook_Loader can handle registering either or both of this with a single declaration.
As with the other examples this can be used as part of class to create self contained ajax calls. While this can be done manually, the Registerables package comes with a very useful Ajax abstract class which can be used.
Filtering Hooks
It is possible access the Hook Collection before it is registered, this allows for the filtering of the hooks.
You can either use the Hook_Collection class constant
Hook_Collection::REGISTER_HOOKS
or its literal string valuepinkcrab/loader/register_hooks
License
MIT License
http://www.opensource.org/licenses/mit-license.html
Change Log
- 1.2.0 - Updated testing dependencies and support for php8, added in the ability to filter hooks prior to registration.
- 1.1.2 - Loader::class has now been marked as deprecated
- 1.1.1 - Typo on register_hooks() (spelt at regster_hooks)
- 1.1.0 - All internal functionality moved over, still has the same ex
- 1.0.2 - Fixed incorrect docblock on Hook_Loader_Collection::pop() and adding missing readme entries for shortcode and ajax.
- 1.0.1 - Added pop() and count() to the hook collection. Not used really from outside, only in tests.
- 1.0.0 - Moved from Plugin Core package. Moved the internal collection to there own Object away from PC Collection.