[wp-trac] [WordPress Trac] #41185: current_priority() in WP_Hook broken after adding callback inside same hook

WordPress Trac noreply at wordpress.org
Tue Jun 27 15:04:07 UTC 2017


#41185: current_priority() in WP_Hook broken after adding callback inside same hook
--------------------------+-----------------------------
 Reporter:  kraftner      |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Plugins       |    Version:  4.8
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 If you add a callback for a hook inside a callback on the very same hook
 current_priority() (introduced in
 https://core.trac.wordpress.org/changeset/39430) returns false. Simple
 example:

 {{{#!php
 <?php
 add_action( 'init', function(){

         add_action( 'init', function(){} );

         global $wp_filter;

         $filter_name     = current_filter();
         $filter_instance = $wp_filter[ $filter_name ];
         $priority        = $filter_instance->current_priority();

         if( false === $priority ){
                 echo ":(";
                 exit();
         }

 }, 0);
 }}}

 The problem seems to be that when you add another callback `add_filter()`
 in `WP_Hook` triggers `resort_active_iterations()` which runs a `foreach`
 over `$this->iterations`.
 In PHP<=5.6 (not in PHP7+) `foreach` moves the internal array pointer and
 leaves it at the end of the array. This in turn then makes the `false ===
 current( $this->iterations )` check at the beginning of
 `current_priority()` return `false`.

 So what I think we need to do is ensure that at the the end of
 `resort_active_iterations()` the array pointer of `$this->iterations` is
 back to where it was before.

 As this touches the very very heart of WP I'm looking for feedback and
 confirmation before even trying to come up with a patch.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41185>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list