-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionHooks.php
259 lines (234 loc) · 6.84 KB
/
ActionHooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class ActionHooks
// Instance of class
{
public static $hooks_instance;
public static $actions;
public static $current_action;
public static $run_actions;
public static function instance()
{
if ( !self::$hooks_instance ){
self::$hooks_instance = new Action_hooks();
}
return self::$hooks_instance;
}
/**
* Add Action
*
* Add a new hook trigger action
*
* @param mixed $name
* @param mixed $function
* @param mixed $priority
* @return bool
*/
public function add_action($name, $function, $priority = 10)
{
if ( is_string($function) ){
// If we have already registered this action return true
if ( isset(self::$actions[$name][$priority][$function]) ){
return true;
}
}elseif ( is_array($function) ){
// Class
if ( isset(self::$actions[$name][$priority][get_class($function[0]) . '-' . $function[1]]) ){
return true;
}
}
/**
* Allows us to iterate through multiple action hooks.
*/
if ( is_array($name) ){
foreach ($name as $name){
// Store the action hook in the $hooks array
if ( is_string($function) ){
// Store the action hook in the $hooks array
self::$actions[$name][$priority][$function] = [
'function' => $function,
];
}elseif ( is_array($function) ){
self::$actions[$name][$priority][get_class($function[0]) . '-' . $function[1]] = [
'class' => $function[0],
'method' => $function[1],
];
}
}
}else{
if ( is_string($function) ){
// Store the action hook in the $hooks array
self::$actions[$name][$priority][$function] = [
'function' => $function,
];
}elseif ( is_array($function) ){
/** @var TYPE_NAME $name */
self::$actions[$name][$priority][get_class($function[0]) . '-' . $function[1]] = [
'class' => $function[0],
'method' => $function[1],
];
}
}
return true;
}
/**
* Do Action
*
* Trigger an action for a particular action hook
*
* @param mixed $name
* @param mixed $arguments
* @return mixed
*/
public function do_action($name, $arguments = '')
{
// Oh, no you didn't. Are you trying to run an action hook that doesn't exist?
if ( !isset(self::$actions[$name]) ){
return $arguments;
}
// Set the current running hook to this
self::$current_action = $name;
// Key sort our action hooks
ksort(self::$actions[$name]);
foreach (self::$actions[$name] as $priority => $names){
if ( is_array($names) ){
foreach ($names as $name){
if ( isset($name['function']) ){
$return = call_user_func_array($name['function'], [
&$arguments,
]);
if ( $return ){
$arguments = $return;
}
self::$run_actions[$name][$priority];
}else{
if ( method_exists($name['class'], $name['method']) ){
$return = call_user_func_array([$name['class'], $name['method']], [
&$arguments,
]);
if ( $return ){
$arguments = $return;
}
self::$run_actions[get_class($name['class']) . '-' . $name['method']][$priority];
}
}
}
}
}
self::$current_action = '';
return $arguments;
}
/**
* Remove Action
*
* Remove an action hook. No more needs to be said.
*
* @param mixed $name
* @param mixed $function
* @param mixed $priority
* @return bool
*/
public function remove_action($name, $function, $priority = 10)
{
if ( !is_array($function) ){
// If the action hook doesn't, just return true
if ( !isset(self::$actions[$name][$priority][$function]) ){
return true;
}
// Remove the action hook from our hooks array
unset(self::$actions[$name][$priority][$function]);
}elseif ( is_array($function) ){
if ( !isset(self::$actions[$name][$priority][$function[0] . '-' . $function[1]]) ){
return true;
}
// Remove the action hook from our hooks array
unset(self::$actions[$name][$priority][$function[0] . '-' . $function[1]]);
}
}
/**
* Current Action
*
* Get the currently running action hook
*
*/
public function current_action()
{
return self::$current_action;
}
/**
* Has Run
*
* Check if a particular hook has been run
*
* @param $action
* @param mixed $priority
* @return bool
*/
public function has_run($action, $priority = 10)
{
if ( isset(self::$actions[$action][$priority]) ){
return true;
}
return false;
}
/**
* Action Exists
*
* Does a particular action hook even exist?
*
* @param mixed $name
* @return bool
*/
public function action_exists($name)
{
if ( isset(self::$actions[$name]) ){
return true;
}
return false;
}
}
/**
* Add a new action hook
*
* @param mixed $name
* @param mixed $function
* @param mixed $priority
* @return bool
*/
function add_action($name, $function, $priority = 10)
{
return Action_hooks::instance()->add_action($name, $function, $priority);
}
/**
* Run an action
*
* @param mixed $name
* @param mixed $arguments
* @return mixed
*/
function do_action($name, $arguments = '')
{
return Action_hooks::instance()->do_action($name, $arguments);
}
/**
* Remove an action
*
* @param mixed $name
* @param mixed $function
* @param mixed $priority
* @return bool
*/
function remove_action($name, $function, $priority = 10)
{
return Action_hooks::instance()->remove_action($name, $function, $priority);
}
/**
* Check if an action exists
*
* @param mixed $name
* @return bool
*/
function action_exists($name)
{
return Action_hooks::instance()->action_exists($name);
}