Skip to content

Instantly share code, notes, and snippets.

@KnowTheCodePro
Created April 10, 2016 17:37
Show Gist options
  • Save KnowTheCodePro/1bc52fc43be86b3f3aff1670a1f04212 to your computer and use it in GitHub Desktop.
Save KnowTheCodePro/1bc52fc43be86b3f3aff1670a1f04212 to your computer and use it in GitHub Desktop.
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