author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* The plugin API is located in this file, which allows for creating actions |
|
4 |
* and filters and hooking functions, and methods. The functions or methods will |
|
5 |
* then be run when the action or filter is called. |
|
6 |
* |
|
7 |
* The API callback examples reference functions, but can be methods of classes. |
|
8 |
* To hook methods, you'll need to pass an array one of two ways. |
|
9 |
* |
|
10 |
* Any of the syntaxes explained in the PHP documentation for the |
|
16 | 11 |
* {@link https://www.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'} |
0 | 12 |
* type are valid. |
13 |
* |
|
16 | 14 |
* Also see the {@link https://developer.wordpress.org/plugins/ Plugin API} for |
0 | 15 |
* more information and examples on how to use a lot of these functions. |
16 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* This file should have no external dependencies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* |
0 | 19 |
* @package WordPress |
20 |
* @subpackage Plugin |
|
5 | 21 |
* @since 1.5.0 |
0 | 22 |
*/ |
23 |
||
24 |
// Initialize the filter globals. |
|
16 | 25 |
require __DIR__ . '/class-wp-hook.php'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
/** @var WP_Hook[] $wp_filter */ |
18 | 28 |
global $wp_filter; |
29 |
||
30 |
/** @var int[] $wp_actions */ |
|
31 |
global $wp_actions; |
|
32 |
||
33 |
/** @var string[] $wp_current_filter */ |
|
34 |
global $wp_current_filter; |
|
0 | 35 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
if ( $wp_filter ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
$wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
} else { |
0 | 39 |
$wp_filter = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
} |
0 | 41 |
|
9 | 42 |
if ( ! isset( $wp_actions ) ) { |
0 | 43 |
$wp_actions = array(); |
9 | 44 |
} |
0 | 45 |
|
9 | 46 |
if ( ! isset( $wp_current_filter ) ) { |
0 | 47 |
$wp_current_filter = array(); |
9 | 48 |
} |
0 | 49 |
|
50 |
/** |
|
18 | 51 |
* Adds a callback function to a filter hook. |
0 | 52 |
* |
53 |
* WordPress offers filter hooks to allow plugins to modify |
|
54 |
* various types of internal data at runtime. |
|
55 |
* |
|
56 |
* A plugin can modify data by binding a callback to a filter hook. When the filter |
|
57 |
* is later applied, each bound callback is run in order of priority, and given |
|
58 |
* the opportunity to modify a value by returning a new value. |
|
59 |
* |
|
60 |
* The following example shows how a callback function is bound to a filter hook. |
|
5 | 61 |
* |
62 |
* Note that `$example` is passed to the callback, (maybe) modified, then returned: |
|
0 | 63 |
* |
5 | 64 |
* function example_callback( $example ) { |
65 |
* // Maybe modify $example in some way. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* return $example; |
5 | 67 |
* } |
68 |
* add_filter( 'example_filter', 'example_callback' ); |
|
0 | 69 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* Bound callbacks can accept from none to the total number of arguments passed as parameters |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* in the corresponding apply_filters() call. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
* In other words, if an apply_filters() call passes four total arguments, callbacks bound to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
* it can accept none (the same as 1) of the arguments or up to four. The important part is that |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
* the `$accepted_args` value must reflect the number of arguments the bound callback *actually* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
* opted to accept. If no arguments were accepted by the callback that is considered to be the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
* same as accepting 1 argument. For example: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* // Filter call. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
* $value = apply_filters( 'hook', $value, $arg2, $arg3 ); |
0 | 81 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* // Accepting zero/one arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
* function example_callback() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
* ... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
* return 'some value'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
* // Accepting two arguments (three possible). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
* function example_callback( $value, $arg2 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
* ... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
* return $maybe_modified_value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
* *Note:* The function will return true whether or not the callback is valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
* It is up to you to take care. This is done for optimization purposes, so |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
* everything is as quick as possible. |
0 | 99 |
* |
100 |
* @since 0.71 |
|
101 |
* |
|
18 | 102 |
* @global WP_Hook[] $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. |
5 | 103 |
* |
18 | 104 |
* @param string $hook_name The name of the filter to add the callback to. |
105 |
* @param callable $callback The callback to be run when the filter is applied. |
|
106 |
* @param int $priority Optional. Used to specify the order in which the functions |
|
107 |
* associated with a particular filter are executed. |
|
108 |
* Lower numbers correspond with earlier execution, |
|
109 |
* and functions with the same priority are executed |
|
110 |
* in the order in which they were added to the filter. Default 10. |
|
111 |
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
|
112 |
* @return true Always returns true. |
|
0 | 113 |
*/ |
18 | 114 |
function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
global $wp_filter; |
0 | 116 |
|
18 | 117 |
if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
118 |
$wp_filter[ $hook_name ] = new WP_Hook(); |
|
5 | 119 |
} |
120 |
||
18 | 121 |
$wp_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args ); |
122 |
||
123 |
return true; |
|
0 | 124 |
} |
125 |
||
126 |
/** |
|
16 | 127 |
* Calls the callback functions that have been added to a filter hook. |
0 | 128 |
* |
18 | 129 |
* This function invokes all functions attached to filter hook `$hook_name`. |
130 |
* It is possible to create new filter hooks by simply calling this function, |
|
131 |
* specifying the name of the new hook using the `$hook_name` parameter. |
|
0 | 132 |
* |
16 | 133 |
* The function also allows for multiple additional arguments to be passed to hooks. |
5 | 134 |
* |
16 | 135 |
* Example usage: |
136 |
* |
|
137 |
* // The filter callback function. |
|
5 | 138 |
* function example_callback( $string, $arg1, $arg2 ) { |
16 | 139 |
* // (maybe) modify $string. |
5 | 140 |
* return $string; |
141 |
* } |
|
142 |
* add_filter( 'example_filter', 'example_callback', 10, 3 ); |
|
0 | 143 |
* |
5 | 144 |
* /* |
16 | 145 |
* * Apply the filters by calling the 'example_callback()' function |
146 |
* * that's hooked onto `example_filter` above. |
|
147 |
* * |
|
148 |
* * - 'example_filter' is the filter hook. |
|
149 |
* * - 'filter me' is the value being filtered. |
|
5 | 150 |
* * - $arg1 and $arg2 are the additional arguments passed to the callback. |
151 |
* $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); |
|
0 | 152 |
* |
153 |
* @since 0.71 |
|
19 | 154 |
* @since 6.0.0 Formalized the existing and already documented `...$args` parameter |
155 |
* by adding it to the function signature. |
|
0 | 156 |
* |
18 | 157 |
* @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
158 |
* @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
|
5 | 159 |
* |
18 | 160 |
* @param string $hook_name The name of the filter hook. |
161 |
* @param mixed $value The value to filter. |
|
162 |
* @param mixed ...$args Additional parameters to pass to the callback functions. |
|
0 | 163 |
* @return mixed The filtered value after all hooked functions are applied to it. |
164 |
*/ |
|
19 | 165 |
function apply_filters( $hook_name, $value, ...$args ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
global $wp_filter, $wp_current_filter; |
0 | 167 |
|
5 | 168 |
// Do 'all' actions first. |
9 | 169 |
if ( isset( $wp_filter['all'] ) ) { |
18 | 170 |
$wp_current_filter[] = $hook_name; |
19 | 171 |
|
172 |
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
|
173 |
_wp_call_all_hook( $all_args ); |
|
0 | 174 |
} |
175 |
||
18 | 176 |
if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
9 | 177 |
if ( isset( $wp_filter['all'] ) ) { |
178 |
array_pop( $wp_current_filter ); |
|
179 |
} |
|
18 | 180 |
|
0 | 181 |
return $value; |
182 |
} |
|
183 |
||
9 | 184 |
if ( ! isset( $wp_filter['all'] ) ) { |
18 | 185 |
$wp_current_filter[] = $hook_name; |
9 | 186 |
} |
0 | 187 |
|
19 | 188 |
// Pass the value to WP_Hook. |
189 |
array_unshift( $args, $value ); |
|
0 | 190 |
|
18 | 191 |
$filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args ); |
0 | 192 |
|
193 |
array_pop( $wp_current_filter ); |
|
194 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
return $filtered; |
0 | 196 |
} |
197 |
||
198 |
/** |
|
16 | 199 |
* Calls the callback functions that have been added to a filter hook, specifying arguments in an array. |
0 | 200 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
* @since 3.0.0 |
5 | 202 |
* |
0 | 203 |
* @see apply_filters() This function is identical, but the arguments passed to the |
18 | 204 |
* functions hooked to `$hook_name` are supplied using an array. |
0 | 205 |
* |
18 | 206 |
* @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
207 |
* @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
|
0 | 208 |
* |
18 | 209 |
* @param string $hook_name The name of the filter hook. |
210 |
* @param array $args The arguments supplied to the functions hooked to `$hook_name`. |
|
0 | 211 |
* @return mixed The filtered value after all hooked functions are applied to it. |
212 |
*/ |
|
18 | 213 |
function apply_filters_ref_array( $hook_name, $args ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
global $wp_filter, $wp_current_filter; |
0 | 215 |
|
16 | 216 |
// Do 'all' actions first. |
9 | 217 |
if ( isset( $wp_filter['all'] ) ) { |
18 | 218 |
$wp_current_filter[] = $hook_name; |
16 | 219 |
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
9 | 220 |
_wp_call_all_hook( $all_args ); |
0 | 221 |
} |
222 |
||
18 | 223 |
if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
9 | 224 |
if ( isset( $wp_filter['all'] ) ) { |
225 |
array_pop( $wp_current_filter ); |
|
226 |
} |
|
18 | 227 |
|
0 | 228 |
return $args[0]; |
229 |
} |
|
230 |
||
9 | 231 |
if ( ! isset( $wp_filter['all'] ) ) { |
18 | 232 |
$wp_current_filter[] = $hook_name; |
9 | 233 |
} |
0 | 234 |
|
18 | 235 |
$filtered = $wp_filter[ $hook_name ]->apply_filters( $args[0], $args ); |
0 | 236 |
|
237 |
array_pop( $wp_current_filter ); |
|
238 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
return $filtered; |
0 | 240 |
} |
241 |
||
242 |
/** |
|
18 | 243 |
* Checks if any filter has been registered for a hook. |
244 |
* |
|
245 |
* When using the `$callback` argument, this function may return a non-boolean value |
|
246 |
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. |
|
247 |
* |
|
248 |
* @since 2.5.0 |
|
249 |
* |
|
250 |
* @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
|
0 | 251 |
* |
19 | 252 |
* @param string $hook_name The name of the filter hook. |
253 |
* @param callable|string|array|false $callback Optional. The callback to check for. |
|
254 |
* This function can be called unconditionally to speculatively check |
|
255 |
* a callback that may or may not exist. Default false. |
|
18 | 256 |
* @return bool|int If `$callback` is omitted, returns boolean for whether the hook has |
257 |
* anything registered. When checking a specific function, the priority |
|
258 |
* of that hook is returned, or false if the function is not attached. |
|
259 |
*/ |
|
260 |
function has_filter( $hook_name, $callback = false ) { |
|
261 |
global $wp_filter; |
|
262 |
||
263 |
if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
|
264 |
return false; |
|
265 |
} |
|
266 |
||
267 |
return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback ); |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* Removes a callback function from a filter hook. |
|
272 |
* |
|
273 |
* This can be used to remove default functions attached to a specific filter |
|
0 | 274 |
* hook and possibly replace them with a substitute. |
275 |
* |
|
18 | 276 |
* To remove a hook, the `$callback` and `$priority` arguments must match |
0 | 277 |
* when the hook was added. This goes for both filters and actions. No warning |
278 |
* will be given on removal failure. |
|
279 |
* |
|
5 | 280 |
* @since 1.2.0 |
0 | 281 |
* |
18 | 282 |
* @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
* |
19 | 284 |
* @param string $hook_name The filter hook to which the function to be removed is hooked. |
285 |
* @param callable|string|array $callback The callback to be removed from running when the filter is applied. |
|
286 |
* This function can be called unconditionally to speculatively remove |
|
287 |
* a callback that may or may not exist. |
|
288 |
* @param int $priority Optional. The exact priority used when adding the original |
|
289 |
* filter callback. Default 10. |
|
18 | 290 |
* @return bool Whether the function existed before it was removed. |
0 | 291 |
*/ |
18 | 292 |
function remove_filter( $hook_name, $callback, $priority = 10 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
global $wp_filter; |
0 | 294 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
295 |
$r = false; |
18 | 296 |
|
297 |
if ( isset( $wp_filter[ $hook_name ] ) ) { |
|
298 |
$r = $wp_filter[ $hook_name ]->remove_filter( $hook_name, $callback, $priority ); |
|
299 |
||
300 |
if ( ! $wp_filter[ $hook_name ]->callbacks ) { |
|
301 |
unset( $wp_filter[ $hook_name ] ); |
|
5 | 302 |
} |
0 | 303 |
} |
304 |
||
305 |
return $r; |
|
306 |
} |
|
307 |
||
308 |
/** |
|
18 | 309 |
* Removes all of the callback functions from a filter hook. |
0 | 310 |
* |
5 | 311 |
* @since 2.7.0 |
0 | 312 |
* |
18 | 313 |
* @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
* |
18 | 315 |
* @param string $hook_name The filter to remove callbacks from. |
316 |
* @param int|false $priority Optional. The priority number to remove them from. |
|
317 |
* Default false. |
|
318 |
* @return true Always returns true. |
|
0 | 319 |
*/ |
18 | 320 |
function remove_all_filters( $hook_name, $priority = false ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
global $wp_filter; |
0 | 322 |
|
18 | 323 |
if ( isset( $wp_filter[ $hook_name ] ) ) { |
324 |
$wp_filter[ $hook_name ]->remove_all_filters( $priority ); |
|
325 |
||
326 |
if ( ! $wp_filter[ $hook_name ]->has_filters() ) { |
|
327 |
unset( $wp_filter[ $hook_name ] ); |
|
5 | 328 |
} |
0 | 329 |
} |
330 |
||
331 |
return true; |
|
332 |
} |
|
333 |
||
334 |
/** |
|
18 | 335 |
* Retrieves the name of the current filter hook. |
0 | 336 |
* |
5 | 337 |
* @since 2.5.0 |
0 | 338 |
* |
18 | 339 |
* @global string[] $wp_current_filter Stores the list of current filters with the current one last |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
* |
18 | 341 |
* @return string Hook name of the current filter. |
0 | 342 |
*/ |
343 |
function current_filter() { |
|
344 |
global $wp_current_filter; |
|
18 | 345 |
|
0 | 346 |
return end( $wp_current_filter ); |
347 |
} |
|
348 |
||
349 |
/** |
|
18 | 350 |
* Returns whether or not a filter hook is currently being processed. |
5 | 351 |
* |
352 |
* The function current_filter() only returns the most recent filter or action |
|
353 |
* being executed. did_action() returns true once the action is initially |
|
354 |
* processed. |
|
355 |
* |
|
18 | 356 |
* This function allows detection for any filter currently being executed |
357 |
* (regardless of whether it's the most recent filter to fire, in the case of |
|
5 | 358 |
* hooks called from hook callbacks) to be verified. |
359 |
* |
|
360 |
* @since 3.9.0 |
|
361 |
* |
|
362 |
* @see current_filter() |
|
363 |
* @see did_action() |
|
18 | 364 |
* @global string[] $wp_current_filter Current filter. |
5 | 365 |
* |
18 | 366 |
* @param null|string $hook_name Optional. Filter hook to check. Defaults to null, |
367 |
* which checks if any filter is currently being run. |
|
5 | 368 |
* @return bool Whether the filter is currently in the stack. |
369 |
*/ |
|
18 | 370 |
function doing_filter( $hook_name = null ) { |
5 | 371 |
global $wp_current_filter; |
372 |
||
18 | 373 |
if ( null === $hook_name ) { |
5 | 374 |
return ! empty( $wp_current_filter ); |
375 |
} |
|
376 |
||
18 | 377 |
return in_array( $hook_name, $wp_current_filter, true ); |
5 | 378 |
} |
379 |
||
380 |
/** |
|
18 | 381 |
* Adds a callback function to an action hook. |
0 | 382 |
* |
383 |
* Actions are the hooks that the WordPress core launches at specific points |
|
384 |
* during execution, or when specific events occur. Plugins can specify that |
|
385 |
* one or more of its PHP functions are executed at these points, using the |
|
386 |
* Action API. |
|
387 |
* |
|
5 | 388 |
* @since 1.2.0 |
0 | 389 |
* |
18 | 390 |
* @param string $hook_name The name of the action to add the callback to. |
391 |
* @param callable $callback The callback to be run when the action is called. |
|
5 | 392 |
* @param int $priority Optional. Used to specify the order in which the functions |
18 | 393 |
* associated with a particular action are executed. |
5 | 394 |
* Lower numbers correspond with earlier execution, |
395 |
* and functions with the same priority are executed |
|
18 | 396 |
* in the order in which they were added to the action. Default 10. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
18 | 398 |
* @return true Always returns true. |
0 | 399 |
*/ |
18 | 400 |
function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { |
401 |
return add_filter( $hook_name, $callback, $priority, $accepted_args ); |
|
0 | 402 |
} |
403 |
||
404 |
/** |
|
18 | 405 |
* Calls the callback functions that have been added to an action hook. |
0 | 406 |
* |
18 | 407 |
* This function invokes all functions attached to action hook `$hook_name`. |
408 |
* It is possible to create new action hooks by simply calling this function, |
|
409 |
* specifying the name of the new hook using the `$hook_name` parameter. |
|
0 | 410 |
* |
16 | 411 |
* You can pass extra arguments to the hooks, much like you can with `apply_filters()`. |
412 |
* |
|
413 |
* Example usage: |
|
414 |
* |
|
415 |
* // The action callback function. |
|
416 |
* function example_callback( $arg1, $arg2 ) { |
|
417 |
* // (maybe) do something with the args. |
|
418 |
* } |
|
419 |
* add_action( 'example_action', 'example_callback', 10, 2 ); |
|
420 |
* |
|
421 |
* /* |
|
422 |
* * Trigger the actions by calling the 'example_callback()' function |
|
423 |
* * that's hooked onto `example_action` above. |
|
424 |
* * |
|
425 |
* * - 'example_action' is the action hook. |
|
426 |
* * - $arg1 and $arg2 are the additional arguments passed to the callback. |
|
427 |
* $value = do_action( 'example_action', $arg1, $arg2 ); |
|
0 | 428 |
* |
5 | 429 |
* @since 1.2.0 |
16 | 430 |
* @since 5.3.0 Formalized the existing and already documented `...$arg` parameter |
431 |
* by adding it to the function signature. |
|
0 | 432 |
* |
18 | 433 |
* @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
434 |
* @global int[] $wp_actions Stores the number of times each action was triggered. |
|
435 |
* @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
|
16 | 436 |
* |
18 | 437 |
* @param string $hook_name The name of the action to be executed. |
438 |
* @param mixed ...$arg Optional. Additional arguments which are passed on to the |
|
439 |
* functions hooked to the action. Default empty. |
|
0 | 440 |
*/ |
18 | 441 |
function do_action( $hook_name, ...$arg ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
global $wp_filter, $wp_actions, $wp_current_filter; |
0 | 443 |
|
18 | 444 |
if ( ! isset( $wp_actions[ $hook_name ] ) ) { |
445 |
$wp_actions[ $hook_name ] = 1; |
|
9 | 446 |
} else { |
18 | 447 |
++$wp_actions[ $hook_name ]; |
9 | 448 |
} |
0 | 449 |
|
16 | 450 |
// Do 'all' actions first. |
9 | 451 |
if ( isset( $wp_filter['all'] ) ) { |
18 | 452 |
$wp_current_filter[] = $hook_name; |
16 | 453 |
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
9 | 454 |
_wp_call_all_hook( $all_args ); |
0 | 455 |
} |
456 |
||
18 | 457 |
if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
9 | 458 |
if ( isset( $wp_filter['all'] ) ) { |
459 |
array_pop( $wp_current_filter ); |
|
460 |
} |
|
18 | 461 |
|
0 | 462 |
return; |
463 |
} |
|
464 |
||
9 | 465 |
if ( ! isset( $wp_filter['all'] ) ) { |
18 | 466 |
$wp_current_filter[] = $hook_name; |
9 | 467 |
} |
0 | 468 |
|
16 | 469 |
if ( empty( $arg ) ) { |
470 |
$arg[] = ''; |
|
471 |
} elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) { |
|
472 |
// Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`. |
|
473 |
$arg[0] = $arg[0][0]; |
|
9 | 474 |
} |
0 | 475 |
|
18 | 476 |
$wp_filter[ $hook_name ]->do_action( $arg ); |
0 | 477 |
|
9 | 478 |
array_pop( $wp_current_filter ); |
0 | 479 |
} |
480 |
||
481 |
/** |
|
16 | 482 |
* Calls the callback functions that have been added to an action hook, specifying arguments in an array. |
0 | 483 |
* |
5 | 484 |
* @since 2.1.0 |
0 | 485 |
* |
5 | 486 |
* @see do_action() This function is identical, but the arguments passed to the |
18 | 487 |
* functions hooked to `$hook_name` are supplied using an array. |
0 | 488 |
* |
18 | 489 |
* @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
490 |
* @global int[] $wp_actions Stores the number of times each action was triggered. |
|
491 |
* @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
|
492 |
* |
|
493 |
* @param string $hook_name The name of the action to be executed. |
|
494 |
* @param array $args The arguments supplied to the functions hooked to `$hook_name`. |
|
0 | 495 |
*/ |
18 | 496 |
function do_action_ref_array( $hook_name, $args ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
global $wp_filter, $wp_actions, $wp_current_filter; |
0 | 498 |
|
18 | 499 |
if ( ! isset( $wp_actions[ $hook_name ] ) ) { |
500 |
$wp_actions[ $hook_name ] = 1; |
|
9 | 501 |
} else { |
18 | 502 |
++$wp_actions[ $hook_name ]; |
9 | 503 |
} |
0 | 504 |
|
16 | 505 |
// Do 'all' actions first. |
9 | 506 |
if ( isset( $wp_filter['all'] ) ) { |
18 | 507 |
$wp_current_filter[] = $hook_name; |
16 | 508 |
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
9 | 509 |
_wp_call_all_hook( $all_args ); |
0 | 510 |
} |
511 |
||
18 | 512 |
if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
9 | 513 |
if ( isset( $wp_filter['all'] ) ) { |
514 |
array_pop( $wp_current_filter ); |
|
515 |
} |
|
18 | 516 |
|
0 | 517 |
return; |
518 |
} |
|
519 |
||
9 | 520 |
if ( ! isset( $wp_filter['all'] ) ) { |
18 | 521 |
$wp_current_filter[] = $hook_name; |
9 | 522 |
} |
0 | 523 |
|
18 | 524 |
$wp_filter[ $hook_name ]->do_action( $args ); |
0 | 525 |
|
9 | 526 |
array_pop( $wp_current_filter ); |
0 | 527 |
} |
528 |
||
529 |
/** |
|
18 | 530 |
* Checks if any action has been registered for a hook. |
531 |
* |
|
532 |
* When using the `$callback` argument, this function may return a non-boolean value |
|
533 |
* that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. |
|
0 | 534 |
* |
5 | 535 |
* @since 2.5.0 |
536 |
* |
|
0 | 537 |
* @see has_filter() has_action() is an alias of has_filter(). |
538 |
* |
|
19 | 539 |
* @param string $hook_name The name of the action hook. |
540 |
* @param callable|string|array|false $callback Optional. The callback to check for. |
|
541 |
* This function can be called unconditionally to speculatively check |
|
542 |
* a callback that may or may not exist. Default false. |
|
18 | 543 |
* @return bool|int If `$callback` is omitted, returns boolean for whether the hook has |
544 |
* anything registered. When checking a specific function, the priority |
|
545 |
* of that hook is returned, or false if the function is not attached. |
|
0 | 546 |
*/ |
18 | 547 |
function has_action( $hook_name, $callback = false ) { |
548 |
return has_filter( $hook_name, $callback ); |
|
549 |
} |
|
550 |
||
551 |
/** |
|
552 |
* Removes a callback function from an action hook. |
|
553 |
* |
|
554 |
* This can be used to remove default functions attached to a specific action |
|
555 |
* hook and possibly replace them with a substitute. |
|
556 |
* |
|
557 |
* To remove a hook, the `$callback` and `$priority` arguments must match |
|
558 |
* when the hook was added. This goes for both filters and actions. No warning |
|
559 |
* will be given on removal failure. |
|
560 |
* |
|
561 |
* @since 1.2.0 |
|
562 |
* |
|
19 | 563 |
* @param string $hook_name The action hook to which the function to be removed is hooked. |
564 |
* @param callable|string|array $callback The name of the function which should be removed. |
|
565 |
* This function can be called unconditionally to speculatively remove |
|
566 |
* a callback that may or may not exist. |
|
567 |
* @param int $priority Optional. The exact priority used when adding the original |
|
568 |
* action callback. Default 10. |
|
18 | 569 |
* @return bool Whether the function is removed. |
570 |
*/ |
|
571 |
function remove_action( $hook_name, $callback, $priority = 10 ) { |
|
572 |
return remove_filter( $hook_name, $callback, $priority ); |
|
0 | 573 |
} |
574 |
||
575 |
/** |
|
18 | 576 |
* Removes all of the callback functions from an action hook. |
0 | 577 |
* |
18 | 578 |
* @since 2.7.0 |
0 | 579 |
* |
18 | 580 |
* @param string $hook_name The action to remove callbacks from. |
581 |
* @param int|false $priority Optional. The priority number to remove them from. |
|
582 |
* Default false. |
|
583 |
* @return true Always returns true. |
|
0 | 584 |
*/ |
18 | 585 |
function remove_all_actions( $hook_name, $priority = false ) { |
586 |
return remove_all_filters( $hook_name, $priority ); |
|
587 |
} |
|
588 |
||
589 |
/** |
|
590 |
* Retrieves the name of the current action hook. |
|
591 |
* |
|
592 |
* @since 3.9.0 |
|
593 |
* |
|
594 |
* @return string Hook name of the current action. |
|
595 |
*/ |
|
596 |
function current_action() { |
|
597 |
return current_filter(); |
|
0 | 598 |
} |
599 |
||
600 |
/** |
|
18 | 601 |
* Returns whether or not an action hook is currently being processed. |
602 |
* |
|
603 |
* @since 3.9.0 |
|
0 | 604 |
* |
18 | 605 |
* @param string|null $hook_name Optional. Action hook to check. Defaults to null, |
606 |
* which checks if any action is currently being run. |
|
607 |
* @return bool Whether the action is currently in the stack. |
|
608 |
*/ |
|
609 |
function doing_action( $hook_name = null ) { |
|
610 |
return doing_filter( $hook_name ); |
|
611 |
} |
|
612 |
||
613 |
/** |
|
614 |
* Retrieves the number of times an action has been fired during the current request. |
|
0 | 615 |
* |
18 | 616 |
* @since 2.1.0 |
617 |
* |
|
618 |
* @global int[] $wp_actions Stores the number of times each action was triggered. |
|
619 |
* |
|
620 |
* @param string $hook_name The name of the action hook. |
|
621 |
* @return int The number of times the action hook has been fired. |
|
0 | 622 |
*/ |
18 | 623 |
function did_action( $hook_name ) { |
624 |
global $wp_actions; |
|
625 |
||
626 |
if ( ! isset( $wp_actions[ $hook_name ] ) ) { |
|
627 |
return 0; |
|
628 |
} |
|
629 |
||
630 |
return $wp_actions[ $hook_name ]; |
|
0 | 631 |
} |
632 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
633 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
* Fires functions attached to a deprecated filter hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
635 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
636 |
* When a filter hook is deprecated, the apply_filters() call is replaced with |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
637 |
* apply_filters_deprecated(), which triggers a deprecation notice and then fires |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
* the original filter hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
* Note: the value and extra arguments passed to the original apply_filters() call |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
* must be passed here to `$args` as an array. For example: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
642 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
* // Old filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
* return apply_filters( 'wpdocs_filter', $value, $extra_arg ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
646 |
* // Deprecated. |
16 | 647 |
* return apply_filters_deprecated( 'wpdocs_filter', array( $value, $extra_arg ), '4.9.0', 'wpdocs_new_filter' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* @see _deprecated_hook() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
* |
18 | 653 |
* @param string $hook_name The name of the filter hook. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
* @param array $args Array of additional function arguments to be passed to apply_filters(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
* @param string $version The version of WordPress that deprecated the hook. |
16 | 656 |
* @param string $replacement Optional. The hook that should have been used. Default empty. |
657 |
* @param string $message Optional. A message regarding the change. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
*/ |
18 | 659 |
function apply_filters_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) { |
660 |
if ( ! has_filter( $hook_name ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
return $args[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
|
18 | 664 |
_deprecated_hook( $hook_name, $version, $replacement, $message ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
665 |
|
18 | 666 |
return apply_filters_ref_array( $hook_name, $args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
667 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
* Fires functions attached to a deprecated action hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
* When an action hook is deprecated, the do_action() call is replaced with |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
* do_action_deprecated(), which triggers a deprecation notice and then fires |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
674 |
* the original hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
677 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
678 |
* @see _deprecated_hook() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
* |
18 | 680 |
* @param string $hook_name The name of the action hook. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
* @param array $args Array of additional function arguments to be passed to do_action(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
682 |
* @param string $version The version of WordPress that deprecated the hook. |
16 | 683 |
* @param string $replacement Optional. The hook that should have been used. Default empty. |
684 |
* @param string $message Optional. A message regarding the change. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
685 |
*/ |
18 | 686 |
function do_action_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) { |
687 |
if ( ! has_action( $hook_name ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
688 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
689 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
|
18 | 691 |
_deprecated_hook( $hook_name, $version, $replacement, $message ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
692 |
|
18 | 693 |
do_action_ref_array( $hook_name, $args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
|
0 | 696 |
// |
697 |
// Functions for handling plugins. |
|
698 |
// |
|
699 |
||
700 |
/** |
|
701 |
* Gets the basename of a plugin. |
|
702 |
* |
|
703 |
* This method extracts the name of a plugin from its filename. |
|
704 |
* |
|
5 | 705 |
* @since 1.5.0 |
0 | 706 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
707 |
* @global array $wp_plugin_paths |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
708 |
* |
0 | 709 |
* @param string $file The filename of plugin. |
710 |
* @return string The name of a plugin. |
|
711 |
*/ |
|
5 | 712 |
function plugin_basename( $file ) { |
713 |
global $wp_plugin_paths; |
|
714 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
715 |
// $wp_plugin_paths contains normalized paths. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
$file = wp_normalize_path( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
717 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
718 |
arsort( $wp_plugin_paths ); |
18 | 719 |
|
5 | 720 |
foreach ( $wp_plugin_paths as $dir => $realdir ) { |
721 |
if ( strpos( $file, $realdir ) === 0 ) { |
|
722 |
$file = $dir . substr( $file, strlen( $realdir ) ); |
|
723 |
} |
|
724 |
} |
|
725 |
||
9 | 726 |
$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); |
5 | 727 |
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); |
728 |
||
16 | 729 |
// Get relative path from plugins directory. |
730 |
$file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file ); |
|
9 | 731 |
$file = trim( $file, '/' ); |
0 | 732 |
return $file; |
733 |
} |
|
734 |
||
735 |
/** |
|
5 | 736 |
* Register a plugin's real path. |
737 |
* |
|
738 |
* This is used in plugin_basename() to resolve symlinked paths. |
|
739 |
* |
|
740 |
* @since 3.9.0 |
|
741 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
742 |
* @see wp_normalize_path() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
744 |
* @global array $wp_plugin_paths |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
* |
5 | 746 |
* @param string $file Known path to the file. |
747 |
* @return bool Whether the path was able to be registered. |
|
748 |
*/ |
|
749 |
function wp_register_plugin_realpath( $file ) { |
|
750 |
global $wp_plugin_paths; |
|
751 |
||
16 | 752 |
// Normalize, but store as static to avoid recalculation of a constant value. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
753 |
static $wp_plugin_path = null, $wpmu_plugin_path = null; |
18 | 754 |
|
5 | 755 |
if ( ! isset( $wp_plugin_path ) ) { |
9 | 756 |
$wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR ); |
5 | 757 |
$wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR ); |
758 |
} |
|
759 |
||
9 | 760 |
$plugin_path = wp_normalize_path( dirname( $file ) ); |
5 | 761 |
$plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) ); |
762 |
||
763 |
if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) { |
|
764 |
return false; |
|
765 |
} |
|
766 |
||
767 |
if ( $plugin_path !== $plugin_realpath ) { |
|
768 |
$wp_plugin_paths[ $plugin_path ] = $plugin_realpath; |
|
769 |
} |
|
770 |
||
771 |
return true; |
|
772 |
} |
|
773 |
||
774 |
/** |
|
775 |
* Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in. |
|
776 |
* |
|
777 |
* @since 2.8.0 |
|
778 |
* |
|
779 |
* @param string $file The filename of the plugin (__FILE__). |
|
780 |
* @return string the filesystem path of the directory that contains the plugin. |
|
0 | 781 |
*/ |
782 |
function plugin_dir_path( $file ) { |
|
783 |
return trailingslashit( dirname( $file ) ); |
|
784 |
} |
|
785 |
||
786 |
/** |
|
5 | 787 |
* Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in. |
0 | 788 |
* |
5 | 789 |
* @since 2.8.0 |
790 |
* |
|
791 |
* @param string $file The filename of the plugin (__FILE__). |
|
792 |
* @return string the URL path of the directory that contains the plugin. |
|
0 | 793 |
*/ |
794 |
function plugin_dir_url( $file ) { |
|
795 |
return trailingslashit( plugins_url( '', $file ) ); |
|
796 |
} |
|
797 |
||
798 |
/** |
|
799 |
* Set the activation hook for a plugin. |
|
800 |
* |
|
801 |
* When a plugin is activated, the action 'activate_PLUGINNAME' hook is |
|
802 |
* called. In the name of this hook, PLUGINNAME is replaced with the name |
|
803 |
* of the plugin, including the optional subdirectory. For example, when the |
|
804 |
* plugin is located in wp-content/plugins/sampleplugin/sample.php, then |
|
805 |
* the name of this hook will become 'activate_sampleplugin/sample.php'. |
|
806 |
* |
|
807 |
* When the plugin consists of only one file and is (as by default) located at |
|
808 |
* wp-content/plugins/sample.php the name of this hook will be |
|
809 |
* 'activate_sample.php'. |
|
810 |
* |
|
5 | 811 |
* @since 2.0.0 |
0 | 812 |
* |
5 | 813 |
* @param string $file The filename of the plugin including the path. |
18 | 814 |
* @param callable $callback The function hooked to the 'activate_PLUGIN' action. |
0 | 815 |
*/ |
18 | 816 |
function register_activation_hook( $file, $callback ) { |
9 | 817 |
$file = plugin_basename( $file ); |
18 | 818 |
add_action( 'activate_' . $file, $callback ); |
0 | 819 |
} |
820 |
||
821 |
/** |
|
18 | 822 |
* Sets the deactivation hook for a plugin. |
0 | 823 |
* |
824 |
* When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is |
|
825 |
* called. In the name of this hook, PLUGINNAME is replaced with the name |
|
826 |
* of the plugin, including the optional subdirectory. For example, when the |
|
827 |
* plugin is located in wp-content/plugins/sampleplugin/sample.php, then |
|
828 |
* the name of this hook will become 'deactivate_sampleplugin/sample.php'. |
|
829 |
* |
|
830 |
* When the plugin consists of only one file and is (as by default) located at |
|
831 |
* wp-content/plugins/sample.php the name of this hook will be |
|
832 |
* 'deactivate_sample.php'. |
|
833 |
* |
|
5 | 834 |
* @since 2.0.0 |
0 | 835 |
* |
5 | 836 |
* @param string $file The filename of the plugin including the path. |
18 | 837 |
* @param callable $callback The function hooked to the 'deactivate_PLUGIN' action. |
0 | 838 |
*/ |
18 | 839 |
function register_deactivation_hook( $file, $callback ) { |
9 | 840 |
$file = plugin_basename( $file ); |
18 | 841 |
add_action( 'deactivate_' . $file, $callback ); |
0 | 842 |
} |
843 |
||
844 |
/** |
|
18 | 845 |
* Sets the uninstallation hook for a plugin. |
0 | 846 |
* |
847 |
* Registers the uninstall hook that will be called when the user clicks on the |
|
848 |
* uninstall link that calls for the plugin to uninstall itself. The link won't |
|
849 |
* be active unless the plugin hooks into the action. |
|
850 |
* |
|
851 |
* The plugin should not run arbitrary code outside of functions, when |
|
852 |
* registering the uninstall hook. In order to run using the hook, the plugin |
|
853 |
* will have to be included, which means that any code laying outside of a |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
854 |
* function will be run during the uninstallation process. The plugin should not |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
855 |
* hinder the uninstallation process. |
0 | 856 |
* |
857 |
* If the plugin can not be written without running code within the plugin, then |
|
858 |
* the plugin should create a file named 'uninstall.php' in the base plugin |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
* folder. This file will be called, if it exists, during the uninstallation process |
0 | 860 |
* bypassing the uninstall hook. The plugin, when using the 'uninstall.php' |
861 |
* should always check for the 'WP_UNINSTALL_PLUGIN' constant, before |
|
862 |
* executing. |
|
863 |
* |
|
5 | 864 |
* @since 2.7.0 |
0 | 865 |
* |
5 | 866 |
* @param string $file Plugin file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
* @param callable $callback The callback to run when the hook is called. Must be |
5 | 868 |
* a static method or function. |
0 | 869 |
*/ |
870 |
function register_uninstall_hook( $file, $callback ) { |
|
871 |
if ( is_array( $callback ) && is_object( $callback[0] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
_doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' ); |
0 | 873 |
return; |
874 |
} |
|
875 |
||
5 | 876 |
/* |
877 |
* The option should not be autoloaded, because it is not needed in most |
|
878 |
* cases. Emphasis should be put on using the 'uninstall.php' way of |
|
879 |
* uninstalling the plugin. |
|
880 |
*/ |
|
16 | 881 |
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); |
882 |
$plugin_basename = plugin_basename( $file ); |
|
18 | 883 |
|
16 | 884 |
if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) { |
885 |
$uninstallable_plugins[ $plugin_basename ] = $callback; |
|
886 |
update_option( 'uninstall_plugins', $uninstallable_plugins ); |
|
887 |
} |
|
0 | 888 |
} |
889 |
||
890 |
/** |
|
18 | 891 |
* Calls the 'all' hook, which will process the functions hooked into it. |
0 | 892 |
* |
893 |
* The 'all' hook passes all of the arguments or parameters that were used for |
|
894 |
* the hook, which this function was called for. |
|
895 |
* |
|
896 |
* This function is used internally for apply_filters(), do_action(), and |
|
897 |
* do_action_ref_array() and is not meant to be used from outside those |
|
898 |
* functions. This function does not check for the existence of the all hook, so |
|
899 |
* it will fail unless the all hook exists prior to this function call. |
|
900 |
* |
|
5 | 901 |
* @since 2.5.0 |
0 | 902 |
* @access private |
903 |
* |
|
18 | 904 |
* @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
905 |
* |
0 | 906 |
* @param array $args The collected parameters from the hook that was called. |
907 |
*/ |
|
9 | 908 |
function _wp_call_all_hook( $args ) { |
0 | 909 |
global $wp_filter; |
910 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
911 |
$wp_filter['all']->do_all_hook( $args ); |
0 | 912 |
} |
913 |
||
914 |
/** |
|
18 | 915 |
* Builds Unique ID for storage and retrieval. |
0 | 916 |
* |
917 |
* The old way to serialize the callback caused issues and this function is the |
|
5 | 918 |
* solution. It works by checking for objects and creating a new property in |
0 | 919 |
* the class to keep track of the object and new objects of the same class that |
920 |
* need to be added. |
|
921 |
* |
|
922 |
* It also allows for the removal of actions and filters for objects after they |
|
923 |
* change class properties. It is possible to include the property $wp_filter_id |
|
924 |
* in your class and set it to "null" or a number to bypass the workaround. |
|
925 |
* However this will prevent you from adding new classes and any new classes |
|
926 |
* will overwrite the previous hook by the same class. |
|
927 |
* |
|
928 |
* Functions and static method callbacks are just returned as strings and |
|
929 |
* shouldn't have any speed penalty. |
|
930 |
* |
|
5 | 931 |
* @link https://core.trac.wordpress.org/ticket/3875 |
932 |
* |
|
933 |
* @since 2.2.3 |
|
16 | 934 |
* @since 5.3.0 Removed workarounds for spl_object_hash(). |
18 | 935 |
* `$hook_name` and `$priority` are no longer used, |
16 | 936 |
* and the function always returns a string. |
19 | 937 |
* |
0 | 938 |
* @access private |
5 | 939 |
* |
19 | 940 |
* @param string $hook_name Unused. The name of the filter to build ID for. |
941 |
* @param callable|string|array $callback The callback to generate ID for. The callback may |
|
942 |
* or may not exist. |
|
943 |
* @param int $priority Unused. The order in which the functions |
|
944 |
* associated with a particular action are executed. |
|
16 | 945 |
* @return string Unique function ID for usage as array key. |
0 | 946 |
*/ |
18 | 947 |
function _wp_filter_build_unique_id( $hook_name, $callback, $priority ) { |
948 |
if ( is_string( $callback ) ) { |
|
949 |
return $callback; |
|
9 | 950 |
} |
0 | 951 |
|
18 | 952 |
if ( is_object( $callback ) ) { |
16 | 953 |
// Closures are currently implemented as objects. |
18 | 954 |
$callback = array( $callback, '' ); |
0 | 955 |
} else { |
18 | 956 |
$callback = (array) $callback; |
0 | 957 |
} |
958 |
||
18 | 959 |
if ( is_object( $callback[0] ) ) { |
16 | 960 |
// Object class calling. |
18 | 961 |
return spl_object_hash( $callback[0] ) . $callback[1]; |
962 |
} elseif ( is_string( $callback[0] ) ) { |
|
16 | 963 |
// Static calling. |
18 | 964 |
return $callback[0] . '::' . $callback[1]; |
0 | 965 |
} |
966 |
} |