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