Completed
Push — update/show-recurring-payments... ( 106a5e...b030b6 )
by
unknown
415:44 queued 407:29
created

Hook_Manager::add_action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 8
loc 8
rs 10
c 0
b 0
f 0
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
 */
7
class 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 View Code Duplication
	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 View Code Duplication
	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
}
69