Make WordPress Core

Ticket #9886: apply_filters_ref_array.diff

File apply_filters_ref_array.diff, 2.0 KB (added by scribu, 15 years ago)

Introduce apply_filters_ref_array()

  • wp-includes/plugin.php

     
    174174}
    175175
    176176/**
     177 * Execute functions hooked on a specific filter hook, specifying arguments in an array.
     178 *
     179 * @see apply_filters() This function is identical, but the arguments passed to the
     180 * functions hooked to <tt>$tag</tt> are supplied using an array.
     181 *
     182 * @package WordPress
     183 * @subpackage Plugin
     184 * @since 3.0
     185 * @global array $wp_filter Stores all of the filters
     186 * @global array $merged_filters Merges the filter hooks using this function.
     187 * @global array $wp_current_filter stores the list of current filters with the current one last
     188 *
     189 * @param string $tag The name of the filter hook.
     190 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
     191 * @return mixed The filtered value after all hooked functions are applied to it.
     192 */
     193function apply_filters_ref_array($tag, $args) {
     194        global $wp_filter, $merged_filters, $wp_current_filter;
     195
     196        $wp_current_filter[] = $tag;
     197
     198        $value = $args[0];
     199
     200        // Do 'all' actions first
     201        if ( isset($wp_filter['all']) ) {
     202                $all_args = func_get_args();
     203                _wp_call_all_hook($all_args);
     204        }
     205
     206        if ( !isset($wp_filter[$tag]) ) {
     207                array_pop($wp_current_filter);
     208                return $value;
     209        }
     210
     211        // Sort
     212        if ( !isset( $merged_filters[ $tag ] ) ) {
     213                ksort($wp_filter[$tag]);
     214                $merged_filters[ $tag ] = true;
     215        }
     216
     217        reset( $wp_filter[ $tag ] );
     218
     219        do {
     220                foreach( (array) current($wp_filter[$tag]) as $the_ )
     221                        if ( !is_null($the_['function']) )
     222                                $value = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     223
     224        } while ( next($wp_filter[$tag]) !== false );
     225
     226        array_pop( $wp_current_filter );
     227
     228        return $value;
     229}
     230
     231/**
    177232 * Removes a function from a specified filter hook.
    178233 *
    179234 * This function removes a function attached to a specified filter hook. This