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
|
6 |
|
public function __construct() { |
46
|
6 |
|
$this->actions = array(); |
47
|
6 |
|
$this->filters = array(); |
48
|
6 |
|
} |
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
|
|
|
* @since 1.0.0 |
59
|
|
|
*/ |
60
|
2 |
|
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
61
|
2 |
|
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Add a new filter to the collection to be registered with WordPress. |
66
|
|
|
* |
67
|
|
|
* @param string $hook The name of the WordPress filter that is being registered. |
68
|
|
|
* @param object $component A reference to the instance of the object on which the filter is defined. |
69
|
|
|
* @param string $callback The name of the function definition on the $component. |
70
|
|
|
* @param int $priority Optional. The priority at which the function should be fired. Default is 10. |
71
|
|
|
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
72
|
|
|
* @since 1.0.0 |
73
|
|
|
*/ |
74
|
|
|
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
75
|
|
|
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* A utility function that is used to register the actions and hooks into a single |
80
|
|
|
* collection. |
81
|
|
|
* |
82
|
|
|
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
83
|
|
|
* @param string $hook The name of the WordPress filter that is being registered. |
84
|
|
|
* @param object $component A reference to the instance of the object on which the filter is defined. |
85
|
|
|
* @param string $callback The name of the function definition on the $component. |
86
|
|
|
* @param int $priority The priority at which the function should be fired. |
87
|
|
|
* @param int $accepted_args The number of arguments that should be passed to the $callback. |
88
|
|
|
* @return array The collection of actions and filters registered with WordPress. |
89
|
|
|
* @since 1.0.0 |
90
|
|
|
*/ |
91
|
2 |
|
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
92
|
2 |
|
$hooks[] = array( |
93
|
2 |
|
'hook' => $hook, |
94
|
2 |
|
'component' => $component, |
95
|
2 |
|
'callback' => $callback, |
96
|
2 |
|
'priority' => $priority, |
97
|
2 |
|
'accepted_args' => $accepted_args, |
98
|
|
|
); |
99
|
|
|
|
100
|
2 |
|
return $hooks; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Register the filters and actions with WordPress. |
105
|
|
|
* |
106
|
|
|
* @since 1.0.0 |
107
|
|
|
*/ |
108
|
6 |
|
public function run() { |
109
|
6 |
|
foreach ( $this->filters as $hook ) { |
110
|
|
|
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
111
|
|
|
} |
112
|
|
|
|
113
|
6 |
|
foreach ( $this->actions as $hook ) { |
114
|
2 |
|
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
115
|
|
|
} |
116
|
6 |
|
} |
117
|
|
|
} |
118
|
|
|
|