39 $wp_filter = array(); |
42 $wp_filter = array(); |
40 } |
43 } |
41 |
44 |
42 if ( ! isset( $wp_actions ) ) { |
45 if ( ! isset( $wp_actions ) ) { |
43 $wp_actions = array(); |
46 $wp_actions = array(); |
|
47 } |
|
48 |
|
49 if ( ! isset( $wp_filters ) ) { |
|
50 $wp_filters = array(); |
44 } |
51 } |
45 |
52 |
46 if ( ! isset( $wp_current_filter ) ) { |
53 if ( ! isset( $wp_current_filter ) ) { |
47 $wp_current_filter = array(); |
54 $wp_current_filter = array(); |
48 } |
55 } |
153 * @since 0.71 |
160 * @since 0.71 |
154 * @since 6.0.0 Formalized the existing and already documented `...$args` parameter |
161 * @since 6.0.0 Formalized the existing and already documented `...$args` parameter |
155 * by adding it to the function signature. |
162 * by adding it to the function signature. |
156 * |
163 * |
157 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
164 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
|
165 * @global int[] $wp_filters Stores the number of times each filter was triggered. |
158 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
166 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
159 * |
167 * |
160 * @param string $hook_name The name of the filter hook. |
168 * @param string $hook_name The name of the filter hook. |
161 * @param mixed $value The value to filter. |
169 * @param mixed $value The value to filter. |
162 * @param mixed ...$args Additional parameters to pass to the callback functions. |
170 * @param mixed ...$args Optional. Additional parameters to pass to the callback functions. |
163 * @return mixed The filtered value after all hooked functions are applied to it. |
171 * @return mixed The filtered value after all hooked functions are applied to it. |
164 */ |
172 */ |
165 function apply_filters( $hook_name, $value, ...$args ) { |
173 function apply_filters( $hook_name, $value, ...$args ) { |
166 global $wp_filter, $wp_current_filter; |
174 global $wp_filter, $wp_filters, $wp_current_filter; |
|
175 |
|
176 if ( ! isset( $wp_filters[ $hook_name ] ) ) { |
|
177 $wp_filters[ $hook_name ] = 1; |
|
178 } else { |
|
179 ++$wp_filters[ $hook_name ]; |
|
180 } |
167 |
181 |
168 // Do 'all' actions first. |
182 // Do 'all' actions first. |
169 if ( isset( $wp_filter['all'] ) ) { |
183 if ( isset( $wp_filter['all'] ) ) { |
170 $wp_current_filter[] = $hook_name; |
184 $wp_current_filter[] = $hook_name; |
171 |
185 |
202 * |
216 * |
203 * @see apply_filters() This function is identical, but the arguments passed to the |
217 * @see apply_filters() This function is identical, but the arguments passed to the |
204 * functions hooked to `$hook_name` are supplied using an array. |
218 * functions hooked to `$hook_name` are supplied using an array. |
205 * |
219 * |
206 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
220 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. |
|
221 * @global int[] $wp_filters Stores the number of times each filter was triggered. |
207 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
222 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. |
208 * |
223 * |
209 * @param string $hook_name The name of the filter hook. |
224 * @param string $hook_name The name of the filter hook. |
210 * @param array $args The arguments supplied to the functions hooked to `$hook_name`. |
225 * @param array $args The arguments supplied to the functions hooked to `$hook_name`. |
211 * @return mixed The filtered value after all hooked functions are applied to it. |
226 * @return mixed The filtered value after all hooked functions are applied to it. |
212 */ |
227 */ |
213 function apply_filters_ref_array( $hook_name, $args ) { |
228 function apply_filters_ref_array( $hook_name, $args ) { |
214 global $wp_filter, $wp_current_filter; |
229 global $wp_filter, $wp_filters, $wp_current_filter; |
|
230 |
|
231 if ( ! isset( $wp_filters[ $hook_name ] ) ) { |
|
232 $wp_filters[ $hook_name ] = 1; |
|
233 } else { |
|
234 ++$wp_filters[ $hook_name ]; |
|
235 } |
215 |
236 |
216 // Do 'all' actions first. |
237 // Do 'all' actions first. |
217 if ( isset( $wp_filter['all'] ) ) { |
238 if ( isset( $wp_filter['all'] ) ) { |
218 $wp_current_filter[] = $hook_name; |
239 $wp_current_filter[] = $hook_name; |
219 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
240 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
347 } |
368 } |
348 |
369 |
349 /** |
370 /** |
350 * Returns whether or not a filter hook is currently being processed. |
371 * Returns whether or not a filter hook is currently being processed. |
351 * |
372 * |
352 * The function current_filter() only returns the most recent filter or action |
373 * The function current_filter() only returns the most recent filter being executed. |
353 * being executed. did_action() returns true once the action is initially |
374 * did_filter() returns the number of times a filter has been applied during |
354 * processed. |
375 * the current request. |
355 * |
376 * |
356 * This function allows detection for any filter currently being executed |
377 * 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 |
378 * (regardless of whether it's the most recent filter to fire, in the case of |
358 * hooks called from hook callbacks) to be verified. |
379 * hooks called from hook callbacks) to be verified. |
359 * |
380 * |
360 * @since 3.9.0 |
381 * @since 3.9.0 |
361 * |
382 * |
362 * @see current_filter() |
383 * @see current_filter() |
363 * @see did_action() |
384 * @see did_filter() |
364 * @global string[] $wp_current_filter Current filter. |
385 * @global string[] $wp_current_filter Current filter. |
365 * |
386 * |
366 * @param null|string $hook_name Optional. Filter hook to check. Defaults to null, |
387 * @param string|null $hook_name Optional. Filter hook to check. Defaults to null, |
367 * which checks if any filter is currently being run. |
388 * which checks if any filter is currently being run. |
368 * @return bool Whether the filter is currently in the stack. |
389 * @return bool Whether the filter is currently in the stack. |
369 */ |
390 */ |
370 function doing_filter( $hook_name = null ) { |
391 function doing_filter( $hook_name = null ) { |
371 global $wp_current_filter; |
392 global $wp_current_filter; |
373 if ( null === $hook_name ) { |
394 if ( null === $hook_name ) { |
374 return ! empty( $wp_current_filter ); |
395 return ! empty( $wp_current_filter ); |
375 } |
396 } |
376 |
397 |
377 return in_array( $hook_name, $wp_current_filter, true ); |
398 return in_array( $hook_name, $wp_current_filter, true ); |
|
399 } |
|
400 |
|
401 /** |
|
402 * Retrieves the number of times a filter has been applied during the current request. |
|
403 * |
|
404 * @since 6.1.0 |
|
405 * |
|
406 * @global int[] $wp_filters Stores the number of times each filter was triggered. |
|
407 * |
|
408 * @param string $hook_name The name of the filter hook. |
|
409 * @return int The number of times the filter hook has been applied. |
|
410 */ |
|
411 function did_filter( $hook_name ) { |
|
412 global $wp_filters; |
|
413 |
|
414 if ( ! isset( $wp_filters[ $hook_name ] ) ) { |
|
415 return 0; |
|
416 } |
|
417 |
|
418 return $wp_filters[ $hook_name ]; |
378 } |
419 } |
379 |
420 |
380 /** |
421 /** |
381 * Adds a callback function to an action hook. |
422 * Adds a callback function to an action hook. |
382 * |
423 * |
422 * * Trigger the actions by calling the 'example_callback()' function |
463 * * Trigger the actions by calling the 'example_callback()' function |
423 * * that's hooked onto `example_action` above. |
464 * * that's hooked onto `example_action` above. |
424 * * |
465 * * |
425 * * - 'example_action' is the action hook. |
466 * * - 'example_action' is the action hook. |
426 * * - $arg1 and $arg2 are the additional arguments passed to the callback. |
467 * * - $arg1 and $arg2 are the additional arguments passed to the callback. |
427 * $value = do_action( 'example_action', $arg1, $arg2 ); |
468 * do_action( 'example_action', $arg1, $arg2 ); |
428 * |
469 * |
429 * @since 1.2.0 |
470 * @since 1.2.0 |
430 * @since 5.3.0 Formalized the existing and already documented `...$arg` parameter |
471 * @since 5.3.0 Formalized the existing and already documented `...$arg` parameter |
431 * by adding it to the function signature. |
472 * by adding it to the function signature. |
432 * |
473 * |
532 * When using the `$callback` argument, this function may return a non-boolean value |
573 * 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. |
574 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. |
534 * |
575 * |
535 * @since 2.5.0 |
576 * @since 2.5.0 |
536 * |
577 * |
537 * @see has_filter() has_action() is an alias of has_filter(). |
578 * @see has_filter() This function is an alias of has_filter(). |
538 * |
579 * |
539 * @param string $hook_name The name of the action hook. |
580 * @param string $hook_name The name of the action hook. |
540 * @param callable|string|array|false $callback Optional. The callback to check for. |
581 * @param callable|string|array|false $callback Optional. The callback to check for. |
541 * This function can be called unconditionally to speculatively check |
582 * This function can be called unconditionally to speculatively check |
542 * a callback that may or may not exist. Default false. |
583 * a callback that may or may not exist. Default false. |
598 } |
639 } |
599 |
640 |
600 /** |
641 /** |
601 * Returns whether or not an action hook is currently being processed. |
642 * Returns whether or not an action hook is currently being processed. |
602 * |
643 * |
|
644 * The function current_action() only returns the most recent action being executed. |
|
645 * did_action() returns the number of times an action has been fired during |
|
646 * the current request. |
|
647 * |
|
648 * This function allows detection for any action currently being executed |
|
649 * (regardless of whether it's the most recent action to fire, in the case of |
|
650 * hooks called from hook callbacks) to be verified. |
|
651 * |
603 * @since 3.9.0 |
652 * @since 3.9.0 |
|
653 * |
|
654 * @see current_action() |
|
655 * @see did_action() |
604 * |
656 * |
605 * @param string|null $hook_name Optional. Action hook to check. Defaults to null, |
657 * @param string|null $hook_name Optional. Action hook to check. Defaults to null, |
606 * which checks if any action is currently being run. |
658 * which checks if any action is currently being run. |
607 * @return bool Whether the action is currently in the stack. |
659 * @return bool Whether the action is currently in the stack. |
608 */ |
660 */ |
653 * @param string $hook_name The name of the filter hook. |
705 * @param string $hook_name The name of the filter hook. |
654 * @param array $args Array of additional function arguments to be passed to apply_filters(). |
706 * @param array $args Array of additional function arguments to be passed to apply_filters(). |
655 * @param string $version The version of WordPress that deprecated the hook. |
707 * @param string $version The version of WordPress that deprecated the hook. |
656 * @param string $replacement Optional. The hook that should have been used. Default empty. |
708 * @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. |
709 * @param string $message Optional. A message regarding the change. Default empty. |
|
710 * @return mixed The filtered value after all hooked functions are applied to it. |
658 */ |
711 */ |
659 function apply_filters_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) { |
712 function apply_filters_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) { |
660 if ( ! has_filter( $hook_name ) ) { |
713 if ( ! has_filter( $hook_name ) ) { |
661 return $args[0]; |
714 return $args[0]; |
662 } |
715 } |
716 $file = wp_normalize_path( $file ); |
769 $file = wp_normalize_path( $file ); |
717 |
770 |
718 arsort( $wp_plugin_paths ); |
771 arsort( $wp_plugin_paths ); |
719 |
772 |
720 foreach ( $wp_plugin_paths as $dir => $realdir ) { |
773 foreach ( $wp_plugin_paths as $dir => $realdir ) { |
721 if ( strpos( $file, $realdir ) === 0 ) { |
774 if ( str_starts_with( $file, $realdir ) ) { |
722 $file = $dir . substr( $file, strlen( $realdir ) ); |
775 $file = $dir . substr( $file, strlen( $realdir ) ); |
723 } |
776 } |
724 } |
777 } |
725 |
778 |
726 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); |
779 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); |
910 |
963 |
911 $wp_filter['all']->do_all_hook( $args ); |
964 $wp_filter['all']->do_all_hook( $args ); |
912 } |
965 } |
913 |
966 |
914 /** |
967 /** |
915 * Builds Unique ID for storage and retrieval. |
968 * Builds a unique string ID for a hook callback function. |
916 * |
|
917 * The old way to serialize the callback caused issues and this function is the |
|
918 * solution. It works by checking for objects and creating a new property in |
|
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 * |
969 * |
928 * Functions and static method callbacks are just returned as strings and |
970 * Functions and static method callbacks are just returned as strings and |
929 * shouldn't have any speed penalty. |
971 * shouldn't have any speed penalty. |
930 * |
972 * |
931 * @link https://core.trac.wordpress.org/ticket/3875 |
973 * @link https://core.trac.wordpress.org/ticket/3875 |