Instantly share code, notes, and snippets.

Embed
What would you like to do?
Refactoring WordPress apply_filters Callback Loops for readability
function apply_filters( $tag, $value ) {
// removed for brevity....
// foreach ( (array) $wp_filter[ $tag ] as $priority_level => $callbacks ) {
// $value = call_event_callbacks_for_this_priority( $callbacks, $args, $value );
// }
do {
$callbacks = current( $wp_filter[ $tag ] );
foreach ( (array) $callbacks as $the_ ) {
if ( ! is_null( $the_['function'] ) ) {
$args[1] = $value;
$value = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) );
}
}
} while ( next($wp_filter[$tag]) !== false );
array_pop( $wp_current_filter );
return $value;
}
function call_event_callbacks_for_this_priority( $callbacks, $args, $value ) {
foreach ( (array) $callbacks as $the_ ) {
if ( is_null( $the_['function'] ) ) {
continue;
}
$args[1] = $value;
$callback_arguments = array_slice( $args, 1, (int) $the_['accepted_args'] );
$value = call_user_func_array( $the_['function'], $callback_arguments );
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment