91 * It is up to you to take care. This is done for optimization purposes, so |
97 * It is up to you to take care. This is done for optimization purposes, so |
92 * everything is as quick as possible. |
98 * everything is as quick as possible. |
93 * |
99 * |
94 * @since 0.71 |
100 * @since 0.71 |
95 * |
101 * |
96 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. |
102 * @global WP_Hook[] $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. |
97 * |
103 * |
98 * @param string $tag The name of the filter to hook the $function_to_add callback to. |
104 * @param string $hook_name The name of the filter to add the callback to. |
99 * @param callable $function_to_add The callback to be run when the filter is applied. |
105 * @param callable $callback The callback to be run when the filter is applied. |
100 * @param int $priority Optional. Used to specify the order in which the functions |
106 * @param int $priority Optional. Used to specify the order in which the functions |
101 * associated with a particular action are executed. |
107 * associated with a particular filter are executed. |
102 * Lower numbers correspond with earlier execution, |
108 * Lower numbers correspond with earlier execution, |
103 * and functions with the same priority are executed |
109 * and functions with the same priority are executed |
104 * in the order in which they were added to the action. Default 10. |
110 * in the order in which they were added to the filter. Default 10. |
105 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
111 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
106 * @return true |
112 * @return true Always returns true. |
107 */ |
113 */ |
108 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { |
114 function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { |
109 global $wp_filter; |
115 global $wp_filter; |
110 if ( ! isset( $wp_filter[ $tag ] ) ) { |
116 |
111 $wp_filter[ $tag ] = new WP_Hook(); |
117 if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
112 } |
118 $wp_filter[ $hook_name ] = new WP_Hook(); |
113 $wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args ); |
119 } |
|
120 |
|
121 $wp_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args ); |
|
122 |
114 return true; |
123 return true; |
115 } |
124 } |
116 |
125 |
117 /** |
126 /** |
118 * Check if any filter has been registered for a hook. |
|
119 * |
|
120 * @since 2.5.0 |
|
121 * |
|
122 * @global array $wp_filter Stores all of the filters and actions. |
|
123 * |
|
124 * @param string $tag The name of the filter hook. |
|
125 * @param callable|bool $function_to_check Optional. The callback to check for. Default false. |
|
126 * @return false|int If $function_to_check is omitted, returns boolean for whether the hook has |
|
127 * anything registered. When checking a specific function, the priority of that |
|
128 * hook is returned, or false if the function is not attached. When using the |
|
129 * $function_to_check argument, this function may return a non-boolean value |
|
130 * that evaluates to false (e.g.) 0, so use the === operator for testing the |
|
131 * return value. |
|
132 */ |
|
133 function has_filter( $tag, $function_to_check = false ) { |
|
134 global $wp_filter; |
|
135 |
|
136 if ( ! isset( $wp_filter[ $tag ] ) ) { |
|
137 return false; |
|
138 } |
|
139 |
|
140 return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check ); |
|
141 } |
|
142 |
|
143 /** |
|
144 * Calls the callback functions that have been added to a filter hook. |
127 * Calls the callback functions that have been added to a filter hook. |
145 * |
128 * |
146 * The callback functions attached to the filter hook are invoked by calling |
129 * This function invokes all functions attached to filter hook `$hook_name`. |
147 * this function. This function can be used to create a new filter hook by |
130 * It is possible to create new filter hooks by simply calling this function, |
148 * simply calling this function with the name of the new hook specified using |
131 * specifying the name of the new hook using the `$hook_name` parameter. |
149 * the `$tag` parameter. |
|
150 * |
132 * |
151 * The function also allows for multiple additional arguments to be passed to hooks. |
133 * The function also allows for multiple additional arguments to be passed to hooks. |
152 * |
134 * |
153 * Example usage: |
135 * Example usage: |
154 * |
136 * |
168 * * - $arg1 and $arg2 are the additional arguments passed to the callback. |
150 * * - $arg1 and $arg2 are the additional arguments passed to the callback. |
169 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); |
151 * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 ); |
170 * |
152 * |
171 * @since 0.71 |
153 * @since 0.71 |
172 * |
154 * |
173 * @global array $wp_filter Stores all of the filters and actions. |
155 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
174 * @global array $wp_current_filter Stores the list of current filters with the current one last. |
156 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
175 * |
157 * |
176 * @param string $tag The name of the filter hook. |
158 * @param string $hook_name The name of the filter hook. |
177 * @param mixed $value The value to filter. |
159 * @param mixed $value The value to filter. |
178 * @param mixed ...$args Additional parameters to pass to the callback functions. |
160 * @param mixed ...$args Additional parameters to pass to the callback functions. |
179 * @return mixed The filtered value after all hooked functions are applied to it. |
161 * @return mixed The filtered value after all hooked functions are applied to it. |
180 */ |
162 */ |
181 function apply_filters( $tag, $value ) { |
163 function apply_filters( $hook_name, $value ) { |
182 global $wp_filter, $wp_current_filter; |
164 global $wp_filter, $wp_current_filter; |
183 |
165 |
184 $args = func_get_args(); |
166 $args = func_get_args(); |
185 |
167 |
186 // Do 'all' actions first. |
168 // Do 'all' actions first. |
187 if ( isset( $wp_filter['all'] ) ) { |
169 if ( isset( $wp_filter['all'] ) ) { |
188 $wp_current_filter[] = $tag; |
170 $wp_current_filter[] = $hook_name; |
189 _wp_call_all_hook( $args ); |
171 _wp_call_all_hook( $args ); |
190 } |
172 } |
191 |
173 |
192 if ( ! isset( $wp_filter[ $tag ] ) ) { |
174 if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
193 if ( isset( $wp_filter['all'] ) ) { |
175 if ( isset( $wp_filter['all'] ) ) { |
194 array_pop( $wp_current_filter ); |
176 array_pop( $wp_current_filter ); |
195 } |
177 } |
|
178 |
196 return $value; |
179 return $value; |
197 } |
180 } |
198 |
181 |
199 if ( ! isset( $wp_filter['all'] ) ) { |
182 if ( ! isset( $wp_filter['all'] ) ) { |
200 $wp_current_filter[] = $tag; |
183 $wp_current_filter[] = $hook_name; |
201 } |
184 } |
202 |
185 |
203 // Don't pass the tag name to WP_Hook. |
186 // Don't pass the tag name to WP_Hook. |
204 array_shift( $args ); |
187 array_shift( $args ); |
205 |
188 |
206 $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args ); |
189 $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args ); |
207 |
190 |
208 array_pop( $wp_current_filter ); |
191 array_pop( $wp_current_filter ); |
209 |
192 |
210 return $filtered; |
193 return $filtered; |
211 } |
194 } |
214 * Calls the callback functions that have been added to a filter hook, specifying arguments in an array. |
197 * Calls the callback functions that have been added to a filter hook, specifying arguments in an array. |
215 * |
198 * |
216 * @since 3.0.0 |
199 * @since 3.0.0 |
217 * |
200 * |
218 * @see apply_filters() This function is identical, but the arguments passed to the |
201 * @see apply_filters() This function is identical, but the arguments passed to the |
219 * functions hooked to `$tag` are supplied using an array. |
202 * functions hooked to `$hook_name` are supplied using an array. |
220 * |
203 * |
221 * @global array $wp_filter Stores all of the filters and actions. |
204 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
222 * @global array $wp_current_filter Stores the list of current filters with the current one last. |
205 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
223 * |
206 * |
224 * @param string $tag The name of the filter hook. |
207 * @param string $hook_name The name of the filter hook. |
225 * @param array $args The arguments supplied to the functions hooked to $tag. |
208 * @param array $args The arguments supplied to the functions hooked to `$hook_name`. |
226 * @return mixed The filtered value after all hooked functions are applied to it. |
209 * @return mixed The filtered value after all hooked functions are applied to it. |
227 */ |
210 */ |
228 function apply_filters_ref_array( $tag, $args ) { |
211 function apply_filters_ref_array( $hook_name, $args ) { |
229 global $wp_filter, $wp_current_filter; |
212 global $wp_filter, $wp_current_filter; |
230 |
213 |
231 // Do 'all' actions first. |
214 // Do 'all' actions first. |
232 if ( isset( $wp_filter['all'] ) ) { |
215 if ( isset( $wp_filter['all'] ) ) { |
233 $wp_current_filter[] = $tag; |
216 $wp_current_filter[] = $hook_name; |
234 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
217 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
235 _wp_call_all_hook( $all_args ); |
218 _wp_call_all_hook( $all_args ); |
236 } |
219 } |
237 |
220 |
238 if ( ! isset( $wp_filter[ $tag ] ) ) { |
221 if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
239 if ( isset( $wp_filter['all'] ) ) { |
222 if ( isset( $wp_filter['all'] ) ) { |
240 array_pop( $wp_current_filter ); |
223 array_pop( $wp_current_filter ); |
241 } |
224 } |
|
225 |
242 return $args[0]; |
226 return $args[0]; |
243 } |
227 } |
244 |
228 |
245 if ( ! isset( $wp_filter['all'] ) ) { |
229 if ( ! isset( $wp_filter['all'] ) ) { |
246 $wp_current_filter[] = $tag; |
230 $wp_current_filter[] = $hook_name; |
247 } |
231 } |
248 |
232 |
249 $filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args ); |
233 $filtered = $wp_filter[ $hook_name ]->apply_filters( $args[0], $args ); |
250 |
234 |
251 array_pop( $wp_current_filter ); |
235 array_pop( $wp_current_filter ); |
252 |
236 |
253 return $filtered; |
237 return $filtered; |
254 } |
238 } |
255 |
239 |
256 /** |
240 /** |
257 * Removes a function from a specified filter hook. |
241 * Checks if any filter has been registered for a hook. |
258 * |
242 * |
259 * This function removes a function attached to a specified filter hook. This |
243 * When using the `$callback` argument, this function may return a non-boolean value |
260 * method can be used to remove default functions attached to a specific filter |
244 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. |
|
245 * |
|
246 * @since 2.5.0 |
|
247 * |
|
248 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
|
249 * |
|
250 * @param string $hook_name The name of the filter hook. |
|
251 * @param callable|false $callback Optional. The callback to check for. Default false. |
|
252 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has |
|
253 * anything registered. When checking a specific function, the priority |
|
254 * of that hook is returned, or false if the function is not attached. |
|
255 */ |
|
256 function has_filter( $hook_name, $callback = false ) { |
|
257 global $wp_filter; |
|
258 |
|
259 if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
|
260 return false; |
|
261 } |
|
262 |
|
263 return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback ); |
|
264 } |
|
265 |
|
266 /** |
|
267 * Removes a callback function from a filter hook. |
|
268 * |
|
269 * This can be used to remove default functions attached to a specific filter |
261 * hook and possibly replace them with a substitute. |
270 * hook and possibly replace them with a substitute. |
262 * |
271 * |
263 * To remove a hook, the $function_to_remove and $priority arguments must match |
272 * To remove a hook, the `$callback` and `$priority` arguments must match |
264 * when the hook was added. This goes for both filters and actions. No warning |
273 * when the hook was added. This goes for both filters and actions. No warning |
265 * will be given on removal failure. |
274 * will be given on removal failure. |
266 * |
275 * |
267 * @since 1.2.0 |
276 * @since 1.2.0 |
268 * |
277 * |
269 * @global array $wp_filter Stores all of the filters and actions. |
278 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
270 * |
279 * |
271 * @param string $tag The filter hook to which the function to be removed is hooked. |
280 * @param string $hook_name The filter hook to which the function to be removed is hooked. |
272 * @param callable $function_to_remove The name of the function which should be removed. |
281 * @param callable $callback The name of the function which should be removed. |
273 * @param int $priority Optional. The priority of the function. Default 10. |
282 * @param int $priority Optional. The exact priority used when adding the original |
274 * @return bool Whether the function existed before it was removed. |
283 * filter callback. Default 10. |
275 */ |
284 * @return bool Whether the function existed before it was removed. |
276 function remove_filter( $tag, $function_to_remove, $priority = 10 ) { |
285 */ |
|
286 function remove_filter( $hook_name, $callback, $priority = 10 ) { |
277 global $wp_filter; |
287 global $wp_filter; |
278 |
288 |
279 $r = false; |
289 $r = false; |
280 if ( isset( $wp_filter[ $tag ] ) ) { |
290 |
281 $r = $wp_filter[ $tag ]->remove_filter( $tag, $function_to_remove, $priority ); |
291 if ( isset( $wp_filter[ $hook_name ] ) ) { |
282 if ( ! $wp_filter[ $tag ]->callbacks ) { |
292 $r = $wp_filter[ $hook_name ]->remove_filter( $hook_name, $callback, $priority ); |
283 unset( $wp_filter[ $tag ] ); |
293 |
|
294 if ( ! $wp_filter[ $hook_name ]->callbacks ) { |
|
295 unset( $wp_filter[ $hook_name ] ); |
284 } |
296 } |
285 } |
297 } |
286 |
298 |
287 return $r; |
299 return $r; |
288 } |
300 } |
289 |
301 |
290 /** |
302 /** |
291 * Remove all of the hooks from a filter. |
303 * Removes all of the callback functions from a filter hook. |
292 * |
304 * |
293 * @since 2.7.0 |
305 * @since 2.7.0 |
294 * |
306 * |
295 * @global array $wp_filter Stores all of the filters and actions. |
307 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
296 * |
308 * |
297 * @param string $tag The filter to remove hooks from. |
309 * @param string $hook_name The filter to remove callbacks from. |
298 * @param int|bool $priority Optional. The priority number to remove. Default false. |
310 * @param int|false $priority Optional. The priority number to remove them from. |
299 * @return true True when finished. |
311 * Default false. |
300 */ |
312 * @return true Always returns true. |
301 function remove_all_filters( $tag, $priority = false ) { |
313 */ |
|
314 function remove_all_filters( $hook_name, $priority = false ) { |
302 global $wp_filter; |
315 global $wp_filter; |
303 |
316 |
304 if ( isset( $wp_filter[ $tag ] ) ) { |
317 if ( isset( $wp_filter[ $hook_name ] ) ) { |
305 $wp_filter[ $tag ]->remove_all_filters( $priority ); |
318 $wp_filter[ $hook_name ]->remove_all_filters( $priority ); |
306 if ( ! $wp_filter[ $tag ]->has_filters() ) { |
319 |
307 unset( $wp_filter[ $tag ] ); |
320 if ( ! $wp_filter[ $hook_name ]->has_filters() ) { |
|
321 unset( $wp_filter[ $hook_name ] ); |
308 } |
322 } |
309 } |
323 } |
310 |
324 |
311 return true; |
325 return true; |
312 } |
326 } |
313 |
327 |
314 /** |
328 /** |
315 * Retrieve the name of the current filter or action. |
329 * Retrieves the name of the current filter hook. |
316 * |
330 * |
317 * @since 2.5.0 |
331 * @since 2.5.0 |
318 * |
332 * |
319 * @global array $wp_current_filter Stores the list of current filters with the current one last |
333 * @global string[] $wp_current_filter Stores the list of current filters with the current one last |
320 * |
334 * |
321 * @return string Hook name of the current filter or action. |
335 * @return string Hook name of the current filter. |
322 */ |
336 */ |
323 function current_filter() { |
337 function current_filter() { |
324 global $wp_current_filter; |
338 global $wp_current_filter; |
|
339 |
325 return end( $wp_current_filter ); |
340 return end( $wp_current_filter ); |
326 } |
341 } |
327 |
342 |
328 /** |
343 /** |
329 * Retrieve the name of the current action. |
344 * Returns whether or not a filter hook is currently being processed. |
330 * |
|
331 * @since 3.9.0 |
|
332 * |
|
333 * @return string Hook name of the current action. |
|
334 */ |
|
335 function current_action() { |
|
336 return current_filter(); |
|
337 } |
|
338 |
|
339 /** |
|
340 * Retrieve the name of a filter currently being processed. |
|
341 * |
345 * |
342 * The function current_filter() only returns the most recent filter or action |
346 * The function current_filter() only returns the most recent filter or action |
343 * being executed. did_action() returns true once the action is initially |
347 * being executed. did_action() returns true once the action is initially |
344 * processed. |
348 * processed. |
345 * |
349 * |
346 * This function allows detection for any filter currently being |
350 * This function allows detection for any filter currently being executed |
347 * executed (despite not being the most recent filter to fire, in the case of |
351 * (regardless of whether it's the most recent filter to fire, in the case of |
348 * hooks called from hook callbacks) to be verified. |
352 * hooks called from hook callbacks) to be verified. |
349 * |
353 * |
350 * @since 3.9.0 |
354 * @since 3.9.0 |
351 * |
355 * |
352 * @see current_filter() |
356 * @see current_filter() |
353 * @see did_action() |
357 * @see did_action() |
354 * @global array $wp_current_filter Current filter. |
358 * @global string[] $wp_current_filter Current filter. |
355 * |
359 * |
356 * @param null|string $filter Optional. Filter to check. Defaults to null, which |
360 * @param null|string $hook_name Optional. Filter hook to check. Defaults to null, |
357 * checks if any filter is currently being run. |
361 * which checks if any filter is currently being run. |
358 * @return bool Whether the filter is currently in the stack. |
362 * @return bool Whether the filter is currently in the stack. |
359 */ |
363 */ |
360 function doing_filter( $filter = null ) { |
364 function doing_filter( $hook_name = null ) { |
361 global $wp_current_filter; |
365 global $wp_current_filter; |
362 |
366 |
363 if ( null === $filter ) { |
367 if ( null === $hook_name ) { |
364 return ! empty( $wp_current_filter ); |
368 return ! empty( $wp_current_filter ); |
365 } |
369 } |
366 |
370 |
367 return in_array( $filter, $wp_current_filter, true ); |
371 return in_array( $hook_name, $wp_current_filter, true ); |
368 } |
372 } |
369 |
373 |
370 /** |
374 /** |
371 * Retrieve the name of an action currently being processed. |
375 * Adds a callback function to an action hook. |
372 * |
|
373 * @since 3.9.0 |
|
374 * |
|
375 * @param string|null $action Optional. Action to check. Defaults to null, which checks |
|
376 * if any action is currently being run. |
|
377 * @return bool Whether the action is currently in the stack. |
|
378 */ |
|
379 function doing_action( $action = null ) { |
|
380 return doing_filter( $action ); |
|
381 } |
|
382 |
|
383 /** |
|
384 * Hooks a function on to a specific action. |
|
385 * |
376 * |
386 * Actions are the hooks that the WordPress core launches at specific points |
377 * Actions are the hooks that the WordPress core launches at specific points |
387 * during execution, or when specific events occur. Plugins can specify that |
378 * during execution, or when specific events occur. Plugins can specify that |
388 * one or more of its PHP functions are executed at these points, using the |
379 * one or more of its PHP functions are executed at these points, using the |
389 * Action API. |
380 * Action API. |
390 * |
381 * |
391 * @since 1.2.0 |
382 * @since 1.2.0 |
392 * |
383 * |
393 * @param string $tag The name of the action to which the $function_to_add is hooked. |
384 * @param string $hook_name The name of the action to add the callback to. |
394 * @param callable $function_to_add The name of the function you wish to be called. |
385 * @param callable $callback The callback to be run when the action is called. |
395 * @param int $priority Optional. Used to specify the order in which the functions |
386 * @param int $priority Optional. Used to specify the order in which the functions |
396 * associated with a particular action are executed. Default 10. |
387 * associated with a particular action are executed. |
397 * Lower numbers correspond with earlier execution, |
388 * Lower numbers correspond with earlier execution, |
398 * and functions with the same priority are executed |
389 * and functions with the same priority are executed |
399 * in the order in which they were added to the action. |
390 * in the order in which they were added to the action. Default 10. |
400 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
391 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. |
401 * @return true Will always return true. |
392 * @return true Always returns true. |
402 */ |
393 */ |
403 function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { |
394 function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { |
404 return add_filter( $tag, $function_to_add, $priority, $accepted_args ); |
395 return add_filter( $hook_name, $callback, $priority, $accepted_args ); |
405 } |
396 } |
406 |
397 |
407 /** |
398 /** |
408 * Execute functions hooked on a specific action hook. |
399 * Calls the callback functions that have been added to an action hook. |
409 * |
400 * |
410 * This function invokes all functions attached to action hook `$tag`. It is |
401 * This function invokes all functions attached to action hook `$hook_name`. |
411 * possible to create new action hooks by simply calling this function, |
402 * It is possible to create new action hooks by simply calling this function, |
412 * specifying the name of the new hook using the `$tag` parameter. |
403 * specifying the name of the new hook using the `$hook_name` parameter. |
413 * |
404 * |
414 * You can pass extra arguments to the hooks, much like you can with `apply_filters()`. |
405 * You can pass extra arguments to the hooks, much like you can with `apply_filters()`. |
415 * |
406 * |
416 * Example usage: |
407 * Example usage: |
417 * |
408 * |
431 * |
422 * |
432 * @since 1.2.0 |
423 * @since 1.2.0 |
433 * @since 5.3.0 Formalized the existing and already documented `...$arg` parameter |
424 * @since 5.3.0 Formalized the existing and already documented `...$arg` parameter |
434 * by adding it to the function signature. |
425 * by adding it to the function signature. |
435 * |
426 * |
436 * @global array $wp_filter Stores all of the filters and actions. |
427 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
437 * @global array $wp_actions Increments the amount of times action was triggered. |
428 * @global int[] $wp_actions Stores the number of times each action was triggered. |
438 * @global array $wp_current_filter Stores the list of current filters with the current one last. |
429 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
439 * |
430 * |
440 * @param string $tag The name of the action to be executed. |
431 * @param string $hook_name The name of the action to be executed. |
441 * @param mixed ...$arg Optional. Additional arguments which are passed on to the |
432 * @param mixed ...$arg Optional. Additional arguments which are passed on to the |
442 * functions hooked to the action. Default empty. |
433 * functions hooked to the action. Default empty. |
443 */ |
434 */ |
444 function do_action( $tag, ...$arg ) { |
435 function do_action( $hook_name, ...$arg ) { |
445 global $wp_filter, $wp_actions, $wp_current_filter; |
436 global $wp_filter, $wp_actions, $wp_current_filter; |
446 |
437 |
447 if ( ! isset( $wp_actions[ $tag ] ) ) { |
438 if ( ! isset( $wp_actions[ $hook_name ] ) ) { |
448 $wp_actions[ $tag ] = 1; |
439 $wp_actions[ $hook_name ] = 1; |
449 } else { |
440 } else { |
450 ++$wp_actions[ $tag ]; |
441 ++$wp_actions[ $hook_name ]; |
451 } |
442 } |
452 |
443 |
453 // Do 'all' actions first. |
444 // Do 'all' actions first. |
454 if ( isset( $wp_filter['all'] ) ) { |
445 if ( isset( $wp_filter['all'] ) ) { |
455 $wp_current_filter[] = $tag; |
446 $wp_current_filter[] = $hook_name; |
456 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
447 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
457 _wp_call_all_hook( $all_args ); |
448 _wp_call_all_hook( $all_args ); |
458 } |
449 } |
459 |
450 |
460 if ( ! isset( $wp_filter[ $tag ] ) ) { |
451 if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
461 if ( isset( $wp_filter['all'] ) ) { |
452 if ( isset( $wp_filter['all'] ) ) { |
462 array_pop( $wp_current_filter ); |
453 array_pop( $wp_current_filter ); |
463 } |
454 } |
|
455 |
464 return; |
456 return; |
465 } |
457 } |
466 |
458 |
467 if ( ! isset( $wp_filter['all'] ) ) { |
459 if ( ! isset( $wp_filter['all'] ) ) { |
468 $wp_current_filter[] = $tag; |
460 $wp_current_filter[] = $hook_name; |
469 } |
461 } |
470 |
462 |
471 if ( empty( $arg ) ) { |
463 if ( empty( $arg ) ) { |
472 $arg[] = ''; |
464 $arg[] = ''; |
473 } elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) { |
465 } elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) { |
474 // Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`. |
466 // Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`. |
475 $arg[0] = $arg[0][0]; |
467 $arg[0] = $arg[0][0]; |
476 } |
468 } |
477 |
469 |
478 $wp_filter[ $tag ]->do_action( $arg ); |
470 $wp_filter[ $hook_name ]->do_action( $arg ); |
479 |
471 |
480 array_pop( $wp_current_filter ); |
472 array_pop( $wp_current_filter ); |
481 } |
473 } |
482 |
474 |
483 /** |
475 /** |
484 * Retrieve the number of times an action is fired. |
476 * Calls the callback functions that have been added to an action hook, specifying arguments in an array. |
485 * |
477 * |
486 * @since 2.1.0 |
478 * @since 2.1.0 |
487 * |
479 * |
488 * @global array $wp_actions Increments the amount of times action was triggered. |
|
489 * |
|
490 * @param string $tag The name of the action hook. |
|
491 * @return int The number of times action hook $tag is fired. |
|
492 */ |
|
493 function did_action( $tag ) { |
|
494 global $wp_actions; |
|
495 |
|
496 if ( ! isset( $wp_actions[ $tag ] ) ) { |
|
497 return 0; |
|
498 } |
|
499 |
|
500 return $wp_actions[ $tag ]; |
|
501 } |
|
502 |
|
503 /** |
|
504 * Calls the callback functions that have been added to an action hook, specifying arguments in an array. |
|
505 * |
|
506 * @since 2.1.0 |
|
507 * |
|
508 * @see do_action() This function is identical, but the arguments passed to the |
480 * @see do_action() This function is identical, but the arguments passed to the |
509 * functions hooked to `$tag` are supplied using an array. |
481 * functions hooked to `$hook_name` are supplied using an array. |
510 * @global array $wp_filter Stores all of the filters and actions. |
482 * |
511 * @global array $wp_actions Increments the amount of times action was triggered. |
483 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
512 * @global array $wp_current_filter Stores the list of current filters with the current one last. |
484 * @global int[] $wp_actions Stores the number of times each action was triggered. |
513 * |
485 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
514 * @param string $tag The name of the action to be executed. |
486 * |
515 * @param array $args The arguments supplied to the functions hooked to `$tag`. |
487 * @param string $hook_name The name of the action to be executed. |
516 */ |
488 * @param array $args The arguments supplied to the functions hooked to `$hook_name`. |
517 function do_action_ref_array( $tag, $args ) { |
489 */ |
|
490 function do_action_ref_array( $hook_name, $args ) { |
518 global $wp_filter, $wp_actions, $wp_current_filter; |
491 global $wp_filter, $wp_actions, $wp_current_filter; |
519 |
492 |
520 if ( ! isset( $wp_actions[ $tag ] ) ) { |
493 if ( ! isset( $wp_actions[ $hook_name ] ) ) { |
521 $wp_actions[ $tag ] = 1; |
494 $wp_actions[ $hook_name ] = 1; |
522 } else { |
495 } else { |
523 ++$wp_actions[ $tag ]; |
496 ++$wp_actions[ $hook_name ]; |
524 } |
497 } |
525 |
498 |
526 // Do 'all' actions first. |
499 // Do 'all' actions first. |
527 if ( isset( $wp_filter['all'] ) ) { |
500 if ( isset( $wp_filter['all'] ) ) { |
528 $wp_current_filter[] = $tag; |
501 $wp_current_filter[] = $hook_name; |
529 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
502 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
530 _wp_call_all_hook( $all_args ); |
503 _wp_call_all_hook( $all_args ); |
531 } |
504 } |
532 |
505 |
533 if ( ! isset( $wp_filter[ $tag ] ) ) { |
506 if ( ! isset( $wp_filter[ $hook_name ] ) ) { |
534 if ( isset( $wp_filter['all'] ) ) { |
507 if ( isset( $wp_filter['all'] ) ) { |
535 array_pop( $wp_current_filter ); |
508 array_pop( $wp_current_filter ); |
536 } |
509 } |
|
510 |
537 return; |
511 return; |
538 } |
512 } |
539 |
513 |
540 if ( ! isset( $wp_filter['all'] ) ) { |
514 if ( ! isset( $wp_filter['all'] ) ) { |
541 $wp_current_filter[] = $tag; |
515 $wp_current_filter[] = $hook_name; |
542 } |
516 } |
543 |
517 |
544 $wp_filter[ $tag ]->do_action( $args ); |
518 $wp_filter[ $hook_name ]->do_action( $args ); |
545 |
519 |
546 array_pop( $wp_current_filter ); |
520 array_pop( $wp_current_filter ); |
547 } |
521 } |
548 |
522 |
549 /** |
523 /** |
550 * Check if any action has been registered for a hook. |
524 * Checks if any action has been registered for a hook. |
|
525 * |
|
526 * When using the `$callback` argument, this function may return a non-boolean value |
|
527 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. |
551 * |
528 * |
552 * @since 2.5.0 |
529 * @since 2.5.0 |
553 * |
530 * |
554 * @see has_filter() has_action() is an alias of has_filter(). |
531 * @see has_filter() has_action() is an alias of has_filter(). |
555 * |
532 * |
556 * @param string $tag The name of the action hook. |
533 * @param string $hook_name The name of the action hook. |
557 * @param callable|bool $function_to_check Optional. The callback to check for. Default false. |
534 * @param callable|false $callback Optional. The callback to check for. Default false. |
558 * @return bool|int If $function_to_check is omitted, returns boolean for whether the hook has |
535 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has |
559 * anything registered. When checking a specific function, the priority of that |
536 * anything registered. When checking a specific function, the priority |
560 * hook is returned, or false if the function is not attached. When using the |
537 * of that hook is returned, or false if the function is not attached. |
561 * $function_to_check argument, this function may return a non-boolean value |
538 */ |
562 * that evaluates to false (e.g.) 0, so use the === operator for testing the |
539 function has_action( $hook_name, $callback = false ) { |
563 * return value. |
540 return has_filter( $hook_name, $callback ); |
564 */ |
541 } |
565 function has_action( $tag, $function_to_check = false ) { |
542 |
566 return has_filter( $tag, $function_to_check ); |
543 /** |
567 } |
544 * Removes a callback function from an action hook. |
568 |
545 * |
569 /** |
546 * This can be used to remove default functions attached to a specific action |
570 * Removes a function from a specified action hook. |
|
571 * |
|
572 * This function removes a function attached to a specified action hook. This |
|
573 * method can be used to remove default functions attached to a specific filter |
|
574 * hook and possibly replace them with a substitute. |
547 * hook and possibly replace them with a substitute. |
575 * |
548 * |
|
549 * To remove a hook, the `$callback` and `$priority` arguments must match |
|
550 * when the hook was added. This goes for both filters and actions. No warning |
|
551 * will be given on removal failure. |
|
552 * |
576 * @since 1.2.0 |
553 * @since 1.2.0 |
577 * |
554 * |
578 * @param string $tag The action hook to which the function to be removed is hooked. |
555 * @param string $hook_name The action hook to which the function to be removed is hooked. |
579 * @param callable $function_to_remove The name of the function which should be removed. |
556 * @param callable $callback The name of the function which should be removed. |
580 * @param int $priority Optional. The priority of the function. Default 10. |
557 * @param int $priority Optional. The exact priority used when adding the original |
|
558 * action callback. Default 10. |
581 * @return bool Whether the function is removed. |
559 * @return bool Whether the function is removed. |
582 */ |
560 */ |
583 function remove_action( $tag, $function_to_remove, $priority = 10 ) { |
561 function remove_action( $hook_name, $callback, $priority = 10 ) { |
584 return remove_filter( $tag, $function_to_remove, $priority ); |
562 return remove_filter( $hook_name, $callback, $priority ); |
585 } |
563 } |
586 |
564 |
587 /** |
565 /** |
588 * Remove all of the hooks from an action. |
566 * Removes all of the callback functions from an action hook. |
589 * |
567 * |
590 * @since 2.7.0 |
568 * @since 2.7.0 |
591 * |
569 * |
592 * @param string $tag The action to remove hooks from. |
570 * @param string $hook_name The action to remove callbacks from. |
593 * @param int|bool $priority The priority number to remove them from. Default false. |
571 * @param int|false $priority Optional. The priority number to remove them from. |
594 * @return true True when finished. |
572 * Default false. |
595 */ |
573 * @return true Always returns true. |
596 function remove_all_actions( $tag, $priority = false ) { |
574 */ |
597 return remove_all_filters( $tag, $priority ); |
575 function remove_all_actions( $hook_name, $priority = false ) { |
|
576 return remove_all_filters( $hook_name, $priority ); |
|
577 } |
|
578 |
|
579 /** |
|
580 * Retrieves the name of the current action hook. |
|
581 * |
|
582 * @since 3.9.0 |
|
583 * |
|
584 * @return string Hook name of the current action. |
|
585 */ |
|
586 function current_action() { |
|
587 return current_filter(); |
|
588 } |
|
589 |
|
590 /** |
|
591 * Returns whether or not an action hook is currently being processed. |
|
592 * |
|
593 * @since 3.9.0 |
|
594 * |
|
595 * @param string|null $hook_name Optional. Action hook to check. Defaults to null, |
|
596 * which checks if any action is currently being run. |
|
597 * @return bool Whether the action is currently in the stack. |
|
598 */ |
|
599 function doing_action( $hook_name = null ) { |
|
600 return doing_filter( $hook_name ); |
|
601 } |
|
602 |
|
603 /** |
|
604 * Retrieves the number of times an action has been fired during the current request. |
|
605 * |
|
606 * @since 2.1.0 |
|
607 * |
|
608 * @global int[] $wp_actions Stores the number of times each action was triggered. |
|
609 * |
|
610 * @param string $hook_name The name of the action hook. |
|
611 * @return int The number of times the action hook has been fired. |
|
612 */ |
|
613 function did_action( $hook_name ) { |
|
614 global $wp_actions; |
|
615 |
|
616 if ( ! isset( $wp_actions[ $hook_name ] ) ) { |
|
617 return 0; |
|
618 } |
|
619 |
|
620 return $wp_actions[ $hook_name ]; |
598 } |
621 } |
599 |
622 |
600 /** |
623 /** |
601 * Fires functions attached to a deprecated filter hook. |
624 * Fires functions attached to a deprecated filter hook. |
602 * |
625 * |