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
|
|
|
* @access protected |
28
|
|
|
* @var array $actions The actions registered with WordPress to fire when the plugin loads. |
29
|
|
|
* @since 1.0.0 |
30
|
|
|
*/ |
31
|
|
|
protected $actions; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The array of filters registered with WordPress. |
35
|
|
|
* |
36
|
|
|
* @access protected |
37
|
|
|
* @var array $filters The filters registered with WordPress to fire when the plugin loads. |
38
|
|
|
* @since 1.0.0 |
39
|
|
|
*/ |
40
|
|
|
protected $filters; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initialize the collections used to maintain the actions and filters. |
44
|
|
|
* |
45
|
|
|
* @since 1.0.0 |
46
|
|
|
*/ |
47
|
2 |
|
public function __construct() { |
48
|
2 |
|
$this->actions = array(); |
49
|
2 |
|
$this->filters = array(); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Add a new action to the collection to be registered with WordPress. |
54
|
|
|
* |
55
|
|
|
* @param string $hook The name of the WordPress action that is being registered. |
56
|
|
|
* @param object $component A reference to the instance of the object on which the action is defined. |
57
|
|
|
* @param string $callback The name of the function definition on the $component. |
58
|
|
|
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
59
|
|
|
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
60
|
|
|
* @since 1.0.0 |
61
|
|
|
*/ |
62
|
2 |
|
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
63
|
2 |
|
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); |
64
|
2 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add a new filter to the collection to be registered with WordPress. |
68
|
|
|
* |
69
|
|
|
* @param string $hook The name of the WordPress filter that is being registered. |
70
|
|
|
* @param object $component A reference to the instance of the object on which the filter is defined. |
71
|
|
|
* @param string $callback The name of the function definition on the $component. |
72
|
|
|
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
73
|
|
|
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
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
|
|
|
* @access private |
85
|
|
|
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
86
|
|
|
* @param string $hook The name of the WordPress filter that is being registered. |
87
|
|
|
* @param object $component A reference to the instance of the object on which the filter is defined. |
88
|
|
|
* @param string $callback The name of the function definition on the $component. |
89
|
|
|
* @param int $priority The priority at which the function should be fired. |
90
|
|
|
* @param int $accepted_args The number of arguments that should be passed to the $callback. |
91
|
|
|
* @return array The collection of actions and filters registered with WordPress. |
92
|
|
|
* @since 1.0.0 |
93
|
|
|
*/ |
94
|
2 |
|
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
95
|
2 |
|
$hooks[] = array( |
96
|
2 |
|
'hook' => $hook, |
97
|
2 |
|
'component' => $component, |
98
|
2 |
|
'callback' => $callback, |
99
|
2 |
|
'priority' => $priority, |
100
|
2 |
|
'accepted_args' => $accepted_args, |
101
|
|
|
); |
102
|
|
|
|
103
|
2 |
|
return $hooks; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Register the filters and actions with WordPress. |
108
|
|
|
* |
109
|
|
|
* @since 1.0.0 |
110
|
|
|
*/ |
111
|
2 |
|
public function run() { |
112
|
2 |
|
foreach ( $this->filters as $hook ) { |
113
|
|
|
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
foreach ( $this->actions as $hook ) { |
117
|
2 |
|
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
118
|
|
|
} |
119
|
2 |
|
} |
120
|
|
|
} |
121
|
|
|
|