Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Hook_Manager
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add_action
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 add_filter
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 reset
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/* HEADER */ // phpcs:ignore
3
4/**
5 * Allows the latest autoloader to register hooks that can be removed when the autoloader is reset.
6 */
7class Hook_Manager {
8
9    /**
10     * An array containing all of the hooks that we've registered.
11     *
12     * @var array
13     */
14    private $registered_hooks;
15
16    /**
17     * The constructor.
18     */
19    public function __construct() {
20        $this->registered_hooks = array();
21    }
22
23    /**
24     * Adds an action to WordPress and registers it internally.
25     *
26     * @param string   $tag           The name of the action which is hooked.
27     * @param callable $callable      The function to call.
28     * @param int      $priority      Used to specify the priority of the action.
29     * @param int      $accepted_args Used to specify the number of arguments the callable accepts.
30     */
31    public function add_action( $tag, $callable, $priority = 10, $accepted_args = 1 ) {
32        $this->registered_hooks[ $tag ][] = array(
33            'priority' => $priority,
34            'callable' => $callable,
35        );
36
37        add_action( $tag, $callable, $priority, $accepted_args );
38    }
39
40    /**
41     * Adds a filter to WordPress and registers it internally.
42     *
43     * @param string   $tag           The name of the filter which is hooked.
44     * @param callable $callable      The function to call.
45     * @param int      $priority      Used to specify the priority of the filter.
46     * @param int      $accepted_args Used to specify the number of arguments the callable accepts.
47     */
48    public function add_filter( $tag, $callable, $priority = 10, $accepted_args = 1 ) {
49        $this->registered_hooks[ $tag ][] = array(
50            'priority' => $priority,
51            'callable' => $callable,
52        );
53
54        add_filter( $tag, $callable, $priority, $accepted_args );
55    }
56
57    /**
58     * Removes all of the registered hooks.
59     */
60    public function reset() {
61        foreach ( $this->registered_hooks as $tag => $hooks ) {
62            foreach ( $hooks as $hook ) {
63                remove_filter( $tag, $hook['callable'], $hook['priority'] );
64            }
65        }
66        $this->registered_hooks = array();
67    }
68}