1 | <?php |
||
9 | class Hook |
||
10 | { |
||
11 | /** |
||
12 | * The action or filter handle attached to. |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $handle; |
||
16 | |||
17 | /** |
||
18 | * The callback object holding the target callable. |
||
19 | * @var Callback |
||
20 | */ |
||
21 | protected $callback; |
||
22 | |||
23 | /** |
||
24 | * The number of parameters defined in the callback's signature. |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $callbackParamCount; |
||
28 | |||
29 | /** |
||
30 | * The action or filter priority the callback is registered on. |
||
31 | * @var mixed |
||
32 | */ |
||
33 | protected $priority; |
||
34 | |||
35 | /** |
||
36 | * The number of times the callback has been invoked. |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $iterations; |
||
40 | |||
41 | /** |
||
42 | * The maximum number of iterations allowed for the callback to be invoked. |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $maxIterations; |
||
46 | |||
47 | /** |
||
48 | * A collection of conditions which control the the invocation of the callback. |
||
49 | * @var Collection |
||
50 | */ |
||
51 | protected $conditions; |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Create a new Hook instance. |
||
56 | * |
||
57 | * @param string $handle Action or filter handle |
||
58 | * @param int $priority |
||
59 | * |
||
60 | * @return static |
||
61 | */ |
||
62 | public static function on($handle, $priority = 10) |
||
66 | |||
67 | /** |
||
68 | * Create a new Hook instance. |
||
69 | * |
||
70 | * @param string $handle Action or filter handle |
||
71 | * @param int $priority |
||
72 | */ |
||
73 | public function __construct($handle, $priority = 10) |
||
78 | |||
79 | /** |
||
80 | * Set the callback to be invoked by the action or filter. |
||
81 | * |
||
82 | * @param callable $callback The callback to be invoked |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function setCallback(callable $callback) |
||
93 | |||
94 | /** |
||
95 | * Set the hook in WordPress. |
||
96 | * |
||
97 | * Both actions and filters are registered as filters. |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function listen() |
||
107 | |||
108 | /** |
||
109 | * Unset the hook in WordPress. |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function remove() |
||
119 | |||
120 | /** |
||
121 | * Control invocation of the callback. |
||
122 | * |
||
123 | * @param mixed $given The first argument passed to the callback. |
||
124 | * Needed to return for filters. |
||
125 | * |
||
126 | * @return mixed Returned value from Callback |
||
127 | */ |
||
128 | public function mediateCallback($given = null) |
||
142 | |||
143 | /** |
||
144 | * Whether or not the callback should be invoked. |
||
145 | * |
||
146 | * @param array $arguments All arguments passed to the callback |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function shouldInvoke(array $arguments) |
||
164 | |||
165 | /** |
||
166 | * Call the callback. |
||
167 | * |
||
168 | * @param array $arguments All arguments passed to the callback |
||
169 | * |
||
170 | * @return mixed The value returned from the callback |
||
171 | */ |
||
172 | protected function invokeCallback($arguments) |
||
182 | |||
183 | /** |
||
184 | * Set the callback to only be invokable one time. |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function once() |
||
194 | |||
195 | /** |
||
196 | * Set the maximum number of callback invocations to allow. |
||
197 | * |
||
198 | * @param int $times The maximum iterations of invocations to allow |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function onlyXtimes($times) |
||
208 | |||
209 | /** |
||
210 | * Prevent the callback from being triggered again. |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function bypass() |
||
220 | |||
221 | /** |
||
222 | * Set the priority the callback should be registered with. |
||
223 | * |
||
224 | * @param mixed $priority The callback priority |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function withPriority($priority) |
||
238 | |||
239 | /** |
||
240 | * Add a condition to control the invocation of the callback. |
||
241 | * |
||
242 | * @param callable $condition A function to evaluate a condition before the |
||
243 | * hook's callback is invoked. |
||
244 | * If the function returns false, the callback |
||
245 | * will not be invoked. |
||
246 | * |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function onlyIf(callable $condition) |
||
255 | |||
256 | /** |
||
257 | * Add a negated condition to control the invocation of the callback. |
||
258 | * |
||
259 | * @param callable $condition |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function onlyIfNot(callable $condition) |
||
269 | |||
270 | /** |
||
271 | * Get the collection of callback invocation conditions. |
||
272 | * |
||
273 | * @return Collection |
||
274 | */ |
||
275 | protected function conditions() |
||
283 | |||
284 | /** |
||
285 | * Whether or not the callback has reached the limit of allowed invocations. |
||
286 | * |
||
287 | * @return boolean true for limit reached/exceeded, otherwise false |
||
288 | */ |
||
289 | protected function hasExceededIterations() |
||
293 | } |
||
294 |