Hook_Loader::run()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 3
eloc 4
nc 4
nop 0
crap 3.072
1
<?php
2
/**
3
 * Register all actions and filters for the plugin
4
 *
5
 * Maintain a list of all hooks that are registered throughout
6
 * the plugin, and register them with the WordPress API. Call the
7
 * run function to execute the list of actions and filters.
8
 *
9
 * @package WPSiteMonitor
10
 * @link https://github.com/BWibrew/WP-Site-Monitor/
11
 * @author Benjamin Wibrew <[email protected]>
12
 * @since 1.0.0
13
 */
14
15
namespace WPSiteMonitor;
16
17
/**
18
 * Class Hook_Loader
19
 *
20
 * @package WPSiteMonitor
21
 */
22
class Hook_Loader {
23
24
	/**
25
	 * The array of actions registered with WordPress.
26
	 *
27
	 * @var array $actions The actions registered with WordPress to fire when the plugin loads.
28
	 * @since 1.0.0
29
	 */
30
	protected $actions;
31
32
	/**
33
	 * The array of filters registered with WordPress.
34
	 *
35
	 * @var array $filters The filters registered with WordPress to fire when the plugin loads.
36
	 * @since 1.0.0
37
	 */
38
	protected $filters;
39
40
	/**
41
	 * Initialize the collections used to maintain the actions and filters.
42
	 *
43
	 * @since 1.0.0
44
	 */
45 7
	public function __construct() {
46 7
		$this->actions = array();
47 7
		$this->filters = array();
48 7
	}
49
50
	/**
51
	 * Add a new action to the collection to be registered with WordPress.
52
	 *
53
	 * @param string $hook The name of the WordPress action that is being registered.
54
	 * @param object $component A reference to the instance of the object on which the action is defined.
55
	 * @param string $callback The name of the function definition on the $component.
56
	 * @param int    $priority Optional. The priority at which the function should be fired. Default is 10.
57
	 * @param int    $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
58
	 *
59
	 * @since    1.0.0
60
	 */
61 6
	public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
62 6
		$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
63 6
	}
64
65
	/**
66
	 * Add a new filter to the collection to be registered with WordPress.
67
	 *
68
	 * @param string $hook The name of the WordPress filter that is being registered.
69
	 * @param object $component A reference to the instance of the object on which the filter is defined.
70
	 * @param string $callback The name of the function definition on the $component.
71
	 * @param int    $priority Optional. The priority at which the function should be fired. Default is 10.
72
	 * @param int    $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
73
	 *
74
	 * @since    1.0.0
75
	 */
76
	public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
77
		$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
78
	}
79
80
	/**
81
	 * A utility function that is used to register the actions and hooks into a single
82
	 * collection.
83
	 *
84
	 * @param array  $hooks The collection of hooks that is being registered (that is, actions or filters).
85
	 * @param string $hook The name of the WordPress filter that is being registered.
86
	 * @param object $component A reference to the instance of the object on which the filter is defined.
87
	 * @param string $callback The name of the function definition on the $component.
88
	 * @param int    $priority The priority at which the function should be fired.
89
	 * @param int    $accepted_args The number of arguments that should be passed to the $callback.
90
	 *
91
	 * @return array The collection of actions and filters registered with WordPress.
92
	 * @since 1.0.0
93
	 */
94 6
	private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
95 6
		$hooks[] = array(
96 6
			'hook'          => $hook,
97 6
			'component'     => $component,
98 6
			'callback'      => $callback,
99 6
			'priority'      => $priority,
100 6
			'accepted_args' => $accepted_args,
101
		);
102
103 6
		return $hooks;
104
	}
105
106
	/**
107
	 * Register the filters and actions with WordPress.
108
	 *
109
	 * @since 1.0.0
110
	 */
111 7
	public function run() {
112 7
		foreach ( $this->filters as $hook ) {
113
			add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
114
		}
115
116 7
		foreach ( $this->actions as $hook ) {
117 6
			add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
118
		}
119 7
	}
120
}
121