Refactoring WordPress apply_filters Callback Loops for readability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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