WordPress.org

Make WordPress Core

Ticket #40906: 40906.patch

File 40906.patch, 3.6 KB (added by wvega, 4 years ago)

Keep $this->callbacks[ $priority ] even if empty.

  • src/wp-includes/class-wp-hook.php

     
    182182                $exists = isset( $this->callbacks[ $priority ][ $function_key ] );
    183183                if ( $exists ) {
    184184                        unset( $this->callbacks[ $priority ][ $function_key ] );
    185                         if ( ! $this->callbacks[ $priority ] ) {
    186                                 unset( $this->callbacks[ $priority ] );
    187                                 if ( $this->nesting_level > 0 ) {
    188                                         $this->resort_active_iterations();
    189                                 }
     185
     186                        if ( $this->nesting_level > 0 ) {
     187                                $this->resort_active_iterations();
    190188                        }
    191189                }
    192190                return $exists;
  • tests/phpunit/tests/hooks/do_action.php

     
    169169                $args = func_get_args();
    170170                $this->events[] = array('action' => __FUNCTION__, 'args'=>$args);
    171171        }
     172
     173        public function test_do_action_with_handler_that_removes_itself() {
     174                $a = new MockAction();
     175                $b = new MockAction();
     176
     177                $this->hook = new WP_Hook();
     178
     179                $this->hook->add_filter( 'handler_that_removes_itself', '__return_null', 10, 1 );
     180                $this->hook->add_filter( 'handler_that_removes_itself', array( $this, '_handler_that_removes_itself' ), 20, 1 );
     181                $this->hook->add_filter( 'handler_that_removes_itself', array( $a, 'action' ), 20 + 1, 1 );
     182                $this->hook->add_filter( 'handler_that_removes_itself', array( $b, 'action' ), 20 + 2, 1 );
     183
     184                $this->hook->do_action( array( null ) );
     185
     186                $this->assertEquals( 1, $a->get_call_count(), 'One of the callbacks was never executed.' );
     187                $this->assertEquals( 1, $b->get_call_count() );
     188        }
     189
     190        public function _handler_that_removes_itself() {
     191                $success = $this->hook->remove_filter( 'handler_that_removes_itself', array( $this, '_handler_that_removes_itself' ), 20 );
     192                $this->assertTrue( $success );
     193        }
    172194}
  • tests/phpunit/tests/hooks/remove_filter.php

     
    1717                $hook->add_filter( $tag, $callback, $priority, $accepted_args );
    1818                $hook->remove_filter( $tag, $callback, $priority );
    1919
    20                 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );
     20                $this->assertEquals( 0, count( $hook->callbacks[ $priority ] ) );
    2121        }
    2222
    2323        public function test_remove_filter_with_object() {
     
    3131                $hook->add_filter( $tag, $callback, $priority, $accepted_args );
    3232                $hook->remove_filter( $tag, $callback, $priority );
    3333
    34                 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );
     34                $this->assertEquals( 0, count( $hook->callbacks[ $priority ] ) );
    3535        }
    3636
    3737        public function test_remove_filter_with_static_method() {
     
    4444                $hook->add_filter( $tag, $callback, $priority, $accepted_args );
    4545                $hook->remove_filter( $tag, $callback, $priority );
    4646
    47                 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );
     47                $this->assertEquals( 0, count( $hook->callbacks[ $priority ] ) );
    4848        }
    4949
    5050        public function test_remove_filters_with_another_at_same_priority() {
     
    7575                $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );
    7676
    7777                $hook->remove_filter( $tag, $callback_one, $priority );
    78                 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );
     78                $this->assertEquals( 0, count( $hook->callbacks[ $priority ] ) );
    7979                $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
    8080        }
    8181}