[wp-trac] [WordPress Trac] #17817: do_action/apply_filters/etc. recursion on same filter kills underlying call

WordPress Trac noreply at wordpress.org
Sat Sep 3 14:54:34 UTC 2016


#17817: do_action/apply_filters/etc. recursion on same filter kills underlying call
-------------------------------------------------+-------------------------
 Reporter:  kernfel                              |       Owner:  pento
     Type:  defect (bug)                         |      Status:  reviewing
 Priority:  normal                               |   Milestone:  4.7
Component:  Plugins                              |     Version:  2.2
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch needs-testing has-unit-    |     Focuses:
  tests                                          |
-------------------------------------------------+-------------------------

Comment (by noplanman):

 Hi @pento

 Happy to test and get this into core =)

 With filters the tests work nicely. But unfortunately my problem persists
 using your latest code changes.

 Using `do_action` doesn't seem to work. Here my tests for you to play
 with:

 {{{
 public function test_remove_and_add_last_action_1() {
   $this->hook = new Wp_Hook();

   $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10,
 0 );

   $this->hook->add_filter( 'remove_and_add', array( $this,
 '_action_remove_and_add1' ), 11, 0 );
   $this->hook->add_filter( 'remove_and_add', array( $this,
 '_action_remove_and_add2' ), 12, 0 );

   $this->action_output = '';
   $this->hook->do_action( [] );

   // Works! First add1, then add2. ($this->action_output = '12')
   $this->assertSame( '12', $this->action_output );
 }

 public function test_remove_and_add_last_action_2() {
   $this->hook = new Wp_Hook();

   $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10,
 0 );

   $this->hook->add_filter( 'remove_and_add', array( $this,
 '_action_remove_and_add1' ), 13, 0 );
   $this->hook->add_filter( 'remove_and_add', array( $this,
 '_action_remove_and_add2' ), 12, 0 );

   $this->action_output = '';
   $this->hook->do_action( [] );

   // Fails! First add2, but no add1. ($this->action_output = '2')
   $this->assertSame( '21', $this->action_output );
 }

 public function _action_remove_and_add1() {
   $this->action_output .= '1';
 }

 public function _action_remove_and_add2() {
   $this->hook->remove_filter( 'remove_and_add', array( $this,
 '_action_remove_and_add2' ), 12 );
   $this->hook->add_filter( 'remove_and_add', array( $this,
 '_action_remove_and_add2' ), 12, 0 );

   $this->action_output .= '2';
 }
 }}}

 Hope this helps.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/17817#comment:199>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list