|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silk; |
|
4
|
|
|
|
|
5
|
|
|
class Hook |
|
6
|
|
|
{ |
|
7
|
|
|
protected $handle; |
|
8
|
|
|
|
|
9
|
|
|
protected $callback; |
|
10
|
|
|
|
|
11
|
|
|
protected $callbackParamCount; |
|
12
|
|
|
|
|
13
|
|
|
protected $priority; |
|
14
|
|
|
|
|
15
|
|
|
protected $iterations; |
|
16
|
|
|
|
|
17
|
|
|
protected $maxIterations; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a new Hook instance |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $handle action or filter handle |
|
24
|
|
|
* @param int $priority |
|
25
|
|
|
* @return static instance |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function on($handle, $priority = 10) |
|
28
|
|
|
{ |
|
29
|
|
|
return new static($handle, $priority); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create a new Hook instance |
|
34
|
|
|
* @param string $handle action or filter handle |
|
35
|
|
|
* @param int $priority |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($handle, $priority = 10) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->handle = $handle; |
|
40
|
|
|
$this->priority = $priority; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* [setCallback description] |
|
45
|
|
|
* @param callable $callback [description] |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setCallback(callable $callback) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->callback = new Callback($callback); |
|
50
|
|
|
$this->callbackParamCount = $this->callback->reflect()->getNumberOfParameters(); |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set the hook in WP |
|
57
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
58
|
|
|
*/ |
|
59
|
|
|
public function listen() |
|
60
|
|
|
{ |
|
61
|
|
|
add_filter($this->handle, [$this, 'mediateCallback'], $this->priority, 100); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Unset the hook in WP |
|
68
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
|
|
public function remove() |
|
71
|
|
|
{ |
|
72
|
|
|
remove_filter($this->handle, [$this, 'mediateCallback'], $this->priority); |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* [mediateCallback description] |
|
79
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function mediateCallback($given = null) |
|
82
|
|
|
{ |
|
83
|
|
|
if (! $this->shouldInvoke(func_get_args())) { |
|
84
|
|
|
return $given; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->invokeCallback(func_get_args()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function shouldInvoke(array $args) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->hasExceededIterations()) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* [invokeCallback description] |
|
101
|
|
|
* @param [type] $arguments [description] |
|
|
|
|
|
|
102
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
103
|
|
|
*/ |
|
104
|
|
|
protected function invokeCallback($arguments) |
|
105
|
|
|
{ |
|
106
|
|
|
$arguments = array_slice($arguments, 0, $this->callbackParamCount ?: null); |
|
107
|
|
|
|
|
108
|
|
|
$this->iterations++; |
|
109
|
|
|
|
|
110
|
|
|
return $this->callback->callArray($arguments); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* [once description] |
|
115
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
116
|
|
|
*/ |
|
117
|
|
|
public function once() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->onlyXtimes(1); |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* [onlyXtimes description] |
|
126
|
|
|
* @param [type] $times [description] |
|
|
|
|
|
|
127
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
128
|
|
|
*/ |
|
129
|
|
|
public function onlyXtimes($times) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->maxIterations = (int) $times; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Prevent the callback from being triggered again |
|
138
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
139
|
|
|
*/ |
|
140
|
|
|
public function bypass() |
|
141
|
|
|
{ |
|
142
|
|
|
$this->onlyXtimes(0); |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* [withPriority description] |
|
149
|
|
|
* @param [type] $priority [description] |
|
|
|
|
|
|
150
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
151
|
|
|
*/ |
|
152
|
|
|
public function withPriority($priority) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->remove(); |
|
155
|
|
|
|
|
156
|
|
|
$this->priority = $priority; |
|
157
|
|
|
|
|
158
|
|
|
$this->listen(); |
|
159
|
|
|
|
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* [hasExceededIterations description] |
|
165
|
|
|
* @return boolean [description] |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function hasExceededIterations() |
|
168
|
|
|
{ |
|
169
|
|
|
return ($this->maxIterations > -1) && ($this->iterations >= $this->maxIterations); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.