From 5bce147d90101f6e1c8bbde17090be326bec169a Mon Sep 17 00:00:00 2001 From: westi Date: Thu, 18 Mar 2010 21:24:07 +0000 Subject: [PATCH] Introduce apply_filters_ref_array(). See #9886 props scribu. git-svn-id: http://svn.automattic.com/wordpress/trunk@13756 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/plugin.php | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/wp-includes/plugin.php b/wp-includes/plugin.php index bb41bd0ad..d88f6518b 100644 --- a/wp-includes/plugin.php +++ b/wp-includes/plugin.php @@ -173,6 +173,61 @@ function apply_filters($tag, $value) { return $value; } +/** + * Execute functions hooked on a specific filter hook, specifying arguments in an array. + * + * @see apply_filters() This function is identical, but the arguments passed to the + * functions hooked to $tag are supplied using an array. + * + * @package WordPress + * @subpackage Plugin + * @since 3.0 + * @global array $wp_filter Stores all of the filters + * @global array $merged_filters Merges the filter hooks using this function. + * @global array $wp_current_filter stores the list of current filters with the current one last + * + * @param string $tag The name of the filter hook. + * @param array $args The arguments supplied to the functions hooked to $tag + * @return mixed The filtered value after all hooked functions are applied to it. + */ +function apply_filters_ref_array($tag, $args) { + global $wp_filter, $merged_filters, $wp_current_filter; + + $wp_current_filter[] = $tag; + + $value = $args[0]; + + // Do 'all' actions first + if ( isset($wp_filter['all']) ) { + $all_args = func_get_args(); + _wp_call_all_hook($all_args); + } + + if ( !isset($wp_filter[$tag]) ) { + array_pop($wp_current_filter); + return $value; + } + + // Sort + if ( !isset( $merged_filters[ $tag ] ) ) { + ksort($wp_filter[$tag]); + $merged_filters[ $tag ] = true; + } + + reset( $wp_filter[ $tag ] ); + + do { + foreach( (array) current($wp_filter[$tag]) as $the_ ) + if ( !is_null($the_['function']) ) + $value = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); + + } while ( next($wp_filter[$tag]) !== false ); + + array_pop( $wp_current_filter ); + + return $value; +} + /** * Removes a function from a specified filter hook. *