[wp-hackers] apply_filters, merge_filters and reset()

Jacob wordpress at santosj.name
Thu Nov 8 01:49:57 GMT 2007


Travis Snoozy wrote:
> Hey folks,
>
>   If I could direct some eyes to the (2.3.1) apply_filters function in
> wp-includes/plugin.php, the code doesn't look right. There's a do/while
> construct on (next($wp_filters[$tag]) !== false). The $wp_filters[$tag]
> array is *not* explicitly reset within the function, although resetting
> that array is done as a side effect of merge_filters. In all the other
> functions in this file using the same (next(...)) construct,
> merge_filters is called unconditionally. In apply_filters,
> merge_filters is called only if the $merged_filters global is unset.
>
> This *seems* like it's a bug (normally, array counters are reset before
> attempting to iterate; calling apply_filters twice with the same
> tag would seem to do nothing on the second call), but I'm unsure if the
> code is like this for a specific reason.
>
> I'm hesitant to file a bug directly on this issue, because I'm patching
> that function to use a foreach instead*. The foreach will change the
> semantics, because the array will always be iterated over its full
> length. I want to make sure I'm not changing the *intended* semantics,
> though.
>
>   
I've been following the current Plugin API bugs and enhancements and I 
would hold off on that if I were you.

http://trac.wordpress.org/ticket/5231
http://trac.wordpress.org/ticket/5232

I think there might be a good reason to not use foreach in that function 
because of the inter references and possible changes. If you add a tag 
while iterating, you will break the array reference and it would need to 
be reset anyway. It is quite possible that the while construct was used 
for the very reason that you should not use foreach when the array you 
are iterating over can change. PHP does not forbade you from doing so, 
but it is generally not good in the sense that it might work in one 
environment and then bomb on another.

And yes, if you read the reports, the do_action and companion should 
have checked for the tag in merged_filters before calling merge_filters 
function.

-- 

Jacob Santos

http://www.santosj.name - blog
http://wordpress.svn.dragonu.net/unittest/ - unofficial WP unit test suite.

Also known as darkdragon and santosj on WP trac.



More information about the wp-hackers mailing list