# Hooks Service

The hooks service is first-of-all [a single-handler service](/foundations-module/utilities/handlers-and-services.md) and second-of-all a [runnable and resettable](/foundations-module/actions.md) object. It's a service designed to work with WordPress actions and filters (commonly referred to as hooks). Basically the service provides the following public methods:

* `add_action` - registers an action with the handler
* `remove_action` - removes an action registered with the handler
* `remove_all_actions` - removes all actions registered with the handler
* `add_filter` - registers a filter with the handler
* `remove_filter` - remove a filter registered with the handler
* `remove_all_filters` - removes all filters registered with the handler

All of these methods are just wrappers against the registered handler. Any handler implementing the [`HooksHandlerInterface`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Hooks/HooksHandlerInterface.php) can be registered with the hooks service, but you may also decide to simply use the default handler instantiated if no handler is passed on in the constructor. The default handler is an instance of the [`DefaultHooksHandler`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Hooks/Handlers/DefaultHooksHandler.php) inspired [by the loader](https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/blob/master/plugin-name/includes/class-plugin-name-loader.php) defined in the [WordPress Plugin Boilerplate by DevinVinson](https://github.com/DevinVinson/WordPress-Plugin-Boilerplate).

{% hint style="info" %}
The intention is to only register the hooks with the WordPress system if the plugin was successfully initialized. If an error occurred, we don't want any half-hooks being called.
{% endhint %}

The default handler maintains all of the registered hooks in protected arrays and calls WordPress' own `add_action` and `add_filter` functions on its own `run` action.

{% hint style="info" %}
The handler's `run` and `reset` methods are automatically called when the hooks service's respective methods are called.
{% endhint %}

### Bypassing the late registration

This approach arguably has its downsides. For example, you can't use any of your own hooks during plugin initialization (because that's when you're supposed to call `run` on the service). If you want to bypass this, there are 2 options available:

1. Just don't use the service. We think the advantages outweigh the disadvantages, but that's for you to decide.
2. Write your own custom handler implementing the [`HooksHandlerInterface`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Hooks/HooksHandlerInterface.php) interface and have it call the WP API directly.

### Available Traits

There are 3 traits available for working with the hooks service.&#x20;

First there is the [`HooksServiceAwareTrait`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Hooks/HooksServiceAwareTrait.php) and the corresponding [`HooksServiceAwareInterface`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Hooks/HooksServiceAwareInterface.php). Basically this allows you to call upon the hooks service instance from anywhere within the object. Technically, you can also use more than one service in your plugin and register different ones with different objects.

```php
<?php

namespace DeepWebSolutions\Plugins\MyPlugin;

use DeepWebSolutions\Framework\Utilities\Hooks\HooksService;
use DeepWebSolutions\Framework\Utilities\Hooks\HooksServiceAwareInterface ;
use DeepWebSolutions\Framework\Utilities\Hooks\HooksServiceAwareTrait;

defined( 'ABSPATH' ) || exit;

class MyClass implements HooksServiceAwareInterface {
    use HooksServiceAwareTrait;
    
    public function register_my_hooks() {
        $hooks_service = $this->get_hooks_service();
        $hooks_service->add_filter( 'dws_myplugin_filter', $this, 'filter_value' );
    }
    
    public function filter_value( $value_to_filter ) {
        // ...modify 'value_to_filter'
        return $value_to_filter;
    }
}

$hooks_service = new HooksService( $plugin_instance, $logging_service_instance );

$my_class = new MyClass();
$my_class->set_hooks_service( $hooks_service );
$my_class->register_my_hooks();

$hooks_service->run();

```

The second method involves injecting the hooks service from outside the instance. This behavior is modelled by the [`HooksServiceRegisterInterface`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Hooks/HooksServiceRegisterInterface.php) interface and the [`HooksServiceRegisterTrait`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Hooks/HooksServiceRegisterTrait.php).

```php
<?php

namespace DeepWebSolutions\Plugins\MyPlugin;

use DeepWebSolutions\Framework\Utilities\Hooks\HooksService;
use DeepWebSolutions\Framework\Utilities\Hooks\HooksServiceRegisterInterface;
use DeepWebSolutions\Framework\Utilities\Hooks\HooksServiceRegisterTrait;

defined( 'ABSPATH' ) || exit;

class MyClass implements HooksServiceRegisterInterface {
    use HooksServiceRegisterTrait;
    
    public function register_hooks( HooksService $hooks_service ) {
        $hooks_service->add_filter( $this->get_hook_tag( 'dws_myplugin_filter' ), $this, 'filter_value' );
    }
    
    public function filter_value( $value_to_filter ) {
        // ...modify 'value_to_filter'
        return $value_to_filter;
    }
}

$hooks_service = new HooksService( $plugin_instance, $logging_service_instance );

$my_class = new MyClass();
$my_class->register_hooks( $hooks_service );

$hooks_service->run();

```

The recommended way, however, is to use the [`SetupHooksTrait`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Actions/Setupable/SetupHooksTrait.php) action trait. It's an [action extension trait](/foundations-module/actions/extension-action-traits.md) for automagically calling the aforementioned `register_hooks` methods upon the `setup` action. If attempts to obtain an instance of the `HooksService` either from the object itself (if it implements the `HooksServiceAwareInterface` interface) or from a[ dependency injection](/key-concepts-and-dev-tools/dependency-injection-php-di.md) container.

There is also an accompanying [`InitializeHooksServiceTrait`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Actions/Initializable/InitializeHooksServiceTrait.php) action trait. This one attempts to set the hooks service on the instance by first querying its parent and lastly the [dependency injection](/key-concepts-and-dev-tools/dependency-injection-php-di.md) container for an instance.

Putting it all together, your code could look something like this:

```php
<?php

namespace DeepWebSolutions\Plugins\MyPlugin;

use DeepWebSolutions\Framework\Foundations\Actions\InitializableInterface;
use DeepWebSolutions\Framework\Foundations\Actions\Initializable\InitializableTrait;
use DeepWebSolutions\Framework\Foundations\Actions\SetupableInterface;
use DeepWebSolutions\Framework\Foundations\Actions\Setupable\SetupableTrait;

use DeepWebSolutions\Framework\Foundations\Utilities\DependencyInjection\ContainerAwareInterface;
use DeepWebSolutions\Framework\Foundations\Utilities\DependencyInjection\ContainerAwareTrait;

use DeepWebSolutions\Framework\Utilities\Actions\Initializable\InitializeHooksServiceTrait;
use DeepWebSolutions\Framework\Utilities\Actions\Setupable\SetupHooksTrait;

use DeepWebSolutions\Framework\Utilities\Hooks\HooksService;
use DeepWebSolutions\Framework\Utilities\Hooks\HooksServiceRegisterInterface;
use DeepWebSolutions\Framework\Utilities\Hooks\HooksServiceRegisterTrait;

defined( 'ABSPATH' ) || exit;

class MyClass implements ContainerAwareInterface, InitializableInterface, SetupableInterface {
    use ContainerAwareTrait;
    use InitializeHooksServiceTrait;
    use InitializableTrait;
    use SetupableTrait;
    use SetupHooksTrait;
    
    public function register_hooks( HooksService $hooks_service ) {
        // register your hooks in here...
    }
}

$my_class = new MyClass();
$my_class->set_container( $my_dependency_injection_container );

$my_class->initialize(); // the hooks service will be set on the instance from the DI container
$my_class->setup(); // the 'register_hooks' method will be automagically called


```

{% hint style="info" %}
If the number of imported interfaces and traits in the example above is scarring you, you can find some comfort in the fact that this is an extreme example built from scratch! Normally you would create pre-built abstract objects that implement most of these features and simply extend them. That's basically what our Core Module does!
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://framework.deep-web-solutions.com/utilities-module/hooks-service.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
